2019-05-13 16:43:57 +03:00
|
|
|
/*
|
|
|
|
* Semihosting configuration
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Imagination Technologies
|
|
|
|
* Copyright (c) 2019 Linaro Ltd
|
|
|
|
*
|
|
|
|
* This controls the configuration of semihosting for all guest
|
|
|
|
* targets that support it. Architecture specific handling is handled
|
|
|
|
* in target/HW/HW-semi.c
|
|
|
|
*
|
2023-08-23 09:53:30 +03:00
|
|
|
* Semihosting is slightly strange in that it is also supported by some
|
2019-05-13 16:43:57 +03:00
|
|
|
* linux-user targets. However in that use case no configuration of
|
|
|
|
* the outputs and command lines is supported.
|
|
|
|
*
|
2023-10-04 12:06:23 +03:00
|
|
|
* The config module is common to all system targets however as vl.c
|
2019-05-13 16:43:57 +03:00
|
|
|
* needs to link against the helpers.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/option.h"
|
|
|
|
#include "qemu/config-file.h"
|
|
|
|
#include "qemu/error-report.h"
|
2021-03-05 16:54:49 +03:00
|
|
|
#include "semihosting/semihost.h"
|
2019-05-14 17:30:14 +03:00
|
|
|
#include "chardev/char.h"
|
2019-05-13 16:43:57 +03:00
|
|
|
|
|
|
|
QemuOptsList qemu_semihosting_config_opts = {
|
|
|
|
.name = "semihosting-config",
|
2022-06-10 16:32:36 +03:00
|
|
|
.merge_lists = true,
|
2019-05-13 16:43:57 +03:00
|
|
|
.implied_opt_name = "enable",
|
|
|
|
.head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
|
|
|
|
.desc = {
|
|
|
|
{
|
|
|
|
.name = "enable",
|
|
|
|
.type = QEMU_OPT_BOOL,
|
2022-08-22 17:12:24 +03:00
|
|
|
}, {
|
|
|
|
.name = "userspace",
|
|
|
|
.type = QEMU_OPT_BOOL,
|
2019-05-13 16:43:57 +03:00
|
|
|
}, {
|
|
|
|
.name = "target",
|
|
|
|
.type = QEMU_OPT_STRING,
|
2019-05-14 17:30:14 +03:00
|
|
|
}, {
|
|
|
|
.name = "chardev",
|
|
|
|
.type = QEMU_OPT_STRING,
|
2019-05-13 16:43:57 +03:00
|
|
|
}, {
|
|
|
|
.name = "arg",
|
|
|
|
.type = QEMU_OPT_STRING,
|
|
|
|
},
|
|
|
|
{ /* end of list */ }
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct SemihostingConfig {
|
|
|
|
bool enabled;
|
2022-08-22 17:12:24 +03:00
|
|
|
bool userspace_enabled;
|
2019-05-13 16:43:57 +03:00
|
|
|
SemihostingTarget target;
|
2022-03-15 14:46:12 +03:00
|
|
|
char **argv;
|
2019-05-13 16:43:57 +03:00
|
|
|
int argc;
|
|
|
|
const char *cmdline; /* concatenated argv */
|
|
|
|
} SemihostingConfig;
|
|
|
|
|
|
|
|
static SemihostingConfig semihosting;
|
2019-05-14 17:30:14 +03:00
|
|
|
static const char *semihost_chardev;
|
2019-05-13 16:43:57 +03:00
|
|
|
|
2022-08-22 17:12:24 +03:00
|
|
|
bool semihosting_enabled(bool is_user)
|
2019-05-13 16:43:57 +03:00
|
|
|
{
|
2022-08-22 17:12:24 +03:00
|
|
|
return semihosting.enabled && (!is_user || semihosting.userspace_enabled);
|
2019-05-13 16:43:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SemihostingTarget semihosting_get_target(void)
|
|
|
|
{
|
|
|
|
return semihosting.target;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *semihosting_get_arg(int i)
|
|
|
|
{
|
|
|
|
if (i >= semihosting.argc) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return semihosting.argv[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
int semihosting_get_argc(void)
|
|
|
|
{
|
|
|
|
return semihosting.argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *semihosting_get_cmdline(void)
|
|
|
|
{
|
|
|
|
if (semihosting.cmdline == NULL && semihosting.argc > 0) {
|
|
|
|
semihosting.cmdline = g_strjoinv(" ", (gchar **)semihosting.argv);
|
|
|
|
}
|
|
|
|
return semihosting.cmdline;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_semihosting_arg(void *opaque,
|
|
|
|
const char *name, const char *val,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
SemihostingConfig *s = opaque;
|
|
|
|
if (strcmp(name, "arg") == 0) {
|
|
|
|
s->argc++;
|
|
|
|
/* one extra element as g_strjoinv() expects NULL-terminated array */
|
2022-03-15 14:46:12 +03:00
|
|
|
s->argv = g_renew(char *, s->argv, s->argc + 1);
|
|
|
|
s->argv[s->argc - 1] = g_strdup(val);
|
2019-05-13 16:43:57 +03:00
|
|
|
s->argv[s->argc] = NULL;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use strings passed via -kernel/-append to initialize semihosting.argv[] */
|
|
|
|
void semihosting_arg_fallback(const char *file, const char *cmd)
|
|
|
|
{
|
|
|
|
char *cmd_token;
|
2023-10-29 17:50:31 +03:00
|
|
|
g_autofree char *cmd_dup = g_strdup(cmd);
|
2019-05-13 16:43:57 +03:00
|
|
|
|
|
|
|
/* argv[0] */
|
|
|
|
add_semihosting_arg(&semihosting, "arg", file, NULL);
|
|
|
|
|
|
|
|
/* split -append and initialize argv[1..n] */
|
2023-10-29 17:50:31 +03:00
|
|
|
cmd_token = strtok(cmd_dup, " ");
|
2019-05-13 16:43:57 +03:00
|
|
|
while (cmd_token) {
|
|
|
|
add_semihosting_arg(&semihosting, "arg", cmd_token, NULL);
|
|
|
|
cmd_token = strtok(NULL, " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void qemu_semihosting_enable(void)
|
|
|
|
{
|
|
|
|
semihosting.enabled = true;
|
|
|
|
semihosting.target = SEMIHOSTING_TARGET_AUTO;
|
|
|
|
}
|
|
|
|
|
2023-10-04 15:00:12 +03:00
|
|
|
int qemu_semihosting_config_options(const char *optstr)
|
2019-05-13 16:43:57 +03:00
|
|
|
{
|
|
|
|
QemuOptsList *opt_list = qemu_find_opts("semihosting-config");
|
2023-10-04 15:00:12 +03:00
|
|
|
QemuOpts *opts = qemu_opts_parse_noisily(opt_list, optstr, false);
|
2019-05-13 16:43:57 +03:00
|
|
|
|
|
|
|
semihosting.enabled = true;
|
|
|
|
|
|
|
|
if (opts != NULL) {
|
|
|
|
semihosting.enabled = qemu_opt_get_bool(opts, "enable",
|
|
|
|
true);
|
2022-08-22 17:12:24 +03:00
|
|
|
semihosting.userspace_enabled = qemu_opt_get_bool(opts, "userspace",
|
|
|
|
false);
|
2019-05-13 16:43:57 +03:00
|
|
|
const char *target = qemu_opt_get(opts, "target");
|
2019-05-14 17:30:14 +03:00
|
|
|
/* setup of chardev is deferred until they are initialised */
|
|
|
|
semihost_chardev = qemu_opt_get(opts, "chardev");
|
2019-05-13 16:43:57 +03:00
|
|
|
if (target != NULL) {
|
|
|
|
if (strcmp("native", target) == 0) {
|
|
|
|
semihosting.target = SEMIHOSTING_TARGET_NATIVE;
|
|
|
|
} else if (strcmp("gdb", target) == 0) {
|
|
|
|
semihosting.target = SEMIHOSTING_TARGET_GDB;
|
|
|
|
} else if (strcmp("auto", target) == 0) {
|
|
|
|
semihosting.target = SEMIHOSTING_TARGET_AUTO;
|
|
|
|
} else {
|
|
|
|
error_report("unsupported semihosting-config %s",
|
2023-10-04 15:00:12 +03:00
|
|
|
optstr);
|
2019-05-13 16:43:57 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
semihosting.target = SEMIHOSTING_TARGET_AUTO;
|
|
|
|
}
|
|
|
|
/* Set semihosting argument count and vector */
|
|
|
|
qemu_opt_foreach(opts, add_semihosting_arg,
|
|
|
|
&semihosting, NULL);
|
|
|
|
} else {
|
2023-10-04 15:00:12 +03:00
|
|
|
error_report("unsupported semihosting-config %s", optstr);
|
2019-05-13 16:43:57 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-02 02:59:06 +03:00
|
|
|
/* We had to defer this until chardevs were created */
|
|
|
|
void qemu_semihosting_chardev_init(void)
|
2019-05-14 17:30:14 +03:00
|
|
|
{
|
2022-05-02 02:59:06 +03:00
|
|
|
Chardev *chr = NULL;
|
|
|
|
|
2019-05-14 17:30:14 +03:00
|
|
|
if (semihost_chardev) {
|
2022-05-02 02:59:06 +03:00
|
|
|
chr = qemu_chr_find(semihost_chardev);
|
2019-05-14 17:30:14 +03:00
|
|
|
if (chr == NULL) {
|
|
|
|
error_report("semihosting chardev '%s' not found",
|
|
|
|
semihost_chardev);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2022-05-02 02:59:06 +03:00
|
|
|
|
|
|
|
qemu_semihosting_console_init(chr);
|
2019-05-14 17:30:14 +03:00
|
|
|
}
|