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
,
87 ret
= cpu_arm_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
);
90 /* now we have a real cpu fault */
91 pc
= (unsigned long)retaddr
;
94 /* the PC is inside the translated code. It means that we have
95 a virtual CPU fault */
96 cpu_restore_state(tb
, env
, pc
);
99 raise_exception(env
->exception_index
);
105 /* FIXME: Pass an axplicit pointer to QF to CPUState, and move saturating
106 instructions into helper.c */
107 uint32_t HELPER(add_setq
)(uint32_t a
, uint32_t b
)
109 uint32_t res
= a
+ b
;
110 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
))
115 uint32_t HELPER(add_saturate
)(uint32_t a
, uint32_t b
)
117 uint32_t res
= a
+ b
;
118 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
)) {
120 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
125 uint32_t HELPER(sub_saturate
)(uint32_t a
, uint32_t b
)
127 uint32_t res
= a
- b
;
128 if (((res
^ a
) & SIGNBIT
) && ((a
^ b
) & SIGNBIT
)) {
130 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
135 uint32_t HELPER(double_saturate
)(int32_t val
)
138 if (val
>= 0x40000000) {
141 } else if (val
<= (int32_t)0xc0000000) {
150 uint32_t HELPER(add_usaturate
)(uint32_t a
, uint32_t b
)
152 uint32_t res
= a
+ b
;
160 uint32_t HELPER(sub_usaturate
)(uint32_t a
, uint32_t b
)
162 uint32_t res
= a
- b
;
170 /* Signed saturation. */
171 static inline uint32_t do_ssat(int32_t val
, int shift
)
177 mask
= (1u << shift
) - 1;
181 } else if (top
< -1) {
188 /* Unsigned saturation. */
189 static inline uint32_t do_usat(int32_t val
, int shift
)
193 max
= (1u << shift
) - 1;
197 } else if (val
> max
) {
204 /* Signed saturate. */
205 uint32_t HELPER(ssat
)(uint32_t x
, uint32_t shift
)
207 return do_ssat(x
, shift
);
210 /* Dual halfword signed saturate. */
211 uint32_t HELPER(ssat16
)(uint32_t x
, uint32_t shift
)
215 res
= (uint16_t)do_ssat((int16_t)x
, shift
);
216 res
|= do_ssat(((int32_t)x
) >> 16, shift
) << 16;
220 /* Unsigned saturate. */
221 uint32_t HELPER(usat
)(uint32_t x
, uint32_t shift
)
223 return do_usat(x
, shift
);
226 /* Dual halfword unsigned saturate. */
227 uint32_t HELPER(usat16
)(uint32_t x
, uint32_t shift
)
231 res
= (uint16_t)do_usat((int16_t)x
, shift
);
232 res
|= do_usat(((int32_t)x
) >> 16, shift
) << 16;
236 void HELPER(wfi
)(void)
238 env
->exception_index
= EXCP_HLT
;
243 void HELPER(exception
)(uint32_t excp
)
245 env
->exception_index
= excp
;
249 uint32_t HELPER(cpsr_read
)(void)
251 return cpsr_read(env
) & ~CPSR_EXEC
;
254 void HELPER(cpsr_write
)(uint32_t val
, uint32_t mask
)
256 cpsr_write(env
, val
, mask
);
259 /* Access to user mode registers from privileged modes. */
260 uint32_t HELPER(get_user_reg
)(uint32_t regno
)
265 val
= env
->banked_r13
[0];
266 } else if (regno
== 14) {
267 val
= env
->banked_r14
[0];
268 } else if (regno
>= 8
269 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
270 val
= env
->usr_regs
[regno
- 8];
272 val
= env
->regs
[regno
];
277 void HELPER(set_user_reg
)(uint32_t regno
, uint32_t val
)
280 env
->banked_r13
[0] = val
;
281 } else if (regno
== 14) {
282 env
->banked_r14
[0] = val
;
283 } else if (regno
>= 8
284 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
285 env
->usr_regs
[regno
- 8] = val
;
287 env
->regs
[regno
] = val
;
291 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
292 The only way to do that in TCG is a conditional branch, which clobbers
293 all our temporaries. For now implement these as helper functions. */
295 uint32_t HELPER (add_cc
)(uint32_t a
, uint32_t b
)
299 env
->NF
= env
->ZF
= result
;
300 env
->CF
= result
< a
;
301 env
->VF
= (a
^ b
^ -1) & (a
^ result
);
305 uint32_t HELPER(adc_cc
)(uint32_t a
, uint32_t b
)
310 env
->CF
= result
< a
;
313 env
->CF
= result
<= a
;
315 env
->VF
= (a
^ b
^ -1) & (a
^ result
);
316 env
->NF
= env
->ZF
= result
;
320 uint32_t HELPER(sub_cc
)(uint32_t a
, uint32_t b
)
324 env
->NF
= env
->ZF
= result
;
326 env
->VF
= (a
^ b
) & (a
^ result
);
330 uint32_t HELPER(sbc_cc
)(uint32_t a
, uint32_t b
)
340 env
->VF
= (a
^ b
) & (a
^ result
);
341 env
->NF
= env
->ZF
= result
;
345 /* Similarly for variable shift instructions. */
347 uint32_t HELPER(shl
)(uint32_t x
, uint32_t i
)
349 int shift
= i
& 0xff;
355 uint32_t HELPER(shr
)(uint32_t x
, uint32_t i
)
357 int shift
= i
& 0xff;
360 return (uint32_t)x
>> shift
;
363 uint32_t HELPER(sar
)(uint32_t x
, uint32_t i
)
365 int shift
= i
& 0xff;
368 return (int32_t)x
>> shift
;
371 uint32_t HELPER(shl_cc
)(uint32_t x
, uint32_t i
)
373 int shift
= i
& 0xff;
380 } else if (shift
!= 0) {
381 env
->CF
= (x
>> (32 - shift
)) & 1;
387 uint32_t HELPER(shr_cc
)(uint32_t x
, uint32_t i
)
389 int shift
= i
& 0xff;
392 env
->CF
= (x
>> 31) & 1;
396 } else if (shift
!= 0) {
397 env
->CF
= (x
>> (shift
- 1)) & 1;
403 uint32_t HELPER(sar_cc
)(uint32_t x
, uint32_t i
)
405 int shift
= i
& 0xff;
407 env
->CF
= (x
>> 31) & 1;
408 return (int32_t)x
>> 31;
409 } else if (shift
!= 0) {
410 env
->CF
= (x
>> (shift
- 1)) & 1;
411 return (int32_t)x
>> shift
;
416 uint32_t HELPER(ror_cc
)(uint32_t x
, uint32_t i
)
420 shift
= shift1
& 0x1f;
423 env
->CF
= (x
>> 31) & 1;
426 env
->CF
= (x
>> (shift
- 1)) & 1;
427 return ((uint32_t)x
>> shift
) | (x
<< (32 - shift
));