blob: 3465776dbe37971e0520ec6e40d98a58944b4d0d [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;
balrog827df9f2008-04-14 21:05:22 +000079 case ARM_CPUID_ARM1136_R2:
pbrook9ee6e8b2007-11-11 00:04:49 +000080 case ARM_CPUID_ARM1136:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +040081 set_feature(env, ARM_FEATURE_V4T);
82 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +000083 set_feature(env, ARM_FEATURE_V6);
84 set_feature(env, ARM_FEATURE_VFP);
85 set_feature(env, ARM_FEATURE_AUXCR);
86 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
87 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
88 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
89 memcpy(env->cp15.c0_c1, arm1136_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +000090 memcpy(env->cp15.c0_c2, arm1136_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +000091 env->cp15.c0_cachetype = 0x1dd20d2;
Juha Riihimäki16440c52010-12-08 13:15:18 +020092 env->cp15.c1_sys = 0x00050078;
pbrook9ee6e8b2007-11-11 00:04:49 +000093 break;
Jamie Iles7807eed2011-07-20 10:32:54 +000094 case ARM_CPUID_ARM1176:
95 set_feature(env, ARM_FEATURE_V4T);
96 set_feature(env, ARM_FEATURE_V5);
97 set_feature(env, ARM_FEATURE_V6);
98 set_feature(env, ARM_FEATURE_V6K);
99 set_feature(env, ARM_FEATURE_VFP);
100 set_feature(env, ARM_FEATURE_AUXCR);
101 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b5;
102 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
103 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
104 memcpy(env->cp15.c0_c1, arm1176_cp15_c0_c1, 8 * sizeof(uint32_t));
105 memcpy(env->cp15.c0_c2, arm1176_cp15_c0_c2, 8 * sizeof(uint32_t));
106 env->cp15.c0_cachetype = 0x1dd20d2;
107 env->cp15.c1_sys = 0x00050078;
108 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000109 case ARM_CPUID_ARM11MPCORE:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400110 set_feature(env, ARM_FEATURE_V4T);
111 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +0000112 set_feature(env, ARM_FEATURE_V6);
113 set_feature(env, ARM_FEATURE_V6K);
114 set_feature(env, ARM_FEATURE_VFP);
115 set_feature(env, ARM_FEATURE_AUXCR);
116 env->vfp.xregs[ARM_VFP_FPSID] = 0x410120b4;
117 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11111111;
118 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00000000;
119 memcpy(env->cp15.c0_c1, mpcore_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000120 memcpy(env->cp15.c0_c2, mpcore_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrook9ee6e8b2007-11-11 00:04:49 +0000121 env->cp15.c0_cachetype = 0x1dd20d2;
122 break;
123 case ARM_CPUID_CORTEXA8:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400124 set_feature(env, ARM_FEATURE_V4T);
125 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +0000126 set_feature(env, ARM_FEATURE_V6);
127 set_feature(env, ARM_FEATURE_V6K);
128 set_feature(env, ARM_FEATURE_V7);
129 set_feature(env, ARM_FEATURE_AUXCR);
130 set_feature(env, ARM_FEATURE_THUMB2);
131 set_feature(env, ARM_FEATURE_VFP);
132 set_feature(env, ARM_FEATURE_VFP3);
133 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +0000134 set_feature(env, ARM_FEATURE_THUMB2EE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000135 env->vfp.xregs[ARM_VFP_FPSID] = 0x410330c0;
136 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
137 env->vfp.xregs[ARM_VFP_MVFR1] = 0x00011100;
138 memcpy(env->cp15.c0_c1, cortexa8_cp15_c0_c1, 8 * sizeof(uint32_t));
balrog22478e72008-07-19 10:12:22 +0000139 memcpy(env->cp15.c0_c2, cortexa8_cp15_c0_c2, 8 * sizeof(uint32_t));
pbrooka49ea272008-12-19 13:37:53 +0000140 env->cp15.c0_cachetype = 0x82048004;
141 env->cp15.c0_clid = (1 << 27) | (2 << 24) | 3;
142 env->cp15.c0_ccsid[0] = 0xe007e01a; /* 16k L1 dcache. */
143 env->cp15.c0_ccsid[1] = 0x2007e01a; /* 16k L1 icache. */
144 env->cp15.c0_ccsid[2] = 0xf0000000; /* No L2 icache. */
Mattias Holm9c486ad2010-12-08 13:15:17 +0200145 env->cp15.c1_sys = 0x00c50078;
pbrook9ee6e8b2007-11-11 00:04:49 +0000146 break;
Paul Brook10055562009-11-19 16:45:20 +0000147 case ARM_CPUID_CORTEXA9:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400148 set_feature(env, ARM_FEATURE_V4T);
149 set_feature(env, ARM_FEATURE_V5);
Paul Brook10055562009-11-19 16:45:20 +0000150 set_feature(env, ARM_FEATURE_V6);
151 set_feature(env, ARM_FEATURE_V6K);
152 set_feature(env, ARM_FEATURE_V7);
153 set_feature(env, ARM_FEATURE_AUXCR);
154 set_feature(env, ARM_FEATURE_THUMB2);
155 set_feature(env, ARM_FEATURE_VFP);
156 set_feature(env, ARM_FEATURE_VFP3);
157 set_feature(env, ARM_FEATURE_VFP_FP16);
158 set_feature(env, ARM_FEATURE_NEON);
159 set_feature(env, ARM_FEATURE_THUMB2EE);
Peter Maydelle1bbf442011-02-03 19:43:22 +0000160 /* Note that A9 supports the MP extensions even for
161 * A9UP and single-core A9MP (which are both different
162 * and valid configurations; we don't model A9UP).
163 */
164 set_feature(env, ARM_FEATURE_V7MP);
Paul Brook10055562009-11-19 16:45:20 +0000165 env->vfp.xregs[ARM_VFP_FPSID] = 0x41034000; /* Guess */
166 env->vfp.xregs[ARM_VFP_MVFR0] = 0x11110222;
167 env->vfp.xregs[ARM_VFP_MVFR1] = 0x01111111;
168 memcpy(env->cp15.c0_c1, cortexa9_cp15_c0_c1, 8 * sizeof(uint32_t));
169 memcpy(env->cp15.c0_c2, cortexa9_cp15_c0_c2, 8 * sizeof(uint32_t));
170 env->cp15.c0_cachetype = 0x80038003;
171 env->cp15.c0_clid = (1 << 27) | (1 << 24) | 3;
172 env->cp15.c0_ccsid[0] = 0xe00fe015; /* 16k L1 dcache. */
173 env->cp15.c0_ccsid[1] = 0x200fe015; /* 16k L1 icache. */
Juha Riihimäki16440c52010-12-08 13:15:18 +0200174 env->cp15.c1_sys = 0x00c50078;
Paul Brook10055562009-11-19 16:45:20 +0000175 break;
pbrook9ee6e8b2007-11-11 00:04:49 +0000176 case ARM_CPUID_CORTEXM3:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400177 set_feature(env, ARM_FEATURE_V4T);
178 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +0000179 set_feature(env, ARM_FEATURE_V6);
180 set_feature(env, ARM_FEATURE_THUMB2);
181 set_feature(env, ARM_FEATURE_V7);
182 set_feature(env, ARM_FEATURE_M);
183 set_feature(env, ARM_FEATURE_DIV);
184 break;
185 case ARM_CPUID_ANY: /* For userspace emulation. */
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400186 set_feature(env, ARM_FEATURE_V4T);
187 set_feature(env, ARM_FEATURE_V5);
pbrook9ee6e8b2007-11-11 00:04:49 +0000188 set_feature(env, ARM_FEATURE_V6);
189 set_feature(env, ARM_FEATURE_V6K);
190 set_feature(env, ARM_FEATURE_V7);
191 set_feature(env, ARM_FEATURE_THUMB2);
192 set_feature(env, ARM_FEATURE_VFP);
193 set_feature(env, ARM_FEATURE_VFP3);
Paul Brook60011492009-11-19 16:45:20 +0000194 set_feature(env, ARM_FEATURE_VFP_FP16);
pbrook9ee6e8b2007-11-11 00:04:49 +0000195 set_feature(env, ARM_FEATURE_NEON);
pbrookfe1479c2008-12-19 13:18:36 +0000196 set_feature(env, ARM_FEATURE_THUMB2EE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000197 set_feature(env, ARM_FEATURE_DIV);
Peter Maydelle1bbf442011-02-03 19:43:22 +0000198 set_feature(env, ARM_FEATURE_V7MP);
pbrook9ee6e8b2007-11-11 00:04:49 +0000199 break;
balrogc3d26892007-07-29 17:57:26 +0000200 case ARM_CPUID_TI915T:
201 case ARM_CPUID_TI925T:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400202 set_feature(env, ARM_FEATURE_V4T);
balrogc3d26892007-07-29 17:57:26 +0000203 set_feature(env, ARM_FEATURE_OMAPCP);
204 env->cp15.c0_cpuid = ARM_CPUID_TI925T; /* Depends on wiring. */
205 env->cp15.c0_cachetype = 0x5109149;
206 env->cp15.c1_sys = 0x00000070;
207 env->cp15.c15_i_max = 0x000;
208 env->cp15.c15_i_min = 0xff0;
209 break;
balrogc1713132007-04-30 01:26:42 +0000210 case ARM_CPUID_PXA250:
211 case ARM_CPUID_PXA255:
212 case ARM_CPUID_PXA260:
213 case ARM_CPUID_PXA261:
214 case ARM_CPUID_PXA262:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400215 set_feature(env, ARM_FEATURE_V4T);
216 set_feature(env, ARM_FEATURE_V5);
balrogc1713132007-04-30 01:26:42 +0000217 set_feature(env, ARM_FEATURE_XSCALE);
218 /* JTAG_ID is ((id << 28) | 0x09265013) */
219 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000220 env->cp15.c1_sys = 0x00000078;
balrogc1713132007-04-30 01:26:42 +0000221 break;
222 case ARM_CPUID_PXA270_A0:
223 case ARM_CPUID_PXA270_A1:
224 case ARM_CPUID_PXA270_B0:
225 case ARM_CPUID_PXA270_B1:
226 case ARM_CPUID_PXA270_C0:
227 case ARM_CPUID_PXA270_C5:
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400228 set_feature(env, ARM_FEATURE_V4T);
229 set_feature(env, ARM_FEATURE_V5);
balrogc1713132007-04-30 01:26:42 +0000230 set_feature(env, ARM_FEATURE_XSCALE);
231 /* JTAG_ID is ((id << 28) | 0x09265013) */
balrog18c9b562007-04-30 02:02:17 +0000232 set_feature(env, ARM_FEATURE_IWMMXT);
233 env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
balrogc1713132007-04-30 01:26:42 +0000234 env->cp15.c0_cachetype = 0xd172172;
balrog610c3c82007-06-24 12:09:48 +0000235 env->cp15.c1_sys = 0x00000078;
pbrookf3d6b952007-03-11 13:03:18 +0000236 break;
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +0400237 case ARM_CPUID_SA1100:
238 case ARM_CPUID_SA1110:
239 set_feature(env, ARM_FEATURE_STRONGARM);
240 env->cp15.c1_sys = 0x00000070;
241 break;
pbrookf3d6b952007-03-11 13:03:18 +0000242 default:
243 cpu_abort(env, "Bad CPU ID: %x\n", id);
244 break;
245 }
246}
247
pbrook40f137e2006-02-20 00:33:36 +0000248void cpu_reset(CPUARMState *env)
249{
pbrookf3d6b952007-03-11 13:03:18 +0000250 uint32_t id;
aliguorieca1bdf2009-01-26 19:54:31 +0000251
252 if (qemu_loglevel_mask(CPU_LOG_RESET)) {
253 qemu_log("CPU Reset (CPU %d)\n", env->cpu_index);
254 log_cpu_state(env, 0);
255 }
256
pbrookf3d6b952007-03-11 13:03:18 +0000257 id = env->cp15.c0_cpuid;
258 memset(env, 0, offsetof(CPUARMState, breakpoints));
259 if (id)
260 cpu_reset_model_id(env, id);
pbrook40f137e2006-02-20 00:33:36 +0000261#if defined (CONFIG_USER_ONLY)
262 env->uncached_cpsr = ARM_CPU_MODE_USR;
Peter Maydell3a807de2010-11-19 15:36:47 +0000263 /* For user mode we must enable access to coprocessors */
pbrook40f137e2006-02-20 00:33:36 +0000264 env->vfp.xregs[ARM_VFP_FPEXC] = 1 << 30;
Peter Maydell3a807de2010-11-19 15:36:47 +0000265 if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
266 env->cp15.c15_cpar = 3;
267 } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
268 env->cp15.c15_cpar = 1;
269 }
pbrook40f137e2006-02-20 00:33:36 +0000270#else
271 /* SVC mode with interrupts disabled. */
272 env->uncached_cpsr = ARM_CPU_MODE_SVC | CPSR_A | CPSR_F | CPSR_I;
pbrook9ee6e8b2007-11-11 00:04:49 +0000273 /* On ARMv7-M the CPSR_I is the value of the PRIMASK register, and is
Paul Brook983fe822010-04-05 19:34:51 +0100274 clear at reset. Initial SP and PC are loaded from ROM. */
275 if (IS_M(env)) {
276 uint32_t pc;
277 uint8_t *rom;
pbrook9ee6e8b2007-11-11 00:04:49 +0000278 env->uncached_cpsr &= ~CPSR_I;
Paul Brook983fe822010-04-05 19:34:51 +0100279 rom = rom_ptr(0);
280 if (rom) {
281 /* We should really use ldl_phys here, in case the guest
282 modified flash and reset itself. However images
283 loaded via -kenrel have not been copied yet, so load the
284 values directly from there. */
285 env->regs[13] = ldl_p(rom);
286 pc = ldl_p(rom + 4);
287 env->thumb = pc & 1;
288 env->regs[15] = pc & ~1;
289 }
290 }
pbrook40f137e2006-02-20 00:33:36 +0000291 env->vfp.xregs[ARM_VFP_FPEXC] = 0;
pbrookb2fa1792008-10-22 19:22:30 +0000292 env->cp15.c2_base_mask = 0xffffc000u;
Peter Maydell74594c92011-03-22 12:16:16 +0000293 /* v7 performance monitor control register: same implementor
294 * field as main ID register, and we implement no event counters.
295 */
296 env->cp15.c9_pmcr = (id & 0xff000000);
pbrook40f137e2006-02-20 00:33:36 +0000297#endif
Peter Maydell3a492f32011-01-14 20:39:18 +0100298 set_flush_to_zero(1, &env->vfp.standard_fp_status);
299 set_flush_inputs_to_zero(1, &env->vfp.standard_fp_status);
300 set_default_nan_mode(1, &env->vfp.standard_fp_status);
Peter Maydell9df38c42011-04-12 13:56:41 +0100301 set_float_detect_tininess(float_tininess_before_rounding,
302 &env->vfp.fp_status);
303 set_float_detect_tininess(float_tininess_before_rounding,
304 &env->vfp.standard_fp_status);
pbrookf3d6b952007-03-11 13:03:18 +0000305 tlb_flush(env, 1);
pbrook40f137e2006-02-20 00:33:36 +0000306}
307
pbrook56aebc82008-10-11 17:55:29 +0000308static int vfp_gdb_get_reg(CPUState *env, uint8_t *buf, int reg)
309{
310 int nregs;
311
312 /* VFP data registers are always little-endian. */
313 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
314 if (reg < nregs) {
315 stfq_le_p(buf, env->vfp.regs[reg]);
316 return 8;
317 }
318 if (arm_feature(env, ARM_FEATURE_NEON)) {
319 /* Aliases for Q regs. */
320 nregs += 16;
321 if (reg < nregs) {
322 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
323 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
324 return 16;
325 }
326 }
327 switch (reg - nregs) {
328 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
329 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
330 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
331 }
332 return 0;
333}
334
335static int vfp_gdb_set_reg(CPUState *env, uint8_t *buf, int reg)
336{
337 int nregs;
338
339 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
340 if (reg < nregs) {
341 env->vfp.regs[reg] = ldfq_le_p(buf);
342 return 8;
343 }
344 if (arm_feature(env, ARM_FEATURE_NEON)) {
345 nregs += 16;
346 if (reg < nregs) {
347 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
348 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
349 return 16;
350 }
351 }
352 switch (reg - nregs) {
353 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
354 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +0200355 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +0000356 }
357 return 0;
358}
359
bellardaaed9092007-11-10 15:15:54 +0000360CPUARMState *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +0000361{
362 CPUARMState *env;
bellardaaed9092007-11-10 15:15:54 +0000363 uint32_t id;
pbrookb26eefb2008-03-31 03:44:26 +0000364 static int inited = 0;
pbrook40f137e2006-02-20 00:33:36 +0000365
bellardaaed9092007-11-10 15:15:54 +0000366 id = cpu_arm_find_by_name(cpu_model);
367 if (id == 0)
368 return NULL;
pbrook40f137e2006-02-20 00:33:36 +0000369 env = qemu_mallocz(sizeof(CPUARMState));
pbrook40f137e2006-02-20 00:33:36 +0000370 cpu_exec_init(env);
pbrookb26eefb2008-03-31 03:44:26 +0000371 if (!inited) {
372 inited = 1;
373 arm_translate_init();
374 }
375
ths01ba9812007-12-09 02:22:57 +0000376 env->cpu_model_str = cpu_model;
bellardaaed9092007-11-10 15:15:54 +0000377 env->cp15.c0_cpuid = id;
pbrook40f137e2006-02-20 00:33:36 +0000378 cpu_reset(env);
pbrook56aebc82008-10-11 17:55:29 +0000379 if (arm_feature(env, ARM_FEATURE_NEON)) {
380 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
381 51, "arm-neon.xml", 0);
382 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
383 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
384 35, "arm-vfp3.xml", 0);
385 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
386 gdb_register_coprocessor(env, vfp_gdb_get_reg, vfp_gdb_set_reg,
387 19, "arm-vfp.xml", 0);
388 }
aliguori0bf46a42009-04-24 18:03:41 +0000389 qemu_init_vcpu(env);
pbrook40f137e2006-02-20 00:33:36 +0000390 return env;
391}
392
pbrook3371d272007-03-08 03:04:12 +0000393struct arm_cpu_t {
394 uint32_t id;
395 const char *name;
396};
397
398static const struct arm_cpu_t arm_cpu_names[] = {
399 { ARM_CPUID_ARM926, "arm926"},
pbrookce819862007-05-08 02:30:40 +0000400 { ARM_CPUID_ARM946, "arm946"},
pbrook3371d272007-03-08 03:04:12 +0000401 { ARM_CPUID_ARM1026, "arm1026"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000402 { ARM_CPUID_ARM1136, "arm1136"},
balrog827df9f2008-04-14 21:05:22 +0000403 { ARM_CPUID_ARM1136_R2, "arm1136-r2"},
Jamie Iles7807eed2011-07-20 10:32:54 +0000404 { ARM_CPUID_ARM1176, "arm1176"},
pbrook9ee6e8b2007-11-11 00:04:49 +0000405 { ARM_CPUID_ARM11MPCORE, "arm11mpcore"},
406 { ARM_CPUID_CORTEXM3, "cortex-m3"},
407 { ARM_CPUID_CORTEXA8, "cortex-a8"},
Paul Brook10055562009-11-19 16:45:20 +0000408 { ARM_CPUID_CORTEXA9, "cortex-a9"},
balrogc3d26892007-07-29 17:57:26 +0000409 { ARM_CPUID_TI925T, "ti925t" },
balrogc1713132007-04-30 01:26:42 +0000410 { ARM_CPUID_PXA250, "pxa250" },
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +0400411 { ARM_CPUID_SA1100, "sa1100" },
412 { ARM_CPUID_SA1110, "sa1110" },
balrogc1713132007-04-30 01:26:42 +0000413 { ARM_CPUID_PXA255, "pxa255" },
414 { ARM_CPUID_PXA260, "pxa260" },
415 { ARM_CPUID_PXA261, "pxa261" },
416 { ARM_CPUID_PXA262, "pxa262" },
417 { ARM_CPUID_PXA270, "pxa270" },
418 { ARM_CPUID_PXA270_A0, "pxa270-a0" },
419 { ARM_CPUID_PXA270_A1, "pxa270-a1" },
420 { ARM_CPUID_PXA270_B0, "pxa270-b0" },
421 { ARM_CPUID_PXA270_B1, "pxa270-b1" },
422 { ARM_CPUID_PXA270_C0, "pxa270-c0" },
423 { ARM_CPUID_PXA270_C5, "pxa270-c5" },
pbrook9ee6e8b2007-11-11 00:04:49 +0000424 { ARM_CPUID_ANY, "any"},
pbrook3371d272007-03-08 03:04:12 +0000425 { 0, NULL}
426};
427
Stefan Weil9a78eea2010-10-22 23:03:33 +0200428void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
pbrook5adb4832007-03-08 03:15:18 +0000429{
430 int i;
431
j_mayerc732abe2007-10-12 06:47:46 +0000432 (*cpu_fprintf)(f, "Available CPUs:\n");
pbrook5adb4832007-03-08 03:15:18 +0000433 for (i = 0; arm_cpu_names[i].name; i++) {
j_mayerc732abe2007-10-12 06:47:46 +0000434 (*cpu_fprintf)(f, " %s\n", arm_cpu_names[i].name);
pbrook5adb4832007-03-08 03:15:18 +0000435 }
436}
437
bellardaaed9092007-11-10 15:15:54 +0000438/* return 0 if not found */
439static uint32_t cpu_arm_find_by_name(const char *name)
pbrook40f137e2006-02-20 00:33:36 +0000440{
pbrook3371d272007-03-08 03:04:12 +0000441 int i;
442 uint32_t id;
443
444 id = 0;
pbrook3371d272007-03-08 03:04:12 +0000445 for (i = 0; arm_cpu_names[i].name; i++) {
446 if (strcmp(name, arm_cpu_names[i].name) == 0) {
447 id = arm_cpu_names[i].id;
448 break;
449 }
450 }
bellardaaed9092007-11-10 15:15:54 +0000451 return id;
pbrook40f137e2006-02-20 00:33:36 +0000452}
453
454void cpu_arm_close(CPUARMState *env)
455{
456 free(env);
457}
458
balrog2f4a40e2007-11-13 01:50:15 +0000459uint32_t cpsr_read(CPUARMState *env)
460{
461 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +0000462 ZF = (env->ZF == 0);
463 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +0000464 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
465 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
466 | ((env->condexec_bits & 0xfc) << 8)
467 | (env->GE << 16);
468}
469
470void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask)
471{
balrog2f4a40e2007-11-13 01:50:15 +0000472 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +0000473 env->ZF = (~val) & CPSR_Z;
474 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +0000475 env->CF = (val >> 29) & 1;
476 env->VF = (val << 3) & 0x80000000;
477 }
478 if (mask & CPSR_Q)
479 env->QF = ((val & CPSR_Q) != 0);
480 if (mask & CPSR_T)
481 env->thumb = ((val & CPSR_T) != 0);
482 if (mask & CPSR_IT_0_1) {
483 env->condexec_bits &= ~3;
484 env->condexec_bits |= (val >> 25) & 3;
485 }
486 if (mask & CPSR_IT_2_7) {
487 env->condexec_bits &= 3;
488 env->condexec_bits |= (val >> 8) & 0xfc;
489 }
490 if (mask & CPSR_GE) {
491 env->GE = (val >> 16) & 0xf;
492 }
493
494 if ((env->uncached_cpsr ^ val) & mask & CPSR_M) {
495 switch_mode(env, val & CPSR_M);
496 }
497 mask &= ~CACHED_CPSR_BITS;
498 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
499}
500
pbrookb26eefb2008-03-31 03:44:26 +0000501/* Sign/zero extend */
502uint32_t HELPER(sxtb16)(uint32_t x)
503{
504 uint32_t res;
505 res = (uint16_t)(int8_t)x;
506 res |= (uint32_t)(int8_t)(x >> 16) << 16;
507 return res;
508}
509
510uint32_t HELPER(uxtb16)(uint32_t x)
511{
512 uint32_t res;
513 res = (uint16_t)(uint8_t)x;
514 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
515 return res;
516}
517
pbrookf51bbbf2008-03-31 03:45:13 +0000518uint32_t HELPER(clz)(uint32_t x)
519{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +0200520 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +0000521}
522
pbrook36706692008-03-31 03:46:19 +0000523int32_t HELPER(sdiv)(int32_t num, int32_t den)
524{
525 if (den == 0)
526 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +0200527 if (num == INT_MIN && den == -1)
528 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +0000529 return num / den;
530}
531
532uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
533{
534 if (den == 0)
535 return 0;
536 return num / den;
537}
538
539uint32_t HELPER(rbit)(uint32_t x)
540{
541 x = ((x & 0xff000000) >> 24)
542 | ((x & 0x00ff0000) >> 8)
543 | ((x & 0x0000ff00) << 8)
544 | ((x & 0x000000ff) << 24);
545 x = ((x & 0xf0f0f0f0) >> 4)
546 | ((x & 0x0f0f0f0f) << 4);
547 x = ((x & 0x88888888) >> 3)
548 | ((x & 0x44444444) >> 1)
549 | ((x & 0x22222222) << 1)
550 | ((x & 0x11111111) << 3);
551 return x;
552}
553
pbrookad694712008-03-31 03:48:30 +0000554uint32_t HELPER(abs)(uint32_t x)
555{
556 return ((int32_t)x < 0) ? -x : x;
557}
558
ths5fafdf22007-09-16 21:08:06 +0000559#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +0000560
561void do_interrupt (CPUState *env)
562{
563 env->exception_index = -1;
564}
565
566int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
j_mayer6ebbf392007-10-14 07:07:08 +0000567 int mmu_idx, int is_softmmu)
bellardb5ff1b32005-11-26 10:38:39 +0000568{
569 if (rw == 2) {
570 env->exception_index = EXCP_PREFETCH_ABORT;
571 env->cp15.c6_insn = address;
572 } else {
573 env->exception_index = EXCP_DATA_ABORT;
574 env->cp15.c6_data = address;
575 }
576 return 1;
577}
578
bellardb5ff1b32005-11-26 10:38:39 +0000579/* These should probably raise undefined insn exceptions. */
pbrook8984bd22008-03-31 03:47:48 +0000580void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +0000581{
582 int op1 = (insn >> 8) & 0xf;
583 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
584 return;
585}
586
pbrook8984bd22008-03-31 03:47:48 +0000587uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +0000588{
589 int op1 = (insn >> 8) & 0xf;
590 cpu_abort(env, "cp%i insn %08x\n", op1, insn);
591 return 0;
592}
593
pbrook8984bd22008-03-31 03:47:48 +0000594void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +0000595{
596 cpu_abort(env, "cp15 insn %08x\n", insn);
597}
598
pbrook8984bd22008-03-31 03:47:48 +0000599uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +0000600{
601 cpu_abort(env, "cp15 insn %08x\n", insn);
bellardb5ff1b32005-11-26 10:38:39 +0000602}
603
pbrook9ee6e8b2007-11-11 00:04:49 +0000604/* These should probably raise undefined insn exceptions. */
pbrook8984bd22008-03-31 03:47:48 +0000605void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000606{
607 cpu_abort(env, "v7m_mrs %d\n", reg);
608}
609
pbrook8984bd22008-03-31 03:47:48 +0000610uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +0000611{
612 cpu_abort(env, "v7m_mrs %d\n", reg);
613 return 0;
614}
615
bellardb5ff1b32005-11-26 10:38:39 +0000616void switch_mode(CPUState *env, int mode)
617{
618 if (mode != ARM_CPU_MODE_USR)
619 cpu_abort(env, "Tried to switch out of user mode\n");
620}
621
pbrookb0109802008-03-31 03:47:03 +0000622void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +0000623{
624 cpu_abort(env, "banked r13 write\n");
625}
626
pbrookb0109802008-03-31 03:47:03 +0000627uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +0000628{
629 cpu_abort(env, "banked r13 read\n");
630 return 0;
631}
632
bellardb5ff1b32005-11-26 10:38:39 +0000633#else
634
pbrook8e716212007-01-20 17:12:09 +0000635extern int semihosting_enabled;
636
bellardb5ff1b32005-11-26 10:38:39 +0000637/* Map CPU modes onto saved register banks. */
638static inline int bank_number (int mode)
639{
640 switch (mode) {
641 case ARM_CPU_MODE_USR:
642 case ARM_CPU_MODE_SYS:
643 return 0;
644 case ARM_CPU_MODE_SVC:
645 return 1;
646 case ARM_CPU_MODE_ABT:
647 return 2;
648 case ARM_CPU_MODE_UND:
649 return 3;
650 case ARM_CPU_MODE_IRQ:
651 return 4;
652 case ARM_CPU_MODE_FIQ:
653 return 5;
654 }
655 cpu_abort(cpu_single_env, "Bad mode %x\n", mode);
656 return -1;
657}
658
659void switch_mode(CPUState *env, int mode)
660{
661 int old_mode;
662 int i;
663
664 old_mode = env->uncached_cpsr & CPSR_M;
665 if (mode == old_mode)
666 return;
667
668 if (old_mode == ARM_CPU_MODE_FIQ) {
669 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000670 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000671 } else if (mode == ARM_CPU_MODE_FIQ) {
672 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +0000673 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +0000674 }
675
676 i = bank_number(old_mode);
677 env->banked_r13[i] = env->regs[13];
678 env->banked_r14[i] = env->regs[14];
679 env->banked_spsr[i] = env->spsr;
680
681 i = bank_number(mode);
682 env->regs[13] = env->banked_r13[i];
683 env->regs[14] = env->banked_r14[i];
684 env->spsr = env->banked_spsr[i];
685}
686
pbrook9ee6e8b2007-11-11 00:04:49 +0000687static void v7m_push(CPUARMState *env, uint32_t val)
688{
689 env->regs[13] -= 4;
690 stl_phys(env->regs[13], val);
691}
692
693static uint32_t v7m_pop(CPUARMState *env)
694{
695 uint32_t val;
696 val = ldl_phys(env->regs[13]);
697 env->regs[13] += 4;
698 return val;
699}
700
701/* Switch to V7M main or process stack pointer. */
702static void switch_v7m_sp(CPUARMState *env, int process)
703{
704 uint32_t tmp;
705 if (env->v7m.current_sp != process) {
706 tmp = env->v7m.other_sp;
707 env->v7m.other_sp = env->regs[13];
708 env->regs[13] = tmp;
709 env->v7m.current_sp = process;
710 }
711}
712
713static void do_v7m_exception_exit(CPUARMState *env)
714{
715 uint32_t type;
716 uint32_t xpsr;
717
718 type = env->regs[15];
719 if (env->v7m.exception != 0)
Paul Brook983fe822010-04-05 19:34:51 +0100720 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
pbrook9ee6e8b2007-11-11 00:04:49 +0000721
722 /* Switch to the target stack. */
723 switch_v7m_sp(env, (type & 4) != 0);
724 /* Pop registers. */
725 env->regs[0] = v7m_pop(env);
726 env->regs[1] = v7m_pop(env);
727 env->regs[2] = v7m_pop(env);
728 env->regs[3] = v7m_pop(env);
729 env->regs[12] = v7m_pop(env);
730 env->regs[14] = v7m_pop(env);
731 env->regs[15] = v7m_pop(env);
732 xpsr = v7m_pop(env);
733 xpsr_write(env, xpsr, 0xfffffdff);
734 /* Undo stack alignment. */
735 if (xpsr & 0x200)
736 env->regs[13] |= 4;
737 /* ??? The exception return type specifies Thread/Handler mode. However
738 this is also implied by the xPSR value. Not sure what to do
739 if there is a mismatch. */
740 /* ??? Likewise for mismatches between the CONTROL register and the stack
741 pointer. */
742}
743
aurel322b3ea312009-03-07 21:48:00 +0000744static void do_interrupt_v7m(CPUARMState *env)
pbrook9ee6e8b2007-11-11 00:04:49 +0000745{
746 uint32_t xpsr = xpsr_read(env);
747 uint32_t lr;
748 uint32_t addr;
749
750 lr = 0xfffffff1;
751 if (env->v7m.current_sp)
752 lr |= 4;
753 if (env->v7m.exception == 0)
754 lr |= 8;
755
756 /* For exceptions we just mark as pending on the NVIC, and let that
757 handle it. */
758 /* TODO: Need to escalate if the current priority is higher than the
759 one we're raising. */
760 switch (env->exception_index) {
761 case EXCP_UDEF:
Paul Brook983fe822010-04-05 19:34:51 +0100762 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
pbrook9ee6e8b2007-11-11 00:04:49 +0000763 return;
764 case EXCP_SWI:
765 env->regs[15] += 2;
Paul Brook983fe822010-04-05 19:34:51 +0100766 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
pbrook9ee6e8b2007-11-11 00:04:49 +0000767 return;
768 case EXCP_PREFETCH_ABORT:
769 case EXCP_DATA_ABORT:
Paul Brook983fe822010-04-05 19:34:51 +0100770 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
pbrook9ee6e8b2007-11-11 00:04:49 +0000771 return;
772 case EXCP_BKPT:
pbrook2ad207d2007-11-24 23:22:11 +0000773 if (semihosting_enabled) {
774 int nr;
775 nr = lduw_code(env->regs[15]) & 0xff;
776 if (nr == 0xab) {
777 env->regs[15] += 2;
778 env->regs[0] = do_arm_semihosting(env);
779 return;
780 }
781 }
Paul Brook983fe822010-04-05 19:34:51 +0100782 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
pbrook9ee6e8b2007-11-11 00:04:49 +0000783 return;
784 case EXCP_IRQ:
Paul Brook983fe822010-04-05 19:34:51 +0100785 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
pbrook9ee6e8b2007-11-11 00:04:49 +0000786 break;
787 case EXCP_EXCEPTION_EXIT:
788 do_v7m_exception_exit(env);
789 return;
790 default:
791 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
792 return; /* Never happens. Keep compiler happy. */
793 }
794
795 /* Align stack pointer. */
796 /* ??? Should only do this if Configuration Control Register
797 STACKALIGN bit is set. */
798 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +0000799 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +0000800 xpsr |= 0x200;
801 }
balrog6c956762008-04-13 00:57:49 +0000802 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000803 v7m_push(env, xpsr);
804 v7m_push(env, env->regs[15]);
805 v7m_push(env, env->regs[14]);
806 v7m_push(env, env->regs[12]);
807 v7m_push(env, env->regs[3]);
808 v7m_push(env, env->regs[2]);
809 v7m_push(env, env->regs[1]);
810 v7m_push(env, env->regs[0]);
811 switch_v7m_sp(env, 0);
812 env->uncached_cpsr &= ~CPSR_IT;
813 env->regs[14] = lr;
814 addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4);
815 env->regs[15] = addr & 0xfffffffe;
816 env->thumb = addr & 1;
817}
818
bellardb5ff1b32005-11-26 10:38:39 +0000819/* Handle a CPU exception. */
820void do_interrupt(CPUARMState *env)
821{
822 uint32_t addr;
823 uint32_t mask;
824 int new_mode;
825 uint32_t offset;
826
pbrook9ee6e8b2007-11-11 00:04:49 +0000827 if (IS_M(env)) {
828 do_interrupt_v7m(env);
829 return;
830 }
bellardb5ff1b32005-11-26 10:38:39 +0000831 /* TODO: Vectored interrupt controller. */
832 switch (env->exception_index) {
833 case EXCP_UDEF:
834 new_mode = ARM_CPU_MODE_UND;
835 addr = 0x04;
836 mask = CPSR_I;
837 if (env->thumb)
838 offset = 2;
839 else
840 offset = 4;
841 break;
842 case EXCP_SWI:
pbrook8e716212007-01-20 17:12:09 +0000843 if (semihosting_enabled) {
844 /* Check for semihosting interrupt. */
845 if (env->thumb) {
846 mask = lduw_code(env->regs[15] - 2) & 0xff;
847 } else {
848 mask = ldl_code(env->regs[15] - 4) & 0xffffff;
849 }
850 /* Only intercept calls from privileged modes, to provide some
851 semblance of security. */
852 if (((mask == 0x123456 && !env->thumb)
853 || (mask == 0xab && env->thumb))
854 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
855 env->regs[0] = do_arm_semihosting(env);
856 return;
857 }
858 }
bellardb5ff1b32005-11-26 10:38:39 +0000859 new_mode = ARM_CPU_MODE_SVC;
860 addr = 0x08;
861 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +0000862 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +0000863 offset = 0;
864 break;
pbrook06c949e2006-02-04 19:35:26 +0000865 case EXCP_BKPT:
pbrook9ee6e8b2007-11-11 00:04:49 +0000866 /* See if this is a semihosting syscall. */
pbrook2ad207d2007-11-24 23:22:11 +0000867 if (env->thumb && semihosting_enabled) {
pbrook9ee6e8b2007-11-11 00:04:49 +0000868 mask = lduw_code(env->regs[15]) & 0xff;
869 if (mask == 0xab
870 && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) {
871 env->regs[15] += 2;
872 env->regs[0] = do_arm_semihosting(env);
873 return;
874 }
875 }
Alex Zuepke81c05da2011-06-03 18:42:17 +0200876 env->cp15.c5_insn = 2;
pbrook9ee6e8b2007-11-11 00:04:49 +0000877 /* Fall through to prefetch abort. */
878 case EXCP_PREFETCH_ABORT:
bellardb5ff1b32005-11-26 10:38:39 +0000879 new_mode = ARM_CPU_MODE_ABT;
880 addr = 0x0c;
881 mask = CPSR_A | CPSR_I;
882 offset = 4;
883 break;
884 case EXCP_DATA_ABORT:
885 new_mode = ARM_CPU_MODE_ABT;
886 addr = 0x10;
887 mask = CPSR_A | CPSR_I;
888 offset = 8;
889 break;
890 case EXCP_IRQ:
891 new_mode = ARM_CPU_MODE_IRQ;
892 addr = 0x18;
893 /* Disable IRQ and imprecise data aborts. */
894 mask = CPSR_A | CPSR_I;
895 offset = 4;
896 break;
897 case EXCP_FIQ:
898 new_mode = ARM_CPU_MODE_FIQ;
899 addr = 0x1c;
900 /* Disable FIQ, IRQ and imprecise data aborts. */
901 mask = CPSR_A | CPSR_I | CPSR_F;
902 offset = 4;
903 break;
904 default:
905 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
906 return; /* Never happens. Keep compiler happy. */
907 }
908 /* High vectors. */
909 if (env->cp15.c1_sys & (1 << 13)) {
910 addr += 0xffff0000;
911 }
912 switch_mode (env, new_mode);
913 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +0000914 /* Clear IT bits. */
915 env->condexec_bits = 0;
Rabin Vincent30a8cac2010-02-15 00:02:36 +0530916 /* Switch to the new mode, and to the correct instruction set. */
bellard6d7e6322005-12-18 16:54:08 +0000917 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
bellardb5ff1b32005-11-26 10:38:39 +0000918 env->uncached_cpsr |= mask;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +0400919 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
920 * and we should just guard the thumb mode on V4 */
921 if (arm_feature(env, ARM_FEATURE_V4T)) {
922 env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0;
923 }
bellardb5ff1b32005-11-26 10:38:39 +0000924 env->regs[14] = env->regs[15] + offset;
925 env->regs[15] = addr;
926 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
927}
928
929/* Check section/page access permissions.
930 Returns the page protection flags, or zero if the access is not
931 permitted. */
932static inline int check_ap(CPUState *env, int ap, int domain, int access_type,
933 int is_user)
934{
pbrook9ee6e8b2007-11-11 00:04:49 +0000935 int prot_ro;
936
bellardb5ff1b32005-11-26 10:38:39 +0000937 if (domain == 3)
938 return PAGE_READ | PAGE_WRITE;
939
pbrook9ee6e8b2007-11-11 00:04:49 +0000940 if (access_type == 1)
941 prot_ro = 0;
942 else
943 prot_ro = PAGE_READ;
944
bellardb5ff1b32005-11-26 10:38:39 +0000945 switch (ap) {
946 case 0:
pbrook78600322006-09-09 14:36:26 +0000947 if (access_type == 1)
bellardb5ff1b32005-11-26 10:38:39 +0000948 return 0;
949 switch ((env->cp15.c1_sys >> 8) & 3) {
950 case 1:
951 return is_user ? 0 : PAGE_READ;
952 case 2:
953 return PAGE_READ;
954 default:
955 return 0;
956 }
957 case 1:
958 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
959 case 2:
960 if (is_user)
pbrook9ee6e8b2007-11-11 00:04:49 +0000961 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +0000962 else
963 return PAGE_READ | PAGE_WRITE;
964 case 3:
965 return PAGE_READ | PAGE_WRITE;
pbrookd4934d12008-12-19 12:39:00 +0000966 case 4: /* Reserved. */
pbrook9ee6e8b2007-11-11 00:04:49 +0000967 return 0;
968 case 5:
969 return is_user ? 0 : prot_ro;
970 case 6:
971 return prot_ro;
pbrookd4934d12008-12-19 12:39:00 +0000972 case 7:
Jamie Iles0ab06d82011-06-23 01:12:59 +0000973 if (!arm_feature (env, ARM_FEATURE_V6K))
pbrookd4934d12008-12-19 12:39:00 +0000974 return 0;
975 return prot_ro;
bellardb5ff1b32005-11-26 10:38:39 +0000976 default:
977 abort();
978 }
979}
980
pbrookb2fa1792008-10-22 19:22:30 +0000981static uint32_t get_level1_table_address(CPUState *env, uint32_t address)
982{
983 uint32_t table;
984
985 if (address & env->cp15.c2_mask)
986 table = env->cp15.c2_base1 & 0xffffc000;
987 else
988 table = env->cp15.c2_base0 & env->cp15.c2_base_mask;
989
990 table |= (address >> 18) & 0x3ffc;
991 return table;
992}
993
pbrook9ee6e8b2007-11-11 00:04:49 +0000994static int get_phys_addr_v5(CPUState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +0000995 int is_user, uint32_t *phys_ptr, int *prot,
996 target_ulong *page_size)
bellardb5ff1b32005-11-26 10:38:39 +0000997{
998 int code;
999 uint32_t table;
1000 uint32_t desc;
1001 int type;
1002 int ap;
1003 int domain;
1004 uint32_t phys_addr;
1005
pbrook9ee6e8b2007-11-11 00:04:49 +00001006 /* Pagetable walk. */
1007 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001008 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001009 desc = ldl_phys(table);
1010 type = (desc & 3);
1011 domain = (env->cp15.c3 >> ((desc >> 4) & 0x1e)) & 3;
1012 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001013 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001014 code = 5;
1015 goto do_fault;
1016 }
1017 if (domain == 0 || domain == 2) {
1018 if (type == 2)
1019 code = 9; /* Section domain fault. */
1020 else
1021 code = 11; /* Page domain fault. */
1022 goto do_fault;
1023 }
1024 if (type == 2) {
1025 /* 1Mb section. */
1026 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
1027 ap = (desc >> 10) & 3;
1028 code = 13;
Paul Brookd4c430a2010-03-17 02:14:28 +00001029 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +00001030 } else {
1031 /* Lookup l2 entry. */
1032 if (type == 1) {
1033 /* Coarse pagetable. */
1034 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1035 } else {
1036 /* Fine pagetable. */
1037 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
1038 }
1039 desc = ldl_phys(table);
1040 switch (desc & 3) {
1041 case 0: /* Page translation fault. */
1042 code = 7;
1043 goto do_fault;
1044 case 1: /* 64k page. */
1045 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1046 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001047 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001048 break;
1049 case 2: /* 4k page. */
1050 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1051 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001052 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001053 break;
1054 case 3: /* 1k page. */
1055 if (type == 1) {
1056 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1057 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1058 } else {
1059 /* Page translation fault. */
1060 code = 7;
1061 goto do_fault;
1062 }
1063 } else {
1064 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
1065 }
1066 ap = (desc >> 4) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00001067 *page_size = 0x400;
pbrook9ee6e8b2007-11-11 00:04:49 +00001068 break;
1069 default:
1070 /* Never happens, but compiler isn't smart enough to tell. */
1071 abort();
1072 }
1073 code = 15;
1074 }
1075 *prot = check_ap(env, ap, domain, access_type, is_user);
1076 if (!*prot) {
1077 /* Access permission fault. */
1078 goto do_fault;
1079 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301080 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001081 *phys_ptr = phys_addr;
1082 return 0;
1083do_fault:
1084 return code | (domain << 4);
1085}
1086
1087static int get_phys_addr_v6(CPUState *env, uint32_t address, int access_type,
Paul Brookd4c430a2010-03-17 02:14:28 +00001088 int is_user, uint32_t *phys_ptr, int *prot,
1089 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001090{
1091 int code;
1092 uint32_t table;
1093 uint32_t desc;
1094 uint32_t xn;
1095 int type;
1096 int ap;
1097 int domain;
1098 uint32_t phys_addr;
1099
1100 /* Pagetable walk. */
1101 /* Lookup l1 descriptor. */
pbrookb2fa1792008-10-22 19:22:30 +00001102 table = get_level1_table_address(env, address);
pbrook9ee6e8b2007-11-11 00:04:49 +00001103 desc = ldl_phys(table);
1104 type = (desc & 3);
1105 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00001106 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001107 code = 5;
1108 domain = 0;
1109 goto do_fault;
1110 } else if (type == 2 && (desc & (1 << 18))) {
1111 /* Supersection. */
1112 domain = 0;
1113 } else {
1114 /* Section or page. */
1115 domain = (desc >> 4) & 0x1e;
1116 }
1117 domain = (env->cp15.c3 >> domain) & 3;
1118 if (domain == 0 || domain == 2) {
1119 if (type == 2)
1120 code = 9; /* Section domain fault. */
1121 else
1122 code = 11; /* Page domain fault. */
1123 goto do_fault;
1124 }
1125 if (type == 2) {
1126 if (desc & (1 << 18)) {
1127 /* Supersection. */
1128 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001129 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001130 } else {
1131 /* Section. */
1132 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00001133 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001134 }
1135 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
1136 xn = desc & (1 << 4);
1137 code = 13;
1138 } else {
1139 /* Lookup l2 entry. */
1140 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
1141 desc = ldl_phys(table);
1142 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
1143 switch (desc & 3) {
1144 case 0: /* Page translation fault. */
1145 code = 7;
1146 goto do_fault;
1147 case 1: /* 64k page. */
1148 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
1149 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +00001150 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001151 break;
1152 case 2: case 3: /* 4k page. */
1153 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
1154 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +00001155 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00001156 break;
1157 default:
1158 /* Never happens, but compiler isn't smart enough to tell. */
1159 abort();
1160 }
1161 code = 15;
1162 }
Juha Riihimäkic0034322010-12-08 13:15:16 +02001163 if (domain == 3) {
1164 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1165 } else {
1166 if (xn && access_type == 2)
1167 goto do_fault;
pbrook9ee6e8b2007-11-11 00:04:49 +00001168
Juha Riihimäkic0034322010-12-08 13:15:16 +02001169 /* The simplified model uses AP[0] as an access control bit. */
1170 if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) {
1171 /* Access flag fault. */
1172 code = (code == 15) ? 6 : 3;
1173 goto do_fault;
1174 }
1175 *prot = check_ap(env, ap, domain, access_type, is_user);
1176 if (!*prot) {
1177 /* Access permission fault. */
1178 goto do_fault;
1179 }
1180 if (!xn) {
1181 *prot |= PAGE_EXEC;
1182 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301183 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001184 *phys_ptr = phys_addr;
1185 return 0;
1186do_fault:
1187 return code | (domain << 4);
1188}
1189
1190static int get_phys_addr_mpu(CPUState *env, uint32_t address, int access_type,
1191 int is_user, uint32_t *phys_ptr, int *prot)
1192{
1193 int n;
1194 uint32_t mask;
1195 uint32_t base;
1196
1197 *phys_ptr = address;
1198 for (n = 7; n >= 0; n--) {
1199 base = env->cp15.c6_region[n];
1200 if ((base & 1) == 0)
1201 continue;
1202 mask = 1 << ((base >> 1) & 0x1f);
1203 /* Keep this shift separate from the above to avoid an
1204 (undefined) << 32. */
1205 mask = (mask << 1) - 1;
1206 if (((base ^ address) & ~mask) == 0)
1207 break;
1208 }
1209 if (n < 0)
1210 return 2;
1211
1212 if (access_type == 2) {
1213 mask = env->cp15.c5_insn;
1214 } else {
1215 mask = env->cp15.c5_data;
1216 }
1217 mask = (mask >> (n * 4)) & 0xf;
1218 switch (mask) {
1219 case 0:
1220 return 1;
1221 case 1:
1222 if (is_user)
1223 return 1;
1224 *prot = PAGE_READ | PAGE_WRITE;
1225 break;
1226 case 2:
1227 *prot = PAGE_READ;
1228 if (!is_user)
1229 *prot |= PAGE_WRITE;
1230 break;
1231 case 3:
1232 *prot = PAGE_READ | PAGE_WRITE;
1233 break;
1234 case 5:
1235 if (is_user)
1236 return 1;
1237 *prot = PAGE_READ;
1238 break;
1239 case 6:
1240 *prot = PAGE_READ;
1241 break;
1242 default:
1243 /* Bad permission. */
1244 return 1;
1245 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301246 *prot |= PAGE_EXEC;
pbrook9ee6e8b2007-11-11 00:04:49 +00001247 return 0;
1248}
1249
1250static inline int get_phys_addr(CPUState *env, uint32_t address,
1251 int access_type, int is_user,
Paul Brookd4c430a2010-03-17 02:14:28 +00001252 uint32_t *phys_ptr, int *prot,
1253 target_ulong *page_size)
pbrook9ee6e8b2007-11-11 00:04:49 +00001254{
bellardb5ff1b32005-11-26 10:38:39 +00001255 /* Fast Context Switch Extension. */
1256 if (address < 0x02000000)
1257 address += env->cp15.c13_fcse;
1258
1259 if ((env->cp15.c1_sys & 1) == 0) {
pbrookce819862007-05-08 02:30:40 +00001260 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00001261 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301262 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +00001263 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001264 return 0;
pbrookce819862007-05-08 02:30:40 +00001265 } else if (arm_feature(env, ARM_FEATURE_MPU)) {
Paul Brookd4c430a2010-03-17 02:14:28 +00001266 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00001267 return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr,
1268 prot);
1269 } else if (env->cp15.c1_sys & (1 << 23)) {
1270 return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001271 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001272 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001273 return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr,
Paul Brookd4c430a2010-03-17 02:14:28 +00001274 prot, page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001275 }
bellardb5ff1b32005-11-26 10:38:39 +00001276}
1277
1278int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
j_mayer6ebbf392007-10-14 07:07:08 +00001279 int access_type, int mmu_idx, int is_softmmu)
bellardb5ff1b32005-11-26 10:38:39 +00001280{
1281 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001282 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001283 int prot;
j_mayer6ebbf392007-10-14 07:07:08 +00001284 int ret, is_user;
bellardb5ff1b32005-11-26 10:38:39 +00001285
j_mayer6ebbf392007-10-14 07:07:08 +00001286 is_user = mmu_idx == MMU_USER_IDX;
Paul Brookd4c430a2010-03-17 02:14:28 +00001287 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot,
1288 &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001289 if (ret == 0) {
1290 /* Map a single [sub]page. */
1291 phys_addr &= ~(uint32_t)0x3ff;
1292 address &= ~(uint32_t)0x3ff;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05301293 tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size);
Paul Brookd4c430a2010-03-17 02:14:28 +00001294 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00001295 }
1296
1297 if (access_type == 2) {
1298 env->cp15.c5_insn = ret;
1299 env->cp15.c6_insn = address;
1300 env->exception_index = EXCP_PREFETCH_ABORT;
1301 } else {
1302 env->cp15.c5_data = ret;
pbrook9ee6e8b2007-11-11 00:04:49 +00001303 if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6))
1304 env->cp15.c5_data |= (1 << 11);
bellardb5ff1b32005-11-26 10:38:39 +00001305 env->cp15.c6_data = address;
1306 env->exception_index = EXCP_DATA_ABORT;
1307 }
1308 return 1;
1309}
1310
Anthony Liguoric227f092009-10-01 16:12:16 -05001311target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
bellardb5ff1b32005-11-26 10:38:39 +00001312{
1313 uint32_t phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00001314 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00001315 int prot;
1316 int ret;
1317
Paul Brookd4c430a2010-03-17 02:14:28 +00001318 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot, &page_size);
bellardb5ff1b32005-11-26 10:38:39 +00001319
1320 if (ret != 0)
1321 return -1;
1322
1323 return phys_addr;
1324}
1325
pbrook8984bd22008-03-31 03:47:48 +00001326void HELPER(set_cp)(CPUState *env, uint32_t insn, uint32_t val)
balrogc1713132007-04-30 01:26:42 +00001327{
1328 int cp_num = (insn >> 8) & 0xf;
1329 int cp_info = (insn >> 5) & 7;
1330 int src = (insn >> 16) & 0xf;
1331 int operand = insn & 0xf;
1332
1333 if (env->cp[cp_num].cp_write)
1334 env->cp[cp_num].cp_write(env->cp[cp_num].opaque,
1335 cp_info, src, operand, val);
1336}
1337
pbrook8984bd22008-03-31 03:47:48 +00001338uint32_t HELPER(get_cp)(CPUState *env, uint32_t insn)
balrogc1713132007-04-30 01:26:42 +00001339{
1340 int cp_num = (insn >> 8) & 0xf;
1341 int cp_info = (insn >> 5) & 7;
1342 int dest = (insn >> 16) & 0xf;
1343 int operand = insn & 0xf;
1344
1345 if (env->cp[cp_num].cp_read)
1346 return env->cp[cp_num].cp_read(env->cp[cp_num].opaque,
1347 cp_info, dest, operand);
1348 return 0;
1349}
1350
pbrookce819862007-05-08 02:30:40 +00001351/* Return basic MPU access permission bits. */
1352static uint32_t simple_mpu_ap_bits(uint32_t val)
1353{
1354 uint32_t ret;
1355 uint32_t mask;
1356 int i;
1357 ret = 0;
1358 mask = 3;
1359 for (i = 0; i < 16; i += 2) {
1360 ret |= (val >> i) & mask;
1361 mask <<= 2;
1362 }
1363 return ret;
1364}
1365
1366/* Pad basic MPU access permission bits to extended format. */
1367static uint32_t extended_mpu_ap_bits(uint32_t val)
1368{
1369 uint32_t ret;
1370 uint32_t mask;
1371 int i;
1372 ret = 0;
1373 mask = 3;
1374 for (i = 0; i < 16; i += 2) {
1375 ret |= (val & mask) << i;
1376 mask <<= 2;
1377 }
1378 return ret;
1379}
1380
pbrook8984bd22008-03-31 03:47:48 +00001381void HELPER(set_cp15)(CPUState *env, uint32_t insn, uint32_t val)
bellardb5ff1b32005-11-26 10:38:39 +00001382{
pbrook9ee6e8b2007-11-11 00:04:49 +00001383 int op1;
1384 int op2;
1385 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001386
pbrook9ee6e8b2007-11-11 00:04:49 +00001387 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001388 op2 = (insn >> 5) & 7;
pbrookce819862007-05-08 02:30:40 +00001389 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001390 switch ((insn >> 16) & 0xf) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001391 case 0:
pbrook9ee6e8b2007-11-11 00:04:49 +00001392 /* ID codes. */
balrog610c3c82007-06-24 12:09:48 +00001393 if (arm_feature(env, ARM_FEATURE_XSCALE))
1394 break;
balrogc3d26892007-07-29 17:57:26 +00001395 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1396 break;
pbrooka49ea272008-12-19 13:37:53 +00001397 if (arm_feature(env, ARM_FEATURE_V7)
1398 && op1 == 2 && crm == 0 && op2 == 0) {
1399 env->cp15.c0_cssel = val & 0xf;
1400 break;
1401 }
bellardb5ff1b32005-11-26 10:38:39 +00001402 goto bad_reg;
1403 case 1: /* System configuration. */
balrogc3d26892007-07-29 17:57:26 +00001404 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1405 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001406 switch (op2) {
1407 case 0:
pbrookce819862007-05-08 02:30:40 +00001408 if (!arm_feature(env, ARM_FEATURE_XSCALE) || crm == 0)
balrogc1713132007-04-30 01:26:42 +00001409 env->cp15.c1_sys = val;
bellardb5ff1b32005-11-26 10:38:39 +00001410 /* ??? Lots of these bits are not implemented. */
1411 /* This may enable/disable the MMU, so do a TLB flush. */
1412 tlb_flush(env, 1);
1413 break;
Stefan Weil61cc8702011-04-13 22:45:22 +02001414 case 1: /* Auxiliary control register. */
balrog610c3c82007-06-24 12:09:48 +00001415 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
1416 env->cp15.c1_xscaleauxcr = val;
balrogc1713132007-04-30 01:26:42 +00001417 break;
balrog610c3c82007-06-24 12:09:48 +00001418 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001419 /* Not implemented. */
1420 break;
bellardb5ff1b32005-11-26 10:38:39 +00001421 case 2:
balrog610c3c82007-06-24 12:09:48 +00001422 if (arm_feature(env, ARM_FEATURE_XSCALE))
1423 goto bad_reg;
pbrook4be27db2008-10-22 16:14:08 +00001424 if (env->cp15.c1_coproc != val) {
1425 env->cp15.c1_coproc = val;
1426 /* ??? Is this safe when called from within a TB? */
1427 tb_flush(env);
1428 }
balrogc1713132007-04-30 01:26:42 +00001429 break;
bellardb5ff1b32005-11-26 10:38:39 +00001430 default:
1431 goto bad_reg;
1432 }
1433 break;
pbrookce819862007-05-08 02:30:40 +00001434 case 2: /* MMU Page table control / MPU cache control. */
1435 if (arm_feature(env, ARM_FEATURE_MPU)) {
1436 switch (op2) {
1437 case 0:
1438 env->cp15.c2_data = val;
1439 break;
1440 case 1:
1441 env->cp15.c2_insn = val;
1442 break;
1443 default:
1444 goto bad_reg;
1445 }
1446 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001447 switch (op2) {
1448 case 0:
1449 env->cp15.c2_base0 = val;
1450 break;
1451 case 1:
1452 env->cp15.c2_base1 = val;
1453 break;
1454 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001455 val &= 7;
1456 env->cp15.c2_control = val;
pbrook9ee6e8b2007-11-11 00:04:49 +00001457 env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> val);
pbrookb2fa1792008-10-22 19:22:30 +00001458 env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> val);
pbrook9ee6e8b2007-11-11 00:04:49 +00001459 break;
1460 default:
1461 goto bad_reg;
1462 }
pbrookce819862007-05-08 02:30:40 +00001463 }
bellardb5ff1b32005-11-26 10:38:39 +00001464 break;
pbrookce819862007-05-08 02:30:40 +00001465 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001466 env->cp15.c3 = val;
balrog405ee3a2007-10-31 00:47:13 +00001467 tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */
bellardb5ff1b32005-11-26 10:38:39 +00001468 break;
1469 case 4: /* Reserved. */
1470 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001471 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001472 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1473 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001474 switch (op2) {
1475 case 0:
pbrookce819862007-05-08 02:30:40 +00001476 if (arm_feature(env, ARM_FEATURE_MPU))
1477 val = extended_mpu_ap_bits(val);
bellardb5ff1b32005-11-26 10:38:39 +00001478 env->cp15.c5_data = val;
1479 break;
1480 case 1:
pbrookce819862007-05-08 02:30:40 +00001481 if (arm_feature(env, ARM_FEATURE_MPU))
1482 val = extended_mpu_ap_bits(val);
1483 env->cp15.c5_insn = val;
1484 break;
1485 case 2:
1486 if (!arm_feature(env, ARM_FEATURE_MPU))
1487 goto bad_reg;
1488 env->cp15.c5_data = val;
1489 break;
1490 case 3:
1491 if (!arm_feature(env, ARM_FEATURE_MPU))
1492 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001493 env->cp15.c5_insn = val;
1494 break;
1495 default:
1496 goto bad_reg;
1497 }
1498 break;
pbrookce819862007-05-08 02:30:40 +00001499 case 6: /* MMU Fault address / MPU base/size. */
1500 if (arm_feature(env, ARM_FEATURE_MPU)) {
1501 if (crm >= 8)
1502 goto bad_reg;
1503 env->cp15.c6_region[crm] = val;
1504 } else {
balrogc3d26892007-07-29 17:57:26 +00001505 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1506 op2 = 0;
pbrookce819862007-05-08 02:30:40 +00001507 switch (op2) {
1508 case 0:
1509 env->cp15.c6_data = val;
1510 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001511 case 1: /* ??? This is WFAR on armv6 */
1512 case 2:
pbrookce819862007-05-08 02:30:40 +00001513 env->cp15.c6_insn = val;
1514 break;
1515 default:
1516 goto bad_reg;
1517 }
bellardb5ff1b32005-11-26 10:38:39 +00001518 }
1519 break;
1520 case 7: /* Cache control. */
balrogc3d26892007-07-29 17:57:26 +00001521 env->cp15.c15_i_max = 0x000;
1522 env->cp15.c15_i_min = 0xff0;
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001523 if (op1 != 0) {
1524 goto bad_reg;
1525 }
1526 /* No cache, so nothing to do except VA->PA translations. */
1527 if (arm_feature(env, ARM_FEATURE_V6K)) {
1528 switch (crm) {
1529 case 4:
1530 if (arm_feature(env, ARM_FEATURE_V7)) {
1531 env->cp15.c7_par = val & 0xfffff6ff;
1532 } else {
1533 env->cp15.c7_par = val & 0xfffff1ff;
1534 }
1535 break;
1536 case 8: {
1537 uint32_t phys_addr;
1538 target_ulong page_size;
1539 int prot;
1540 int ret, is_user = op2 & 2;
1541 int access_type = op2 & 1;
1542
1543 if (op2 & 4) {
1544 /* Other states are only available with TrustZone */
1545 goto bad_reg;
1546 }
1547 ret = get_phys_addr(env, val, access_type, is_user,
1548 &phys_addr, &prot, &page_size);
1549 if (ret == 0) {
1550 /* We do not set any attribute bits in the PAR */
1551 if (page_size == (1 << 24)
1552 && arm_feature(env, ARM_FEATURE_V7)) {
1553 env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1;
1554 } else {
1555 env->cp15.c7_par = phys_addr & 0xfffff000;
1556 }
1557 } else {
1558 env->cp15.c7_par = ((ret & (10 << 1)) >> 5) |
1559 ((ret & (12 << 1)) >> 6) |
1560 ((ret & 0xf) << 1) | 1;
1561 }
1562 break;
1563 }
1564 }
1565 }
bellardb5ff1b32005-11-26 10:38:39 +00001566 break;
1567 case 8: /* MMU TLB control. */
1568 switch (op2) {
1569 case 0: /* Invalidate all. */
1570 tlb_flush(env, 0);
1571 break;
1572 case 1: /* Invalidate single TLB entry. */
Paul Brookd4c430a2010-03-17 02:14:28 +00001573 tlb_flush_page(env, val & TARGET_PAGE_MASK);
bellardb5ff1b32005-11-26 10:38:39 +00001574 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00001575 case 2: /* Invalidate on ASID. */
1576 tlb_flush(env, val == 0);
1577 break;
1578 case 3: /* Invalidate single entry on MVA. */
1579 /* ??? This is like case 1, but ignores ASID. */
1580 tlb_flush(env, 1);
1581 break;
bellardb5ff1b32005-11-26 10:38:39 +00001582 default:
1583 goto bad_reg;
1584 }
1585 break;
pbrookce819862007-05-08 02:30:40 +00001586 case 9:
balrogc3d26892007-07-29 17:57:26 +00001587 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1588 break;
Dmitry Eremin-Solenikov5bc95aa2011-04-19 18:56:45 +04001589 if (arm_feature(env, ARM_FEATURE_STRONGARM))
1590 break; /* Ignore ReadBuffer access */
pbrookce819862007-05-08 02:30:40 +00001591 switch (crm) {
1592 case 0: /* Cache lockdown. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001593 switch (op1) {
1594 case 0: /* L1 cache. */
1595 switch (op2) {
1596 case 0:
1597 env->cp15.c9_data = val;
1598 break;
1599 case 1:
1600 env->cp15.c9_insn = val;
1601 break;
1602 default:
1603 goto bad_reg;
1604 }
1605 break;
1606 case 1: /* L2 cache. */
1607 /* Ignore writes to L2 lockdown/auxiliary registers. */
1608 break;
1609 default:
1610 goto bad_reg;
1611 }
1612 break;
pbrookce819862007-05-08 02:30:40 +00001613 case 1: /* TCM memory region registers. */
1614 /* Not implemented. */
1615 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00001616 case 12: /* Performance monitor control */
1617 /* Performance monitors are implementation defined in v7,
1618 * but with an ARM recommended set of registers, which we
1619 * follow (although we don't actually implement any counters)
1620 */
1621 if (!arm_feature(env, ARM_FEATURE_V7)) {
1622 goto bad_reg;
1623 }
1624 switch (op2) {
1625 case 0: /* performance monitor control register */
1626 /* only the DP, X, D and E bits are writable */
1627 env->cp15.c9_pmcr &= ~0x39;
1628 env->cp15.c9_pmcr |= (val & 0x39);
1629 break;
1630 case 1: /* Count enable set register */
1631 val &= (1 << 31);
1632 env->cp15.c9_pmcnten |= val;
1633 break;
1634 case 2: /* Count enable clear */
1635 val &= (1 << 31);
1636 env->cp15.c9_pmcnten &= ~val;
1637 break;
1638 case 3: /* Overflow flag status */
1639 env->cp15.c9_pmovsr &= ~val;
1640 break;
1641 case 4: /* Software increment */
1642 /* RAZ/WI since we don't implement the software-count event */
1643 break;
1644 case 5: /* Event counter selection register */
1645 /* Since we don't implement any events, writing to this register
1646 * is actually UNPREDICTABLE. So we choose to RAZ/WI.
1647 */
1648 break;
1649 default:
1650 goto bad_reg;
1651 }
1652 break;
1653 case 13: /* Performance counters */
1654 if (!arm_feature(env, ARM_FEATURE_V7)) {
1655 goto bad_reg;
1656 }
1657 switch (op2) {
1658 case 0: /* Cycle count register: not implemented, so RAZ/WI */
1659 break;
1660 case 1: /* Event type select */
1661 env->cp15.c9_pmxevtyper = val & 0xff;
1662 break;
1663 case 2: /* Event count register */
1664 /* Unimplemented (we have no events), RAZ/WI */
1665 break;
1666 default:
1667 goto bad_reg;
1668 }
1669 break;
1670 case 14: /* Performance monitor control */
1671 if (!arm_feature(env, ARM_FEATURE_V7)) {
1672 goto bad_reg;
1673 }
1674 switch (op2) {
1675 case 0: /* user enable */
1676 env->cp15.c9_pmuserenr = val & 1;
1677 /* changes access rights for cp registers, so flush tbs */
1678 tb_flush(env);
1679 break;
1680 case 1: /* interrupt enable set */
1681 /* We have no event counters so only the C bit can be changed */
1682 val &= (1 << 31);
1683 env->cp15.c9_pminten |= val;
1684 break;
1685 case 2: /* interrupt enable clear */
1686 val &= (1 << 31);
1687 env->cp15.c9_pminten &= ~val;
1688 break;
1689 }
1690 break;
bellardb5ff1b32005-11-26 10:38:39 +00001691 default:
1692 goto bad_reg;
1693 }
1694 break;
1695 case 10: /* MMU TLB lockdown. */
1696 /* ??? TLB lockdown not implemented. */
1697 break;
bellardb5ff1b32005-11-26 10:38:39 +00001698 case 12: /* Reserved. */
1699 goto bad_reg;
1700 case 13: /* Process ID. */
1701 switch (op2) {
1702 case 0:
pbrookd07edbf2006-07-21 22:39:57 +00001703 /* Unlike real hardware the qemu TLB uses virtual addresses,
1704 not modified virtual addresses, so this causes a TLB flush.
1705 */
1706 if (env->cp15.c13_fcse != val)
1707 tlb_flush(env, 1);
1708 env->cp15.c13_fcse = val;
bellardb5ff1b32005-11-26 10:38:39 +00001709 break;
1710 case 1:
pbrookd07edbf2006-07-21 22:39:57 +00001711 /* This changes the ASID, so do a TLB flush. */
pbrookce819862007-05-08 02:30:40 +00001712 if (env->cp15.c13_context != val
1713 && !arm_feature(env, ARM_FEATURE_MPU))
pbrookd07edbf2006-07-21 22:39:57 +00001714 tlb_flush(env, 0);
1715 env->cp15.c13_context = val;
bellardb5ff1b32005-11-26 10:38:39 +00001716 break;
1717 default:
1718 goto bad_reg;
1719 }
1720 break;
1721 case 14: /* Reserved. */
1722 goto bad_reg;
1723 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00001724 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
pbrookce819862007-05-08 02:30:40 +00001725 if (op2 == 0 && crm == 1) {
balrog2e232132007-08-01 02:31:54 +00001726 if (env->cp15.c15_cpar != (val & 0x3fff)) {
1727 /* Changes cp0 to cp13 behavior, so needs a TB flush. */
1728 tb_flush(env);
1729 env->cp15.c15_cpar = val & 0x3fff;
1730 }
balrogc1713132007-04-30 01:26:42 +00001731 break;
1732 }
1733 goto bad_reg;
1734 }
balrogc3d26892007-07-29 17:57:26 +00001735 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1736 switch (crm) {
1737 case 0:
1738 break;
1739 case 1: /* Set TI925T configuration. */
1740 env->cp15.c15_ticonfig = val & 0xe7;
1741 env->cp15.c0_cpuid = (val & (1 << 5)) ? /* OS_TYPE bit */
1742 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
1743 break;
1744 case 2: /* Set I_max. */
1745 env->cp15.c15_i_max = val;
1746 break;
1747 case 3: /* Set I_min. */
1748 env->cp15.c15_i_min = val;
1749 break;
1750 case 4: /* Set thread-ID. */
1751 env->cp15.c15_threadid = val & 0xffff;
1752 break;
1753 case 8: /* Wait-for-interrupt (deprecated). */
1754 cpu_interrupt(env, CPU_INTERRUPT_HALT);
1755 break;
1756 default:
1757 goto bad_reg;
1758 }
1759 }
bellardb5ff1b32005-11-26 10:38:39 +00001760 break;
1761 }
1762 return;
1763bad_reg:
1764 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001765 cpu_abort(env, "Unimplemented cp15 register write (c%d, c%d, {%d, %d})\n",
1766 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00001767}
1768
pbrook8984bd22008-03-31 03:47:48 +00001769uint32_t HELPER(get_cp15)(CPUState *env, uint32_t insn)
bellardb5ff1b32005-11-26 10:38:39 +00001770{
pbrook9ee6e8b2007-11-11 00:04:49 +00001771 int op1;
1772 int op2;
1773 int crm;
bellardb5ff1b32005-11-26 10:38:39 +00001774
pbrook9ee6e8b2007-11-11 00:04:49 +00001775 op1 = (insn >> 21) & 7;
bellardb5ff1b32005-11-26 10:38:39 +00001776 op2 = (insn >> 5) & 7;
balrogc3d26892007-07-29 17:57:26 +00001777 crm = insn & 0xf;
bellardb5ff1b32005-11-26 10:38:39 +00001778 switch ((insn >> 16) & 0xf) {
1779 case 0: /* ID codes. */
pbrook9ee6e8b2007-11-11 00:04:49 +00001780 switch (op1) {
1781 case 0:
1782 switch (crm) {
1783 case 0:
1784 switch (op2) {
1785 case 0: /* Device ID. */
1786 return env->cp15.c0_cpuid;
1787 case 1: /* Cache Type. */
1788 return env->cp15.c0_cachetype;
1789 case 2: /* TCM status. */
1790 return 0;
1791 case 3: /* TLB type register. */
1792 return 0; /* No lockable TLB entries. */
Peter Maydell607b4b02011-02-03 19:43:23 +00001793 case 5: /* MPIDR */
1794 /* The MPIDR was standardised in v7; prior to
1795 * this it was implemented only in the 11MPCore.
1796 * For all other pre-v7 cores it does not exist.
1797 */
1798 if (arm_feature(env, ARM_FEATURE_V7) ||
1799 ARM_CPUID(env) == ARM_CPUID_ARM11MPCORE) {
1800 int mpidr = env->cpu_index;
1801 /* We don't support setting cluster ID ([8..11])
1802 * so these bits always RAZ.
1803 */
1804 if (arm_feature(env, ARM_FEATURE_V7MP)) {
1805 mpidr |= (1 << 31);
1806 /* Cores which are uniprocessor (non-coherent)
1807 * but still implement the MP extensions set
1808 * bit 30. (For instance, A9UP.) However we do
1809 * not currently model any of those cores.
1810 */
1811 }
1812 return mpidr;
Paul Brook10055562009-11-19 16:45:20 +00001813 }
Peter Maydell607b4b02011-02-03 19:43:23 +00001814 /* otherwise fall through to the unimplemented-reg case */
pbrook9ee6e8b2007-11-11 00:04:49 +00001815 default:
1816 goto bad_reg;
1817 }
1818 case 1:
1819 if (!arm_feature(env, ARM_FEATURE_V6))
1820 goto bad_reg;
1821 return env->cp15.c0_c1[op2];
1822 case 2:
1823 if (!arm_feature(env, ARM_FEATURE_V6))
1824 goto bad_reg;
1825 return env->cp15.c0_c2[op2];
1826 case 3: case 4: case 5: case 6: case 7:
1827 return 0;
1828 default:
1829 goto bad_reg;
1830 }
1831 case 1:
1832 /* These registers aren't documented on arm11 cores. However
1833 Linux looks at them anyway. */
1834 if (!arm_feature(env, ARM_FEATURE_V6))
1835 goto bad_reg;
1836 if (crm != 0)
1837 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001838 if (!arm_feature(env, ARM_FEATURE_V7))
1839 return 0;
1840
1841 switch (op2) {
1842 case 0:
1843 return env->cp15.c0_ccsid[env->cp15.c0_cssel];
1844 case 1:
1845 return env->cp15.c0_clid;
1846 case 7:
1847 return 0;
1848 }
1849 goto bad_reg;
1850 case 2:
1851 if (op2 != 0 || crm != 0)
balrog610c3c82007-06-24 12:09:48 +00001852 goto bad_reg;
pbrooka49ea272008-12-19 13:37:53 +00001853 return env->cp15.c0_cssel;
pbrook9ee6e8b2007-11-11 00:04:49 +00001854 default:
1855 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001856 }
1857 case 1: /* System configuration. */
balrogc3d26892007-07-29 17:57:26 +00001858 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1859 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001860 switch (op2) {
1861 case 0: /* Control register. */
1862 return env->cp15.c1_sys;
1863 case 1: /* Auxiliary control register. */
balrogc1713132007-04-30 01:26:42 +00001864 if (arm_feature(env, ARM_FEATURE_XSCALE))
balrog610c3c82007-06-24 12:09:48 +00001865 return env->cp15.c1_xscaleauxcr;
pbrook9ee6e8b2007-11-11 00:04:49 +00001866 if (!arm_feature(env, ARM_FEATURE_AUXCR))
1867 goto bad_reg;
1868 switch (ARM_CPUID(env)) {
1869 case ARM_CPUID_ARM1026:
1870 return 1;
1871 case ARM_CPUID_ARM1136:
balrog827df9f2008-04-14 21:05:22 +00001872 case ARM_CPUID_ARM1136_R2:
Jamie Iles7807eed2011-07-20 10:32:54 +00001873 case ARM_CPUID_ARM1176:
pbrook9ee6e8b2007-11-11 00:04:49 +00001874 return 7;
1875 case ARM_CPUID_ARM11MPCORE:
1876 return 1;
1877 case ARM_CPUID_CORTEXA8:
aurel32533d1772009-03-07 22:10:28 +00001878 return 2;
Paul Brook10055562009-11-19 16:45:20 +00001879 case ARM_CPUID_CORTEXA9:
1880 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001881 default:
1882 goto bad_reg;
1883 }
bellardb5ff1b32005-11-26 10:38:39 +00001884 case 2: /* Coprocessor access register. */
balrog610c3c82007-06-24 12:09:48 +00001885 if (arm_feature(env, ARM_FEATURE_XSCALE))
1886 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001887 return env->cp15.c1_coproc;
1888 default:
1889 goto bad_reg;
1890 }
pbrookce819862007-05-08 02:30:40 +00001891 case 2: /* MMU Page table control / MPU cache control. */
1892 if (arm_feature(env, ARM_FEATURE_MPU)) {
1893 switch (op2) {
1894 case 0:
1895 return env->cp15.c2_data;
1896 break;
1897 case 1:
1898 return env->cp15.c2_insn;
1899 break;
1900 default:
1901 goto bad_reg;
1902 }
1903 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00001904 switch (op2) {
1905 case 0:
1906 return env->cp15.c2_base0;
1907 case 1:
1908 return env->cp15.c2_base1;
1909 case 2:
pbrookb2fa1792008-10-22 19:22:30 +00001910 return env->cp15.c2_control;
pbrook9ee6e8b2007-11-11 00:04:49 +00001911 default:
1912 goto bad_reg;
1913 }
1914 }
pbrookce819862007-05-08 02:30:40 +00001915 case 3: /* MMU Domain access control / MPU write buffer control. */
bellardb5ff1b32005-11-26 10:38:39 +00001916 return env->cp15.c3;
1917 case 4: /* Reserved. */
1918 goto bad_reg;
pbrookce819862007-05-08 02:30:40 +00001919 case 5: /* MMU Fault status / MPU access permission. */
balrogc3d26892007-07-29 17:57:26 +00001920 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1921 op2 = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001922 switch (op2) {
1923 case 0:
pbrookce819862007-05-08 02:30:40 +00001924 if (arm_feature(env, ARM_FEATURE_MPU))
1925 return simple_mpu_ap_bits(env->cp15.c5_data);
bellardb5ff1b32005-11-26 10:38:39 +00001926 return env->cp15.c5_data;
1927 case 1:
pbrookce819862007-05-08 02:30:40 +00001928 if (arm_feature(env, ARM_FEATURE_MPU))
1929 return simple_mpu_ap_bits(env->cp15.c5_data);
1930 return env->cp15.c5_insn;
1931 case 2:
1932 if (!arm_feature(env, ARM_FEATURE_MPU))
1933 goto bad_reg;
1934 return env->cp15.c5_data;
1935 case 3:
1936 if (!arm_feature(env, ARM_FEATURE_MPU))
1937 goto bad_reg;
bellardb5ff1b32005-11-26 10:38:39 +00001938 return env->cp15.c5_insn;
1939 default:
1940 goto bad_reg;
1941 }
pbrook9ee6e8b2007-11-11 00:04:49 +00001942 case 6: /* MMU Fault address. */
pbrookce819862007-05-08 02:30:40 +00001943 if (arm_feature(env, ARM_FEATURE_MPU)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00001944 if (crm >= 8)
pbrookce819862007-05-08 02:30:40 +00001945 goto bad_reg;
pbrook9ee6e8b2007-11-11 00:04:49 +00001946 return env->cp15.c6_region[crm];
pbrookce819862007-05-08 02:30:40 +00001947 } else {
balrogc3d26892007-07-29 17:57:26 +00001948 if (arm_feature(env, ARM_FEATURE_OMAPCP))
1949 op2 = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00001950 switch (op2) {
1951 case 0:
1952 return env->cp15.c6_data;
1953 case 1:
1954 if (arm_feature(env, ARM_FEATURE_V6)) {
1955 /* Watchpoint Fault Adrress. */
1956 return 0; /* Not implemented. */
1957 } else {
1958 /* Instruction Fault Adrress. */
1959 /* Arm9 doesn't have an IFAR, but implementing it anyway
1960 shouldn't do any harm. */
1961 return env->cp15.c6_insn;
1962 }
1963 case 2:
1964 if (arm_feature(env, ARM_FEATURE_V6)) {
1965 /* Instruction Fault Adrress. */
1966 return env->cp15.c6_insn;
1967 } else {
1968 goto bad_reg;
1969 }
1970 default:
1971 goto bad_reg;
1972 }
bellardb5ff1b32005-11-26 10:38:39 +00001973 }
1974 case 7: /* Cache control. */
Adam Lackorzynskif8bf8602011-03-05 13:51:44 +01001975 if (crm == 4 && op1 == 0 && op2 == 0) {
1976 return env->cp15.c7_par;
1977 }
pbrook6fbe23d2008-04-01 17:19:11 +00001978 /* FIXME: Should only clear Z flag if destination is r15. */
1979 env->ZF = 0;
bellardb5ff1b32005-11-26 10:38:39 +00001980 return 0;
1981 case 8: /* MMU TLB control. */
1982 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00001983 case 9:
1984 switch (crm) {
1985 case 0: /* Cache lockdown */
1986 switch (op1) {
1987 case 0: /* L1 cache. */
1988 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
1989 return 0;
1990 }
1991 switch (op2) {
1992 case 0:
1993 return env->cp15.c9_data;
1994 case 1:
1995 return env->cp15.c9_insn;
1996 default:
1997 goto bad_reg;
1998 }
1999 case 1: /* L2 cache */
2000 if (crm != 0) {
2001 goto bad_reg;
2002 }
2003 /* L2 Lockdown and Auxiliary control. */
2004 return 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002005 default:
2006 goto bad_reg;
2007 }
Peter Maydell74594c92011-03-22 12:16:16 +00002008 break;
2009 case 12: /* Performance monitor control */
2010 if (!arm_feature(env, ARM_FEATURE_V7)) {
pbrook9ee6e8b2007-11-11 00:04:49 +00002011 goto bad_reg;
Peter Maydell74594c92011-03-22 12:16:16 +00002012 }
2013 switch (op2) {
2014 case 0: /* performance monitor control register */
2015 return env->cp15.c9_pmcr;
2016 case 1: /* count enable set */
2017 case 2: /* count enable clear */
2018 return env->cp15.c9_pmcnten;
2019 case 3: /* overflow flag status */
2020 return env->cp15.c9_pmovsr;
2021 case 4: /* software increment */
2022 case 5: /* event counter selection register */
2023 return 0; /* Unimplemented, RAZ/WI */
2024 default:
2025 goto bad_reg;
2026 }
2027 case 13: /* Performance counters */
2028 if (!arm_feature(env, ARM_FEATURE_V7)) {
2029 goto bad_reg;
2030 }
2031 switch (op2) {
2032 case 1: /* Event type select */
2033 return env->cp15.c9_pmxevtyper;
2034 case 0: /* Cycle count register */
2035 case 2: /* Event count register */
2036 /* Unimplemented, so RAZ/WI */
2037 return 0;
2038 default:
2039 goto bad_reg;
2040 }
2041 case 14: /* Performance monitor control */
2042 if (!arm_feature(env, ARM_FEATURE_V7)) {
2043 goto bad_reg;
2044 }
2045 switch (op2) {
2046 case 0: /* user enable */
2047 return env->cp15.c9_pmuserenr;
2048 case 1: /* interrupt enable set */
2049 case 2: /* interrupt enable clear */
2050 return env->cp15.c9_pminten;
2051 default:
2052 goto bad_reg;
2053 }
bellardb5ff1b32005-11-26 10:38:39 +00002054 default:
2055 goto bad_reg;
2056 }
Peter Maydell74594c92011-03-22 12:16:16 +00002057 break;
bellardb5ff1b32005-11-26 10:38:39 +00002058 case 10: /* MMU TLB lockdown. */
2059 /* ??? TLB lockdown not implemented. */
2060 return 0;
2061 case 11: /* TCM DMA control. */
2062 case 12: /* Reserved. */
2063 goto bad_reg;
2064 case 13: /* Process ID. */
2065 switch (op2) {
2066 case 0:
2067 return env->cp15.c13_fcse;
2068 case 1:
2069 return env->cp15.c13_context;
2070 default:
2071 goto bad_reg;
2072 }
2073 case 14: /* Reserved. */
2074 goto bad_reg;
2075 case 15: /* Implementation specific. */
balrogc1713132007-04-30 01:26:42 +00002076 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
balrogc3d26892007-07-29 17:57:26 +00002077 if (op2 == 0 && crm == 1)
balrogc1713132007-04-30 01:26:42 +00002078 return env->cp15.c15_cpar;
2079
2080 goto bad_reg;
2081 }
balrogc3d26892007-07-29 17:57:26 +00002082 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
2083 switch (crm) {
2084 case 0:
2085 return 0;
2086 case 1: /* Read TI925T configuration. */
2087 return env->cp15.c15_ticonfig;
2088 case 2: /* Read I_max. */
2089 return env->cp15.c15_i_max;
2090 case 3: /* Read I_min. */
2091 return env->cp15.c15_i_min;
2092 case 4: /* Read thread-ID. */
2093 return env->cp15.c15_threadid;
2094 case 8: /* TI925T_status */
2095 return 0;
2096 }
balrog827df9f2008-04-14 21:05:22 +00002097 /* TODO: Peripheral port remap register:
2098 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt
2099 * controller base address at $rn & ~0xfff and map size of
2100 * 0x200 << ($rn & 0xfff), when MMU is off. */
balrogc3d26892007-07-29 17:57:26 +00002101 goto bad_reg;
2102 }
bellardb5ff1b32005-11-26 10:38:39 +00002103 return 0;
2104 }
2105bad_reg:
2106 /* ??? For debugging only. Should raise illegal instruction exception. */
pbrook9ee6e8b2007-11-11 00:04:49 +00002107 cpu_abort(env, "Unimplemented cp15 register read (c%d, c%d, {%d, %d})\n",
2108 (insn >> 16) & 0xf, crm, op1, op2);
bellardb5ff1b32005-11-26 10:38:39 +00002109 return 0;
2110}
2111
pbrookb0109802008-03-31 03:47:03 +00002112void HELPER(set_r13_banked)(CPUState *env, uint32_t mode, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002113{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002114 if ((env->uncached_cpsr & CPSR_M) == mode) {
2115 env->regs[13] = val;
2116 } else {
2117 env->banked_r13[bank_number(mode)] = val;
2118 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002119}
2120
pbrookb0109802008-03-31 03:47:03 +00002121uint32_t HELPER(get_r13_banked)(CPUState *env, uint32_t mode)
pbrook9ee6e8b2007-11-11 00:04:49 +00002122{
Peter Maydell39ea3d42011-01-14 20:39:18 +01002123 if ((env->uncached_cpsr & CPSR_M) == mode) {
2124 return env->regs[13];
2125 } else {
2126 return env->banked_r13[bank_number(mode)];
2127 }
pbrook9ee6e8b2007-11-11 00:04:49 +00002128}
2129
pbrook8984bd22008-03-31 03:47:48 +00002130uint32_t HELPER(v7m_mrs)(CPUState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00002131{
2132 switch (reg) {
2133 case 0: /* APSR */
2134 return xpsr_read(env) & 0xf8000000;
2135 case 1: /* IAPSR */
2136 return xpsr_read(env) & 0xf80001ff;
2137 case 2: /* EAPSR */
2138 return xpsr_read(env) & 0xff00fc00;
2139 case 3: /* xPSR */
2140 return xpsr_read(env) & 0xff00fdff;
2141 case 5: /* IPSR */
2142 return xpsr_read(env) & 0x000001ff;
2143 case 6: /* EPSR */
2144 return xpsr_read(env) & 0x0700fc00;
2145 case 7: /* IEPSR */
2146 return xpsr_read(env) & 0x0700edff;
2147 case 8: /* MSP */
2148 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
2149 case 9: /* PSP */
2150 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
2151 case 16: /* PRIMASK */
2152 return (env->uncached_cpsr & CPSR_I) != 0;
Sebastian Huber82845822011-05-29 02:58:41 +00002153 case 17: /* BASEPRI */
2154 case 18: /* BASEPRI_MAX */
pbrook9ee6e8b2007-11-11 00:04:49 +00002155 return env->v7m.basepri;
Sebastian Huber82845822011-05-29 02:58:41 +00002156 case 19: /* FAULTMASK */
2157 return (env->uncached_cpsr & CPSR_F) != 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00002158 case 20: /* CONTROL */
2159 return env->v7m.control;
2160 default:
2161 /* ??? For debugging only. */
2162 cpu_abort(env, "Unimplemented system register read (%d)\n", reg);
2163 return 0;
2164 }
2165}
2166
pbrook8984bd22008-03-31 03:47:48 +00002167void HELPER(v7m_msr)(CPUState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00002168{
2169 switch (reg) {
2170 case 0: /* APSR */
2171 xpsr_write(env, val, 0xf8000000);
2172 break;
2173 case 1: /* IAPSR */
2174 xpsr_write(env, val, 0xf8000000);
2175 break;
2176 case 2: /* EAPSR */
2177 xpsr_write(env, val, 0xfe00fc00);
2178 break;
2179 case 3: /* xPSR */
2180 xpsr_write(env, val, 0xfe00fc00);
2181 break;
2182 case 5: /* IPSR */
2183 /* IPSR bits are readonly. */
2184 break;
2185 case 6: /* EPSR */
2186 xpsr_write(env, val, 0x0600fc00);
2187 break;
2188 case 7: /* IEPSR */
2189 xpsr_write(env, val, 0x0600fc00);
2190 break;
2191 case 8: /* MSP */
2192 if (env->v7m.current_sp)
2193 env->v7m.other_sp = val;
2194 else
2195 env->regs[13] = val;
2196 break;
2197 case 9: /* PSP */
2198 if (env->v7m.current_sp)
2199 env->regs[13] = val;
2200 else
2201 env->v7m.other_sp = val;
2202 break;
2203 case 16: /* PRIMASK */
2204 if (val & 1)
2205 env->uncached_cpsr |= CPSR_I;
2206 else
2207 env->uncached_cpsr &= ~CPSR_I;
2208 break;
Sebastian Huber82845822011-05-29 02:58:41 +00002209 case 17: /* BASEPRI */
2210 env->v7m.basepri = val & 0xff;
2211 break;
2212 case 18: /* BASEPRI_MAX */
2213 val &= 0xff;
2214 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
2215 env->v7m.basepri = val;
2216 break;
2217 case 19: /* FAULTMASK */
pbrook9ee6e8b2007-11-11 00:04:49 +00002218 if (val & 1)
2219 env->uncached_cpsr |= CPSR_F;
2220 else
2221 env->uncached_cpsr &= ~CPSR_F;
2222 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00002223 case 20: /* CONTROL */
2224 env->v7m.control = val & 3;
2225 switch_v7m_sp(env, (val & 2) != 0);
2226 break;
2227 default:
2228 /* ??? For debugging only. */
2229 cpu_abort(env, "Unimplemented system register write (%d)\n", reg);
2230 return;
2231 }
2232}
2233
balrogc1713132007-04-30 01:26:42 +00002234void cpu_arm_set_cp_io(CPUARMState *env, int cpnum,
2235 ARMReadCPFunc *cp_read, ARMWriteCPFunc *cp_write,
2236 void *opaque)
2237{
2238 if (cpnum < 0 || cpnum > 14) {
2239 cpu_abort(env, "Bad coprocessor number: %i\n", cpnum);
2240 return;
2241 }
2242
2243 env->cp[cpnum].cp_read = cp_read;
2244 env->cp[cpnum].cp_write = cp_write;
2245 env->cp[cpnum].opaque = opaque;
2246}
2247
bellardb5ff1b32005-11-26 10:38:39 +00002248#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00002249
2250/* Note that signed overflow is undefined in C. The following routines are
2251 careful to use unsigned types where modulo arithmetic is required.
2252 Failure to do so _will_ break on newer gcc. */
2253
2254/* Signed saturating arithmetic. */
2255
aurel321654b2d2008-04-11 04:55:07 +00002256/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002257static inline uint16_t add16_sat(uint16_t a, uint16_t b)
2258{
2259 uint16_t res;
2260
2261 res = a + b;
2262 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
2263 if (a & 0x8000)
2264 res = 0x8000;
2265 else
2266 res = 0x7fff;
2267 }
2268 return res;
2269}
2270
aurel321654b2d2008-04-11 04:55:07 +00002271/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002272static inline uint8_t add8_sat(uint8_t a, uint8_t b)
2273{
2274 uint8_t res;
2275
2276 res = a + b;
2277 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
2278 if (a & 0x80)
2279 res = 0x80;
2280 else
2281 res = 0x7f;
2282 }
2283 return res;
2284}
2285
aurel321654b2d2008-04-11 04:55:07 +00002286/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002287static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
2288{
2289 uint16_t res;
2290
2291 res = a - b;
2292 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
2293 if (a & 0x8000)
2294 res = 0x8000;
2295 else
2296 res = 0x7fff;
2297 }
2298 return res;
2299}
2300
aurel321654b2d2008-04-11 04:55:07 +00002301/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00002302static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
2303{
2304 uint8_t res;
2305
2306 res = a - b;
2307 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
2308 if (a & 0x80)
2309 res = 0x80;
2310 else
2311 res = 0x7f;
2312 }
2313 return res;
2314}
2315
2316#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
2317#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
2318#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
2319#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
2320#define PFX q
2321
2322#include "op_addsub.h"
2323
2324/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00002325static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002326{
2327 uint16_t res;
2328 res = a + b;
2329 if (res < a)
2330 res = 0xffff;
2331 return res;
2332}
2333
pbrook460a09c2008-05-01 12:04:35 +00002334static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002335{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002336 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002337 return a - b;
2338 else
2339 return 0;
2340}
2341
2342static inline uint8_t add8_usat(uint8_t a, uint8_t b)
2343{
2344 uint8_t res;
2345 res = a + b;
2346 if (res < a)
2347 res = 0xff;
2348 return res;
2349}
2350
2351static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
2352{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08002353 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00002354 return a - b;
2355 else
2356 return 0;
2357}
2358
2359#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
2360#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
2361#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
2362#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
2363#define PFX uq
2364
2365#include "op_addsub.h"
2366
2367/* Signed modulo arithmetic. */
2368#define SARITH16(a, b, n, op) do { \
2369 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002370 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002371 RESULT(sum, n, 16); \
2372 if (sum >= 0) \
2373 ge |= 3 << (n * 2); \
2374 } while(0)
2375
2376#define SARITH8(a, b, n, op) do { \
2377 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00002378 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00002379 RESULT(sum, n, 8); \
2380 if (sum >= 0) \
2381 ge |= 1 << n; \
2382 } while(0)
2383
2384
2385#define ADD16(a, b, n) SARITH16(a, b, n, +)
2386#define SUB16(a, b, n) SARITH16(a, b, n, -)
2387#define ADD8(a, b, n) SARITH8(a, b, n, +)
2388#define SUB8(a, b, n) SARITH8(a, b, n, -)
2389#define PFX s
2390#define ARITH_GE
2391
2392#include "op_addsub.h"
2393
2394/* Unsigned modulo arithmetic. */
2395#define ADD16(a, b, n) do { \
2396 uint32_t sum; \
2397 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
2398 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00002399 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00002400 ge |= 3 << (n * 2); \
2401 } while(0)
2402
2403#define ADD8(a, b, n) do { \
2404 uint32_t sum; \
2405 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
2406 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00002407 if ((sum >> 8) == 1) \
2408 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002409 } while(0)
2410
2411#define SUB16(a, b, n) do { \
2412 uint32_t sum; \
2413 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
2414 RESULT(sum, n, 16); \
2415 if ((sum >> 16) == 0) \
2416 ge |= 3 << (n * 2); \
2417 } while(0)
2418
2419#define SUB8(a, b, n) do { \
2420 uint32_t sum; \
2421 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
2422 RESULT(sum, n, 8); \
2423 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00002424 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00002425 } while(0)
2426
2427#define PFX u
2428#define ARITH_GE
2429
2430#include "op_addsub.h"
2431
2432/* Halved signed arithmetic. */
2433#define ADD16(a, b, n) \
2434 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
2435#define SUB16(a, b, n) \
2436 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
2437#define ADD8(a, b, n) \
2438 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
2439#define SUB8(a, b, n) \
2440 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
2441#define PFX sh
2442
2443#include "op_addsub.h"
2444
2445/* Halved unsigned arithmetic. */
2446#define ADD16(a, b, n) \
2447 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2448#define SUB16(a, b, n) \
2449 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
2450#define ADD8(a, b, n) \
2451 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2452#define SUB8(a, b, n) \
2453 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
2454#define PFX uh
2455
2456#include "op_addsub.h"
2457
2458static inline uint8_t do_usad(uint8_t a, uint8_t b)
2459{
2460 if (a > b)
2461 return a - b;
2462 else
2463 return b - a;
2464}
2465
2466/* Unsigned sum of absolute byte differences. */
2467uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
2468{
2469 uint32_t sum;
2470 sum = do_usad(a, b);
2471 sum += do_usad(a >> 8, b >> 8);
2472 sum += do_usad(a >> 16, b >>16);
2473 sum += do_usad(a >> 24, b >> 24);
2474 return sum;
2475}
2476
2477/* For ARMv6 SEL instruction. */
2478uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
2479{
2480 uint32_t mask;
2481
2482 mask = 0;
2483 if (flags & 1)
2484 mask |= 0xff;
2485 if (flags & 2)
2486 mask |= 0xff00;
2487 if (flags & 4)
2488 mask |= 0xff0000;
2489 if (flags & 8)
2490 mask |= 0xff000000;
2491 return (a & mask) | (b & ~mask);
2492}
2493
pbrook5e3f8782008-03-31 03:47:34 +00002494uint32_t HELPER(logicq_cc)(uint64_t val)
2495{
2496 return (val >> 32) | (val != 0);
2497}
pbrook4373f3c2008-03-31 03:47:19 +00002498
2499/* VFP support. We follow the convention used for VFP instrunctions:
2500 Single precition routines have a "s" suffix, double precision a
2501 "d" suffix. */
2502
2503/* Convert host exception flags to vfp form. */
2504static inline int vfp_exceptbits_from_host(int host_bits)
2505{
2506 int target_bits = 0;
2507
2508 if (host_bits & float_flag_invalid)
2509 target_bits |= 1;
2510 if (host_bits & float_flag_divbyzero)
2511 target_bits |= 2;
2512 if (host_bits & float_flag_overflow)
2513 target_bits |= 4;
Peter Maydell36802b62011-05-19 14:46:18 +01002514 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
pbrook4373f3c2008-03-31 03:47:19 +00002515 target_bits |= 8;
2516 if (host_bits & float_flag_inexact)
2517 target_bits |= 0x10;
Peter Maydellcecd8502011-01-06 19:37:55 +00002518 if (host_bits & float_flag_input_denormal)
2519 target_bits |= 0x80;
pbrook4373f3c2008-03-31 03:47:19 +00002520 return target_bits;
2521}
2522
2523uint32_t HELPER(vfp_get_fpscr)(CPUState *env)
2524{
2525 int i;
2526 uint32_t fpscr;
2527
2528 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
2529 | (env->vfp.vec_len << 16)
2530 | (env->vfp.vec_stride << 20);
2531 i = get_float_exception_flags(&env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002532 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002533 fpscr |= vfp_exceptbits_from_host(i);
2534 return fpscr;
2535}
2536
Peter Maydell01653292010-11-24 15:20:04 +00002537uint32_t vfp_get_fpscr(CPUState *env)
2538{
2539 return HELPER(vfp_get_fpscr)(env);
2540}
2541
pbrook4373f3c2008-03-31 03:47:19 +00002542/* Convert vfp exception flags to target form. */
2543static inline int vfp_exceptbits_to_host(int target_bits)
2544{
2545 int host_bits = 0;
2546
2547 if (target_bits & 1)
2548 host_bits |= float_flag_invalid;
2549 if (target_bits & 2)
2550 host_bits |= float_flag_divbyzero;
2551 if (target_bits & 4)
2552 host_bits |= float_flag_overflow;
2553 if (target_bits & 8)
2554 host_bits |= float_flag_underflow;
2555 if (target_bits & 0x10)
2556 host_bits |= float_flag_inexact;
Peter Maydellcecd8502011-01-06 19:37:55 +00002557 if (target_bits & 0x80)
2558 host_bits |= float_flag_input_denormal;
pbrook4373f3c2008-03-31 03:47:19 +00002559 return host_bits;
2560}
2561
2562void HELPER(vfp_set_fpscr)(CPUState *env, uint32_t val)
2563{
2564 int i;
2565 uint32_t changed;
2566
2567 changed = env->vfp.xregs[ARM_VFP_FPSCR];
2568 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
2569 env->vfp.vec_len = (val >> 16) & 7;
2570 env->vfp.vec_stride = (val >> 20) & 3;
2571
2572 changed ^= val;
2573 if (changed & (3 << 22)) {
2574 i = (val >> 22) & 3;
2575 switch (i) {
2576 case 0:
2577 i = float_round_nearest_even;
2578 break;
2579 case 1:
2580 i = float_round_up;
2581 break;
2582 case 2:
2583 i = float_round_down;
2584 break;
2585 case 3:
2586 i = float_round_to_zero;
2587 break;
2588 }
2589 set_float_rounding_mode(i, &env->vfp.fp_status);
2590 }
Peter Maydellcecd8502011-01-06 19:37:55 +00002591 if (changed & (1 << 24)) {
pbrookfe76d972008-12-19 14:33:59 +00002592 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
Peter Maydellcecd8502011-01-06 19:37:55 +00002593 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
2594 }
pbrook5c7908e2008-12-19 13:53:37 +00002595 if (changed & (1 << 25))
2596 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002597
Peter Maydellb12c3902011-01-06 19:37:54 +00002598 i = vfp_exceptbits_to_host(val);
pbrook4373f3c2008-03-31 03:47:19 +00002599 set_float_exception_flags(i, &env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01002600 set_float_exception_flags(0, &env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00002601}
2602
Peter Maydell01653292010-11-24 15:20:04 +00002603void vfp_set_fpscr(CPUState *env, uint32_t val)
2604{
2605 HELPER(vfp_set_fpscr)(env, val);
2606}
2607
pbrook4373f3c2008-03-31 03:47:19 +00002608#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
2609
2610#define VFP_BINOP(name) \
Peter Maydellae1857e2011-05-25 14:51:48 +00002611float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002612{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002613 float_status *fpst = fpstp; \
2614 return float32_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002615} \
Peter Maydellae1857e2011-05-25 14:51:48 +00002616float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002617{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00002618 float_status *fpst = fpstp; \
2619 return float64_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002620}
2621VFP_BINOP(add)
2622VFP_BINOP(sub)
2623VFP_BINOP(mul)
2624VFP_BINOP(div)
2625#undef VFP_BINOP
2626
2627float32 VFP_HELPER(neg, s)(float32 a)
2628{
2629 return float32_chs(a);
2630}
2631
2632float64 VFP_HELPER(neg, d)(float64 a)
2633{
balrog66230e02008-04-20 00:58:01 +00002634 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002635}
2636
2637float32 VFP_HELPER(abs, s)(float32 a)
2638{
2639 return float32_abs(a);
2640}
2641
2642float64 VFP_HELPER(abs, d)(float64 a)
2643{
balrog66230e02008-04-20 00:58:01 +00002644 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00002645}
2646
2647float32 VFP_HELPER(sqrt, s)(float32 a, CPUState *env)
2648{
2649 return float32_sqrt(a, &env->vfp.fp_status);
2650}
2651
2652float64 VFP_HELPER(sqrt, d)(float64 a, CPUState *env)
2653{
2654 return float64_sqrt(a, &env->vfp.fp_status);
2655}
2656
2657/* XXX: check quiet/signaling case */
2658#define DO_VFP_cmp(p, type) \
2659void VFP_HELPER(cmp, p)(type a, type b, CPUState *env) \
2660{ \
2661 uint32_t flags; \
2662 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
2663 case 0: flags = 0x6; break; \
2664 case -1: flags = 0x8; break; \
2665 case 1: flags = 0x2; break; \
2666 default: case 2: flags = 0x3; break; \
2667 } \
2668 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2669 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2670} \
2671void VFP_HELPER(cmpe, p)(type a, type b, CPUState *env) \
2672{ \
2673 uint32_t flags; \
2674 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
2675 case 0: flags = 0x6; break; \
2676 case -1: flags = 0x8; break; \
2677 case 1: flags = 0x2; break; \
2678 default: case 2: flags = 0x3; break; \
2679 } \
2680 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
2681 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
2682}
2683DO_VFP_cmp(s, float32)
2684DO_VFP_cmp(d, float64)
2685#undef DO_VFP_cmp
2686
Peter Maydell5500b062011-05-19 14:46:19 +01002687/* Integer to float and float to integer conversions */
2688
2689#define CONV_ITOF(name, fsz, sign) \
2690 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
2691{ \
2692 float_status *fpst = fpstp; \
2693 return sign##int32_to_##float##fsz(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002694}
2695
Peter Maydell5500b062011-05-19 14:46:19 +01002696#define CONV_FTOI(name, fsz, sign, round) \
2697uint32_t HELPER(name)(float##fsz x, void *fpstp) \
2698{ \
2699 float_status *fpst = fpstp; \
2700 if (float##fsz##_is_any_nan(x)) { \
2701 float_raise(float_flag_invalid, fpst); \
2702 return 0; \
2703 } \
2704 return float##fsz##_to_##sign##int32##round(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002705}
2706
Peter Maydell5500b062011-05-19 14:46:19 +01002707#define FLOAT_CONVS(name, p, fsz, sign) \
2708CONV_ITOF(vfp_##name##to##p, fsz, sign) \
2709CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
2710CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
pbrook4373f3c2008-03-31 03:47:19 +00002711
Peter Maydell5500b062011-05-19 14:46:19 +01002712FLOAT_CONVS(si, s, 32, )
2713FLOAT_CONVS(si, d, 64, )
2714FLOAT_CONVS(ui, s, 32, u)
2715FLOAT_CONVS(ui, d, 64, u)
pbrook4373f3c2008-03-31 03:47:19 +00002716
Peter Maydell5500b062011-05-19 14:46:19 +01002717#undef CONV_ITOF
2718#undef CONV_FTOI
2719#undef FLOAT_CONVS
pbrook4373f3c2008-03-31 03:47:19 +00002720
2721/* floating point conversion */
2722float64 VFP_HELPER(fcvtd, s)(float32 x, CPUState *env)
2723{
Peter Maydell2d627732010-12-07 15:37:34 +00002724 float64 r = float32_to_float64(x, &env->vfp.fp_status);
2725 /* ARM requires that S<->D conversion of any kind of NaN generates
2726 * a quiet NaN by forcing the most significant frac bit to 1.
2727 */
2728 return float64_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002729}
2730
2731float32 VFP_HELPER(fcvts, d)(float64 x, CPUState *env)
2732{
Peter Maydell2d627732010-12-07 15:37:34 +00002733 float32 r = float64_to_float32(x, &env->vfp.fp_status);
2734 /* ARM requires that S<->D conversion of any kind of NaN generates
2735 * a quiet NaN by forcing the most significant frac bit to 1.
2736 */
2737 return float32_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00002738}
2739
2740/* VFP3 fixed point conversion. */
Peter Maydell622465e2011-03-14 07:23:11 +00002741#define VFP_CONV_FIX(name, p, fsz, itype, sign) \
Peter Maydell5500b062011-05-19 14:46:19 +01002742float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \
2743 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002744{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002745 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002746 float##fsz tmp; \
Peter Maydell5500b062011-05-19 14:46:19 +01002747 tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \
2748 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002749} \
Peter Maydell5500b062011-05-19 14:46:19 +01002750uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \
2751 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00002752{ \
Peter Maydell5500b062011-05-19 14:46:19 +01002753 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00002754 float##fsz tmp; \
2755 if (float##fsz##_is_any_nan(x)) { \
Peter Maydell5500b062011-05-19 14:46:19 +01002756 float_raise(float_flag_invalid, fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00002757 return 0; \
Peter Maydell09d94872010-12-07 15:37:34 +00002758 } \
Peter Maydell5500b062011-05-19 14:46:19 +01002759 tmp = float##fsz##_scalbn(x, shift, fpst); \
2760 return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00002761}
2762
Peter Maydell622465e2011-03-14 07:23:11 +00002763VFP_CONV_FIX(sh, d, 64, int16, )
2764VFP_CONV_FIX(sl, d, 64, int32, )
2765VFP_CONV_FIX(uh, d, 64, uint16, u)
2766VFP_CONV_FIX(ul, d, 64, uint32, u)
2767VFP_CONV_FIX(sh, s, 32, int16, )
2768VFP_CONV_FIX(sl, s, 32, int32, )
2769VFP_CONV_FIX(uh, s, 32, uint16, u)
2770VFP_CONV_FIX(ul, s, 32, uint32, u)
pbrook4373f3c2008-03-31 03:47:19 +00002771#undef VFP_CONV_FIX
2772
Paul Brook60011492009-11-19 16:45:20 +00002773/* Half precision conversions. */
Peter Maydell2d981da2011-02-10 11:29:01 +00002774static float32 do_fcvt_f16_to_f32(uint32_t a, CPUState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002775{
Paul Brook60011492009-11-19 16:45:20 +00002776 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002777 float32 r = float16_to_float32(make_float16(a), ieee, s);
2778 if (ieee) {
2779 return float32_maybe_silence_nan(r);
2780 }
2781 return r;
Paul Brook60011492009-11-19 16:45:20 +00002782}
2783
Peter Maydell2d981da2011-02-10 11:29:01 +00002784static uint32_t do_fcvt_f32_to_f16(float32 a, CPUState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00002785{
Paul Brook60011492009-11-19 16:45:20 +00002786 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00002787 float16 r = float32_to_float16(a, ieee, s);
2788 if (ieee) {
2789 r = float16_maybe_silence_nan(r);
2790 }
2791 return float16_val(r);
Paul Brook60011492009-11-19 16:45:20 +00002792}
2793
Peter Maydell2d981da2011-02-10 11:29:01 +00002794float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2795{
2796 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
2797}
2798
2799uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUState *env)
2800{
2801 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
2802}
2803
2804float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUState *env)
2805{
2806 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
2807}
2808
2809uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUState *env)
2810{
2811 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
2812}
2813
Peter Maydelldda3ec42011-03-14 15:37:12 +00002814#define float32_two make_float32(0x40000000)
Peter Maydell6aae3df2011-03-14 15:37:13 +00002815#define float32_three make_float32(0x40400000)
2816#define float32_one_point_five make_float32(0x3fc00000)
Peter Maydelldda3ec42011-03-14 15:37:12 +00002817
pbrook4373f3c2008-03-31 03:47:19 +00002818float32 HELPER(recps_f32)(float32 a, float32 b, CPUState *env)
2819{
Peter Maydelldda3ec42011-03-14 15:37:12 +00002820 float_status *s = &env->vfp.standard_fp_status;
2821 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2822 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002823 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2824 float_raise(float_flag_input_denormal, s);
2825 }
Peter Maydelldda3ec42011-03-14 15:37:12 +00002826 return float32_two;
2827 }
2828 return float32_sub(float32_two, float32_mul(a, b, s), s);
pbrook4373f3c2008-03-31 03:47:19 +00002829}
2830
2831float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUState *env)
2832{
Peter Maydell71826962011-01-14 20:39:18 +01002833 float_status *s = &env->vfp.standard_fp_status;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002834 float32 product;
2835 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
2836 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002837 if (!(float32_is_zero(a) || float32_is_zero(b))) {
2838 float_raise(float_flag_input_denormal, s);
2839 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002840 return float32_one_point_five;
Peter Maydell9ea62f52011-01-14 20:39:18 +01002841 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00002842 product = float32_mul(a, b, s);
2843 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
pbrook4373f3c2008-03-31 03:47:19 +00002844}
2845
pbrook8f8e3aa2008-03-31 03:48:01 +00002846/* NEON helpers. */
2847
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01002848/* Constants 256 and 512 are used in some helpers; we avoid relying on
2849 * int->float conversions at run-time. */
2850#define float64_256 make_float64(0x4070000000000000LL)
2851#define float64_512 make_float64(0x4080000000000000LL)
2852
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002853/* The algorithm that must be used to calculate the estimate
2854 * is specified by the ARM ARM.
2855 */
2856static float64 recip_estimate(float64 a, CPUState *env)
2857{
Peter Maydell1146a812011-05-19 14:46:14 +01002858 /* These calculations mustn't set any fp exception flags,
2859 * so we use a local copy of the fp_status.
2860 */
2861 float_status dummy_status = env->vfp.standard_fp_status;
2862 float_status *s = &dummy_status;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002863 /* q = (int)(a * 512.0) */
2864 float64 q = float64_mul(float64_512, a, s);
2865 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2866
2867 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
2868 q = int64_to_float64(q_int, s);
2869 q = float64_add(q, float64_half, s);
2870 q = float64_div(q, float64_512, s);
2871 q = float64_div(float64_one, q, s);
2872
2873 /* s = (int)(256.0 * r + 0.5) */
2874 q = float64_mul(q, float64_256, s);
2875 q = float64_add(q, float64_half, s);
2876 q_int = float64_to_int64_round_to_zero(q, s);
2877
2878 /* return (double)s / 256.0 */
2879 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2880}
2881
pbrook4373f3c2008-03-31 03:47:19 +00002882float32 HELPER(recpe_f32)(float32 a, CPUState *env)
2883{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002884 float_status *s = &env->vfp.standard_fp_status;
2885 float64 f64;
2886 uint32_t val32 = float32_val(a);
2887
2888 int result_exp;
2889 int a_exp = (val32 & 0x7f800000) >> 23;
2890 int sign = val32 & 0x80000000;
2891
2892 if (float32_is_any_nan(a)) {
2893 if (float32_is_signaling_nan(a)) {
2894 float_raise(float_flag_invalid, s);
2895 }
2896 return float32_default_nan;
2897 } else if (float32_is_infinity(a)) {
2898 return float32_set_sign(float32_zero, float32_is_neg(a));
2899 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002900 if (!float32_is_zero(a)) {
2901 float_raise(float_flag_input_denormal, s);
2902 }
Christophe Lyonfe0e4872011-02-21 17:38:47 +01002903 float_raise(float_flag_divbyzero, s);
2904 return float32_set_sign(float32_infinity, float32_is_neg(a));
2905 } else if (a_exp >= 253) {
2906 float_raise(float_flag_underflow, s);
2907 return float32_set_sign(float32_zero, float32_is_neg(a));
2908 }
2909
2910 f64 = make_float64((0x3feULL << 52)
2911 | ((int64_t)(val32 & 0x7fffff) << 29));
2912
2913 result_exp = 253 - a_exp;
2914
2915 f64 = recip_estimate(f64, env);
2916
2917 val32 = sign
2918 | ((result_exp & 0xff) << 23)
2919 | ((float64_val(f64) >> 29) & 0x7fffff);
2920 return make_float32(val32);
pbrook4373f3c2008-03-31 03:47:19 +00002921}
2922
Christophe Lyone07be5d2011-02-21 17:38:48 +01002923/* The algorithm that must be used to calculate the estimate
2924 * is specified by the ARM ARM.
2925 */
2926static float64 recip_sqrt_estimate(float64 a, CPUState *env)
2927{
Peter Maydell1146a812011-05-19 14:46:14 +01002928 /* These calculations mustn't set any fp exception flags,
2929 * so we use a local copy of the fp_status.
2930 */
2931 float_status dummy_status = env->vfp.standard_fp_status;
2932 float_status *s = &dummy_status;
Christophe Lyone07be5d2011-02-21 17:38:48 +01002933 float64 q;
2934 int64_t q_int;
2935
2936 if (float64_lt(a, float64_half, s)) {
2937 /* range 0.25 <= a < 0.5 */
2938
2939 /* a in units of 1/512 rounded down */
2940 /* q0 = (int)(a * 512.0); */
2941 q = float64_mul(float64_512, a, s);
2942 q_int = float64_to_int64_round_to_zero(q, s);
2943
2944 /* reciprocal root r */
2945 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
2946 q = int64_to_float64(q_int, s);
2947 q = float64_add(q, float64_half, s);
2948 q = float64_div(q, float64_512, s);
2949 q = float64_sqrt(q, s);
2950 q = float64_div(float64_one, q, s);
2951 } else {
2952 /* range 0.5 <= a < 1.0 */
2953
2954 /* a in units of 1/256 rounded down */
2955 /* q1 = (int)(a * 256.0); */
2956 q = float64_mul(float64_256, a, s);
2957 int64_t q_int = float64_to_int64_round_to_zero(q, s);
2958
2959 /* reciprocal root r */
2960 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
2961 q = int64_to_float64(q_int, s);
2962 q = float64_add(q, float64_half, s);
2963 q = float64_div(q, float64_256, s);
2964 q = float64_sqrt(q, s);
2965 q = float64_div(float64_one, q, s);
2966 }
2967 /* r in units of 1/256 rounded to nearest */
2968 /* s = (int)(256.0 * r + 0.5); */
2969
2970 q = float64_mul(q, float64_256,s );
2971 q = float64_add(q, float64_half, s);
2972 q_int = float64_to_int64_round_to_zero(q, s);
2973
2974 /* return (double)s / 256.0;*/
2975 return float64_div(int64_to_float64(q_int, s), float64_256, s);
2976}
2977
pbrook4373f3c2008-03-31 03:47:19 +00002978float32 HELPER(rsqrte_f32)(float32 a, CPUState *env)
2979{
Christophe Lyone07be5d2011-02-21 17:38:48 +01002980 float_status *s = &env->vfp.standard_fp_status;
2981 int result_exp;
2982 float64 f64;
2983 uint32_t val;
2984 uint64_t val64;
2985
2986 val = float32_val(a);
2987
2988 if (float32_is_any_nan(a)) {
2989 if (float32_is_signaling_nan(a)) {
2990 float_raise(float_flag_invalid, s);
2991 }
2992 return float32_default_nan;
2993 } else if (float32_is_zero_or_denormal(a)) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01002994 if (!float32_is_zero(a)) {
2995 float_raise(float_flag_input_denormal, s);
2996 }
Christophe Lyone07be5d2011-02-21 17:38:48 +01002997 float_raise(float_flag_divbyzero, s);
2998 return float32_set_sign(float32_infinity, float32_is_neg(a));
2999 } else if (float32_is_neg(a)) {
3000 float_raise(float_flag_invalid, s);
3001 return float32_default_nan;
3002 } else if (float32_is_infinity(a)) {
3003 return float32_zero;
3004 }
3005
3006 /* Normalize to a double-precision value between 0.25 and 1.0,
3007 * preserving the parity of the exponent. */
3008 if ((val & 0x800000) == 0) {
3009 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3010 | (0x3feULL << 52)
3011 | ((uint64_t)(val & 0x7fffff) << 29));
3012 } else {
3013 f64 = make_float64(((uint64_t)(val & 0x80000000) << 32)
3014 | (0x3fdULL << 52)
3015 | ((uint64_t)(val & 0x7fffff) << 29));
3016 }
3017
3018 result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2;
3019
3020 f64 = recip_sqrt_estimate(f64, env);
3021
3022 val64 = float64_val(f64);
3023
3024 val = ((val64 >> 63) & 0x80000000)
3025 | ((result_exp & 0xff) << 23)
3026 | ((val64 >> 29) & 0x7fffff);
3027 return make_float32(val);
pbrook4373f3c2008-03-31 03:47:19 +00003028}
3029
3030uint32_t HELPER(recpe_u32)(uint32_t a, CPUState *env)
3031{
Christophe Lyonfe0e4872011-02-21 17:38:47 +01003032 float64 f64;
3033
3034 if ((a & 0x80000000) == 0) {
3035 return 0xffffffff;
3036 }
3037
3038 f64 = make_float64((0x3feULL << 52)
3039 | ((int64_t)(a & 0x7fffffff) << 21));
3040
3041 f64 = recip_estimate (f64, env);
3042
3043 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003044}
3045
3046uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUState *env)
3047{
Christophe Lyone07be5d2011-02-21 17:38:48 +01003048 float64 f64;
3049
3050 if ((a & 0xc0000000) == 0) {
3051 return 0xffffffff;
3052 }
3053
3054 if (a & 0x80000000) {
3055 f64 = make_float64((0x3feULL << 52)
3056 | ((uint64_t)(a & 0x7fffffff) << 21));
3057 } else { /* bits 31-30 == '01' */
3058 f64 = make_float64((0x3fdULL << 52)
3059 | ((uint64_t)(a & 0x3fffffff) << 22));
3060 }
3061
3062 f64 = recip_sqrt_estimate(f64, env);
3063
3064 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00003065}
pbrookfe1479c2008-12-19 13:18:36 +00003066
3067void HELPER(set_teecr)(CPUState *env, uint32_t val)
3068{
3069 val &= 1;
3070 if (env->teecr != val) {
3071 env->teecr = val;
3072 tb_flush(env);
3073 }
3074}