4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
24 #include "cpu_loop-common.h"
25 #include "hw/semihosting/common-semi.h"
27 #define get_user_code_u32(x, gaddr, env) \
28 ({ abi_long __r = get_user_u32((x), (gaddr)); \
29 if (!__r && bswap_code(arm_sctlr_b(env))) { \
35 #define get_user_code_u16(x, gaddr, env) \
36 ({ abi_long __r = get_user_u16((x), (gaddr)); \
37 if (!__r && bswap_code(arm_sctlr_b(env))) { \
43 #define get_user_data_u32(x, gaddr, env) \
44 ({ abi_long __r = get_user_u32((x), (gaddr)); \
45 if (!__r && arm_cpu_bswap_data(env)) { \
51 #define get_user_data_u16(x, gaddr, env) \
52 ({ abi_long __r = get_user_u16((x), (gaddr)); \
53 if (!__r && arm_cpu_bswap_data(env)) { \
59 #define put_user_data_u32(x, gaddr, env) \
60 ({ typeof(x) __x = (x); \
61 if (arm_cpu_bswap_data(env)) { \
64 put_user_u32(__x, (gaddr)); \
67 #define put_user_data_u16(x, gaddr, env) \
68 ({ typeof(x) __x = (x); \
69 if (arm_cpu_bswap_data(env)) { \
72 put_user_u16(__x, (gaddr)); \
75 /* Commpage handling -- there is no commpage for AArch64 */
78 * See the Linux kernel's Documentation/arm/kernel_user_helpers.txt
80 * r0 = pointer to oldval
81 * r1 = pointer to newval
82 * r2 = pointer to target value
85 * r0 = 0 if *ptr was changed, non-0 if no exchange happened
86 * C set if *ptr was changed, clear if no exchange happened
88 * Note segv's in kernel helpers are a bit tricky, we can set the
89 * data address sensibly but the PC address is just the entry point.
91 static void arm_kernel_cmpxchg64_helper(CPUARMState
*env
)
93 uint64_t oldval
, newval
, val
;
95 target_siginfo_t info
;
97 /* Based on the 32 bit code in do_kernel_trap */
99 /* XXX: This only works between threads, not between processes.
100 It's probably possible to implement this with native host
101 operations. However things like ldrex/strex are much harder so
102 there's not much point trying. */
104 cpsr
= cpsr_read(env
);
107 if (get_user_u64(oldval
, env
->regs
[0])) {
108 env
->exception
.vaddress
= env
->regs
[0];
112 if (get_user_u64(newval
, env
->regs
[1])) {
113 env
->exception
.vaddress
= env
->regs
[1];
117 if (get_user_u64(val
, addr
)) {
118 env
->exception
.vaddress
= addr
;
125 if (put_user_u64(val
, addr
)) {
126 env
->exception
.vaddress
= addr
;
136 cpsr_write(env
, cpsr
, CPSR_C
, CPSRWriteByInstr
);
142 /* We get the PC of the entry address - which is as good as anything,
143 on a real kernel what you get depends on which mode it uses. */
144 info
.si_signo
= TARGET_SIGSEGV
;
146 /* XXX: check env->error_code */
147 info
.si_code
= TARGET_SEGV_MAPERR
;
148 info
._sifields
._sigfault
._addr
= env
->exception
.vaddress
;
149 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
152 /* Handle a jump to the kernel code page. */
154 do_kernel_trap(CPUARMState
*env
)
160 switch (env
->regs
[15]) {
161 case 0xffff0fa0: /* __kernel_memory_barrier */
162 /* ??? No-op. Will need to do better for SMP. */
164 case 0xffff0fc0: /* __kernel_cmpxchg */
165 /* XXX: This only works between threads, not between processes.
166 It's probably possible to implement this with native host
167 operations. However things like ldrex/strex are much harder so
168 there's not much point trying. */
170 cpsr
= cpsr_read(env
);
172 /* FIXME: This should SEGV if the access fails. */
173 if (get_user_u32(val
, addr
))
175 if (val
== env
->regs
[0]) {
177 /* FIXME: Check for segfaults. */
178 put_user_u32(val
, addr
);
185 cpsr_write(env
, cpsr
, CPSR_C
, CPSRWriteByInstr
);
188 case 0xffff0fe0: /* __kernel_get_tls */
189 env
->regs
[0] = cpu_get_tls(env
);
191 case 0xffff0f60: /* __kernel_cmpxchg64 */
192 arm_kernel_cmpxchg64_helper(env
);
198 /* Jump back to the caller. */
199 addr
= env
->regs
[14];
204 env
->regs
[15] = addr
;
209 static bool insn_is_linux_bkpt(uint32_t opcode
, bool is_thumb
)
212 * Return true if this insn is one of the three magic UDF insns
213 * which the kernel treats as breakpoint insns.
216 return (opcode
& 0x0fffffff) == 0x07f001f0;
219 * Note that we get the two halves of the 32-bit T32 insn
220 * in the opposite order to the value the kernel uses in
221 * its undef_hook struct.
223 return ((opcode
& 0xffff) == 0xde01) || (opcode
== 0xa000f7f0);
227 void cpu_loop(CPUARMState
*env
)
229 CPUState
*cs
= env_cpu(env
);
231 unsigned int n
, insn
;
232 target_siginfo_t info
;
238 trapnr
= cpu_exec(cs
);
240 process_queued_cpu_work(cs
);
247 TaskState
*ts
= cs
->opaque
;
251 /* we handle the FPU emulation here, as Linux */
252 /* we get the opcode */
253 /* FIXME - what to do if get_user() fails? */
254 get_user_code_u32(opcode
, env
->regs
[15], env
);
257 * The Linux kernel treats some UDF patterns specially
258 * to use as breakpoints (instead of the architectural
259 * bkpt insn). These should trigger a SIGTRAP rather
262 if (insn_is_linux_bkpt(opcode
, env
->thumb
)) {
266 rc
= EmulateAll(opcode
, &ts
->fpa
, env
);
267 if (rc
== 0) { /* illegal instruction */
268 info
.si_signo
= TARGET_SIGILL
;
270 info
.si_code
= TARGET_ILL_ILLOPN
;
271 info
._sifields
._sigfault
._addr
= env
->regs
[15];
272 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
273 } else if (rc
< 0) { /* FP exception */
276 /* translate softfloat flags to FPSR flags */
277 if (-rc
& float_flag_invalid
)
279 if (-rc
& float_flag_divbyzero
)
281 if (-rc
& float_flag_overflow
)
283 if (-rc
& float_flag_underflow
)
285 if (-rc
& float_flag_inexact
)
288 FPSR fpsr
= ts
->fpa
.fpsr
;
289 //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
291 if (fpsr
& (arm_fpe
<< 16)) { /* exception enabled? */
292 info
.si_signo
= TARGET_SIGFPE
;
295 /* ordered by priority, least first */
296 if (arm_fpe
& BIT_IXC
) info
.si_code
= TARGET_FPE_FLTRES
;
297 if (arm_fpe
& BIT_UFC
) info
.si_code
= TARGET_FPE_FLTUND
;
298 if (arm_fpe
& BIT_OFC
) info
.si_code
= TARGET_FPE_FLTOVF
;
299 if (arm_fpe
& BIT_DZC
) info
.si_code
= TARGET_FPE_FLTDIV
;
300 if (arm_fpe
& BIT_IOC
) info
.si_code
= TARGET_FPE_FLTINV
;
302 info
._sifields
._sigfault
._addr
= env
->regs
[15];
303 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
308 /* accumulate unenabled exceptions */
309 if ((!(fpsr
& BIT_IXE
)) && (arm_fpe
& BIT_IXC
))
311 if ((!(fpsr
& BIT_UFE
)) && (arm_fpe
& BIT_UFC
))
313 if ((!(fpsr
& BIT_OFE
)) && (arm_fpe
& BIT_OFC
))
315 if ((!(fpsr
& BIT_DZE
)) && (arm_fpe
& BIT_DZC
))
317 if ((!(fpsr
& BIT_IOE
)) && (arm_fpe
& BIT_IOC
))
320 } else { /* everything OK */
331 /* Thumb is always EABI style with syscall number in r7 */
335 * Equivalent of kernel CONFIG_OABI_COMPAT: read the
336 * Arm SVC insn to extract the immediate, which is the
337 * syscall number in OABI.
339 /* FIXME - what to do if get_user() fails? */
340 get_user_code_u32(insn
, env
->regs
[15] - 4, env
);
343 /* zero immediate: EABI, syscall number in r7 */
347 * This XOR matches the kernel code: an immediate
348 * in the valid range (0x900000 .. 0x9fffff) is
349 * converted into the correct EABI-style syscall
350 * number; invalid immediates end up as values
351 * > 0xfffff and are handled below as out-of-range.
353 n
^= ARM_SYSCALL_BASE
;
358 if (n
> ARM_NR_BASE
) {
360 case ARM_NR_cacheflush
:
364 cpu_set_tls(env
, env
->regs
[0]);
367 case ARM_NR_breakpoint
:
368 env
->regs
[15] -= env
->thumb
? 2 : 4;
371 env
->regs
[0] = cpu_get_tls(env
);
376 * Syscalls 0xf0000..0xf07ff (or 0x9f0000..
377 * 0x9f07ff in OABI numbering) are defined
378 * to return -ENOSYS rather than raising
379 * SIGILL. Note that we have already
380 * removed the 0x900000 prefix.
382 qemu_log_mask(LOG_UNIMP
,
383 "qemu: Unsupported ARM syscall: 0x%x\n",
385 env
->regs
[0] = -TARGET_ENOSYS
;
388 * Otherwise SIGILL. This includes any SWI with
389 * immediate not originally 0x9fxxxx, because
390 * of the earlier XOR.
392 info
.si_signo
= TARGET_SIGILL
;
394 info
.si_code
= TARGET_ILL_ILLTRP
;
395 info
._sifields
._sigfault
._addr
= env
->regs
[15];
397 info
._sifields
._sigfault
._addr
-= 2;
399 info
._sifields
._sigfault
._addr
-= 4;
401 queue_signal(env
, info
.si_signo
,
402 QEMU_SI_FAULT
, &info
);
407 ret
= do_syscall(env
,
416 if (ret
== -TARGET_ERESTARTSYS
) {
417 env
->regs
[15] -= env
->thumb
? 2 : 4;
418 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
425 env
->regs
[0] = do_common_semihosting(cs
);
426 env
->regs
[15] += env
->thumb
? 2 : 4;
429 /* just indicate that signals should be handled asap */
431 case EXCP_PREFETCH_ABORT
:
432 case EXCP_DATA_ABORT
:
433 addr
= env
->exception
.vaddress
;
435 info
.si_signo
= TARGET_SIGSEGV
;
437 /* XXX: check env->error_code */
438 info
.si_code
= TARGET_SEGV_MAPERR
;
439 info
._sifields
._sigfault
._addr
= addr
;
440 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
446 info
.si_signo
= TARGET_SIGTRAP
;
448 info
.si_code
= TARGET_TRAP_BRKPT
;
449 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
451 case EXCP_KERNEL_TRAP
:
452 if (do_kernel_trap(env
))
456 /* nothing to do here for user-mode, just resume guest code */
459 cpu_exec_step_atomic(cs
);
463 EXCP_DUMP(env
, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr
);
466 process_pending_signals(env
);
470 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
472 CPUState
*cpu
= env_cpu(env
);
473 TaskState
*ts
= cpu
->opaque
;
474 struct image_info
*info
= ts
->info
;
477 cpsr_write(env
, regs
->uregs
[16], CPSR_USER
| CPSR_EXEC
,
479 for(i
= 0; i
< 16; i
++) {
480 env
->regs
[i
] = regs
->uregs
[i
];
482 #ifdef TARGET_WORDS_BIGENDIAN
484 if (EF_ARM_EABI_VERSION(info
->elf_flags
) >= EF_ARM_EABI_VER4
485 && (info
->elf_flags
& EF_ARM_BE8
)) {
486 env
->uncached_cpsr
|= CPSR_E
;
487 env
->cp15
.sctlr_el
[1] |= SCTLR_E0E
;
489 env
->cp15
.sctlr_el
[1] |= SCTLR_B
;
491 arm_rebuild_hflags(env
);
494 ts
->stack_base
= info
->start_stack
;
495 ts
->heap_base
= info
->brk
;
496 /* This will be filled in on the first SYS_HEAPINFO call. */