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 ARMCPU
*cpu
= arm_env_get_cpu(env
);
28 CPUState
*cs
= CPU(cpu
);
30 cs
->exception_index
= tt
;
34 uint32_t HELPER(neon_tbl
)(CPUARMState
*env
, 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 "exec/softmmu_exec.h"
60 #define MMUSUFFIX _mmu
63 #include "exec/softmmu_template.h"
66 #include "exec/softmmu_template.h"
69 #include "exec/softmmu_template.h"
72 #include "exec/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)
78 void tlb_fill(CPUState
*cs
, target_ulong addr
, int is_write
, int mmu_idx
,
83 ret
= arm_cpu_handle_mmu_fault(cs
, addr
, is_write
, mmu_idx
);
85 ARMCPU
*cpu
= ARM_CPU(cs
);
86 CPUARMState
*env
= &cpu
->env
;
89 /* now we have a real cpu fault */
90 cpu_restore_state(cs
, retaddr
);
92 raise_exception(env
, cs
->exception_index
);
97 uint32_t HELPER(add_setq
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
100 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
))
105 uint32_t HELPER(add_saturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
107 uint32_t res
= a
+ b
;
108 if (((res
^ a
) & SIGNBIT
) && !((a
^ b
) & SIGNBIT
)) {
110 res
= ~(((int32_t)a
>> 31) ^ SIGNBIT
);
115 uint32_t HELPER(sub_saturate
)(CPUARMState
*env
, 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(double_saturate
)(CPUARMState
*env
, int32_t val
)
128 if (val
>= 0x40000000) {
131 } else if (val
<= (int32_t)0xc0000000) {
140 uint32_t HELPER(add_usaturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
142 uint32_t res
= a
+ b
;
150 uint32_t HELPER(sub_usaturate
)(CPUARMState
*env
, uint32_t a
, uint32_t b
)
152 uint32_t res
= a
- b
;
160 /* Signed saturation. */
161 static inline uint32_t do_ssat(CPUARMState
*env
, int32_t val
, int shift
)
167 mask
= (1u << shift
) - 1;
171 } else if (top
< -1) {
178 /* Unsigned saturation. */
179 static inline uint32_t do_usat(CPUARMState
*env
, int32_t val
, int shift
)
183 max
= (1u << shift
) - 1;
187 } else if (val
> max
) {
194 /* Signed saturate. */
195 uint32_t HELPER(ssat
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
197 return do_ssat(env
, x
, shift
);
200 /* Dual halfword signed saturate. */
201 uint32_t HELPER(ssat16
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
205 res
= (uint16_t)do_ssat(env
, (int16_t)x
, shift
);
206 res
|= do_ssat(env
, ((int32_t)x
) >> 16, shift
) << 16;
210 /* Unsigned saturate. */
211 uint32_t HELPER(usat
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
213 return do_usat(env
, x
, shift
);
216 /* Dual halfword unsigned saturate. */
217 uint32_t HELPER(usat16
)(CPUARMState
*env
, uint32_t x
, uint32_t shift
)
221 res
= (uint16_t)do_usat(env
, (int16_t)x
, shift
);
222 res
|= do_usat(env
, ((int32_t)x
) >> 16, shift
) << 16;
226 void HELPER(wfi
)(CPUARMState
*env
)
228 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
230 cs
->exception_index
= EXCP_HLT
;
235 void HELPER(wfe
)(CPUARMState
*env
)
237 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
239 /* Don't actually halt the CPU, just yield back to top
242 cs
->exception_index
= EXCP_YIELD
;
246 void HELPER(exception
)(CPUARMState
*env
, uint32_t excp
)
248 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
250 cs
->exception_index
= excp
;
254 uint32_t HELPER(cpsr_read
)(CPUARMState
*env
)
256 return cpsr_read(env
) & ~CPSR_EXEC
;
259 void HELPER(cpsr_write
)(CPUARMState
*env
, uint32_t val
, uint32_t mask
)
261 cpsr_write(env
, val
, mask
);
264 /* Access to user mode registers from privileged modes. */
265 uint32_t HELPER(get_user_reg
)(CPUARMState
*env
, uint32_t regno
)
270 val
= env
->banked_r13
[0];
271 } else if (regno
== 14) {
272 val
= env
->banked_r14
[0];
273 } else if (regno
>= 8
274 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
275 val
= env
->usr_regs
[regno
- 8];
277 val
= env
->regs
[regno
];
282 void HELPER(set_user_reg
)(CPUARMState
*env
, uint32_t regno
, uint32_t val
)
285 env
->banked_r13
[0] = val
;
286 } else if (regno
== 14) {
287 env
->banked_r14
[0] = val
;
288 } else if (regno
>= 8
289 && (env
->uncached_cpsr
& 0x1f) == ARM_CPU_MODE_FIQ
) {
290 env
->usr_regs
[regno
- 8] = val
;
292 env
->regs
[regno
] = val
;
296 void HELPER(access_check_cp_reg
)(CPUARMState
*env
, void *rip
)
298 const ARMCPRegInfo
*ri
= rip
;
299 switch (ri
->accessfn(env
, ri
)) {
303 case CP_ACCESS_TRAP_UNCATEGORIZED
:
304 /* These cases will eventually need to generate different
305 * syndrome information.
309 g_assert_not_reached();
311 raise_exception(env
, EXCP_UDEF
);
314 void HELPER(set_cp_reg
)(CPUARMState
*env
, void *rip
, uint32_t value
)
316 const ARMCPRegInfo
*ri
= rip
;
318 ri
->writefn(env
, ri
, value
);
321 uint32_t HELPER(get_cp_reg
)(CPUARMState
*env
, void *rip
)
323 const ARMCPRegInfo
*ri
= rip
;
325 return ri
->readfn(env
, ri
);
328 void HELPER(set_cp_reg64
)(CPUARMState
*env
, void *rip
, uint64_t value
)
330 const ARMCPRegInfo
*ri
= rip
;
332 ri
->writefn(env
, ri
, value
);
335 uint64_t HELPER(get_cp_reg64
)(CPUARMState
*env
, void *rip
)
337 const ARMCPRegInfo
*ri
= rip
;
339 return ri
->readfn(env
, ri
);
342 void HELPER(msr_i_pstate
)(CPUARMState
*env
, uint32_t op
, uint32_t imm
)
344 /* MSR_i to update PSTATE. This is OK from EL0 only if UMA is set.
345 * Note that SPSel is never OK from EL0; we rely on handle_msr_i()
346 * to catch that case at translate time.
348 if (arm_current_pl(env
) == 0 && !(env
->cp15
.c1_sys
& SCTLR_UMA
)) {
349 raise_exception(env
, EXCP_UDEF
);
353 case 0x05: /* SPSel */
354 env
->pstate
= deposit32(env
->pstate
, 0, 1, imm
);
356 case 0x1e: /* DAIFSet */
357 env
->daif
|= (imm
<< 6) & PSTATE_DAIF
;
359 case 0x1f: /* DAIFClear */
360 env
->daif
&= ~((imm
<< 6) & PSTATE_DAIF
);
363 g_assert_not_reached();
367 /* ??? Flag setting arithmetic is awkward because we need to do comparisons.
368 The only way to do that in TCG is a conditional branch, which clobbers
369 all our temporaries. For now implement these as helper functions. */
371 /* Similarly for variable shift instructions. */
373 uint32_t HELPER(shl_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
375 int shift
= i
& 0xff;
382 } else if (shift
!= 0) {
383 env
->CF
= (x
>> (32 - shift
)) & 1;
389 uint32_t HELPER(shr_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
391 int shift
= i
& 0xff;
394 env
->CF
= (x
>> 31) & 1;
398 } else if (shift
!= 0) {
399 env
->CF
= (x
>> (shift
- 1)) & 1;
405 uint32_t HELPER(sar_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
407 int shift
= i
& 0xff;
409 env
->CF
= (x
>> 31) & 1;
410 return (int32_t)x
>> 31;
411 } else if (shift
!= 0) {
412 env
->CF
= (x
>> (shift
- 1)) & 1;
413 return (int32_t)x
>> shift
;
418 uint32_t HELPER(ror_cc
)(CPUARMState
*env
, uint32_t x
, uint32_t i
)
422 shift
= shift1
& 0x1f;
425 env
->CF
= (x
>> 31) & 1;
428 env
->CF
= (x
>> (shift
- 1)) & 1;
429 return ((uint32_t)x
>> shift
) | (x
<< (32 - shift
));