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"
26 #define get_user_code_u32(x, gaddr, env) \
27 ({ abi_long __r = get_user_u32((x), (gaddr)); \
28 if (!__r && bswap_code(arm_sctlr_b(env))) { \
34 #define get_user_code_u16(x, gaddr, env) \
35 ({ abi_long __r = get_user_u16((x), (gaddr)); \
36 if (!__r && bswap_code(arm_sctlr_b(env))) { \
42 #define get_user_data_u32(x, gaddr, env) \
43 ({ abi_long __r = get_user_u32((x), (gaddr)); \
44 if (!__r && arm_cpu_bswap_data(env)) { \
50 #define get_user_data_u16(x, gaddr, env) \
51 ({ abi_long __r = get_user_u16((x), (gaddr)); \
52 if (!__r && arm_cpu_bswap_data(env)) { \
58 #define put_user_data_u32(x, gaddr, env) \
59 ({ typeof(x) __x = (x); \
60 if (arm_cpu_bswap_data(env)) { \
63 put_user_u32(__x, (gaddr)); \
66 #define put_user_data_u16(x, gaddr, env) \
67 ({ typeof(x) __x = (x); \
68 if (arm_cpu_bswap_data(env)) { \
71 put_user_u16(__x, (gaddr)); \
74 /* Commpage handling -- there is no commpage for AArch64 */
77 * See the Linux kernel's Documentation/arm/kernel_user_helpers.txt
79 * r0 = pointer to oldval
80 * r1 = pointer to newval
81 * r2 = pointer to target value
84 * r0 = 0 if *ptr was changed, non-0 if no exchange happened
85 * C set if *ptr was changed, clear if no exchange happened
87 * Note segv's in kernel helpers are a bit tricky, we can set the
88 * data address sensibly but the PC address is just the entry point.
90 static void arm_kernel_cmpxchg64_helper(CPUARMState
*env
)
92 uint64_t oldval
, newval
, val
;
94 target_siginfo_t info
;
96 /* Based on the 32 bit code in do_kernel_trap */
98 /* XXX: This only works between threads, not between processes.
99 It's probably possible to implement this with native host
100 operations. However things like ldrex/strex are much harder so
101 there's not much point trying. */
103 cpsr
= cpsr_read(env
);
106 if (get_user_u64(oldval
, env
->regs
[0])) {
107 env
->exception
.vaddress
= env
->regs
[0];
111 if (get_user_u64(newval
, env
->regs
[1])) {
112 env
->exception
.vaddress
= env
->regs
[1];
116 if (get_user_u64(val
, addr
)) {
117 env
->exception
.vaddress
= addr
;
124 if (put_user_u64(val
, addr
)) {
125 env
->exception
.vaddress
= addr
;
135 cpsr_write(env
, cpsr
, CPSR_C
, CPSRWriteByInstr
);
141 /* We get the PC of the entry address - which is as good as anything,
142 on a real kernel what you get depends on which mode it uses. */
143 info
.si_signo
= TARGET_SIGSEGV
;
145 /* XXX: check env->error_code */
146 info
.si_code
= TARGET_SEGV_MAPERR
;
147 info
._sifields
._sigfault
._addr
= env
->exception
.vaddress
;
148 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
151 /* Handle a jump to the kernel code page. */
153 do_kernel_trap(CPUARMState
*env
)
159 switch (env
->regs
[15]) {
160 case 0xffff0fa0: /* __kernel_memory_barrier */
161 /* ??? No-op. Will need to do better for SMP. */
163 case 0xffff0fc0: /* __kernel_cmpxchg */
164 /* XXX: This only works between threads, not between processes.
165 It's probably possible to implement this with native host
166 operations. However things like ldrex/strex are much harder so
167 there's not much point trying. */
169 cpsr
= cpsr_read(env
);
171 /* FIXME: This should SEGV if the access fails. */
172 if (get_user_u32(val
, addr
))
174 if (val
== env
->regs
[0]) {
176 /* FIXME: Check for segfaults. */
177 put_user_u32(val
, addr
);
184 cpsr_write(env
, cpsr
, CPSR_C
, CPSRWriteByInstr
);
187 case 0xffff0fe0: /* __kernel_get_tls */
188 env
->regs
[0] = cpu_get_tls(env
);
190 case 0xffff0f60: /* __kernel_cmpxchg64 */
191 arm_kernel_cmpxchg64_helper(env
);
197 /* Jump back to the caller. */
198 addr
= env
->regs
[14];
203 env
->regs
[15] = addr
;
208 static bool insn_is_linux_bkpt(uint32_t opcode
, bool is_thumb
)
211 * Return true if this insn is one of the three magic UDF insns
212 * which the kernel treats as breakpoint insns.
215 return (opcode
& 0x0fffffff) == 0x07f001f0;
218 * Note that we get the two halves of the 32-bit T32 insn
219 * in the opposite order to the value the kernel uses in
220 * its undef_hook struct.
222 return ((opcode
& 0xffff) == 0xde01) || (opcode
== 0xa000f7f0);
226 void cpu_loop(CPUARMState
*env
)
228 CPUState
*cs
= env_cpu(env
);
230 unsigned int n
, insn
;
231 target_siginfo_t info
;
237 trapnr
= cpu_exec(cs
);
239 process_queued_cpu_work(cs
);
246 TaskState
*ts
= cs
->opaque
;
250 /* we handle the FPU emulation here, as Linux */
251 /* we get the opcode */
252 /* FIXME - what to do if get_user() fails? */
253 get_user_code_u32(opcode
, env
->regs
[15], env
);
256 * The Linux kernel treats some UDF patterns specially
257 * to use as breakpoints (instead of the architectural
258 * bkpt insn). These should trigger a SIGTRAP rather
261 if (insn_is_linux_bkpt(opcode
, env
->thumb
)) {
265 rc
= EmulateAll(opcode
, &ts
->fpa
, env
);
266 if (rc
== 0) { /* illegal instruction */
267 info
.si_signo
= TARGET_SIGILL
;
269 info
.si_code
= TARGET_ILL_ILLOPN
;
270 info
._sifields
._sigfault
._addr
= env
->regs
[15];
271 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
272 } else if (rc
< 0) { /* FP exception */
275 /* translate softfloat flags to FPSR flags */
276 if (-rc
& float_flag_invalid
)
278 if (-rc
& float_flag_divbyzero
)
280 if (-rc
& float_flag_overflow
)
282 if (-rc
& float_flag_underflow
)
284 if (-rc
& float_flag_inexact
)
287 FPSR fpsr
= ts
->fpa
.fpsr
;
288 //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
290 if (fpsr
& (arm_fpe
<< 16)) { /* exception enabled? */
291 info
.si_signo
= TARGET_SIGFPE
;
294 /* ordered by priority, least first */
295 if (arm_fpe
& BIT_IXC
) info
.si_code
= TARGET_FPE_FLTRES
;
296 if (arm_fpe
& BIT_UFC
) info
.si_code
= TARGET_FPE_FLTUND
;
297 if (arm_fpe
& BIT_OFC
) info
.si_code
= TARGET_FPE_FLTOVF
;
298 if (arm_fpe
& BIT_DZC
) info
.si_code
= TARGET_FPE_FLTDIV
;
299 if (arm_fpe
& BIT_IOC
) info
.si_code
= TARGET_FPE_FLTINV
;
301 info
._sifields
._sigfault
._addr
= env
->regs
[15];
302 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
307 /* accumulate unenabled exceptions */
308 if ((!(fpsr
& BIT_IXE
)) && (arm_fpe
& BIT_IXC
))
310 if ((!(fpsr
& BIT_UFE
)) && (arm_fpe
& BIT_UFC
))
312 if ((!(fpsr
& BIT_OFE
)) && (arm_fpe
& BIT_OFC
))
314 if ((!(fpsr
& BIT_DZE
)) && (arm_fpe
& BIT_DZC
))
316 if ((!(fpsr
& BIT_IOE
)) && (arm_fpe
& BIT_IOC
))
319 } else { /* everything OK */
330 /* Thumb is always EABI style with syscall number in r7 */
334 * Equivalent of kernel CONFIG_OABI_COMPAT: read the
335 * Arm SVC insn to extract the immediate, which is the
336 * syscall number in OABI.
338 /* FIXME - what to do if get_user() fails? */
339 get_user_code_u32(insn
, env
->regs
[15] - 4, env
);
342 /* zero immediate: EABI, syscall number in r7 */
346 * This XOR matches the kernel code: an immediate
347 * in the valid range (0x900000 .. 0x9fffff) is
348 * converted into the correct EABI-style syscall
349 * number; invalid immediates end up as values
350 * > 0xfffff and are handled below as out-of-range.
352 n
^= ARM_SYSCALL_BASE
;
357 if (n
> ARM_NR_BASE
) {
359 case ARM_NR_cacheflush
:
363 cpu_set_tls(env
, env
->regs
[0]);
366 case ARM_NR_breakpoint
:
367 env
->regs
[15] -= env
->thumb
? 2 : 4;
370 env
->regs
[0] = cpu_get_tls(env
);
375 * Syscalls 0xf0000..0xf07ff (or 0x9f0000..
376 * 0x9f07ff in OABI numbering) are defined
377 * to return -ENOSYS rather than raising
378 * SIGILL. Note that we have already
379 * removed the 0x900000 prefix.
381 qemu_log_mask(LOG_UNIMP
,
382 "qemu: Unsupported ARM syscall: 0x%x\n",
384 env
->regs
[0] = -TARGET_ENOSYS
;
387 * Otherwise SIGILL. This includes any SWI with
388 * immediate not originally 0x9fxxxx, because
389 * of the earlier XOR.
391 info
.si_signo
= TARGET_SIGILL
;
393 info
.si_code
= TARGET_ILL_ILLTRP
;
394 info
._sifields
._sigfault
._addr
= env
->regs
[15];
396 info
._sifields
._sigfault
._addr
-= 2;
398 info
._sifields
._sigfault
._addr
-= 4;
400 queue_signal(env
, info
.si_signo
,
401 QEMU_SI_FAULT
, &info
);
406 ret
= do_syscall(env
,
415 if (ret
== -TARGET_ERESTARTSYS
) {
416 env
->regs
[15] -= env
->thumb
? 2 : 4;
417 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
424 env
->regs
[0] = do_arm_semihosting(env
);
425 env
->regs
[15] += env
->thumb
? 2 : 4;
428 /* just indicate that signals should be handled asap */
430 case EXCP_PREFETCH_ABORT
:
431 case EXCP_DATA_ABORT
:
432 addr
= env
->exception
.vaddress
;
434 info
.si_signo
= TARGET_SIGSEGV
;
436 /* XXX: check env->error_code */
437 info
.si_code
= TARGET_SEGV_MAPERR
;
438 info
._sifields
._sigfault
._addr
= addr
;
439 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
445 info
.si_signo
= TARGET_SIGTRAP
;
447 info
.si_code
= TARGET_TRAP_BRKPT
;
448 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
450 case EXCP_KERNEL_TRAP
:
451 if (do_kernel_trap(env
))
455 /* nothing to do here for user-mode, just resume guest code */
458 cpu_exec_step_atomic(cs
);
462 EXCP_DUMP(env
, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr
);
465 process_pending_signals(env
);
469 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
471 CPUState
*cpu
= env_cpu(env
);
472 TaskState
*ts
= cpu
->opaque
;
473 struct image_info
*info
= ts
->info
;
476 cpsr_write(env
, regs
->uregs
[16], CPSR_USER
| CPSR_EXEC
,
478 for(i
= 0; i
< 16; i
++) {
479 env
->regs
[i
] = regs
->uregs
[i
];
481 #ifdef TARGET_WORDS_BIGENDIAN
483 if (EF_ARM_EABI_VERSION(info
->elf_flags
) >= EF_ARM_EABI_VER4
484 && (info
->elf_flags
& EF_ARM_BE8
)) {
485 env
->uncached_cpsr
|= CPSR_E
;
486 env
->cp15
.sctlr_el
[1] |= SCTLR_E0E
;
488 env
->cp15
.sctlr_el
[1] |= SCTLR_B
;
490 arm_rebuild_hflags(env
);
493 ts
->stack_base
= info
->start_stack
;
494 ts
->heap_base
= info
->brk
;
495 /* This will be filled in on the first SYS_HEAPINFO call. */