]> git.proxmox.com Git - qemu.git/blame - target-arm/op_helper.c
Remove unused is_softmmu parameter from cpu_handle_mmu_fault
[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 */
6ebbf390 78void tlb_fill (target_ulong addr, int is_write, int mmu_idx, void *retaddr)
b5ff1b31
FB
79{
80 TranslationBlock *tb;
81 CPUState *saved_env;
44f8625d 82 unsigned long pc;
b5ff1b31
FB
83 int ret;
84
85 /* XXX: hack to restore env in all cases, even if not called from
86 generated code */
87 saved_env = env;
88 env = cpu_single_env;
97b348e7 89 ret = cpu_arm_handle_mmu_fault(env, addr, is_write, mmu_idx);
551bd27f 90 if (unlikely(ret)) {
b5ff1b31
FB
91 if (retaddr) {
92 /* now we have a real cpu fault */
44f8625d 93 pc = (unsigned long)retaddr;
b5ff1b31
FB
94 tb = tb_find_pc(pc);
95 if (tb) {
96 /* the PC is inside the translated code. It means that we have
97 a virtual CPU fault */
618ba8e6 98 cpu_restore_state(tb, env, pc);
b5ff1b31
FB
99 }
100 }
101 raise_exception(env->exception_index);
102 }
103 env = saved_env;
104}
b5ff1b31 105#endif
1497c961 106
ad69471c
PB
107/* FIXME: Pass an axplicit pointer to QF to CPUState, and move saturating
108 instructions into helper.c */
1497c961
PB
109uint32_t HELPER(add_setq)(uint32_t a, uint32_t b)
110{
111 uint32_t res = a + b;
112 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
113 env->QF = 1;
114 return res;
115}
116
117uint32_t HELPER(add_saturate)(uint32_t a, uint32_t b)
118{
119 uint32_t res = a + b;
120 if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
121 env->QF = 1;
122 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
123 }
124 return res;
125}
126
127uint32_t HELPER(sub_saturate)(uint32_t a, uint32_t b)
128{
129 uint32_t res = a - b;
130 if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
131 env->QF = 1;
132 res = ~(((int32_t)a >> 31) ^ SIGNBIT);
133 }
134 return res;
135}
136
137uint32_t HELPER(double_saturate)(int32_t val)
138{
139 uint32_t res;
140 if (val >= 0x40000000) {
141 res = ~SIGNBIT;
142 env->QF = 1;
143 } else if (val <= (int32_t)0xc0000000) {
144 res = SIGNBIT;
145 env->QF = 1;
146 } else {
147 res = val << 1;
148 }
149 return res;
150}
151
152uint32_t HELPER(add_usaturate)(uint32_t a, uint32_t b)
153{
154 uint32_t res = a + b;
155 if (res < a) {
156 env->QF = 1;
157 res = ~0;
158 }
159 return res;
160}
161
162uint32_t HELPER(sub_usaturate)(uint32_t a, uint32_t b)
163{
164 uint32_t res = a - b;
165 if (res > a) {
166 env->QF = 1;
167 res = 0;
168 }
169 return res;
170}
171
6ddbc6e4
PB
172/* Signed saturation. */
173static inline uint32_t do_ssat(int32_t val, int shift)
174{
175 int32_t top;
176 uint32_t mask;
177
6ddbc6e4
PB
178 top = val >> shift;
179 mask = (1u << shift) - 1;
180 if (top > 0) {
181 env->QF = 1;
182 return mask;
183 } else if (top < -1) {
184 env->QF = 1;
185 return ~mask;
186 }
187 return val;
188}
189
190/* Unsigned saturation. */
191static inline uint32_t do_usat(int32_t val, int shift)
192{
193 uint32_t max;
194
6ddbc6e4
PB
195 max = (1u << shift) - 1;
196 if (val < 0) {
197 env->QF = 1;
198 return 0;
199 } else if (val > max) {
200 env->QF = 1;
201 return max;
202 }
203 return val;
204}
205
206/* Signed saturate. */
207uint32_t HELPER(ssat)(uint32_t x, uint32_t shift)
208{
209 return do_ssat(x, shift);
210}
211
212/* Dual halfword signed saturate. */
213uint32_t HELPER(ssat16)(uint32_t x, uint32_t shift)
214{
215 uint32_t res;
216
217 res = (uint16_t)do_ssat((int16_t)x, shift);
218 res |= do_ssat(((int32_t)x) >> 16, shift) << 16;
219 return res;
220}
221
222/* Unsigned saturate. */
223uint32_t HELPER(usat)(uint32_t x, uint32_t shift)
224{
225 return do_usat(x, shift);
226}
227
228/* Dual halfword unsigned saturate. */
229uint32_t HELPER(usat16)(uint32_t x, uint32_t shift)
230{
231 uint32_t res;
232
233 res = (uint16_t)do_usat((int16_t)x, shift);
234 res |= do_usat(((int32_t)x) >> 16, shift) << 16;
235 return res;
236}
d9ba4830
PB
237
238void HELPER(wfi)(void)
239{
240 env->exception_index = EXCP_HLT;
241 env->halted = 1;
1162c041 242 cpu_loop_exit(env);
d9ba4830
PB
243}
244
245void HELPER(exception)(uint32_t excp)
246{
247 env->exception_index = excp;
1162c041 248 cpu_loop_exit(env);
d9ba4830
PB
249}
250
251uint32_t HELPER(cpsr_read)(void)
252{
253 return cpsr_read(env) & ~CPSR_EXEC;
254}
255
256void HELPER(cpsr_write)(uint32_t val, uint32_t mask)
257{
258 cpsr_write(env, val, mask);
259}
b0109805
PB
260
261/* Access to user mode registers from privileged modes. */
262uint32_t HELPER(get_user_reg)(uint32_t regno)
263{
264 uint32_t val;
265
266 if (regno == 13) {
267 val = env->banked_r13[0];
268 } else if (regno == 14) {
269 val = env->banked_r14[0];
270 } else if (regno >= 8
271 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
272 val = env->usr_regs[regno - 8];
273 } else {
274 val = env->regs[regno];
275 }
276 return val;
277}
278
279void HELPER(set_user_reg)(uint32_t regno, uint32_t val)
280{
281 if (regno == 13) {
282 env->banked_r13[0] = val;
283 } else if (regno == 14) {
284 env->banked_r14[0] = val;
285 } else if (regno >= 8
286 && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
287 env->usr_regs[regno - 8] = val;
288 } else {
289 env->regs[regno] = val;
290 }
291}
292
8984bd2e
PB
293/* ??? Flag setting arithmetic is awkward because we need to do comparisons.
294 The only way to do that in TCG is a conditional branch, which clobbers
295 all our temporaries. For now implement these as helper functions. */
296
297uint32_t HELPER (add_cc)(uint32_t a, uint32_t b)
298{
299 uint32_t result;
37f9ba46 300 result = a + b;
6fbe23d5 301 env->NF = env->ZF = result;
8984bd2e
PB
302 env->CF = result < a;
303 env->VF = (a ^ b ^ -1) & (a ^ result);
304 return result;
305}
306
307uint32_t HELPER(adc_cc)(uint32_t a, uint32_t b)
308{
309 uint32_t result;
310 if (!env->CF) {
311 result = a + b;
312 env->CF = result < a;
313 } else {
314 result = a + b + 1;
315 env->CF = result <= a;
316 }
317 env->VF = (a ^ b ^ -1) & (a ^ result);
6fbe23d5 318 env->NF = env->ZF = result;
8984bd2e
PB
319 return result;
320}
321
322uint32_t HELPER(sub_cc)(uint32_t a, uint32_t b)
323{
324 uint32_t result;
325 result = a - b;
6fbe23d5 326 env->NF = env->ZF = result;
8984bd2e
PB
327 env->CF = a >= b;
328 env->VF = (a ^ b) & (a ^ result);
329 return result;
330}
331
332uint32_t HELPER(sbc_cc)(uint32_t a, uint32_t b)
333{
334 uint32_t result;
335 if (!env->CF) {
336 result = a - b - 1;
337 env->CF = a > b;
338 } else {
339 result = a - b;
340 env->CF = a >= b;
341 }
342 env->VF = (a ^ b) & (a ^ result);
6fbe23d5 343 env->NF = env->ZF = result;
8984bd2e
PB
344 return result;
345}
346
347/* Similarly for variable shift instructions. */
348
349uint32_t HELPER(shl)(uint32_t x, uint32_t i)
350{
351 int shift = i & 0xff;
352 if (shift >= 32)
353 return 0;
354 return x << shift;
355}
356
357uint32_t HELPER(shr)(uint32_t x, uint32_t i)
358{
359 int shift = i & 0xff;
360 if (shift >= 32)
361 return 0;
362 return (uint32_t)x >> shift;
363}
364
365uint32_t HELPER(sar)(uint32_t x, uint32_t i)
366{
367 int shift = i & 0xff;
368 if (shift >= 32)
369 shift = 31;
370 return (int32_t)x >> shift;
371}
372
8984bd2e
PB
373uint32_t HELPER(shl_cc)(uint32_t x, uint32_t i)
374{
375 int shift = i & 0xff;
376 if (shift >= 32) {
377 if (shift == 32)
378 env->CF = x & 1;
379 else
380 env->CF = 0;
381 return 0;
382 } else if (shift != 0) {
383 env->CF = (x >> (32 - shift)) & 1;
384 return x << shift;
385 }
386 return x;
387}
388
389uint32_t HELPER(shr_cc)(uint32_t x, uint32_t i)
390{
391 int shift = i & 0xff;
392 if (shift >= 32) {
393 if (shift == 32)
394 env->CF = (x >> 31) & 1;
395 else
396 env->CF = 0;
397 return 0;
398 } else if (shift != 0) {
399 env->CF = (x >> (shift - 1)) & 1;
400 return x >> shift;
401 }
402 return x;
403}
404
405uint32_t HELPER(sar_cc)(uint32_t x, uint32_t i)
406{
407 int shift = i & 0xff;
408 if (shift >= 32) {
409 env->CF = (x >> 31) & 1;
410 return (int32_t)x >> 31;
411 } else if (shift != 0) {
412 env->CF = (x >> (shift - 1)) & 1;
413 return (int32_t)x >> shift;
414 }
415 return x;
416}
417
418uint32_t HELPER(ror_cc)(uint32_t x, uint32_t i)
419{
420 int shift1, shift;
421 shift1 = i & 0xff;
422 shift = shift1 & 0x1f;
423 if (shift == 0) {
424 if (shift1 != 0)
425 env->CF = (x >> 31) & 1;
426 return x;
427 } else {
428 env->CF = (x >> (shift - 1)) & 1;
429 return ((uint32_t)x >> shift) | (x << (32 - shift));
430 }
431}