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/>.
20 #include "dyngen-exec.h"
23 #define SIGNBIT (uint32_t)0x80000000
24 #define SIGNBIT64 ((uint64_t)1 << 63)
26 #if !defined(CONFIG_USER_ONLY)
27 static void raise_exception(int tt
)
29 env
->exception_index
= tt
;
34 uint32_t HELPER(neon_tbl
)(uint32_t ireg
, uint32_t def
,
35 uint32_t rn
, uint32_t maxindex
)
42 table
= (uint64_t *)&env
->vfp
.regs
[rn
];
44 for (shift
= 0; shift
< 32; shift
+= 8) {
45 index
= (ireg
>> shift
) & 0xff;
46 if (index
< maxindex
) {
47 tmp
= (table
[index
>> 3] >> ((index
& 7) << 3)) & 0xff;
50 val
|= def
& (0xff << shift
);
56 #if !defined(CONFIG_USER_ONLY)
58 #include "softmmu_exec.h"
60 #define MMUSUFFIX _mmu
63 #include "softmmu_template.h"
66 #include "softmmu_template.h"
69 #include "softmmu_template.h"
72 #include "softmmu_template.h"
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 */
78 void tlb_fill(CPUState
*env1
, target_ulong addr
, int is_write
, int mmu_idx
,
88 ret
= cpu_arm_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
);
91 /* now we have a real cpu fault */
92 pc
= (unsigned long)retaddr
;
95 /* the PC is inside the translated code. It means that we have
96 a virtual CPU fault */
97 cpu_restore_state(tb
, env
, pc
);
100 raise_exception(env
->exception_index
);
106 /* FIXME: Pass an axplicit pointer to QF to CPUState, and move saturating
107 instructions into helper.c */
108 uint32_t HELPER(add_setq
)(uint32_t a
, uint32_t b
)
110 uint32_t res
= a
+ b
;
111 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
))
116 uint32_t HELPER(add_saturate
)(uint32_t a
, uint32_t b
)
118 uint32_t res
= a
+ b
;
119 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
)) {
121 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
126 uint32_t HELPER(sub_saturate
)(uint32_t a
, uint32_t b
)
128 uint32_t res
= a
- b
;
129 if (((res
^ a
) & SIGNBIT
) && ((a
^ b
) & SIGNBIT
)) {
131 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
136 uint32_t HELPER(double_saturate
)(int32_t val
)
139 if (val
>= 0x40000000) {
142 } else if (val
<= (int32_t)0xc0000000) {
151 uint32_t HELPER(add_usaturate
)(uint32_t a
, uint32_t b
)
153 uint32_t res
= a
+ b
;
161 uint32_t HELPER(sub_usaturate
)(uint32_t a
, uint32_t b
)
163 uint32_t res
= a
- b
;
171 /* Signed saturation. */
172 static inline uint32_t do_ssat(int32_t val
, int shift
)
178 mask
= (1u << shift
) - 1;
182 } else if (top
< -1) {
189 /* Unsigned saturation. */
190 static inline uint32_t do_usat(int32_t val
, int shift
)
194 max
= (1u << shift
) - 1;
198 } else if (val
> max
) {
205 /* Signed saturate. */
206 uint32_t HELPER(ssat
)(uint32_t x
, uint32_t shift
)
208 return do_ssat(x
, shift
);
211 /* Dual halfword signed saturate. */
212 uint32_t HELPER(ssat16
)(uint32_t x
, uint32_t shift
)
216 res
= (uint16_t)do_ssat((int16_t)x
, shift
);
217 res
|= do_ssat(((int32_t)x
) >> 16, shift
) << 16;
221 /* Unsigned saturate. */
222 uint32_t HELPER(usat
)(uint32_t x
, uint32_t shift
)
224 return do_usat(x
, shift
);
227 /* Dual halfword unsigned saturate. */
228 uint32_t HELPER(usat16
)(uint32_t x
, uint32_t shift
)
232 res
= (uint16_t)do_usat((int16_t)x
, shift
);
233 res
|= do_usat(((int32_t)x
) >> 16, shift
) << 16;
237 void HELPER(wfi
)(void)
239 env
->exception_index
= EXCP_HLT
;
244 void HELPER(exception
)(uint32_t excp
)
246 env
->exception_index
= excp
;
250 uint32_t HELPER(cpsr_read
)(void)
252 return cpsr_read(env
) & ~CPSR_EXEC
;
255 void HELPER(cpsr_write
)(uint32_t val
, uint32_t mask
)
257 cpsr_write(env
, val
, mask
);
260 /* Access to user mode registers from privileged modes. */
261 uint32_t HELPER(get_user_reg
)(uint32_t regno
)
266 val
= env
->banked_r13
[0];
267 } else if (regno
== 14) {
268 val
= env
->banked_r14
[0];
269 } else if (regno
>= 8
270 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
271 val
= env
->usr_regs
[regno
- 8];
273 val
= env
->regs
[regno
];
278 void HELPER(set_user_reg
)(uint32_t regno
, uint32_t val
)
281 env
->banked_r13
[0] = val
;
282 } else if (regno
== 14) {
283 env
->banked_r14
[0] = val
;
284 } else if (regno
>= 8
285 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
286 env
->usr_regs
[regno
- 8] = val
;
288 env
->regs
[regno
] = val
;
292 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
293 The only way to do that in TCG is a conditional branch, which clobbers
294 all our temporaries. For now implement these as helper functions. */
296 uint32_t HELPER (add_cc
)(uint32_t a
, uint32_t b
)
300 env
->NF
= env
->ZF
= result
;
301 env
->CF
= result
< a
;
302 env
->VF
= (a
^ b
^ -1) & (a
^ result
);
306 uint32_t HELPER(adc_cc
)(uint32_t a
, uint32_t b
)
311 env
->CF
= result
< a
;
314 env
->CF
= result
<= a
;
316 env
->VF
= (a
^ b
^ -1) & (a
^ result
);
317 env
->NF
= env
->ZF
= result
;
321 uint32_t HELPER(sub_cc
)(uint32_t a
, uint32_t b
)
325 env
->NF
= env
->ZF
= result
;
327 env
->VF
= (a
^ b
) & (a
^ result
);
331 uint32_t HELPER(sbc_cc
)(uint32_t a
, uint32_t b
)
341 env
->VF
= (a
^ b
) & (a
^ result
);
342 env
->NF
= env
->ZF
= result
;
346 /* Similarly for variable shift instructions. */
348 uint32_t HELPER(shl
)(uint32_t x
, uint32_t i
)
350 int shift
= i
& 0xff;
356 uint32_t HELPER(shr
)(uint32_t x
, uint32_t i
)
358 int shift
= i
& 0xff;
361 return (uint32_t)x
>> shift
;
364 uint32_t HELPER(sar
)(uint32_t x
, uint32_t i
)
366 int shift
= i
& 0xff;
369 return (int32_t)x
>> shift
;
372 uint32_t HELPER(shl_cc
)(uint32_t x
, uint32_t i
)
374 int shift
= i
& 0xff;
381 } else if (shift
!= 0) {
382 env
->CF
= (x
>> (32 - shift
)) & 1;
388 uint32_t HELPER(shr_cc
)(uint32_t x
, uint32_t i
)
390 int shift
= i
& 0xff;
393 env
->CF
= (x
>> 31) & 1;
397 } else if (shift
!= 0) {
398 env
->CF
= (x
>> (shift
- 1)) & 1;
404 uint32_t HELPER(sar_cc
)(uint32_t x
, uint32_t i
)
406 int shift
= i
& 0xff;
408 env
->CF
= (x
>> 31) & 1;
409 return (int32_t)x
>> 31;
410 } else if (shift
!= 0) {
411 env
->CF
= (x
>> (shift
- 1)) & 1;
412 return (int32_t)x
>> shift
;
417 uint32_t HELPER(ror_cc
)(uint32_t x
, uint32_t i
)
421 shift
= shift1
& 0x1f;
424 env
->CF
= (x
>> 31) & 1;
427 env
->CF
= (x
>> (shift
- 1)) & 1;
428 return ((uint32_t)x
>> shift
) | (x
<< (32 - shift
));