blob: 261d547d64b2afccae62ea1b9556e7a360cd5cfd [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"
pbrook9ee6e8b2007-11-11 00:04:49 +00006#include "gdbstub.h"
LluĂ­s7b592202011-04-13 18:38:24 +02007#include "helper.h"
aurel32ca10f862008-04-11 21:35:42 +00008#include "qemu-common.h"
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +02009#include "host-utils.h"
Paul Brook4f78c9a2010-04-05 19:56:34 +010010#if !defined(CONFIG_USER_ONLY)
Paul Brook983fe822010-04-05 19:34:51 +010011#include "hw/loader.h"
Paul Brook4f78c9a2010-04-05 19:56:34 +010012#endif
pbrook9ee6e8b2007-11-11 00:04:49 +000013
Paul Brook10055562009-11-19 16:45:20 +000014static uint32_t cortexa9_cp15_c0_c1[8] =
15{ 0x1031, 0x11, 0x000, 0, 0x00100103, 0x20000000, 0x01230000, 0x00002111 };
16
17static uint32_t cortexa9_cp15_c0_c2[8] =
18{ 0x00101111, 0x13112111, 0x21232041, 0x11112131, 0x00111142, 0, 0, 0 };
19
pbrook9ee6e8b2007-11-11 00:04:49 +000020static uint32_t cortexa8_cp15_c0_c1[8] =
21{ 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
22
23static uint32_t cortexa8_cp15_c0_c2[8] =
24{ 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
25
26static uint32_t mpcore_cp15_c0_c1[8] =
27{ 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
28
29static uint32_t mpcore_cp15_c0_c2[8] =
30{ 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
31
32static uint32_t arm1136_cp15_c0_c1[8] =
33{ 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
34
35static uint32_t arm1136_cp15_c0_c2[8] =
36{ 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
bellardb5ff1b32005-11-26 10:38:39 +000037
Jamie Iles7807eed2011-07-20 10:32:54 +000038static uint32_t arm1176_cp15_c0_c1[8] =
39{ 0x111, 0x11, 0x33, 0, 0x01130003, 0x10030302, 0x01222100, 0 };
40
41static uint32_t arm1176_cp15_c0_c2[8] =
42{ 0x0140011, 0x12002111, 0x11231121, 0x01102131, 0x01141, 0, 0, 0 };
43
bellardaaed9092007-11-10 15:15:54 +000044static uint32_t cpu_arm_find_by_name(const char *name);
45
pbrookf3d6b952007-03-11 13:03:18 +000046static inline void set_feature(CPUARMState *env, int feature)
47{
48 env->features |= 1u << feature;
49}
50
51static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
52{
53 env->cp15.c0_cpuid = id;
54 switch (id) {
55 case ARM_CPUID_ARM926:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040056 set_feature(env, ARM_FEATURE_V5);
pbrookf3d6b952007-03-11 13:03:18 +000057 set_feature(env, ARM_FEATURE_VFP);
58 env->vfp.xregs[ARM_VFP_FPSID] = 0x41011090;
balrogc1713132007-04-30 01:26:42 +000059 env->cp15.c0_cachetype = 0x1dd20d2;
balrog610c3c82007-06-24 12:09:48 +000060 env->cp15.c1_sys = 0x00090078;
pbrookf3d6b952007-03-11 13:03:18 +000061 break;
pbrookce819862007-05-08 02:30:40 +000062 case ARM_CPUID_ARM946:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040063 set_feature(env, ARM_FEATURE_V5);
pbrookce819862007-05-08 02:30:40 +000064 set_feature(env, ARM_FEATURE_MPU);
65 env->cp15.c0_cachetype = 0x0f004006;
balrog610c3c82007-06-24 12:09:48 +000066 env->cp15.c1_sys = 0x00000078;
pbrookce819862007-05-08 02:30:40 +000067 break;
pbrookf3d6b952007-03-11 13:03:18 +000068 case ARM_CPUID_ARM1026:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040069 set_feature(env, ARM_FEATURE_V5);
pbrookf3d6b952007-03-11 13:03:18 +000070 set_feature(env, ARM_FEATURE_VFP);
71 set_feature(env, ARM_FEATURE_AUXCR);
72 env->vfp.xregs[ARM_VFP_FPSID] = 0x410110a0;
balrogc1713132007-04-30 01:26:42 +000073 env->cp15.c0_cachetype = 0x1dd20d2;
balrog610c3c82007-06-24 12:09:48 +000074 env->cp15.c1_sys = 0x00090078;
balrogc1713132007-04-30 01:26:42 +000075 break;
pbrook9ee6e8b2007-11-11 00:04:49 +000076 case ARM_CPUID_ARM1136:
Peter Maydell906879a2011-07-20 10:32:55 +000077 /* This is the 1136 r1, which is a v6K core */
78 set_feature(env, ARM_FEATURE_V6K);
79 /* Fall through */
80 case ARM_CPUID_ARM1136_R2:
81 /* What qemu calls "arm1136_r2" is actually the 1136 r0p2, ie an
82 * older core than plain "arm1136". In particular this does not
83 * have the v6K features.
84 */
pbrook9ee6e8b2007-11-11 00:04:49 +000085 set_feature(env, ARM_FEATURE_V6);
86 set_feature(env, ARM_FEATURE_VFP);
Peter Maydell906879a2011-07-20 10:32:55 +000087 /* These ID register values are correct for 1136 but may be wrong
88 * for 1136_r2 (in particular r0p2 does not actually implement most
89 * of the ID registers).
90 */
pbrook9ee6e8b2007-11-11 00:04:49 +000091 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
92 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
93 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
94 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +000095 memcpy(env->cp15.c0_c2, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +000096 env->cp15.c0_cachetype = 0x1dd20d2;
Juha Riihimäki16440c52010-12-08 13:15:18 +020097 env->cp15.c1_sys = 0x00050078;
pbrook9ee6e8b2007-11-11 00:04:49 +000098 break;
Jamie Iles7807eed2011-07-20 10:32:54 +000099 case ARM_CPUID_ARM1176:
Jamie Iles7807eed2011-07-20 10:32:54 +0000100 set_feature(env, ARM_FEATURE_V6K);
101 set_feature(env, ARM_FEATURE_VFP);
Peter Maydell906879a2011-07-20 10:32:55 +0000102 set_feature(env, ARM_FEATURE_VAPA);
Jamie Iles7807eed2011-07-20 10:32:54 +0000103 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b5;
104 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
105 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
106 memcpy(env->cp15.c0_c1, arm1176_cp15_c0_c1, 8 * sizeof(uint32_t));
107 memcpy(env->cp15.c0_c2, arm1176_cp15_c0_c2, 8 * sizeof(uint32_t));
108 env->cp15.c0_cachetype = 0x1dd20d2;
109 env->cp15.c1_sys = 0x00050078;
110 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000111 case ARM_CPUID_ARM11MPCORE:
pbrook9ee6e8b2007-11-11 00:04:49 +0000112 set_feature(env, ARM_FEATURE_V6K);
113 set_feature(env, ARM_FEATURE_VFP);
Peter Maydell906879a2011-07-20 10:32:55 +0000114 set_feature(env, ARM_FEATURE_VAPA);
pbrook9ee6e8b2007-11-11 00:04:49 +0000115 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
116 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
117 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
118 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000119 memcpy(env->cp15.c0_c2, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +0000120 env->cp15.c0_cachetype = 0x1dd20d2;
121 break;
122 case ARM_CPUID_CORTEXA8:
pbrook9ee6e8b2007-11-11 00:04:49 +0000123 set_feature(env, ARM_FEATURE_V7);
pbrook9ee6e8b2007-11-11 00:04:49 +0000124 set_feature(env, ARM_FEATURE_VFP3);
125 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +0000126 set_feature(env, ARM_FEATURE_THUMB2EE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000127 env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
128 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
129 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
130 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000131 memcpy(env->cp15.c0_c2, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrooka49ea272008-12-19 13:37:53 +0000132 env->cp15.c0_cachetype = 0x82048004;
133 env->cp15.c0_clid = (1 << 27) | (2 << 24) | 3;
134 env->cp15.c0_ccsid[0] = 0xe007e01a; /* 16k L1 dcache. */
135 env->cp15.c0_ccsid[1] = 0x2007e01a; /* 16k L1 icache. */
136 env->cp15.c0_ccsid[2] = 0xf0000000; /* No L2 icache. */
Mattias Holm9c486ad2010-12-08 13:15:17 +0200137 env->cp15.c1_sys = 0x00c50078;
pbrook9ee6e8b2007-11-11 00:04:49 +0000138 break;
Paul Brook10055562009-11-19 16:45:20 +0000139 case ARM_CPUID_CORTEXA9:
Paul Brook10055562009-11-19 16:45:20 +0000140 set_feature(env, ARM_FEATURE_V7);
Paul Brook10055562009-11-19 16:45:20 +0000141 set_feature(env, ARM_FEATURE_VFP3);
142 set_feature(env, ARM_FEATURE_VFP_FP16);
143 set_feature(env, ARM_FEATURE_NEON);
144 set_feature(env, ARM_FEATURE_THUMB2EE);
Peter Maydelle1bbf442011-02-03 19:43:22 +0000145 /* Note that A9 supports the MP extensions even for
146 * A9UP and single-core A9MP (which are both different
147 * and valid configurations; we don't model A9UP).
148 */
149 set_feature(env, ARM_FEATURE_V7MP);
Paul Brook10055562009-11-19 16:45:20 +0000150 env->vfp.xregs[ARM_VFP_FPSID] = 0x41034000; /* Guess */
151 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
152 env->vfp.xregs[ARM_VFP_MVFR1] = 0x01111111;
153 memcpy(env->cp15.c0_c1, cortexa9_cp15_c0_c1, 8 * sizeof(uint32_t));
154 memcpy(env->cp15.c0_c2, cortexa9_cp15_c0_c2, 8 * sizeof(uint32_t));
155 env->cp15.c0_cachetype = 0x80038003;
156 env->cp15.c0_clid = (1 << 27) | (1 << 24) | 3;
157 env->cp15.c0_ccsid[0] = 0xe00fe015; /* 16k L1 dcache. */
158 env->cp15.c0_ccsid[1] = 0x200fe015; /* 16k L1 icache. */
Juha Riihimäki16440c52010-12-08 13:15:18 +0200159 env->cp15.c1_sys = 0x00c50078;
Paul Brook10055562009-11-19 16:45:20 +0000160 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000161 case ARM_CPUID_CORTEXM3:
pbrook9ee6e8b2007-11-11 00:04:49 +0000162 set_feature(env, ARM_FEATURE_V7);
163 set_feature(env, ARM_FEATURE_M);
pbrook9ee6e8b2007-11-11 00:04:49 +0000164 break;
165 case ARM_CPUID_ANY: /* For userspace emulation. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000166 set_feature(env, ARM_FEATURE_V7);
Peter Maydellda97f522011-10-19 16:14:07 +0000167 set_feature(env, ARM_FEATURE_VFP4);
Paul Brook60011492009-11-19 16:45:20 +0000168 set_feature(env, ARM_FEATURE_VFP_FP16);
pbrook9ee6e8b2007-11-11 00:04:49 +0000169 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +0000170 set_feature(env, ARM_FEATURE_THUMB2EE);
Peter Maydellb8b8ea02011-10-19 16:14:06 +0000171 set_feature(env, ARM_FEATURE_ARM_DIV);
Peter Maydelle1bbf442011-02-03 19:43:22 +0000172 set_feature(env, ARM_FEATURE_V7MP);
pbrook9ee6e8b2007-11-11 00:04:49 +0000173 break;
balrogc3d26892007-07-29 17:57:26 +0000174 case ARM_CPUID_TI915T:
175 case ARM_CPUID_TI925T:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400176 set_feature(env, ARM_FEATURE_V4T);
balrogc3d26892007-07-29 17:57:26 +0000177 set_feature(env, ARM_FEATURE_OMAPCP);
178 env->cp15.c0_cpuid = ARM_CPUID_TI925T; /* Depends on wiring. */
179 env->cp15.c0_cachetype = 0x5109149;
180 env->cp15.c1_sys = 0x00000070;
181 env->cp15.c15_i_max = 0x000;
182 env->cp15.c15_i_min = 0xff0;
183 break;
balrogc1713132007-04-30 01:26:42 +0000184 case ARM_CPUID_PXA250:
185 case ARM_CPUID_PXA255:
186 case ARM_CPUID_PXA260:
187 case ARM_CPUID_PXA261:
188 case ARM_CPUID_PXA262:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400189 set_feature(env, ARM_FEATURE_V5);
balrogc1713132007-04-30 01:26:42 +0000190 set_feature(env, ARM_FEATURE_XSCALE);
191 /* JTAG_ID is ((id << 28) | 0x09265013) */
192 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000193 env->cp15.c1_sys = 0x00000078;
balrogc1713132007-04-30 01:26:42 +0000194 break;
195 case ARM_CPUID_PXA270_A0:
196 case ARM_CPUID_PXA270_A1:
197 case ARM_CPUID_PXA270_B0:
198 case ARM_CPUID_PXA270_B1:
199 case ARM_CPUID_PXA270_C0:
200 case ARM_CPUID_PXA270_C5:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400201 set_feature(env, ARM_FEATURE_V5);
balrogc1713132007-04-30 01:26:42 +0000202 set_feature(env, ARM_FEATURE_XSCALE);
203 /* JTAG_ID is ((id << 28) | 0x09265013) */
balrog18c9b562007-04-30 02:02:17 +0000204 set_feature(env, ARM_FEATURE_IWMMXT);
205 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
balrogc1713132007-04-30 01:26:42 +0000206 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000207 env->cp15.c1_sys = 0x00000078;
pbrookf3d6b952007-03-11 13:03:18 +0000208 break;
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +0400209 case ARM_CPUID_SA1100:
210 case ARM_CPUID_SA1110:
211 set_feature(env, ARM_FEATURE_STRONGARM);
212 env->cp15.c1_sys = 0x00000070;
213 break;
pbrookf3d6b952007-03-11 13:03:18 +0000214 default:
215 cpu_abort(env, "Bad CPU ID: %x\n", id);
216 break;
217 }
Peter Maydell906879a2011-07-20 10:32:55 +0000218
219 /* Some features automatically imply others: */
220 if (arm_feature(env, ARM_FEATURE_V7)) {
221 set_feature(env, ARM_FEATURE_VAPA);
Andreas Färberb3faf5f2011-12-13 18:19:24 +0000222 set_feature(env, ARM_FEATURE_THUMB2);
Andreas Färberbbc5c5f2011-12-13 18:19:24 +0000223 if (!arm_feature(env, ARM_FEATURE_M)) {
224 set_feature(env, ARM_FEATURE_V6K);
225 } else {
226 set_feature(env, ARM_FEATURE_V6);
227 }
Peter Maydell906879a2011-07-20 10:32:55 +0000228 }
Andreas Färber6bf62122011-12-13 18:19:24 +0000229 if (arm_feature(env, ARM_FEATURE_V6K)) {
230 set_feature(env, ARM_FEATURE_V6);
231 }
Andreas Färber08c40f32011-12-13 18:19:24 +0000232 if (arm_feature(env, ARM_FEATURE_V6)) {
233 set_feature(env, ARM_FEATURE_V5);
Andreas Färber10e87702011-12-13 18:19:24 +0000234 if (!arm_feature(env, ARM_FEATURE_M)) {
235 set_feature(env, ARM_FEATURE_AUXCR);
236 }
Andreas Färber08c40f32011-12-13 18:19:24 +0000237 }
Andreas Färberddb572e2011-12-13 18:19:23 +0000238 if (arm_feature(env, ARM_FEATURE_V5)) {
239 set_feature(env, ARM_FEATURE_V4T);
240 }
Andreas Färber908ce982011-12-13 18:19:24 +0000241 if (arm_feature(env, ARM_FEATURE_M)) {
242 set_feature(env, ARM_FEATURE_THUMB_DIV);
243 }
Peter Maydellb8b8ea02011-10-19 16:14:06 +0000244 if (arm_feature(env, ARM_FEATURE_ARM_DIV)) {
245 set_feature(env, ARM_FEATURE_THUMB_DIV);
246 }
Andreas Färberdb8336c2011-12-13 18:19:25 +0000247 if (arm_feature(env, ARM_FEATURE_VFP4)) {
248 set_feature(env, ARM_FEATURE_VFP3);
249 }
Andreas Färber86e72db2011-12-13 18:19:24 +0000250 if (arm_feature(env, ARM_FEATURE_VFP3)) {
251 set_feature(env, ARM_FEATURE_VFP);
252 }
pbrookf3d6b952007-03-11 13:03:18 +0000253}
254
pbrook40f137e2006-02-20 00:33:36 +0000255void cpu_reset(CPUARMState *env)
256{
pbrookf3d6b952007-03-11 13:03:18 +0000257 uint32_t id;
aliguorieca1bdf2009-01-26 19:54:31 +0000258
259 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
260 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
261 log_cpu_state(env, 0);
262 }
263
pbrookf3d6b952007-03-11 13:03:18 +0000264 id = env->cp15.c0_cpuid;
265 memset(env, 0, offsetof(CPUARMState, breakpoints));
266 if (id)
267 cpu_reset_model_id(env, id);
pbrook40f137e2006-02-20 00:33:36 +0000268#if defined (CONFIG_USER_ONLY)
269 env->uncached_cpsr = ARM_CPU_MODE_USR;
Peter Maydell3a807de2010-11-19 15:36:47 +0000270 /* For user mode we must enable access to coprocessors */
pbrook40f137e2006-02-20 00:33:36 +0000271 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
Peter Maydell3a807de2010-11-19 15:36:47 +0000272 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
273 env->cp15.c15_cpar = 3;
274 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
275 env->cp15.c15_cpar = 1;
276 }
pbrook40f137e2006-02-20 00:33:36 +0000277#else
278 /* SVC mode with interrupts disabled. */
279 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
pbrook9ee6e8b2007-11-11 00:04:49 +0000280 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
Paul Brook983fe822010-04-05 19:34:51 +0100281 clear at reset. Initial SP and PC are loaded from ROM. */
282 if (IS_M(env)) {
283 uint32_t pc;
284 uint8_t *rom;
pbrook9ee6e8b2007-11-11 00:04:49 +0000285 env->uncached_cpsr &= ~CPSR_I;
Paul Brook983fe822010-04-05 19:34:51 +0100286 rom = rom_ptr(0);
287 if (rom) {
288 /* We should really use ldl_phys here, in case the guest
289 modified flash and reset itself. However images
Andreas Färber297d1b42011-10-02 18:35:48 +0200290 loaded via -kernel have not been copied yet, so load the
Paul Brook983fe822010-04-05 19:34:51 +0100291 values directly from there. */
292 env->regs[13] = ldl_p(rom);
293 pc = ldl_p(rom + 4);
294 env->thumb = pc & 1;
295 env->regs[15] = pc & ~1;
296 }
297 }
pbrook40f137e2006-02-20 00:33:36 +0000298 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
pbrookb2fa1792008-10-22 19:22:30 +0000299 env->cp15.c2_base_mask = 0xffffc000u;
Peter Maydell74594c92011-03-22 12:16:16 +0000300 /* v7 performance monitor control register: same implementor
301 * field as main ID register, and we implement no event counters.
302 */
303 env->cp15.c9_pmcr = (id & 0xff000000);
pbrook40f137e2006-02-20 00:33:36 +0000304#endif
Peter Maydell3a492f32011-01-14 20:39:18 +0100305 set_flush_to_zero(1, &env->vfp.standard_fp_status);
306 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
307 set_default_nan_mode(1, &env->vfp.standard_fp_status);
Peter Maydell9df38c42011-04-12 13:56:41 +0100308 set_float_detect_tininess(float_tininess_before_rounding,
309 &env->vfp.fp_status);
310 set_float_detect_tininess(float_tininess_before_rounding,
311 &env->vfp.standard_fp_status);
pbrookf3d6b952007-03-11 13:03:18 +0000312 tlb_flush(env, 1);
pbrook40f137e2006-02-20 00:33:36 +0000313}
314
pbrook56aebc82008-10-11 17:55:29 +0000315static int vfp_gdb_get_reg(CPUState *env, uint8_t *buf, int reg)
316{
317 int nregs;
318
319 /* VFP data registers are always little-endian. */
320 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
321 if (reg < nregs) {
322 stfq_le_p(buf, env->vfp.regs[reg]);
323 return 8;
324 }
325 if (arm_feature(env, ARM_FEATURE_NEON)) {
326 /* Aliases for Q regs. */
327 nregs += 16;
328 if (reg < nregs) {
329 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
330 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
331 return 16;
332 }
333 }
334 switch (reg - nregs) {
335 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
336 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
337 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
338 }
339 return 0;
340}
341
342static int vfp_gdb_set_reg(CPUState *env, uint8_t *buf, int reg)
343{
344 int nregs;
345
346 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
347 if (reg < nregs) {
348 env->vfp.regs[reg] = ldfq_le_p(buf);
349 return 8;
350 }
351 if (arm_feature(env, ARM_FEATURE_NEON)) {
352 nregs += 16;
353 if (reg < nregs) {
354 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
355 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
356 return 16;
357 }
358 }
359 switch (reg - nregs) {
360 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
361 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +0200362 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +0000363 }
364 return 0;
365}
366
bellardaaed9092007-11-10 15:15:54 +0000367CPUARMState *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +0000368{
369 CPUARMState *env;
bellardaaed9092007-11-10 15:15:54 +0000370 uint32_t id;
pbrookb26eefb2008-03-31 03:44:26 +0000371 static int inited = 0;
pbrook40f137e2006-02-20 00:33:36 +0000372
bellardaaed9092007-11-10 15:15:54 +0000373 id = cpu_arm_find_by_name(cpu_model);
374 if (id == 0)
375 return NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -0500376 env = g_malloc0(sizeof(CPUARMState));
pbrook40f137e2006-02-20 00:33:36 +0000377 cpu_exec_init(env);
Peter Maydellf4fc2472011-11-25 19:25:50 +0100378 if (tcg_enabled() && !inited) {
pbrookb26eefb2008-03-31 03:44:26 +0000379 inited = 1;
380 arm_translate_init();
381 }
382
ths01ba9812007-12-09 02:22:57 +0000383 env->cpu_model_str = cpu_model;
bellardaaed9092007-11-10 15:15:54 +0000384 env->cp15.c0_cpuid = id;
pbrook40f137e2006-02-20 00:33:36 +0000385 cpu_reset(env);
pbrook56aebc82008-10-11 17:55:29 +0000386 if (arm_feature(env, ARM_FEATURE_NEON)) {
387 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
388 51, "arm-neon.xml", 0);
389 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
390 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
391 35, "arm-vfp3.xml", 0);
392 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
393 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
394 19, "arm-vfp.xml", 0);
395 }
aliguori0bf46a42009-04-24 18:03:41 +0000396 qemu_init_vcpu(env);
pbrook40f137e2006-02-20 00:33:36 +0000397 return env;
398}
399
pbrook3371d272007-03-08 03:04:12 +0000400struct arm_cpu_t {
401 uint32_t id;
402 const char *name;
403};
404
405static const struct arm_cpu_t arm_cpu_names[] = {
406 { ARM_CPUID_ARM926, "arm926"},
pbrookce819862007-05-08 02:30:40 +0000407 { ARM_CPUID_ARM946, "arm946"},
pbrook3371d272007-03-08 03:04:12 +0000408 { ARM_CPUID_ARM1026, "arm1026"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000409 { ARM_CPUID_ARM1136, "arm1136"},
balrog827df9f2008-04-14 21:05:22 +0000410 { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
Jamie Iles7807eed2011-07-20 10:32:54 +0000411 { ARM_CPUID_ARM1176, "arm1176"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000412 { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
413 { ARM_CPUID_CORTEXM3, "cortex-m3"},
414 { ARM_CPUID_CORTEXA8, "cortex-a8"},
Paul Brook10055562009-11-19 16:45:20 +0000415 { ARM_CPUID_CORTEXA9, "cortex-a9"},
balrogc3d26892007-07-29 17:57:26 +0000416 { ARM_CPUID_TI925T, "ti925t" },
balrogc1713132007-04-30 01:26:42 +0000417 { ARM_CPUID_PXA250, "pxa250" },
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +0400418 { ARM_CPUID_SA1100, "sa1100" },
419 { ARM_CPUID_SA1110, "sa1110" },
balrogc1713132007-04-30 01:26:42 +0000420 { ARM_CPUID_PXA255, "pxa255" },
421 { ARM_CPUID_PXA260, "pxa260" },
422 { ARM_CPUID_PXA261, "pxa261" },
423 { ARM_CPUID_PXA262, "pxa262" },
424 { ARM_CPUID_PXA270, "pxa270" },
425 { ARM_CPUID_PXA270_A0, "pxa270-a0" },
426 { ARM_CPUID_PXA270_A1, "pxa270-a1" },
427 { ARM_CPUID_PXA270_B0, "pxa270-b0" },
428 { ARM_CPUID_PXA270_B1, "pxa270-b1" },
429 { ARM_CPUID_PXA270_C0, "pxa270-c0" },
430 { ARM_CPUID_PXA270_C5, "pxa270-c5" },
pbrook9ee6e8b2007-11-11 00:04:49 +0000431 { ARM_CPUID_ANY, "any"},
pbrook3371d272007-03-08 03:04:12 +0000432 { 0, NULL}
433};
434
Stefan Weil9a78eea2010-10-22 23:03:33 +0200435void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
pbrook5adb4832007-03-08 03:15:18 +0000436{
437 int i;
438
j_mayerc732abe2007-10-12 06:47:46 +0000439 (*cpu_fprintf)(f, "Available CPUs:\n");
pbrook5adb4832007-03-08 03:15:18 +0000440 for (i = 0; arm_cpu_names[i].name; i++) {
j_mayerc732abe2007-10-12 06:47:46 +0000441 (*cpu_fprintf)(f, " %s\n", arm_cpu_names[i].name);
pbrook5adb4832007-03-08 03:15:18 +0000442 }
443}
444
bellardaaed9092007-11-10 15:15:54 +0000445/* return 0 if not found */
446static uint32_t cpu_arm_find_by_name(const char *name)
pbrook40f137e2006-02-20 00:33:36 +0000447{
pbrook3371d272007-03-08 03:04:12 +0000448 int i;
449 uint32_t id;
450
451 id = 0;
pbrook3371d272007-03-08 03:04:12 +0000452 for (i = 0; arm_cpu_names[i].name; i++) {
453 if (strcmp(name, arm_cpu_names[i].name) == 0) {
454 id = arm_cpu_names[i].id;
455 break;
456 }
457 }
bellardaaed9092007-11-10 15:15:54 +0000458 return id;
pbrook40f137e2006-02-20 00:33:36 +0000459}
460
461void cpu_arm_close(CPUARMState *env)
462{
Andreas Färber12b1de32011-10-10 02:52:28 +0000463 g_free(env);
pbrook40f137e2006-02-20 00:33:36 +0000464}
465
Peter Maydell37064a82012-01-05 15:49:06 +0000466static int bad_mode_switch(CPUState *env, int mode)
467{
468 /* Return true if it is not valid for us to switch to
469 * this CPU mode (ie all the UNPREDICTABLE cases in
470 * the ARM ARM CPSRWriteByInstr pseudocode).
471 */
472 switch (mode) {
473 case ARM_CPU_MODE_USR:
474 case ARM_CPU_MODE_SYS:
475 case ARM_CPU_MODE_SVC:
476 case ARM_CPU_MODE_ABT:
477 case ARM_CPU_MODE_UND:
478 case ARM_CPU_MODE_IRQ:
479 case ARM_CPU_MODE_FIQ:
480 return 0;
481 default:
482 return 1;
483 }
484}
485
balrog2f4a40e2007-11-13 01:50:15 +0000486uint32_t cpsr_read(CPUARMState *env)
487{
488 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +0000489 ZF = (env->ZF == 0);
490 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +0000491 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
492 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
493 | ((env->condexec_bits & 0xfc) << 8)
494 | (env->GE << 16);
495}
496
497void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
498{
balrog2f4a40e2007-11-13 01:50:15 +0000499 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +0000500 env->ZF = (~val) & CPSR_Z;
501 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +0000502 env->CF = (val >> 29) & 1;
503 env->VF = (val << 3) & 0x80000000;
504 }
505 if (mask & CPSR_Q)
506 env->QF = ((val & CPSR_Q) != 0);
507 if (mask & CPSR_T)
508 env->thumb = ((val & CPSR_T) != 0);
509 if (mask & CPSR_IT_0_1) {
510 env->condexec_bits &= ~3;
511 env->condexec_bits |= (val >> 25) & 3;
512 }
513 if (mask & CPSR_IT_2_7) {
514 env->condexec_bits &= 3;
515 env->condexec_bits |= (val >> 8) & 0xfc;
516 }
517 if (mask & CPSR_GE) {
518 env->GE = (val >> 16) & 0xf;
519 }
520
521 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
Peter Maydell37064a82012-01-05 15:49:06 +0000522 if (bad_mode_switch(env, val & CPSR_M)) {
523 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
524 * We choose to ignore the attempt and leave the CPSR M field
525 * untouched.
526 */
527 mask &= ~CPSR_M;
528 } else {
529 switch_mode(env, val & CPSR_M);
530 }
balrog2f4a40e2007-11-13 01:50:15 +0000531 }
532 mask &= ~CACHED_CPSR_BITS;
533 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
534}
535
pbrookb26eefb2008-03-31 03:44:26 +0000536/* Sign/zero extend */
537uint32_t HELPER(sxtb16)(uint32_t x)
538{
539 uint32_t res;
540 res = (uint16_t)(int8_t)x;
541 res |= (uint32_t)(int8_t)(x >> 16) << 16;
542 return res;
543}
544
545uint32_t HELPER(uxtb16)(uint32_t x)
546{
547 uint32_t res;
548 res = (uint16_t)(uint8_t)x;
549 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
550 return res;
551}
552
pbrookf51bbbf2008-03-31 03:45:13 +0000553uint32_t HELPER(clz)(uint32_t x)
554{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +0200555 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +0000556}
557
pbrook36706692008-03-31 03:46:19 +0000558int32_t HELPER(sdiv)(int32_t num, int32_t den)
559{
560 if (den == 0)
561 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +0200562 if (num == INT_MIN && den == -1)
563 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +0000564 return num / den;
565}
566
567uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
568{
569 if (den == 0)
570 return 0;
571 return num / den;
572}
573
574uint32_t HELPER(rbit)(uint32_t x)
575{
576 x = ((x & 0xff000000) >> 24)
577 | ((x & 0x00ff0000) >> 8)
578 | ((x & 0x0000ff00) << 8)
579 | ((x & 0x000000ff) << 24);
580 x = ((x & 0xf0f0f0f0) >> 4)
581 | ((x & 0x0f0f0f0f) << 4);
582 x = ((x & 0x88888888) >> 3)
583 | ((x & 0x44444444) >> 1)
584 | ((x & 0x22222222) << 1)
585 | ((x & 0x11111111) << 3);
586 return x;
587}
588
pbrookad694712008-03-31 03:48:30 +0000589uint32_t HELPER(abs)(uint32_t x)
590{
591 return ((int32_t)x < 0) ? -x : x;
592}
593
ths5fafdf22007-09-16 21:08:06 +0000594#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +0000595
596void do_interrupt (CPUState *env)
597{
598 env->exception_index = -1;
599}
600
601int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
Blue Swirl97b348e2011-08-01 16:12:17 +0000602 int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +0000603{
604 if (rw == 2) {
605 env->exception_index = EXCP_PREFETCH_ABORT;
606 env->cp15.c6_insn = address;
607 } else {
608 env->exception_index = EXCP_DATA_ABORT;
609 env->cp15.c6_data = address;
610 }
611 return 1;
612}
613
bellardb5ff1b32005-11-26 10:38:39 +0000614/* These should probably raise undefined insn exceptions. */
pbrook8984bd22008-03-31 03:47:48 +0000615void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +0000616{
617 int op1 = (insn >> 8) & 0xf;
618 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
619 return;
620}
621
pbrook8984bd22008-03-31 03:47:48 +0000622uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +0000623{
624 int op1 = (insn >> 8) & 0xf;
625 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
626 return 0;
627}
628
pbrook8984bd22008-03-31 03:47:48 +0000629void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +0000630{
631 cpu_abort(env, "cp15 insn %08x\n", insn);
632}
633
pbrook8984bd22008-03-31 03:47:48 +0000634uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +0000635{
636 cpu_abort(env, "cp15 insn %08x\n", insn);
bellardb5ff1b32005-11-26 10:38:39 +0000637}
638
pbrook9ee6e8b2007-11-11 00:04:49 +0000639/* These should probably raise undefined insn exceptions. */
pbrook8984bd22008-03-31 03:47:48 +0000640void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000641{
642 cpu_abort(env, "v7m_mrs %d\n", reg);
643}
644
pbrook8984bd22008-03-31 03:47:48 +0000645uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +0000646{
647 cpu_abort(env, "v7m_mrs %d\n", reg);
648 return 0;
649}
650
bellardb5ff1b32005-11-26 10:38:39 +0000651void switch_mode(CPUState *env, int mode)
652{
653 if (mode != ARM_CPU_MODE_USR)
654 cpu_abort(env, "Tried to switch out of user mode\n");
655}
656
pbrookb0109802008-03-31 03:47:03 +0000657void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000658{
659 cpu_abort(env, "banked r13 write\n");
660}
661
pbrookb0109802008-03-31 03:47:03 +0000662uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +0000663{
664 cpu_abort(env, "banked r13 read\n");
665 return 0;
666}
667
bellardb5ff1b32005-11-26 10:38:39 +0000668#else
669
pbrook8e716212007-01-20 17:12:09 +0000670extern int semihosting_enabled;
671
bellardb5ff1b32005-11-26 10:38:39 +0000672/* Map CPU modes onto saved register banks. */
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000673static inline int bank_number(CPUState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +0000674{
675 switch (mode) {
676 case ARM_CPU_MODE_USR:
677 case ARM_CPU_MODE_SYS:
678 return 0;
679 case ARM_CPU_MODE_SVC:
680 return 1;
681 case ARM_CPU_MODE_ABT:
682 return 2;
683 case ARM_CPU_MODE_UND:
684 return 3;
685 case ARM_CPU_MODE_IRQ:
686 return 4;
687 case ARM_CPU_MODE_FIQ:
688 return 5;
689 }
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000690 cpu_abort(env, "Bad mode %x\n", mode);
bellardb5ff1b32005-11-26 10:38:39 +0000691 return -1;
692}
693
694void switch_mode(CPUState *env, int mode)
695{
696 int old_mode;
697 int i;
698
699 old_mode = env->uncached_cpsr & CPSR_M;
700 if (mode == old_mode)
701 return;
702
703 if (old_mode == ARM_CPU_MODE_FIQ) {
704 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000705 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000706 } else if (mode == ARM_CPU_MODE_FIQ) {
707 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000708 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000709 }
710
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000711 i = bank_number(env, old_mode);
bellardb5ff1b32005-11-26 10:38:39 +0000712 env->banked_r13[i] = env->regs[13];
713 env->banked_r14[i] = env->regs[14];
714 env->banked_spsr[i] = env->spsr;
715
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000716 i = bank_number(env, mode);
bellardb5ff1b32005-11-26 10:38:39 +0000717 env->regs[13] = env->banked_r13[i];
718 env->regs[14] = env->banked_r14[i];
719 env->spsr = env->banked_spsr[i];
720}
721
pbrook9ee6e8b2007-11-11 00:04:49 +0000722static void v7m_push(CPUARMState *env, uint32_t val)
723{
724 env->regs[13] -= 4;
725 stl_phys(env->regs[13], val);
726}
727
728static uint32_t v7m_pop(CPUARMState *env)
729{
730 uint32_t val;
731 val = ldl_phys(env->regs[13]);
732 env->regs[13] += 4;
733 return val;
734}
735
736/* Switch to V7M main or process stack pointer. */
737static void switch_v7m_sp(CPUARMState *env, int process)
738{
739 uint32_t tmp;
740 if (env->v7m.current_sp != process) {
741 tmp = env->v7m.other_sp;
742 env->v7m.other_sp = env->regs[13];
743 env->regs[13] = tmp;
744 env->v7m.current_sp = process;
745 }
746}
747
748static void do_v7m_exception_exit(CPUARMState *env)
749{
750 uint32_t type;
751 uint32_t xpsr;
752
753 type = env->regs[15];
754 if (env->v7m.exception != 0)
Paul Brook983fe822010-04-05 19:34:51 +0100755 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
pbrook9ee6e8b2007-11-11 00:04:49 +0000756
757 /* Switch to the target stack. */
758 switch_v7m_sp(env, (type & 4) != 0);
759 /* Pop registers. */
760 env->regs[0] = v7m_pop(env);
761 env->regs[1] = v7m_pop(env);
762 env->regs[2] = v7m_pop(env);
763 env->regs[3] = v7m_pop(env);
764 env->regs[12] = v7m_pop(env);
765 env->regs[14] = v7m_pop(env);
766 env->regs[15] = v7m_pop(env);
767 xpsr = v7m_pop(env);
768 xpsr_write(env, xpsr, 0xfffffdff);
769 /* Undo stack alignment. */
770 if (xpsr & 0x200)
771 env->regs[13] |= 4;
772 /* ??? The exception return type specifies Thread/Handler mode. However
773 this is also implied by the xPSR value. Not sure what to do
774 if there is a mismatch. */
775 /* ??? Likewise for mismatches between the CONTROL register and the stack
776 pointer. */
777}
778
aurel322b3ea312009-03-07 21:48:00 +0000779static void do_interrupt_v7m(CPUARMState *env)
pbrook9ee6e8b2007-11-11 00:04:49 +0000780{
781 uint32_t xpsr = xpsr_read(env);
782 uint32_t lr;
783 uint32_t addr;
784
785 lr = 0xfffffff1;
786 if (env->v7m.current_sp)
787 lr |= 4;
788 if (env->v7m.exception == 0)
789 lr |= 8;
790
791 /* For exceptions we just mark as pending on the NVIC, and let that
792 handle it. */
793 /* TODO: Need to escalate if the current priority is higher than the
794 one we're raising. */
795 switch (env->exception_index) {
796 case EXCP_UDEF:
Paul Brook983fe822010-04-05 19:34:51 +0100797 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000798 return;
799 case EXCP_SWI:
800 env->regs[15] += 2;
Paul Brook983fe822010-04-05 19:34:51 +0100801 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
pbrook9ee6e8b2007-11-11 00:04:49 +0000802 return;
803 case EXCP_PREFETCH_ABORT:
804 case EXCP_DATA_ABORT:
Paul Brook983fe822010-04-05 19:34:51 +0100805 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
pbrook9ee6e8b2007-11-11 00:04:49 +0000806 return;
807 case EXCP_BKPT:
pbrook2ad207d2007-11-24 23:22:11 +0000808 if (semihosting_enabled) {
809 int nr;
810 nr = lduw_code(env->regs[15]) & 0xff;
811 if (nr == 0xab) {
812 env->regs[15] += 2;
813 env->regs[0] = do_arm_semihosting(env);
814 return;
815 }
816 }
Paul Brook983fe822010-04-05 19:34:51 +0100817 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
pbrook9ee6e8b2007-11-11 00:04:49 +0000818 return;
819 case EXCP_IRQ:
Paul Brook983fe822010-04-05 19:34:51 +0100820 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
pbrook9ee6e8b2007-11-11 00:04:49 +0000821 break;
822 case EXCP_EXCEPTION_EXIT:
823 do_v7m_exception_exit(env);
824 return;
825 default:
826 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
827 return; /* Never happens. Keep compiler happy. */
828 }
829
830 /* Align stack pointer. */
831 /* ??? Should only do this if Configuration Control Register
832 STACKALIGN bit is set. */
833 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +0000834 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +0000835 xpsr |= 0x200;
836 }
balrog6c956762008-04-13 00:57:49 +0000837 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000838 v7m_push(env, xpsr);
839 v7m_push(env, env->regs[15]);
840 v7m_push(env, env->regs[14]);
841 v7m_push(env, env->regs[12]);
842 v7m_push(env, env->regs[3]);
843 v7m_push(env, env->regs[2]);
844 v7m_push(env, env->regs[1]);
845 v7m_push(env, env->regs[0]);
846 switch_v7m_sp(env, 0);
847 env->uncached_cpsr &= ~CPSR_IT;
848 env->regs[14] = lr;
849 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
850 env->regs[15] = addr & 0xfffffffe;
851 env->thumb = addr & 1;
852}
853
bellardb5ff1b32005-11-26 10:38:39 +0000854/* Handle a CPU exception. */
855void do_interrupt(CPUARMState *env)
856{
857 uint32_t addr;
858 uint32_t mask;
859 int new_mode;
860 uint32_t offset;
861
pbrook9ee6e8b2007-11-11 00:04:49 +0000862 if (IS_M(env)) {
863 do_interrupt_v7m(env);
864 return;
865 }
bellardb5ff1b32005-11-26 10:38:39 +0000866 /* TODO: Vectored interrupt controller. */
867 switch (env->exception_index) {
868 case EXCP_UDEF:
869 new_mode = ARM_CPU_MODE_UND;
870 addr = 0x04;
871 mask = CPSR_I;
872 if (env->thumb)
873 offset = 2;
874 else
875 offset = 4;
876 break;
877 case EXCP_SWI:
pbrook8e716212007-01-20 17:12:09 +0000878 if (semihosting_enabled) {
879 /* Check for semihosting interrupt. */
880 if (env->thumb) {
881 mask = lduw_code(env->regs[15] - 2) & 0xff;
882 } else {
883 mask = ldl_code(env->regs[15] - 4) & 0xffffff;
884 }
885 /* Only intercept calls from privileged modes, to provide some
886 semblance of security. */
887 if (((mask == 0x123456 && !env->thumb)
888 || (mask == 0xab && env->thumb))
889 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
890 env->regs[0] = do_arm_semihosting(env);
891 return;
892 }
893 }
bellardb5ff1b32005-11-26 10:38:39 +0000894 new_mode = ARM_CPU_MODE_SVC;
895 addr = 0x08;
896 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +0000897 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +0000898 offset = 0;
899 break;
pbrook06c949e2006-02-04 19:35:26 +0000900 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +0000901 /* See if this is a semihosting syscall. */
pbrook2ad207d2007-11-24 23:22:11 +0000902 if (env->thumb && semihosting_enabled) {
pbrook9ee6e8b2007-11-11 00:04:49 +0000903 mask = lduw_code(env->regs[15]) & 0xff;
904 if (mask == 0xab
905 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
906 env->regs[15] += 2;
907 env->regs[0] = do_arm_semihosting(env);
908 return;
909 }
910 }
Alex Zuepke81c05da2011-06-03 18:42:17 +0200911 env->cp15.c5_insn = 2;
pbrook9ee6e8b2007-11-11 00:04:49 +0000912 /* Fall through to prefetch abort. */
913 case EXCP_PREFETCH_ABORT:
bellardb5ff1b32005-11-26 10:38:39 +0000914 new_mode = ARM_CPU_MODE_ABT;
915 addr = 0x0c;
916 mask = CPSR_A | CPSR_I;
917 offset = 4;
918 break;
919 case EXCP_DATA_ABORT:
920 new_mode = ARM_CPU_MODE_ABT;
921 addr = 0x10;
922 mask = CPSR_A | CPSR_I;
923 offset = 8;
924 break;
925 case EXCP_IRQ:
926 new_mode = ARM_CPU_MODE_IRQ;
927 addr = 0x18;
928 /* Disable IRQ and imprecise data aborts. */
929 mask = CPSR_A | CPSR_I;
930 offset = 4;
931 break;
932 case EXCP_FIQ:
933 new_mode = ARM_CPU_MODE_FIQ;
934 addr = 0x1c;
935 /* Disable FIQ, IRQ and imprecise data aborts. */
936 mask = CPSR_A | CPSR_I | CPSR_F;
937 offset = 4;
938 break;
939 default:
940 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
941 return; /* Never happens. Keep compiler happy. */
942 }
943 /* High vectors. */
944 if (env->cp15.c1_sys & (1 << 13)) {
945 addr += 0xffff0000;
946 }
947 switch_mode (env, new_mode);
948 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +0000949 /* Clear IT bits. */
950 env->condexec_bits = 0;
Rabin Vincent30a8cac2010-02-15 00:02:36 +0530951 /* Switch to the new mode, and to the correct instruction set. */
bellard6d7e6322005-12-18 16:54:08 +0000952 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
bellardb5ff1b32005-11-26 10:38:39 +0000953 env->uncached_cpsr |= mask;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400954 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
955 * and we should just guard the thumb mode on V4 */
956 if (arm_feature(env, ARM_FEATURE_V4T)) {
957 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
958 }
bellardb5ff1b32005-11-26 10:38:39 +0000959 env->regs[14] = env->regs[15] + offset;
960 env->regs[15] = addr;
961 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
962}
963
964/* Check section/page access permissions.
965 Returns the page protection flags, or zero if the access is not
966 permitted. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000967static inline int check_ap(CPUState *env, int ap, int domain_prot,
968 int access_type, int is_user)
bellardb5ff1b32005-11-26 10:38:39 +0000969{
pbrook9ee6e8b2007-11-11 00:04:49 +0000970 int prot_ro;
971
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000972 if (domain_prot == 3) {
bellardb5ff1b32005-11-26 10:38:39 +0000973 return PAGE_READ | PAGE_WRITE;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000974 }
bellardb5ff1b32005-11-26 10:38:39 +0000975
pbrook9ee6e8b2007-11-11 00:04:49 +0000976 if (access_type == 1)
977 prot_ro = 0;
978 else
979 prot_ro = PAGE_READ;
980
bellardb5ff1b32005-11-26 10:38:39 +0000981 switch (ap) {
982 case 0:
pbrook78600322006-09-09 14:36:26 +0000983 if (access_type == 1)
bellardb5ff1b32005-11-26 10:38:39 +0000984 return 0;
985 switch ((env->cp15.c1_sys >> 8) & 3) {
986 case 1:
987 return is_user ? 0 : PAGE_READ;
988 case 2:
989 return PAGE_READ;
990 default:
991 return 0;
992 }
993 case 1:
994 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
995 case 2:
996 if (is_user)
pbrook9ee6e8b2007-11-11 00:04:49 +0000997 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +0000998 else
999 return PAGE_READ | PAGE_WRITE;
1000 case 3:
1001 return PAGE_READ | PAGE_WRITE;
pbrookd4934d12008-12-19 12:39:00 +00001002 case 4: /* Reserved. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001003 return 0;
1004 case 5:
1005 return is_user ? 0 : prot_ro;
1006 case 6:
1007 return prot_ro;
pbrookd4934d12008-12-19 12:39:00 +00001008 case 7:
Jamie Iles0ab06d82011-06-23 01:12:59 +00001009 if (!arm_feature (env, ARM_FEATURE_V6K))
pbrookd4934d12008-12-19 12:39:00 +00001010 return 0;
1011 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +00001012 default:
1013 abort();
1014 }
1015}
1016
pbrookb2fa1792008-10-22 19:22:30 +00001017static uint32_t get_level1_table_address(CPUState *env, uint32_t address)
1018{
1019 uint32_t table;
1020
1021 if (address & env->cp15.c2_mask)
1022 table = env->cp15.c2_base1 & 0xffffc000;
1023 else
1024 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1025
1026 table |= (address >> 18) & 0x3ffc;
1027 return table;
1028}
1029
pbrook9ee6e8b2007-11-11 00:04:49 +00001030static int get_phys_addr_v5(CPUState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +00001031 int is_user, uint32_t *phys_ptr, int *prot,
1032 target_ulong *page_size)
bellardb5ff1b32005-11-26 10:38:39 +00001033{
1034 int code;
1035 uint32_t table;
1036 uint32_t desc;
1037 int type;
1038 int ap;
1039 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001040 int domain_prot;
bellardb5ff1b32005-11-26 10:38:39 +00001041 uint32_t phys_addr;
1042
pbrook9ee6e8b2007-11-11 00:04:49 +00001043 /* Pagetable walk. */
1044 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001045 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001046 desc = ldl_phys(table);
1047 type = (desc & 3);
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001048 domain = (desc >> 5) & 0x0f;
1049 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
pbrook9ee6e8b2007-11-11 00:04:49 +00001050 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001051 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001052 code = 5;
1053 goto do_fault;
1054 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001055 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001056 if (type == 2)
1057 code = 9; /* Section domain fault. */
1058 else
1059 code = 11; /* Page domain fault. */
1060 goto do_fault;
1061 }
1062 if (type == 2) {
1063 /* 1Mb section. */
1064 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1065 ap = (desc >> 10) & 3;
1066 code = 13;
Paul Brookd4c430a2010-03-17 02:14:28 +00001067 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +00001068 } else {
1069 /* Lookup l2 entry. */
1070 if (type == 1) {
1071 /* Coarse pagetable. */
1072 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1073 } else {
1074 /* Fine pagetable. */
1075 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1076 }
1077 desc = ldl_phys(table);
1078 switch (desc & 3) {
1079 case 0: /* Page translation fault. */
1080 code = 7;
1081 goto do_fault;
1082 case 1: /* 64k page. */
1083 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1084 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001085 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001086 break;
1087 case 2: /* 4k page. */
1088 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1089 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001090 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001091 break;
1092 case 3: /* 1k page. */
1093 if (type == 1) {
1094 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1095 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1096 } else {
1097 /* Page translation fault. */
1098 code = 7;
1099 goto do_fault;
1100 }
1101 } else {
1102 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1103 }
1104 ap = (desc >> 4) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001105 *page_size = 0x400;
pbrook9ee6e8b2007-11-11 00:04:49 +00001106 break;
1107 default:
1108 /* Never happens, but compiler isn't smart enough to tell. */
1109 abort();
1110 }
1111 code = 15;
1112 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001113 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
pbrook9ee6e8b2007-11-11 00:04:49 +00001114 if (!*prot) {
1115 /* Access permission fault. */
1116 goto do_fault;
1117 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301118 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001119 *phys_ptr = phys_addr;
1120 return 0;
1121do_fault:
1122 return code | (domain << 4);
1123}
1124
1125static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +00001126 int is_user, uint32_t *phys_ptr, int *prot,
1127 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001128{
1129 int code;
1130 uint32_t table;
1131 uint32_t desc;
1132 uint32_t xn;
1133 int type;
1134 int ap;
1135 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001136 int domain_prot;
pbrook9ee6e8b2007-11-11 00:04:49 +00001137 uint32_t phys_addr;
1138
1139 /* Pagetable walk. */
1140 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001141 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001142 desc = ldl_phys(table);
1143 type = (desc & 3);
1144 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001145 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001146 code = 5;
1147 domain = 0;
1148 goto do_fault;
1149 } else if (type == 2 && (desc & (1 << 18))) {
1150 /* Supersection. */
1151 domain = 0;
1152 } else {
1153 /* Section or page. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001154 domain = (desc >> 5) & 0x0f;
pbrook9ee6e8b2007-11-11 00:04:49 +00001155 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001156 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1157 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001158 if (type == 2)
1159 code = 9; /* Section domain fault. */
1160 else
1161 code = 11; /* Page domain fault. */
1162 goto do_fault;
1163 }
1164 if (type == 2) {
1165 if (desc & (1 << 18)) {
1166 /* Supersection. */
1167 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001168 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001169 } else {
1170 /* Section. */
1171 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001172 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001173 }
1174 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1175 xn = desc & (1 << 4);
1176 code = 13;
1177 } else {
1178 /* Lookup l2 entry. */
1179 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1180 desc = ldl_phys(table);
1181 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1182 switch (desc & 3) {
1183 case 0: /* Page translation fault. */
1184 code = 7;
1185 goto do_fault;
1186 case 1: /* 64k page. */
1187 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1188 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +00001189 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001190 break;
1191 case 2: case 3: /* 4k page. */
1192 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1193 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +00001194 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001195 break;
1196 default:
1197 /* Never happens, but compiler isn't smart enough to tell. */
1198 abort();
1199 }
1200 code = 15;
1201 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001202 if (domain_prot == 3) {
Juha Riihimäkic0034322010-12-08 13:15:16 +02001203 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1204 } else {
1205 if (xn && access_type == 2)
1206 goto do_fault;
pbrook9ee6e8b2007-11-11 00:04:49 +00001207
Juha Riihimäkic0034322010-12-08 13:15:16 +02001208 /* The simplified model uses AP[0] as an access control bit. */
1209 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1210 /* Access flag fault. */
1211 code = (code == 15) ? 6 : 3;
1212 goto do_fault;
1213 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001214 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
Juha Riihimäkic0034322010-12-08 13:15:16 +02001215 if (!*prot) {
1216 /* Access permission fault. */
1217 goto do_fault;
1218 }
1219 if (!xn) {
1220 *prot |= PAGE_EXEC;
1221 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301222 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001223 *phys_ptr = phys_addr;
1224 return 0;
1225do_fault:
1226 return code | (domain << 4);
1227}
1228
1229static int get_phys_addr_mpu(CPUState *env, uint32_t address, int access_type,
1230 int is_user, uint32_t *phys_ptr, int *prot)
1231{
1232 int n;
1233 uint32_t mask;
1234 uint32_t base;
1235
1236 *phys_ptr = address;
1237 for (n = 7; n >= 0; n--) {
1238 base = env->cp15.c6_region[n];
1239 if ((base & 1) == 0)
1240 continue;
1241 mask = 1 << ((base >> 1) & 0x1f);
1242 /* Keep this shift separate from the above to avoid an
1243 (undefined) << 32. */
1244 mask = (mask << 1) - 1;
1245 if (((base ^ address) & ~mask) == 0)
1246 break;
1247 }
1248 if (n < 0)
1249 return 2;
1250
1251 if (access_type == 2) {
1252 mask = env->cp15.c5_insn;
1253 } else {
1254 mask = env->cp15.c5_data;
1255 }
1256 mask = (mask >> (n * 4)) & 0xf;
1257 switch (mask) {
1258 case 0:
1259 return 1;
1260 case 1:
1261 if (is_user)
1262 return 1;
1263 *prot = PAGE_READ | PAGE_WRITE;
1264 break;
1265 case 2:
1266 *prot = PAGE_READ;
1267 if (!is_user)
1268 *prot |= PAGE_WRITE;
1269 break;
1270 case 3:
1271 *prot = PAGE_READ | PAGE_WRITE;
1272 break;
1273 case 5:
1274 if (is_user)
1275 return 1;
1276 *prot = PAGE_READ;
1277 break;
1278 case 6:
1279 *prot = PAGE_READ;
1280 break;
1281 default:
1282 /* Bad permission. */
1283 return 1;
1284 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301285 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001286 return 0;
1287}
1288
1289static inline int get_phys_addr(CPUState *env, uint32_t address,
1290 int access_type, int is_user,
Paul Brookd4c430a2010-03-17 02:14:28 +00001291 uint32_t *phys_ptr, int *prot,
1292 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001293{
bellardb5ff1b32005-11-26 10:38:39 +00001294 /* Fast Context Switch Extension. */
1295 if (address < 0x02000000)
1296 address += env->cp15.c13_fcse;
1297
1298 if ((env->cp15.c1_sys & 1) == 0) {
pbrookce819862007-05-08 02:30:40 +00001299 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00001300 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301301 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +00001302 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001303 return 0;
pbrookce819862007-05-08 02:30:40 +00001304 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
Paul Brookd4c430a2010-03-17 02:14:28 +00001305 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001306 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1307 prot);
1308 } else if (env->cp15.c1_sys & (1 << 23)) {
1309 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001310 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001311 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001312 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001313 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001314 }
bellardb5ff1b32005-11-26 10:38:39 +00001315}
1316
1317int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
Blue Swirl97b348e2011-08-01 16:12:17 +00001318 int access_type, int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00001319{
1320 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001321 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001322 int prot;
j_mayer6ebbf392007-10-14 07:07:08 +00001323 int ret, is_user;
bellardb5ff1b32005-11-26 10:38:39 +00001324
j_mayer6ebbf392007-10-14 07:07:08 +00001325 is_user = mmu_idx == MMU_USER_IDX;
Paul Brookd4c430a2010-03-17 02:14:28 +00001326 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1327 &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001328 if (ret == 0) {
1329 /* Map a single [sub]page. */
1330 phys_addr &= ~(uint32_t)0x3ff;
1331 address &= ~(uint32_t)0x3ff;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301332 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
Paul Brookd4c430a2010-03-17 02:14:28 +00001333 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00001334 }
1335
1336 if (access_type == 2) {
1337 env->cp15.c5_insn = ret;
1338 env->cp15.c6_insn = address;
1339 env->exception_index = EXCP_PREFETCH_ABORT;
1340 } else {
1341 env->cp15.c5_data = ret;
pbrook9ee6e8b2007-11-11 00:04:49 +00001342 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1343 env->cp15.c5_data |= (1 << 11);
bellardb5ff1b32005-11-26 10:38:39 +00001344 env->cp15.c6_data = address;
1345 env->exception_index = EXCP_DATA_ABORT;
1346 }
1347 return 1;
1348}
1349
Anthony Liguoric227f092009-10-01 16:12:16 -05001350target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
bellardb5ff1b32005-11-26 10:38:39 +00001351{
1352 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001353 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001354 int prot;
1355 int ret;
1356
Paul Brookd4c430a2010-03-17 02:14:28 +00001357 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001358
1359 if (ret != 0)
1360 return -1;
1361
1362 return phys_addr;
1363}
1364
pbrook8984bd22008-03-31 03:47:48 +00001365void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +00001366{
1367 int cp_num = (insn >> 8) & 0xf;
1368 int cp_info = (insn >> 5) & 7;
1369 int src = (insn >> 16) & 0xf;
1370 int operand = insn & 0xf;
1371
1372 if (env->cp[cp_num].cp_write)
1373 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1374 cp_info, src, operand, val);
1375}
1376
pbrook8984bd22008-03-31 03:47:48 +00001377uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +00001378{
1379 int cp_num = (insn >> 8) & 0xf;
1380 int cp_info = (insn >> 5) & 7;
1381 int dest = (insn >> 16) & 0xf;
1382 int operand = insn & 0xf;
1383
1384 if (env->cp[cp_num].cp_read)
1385 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1386 cp_info, dest, operand);
1387 return 0;
1388}
1389
pbrookce819862007-05-08 02:30:40 +00001390/* Return basic MPU access permission bits. */
1391static uint32_t simple_mpu_ap_bits(uint32_t val)
1392{
1393 uint32_t ret;
1394 uint32_t mask;
1395 int i;
1396 ret = 0;
1397 mask = 3;
1398 for (i = 0; i < 16; i += 2) {
1399 ret |= (val >> i) & mask;
1400 mask <<= 2;
1401 }
1402 return ret;
1403}
1404
1405/* Pad basic MPU access permission bits to extended format. */
1406static uint32_t extended_mpu_ap_bits(uint32_t val)
1407{
1408 uint32_t ret;
1409 uint32_t mask;
1410 int i;
1411 ret = 0;
1412 mask = 3;
1413 for (i = 0; i < 16; i += 2) {
1414 ret |= (val & mask) << i;
1415 mask <<= 2;
1416 }
1417 return ret;
1418}
1419
pbrook8984bd22008-03-31 03:47:48 +00001420void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +00001421{
pbrook9ee6e8b2007-11-11 00:04:49 +00001422 int op1;
1423 int op2;
1424 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001425
pbrook9ee6e8b2007-11-11 00:04:49 +00001426 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001427 op2 = (insn >> 5) & 7;
pbrookce819862007-05-08 02:30:40 +00001428 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001429 switch ((insn >> 16) & 0xf) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001430 case 0:
pbrook9ee6e8b2007-11-11 00:04:49 +00001431 /* ID codes. */
balrog610c3c82007-06-24 12:09:48 +00001432 if (arm_feature(env, ARM_FEATURE_XSCALE))
1433 break;
balrogc3d26892007-07-29 17:57:26 +00001434 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1435 break;
pbrooka49ea272008-12-19 13:37:53 +00001436 if (arm_feature(env, ARM_FEATURE_V7)
1437 && op1 == 2 && crm == 0 && op2 == 0) {
1438 env->cp15.c0_cssel = val & 0xf;
1439 break;
1440 }
bellardb5ff1b32005-11-26 10:38:39 +00001441 goto bad_reg;
1442 case 1: /* System configuration. */
balrogc3d26892007-07-29 17:57:26 +00001443 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1444 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001445 switch (op2) {
1446 case 0:
pbrookce819862007-05-08 02:30:40 +00001447 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
balrogc1713132007-04-30 01:26:42 +00001448 env->cp15.c1_sys = val;
bellardb5ff1b32005-11-26 10:38:39 +00001449 /* ??? Lots of these bits are not implemented. */
1450 /* This may enable/disable the MMU, so do a TLB flush. */
1451 tlb_flush(env, 1);
1452 break;
Stefan Weil61cc8702011-04-13 22:45:22 +02001453 case 1: /* Auxiliary control register. */
balrog610c3c82007-06-24 12:09:48 +00001454 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1455 env->cp15.c1_xscaleauxcr = val;
balrogc1713132007-04-30 01:26:42 +00001456 break;
balrog610c3c82007-06-24 12:09:48 +00001457 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001458 /* Not implemented. */
1459 break;
bellardb5ff1b32005-11-26 10:38:39 +00001460 case 2:
balrog610c3c82007-06-24 12:09:48 +00001461 if (arm_feature(env, ARM_FEATURE_XSCALE))
1462 goto bad_reg;
pbrook4be27db2008-10-22 16:14:08 +00001463 if (env->cp15.c1_coproc != val) {
1464 env->cp15.c1_coproc = val;
1465 /* ??? Is this safe when called from within a TB? */
1466 tb_flush(env);
1467 }
balrogc1713132007-04-30 01:26:42 +00001468 break;
bellardb5ff1b32005-11-26 10:38:39 +00001469 default:
1470 goto bad_reg;
1471 }
1472 break;
pbrookce819862007-05-08 02:30:40 +00001473 case 2: /* MMU Page table control / MPU cache control. */
1474 if (arm_feature(env, ARM_FEATURE_MPU)) {
1475 switch (op2) {
1476 case 0:
1477 env->cp15.c2_data = val;
1478 break;
1479 case 1:
1480 env->cp15.c2_insn = val;
1481 break;
1482 default:
1483 goto bad_reg;
1484 }
1485 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001486 switch (op2) {
1487 case 0:
1488 env->cp15.c2_base0 = val;
1489 break;
1490 case 1:
1491 env->cp15.c2_base1 = val;
1492 break;
1493 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001494 val &= 7;
1495 env->cp15.c2_control = val;
pbrook9ee6e8b2007-11-11 00:04:49 +00001496 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
pbrookb2fa1792008-10-22 19:22:30 +00001497 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
pbrook9ee6e8b2007-11-11 00:04:49 +00001498 break;
1499 default:
1500 goto bad_reg;
1501 }
pbrookce819862007-05-08 02:30:40 +00001502 }
bellardb5ff1b32005-11-26 10:38:39 +00001503 break;
pbrookce819862007-05-08 02:30:40 +00001504 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001505 env->cp15.c3 = val;
balrog405ee3a2007-10-31 00:47:13 +00001506 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
bellardb5ff1b32005-11-26 10:38:39 +00001507 break;
1508 case 4: /* Reserved. */
1509 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001510 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001511 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1512 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001513 switch (op2) {
1514 case 0:
pbrookce819862007-05-08 02:30:40 +00001515 if (arm_feature(env, ARM_FEATURE_MPU))
1516 val = extended_mpu_ap_bits(val);
bellardb5ff1b32005-11-26 10:38:39 +00001517 env->cp15.c5_data = val;
1518 break;
1519 case 1:
pbrookce819862007-05-08 02:30:40 +00001520 if (arm_feature(env, ARM_FEATURE_MPU))
1521 val = extended_mpu_ap_bits(val);
1522 env->cp15.c5_insn = val;
1523 break;
1524 case 2:
1525 if (!arm_feature(env, ARM_FEATURE_MPU))
1526 goto bad_reg;
1527 env->cp15.c5_data = val;
1528 break;
1529 case 3:
1530 if (!arm_feature(env, ARM_FEATURE_MPU))
1531 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001532 env->cp15.c5_insn = val;
1533 break;
1534 default:
1535 goto bad_reg;
1536 }
1537 break;
pbrookce819862007-05-08 02:30:40 +00001538 case 6: /* MMU Fault address / MPU base/size. */
1539 if (arm_feature(env, ARM_FEATURE_MPU)) {
1540 if (crm >= 8)
1541 goto bad_reg;
1542 env->cp15.c6_region[crm] = val;
1543 } else {
balrogc3d26892007-07-29 17:57:26 +00001544 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1545 op2 = 0;
pbrookce819862007-05-08 02:30:40 +00001546 switch (op2) {
1547 case 0:
1548 env->cp15.c6_data = val;
1549 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001550 case 1: /* ??? This is WFAR on armv6 */
1551 case 2:
pbrookce819862007-05-08 02:30:40 +00001552 env->cp15.c6_insn = val;
1553 break;
1554 default:
1555 goto bad_reg;
1556 }
bellardb5ff1b32005-11-26 10:38:39 +00001557 }
1558 break;
1559 case 7: /* Cache control. */
balrogc3d26892007-07-29 17:57:26 +00001560 env->cp15.c15_i_max = 0x000;
1561 env->cp15.c15_i_min = 0xff0;
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001562 if (op1 != 0) {
1563 goto bad_reg;
1564 }
1565 /* No cache, so nothing to do except VA->PA translations. */
Peter Maydell906879a2011-07-20 10:32:55 +00001566 if (arm_feature(env, ARM_FEATURE_VAPA)) {
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001567 switch (crm) {
1568 case 4:
1569 if (arm_feature(env, ARM_FEATURE_V7)) {
1570 env->cp15.c7_par = val & 0xfffff6ff;
1571 } else {
1572 env->cp15.c7_par = val & 0xfffff1ff;
1573 }
1574 break;
1575 case 8: {
1576 uint32_t phys_addr;
1577 target_ulong page_size;
1578 int prot;
1579 int ret, is_user = op2 & 2;
1580 int access_type = op2 & 1;
1581
1582 if (op2 & 4) {
1583 /* Other states are only available with TrustZone */
1584 goto bad_reg;
1585 }
1586 ret = get_phys_addr(env, val, access_type, is_user,
1587 &phys_addr, &prot, &page_size);
1588 if (ret == 0) {
1589 /* We do not set any attribute bits in the PAR */
1590 if (page_size == (1 << 24)
1591 && arm_feature(env, ARM_FEATURE_V7)) {
1592 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1593 } else {
1594 env->cp15.c7_par = phys_addr & 0xfffff000;
1595 }
1596 } else {
1597 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1598 ((ret & (12 << 1)) >> 6) |
1599 ((ret & 0xf) << 1) | 1;
1600 }
1601 break;
1602 }
1603 }
1604 }
bellardb5ff1b32005-11-26 10:38:39 +00001605 break;
1606 case 8: /* MMU TLB control. */
1607 switch (op2) {
1608 case 0: /* Invalidate all. */
1609 tlb_flush(env, 0);
1610 break;
1611 case 1: /* Invalidate single TLB entry. */
Paul Brookd4c430a2010-03-17 02:14:28 +00001612 tlb_flush_page(env, val & TARGET_PAGE_MASK);
bellardb5ff1b32005-11-26 10:38:39 +00001613 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001614 case 2: /* Invalidate on ASID. */
1615 tlb_flush(env, val == 0);
1616 break;
1617 case 3: /* Invalidate single entry on MVA. */
1618 /* ??? This is like case 1, but ignores ASID. */
1619 tlb_flush(env, 1);
1620 break;
bellardb5ff1b32005-11-26 10:38:39 +00001621 default:
1622 goto bad_reg;
1623 }
1624 break;
pbrookce819862007-05-08 02:30:40 +00001625 case 9:
balrogc3d26892007-07-29 17:57:26 +00001626 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1627 break;
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +04001628 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1629 break; /* Ignore ReadBuffer access */
pbrookce819862007-05-08 02:30:40 +00001630 switch (crm) {
1631 case 0: /* Cache lockdown. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001632 switch (op1) {
1633 case 0: /* L1 cache. */
1634 switch (op2) {
1635 case 0:
1636 env->cp15.c9_data = val;
1637 break;
1638 case 1:
1639 env->cp15.c9_insn = val;
1640 break;
1641 default:
1642 goto bad_reg;
1643 }
1644 break;
1645 case 1: /* L2 cache. */
1646 /* Ignore writes to L2 lockdown/auxiliary registers. */
1647 break;
1648 default:
1649 goto bad_reg;
1650 }
1651 break;
pbrookce819862007-05-08 02:30:40 +00001652 case 1: /* TCM memory region registers. */
1653 /* Not implemented. */
1654 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00001655 case 12: /* Performance monitor control */
1656 /* Performance monitors are implementation defined in v7,
1657 * but with an ARM recommended set of registers, which we
1658 * follow (although we don't actually implement any counters)
1659 */
1660 if (!arm_feature(env, ARM_FEATURE_V7)) {
1661 goto bad_reg;
1662 }
1663 switch (op2) {
1664 case 0: /* performance monitor control register */
1665 /* only the DP, X, D and E bits are writable */
1666 env->cp15.c9_pmcr &= ~0x39;
1667 env->cp15.c9_pmcr |= (val & 0x39);
1668 break;
1669 case 1: /* Count enable set register */
1670 val &= (1 << 31);
1671 env->cp15.c9_pmcnten |= val;
1672 break;
1673 case 2: /* Count enable clear */
1674 val &= (1 << 31);
1675 env->cp15.c9_pmcnten &= ~val;
1676 break;
1677 case 3: /* Overflow flag status */
1678 env->cp15.c9_pmovsr &= ~val;
1679 break;
1680 case 4: /* Software increment */
1681 /* RAZ/WI since we don't implement the software-count event */
1682 break;
1683 case 5: /* Event counter selection register */
1684 /* Since we don't implement any events, writing to this register
1685 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1686 */
1687 break;
1688 default:
1689 goto bad_reg;
1690 }
1691 break;
1692 case 13: /* Performance counters */
1693 if (!arm_feature(env, ARM_FEATURE_V7)) {
1694 goto bad_reg;
1695 }
1696 switch (op2) {
1697 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1698 break;
1699 case 1: /* Event type select */
1700 env->cp15.c9_pmxevtyper = val & 0xff;
1701 break;
1702 case 2: /* Event count register */
1703 /* Unimplemented (we have no events), RAZ/WI */
1704 break;
1705 default:
1706 goto bad_reg;
1707 }
1708 break;
1709 case 14: /* Performance monitor control */
1710 if (!arm_feature(env, ARM_FEATURE_V7)) {
1711 goto bad_reg;
1712 }
1713 switch (op2) {
1714 case 0: /* user enable */
1715 env->cp15.c9_pmuserenr = val & 1;
1716 /* changes access rights for cp registers, so flush tbs */
1717 tb_flush(env);
1718 break;
1719 case 1: /* interrupt enable set */
1720 /* We have no event counters so only the C bit can be changed */
1721 val &= (1 << 31);
1722 env->cp15.c9_pminten |= val;
1723 break;
1724 case 2: /* interrupt enable clear */
1725 val &= (1 << 31);
1726 env->cp15.c9_pminten &= ~val;
1727 break;
1728 }
1729 break;
bellardb5ff1b32005-11-26 10:38:39 +00001730 default:
1731 goto bad_reg;
1732 }
1733 break;
1734 case 10: /* MMU TLB lockdown. */
1735 /* ??? TLB lockdown not implemented. */
1736 break;
bellardb5ff1b32005-11-26 10:38:39 +00001737 case 12: /* Reserved. */
1738 goto bad_reg;
1739 case 13: /* Process ID. */
1740 switch (op2) {
1741 case 0:
pbrookd07edbf2006-07-21 22:39:57 +00001742 /* Unlike real hardware the qemu TLB uses virtual addresses,
1743 not modified virtual addresses, so this causes a TLB flush.
1744 */
1745 if (env->cp15.c13_fcse != val)
1746 tlb_flush(env, 1);
1747 env->cp15.c13_fcse = val;
bellardb5ff1b32005-11-26 10:38:39 +00001748 break;
1749 case 1:
pbrookd07edbf2006-07-21 22:39:57 +00001750 /* This changes the ASID, so do a TLB flush. */
pbrookce819862007-05-08 02:30:40 +00001751 if (env->cp15.c13_context != val
1752 && !arm_feature(env, ARM_FEATURE_MPU))
pbrookd07edbf2006-07-21 22:39:57 +00001753 tlb_flush(env, 0);
1754 env->cp15.c13_context = val;
bellardb5ff1b32005-11-26 10:38:39 +00001755 break;
1756 default:
1757 goto bad_reg;
1758 }
1759 break;
1760 case 14: /* Reserved. */
1761 goto bad_reg;
1762 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00001763 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
pbrookce819862007-05-08 02:30:40 +00001764 if (op2 == 0 && crm == 1) {
balrog2e232132007-08-01 02:31:54 +00001765 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1766 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1767 tb_flush(env);
1768 env->cp15.c15_cpar = val & 0x3fff;
1769 }
balrogc1713132007-04-30 01:26:42 +00001770 break;
1771 }
1772 goto bad_reg;
1773 }
balrogc3d26892007-07-29 17:57:26 +00001774 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1775 switch (crm) {
1776 case 0:
1777 break;
1778 case 1: /* Set TI925T configuration. */
1779 env->cp15.c15_ticonfig = val & 0xe7;
1780 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1781 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1782 break;
1783 case 2: /* Set I_max. */
1784 env->cp15.c15_i_max = val;
1785 break;
1786 case 3: /* Set I_min. */
1787 env->cp15.c15_i_min = val;
1788 break;
1789 case 4: /* Set thread-ID. */
1790 env->cp15.c15_threadid = val & 0xffff;
1791 break;
1792 case 8: /* Wait-for-interrupt (deprecated). */
1793 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1794 break;
1795 default:
1796 goto bad_reg;
1797 }
1798 }
bellardb5ff1b32005-11-26 10:38:39 +00001799 break;
1800 }
1801 return;
1802bad_reg:
1803 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001804 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1805 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00001806}
1807
pbrook8984bd22008-03-31 03:47:48 +00001808uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +00001809{
pbrook9ee6e8b2007-11-11 00:04:49 +00001810 int op1;
1811 int op2;
1812 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001813
pbrook9ee6e8b2007-11-11 00:04:49 +00001814 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001815 op2 = (insn >> 5) & 7;
balrogc3d26892007-07-29 17:57:26 +00001816 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001817 switch ((insn >> 16) & 0xf) {
1818 case 0: /* ID codes. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001819 switch (op1) {
1820 case 0:
1821 switch (crm) {
1822 case 0:
1823 switch (op2) {
1824 case 0: /* Device ID. */
1825 return env->cp15.c0_cpuid;
1826 case 1: /* Cache Type. */
1827 return env->cp15.c0_cachetype;
1828 case 2: /* TCM status. */
1829 return 0;
1830 case 3: /* TLB type register. */
1831 return 0; /* No lockable TLB entries. */
Peter Maydell607b4b02011-02-03 19:43:23 +00001832 case 5: /* MPIDR */
1833 /* The MPIDR was standardised in v7; prior to
1834 * this it was implemented only in the 11MPCore.
1835 * For all other pre-v7 cores it does not exist.
1836 */
1837 if (arm_feature(env, ARM_FEATURE_V7) ||
1838 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1839 int mpidr = env->cpu_index;
1840 /* We don't support setting cluster ID ([8..11])
1841 * so these bits always RAZ.
1842 */
1843 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1844 mpidr |= (1 << 31);
1845 /* Cores which are uniprocessor (non-coherent)
1846 * but still implement the MP extensions set
1847 * bit 30. (For instance, A9UP.) However we do
1848 * not currently model any of those cores.
1849 */
1850 }
1851 return mpidr;
Paul Brook10055562009-11-19 16:45:20 +00001852 }
Peter Maydell607b4b02011-02-03 19:43:23 +00001853 /* otherwise fall through to the unimplemented-reg case */
pbrook9ee6e8b2007-11-11 00:04:49 +00001854 default:
1855 goto bad_reg;
1856 }
1857 case 1:
1858 if (!arm_feature(env, ARM_FEATURE_V6))
1859 goto bad_reg;
1860 return env->cp15.c0_c1[op2];
1861 case 2:
1862 if (!arm_feature(env, ARM_FEATURE_V6))
1863 goto bad_reg;
1864 return env->cp15.c0_c2[op2];
1865 case 3: case 4: case 5: case 6: case 7:
1866 return 0;
1867 default:
1868 goto bad_reg;
1869 }
1870 case 1:
1871 /* These registers aren't documented on arm11 cores. However
1872 Linux looks at them anyway. */
1873 if (!arm_feature(env, ARM_FEATURE_V6))
1874 goto bad_reg;
1875 if (crm != 0)
1876 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001877 if (!arm_feature(env, ARM_FEATURE_V7))
1878 return 0;
1879
1880 switch (op2) {
1881 case 0:
1882 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1883 case 1:
1884 return env->cp15.c0_clid;
1885 case 7:
1886 return 0;
1887 }
1888 goto bad_reg;
1889 case 2:
1890 if (op2 != 0 || crm != 0)
balrog610c3c82007-06-24 12:09:48 +00001891 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001892 return env->cp15.c0_cssel;
pbrook9ee6e8b2007-11-11 00:04:49 +00001893 default:
1894 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001895 }
1896 case 1: /* System configuration. */
balrogc3d26892007-07-29 17:57:26 +00001897 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1898 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001899 switch (op2) {
1900 case 0: /* Control register. */
1901 return env->cp15.c1_sys;
1902 case 1: /* Auxiliary control register. */
balrogc1713132007-04-30 01:26:42 +00001903 if (arm_feature(env, ARM_FEATURE_XSCALE))
balrog610c3c82007-06-24 12:09:48 +00001904 return env->cp15.c1_xscaleauxcr;
pbrook9ee6e8b2007-11-11 00:04:49 +00001905 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1906 goto bad_reg;
1907 switch (ARM_CPUID(env)) {
1908 case ARM_CPUID_ARM1026:
1909 return 1;
1910 case ARM_CPUID_ARM1136:
balrog827df9f2008-04-14 21:05:22 +00001911 case ARM_CPUID_ARM1136_R2:
Jamie Iles7807eed2011-07-20 10:32:54 +00001912 case ARM_CPUID_ARM1176:
pbrook9ee6e8b2007-11-11 00:04:49 +00001913 return 7;
1914 case ARM_CPUID_ARM11MPCORE:
1915 return 1;
1916 case ARM_CPUID_CORTEXA8:
aurel32533d1772009-03-07 22:10:28 +00001917 return 2;
Paul Brook10055562009-11-19 16:45:20 +00001918 case ARM_CPUID_CORTEXA9:
1919 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001920 default:
1921 goto bad_reg;
1922 }
bellardb5ff1b32005-11-26 10:38:39 +00001923 case 2: /* Coprocessor access register. */
balrog610c3c82007-06-24 12:09:48 +00001924 if (arm_feature(env, ARM_FEATURE_XSCALE))
1925 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001926 return env->cp15.c1_coproc;
1927 default:
1928 goto bad_reg;
1929 }
pbrookce819862007-05-08 02:30:40 +00001930 case 2: /* MMU Page table control / MPU cache control. */
1931 if (arm_feature(env, ARM_FEATURE_MPU)) {
1932 switch (op2) {
1933 case 0:
1934 return env->cp15.c2_data;
1935 break;
1936 case 1:
1937 return env->cp15.c2_insn;
1938 break;
1939 default:
1940 goto bad_reg;
1941 }
1942 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001943 switch (op2) {
1944 case 0:
1945 return env->cp15.c2_base0;
1946 case 1:
1947 return env->cp15.c2_base1;
1948 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001949 return env->cp15.c2_control;
pbrook9ee6e8b2007-11-11 00:04:49 +00001950 default:
1951 goto bad_reg;
1952 }
1953 }
pbrookce819862007-05-08 02:30:40 +00001954 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001955 return env->cp15.c3;
1956 case 4: /* Reserved. */
1957 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001958 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001959 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1960 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001961 switch (op2) {
1962 case 0:
pbrookce819862007-05-08 02:30:40 +00001963 if (arm_feature(env, ARM_FEATURE_MPU))
1964 return simple_mpu_ap_bits(env->cp15.c5_data);
bellardb5ff1b32005-11-26 10:38:39 +00001965 return env->cp15.c5_data;
1966 case 1:
pbrookce819862007-05-08 02:30:40 +00001967 if (arm_feature(env, ARM_FEATURE_MPU))
1968 return simple_mpu_ap_bits(env->cp15.c5_data);
1969 return env->cp15.c5_insn;
1970 case 2:
1971 if (!arm_feature(env, ARM_FEATURE_MPU))
1972 goto bad_reg;
1973 return env->cp15.c5_data;
1974 case 3:
1975 if (!arm_feature(env, ARM_FEATURE_MPU))
1976 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001977 return env->cp15.c5_insn;
1978 default:
1979 goto bad_reg;
1980 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001981 case 6: /* MMU Fault address. */
pbrookce819862007-05-08 02:30:40 +00001982 if (arm_feature(env, ARM_FEATURE_MPU)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001983 if (crm >= 8)
pbrookce819862007-05-08 02:30:40 +00001984 goto bad_reg;
pbrook9ee6e8b2007-11-11 00:04:49 +00001985 return env->cp15.c6_region[crm];
pbrookce819862007-05-08 02:30:40 +00001986 } else {
balrogc3d26892007-07-29 17:57:26 +00001987 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1988 op2 = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001989 switch (op2) {
1990 case 0:
1991 return env->cp15.c6_data;
1992 case 1:
1993 if (arm_feature(env, ARM_FEATURE_V6)) {
1994 /* Watchpoint Fault Adrress. */
1995 return 0; /* Not implemented. */
1996 } else {
1997 /* Instruction Fault Adrress. */
1998 /* Arm9 doesn't have an IFAR, but implementing it anyway
1999 shouldn't do any harm. */
2000 return env->cp15.c6_insn;
2001 }
2002 case 2:
2003 if (arm_feature(env, ARM_FEATURE_V6)) {
2004 /* Instruction Fault Adrress. */
2005 return env->cp15.c6_insn;
2006 } else {
2007 goto bad_reg;
2008 }
2009 default:
2010 goto bad_reg;
2011 }
bellardb5ff1b32005-11-26 10:38:39 +00002012 }
2013 case 7: /* Cache control. */
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01002014 if (crm == 4 && op1 == 0 && op2 == 0) {
2015 return env->cp15.c7_par;
2016 }
pbrook6fbe23d2008-04-01 17:19:11 +00002017 /* FIXME: Should only clear Z flag if destination is r15. */
2018 env->ZF = 0;
bellardb5ff1b32005-11-26 10:38:39 +00002019 return 0;
2020 case 8: /* MMU TLB control. */
2021 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00002022 case 9:
2023 switch (crm) {
2024 case 0: /* Cache lockdown */
2025 switch (op1) {
2026 case 0: /* L1 cache. */
2027 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2028 return 0;
2029 }
2030 switch (op2) {
2031 case 0:
2032 return env->cp15.c9_data;
2033 case 1:
2034 return env->cp15.c9_insn;
2035 default:
2036 goto bad_reg;
2037 }
2038 case 1: /* L2 cache */
2039 if (crm != 0) {
2040 goto bad_reg;
2041 }
2042 /* L2 Lockdown and Auxiliary control. */
2043 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002044 default:
2045 goto bad_reg;
2046 }
Peter Maydell74594c92011-03-22 12:16:16 +00002047 break;
2048 case 12: /* Performance monitor control */
2049 if (!arm_feature(env, ARM_FEATURE_V7)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002050 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00002051 }
2052 switch (op2) {
2053 case 0: /* performance monitor control register */
2054 return env->cp15.c9_pmcr;
2055 case 1: /* count enable set */
2056 case 2: /* count enable clear */
2057 return env->cp15.c9_pmcnten;
2058 case 3: /* overflow flag status */
2059 return env->cp15.c9_pmovsr;
2060 case 4: /* software increment */
2061 case 5: /* event counter selection register */
2062 return 0; /* Unimplemented, RAZ/WI */
2063 default:
2064 goto bad_reg;
2065 }
2066 case 13: /* Performance counters */
2067 if (!arm_feature(env, ARM_FEATURE_V7)) {
2068 goto bad_reg;
2069 }
2070 switch (op2) {
2071 case 1: /* Event type select */
2072 return env->cp15.c9_pmxevtyper;
2073 case 0: /* Cycle count register */
2074 case 2: /* Event count register */
2075 /* Unimplemented, so RAZ/WI */
2076 return 0;
2077 default:
2078 goto bad_reg;
2079 }
2080 case 14: /* Performance monitor control */
2081 if (!arm_feature(env, ARM_FEATURE_V7)) {
2082 goto bad_reg;
2083 }
2084 switch (op2) {
2085 case 0: /* user enable */
2086 return env->cp15.c9_pmuserenr;
2087 case 1: /* interrupt enable set */
2088 case 2: /* interrupt enable clear */
2089 return env->cp15.c9_pminten;
2090 default:
2091 goto bad_reg;
2092 }
bellardb5ff1b32005-11-26 10:38:39 +00002093 default:
2094 goto bad_reg;
2095 }
Peter Maydell74594c92011-03-22 12:16:16 +00002096 break;
bellardb5ff1b32005-11-26 10:38:39 +00002097 case 10: /* MMU TLB lockdown. */
2098 /* ??? TLB lockdown not implemented. */
2099 return 0;
2100 case 11: /* TCM DMA control. */
2101 case 12: /* Reserved. */
2102 goto bad_reg;
2103 case 13: /* Process ID. */
2104 switch (op2) {
2105 case 0:
2106 return env->cp15.c13_fcse;
2107 case 1:
2108 return env->cp15.c13_context;
2109 default:
2110 goto bad_reg;
2111 }
2112 case 14: /* Reserved. */
2113 goto bad_reg;
2114 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00002115 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
balrogc3d26892007-07-29 17:57:26 +00002116 if (op2 == 0 && crm == 1)
balrogc1713132007-04-30 01:26:42 +00002117 return env->cp15.c15_cpar;
2118
2119 goto bad_reg;
2120 }
balrogc3d26892007-07-29 17:57:26 +00002121 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2122 switch (crm) {
2123 case 0:
2124 return 0;
2125 case 1: /* Read TI925T configuration. */
2126 return env->cp15.c15_ticonfig;
2127 case 2: /* Read I_max. */
2128 return env->cp15.c15_i_max;
2129 case 3: /* Read I_min. */
2130 return env->cp15.c15_i_min;
2131 case 4: /* Read thread-ID. */
2132 return env->cp15.c15_threadid;
2133 case 8: /* TI925T_status */
2134 return 0;
2135 }
balrog827df9f2008-04-14 21:05:22 +00002136 /* TODO: Peripheral port remap register:
2137 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2138 * controller base address at $rn & ~0xfff and map size of
2139 * 0x200 << ($rn & 0xfff), when MMU is off. */
balrogc3d26892007-07-29 17:57:26 +00002140 goto bad_reg;
2141 }
bellardb5ff1b32005-11-26 10:38:39 +00002142 return 0;
2143 }
2144bad_reg:
2145 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002146 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2147 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00002148 return 0;
2149}
2150
pbrookb0109802008-03-31 03:47:03 +00002151void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002152{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002153 if ((env->uncached_cpsr & CPSR_M) == mode) {
2154 env->regs[13] = val;
2155 } else {
Peter Maydell1b9e01c2012-01-05 15:49:06 +00002156 env->banked_r13[bank_number(env, mode)] = val;
Peter Maydell39ea3d42011-01-14 20:39:18 +01002157 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002158}
2159
pbrookb0109802008-03-31 03:47:03 +00002160uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00002161{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002162 if ((env->uncached_cpsr & CPSR_M) == mode) {
2163 return env->regs[13];
2164 } else {
Peter Maydell1b9e01c2012-01-05 15:49:06 +00002165 return env->banked_r13[bank_number(env, mode)];
Peter Maydell39ea3d42011-01-14 20:39:18 +01002166 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002167}
2168
pbrook8984bd22008-03-31 03:47:48 +00002169uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00002170{
2171 switch (reg) {
2172 case 0: /* APSR */
2173 return xpsr_read(env) & 0xf8000000;
2174 case 1: /* IAPSR */
2175 return xpsr_read(env) & 0xf80001ff;
2176 case 2: /* EAPSR */
2177 return xpsr_read(env) & 0xff00fc00;
2178 case 3: /* xPSR */
2179 return xpsr_read(env) & 0xff00fdff;
2180 case 5: /* IPSR */
2181 return xpsr_read(env) & 0x000001ff;
2182 case 6: /* EPSR */
2183 return xpsr_read(env) & 0x0700fc00;
2184 case 7: /* IEPSR */
2185 return xpsr_read(env) & 0x0700edff;
2186 case 8: /* MSP */
2187 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2188 case 9: /* PSP */
2189 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2190 case 16: /* PRIMASK */
2191 return (env->uncached_cpsr & CPSR_I) != 0;
Sebastian Huber82845822011-05-29 02:58:41 +00002192 case 17: /* BASEPRI */
2193 case 18: /* BASEPRI_MAX */
pbrook9ee6e8b2007-11-11 00:04:49 +00002194 return env->v7m.basepri;
Sebastian Huber82845822011-05-29 02:58:41 +00002195 case 19: /* FAULTMASK */
2196 return (env->uncached_cpsr & CPSR_F) != 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002197 case 20: /* CONTROL */
2198 return env->v7m.control;
2199 default:
2200 /* ??? For debugging only. */
2201 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2202 return 0;
2203 }
2204}
2205
pbrook8984bd22008-03-31 03:47:48 +00002206void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002207{
2208 switch (reg) {
2209 case 0: /* APSR */
2210 xpsr_write(env, val, 0xf8000000);
2211 break;
2212 case 1: /* IAPSR */
2213 xpsr_write(env, val, 0xf8000000);
2214 break;
2215 case 2: /* EAPSR */
2216 xpsr_write(env, val, 0xfe00fc00);
2217 break;
2218 case 3: /* xPSR */
2219 xpsr_write(env, val, 0xfe00fc00);
2220 break;
2221 case 5: /* IPSR */
2222 /* IPSR bits are readonly. */
2223 break;
2224 case 6: /* EPSR */
2225 xpsr_write(env, val, 0x0600fc00);
2226 break;
2227 case 7: /* IEPSR */
2228 xpsr_write(env, val, 0x0600fc00);
2229 break;
2230 case 8: /* MSP */
2231 if (env->v7m.current_sp)
2232 env->v7m.other_sp = val;
2233 else
2234 env->regs[13] = val;
2235 break;
2236 case 9: /* PSP */
2237 if (env->v7m.current_sp)
2238 env->regs[13] = val;
2239 else
2240 env->v7m.other_sp = val;
2241 break;
2242 case 16: /* PRIMASK */
2243 if (val & 1)
2244 env->uncached_cpsr |= CPSR_I;
2245 else
2246 env->uncached_cpsr &= ~CPSR_I;
2247 break;
Sebastian Huber82845822011-05-29 02:58:41 +00002248 case 17: /* BASEPRI */
2249 env->v7m.basepri = val & 0xff;
2250 break;
2251 case 18: /* BASEPRI_MAX */
2252 val &= 0xff;
2253 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2254 env->v7m.basepri = val;
2255 break;
2256 case 19: /* FAULTMASK */
pbrook9ee6e8b2007-11-11 00:04:49 +00002257 if (val & 1)
2258 env->uncached_cpsr |= CPSR_F;
2259 else
2260 env->uncached_cpsr &= ~CPSR_F;
2261 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00002262 case 20: /* CONTROL */
2263 env->v7m.control = val & 3;
2264 switch_v7m_sp(env, (val & 2) != 0);
2265 break;
2266 default:
2267 /* ??? For debugging only. */
2268 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2269 return;
2270 }
2271}
2272
balrogc1713132007-04-30 01:26:42 +00002273void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
2274 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
2275 void *opaque)
2276{
2277 if (cpnum < 0 || cpnum > 14) {
2278 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2279 return;
2280 }
2281
2282 env->cp[cpnum].cp_read = cp_read;
2283 env->cp[cpnum].cp_write = cp_write;
2284 env->cp[cpnum].opaque = opaque;
2285}
2286
bellardb5ff1b32005-11-26 10:38:39 +00002287#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00002288
2289/* Note that signed overflow is undefined in C. The following routines are
2290 careful to use unsigned types where modulo arithmetic is required.
2291 Failure to do so _will_ break on newer gcc. */
2292
2293/* Signed saturating arithmetic. */
2294
aurel321654b2d2008-04-11 04:55:07 +00002295/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002296static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2297{
2298 uint16_t res;
2299
2300 res = a + b;
2301 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2302 if (a & 0x8000)
2303 res = 0x8000;
2304 else
2305 res = 0x7fff;
2306 }
2307 return res;
2308}
2309
aurel321654b2d2008-04-11 04:55:07 +00002310/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002311static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2312{
2313 uint8_t res;
2314
2315 res = a + b;
2316 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2317 if (a & 0x80)
2318 res = 0x80;
2319 else
2320 res = 0x7f;
2321 }
2322 return res;
2323}
2324
aurel321654b2d2008-04-11 04:55:07 +00002325/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002326static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2327{
2328 uint16_t res;
2329
2330 res = a - b;
2331 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2332 if (a & 0x8000)
2333 res = 0x8000;
2334 else
2335 res = 0x7fff;
2336 }
2337 return res;
2338}
2339
aurel321654b2d2008-04-11 04:55:07 +00002340/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002341static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2342{
2343 uint8_t res;
2344
2345 res = a - b;
2346 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2347 if (a & 0x80)
2348 res = 0x80;
2349 else
2350 res = 0x7f;
2351 }
2352 return res;
2353}
2354
2355#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2356#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2357#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2358#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2359#define PFX q
2360
2361#include "op_addsub.h"
2362
2363/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00002364static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002365{
2366 uint16_t res;
2367 res = a + b;
2368 if (res < a)
2369 res = 0xffff;
2370 return res;
2371}
2372
pbrook460a09c2008-05-01 12:04:35 +00002373static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002374{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002375 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002376 return a - b;
2377 else
2378 return 0;
2379}
2380
2381static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2382{
2383 uint8_t res;
2384 res = a + b;
2385 if (res < a)
2386 res = 0xff;
2387 return res;
2388}
2389
2390static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2391{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002392 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002393 return a - b;
2394 else
2395 return 0;
2396}
2397
2398#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2399#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2400#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2401#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2402#define PFX uq
2403
2404#include "op_addsub.h"
2405
2406/* Signed modulo arithmetic. */
2407#define SARITH16(a, b, n, op) do { \
2408 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002409 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002410 RESULT(sum, n, 16); \
2411 if (sum >= 0) \
2412 ge |= 3 << (n * 2); \
2413 } while(0)
2414
2415#define SARITH8(a, b, n, op) do { \
2416 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002417 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002418 RESULT(sum, n, 8); \
2419 if (sum >= 0) \
2420 ge |= 1 << n; \
2421 } while(0)
2422
2423
2424#define ADD16(a, b, n) SARITH16(a, b, n, +)
2425#define SUB16(a, b, n) SARITH16(a, b, n, -)
2426#define ADD8(a, b, n) SARITH8(a, b, n, +)
2427#define SUB8(a, b, n) SARITH8(a, b, n, -)
2428#define PFX s
2429#define ARITH_GE
2430
2431#include "op_addsub.h"
2432
2433/* Unsigned modulo arithmetic. */
2434#define ADD16(a, b, n) do { \
2435 uint32_t sum; \
2436 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2437 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00002438 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00002439 ge |= 3 << (n * 2); \
2440 } while(0)
2441
2442#define ADD8(a, b, n) do { \
2443 uint32_t sum; \
2444 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2445 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00002446 if ((sum >> 8) == 1) \
2447 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002448 } while(0)
2449
2450#define SUB16(a, b, n) do { \
2451 uint32_t sum; \
2452 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2453 RESULT(sum, n, 16); \
2454 if ((sum >> 16) == 0) \
2455 ge |= 3 << (n * 2); \
2456 } while(0)
2457
2458#define SUB8(a, b, n) do { \
2459 uint32_t sum; \
2460 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2461 RESULT(sum, n, 8); \
2462 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00002463 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002464 } while(0)
2465
2466#define PFX u
2467#define ARITH_GE
2468
2469#include "op_addsub.h"
2470
2471/* Halved signed arithmetic. */
2472#define ADD16(a, b, n) \
2473 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2474#define SUB16(a, b, n) \
2475 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2476#define ADD8(a, b, n) \
2477 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2478#define SUB8(a, b, n) \
2479 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2480#define PFX sh
2481
2482#include "op_addsub.h"
2483
2484/* Halved unsigned arithmetic. */
2485#define ADD16(a, b, n) \
2486 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2487#define SUB16(a, b, n) \
2488 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2489#define ADD8(a, b, n) \
2490 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2491#define SUB8(a, b, n) \
2492 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2493#define PFX uh
2494
2495#include "op_addsub.h"
2496
2497static inline uint8_t do_usad(uint8_t a, uint8_t b)
2498{
2499 if (a > b)
2500 return a - b;
2501 else
2502 return b - a;
2503}
2504
2505/* Unsigned sum of absolute byte differences. */
2506uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2507{
2508 uint32_t sum;
2509 sum = do_usad(a, b);
2510 sum += do_usad(a >> 8, b >> 8);
2511 sum += do_usad(a >> 16, b >>16);
2512 sum += do_usad(a >> 24, b >> 24);
2513 return sum;
2514}
2515
2516/* For ARMv6 SEL instruction. */
2517uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2518{
2519 uint32_t mask;
2520
2521 mask = 0;
2522 if (flags & 1)
2523 mask |= 0xff;
2524 if (flags & 2)
2525 mask |= 0xff00;
2526 if (flags & 4)
2527 mask |= 0xff0000;
2528 if (flags & 8)
2529 mask |= 0xff000000;
2530 return (a & mask) | (b & ~mask);
2531}
2532
pbrook5e3f8782008-03-31 03:47:34 +00002533uint32_t HELPER(logicq_cc)(uint64_t val)
2534{
2535 return (val >> 32) | (val != 0);
2536}
pbrook4373f3c2008-03-31 03:47:19 +00002537
2538/* VFP support. We follow the convention used for VFP instrunctions:
2539 Single precition routines have a "s" suffix, double precision a
2540 "d" suffix. */
2541
2542/* Convert host exception flags to vfp form. */
2543static inline int vfp_exceptbits_from_host(int host_bits)
2544{
2545 int target_bits = 0;
2546
2547 if (host_bits & float_flag_invalid)
2548 target_bits |= 1;
2549 if (host_bits & float_flag_divbyzero)
2550 target_bits |= 2;
2551 if (host_bits & float_flag_overflow)
2552 target_bits |= 4;
Peter Maydell36802b62011-05-19 14:46:18 +01002553 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
pbrook4373f3c2008-03-31 03:47:19 +00002554 target_bits |= 8;
2555 if (host_bits & float_flag_inexact)
2556 target_bits |= 0x10;
Peter Maydellcecd8502011-01-06 19:37:55 +00002557 if (host_bits & float_flag_input_denormal)
2558 target_bits |= 0x80;
pbrook4373f3c2008-03-31 03:47:19 +00002559 return target_bits;
2560}
2561
2562uint32_t HELPER(vfp_get_fpscr)(CPUState *env)
2563{
2564 int i;
2565 uint32_t fpscr;
2566
2567 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2568 | (env->vfp.vec_len << 16)
2569 | (env->vfp.vec_stride << 20);
2570 i = get_float_exception_flags(&env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002571 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002572 fpscr |= vfp_exceptbits_from_host(i);
2573 return fpscr;
2574}
2575
Peter Maydell01653292010-11-24 15:20:04 +00002576uint32_t vfp_get_fpscr(CPUState *env)
2577{
2578 return HELPER(vfp_get_fpscr)(env);
2579}
2580
pbrook4373f3c2008-03-31 03:47:19 +00002581/* Convert vfp exception flags to target form. */
2582static inline int vfp_exceptbits_to_host(int target_bits)
2583{
2584 int host_bits = 0;
2585
2586 if (target_bits & 1)
2587 host_bits |= float_flag_invalid;
2588 if (target_bits & 2)
2589 host_bits |= float_flag_divbyzero;
2590 if (target_bits & 4)
2591 host_bits |= float_flag_overflow;
2592 if (target_bits & 8)
2593 host_bits |= float_flag_underflow;
2594 if (target_bits & 0x10)
2595 host_bits |= float_flag_inexact;
Peter Maydellcecd8502011-01-06 19:37:55 +00002596 if (target_bits & 0x80)
2597 host_bits |= float_flag_input_denormal;
pbrook4373f3c2008-03-31 03:47:19 +00002598 return host_bits;
2599}
2600
2601void HELPER(vfp_set_fpscr)(CPUState *env, uint32_t val)
2602{
2603 int i;
2604 uint32_t changed;
2605
2606 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2607 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2608 env->vfp.vec_len = (val >> 16) & 7;
2609 env->vfp.vec_stride = (val >> 20) & 3;
2610
2611 changed ^= val;
2612 if (changed & (3 << 22)) {
2613 i = (val >> 22) & 3;
2614 switch (i) {
2615 case 0:
2616 i = float_round_nearest_even;
2617 break;
2618 case 1:
2619 i = float_round_up;
2620 break;
2621 case 2:
2622 i = float_round_down;
2623 break;
2624 case 3:
2625 i = float_round_to_zero;
2626 break;
2627 }
2628 set_float_rounding_mode(i, &env->vfp.fp_status);
2629 }
Peter Maydellcecd8502011-01-06 19:37:55 +00002630 if (changed & (1 << 24)) {
pbrookfe76d972008-12-19 14:33:59 +00002631 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
Peter Maydellcecd8502011-01-06 19:37:55 +00002632 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2633 }
pbrook5c7908e2008-12-19 13:53:37 +00002634 if (changed & (1 << 25))
2635 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002636
Peter Maydellb12c3902011-01-06 19:37:54 +00002637 i = vfp_exceptbits_to_host(val);
pbrook4373f3c2008-03-31 03:47:19 +00002638 set_float_exception_flags(i, &env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002639 set_float_exception_flags(0, &env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002640}
2641
Peter Maydell01653292010-11-24 15:20:04 +00002642void vfp_set_fpscr(CPUState *env, uint32_t val)
2643{
2644 HELPER(vfp_set_fpscr)(env, val);
2645}
2646
pbrook4373f3c2008-03-31 03:47:19 +00002647#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2648
2649#define VFP_BINOP(name) \
Peter Maydellae1857e2011-05-25 14:51:48 +00002650float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002651{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002652 float_status *fpst = fpstp; \
2653 return float32_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002654} \
Peter Maydellae1857e2011-05-25 14:51:48 +00002655float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002656{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002657 float_status *fpst = fpstp; \
2658 return float64_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002659}
2660VFP_BINOP(add)
2661VFP_BINOP(sub)
2662VFP_BINOP(mul)
2663VFP_BINOP(div)
2664#undef VFP_BINOP
2665
2666float32 VFP_HELPER(neg, s)(float32 a)
2667{
2668 return float32_chs(a);
2669}
2670
2671float64 VFP_HELPER(neg, d)(float64 a)
2672{
balrog66230e02008-04-20 00:58:01 +00002673 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002674}
2675
2676float32 VFP_HELPER(abs, s)(float32 a)
2677{
2678 return float32_abs(a);
2679}
2680
2681float64 VFP_HELPER(abs, d)(float64 a)
2682{
balrog66230e02008-04-20 00:58:01 +00002683 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002684}
2685
2686float32 VFP_HELPER(sqrt, s)(float32 a, CPUState *env)
2687{
2688 return float32_sqrt(a, &env->vfp.fp_status);
2689}
2690
2691float64 VFP_HELPER(sqrt, d)(float64 a, CPUState *env)
2692{
2693 return float64_sqrt(a, &env->vfp.fp_status);
2694}
2695
2696/* XXX: check quiet/signaling case */
2697#define DO_VFP_cmp(p, type) \
2698void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2699{ \
2700 uint32_t flags; \
2701 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2702 case 0: flags = 0x6; break; \
2703 case -1: flags = 0x8; break; \
2704 case 1: flags = 0x2; break; \
2705 default: case 2: flags = 0x3; break; \
2706 } \
2707 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2708 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2709} \
2710void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2711{ \
2712 uint32_t flags; \
2713 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2714 case 0: flags = 0x6; break; \
2715 case -1: flags = 0x8; break; \
2716 case 1: flags = 0x2; break; \
2717 default: case 2: flags = 0x3; break; \
2718 } \
2719 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2720 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2721}
2722DO_VFP_cmp(s, float32)
2723DO_VFP_cmp(d, float64)
2724#undef DO_VFP_cmp
2725
Peter Maydell5500b062011-05-19 14:46:19 +01002726/* Integer to float and float to integer conversions */
2727
2728#define CONV_ITOF(name, fsz, sign) \
2729 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2730{ \
2731 float_status *fpst = fpstp; \
2732 return sign##int32_to_##float##fsz(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002733}
2734
Peter Maydell5500b062011-05-19 14:46:19 +01002735#define CONV_FTOI(name, fsz, sign, round) \
2736uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2737{ \
2738 float_status *fpst = fpstp; \
2739 if (float##fsz##_is_any_nan(x)) { \
2740 float_raise(float_flag_invalid, fpst); \
2741 return 0; \
2742 } \
2743 return float##fsz##_to_##sign##int32##round(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002744}
2745
Peter Maydell5500b062011-05-19 14:46:19 +01002746#define FLOAT_CONVS(name, p, fsz, sign) \
2747CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2748CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2749CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
pbrook4373f3c2008-03-31 03:47:19 +00002750
Peter Maydell5500b062011-05-19 14:46:19 +01002751FLOAT_CONVS(si, s, 32, )
2752FLOAT_CONVS(si, d, 64, )
2753FLOAT_CONVS(ui, s, 32, u)
2754FLOAT_CONVS(ui, d, 64, u)
pbrook4373f3c2008-03-31 03:47:19 +00002755
Peter Maydell5500b062011-05-19 14:46:19 +01002756#undef CONV_ITOF
2757#undef CONV_FTOI
2758#undef FLOAT_CONVS
pbrook4373f3c2008-03-31 03:47:19 +00002759
2760/* floating point conversion */
2761float64 VFP_HELPER(fcvtd, s)(float32 x, CPUState *env)
2762{
Peter Maydell2d627732010-12-07 15:37:34 +00002763 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2764 /* ARM requires that S<->D conversion of any kind of NaN generates
2765 * a quiet NaN by forcing the most significant frac bit to 1.
2766 */
2767 return float64_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002768}
2769
2770float32 VFP_HELPER(fcvts, d)(float64 x, CPUState *env)
2771{
Peter Maydell2d627732010-12-07 15:37:34 +00002772 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2773 /* ARM requires that S<->D conversion of any kind of NaN generates
2774 * a quiet NaN by forcing the most significant frac bit to 1.
2775 */
2776 return float32_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002777}
2778
2779/* VFP3 fixed point conversion. */
Peter Maydell622465e2011-03-14 07:23:11 +00002780#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
Peter Maydell5500b062011-05-19 14:46:19 +01002781float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2782 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002783{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002784 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002785 float##fsz tmp; \
Peter Maydell5500b062011-05-19 14:46:19 +01002786 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2787 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002788} \
Peter Maydell5500b062011-05-19 14:46:19 +01002789uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2790 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002791{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002792 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002793 float##fsz tmp; \
2794 if (float##fsz##_is_any_nan(x)) { \
Peter Maydell5500b062011-05-19 14:46:19 +01002795 float_raise(float_flag_invalid, fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00002796 return 0; \
Peter Maydell09d94872010-12-07 15:37:34 +00002797 } \
Peter Maydell5500b062011-05-19 14:46:19 +01002798 tmp = float##fsz##_scalbn(x, shift, fpst); \
2799 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002800}
2801
Peter Maydell622465e2011-03-14 07:23:11 +00002802VFP_CONV_FIX(sh, d, 64, int16, )
2803VFP_CONV_FIX(sl, d, 64, int32, )
2804VFP_CONV_FIX(uh, d, 64, uint16, u)
2805VFP_CONV_FIX(ul, d, 64, uint32, u)
2806VFP_CONV_FIX(sh, s, 32, int16, )
2807VFP_CONV_FIX(sl, s, 32, int32, )
2808VFP_CONV_FIX(uh, s, 32, uint16, u)
2809VFP_CONV_FIX(ul, s, 32, uint32, u)
pbrook4373f3c2008-03-31 03:47:19 +00002810#undef VFP_CONV_FIX
2811
Paul Brook60011492009-11-19 16:45:20 +00002812/* Half precision conversions. */
Peter Maydell2d981da2011-02-10 11:29:01 +00002813static float32 do_fcvt_f16_to_f32(uint32_t a, CPUState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002814{
Paul Brook60011492009-11-19 16:45:20 +00002815 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002816 float32 r = float16_to_float32(make_float16(a), ieee, s);
2817 if (ieee) {
2818 return float32_maybe_silence_nan(r);
2819 }
2820 return r;
Paul Brook60011492009-11-19 16:45:20 +00002821}
2822
Peter Maydell2d981da2011-02-10 11:29:01 +00002823static uint32_t do_fcvt_f32_to_f16(float32 a, CPUState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002824{
Paul Brook60011492009-11-19 16:45:20 +00002825 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002826 float16 r = float32_to_float16(a, ieee, s);
2827 if (ieee) {
2828 r = float16_maybe_silence_nan(r);
2829 }
2830 return float16_val(r);
Paul Brook60011492009-11-19 16:45:20 +00002831}
2832
Peter Maydell2d981da2011-02-10 11:29:01 +00002833float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2834{
2835 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2836}
2837
2838uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUState *env)
2839{
2840 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2841}
2842
2843float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2844{
2845 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2846}
2847
2848uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUState *env)
2849{
2850 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2851}
2852
Peter Maydelldda3ec42011-03-14 15:37:12 +00002853#define float32_two make_float32(0x40000000)
Peter Maydell6aae3df2011-03-14 15:37:13 +00002854#define float32_three make_float32(0x40400000)
2855#define float32_one_point_five make_float32(0x3fc00000)
Peter Maydelldda3ec42011-03-14 15:37:12 +00002856
pbrook4373f3c2008-03-31 03:47:19 +00002857float32 HELPER(recps_f32)(float32 a, float32 b, CPUState *env)
2858{
Peter Maydelldda3ec42011-03-14 15:37:12 +00002859 float_status *s = &env->vfp.standard_fp_status;
2860 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2861 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002862 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2863 float_raise(float_flag_input_denormal, s);
2864 }
Peter Maydelldda3ec42011-03-14 15:37:12 +00002865 return float32_two;
2866 }
2867 return float32_sub(float32_two, float32_mul(a, b, s), s);
pbrook4373f3c2008-03-31 03:47:19 +00002868}
2869
2870float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUState *env)
2871{
Peter Maydell71826962011-01-14 20:39:18 +01002872 float_status *s = &env->vfp.standard_fp_status;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002873 float32 product;
2874 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2875 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002876 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2877 float_raise(float_flag_input_denormal, s);
2878 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002879 return float32_one_point_five;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002880 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002881 product = float32_mul(a, b, s);
2882 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
pbrook4373f3c2008-03-31 03:47:19 +00002883}
2884
pbrook8f8e3aa2008-03-31 03:48:01 +00002885/* NEON helpers. */
2886
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01002887/* Constants 256 and 512 are used in some helpers; we avoid relying on
2888 * int->float conversions at run-time. */
2889#define float64_256 make_float64(0x4070000000000000LL)
2890#define float64_512 make_float64(0x4080000000000000LL)
2891
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002892/* The algorithm that must be used to calculate the estimate
2893 * is specified by the ARM ARM.
2894 */
2895static float64 recip_estimate(float64 a, CPUState *env)
2896{
Peter Maydell1146a812011-05-19 14:46:14 +01002897 /* These calculations mustn't set any fp exception flags,
2898 * so we use a local copy of the fp_status.
2899 */
2900 float_status dummy_status = env->vfp.standard_fp_status;
2901 float_status *s = &dummy_status;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002902 /* q = (int)(a * 512.0) */
2903 float64 q = float64_mul(float64_512, a, s);
2904 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2905
2906 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2907 q = int64_to_float64(q_int, s);
2908 q = float64_add(q, float64_half, s);
2909 q = float64_div(q, float64_512, s);
2910 q = float64_div(float64_one, q, s);
2911
2912 /* s = (int)(256.0 * r + 0.5) */
2913 q = float64_mul(q, float64_256, s);
2914 q = float64_add(q, float64_half, s);
2915 q_int = float64_to_int64_round_to_zero(q, s);
2916
2917 /* return (double)s / 256.0 */
2918 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2919}
2920
pbrook4373f3c2008-03-31 03:47:19 +00002921float32 HELPER(recpe_f32)(float32 a, CPUState *env)
2922{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002923 float_status *s = &env->vfp.standard_fp_status;
2924 float64 f64;
2925 uint32_t val32 = float32_val(a);
2926
2927 int result_exp;
2928 int a_exp = (val32 & 0x7f800000) >> 23;
2929 int sign = val32 & 0x80000000;
2930
2931 if (float32_is_any_nan(a)) {
2932 if (float32_is_signaling_nan(a)) {
2933 float_raise(float_flag_invalid, s);
2934 }
2935 return float32_default_nan;
2936 } else if (float32_is_infinity(a)) {
2937 return float32_set_sign(float32_zero, float32_is_neg(a));
2938 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002939 if (!float32_is_zero(a)) {
2940 float_raise(float_flag_input_denormal, s);
2941 }
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002942 float_raise(float_flag_divbyzero, s);
2943 return float32_set_sign(float32_infinity, float32_is_neg(a));
2944 } else if (a_exp >= 253) {
2945 float_raise(float_flag_underflow, s);
2946 return float32_set_sign(float32_zero, float32_is_neg(a));
2947 }
2948
2949 f64 = make_float64((0x3feULL << 52)
2950 | ((int64_t)(val32 & 0x7fffff) << 29));
2951
2952 result_exp = 253 - a_exp;
2953
2954 f64 = recip_estimate(f64, env);
2955
2956 val32 = sign
2957 | ((result_exp & 0xff) << 23)
2958 | ((float64_val(f64) >> 29) & 0x7fffff);
2959 return make_float32(val32);
pbrook4373f3c2008-03-31 03:47:19 +00002960}
2961
Christophe Lyone07be5d2011-02-21 17:38:48 +01002962/* The algorithm that must be used to calculate the estimate
2963 * is specified by the ARM ARM.
2964 */
2965static float64 recip_sqrt_estimate(float64 a, CPUState *env)
2966{
Peter Maydell1146a812011-05-19 14:46:14 +01002967 /* These calculations mustn't set any fp exception flags,
2968 * so we use a local copy of the fp_status.
2969 */
2970 float_status dummy_status = env->vfp.standard_fp_status;
2971 float_status *s = &dummy_status;
Christophe Lyone07be5d2011-02-21 17:38:48 +01002972 float64 q;
2973 int64_t q_int;
2974
2975 if (float64_lt(a, float64_half, s)) {
2976 /* range 0.25 <= a < 0.5 */
2977
2978 /* a in units of 1/512 rounded down */
2979 /* q0 = (int)(a * 512.0); */
2980 q = float64_mul(float64_512, a, s);
2981 q_int = float64_to_int64_round_to_zero(q, s);
2982
2983 /* reciprocal root r */
2984 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2985 q = int64_to_float64(q_int, s);
2986 q = float64_add(q, float64_half, s);
2987 q = float64_div(q, float64_512, s);
2988 q = float64_sqrt(q, s);
2989 q = float64_div(float64_one, q, s);
2990 } else {
2991 /* range 0.5 <= a < 1.0 */
2992
2993 /* a in units of 1/256 rounded down */
2994 /* q1 = (int)(a * 256.0); */
2995 q = float64_mul(float64_256, a, s);
2996 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2997
2998 /* reciprocal root r */
2999 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3000 q = int64_to_float64(q_int, s);
3001 q = float64_add(q, float64_half, s);
3002 q = float64_div(q, float64_256, s);
3003 q = float64_sqrt(q, s);
3004 q = float64_div(float64_one, q, s);
3005 }
3006 /* r in units of 1/256 rounded to nearest */
3007 /* s = (int)(256.0 * r + 0.5); */
3008
3009 q = float64_mul(q, float64_256,s );
3010 q = float64_add(q, float64_half, s);
3011 q_int = float64_to_int64_round_to_zero(q, s);
3012
3013 /* return (double)s / 256.0;*/
3014 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3015}
3016
pbrook4373f3c2008-03-31 03:47:19 +00003017float32 HELPER(rsqrte_f32)(float32 a, CPUState *env)
3018{
Christophe Lyone07be5d2011-02-21 17:38:48 +01003019 float_status *s = &env->vfp.standard_fp_status;
3020 int result_exp;
3021 float64 f64;
3022 uint32_t val;
3023 uint64_t val64;
3024
3025 val = float32_val(a);
3026
3027 if (float32_is_any_nan(a)) {
3028 if (float32_is_signaling_nan(a)) {
3029 float_raise(float_flag_invalid, s);
3030 }
3031 return float32_default_nan;
3032 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003033 if (!float32_is_zero(a)) {
3034 float_raise(float_flag_input_denormal, s);
3035 }
Christophe Lyone07be5d2011-02-21 17:38:48 +01003036 float_raise(float_flag_divbyzero, s);
3037 return float32_set_sign(float32_infinity, float32_is_neg(a));
3038 } else if (float32_is_neg(a)) {
3039 float_raise(float_flag_invalid, s);
3040 return float32_default_nan;
3041 } else if (float32_is_infinity(a)) {
3042 return float32_zero;
3043 }
3044
3045 /* Normalize to a double-precision value between 0.25 and 1.0,
3046 * preserving the parity of the exponent. */
3047 if ((val & 0x800000) == 0) {
3048 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3049 | (0x3feULL << 52)
3050 | ((uint64_t)(val & 0x7fffff) << 29));
3051 } else {
3052 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3053 | (0x3fdULL << 52)
3054 | ((uint64_t)(val & 0x7fffff) << 29));
3055 }
3056
3057 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3058
3059 f64 = recip_sqrt_estimate(f64, env);
3060
3061 val64 = float64_val(f64);
3062
Christophe LYON26cc6ab2011-10-19 16:14:05 +00003063 val = ((result_exp & 0xff) << 23)
Christophe Lyone07be5d2011-02-21 17:38:48 +01003064 | ((val64 >> 29) & 0x7fffff);
3065 return make_float32(val);
pbrook4373f3c2008-03-31 03:47:19 +00003066}
3067
3068uint32_t HELPER(recpe_u32)(uint32_t a, CPUState *env)
3069{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003070 float64 f64;
3071
3072 if ((a & 0x80000000) == 0) {
3073 return 0xffffffff;
3074 }
3075
3076 f64 = make_float64((0x3feULL << 52)
3077 | ((int64_t)(a & 0x7fffffff) << 21));
3078
3079 f64 = recip_estimate (f64, env);
3080
3081 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003082}
3083
3084uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)
3085{
Christophe Lyone07be5d2011-02-21 17:38:48 +01003086 float64 f64;
3087
3088 if ((a & 0xc0000000) == 0) {
3089 return 0xffffffff;
3090 }
3091
3092 if (a & 0x80000000) {
3093 f64 = make_float64((0x3feULL << 52)
3094 | ((uint64_t)(a & 0x7fffffff) << 21));
3095 } else { /* bits 31-30 == '01' */
3096 f64 = make_float64((0x3fdULL << 52)
3097 | ((uint64_t)(a & 0x3fffffff) << 22));
3098 }
3099
3100 f64 = recip_sqrt_estimate(f64, env);
3101
3102 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003103}
pbrookfe1479c2008-12-19 13:18:36 +00003104
Peter Maydellda97f522011-10-19 16:14:07 +00003105/* VFPv4 fused multiply-accumulate */
3106float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3107{
3108 float_status *fpst = fpstp;
3109 return float32_muladd(a, b, c, 0, fpst);
3110}
3111
3112float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3113{
3114 float_status *fpst = fpstp;
3115 return float64_muladd(a, b, c, 0, fpst);
3116}
3117
pbrookfe1479c2008-12-19 13:18:36 +00003118void HELPER(set_teecr)(CPUState *env, uint32_t val)
3119{
3120 val &= 1;
3121 if (env->teecr != val) {
3122 env->teecr = val;
3123 tb_flush(env);
3124 }
3125}