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"
3863c91552SPaolo Bonzini #include "exec/exec-all.h"
3916aa8eaaSPhilippe Mathieu-Daudé #include "user/guest-base.h"
40d7ec12f8SRichard Henderson #include "tcg/startup.h"
411de7afc9SPaolo Bonzini #include "qemu/timer.h"
421de7afc9SPaolo Bonzini #include "qemu/envlist.h"
4329aabb4fSWarner Losh #include "qemu/cutils.h"
44508127e2SPaolo Bonzini #include "exec/log.h"
456913e79cSLluís Vilanova #include "trace/control.h"
4603ecf078SWarner Losh #include "crypto/init.h"
4703ecf078SWarner Losh #include "qemu/guest-random.h"
48d96bf49bSAlex Bennée #include "gdbstub/user.h"
49fc0d96b4SBlue Swirl
50d1dc9ab3SWarner Losh #include "host-os.h"
51031fe7afSWarner Losh #include "target_arch_cpu.h"
52d1dc9ab3SWarner Losh
538c45039fSRichard Henderson
548c45039fSRichard Henderson /*
558c45039fSRichard Henderson * TODO: Remove these and rely only on qemu_real_host_page_size().
568c45039fSRichard Henderson */
578c45039fSRichard Henderson uintptr_t qemu_host_page_size;
588c45039fSRichard Henderson intptr_t qemu_host_page_mask;
598c45039fSRichard Henderson
603cfb0456SPeter Maydell static bool opt_one_insn_per_tb;
615ca870b9SRichard Henderson uintptr_t guest_base;
62e307c192SRichard Henderson bool have_guest_base;
63be04f210SWarner Losh /*
64be04f210SWarner Losh * When running 32-on-64 we should make sure we can fit all of the possible
65be04f210SWarner Losh * guest address space into a contiguous chunk of virtual host memory.
66be04f210SWarner Losh *
67be04f210SWarner Losh * This way we will never overlap with our own libraries or binaries or stack
68be04f210SWarner Losh * or anything else that QEMU maps.
69be04f210SWarner Losh *
70be04f210SWarner Losh * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
71be04f210SWarner Losh * of the address for the kernel. Some cpus rely on this and user space
72be04f210SWarner Losh * uses the high bit(s) for pointer tagging and the like. For them, we
73be04f210SWarner Losh * must preserve the expected address space.
74be04f210SWarner Losh */
75be04f210SWarner Losh #ifndef MAX_RESERVED_VA
76be04f210SWarner Losh # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
77be04f210SWarner Losh # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
78be04f210SWarner Losh (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
7995059f9cSRichard Henderson # define MAX_RESERVED_VA 0xfffffffful
80be04f210SWarner Losh # else
8195059f9cSRichard Henderson # define MAX_RESERVED_VA ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
82be04f210SWarner Losh # endif
83be04f210SWarner Losh # else
84be04f210SWarner Losh # define MAX_RESERVED_VA 0
85be04f210SWarner Losh # endif
86be04f210SWarner Losh #endif
87be04f210SWarner Losh
88be04f210SWarner Losh /*
89be04f210SWarner Losh * That said, reserving *too* much vm space via mmap can run into problems
90be04f210SWarner Losh * with rlimits, oom due to page table creation, etc. We will still try it,
91be04f210SWarner Losh * if directed by the command-line option, but not by default.
92be04f210SWarner Losh */
93be04f210SWarner Losh #if HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32
94be04f210SWarner Losh unsigned long reserved_va = MAX_RESERVED_VA;
95be04f210SWarner Losh #else
96d6ef40bfSPeter Maydell unsigned long reserved_va;
97be04f210SWarner Losh #endif
981b530a6dSaurel32
9986327290SStacey Son const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
100fccae322SPeter Maydell const char *qemu_uname_release;
10101a298a5SWarner Losh char qemu_proc_pathname[PATH_MAX]; /* full path to exeutable */
10284778508Sblueswir1
103312a0b1cSWarner Losh unsigned long target_maxtsiz = TARGET_MAXTSIZ; /* max text size */
104312a0b1cSWarner Losh unsigned long target_dfldsiz = TARGET_DFLDSIZ; /* initial data size limit */
105312a0b1cSWarner Losh unsigned long target_maxdsiz = TARGET_MAXDSIZ; /* max data size */
106312a0b1cSWarner Losh unsigned long target_dflssiz = TARGET_DFLSSIZ; /* initial data size limit */
107312a0b1cSWarner Losh unsigned long target_maxssiz = TARGET_MAXSSIZ; /* max stack size */
108312a0b1cSWarner Losh unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */
10984778508Sblueswir1
11063cca106SWarner Losh /* Helper routines for implementing atomic operations. */
1119399f095Sblueswir1
fork_start(void)1129399f095Sblueswir1 void fork_start(void)
1139399f095Sblueswir1 {
11463cca106SWarner Losh start_exclusive();
11563cca106SWarner Losh cpu_list_lock();
11663cca106SWarner Losh mmap_fork_start();
1173d6ed98dSIlya Leoshkevich gdbserver_fork_start();
1189399f095Sblueswir1 }
1199399f095Sblueswir1
fork_end(pid_t pid)1204edc98fcSIlya Leoshkevich void fork_end(pid_t pid)
1219399f095Sblueswir1 {
1224edc98fcSIlya Leoshkevich bool child = pid == 0;
1234edc98fcSIlya Leoshkevich
1249399f095Sblueswir1 if (child) {
12563cca106SWarner Losh CPUState *cpu, *next_cpu;
12663cca106SWarner Losh /*
12763cca106SWarner Losh * Child processes created by fork() only have a single thread. Discard
12863cca106SWarner Losh * information about the parent threads.
12963cca106SWarner Losh */
13063cca106SWarner Losh CPU_FOREACH_SAFE(cpu, next_cpu) {
13163cca106SWarner Losh if (cpu != thread_cpu) {
1323c55dd58SPhilippe Mathieu-Daudé QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
13363cca106SWarner Losh }
13463cca106SWarner Losh }
13563cca106SWarner Losh mmap_fork_end(child);
13663cca106SWarner Losh /*
13763cca106SWarner Losh * qemu_init_cpu_list() takes care of reinitializing the exclusive
13863cca106SWarner Losh * state, so we don't need to end_exclusive() here.
13963cca106SWarner Losh */
14063cca106SWarner Losh qemu_init_cpu_list();
141d4e1369aSIlya Leoshkevich get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id();
1426604b057SIlya Leoshkevich gdbserver_fork_end(thread_cpu, pid);
14363cca106SWarner Losh } else {
14463cca106SWarner Losh mmap_fork_end(child);
14563cca106SWarner Losh cpu_list_unlock();
1466604b057SIlya Leoshkevich gdbserver_fork_end(thread_cpu, pid);
14763cca106SWarner Losh end_exclusive();
1489399f095Sblueswir1 }
1499399f095Sblueswir1 }
1509399f095Sblueswir1
cpu_loop(CPUArchState * env)151031fe7afSWarner Losh void cpu_loop(CPUArchState *env)
15231fc12dfSblueswir1 {
153031fe7afSWarner Losh target_cpu_loop(env);
15431fc12dfSblueswir1 }
15531fc12dfSblueswir1
usage(void)15684778508Sblueswir1 static void usage(void)
15784778508Sblueswir1 {
1587e563bfbSThomas Huth printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
1590781dd6eSThomas Huth "\n" QEMU_COPYRIGHT "\n"
1602e59915dSPaolo Bonzini "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
16184778508Sblueswir1 "BSD CPU emulator (compiled for %s emulation)\n"
16284778508Sblueswir1 "\n"
16384778508Sblueswir1 "Standard options:\n"
16484778508Sblueswir1 "-h print this help\n"
16584778508Sblueswir1 "-g port wait gdb connection to port\n"
16684778508Sblueswir1 "-L path set the elf interpreter prefix (default=%s)\n"
16784778508Sblueswir1 "-s size set the stack size in bytes (default=%ld)\n"
168c8057f95SPeter Maydell "-cpu model select CPU (-cpu help for list)\n"
16984778508Sblueswir1 "-drop-ld-preload drop LD_PRELOAD for target process\n"
170fc0d96b4SBlue Swirl "-E var=value sets/modifies targets environment variable(s)\n"
171fc0d96b4SBlue Swirl "-U var unsets targets environment variable(s)\n"
1722fa5d9baSBlue Swirl "-B address set guest_base address to address\n"
17384778508Sblueswir1 "\n"
17484778508Sblueswir1 "Debug options:\n"
175989b697dSPeter Maydell "-d item1[,...] enable logging of specified items\n"
176989b697dSPeter Maydell " (use '-d help' for a list of log items)\n"
177989b697dSPeter Maydell "-D logfile write logs to 'logfile' (default stderr)\n"
178060e0cd7SPeter Maydell "-one-insn-per-tb run with one guest instruction per emulated TB\n"
17984778508Sblueswir1 "-strace log system calls\n"
1806913e79cSLluís Vilanova "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
1816913e79cSLluís Vilanova " specify tracing options\n"
18284778508Sblueswir1 "\n"
18384778508Sblueswir1 "Environment variables:\n"
18484778508Sblueswir1 "QEMU_STRACE Print system calls and arguments similar to the\n"
18584778508Sblueswir1 " 'strace' program. Enable by setting to any value.\n"
186fc0d96b4SBlue Swirl "You can use -E and -U options to set/unset environment variables\n"
187fc0d96b4SBlue Swirl "for target process. It is possible to provide several variables\n"
188fc0d96b4SBlue Swirl "by repeating the option. For example:\n"
189fc0d96b4SBlue Swirl " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
190fc0d96b4SBlue Swirl "Note that if you provide several changes to single variable\n"
191fc0d96b4SBlue Swirl "last change will stay in effect.\n"
192f5048cb7SEric Blake "\n"
193f5048cb7SEric Blake QEMU_HELP_BOTTOM "\n"
19484778508Sblueswir1 ,
1952e59915dSPaolo Bonzini TARGET_NAME,
19684778508Sblueswir1 interp_prefix,
197312a0b1cSWarner Losh target_dflssiz);
1982d18e637Sblueswir1 exit(1);
19984778508Sblueswir1 }
20084778508Sblueswir1
201d42df502SWarner Losh __thread CPUState *thread_cpu;
20284778508Sblueswir1
stop_all_tasks(void)203653ccec2SWarner Losh void stop_all_tasks(void)
204653ccec2SWarner Losh {
205653ccec2SWarner Losh /*
206653ccec2SWarner Losh * We trust when using NPTL (pthreads) start_exclusive() handles thread
207653ccec2SWarner Losh * stopping correctly.
208653ccec2SWarner Losh */
209653ccec2SWarner Losh start_exclusive();
210653ccec2SWarner Losh }
211653ccec2SWarner Losh
qemu_cpu_is_self(CPUState * cpu)21248f59211SEd Maste bool qemu_cpu_is_self(CPUState *cpu)
21348f59211SEd Maste {
21448f59211SEd Maste return thread_cpu == cpu;
21548f59211SEd Maste }
21648f59211SEd Maste
qemu_cpu_kick(CPUState * cpu)21748f59211SEd Maste void qemu_cpu_kick(CPUState *cpu)
21848f59211SEd Maste {
21948f59211SEd Maste cpu_exit(cpu);
22048f59211SEd Maste }
22148f59211SEd Maste
22284778508Sblueswir1 /* Assumes contents are already zeroed. */
init_task_state(TaskState * ts)223b46d4ad7SWarner Losh static void init_task_state(TaskState *ts)
22484778508Sblueswir1 {
22546f4f76dSWarner Losh ts->sigaltstack_used = (struct target_sigaltstack) {
22646f4f76dSWarner Losh .ss_sp = 0,
22746f4f76dSWarner Losh .ss_size = 0,
22846f4f76dSWarner Losh .ss_flags = TARGET_SS_DISABLE,
22946f4f76dSWarner Losh };
23084778508Sblueswir1 }
23184778508Sblueswir1
gemu_log(const char * fmt,...)232f0f7f9dcSWarner Losh void gemu_log(const char *fmt, ...)
233f0f7f9dcSWarner Losh {
234f0f7f9dcSWarner Losh va_list ap;
235f0f7f9dcSWarner Losh
236f0f7f9dcSWarner Losh va_start(ap, fmt);
237f0f7f9dcSWarner Losh vfprintf(stderr, fmt, ap);
238f0f7f9dcSWarner Losh va_end(ap);
239f0f7f9dcSWarner Losh }
240f0f7f9dcSWarner Losh
241312a0b1cSWarner Losh static void
adjust_ssize(void)242312a0b1cSWarner Losh adjust_ssize(void)
243312a0b1cSWarner Losh {
244312a0b1cSWarner Losh struct rlimit rl;
245312a0b1cSWarner Losh
246312a0b1cSWarner Losh if (getrlimit(RLIMIT_STACK, &rl) != 0) {
247312a0b1cSWarner Losh return;
248312a0b1cSWarner Losh }
249312a0b1cSWarner Losh
250312a0b1cSWarner Losh target_maxssiz = MIN(target_maxssiz, rl.rlim_max);
251312a0b1cSWarner Losh target_dflssiz = MIN(MAX(target_dflssiz, rl.rlim_cur), target_maxssiz);
252312a0b1cSWarner Losh
253312a0b1cSWarner Losh rl.rlim_max = target_maxssiz;
254312a0b1cSWarner Losh rl.rlim_cur = target_dflssiz;
255312a0b1cSWarner Losh setrlimit(RLIMIT_STACK, &rl);
256312a0b1cSWarner Losh }
257312a0b1cSWarner Losh
save_proc_pathname(char * argv0)25801a298a5SWarner Losh static void save_proc_pathname(char *argv0)
25901a298a5SWarner Losh {
26001a298a5SWarner Losh int mib[4];
26101a298a5SWarner Losh size_t len;
26201a298a5SWarner Losh
26301a298a5SWarner Losh mib[0] = CTL_KERN;
26401a298a5SWarner Losh mib[1] = KERN_PROC;
26501a298a5SWarner Losh mib[2] = KERN_PROC_PATHNAME;
26601a298a5SWarner Losh mib[3] = -1;
26701a298a5SWarner Losh
26801a298a5SWarner Losh len = sizeof(qemu_proc_pathname);
26901a298a5SWarner Losh if (sysctl(mib, 4, qemu_proc_pathname, &len, NULL, 0)) {
27001a298a5SWarner Losh perror("sysctl");
27101a298a5SWarner Losh }
27201a298a5SWarner Losh }
27301a298a5SWarner Losh
main(int argc,char ** argv)27484778508Sblueswir1 int main(int argc, char **argv)
27584778508Sblueswir1 {
27684778508Sblueswir1 const char *filename;
27784778508Sblueswir1 const char *cpu_model;
2782278b939SIgor Mammedov const char *cpu_type;
279989b697dSPeter Maydell const char *log_file = NULL;
280c235d738SMatthew Fernandez const char *log_mask = NULL;
28103ecf078SWarner Losh const char *seed_optarg = NULL;
28284778508Sblueswir1 struct target_pt_regs regs1, *regs = ®s1;
28384778508Sblueswir1 struct image_info info1, *info = &info1;
284d37853f9SWarner Losh struct bsd_binprm bprm;
285031fe7afSWarner Losh TaskState *ts;
2869349b4f9SAndreas Färber CPUArchState *env;
287db6b81d4SAndreas Färber CPUState *cpu;
28829aabb4fSWarner Losh int optind, rv;
28984778508Sblueswir1 const char *r;
290fcedd920SAlex Bennée const char *gdbstub = NULL;
291fc0d96b4SBlue Swirl char **target_environ, **wrk;
292fc0d96b4SBlue Swirl envlist_t *envlist = NULL;
293b8012648SColin Percival char *argv0 = NULL;
29484778508Sblueswir1
295312a0b1cSWarner Losh adjust_ssize();
296312a0b1cSWarner Losh
297b23a51dcSWarner Losh if (argc <= 1) {
29884778508Sblueswir1 usage();
299b23a51dcSWarner Losh }
30084778508Sblueswir1
30101a298a5SWarner Losh save_proc_pathname(argv[0]);
30201a298a5SWarner Losh
303f5852efaSChristophe Fergeau error_init(argv[0]);
304fe4db84dSDaniel P. Berrange module_call_init(MODULE_INIT_TRACE);
305267f685bSPaolo Bonzini qemu_init_cpu_list();
306ce008c1fSAndreas Färber module_call_init(MODULE_INIT_QOM);
307ce008c1fSAndreas Färber
308ec45bbe5SSaurav Sachidanand envlist = envlist_create();
309fc0d96b4SBlue Swirl
3107f750efcSAndreas Schwab /*
3117f750efcSAndreas Schwab * add current environment into the list
3127f750efcSAndreas Schwab * envlist_setenv adds to the front of the list; to preserve environ
3137f750efcSAndreas Schwab * order add from back to front
3147f750efcSAndreas Schwab */
315fc0d96b4SBlue Swirl for (wrk = environ; *wrk != NULL; wrk++) {
3167f750efcSAndreas Schwab continue;
3177f750efcSAndreas Schwab }
3187f750efcSAndreas Schwab while (wrk != environ) {
3197f750efcSAndreas Schwab wrk--;
320fc0d96b4SBlue Swirl (void) envlist_setenv(envlist, *wrk);
321fc0d96b4SBlue Swirl }
322fc0d96b4SBlue Swirl
3238c45039fSRichard Henderson qemu_host_page_size = getpagesize();
3248c45039fSRichard Henderson qemu_host_page_size = MAX(qemu_host_page_size, TARGET_PAGE_SIZE);
3258c45039fSRichard Henderson
32684778508Sblueswir1 cpu_model = NULL;
3270c62de2fSJuergen Lock
3286913e79cSLluís Vilanova qemu_add_opts(&qemu_trace_opts);
3296913e79cSLluís Vilanova
33084778508Sblueswir1 optind = 1;
33184778508Sblueswir1 for (;;) {
332b23a51dcSWarner Losh if (optind >= argc) {
33384778508Sblueswir1 break;
334b23a51dcSWarner Losh }
33584778508Sblueswir1 r = argv[optind];
336b23a51dcSWarner Losh if (r[0] != '-') {
33784778508Sblueswir1 break;
338b23a51dcSWarner Losh }
33984778508Sblueswir1 optind++;
34084778508Sblueswir1 r++;
34184778508Sblueswir1 if (!strcmp(r, "-")) {
34284778508Sblueswir1 break;
34384778508Sblueswir1 } else if (!strcmp(r, "d")) {
344c235d738SMatthew Fernandez if (optind >= argc) {
34584778508Sblueswir1 break;
34684778508Sblueswir1 }
347c235d738SMatthew Fernandez log_mask = argv[optind++];
348c235d738SMatthew Fernandez } else if (!strcmp(r, "D")) {
349c235d738SMatthew Fernandez if (optind >= argc) {
350c235d738SMatthew Fernandez break;
35184778508Sblueswir1 }
352c235d738SMatthew Fernandez log_file = argv[optind++];
353fc0d96b4SBlue Swirl } else if (!strcmp(r, "E")) {
354fc0d96b4SBlue Swirl r = argv[optind++];
355b23a51dcSWarner Losh if (envlist_setenv(envlist, r) != 0) {
356fc0d96b4SBlue Swirl usage();
357b23a51dcSWarner Losh }
358f66724c9SStefan Weil } else if (!strcmp(r, "ignore-environment")) {
359f66724c9SStefan Weil envlist_free(envlist);
360ec45bbe5SSaurav Sachidanand envlist = envlist_create();
361fc0d96b4SBlue Swirl } else if (!strcmp(r, "U")) {
362fc0d96b4SBlue Swirl r = argv[optind++];
363b23a51dcSWarner Losh if (envlist_unsetenv(envlist, r) != 0) {
364fc0d96b4SBlue Swirl usage();
365b23a51dcSWarner Losh }
36684778508Sblueswir1 } else if (!strcmp(r, "s")) {
36784778508Sblueswir1 r = argv[optind++];
368312a0b1cSWarner Losh rv = qemu_strtoul(r, &r, 0, &target_dflssiz);
369312a0b1cSWarner Losh if (rv < 0 || target_dflssiz <= 0) {
37084778508Sblueswir1 usage();
371b23a51dcSWarner Losh }
372b23a51dcSWarner Losh if (*r == 'M') {
373312a0b1cSWarner Losh target_dflssiz *= 1024 * 1024;
374b23a51dcSWarner Losh } else if (*r == 'k' || *r == 'K') {
375312a0b1cSWarner Losh target_dflssiz *= 1024;
376312a0b1cSWarner Losh }
377312a0b1cSWarner Losh if (target_dflssiz > target_maxssiz) {
378312a0b1cSWarner Losh usage();
379b23a51dcSWarner Losh }
38084778508Sblueswir1 } else if (!strcmp(r, "L")) {
38184778508Sblueswir1 interp_prefix = argv[optind++];
38284778508Sblueswir1 } else if (!strcmp(r, "p")) {
38301e44980SRichard Henderson unsigned size, want = qemu_real_host_page_size();
38401e44980SRichard Henderson
38501e44980SRichard Henderson r = argv[optind++];
38601e44980SRichard Henderson if (qemu_strtoui(r, NULL, 10, &size) || size != want) {
38701e44980SRichard Henderson warn_report("Deprecated page size option cannot "
38801e44980SRichard Henderson "change host page size (%u)", want);
38984778508Sblueswir1 }
39084778508Sblueswir1 } else if (!strcmp(r, "g")) {
391fcedd920SAlex Bennée gdbstub = g_strdup(argv[optind++]);
39284778508Sblueswir1 } else if (!strcmp(r, "r")) {
39384778508Sblueswir1 qemu_uname_release = argv[optind++];
39484778508Sblueswir1 } else if (!strcmp(r, "cpu")) {
39584778508Sblueswir1 cpu_model = argv[optind++];
396c8057f95SPeter Maydell if (is_help_option(cpu_model)) {
397dfa47531SGavin Shan list_cpus();
3982d18e637Sblueswir1 exit(1);
39984778508Sblueswir1 }
4002fa5d9baSBlue Swirl } else if (!strcmp(r, "B")) {
40129aabb4fSWarner Losh rv = qemu_strtoul(argv[optind++], NULL, 0, &guest_base);
40229aabb4fSWarner Losh if (rv < 0) {
40329aabb4fSWarner Losh usage();
40429aabb4fSWarner Losh }
405e307c192SRichard Henderson have_guest_base = true;
40684778508Sblueswir1 } else if (!strcmp(r, "drop-ld-preload")) {
407fc0d96b4SBlue Swirl (void) envlist_unsetenv(envlist, "LD_PRELOAD");
40803ecf078SWarner Losh } else if (!strcmp(r, "seed")) {
40903ecf078SWarner Losh seed_optarg = optarg;
410c61a0d31SPhilippe Mathieu-Daudé } else if (!strcmp(r, "one-insn-per-tb")) {
4113cfb0456SPeter Maydell opt_one_insn_per_tb = true;
41284778508Sblueswir1 } else if (!strcmp(r, "strace")) {
41384778508Sblueswir1 do_strace = 1;
4146913e79cSLluís Vilanova } else if (!strcmp(r, "trace")) {
41592eecfffSPaolo Bonzini trace_opt_parse(optarg);
416b8012648SColin Percival } else if (!strcmp(r, "0")) {
417b8012648SColin Percival argv0 = argv[optind++];
4186913e79cSLluís Vilanova } else {
41984778508Sblueswir1 usage();
42084778508Sblueswir1 }
42184778508Sblueswir1 }
42284778508Sblueswir1
4238c45039fSRichard Henderson qemu_host_page_mask = -qemu_host_page_size;
4248c45039fSRichard Henderson
425c235d738SMatthew Fernandez /* init debug */
426905c78feSRichard Henderson {
427905c78feSRichard Henderson int mask = 0;
428c235d738SMatthew Fernandez if (log_mask) {
4294fde1ebaSPeter Maydell mask = qemu_str_to_log_mask(log_mask);
430c235d738SMatthew Fernandez if (!mask) {
43159a6fa6eSPeter Maydell qemu_print_log_usage(stdout);
432c235d738SMatthew Fernandez exit(1);
433c235d738SMatthew Fernandez }
434905c78feSRichard Henderson }
435905c78feSRichard Henderson qemu_set_log_filename_flags(log_file, mask, &error_fatal);
436c235d738SMatthew Fernandez }
437c235d738SMatthew Fernandez
4384b5dfd82SPeter Maydell if (optind >= argc) {
4394b5dfd82SPeter Maydell usage();
4404b5dfd82SPeter Maydell }
4414b5dfd82SPeter Maydell filename = argv[optind];
442b8012648SColin Percival if (argv0) {
443b8012648SColin Percival argv[optind] = argv0;
444b8012648SColin Percival }
4454b5dfd82SPeter Maydell
4466913e79cSLluís Vilanova if (!trace_init_backends()) {
4476913e79cSLluís Vilanova exit(1);
4486913e79cSLluís Vilanova }
44992eecfffSPaolo Bonzini trace_init_file();
4506913e79cSLluís Vilanova
45184778508Sblueswir1 /* Zero out regs */
45284778508Sblueswir1 memset(regs, 0, sizeof(struct target_pt_regs));
45384778508Sblueswir1
454d37853f9SWarner Losh /* Zero bsd params */
455d37853f9SWarner Losh memset(&bprm, 0, sizeof(bprm));
456d37853f9SWarner Losh
45784778508Sblueswir1 /* Zero out image_info */
45884778508Sblueswir1 memset(info, 0, sizeof(struct image_info));
45984778508Sblueswir1
46084778508Sblueswir1 /* Scan interp_prefix dir for replacement files. */
46184778508Sblueswir1 init_paths(interp_prefix);
46284778508Sblueswir1
46384778508Sblueswir1 if (cpu_model == NULL) {
464031fe7afSWarner Losh cpu_model = TARGET_DEFAULT_CPU_MODEL;
46584778508Sblueswir1 }
4662b5249b8SIgor Mammedov
467b86f59c7SClaudio Fontana cpu_type = parse_cpu_option(cpu_model);
468031fe7afSWarner Losh
4692b5249b8SIgor Mammedov /* init tcg before creating CPUs and to get qemu_host_page_size */
470940e43aaSClaudio Fontana {
4713cfb0456SPeter Maydell AccelState *accel = current_accel();
4723cfb0456SPeter Maydell AccelClass *ac = ACCEL_GET_CLASS(accel);
4732b5249b8SIgor Mammedov
474b86f59c7SClaudio Fontana accel_init_interfaces(ac);
4753cfb0456SPeter Maydell object_property_set_bool(OBJECT(accel), "one-insn-per-tb",
4763cfb0456SPeter Maydell opt_one_insn_per_tb, &error_abort);
47792242f34SClaudio Fontana ac->init_machine(NULL);
478940e43aaSClaudio Fontana }
4792278b939SIgor Mammedov cpu = cpu_create(cpu_type);
480b77af26eSRichard Henderson env = cpu_env(cpu);
481db6b81d4SAndreas Färber cpu_reset(cpu);
482db6b81d4SAndreas Färber thread_cpu = cpu;
48384778508Sblueswir1
48484778508Sblueswir1 if (getenv("QEMU_STRACE")) {
48584778508Sblueswir1 do_strace = 1;
48684778508Sblueswir1 }
48784778508Sblueswir1
488fc0d96b4SBlue Swirl target_environ = envlist_to_environ(envlist, NULL);
489fc0d96b4SBlue Swirl envlist_free(envlist);
49084778508Sblueswir1
49103ecf078SWarner Losh {
49203ecf078SWarner Losh Error *err = NULL;
49303ecf078SWarner Losh if (seed_optarg != NULL) {
49403ecf078SWarner Losh qemu_guest_random_seed_main(seed_optarg, &err);
49503ecf078SWarner Losh } else {
49603ecf078SWarner Losh qcrypto_init(&err);
49703ecf078SWarner Losh }
49803ecf078SWarner Losh if (err) {
49903ecf078SWarner Losh error_reportf_err(err, "cannot initialize crypto: ");
50003ecf078SWarner Losh exit(1);
50103ecf078SWarner Losh }
50203ecf078SWarner Losh }
50303ecf078SWarner Losh
5042fa5d9baSBlue Swirl /*
505fa79cde6SRichard Henderson * Now that page sizes are configured we can do
5062fa5d9baSBlue Swirl * proper page alignment for guest_base.
5072fa5d9baSBlue Swirl */
50828b61d49SRichard Henderson if (have_guest_base) {
50928b61d49SRichard Henderson if (guest_base & ~qemu_host_page_mask) {
51028b61d49SRichard Henderson error_report("Selected guest base not host page aligned");
51128b61d49SRichard Henderson exit(1);
51228b61d49SRichard Henderson }
51328b61d49SRichard Henderson }
51428b61d49SRichard Henderson
51528b61d49SRichard Henderson /*
51628b61d49SRichard Henderson * If reserving host virtual address space, do so now.
51728b61d49SRichard Henderson * Combined with '-B', ensure that the chosen range is free.
51828b61d49SRichard Henderson */
51928b61d49SRichard Henderson if (reserved_va) {
52028b61d49SRichard Henderson void *p;
52128b61d49SRichard Henderson
52228b61d49SRichard Henderson if (have_guest_base) {
52328b61d49SRichard Henderson p = mmap((void *)guest_base, reserved_va + 1, PROT_NONE,
52428b61d49SRichard Henderson MAP_ANON | MAP_PRIVATE | MAP_FIXED | MAP_EXCL, -1, 0);
52528b61d49SRichard Henderson } else {
52628b61d49SRichard Henderson p = mmap(NULL, reserved_va + 1, PROT_NONE,
52728b61d49SRichard Henderson MAP_ANON | MAP_PRIVATE, -1, 0);
52828b61d49SRichard Henderson }
52928b61d49SRichard Henderson if (p == MAP_FAILED) {
53028b61d49SRichard Henderson const char *err = strerror(errno);
53128b61d49SRichard Henderson char *sz = size_to_str(reserved_va + 1);
53228b61d49SRichard Henderson
53328b61d49SRichard Henderson if (have_guest_base) {
53428b61d49SRichard Henderson error_report("Cannot allocate %s bytes at -B %p for guest "
53528b61d49SRichard Henderson "address space: %s", sz, (void *)guest_base, err);
53628b61d49SRichard Henderson } else {
53728b61d49SRichard Henderson error_report("Cannot allocate %s bytes for guest "
53828b61d49SRichard Henderson "address space: %s", sz, err);
53928b61d49SRichard Henderson }
54028b61d49SRichard Henderson exit(1);
54128b61d49SRichard Henderson }
54228b61d49SRichard Henderson guest_base = (uintptr_t)p;
54328b61d49SRichard Henderson have_guest_base = true;
54428b61d49SRichard Henderson
54528b61d49SRichard Henderson /* Ensure that mmap_next_start is within range. */
54628b61d49SRichard Henderson if (reserved_va <= mmap_next_start) {
54728b61d49SRichard Henderson mmap_next_start = (reserved_va / 4 * 3)
54828b61d49SRichard Henderson & TARGET_PAGE_MASK & qemu_host_page_mask;
54928b61d49SRichard Henderson }
55028b61d49SRichard Henderson }
5512fa5d9baSBlue Swirl
552d37853f9SWarner Losh if (loader_exec(filename, argv + optind, target_environ, regs, info,
553d37853f9SWarner Losh &bprm) != 0) {
55484778508Sblueswir1 printf("Error loading %s\n", filename);
55584778508Sblueswir1 _exit(1);
55684778508Sblueswir1 }
55784778508Sblueswir1
55884778508Sblueswir1 for (wrk = target_environ; *wrk; wrk++) {
559ec45bbe5SSaurav Sachidanand g_free(*wrk);
56084778508Sblueswir1 }
56184778508Sblueswir1
562ec45bbe5SSaurav Sachidanand g_free(target_environ);
56384778508Sblueswir1
56413829020SPaolo Bonzini if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
56543b76161SRichard Henderson FILE *f = qemu_log_trylock();
56643b76161SRichard Henderson if (f) {
56743b76161SRichard Henderson fprintf(f, "guest_base %p\n", (void *)guest_base);
56843b76161SRichard Henderson fprintf(f, "page layout changed following binary load\n");
56943b76161SRichard Henderson page_dump(f);
57084778508Sblueswir1
57143b76161SRichard Henderson fprintf(f, "end_code 0x" TARGET_ABI_FMT_lx "\n",
57243b76161SRichard Henderson info->end_code);
57343b76161SRichard Henderson fprintf(f, "start_code 0x" TARGET_ABI_FMT_lx "\n",
57484778508Sblueswir1 info->start_code);
57543b76161SRichard Henderson fprintf(f, "start_data 0x" TARGET_ABI_FMT_lx "\n",
57684778508Sblueswir1 info->start_data);
57743b76161SRichard Henderson fprintf(f, "end_data 0x" TARGET_ABI_FMT_lx "\n",
57843b76161SRichard Henderson info->end_data);
57943b76161SRichard Henderson fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n",
58084778508Sblueswir1 info->start_stack);
58143b76161SRichard Henderson fprintf(f, "brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
58243b76161SRichard Henderson fprintf(f, "entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
58343b76161SRichard Henderson
58443b76161SRichard Henderson qemu_log_unlock(f);
58543b76161SRichard Henderson }
5862e77eac6Sblueswir1 }
58784778508Sblueswir1
588031fe7afSWarner Losh /* build Task State */
589031fe7afSWarner Losh ts = g_new0(TaskState, 1);
590031fe7afSWarner Losh init_task_state(ts);
591031fe7afSWarner Losh ts->info = info;
592031fe7afSWarner Losh ts->bprm = &bprm;
593031fe7afSWarner Losh cpu->opaque = ts;
594031fe7afSWarner Losh
59584778508Sblueswir1 target_set_brk(info->brk);
59684778508Sblueswir1 syscall_init();
59784778508Sblueswir1 signal_init();
59884778508Sblueswir1
59934bc8475SWarner Losh /*
60034bc8475SWarner Losh * Now that we've loaded the binary, GUEST_BASE is fixed. Delay
60134bc8475SWarner Losh * generating the prologue until now so that the prologue can take
60234bc8475SWarner Losh * the real value of GUEST_BASE into account.
60334bc8475SWarner Losh */
604935f75aeSRichard Henderson tcg_prologue_init();
6059002ec79SRichard Henderson
606031fe7afSWarner Losh target_cpu_init(env, regs);
60784778508Sblueswir1
608fcedd920SAlex Bennée if (gdbstub) {
609fcedd920SAlex Bennée gdbserver_start(gdbstub);
610f84e313eSGustavo Romero gdb_handlesig(cpu, 0, NULL, NULL, 0);
61184778508Sblueswir1 }
61278cfb07fSJuergen Lock cpu_loop(env);
61384778508Sblueswir1 /* never exits */
61484778508Sblueswir1 return 0;
61584778508Sblueswir1 }
616