2003-02-19 01:55:36 +03:00
|
|
|
/*
|
2003-10-01 00:57:29 +04:00
|
|
|
* qemu user main
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2008-01-06 20:21:48 +03:00
|
|
|
* Copyright (c) 2003-2008 Fabrice Bellard
|
2003-02-19 01:55:36 +03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2009-07-17 00:47:01 +04:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-02-19 01:55:36 +03:00
|
|
|
*/
|
2019-05-23 17:35:05 +03:00
|
|
|
|
2016-01-26 21:17:02 +03:00
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:08 +03:00
|
|
|
#include "qemu-common.h"
|
2018-06-25 15:42:37 +03:00
|
|
|
#include "qemu/units.h"
|
2021-02-04 19:39:24 +03:00
|
|
|
#include "qemu/accel.h"
|
2019-05-23 17:35:05 +03:00
|
|
|
#include "sysemu/tcg.h"
|
2016-06-01 12:44:21 +03:00
|
|
|
#include "qemu-version.h"
|
2009-04-07 10:57:11 +04:00
|
|
|
#include <sys/syscall.h>
|
2010-03-20 00:21:13 +03:00
|
|
|
#include <sys/resource.h>
|
2020-05-13 20:51:28 +03:00
|
|
|
#include <sys/shm.h>
|
2021-02-22 13:50:04 +03:00
|
|
|
#include <linux/binfmts.h>
|
2003-02-19 01:55:36 +03:00
|
|
|
|
2016-06-15 20:27:16 +03:00
|
|
|
#include "qapi/error.h"
|
2003-03-23 23:17:16 +03:00
|
|
|
#include "qemu.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/path.h"
|
2019-08-12 08:23:49 +03:00
|
|
|
#include "qemu/queue.h"
|
2016-07-15 20:08:38 +03:00
|
|
|
#include "qemu/config-file.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/cutils.h"
|
log: Make glib logging go through QEMU
This commit adds a error_init() helper which calls
g_log_set_default_handler() so that glib logs (g_log, g_warning, ...)
are handled similarly to other QEMU logs. This means they will get a
timestamp if timestamps are enabled, and they will go through the
HMP monitor if one is configured.
This commit also adds a call to error_init() to the binaries
installed by QEMU. Since error_init() also calls error_set_progname(),
this means that *-linux-user, *-bsd-user and qemu-pr-helper messages
output with error_report, info_report, ... will slightly change: they
will be prefixed by the binary name.
glib debug messages are enabled through G_MESSAGES_DEBUG similarly to
the glib default log handler.
At the moment, this change will mostly impact SPICE logging if your
spice version is >= 0.14.1. With older spice versions, this is not going
to work as expected, but will not have any ill effect, so this call is
not conditional on the SPICE version.
Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <20190131164614.19209-3-cfergeau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2019-01-31 19:46:14 +03:00
|
|
|
#include "qemu/error-report.h"
|
2016-03-20 20:16:19 +03:00
|
|
|
#include "qemu/help_option.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2017-07-24 12:41:51 +03:00
|
|
|
#include "qemu/plugin.h"
|
2016-03-15 15:18:37 +03:00
|
|
|
#include "exec/exec-all.h"
|
2020-01-01 14:23:00 +03:00
|
|
|
#include "tcg/tcg.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
|
|
|
#include "qemu/envlist.h"
|
2019-03-14 23:10:53 +03:00
|
|
|
#include "qemu/guest-random.h"
|
2012-03-30 21:02:50 +04:00
|
|
|
#include "elf.h"
|
2016-07-15 20:08:38 +03:00
|
|
|
#include "trace/control.h"
|
2018-02-20 20:33:04 +03:00
|
|
|
#include "target_elf.h"
|
2018-04-11 21:56:33 +03:00
|
|
|
#include "cpu_loop-common.h"
|
2019-03-13 23:53:22 +03:00
|
|
|
#include "crypto/init.h"
|
2021-07-02 01:12:55 +03:00
|
|
|
#include "fd-trans.h"
|
2009-01-30 22:59:17 +03:00
|
|
|
|
2021-02-22 13:50:04 +03:00
|
|
|
#ifndef AT_FLAGS_PRESERVE_ARGV0
|
|
|
|
#define AT_FLAGS_PRESERVE_ARGV0_BIT 0
|
|
|
|
#define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT)
|
|
|
|
#endif
|
|
|
|
|
2009-01-30 23:09:01 +03:00
|
|
|
char *exec_path;
|
|
|
|
|
2009-04-06 00:08:59 +04:00
|
|
|
int singlestep;
|
2015-08-29 10:29:52 +03:00
|
|
|
static const char *argv0;
|
2020-04-30 22:01:19 +03:00
|
|
|
static const char *gdbstub;
|
2015-08-29 10:29:52 +03:00
|
|
|
static envlist_t *envlist;
|
2013-07-02 20:26:11 +04:00
|
|
|
static const char *cpu_model;
|
2018-02-07 13:40:26 +03:00
|
|
|
static const char *cpu_type;
|
2019-03-14 23:10:53 +03:00
|
|
|
static const char *seed_optarg;
|
2009-07-17 15:48:08 +04:00
|
|
|
unsigned long mmap_min_addr;
|
2021-02-12 21:48:34 +03:00
|
|
|
uintptr_t guest_base;
|
2020-05-13 20:51:29 +03:00
|
|
|
bool have_guest_base;
|
2015-11-13 15:20:35 +03:00
|
|
|
|
2020-02-04 05:54:14 +03:00
|
|
|
/*
|
|
|
|
* Used to implement backwards-compatibility for the `-strace`, and
|
|
|
|
* QEMU_STRACE options. Without this, the QEMU_LOG can be overwritten by
|
|
|
|
* -strace, or vice versa.
|
|
|
|
*/
|
|
|
|
static bool enable_strace;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The last log mask given by the user in an environment variable or argument.
|
|
|
|
* Used to support command line arguments overriding environment variables.
|
|
|
|
*/
|
|
|
|
static int last_log_mask;
|
|
|
|
|
2011-12-14 03:33:28 +04:00
|
|
|
/*
|
|
|
|
* When running 32-on-64 we should make sure we can fit all of the possible
|
|
|
|
* guest address space into a contiguous chunk of virtual host memory.
|
|
|
|
*
|
|
|
|
* This way we will never overlap with our own libraries or binaries or stack
|
|
|
|
* or anything else that QEMU maps.
|
2017-10-05 17:36:00 +03:00
|
|
|
*
|
|
|
|
* Many cpus reserve the high bit (or more than one for some 64-bit cpus)
|
|
|
|
* of the address for the kernel. Some cpus rely on this and user space
|
|
|
|
* uses the high bit(s) for pointer tagging and the like. For them, we
|
|
|
|
* must preserve the expected address space.
|
2011-12-14 03:33:28 +04:00
|
|
|
*/
|
2017-10-05 17:36:00 +03:00
|
|
|
#ifndef MAX_RESERVED_VA
|
|
|
|
# if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS
|
|
|
|
# if TARGET_VIRT_ADDR_SPACE_BITS == 32 && \
|
|
|
|
(TARGET_LONG_BITS == 32 || defined(TARGET_ABI32))
|
|
|
|
/* There are a number of places where we assign reserved_va to a variable
|
|
|
|
of type abi_ulong and expect it to fit. Avoid the last page. */
|
2019-08-22 21:59:28 +03:00
|
|
|
# define MAX_RESERVED_VA(CPU) (0xfffffffful & TARGET_PAGE_MASK)
|
2017-10-05 17:36:00 +03:00
|
|
|
# else
|
2019-08-22 21:59:28 +03:00
|
|
|
# define MAX_RESERVED_VA(CPU) (1ul << TARGET_VIRT_ADDR_SPACE_BITS)
|
2017-10-05 17:36:00 +03:00
|
|
|
# endif
|
2013-01-03 17:17:18 +04:00
|
|
|
# else
|
2019-08-22 21:59:28 +03:00
|
|
|
# define MAX_RESERVED_VA(CPU) 0
|
2013-01-03 17:17:18 +04:00
|
|
|
# endif
|
2017-10-05 17:36:00 +03:00
|
|
|
#endif
|
|
|
|
|
2010-05-29 05:27:35 +04:00
|
|
|
unsigned long reserved_va;
|
2009-04-06 00:08:59 +04:00
|
|
|
|
2015-07-06 21:03:38 +03:00
|
|
|
static void usage(int exitcode);
|
2011-08-06 10:54:12 +04:00
|
|
|
|
2010-05-26 18:08:22 +04:00
|
|
|
static const char *interp_prefix = CONFIG_QEMU_INTERP_PREFIX;
|
2014-03-04 06:28:43 +04:00
|
|
|
const char *qemu_uname_release;
|
2003-03-03 18:02:29 +03:00
|
|
|
|
2003-03-23 19:49:39 +03:00
|
|
|
/* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
|
|
|
|
we allocate a bigger stack. Need a better solution, for example
|
|
|
|
by remapping the process stack directly at the right place */
|
2010-03-20 00:21:13 +03:00
|
|
|
unsigned long guest_stack_size = 8 * 1024 * 1024UL;
|
2003-02-19 01:55:36 +03:00
|
|
|
|
2008-08-18 00:26:25 +04:00
|
|
|
#if defined(TARGET_I386)
|
2012-02-25 06:37:53 +04:00
|
|
|
int cpu_get_pic_interrupt(CPUX86State *env)
|
2003-06-24 17:30:31 +04:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2008-08-18 00:26:25 +04:00
|
|
|
#endif
|
2003-06-24 17:30:31 +04:00
|
|
|
|
2008-06-08 00:50:51 +04:00
|
|
|
/***********************************************************/
|
|
|
|
/* Helper routines for implementing atomic operations. */
|
|
|
|
|
|
|
|
/* Make sure everything is in a consistent state for calling fork(). */
|
|
|
|
void fork_start(void)
|
|
|
|
{
|
2017-12-07 15:41:21 +03:00
|
|
|
start_exclusive();
|
2009-12-04 16:16:31 +03:00
|
|
|
mmap_fork_start();
|
2017-12-04 17:22:11 +03:00
|
|
|
cpu_list_lock();
|
2008-06-08 00:50:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void fork_end(int child)
|
|
|
|
{
|
2009-12-04 16:16:31 +03:00
|
|
|
mmap_fork_end(child);
|
2008-06-08 00:50:51 +04:00
|
|
|
if (child) {
|
2013-06-25 01:50:24 +04:00
|
|
|
CPUState *cpu, *next_cpu;
|
2008-06-08 00:50:51 +04:00
|
|
|
/* Child processes created by fork() only have a single thread.
|
|
|
|
Discard information about the parent threads. */
|
2013-06-25 01:50:24 +04:00
|
|
|
CPU_FOREACH_SAFE(cpu, next_cpu) {
|
|
|
|
if (cpu != thread_cpu) {
|
2018-08-19 12:13:35 +03:00
|
|
|
QTAILQ_REMOVE_RCU(&cpus, cpu, node);
|
2013-06-25 01:50:24 +04:00
|
|
|
}
|
|
|
|
}
|
2016-08-28 04:45:14 +03:00
|
|
|
qemu_init_cpu_list();
|
2015-06-24 05:31:16 +03:00
|
|
|
gdbserver_fork(thread_cpu);
|
2017-12-07 15:41:21 +03:00
|
|
|
/* qemu_init_cpu_list() takes care of reinitializing the
|
|
|
|
* exclusive state, so we don't need to end_exclusive() here.
|
|
|
|
*/
|
2008-06-08 00:50:51 +04:00
|
|
|
} else {
|
2016-08-28 04:45:14 +03:00
|
|
|
cpu_list_unlock();
|
2017-12-07 15:41:21 +03:00
|
|
|
end_exclusive();
|
2008-06-08 00:50:51 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 16:22:46 +03:00
|
|
|
__thread CPUState *thread_cpu;
|
2003-06-25 20:18:50 +04:00
|
|
|
|
2016-08-02 20:27:39 +03:00
|
|
|
bool qemu_cpu_is_self(CPUState *cpu)
|
|
|
|
{
|
|
|
|
return thread_cpu == cpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_cpu_kick(CPUState *cpu)
|
|
|
|
{
|
|
|
|
cpu_exit(cpu);
|
|
|
|
}
|
|
|
|
|
2009-04-07 10:57:11 +04:00
|
|
|
void task_settid(TaskState *ts)
|
|
|
|
{
|
|
|
|
if (ts->ts_tid == 0) {
|
|
|
|
ts->ts_tid = (pid_t)syscall(SYS_gettid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop_all_tasks(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We trust that when using NPTL, start_exclusive()
|
|
|
|
* handles thread stopping correctly.
|
|
|
|
*/
|
|
|
|
start_exclusive();
|
|
|
|
}
|
|
|
|
|
2008-06-09 18:02:50 +04:00
|
|
|
/* Assumes contents are already zeroed. */
|
2008-05-31 20:11:38 +04:00
|
|
|
void init_task_state(TaskState *ts)
|
|
|
|
{
|
|
|
|
ts->used = 1;
|
2019-07-25 16:16:45 +03:00
|
|
|
ts->sigaltstack_used = (struct target_sigaltstack) {
|
|
|
|
.ss_sp = 0,
|
|
|
|
.ss_size = 0,
|
|
|
|
.ss_flags = TARGET_SS_DISABLE,
|
|
|
|
};
|
2008-05-31 20:11:38 +04:00
|
|
|
}
|
2011-08-06 10:54:12 +04:00
|
|
|
|
2013-07-02 19:43:21 +04:00
|
|
|
CPUArchState *cpu_copy(CPUArchState *env)
|
|
|
|
{
|
2019-03-23 02:07:18 +03:00
|
|
|
CPUState *cpu = env_cpu(env);
|
2018-02-07 13:40:26 +03:00
|
|
|
CPUState *new_cpu = cpu_create(cpu_type);
|
2015-03-23 15:55:52 +03:00
|
|
|
CPUArchState *new_env = new_cpu->env_ptr;
|
2013-07-02 19:43:21 +04:00
|
|
|
CPUBreakpoint *bp;
|
|
|
|
|
|
|
|
/* Reset non arch specific state */
|
2013-09-02 18:57:02 +04:00
|
|
|
cpu_reset(new_cpu);
|
2013-07-02 19:43:21 +04:00
|
|
|
|
2021-03-02 06:21:08 +03:00
|
|
|
new_cpu->tcg_cflags = cpu->tcg_cflags;
|
2013-07-02 19:43:21 +04:00
|
|
|
memcpy(new_env, env, sizeof(CPUArchState));
|
|
|
|
|
|
|
|
/* Clone all break/watchpoints.
|
|
|
|
Note: Once we support ptrace with hw-debug register access, make sure
|
|
|
|
BP_CPU break/watchpoints are handled correctly on clone. */
|
2015-06-12 12:24:10 +03:00
|
|
|
QTAILQ_INIT(&new_cpu->breakpoints);
|
2013-08-26 23:22:53 +04:00
|
|
|
QTAILQ_FOREACH(bp, &cpu->breakpoints, entry) {
|
2013-09-02 19:26:20 +04:00
|
|
|
cpu_breakpoint_insert(new_cpu, bp->pc, bp->flags, NULL);
|
2013-07-02 19:43:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return new_env;
|
|
|
|
}
|
|
|
|
|
2011-08-06 10:54:12 +04:00
|
|
|
static void handle_arg_help(const char *arg)
|
|
|
|
{
|
2015-09-28 16:12:16 +03:00
|
|
|
usage(EXIT_SUCCESS);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_log(const char *arg)
|
|
|
|
{
|
2020-02-04 05:54:14 +03:00
|
|
|
last_log_mask = qemu_str_to_log_mask(arg);
|
|
|
|
if (!last_log_mask) {
|
2013-02-11 20:41:21 +04:00
|
|
|
qemu_print_log_usage(stdout);
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 13:35:14 +03:00
|
|
|
static void handle_arg_dfilter(const char *arg)
|
|
|
|
{
|
2019-08-30 17:36:48 +03:00
|
|
|
qemu_set_dfilter_ranges(arg, &error_fatal);
|
2017-10-17 13:35:14 +03:00
|
|
|
}
|
|
|
|
|
2011-11-08 13:46:44 +04:00
|
|
|
static void handle_arg_log_filename(const char *arg)
|
|
|
|
{
|
2016-06-15 20:27:16 +03:00
|
|
|
qemu_set_log_filename(arg, &error_fatal);
|
2011-11-08 13:46:44 +04:00
|
|
|
}
|
|
|
|
|
2011-08-06 10:54:12 +04:00
|
|
|
static void handle_arg_set_env(const char *arg)
|
|
|
|
{
|
|
|
|
char *r, *p, *token;
|
|
|
|
r = p = strdup(arg);
|
|
|
|
while ((token = strsep(&p, ",")) != NULL) {
|
|
|
|
if (envlist_setenv(envlist, token) != 0) {
|
2015-09-28 16:12:16 +03:00
|
|
|
usage(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_unset_env(const char *arg)
|
|
|
|
{
|
|
|
|
char *r, *p, *token;
|
|
|
|
r = p = strdup(arg);
|
|
|
|
while ((token = strsep(&p, ",")) != NULL) {
|
|
|
|
if (envlist_unsetenv(envlist, token) != 0) {
|
2015-09-28 16:12:16 +03:00
|
|
|
usage(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_argv0(const char *arg)
|
|
|
|
{
|
|
|
|
argv0 = strdup(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_stack_size(const char *arg)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
guest_stack_size = strtoul(arg, &p, 0);
|
|
|
|
if (guest_stack_size == 0) {
|
2015-09-28 16:12:16 +03:00
|
|
|
usage(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == 'M') {
|
2018-06-25 15:42:37 +03:00
|
|
|
guest_stack_size *= MiB;
|
2011-08-06 10:54:12 +04:00
|
|
|
} else if (*p == 'k' || *p == 'K') {
|
2018-06-25 15:42:37 +03:00
|
|
|
guest_stack_size *= KiB;
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_ld_prefix(const char *arg)
|
|
|
|
{
|
|
|
|
interp_prefix = strdup(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_pagesize(const char *arg)
|
|
|
|
{
|
|
|
|
qemu_host_page_size = atoi(arg);
|
|
|
|
if (qemu_host_page_size == 0 ||
|
|
|
|
(qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
|
|
|
|
fprintf(stderr, "page size must be a power of two\n");
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 23:10:53 +03:00
|
|
|
static void handle_arg_seed(const char *arg)
|
2014-10-14 19:18:17 +04:00
|
|
|
{
|
2019-03-14 23:10:53 +03:00
|
|
|
seed_optarg = arg;
|
2014-10-14 19:18:17 +04:00
|
|
|
}
|
|
|
|
|
2011-08-06 10:54:12 +04:00
|
|
|
static void handle_arg_gdb(const char *arg)
|
|
|
|
{
|
2020-04-30 22:01:19 +03:00
|
|
|
gdbstub = g_strdup(arg);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_uname(const char *arg)
|
|
|
|
{
|
|
|
|
qemu_uname_release = strdup(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_cpu(const char *arg)
|
|
|
|
{
|
|
|
|
cpu_model = strdup(arg);
|
2012-08-02 16:45:54 +04:00
|
|
|
if (cpu_model == NULL || is_help_option(cpu_model)) {
|
2011-08-06 10:54:12 +04:00
|
|
|
/* XXX: implement xxx_cpu_list for targets that still miss it */
|
2012-09-06 00:41:08 +04:00
|
|
|
#if defined(cpu_list)
|
2019-04-17 22:17:57 +03:00
|
|
|
cpu_list();
|
2011-08-06 10:54:12 +04:00
|
|
|
#endif
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_guest_base(const char *arg)
|
|
|
|
{
|
|
|
|
guest_base = strtol(arg, NULL, 0);
|
2020-05-13 20:51:29 +03:00
|
|
|
have_guest_base = true;
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_reserved_va(const char *arg)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int shift = 0;
|
|
|
|
reserved_va = strtoul(arg, &p, 0);
|
|
|
|
switch (*p) {
|
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
shift = 10;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
shift = 20;
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
shift = 30;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (shift) {
|
|
|
|
unsigned long unshifted = reserved_va;
|
|
|
|
p++;
|
|
|
|
reserved_va <<= shift;
|
2019-08-22 21:59:28 +03:00
|
|
|
if (reserved_va >> shift != unshifted) {
|
2011-08-06 10:54:12 +04:00
|
|
|
fprintf(stderr, "Reserved virtual address too big\n");
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*p) {
|
|
|
|
fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p);
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_singlestep(const char *arg)
|
|
|
|
{
|
|
|
|
singlestep = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_strace(const char *arg)
|
|
|
|
{
|
2020-02-04 05:54:14 +03:00
|
|
|
enable_strace = true;
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_arg_version(const char *arg)
|
|
|
|
{
|
2018-02-15 14:06:47 +03:00
|
|
|
printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
|
2016-10-05 12:54:44 +03:00
|
|
|
"\n" QEMU_COPYRIGHT "\n");
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_SUCCESS);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
|
2016-07-15 20:08:38 +03:00
|
|
|
static void handle_arg_trace(const char *arg)
|
|
|
|
{
|
2020-11-02 14:58:41 +03:00
|
|
|
trace_opt_parse(arg);
|
2016-07-15 20:08:38 +03:00
|
|
|
}
|
|
|
|
|
2019-09-06 19:57:13 +03:00
|
|
|
#if defined(TARGET_XTENSA)
|
|
|
|
static void handle_arg_abi_call0(const char *arg)
|
|
|
|
{
|
|
|
|
xtensa_set_abi_call0();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-07-24 12:41:51 +03:00
|
|
|
static QemuPluginList plugins = QTAILQ_HEAD_INITIALIZER(plugins);
|
|
|
|
|
|
|
|
#ifdef CONFIG_PLUGIN
|
|
|
|
static void handle_arg_plugin(const char *arg)
|
|
|
|
{
|
|
|
|
qemu_plugin_opt_parse(arg, &plugins);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-08-06 10:54:12 +04:00
|
|
|
struct qemu_argument {
|
|
|
|
const char *argv;
|
|
|
|
const char *env;
|
|
|
|
bool has_arg;
|
|
|
|
void (*handle_opt)(const char *arg);
|
|
|
|
const char *example;
|
|
|
|
const char *help;
|
|
|
|
};
|
|
|
|
|
2012-05-21 23:56:19 +04:00
|
|
|
static const struct qemu_argument arg_table[] = {
|
2011-08-06 10:54:12 +04:00
|
|
|
{"h", "", false, handle_arg_help,
|
|
|
|
"", "print this help"},
|
2015-07-06 21:03:39 +03:00
|
|
|
{"help", "", false, handle_arg_help,
|
|
|
|
"", ""},
|
2011-08-06 10:54:12 +04:00
|
|
|
{"g", "QEMU_GDB", true, handle_arg_gdb,
|
|
|
|
"port", "wait gdb connection to 'port'"},
|
|
|
|
{"L", "QEMU_LD_PREFIX", true, handle_arg_ld_prefix,
|
|
|
|
"path", "set the elf interpreter prefix to 'path'"},
|
|
|
|
{"s", "QEMU_STACK_SIZE", true, handle_arg_stack_size,
|
|
|
|
"size", "set the stack size to 'size' bytes"},
|
|
|
|
{"cpu", "QEMU_CPU", true, handle_arg_cpu,
|
2012-08-02 16:45:54 +04:00
|
|
|
"model", "select CPU (-cpu help for list)"},
|
2011-08-06 10:54:12 +04:00
|
|
|
{"E", "QEMU_SET_ENV", true, handle_arg_set_env,
|
|
|
|
"var=value", "sets targets environment variable (see below)"},
|
|
|
|
{"U", "QEMU_UNSET_ENV", true, handle_arg_unset_env,
|
|
|
|
"var", "unsets targets environment variable (see below)"},
|
|
|
|
{"0", "QEMU_ARGV0", true, handle_arg_argv0,
|
|
|
|
"argv0", "forces target process argv[0] to be 'argv0'"},
|
|
|
|
{"r", "QEMU_UNAME", true, handle_arg_uname,
|
|
|
|
"uname", "set qemu uname release string to 'uname'"},
|
|
|
|
{"B", "QEMU_GUEST_BASE", true, handle_arg_guest_base,
|
|
|
|
"address", "set guest_base address to 'address'"},
|
|
|
|
{"R", "QEMU_RESERVED_VA", true, handle_arg_reserved_va,
|
|
|
|
"size", "reserve 'size' bytes for guest virtual address space"},
|
|
|
|
{"d", "QEMU_LOG", true, handle_arg_log,
|
2013-02-26 21:52:40 +04:00
|
|
|
"item[,...]", "enable logging of specified items "
|
|
|
|
"(use '-d help' for a list of items)"},
|
2017-10-17 13:35:14 +03:00
|
|
|
{"dfilter", "QEMU_DFILTER", true, handle_arg_dfilter,
|
|
|
|
"range[,...]","filter logging based on address range"},
|
2011-11-08 13:46:44 +04:00
|
|
|
{"D", "QEMU_LOG_FILENAME", true, handle_arg_log_filename,
|
2013-02-26 21:52:40 +04:00
|
|
|
"logfile", "write logs to 'logfile' (default stderr)"},
|
2011-08-06 10:54:12 +04:00
|
|
|
{"p", "QEMU_PAGESIZE", true, handle_arg_pagesize,
|
|
|
|
"pagesize", "set the host page size to 'pagesize'"},
|
|
|
|
{"singlestep", "QEMU_SINGLESTEP", false, handle_arg_singlestep,
|
|
|
|
"", "run in singlestep mode"},
|
|
|
|
{"strace", "QEMU_STRACE", false, handle_arg_strace,
|
|
|
|
"", "log system calls"},
|
2019-03-14 23:10:53 +03:00
|
|
|
{"seed", "QEMU_RAND_SEED", true, handle_arg_seed,
|
2014-10-14 19:18:17 +04:00
|
|
|
"", "Seed for pseudo-random number generator"},
|
2016-07-15 20:08:38 +03:00
|
|
|
{"trace", "QEMU_TRACE", true, handle_arg_trace,
|
|
|
|
"", "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
|
2017-07-24 12:41:51 +03:00
|
|
|
#ifdef CONFIG_PLUGIN
|
|
|
|
{"plugin", "QEMU_PLUGIN", true, handle_arg_plugin,
|
|
|
|
"", "[file=]<file>[,arg=<string>]"},
|
|
|
|
#endif
|
2011-08-06 10:54:12 +04:00
|
|
|
{"version", "QEMU_VERSION", false, handle_arg_version,
|
2011-09-29 18:48:12 +04:00
|
|
|
"", "display version information and exit"},
|
2019-09-06 19:57:13 +03:00
|
|
|
#if defined(TARGET_XTENSA)
|
|
|
|
{"xtensa-abi-call0", "QEMU_XTENSA_ABI_CALL0", false, handle_arg_abi_call0,
|
|
|
|
"", "assume CALL0 Xtensa ABI"},
|
|
|
|
#endif
|
2011-08-06 10:54:12 +04:00
|
|
|
{NULL, NULL, false, NULL, NULL, NULL}
|
|
|
|
};
|
|
|
|
|
2015-07-06 21:03:38 +03:00
|
|
|
static void usage(int exitcode)
|
2011-08-06 10:54:12 +04:00
|
|
|
{
|
2012-05-21 23:56:19 +04:00
|
|
|
const struct qemu_argument *arginfo;
|
2011-08-06 10:54:12 +04:00
|
|
|
int maxarglen;
|
|
|
|
int maxenvlen;
|
|
|
|
|
2013-06-04 16:45:27 +04:00
|
|
|
printf("usage: qemu-" TARGET_NAME " [options] program [arguments...]\n"
|
|
|
|
"Linux CPU emulator (compiled for " TARGET_NAME " emulation)\n"
|
2011-08-06 10:54:12 +04:00
|
|
|
"\n"
|
|
|
|
"Options and associated environment variables:\n"
|
|
|
|
"\n");
|
|
|
|
|
2013-02-14 12:46:43 +04:00
|
|
|
/* Calculate column widths. We must always have at least enough space
|
|
|
|
* for the column header.
|
|
|
|
*/
|
|
|
|
maxarglen = strlen("Argument");
|
|
|
|
maxenvlen = strlen("Env-variable");
|
2011-08-06 10:54:12 +04:00
|
|
|
|
|
|
|
for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
|
2013-02-14 12:46:43 +04:00
|
|
|
int arglen = strlen(arginfo->argv);
|
|
|
|
if (arginfo->has_arg) {
|
|
|
|
arglen += strlen(arginfo->example) + 1;
|
|
|
|
}
|
2011-08-06 10:54:12 +04:00
|
|
|
if (strlen(arginfo->env) > maxenvlen) {
|
|
|
|
maxenvlen = strlen(arginfo->env);
|
|
|
|
}
|
2013-02-14 12:46:43 +04:00
|
|
|
if (arglen > maxarglen) {
|
|
|
|
maxarglen = arglen;
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 12:46:43 +04:00
|
|
|
printf("%-*s %-*s Description\n", maxarglen+1, "Argument",
|
|
|
|
maxenvlen, "Env-variable");
|
2011-08-06 10:54:12 +04:00
|
|
|
|
|
|
|
for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
|
|
|
|
if (arginfo->has_arg) {
|
|
|
|
printf("-%s %-*s %-*s %s\n", arginfo->argv,
|
2013-02-14 12:46:43 +04:00
|
|
|
(int)(maxarglen - strlen(arginfo->argv) - 1),
|
|
|
|
arginfo->example, maxenvlen, arginfo->env, arginfo->help);
|
2011-08-06 10:54:12 +04:00
|
|
|
} else {
|
2013-02-14 12:46:43 +04:00
|
|
|
printf("-%-*s %-*s %s\n", maxarglen, arginfo->argv,
|
2011-08-06 10:54:12 +04:00
|
|
|
maxenvlen, arginfo->env,
|
|
|
|
arginfo->help);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n"
|
|
|
|
"Defaults:\n"
|
|
|
|
"QEMU_LD_PREFIX = %s\n"
|
2013-02-26 21:52:40 +04:00
|
|
|
"QEMU_STACK_SIZE = %ld byte\n",
|
2011-08-06 10:54:12 +04:00
|
|
|
interp_prefix,
|
2013-02-26 21:52:40 +04:00
|
|
|
guest_stack_size);
|
2011-08-06 10:54:12 +04:00
|
|
|
|
|
|
|
printf("\n"
|
|
|
|
"You can use -E and -U options or the QEMU_SET_ENV and\n"
|
|
|
|
"QEMU_UNSET_ENV environment variables to set and unset\n"
|
|
|
|
"environment variables for the target process.\n"
|
|
|
|
"It is possible to provide several variables by separating them\n"
|
|
|
|
"by commas in getsubopt(3) style. Additionally it is possible to\n"
|
|
|
|
"provide the -E and -U options multiple times.\n"
|
|
|
|
"The following lines are equivalent:\n"
|
|
|
|
" -E var1=val2 -E var2=val2 -U LD_PRELOAD -U LD_DEBUG\n"
|
|
|
|
" -E var1=val2,var2=val2 -U LD_PRELOAD,LD_DEBUG\n"
|
|
|
|
" QEMU_SET_ENV=var1=val2,var2=val2 QEMU_UNSET_ENV=LD_PRELOAD,LD_DEBUG\n"
|
|
|
|
"Note that if you provide several changes to a single variable\n"
|
2017-08-03 19:33:53 +03:00
|
|
|
"the last change will stay in effect.\n"
|
|
|
|
"\n"
|
|
|
|
QEMU_HELP_BOTTOM "\n");
|
2011-08-06 10:54:12 +04:00
|
|
|
|
2015-07-06 21:03:38 +03:00
|
|
|
exit(exitcode);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_args(int argc, char **argv)
|
|
|
|
{
|
|
|
|
const char *r;
|
|
|
|
int optind;
|
2012-05-21 23:56:19 +04:00
|
|
|
const struct qemu_argument *arginfo;
|
2011-08-06 10:54:12 +04:00
|
|
|
|
|
|
|
for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
|
|
|
|
if (arginfo->env == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = getenv(arginfo->env);
|
|
|
|
if (r != NULL) {
|
|
|
|
arginfo->handle_opt(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
optind = 1;
|
|
|
|
for (;;) {
|
|
|
|
if (optind >= argc) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
r = argv[optind];
|
|
|
|
if (r[0] != '-') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
optind++;
|
|
|
|
r++;
|
|
|
|
if (!strcmp(r, "-")) {
|
|
|
|
break;
|
|
|
|
}
|
2015-07-06 21:03:41 +03:00
|
|
|
/* Treat --foo the same as -foo. */
|
|
|
|
if (r[0] == '-') {
|
|
|
|
r++;
|
|
|
|
}
|
2011-08-06 10:54:12 +04:00
|
|
|
|
|
|
|
for (arginfo = arg_table; arginfo->handle_opt != NULL; arginfo++) {
|
|
|
|
if (!strcmp(r, arginfo->argv)) {
|
|
|
|
if (arginfo->has_arg) {
|
2011-09-29 18:48:12 +04:00
|
|
|
if (optind >= argc) {
|
2015-07-06 21:03:40 +03:00
|
|
|
(void) fprintf(stderr,
|
|
|
|
"qemu: missing argument for option '%s'\n", r);
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-09-29 18:48:12 +04:00
|
|
|
}
|
|
|
|
arginfo->handle_opt(argv[optind]);
|
2011-08-06 10:54:12 +04:00
|
|
|
optind++;
|
2011-09-29 18:48:12 +04:00
|
|
|
} else {
|
|
|
|
arginfo->handle_opt(NULL);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no option matched the current argv */
|
|
|
|
if (arginfo->handle_opt == NULL) {
|
2015-07-06 21:03:40 +03:00
|
|
|
(void) fprintf(stderr, "qemu: unknown option '%s'\n", r);
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind >= argc) {
|
2015-07-06 21:03:40 +03:00
|
|
|
(void) fprintf(stderr, "qemu: no user program specified\n");
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-08-06 10:54:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
exec_path = argv[optind];
|
|
|
|
|
|
|
|
return optind;
|
|
|
|
}
|
|
|
|
|
2008-12-10 22:18:40 +03:00
|
|
|
int main(int argc, char **argv, char **envp)
|
2003-02-19 01:55:36 +03:00
|
|
|
{
|
2003-02-19 02:00:51 +03:00
|
|
|
struct target_pt_regs regs1, *regs = ®s1;
|
2003-02-19 01:55:36 +03:00
|
|
|
struct image_info info1, *info = &info1;
|
2009-04-07 10:57:11 +04:00
|
|
|
struct linux_binprm bprm;
|
linux-user: fix memory leaks with NPTL emulation
Running programs that create large numbers of threads, such as this
snippet from libstdc++'s pthread7-rope.cc:
const int max_thread_count = 4;
const int max_loop_count = 10000;
...
for (int j = 0; j < max_loop_count; j++)
{
...
for (int i = 0; i < max_thread_count; i++)
pthread_create (&tid[i], NULL, thread_main, 0);
for (int i = 0; i < max_thread_count; i++)
pthread_join (tid[i], NULL);
}
in user-mode emulation will quickly run out of memory. This is caused
by a failure to free memory in do_syscall prior to thread exit:
/* TODO: Free CPU state. */
pthread_exit(NULL);
The first step in fixing this is to make all TaskStates used by QEMU
dynamically allocated. The TaskState used by the initial thread was
not, as it was allocated on main's stack. So fix that, free the
cpu_env, free the TaskState, and we're home free, right?
Not exactly. When we create a thread, we do:
ts = qemu_mallocz(sizeof(TaskState) + NEW_STACK_SIZE);
...
new_stack = ts->stack;
...
ret = pthread_attr_setstack(&attr, new_stack, NEW_STACK_SIZE);
If we blindly free the TaskState, then, we yank the current (host)
thread's stack out from underneath it while it still has things to do,
like calling pthread_exit. That causes problems, as you might expect.
The solution adopted here is to let the C library allocate the thread's
stack (so the C library can properly clean it up at pthread_exit) and
provide a hint that we want NEW_STACK_SIZE bytes of stack.
With those two changes, we're done, right? Well, almost. You see,
we're creating all these host threads and their parent threads never
bother to check that their children are finished. There's no good place
for the parent threads to do so. Therefore, we need to create the
threads in a detached state so the parent thread doesn't have to call
pthread_join on the child to release the child's resources; the child
does so automatically.
With those three major changes, we can comfortably run programs like the
above without exhausting memory. We do need to delete 'stack' from the
TaskState structure.
Signed-off-by: Nathan Froyd <froydnj@codesourcery.com>
Signed-off-by: Riku Voipio <riku.voipio@nokia.com>
2010-10-29 18:48:57 +04:00
|
|
|
TaskState *ts;
|
2012-03-14 04:38:32 +04:00
|
|
|
CPUArchState *env;
|
2013-06-27 21:49:31 +04:00
|
|
|
CPUState *cpu;
|
2003-03-03 18:02:29 +03:00
|
|
|
int optind;
|
2009-01-30 22:59:17 +03:00
|
|
|
char **target_environ, **wrk;
|
2009-04-15 20:11:52 +04:00
|
|
|
char **target_argv;
|
|
|
|
int target_argc;
|
|
|
|
int i;
|
2009-06-19 11:39:36 +04:00
|
|
|
int ret;
|
2013-08-30 03:46:44 +04:00
|
|
|
int execfd;
|
2020-02-04 05:54:14 +03:00
|
|
|
int log_mask;
|
2019-08-22 21:59:28 +03:00
|
|
|
unsigned long max_reserved_va;
|
2021-02-22 13:50:04 +03:00
|
|
|
bool preserve_argv0;
|
2007-06-17 20:38:39 +04:00
|
|
|
|
log: Make glib logging go through QEMU
This commit adds a error_init() helper which calls
g_log_set_default_handler() so that glib logs (g_log, g_warning, ...)
are handled similarly to other QEMU logs. This means they will get a
timestamp if timestamps are enabled, and they will go through the
HMP monitor if one is configured.
This commit also adds a call to error_init() to the binaries
installed by QEMU. Since error_init() also calls error_set_progname(),
this means that *-linux-user, *-bsd-user and qemu-pr-helper messages
output with error_report, info_report, ... will slightly change: they
will be prefixed by the binary name.
glib debug messages are enabled through G_MESSAGES_DEBUG similarly to
the glib default log handler.
At the moment, this change will mostly impact SPICE logging if your
spice version is >= 0.14.1. With older spice versions, this is not going
to work as expected, but will not have any ill effect, so this call is
not conditional on the SPICE version.
Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-Id: <20190131164614.19209-3-cfergeau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
2019-01-31 19:46:14 +03:00
|
|
|
error_init(argv[0]);
|
2016-10-04 16:35:52 +03:00
|
|
|
module_call_init(MODULE_INIT_TRACE);
|
2016-08-28 04:45:14 +03:00
|
|
|
qemu_init_cpu_list();
|
2012-03-05 00:32:36 +04:00
|
|
|
module_call_init(MODULE_INIT_QOM);
|
|
|
|
|
2017-03-20 20:38:28 +03:00
|
|
|
envlist = envlist_create();
|
2009-01-30 22:59:17 +03:00
|
|
|
|
|
|
|
/* add current environment into the list */
|
|
|
|
for (wrk = environ; *wrk != NULL; wrk++) {
|
|
|
|
(void) envlist_setenv(envlist, *wrk);
|
|
|
|
}
|
|
|
|
|
2010-03-20 00:21:13 +03:00
|
|
|
/* Read the stack limit from the kernel. If it's "unlimited",
|
|
|
|
then we can do little else besides use the default. */
|
|
|
|
{
|
|
|
|
struct rlimit lim;
|
|
|
|
if (getrlimit(RLIMIT_STACK, &lim) == 0
|
2010-04-11 23:07:35 +04:00
|
|
|
&& lim.rlim_cur != RLIM_INFINITY
|
|
|
|
&& lim.rlim_cur == (target_long)lim.rlim_cur) {
|
2010-03-20 00:21:13 +03:00
|
|
|
guest_stack_size = lim.rlim_cur;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-19 11:08:28 +03:00
|
|
|
cpu_model = NULL;
|
2010-02-20 20:14:59 +03:00
|
|
|
|
2016-07-15 20:08:38 +03:00
|
|
|
qemu_add_opts(&qemu_trace_opts);
|
2017-07-24 12:41:51 +03:00
|
|
|
qemu_plugin_add_opts();
|
2016-07-15 20:08:38 +03:00
|
|
|
|
2011-08-06 10:54:12 +04:00
|
|
|
optind = parse_args(argc, argv);
|
2003-03-03 18:02:29 +03:00
|
|
|
|
2020-02-04 05:54:14 +03:00
|
|
|
log_mask = last_log_mask | (enable_strace ? LOG_STRACE : 0);
|
|
|
|
if (log_mask) {
|
|
|
|
qemu_log_needs_buffers();
|
|
|
|
qemu_set_log(log_mask);
|
|
|
|
}
|
|
|
|
|
2016-07-15 20:08:38 +03:00
|
|
|
if (!trace_init_backends()) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2020-11-02 14:58:41 +03:00
|
|
|
trace_init_file();
|
2020-10-27 11:58:26 +03:00
|
|
|
qemu_plugin_load_list(&plugins, &error_fatal);
|
2016-07-15 20:08:38 +03:00
|
|
|
|
2003-02-19 01:55:36 +03:00
|
|
|
/* Zero out regs */
|
2003-02-19 02:00:51 +03:00
|
|
|
memset(regs, 0, sizeof(struct target_pt_regs));
|
2003-02-19 01:55:36 +03:00
|
|
|
|
|
|
|
/* Zero out image_info */
|
|
|
|
memset(info, 0, sizeof(struct image_info));
|
|
|
|
|
2009-04-07 10:57:11 +04:00
|
|
|
memset(&bprm, 0, sizeof (bprm));
|
|
|
|
|
2003-04-11 04:13:41 +04:00
|
|
|
/* Scan interp_prefix dir for replacement files. */
|
|
|
|
init_paths(interp_prefix);
|
|
|
|
|
2013-09-03 23:12:20 +04:00
|
|
|
init_qemu_uname_release();
|
|
|
|
|
2021-02-22 13:50:04 +03:00
|
|
|
/*
|
|
|
|
* Manage binfmt-misc open-binary flag
|
|
|
|
*/
|
2018-02-20 20:33:05 +03:00
|
|
|
execfd = qemu_getauxval(AT_EXECFD);
|
|
|
|
if (execfd == 0) {
|
2019-07-14 16:40:27 +03:00
|
|
|
execfd = open(exec_path, O_RDONLY);
|
2018-02-20 20:33:05 +03:00
|
|
|
if (execfd < 0) {
|
2019-07-14 16:40:27 +03:00
|
|
|
printf("Error while loading %s: %s\n", exec_path, strerror(errno));
|
2018-02-20 20:33:05 +03:00
|
|
|
_exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 13:50:04 +03:00
|
|
|
/*
|
|
|
|
* get binfmt_misc flags
|
|
|
|
*/
|
|
|
|
preserve_argv0 = !!(qemu_getauxval(AT_FLAGS) & AT_FLAGS_PRESERVE_ARGV0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Manage binfmt-misc preserve-arg[0] flag
|
|
|
|
* argv[optind] full path to the binary
|
|
|
|
* argv[optind + 1] original argv[0]
|
|
|
|
*/
|
|
|
|
if (optind + 1 < argc && preserve_argv0) {
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
2007-11-08 16:56:19 +03:00
|
|
|
if (cpu_model == NULL) {
|
2018-02-20 20:33:05 +03:00
|
|
|
cpu_model = cpu_get_model(get_elf_eflags(execfd));
|
2007-11-10 18:15:54 +03:00
|
|
|
}
|
2019-04-17 05:59:40 +03:00
|
|
|
cpu_type = parse_cpu_option(cpu_model);
|
2018-02-07 13:40:26 +03:00
|
|
|
|
2018-05-17 14:51:17 +03:00
|
|
|
/* init tcg before creating CPUs and to get qemu_host_page_size */
|
2021-02-04 19:39:24 +03:00
|
|
|
{
|
|
|
|
AccelClass *ac = ACCEL_GET_CLASS(current_accel());
|
2018-02-07 13:40:26 +03:00
|
|
|
|
2021-02-04 19:39:25 +03:00
|
|
|
accel_init_interfaces(ac);
|
2021-03-22 16:27:58 +03:00
|
|
|
ac->init_machine(NULL);
|
2021-02-04 19:39:24 +03:00
|
|
|
}
|
2018-02-07 13:40:26 +03:00
|
|
|
cpu = cpu_create(cpu_type);
|
2015-02-26 23:37:49 +03:00
|
|
|
env = cpu->env_ptr;
|
2013-07-26 18:42:25 +04:00
|
|
|
cpu_reset(cpu);
|
2013-06-27 21:49:31 +04:00
|
|
|
thread_cpu = cpu;
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2019-08-22 21:59:28 +03:00
|
|
|
/*
|
|
|
|
* Reserving too much vm space via mmap can run into problems
|
|
|
|
* with rlimits, oom due to page table creation, etc. We will
|
|
|
|
* still try it, if directed by the command-line option, but
|
|
|
|
* not by default.
|
|
|
|
*/
|
|
|
|
max_reserved_va = MAX_RESERVED_VA(cpu);
|
|
|
|
if (reserved_va != 0) {
|
|
|
|
if (max_reserved_va && reserved_va > max_reserved_va) {
|
|
|
|
fprintf(stderr, "Reserved virtual address too big\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else if (HOST_LONG_BITS == 64 && TARGET_VIRT_ADDR_SPACE_BITS <= 32) {
|
|
|
|
/*
|
|
|
|
* reserved_va must be aligned with the host page size
|
|
|
|
* as it is used with mmap()
|
|
|
|
*/
|
|
|
|
reserved_va = max_reserved_va & qemu_host_page_mask;
|
|
|
|
}
|
|
|
|
|
2019-03-13 23:53:22 +03:00
|
|
|
{
|
|
|
|
Error *err = NULL;
|
|
|
|
if (seed_optarg != NULL) {
|
|
|
|
qemu_guest_random_seed_main(seed_optarg, &err);
|
|
|
|
} else {
|
|
|
|
qcrypto_init(&err);
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
error_reportf_err(err, "cannot initialize crypto: ");
|
|
|
|
exit(1);
|
2019-03-14 23:10:53 +03:00
|
|
|
}
|
2014-10-14 19:18:17 +04:00
|
|
|
}
|
|
|
|
|
2009-01-30 22:59:17 +03:00
|
|
|
target_environ = envlist_to_environ(envlist, NULL);
|
|
|
|
envlist_free(envlist);
|
2007-06-17 20:38:39 +04:00
|
|
|
|
2009-07-17 15:48:08 +04:00
|
|
|
/*
|
|
|
|
* Read in mmap_min_addr kernel parameter. This value is used
|
|
|
|
* When loading the ELF image to determine whether guest_base
|
2010-03-11 02:39:07 +03:00
|
|
|
* is needed. It is also used in mmap_find_vma.
|
2009-07-17 15:48:08 +04:00
|
|
|
*/
|
2010-03-11 02:39:07 +03:00
|
|
|
{
|
2009-07-17 15:48:08 +04:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
|
|
|
|
unsigned long tmp;
|
2020-07-25 00:23:14 +03:00
|
|
|
if (fscanf(fp, "%lu", &tmp) == 1 && tmp != 0) {
|
2009-07-17 15:48:08 +04:00
|
|
|
mmap_min_addr = tmp;
|
2020-07-25 00:23:14 +03:00
|
|
|
qemu_log_mask(CPU_LOG_PAGE, "host mmap_min_addr=0x%lx\n",
|
|
|
|
mmap_min_addr);
|
2009-07-17 15:48:08 +04:00
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 00:23:14 +03:00
|
|
|
/*
|
|
|
|
* We prefer to not make NULL pointers accessible to QEMU.
|
|
|
|
* If we're in a chroot with no /proc, fall back to 1 page.
|
|
|
|
*/
|
|
|
|
if (mmap_min_addr == 0) {
|
|
|
|
mmap_min_addr = qemu_host_page_size;
|
|
|
|
qemu_log_mask(CPU_LOG_PAGE,
|
|
|
|
"host mmap_min_addr=0x%lx (fallback)\n",
|
|
|
|
mmap_min_addr);
|
|
|
|
}
|
|
|
|
|
2009-04-15 20:11:52 +04:00
|
|
|
/*
|
|
|
|
* Prepare copy of argv vector for target.
|
|
|
|
*/
|
|
|
|
target_argc = argc - optind;
|
|
|
|
target_argv = calloc(target_argc + 1, sizeof (char *));
|
|
|
|
if (target_argv == NULL) {
|
2018-12-14 01:37:37 +03:00
|
|
|
(void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2009-04-15 20:11:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If argv0 is specified (using '-0' switch) we replace
|
|
|
|
* argv[0] pointer with the given one.
|
|
|
|
*/
|
|
|
|
i = 0;
|
|
|
|
if (argv0 != NULL) {
|
|
|
|
target_argv[i++] = strdup(argv0);
|
|
|
|
}
|
|
|
|
for (; i < target_argc; i++) {
|
|
|
|
target_argv[i] = strdup(argv[optind + i]);
|
|
|
|
}
|
|
|
|
target_argv[target_argc] = NULL;
|
|
|
|
|
2015-09-14 14:53:03 +03:00
|
|
|
ts = g_new0(TaskState, 1);
|
2009-04-07 10:57:11 +04:00
|
|
|
init_task_state(ts);
|
|
|
|
/* build Task State */
|
|
|
|
ts->info = info;
|
|
|
|
ts->bprm = &bprm;
|
2013-08-26 20:14:44 +04:00
|
|
|
cpu->opaque = ts;
|
2009-04-07 10:57:11 +04:00
|
|
|
task_settid(ts);
|
|
|
|
|
2021-07-02 01:12:55 +03:00
|
|
|
fd_trans_init();
|
|
|
|
|
2019-07-14 16:40:27 +03:00
|
|
|
ret = loader_exec(execfd, exec_path, target_argv, target_environ, regs,
|
2009-06-19 11:39:36 +04:00
|
|
|
info, &bprm);
|
|
|
|
if (ret != 0) {
|
2019-07-14 16:40:27 +03:00
|
|
|
printf("Error while loading %s: %s\n", exec_path, strerror(-ret));
|
2015-09-28 16:12:16 +03:00
|
|
|
_exit(EXIT_FAILURE);
|
2007-06-17 20:38:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (wrk = target_environ; *wrk; wrk++) {
|
2017-03-20 20:38:28 +03:00
|
|
|
g_free(*wrk);
|
2003-02-19 01:55:36 +03:00
|
|
|
}
|
2007-09-17 12:09:54 +04:00
|
|
|
|
2017-03-20 20:38:28 +03:00
|
|
|
g_free(target_environ);
|
2007-06-17 20:38:39 +04:00
|
|
|
|
2015-11-13 14:32:19 +03:00
|
|
|
if (qemu_loglevel_mask(CPU_LOG_PAGE)) {
|
2021-02-12 21:48:34 +03:00
|
|
|
qemu_log("guest_base %p\n", (void *)guest_base);
|
2019-12-05 15:25:15 +03:00
|
|
|
log_page_dump("binary load");
|
2009-01-20 19:57:34 +03:00
|
|
|
|
|
|
|
qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
|
|
|
|
qemu_log("end_code 0x" TARGET_ABI_FMT_lx "\n", info->end_code);
|
2016-12-15 20:38:11 +03:00
|
|
|
qemu_log("start_code 0x" TARGET_ABI_FMT_lx "\n", info->start_code);
|
|
|
|
qemu_log("start_data 0x" TARGET_ABI_FMT_lx "\n", info->start_data);
|
2009-01-20 19:57:34 +03:00
|
|
|
qemu_log("end_data 0x" TARGET_ABI_FMT_lx "\n", info->end_data);
|
2016-12-15 20:38:11 +03:00
|
|
|
qemu_log("start_stack 0x" TARGET_ABI_FMT_lx "\n", info->start_stack);
|
2009-01-20 19:57:34 +03:00
|
|
|
qemu_log("brk 0x" TARGET_ABI_FMT_lx "\n", info->brk);
|
|
|
|
qemu_log("entry 0x" TARGET_ABI_FMT_lx "\n", info->entry);
|
2016-12-15 20:38:11 +03:00
|
|
|
qemu_log("argv_start 0x" TARGET_ABI_FMT_lx "\n", info->arg_start);
|
|
|
|
qemu_log("env_start 0x" TARGET_ABI_FMT_lx "\n",
|
|
|
|
info->arg_end + (abi_ulong)sizeof(abi_ulong));
|
|
|
|
qemu_log("auxv_start 0x" TARGET_ABI_FMT_lx "\n", info->saved_auxv);
|
2009-01-20 19:57:34 +03:00
|
|
|
}
|
2003-02-19 01:55:36 +03:00
|
|
|
|
2006-03-25 22:31:22 +03:00
|
|
|
target_set_brk(info->brk);
|
2003-02-19 01:55:36 +03:00
|
|
|
syscall_init();
|
2003-03-23 04:06:05 +03:00
|
|
|
signal_init();
|
2003-02-19 01:55:36 +03:00
|
|
|
|
2010-05-06 19:50:41 +04:00
|
|
|
/* Now that we've loaded the binary, GUEST_BASE is fixed. Delay
|
|
|
|
generating the prologue until now so that the prologue can take
|
|
|
|
the real value of GUEST_BASE into account. */
|
2017-07-13 00:15:52 +03:00
|
|
|
tcg_prologue_init(tcg_ctx);
|
2010-05-06 19:50:41 +04:00
|
|
|
|
2018-04-11 21:56:33 +03:00
|
|
|
target_cpu_copy_regs(env, regs);
|
|
|
|
|
2020-04-30 22:01:19 +03:00
|
|
|
if (gdbstub) {
|
|
|
|
if (gdbserver_start(gdbstub) < 0) {
|
|
|
|
fprintf(stderr, "qemu: could not open gdbserver on %s\n",
|
|
|
|
gdbstub);
|
2015-09-28 16:12:16 +03:00
|
|
|
exit(EXIT_FAILURE);
|
2011-09-06 17:15:50 +04:00
|
|
|
}
|
2013-06-27 21:49:31 +04:00
|
|
|
gdb_handlesig(cpu, 0);
|
2005-04-17 23:16:13 +04:00
|
|
|
}
|
2003-03-22 20:31:38 +03:00
|
|
|
cpu_loop(env);
|
|
|
|
/* never exits */
|
2003-02-19 01:55:36 +03:00
|
|
|
return 0;
|
|
|
|
}
|