xref: /openbmc/qemu/bsd-user/main.c (revision 905c78fe)
184778508Sblueswir1 /*
24c0a4fe6SWarner Losh  *  qemu bsd user main
384778508Sblueswir1  *
484778508Sblueswir1  *  Copyright (c) 2003-2008 Fabrice Bellard
54c0a4fe6SWarner Losh  *  Copyright (c) 2013-14 Stacey Son
684778508Sblueswir1  *
784778508Sblueswir1  *  This program is free software; you can redistribute it and/or modify
884778508Sblueswir1  *  it under the terms of the GNU General Public License as published by
984778508Sblueswir1  *  the Free Software Foundation; either version 2 of the License, or
1084778508Sblueswir1  *  (at your option) any later version.
1184778508Sblueswir1  *
1284778508Sblueswir1  *  This program is distributed in the hope that it will be useful,
1384778508Sblueswir1  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1484778508Sblueswir1  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1584778508Sblueswir1  *  GNU General Public License for more details.
1684778508Sblueswir1  *
1784778508Sblueswir1  *  You should have received a copy of the GNU General Public License
188167ee88SBlue Swirl  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
1984778508Sblueswir1  */
2014a48c1dSMarkus Armbruster 
21312a0b1cSWarner Losh #include <sys/types.h>
22312a0b1cSWarner Losh #include <sys/time.h>
23312a0b1cSWarner Losh #include <sys/resource.h>
24312a0b1cSWarner Losh #include <sys/sysctl.h>
25312a0b1cSWarner Losh 
262231197cSPeter Maydell #include "qemu/osdep.h"
27a8d25326SMarkus Armbruster #include "qemu-common.h"
2866d26ddbSPhilippe Mathieu-Daudé #include "qemu/units.h"
29940e43aaSClaudio Fontana #include "qemu/accel.h"
3014a48c1dSMarkus Armbruster #include "sysemu/tcg.h"
318c1c230aSEd Maste #include "qemu-version.h"
3284778508Sblueswir1 #include <machine/trap.h>
3384778508Sblueswir1 
34daa76aa4SMarkus Armbruster #include "qapi/error.h"
3584778508Sblueswir1 #include "qemu.h"
366913e79cSLluís Vilanova #include "qemu/config-file.h"
37f5852efaSChristophe Fergeau #include "qemu/error-report.h"
38f348b6d1SVeronia Bahaa #include "qemu/path.h"
39f348b6d1SVeronia Bahaa #include "qemu/help_option.h"
400b8fa32fSMarkus Armbruster #include "qemu/module.h"
4163c91552SPaolo Bonzini #include "exec/exec-all.h"
42dcb32f1dSPhilippe Mathieu-Daudé #include "tcg/tcg.h"
431de7afc9SPaolo Bonzini #include "qemu/timer.h"
441de7afc9SPaolo Bonzini #include "qemu/envlist.h"
4529aabb4fSWarner Losh #include "qemu/cutils.h"
46508127e2SPaolo Bonzini #include "exec/log.h"
476913e79cSLluís Vilanova #include "trace/control.h"
4803ecf078SWarner Losh #include "crypto/init.h"
4903ecf078SWarner Losh #include "qemu/guest-random.h"
50fc0d96b4SBlue Swirl 
51d1dc9ab3SWarner Losh #include "host-os.h"
52031fe7afSWarner Losh #include "target_arch_cpu.h"
53d1dc9ab3SWarner Losh 
541b530a6dSaurel32 int singlestep;
555ca870b9SRichard Henderson uintptr_t guest_base;
56e307c192SRichard Henderson bool have_guest_base;
57be04f210SWarner Losh /*
58be04f210SWarner Losh  * When running 32-on-64 we should make sure we can fit all of the possible
59be04f210SWarner Losh  * guest address space into a contiguous chunk of virtual host memory.
60be04f210SWarner Losh  *
61be04f210SWarner Losh  * This way we will never overlap with our own libraries or binaries or stack
62be04f210SWarner Losh  * or anything else that QEMU maps.
63be04f210SWarner Losh  *
64be04f210SWarner Losh  * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
65be04f210SWarner Losh  * of the address for the kernel.  Some cpus rely on this and user space
66be04f210SWarner Losh  * uses the high bit(s) for pointer tagging and the like.  For them, we
67be04f210SWarner Losh  * must preserve the expected address space.
68be04f210SWarner Losh  */
69be04f210SWarner Losh #ifndef MAX_RESERVED_VA
70be04f210SWarner Losh # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
71be04f210SWarner Losh #  if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
72be04f210SWarner Losh       (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
73be04f210SWarner Losh /*
74be04f210SWarner Losh  * There are a number of places where we assign reserved_va to a variable
75be04f210SWarner Losh  * of type abi_ulong and expect it to fit.  Avoid the last page.
76be04f210SWarner Losh  */
77be04f210SWarner Losh #   define MAX_RESERVED_VA  (0xfffffffful & TARGET_PAGE_MASK)
78be04f210SWarner Losh #  else
79be04f210SWarner Losh #   define MAX_RESERVED_VA  (1ul << TARGET_VIRT_ADDR_SPACE_BITS)
80be04f210SWarner Losh #  endif
81be04f210SWarner Losh # else
82be04f210SWarner Losh #  define MAX_RESERVED_VA  0
83be04f210SWarner Losh # endif
84be04f210SWarner Losh #endif
85be04f210SWarner Losh 
86be04f210SWarner Losh /*
87be04f210SWarner Losh  * That said, reserving *too* much vm space via mmap can run into problems
88be04f210SWarner Losh  * with rlimits, oom due to page table creation, etc.  We will still try it,
89be04f210SWarner Losh  * if directed by the command-line option, but not by default.
90be04f210SWarner Losh  */
91be04f210SWarner Losh #if HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32
92be04f210SWarner Losh unsigned long reserved_va = MAX_RESERVED_VA;
93be04f210SWarner Losh #else
94d6ef40bfSPeter Maydell unsigned long reserved_va;
95be04f210SWarner Losh #endif
961b530a6dSaurel32 
977ee2822cSPaolo Bonzini static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
98fccae322SPeter Maydell const char *qemu_uname_release;
9901a298a5SWarner Losh char qemu_proc_pathname[PATH_MAX];  /* full path to exeutable */
10084778508Sblueswir1 
101312a0b1cSWarner Losh unsigned long target_maxtsiz = TARGET_MAXTSIZ;   /* max text size */
102312a0b1cSWarner Losh unsigned long target_dfldsiz = TARGET_DFLDSIZ;   /* initial data size limit */
103312a0b1cSWarner Losh unsigned long target_maxdsiz = TARGET_MAXDSIZ;   /* max data size */
104312a0b1cSWarner Losh unsigned long target_dflssiz = TARGET_DFLSSIZ;   /* initial data size limit */
105312a0b1cSWarner Losh unsigned long target_maxssiz = TARGET_MAXSSIZ;   /* max stack size */
106312a0b1cSWarner Losh unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */
10784778508Sblueswir1 
10863cca106SWarner Losh /* Helper routines for implementing atomic operations. */
1099399f095Sblueswir1 
1109399f095Sblueswir1 void fork_start(void)
1119399f095Sblueswir1 {
11263cca106SWarner Losh     start_exclusive();
11363cca106SWarner Losh     cpu_list_lock();
11463cca106SWarner Losh     mmap_fork_start();
1159399f095Sblueswir1 }
1169399f095Sblueswir1 
1179399f095Sblueswir1 void fork_end(int child)
1189399f095Sblueswir1 {
1199399f095Sblueswir1     if (child) {
12063cca106SWarner Losh         CPUState *cpu, *next_cpu;
12163cca106SWarner Losh         /*
12263cca106SWarner Losh          * Child processes created by fork() only have a single thread.  Discard
12363cca106SWarner Losh          * information about the parent threads.
12463cca106SWarner Losh          */
12563cca106SWarner Losh         CPU_FOREACH_SAFE(cpu, next_cpu) {
12663cca106SWarner Losh             if (cpu != thread_cpu) {
12763cca106SWarner Losh                 QTAILQ_REMOVE_RCU(&cpus, cpu, node);
12863cca106SWarner Losh             }
12963cca106SWarner Losh         }
13063cca106SWarner Losh         mmap_fork_end(child);
13163cca106SWarner Losh         /*
13263cca106SWarner Losh          * qemu_init_cpu_list() takes care of reinitializing the exclusive
13363cca106SWarner Losh          * state, so we don't need to end_exclusive() here.
13463cca106SWarner Losh          */
13563cca106SWarner Losh         qemu_init_cpu_list();
136f7ec7f7bSPeter Crosthwaite         gdbserver_fork(thread_cpu);
13763cca106SWarner Losh     } else {
13863cca106SWarner Losh         mmap_fork_end(child);
13963cca106SWarner Losh         cpu_list_unlock();
14063cca106SWarner Losh         end_exclusive();
1419399f095Sblueswir1     }
1429399f095Sblueswir1 }
1439399f095Sblueswir1 
144031fe7afSWarner Losh void cpu_loop(CPUArchState *env)
14531fc12dfSblueswir1 {
146031fe7afSWarner Losh     target_cpu_loop(env);
14731fc12dfSblueswir1 }
14831fc12dfSblueswir1 
14984778508Sblueswir1 static void usage(void)
15084778508Sblueswir1 {
1517e563bfbSThomas Huth     printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
1520781dd6eSThomas Huth            "\n" QEMU_COPYRIGHT "\n"
1532e59915dSPaolo Bonzini            "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
15484778508Sblueswir1            "BSD CPU emulator (compiled for %s emulation)\n"
15584778508Sblueswir1            "\n"
15684778508Sblueswir1            "Standard options:\n"
15784778508Sblueswir1            "-h                print this help\n"
15884778508Sblueswir1            "-g port           wait gdb connection to port\n"
15984778508Sblueswir1            "-L path           set the elf interpreter prefix (default=%s)\n"
16084778508Sblueswir1            "-s size           set the stack size in bytes (default=%ld)\n"
161c8057f95SPeter Maydell            "-cpu model        select CPU (-cpu help for list)\n"
16284778508Sblueswir1            "-drop-ld-preload  drop LD_PRELOAD for target process\n"
163fc0d96b4SBlue Swirl            "-E var=value      sets/modifies targets environment variable(s)\n"
164fc0d96b4SBlue Swirl            "-U var            unsets targets environment variable(s)\n"
1652fa5d9baSBlue Swirl            "-B address        set guest_base address to address\n"
16684778508Sblueswir1            "\n"
16784778508Sblueswir1            "Debug options:\n"
168989b697dSPeter Maydell            "-d item1[,...]    enable logging of specified items\n"
169989b697dSPeter Maydell            "                  (use '-d help' for a list of log items)\n"
170989b697dSPeter Maydell            "-D logfile        write logs to 'logfile' (default stderr)\n"
1711b530a6dSaurel32            "-singlestep       always run in singlestep mode\n"
17284778508Sblueswir1            "-strace           log system calls\n"
1736913e79cSLluís Vilanova            "-trace            [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
1746913e79cSLluís Vilanova            "                  specify tracing options\n"
17584778508Sblueswir1            "\n"
17684778508Sblueswir1            "Environment variables:\n"
17784778508Sblueswir1            "QEMU_STRACE       Print system calls and arguments similar to the\n"
17884778508Sblueswir1            "                  'strace' program.  Enable by setting to any value.\n"
179fc0d96b4SBlue Swirl            "You can use -E and -U options to set/unset environment variables\n"
180fc0d96b4SBlue Swirl            "for target process.  It is possible to provide several variables\n"
181fc0d96b4SBlue Swirl            "by repeating the option.  For example:\n"
182fc0d96b4SBlue Swirl            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
183fc0d96b4SBlue Swirl            "Note that if you provide several changes to single variable\n"
184fc0d96b4SBlue Swirl            "last change will stay in effect.\n"
185f5048cb7SEric Blake            "\n"
186f5048cb7SEric Blake            QEMU_HELP_BOTTOM "\n"
18784778508Sblueswir1            ,
1882e59915dSPaolo Bonzini            TARGET_NAME,
18984778508Sblueswir1            interp_prefix,
190312a0b1cSWarner Losh            target_dflssiz);
1912d18e637Sblueswir1     exit(1);
19284778508Sblueswir1 }
19384778508Sblueswir1 
194d42df502SWarner Losh __thread CPUState *thread_cpu;
19584778508Sblueswir1 
196653ccec2SWarner Losh void stop_all_tasks(void)
197653ccec2SWarner Losh {
198653ccec2SWarner Losh     /*
199653ccec2SWarner Losh      * We trust when using NPTL (pthreads) start_exclusive() handles thread
200653ccec2SWarner Losh      * stopping correctly.
201653ccec2SWarner Losh      */
202653ccec2SWarner Losh     start_exclusive();
203653ccec2SWarner Losh }
204653ccec2SWarner Losh 
20548f59211SEd Maste bool qemu_cpu_is_self(CPUState *cpu)
20648f59211SEd Maste {
20748f59211SEd Maste     return thread_cpu == cpu;
20848f59211SEd Maste }
20948f59211SEd Maste 
21048f59211SEd Maste void qemu_cpu_kick(CPUState *cpu)
21148f59211SEd Maste {
21248f59211SEd Maste     cpu_exit(cpu);
21348f59211SEd Maste }
21448f59211SEd Maste 
21584778508Sblueswir1 /* Assumes contents are already zeroed.  */
216b46d4ad7SWarner Losh static void init_task_state(TaskState *ts)
21784778508Sblueswir1 {
21846f4f76dSWarner Losh     ts->sigaltstack_used = (struct target_sigaltstack) {
21946f4f76dSWarner Losh         .ss_sp = 0,
22046f4f76dSWarner Losh         .ss_size = 0,
22146f4f76dSWarner Losh         .ss_flags = TARGET_SS_DISABLE,
22246f4f76dSWarner Losh     };
22384778508Sblueswir1 }
22484778508Sblueswir1 
225f0f7f9dcSWarner Losh void gemu_log(const char *fmt, ...)
226f0f7f9dcSWarner Losh {
227f0f7f9dcSWarner Losh     va_list ap;
228f0f7f9dcSWarner Losh 
229f0f7f9dcSWarner Losh     va_start(ap, fmt);
230f0f7f9dcSWarner Losh     vfprintf(stderr, fmt, ap);
231f0f7f9dcSWarner Losh     va_end(ap);
232f0f7f9dcSWarner Losh }
233f0f7f9dcSWarner Losh 
234312a0b1cSWarner Losh static void
235312a0b1cSWarner Losh adjust_ssize(void)
236312a0b1cSWarner Losh {
237312a0b1cSWarner Losh     struct rlimit rl;
238312a0b1cSWarner Losh 
239312a0b1cSWarner Losh     if (getrlimit(RLIMIT_STACK, &rl) != 0) {
240312a0b1cSWarner Losh         return;
241312a0b1cSWarner Losh     }
242312a0b1cSWarner Losh 
243312a0b1cSWarner Losh     target_maxssiz = MIN(target_maxssiz, rl.rlim_max);
244312a0b1cSWarner Losh     target_dflssiz = MIN(MAX(target_dflssiz, rl.rlim_cur), target_maxssiz);
245312a0b1cSWarner Losh 
246312a0b1cSWarner Losh     rl.rlim_max = target_maxssiz;
247312a0b1cSWarner Losh     rl.rlim_cur = target_dflssiz;
248312a0b1cSWarner Losh     setrlimit(RLIMIT_STACK, &rl);
249312a0b1cSWarner Losh }
250312a0b1cSWarner Losh 
25101a298a5SWarner Losh static void save_proc_pathname(char *argv0)
25201a298a5SWarner Losh {
25301a298a5SWarner Losh     int mib[4];
25401a298a5SWarner Losh     size_t len;
25501a298a5SWarner Losh 
25601a298a5SWarner Losh     mib[0] = CTL_KERN;
25701a298a5SWarner Losh     mib[1] = KERN_PROC;
25801a298a5SWarner Losh     mib[2] = KERN_PROC_PATHNAME;
25901a298a5SWarner Losh     mib[3] = -1;
26001a298a5SWarner Losh 
26101a298a5SWarner Losh     len = sizeof(qemu_proc_pathname);
26201a298a5SWarner Losh     if (sysctl(mib, 4, qemu_proc_pathname, &len, NULL, 0)) {
26301a298a5SWarner Losh         perror("sysctl");
26401a298a5SWarner Losh     }
26501a298a5SWarner Losh }
26601a298a5SWarner Losh 
26784778508Sblueswir1 int main(int argc, char **argv)
26884778508Sblueswir1 {
26984778508Sblueswir1     const char *filename;
27084778508Sblueswir1     const char *cpu_model;
2712278b939SIgor Mammedov     const char *cpu_type;
272989b697dSPeter Maydell     const char *log_file = NULL;
273c235d738SMatthew Fernandez     const char *log_mask = NULL;
27403ecf078SWarner Losh     const char *seed_optarg = NULL;
27584778508Sblueswir1     struct target_pt_regs regs1, *regs = &regs1;
27684778508Sblueswir1     struct image_info info1, *info = &info1;
277d37853f9SWarner Losh     struct bsd_binprm bprm;
278031fe7afSWarner Losh     TaskState *ts;
2799349b4f9SAndreas Färber     CPUArchState *env;
280db6b81d4SAndreas Färber     CPUState *cpu;
28129aabb4fSWarner Losh     int optind, rv;
28284778508Sblueswir1     const char *r;
283fcedd920SAlex Bennée     const char *gdbstub = NULL;
284fc0d96b4SBlue Swirl     char **target_environ, **wrk;
285fc0d96b4SBlue Swirl     envlist_t *envlist = NULL;
286b8012648SColin Percival     char *argv0 = NULL;
28784778508Sblueswir1 
288312a0b1cSWarner Losh     adjust_ssize();
289312a0b1cSWarner Losh 
290b23a51dcSWarner Losh     if (argc <= 1) {
29184778508Sblueswir1         usage();
292b23a51dcSWarner Losh     }
29384778508Sblueswir1 
29401a298a5SWarner Losh     save_proc_pathname(argv[0]);
29501a298a5SWarner Losh 
296f5852efaSChristophe Fergeau     error_init(argv[0]);
297fe4db84dSDaniel P. Berrange     module_call_init(MODULE_INIT_TRACE);
298267f685bSPaolo Bonzini     qemu_init_cpu_list();
299ce008c1fSAndreas Färber     module_call_init(MODULE_INIT_QOM);
300ce008c1fSAndreas Färber 
301ec45bbe5SSaurav Sachidanand     envlist = envlist_create();
302fc0d96b4SBlue Swirl 
303fc0d96b4SBlue Swirl     /* add current environment into the list */
304fc0d96b4SBlue Swirl     for (wrk = environ; *wrk != NULL; wrk++) {
305fc0d96b4SBlue Swirl         (void) envlist_setenv(envlist, *wrk);
306fc0d96b4SBlue Swirl     }
307fc0d96b4SBlue Swirl 
30884778508Sblueswir1     cpu_model = NULL;
3090c62de2fSJuergen Lock 
3106913e79cSLluís Vilanova     qemu_add_opts(&qemu_trace_opts);
3116913e79cSLluís Vilanova 
31284778508Sblueswir1     optind = 1;
31384778508Sblueswir1     for (;;) {
314b23a51dcSWarner Losh         if (optind >= argc) {
31584778508Sblueswir1             break;
316b23a51dcSWarner Losh         }
31784778508Sblueswir1         r = argv[optind];
318b23a51dcSWarner Losh         if (r[0] != '-') {
31984778508Sblueswir1             break;
320b23a51dcSWarner Losh         }
32184778508Sblueswir1         optind++;
32284778508Sblueswir1         r++;
32384778508Sblueswir1         if (!strcmp(r, "-")) {
32484778508Sblueswir1             break;
32584778508Sblueswir1         } else if (!strcmp(r, "d")) {
326c235d738SMatthew Fernandez             if (optind >= argc) {
32784778508Sblueswir1                 break;
32884778508Sblueswir1             }
329c235d738SMatthew Fernandez             log_mask = argv[optind++];
330c235d738SMatthew Fernandez         } else if (!strcmp(r, "D")) {
331c235d738SMatthew Fernandez             if (optind >= argc) {
332c235d738SMatthew Fernandez                 break;
33384778508Sblueswir1             }
334c235d738SMatthew Fernandez             log_file = argv[optind++];
335fc0d96b4SBlue Swirl         } else if (!strcmp(r, "E")) {
336fc0d96b4SBlue Swirl             r = argv[optind++];
337b23a51dcSWarner Losh             if (envlist_setenv(envlist, r) != 0) {
338fc0d96b4SBlue Swirl                 usage();
339b23a51dcSWarner Losh             }
340f66724c9SStefan Weil         } else if (!strcmp(r, "ignore-environment")) {
341f66724c9SStefan Weil             envlist_free(envlist);
342ec45bbe5SSaurav Sachidanand             envlist = envlist_create();
343fc0d96b4SBlue Swirl         } else if (!strcmp(r, "U")) {
344fc0d96b4SBlue Swirl             r = argv[optind++];
345b23a51dcSWarner Losh             if (envlist_unsetenv(envlist, r) != 0) {
346fc0d96b4SBlue Swirl                 usage();
347b23a51dcSWarner Losh             }
34884778508Sblueswir1         } else if (!strcmp(r, "s")) {
34984778508Sblueswir1             r = argv[optind++];
350312a0b1cSWarner Losh             rv = qemu_strtoul(r, &r, 0, &target_dflssiz);
351312a0b1cSWarner Losh             if (rv < 0 || target_dflssiz <= 0) {
35284778508Sblueswir1                 usage();
353b23a51dcSWarner Losh             }
354b23a51dcSWarner Losh             if (*r == 'M') {
355312a0b1cSWarner Losh                 target_dflssiz *= 1024 * 1024;
356b23a51dcSWarner Losh             } else if (*r == 'k' || *r == 'K') {
357312a0b1cSWarner Losh                 target_dflssiz *= 1024;
358312a0b1cSWarner Losh             }
359312a0b1cSWarner Losh             if (target_dflssiz > target_maxssiz) {
360312a0b1cSWarner Losh                 usage();
361b23a51dcSWarner Losh             }
36284778508Sblueswir1         } else if (!strcmp(r, "L")) {
36384778508Sblueswir1             interp_prefix = argv[optind++];
36484778508Sblueswir1         } else if (!strcmp(r, "p")) {
36584778508Sblueswir1             qemu_host_page_size = atoi(argv[optind++]);
36684778508Sblueswir1             if (qemu_host_page_size == 0 ||
36784778508Sblueswir1                 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
36884778508Sblueswir1                 fprintf(stderr, "page size must be a power of two\n");
36984778508Sblueswir1                 exit(1);
37084778508Sblueswir1             }
37184778508Sblueswir1         } else if (!strcmp(r, "g")) {
372fcedd920SAlex Bennée             gdbstub = g_strdup(argv[optind++]);
37384778508Sblueswir1         } else if (!strcmp(r, "r")) {
37484778508Sblueswir1             qemu_uname_release = argv[optind++];
37584778508Sblueswir1         } else if (!strcmp(r, "cpu")) {
37684778508Sblueswir1             cpu_model = argv[optind++];
377c8057f95SPeter Maydell             if (is_help_option(cpu_model)) {
37884778508Sblueswir1                 /* XXX: implement xxx_cpu_list for targets that still miss it */
37984778508Sblueswir1 #if defined(cpu_list)
3800442428aSMarkus Armbruster                 cpu_list();
38184778508Sblueswir1 #endif
3822d18e637Sblueswir1                 exit(1);
38384778508Sblueswir1             }
3842fa5d9baSBlue Swirl         } else if (!strcmp(r, "B")) {
38529aabb4fSWarner Losh             rv = qemu_strtoul(argv[optind++], NULL, 0, &guest_base);
38629aabb4fSWarner Losh             if (rv < 0) {
38729aabb4fSWarner Losh                 usage();
38829aabb4fSWarner Losh             }
389e307c192SRichard Henderson             have_guest_base = true;
39084778508Sblueswir1         } else if (!strcmp(r, "drop-ld-preload")) {
391fc0d96b4SBlue Swirl             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
39203ecf078SWarner Losh         } else if (!strcmp(r, "seed")) {
39303ecf078SWarner Losh             seed_optarg = optarg;
3941b530a6dSaurel32         } else if (!strcmp(r, "singlestep")) {
3951b530a6dSaurel32             singlestep = 1;
39684778508Sblueswir1         } else if (!strcmp(r, "strace")) {
39784778508Sblueswir1             do_strace = 1;
3986913e79cSLluís Vilanova         } else if (!strcmp(r, "trace")) {
39992eecfffSPaolo Bonzini             trace_opt_parse(optarg);
400b8012648SColin Percival         } else if (!strcmp(r, "0")) {
401b8012648SColin Percival             argv0 = argv[optind++];
4026913e79cSLluís Vilanova         } else {
40384778508Sblueswir1             usage();
40484778508Sblueswir1         }
40584778508Sblueswir1     }
40684778508Sblueswir1 
407c235d738SMatthew Fernandez     /* init debug */
408*905c78feSRichard Henderson     {
409*905c78feSRichard Henderson         int mask = 0;
410c235d738SMatthew Fernandez         if (log_mask) {
4114fde1ebaSPeter Maydell             mask = qemu_str_to_log_mask(log_mask);
412c235d738SMatthew Fernandez             if (!mask) {
41359a6fa6eSPeter Maydell                 qemu_print_log_usage(stdout);
414c235d738SMatthew Fernandez                 exit(1);
415c235d738SMatthew Fernandez             }
416*905c78feSRichard Henderson         }
417*905c78feSRichard Henderson         qemu_set_log_filename_flags(log_file, mask, &error_fatal);
418c235d738SMatthew Fernandez     }
419c235d738SMatthew Fernandez 
4204b5dfd82SPeter Maydell     if (optind >= argc) {
4214b5dfd82SPeter Maydell         usage();
4224b5dfd82SPeter Maydell     }
4234b5dfd82SPeter Maydell     filename = argv[optind];
424b8012648SColin Percival     if (argv0) {
425b8012648SColin Percival         argv[optind] = argv0;
426b8012648SColin Percival     }
4274b5dfd82SPeter Maydell 
4286913e79cSLluís Vilanova     if (!trace_init_backends()) {
4296913e79cSLluís Vilanova         exit(1);
4306913e79cSLluís Vilanova     }
43192eecfffSPaolo Bonzini     trace_init_file();
4326913e79cSLluís Vilanova 
43384778508Sblueswir1     /* Zero out regs */
43484778508Sblueswir1     memset(regs, 0, sizeof(struct target_pt_regs));
43584778508Sblueswir1 
436d37853f9SWarner Losh     /* Zero bsd params */
437d37853f9SWarner Losh     memset(&bprm, 0, sizeof(bprm));
438d37853f9SWarner Losh 
43984778508Sblueswir1     /* Zero out image_info */
44084778508Sblueswir1     memset(info, 0, sizeof(struct image_info));
44184778508Sblueswir1 
44284778508Sblueswir1     /* Scan interp_prefix dir for replacement files. */
44384778508Sblueswir1     init_paths(interp_prefix);
44484778508Sblueswir1 
44584778508Sblueswir1     if (cpu_model == NULL) {
446031fe7afSWarner Losh         cpu_model = TARGET_DEFAULT_CPU_MODEL;
44784778508Sblueswir1     }
4482b5249b8SIgor Mammedov 
449b86f59c7SClaudio Fontana     cpu_type = parse_cpu_option(cpu_model);
450031fe7afSWarner Losh 
4512b5249b8SIgor Mammedov     /* init tcg before creating CPUs and to get qemu_host_page_size */
452940e43aaSClaudio Fontana     {
453940e43aaSClaudio Fontana         AccelClass *ac = ACCEL_GET_CLASS(current_accel());
4542b5249b8SIgor Mammedov 
455b86f59c7SClaudio Fontana         accel_init_interfaces(ac);
45692242f34SClaudio Fontana         ac->init_machine(NULL);
457940e43aaSClaudio Fontana     }
4582278b939SIgor Mammedov     cpu = cpu_create(cpu_type);
4592994fd96SEduardo Habkost     env = cpu->env_ptr;
460db6b81d4SAndreas Färber     cpu_reset(cpu);
461db6b81d4SAndreas Färber     thread_cpu = cpu;
46284778508Sblueswir1 
46384778508Sblueswir1     if (getenv("QEMU_STRACE")) {
46484778508Sblueswir1         do_strace = 1;
46584778508Sblueswir1     }
46684778508Sblueswir1 
467fc0d96b4SBlue Swirl     target_environ = envlist_to_environ(envlist, NULL);
468fc0d96b4SBlue Swirl     envlist_free(envlist);
46984778508Sblueswir1 
470be04f210SWarner Losh     if (reserved_va) {
471be04f210SWarner Losh             mmap_next_start = reserved_va;
472be04f210SWarner Losh     }
473be04f210SWarner Losh 
47403ecf078SWarner Losh     {
47503ecf078SWarner Losh         Error *err = NULL;
47603ecf078SWarner Losh         if (seed_optarg != NULL) {
47703ecf078SWarner Losh             qemu_guest_random_seed_main(seed_optarg, &err);
47803ecf078SWarner Losh         } else {
47903ecf078SWarner Losh             qcrypto_init(&err);
48003ecf078SWarner Losh         }
48103ecf078SWarner Losh         if (err) {
48203ecf078SWarner Losh             error_reportf_err(err, "cannot initialize crypto: ");
48303ecf078SWarner Losh             exit(1);
48403ecf078SWarner Losh         }
48503ecf078SWarner Losh     }
48603ecf078SWarner Losh 
4872fa5d9baSBlue Swirl     /*
488fa79cde6SRichard Henderson      * Now that page sizes are configured we can do
4892fa5d9baSBlue Swirl      * proper page alignment for guest_base.
4902fa5d9baSBlue Swirl      */
4912fa5d9baSBlue Swirl     guest_base = HOST_PAGE_ALIGN(guest_base);
4922fa5d9baSBlue Swirl 
493d37853f9SWarner Losh     if (loader_exec(filename, argv + optind, target_environ, regs, info,
494d37853f9SWarner Losh                     &bprm) != 0) {
49584778508Sblueswir1         printf("Error loading %s\n", filename);
49684778508Sblueswir1         _exit(1);
49784778508Sblueswir1     }
49884778508Sblueswir1 
49984778508Sblueswir1     for (wrk = target_environ; *wrk; wrk++) {
500ec45bbe5SSaurav Sachidanand         g_free(*wrk);
50184778508Sblueswir1     }
50284778508Sblueswir1 
503ec45bbe5SSaurav Sachidanand     g_free(target_environ);
50484778508Sblueswir1 
50513829020SPaolo Bonzini     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
50643b76161SRichard Henderson         FILE *f = qemu_log_trylock();
50743b76161SRichard Henderson         if (f) {
50843b76161SRichard Henderson             fprintf(f, "guest_base  %p\n", (void *)guest_base);
50943b76161SRichard Henderson             fprintf(f, "page layout changed following binary load\n");
51043b76161SRichard Henderson             page_dump(f);
51184778508Sblueswir1 
51243b76161SRichard Henderson             fprintf(f, "start_brk   0x" TARGET_ABI_FMT_lx "\n",
51343b76161SRichard Henderson                     info->start_brk);
51443b76161SRichard Henderson             fprintf(f, "end_code    0x" TARGET_ABI_FMT_lx "\n",
51543b76161SRichard Henderson                     info->end_code);
51643b76161SRichard Henderson             fprintf(f, "start_code  0x" TARGET_ABI_FMT_lx "\n",
51784778508Sblueswir1                     info->start_code);
51843b76161SRichard Henderson             fprintf(f, "start_data  0x" TARGET_ABI_FMT_lx "\n",
51984778508Sblueswir1                     info->start_data);
52043b76161SRichard Henderson             fprintf(f, "end_data    0x" TARGET_ABI_FMT_lx "\n",
52143b76161SRichard Henderson                     info->end_data);
52243b76161SRichard Henderson             fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n",
52384778508Sblueswir1                     info->start_stack);
52443b76161SRichard Henderson             fprintf(f, "brk         0x" TARGET_ABI_FMT_lx "\n", info->brk);
52543b76161SRichard Henderson             fprintf(f, "entry       0x" TARGET_ABI_FMT_lx "\n", info->entry);
52643b76161SRichard Henderson 
52743b76161SRichard Henderson             qemu_log_unlock(f);
52843b76161SRichard Henderson         }
5292e77eac6Sblueswir1     }
53084778508Sblueswir1 
531031fe7afSWarner Losh     /* build Task State */
532031fe7afSWarner Losh     ts = g_new0(TaskState, 1);
533031fe7afSWarner Losh     init_task_state(ts);
534031fe7afSWarner Losh     ts->info = info;
535031fe7afSWarner Losh     ts->bprm = &bprm;
536031fe7afSWarner Losh     cpu->opaque = ts;
537031fe7afSWarner Losh 
53884778508Sblueswir1     target_set_brk(info->brk);
53984778508Sblueswir1     syscall_init();
54084778508Sblueswir1     signal_init();
54184778508Sblueswir1 
54234bc8475SWarner Losh     /*
54334bc8475SWarner Losh      * Now that we've loaded the binary, GUEST_BASE is fixed.  Delay
54434bc8475SWarner Losh      * generating the prologue until now so that the prologue can take
54534bc8475SWarner Losh      * the real value of GUEST_BASE into account.
54634bc8475SWarner Losh      */
547b1311c4aSEmilio G. Cota     tcg_prologue_init(tcg_ctx);
5489002ec79SRichard Henderson 
549031fe7afSWarner Losh     target_cpu_init(env, regs);
55084778508Sblueswir1 
551fcedd920SAlex Bennée     if (gdbstub) {
552fcedd920SAlex Bennée         gdbserver_start(gdbstub);
553db6b81d4SAndreas Färber         gdb_handlesig(cpu, 0);
55484778508Sblueswir1     }
55578cfb07fSJuergen Lock     cpu_loop(env);
55684778508Sblueswir1     /* never exits */
55784778508Sblueswir1     return 0;
55884778508Sblueswir1 }
559