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 inline void set_feature(CPUARMState *env, int feature)
51 env->features |= 1u << feature;
54 static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
57 case ARM_CPUID_ARM926:
58 set_feature(env, ARM_FEATURE_V5);
59 set_feature(env, ARM_FEATURE_VFP);
60 env->vfp.xregs[ARM_VFP_FPSID] = 0x41011090;
61 env->cp15.c0_cachetype = 0x1dd20d2;
62 env->cp15.c1_sys = 0x00090078;
64 case ARM_CPUID_ARM946:
65 set_feature(env, ARM_FEATURE_V5);
66 set_feature(env, ARM_FEATURE_MPU);
67 env->cp15.c0_cachetype = 0x0f004006;
68 env->cp15.c1_sys = 0x00000078;
70 case ARM_CPUID_ARM1026:
71 set_feature(env, ARM_FEATURE_V5);
72 set_feature(env, ARM_FEATURE_VFP);
73 set_feature(env, ARM_FEATURE_AUXCR);
74 env->vfp.xregs[ARM_VFP_FPSID] = 0x410110a0;
75 env->cp15.c0_cachetype = 0x1dd20d2;
76 env->cp15.c1_sys = 0x00090078;
78 case ARM_CPUID_ARM1136:
79 /* This is the 1136 r1, which is a v6K core */
80 set_feature(env, ARM_FEATURE_V6K);
82 case ARM_CPUID_ARM1136_R2:
83 /* What qemu calls "arm1136_r2" is actually the 1136 r0p2, ie an
84 * older core than plain "arm1136". In particular this does not
85 * have the v6K features.
87 set_feature(env, ARM_FEATURE_V6);
88 set_feature(env, ARM_FEATURE_VFP);
89 /* These ID register values are correct for 1136 but may be wrong
90 * for 1136_r2 (in particular r0p2 does not actually implement most
91 * of the ID registers).
93 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
94 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
95 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
96 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
97 memcpy(env->cp15.c0_c2, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
98 env->cp15.c0_cachetype = 0x1dd20d2;
99 env->cp15.c1_sys = 0x00050078;
101 case ARM_CPUID_ARM1176:
102 set_feature(env, ARM_FEATURE_V6K);
103 set_feature(env, ARM_FEATURE_VFP);
104 set_feature(env, ARM_FEATURE_VAPA);
105 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b5;
106 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
107 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
108 memcpy(env->cp15.c0_c1, arm1176_cp15_c0_c1, 8 * sizeof(uint32_t));
109 memcpy(env->cp15.c0_c2, arm1176_cp15_c0_c2, 8 * sizeof(uint32_t));
110 env->cp15.c0_cachetype = 0x1dd20d2;
111 env->cp15.c1_sys = 0x00050078;
113 case ARM_CPUID_ARM11MPCORE:
114 set_feature(env, ARM_FEATURE_V6K);
115 set_feature(env, ARM_FEATURE_VFP);
116 set_feature(env, ARM_FEATURE_VAPA);
117 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
118 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
119 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
120 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
121 memcpy(env->cp15.c0_c2, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
122 env->cp15.c0_cachetype = 0x1dd20d2;
124 case ARM_CPUID_CORTEXA8:
125 set_feature(env, ARM_FEATURE_V7);
126 set_feature(env, ARM_FEATURE_VFP3);
127 set_feature(env, ARM_FEATURE_NEON);
128 set_feature(env, ARM_FEATURE_THUMB2EE);
129 env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
130 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
131 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
132 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
133 memcpy(env->cp15.c0_c2, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
134 env->cp15.c0_cachetype = 0x82048004;
135 env->cp15.c0_clid = (1 << 27) | (2 << 24) | 3;
136 env->cp15.c0_ccsid[0] = 0xe007e01a; /* 16k L1 dcache. */
137 env->cp15.c0_ccsid[1] = 0x2007e01a; /* 16k L1 icache. */
138 env->cp15.c0_ccsid[2] = 0xf0000000; /* No L2 icache. */
139 env->cp15.c1_sys = 0x00c50078;
141 case ARM_CPUID_CORTEXA9:
142 set_feature(env, ARM_FEATURE_V7);
143 set_feature(env, ARM_FEATURE_VFP3);
144 set_feature(env, ARM_FEATURE_VFP_FP16);
145 set_feature(env, ARM_FEATURE_NEON);
146 set_feature(env, ARM_FEATURE_THUMB2EE);
147 /* Note that A9 supports the MP extensions even for
148 * A9UP and single-core A9MP (which are both different
149 * and valid configurations; we don't model A9UP).
151 set_feature(env, ARM_FEATURE_V7MP);
152 env->vfp.xregs[ARM_VFP_FPSID] = 0x41033090;
153 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
154 env->vfp.xregs[ARM_VFP_MVFR1] = 0x01111111;
155 memcpy(env->cp15.c0_c1, cortexa9_cp15_c0_c1, 8 * sizeof(uint32_t));
156 memcpy(env->cp15.c0_c2, cortexa9_cp15_c0_c2, 8 * sizeof(uint32_t));
157 env->cp15.c0_cachetype = 0x80038003;
158 env->cp15.c0_clid = (1 << 27) | (1 << 24) | 3;
159 env->cp15.c0_ccsid[0] = 0xe00fe015; /* 16k L1 dcache. */
160 env->cp15.c0_ccsid[1] = 0x200fe015; /* 16k L1 icache. */
161 env->cp15.c1_sys = 0x00c50078;
163 case ARM_CPUID_CORTEXA15:
164 set_feature(env, ARM_FEATURE_V7);
165 set_feature(env, ARM_FEATURE_VFP4);
166 set_feature(env, ARM_FEATURE_VFP_FP16);
167 set_feature(env, ARM_FEATURE_NEON);
168 set_feature(env, ARM_FEATURE_THUMB2EE);
169 set_feature(env, ARM_FEATURE_ARM_DIV);
170 set_feature(env, ARM_FEATURE_V7MP);
171 set_feature(env, ARM_FEATURE_GENERIC_TIMER);
172 env->vfp.xregs[ARM_VFP_FPSID] = 0x410430f0;
173 env->vfp.xregs[ARM_VFP_MVFR0] = 0x10110222;
174 env->vfp.xregs[ARM_VFP_MVFR1] = 0x11111111;
175 memcpy(env->cp15.c0_c1, cortexa15_cp15_c0_c1, 8 * sizeof(uint32_t));
176 memcpy(env->cp15.c0_c2, cortexa15_cp15_c0_c2, 8 * sizeof(uint32_t));
177 env->cp15.c0_cachetype = 0x8444c004;
178 env->cp15.c0_clid = 0x0a200023;
179 env->cp15.c0_ccsid[0] = 0x701fe00a; /* 32K L1 dcache */
180 env->cp15.c0_ccsid[1] = 0x201fe00a; /* 32K L1 icache */
181 env->cp15.c0_ccsid[2] = 0x711fe07a; /* 4096K L2 unified cache */
182 env->cp15.c1_sys = 0x00c50078;
184 case ARM_CPUID_CORTEXM3:
185 set_feature(env, ARM_FEATURE_V7);
186 set_feature(env, ARM_FEATURE_M);
188 case ARM_CPUID_ANY: /* For userspace emulation. */
189 set_feature(env, ARM_FEATURE_V7);
190 set_feature(env, ARM_FEATURE_VFP4);
191 set_feature(env, ARM_FEATURE_VFP_FP16);
192 set_feature(env, ARM_FEATURE_NEON);
193 set_feature(env, ARM_FEATURE_THUMB2EE);
194 set_feature(env, ARM_FEATURE_ARM_DIV);
195 set_feature(env, ARM_FEATURE_V7MP);
197 case ARM_CPUID_TI915T:
198 case ARM_CPUID_TI925T:
199 set_feature(env, ARM_FEATURE_V4T);
200 set_feature(env, ARM_FEATURE_OMAPCP);
201 env->cp15.c0_cachetype = 0x5109149;
202 env->cp15.c1_sys = 0x00000070;
203 env->cp15.c15_i_max = 0x000;
204 env->cp15.c15_i_min = 0xff0;
206 case ARM_CPUID_PXA250:
207 case ARM_CPUID_PXA255:
208 case ARM_CPUID_PXA260:
209 case ARM_CPUID_PXA261:
210 case ARM_CPUID_PXA262:
211 set_feature(env, ARM_FEATURE_V5);
212 set_feature(env, ARM_FEATURE_XSCALE);
213 /* JTAG_ID is ((id << 28) | 0x09265013) */
214 env->cp15.c0_cachetype = 0xd172172;
215 env->cp15.c1_sys = 0x00000078;
217 case ARM_CPUID_PXA270_A0:
218 case ARM_CPUID_PXA270_A1:
219 case ARM_CPUID_PXA270_B0:
220 case ARM_CPUID_PXA270_B1:
221 case ARM_CPUID_PXA270_C0:
222 case ARM_CPUID_PXA270_C5:
223 set_feature(env, ARM_FEATURE_V5);
224 set_feature(env, ARM_FEATURE_XSCALE);
225 /* JTAG_ID is ((id << 28) | 0x09265013) */
226 set_feature(env, ARM_FEATURE_IWMMXT);
227 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
228 env->cp15.c0_cachetype = 0xd172172;
229 env->cp15.c1_sys = 0x00000078;
231 case ARM_CPUID_SA1100:
232 case ARM_CPUID_SA1110:
233 set_feature(env, ARM_FEATURE_STRONGARM);
234 env->cp15.c1_sys = 0x00000070;
237 cpu_abort(env, "Bad CPU ID: %x\n", id);
241 /* Some features automatically imply others: */
242 if (arm_feature(env, ARM_FEATURE_V7)) {
243 set_feature(env, ARM_FEATURE_VAPA);
244 set_feature(env, ARM_FEATURE_THUMB2);
245 if (!arm_feature(env, ARM_FEATURE_M)) {
246 set_feature(env, ARM_FEATURE_V6K);
248 set_feature(env, ARM_FEATURE_V6);
251 if (arm_feature(env, ARM_FEATURE_V6K)) {
252 set_feature(env, ARM_FEATURE_V6);
253 set_feature(env, ARM_FEATURE_MVFR);
255 if (arm_feature(env, ARM_FEATURE_V6)) {
256 set_feature(env, ARM_FEATURE_V5);
257 if (!arm_feature(env, ARM_FEATURE_M)) {
258 set_feature(env, ARM_FEATURE_AUXCR);
261 if (arm_feature(env, ARM_FEATURE_V5)) {
262 set_feature(env, ARM_FEATURE_V4T);
264 if (arm_feature(env, ARM_FEATURE_M)) {
265 set_feature(env, ARM_FEATURE_THUMB_DIV);
267 if (arm_feature(env, ARM_FEATURE_ARM_DIV)) {
268 set_feature(env, ARM_FEATURE_THUMB_DIV);
270 if (arm_feature(env, ARM_FEATURE_VFP4)) {
271 set_feature(env, ARM_FEATURE_VFP3);
273 if (arm_feature(env, ARM_FEATURE_VFP3)) {
274 set_feature(env, ARM_FEATURE_VFP);
278 /* TODO Move contents into arm_cpu_reset() in cpu.c,
279 * once cpu_reset_model_id() is eliminated,
280 * and then forward to cpu_reset() here.
282 void cpu_state_reset(CPUARMState *env)
286 ARMCPU *cpu = arm_env_get_cpu(env);
288 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
289 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
290 log_cpu_state(env, 0);
294 tmp = env->cp15.c15_config_base_address;
295 memset(env, 0, offsetof(CPUARMState, breakpoints));
297 cpu_reset_model_id(env, id);
298 env->cp15.c15_config_base_address = tmp;
299 env->cp15.c0_cpuid = cpu->midr;
300 #if defined (CONFIG_USER_ONLY)
301 env->uncached_cpsr = ARM_CPU_MODE_USR;
302 /* For user mode we must enable access to coprocessors */
303 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
304 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
305 env->cp15.c15_cpar = 3;
306 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
307 env->cp15.c15_cpar = 1;
310 /* SVC mode with interrupts disabled. */
311 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
312 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
313 clear at reset. Initial SP and PC are loaded from ROM. */
317 env->uncached_cpsr &= ~CPSR_I;
320 /* We should really use ldl_phys here, in case the guest
321 modified flash and reset itself. However images
322 loaded via -kernel have not been copied yet, so load the
323 values directly from there. */
324 env->regs[13] = ldl_p(rom);
327 env->regs[15] = pc & ~1;
330 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
331 env->cp15.c2_base_mask = 0xffffc000u;
332 /* v7 performance monitor control register: same implementor
333 * field as main ID register, and we implement no event counters.
335 env->cp15.c9_pmcr = (id & 0xff000000);
337 set_flush_to_zero(1, &env->vfp.standard_fp_status);
338 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
339 set_default_nan_mode(1, &env->vfp.standard_fp_status);
340 set_float_detect_tininess(float_tininess_before_rounding,
341 &env->vfp.fp_status);
342 set_float_detect_tininess(float_tininess_before_rounding,
343 &env->vfp.standard_fp_status);
345 /* Reset is a state change for some CPUARMState fields which we
346 * bake assumptions about into translated code, so we need to
352 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
356 /* VFP data registers are always little-endian. */
357 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
359 stfq_le_p(buf, env->vfp.regs[reg]);
362 if (arm_feature(env, ARM_FEATURE_NEON)) {
363 /* Aliases for Q regs. */
366 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
367 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
371 switch (reg - nregs) {
372 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
373 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
374 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
379 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
383 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
385 env->vfp.regs[reg] = ldfq_le_p(buf);
388 if (arm_feature(env, ARM_FEATURE_NEON)) {
391 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
392 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
396 switch (reg - nregs) {
397 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
398 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
399 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
404 CPUARMState *cpu_arm_init(const char *cpu_model)
408 static int inited = 0;
410 if (!object_class_by_name(cpu_model)) {
413 cpu = ARM_CPU(object_new(cpu_model));
415 env->cpu_model_str = cpu_model;
417 if (tcg_enabled() && !inited) {
419 arm_translate_init();
422 cpu_state_reset(env);
423 if (arm_feature(env, ARM_FEATURE_NEON)) {
424 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
425 51, "arm-neon.xml", 0);
426 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
427 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
428 35, "arm-vfp3.xml", 0);
429 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
430 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
431 19, "arm-vfp.xml", 0);
437 typedef struct ARMCPUListState {
438 fprintf_function cpu_fprintf;
442 /* Sort alphabetically by type name, except for "any". */
443 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
445 ObjectClass *class_a = (ObjectClass *)a;
446 ObjectClass *class_b = (ObjectClass *)b;
447 const char *name_a, *name_b;
449 name_a = object_class_get_name(class_a);
450 name_b = object_class_get_name(class_b);
451 if (strcmp(name_a, "any") == 0) {
453 } else if (strcmp(name_b, "any") == 0) {
456 return strcmp(name_a, name_b);
460 static void arm_cpu_list_entry(gpointer data, gpointer user_data)
462 ObjectClass *oc = data;
463 ARMCPUListState *s = user_data;
465 (*s->cpu_fprintf)(s->file, " %s\n",
466 object_class_get_name(oc));
469 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
471 ARMCPUListState s = {
473 .cpu_fprintf = cpu_fprintf,
477 list = object_class_get_list(TYPE_ARM_CPU, false);
478 list = g_slist_sort(list, arm_cpu_list_compare);
479 (*cpu_fprintf)(f, "Available CPUs:\n");
480 g_slist_foreach(list, arm_cpu_list_entry, &s);
484 static int bad_mode_switch(CPUARMState *env, int mode)
486 /* Return true if it is not valid for us to switch to
487 * this CPU mode (ie all the UNPREDICTABLE cases in
488 * the ARM ARM CPSRWriteByInstr pseudocode).
491 case ARM_CPU_MODE_USR:
492 case ARM_CPU_MODE_SYS:
493 case ARM_CPU_MODE_SVC:
494 case ARM_CPU_MODE_ABT:
495 case ARM_CPU_MODE_UND:
496 case ARM_CPU_MODE_IRQ:
497 case ARM_CPU_MODE_FIQ:
504 uint32_t cpsr_read(CPUARMState *env)
508 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
509 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
510 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
511 | ((env->condexec_bits & 0xfc) << 8)
515 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
517 if (mask & CPSR_NZCV) {
518 env->ZF = (~val) & CPSR_Z;
520 env->CF = (val >> 29) & 1;
521 env->VF = (val << 3) & 0x80000000;
524 env->QF = ((val & CPSR_Q) != 0);
526 env->thumb = ((val & CPSR_T) != 0);
527 if (mask & CPSR_IT_0_1) {
528 env->condexec_bits &= ~3;
529 env->condexec_bits |= (val >> 25) & 3;
531 if (mask & CPSR_IT_2_7) {
532 env->condexec_bits &= 3;
533 env->condexec_bits |= (val >> 8) & 0xfc;
535 if (mask & CPSR_GE) {
536 env->GE = (val >> 16) & 0xf;
539 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
540 if (bad_mode_switch(env, val & CPSR_M)) {
541 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
542 * We choose to ignore the attempt and leave the CPSR M field
547 switch_mode(env, val & CPSR_M);
550 mask &= ~CACHED_CPSR_BITS;
551 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
554 /* Sign/zero extend */
555 uint32_t HELPER(sxtb16)(uint32_t x)
558 res = (uint16_t)(int8_t)x;
559 res |= (uint32_t)(int8_t)(x >> 16) << 16;
563 uint32_t HELPER(uxtb16)(uint32_t x)
566 res = (uint16_t)(uint8_t)x;
567 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
571 uint32_t HELPER(clz)(uint32_t x)
576 int32_t HELPER(sdiv)(int32_t num, int32_t den)
580 if (num == INT_MIN && den == -1)
585 uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
592 uint32_t HELPER(rbit)(uint32_t x)
594 x = ((x & 0xff000000) >> 24)
595 | ((x & 0x00ff0000) >> 8)
596 | ((x & 0x0000ff00) << 8)
597 | ((x & 0x000000ff) << 24);
598 x = ((x & 0xf0f0f0f0) >> 4)
599 | ((x & 0x0f0f0f0f) << 4);
600 x = ((x & 0x88888888) >> 3)
601 | ((x & 0x44444444) >> 1)
602 | ((x & 0x22222222) << 1)
603 | ((x & 0x11111111) << 3);
607 uint32_t HELPER(abs)(uint32_t x)
609 return ((int32_t)x < 0) ? -x : x;
612 #if defined(CONFIG_USER_ONLY)
614 void do_interrupt (CPUARMState *env)
616 env->exception_index = -1;
619 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
623 env->exception_index = EXCP_PREFETCH_ABORT;
624 env->cp15.c6_insn = address;
626 env->exception_index = EXCP_DATA_ABORT;
627 env->cp15.c6_data = address;
632 /* These should probably raise undefined insn exceptions. */
633 void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
635 int op1 = (insn >> 8) & 0xf;
636 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
640 uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
642 int op1 = (insn >> 8) & 0xf;
643 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
647 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
649 cpu_abort(env, "cp15 insn %08x\n", insn);
652 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
654 cpu_abort(env, "cp15 insn %08x\n", insn);
657 /* These should probably raise undefined insn exceptions. */
658 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
660 cpu_abort(env, "v7m_mrs %d\n", reg);
663 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
665 cpu_abort(env, "v7m_mrs %d\n", reg);
669 void switch_mode(CPUARMState *env, int mode)
671 if (mode != ARM_CPU_MODE_USR)
672 cpu_abort(env, "Tried to switch out of user mode\n");
675 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
677 cpu_abort(env, "banked r13 write\n");
680 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
682 cpu_abort(env, "banked r13 read\n");
688 /* Map CPU modes onto saved register banks. */
689 static inline int bank_number(CPUARMState *env, int mode)
692 case ARM_CPU_MODE_USR:
693 case ARM_CPU_MODE_SYS:
695 case ARM_CPU_MODE_SVC:
697 case ARM_CPU_MODE_ABT:
699 case ARM_CPU_MODE_UND:
701 case ARM_CPU_MODE_IRQ:
703 case ARM_CPU_MODE_FIQ:
706 cpu_abort(env, "Bad mode %x\n", mode);
710 void switch_mode(CPUARMState *env, int mode)
715 old_mode = env->uncached_cpsr & CPSR_M;
716 if (mode == old_mode)
719 if (old_mode == ARM_CPU_MODE_FIQ) {
720 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
721 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
722 } else if (mode == ARM_CPU_MODE_FIQ) {
723 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
724 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
727 i = bank_number(env, old_mode);
728 env->banked_r13[i] = env->regs[13];
729 env->banked_r14[i] = env->regs[14];
730 env->banked_spsr[i] = env->spsr;
732 i = bank_number(env, mode);
733 env->regs[13] = env->banked_r13[i];
734 env->regs[14] = env->banked_r14[i];
735 env->spsr = env->banked_spsr[i];
738 static void v7m_push(CPUARMState *env, uint32_t val)
741 stl_phys(env->regs[13], val);
744 static uint32_t v7m_pop(CPUARMState *env)
747 val = ldl_phys(env->regs[13]);
752 /* Switch to V7M main or process stack pointer. */
753 static void switch_v7m_sp(CPUARMState *env, int process)
756 if (env->v7m.current_sp != process) {
757 tmp = env->v7m.other_sp;
758 env->v7m.other_sp = env->regs[13];
760 env->v7m.current_sp = process;
764 static void do_v7m_exception_exit(CPUARMState *env)
769 type = env->regs[15];
770 if (env->v7m.exception != 0)
771 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
773 /* Switch to the target stack. */
774 switch_v7m_sp(env, (type & 4) != 0);
776 env->regs[0] = v7m_pop(env);
777 env->regs[1] = v7m_pop(env);
778 env->regs[2] = v7m_pop(env);
779 env->regs[3] = v7m_pop(env);
780 env->regs[12] = v7m_pop(env);
781 env->regs[14] = v7m_pop(env);
782 env->regs[15] = v7m_pop(env);
784 xpsr_write(env, xpsr, 0xfffffdff);
785 /* Undo stack alignment. */
788 /* ??? The exception return type specifies Thread/Handler mode. However
789 this is also implied by the xPSR value. Not sure what to do
790 if there is a mismatch. */
791 /* ??? Likewise for mismatches between the CONTROL register and the stack
795 static void do_interrupt_v7m(CPUARMState *env)
797 uint32_t xpsr = xpsr_read(env);
802 if (env->v7m.current_sp)
804 if (env->v7m.exception == 0)
807 /* For exceptions we just mark as pending on the NVIC, and let that
809 /* TODO: Need to escalate if the current priority is higher than the
810 one we're raising. */
811 switch (env->exception_index) {
813 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
817 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
819 case EXCP_PREFETCH_ABORT:
820 case EXCP_DATA_ABORT:
821 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
824 if (semihosting_enabled) {
826 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
829 env->regs[0] = do_arm_semihosting(env);
833 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
836 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
838 case EXCP_EXCEPTION_EXIT:
839 do_v7m_exception_exit(env);
842 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
843 return; /* Never happens. Keep compiler happy. */
846 /* Align stack pointer. */
847 /* ??? Should only do this if Configuration Control Register
848 STACKALIGN bit is set. */
849 if (env->regs[13] & 4) {
853 /* Switch to the handler mode. */
855 v7m_push(env, env->regs[15]);
856 v7m_push(env, env->regs[14]);
857 v7m_push(env, env->regs[12]);
858 v7m_push(env, env->regs[3]);
859 v7m_push(env, env->regs[2]);
860 v7m_push(env, env->regs[1]);
861 v7m_push(env, env->regs[0]);
862 switch_v7m_sp(env, 0);
864 env->condexec_bits = 0;
866 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
867 env->regs[15] = addr & 0xfffffffe;
868 env->thumb = addr & 1;
871 /* Handle a CPU exception. */
872 void do_interrupt(CPUARMState *env)
880 do_interrupt_v7m(env);
883 /* TODO: Vectored interrupt controller. */
884 switch (env->exception_index) {
886 new_mode = ARM_CPU_MODE_UND;
895 if (semihosting_enabled) {
896 /* Check for semihosting interrupt. */
898 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
900 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
903 /* Only intercept calls from privileged modes, to provide some
904 semblance of security. */
905 if (((mask == 0x123456 && !env->thumb)
906 || (mask == 0xab && env->thumb))
907 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
908 env->regs[0] = do_arm_semihosting(env);
912 new_mode = ARM_CPU_MODE_SVC;
915 /* The PC already points to the next instruction. */
919 /* See if this is a semihosting syscall. */
920 if (env->thumb && semihosting_enabled) {
921 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
923 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
925 env->regs[0] = do_arm_semihosting(env);
929 env->cp15.c5_insn = 2;
930 /* Fall through to prefetch abort. */
931 case EXCP_PREFETCH_ABORT:
932 new_mode = ARM_CPU_MODE_ABT;
934 mask = CPSR_A | CPSR_I;
937 case EXCP_DATA_ABORT:
938 new_mode = ARM_CPU_MODE_ABT;
940 mask = CPSR_A | CPSR_I;
944 new_mode = ARM_CPU_MODE_IRQ;
946 /* Disable IRQ and imprecise data aborts. */
947 mask = CPSR_A | CPSR_I;
951 new_mode = ARM_CPU_MODE_FIQ;
953 /* Disable FIQ, IRQ and imprecise data aborts. */
954 mask = CPSR_A | CPSR_I | CPSR_F;
958 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
959 return; /* Never happens. Keep compiler happy. */
962 if (env->cp15.c1_sys & (1 << 13)) {
965 switch_mode (env, new_mode);
966 env->spsr = cpsr_read(env);
968 env->condexec_bits = 0;
969 /* Switch to the new mode, and to the correct instruction set. */
970 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
971 env->uncached_cpsr |= mask;
972 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
973 * and we should just guard the thumb mode on V4 */
974 if (arm_feature(env, ARM_FEATURE_V4T)) {
975 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
977 env->regs[14] = env->regs[15] + offset;
978 env->regs[15] = addr;
979 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
982 /* Check section/page access permissions.
983 Returns the page protection flags, or zero if the access is not
985 static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
986 int access_type, int is_user)
990 if (domain_prot == 3) {
991 return PAGE_READ | PAGE_WRITE;
994 if (access_type == 1)
1001 if (access_type == 1)
1003 switch ((env->cp15.c1_sys >> 8) & 3) {
1005 return is_user ? 0 : PAGE_READ;
1012 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
1017 return PAGE_READ | PAGE_WRITE;
1019 return PAGE_READ | PAGE_WRITE;
1020 case 4: /* Reserved. */
1023 return is_user ? 0 : prot_ro;
1027 if (!arm_feature (env, ARM_FEATURE_V6K))
1035 static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
1039 if (address & env->cp15.c2_mask)
1040 table = env->cp15.c2_base1 & 0xffffc000;
1042 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1044 table |= (address >> 18) & 0x3ffc;
1048 static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
1049 int is_user, uint32_t *phys_ptr, int *prot,
1050 target_ulong *page_size)
1061 /* Pagetable walk. */
1062 /* Lookup l1 descriptor. */
1063 table = get_level1_table_address(env, address);
1064 desc = ldl_phys(table);
1066 domain = (desc >> 5) & 0x0f;
1067 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1069 /* Section translation fault. */
1073 if (domain_prot == 0 || domain_prot == 2) {
1075 code = 9; /* Section domain fault. */
1077 code = 11; /* Page domain fault. */
1082 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1083 ap = (desc >> 10) & 3;
1085 *page_size = 1024 * 1024;
1087 /* Lookup l2 entry. */
1089 /* Coarse pagetable. */
1090 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1092 /* Fine pagetable. */
1093 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1095 desc = ldl_phys(table);
1097 case 0: /* Page translation fault. */
1100 case 1: /* 64k page. */
1101 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1102 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1103 *page_size = 0x10000;
1105 case 2: /* 4k page. */
1106 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1107 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
1108 *page_size = 0x1000;
1110 case 3: /* 1k page. */
1112 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1113 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1115 /* Page translation fault. */
1120 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1122 ap = (desc >> 4) & 3;
1126 /* Never happens, but compiler isn't smart enough to tell. */
1131 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1133 /* Access permission fault. */
1137 *phys_ptr = phys_addr;
1140 return code | (domain << 4);
1143 static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
1144 int is_user, uint32_t *phys_ptr, int *prot,
1145 target_ulong *page_size)
1157 /* Pagetable walk. */
1158 /* Lookup l1 descriptor. */
1159 table = get_level1_table_address(env, address);
1160 desc = ldl_phys(table);
1163 /* Section translation fault. */
1167 } else if (type == 2 && (desc & (1 << 18))) {
1171 /* Section or page. */
1172 domain = (desc >> 5) & 0x0f;
1174 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1175 if (domain_prot == 0 || domain_prot == 2) {
1177 code = 9; /* Section domain fault. */
1179 code = 11; /* Page domain fault. */
1183 if (desc & (1 << 18)) {
1185 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1186 *page_size = 0x1000000;
1189 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1190 *page_size = 0x100000;
1192 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1193 xn = desc & (1 << 4);
1196 /* Lookup l2 entry. */
1197 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1198 desc = ldl_phys(table);
1199 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1201 case 0: /* Page translation fault. */
1204 case 1: /* 64k page. */
1205 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1206 xn = desc & (1 << 15);
1207 *page_size = 0x10000;
1209 case 2: case 3: /* 4k page. */
1210 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1212 *page_size = 0x1000;
1215 /* Never happens, but compiler isn't smart enough to tell. */
1220 if (domain_prot == 3) {
1221 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1223 if (xn && access_type == 2)
1226 /* The simplified model uses AP[0] as an access control bit. */
1227 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1228 /* Access flag fault. */
1229 code = (code == 15) ? 6 : 3;
1232 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
1234 /* Access permission fault. */
1241 *phys_ptr = phys_addr;
1244 return code | (domain << 4);
1247 static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
1248 int is_user, uint32_t *phys_ptr, int *prot)
1254 *phys_ptr = address;
1255 for (n = 7; n >= 0; n--) {
1256 base = env->cp15.c6_region[n];
1257 if ((base & 1) == 0)
1259 mask = 1 << ((base >> 1) & 0x1f);
1260 /* Keep this shift separate from the above to avoid an
1261 (undefined) << 32. */
1262 mask = (mask << 1) - 1;
1263 if (((base ^ address) & ~mask) == 0)
1269 if (access_type == 2) {
1270 mask = env->cp15.c5_insn;
1272 mask = env->cp15.c5_data;
1274 mask = (mask >> (n * 4)) & 0xf;
1281 *prot = PAGE_READ | PAGE_WRITE;
1286 *prot |= PAGE_WRITE;
1289 *prot = PAGE_READ | PAGE_WRITE;
1300 /* Bad permission. */
1307 static inline int get_phys_addr(CPUARMState *env, uint32_t address,
1308 int access_type, int is_user,
1309 uint32_t *phys_ptr, int *prot,
1310 target_ulong *page_size)
1312 /* Fast Context Switch Extension. */
1313 if (address < 0x02000000)
1314 address += env->cp15.c13_fcse;
1316 if ((env->cp15.c1_sys & 1) == 0) {
1317 /* MMU/MPU disabled. */
1318 *phys_ptr = address;
1319 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1320 *page_size = TARGET_PAGE_SIZE;
1322 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
1323 *page_size = TARGET_PAGE_SIZE;
1324 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1326 } else if (env->cp15.c1_sys & (1 << 23)) {
1327 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1330 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1335 int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
1336 int access_type, int mmu_idx)
1339 target_ulong page_size;
1343 is_user = mmu_idx == MMU_USER_IDX;
1344 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1347 /* Map a single [sub]page. */
1348 phys_addr &= ~(uint32_t)0x3ff;
1349 address &= ~(uint32_t)0x3ff;
1350 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
1354 if (access_type == 2) {
1355 env->cp15.c5_insn = ret;
1356 env->cp15.c6_insn = address;
1357 env->exception_index = EXCP_PREFETCH_ABORT;
1359 env->cp15.c5_data = ret;
1360 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1361 env->cp15.c5_data |= (1 << 11);
1362 env->cp15.c6_data = address;
1363 env->exception_index = EXCP_DATA_ABORT;
1368 target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
1371 target_ulong page_size;
1375 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
1383 void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
1385 int cp_num = (insn >> 8) & 0xf;
1386 int cp_info = (insn >> 5) & 7;
1387 int src = (insn >> 16) & 0xf;
1388 int operand = insn & 0xf;
1390 if (env->cp[cp_num].cp_write)
1391 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1392 cp_info, src, operand, val);
1395 uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
1397 int cp_num = (insn >> 8) & 0xf;
1398 int cp_info = (insn >> 5) & 7;
1399 int dest = (insn >> 16) & 0xf;
1400 int operand = insn & 0xf;
1402 if (env->cp[cp_num].cp_read)
1403 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1404 cp_info, dest, operand);
1408 /* Return basic MPU access permission bits. */
1409 static uint32_t simple_mpu_ap_bits(uint32_t val)
1416 for (i = 0; i < 16; i += 2) {
1417 ret |= (val >> i) & mask;
1423 /* Pad basic MPU access permission bits to extended format. */
1424 static uint32_t extended_mpu_ap_bits(uint32_t val)
1431 for (i = 0; i < 16; i += 2) {
1432 ret |= (val & mask) << i;
1438 void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
1444 op1 = (insn >> 21) & 7;
1445 op2 = (insn >> 5) & 7;
1447 switch ((insn >> 16) & 0xf) {
1450 if (arm_feature(env, ARM_FEATURE_XSCALE))
1452 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1454 if (arm_feature(env, ARM_FEATURE_V7)
1455 && op1 == 2 && crm == 0 && op2 == 0) {
1456 env->cp15.c0_cssel = val & 0xf;
1460 case 1: /* System configuration. */
1461 if (arm_feature(env, ARM_FEATURE_V7)
1462 && op1 == 0 && crm == 1 && op2 == 0) {
1463 env->cp15.c1_scr = val;
1466 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1470 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
1471 env->cp15.c1_sys = val;
1472 /* ??? Lots of these bits are not implemented. */
1473 /* This may enable/disable the MMU, so do a TLB flush. */
1476 case 1: /* Auxiliary control register. */
1477 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1478 env->cp15.c1_xscaleauxcr = val;
1481 /* Not implemented. */
1484 if (arm_feature(env, ARM_FEATURE_XSCALE))
1486 if (env->cp15.c1_coproc != val) {
1487 env->cp15.c1_coproc = val;
1488 /* ??? Is this safe when called from within a TB? */
1496 case 2: /* MMU Page table control / MPU cache control. */
1497 if (arm_feature(env, ARM_FEATURE_MPU)) {
1500 env->cp15.c2_data = val;
1503 env->cp15.c2_insn = val;
1511 env->cp15.c2_base0 = val;
1514 env->cp15.c2_base1 = val;
1518 env->cp15.c2_control = val;
1519 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
1520 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
1527 case 3: /* MMU Domain access control / MPU write buffer control. */
1529 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
1531 case 4: /* Reserved. */
1533 case 5: /* MMU Fault status / MPU access permission. */
1534 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1538 if (arm_feature(env, ARM_FEATURE_MPU))
1539 val = extended_mpu_ap_bits(val);
1540 env->cp15.c5_data = val;
1543 if (arm_feature(env, ARM_FEATURE_MPU))
1544 val = extended_mpu_ap_bits(val);
1545 env->cp15.c5_insn = val;
1548 if (!arm_feature(env, ARM_FEATURE_MPU))
1550 env->cp15.c5_data = val;
1553 if (!arm_feature(env, ARM_FEATURE_MPU))
1555 env->cp15.c5_insn = val;
1561 case 6: /* MMU Fault address / MPU base/size. */
1562 if (arm_feature(env, ARM_FEATURE_MPU)) {
1565 env->cp15.c6_region[crm] = val;
1567 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1571 env->cp15.c6_data = val;
1573 case 1: /* ??? This is WFAR on armv6 */
1575 env->cp15.c6_insn = val;
1582 case 7: /* Cache control. */
1583 env->cp15.c15_i_max = 0x000;
1584 env->cp15.c15_i_min = 0xff0;
1588 /* No cache, so nothing to do except VA->PA translations. */
1589 if (arm_feature(env, ARM_FEATURE_VAPA)) {
1592 if (arm_feature(env, ARM_FEATURE_V7)) {
1593 env->cp15.c7_par = val & 0xfffff6ff;
1595 env->cp15.c7_par = val & 0xfffff1ff;
1600 target_ulong page_size;
1602 int ret, is_user = op2 & 2;
1603 int access_type = op2 & 1;
1606 /* Other states are only available with TrustZone */
1609 ret = get_phys_addr(env, val, access_type, is_user,
1610 &phys_addr, &prot, &page_size);
1612 /* We do not set any attribute bits in the PAR */
1613 if (page_size == (1 << 24)
1614 && arm_feature(env, ARM_FEATURE_V7)) {
1615 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1617 env->cp15.c7_par = phys_addr & 0xfffff000;
1620 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1621 ((ret & (12 << 1)) >> 6) |
1622 ((ret & 0xf) << 1) | 1;
1629 case 8: /* MMU TLB control. */
1631 case 0: /* Invalidate all (TLBIALL) */
1634 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
1635 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1637 case 2: /* Invalidate by ASID (TLBIASID) */
1638 tlb_flush(env, val == 0);
1640 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1641 tlb_flush_page(env, val & TARGET_PAGE_MASK);
1648 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1650 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1651 break; /* Ignore ReadBuffer access */
1653 case 0: /* Cache lockdown. */
1655 case 0: /* L1 cache. */
1658 env->cp15.c9_data = val;
1661 env->cp15.c9_insn = val;
1667 case 1: /* L2 cache. */
1668 /* Ignore writes to L2 lockdown/auxiliary registers. */
1674 case 1: /* TCM memory region registers. */
1675 /* Not implemented. */
1677 case 12: /* Performance monitor control */
1678 /* Performance monitors are implementation defined in v7,
1679 * but with an ARM recommended set of registers, which we
1680 * follow (although we don't actually implement any counters)
1682 if (!arm_feature(env, ARM_FEATURE_V7)) {
1686 case 0: /* performance monitor control register */
1687 /* only the DP, X, D and E bits are writable */
1688 env->cp15.c9_pmcr &= ~0x39;
1689 env->cp15.c9_pmcr |= (val & 0x39);
1691 case 1: /* Count enable set register */
1693 env->cp15.c9_pmcnten |= val;
1695 case 2: /* Count enable clear */
1697 env->cp15.c9_pmcnten &= ~val;
1699 case 3: /* Overflow flag status */
1700 env->cp15.c9_pmovsr &= ~val;
1702 case 4: /* Software increment */
1703 /* RAZ/WI since we don't implement the software-count event */
1705 case 5: /* Event counter selection register */
1706 /* Since we don't implement any events, writing to this register
1707 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1714 case 13: /* Performance counters */
1715 if (!arm_feature(env, ARM_FEATURE_V7)) {
1719 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1721 case 1: /* Event type select */
1722 env->cp15.c9_pmxevtyper = val & 0xff;
1724 case 2: /* Event count register */
1725 /* Unimplemented (we have no events), RAZ/WI */
1731 case 14: /* Performance monitor control */
1732 if (!arm_feature(env, ARM_FEATURE_V7)) {
1736 case 0: /* user enable */
1737 env->cp15.c9_pmuserenr = val & 1;
1738 /* changes access rights for cp registers, so flush tbs */
1741 case 1: /* interrupt enable set */
1742 /* We have no event counters so only the C bit can be changed */
1744 env->cp15.c9_pminten |= val;
1746 case 2: /* interrupt enable clear */
1748 env->cp15.c9_pminten &= ~val;
1756 case 10: /* MMU TLB lockdown. */
1757 /* ??? TLB lockdown not implemented. */
1759 case 12: /* Reserved. */
1761 case 13: /* Process ID. */
1764 /* Unlike real hardware the qemu TLB uses virtual addresses,
1765 not modified virtual addresses, so this causes a TLB flush.
1767 if (env->cp15.c13_fcse != val)
1769 env->cp15.c13_fcse = val;
1772 /* This changes the ASID, so do a TLB flush. */
1773 if (env->cp15.c13_context != val
1774 && !arm_feature(env, ARM_FEATURE_MPU))
1776 env->cp15.c13_context = val;
1782 case 14: /* Generic timer */
1783 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1784 /* Dummy implementation: RAZ/WI for all */
1788 case 15: /* Implementation specific. */
1789 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1790 if (op2 == 0 && crm == 1) {
1791 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1792 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1794 env->cp15.c15_cpar = val & 0x3fff;
1800 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1804 case 1: /* Set TI925T configuration. */
1805 env->cp15.c15_ticonfig = val & 0xe7;
1806 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1807 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1809 case 2: /* Set I_max. */
1810 env->cp15.c15_i_max = val;
1812 case 3: /* Set I_min. */
1813 env->cp15.c15_i_min = val;
1815 case 4: /* Set thread-ID. */
1816 env->cp15.c15_threadid = val & 0xffff;
1818 case 8: /* Wait-for-interrupt (deprecated). */
1819 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1825 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1828 if ((op1 == 0) && (op2 == 0)) {
1829 env->cp15.c15_power_control = val;
1830 } else if ((op1 == 0) && (op2 == 1)) {
1831 env->cp15.c15_diagnostic = val;
1832 } else if ((op1 == 0) && (op2 == 2)) {
1833 env->cp15.c15_power_diagnostic = val;
1843 /* ??? For debugging only. Should raise illegal instruction exception. */
1844 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1845 (insn >> 16) & 0xf, crm, op1, op2);
1848 uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
1854 op1 = (insn >> 21) & 7;
1855 op2 = (insn >> 5) & 7;
1857 switch ((insn >> 16) & 0xf) {
1858 case 0: /* ID codes. */
1864 case 0: /* Device ID. */
1865 return env->cp15.c0_cpuid;
1866 case 1: /* Cache Type. */
1867 return env->cp15.c0_cachetype;
1868 case 2: /* TCM status. */
1870 case 3: /* TLB type register. */
1871 return 0; /* No lockable TLB entries. */
1873 /* The MPIDR was standardised in v7; prior to
1874 * this it was implemented only in the 11MPCore.
1875 * For all other pre-v7 cores it does not exist.
1877 if (arm_feature(env, ARM_FEATURE_V7) ||
1878 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1879 int mpidr = env->cpu_index;
1880 /* We don't support setting cluster ID ([8..11])
1881 * so these bits always RAZ.
1883 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1885 /* Cores which are uniprocessor (non-coherent)
1886 * but still implement the MP extensions set
1887 * bit 30. (For instance, A9UP.) However we do
1888 * not currently model any of those cores.
1893 /* otherwise fall through to the unimplemented-reg case */
1898 if (!arm_feature(env, ARM_FEATURE_V6))
1900 return env->cp15.c0_c1[op2];
1902 if (!arm_feature(env, ARM_FEATURE_V6))
1904 return env->cp15.c0_c2[op2];
1905 case 3: case 4: case 5: case 6: case 7:
1911 /* These registers aren't documented on arm11 cores. However
1912 Linux looks at them anyway. */
1913 if (!arm_feature(env, ARM_FEATURE_V6))
1917 if (!arm_feature(env, ARM_FEATURE_V7))
1922 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1924 return env->cp15.c0_clid;
1930 if (op2 != 0 || crm != 0)
1932 return env->cp15.c0_cssel;
1936 case 1: /* System configuration. */
1937 if (arm_feature(env, ARM_FEATURE_V7)
1938 && op1 == 0 && crm == 1 && op2 == 0) {
1939 return env->cp15.c1_scr;
1941 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1944 case 0: /* Control register. */
1945 return env->cp15.c1_sys;
1946 case 1: /* Auxiliary control register. */
1947 if (arm_feature(env, ARM_FEATURE_XSCALE))
1948 return env->cp15.c1_xscaleauxcr;
1949 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1951 switch (ARM_CPUID(env)) {
1952 case ARM_CPUID_ARM1026:
1954 case ARM_CPUID_ARM1136:
1955 case ARM_CPUID_ARM1136_R2:
1956 case ARM_CPUID_ARM1176:
1958 case ARM_CPUID_ARM11MPCORE:
1960 case ARM_CPUID_CORTEXA8:
1962 case ARM_CPUID_CORTEXA9:
1963 case ARM_CPUID_CORTEXA15:
1968 case 2: /* Coprocessor access register. */
1969 if (arm_feature(env, ARM_FEATURE_XSCALE))
1971 return env->cp15.c1_coproc;
1975 case 2: /* MMU Page table control / MPU cache control. */
1976 if (arm_feature(env, ARM_FEATURE_MPU)) {
1979 return env->cp15.c2_data;
1982 return env->cp15.c2_insn;
1990 return env->cp15.c2_base0;
1992 return env->cp15.c2_base1;
1994 return env->cp15.c2_control;
1999 case 3: /* MMU Domain access control / MPU write buffer control. */
2000 return env->cp15.c3;
2001 case 4: /* Reserved. */
2003 case 5: /* MMU Fault status / MPU access permission. */
2004 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2008 if (arm_feature(env, ARM_FEATURE_MPU))
2009 return simple_mpu_ap_bits(env->cp15.c5_data);
2010 return env->cp15.c5_data;
2012 if (arm_feature(env, ARM_FEATURE_MPU))
2013 return simple_mpu_ap_bits(env->cp15.c5_insn);
2014 return env->cp15.c5_insn;
2016 if (!arm_feature(env, ARM_FEATURE_MPU))
2018 return env->cp15.c5_data;
2020 if (!arm_feature(env, ARM_FEATURE_MPU))
2022 return env->cp15.c5_insn;
2026 case 6: /* MMU Fault address. */
2027 if (arm_feature(env, ARM_FEATURE_MPU)) {
2030 return env->cp15.c6_region[crm];
2032 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2036 return env->cp15.c6_data;
2038 if (arm_feature(env, ARM_FEATURE_V6)) {
2039 /* Watchpoint Fault Adrress. */
2040 return 0; /* Not implemented. */
2042 /* Instruction Fault Adrress. */
2043 /* Arm9 doesn't have an IFAR, but implementing it anyway
2044 shouldn't do any harm. */
2045 return env->cp15.c6_insn;
2048 if (arm_feature(env, ARM_FEATURE_V6)) {
2049 /* Instruction Fault Adrress. */
2050 return env->cp15.c6_insn;
2058 case 7: /* Cache control. */
2059 if (crm == 4 && op1 == 0 && op2 == 0) {
2060 return env->cp15.c7_par;
2062 /* FIXME: Should only clear Z flag if destination is r15. */
2065 case 8: /* MMU TLB control. */
2069 case 0: /* Cache lockdown */
2071 case 0: /* L1 cache. */
2072 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2077 return env->cp15.c9_data;
2079 return env->cp15.c9_insn;
2083 case 1: /* L2 cache */
2084 /* L2 Lockdown and Auxiliary control. */
2087 /* L2 cache lockdown (A8 only) */
2090 /* L2 cache auxiliary control (A8) or control (A15) */
2091 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
2092 /* Linux wants the number of processors from here.
2093 * Might as well set the interrupt-controller bit too.
2095 return ((smp_cpus - 1) << 24) | (1 << 23);
2099 /* L2 cache extended control (A15) */
2108 case 12: /* Performance monitor control */
2109 if (!arm_feature(env, ARM_FEATURE_V7)) {
2113 case 0: /* performance monitor control register */
2114 return env->cp15.c9_pmcr;
2115 case 1: /* count enable set */
2116 case 2: /* count enable clear */
2117 return env->cp15.c9_pmcnten;
2118 case 3: /* overflow flag status */
2119 return env->cp15.c9_pmovsr;
2120 case 4: /* software increment */
2121 case 5: /* event counter selection register */
2122 return 0; /* Unimplemented, RAZ/WI */
2126 case 13: /* Performance counters */
2127 if (!arm_feature(env, ARM_FEATURE_V7)) {
2131 case 1: /* Event type select */
2132 return env->cp15.c9_pmxevtyper;
2133 case 0: /* Cycle count register */
2134 case 2: /* Event count register */
2135 /* Unimplemented, so RAZ/WI */
2140 case 14: /* Performance monitor control */
2141 if (!arm_feature(env, ARM_FEATURE_V7)) {
2145 case 0: /* user enable */
2146 return env->cp15.c9_pmuserenr;
2147 case 1: /* interrupt enable set */
2148 case 2: /* interrupt enable clear */
2149 return env->cp15.c9_pminten;
2157 case 10: /* MMU TLB lockdown. */
2158 /* ??? TLB lockdown not implemented. */
2160 case 11: /* TCM DMA control. */
2161 case 12: /* Reserved. */
2163 case 13: /* Process ID. */
2166 return env->cp15.c13_fcse;
2168 return env->cp15.c13_context;
2172 case 14: /* Generic timer */
2173 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
2174 /* Dummy implementation: RAZ/WI for all */
2178 case 15: /* Implementation specific. */
2179 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
2180 if (op2 == 0 && crm == 1)
2181 return env->cp15.c15_cpar;
2185 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2189 case 1: /* Read TI925T configuration. */
2190 return env->cp15.c15_ticonfig;
2191 case 2: /* Read I_max. */
2192 return env->cp15.c15_i_max;
2193 case 3: /* Read I_min. */
2194 return env->cp15.c15_i_min;
2195 case 4: /* Read thread-ID. */
2196 return env->cp15.c15_threadid;
2197 case 8: /* TI925T_status */
2200 /* TODO: Peripheral port remap register:
2201 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2202 * controller base address at $rn & ~0xfff and map size of
2203 * 0x200 << ($rn & 0xfff), when MMU is off. */
2206 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
2209 if ((op1 == 4) && (op2 == 0)) {
2210 /* The config_base_address should hold the value of
2211 * the peripheral base. ARM should get this from a CPU
2212 * object property, but that support isn't available in
2213 * December 2011. Default to 0 for now and board models
2214 * that care can set it by a private hook */
2215 return env->cp15.c15_config_base_address;
2216 } else if ((op1 == 0) && (op2 == 0)) {
2217 /* power_control should be set to maximum latency. Again,
2218 default to 0 and set by private hook */
2219 return env->cp15.c15_power_control;
2220 } else if ((op1 == 0) && (op2 == 1)) {
2221 return env->cp15.c15_diagnostic;
2222 } else if ((op1 == 0) && (op2 == 2)) {
2223 return env->cp15.c15_power_diagnostic;
2226 case 1: /* NEON Busy */
2228 case 5: /* tlb lockdown */
2231 if ((op1 == 5) && (op2 == 2)) {
2243 /* ??? For debugging only. Should raise illegal instruction exception. */
2244 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2245 (insn >> 16) & 0xf, crm, op1, op2);
2249 void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
2251 if ((env->uncached_cpsr & CPSR_M) == mode) {
2252 env->regs[13] = val;
2254 env->banked_r13[bank_number(env, mode)] = val;
2258 uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
2260 if ((env->uncached_cpsr & CPSR_M) == mode) {
2261 return env->regs[13];
2263 return env->banked_r13[bank_number(env, mode)];
2267 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
2271 return xpsr_read(env) & 0xf8000000;
2273 return xpsr_read(env) & 0xf80001ff;
2275 return xpsr_read(env) & 0xff00fc00;
2277 return xpsr_read(env) & 0xff00fdff;
2279 return xpsr_read(env) & 0x000001ff;
2281 return xpsr_read(env) & 0x0700fc00;
2283 return xpsr_read(env) & 0x0700edff;
2285 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2287 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2288 case 16: /* PRIMASK */
2289 return (env->uncached_cpsr & CPSR_I) != 0;
2290 case 17: /* BASEPRI */
2291 case 18: /* BASEPRI_MAX */
2292 return env->v7m.basepri;
2293 case 19: /* FAULTMASK */
2294 return (env->uncached_cpsr & CPSR_F) != 0;
2295 case 20: /* CONTROL */
2296 return env->v7m.control;
2298 /* ??? For debugging only. */
2299 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2304 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
2308 xpsr_write(env, val, 0xf8000000);
2311 xpsr_write(env, val, 0xf8000000);
2314 xpsr_write(env, val, 0xfe00fc00);
2317 xpsr_write(env, val, 0xfe00fc00);
2320 /* IPSR bits are readonly. */
2323 xpsr_write(env, val, 0x0600fc00);
2326 xpsr_write(env, val, 0x0600fc00);
2329 if (env->v7m.current_sp)
2330 env->v7m.other_sp = val;
2332 env->regs[13] = val;
2335 if (env->v7m.current_sp)
2336 env->regs[13] = val;
2338 env->v7m.other_sp = val;
2340 case 16: /* PRIMASK */
2342 env->uncached_cpsr |= CPSR_I;
2344 env->uncached_cpsr &= ~CPSR_I;
2346 case 17: /* BASEPRI */
2347 env->v7m.basepri = val & 0xff;
2349 case 18: /* BASEPRI_MAX */
2351 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2352 env->v7m.basepri = val;
2354 case 19: /* FAULTMASK */
2356 env->uncached_cpsr |= CPSR_F;
2358 env->uncached_cpsr &= ~CPSR_F;
2360 case 20: /* CONTROL */
2361 env->v7m.control = val & 3;
2362 switch_v7m_sp(env, (val & 2) != 0);
2365 /* ??? For debugging only. */
2366 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2371 void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
2372 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
2375 if (cpnum < 0 || cpnum > 14) {
2376 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2380 env->cp[cpnum].cp_read = cp_read;
2381 env->cp[cpnum].cp_write = cp_write;
2382 env->cp[cpnum].opaque = opaque;
2387 /* Note that signed overflow is undefined in C. The following routines are
2388 careful to use unsigned types where modulo arithmetic is required.
2389 Failure to do so _will_ break on newer gcc. */
2391 /* Signed saturating arithmetic. */
2393 /* Perform 16-bit signed saturating addition. */
2394 static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2399 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2408 /* Perform 8-bit signed saturating addition. */
2409 static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2414 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2423 /* Perform 16-bit signed saturating subtraction. */
2424 static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2429 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2438 /* Perform 8-bit signed saturating subtraction. */
2439 static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2444 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2453 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2454 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2455 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2456 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2459 #include "op_addsub.h"
2461 /* Unsigned saturating arithmetic. */
2462 static inline uint16_t add16_usat(uint16_t a, uint16_t b)
2471 static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
2479 static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2488 static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2496 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2497 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2498 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2499 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2502 #include "op_addsub.h"
2504 /* Signed modulo arithmetic. */
2505 #define SARITH16(a, b, n, op) do { \
2507 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
2508 RESULT(sum, n, 16); \
2510 ge |= 3 << (n * 2); \
2513 #define SARITH8(a, b, n, op) do { \
2515 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
2516 RESULT(sum, n, 8); \
2522 #define ADD16(a, b, n) SARITH16(a, b, n, +)
2523 #define SUB16(a, b, n) SARITH16(a, b, n, -)
2524 #define ADD8(a, b, n) SARITH8(a, b, n, +)
2525 #define SUB8(a, b, n) SARITH8(a, b, n, -)
2529 #include "op_addsub.h"
2531 /* Unsigned modulo arithmetic. */
2532 #define ADD16(a, b, n) do { \
2534 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2535 RESULT(sum, n, 16); \
2536 if ((sum >> 16) == 1) \
2537 ge |= 3 << (n * 2); \
2540 #define ADD8(a, b, n) do { \
2542 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2543 RESULT(sum, n, 8); \
2544 if ((sum >> 8) == 1) \
2548 #define SUB16(a, b, n) do { \
2550 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2551 RESULT(sum, n, 16); \
2552 if ((sum >> 16) == 0) \
2553 ge |= 3 << (n * 2); \
2556 #define SUB8(a, b, n) do { \
2558 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2559 RESULT(sum, n, 8); \
2560 if ((sum >> 8) == 0) \
2567 #include "op_addsub.h"
2569 /* Halved signed arithmetic. */
2570 #define ADD16(a, b, n) \
2571 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2572 #define SUB16(a, b, n) \
2573 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2574 #define ADD8(a, b, n) \
2575 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2576 #define SUB8(a, b, n) \
2577 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2580 #include "op_addsub.h"
2582 /* Halved unsigned arithmetic. */
2583 #define ADD16(a, b, n) \
2584 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2585 #define SUB16(a, b, n) \
2586 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2587 #define ADD8(a, b, n) \
2588 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2589 #define SUB8(a, b, n) \
2590 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2593 #include "op_addsub.h"
2595 static inline uint8_t do_usad(uint8_t a, uint8_t b)
2603 /* Unsigned sum of absolute byte differences. */
2604 uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2607 sum = do_usad(a, b);
2608 sum += do_usad(a >> 8, b >> 8);
2609 sum += do_usad(a >> 16, b >>16);
2610 sum += do_usad(a >> 24, b >> 24);
2614 /* For ARMv6 SEL instruction. */
2615 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2628 return (a & mask) | (b & ~mask);
2631 uint32_t HELPER(logicq_cc)(uint64_t val)
2633 return (val >> 32) | (val != 0);
2636 /* VFP support. We follow the convention used for VFP instrunctions:
2637 Single precition routines have a "s" suffix, double precision a
2640 /* Convert host exception flags to vfp form. */
2641 static inline int vfp_exceptbits_from_host(int host_bits)
2643 int target_bits = 0;
2645 if (host_bits & float_flag_invalid)
2647 if (host_bits & float_flag_divbyzero)
2649 if (host_bits & float_flag_overflow)
2651 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
2653 if (host_bits & float_flag_inexact)
2654 target_bits |= 0x10;
2655 if (host_bits & float_flag_input_denormal)
2656 target_bits |= 0x80;
2660 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2665 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2666 | (env->vfp.vec_len << 16)
2667 | (env->vfp.vec_stride << 20);
2668 i = get_float_exception_flags(&env->vfp.fp_status);
2669 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
2670 fpscr |= vfp_exceptbits_from_host(i);
2674 uint32_t vfp_get_fpscr(CPUARMState *env)
2676 return HELPER(vfp_get_fpscr)(env);
2679 /* Convert vfp exception flags to target form. */
2680 static inline int vfp_exceptbits_to_host(int target_bits)
2684 if (target_bits & 1)
2685 host_bits |= float_flag_invalid;
2686 if (target_bits & 2)
2687 host_bits |= float_flag_divbyzero;
2688 if (target_bits & 4)
2689 host_bits |= float_flag_overflow;
2690 if (target_bits & 8)
2691 host_bits |= float_flag_underflow;
2692 if (target_bits & 0x10)
2693 host_bits |= float_flag_inexact;
2694 if (target_bits & 0x80)
2695 host_bits |= float_flag_input_denormal;
2699 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
2704 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2705 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2706 env->vfp.vec_len = (val >> 16) & 7;
2707 env->vfp.vec_stride = (val >> 20) & 3;
2710 if (changed & (3 << 22)) {
2711 i = (val >> 22) & 3;
2714 i = float_round_nearest_even;
2720 i = float_round_down;
2723 i = float_round_to_zero;
2726 set_float_rounding_mode(i, &env->vfp.fp_status);
2728 if (changed & (1 << 24)) {
2729 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2730 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2732 if (changed & (1 << 25))
2733 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
2735 i = vfp_exceptbits_to_host(val);
2736 set_float_exception_flags(i, &env->vfp.fp_status);
2737 set_float_exception_flags(0, &env->vfp.standard_fp_status);
2740 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
2742 HELPER(vfp_set_fpscr)(env, val);
2745 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2747 #define VFP_BINOP(name) \
2748 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
2750 float_status *fpst = fpstp; \
2751 return float32_ ## name(a, b, fpst); \
2753 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
2755 float_status *fpst = fpstp; \
2756 return float64_ ## name(a, b, fpst); \
2764 float32 VFP_HELPER(neg, s)(float32 a)
2766 return float32_chs(a);
2769 float64 VFP_HELPER(neg, d)(float64 a)
2771 return float64_chs(a);
2774 float32 VFP_HELPER(abs, s)(float32 a)
2776 return float32_abs(a);
2779 float64 VFP_HELPER(abs, d)(float64 a)
2781 return float64_abs(a);
2784 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
2786 return float32_sqrt(a, &env->vfp.fp_status);
2789 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
2791 return float64_sqrt(a, &env->vfp.fp_status);
2794 /* XXX: check quiet/signaling case */
2795 #define DO_VFP_cmp(p, type) \
2796 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
2799 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2800 case 0: flags = 0x6; break; \
2801 case -1: flags = 0x8; break; \
2802 case 1: flags = 0x2; break; \
2803 default: case 2: flags = 0x3; break; \
2805 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2806 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2808 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
2811 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2812 case 0: flags = 0x6; break; \
2813 case -1: flags = 0x8; break; \
2814 case 1: flags = 0x2; break; \
2815 default: case 2: flags = 0x3; break; \
2817 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2818 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2820 DO_VFP_cmp(s, float32)
2821 DO_VFP_cmp(d, float64)
2824 /* Integer to float and float to integer conversions */
2826 #define CONV_ITOF(name, fsz, sign) \
2827 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2829 float_status *fpst = fpstp; \
2830 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
2833 #define CONV_FTOI(name, fsz, sign, round) \
2834 uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2836 float_status *fpst = fpstp; \
2837 if (float##fsz##_is_any_nan(x)) { \
2838 float_raise(float_flag_invalid, fpst); \
2841 return float##fsz##_to_##sign##int32##round(x, fpst); \
2844 #define FLOAT_CONVS(name, p, fsz, sign) \
2845 CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2846 CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2847 CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
2849 FLOAT_CONVS(si, s, 32, )
2850 FLOAT_CONVS(si, d, 64, )
2851 FLOAT_CONVS(ui, s, 32, u)
2852 FLOAT_CONVS(ui, d, 64, u)
2858 /* floating point conversion */
2859 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
2861 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2862 /* ARM requires that S<->D conversion of any kind of NaN generates
2863 * a quiet NaN by forcing the most significant frac bit to 1.
2865 return float64_maybe_silence_nan(r);
2868 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
2870 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2871 /* ARM requires that S<->D conversion of any kind of NaN generates
2872 * a quiet NaN by forcing the most significant frac bit to 1.
2874 return float32_maybe_silence_nan(r);
2877 /* VFP3 fixed point conversion. */
2878 #define VFP_CONV_FIX(name, p, fsz, itype, sign) \
2879 float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2882 float_status *fpst = fpstp; \
2884 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2885 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
2887 uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2890 float_status *fpst = fpstp; \
2892 if (float##fsz##_is_any_nan(x)) { \
2893 float_raise(float_flag_invalid, fpst); \
2896 tmp = float##fsz##_scalbn(x, shift, fpst); \
2897 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
2900 VFP_CONV_FIX(sh, d, 64, int16, )
2901 VFP_CONV_FIX(sl, d, 64, int32, )
2902 VFP_CONV_FIX(uh, d, 64, uint16, u)
2903 VFP_CONV_FIX(ul, d, 64, uint32, u)
2904 VFP_CONV_FIX(sh, s, 32, int16, )
2905 VFP_CONV_FIX(sl, s, 32, int32, )
2906 VFP_CONV_FIX(uh, s, 32, uint16, u)
2907 VFP_CONV_FIX(ul, s, 32, uint32, u)
2910 /* Half precision conversions. */
2911 static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
2913 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2914 float32 r = float16_to_float32(make_float16(a), ieee, s);
2916 return float32_maybe_silence_nan(r);
2921 static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
2923 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2924 float16 r = float32_to_float16(a, ieee, s);
2926 r = float16_maybe_silence_nan(r);
2928 return float16_val(r);
2931 float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2933 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2936 uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2938 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2941 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
2943 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2946 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
2948 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2951 #define float32_two make_float32(0x40000000)
2952 #define float32_three make_float32(0x40400000)
2953 #define float32_one_point_five make_float32(0x3fc00000)
2955 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
2957 float_status *s = &env->vfp.standard_fp_status;
2958 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2959 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2960 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2961 float_raise(float_flag_input_denormal, s);
2965 return float32_sub(float32_two, float32_mul(a, b, s), s);
2968 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
2970 float_status *s = &env->vfp.standard_fp_status;
2972 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2973 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
2974 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2975 float_raise(float_flag_input_denormal, s);
2977 return float32_one_point_five;
2979 product = float32_mul(a, b, s);
2980 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
2985 /* Constants 256 and 512 are used in some helpers; we avoid relying on
2986 * int->float conversions at run-time. */
2987 #define float64_256 make_float64(0x4070000000000000LL)
2988 #define float64_512 make_float64(0x4080000000000000LL)
2990 /* The algorithm that must be used to calculate the estimate
2991 * is specified by the ARM ARM.
2993 static float64 recip_estimate(float64 a, CPUARMState *env)
2995 /* These calculations mustn't set any fp exception flags,
2996 * so we use a local copy of the fp_status.
2998 float_status dummy_status = env->vfp.standard_fp_status;
2999 float_status *s = &dummy_status;
3000 /* q = (int)(a * 512.0) */
3001 float64 q = float64_mul(float64_512, a, s);
3002 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3004 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
3005 q = int64_to_float64(q_int, s);
3006 q = float64_add(q, float64_half, s);
3007 q = float64_div(q, float64_512, s);
3008 q = float64_div(float64_one, q, s);
3010 /* s = (int)(256.0 * r + 0.5) */
3011 q = float64_mul(q, float64_256, s);
3012 q = float64_add(q, float64_half, s);
3013 q_int = float64_to_int64_round_to_zero(q, s);
3015 /* return (double)s / 256.0 */
3016 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3019 float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
3021 float_status *s = &env->vfp.standard_fp_status;
3023 uint32_t val32 = float32_val(a);
3026 int a_exp = (val32 & 0x7f800000) >> 23;
3027 int sign = val32 & 0x80000000;
3029 if (float32_is_any_nan(a)) {
3030 if (float32_is_signaling_nan(a)) {
3031 float_raise(float_flag_invalid, s);
3033 return float32_default_nan;
3034 } else if (float32_is_infinity(a)) {
3035 return float32_set_sign(float32_zero, float32_is_neg(a));
3036 } else if (float32_is_zero_or_denormal(a)) {
3037 if (!float32_is_zero(a)) {
3038 float_raise(float_flag_input_denormal, s);
3040 float_raise(float_flag_divbyzero, s);
3041 return float32_set_sign(float32_infinity, float32_is_neg(a));
3042 } else if (a_exp >= 253) {
3043 float_raise(float_flag_underflow, s);
3044 return float32_set_sign(float32_zero, float32_is_neg(a));
3047 f64 = make_float64((0x3feULL << 52)
3048 | ((int64_t)(val32 & 0x7fffff) << 29));
3050 result_exp = 253 - a_exp;
3052 f64 = recip_estimate(f64, env);
3055 | ((result_exp & 0xff) << 23)
3056 | ((float64_val(f64) >> 29) & 0x7fffff);
3057 return make_float32(val32);
3060 /* The algorithm that must be used to calculate the estimate
3061 * is specified by the ARM ARM.
3063 static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
3065 /* These calculations mustn't set any fp exception flags,
3066 * so we use a local copy of the fp_status.
3068 float_status dummy_status = env->vfp.standard_fp_status;
3069 float_status *s = &dummy_status;
3073 if (float64_lt(a, float64_half, s)) {
3074 /* range 0.25 <= a < 0.5 */
3076 /* a in units of 1/512 rounded down */
3077 /* q0 = (int)(a * 512.0); */
3078 q = float64_mul(float64_512, a, s);
3079 q_int = float64_to_int64_round_to_zero(q, s);
3081 /* reciprocal root r */
3082 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
3083 q = int64_to_float64(q_int, s);
3084 q = float64_add(q, float64_half, s);
3085 q = float64_div(q, float64_512, s);
3086 q = float64_sqrt(q, s);
3087 q = float64_div(float64_one, q, s);
3089 /* range 0.5 <= a < 1.0 */
3091 /* a in units of 1/256 rounded down */
3092 /* q1 = (int)(a * 256.0); */
3093 q = float64_mul(float64_256, a, s);
3094 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3096 /* reciprocal root r */
3097 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3098 q = int64_to_float64(q_int, s);
3099 q = float64_add(q, float64_half, s);
3100 q = float64_div(q, float64_256, s);
3101 q = float64_sqrt(q, s);
3102 q = float64_div(float64_one, q, s);
3104 /* r in units of 1/256 rounded to nearest */
3105 /* s = (int)(256.0 * r + 0.5); */
3107 q = float64_mul(q, float64_256,s );
3108 q = float64_add(q, float64_half, s);
3109 q_int = float64_to_int64_round_to_zero(q, s);
3111 /* return (double)s / 256.0;*/
3112 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3115 float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
3117 float_status *s = &env->vfp.standard_fp_status;
3123 val = float32_val(a);
3125 if (float32_is_any_nan(a)) {
3126 if (float32_is_signaling_nan(a)) {
3127 float_raise(float_flag_invalid, s);
3129 return float32_default_nan;
3130 } else if (float32_is_zero_or_denormal(a)) {
3131 if (!float32_is_zero(a)) {
3132 float_raise(float_flag_input_denormal, s);
3134 float_raise(float_flag_divbyzero, s);
3135 return float32_set_sign(float32_infinity, float32_is_neg(a));
3136 } else if (float32_is_neg(a)) {
3137 float_raise(float_flag_invalid, s);
3138 return float32_default_nan;
3139 } else if (float32_is_infinity(a)) {
3140 return float32_zero;
3143 /* Normalize to a double-precision value between 0.25 and 1.0,
3144 * preserving the parity of the exponent. */
3145 if ((val & 0x800000) == 0) {
3146 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3148 | ((uint64_t)(val & 0x7fffff) << 29));
3150 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3152 | ((uint64_t)(val & 0x7fffff) << 29));
3155 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3157 f64 = recip_sqrt_estimate(f64, env);
3159 val64 = float64_val(f64);
3161 val = ((result_exp & 0xff) << 23)
3162 | ((val64 >> 29) & 0x7fffff);
3163 return make_float32(val);
3166 uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
3170 if ((a & 0x80000000) == 0) {
3174 f64 = make_float64((0x3feULL << 52)
3175 | ((int64_t)(a & 0x7fffffff) << 21));
3177 f64 = recip_estimate (f64, env);
3179 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3182 uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
3186 if ((a & 0xc0000000) == 0) {
3190 if (a & 0x80000000) {
3191 f64 = make_float64((0x3feULL << 52)
3192 | ((uint64_t)(a & 0x7fffffff) << 21));
3193 } else { /* bits 31-30 == '01' */
3194 f64 = make_float64((0x3fdULL << 52)
3195 | ((uint64_t)(a & 0x3fffffff) << 22));
3198 f64 = recip_sqrt_estimate(f64, env);
3200 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
3203 /* VFPv4 fused multiply-accumulate */
3204 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3206 float_status *fpst = fpstp;
3207 return float32_muladd(a, b, c, 0, fpst);
3210 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3212 float_status *fpst = fpstp;
3213 return float64_muladd(a, b, c, 0, fpst);
3216 void HELPER(set_teecr)(CPUARMState *env, uint32_t val)
3219 if (env->teecr != val) {