]>
Commit | Line | Data |
---|---|---|
31e31b8a | 1 | /* |
93ac68bc | 2 | * qemu user main |
5fafdf24 | 3 | * |
68d0f70e | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
31e31b8a FB |
5 | * |
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. | |
10 | * | |
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. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
8167ee88 | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
31e31b8a | 18 | */ |
14a48c1d | 19 | |
d39594e9 | 20 | #include "qemu/osdep.h" |
49f95221 | 21 | #include "qemu/help-texts.h" |
b52713c1 | 22 | #include "qemu/units.h" |
940e43aa | 23 | #include "qemu/accel.h" |
67a1de0d | 24 | #include "qemu-version.h" |
edf8e2af | 25 | #include <sys/syscall.h> |
703e0e89 | 26 | #include <sys/resource.h> |
ee947430 | 27 | #include <sys/shm.h> |
6e1c0d7b | 28 | #include <linux/binfmts.h> |
31e31b8a | 29 | |
daa76aa4 | 30 | #include "qapi/error.h" |
3ef693a0 | 31 | #include "qemu.h" |
3b249d26 | 32 | #include "user-internals.h" |
f348b6d1 | 33 | #include "qemu/path.h" |
dc5e9ac7 | 34 | #include "qemu/queue.h" |
6533dd6e | 35 | #include "qemu/config-file.h" |
f348b6d1 | 36 | #include "qemu/cutils.h" |
f5852efa | 37 | #include "qemu/error-report.h" |
f348b6d1 | 38 | #include "qemu/help_option.h" |
0b8fa32f | 39 | #include "qemu/module.h" |
f308f64e | 40 | #include "qemu/plugin.h" |
63c91552 | 41 | #include "exec/exec-all.h" |
85b4fa0c | 42 | #include "exec/gdbstub.h" |
d96bf49b | 43 | #include "gdbstub/user.h" |
dcb32f1d | 44 | #include "tcg/tcg.h" |
1de7afc9 PB |
45 | #include "qemu/timer.h" |
46 | #include "qemu/envlist.h" | |
5ebdd774 | 47 | #include "qemu/guest-random.h" |
d8fd2954 | 48 | #include "elf.h" |
6533dd6e | 49 | #include "trace/control.h" |
542ca434 | 50 | #include "target_elf.h" |
cd71c089 | 51 | #include "cpu_loop-common.h" |
a573e9ba | 52 | #include "crypto/init.h" |
c093364f | 53 | #include "fd-trans.h" |
2113aed6 | 54 | #include "signal-common.h" |
3ad0a769 | 55 | #include "loader.h" |
5423e6d3 | 56 | #include "user-mmap.h" |
5584e2db | 57 | #include "accel/tcg/perf.h" |
04a6dfeb | 58 | |
e4a4aaa5 RH |
59 | #ifdef CONFIG_SEMIHOSTING |
60 | #include "semihosting/semihost.h" | |
61 | #endif | |
62 | ||
6e1c0d7b LV |
63 | #ifndef AT_FLAGS_PRESERVE_ARGV0 |
64 | #define AT_FLAGS_PRESERVE_ARGV0_BIT 0 | |
65 | #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT) | |
66 | #endif | |
67 | ||
d088d664 AJ |
68 | char *exec_path; |
69 | ||
1b530a6d | 70 | int singlestep; |
8cb76755 | 71 | static const char *argv0; |
fcedd920 | 72 | static const char *gdbstub; |
8cb76755 | 73 | static envlist_t *envlist; |
51fb256a | 74 | static const char *cpu_model; |
2278b939 | 75 | static const char *cpu_type; |
5ebdd774 | 76 | static const char *seed_optarg; |
379f6698 | 77 | unsigned long mmap_min_addr; |
5ca870b9 | 78 | uintptr_t guest_base; |
e307c192 | 79 | bool have_guest_base; |
120a9848 | 80 | |
4b25a506 JK |
81 | /* |
82 | * Used to implement backwards-compatibility for the `-strace`, and | |
83 | * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by | |
84 | * -strace, or vice versa. | |
85 | */ | |
86 | static bool enable_strace; | |
87 | ||
88 | /* | |
89 | * The last log mask given by the user in an environment variable or argument. | |
90 | * Used to support command line arguments overriding environment variables. | |
91 | */ | |
92 | static int last_log_mask; | |
b410253f | 93 | static const char *last_log_filename; |
4b25a506 | 94 | |
288e65b9 AG |
95 | /* |
96 | * When running 32-on-64 we should make sure we can fit all of the possible | |
97 | * guest address space into a contiguous chunk of virtual host memory. | |
98 | * | |
99 | * This way we will never overlap with our own libraries or binaries or stack | |
100 | * or anything else that QEMU maps. | |
18e80c55 RH |
101 | * |
102 | * Many cpus reserve the high bit (or more than one for some 64-bit cpus) | |
103 | * of the address for the kernel. Some cpus rely on this and user space | |
104 | * uses the high bit(s) for pointer tagging and the like. For them, we | |
105 | * must preserve the expected address space. | |
288e65b9 | 106 | */ |
18e80c55 RH |
107 | #ifndef MAX_RESERVED_VA |
108 | # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS | |
109 | # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \ | |
110 | (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32)) | |
111 | /* There are a number of places where we assign reserved_va to a variable | |
112 | of type abi_ulong and expect it to fit. Avoid the last page. */ | |
8f67b9c6 | 113 | # define MAX_RESERVED_VA(CPU) (0xfffffffful & TARGET_PAGE_MASK) |
18e80c55 | 114 | # else |
8f67b9c6 | 115 | # define MAX_RESERVED_VA(CPU) (1ul << TARGET_VIRT_ADDR_SPACE_BITS) |
18e80c55 | 116 | # endif |
314992b1 | 117 | # else |
8f67b9c6 | 118 | # define MAX_RESERVED_VA(CPU) 0 |
314992b1 | 119 | # endif |
18e80c55 RH |
120 | #endif |
121 | ||
68a1c816 | 122 | unsigned long reserved_va; |
1b530a6d | 123 | |
d03f9c32 | 124 | static void usage(int exitcode); |
fc9c5412 | 125 | |
7ee2822c | 126 | static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX; |
e586822a | 127 | const char *qemu_uname_release; |
586314f2 | 128 | |
0a3346b5 | 129 | #if !defined(TARGET_DEFAULT_STACK_SIZE) |
9de5e440 FB |
130 | /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so |
131 | we allocate a bigger stack. Need a better solution, for example | |
132 | by remapping the process stack directly at the right place */ | |
0a3346b5 HD |
133 | #define TARGET_DEFAULT_STACK_SIZE 8 * 1024 * 1024UL |
134 | #endif | |
135 | ||
136 | unsigned long guest_stack_size = TARGET_DEFAULT_STACK_SIZE; | |
31e31b8a | 137 | |
d5975363 PB |
138 | /***********************************************************/ |
139 | /* Helper routines for implementing atomic operations. */ | |
140 | ||
d5975363 PB |
141 | /* Make sure everything is in a consistent state for calling fork(). */ |
142 | void fork_start(void) | |
143 | { | |
06065c45 | 144 | start_exclusive(); |
d032d1b4 | 145 | mmap_fork_start(); |
024949ca | 146 | cpu_list_lock(); |
f7e15aff | 147 | qemu_plugin_user_prefork_lock(); |
d5975363 PB |
148 | } |
149 | ||
150 | void fork_end(int child) | |
151 | { | |
f7e15aff | 152 | qemu_plugin_user_postfork(child); |
d032d1b4 | 153 | mmap_fork_end(child); |
d5975363 | 154 | if (child) { |
bdc44640 | 155 | CPUState *cpu, *next_cpu; |
d5975363 PB |
156 | /* Child processes created by fork() only have a single thread. |
157 | Discard information about the parent threads. */ | |
bdc44640 AF |
158 | CPU_FOREACH_SAFE(cpu, next_cpu) { |
159 | if (cpu != thread_cpu) { | |
068a5ea0 | 160 | QTAILQ_REMOVE_RCU(&cpus, cpu, node); |
bdc44640 AF |
161 | } |
162 | } | |
267f685b | 163 | qemu_init_cpu_list(); |
f7ec7f7b | 164 | gdbserver_fork(thread_cpu); |
d5975363 | 165 | } else { |
267f685b | 166 | cpu_list_unlock(); |
d5975363 | 167 | } |
7de0816f IL |
168 | /* |
169 | * qemu_init_cpu_list() reinitialized the child exclusive state, but we | |
170 | * also need to keep current_cpu consistent, so call end_exclusive() for | |
171 | * both child and parent. | |
172 | */ | |
173 | end_exclusive(); | |
d5975363 PB |
174 | } |
175 | ||
b44316fb | 176 | __thread CPUState *thread_cpu; |
59faf6d6 | 177 | |
178f9429 SF |
178 | bool qemu_cpu_is_self(CPUState *cpu) |
179 | { | |
180 | return thread_cpu == cpu; | |
181 | } | |
182 | ||
183 | void qemu_cpu_kick(CPUState *cpu) | |
184 | { | |
185 | cpu_exit(cpu); | |
186 | } | |
187 | ||
edf8e2af MW |
188 | void task_settid(TaskState *ts) |
189 | { | |
190 | if (ts->ts_tid == 0) { | |
edf8e2af | 191 | ts->ts_tid = (pid_t)syscall(SYS_gettid); |
edf8e2af MW |
192 | } |
193 | } | |
194 | ||
195 | void stop_all_tasks(void) | |
196 | { | |
197 | /* | |
198 | * We trust that when using NPTL, start_exclusive() | |
199 | * handles thread stopping correctly. | |
200 | */ | |
201 | start_exclusive(); | |
202 | } | |
203 | ||
c3a92833 | 204 | /* Assumes contents are already zeroed. */ |
624f7979 PB |
205 | void init_task_state(TaskState *ts) |
206 | { | |
eb33cdae CE |
207 | long ticks_per_sec; |
208 | struct timespec bt; | |
209 | ||
624f7979 | 210 | ts->used = 1; |
5bfce0b7 PM |
211 | ts->sigaltstack_used = (struct target_sigaltstack) { |
212 | .ss_sp = 0, | |
213 | .ss_size = 0, | |
214 | .ss_flags = TARGET_SS_DISABLE, | |
215 | }; | |
eb33cdae CE |
216 | |
217 | /* Capture task start time relative to system boot */ | |
218 | ||
219 | ticks_per_sec = sysconf(_SC_CLK_TCK); | |
220 | ||
221 | if ((ticks_per_sec > 0) && !clock_gettime(CLOCK_BOOTTIME, &bt)) { | |
222 | /* start_boottime is expressed in clock ticks */ | |
223 | ts->start_boottime = bt.tv_sec * (uint64_t) ticks_per_sec; | |
224 | ts->start_boottime += bt.tv_nsec * (uint64_t) ticks_per_sec / | |
225 | NANOSECONDS_PER_SECOND; | |
226 | } | |
624f7979 | 227 | } |
fc9c5412 | 228 | |
30ba0ee5 AF |
229 | CPUArchState *cpu_copy(CPUArchState *env) |
230 | { | |
29a0af61 | 231 | CPUState *cpu = env_cpu(env); |
2278b939 | 232 | CPUState *new_cpu = cpu_create(cpu_type); |
61c7480f | 233 | CPUArchState *new_env = new_cpu->env_ptr; |
30ba0ee5 | 234 | CPUBreakpoint *bp; |
30ba0ee5 AF |
235 | |
236 | /* Reset non arch specific state */ | |
75a34036 | 237 | cpu_reset(new_cpu); |
30ba0ee5 | 238 | |
6cc9d67c | 239 | new_cpu->tcg_cflags = cpu->tcg_cflags; |
30ba0ee5 AF |
240 | memcpy(new_env, env, sizeof(CPUArchState)); |
241 | ||
242 | /* Clone all break/watchpoints. | |
243 | Note: Once we support ptrace with hw-debug register access, make sure | |
244 | BP_CPU break/watchpoints are handled correctly on clone. */ | |
1d085f6c | 245 | QTAILQ_INIT(&new_cpu->breakpoints); |
f0c3c505 | 246 | QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) { |
b3310ab3 | 247 | cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL); |
30ba0ee5 | 248 | } |
30ba0ee5 AF |
249 | |
250 | return new_env; | |
251 | } | |
252 | ||
fc9c5412 JS |
253 | static void handle_arg_help(const char *arg) |
254 | { | |
4d1275c2 | 255 | usage(EXIT_SUCCESS); |
fc9c5412 JS |
256 | } |
257 | ||
258 | static void handle_arg_log(const char *arg) | |
259 | { | |
4b25a506 JK |
260 | last_log_mask = qemu_str_to_log_mask(arg); |
261 | if (!last_log_mask) { | |
59a6fa6e | 262 | qemu_print_log_usage(stdout); |
4d1275c2 | 263 | exit(EXIT_FAILURE); |
fc9c5412 | 264 | } |
fc9c5412 JS |
265 | } |
266 | ||
8423fa90 AB |
267 | static void handle_arg_dfilter(const char *arg) |
268 | { | |
7f4341e8 | 269 | qemu_set_dfilter_ranges(arg, &error_fatal); |
8423fa90 AB |
270 | } |
271 | ||
50171d42 CWR |
272 | static void handle_arg_log_filename(const char *arg) |
273 | { | |
b410253f | 274 | last_log_filename = arg; |
50171d42 CWR |
275 | } |
276 | ||
fc9c5412 JS |
277 | static void handle_arg_set_env(const char *arg) |
278 | { | |
279 | char *r, *p, *token; | |
280 | r = p = strdup(arg); | |
281 | while ((token = strsep(&p, ",")) != NULL) { | |
282 | if (envlist_setenv(envlist, token) != 0) { | |
4d1275c2 | 283 | usage(EXIT_FAILURE); |
fc9c5412 JS |
284 | } |
285 | } | |
286 | free(r); | |
287 | } | |
288 | ||
289 | static void handle_arg_unset_env(const char *arg) | |
290 | { | |
291 | char *r, *p, *token; | |
292 | r = p = strdup(arg); | |
293 | while ((token = strsep(&p, ",")) != NULL) { | |
294 | if (envlist_unsetenv(envlist, token) != 0) { | |
4d1275c2 | 295 | usage(EXIT_FAILURE); |
fc9c5412 JS |
296 | } |
297 | } | |
298 | free(r); | |
299 | } | |
300 | ||
301 | static void handle_arg_argv0(const char *arg) | |
302 | { | |
303 | argv0 = strdup(arg); | |
304 | } | |
305 | ||
306 | static void handle_arg_stack_size(const char *arg) | |
307 | { | |
308 | char *p; | |
309 | guest_stack_size = strtoul(arg, &p, 0); | |
310 | if (guest_stack_size == 0) { | |
4d1275c2 | 311 | usage(EXIT_FAILURE); |
fc9c5412 JS |
312 | } |
313 | ||
314 | if (*p == 'M') { | |
b52713c1 | 315 | guest_stack_size *= MiB; |
fc9c5412 | 316 | } else if (*p == 'k' || *p == 'K') { |
b52713c1 | 317 | guest_stack_size *= KiB; |
fc9c5412 JS |
318 | } |
319 | } | |
320 | ||
321 | static void handle_arg_ld_prefix(const char *arg) | |
322 | { | |
323 | interp_prefix = strdup(arg); | |
324 | } | |
325 | ||
326 | static void handle_arg_pagesize(const char *arg) | |
327 | { | |
328 | qemu_host_page_size = atoi(arg); | |
329 | if (qemu_host_page_size == 0 || | |
330 | (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) { | |
331 | fprintf(stderr, "page size must be a power of two\n"); | |
4d1275c2 | 332 | exit(EXIT_FAILURE); |
fc9c5412 JS |
333 | } |
334 | } | |
335 | ||
5ebdd774 | 336 | static void handle_arg_seed(const char *arg) |
c5e4a5a9 | 337 | { |
5ebdd774 | 338 | seed_optarg = arg; |
c5e4a5a9 MR |
339 | } |
340 | ||
fc9c5412 JS |
341 | static void handle_arg_gdb(const char *arg) |
342 | { | |
fcedd920 | 343 | gdbstub = g_strdup(arg); |
fc9c5412 JS |
344 | } |
345 | ||
346 | static void handle_arg_uname(const char *arg) | |
347 | { | |
348 | qemu_uname_release = strdup(arg); | |
349 | } | |
350 | ||
351 | static void handle_arg_cpu(const char *arg) | |
352 | { | |
353 | cpu_model = strdup(arg); | |
c8057f95 | 354 | if (cpu_model == NULL || is_help_option(cpu_model)) { |
fc9c5412 | 355 | /* XXX: implement xxx_cpu_list for targets that still miss it */ |
e916cbf8 | 356 | #if defined(cpu_list) |
0442428a | 357 | cpu_list(); |
fc9c5412 | 358 | #endif |
4d1275c2 | 359 | exit(EXIT_FAILURE); |
fc9c5412 JS |
360 | } |
361 | } | |
362 | ||
fc9c5412 JS |
363 | static void handle_arg_guest_base(const char *arg) |
364 | { | |
365 | guest_base = strtol(arg, NULL, 0); | |
e307c192 | 366 | have_guest_base = true; |
fc9c5412 JS |
367 | } |
368 | ||
369 | static void handle_arg_reserved_va(const char *arg) | |
370 | { | |
371 | char *p; | |
372 | int shift = 0; | |
373 | reserved_va = strtoul(arg, &p, 0); | |
374 | switch (*p) { | |
375 | case 'k': | |
376 | case 'K': | |
377 | shift = 10; | |
378 | break; | |
379 | case 'M': | |
380 | shift = 20; | |
381 | break; | |
382 | case 'G': | |
383 | shift = 30; | |
384 | break; | |
385 | } | |
386 | if (shift) { | |
387 | unsigned long unshifted = reserved_va; | |
388 | p++; | |
389 | reserved_va <<= shift; | |
8f67b9c6 | 390 | if (reserved_va >> shift != unshifted) { |
fc9c5412 | 391 | fprintf(stderr, "Reserved virtual address too big\n"); |
4d1275c2 | 392 | exit(EXIT_FAILURE); |
fc9c5412 JS |
393 | } |
394 | } | |
395 | if (*p) { | |
396 | fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p); | |
4d1275c2 | 397 | exit(EXIT_FAILURE); |
fc9c5412 JS |
398 | } |
399 | } | |
fc9c5412 JS |
400 | |
401 | static void handle_arg_singlestep(const char *arg) | |
402 | { | |
403 | singlestep = 1; | |
404 | } | |
405 | ||
406 | static void handle_arg_strace(const char *arg) | |
407 | { | |
4b25a506 | 408 | enable_strace = true; |
fc9c5412 JS |
409 | } |
410 | ||
411 | static void handle_arg_version(const char *arg) | |
412 | { | |
7e563bfb | 413 | printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION |
0781dd6e | 414 | "\n" QEMU_COPYRIGHT "\n"); |
4d1275c2 | 415 | exit(EXIT_SUCCESS); |
fc9c5412 JS |
416 | } |
417 | ||
6533dd6e LV |
418 | static void handle_arg_trace(const char *arg) |
419 | { | |
92eecfff | 420 | trace_opt_parse(arg); |
6533dd6e LV |
421 | } |
422 | ||
130ea832 MF |
423 | #if defined(TARGET_XTENSA) |
424 | static void handle_arg_abi_call0(const char *arg) | |
425 | { | |
426 | xtensa_set_abi_call0(); | |
427 | } | |
428 | #endif | |
429 | ||
5584e2db IL |
430 | static void handle_arg_perfmap(const char *arg) |
431 | { | |
432 | perf_enable_perfmap(); | |
433 | } | |
434 | ||
435 | static void handle_arg_jitdump(const char *arg) | |
436 | { | |
437 | perf_enable_jitdump(); | |
438 | } | |
439 | ||
f308f64e LV |
440 | static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins); |
441 | ||
442 | #ifdef CONFIG_PLUGIN | |
443 | static void handle_arg_plugin(const char *arg) | |
444 | { | |
445 | qemu_plugin_opt_parse(arg, &plugins); | |
446 | } | |
447 | #endif | |
448 | ||
fc9c5412 JS |
449 | struct qemu_argument { |
450 | const char *argv; | |
451 | const char *env; | |
452 | bool has_arg; | |
453 | void (*handle_opt)(const char *arg); | |
454 | const char *example; | |
455 | const char *help; | |
456 | }; | |
457 | ||
42644cee | 458 | static const struct qemu_argument arg_table[] = { |
fc9c5412 JS |
459 | {"h", "", false, handle_arg_help, |
460 | "", "print this help"}, | |
daaf8c8e MI |
461 | {"help", "", false, handle_arg_help, |
462 | "", ""}, | |
fc9c5412 JS |
463 | {"g", "QEMU_GDB", true, handle_arg_gdb, |
464 | "port", "wait gdb connection to 'port'"}, | |
465 | {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix, | |
466 | "path", "set the elf interpreter prefix to 'path'"}, | |
467 | {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size, | |
468 | "size", "set the stack size to 'size' bytes"}, | |
469 | {"cpu", "QEMU_CPU", true, handle_arg_cpu, | |
c8057f95 | 470 | "model", "select CPU (-cpu help for list)"}, |
fc9c5412 JS |
471 | {"E", "QEMU_SET_ENV", true, handle_arg_set_env, |
472 | "var=value", "sets targets environment variable (see below)"}, | |
473 | {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env, | |
474 | "var", "unsets targets environment variable (see below)"}, | |
475 | {"0", "QEMU_ARGV0", true, handle_arg_argv0, | |
476 | "argv0", "forces target process argv[0] to be 'argv0'"}, | |
477 | {"r", "QEMU_UNAME", true, handle_arg_uname, | |
478 | "uname", "set qemu uname release string to 'uname'"}, | |
fc9c5412 JS |
479 | {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base, |
480 | "address", "set guest_base address to 'address'"}, | |
481 | {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va, | |
482 | "size", "reserve 'size' bytes for guest virtual address space"}, | |
fc9c5412 | 483 | {"d", "QEMU_LOG", true, handle_arg_log, |
989b697d PM |
484 | "item[,...]", "enable logging of specified items " |
485 | "(use '-d help' for a list of items)"}, | |
8423fa90 AB |
486 | {"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter, |
487 | "range[,...]","filter logging based on address range"}, | |
50171d42 | 488 | {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename, |
989b697d | 489 | "logfile", "write logs to 'logfile' (default stderr)"}, |
fc9c5412 JS |
490 | {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize, |
491 | "pagesize", "set the host page size to 'pagesize'"}, | |
492 | {"singlestep", "QEMU_SINGLESTEP", false, handle_arg_singlestep, | |
493 | "", "run in singlestep mode"}, | |
494 | {"strace", "QEMU_STRACE", false, handle_arg_strace, | |
495 | "", "log system calls"}, | |
5ebdd774 | 496 | {"seed", "QEMU_RAND_SEED", true, handle_arg_seed, |
c5e4a5a9 | 497 | "", "Seed for pseudo-random number generator"}, |
6533dd6e LV |
498 | {"trace", "QEMU_TRACE", true, handle_arg_trace, |
499 | "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"}, | |
f308f64e LV |
500 | #ifdef CONFIG_PLUGIN |
501 | {"plugin", "QEMU_PLUGIN", true, handle_arg_plugin, | |
3a445acb | 502 | "", "[file=]<file>[,<argname>=<argvalue>]"}, |
f308f64e | 503 | #endif |
fc9c5412 | 504 | {"version", "QEMU_VERSION", false, handle_arg_version, |
1386d4c0 | 505 | "", "display version information and exit"}, |
130ea832 MF |
506 | #if defined(TARGET_XTENSA) |
507 | {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0, | |
508 | "", "assume CALL0 Xtensa ABI"}, | |
509 | #endif | |
5584e2db IL |
510 | {"perfmap", "QEMU_PERFMAP", false, handle_arg_perfmap, |
511 | "", "Generate a /tmp/perf-${pid}.map file for perf"}, | |
512 | {"jitdump", "QEMU_JITDUMP", false, handle_arg_jitdump, | |
513 | "", "Generate a jit-${pid}.dump file for perf"}, | |
fc9c5412 JS |
514 | {NULL, NULL, false, NULL, NULL, NULL} |
515 | }; | |
516 | ||
d03f9c32 | 517 | static void usage(int exitcode) |
fc9c5412 | 518 | { |
42644cee | 519 | const struct qemu_argument *arginfo; |
fc9c5412 JS |
520 | int maxarglen; |
521 | int maxenvlen; | |
522 | ||
2e59915d PB |
523 | printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n" |
524 | "Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n" | |
fc9c5412 JS |
525 | "\n" |
526 | "Options and associated environment variables:\n" | |
527 | "\n"); | |
528 | ||
63ec54d7 PM |
529 | /* Calculate column widths. We must always have at least enough space |
530 | * for the column header. | |
531 | */ | |
532 | maxarglen = strlen("Argument"); | |
533 | maxenvlen = strlen("Env-variable"); | |
fc9c5412 JS |
534 | |
535 | for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { | |
63ec54d7 PM |
536 | int arglen = strlen(arginfo->argv); |
537 | if (arginfo->has_arg) { | |
538 | arglen += strlen(arginfo->example) + 1; | |
539 | } | |
fc9c5412 JS |
540 | if (strlen(arginfo->env) > maxenvlen) { |
541 | maxenvlen = strlen(arginfo->env); | |
542 | } | |
63ec54d7 PM |
543 | if (arglen > maxarglen) { |
544 | maxarglen = arglen; | |
fc9c5412 JS |
545 | } |
546 | } | |
547 | ||
63ec54d7 PM |
548 | printf("%-*s %-*s Description\n", maxarglen+1, "Argument", |
549 | maxenvlen, "Env-variable"); | |
fc9c5412 JS |
550 | |
551 | for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { | |
552 | if (arginfo->has_arg) { | |
553 | printf("-%s %-*s %-*s %s\n", arginfo->argv, | |
63ec54d7 PM |
554 | (int)(maxarglen - strlen(arginfo->argv) - 1), |
555 | arginfo->example, maxenvlen, arginfo->env, arginfo->help); | |
fc9c5412 | 556 | } else { |
63ec54d7 | 557 | printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv, |
fc9c5412 JS |
558 | maxenvlen, arginfo->env, |
559 | arginfo->help); | |
560 | } | |
561 | } | |
562 | ||
563 | printf("\n" | |
564 | "Defaults:\n" | |
565 | "QEMU_LD_PREFIX = %s\n" | |
989b697d | 566 | "QEMU_STACK_SIZE = %ld byte\n", |
fc9c5412 | 567 | interp_prefix, |
989b697d | 568 | guest_stack_size); |
fc9c5412 JS |
569 | |
570 | printf("\n" | |
571 | "You can use -E and -U options or the QEMU_SET_ENV and\n" | |
572 | "QEMU_UNSET_ENV environment variables to set and unset\n" | |
573 | "environment variables for the target process.\n" | |
574 | "It is possible to provide several variables by separating them\n" | |
575 | "by commas in getsubopt(3) style. Additionally it is possible to\n" | |
576 | "provide the -E and -U options multiple times.\n" | |
577 | "The following lines are equivalent:\n" | |
578 | " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n" | |
579 | " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n" | |
580 | " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n" | |
581 | "Note that if you provide several changes to a single variable\n" | |
f5048cb7 EB |
582 | "the last change will stay in effect.\n" |
583 | "\n" | |
584 | QEMU_HELP_BOTTOM "\n"); | |
fc9c5412 | 585 | |
d03f9c32 | 586 | exit(exitcode); |
fc9c5412 JS |
587 | } |
588 | ||
589 | static int parse_args(int argc, char **argv) | |
590 | { | |
591 | const char *r; | |
592 | int optind; | |
42644cee | 593 | const struct qemu_argument *arginfo; |
fc9c5412 JS |
594 | |
595 | for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { | |
596 | if (arginfo->env == NULL) { | |
597 | continue; | |
598 | } | |
599 | ||
600 | r = getenv(arginfo->env); | |
601 | if (r != NULL) { | |
602 | arginfo->handle_opt(r); | |
603 | } | |
604 | } | |
605 | ||
606 | optind = 1; | |
607 | for (;;) { | |
608 | if (optind >= argc) { | |
609 | break; | |
610 | } | |
611 | r = argv[optind]; | |
612 | if (r[0] != '-') { | |
613 | break; | |
614 | } | |
615 | optind++; | |
616 | r++; | |
617 | if (!strcmp(r, "-")) { | |
618 | break; | |
619 | } | |
ba02577c MI |
620 | /* Treat --foo the same as -foo. */ |
621 | if (r[0] == '-') { | |
622 | r++; | |
623 | } | |
fc9c5412 JS |
624 | |
625 | for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) { | |
626 | if (!strcmp(r, arginfo->argv)) { | |
fc9c5412 | 627 | if (arginfo->has_arg) { |
1386d4c0 | 628 | if (optind >= argc) { |
138940bf MI |
629 | (void) fprintf(stderr, |
630 | "qemu: missing argument for option '%s'\n", r); | |
4d1275c2 | 631 | exit(EXIT_FAILURE); |
1386d4c0 PM |
632 | } |
633 | arginfo->handle_opt(argv[optind]); | |
fc9c5412 | 634 | optind++; |
1386d4c0 PM |
635 | } else { |
636 | arginfo->handle_opt(NULL); | |
fc9c5412 | 637 | } |
fc9c5412 JS |
638 | break; |
639 | } | |
640 | } | |
641 | ||
642 | /* no option matched the current argv */ | |
643 | if (arginfo->handle_opt == NULL) { | |
138940bf | 644 | (void) fprintf(stderr, "qemu: unknown option '%s'\n", r); |
4d1275c2 | 645 | exit(EXIT_FAILURE); |
fc9c5412 JS |
646 | } |
647 | } | |
648 | ||
649 | if (optind >= argc) { | |
138940bf | 650 | (void) fprintf(stderr, "qemu: no user program specified\n"); |
4d1275c2 | 651 | exit(EXIT_FAILURE); |
fc9c5412 JS |
652 | } |
653 | ||
fc9c5412 JS |
654 | exec_path = argv[optind]; |
655 | ||
656 | return optind; | |
657 | } | |
658 | ||
902b3d5c | 659 | int main(int argc, char **argv, char **envp) |
31e31b8a | 660 | { |
01ffc75b | 661 | struct target_pt_regs regs1, *regs = ®s1; |
31e31b8a | 662 | struct image_info info1, *info = &info1; |
edf8e2af | 663 | struct linux_binprm bprm; |
48e15fc2 | 664 | TaskState *ts; |
9349b4f9 | 665 | CPUArchState *env; |
db6b81d4 | 666 | CPUState *cpu; |
586314f2 | 667 | int optind; |
04a6dfeb | 668 | char **target_environ, **wrk; |
7d8cec95 AJ |
669 | char **target_argv; |
670 | int target_argc; | |
7d8cec95 | 671 | int i; |
fd4d81dd | 672 | int ret; |
03cfd8fa | 673 | int execfd; |
8f67b9c6 | 674 | unsigned long max_reserved_va; |
6e1c0d7b | 675 | bool preserve_argv0; |
b12b6a18 | 676 | |
f5852efa | 677 | error_init(argv[0]); |
fe4db84d | 678 | module_call_init(MODULE_INIT_TRACE); |
267f685b | 679 | qemu_init_cpu_list(); |
ce008c1f AF |
680 | module_call_init(MODULE_INIT_QOM); |
681 | ||
ec45bbe5 | 682 | envlist = envlist_create(); |
04a6dfeb AJ |
683 | |
684 | /* add current environment into the list */ | |
685 | for (wrk = environ; *wrk != NULL; wrk++) { | |
686 | (void) envlist_setenv(envlist, *wrk); | |
687 | } | |
688 | ||
703e0e89 RH |
689 | /* Read the stack limit from the kernel. If it's "unlimited", |
690 | then we can do little else besides use the default. */ | |
691 | { | |
692 | struct rlimit lim; | |
693 | if (getrlimit(RLIMIT_STACK, &lim) == 0 | |
81bbe906 | 694 | && lim.rlim_cur != RLIM_INFINITY |
0a3346b5 HD |
695 | && lim.rlim_cur == (target_long)lim.rlim_cur |
696 | && lim.rlim_cur > guest_stack_size) { | |
703e0e89 RH |
697 | guest_stack_size = lim.rlim_cur; |
698 | } | |
699 | } | |
700 | ||
b1f9be31 | 701 | cpu_model = NULL; |
b5ec5ce0 | 702 | |
6533dd6e | 703 | qemu_add_opts(&qemu_trace_opts); |
f308f64e | 704 | qemu_plugin_add_opts(); |
6533dd6e | 705 | |
fc9c5412 | 706 | optind = parse_args(argc, argv); |
586314f2 | 707 | |
b410253f RH |
708 | qemu_set_log_filename_flags(last_log_filename, |
709 | last_log_mask | (enable_strace * LOG_STRACE), | |
710 | &error_fatal); | |
4b25a506 | 711 | |
6533dd6e LV |
712 | if (!trace_init_backends()) { |
713 | exit(1); | |
714 | } | |
92eecfff | 715 | trace_init_file(); |
0572f558 | 716 | qemu_plugin_load_list(&plugins, &error_fatal); |
6533dd6e | 717 | |
31e31b8a | 718 | /* Zero out regs */ |
01ffc75b | 719 | memset(regs, 0, sizeof(struct target_pt_regs)); |
31e31b8a FB |
720 | |
721 | /* Zero out image_info */ | |
722 | memset(info, 0, sizeof(struct image_info)); | |
723 | ||
edf8e2af MW |
724 | memset(&bprm, 0, sizeof (bprm)); |
725 | ||
74cd30b8 FB |
726 | /* Scan interp_prefix dir for replacement files. */ |
727 | init_paths(interp_prefix); | |
728 | ||
4a24a758 PM |
729 | init_qemu_uname_release(); |
730 | ||
6e1c0d7b LV |
731 | /* |
732 | * Manage binfmt-misc open-binary flag | |
733 | */ | |
768fe76e YS |
734 | execfd = qemu_getauxval(AT_EXECFD); |
735 | if (execfd == 0) { | |
9d3019bc | 736 | execfd = open(exec_path, O_RDONLY); |
768fe76e | 737 | if (execfd < 0) { |
9d3019bc | 738 | printf("Error while loading %s: %s\n", exec_path, strerror(errno)); |
768fe76e YS |
739 | _exit(EXIT_FAILURE); |
740 | } | |
741 | } | |
742 | ||
6e1c0d7b LV |
743 | /* |
744 | * get binfmt_misc flags | |
745 | */ | |
746 | preserve_argv0 = !!(qemu_getauxval(AT_FLAGS) & AT_FLAGS_PRESERVE_ARGV0); | |
747 | ||
748 | /* | |
749 | * Manage binfmt-misc preserve-arg[0] flag | |
750 | * argv[optind] full path to the binary | |
751 | * argv[optind + 1] original argv[0] | |
752 | */ | |
753 | if (optind + 1 < argc && preserve_argv0) { | |
754 | optind++; | |
755 | } | |
756 | ||
46027c07 | 757 | if (cpu_model == NULL) { |
768fe76e | 758 | cpu_model = cpu_get_model(get_elf_eflags(execfd)); |
aaed909a | 759 | } |
c1c8cfe5 | 760 | cpu_type = parse_cpu_option(cpu_model); |
2278b939 | 761 | |
2b5249b8 | 762 | /* init tcg before creating CPUs and to get qemu_host_page_size */ |
940e43aa CF |
763 | { |
764 | AccelClass *ac = ACCEL_GET_CLASS(current_accel()); | |
2278b939 | 765 | |
b86f59c7 | 766 | accel_init_interfaces(ac); |
92242f34 | 767 | ac->init_machine(NULL); |
940e43aa | 768 | } |
2278b939 | 769 | cpu = cpu_create(cpu_type); |
2994fd96 | 770 | env = cpu->env_ptr; |
0ac46af3 | 771 | cpu_reset(cpu); |
db6b81d4 | 772 | thread_cpu = cpu; |
3b46e624 | 773 | |
8f67b9c6 RH |
774 | /* |
775 | * Reserving too much vm space via mmap can run into problems | |
776 | * with rlimits, oom due to page table creation, etc. We will | |
777 | * still try it, if directed by the command-line option, but | |
778 | * not by default. | |
779 | */ | |
780 | max_reserved_va = MAX_RESERVED_VA(cpu); | |
781 | if (reserved_va != 0) { | |
782 | if (max_reserved_va && reserved_va > max_reserved_va) { | |
783 | fprintf(stderr, "Reserved virtual address too big\n"); | |
784 | exit(EXIT_FAILURE); | |
785 | } | |
786 | } else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) { | |
787 | /* | |
788 | * reserved_va must be aligned with the host page size | |
789 | * as it is used with mmap() | |
790 | */ | |
791 | reserved_va = max_reserved_va & qemu_host_page_mask; | |
792 | } | |
793 | ||
a573e9ba RH |
794 | { |
795 | Error *err = NULL; | |
796 | if (seed_optarg != NULL) { | |
a573e9ba RH |
797 | qemu_guest_random_seed_main(seed_optarg, &err); |
798 | } else { | |
799 | qcrypto_init(&err); | |
800 | } | |
801 | if (err) { | |
802 | error_reportf_err(err, "cannot initialize crypto: "); | |
803 | exit(1); | |
5ebdd774 | 804 | } |
c5e4a5a9 MR |
805 | } |
806 | ||
04a6dfeb AJ |
807 | target_environ = envlist_to_environ(envlist, NULL); |
808 | envlist_free(envlist); | |
b12b6a18 | 809 | |
379f6698 PB |
810 | /* |
811 | * Read in mmap_min_addr kernel parameter. This value is used | |
812 | * When loading the ELF image to determine whether guest_base | |
14f24e14 | 813 | * is needed. It is also used in mmap_find_vma. |
379f6698 | 814 | */ |
14f24e14 | 815 | { |
379f6698 PB |
816 | FILE *fp; |
817 | ||
818 | if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) { | |
819 | unsigned long tmp; | |
c9f80666 | 820 | if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) { |
379f6698 | 821 | mmap_min_addr = tmp; |
c9f80666 RH |
822 | qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n", |
823 | mmap_min_addr); | |
379f6698 PB |
824 | } |
825 | fclose(fp); | |
826 | } | |
827 | } | |
379f6698 | 828 | |
c9f80666 RH |
829 | /* |
830 | * We prefer to not make NULL pointers accessible to QEMU. | |
831 | * If we're in a chroot with no /proc, fall back to 1 page. | |
832 | */ | |
833 | if (mmap_min_addr == 0) { | |
834 | mmap_min_addr = qemu_host_page_size; | |
835 | qemu_log_mask(CPU_LOG_PAGE, | |
836 | "host mmap_min_addr=0x%lx (fallback)\n", | |
837 | mmap_min_addr); | |
838 | } | |
839 | ||
7d8cec95 AJ |
840 | /* |
841 | * Prepare copy of argv vector for target. | |
842 | */ | |
843 | target_argc = argc - optind; | |
844 | target_argv = calloc(target_argc + 1, sizeof (char *)); | |
845 | if (target_argv == NULL) { | |
7d37435b PB |
846 | (void) fprintf(stderr, "Unable to allocate memory for target_argv\n"); |
847 | exit(EXIT_FAILURE); | |
7d8cec95 AJ |
848 | } |
849 | ||
850 | /* | |
851 | * If argv0 is specified (using '-0' switch) we replace | |
852 | * argv[0] pointer with the given one. | |
853 | */ | |
854 | i = 0; | |
855 | if (argv0 != NULL) { | |
856 | target_argv[i++] = strdup(argv0); | |
857 | } | |
858 | for (; i < target_argc; i++) { | |
859 | target_argv[i] = strdup(argv[optind + i]); | |
860 | } | |
861 | target_argv[target_argc] = NULL; | |
862 | ||
c78d65e8 | 863 | ts = g_new0(TaskState, 1); |
edf8e2af MW |
864 | init_task_state(ts); |
865 | /* build Task State */ | |
866 | ts->info = info; | |
867 | ts->bprm = &bprm; | |
0429a971 | 868 | cpu->opaque = ts; |
edf8e2af MW |
869 | task_settid(ts); |
870 | ||
c093364f OA |
871 | fd_trans_init(); |
872 | ||
9d3019bc | 873 | ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs, |
fd4d81dd AP |
874 | info, &bprm); |
875 | if (ret != 0) { | |
9d3019bc | 876 | printf("Error while loading %s: %s\n", exec_path, strerror(-ret)); |
4d1275c2 | 877 | _exit(EXIT_FAILURE); |
b12b6a18 TS |
878 | } |
879 | ||
880 | for (wrk = target_environ; *wrk; wrk++) { | |
ec45bbe5 | 881 | g_free(*wrk); |
31e31b8a | 882 | } |
3b46e624 | 883 | |
ec45bbe5 | 884 | g_free(target_environ); |
b12b6a18 | 885 | |
13829020 | 886 | if (qemu_loglevel_mask(CPU_LOG_PAGE)) { |
93756fdc RH |
887 | FILE *f = qemu_log_trylock(); |
888 | if (f) { | |
889 | fprintf(f, "guest_base %p\n", (void *)guest_base); | |
890 | fprintf(f, "page layout changed following binary load\n"); | |
891 | page_dump(f); | |
892 | ||
893 | fprintf(f, "start_brk 0x" TARGET_ABI_FMT_lx "\n", | |
894 | info->start_brk); | |
895 | fprintf(f, "end_code 0x" TARGET_ABI_FMT_lx "\n", | |
896 | info->end_code); | |
897 | fprintf(f, "start_code 0x" TARGET_ABI_FMT_lx "\n", | |
898 | info->start_code); | |
899 | fprintf(f, "start_data 0x" TARGET_ABI_FMT_lx "\n", | |
900 | info->start_data); | |
901 | fprintf(f, "end_data 0x" TARGET_ABI_FMT_lx "\n", | |
902 | info->end_data); | |
903 | fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n", | |
904 | info->start_stack); | |
905 | fprintf(f, "brk 0x" TARGET_ABI_FMT_lx "\n", | |
906 | info->brk); | |
907 | fprintf(f, "entry 0x" TARGET_ABI_FMT_lx "\n", | |
908 | info->entry); | |
909 | fprintf(f, "argv_start 0x" TARGET_ABI_FMT_lx "\n", | |
60f1c801 | 910 | info->argv); |
93756fdc | 911 | fprintf(f, "env_start 0x" TARGET_ABI_FMT_lx "\n", |
60f1c801 | 912 | info->envp); |
93756fdc RH |
913 | fprintf(f, "auxv_start 0x" TARGET_ABI_FMT_lx "\n", |
914 | info->saved_auxv); | |
915 | qemu_log_unlock(f); | |
916 | } | |
2e77eac6 | 917 | } |
31e31b8a | 918 | |
53a5960a | 919 | target_set_brk(info->brk); |
31e31b8a | 920 | syscall_init(); |
66fb9763 | 921 | signal_init(); |
31e31b8a | 922 | |
9002ec79 RH |
923 | /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay |
924 | generating the prologue until now so that the prologue can take | |
925 | the real value of GUEST_BASE into account. */ | |
b1311c4a | 926 | tcg_prologue_init(tcg_ctx); |
9002ec79 | 927 | |
cd71c089 LV |
928 | target_cpu_copy_regs(env, regs); |
929 | ||
fcedd920 AB |
930 | if (gdbstub) { |
931 | if (gdbserver_start(gdbstub) < 0) { | |
932 | fprintf(stderr, "qemu: could not open gdbserver on %s\n", | |
933 | gdbstub); | |
4d1275c2 | 934 | exit(EXIT_FAILURE); |
ff7a981a | 935 | } |
db6b81d4 | 936 | gdb_handlesig(cpu, 0); |
1fddef4b | 937 | } |
e4a4aaa5 RH |
938 | |
939 | #ifdef CONFIG_SEMIHOSTING | |
940 | qemu_semihosting_guestfd_init(); | |
941 | #endif | |
942 | ||
1b6b029e FB |
943 | cpu_loop(env); |
944 | /* never exits */ | |
31e31b8a FB |
945 | return 0; |
946 | } |