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"
23 #include "cpu_loop-common.h"
25 #define get_user_code_u32(x, gaddr, env) \
26 ({ abi_long __r = get_user_u32((x), (gaddr)); \
27 if (!__r && bswap_code(arm_sctlr_b(env))) { \
33 #define get_user_code_u16(x, gaddr, env) \
34 ({ abi_long __r = get_user_u16((x), (gaddr)); \
35 if (!__r && bswap_code(arm_sctlr_b(env))) { \
41 #define get_user_data_u32(x, gaddr, env) \
42 ({ abi_long __r = get_user_u32((x), (gaddr)); \
43 if (!__r && arm_cpu_bswap_data(env)) { \
49 #define get_user_data_u16(x, gaddr, env) \
50 ({ abi_long __r = get_user_u16((x), (gaddr)); \
51 if (!__r && arm_cpu_bswap_data(env)) { \
57 #define put_user_data_u32(x, gaddr, env) \
58 ({ typeof(x) __x = (x); \
59 if (arm_cpu_bswap_data(env)) { \
62 put_user_u32(__x, (gaddr)); \
65 #define put_user_data_u16(x, gaddr, env) \
66 ({ typeof(x) __x = (x); \
67 if (arm_cpu_bswap_data(env)) { \
70 put_user_u16(__x, (gaddr)); \
73 /* Commpage handling -- there is no commpage for AArch64 */
76 * See the Linux kernel's Documentation/arm/kernel_user_helpers.txt
78 * r0 = pointer to oldval
79 * r1 = pointer to newval
80 * r2 = pointer to target value
83 * r0 = 0 if *ptr was changed, non-0 if no exchange happened
84 * C set if *ptr was changed, clear if no exchange happened
86 * Note segv's in kernel helpers are a bit tricky, we can set the
87 * data address sensibly but the PC address is just the entry point.
89 static void arm_kernel_cmpxchg64_helper(CPUARMState
*env
)
91 uint64_t oldval
, newval
, val
;
93 target_siginfo_t info
;
95 /* Based on the 32 bit code in do_kernel_trap */
97 /* XXX: This only works between threads, not between processes.
98 It's probably possible to implement this with native host
99 operations. However things like ldrex/strex are much harder so
100 there's not much point trying. */
102 cpsr
= cpsr_read(env
);
105 if (get_user_u64(oldval
, env
->regs
[0])) {
106 env
->exception
.vaddress
= env
->regs
[0];
110 if (get_user_u64(newval
, env
->regs
[1])) {
111 env
->exception
.vaddress
= env
->regs
[1];
115 if (get_user_u64(val
, addr
)) {
116 env
->exception
.vaddress
= addr
;
123 if (put_user_u64(val
, addr
)) {
124 env
->exception
.vaddress
= addr
;
134 cpsr_write(env
, cpsr
, CPSR_C
, CPSRWriteByInstr
);
140 /* We get the PC of the entry address - which is as good as anything,
141 on a real kernel what you get depends on which mode it uses. */
142 info
.si_signo
= TARGET_SIGSEGV
;
144 /* XXX: check env->error_code */
145 info
.si_code
= TARGET_SEGV_MAPERR
;
146 info
._sifields
._sigfault
._addr
= env
->exception
.vaddress
;
147 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
150 /* Handle a jump to the kernel code page. */
152 do_kernel_trap(CPUARMState
*env
)
158 switch (env
->regs
[15]) {
159 case 0xffff0fa0: /* __kernel_memory_barrier */
160 /* ??? No-op. Will need to do better for SMP. */
162 case 0xffff0fc0: /* __kernel_cmpxchg */
163 /* XXX: This only works between threads, not between processes.
164 It's probably possible to implement this with native host
165 operations. However things like ldrex/strex are much harder so
166 there's not much point trying. */
168 cpsr
= cpsr_read(env
);
170 /* FIXME: This should SEGV if the access fails. */
171 if (get_user_u32(val
, addr
))
173 if (val
== env
->regs
[0]) {
175 /* FIXME: Check for segfaults. */
176 put_user_u32(val
, addr
);
183 cpsr_write(env
, cpsr
, CPSR_C
, CPSRWriteByInstr
);
186 case 0xffff0fe0: /* __kernel_get_tls */
187 env
->regs
[0] = cpu_get_tls(env
);
189 case 0xffff0f60: /* __kernel_cmpxchg64 */
190 arm_kernel_cmpxchg64_helper(env
);
196 /* Jump back to the caller. */
197 addr
= env
->regs
[14];
202 env
->regs
[15] = addr
;
207 void cpu_loop(CPUARMState
*env
)
209 CPUState
*cs
= CPU(arm_env_get_cpu(env
));
211 unsigned int n
, insn
;
212 target_siginfo_t info
;
218 trapnr
= cpu_exec(cs
);
220 process_queued_cpu_work(cs
);
227 TaskState
*ts
= cs
->opaque
;
231 /* we handle the FPU emulation here, as Linux */
232 /* we get the opcode */
233 /* FIXME - what to do if get_user() fails? */
234 get_user_code_u32(opcode
, env
->regs
[15], env
);
236 rc
= EmulateAll(opcode
, &ts
->fpa
, env
);
237 if (rc
== 0) { /* illegal instruction */
238 info
.si_signo
= TARGET_SIGILL
;
240 info
.si_code
= TARGET_ILL_ILLOPN
;
241 info
._sifields
._sigfault
._addr
= env
->regs
[15];
242 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
243 } else if (rc
< 0) { /* FP exception */
246 /* translate softfloat flags to FPSR flags */
247 if (-rc
& float_flag_invalid
)
249 if (-rc
& float_flag_divbyzero
)
251 if (-rc
& float_flag_overflow
)
253 if (-rc
& float_flag_underflow
)
255 if (-rc
& float_flag_inexact
)
258 FPSR fpsr
= ts
->fpa
.fpsr
;
259 //printf("fpsr 0x%x, arm_fpe 0x%x\n",fpsr,arm_fpe);
261 if (fpsr
& (arm_fpe
<< 16)) { /* exception enabled? */
262 info
.si_signo
= TARGET_SIGFPE
;
265 /* ordered by priority, least first */
266 if (arm_fpe
& BIT_IXC
) info
.si_code
= TARGET_FPE_FLTRES
;
267 if (arm_fpe
& BIT_UFC
) info
.si_code
= TARGET_FPE_FLTUND
;
268 if (arm_fpe
& BIT_OFC
) info
.si_code
= TARGET_FPE_FLTOVF
;
269 if (arm_fpe
& BIT_DZC
) info
.si_code
= TARGET_FPE_FLTDIV
;
270 if (arm_fpe
& BIT_IOC
) info
.si_code
= TARGET_FPE_FLTINV
;
272 info
._sifields
._sigfault
._addr
= env
->regs
[15];
273 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
278 /* accumulate unenabled exceptions */
279 if ((!(fpsr
& BIT_IXE
)) && (arm_fpe
& BIT_IXC
))
281 if ((!(fpsr
& BIT_UFE
)) && (arm_fpe
& BIT_UFC
))
283 if ((!(fpsr
& BIT_OFE
)) && (arm_fpe
& BIT_OFC
))
285 if ((!(fpsr
& BIT_DZE
)) && (arm_fpe
& BIT_DZC
))
287 if ((!(fpsr
& BIT_IOE
)) && (arm_fpe
& BIT_IOC
))
290 } else { /* everything OK */
301 if (trapnr
== EXCP_BKPT
) {
303 /* FIXME - what to do if get_user() fails? */
304 get_user_code_u16(insn
, env
->regs
[15], env
);
308 /* FIXME - what to do if get_user() fails? */
309 get_user_code_u32(insn
, env
->regs
[15], env
);
310 n
= (insn
& 0xf) | ((insn
>> 4) & 0xff0);
315 /* FIXME - what to do if get_user() fails? */
316 get_user_code_u16(insn
, env
->regs
[15] - 2, env
);
319 /* FIXME - what to do if get_user() fails? */
320 get_user_code_u32(insn
, env
->regs
[15] - 4, env
);
325 if (n
== ARM_NR_cacheflush
) {
327 } else if (n
== ARM_NR_semihosting
328 || n
== ARM_NR_thumb_semihosting
) {
329 env
->regs
[0] = do_arm_semihosting (env
);
330 } else if (n
== 0 || n
>= ARM_SYSCALL_BASE
|| env
->thumb
) {
332 if (env
->thumb
|| n
== 0) {
335 n
-= ARM_SYSCALL_BASE
;
338 if ( n
> ARM_NR_BASE
) {
340 case ARM_NR_cacheflush
:
344 cpu_set_tls(env
, env
->regs
[0]);
347 case ARM_NR_breakpoint
:
348 env
->regs
[15] -= env
->thumb
? 2 : 4;
351 env
->regs
[0] = cpu_get_tls(env
);
354 gemu_log("qemu: Unsupported ARM syscall: 0x%x\n",
356 env
->regs
[0] = -TARGET_ENOSYS
;
360 ret
= do_syscall(env
,
369 if (ret
== -TARGET_ERESTARTSYS
) {
370 env
->regs
[15] -= env
->thumb
? 2 : 4;
371 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
381 env
->regs
[0] = do_arm_semihosting(env
);
384 /* just indicate that signals should be handled asap */
386 case EXCP_PREFETCH_ABORT
:
387 case EXCP_DATA_ABORT
:
388 addr
= env
->exception
.vaddress
;
390 info
.si_signo
= TARGET_SIGSEGV
;
392 /* XXX: check env->error_code */
393 info
.si_code
= TARGET_SEGV_MAPERR
;
394 info
._sifields
._sigfault
._addr
= addr
;
395 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
400 info
.si_signo
= TARGET_SIGTRAP
;
402 info
.si_code
= TARGET_TRAP_BRKPT
;
403 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
405 case EXCP_KERNEL_TRAP
:
406 if (do_kernel_trap(env
))
410 /* nothing to do here for user-mode, just resume guest code */
413 cpu_exec_step_atomic(cs
);
417 EXCP_DUMP(env
, "qemu: unhandled CPU exception 0x%x - aborting\n", trapnr
);
420 process_pending_signals(env
);
424 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
426 CPUState
*cpu
= ENV_GET_CPU(env
);
427 TaskState
*ts
= cpu
->opaque
;
428 struct image_info
*info
= ts
->info
;
431 cpsr_write(env
, regs
->uregs
[16], CPSR_USER
| CPSR_EXEC
,
433 for(i
= 0; i
< 16; i
++) {
434 env
->regs
[i
] = regs
->uregs
[i
];
436 #ifdef TARGET_WORDS_BIGENDIAN
438 if (EF_ARM_EABI_VERSION(info
->elf_flags
) >= EF_ARM_EABI_VER4
439 && (info
->elf_flags
& EF_ARM_BE8
)) {
440 env
->uncached_cpsr
|= CPSR_E
;
441 env
->cp15
.sctlr_el
[1] |= SCTLR_E0E
;
443 env
->cp15
.sctlr_el
[1] |= SCTLR_B
;
447 ts
->stack_base
= info
->start_stack
;
448 ts
->heap_base
= info
->brk
;
449 /* This will be filled in on the first SYS_HEAPINFO call. */