Merge remote branch 'qmp/for-anthony' into staging
[qemu.git] / target-arm / op_helper.c
blob9b1a0143d24e2ca5e0822d039939f80da7e30df6
1 /*
2 * ARM helper routines
4 * Copyright (c) 2005-2007 CodeSourcery, LLC
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "exec.h"
20 #include "helpers.h"
22 #define SIGNBIT (uint32_t)0x80000000
23 #define SIGNBIT64 ((uint64_t)1 << 63)
25 void raise_exception(int tt)
27 env->exception_index = tt;
28 cpu_loop_exit();
31 /* thread support */
33 static spinlock_t global_cpu_lock = SPIN_LOCK_UNLOCKED;
35 void cpu_lock(void)
37 spin_lock(&global_cpu_lock);
40 void cpu_unlock(void)
42 spin_unlock(&global_cpu_lock);
45 uint32_t HELPER(neon_tbl)(uint32_t ireg, uint32_t def,
46 uint32_t rn, uint32_t maxindex)
48 uint32_t val;
49 uint32_t tmp;
50 int index;
51 int shift;
52 uint64_t *table;
53 table = (uint64_t *)&env->vfp.regs[rn];
54 val = 0;
55 for (shift = 0; shift < 32; shift += 8) {
56 index = (ireg >> shift) & 0xff;
57 if (index < maxindex) {
58 tmp = (table[index >> 3] >> ((index & 7) << 3)) & 0xff;
59 val |= tmp << shift;
60 } else {
61 val |= def & (0xff << shift);
64 return val;
67 #if !defined(CONFIG_USER_ONLY)
69 #define MMUSUFFIX _mmu
71 #define SHIFT 0
72 #include "softmmu_template.h"
74 #define SHIFT 1
75 #include "softmmu_template.h"
77 #define SHIFT 2
78 #include "softmmu_template.h"
80 #define SHIFT 3
81 #include "softmmu_template.h"
83 /* try to fill the TLB and return an exception if error. If retaddr is
84 NULL, it means that the function was called in C code (i.e. not
85 from generated code or from helper.c) */
86 /* XXX: fix it to restore all registers */
87 void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
89 TranslationBlock *tb;
90 CPUState *saved_env;
91 unsigned long pc;
92 int ret;
94 /* XXX: hack to restore env in all cases, even if not called from
95 generated code */
96 saved_env = env;
97 env = cpu_single_env;
98 ret = cpu_arm_handle_mmu_fault(env, addr, is_write, mmu_idx, 1);
99 if (unlikely(ret)) {
100 if (retaddr) {
101 /* now we have a real cpu fault */
102 pc = (unsigned long)retaddr;
103 tb = tb_find_pc(pc);
104 if (tb) {
105 /* the PC is inside the translated code. It means that we have
106 a virtual CPU fault */
107 cpu_restore_state(tb, env, pc, NULL);
110 raise_exception(env->exception_index);
112 env = saved_env;
114 #endif
116 /* FIXME: Pass an axplicit pointer to QF to CPUState, and move saturating
117 instructions into helper.c */
118 uint32_t HELPER(add_setq)(uint32_t a, uint32_t b)
120 uint32_t res = a + b;
121 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
122 env->QF = 1;
123 return res;
126 uint32_t HELPER(add_saturate)(uint32_t a, uint32_t b)
128 uint32_t res = a + b;
129 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
130 env->QF = 1;
131 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
133 return res;
136 uint32_t HELPER(sub_saturate)(uint32_t a, uint32_t b)
138 uint32_t res = a - b;
139 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
140 env->QF = 1;
141 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
143 return res;
146 uint32_t HELPER(double_saturate)(int32_t val)
148 uint32_t res;
149 if (val >= 0x40000000) {
150 res = ~SIGNBIT;
151 env->QF = 1;
152 } else if (val <= (int32_t)0xc0000000) {
153 res = SIGNBIT;
154 env->QF = 1;
155 } else {
156 res = val << 1;
158 return res;
161 uint32_t HELPER(add_usaturate)(uint32_t a, uint32_t b)
163 uint32_t res = a + b;
164 if (res < a) {
165 env->QF = 1;
166 res = ~0;
168 return res;
171 uint32_t HELPER(sub_usaturate)(uint32_t a, uint32_t b)
173 uint32_t res = a - b;
174 if (res > a) {
175 env->QF = 1;
176 res = 0;
178 return res;
181 /* Signed saturation. */
182 static inline uint32_t do_ssat(int32_t val, int shift)
184 int32_t top;
185 uint32_t mask;
187 top = val >> shift;
188 mask = (1u << shift) - 1;
189 if (top > 0) {
190 env->QF = 1;
191 return mask;
192 } else if (top < -1) {
193 env->QF = 1;
194 return ~mask;
196 return val;
199 /* Unsigned saturation. */
200 static inline uint32_t do_usat(int32_t val, int shift)
202 uint32_t max;
204 max = (1u << shift) - 1;
205 if (val < 0) {
206 env->QF = 1;
207 return 0;
208 } else if (val > max) {
209 env->QF = 1;
210 return max;
212 return val;
215 /* Signed saturate. */
216 uint32_t HELPER(ssat)(uint32_t x, uint32_t shift)
218 return do_ssat(x, shift);
221 /* Dual halfword signed saturate. */
222 uint32_t HELPER(ssat16)(uint32_t x, uint32_t shift)
224 uint32_t res;
226 res = (uint16_t)do_ssat((int16_t)x, shift);
227 res |= do_ssat(((int32_t)x) >> 16, shift) << 16;
228 return res;
231 /* Unsigned saturate. */
232 uint32_t HELPER(usat)(uint32_t x, uint32_t shift)
234 return do_usat(x, shift);
237 /* Dual halfword unsigned saturate. */
238 uint32_t HELPER(usat16)(uint32_t x, uint32_t shift)
240 uint32_t res;
242 res = (uint16_t)do_usat((int16_t)x, shift);
243 res |= do_usat(((int32_t)x) >> 16, shift) << 16;
244 return res;
247 void HELPER(wfi)(void)
249 env->exception_index = EXCP_HLT;
250 env->halted = 1;
251 cpu_loop_exit();
254 void HELPER(exception)(uint32_t excp)
256 env->exception_index = excp;
257 cpu_loop_exit();
260 uint32_t HELPER(cpsr_read)(void)
262 return cpsr_read(env) & ~CPSR_EXEC;
265 void HELPER(cpsr_write)(uint32_t val, uint32_t mask)
267 cpsr_write(env, val, mask);
270 /* Access to user mode registers from privileged modes. */
271 uint32_t HELPER(get_user_reg)(uint32_t regno)
273 uint32_t val;
275 if (regno == 13) {
276 val = env->banked_r13[0];
277 } else if (regno == 14) {
278 val = env->banked_r14[0];
279 } else if (regno >= 8
280 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
281 val = env->usr_regs[regno - 8];
282 } else {
283 val = env->regs[regno];
285 return val;
288 void HELPER(set_user_reg)(uint32_t regno, uint32_t val)
290 if (regno == 13) {
291 env->banked_r13[0] = val;
292 } else if (regno == 14) {
293 env->banked_r14[0] = val;
294 } else if (regno >= 8
295 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
296 env->usr_regs[regno - 8] = val;
297 } else {
298 env->regs[regno] = val;
302 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
303 The only way to do that in TCG is a conditional branch, which clobbers
304 all our temporaries. For now implement these as helper functions. */
306 uint32_t HELPER (add_cc)(uint32_t a, uint32_t b)
308 uint32_t result;
309 result = a + b;
310 env->NF = env->ZF = result;
311 env->CF = result < a;
312 env->VF = (a ^ b ^ -1) & (a ^ result);
313 return result;
316 uint32_t HELPER(adc_cc)(uint32_t a, uint32_t b)
318 uint32_t result;
319 if (!env->CF) {
320 result = a + b;
321 env->CF = result < a;
322 } else {
323 result = a + b + 1;
324 env->CF = result <= a;
326 env->VF = (a ^ b ^ -1) & (a ^ result);
327 env->NF = env->ZF = result;
328 return result;
331 uint32_t HELPER(sub_cc)(uint32_t a, uint32_t b)
333 uint32_t result;
334 result = a - b;
335 env->NF = env->ZF = result;
336 env->CF = a >= b;
337 env->VF = (a ^ b) & (a ^ result);
338 return result;
341 uint32_t HELPER(sbc_cc)(uint32_t a, uint32_t b)
343 uint32_t result;
344 if (!env->CF) {
345 result = a - b - 1;
346 env->CF = a > b;
347 } else {
348 result = a - b;
349 env->CF = a >= b;
351 env->VF = (a ^ b) & (a ^ result);
352 env->NF = env->ZF = result;
353 return result;
356 /* Similarly for variable shift instructions. */
358 uint32_t HELPER(shl)(uint32_t x, uint32_t i)
360 int shift = i & 0xff;
361 if (shift >= 32)
362 return 0;
363 return x << shift;
366 uint32_t HELPER(shr)(uint32_t x, uint32_t i)
368 int shift = i & 0xff;
369 if (shift >= 32)
370 return 0;
371 return (uint32_t)x >> shift;
374 uint32_t HELPER(sar)(uint32_t x, uint32_t i)
376 int shift = i & 0xff;
377 if (shift >= 32)
378 shift = 31;
379 return (int32_t)x >> shift;
382 uint32_t HELPER(shl_cc)(uint32_t x, uint32_t i)
384 int shift = i & 0xff;
385 if (shift >= 32) {
386 if (shift == 32)
387 env->CF = x & 1;
388 else
389 env->CF = 0;
390 return 0;
391 } else if (shift != 0) {
392 env->CF = (x >> (32 - shift)) & 1;
393 return x << shift;
395 return x;
398 uint32_t HELPER(shr_cc)(uint32_t x, uint32_t i)
400 int shift = i & 0xff;
401 if (shift >= 32) {
402 if (shift == 32)
403 env->CF = (x >> 31) & 1;
404 else
405 env->CF = 0;
406 return 0;
407 } else if (shift != 0) {
408 env->CF = (x >> (shift - 1)) & 1;
409 return x >> shift;
411 return x;
414 uint32_t HELPER(sar_cc)(uint32_t x, uint32_t i)
416 int shift = i & 0xff;
417 if (shift >= 32) {
418 env->CF = (x >> 31) & 1;
419 return (int32_t)x >> 31;
420 } else if (shift != 0) {
421 env->CF = (x >> (shift - 1)) & 1;
422 return (int32_t)x >> shift;
424 return x;
427 uint32_t HELPER(ror_cc)(uint32_t x, uint32_t i)
429 int shift1, shift;
430 shift1 = i & 0xff;
431 shift = shift1 & 0x1f;
432 if (shift == 0) {
433 if (shift1 != 0)
434 env->CF = (x >> 31) & 1;
435 return x;
436 } else {
437 env->CF = (x >> (shift - 1)) & 1;
438 return ((uint32_t)x >> shift) | (x << (32 - shift));
442 uint64_t HELPER(neon_add_saturate_s64)(uint64_t src1, uint64_t src2)
444 uint64_t res;
446 res = src1 + src2;
447 if (((res ^ src1) & SIGNBIT64) && !((src1 ^ src2) & SIGNBIT64)) {
448 env->QF = 1;
449 res = ((int64_t)src1 >> 63) ^ ~SIGNBIT64;
451 return res;
454 uint64_t HELPER(neon_add_saturate_u64)(uint64_t src1, uint64_t src2)
456 uint64_t res;
458 res = src1 + src2;
459 if (res < src1) {
460 env->QF = 1;
461 res = ~(uint64_t)0;
463 return res;
466 uint64_t HELPER(neon_sub_saturate_s64)(uint64_t src1, uint64_t src2)
468 uint64_t res;
470 res = src1 - src2;
471 if (((res ^ src1) & SIGNBIT64) && ((src1 ^ src2) & SIGNBIT64)) {
472 env->QF = 1;
473 res = ((int64_t)src1 >> 63) ^ ~SIGNBIT64;
475 return res;
478 uint64_t HELPER(neon_sub_saturate_u64)(uint64_t src1, uint64_t src2)
480 uint64_t res;
482 if (src1 < src2) {
483 env->QF = 1;
484 res = 0;
485 } else {
486 res = src1 - src2;
488 return res;