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