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
);
153 void cpu_loop(CPUAlphaState
*env
)
155 CPUState
*cs
= CPU(alpha_env_get_cpu(env
));
157 target_siginfo_t info
;
161 bool arch_interrupt
= true;
164 trapnr
= cpu_exec(cs
);
166 process_queued_cpu_work(cs
);
170 fprintf(stderr
, "Reset requested. Exit\n");
174 fprintf(stderr
, "Machine check exception. Exit\n");
177 case EXCP_SMP_INTERRUPT
:
178 case EXCP_CLK_INTERRUPT
:
179 case EXCP_DEV_INTERRUPT
:
180 fprintf(stderr
, "External interrupt. Exit\n");
184 info
.si_signo
= TARGET_SIGSEGV
;
186 info
.si_code
= (page_get_flags(env
->trap_arg0
) & PAGE_VALID
187 ? TARGET_SEGV_ACCERR
: TARGET_SEGV_MAPERR
);
188 info
._sifields
._sigfault
._addr
= env
->trap_arg0
;
189 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
192 info
.si_signo
= TARGET_SIGBUS
;
194 info
.si_code
= TARGET_BUS_ADRALN
;
195 info
._sifields
._sigfault
._addr
= env
->trap_arg0
;
196 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
200 info
.si_signo
= TARGET_SIGILL
;
202 info
.si_code
= TARGET_ILL_ILLOPC
;
203 info
._sifields
._sigfault
._addr
= env
->pc
;
204 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
207 info
.si_signo
= TARGET_SIGFPE
;
209 info
.si_code
= TARGET_FPE_FLTINV
;
210 info
._sifields
._sigfault
._addr
= env
->pc
;
211 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
214 /* No-op. Linux simply re-enables the FPU. */
217 switch (env
->error_code
) {
220 info
.si_signo
= TARGET_SIGTRAP
;
222 info
.si_code
= TARGET_TRAP_BRKPT
;
223 info
._sifields
._sigfault
._addr
= env
->pc
;
224 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
228 info
.si_signo
= TARGET_SIGTRAP
;
231 info
._sifields
._sigfault
._addr
= env
->pc
;
232 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
236 trapnr
= env
->ir
[IR_V0
];
237 sysret
= do_syscall(env
, trapnr
,
238 env
->ir
[IR_A0
], env
->ir
[IR_A1
],
239 env
->ir
[IR_A2
], env
->ir
[IR_A3
],
240 env
->ir
[IR_A4
], env
->ir
[IR_A5
],
242 if (sysret
== -TARGET_ERESTARTSYS
) {
246 if (sysret
== -TARGET_QEMU_ESIGRETURN
) {
249 /* Syscall writes 0 to V0 to bypass error check, similar
250 to how this is handled internal to Linux kernel.
251 (Ab)use trapnr temporarily as boolean indicating error. */
252 trapnr
= (env
->ir
[IR_V0
] != 0 && sysret
< 0);
253 env
->ir
[IR_V0
] = (trapnr
? -sysret
: sysret
);
254 env
->ir
[IR_A3
] = trapnr
;
258 /* ??? We can probably elide the code using page_unprotect
259 that is checking for self-modifying code. Instead we
260 could simply call tb_flush here. Until we work out the
261 changes required to turn off the extra write protection,
262 this can be a no-op. */
266 /* Handled in the translator for usermode. */
270 /* Handled in the translator for usermode. */
274 info
.si_signo
= TARGET_SIGFPE
;
275 switch (env
->ir
[IR_A0
]) {
276 case TARGET_GEN_INTOVF
:
277 info
.si_code
= TARGET_FPE_INTOVF
;
279 case TARGET_GEN_INTDIV
:
280 info
.si_code
= TARGET_FPE_INTDIV
;
282 case TARGET_GEN_FLTOVF
:
283 info
.si_code
= TARGET_FPE_FLTOVF
;
285 case TARGET_GEN_FLTUND
:
286 info
.si_code
= TARGET_FPE_FLTUND
;
288 case TARGET_GEN_FLTINV
:
289 info
.si_code
= TARGET_FPE_FLTINV
;
291 case TARGET_GEN_FLTINE
:
292 info
.si_code
= TARGET_FPE_FLTRES
;
294 case TARGET_GEN_ROPRAND
:
298 info
.si_signo
= TARGET_SIGTRAP
;
303 info
._sifields
._sigfault
._addr
= env
->pc
;
304 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
311 info
.si_signo
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
314 info
.si_code
= TARGET_TRAP_BRKPT
;
315 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
317 arch_interrupt
= false;
321 /* Just indicate that signals should be handled asap. */
324 cpu_exec_step_atomic(cs
);
325 arch_interrupt
= false;
328 printf ("Unhandled trap: 0x%x\n", trapnr
);
329 cpu_dump_state(cs
, stderr
, fprintf
, 0);
332 process_pending_signals (env
);
334 /* Most of the traps imply a transition through PALcode, which
335 implies an REI instruction has been executed. Which means
336 that RX and LOCK_ADDR should be cleared. But there are a
337 few exceptions for traps internal to QEMU. */
338 if (arch_interrupt
) {
339 env
->flags
&= ~ENV_FLAG_RX_FLAG
;
344 #endif /* TARGET_ALPHA */
348 /* s390x masks the fault address it reports in si_addr for SIGSEGV and SIGBUS */
349 #define S390X_FAIL_ADDR_MASK -4096LL
351 void cpu_loop(CPUS390XState
*env
)
353 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
355 target_siginfo_t info
;
361 trapnr
= cpu_exec(cs
);
363 process_queued_cpu_work(cs
);
367 /* Just indicate that signals should be handled asap. */
371 n
= env
->int_svc_code
;
376 env
->psw
.addr
+= env
->int_svc_ilen
;
377 ret
= do_syscall(env
, n
, env
->regs
[2], env
->regs
[3],
378 env
->regs
[4], env
->regs
[5],
379 env
->regs
[6], env
->regs
[7], 0, 0);
380 if (ret
== -TARGET_ERESTARTSYS
) {
381 env
->psw
.addr
-= env
->int_svc_ilen
;
382 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
388 sig
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
390 n
= TARGET_TRAP_BRKPT
;
395 n
= env
->int_pgm_code
;
400 n
= TARGET_ILL_ILLOPC
;
404 sig
= TARGET_SIGSEGV
;
405 /* XXX: check env->error_code */
406 n
= TARGET_SEGV_MAPERR
;
407 addr
= env
->__excp_addr
& S390X_FAIL_ADDR_MASK
;
410 case PGM_SPECIFICATION
:
415 n
= TARGET_ILL_ILLOPN
;
418 case PGM_FIXPT_OVERFLOW
:
420 n
= TARGET_FPE_INTOVF
;
422 case PGM_FIXPT_DIVIDE
:
424 n
= TARGET_FPE_INTDIV
;
428 n
= (env
->fpc
>> 8) & 0xff;
430 /* compare-and-trap */
433 /* An IEEE exception, simulated or otherwise. */
435 n
= TARGET_FPE_FLTINV
;
436 } else if (n
& 0x40) {
437 n
= TARGET_FPE_FLTDIV
;
438 } else if (n
& 0x20) {
439 n
= TARGET_FPE_FLTOVF
;
440 } else if (n
& 0x10) {
441 n
= TARGET_FPE_FLTUND
;
442 } else if (n
& 0x08) {
443 n
= TARGET_FPE_FLTRES
;
445 /* ??? Quantum exception; BFP, DFP error. */
453 fprintf(stderr
, "Unhandled program exception: %#x\n", n
);
454 cpu_dump_state(cs
, stderr
, fprintf
, 0);
460 addr
= env
->psw
.addr
;
465 info
._sifields
._sigfault
._addr
= addr
;
466 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
470 cpu_exec_step_atomic(cs
);
473 fprintf(stderr
, "Unhandled trap: 0x%x\n", trapnr
);
474 cpu_dump_state(cs
, stderr
, fprintf
, 0);
477 process_pending_signals (env
);
481 #endif /* TARGET_S390X */
485 static void gen_sigill_reg(CPUTLGState
*env
)
487 target_siginfo_t info
;
489 info
.si_signo
= TARGET_SIGILL
;
491 info
.si_code
= TARGET_ILL_PRVREG
;
492 info
._sifields
._sigfault
._addr
= env
->pc
;
493 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
496 static void do_signal(CPUTLGState
*env
, int signo
, int sigcode
)
498 target_siginfo_t info
;
500 info
.si_signo
= signo
;
502 info
._sifields
._sigfault
._addr
= env
->pc
;
504 if (signo
== TARGET_SIGSEGV
) {
505 /* The passed in sigcode is a dummy; check for a page mapping
506 and pass either MAPERR or ACCERR. */
507 target_ulong addr
= env
->excaddr
;
508 info
._sifields
._sigfault
._addr
= addr
;
509 if (page_check_range(addr
, 1, PAGE_VALID
) < 0) {
510 sigcode
= TARGET_SEGV_MAPERR
;
512 sigcode
= TARGET_SEGV_ACCERR
;
515 info
.si_code
= sigcode
;
517 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
520 static void gen_sigsegv_maperr(CPUTLGState
*env
, target_ulong addr
)
523 do_signal(env
, TARGET_SIGSEGV
, 0);
526 static void set_regval(CPUTLGState
*env
, uint8_t reg
, uint64_t val
)
528 if (unlikely(reg
>= TILEGX_R_COUNT
)) {
542 g_assert_not_reached();
545 env
->regs
[reg
] = val
;
549 * Compare the 8-byte contents of the CmpValue SPR with the 8-byte value in
550 * memory at the address held in the first source register. If the values are
551 * not equal, then no memory operation is performed. If the values are equal,
552 * the 8-byte quantity from the second source register is written into memory
553 * at the address held in the first source register. In either case, the result
554 * of the instruction is the value read from memory. The compare and write to
555 * memory are atomic and thus can be used for synchronization purposes. This
556 * instruction only operates for addresses aligned to a 8-byte boundary.
557 * Unaligned memory access causes an Unaligned Data Reference interrupt.
559 * Functional Description (64-bit)
560 * uint64_t memVal = memoryReadDoubleWord (rf[SrcA]);
562 * if (memVal == SPR[CmpValueSPR])
563 * memoryWriteDoubleWord (rf[SrcA], rf[SrcB]);
565 * Functional Description (32-bit)
566 * uint64_t memVal = signExtend32 (memoryReadWord (rf[SrcA]));
568 * if (memVal == signExtend32 (SPR[CmpValueSPR]))
569 * memoryWriteWord (rf[SrcA], rf[SrcB]);
572 * This function also processes exch and exch4 which need not process SPR.
574 static void do_exch(CPUTLGState
*env
, bool quad
, bool cmp
)
577 target_long val
, sprval
;
581 addr
= env
->atomic_srca
;
582 if (quad
? get_user_s64(val
, addr
) : get_user_s32(val
, addr
)) {
588 sprval
= env
->spregs
[TILEGX_SPR_CMPEXCH
];
590 sprval
= sextract64(env
->spregs
[TILEGX_SPR_CMPEXCH
], 0, 32);
594 if (!cmp
|| val
== sprval
) {
595 target_long valb
= env
->atomic_srcb
;
596 if (quad
? put_user_u64(valb
, addr
) : put_user_u32(valb
, addr
)) {
601 set_regval(env
, env
->atomic_dstr
, val
);
607 gen_sigsegv_maperr(env
, addr
);
610 static void do_fetch(CPUTLGState
*env
, int trapnr
, bool quad
)
614 target_long val
, valb
;
618 addr
= env
->atomic_srca
;
619 valb
= env
->atomic_srcb
;
620 if (quad
? get_user_s64(val
, addr
) : get_user_s32(val
, addr
)) {
625 case TILEGX_EXCP_OPCODE_FETCHADD
:
626 case TILEGX_EXCP_OPCODE_FETCHADD4
:
629 case TILEGX_EXCP_OPCODE_FETCHADDGEZ
:
635 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4
:
637 if ((int32_t)valb
< 0) {
641 case TILEGX_EXCP_OPCODE_FETCHAND
:
642 case TILEGX_EXCP_OPCODE_FETCHAND4
:
645 case TILEGX_EXCP_OPCODE_FETCHOR
:
646 case TILEGX_EXCP_OPCODE_FETCHOR4
:
650 g_assert_not_reached();
654 if (quad
? put_user_u64(valb
, addr
) : put_user_u32(valb
, addr
)) {
659 set_regval(env
, env
->atomic_dstr
, val
);
665 gen_sigsegv_maperr(env
, addr
);
668 void cpu_loop(CPUTLGState
*env
)
670 CPUState
*cs
= CPU(tilegx_env_get_cpu(env
));
675 trapnr
= cpu_exec(cs
);
677 process_queued_cpu_work(cs
);
680 case TILEGX_EXCP_SYSCALL
:
682 abi_ulong ret
= do_syscall(env
, env
->regs
[TILEGX_R_NR
],
683 env
->regs
[0], env
->regs
[1],
684 env
->regs
[2], env
->regs
[3],
685 env
->regs
[4], env
->regs
[5],
686 env
->regs
[6], env
->regs
[7]);
687 if (ret
== -TARGET_ERESTARTSYS
) {
689 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
690 env
->regs
[TILEGX_R_RE
] = ret
;
691 env
->regs
[TILEGX_R_ERR
] = TILEGX_IS_ERRNO(ret
) ? -ret
: 0;
695 case TILEGX_EXCP_OPCODE_EXCH
:
696 do_exch(env
, true, false);
698 case TILEGX_EXCP_OPCODE_EXCH4
:
699 do_exch(env
, false, false);
701 case TILEGX_EXCP_OPCODE_CMPEXCH
:
702 do_exch(env
, true, true);
704 case TILEGX_EXCP_OPCODE_CMPEXCH4
:
705 do_exch(env
, false, true);
707 case TILEGX_EXCP_OPCODE_FETCHADD
:
708 case TILEGX_EXCP_OPCODE_FETCHADDGEZ
:
709 case TILEGX_EXCP_OPCODE_FETCHAND
:
710 case TILEGX_EXCP_OPCODE_FETCHOR
:
711 do_fetch(env
, trapnr
, true);
713 case TILEGX_EXCP_OPCODE_FETCHADD4
:
714 case TILEGX_EXCP_OPCODE_FETCHADDGEZ4
:
715 case TILEGX_EXCP_OPCODE_FETCHAND4
:
716 case TILEGX_EXCP_OPCODE_FETCHOR4
:
717 do_fetch(env
, trapnr
, false);
719 case TILEGX_EXCP_SIGNAL
:
720 do_signal(env
, env
->signo
, env
->sigcode
);
722 case TILEGX_EXCP_REG_IDN_ACCESS
:
723 case TILEGX_EXCP_REG_UDN_ACCESS
:
727 cpu_exec_step_atomic(cs
);
730 fprintf(stderr
, "trapnr is %d[0x%x].\n", trapnr
, trapnr
);
731 g_assert_not_reached();
733 process_pending_signals(env
);
741 void cpu_loop(CPURISCVState
*env
)
743 CPUState
*cs
= CPU(riscv_env_get_cpu(env
));
744 int trapnr
, signum
, sigcode
;
745 target_ulong sigaddr
;
750 trapnr
= cpu_exec(cs
);
752 process_queued_cpu_work(cs
);
760 /* just indicate that signals should be handled asap */
763 cpu_exec_step_atomic(cs
);
765 case RISCV_EXCP_U_ECALL
:
767 if (env
->gpr
[xA7
] == TARGET_NR_arch_specific_syscall
+ 15) {
768 /* riscv_flush_icache_syscall is a no-op in QEMU as
769 self-modifying code is automatically detected */
772 ret
= do_syscall(env
,
782 if (ret
== -TARGET_ERESTARTSYS
) {
784 } else if (ret
!= -TARGET_QEMU_ESIGRETURN
) {
787 if (cs
->singlestep_enabled
) {
791 case RISCV_EXCP_ILLEGAL_INST
:
792 signum
= TARGET_SIGILL
;
793 sigcode
= TARGET_ILL_ILLOPC
;
795 case RISCV_EXCP_BREAKPOINT
:
796 signum
= TARGET_SIGTRAP
;
797 sigcode
= TARGET_TRAP_BRKPT
;
800 case RISCV_EXCP_INST_PAGE_FAULT
:
801 case RISCV_EXCP_LOAD_PAGE_FAULT
:
802 case RISCV_EXCP_STORE_PAGE_FAULT
:
803 signum
= TARGET_SIGSEGV
;
804 sigcode
= TARGET_SEGV_MAPERR
;
808 signum
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
809 sigcode
= TARGET_TRAP_BRKPT
;
812 EXCP_DUMP(env
, "\nqemu: unhandled CPU exception %#x - aborting\n",
818 target_siginfo_t info
= {
822 ._sifields
._sigfault
._addr
= sigaddr
824 queue_signal(env
, info
.si_signo
, QEMU_SI_KILL
, &info
);
827 process_pending_signals(env
);
831 #endif /* TARGET_RISCV */
835 static abi_ulong
hppa_lws(CPUHPPAState
*env
)
837 uint32_t which
= env
->gr
[20];
838 abi_ulong addr
= env
->gr
[26];
839 abi_ulong old
= env
->gr
[25];
840 abi_ulong
new = env
->gr
[24];
845 return -TARGET_ENOSYS
;
847 case 0: /* elf32 atomic 32bit cmpxchg */
848 if ((addr
& 3) || !access_ok(VERIFY_WRITE
, addr
, 4)) {
849 return -TARGET_EFAULT
;
853 ret
= atomic_cmpxchg((uint32_t *)g2h(addr
), old
, new);
857 case 2: /* elf32 atomic "new" cmpxchg */
860 return -TARGET_ENOSYS
;
862 if (((addr
| old
| new) & ((1 << size
) - 1))
863 || !access_ok(VERIFY_WRITE
, addr
, 1 << size
)
864 || !access_ok(VERIFY_READ
, old
, 1 << size
)
865 || !access_ok(VERIFY_READ
, new, 1 << size
)) {
866 return -TARGET_EFAULT
;
868 /* Note that below we use host-endian loads so that the cmpxchg
869 can be host-endian as well. */
872 old
= *(uint8_t *)g2h(old
);
873 new = *(uint8_t *)g2h(new);
874 ret
= atomic_cmpxchg((uint8_t *)g2h(addr
), old
, new);
878 old
= *(uint16_t *)g2h(old
);
879 new = *(uint16_t *)g2h(new);
880 ret
= atomic_cmpxchg((uint16_t *)g2h(addr
), old
, new);
884 old
= *(uint32_t *)g2h(old
);
885 new = *(uint32_t *)g2h(new);
886 ret
= atomic_cmpxchg((uint32_t *)g2h(addr
), old
, new);
891 uint64_t o64
, n64
, r64
;
892 o64
= *(uint64_t *)g2h(old
);
893 n64
= *(uint64_t *)g2h(new);
894 #ifdef CONFIG_ATOMIC64
895 r64
= atomic_cmpxchg__nocheck((uint64_t *)g2h(addr
), o64
, n64
);
899 r64
= *(uint64_t *)g2h(addr
);
902 *(uint64_t *)g2h(addr
) = n64
;
917 void cpu_loop(CPUHPPAState
*env
)
919 CPUState
*cs
= CPU(hppa_env_get_cpu(env
));
920 target_siginfo_t info
;
926 trapnr
= cpu_exec(cs
);
928 process_queued_cpu_work(cs
);
932 ret
= do_syscall(env
, env
->gr
[20],
933 env
->gr
[26], env
->gr
[25],
934 env
->gr
[24], env
->gr
[23],
935 env
->gr
[22], env
->gr
[21], 0, 0);
939 /* We arrived here by faking the gateway page. Return. */
940 env
->iaoq_f
= env
->gr
[31];
941 env
->iaoq_b
= env
->gr
[31] + 4;
943 case -TARGET_ERESTARTSYS
:
944 case -TARGET_QEMU_ESIGRETURN
:
948 case EXCP_SYSCALL_LWS
:
949 env
->gr
[21] = hppa_lws(env
);
950 /* We arrived here by faking the gateway page. Return. */
951 env
->iaoq_f
= env
->gr
[31];
952 env
->iaoq_b
= env
->gr
[31] + 4;
956 case EXCP_NA_ITLB_MISS
:
957 case EXCP_NA_DTLB_MISS
:
964 info
.si_signo
= TARGET_SIGSEGV
;
966 info
.si_code
= TARGET_SEGV_ACCERR
;
967 info
._sifields
._sigfault
._addr
= env
->cr
[CR_IOR
];
968 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
971 info
.si_signo
= TARGET_SIGBUS
;
974 info
._sifields
._sigfault
._addr
= env
->cr
[CR_IOR
];
975 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
980 info
.si_signo
= TARGET_SIGILL
;
982 info
.si_code
= TARGET_ILL_ILLOPN
;
983 info
._sifields
._sigfault
._addr
= env
->iaoq_f
;
984 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
989 info
.si_signo
= TARGET_SIGFPE
;
992 info
._sifields
._sigfault
._addr
= env
->iaoq_f
;
993 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
996 trapnr
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
998 info
.si_signo
= trapnr
;
1000 info
.si_code
= TARGET_TRAP_BRKPT
;
1001 queue_signal(env
, trapnr
, QEMU_SI_FAULT
, &info
);
1004 case EXCP_INTERRUPT
:
1005 /* just indicate that signals should be handled asap */
1008 g_assert_not_reached();
1010 process_pending_signals(env
);
1014 #endif /* TARGET_HPPA */
1016 #ifdef TARGET_XTENSA
1018 static void xtensa_rfw(CPUXtensaState
*env
)
1020 xtensa_restore_owb(env
);
1021 env
->pc
= env
->sregs
[EPC1
];
1024 static void xtensa_rfwu(CPUXtensaState
*env
)
1026 env
->sregs
[WINDOW_START
] |= (1 << env
->sregs
[WINDOW_BASE
]);
1030 static void xtensa_rfwo(CPUXtensaState
*env
)
1032 env
->sregs
[WINDOW_START
] &= ~(1 << env
->sregs
[WINDOW_BASE
]);
1036 static void xtensa_overflow4(CPUXtensaState
*env
)
1038 put_user_ual(env
->regs
[0], env
->regs
[5] - 16);
1039 put_user_ual(env
->regs
[1], env
->regs
[5] - 12);
1040 put_user_ual(env
->regs
[2], env
->regs
[5] - 8);
1041 put_user_ual(env
->regs
[3], env
->regs
[5] - 4);
1045 static void xtensa_underflow4(CPUXtensaState
*env
)
1047 get_user_ual(env
->regs
[0], env
->regs
[5] - 16);
1048 get_user_ual(env
->regs
[1], env
->regs
[5] - 12);
1049 get_user_ual(env
->regs
[2], env
->regs
[5] - 8);
1050 get_user_ual(env
->regs
[3], env
->regs
[5] - 4);
1054 static void xtensa_overflow8(CPUXtensaState
*env
)
1056 put_user_ual(env
->regs
[0], env
->regs
[9] - 16);
1057 get_user_ual(env
->regs
[0], env
->regs
[1] - 12);
1058 put_user_ual(env
->regs
[1], env
->regs
[9] - 12);
1059 put_user_ual(env
->regs
[2], env
->regs
[9] - 8);
1060 put_user_ual(env
->regs
[3], env
->regs
[9] - 4);
1061 put_user_ual(env
->regs
[4], env
->regs
[0] - 32);
1062 put_user_ual(env
->regs
[5], env
->regs
[0] - 28);
1063 put_user_ual(env
->regs
[6], env
->regs
[0] - 24);
1064 put_user_ual(env
->regs
[7], env
->regs
[0] - 20);
1068 static void xtensa_underflow8(CPUXtensaState
*env
)
1070 get_user_ual(env
->regs
[0], env
->regs
[9] - 16);
1071 get_user_ual(env
->regs
[1], env
->regs
[9] - 12);
1072 get_user_ual(env
->regs
[2], env
->regs
[9] - 8);
1073 get_user_ual(env
->regs
[7], env
->regs
[1] - 12);
1074 get_user_ual(env
->regs
[3], env
->regs
[9] - 4);
1075 get_user_ual(env
->regs
[4], env
->regs
[7] - 32);
1076 get_user_ual(env
->regs
[5], env
->regs
[7] - 28);
1077 get_user_ual(env
->regs
[6], env
->regs
[7] - 24);
1078 get_user_ual(env
->regs
[7], env
->regs
[7] - 20);
1082 static void xtensa_overflow12(CPUXtensaState
*env
)
1084 put_user_ual(env
->regs
[0], env
->regs
[13] - 16);
1085 get_user_ual(env
->regs
[0], env
->regs
[1] - 12);
1086 put_user_ual(env
->regs
[1], env
->regs
[13] - 12);
1087 put_user_ual(env
->regs
[2], env
->regs
[13] - 8);
1088 put_user_ual(env
->regs
[3], env
->regs
[13] - 4);
1089 put_user_ual(env
->regs
[4], env
->regs
[0] - 48);
1090 put_user_ual(env
->regs
[5], env
->regs
[0] - 44);
1091 put_user_ual(env
->regs
[6], env
->regs
[0] - 40);
1092 put_user_ual(env
->regs
[7], env
->regs
[0] - 36);
1093 put_user_ual(env
->regs
[8], env
->regs
[0] - 32);
1094 put_user_ual(env
->regs
[9], env
->regs
[0] - 28);
1095 put_user_ual(env
->regs
[10], env
->regs
[0] - 24);
1096 put_user_ual(env
->regs
[11], env
->regs
[0] - 20);
1100 static void xtensa_underflow12(CPUXtensaState
*env
)
1102 get_user_ual(env
->regs
[0], env
->regs
[13] - 16);
1103 get_user_ual(env
->regs
[1], env
->regs
[13] - 12);
1104 get_user_ual(env
->regs
[2], env
->regs
[13] - 8);
1105 get_user_ual(env
->regs
[11], env
->regs
[1] - 12);
1106 get_user_ual(env
->regs
[3], env
->regs
[13] - 4);
1107 get_user_ual(env
->regs
[4], env
->regs
[11] - 48);
1108 get_user_ual(env
->regs
[5], env
->regs
[11] - 44);
1109 get_user_ual(env
->regs
[6], env
->regs
[11] - 40);
1110 get_user_ual(env
->regs
[7], env
->regs
[11] - 36);
1111 get_user_ual(env
->regs
[8], env
->regs
[11] - 32);
1112 get_user_ual(env
->regs
[9], env
->regs
[11] - 28);
1113 get_user_ual(env
->regs
[10], env
->regs
[11] - 24);
1114 get_user_ual(env
->regs
[11], env
->regs
[11] - 20);
1118 void cpu_loop(CPUXtensaState
*env
)
1120 CPUState
*cs
= CPU(xtensa_env_get_cpu(env
));
1121 target_siginfo_t info
;
1127 trapnr
= cpu_exec(cs
);
1129 process_queued_cpu_work(cs
);
1131 env
->sregs
[PS
] &= ~PS_EXCM
;
1133 case EXCP_INTERRUPT
:
1136 case EXC_WINDOW_OVERFLOW4
:
1137 xtensa_overflow4(env
);
1139 case EXC_WINDOW_UNDERFLOW4
:
1140 xtensa_underflow4(env
);
1142 case EXC_WINDOW_OVERFLOW8
:
1143 xtensa_overflow8(env
);
1145 case EXC_WINDOW_UNDERFLOW8
:
1146 xtensa_underflow8(env
);
1148 case EXC_WINDOW_OVERFLOW12
:
1149 xtensa_overflow12(env
);
1151 case EXC_WINDOW_UNDERFLOW12
:
1152 xtensa_underflow12(env
);
1156 switch (env
->sregs
[EXCCAUSE
]) {
1157 case ILLEGAL_INSTRUCTION_CAUSE
:
1158 case PRIVILEGED_CAUSE
:
1159 info
.si_signo
= TARGET_SIGILL
;
1162 env
->sregs
[EXCCAUSE
] == ILLEGAL_INSTRUCTION_CAUSE
?
1163 TARGET_ILL_ILLOPC
: TARGET_ILL_PRVOPC
;
1164 info
._sifields
._sigfault
._addr
= env
->sregs
[EPC1
];
1165 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
1170 ret
= do_syscall(env
, env
->regs
[2],
1171 env
->regs
[6], env
->regs
[3],
1172 env
->regs
[4], env
->regs
[5],
1173 env
->regs
[8], env
->regs
[9], 0, 0);
1179 case -TARGET_ERESTARTSYS
:
1183 case -TARGET_QEMU_ESIGRETURN
:
1189 env
->sregs
[PS
] = deposit32(env
->sregs
[PS
],
1192 env
->sregs
[WINDOW_BASE
]);
1194 switch (env
->regs
[0] & 0xc0000000) {
1197 xtensa_rotate_window(env
, -1);
1198 xtensa_underflow4(env
);
1202 xtensa_rotate_window(env
, -2);
1203 xtensa_underflow8(env
);
1207 xtensa_rotate_window(env
, -3);
1208 xtensa_underflow12(env
);
1213 case INTEGER_DIVIDE_BY_ZERO_CAUSE
:
1214 info
.si_signo
= TARGET_SIGFPE
;
1216 info
.si_code
= TARGET_FPE_INTDIV
;
1217 info
._sifields
._sigfault
._addr
= env
->sregs
[EPC1
];
1218 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
1221 case LOAD_PROHIBITED_CAUSE
:
1222 case STORE_PROHIBITED_CAUSE
:
1223 info
.si_signo
= TARGET_SIGSEGV
;
1225 info
.si_code
= TARGET_SEGV_ACCERR
;
1226 info
._sifields
._sigfault
._addr
= env
->sregs
[EXCVADDR
];
1227 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
1231 fprintf(stderr
, "exccause = %d\n", env
->sregs
[EXCCAUSE
]);
1232 g_assert_not_reached();
1236 trapnr
= gdb_handlesig(cs
, TARGET_SIGTRAP
);
1238 info
.si_signo
= trapnr
;
1240 info
.si_code
= TARGET_TRAP_BRKPT
;
1241 queue_signal(env
, trapnr
, QEMU_SI_FAULT
, &info
);
1246 fprintf(stderr
, "trapnr = %d\n", trapnr
);
1247 g_assert_not_reached();
1249 process_pending_signals(env
);
1253 #endif /* TARGET_XTENSA */
1255 __thread CPUState
*thread_cpu
;
1257 bool qemu_cpu_is_self(CPUState
*cpu
)
1259 return thread_cpu
== cpu
;
1262 void qemu_cpu_kick(CPUState
*cpu
)
1267 void task_settid(TaskState
*ts
)
1269 if (ts
->ts_tid
== 0) {
1270 ts
->ts_tid
= (pid_t
)syscall(SYS_gettid
);
1274 void stop_all_tasks(void)
1277 * We trust that when using NPTL, start_exclusive()
1278 * handles thread stopping correctly.
1283 /* Assumes contents are already zeroed. */
1284 void init_task_state(TaskState
*ts
)
1289 CPUArchState
*cpu_copy(CPUArchState
*env
)
1291 CPUState
*cpu
= ENV_GET_CPU(env
);
1292 CPUState
*new_cpu
= cpu_create(cpu_type
);
1293 CPUArchState
*new_env
= new_cpu
->env_ptr
;
1297 /* Reset non arch specific state */
1300 memcpy(new_env
, env
, sizeof(CPUArchState
));
1302 /* Clone all break/watchpoints.
1303 Note: Once we support ptrace with hw-debug register access, make sure
1304 BP_CPU break/watchpoints are handled correctly on clone. */
1305 QTAILQ_INIT(&new_cpu
->breakpoints
);
1306 QTAILQ_INIT(&new_cpu
->watchpoints
);
1307 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
1308 cpu_breakpoint_insert(new_cpu
, bp
->pc
, bp
->flags
, NULL
);
1310 QTAILQ_FOREACH(wp
, &cpu
->watchpoints
, entry
) {
1311 cpu_watchpoint_insert(new_cpu
, wp
->vaddr
, wp
->len
, wp
->flags
, NULL
);
1317 static void handle_arg_help(const char *arg
)
1319 usage(EXIT_SUCCESS
);
1322 static void handle_arg_log(const char *arg
)
1326 mask
= qemu_str_to_log_mask(arg
);
1328 qemu_print_log_usage(stdout
);
1331 qemu_log_needs_buffers();
1335 static void handle_arg_dfilter(const char *arg
)
1337 qemu_set_dfilter_ranges(arg
, NULL
);
1340 static void handle_arg_log_filename(const char *arg
)
1342 qemu_set_log_filename(arg
, &error_fatal
);
1345 static void handle_arg_set_env(const char *arg
)
1347 char *r
, *p
, *token
;
1348 r
= p
= strdup(arg
);
1349 while ((token
= strsep(&p
, ",")) != NULL
) {
1350 if (envlist_setenv(envlist
, token
) != 0) {
1351 usage(EXIT_FAILURE
);
1357 static void handle_arg_unset_env(const char *arg
)
1359 char *r
, *p
, *token
;
1360 r
= p
= strdup(arg
);
1361 while ((token
= strsep(&p
, ",")) != NULL
) {
1362 if (envlist_unsetenv(envlist
, token
) != 0) {
1363 usage(EXIT_FAILURE
);
1369 static void handle_arg_argv0(const char *arg
)
1371 argv0
= strdup(arg
);
1374 static void handle_arg_stack_size(const char *arg
)
1377 guest_stack_size
= strtoul(arg
, &p
, 0);
1378 if (guest_stack_size
== 0) {
1379 usage(EXIT_FAILURE
);
1383 guest_stack_size
*= 1024 * 1024;
1384 } else if (*p
== 'k' || *p
== 'K') {
1385 guest_stack_size
*= 1024;
1389 static void handle_arg_ld_prefix(const char *arg
)
1391 interp_prefix
= strdup(arg
);
1394 static void handle_arg_pagesize(const char *arg
)
1396 qemu_host_page_size
= atoi(arg
);
1397 if (qemu_host_page_size
== 0 ||
1398 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
1399 fprintf(stderr
, "page size must be a power of two\n");
1404 static void handle_arg_randseed(const char *arg
)
1406 unsigned long long seed
;
1408 if (parse_uint_full(arg
, &seed
, 0) != 0 || seed
> UINT_MAX
) {
1409 fprintf(stderr
, "Invalid seed number: %s\n", arg
);
1415 static void handle_arg_gdb(const char *arg
)
1417 gdbstub_port
= atoi(arg
);
1420 static void handle_arg_uname(const char *arg
)
1422 qemu_uname_release
= strdup(arg
);
1425 static void handle_arg_cpu(const char *arg
)
1427 cpu_model
= strdup(arg
);
1428 if (cpu_model
== NULL
|| is_help_option(cpu_model
)) {
1429 /* XXX: implement xxx_cpu_list for targets that still miss it */
1430 #if defined(cpu_list)
1431 cpu_list(stdout
, &fprintf
);
1437 static void handle_arg_guest_base(const char *arg
)
1439 guest_base
= strtol(arg
, NULL
, 0);
1440 have_guest_base
= 1;
1443 static void handle_arg_reserved_va(const char *arg
)
1447 reserved_va
= strtoul(arg
, &p
, 0);
1461 unsigned long unshifted
= reserved_va
;
1463 reserved_va
<<= shift
;
1464 if (reserved_va
>> shift
!= unshifted
1465 || (MAX_RESERVED_VA
&& reserved_va
> MAX_RESERVED_VA
)) {
1466 fprintf(stderr
, "Reserved virtual address too big\n");
1471 fprintf(stderr
, "Unrecognised -R size suffix '%s'\n", p
);
1476 static void handle_arg_singlestep(const char *arg
)
1481 static void handle_arg_strace(const char *arg
)
1486 static void handle_arg_version(const char *arg
)
1488 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
1489 "\n" QEMU_COPYRIGHT
"\n");
1493 static char *trace_file
;
1494 static void handle_arg_trace(const char *arg
)
1497 trace_file
= trace_opt_parse(arg
);
1500 struct qemu_argument
{
1504 void (*handle_opt
)(const char *arg
);
1505 const char *example
;
1509 static const struct qemu_argument arg_table
[] = {
1510 {"h", "", false, handle_arg_help
,
1511 "", "print this help"},
1512 {"help", "", false, handle_arg_help
,
1514 {"g", "QEMU_GDB", true, handle_arg_gdb
,
1515 "port", "wait gdb connection to 'port'"},
1516 {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix
,
1517 "path", "set the elf interpreter prefix to 'path'"},
1518 {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size
,
1519 "size", "set the stack size to 'size' bytes"},
1520 {"cpu", "QEMU_CPU", true, handle_arg_cpu
,
1521 "model", "select CPU (-cpu help for list)"},
1522 {"E", "QEMU_SET_ENV", true, handle_arg_set_env
,
1523 "var=value", "sets targets environment variable (see below)"},
1524 {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env
,
1525 "var", "unsets targets environment variable (see below)"},
1526 {"0", "QEMU_ARGV0", true, handle_arg_argv0
,
1527 "argv0", "forces target process argv[0] to be 'argv0'"},
1528 {"r", "QEMU_UNAME", true, handle_arg_uname
,
1529 "uname", "set qemu uname release string to 'uname'"},
1530 {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base
,
1531 "address", "set guest_base address to 'address'"},
1532 {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va
,
1533 "size", "reserve 'size' bytes for guest virtual address space"},
1534 {"d", "QEMU_LOG", true, handle_arg_log
,
1535 "item[,...]", "enable logging of specified items "
1536 "(use '-d help' for a list of items)"},
1537 {"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter
,
1538 "range[,...]","filter logging based on address range"},
1539 {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename
,
1540 "logfile", "write logs to 'logfile' (default stderr)"},
1541 {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize
,
1542 "pagesize", "set the host page size to 'pagesize'"},
1543 {"singlestep", "QEMU_SINGLESTEP", false, handle_arg_singlestep
,
1544 "", "run in singlestep mode"},
1545 {"strace", "QEMU_STRACE", false, handle_arg_strace
,
1546 "", "log system calls"},
1547 {"seed", "QEMU_RAND_SEED", true, handle_arg_randseed
,
1548 "", "Seed for pseudo-random number generator"},
1549 {"trace", "QEMU_TRACE", true, handle_arg_trace
,
1550 "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
1551 {"version", "QEMU_VERSION", false, handle_arg_version
,
1552 "", "display version information and exit"},
1553 {NULL
, NULL
, false, NULL
, NULL
, NULL
}
1556 static void usage(int exitcode
)
1558 const struct qemu_argument
*arginfo
;
1562 printf("usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
1563 "Linux CPU emulator (compiled for " TARGET_NAME
" emulation)\n"
1565 "Options and associated environment variables:\n"
1568 /* Calculate column widths. We must always have at least enough space
1569 * for the column header.
1571 maxarglen
= strlen("Argument");
1572 maxenvlen
= strlen("Env-variable");
1574 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
1575 int arglen
= strlen(arginfo
->argv
);
1576 if (arginfo
->has_arg
) {
1577 arglen
+= strlen(arginfo
->example
) + 1;
1579 if (strlen(arginfo
->env
) > maxenvlen
) {
1580 maxenvlen
= strlen(arginfo
->env
);
1582 if (arglen
> maxarglen
) {
1587 printf("%-*s %-*s Description\n", maxarglen
+1, "Argument",
1588 maxenvlen
, "Env-variable");
1590 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
1591 if (arginfo
->has_arg
) {
1592 printf("-%s %-*s %-*s %s\n", arginfo
->argv
,
1593 (int)(maxarglen
- strlen(arginfo
->argv
) - 1),
1594 arginfo
->example
, maxenvlen
, arginfo
->env
, arginfo
->help
);
1596 printf("-%-*s %-*s %s\n", maxarglen
, arginfo
->argv
,
1597 maxenvlen
, arginfo
->env
,
1604 "QEMU_LD_PREFIX = %s\n"
1605 "QEMU_STACK_SIZE = %ld byte\n",
1610 "You can use -E and -U options or the QEMU_SET_ENV and\n"
1611 "QEMU_UNSET_ENV environment variables to set and unset\n"
1612 "environment variables for the target process.\n"
1613 "It is possible to provide several variables by separating them\n"
1614 "by commas in getsubopt(3) style. Additionally it is possible to\n"
1615 "provide the -E and -U options multiple times.\n"
1616 "The following lines are equivalent:\n"
1617 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
1618 " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
1619 " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
1620 "Note that if you provide several changes to a single variable\n"
1621 "the last change will stay in effect.\n"
1623 QEMU_HELP_BOTTOM
"\n");
1628 static int parse_args(int argc
, char **argv
)
1632 const struct qemu_argument
*arginfo
;
1634 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
1635 if (arginfo
->env
== NULL
) {
1639 r
= getenv(arginfo
->env
);
1641 arginfo
->handle_opt(r
);
1647 if (optind
>= argc
) {
1656 if (!strcmp(r
, "-")) {
1659 /* Treat --foo the same as -foo. */
1664 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
1665 if (!strcmp(r
, arginfo
->argv
)) {
1666 if (arginfo
->has_arg
) {
1667 if (optind
>= argc
) {
1668 (void) fprintf(stderr
,
1669 "qemu: missing argument for option '%s'\n", r
);
1672 arginfo
->handle_opt(argv
[optind
]);
1675 arginfo
->handle_opt(NULL
);
1681 /* no option matched the current argv */
1682 if (arginfo
->handle_opt
== NULL
) {
1683 (void) fprintf(stderr
, "qemu: unknown option '%s'\n", r
);
1688 if (optind
>= argc
) {
1689 (void) fprintf(stderr
, "qemu: no user program specified\n");
1693 filename
= argv
[optind
];
1694 exec_path
= argv
[optind
];
1699 int main(int argc
, char **argv
, char **envp
)
1701 struct target_pt_regs regs1
, *regs
= ®s1
;
1702 struct image_info info1
, *info
= &info1
;
1703 struct linux_binprm bprm
;
1708 char **target_environ
, **wrk
;
1715 module_call_init(MODULE_INIT_TRACE
);
1716 qemu_init_cpu_list();
1717 module_call_init(MODULE_INIT_QOM
);
1719 envlist
= envlist_create();
1721 /* add current environment into the list */
1722 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
1723 (void) envlist_setenv(envlist
, *wrk
);
1726 /* Read the stack limit from the kernel. If it's "unlimited",
1727 then we can do little else besides use the default. */
1730 if (getrlimit(RLIMIT_STACK
, &lim
) == 0
1731 && lim
.rlim_cur
!= RLIM_INFINITY
1732 && lim
.rlim_cur
== (target_long
)lim
.rlim_cur
) {
1733 guest_stack_size
= lim
.rlim_cur
;
1741 qemu_add_opts(&qemu_trace_opts
);
1743 optind
= parse_args(argc
, argv
);
1745 if (!trace_init_backends()) {
1748 trace_init_file(trace_file
);
1751 memset(regs
, 0, sizeof(struct target_pt_regs
));
1753 /* Zero out image_info */
1754 memset(info
, 0, sizeof(struct image_info
));
1756 memset(&bprm
, 0, sizeof (bprm
));
1758 /* Scan interp_prefix dir for replacement files. */
1759 init_paths(interp_prefix
);
1761 init_qemu_uname_release();
1763 execfd
= qemu_getauxval(AT_EXECFD
);
1765 execfd
= open(filename
, O_RDONLY
);
1767 printf("Error while loading %s: %s\n", filename
, strerror(errno
));
1768 _exit(EXIT_FAILURE
);
1772 if (cpu_model
== NULL
) {
1773 cpu_model
= cpu_get_model(get_elf_eflags(execfd
));
1775 cpu_type
= parse_cpu_model(cpu_model
);
1778 /* NOTE: we need to init the CPU at this stage to get
1779 qemu_host_page_size */
1781 cpu
= cpu_create(cpu_type
);
1787 if (getenv("QEMU_STRACE")) {
1791 if (getenv("QEMU_RAND_SEED")) {
1792 handle_arg_randseed(getenv("QEMU_RAND_SEED"));
1795 target_environ
= envlist_to_environ(envlist
, NULL
);
1796 envlist_free(envlist
);
1799 * Now that page sizes are configured in cpu_init() we can do
1800 * proper page alignment for guest_base.
1802 guest_base
= HOST_PAGE_ALIGN(guest_base
);
1804 if (reserved_va
|| have_guest_base
) {
1805 guest_base
= init_guest_space(guest_base
, reserved_va
, 0,
1807 if (guest_base
== (unsigned long)-1) {
1808 fprintf(stderr
, "Unable to reserve 0x%lx bytes of virtual address "
1809 "space for use as guest address space (check your virtual "
1810 "memory ulimit setting or reserve less using -R option)\n",
1816 mmap_next_start
= reserved_va
;
1821 * Read in mmap_min_addr kernel parameter. This value is used
1822 * When loading the ELF image to determine whether guest_base
1823 * is needed. It is also used in mmap_find_vma.
1828 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
1830 if (fscanf(fp
, "%lu", &tmp
) == 1) {
1831 mmap_min_addr
= tmp
;
1832 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n", mmap_min_addr
);
1839 * Prepare copy of argv vector for target.
1841 target_argc
= argc
- optind
;
1842 target_argv
= calloc(target_argc
+ 1, sizeof (char *));
1843 if (target_argv
== NULL
) {
1844 (void) fprintf(stderr
, "Unable to allocate memory for target_argv\n");
1849 * If argv0 is specified (using '-0' switch) we replace
1850 * argv[0] pointer with the given one.
1853 if (argv0
!= NULL
) {
1854 target_argv
[i
++] = strdup(argv0
);
1856 for (; i
< target_argc
; i
++) {
1857 target_argv
[i
] = strdup(argv
[optind
+ i
]);
1859 target_argv
[target_argc
] = NULL
;
1861 ts
= g_new0(TaskState
, 1);
1862 init_task_state(ts
);
1863 /* build Task State */
1869 ret
= loader_exec(execfd
, filename
, target_argv
, target_environ
, regs
,
1872 printf("Error while loading %s: %s\n", filename
, strerror(-ret
));
1873 _exit(EXIT_FAILURE
);
1876 for (wrk
= target_environ
; *wrk
; wrk
++) {
1880 g_free(target_environ
);
1882 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
1883 qemu_log("guest_base 0x%lx\n", guest_base
);
1886 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
1887 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
1888 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n", info
->start_code
);
1889 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n", info
->start_data
);
1890 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
1891 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n", info
->start_stack
);
1892 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
1893 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
1894 qemu_log("argv_start 0x" TARGET_ABI_FMT_lx
"\n", info
->arg_start
);
1895 qemu_log("env_start 0x" TARGET_ABI_FMT_lx
"\n",
1896 info
->arg_end
+ (abi_ulong
)sizeof(abi_ulong
));
1897 qemu_log("auxv_start 0x" TARGET_ABI_FMT_lx
"\n", info
->saved_auxv
);
1900 target_set_brk(info
->brk
);
1904 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
1905 generating the prologue until now so that the prologue can take
1906 the real value of GUEST_BASE into account. */
1907 tcg_prologue_init(tcg_ctx
);
1910 target_cpu_copy_regs(env
, regs
);
1912 #if defined(TARGET_RISCV)
1914 env
->pc
= regs
->sepc
;
1915 env
->gpr
[xSP
] = regs
->sp
;
1917 #elif defined(TARGET_ALPHA)
1921 for(i
= 0; i
< 28; i
++) {
1922 env
->ir
[i
] = ((abi_ulong
*)regs
)[i
];
1924 env
->ir
[IR_SP
] = regs
->usp
;
1927 #elif defined(TARGET_S390X)
1930 for (i
= 0; i
< 16; i
++) {
1931 env
->regs
[i
] = regs
->gprs
[i
];
1933 env
->psw
.mask
= regs
->psw
.mask
;
1934 env
->psw
.addr
= regs
->psw
.addr
;
1936 #elif defined(TARGET_TILEGX)
1939 for (i
= 0; i
< TILEGX_R_COUNT
; i
++) {
1940 env
->regs
[i
] = regs
->regs
[i
];
1942 for (i
= 0; i
< TILEGX_SPR_COUNT
; i
++) {
1947 #elif defined(TARGET_HPPA)
1950 for (i
= 1; i
< 32; i
++) {
1951 env
->gr
[i
] = regs
->gr
[i
];
1953 env
->iaoq_f
= regs
->iaoq
[0];
1954 env
->iaoq_b
= regs
->iaoq
[1];
1956 #elif defined(TARGET_XTENSA)
1959 for (i
= 0; i
< 16; ++i
) {
1960 env
->regs
[i
] = regs
->areg
[i
];
1962 env
->sregs
[WINDOW_START
] = regs
->windowstart
;
1968 if (gdbserver_start(gdbstub_port
) < 0) {
1969 fprintf(stderr
, "qemu: could not open gdbserver on port %d\n",
1973 gdb_handlesig(cpu
, 0);