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/>.
20 #include "qemu/osdep.h"
21 #include "qemu/help-texts.h"
22 #include "qemu/units.h"
23 #include "qemu/accel.h"
24 #include "qemu-version.h"
25 #include <sys/syscall.h>
26 #include <sys/resource.h>
28 #include <linux/binfmts.h>
30 #include "qapi/error.h"
32 #include "user-internals.h"
33 #include "qemu/path.h"
34 #include "qemu/queue.h"
35 #include "qemu/config-file.h"
36 #include "qemu/cutils.h"
37 #include "qemu/error-report.h"
38 #include "qemu/help_option.h"
39 #include "qemu/module.h"
40 #include "qemu/plugin.h"
41 #include "user/guest-base.h"
42 #include "user/page-protection.h"
43 #include "exec/gdbstub.h"
44 #include "gdbstub/user.h"
45 #include "tcg/startup.h"
46 #include "qemu/timer.h"
47 #include "qemu/envlist.h"
48 #include "qemu/guest-random.h"
50 #include "trace/control.h"
51 #include "target_elf.h"
52 #include "user/cpu_loop.h"
53 #include "crypto/init.h"
55 #include "signal-common.h"
57 #include "user-mmap.h"
59 #include "exec/page-vary.h"
61 #ifdef CONFIG_SEMIHOSTING
62 #include "semihosting/semihost.h"
65 #ifndef AT_FLAGS_PRESERVE_ARGV0
66 #define AT_FLAGS_PRESERVE_ARGV0_BIT 0
67 #define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT)
71 char real_exec_path
[PATH_MAX
];
73 static bool opt_one_insn_per_tb
;
74 static unsigned long opt_tb_size
;
75 static const char *argv0
;
76 static const char *gdbstub
;
77 static envlist_t
*envlist
;
78 static const char *cpu_model
;
79 static const char *cpu_type
;
80 static const char *seed_optarg
;
81 unsigned long mmap_min_addr
;
86 * Used to implement backwards-compatibility for the `-strace`, and
87 * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
88 * -strace, or vice versa.
90 static bool enable_strace
;
93 * The last log mask given by the user in an environment variable or argument.
94 * Used to support command line arguments overriding environment variables.
96 static int last_log_mask
;
97 static const char *last_log_filename
;
100 * When running 32-on-64 we should make sure we can fit all of the possible
101 * guest address space into a contiguous chunk of virtual host memory.
103 * This way we will never overlap with our own libraries or binaries or stack
104 * or anything else that QEMU maps.
106 * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
107 * of the address for the kernel. Some cpus rely on this and user space
108 * uses the high bit(s) for pointer tagging and the like. For them, we
109 * must preserve the expected address space.
111 #ifndef MAX_RESERVED_VA
112 # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
113 # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
114 (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
115 # define MAX_RESERVED_VA(CPU) 0xfffffffful
117 # define MAX_RESERVED_VA(CPU) ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
120 # define MAX_RESERVED_VA(CPU) 0
124 unsigned long reserved_va
;
125 unsigned long guest_addr_max
;
127 static void usage(int exitcode
);
129 static const char *interp_prefix
= CONFIG_QEMU_INTERP_PREFIX
;
130 const char *qemu_uname_release
;
132 #if !defined(TARGET_DEFAULT_STACK_SIZE)
133 /* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
134 we allocate a bigger stack. Need a better solution, for example
135 by remapping the process stack directly at the right place */
136 #define TARGET_DEFAULT_STACK_SIZE 8 * 1024 * 1024UL
139 unsigned long guest_stack_size
= TARGET_DEFAULT_STACK_SIZE
;
141 /***********************************************************/
142 /* Helper routines for implementing atomic operations. */
144 /* Make sure everything is in a consistent state for calling fork(). */
145 void fork_start(void)
150 qemu_plugin_user_prefork_lock();
151 gdbserver_fork_start();
154 void fork_end(pid_t pid
)
156 bool child
= pid
== 0;
158 qemu_plugin_user_postfork(child
);
159 mmap_fork_end(child
);
161 CPUState
*cpu
, *next_cpu
;
162 /* Child processes created by fork() only have a single thread.
163 Discard information about the parent threads. */
164 CPU_FOREACH_SAFE(cpu
, next_cpu
) {
165 if (cpu
!= thread_cpu
) {
166 QTAILQ_REMOVE_RCU(&cpus_queue
, cpu
, node
);
169 qemu_init_cpu_list();
170 get_task_state(thread_cpu
)->ts_tid
= qemu_get_thread_id();
174 gdbserver_fork_end(thread_cpu
, pid
);
176 * qemu_init_cpu_list() reinitialized the child exclusive state, but we
177 * also need to keep current_cpu consistent, so call end_exclusive() for
178 * both child and parent.
183 __thread CPUState
*thread_cpu
;
185 bool qemu_cpu_is_self(CPUState
*cpu
)
187 return thread_cpu
== cpu
;
190 void qemu_cpu_kick(CPUState
*cpu
)
195 void task_settid(TaskState
*ts
)
197 if (ts
->ts_tid
== 0) {
198 ts
->ts_tid
= (pid_t
)syscall(SYS_gettid
);
202 void stop_all_tasks(void)
205 * We trust that when using NPTL, start_exclusive()
206 * handles thread stopping correctly.
211 /* Assumes contents are already zeroed. */
212 void init_task_state(TaskState
*ts
)
218 ts
->sigaltstack_used
= (struct target_sigaltstack
) {
221 .ss_flags
= TARGET_SS_DISABLE
,
224 /* Capture task start time relative to system boot */
226 ticks_per_sec
= sysconf(_SC_CLK_TCK
);
228 if ((ticks_per_sec
> 0) && !clock_gettime(CLOCK_BOOTTIME
, &bt
)) {
229 /* start_boottime is expressed in clock ticks */
230 ts
->start_boottime
= bt
.tv_sec
* (uint64_t) ticks_per_sec
;
231 ts
->start_boottime
+= bt
.tv_nsec
* (uint64_t) ticks_per_sec
/
232 NANOSECONDS_PER_SECOND
;
236 CPUArchState
*cpu_copy(CPUArchState
*env
)
238 CPUState
*cpu
= env_cpu(env
);
239 CPUState
*new_cpu
= cpu_create(cpu_type
);
240 CPUArchState
*new_env
= cpu_env(new_cpu
);
243 /* Reset non arch specific state */
246 new_cpu
->tcg_cflags
= cpu
->tcg_cflags
;
247 memcpy(new_env
, env
, sizeof(CPUArchState
));
248 #if defined(TARGET_I386) || defined(TARGET_X86_64)
249 new_env
->gdt
.base
= target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES
,
250 PROT_READ
| PROT_WRITE
,
251 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
252 memcpy(g2h_untagged(new_env
->gdt
.base
), g2h_untagged(env
->gdt
.base
),
253 sizeof(uint64_t) * TARGET_GDT_ENTRIES
);
254 OBJECT(new_cpu
)->free
= OBJECT(cpu
)->free
;
257 /* Clone all break/watchpoints.
258 Note: Once we support ptrace with hw-debug register access, make sure
259 BP_CPU break/watchpoints are handled correctly on clone. */
260 QTAILQ_INIT(&new_cpu
->breakpoints
);
261 QTAILQ_FOREACH(bp
, &cpu
->breakpoints
, entry
) {
262 cpu_breakpoint_insert(new_cpu
, bp
->pc
, bp
->flags
, NULL
);
268 static void handle_arg_help(const char *arg
)
273 static void handle_arg_log(const char *arg
)
275 last_log_mask
= qemu_str_to_log_mask(arg
);
276 if (!last_log_mask
) {
277 qemu_print_log_usage(stdout
);
282 static void handle_arg_dfilter(const char *arg
)
284 qemu_set_dfilter_ranges(arg
, &error_fatal
);
287 static void handle_arg_log_filename(const char *arg
)
289 last_log_filename
= arg
;
292 static void handle_arg_set_env(const char *arg
)
296 while ((token
= strsep(&p
, ",")) != NULL
) {
297 if (envlist_setenv(envlist
, token
) != 0) {
304 static void handle_arg_unset_env(const char *arg
)
308 while ((token
= strsep(&p
, ",")) != NULL
) {
309 if (envlist_unsetenv(envlist
, token
) != 0) {
316 static void handle_arg_argv0(const char *arg
)
321 static void handle_arg_stack_size(const char *arg
)
324 guest_stack_size
= strtoul(arg
, &p
, 0);
325 if (guest_stack_size
== 0) {
330 guest_stack_size
*= MiB
;
331 } else if (*p
== 'k' || *p
== 'K') {
332 guest_stack_size
*= KiB
;
336 static void handle_arg_ld_prefix(const char *arg
)
338 interp_prefix
= strdup(arg
);
341 static void handle_arg_pagesize(const char *arg
)
343 unsigned size
, want
= qemu_real_host_page_size();
345 if (qemu_strtoui(arg
, NULL
, 10, &size
) || size
!= want
) {
346 warn_report("Deprecated page size option cannot "
347 "change host page size (%u)", want
);
351 static void handle_arg_seed(const char *arg
)
356 static void handle_arg_gdb(const char *arg
)
358 gdbstub
= g_strdup(arg
);
361 static void handle_arg_uname(const char *arg
)
363 qemu_uname_release
= strdup(arg
);
366 static void handle_arg_cpu(const char *arg
)
368 cpu_model
= strdup(arg
);
369 if (cpu_model
== NULL
|| is_help_option(cpu_model
)) {
375 static void handle_arg_guest_base(const char *arg
)
377 guest_base
= strtol(arg
, NULL
, 0);
378 have_guest_base
= true;
381 static void handle_arg_reserved_va(const char *arg
)
387 val
= strtoul(arg
, &p
, 0);
401 unsigned long unshifted
= val
;
404 if (val
>> shift
!= unshifted
) {
405 fprintf(stderr
, "Reserved virtual address too big\n");
410 fprintf(stderr
, "Unrecognised -R size suffix '%s'\n", p
);
413 /* The representation is size - 1, with 0 remaining "default". */
414 reserved_va
= val
? val
- 1 : 0;
417 static const char *rtsig_map
= CONFIG_QEMU_RTSIG_MAP
;
419 static void handle_arg_rtsig_map(const char *arg
)
424 static void handle_arg_one_insn_per_tb(const char *arg
)
426 opt_one_insn_per_tb
= true;
429 static void handle_arg_tb_size(const char *arg
)
431 if (qemu_strtoul(arg
, NULL
, 0, &opt_tb_size
)) {
436 static void handle_arg_strace(const char *arg
)
438 enable_strace
= true;
441 static void handle_arg_version(const char *arg
)
443 printf("qemu-" TARGET_NAME
" version " QEMU_FULL_VERSION
444 "\n" QEMU_COPYRIGHT
"\n");
448 static void handle_arg_trace(const char *arg
)
450 trace_opt_parse(arg
);
453 #if defined(TARGET_XTENSA)
454 static void handle_arg_abi_call0(const char *arg
)
456 xtensa_set_abi_call0();
460 static void handle_arg_perfmap(const char *arg
)
462 perf_enable_perfmap();
465 static void handle_arg_jitdump(const char *arg
)
467 perf_enable_jitdump();
470 static QemuPluginList plugins
= QTAILQ_HEAD_INITIALIZER(plugins
);
473 static void handle_arg_plugin(const char *arg
)
475 qemu_plugin_opt_parse(arg
, &plugins
);
479 struct qemu_argument
{
483 void (*handle_opt
)(const char *arg
);
488 static const struct qemu_argument arg_table
[] = {
489 {"h", "", false, handle_arg_help
,
490 "", "print this help"},
491 {"help", "", false, handle_arg_help
,
493 {"g", "QEMU_GDB", true, handle_arg_gdb
,
494 "port", "wait gdb connection to 'port'"},
495 {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix
,
496 "path", "set the elf interpreter prefix to 'path'"},
497 {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size
,
498 "size", "set the stack size to 'size' bytes"},
499 {"cpu", "QEMU_CPU", true, handle_arg_cpu
,
500 "model", "select CPU (-cpu help for list)"},
501 {"E", "QEMU_SET_ENV", true, handle_arg_set_env
,
502 "var=value", "sets targets environment variable (see below)"},
503 {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env
,
504 "var", "unsets targets environment variable (see below)"},
505 {"0", "QEMU_ARGV0", true, handle_arg_argv0
,
506 "argv0", "forces target process argv[0] to be 'argv0'"},
507 {"r", "QEMU_UNAME", true, handle_arg_uname
,
508 "uname", "set qemu uname release string to 'uname'"},
509 {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base
,
510 "address", "set guest_base address to 'address'"},
511 {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va
,
512 "size", "reserve 'size' bytes for guest virtual address space"},
513 {"t", "QEMU_RTSIG_MAP", true, handle_arg_rtsig_map
,
515 "map target rt signals [tsig,tsig+n) to [hsig,hsig+n]"},
516 {"d", "QEMU_LOG", true, handle_arg_log
,
517 "item[,...]", "enable logging of specified items "
518 "(use '-d help' for a list of items)"},
519 {"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter
,
520 "range[,...]","filter logging based on address range"},
521 {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename
,
522 "logfile", "write logs to 'logfile' (default stderr)"},
523 {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize
,
524 "pagesize", "deprecated change to host page size"},
526 "QEMU_ONE_INSN_PER_TB", false, handle_arg_one_insn_per_tb
,
527 "", "run with one guest instruction per emulated TB"},
528 {"tb-size", "QEMU_TB_SIZE", true, handle_arg_tb_size
,
529 "size", "TCG translation block cache size"},
530 {"strace", "QEMU_STRACE", false, handle_arg_strace
,
531 "", "log system calls"},
532 {"seed", "QEMU_RAND_SEED", true, handle_arg_seed
,
533 "", "Seed for pseudo-random number generator"},
534 {"trace", "QEMU_TRACE", true, handle_arg_trace
,
535 "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
537 {"plugin", "QEMU_PLUGIN", true, handle_arg_plugin
,
538 "", "[file=]<file>[,<argname>=<argvalue>]"},
540 {"version", "QEMU_VERSION", false, handle_arg_version
,
541 "", "display version information and exit"},
542 #if defined(TARGET_XTENSA)
543 {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0
,
544 "", "assume CALL0 Xtensa ABI"},
546 {"perfmap", "QEMU_PERFMAP", false, handle_arg_perfmap
,
547 "", "Generate a /tmp/perf-${pid}.map file for perf"},
548 {"jitdump", "QEMU_JITDUMP", false, handle_arg_jitdump
,
549 "", "Generate a jit-${pid}.dump file for perf"},
550 {NULL
, NULL
, false, NULL
, NULL
, NULL
}
553 static void usage(int exitcode
)
555 const struct qemu_argument
*arginfo
;
559 printf("usage: qemu-" TARGET_NAME
" [options] program [arguments...]\n"
560 "Linux CPU emulator (compiled for " TARGET_NAME
" emulation)\n"
562 "Options and associated environment variables:\n"
565 /* Calculate column widths. We must always have at least enough space
566 * for the column header.
568 maxarglen
= strlen("Argument");
569 maxenvlen
= strlen("Env-variable");
571 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
572 int arglen
= strlen(arginfo
->argv
);
573 if (arginfo
->has_arg
) {
574 arglen
+= strlen(arginfo
->example
) + 1;
576 if (strlen(arginfo
->env
) > maxenvlen
) {
577 maxenvlen
= strlen(arginfo
->env
);
579 if (arglen
> maxarglen
) {
584 printf("%-*s %-*s Description\n", maxarglen
+1, "Argument",
585 maxenvlen
, "Env-variable");
587 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
588 if (arginfo
->has_arg
) {
589 printf("-%s %-*s %-*s %s\n", arginfo
->argv
,
590 (int)(maxarglen
- strlen(arginfo
->argv
) - 1),
591 arginfo
->example
, maxenvlen
, arginfo
->env
, arginfo
->help
);
593 printf("-%-*s %-*s %s\n", maxarglen
, arginfo
->argv
,
594 maxenvlen
, arginfo
->env
,
601 "QEMU_LD_PREFIX = %s\n"
602 "QEMU_STACK_SIZE = %ld byte\n",
607 "You can use -E and -U options or the QEMU_SET_ENV and\n"
608 "QEMU_UNSET_ENV environment variables to set and unset\n"
609 "environment variables for the target process.\n"
610 "It is possible to provide several variables by separating them\n"
611 "by commas in getsubopt(3) style. Additionally it is possible to\n"
612 "provide the -E and -U options multiple times.\n"
613 "The following lines are equivalent:\n"
614 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
615 " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
616 " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
617 "Note that if you provide several changes to a single variable\n"
618 "the last change will stay in effect.\n"
620 QEMU_HELP_BOTTOM
"\n");
625 static int parse_args(int argc
, char **argv
)
629 const struct qemu_argument
*arginfo
;
631 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
632 if (arginfo
->env
== NULL
) {
636 r
= getenv(arginfo
->env
);
638 arginfo
->handle_opt(r
);
644 if (optind
>= argc
) {
653 if (!strcmp(r
, "-")) {
656 /* Treat --foo the same as -foo. */
661 for (arginfo
= arg_table
; arginfo
->handle_opt
!= NULL
; arginfo
++) {
662 if (!strcmp(r
, arginfo
->argv
)) {
663 if (arginfo
->has_arg
) {
664 if (optind
>= argc
) {
665 (void) fprintf(stderr
,
666 "qemu: missing argument for option '%s'\n", r
);
669 arginfo
->handle_opt(argv
[optind
]);
672 arginfo
->handle_opt(NULL
);
678 /* no option matched the current argv */
679 if (arginfo
->handle_opt
== NULL
) {
680 (void) fprintf(stderr
, "qemu: unknown option '%s'\n", r
);
685 if (optind
>= argc
) {
686 (void) fprintf(stderr
, "qemu: no user program specified\n");
690 exec_path
= argv
[optind
];
695 int main(int argc
, char **argv
, char **envp
)
697 struct target_pt_regs regs1
, *regs
= ®s1
;
698 struct image_info info1
, *info
= &info1
;
699 struct linux_binprm bprm
;
704 char **target_environ
, **wrk
;
711 unsigned long max_reserved_va
;
715 module_call_init(MODULE_INIT_TRACE
);
716 qemu_init_cpu_list();
717 module_call_init(MODULE_INIT_QOM
);
719 envlist
= envlist_create();
722 * add current environment into the list
723 * envlist_setenv adds to the front of the list; to preserve environ
724 * order add from back to front
726 for (wrk
= environ
; *wrk
!= NULL
; wrk
++) {
729 while (wrk
!= environ
) {
731 (void) envlist_setenv(envlist
, *wrk
);
734 /* Read the stack limit from the kernel. If it's "unlimited",
735 then we can do little else besides use the default. */
738 if (getrlimit(RLIMIT_STACK
, &lim
) == 0
739 && lim
.rlim_cur
!= RLIM_INFINITY
740 && lim
.rlim_cur
== (target_long
)lim
.rlim_cur
741 && lim
.rlim_cur
> guest_stack_size
) {
742 guest_stack_size
= lim
.rlim_cur
;
748 qemu_add_opts(&qemu_trace_opts
);
749 qemu_plugin_add_opts();
751 optind
= parse_args(argc
, argv
);
753 qemu_set_log_filename_flags(last_log_filename
,
754 last_log_mask
| (enable_strace
* LOG_STRACE
),
757 if (!trace_init_backends()) {
761 qemu_plugin_load_list(&plugins
, &error_fatal
);
764 memset(regs
, 0, sizeof(struct target_pt_regs
));
766 /* Zero out image_info */
767 memset(info
, 0, sizeof(struct image_info
));
769 memset(&bprm
, 0, sizeof (bprm
));
771 /* Scan interp_prefix dir for replacement files. */
772 init_paths(interp_prefix
);
774 init_qemu_uname_release();
777 * Manage binfmt-misc open-binary flag
780 execfd
= qemu_getauxval(AT_EXECFD
);
782 execfd
= open(exec_path
, O_RDONLY
);
784 printf("Error while loading %s: %s\n", exec_path
, strerror(errno
));
789 /* Resolve executable file name to full path name */
790 if (realpath(exec_path
, real_exec_path
)) {
791 exec_path
= real_exec_path
;
795 * get binfmt_misc flags
797 preserve_argv0
= !!(qemu_getauxval(AT_FLAGS
) & AT_FLAGS_PRESERVE_ARGV0
);
800 * Manage binfmt-misc preserve-arg[0] flag
801 * argv[optind] full path to the binary
802 * argv[optind + 1] original argv[0]
804 if (optind
+ 1 < argc
&& preserve_argv0
) {
808 if (cpu_model
== NULL
) {
809 cpu_model
= cpu_get_model(get_elf_eflags(execfd
));
811 cpu_type
= parse_cpu_option(cpu_model
);
813 /* init tcg before creating CPUs */
815 AccelState
*accel
= current_accel();
816 AccelClass
*ac
= ACCEL_GET_CLASS(accel
);
818 accel_init_interfaces(ac
);
819 object_property_set_bool(OBJECT(accel
), "one-insn-per-tb",
820 opt_one_insn_per_tb
, &error_abort
);
821 object_property_set_int(OBJECT(accel
), "tb-size",
822 opt_tb_size
, &error_abort
);
823 ac
->init_machine(accel
, NULL
);
827 * Finalize page size before creating CPUs.
828 * This will do nothing if !TARGET_PAGE_BITS_VARY.
829 * The most efficient setting is to match the host.
831 host_page_size
= qemu_real_host_page_size();
832 set_preferred_target_page_bits(ctz32(host_page_size
));
833 finalize_target_page_bits();
835 cpu
= cpu_create(cpu_type
);
841 * Reserving too much vm space via mmap can run into problems with rlimits,
842 * oom due to page table creation, etc. We will still try it, if directed
843 * by the command-line option, but not by default. Unless we're running a
844 * target address space of 32 or fewer bits on a host with 64 bits.
846 max_reserved_va
= MAX_RESERVED_VA(cpu
);
847 if (reserved_va
!= 0) {
848 if ((reserved_va
+ 1) % host_page_size
) {
849 char *s
= size_to_str(host_page_size
);
850 fprintf(stderr
, "Reserved virtual address not aligned mod %s\n", s
);
854 if (max_reserved_va
&& reserved_va
> max_reserved_va
) {
855 fprintf(stderr
, "Reserved virtual address too big\n");
858 } else if (HOST_LONG_BITS
== 64 && TARGET_VIRT_ADDR_SPACE_BITS
<= 32) {
859 /* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */
860 reserved_va
= max_reserved_va
;
862 if (reserved_va
!= 0) {
863 guest_addr_max
= reserved_va
;
864 } else if (MIN(TARGET_VIRT_ADDR_SPACE_BITS
, TARGET_ABI_BITS
) <= 32) {
865 guest_addr_max
= UINT32_MAX
;
867 guest_addr_max
= ~0ul;
871 * Temporarily disable
872 * "comparison is always false due to limited range of data type"
873 * due to comparison between (possible) uint64_t and uintptr_t.
875 #pragma GCC diagnostic push
876 #pragma GCC diagnostic ignored "-Wtype-limits"
877 #pragma GCC diagnostic ignored "-Wtautological-compare"
880 * Select an initial value for task_unmapped_base that is in range.
883 if (TASK_UNMAPPED_BASE
< reserved_va
) {
884 task_unmapped_base
= TASK_UNMAPPED_BASE
;
886 /* The most common default formula is TASK_SIZE / 3. */
887 task_unmapped_base
= TARGET_PAGE_ALIGN(reserved_va
/ 3);
889 } else if (TASK_UNMAPPED_BASE
< UINTPTR_MAX
) {
890 task_unmapped_base
= TASK_UNMAPPED_BASE
;
892 /* 32-bit host: pick something medium size. */
893 task_unmapped_base
= 0x10000000;
895 mmap_next_start
= task_unmapped_base
;
897 /* Similarly for elf_et_dyn_base. */
899 if (ELF_ET_DYN_BASE
< reserved_va
) {
900 elf_et_dyn_base
= ELF_ET_DYN_BASE
;
902 /* The most common default formula is TASK_SIZE / 3 * 2. */
903 elf_et_dyn_base
= TARGET_PAGE_ALIGN(reserved_va
/ 3) * 2;
905 } else if (ELF_ET_DYN_BASE
< UINTPTR_MAX
) {
906 elf_et_dyn_base
= ELF_ET_DYN_BASE
;
908 /* 32-bit host: pick something medium size. */
909 elf_et_dyn_base
= 0x18000000;
912 #pragma GCC diagnostic pop
916 if (seed_optarg
!= NULL
) {
917 qemu_guest_random_seed_main(seed_optarg
, &err
);
922 error_reportf_err(err
, "cannot initialize crypto: ");
927 target_environ
= envlist_to_environ(envlist
, NULL
);
928 envlist_free(envlist
);
931 * Read in mmap_min_addr kernel parameter. This value is used
932 * When loading the ELF image to determine whether guest_base
933 * is needed. It is also used in mmap_find_vma.
938 if ((fp
= fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL
) {
940 if (fscanf(fp
, "%lu", &tmp
) == 1 && tmp
!= 0) {
941 mmap_min_addr
= MAX(tmp
, host_page_size
);
942 qemu_log_mask(CPU_LOG_PAGE
, "host mmap_min_addr=0x%lx\n",
950 * We prefer to not make NULL pointers accessible to QEMU.
951 * If we're in a chroot with no /proc, fall back to 1 page.
953 if (mmap_min_addr
== 0) {
954 mmap_min_addr
= host_page_size
;
955 qemu_log_mask(CPU_LOG_PAGE
,
956 "host mmap_min_addr=0x%lx (fallback)\n",
961 * Prepare copy of argv vector for target.
963 target_argc
= argc
- optind
;
964 target_argv
= g_new0(char *, target_argc
+ 1);
967 * If argv0 is specified (using '-0' switch) we replace
968 * argv[0] pointer with the given one.
972 target_argv
[i
++] = strdup(argv0
);
974 for (; i
< target_argc
; i
++) {
975 target_argv
[i
] = strdup(argv
[optind
+ i
]);
977 target_argv
[target_argc
] = NULL
;
979 ts
= g_new0(TaskState
, 1);
981 /* build Task State */
989 ret
= loader_exec(execfd
, exec_path
, target_argv
, target_environ
, regs
,
992 printf("Error while loading %s: %s\n", exec_path
, strerror(-ret
));
996 for (wrk
= target_environ
; *wrk
; wrk
++) {
1000 g_free(target_environ
);
1002 if (qemu_loglevel_mask(CPU_LOG_PAGE
)) {
1003 FILE *f
= qemu_log_trylock();
1005 fprintf(f
, "guest_base %p\n", (void *)guest_base
);
1006 fprintf(f
, "page layout changed following binary load\n");
1009 fprintf(f
, "end_code 0x" TARGET_ABI_FMT_lx
"\n",
1011 fprintf(f
, "start_code 0x" TARGET_ABI_FMT_lx
"\n",
1013 fprintf(f
, "start_data 0x" TARGET_ABI_FMT_lx
"\n",
1015 fprintf(f
, "end_data 0x" TARGET_ABI_FMT_lx
"\n",
1017 fprintf(f
, "start_stack 0x" TARGET_ABI_FMT_lx
"\n",
1019 fprintf(f
, "brk 0x" TARGET_ABI_FMT_lx
"\n",
1021 fprintf(f
, "entry 0x" TARGET_ABI_FMT_lx
"\n",
1023 fprintf(f
, "argv_start 0x" TARGET_ABI_FMT_lx
"\n",
1025 fprintf(f
, "env_start 0x" TARGET_ABI_FMT_lx
"\n",
1027 fprintf(f
, "auxv_start 0x" TARGET_ABI_FMT_lx
"\n",
1033 target_set_brk(info
->brk
);
1035 signal_init(rtsig_map
);
1037 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
1038 generating the prologue until now so that the prologue can take
1039 the real value of GUEST_BASE into account. */
1040 tcg_prologue_init();
1042 target_cpu_copy_regs(env
, regs
);
1045 gdbserver_start(gdbstub
, &error_fatal
);
1048 #ifdef CONFIG_SEMIHOSTING
1049 qemu_semihosting_guestfd_init();