4 #include "host-utils.h"
5 #if !defined(CONFIG_USER_ONLY)
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;
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;
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;
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;
81 case ARM_CPUID_ARM1136:
82 /* This is the 1136 r1, which is a v6K core */
83 set_feature(env, ARM_FEATURE_V6K);
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;
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;
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;
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;
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;
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;
187 case ARM_CPUID_CORTEXM3:
188 set_feature(env, ARM_FEATURE_V7);
189 set_feature(env, ARM_FEATURE_M);
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);
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;
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;
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;
235 case ARM_CPUID_SA1100:
236 case ARM_CPUID_SA1110:
237 set_feature(env, ARM_FEATURE_STRONGARM);
238 env->cp15.c1_sys = 0x00000070;
241 cpu_abort(env, "Bad CPU ID: %x\n", id);
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);
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 /* TODO Move contents into arm_cpu_reset() in cpu.c,
282 * once cpu_reset_model_id() is eliminated,
283 * and then forward to cpu_reset() here.
285 void cpu_state_reset(CPUARMState *env)
290 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
291 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
292 log_cpu_state(env, 0);
295 id = env->cp15.c0_cpuid;
296 tmp = env->cp15.c15_config_base_address;
297 memset(env, 0, offsetof(CPUARMState, breakpoints));
299 cpu_reset_model_id(env, id);
300 env->cp15.c15_config_base_address = tmp;
301 #if defined (CONFIG_USER_ONLY)
302 env->uncached_cpsr = ARM_CPU_MODE_USR;
303 /* For user mode we must enable access to coprocessors */
304 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
305 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
306 env->cp15.c15_cpar = 3;
307 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
308 env->cp15.c15_cpar = 1;
311 /* SVC mode with interrupts disabled. */
312 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
313 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
314 clear at reset. Initial SP and PC are loaded from ROM. */
318 env->uncached_cpsr &= ~CPSR_I;
321 /* We should really use ldl_phys here, in case the guest
322 modified flash and reset itself. However images
323 loaded via -kernel have not been copied yet, so load the
324 values directly from there. */
325 env->regs[13] = ldl_p(rom);
328 env->regs[15] = pc & ~1;
331 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
332 env->cp15.c2_base_mask = 0xffffc000u;
333 /* v7 performance monitor control register: same implementor
334 * field as main ID register, and we implement no event counters.
336 env->cp15.c9_pmcr = (id & 0xff000000);
338 set_flush_to_zero(1, &env->vfp.standard_fp_status);
339 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
340 set_default_nan_mode(1, &env->vfp.standard_fp_status);
341 set_float_detect_tininess(float_tininess_before_rounding,
342 &env->vfp.fp_status);
343 set_float_detect_tininess(float_tininess_before_rounding,
344 &env->vfp.standard_fp_status);
346 /* Reset is a state change for some CPUARMState fields which we
347 * bake assumptions about into translated code, so we need to
353 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
357 /* VFP data registers are always little-endian. */
358 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
360 stfq_le_p(buf, env->vfp.regs[reg]);
363 if (arm_feature(env, ARM_FEATURE_NEON)) {
364 /* Aliases for Q regs. */
367 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
368 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
372 switch (reg - nregs) {
373 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
374 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
375 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
380 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
384 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
386 env->vfp.regs[reg] = ldfq_le_p(buf);
389 if (arm_feature(env, ARM_FEATURE_NEON)) {
392 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
393 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
397 switch (reg - nregs) {
398 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
399 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
400 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
405 CPUARMState *cpu_arm_init(const char *cpu_model)
410 static int inited = 0;
412 id = cpu_arm_find_by_name(cpu_model);
415 cpu = ARM_CPU(object_new(TYPE_ARM_CPU));
418 if (tcg_enabled() && !inited) {
420 arm_translate_init();
423 env->cpu_model_str = cpu_model;
424 env->cp15.c0_cpuid = id;
425 cpu_state_reset(env);
426 if (arm_feature(env, ARM_FEATURE_NEON)) {
427 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
428 51, "arm-neon.xml", 0);
429 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
430 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
431 35, "arm-vfp3.xml", 0);
432 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
433 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
434 19, "arm-vfp.xml", 0);
445 static const struct arm_cpu_t arm_cpu_names[] = {
446 { ARM_CPUID_ARM926, "arm926"},
447 { ARM_CPUID_ARM946, "arm946"},
448 { ARM_CPUID_ARM1026, "arm1026"},
449 { ARM_CPUID_ARM1136, "arm1136"},
450 { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
451 { ARM_CPUID_ARM1176, "arm1176"},
452 { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
453 { ARM_CPUID_CORTEXM3, "cortex-m3"},
454 { ARM_CPUID_CORTEXA8, "cortex-a8"},
455 { ARM_CPUID_CORTEXA9, "cortex-a9"},
456 { ARM_CPUID_CORTEXA15, "cortex-a15" },
457 { ARM_CPUID_TI925T, "ti925t" },
458 { ARM_CPUID_PXA250, "pxa250" },
459 { ARM_CPUID_SA1100, "sa1100" },
460 { ARM_CPUID_SA1110, "sa1110" },
461 { ARM_CPUID_PXA255, "pxa255" },
462 { ARM_CPUID_PXA260, "pxa260" },
463 { ARM_CPUID_PXA261, "pxa261" },
464 { ARM_CPUID_PXA262, "pxa262" },
465 { ARM_CPUID_PXA270, "pxa270" },
466 { ARM_CPUID_PXA270_A0, "pxa270-a0" },
467 { ARM_CPUID_PXA270_A1, "pxa270-a1" },
468 { ARM_CPUID_PXA270_B0, "pxa270-b0" },
469 { ARM_CPUID_PXA270_B1, "pxa270-b1" },
470 { ARM_CPUID_PXA270_C0, "pxa270-c0" },
471 { ARM_CPUID_PXA270_C5, "pxa270-c5" },
472 { ARM_CPUID_ANY, "any"},
476 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
480 (*cpu_fprintf)(f, "Available CPUs:\n");
481 for (i = 0; arm_cpu_names[i].name; i++) {
482 (*cpu_fprintf)(f, " %s\n", arm_cpu_names[i].name);
486 /* return 0 if not found */
487 static uint32_t cpu_arm_find_by_name(const char *name)
493 for (i = 0; arm_cpu_names[i].name; i++) {
494 if (strcmp(name, arm_cpu_names[i].name) == 0) {
495 id = arm_cpu_names[i].id;
502 static int bad_mode_switch(CPUARMState *env, int mode)
504 /* Return true if it is not valid for us to switch to
505 * this CPU mode (ie all the UNPREDICTABLE cases in
506 * the ARM ARM CPSRWriteByInstr pseudocode).
509 case ARM_CPU_MODE_USR:
510 case ARM_CPU_MODE_SYS:
511 case ARM_CPU_MODE_SVC:
512 case ARM_CPU_MODE_ABT:
513 case ARM_CPU_MODE_UND:
514 case ARM_CPU_MODE_IRQ:
515 case ARM_CPU_MODE_FIQ:
522 uint32_t cpsr_read(CPUARMState *env)
526 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
527 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
528 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
529 | ((env->condexec_bits & 0xfc) << 8)
533 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
535 if (mask & CPSR_NZCV) {
536 env->ZF = (~val) & CPSR_Z;
538 env->CF = (val >> 29) & 1;
539 env->VF = (val << 3) & 0x80000000;
542 env->QF = ((val & CPSR_Q) != 0);
544 env->thumb = ((val & CPSR_T) != 0);
545 if (mask & CPSR_IT_0_1) {
546 env->condexec_bits &= ~3;
547 env->condexec_bits |= (val >> 25) & 3;
549 if (mask & CPSR_IT_2_7) {
550 env->condexec_bits &= 3;
551 env->condexec_bits |= (val >> 8) & 0xfc;
553 if (mask & CPSR_GE) {
554 env->GE = (val >> 16) & 0xf;
557 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
558 if (bad_mode_switch(env, val & CPSR_M)) {
559 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
560 * We choose to ignore the attempt and leave the CPSR M field
565 switch_mode(env, val & CPSR_M);
568 mask &= ~CACHED_CPSR_BITS;
569 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
572 /* Sign/zero extend */
573 uint32_t HELPER(sxtb16)(uint32_t x)
576 res = (uint16_t)(int8_t)x;
577 res |= (uint32_t)(int8_t)(x >> 16) << 16;
581 uint32_t HELPER(uxtb16)(uint32_t x)
584 res = (uint16_t)(uint8_t)x;
585 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
589 uint32_t HELPER(clz)(uint32_t x)
594 int32_t HELPER(sdiv)(int32_t num, int32_t den)
598 if (num == INT_MIN && den == -1)
603 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
610 uint32_t HELPER(rbit)(uint32_t x)
612 x = ((x & 0xff000000) >> 24)
613 | ((x & 0x00ff0000) >> 8)
614 | ((x & 0x0000ff00) << 8)
615 | ((x & 0x000000ff) << 24);
616 x = ((x & 0xf0f0f0f0) >> 4)
617 | ((x & 0x0f0f0f0f) << 4);
618 x = ((x & 0x88888888) >> 3)
619 | ((x & 0x44444444) >> 1)
620 | ((x & 0x22222222) << 1)
621 | ((x & 0x11111111) << 3);
625 uint32_t HELPER(abs)(uint32_t x)
627 return ((int32_t)x < 0) ? -x : x;
630 #if defined(CONFIG_USER_ONLY)
632 void do_interrupt (CPUARMState *env)
634 env->exception_index = -1;
637 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
641 env->exception_index = EXCP_PREFETCH_ABORT;
642 env->cp15.c6_insn = address;
644 env->exception_index = EXCP_DATA_ABORT;
645 env->cp15.c6_data = address;
650 /* These should probably raise undefined insn exceptions. */
651 void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
653 int op1 = (insn >> 8) & 0xf;
654 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
658 uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
660 int op1 = (insn >> 8) & 0xf;
661 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
665 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
667 cpu_abort(env, "cp15 insn %08x\n", insn);
670 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
672 cpu_abort(env, "cp15 insn %08x\n", insn);
675 /* These should probably raise undefined insn exceptions. */
676 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
678 cpu_abort(env, "v7m_mrs %d\n", reg);
681 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
683 cpu_abort(env, "v7m_mrs %d\n", reg);
687 void switch_mode(CPUARMState *env, int mode)
689 if (mode != ARM_CPU_MODE_USR)
690 cpu_abort(env, "Tried to switch out of user mode\n");
693 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
695 cpu_abort(env, "banked r13 write\n");
698 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
700 cpu_abort(env, "banked r13 read\n");
706 /* Map CPU modes onto saved register banks. */
707 static inline int bank_number(CPUARMState *env, int mode)
710 case ARM_CPU_MODE_USR:
711 case ARM_CPU_MODE_SYS:
713 case ARM_CPU_MODE_SVC:
715 case ARM_CPU_MODE_ABT:
717 case ARM_CPU_MODE_UND:
719 case ARM_CPU_MODE_IRQ:
721 case ARM_CPU_MODE_FIQ:
724 cpu_abort(env, "Bad mode %x\n", mode);
728 void switch_mode(CPUARMState *env, int mode)
733 old_mode = env->uncached_cpsr & CPSR_M;
734 if (mode == old_mode)
737 if (old_mode == ARM_CPU_MODE_FIQ) {
738 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
739 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
740 } else if (mode == ARM_CPU_MODE_FIQ) {
741 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
742 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
745 i = bank_number(env, old_mode);
746 env->banked_r13[i] = env->regs[13];
747 env->banked_r14[i] = env->regs[14];
748 env->banked_spsr[i] = env->spsr;
750 i = bank_number(env, mode);
751 env->regs[13] = env->banked_r13[i];
752 env->regs[14] = env->banked_r14[i];
753 env->spsr = env->banked_spsr[i];
756 static void v7m_push(CPUARMState *env, uint32_t val)
759 stl_phys(env->regs[13], val);
762 static uint32_t v7m_pop(CPUARMState *env)
765 val = ldl_phys(env->regs[13]);
770 /* Switch to V7M main or process stack pointer. */
771 static void switch_v7m_sp(CPUARMState *env, int process)
774 if (env->v7m.current_sp != process) {
775 tmp = env->v7m.other_sp;
776 env->v7m.other_sp = env->regs[13];
778 env->v7m.current_sp = process;
782 static void do_v7m_exception_exit(CPUARMState *env)
787 type = env->regs[15];
788 if (env->v7m.exception != 0)
789 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
791 /* Switch to the target stack. */
792 switch_v7m_sp(env, (type & 4) != 0);
794 env->regs[0] = v7m_pop(env);
795 env->regs[1] = v7m_pop(env);
796 env->regs[2] = v7m_pop(env);
797 env->regs[3] = v7m_pop(env);
798 env->regs[12] = v7m_pop(env);
799 env->regs[14] = v7m_pop(env);
800 env->regs[15] = v7m_pop(env);
802 xpsr_write(env, xpsr, 0xfffffdff);
803 /* Undo stack alignment. */
806 /* ??? The exception return type specifies Thread/Handler mode. However
807 this is also implied by the xPSR value. Not sure what to do
808 if there is a mismatch. */
809 /* ??? Likewise for mismatches between the CONTROL register and the stack
813 static void do_interrupt_v7m(CPUARMState *env)
815 uint32_t xpsr = xpsr_read(env);
820 if (env->v7m.current_sp)
822 if (env->v7m.exception == 0)
825 /* For exceptions we just mark as pending on the NVIC, and let that
827 /* TODO: Need to escalate if the current priority is higher than the
828 one we're raising. */
829 switch (env->exception_index) {
831 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
835 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
837 case EXCP_PREFETCH_ABORT:
838 case EXCP_DATA_ABORT:
839 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
842 if (semihosting_enabled) {
844 nr = lduw_code(env->regs[15]) & 0xff;
847 env->regs[0] = do_arm_semihosting(env);
851 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
854 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
856 case EXCP_EXCEPTION_EXIT:
857 do_v7m_exception_exit(env);
860 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
861 return; /* Never happens. Keep compiler happy. */
864 /* Align stack pointer. */
865 /* ??? Should only do this if Configuration Control Register
866 STACKALIGN bit is set. */
867 if (env->regs[13] & 4) {
871 /* Switch to the handler mode. */
873 v7m_push(env, env->regs[15]);
874 v7m_push(env, env->regs[14]);
875 v7m_push(env, env->regs[12]);
876 v7m_push(env, env->regs[3]);
877 v7m_push(env, env->regs[2]);
878 v7m_push(env, env->regs[1]);
879 v7m_push(env, env->regs[0]);
880 switch_v7m_sp(env, 0);
882 env->condexec_bits = 0;
884 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
885 env->regs[15] = addr & 0xfffffffe;
886 env->thumb = addr & 1;
889 /* Handle a CPU exception. */
890 void do_interrupt(CPUARMState *env)
898 do_interrupt_v7m(env);
901 /* TODO: Vectored interrupt controller. */
902 switch (env->exception_index) {
904 new_mode = ARM_CPU_MODE_UND;
913 if (semihosting_enabled) {
914 /* Check for semihosting interrupt. */
916 mask = lduw_code(env->regs[15] - 2) & 0xff;
918 mask = ldl_code(env->regs[15] - 4) & 0xffffff;
920 /* Only intercept calls from privileged modes, to provide some
921 semblance of security. */
922 if (((mask == 0x123456 && !env->thumb)
923 || (mask == 0xab && env->thumb))
924 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
925 env->regs[0] = do_arm_semihosting(env);
929 new_mode = ARM_CPU_MODE_SVC;
932 /* The PC already points to the next instruction. */
936 /* See if this is a semihosting syscall. */
937 if (env->thumb && semihosting_enabled) {
938 mask = lduw_code(env->regs[15]) & 0xff;
940 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
942 env->regs[0] = do_arm_semihosting(env);
946 env->cp15.c5_insn = 2;
947 /* Fall through to prefetch abort. */
948 case EXCP_PREFETCH_ABORT:
949 new_mode = ARM_CPU_MODE_ABT;
951 mask = CPSR_A | CPSR_I;
954 case EXCP_DATA_ABORT:
955 new_mode = ARM_CPU_MODE_ABT;
957 mask = CPSR_A | CPSR_I;
961 new_mode = ARM_CPU_MODE_IRQ;
963 /* Disable IRQ and imprecise data aborts. */
964 mask = CPSR_A | CPSR_I;
968 new_mode = ARM_CPU_MODE_FIQ;
970 /* Disable FIQ, IRQ and imprecise data aborts. */
971 mask = CPSR_A | CPSR_I | CPSR_F;
975 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
976 return; /* Never happens. Keep compiler happy. */
979 if (env->cp15.c1_sys & (1 << 13)) {
982 switch_mode (env, new_mode);
983 env->spsr = cpsr_read(env);
985 env->condexec_bits = 0;
986 /* Switch to the new mode, and to the correct instruction set. */
987 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
988 env->uncached_cpsr |= mask;
989 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
990 * and we should just guard the thumb mode on V4 */
991 if (arm_feature(env, ARM_FEATURE_V4T)) {
992 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
994 env->regs[14] = env->regs[15] + offset;
995 env->regs[15] = addr;
996 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
999 /* Check section/page access permissions.
1000 Returns the page protection flags, or zero if the access is not
1002 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
1003 int access_type, int is_user)
1007 if (domain_prot == 3) {
1008 return PAGE_READ | PAGE_WRITE;
1011 if (access_type == 1)
1014 prot_ro = PAGE_READ;
1018 if (access_type == 1)
1020 switch ((env->cp15.c1_sys >> 8) & 3) {
1022 return is_user ? 0 : PAGE_READ;
1029 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
1034 return PAGE_READ | PAGE_WRITE;
1036 return PAGE_READ | PAGE_WRITE;
1037 case 4: /* Reserved. */
1040 return is_user ? 0 : prot_ro;
1044 if (!arm_feature (env, ARM_FEATURE_V6K))
1052 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
1056 if (address & env->cp15.c2_mask)
1057 table = env->cp15.c2_base1 & 0xffffc000;
1059 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1061 table |= (address >> 18) & 0x3ffc;
1065 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
1066 int is_user, uint32_t *phys_ptr, int *prot,
1067 target_ulong *page_size)
1078 /* Pagetable walk. */
1079 /* Lookup l1 descriptor. */
1080 table = get_level1_table_address(env, address);
1081 desc = ldl_phys(table);
1083 domain = (desc >> 5) & 0x0f;
1084 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1086 /* Section translation fault. */
1090 if (domain_prot == 0 || domain_prot == 2) {
1092 code = 9; /* Section domain fault. */
1094 code = 11; /* Page domain fault. */
1099 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1100 ap = (desc >> 10) & 3;
1102 *page_size = 1024 * 1024;
1104 /* Lookup l2 entry. */
1106 /* Coarse pagetable. */
1107 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1109 /* Fine pagetable. */
1110 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1112 desc = ldl_phys(table);
1114 case 0: /* Page translation fault. */
1117 case 1: /* 64k page. */
1118 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1119 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1120 *page_size = 0x10000;
1122 case 2: /* 4k page. */
1123 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1124 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1125 *page_size = 0x1000;
1127 case 3: /* 1k page. */
1129 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1130 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1132 /* Page translation fault. */
1137 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1139 ap = (desc >> 4) & 3;
1143 /* Never happens, but compiler isn't smart enough to tell. */
1148 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1150 /* Access permission fault. */
1154 *phys_ptr = phys_addr;
1157 return code | (domain << 4);
1160 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
1161 int is_user, uint32_t *phys_ptr, int *prot,
1162 target_ulong *page_size)
1174 /* Pagetable walk. */
1175 /* Lookup l1 descriptor. */
1176 table = get_level1_table_address(env, address);
1177 desc = ldl_phys(table);
1180 /* Section translation fault. */
1184 } else if (type == 2 && (desc & (1 << 18))) {
1188 /* Section or page. */
1189 domain = (desc >> 5) & 0x0f;
1191 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1192 if (domain_prot == 0 || domain_prot == 2) {
1194 code = 9; /* Section domain fault. */
1196 code = 11; /* Page domain fault. */
1200 if (desc & (1 << 18)) {
1202 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1203 *page_size = 0x1000000;
1206 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1207 *page_size = 0x100000;
1209 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1210 xn = desc & (1 << 4);
1213 /* Lookup l2 entry. */
1214 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1215 desc = ldl_phys(table);
1216 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1218 case 0: /* Page translation fault. */
1221 case 1: /* 64k page. */
1222 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1223 xn = desc & (1 << 15);
1224 *page_size = 0x10000;
1226 case 2: case 3: /* 4k page. */
1227 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1229 *page_size = 0x1000;
1232 /* Never happens, but compiler isn't smart enough to tell. */
1237 if (domain_prot == 3) {
1238 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1240 if (xn && access_type == 2)
1243 /* The simplified model uses AP[0] as an access control bit. */
1244 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1245 /* Access flag fault. */
1246 code = (code == 15) ? 6 : 3;
1249 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1251 /* Access permission fault. */
1258 *phys_ptr = phys_addr;
1261 return code | (domain << 4);
1264 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1265 int is_user, uint32_t *phys_ptr, int *prot)
1271 *phys_ptr = address;
1272 for (n = 7; n >= 0; n--) {
1273 base = env->cp15.c6_region[n];
1274 if ((base & 1) == 0)
1276 mask = 1 << ((base >> 1) & 0x1f);
1277 /* Keep this shift separate from the above to avoid an
1278 (undefined) << 32. */
1279 mask = (mask << 1) - 1;
1280 if (((base ^ address) & ~mask) == 0)
1286 if (access_type == 2) {
1287 mask = env->cp15.c5_insn;
1289 mask = env->cp15.c5_data;
1291 mask = (mask >> (n * 4)) & 0xf;
1298 *prot = PAGE_READ | PAGE_WRITE;
1303 *prot |= PAGE_WRITE;
1306 *prot = PAGE_READ | PAGE_WRITE;
1317 /* Bad permission. */
1324 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1325 int access_type, int is_user,
1326 uint32_t *phys_ptr, int *prot,
1327 target_ulong *page_size)
1329 /* Fast Context Switch Extension. */
1330 if (address < 0x02000000)
1331 address += env->cp15.c13_fcse;
1333 if ((env->cp15.c1_sys & 1) == 0) {
1334 /* MMU/MPU disabled. */
1335 *phys_ptr = address;
1336 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1337 *page_size = TARGET_PAGE_SIZE;
1339 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1340 *page_size = TARGET_PAGE_SIZE;
1341 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1343 } else if (env->cp15.c1_sys & (1 << 23)) {
1344 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1347 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1352 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1353 int access_type, int mmu_idx)
1356 target_ulong page_size;
1360 is_user = mmu_idx == MMU_USER_IDX;
1361 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1364 /* Map a single [sub]page. */
1365 phys_addr &= ~(uint32_t)0x3ff;
1366 address &= ~(uint32_t)0x3ff;
1367 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1371 if (access_type == 2) {
1372 env->cp15.c5_insn = ret;
1373 env->cp15.c6_insn = address;
1374 env->exception_index = EXCP_PREFETCH_ABORT;
1376 env->cp15.c5_data = ret;
1377 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1378 env->cp15.c5_data |= (1 << 11);
1379 env->cp15.c6_data = address;
1380 env->exception_index = EXCP_DATA_ABORT;
1385 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1388 target_ulong page_size;
1392 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1400 void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
1402 int cp_num = (insn >> 8) & 0xf;
1403 int cp_info = (insn >> 5) & 7;
1404 int src = (insn >> 16) & 0xf;
1405 int operand = insn & 0xf;
1407 if (env->cp[cp_num].cp_write)
1408 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1409 cp_info, src, operand, val);
1412 uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
1414 int cp_num = (insn >> 8) & 0xf;
1415 int cp_info = (insn >> 5) & 7;
1416 int dest = (insn >> 16) & 0xf;
1417 int operand = insn & 0xf;
1419 if (env->cp[cp_num].cp_read)
1420 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1421 cp_info, dest, operand);
1425 /* Return basic MPU access permission bits. */
1426 static uint32_t simple_mpu_ap_bits(uint32_t val)
1433 for (i = 0; i < 16; i += 2) {
1434 ret |= (val >> i) & mask;
1440 /* Pad basic MPU access permission bits to extended format. */
1441 static uint32_t extended_mpu_ap_bits(uint32_t val)
1448 for (i = 0; i < 16; i += 2) {
1449 ret |= (val & mask) << i;
1455 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1461 op1 = (insn >> 21) & 7;
1462 op2 = (insn >> 5) & 7;
1464 switch ((insn >> 16) & 0xf) {
1467 if (arm_feature(env, ARM_FEATURE_XSCALE))
1469 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1471 if (arm_feature(env, ARM_FEATURE_V7)
1472 && op1 == 2 && crm == 0 && op2 == 0) {
1473 env->cp15.c0_cssel = val & 0xf;
1477 case 1: /* System configuration. */
1478 if (arm_feature(env, ARM_FEATURE_V7)
1479 && op1 == 0 && crm == 1 && op2 == 0) {
1480 env->cp15.c1_scr = val;
1483 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1487 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1488 env->cp15.c1_sys = val;
1489 /* ??? Lots of these bits are not implemented. */
1490 /* This may enable/disable the MMU, so do a TLB flush. */
1493 case 1: /* Auxiliary control register. */
1494 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1495 env->cp15.c1_xscaleauxcr = val;
1498 /* Not implemented. */
1501 if (arm_feature(env, ARM_FEATURE_XSCALE))
1503 if (env->cp15.c1_coproc != val) {
1504 env->cp15.c1_coproc = val;
1505 /* ??? Is this safe when called from within a TB? */
1513 case 2: /* MMU Page table control / MPU cache control. */
1514 if (arm_feature(env, ARM_FEATURE_MPU)) {
1517 env->cp15.c2_data = val;
1520 env->cp15.c2_insn = val;
1528 env->cp15.c2_base0 = val;
1531 env->cp15.c2_base1 = val;
1535 env->cp15.c2_control = val;
1536 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1537 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
1544 case 3: /* MMU Domain access control / MPU write buffer control. */
1546 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1548 case 4: /* Reserved. */
1550 case 5: /* MMU Fault status / MPU access permission. */
1551 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1555 if (arm_feature(env, ARM_FEATURE_MPU))
1556 val = extended_mpu_ap_bits(val);
1557 env->cp15.c5_data = val;
1560 if (arm_feature(env, ARM_FEATURE_MPU))
1561 val = extended_mpu_ap_bits(val);
1562 env->cp15.c5_insn = val;
1565 if (!arm_feature(env, ARM_FEATURE_MPU))
1567 env->cp15.c5_data = val;
1570 if (!arm_feature(env, ARM_FEATURE_MPU))
1572 env->cp15.c5_insn = val;
1578 case 6: /* MMU Fault address / MPU base/size. */
1579 if (arm_feature(env, ARM_FEATURE_MPU)) {
1582 env->cp15.c6_region[crm] = val;
1584 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1588 env->cp15.c6_data = val;
1590 case 1: /* ??? This is WFAR on armv6 */
1592 env->cp15.c6_insn = val;
1599 case 7: /* Cache control. */
1600 env->cp15.c15_i_max = 0x000;
1601 env->cp15.c15_i_min = 0xff0;
1605 /* No cache, so nothing to do except VA->PA translations. */
1606 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1609 if (arm_feature(env, ARM_FEATURE_V7)) {
1610 env->cp15.c7_par = val & 0xfffff6ff;
1612 env->cp15.c7_par = val & 0xfffff1ff;
1617 target_ulong page_size;
1619 int ret, is_user = op2 & 2;
1620 int access_type = op2 & 1;
1623 /* Other states are only available with TrustZone */
1626 ret = get_phys_addr(env, val, access_type, is_user,
1627 &phys_addr, &prot, &page_size);
1629 /* We do not set any attribute bits in the PAR */
1630 if (page_size == (1 << 24)
1631 && arm_feature(env, ARM_FEATURE_V7)) {
1632 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1634 env->cp15.c7_par = phys_addr & 0xfffff000;
1637 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1638 ((ret & (12 << 1)) >> 6) |
1639 ((ret & 0xf) << 1) | 1;
1646 case 8: /* MMU TLB control. */
1648 case 0: /* Invalidate all (TLBIALL) */
1651 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
1652 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1654 case 2: /* Invalidate by ASID (TLBIASID) */
1655 tlb_flush(env, val == 0);
1657 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1658 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1665 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1667 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1668 break; /* Ignore ReadBuffer access */
1670 case 0: /* Cache lockdown. */
1672 case 0: /* L1 cache. */
1675 env->cp15.c9_data = val;
1678 env->cp15.c9_insn = val;
1684 case 1: /* L2 cache. */
1685 /* Ignore writes to L2 lockdown/auxiliary registers. */
1691 case 1: /* TCM memory region registers. */
1692 /* Not implemented. */
1694 case 12: /* Performance monitor control */
1695 /* Performance monitors are implementation defined in v7,
1696 * but with an ARM recommended set of registers, which we
1697 * follow (although we don't actually implement any counters)
1699 if (!arm_feature(env, ARM_FEATURE_V7)) {
1703 case 0: /* performance monitor control register */
1704 /* only the DP, X, D and E bits are writable */
1705 env->cp15.c9_pmcr &= ~0x39;
1706 env->cp15.c9_pmcr |= (val & 0x39);
1708 case 1: /* Count enable set register */
1710 env->cp15.c9_pmcnten |= val;
1712 case 2: /* Count enable clear */
1714 env->cp15.c9_pmcnten &= ~val;
1716 case 3: /* Overflow flag status */
1717 env->cp15.c9_pmovsr &= ~val;
1719 case 4: /* Software increment */
1720 /* RAZ/WI since we don't implement the software-count event */
1722 case 5: /* Event counter selection register */
1723 /* Since we don't implement any events, writing to this register
1724 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1731 case 13: /* Performance counters */
1732 if (!arm_feature(env, ARM_FEATURE_V7)) {
1736 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1738 case 1: /* Event type select */
1739 env->cp15.c9_pmxevtyper = val & 0xff;
1741 case 2: /* Event count register */
1742 /* Unimplemented (we have no events), RAZ/WI */
1748 case 14: /* Performance monitor control */
1749 if (!arm_feature(env, ARM_FEATURE_V7)) {
1753 case 0: /* user enable */
1754 env->cp15.c9_pmuserenr = val & 1;
1755 /* changes access rights for cp registers, so flush tbs */
1758 case 1: /* interrupt enable set */
1759 /* We have no event counters so only the C bit can be changed */
1761 env->cp15.c9_pminten |= val;
1763 case 2: /* interrupt enable clear */
1765 env->cp15.c9_pminten &= ~val;
1773 case 10: /* MMU TLB lockdown. */
1774 /* ??? TLB lockdown not implemented. */
1776 case 12: /* Reserved. */
1778 case 13: /* Process ID. */
1781 /* Unlike real hardware the qemu TLB uses virtual addresses,
1782 not modified virtual addresses, so this causes a TLB flush.
1784 if (env->cp15.c13_fcse != val)
1786 env->cp15.c13_fcse = val;
1789 /* This changes the ASID, so do a TLB flush. */
1790 if (env->cp15.c13_context != val
1791 && !arm_feature(env, ARM_FEATURE_MPU))
1793 env->cp15.c13_context = val;
1799 case 14: /* Generic timer */
1800 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1801 /* Dummy implementation: RAZ/WI for all */
1805 case 15: /* Implementation specific. */
1806 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1807 if (op2 == 0 && crm == 1) {
1808 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1809 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1811 env->cp15.c15_cpar = val & 0x3fff;
1817 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1821 case 1: /* Set TI925T configuration. */
1822 env->cp15.c15_ticonfig = val & 0xe7;
1823 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1824 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1826 case 2: /* Set I_max. */
1827 env->cp15.c15_i_max = val;
1829 case 3: /* Set I_min. */
1830 env->cp15.c15_i_min = val;
1832 case 4: /* Set thread-ID. */
1833 env->cp15.c15_threadid = val & 0xffff;
1835 case 8: /* Wait-for-interrupt (deprecated). */
1836 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1842 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1845 if ((op1 == 0) && (op2 == 0)) {
1846 env->cp15.c15_power_control = val;
1847 } else if ((op1 == 0) && (op2 == 1)) {
1848 env->cp15.c15_diagnostic = val;
1849 } else if ((op1 == 0) && (op2 == 2)) {
1850 env->cp15.c15_power_diagnostic = val;
1860 /* ??? For debugging only. Should raise illegal instruction exception. */
1861 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1862 (insn >> 16) & 0xf, crm, op1, op2);
1865 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1871 op1 = (insn >> 21) & 7;
1872 op2 = (insn >> 5) & 7;
1874 switch ((insn >> 16) & 0xf) {
1875 case 0: /* ID codes. */
1881 case 0: /* Device ID. */
1882 return env->cp15.c0_cpuid;
1883 case 1: /* Cache Type. */
1884 return env->cp15.c0_cachetype;
1885 case 2: /* TCM status. */
1887 case 3: /* TLB type register. */
1888 return 0; /* No lockable TLB entries. */
1890 /* The MPIDR was standardised in v7; prior to
1891 * this it was implemented only in the 11MPCore.
1892 * For all other pre-v7 cores it does not exist.
1894 if (arm_feature(env, ARM_FEATURE_V7) ||
1895 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1896 int mpidr = env->cpu_index;
1897 /* We don't support setting cluster ID ([8..11])
1898 * so these bits always RAZ.
1900 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1902 /* Cores which are uniprocessor (non-coherent)
1903 * but still implement the MP extensions set
1904 * bit 30. (For instance, A9UP.) However we do
1905 * not currently model any of those cores.
1910 /* otherwise fall through to the unimplemented-reg case */
1915 if (!arm_feature(env, ARM_FEATURE_V6))
1917 return env->cp15.c0_c1[op2];
1919 if (!arm_feature(env, ARM_FEATURE_V6))
1921 return env->cp15.c0_c2[op2];
1922 case 3: case 4: case 5: case 6: case 7:
1928 /* These registers aren't documented on arm11 cores. However
1929 Linux looks at them anyway. */
1930 if (!arm_feature(env, ARM_FEATURE_V6))
1934 if (!arm_feature(env, ARM_FEATURE_V7))
1939 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1941 return env->cp15.c0_clid;
1947 if (op2 != 0 || crm != 0)
1949 return env->cp15.c0_cssel;
1953 case 1: /* System configuration. */
1954 if (arm_feature(env, ARM_FEATURE_V7)
1955 && op1 == 0 && crm == 1 && op2 == 0) {
1956 return env->cp15.c1_scr;
1958 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1961 case 0: /* Control register. */
1962 return env->cp15.c1_sys;
1963 case 1: /* Auxiliary control register. */
1964 if (arm_feature(env, ARM_FEATURE_XSCALE))
1965 return env->cp15.c1_xscaleauxcr;
1966 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1968 switch (ARM_CPUID(env)) {
1969 case ARM_CPUID_ARM1026:
1971 case ARM_CPUID_ARM1136:
1972 case ARM_CPUID_ARM1136_R2:
1973 case ARM_CPUID_ARM1176:
1975 case ARM_CPUID_ARM11MPCORE:
1977 case ARM_CPUID_CORTEXA8:
1979 case ARM_CPUID_CORTEXA9:
1980 case ARM_CPUID_CORTEXA15:
1985 case 2: /* Coprocessor access register. */
1986 if (arm_feature(env, ARM_FEATURE_XSCALE))
1988 return env->cp15.c1_coproc;
1992 case 2: /* MMU Page table control / MPU cache control. */
1993 if (arm_feature(env, ARM_FEATURE_MPU)) {
1996 return env->cp15.c2_data;
1999 return env->cp15.c2_insn;
2007 return env->cp15.c2_base0;
2009 return env->cp15.c2_base1;
2011 return env->cp15.c2_control;
2016 case 3: /* MMU Domain access control / MPU write buffer control. */
2017 return env->cp15.c3;
2018 case 4: /* Reserved. */
2020 case 5: /* MMU Fault status / MPU access permission. */
2021 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2025 if (arm_feature(env, ARM_FEATURE_MPU))
2026 return simple_mpu_ap_bits(env->cp15.c5_data);
2027 return env->cp15.c5_data;
2029 if (arm_feature(env, ARM_FEATURE_MPU))
2030 return simple_mpu_ap_bits(env->cp15.c5_insn);
2031 return env->cp15.c5_insn;
2033 if (!arm_feature(env, ARM_FEATURE_MPU))
2035 return env->cp15.c5_data;
2037 if (!arm_feature(env, ARM_FEATURE_MPU))
2039 return env->cp15.c5_insn;
2043 case 6: /* MMU Fault address. */
2044 if (arm_feature(env, ARM_FEATURE_MPU)) {
2047 return env->cp15.c6_region[crm];
2049 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2053 return env->cp15.c6_data;
2055 if (arm_feature(env, ARM_FEATURE_V6)) {
2056 /* Watchpoint Fault Adrress. */
2057 return 0; /* Not implemented. */
2059 /* Instruction Fault Adrress. */
2060 /* Arm9 doesn't have an IFAR, but implementing it anyway
2061 shouldn't do any harm. */
2062 return env->cp15.c6_insn;
2065 if (arm_feature(env, ARM_FEATURE_V6)) {
2066 /* Instruction Fault Adrress. */
2067 return env->cp15.c6_insn;
2075 case 7: /* Cache control. */
2076 if (crm == 4 && op1 == 0 && op2 == 0) {
2077 return env->cp15.c7_par;
2079 /* FIXME: Should only clear Z flag if destination is r15. */
2082 case 8: /* MMU TLB control. */
2086 case 0: /* Cache lockdown */
2088 case 0: /* L1 cache. */
2089 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2094 return env->cp15.c9_data;
2096 return env->cp15.c9_insn;
2100 case 1: /* L2 cache */
2101 /* L2 Lockdown and Auxiliary control. */
2104 /* L2 cache lockdown (A8 only) */
2107 /* L2 cache auxiliary control (A8) or control (A15) */
2108 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
2109 /* Linux wants the number of processors from here.
2110 * Might as well set the interrupt-controller bit too.
2112 return ((smp_cpus - 1) << 24) | (1 << 23);
2116 /* L2 cache extended control (A15) */
2125 case 12: /* Performance monitor control */
2126 if (!arm_feature(env, ARM_FEATURE_V7)) {
2130 case 0: /* performance monitor control register */
2131 return env->cp15.c9_pmcr;
2132 case 1: /* count enable set */
2133 case 2: /* count enable clear */
2134 return env->cp15.c9_pmcnten;
2135 case 3: /* overflow flag status */
2136 return env->cp15.c9_pmovsr;
2137 case 4: /* software increment */
2138 case 5: /* event counter selection register */
2139 return 0; /* Unimplemented, RAZ/WI */
2143 case 13: /* Performance counters */
2144 if (!arm_feature(env, ARM_FEATURE_V7)) {
2148 case 1: /* Event type select */
2149 return env->cp15.c9_pmxevtyper;
2150 case 0: /* Cycle count register */
2151 case 2: /* Event count register */
2152 /* Unimplemented, so RAZ/WI */
2157 case 14: /* Performance monitor control */
2158 if (!arm_feature(env, ARM_FEATURE_V7)) {
2162 case 0: /* user enable */
2163 return env->cp15.c9_pmuserenr;
2164 case 1: /* interrupt enable set */
2165 case 2: /* interrupt enable clear */
2166 return env->cp15.c9_pminten;
2174 case 10: /* MMU TLB lockdown. */
2175 /* ??? TLB lockdown not implemented. */
2177 case 11: /* TCM DMA control. */
2178 case 12: /* Reserved. */
2180 case 13: /* Process ID. */
2183 return env->cp15.c13_fcse;
2185 return env->cp15.c13_context;
2189 case 14: /* Generic timer */
2190 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
2191 /* Dummy implementation: RAZ/WI for all */
2195 case 15: /* Implementation specific. */
2196 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2197 if (op2 == 0 && crm == 1)
2198 return env->cp15.c15_cpar;
2202 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2206 case 1: /* Read TI925T configuration. */
2207 return env->cp15.c15_ticonfig;
2208 case 2: /* Read I_max. */
2209 return env->cp15.c15_i_max;
2210 case 3: /* Read I_min. */
2211 return env->cp15.c15_i_min;
2212 case 4: /* Read thread-ID. */
2213 return env->cp15.c15_threadid;
2214 case 8: /* TI925T_status */
2217 /* TODO: Peripheral port remap register:
2218 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2219 * controller base address at $rn & ~0xfff and map size of
2220 * 0x200 << ($rn & 0xfff), when MMU is off. */
2223 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
2226 if ((op1 == 4) && (op2 == 0)) {
2227 /* The config_base_address should hold the value of
2228 * the peripheral base. ARM should get this from a CPU
2229 * object property, but that support isn't available in
2230 * December 2011. Default to 0 for now and board models
2231 * that care can set it by a private hook */
2232 return env->cp15.c15_config_base_address;
2233 } else if ((op1 == 0) && (op2 == 0)) {
2234 /* power_control should be set to maximum latency. Again,
2235 default to 0 and set by private hook */
2236 return env->cp15.c15_power_control;
2237 } else if ((op1 == 0) && (op2 == 1)) {
2238 return env->cp15.c15_diagnostic;
2239 } else if ((op1 == 0) && (op2 == 2)) {
2240 return env->cp15.c15_power_diagnostic;
2243 case 1: /* NEON Busy */
2245 case 5: /* tlb lockdown */
2248 if ((op1 == 5) && (op2 == 2)) {
2260 /* ??? For debugging only. Should raise illegal instruction exception. */
2261 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2262 (insn >> 16) & 0xf, crm, op1, op2);
2266 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2268 if ((env->uncached_cpsr & CPSR_M) == mode) {
2269 env->regs[13] = val;
2271 env->banked_r13[bank_number(env, mode)] = val;
2275 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2277 if ((env->uncached_cpsr & CPSR_M) == mode) {
2278 return env->regs[13];
2280 return env->banked_r13[bank_number(env, mode)];
2284 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2288 return xpsr_read(env) & 0xf8000000;
2290 return xpsr_read(env) & 0xf80001ff;
2292 return xpsr_read(env) & 0xff00fc00;
2294 return xpsr_read(env) & 0xff00fdff;
2296 return xpsr_read(env) & 0x000001ff;
2298 return xpsr_read(env) & 0x0700fc00;
2300 return xpsr_read(env) & 0x0700edff;
2302 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2304 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2305 case 16: /* PRIMASK */
2306 return (env->uncached_cpsr & CPSR_I) != 0;
2307 case 17: /* BASEPRI */
2308 case 18: /* BASEPRI_MAX */
2309 return env->v7m.basepri;
2310 case 19: /* FAULTMASK */
2311 return (env->uncached_cpsr & CPSR_F) != 0;
2312 case 20: /* CONTROL */
2313 return env->v7m.control;
2315 /* ??? For debugging only. */
2316 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2321 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2325 xpsr_write(env, val, 0xf8000000);
2328 xpsr_write(env, val, 0xf8000000);
2331 xpsr_write(env, val, 0xfe00fc00);
2334 xpsr_write(env, val, 0xfe00fc00);
2337 /* IPSR bits are readonly. */
2340 xpsr_write(env, val, 0x0600fc00);
2343 xpsr_write(env, val, 0x0600fc00);
2346 if (env->v7m.current_sp)
2347 env->v7m.other_sp = val;
2349 env->regs[13] = val;
2352 if (env->v7m.current_sp)
2353 env->regs[13] = val;
2355 env->v7m.other_sp = val;
2357 case 16: /* PRIMASK */
2359 env->uncached_cpsr |= CPSR_I;
2361 env->uncached_cpsr &= ~CPSR_I;
2363 case 17: /* BASEPRI */
2364 env->v7m.basepri = val & 0xff;
2366 case 18: /* BASEPRI_MAX */
2368 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2369 env->v7m.basepri = val;
2371 case 19: /* FAULTMASK */
2373 env->uncached_cpsr |= CPSR_F;
2375 env->uncached_cpsr &= ~CPSR_F;
2377 case 20: /* CONTROL */
2378 env->v7m.control = val & 3;
2379 switch_v7m_sp(env, (val & 2) != 0);
2382 /* ??? For debugging only. */
2383 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2388 void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
2389 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
2392 if (cpnum < 0 || cpnum > 14) {
2393 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2397 env->cp[cpnum].cp_read = cp_read;
2398 env->cp[cpnum].cp_write = cp_write;
2399 env->cp[cpnum].opaque = opaque;
2404 /* Note that signed overflow is undefined in C. The following routines are
2405 careful to use unsigned types where modulo arithmetic is required.
2406 Failure to do so _will_ break on newer gcc. */
2408 /* Signed saturating arithmetic. */
2410 /* Perform 16-bit signed saturating addition. */
2411 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2416 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2425 /* Perform 8-bit signed saturating addition. */
2426 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2431 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2440 /* Perform 16-bit signed saturating subtraction. */
2441 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2446 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2455 /* Perform 8-bit signed saturating subtraction. */
2456 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2461 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2470 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2471 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2472 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2473 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2476 #include "op_addsub.h"
2478 /* Unsigned saturating arithmetic. */
2479 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2488 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2496 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2505 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2513 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2514 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2515 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2516 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2519 #include "op_addsub.h"
2521 /* Signed modulo arithmetic. */
2522 #define SARITH16(a, b, n, op) do { \
2524 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2525 RESULT(sum, n, 16); \
2527 ge |= 3 << (n * 2); \
2530 #define SARITH8(a, b, n, op) do { \
2532 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2533 RESULT(sum, n, 8); \
2539 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2540 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2541 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2542 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2546 #include "op_addsub.h"
2548 /* Unsigned modulo arithmetic. */
2549 #define ADD16(a, b, n) do { \
2551 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2552 RESULT(sum, n, 16); \
2553 if ((sum >> 16) == 1) \
2554 ge |= 3 << (n * 2); \
2557 #define ADD8(a, b, n) do { \
2559 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2560 RESULT(sum, n, 8); \
2561 if ((sum >> 8) == 1) \
2565 #define SUB16(a, b, n) do { \
2567 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2568 RESULT(sum, n, 16); \
2569 if ((sum >> 16) == 0) \
2570 ge |= 3 << (n * 2); \
2573 #define SUB8(a, b, n) do { \
2575 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2576 RESULT(sum, n, 8); \
2577 if ((sum >> 8) == 0) \
2584 #include "op_addsub.h"
2586 /* Halved signed arithmetic. */
2587 #define ADD16(a, b, n) \
2588 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2589 #define SUB16(a, b, n) \
2590 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2591 #define ADD8(a, b, n) \
2592 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2593 #define SUB8(a, b, n) \
2594 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2597 #include "op_addsub.h"
2599 /* Halved unsigned arithmetic. */
2600 #define ADD16(a, b, n) \
2601 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2602 #define SUB16(a, b, n) \
2603 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2604 #define ADD8(a, b, n) \
2605 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2606 #define SUB8(a, b, n) \
2607 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2610 #include "op_addsub.h"
2612 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2620 /* Unsigned sum of absolute byte differences. */
2621 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2624 sum = do_usad(a, b);
2625 sum += do_usad(a >> 8, b >> 8);
2626 sum += do_usad(a >> 16, b >>16);
2627 sum += do_usad(a >> 24, b >> 24);
2631 /* For ARMv6 SEL instruction. */
2632 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2645 return (a & mask) | (b & ~mask);
2648 uint32_t HELPER(logicq_cc)(uint64_t val)
2650 return (val >> 32) | (val != 0);
2653 /* VFP support. We follow the convention used for VFP instrunctions:
2654 Single precition routines have a "s" suffix, double precision a
2657 /* Convert host exception flags to vfp form. */
2658 static inline int vfp_exceptbits_from_host(int host_bits)
2660 int target_bits = 0;
2662 if (host_bits & float_flag_invalid)
2664 if (host_bits & float_flag_divbyzero)
2666 if (host_bits & float_flag_overflow)
2668 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2670 if (host_bits & float_flag_inexact)
2671 target_bits |= 0x10;
2672 if (host_bits & float_flag_input_denormal)
2673 target_bits |= 0x80;
2677 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2682 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2683 | (env->vfp.vec_len << 16)
2684 | (env->vfp.vec_stride << 20);
2685 i = get_float_exception_flags(&env->vfp.fp_status);
2686 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2687 fpscr |= vfp_exceptbits_from_host(i);
2691 uint32_t vfp_get_fpscr(CPUARMState *env)
2693 return HELPER(vfp_get_fpscr)(env);
2696 /* Convert vfp exception flags to target form. */
2697 static inline int vfp_exceptbits_to_host(int target_bits)
2701 if (target_bits & 1)
2702 host_bits |= float_flag_invalid;
2703 if (target_bits & 2)
2704 host_bits |= float_flag_divbyzero;
2705 if (target_bits & 4)
2706 host_bits |= float_flag_overflow;
2707 if (target_bits & 8)
2708 host_bits |= float_flag_underflow;
2709 if (target_bits & 0x10)
2710 host_bits |= float_flag_inexact;
2711 if (target_bits & 0x80)
2712 host_bits |= float_flag_input_denormal;
2716 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2721 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2722 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2723 env->vfp.vec_len = (val >> 16) & 7;
2724 env->vfp.vec_stride = (val >> 20) & 3;
2727 if (changed & (3 << 22)) {
2728 i = (val >> 22) & 3;
2731 i = float_round_nearest_even;
2737 i = float_round_down;
2740 i = float_round_to_zero;
2743 set_float_rounding_mode(i, &env->vfp.fp_status);
2745 if (changed & (1 << 24)) {
2746 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2747 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2749 if (changed & (1 << 25))
2750 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2752 i = vfp_exceptbits_to_host(val);
2753 set_float_exception_flags(i, &env->vfp.fp_status);
2754 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2757 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2759 HELPER(vfp_set_fpscr)(env, val);
2762 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2764 #define VFP_BINOP(name) \
2765 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2767 float_status *fpst = fpstp; \
2768 return float32_ ## name(a, b, fpst); \
2770 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2772 float_status *fpst = fpstp; \
2773 return float64_ ## name(a, b, fpst); \
2781 float32 VFP_HELPER(neg, s)(float32 a)
2783 return float32_chs(a);
2786 float64 VFP_HELPER(neg, d)(float64 a)
2788 return float64_chs(a);
2791 float32 VFP_HELPER(abs, s)(float32 a)
2793 return float32_abs(a);
2796 float64 VFP_HELPER(abs, d)(float64 a)
2798 return float64_abs(a);
2801 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2803 return float32_sqrt(a, &env->vfp.fp_status);
2806 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2808 return float64_sqrt(a, &env->vfp.fp_status);
2811 /* XXX: check quiet/signaling case */
2812 #define DO_VFP_cmp(p, type) \
2813 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2816 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2817 case 0: flags = 0x6; break; \
2818 case -1: flags = 0x8; break; \
2819 case 1: flags = 0x2; break; \
2820 default: case 2: flags = 0x3; break; \
2822 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2823 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2825 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2828 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2829 case 0: flags = 0x6; break; \
2830 case -1: flags = 0x8; break; \
2831 case 1: flags = 0x2; break; \
2832 default: case 2: flags = 0x3; break; \
2834 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2835 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2837 DO_VFP_cmp(s, float32)
2838 DO_VFP_cmp(d, float64)
2841 /* Integer to float and float to integer conversions */
2843 #define CONV_ITOF(name, fsz, sign) \
2844 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2846 float_status *fpst = fpstp; \
2847 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2850 #define CONV_FTOI(name, fsz, sign, round) \
2851 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2853 float_status *fpst = fpstp; \
2854 if (float##fsz##_is_any_nan(x)) { \
2855 float_raise(float_flag_invalid, fpst); \
2858 return float##fsz##_to_##sign##int32##round(x, fpst); \
2861 #define FLOAT_CONVS(name, p, fsz, sign) \
2862 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2863 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2864 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2866 FLOAT_CONVS(si, s, 32, )
2867 FLOAT_CONVS(si, d, 64, )
2868 FLOAT_CONVS(ui, s, 32, u)
2869 FLOAT_CONVS(ui, d, 64, u)
2875 /* floating point conversion */
2876 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2878 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2879 /* ARM requires that S<->D conversion of any kind of NaN generates
2880 * a quiet NaN by forcing the most significant frac bit to 1.
2882 return float64_maybe_silence_nan(r);
2885 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2887 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2888 /* ARM requires that S<->D conversion of any kind of NaN generates
2889 * a quiet NaN by forcing the most significant frac bit to 1.
2891 return float32_maybe_silence_nan(r);
2894 /* VFP3 fixed point conversion. */
2895 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2896 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2899 float_status *fpst = fpstp; \
2901 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2902 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2904 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2907 float_status *fpst = fpstp; \
2909 if (float##fsz##_is_any_nan(x)) { \
2910 float_raise(float_flag_invalid, fpst); \
2913 tmp = float##fsz##_scalbn(x, shift, fpst); \
2914 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2917 VFP_CONV_FIX(sh, d, 64, int16, )
2918 VFP_CONV_FIX(sl, d, 64, int32, )
2919 VFP_CONV_FIX(uh, d, 64, uint16, u)
2920 VFP_CONV_FIX(ul, d, 64, uint32, u)
2921 VFP_CONV_FIX(sh, s, 32, int16, )
2922 VFP_CONV_FIX(sl, s, 32, int32, )
2923 VFP_CONV_FIX(uh, s, 32, uint16, u)
2924 VFP_CONV_FIX(ul, s, 32, uint32, u)
2927 /* Half precision conversions. */
2928 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2930 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2931 float32 r = float16_to_float32(make_float16(a), ieee, s);
2933 return float32_maybe_silence_nan(r);
2938 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2940 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2941 float16 r = float32_to_float16(a, ieee, s);
2943 r = float16_maybe_silence_nan(r);
2945 return float16_val(r);
2948 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2950 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2953 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2955 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2958 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2960 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2963 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2965 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2968 #define float32_two make_float32(0x40000000)
2969 #define float32_three make_float32(0x40400000)
2970 #define float32_one_point_five make_float32(0x3fc00000)
2972 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2974 float_status *s = &env->vfp.standard_fp_status;
2975 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2976 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2977 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2978 float_raise(float_flag_input_denormal, s);
2982 return float32_sub(float32_two, float32_mul(a, b, s), s);
2985 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2987 float_status *s = &env->vfp.standard_fp_status;
2989 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2990 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2991 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2992 float_raise(float_flag_input_denormal, s);
2994 return float32_one_point_five;
2996 product = float32_mul(a, b, s);
2997 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
3002 /* Constants 256 and 512 are used in some helpers; we avoid relying on
3003 * int->float conversions at run-time. */
3004 #define float64_256 make_float64(0x4070000000000000LL)
3005 #define float64_512 make_float64(0x4080000000000000LL)
3007 /* The algorithm that must be used to calculate the estimate
3008 * is specified by the ARM ARM.
3010 static float64 recip_estimate(float64 a, CPUARMState *env)
3012 /* These calculations mustn't set any fp exception flags,
3013 * so we use a local copy of the fp_status.
3015 float_status dummy_status = env->vfp.standard_fp_status;
3016 float_status *s = &dummy_status;
3017 /* q = (int)(a * 512.0) */
3018 float64 q = float64_mul(float64_512, a, s);
3019 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3021 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
3022 q = int64_to_float64(q_int, s);
3023 q = float64_add(q, float64_half, s);
3024 q = float64_div(q, float64_512, s);
3025 q = float64_div(float64_one, q, s);
3027 /* s = (int)(256.0 * r + 0.5) */
3028 q = float64_mul(q, float64_256, s);
3029 q = float64_add(q, float64_half, s);
3030 q_int = float64_to_int64_round_to_zero(q, s);
3032 /* return (double)s / 256.0 */
3033 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3036 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
3038 float_status *s = &env->vfp.standard_fp_status;
3040 uint32_t val32 = float32_val(a);
3043 int a_exp = (val32 & 0x7f800000) >> 23;
3044 int sign = val32 & 0x80000000;
3046 if (float32_is_any_nan(a)) {
3047 if (float32_is_signaling_nan(a)) {
3048 float_raise(float_flag_invalid, s);
3050 return float32_default_nan;
3051 } else if (float32_is_infinity(a)) {
3052 return float32_set_sign(float32_zero, float32_is_neg(a));
3053 } else if (float32_is_zero_or_denormal(a)) {
3054 if (!float32_is_zero(a)) {
3055 float_raise(float_flag_input_denormal, s);
3057 float_raise(float_flag_divbyzero, s);
3058 return float32_set_sign(float32_infinity, float32_is_neg(a));
3059 } else if (a_exp >= 253) {
3060 float_raise(float_flag_underflow, s);
3061 return float32_set_sign(float32_zero, float32_is_neg(a));
3064 f64 = make_float64((0x3feULL << 52)
3065 | ((int64_t)(val32 & 0x7fffff) << 29));
3067 result_exp = 253 - a_exp;
3069 f64 = recip_estimate(f64, env);
3072 | ((result_exp & 0xff) << 23)
3073 | ((float64_val(f64) >> 29) & 0x7fffff);
3074 return make_float32(val32);
3077 /* The algorithm that must be used to calculate the estimate
3078 * is specified by the ARM ARM.
3080 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
3082 /* These calculations mustn't set any fp exception flags,
3083 * so we use a local copy of the fp_status.
3085 float_status dummy_status = env->vfp.standard_fp_status;
3086 float_status *s = &dummy_status;
3090 if (float64_lt(a, float64_half, s)) {
3091 /* range 0.25 <= a < 0.5 */
3093 /* a in units of 1/512 rounded down */
3094 /* q0 = (int)(a * 512.0); */
3095 q = float64_mul(float64_512, a, s);
3096 q_int = float64_to_int64_round_to_zero(q, s);
3098 /* reciprocal root r */
3099 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
3100 q = int64_to_float64(q_int, s);
3101 q = float64_add(q, float64_half, s);
3102 q = float64_div(q, float64_512, s);
3103 q = float64_sqrt(q, s);
3104 q = float64_div(float64_one, q, s);
3106 /* range 0.5 <= a < 1.0 */
3108 /* a in units of 1/256 rounded down */
3109 /* q1 = (int)(a * 256.0); */
3110 q = float64_mul(float64_256, a, s);
3111 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3113 /* reciprocal root r */
3114 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3115 q = int64_to_float64(q_int, s);
3116 q = float64_add(q, float64_half, s);
3117 q = float64_div(q, float64_256, s);
3118 q = float64_sqrt(q, s);
3119 q = float64_div(float64_one, q, s);
3121 /* r in units of 1/256 rounded to nearest */
3122 /* s = (int)(256.0 * r + 0.5); */
3124 q = float64_mul(q, float64_256,s );
3125 q = float64_add(q, float64_half, s);
3126 q_int = float64_to_int64_round_to_zero(q, s);
3128 /* return (double)s / 256.0;*/
3129 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3132 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
3134 float_status *s = &env->vfp.standard_fp_status;
3140 val = float32_val(a);
3142 if (float32_is_any_nan(a)) {
3143 if (float32_is_signaling_nan(a)) {
3144 float_raise(float_flag_invalid, s);
3146 return float32_default_nan;
3147 } else if (float32_is_zero_or_denormal(a)) {
3148 if (!float32_is_zero(a)) {
3149 float_raise(float_flag_input_denormal, s);
3151 float_raise(float_flag_divbyzero, s);
3152 return float32_set_sign(float32_infinity, float32_is_neg(a));
3153 } else if (float32_is_neg(a)) {
3154 float_raise(float_flag_invalid, s);
3155 return float32_default_nan;
3156 } else if (float32_is_infinity(a)) {
3157 return float32_zero;
3160 /* Normalize to a double-precision value between 0.25 and 1.0,
3161 * preserving the parity of the exponent. */
3162 if ((val & 0x800000) == 0) {
3163 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3165 | ((uint64_t)(val & 0x7fffff) << 29));
3167 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3169 | ((uint64_t)(val & 0x7fffff) << 29));
3172 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3174 f64 = recip_sqrt_estimate(f64, env);
3176 val64 = float64_val(f64);
3178 val = ((result_exp & 0xff) << 23)
3179 | ((val64 >> 29) & 0x7fffff);
3180 return make_float32(val);
3183 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
3187 if ((a & 0x80000000) == 0) {
3191 f64 = make_float64((0x3feULL << 52)
3192 | ((int64_t)(a & 0x7fffffff) << 21));
3194 f64 = recip_estimate (f64, env);
3196 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3199 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3203 if ((a & 0xc0000000) == 0) {
3207 if (a & 0x80000000) {
3208 f64 = make_float64((0x3feULL << 52)
3209 | ((uint64_t)(a & 0x7fffffff) << 21));
3210 } else { /* bits 31-30 == '01' */
3211 f64 = make_float64((0x3fdULL << 52)
3212 | ((uint64_t)(a & 0x3fffffff) << 22));
3215 f64 = recip_sqrt_estimate(f64, env);
3217 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3220 /* VFPv4 fused multiply-accumulate */
3221 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3223 float_status *fpst = fpstp;
3224 return float32_muladd(a, b, c, 0, fpst);
3227 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3229 float_status *fpst = fpstp;
3230 return float64_muladd(a, b, c, 0, fpst);
3233 void HELPER(set_teecr)(CPUARMState *env, uint32_t val)
3236 if (env->teecr != val) {