blob: 28f127baf832c09d8674a58c81e00ec53266d334 [file] [log] [blame]
bellardb5ff1b32005-11-26 10:38:39 +00001#include "cpu.h"
pbrook9ee6e8b2007-11-11 00:04:49 +00002#include "gdbstub.h"
Lluís7b592202011-04-13 18:38:24 +02003#include "helper.h"
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +02004#include "host-utils.h"
Paul Brook4f78c9a2010-04-05 19:56:34 +01005#if !defined(CONFIG_USER_ONLY)
Paul Brook983fe822010-04-05 19:34:51 +01006#include "hw/loader.h"
Paul Brook4f78c9a2010-04-05 19:56:34 +01007#endif
Peter Maydell0b03bdf2012-01-25 12:42:29 +00008#include "sysemu.h"
9
10static uint32_t cortexa15_cp15_c0_c1[8] = {
11 0x00001131, 0x00011011, 0x02010555, 0x00000000,
12 0x10201105, 0x20000000, 0x01240000, 0x02102211
13};
14
15static uint32_t cortexa15_cp15_c0_c2[8] = {
16 0x02101110, 0x13112111, 0x21232041, 0x11112131, 0x10011142, 0, 0, 0
17};
pbrook9ee6e8b2007-11-11 00:04:49 +000018
Paul Brook10055562009-11-19 16:45:20 +000019static uint32_t cortexa9_cp15_c0_c1[8] =
20{ 0x1031, 0x11, 0x000, 0, 0x00100103, 0x20000000, 0x01230000, 0x00002111 };
21
22static uint32_t cortexa9_cp15_c0_c2[8] =
23{ 0x00101111, 0x13112111, 0x21232041, 0x11112131, 0x00111142, 0, 0, 0 };
24
pbrook9ee6e8b2007-11-11 00:04:49 +000025static uint32_t cortexa8_cp15_c0_c1[8] =
26{ 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
27
28static uint32_t cortexa8_cp15_c0_c2[8] =
29{ 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
30
31static uint32_t mpcore_cp15_c0_c1[8] =
32{ 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
33
34static uint32_t mpcore_cp15_c0_c2[8] =
35{ 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
36
37static uint32_t arm1136_cp15_c0_c1[8] =
38{ 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
39
40static uint32_t arm1136_cp15_c0_c2[8] =
41{ 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
bellardb5ff1b32005-11-26 10:38:39 +000042
Jamie Iles7807eed2011-07-20 10:32:54 +000043static uint32_t arm1176_cp15_c0_c1[8] =
44{ 0x111, 0x11, 0x33, 0, 0x01130003, 0x10030302, 0x01222100, 0 };
45
46static uint32_t arm1176_cp15_c0_c2[8] =
47{ 0x0140011, 0x12002111, 0x11231121, 0x01102131, 0x01141, 0, 0, 0 };
48
bellardaaed9092007-11-10 15:15:54 +000049static uint32_t cpu_arm_find_by_name(const char *name);
50
pbrookf3d6b952007-03-11 13:03:18 +000051static inline void set_feature(CPUARMState *env, int feature)
52{
53 env->features |= 1u << feature;
54}
55
56static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
57{
58 env->cp15.c0_cpuid = id;
59 switch (id) {
60 case ARM_CPUID_ARM926:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040061 set_feature(env, ARM_FEATURE_V5);
pbrookf3d6b952007-03-11 13:03:18 +000062 set_feature(env, ARM_FEATURE_VFP);
63 env->vfp.xregs[ARM_VFP_FPSID] = 0x41011090;
balrogc1713132007-04-30 01:26:42 +000064 env->cp15.c0_cachetype = 0x1dd20d2;
balrog610c3c82007-06-24 12:09:48 +000065 env->cp15.c1_sys = 0x00090078;
pbrookf3d6b952007-03-11 13:03:18 +000066 break;
pbrookce819862007-05-08 02:30:40 +000067 case ARM_CPUID_ARM946:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040068 set_feature(env, ARM_FEATURE_V5);
pbrookce819862007-05-08 02:30:40 +000069 set_feature(env, ARM_FEATURE_MPU);
70 env->cp15.c0_cachetype = 0x0f004006;
balrog610c3c82007-06-24 12:09:48 +000071 env->cp15.c1_sys = 0x00000078;
pbrookce819862007-05-08 02:30:40 +000072 break;
pbrookf3d6b952007-03-11 13:03:18 +000073 case ARM_CPUID_ARM1026:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040074 set_feature(env, ARM_FEATURE_V5);
pbrookf3d6b952007-03-11 13:03:18 +000075 set_feature(env, ARM_FEATURE_VFP);
76 set_feature(env, ARM_FEATURE_AUXCR);
77 env->vfp.xregs[ARM_VFP_FPSID] = 0x410110a0;
balrogc1713132007-04-30 01:26:42 +000078 env->cp15.c0_cachetype = 0x1dd20d2;
balrog610c3c82007-06-24 12:09:48 +000079 env->cp15.c1_sys = 0x00090078;
balrogc1713132007-04-30 01:26:42 +000080 break;
pbrook9ee6e8b2007-11-11 00:04:49 +000081 case ARM_CPUID_ARM1136:
Peter Maydell906879a2011-07-20 10:32:55 +000082 /* This is the 1136 r1, which is a v6K core */
83 set_feature(env, ARM_FEATURE_V6K);
84 /* Fall through */
85 case ARM_CPUID_ARM1136_R2:
86 /* What qemu calls "arm1136_r2" is actually the 1136 r0p2, ie an
87 * older core than plain "arm1136". In particular this does not
88 * have the v6K features.
89 */
pbrook9ee6e8b2007-11-11 00:04:49 +000090 set_feature(env, ARM_FEATURE_V6);
91 set_feature(env, ARM_FEATURE_VFP);
Peter Maydell906879a2011-07-20 10:32:55 +000092 /* These ID register values are correct for 1136 but may be wrong
93 * for 1136_r2 (in particular r0p2 does not actually implement most
94 * of the ID registers).
95 */
pbrook9ee6e8b2007-11-11 00:04:49 +000096 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
97 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
98 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
99 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000100 memcpy(env->cp15.c0_c2, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +0000101 env->cp15.c0_cachetype = 0x1dd20d2;
Juha Riihimäki16440c52010-12-08 13:15:18 +0200102 env->cp15.c1_sys = 0x00050078;
pbrook9ee6e8b2007-11-11 00:04:49 +0000103 break;
Jamie Iles7807eed2011-07-20 10:32:54 +0000104 case ARM_CPUID_ARM1176:
Jamie Iles7807eed2011-07-20 10:32:54 +0000105 set_feature(env, ARM_FEATURE_V6K);
106 set_feature(env, ARM_FEATURE_VFP);
Peter Maydell906879a2011-07-20 10:32:55 +0000107 set_feature(env, ARM_FEATURE_VAPA);
Jamie Iles7807eed2011-07-20 10:32:54 +0000108 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b5;
109 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
110 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
111 memcpy(env->cp15.c0_c1, arm1176_cp15_c0_c1, 8 * sizeof(uint32_t));
112 memcpy(env->cp15.c0_c2, arm1176_cp15_c0_c2, 8 * sizeof(uint32_t));
113 env->cp15.c0_cachetype = 0x1dd20d2;
114 env->cp15.c1_sys = 0x00050078;
115 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000116 case ARM_CPUID_ARM11MPCORE:
pbrook9ee6e8b2007-11-11 00:04:49 +0000117 set_feature(env, ARM_FEATURE_V6K);
118 set_feature(env, ARM_FEATURE_VFP);
Peter Maydell906879a2011-07-20 10:32:55 +0000119 set_feature(env, ARM_FEATURE_VAPA);
pbrook9ee6e8b2007-11-11 00:04:49 +0000120 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
121 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
122 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
123 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000124 memcpy(env->cp15.c0_c2, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +0000125 env->cp15.c0_cachetype = 0x1dd20d2;
126 break;
127 case ARM_CPUID_CORTEXA8:
pbrook9ee6e8b2007-11-11 00:04:49 +0000128 set_feature(env, ARM_FEATURE_V7);
pbrook9ee6e8b2007-11-11 00:04:49 +0000129 set_feature(env, ARM_FEATURE_VFP3);
130 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +0000131 set_feature(env, ARM_FEATURE_THUMB2EE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000132 env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
133 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
134 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
135 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000136 memcpy(env->cp15.c0_c2, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrooka49ea272008-12-19 13:37:53 +0000137 env->cp15.c0_cachetype = 0x82048004;
138 env->cp15.c0_clid = (1 << 27) | (2 << 24) | 3;
139 env->cp15.c0_ccsid[0] = 0xe007e01a; /* 16k L1 dcache. */
140 env->cp15.c0_ccsid[1] = 0x2007e01a; /* 16k L1 icache. */
141 env->cp15.c0_ccsid[2] = 0xf0000000; /* No L2 icache. */
Mattias Holm9c486ad2010-12-08 13:15:17 +0200142 env->cp15.c1_sys = 0x00c50078;
pbrook9ee6e8b2007-11-11 00:04:49 +0000143 break;
Paul Brook10055562009-11-19 16:45:20 +0000144 case ARM_CPUID_CORTEXA9:
Paul Brook10055562009-11-19 16:45:20 +0000145 set_feature(env, ARM_FEATURE_V7);
Paul Brook10055562009-11-19 16:45:20 +0000146 set_feature(env, ARM_FEATURE_VFP3);
147 set_feature(env, ARM_FEATURE_VFP_FP16);
148 set_feature(env, ARM_FEATURE_NEON);
149 set_feature(env, ARM_FEATURE_THUMB2EE);
Peter Maydelle1bbf442011-02-03 19:43:22 +0000150 /* Note that A9 supports the MP extensions even for
151 * A9UP and single-core A9MP (which are both different
152 * and valid configurations; we don't model A9UP).
153 */
154 set_feature(env, ARM_FEATURE_V7MP);
Peter Maydell2d2624a2012-02-07 17:56:57 +0000155 env->vfp.xregs[ARM_VFP_FPSID] = 0x41033090;
Paul Brook10055562009-11-19 16:45:20 +0000156 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
157 env->vfp.xregs[ARM_VFP_MVFR1] = 0x01111111;
158 memcpy(env->cp15.c0_c1, cortexa9_cp15_c0_c1, 8 * sizeof(uint32_t));
159 memcpy(env->cp15.c0_c2, cortexa9_cp15_c0_c2, 8 * sizeof(uint32_t));
160 env->cp15.c0_cachetype = 0x80038003;
161 env->cp15.c0_clid = (1 << 27) | (1 << 24) | 3;
162 env->cp15.c0_ccsid[0] = 0xe00fe015; /* 16k L1 dcache. */
163 env->cp15.c0_ccsid[1] = 0x200fe015; /* 16k L1 icache. */
Juha Riihimäki16440c52010-12-08 13:15:18 +0200164 env->cp15.c1_sys = 0x00c50078;
Paul Brook10055562009-11-19 16:45:20 +0000165 break;
Peter Maydell0b03bdf2012-01-25 12:42:29 +0000166 case ARM_CPUID_CORTEXA15:
167 set_feature(env, ARM_FEATURE_V7);
168 set_feature(env, ARM_FEATURE_VFP4);
169 set_feature(env, ARM_FEATURE_VFP_FP16);
170 set_feature(env, ARM_FEATURE_NEON);
171 set_feature(env, ARM_FEATURE_THUMB2EE);
172 set_feature(env, ARM_FEATURE_ARM_DIV);
173 set_feature(env, ARM_FEATURE_V7MP);
174 set_feature(env, ARM_FEATURE_GENERIC_TIMER);
175 env->vfp.xregs[ARM_VFP_FPSID] = 0x410430f0;
176 env->vfp.xregs[ARM_VFP_MVFR0] = 0x10110222;
177 env->vfp.xregs[ARM_VFP_MVFR1] = 0x11111111;
178 memcpy(env->cp15.c0_c1, cortexa15_cp15_c0_c1, 8 * sizeof(uint32_t));
179 memcpy(env->cp15.c0_c2, cortexa15_cp15_c0_c2, 8 * sizeof(uint32_t));
180 env->cp15.c0_cachetype = 0x8444c004;
181 env->cp15.c0_clid = 0x0a200023;
182 env->cp15.c0_ccsid[0] = 0x701fe00a; /* 32K L1 dcache */
183 env->cp15.c0_ccsid[1] = 0x201fe00a; /* 32K L1 icache */
184 env->cp15.c0_ccsid[2] = 0x711fe07a; /* 4096K L2 unified cache */
185 env->cp15.c1_sys = 0x00c50078;
186 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000187 case ARM_CPUID_CORTEXM3:
pbrook9ee6e8b2007-11-11 00:04:49 +0000188 set_feature(env, ARM_FEATURE_V7);
189 set_feature(env, ARM_FEATURE_M);
pbrook9ee6e8b2007-11-11 00:04:49 +0000190 break;
191 case ARM_CPUID_ANY: /* For userspace emulation. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000192 set_feature(env, ARM_FEATURE_V7);
Peter Maydellda97f522011-10-19 16:14:07 +0000193 set_feature(env, ARM_FEATURE_VFP4);
Paul Brook60011492009-11-19 16:45:20 +0000194 set_feature(env, ARM_FEATURE_VFP_FP16);
pbrook9ee6e8b2007-11-11 00:04:49 +0000195 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +0000196 set_feature(env, ARM_FEATURE_THUMB2EE);
Peter Maydellb8b8ea02011-10-19 16:14:06 +0000197 set_feature(env, ARM_FEATURE_ARM_DIV);
Peter Maydelle1bbf442011-02-03 19:43:22 +0000198 set_feature(env, ARM_FEATURE_V7MP);
pbrook9ee6e8b2007-11-11 00:04:49 +0000199 break;
balrogc3d26892007-07-29 17:57:26 +0000200 case ARM_CPUID_TI915T:
201 case ARM_CPUID_TI925T:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400202 set_feature(env, ARM_FEATURE_V4T);
balrogc3d26892007-07-29 17:57:26 +0000203 set_feature(env, ARM_FEATURE_OMAPCP);
204 env->cp15.c0_cpuid = ARM_CPUID_TI925T; /* Depends on wiring. */
205 env->cp15.c0_cachetype = 0x5109149;
206 env->cp15.c1_sys = 0x00000070;
207 env->cp15.c15_i_max = 0x000;
208 env->cp15.c15_i_min = 0xff0;
209 break;
balrogc1713132007-04-30 01:26:42 +0000210 case ARM_CPUID_PXA250:
211 case ARM_CPUID_PXA255:
212 case ARM_CPUID_PXA260:
213 case ARM_CPUID_PXA261:
214 case ARM_CPUID_PXA262:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400215 set_feature(env, ARM_FEATURE_V5);
balrogc1713132007-04-30 01:26:42 +0000216 set_feature(env, ARM_FEATURE_XSCALE);
217 /* JTAG_ID is ((id << 28) | 0x09265013) */
218 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000219 env->cp15.c1_sys = 0x00000078;
balrogc1713132007-04-30 01:26:42 +0000220 break;
221 case ARM_CPUID_PXA270_A0:
222 case ARM_CPUID_PXA270_A1:
223 case ARM_CPUID_PXA270_B0:
224 case ARM_CPUID_PXA270_B1:
225 case ARM_CPUID_PXA270_C0:
226 case ARM_CPUID_PXA270_C5:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400227 set_feature(env, ARM_FEATURE_V5);
balrogc1713132007-04-30 01:26:42 +0000228 set_feature(env, ARM_FEATURE_XSCALE);
229 /* JTAG_ID is ((id << 28) | 0x09265013) */
balrog18c9b562007-04-30 02:02:17 +0000230 set_feature(env, ARM_FEATURE_IWMMXT);
231 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
balrogc1713132007-04-30 01:26:42 +0000232 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000233 env->cp15.c1_sys = 0x00000078;
pbrookf3d6b952007-03-11 13:03:18 +0000234 break;
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +0400235 case ARM_CPUID_SA1100:
236 case ARM_CPUID_SA1110:
237 set_feature(env, ARM_FEATURE_STRONGARM);
238 env->cp15.c1_sys = 0x00000070;
239 break;
pbrookf3d6b952007-03-11 13:03:18 +0000240 default:
241 cpu_abort(env, "Bad CPU ID: %x\n", id);
242 break;
243 }
Peter Maydell906879a2011-07-20 10:32:55 +0000244
245 /* Some features automatically imply others: */
246 if (arm_feature(env, ARM_FEATURE_V7)) {
247 set_feature(env, ARM_FEATURE_VAPA);
Andreas Färberb3faf5f2011-12-13 18:19:24 +0000248 set_feature(env, ARM_FEATURE_THUMB2);
Andreas Färberbbc5c5f2011-12-13 18:19:24 +0000249 if (!arm_feature(env, ARM_FEATURE_M)) {
250 set_feature(env, ARM_FEATURE_V6K);
251 } else {
252 set_feature(env, ARM_FEATURE_V6);
253 }
Peter Maydell906879a2011-07-20 10:32:55 +0000254 }
Andreas Färber6bf62122011-12-13 18:19:24 +0000255 if (arm_feature(env, ARM_FEATURE_V6K)) {
256 set_feature(env, ARM_FEATURE_V6);
Andrew Towers06ed5d62012-03-29 02:41:08 +0000257 set_feature(env, ARM_FEATURE_MVFR);
Andreas Färber6bf62122011-12-13 18:19:24 +0000258 }
Andreas Färber08c40f32011-12-13 18:19:24 +0000259 if (arm_feature(env, ARM_FEATURE_V6)) {
260 set_feature(env, ARM_FEATURE_V5);
Andreas Färber10e87702011-12-13 18:19:24 +0000261 if (!arm_feature(env, ARM_FEATURE_M)) {
262 set_feature(env, ARM_FEATURE_AUXCR);
263 }
Andreas Färber08c40f32011-12-13 18:19:24 +0000264 }
Andreas Färberddb572e2011-12-13 18:19:23 +0000265 if (arm_feature(env, ARM_FEATURE_V5)) {
266 set_feature(env, ARM_FEATURE_V4T);
267 }
Andreas Färber908ce982011-12-13 18:19:24 +0000268 if (arm_feature(env, ARM_FEATURE_M)) {
269 set_feature(env, ARM_FEATURE_THUMB_DIV);
270 }
Peter Maydellb8b8ea02011-10-19 16:14:06 +0000271 if (arm_feature(env, ARM_FEATURE_ARM_DIV)) {
272 set_feature(env, ARM_FEATURE_THUMB_DIV);
273 }
Andreas Färberdb8336c2011-12-13 18:19:25 +0000274 if (arm_feature(env, ARM_FEATURE_VFP4)) {
275 set_feature(env, ARM_FEATURE_VFP3);
276 }
Andreas Färber86e72db2011-12-13 18:19:24 +0000277 if (arm_feature(env, ARM_FEATURE_VFP3)) {
278 set_feature(env, ARM_FEATURE_VFP);
279 }
pbrookf3d6b952007-03-11 13:03:18 +0000280}
281
Andreas Färberdec9c2d2012-03-29 04:50:31 +0000282/* TODO Move contents into arm_cpu_reset() in cpu.c,
283 * once cpu_reset_model_id() is eliminated,
284 * and then forward to cpu_reset() here.
285 */
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100286void cpu_state_reset(CPUARMState *env)
pbrook40f137e2006-02-20 00:33:36 +0000287{
pbrookf3d6b952007-03-11 13:03:18 +0000288 uint32_t id;
Mark Langsdorf5fe91012012-01-25 11:49:46 +0000289 uint32_t tmp = 0;
aliguorieca1bdf2009-01-26 19:54:31 +0000290
291 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
292 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
293 log_cpu_state(env, 0);
294 }
295
pbrookf3d6b952007-03-11 13:03:18 +0000296 id = env->cp15.c0_cpuid;
Mark Langsdorf5fe91012012-01-25 11:49:46 +0000297 tmp = env->cp15.c15_config_base_address;
pbrookf3d6b952007-03-11 13:03:18 +0000298 memset(env, 0, offsetof(CPUARMState, breakpoints));
299 if (id)
300 cpu_reset_model_id(env, id);
Mark Langsdorf5fe91012012-01-25 11:49:46 +0000301 env->cp15.c15_config_base_address = tmp;
pbrook40f137e2006-02-20 00:33:36 +0000302#if defined (CONFIG_USER_ONLY)
303 env->uncached_cpsr = ARM_CPU_MODE_USR;
Peter Maydell3a807de2010-11-19 15:36:47 +0000304 /* For user mode we must enable access to coprocessors */
pbrook40f137e2006-02-20 00:33:36 +0000305 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
Peter Maydell3a807de2010-11-19 15:36:47 +0000306 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
307 env->cp15.c15_cpar = 3;
308 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
309 env->cp15.c15_cpar = 1;
310 }
pbrook40f137e2006-02-20 00:33:36 +0000311#else
312 /* SVC mode with interrupts disabled. */
313 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
pbrook9ee6e8b2007-11-11 00:04:49 +0000314 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
Paul Brook983fe822010-04-05 19:34:51 +0100315 clear at reset. Initial SP and PC are loaded from ROM. */
316 if (IS_M(env)) {
317 uint32_t pc;
318 uint8_t *rom;
pbrook9ee6e8b2007-11-11 00:04:49 +0000319 env->uncached_cpsr &= ~CPSR_I;
Paul Brook983fe822010-04-05 19:34:51 +0100320 rom = rom_ptr(0);
321 if (rom) {
322 /* We should really use ldl_phys here, in case the guest
323 modified flash and reset itself. However images
Andreas Färber297d1b42011-10-02 18:35:48 +0200324 loaded via -kernel have not been copied yet, so load the
Paul Brook983fe822010-04-05 19:34:51 +0100325 values directly from there. */
326 env->regs[13] = ldl_p(rom);
327 pc = ldl_p(rom + 4);
328 env->thumb = pc & 1;
329 env->regs[15] = pc & ~1;
330 }
331 }
pbrook40f137e2006-02-20 00:33:36 +0000332 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
pbrookb2fa1792008-10-22 19:22:30 +0000333 env->cp15.c2_base_mask = 0xffffc000u;
Peter Maydell74594c92011-03-22 12:16:16 +0000334 /* v7 performance monitor control register: same implementor
335 * field as main ID register, and we implement no event counters.
336 */
337 env->cp15.c9_pmcr = (id & 0xff000000);
pbrook40f137e2006-02-20 00:33:36 +0000338#endif
Peter Maydell3a492f32011-01-14 20:39:18 +0100339 set_flush_to_zero(1, &env->vfp.standard_fp_status);
340 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
341 set_default_nan_mode(1, &env->vfp.standard_fp_status);
Peter Maydell9df38c42011-04-12 13:56:41 +0100342 set_float_detect_tininess(float_tininess_before_rounding,
343 &env->vfp.fp_status);
344 set_float_detect_tininess(float_tininess_before_rounding,
345 &env->vfp.standard_fp_status);
pbrookf3d6b952007-03-11 13:03:18 +0000346 tlb_flush(env, 1);
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100347 /* Reset is a state change for some CPUARMState fields which we
Peter Maydella84fac12012-02-01 17:23:04 +0000348 * bake assumptions about into translated code, so we need to
349 * tb_flush().
350 */
351 tb_flush(env);
pbrook40f137e2006-02-20 00:33:36 +0000352}
353
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100354static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000355{
356 int nregs;
357
358 /* VFP data registers are always little-endian. */
359 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
360 if (reg < nregs) {
361 stfq_le_p(buf, env->vfp.regs[reg]);
362 return 8;
363 }
364 if (arm_feature(env, ARM_FEATURE_NEON)) {
365 /* Aliases for Q regs. */
366 nregs += 16;
367 if (reg < nregs) {
368 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
369 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
370 return 16;
371 }
372 }
373 switch (reg - nregs) {
374 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
375 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
376 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
377 }
378 return 0;
379}
380
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100381static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000382{
383 int nregs;
384
385 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
386 if (reg < nregs) {
387 env->vfp.regs[reg] = ldfq_le_p(buf);
388 return 8;
389 }
390 if (arm_feature(env, ARM_FEATURE_NEON)) {
391 nregs += 16;
392 if (reg < nregs) {
393 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
394 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
395 return 16;
396 }
397 }
398 switch (reg - nregs) {
399 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
400 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +0200401 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +0000402 }
403 return 0;
404}
405
bellardaaed9092007-11-10 15:15:54 +0000406CPUARMState *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +0000407{
Andreas Färberdec9c2d2012-03-29 04:50:31 +0000408 ARMCPU *cpu;
pbrook40f137e2006-02-20 00:33:36 +0000409 CPUARMState *env;
bellardaaed9092007-11-10 15:15:54 +0000410 uint32_t id;
pbrookb26eefb2008-03-31 03:44:26 +0000411 static int inited = 0;
pbrook40f137e2006-02-20 00:33:36 +0000412
bellardaaed9092007-11-10 15:15:54 +0000413 id = cpu_arm_find_by_name(cpu_model);
414 if (id == 0)
415 return NULL;
Andreas Färberdec9c2d2012-03-29 04:50:31 +0000416 cpu = ARM_CPU(object_new(TYPE_ARM_CPU));
417 env = &cpu->env;
pbrook40f137e2006-02-20 00:33:36 +0000418 cpu_exec_init(env);
Peter Maydellf4fc2472011-11-25 19:25:50 +0100419 if (tcg_enabled() && !inited) {
pbrookb26eefb2008-03-31 03:44:26 +0000420 inited = 1;
421 arm_translate_init();
422 }
423
ths01ba9812007-12-09 02:22:57 +0000424 env->cpu_model_str = cpu_model;
bellardaaed9092007-11-10 15:15:54 +0000425 env->cp15.c0_cpuid = id;
Andreas Färber1bba0dc2012-02-08 03:03:33 +0100426 cpu_state_reset(env);
pbrook56aebc82008-10-11 17:55:29 +0000427 if (arm_feature(env, ARM_FEATURE_NEON)) {
428 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
429 51, "arm-neon.xml", 0);
430 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
431 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
432 35, "arm-vfp3.xml", 0);
433 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
434 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
435 19, "arm-vfp.xml", 0);
436 }
aliguori0bf46a42009-04-24 18:03:41 +0000437 qemu_init_vcpu(env);
pbrook40f137e2006-02-20 00:33:36 +0000438 return env;
439}
440
pbrook3371d272007-03-08 03:04:12 +0000441struct arm_cpu_t {
442 uint32_t id;
443 const char *name;
444};
445
446static const struct arm_cpu_t arm_cpu_names[] = {
447 { ARM_CPUID_ARM926, "arm926"},
pbrookce819862007-05-08 02:30:40 +0000448 { ARM_CPUID_ARM946, "arm946"},
pbrook3371d272007-03-08 03:04:12 +0000449 { ARM_CPUID_ARM1026, "arm1026"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000450 { ARM_CPUID_ARM1136, "arm1136"},
balrog827df9f2008-04-14 21:05:22 +0000451 { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
Jamie Iles7807eed2011-07-20 10:32:54 +0000452 { ARM_CPUID_ARM1176, "arm1176"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000453 { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
454 { ARM_CPUID_CORTEXM3, "cortex-m3"},
455 { ARM_CPUID_CORTEXA8, "cortex-a8"},
Paul Brook10055562009-11-19 16:45:20 +0000456 { ARM_CPUID_CORTEXA9, "cortex-a9"},
Peter Maydell0b03bdf2012-01-25 12:42:29 +0000457 { ARM_CPUID_CORTEXA15, "cortex-a15" },
balrogc3d26892007-07-29 17:57:26 +0000458 { ARM_CPUID_TI925T, "ti925t" },
balrogc1713132007-04-30 01:26:42 +0000459 { ARM_CPUID_PXA250, "pxa250" },
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +0400460 { ARM_CPUID_SA1100, "sa1100" },
461 { ARM_CPUID_SA1110, "sa1110" },
balrogc1713132007-04-30 01:26:42 +0000462 { ARM_CPUID_PXA255, "pxa255" },
463 { ARM_CPUID_PXA260, "pxa260" },
464 { ARM_CPUID_PXA261, "pxa261" },
465 { ARM_CPUID_PXA262, "pxa262" },
466 { ARM_CPUID_PXA270, "pxa270" },
467 { ARM_CPUID_PXA270_A0, "pxa270-a0" },
468 { ARM_CPUID_PXA270_A1, "pxa270-a1" },
469 { ARM_CPUID_PXA270_B0, "pxa270-b0" },
470 { ARM_CPUID_PXA270_B1, "pxa270-b1" },
471 { ARM_CPUID_PXA270_C0, "pxa270-c0" },
472 { ARM_CPUID_PXA270_C5, "pxa270-c5" },
pbrook9ee6e8b2007-11-11 00:04:49 +0000473 { ARM_CPUID_ANY, "any"},
pbrook3371d272007-03-08 03:04:12 +0000474 { 0, NULL}
475};
476
Stefan Weil9a78eea2010-10-22 23:03:33 +0200477void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
pbrook5adb4832007-03-08 03:15:18 +0000478{
479 int i;
480
j_mayerc732abe2007-10-12 06:47:46 +0000481 (*cpu_fprintf)(f, "Available CPUs:\n");
pbrook5adb4832007-03-08 03:15:18 +0000482 for (i = 0; arm_cpu_names[i].name; i++) {
j_mayerc732abe2007-10-12 06:47:46 +0000483 (*cpu_fprintf)(f, " %s\n", arm_cpu_names[i].name);
pbrook5adb4832007-03-08 03:15:18 +0000484 }
485}
486
bellardaaed9092007-11-10 15:15:54 +0000487/* return 0 if not found */
488static uint32_t cpu_arm_find_by_name(const char *name)
pbrook40f137e2006-02-20 00:33:36 +0000489{
pbrook3371d272007-03-08 03:04:12 +0000490 int i;
491 uint32_t id;
492
493 id = 0;
pbrook3371d272007-03-08 03:04:12 +0000494 for (i = 0; arm_cpu_names[i].name; i++) {
495 if (strcmp(name, arm_cpu_names[i].name) == 0) {
496 id = arm_cpu_names[i].id;
497 break;
498 }
499 }
bellardaaed9092007-11-10 15:15:54 +0000500 return id;
pbrook40f137e2006-02-20 00:33:36 +0000501}
502
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100503static int bad_mode_switch(CPUARMState *env, int mode)
Peter Maydell37064a82012-01-05 15:49:06 +0000504{
505 /* Return true if it is not valid for us to switch to
506 * this CPU mode (ie all the UNPREDICTABLE cases in
507 * the ARM ARM CPSRWriteByInstr pseudocode).
508 */
509 switch (mode) {
510 case ARM_CPU_MODE_USR:
511 case ARM_CPU_MODE_SYS:
512 case ARM_CPU_MODE_SVC:
513 case ARM_CPU_MODE_ABT:
514 case ARM_CPU_MODE_UND:
515 case ARM_CPU_MODE_IRQ:
516 case ARM_CPU_MODE_FIQ:
517 return 0;
518 default:
519 return 1;
520 }
521}
522
balrog2f4a40e2007-11-13 01:50:15 +0000523uint32_t cpsr_read(CPUARMState *env)
524{
525 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +0000526 ZF = (env->ZF == 0);
527 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +0000528 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
529 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
530 | ((env->condexec_bits & 0xfc) << 8)
531 | (env->GE << 16);
532}
533
534void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
535{
balrog2f4a40e2007-11-13 01:50:15 +0000536 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +0000537 env->ZF = (~val) & CPSR_Z;
538 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +0000539 env->CF = (val >> 29) & 1;
540 env->VF = (val << 3) & 0x80000000;
541 }
542 if (mask & CPSR_Q)
543 env->QF = ((val & CPSR_Q) != 0);
544 if (mask & CPSR_T)
545 env->thumb = ((val & CPSR_T) != 0);
546 if (mask & CPSR_IT_0_1) {
547 env->condexec_bits &= ~3;
548 env->condexec_bits |= (val >> 25) & 3;
549 }
550 if (mask & CPSR_IT_2_7) {
551 env->condexec_bits &= 3;
552 env->condexec_bits |= (val >> 8) & 0xfc;
553 }
554 if (mask & CPSR_GE) {
555 env->GE = (val >> 16) & 0xf;
556 }
557
558 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
Peter Maydell37064a82012-01-05 15:49:06 +0000559 if (bad_mode_switch(env, val & CPSR_M)) {
560 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE.
561 * We choose to ignore the attempt and leave the CPSR M field
562 * untouched.
563 */
564 mask &= ~CPSR_M;
565 } else {
566 switch_mode(env, val & CPSR_M);
567 }
balrog2f4a40e2007-11-13 01:50:15 +0000568 }
569 mask &= ~CACHED_CPSR_BITS;
570 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
571}
572
pbrookb26eefb2008-03-31 03:44:26 +0000573/* Sign/zero extend */
574uint32_t HELPER(sxtb16)(uint32_t x)
575{
576 uint32_t res;
577 res = (uint16_t)(int8_t)x;
578 res |= (uint32_t)(int8_t)(x >> 16) << 16;
579 return res;
580}
581
582uint32_t HELPER(uxtb16)(uint32_t x)
583{
584 uint32_t res;
585 res = (uint16_t)(uint8_t)x;
586 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
587 return res;
588}
589
pbrookf51bbbf2008-03-31 03:45:13 +0000590uint32_t HELPER(clz)(uint32_t x)
591{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +0200592 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +0000593}
594
pbrook36706692008-03-31 03:46:19 +0000595int32_t HELPER(sdiv)(int32_t num, int32_t den)
596{
597 if (den == 0)
598 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +0200599 if (num == INT_MIN && den == -1)
600 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +0000601 return num / den;
602}
603
604uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
605{
606 if (den == 0)
607 return 0;
608 return num / den;
609}
610
611uint32_t HELPER(rbit)(uint32_t x)
612{
613 x = ((x & 0xff000000) >> 24)
614 | ((x & 0x00ff0000) >> 8)
615 | ((x & 0x0000ff00) << 8)
616 | ((x & 0x000000ff) << 24);
617 x = ((x & 0xf0f0f0f0) >> 4)
618 | ((x & 0x0f0f0f0f) << 4);
619 x = ((x & 0x88888888) >> 3)
620 | ((x & 0x44444444) >> 1)
621 | ((x & 0x22222222) << 1)
622 | ((x & 0x11111111) << 3);
623 return x;
624}
625
pbrookad694712008-03-31 03:48:30 +0000626uint32_t HELPER(abs)(uint32_t x)
627{
628 return ((int32_t)x < 0) ? -x : x;
629}
630
ths5fafdf22007-09-16 21:08:06 +0000631#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +0000632
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100633void do_interrupt (CPUARMState *env)
bellardb5ff1b32005-11-26 10:38:39 +0000634{
635 env->exception_index = -1;
636}
637
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100638int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw,
Blue Swirl97b348e2011-08-01 16:12:17 +0000639 int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +0000640{
641 if (rw == 2) {
642 env->exception_index = EXCP_PREFETCH_ABORT;
643 env->cp15.c6_insn = address;
644 } else {
645 env->exception_index = EXCP_DATA_ABORT;
646 env->cp15.c6_data = address;
647 }
648 return 1;
649}
650
bellardb5ff1b32005-11-26 10:38:39 +0000651/* These should probably raise undefined insn exceptions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100652void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +0000653{
654 int op1 = (insn >> 8) & 0xf;
655 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
656 return;
657}
658
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100659uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +0000660{
661 int op1 = (insn >> 8) & 0xf;
662 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
663 return 0;
664}
665
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100666void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +0000667{
668 cpu_abort(env, "cp15 insn %08x\n", insn);
669}
670
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100671uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +0000672{
673 cpu_abort(env, "cp15 insn %08x\n", insn);
bellardb5ff1b32005-11-26 10:38:39 +0000674}
675
pbrook9ee6e8b2007-11-11 00:04:49 +0000676/* These should probably raise undefined insn exceptions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100677void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000678{
679 cpu_abort(env, "v7m_mrs %d\n", reg);
680}
681
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100682uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +0000683{
684 cpu_abort(env, "v7m_mrs %d\n", reg);
685 return 0;
686}
687
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100688void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +0000689{
690 if (mode != ARM_CPU_MODE_USR)
691 cpu_abort(env, "Tried to switch out of user mode\n");
692}
693
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100694void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000695{
696 cpu_abort(env, "banked r13 write\n");
697}
698
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100699uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +0000700{
701 cpu_abort(env, "banked r13 read\n");
702 return 0;
703}
704
bellardb5ff1b32005-11-26 10:38:39 +0000705#else
706
707/* Map CPU modes onto saved register banks. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100708static inline int bank_number(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +0000709{
710 switch (mode) {
711 case ARM_CPU_MODE_USR:
712 case ARM_CPU_MODE_SYS:
713 return 0;
714 case ARM_CPU_MODE_SVC:
715 return 1;
716 case ARM_CPU_MODE_ABT:
717 return 2;
718 case ARM_CPU_MODE_UND:
719 return 3;
720 case ARM_CPU_MODE_IRQ:
721 return 4;
722 case ARM_CPU_MODE_FIQ:
723 return 5;
724 }
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000725 cpu_abort(env, "Bad mode %x\n", mode);
bellardb5ff1b32005-11-26 10:38:39 +0000726 return -1;
727}
728
Andreas Färber0ecb72a2012-03-14 01:38:21 +0100729void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +0000730{
731 int old_mode;
732 int i;
733
734 old_mode = env->uncached_cpsr & CPSR_M;
735 if (mode == old_mode)
736 return;
737
738 if (old_mode == ARM_CPU_MODE_FIQ) {
739 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000740 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000741 } else if (mode == ARM_CPU_MODE_FIQ) {
742 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000743 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000744 }
745
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000746 i = bank_number(env, old_mode);
bellardb5ff1b32005-11-26 10:38:39 +0000747 env->banked_r13[i] = env->regs[13];
748 env->banked_r14[i] = env->regs[14];
749 env->banked_spsr[i] = env->spsr;
750
Peter Maydell1b9e01c2012-01-05 15:49:06 +0000751 i = bank_number(env, mode);
bellardb5ff1b32005-11-26 10:38:39 +0000752 env->regs[13] = env->banked_r13[i];
753 env->regs[14] = env->banked_r14[i];
754 env->spsr = env->banked_spsr[i];
755}
756
pbrook9ee6e8b2007-11-11 00:04:49 +0000757static void v7m_push(CPUARMState *env, uint32_t val)
758{
759 env->regs[13] -= 4;
760 stl_phys(env->regs[13], val);
761}
762
763static uint32_t v7m_pop(CPUARMState *env)
764{
765 uint32_t val;
766 val = ldl_phys(env->regs[13]);
767 env->regs[13] += 4;
768 return val;
769}
770
771/* Switch to V7M main or process stack pointer. */
772static void switch_v7m_sp(CPUARMState *env, int process)
773{
774 uint32_t tmp;
775 if (env->v7m.current_sp != process) {
776 tmp = env->v7m.other_sp;
777 env->v7m.other_sp = env->regs[13];
778 env->regs[13] = tmp;
779 env->v7m.current_sp = process;
780 }
781}
782
783static void do_v7m_exception_exit(CPUARMState *env)
784{
785 uint32_t type;
786 uint32_t xpsr;
787
788 type = env->regs[15];
789 if (env->v7m.exception != 0)
Paul Brook983fe822010-04-05 19:34:51 +0100790 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
pbrook9ee6e8b2007-11-11 00:04:49 +0000791
792 /* Switch to the target stack. */
793 switch_v7m_sp(env, (type & 4) != 0);
794 /* Pop registers. */
795 env->regs[0] = v7m_pop(env);
796 env->regs[1] = v7m_pop(env);
797 env->regs[2] = v7m_pop(env);
798 env->regs[3] = v7m_pop(env);
799 env->regs[12] = v7m_pop(env);
800 env->regs[14] = v7m_pop(env);
801 env->regs[15] = v7m_pop(env);
802 xpsr = v7m_pop(env);
803 xpsr_write(env, xpsr, 0xfffffdff);
804 /* Undo stack alignment. */
805 if (xpsr & 0x200)
806 env->regs[13] |= 4;
807 /* ??? The exception return type specifies Thread/Handler mode. However
808 this is also implied by the xPSR value. Not sure what to do
809 if there is a mismatch. */
810 /* ??? Likewise for mismatches between the CONTROL register and the stack
811 pointer. */
812}
813
aurel322b3ea312009-03-07 21:48:00 +0000814static void do_interrupt_v7m(CPUARMState *env)
pbrook9ee6e8b2007-11-11 00:04:49 +0000815{
816 uint32_t xpsr = xpsr_read(env);
817 uint32_t lr;
818 uint32_t addr;
819
820 lr = 0xfffffff1;
821 if (env->v7m.current_sp)
822 lr |= 4;
823 if (env->v7m.exception == 0)
824 lr |= 8;
825
826 /* For exceptions we just mark as pending on the NVIC, and let that
827 handle it. */
828 /* TODO: Need to escalate if the current priority is higher than the
829 one we're raising. */
830 switch (env->exception_index) {
831 case EXCP_UDEF:
Paul Brook983fe822010-04-05 19:34:51 +0100832 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000833 return;
834 case EXCP_SWI:
835 env->regs[15] += 2;
Paul Brook983fe822010-04-05 19:34:51 +0100836 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
pbrook9ee6e8b2007-11-11 00:04:49 +0000837 return;
838 case EXCP_PREFETCH_ABORT:
839 case EXCP_DATA_ABORT:
Paul Brook983fe822010-04-05 19:34:51 +0100840 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
pbrook9ee6e8b2007-11-11 00:04:49 +0000841 return;
842 case EXCP_BKPT:
pbrook2ad207d2007-11-24 23:22:11 +0000843 if (semihosting_enabled) {
844 int nr;
Paul Brookd8fd2952012-03-30 18:02:50 +0100845 nr = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
pbrook2ad207d2007-11-24 23:22:11 +0000846 if (nr == 0xab) {
847 env->regs[15] += 2;
848 env->regs[0] = do_arm_semihosting(env);
849 return;
850 }
851 }
Paul Brook983fe822010-04-05 19:34:51 +0100852 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
pbrook9ee6e8b2007-11-11 00:04:49 +0000853 return;
854 case EXCP_IRQ:
Paul Brook983fe822010-04-05 19:34:51 +0100855 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
pbrook9ee6e8b2007-11-11 00:04:49 +0000856 break;
857 case EXCP_EXCEPTION_EXIT:
858 do_v7m_exception_exit(env);
859 return;
860 default:
861 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
862 return; /* Never happens. Keep compiler happy. */
863 }
864
865 /* Align stack pointer. */
866 /* ??? Should only do this if Configuration Control Register
867 STACKALIGN bit is set. */
868 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +0000869 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +0000870 xpsr |= 0x200;
871 }
balrog6c956762008-04-13 00:57:49 +0000872 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000873 v7m_push(env, xpsr);
874 v7m_push(env, env->regs[15]);
875 v7m_push(env, env->regs[14]);
876 v7m_push(env, env->regs[12]);
877 v7m_push(env, env->regs[3]);
878 v7m_push(env, env->regs[2]);
879 v7m_push(env, env->regs[1]);
880 v7m_push(env, env->regs[0]);
881 switch_v7m_sp(env, 0);
Peter Maydellc98d1742012-03-14 12:26:10 +0000882 /* Clear IT bits */
883 env->condexec_bits = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +0000884 env->regs[14] = lr;
885 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
886 env->regs[15] = addr & 0xfffffffe;
887 env->thumb = addr & 1;
888}
889
bellardb5ff1b32005-11-26 10:38:39 +0000890/* Handle a CPU exception. */
891void do_interrupt(CPUARMState *env)
892{
893 uint32_t addr;
894 uint32_t mask;
895 int new_mode;
896 uint32_t offset;
897
pbrook9ee6e8b2007-11-11 00:04:49 +0000898 if (IS_M(env)) {
899 do_interrupt_v7m(env);
900 return;
901 }
bellardb5ff1b32005-11-26 10:38:39 +0000902 /* TODO: Vectored interrupt controller. */
903 switch (env->exception_index) {
904 case EXCP_UDEF:
905 new_mode = ARM_CPU_MODE_UND;
906 addr = 0x04;
907 mask = CPSR_I;
908 if (env->thumb)
909 offset = 2;
910 else
911 offset = 4;
912 break;
913 case EXCP_SWI:
pbrook8e716212007-01-20 17:12:09 +0000914 if (semihosting_enabled) {
915 /* Check for semihosting interrupt. */
916 if (env->thumb) {
Paul Brookd8fd2952012-03-30 18:02:50 +0100917 mask = arm_lduw_code(env->regs[15] - 2, env->bswap_code) & 0xff;
pbrook8e716212007-01-20 17:12:09 +0000918 } else {
Paul Brookd8fd2952012-03-30 18:02:50 +0100919 mask = arm_ldl_code(env->regs[15] - 4, env->bswap_code)
920 & 0xffffff;
pbrook8e716212007-01-20 17:12:09 +0000921 }
922 /* Only intercept calls from privileged modes, to provide some
923 semblance of security. */
924 if (((mask == 0x123456 && !env->thumb)
925 || (mask == 0xab && env->thumb))
926 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
927 env->regs[0] = do_arm_semihosting(env);
928 return;
929 }
930 }
bellardb5ff1b32005-11-26 10:38:39 +0000931 new_mode = ARM_CPU_MODE_SVC;
932 addr = 0x08;
933 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +0000934 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +0000935 offset = 0;
936 break;
pbrook06c949e2006-02-04 19:35:26 +0000937 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +0000938 /* See if this is a semihosting syscall. */
pbrook2ad207d2007-11-24 23:22:11 +0000939 if (env->thumb && semihosting_enabled) {
Paul Brookd8fd2952012-03-30 18:02:50 +0100940 mask = arm_lduw_code(env->regs[15], env->bswap_code) & 0xff;
pbrook9ee6e8b2007-11-11 00:04:49 +0000941 if (mask == 0xab
942 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
943 env->regs[15] += 2;
944 env->regs[0] = do_arm_semihosting(env);
945 return;
946 }
947 }
Alex Zuepke81c05da2011-06-03 18:42:17 +0200948 env->cp15.c5_insn = 2;
pbrook9ee6e8b2007-11-11 00:04:49 +0000949 /* Fall through to prefetch abort. */
950 case EXCP_PREFETCH_ABORT:
bellardb5ff1b32005-11-26 10:38:39 +0000951 new_mode = ARM_CPU_MODE_ABT;
952 addr = 0x0c;
953 mask = CPSR_A | CPSR_I;
954 offset = 4;
955 break;
956 case EXCP_DATA_ABORT:
957 new_mode = ARM_CPU_MODE_ABT;
958 addr = 0x10;
959 mask = CPSR_A | CPSR_I;
960 offset = 8;
961 break;
962 case EXCP_IRQ:
963 new_mode = ARM_CPU_MODE_IRQ;
964 addr = 0x18;
965 /* Disable IRQ and imprecise data aborts. */
966 mask = CPSR_A | CPSR_I;
967 offset = 4;
968 break;
969 case EXCP_FIQ:
970 new_mode = ARM_CPU_MODE_FIQ;
971 addr = 0x1c;
972 /* Disable FIQ, IRQ and imprecise data aborts. */
973 mask = CPSR_A | CPSR_I | CPSR_F;
974 offset = 4;
975 break;
976 default:
977 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
978 return; /* Never happens. Keep compiler happy. */
979 }
980 /* High vectors. */
981 if (env->cp15.c1_sys & (1 << 13)) {
982 addr += 0xffff0000;
983 }
984 switch_mode (env, new_mode);
985 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +0000986 /* Clear IT bits. */
987 env->condexec_bits = 0;
Rabin Vincent30a8cac2010-02-15 00:02:36 +0530988 /* Switch to the new mode, and to the correct instruction set. */
bellard6d7e6322005-12-18 16:54:08 +0000989 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
bellardb5ff1b32005-11-26 10:38:39 +0000990 env->uncached_cpsr |= mask;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400991 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
992 * and we should just guard the thumb mode on V4 */
993 if (arm_feature(env, ARM_FEATURE_V4T)) {
994 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
995 }
bellardb5ff1b32005-11-26 10:38:39 +0000996 env->regs[14] = env->regs[15] + offset;
997 env->regs[15] = addr;
998 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
999}
1000
1001/* Check section/page access permissions.
1002 Returns the page protection flags, or zero if the access is not
1003 permitted. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001004static inline int check_ap(CPUARMState *env, int ap, int domain_prot,
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001005 int access_type, int is_user)
bellardb5ff1b32005-11-26 10:38:39 +00001006{
pbrook9ee6e8b2007-11-11 00:04:49 +00001007 int prot_ro;
1008
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001009 if (domain_prot == 3) {
bellardb5ff1b32005-11-26 10:38:39 +00001010 return PAGE_READ | PAGE_WRITE;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001011 }
bellardb5ff1b32005-11-26 10:38:39 +00001012
pbrook9ee6e8b2007-11-11 00:04:49 +00001013 if (access_type == 1)
1014 prot_ro = 0;
1015 else
1016 prot_ro = PAGE_READ;
1017
bellardb5ff1b32005-11-26 10:38:39 +00001018 switch (ap) {
1019 case 0:
pbrook78600322006-09-09 14:36:26 +00001020 if (access_type == 1)
bellardb5ff1b32005-11-26 10:38:39 +00001021 return 0;
1022 switch ((env->cp15.c1_sys >> 8) & 3) {
1023 case 1:
1024 return is_user ? 0 : PAGE_READ;
1025 case 2:
1026 return PAGE_READ;
1027 default:
1028 return 0;
1029 }
1030 case 1:
1031 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
1032 case 2:
1033 if (is_user)
pbrook9ee6e8b2007-11-11 00:04:49 +00001034 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +00001035 else
1036 return PAGE_READ | PAGE_WRITE;
1037 case 3:
1038 return PAGE_READ | PAGE_WRITE;
pbrookd4934d12008-12-19 12:39:00 +00001039 case 4: /* Reserved. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001040 return 0;
1041 case 5:
1042 return is_user ? 0 : prot_ro;
1043 case 6:
1044 return prot_ro;
pbrookd4934d12008-12-19 12:39:00 +00001045 case 7:
Jamie Iles0ab06d82011-06-23 01:12:59 +00001046 if (!arm_feature (env, ARM_FEATURE_V6K))
pbrookd4934d12008-12-19 12:39:00 +00001047 return 0;
1048 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +00001049 default:
1050 abort();
1051 }
1052}
1053
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001054static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address)
pbrookb2fa1792008-10-22 19:22:30 +00001055{
1056 uint32_t table;
1057
1058 if (address & env->cp15.c2_mask)
1059 table = env->cp15.c2_base1 & 0xffffc000;
1060 else
1061 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1062
1063 table |= (address >> 18) & 0x3ffc;
1064 return table;
1065}
1066
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001067static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +00001068 int is_user, uint32_t *phys_ptr, int *prot,
1069 target_ulong *page_size)
bellardb5ff1b32005-11-26 10:38:39 +00001070{
1071 int code;
1072 uint32_t table;
1073 uint32_t desc;
1074 int type;
1075 int ap;
1076 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001077 int domain_prot;
bellardb5ff1b32005-11-26 10:38:39 +00001078 uint32_t phys_addr;
1079
pbrook9ee6e8b2007-11-11 00:04:49 +00001080 /* Pagetable walk. */
1081 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001082 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001083 desc = ldl_phys(table);
1084 type = (desc & 3);
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001085 domain = (desc >> 5) & 0x0f;
1086 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
pbrook9ee6e8b2007-11-11 00:04:49 +00001087 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001088 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001089 code = 5;
1090 goto do_fault;
1091 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001092 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001093 if (type == 2)
1094 code = 9; /* Section domain fault. */
1095 else
1096 code = 11; /* Page domain fault. */
1097 goto do_fault;
1098 }
1099 if (type == 2) {
1100 /* 1Mb section. */
1101 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1102 ap = (desc >> 10) & 3;
1103 code = 13;
Paul Brookd4c430a2010-03-17 02:14:28 +00001104 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +00001105 } else {
1106 /* Lookup l2 entry. */
1107 if (type == 1) {
1108 /* Coarse pagetable. */
1109 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1110 } else {
1111 /* Fine pagetable. */
1112 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1113 }
1114 desc = ldl_phys(table);
1115 switch (desc & 3) {
1116 case 0: /* Page translation fault. */
1117 code = 7;
1118 goto do_fault;
1119 case 1: /* 64k page. */
1120 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1121 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001122 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001123 break;
1124 case 2: /* 4k page. */
1125 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1126 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001127 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001128 break;
1129 case 3: /* 1k page. */
1130 if (type == 1) {
1131 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1132 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1133 } else {
1134 /* Page translation fault. */
1135 code = 7;
1136 goto do_fault;
1137 }
1138 } else {
1139 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1140 }
1141 ap = (desc >> 4) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001142 *page_size = 0x400;
pbrook9ee6e8b2007-11-11 00:04:49 +00001143 break;
1144 default:
1145 /* Never happens, but compiler isn't smart enough to tell. */
1146 abort();
1147 }
1148 code = 15;
1149 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001150 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
pbrook9ee6e8b2007-11-11 00:04:49 +00001151 if (!*prot) {
1152 /* Access permission fault. */
1153 goto do_fault;
1154 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301155 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001156 *phys_ptr = phys_addr;
1157 return 0;
1158do_fault:
1159 return code | (domain << 4);
1160}
1161
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001162static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +00001163 int is_user, uint32_t *phys_ptr, int *prot,
1164 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001165{
1166 int code;
1167 uint32_t table;
1168 uint32_t desc;
1169 uint32_t xn;
1170 int type;
1171 int ap;
1172 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001173 int domain_prot;
pbrook9ee6e8b2007-11-11 00:04:49 +00001174 uint32_t phys_addr;
1175
1176 /* Pagetable walk. */
1177 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001178 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001179 desc = ldl_phys(table);
1180 type = (desc & 3);
1181 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001182 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001183 code = 5;
1184 domain = 0;
1185 goto do_fault;
1186 } else if (type == 2 && (desc & (1 << 18))) {
1187 /* Supersection. */
1188 domain = 0;
1189 } else {
1190 /* Section or page. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001191 domain = (desc >> 5) & 0x0f;
pbrook9ee6e8b2007-11-11 00:04:49 +00001192 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001193 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1194 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001195 if (type == 2)
1196 code = 9; /* Section domain fault. */
1197 else
1198 code = 11; /* Page domain fault. */
1199 goto do_fault;
1200 }
1201 if (type == 2) {
1202 if (desc & (1 << 18)) {
1203 /* Supersection. */
1204 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001205 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001206 } else {
1207 /* Section. */
1208 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001209 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001210 }
1211 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1212 xn = desc & (1 << 4);
1213 code = 13;
1214 } else {
1215 /* Lookup l2 entry. */
1216 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1217 desc = ldl_phys(table);
1218 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1219 switch (desc & 3) {
1220 case 0: /* Page translation fault. */
1221 code = 7;
1222 goto do_fault;
1223 case 1: /* 64k page. */
1224 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1225 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +00001226 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001227 break;
1228 case 2: case 3: /* 4k page. */
1229 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1230 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +00001231 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001232 break;
1233 default:
1234 /* Never happens, but compiler isn't smart enough to tell. */
1235 abort();
1236 }
1237 code = 15;
1238 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001239 if (domain_prot == 3) {
Juha Riihimäkic0034322010-12-08 13:15:16 +02001240 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1241 } else {
1242 if (xn && access_type == 2)
1243 goto do_fault;
pbrook9ee6e8b2007-11-11 00:04:49 +00001244
Juha Riihimäkic0034322010-12-08 13:15:16 +02001245 /* The simplified model uses AP[0] as an access control bit. */
1246 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1247 /* Access flag fault. */
1248 code = (code == 15) ? 6 : 3;
1249 goto do_fault;
1250 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001251 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
Juha Riihimäkic0034322010-12-08 13:15:16 +02001252 if (!*prot) {
1253 /* Access permission fault. */
1254 goto do_fault;
1255 }
1256 if (!xn) {
1257 *prot |= PAGE_EXEC;
1258 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301259 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001260 *phys_ptr = phys_addr;
1261 return 0;
1262do_fault:
1263 return code | (domain << 4);
1264}
1265
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001266static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, int access_type,
pbrook9ee6e8b2007-11-11 00:04:49 +00001267 int is_user, uint32_t *phys_ptr, int *prot)
1268{
1269 int n;
1270 uint32_t mask;
1271 uint32_t base;
1272
1273 *phys_ptr = address;
1274 for (n = 7; n >= 0; n--) {
1275 base = env->cp15.c6_region[n];
1276 if ((base & 1) == 0)
1277 continue;
1278 mask = 1 << ((base >> 1) & 0x1f);
1279 /* Keep this shift separate from the above to avoid an
1280 (undefined) << 32. */
1281 mask = (mask << 1) - 1;
1282 if (((base ^ address) & ~mask) == 0)
1283 break;
1284 }
1285 if (n < 0)
1286 return 2;
1287
1288 if (access_type == 2) {
1289 mask = env->cp15.c5_insn;
1290 } else {
1291 mask = env->cp15.c5_data;
1292 }
1293 mask = (mask >> (n * 4)) & 0xf;
1294 switch (mask) {
1295 case 0:
1296 return 1;
1297 case 1:
1298 if (is_user)
1299 return 1;
1300 *prot = PAGE_READ | PAGE_WRITE;
1301 break;
1302 case 2:
1303 *prot = PAGE_READ;
1304 if (!is_user)
1305 *prot |= PAGE_WRITE;
1306 break;
1307 case 3:
1308 *prot = PAGE_READ | PAGE_WRITE;
1309 break;
1310 case 5:
1311 if (is_user)
1312 return 1;
1313 *prot = PAGE_READ;
1314 break;
1315 case 6:
1316 *prot = PAGE_READ;
1317 break;
1318 default:
1319 /* Bad permission. */
1320 return 1;
1321 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301322 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001323 return 0;
1324}
1325
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001326static inline int get_phys_addr(CPUARMState *env, uint32_t address,
pbrook9ee6e8b2007-11-11 00:04:49 +00001327 int access_type, int is_user,
Paul Brookd4c430a2010-03-17 02:14:28 +00001328 uint32_t *phys_ptr, int *prot,
1329 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001330{
bellardb5ff1b32005-11-26 10:38:39 +00001331 /* Fast Context Switch Extension. */
1332 if (address < 0x02000000)
1333 address += env->cp15.c13_fcse;
1334
1335 if ((env->cp15.c1_sys & 1) == 0) {
pbrookce819862007-05-08 02:30:40 +00001336 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00001337 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301338 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +00001339 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001340 return 0;
pbrookce819862007-05-08 02:30:40 +00001341 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
Paul Brookd4c430a2010-03-17 02:14:28 +00001342 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001343 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1344 prot);
1345 } else if (env->cp15.c1_sys & (1 << 23)) {
1346 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001347 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001348 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001349 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001350 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001351 }
bellardb5ff1b32005-11-26 10:38:39 +00001352}
1353
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001354int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address,
Blue Swirl97b348e2011-08-01 16:12:17 +00001355 int access_type, int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00001356{
1357 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001358 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001359 int prot;
j_mayer6ebbf392007-10-14 07:07:08 +00001360 int ret, is_user;
bellardb5ff1b32005-11-26 10:38:39 +00001361
j_mayer6ebbf392007-10-14 07:07:08 +00001362 is_user = mmu_idx == MMU_USER_IDX;
Paul Brookd4c430a2010-03-17 02:14:28 +00001363 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1364 &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001365 if (ret == 0) {
1366 /* Map a single [sub]page. */
1367 phys_addr &= ~(uint32_t)0x3ff;
1368 address &= ~(uint32_t)0x3ff;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301369 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
Paul Brookd4c430a2010-03-17 02:14:28 +00001370 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00001371 }
1372
1373 if (access_type == 2) {
1374 env->cp15.c5_insn = ret;
1375 env->cp15.c6_insn = address;
1376 env->exception_index = EXCP_PREFETCH_ABORT;
1377 } else {
1378 env->cp15.c5_data = ret;
pbrook9ee6e8b2007-11-11 00:04:49 +00001379 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1380 env->cp15.c5_data |= (1 << 11);
bellardb5ff1b32005-11-26 10:38:39 +00001381 env->cp15.c6_data = address;
1382 env->exception_index = EXCP_DATA_ABORT;
1383 }
1384 return 1;
1385}
1386
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001387target_phys_addr_t cpu_get_phys_page_debug(CPUARMState *env, target_ulong addr)
bellardb5ff1b32005-11-26 10:38:39 +00001388{
1389 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001390 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001391 int prot;
1392 int ret;
1393
Paul Brookd4c430a2010-03-17 02:14:28 +00001394 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001395
1396 if (ret != 0)
1397 return -1;
1398
1399 return phys_addr;
1400}
1401
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001402void HELPER(set_cp)(CPUARMState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +00001403{
1404 int cp_num = (insn >> 8) & 0xf;
1405 int cp_info = (insn >> 5) & 7;
1406 int src = (insn >> 16) & 0xf;
1407 int operand = insn & 0xf;
1408
1409 if (env->cp[cp_num].cp_write)
1410 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1411 cp_info, src, operand, val);
1412}
1413
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001414uint32_t HELPER(get_cp)(CPUARMState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +00001415{
1416 int cp_num = (insn >> 8) & 0xf;
1417 int cp_info = (insn >> 5) & 7;
1418 int dest = (insn >> 16) & 0xf;
1419 int operand = insn & 0xf;
1420
1421 if (env->cp[cp_num].cp_read)
1422 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1423 cp_info, dest, operand);
1424 return 0;
1425}
1426
pbrookce819862007-05-08 02:30:40 +00001427/* Return basic MPU access permission bits. */
1428static uint32_t simple_mpu_ap_bits(uint32_t val)
1429{
1430 uint32_t ret;
1431 uint32_t mask;
1432 int i;
1433 ret = 0;
1434 mask = 3;
1435 for (i = 0; i < 16; i += 2) {
1436 ret |= (val >> i) & mask;
1437 mask <<= 2;
1438 }
1439 return ret;
1440}
1441
1442/* Pad basic MPU access permission bits to extended format. */
1443static uint32_t extended_mpu_ap_bits(uint32_t val)
1444{
1445 uint32_t ret;
1446 uint32_t mask;
1447 int i;
1448 ret = 0;
1449 mask = 3;
1450 for (i = 0; i < 16; i += 2) {
1451 ret |= (val & mask) << i;
1452 mask <<= 2;
1453 }
1454 return ret;
1455}
1456
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001457void HELPER(set_cp15)(CPUARMState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +00001458{
pbrook9ee6e8b2007-11-11 00:04:49 +00001459 int op1;
1460 int op2;
1461 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001462
pbrook9ee6e8b2007-11-11 00:04:49 +00001463 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001464 op2 = (insn >> 5) & 7;
pbrookce819862007-05-08 02:30:40 +00001465 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001466 switch ((insn >> 16) & 0xf) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001467 case 0:
pbrook9ee6e8b2007-11-11 00:04:49 +00001468 /* ID codes. */
balrog610c3c82007-06-24 12:09:48 +00001469 if (arm_feature(env, ARM_FEATURE_XSCALE))
1470 break;
balrogc3d26892007-07-29 17:57:26 +00001471 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1472 break;
pbrooka49ea272008-12-19 13:37:53 +00001473 if (arm_feature(env, ARM_FEATURE_V7)
1474 && op1 == 2 && crm == 0 && op2 == 0) {
1475 env->cp15.c0_cssel = val & 0xf;
1476 break;
1477 }
bellardb5ff1b32005-11-26 10:38:39 +00001478 goto bad_reg;
1479 case 1: /* System configuration. */
Rob Herring2be27622012-01-13 17:25:08 +00001480 if (arm_feature(env, ARM_FEATURE_V7)
1481 && op1 == 0 && crm == 1 && op2 == 0) {
1482 env->cp15.c1_scr = val;
1483 break;
1484 }
balrogc3d26892007-07-29 17:57:26 +00001485 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1486 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001487 switch (op2) {
1488 case 0:
pbrookce819862007-05-08 02:30:40 +00001489 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
balrogc1713132007-04-30 01:26:42 +00001490 env->cp15.c1_sys = val;
bellardb5ff1b32005-11-26 10:38:39 +00001491 /* ??? Lots of these bits are not implemented. */
1492 /* This may enable/disable the MMU, so do a TLB flush. */
1493 tlb_flush(env, 1);
1494 break;
Stefan Weil61cc8702011-04-13 22:45:22 +02001495 case 1: /* Auxiliary control register. */
balrog610c3c82007-06-24 12:09:48 +00001496 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1497 env->cp15.c1_xscaleauxcr = val;
balrogc1713132007-04-30 01:26:42 +00001498 break;
balrog610c3c82007-06-24 12:09:48 +00001499 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001500 /* Not implemented. */
1501 break;
bellardb5ff1b32005-11-26 10:38:39 +00001502 case 2:
balrog610c3c82007-06-24 12:09:48 +00001503 if (arm_feature(env, ARM_FEATURE_XSCALE))
1504 goto bad_reg;
pbrook4be27db2008-10-22 16:14:08 +00001505 if (env->cp15.c1_coproc != val) {
1506 env->cp15.c1_coproc = val;
1507 /* ??? Is this safe when called from within a TB? */
1508 tb_flush(env);
1509 }
balrogc1713132007-04-30 01:26:42 +00001510 break;
bellardb5ff1b32005-11-26 10:38:39 +00001511 default:
1512 goto bad_reg;
1513 }
1514 break;
pbrookce819862007-05-08 02:30:40 +00001515 case 2: /* MMU Page table control / MPU cache control. */
1516 if (arm_feature(env, ARM_FEATURE_MPU)) {
1517 switch (op2) {
1518 case 0:
1519 env->cp15.c2_data = val;
1520 break;
1521 case 1:
1522 env->cp15.c2_insn = val;
1523 break;
1524 default:
1525 goto bad_reg;
1526 }
1527 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001528 switch (op2) {
1529 case 0:
1530 env->cp15.c2_base0 = val;
1531 break;
1532 case 1:
1533 env->cp15.c2_base1 = val;
1534 break;
1535 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001536 val &= 7;
1537 env->cp15.c2_control = val;
pbrook9ee6e8b2007-11-11 00:04:49 +00001538 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
pbrookb2fa1792008-10-22 19:22:30 +00001539 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
pbrook9ee6e8b2007-11-11 00:04:49 +00001540 break;
1541 default:
1542 goto bad_reg;
1543 }
pbrookce819862007-05-08 02:30:40 +00001544 }
bellardb5ff1b32005-11-26 10:38:39 +00001545 break;
pbrookce819862007-05-08 02:30:40 +00001546 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001547 env->cp15.c3 = val;
balrog405ee3a2007-10-31 00:47:13 +00001548 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
bellardb5ff1b32005-11-26 10:38:39 +00001549 break;
1550 case 4: /* Reserved. */
1551 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001552 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001553 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1554 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001555 switch (op2) {
1556 case 0:
pbrookce819862007-05-08 02:30:40 +00001557 if (arm_feature(env, ARM_FEATURE_MPU))
1558 val = extended_mpu_ap_bits(val);
bellardb5ff1b32005-11-26 10:38:39 +00001559 env->cp15.c5_data = val;
1560 break;
1561 case 1:
pbrookce819862007-05-08 02:30:40 +00001562 if (arm_feature(env, ARM_FEATURE_MPU))
1563 val = extended_mpu_ap_bits(val);
1564 env->cp15.c5_insn = val;
1565 break;
1566 case 2:
1567 if (!arm_feature(env, ARM_FEATURE_MPU))
1568 goto bad_reg;
1569 env->cp15.c5_data = val;
1570 break;
1571 case 3:
1572 if (!arm_feature(env, ARM_FEATURE_MPU))
1573 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001574 env->cp15.c5_insn = val;
1575 break;
1576 default:
1577 goto bad_reg;
1578 }
1579 break;
pbrookce819862007-05-08 02:30:40 +00001580 case 6: /* MMU Fault address / MPU base/size. */
1581 if (arm_feature(env, ARM_FEATURE_MPU)) {
1582 if (crm >= 8)
1583 goto bad_reg;
1584 env->cp15.c6_region[crm] = val;
1585 } else {
balrogc3d26892007-07-29 17:57:26 +00001586 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1587 op2 = 0;
pbrookce819862007-05-08 02:30:40 +00001588 switch (op2) {
1589 case 0:
1590 env->cp15.c6_data = val;
1591 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001592 case 1: /* ??? This is WFAR on armv6 */
1593 case 2:
pbrookce819862007-05-08 02:30:40 +00001594 env->cp15.c6_insn = val;
1595 break;
1596 default:
1597 goto bad_reg;
1598 }
bellardb5ff1b32005-11-26 10:38:39 +00001599 }
1600 break;
1601 case 7: /* Cache control. */
balrogc3d26892007-07-29 17:57:26 +00001602 env->cp15.c15_i_max = 0x000;
1603 env->cp15.c15_i_min = 0xff0;
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001604 if (op1 != 0) {
1605 goto bad_reg;
1606 }
1607 /* No cache, so nothing to do except VA->PA translations. */
Peter Maydell906879a2011-07-20 10:32:55 +00001608 if (arm_feature(env, ARM_FEATURE_VAPA)) {
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001609 switch (crm) {
1610 case 4:
1611 if (arm_feature(env, ARM_FEATURE_V7)) {
1612 env->cp15.c7_par = val & 0xfffff6ff;
1613 } else {
1614 env->cp15.c7_par = val & 0xfffff1ff;
1615 }
1616 break;
1617 case 8: {
1618 uint32_t phys_addr;
1619 target_ulong page_size;
1620 int prot;
1621 int ret, is_user = op2 & 2;
1622 int access_type = op2 & 1;
1623
1624 if (op2 & 4) {
1625 /* Other states are only available with TrustZone */
1626 goto bad_reg;
1627 }
1628 ret = get_phys_addr(env, val, access_type, is_user,
1629 &phys_addr, &prot, &page_size);
1630 if (ret == 0) {
1631 /* We do not set any attribute bits in the PAR */
1632 if (page_size == (1 << 24)
1633 && arm_feature(env, ARM_FEATURE_V7)) {
1634 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1635 } else {
1636 env->cp15.c7_par = phys_addr & 0xfffff000;
1637 }
1638 } else {
1639 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1640 ((ret & (12 << 1)) >> 6) |
1641 ((ret & 0xf) << 1) | 1;
1642 }
1643 break;
1644 }
1645 }
1646 }
bellardb5ff1b32005-11-26 10:38:39 +00001647 break;
1648 case 8: /* MMU TLB control. */
1649 switch (op2) {
Peter Maydelldc8714c2012-01-25 11:49:46 +00001650 case 0: /* Invalidate all (TLBIALL) */
1651 tlb_flush(env, 1);
bellardb5ff1b32005-11-26 10:38:39 +00001652 break;
Peter Maydelldc8714c2012-01-25 11:49:46 +00001653 case 1: /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
Paul Brookd4c430a2010-03-17 02:14:28 +00001654 tlb_flush_page(env, val & TARGET_PAGE_MASK);
bellardb5ff1b32005-11-26 10:38:39 +00001655 break;
Peter Maydelldc8714c2012-01-25 11:49:46 +00001656 case 2: /* Invalidate by ASID (TLBIASID) */
pbrook9ee6e8b2007-11-11 00:04:49 +00001657 tlb_flush(env, val == 0);
1658 break;
Peter Maydelldc8714c2012-01-25 11:49:46 +00001659 case 3: /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
1660 tlb_flush_page(env, val & TARGET_PAGE_MASK);
pbrook9ee6e8b2007-11-11 00:04:49 +00001661 break;
bellardb5ff1b32005-11-26 10:38:39 +00001662 default:
1663 goto bad_reg;
1664 }
1665 break;
pbrookce819862007-05-08 02:30:40 +00001666 case 9:
balrogc3d26892007-07-29 17:57:26 +00001667 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1668 break;
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +04001669 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1670 break; /* Ignore ReadBuffer access */
pbrookce819862007-05-08 02:30:40 +00001671 switch (crm) {
1672 case 0: /* Cache lockdown. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001673 switch (op1) {
1674 case 0: /* L1 cache. */
1675 switch (op2) {
1676 case 0:
1677 env->cp15.c9_data = val;
1678 break;
1679 case 1:
1680 env->cp15.c9_insn = val;
1681 break;
1682 default:
1683 goto bad_reg;
1684 }
1685 break;
1686 case 1: /* L2 cache. */
1687 /* Ignore writes to L2 lockdown/auxiliary registers. */
1688 break;
1689 default:
1690 goto bad_reg;
1691 }
1692 break;
pbrookce819862007-05-08 02:30:40 +00001693 case 1: /* TCM memory region registers. */
1694 /* Not implemented. */
1695 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00001696 case 12: /* Performance monitor control */
1697 /* Performance monitors are implementation defined in v7,
1698 * but with an ARM recommended set of registers, which we
1699 * follow (although we don't actually implement any counters)
1700 */
1701 if (!arm_feature(env, ARM_FEATURE_V7)) {
1702 goto bad_reg;
1703 }
1704 switch (op2) {
1705 case 0: /* performance monitor control register */
1706 /* only the DP, X, D and E bits are writable */
1707 env->cp15.c9_pmcr &= ~0x39;
1708 env->cp15.c9_pmcr |= (val & 0x39);
1709 break;
1710 case 1: /* Count enable set register */
1711 val &= (1 << 31);
1712 env->cp15.c9_pmcnten |= val;
1713 break;
1714 case 2: /* Count enable clear */
1715 val &= (1 << 31);
1716 env->cp15.c9_pmcnten &= ~val;
1717 break;
1718 case 3: /* Overflow flag status */
1719 env->cp15.c9_pmovsr &= ~val;
1720 break;
1721 case 4: /* Software increment */
1722 /* RAZ/WI since we don't implement the software-count event */
1723 break;
1724 case 5: /* Event counter selection register */
1725 /* Since we don't implement any events, writing to this register
1726 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1727 */
1728 break;
1729 default:
1730 goto bad_reg;
1731 }
1732 break;
1733 case 13: /* Performance counters */
1734 if (!arm_feature(env, ARM_FEATURE_V7)) {
1735 goto bad_reg;
1736 }
1737 switch (op2) {
1738 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1739 break;
1740 case 1: /* Event type select */
1741 env->cp15.c9_pmxevtyper = val & 0xff;
1742 break;
1743 case 2: /* Event count register */
1744 /* Unimplemented (we have no events), RAZ/WI */
1745 break;
1746 default:
1747 goto bad_reg;
1748 }
1749 break;
1750 case 14: /* Performance monitor control */
1751 if (!arm_feature(env, ARM_FEATURE_V7)) {
1752 goto bad_reg;
1753 }
1754 switch (op2) {
1755 case 0: /* user enable */
1756 env->cp15.c9_pmuserenr = val & 1;
1757 /* changes access rights for cp registers, so flush tbs */
1758 tb_flush(env);
1759 break;
1760 case 1: /* interrupt enable set */
1761 /* We have no event counters so only the C bit can be changed */
1762 val &= (1 << 31);
1763 env->cp15.c9_pminten |= val;
1764 break;
1765 case 2: /* interrupt enable clear */
1766 val &= (1 << 31);
1767 env->cp15.c9_pminten &= ~val;
1768 break;
1769 }
1770 break;
bellardb5ff1b32005-11-26 10:38:39 +00001771 default:
1772 goto bad_reg;
1773 }
1774 break;
1775 case 10: /* MMU TLB lockdown. */
1776 /* ??? TLB lockdown not implemented. */
1777 break;
bellardb5ff1b32005-11-26 10:38:39 +00001778 case 12: /* Reserved. */
1779 goto bad_reg;
1780 case 13: /* Process ID. */
1781 switch (op2) {
1782 case 0:
pbrookd07edbf2006-07-21 22:39:57 +00001783 /* Unlike real hardware the qemu TLB uses virtual addresses,
1784 not modified virtual addresses, so this causes a TLB flush.
1785 */
1786 if (env->cp15.c13_fcse != val)
1787 tlb_flush(env, 1);
1788 env->cp15.c13_fcse = val;
bellardb5ff1b32005-11-26 10:38:39 +00001789 break;
1790 case 1:
pbrookd07edbf2006-07-21 22:39:57 +00001791 /* This changes the ASID, so do a TLB flush. */
pbrookce819862007-05-08 02:30:40 +00001792 if (env->cp15.c13_context != val
1793 && !arm_feature(env, ARM_FEATURE_MPU))
pbrookd07edbf2006-07-21 22:39:57 +00001794 tlb_flush(env, 0);
1795 env->cp15.c13_context = val;
bellardb5ff1b32005-11-26 10:38:39 +00001796 break;
1797 default:
1798 goto bad_reg;
1799 }
1800 break;
Peter Maydell0383ac02012-01-25 12:42:29 +00001801 case 14: /* Generic timer */
1802 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
1803 /* Dummy implementation: RAZ/WI for all */
1804 break;
1805 }
bellardb5ff1b32005-11-26 10:38:39 +00001806 goto bad_reg;
1807 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00001808 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
pbrookce819862007-05-08 02:30:40 +00001809 if (op2 == 0 && crm == 1) {
balrog2e232132007-08-01 02:31:54 +00001810 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1811 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1812 tb_flush(env);
1813 env->cp15.c15_cpar = val & 0x3fff;
1814 }
balrogc1713132007-04-30 01:26:42 +00001815 break;
1816 }
1817 goto bad_reg;
1818 }
balrogc3d26892007-07-29 17:57:26 +00001819 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1820 switch (crm) {
1821 case 0:
1822 break;
1823 case 1: /* Set TI925T configuration. */
1824 env->cp15.c15_ticonfig = val & 0xe7;
1825 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1826 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1827 break;
1828 case 2: /* Set I_max. */
1829 env->cp15.c15_i_max = val;
1830 break;
1831 case 3: /* Set I_min. */
1832 env->cp15.c15_i_min = val;
1833 break;
1834 case 4: /* Set thread-ID. */
1835 env->cp15.c15_threadid = val & 0xffff;
1836 break;
1837 case 8: /* Wait-for-interrupt (deprecated). */
1838 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1839 break;
1840 default:
1841 goto bad_reg;
1842 }
1843 }
Mark Langsdorf7da362d2012-01-05 15:49:06 +00001844 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
1845 switch (crm) {
1846 case 0:
1847 if ((op1 == 0) && (op2 == 0)) {
1848 env->cp15.c15_power_control = val;
1849 } else if ((op1 == 0) && (op2 == 1)) {
1850 env->cp15.c15_diagnostic = val;
1851 } else if ((op1 == 0) && (op2 == 2)) {
1852 env->cp15.c15_power_diagnostic = val;
1853 }
1854 default:
1855 break;
1856 }
1857 }
bellardb5ff1b32005-11-26 10:38:39 +00001858 break;
1859 }
1860 return;
1861bad_reg:
1862 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001863 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1864 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00001865}
1866
Andreas Färber0ecb72a2012-03-14 01:38:21 +01001867uint32_t HELPER(get_cp15)(CPUARMState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +00001868{
pbrook9ee6e8b2007-11-11 00:04:49 +00001869 int op1;
1870 int op2;
1871 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001872
pbrook9ee6e8b2007-11-11 00:04:49 +00001873 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001874 op2 = (insn >> 5) & 7;
balrogc3d26892007-07-29 17:57:26 +00001875 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001876 switch ((insn >> 16) & 0xf) {
1877 case 0: /* ID codes. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001878 switch (op1) {
1879 case 0:
1880 switch (crm) {
1881 case 0:
1882 switch (op2) {
1883 case 0: /* Device ID. */
1884 return env->cp15.c0_cpuid;
1885 case 1: /* Cache Type. */
1886 return env->cp15.c0_cachetype;
1887 case 2: /* TCM status. */
1888 return 0;
1889 case 3: /* TLB type register. */
1890 return 0; /* No lockable TLB entries. */
Peter Maydell607b4b02011-02-03 19:43:23 +00001891 case 5: /* MPIDR */
1892 /* The MPIDR was standardised in v7; prior to
1893 * this it was implemented only in the 11MPCore.
1894 * For all other pre-v7 cores it does not exist.
1895 */
1896 if (arm_feature(env, ARM_FEATURE_V7) ||
1897 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1898 int mpidr = env->cpu_index;
1899 /* We don't support setting cluster ID ([8..11])
1900 * so these bits always RAZ.
1901 */
1902 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1903 mpidr |= (1 << 31);
1904 /* Cores which are uniprocessor (non-coherent)
1905 * but still implement the MP extensions set
1906 * bit 30. (For instance, A9UP.) However we do
1907 * not currently model any of those cores.
1908 */
1909 }
1910 return mpidr;
Paul Brook10055562009-11-19 16:45:20 +00001911 }
Peter Maydell607b4b02011-02-03 19:43:23 +00001912 /* otherwise fall through to the unimplemented-reg case */
pbrook9ee6e8b2007-11-11 00:04:49 +00001913 default:
1914 goto bad_reg;
1915 }
1916 case 1:
1917 if (!arm_feature(env, ARM_FEATURE_V6))
1918 goto bad_reg;
1919 return env->cp15.c0_c1[op2];
1920 case 2:
1921 if (!arm_feature(env, ARM_FEATURE_V6))
1922 goto bad_reg;
1923 return env->cp15.c0_c2[op2];
1924 case 3: case 4: case 5: case 6: case 7:
1925 return 0;
1926 default:
1927 goto bad_reg;
1928 }
1929 case 1:
1930 /* These registers aren't documented on arm11 cores. However
1931 Linux looks at them anyway. */
1932 if (!arm_feature(env, ARM_FEATURE_V6))
1933 goto bad_reg;
1934 if (crm != 0)
1935 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001936 if (!arm_feature(env, ARM_FEATURE_V7))
1937 return 0;
1938
1939 switch (op2) {
1940 case 0:
1941 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1942 case 1:
1943 return env->cp15.c0_clid;
1944 case 7:
1945 return 0;
1946 }
1947 goto bad_reg;
1948 case 2:
1949 if (op2 != 0 || crm != 0)
balrog610c3c82007-06-24 12:09:48 +00001950 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001951 return env->cp15.c0_cssel;
pbrook9ee6e8b2007-11-11 00:04:49 +00001952 default:
1953 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001954 }
1955 case 1: /* System configuration. */
Rob Herring2be27622012-01-13 17:25:08 +00001956 if (arm_feature(env, ARM_FEATURE_V7)
1957 && op1 == 0 && crm == 1 && op2 == 0) {
1958 return env->cp15.c1_scr;
1959 }
balrogc3d26892007-07-29 17:57:26 +00001960 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1961 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001962 switch (op2) {
1963 case 0: /* Control register. */
1964 return env->cp15.c1_sys;
1965 case 1: /* Auxiliary control register. */
balrogc1713132007-04-30 01:26:42 +00001966 if (arm_feature(env, ARM_FEATURE_XSCALE))
balrog610c3c82007-06-24 12:09:48 +00001967 return env->cp15.c1_xscaleauxcr;
pbrook9ee6e8b2007-11-11 00:04:49 +00001968 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1969 goto bad_reg;
1970 switch (ARM_CPUID(env)) {
1971 case ARM_CPUID_ARM1026:
1972 return 1;
1973 case ARM_CPUID_ARM1136:
balrog827df9f2008-04-14 21:05:22 +00001974 case ARM_CPUID_ARM1136_R2:
Jamie Iles7807eed2011-07-20 10:32:54 +00001975 case ARM_CPUID_ARM1176:
pbrook9ee6e8b2007-11-11 00:04:49 +00001976 return 7;
1977 case ARM_CPUID_ARM11MPCORE:
1978 return 1;
1979 case ARM_CPUID_CORTEXA8:
aurel32533d1772009-03-07 22:10:28 +00001980 return 2;
Paul Brook10055562009-11-19 16:45:20 +00001981 case ARM_CPUID_CORTEXA9:
Peter Maydell0b03bdf2012-01-25 12:42:29 +00001982 case ARM_CPUID_CORTEXA15:
Paul Brook10055562009-11-19 16:45:20 +00001983 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001984 default:
1985 goto bad_reg;
1986 }
bellardb5ff1b32005-11-26 10:38:39 +00001987 case 2: /* Coprocessor access register. */
balrog610c3c82007-06-24 12:09:48 +00001988 if (arm_feature(env, ARM_FEATURE_XSCALE))
1989 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001990 return env->cp15.c1_coproc;
1991 default:
1992 goto bad_reg;
1993 }
pbrookce819862007-05-08 02:30:40 +00001994 case 2: /* MMU Page table control / MPU cache control. */
1995 if (arm_feature(env, ARM_FEATURE_MPU)) {
1996 switch (op2) {
1997 case 0:
1998 return env->cp15.c2_data;
1999 break;
2000 case 1:
2001 return env->cp15.c2_insn;
2002 break;
2003 default:
2004 goto bad_reg;
2005 }
2006 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00002007 switch (op2) {
2008 case 0:
2009 return env->cp15.c2_base0;
2010 case 1:
2011 return env->cp15.c2_base1;
2012 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00002013 return env->cp15.c2_control;
pbrook9ee6e8b2007-11-11 00:04:49 +00002014 default:
2015 goto bad_reg;
2016 }
2017 }
pbrookce819862007-05-08 02:30:40 +00002018 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00002019 return env->cp15.c3;
2020 case 4: /* Reserved. */
2021 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00002022 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00002023 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2024 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00002025 switch (op2) {
2026 case 0:
pbrookce819862007-05-08 02:30:40 +00002027 if (arm_feature(env, ARM_FEATURE_MPU))
2028 return simple_mpu_ap_bits(env->cp15.c5_data);
bellardb5ff1b32005-11-26 10:38:39 +00002029 return env->cp15.c5_data;
2030 case 1:
pbrookce819862007-05-08 02:30:40 +00002031 if (arm_feature(env, ARM_FEATURE_MPU))
Peter Maydell4de47792012-03-14 12:26:10 +00002032 return simple_mpu_ap_bits(env->cp15.c5_insn);
pbrookce819862007-05-08 02:30:40 +00002033 return env->cp15.c5_insn;
2034 case 2:
2035 if (!arm_feature(env, ARM_FEATURE_MPU))
2036 goto bad_reg;
2037 return env->cp15.c5_data;
2038 case 3:
2039 if (!arm_feature(env, ARM_FEATURE_MPU))
2040 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00002041 return env->cp15.c5_insn;
2042 default:
2043 goto bad_reg;
2044 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002045 case 6: /* MMU Fault address. */
pbrookce819862007-05-08 02:30:40 +00002046 if (arm_feature(env, ARM_FEATURE_MPU)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002047 if (crm >= 8)
pbrookce819862007-05-08 02:30:40 +00002048 goto bad_reg;
pbrook9ee6e8b2007-11-11 00:04:49 +00002049 return env->cp15.c6_region[crm];
pbrookce819862007-05-08 02:30:40 +00002050 } else {
balrogc3d26892007-07-29 17:57:26 +00002051 if (arm_feature(env, ARM_FEATURE_OMAPCP))
2052 op2 = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002053 switch (op2) {
2054 case 0:
2055 return env->cp15.c6_data;
2056 case 1:
2057 if (arm_feature(env, ARM_FEATURE_V6)) {
2058 /* Watchpoint Fault Adrress. */
2059 return 0; /* Not implemented. */
2060 } else {
2061 /* Instruction Fault Adrress. */
2062 /* Arm9 doesn't have an IFAR, but implementing it anyway
2063 shouldn't do any harm. */
2064 return env->cp15.c6_insn;
2065 }
2066 case 2:
2067 if (arm_feature(env, ARM_FEATURE_V6)) {
2068 /* Instruction Fault Adrress. */
2069 return env->cp15.c6_insn;
2070 } else {
2071 goto bad_reg;
2072 }
2073 default:
2074 goto bad_reg;
2075 }
bellardb5ff1b32005-11-26 10:38:39 +00002076 }
2077 case 7: /* Cache control. */
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01002078 if (crm == 4 && op1 == 0 && op2 == 0) {
2079 return env->cp15.c7_par;
2080 }
pbrook6fbe23d2008-04-01 17:19:11 +00002081 /* FIXME: Should only clear Z flag if destination is r15. */
2082 env->ZF = 0;
bellardb5ff1b32005-11-26 10:38:39 +00002083 return 0;
2084 case 8: /* MMU TLB control. */
2085 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00002086 case 9:
2087 switch (crm) {
2088 case 0: /* Cache lockdown */
2089 switch (op1) {
2090 case 0: /* L1 cache. */
2091 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2092 return 0;
2093 }
2094 switch (op2) {
2095 case 0:
2096 return env->cp15.c9_data;
2097 case 1:
2098 return env->cp15.c9_insn;
2099 default:
2100 goto bad_reg;
2101 }
2102 case 1: /* L2 cache */
Peter Maydell0b03bdf2012-01-25 12:42:29 +00002103 /* L2 Lockdown and Auxiliary control. */
2104 switch (op2) {
2105 case 0:
2106 /* L2 cache lockdown (A8 only) */
2107 return 0;
2108 case 2:
2109 /* L2 cache auxiliary control (A8) or control (A15) */
2110 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA15) {
2111 /* Linux wants the number of processors from here.
2112 * Might as well set the interrupt-controller bit too.
2113 */
2114 return ((smp_cpus - 1) << 24) | (1 << 23);
2115 }
2116 return 0;
2117 case 3:
2118 /* L2 cache extended control (A15) */
2119 return 0;
2120 default:
Peter Maydell74594c92011-03-22 12:16:16 +00002121 goto bad_reg;
2122 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002123 default:
2124 goto bad_reg;
2125 }
Peter Maydell74594c92011-03-22 12:16:16 +00002126 break;
2127 case 12: /* Performance monitor control */
2128 if (!arm_feature(env, ARM_FEATURE_V7)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002129 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00002130 }
2131 switch (op2) {
2132 case 0: /* performance monitor control register */
2133 return env->cp15.c9_pmcr;
2134 case 1: /* count enable set */
2135 case 2: /* count enable clear */
2136 return env->cp15.c9_pmcnten;
2137 case 3: /* overflow flag status */
2138 return env->cp15.c9_pmovsr;
2139 case 4: /* software increment */
2140 case 5: /* event counter selection register */
2141 return 0; /* Unimplemented, RAZ/WI */
2142 default:
2143 goto bad_reg;
2144 }
2145 case 13: /* Performance counters */
2146 if (!arm_feature(env, ARM_FEATURE_V7)) {
2147 goto bad_reg;
2148 }
2149 switch (op2) {
2150 case 1: /* Event type select */
2151 return env->cp15.c9_pmxevtyper;
2152 case 0: /* Cycle count register */
2153 case 2: /* Event count register */
2154 /* Unimplemented, so RAZ/WI */
2155 return 0;
2156 default:
2157 goto bad_reg;
2158 }
2159 case 14: /* Performance monitor control */
2160 if (!arm_feature(env, ARM_FEATURE_V7)) {
2161 goto bad_reg;
2162 }
2163 switch (op2) {
2164 case 0: /* user enable */
2165 return env->cp15.c9_pmuserenr;
2166 case 1: /* interrupt enable set */
2167 case 2: /* interrupt enable clear */
2168 return env->cp15.c9_pminten;
2169 default:
2170 goto bad_reg;
2171 }
bellardb5ff1b32005-11-26 10:38:39 +00002172 default:
2173 goto bad_reg;
2174 }
Peter Maydell74594c92011-03-22 12:16:16 +00002175 break;
bellardb5ff1b32005-11-26 10:38:39 +00002176 case 10: /* MMU TLB lockdown. */
2177 /* ??? TLB lockdown not implemented. */
2178 return 0;
2179 case 11: /* TCM DMA control. */
2180 case 12: /* Reserved. */
2181 goto bad_reg;
2182 case 13: /* Process ID. */
2183 switch (op2) {
2184 case 0:
2185 return env->cp15.c13_fcse;
2186 case 1:
2187 return env->cp15.c13_context;
2188 default:
2189 goto bad_reg;
2190 }
Peter Maydell0383ac02012-01-25 12:42:29 +00002191 case 14: /* Generic timer */
2192 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
2193 /* Dummy implementation: RAZ/WI for all */
2194 return 0;
2195 }
bellardb5ff1b32005-11-26 10:38:39 +00002196 goto bad_reg;
2197 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00002198 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
balrogc3d26892007-07-29 17:57:26 +00002199 if (op2 == 0 && crm == 1)
balrogc1713132007-04-30 01:26:42 +00002200 return env->cp15.c15_cpar;
2201
2202 goto bad_reg;
2203 }
balrogc3d26892007-07-29 17:57:26 +00002204 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2205 switch (crm) {
2206 case 0:
2207 return 0;
2208 case 1: /* Read TI925T configuration. */
2209 return env->cp15.c15_ticonfig;
2210 case 2: /* Read I_max. */
2211 return env->cp15.c15_i_max;
2212 case 3: /* Read I_min. */
2213 return env->cp15.c15_i_min;
2214 case 4: /* Read thread-ID. */
2215 return env->cp15.c15_threadid;
2216 case 8: /* TI925T_status */
2217 return 0;
2218 }
balrog827df9f2008-04-14 21:05:22 +00002219 /* TODO: Peripheral port remap register:
2220 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2221 * controller base address at $rn & ~0xfff and map size of
2222 * 0x200 << ($rn & 0xfff), when MMU is off. */
balrogc3d26892007-07-29 17:57:26 +00002223 goto bad_reg;
2224 }
Mark Langsdorf7da362d2012-01-05 15:49:06 +00002225 if (ARM_CPUID(env) == ARM_CPUID_CORTEXA9) {
2226 switch (crm) {
2227 case 0:
2228 if ((op1 == 4) && (op2 == 0)) {
2229 /* The config_base_address should hold the value of
2230 * the peripheral base. ARM should get this from a CPU
2231 * object property, but that support isn't available in
2232 * December 2011. Default to 0 for now and board models
2233 * that care can set it by a private hook */
2234 return env->cp15.c15_config_base_address;
2235 } else if ((op1 == 0) && (op2 == 0)) {
2236 /* power_control should be set to maximum latency. Again,
2237 default to 0 and set by private hook */
2238 return env->cp15.c15_power_control;
2239 } else if ((op1 == 0) && (op2 == 1)) {
2240 return env->cp15.c15_diagnostic;
2241 } else if ((op1 == 0) && (op2 == 2)) {
2242 return env->cp15.c15_power_diagnostic;
2243 }
2244 break;
2245 case 1: /* NEON Busy */
2246 return 0;
2247 case 5: /* tlb lockdown */
2248 case 6:
2249 case 7:
2250 if ((op1 == 5) && (op2 == 2)) {
2251 return 0;
2252 }
2253 break;
2254 default:
2255 break;
2256 }
2257 goto bad_reg;
2258 }
bellardb5ff1b32005-11-26 10:38:39 +00002259 return 0;
2260 }
2261bad_reg:
2262 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002263 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2264 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00002265 return 0;
2266}
2267
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002268void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002269{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002270 if ((env->uncached_cpsr & CPSR_M) == mode) {
2271 env->regs[13] = val;
2272 } else {
Peter Maydell1b9e01c2012-01-05 15:49:06 +00002273 env->banked_r13[bank_number(env, mode)] = val;
Peter Maydell39ea3d42011-01-14 20:39:18 +01002274 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002275}
2276
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002277uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00002278{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002279 if ((env->uncached_cpsr & CPSR_M) == mode) {
2280 return env->regs[13];
2281 } else {
Peter Maydell1b9e01c2012-01-05 15:49:06 +00002282 return env->banked_r13[bank_number(env, mode)];
Peter Maydell39ea3d42011-01-14 20:39:18 +01002283 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002284}
2285
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002286uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00002287{
2288 switch (reg) {
2289 case 0: /* APSR */
2290 return xpsr_read(env) & 0xf8000000;
2291 case 1: /* IAPSR */
2292 return xpsr_read(env) & 0xf80001ff;
2293 case 2: /* EAPSR */
2294 return xpsr_read(env) & 0xff00fc00;
2295 case 3: /* xPSR */
2296 return xpsr_read(env) & 0xff00fdff;
2297 case 5: /* IPSR */
2298 return xpsr_read(env) & 0x000001ff;
2299 case 6: /* EPSR */
2300 return xpsr_read(env) & 0x0700fc00;
2301 case 7: /* IEPSR */
2302 return xpsr_read(env) & 0x0700edff;
2303 case 8: /* MSP */
2304 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2305 case 9: /* PSP */
2306 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2307 case 16: /* PRIMASK */
2308 return (env->uncached_cpsr & CPSR_I) != 0;
Sebastian Huber82845822011-05-29 02:58:41 +00002309 case 17: /* BASEPRI */
2310 case 18: /* BASEPRI_MAX */
pbrook9ee6e8b2007-11-11 00:04:49 +00002311 return env->v7m.basepri;
Sebastian Huber82845822011-05-29 02:58:41 +00002312 case 19: /* FAULTMASK */
2313 return (env->uncached_cpsr & CPSR_F) != 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002314 case 20: /* CONTROL */
2315 return env->v7m.control;
2316 default:
2317 /* ??? For debugging only. */
2318 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2319 return 0;
2320 }
2321}
2322
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002323void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002324{
2325 switch (reg) {
2326 case 0: /* APSR */
2327 xpsr_write(env, val, 0xf8000000);
2328 break;
2329 case 1: /* IAPSR */
2330 xpsr_write(env, val, 0xf8000000);
2331 break;
2332 case 2: /* EAPSR */
2333 xpsr_write(env, val, 0xfe00fc00);
2334 break;
2335 case 3: /* xPSR */
2336 xpsr_write(env, val, 0xfe00fc00);
2337 break;
2338 case 5: /* IPSR */
2339 /* IPSR bits are readonly. */
2340 break;
2341 case 6: /* EPSR */
2342 xpsr_write(env, val, 0x0600fc00);
2343 break;
2344 case 7: /* IEPSR */
2345 xpsr_write(env, val, 0x0600fc00);
2346 break;
2347 case 8: /* MSP */
2348 if (env->v7m.current_sp)
2349 env->v7m.other_sp = val;
2350 else
2351 env->regs[13] = val;
2352 break;
2353 case 9: /* PSP */
2354 if (env->v7m.current_sp)
2355 env->regs[13] = val;
2356 else
2357 env->v7m.other_sp = val;
2358 break;
2359 case 16: /* PRIMASK */
2360 if (val & 1)
2361 env->uncached_cpsr |= CPSR_I;
2362 else
2363 env->uncached_cpsr &= ~CPSR_I;
2364 break;
Sebastian Huber82845822011-05-29 02:58:41 +00002365 case 17: /* BASEPRI */
2366 env->v7m.basepri = val & 0xff;
2367 break;
2368 case 18: /* BASEPRI_MAX */
2369 val &= 0xff;
2370 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2371 env->v7m.basepri = val;
2372 break;
2373 case 19: /* FAULTMASK */
pbrook9ee6e8b2007-11-11 00:04:49 +00002374 if (val & 1)
2375 env->uncached_cpsr |= CPSR_F;
2376 else
2377 env->uncached_cpsr &= ~CPSR_F;
2378 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00002379 case 20: /* CONTROL */
2380 env->v7m.control = val & 3;
2381 switch_v7m_sp(env, (val & 2) != 0);
2382 break;
2383 default:
2384 /* ??? For debugging only. */
2385 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2386 return;
2387 }
2388}
2389
balrogc1713132007-04-30 01:26:42 +00002390void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
2391 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
2392 void *opaque)
2393{
2394 if (cpnum < 0 || cpnum > 14) {
2395 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2396 return;
2397 }
2398
2399 env->cp[cpnum].cp_read = cp_read;
2400 env->cp[cpnum].cp_write = cp_write;
2401 env->cp[cpnum].opaque = opaque;
2402}
2403
bellardb5ff1b32005-11-26 10:38:39 +00002404#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00002405
2406/* Note that signed overflow is undefined in C. The following routines are
2407 careful to use unsigned types where modulo arithmetic is required.
2408 Failure to do so _will_ break on newer gcc. */
2409
2410/* Signed saturating arithmetic. */
2411
aurel321654b2d2008-04-11 04:55:07 +00002412/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002413static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2414{
2415 uint16_t res;
2416
2417 res = a + b;
2418 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2419 if (a & 0x8000)
2420 res = 0x8000;
2421 else
2422 res = 0x7fff;
2423 }
2424 return res;
2425}
2426
aurel321654b2d2008-04-11 04:55:07 +00002427/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002428static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2429{
2430 uint8_t res;
2431
2432 res = a + b;
2433 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2434 if (a & 0x80)
2435 res = 0x80;
2436 else
2437 res = 0x7f;
2438 }
2439 return res;
2440}
2441
aurel321654b2d2008-04-11 04:55:07 +00002442/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002443static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2444{
2445 uint16_t res;
2446
2447 res = a - b;
2448 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2449 if (a & 0x8000)
2450 res = 0x8000;
2451 else
2452 res = 0x7fff;
2453 }
2454 return res;
2455}
2456
aurel321654b2d2008-04-11 04:55:07 +00002457/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002458static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2459{
2460 uint8_t res;
2461
2462 res = a - b;
2463 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2464 if (a & 0x80)
2465 res = 0x80;
2466 else
2467 res = 0x7f;
2468 }
2469 return res;
2470}
2471
2472#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2473#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2474#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2475#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2476#define PFX q
2477
2478#include "op_addsub.h"
2479
2480/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00002481static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002482{
2483 uint16_t res;
2484 res = a + b;
2485 if (res < a)
2486 res = 0xffff;
2487 return res;
2488}
2489
pbrook460a09c2008-05-01 12:04:35 +00002490static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002491{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002492 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002493 return a - b;
2494 else
2495 return 0;
2496}
2497
2498static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2499{
2500 uint8_t res;
2501 res = a + b;
2502 if (res < a)
2503 res = 0xff;
2504 return res;
2505}
2506
2507static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2508{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002509 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002510 return a - b;
2511 else
2512 return 0;
2513}
2514
2515#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2516#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2517#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2518#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2519#define PFX uq
2520
2521#include "op_addsub.h"
2522
2523/* Signed modulo arithmetic. */
2524#define SARITH16(a, b, n, op) do { \
2525 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002526 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002527 RESULT(sum, n, 16); \
2528 if (sum >= 0) \
2529 ge |= 3 << (n * 2); \
2530 } while(0)
2531
2532#define SARITH8(a, b, n, op) do { \
2533 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002534 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002535 RESULT(sum, n, 8); \
2536 if (sum >= 0) \
2537 ge |= 1 << n; \
2538 } while(0)
2539
2540
2541#define ADD16(a, b, n) SARITH16(a, b, n, +)
2542#define SUB16(a, b, n) SARITH16(a, b, n, -)
2543#define ADD8(a, b, n) SARITH8(a, b, n, +)
2544#define SUB8(a, b, n) SARITH8(a, b, n, -)
2545#define PFX s
2546#define ARITH_GE
2547
2548#include "op_addsub.h"
2549
2550/* Unsigned modulo arithmetic. */
2551#define ADD16(a, b, n) do { \
2552 uint32_t sum; \
2553 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2554 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00002555 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00002556 ge |= 3 << (n * 2); \
2557 } while(0)
2558
2559#define ADD8(a, b, n) do { \
2560 uint32_t sum; \
2561 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2562 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00002563 if ((sum >> 8) == 1) \
2564 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002565 } while(0)
2566
2567#define SUB16(a, b, n) do { \
2568 uint32_t sum; \
2569 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2570 RESULT(sum, n, 16); \
2571 if ((sum >> 16) == 0) \
2572 ge |= 3 << (n * 2); \
2573 } while(0)
2574
2575#define SUB8(a, b, n) do { \
2576 uint32_t sum; \
2577 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2578 RESULT(sum, n, 8); \
2579 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00002580 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002581 } while(0)
2582
2583#define PFX u
2584#define ARITH_GE
2585
2586#include "op_addsub.h"
2587
2588/* Halved signed arithmetic. */
2589#define ADD16(a, b, n) \
2590 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2591#define SUB16(a, b, n) \
2592 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2593#define ADD8(a, b, n) \
2594 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2595#define SUB8(a, b, n) \
2596 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2597#define PFX sh
2598
2599#include "op_addsub.h"
2600
2601/* Halved unsigned arithmetic. */
2602#define ADD16(a, b, n) \
2603 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2604#define SUB16(a, b, n) \
2605 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2606#define ADD8(a, b, n) \
2607 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2608#define SUB8(a, b, n) \
2609 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2610#define PFX uh
2611
2612#include "op_addsub.h"
2613
2614static inline uint8_t do_usad(uint8_t a, uint8_t b)
2615{
2616 if (a > b)
2617 return a - b;
2618 else
2619 return b - a;
2620}
2621
2622/* Unsigned sum of absolute byte differences. */
2623uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2624{
2625 uint32_t sum;
2626 sum = do_usad(a, b);
2627 sum += do_usad(a >> 8, b >> 8);
2628 sum += do_usad(a >> 16, b >>16);
2629 sum += do_usad(a >> 24, b >> 24);
2630 return sum;
2631}
2632
2633/* For ARMv6 SEL instruction. */
2634uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2635{
2636 uint32_t mask;
2637
2638 mask = 0;
2639 if (flags & 1)
2640 mask |= 0xff;
2641 if (flags & 2)
2642 mask |= 0xff00;
2643 if (flags & 4)
2644 mask |= 0xff0000;
2645 if (flags & 8)
2646 mask |= 0xff000000;
2647 return (a & mask) | (b & ~mask);
2648}
2649
pbrook5e3f8782008-03-31 03:47:34 +00002650uint32_t HELPER(logicq_cc)(uint64_t val)
2651{
2652 return (val >> 32) | (val != 0);
2653}
pbrook4373f3c2008-03-31 03:47:19 +00002654
2655/* VFP support. We follow the convention used for VFP instrunctions:
2656 Single precition routines have a "s" suffix, double precision a
2657 "d" suffix. */
2658
2659/* Convert host exception flags to vfp form. */
2660static inline int vfp_exceptbits_from_host(int host_bits)
2661{
2662 int target_bits = 0;
2663
2664 if (host_bits & float_flag_invalid)
2665 target_bits |= 1;
2666 if (host_bits & float_flag_divbyzero)
2667 target_bits |= 2;
2668 if (host_bits & float_flag_overflow)
2669 target_bits |= 4;
Peter Maydell36802b62011-05-19 14:46:18 +01002670 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
pbrook4373f3c2008-03-31 03:47:19 +00002671 target_bits |= 8;
2672 if (host_bits & float_flag_inexact)
2673 target_bits |= 0x10;
Peter Maydellcecd8502011-01-06 19:37:55 +00002674 if (host_bits & float_flag_input_denormal)
2675 target_bits |= 0x80;
pbrook4373f3c2008-03-31 03:47:19 +00002676 return target_bits;
2677}
2678
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002679uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002680{
2681 int i;
2682 uint32_t fpscr;
2683
2684 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2685 | (env->vfp.vec_len << 16)
2686 | (env->vfp.vec_stride << 20);
2687 i = get_float_exception_flags(&env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002688 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002689 fpscr |= vfp_exceptbits_from_host(i);
2690 return fpscr;
2691}
2692
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002693uint32_t vfp_get_fpscr(CPUARMState *env)
Peter Maydell01653292010-11-24 15:20:04 +00002694{
2695 return HELPER(vfp_get_fpscr)(env);
2696}
2697
pbrook4373f3c2008-03-31 03:47:19 +00002698/* Convert vfp exception flags to target form. */
2699static inline int vfp_exceptbits_to_host(int target_bits)
2700{
2701 int host_bits = 0;
2702
2703 if (target_bits & 1)
2704 host_bits |= float_flag_invalid;
2705 if (target_bits & 2)
2706 host_bits |= float_flag_divbyzero;
2707 if (target_bits & 4)
2708 host_bits |= float_flag_overflow;
2709 if (target_bits & 8)
2710 host_bits |= float_flag_underflow;
2711 if (target_bits & 0x10)
2712 host_bits |= float_flag_inexact;
Peter Maydellcecd8502011-01-06 19:37:55 +00002713 if (target_bits & 0x80)
2714 host_bits |= float_flag_input_denormal;
pbrook4373f3c2008-03-31 03:47:19 +00002715 return host_bits;
2716}
2717
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002718void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
pbrook4373f3c2008-03-31 03:47:19 +00002719{
2720 int i;
2721 uint32_t changed;
2722
2723 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2724 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2725 env->vfp.vec_len = (val >> 16) & 7;
2726 env->vfp.vec_stride = (val >> 20) & 3;
2727
2728 changed ^= val;
2729 if (changed & (3 << 22)) {
2730 i = (val >> 22) & 3;
2731 switch (i) {
2732 case 0:
2733 i = float_round_nearest_even;
2734 break;
2735 case 1:
2736 i = float_round_up;
2737 break;
2738 case 2:
2739 i = float_round_down;
2740 break;
2741 case 3:
2742 i = float_round_to_zero;
2743 break;
2744 }
2745 set_float_rounding_mode(i, &env->vfp.fp_status);
2746 }
Peter Maydellcecd8502011-01-06 19:37:55 +00002747 if (changed & (1 << 24)) {
pbrookfe76d972008-12-19 14:33:59 +00002748 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
Peter Maydellcecd8502011-01-06 19:37:55 +00002749 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2750 }
pbrook5c7908e2008-12-19 13:53:37 +00002751 if (changed & (1 << 25))
2752 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002753
Peter Maydellb12c3902011-01-06 19:37:54 +00002754 i = vfp_exceptbits_to_host(val);
pbrook4373f3c2008-03-31 03:47:19 +00002755 set_float_exception_flags(i, &env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002756 set_float_exception_flags(0, &env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002757}
2758
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002759void vfp_set_fpscr(CPUARMState *env, uint32_t val)
Peter Maydell01653292010-11-24 15:20:04 +00002760{
2761 HELPER(vfp_set_fpscr)(env, val);
2762}
2763
pbrook4373f3c2008-03-31 03:47:19 +00002764#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2765
2766#define VFP_BINOP(name) \
Peter Maydellae1857e2011-05-25 14:51:48 +00002767float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002768{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002769 float_status *fpst = fpstp; \
2770 return float32_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002771} \
Peter Maydellae1857e2011-05-25 14:51:48 +00002772float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002773{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002774 float_status *fpst = fpstp; \
2775 return float64_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002776}
2777VFP_BINOP(add)
2778VFP_BINOP(sub)
2779VFP_BINOP(mul)
2780VFP_BINOP(div)
2781#undef VFP_BINOP
2782
2783float32 VFP_HELPER(neg, s)(float32 a)
2784{
2785 return float32_chs(a);
2786}
2787
2788float64 VFP_HELPER(neg, d)(float64 a)
2789{
balrog66230e02008-04-20 00:58:01 +00002790 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002791}
2792
2793float32 VFP_HELPER(abs, s)(float32 a)
2794{
2795 return float32_abs(a);
2796}
2797
2798float64 VFP_HELPER(abs, d)(float64 a)
2799{
balrog66230e02008-04-20 00:58:01 +00002800 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002801}
2802
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002803float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002804{
2805 return float32_sqrt(a, &env->vfp.fp_status);
2806}
2807
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002808float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002809{
2810 return float64_sqrt(a, &env->vfp.fp_status);
2811}
2812
2813/* XXX: check quiet/signaling case */
2814#define DO_VFP_cmp(p, type) \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002815void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00002816{ \
2817 uint32_t flags; \
2818 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2819 case 0: flags = 0x6; break; \
2820 case -1: flags = 0x8; break; \
2821 case 1: flags = 0x2; break; \
2822 default: case 2: flags = 0x3; break; \
2823 } \
2824 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2825 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2826} \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002827void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00002828{ \
2829 uint32_t flags; \
2830 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2831 case 0: flags = 0x6; break; \
2832 case -1: flags = 0x8; break; \
2833 case 1: flags = 0x2; break; \
2834 default: case 2: flags = 0x3; break; \
2835 } \
2836 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2837 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2838}
2839DO_VFP_cmp(s, float32)
2840DO_VFP_cmp(d, float64)
2841#undef DO_VFP_cmp
2842
Peter Maydell5500b062011-05-19 14:46:19 +01002843/* Integer to float and float to integer conversions */
2844
2845#define CONV_ITOF(name, fsz, sign) \
2846 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2847{ \
2848 float_status *fpst = fpstp; \
Peter Maydell85836972012-01-25 11:49:46 +00002849 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002850}
2851
Peter Maydell5500b062011-05-19 14:46:19 +01002852#define CONV_FTOI(name, fsz, sign, round) \
2853uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2854{ \
2855 float_status *fpst = fpstp; \
2856 if (float##fsz##_is_any_nan(x)) { \
2857 float_raise(float_flag_invalid, fpst); \
2858 return 0; \
2859 } \
2860 return float##fsz##_to_##sign##int32##round(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002861}
2862
Peter Maydell5500b062011-05-19 14:46:19 +01002863#define FLOAT_CONVS(name, p, fsz, sign) \
2864CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2865CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2866CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
pbrook4373f3c2008-03-31 03:47:19 +00002867
Peter Maydell5500b062011-05-19 14:46:19 +01002868FLOAT_CONVS(si, s, 32, )
2869FLOAT_CONVS(si, d, 64, )
2870FLOAT_CONVS(ui, s, 32, u)
2871FLOAT_CONVS(ui, d, 64, u)
pbrook4373f3c2008-03-31 03:47:19 +00002872
Peter Maydell5500b062011-05-19 14:46:19 +01002873#undef CONV_ITOF
2874#undef CONV_FTOI
2875#undef FLOAT_CONVS
pbrook4373f3c2008-03-31 03:47:19 +00002876
2877/* floating point conversion */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002878float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002879{
Peter Maydell2d627732010-12-07 15:37:34 +00002880 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2881 /* ARM requires that S<->D conversion of any kind of NaN generates
2882 * a quiet NaN by forcing the most significant frac bit to 1.
2883 */
2884 return float64_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002885}
2886
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002887float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002888{
Peter Maydell2d627732010-12-07 15:37:34 +00002889 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2890 /* ARM requires that S<->D conversion of any kind of NaN generates
2891 * a quiet NaN by forcing the most significant frac bit to 1.
2892 */
2893 return float32_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002894}
2895
2896/* VFP3 fixed point conversion. */
Peter Maydell622465e2011-03-14 07:23:11 +00002897#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
Peter Maydell5500b062011-05-19 14:46:19 +01002898float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2899 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002900{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002901 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002902 float##fsz tmp; \
Peter Maydell5500b062011-05-19 14:46:19 +01002903 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2904 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002905} \
Peter Maydell5500b062011-05-19 14:46:19 +01002906uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2907 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002908{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002909 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002910 float##fsz tmp; \
2911 if (float##fsz##_is_any_nan(x)) { \
Peter Maydell5500b062011-05-19 14:46:19 +01002912 float_raise(float_flag_invalid, fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00002913 return 0; \
Peter Maydell09d94872010-12-07 15:37:34 +00002914 } \
Peter Maydell5500b062011-05-19 14:46:19 +01002915 tmp = float##fsz##_scalbn(x, shift, fpst); \
2916 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002917}
2918
Peter Maydell622465e2011-03-14 07:23:11 +00002919VFP_CONV_FIX(sh, d, 64, int16, )
2920VFP_CONV_FIX(sl, d, 64, int32, )
2921VFP_CONV_FIX(uh, d, 64, uint16, u)
2922VFP_CONV_FIX(ul, d, 64, uint32, u)
2923VFP_CONV_FIX(sh, s, 32, int16, )
2924VFP_CONV_FIX(sl, s, 32, int32, )
2925VFP_CONV_FIX(uh, s, 32, uint16, u)
2926VFP_CONV_FIX(ul, s, 32, uint32, u)
pbrook4373f3c2008-03-31 03:47:19 +00002927#undef VFP_CONV_FIX
2928
Paul Brook60011492009-11-19 16:45:20 +00002929/* Half precision conversions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002930static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002931{
Paul Brook60011492009-11-19 16:45:20 +00002932 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002933 float32 r = float16_to_float32(make_float16(a), ieee, s);
2934 if (ieee) {
2935 return float32_maybe_silence_nan(r);
2936 }
2937 return r;
Paul Brook60011492009-11-19 16:45:20 +00002938}
2939
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002940static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002941{
Paul Brook60011492009-11-19 16:45:20 +00002942 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002943 float16 r = float32_to_float16(a, ieee, s);
2944 if (ieee) {
2945 r = float16_maybe_silence_nan(r);
2946 }
2947 return float16_val(r);
Paul Brook60011492009-11-19 16:45:20 +00002948}
2949
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002950float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002951{
2952 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2953}
2954
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002955uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002956{
2957 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2958}
2959
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002960float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002961{
2962 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2963}
2964
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002965uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00002966{
2967 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2968}
2969
Peter Maydelldda3ec42011-03-14 15:37:12 +00002970#define float32_two make_float32(0x40000000)
Peter Maydell6aae3df2011-03-14 15:37:13 +00002971#define float32_three make_float32(0x40400000)
2972#define float32_one_point_five make_float32(0x3fc00000)
Peter Maydelldda3ec42011-03-14 15:37:12 +00002973
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002974float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002975{
Peter Maydelldda3ec42011-03-14 15:37:12 +00002976 float_status *s = &env->vfp.standard_fp_status;
2977 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2978 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002979 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2980 float_raise(float_flag_input_denormal, s);
2981 }
Peter Maydelldda3ec42011-03-14 15:37:12 +00002982 return float32_two;
2983 }
2984 return float32_sub(float32_two, float32_mul(a, b, s), s);
pbrook4373f3c2008-03-31 03:47:19 +00002985}
2986
Andreas Färber0ecb72a2012-03-14 01:38:21 +01002987float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00002988{
Peter Maydell71826962011-01-14 20:39:18 +01002989 float_status *s = &env->vfp.standard_fp_status;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002990 float32 product;
2991 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2992 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002993 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2994 float_raise(float_flag_input_denormal, s);
2995 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002996 return float32_one_point_five;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002997 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002998 product = float32_mul(a, b, s);
2999 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
pbrook4373f3c2008-03-31 03:47:19 +00003000}
3001
pbrook8f8e3aa2008-03-31 03:48:01 +00003002/* NEON helpers. */
3003
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01003004/* Constants 256 and 512 are used in some helpers; we avoid relying on
3005 * int->float conversions at run-time. */
3006#define float64_256 make_float64(0x4070000000000000LL)
3007#define float64_512 make_float64(0x4080000000000000LL)
3008
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003009/* The algorithm that must be used to calculate the estimate
3010 * is specified by the ARM ARM.
3011 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003012static float64 recip_estimate(float64 a, CPUARMState *env)
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003013{
Peter Maydell1146a812011-05-19 14:46:14 +01003014 /* These calculations mustn't set any fp exception flags,
3015 * so we use a local copy of the fp_status.
3016 */
3017 float_status dummy_status = env->vfp.standard_fp_status;
3018 float_status *s = &dummy_status;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003019 /* q = (int)(a * 512.0) */
3020 float64 q = float64_mul(float64_512, a, s);
3021 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3022
3023 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
3024 q = int64_to_float64(q_int, s);
3025 q = float64_add(q, float64_half, s);
3026 q = float64_div(q, float64_512, s);
3027 q = float64_div(float64_one, q, s);
3028
3029 /* s = (int)(256.0 * r + 0.5) */
3030 q = float64_mul(q, float64_256, s);
3031 q = float64_add(q, float64_half, s);
3032 q_int = float64_to_int64_round_to_zero(q, s);
3033
3034 /* return (double)s / 256.0 */
3035 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3036}
3037
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003038float32 HELPER(recpe_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003039{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003040 float_status *s = &env->vfp.standard_fp_status;
3041 float64 f64;
3042 uint32_t val32 = float32_val(a);
3043
3044 int result_exp;
3045 int a_exp = (val32 & 0x7f800000) >> 23;
3046 int sign = val32 & 0x80000000;
3047
3048 if (float32_is_any_nan(a)) {
3049 if (float32_is_signaling_nan(a)) {
3050 float_raise(float_flag_invalid, s);
3051 }
3052 return float32_default_nan;
3053 } else if (float32_is_infinity(a)) {
3054 return float32_set_sign(float32_zero, float32_is_neg(a));
3055 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003056 if (!float32_is_zero(a)) {
3057 float_raise(float_flag_input_denormal, s);
3058 }
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003059 float_raise(float_flag_divbyzero, s);
3060 return float32_set_sign(float32_infinity, float32_is_neg(a));
3061 } else if (a_exp >= 253) {
3062 float_raise(float_flag_underflow, s);
3063 return float32_set_sign(float32_zero, float32_is_neg(a));
3064 }
3065
3066 f64 = make_float64((0x3feULL << 52)
3067 | ((int64_t)(val32 & 0x7fffff) << 29));
3068
3069 result_exp = 253 - a_exp;
3070
3071 f64 = recip_estimate(f64, env);
3072
3073 val32 = sign
3074 | ((result_exp & 0xff) << 23)
3075 | ((float64_val(f64) >> 29) & 0x7fffff);
3076 return make_float32(val32);
pbrook4373f3c2008-03-31 03:47:19 +00003077}
3078
Christophe Lyone07be5d2011-02-21 17:38:48 +01003079/* The algorithm that must be used to calculate the estimate
3080 * is specified by the ARM ARM.
3081 */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003082static float64 recip_sqrt_estimate(float64 a, CPUARMState *env)
Christophe Lyone07be5d2011-02-21 17:38:48 +01003083{
Peter Maydell1146a812011-05-19 14:46:14 +01003084 /* These calculations mustn't set any fp exception flags,
3085 * so we use a local copy of the fp_status.
3086 */
3087 float_status dummy_status = env->vfp.standard_fp_status;
3088 float_status *s = &dummy_status;
Christophe Lyone07be5d2011-02-21 17:38:48 +01003089 float64 q;
3090 int64_t q_int;
3091
3092 if (float64_lt(a, float64_half, s)) {
3093 /* range 0.25 <= a < 0.5 */
3094
3095 /* a in units of 1/512 rounded down */
3096 /* q0 = (int)(a * 512.0); */
3097 q = float64_mul(float64_512, a, s);
3098 q_int = float64_to_int64_round_to_zero(q, s);
3099
3100 /* reciprocal root r */
3101 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
3102 q = int64_to_float64(q_int, s);
3103 q = float64_add(q, float64_half, s);
3104 q = float64_div(q, float64_512, s);
3105 q = float64_sqrt(q, s);
3106 q = float64_div(float64_one, q, s);
3107 } else {
3108 /* range 0.5 <= a < 1.0 */
3109
3110 /* a in units of 1/256 rounded down */
3111 /* q1 = (int)(a * 256.0); */
3112 q = float64_mul(float64_256, a, s);
3113 int64_t q_int = float64_to_int64_round_to_zero(q, s);
3114
3115 /* reciprocal root r */
3116 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
3117 q = int64_to_float64(q_int, s);
3118 q = float64_add(q, float64_half, s);
3119 q = float64_div(q, float64_256, s);
3120 q = float64_sqrt(q, s);
3121 q = float64_div(float64_one, q, s);
3122 }
3123 /* r in units of 1/256 rounded to nearest */
3124 /* s = (int)(256.0 * r + 0.5); */
3125
3126 q = float64_mul(q, float64_256,s );
3127 q = float64_add(q, float64_half, s);
3128 q_int = float64_to_int64_round_to_zero(q, s);
3129
3130 /* return (double)s / 256.0;*/
3131 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3132}
3133
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003134float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003135{
Christophe Lyone07be5d2011-02-21 17:38:48 +01003136 float_status *s = &env->vfp.standard_fp_status;
3137 int result_exp;
3138 float64 f64;
3139 uint32_t val;
3140 uint64_t val64;
3141
3142 val = float32_val(a);
3143
3144 if (float32_is_any_nan(a)) {
3145 if (float32_is_signaling_nan(a)) {
3146 float_raise(float_flag_invalid, s);
3147 }
3148 return float32_default_nan;
3149 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003150 if (!float32_is_zero(a)) {
3151 float_raise(float_flag_input_denormal, s);
3152 }
Christophe Lyone07be5d2011-02-21 17:38:48 +01003153 float_raise(float_flag_divbyzero, s);
3154 return float32_set_sign(float32_infinity, float32_is_neg(a));
3155 } else if (float32_is_neg(a)) {
3156 float_raise(float_flag_invalid, s);
3157 return float32_default_nan;
3158 } else if (float32_is_infinity(a)) {
3159 return float32_zero;
3160 }
3161
3162 /* Normalize to a double-precision value between 0.25 and 1.0,
3163 * preserving the parity of the exponent. */
3164 if ((val & 0x800000) == 0) {
3165 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3166 | (0x3feULL << 52)
3167 | ((uint64_t)(val & 0x7fffff) << 29));
3168 } else {
3169 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3170 | (0x3fdULL << 52)
3171 | ((uint64_t)(val & 0x7fffff) << 29));
3172 }
3173
3174 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3175
3176 f64 = recip_sqrt_estimate(f64, env);
3177
3178 val64 = float64_val(f64);
3179
Christophe LYON26cc6ab2011-10-19 16:14:05 +00003180 val = ((result_exp & 0xff) << 23)
Christophe Lyone07be5d2011-02-21 17:38:48 +01003181 | ((val64 >> 29) & 0x7fffff);
3182 return make_float32(val);
pbrook4373f3c2008-03-31 03:47:19 +00003183}
3184
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003185uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003186{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003187 float64 f64;
3188
3189 if ((a & 0x80000000) == 0) {
3190 return 0xffffffff;
3191 }
3192
3193 f64 = make_float64((0x3feULL << 52)
3194 | ((int64_t)(a & 0x7fffffff) << 21));
3195
3196 f64 = recip_estimate (f64, env);
3197
3198 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003199}
3200
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003201uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00003202{
Christophe Lyone07be5d2011-02-21 17:38:48 +01003203 float64 f64;
3204
3205 if ((a & 0xc0000000) == 0) {
3206 return 0xffffffff;
3207 }
3208
3209 if (a & 0x80000000) {
3210 f64 = make_float64((0x3feULL << 52)
3211 | ((uint64_t)(a & 0x7fffffff) << 21));
3212 } else { /* bits 31-30 == '01' */
3213 f64 = make_float64((0x3fdULL << 52)
3214 | ((uint64_t)(a & 0x3fffffff) << 22));
3215 }
3216
3217 f64 = recip_sqrt_estimate(f64, env);
3218
3219 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003220}
pbrookfe1479c2008-12-19 13:18:36 +00003221
Peter Maydellda97f522011-10-19 16:14:07 +00003222/* VFPv4 fused multiply-accumulate */
3223float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3224{
3225 float_status *fpst = fpstp;
3226 return float32_muladd(a, b, c, 0, fpst);
3227}
3228
3229float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3230{
3231 float_status *fpst = fpstp;
3232 return float64_muladd(a, b, c, 0, fpst);
3233}
3234
Andreas Färber0ecb72a2012-03-14 01:38:21 +01003235void HELPER(set_teecr)(CPUARMState *env, uint32_t val)
pbrookfe1479c2008-12-19 13:18:36 +00003236{
3237 val &= 1;
3238 if (env->teecr != val) {
3239 env->teecr = val;
3240 tb_flush(env);
3241 }
3242}