4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2013-14 Stacey Son
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu-common.h"
23 #include "qemu/units.h"
24 #include "qemu/accel.h"
25 #include "sysemu/tcg.h"
26 #include "qemu-version.h"
27 #include <machine/trap.h>
29 #include "qapi/error.h"
31 #include "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "qemu/path.h"
34 #include "qemu/help_option.h"
35 #include "qemu/module.h"
36 #include "exec/exec-all.h"
38 #include "qemu/timer.h"
39 #include "qemu/envlist.h"
40 #include "qemu/cutils.h"
42 #include "trace/control.h"
45 unsigned long mmap_min_addr
;
48 unsigned long reserved_va
;
50 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
51 const char *qemu_uname_release
;
52 enum BSDType bsd_type
;
55 * XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
56 * we allocate a bigger stack. Need a better solution, for example
57 * by remapping the process stack directly at the right place
59 unsigned long x86_stack_size
= 512 * 1024;
61 void gemu_log(const char *fmt
, ...)
66 vfprintf(stderr
, fmt
, ap
);
70 #if defined(TARGET_I386)
71 int cpu_get_pic_interrupt(CPUX86State
*env
)
81 void fork_end(int child
)
84 gdbserver_fork(thread_cpu
);
89 /***********************************************************/
90 /* CPUX86 core interface */
92 uint64_t cpu_get_tsc(CPUX86State
*env
)
94 return cpu_get_host_ticks();
97 static void write_dt(void *ptr
, unsigned long addr
, unsigned long limit
,
102 e1
= (addr
<< 16) | (limit
& 0xffff);
103 e2
= ((addr
>> 16) & 0xff) | (addr
& 0xff000000) | (limit
& 0x000f0000);
110 static uint64_t *idt_table
;
112 static void set_gate64(void *ptr
, unsigned int type
, unsigned int dpl
,
113 uint64_t addr
, unsigned int sel
)
116 e1
= (addr
& 0xffff) | (sel
<< 16);
117 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
121 p
[2] = tswap32(addr
>> 32);
124 /* only dpl matters as we do only user space emulation */
125 static void set_idt(int n
, unsigned int dpl
)
127 set_gate64(idt_table
+ n
* 2, 0, dpl
, 0, 0);
130 static void set_gate(void *ptr
, unsigned int type
, unsigned int dpl
,
131 uint32_t addr
, unsigned int sel
)
134 e1
= (addr
& 0xffff) | (sel
<< 16);
135 e2
= (addr
& 0xffff0000) | 0x8000 | (dpl
<< 13) | (type
<< 8);
141 /* only dpl matters as we do only user space emulation */
142 static void set_idt(int n
, unsigned int dpl
)
144 set_gate(idt_table
+ n
, 0, dpl
, 0, 0);
148 void cpu_loop(CPUX86State
*env
)
150 CPUState
*cs
= env_cpu(env
);
153 /* target_siginfo_t info; */
157 trapnr
= cpu_exec(cs
);
159 process_queued_cpu_work(cs
);
163 /* syscall from int $0x80 */
164 if (bsd_type
== target_freebsd
) {
165 abi_ulong params
= (abi_ulong
) env
->regs
[R_ESP
] +
167 int32_t syscall_nr
= env
->regs
[R_EAX
];
168 int32_t arg1
, arg2
, arg3
, arg4
, arg5
, arg6
, arg7
, arg8
;
170 if (syscall_nr
== TARGET_FREEBSD_NR_syscall
) {
171 get_user_s32(syscall_nr
, params
);
172 params
+= sizeof(int32_t);
173 } else if (syscall_nr
== TARGET_FREEBSD_NR___syscall
) {
174 get_user_s32(syscall_nr
, params
);
175 params
+= sizeof(int64_t);
177 get_user_s32(arg1
, params
);
178 params
+= sizeof(int32_t);
179 get_user_s32(arg2
, params
);
180 params
+= sizeof(int32_t);
181 get_user_s32(arg3
, params
);
182 params
+= sizeof(int32_t);
183 get_user_s32(arg4
, params
);
184 params
+= sizeof(int32_t);
185 get_user_s32(arg5
, params
);
186 params
+= sizeof(int32_t);
187 get_user_s32(arg6
, params
);
188 params
+= sizeof(int32_t);
189 get_user_s32(arg7
, params
);
190 params
+= sizeof(int32_t);
191 get_user_s32(arg8
, params
);
192 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
202 } else { /* if (bsd_type == target_openbsd) */
203 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
212 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
213 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
216 env
->eflags
&= ~CC_C
;
221 /* syscall from syscall instruction */
222 if (bsd_type
== target_freebsd
) {
223 env
->regs
[R_EAX
] = do_freebsd_syscall(env
,
231 } else { /* if (bsd_type == target_openbsd) */
232 env
->regs
[R_EAX
] = do_openbsd_syscall(env
,
241 env
->eip
= env
->exception_next_eip
;
242 if (((abi_ulong
)env
->regs
[R_EAX
]) >= (abi_ulong
)(-515)) {
243 env
->regs
[R_EAX
] = -env
->regs
[R_EAX
];
246 env
->eflags
&= ~CC_C
;
251 /* just indicate that signals should be handled asap */
254 pc
= env
->segs
[R_CS
].base
+ env
->eip
;
256 "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
260 process_pending_signals(env
);
265 static void usage(void)
267 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
268 "\n" QEMU_COPYRIGHT
"\n"
269 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
270 "BSD CPU emulator (compiled for %s emulation)\n"
272 "Standard options:\n"
273 "-h print this help\n"
274 "-g port wait gdb connection to port\n"
275 "-L path set the elf interpreter prefix (default=%s)\n"
276 "-s size set the stack size in bytes (default=%ld)\n"
277 "-cpu model select CPU (-cpu help for list)\n"
278 "-drop-ld-preload drop LD_PRELOAD for target process\n"
279 "-E var=value sets/modifies targets environment variable(s)\n"
280 "-U var unsets targets environment variable(s)\n"
281 "-B address set guest_base address to address\n"
282 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
285 "-d item1[,...] enable logging of specified items\n"
286 " (use '-d help' for a list of log items)\n"
287 "-D logfile write logs to 'logfile' (default stderr)\n"
288 "-p pagesize set the host page size to 'pagesize'\n"
289 "-singlestep always run in singlestep mode\n"
290 "-strace log system calls\n"
291 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
292 " specify tracing options\n"
294 "Environment variables:\n"
295 "QEMU_STRACE Print system calls and arguments similar to the\n"
296 " 'strace' program. Enable by setting to any value.\n"
297 "You can use -E and -U options to set/unset environment variables\n"
298 "for target process. It is possible to provide several variables\n"
299 "by repeating the option. For example:\n"
300 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
301 "Note that if you provide several changes to single variable\n"
302 "last change will stay in effect.\n"
304 QEMU_HELP_BOTTOM
"\n"
312 THREAD CPUState
*thread_cpu
;
314 bool qemu_cpu_is_self(CPUState
*cpu
)
316 return thread_cpu
== cpu
;
319 void qemu_cpu_kick(CPUState
*cpu
)
324 /* Assumes contents are already zeroed. */
325 void init_task_state(TaskState
*ts
)
330 ts
->first_free
= ts
->sigqueue_table
;
331 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
332 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
334 ts
->sigqueue_table
[i
].next
= NULL
;
337 int main(int argc
, char **argv
)
339 const char *filename
;
340 const char *cpu_model
;
341 const char *cpu_type
;
342 const char *log_file
= NULL
;
343 const char *log_mask
= NULL
;
344 struct target_pt_regs regs1
, *regs
= ®s1
;
345 struct image_info info1
, *info
= &info1
;
346 struct bsd_binprm bprm
;
347 TaskState ts1
, *ts
= &ts1
;
352 const char *gdbstub
= NULL
;
353 char **target_environ
, **wrk
;
354 envlist_t
*envlist
= NULL
;
355 bsd_type
= target_openbsd
;
362 module_call_init(MODULE_INIT_TRACE
);
363 qemu_init_cpu_list();
364 module_call_init(MODULE_INIT_QOM
);
366 envlist
= envlist_create();
368 /* add current environment into the list */
369 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
370 (void) envlist_setenv(envlist
, *wrk
);
375 qemu_add_opts(&qemu_trace_opts
);
379 if (optind
>= argc
) {
388 if (!strcmp(r
, "-")) {
390 } else if (!strcmp(r
, "d")) {
391 if (optind
>= argc
) {
394 log_mask
= argv
[optind
++];
395 } else if (!strcmp(r
, "D")) {
396 if (optind
>= argc
) {
399 log_file
= argv
[optind
++];
400 } else if (!strcmp(r
, "E")) {
402 if (envlist_setenv(envlist
, r
) != 0) {
405 } else if (!strcmp(r
, "ignore-environment")) {
406 envlist_free(envlist
);
407 envlist
= envlist_create();
408 } else if (!strcmp(r
, "U")) {
410 if (envlist_unsetenv(envlist
, r
) != 0) {
413 } else if (!strcmp(r
, "s")) {
415 rv
= qemu_strtoul(r
, &r
, 0, &x86_stack_size
);
416 if (rv
< 0 || x86_stack_size
<= 0) {
420 x86_stack_size
*= MiB
;
421 } else if (*r
== 'k' || *r
== 'K') {
422 x86_stack_size
*= KiB
;
424 } else if (!strcmp(r
, "L")) {
425 interp_prefix
= argv
[optind
++];
426 } else if (!strcmp(r
, "p")) {
427 qemu_host_page_size
= atoi(argv
[optind
++]);
428 if (qemu_host_page_size
== 0 ||
429 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
430 fprintf(stderr
, "page size must be a power of two\n");
433 } else if (!strcmp(r
, "g")) {
434 gdbstub
= g_strdup(argv
[optind
++]);
435 } else if (!strcmp(r
, "r")) {
436 qemu_uname_release
= argv
[optind
++];
437 } else if (!strcmp(r
, "cpu")) {
438 cpu_model
= argv
[optind
++];
439 if (is_help_option(cpu_model
)) {
440 /* XXX: implement xxx_cpu_list for targets that still miss it */
441 #if defined(cpu_list)
446 } else if (!strcmp(r
, "B")) {
447 rv
= qemu_strtoul(argv
[optind
++], NULL
, 0, &guest_base
);
451 have_guest_base
= true;
452 } else if (!strcmp(r
, "drop-ld-preload")) {
453 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
454 } else if (!strcmp(r
, "bsd")) {
455 if (!strcasecmp(argv
[optind
], "freebsd")) {
456 bsd_type
= target_freebsd
;
457 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
458 bsd_type
= target_netbsd
;
459 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
460 bsd_type
= target_openbsd
;
465 } else if (!strcmp(r
, "singlestep")) {
467 } else if (!strcmp(r
, "strace")) {
469 } else if (!strcmp(r
, "trace")) {
470 trace_opt_parse(optarg
);
477 qemu_log_needs_buffers();
478 qemu_set_log_filename(log_file
, &error_fatal
);
482 mask
= qemu_str_to_log_mask(log_mask
);
484 qemu_print_log_usage(stdout
);
490 if (optind
>= argc
) {
493 filename
= argv
[optind
];
495 if (!trace_init_backends()) {
501 memset(regs
, 0, sizeof(struct target_pt_regs
));
503 /* Zero bsd params */
504 memset(&bprm
, 0, sizeof(bprm
));
506 /* Zero out image_info */
507 memset(info
, 0, sizeof(struct image_info
));
509 /* Scan interp_prefix dir for replacement files. */
510 init_paths(interp_prefix
);
512 if (cpu_model
== NULL
) {
513 #if defined(TARGET_I386)
515 cpu_model
= "qemu64";
517 cpu_model
= "qemu32";
524 cpu_type
= parse_cpu_option(cpu_model
);
525 /* init tcg before creating CPUs and to get qemu_host_page_size */
527 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
529 accel_init_interfaces(ac
);
530 ac
->init_machine(NULL
);
532 cpu
= cpu_create(cpu_type
);
537 if (getenv("QEMU_STRACE")) {
541 target_environ
= envlist_to_environ(envlist
, NULL
);
542 envlist_free(envlist
);
545 * Now that page sizes are configured we can do
546 * proper page alignment for guest_base.
548 guest_base
= HOST_PAGE_ALIGN(guest_base
);
551 * Read in mmap_min_addr kernel parameter. This value is used
552 * When loading the ELF image to determine whether guest_base
555 * When user has explicitly set the quest base, we skip this
558 if (!have_guest_base
) {
561 fp
= fopen("/proc/sys/vm/mmap_min_addr", "r");
564 if (fscanf(fp
, "%lu", &tmp
) == 1) {
566 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n",
573 if (loader_exec(filename
, argv
+ optind
, target_environ
, regs
, info
,
575 printf("Error loading %s\n", filename
);
579 for (wrk
= target_environ
; *wrk
; wrk
++) {
583 g_free(target_environ
);
585 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
586 qemu_log("guest_base %p\n", (void *)guest_base
);
587 log_page_dump("binary load");
589 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
590 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
591 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
593 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
595 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
596 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
598 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
599 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
602 target_set_brk(info
->brk
);
607 * Now that we've loaded the binary, GUEST_BASE is fixed. Delay
608 * generating the prologue until now so that the prologue can take
609 * the real value of GUEST_BASE into account.
611 tcg_prologue_init(tcg_ctx
);
613 /* build Task State */
614 memset(ts
, 0, sizeof(TaskState
));
619 #if defined(TARGET_I386)
620 env
->cr
[0] = CR0_PG_MASK
| CR0_WP_MASK
| CR0_PE_MASK
;
621 env
->hflags
|= HF_PE_MASK
| HF_CPL_MASK
;
622 if (env
->features
[FEAT_1_EDX
] & CPUID_SSE
) {
623 env
->cr
[4] |= CR4_OSFXSR_MASK
;
624 env
->hflags
|= HF_OSFXSR_MASK
;
627 /* enable 64 bit mode if possible */
628 if (!(env
->features
[FEAT_8000_0001_EDX
] & CPUID_EXT2_LM
)) {
629 fprintf(stderr
, "The selected x86 CPU does not support 64 bit mode\n");
632 env
->cr
[4] |= CR4_PAE_MASK
;
633 env
->efer
|= MSR_EFER_LMA
| MSR_EFER_LME
;
634 env
->hflags
|= HF_LMA_MASK
;
637 /* flags setup : we activate the IRQs by default as in user mode */
638 env
->eflags
|= IF_MASK
;
640 /* linux register setup */
642 env
->regs
[R_EAX
] = regs
->rax
;
643 env
->regs
[R_EBX
] = regs
->rbx
;
644 env
->regs
[R_ECX
] = regs
->rcx
;
645 env
->regs
[R_EDX
] = regs
->rdx
;
646 env
->regs
[R_ESI
] = regs
->rsi
;
647 env
->regs
[R_EDI
] = regs
->rdi
;
648 env
->regs
[R_EBP
] = regs
->rbp
;
649 env
->regs
[R_ESP
] = regs
->rsp
;
650 env
->eip
= regs
->rip
;
652 env
->regs
[R_EAX
] = regs
->eax
;
653 env
->regs
[R_EBX
] = regs
->ebx
;
654 env
->regs
[R_ECX
] = regs
->ecx
;
655 env
->regs
[R_EDX
] = regs
->edx
;
656 env
->regs
[R_ESI
] = regs
->esi
;
657 env
->regs
[R_EDI
] = regs
->edi
;
658 env
->regs
[R_EBP
] = regs
->ebp
;
659 env
->regs
[R_ESP
] = regs
->esp
;
660 env
->eip
= regs
->eip
;
663 /* linux interrupt setup */
665 env
->idt
.limit
= 511;
667 env
->idt
.limit
= 255;
669 env
->idt
.base
= target_mmap(0, sizeof(uint64_t) * (env
->idt
.limit
+ 1),
670 PROT_READ
| PROT_WRITE
,
671 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
672 idt_table
= g2h_untagged(env
->idt
.base
);
695 /* linux segment setup */
698 env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
699 PROT_READ
| PROT_WRITE
,
700 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
701 env
->gdt
.limit
= sizeof(uint64_t) * TARGET_GDT_ENTRIES
- 1;
702 gdt_table
= g2h_untagged(env
->gdt
.base
);
704 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
705 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
706 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
708 /* 64 bit code segment */
709 write_dt(&gdt_table
[__USER_CS
>> 3], 0, 0xfffff,
710 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
712 (3 << DESC_DPL_SHIFT
) | (0xa << DESC_TYPE_SHIFT
));
714 write_dt(&gdt_table
[__USER_DS
>> 3], 0, 0xfffff,
715 DESC_G_MASK
| DESC_B_MASK
| DESC_P_MASK
| DESC_S_MASK
|
716 (3 << DESC_DPL_SHIFT
) | (0x2 << DESC_TYPE_SHIFT
));
719 cpu_x86_load_seg(env
, R_CS
, __USER_CS
);
720 cpu_x86_load_seg(env
, R_SS
, __USER_DS
);
722 cpu_x86_load_seg(env
, R_DS
, __USER_DS
);
723 cpu_x86_load_seg(env
, R_ES
, __USER_DS
);
724 cpu_x86_load_seg(env
, R_FS
, __USER_DS
);
725 cpu_x86_load_seg(env
, R_GS
, __USER_DS
);
726 /* This hack makes Wine work... */
727 env
->segs
[R_FS
].selector
= 0;
729 cpu_x86_load_seg(env
, R_DS
, 0);
730 cpu_x86_load_seg(env
, R_ES
, 0);
731 cpu_x86_load_seg(env
, R_FS
, 0);
732 cpu_x86_load_seg(env
, R_GS
, 0);
735 #error unsupported target CPU
739 gdbserver_start(gdbstub
);
740 gdb_handlesig(cpu
, 0);