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 static void raise_exception(CPUARMState
*env
, int tt
)
27 env
->exception_index
= tt
;
31 uint32_t HELPER(neon_tbl
)(CPUARMState
*env
, uint32_t ireg
, uint32_t def
,
32 uint32_t rn
, uint32_t maxindex
)
39 table
= (uint64_t *)&env
->vfp
.regs
[rn
];
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;
47 val
|= def
& (0xff << shift
);
53 #if !defined(CONFIG_USER_ONLY)
55 #include "softmmu_exec.h"
57 #define MMUSUFFIX _mmu
60 #include "softmmu_template.h"
63 #include "softmmu_template.h"
66 #include "softmmu_template.h"
69 #include "softmmu_template.h"
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
,
80 ret
= cpu_arm_handle_mmu_fault(env
, addr
, is_write
, mmu_idx
);
83 /* now we have a real cpu fault */
84 tb
= tb_find_pc(retaddr
);
86 /* the PC is inside the translated code. It means that we have
87 a virtual CPU fault */
88 cpu_restore_state(tb
, env
, retaddr
);
91 raise_exception(env
, env
->exception_index
);
96 /* FIXME: Pass an explicit pointer to QF to CPUARMState, and move saturating
97 instructions into helper.c */
98 uint32_t HELPER(add_setq
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
100 uint32_t res
= a
+ b
;
101 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
))
106 uint32_t HELPER(add_saturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
108 uint32_t res
= a
+ b
;
109 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
)) {
111 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
116 uint32_t HELPER(sub_saturate
)(CPUARMState
*env
, 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(double_saturate
)(CPUARMState
*env
, int32_t val
)
129 if (val
>= 0x40000000) {
132 } else if (val
<= (int32_t)0xc0000000) {
141 uint32_t HELPER(add_usaturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
143 uint32_t res
= a
+ b
;
151 uint32_t HELPER(sub_usaturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
153 uint32_t res
= a
- b
;
161 /* Signed saturation. */
162 static inline uint32_t do_ssat(CPUARMState
*env
, int32_t val
, int shift
)
168 mask
= (1u << shift
) - 1;
172 } else if (top
< -1) {
179 /* Unsigned saturation. */
180 static inline uint32_t do_usat(CPUARMState
*env
, int32_t val
, int shift
)
184 max
= (1u << shift
) - 1;
188 } else if (val
> max
) {
195 /* Signed saturate. */
196 uint32_t HELPER(ssat
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
198 return do_ssat(env
, x
, shift
);
201 /* Dual halfword signed saturate. */
202 uint32_t HELPER(ssat16
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
206 res
= (uint16_t)do_ssat(env
, (int16_t)x
, shift
);
207 res
|= do_ssat(env
, ((int32_t)x
) >> 16, shift
) << 16;
211 /* Unsigned saturate. */
212 uint32_t HELPER(usat
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
214 return do_usat(env
, x
, shift
);
217 /* Dual halfword unsigned saturate. */
218 uint32_t HELPER(usat16
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
222 res
= (uint16_t)do_usat(env
, (int16_t)x
, shift
);
223 res
|= do_usat(env
, ((int32_t)x
) >> 16, shift
) << 16;
227 void HELPER(wfi
)(CPUARMState
*env
)
229 env
->exception_index
= EXCP_HLT
;
234 void HELPER(exception
)(CPUARMState
*env
, uint32_t excp
)
236 env
->exception_index
= excp
;
240 uint32_t HELPER(cpsr_read
)(CPUARMState
*env
)
242 return cpsr_read(env
) & ~CPSR_EXEC
;
245 void HELPER(cpsr_write
)(CPUARMState
*env
, uint32_t val
, uint32_t mask
)
247 cpsr_write(env
, val
, mask
);
250 /* Access to user mode registers from privileged modes. */
251 uint32_t HELPER(get_user_reg
)(CPUARMState
*env
, uint32_t regno
)
256 val
= env
->banked_r13
[0];
257 } else if (regno
== 14) {
258 val
= env
->banked_r14
[0];
259 } else if (regno
>= 8
260 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
261 val
= env
->usr_regs
[regno
- 8];
263 val
= env
->regs
[regno
];
268 void HELPER(set_user_reg
)(CPUARMState
*env
, uint32_t regno
, uint32_t val
)
271 env
->banked_r13
[0] = val
;
272 } else if (regno
== 14) {
273 env
->banked_r14
[0] = val
;
274 } else if (regno
>= 8
275 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
276 env
->usr_regs
[regno
- 8] = val
;
278 env
->regs
[regno
] = val
;
282 void HELPER(set_cp_reg
)(CPUARMState
*env
, void *rip
, uint32_t value
)
284 const ARMCPRegInfo
*ri
= rip
;
285 int excp
= ri
->writefn(env
, ri
, value
);
287 raise_exception(env
, excp
);
291 uint32_t HELPER(get_cp_reg
)(CPUARMState
*env
, void *rip
)
293 const ARMCPRegInfo
*ri
= rip
;
295 int excp
= ri
->readfn(env
, ri
, &value
);
297 raise_exception(env
, excp
);
302 void HELPER(set_cp_reg64
)(CPUARMState
*env
, void *rip
, uint64_t value
)
304 const ARMCPRegInfo
*ri
= rip
;
305 int excp
= ri
->writefn(env
, ri
, value
);
307 raise_exception(env
, excp
);
311 uint64_t HELPER(get_cp_reg64
)(CPUARMState
*env
, void *rip
)
313 const ARMCPRegInfo
*ri
= rip
;
315 int excp
= ri
->readfn(env
, ri
, &value
);
317 raise_exception(env
, excp
);
322 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
323 The only way to do that in TCG is a conditional branch, which clobbers
324 all our temporaries. For now implement these as helper functions. */
326 uint32_t HELPER(adc_cc
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
331 env
->CF
= result
< a
;
334 env
->CF
= result
<= a
;
336 env
->VF
= (a
^ b
^ -1) & (a
^ result
);
337 env
->NF
= env
->ZF
= result
;
341 uint32_t HELPER(sbc_cc
)(CPUARMState
*env
, 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_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
360 int shift
= i
& 0xff;
367 } else if (shift
!= 0) {
368 env
->CF
= (x
>> (32 - shift
)) & 1;
374 uint32_t HELPER(shr_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
376 int shift
= i
& 0xff;
379 env
->CF
= (x
>> 31) & 1;
383 } else if (shift
!= 0) {
384 env
->CF
= (x
>> (shift
- 1)) & 1;
390 uint32_t HELPER(sar_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
392 int shift
= i
& 0xff;
394 env
->CF
= (x
>> 31) & 1;
395 return (int32_t)x
>> 31;
396 } else if (shift
!= 0) {
397 env
->CF
= (x
>> (shift
- 1)) & 1;
398 return (int32_t)x
>> shift
;
403 uint32_t HELPER(ror_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
407 shift
= shift1
& 0x1f;
410 env
->CF
= (x
>> 31) & 1;
413 env
->CF
= (x
>> (shift
- 1)) & 1;
414 return ((uint32_t)x
>> shift
) | (x
<< (32 - shift
));