blob: c9470eeccfc55c452d361f8df3bc3331f7723ea5 [file] [log] [blame]
bellard31e31b82003-02-18 22:55:36 +00001/*
bellard93ac68b2003-09-30 20:57:29 +00002 * qemu user main
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
bellard31e31b82003-02-18 22:55:36 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard31e31b82003-02-18 22:55:36 +000018 */
Markus Armbruster14a48c12019-05-23 16:35:05 +020019
Peter Maydelld39594e2016-01-26 18:17:02 +000020#include "qemu/osdep.h"
Marc-André Lureau49f95222022-04-20 17:25:49 +040021#include "qemu/help-texts.h"
Philippe Mathieu-Daudéb52713c2018-06-25 09:42:37 -030022#include "qemu/units.h"
Claudio Fontana940e43a2021-02-04 17:39:24 +010023#include "qemu/accel.h"
Fam Zheng67a1de02016-06-01 17:44:21 +080024#include "qemu-version.h"
Mika Westerbergedf8e2a2009-04-07 09:57:11 +030025#include <sys/syscall.h>
Richard Henderson703e0e82010-03-19 14:21:13 -070026#include <sys/resource.h>
Alex Bennéeee947432020-05-13 18:51:28 +010027#include <sys/shm.h>
Laurent Vivier6e1c0d72021-02-22 11:50:04 +010028#include <linux/binfmts.h>
bellard31e31b82003-02-18 22:55:36 +000029
Markus Armbrusterdaa76aa2016-06-15 19:27:16 +020030#include "qapi/error.h"
bellard3ef693a2003-03-23 20:17:16 +000031#include "qemu.h"
Peter Maydell3b249d22021-09-08 16:44:03 +010032#include "user-internals.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020033#include "qemu/path.h"
Markus Armbrusterdc5e9ac2019-08-12 07:23:49 +020034#include "qemu/queue.h"
Lluís Vilanova6533dd62016-07-15 19:08:38 +020035#include "qemu/config-file.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020036#include "qemu/cutils.h"
Christophe Fergeauf5852ef2019-01-31 17:46:14 +010037#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020038#include "qemu/help_option.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020039#include "qemu/module.h"
Lluís Vilanovaf308f642017-07-24 12:41:51 +030040#include "qemu/plugin.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010041#include "exec/exec-all.h"
Peter Maydell85b4fa02021-09-08 16:44:04 +010042#include "exec/gdbstub.h"
Alex Bennéed96bf492023-03-02 18:57:47 -080043#include "gdbstub/user.h"
Richard Hendersond7ec12f2023-09-29 19:54:54 -070044#include "tcg/startup.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010045#include "qemu/timer.h"
46#include "qemu/envlist.h"
Richard Henderson5ebdd772019-03-14 13:10:53 -070047#include "qemu/guest-random.h"
Paul Brookd8fd2952012-03-30 18:02:50 +010048#include "elf.h"
Lluís Vilanova6533dd62016-07-15 19:08:38 +020049#include "trace/control.h"
Laurent Vivier542ca432018-02-20 18:33:04 +010050#include "target_elf.h"
Laurent Viviercd71c082018-04-11 20:56:33 +020051#include "cpu_loop-common.h"
Richard Hendersona573e9b2019-03-13 13:53:22 -070052#include "crypto/init.h"
Owen Andersonc0933642021-07-01 22:12:55 +000053#include "fd-trans.h"
Peter Maydell2113aed2021-09-08 16:43:59 +010054#include "signal-common.h"
Peter Maydell3ad0a762021-09-08 16:44:00 +010055#include "loader.h"
Peter Maydell5423e6d2021-09-08 16:44:01 +010056#include "user-mmap.h"
Ilya Leoshkevich5584e2d2023-01-12 16:20:13 +010057#include "accel/tcg/perf.h"
aurel3204a6dfe2009-01-30 19:59:17 +000058
Richard Hendersone4a4aaa2022-05-01 17:21:00 -070059#ifdef CONFIG_SEMIHOSTING
60#include "semihosting/semihost.h"
61#endif
62
Laurent Vivier6e1c0d72021-02-22 11:50:04 +010063#ifndef AT_FLAGS_PRESERVE_ARGV0
64#define AT_FLAGS_PRESERVE_ARGV0_BIT 0
65#define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT)
66#endif
67
aurel32d088d662009-01-30 20:09:01 +000068char *exec_path;
Helge Deller258bec32022-12-05 12:38:25 +010069char real_exec_path[PATH_MAX];
aurel32d088d662009-01-30 20:09:01 +000070
Peter Maydell3cfb0452023-04-17 17:40:32 +010071static bool opt_one_insn_per_tb;
Stefan Weil8cb76752015-08-29 09:29:52 +020072static const char *argv0;
Alex Bennéefcedd922020-04-30 20:01:19 +010073static const char *gdbstub;
Stefan Weil8cb76752015-08-29 09:29:52 +020074static envlist_t *envlist;
Andreas Färber51fb2562013-07-02 18:26:11 +020075static const char *cpu_model;
Igor Mammedov2278b932018-02-07 11:40:26 +010076static const char *cpu_type;
Richard Henderson5ebdd772019-03-14 13:10:53 -070077static const char *seed_optarg;
Paul Brook379f6692009-07-17 12:48:08 +010078unsigned long mmap_min_addr;
Richard Henderson5ca870b2021-02-12 10:48:34 -080079uintptr_t guest_base;
Richard Hendersone307c192020-05-13 18:51:29 +010080bool have_guest_base;
Paolo Bonzini120a9842015-11-13 13:20:35 +010081
Alexander Graf288e65b2011-12-14 00:33:28 +010082/*
Josh Kunz4b25a502020-02-03 18:54:14 -080083 * Used to implement backwards-compatibility for the `-strace`, and
84 * QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
85 * -strace, or vice versa.
86 */
87static bool enable_strace;
88
89/*
90 * The last log mask given by the user in an environment variable or argument.
91 * Used to support command line arguments overriding environment variables.
92 */
93static int last_log_mask;
Richard Hendersonb4102532022-04-17 11:30:09 -070094static const char *last_log_filename;
Josh Kunz4b25a502020-02-03 18:54:14 -080095
96/*
Alexander Graf288e65b2011-12-14 00:33:28 +010097 * When running 32-on-64 we should make sure we can fit all of the possible
98 * guest address space into a contiguous chunk of virtual host memory.
99 *
100 * This way we will never overlap with our own libraries or binaries or stack
101 * or anything else that QEMU maps.
Richard Henderson18e80c52017-10-05 10:36:00 -0400102 *
103 * Many cpus reserve the high bit (or more than one for some 64-bit cpus)
104 * of the address for the kernel. Some cpus rely on this and user space
105 * uses the high bit(s) for pointer tagging and the like. For them, we
106 * must preserve the expected address space.
Alexander Graf288e65b2011-12-14 00:33:28 +0100107 */
Richard Henderson18e80c52017-10-05 10:36:00 -0400108#ifndef MAX_RESERVED_VA
109# if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
110# if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
111 (TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
Richard Henderson95059f92023-03-06 01:26:29 +0300112# define MAX_RESERVED_VA(CPU) 0xfffffffful
Richard Henderson18e80c52017-10-05 10:36:00 -0400113# else
Richard Henderson95059f92023-03-06 01:26:29 +0300114# define MAX_RESERVED_VA(CPU) ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1)
Richard Henderson18e80c52017-10-05 10:36:00 -0400115# endif
Alexander Graf314992b2013-01-03 14:17:18 +0100116# else
Richard Henderson8f67b9c2019-08-22 11:59:28 -0700117# define MAX_RESERVED_VA(CPU) 0
Alexander Graf314992b2013-01-03 14:17:18 +0100118# endif
Richard Henderson18e80c52017-10-05 10:36:00 -0400119#endif
120
Paul Brook68a1c812010-05-29 02:27:35 +0100121unsigned long reserved_va;
aurel321b530a62009-04-05 20:08:59 +0000122
Meador Inged03f9c32015-07-06 11:03:38 -0700123static void usage(int exitcode);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200124
Paolo Bonzini7ee28222010-05-26 16:08:22 +0200125static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
Riku Voipioe5868222014-03-04 04:28:43 +0200126const char *qemu_uname_release;
bellard586314f2003-03-03 15:02:29 +0000127
Helge Deller0a3346b2022-09-24 13:44:59 +0200128#if !defined(TARGET_DEFAULT_STACK_SIZE)
bellard9de5e442003-03-23 16:49:39 +0000129/* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
130 we allocate a bigger stack. Need a better solution, for example
131 by remapping the process stack directly at the right place */
Helge Deller0a3346b2022-09-24 13:44:59 +0200132#define TARGET_DEFAULT_STACK_SIZE 8 * 1024 * 1024UL
133#endif
134
135unsigned long guest_stack_size = TARGET_DEFAULT_STACK_SIZE;
bellard31e31b82003-02-18 22:55:36 +0000136
pbrookd5975362008-06-07 20:50:51 +0000137/***********************************************************/
138/* Helper routines for implementing atomic operations. */
139
pbrookd5975362008-06-07 20:50:51 +0000140/* Make sure everything is in a consistent state for calling fork(). */
141void fork_start(void)
142{
Peter Maydell06065c42017-12-07 12:41:21 +0000143 start_exclusive();
Riku Voipiod032d1b2009-12-04 15:16:31 +0200144 mmap_fork_start();
Peter Maydell024949c2017-12-04 14:22:11 +0000145 cpu_list_lock();
Alex Bennéef7e15af2022-10-04 12:52:21 +0100146 qemu_plugin_user_prefork_lock();
pbrookd5975362008-06-07 20:50:51 +0000147}
148
149void fork_end(int child)
150{
Alex Bennéef7e15af2022-10-04 12:52:21 +0100151 qemu_plugin_user_postfork(child);
Riku Voipiod032d1b2009-12-04 15:16:31 +0200152 mmap_fork_end(child);
pbrookd5975362008-06-07 20:50:51 +0000153 if (child) {
Andreas Färberbdc44642013-06-24 23:50:24 +0200154 CPUState *cpu, *next_cpu;
pbrookd5975362008-06-07 20:50:51 +0000155 /* Child processes created by fork() only have a single thread.
156 Discard information about the parent threads. */
Andreas Färberbdc44642013-06-24 23:50:24 +0200157 CPU_FOREACH_SAFE(cpu, next_cpu) {
158 if (cpu != thread_cpu) {
Philippe Mathieu-Daudé3c55dd52023-10-09 09:02:04 +0200159 QTAILQ_REMOVE_RCU(&cpus_queue, cpu, node);
Andreas Färberbdc44642013-06-24 23:50:24 +0200160 }
161 }
Paolo Bonzini267f6852016-08-28 03:45:14 +0200162 qemu_init_cpu_list();
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -0700163 gdbserver_fork(thread_cpu);
pbrookd5975362008-06-07 20:50:51 +0000164 } else {
Paolo Bonzini267f6852016-08-28 03:45:14 +0200165 cpu_list_unlock();
pbrookd5975362008-06-07 20:50:51 +0000166 }
Ilya Leoshkevich7de08162023-02-14 15:08:26 +0100167 /*
168 * qemu_init_cpu_list() reinitialized the child exclusive state, but we
169 * also need to keep current_cpu consistent, so call end_exclusive() for
170 * both child and parent.
171 */
172 end_exclusive();
pbrookd5975362008-06-07 20:50:51 +0000173}
174
Peter Maydellb44316f2018-02-13 13:22:46 +0000175__thread CPUState *thread_cpu;
bellard59faf6d2003-06-25 16:18:50 +0000176
Sergey Fedorov178f9422016-08-02 18:27:39 +0100177bool qemu_cpu_is_self(CPUState *cpu)
178{
179 return thread_cpu == cpu;
180}
181
182void qemu_cpu_kick(CPUState *cpu)
183{
184 cpu_exit(cpu);
185}
186
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300187void task_settid(TaskState *ts)
188{
189 if (ts->ts_tid == 0) {
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300190 ts->ts_tid = (pid_t)syscall(SYS_gettid);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300191 }
192}
193
194void stop_all_tasks(void)
195{
196 /*
197 * We trust that when using NPTL, start_exclusive()
198 * handles thread stopping correctly.
199 */
200 start_exclusive();
201}
202
pbrookc3a92832008-06-09 14:02:50 +0000203/* Assumes contents are already zeroed. */
pbrook624f7972008-05-31 16:11:38 +0000204void init_task_state(TaskState *ts)
205{
Cameron Esfahanieb33cda2022-01-27 16:12:51 -0800206 long ticks_per_sec;
207 struct timespec bt;
208
pbrook624f7972008-05-31 16:11:38 +0000209 ts->used = 1;
Peter Maydell5bfce0b2019-07-25 14:16:45 +0100210 ts->sigaltstack_used = (struct target_sigaltstack) {
211 .ss_sp = 0,
212 .ss_size = 0,
213 .ss_flags = TARGET_SS_DISABLE,
214 };
Cameron Esfahanieb33cda2022-01-27 16:12:51 -0800215
216 /* Capture task start time relative to system boot */
217
218 ticks_per_sec = sysconf(_SC_CLK_TCK);
219
220 if ((ticks_per_sec > 0) && !clock_gettime(CLOCK_BOOTTIME, &bt)) {
221 /* start_boottime is expressed in clock ticks */
222 ts->start_boottime = bt.tv_sec * (uint64_t) ticks_per_sec;
223 ts->start_boottime += bt.tv_nsec * (uint64_t) ticks_per_sec /
224 NANOSECONDS_PER_SECOND;
225 }
pbrook624f7972008-05-31 16:11:38 +0000226}
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200227
Andreas Färber30ba0ee2013-07-02 17:43:21 +0200228CPUArchState *cpu_copy(CPUArchState *env)
229{
Richard Henderson29a0af62019-03-22 16:07:18 -0700230 CPUState *cpu = env_cpu(env);
Igor Mammedov2278b932018-02-07 11:40:26 +0100231 CPUState *new_cpu = cpu_create(cpu_type);
Richard Hendersonb77af262023-09-13 17:22:49 -0700232 CPUArchState *new_env = cpu_env(new_cpu);
Andreas Färber30ba0ee2013-07-02 17:43:21 +0200233 CPUBreakpoint *bp;
Andreas Färber30ba0ee2013-07-02 17:43:21 +0200234
235 /* Reset non arch specific state */
Andreas Färber75a34032013-09-02 16:57:02 +0200236 cpu_reset(new_cpu);
Andreas Färber30ba0ee2013-07-02 17:43:21 +0200237
Richard Henderson6cc9d672021-03-01 19:21:08 -0800238 new_cpu->tcg_cflags = cpu->tcg_cflags;
Andreas Färber30ba0ee2013-07-02 17:43:21 +0200239 memcpy(new_env, env, sizeof(CPUArchState));
fanwj@mail.ustc.edu.cn2732c732023-02-08 23:49:12 +0800240#if defined(TARGET_I386) || defined(TARGET_X86_64)
241 new_env->gdt.base = target_mmap(0, sizeof(uint64_t) * TARGET_GDT_ENTRIES,
242 PROT_READ | PROT_WRITE,
243 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
244 memcpy(g2h_untagged(new_env->gdt.base), g2h_untagged(env->gdt.base),
245 sizeof(uint64_t) * TARGET_GDT_ENTRIES);
246 OBJECT(new_cpu)->free = OBJECT(cpu)->free;
247#endif
Andreas Färber30ba0ee2013-07-02 17:43:21 +0200248
249 /* Clone all break/watchpoints.
250 Note: Once we support ptrace with hw-debug register access, make sure
251 BP_CPU break/watchpoints are handled correctly on clone. */
Thierry Bultel1d085f62015-06-12 11:24:10 +0200252 QTAILQ_INIT(&new_cpu->breakpoints);
Andreas Färberf0c3c502013-08-26 21:22:53 +0200253 QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200254 cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
Andreas Färber30ba0ee2013-07-02 17:43:21 +0200255 }
Andreas Färber30ba0ee2013-07-02 17:43:21 +0200256
257 return new_env;
258}
259
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200260static void handle_arg_help(const char *arg)
261{
Riku Voipio4d1275c2015-09-28 16:12:16 +0300262 usage(EXIT_SUCCESS);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200263}
264
265static void handle_arg_log(const char *arg)
266{
Josh Kunz4b25a502020-02-03 18:54:14 -0800267 last_log_mask = qemu_str_to_log_mask(arg);
268 if (!last_log_mask) {
Peter Maydell59a6fa62013-02-11 16:41:21 +0000269 qemu_print_log_usage(stdout);
Riku Voipio4d1275c2015-09-28 16:12:16 +0300270 exit(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200271 }
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200272}
273
Alex Bennée8423fa92017-10-17 11:35:14 +0100274static void handle_arg_dfilter(const char *arg)
275{
Alex Bennée7f4341e2019-08-30 15:36:48 +0100276 qemu_set_dfilter_ranges(arg, &error_fatal);
Alex Bennée8423fa92017-10-17 11:35:14 +0100277}
278
陳韋任50171d42011-11-08 17:46:44 +0800279static void handle_arg_log_filename(const char *arg)
280{
Richard Hendersonb4102532022-04-17 11:30:09 -0700281 last_log_filename = arg;
陳韋任50171d42011-11-08 17:46:44 +0800282}
283
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200284static void handle_arg_set_env(const char *arg)
285{
286 char *r, *p, *token;
287 r = p = strdup(arg);
288 while ((token = strsep(&p, ",")) != NULL) {
289 if (envlist_setenv(envlist, token) != 0) {
Riku Voipio4d1275c2015-09-28 16:12:16 +0300290 usage(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200291 }
292 }
293 free(r);
294}
295
296static void handle_arg_unset_env(const char *arg)
297{
298 char *r, *p, *token;
299 r = p = strdup(arg);
300 while ((token = strsep(&p, ",")) != NULL) {
301 if (envlist_unsetenv(envlist, token) != 0) {
Riku Voipio4d1275c2015-09-28 16:12:16 +0300302 usage(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200303 }
304 }
305 free(r);
306}
307
308static void handle_arg_argv0(const char *arg)
309{
310 argv0 = strdup(arg);
311}
312
313static void handle_arg_stack_size(const char *arg)
314{
315 char *p;
316 guest_stack_size = strtoul(arg, &p, 0);
317 if (guest_stack_size == 0) {
Riku Voipio4d1275c2015-09-28 16:12:16 +0300318 usage(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200319 }
320
321 if (*p == 'M') {
Philippe Mathieu-Daudéb52713c2018-06-25 09:42:37 -0300322 guest_stack_size *= MiB;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200323 } else if (*p == 'k' || *p == 'K') {
Philippe Mathieu-Daudéb52713c2018-06-25 09:42:37 -0300324 guest_stack_size *= KiB;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200325 }
326}
327
328static void handle_arg_ld_prefix(const char *arg)
329{
330 interp_prefix = strdup(arg);
331}
332
333static void handle_arg_pagesize(const char *arg)
334{
335 qemu_host_page_size = atoi(arg);
336 if (qemu_host_page_size == 0 ||
337 (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
338 fprintf(stderr, "page size must be a power of two\n");
Riku Voipio4d1275c2015-09-28 16:12:16 +0300339 exit(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200340 }
341}
342
Richard Henderson5ebdd772019-03-14 13:10:53 -0700343static void handle_arg_seed(const char *arg)
Magnus Reftelc5e4a5a2014-10-14 17:18:17 +0200344{
Richard Henderson5ebdd772019-03-14 13:10:53 -0700345 seed_optarg = arg;
Magnus Reftelc5e4a5a2014-10-14 17:18:17 +0200346}
347
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200348static void handle_arg_gdb(const char *arg)
349{
Alex Bennéefcedd922020-04-30 20:01:19 +0100350 gdbstub = g_strdup(arg);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200351}
352
353static void handle_arg_uname(const char *arg)
354{
355 qemu_uname_release = strdup(arg);
356}
357
358static void handle_arg_cpu(const char *arg)
359{
360 cpu_model = strdup(arg);
Peter Maydellc8057f92012-08-02 13:45:54 +0100361 if (cpu_model == NULL || is_help_option(cpu_model)) {
Thomas Huthb67e5cb2023-04-24 14:21:26 +0200362 list_cpus();
Riku Voipio4d1275c2015-09-28 16:12:16 +0300363 exit(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200364 }
365}
366
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200367static void handle_arg_guest_base(const char *arg)
368{
369 guest_base = strtol(arg, NULL, 0);
Richard Hendersone307c192020-05-13 18:51:29 +0100370 have_guest_base = true;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200371}
372
373static void handle_arg_reserved_va(const char *arg)
374{
375 char *p;
376 int shift = 0;
Richard Henderson95059f92023-03-06 01:26:29 +0300377 unsigned long val;
378
379 val = strtoul(arg, &p, 0);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200380 switch (*p) {
381 case 'k':
382 case 'K':
383 shift = 10;
384 break;
385 case 'M':
386 shift = 20;
387 break;
388 case 'G':
389 shift = 30;
390 break;
391 }
392 if (shift) {
Richard Henderson95059f92023-03-06 01:26:29 +0300393 unsigned long unshifted = val;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200394 p++;
Richard Henderson95059f92023-03-06 01:26:29 +0300395 val <<= shift;
396 if (val >> shift != unshifted) {
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200397 fprintf(stderr, "Reserved virtual address too big\n");
Riku Voipio4d1275c2015-09-28 16:12:16 +0300398 exit(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200399 }
400 }
401 if (*p) {
402 fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
Riku Voipio4d1275c2015-09-28 16:12:16 +0300403 exit(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200404 }
Richard Henderson95059f92023-03-06 01:26:29 +0300405 /* The representation is size - 1, with 0 remaining "default". */
406 reserved_va = val ? val - 1 : 0;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200407}
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200408
Peter Maydelle99c1f82023-04-17 17:40:35 +0100409static void handle_arg_one_insn_per_tb(const char *arg)
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200410{
Peter Maydell3cfb0452023-04-17 17:40:32 +0100411 opt_one_insn_per_tb = true;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200412}
413
414static void handle_arg_strace(const char *arg)
415{
Josh Kunz4b25a502020-02-03 18:54:14 -0800416 enable_strace = true;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200417}
418
419static void handle_arg_version(const char *arg)
420{
Thomas Huth7e563bf2018-02-15 12:06:47 +0100421 printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
Thomas Huth0781dd62016-10-05 11:54:44 +0200422 "\n" QEMU_COPYRIGHT "\n");
Riku Voipio4d1275c2015-09-28 16:12:16 +0300423 exit(EXIT_SUCCESS);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200424}
425
Lluís Vilanova6533dd62016-07-15 19:08:38 +0200426static void handle_arg_trace(const char *arg)
427{
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500428 trace_opt_parse(arg);
Lluís Vilanova6533dd62016-07-15 19:08:38 +0200429}
430
Max Filippov130ea832019-09-06 09:57:13 -0700431#if defined(TARGET_XTENSA)
432static void handle_arg_abi_call0(const char *arg)
433{
434 xtensa_set_abi_call0();
435}
436#endif
437
Ilya Leoshkevich5584e2d2023-01-12 16:20:13 +0100438static void handle_arg_perfmap(const char *arg)
439{
440 perf_enable_perfmap();
441}
442
443static void handle_arg_jitdump(const char *arg)
444{
445 perf_enable_jitdump();
446}
447
Lluís Vilanovaf308f642017-07-24 12:41:51 +0300448static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
449
450#ifdef CONFIG_PLUGIN
451static void handle_arg_plugin(const char *arg)
452{
453 qemu_plugin_opt_parse(arg, &plugins);
454}
455#endif
456
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200457struct qemu_argument {
458 const char *argv;
459 const char *env;
460 bool has_arg;
461 void (*handle_opt)(const char *arg);
462 const char *example;
463 const char *help;
464};
465
Jim Meyering42644ce2012-05-21 21:56:19 +0200466static const struct qemu_argument arg_table[] = {
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200467 {"h", "", false, handle_arg_help,
468 "", "print this help"},
Meador Ingedaaf8c82015-07-06 11:03:39 -0700469 {"help", "", false, handle_arg_help,
470 "", ""},
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200471 {"g", "QEMU_GDB", true, handle_arg_gdb,
472 "port", "wait gdb connection to 'port'"},
473 {"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix,
474 "path", "set the elf interpreter prefix to 'path'"},
475 {"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size,
476 "size", "set the stack size to 'size' bytes"},
477 {"cpu", "QEMU_CPU", true, handle_arg_cpu,
Peter Maydellc8057f92012-08-02 13:45:54 +0100478 "model", "select CPU (-cpu help for list)"},
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200479 {"E", "QEMU_SET_ENV", true, handle_arg_set_env,
480 "var=value", "sets targets environment variable (see below)"},
481 {"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env,
482 "var", "unsets targets environment variable (see below)"},
483 {"0", "QEMU_ARGV0", true, handle_arg_argv0,
484 "argv0", "forces target process argv[0] to be 'argv0'"},
485 {"r", "QEMU_UNAME", true, handle_arg_uname,
486 "uname", "set qemu uname release string to 'uname'"},
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200487 {"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base,
488 "address", "set guest_base address to 'address'"},
489 {"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va,
490 "size", "reserve 'size' bytes for guest virtual address space"},
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200491 {"d", "QEMU_LOG", true, handle_arg_log,
Peter Maydell989b6972013-02-26 17:52:40 +0000492 "item[,...]", "enable logging of specified items "
493 "(use '-d help' for a list of items)"},
Alex Bennée8423fa92017-10-17 11:35:14 +0100494 {"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter,
495 "range[,...]","filter logging based on address range"},
陳韋任50171d42011-11-08 17:46:44 +0800496 {"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename,
Peter Maydell989b6972013-02-26 17:52:40 +0000497 "logfile", "write logs to 'logfile' (default stderr)"},
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200498 {"p", "QEMU_PAGESIZE", true, handle_arg_pagesize,
499 "pagesize", "set the host page size to 'pagesize'"},
Peter Maydelle99c1f82023-04-17 17:40:35 +0100500 {"one-insn-per-tb",
501 "QEMU_ONE_INSN_PER_TB", false, handle_arg_one_insn_per_tb,
502 "", "run with one guest instruction per emulated TB"},
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200503 {"strace", "QEMU_STRACE", false, handle_arg_strace,
504 "", "log system calls"},
Richard Henderson5ebdd772019-03-14 13:10:53 -0700505 {"seed", "QEMU_RAND_SEED", true, handle_arg_seed,
Magnus Reftelc5e4a5a2014-10-14 17:18:17 +0200506 "", "Seed for pseudo-random number generator"},
Lluís Vilanova6533dd62016-07-15 19:08:38 +0200507 {"trace", "QEMU_TRACE", true, handle_arg_trace,
508 "", "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
Lluís Vilanovaf308f642017-07-24 12:41:51 +0300509#ifdef CONFIG_PLUGIN
510 {"plugin", "QEMU_PLUGIN", true, handle_arg_plugin,
Mahmoud Mandour3a445ac2021-07-30 15:58:05 +0200511 "", "[file=]<file>[,<argname>=<argvalue>]"},
Lluís Vilanovaf308f642017-07-24 12:41:51 +0300512#endif
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200513 {"version", "QEMU_VERSION", false, handle_arg_version,
Peter Maydell1386d4c2011-09-29 15:48:12 +0100514 "", "display version information and exit"},
Max Filippov130ea832019-09-06 09:57:13 -0700515#if defined(TARGET_XTENSA)
516 {"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0,
517 "", "assume CALL0 Xtensa ABI"},
518#endif
Ilya Leoshkevich5584e2d2023-01-12 16:20:13 +0100519 {"perfmap", "QEMU_PERFMAP", false, handle_arg_perfmap,
520 "", "Generate a /tmp/perf-${pid}.map file for perf"},
521 {"jitdump", "QEMU_JITDUMP", false, handle_arg_jitdump,
522 "", "Generate a jit-${pid}.dump file for perf"},
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200523 {NULL, NULL, false, NULL, NULL, NULL}
524};
525
Meador Inged03f9c32015-07-06 11:03:38 -0700526static void usage(int exitcode)
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200527{
Jim Meyering42644ce2012-05-21 21:56:19 +0200528 const struct qemu_argument *arginfo;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200529 int maxarglen;
530 int maxenvlen;
531
Paolo Bonzini2e599152013-06-04 14:45:27 +0200532 printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
533 "Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n"
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200534 "\n"
535 "Options and associated environment variables:\n"
536 "\n");
537
Peter Maydell63ec54d2013-02-14 08:46:43 +0000538 /* Calculate column widths. We must always have at least enough space
539 * for the column header.
540 */
541 maxarglen = strlen("Argument");
542 maxenvlen = strlen("Env-variable");
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200543
544 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
Peter Maydell63ec54d2013-02-14 08:46:43 +0000545 int arglen = strlen(arginfo->argv);
546 if (arginfo->has_arg) {
547 arglen += strlen(arginfo->example) + 1;
548 }
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200549 if (strlen(arginfo->env) > maxenvlen) {
550 maxenvlen = strlen(arginfo->env);
551 }
Peter Maydell63ec54d2013-02-14 08:46:43 +0000552 if (arglen > maxarglen) {
553 maxarglen = arglen;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200554 }
555 }
556
Peter Maydell63ec54d2013-02-14 08:46:43 +0000557 printf("%-*s %-*s Description\n", maxarglen+1, "Argument",
558 maxenvlen, "Env-variable");
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200559
560 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
561 if (arginfo->has_arg) {
562 printf("-%s %-*s %-*s %s\n", arginfo->argv,
Peter Maydell63ec54d2013-02-14 08:46:43 +0000563 (int)(maxarglen - strlen(arginfo->argv) - 1),
564 arginfo->example, maxenvlen, arginfo->env, arginfo->help);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200565 } else {
Peter Maydell63ec54d2013-02-14 08:46:43 +0000566 printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv,
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200567 maxenvlen, arginfo->env,
568 arginfo->help);
569 }
570 }
571
572 printf("\n"
573 "Defaults:\n"
574 "QEMU_LD_PREFIX = %s\n"
Peter Maydell989b6972013-02-26 17:52:40 +0000575 "QEMU_STACK_SIZE = %ld byte\n",
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200576 interp_prefix,
Peter Maydell989b6972013-02-26 17:52:40 +0000577 guest_stack_size);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200578
579 printf("\n"
580 "You can use -E and -U options or the QEMU_SET_ENV and\n"
581 "QEMU_UNSET_ENV environment variables to set and unset\n"
582 "environment variables for the target process.\n"
583 "It is possible to provide several variables by separating them\n"
584 "by commas in getsubopt(3) style. Additionally it is possible to\n"
585 "provide the -E and -U options multiple times.\n"
586 "The following lines are equivalent:\n"
587 " -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
588 " -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
589 " QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
590 "Note that if you provide several changes to a single variable\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500591 "the last change will stay in effect.\n"
592 "\n"
593 QEMU_HELP_BOTTOM "\n");
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200594
Meador Inged03f9c32015-07-06 11:03:38 -0700595 exit(exitcode);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200596}
597
598static int parse_args(int argc, char **argv)
599{
600 const char *r;
601 int optind;
Jim Meyering42644ce2012-05-21 21:56:19 +0200602 const struct qemu_argument *arginfo;
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200603
604 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
605 if (arginfo->env == NULL) {
606 continue;
607 }
608
609 r = getenv(arginfo->env);
610 if (r != NULL) {
611 arginfo->handle_opt(r);
612 }
613 }
614
615 optind = 1;
616 for (;;) {
617 if (optind >= argc) {
618 break;
619 }
620 r = argv[optind];
621 if (r[0] != '-') {
622 break;
623 }
624 optind++;
625 r++;
626 if (!strcmp(r, "-")) {
627 break;
628 }
Meador Ingeba025772015-07-06 11:03:41 -0700629 /* Treat --foo the same as -foo. */
630 if (r[0] == '-') {
631 r++;
632 }
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200633
634 for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
635 if (!strcmp(r, arginfo->argv)) {
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200636 if (arginfo->has_arg) {
Peter Maydell1386d4c2011-09-29 15:48:12 +0100637 if (optind >= argc) {
Meador Inge138940b2015-07-06 11:03:40 -0700638 (void) fprintf(stderr,
639 "qemu: missing argument for option '%s'\n", r);
Riku Voipio4d1275c2015-09-28 16:12:16 +0300640 exit(EXIT_FAILURE);
Peter Maydell1386d4c2011-09-29 15:48:12 +0100641 }
642 arginfo->handle_opt(argv[optind]);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200643 optind++;
Peter Maydell1386d4c2011-09-29 15:48:12 +0100644 } else {
645 arginfo->handle_opt(NULL);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200646 }
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200647 break;
648 }
649 }
650
651 /* no option matched the current argv */
652 if (arginfo->handle_opt == NULL) {
Meador Inge138940b2015-07-06 11:03:40 -0700653 (void) fprintf(stderr, "qemu: unknown option '%s'\n", r);
Riku Voipio4d1275c2015-09-28 16:12:16 +0300654 exit(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200655 }
656 }
657
658 if (optind >= argc) {
Meador Inge138940b2015-07-06 11:03:40 -0700659 (void) fprintf(stderr, "qemu: no user program specified\n");
Riku Voipio4d1275c2015-09-28 16:12:16 +0300660 exit(EXIT_FAILURE);
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200661 }
662
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200663 exec_path = argv[optind];
664
665 return optind;
666}
667
malc902b3d52008-12-10 19:18:40 +0000668int main(int argc, char **argv, char **envp)
bellard31e31b82003-02-18 22:55:36 +0000669{
bellard01ffc752003-02-18 23:00:51 +0000670 struct target_pt_regs regs1, *regs = &regs1;
bellard31e31b82003-02-18 22:55:36 +0000671 struct image_info info1, *info = &info1;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300672 struct linux_binprm bprm;
Nathan Froyd48e15fc2010-10-29 07:48:57 -0700673 TaskState *ts;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100674 CPUArchState *env;
Andreas Färberdb6b81d2013-06-27 19:49:31 +0200675 CPUState *cpu;
bellard586314f2003-03-03 15:02:29 +0000676 int optind;
aurel3204a6dfe2009-01-30 19:59:17 +0000677 char **target_environ, **wrk;
aurel327d8cec92009-04-15 16:11:52 +0000678 char **target_argv;
679 int target_argc;
aurel327d8cec92009-04-15 16:11:52 +0000680 int i;
Arnaud Patardfd4d81d2009-06-19 10:39:36 +0300681 int ret;
Laurent Vivier03cfd8f2013-08-30 01:46:44 +0200682 int execfd;
Richard Henderson8f67b9c2019-08-22 11:59:28 -0700683 unsigned long max_reserved_va;
Laurent Vivier6e1c0d72021-02-22 11:50:04 +0100684 bool preserve_argv0;
thsb12b6a12007-06-17 16:38:39 +0000685
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100686 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100687 module_call_init(MODULE_INIT_TRACE);
Paolo Bonzini267f6852016-08-28 03:45:14 +0200688 qemu_init_cpu_list();
Andreas Färberce008c12012-03-04 21:32:36 +0100689 module_call_init(MODULE_INIT_QOM);
690
Saurav Sachidanandec45bbe2017-03-20 17:38:28 +0000691 envlist = envlist_create();
aurel3204a6dfe2009-01-30 19:59:17 +0000692
Andreas Schwab7f750ef2023-03-29 17:00:06 +0200693 /*
694 * add current environment into the list
695 * envlist_setenv adds to the front of the list; to preserve environ
696 * order add from back to front
697 */
aurel3204a6dfe2009-01-30 19:59:17 +0000698 for (wrk = environ; *wrk != NULL; wrk++) {
Andreas Schwab7f750ef2023-03-29 17:00:06 +0200699 continue;
700 }
701 while (wrk != environ) {
702 wrk--;
aurel3204a6dfe2009-01-30 19:59:17 +0000703 (void) envlist_setenv(envlist, *wrk);
704 }
705
Richard Henderson703e0e82010-03-19 14:21:13 -0700706 /* Read the stack limit from the kernel. If it's "unlimited",
707 then we can do little else besides use the default. */
708 {
709 struct rlimit lim;
710 if (getrlimit(RLIMIT_STACK, &lim) == 0
takasi-y@ops.dti.ne.jp81bbe902010-04-12 04:07:35 +0900711 && lim.rlim_cur != RLIM_INFINITY
Helge Deller0a3346b2022-09-24 13:44:59 +0200712 && lim.rlim_cur == (target_long)lim.rlim_cur
713 && lim.rlim_cur > guest_stack_size) {
Richard Henderson703e0e82010-03-19 14:21:13 -0700714 guest_stack_size = lim.rlim_cur;
715 }
716 }
717
j_mayerb1f9be32007-03-19 08:08:28 +0000718 cpu_model = NULL;
john cooperb5ec5ce2010-02-20 11:14:59 -0600719
Lluís Vilanova6533dd62016-07-15 19:08:38 +0200720 qemu_add_opts(&qemu_trace_opts);
Lluís Vilanovaf308f642017-07-24 12:41:51 +0300721 qemu_plugin_add_opts();
Lluís Vilanova6533dd62016-07-15 19:08:38 +0200722
Johannes Schauerfc9c5412011-08-06 08:54:12 +0200723 optind = parse_args(argc, argv);
Peter Maydell4b5dfd82011-07-18 11:44:09 +0100724
Richard Hendersonb4102532022-04-17 11:30:09 -0700725 qemu_set_log_filename_flags(last_log_filename,
726 last_log_mask | (enable_strace * LOG_STRACE),
727 &error_fatal);
Josh Kunz4b25a502020-02-03 18:54:14 -0800728
Lluís Vilanova6533dd62016-07-15 19:08:38 +0200729 if (!trace_init_backends()) {
730 exit(1);
731 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500732 trace_init_file();
Paolo Bonzini0572f552020-10-27 04:58:26 -0400733 qemu_plugin_load_list(&plugins, &error_fatal);
Lluís Vilanova6533dd62016-07-15 19:08:38 +0200734
bellard31e31b82003-02-18 22:55:36 +0000735 /* Zero out regs */
bellard01ffc752003-02-18 23:00:51 +0000736 memset(regs, 0, sizeof(struct target_pt_regs));
bellard31e31b82003-02-18 22:55:36 +0000737
738 /* Zero out image_info */
739 memset(info, 0, sizeof(struct image_info));
740
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300741 memset(&bprm, 0, sizeof (bprm));
742
bellard74cd30b2003-04-11 00:13:41 +0000743 /* Scan interp_prefix dir for replacement files. */
744 init_paths(interp_prefix);
745
Peter Maydell4a24a752013-09-03 20:12:20 +0100746 init_qemu_uname_release();
747
Laurent Vivier6e1c0d72021-02-22 11:50:04 +0100748 /*
749 * Manage binfmt-misc open-binary flag
750 */
YunQiang Su768fe762018-02-20 18:33:05 +0100751 execfd = qemu_getauxval(AT_EXECFD);
752 if (execfd == 0) {
Laurent Vivier9d3019b2019-07-14 15:40:27 +0200753 execfd = open(exec_path, O_RDONLY);
YunQiang Su768fe762018-02-20 18:33:05 +0100754 if (execfd < 0) {
Laurent Vivier9d3019b2019-07-14 15:40:27 +0200755 printf("Error while loading %s: %s\n", exec_path, strerror(errno));
YunQiang Su768fe762018-02-20 18:33:05 +0100756 _exit(EXIT_FAILURE);
757 }
758 }
759
Helge Deller258bec32022-12-05 12:38:25 +0100760 /* Resolve executable file name to full path name */
761 if (realpath(exec_path, real_exec_path)) {
762 exec_path = real_exec_path;
763 }
764
Laurent Vivier6e1c0d72021-02-22 11:50:04 +0100765 /*
766 * get binfmt_misc flags
767 */
768 preserve_argv0 = !!(qemu_getauxval(AT_FLAGS) & AT_FLAGS_PRESERVE_ARGV0);
769
770 /*
771 * Manage binfmt-misc preserve-arg[0] flag
772 * argv[optind] full path to the binary
773 * argv[optind + 1] original argv[0]
774 */
775 if (optind + 1 < argc && preserve_argv0) {
776 optind++;
777 }
778
bellard46027c02007-11-08 13:56:19 +0000779 if (cpu_model == NULL) {
YunQiang Su768fe762018-02-20 18:33:05 +0100780 cpu_model = cpu_get_model(get_elf_eflags(execfd));
bellardaaed9092007-11-10 15:15:54 +0000781 }
Eduardo Habkostc1c8cfe2019-04-16 23:59:40 -0300782 cpu_type = parse_cpu_option(cpu_model);
Igor Mammedov2278b932018-02-07 11:40:26 +0100783
Igor Mammedov2b5249b2018-05-17 13:51:17 +0200784 /* init tcg before creating CPUs and to get qemu_host_page_size */
Claudio Fontana940e43a2021-02-04 17:39:24 +0100785 {
Peter Maydell3cfb0452023-04-17 17:40:32 +0100786 AccelState *accel = current_accel();
787 AccelClass *ac = ACCEL_GET_CLASS(accel);
Igor Mammedov2278b932018-02-07 11:40:26 +0100788
Claudio Fontanab86f59c2021-02-04 17:39:25 +0100789 accel_init_interfaces(ac);
Peter Maydell3cfb0452023-04-17 17:40:32 +0100790 object_property_set_bool(OBJECT(accel), "one-insn-per-tb",
791 opt_one_insn_per_tb, &error_abort);
Claudio Fontana92242f32021-03-22 14:27:58 +0100792 ac->init_machine(NULL);
Claudio Fontana940e43a2021-02-04 17:39:24 +0100793 }
Igor Mammedov2278b932018-02-07 11:40:26 +0100794 cpu = cpu_create(cpu_type);
Richard Hendersonb77af262023-09-13 17:22:49 -0700795 env = cpu_env(cpu);
Andreas Färber0ac46af2013-07-26 16:42:25 +0200796 cpu_reset(cpu);
Andreas Färberdb6b81d2013-06-27 19:49:31 +0200797 thread_cpu = cpu;
ths3b46e622007-09-17 08:09:54 +0000798
Richard Henderson8f67b9c2019-08-22 11:59:28 -0700799 /*
800 * Reserving too much vm space via mmap can run into problems
801 * with rlimits, oom due to page table creation, etc. We will
802 * still try it, if directed by the command-line option, but
803 * not by default.
804 */
805 max_reserved_va = MAX_RESERVED_VA(cpu);
806 if (reserved_va != 0) {
Richard Henderson95059f92023-03-06 01:26:29 +0300807 if ((reserved_va + 1) % qemu_host_page_size) {
Richard Henderson2f7828b2023-03-06 20:38:30 -0800808 char *s = size_to_str(qemu_host_page_size);
809 fprintf(stderr, "Reserved virtual address not aligned mod %s\n", s);
810 g_free(s);
811 exit(EXIT_FAILURE);
812 }
Richard Henderson8f67b9c2019-08-22 11:59:28 -0700813 if (max_reserved_va && reserved_va > max_reserved_va) {
814 fprintf(stderr, "Reserved virtual address too big\n");
815 exit(EXIT_FAILURE);
816 }
817 } else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) {
Richard Henderson95059f92023-03-06 01:26:29 +0300818 /* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */
819 reserved_va = max_reserved_va;
Richard Henderson8f67b9c2019-08-22 11:59:28 -0700820 }
821
Richard Hendersonc8fb5cf2023-08-02 14:25:27 -0700822 /*
823 * Temporarily disable
824 * "comparison is always false due to limited range of data type"
825 * due to comparison between (possible) uint64_t and uintptr_t.
826 */
827#pragma GCC diagnostic push
828#pragma GCC diagnostic ignored "-Wtype-limits"
829
830 /*
831 * Select an initial value for task_unmapped_base that is in range.
832 */
833 if (reserved_va) {
834 if (TASK_UNMAPPED_BASE < reserved_va) {
835 task_unmapped_base = TASK_UNMAPPED_BASE;
836 } else {
837 /* The most common default formula is TASK_SIZE / 3. */
838 task_unmapped_base = TARGET_PAGE_ALIGN(reserved_va / 3);
839 }
840 } else if (TASK_UNMAPPED_BASE < UINTPTR_MAX) {
841 task_unmapped_base = TASK_UNMAPPED_BASE;
842 } else {
843 /* 32-bit host: pick something medium size. */
844 task_unmapped_base = 0x10000000;
845 }
846 mmap_next_start = task_unmapped_base;
847
Richard Hendersonda2b71f2023-08-02 15:17:33 -0700848 /* Similarly for elf_et_dyn_base. */
849 if (reserved_va) {
850 if (ELF_ET_DYN_BASE < reserved_va) {
851 elf_et_dyn_base = ELF_ET_DYN_BASE;
852 } else {
853 /* The most common default formula is TASK_SIZE / 3 * 2. */
854 elf_et_dyn_base = TARGET_PAGE_ALIGN(reserved_va / 3) * 2;
855 }
856 } else if (ELF_ET_DYN_BASE < UINTPTR_MAX) {
857 elf_et_dyn_base = ELF_ET_DYN_BASE;
858 } else {
859 /* 32-bit host: pick something medium size. */
860 elf_et_dyn_base = 0x18000000;
861 }
862
Richard Hendersonc8fb5cf2023-08-02 14:25:27 -0700863#pragma GCC diagnostic pop
864
Richard Hendersona573e9b2019-03-13 13:53:22 -0700865 {
866 Error *err = NULL;
867 if (seed_optarg != NULL) {
Richard Hendersona573e9b2019-03-13 13:53:22 -0700868 qemu_guest_random_seed_main(seed_optarg, &err);
869 } else {
870 qcrypto_init(&err);
Richard Henderson5ebdd772019-03-14 13:10:53 -0700871 }
Richard Hendersona573e9b2019-03-13 13:53:22 -0700872 if (err) {
873 error_reportf_err(err, "cannot initialize crypto: ");
874 exit(1);
875 }
Magnus Reftelc5e4a5a2014-10-14 17:18:17 +0200876 }
877
aurel3204a6dfe2009-01-30 19:59:17 +0000878 target_environ = envlist_to_environ(envlist, NULL);
879 envlist_free(envlist);
thsb12b6a12007-06-17 16:38:39 +0000880
Paul Brook379f6692009-07-17 12:48:08 +0100881 /*
Paul Brook379f6692009-07-17 12:48:08 +0100882 * Read in mmap_min_addr kernel parameter. This value is used
883 * When loading the ELF image to determine whether guest_base
Richard Henderson14f24e12010-03-10 15:39:07 -0800884 * is needed. It is also used in mmap_find_vma.
Paul Brook379f6692009-07-17 12:48:08 +0100885 */
Richard Henderson14f24e12010-03-10 15:39:07 -0800886 {
Paul Brook379f6692009-07-17 12:48:08 +0100887 FILE *fp;
888
889 if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
890 unsigned long tmp;
Richard Hendersonc9f80662020-07-24 14:23:14 -0700891 if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) {
Paul Brook379f6692009-07-17 12:48:08 +0100892 mmap_min_addr = tmp;
Richard Hendersonc9f80662020-07-24 14:23:14 -0700893 qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n",
894 mmap_min_addr);
Paul Brook379f6692009-07-17 12:48:08 +0100895 }
896 fclose(fp);
897 }
898 }
Paul Brook379f6692009-07-17 12:48:08 +0100899
aurel327d8cec92009-04-15 16:11:52 +0000900 /*
Richard Hendersonc9f80662020-07-24 14:23:14 -0700901 * We prefer to not make NULL pointers accessible to QEMU.
902 * If we're in a chroot with no /proc, fall back to 1 page.
903 */
904 if (mmap_min_addr == 0) {
905 mmap_min_addr = qemu_host_page_size;
906 qemu_log_mask(CPU_LOG_PAGE,
907 "host mmap_min_addr=0x%lx (fallback)\n",
908 mmap_min_addr);
909 }
910
911 /*
aurel327d8cec92009-04-15 16:11:52 +0000912 * Prepare copy of argv vector for target.
913 */
914 target_argc = argc - optind;
915 target_argv = calloc(target_argc + 1, sizeof (char *));
916 if (target_argv == NULL) {
Paolo Bonzini7d374352018-12-13 23:37:37 +0100917 (void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
918 exit(EXIT_FAILURE);
aurel327d8cec92009-04-15 16:11:52 +0000919 }
920
921 /*
922 * If argv0 is specified (using '-0' switch) we replace
923 * argv[0] pointer with the given one.
924 */
925 i = 0;
926 if (argv0 != NULL) {
927 target_argv[i++] = strdup(argv0);
928 }
929 for (; i < target_argc; i++) {
930 target_argv[i] = strdup(argv[optind + i]);
931 }
932 target_argv[target_argc] = NULL;
933
Markus Armbrusterc78d65e2015-09-14 13:53:03 +0200934 ts = g_new0(TaskState, 1);
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300935 init_task_state(ts);
936 /* build Task State */
937 ts->info = info;
938 ts->bprm = &bprm;
Andreas Färber0429a972013-08-26 18:14:44 +0200939 cpu->opaque = ts;
Mika Westerbergedf8e2a2009-04-07 09:57:11 +0300940 task_settid(ts);
941
Owen Andersonc0933642021-07-01 22:12:55 +0000942 fd_trans_init();
943
Laurent Vivier9d3019b2019-07-14 15:40:27 +0200944 ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
Arnaud Patardfd4d81d2009-06-19 10:39:36 +0300945 info, &bprm);
946 if (ret != 0) {
Laurent Vivier9d3019b2019-07-14 15:40:27 +0200947 printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
Riku Voipio4d1275c2015-09-28 16:12:16 +0300948 _exit(EXIT_FAILURE);
thsb12b6a12007-06-17 16:38:39 +0000949 }
950
951 for (wrk = target_environ; *wrk; wrk++) {
Saurav Sachidanandec45bbe2017-03-20 17:38:28 +0000952 g_free(*wrk);
bellard31e31b82003-02-18 22:55:36 +0000953 }
ths3b46e622007-09-17 08:09:54 +0000954
Saurav Sachidanandec45bbe2017-03-20 17:38:28 +0000955 g_free(target_environ);
thsb12b6a12007-06-17 16:38:39 +0000956
Paolo Bonzini13829022015-11-13 12:32:19 +0100957 if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
Richard Henderson93756fd2022-04-17 11:30:02 -0700958 FILE *f = qemu_log_trylock();
959 if (f) {
960 fprintf(f, "guest_base %p\n", (void *)guest_base);
961 fprintf(f, "page layout changed following binary load\n");
962 page_dump(f);
ths3b46e622007-09-17 08:09:54 +0000963
Richard Henderson93756fd2022-04-17 11:30:02 -0700964 fprintf(f, "end_code 0x" TARGET_ABI_FMT_lx "\n",
965 info->end_code);
966 fprintf(f, "start_code 0x" TARGET_ABI_FMT_lx "\n",
967 info->start_code);
968 fprintf(f, "start_data 0x" TARGET_ABI_FMT_lx "\n",
969 info->start_data);
970 fprintf(f, "end_data 0x" TARGET_ABI_FMT_lx "\n",
971 info->end_data);
972 fprintf(f, "start_stack 0x" TARGET_ABI_FMT_lx "\n",
973 info->start_stack);
974 fprintf(f, "brk 0x" TARGET_ABI_FMT_lx "\n",
975 info->brk);
976 fprintf(f, "entry 0x" TARGET_ABI_FMT_lx "\n",
977 info->entry);
978 fprintf(f, "argv_start 0x" TARGET_ABI_FMT_lx "\n",
Richard Henderson60f1c802022-04-26 19:51:29 -0700979 info->argv);
Richard Henderson93756fd2022-04-17 11:30:02 -0700980 fprintf(f, "env_start 0x" TARGET_ABI_FMT_lx "\n",
Richard Henderson60f1c802022-04-26 19:51:29 -0700981 info->envp);
Richard Henderson93756fd2022-04-17 11:30:02 -0700982 fprintf(f, "auxv_start 0x" TARGET_ABI_FMT_lx "\n",
983 info->saved_auxv);
984 qemu_log_unlock(f);
985 }
blueswir12e77eac2009-01-20 16:57:34 +0000986 }
bellard31e31b82003-02-18 22:55:36 +0000987
pbrook53a59602006-03-25 19:31:22 +0000988 target_set_brk(info->brk);
bellard31e31b82003-02-18 22:55:36 +0000989 syscall_init();
bellard66fb9762003-03-23 01:06:05 +0000990 signal_init();
bellard31e31b82003-02-18 22:55:36 +0000991
Richard Henderson9002ec72010-05-06 08:50:41 -0700992 /* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
993 generating the prologue until now so that the prologue can take
994 the real value of GUEST_BASE into account. */
Richard Henderson935f75a2023-09-29 19:35:26 -0700995 tcg_prologue_init();
Richard Henderson9002ec72010-05-06 08:50:41 -0700996
Laurent Viviercd71c082018-04-11 20:56:33 +0200997 target_cpu_copy_regs(env, regs);
998
Alex Bennéefcedd922020-04-30 20:01:19 +0100999 if (gdbstub) {
1000 if (gdbserver_start(gdbstub) < 0) {
1001 fprintf(stderr, "qemu: could not open gdbserver on %s\n",
1002 gdbstub);
Riku Voipio4d1275c2015-09-28 16:12:16 +03001003 exit(EXIT_FAILURE);
Peter Maydellff7a9812011-09-06 14:15:50 +01001004 }
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001005 gdb_handlesig(cpu, 0);
bellard1fddef42005-04-17 19:16:13 +00001006 }
Richard Hendersone4a4aaa2022-05-01 17:21:00 -07001007
1008#ifdef CONFIG_SEMIHOSTING
1009 qemu_semihosting_guestfd_init();
1010#endif
1011
bellard1b6b0292003-03-22 17:31:38 +00001012 cpu_loop(env);
1013 /* never exits */
bellard31e31b82003-02-18 22:55:36 +00001014 return 0;
1015}