]> git.proxmox.com Git - qemu.git/blame - target-arm/op_helper.c
softmmu_header: pass CPUState to tlb_fill
[qemu.git] / target-arm / op_helper.c
CommitLineData
b7bcbe95
FB
1/*
2 * ARM helper routines
5fafdf24 3 *
9ee6e8bb 4 * Copyright (c) 2005-2007 CodeSourcery, LLC
b7bcbe95
FB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
b7bcbe95 18 */
3e457172
BS
19#include "cpu.h"
20#include "dyngen-exec.h"
7b59220e 21#include "helper.h"
b7bcbe95 22
ad69471c
PB
23#define SIGNBIT (uint32_t)0x80000000
24#define SIGNBIT64 ((uint64_t)1 << 63)
25
3e457172
BS
26#if !defined(CONFIG_USER_ONLY)
27static void raise_exception(int tt)
b7bcbe95
FB
28{
29 env->exception_index = tt;
1162c041 30 cpu_loop_exit(env);
b7bcbe95 31}
3e457172 32#endif
b7bcbe95 33
8f8e3aa4
PB
34uint32_t HELPER(neon_tbl)(uint32_t ireg, uint32_t def,
35 uint32_t rn, uint32_t maxindex)
9ee6e8bb
PB
36{
37 uint32_t val;
9ee6e8bb
PB
38 uint32_t tmp;
39 int index;
40 int shift;
41 uint64_t *table;
42 table = (uint64_t *)&env->vfp.regs[rn];
43 val = 0;
9ee6e8bb 44 for (shift = 0; shift < 32; shift += 8) {
8f8e3aa4
PB
45 index = (ireg >> shift) & 0xff;
46 if (index < maxindex) {
3018f259 47 tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
9ee6e8bb
PB
48 val |= tmp << shift;
49 } else {
8f8e3aa4 50 val |= def & (0xff << shift);
9ee6e8bb
PB
51 }
52 }
8f8e3aa4 53 return val;
9ee6e8bb
PB
54}
55
b5ff1b31
FB
56#if !defined(CONFIG_USER_ONLY)
57
3e457172
BS
58#include "softmmu_exec.h"
59
b5ff1b31 60#define MMUSUFFIX _mmu
b5ff1b31
FB
61
62#define SHIFT 0
63#include "softmmu_template.h"
64
65#define SHIFT 1
66#include "softmmu_template.h"
67
68#define SHIFT 2
69#include "softmmu_template.h"
70
71#define SHIFT 3
72#include "softmmu_template.h"
73
74/* try to fill the TLB and return an exception if error. If retaddr is
75 NULL, it means that the function was called in C code (i.e. not
76 from generated code or from helper.c) */
77/* XXX: fix it to restore all registers */
bccd9ec5
BS
78void tlb_fill(CPUState *env1, target_ulong addr, int is_write, int mmu_idx,
79 void *retaddr)
b5ff1b31
FB
80{
81 TranslationBlock *tb;
82 CPUState *saved_env;
44f8625d 83 unsigned long pc;
b5ff1b31
FB
84 int ret;
85
b5ff1b31 86 saved_env = env;
97b348e7 87 ret = cpu_arm_handle_mmu_fault(env, addr, is_write, mmu_idx);
551bd27f 88 if (unlikely(ret)) {
b5ff1b31
FB
89 if (retaddr) {
90 /* now we have a real cpu fault */
44f8625d 91 pc = (unsigned long)retaddr;
b5ff1b31
FB
92 tb = tb_find_pc(pc);
93 if (tb) {
94 /* the PC is inside the translated code. It means that we have
95 a virtual CPU fault */
618ba8e6 96 cpu_restore_state(tb, env, pc);
b5ff1b31
FB
97 }
98 }
99 raise_exception(env->exception_index);
100 }
101 env = saved_env;
102}
b5ff1b31 103#endif
1497c961 104
ad69471c
PB
105/* FIXME: Pass an axplicit pointer to QF to CPUState, and move saturating
106 instructions into helper.c */
1497c961
PB
107uint32_t HELPER(add_setq)(uint32_t a, uint32_t b)
108{
109 uint32_t res = a + b;
110 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
111 env->QF = 1;
112 return res;
113}
114
115uint32_t HELPER(add_saturate)(uint32_t a, uint32_t b)
116{
117 uint32_t res = a + b;
118 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
119 env->QF = 1;
120 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
121 }
122 return res;
123}
124
125uint32_t HELPER(sub_saturate)(uint32_t a, uint32_t b)
126{
127 uint32_t res = a - b;
128 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
129 env->QF = 1;
130 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
131 }
132 return res;
133}
134
135uint32_t HELPER(double_saturate)(int32_t val)
136{
137 uint32_t res;
138 if (val >= 0x40000000) {
139 res = ~SIGNBIT;
140 env->QF = 1;
141 } else if (val <= (int32_t)0xc0000000) {
142 res = SIGNBIT;
143 env->QF = 1;
144 } else {
145 res = val << 1;
146 }
147 return res;
148}
149
150uint32_t HELPER(add_usaturate)(uint32_t a, uint32_t b)
151{
152 uint32_t res = a + b;
153 if (res < a) {
154 env->QF = 1;
155 res = ~0;
156 }
157 return res;
158}
159
160uint32_t HELPER(sub_usaturate)(uint32_t a, uint32_t b)
161{
162 uint32_t res = a - b;
163 if (res > a) {
164 env->QF = 1;
165 res = 0;
166 }
167 return res;
168}
169
6ddbc6e4
PB
170/* Signed saturation. */
171static inline uint32_t do_ssat(int32_t val, int shift)
172{
173 int32_t top;
174 uint32_t mask;
175
6ddbc6e4
PB
176 top = val >> shift;
177 mask = (1u << shift) - 1;
178 if (top > 0) {
179 env->QF = 1;
180 return mask;
181 } else if (top < -1) {
182 env->QF = 1;
183 return ~mask;
184 }
185 return val;
186}
187
188/* Unsigned saturation. */
189static inline uint32_t do_usat(int32_t val, int shift)
190{
191 uint32_t max;
192
6ddbc6e4
PB
193 max = (1u << shift) - 1;
194 if (val < 0) {
195 env->QF = 1;
196 return 0;
197 } else if (val > max) {
198 env->QF = 1;
199 return max;
200 }
201 return val;
202}
203
204/* Signed saturate. */
205uint32_t HELPER(ssat)(uint32_t x, uint32_t shift)
206{
207 return do_ssat(x, shift);
208}
209
210/* Dual halfword signed saturate. */
211uint32_t HELPER(ssat16)(uint32_t x, uint32_t shift)
212{
213 uint32_t res;
214
215 res = (uint16_t)do_ssat((int16_t)x, shift);
216 res |= do_ssat(((int32_t)x) >> 16, shift) << 16;
217 return res;
218}
219
220/* Unsigned saturate. */
221uint32_t HELPER(usat)(uint32_t x, uint32_t shift)
222{
223 return do_usat(x, shift);
224}
225
226/* Dual halfword unsigned saturate. */
227uint32_t HELPER(usat16)(uint32_t x, uint32_t shift)
228{
229 uint32_t res;
230
231 res = (uint16_t)do_usat((int16_t)x, shift);
232 res |= do_usat(((int32_t)x) >> 16, shift) << 16;
233 return res;
234}
d9ba4830
PB
235
236void HELPER(wfi)(void)
237{
238 env->exception_index = EXCP_HLT;
239 env->halted = 1;
1162c041 240 cpu_loop_exit(env);
d9ba4830
PB
241}
242
243void HELPER(exception)(uint32_t excp)
244{
245 env->exception_index = excp;
1162c041 246 cpu_loop_exit(env);
d9ba4830
PB
247}
248
249uint32_t HELPER(cpsr_read)(void)
250{
251 return cpsr_read(env) & ~CPSR_EXEC;
252}
253
254void HELPER(cpsr_write)(uint32_t val, uint32_t mask)
255{
256 cpsr_write(env, val, mask);
257}
b0109805
PB
258
259/* Access to user mode registers from privileged modes. */
260uint32_t HELPER(get_user_reg)(uint32_t regno)
261{
262 uint32_t val;
263
264 if (regno == 13) {
265 val = env->banked_r13[0];
266 } else if (regno == 14) {
267 val = env->banked_r14[0];
268 } else if (regno >= 8
269 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
270 val = env->usr_regs[regno - 8];
271 } else {
272 val = env->regs[regno];
273 }
274 return val;
275}
276
277void HELPER(set_user_reg)(uint32_t regno, uint32_t val)
278{
279 if (regno == 13) {
280 env->banked_r13[0] = val;
281 } else if (regno == 14) {
282 env->banked_r14[0] = val;
283 } else if (regno >= 8
284 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
285 env->usr_regs[regno - 8] = val;
286 } else {
287 env->regs[regno] = val;
288 }
289}
290
8984bd2e
PB
291/* ??? Flag setting arithmetic is awkward because we need to do comparisons.
292 The only way to do that in TCG is a conditional branch, which clobbers
293 all our temporaries. For now implement these as helper functions. */
294
295uint32_t HELPER (add_cc)(uint32_t a, uint32_t b)
296{
297 uint32_t result;
37f9ba46 298 result = a + b;
6fbe23d5 299 env->NF = env->ZF = result;
8984bd2e
PB
300 env->CF = result < a;
301 env->VF = (a ^ b ^ -1) & (a ^ result);
302 return result;
303}
304
305uint32_t HELPER(adc_cc)(uint32_t a, uint32_t b)
306{
307 uint32_t result;
308 if (!env->CF) {
309 result = a + b;
310 env->CF = result < a;
311 } else {
312 result = a + b + 1;
313 env->CF = result <= a;
314 }
315 env->VF = (a ^ b ^ -1) & (a ^ result);
6fbe23d5 316 env->NF = env->ZF = result;
8984bd2e
PB
317 return result;
318}
319
320uint32_t HELPER(sub_cc)(uint32_t a, uint32_t b)
321{
322 uint32_t result;
323 result = a - b;
6fbe23d5 324 env->NF = env->ZF = result;
8984bd2e
PB
325 env->CF = a >= b;
326 env->VF = (a ^ b) & (a ^ result);
327 return result;
328}
329
330uint32_t HELPER(sbc_cc)(uint32_t a, uint32_t b)
331{
332 uint32_t result;
333 if (!env->CF) {
334 result = a - b - 1;
335 env->CF = a > b;
336 } else {
337 result = a - b;
338 env->CF = a >= b;
339 }
340 env->VF = (a ^ b) & (a ^ result);
6fbe23d5 341 env->NF = env->ZF = result;
8984bd2e
PB
342 return result;
343}
344
345/* Similarly for variable shift instructions. */
346
347uint32_t HELPER(shl)(uint32_t x, uint32_t i)
348{
349 int shift = i & 0xff;
350 if (shift >= 32)
351 return 0;
352 return x << shift;
353}
354
355uint32_t HELPER(shr)(uint32_t x, uint32_t i)
356{
357 int shift = i & 0xff;
358 if (shift >= 32)
359 return 0;
360 return (uint32_t)x >> shift;
361}
362
363uint32_t HELPER(sar)(uint32_t x, uint32_t i)
364{
365 int shift = i & 0xff;
366 if (shift >= 32)
367 shift = 31;
368 return (int32_t)x >> shift;
369}
370
8984bd2e
PB
371uint32_t HELPER(shl_cc)(uint32_t x, uint32_t i)
372{
373 int shift = i & 0xff;
374 if (shift >= 32) {
375 if (shift == 32)
376 env->CF = x & 1;
377 else
378 env->CF = 0;
379 return 0;
380 } else if (shift != 0) {
381 env->CF = (x >> (32 - shift)) & 1;
382 return x << shift;
383 }
384 return x;
385}
386
387uint32_t HELPER(shr_cc)(uint32_t x, uint32_t i)
388{
389 int shift = i & 0xff;
390 if (shift >= 32) {
391 if (shift == 32)
392 env->CF = (x >> 31) & 1;
393 else
394 env->CF = 0;
395 return 0;
396 } else if (shift != 0) {
397 env->CF = (x >> (shift - 1)) & 1;
398 return x >> shift;
399 }
400 return x;
401}
402
403uint32_t HELPER(sar_cc)(uint32_t x, uint32_t i)
404{
405 int shift = i & 0xff;
406 if (shift >= 32) {
407 env->CF = (x >> 31) & 1;
408 return (int32_t)x >> 31;
409 } else if (shift != 0) {
410 env->CF = (x >> (shift - 1)) & 1;
411 return (int32_t)x >> shift;
412 }
413 return x;
414}
415
416uint32_t HELPER(ror_cc)(uint32_t x, uint32_t i)
417{
418 int shift1, shift;
419 shift1 = i & 0xff;
420 shift = shift1 & 0x1f;
421 if (shift == 0) {
422 if (shift1 != 0)
423 env->CF = (x >> 31) & 1;
424 return x;
425 } else {
426 env->CF = (x >> (shift - 1)) & 1;
427 return ((uint32_t)x >> shift) | (x << (32 - shift));
428 }
429}