blob: 1949202f27284275b9204100d6882b8dfc4d0558 [file] [log] [blame]
bellardb5ff1b32005-11-26 10:38:39 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#include "cpu.h"
pbrook9ee6e8b2007-11-11 00:04:49 +00006#include "gdbstub.h"
LluĂ­s7b592202011-04-13 18:38:24 +02007#include "helper.h"
aurel32ca10f862008-04-11 21:35:42 +00008#include "qemu-common.h"
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +02009#include "host-utils.h"
Paul Brook4f78c9a2010-04-05 19:56:34 +010010#if !defined(CONFIG_USER_ONLY)
Paul Brook983fe822010-04-05 19:34:51 +010011#include "hw/loader.h"
Paul Brook4f78c9a2010-04-05 19:56:34 +010012#endif
pbrook9ee6e8b2007-11-11 00:04:49 +000013
Paul Brook10055562009-11-19 16:45:20 +000014static uint32_t cortexa9_cp15_c0_c1[8] =
15{ 0x1031, 0x11, 0x000, 0, 0x00100103, 0x20000000, 0x01230000, 0x00002111 };
16
17static uint32_t cortexa9_cp15_c0_c2[8] =
18{ 0x00101111, 0x13112111, 0x21232041, 0x11112131, 0x00111142, 0, 0, 0 };
19
pbrook9ee6e8b2007-11-11 00:04:49 +000020static uint32_t cortexa8_cp15_c0_c1[8] =
21{ 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 };
22
23static uint32_t cortexa8_cp15_c0_c2[8] =
24{ 0x00101111, 0x12112111, 0x21232031, 0x11112131, 0x00111142, 0, 0, 0 };
25
26static uint32_t mpcore_cp15_c0_c1[8] =
27{ 0x111, 0x1, 0, 0x2, 0x01100103, 0x10020302, 0x01222000, 0 };
28
29static uint32_t mpcore_cp15_c0_c2[8] =
30{ 0x00100011, 0x12002111, 0x11221011, 0x01102131, 0x141, 0, 0, 0 };
31
32static uint32_t arm1136_cp15_c0_c1[8] =
33{ 0x111, 0x1, 0x2, 0x3, 0x01130003, 0x10030302, 0x01222110, 0 };
34
35static uint32_t arm1136_cp15_c0_c2[8] =
36{ 0x00140011, 0x12002111, 0x11231111, 0x01102131, 0x141, 0, 0, 0 };
bellardb5ff1b32005-11-26 10:38:39 +000037
Jamie Iles7807eed2011-07-20 10:32:54 +000038static uint32_t arm1176_cp15_c0_c1[8] =
39{ 0x111, 0x11, 0x33, 0, 0x01130003, 0x10030302, 0x01222100, 0 };
40
41static uint32_t arm1176_cp15_c0_c2[8] =
42{ 0x0140011, 0x12002111, 0x11231121, 0x01102131, 0x01141, 0, 0, 0 };
43
bellardaaed9092007-11-10 15:15:54 +000044static uint32_t cpu_arm_find_by_name(const char *name);
45
pbrookf3d6b952007-03-11 13:03:18 +000046static inline void set_feature(CPUARMState *env, int feature)
47{
48 env->features |= 1u << feature;
49}
50
51static void cpu_reset_model_id(CPUARMState *env, uint32_t id)
52{
53 env->cp15.c0_cpuid = id;
54 switch (id) {
55 case ARM_CPUID_ARM926:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040056 set_feature(env, ARM_FEATURE_V4T);
57 set_feature(env, ARM_FEATURE_V5);
pbrookf3d6b952007-03-11 13:03:18 +000058 set_feature(env, ARM_FEATURE_VFP);
59 env->vfp.xregs[ARM_VFP_FPSID] = 0x41011090;
balrogc1713132007-04-30 01:26:42 +000060 env->cp15.c0_cachetype = 0x1dd20d2;
balrog610c3c82007-06-24 12:09:48 +000061 env->cp15.c1_sys = 0x00090078;
pbrookf3d6b952007-03-11 13:03:18 +000062 break;
pbrookce819862007-05-08 02:30:40 +000063 case ARM_CPUID_ARM946:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040064 set_feature(env, ARM_FEATURE_V4T);
65 set_feature(env, ARM_FEATURE_V5);
pbrookce819862007-05-08 02:30:40 +000066 set_feature(env, ARM_FEATURE_MPU);
67 env->cp15.c0_cachetype = 0x0f004006;
balrog610c3c82007-06-24 12:09:48 +000068 env->cp15.c1_sys = 0x00000078;
pbrookce819862007-05-08 02:30:40 +000069 break;
pbrookf3d6b952007-03-11 13:03:18 +000070 case ARM_CPUID_ARM1026:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040071 set_feature(env, ARM_FEATURE_V4T);
72 set_feature(env, ARM_FEATURE_V5);
pbrookf3d6b952007-03-11 13:03:18 +000073 set_feature(env, ARM_FEATURE_VFP);
74 set_feature(env, ARM_FEATURE_AUXCR);
75 env->vfp.xregs[ARM_VFP_FPSID] = 0x410110a0;
balrogc1713132007-04-30 01:26:42 +000076 env->cp15.c0_cachetype = 0x1dd20d2;
balrog610c3c82007-06-24 12:09:48 +000077 env->cp15.c1_sys = 0x00090078;
balrogc1713132007-04-30 01:26:42 +000078 break;
pbrook9ee6e8b2007-11-11 00:04:49 +000079 case ARM_CPUID_ARM1136:
Peter Maydell906879a2011-07-20 10:32:55 +000080 /* This is the 1136 r1, which is a v6K core */
81 set_feature(env, ARM_FEATURE_V6K);
82 /* Fall through */
83 case ARM_CPUID_ARM1136_R2:
84 /* What qemu calls "arm1136_r2" is actually the 1136 r0p2, ie an
85 * older core than plain "arm1136". In particular this does not
86 * have the v6K features.
87 */
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040088 set_feature(env, ARM_FEATURE_V4T);
89 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +000090 set_feature(env, ARM_FEATURE_V6);
91 set_feature(env, ARM_FEATURE_VFP);
92 set_feature(env, ARM_FEATURE_AUXCR);
Peter Maydell906879a2011-07-20 10:32:55 +000093 /* These ID register values are correct for 1136 but may be wrong
94 * for 1136_r2 (in particular r0p2 does not actually implement most
95 * of the ID registers).
96 */
pbrook9ee6e8b2007-11-11 00:04:49 +000097 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
98 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
99 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
100 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000101 memcpy(env->cp15.c0_c2, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +0000102 env->cp15.c0_cachetype = 0x1dd20d2;
Juha Riihimäki16440c52010-12-08 13:15:18 +0200103 env->cp15.c1_sys = 0x00050078;
pbrook9ee6e8b2007-11-11 00:04:49 +0000104 break;
Jamie Iles7807eed2011-07-20 10:32:54 +0000105 case ARM_CPUID_ARM1176:
106 set_feature(env, ARM_FEATURE_V4T);
107 set_feature(env, ARM_FEATURE_V5);
108 set_feature(env, ARM_FEATURE_V6);
109 set_feature(env, ARM_FEATURE_V6K);
110 set_feature(env, ARM_FEATURE_VFP);
111 set_feature(env, ARM_FEATURE_AUXCR);
Peter Maydell906879a2011-07-20 10:32:55 +0000112 set_feature(env, ARM_FEATURE_VAPA);
Jamie Iles7807eed2011-07-20 10:32:54 +0000113 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b5;
114 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
115 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
116 memcpy(env->cp15.c0_c1, arm1176_cp15_c0_c1, 8 * sizeof(uint32_t));
117 memcpy(env->cp15.c0_c2, arm1176_cp15_c0_c2, 8 * sizeof(uint32_t));
118 env->cp15.c0_cachetype = 0x1dd20d2;
119 env->cp15.c1_sys = 0x00050078;
120 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000121 case ARM_CPUID_ARM11MPCORE:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400122 set_feature(env, ARM_FEATURE_V4T);
123 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +0000124 set_feature(env, ARM_FEATURE_V6);
125 set_feature(env, ARM_FEATURE_V6K);
126 set_feature(env, ARM_FEATURE_VFP);
127 set_feature(env, ARM_FEATURE_AUXCR);
Peter Maydell906879a2011-07-20 10:32:55 +0000128 set_feature(env, ARM_FEATURE_VAPA);
pbrook9ee6e8b2007-11-11 00:04:49 +0000129 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
130 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
131 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
132 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000133 memcpy(env->cp15.c0_c2, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +0000134 env->cp15.c0_cachetype = 0x1dd20d2;
135 break;
136 case ARM_CPUID_CORTEXA8:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400137 set_feature(env, ARM_FEATURE_V4T);
138 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +0000139 set_feature(env, ARM_FEATURE_V6);
140 set_feature(env, ARM_FEATURE_V6K);
141 set_feature(env, ARM_FEATURE_V7);
142 set_feature(env, ARM_FEATURE_AUXCR);
143 set_feature(env, ARM_FEATURE_THUMB2);
144 set_feature(env, ARM_FEATURE_VFP);
145 set_feature(env, ARM_FEATURE_VFP3);
146 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +0000147 set_feature(env, ARM_FEATURE_THUMB2EE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000148 env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
149 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
150 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
151 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000152 memcpy(env->cp15.c0_c2, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrooka49ea272008-12-19 13:37:53 +0000153 env->cp15.c0_cachetype = 0x82048004;
154 env->cp15.c0_clid = (1 << 27) | (2 << 24) | 3;
155 env->cp15.c0_ccsid[0] = 0xe007e01a; /* 16k L1 dcache. */
156 env->cp15.c0_ccsid[1] = 0x2007e01a; /* 16k L1 icache. */
157 env->cp15.c0_ccsid[2] = 0xf0000000; /* No L2 icache. */
Mattias Holm9c486ad2010-12-08 13:15:17 +0200158 env->cp15.c1_sys = 0x00c50078;
pbrook9ee6e8b2007-11-11 00:04:49 +0000159 break;
Paul Brook10055562009-11-19 16:45:20 +0000160 case ARM_CPUID_CORTEXA9:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400161 set_feature(env, ARM_FEATURE_V4T);
162 set_feature(env, ARM_FEATURE_V5);
Paul Brook10055562009-11-19 16:45:20 +0000163 set_feature(env, ARM_FEATURE_V6);
164 set_feature(env, ARM_FEATURE_V6K);
165 set_feature(env, ARM_FEATURE_V7);
166 set_feature(env, ARM_FEATURE_AUXCR);
167 set_feature(env, ARM_FEATURE_THUMB2);
168 set_feature(env, ARM_FEATURE_VFP);
169 set_feature(env, ARM_FEATURE_VFP3);
170 set_feature(env, ARM_FEATURE_VFP_FP16);
171 set_feature(env, ARM_FEATURE_NEON);
172 set_feature(env, ARM_FEATURE_THUMB2EE);
Peter Maydelle1bbf442011-02-03 19:43:22 +0000173 /* Note that A9 supports the MP extensions even for
174 * A9UP and single-core A9MP (which are both different
175 * and valid configurations; we don't model A9UP).
176 */
177 set_feature(env, ARM_FEATURE_V7MP);
Paul Brook10055562009-11-19 16:45:20 +0000178 env->vfp.xregs[ARM_VFP_FPSID] = 0x41034000; /* Guess */
179 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
180 env->vfp.xregs[ARM_VFP_MVFR1] = 0x01111111;
181 memcpy(env->cp15.c0_c1, cortexa9_cp15_c0_c1, 8 * sizeof(uint32_t));
182 memcpy(env->cp15.c0_c2, cortexa9_cp15_c0_c2, 8 * sizeof(uint32_t));
183 env->cp15.c0_cachetype = 0x80038003;
184 env->cp15.c0_clid = (1 << 27) | (1 << 24) | 3;
185 env->cp15.c0_ccsid[0] = 0xe00fe015; /* 16k L1 dcache. */
186 env->cp15.c0_ccsid[1] = 0x200fe015; /* 16k L1 icache. */
Juha Riihimäki16440c52010-12-08 13:15:18 +0200187 env->cp15.c1_sys = 0x00c50078;
Paul Brook10055562009-11-19 16:45:20 +0000188 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000189 case ARM_CPUID_CORTEXM3:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400190 set_feature(env, ARM_FEATURE_V4T);
191 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +0000192 set_feature(env, ARM_FEATURE_V6);
193 set_feature(env, ARM_FEATURE_THUMB2);
194 set_feature(env, ARM_FEATURE_V7);
195 set_feature(env, ARM_FEATURE_M);
Peter Maydell47789992011-10-19 16:14:06 +0000196 set_feature(env, ARM_FEATURE_THUMB_DIV);
pbrook9ee6e8b2007-11-11 00:04:49 +0000197 break;
198 case ARM_CPUID_ANY: /* For userspace emulation. */
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400199 set_feature(env, ARM_FEATURE_V4T);
200 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +0000201 set_feature(env, ARM_FEATURE_V6);
202 set_feature(env, ARM_FEATURE_V6K);
203 set_feature(env, ARM_FEATURE_V7);
204 set_feature(env, ARM_FEATURE_THUMB2);
205 set_feature(env, ARM_FEATURE_VFP);
206 set_feature(env, ARM_FEATURE_VFP3);
Peter Maydellda97f522011-10-19 16:14:07 +0000207 set_feature(env, ARM_FEATURE_VFP4);
Paul Brook60011492009-11-19 16:45:20 +0000208 set_feature(env, ARM_FEATURE_VFP_FP16);
pbrook9ee6e8b2007-11-11 00:04:49 +0000209 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +0000210 set_feature(env, ARM_FEATURE_THUMB2EE);
Peter Maydellb8b8ea02011-10-19 16:14:06 +0000211 set_feature(env, ARM_FEATURE_ARM_DIV);
Peter Maydelle1bbf442011-02-03 19:43:22 +0000212 set_feature(env, ARM_FEATURE_V7MP);
pbrook9ee6e8b2007-11-11 00:04:49 +0000213 break;
balrogc3d26892007-07-29 17:57:26 +0000214 case ARM_CPUID_TI915T:
215 case ARM_CPUID_TI925T:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400216 set_feature(env, ARM_FEATURE_V4T);
balrogc3d26892007-07-29 17:57:26 +0000217 set_feature(env, ARM_FEATURE_OMAPCP);
218 env->cp15.c0_cpuid = ARM_CPUID_TI925T; /* Depends on wiring. */
219 env->cp15.c0_cachetype = 0x5109149;
220 env->cp15.c1_sys = 0x00000070;
221 env->cp15.c15_i_max = 0x000;
222 env->cp15.c15_i_min = 0xff0;
223 break;
balrogc1713132007-04-30 01:26:42 +0000224 case ARM_CPUID_PXA250:
225 case ARM_CPUID_PXA255:
226 case ARM_CPUID_PXA260:
227 case ARM_CPUID_PXA261:
228 case ARM_CPUID_PXA262:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400229 set_feature(env, ARM_FEATURE_V4T);
230 set_feature(env, ARM_FEATURE_V5);
balrogc1713132007-04-30 01:26:42 +0000231 set_feature(env, ARM_FEATURE_XSCALE);
232 /* JTAG_ID is ((id << 28) | 0x09265013) */
233 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000234 env->cp15.c1_sys = 0x00000078;
balrogc1713132007-04-30 01:26:42 +0000235 break;
236 case ARM_CPUID_PXA270_A0:
237 case ARM_CPUID_PXA270_A1:
238 case ARM_CPUID_PXA270_B0:
239 case ARM_CPUID_PXA270_B1:
240 case ARM_CPUID_PXA270_C0:
241 case ARM_CPUID_PXA270_C5:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400242 set_feature(env, ARM_FEATURE_V4T);
243 set_feature(env, ARM_FEATURE_V5);
balrogc1713132007-04-30 01:26:42 +0000244 set_feature(env, ARM_FEATURE_XSCALE);
245 /* JTAG_ID is ((id << 28) | 0x09265013) */
balrog18c9b562007-04-30 02:02:17 +0000246 set_feature(env, ARM_FEATURE_IWMMXT);
247 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
balrogc1713132007-04-30 01:26:42 +0000248 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000249 env->cp15.c1_sys = 0x00000078;
pbrookf3d6b952007-03-11 13:03:18 +0000250 break;
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +0400251 case ARM_CPUID_SA1100:
252 case ARM_CPUID_SA1110:
253 set_feature(env, ARM_FEATURE_STRONGARM);
254 env->cp15.c1_sys = 0x00000070;
255 break;
pbrookf3d6b952007-03-11 13:03:18 +0000256 default:
257 cpu_abort(env, "Bad CPU ID: %x\n", id);
258 break;
259 }
Peter Maydell906879a2011-07-20 10:32:55 +0000260
261 /* Some features automatically imply others: */
262 if (arm_feature(env, ARM_FEATURE_V7)) {
263 set_feature(env, ARM_FEATURE_VAPA);
264 }
Peter Maydellb8b8ea02011-10-19 16:14:06 +0000265 if (arm_feature(env, ARM_FEATURE_ARM_DIV)) {
266 set_feature(env, ARM_FEATURE_THUMB_DIV);
267 }
pbrookf3d6b952007-03-11 13:03:18 +0000268}
269
pbrook40f137e2006-02-20 00:33:36 +0000270void cpu_reset(CPUARMState *env)
271{
pbrookf3d6b952007-03-11 13:03:18 +0000272 uint32_t id;
aliguorieca1bdf2009-01-26 19:54:31 +0000273
274 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
275 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
276 log_cpu_state(env, 0);
277 }
278
pbrookf3d6b952007-03-11 13:03:18 +0000279 id = env->cp15.c0_cpuid;
280 memset(env, 0, offsetof(CPUARMState, breakpoints));
281 if (id)
282 cpu_reset_model_id(env, id);
pbrook40f137e2006-02-20 00:33:36 +0000283#if defined (CONFIG_USER_ONLY)
284 env->uncached_cpsr = ARM_CPU_MODE_USR;
Peter Maydell3a807de2010-11-19 15:36:47 +0000285 /* For user mode we must enable access to coprocessors */
pbrook40f137e2006-02-20 00:33:36 +0000286 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
Peter Maydell3a807de2010-11-19 15:36:47 +0000287 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
288 env->cp15.c15_cpar = 3;
289 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
290 env->cp15.c15_cpar = 1;
291 }
pbrook40f137e2006-02-20 00:33:36 +0000292#else
293 /* SVC mode with interrupts disabled. */
294 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
pbrook9ee6e8b2007-11-11 00:04:49 +0000295 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
Paul Brook983fe822010-04-05 19:34:51 +0100296 clear at reset. Initial SP and PC are loaded from ROM. */
297 if (IS_M(env)) {
298 uint32_t pc;
299 uint8_t *rom;
pbrook9ee6e8b2007-11-11 00:04:49 +0000300 env->uncached_cpsr &= ~CPSR_I;
Paul Brook983fe822010-04-05 19:34:51 +0100301 rom = rom_ptr(0);
302 if (rom) {
303 /* We should really use ldl_phys here, in case the guest
304 modified flash and reset itself. However images
Andreas Färber297d1b42011-10-02 18:35:48 +0200305 loaded via -kernel have not been copied yet, so load the
Paul Brook983fe822010-04-05 19:34:51 +0100306 values directly from there. */
307 env->regs[13] = ldl_p(rom);
308 pc = ldl_p(rom + 4);
309 env->thumb = pc & 1;
310 env->regs[15] = pc & ~1;
311 }
312 }
pbrook40f137e2006-02-20 00:33:36 +0000313 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
pbrookb2fa1792008-10-22 19:22:30 +0000314 env->cp15.c2_base_mask = 0xffffc000u;
Peter Maydell74594c92011-03-22 12:16:16 +0000315 /* v7 performance monitor control register: same implementor
316 * field as main ID register, and we implement no event counters.
317 */
318 env->cp15.c9_pmcr = (id & 0xff000000);
pbrook40f137e2006-02-20 00:33:36 +0000319#endif
Peter Maydell3a492f32011-01-14 20:39:18 +0100320 set_flush_to_zero(1, &env->vfp.standard_fp_status);
321 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
322 set_default_nan_mode(1, &env->vfp.standard_fp_status);
Peter Maydell9df38c42011-04-12 13:56:41 +0100323 set_float_detect_tininess(float_tininess_before_rounding,
324 &env->vfp.fp_status);
325 set_float_detect_tininess(float_tininess_before_rounding,
326 &env->vfp.standard_fp_status);
pbrookf3d6b952007-03-11 13:03:18 +0000327 tlb_flush(env, 1);
pbrook40f137e2006-02-20 00:33:36 +0000328}
329
pbrook56aebc82008-10-11 17:55:29 +0000330static int vfp_gdb_get_reg(CPUState *env, uint8_t *buf, int reg)
331{
332 int nregs;
333
334 /* VFP data registers are always little-endian. */
335 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
336 if (reg < nregs) {
337 stfq_le_p(buf, env->vfp.regs[reg]);
338 return 8;
339 }
340 if (arm_feature(env, ARM_FEATURE_NEON)) {
341 /* Aliases for Q regs. */
342 nregs += 16;
343 if (reg < nregs) {
344 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
345 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
346 return 16;
347 }
348 }
349 switch (reg - nregs) {
350 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
351 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
352 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
353 }
354 return 0;
355}
356
357static int vfp_gdb_set_reg(CPUState *env, uint8_t *buf, int reg)
358{
359 int nregs;
360
361 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
362 if (reg < nregs) {
363 env->vfp.regs[reg] = ldfq_le_p(buf);
364 return 8;
365 }
366 if (arm_feature(env, ARM_FEATURE_NEON)) {
367 nregs += 16;
368 if (reg < nregs) {
369 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
370 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
371 return 16;
372 }
373 }
374 switch (reg - nregs) {
375 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
376 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +0200377 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +0000378 }
379 return 0;
380}
381
bellardaaed9092007-11-10 15:15:54 +0000382CPUARMState *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +0000383{
384 CPUARMState *env;
bellardaaed9092007-11-10 15:15:54 +0000385 uint32_t id;
pbrookb26eefb2008-03-31 03:44:26 +0000386 static int inited = 0;
pbrook40f137e2006-02-20 00:33:36 +0000387
bellardaaed9092007-11-10 15:15:54 +0000388 id = cpu_arm_find_by_name(cpu_model);
389 if (id == 0)
390 return NULL;
Anthony Liguori7267c092011-08-20 22:09:37 -0500391 env = g_malloc0(sizeof(CPUARMState));
pbrook40f137e2006-02-20 00:33:36 +0000392 cpu_exec_init(env);
Peter Maydellf4fc2472011-11-25 19:25:50 +0100393 if (tcg_enabled() && !inited) {
pbrookb26eefb2008-03-31 03:44:26 +0000394 inited = 1;
395 arm_translate_init();
396 }
397
ths01ba9812007-12-09 02:22:57 +0000398 env->cpu_model_str = cpu_model;
bellardaaed9092007-11-10 15:15:54 +0000399 env->cp15.c0_cpuid = id;
pbrook40f137e2006-02-20 00:33:36 +0000400 cpu_reset(env);
pbrook56aebc82008-10-11 17:55:29 +0000401 if (arm_feature(env, ARM_FEATURE_NEON)) {
402 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
403 51, "arm-neon.xml", 0);
404 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
405 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
406 35, "arm-vfp3.xml", 0);
407 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
408 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
409 19, "arm-vfp.xml", 0);
410 }
aliguori0bf46a42009-04-24 18:03:41 +0000411 qemu_init_vcpu(env);
pbrook40f137e2006-02-20 00:33:36 +0000412 return env;
413}
414
pbrook3371d272007-03-08 03:04:12 +0000415struct arm_cpu_t {
416 uint32_t id;
417 const char *name;
418};
419
420static const struct arm_cpu_t arm_cpu_names[] = {
421 { ARM_CPUID_ARM926, "arm926"},
pbrookce819862007-05-08 02:30:40 +0000422 { ARM_CPUID_ARM946, "arm946"},
pbrook3371d272007-03-08 03:04:12 +0000423 { ARM_CPUID_ARM1026, "arm1026"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000424 { ARM_CPUID_ARM1136, "arm1136"},
balrog827df9f2008-04-14 21:05:22 +0000425 { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
Jamie Iles7807eed2011-07-20 10:32:54 +0000426 { ARM_CPUID_ARM1176, "arm1176"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000427 { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
428 { ARM_CPUID_CORTEXM3, "cortex-m3"},
429 { ARM_CPUID_CORTEXA8, "cortex-a8"},
Paul Brook10055562009-11-19 16:45:20 +0000430 { ARM_CPUID_CORTEXA9, "cortex-a9"},
balrogc3d26892007-07-29 17:57:26 +0000431 { ARM_CPUID_TI925T, "ti925t" },
balrogc1713132007-04-30 01:26:42 +0000432 { ARM_CPUID_PXA250, "pxa250" },
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +0400433 { ARM_CPUID_SA1100, "sa1100" },
434 { ARM_CPUID_SA1110, "sa1110" },
balrogc1713132007-04-30 01:26:42 +0000435 { ARM_CPUID_PXA255, "pxa255" },
436 { ARM_CPUID_PXA260, "pxa260" },
437 { ARM_CPUID_PXA261, "pxa261" },
438 { ARM_CPUID_PXA262, "pxa262" },
439 { ARM_CPUID_PXA270, "pxa270" },
440 { ARM_CPUID_PXA270_A0, "pxa270-a0" },
441 { ARM_CPUID_PXA270_A1, "pxa270-a1" },
442 { ARM_CPUID_PXA270_B0, "pxa270-b0" },
443 { ARM_CPUID_PXA270_B1, "pxa270-b1" },
444 { ARM_CPUID_PXA270_C0, "pxa270-c0" },
445 { ARM_CPUID_PXA270_C5, "pxa270-c5" },
pbrook9ee6e8b2007-11-11 00:04:49 +0000446 { ARM_CPUID_ANY, "any"},
pbrook3371d272007-03-08 03:04:12 +0000447 { 0, NULL}
448};
449
Stefan Weil9a78eea2010-10-22 23:03:33 +0200450void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
pbrook5adb4832007-03-08 03:15:18 +0000451{
452 int i;
453
j_mayerc732abe2007-10-12 06:47:46 +0000454 (*cpu_fprintf)(f, "Available CPUs:\n");
pbrook5adb4832007-03-08 03:15:18 +0000455 for (i = 0; arm_cpu_names[i].name; i++) {
j_mayerc732abe2007-10-12 06:47:46 +0000456 (*cpu_fprintf)(f, " %s\n", arm_cpu_names[i].name);
pbrook5adb4832007-03-08 03:15:18 +0000457 }
458}
459
bellardaaed9092007-11-10 15:15:54 +0000460/* return 0 if not found */
461static uint32_t cpu_arm_find_by_name(const char *name)
pbrook40f137e2006-02-20 00:33:36 +0000462{
pbrook3371d272007-03-08 03:04:12 +0000463 int i;
464 uint32_t id;
465
466 id = 0;
pbrook3371d272007-03-08 03:04:12 +0000467 for (i = 0; arm_cpu_names[i].name; i++) {
468 if (strcmp(name, arm_cpu_names[i].name) == 0) {
469 id = arm_cpu_names[i].id;
470 break;
471 }
472 }
bellardaaed9092007-11-10 15:15:54 +0000473 return id;
pbrook40f137e2006-02-20 00:33:36 +0000474}
475
476void cpu_arm_close(CPUARMState *env)
477{
Andreas Färber12b1de32011-10-10 02:52:28 +0000478 g_free(env);
pbrook40f137e2006-02-20 00:33:36 +0000479}
480
balrog2f4a40e2007-11-13 01:50:15 +0000481uint32_t cpsr_read(CPUARMState *env)
482{
483 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +0000484 ZF = (env->ZF == 0);
485 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +0000486 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
487 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
488 | ((env->condexec_bits & 0xfc) << 8)
489 | (env->GE << 16);
490}
491
492void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
493{
balrog2f4a40e2007-11-13 01:50:15 +0000494 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +0000495 env->ZF = (~val) & CPSR_Z;
496 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +0000497 env->CF = (val >> 29) & 1;
498 env->VF = (val << 3) & 0x80000000;
499 }
500 if (mask & CPSR_Q)
501 env->QF = ((val & CPSR_Q) != 0);
502 if (mask & CPSR_T)
503 env->thumb = ((val & CPSR_T) != 0);
504 if (mask & CPSR_IT_0_1) {
505 env->condexec_bits &= ~3;
506 env->condexec_bits |= (val >> 25) & 3;
507 }
508 if (mask & CPSR_IT_2_7) {
509 env->condexec_bits &= 3;
510 env->condexec_bits |= (val >> 8) & 0xfc;
511 }
512 if (mask & CPSR_GE) {
513 env->GE = (val >> 16) & 0xf;
514 }
515
516 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
517 switch_mode(env, val & CPSR_M);
518 }
519 mask &= ~CACHED_CPSR_BITS;
520 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
521}
522
pbrookb26eefb2008-03-31 03:44:26 +0000523/* Sign/zero extend */
524uint32_t HELPER(sxtb16)(uint32_t x)
525{
526 uint32_t res;
527 res = (uint16_t)(int8_t)x;
528 res |= (uint32_t)(int8_t)(x >> 16) << 16;
529 return res;
530}
531
532uint32_t HELPER(uxtb16)(uint32_t x)
533{
534 uint32_t res;
535 res = (uint16_t)(uint8_t)x;
536 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
537 return res;
538}
539
pbrookf51bbbf2008-03-31 03:45:13 +0000540uint32_t HELPER(clz)(uint32_t x)
541{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +0200542 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +0000543}
544
pbrook36706692008-03-31 03:46:19 +0000545int32_t HELPER(sdiv)(int32_t num, int32_t den)
546{
547 if (den == 0)
548 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +0200549 if (num == INT_MIN && den == -1)
550 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +0000551 return num / den;
552}
553
554uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
555{
556 if (den == 0)
557 return 0;
558 return num / den;
559}
560
561uint32_t HELPER(rbit)(uint32_t x)
562{
563 x = ((x & 0xff000000) >> 24)
564 | ((x & 0x00ff0000) >> 8)
565 | ((x & 0x0000ff00) << 8)
566 | ((x & 0x000000ff) << 24);
567 x = ((x & 0xf0f0f0f0) >> 4)
568 | ((x & 0x0f0f0f0f) << 4);
569 x = ((x & 0x88888888) >> 3)
570 | ((x & 0x44444444) >> 1)
571 | ((x & 0x22222222) << 1)
572 | ((x & 0x11111111) << 3);
573 return x;
574}
575
pbrookad694712008-03-31 03:48:30 +0000576uint32_t HELPER(abs)(uint32_t x)
577{
578 return ((int32_t)x < 0) ? -x : x;
579}
580
ths5fafdf22007-09-16 21:08:06 +0000581#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +0000582
583void do_interrupt (CPUState *env)
584{
585 env->exception_index = -1;
586}
587
588int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
Blue Swirl97b348e2011-08-01 16:12:17 +0000589 int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +0000590{
591 if (rw == 2) {
592 env->exception_index = EXCP_PREFETCH_ABORT;
593 env->cp15.c6_insn = address;
594 } else {
595 env->exception_index = EXCP_DATA_ABORT;
596 env->cp15.c6_data = address;
597 }
598 return 1;
599}
600
bellardb5ff1b32005-11-26 10:38:39 +0000601/* These should probably raise undefined insn exceptions. */
pbrook8984bd22008-03-31 03:47:48 +0000602void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +0000603{
604 int op1 = (insn >> 8) & 0xf;
605 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
606 return;
607}
608
pbrook8984bd22008-03-31 03:47:48 +0000609uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +0000610{
611 int op1 = (insn >> 8) & 0xf;
612 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
613 return 0;
614}
615
pbrook8984bd22008-03-31 03:47:48 +0000616void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +0000617{
618 cpu_abort(env, "cp15 insn %08x\n", insn);
619}
620
pbrook8984bd22008-03-31 03:47:48 +0000621uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +0000622{
623 cpu_abort(env, "cp15 insn %08x\n", insn);
bellardb5ff1b32005-11-26 10:38:39 +0000624}
625
pbrook9ee6e8b2007-11-11 00:04:49 +0000626/* These should probably raise undefined insn exceptions. */
pbrook8984bd22008-03-31 03:47:48 +0000627void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000628{
629 cpu_abort(env, "v7m_mrs %d\n", reg);
630}
631
pbrook8984bd22008-03-31 03:47:48 +0000632uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +0000633{
634 cpu_abort(env, "v7m_mrs %d\n", reg);
635 return 0;
636}
637
bellardb5ff1b32005-11-26 10:38:39 +0000638void switch_mode(CPUState *env, int mode)
639{
640 if (mode != ARM_CPU_MODE_USR)
641 cpu_abort(env, "Tried to switch out of user mode\n");
642}
643
pbrookb0109802008-03-31 03:47:03 +0000644void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000645{
646 cpu_abort(env, "banked r13 write\n");
647}
648
pbrookb0109802008-03-31 03:47:03 +0000649uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +0000650{
651 cpu_abort(env, "banked r13 read\n");
652 return 0;
653}
654
bellardb5ff1b32005-11-26 10:38:39 +0000655#else
656
pbrook8e716212007-01-20 17:12:09 +0000657extern int semihosting_enabled;
658
bellardb5ff1b32005-11-26 10:38:39 +0000659/* Map CPU modes onto saved register banks. */
660static inline int bank_number (int mode)
661{
662 switch (mode) {
663 case ARM_CPU_MODE_USR:
664 case ARM_CPU_MODE_SYS:
665 return 0;
666 case ARM_CPU_MODE_SVC:
667 return 1;
668 case ARM_CPU_MODE_ABT:
669 return 2;
670 case ARM_CPU_MODE_UND:
671 return 3;
672 case ARM_CPU_MODE_IRQ:
673 return 4;
674 case ARM_CPU_MODE_FIQ:
675 return 5;
676 }
677 cpu_abort(cpu_single_env, "Bad mode %x\n", mode);
678 return -1;
679}
680
681void switch_mode(CPUState *env, int mode)
682{
683 int old_mode;
684 int i;
685
686 old_mode = env->uncached_cpsr & CPSR_M;
687 if (mode == old_mode)
688 return;
689
690 if (old_mode == ARM_CPU_MODE_FIQ) {
691 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000692 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000693 } else if (mode == ARM_CPU_MODE_FIQ) {
694 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000695 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000696 }
697
698 i = bank_number(old_mode);
699 env->banked_r13[i] = env->regs[13];
700 env->banked_r14[i] = env->regs[14];
701 env->banked_spsr[i] = env->spsr;
702
703 i = bank_number(mode);
704 env->regs[13] = env->banked_r13[i];
705 env->regs[14] = env->banked_r14[i];
706 env->spsr = env->banked_spsr[i];
707}
708
pbrook9ee6e8b2007-11-11 00:04:49 +0000709static void v7m_push(CPUARMState *env, uint32_t val)
710{
711 env->regs[13] -= 4;
712 stl_phys(env->regs[13], val);
713}
714
715static uint32_t v7m_pop(CPUARMState *env)
716{
717 uint32_t val;
718 val = ldl_phys(env->regs[13]);
719 env->regs[13] += 4;
720 return val;
721}
722
723/* Switch to V7M main or process stack pointer. */
724static void switch_v7m_sp(CPUARMState *env, int process)
725{
726 uint32_t tmp;
727 if (env->v7m.current_sp != process) {
728 tmp = env->v7m.other_sp;
729 env->v7m.other_sp = env->regs[13];
730 env->regs[13] = tmp;
731 env->v7m.current_sp = process;
732 }
733}
734
735static void do_v7m_exception_exit(CPUARMState *env)
736{
737 uint32_t type;
738 uint32_t xpsr;
739
740 type = env->regs[15];
741 if (env->v7m.exception != 0)
Paul Brook983fe822010-04-05 19:34:51 +0100742 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
pbrook9ee6e8b2007-11-11 00:04:49 +0000743
744 /* Switch to the target stack. */
745 switch_v7m_sp(env, (type & 4) != 0);
746 /* Pop registers. */
747 env->regs[0] = v7m_pop(env);
748 env->regs[1] = v7m_pop(env);
749 env->regs[2] = v7m_pop(env);
750 env->regs[3] = v7m_pop(env);
751 env->regs[12] = v7m_pop(env);
752 env->regs[14] = v7m_pop(env);
753 env->regs[15] = v7m_pop(env);
754 xpsr = v7m_pop(env);
755 xpsr_write(env, xpsr, 0xfffffdff);
756 /* Undo stack alignment. */
757 if (xpsr & 0x200)
758 env->regs[13] |= 4;
759 /* ??? The exception return type specifies Thread/Handler mode. However
760 this is also implied by the xPSR value. Not sure what to do
761 if there is a mismatch. */
762 /* ??? Likewise for mismatches between the CONTROL register and the stack
763 pointer. */
764}
765
aurel322b3ea312009-03-07 21:48:00 +0000766static void do_interrupt_v7m(CPUARMState *env)
pbrook9ee6e8b2007-11-11 00:04:49 +0000767{
768 uint32_t xpsr = xpsr_read(env);
769 uint32_t lr;
770 uint32_t addr;
771
772 lr = 0xfffffff1;
773 if (env->v7m.current_sp)
774 lr |= 4;
775 if (env->v7m.exception == 0)
776 lr |= 8;
777
778 /* For exceptions we just mark as pending on the NVIC, and let that
779 handle it. */
780 /* TODO: Need to escalate if the current priority is higher than the
781 one we're raising. */
782 switch (env->exception_index) {
783 case EXCP_UDEF:
Paul Brook983fe822010-04-05 19:34:51 +0100784 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000785 return;
786 case EXCP_SWI:
787 env->regs[15] += 2;
Paul Brook983fe822010-04-05 19:34:51 +0100788 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
pbrook9ee6e8b2007-11-11 00:04:49 +0000789 return;
790 case EXCP_PREFETCH_ABORT:
791 case EXCP_DATA_ABORT:
Paul Brook983fe822010-04-05 19:34:51 +0100792 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
pbrook9ee6e8b2007-11-11 00:04:49 +0000793 return;
794 case EXCP_BKPT:
pbrook2ad207d2007-11-24 23:22:11 +0000795 if (semihosting_enabled) {
796 int nr;
797 nr = lduw_code(env->regs[15]) & 0xff;
798 if (nr == 0xab) {
799 env->regs[15] += 2;
800 env->regs[0] = do_arm_semihosting(env);
801 return;
802 }
803 }
Paul Brook983fe822010-04-05 19:34:51 +0100804 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
pbrook9ee6e8b2007-11-11 00:04:49 +0000805 return;
806 case EXCP_IRQ:
Paul Brook983fe822010-04-05 19:34:51 +0100807 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
pbrook9ee6e8b2007-11-11 00:04:49 +0000808 break;
809 case EXCP_EXCEPTION_EXIT:
810 do_v7m_exception_exit(env);
811 return;
812 default:
813 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
814 return; /* Never happens. Keep compiler happy. */
815 }
816
817 /* Align stack pointer. */
818 /* ??? Should only do this if Configuration Control Register
819 STACKALIGN bit is set. */
820 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +0000821 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +0000822 xpsr |= 0x200;
823 }
balrog6c956762008-04-13 00:57:49 +0000824 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000825 v7m_push(env, xpsr);
826 v7m_push(env, env->regs[15]);
827 v7m_push(env, env->regs[14]);
828 v7m_push(env, env->regs[12]);
829 v7m_push(env, env->regs[3]);
830 v7m_push(env, env->regs[2]);
831 v7m_push(env, env->regs[1]);
832 v7m_push(env, env->regs[0]);
833 switch_v7m_sp(env, 0);
834 env->uncached_cpsr &= ~CPSR_IT;
835 env->regs[14] = lr;
836 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
837 env->regs[15] = addr & 0xfffffffe;
838 env->thumb = addr & 1;
839}
840
bellardb5ff1b32005-11-26 10:38:39 +0000841/* Handle a CPU exception. */
842void do_interrupt(CPUARMState *env)
843{
844 uint32_t addr;
845 uint32_t mask;
846 int new_mode;
847 uint32_t offset;
848
pbrook9ee6e8b2007-11-11 00:04:49 +0000849 if (IS_M(env)) {
850 do_interrupt_v7m(env);
851 return;
852 }
bellardb5ff1b32005-11-26 10:38:39 +0000853 /* TODO: Vectored interrupt controller. */
854 switch (env->exception_index) {
855 case EXCP_UDEF:
856 new_mode = ARM_CPU_MODE_UND;
857 addr = 0x04;
858 mask = CPSR_I;
859 if (env->thumb)
860 offset = 2;
861 else
862 offset = 4;
863 break;
864 case EXCP_SWI:
pbrook8e716212007-01-20 17:12:09 +0000865 if (semihosting_enabled) {
866 /* Check for semihosting interrupt. */
867 if (env->thumb) {
868 mask = lduw_code(env->regs[15] - 2) & 0xff;
869 } else {
870 mask = ldl_code(env->regs[15] - 4) & 0xffffff;
871 }
872 /* Only intercept calls from privileged modes, to provide some
873 semblance of security. */
874 if (((mask == 0x123456 && !env->thumb)
875 || (mask == 0xab && env->thumb))
876 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
877 env->regs[0] = do_arm_semihosting(env);
878 return;
879 }
880 }
bellardb5ff1b32005-11-26 10:38:39 +0000881 new_mode = ARM_CPU_MODE_SVC;
882 addr = 0x08;
883 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +0000884 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +0000885 offset = 0;
886 break;
pbrook06c949e2006-02-04 19:35:26 +0000887 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +0000888 /* See if this is a semihosting syscall. */
pbrook2ad207d2007-11-24 23:22:11 +0000889 if (env->thumb && semihosting_enabled) {
pbrook9ee6e8b2007-11-11 00:04:49 +0000890 mask = lduw_code(env->regs[15]) & 0xff;
891 if (mask == 0xab
892 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
893 env->regs[15] += 2;
894 env->regs[0] = do_arm_semihosting(env);
895 return;
896 }
897 }
Alex Zuepke81c05da2011-06-03 18:42:17 +0200898 env->cp15.c5_insn = 2;
pbrook9ee6e8b2007-11-11 00:04:49 +0000899 /* Fall through to prefetch abort. */
900 case EXCP_PREFETCH_ABORT:
bellardb5ff1b32005-11-26 10:38:39 +0000901 new_mode = ARM_CPU_MODE_ABT;
902 addr = 0x0c;
903 mask = CPSR_A | CPSR_I;
904 offset = 4;
905 break;
906 case EXCP_DATA_ABORT:
907 new_mode = ARM_CPU_MODE_ABT;
908 addr = 0x10;
909 mask = CPSR_A | CPSR_I;
910 offset = 8;
911 break;
912 case EXCP_IRQ:
913 new_mode = ARM_CPU_MODE_IRQ;
914 addr = 0x18;
915 /* Disable IRQ and imprecise data aborts. */
916 mask = CPSR_A | CPSR_I;
917 offset = 4;
918 break;
919 case EXCP_FIQ:
920 new_mode = ARM_CPU_MODE_FIQ;
921 addr = 0x1c;
922 /* Disable FIQ, IRQ and imprecise data aborts. */
923 mask = CPSR_A | CPSR_I | CPSR_F;
924 offset = 4;
925 break;
926 default:
927 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
928 return; /* Never happens. Keep compiler happy. */
929 }
930 /* High vectors. */
931 if (env->cp15.c1_sys & (1 << 13)) {
932 addr += 0xffff0000;
933 }
934 switch_mode (env, new_mode);
935 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +0000936 /* Clear IT bits. */
937 env->condexec_bits = 0;
Rabin Vincent30a8cac2010-02-15 00:02:36 +0530938 /* Switch to the new mode, and to the correct instruction set. */
bellard6d7e6322005-12-18 16:54:08 +0000939 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
bellardb5ff1b32005-11-26 10:38:39 +0000940 env->uncached_cpsr |= mask;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400941 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
942 * and we should just guard the thumb mode on V4 */
943 if (arm_feature(env, ARM_FEATURE_V4T)) {
944 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
945 }
bellardb5ff1b32005-11-26 10:38:39 +0000946 env->regs[14] = env->regs[15] + offset;
947 env->regs[15] = addr;
948 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
949}
950
951/* Check section/page access permissions.
952 Returns the page protection flags, or zero if the access is not
953 permitted. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000954static inline int check_ap(CPUState *env, int ap, int domain_prot,
955 int access_type, int is_user)
bellardb5ff1b32005-11-26 10:38:39 +0000956{
pbrook9ee6e8b2007-11-11 00:04:49 +0000957 int prot_ro;
958
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000959 if (domain_prot == 3) {
bellardb5ff1b32005-11-26 10:38:39 +0000960 return PAGE_READ | PAGE_WRITE;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +0000961 }
bellardb5ff1b32005-11-26 10:38:39 +0000962
pbrook9ee6e8b2007-11-11 00:04:49 +0000963 if (access_type == 1)
964 prot_ro = 0;
965 else
966 prot_ro = PAGE_READ;
967
bellardb5ff1b32005-11-26 10:38:39 +0000968 switch (ap) {
969 case 0:
pbrook78600322006-09-09 14:36:26 +0000970 if (access_type == 1)
bellardb5ff1b32005-11-26 10:38:39 +0000971 return 0;
972 switch ((env->cp15.c1_sys >> 8) & 3) {
973 case 1:
974 return is_user ? 0 : PAGE_READ;
975 case 2:
976 return PAGE_READ;
977 default:
978 return 0;
979 }
980 case 1:
981 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
982 case 2:
983 if (is_user)
pbrook9ee6e8b2007-11-11 00:04:49 +0000984 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +0000985 else
986 return PAGE_READ | PAGE_WRITE;
987 case 3:
988 return PAGE_READ | PAGE_WRITE;
pbrookd4934d12008-12-19 12:39:00 +0000989 case 4: /* Reserved. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000990 return 0;
991 case 5:
992 return is_user ? 0 : prot_ro;
993 case 6:
994 return prot_ro;
pbrookd4934d12008-12-19 12:39:00 +0000995 case 7:
Jamie Iles0ab06d82011-06-23 01:12:59 +0000996 if (!arm_feature (env, ARM_FEATURE_V6K))
pbrookd4934d12008-12-19 12:39:00 +0000997 return 0;
998 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +0000999 default:
1000 abort();
1001 }
1002}
1003
pbrookb2fa1792008-10-22 19:22:30 +00001004static uint32_t get_level1_table_address(CPUState *env, uint32_t address)
1005{
1006 uint32_t table;
1007
1008 if (address & env->cp15.c2_mask)
1009 table = env->cp15.c2_base1 & 0xffffc000;
1010 else
1011 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
1012
1013 table |= (address >> 18) & 0x3ffc;
1014 return table;
1015}
1016
pbrook9ee6e8b2007-11-11 00:04:49 +00001017static int get_phys_addr_v5(CPUState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +00001018 int is_user, uint32_t *phys_ptr, int *prot,
1019 target_ulong *page_size)
bellardb5ff1b32005-11-26 10:38:39 +00001020{
1021 int code;
1022 uint32_t table;
1023 uint32_t desc;
1024 int type;
1025 int ap;
1026 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001027 int domain_prot;
bellardb5ff1b32005-11-26 10:38:39 +00001028 uint32_t phys_addr;
1029
pbrook9ee6e8b2007-11-11 00:04:49 +00001030 /* Pagetable walk. */
1031 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001032 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001033 desc = ldl_phys(table);
1034 type = (desc & 3);
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001035 domain = (desc >> 5) & 0x0f;
1036 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
pbrook9ee6e8b2007-11-11 00:04:49 +00001037 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001038 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001039 code = 5;
1040 goto do_fault;
1041 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001042 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001043 if (type == 2)
1044 code = 9; /* Section domain fault. */
1045 else
1046 code = 11; /* Page domain fault. */
1047 goto do_fault;
1048 }
1049 if (type == 2) {
1050 /* 1Mb section. */
1051 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1052 ap = (desc >> 10) & 3;
1053 code = 13;
Paul Brookd4c430a2010-03-17 02:14:28 +00001054 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +00001055 } else {
1056 /* Lookup l2 entry. */
1057 if (type == 1) {
1058 /* Coarse pagetable. */
1059 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1060 } else {
1061 /* Fine pagetable. */
1062 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1063 }
1064 desc = ldl_phys(table);
1065 switch (desc & 3) {
1066 case 0: /* Page translation fault. */
1067 code = 7;
1068 goto do_fault;
1069 case 1: /* 64k page. */
1070 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1071 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001072 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001073 break;
1074 case 2: /* 4k page. */
1075 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1076 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001077 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001078 break;
1079 case 3: /* 1k page. */
1080 if (type == 1) {
1081 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1082 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1083 } else {
1084 /* Page translation fault. */
1085 code = 7;
1086 goto do_fault;
1087 }
1088 } else {
1089 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1090 }
1091 ap = (desc >> 4) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001092 *page_size = 0x400;
pbrook9ee6e8b2007-11-11 00:04:49 +00001093 break;
1094 default:
1095 /* Never happens, but compiler isn't smart enough to tell. */
1096 abort();
1097 }
1098 code = 15;
1099 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001100 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
pbrook9ee6e8b2007-11-11 00:04:49 +00001101 if (!*prot) {
1102 /* Access permission fault. */
1103 goto do_fault;
1104 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301105 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001106 *phys_ptr = phys_addr;
1107 return 0;
1108do_fault:
1109 return code | (domain << 4);
1110}
1111
1112static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +00001113 int is_user, uint32_t *phys_ptr, int *prot,
1114 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001115{
1116 int code;
1117 uint32_t table;
1118 uint32_t desc;
1119 uint32_t xn;
1120 int type;
1121 int ap;
1122 int domain;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001123 int domain_prot;
pbrook9ee6e8b2007-11-11 00:04:49 +00001124 uint32_t phys_addr;
1125
1126 /* Pagetable walk. */
1127 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001128 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001129 desc = ldl_phys(table);
1130 type = (desc & 3);
1131 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001132 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001133 code = 5;
1134 domain = 0;
1135 goto do_fault;
1136 } else if (type == 2 && (desc & (1 << 18))) {
1137 /* Supersection. */
1138 domain = 0;
1139 } else {
1140 /* Section or page. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001141 domain = (desc >> 5) & 0x0f;
pbrook9ee6e8b2007-11-11 00:04:49 +00001142 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001143 domain_prot = (env->cp15.c3 >> (domain * 2)) & 3;
1144 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001145 if (type == 2)
1146 code = 9; /* Section domain fault. */
1147 else
1148 code = 11; /* Page domain fault. */
1149 goto do_fault;
1150 }
1151 if (type == 2) {
1152 if (desc & (1 << 18)) {
1153 /* Supersection. */
1154 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001155 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001156 } else {
1157 /* Section. */
1158 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001159 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001160 }
1161 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1162 xn = desc & (1 << 4);
1163 code = 13;
1164 } else {
1165 /* Lookup l2 entry. */
1166 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1167 desc = ldl_phys(table);
1168 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1169 switch (desc & 3) {
1170 case 0: /* Page translation fault. */
1171 code = 7;
1172 goto do_fault;
1173 case 1: /* 64k page. */
1174 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1175 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +00001176 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001177 break;
1178 case 2: case 3: /* 4k page. */
1179 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1180 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +00001181 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001182 break;
1183 default:
1184 /* Never happens, but compiler isn't smart enough to tell. */
1185 abort();
1186 }
1187 code = 15;
1188 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001189 if (domain_prot == 3) {
Juha Riihimäkic0034322010-12-08 13:15:16 +02001190 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1191 } else {
1192 if (xn && access_type == 2)
1193 goto do_fault;
pbrook9ee6e8b2007-11-11 00:04:49 +00001194
Juha Riihimäkic0034322010-12-08 13:15:16 +02001195 /* The simplified model uses AP[0] as an access control bit. */
1196 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1197 /* Access flag fault. */
1198 code = (code == 15) ? 6 : 3;
1199 goto do_fault;
1200 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00001201 *prot = check_ap(env, ap, domain_prot, access_type, is_user);
Juha Riihimäkic0034322010-12-08 13:15:16 +02001202 if (!*prot) {
1203 /* Access permission fault. */
1204 goto do_fault;
1205 }
1206 if (!xn) {
1207 *prot |= PAGE_EXEC;
1208 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301209 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001210 *phys_ptr = phys_addr;
1211 return 0;
1212do_fault:
1213 return code | (domain << 4);
1214}
1215
1216static int get_phys_addr_mpu(CPUState *env, uint32_t address, int access_type,
1217 int is_user, uint32_t *phys_ptr, int *prot)
1218{
1219 int n;
1220 uint32_t mask;
1221 uint32_t base;
1222
1223 *phys_ptr = address;
1224 for (n = 7; n >= 0; n--) {
1225 base = env->cp15.c6_region[n];
1226 if ((base & 1) == 0)
1227 continue;
1228 mask = 1 << ((base >> 1) & 0x1f);
1229 /* Keep this shift separate from the above to avoid an
1230 (undefined) << 32. */
1231 mask = (mask << 1) - 1;
1232 if (((base ^ address) & ~mask) == 0)
1233 break;
1234 }
1235 if (n < 0)
1236 return 2;
1237
1238 if (access_type == 2) {
1239 mask = env->cp15.c5_insn;
1240 } else {
1241 mask = env->cp15.c5_data;
1242 }
1243 mask = (mask >> (n * 4)) & 0xf;
1244 switch (mask) {
1245 case 0:
1246 return 1;
1247 case 1:
1248 if (is_user)
1249 return 1;
1250 *prot = PAGE_READ | PAGE_WRITE;
1251 break;
1252 case 2:
1253 *prot = PAGE_READ;
1254 if (!is_user)
1255 *prot |= PAGE_WRITE;
1256 break;
1257 case 3:
1258 *prot = PAGE_READ | PAGE_WRITE;
1259 break;
1260 case 5:
1261 if (is_user)
1262 return 1;
1263 *prot = PAGE_READ;
1264 break;
1265 case 6:
1266 *prot = PAGE_READ;
1267 break;
1268 default:
1269 /* Bad permission. */
1270 return 1;
1271 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301272 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001273 return 0;
1274}
1275
1276static inline int get_phys_addr(CPUState *env, uint32_t address,
1277 int access_type, int is_user,
Paul Brookd4c430a2010-03-17 02:14:28 +00001278 uint32_t *phys_ptr, int *prot,
1279 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001280{
bellardb5ff1b32005-11-26 10:38:39 +00001281 /* Fast Context Switch Extension. */
1282 if (address < 0x02000000)
1283 address += env->cp15.c13_fcse;
1284
1285 if ((env->cp15.c1_sys & 1) == 0) {
pbrookce819862007-05-08 02:30:40 +00001286 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00001287 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301288 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +00001289 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001290 return 0;
pbrookce819862007-05-08 02:30:40 +00001291 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
Paul Brookd4c430a2010-03-17 02:14:28 +00001292 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001293 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1294 prot);
1295 } else if (env->cp15.c1_sys & (1 << 23)) {
1296 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001297 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001298 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001299 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001300 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001301 }
bellardb5ff1b32005-11-26 10:38:39 +00001302}
1303
1304int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
Blue Swirl97b348e2011-08-01 16:12:17 +00001305 int access_type, int mmu_idx)
bellardb5ff1b32005-11-26 10:38:39 +00001306{
1307 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001308 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001309 int prot;
j_mayer6ebbf392007-10-14 07:07:08 +00001310 int ret, is_user;
bellardb5ff1b32005-11-26 10:38:39 +00001311
j_mayer6ebbf392007-10-14 07:07:08 +00001312 is_user = mmu_idx == MMU_USER_IDX;
Paul Brookd4c430a2010-03-17 02:14:28 +00001313 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1314 &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001315 if (ret == 0) {
1316 /* Map a single [sub]page. */
1317 phys_addr &= ~(uint32_t)0x3ff;
1318 address &= ~(uint32_t)0x3ff;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301319 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
Paul Brookd4c430a2010-03-17 02:14:28 +00001320 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00001321 }
1322
1323 if (access_type == 2) {
1324 env->cp15.c5_insn = ret;
1325 env->cp15.c6_insn = address;
1326 env->exception_index = EXCP_PREFETCH_ABORT;
1327 } else {
1328 env->cp15.c5_data = ret;
pbrook9ee6e8b2007-11-11 00:04:49 +00001329 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1330 env->cp15.c5_data |= (1 << 11);
bellardb5ff1b32005-11-26 10:38:39 +00001331 env->cp15.c6_data = address;
1332 env->exception_index = EXCP_DATA_ABORT;
1333 }
1334 return 1;
1335}
1336
Anthony Liguoric227f092009-10-01 16:12:16 -05001337target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
bellardb5ff1b32005-11-26 10:38:39 +00001338{
1339 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001340 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001341 int prot;
1342 int ret;
1343
Paul Brookd4c430a2010-03-17 02:14:28 +00001344 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001345
1346 if (ret != 0)
1347 return -1;
1348
1349 return phys_addr;
1350}
1351
pbrook8984bd22008-03-31 03:47:48 +00001352void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +00001353{
1354 int cp_num = (insn >> 8) & 0xf;
1355 int cp_info = (insn >> 5) & 7;
1356 int src = (insn >> 16) & 0xf;
1357 int operand = insn & 0xf;
1358
1359 if (env->cp[cp_num].cp_write)
1360 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1361 cp_info, src, operand, val);
1362}
1363
pbrook8984bd22008-03-31 03:47:48 +00001364uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +00001365{
1366 int cp_num = (insn >> 8) & 0xf;
1367 int cp_info = (insn >> 5) & 7;
1368 int dest = (insn >> 16) & 0xf;
1369 int operand = insn & 0xf;
1370
1371 if (env->cp[cp_num].cp_read)
1372 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1373 cp_info, dest, operand);
1374 return 0;
1375}
1376
pbrookce819862007-05-08 02:30:40 +00001377/* Return basic MPU access permission bits. */
1378static uint32_t simple_mpu_ap_bits(uint32_t val)
1379{
1380 uint32_t ret;
1381 uint32_t mask;
1382 int i;
1383 ret = 0;
1384 mask = 3;
1385 for (i = 0; i < 16; i += 2) {
1386 ret |= (val >> i) & mask;
1387 mask <<= 2;
1388 }
1389 return ret;
1390}
1391
1392/* Pad basic MPU access permission bits to extended format. */
1393static uint32_t extended_mpu_ap_bits(uint32_t val)
1394{
1395 uint32_t ret;
1396 uint32_t mask;
1397 int i;
1398 ret = 0;
1399 mask = 3;
1400 for (i = 0; i < 16; i += 2) {
1401 ret |= (val & mask) << i;
1402 mask <<= 2;
1403 }
1404 return ret;
1405}
1406
pbrook8984bd22008-03-31 03:47:48 +00001407void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +00001408{
pbrook9ee6e8b2007-11-11 00:04:49 +00001409 int op1;
1410 int op2;
1411 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001412
pbrook9ee6e8b2007-11-11 00:04:49 +00001413 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001414 op2 = (insn >> 5) & 7;
pbrookce819862007-05-08 02:30:40 +00001415 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001416 switch ((insn >> 16) & 0xf) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001417 case 0:
pbrook9ee6e8b2007-11-11 00:04:49 +00001418 /* ID codes. */
balrog610c3c82007-06-24 12:09:48 +00001419 if (arm_feature(env, ARM_FEATURE_XSCALE))
1420 break;
balrogc3d26892007-07-29 17:57:26 +00001421 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1422 break;
pbrooka49ea272008-12-19 13:37:53 +00001423 if (arm_feature(env, ARM_FEATURE_V7)
1424 && op1 == 2 && crm == 0 && op2 == 0) {
1425 env->cp15.c0_cssel = val & 0xf;
1426 break;
1427 }
bellardb5ff1b32005-11-26 10:38:39 +00001428 goto bad_reg;
1429 case 1: /* System configuration. */
balrogc3d26892007-07-29 17:57:26 +00001430 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1431 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001432 switch (op2) {
1433 case 0:
pbrookce819862007-05-08 02:30:40 +00001434 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
balrogc1713132007-04-30 01:26:42 +00001435 env->cp15.c1_sys = val;
bellardb5ff1b32005-11-26 10:38:39 +00001436 /* ??? Lots of these bits are not implemented. */
1437 /* This may enable/disable the MMU, so do a TLB flush. */
1438 tlb_flush(env, 1);
1439 break;
Stefan Weil61cc8702011-04-13 22:45:22 +02001440 case 1: /* Auxiliary control register. */
balrog610c3c82007-06-24 12:09:48 +00001441 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1442 env->cp15.c1_xscaleauxcr = val;
balrogc1713132007-04-30 01:26:42 +00001443 break;
balrog610c3c82007-06-24 12:09:48 +00001444 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001445 /* Not implemented. */
1446 break;
bellardb5ff1b32005-11-26 10:38:39 +00001447 case 2:
balrog610c3c82007-06-24 12:09:48 +00001448 if (arm_feature(env, ARM_FEATURE_XSCALE))
1449 goto bad_reg;
pbrook4be27db2008-10-22 16:14:08 +00001450 if (env->cp15.c1_coproc != val) {
1451 env->cp15.c1_coproc = val;
1452 /* ??? Is this safe when called from within a TB? */
1453 tb_flush(env);
1454 }
balrogc1713132007-04-30 01:26:42 +00001455 break;
bellardb5ff1b32005-11-26 10:38:39 +00001456 default:
1457 goto bad_reg;
1458 }
1459 break;
pbrookce819862007-05-08 02:30:40 +00001460 case 2: /* MMU Page table control / MPU cache control. */
1461 if (arm_feature(env, ARM_FEATURE_MPU)) {
1462 switch (op2) {
1463 case 0:
1464 env->cp15.c2_data = val;
1465 break;
1466 case 1:
1467 env->cp15.c2_insn = val;
1468 break;
1469 default:
1470 goto bad_reg;
1471 }
1472 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001473 switch (op2) {
1474 case 0:
1475 env->cp15.c2_base0 = val;
1476 break;
1477 case 1:
1478 env->cp15.c2_base1 = val;
1479 break;
1480 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001481 val &= 7;
1482 env->cp15.c2_control = val;
pbrook9ee6e8b2007-11-11 00:04:49 +00001483 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
pbrookb2fa1792008-10-22 19:22:30 +00001484 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
pbrook9ee6e8b2007-11-11 00:04:49 +00001485 break;
1486 default:
1487 goto bad_reg;
1488 }
pbrookce819862007-05-08 02:30:40 +00001489 }
bellardb5ff1b32005-11-26 10:38:39 +00001490 break;
pbrookce819862007-05-08 02:30:40 +00001491 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001492 env->cp15.c3 = val;
balrog405ee3a2007-10-31 00:47:13 +00001493 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
bellardb5ff1b32005-11-26 10:38:39 +00001494 break;
1495 case 4: /* Reserved. */
1496 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001497 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001498 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1499 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001500 switch (op2) {
1501 case 0:
pbrookce819862007-05-08 02:30:40 +00001502 if (arm_feature(env, ARM_FEATURE_MPU))
1503 val = extended_mpu_ap_bits(val);
bellardb5ff1b32005-11-26 10:38:39 +00001504 env->cp15.c5_data = val;
1505 break;
1506 case 1:
pbrookce819862007-05-08 02:30:40 +00001507 if (arm_feature(env, ARM_FEATURE_MPU))
1508 val = extended_mpu_ap_bits(val);
1509 env->cp15.c5_insn = val;
1510 break;
1511 case 2:
1512 if (!arm_feature(env, ARM_FEATURE_MPU))
1513 goto bad_reg;
1514 env->cp15.c5_data = val;
1515 break;
1516 case 3:
1517 if (!arm_feature(env, ARM_FEATURE_MPU))
1518 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001519 env->cp15.c5_insn = val;
1520 break;
1521 default:
1522 goto bad_reg;
1523 }
1524 break;
pbrookce819862007-05-08 02:30:40 +00001525 case 6: /* MMU Fault address / MPU base/size. */
1526 if (arm_feature(env, ARM_FEATURE_MPU)) {
1527 if (crm >= 8)
1528 goto bad_reg;
1529 env->cp15.c6_region[crm] = val;
1530 } else {
balrogc3d26892007-07-29 17:57:26 +00001531 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1532 op2 = 0;
pbrookce819862007-05-08 02:30:40 +00001533 switch (op2) {
1534 case 0:
1535 env->cp15.c6_data = val;
1536 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001537 case 1: /* ??? This is WFAR on armv6 */
1538 case 2:
pbrookce819862007-05-08 02:30:40 +00001539 env->cp15.c6_insn = val;
1540 break;
1541 default:
1542 goto bad_reg;
1543 }
bellardb5ff1b32005-11-26 10:38:39 +00001544 }
1545 break;
1546 case 7: /* Cache control. */
balrogc3d26892007-07-29 17:57:26 +00001547 env->cp15.c15_i_max = 0x000;
1548 env->cp15.c15_i_min = 0xff0;
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001549 if (op1 != 0) {
1550 goto bad_reg;
1551 }
1552 /* No cache, so nothing to do except VA->PA translations. */
Peter Maydell906879a2011-07-20 10:32:55 +00001553 if (arm_feature(env, ARM_FEATURE_VAPA)) {
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001554 switch (crm) {
1555 case 4:
1556 if (arm_feature(env, ARM_FEATURE_V7)) {
1557 env->cp15.c7_par = val & 0xfffff6ff;
1558 } else {
1559 env->cp15.c7_par = val & 0xfffff1ff;
1560 }
1561 break;
1562 case 8: {
1563 uint32_t phys_addr;
1564 target_ulong page_size;
1565 int prot;
1566 int ret, is_user = op2 & 2;
1567 int access_type = op2 & 1;
1568
1569 if (op2 & 4) {
1570 /* Other states are only available with TrustZone */
1571 goto bad_reg;
1572 }
1573 ret = get_phys_addr(env, val, access_type, is_user,
1574 &phys_addr, &prot, &page_size);
1575 if (ret == 0) {
1576 /* We do not set any attribute bits in the PAR */
1577 if (page_size == (1 << 24)
1578 && arm_feature(env, ARM_FEATURE_V7)) {
1579 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1580 } else {
1581 env->cp15.c7_par = phys_addr & 0xfffff000;
1582 }
1583 } else {
1584 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1585 ((ret & (12 << 1)) >> 6) |
1586 ((ret & 0xf) << 1) | 1;
1587 }
1588 break;
1589 }
1590 }
1591 }
bellardb5ff1b32005-11-26 10:38:39 +00001592 break;
1593 case 8: /* MMU TLB control. */
1594 switch (op2) {
1595 case 0: /* Invalidate all. */
1596 tlb_flush(env, 0);
1597 break;
1598 case 1: /* Invalidate single TLB entry. */
Paul Brookd4c430a2010-03-17 02:14:28 +00001599 tlb_flush_page(env, val & TARGET_PAGE_MASK);
bellardb5ff1b32005-11-26 10:38:39 +00001600 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001601 case 2: /* Invalidate on ASID. */
1602 tlb_flush(env, val == 0);
1603 break;
1604 case 3: /* Invalidate single entry on MVA. */
1605 /* ??? This is like case 1, but ignores ASID. */
1606 tlb_flush(env, 1);
1607 break;
bellardb5ff1b32005-11-26 10:38:39 +00001608 default:
1609 goto bad_reg;
1610 }
1611 break;
pbrookce819862007-05-08 02:30:40 +00001612 case 9:
balrogc3d26892007-07-29 17:57:26 +00001613 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1614 break;
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +04001615 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1616 break; /* Ignore ReadBuffer access */
pbrookce819862007-05-08 02:30:40 +00001617 switch (crm) {
1618 case 0: /* Cache lockdown. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001619 switch (op1) {
1620 case 0: /* L1 cache. */
1621 switch (op2) {
1622 case 0:
1623 env->cp15.c9_data = val;
1624 break;
1625 case 1:
1626 env->cp15.c9_insn = val;
1627 break;
1628 default:
1629 goto bad_reg;
1630 }
1631 break;
1632 case 1: /* L2 cache. */
1633 /* Ignore writes to L2 lockdown/auxiliary registers. */
1634 break;
1635 default:
1636 goto bad_reg;
1637 }
1638 break;
pbrookce819862007-05-08 02:30:40 +00001639 case 1: /* TCM memory region registers. */
1640 /* Not implemented. */
1641 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00001642 case 12: /* Performance monitor control */
1643 /* Performance monitors are implementation defined in v7,
1644 * but with an ARM recommended set of registers, which we
1645 * follow (although we don't actually implement any counters)
1646 */
1647 if (!arm_feature(env, ARM_FEATURE_V7)) {
1648 goto bad_reg;
1649 }
1650 switch (op2) {
1651 case 0: /* performance monitor control register */
1652 /* only the DP, X, D and E bits are writable */
1653 env->cp15.c9_pmcr &= ~0x39;
1654 env->cp15.c9_pmcr |= (val & 0x39);
1655 break;
1656 case 1: /* Count enable set register */
1657 val &= (1 << 31);
1658 env->cp15.c9_pmcnten |= val;
1659 break;
1660 case 2: /* Count enable clear */
1661 val &= (1 << 31);
1662 env->cp15.c9_pmcnten &= ~val;
1663 break;
1664 case 3: /* Overflow flag status */
1665 env->cp15.c9_pmovsr &= ~val;
1666 break;
1667 case 4: /* Software increment */
1668 /* RAZ/WI since we don't implement the software-count event */
1669 break;
1670 case 5: /* Event counter selection register */
1671 /* Since we don't implement any events, writing to this register
1672 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1673 */
1674 break;
1675 default:
1676 goto bad_reg;
1677 }
1678 break;
1679 case 13: /* Performance counters */
1680 if (!arm_feature(env, ARM_FEATURE_V7)) {
1681 goto bad_reg;
1682 }
1683 switch (op2) {
1684 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1685 break;
1686 case 1: /* Event type select */
1687 env->cp15.c9_pmxevtyper = val & 0xff;
1688 break;
1689 case 2: /* Event count register */
1690 /* Unimplemented (we have no events), RAZ/WI */
1691 break;
1692 default:
1693 goto bad_reg;
1694 }
1695 break;
1696 case 14: /* Performance monitor control */
1697 if (!arm_feature(env, ARM_FEATURE_V7)) {
1698 goto bad_reg;
1699 }
1700 switch (op2) {
1701 case 0: /* user enable */
1702 env->cp15.c9_pmuserenr = val & 1;
1703 /* changes access rights for cp registers, so flush tbs */
1704 tb_flush(env);
1705 break;
1706 case 1: /* interrupt enable set */
1707 /* We have no event counters so only the C bit can be changed */
1708 val &= (1 << 31);
1709 env->cp15.c9_pminten |= val;
1710 break;
1711 case 2: /* interrupt enable clear */
1712 val &= (1 << 31);
1713 env->cp15.c9_pminten &= ~val;
1714 break;
1715 }
1716 break;
bellardb5ff1b32005-11-26 10:38:39 +00001717 default:
1718 goto bad_reg;
1719 }
1720 break;
1721 case 10: /* MMU TLB lockdown. */
1722 /* ??? TLB lockdown not implemented. */
1723 break;
bellardb5ff1b32005-11-26 10:38:39 +00001724 case 12: /* Reserved. */
1725 goto bad_reg;
1726 case 13: /* Process ID. */
1727 switch (op2) {
1728 case 0:
pbrookd07edbf2006-07-21 22:39:57 +00001729 /* Unlike real hardware the qemu TLB uses virtual addresses,
1730 not modified virtual addresses, so this causes a TLB flush.
1731 */
1732 if (env->cp15.c13_fcse != val)
1733 tlb_flush(env, 1);
1734 env->cp15.c13_fcse = val;
bellardb5ff1b32005-11-26 10:38:39 +00001735 break;
1736 case 1:
pbrookd07edbf2006-07-21 22:39:57 +00001737 /* This changes the ASID, so do a TLB flush. */
pbrookce819862007-05-08 02:30:40 +00001738 if (env->cp15.c13_context != val
1739 && !arm_feature(env, ARM_FEATURE_MPU))
pbrookd07edbf2006-07-21 22:39:57 +00001740 tlb_flush(env, 0);
1741 env->cp15.c13_context = val;
bellardb5ff1b32005-11-26 10:38:39 +00001742 break;
1743 default:
1744 goto bad_reg;
1745 }
1746 break;
1747 case 14: /* Reserved. */
1748 goto bad_reg;
1749 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00001750 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
pbrookce819862007-05-08 02:30:40 +00001751 if (op2 == 0 && crm == 1) {
balrog2e232132007-08-01 02:31:54 +00001752 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1753 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1754 tb_flush(env);
1755 env->cp15.c15_cpar = val & 0x3fff;
1756 }
balrogc1713132007-04-30 01:26:42 +00001757 break;
1758 }
1759 goto bad_reg;
1760 }
balrogc3d26892007-07-29 17:57:26 +00001761 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1762 switch (crm) {
1763 case 0:
1764 break;
1765 case 1: /* Set TI925T configuration. */
1766 env->cp15.c15_ticonfig = val & 0xe7;
1767 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1768 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1769 break;
1770 case 2: /* Set I_max. */
1771 env->cp15.c15_i_max = val;
1772 break;
1773 case 3: /* Set I_min. */
1774 env->cp15.c15_i_min = val;
1775 break;
1776 case 4: /* Set thread-ID. */
1777 env->cp15.c15_threadid = val & 0xffff;
1778 break;
1779 case 8: /* Wait-for-interrupt (deprecated). */
1780 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1781 break;
1782 default:
1783 goto bad_reg;
1784 }
1785 }
bellardb5ff1b32005-11-26 10:38:39 +00001786 break;
1787 }
1788 return;
1789bad_reg:
1790 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001791 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1792 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00001793}
1794
pbrook8984bd22008-03-31 03:47:48 +00001795uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +00001796{
pbrook9ee6e8b2007-11-11 00:04:49 +00001797 int op1;
1798 int op2;
1799 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001800
pbrook9ee6e8b2007-11-11 00:04:49 +00001801 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001802 op2 = (insn >> 5) & 7;
balrogc3d26892007-07-29 17:57:26 +00001803 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001804 switch ((insn >> 16) & 0xf) {
1805 case 0: /* ID codes. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001806 switch (op1) {
1807 case 0:
1808 switch (crm) {
1809 case 0:
1810 switch (op2) {
1811 case 0: /* Device ID. */
1812 return env->cp15.c0_cpuid;
1813 case 1: /* Cache Type. */
1814 return env->cp15.c0_cachetype;
1815 case 2: /* TCM status. */
1816 return 0;
1817 case 3: /* TLB type register. */
1818 return 0; /* No lockable TLB entries. */
Peter Maydell607b4b02011-02-03 19:43:23 +00001819 case 5: /* MPIDR */
1820 /* The MPIDR was standardised in v7; prior to
1821 * this it was implemented only in the 11MPCore.
1822 * For all other pre-v7 cores it does not exist.
1823 */
1824 if (arm_feature(env, ARM_FEATURE_V7) ||
1825 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1826 int mpidr = env->cpu_index;
1827 /* We don't support setting cluster ID ([8..11])
1828 * so these bits always RAZ.
1829 */
1830 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1831 mpidr |= (1 << 31);
1832 /* Cores which are uniprocessor (non-coherent)
1833 * but still implement the MP extensions set
1834 * bit 30. (For instance, A9UP.) However we do
1835 * not currently model any of those cores.
1836 */
1837 }
1838 return mpidr;
Paul Brook10055562009-11-19 16:45:20 +00001839 }
Peter Maydell607b4b02011-02-03 19:43:23 +00001840 /* otherwise fall through to the unimplemented-reg case */
pbrook9ee6e8b2007-11-11 00:04:49 +00001841 default:
1842 goto bad_reg;
1843 }
1844 case 1:
1845 if (!arm_feature(env, ARM_FEATURE_V6))
1846 goto bad_reg;
1847 return env->cp15.c0_c1[op2];
1848 case 2:
1849 if (!arm_feature(env, ARM_FEATURE_V6))
1850 goto bad_reg;
1851 return env->cp15.c0_c2[op2];
1852 case 3: case 4: case 5: case 6: case 7:
1853 return 0;
1854 default:
1855 goto bad_reg;
1856 }
1857 case 1:
1858 /* These registers aren't documented on arm11 cores. However
1859 Linux looks at them anyway. */
1860 if (!arm_feature(env, ARM_FEATURE_V6))
1861 goto bad_reg;
1862 if (crm != 0)
1863 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001864 if (!arm_feature(env, ARM_FEATURE_V7))
1865 return 0;
1866
1867 switch (op2) {
1868 case 0:
1869 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1870 case 1:
1871 return env->cp15.c0_clid;
1872 case 7:
1873 return 0;
1874 }
1875 goto bad_reg;
1876 case 2:
1877 if (op2 != 0 || crm != 0)
balrog610c3c82007-06-24 12:09:48 +00001878 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001879 return env->cp15.c0_cssel;
pbrook9ee6e8b2007-11-11 00:04:49 +00001880 default:
1881 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001882 }
1883 case 1: /* System configuration. */
balrogc3d26892007-07-29 17:57:26 +00001884 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1885 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001886 switch (op2) {
1887 case 0: /* Control register. */
1888 return env->cp15.c1_sys;
1889 case 1: /* Auxiliary control register. */
balrogc1713132007-04-30 01:26:42 +00001890 if (arm_feature(env, ARM_FEATURE_XSCALE))
balrog610c3c82007-06-24 12:09:48 +00001891 return env->cp15.c1_xscaleauxcr;
pbrook9ee6e8b2007-11-11 00:04:49 +00001892 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1893 goto bad_reg;
1894 switch (ARM_CPUID(env)) {
1895 case ARM_CPUID_ARM1026:
1896 return 1;
1897 case ARM_CPUID_ARM1136:
balrog827df9f2008-04-14 21:05:22 +00001898 case ARM_CPUID_ARM1136_R2:
Jamie Iles7807eed2011-07-20 10:32:54 +00001899 case ARM_CPUID_ARM1176:
pbrook9ee6e8b2007-11-11 00:04:49 +00001900 return 7;
1901 case ARM_CPUID_ARM11MPCORE:
1902 return 1;
1903 case ARM_CPUID_CORTEXA8:
aurel32533d1772009-03-07 22:10:28 +00001904 return 2;
Paul Brook10055562009-11-19 16:45:20 +00001905 case ARM_CPUID_CORTEXA9:
1906 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001907 default:
1908 goto bad_reg;
1909 }
bellardb5ff1b32005-11-26 10:38:39 +00001910 case 2: /* Coprocessor access register. */
balrog610c3c82007-06-24 12:09:48 +00001911 if (arm_feature(env, ARM_FEATURE_XSCALE))
1912 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001913 return env->cp15.c1_coproc;
1914 default:
1915 goto bad_reg;
1916 }
pbrookce819862007-05-08 02:30:40 +00001917 case 2: /* MMU Page table control / MPU cache control. */
1918 if (arm_feature(env, ARM_FEATURE_MPU)) {
1919 switch (op2) {
1920 case 0:
1921 return env->cp15.c2_data;
1922 break;
1923 case 1:
1924 return env->cp15.c2_insn;
1925 break;
1926 default:
1927 goto bad_reg;
1928 }
1929 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001930 switch (op2) {
1931 case 0:
1932 return env->cp15.c2_base0;
1933 case 1:
1934 return env->cp15.c2_base1;
1935 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001936 return env->cp15.c2_control;
pbrook9ee6e8b2007-11-11 00:04:49 +00001937 default:
1938 goto bad_reg;
1939 }
1940 }
pbrookce819862007-05-08 02:30:40 +00001941 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001942 return env->cp15.c3;
1943 case 4: /* Reserved. */
1944 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001945 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001946 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1947 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001948 switch (op2) {
1949 case 0:
pbrookce819862007-05-08 02:30:40 +00001950 if (arm_feature(env, ARM_FEATURE_MPU))
1951 return simple_mpu_ap_bits(env->cp15.c5_data);
bellardb5ff1b32005-11-26 10:38:39 +00001952 return env->cp15.c5_data;
1953 case 1:
pbrookce819862007-05-08 02:30:40 +00001954 if (arm_feature(env, ARM_FEATURE_MPU))
1955 return simple_mpu_ap_bits(env->cp15.c5_data);
1956 return env->cp15.c5_insn;
1957 case 2:
1958 if (!arm_feature(env, ARM_FEATURE_MPU))
1959 goto bad_reg;
1960 return env->cp15.c5_data;
1961 case 3:
1962 if (!arm_feature(env, ARM_FEATURE_MPU))
1963 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001964 return env->cp15.c5_insn;
1965 default:
1966 goto bad_reg;
1967 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001968 case 6: /* MMU Fault address. */
pbrookce819862007-05-08 02:30:40 +00001969 if (arm_feature(env, ARM_FEATURE_MPU)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001970 if (crm >= 8)
pbrookce819862007-05-08 02:30:40 +00001971 goto bad_reg;
pbrook9ee6e8b2007-11-11 00:04:49 +00001972 return env->cp15.c6_region[crm];
pbrookce819862007-05-08 02:30:40 +00001973 } else {
balrogc3d26892007-07-29 17:57:26 +00001974 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1975 op2 = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001976 switch (op2) {
1977 case 0:
1978 return env->cp15.c6_data;
1979 case 1:
1980 if (arm_feature(env, ARM_FEATURE_V6)) {
1981 /* Watchpoint Fault Adrress. */
1982 return 0; /* Not implemented. */
1983 } else {
1984 /* Instruction Fault Adrress. */
1985 /* Arm9 doesn't have an IFAR, but implementing it anyway
1986 shouldn't do any harm. */
1987 return env->cp15.c6_insn;
1988 }
1989 case 2:
1990 if (arm_feature(env, ARM_FEATURE_V6)) {
1991 /* Instruction Fault Adrress. */
1992 return env->cp15.c6_insn;
1993 } else {
1994 goto bad_reg;
1995 }
1996 default:
1997 goto bad_reg;
1998 }
bellardb5ff1b32005-11-26 10:38:39 +00001999 }
2000 case 7: /* Cache control. */
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01002001 if (crm == 4 && op1 == 0 && op2 == 0) {
2002 return env->cp15.c7_par;
2003 }
pbrook6fbe23d2008-04-01 17:19:11 +00002004 /* FIXME: Should only clear Z flag if destination is r15. */
2005 env->ZF = 0;
bellardb5ff1b32005-11-26 10:38:39 +00002006 return 0;
2007 case 8: /* MMU TLB control. */
2008 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00002009 case 9:
2010 switch (crm) {
2011 case 0: /* Cache lockdown */
2012 switch (op1) {
2013 case 0: /* L1 cache. */
2014 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2015 return 0;
2016 }
2017 switch (op2) {
2018 case 0:
2019 return env->cp15.c9_data;
2020 case 1:
2021 return env->cp15.c9_insn;
2022 default:
2023 goto bad_reg;
2024 }
2025 case 1: /* L2 cache */
2026 if (crm != 0) {
2027 goto bad_reg;
2028 }
2029 /* L2 Lockdown and Auxiliary control. */
2030 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002031 default:
2032 goto bad_reg;
2033 }
Peter Maydell74594c92011-03-22 12:16:16 +00002034 break;
2035 case 12: /* Performance monitor control */
2036 if (!arm_feature(env, ARM_FEATURE_V7)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002037 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00002038 }
2039 switch (op2) {
2040 case 0: /* performance monitor control register */
2041 return env->cp15.c9_pmcr;
2042 case 1: /* count enable set */
2043 case 2: /* count enable clear */
2044 return env->cp15.c9_pmcnten;
2045 case 3: /* overflow flag status */
2046 return env->cp15.c9_pmovsr;
2047 case 4: /* software increment */
2048 case 5: /* event counter selection register */
2049 return 0; /* Unimplemented, RAZ/WI */
2050 default:
2051 goto bad_reg;
2052 }
2053 case 13: /* Performance counters */
2054 if (!arm_feature(env, ARM_FEATURE_V7)) {
2055 goto bad_reg;
2056 }
2057 switch (op2) {
2058 case 1: /* Event type select */
2059 return env->cp15.c9_pmxevtyper;
2060 case 0: /* Cycle count register */
2061 case 2: /* Event count register */
2062 /* Unimplemented, so RAZ/WI */
2063 return 0;
2064 default:
2065 goto bad_reg;
2066 }
2067 case 14: /* Performance monitor control */
2068 if (!arm_feature(env, ARM_FEATURE_V7)) {
2069 goto bad_reg;
2070 }
2071 switch (op2) {
2072 case 0: /* user enable */
2073 return env->cp15.c9_pmuserenr;
2074 case 1: /* interrupt enable set */
2075 case 2: /* interrupt enable clear */
2076 return env->cp15.c9_pminten;
2077 default:
2078 goto bad_reg;
2079 }
bellardb5ff1b32005-11-26 10:38:39 +00002080 default:
2081 goto bad_reg;
2082 }
Peter Maydell74594c92011-03-22 12:16:16 +00002083 break;
bellardb5ff1b32005-11-26 10:38:39 +00002084 case 10: /* MMU TLB lockdown. */
2085 /* ??? TLB lockdown not implemented. */
2086 return 0;
2087 case 11: /* TCM DMA control. */
2088 case 12: /* Reserved. */
2089 goto bad_reg;
2090 case 13: /* Process ID. */
2091 switch (op2) {
2092 case 0:
2093 return env->cp15.c13_fcse;
2094 case 1:
2095 return env->cp15.c13_context;
2096 default:
2097 goto bad_reg;
2098 }
2099 case 14: /* Reserved. */
2100 goto bad_reg;
2101 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00002102 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
balrogc3d26892007-07-29 17:57:26 +00002103 if (op2 == 0 && crm == 1)
balrogc1713132007-04-30 01:26:42 +00002104 return env->cp15.c15_cpar;
2105
2106 goto bad_reg;
2107 }
balrogc3d26892007-07-29 17:57:26 +00002108 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2109 switch (crm) {
2110 case 0:
2111 return 0;
2112 case 1: /* Read TI925T configuration. */
2113 return env->cp15.c15_ticonfig;
2114 case 2: /* Read I_max. */
2115 return env->cp15.c15_i_max;
2116 case 3: /* Read I_min. */
2117 return env->cp15.c15_i_min;
2118 case 4: /* Read thread-ID. */
2119 return env->cp15.c15_threadid;
2120 case 8: /* TI925T_status */
2121 return 0;
2122 }
balrog827df9f2008-04-14 21:05:22 +00002123 /* TODO: Peripheral port remap register:
2124 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2125 * controller base address at $rn & ~0xfff and map size of
2126 * 0x200 << ($rn & 0xfff), when MMU is off. */
balrogc3d26892007-07-29 17:57:26 +00002127 goto bad_reg;
2128 }
bellardb5ff1b32005-11-26 10:38:39 +00002129 return 0;
2130 }
2131bad_reg:
2132 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002133 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2134 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00002135 return 0;
2136}
2137
pbrookb0109802008-03-31 03:47:03 +00002138void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002139{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002140 if ((env->uncached_cpsr & CPSR_M) == mode) {
2141 env->regs[13] = val;
2142 } else {
2143 env->banked_r13[bank_number(mode)] = val;
2144 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002145}
2146
pbrookb0109802008-03-31 03:47:03 +00002147uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00002148{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002149 if ((env->uncached_cpsr & CPSR_M) == mode) {
2150 return env->regs[13];
2151 } else {
2152 return env->banked_r13[bank_number(mode)];
2153 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002154}
2155
pbrook8984bd22008-03-31 03:47:48 +00002156uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00002157{
2158 switch (reg) {
2159 case 0: /* APSR */
2160 return xpsr_read(env) & 0xf8000000;
2161 case 1: /* IAPSR */
2162 return xpsr_read(env) & 0xf80001ff;
2163 case 2: /* EAPSR */
2164 return xpsr_read(env) & 0xff00fc00;
2165 case 3: /* xPSR */
2166 return xpsr_read(env) & 0xff00fdff;
2167 case 5: /* IPSR */
2168 return xpsr_read(env) & 0x000001ff;
2169 case 6: /* EPSR */
2170 return xpsr_read(env) & 0x0700fc00;
2171 case 7: /* IEPSR */
2172 return xpsr_read(env) & 0x0700edff;
2173 case 8: /* MSP */
2174 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2175 case 9: /* PSP */
2176 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2177 case 16: /* PRIMASK */
2178 return (env->uncached_cpsr & CPSR_I) != 0;
Sebastian Huber82845822011-05-29 02:58:41 +00002179 case 17: /* BASEPRI */
2180 case 18: /* BASEPRI_MAX */
pbrook9ee6e8b2007-11-11 00:04:49 +00002181 return env->v7m.basepri;
Sebastian Huber82845822011-05-29 02:58:41 +00002182 case 19: /* FAULTMASK */
2183 return (env->uncached_cpsr & CPSR_F) != 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002184 case 20: /* CONTROL */
2185 return env->v7m.control;
2186 default:
2187 /* ??? For debugging only. */
2188 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2189 return 0;
2190 }
2191}
2192
pbrook8984bd22008-03-31 03:47:48 +00002193void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002194{
2195 switch (reg) {
2196 case 0: /* APSR */
2197 xpsr_write(env, val, 0xf8000000);
2198 break;
2199 case 1: /* IAPSR */
2200 xpsr_write(env, val, 0xf8000000);
2201 break;
2202 case 2: /* EAPSR */
2203 xpsr_write(env, val, 0xfe00fc00);
2204 break;
2205 case 3: /* xPSR */
2206 xpsr_write(env, val, 0xfe00fc00);
2207 break;
2208 case 5: /* IPSR */
2209 /* IPSR bits are readonly. */
2210 break;
2211 case 6: /* EPSR */
2212 xpsr_write(env, val, 0x0600fc00);
2213 break;
2214 case 7: /* IEPSR */
2215 xpsr_write(env, val, 0x0600fc00);
2216 break;
2217 case 8: /* MSP */
2218 if (env->v7m.current_sp)
2219 env->v7m.other_sp = val;
2220 else
2221 env->regs[13] = val;
2222 break;
2223 case 9: /* PSP */
2224 if (env->v7m.current_sp)
2225 env->regs[13] = val;
2226 else
2227 env->v7m.other_sp = val;
2228 break;
2229 case 16: /* PRIMASK */
2230 if (val & 1)
2231 env->uncached_cpsr |= CPSR_I;
2232 else
2233 env->uncached_cpsr &= ~CPSR_I;
2234 break;
Sebastian Huber82845822011-05-29 02:58:41 +00002235 case 17: /* BASEPRI */
2236 env->v7m.basepri = val & 0xff;
2237 break;
2238 case 18: /* BASEPRI_MAX */
2239 val &= 0xff;
2240 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2241 env->v7m.basepri = val;
2242 break;
2243 case 19: /* FAULTMASK */
pbrook9ee6e8b2007-11-11 00:04:49 +00002244 if (val & 1)
2245 env->uncached_cpsr |= CPSR_F;
2246 else
2247 env->uncached_cpsr &= ~CPSR_F;
2248 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00002249 case 20: /* CONTROL */
2250 env->v7m.control = val & 3;
2251 switch_v7m_sp(env, (val & 2) != 0);
2252 break;
2253 default:
2254 /* ??? For debugging only. */
2255 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2256 return;
2257 }
2258}
2259
balrogc1713132007-04-30 01:26:42 +00002260void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
2261 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
2262 void *opaque)
2263{
2264 if (cpnum < 0 || cpnum > 14) {
2265 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2266 return;
2267 }
2268
2269 env->cp[cpnum].cp_read = cp_read;
2270 env->cp[cpnum].cp_write = cp_write;
2271 env->cp[cpnum].opaque = opaque;
2272}
2273
bellardb5ff1b32005-11-26 10:38:39 +00002274#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00002275
2276/* Note that signed overflow is undefined in C. The following routines are
2277 careful to use unsigned types where modulo arithmetic is required.
2278 Failure to do so _will_ break on newer gcc. */
2279
2280/* Signed saturating arithmetic. */
2281
aurel321654b2d2008-04-11 04:55:07 +00002282/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002283static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2284{
2285 uint16_t res;
2286
2287 res = a + b;
2288 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2289 if (a & 0x8000)
2290 res = 0x8000;
2291 else
2292 res = 0x7fff;
2293 }
2294 return res;
2295}
2296
aurel321654b2d2008-04-11 04:55:07 +00002297/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002298static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2299{
2300 uint8_t res;
2301
2302 res = a + b;
2303 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2304 if (a & 0x80)
2305 res = 0x80;
2306 else
2307 res = 0x7f;
2308 }
2309 return res;
2310}
2311
aurel321654b2d2008-04-11 04:55:07 +00002312/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002313static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2314{
2315 uint16_t res;
2316
2317 res = a - b;
2318 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2319 if (a & 0x8000)
2320 res = 0x8000;
2321 else
2322 res = 0x7fff;
2323 }
2324 return res;
2325}
2326
aurel321654b2d2008-04-11 04:55:07 +00002327/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002328static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2329{
2330 uint8_t res;
2331
2332 res = a - b;
2333 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2334 if (a & 0x80)
2335 res = 0x80;
2336 else
2337 res = 0x7f;
2338 }
2339 return res;
2340}
2341
2342#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2343#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2344#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2345#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2346#define PFX q
2347
2348#include "op_addsub.h"
2349
2350/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00002351static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002352{
2353 uint16_t res;
2354 res = a + b;
2355 if (res < a)
2356 res = 0xffff;
2357 return res;
2358}
2359
pbrook460a09c2008-05-01 12:04:35 +00002360static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002361{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002362 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002363 return a - b;
2364 else
2365 return 0;
2366}
2367
2368static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2369{
2370 uint8_t res;
2371 res = a + b;
2372 if (res < a)
2373 res = 0xff;
2374 return res;
2375}
2376
2377static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2378{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002379 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002380 return a - b;
2381 else
2382 return 0;
2383}
2384
2385#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2386#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2387#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2388#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2389#define PFX uq
2390
2391#include "op_addsub.h"
2392
2393/* Signed modulo arithmetic. */
2394#define SARITH16(a, b, n, op) do { \
2395 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002396 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002397 RESULT(sum, n, 16); \
2398 if (sum >= 0) \
2399 ge |= 3 << (n * 2); \
2400 } while(0)
2401
2402#define SARITH8(a, b, n, op) do { \
2403 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002404 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002405 RESULT(sum, n, 8); \
2406 if (sum >= 0) \
2407 ge |= 1 << n; \
2408 } while(0)
2409
2410
2411#define ADD16(a, b, n) SARITH16(a, b, n, +)
2412#define SUB16(a, b, n) SARITH16(a, b, n, -)
2413#define ADD8(a, b, n) SARITH8(a, b, n, +)
2414#define SUB8(a, b, n) SARITH8(a, b, n, -)
2415#define PFX s
2416#define ARITH_GE
2417
2418#include "op_addsub.h"
2419
2420/* Unsigned modulo arithmetic. */
2421#define ADD16(a, b, n) do { \
2422 uint32_t sum; \
2423 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2424 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00002425 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00002426 ge |= 3 << (n * 2); \
2427 } while(0)
2428
2429#define ADD8(a, b, n) do { \
2430 uint32_t sum; \
2431 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2432 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00002433 if ((sum >> 8) == 1) \
2434 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002435 } while(0)
2436
2437#define SUB16(a, b, n) do { \
2438 uint32_t sum; \
2439 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2440 RESULT(sum, n, 16); \
2441 if ((sum >> 16) == 0) \
2442 ge |= 3 << (n * 2); \
2443 } while(0)
2444
2445#define SUB8(a, b, n) do { \
2446 uint32_t sum; \
2447 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2448 RESULT(sum, n, 8); \
2449 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00002450 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002451 } while(0)
2452
2453#define PFX u
2454#define ARITH_GE
2455
2456#include "op_addsub.h"
2457
2458/* Halved signed arithmetic. */
2459#define ADD16(a, b, n) \
2460 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2461#define SUB16(a, b, n) \
2462 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2463#define ADD8(a, b, n) \
2464 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2465#define SUB8(a, b, n) \
2466 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2467#define PFX sh
2468
2469#include "op_addsub.h"
2470
2471/* Halved unsigned arithmetic. */
2472#define ADD16(a, b, n) \
2473 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2474#define SUB16(a, b, n) \
2475 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2476#define ADD8(a, b, n) \
2477 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2478#define SUB8(a, b, n) \
2479 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2480#define PFX uh
2481
2482#include "op_addsub.h"
2483
2484static inline uint8_t do_usad(uint8_t a, uint8_t b)
2485{
2486 if (a > b)
2487 return a - b;
2488 else
2489 return b - a;
2490}
2491
2492/* Unsigned sum of absolute byte differences. */
2493uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2494{
2495 uint32_t sum;
2496 sum = do_usad(a, b);
2497 sum += do_usad(a >> 8, b >> 8);
2498 sum += do_usad(a >> 16, b >>16);
2499 sum += do_usad(a >> 24, b >> 24);
2500 return sum;
2501}
2502
2503/* For ARMv6 SEL instruction. */
2504uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2505{
2506 uint32_t mask;
2507
2508 mask = 0;
2509 if (flags & 1)
2510 mask |= 0xff;
2511 if (flags & 2)
2512 mask |= 0xff00;
2513 if (flags & 4)
2514 mask |= 0xff0000;
2515 if (flags & 8)
2516 mask |= 0xff000000;
2517 return (a & mask) | (b & ~mask);
2518}
2519
pbrook5e3f8782008-03-31 03:47:34 +00002520uint32_t HELPER(logicq_cc)(uint64_t val)
2521{
2522 return (val >> 32) | (val != 0);
2523}
pbrook4373f3c2008-03-31 03:47:19 +00002524
2525/* VFP support. We follow the convention used for VFP instrunctions:
2526 Single precition routines have a "s" suffix, double precision a
2527 "d" suffix. */
2528
2529/* Convert host exception flags to vfp form. */
2530static inline int vfp_exceptbits_from_host(int host_bits)
2531{
2532 int target_bits = 0;
2533
2534 if (host_bits & float_flag_invalid)
2535 target_bits |= 1;
2536 if (host_bits & float_flag_divbyzero)
2537 target_bits |= 2;
2538 if (host_bits & float_flag_overflow)
2539 target_bits |= 4;
Peter Maydell36802b62011-05-19 14:46:18 +01002540 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
pbrook4373f3c2008-03-31 03:47:19 +00002541 target_bits |= 8;
2542 if (host_bits & float_flag_inexact)
2543 target_bits |= 0x10;
Peter Maydellcecd8502011-01-06 19:37:55 +00002544 if (host_bits & float_flag_input_denormal)
2545 target_bits |= 0x80;
pbrook4373f3c2008-03-31 03:47:19 +00002546 return target_bits;
2547}
2548
2549uint32_t HELPER(vfp_get_fpscr)(CPUState *env)
2550{
2551 int i;
2552 uint32_t fpscr;
2553
2554 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2555 | (env->vfp.vec_len << 16)
2556 | (env->vfp.vec_stride << 20);
2557 i = get_float_exception_flags(&env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002558 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002559 fpscr |= vfp_exceptbits_from_host(i);
2560 return fpscr;
2561}
2562
Peter Maydell01653292010-11-24 15:20:04 +00002563uint32_t vfp_get_fpscr(CPUState *env)
2564{
2565 return HELPER(vfp_get_fpscr)(env);
2566}
2567
pbrook4373f3c2008-03-31 03:47:19 +00002568/* Convert vfp exception flags to target form. */
2569static inline int vfp_exceptbits_to_host(int target_bits)
2570{
2571 int host_bits = 0;
2572
2573 if (target_bits & 1)
2574 host_bits |= float_flag_invalid;
2575 if (target_bits & 2)
2576 host_bits |= float_flag_divbyzero;
2577 if (target_bits & 4)
2578 host_bits |= float_flag_overflow;
2579 if (target_bits & 8)
2580 host_bits |= float_flag_underflow;
2581 if (target_bits & 0x10)
2582 host_bits |= float_flag_inexact;
Peter Maydellcecd8502011-01-06 19:37:55 +00002583 if (target_bits & 0x80)
2584 host_bits |= float_flag_input_denormal;
pbrook4373f3c2008-03-31 03:47:19 +00002585 return host_bits;
2586}
2587
2588void HELPER(vfp_set_fpscr)(CPUState *env, uint32_t val)
2589{
2590 int i;
2591 uint32_t changed;
2592
2593 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2594 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2595 env->vfp.vec_len = (val >> 16) & 7;
2596 env->vfp.vec_stride = (val >> 20) & 3;
2597
2598 changed ^= val;
2599 if (changed & (3 << 22)) {
2600 i = (val >> 22) & 3;
2601 switch (i) {
2602 case 0:
2603 i = float_round_nearest_even;
2604 break;
2605 case 1:
2606 i = float_round_up;
2607 break;
2608 case 2:
2609 i = float_round_down;
2610 break;
2611 case 3:
2612 i = float_round_to_zero;
2613 break;
2614 }
2615 set_float_rounding_mode(i, &env->vfp.fp_status);
2616 }
Peter Maydellcecd8502011-01-06 19:37:55 +00002617 if (changed & (1 << 24)) {
pbrookfe76d972008-12-19 14:33:59 +00002618 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
Peter Maydellcecd8502011-01-06 19:37:55 +00002619 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2620 }
pbrook5c7908e2008-12-19 13:53:37 +00002621 if (changed & (1 << 25))
2622 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002623
Peter Maydellb12c3902011-01-06 19:37:54 +00002624 i = vfp_exceptbits_to_host(val);
pbrook4373f3c2008-03-31 03:47:19 +00002625 set_float_exception_flags(i, &env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002626 set_float_exception_flags(0, &env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002627}
2628
Peter Maydell01653292010-11-24 15:20:04 +00002629void vfp_set_fpscr(CPUState *env, uint32_t val)
2630{
2631 HELPER(vfp_set_fpscr)(env, val);
2632}
2633
pbrook4373f3c2008-03-31 03:47:19 +00002634#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2635
2636#define VFP_BINOP(name) \
Peter Maydellae1857e2011-05-25 14:51:48 +00002637float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002638{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002639 float_status *fpst = fpstp; \
2640 return float32_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002641} \
Peter Maydellae1857e2011-05-25 14:51:48 +00002642float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002643{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002644 float_status *fpst = fpstp; \
2645 return float64_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002646}
2647VFP_BINOP(add)
2648VFP_BINOP(sub)
2649VFP_BINOP(mul)
2650VFP_BINOP(div)
2651#undef VFP_BINOP
2652
2653float32 VFP_HELPER(neg, s)(float32 a)
2654{
2655 return float32_chs(a);
2656}
2657
2658float64 VFP_HELPER(neg, d)(float64 a)
2659{
balrog66230e02008-04-20 00:58:01 +00002660 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002661}
2662
2663float32 VFP_HELPER(abs, s)(float32 a)
2664{
2665 return float32_abs(a);
2666}
2667
2668float64 VFP_HELPER(abs, d)(float64 a)
2669{
balrog66230e02008-04-20 00:58:01 +00002670 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002671}
2672
2673float32 VFP_HELPER(sqrt, s)(float32 a, CPUState *env)
2674{
2675 return float32_sqrt(a, &env->vfp.fp_status);
2676}
2677
2678float64 VFP_HELPER(sqrt, d)(float64 a, CPUState *env)
2679{
2680 return float64_sqrt(a, &env->vfp.fp_status);
2681}
2682
2683/* XXX: check quiet/signaling case */
2684#define DO_VFP_cmp(p, type) \
2685void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2686{ \
2687 uint32_t flags; \
2688 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2689 case 0: flags = 0x6; break; \
2690 case -1: flags = 0x8; break; \
2691 case 1: flags = 0x2; break; \
2692 default: case 2: flags = 0x3; break; \
2693 } \
2694 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2695 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2696} \
2697void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2698{ \
2699 uint32_t flags; \
2700 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2701 case 0: flags = 0x6; break; \
2702 case -1: flags = 0x8; break; \
2703 case 1: flags = 0x2; break; \
2704 default: case 2: flags = 0x3; break; \
2705 } \
2706 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2707 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2708}
2709DO_VFP_cmp(s, float32)
2710DO_VFP_cmp(d, float64)
2711#undef DO_VFP_cmp
2712
Peter Maydell5500b062011-05-19 14:46:19 +01002713/* Integer to float and float to integer conversions */
2714
2715#define CONV_ITOF(name, fsz, sign) \
2716 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2717{ \
2718 float_status *fpst = fpstp; \
2719 return sign##int32_to_##float##fsz(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002720}
2721
Peter Maydell5500b062011-05-19 14:46:19 +01002722#define CONV_FTOI(name, fsz, sign, round) \
2723uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2724{ \
2725 float_status *fpst = fpstp; \
2726 if (float##fsz##_is_any_nan(x)) { \
2727 float_raise(float_flag_invalid, fpst); \
2728 return 0; \
2729 } \
2730 return float##fsz##_to_##sign##int32##round(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002731}
2732
Peter Maydell5500b062011-05-19 14:46:19 +01002733#define FLOAT_CONVS(name, p, fsz, sign) \
2734CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2735CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2736CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
pbrook4373f3c2008-03-31 03:47:19 +00002737
Peter Maydell5500b062011-05-19 14:46:19 +01002738FLOAT_CONVS(si, s, 32, )
2739FLOAT_CONVS(si, d, 64, )
2740FLOAT_CONVS(ui, s, 32, u)
2741FLOAT_CONVS(ui, d, 64, u)
pbrook4373f3c2008-03-31 03:47:19 +00002742
Peter Maydell5500b062011-05-19 14:46:19 +01002743#undef CONV_ITOF
2744#undef CONV_FTOI
2745#undef FLOAT_CONVS
pbrook4373f3c2008-03-31 03:47:19 +00002746
2747/* floating point conversion */
2748float64 VFP_HELPER(fcvtd, s)(float32 x, CPUState *env)
2749{
Peter Maydell2d627732010-12-07 15:37:34 +00002750 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2751 /* ARM requires that S<->D conversion of any kind of NaN generates
2752 * a quiet NaN by forcing the most significant frac bit to 1.
2753 */
2754 return float64_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002755}
2756
2757float32 VFP_HELPER(fcvts, d)(float64 x, CPUState *env)
2758{
Peter Maydell2d627732010-12-07 15:37:34 +00002759 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2760 /* ARM requires that S<->D conversion of any kind of NaN generates
2761 * a quiet NaN by forcing the most significant frac bit to 1.
2762 */
2763 return float32_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002764}
2765
2766/* VFP3 fixed point conversion. */
Peter Maydell622465e2011-03-14 07:23:11 +00002767#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
Peter Maydell5500b062011-05-19 14:46:19 +01002768float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2769 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002770{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002771 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002772 float##fsz tmp; \
Peter Maydell5500b062011-05-19 14:46:19 +01002773 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2774 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002775} \
Peter Maydell5500b062011-05-19 14:46:19 +01002776uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2777 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002778{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002779 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002780 float##fsz tmp; \
2781 if (float##fsz##_is_any_nan(x)) { \
Peter Maydell5500b062011-05-19 14:46:19 +01002782 float_raise(float_flag_invalid, fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00002783 return 0; \
Peter Maydell09d94872010-12-07 15:37:34 +00002784 } \
Peter Maydell5500b062011-05-19 14:46:19 +01002785 tmp = float##fsz##_scalbn(x, shift, fpst); \
2786 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002787}
2788
Peter Maydell622465e2011-03-14 07:23:11 +00002789VFP_CONV_FIX(sh, d, 64, int16, )
2790VFP_CONV_FIX(sl, d, 64, int32, )
2791VFP_CONV_FIX(uh, d, 64, uint16, u)
2792VFP_CONV_FIX(ul, d, 64, uint32, u)
2793VFP_CONV_FIX(sh, s, 32, int16, )
2794VFP_CONV_FIX(sl, s, 32, int32, )
2795VFP_CONV_FIX(uh, s, 32, uint16, u)
2796VFP_CONV_FIX(ul, s, 32, uint32, u)
pbrook4373f3c2008-03-31 03:47:19 +00002797#undef VFP_CONV_FIX
2798
Paul Brook60011492009-11-19 16:45:20 +00002799/* Half precision conversions. */
Peter Maydell2d981da2011-02-10 11:29:01 +00002800static float32 do_fcvt_f16_to_f32(uint32_t a, CPUState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002801{
Paul Brook60011492009-11-19 16:45:20 +00002802 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002803 float32 r = float16_to_float32(make_float16(a), ieee, s);
2804 if (ieee) {
2805 return float32_maybe_silence_nan(r);
2806 }
2807 return r;
Paul Brook60011492009-11-19 16:45:20 +00002808}
2809
Peter Maydell2d981da2011-02-10 11:29:01 +00002810static uint32_t do_fcvt_f32_to_f16(float32 a, CPUState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002811{
Paul Brook60011492009-11-19 16:45:20 +00002812 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002813 float16 r = float32_to_float16(a, ieee, s);
2814 if (ieee) {
2815 r = float16_maybe_silence_nan(r);
2816 }
2817 return float16_val(r);
Paul Brook60011492009-11-19 16:45:20 +00002818}
2819
Peter Maydell2d981da2011-02-10 11:29:01 +00002820float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2821{
2822 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2823}
2824
2825uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUState *env)
2826{
2827 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2828}
2829
2830float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2831{
2832 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2833}
2834
2835uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUState *env)
2836{
2837 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2838}
2839
Peter Maydelldda3ec42011-03-14 15:37:12 +00002840#define float32_two make_float32(0x40000000)
Peter Maydell6aae3df2011-03-14 15:37:13 +00002841#define float32_three make_float32(0x40400000)
2842#define float32_one_point_five make_float32(0x3fc00000)
Peter Maydelldda3ec42011-03-14 15:37:12 +00002843
pbrook4373f3c2008-03-31 03:47:19 +00002844float32 HELPER(recps_f32)(float32 a, float32 b, CPUState *env)
2845{
Peter Maydelldda3ec42011-03-14 15:37:12 +00002846 float_status *s = &env->vfp.standard_fp_status;
2847 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2848 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002849 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2850 float_raise(float_flag_input_denormal, s);
2851 }
Peter Maydelldda3ec42011-03-14 15:37:12 +00002852 return float32_two;
2853 }
2854 return float32_sub(float32_two, float32_mul(a, b, s), s);
pbrook4373f3c2008-03-31 03:47:19 +00002855}
2856
2857float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUState *env)
2858{
Peter Maydell71826962011-01-14 20:39:18 +01002859 float_status *s = &env->vfp.standard_fp_status;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002860 float32 product;
2861 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2862 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002863 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2864 float_raise(float_flag_input_denormal, s);
2865 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002866 return float32_one_point_five;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002867 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002868 product = float32_mul(a, b, s);
2869 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
pbrook4373f3c2008-03-31 03:47:19 +00002870}
2871
pbrook8f8e3aa2008-03-31 03:48:01 +00002872/* NEON helpers. */
2873
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01002874/* Constants 256 and 512 are used in some helpers; we avoid relying on
2875 * int->float conversions at run-time. */
2876#define float64_256 make_float64(0x4070000000000000LL)
2877#define float64_512 make_float64(0x4080000000000000LL)
2878
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002879/* The algorithm that must be used to calculate the estimate
2880 * is specified by the ARM ARM.
2881 */
2882static float64 recip_estimate(float64 a, CPUState *env)
2883{
Peter Maydell1146a812011-05-19 14:46:14 +01002884 /* These calculations mustn't set any fp exception flags,
2885 * so we use a local copy of the fp_status.
2886 */
2887 float_status dummy_status = env->vfp.standard_fp_status;
2888 float_status *s = &dummy_status;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002889 /* q = (int)(a * 512.0) */
2890 float64 q = float64_mul(float64_512, a, s);
2891 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2892
2893 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2894 q = int64_to_float64(q_int, s);
2895 q = float64_add(q, float64_half, s);
2896 q = float64_div(q, float64_512, s);
2897 q = float64_div(float64_one, q, s);
2898
2899 /* s = (int)(256.0 * r + 0.5) */
2900 q = float64_mul(q, float64_256, s);
2901 q = float64_add(q, float64_half, s);
2902 q_int = float64_to_int64_round_to_zero(q, s);
2903
2904 /* return (double)s / 256.0 */
2905 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2906}
2907
pbrook4373f3c2008-03-31 03:47:19 +00002908float32 HELPER(recpe_f32)(float32 a, CPUState *env)
2909{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002910 float_status *s = &env->vfp.standard_fp_status;
2911 float64 f64;
2912 uint32_t val32 = float32_val(a);
2913
2914 int result_exp;
2915 int a_exp = (val32 & 0x7f800000) >> 23;
2916 int sign = val32 & 0x80000000;
2917
2918 if (float32_is_any_nan(a)) {
2919 if (float32_is_signaling_nan(a)) {
2920 float_raise(float_flag_invalid, s);
2921 }
2922 return float32_default_nan;
2923 } else if (float32_is_infinity(a)) {
2924 return float32_set_sign(float32_zero, float32_is_neg(a));
2925 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002926 if (!float32_is_zero(a)) {
2927 float_raise(float_flag_input_denormal, s);
2928 }
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002929 float_raise(float_flag_divbyzero, s);
2930 return float32_set_sign(float32_infinity, float32_is_neg(a));
2931 } else if (a_exp >= 253) {
2932 float_raise(float_flag_underflow, s);
2933 return float32_set_sign(float32_zero, float32_is_neg(a));
2934 }
2935
2936 f64 = make_float64((0x3feULL << 52)
2937 | ((int64_t)(val32 & 0x7fffff) << 29));
2938
2939 result_exp = 253 - a_exp;
2940
2941 f64 = recip_estimate(f64, env);
2942
2943 val32 = sign
2944 | ((result_exp & 0xff) << 23)
2945 | ((float64_val(f64) >> 29) & 0x7fffff);
2946 return make_float32(val32);
pbrook4373f3c2008-03-31 03:47:19 +00002947}
2948
Christophe Lyone07be5d2011-02-21 17:38:48 +01002949/* The algorithm that must be used to calculate the estimate
2950 * is specified by the ARM ARM.
2951 */
2952static float64 recip_sqrt_estimate(float64 a, CPUState *env)
2953{
Peter Maydell1146a812011-05-19 14:46:14 +01002954 /* These calculations mustn't set any fp exception flags,
2955 * so we use a local copy of the fp_status.
2956 */
2957 float_status dummy_status = env->vfp.standard_fp_status;
2958 float_status *s = &dummy_status;
Christophe Lyone07be5d2011-02-21 17:38:48 +01002959 float64 q;
2960 int64_t q_int;
2961
2962 if (float64_lt(a, float64_half, s)) {
2963 /* range 0.25 <= a < 0.5 */
2964
2965 /* a in units of 1/512 rounded down */
2966 /* q0 = (int)(a * 512.0); */
2967 q = float64_mul(float64_512, a, s);
2968 q_int = float64_to_int64_round_to_zero(q, s);
2969
2970 /* reciprocal root r */
2971 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2972 q = int64_to_float64(q_int, s);
2973 q = float64_add(q, float64_half, s);
2974 q = float64_div(q, float64_512, s);
2975 q = float64_sqrt(q, s);
2976 q = float64_div(float64_one, q, s);
2977 } else {
2978 /* range 0.5 <= a < 1.0 */
2979
2980 /* a in units of 1/256 rounded down */
2981 /* q1 = (int)(a * 256.0); */
2982 q = float64_mul(float64_256, a, s);
2983 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2984
2985 /* reciprocal root r */
2986 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2987 q = int64_to_float64(q_int, s);
2988 q = float64_add(q, float64_half, s);
2989 q = float64_div(q, float64_256, s);
2990 q = float64_sqrt(q, s);
2991 q = float64_div(float64_one, q, s);
2992 }
2993 /* r in units of 1/256 rounded to nearest */
2994 /* s = (int)(256.0 * r + 0.5); */
2995
2996 q = float64_mul(q, float64_256,s );
2997 q = float64_add(q, float64_half, s);
2998 q_int = float64_to_int64_round_to_zero(q, s);
2999
3000 /* return (double)s / 256.0;*/
3001 return float64_div(int64_to_float64(q_int, s), float64_256, s);
3002}
3003
pbrook4373f3c2008-03-31 03:47:19 +00003004float32 HELPER(rsqrte_f32)(float32 a, CPUState *env)
3005{
Christophe Lyone07be5d2011-02-21 17:38:48 +01003006 float_status *s = &env->vfp.standard_fp_status;
3007 int result_exp;
3008 float64 f64;
3009 uint32_t val;
3010 uint64_t val64;
3011
3012 val = float32_val(a);
3013
3014 if (float32_is_any_nan(a)) {
3015 if (float32_is_signaling_nan(a)) {
3016 float_raise(float_flag_invalid, s);
3017 }
3018 return float32_default_nan;
3019 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01003020 if (!float32_is_zero(a)) {
3021 float_raise(float_flag_input_denormal, s);
3022 }
Christophe Lyone07be5d2011-02-21 17:38:48 +01003023 float_raise(float_flag_divbyzero, s);
3024 return float32_set_sign(float32_infinity, float32_is_neg(a));
3025 } else if (float32_is_neg(a)) {
3026 float_raise(float_flag_invalid, s);
3027 return float32_default_nan;
3028 } else if (float32_is_infinity(a)) {
3029 return float32_zero;
3030 }
3031
3032 /* Normalize to a double-precision value between 0.25 and 1.0,
3033 * preserving the parity of the exponent. */
3034 if ((val & 0x800000) == 0) {
3035 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3036 | (0x3feULL << 52)
3037 | ((uint64_t)(val & 0x7fffff) << 29));
3038 } else {
3039 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3040 | (0x3fdULL << 52)
3041 | ((uint64_t)(val & 0x7fffff) << 29));
3042 }
3043
3044 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3045
3046 f64 = recip_sqrt_estimate(f64, env);
3047
3048 val64 = float64_val(f64);
3049
Christophe LYON26cc6ab2011-10-19 16:14:05 +00003050 val = ((result_exp & 0xff) << 23)
Christophe Lyone07be5d2011-02-21 17:38:48 +01003051 | ((val64 >> 29) & 0x7fffff);
3052 return make_float32(val);
pbrook4373f3c2008-03-31 03:47:19 +00003053}
3054
3055uint32_t HELPER(recpe_u32)(uint32_t a, CPUState *env)
3056{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003057 float64 f64;
3058
3059 if ((a & 0x80000000) == 0) {
3060 return 0xffffffff;
3061 }
3062
3063 f64 = make_float64((0x3feULL << 52)
3064 | ((int64_t)(a & 0x7fffffff) << 21));
3065
3066 f64 = recip_estimate (f64, env);
3067
3068 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003069}
3070
3071uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)
3072{
Christophe Lyone07be5d2011-02-21 17:38:48 +01003073 float64 f64;
3074
3075 if ((a & 0xc0000000) == 0) {
3076 return 0xffffffff;
3077 }
3078
3079 if (a & 0x80000000) {
3080 f64 = make_float64((0x3feULL << 52)
3081 | ((uint64_t)(a & 0x7fffffff) << 21));
3082 } else { /* bits 31-30 == '01' */
3083 f64 = make_float64((0x3fdULL << 52)
3084 | ((uint64_t)(a & 0x3fffffff) << 22));
3085 }
3086
3087 f64 = recip_sqrt_estimate(f64, env);
3088
3089 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003090}
pbrookfe1479c2008-12-19 13:18:36 +00003091
Peter Maydellda97f522011-10-19 16:14:07 +00003092/* VFPv4 fused multiply-accumulate */
3093float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
3094{
3095 float_status *fpst = fpstp;
3096 return float32_muladd(a, b, c, 0, fpst);
3097}
3098
3099float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
3100{
3101 float_status *fpst = fpstp;
3102 return float64_muladd(a, b, c, 0, fpst);
3103}
3104
pbrookfe1479c2008-12-19 13:18:36 +00003105void HELPER(set_teecr)(CPUState *env, uint32_t val)
3106{
3107 val &= 1;
3108 if (env->teecr != val) {
3109 env->teecr = val;
3110 tb_flush(env);
3111 }
3112}