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/>.
19 #include "qemu/osdep.h"
20 #include "qemu-version.h"
21 #include <sys/syscall.h>
22 #include <sys/resource.h>
24 #include "qapi/error.h"
26 #include "qemu/path.h"
27 #include "qemu/config-file.h"
28 #include "qemu/cutils.h"
29 #include "qemu/help_option.h"
31 #include "exec/exec-all.h"
33 #include "qemu/timer.h"
34 #include "qemu/envlist.h"
36 #include "trace/control.h"
37 #include "target_elf.h"
38 #include "cpu_loop-common.h"
43 static const char *filename
;
44 static const char *argv0
;
45 static int gdbstub_port
;
46 static envlist_t
*envlist
;
47 static const char *cpu_model
;
48 static const char *cpu_type
;
49 unsigned long mmap_min_addr
;
50 unsigned long guest_base
;
54 * When running 32-on-64 we should make sure we can fit all of the possible
55 * guest address space into a contiguous chunk of virtual host memory.
57 * This way we will never overlap with our own libraries or binaries or stack
58 * or anything else that QEMU maps.
60 * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
61 * of the address for the kernel. Some cpus rely on this and user space
62 * uses the high bit(s) for pointer tagging and the like. For them, we
63 * must preserve the expected address space.
65 #ifndef MAX_RESERVED_VA
66 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
67 # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
68 (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
69 /* There are a number of places where we assign reserved_va to a variable
70 of type abi_ulong and expect it to fit. Avoid the last page. */
71 # define MAX_RESERVED_VA (0xfffffffful & TARGET_PAGE_MASK)
73 # define MAX_RESERVED_VA (1ul << TARGET_VIRT_ADDR_SPACE_BITS)
76 # define MAX_RESERVED_VA 0
80 /* That said, reserving *too* much vm space via mmap can run into problems
81 with rlimits, oom due to page table creation, etc. We will still try it,
82 if directed by the command-line option, but not by default. */
83 #if HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32
84 unsigned long reserved_va
= MAX_RESERVED_VA
;
86 unsigned long reserved_va
;
89 static void usage(int exitcode
);
91 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
92 const char *qemu_uname_release
;
94 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
95 we allocate a bigger stack. Need a better solution, for example
96 by remapping the process stack directly at the right place */
97 unsigned long guest_stack_size
= 8 * 1024 * 1024UL;
99 void gemu_log(const char *fmt
, ...)
104 vfprintf(stderr
, fmt
, ap
);
108 #if defined(TARGET_I386)
109 int cpu_get_pic_interrupt(CPUX86State
*env
)
115 /***********************************************************/
116 /* Helper routines for implementing atomic operations. */
118 /* Make sure everything is in a consistent state for calling fork(). */
119 void fork_start(void)
123 qemu_mutex_lock(&tb_ctx
.tb_lock
);
127 void fork_end(int child
)
129 mmap_fork_end(child
);
131 CPUState
*cpu
, *next_cpu
;
132 /* Child processes created by fork() only have a single thread.
133 Discard information about the parent threads. */
134 CPU_FOREACH_SAFE(cpu
, next_cpu
) {
135 if (cpu
!= thread_cpu
) {
136 QTAILQ_REMOVE(&cpus
, cpu
, node
);
139 qemu_mutex_init(&tb_ctx
.tb_lock
);
140 qemu_init_cpu_list();
141 gdbserver_fork(thread_cpu
);
142 /* qemu_init_cpu_list() takes care of reinitializing the
143 * exclusive state, so we don't need to end_exclusive() here.
146 qemu_mutex_unlock(&tb_ctx
.tb_lock
);
154 static void gen_sigill_reg(CPUTLGState
*env
)
156 target_siginfo_t info
;
158 info
.si_signo
= TARGET_SIGILL
;
160 info
.si_code
= TARGET_ILL_PRVREG
;
161 info
._sifields
._sigfault
._addr
= env
->pc
;
162 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
165 static void do_signal(CPUTLGState
*env
, int signo
, int sigcode
)
167 target_siginfo_t info
;
169 info
.si_signo
= signo
;
171 info
._sifields
._sigfault
._addr
= env
->pc
;
173 if (signo
== TARGET_SIGSEGV
) {
174 /* The passed in sigcode is a dummy; check for a page mapping
175 and pass either MAPERR or ACCERR. */
176 target_ulong addr
= env
->excaddr
;
177 info
._sifields
._sigfault
._addr
= addr
;
178 if (page_check_range(addr
, 1, PAGE_VALID
) < 0) {
179 sigcode
= TARGET_SEGV_MAPERR
;
181 sigcode
= TARGET_SEGV_ACCERR
;
184 info
.si_code
= sigcode
;
186 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
189 static void gen_sigsegv_maperr(CPUTLGState
*env
, target_ulong addr
)
192 do_signal(env
, TARGET_SIGSEGV
, 0);
195 static void set_regval(CPUTLGState
*env
, uint8_t reg
, uint64_t val
)
197 if (unlikely(reg
>= TILEGX_R_COUNT
)) {
211 g_assert_not_reached();
214 env
->regs
[reg
] = val
;
218 * Compare the 8-byte contents of the CmpValue SPR with the 8-byte value in
219 * memory at the address held in the first source register. If the values are
220 * not equal, then no memory operation is performed. If the values are equal,
221 * the 8-byte quantity from the second source register is written into memory
222 * at the address held in the first source register. In either case, the result
223 * of the instruction is the value read from memory. The compare and write to
224 * memory are atomic and thus can be used for synchronization purposes. This
225 * instruction only operates for addresses aligned to a 8-byte boundary.
226 * Unaligned memory access causes an Unaligned Data Reference interrupt.
228 * Functional Description (64-bit)
229 * uint64_t memVal = memoryReadDoubleWord (rf[SrcA]);
231 * if (memVal == SPR[CmpValueSPR])
232 * memoryWriteDoubleWord (rf[SrcA], rf[SrcB]);
234 * Functional Description (32-bit)
235 * uint64_t memVal = signExtend32 (memoryReadWord (rf[SrcA]));
237 * if (memVal == signExtend32 (SPR[CmpValueSPR]))
238 * memoryWriteWord (rf[SrcA], rf[SrcB]);
241 * This function also processes exch and exch4 which need not process SPR.
243 static void do_exch(CPUTLGState
*env
, bool quad
, bool cmp
)
246 target_long val
, sprval
;
250 addr
= env
->atomic_srca
;
251 if (quad
? get_user_s64(val
, addr
) : get_user_s32(val
, addr
)) {
257 sprval
= env
->spregs
[TILEGX_SPR_CMPEXCH
];
259 sprval
= sextract64(env
->spregs
[TILEGX_SPR_CMPEXCH
], 0, 32);
263 if (!cmp
|| val
== sprval
) {
264 target_long valb
= env
->atomic_srcb
;
265 if (quad
? put_user_u64(valb
, addr
) : put_user_u32(valb
, addr
)) {
270 set_regval(env
, env
->atomic_dstr
, val
);
276 gen_sigsegv_maperr(env
, addr
);
279 static void do_fetch(CPUTLGState
*env
, int trapnr
, bool quad
)
283 target_long val
, valb
;
287 addr
= env
->atomic_srca
;
288 valb
= env
->atomic_srcb
;
289 if (quad
? get_user_s64(val
, addr
) : get_user_s32(val
, addr
)) {
294 case TILEGX_EXCP_OPCODE_FETCHADD
:
295 case TILEGX_EXCP_OPCODE_FETCHADD4
:
298 case TILEGX_EXCP_OPCODE_FETCHADDGEZ
:
304 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4
:
306 if ((int32_t)valb
< 0) {
310 case TILEGX_EXCP_OPCODE_FETCHAND
:
311 case TILEGX_EXCP_OPCODE_FETCHAND4
:
314 case TILEGX_EXCP_OPCODE_FETCHOR
:
315 case TILEGX_EXCP_OPCODE_FETCHOR4
:
319 g_assert_not_reached();
323 if (quad
? put_user_u64(valb
, addr
) : put_user_u32(valb
, addr
)) {
328 set_regval(env
, env
->atomic_dstr
, val
);
334 gen_sigsegv_maperr(env
, addr
);
337 void cpu_loop(CPUTLGState
*env
)
339 CPUState
*cs
= CPU(tilegx_env_get_cpu(env
));
344 trapnr
= cpu_exec(cs
);
346 process_queued_cpu_work(cs
);
349 case TILEGX_EXCP_SYSCALL
:
351 abi_ulong ret
= do_syscall(env
, env
->regs
[TILEGX_R_NR
],
352 env
->regs
[0], env
->regs
[1],
353 env
->regs
[2], env
->regs
[3],
354 env
->regs
[4], env
->regs
[5],
355 env
->regs
[6], env
->regs
[7]);
356 if (ret
== -TARGET_ERESTARTSYS
) {
358 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
359 env
->regs
[TILEGX_R_RE
] = ret
;
360 env
->regs
[TILEGX_R_ERR
] = TILEGX_IS_ERRNO(ret
) ? -ret
: 0;
364 case TILEGX_EXCP_OPCODE_EXCH
:
365 do_exch(env
, true, false);
367 case TILEGX_EXCP_OPCODE_EXCH4
:
368 do_exch(env
, false, false);
370 case TILEGX_EXCP_OPCODE_CMPEXCH
:
371 do_exch(env
, true, true);
373 case TILEGX_EXCP_OPCODE_CMPEXCH4
:
374 do_exch(env
, false, true);
376 case TILEGX_EXCP_OPCODE_FETCHADD
:
377 case TILEGX_EXCP_OPCODE_FETCHADDGEZ
:
378 case TILEGX_EXCP_OPCODE_FETCHAND
:
379 case TILEGX_EXCP_OPCODE_FETCHOR
:
380 do_fetch(env
, trapnr
, true);
382 case TILEGX_EXCP_OPCODE_FETCHADD4
:
383 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4
:
384 case TILEGX_EXCP_OPCODE_FETCHAND4
:
385 case TILEGX_EXCP_OPCODE_FETCHOR4
:
386 do_fetch(env
, trapnr
, false);
388 case TILEGX_EXCP_SIGNAL
:
389 do_signal(env
, env
->signo
, env
->sigcode
);
391 case TILEGX_EXCP_REG_IDN_ACCESS
:
392 case TILEGX_EXCP_REG_UDN_ACCESS
:
396 cpu_exec_step_atomic(cs
);
399 fprintf(stderr
, "trapnr is %d[0x%x].\n", trapnr
, trapnr
);
400 g_assert_not_reached();
402 process_pending_signals(env
);
410 void cpu_loop(CPURISCVState
*env
)
412 CPUState
*cs
= CPU(riscv_env_get_cpu(env
));
413 int trapnr
, signum
, sigcode
;
414 target_ulong sigaddr
;
419 trapnr
= cpu_exec(cs
);
421 process_queued_cpu_work(cs
);
429 /* just indicate that signals should be handled asap */
432 cpu_exec_step_atomic(cs
);
434 case RISCV_EXCP_U_ECALL
:
436 if (env
->gpr
[xA7
] == TARGET_NR_arch_specific_syscall
+ 15) {
437 /* riscv_flush_icache_syscall is a no-op in QEMU as
438 self-modifying code is automatically detected */
441 ret
= do_syscall(env
,
451 if (ret
== -TARGET_ERESTARTSYS
) {
453 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
456 if (cs
->singlestep_enabled
) {
460 case RISCV_EXCP_ILLEGAL_INST
:
461 signum
= TARGET_SIGILL
;
462 sigcode
= TARGET_ILL_ILLOPC
;
464 case RISCV_EXCP_BREAKPOINT
:
465 signum
= TARGET_SIGTRAP
;
466 sigcode
= TARGET_TRAP_BRKPT
;
469 case RISCV_EXCP_INST_PAGE_FAULT
:
470 case RISCV_EXCP_LOAD_PAGE_FAULT
:
471 case RISCV_EXCP_STORE_PAGE_FAULT
:
472 signum
= TARGET_SIGSEGV
;
473 sigcode
= TARGET_SEGV_MAPERR
;
477 signum
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
478 sigcode
= TARGET_TRAP_BRKPT
;
481 EXCP_DUMP(env
, "\nqemu: unhandled CPU exception %#x - aborting\n",
487 target_siginfo_t info
= {
491 ._sifields
._sigfault
._addr
= sigaddr
493 queue_signal(env
, info
.si_signo
, QEMU_SI_KILL
, &info
);
496 process_pending_signals(env
);
500 #endif /* TARGET_RISCV */
504 static abi_ulong
hppa_lws(CPUHPPAState
*env
)
506 uint32_t which
= env
->gr
[20];
507 abi_ulong addr
= env
->gr
[26];
508 abi_ulong old
= env
->gr
[25];
509 abi_ulong
new = env
->gr
[24];
514 return -TARGET_ENOSYS
;
516 case 0: /* elf32 atomic 32bit cmpxchg */
517 if ((addr
& 3) || !access_ok(VERIFY_WRITE
, addr
, 4)) {
518 return -TARGET_EFAULT
;
522 ret
= atomic_cmpxchg((uint32_t *)g2h(addr
), old
, new);
526 case 2: /* elf32 atomic "new" cmpxchg */
529 return -TARGET_ENOSYS
;
531 if (((addr
| old
| new) & ((1 << size
) - 1))
532 || !access_ok(VERIFY_WRITE
, addr
, 1 << size
)
533 || !access_ok(VERIFY_READ
, old
, 1 << size
)
534 || !access_ok(VERIFY_READ
, new, 1 << size
)) {
535 return -TARGET_EFAULT
;
537 /* Note that below we use host-endian loads so that the cmpxchg
538 can be host-endian as well. */
541 old
= *(uint8_t *)g2h(old
);
542 new = *(uint8_t *)g2h(new);
543 ret
= atomic_cmpxchg((uint8_t *)g2h(addr
), old
, new);
547 old
= *(uint16_t *)g2h(old
);
548 new = *(uint16_t *)g2h(new);
549 ret
= atomic_cmpxchg((uint16_t *)g2h(addr
), old
, new);
553 old
= *(uint32_t *)g2h(old
);
554 new = *(uint32_t *)g2h(new);
555 ret
= atomic_cmpxchg((uint32_t *)g2h(addr
), old
, new);
560 uint64_t o64
, n64
, r64
;
561 o64
= *(uint64_t *)g2h(old
);
562 n64
= *(uint64_t *)g2h(new);
563 #ifdef CONFIG_ATOMIC64
564 r64
= atomic_cmpxchg__nocheck((uint64_t *)g2h(addr
), o64
, n64
);
568 r64
= *(uint64_t *)g2h(addr
);
571 *(uint64_t *)g2h(addr
) = n64
;
586 void cpu_loop(CPUHPPAState
*env
)
588 CPUState
*cs
= CPU(hppa_env_get_cpu(env
));
589 target_siginfo_t info
;
595 trapnr
= cpu_exec(cs
);
597 process_queued_cpu_work(cs
);
601 ret
= do_syscall(env
, env
->gr
[20],
602 env
->gr
[26], env
->gr
[25],
603 env
->gr
[24], env
->gr
[23],
604 env
->gr
[22], env
->gr
[21], 0, 0);
608 /* We arrived here by faking the gateway page. Return. */
609 env
->iaoq_f
= env
->gr
[31];
610 env
->iaoq_b
= env
->gr
[31] + 4;
612 case -TARGET_ERESTARTSYS
:
613 case -TARGET_QEMU_ESIGRETURN
:
617 case EXCP_SYSCALL_LWS
:
618 env
->gr
[21] = hppa_lws(env
);
619 /* We arrived here by faking the gateway page. Return. */
620 env
->iaoq_f
= env
->gr
[31];
621 env
->iaoq_b
= env
->gr
[31] + 4;
625 case EXCP_NA_ITLB_MISS
:
626 case EXCP_NA_DTLB_MISS
:
633 info
.si_signo
= TARGET_SIGSEGV
;
635 info
.si_code
= TARGET_SEGV_ACCERR
;
636 info
._sifields
._sigfault
._addr
= env
->cr
[CR_IOR
];
637 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
640 info
.si_signo
= TARGET_SIGBUS
;
643 info
._sifields
._sigfault
._addr
= env
->cr
[CR_IOR
];
644 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
649 info
.si_signo
= TARGET_SIGILL
;
651 info
.si_code
= TARGET_ILL_ILLOPN
;
652 info
._sifields
._sigfault
._addr
= env
->iaoq_f
;
653 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
658 info
.si_signo
= TARGET_SIGFPE
;
661 info
._sifields
._sigfault
._addr
= env
->iaoq_f
;
662 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
665 trapnr
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
667 info
.si_signo
= trapnr
;
669 info
.si_code
= TARGET_TRAP_BRKPT
;
670 queue_signal(env
, trapnr
, QEMU_SI_FAULT
, &info
);
674 /* just indicate that signals should be handled asap */
677 g_assert_not_reached();
679 process_pending_signals(env
);
683 #endif /* TARGET_HPPA */
687 static void xtensa_rfw(CPUXtensaState
*env
)
689 xtensa_restore_owb(env
);
690 env
->pc
= env
->sregs
[EPC1
];
693 static void xtensa_rfwu(CPUXtensaState
*env
)
695 env
->sregs
[WINDOW_START
] |= (1 << env
->sregs
[WINDOW_BASE
]);
699 static void xtensa_rfwo(CPUXtensaState
*env
)
701 env
->sregs
[WINDOW_START
] &= ~(1 << env
->sregs
[WINDOW_BASE
]);
705 static void xtensa_overflow4(CPUXtensaState
*env
)
707 put_user_ual(env
->regs
[0], env
->regs
[5] - 16);
708 put_user_ual(env
->regs
[1], env
->regs
[5] - 12);
709 put_user_ual(env
->regs
[2], env
->regs
[5] - 8);
710 put_user_ual(env
->regs
[3], env
->regs
[5] - 4);
714 static void xtensa_underflow4(CPUXtensaState
*env
)
716 get_user_ual(env
->regs
[0], env
->regs
[5] - 16);
717 get_user_ual(env
->regs
[1], env
->regs
[5] - 12);
718 get_user_ual(env
->regs
[2], env
->regs
[5] - 8);
719 get_user_ual(env
->regs
[3], env
->regs
[5] - 4);
723 static void xtensa_overflow8(CPUXtensaState
*env
)
725 put_user_ual(env
->regs
[0], env
->regs
[9] - 16);
726 get_user_ual(env
->regs
[0], env
->regs
[1] - 12);
727 put_user_ual(env
->regs
[1], env
->regs
[9] - 12);
728 put_user_ual(env
->regs
[2], env
->regs
[9] - 8);
729 put_user_ual(env
->regs
[3], env
->regs
[9] - 4);
730 put_user_ual(env
->regs
[4], env
->regs
[0] - 32);
731 put_user_ual(env
->regs
[5], env
->regs
[0] - 28);
732 put_user_ual(env
->regs
[6], env
->regs
[0] - 24);
733 put_user_ual(env
->regs
[7], env
->regs
[0] - 20);
737 static void xtensa_underflow8(CPUXtensaState
*env
)
739 get_user_ual(env
->regs
[0], env
->regs
[9] - 16);
740 get_user_ual(env
->regs
[1], env
->regs
[9] - 12);
741 get_user_ual(env
->regs
[2], env
->regs
[9] - 8);
742 get_user_ual(env
->regs
[7], env
->regs
[1] - 12);
743 get_user_ual(env
->regs
[3], env
->regs
[9] - 4);
744 get_user_ual(env
->regs
[4], env
->regs
[7] - 32);
745 get_user_ual(env
->regs
[5], env
->regs
[7] - 28);
746 get_user_ual(env
->regs
[6], env
->regs
[7] - 24);
747 get_user_ual(env
->regs
[7], env
->regs
[7] - 20);
751 static void xtensa_overflow12(CPUXtensaState
*env
)
753 put_user_ual(env
->regs
[0], env
->regs
[13] - 16);
754 get_user_ual(env
->regs
[0], env
->regs
[1] - 12);
755 put_user_ual(env
->regs
[1], env
->regs
[13] - 12);
756 put_user_ual(env
->regs
[2], env
->regs
[13] - 8);
757 put_user_ual(env
->regs
[3], env
->regs
[13] - 4);
758 put_user_ual(env
->regs
[4], env
->regs
[0] - 48);
759 put_user_ual(env
->regs
[5], env
->regs
[0] - 44);
760 put_user_ual(env
->regs
[6], env
->regs
[0] - 40);
761 put_user_ual(env
->regs
[7], env
->regs
[0] - 36);
762 put_user_ual(env
->regs
[8], env
->regs
[0] - 32);
763 put_user_ual(env
->regs
[9], env
->regs
[0] - 28);
764 put_user_ual(env
->regs
[10], env
->regs
[0] - 24);
765 put_user_ual(env
->regs
[11], env
->regs
[0] - 20);
769 static void xtensa_underflow12(CPUXtensaState
*env
)
771 get_user_ual(env
->regs
[0], env
->regs
[13] - 16);
772 get_user_ual(env
->regs
[1], env
->regs
[13] - 12);
773 get_user_ual(env
->regs
[2], env
->regs
[13] - 8);
774 get_user_ual(env
->regs
[11], env
->regs
[1] - 12);
775 get_user_ual(env
->regs
[3], env
->regs
[13] - 4);
776 get_user_ual(env
->regs
[4], env
->regs
[11] - 48);
777 get_user_ual(env
->regs
[5], env
->regs
[11] - 44);
778 get_user_ual(env
->regs
[6], env
->regs
[11] - 40);
779 get_user_ual(env
->regs
[7], env
->regs
[11] - 36);
780 get_user_ual(env
->regs
[8], env
->regs
[11] - 32);
781 get_user_ual(env
->regs
[9], env
->regs
[11] - 28);
782 get_user_ual(env
->regs
[10], env
->regs
[11] - 24);
783 get_user_ual(env
->regs
[11], env
->regs
[11] - 20);
787 void cpu_loop(CPUXtensaState
*env
)
789 CPUState
*cs
= CPU(xtensa_env_get_cpu(env
));
790 target_siginfo_t info
;
796 trapnr
= cpu_exec(cs
);
798 process_queued_cpu_work(cs
);
800 env
->sregs
[PS
] &= ~PS_EXCM
;
805 case EXC_WINDOW_OVERFLOW4
:
806 xtensa_overflow4(env
);
808 case EXC_WINDOW_UNDERFLOW4
:
809 xtensa_underflow4(env
);
811 case EXC_WINDOW_OVERFLOW8
:
812 xtensa_overflow8(env
);
814 case EXC_WINDOW_UNDERFLOW8
:
815 xtensa_underflow8(env
);
817 case EXC_WINDOW_OVERFLOW12
:
818 xtensa_overflow12(env
);
820 case EXC_WINDOW_UNDERFLOW12
:
821 xtensa_underflow12(env
);
825 switch (env
->sregs
[EXCCAUSE
]) {
826 case ILLEGAL_INSTRUCTION_CAUSE
:
827 case PRIVILEGED_CAUSE
:
828 info
.si_signo
= TARGET_SIGILL
;
831 env
->sregs
[EXCCAUSE
] == ILLEGAL_INSTRUCTION_CAUSE
?
832 TARGET_ILL_ILLOPC
: TARGET_ILL_PRVOPC
;
833 info
._sifields
._sigfault
._addr
= env
->sregs
[EPC1
];
834 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
839 ret
= do_syscall(env
, env
->regs
[2],
840 env
->regs
[6], env
->regs
[3],
841 env
->regs
[4], env
->regs
[5],
842 env
->regs
[8], env
->regs
[9], 0, 0);
848 case -TARGET_ERESTARTSYS
:
852 case -TARGET_QEMU_ESIGRETURN
:
858 env
->sregs
[PS
] = deposit32(env
->sregs
[PS
],
861 env
->sregs
[WINDOW_BASE
]);
863 switch (env
->regs
[0] & 0xc0000000) {
866 xtensa_rotate_window(env
, -1);
867 xtensa_underflow4(env
);
871 xtensa_rotate_window(env
, -2);
872 xtensa_underflow8(env
);
876 xtensa_rotate_window(env
, -3);
877 xtensa_underflow12(env
);
882 case INTEGER_DIVIDE_BY_ZERO_CAUSE
:
883 info
.si_signo
= TARGET_SIGFPE
;
885 info
.si_code
= TARGET_FPE_INTDIV
;
886 info
._sifields
._sigfault
._addr
= env
->sregs
[EPC1
];
887 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
890 case LOAD_PROHIBITED_CAUSE
:
891 case STORE_PROHIBITED_CAUSE
:
892 info
.si_signo
= TARGET_SIGSEGV
;
894 info
.si_code
= TARGET_SEGV_ACCERR
;
895 info
._sifields
._sigfault
._addr
= env
->sregs
[EXCVADDR
];
896 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
900 fprintf(stderr
, "exccause = %d\n", env
->sregs
[EXCCAUSE
]);
901 g_assert_not_reached();
905 trapnr
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
907 info
.si_signo
= trapnr
;
909 info
.si_code
= TARGET_TRAP_BRKPT
;
910 queue_signal(env
, trapnr
, QEMU_SI_FAULT
, &info
);
915 fprintf(stderr
, "trapnr = %d\n", trapnr
);
916 g_assert_not_reached();
918 process_pending_signals(env
);
922 #endif /* TARGET_XTENSA */
924 __thread CPUState
*thread_cpu
;
926 bool qemu_cpu_is_self(CPUState
*cpu
)
928 return thread_cpu
== cpu
;
931 void qemu_cpu_kick(CPUState
*cpu
)
936 void task_settid(TaskState
*ts
)
938 if (ts
->ts_tid
== 0) {
939 ts
->ts_tid
= (pid_t
)syscall(SYS_gettid
);
943 void stop_all_tasks(void)
946 * We trust that when using NPTL, start_exclusive()
947 * handles thread stopping correctly.
952 /* Assumes contents are already zeroed. */
953 void init_task_state(TaskState
*ts
)
958 CPUArchState
*cpu_copy(CPUArchState
*env
)
960 CPUState
*cpu
= ENV_GET_CPU(env
);
961 CPUState
*new_cpu
= cpu_create(cpu_type
);
962 CPUArchState
*new_env
= new_cpu
->env_ptr
;
966 /* Reset non arch specific state */
969 memcpy(new_env
, env
, sizeof(CPUArchState
));
971 /* Clone all break/watchpoints.
972 Note: Once we support ptrace with hw-debug register access, make sure
973 BP_CPU break/watchpoints are handled correctly on clone. */
974 QTAILQ_INIT(&new_cpu
->breakpoints
);
975 QTAILQ_INIT(&new_cpu
->watchpoints
);
976 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
977 cpu_breakpoint_insert(new_cpu
, bp
->pc
, bp
->flags
, NULL
);
979 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
980 cpu_watchpoint_insert(new_cpu
, wp
->vaddr
, wp
->len
, wp
->flags
, NULL
);
986 static void handle_arg_help(const char *arg
)
991 static void handle_arg_log(const char *arg
)
995 mask
= qemu_str_to_log_mask(arg
);
997 qemu_print_log_usage(stdout
);
1000 qemu_log_needs_buffers();
1004 static void handle_arg_dfilter(const char *arg
)
1006 qemu_set_dfilter_ranges(arg
, NULL
);
1009 static void handle_arg_log_filename(const char *arg
)
1011 qemu_set_log_filename(arg
, &error_fatal
);
1014 static void handle_arg_set_env(const char *arg
)
1016 char *r
, *p
, *token
;
1017 r
= p
= strdup(arg
);
1018 while ((token
= strsep(&p
, ",")) != NULL
) {
1019 if (envlist_setenv(envlist
, token
) != 0) {
1020 usage(EXIT_FAILURE
);
1026 static void handle_arg_unset_env(const char *arg
)
1028 char *r
, *p
, *token
;
1029 r
= p
= strdup(arg
);
1030 while ((token
= strsep(&p
, ",")) != NULL
) {
1031 if (envlist_unsetenv(envlist
, token
) != 0) {
1032 usage(EXIT_FAILURE
);
1038 static void handle_arg_argv0(const char *arg
)
1040 argv0
= strdup(arg
);
1043 static void handle_arg_stack_size(const char *arg
)
1046 guest_stack_size
= strtoul(arg
, &p
, 0);
1047 if (guest_stack_size
== 0) {
1048 usage(EXIT_FAILURE
);
1052 guest_stack_size
*= 1024 * 1024;
1053 } else if (*p
== 'k' || *p
== 'K') {
1054 guest_stack_size
*= 1024;
1058 static void handle_arg_ld_prefix(const char *arg
)
1060 interp_prefix
= strdup(arg
);
1063 static void handle_arg_pagesize(const char *arg
)
1065 qemu_host_page_size
= atoi(arg
);
1066 if (qemu_host_page_size
== 0 ||
1067 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
1068 fprintf(stderr
, "page size must be a power of two\n");
1073 static void handle_arg_randseed(const char *arg
)
1075 unsigned long long seed
;
1077 if (parse_uint_full(arg
, &seed
, 0) != 0 || seed
> UINT_MAX
) {
1078 fprintf(stderr
, "Invalid seed number: %s\n", arg
);
1084 static void handle_arg_gdb(const char *arg
)
1086 gdbstub_port
= atoi(arg
);
1089 static void handle_arg_uname(const char *arg
)
1091 qemu_uname_release
= strdup(arg
);
1094 static void handle_arg_cpu(const char *arg
)
1096 cpu_model
= strdup(arg
);
1097 if (cpu_model
== NULL
|| is_help_option(cpu_model
)) {
1098 /* XXX: implement xxx_cpu_list for targets that still miss it */
1099 #if defined(cpu_list)
1100 cpu_list(stdout
, &fprintf
);
1106 static void handle_arg_guest_base(const char *arg
)
1108 guest_base
= strtol(arg
, NULL
, 0);
1109 have_guest_base
= 1;
1112 static void handle_arg_reserved_va(const char *arg
)
1116 reserved_va
= strtoul(arg
, &p
, 0);
1130 unsigned long unshifted
= reserved_va
;
1132 reserved_va
<<= shift
;
1133 if (reserved_va
>> shift
!= unshifted
1134 || (MAX_RESERVED_VA
&& reserved_va
> MAX_RESERVED_VA
)) {
1135 fprintf(stderr
, "Reserved virtual address too big\n");
1140 fprintf(stderr
, "Unrecognised -R size suffix '%s'\n", p
);
1145 static void handle_arg_singlestep(const char *arg
)
1150 static void handle_arg_strace(const char *arg
)
1155 static void handle_arg_version(const char *arg
)
1157 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
1158 "\n" QEMU_COPYRIGHT
"\n");
1162 static char *trace_file
;
1163 static void handle_arg_trace(const char *arg
)
1166 trace_file
= trace_opt_parse(arg
);
1169 struct qemu_argument
{
1173 void (*handle_opt
)(const char *arg
);
1174 const char *example
;
1178 static const struct qemu_argument arg_table
[] = {
1179 {"h", "", false, handle_arg_help
,
1180 "", "print this help"},
1181 {"help", "", false, handle_arg_help
,
1183 {"g", "QEMU_GDB", true, handle_arg_gdb
,
1184 "port", "wait gdb connection to 'port'"},
1185 {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix
,
1186 "path", "set the elf interpreter prefix to 'path'"},
1187 {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size
,
1188 "size", "set the stack size to 'size' bytes"},
1189 {"cpu", "QEMU_CPU", true, handle_arg_cpu
,
1190 "model", "select CPU (-cpu help for list)"},
1191 {"E", "QEMU_SET_ENV", true, handle_arg_set_env
,
1192 "var=value", "sets targets environment variable (see below)"},
1193 {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env
,
1194 "var", "unsets targets environment variable (see below)"},
1195 {"0", "QEMU_ARGV0", true, handle_arg_argv0
,
1196 "argv0", "forces target process argv[0] to be 'argv0'"},
1197 {"r", "QEMU_UNAME", true, handle_arg_uname
,
1198 "uname", "set qemu uname release string to 'uname'"},
1199 {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base
,
1200 "address", "set guest_base address to 'address'"},
1201 {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va
,
1202 "size", "reserve 'size' bytes for guest virtual address space"},
1203 {"d", "QEMU_LOG", true, handle_arg_log
,
1204 "item[,...]", "enable logging of specified items "
1205 "(use '-d help' for a list of items)"},
1206 {"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter
,
1207 "range[,...]","filter logging based on address range"},
1208 {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename
,
1209 "logfile", "write logs to 'logfile' (default stderr)"},
1210 {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize
,
1211 "pagesize", "set the host page size to 'pagesize'"},
1212 {"singlestep", "QEMU_SINGLESTEP", false, handle_arg_singlestep
,
1213 "", "run in singlestep mode"},
1214 {"strace", "QEMU_STRACE", false, handle_arg_strace
,
1215 "", "log system calls"},
1216 {"seed", "QEMU_RAND_SEED", true, handle_arg_randseed
,
1217 "", "Seed for pseudo-random number generator"},
1218 {"trace", "QEMU_TRACE", true, handle_arg_trace
,
1219 "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
1220 {"version", "QEMU_VERSION", false, handle_arg_version
,
1221 "", "display version information and exit"},
1222 {NULL
, NULL
, false, NULL
, NULL
, NULL
}
1225 static void usage(int exitcode
)
1227 const struct qemu_argument
*arginfo
;
1231 printf("usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
1232 "Linux CPU emulator (compiled for " TARGET_NAME
" emulation)\n"
1234 "Options and associated environment variables:\n"
1237 /* Calculate column widths. We must always have at least enough space
1238 * for the column header.
1240 maxarglen
= strlen("Argument");
1241 maxenvlen
= strlen("Env-variable");
1243 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
1244 int arglen
= strlen(arginfo
->argv
);
1245 if (arginfo
->has_arg
) {
1246 arglen
+= strlen(arginfo
->example
) + 1;
1248 if (strlen(arginfo
->env
) > maxenvlen
) {
1249 maxenvlen
= strlen(arginfo
->env
);
1251 if (arglen
> maxarglen
) {
1256 printf("%-*s %-*s Description\n", maxarglen
+1, "Argument",
1257 maxenvlen
, "Env-variable");
1259 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
1260 if (arginfo
->has_arg
) {
1261 printf("-%s %-*s %-*s %s\n", arginfo
->argv
,
1262 (int)(maxarglen
- strlen(arginfo
->argv
) - 1),
1263 arginfo
->example
, maxenvlen
, arginfo
->env
, arginfo
->help
);
1265 printf("-%-*s %-*s %s\n", maxarglen
, arginfo
->argv
,
1266 maxenvlen
, arginfo
->env
,
1273 "QEMU_LD_PREFIX = %s\n"
1274 "QEMU_STACK_SIZE = %ld byte\n",
1279 "You can use -E and -U options or the QEMU_SET_ENV and\n"
1280 "QEMU_UNSET_ENV environment variables to set and unset\n"
1281 "environment variables for the target process.\n"
1282 "It is possible to provide several variables by separating them\n"
1283 "by commas in getsubopt(3) style. Additionally it is possible to\n"
1284 "provide the -E and -U options multiple times.\n"
1285 "The following lines are equivalent:\n"
1286 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
1287 " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
1288 " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
1289 "Note that if you provide several changes to a single variable\n"
1290 "the last change will stay in effect.\n"
1292 QEMU_HELP_BOTTOM
"\n");
1297 static int parse_args(int argc
, char **argv
)
1301 const struct qemu_argument
*arginfo
;
1303 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
1304 if (arginfo
->env
== NULL
) {
1308 r
= getenv(arginfo
->env
);
1310 arginfo
->handle_opt(r
);
1316 if (optind
>= argc
) {
1325 if (!strcmp(r
, "-")) {
1328 /* Treat --foo the same as -foo. */
1333 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
1334 if (!strcmp(r
, arginfo
->argv
)) {
1335 if (arginfo
->has_arg
) {
1336 if (optind
>= argc
) {
1337 (void) fprintf(stderr
,
1338 "qemu: missing argument for option '%s'\n", r
);
1341 arginfo
->handle_opt(argv
[optind
]);
1344 arginfo
->handle_opt(NULL
);
1350 /* no option matched the current argv */
1351 if (arginfo
->handle_opt
== NULL
) {
1352 (void) fprintf(stderr
, "qemu: unknown option '%s'\n", r
);
1357 if (optind
>= argc
) {
1358 (void) fprintf(stderr
, "qemu: no user program specified\n");
1362 filename
= argv
[optind
];
1363 exec_path
= argv
[optind
];
1368 int main(int argc
, char **argv
, char **envp
)
1370 struct target_pt_regs regs1
, *regs
= ®s1
;
1371 struct image_info info1
, *info
= &info1
;
1372 struct linux_binprm bprm
;
1377 char **target_environ
, **wrk
;
1384 module_call_init(MODULE_INIT_TRACE
);
1385 qemu_init_cpu_list();
1386 module_call_init(MODULE_INIT_QOM
);
1388 envlist
= envlist_create();
1390 /* add current environment into the list */
1391 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
1392 (void) envlist_setenv(envlist
, *wrk
);
1395 /* Read the stack limit from the kernel. If it's "unlimited",
1396 then we can do little else besides use the default. */
1399 if (getrlimit(RLIMIT_STACK
, &lim
) == 0
1400 && lim
.rlim_cur
!= RLIM_INFINITY
1401 && lim
.rlim_cur
== (target_long
)lim
.rlim_cur
) {
1402 guest_stack_size
= lim
.rlim_cur
;
1410 qemu_add_opts(&qemu_trace_opts
);
1412 optind
= parse_args(argc
, argv
);
1414 if (!trace_init_backends()) {
1417 trace_init_file(trace_file
);
1420 memset(regs
, 0, sizeof(struct target_pt_regs
));
1422 /* Zero out image_info */
1423 memset(info
, 0, sizeof(struct image_info
));
1425 memset(&bprm
, 0, sizeof (bprm
));
1427 /* Scan interp_prefix dir for replacement files. */
1428 init_paths(interp_prefix
);
1430 init_qemu_uname_release();
1432 execfd
= qemu_getauxval(AT_EXECFD
);
1434 execfd
= open(filename
, O_RDONLY
);
1436 printf("Error while loading %s: %s\n", filename
, strerror(errno
));
1437 _exit(EXIT_FAILURE
);
1441 if (cpu_model
== NULL
) {
1442 cpu_model
= cpu_get_model(get_elf_eflags(execfd
));
1444 cpu_type
= parse_cpu_model(cpu_model
);
1447 /* NOTE: we need to init the CPU at this stage to get
1448 qemu_host_page_size */
1450 cpu
= cpu_create(cpu_type
);
1456 if (getenv("QEMU_STRACE")) {
1460 if (getenv("QEMU_RAND_SEED")) {
1461 handle_arg_randseed(getenv("QEMU_RAND_SEED"));
1464 target_environ
= envlist_to_environ(envlist
, NULL
);
1465 envlist_free(envlist
);
1468 * Now that page sizes are configured in cpu_init() we can do
1469 * proper page alignment for guest_base.
1471 guest_base
= HOST_PAGE_ALIGN(guest_base
);
1473 if (reserved_va
|| have_guest_base
) {
1474 guest_base
= init_guest_space(guest_base
, reserved_va
, 0,
1476 if (guest_base
== (unsigned long)-1) {
1477 fprintf(stderr
, "Unable to reserve 0x%lx bytes of virtual address "
1478 "space for use as guest address space (check your virtual "
1479 "memory ulimit setting or reserve less using -R option)\n",
1485 mmap_next_start
= reserved_va
;
1490 * Read in mmap_min_addr kernel parameter. This value is used
1491 * When loading the ELF image to determine whether guest_base
1492 * is needed. It is also used in mmap_find_vma.
1497 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
1499 if (fscanf(fp
, "%lu", &tmp
) == 1) {
1500 mmap_min_addr
= tmp
;
1501 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
1508 * Prepare copy of argv vector for target.
1510 target_argc
= argc
- optind
;
1511 target_argv
= calloc(target_argc
+ 1, sizeof (char *));
1512 if (target_argv
== NULL
) {
1513 (void) fprintf(stderr
, "Unable to allocate memory for target_argv\n");
1518 * If argv0 is specified (using '-0' switch) we replace
1519 * argv[0] pointer with the given one.
1522 if (argv0
!= NULL
) {
1523 target_argv
[i
++] = strdup(argv0
);
1525 for (; i
< target_argc
; i
++) {
1526 target_argv
[i
] = strdup(argv
[optind
+ i
]);
1528 target_argv
[target_argc
] = NULL
;
1530 ts
= g_new0(TaskState
, 1);
1531 init_task_state(ts
);
1532 /* build Task State */
1538 ret
= loader_exec(execfd
, filename
, target_argv
, target_environ
, regs
,
1541 printf("Error while loading %s: %s\n", filename
, strerror(-ret
));
1542 _exit(EXIT_FAILURE
);
1545 for (wrk
= target_environ
; *wrk
; wrk
++) {
1549 g_free(target_environ
);
1551 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
1552 qemu_log("guest_base 0x%lx\n", guest_base
);
1555 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
1556 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
1557 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n", info
->start_code
);
1558 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n", info
->start_data
);
1559 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
1560 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n", info
->start_stack
);
1561 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
1562 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
1563 qemu_log("argv_start 0x" TARGET_ABI_FMT_lx
"\n", info
->arg_start
);
1564 qemu_log("env_start 0x" TARGET_ABI_FMT_lx
"\n",
1565 info
->arg_end
+ (abi_ulong
)sizeof(abi_ulong
));
1566 qemu_log("auxv_start 0x" TARGET_ABI_FMT_lx
"\n", info
->saved_auxv
);
1569 target_set_brk(info
->brk
);
1573 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
1574 generating the prologue until now so that the prologue can take
1575 the real value of GUEST_BASE into account. */
1576 tcg_prologue_init(tcg_ctx
);
1579 target_cpu_copy_regs(env
, regs
);
1581 #if defined(TARGET_RISCV)
1583 env
->pc
= regs
->sepc
;
1584 env
->gpr
[xSP
] = regs
->sp
;
1586 #elif defined(TARGET_TILEGX)
1589 for (i
= 0; i
< TILEGX_R_COUNT
; i
++) {
1590 env
->regs
[i
] = regs
->regs
[i
];
1592 for (i
= 0; i
< TILEGX_SPR_COUNT
; i
++) {
1597 #elif defined(TARGET_HPPA)
1600 for (i
= 1; i
< 32; i
++) {
1601 env
->gr
[i
] = regs
->gr
[i
];
1603 env
->iaoq_f
= regs
->iaoq
[0];
1604 env
->iaoq_b
= regs
->iaoq
[1];
1606 #elif defined(TARGET_XTENSA)
1609 for (i
= 0; i
< 16; ++i
) {
1610 env
->regs
[i
] = regs
->areg
[i
];
1612 env
->sregs
[WINDOW_START
] = regs
->windowstart
;
1618 if (gdbserver_start(gdbstub_port
) < 0) {
1619 fprintf(stderr
, "qemu: could not open gdbserver on port %d\n",
1623 gdb_handlesig(cpu
, 0);