xref: /openbmc/qemu/bsd-user/main.c (revision f7bc08e935382e80a2b9e1110e246d6aaabadb51)
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 
2148e438a3SMarkus Armbruster #include "qemu/osdep.h"
22312a0b1cSWarner Losh #include <sys/resource.h>
23312a0b1cSWarner Losh #include <sys/sysctl.h>
24312a0b1cSWarner Losh 
2549f95221SMarc-André Lureau #include "qemu/help-texts.h"
2666d26ddbSPhilippe Mathieu-Daudé #include "qemu/units.h"
27940e43aaSClaudio Fontana #include "qemu/accel.h"
288c1c230aSEd Maste #include "qemu-version.h"
2984778508Sblueswir1 #include <machine/trap.h>
3084778508Sblueswir1 
31daa76aa4SMarkus Armbruster #include "qapi/error.h"
3284778508Sblueswir1 #include "qemu.h"
336913e79cSLluís Vilanova #include "qemu/config-file.h"
34f5852efaSChristophe Fergeau #include "qemu/error-report.h"
35f348b6d1SVeronia Bahaa #include "qemu/path.h"
36f348b6d1SVeronia Bahaa #include "qemu/help_option.h"
370b8fa32fSMarkus Armbruster #include "qemu/module.h"
385b6828d1SJessica Clarke #include "qemu/plugin.h"
3963c91552SPaolo Bonzini #include "exec/exec-all.h"
4016aa8eaaSPhilippe Mathieu-Daudé #include "user/guest-base.h"
41d7ec12f8SRichard Henderson #include "tcg/startup.h"
421de7afc9SPaolo Bonzini #include "qemu/timer.h"
431de7afc9SPaolo Bonzini #include "qemu/envlist.h"
4429aabb4fSWarner Losh #include "qemu/cutils.h"
45508127e2SPaolo Bonzini #include "exec/log.h"
466913e79cSLluís Vilanova #include "trace/control.h"
4703ecf078SWarner Losh #include "crypto/init.h"
4803ecf078SWarner Losh #include "qemu/guest-random.h"
49d96bf49bSAlex Bennée #include "gdbstub/user.h"
50ba379542SWarner Losh #include "exec/page-vary.h"
51fc0d96b4SBlue Swirl 
52d1dc9ab3SWarner Losh #include "host-os.h"
53031fe7afSWarner Losh #include "target_arch_cpu.h"
54d1dc9ab3SWarner Losh 
558c45039fSRichard Henderson 
568c45039fSRichard Henderson /*
578c45039fSRichard Henderson  * TODO: Remove these and rely only on qemu_real_host_page_size().
588c45039fSRichard Henderson  */
598c45039fSRichard Henderson uintptr_t qemu_host_page_size;
608c45039fSRichard Henderson intptr_t qemu_host_page_mask;
618c45039fSRichard Henderson 
623cfb0456SPeter Maydell static bool opt_one_insn_per_tb;
6389974523SIlya Leoshkevich static unsigned long opt_tb_size;
645ca870b9SRichard Henderson uintptr_t guest_base;
65e307c192SRichard Henderson bool have_guest_base;
66be04f210SWarner Losh /*
67be04f210SWarner Losh  * When running 32-on-64 we should make sure we can fit all of the possible
68be04f210SWarner Losh  * guest address space into a contiguous chunk of virtual host memory.
69be04f210SWarner Losh  *
70be04f210SWarner Losh  * This way we will never overlap with our own libraries or binaries or stack
71be04f210SWarner Losh  * or anything else that QEMU maps.
72be04f210SWarner Losh  *
73be04f210SWarner Losh  * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
74be04f210SWarner Losh  * of the address for the kernel.  Some cpus rely on this and user space
75be04f210SWarner Losh  * uses the high bit(s) for pointer tagging and the like.  For them, we
76be04f210SWarner Losh  * must preserve the expected address space.
77be04f210SWarner Losh  */
78be04f210SWarner Losh #ifndef MAX_RESERVED_VA
79be04f210SWarner Losh # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
80be04f210SWarner Losh #  if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
81be04f210SWarner Losh       (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
82cb4c2590SWarner Losh #   define MAX_RESERVED_VA(CPU)  0xfffffffful
83be04f210SWarner Losh #  else
84cb4c2590SWarner Losh #   define MAX_RESERVED_VA(CPU)  ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
85be04f210SWarner Losh #  endif
86be04f210SWarner Losh # else
87cb4c2590SWarner Losh #  define MAX_RESERVED_VA(CPU)  0
88be04f210SWarner Losh # endif
89be04f210SWarner Losh #endif
90be04f210SWarner Losh 
91d6ef40bfSPeter Maydell unsigned long reserved_va;
921b530a6dSaurel32 
9386327290SStacey Son const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
94fccae322SPeter Maydell const char *qemu_uname_release;
9584778508Sblueswir1 
96312a0b1cSWarner Losh unsigned long target_maxtsiz = TARGET_MAXTSIZ;   /* max text size */
97312a0b1cSWarner Losh unsigned long target_dfldsiz = TARGET_DFLDSIZ;   /* initial data size limit */
98312a0b1cSWarner Losh unsigned long target_maxdsiz = TARGET_MAXDSIZ;   /* max data size */
99312a0b1cSWarner Losh unsigned long target_dflssiz = TARGET_DFLSSIZ;   /* initial data size limit */
100312a0b1cSWarner Losh unsigned long target_maxssiz = TARGET_MAXSSIZ;   /* max stack size */
101312a0b1cSWarner Losh unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */
10284778508Sblueswir1 
10363cca106SWarner Losh /* Helper routines for implementing atomic operations. */
1049399f095Sblueswir1 
fork_start(void)1059399f095Sblueswir1 void fork_start(void)
1069399f095Sblueswir1 {
10763cca106SWarner Losh     start_exclusive();
10863cca106SWarner Losh     mmap_fork_start();
1095b6828d1SJessica Clarke     cpu_list_lock();
1105b6828d1SJessica Clarke     qemu_plugin_user_prefork_lock();
1113d6ed98dSIlya Leoshkevich     gdbserver_fork_start();
1129399f095Sblueswir1 }
1139399f095Sblueswir1 
fork_end(pid_t pid)1144edc98fcSIlya Leoshkevich void fork_end(pid_t pid)
1159399f095Sblueswir1 {
1164edc98fcSIlya Leoshkevich     bool child = pid == 0;
1174edc98fcSIlya Leoshkevich 
1185b6828d1SJessica Clarke     qemu_plugin_user_postfork(child);
1195b6828d1SJessica Clarke     mmap_fork_end(child);
1209399f095Sblueswir1     if (child) {
12163cca106SWarner Losh         CPUState *cpu, *next_cpu;
12263cca106SWarner Losh         /*
1235b6828d1SJessica Clarke          * Child processes created by fork() only have a single thread.
1245b6828d1SJessica Clarke          * Discard information about the parent threads.
12563cca106SWarner Losh          */
12663cca106SWarner Losh         CPU_FOREACH_SAFE(cpu, next_cpu) {
12763cca106SWarner Losh             if (cpu != thread_cpu) {
1283c55dd58SPhilippe Mathieu-Daudé                 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
12963cca106SWarner Losh             }
13063cca106SWarner Losh         }
13163cca106SWarner Losh         qemu_init_cpu_list();
132d4e1369aSIlya Leoshkevich         get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
13363cca106SWarner Losh     } else {
13463cca106SWarner Losh         cpu_list_unlock();
1359399f095Sblueswir1     }
1365b6828d1SJessica Clarke     gdbserver_fork_end(thread_cpu, pid);
1375b6828d1SJessica Clarke     /*
1385b6828d1SJessica Clarke      * qemu_init_cpu_list() reinitialized the child exclusive state, but we
1395b6828d1SJessica Clarke      * also need to keep current_cpu consistent, so call end_exclusive() for
1405b6828d1SJessica Clarke      * both child and parent.
1415b6828d1SJessica Clarke      */
1425b6828d1SJessica Clarke     end_exclusive();
1439399f095Sblueswir1 }
1449399f095Sblueswir1 
cpu_loop(CPUArchState * env)145031fe7afSWarner Losh void cpu_loop(CPUArchState *env)
14631fc12dfSblueswir1 {
147031fe7afSWarner Losh     target_cpu_loop(env);
14831fc12dfSblueswir1 }
14931fc12dfSblueswir1 
usage(void)15084778508Sblueswir1 static void usage(void)
15184778508Sblueswir1 {
1527e563bfbSThomas Huth     printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
1530781dd6eSThomas Huth            "\n" QEMU_COPYRIGHT "\n"
1542e59915dSPaolo Bonzini            "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
15584778508Sblueswir1            "BSD CPU emulator (compiled for %s emulation)\n"
15684778508Sblueswir1            "\n"
15784778508Sblueswir1            "Standard options:\n"
15884778508Sblueswir1            "-h                print this help\n"
15984778508Sblueswir1            "-g port           wait gdb connection to port\n"
16084778508Sblueswir1            "-L path           set the elf interpreter prefix (default=%s)\n"
16184778508Sblueswir1            "-s size           set the stack size in bytes (default=%ld)\n"
162c8057f95SPeter Maydell            "-cpu model        select CPU (-cpu help for list)\n"
16384778508Sblueswir1            "-drop-ld-preload  drop LD_PRELOAD for target process\n"
164fc0d96b4SBlue Swirl            "-E var=value      sets/modifies targets environment variable(s)\n"
165fc0d96b4SBlue Swirl            "-U var            unsets targets environment variable(s)\n"
1662fa5d9baSBlue Swirl            "-B address        set guest_base address to address\n"
16784778508Sblueswir1            "\n"
16884778508Sblueswir1            "Debug options:\n"
169989b697dSPeter Maydell            "-d item1[,...]    enable logging of specified items\n"
170989b697dSPeter Maydell            "                  (use '-d help' for a list of log items)\n"
171989b697dSPeter Maydell            "-D logfile        write logs to 'logfile' (default stderr)\n"
172060e0cd7SPeter Maydell            "-one-insn-per-tb  run with one guest instruction per emulated TB\n"
17389974523SIlya Leoshkevich            "-tb-size size     TCG translation block cache size\n"
17484778508Sblueswir1            "-strace           log system calls\n"
1756913e79cSLluís Vilanova            "-trace            [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
1766913e79cSLluís Vilanova            "                  specify tracing options\n"
17784778508Sblueswir1            "\n"
17884778508Sblueswir1            "Environment variables:\n"
17984778508Sblueswir1            "QEMU_STRACE       Print system calls and arguments similar to the\n"
18084778508Sblueswir1            "                  'strace' program.  Enable by setting to any value.\n"
181fc0d96b4SBlue Swirl            "You can use -E and -U options to set/unset environment variables\n"
182fc0d96b4SBlue Swirl            "for target process.  It is possible to provide several variables\n"
183fc0d96b4SBlue Swirl            "by repeating the option.  For example:\n"
184fc0d96b4SBlue Swirl            "    -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
185fc0d96b4SBlue Swirl            "Note that if you provide several changes to single variable\n"
186fc0d96b4SBlue Swirl            "last change will stay in effect.\n"
187f5048cb7SEric Blake            "\n"
188f5048cb7SEric Blake            QEMU_HELP_BOTTOM "\n"
18984778508Sblueswir1            ,
1902e59915dSPaolo Bonzini            TARGET_NAME,
19184778508Sblueswir1            interp_prefix,
192312a0b1cSWarner Losh            target_dflssiz);
1932d18e637Sblueswir1     exit(1);
19484778508Sblueswir1 }
19584778508Sblueswir1 
196d42df502SWarner Losh __thread CPUState *thread_cpu;
19784778508Sblueswir1 
stop_all_tasks(void)198653ccec2SWarner Losh void stop_all_tasks(void)
199653ccec2SWarner Losh {
200653ccec2SWarner Losh     /*
201653ccec2SWarner Losh      * We trust when using NPTL (pthreads) start_exclusive() handles thread
202653ccec2SWarner Losh      * stopping correctly.
203653ccec2SWarner Losh      */
204653ccec2SWarner Losh     start_exclusive();
205653ccec2SWarner Losh }
206653ccec2SWarner Losh 
qemu_cpu_is_self(CPUState * cpu)20748f59211SEd Maste bool qemu_cpu_is_self(CPUState *cpu)
20848f59211SEd Maste {
20948f59211SEd Maste     return thread_cpu == cpu;
21048f59211SEd Maste }
21148f59211SEd Maste 
qemu_cpu_kick(CPUState * cpu)21248f59211SEd Maste void qemu_cpu_kick(CPUState *cpu)
21348f59211SEd Maste {
21448f59211SEd Maste     cpu_exit(cpu);
21548f59211SEd Maste }
21648f59211SEd Maste 
21784778508Sblueswir1 /* Assumes contents are already zeroed.  */
init_task_state(TaskState * ts)218b46d4ad7SWarner Losh static void init_task_state(TaskState *ts)
21984778508Sblueswir1 {
22046f4f76dSWarner Losh     ts->sigaltstack_used = (struct target_sigaltstack) {
22146f4f76dSWarner Losh         .ss_sp = 0,
22246f4f76dSWarner Losh         .ss_size = 0,
22346f4f76dSWarner Losh         .ss_flags = TARGET_SS_DISABLE,
22446f4f76dSWarner Losh     };
22584778508Sblueswir1 }
22684778508Sblueswir1 
gemu_log(const char * fmt,...)227f0f7f9dcSWarner Losh void gemu_log(const char *fmt, ...)
228f0f7f9dcSWarner Losh {
229f0f7f9dcSWarner Losh     va_list ap;
230f0f7f9dcSWarner Losh 
231f0f7f9dcSWarner Losh     va_start(ap, fmt);
232f0f7f9dcSWarner Losh     vfprintf(stderr, fmt, ap);
233f0f7f9dcSWarner Losh     va_end(ap);
234f0f7f9dcSWarner Losh }
235f0f7f9dcSWarner Losh 
236312a0b1cSWarner Losh static void
adjust_ssize(void)237312a0b1cSWarner Losh adjust_ssize(void)
238312a0b1cSWarner Losh {
239312a0b1cSWarner Losh     struct rlimit rl;
240312a0b1cSWarner Losh 
241312a0b1cSWarner Losh     if (getrlimit(RLIMIT_STACK, &rl) != 0) {
242312a0b1cSWarner Losh         return;
243312a0b1cSWarner Losh     }
244312a0b1cSWarner Losh 
245312a0b1cSWarner Losh     target_maxssiz = MIN(target_maxssiz, rl.rlim_max);
246312a0b1cSWarner Losh     target_dflssiz = MIN(MAX(target_dflssiz, rl.rlim_cur), target_maxssiz);
247312a0b1cSWarner Losh 
248312a0b1cSWarner Losh     rl.rlim_max = target_maxssiz;
249312a0b1cSWarner Losh     rl.rlim_cur = target_dflssiz;
250312a0b1cSWarner Losh     setrlimit(RLIMIT_STACK, &rl);
251312a0b1cSWarner Losh }
252312a0b1cSWarner Losh 
main(int argc,char ** argv)25384778508Sblueswir1 int main(int argc, char **argv)
25484778508Sblueswir1 {
25584778508Sblueswir1     const char *filename;
25684778508Sblueswir1     const char *cpu_model;
2572278b939SIgor Mammedov     const char *cpu_type;
258989b697dSPeter Maydell     const char *log_file = NULL;
259c235d738SMatthew Fernandez     const char *log_mask = NULL;
26003ecf078SWarner Losh     const char *seed_optarg = NULL;
26184778508Sblueswir1     struct target_pt_regs regs1, *regs = &regs1;
26284778508Sblueswir1     struct image_info info1, *info = &info1;
263d37853f9SWarner Losh     struct bsd_binprm bprm;
264031fe7afSWarner Losh     TaskState *ts;
2659349b4f9SAndreas Färber     CPUArchState *env;
266db6b81d4SAndreas Färber     CPUState *cpu;
26729aabb4fSWarner Losh     int optind, rv;
26884778508Sblueswir1     const char *r;
269fcedd920SAlex Bennée     const char *gdbstub = NULL;
270fc0d96b4SBlue Swirl     char **target_environ, **wrk;
271fc0d96b4SBlue Swirl     envlist_t *envlist = NULL;
272b8012648SColin Percival     char *argv0 = NULL;
273ba379542SWarner Losh     int host_page_size;
274cb4c2590SWarner Losh     unsigned long max_reserved_va;
27584778508Sblueswir1 
276312a0b1cSWarner Losh     adjust_ssize();
277312a0b1cSWarner Losh 
278b23a51dcSWarner Losh     if (argc <= 1) {
27984778508Sblueswir1         usage();
280b23a51dcSWarner Losh     }
28184778508Sblueswir1 
28201a298a5SWarner Losh 
283f5852efaSChristophe Fergeau     error_init(argv[0]);
284fe4db84dSDaniel P. Berrange     module_call_init(MODULE_INIT_TRACE);
285267f685bSPaolo Bonzini     qemu_init_cpu_list();
286ce008c1fSAndreas Färber     module_call_init(MODULE_INIT_QOM);
287ce008c1fSAndreas Färber 
288ec45bbe5SSaurav Sachidanand     envlist = envlist_create();
289fc0d96b4SBlue Swirl 
2907f750efcSAndreas Schwab     /*
2917f750efcSAndreas Schwab      * add current environment into the list
2927f750efcSAndreas Schwab      * envlist_setenv adds to the front of the list; to preserve environ
2937f750efcSAndreas Schwab      * order add from back to front
2947f750efcSAndreas Schwab      */
295fc0d96b4SBlue Swirl     for (wrk = environ; *wrk != NULL; wrk++) {
2967f750efcSAndreas Schwab         continue;
2977f750efcSAndreas Schwab     }
2987f750efcSAndreas Schwab     while (wrk != environ) {
2997f750efcSAndreas Schwab         wrk--;
300fc0d96b4SBlue Swirl         (void) envlist_setenv(envlist, *wrk);
301fc0d96b4SBlue Swirl     }
302fc0d96b4SBlue Swirl 
3038c45039fSRichard Henderson     qemu_host_page_size = getpagesize();
3048c45039fSRichard Henderson     qemu_host_page_size = MAX(qemu_host_page_size, TARGET_PAGE_SIZE);
3058c45039fSRichard Henderson 
30684778508Sblueswir1     cpu_model = NULL;
3070c62de2fSJuergen Lock 
3086913e79cSLluís Vilanova     qemu_add_opts(&qemu_trace_opts);
3096913e79cSLluís Vilanova 
31084778508Sblueswir1     optind = 1;
31184778508Sblueswir1     for (;;) {
312b23a51dcSWarner Losh         if (optind >= argc) {
31384778508Sblueswir1             break;
314b23a51dcSWarner Losh         }
31584778508Sblueswir1         r = argv[optind];
316b23a51dcSWarner Losh         if (r[0] != '-') {
31784778508Sblueswir1             break;
318b23a51dcSWarner Losh         }
31984778508Sblueswir1         optind++;
32084778508Sblueswir1         r++;
32184778508Sblueswir1         if (!strcmp(r, "-")) {
32284778508Sblueswir1             break;
32384778508Sblueswir1         } else if (!strcmp(r, "d")) {
324c235d738SMatthew Fernandez             if (optind >= argc) {
32584778508Sblueswir1                 break;
32684778508Sblueswir1             }
327c235d738SMatthew Fernandez             log_mask = argv[optind++];
328c235d738SMatthew Fernandez         } else if (!strcmp(r, "D")) {
329c235d738SMatthew Fernandez             if (optind >= argc) {
330c235d738SMatthew Fernandez                 break;
33184778508Sblueswir1             }
332c235d738SMatthew Fernandez             log_file = argv[optind++];
333fc0d96b4SBlue Swirl         } else if (!strcmp(r, "E")) {
334fc0d96b4SBlue Swirl             r = argv[optind++];
335b23a51dcSWarner Losh             if (envlist_setenv(envlist, r) != 0) {
336fc0d96b4SBlue Swirl                 usage();
337b23a51dcSWarner Losh             }
338f66724c9SStefan Weil         } else if (!strcmp(r, "ignore-environment")) {
339f66724c9SStefan Weil             envlist_free(envlist);
340ec45bbe5SSaurav Sachidanand             envlist = envlist_create();
341fc0d96b4SBlue Swirl         } else if (!strcmp(r, "U")) {
342fc0d96b4SBlue Swirl             r = argv[optind++];
343b23a51dcSWarner Losh             if (envlist_unsetenv(envlist, r) != 0) {
344fc0d96b4SBlue Swirl                 usage();
345b23a51dcSWarner Losh             }
34684778508Sblueswir1         } else if (!strcmp(r, "s")) {
34784778508Sblueswir1             r = argv[optind++];
348312a0b1cSWarner Losh             rv = qemu_strtoul(r, &r, 0, &target_dflssiz);
349312a0b1cSWarner Losh             if (rv < 0 || target_dflssiz <= 0) {
35084778508Sblueswir1                 usage();
351b23a51dcSWarner Losh             }
352b23a51dcSWarner Losh             if (*r == 'M') {
353312a0b1cSWarner Losh                 target_dflssiz *= 1024 * 1024;
354b23a51dcSWarner Losh             } else if (*r == 'k' || *r == 'K') {
355312a0b1cSWarner Losh                 target_dflssiz *= 1024;
356312a0b1cSWarner Losh             }
357312a0b1cSWarner Losh             if (target_dflssiz > target_maxssiz) {
358312a0b1cSWarner Losh                 usage();
359b23a51dcSWarner Losh             }
36084778508Sblueswir1         } else if (!strcmp(r, "L")) {
36184778508Sblueswir1             interp_prefix = argv[optind++];
36284778508Sblueswir1         } else if (!strcmp(r, "p")) {
36301e44980SRichard Henderson             unsigned size, want = qemu_real_host_page_size();
36401e44980SRichard Henderson 
36501e44980SRichard Henderson             r = argv[optind++];
36601e44980SRichard Henderson             if (qemu_strtoui(r, NULL, 10, &size) || size != want) {
36701e44980SRichard Henderson                 warn_report("Deprecated page size option cannot "
36801e44980SRichard Henderson                             "change host page size (%u)", want);
36984778508Sblueswir1             }
37084778508Sblueswir1         } else if (!strcmp(r, "g")) {
371fcedd920SAlex Bennée             gdbstub = g_strdup(argv[optind++]);
37284778508Sblueswir1         } else if (!strcmp(r, "r")) {
37384778508Sblueswir1             qemu_uname_release = argv[optind++];
37484778508Sblueswir1         } else if (!strcmp(r, "cpu")) {
37584778508Sblueswir1             cpu_model = argv[optind++];
376c8057f95SPeter Maydell             if (is_help_option(cpu_model)) {
377dfa47531SGavin Shan                 list_cpus();
3782d18e637Sblueswir1                 exit(1);
37984778508Sblueswir1             }
3802fa5d9baSBlue Swirl         } else if (!strcmp(r, "B")) {
38129aabb4fSWarner Losh             rv = qemu_strtoul(argv[optind++], NULL, 0, &guest_base);
38229aabb4fSWarner Losh             if (rv < 0) {
38329aabb4fSWarner Losh                 usage();
38429aabb4fSWarner Losh             }
385e307c192SRichard Henderson             have_guest_base = true;
38684778508Sblueswir1         } else if (!strcmp(r, "drop-ld-preload")) {
387fc0d96b4SBlue Swirl             (void) envlist_unsetenv(envlist, "LD_PRELOAD");
38803ecf078SWarner Losh         } else if (!strcmp(r, "seed")) {
38903ecf078SWarner Losh             seed_optarg = optarg;
390c61a0d31SPhilippe Mathieu-Daudé         } else if (!strcmp(r, "one-insn-per-tb")) {
3913cfb0456SPeter Maydell             opt_one_insn_per_tb = true;
39289974523SIlya Leoshkevich         } else if (!strcmp(r, "tb-size")) {
39389974523SIlya Leoshkevich             r = argv[optind++];
39489974523SIlya Leoshkevich             if (qemu_strtoul(r, NULL, 0, &opt_tb_size)) {
39589974523SIlya Leoshkevich                 usage();
39689974523SIlya Leoshkevich             }
39784778508Sblueswir1         } else if (!strcmp(r, "strace")) {
39884778508Sblueswir1             do_strace = 1;
3996913e79cSLluís Vilanova         } else if (!strcmp(r, "trace")) {
40092eecfffSPaolo Bonzini             trace_opt_parse(optarg);
401b8012648SColin Percival         } else if (!strcmp(r, "0")) {
402b8012648SColin Percival             argv0 = argv[optind++];
4036913e79cSLluís Vilanova         } else {
40484778508Sblueswir1             usage();
40584778508Sblueswir1         }
40684778508Sblueswir1     }
40784778508Sblueswir1 
4088c45039fSRichard Henderson     qemu_host_page_mask = -qemu_host_page_size;
4098c45039fSRichard Henderson 
410c235d738SMatthew Fernandez     /* init debug */
411905c78feSRichard Henderson     {
412905c78feSRichard Henderson         int mask = 0;
413c235d738SMatthew Fernandez         if (log_mask) {
4144fde1ebaSPeter Maydell             mask = qemu_str_to_log_mask(log_mask);
415c235d738SMatthew Fernandez             if (!mask) {
41659a6fa6eSPeter Maydell                 qemu_print_log_usage(stdout);
417c235d738SMatthew Fernandez                 exit(1);
418c235d738SMatthew Fernandez             }
419905c78feSRichard Henderson         }
420905c78feSRichard Henderson         qemu_set_log_filename_flags(log_file, mask, &error_fatal);
421c235d738SMatthew Fernandez     }
422c235d738SMatthew Fernandez 
4234b5dfd82SPeter Maydell     if (optind >= argc) {
4244b5dfd82SPeter Maydell         usage();
4254b5dfd82SPeter Maydell     }
4264b5dfd82SPeter Maydell     filename = argv[optind];
427b8012648SColin Percival     if (argv0) {
428b8012648SColin Percival         argv[optind] = argv0;
429b8012648SColin Percival     }
4304b5dfd82SPeter Maydell 
4316913e79cSLluís Vilanova     if (!trace_init_backends()) {
4326913e79cSLluís Vilanova         exit(1);
4336913e79cSLluís Vilanova     }
43492eecfffSPaolo Bonzini     trace_init_file();
4356913e79cSLluís Vilanova 
43684778508Sblueswir1     /* Zero out regs */
43784778508Sblueswir1     memset(regs, 0, sizeof(struct target_pt_regs));
43884778508Sblueswir1 
439d37853f9SWarner Losh     /* Zero bsd params */
440d37853f9SWarner Losh     memset(&bprm, 0, sizeof(bprm));
441d37853f9SWarner Losh 
44284778508Sblueswir1     /* Zero out image_info */
44384778508Sblueswir1     memset(info, 0, sizeof(struct image_info));
44484778508Sblueswir1 
44584778508Sblueswir1     /* Scan interp_prefix dir for replacement files. */
44684778508Sblueswir1     init_paths(interp_prefix);
44784778508Sblueswir1 
44884778508Sblueswir1     if (cpu_model == NULL) {
449031fe7afSWarner Losh         cpu_model = TARGET_DEFAULT_CPU_MODEL;
45084778508Sblueswir1     }
4512b5249b8SIgor Mammedov 
452b86f59c7SClaudio Fontana     cpu_type = parse_cpu_option(cpu_model);
453031fe7afSWarner Losh 
4542b5249b8SIgor Mammedov     /* init tcg before creating CPUs and to get qemu_host_page_size */
455940e43aaSClaudio Fontana     {
4563cfb0456SPeter Maydell         AccelState *accel = current_accel();
4573cfb0456SPeter Maydell         AccelClass *ac = ACCEL_GET_CLASS(accel);
4582b5249b8SIgor Mammedov 
459b86f59c7SClaudio Fontana         accel_init_interfaces(ac);
4603cfb0456SPeter Maydell         object_property_set_bool(OBJECT(accel), "one-insn-per-tb",
4613cfb0456SPeter Maydell                                  opt_one_insn_per_tb, &error_abort);
46289974523SIlya Leoshkevich         object_property_set_int(OBJECT(accel), "tb-size",
46389974523SIlya Leoshkevich                                 opt_tb_size, &error_abort);
46492242f34SClaudio Fontana         ac->init_machine(NULL);
465940e43aaSClaudio Fontana     }
466ba379542SWarner Losh 
467ba379542SWarner Losh     /*
468ba379542SWarner Losh      * Finalize page size before creating CPUs.
469ba379542SWarner Losh      * This will do nothing if !TARGET_PAGE_BITS_VARY.
470ba379542SWarner Losh      * The most efficient setting is to match the host.
471ba379542SWarner Losh      */
472ba379542SWarner Losh     host_page_size = qemu_real_host_page_size();
473ba379542SWarner Losh     set_preferred_target_page_bits(ctz32(host_page_size));
474ba379542SWarner Losh     finalize_target_page_bits();
475ba379542SWarner Losh 
4762278b939SIgor Mammedov     cpu = cpu_create(cpu_type);
477b77af26eSRichard Henderson     env = cpu_env(cpu);
478db6b81d4SAndreas Färber     cpu_reset(cpu);
479db6b81d4SAndreas Färber     thread_cpu = cpu;
48084778508Sblueswir1 
481cb4c2590SWarner Losh     /*
482cb4c2590SWarner Losh      * Reserving too much vm space via mmap can run into problems with rlimits,
483cb4c2590SWarner Losh      * oom due to page table creation, etc.  We will still try it, if directed
484cb4c2590SWarner Losh      * by the command-line option, but not by default. Unless we're running a
485cb4c2590SWarner Losh      * target address space of 32 or fewer bits on a host with 64 bits.
486cb4c2590SWarner Losh      */
487cb4c2590SWarner Losh     max_reserved_va = MAX_RESERVED_VA(cpu);
488cb4c2590SWarner Losh     if (reserved_va != 0) {
489cb4c2590SWarner Losh         if ((reserved_va + 1) % host_page_size) {
490cb4c2590SWarner Losh             char *s = size_to_str(host_page_size);
491cb4c2590SWarner Losh             fprintf(stderr, "Reserved virtual address not aligned mod %s\n", s);
492cb4c2590SWarner Losh             g_free(s);
493cb4c2590SWarner Losh             exit(EXIT_FAILURE);
494cb4c2590SWarner Losh         }
495cb4c2590SWarner Losh         if (max_reserved_va && reserved_va > max_reserved_va) {
496cb4c2590SWarner Losh             fprintf(stderr, "Reserved virtual address too big\n");
497cb4c2590SWarner Losh             exit(EXIT_FAILURE);
498cb4c2590SWarner Losh         }
499cb4c2590SWarner Losh     } else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) {
500cb4c2590SWarner Losh         /* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */
501cb4c2590SWarner Losh         reserved_va = max_reserved_va;
502cb4c2590SWarner Losh     }
503cb4c2590SWarner Losh 
50484778508Sblueswir1     if (getenv("QEMU_STRACE")) {
50584778508Sblueswir1         do_strace = 1;
50684778508Sblueswir1     }
50784778508Sblueswir1 
508fc0d96b4SBlue Swirl     target_environ = envlist_to_environ(envlist, NULL);
509fc0d96b4SBlue Swirl     envlist_free(envlist);
51084778508Sblueswir1 
51103ecf078SWarner Losh     {
51203ecf078SWarner Losh         Error *err = NULL;
51303ecf078SWarner Losh         if (seed_optarg != NULL) {
51403ecf078SWarner Losh             qemu_guest_random_seed_main(seed_optarg, &err);
51503ecf078SWarner Losh         } else {
51603ecf078SWarner Losh             qcrypto_init(&err);
51703ecf078SWarner Losh         }
51803ecf078SWarner Losh         if (err) {
51903ecf078SWarner Losh             error_reportf_err(err, "cannot initialize crypto: ");
52003ecf078SWarner Losh             exit(1);
52103ecf078SWarner Losh         }
52203ecf078SWarner Losh     }
52303ecf078SWarner Losh 
5242fa5d9baSBlue Swirl     /*
525fa79cde6SRichard Henderson      * Now that page sizes are configured we can do
5262fa5d9baSBlue Swirl      * proper page alignment for guest_base.
5272fa5d9baSBlue Swirl      */
52828b61d49SRichard Henderson     if (have_guest_base) {
52928b61d49SRichard Henderson         if (guest_base & ~qemu_host_page_mask) {
53028b61d49SRichard Henderson             error_report("Selected guest base not host page aligned");
53128b61d49SRichard Henderson             exit(1);
53228b61d49SRichard Henderson         }
53328b61d49SRichard Henderson     }
53428b61d49SRichard Henderson 
53528b61d49SRichard Henderson     /*
53628b61d49SRichard Henderson      * If reserving host virtual address space, do so now.
53728b61d49SRichard Henderson      * Combined with '-B', ensure that the chosen range is free.
53828b61d49SRichard Henderson      */
53928b61d49SRichard Henderson     if (reserved_va) {
54028b61d49SRichard Henderson         void *p;
54128b61d49SRichard Henderson 
54228b61d49SRichard Henderson         if (have_guest_base) {
54328b61d49SRichard Henderson             p = mmap((void *)guest_base, reserved_va + 1, PROT_NONE,
54428b61d49SRichard Henderson                      MAP_ANON | MAP_PRIVATE | MAP_FIXED | MAP_EXCL, -1, 0);
54528b61d49SRichard Henderson         } else {
54628b61d49SRichard Henderson             p = mmap(NULL, reserved_va + 1, PROT_NONE,
54728b61d49SRichard Henderson                      MAP_ANON | MAP_PRIVATE, -1, 0);
54828b61d49SRichard Henderson         }
54928b61d49SRichard Henderson         if (p == MAP_FAILED) {
55028b61d49SRichard Henderson             const char *err = strerror(errno);
55128b61d49SRichard Henderson             char *sz = size_to_str(reserved_va + 1);
55228b61d49SRichard Henderson 
55328b61d49SRichard Henderson             if (have_guest_base) {
55428b61d49SRichard Henderson                 error_report("Cannot allocate %s bytes at -B %p for guest "
55528b61d49SRichard Henderson                              "address space: %s", sz, (void *)guest_base, err);
55628b61d49SRichard Henderson             } else {
55728b61d49SRichard Henderson                 error_report("Cannot allocate %s bytes for guest "
55828b61d49SRichard Henderson                              "address space: %s", sz, err);
55928b61d49SRichard Henderson             }
56028b61d49SRichard Henderson             exit(1);
56128b61d49SRichard Henderson         }
56228b61d49SRichard Henderson         guest_base = (uintptr_t)p;
56328b61d49SRichard Henderson         have_guest_base = true;
56428b61d49SRichard Henderson 
56528b61d49SRichard Henderson         /* Ensure that mmap_next_start is within range. */
56628b61d49SRichard Henderson         if (reserved_va <= mmap_next_start) {
56728b61d49SRichard Henderson             mmap_next_start = (reserved_va / 4 * 3)
56828b61d49SRichard Henderson                               & TARGET_PAGE_MASK & qemu_host_page_mask;
56928b61d49SRichard Henderson         }
57028b61d49SRichard Henderson     }
5712fa5d9baSBlue Swirl 
572d37853f9SWarner Losh     if (loader_exec(filename, argv + optind, target_environ, regs, info,
573d37853f9SWarner Losh                     &bprm) != 0) {
57484778508Sblueswir1         printf("Error loading %s\n", filename);
57584778508Sblueswir1         _exit(1);
57684778508Sblueswir1     }
57784778508Sblueswir1 
57884778508Sblueswir1     for (wrk = target_environ; *wrk; wrk++) {
579ec45bbe5SSaurav Sachidanand         g_free(*wrk);
58084778508Sblueswir1     }
58184778508Sblueswir1 
582ec45bbe5SSaurav Sachidanand     g_free(target_environ);
58384778508Sblueswir1 
58413829020SPaolo Bonzini     if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
58543b76161SRichard Henderson         FILE *f = qemu_log_trylock();
58643b76161SRichard Henderson         if (f) {
58743b76161SRichard Henderson             fprintf(f, "guest_base  %p\n", (void *)guest_base);
58843b76161SRichard Henderson             fprintf(f, "page layout changed following binary load\n");
58943b76161SRichard Henderson             page_dump(f);
59084778508Sblueswir1 
59143b76161SRichard Henderson             fprintf(f, "end_code    0x" TARGET_ABI_FMT_lx "\n",
59243b76161SRichard Henderson                     info->end_code);
59343b76161SRichard Henderson             fprintf(f, "start_code  0x" TARGET_ABI_FMT_lx "\n",
59484778508Sblueswir1                     info->start_code);
59543b76161SRichard Henderson             fprintf(f, "start_data  0x" TARGET_ABI_FMT_lx "\n",
59684778508Sblueswir1                     info->start_data);
59743b76161SRichard Henderson             fprintf(f, "end_data    0x" TARGET_ABI_FMT_lx "\n",
59843b76161SRichard Henderson                     info->end_data);
59943b76161SRichard Henderson             fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n",
60084778508Sblueswir1                     info->start_stack);
60143b76161SRichard Henderson             fprintf(f, "brk         0x" TARGET_ABI_FMT_lx "\n", info->brk);
60243b76161SRichard Henderson             fprintf(f, "entry       0x" TARGET_ABI_FMT_lx "\n", info->entry);
60343b76161SRichard Henderson 
60443b76161SRichard Henderson             qemu_log_unlock(f);
60543b76161SRichard Henderson         }
6062e77eac6Sblueswir1     }
60784778508Sblueswir1 
608031fe7afSWarner Losh     /* build Task State */
609031fe7afSWarner Losh     ts = g_new0(TaskState, 1);
610031fe7afSWarner Losh     init_task_state(ts);
611031fe7afSWarner Losh     ts->info = info;
612031fe7afSWarner Losh     ts->bprm = &bprm;
613*52a523afSJessica Clarke     ts->ts_tid = qemu_get_thread_id();
614031fe7afSWarner Losh     cpu->opaque = ts;
615031fe7afSWarner Losh 
61684778508Sblueswir1     target_set_brk(info->brk);
61784778508Sblueswir1     syscall_init();
61884778508Sblueswir1     signal_init();
61984778508Sblueswir1 
62034bc8475SWarner Losh     /*
62134bc8475SWarner Losh      * Now that we've loaded the binary, GUEST_BASE is fixed.  Delay
62234bc8475SWarner Losh      * generating the prologue until now so that the prologue can take
62334bc8475SWarner Losh      * the real value of GUEST_BASE into account.
62434bc8475SWarner Losh      */
625935f75aeSRichard Henderson     tcg_prologue_init();
6269002ec79SRichard Henderson 
627031fe7afSWarner Losh     target_cpu_init(env, regs);
62884778508Sblueswir1 
629fcedd920SAlex Bennée     if (gdbstub) {
630fcedd920SAlex Bennée         gdbserver_start(gdbstub);
631f84e313eSGustavo Romero         gdb_handlesig(cpu, 0, NULL, NULL, 0);
63284778508Sblueswir1     }
63378cfb07fSJuergen Lock     cpu_loop(env);
63484778508Sblueswir1     /* never exits */
63584778508Sblueswir1     return 0;
63684778508Sblueswir1 }
637