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 #include "target_arch_cpu.h"
47 #include <sys/sysctl.h>
50 unsigned long mmap_min_addr
;
53 unsigned long reserved_va
;
55 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
56 const char *qemu_uname_release
;
57 enum BSDType bsd_type
;
58 char qemu_proc_pathname
[PATH_MAX
]; /* full path to exeutable */
61 * XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
62 * we allocate a bigger stack. Need a better solution, for example
63 * by remapping the process stack directly at the right place
65 unsigned long x86_stack_size
= 512 * 1024;
67 void gemu_log(const char *fmt
, ...)
72 vfprintf(stderr
, fmt
, ap
);
80 void fork_end(int child
)
83 gdbserver_fork(thread_cpu
);
87 void cpu_loop(CPUArchState
*env
)
92 static void usage(void)
94 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
95 "\n" QEMU_COPYRIGHT
"\n"
96 "usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
97 "BSD CPU emulator (compiled for %s emulation)\n"
100 "-h print this help\n"
101 "-g port wait gdb connection to port\n"
102 "-L path set the elf interpreter prefix (default=%s)\n"
103 "-s size set the stack size in bytes (default=%ld)\n"
104 "-cpu model select CPU (-cpu help for list)\n"
105 "-drop-ld-preload drop LD_PRELOAD for target process\n"
106 "-E var=value sets/modifies targets environment variable(s)\n"
107 "-U var unsets targets environment variable(s)\n"
108 "-B address set guest_base address to address\n"
109 "-bsd type select emulated BSD type FreeBSD/NetBSD/OpenBSD (default)\n"
112 "-d item1[,...] enable logging of specified items\n"
113 " (use '-d help' for a list of log items)\n"
114 "-D logfile write logs to 'logfile' (default stderr)\n"
115 "-p pagesize set the host page size to 'pagesize'\n"
116 "-singlestep always run in singlestep mode\n"
117 "-strace log system calls\n"
118 "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
119 " specify tracing options\n"
121 "Environment variables:\n"
122 "QEMU_STRACE Print system calls and arguments similar to the\n"
123 " 'strace' program. Enable by setting to any value.\n"
124 "You can use -E and -U options to set/unset environment variables\n"
125 "for target process. It is possible to provide several variables\n"
126 "by repeating the option. For example:\n"
127 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
128 "Note that if you provide several changes to single variable\n"
129 "last change will stay in effect.\n"
131 QEMU_HELP_BOTTOM
"\n"
139 __thread CPUState
*thread_cpu
;
141 bool qemu_cpu_is_self(CPUState
*cpu
)
143 return thread_cpu
== cpu
;
146 void qemu_cpu_kick(CPUState
*cpu
)
151 /* Assumes contents are already zeroed. */
152 void init_task_state(TaskState
*ts
)
157 ts
->first_free
= ts
->sigqueue_table
;
158 for (i
= 0; i
< MAX_SIGQUEUE_SIZE
- 1; i
++) {
159 ts
->sigqueue_table
[i
].next
= &ts
->sigqueue_table
[i
+ 1];
161 ts
->sigqueue_table
[i
].next
= NULL
;
164 static void save_proc_pathname(char *argv0
)
171 mib
[2] = KERN_PROC_PATHNAME
;
174 len
= sizeof(qemu_proc_pathname
);
175 if (sysctl(mib
, 4, qemu_proc_pathname
, &len
, NULL
, 0)) {
180 int main(int argc
, char **argv
)
182 const char *filename
;
183 const char *cpu_model
;
184 const char *cpu_type
;
185 const char *log_file
= NULL
;
186 const char *log_mask
= NULL
;
187 struct target_pt_regs regs1
, *regs
= ®s1
;
188 struct image_info info1
, *info
= &info1
;
189 struct bsd_binprm bprm
;
195 const char *gdbstub
= NULL
;
196 char **target_environ
, **wrk
;
197 envlist_t
*envlist
= NULL
;
198 bsd_type
= HOST_DEFAULT_BSD_TYPE
;
204 save_proc_pathname(argv
[0]);
207 module_call_init(MODULE_INIT_TRACE
);
208 qemu_init_cpu_list();
209 module_call_init(MODULE_INIT_QOM
);
211 envlist
= envlist_create();
213 /* add current environment into the list */
214 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
215 (void) envlist_setenv(envlist
, *wrk
);
220 qemu_add_opts(&qemu_trace_opts
);
224 if (optind
>= argc
) {
233 if (!strcmp(r
, "-")) {
235 } else if (!strcmp(r
, "d")) {
236 if (optind
>= argc
) {
239 log_mask
= argv
[optind
++];
240 } else if (!strcmp(r
, "D")) {
241 if (optind
>= argc
) {
244 log_file
= argv
[optind
++];
245 } else if (!strcmp(r
, "E")) {
247 if (envlist_setenv(envlist
, r
) != 0) {
250 } else if (!strcmp(r
, "ignore-environment")) {
251 envlist_free(envlist
);
252 envlist
= envlist_create();
253 } else if (!strcmp(r
, "U")) {
255 if (envlist_unsetenv(envlist
, r
) != 0) {
258 } else if (!strcmp(r
, "s")) {
260 rv
= qemu_strtoul(r
, &r
, 0, &x86_stack_size
);
261 if (rv
< 0 || x86_stack_size
<= 0) {
265 x86_stack_size
*= MiB
;
266 } else if (*r
== 'k' || *r
== 'K') {
267 x86_stack_size
*= KiB
;
269 } else if (!strcmp(r
, "L")) {
270 interp_prefix
= argv
[optind
++];
271 } else if (!strcmp(r
, "p")) {
272 qemu_host_page_size
= atoi(argv
[optind
++]);
273 if (qemu_host_page_size
== 0 ||
274 (qemu_host_page_size
& (qemu_host_page_size
- 1)) != 0) {
275 fprintf(stderr
, "page size must be a power of two\n");
278 } else if (!strcmp(r
, "g")) {
279 gdbstub
= g_strdup(argv
[optind
++]);
280 } else if (!strcmp(r
, "r")) {
281 qemu_uname_release
= argv
[optind
++];
282 } else if (!strcmp(r
, "cpu")) {
283 cpu_model
= argv
[optind
++];
284 if (is_help_option(cpu_model
)) {
285 /* XXX: implement xxx_cpu_list for targets that still miss it */
286 #if defined(cpu_list)
291 } else if (!strcmp(r
, "B")) {
292 rv
= qemu_strtoul(argv
[optind
++], NULL
, 0, &guest_base
);
296 have_guest_base
= true;
297 } else if (!strcmp(r
, "drop-ld-preload")) {
298 (void) envlist_unsetenv(envlist
, "LD_PRELOAD");
299 } else if (!strcmp(r
, "bsd")) {
300 if (!strcasecmp(argv
[optind
], "freebsd")) {
301 bsd_type
= target_freebsd
;
302 } else if (!strcasecmp(argv
[optind
], "netbsd")) {
303 bsd_type
= target_netbsd
;
304 } else if (!strcasecmp(argv
[optind
], "openbsd")) {
305 bsd_type
= target_openbsd
;
310 } else if (!strcmp(r
, "singlestep")) {
312 } else if (!strcmp(r
, "strace")) {
314 } else if (!strcmp(r
, "trace")) {
315 trace_opt_parse(optarg
);
322 qemu_log_needs_buffers();
323 qemu_set_log_filename(log_file
, &error_fatal
);
327 mask
= qemu_str_to_log_mask(log_mask
);
329 qemu_print_log_usage(stdout
);
335 if (optind
>= argc
) {
338 filename
= argv
[optind
];
340 if (!trace_init_backends()) {
346 memset(regs
, 0, sizeof(struct target_pt_regs
));
348 /* Zero bsd params */
349 memset(&bprm
, 0, sizeof(bprm
));
351 /* Zero out image_info */
352 memset(info
, 0, sizeof(struct image_info
));
354 /* Scan interp_prefix dir for replacement files. */
355 init_paths(interp_prefix
);
357 if (cpu_model
== NULL
) {
358 cpu_model
= TARGET_DEFAULT_CPU_MODEL
;
361 cpu_type
= parse_cpu_option(cpu_model
);
363 /* init tcg before creating CPUs and to get qemu_host_page_size */
365 AccelClass
*ac
= ACCEL_GET_CLASS(current_accel());
367 accel_init_interfaces(ac
);
368 ac
->init_machine(NULL
);
370 cpu
= cpu_create(cpu_type
);
375 if (getenv("QEMU_STRACE")) {
379 target_environ
= envlist_to_environ(envlist
, NULL
);
380 envlist_free(envlist
);
383 * Now that page sizes are configured we can do
384 * proper page alignment for guest_base.
386 guest_base
= HOST_PAGE_ALIGN(guest_base
);
388 if (loader_exec(filename
, argv
+ optind
, target_environ
, regs
, info
,
390 printf("Error loading %s\n", filename
);
394 for (wrk
= target_environ
; *wrk
; wrk
++) {
398 g_free(target_environ
);
400 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
401 qemu_log("guest_base %p\n", (void *)guest_base
);
402 log_page_dump("binary load");
404 qemu_log("start_brk 0x" TARGET_ABI_FMT_lx
"\n", info
->start_brk
);
405 qemu_log("end_code 0x" TARGET_ABI_FMT_lx
"\n", info
->end_code
);
406 qemu_log("start_code 0x" TARGET_ABI_FMT_lx
"\n",
408 qemu_log("start_data 0x" TARGET_ABI_FMT_lx
"\n",
410 qemu_log("end_data 0x" TARGET_ABI_FMT_lx
"\n", info
->end_data
);
411 qemu_log("start_stack 0x" TARGET_ABI_FMT_lx
"\n",
413 qemu_log("brk 0x" TARGET_ABI_FMT_lx
"\n", info
->brk
);
414 qemu_log("entry 0x" TARGET_ABI_FMT_lx
"\n", info
->entry
);
417 /* build Task State */
418 ts
= g_new0(TaskState
, 1);
424 target_set_brk(info
->brk
);
429 * Now that we've loaded the binary, GUEST_BASE is fixed. Delay
430 * generating the prologue until now so that the prologue can take
431 * the real value of GUEST_BASE into account.
433 tcg_prologue_init(tcg_ctx
);
435 target_cpu_init(env
, regs
);
438 gdbserver_start(gdbstub
);
439 gdb_handlesig(cpu
, 0);