]>
| Commit | Line | Data |
|---|---|---|
| 84778508 | 1 | /* |
| 4c0a4fe6 | 2 | * qemu bsd user main |
| 84778508 BS |
3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
| 4c0a4fe6 | 5 | * Copyright (c) 2013-14 Stacey Son |
| 84778508 BS |
6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify | |
| 8 | * it under the terms of the GNU General Public License as published by | |
| 9 | * the Free Software Foundation; either version 2 of the License, or | |
| 10 | * (at your option) any later version. | |
| 11 | * | |
| 12 | * This program is distributed in the hope that it will be useful, | |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 15 | * GNU General Public License for more details. | |
| 16 | * | |
| 17 | * You should have received a copy of the GNU General Public License | |
| 8167ee88 | 18 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 84778508 | 19 | */ |
| 14a48c1d | 20 | |
| 48e438a3 | 21 | #include "qemu/osdep.h" |
| 312a0b1c WL |
22 | #include <sys/resource.h> |
| 23 | #include <sys/sysctl.h> | |
| 24 | ||
| 49f95221 | 25 | #include "qemu/help-texts.h" |
| 66d26ddb | 26 | #include "qemu/units.h" |
| 940e43aa | 27 | #include "qemu/accel.h" |
| 8c1c230a | 28 | #include "qemu-version.h" |
| 84778508 BS |
29 | #include <machine/trap.h> |
| 30 | ||
| daa76aa4 | 31 | #include "qapi/error.h" |
| 84778508 | 32 | #include "qemu.h" |
| 6913e79c | 33 | #include "qemu/config-file.h" |
| f5852efa | 34 | #include "qemu/error-report.h" |
| f348b6d1 VB |
35 | #include "qemu/path.h" |
| 36 | #include "qemu/help_option.h" | |
| 0b8fa32f | 37 | #include "qemu/module.h" |
| 5b6828d1 | 38 | #include "qemu/plugin.h" |
| 16aa8eaa | 39 | #include "user/guest-base.h" |
| 970ae60e | 40 | #include "user/page-protection.h" |
| f7a7e7dd | 41 | #include "accel/accel-ops.h" |
| d7ec12f8 | 42 | #include "tcg/startup.h" |
| 1de7afc9 PB |
43 | #include "qemu/timer.h" |
| 44 | #include "qemu/envlist.h" | |
| 29aabb4f | 45 | #include "qemu/cutils.h" |
| 508127e2 | 46 | #include "exec/log.h" |
| 6913e79c | 47 | #include "trace/control.h" |
| 03ecf078 WL |
48 | #include "crypto/init.h" |
| 49 | #include "qemu/guest-random.h" | |
| d96bf49b | 50 | #include "gdbstub/user.h" |
| ba379542 | 51 | #include "exec/page-vary.h" |
| fc0d96b4 | 52 | |
| d1dc9ab3 | 53 | #include "host-os.h" |
| 031fe7af | 54 | #include "target_arch_cpu.h" |
| d1dc9ab3 | 55 | |
| 8c45039f RH |
56 | |
| 57 | /* | |
| 58 | * TODO: Remove these and rely only on qemu_real_host_page_size(). | |
| 59 | */ | |
| 60 | uintptr_t qemu_host_page_size; | |
| 61 | intptr_t qemu_host_page_mask; | |
| 62 | ||
| 3cfb0456 | 63 | static bool opt_one_insn_per_tb; |
| 89974523 | 64 | static unsigned long opt_tb_size; |
| 5ca870b9 | 65 | uintptr_t guest_base; |
| e307c192 | 66 | bool have_guest_base; |
| be04f210 WL |
67 | /* |
| 68 | * When running 32-on-64 we should make sure we can fit all of the possible | |
| 69 | * guest address space into a contiguous chunk of virtual host memory. | |
| 70 | * | |
| 71 | * This way we will never overlap with our own libraries or binaries or stack | |
| 72 | * or anything else that QEMU maps. | |
| 73 | * | |
| 74 | * Many cpus reserve the high bit (or more than one for some 64-bit cpus) | |
| 75 | * of the address for the kernel. Some cpus rely on this and user space | |
| 76 | * uses the high bit(s) for pointer tagging and the like. For them, we | |
| 77 | * must preserve the expected address space. | |
| 78 | */ | |
| 79 | #ifndef MAX_RESERVED_VA | |
| 80 | # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS | |
| 81 | # if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \ | |
| 82 | (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32)) | |
| cb4c2590 | 83 | # define MAX_RESERVED_VA(CPU) 0xfffffffful |
| be04f210 | 84 | # else |
| cb4c2590 | 85 | # define MAX_RESERVED_VA(CPU) ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1) |
| be04f210 WL |
86 | # endif |
| 87 | # else | |
| cb4c2590 | 88 | # define MAX_RESERVED_VA(CPU) 0 |
| be04f210 WL |
89 | # endif |
| 90 | #endif | |
| 91 | ||
| d6ef40bf | 92 | unsigned long reserved_va; |
| 30da4760 | 93 | unsigned long guest_addr_max; |
| 1b530a6d | 94 | |
| 86327290 | 95 | const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX; |
| fccae322 | 96 | const char *qemu_uname_release; |
| 84778508 | 97 | |
| 312a0b1c WL |
98 | unsigned long target_maxtsiz = TARGET_MAXTSIZ; /* max text size */ |
| 99 | unsigned long target_dfldsiz = TARGET_DFLDSIZ; /* initial data size limit */ | |
| 100 | unsigned long target_maxdsiz = TARGET_MAXDSIZ; /* max data size */ | |
| 101 | unsigned long target_dflssiz = TARGET_DFLSSIZ; /* initial data size limit */ | |
| 102 | unsigned long target_maxssiz = TARGET_MAXSSIZ; /* max stack size */ | |
| 103 | unsigned long target_sgrowsiz = TARGET_SGROWSIZ; /* amount to grow stack */ | |
| 84778508 | 104 | |
| 63cca106 | 105 | /* Helper routines for implementing atomic operations. */ |
| 9399f095 | 106 | |
| 9399f095 BS |
107 | void fork_start(void) |
| 108 | { | |
| 63cca106 | 109 | start_exclusive(); |
| 63cca106 | 110 | mmap_fork_start(); |
| 5b6828d1 JC |
111 | cpu_list_lock(); |
| 112 | qemu_plugin_user_prefork_lock(); | |
| 3d6ed98d | 113 | gdbserver_fork_start(); |
| 9399f095 BS |
114 | } |
| 115 | ||
| 4edc98fc | 116 | void fork_end(pid_t pid) |
| 9399f095 | 117 | { |
| 4edc98fc IL |
118 | bool child = pid == 0; |
| 119 | ||
| 5b6828d1 JC |
120 | qemu_plugin_user_postfork(child); |
| 121 | mmap_fork_end(child); | |
| 9399f095 | 122 | if (child) { |
| 63cca106 WL |
123 | CPUState *cpu, *next_cpu; |
| 124 | /* | |
| 5b6828d1 JC |
125 | * Child processes created by fork() only have a single thread. |
| 126 | * Discard information about the parent threads. | |
| 63cca106 WL |
127 | */ |
| 128 | CPU_FOREACH_SAFE(cpu, next_cpu) { | |
| 129 | if (cpu != thread_cpu) { | |
| 3c55dd58 | 130 | QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node); |
| 63cca106 WL |
131 | } |
| 132 | } | |
| 63cca106 | 133 | qemu_init_cpu_list(); |
| d4e1369a | 134 | get_task_state(thread_cpu)->ts_tid = qemu_get_thread_id(); |
| 63cca106 | 135 | } else { |
| 63cca106 | 136 | cpu_list_unlock(); |
| 9399f095 | 137 | } |
| 5b6828d1 JC |
138 | gdbserver_fork_end(thread_cpu, pid); |
| 139 | /* | |
| 140 | * qemu_init_cpu_list() reinitialized the child exclusive state, but we | |
| 141 | * also need to keep current_cpu consistent, so call end_exclusive() for | |
| 142 | * both child and parent. | |
| 143 | */ | |
| 144 | end_exclusive(); | |
| 9399f095 BS |
145 | } |
| 146 | ||
| 031fe7af | 147 | void cpu_loop(CPUArchState *env) |
| 31fc12df | 148 | { |
| 031fe7af | 149 | target_cpu_loop(env); |
| 31fc12df BS |
150 | } |
| 151 | ||
| 84778508 BS |
152 | static void usage(void) |
| 153 | { | |
| 7e563bfb | 154 | printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION |
| 0781dd6e | 155 | "\n" QEMU_COPYRIGHT "\n" |
| 2e59915d | 156 | "usage: qemu-" TARGET_NAME " [options] program [arguments...]\n" |
| 84778508 BS |
157 | "BSD CPU emulator (compiled for %s emulation)\n" |
| 158 | "\n" | |
| 159 | "Standard options:\n" | |
| 160 | "-h print this help\n" | |
| 161 | "-g port wait gdb connection to port\n" | |
| 162 | "-L path set the elf interpreter prefix (default=%s)\n" | |
| 163 | "-s size set the stack size in bytes (default=%ld)\n" | |
| c8057f95 | 164 | "-cpu model select CPU (-cpu help for list)\n" |
| 84778508 | 165 | "-drop-ld-preload drop LD_PRELOAD for target process\n" |
| fc0d96b4 BS |
166 | "-E var=value sets/modifies targets environment variable(s)\n" |
| 167 | "-U var unsets targets environment variable(s)\n" | |
| 2fa5d9ba | 168 | "-B address set guest_base address to address\n" |
| 84778508 BS |
169 | "\n" |
| 170 | "Debug options:\n" | |
| 989b697d PM |
171 | "-d item1[,...] enable logging of specified items\n" |
| 172 | " (use '-d help' for a list of log items)\n" | |
| 173 | "-D logfile write logs to 'logfile' (default stderr)\n" | |
| 060e0cd7 | 174 | "-one-insn-per-tb run with one guest instruction per emulated TB\n" |
| 89974523 | 175 | "-tb-size size TCG translation block cache size\n" |
| 989b697d | 176 | "-strace log system calls\n" |
| 6913e79c LV |
177 | "-trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
| 178 | " specify tracing options\n" | |
| 9b23bf50 PB |
179 | #ifdef CONFIG_PLUGIN |
| 180 | "-plugin [file=]<file>[,<argname>=<argvalue>]\n" | |
| 181 | #endif | |
| 84778508 BS |
182 | "\n" |
| 183 | "Environment variables:\n" | |
| 184 | "QEMU_STRACE Print system calls and arguments similar to the\n" | |
| 185 | " 'strace' program. Enable by setting to any value.\n" | |
| fc0d96b4 BS |
186 | "You can use -E and -U options to set/unset environment variables\n" |
| 187 | "for target process. It is possible to provide several variables\n" | |
| 188 | "by repeating the option. For example:\n" | |
| 189 | " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n" | |
| 190 | "Note that if you provide several changes to single variable\n" | |
| 191 | "last change will stay in effect.\n" | |
| f5048cb7 EB |
192 | "\n" |
| 193 | QEMU_HELP_BOTTOM "\n" | |
| 84778508 | 194 | , |
| 2e59915d | 195 | TARGET_NAME, |
| 84778508 | 196 | interp_prefix, |
| 312a0b1c | 197 | target_dflssiz); |
| 2d18e637 | 198 | exit(1); |
| 84778508 BS |
199 | } |
| 200 | ||
| d42df502 | 201 | __thread CPUState *thread_cpu; |
| 84778508 | 202 | |
| 653ccec2 WL |
203 | void stop_all_tasks(void) |
| 204 | { | |
| 205 | /* | |
| 206 | * We trust when using NPTL (pthreads) start_exclusive() handles thread | |
| 207 | * stopping correctly. | |
| 208 | */ | |
| 209 | start_exclusive(); | |
| 210 | } | |
| 211 | ||
| 48f59211 EM |
212 | bool qemu_cpu_is_self(CPUState *cpu) |
| 213 | { | |
| 214 | return thread_cpu == cpu; | |
| 215 | } | |
| 216 | ||
| 84778508 | 217 | /* Assumes contents are already zeroed. */ |
| b46d4ad7 | 218 | static void init_task_state(TaskState *ts) |
| 84778508 | 219 | { |
| 46f4f76d WL |
220 | ts->sigaltstack_used = (struct target_sigaltstack) { |
| 221 | .ss_sp = 0, | |
| 222 | .ss_size = 0, | |
| 223 | .ss_flags = TARGET_SS_DISABLE, | |
| 224 | }; | |
| 84778508 BS |
225 | } |
| 226 | ||
| 9b23bf50 PB |
227 | static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins); |
| 228 | ||
| f0f7f9dc WL |
229 | void gemu_log(const char *fmt, ...) |
| 230 | { | |
| 231 | va_list ap; | |
| 232 | ||
| 233 | va_start(ap, fmt); | |
| 234 | vfprintf(stderr, fmt, ap); | |
| 235 | va_end(ap); | |
| 236 | } | |
| 237 | ||
| 312a0b1c WL |
238 | static void |
| 239 | adjust_ssize(void) | |
| 240 | { | |
| 241 | struct rlimit rl; | |
| 242 | ||
| 243 | if (getrlimit(RLIMIT_STACK, &rl) != 0) { | |
| 244 | return; | |
| 245 | } | |
| 246 | ||
| 247 | target_maxssiz = MIN(target_maxssiz, rl.rlim_max); | |
| 248 | target_dflssiz = MIN(MAX(target_dflssiz, rl.rlim_cur), target_maxssiz); | |
| 249 | ||
| 250 | rl.rlim_max = target_maxssiz; | |
| 251 | rl.rlim_cur = target_dflssiz; | |
| 252 | setrlimit(RLIMIT_STACK, &rl); | |
| 253 | } | |
| 254 | ||
| 84778508 BS |
255 | int main(int argc, char **argv) |
| 256 | { | |
| 257 | const char *filename; | |
| 258 | const char *cpu_model; | |
| 2278b939 | 259 | const char *cpu_type; |
| 989b697d | 260 | const char *log_file = NULL; |
| c235d738 | 261 | const char *log_mask = NULL; |
| 03ecf078 | 262 | const char *seed_optarg = NULL; |
| 84778508 BS |
263 | struct target_pt_regs regs1, *regs = ®s1; |
| 264 | struct image_info info1, *info = &info1; | |
| d37853f9 | 265 | struct bsd_binprm bprm; |
| 031fe7af | 266 | TaskState *ts; |
| 9349b4f9 | 267 | CPUArchState *env; |
| db6b81d4 | 268 | CPUState *cpu; |
| 29aabb4f | 269 | int optind, rv; |
| 84778508 | 270 | const char *r; |
| fcedd920 | 271 | const char *gdbstub = NULL; |
| fc0d96b4 BS |
272 | char **target_environ, **wrk; |
| 273 | envlist_t *envlist = NULL; | |
| b8012648 | 274 | char *argv0 = NULL; |
| ba379542 | 275 | int host_page_size; |
| cb4c2590 | 276 | unsigned long max_reserved_va; |
| 84778508 | 277 | |
| 312a0b1c WL |
278 | adjust_ssize(); |
| 279 | ||
| b23a51dc | 280 | if (argc <= 1) { |
| 84778508 | 281 | usage(); |
| b23a51dc | 282 | } |
| 84778508 | 283 | |
| 01a298a5 | 284 | |
| f5852efa | 285 | error_init(argv[0]); |
| fe4db84d | 286 | module_call_init(MODULE_INIT_TRACE); |
| 267f685b | 287 | qemu_init_cpu_list(); |
| ce008c1f AF |
288 | module_call_init(MODULE_INIT_QOM); |
| 289 | ||
| ec45bbe5 | 290 | envlist = envlist_create(); |
| fc0d96b4 | 291 | |
| 7f750efc AS |
292 | /* |
| 293 | * add current environment into the list | |
| 294 | * envlist_setenv adds to the front of the list; to preserve environ | |
| 295 | * order add from back to front | |
| 296 | */ | |
| fc0d96b4 | 297 | for (wrk = environ; *wrk != NULL; wrk++) { |
| 7f750efc AS |
298 | continue; |
| 299 | } | |
| 300 | while (wrk != environ) { | |
| 301 | wrk--; | |
| fc0d96b4 BS |
302 | (void) envlist_setenv(envlist, *wrk); |
| 303 | } | |
| 304 | ||
| 8c45039f RH |
305 | qemu_host_page_size = getpagesize(); |
| 306 | qemu_host_page_size = MAX(qemu_host_page_size, TARGET_PAGE_SIZE); | |
| 307 | ||
| 84778508 | 308 | cpu_model = NULL; |
| 0c62de2f | 309 | |
| 6913e79c | 310 | qemu_add_opts(&qemu_trace_opts); |
| 9b23bf50 | 311 | qemu_plugin_add_opts(); |
| 6913e79c | 312 | |
| 84778508 | 313 | optind = 1; |
| 6913e79c | 314 | for (;;) { |
| b23a51dc | 315 | if (optind >= argc) { |
| 84778508 | 316 | break; |
| b23a51dc | 317 | } |
| 84778508 | 318 | r = argv[optind]; |
| b23a51dc | 319 | if (r[0] != '-') { |
| 84778508 | 320 | break; |
| b23a51dc | 321 | } |
| 84778508 BS |
322 | optind++; |
| 323 | r++; | |
| 324 | if (!strcmp(r, "-")) { | |
| 325 | break; | |
| 326 | } else if (!strcmp(r, "d")) { | |
| c235d738 | 327 | if (optind >= argc) { |
| 84778508 | 328 | break; |
| 84778508 | 329 | } |
| c235d738 MF |
330 | log_mask = argv[optind++]; |
| 331 | } else if (!strcmp(r, "D")) { | |
| 332 | if (optind >= argc) { | |
| 333 | break; | |
| 334 | } | |
| 335 | log_file = argv[optind++]; | |
| fc0d96b4 BS |
336 | } else if (!strcmp(r, "E")) { |
| 337 | r = argv[optind++]; | |
| b23a51dc | 338 | if (envlist_setenv(envlist, r) != 0) { |
| fc0d96b4 | 339 | usage(); |
| b23a51dc | 340 | } |
| f66724c9 SW |
341 | } else if (!strcmp(r, "ignore-environment")) { |
| 342 | envlist_free(envlist); | |
| ec45bbe5 | 343 | envlist = envlist_create(); |
| fc0d96b4 BS |
344 | } else if (!strcmp(r, "U")) { |
| 345 | r = argv[optind++]; | |
| b23a51dc | 346 | if (envlist_unsetenv(envlist, r) != 0) { |
| fc0d96b4 | 347 | usage(); |
| b23a51dc | 348 | } |
| 84778508 BS |
349 | } else if (!strcmp(r, "s")) { |
| 350 | r = argv[optind++]; | |
| 312a0b1c WL |
351 | rv = qemu_strtoul(r, &r, 0, &target_dflssiz); |
| 352 | if (rv < 0 || target_dflssiz <= 0) { | |
| 84778508 | 353 | usage(); |
| b23a51dc WL |
354 | } |
| 355 | if (*r == 'M') { | |
| 312a0b1c | 356 | target_dflssiz *= 1024 * 1024; |
| b23a51dc | 357 | } else if (*r == 'k' || *r == 'K') { |
| 312a0b1c WL |
358 | target_dflssiz *= 1024; |
| 359 | } | |
| 360 | if (target_dflssiz > target_maxssiz) { | |
| 361 | usage(); | |
| b23a51dc | 362 | } |
| 84778508 BS |
363 | } else if (!strcmp(r, "L")) { |
| 364 | interp_prefix = argv[optind++]; | |
| 84778508 | 365 | } else if (!strcmp(r, "g")) { |
| fcedd920 | 366 | gdbstub = g_strdup(argv[optind++]); |
| 84778508 BS |
367 | } else if (!strcmp(r, "r")) { |
| 368 | qemu_uname_release = argv[optind++]; | |
| 369 | } else if (!strcmp(r, "cpu")) { | |
| 370 | cpu_model = argv[optind++]; | |
| c8057f95 | 371 | if (is_help_option(cpu_model)) { |
| dfa47531 | 372 | list_cpus(); |
| 2d18e637 | 373 | exit(1); |
| 84778508 | 374 | } |
| 2fa5d9ba | 375 | } else if (!strcmp(r, "B")) { |
| 29aabb4f WL |
376 | rv = qemu_strtoul(argv[optind++], NULL, 0, &guest_base); |
| 377 | if (rv < 0) { | |
| 378 | usage(); | |
| 379 | } | |
| d60c3b93 | 380 | have_guest_base = true; |
| 84778508 | 381 | } else if (!strcmp(r, "drop-ld-preload")) { |
| fc0d96b4 | 382 | (void) envlist_unsetenv(envlist, "LD_PRELOAD"); |
| 03ecf078 WL |
383 | } else if (!strcmp(r, "seed")) { |
| 384 | seed_optarg = optarg; | |
| c61a0d31 | 385 | } else if (!strcmp(r, "one-insn-per-tb")) { |
| 3cfb0456 | 386 | opt_one_insn_per_tb = true; |
| 89974523 IL |
387 | } else if (!strcmp(r, "tb-size")) { |
| 388 | r = argv[optind++]; | |
| 389 | if (qemu_strtoul(r, NULL, 0, &opt_tb_size)) { | |
| 390 | usage(); | |
| 391 | } | |
| 84778508 BS |
392 | } else if (!strcmp(r, "strace")) { |
| 393 | do_strace = 1; | |
| 6913e79c | 394 | } else if (!strcmp(r, "trace")) { |
| 92eecfff | 395 | trace_opt_parse(optarg); |
| 9b23bf50 PB |
396 | #ifdef CONFIG_PLUGIN |
| 397 | } else if (!strcmp(r, "plugin")) { | |
| 398 | r = argv[optind++]; | |
| 399 | qemu_plugin_opt_parse(r, &plugins); | |
| 400 | #endif | |
| b8012648 CP |
401 | } else if (!strcmp(r, "0")) { |
| 402 | argv0 = argv[optind++]; | |
| 6913e79c | 403 | } else { |
| 84778508 BS |
404 | usage(); |
| 405 | } | |
| 406 | } | |
| 84778508 | 407 | |
| 8c45039f RH |
408 | qemu_host_page_mask = -qemu_host_page_size; |
| 409 | ||
| c235d738 | 410 | /* init debug */ |
| 905c78fe RH |
411 | { |
| 412 | int mask = 0; | |
| 413 | if (log_mask) { | |
| 414 | mask = qemu_str_to_log_mask(log_mask); | |
| 415 | if (!mask) { | |
| 416 | qemu_print_log_usage(stdout); | |
| 417 | exit(1); | |
| 418 | } | |
| c235d738 | 419 | } |
| 905c78fe | 420 | qemu_set_log_filename_flags(log_file, mask, &error_fatal); |
| c235d738 MF |
421 | } |
| 422 | ||
| 4b5dfd82 PM |
423 | if (optind >= argc) { |
| 424 | usage(); | |
| 425 | } | |
| 426 | filename = argv[optind]; | |
| b8012648 CP |
427 | if (argv0) { |
| 428 | argv[optind] = argv0; | |
| 429 | } | |
| 4b5dfd82 | 430 | |
| 6913e79c LV |
431 | if (!trace_init_backends()) { |
| 432 | exit(1); | |
| 433 | } | |
| 92eecfff | 434 | trace_init_file(); |
| 9b23bf50 | 435 | qemu_plugin_load_list(&plugins, &error_fatal); |
| 6913e79c | 436 | |
| 84778508 BS |
437 | /* Zero out regs */ |
| 438 | memset(regs, 0, sizeof(struct target_pt_regs)); | |
| 439 | ||
| d37853f9 WL |
440 | /* Zero bsd params */ |
| 441 | memset(&bprm, 0, sizeof(bprm)); | |
| 442 | ||
| 84778508 BS |
443 | /* Zero out image_info */ |
| 444 | memset(info, 0, sizeof(struct image_info)); | |
| 445 | ||
| 446 | /* Scan interp_prefix dir for replacement files. */ | |
| 447 | init_paths(interp_prefix); | |
| 448 | ||
| 449 | if (cpu_model == NULL) { | |
| 031fe7af | 450 | cpu_model = TARGET_DEFAULT_CPU_MODEL; |
| 84778508 | 451 | } |
| 2b5249b8 | 452 | |
| b86f59c7 | 453 | cpu_type = parse_cpu_option(cpu_model); |
| 031fe7af | 454 | |
| 2b5249b8 | 455 | /* init tcg before creating CPUs and to get qemu_host_page_size */ |
| 940e43aa | 456 | { |
| 3cfb0456 PM |
457 | AccelState *accel = current_accel(); |
| 458 | AccelClass *ac = ACCEL_GET_CLASS(accel); | |
| 2b5249b8 | 459 | |
| b86f59c7 | 460 | accel_init_interfaces(ac); |
| 3cfb0456 PM |
461 | object_property_set_bool(OBJECT(accel), "one-insn-per-tb", |
| 462 | opt_one_insn_per_tb, &error_abort); | |
| 89974523 IL |
463 | object_property_set_int(OBJECT(accel), "tb-size", |
| 464 | opt_tb_size, &error_abort); | |
| 51e18961 | 465 | ac->init_machine(accel, NULL); |
| 940e43aa | 466 | } |
| ba379542 WL |
467 | |
| 468 | /* | |
| 469 | * Finalize page size before creating CPUs. | |
| 470 | * This will do nothing if !TARGET_PAGE_BITS_VARY. | |
| 471 | * The most efficient setting is to match the host. | |
| 472 | */ | |
| 473 | host_page_size = qemu_real_host_page_size(); | |
| 474 | set_preferred_target_page_bits(ctz32(host_page_size)); | |
| 475 | finalize_target_page_bits(); | |
| 476 | ||
| 2278b939 | 477 | cpu = cpu_create(cpu_type); |
| b77af26e | 478 | env = cpu_env(cpu); |
| db6b81d4 | 479 | cpu_reset(cpu); |
| db6b81d4 | 480 | thread_cpu = cpu; |
| 84778508 | 481 | |
| cb4c2590 WL |
482 | /* |
| 483 | * Reserving too much vm space via mmap can run into problems with rlimits, | |
| 484 | * oom due to page table creation, etc. We will still try it, if directed | |
| 485 | * by the command-line option, but not by default. Unless we're running a | |
| 486 | * target address space of 32 or fewer bits on a host with 64 bits. | |
| 487 | */ | |
| 488 | max_reserved_va = MAX_RESERVED_VA(cpu); | |
| 489 | if (reserved_va != 0) { | |
| 490 | if ((reserved_va + 1) % host_page_size) { | |
| 491 | char *s = size_to_str(host_page_size); | |
| 492 | fprintf(stderr, "Reserved virtual address not aligned mod %s\n", s); | |
| 493 | g_free(s); | |
| 494 | exit(EXIT_FAILURE); | |
| 495 | } | |
| 496 | if (max_reserved_va && reserved_va > max_reserved_va) { | |
| 497 | fprintf(stderr, "Reserved virtual address too big\n"); | |
| 498 | exit(EXIT_FAILURE); | |
| 499 | } | |
| 500 | } else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) { | |
| 501 | /* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */ | |
| 502 | reserved_va = max_reserved_va; | |
| 503 | } | |
| 30da4760 RH |
504 | if (reserved_va != 0) { |
| 505 | guest_addr_max = reserved_va; | |
| 506 | } else if (MIN(TARGET_VIRT_ADDR_SPACE_BITS, TARGET_ABI_BITS) <= 32) { | |
| 507 | guest_addr_max = UINT32_MAX; | |
| 508 | } else { | |
| 509 | guest_addr_max = ~0ul; | |
| 510 | } | |
| cb4c2590 | 511 | |
| 84778508 BS |
512 | if (getenv("QEMU_STRACE")) { |
| 513 | do_strace = 1; | |
| 514 | } | |
| 515 | ||
| fc0d96b4 BS |
516 | target_environ = envlist_to_environ(envlist, NULL); |
| 517 | envlist_free(envlist); | |
| 518 | ||
| 03ecf078 WL |
519 | { |
| 520 | Error *err = NULL; | |
| 521 | if (seed_optarg != NULL) { | |
| 522 | qemu_guest_random_seed_main(seed_optarg, &err); | |
| 523 | } else { | |
| 524 | qcrypto_init(&err); | |
| 525 | } | |
| 526 | if (err) { | |
| 527 | error_reportf_err(err, "cannot initialize crypto: "); | |
| 528 | exit(1); | |
| 529 | } | |
| 530 | } | |
| 531 | ||
| 2fa5d9ba | 532 | /* |
| fa79cde6 | 533 | * Now that page sizes are configured we can do |
| 2fa5d9ba BS |
534 | * proper page alignment for guest_base. |
| 535 | */ | |
| 28b61d49 RH |
536 | if (have_guest_base) { |
| 537 | if (guest_base & ~qemu_host_page_mask) { | |
| 538 | error_report("Selected guest base not host page aligned"); | |
| 539 | exit(1); | |
| 540 | } | |
| 541 | } | |
| 542 | ||
| 543 | /* | |
| 544 | * If reserving host virtual address space, do so now. | |
| 545 | * Combined with '-B', ensure that the chosen range is free. | |
| 546 | */ | |
| 547 | if (reserved_va) { | |
| 548 | void *p; | |
| 549 | ||
| 550 | if (have_guest_base) { | |
| 551 | p = mmap((void *)guest_base, reserved_va + 1, PROT_NONE, | |
| 552 | MAP_ANON | MAP_PRIVATE | MAP_FIXED | MAP_EXCL, -1, 0); | |
| 553 | } else { | |
| 554 | p = mmap(NULL, reserved_va + 1, PROT_NONE, | |
| 555 | MAP_ANON | MAP_PRIVATE, -1, 0); | |
| 556 | } | |
| 557 | if (p == MAP_FAILED) { | |
| 558 | const char *err = strerror(errno); | |
| 559 | char *sz = size_to_str(reserved_va + 1); | |
| 560 | ||
| 561 | if (have_guest_base) { | |
| 562 | error_report("Cannot allocate %s bytes at -B %p for guest " | |
| 563 | "address space: %s", sz, (void *)guest_base, err); | |
| 564 | } else { | |
| 565 | error_report("Cannot allocate %s bytes for guest " | |
| 566 | "address space: %s", sz, err); | |
| 567 | } | |
| 568 | exit(1); | |
| 569 | } | |
| 570 | guest_base = (uintptr_t)p; | |
| 571 | have_guest_base = true; | |
| 572 | ||
| 573 | /* Ensure that mmap_next_start is within range. */ | |
| 574 | if (reserved_va <= mmap_next_start) { | |
| 575 | mmap_next_start = (reserved_va / 4 * 3) | |
| 576 | & TARGET_PAGE_MASK & qemu_host_page_mask; | |
| 577 | } | |
| 578 | } | |
| 2fa5d9ba | 579 | |
| d37853f9 WL |
580 | if (loader_exec(filename, argv + optind, target_environ, regs, info, |
| 581 | &bprm) != 0) { | |
| 84778508 BS |
582 | printf("Error loading %s\n", filename); |
| 583 | _exit(1); | |
| 584 | } | |
| 585 | ||
| 586 | for (wrk = target_environ; *wrk; wrk++) { | |
| ec45bbe5 | 587 | g_free(*wrk); |
| 84778508 BS |
588 | } |
| 589 | ||
| ec45bbe5 | 590 | g_free(target_environ); |
| 84778508 | 591 | |
| 13829020 | 592 | if (qemu_loglevel_mask(CPU_LOG_PAGE)) { |
| 43b76161 RH |
593 | FILE *f = qemu_log_trylock(); |
| 594 | if (f) { | |
| 595 | fprintf(f, "guest_base %p\n", (void *)guest_base); | |
| 596 | fprintf(f, "page layout changed following binary load\n"); | |
| 597 | page_dump(f); | |
| 598 | ||
| 43b76161 RH |
599 | fprintf(f, "end_code 0x" TARGET_ABI_FMT_lx "\n", |
| 600 | info->end_code); | |
| 601 | fprintf(f, "start_code 0x" TARGET_ABI_FMT_lx "\n", | |
| 602 | info->start_code); | |
| 603 | fprintf(f, "start_data 0x" TARGET_ABI_FMT_lx "\n", | |
| 604 | info->start_data); | |
| 605 | fprintf(f, "end_data 0x" TARGET_ABI_FMT_lx "\n", | |
| 606 | info->end_data); | |
| 607 | fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n", | |
| 608 | info->start_stack); | |
| 609 | fprintf(f, "brk 0x" TARGET_ABI_FMT_lx "\n", info->brk); | |
| 610 | fprintf(f, "entry 0x" TARGET_ABI_FMT_lx "\n", info->entry); | |
| 611 | ||
| 612 | qemu_log_unlock(f); | |
| 613 | } | |
| 2e77eac6 | 614 | } |
| 84778508 | 615 | |
| 031fe7af WL |
616 | /* build Task State */ |
| 617 | ts = g_new0(TaskState, 1); | |
| 618 | init_task_state(ts); | |
| 619 | ts->info = info; | |
| 620 | ts->bprm = &bprm; | |
| 52a523af | 621 | ts->ts_tid = qemu_get_thread_id(); |
| 031fe7af WL |
622 | cpu->opaque = ts; |
| 623 | ||
| 84778508 BS |
624 | target_set_brk(info->brk); |
| 625 | syscall_init(); | |
| 626 | signal_init(); | |
| 627 | ||
| 34bc8475 WL |
628 | /* |
| 629 | * Now that we've loaded the binary, GUEST_BASE is fixed. Delay | |
| 630 | * generating the prologue until now so that the prologue can take | |
| 631 | * the real value of GUEST_BASE into account. | |
| 632 | */ | |
| 935f75ae | 633 | tcg_prologue_init(); |
| 9002ec79 | 634 | |
| 031fe7af | 635 | target_cpu_init(env, regs); |
| 84778508 | 636 | |
| fcedd920 | 637 | if (gdbstub) { |
| c0e6b8b7 | 638 | gdbserver_start(gdbstub, &error_fatal); |
| 84778508 | 639 | } |
| 78cfb07f | 640 | cpu_loop(env); |
| 84778508 BS |
641 | /* never exits */ |
| 642 | return 0; | |
| 643 | } |