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