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/>.
22 #define SIGNBIT (uint32_t)0x80000000
23 #define SIGNBIT64 ((uint64_t)1 << 63)
25 void raise_exception(int tt
)
27 env
->exception_index
= tt
;
33 static spinlock_t global_cpu_lock
= SPIN_LOCK_UNLOCKED
;
37 spin_lock(&global_cpu_lock
);
42 spin_unlock(&global_cpu_lock
);
45 uint32_t HELPER(neon_tbl
)(uint32_t ireg
, uint32_t def
,
46 uint32_t rn
, uint32_t maxindex
)
53 table
= (uint64_t *)&env
->vfp
.regs
[rn
];
55 for (shift
= 0; shift
< 32; shift
+= 8) {
56 index
= (ireg
>> shift
) & 0xff;
57 if (index
< maxindex
) {
58 tmp
= (table
[index
>> 3] >> ((index
& 7) << 3)) & 0xff;
61 val
|= def
& (0xff << shift
);
67 #if !defined(CONFIG_USER_ONLY)
69 #define MMUSUFFIX _mmu
72 #include "softmmu_template.h"
75 #include "softmmu_template.h"
78 #include "softmmu_template.h"
81 #include "softmmu_template.h"
83 /* try to fill the TLB and return an exception if error. If retaddr is
84 NULL, it means that the function was called in C code (i.e. not
85 from generated code or from helper.c) */
86 /* XXX: fix it to restore all registers */
87 void tlb_fill (target_ulong addr
, int is_write
, int mmu_idx
, void *retaddr
)
94 /* XXX: hack to restore env in all cases, even if not called from
98 ret
= cpu_arm_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
, 1);
101 /* now we have a real cpu fault */
102 pc
= (uintptr_t)retaddr
;
105 /* the PC is inside the translated code. It means that we have
106 a virtual CPU fault */
107 cpu_restore_state(tb
, env
, pc
, NULL
);
110 raise_exception(env
->exception_index
);
116 /* FIXME: Pass an axplicit pointer to QF to CPUState, and move saturating
117 instructions into helper.c */
118 uint32_t HELPER(add_setq
)(uint32_t a
, uint32_t b
)
120 uint32_t res
= a
+ b
;
121 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
))
126 uint32_t HELPER(add_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(sub_saturate
)(uint32_t a
, uint32_t b
)
138 uint32_t res
= a
- b
;
139 if (((res
^ a
) & SIGNBIT
) && ((a
^ b
) & SIGNBIT
)) {
141 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
146 uint32_t HELPER(double_saturate
)(int32_t val
)
149 if (val
>= 0x40000000) {
152 } else if (val
<= (int32_t)0xc0000000) {
161 uint32_t HELPER(add_usaturate
)(uint32_t a
, uint32_t b
)
163 uint32_t res
= a
+ b
;
171 uint32_t HELPER(sub_usaturate
)(uint32_t a
, uint32_t b
)
173 uint32_t res
= a
- b
;
181 /* Signed saturation. */
182 static inline uint32_t do_ssat(int32_t val
, int shift
)
188 mask
= (1u << shift
) - 1;
192 } else if (top
< -1) {
199 /* Unsigned saturation. */
200 static inline uint32_t do_usat(int32_t val
, int shift
)
204 max
= (1u << shift
) - 1;
208 } else if (val
> max
) {
215 /* Signed saturate. */
216 uint32_t HELPER(ssat
)(uint32_t x
, uint32_t shift
)
218 return do_ssat(x
, shift
);
221 /* Dual halfword signed saturate. */
222 uint32_t HELPER(ssat16
)(uint32_t x
, uint32_t shift
)
226 res
= (uint16_t)do_ssat((int16_t)x
, shift
);
227 res
|= do_ssat(((int32_t)x
) >> 16, shift
) << 16;
231 /* Unsigned saturate. */
232 uint32_t HELPER(usat
)(uint32_t x
, uint32_t shift
)
234 return do_usat(x
, shift
);
237 /* Dual halfword unsigned saturate. */
238 uint32_t HELPER(usat16
)(uint32_t x
, uint32_t shift
)
242 res
= (uint16_t)do_usat((int16_t)x
, shift
);
243 res
|= do_usat(((int32_t)x
) >> 16, shift
) << 16;
247 void HELPER(wfi
)(void)
249 env
->exception_index
= EXCP_HLT
;
254 void HELPER(exception
)(uint32_t excp
)
256 env
->exception_index
= excp
;
260 uint32_t HELPER(cpsr_read
)(void)
262 return cpsr_read(env
) & ~CPSR_EXEC
;
265 void HELPER(cpsr_write
)(uint32_t val
, uint32_t mask
)
267 cpsr_write(env
, val
, mask
);
270 /* Access to user mode registers from privileged modes. */
271 uint32_t HELPER(get_user_reg
)(uint32_t regno
)
276 val
= env
->banked_r13
[0];
277 } else if (regno
== 14) {
278 val
= env
->banked_r14
[0];
279 } else if (regno
>= 8
280 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
281 val
= env
->usr_regs
[regno
- 8];
283 val
= env
->regs
[regno
];
288 void HELPER(set_user_reg
)(uint32_t regno
, uint32_t val
)
291 env
->banked_r13
[0] = val
;
292 } else if (regno
== 14) {
293 env
->banked_r14
[0] = val
;
294 } else if (regno
>= 8
295 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
296 env
->usr_regs
[regno
- 8] = val
;
298 env
->regs
[regno
] = val
;
302 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
303 The only way to do that in TCG is a conditional branch, which clobbers
304 all our temporaries. For now implement these as helper functions. */
306 uint32_t HELPER (add_cc
)(uint32_t a
, uint32_t b
)
310 env
->NF
= env
->ZF
= result
;
311 env
->CF
= result
< a
;
312 env
->VF
= (a
^ b
^ -1) & (a
^ result
);
316 uint32_t HELPER(adc_cc
)(uint32_t a
, uint32_t b
)
321 env
->CF
= result
< a
;
324 env
->CF
= result
<= a
;
326 env
->VF
= (a
^ b
^ -1) & (a
^ result
);
327 env
->NF
= env
->ZF
= result
;
331 uint32_t HELPER(sub_cc
)(uint32_t a
, uint32_t b
)
335 env
->NF
= env
->ZF
= result
;
337 env
->VF
= (a
^ b
) & (a
^ result
);
341 uint32_t HELPER(sbc_cc
)(uint32_t a
, uint32_t b
)
351 env
->VF
= (a
^ b
) & (a
^ result
);
352 env
->NF
= env
->ZF
= result
;
356 /* Similarly for variable shift instructions. */
358 uint32_t HELPER(shl
)(uint32_t x
, uint32_t i
)
360 int shift
= i
& 0xff;
366 uint32_t HELPER(shr
)(uint32_t x
, uint32_t i
)
368 int shift
= i
& 0xff;
371 return (uint32_t)x
>> shift
;
374 uint32_t HELPER(sar
)(uint32_t x
, uint32_t i
)
376 int shift
= i
& 0xff;
379 return (int32_t)x
>> shift
;
382 uint32_t HELPER(ror
)(uint32_t x
, uint32_t i
)
384 int shift
= i
& 0xff;
387 return (x
>> shift
) | (x
<< (32 - shift
));
390 uint32_t HELPER(shl_cc
)(uint32_t x
, uint32_t i
)
392 int shift
= i
& 0xff;
399 } else if (shift
!= 0) {
400 env
->CF
= (x
>> (32 - shift
)) & 1;
406 uint32_t HELPER(shr_cc
)(uint32_t x
, uint32_t i
)
408 int shift
= i
& 0xff;
411 env
->CF
= (x
>> 31) & 1;
415 } else if (shift
!= 0) {
416 env
->CF
= (x
>> (shift
- 1)) & 1;
422 uint32_t HELPER(sar_cc
)(uint32_t x
, uint32_t i
)
424 int shift
= i
& 0xff;
426 env
->CF
= (x
>> 31) & 1;
427 return (int32_t)x
>> 31;
428 } else if (shift
!= 0) {
429 env
->CF
= (x
>> (shift
- 1)) & 1;
430 return (int32_t)x
>> shift
;
435 uint32_t HELPER(ror_cc
)(uint32_t x
, uint32_t i
)
439 shift
= shift1
& 0x1f;
442 env
->CF
= (x
>> 31) & 1;
445 env
->CF
= (x
>> (shift
- 1)) & 1;
446 return ((uint32_t)x
>> shift
) | (x
<< (32 - shift
));
450 uint64_t HELPER(neon_add_saturate_s64
)(uint64_t src1
, uint64_t src2
)
455 if (((res
^ src1
) & SIGNBIT64
) && !((src1
^ src2
) & SIGNBIT64
)) {
457 res
= ((int64_t)src1
>> 63) ^ ~SIGNBIT64
;
462 uint64_t HELPER(neon_add_saturate_u64
)(uint64_t src1
, uint64_t src2
)
474 uint64_t HELPER(neon_sub_saturate_s64
)(uint64_t src1
, uint64_t src2
)
479 if (((res
^ src1
) & SIGNBIT64
) && ((src1
^ src2
) & SIGNBIT64
)) {
481 res
= ((int64_t)src1
>> 63) ^ ~SIGNBIT64
;
486 uint64_t HELPER(neon_sub_saturate_u64
)(uint64_t src1
, uint64_t src2
)