blob: cb95c6eaba689ade642ccc42d102c70efc93cb7c [file] [log] [blame]
bellardb5ff1b32005-11-26 10:38:39 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "cpu.h"
6#include "exec-all.h"
pbrook9ee6e8b2007-11-11 00:04:49 +00007#include "gdbstub.h"
pbrookb26eefb2008-03-31 03:44:26 +00008#include "helpers.h"
aurel32ca10f862008-04-11 21:35:42 +00009#include "qemu-common.h"
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +020010#include "host-utils.h"
pbrook9ee6e8b2007-11-11 00:04:49 +000011
12static uint32_t cortexa8_cp15_c0_c1[8] =
13{ 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
14
15static uint32_t cortexa8_cp15_c0_c2[8] =
16{ 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
17
18static uint32_t mpcore_cp15_c0_c1[8] =
19{ 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
20
21static uint32_t mpcore_cp15_c0_c2[8] =
22{ 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
23
24static uint32_t arm1136_cp15_c0_c1[8] =
25{ 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
26
27static uint32_t arm1136_cp15_c0_c2[8] =
28{ 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
bellardb5ff1b32005-11-26 10:38:39 +000029
bellardaaed9092007-11-10 15:15:54 +000030static uint32_t cpu_arm_find_by_name(const char *name);
31
pbrookf3d6b952007-03-11 13:03:18 +000032static inline void set_feature(CPUARMState *env, int feature)
33{
34 env->features |= 1u << feature;
35}
36
37static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
38{
39 env->cp15.c0_cpuid = id;
40 switch (id) {
41 case ARM_CPUID_ARM926:
42 set_feature(env, ARM_FEATURE_VFP);
43 env->vfp.xregs[ARM_VFP_FPSID] = 0x41011090;
balrogc1713132007-04-30 01:26:42 +000044 env->cp15.c0_cachetype = 0x1dd20d2;
balrog610c3c82007-06-24 12:09:48 +000045 env->cp15.c1_sys = 0x00090078;
pbrookf3d6b952007-03-11 13:03:18 +000046 break;
pbrookce819862007-05-08 02:30:40 +000047 case ARM_CPUID_ARM946:
48 set_feature(env, ARM_FEATURE_MPU);
49 env->cp15.c0_cachetype = 0x0f004006;
balrog610c3c82007-06-24 12:09:48 +000050 env->cp15.c1_sys = 0x00000078;
pbrookce819862007-05-08 02:30:40 +000051 break;
pbrookf3d6b952007-03-11 13:03:18 +000052 case ARM_CPUID_ARM1026:
53 set_feature(env, ARM_FEATURE_VFP);
54 set_feature(env, ARM_FEATURE_AUXCR);
55 env->vfp.xregs[ARM_VFP_FPSID] = 0x410110a0;
balrogc1713132007-04-30 01:26:42 +000056 env->cp15.c0_cachetype = 0x1dd20d2;
balrog610c3c82007-06-24 12:09:48 +000057 env->cp15.c1_sys = 0x00090078;
balrogc1713132007-04-30 01:26:42 +000058 break;
balrog827df9f2008-04-14 21:05:22 +000059 case ARM_CPUID_ARM1136_R2:
pbrook9ee6e8b2007-11-11 00:04:49 +000060 case ARM_CPUID_ARM1136:
61 set_feature(env, ARM_FEATURE_V6);
62 set_feature(env, ARM_FEATURE_VFP);
63 set_feature(env, ARM_FEATURE_AUXCR);
64 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
65 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
66 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
67 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +000068 memcpy(env->cp15.c0_c2, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +000069 env->cp15.c0_cachetype = 0x1dd20d2;
70 break;
71 case ARM_CPUID_ARM11MPCORE:
72 set_feature(env, ARM_FEATURE_V6);
73 set_feature(env, ARM_FEATURE_V6K);
74 set_feature(env, ARM_FEATURE_VFP);
75 set_feature(env, ARM_FEATURE_AUXCR);
76 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
77 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
78 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
79 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +000080 memcpy(env->cp15.c0_c2, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +000081 env->cp15.c0_cachetype = 0x1dd20d2;
82 break;
83 case ARM_CPUID_CORTEXA8:
84 set_feature(env, ARM_FEATURE_V6);
85 set_feature(env, ARM_FEATURE_V6K);
86 set_feature(env, ARM_FEATURE_V7);
87 set_feature(env, ARM_FEATURE_AUXCR);
88 set_feature(env, ARM_FEATURE_THUMB2);
89 set_feature(env, ARM_FEATURE_VFP);
90 set_feature(env, ARM_FEATURE_VFP3);
91 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +000092 set_feature(env, ARM_FEATURE_THUMB2EE);
pbrook9ee6e8b2007-11-11 00:04:49 +000093 env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
94 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
95 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
96 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +000097 memcpy(env->cp15.c0_c2, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrooka49ea272008-12-19 13:37:53 +000098 env->cp15.c0_cachetype = 0x82048004;
99 env->cp15.c0_clid = (1 << 27) | (2 << 24) | 3;
100 env->cp15.c0_ccsid[0] = 0xe007e01a; /* 16k L1 dcache. */
101 env->cp15.c0_ccsid[1] = 0x2007e01a; /* 16k L1 icache. */
102 env->cp15.c0_ccsid[2] = 0xf0000000; /* No L2 icache. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000103 break;
104 case ARM_CPUID_CORTEXM3:
105 set_feature(env, ARM_FEATURE_V6);
106 set_feature(env, ARM_FEATURE_THUMB2);
107 set_feature(env, ARM_FEATURE_V7);
108 set_feature(env, ARM_FEATURE_M);
109 set_feature(env, ARM_FEATURE_DIV);
110 break;
111 case ARM_CPUID_ANY: /* For userspace emulation. */
112 set_feature(env, ARM_FEATURE_V6);
113 set_feature(env, ARM_FEATURE_V6K);
114 set_feature(env, ARM_FEATURE_V7);
115 set_feature(env, ARM_FEATURE_THUMB2);
116 set_feature(env, ARM_FEATURE_VFP);
117 set_feature(env, ARM_FEATURE_VFP3);
Paul Brook60011492009-11-19 16:45:20 +0000118 set_feature(env, ARM_FEATURE_VFP_FP16);
pbrook9ee6e8b2007-11-11 00:04:49 +0000119 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +0000120 set_feature(env, ARM_FEATURE_THUMB2EE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000121 set_feature(env, ARM_FEATURE_DIV);
122 break;
balrogc3d26892007-07-29 17:57:26 +0000123 case ARM_CPUID_TI915T:
124 case ARM_CPUID_TI925T:
125 set_feature(env, ARM_FEATURE_OMAPCP);
126 env->cp15.c0_cpuid = ARM_CPUID_TI925T; /* Depends on wiring. */
127 env->cp15.c0_cachetype = 0x5109149;
128 env->cp15.c1_sys = 0x00000070;
129 env->cp15.c15_i_max = 0x000;
130 env->cp15.c15_i_min = 0xff0;
131 break;
balrogc1713132007-04-30 01:26:42 +0000132 case ARM_CPUID_PXA250:
133 case ARM_CPUID_PXA255:
134 case ARM_CPUID_PXA260:
135 case ARM_CPUID_PXA261:
136 case ARM_CPUID_PXA262:
137 set_feature(env, ARM_FEATURE_XSCALE);
138 /* JTAG_ID is ((id << 28) | 0x09265013) */
139 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000140 env->cp15.c1_sys = 0x00000078;
balrogc1713132007-04-30 01:26:42 +0000141 break;
142 case ARM_CPUID_PXA270_A0:
143 case ARM_CPUID_PXA270_A1:
144 case ARM_CPUID_PXA270_B0:
145 case ARM_CPUID_PXA270_B1:
146 case ARM_CPUID_PXA270_C0:
147 case ARM_CPUID_PXA270_C5:
148 set_feature(env, ARM_FEATURE_XSCALE);
149 /* JTAG_ID is ((id << 28) | 0x09265013) */
balrog18c9b562007-04-30 02:02:17 +0000150 set_feature(env, ARM_FEATURE_IWMMXT);
151 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
balrogc1713132007-04-30 01:26:42 +0000152 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000153 env->cp15.c1_sys = 0x00000078;
pbrookf3d6b952007-03-11 13:03:18 +0000154 break;
155 default:
156 cpu_abort(env, "Bad CPU ID: %x\n", id);
157 break;
158 }
159}
160
pbrook40f137e2006-02-20 00:33:36 +0000161void cpu_reset(CPUARMState *env)
162{
pbrookf3d6b952007-03-11 13:03:18 +0000163 uint32_t id;
aliguorieca1bdf2009-01-26 19:54:31 +0000164
165 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
166 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
167 log_cpu_state(env, 0);
168 }
169
pbrookf3d6b952007-03-11 13:03:18 +0000170 id = env->cp15.c0_cpuid;
171 memset(env, 0, offsetof(CPUARMState, breakpoints));
172 if (id)
173 cpu_reset_model_id(env, id);
pbrook40f137e2006-02-20 00:33:36 +0000174#if defined (CONFIG_USER_ONLY)
175 env->uncached_cpsr = ARM_CPU_MODE_USR;
176 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
177#else
178 /* SVC mode with interrupts disabled. */
179 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
pbrook9ee6e8b2007-11-11 00:04:49 +0000180 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
181 clear at reset. */
182 if (IS_M(env))
183 env->uncached_cpsr &= ~CPSR_I;
pbrook40f137e2006-02-20 00:33:36 +0000184 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
pbrookb2fa1792008-10-22 19:22:30 +0000185 env->cp15.c2_base_mask = 0xffffc000u;
pbrook40f137e2006-02-20 00:33:36 +0000186#endif
187 env->regs[15] = 0;
pbrookf3d6b952007-03-11 13:03:18 +0000188 tlb_flush(env, 1);
pbrook40f137e2006-02-20 00:33:36 +0000189}
190
pbrook56aebc82008-10-11 17:55:29 +0000191static int vfp_gdb_get_reg(CPUState *env, uint8_t *buf, int reg)
192{
193 int nregs;
194
195 /* VFP data registers are always little-endian. */
196 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
197 if (reg < nregs) {
198 stfq_le_p(buf, env->vfp.regs[reg]);
199 return 8;
200 }
201 if (arm_feature(env, ARM_FEATURE_NEON)) {
202 /* Aliases for Q regs. */
203 nregs += 16;
204 if (reg < nregs) {
205 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
206 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
207 return 16;
208 }
209 }
210 switch (reg - nregs) {
211 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
212 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
213 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
214 }
215 return 0;
216}
217
218static int vfp_gdb_set_reg(CPUState *env, uint8_t *buf, int reg)
219{
220 int nregs;
221
222 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
223 if (reg < nregs) {
224 env->vfp.regs[reg] = ldfq_le_p(buf);
225 return 8;
226 }
227 if (arm_feature(env, ARM_FEATURE_NEON)) {
228 nregs += 16;
229 if (reg < nregs) {
230 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
231 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
232 return 16;
233 }
234 }
235 switch (reg - nregs) {
236 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
237 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +0200238 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +0000239 }
240 return 0;
241}
242
bellardaaed9092007-11-10 15:15:54 +0000243CPUARMState *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +0000244{
245 CPUARMState *env;
bellardaaed9092007-11-10 15:15:54 +0000246 uint32_t id;
pbrookb26eefb2008-03-31 03:44:26 +0000247 static int inited = 0;
pbrook40f137e2006-02-20 00:33:36 +0000248
bellardaaed9092007-11-10 15:15:54 +0000249 id = cpu_arm_find_by_name(cpu_model);
250 if (id == 0)
251 return NULL;
pbrook40f137e2006-02-20 00:33:36 +0000252 env = qemu_mallocz(sizeof(CPUARMState));
pbrook40f137e2006-02-20 00:33:36 +0000253 cpu_exec_init(env);
pbrookb26eefb2008-03-31 03:44:26 +0000254 if (!inited) {
255 inited = 1;
256 arm_translate_init();
257 }
258
ths01ba9812007-12-09 02:22:57 +0000259 env->cpu_model_str = cpu_model;
bellardaaed9092007-11-10 15:15:54 +0000260 env->cp15.c0_cpuid = id;
pbrook40f137e2006-02-20 00:33:36 +0000261 cpu_reset(env);
pbrook56aebc82008-10-11 17:55:29 +0000262 if (arm_feature(env, ARM_FEATURE_NEON)) {
263 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
264 51, "arm-neon.xml", 0);
265 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
266 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
267 35, "arm-vfp3.xml", 0);
268 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
269 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
270 19, "arm-vfp.xml", 0);
271 }
aliguori0bf46a42009-04-24 18:03:41 +0000272 qemu_init_vcpu(env);
pbrook40f137e2006-02-20 00:33:36 +0000273 return env;
274}
275
pbrook3371d272007-03-08 03:04:12 +0000276struct arm_cpu_t {
277 uint32_t id;
278 const char *name;
279};
280
281static const struct arm_cpu_t arm_cpu_names[] = {
282 { ARM_CPUID_ARM926, "arm926"},
pbrookce819862007-05-08 02:30:40 +0000283 { ARM_CPUID_ARM946, "arm946"},
pbrook3371d272007-03-08 03:04:12 +0000284 { ARM_CPUID_ARM1026, "arm1026"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000285 { ARM_CPUID_ARM1136, "arm1136"},
balrog827df9f2008-04-14 21:05:22 +0000286 { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000287 { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
288 { ARM_CPUID_CORTEXM3, "cortex-m3"},
289 { ARM_CPUID_CORTEXA8, "cortex-a8"},
balrogc3d26892007-07-29 17:57:26 +0000290 { ARM_CPUID_TI925T, "ti925t" },
balrogc1713132007-04-30 01:26:42 +0000291 { ARM_CPUID_PXA250, "pxa250" },
292 { ARM_CPUID_PXA255, "pxa255" },
293 { ARM_CPUID_PXA260, "pxa260" },
294 { ARM_CPUID_PXA261, "pxa261" },
295 { ARM_CPUID_PXA262, "pxa262" },
296 { ARM_CPUID_PXA270, "pxa270" },
297 { ARM_CPUID_PXA270_A0, "pxa270-a0" },
298 { ARM_CPUID_PXA270_A1, "pxa270-a1" },
299 { ARM_CPUID_PXA270_B0, "pxa270-b0" },
300 { ARM_CPUID_PXA270_B1, "pxa270-b1" },
301 { ARM_CPUID_PXA270_C0, "pxa270-c0" },
302 { ARM_CPUID_PXA270_C5, "pxa270-c5" },
pbrook9ee6e8b2007-11-11 00:04:49 +0000303 { ARM_CPUID_ANY, "any"},
pbrook3371d272007-03-08 03:04:12 +0000304 { 0, NULL}
305};
306
j_mayerc732abe2007-10-12 06:47:46 +0000307void arm_cpu_list(FILE *f, int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
pbrook5adb4832007-03-08 03:15:18 +0000308{
309 int i;
310
j_mayerc732abe2007-10-12 06:47:46 +0000311 (*cpu_fprintf)(f, "Available CPUs:\n");
pbrook5adb4832007-03-08 03:15:18 +0000312 for (i = 0; arm_cpu_names[i].name; i++) {
j_mayerc732abe2007-10-12 06:47:46 +0000313 (*cpu_fprintf)(f, " %s\n", arm_cpu_names[i].name);
pbrook5adb4832007-03-08 03:15:18 +0000314 }
315}
316
bellardaaed9092007-11-10 15:15:54 +0000317/* return 0 if not found */
318static uint32_t cpu_arm_find_by_name(const char *name)
pbrook40f137e2006-02-20 00:33:36 +0000319{
pbrook3371d272007-03-08 03:04:12 +0000320 int i;
321 uint32_t id;
322
323 id = 0;
pbrook3371d272007-03-08 03:04:12 +0000324 for (i = 0; arm_cpu_names[i].name; i++) {
325 if (strcmp(name, arm_cpu_names[i].name) == 0) {
326 id = arm_cpu_names[i].id;
327 break;
328 }
329 }
bellardaaed9092007-11-10 15:15:54 +0000330 return id;
pbrook40f137e2006-02-20 00:33:36 +0000331}
332
333void cpu_arm_close(CPUARMState *env)
334{
335 free(env);
336}
337
balrog2f4a40e2007-11-13 01:50:15 +0000338uint32_t cpsr_read(CPUARMState *env)
339{
340 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +0000341 ZF = (env->ZF == 0);
342 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +0000343 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
344 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
345 | ((env->condexec_bits & 0xfc) << 8)
346 | (env->GE << 16);
347}
348
349void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
350{
balrog2f4a40e2007-11-13 01:50:15 +0000351 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +0000352 env->ZF = (~val) & CPSR_Z;
353 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +0000354 env->CF = (val >> 29) & 1;
355 env->VF = (val << 3) & 0x80000000;
356 }
357 if (mask & CPSR_Q)
358 env->QF = ((val & CPSR_Q) != 0);
359 if (mask & CPSR_T)
360 env->thumb = ((val & CPSR_T) != 0);
361 if (mask & CPSR_IT_0_1) {
362 env->condexec_bits &= ~3;
363 env->condexec_bits |= (val >> 25) & 3;
364 }
365 if (mask & CPSR_IT_2_7) {
366 env->condexec_bits &= 3;
367 env->condexec_bits |= (val >> 8) & 0xfc;
368 }
369 if (mask & CPSR_GE) {
370 env->GE = (val >> 16) & 0xf;
371 }
372
373 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
374 switch_mode(env, val & CPSR_M);
375 }
376 mask &= ~CACHED_CPSR_BITS;
377 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
378}
379
pbrookb26eefb2008-03-31 03:44:26 +0000380/* Sign/zero extend */
381uint32_t HELPER(sxtb16)(uint32_t x)
382{
383 uint32_t res;
384 res = (uint16_t)(int8_t)x;
385 res |= (uint32_t)(int8_t)(x >> 16) << 16;
386 return res;
387}
388
389uint32_t HELPER(uxtb16)(uint32_t x)
390{
391 uint32_t res;
392 res = (uint16_t)(uint8_t)x;
393 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
394 return res;
395}
396
pbrookf51bbbf2008-03-31 03:45:13 +0000397uint32_t HELPER(clz)(uint32_t x)
398{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +0200399 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +0000400}
401
pbrook36706692008-03-31 03:46:19 +0000402int32_t HELPER(sdiv)(int32_t num, int32_t den)
403{
404 if (den == 0)
405 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +0200406 if (num == INT_MIN && den == -1)
407 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +0000408 return num / den;
409}
410
411uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
412{
413 if (den == 0)
414 return 0;
415 return num / den;
416}
417
418uint32_t HELPER(rbit)(uint32_t x)
419{
420 x = ((x & 0xff000000) >> 24)
421 | ((x & 0x00ff0000) >> 8)
422 | ((x & 0x0000ff00) << 8)
423 | ((x & 0x000000ff) << 24);
424 x = ((x & 0xf0f0f0f0) >> 4)
425 | ((x & 0x0f0f0f0f) << 4);
426 x = ((x & 0x88888888) >> 3)
427 | ((x & 0x44444444) >> 1)
428 | ((x & 0x22222222) << 1)
429 | ((x & 0x11111111) << 3);
430 return x;
431}
432
pbrookad694712008-03-31 03:48:30 +0000433uint32_t HELPER(abs)(uint32_t x)
434{
435 return ((int32_t)x < 0) ? -x : x;
436}
437
ths5fafdf22007-09-16 21:08:06 +0000438#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +0000439
440void do_interrupt (CPUState *env)
441{
442 env->exception_index = -1;
443}
444
pbrook9ee6e8b2007-11-11 00:04:49 +0000445/* Structure used to record exclusive memory locations. */
446typedef struct mmon_state {
447 struct mmon_state *next;
448 CPUARMState *cpu_env;
449 uint32_t addr;
450} mmon_state;
451
452/* Chain of current locks. */
453static mmon_state* mmon_head = NULL;
454
bellardb5ff1b32005-11-26 10:38:39 +0000455int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
j_mayer6ebbf392007-10-14 07:07:08 +0000456 int mmu_idx, int is_softmmu)
bellardb5ff1b32005-11-26 10:38:39 +0000457{
458 if (rw == 2) {
459 env->exception_index = EXCP_PREFETCH_ABORT;
460 env->cp15.c6_insn = address;
461 } else {
462 env->exception_index = EXCP_DATA_ABORT;
463 env->cp15.c6_data = address;
464 }
465 return 1;
466}
467
pbrook9ee6e8b2007-11-11 00:04:49 +0000468static void allocate_mmon_state(CPUState *env)
469{
470 env->mmon_entry = malloc(sizeof (mmon_state));
pbrook9ee6e8b2007-11-11 00:04:49 +0000471 memset (env->mmon_entry, 0, sizeof (mmon_state));
472 env->mmon_entry->cpu_env = env;
473 mmon_head = env->mmon_entry;
474}
475
476/* Flush any monitor locks for the specified address. */
477static void flush_mmon(uint32_t addr)
478{
479 mmon_state *mon;
480
481 for (mon = mmon_head; mon; mon = mon->next)
482 {
483 if (mon->addr != addr)
484 continue;
485
486 mon->addr = 0;
487 break;
488 }
489}
490
491/* Mark an address for exclusive access. */
pbrook8f8e3aa2008-03-31 03:48:01 +0000492void HELPER(mark_exclusive)(CPUState *env, uint32_t addr)
pbrook9ee6e8b2007-11-11 00:04:49 +0000493{
494 if (!env->mmon_entry)
495 allocate_mmon_state(env);
496 /* Clear any previous locks. */
497 flush_mmon(addr);
498 env->mmon_entry->addr = addr;
499}
500
501/* Test if an exclusive address is still exclusive. Returns zero
502 if the address is still exclusive. */
pbrook8f8e3aa2008-03-31 03:48:01 +0000503uint32_t HELPER(test_exclusive)(CPUState *env, uint32_t addr)
pbrook9ee6e8b2007-11-11 00:04:49 +0000504{
505 int res;
506
507 if (!env->mmon_entry)
508 return 1;
509 if (env->mmon_entry->addr == addr)
510 res = 0;
511 else
512 res = 1;
513 flush_mmon(addr);
514 return res;
515}
516
pbrook8f8e3aa2008-03-31 03:48:01 +0000517void HELPER(clrex)(CPUState *env)
pbrook9ee6e8b2007-11-11 00:04:49 +0000518{
519 if (!(env->mmon_entry && env->mmon_entry->addr))
520 return;
521 flush_mmon(env->mmon_entry->addr);
522}
523
Anthony Liguoric227f092009-10-01 16:12:16 -0500524target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
bellardb5ff1b32005-11-26 10:38:39 +0000525{
526 return addr;
527}
528
529/* These should probably raise undefined insn exceptions. */
pbrook8984bd22008-03-31 03:47:48 +0000530void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +0000531{
532 int op1 = (insn >> 8) & 0xf;
533 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
534 return;
535}
536
pbrook8984bd22008-03-31 03:47:48 +0000537uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +0000538{
539 int op1 = (insn >> 8) & 0xf;
540 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
541 return 0;
542}
543
pbrook8984bd22008-03-31 03:47:48 +0000544void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +0000545{
546 cpu_abort(env, "cp15 insn %08x\n", insn);
547}
548
pbrook8984bd22008-03-31 03:47:48 +0000549uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +0000550{
551 cpu_abort(env, "cp15 insn %08x\n", insn);
552 return 0;
553}
554
pbrook9ee6e8b2007-11-11 00:04:49 +0000555/* These should probably raise undefined insn exceptions. */
pbrook8984bd22008-03-31 03:47:48 +0000556void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000557{
558 cpu_abort(env, "v7m_mrs %d\n", reg);
559}
560
pbrook8984bd22008-03-31 03:47:48 +0000561uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +0000562{
563 cpu_abort(env, "v7m_mrs %d\n", reg);
564 return 0;
565}
566
bellardb5ff1b32005-11-26 10:38:39 +0000567void switch_mode(CPUState *env, int mode)
568{
569 if (mode != ARM_CPU_MODE_USR)
570 cpu_abort(env, "Tried to switch out of user mode\n");
571}
572
pbrookb0109802008-03-31 03:47:03 +0000573void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000574{
575 cpu_abort(env, "banked r13 write\n");
576}
577
pbrookb0109802008-03-31 03:47:03 +0000578uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +0000579{
580 cpu_abort(env, "banked r13 read\n");
581 return 0;
582}
583
bellardb5ff1b32005-11-26 10:38:39 +0000584#else
585
pbrook8e716212007-01-20 17:12:09 +0000586extern int semihosting_enabled;
587
bellardb5ff1b32005-11-26 10:38:39 +0000588/* Map CPU modes onto saved register banks. */
589static inline int bank_number (int mode)
590{
591 switch (mode) {
592 case ARM_CPU_MODE_USR:
593 case ARM_CPU_MODE_SYS:
594 return 0;
595 case ARM_CPU_MODE_SVC:
596 return 1;
597 case ARM_CPU_MODE_ABT:
598 return 2;
599 case ARM_CPU_MODE_UND:
600 return 3;
601 case ARM_CPU_MODE_IRQ:
602 return 4;
603 case ARM_CPU_MODE_FIQ:
604 return 5;
605 }
606 cpu_abort(cpu_single_env, "Bad mode %x\n", mode);
607 return -1;
608}
609
610void switch_mode(CPUState *env, int mode)
611{
612 int old_mode;
613 int i;
614
615 old_mode = env->uncached_cpsr & CPSR_M;
616 if (mode == old_mode)
617 return;
618
619 if (old_mode == ARM_CPU_MODE_FIQ) {
620 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000621 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000622 } else if (mode == ARM_CPU_MODE_FIQ) {
623 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000624 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000625 }
626
627 i = bank_number(old_mode);
628 env->banked_r13[i] = env->regs[13];
629 env->banked_r14[i] = env->regs[14];
630 env->banked_spsr[i] = env->spsr;
631
632 i = bank_number(mode);
633 env->regs[13] = env->banked_r13[i];
634 env->regs[14] = env->banked_r14[i];
635 env->spsr = env->banked_spsr[i];
636}
637
pbrook9ee6e8b2007-11-11 00:04:49 +0000638static void v7m_push(CPUARMState *env, uint32_t val)
639{
640 env->regs[13] -= 4;
641 stl_phys(env->regs[13], val);
642}
643
644static uint32_t v7m_pop(CPUARMState *env)
645{
646 uint32_t val;
647 val = ldl_phys(env->regs[13]);
648 env->regs[13] += 4;
649 return val;
650}
651
652/* Switch to V7M main or process stack pointer. */
653static void switch_v7m_sp(CPUARMState *env, int process)
654{
655 uint32_t tmp;
656 if (env->v7m.current_sp != process) {
657 tmp = env->v7m.other_sp;
658 env->v7m.other_sp = env->regs[13];
659 env->regs[13] = tmp;
660 env->v7m.current_sp = process;
661 }
662}
663
664static void do_v7m_exception_exit(CPUARMState *env)
665{
666 uint32_t type;
667 uint32_t xpsr;
668
669 type = env->regs[15];
670 if (env->v7m.exception != 0)
671 armv7m_nvic_complete_irq(env->v7m.nvic, env->v7m.exception);
672
673 /* Switch to the target stack. */
674 switch_v7m_sp(env, (type & 4) != 0);
675 /* Pop registers. */
676 env->regs[0] = v7m_pop(env);
677 env->regs[1] = v7m_pop(env);
678 env->regs[2] = v7m_pop(env);
679 env->regs[3] = v7m_pop(env);
680 env->regs[12] = v7m_pop(env);
681 env->regs[14] = v7m_pop(env);
682 env->regs[15] = v7m_pop(env);
683 xpsr = v7m_pop(env);
684 xpsr_write(env, xpsr, 0xfffffdff);
685 /* Undo stack alignment. */
686 if (xpsr & 0x200)
687 env->regs[13] |= 4;
688 /* ??? The exception return type specifies Thread/Handler mode. However
689 this is also implied by the xPSR value. Not sure what to do
690 if there is a mismatch. */
691 /* ??? Likewise for mismatches between the CONTROL register and the stack
692 pointer. */
693}
694
aurel322b3ea312009-03-07 21:48:00 +0000695static void do_interrupt_v7m(CPUARMState *env)
pbrook9ee6e8b2007-11-11 00:04:49 +0000696{
697 uint32_t xpsr = xpsr_read(env);
698 uint32_t lr;
699 uint32_t addr;
700
701 lr = 0xfffffff1;
702 if (env->v7m.current_sp)
703 lr |= 4;
704 if (env->v7m.exception == 0)
705 lr |= 8;
706
707 /* For exceptions we just mark as pending on the NVIC, and let that
708 handle it. */
709 /* TODO: Need to escalate if the current priority is higher than the
710 one we're raising. */
711 switch (env->exception_index) {
712 case EXCP_UDEF:
713 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_USAGE);
714 return;
715 case EXCP_SWI:
716 env->regs[15] += 2;
717 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_SVC);
718 return;
719 case EXCP_PREFETCH_ABORT:
720 case EXCP_DATA_ABORT:
721 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_MEM);
722 return;
723 case EXCP_BKPT:
pbrook2ad207d2007-11-24 23:22:11 +0000724 if (semihosting_enabled) {
725 int nr;
726 nr = lduw_code(env->regs[15]) & 0xff;
727 if (nr == 0xab) {
728 env->regs[15] += 2;
729 env->regs[0] = do_arm_semihosting(env);
730 return;
731 }
732 }
pbrook9ee6e8b2007-11-11 00:04:49 +0000733 armv7m_nvic_set_pending(env->v7m.nvic, ARMV7M_EXCP_DEBUG);
734 return;
735 case EXCP_IRQ:
736 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->v7m.nvic);
737 break;
738 case EXCP_EXCEPTION_EXIT:
739 do_v7m_exception_exit(env);
740 return;
741 default:
742 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
743 return; /* Never happens. Keep compiler happy. */
744 }
745
746 /* Align stack pointer. */
747 /* ??? Should only do this if Configuration Control Register
748 STACKALIGN bit is set. */
749 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +0000750 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +0000751 xpsr |= 0x200;
752 }
balrog6c956762008-04-13 00:57:49 +0000753 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000754 v7m_push(env, xpsr);
755 v7m_push(env, env->regs[15]);
756 v7m_push(env, env->regs[14]);
757 v7m_push(env, env->regs[12]);
758 v7m_push(env, env->regs[3]);
759 v7m_push(env, env->regs[2]);
760 v7m_push(env, env->regs[1]);
761 v7m_push(env, env->regs[0]);
762 switch_v7m_sp(env, 0);
763 env->uncached_cpsr &= ~CPSR_IT;
764 env->regs[14] = lr;
765 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
766 env->regs[15] = addr & 0xfffffffe;
767 env->thumb = addr & 1;
768}
769
bellardb5ff1b32005-11-26 10:38:39 +0000770/* Handle a CPU exception. */
771void do_interrupt(CPUARMState *env)
772{
773 uint32_t addr;
774 uint32_t mask;
775 int new_mode;
776 uint32_t offset;
777
pbrook9ee6e8b2007-11-11 00:04:49 +0000778 if (IS_M(env)) {
779 do_interrupt_v7m(env);
780 return;
781 }
bellardb5ff1b32005-11-26 10:38:39 +0000782 /* TODO: Vectored interrupt controller. */
783 switch (env->exception_index) {
784 case EXCP_UDEF:
785 new_mode = ARM_CPU_MODE_UND;
786 addr = 0x04;
787 mask = CPSR_I;
788 if (env->thumb)
789 offset = 2;
790 else
791 offset = 4;
792 break;
793 case EXCP_SWI:
pbrook8e716212007-01-20 17:12:09 +0000794 if (semihosting_enabled) {
795 /* Check for semihosting interrupt. */
796 if (env->thumb) {
797 mask = lduw_code(env->regs[15] - 2) & 0xff;
798 } else {
799 mask = ldl_code(env->regs[15] - 4) & 0xffffff;
800 }
801 /* Only intercept calls from privileged modes, to provide some
802 semblance of security. */
803 if (((mask == 0x123456 && !env->thumb)
804 || (mask == 0xab && env->thumb))
805 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
806 env->regs[0] = do_arm_semihosting(env);
807 return;
808 }
809 }
bellardb5ff1b32005-11-26 10:38:39 +0000810 new_mode = ARM_CPU_MODE_SVC;
811 addr = 0x08;
812 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +0000813 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +0000814 offset = 0;
815 break;
pbrook06c949e2006-02-04 19:35:26 +0000816 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +0000817 /* See if this is a semihosting syscall. */
pbrook2ad207d2007-11-24 23:22:11 +0000818 if (env->thumb && semihosting_enabled) {
pbrook9ee6e8b2007-11-11 00:04:49 +0000819 mask = lduw_code(env->regs[15]) & 0xff;
820 if (mask == 0xab
821 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
822 env->regs[15] += 2;
823 env->regs[0] = do_arm_semihosting(env);
824 return;
825 }
826 }
827 /* Fall through to prefetch abort. */
828 case EXCP_PREFETCH_ABORT:
bellardb5ff1b32005-11-26 10:38:39 +0000829 new_mode = ARM_CPU_MODE_ABT;
830 addr = 0x0c;
831 mask = CPSR_A | CPSR_I;
832 offset = 4;
833 break;
834 case EXCP_DATA_ABORT:
835 new_mode = ARM_CPU_MODE_ABT;
836 addr = 0x10;
837 mask = CPSR_A | CPSR_I;
838 offset = 8;
839 break;
840 case EXCP_IRQ:
841 new_mode = ARM_CPU_MODE_IRQ;
842 addr = 0x18;
843 /* Disable IRQ and imprecise data aborts. */
844 mask = CPSR_A | CPSR_I;
845 offset = 4;
846 break;
847 case EXCP_FIQ:
848 new_mode = ARM_CPU_MODE_FIQ;
849 addr = 0x1c;
850 /* Disable FIQ, IRQ and imprecise data aborts. */
851 mask = CPSR_A | CPSR_I | CPSR_F;
852 offset = 4;
853 break;
854 default:
855 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
856 return; /* Never happens. Keep compiler happy. */
857 }
858 /* High vectors. */
859 if (env->cp15.c1_sys & (1 << 13)) {
860 addr += 0xffff0000;
861 }
862 switch_mode (env, new_mode);
863 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +0000864 /* Clear IT bits. */
865 env->condexec_bits = 0;
bellard6d7e6322005-12-18 16:54:08 +0000866 /* Switch to the new mode, and switch to Arm mode. */
bellardb5ff1b32005-11-26 10:38:39 +0000867 /* ??? Thumb interrupt handlers not implemented. */
bellard6d7e6322005-12-18 16:54:08 +0000868 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
bellardb5ff1b32005-11-26 10:38:39 +0000869 env->uncached_cpsr |= mask;
bellard6d7e6322005-12-18 16:54:08 +0000870 env->thumb = 0;
bellardb5ff1b32005-11-26 10:38:39 +0000871 env->regs[14] = env->regs[15] + offset;
872 env->regs[15] = addr;
873 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
874}
875
876/* Check section/page access permissions.
877 Returns the page protection flags, or zero if the access is not
878 permitted. */
879static inline int check_ap(CPUState *env, int ap, int domain, int access_type,
880 int is_user)
881{
pbrook9ee6e8b2007-11-11 00:04:49 +0000882 int prot_ro;
883
bellardb5ff1b32005-11-26 10:38:39 +0000884 if (domain == 3)
885 return PAGE_READ | PAGE_WRITE;
886
pbrook9ee6e8b2007-11-11 00:04:49 +0000887 if (access_type == 1)
888 prot_ro = 0;
889 else
890 prot_ro = PAGE_READ;
891
bellardb5ff1b32005-11-26 10:38:39 +0000892 switch (ap) {
893 case 0:
pbrook78600322006-09-09 14:36:26 +0000894 if (access_type == 1)
bellardb5ff1b32005-11-26 10:38:39 +0000895 return 0;
896 switch ((env->cp15.c1_sys >> 8) & 3) {
897 case 1:
898 return is_user ? 0 : PAGE_READ;
899 case 2:
900 return PAGE_READ;
901 default:
902 return 0;
903 }
904 case 1:
905 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
906 case 2:
907 if (is_user)
pbrook9ee6e8b2007-11-11 00:04:49 +0000908 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +0000909 else
910 return PAGE_READ | PAGE_WRITE;
911 case 3:
912 return PAGE_READ | PAGE_WRITE;
pbrookd4934d12008-12-19 12:39:00 +0000913 case 4: /* Reserved. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000914 return 0;
915 case 5:
916 return is_user ? 0 : prot_ro;
917 case 6:
918 return prot_ro;
pbrookd4934d12008-12-19 12:39:00 +0000919 case 7:
920 if (!arm_feature (env, ARM_FEATURE_V7))
921 return 0;
922 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +0000923 default:
924 abort();
925 }
926}
927
pbrookb2fa1792008-10-22 19:22:30 +0000928static uint32_t get_level1_table_address(CPUState *env, uint32_t address)
929{
930 uint32_t table;
931
932 if (address & env->cp15.c2_mask)
933 table = env->cp15.c2_base1 & 0xffffc000;
934 else
935 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
936
937 table |= (address >> 18) & 0x3ffc;
938 return table;
939}
940
pbrook9ee6e8b2007-11-11 00:04:49 +0000941static int get_phys_addr_v5(CPUState *env, uint32_t address, int access_type,
942 int is_user, uint32_t *phys_ptr, int *prot)
bellardb5ff1b32005-11-26 10:38:39 +0000943{
944 int code;
945 uint32_t table;
946 uint32_t desc;
947 int type;
948 int ap;
949 int domain;
950 uint32_t phys_addr;
951
pbrook9ee6e8b2007-11-11 00:04:49 +0000952 /* Pagetable walk. */
953 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +0000954 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +0000955 desc = ldl_phys(table);
956 type = (desc & 3);
957 domain = (env->cp15.c3 >> ((desc >> 4) & 0x1e)) & 3;
958 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +0000959 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000960 code = 5;
961 goto do_fault;
962 }
963 if (domain == 0 || domain == 2) {
964 if (type == 2)
965 code = 9; /* Section domain fault. */
966 else
967 code = 11; /* Page domain fault. */
968 goto do_fault;
969 }
970 if (type == 2) {
971 /* 1Mb section. */
972 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
973 ap = (desc >> 10) & 3;
974 code = 13;
975 } else {
976 /* Lookup l2 entry. */
977 if (type == 1) {
978 /* Coarse pagetable. */
979 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
980 } else {
981 /* Fine pagetable. */
982 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
983 }
984 desc = ldl_phys(table);
985 switch (desc & 3) {
986 case 0: /* Page translation fault. */
987 code = 7;
988 goto do_fault;
989 case 1: /* 64k page. */
990 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
991 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
992 break;
993 case 2: /* 4k page. */
994 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
995 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
996 break;
997 case 3: /* 1k page. */
998 if (type == 1) {
999 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1000 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1001 } else {
1002 /* Page translation fault. */
1003 code = 7;
1004 goto do_fault;
1005 }
1006 } else {
1007 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1008 }
1009 ap = (desc >> 4) & 3;
1010 break;
1011 default:
1012 /* Never happens, but compiler isn't smart enough to tell. */
1013 abort();
1014 }
1015 code = 15;
1016 }
1017 *prot = check_ap(env, ap, domain, access_type, is_user);
1018 if (!*prot) {
1019 /* Access permission fault. */
1020 goto do_fault;
1021 }
1022 *phys_ptr = phys_addr;
1023 return 0;
1024do_fault:
1025 return code | (domain << 4);
1026}
1027
1028static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,
1029 int is_user, uint32_t *phys_ptr, int *prot)
1030{
1031 int code;
1032 uint32_t table;
1033 uint32_t desc;
1034 uint32_t xn;
1035 int type;
1036 int ap;
1037 int domain;
1038 uint32_t phys_addr;
1039
1040 /* Pagetable walk. */
1041 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001042 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001043 desc = ldl_phys(table);
1044 type = (desc & 3);
1045 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001046 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001047 code = 5;
1048 domain = 0;
1049 goto do_fault;
1050 } else if (type == 2 && (desc & (1 << 18))) {
1051 /* Supersection. */
1052 domain = 0;
1053 } else {
1054 /* Section or page. */
1055 domain = (desc >> 4) & 0x1e;
1056 }
1057 domain = (env->cp15.c3 >> domain) & 3;
1058 if (domain == 0 || domain == 2) {
1059 if (type == 2)
1060 code = 9; /* Section domain fault. */
1061 else
1062 code = 11; /* Page domain fault. */
1063 goto do_fault;
1064 }
1065 if (type == 2) {
1066 if (desc & (1 << 18)) {
1067 /* Supersection. */
1068 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
1069 } else {
1070 /* Section. */
1071 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1072 }
1073 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1074 xn = desc & (1 << 4);
1075 code = 13;
1076 } else {
1077 /* Lookup l2 entry. */
1078 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1079 desc = ldl_phys(table);
1080 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1081 switch (desc & 3) {
1082 case 0: /* Page translation fault. */
1083 code = 7;
1084 goto do_fault;
1085 case 1: /* 64k page. */
1086 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1087 xn = desc & (1 << 15);
1088 break;
1089 case 2: case 3: /* 4k page. */
1090 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1091 xn = desc & 1;
1092 break;
1093 default:
1094 /* Never happens, but compiler isn't smart enough to tell. */
1095 abort();
1096 }
1097 code = 15;
1098 }
1099 if (xn && access_type == 2)
1100 goto do_fault;
1101
pbrookd4934d12008-12-19 12:39:00 +00001102 /* The simplified model uses AP[0] as an access control bit. */
1103 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1104 /* Access flag fault. */
1105 code = (code == 15) ? 6 : 3;
1106 goto do_fault;
1107 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001108 *prot = check_ap(env, ap, domain, access_type, is_user);
1109 if (!*prot) {
1110 /* Access permission fault. */
1111 goto do_fault;
1112 }
1113 *phys_ptr = phys_addr;
1114 return 0;
1115do_fault:
1116 return code | (domain << 4);
1117}
1118
1119static int get_phys_addr_mpu(CPUState *env, uint32_t address, int access_type,
1120 int is_user, uint32_t *phys_ptr, int *prot)
1121{
1122 int n;
1123 uint32_t mask;
1124 uint32_t base;
1125
1126 *phys_ptr = address;
1127 for (n = 7; n >= 0; n--) {
1128 base = env->cp15.c6_region[n];
1129 if ((base & 1) == 0)
1130 continue;
1131 mask = 1 << ((base >> 1) & 0x1f);
1132 /* Keep this shift separate from the above to avoid an
1133 (undefined) << 32. */
1134 mask = (mask << 1) - 1;
1135 if (((base ^ address) & ~mask) == 0)
1136 break;
1137 }
1138 if (n < 0)
1139 return 2;
1140
1141 if (access_type == 2) {
1142 mask = env->cp15.c5_insn;
1143 } else {
1144 mask = env->cp15.c5_data;
1145 }
1146 mask = (mask >> (n * 4)) & 0xf;
1147 switch (mask) {
1148 case 0:
1149 return 1;
1150 case 1:
1151 if (is_user)
1152 return 1;
1153 *prot = PAGE_READ | PAGE_WRITE;
1154 break;
1155 case 2:
1156 *prot = PAGE_READ;
1157 if (!is_user)
1158 *prot |= PAGE_WRITE;
1159 break;
1160 case 3:
1161 *prot = PAGE_READ | PAGE_WRITE;
1162 break;
1163 case 5:
1164 if (is_user)
1165 return 1;
1166 *prot = PAGE_READ;
1167 break;
1168 case 6:
1169 *prot = PAGE_READ;
1170 break;
1171 default:
1172 /* Bad permission. */
1173 return 1;
1174 }
1175 return 0;
1176}
1177
1178static inline int get_phys_addr(CPUState *env, uint32_t address,
1179 int access_type, int is_user,
1180 uint32_t *phys_ptr, int *prot)
1181{
bellardb5ff1b32005-11-26 10:38:39 +00001182 /* Fast Context Switch Extension. */
1183 if (address < 0x02000000)
1184 address += env->cp15.c13_fcse;
1185
1186 if ((env->cp15.c1_sys & 1) == 0) {
pbrookce819862007-05-08 02:30:40 +00001187 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00001188 *phys_ptr = address;
1189 *prot = PAGE_READ | PAGE_WRITE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001190 return 0;
pbrookce819862007-05-08 02:30:40 +00001191 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001192 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1193 prot);
1194 } else if (env->cp15.c1_sys & (1 << 23)) {
1195 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
1196 prot);
bellardb5ff1b32005-11-26 10:38:39 +00001197 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001198 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
1199 prot);
bellardb5ff1b32005-11-26 10:38:39 +00001200 }
bellardb5ff1b32005-11-26 10:38:39 +00001201}
1202
1203int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
j_mayer6ebbf392007-10-14 07:07:08 +00001204 int access_type, int mmu_idx, int is_softmmu)
bellardb5ff1b32005-11-26 10:38:39 +00001205{
1206 uint32_t phys_addr;
1207 int prot;
j_mayer6ebbf392007-10-14 07:07:08 +00001208 int ret, is_user;
bellardb5ff1b32005-11-26 10:38:39 +00001209
j_mayer6ebbf392007-10-14 07:07:08 +00001210 is_user = mmu_idx == MMU_USER_IDX;
bellardb5ff1b32005-11-26 10:38:39 +00001211 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot);
1212 if (ret == 0) {
1213 /* Map a single [sub]page. */
1214 phys_addr &= ~(uint32_t)0x3ff;
1215 address &= ~(uint32_t)0x3ff;
j_mayer6ebbf392007-10-14 07:07:08 +00001216 return tlb_set_page (env, address, phys_addr, prot, mmu_idx,
bellardb5ff1b32005-11-26 10:38:39 +00001217 is_softmmu);
1218 }
1219
1220 if (access_type == 2) {
1221 env->cp15.c5_insn = ret;
1222 env->cp15.c6_insn = address;
1223 env->exception_index = EXCP_PREFETCH_ABORT;
1224 } else {
1225 env->cp15.c5_data = ret;
pbrook9ee6e8b2007-11-11 00:04:49 +00001226 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1227 env->cp15.c5_data |= (1 << 11);
bellardb5ff1b32005-11-26 10:38:39 +00001228 env->cp15.c6_data = address;
1229 env->exception_index = EXCP_DATA_ABORT;
1230 }
1231 return 1;
1232}
1233
Anthony Liguoric227f092009-10-01 16:12:16 -05001234target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
bellardb5ff1b32005-11-26 10:38:39 +00001235{
1236 uint32_t phys_addr;
1237 int prot;
1238 int ret;
1239
1240 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot);
1241
1242 if (ret != 0)
1243 return -1;
1244
1245 return phys_addr;
1246}
1247
pbrook9ee6e8b2007-11-11 00:04:49 +00001248/* Not really implemented. Need to figure out a sane way of doing this.
1249 Maybe add generic watchpoint support and use that. */
1250
pbrook8f8e3aa2008-03-31 03:48:01 +00001251void HELPER(mark_exclusive)(CPUState *env, uint32_t addr)
pbrook9ee6e8b2007-11-11 00:04:49 +00001252{
1253 env->mmon_addr = addr;
1254}
1255
pbrook8f8e3aa2008-03-31 03:48:01 +00001256uint32_t HELPER(test_exclusive)(CPUState *env, uint32_t addr)
pbrook9ee6e8b2007-11-11 00:04:49 +00001257{
1258 return (env->mmon_addr != addr);
1259}
1260
pbrook8f8e3aa2008-03-31 03:48:01 +00001261void HELPER(clrex)(CPUState *env)
pbrook9ee6e8b2007-11-11 00:04:49 +00001262{
1263 env->mmon_addr = -1;
1264}
1265
pbrook8984bd22008-03-31 03:47:48 +00001266void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +00001267{
1268 int cp_num = (insn >> 8) & 0xf;
1269 int cp_info = (insn >> 5) & 7;
1270 int src = (insn >> 16) & 0xf;
1271 int operand = insn & 0xf;
1272
1273 if (env->cp[cp_num].cp_write)
1274 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1275 cp_info, src, operand, val);
1276}
1277
pbrook8984bd22008-03-31 03:47:48 +00001278uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +00001279{
1280 int cp_num = (insn >> 8) & 0xf;
1281 int cp_info = (insn >> 5) & 7;
1282 int dest = (insn >> 16) & 0xf;
1283 int operand = insn & 0xf;
1284
1285 if (env->cp[cp_num].cp_read)
1286 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1287 cp_info, dest, operand);
1288 return 0;
1289}
1290
pbrookce819862007-05-08 02:30:40 +00001291/* Return basic MPU access permission bits. */
1292static uint32_t simple_mpu_ap_bits(uint32_t val)
1293{
1294 uint32_t ret;
1295 uint32_t mask;
1296 int i;
1297 ret = 0;
1298 mask = 3;
1299 for (i = 0; i < 16; i += 2) {
1300 ret |= (val >> i) & mask;
1301 mask <<= 2;
1302 }
1303 return ret;
1304}
1305
1306/* Pad basic MPU access permission bits to extended format. */
1307static uint32_t extended_mpu_ap_bits(uint32_t val)
1308{
1309 uint32_t ret;
1310 uint32_t mask;
1311 int i;
1312 ret = 0;
1313 mask = 3;
1314 for (i = 0; i < 16; i += 2) {
1315 ret |= (val & mask) << i;
1316 mask <<= 2;
1317 }
1318 return ret;
1319}
1320
pbrook8984bd22008-03-31 03:47:48 +00001321void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +00001322{
pbrook9ee6e8b2007-11-11 00:04:49 +00001323 int op1;
1324 int op2;
1325 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001326
pbrook9ee6e8b2007-11-11 00:04:49 +00001327 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001328 op2 = (insn >> 5) & 7;
pbrookce819862007-05-08 02:30:40 +00001329 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001330 switch ((insn >> 16) & 0xf) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001331 case 0:
pbrook9ee6e8b2007-11-11 00:04:49 +00001332 /* ID codes. */
balrog610c3c82007-06-24 12:09:48 +00001333 if (arm_feature(env, ARM_FEATURE_XSCALE))
1334 break;
balrogc3d26892007-07-29 17:57:26 +00001335 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1336 break;
pbrooka49ea272008-12-19 13:37:53 +00001337 if (arm_feature(env, ARM_FEATURE_V7)
1338 && op1 == 2 && crm == 0 && op2 == 0) {
1339 env->cp15.c0_cssel = val & 0xf;
1340 break;
1341 }
bellardb5ff1b32005-11-26 10:38:39 +00001342 goto bad_reg;
1343 case 1: /* System configuration. */
balrogc3d26892007-07-29 17:57:26 +00001344 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1345 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001346 switch (op2) {
1347 case 0:
pbrookce819862007-05-08 02:30:40 +00001348 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
balrogc1713132007-04-30 01:26:42 +00001349 env->cp15.c1_sys = val;
bellardb5ff1b32005-11-26 10:38:39 +00001350 /* ??? Lots of these bits are not implemented. */
1351 /* This may enable/disable the MMU, so do a TLB flush. */
1352 tlb_flush(env, 1);
1353 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001354 case 1: /* Auxiliary cotrol register. */
balrog610c3c82007-06-24 12:09:48 +00001355 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1356 env->cp15.c1_xscaleauxcr = val;
balrogc1713132007-04-30 01:26:42 +00001357 break;
balrog610c3c82007-06-24 12:09:48 +00001358 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001359 /* Not implemented. */
1360 break;
bellardb5ff1b32005-11-26 10:38:39 +00001361 case 2:
balrog610c3c82007-06-24 12:09:48 +00001362 if (arm_feature(env, ARM_FEATURE_XSCALE))
1363 goto bad_reg;
pbrook4be27db2008-10-22 16:14:08 +00001364 if (env->cp15.c1_coproc != val) {
1365 env->cp15.c1_coproc = val;
1366 /* ??? Is this safe when called from within a TB? */
1367 tb_flush(env);
1368 }
balrogc1713132007-04-30 01:26:42 +00001369 break;
bellardb5ff1b32005-11-26 10:38:39 +00001370 default:
1371 goto bad_reg;
1372 }
1373 break;
pbrookce819862007-05-08 02:30:40 +00001374 case 2: /* MMU Page table control / MPU cache control. */
1375 if (arm_feature(env, ARM_FEATURE_MPU)) {
1376 switch (op2) {
1377 case 0:
1378 env->cp15.c2_data = val;
1379 break;
1380 case 1:
1381 env->cp15.c2_insn = val;
1382 break;
1383 default:
1384 goto bad_reg;
1385 }
1386 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001387 switch (op2) {
1388 case 0:
1389 env->cp15.c2_base0 = val;
1390 break;
1391 case 1:
1392 env->cp15.c2_base1 = val;
1393 break;
1394 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001395 val &= 7;
1396 env->cp15.c2_control = val;
pbrook9ee6e8b2007-11-11 00:04:49 +00001397 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
pbrookb2fa1792008-10-22 19:22:30 +00001398 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
pbrook9ee6e8b2007-11-11 00:04:49 +00001399 break;
1400 default:
1401 goto bad_reg;
1402 }
pbrookce819862007-05-08 02:30:40 +00001403 }
bellardb5ff1b32005-11-26 10:38:39 +00001404 break;
pbrookce819862007-05-08 02:30:40 +00001405 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001406 env->cp15.c3 = val;
balrog405ee3a2007-10-31 00:47:13 +00001407 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
bellardb5ff1b32005-11-26 10:38:39 +00001408 break;
1409 case 4: /* Reserved. */
1410 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001411 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001412 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1413 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001414 switch (op2) {
1415 case 0:
pbrookce819862007-05-08 02:30:40 +00001416 if (arm_feature(env, ARM_FEATURE_MPU))
1417 val = extended_mpu_ap_bits(val);
bellardb5ff1b32005-11-26 10:38:39 +00001418 env->cp15.c5_data = val;
1419 break;
1420 case 1:
pbrookce819862007-05-08 02:30:40 +00001421 if (arm_feature(env, ARM_FEATURE_MPU))
1422 val = extended_mpu_ap_bits(val);
1423 env->cp15.c5_insn = val;
1424 break;
1425 case 2:
1426 if (!arm_feature(env, ARM_FEATURE_MPU))
1427 goto bad_reg;
1428 env->cp15.c5_data = val;
1429 break;
1430 case 3:
1431 if (!arm_feature(env, ARM_FEATURE_MPU))
1432 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001433 env->cp15.c5_insn = val;
1434 break;
1435 default:
1436 goto bad_reg;
1437 }
1438 break;
pbrookce819862007-05-08 02:30:40 +00001439 case 6: /* MMU Fault address / MPU base/size. */
1440 if (arm_feature(env, ARM_FEATURE_MPU)) {
1441 if (crm >= 8)
1442 goto bad_reg;
1443 env->cp15.c6_region[crm] = val;
1444 } else {
balrogc3d26892007-07-29 17:57:26 +00001445 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1446 op2 = 0;
pbrookce819862007-05-08 02:30:40 +00001447 switch (op2) {
1448 case 0:
1449 env->cp15.c6_data = val;
1450 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001451 case 1: /* ??? This is WFAR on armv6 */
1452 case 2:
pbrookce819862007-05-08 02:30:40 +00001453 env->cp15.c6_insn = val;
1454 break;
1455 default:
1456 goto bad_reg;
1457 }
bellardb5ff1b32005-11-26 10:38:39 +00001458 }
1459 break;
1460 case 7: /* Cache control. */
balrogc3d26892007-07-29 17:57:26 +00001461 env->cp15.c15_i_max = 0x000;
1462 env->cp15.c15_i_min = 0xff0;
bellardb5ff1b32005-11-26 10:38:39 +00001463 /* No cache, so nothing to do. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001464 /* ??? MPCore has VA to PA translation functions. */
bellardb5ff1b32005-11-26 10:38:39 +00001465 break;
1466 case 8: /* MMU TLB control. */
1467 switch (op2) {
1468 case 0: /* Invalidate all. */
1469 tlb_flush(env, 0);
1470 break;
1471 case 1: /* Invalidate single TLB entry. */
1472#if 0
1473 /* ??? This is wrong for large pages and sections. */
1474 /* As an ugly hack to make linux work we always flush a 4K
1475 pages. */
1476 val &= 0xfffff000;
1477 tlb_flush_page(env, val);
1478 tlb_flush_page(env, val + 0x400);
1479 tlb_flush_page(env, val + 0x800);
1480 tlb_flush_page(env, val + 0xc00);
1481#else
1482 tlb_flush(env, 1);
1483#endif
1484 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001485 case 2: /* Invalidate on ASID. */
1486 tlb_flush(env, val == 0);
1487 break;
1488 case 3: /* Invalidate single entry on MVA. */
1489 /* ??? This is like case 1, but ignores ASID. */
1490 tlb_flush(env, 1);
1491 break;
bellardb5ff1b32005-11-26 10:38:39 +00001492 default:
1493 goto bad_reg;
1494 }
1495 break;
pbrookce819862007-05-08 02:30:40 +00001496 case 9:
balrogc3d26892007-07-29 17:57:26 +00001497 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1498 break;
pbrookce819862007-05-08 02:30:40 +00001499 switch (crm) {
1500 case 0: /* Cache lockdown. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001501 switch (op1) {
1502 case 0: /* L1 cache. */
1503 switch (op2) {
1504 case 0:
1505 env->cp15.c9_data = val;
1506 break;
1507 case 1:
1508 env->cp15.c9_insn = val;
1509 break;
1510 default:
1511 goto bad_reg;
1512 }
1513 break;
1514 case 1: /* L2 cache. */
1515 /* Ignore writes to L2 lockdown/auxiliary registers. */
1516 break;
1517 default:
1518 goto bad_reg;
1519 }
1520 break;
pbrookce819862007-05-08 02:30:40 +00001521 case 1: /* TCM memory region registers. */
1522 /* Not implemented. */
1523 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001524 default:
1525 goto bad_reg;
1526 }
1527 break;
1528 case 10: /* MMU TLB lockdown. */
1529 /* ??? TLB lockdown not implemented. */
1530 break;
bellardb5ff1b32005-11-26 10:38:39 +00001531 case 12: /* Reserved. */
1532 goto bad_reg;
1533 case 13: /* Process ID. */
1534 switch (op2) {
1535 case 0:
pbrookd07edbf2006-07-21 22:39:57 +00001536 /* Unlike real hardware the qemu TLB uses virtual addresses,
1537 not modified virtual addresses, so this causes a TLB flush.
1538 */
1539 if (env->cp15.c13_fcse != val)
1540 tlb_flush(env, 1);
1541 env->cp15.c13_fcse = val;
bellardb5ff1b32005-11-26 10:38:39 +00001542 break;
1543 case 1:
pbrookd07edbf2006-07-21 22:39:57 +00001544 /* This changes the ASID, so do a TLB flush. */
pbrookce819862007-05-08 02:30:40 +00001545 if (env->cp15.c13_context != val
1546 && !arm_feature(env, ARM_FEATURE_MPU))
pbrookd07edbf2006-07-21 22:39:57 +00001547 tlb_flush(env, 0);
1548 env->cp15.c13_context = val;
bellardb5ff1b32005-11-26 10:38:39 +00001549 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001550 case 2:
1551 env->cp15.c13_tls1 = val;
1552 break;
1553 case 3:
1554 env->cp15.c13_tls2 = val;
1555 break;
1556 case 4:
1557 env->cp15.c13_tls3 = val;
1558 break;
bellardb5ff1b32005-11-26 10:38:39 +00001559 default:
1560 goto bad_reg;
1561 }
1562 break;
1563 case 14: /* Reserved. */
1564 goto bad_reg;
1565 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00001566 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
pbrookce819862007-05-08 02:30:40 +00001567 if (op2 == 0 && crm == 1) {
balrog2e232132007-08-01 02:31:54 +00001568 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1569 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1570 tb_flush(env);
1571 env->cp15.c15_cpar = val & 0x3fff;
1572 }
balrogc1713132007-04-30 01:26:42 +00001573 break;
1574 }
1575 goto bad_reg;
1576 }
balrogc3d26892007-07-29 17:57:26 +00001577 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1578 switch (crm) {
1579 case 0:
1580 break;
1581 case 1: /* Set TI925T configuration. */
1582 env->cp15.c15_ticonfig = val & 0xe7;
1583 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1584 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1585 break;
1586 case 2: /* Set I_max. */
1587 env->cp15.c15_i_max = val;
1588 break;
1589 case 3: /* Set I_min. */
1590 env->cp15.c15_i_min = val;
1591 break;
1592 case 4: /* Set thread-ID. */
1593 env->cp15.c15_threadid = val & 0xffff;
1594 break;
1595 case 8: /* Wait-for-interrupt (deprecated). */
1596 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1597 break;
1598 default:
1599 goto bad_reg;
1600 }
1601 }
bellardb5ff1b32005-11-26 10:38:39 +00001602 break;
1603 }
1604 return;
1605bad_reg:
1606 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001607 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1608 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00001609}
1610
pbrook8984bd22008-03-31 03:47:48 +00001611uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +00001612{
pbrook9ee6e8b2007-11-11 00:04:49 +00001613 int op1;
1614 int op2;
1615 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001616
pbrook9ee6e8b2007-11-11 00:04:49 +00001617 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001618 op2 = (insn >> 5) & 7;
balrogc3d26892007-07-29 17:57:26 +00001619 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001620 switch ((insn >> 16) & 0xf) {
1621 case 0: /* ID codes. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001622 switch (op1) {
1623 case 0:
1624 switch (crm) {
1625 case 0:
1626 switch (op2) {
1627 case 0: /* Device ID. */
1628 return env->cp15.c0_cpuid;
1629 case 1: /* Cache Type. */
1630 return env->cp15.c0_cachetype;
1631 case 2: /* TCM status. */
1632 return 0;
1633 case 3: /* TLB type register. */
1634 return 0; /* No lockable TLB entries. */
1635 case 5: /* CPU ID */
1636 return env->cpu_index;
1637 default:
1638 goto bad_reg;
1639 }
1640 case 1:
1641 if (!arm_feature(env, ARM_FEATURE_V6))
1642 goto bad_reg;
1643 return env->cp15.c0_c1[op2];
1644 case 2:
1645 if (!arm_feature(env, ARM_FEATURE_V6))
1646 goto bad_reg;
1647 return env->cp15.c0_c2[op2];
1648 case 3: case 4: case 5: case 6: case 7:
1649 return 0;
1650 default:
1651 goto bad_reg;
1652 }
1653 case 1:
1654 /* These registers aren't documented on arm11 cores. However
1655 Linux looks at them anyway. */
1656 if (!arm_feature(env, ARM_FEATURE_V6))
1657 goto bad_reg;
1658 if (crm != 0)
1659 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001660 if (!arm_feature(env, ARM_FEATURE_V7))
1661 return 0;
1662
1663 switch (op2) {
1664 case 0:
1665 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1666 case 1:
1667 return env->cp15.c0_clid;
1668 case 7:
1669 return 0;
1670 }
1671 goto bad_reg;
1672 case 2:
1673 if (op2 != 0 || crm != 0)
balrog610c3c82007-06-24 12:09:48 +00001674 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001675 return env->cp15.c0_cssel;
pbrook9ee6e8b2007-11-11 00:04:49 +00001676 default:
1677 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001678 }
1679 case 1: /* System configuration. */
balrogc3d26892007-07-29 17:57:26 +00001680 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1681 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001682 switch (op2) {
1683 case 0: /* Control register. */
1684 return env->cp15.c1_sys;
1685 case 1: /* Auxiliary control register. */
balrogc1713132007-04-30 01:26:42 +00001686 if (arm_feature(env, ARM_FEATURE_XSCALE))
balrog610c3c82007-06-24 12:09:48 +00001687 return env->cp15.c1_xscaleauxcr;
pbrook9ee6e8b2007-11-11 00:04:49 +00001688 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1689 goto bad_reg;
1690 switch (ARM_CPUID(env)) {
1691 case ARM_CPUID_ARM1026:
1692 return 1;
1693 case ARM_CPUID_ARM1136:
balrog827df9f2008-04-14 21:05:22 +00001694 case ARM_CPUID_ARM1136_R2:
pbrook9ee6e8b2007-11-11 00:04:49 +00001695 return 7;
1696 case ARM_CPUID_ARM11MPCORE:
1697 return 1;
1698 case ARM_CPUID_CORTEXA8:
aurel32533d1772009-03-07 22:10:28 +00001699 return 2;
pbrook9ee6e8b2007-11-11 00:04:49 +00001700 default:
1701 goto bad_reg;
1702 }
bellardb5ff1b32005-11-26 10:38:39 +00001703 case 2: /* Coprocessor access register. */
balrog610c3c82007-06-24 12:09:48 +00001704 if (arm_feature(env, ARM_FEATURE_XSCALE))
1705 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001706 return env->cp15.c1_coproc;
1707 default:
1708 goto bad_reg;
1709 }
pbrookce819862007-05-08 02:30:40 +00001710 case 2: /* MMU Page table control / MPU cache control. */
1711 if (arm_feature(env, ARM_FEATURE_MPU)) {
1712 switch (op2) {
1713 case 0:
1714 return env->cp15.c2_data;
1715 break;
1716 case 1:
1717 return env->cp15.c2_insn;
1718 break;
1719 default:
1720 goto bad_reg;
1721 }
1722 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001723 switch (op2) {
1724 case 0:
1725 return env->cp15.c2_base0;
1726 case 1:
1727 return env->cp15.c2_base1;
1728 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001729 return env->cp15.c2_control;
pbrook9ee6e8b2007-11-11 00:04:49 +00001730 default:
1731 goto bad_reg;
1732 }
1733 }
pbrookce819862007-05-08 02:30:40 +00001734 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001735 return env->cp15.c3;
1736 case 4: /* Reserved. */
1737 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001738 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001739 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1740 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001741 switch (op2) {
1742 case 0:
pbrookce819862007-05-08 02:30:40 +00001743 if (arm_feature(env, ARM_FEATURE_MPU))
1744 return simple_mpu_ap_bits(env->cp15.c5_data);
bellardb5ff1b32005-11-26 10:38:39 +00001745 return env->cp15.c5_data;
1746 case 1:
pbrookce819862007-05-08 02:30:40 +00001747 if (arm_feature(env, ARM_FEATURE_MPU))
1748 return simple_mpu_ap_bits(env->cp15.c5_data);
1749 return env->cp15.c5_insn;
1750 case 2:
1751 if (!arm_feature(env, ARM_FEATURE_MPU))
1752 goto bad_reg;
1753 return env->cp15.c5_data;
1754 case 3:
1755 if (!arm_feature(env, ARM_FEATURE_MPU))
1756 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001757 return env->cp15.c5_insn;
1758 default:
1759 goto bad_reg;
1760 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001761 case 6: /* MMU Fault address. */
pbrookce819862007-05-08 02:30:40 +00001762 if (arm_feature(env, ARM_FEATURE_MPU)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001763 if (crm >= 8)
pbrookce819862007-05-08 02:30:40 +00001764 goto bad_reg;
pbrook9ee6e8b2007-11-11 00:04:49 +00001765 return env->cp15.c6_region[crm];
pbrookce819862007-05-08 02:30:40 +00001766 } else {
balrogc3d26892007-07-29 17:57:26 +00001767 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1768 op2 = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001769 switch (op2) {
1770 case 0:
1771 return env->cp15.c6_data;
1772 case 1:
1773 if (arm_feature(env, ARM_FEATURE_V6)) {
1774 /* Watchpoint Fault Adrress. */
1775 return 0; /* Not implemented. */
1776 } else {
1777 /* Instruction Fault Adrress. */
1778 /* Arm9 doesn't have an IFAR, but implementing it anyway
1779 shouldn't do any harm. */
1780 return env->cp15.c6_insn;
1781 }
1782 case 2:
1783 if (arm_feature(env, ARM_FEATURE_V6)) {
1784 /* Instruction Fault Adrress. */
1785 return env->cp15.c6_insn;
1786 } else {
1787 goto bad_reg;
1788 }
1789 default:
1790 goto bad_reg;
1791 }
bellardb5ff1b32005-11-26 10:38:39 +00001792 }
1793 case 7: /* Cache control. */
pbrook6fbe23d2008-04-01 17:19:11 +00001794 /* FIXME: Should only clear Z flag if destination is r15. */
1795 env->ZF = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001796 return 0;
1797 case 8: /* MMU TLB control. */
1798 goto bad_reg;
1799 case 9: /* Cache lockdown. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001800 switch (op1) {
1801 case 0: /* L1 cache. */
1802 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1803 return 0;
1804 switch (op2) {
1805 case 0:
1806 return env->cp15.c9_data;
1807 case 1:
1808 return env->cp15.c9_insn;
1809 default:
1810 goto bad_reg;
1811 }
1812 case 1: /* L2 cache */
1813 if (crm != 0)
1814 goto bad_reg;
1815 /* L2 Lockdown and Auxiliary control. */
balrogc3d26892007-07-29 17:57:26 +00001816 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00001817 default:
1818 goto bad_reg;
1819 }
1820 case 10: /* MMU TLB lockdown. */
1821 /* ??? TLB lockdown not implemented. */
1822 return 0;
1823 case 11: /* TCM DMA control. */
1824 case 12: /* Reserved. */
1825 goto bad_reg;
1826 case 13: /* Process ID. */
1827 switch (op2) {
1828 case 0:
1829 return env->cp15.c13_fcse;
1830 case 1:
1831 return env->cp15.c13_context;
pbrook9ee6e8b2007-11-11 00:04:49 +00001832 case 2:
1833 return env->cp15.c13_tls1;
1834 case 3:
1835 return env->cp15.c13_tls2;
1836 case 4:
1837 return env->cp15.c13_tls3;
bellardb5ff1b32005-11-26 10:38:39 +00001838 default:
1839 goto bad_reg;
1840 }
1841 case 14: /* Reserved. */
1842 goto bad_reg;
1843 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00001844 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
balrogc3d26892007-07-29 17:57:26 +00001845 if (op2 == 0 && crm == 1)
balrogc1713132007-04-30 01:26:42 +00001846 return env->cp15.c15_cpar;
1847
1848 goto bad_reg;
1849 }
balrogc3d26892007-07-29 17:57:26 +00001850 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1851 switch (crm) {
1852 case 0:
1853 return 0;
1854 case 1: /* Read TI925T configuration. */
1855 return env->cp15.c15_ticonfig;
1856 case 2: /* Read I_max. */
1857 return env->cp15.c15_i_max;
1858 case 3: /* Read I_min. */
1859 return env->cp15.c15_i_min;
1860 case 4: /* Read thread-ID. */
1861 return env->cp15.c15_threadid;
1862 case 8: /* TI925T_status */
1863 return 0;
1864 }
balrog827df9f2008-04-14 21:05:22 +00001865 /* TODO: Peripheral port remap register:
1866 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
1867 * controller base address at $rn & ~0xfff and map size of
1868 * 0x200 << ($rn & 0xfff), when MMU is off. */
balrogc3d26892007-07-29 17:57:26 +00001869 goto bad_reg;
1870 }
bellardb5ff1b32005-11-26 10:38:39 +00001871 return 0;
1872 }
1873bad_reg:
1874 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001875 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
1876 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00001877 return 0;
1878}
1879
pbrookb0109802008-03-31 03:47:03 +00001880void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00001881{
1882 env->banked_r13[bank_number(mode)] = val;
1883}
1884
pbrookb0109802008-03-31 03:47:03 +00001885uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00001886{
1887 return env->banked_r13[bank_number(mode)];
1888}
1889
pbrook8984bd22008-03-31 03:47:48 +00001890uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00001891{
1892 switch (reg) {
1893 case 0: /* APSR */
1894 return xpsr_read(env) & 0xf8000000;
1895 case 1: /* IAPSR */
1896 return xpsr_read(env) & 0xf80001ff;
1897 case 2: /* EAPSR */
1898 return xpsr_read(env) & 0xff00fc00;
1899 case 3: /* xPSR */
1900 return xpsr_read(env) & 0xff00fdff;
1901 case 5: /* IPSR */
1902 return xpsr_read(env) & 0x000001ff;
1903 case 6: /* EPSR */
1904 return xpsr_read(env) & 0x0700fc00;
1905 case 7: /* IEPSR */
1906 return xpsr_read(env) & 0x0700edff;
1907 case 8: /* MSP */
1908 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
1909 case 9: /* PSP */
1910 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
1911 case 16: /* PRIMASK */
1912 return (env->uncached_cpsr & CPSR_I) != 0;
1913 case 17: /* FAULTMASK */
1914 return (env->uncached_cpsr & CPSR_F) != 0;
1915 case 18: /* BASEPRI */
1916 case 19: /* BASEPRI_MAX */
1917 return env->v7m.basepri;
1918 case 20: /* CONTROL */
1919 return env->v7m.control;
1920 default:
1921 /* ??? For debugging only. */
1922 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
1923 return 0;
1924 }
1925}
1926
pbrook8984bd22008-03-31 03:47:48 +00001927void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00001928{
1929 switch (reg) {
1930 case 0: /* APSR */
1931 xpsr_write(env, val, 0xf8000000);
1932 break;
1933 case 1: /* IAPSR */
1934 xpsr_write(env, val, 0xf8000000);
1935 break;
1936 case 2: /* EAPSR */
1937 xpsr_write(env, val, 0xfe00fc00);
1938 break;
1939 case 3: /* xPSR */
1940 xpsr_write(env, val, 0xfe00fc00);
1941 break;
1942 case 5: /* IPSR */
1943 /* IPSR bits are readonly. */
1944 break;
1945 case 6: /* EPSR */
1946 xpsr_write(env, val, 0x0600fc00);
1947 break;
1948 case 7: /* IEPSR */
1949 xpsr_write(env, val, 0x0600fc00);
1950 break;
1951 case 8: /* MSP */
1952 if (env->v7m.current_sp)
1953 env->v7m.other_sp = val;
1954 else
1955 env->regs[13] = val;
1956 break;
1957 case 9: /* PSP */
1958 if (env->v7m.current_sp)
1959 env->regs[13] = val;
1960 else
1961 env->v7m.other_sp = val;
1962 break;
1963 case 16: /* PRIMASK */
1964 if (val & 1)
1965 env->uncached_cpsr |= CPSR_I;
1966 else
1967 env->uncached_cpsr &= ~CPSR_I;
1968 break;
1969 case 17: /* FAULTMASK */
1970 if (val & 1)
1971 env->uncached_cpsr |= CPSR_F;
1972 else
1973 env->uncached_cpsr &= ~CPSR_F;
1974 break;
1975 case 18: /* BASEPRI */
1976 env->v7m.basepri = val & 0xff;
1977 break;
1978 case 19: /* BASEPRI_MAX */
1979 val &= 0xff;
1980 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
1981 env->v7m.basepri = val;
1982 break;
1983 case 20: /* CONTROL */
1984 env->v7m.control = val & 3;
1985 switch_v7m_sp(env, (val & 2) != 0);
1986 break;
1987 default:
1988 /* ??? For debugging only. */
1989 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
1990 return;
1991 }
1992}
1993
balrogc1713132007-04-30 01:26:42 +00001994void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
1995 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
1996 void *opaque)
1997{
1998 if (cpnum < 0 || cpnum > 14) {
1999 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2000 return;
2001 }
2002
2003 env->cp[cpnum].cp_read = cp_read;
2004 env->cp[cpnum].cp_write = cp_write;
2005 env->cp[cpnum].opaque = opaque;
2006}
2007
bellardb5ff1b32005-11-26 10:38:39 +00002008#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00002009
2010/* Note that signed overflow is undefined in C. The following routines are
2011 careful to use unsigned types where modulo arithmetic is required.
2012 Failure to do so _will_ break on newer gcc. */
2013
2014/* Signed saturating arithmetic. */
2015
aurel321654b2d2008-04-11 04:55:07 +00002016/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002017static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2018{
2019 uint16_t res;
2020
2021 res = a + b;
2022 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2023 if (a & 0x8000)
2024 res = 0x8000;
2025 else
2026 res = 0x7fff;
2027 }
2028 return res;
2029}
2030
aurel321654b2d2008-04-11 04:55:07 +00002031/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002032static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2033{
2034 uint8_t res;
2035
2036 res = a + b;
2037 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2038 if (a & 0x80)
2039 res = 0x80;
2040 else
2041 res = 0x7f;
2042 }
2043 return res;
2044}
2045
aurel321654b2d2008-04-11 04:55:07 +00002046/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002047static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2048{
2049 uint16_t res;
2050
2051 res = a - b;
2052 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2053 if (a & 0x8000)
2054 res = 0x8000;
2055 else
2056 res = 0x7fff;
2057 }
2058 return res;
2059}
2060
aurel321654b2d2008-04-11 04:55:07 +00002061/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002062static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2063{
2064 uint8_t res;
2065
2066 res = a - b;
2067 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2068 if (a & 0x80)
2069 res = 0x80;
2070 else
2071 res = 0x7f;
2072 }
2073 return res;
2074}
2075
2076#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2077#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2078#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2079#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2080#define PFX q
2081
2082#include "op_addsub.h"
2083
2084/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00002085static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002086{
2087 uint16_t res;
2088 res = a + b;
2089 if (res < a)
2090 res = 0xffff;
2091 return res;
2092}
2093
pbrook460a09c2008-05-01 12:04:35 +00002094static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002095{
2096 if (a < b)
2097 return a - b;
2098 else
2099 return 0;
2100}
2101
2102static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2103{
2104 uint8_t res;
2105 res = a + b;
2106 if (res < a)
2107 res = 0xff;
2108 return res;
2109}
2110
2111static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2112{
2113 if (a < b)
2114 return a - b;
2115 else
2116 return 0;
2117}
2118
2119#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2120#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2121#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2122#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2123#define PFX uq
2124
2125#include "op_addsub.h"
2126
2127/* Signed modulo arithmetic. */
2128#define SARITH16(a, b, n, op) do { \
2129 int32_t sum; \
2130 sum = (int16_t)((uint16_t)(a) op (uint16_t)(b)); \
2131 RESULT(sum, n, 16); \
2132 if (sum >= 0) \
2133 ge |= 3 << (n * 2); \
2134 } while(0)
2135
2136#define SARITH8(a, b, n, op) do { \
2137 int32_t sum; \
2138 sum = (int8_t)((uint8_t)(a) op (uint8_t)(b)); \
2139 RESULT(sum, n, 8); \
2140 if (sum >= 0) \
2141 ge |= 1 << n; \
2142 } while(0)
2143
2144
2145#define ADD16(a, b, n) SARITH16(a, b, n, +)
2146#define SUB16(a, b, n) SARITH16(a, b, n, -)
2147#define ADD8(a, b, n) SARITH8(a, b, n, +)
2148#define SUB8(a, b, n) SARITH8(a, b, n, -)
2149#define PFX s
2150#define ARITH_GE
2151
2152#include "op_addsub.h"
2153
2154/* Unsigned modulo arithmetic. */
2155#define ADD16(a, b, n) do { \
2156 uint32_t sum; \
2157 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2158 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00002159 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00002160 ge |= 3 << (n * 2); \
2161 } while(0)
2162
2163#define ADD8(a, b, n) do { \
2164 uint32_t sum; \
2165 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2166 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00002167 if ((sum >> 8) == 1) \
2168 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002169 } while(0)
2170
2171#define SUB16(a, b, n) do { \
2172 uint32_t sum; \
2173 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2174 RESULT(sum, n, 16); \
2175 if ((sum >> 16) == 0) \
2176 ge |= 3 << (n * 2); \
2177 } while(0)
2178
2179#define SUB8(a, b, n) do { \
2180 uint32_t sum; \
2181 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2182 RESULT(sum, n, 8); \
2183 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00002184 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002185 } while(0)
2186
2187#define PFX u
2188#define ARITH_GE
2189
2190#include "op_addsub.h"
2191
2192/* Halved signed arithmetic. */
2193#define ADD16(a, b, n) \
2194 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2195#define SUB16(a, b, n) \
2196 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2197#define ADD8(a, b, n) \
2198 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2199#define SUB8(a, b, n) \
2200 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2201#define PFX sh
2202
2203#include "op_addsub.h"
2204
2205/* Halved unsigned arithmetic. */
2206#define ADD16(a, b, n) \
2207 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2208#define SUB16(a, b, n) \
2209 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2210#define ADD8(a, b, n) \
2211 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2212#define SUB8(a, b, n) \
2213 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2214#define PFX uh
2215
2216#include "op_addsub.h"
2217
2218static inline uint8_t do_usad(uint8_t a, uint8_t b)
2219{
2220 if (a > b)
2221 return a - b;
2222 else
2223 return b - a;
2224}
2225
2226/* Unsigned sum of absolute byte differences. */
2227uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2228{
2229 uint32_t sum;
2230 sum = do_usad(a, b);
2231 sum += do_usad(a >> 8, b >> 8);
2232 sum += do_usad(a >> 16, b >>16);
2233 sum += do_usad(a >> 24, b >> 24);
2234 return sum;
2235}
2236
2237/* For ARMv6 SEL instruction. */
2238uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2239{
2240 uint32_t mask;
2241
2242 mask = 0;
2243 if (flags & 1)
2244 mask |= 0xff;
2245 if (flags & 2)
2246 mask |= 0xff00;
2247 if (flags & 4)
2248 mask |= 0xff0000;
2249 if (flags & 8)
2250 mask |= 0xff000000;
2251 return (a & mask) | (b & ~mask);
2252}
2253
pbrook5e3f8782008-03-31 03:47:34 +00002254uint32_t HELPER(logicq_cc)(uint64_t val)
2255{
2256 return (val >> 32) | (val != 0);
2257}
pbrook4373f3c2008-03-31 03:47:19 +00002258
2259/* VFP support. We follow the convention used for VFP instrunctions:
2260 Single precition routines have a "s" suffix, double precision a
2261 "d" suffix. */
2262
2263/* Convert host exception flags to vfp form. */
2264static inline int vfp_exceptbits_from_host(int host_bits)
2265{
2266 int target_bits = 0;
2267
2268 if (host_bits & float_flag_invalid)
2269 target_bits |= 1;
2270 if (host_bits & float_flag_divbyzero)
2271 target_bits |= 2;
2272 if (host_bits & float_flag_overflow)
2273 target_bits |= 4;
2274 if (host_bits & float_flag_underflow)
2275 target_bits |= 8;
2276 if (host_bits & float_flag_inexact)
2277 target_bits |= 0x10;
2278 return target_bits;
2279}
2280
2281uint32_t HELPER(vfp_get_fpscr)(CPUState *env)
2282{
2283 int i;
2284 uint32_t fpscr;
2285
2286 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2287 | (env->vfp.vec_len << 16)
2288 | (env->vfp.vec_stride << 20);
2289 i = get_float_exception_flags(&env->vfp.fp_status);
2290 fpscr |= vfp_exceptbits_from_host(i);
2291 return fpscr;
2292}
2293
2294/* Convert vfp exception flags to target form. */
2295static inline int vfp_exceptbits_to_host(int target_bits)
2296{
2297 int host_bits = 0;
2298
2299 if (target_bits & 1)
2300 host_bits |= float_flag_invalid;
2301 if (target_bits & 2)
2302 host_bits |= float_flag_divbyzero;
2303 if (target_bits & 4)
2304 host_bits |= float_flag_overflow;
2305 if (target_bits & 8)
2306 host_bits |= float_flag_underflow;
2307 if (target_bits & 0x10)
2308 host_bits |= float_flag_inexact;
2309 return host_bits;
2310}
2311
2312void HELPER(vfp_set_fpscr)(CPUState *env, uint32_t val)
2313{
2314 int i;
2315 uint32_t changed;
2316
2317 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2318 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2319 env->vfp.vec_len = (val >> 16) & 7;
2320 env->vfp.vec_stride = (val >> 20) & 3;
2321
2322 changed ^= val;
2323 if (changed & (3 << 22)) {
2324 i = (val >> 22) & 3;
2325 switch (i) {
2326 case 0:
2327 i = float_round_nearest_even;
2328 break;
2329 case 1:
2330 i = float_round_up;
2331 break;
2332 case 2:
2333 i = float_round_down;
2334 break;
2335 case 3:
2336 i = float_round_to_zero;
2337 break;
2338 }
2339 set_float_rounding_mode(i, &env->vfp.fp_status);
2340 }
pbrookfe76d972008-12-19 14:33:59 +00002341 if (changed & (1 << 24))
2342 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
pbrook5c7908e2008-12-19 13:53:37 +00002343 if (changed & (1 << 25))
2344 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002345
2346 i = vfp_exceptbits_to_host((val >> 8) & 0x1f);
2347 set_float_exception_flags(i, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002348}
2349
2350#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2351
2352#define VFP_BINOP(name) \
2353float32 VFP_HELPER(name, s)(float32 a, float32 b, CPUState *env) \
2354{ \
2355 return float32_ ## name (a, b, &env->vfp.fp_status); \
2356} \
2357float64 VFP_HELPER(name, d)(float64 a, float64 b, CPUState *env) \
2358{ \
2359 return float64_ ## name (a, b, &env->vfp.fp_status); \
2360}
2361VFP_BINOP(add)
2362VFP_BINOP(sub)
2363VFP_BINOP(mul)
2364VFP_BINOP(div)
2365#undef VFP_BINOP
2366
2367float32 VFP_HELPER(neg, s)(float32 a)
2368{
2369 return float32_chs(a);
2370}
2371
2372float64 VFP_HELPER(neg, d)(float64 a)
2373{
balrog66230e02008-04-20 00:58:01 +00002374 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002375}
2376
2377float32 VFP_HELPER(abs, s)(float32 a)
2378{
2379 return float32_abs(a);
2380}
2381
2382float64 VFP_HELPER(abs, d)(float64 a)
2383{
balrog66230e02008-04-20 00:58:01 +00002384 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002385}
2386
2387float32 VFP_HELPER(sqrt, s)(float32 a, CPUState *env)
2388{
2389 return float32_sqrt(a, &env->vfp.fp_status);
2390}
2391
2392float64 VFP_HELPER(sqrt, d)(float64 a, CPUState *env)
2393{
2394 return float64_sqrt(a, &env->vfp.fp_status);
2395}
2396
2397/* XXX: check quiet/signaling case */
2398#define DO_VFP_cmp(p, type) \
2399void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2400{ \
2401 uint32_t flags; \
2402 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2403 case 0: flags = 0x6; break; \
2404 case -1: flags = 0x8; break; \
2405 case 1: flags = 0x2; break; \
2406 default: case 2: flags = 0x3; break; \
2407 } \
2408 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2409 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2410} \
2411void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2412{ \
2413 uint32_t flags; \
2414 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2415 case 0: flags = 0x6; break; \
2416 case -1: flags = 0x8; break; \
2417 case 1: flags = 0x2; break; \
2418 default: case 2: flags = 0x3; break; \
2419 } \
2420 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2421 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2422}
2423DO_VFP_cmp(s, float32)
2424DO_VFP_cmp(d, float64)
2425#undef DO_VFP_cmp
2426
2427/* Helper routines to perform bitwise copies between float and int. */
2428static inline float32 vfp_itos(uint32_t i)
2429{
2430 union {
2431 uint32_t i;
2432 float32 s;
2433 } v;
2434
2435 v.i = i;
2436 return v.s;
2437}
2438
2439static inline uint32_t vfp_stoi(float32 s)
2440{
2441 union {
2442 uint32_t i;
2443 float32 s;
2444 } v;
2445
2446 v.s = s;
2447 return v.i;
2448}
2449
2450static inline float64 vfp_itod(uint64_t i)
2451{
2452 union {
2453 uint64_t i;
2454 float64 d;
2455 } v;
2456
2457 v.i = i;
2458 return v.d;
2459}
2460
2461static inline uint64_t vfp_dtoi(float64 d)
2462{
2463 union {
2464 uint64_t i;
2465 float64 d;
2466 } v;
2467
2468 v.d = d;
2469 return v.i;
2470}
2471
2472/* Integer to float conversion. */
2473float32 VFP_HELPER(uito, s)(float32 x, CPUState *env)
2474{
2475 return uint32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2476}
2477
2478float64 VFP_HELPER(uito, d)(float32 x, CPUState *env)
2479{
2480 return uint32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2481}
2482
2483float32 VFP_HELPER(sito, s)(float32 x, CPUState *env)
2484{
2485 return int32_to_float32(vfp_stoi(x), &env->vfp.fp_status);
2486}
2487
2488float64 VFP_HELPER(sito, d)(float32 x, CPUState *env)
2489{
2490 return int32_to_float64(vfp_stoi(x), &env->vfp.fp_status);
2491}
2492
2493/* Float to integer conversion. */
2494float32 VFP_HELPER(toui, s)(float32 x, CPUState *env)
2495{
2496 return vfp_itos(float32_to_uint32(x, &env->vfp.fp_status));
2497}
2498
2499float32 VFP_HELPER(toui, d)(float64 x, CPUState *env)
2500{
2501 return vfp_itos(float64_to_uint32(x, &env->vfp.fp_status));
2502}
2503
2504float32 VFP_HELPER(tosi, s)(float32 x, CPUState *env)
2505{
2506 return vfp_itos(float32_to_int32(x, &env->vfp.fp_status));
2507}
2508
2509float32 VFP_HELPER(tosi, d)(float64 x, CPUState *env)
2510{
2511 return vfp_itos(float64_to_int32(x, &env->vfp.fp_status));
2512}
2513
2514float32 VFP_HELPER(touiz, s)(float32 x, CPUState *env)
2515{
2516 return vfp_itos(float32_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2517}
2518
2519float32 VFP_HELPER(touiz, d)(float64 x, CPUState *env)
2520{
2521 return vfp_itos(float64_to_uint32_round_to_zero(x, &env->vfp.fp_status));
2522}
2523
2524float32 VFP_HELPER(tosiz, s)(float32 x, CPUState *env)
2525{
2526 return vfp_itos(float32_to_int32_round_to_zero(x, &env->vfp.fp_status));
2527}
2528
2529float32 VFP_HELPER(tosiz, d)(float64 x, CPUState *env)
2530{
2531 return vfp_itos(float64_to_int32_round_to_zero(x, &env->vfp.fp_status));
2532}
2533
2534/* floating point conversion */
2535float64 VFP_HELPER(fcvtd, s)(float32 x, CPUState *env)
2536{
2537 return float32_to_float64(x, &env->vfp.fp_status);
2538}
2539
2540float32 VFP_HELPER(fcvts, d)(float64 x, CPUState *env)
2541{
2542 return float64_to_float32(x, &env->vfp.fp_status);
2543}
2544
2545/* VFP3 fixed point conversion. */
2546#define VFP_CONV_FIX(name, p, ftype, itype, sign) \
2547ftype VFP_HELPER(name##to, p)(ftype x, uint32_t shift, CPUState *env) \
2548{ \
2549 ftype tmp; \
2550 tmp = sign##int32_to_##ftype ((itype)vfp_##p##toi(x), \
2551 &env->vfp.fp_status); \
pbrook644ad802008-12-19 13:02:08 +00002552 return ftype##_scalbn(tmp, -(int)shift, &env->vfp.fp_status); \
pbrook4373f3c2008-03-31 03:47:19 +00002553} \
2554ftype VFP_HELPER(to##name, p)(ftype x, uint32_t shift, CPUState *env) \
2555{ \
2556 ftype tmp; \
2557 tmp = ftype##_scalbn(x, shift, &env->vfp.fp_status); \
2558 return vfp_ito##p((itype)ftype##_to_##sign##int32_round_to_zero(tmp, \
2559 &env->vfp.fp_status)); \
2560}
2561
2562VFP_CONV_FIX(sh, d, float64, int16, )
2563VFP_CONV_FIX(sl, d, float64, int32, )
2564VFP_CONV_FIX(uh, d, float64, uint16, u)
2565VFP_CONV_FIX(ul, d, float64, uint32, u)
2566VFP_CONV_FIX(sh, s, float32, int16, )
2567VFP_CONV_FIX(sl, s, float32, int32, )
2568VFP_CONV_FIX(uh, s, float32, uint16, u)
2569VFP_CONV_FIX(ul, s, float32, uint32, u)
2570#undef VFP_CONV_FIX
2571
Paul Brook60011492009-11-19 16:45:20 +00002572/* Half precision conversions. */
2573float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2574{
2575 float_status *s = &env->vfp.fp_status;
2576 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2577 return float16_to_float32(a, ieee, s);
2578}
2579
2580uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUState *env)
2581{
2582 float_status *s = &env->vfp.fp_status;
2583 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
2584 return float32_to_float16(a, ieee, s);
2585}
2586
pbrook4373f3c2008-03-31 03:47:19 +00002587float32 HELPER(recps_f32)(float32 a, float32 b, CPUState *env)
2588{
2589 float_status *s = &env->vfp.fp_status;
2590 float32 two = int32_to_float32(2, s);
2591 return float32_sub(two, float32_mul(a, b, s), s);
2592}
2593
2594float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUState *env)
2595{
2596 float_status *s = &env->vfp.fp_status;
2597 float32 three = int32_to_float32(3, s);
2598 return float32_sub(three, float32_mul(a, b, s), s);
2599}
2600
pbrook8f8e3aa2008-03-31 03:48:01 +00002601/* NEON helpers. */
2602
pbrook4373f3c2008-03-31 03:47:19 +00002603/* TODO: The architecture specifies the value that the estimate functions
2604 should return. We return the exact reciprocal/root instead. */
2605float32 HELPER(recpe_f32)(float32 a, CPUState *env)
2606{
2607 float_status *s = &env->vfp.fp_status;
2608 float32 one = int32_to_float32(1, s);
2609 return float32_div(one, a, s);
2610}
2611
2612float32 HELPER(rsqrte_f32)(float32 a, CPUState *env)
2613{
2614 float_status *s = &env->vfp.fp_status;
2615 float32 one = int32_to_float32(1, s);
2616 return float32_div(one, float32_sqrt(a, s), s);
2617}
2618
2619uint32_t HELPER(recpe_u32)(uint32_t a, CPUState *env)
2620{
2621 float_status *s = &env->vfp.fp_status;
2622 float32 tmp;
2623 tmp = int32_to_float32(a, s);
2624 tmp = float32_scalbn(tmp, -32, s);
2625 tmp = helper_recpe_f32(tmp, env);
2626 tmp = float32_scalbn(tmp, 31, s);
2627 return float32_to_int32(tmp, s);
2628}
2629
2630uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)
2631{
2632 float_status *s = &env->vfp.fp_status;
2633 float32 tmp;
2634 tmp = int32_to_float32(a, s);
2635 tmp = float32_scalbn(tmp, -32, s);
2636 tmp = helper_rsqrte_f32(tmp, env);
2637 tmp = float32_scalbn(tmp, 31, s);
2638 return float32_to_int32(tmp, s);
2639}
pbrookfe1479c2008-12-19 13:18:36 +00002640
2641void HELPER(set_teecr)(CPUState *env, uint32_t val)
2642{
2643 val &= 1;
2644 if (env->teecr != val) {
2645 env->teecr = val;
2646 tb_flush(env);
2647 }
2648}