]> Git Repo - qemu.git/blob - target-arm/op_helper.c
Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20140310' into...
[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 "cpu.h"
20 #include "helper.h"
21
22 #define SIGNBIT (uint32_t)0x80000000
23 #define SIGNBIT64 ((uint64_t)1 << 63)
24
25 static void raise_exception(CPUARMState *env, int tt)
26 {
27     env->exception_index = tt;
28     cpu_loop_exit(env);
29 }
30
31 uint32_t HELPER(neon_tbl)(CPUARMState *env, 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 #include "exec/softmmu_exec.h"
56
57 #define MMUSUFFIX _mmu
58
59 #define SHIFT 0
60 #include "exec/softmmu_template.h"
61
62 #define SHIFT 1
63 #include "exec/softmmu_template.h"
64
65 #define SHIFT 2
66 #include "exec/softmmu_template.h"
67
68 #define SHIFT 3
69 #include "exec/softmmu_template.h"
70
71 /* try to fill the TLB and return an exception if error. If retaddr is
72    NULL, it means that the function was called in C code (i.e. not
73    from generated code or from helper.c) */
74 void tlb_fill(CPUARMState *env, target_ulong addr, int is_write, int mmu_idx,
75               uintptr_t retaddr)
76 {
77     int ret;
78
79     ret = cpu_arm_handle_mmu_fault(env, addr, is_write, mmu_idx);
80     if (unlikely(ret)) {
81         if (retaddr) {
82             /* now we have a real cpu fault */
83             cpu_restore_state(env, retaddr);
84         }
85         raise_exception(env, env->exception_index);
86     }
87 }
88 #endif
89
90 uint32_t HELPER(add_setq)(CPUARMState *env, uint32_t a, uint32_t b)
91 {
92     uint32_t res = a + b;
93     if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT))
94         env->QF = 1;
95     return res;
96 }
97
98 uint32_t HELPER(add_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
99 {
100     uint32_t res = a + b;
101     if (((res ^ a) & SIGNBIT) && !((a ^ b) & SIGNBIT)) {
102         env->QF = 1;
103         res = ~(((int32_t)a >> 31) ^ SIGNBIT);
104     }
105     return res;
106 }
107
108 uint32_t HELPER(sub_saturate)(CPUARMState *env, uint32_t a, uint32_t b)
109 {
110     uint32_t res = a - b;
111     if (((res ^ a) & SIGNBIT) && ((a ^ b) & SIGNBIT)) {
112         env->QF = 1;
113         res = ~(((int32_t)a >> 31) ^ SIGNBIT);
114     }
115     return res;
116 }
117
118 uint32_t HELPER(double_saturate)(CPUARMState *env, int32_t val)
119 {
120     uint32_t res;
121     if (val >= 0x40000000) {
122         res = ~SIGNBIT;
123         env->QF = 1;
124     } else if (val <= (int32_t)0xc0000000) {
125         res = SIGNBIT;
126         env->QF = 1;
127     } else {
128         res = val << 1;
129     }
130     return res;
131 }
132
133 uint32_t HELPER(add_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
134 {
135     uint32_t res = a + b;
136     if (res < a) {
137         env->QF = 1;
138         res = ~0;
139     }
140     return res;
141 }
142
143 uint32_t HELPER(sub_usaturate)(CPUARMState *env, uint32_t a, uint32_t b)
144 {
145     uint32_t res = a - b;
146     if (res > a) {
147         env->QF = 1;
148         res = 0;
149     }
150     return res;
151 }
152
153 /* Signed saturation.  */
154 static inline uint32_t do_ssat(CPUARMState *env, int32_t val, int shift)
155 {
156     int32_t top;
157     uint32_t mask;
158
159     top = val >> shift;
160     mask = (1u << shift) - 1;
161     if (top > 0) {
162         env->QF = 1;
163         return mask;
164     } else if (top < -1) {
165         env->QF = 1;
166         return ~mask;
167     }
168     return val;
169 }
170
171 /* Unsigned saturation.  */
172 static inline uint32_t do_usat(CPUARMState *env, int32_t val, int shift)
173 {
174     uint32_t max;
175
176     max = (1u << shift) - 1;
177     if (val < 0) {
178         env->QF = 1;
179         return 0;
180     } else if (val > max) {
181         env->QF = 1;
182         return max;
183     }
184     return val;
185 }
186
187 /* Signed saturate.  */
188 uint32_t HELPER(ssat)(CPUARMState *env, uint32_t x, uint32_t shift)
189 {
190     return do_ssat(env, x, shift);
191 }
192
193 /* Dual halfword signed saturate.  */
194 uint32_t HELPER(ssat16)(CPUARMState *env, uint32_t x, uint32_t shift)
195 {
196     uint32_t res;
197
198     res = (uint16_t)do_ssat(env, (int16_t)x, shift);
199     res |= do_ssat(env, ((int32_t)x) >> 16, shift) << 16;
200     return res;
201 }
202
203 /* Unsigned saturate.  */
204 uint32_t HELPER(usat)(CPUARMState *env, uint32_t x, uint32_t shift)
205 {
206     return do_usat(env, x, shift);
207 }
208
209 /* Dual halfword unsigned saturate.  */
210 uint32_t HELPER(usat16)(CPUARMState *env, uint32_t x, uint32_t shift)
211 {
212     uint32_t res;
213
214     res = (uint16_t)do_usat(env, (int16_t)x, shift);
215     res |= do_usat(env, ((int32_t)x) >> 16, shift) << 16;
216     return res;
217 }
218
219 void HELPER(wfi)(CPUARMState *env)
220 {
221     CPUState *cs = CPU(arm_env_get_cpu(env));
222
223     env->exception_index = EXCP_HLT;
224     cs->halted = 1;
225     cpu_loop_exit(env);
226 }
227
228 void HELPER(wfe)(CPUARMState *env)
229 {
230     /* Don't actually halt the CPU, just yield back to top
231      * level loop
232      */
233     env->exception_index = EXCP_YIELD;
234     cpu_loop_exit(env);
235 }
236
237 void HELPER(exception)(CPUARMState *env, uint32_t excp)
238 {
239     env->exception_index = excp;
240     cpu_loop_exit(env);
241 }
242
243 uint32_t HELPER(cpsr_read)(CPUARMState *env)
244 {
245     return cpsr_read(env) & ~CPSR_EXEC;
246 }
247
248 void HELPER(cpsr_write)(CPUARMState *env, uint32_t val, uint32_t mask)
249 {
250     cpsr_write(env, val, mask);
251 }
252
253 /* Access to user mode registers from privileged modes.  */
254 uint32_t HELPER(get_user_reg)(CPUARMState *env, uint32_t regno)
255 {
256     uint32_t val;
257
258     if (regno == 13) {
259         val = env->banked_r13[0];
260     } else if (regno == 14) {
261         val = env->banked_r14[0];
262     } else if (regno >= 8
263                && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
264         val = env->usr_regs[regno - 8];
265     } else {
266         val = env->regs[regno];
267     }
268     return val;
269 }
270
271 void HELPER(set_user_reg)(CPUARMState *env, uint32_t regno, uint32_t val)
272 {
273     if (regno == 13) {
274         env->banked_r13[0] = val;
275     } else if (regno == 14) {
276         env->banked_r14[0] = val;
277     } else if (regno >= 8
278                && (env->uncached_cpsr & 0x1f) == ARM_CPU_MODE_FIQ) {
279         env->usr_regs[regno - 8] = val;
280     } else {
281         env->regs[regno] = val;
282     }
283 }
284
285 void HELPER(access_check_cp_reg)(CPUARMState *env, void *rip)
286 {
287     const ARMCPRegInfo *ri = rip;
288     switch (ri->accessfn(env, ri)) {
289     case CP_ACCESS_OK:
290         return;
291     case CP_ACCESS_TRAP:
292     case CP_ACCESS_TRAP_UNCATEGORIZED:
293         /* These cases will eventually need to generate different
294          * syndrome information.
295          */
296         break;
297     default:
298         g_assert_not_reached();
299     }
300     raise_exception(env, EXCP_UDEF);
301 }
302
303 void HELPER(set_cp_reg)(CPUARMState *env, void *rip, uint32_t value)
304 {
305     const ARMCPRegInfo *ri = rip;
306
307     ri->writefn(env, ri, value);
308 }
309
310 uint32_t HELPER(get_cp_reg)(CPUARMState *env, void *rip)
311 {
312     const ARMCPRegInfo *ri = rip;
313
314     return ri->readfn(env, ri);
315 }
316
317 void HELPER(set_cp_reg64)(CPUARMState *env, void *rip, uint64_t value)
318 {
319     const ARMCPRegInfo *ri = rip;
320
321     ri->writefn(env, ri, value);
322 }
323
324 uint64_t HELPER(get_cp_reg64)(CPUARMState *env, void *rip)
325 {
326     const ARMCPRegInfo *ri = rip;
327
328     return ri->readfn(env, ri);
329 }
330
331 void HELPER(msr_i_pstate)(CPUARMState *env, uint32_t op, uint32_t imm)
332 {
333     /* MSR_i to update PSTATE. This is OK from EL0 only if UMA is set.
334      * Note that SPSel is never OK from EL0; we rely on handle_msr_i()
335      * to catch that case at translate time.
336      */
337     if (arm_current_pl(env) == 0 && !(env->cp15.c1_sys & SCTLR_UMA)) {
338         raise_exception(env, EXCP_UDEF);
339     }
340
341     switch (op) {
342     case 0x05: /* SPSel */
343         env->pstate = deposit32(env->pstate, 0, 1, imm);
344         break;
345     case 0x1e: /* DAIFSet */
346         env->daif |= (imm << 6) & PSTATE_DAIF;
347         break;
348     case 0x1f: /* DAIFClear */
349         env->daif &= ~((imm << 6) & PSTATE_DAIF);
350         break;
351     default:
352         g_assert_not_reached();
353     }
354 }
355
356 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
357    The only way to do that in TCG is a conditional branch, which clobbers
358    all our temporaries.  For now implement these as helper functions.  */
359
360 /* Similarly for variable shift instructions.  */
361
362 uint32_t HELPER(shl_cc)(CPUARMState *env, uint32_t x, uint32_t i)
363 {
364     int shift = i & 0xff;
365     if (shift >= 32) {
366         if (shift == 32)
367             env->CF = x & 1;
368         else
369             env->CF = 0;
370         return 0;
371     } else if (shift != 0) {
372         env->CF = (x >> (32 - shift)) & 1;
373         return x << shift;
374     }
375     return x;
376 }
377
378 uint32_t HELPER(shr_cc)(CPUARMState *env, uint32_t x, uint32_t i)
379 {
380     int shift = i & 0xff;
381     if (shift >= 32) {
382         if (shift == 32)
383             env->CF = (x >> 31) & 1;
384         else
385             env->CF = 0;
386         return 0;
387     } else if (shift != 0) {
388         env->CF = (x >> (shift - 1)) & 1;
389         return x >> shift;
390     }
391     return x;
392 }
393
394 uint32_t HELPER(sar_cc)(CPUARMState *env, uint32_t x, uint32_t i)
395 {
396     int shift = i & 0xff;
397     if (shift >= 32) {
398         env->CF = (x >> 31) & 1;
399         return (int32_t)x >> 31;
400     } else if (shift != 0) {
401         env->CF = (x >> (shift - 1)) & 1;
402         return (int32_t)x >> shift;
403     }
404     return x;
405 }
406
407 uint32_t HELPER(ror_cc)(CPUARMState *env, uint32_t x, uint32_t i)
408 {
409     int shift1, shift;
410     shift1 = i & 0xff;
411     shift = shift1 & 0x1f;
412     if (shift == 0) {
413         if (shift1 != 0)
414             env->CF = (x >> 31) & 1;
415         return x;
416     } else {
417         env->CF = (x >> (shift - 1)) & 1;
418         return ((uint32_t)x >> shift) | (x << (32 - shift));
419     }
420 }
This page took 0.047178 seconds and 4 git commands to generate.