Import QEMU upstream snapshot d2e570c
Upstream: https://gitlab.com/qemu-project/qemu.git Upstream-Commit: d2e570cc0f97b936902a5b1b86b73c0f5998b475
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
|
||||
config SEMIHOSTING
|
||||
bool
|
||||
depends on TCG
|
||||
|
||||
config ARM_COMPATIBLE_SEMIHOSTING
|
||||
bool
|
||||
select SEMIHOSTING
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Stubs for platforms different from ARM
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "semihosting/semihost.h"
|
||||
|
||||
bool semihosting_arm_compatible(void)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void semihosting_arm_compatible_init(void)
|
||||
{
|
||||
g_assert_not_reached();
|
||||
}
|
||||
@@ -0,0 +1,826 @@
|
||||
/*
|
||||
* Semihosting support for systems modeled on the Arm "Angel"
|
||||
* semihosting syscalls design. This includes Arm and RISC-V processors
|
||||
*
|
||||
* Copyright (c) 2005, 2007 CodeSourcery.
|
||||
* Copyright (c) 2019 Linaro
|
||||
* Written by Paul Brook.
|
||||
*
|
||||
* Copyright © 2020 by Keith Packard <[email protected]>
|
||||
* Adapted for systems other than ARM, including RISC-V, by Keith Packard
|
||||
*
|
||||
* 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
|
||||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* ARM Semihosting is documented in:
|
||||
* Semihosting for AArch32 and AArch64 Release 2.0
|
||||
* https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst
|
||||
*
|
||||
* RISC-V Semihosting is documented in:
|
||||
* RISC-V Semihosting
|
||||
* https://github.com/riscv/riscv-semihosting-spec/blob/main/riscv-semihosting-spec.adoc
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/timer.h"
|
||||
#include "exec/gdbstub.h"
|
||||
#include "gdbstub/syscalls.h"
|
||||
#include "semihosting/semihost.h"
|
||||
#include "semihosting/console.h"
|
||||
#include "semihosting/common-semi.h"
|
||||
#include "semihosting/guestfd.h"
|
||||
#include "semihosting/syscalls.h"
|
||||
|
||||
#ifdef CONFIG_USER_ONLY
|
||||
#include "qemu.h"
|
||||
|
||||
#define COMMON_SEMI_HEAP_SIZE (128 * 1024 * 1024)
|
||||
#else
|
||||
#include "qemu/cutils.h"
|
||||
#include "hw/core/loader.h"
|
||||
#include "hw/core/boards.h"
|
||||
#endif
|
||||
|
||||
#define TARGET_SYS_OPEN 0x01
|
||||
#define TARGET_SYS_CLOSE 0x02
|
||||
#define TARGET_SYS_WRITEC 0x03
|
||||
#define TARGET_SYS_WRITE0 0x04
|
||||
#define TARGET_SYS_WRITE 0x05
|
||||
#define TARGET_SYS_READ 0x06
|
||||
#define TARGET_SYS_READC 0x07
|
||||
#define TARGET_SYS_ISERROR 0x08
|
||||
#define TARGET_SYS_ISTTY 0x09
|
||||
#define TARGET_SYS_SEEK 0x0a
|
||||
#define TARGET_SYS_FLEN 0x0c
|
||||
#define TARGET_SYS_TMPNAM 0x0d
|
||||
#define TARGET_SYS_REMOVE 0x0e
|
||||
#define TARGET_SYS_RENAME 0x0f
|
||||
#define TARGET_SYS_CLOCK 0x10
|
||||
#define TARGET_SYS_TIME 0x11
|
||||
#define TARGET_SYS_SYSTEM 0x12
|
||||
#define TARGET_SYS_ERRNO 0x13
|
||||
#define TARGET_SYS_GET_CMDLINE 0x15
|
||||
#define TARGET_SYS_HEAPINFO 0x16
|
||||
#define TARGET_SYS_EXIT 0x18
|
||||
#define TARGET_SYS_SYNCCACHE 0x19
|
||||
#define TARGET_SYS_EXIT_EXTENDED 0x20
|
||||
#define TARGET_SYS_ELAPSED 0x30
|
||||
#define TARGET_SYS_TICKFREQ 0x31
|
||||
|
||||
/* ADP_Stopped_ApplicationExit is used for exit(0),
|
||||
* anything else is implemented as exit(1) */
|
||||
#define ADP_Stopped_ApplicationExit (0x20026)
|
||||
|
||||
#ifndef O_BINARY
|
||||
#define O_BINARY 0
|
||||
#endif
|
||||
|
||||
static int gdb_open_modeflags[12] = {
|
||||
GDB_O_RDONLY,
|
||||
GDB_O_RDONLY,
|
||||
GDB_O_RDWR,
|
||||
GDB_O_RDWR,
|
||||
GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
|
||||
GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
|
||||
GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
|
||||
GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
|
||||
GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
|
||||
GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
|
||||
GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
|
||||
GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
|
||||
};
|
||||
|
||||
/*
|
||||
* For ARM semihosting, we have a separate structure for routing
|
||||
* data for the console which is outside the guest fd address space.
|
||||
*/
|
||||
static GuestFD console_in_gf;
|
||||
static GuestFD console_out_gf;
|
||||
|
||||
#ifndef CONFIG_USER_ONLY
|
||||
|
||||
/**
|
||||
* common_semi_find_bases: find information about ram and heap base
|
||||
*
|
||||
* This function attempts to provide meaningful numbers for RAM and
|
||||
* HEAP base addresses. The rambase is simply the lowest addressable
|
||||
* RAM position. For the heapbase we ask the loader to scan the
|
||||
* address space and the largest available gap by querying the "ROM"
|
||||
* regions.
|
||||
*
|
||||
* Returns: a structure with the numbers we need.
|
||||
*/
|
||||
|
||||
typedef struct LayoutInfo {
|
||||
vaddr rambase;
|
||||
size_t ramsize;
|
||||
hwaddr heapbase;
|
||||
hwaddr heaplimit;
|
||||
} LayoutInfo;
|
||||
|
||||
static bool find_ram_cb(Int128 start, Int128 len, const MemoryRegion *mr,
|
||||
hwaddr offset_in_region, void *opaque)
|
||||
{
|
||||
LayoutInfo *info = (LayoutInfo *) opaque;
|
||||
uint64_t size = int128_get64(len);
|
||||
|
||||
if (!mr->ram || mr->readonly) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (size > info->ramsize) {
|
||||
info->rambase = int128_get64(start);
|
||||
info->ramsize = size;
|
||||
}
|
||||
|
||||
/* search exhaustively for largest RAM */
|
||||
return false;
|
||||
}
|
||||
|
||||
static LayoutInfo common_semi_find_bases(CPUState *cs)
|
||||
{
|
||||
FlatView *fv;
|
||||
LayoutInfo info = { 0, 0, 0, 0 };
|
||||
|
||||
RCU_READ_LOCK_GUARD();
|
||||
|
||||
fv = address_space_to_flatview(cs->as);
|
||||
flatview_for_each_range(fv, find_ram_cb, &info);
|
||||
|
||||
/*
|
||||
* If we have found the RAM lets iterate through the ROM blobs to
|
||||
* work out the best place for the remainder of RAM and split it
|
||||
* equally between stack and heap.
|
||||
*/
|
||||
if (info.rambase || info.ramsize > 0) {
|
||||
RomGap gap = rom_find_largest_gap_between(info.rambase, info.ramsize);
|
||||
info.heapbase = gap.base;
|
||||
info.heaplimit = gap.base + gap.size;
|
||||
}
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#include "semihosting/common-semi.h"
|
||||
|
||||
/*
|
||||
* Read the input value from the argument block; fail the semihosting
|
||||
* call if the memory read fails. Eventually we could use a generic
|
||||
* CPUState helper function here.
|
||||
* Note that GET_ARG() handles memory access errors by jumping to
|
||||
* do_fault, so must be used as the first thing done in handling a
|
||||
* semihosting call, to avoid accidentally leaking allocated resources.
|
||||
* SET_ARG(), since it unavoidably happens late, instead returns an
|
||||
* error indication (0 on success, non-0 for error) which the caller
|
||||
* should check.
|
||||
*/
|
||||
|
||||
#define GET_ARG(n) do { \
|
||||
if (is_64bit_semihosting(env)) { \
|
||||
if (get_user_u64(arg ## n, args + (n) * 8)) { \
|
||||
goto do_fault; \
|
||||
} \
|
||||
} else { \
|
||||
if (get_user_u32(arg ## n, args + (n) * 4)) { \
|
||||
goto do_fault; \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define SET_ARG(n, val) \
|
||||
(is_64bit_semihosting(env) ? \
|
||||
put_user_u64(val, args + (n) * 8) : \
|
||||
put_user_u32(val, args + (n) * 4))
|
||||
|
||||
|
||||
/*
|
||||
* The semihosting API has no concept of its errno being thread-safe,
|
||||
* as the API design predates SMP CPUs and was intended as a simple
|
||||
* real-hardware set of debug functionality. For QEMU, we make the
|
||||
* errno be per-thread in linux-user mode; in system-mode it is a simple
|
||||
* global, and we assume that the guest takes care of avoiding any races.
|
||||
*/
|
||||
#ifndef CONFIG_USER_ONLY
|
||||
static uint64_t syscall_err;
|
||||
|
||||
#include "semihosting/uaccess.h"
|
||||
#endif
|
||||
|
||||
static inline uint32_t get_swi_errno(CPUState *cs)
|
||||
{
|
||||
#ifdef CONFIG_USER_ONLY
|
||||
TaskState *ts = get_task_state(cs);
|
||||
|
||||
return ts->swi_errno;
|
||||
#else
|
||||
return syscall_err;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void common_semi_cb(CPUState *cs, uint64_t ret, int err)
|
||||
{
|
||||
if (err) {
|
||||
#ifdef CONFIG_USER_ONLY
|
||||
TaskState *ts = get_task_state(cs);
|
||||
ts->swi_errno = err;
|
||||
#else
|
||||
syscall_err = err;
|
||||
#endif
|
||||
}
|
||||
common_semi_set_ret(cs, ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* Use 0xdeadbeef as the return value when there isn't a defined
|
||||
* return value for the call.
|
||||
*/
|
||||
static void common_semi_dead_cb(CPUState *cs, uint64_t ret, int err)
|
||||
{
|
||||
common_semi_set_ret(cs, 0xdeadbeef);
|
||||
}
|
||||
|
||||
/*
|
||||
* SYS_READ and SYS_WRITE always return the number of bytes not read/written.
|
||||
* There is no error condition, other than returning the original length.
|
||||
*/
|
||||
static void common_semi_rw_cb(CPUState *cs, uint64_t ret, int err)
|
||||
{
|
||||
/* Recover the original length from the third argument. */
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
uint64_t args = common_semi_arg(cs, 1);
|
||||
uint64_t arg2;
|
||||
GET_ARG(2);
|
||||
|
||||
if (err) {
|
||||
do_fault:
|
||||
ret = 0; /* error: no bytes transmitted */
|
||||
}
|
||||
common_semi_set_ret(cs, arg2 - ret);
|
||||
}
|
||||
|
||||
/*
|
||||
* Convert from Posix ret+errno to Arm SYS_ISTTY return values.
|
||||
* With gdbstub, err is only ever set for protocol errors to EIO.
|
||||
*/
|
||||
static void common_semi_istty_cb(CPUState *cs, uint64_t ret, int err)
|
||||
{
|
||||
if (err) {
|
||||
ret = (err == ENOTTY ? 0 : -1);
|
||||
}
|
||||
common_semi_cb(cs, ret, err);
|
||||
}
|
||||
|
||||
/*
|
||||
* SYS_SEEK returns 0 on success, not the resulting offset.
|
||||
*/
|
||||
static void common_semi_seek_cb(CPUState *cs, uint64_t ret, int err)
|
||||
{
|
||||
if (!err) {
|
||||
ret = 0;
|
||||
}
|
||||
common_semi_cb(cs, ret, err);
|
||||
}
|
||||
|
||||
/*
|
||||
* Return an address in target memory of 64 bytes where the remote
|
||||
* gdb should write its stat struct. (The format of this structure
|
||||
* is defined by GDB's remote protocol and is not target-specific.)
|
||||
* We put this on the guest's stack just below SP.
|
||||
*/
|
||||
static uint64_t common_semi_flen_buf(CPUState *cs)
|
||||
{
|
||||
vaddr sp = common_semi_stack_bottom(cs);
|
||||
return sp - 64;
|
||||
}
|
||||
|
||||
static void
|
||||
common_semi_flen_fstat_cb(CPUState *cs, uint64_t ret, int err)
|
||||
{
|
||||
if (!err) {
|
||||
/* The size is always stored in big-endian order, extract the value. */
|
||||
uint64_t size;
|
||||
if (cpu_memory_rw_debug(cs, common_semi_flen_buf(cs) +
|
||||
offsetof(struct gdb_stat, gdb_st_size),
|
||||
&size, 8, 0)) {
|
||||
ret = -1, err = EFAULT;
|
||||
} else {
|
||||
ret = be64_to_cpu(size);
|
||||
}
|
||||
}
|
||||
common_semi_cb(cs, ret, err);
|
||||
}
|
||||
|
||||
static void
|
||||
common_semi_readc_cb(CPUState *cs, uint64_t ret, int err)
|
||||
{
|
||||
if (!err) {
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
uint8_t ch;
|
||||
|
||||
if (get_user_u8(ch, common_semi_stack_bottom(cs) - 1)) {
|
||||
ret = -1, err = EFAULT;
|
||||
} else {
|
||||
ret = ch;
|
||||
}
|
||||
}
|
||||
common_semi_cb(cs, ret, err);
|
||||
}
|
||||
|
||||
#define SHFB_MAGIC_0 0x53
|
||||
#define SHFB_MAGIC_1 0x48
|
||||
#define SHFB_MAGIC_2 0x46
|
||||
#define SHFB_MAGIC_3 0x42
|
||||
|
||||
/* Feature bits reportable in feature byte 0 */
|
||||
#define SH_EXT_EXIT_EXTENDED (1 << 0)
|
||||
#define SH_EXT_STDOUT_STDERR (1 << 1)
|
||||
|
||||
static const uint8_t featurefile_data[] = {
|
||||
SHFB_MAGIC_0,
|
||||
SHFB_MAGIC_1,
|
||||
SHFB_MAGIC_2,
|
||||
SHFB_MAGIC_3,
|
||||
SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */
|
||||
};
|
||||
|
||||
bool semihosting_arm_compatible(void)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void semihosting_arm_compatible_init(void)
|
||||
{
|
||||
/* For ARM-compat, the console is in a separate namespace. */
|
||||
if (use_gdb_syscalls()) {
|
||||
console_in_gf.type = GuestFDGDB;
|
||||
console_in_gf.hostfd = 0;
|
||||
console_out_gf.type = GuestFDGDB;
|
||||
console_out_gf.hostfd = 2;
|
||||
} else {
|
||||
console_in_gf.type = GuestFDConsole;
|
||||
console_out_gf.type = GuestFDConsole;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Do a semihosting call.
|
||||
*
|
||||
* The specification always says that the "return register" either
|
||||
* returns a specific value or is corrupted, so we don't need to
|
||||
* report to our caller whether we are returning a value or trying to
|
||||
* leave the register unchanged.
|
||||
*/
|
||||
void do_common_semihosting(CPUState *cs)
|
||||
{
|
||||
CPUArchState *env = cpu_env(cs);
|
||||
uint64_t args;
|
||||
uint64_t arg0, arg1, arg2, arg3;
|
||||
uint64_t ul_ret;
|
||||
char * s;
|
||||
int nr;
|
||||
int64_t elapsed;
|
||||
|
||||
nr = common_semi_arg(cs, 0) & 0xffffffffU;
|
||||
args = common_semi_arg(cs, 1);
|
||||
|
||||
switch (nr) {
|
||||
case TARGET_SYS_OPEN:
|
||||
{
|
||||
int ret, err = 0;
|
||||
int hostfd;
|
||||
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
GET_ARG(2);
|
||||
s = lock_user_string(arg0);
|
||||
if (!s) {
|
||||
goto do_fault;
|
||||
}
|
||||
if (arg1 >= 12) {
|
||||
unlock_user(s, arg0, 0);
|
||||
common_semi_cb(cs, -1, EINVAL);
|
||||
break;
|
||||
}
|
||||
|
||||
if (strcmp(s, ":tt") == 0) {
|
||||
/*
|
||||
* We implement SH_EXT_STDOUT_STDERR, so:
|
||||
* open for read == stdin
|
||||
* open for write == stdout
|
||||
* open for append == stderr
|
||||
*/
|
||||
if (arg1 < 4) {
|
||||
hostfd = STDIN_FILENO;
|
||||
} else if (arg1 < 8) {
|
||||
hostfd = STDOUT_FILENO;
|
||||
} else {
|
||||
hostfd = STDERR_FILENO;
|
||||
}
|
||||
ret = alloc_guestfd();
|
||||
associate_guestfd(ret, hostfd);
|
||||
} else if (strcmp(s, ":semihosting-features") == 0) {
|
||||
/* We must fail opens for modes other than 0 ('r') or 1 ('rb') */
|
||||
if (arg1 != 0 && arg1 != 1) {
|
||||
ret = -1;
|
||||
err = EACCES;
|
||||
} else {
|
||||
ret = alloc_guestfd();
|
||||
staticfile_guestfd(ret, featurefile_data,
|
||||
sizeof(featurefile_data));
|
||||
}
|
||||
} else {
|
||||
unlock_user(s, arg0, 0);
|
||||
semihost_sys_open(cs, common_semi_cb, arg0, arg2 + 1,
|
||||
gdb_open_modeflags[arg1], 0644);
|
||||
break;
|
||||
}
|
||||
unlock_user(s, arg0, 0);
|
||||
common_semi_cb(cs, ret, err);
|
||||
break;
|
||||
}
|
||||
|
||||
case TARGET_SYS_CLOSE:
|
||||
GET_ARG(0);
|
||||
semihost_sys_close(cs, common_semi_cb, arg0);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_WRITEC:
|
||||
/*
|
||||
* FIXME: the byte to be written is in a uint64_t slot,
|
||||
* which means this is wrong for a big-endian guest.
|
||||
*/
|
||||
semihost_sys_write_gf(cs, common_semi_dead_cb,
|
||||
&console_out_gf, args, 1);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_WRITE0:
|
||||
{
|
||||
ssize_t len = target_strlen(args);
|
||||
if (len < 0) {
|
||||
common_semi_dead_cb(cs, -1, EFAULT);
|
||||
} else {
|
||||
semihost_sys_write_gf(cs, common_semi_dead_cb,
|
||||
&console_out_gf, args, len);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case TARGET_SYS_WRITE:
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
GET_ARG(2);
|
||||
semihost_sys_write(cs, common_semi_rw_cb, arg0, arg1, arg2);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_READ:
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
GET_ARG(2);
|
||||
semihost_sys_read(cs, common_semi_rw_cb, arg0, arg1, arg2);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_READC:
|
||||
semihost_sys_read_gf(cs, common_semi_readc_cb, &console_in_gf,
|
||||
common_semi_stack_bottom(cs) - 1, 1);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_ISERROR:
|
||||
{
|
||||
GET_ARG(0);
|
||||
bool ret = is_64bit_semihosting(env) ?
|
||||
(int64_t)arg0 < 0 : (int32_t)arg0 < 0;
|
||||
common_semi_set_ret(cs, ret);
|
||||
break;
|
||||
}
|
||||
case TARGET_SYS_ISTTY:
|
||||
GET_ARG(0);
|
||||
semihost_sys_isatty(cs, common_semi_istty_cb, arg0);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_SEEK:
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
semihost_sys_lseek(cs, common_semi_seek_cb, arg0, arg1, GDB_SEEK_SET);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_FLEN:
|
||||
GET_ARG(0);
|
||||
semihost_sys_flen(cs, common_semi_flen_fstat_cb, common_semi_cb,
|
||||
arg0, common_semi_flen_buf(cs));
|
||||
break;
|
||||
|
||||
case TARGET_SYS_TMPNAM:
|
||||
{
|
||||
int len;
|
||||
char *p;
|
||||
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
GET_ARG(2);
|
||||
len = asprintf(&s, "%s/qemu-%x%02x", g_get_tmp_dir(),
|
||||
getpid(), (int)arg1 & 0xff);
|
||||
if (len < 0) {
|
||||
common_semi_set_ret(cs, -1);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Allow for trailing NUL */
|
||||
len++;
|
||||
/* Make sure there's enough space in the buffer */
|
||||
if (len > arg2) {
|
||||
free(s);
|
||||
common_semi_set_ret(cs, -1);
|
||||
break;
|
||||
}
|
||||
p = lock_user(VERIFY_WRITE, arg0, len, 0);
|
||||
if (!p) {
|
||||
free(s);
|
||||
goto do_fault;
|
||||
}
|
||||
memcpy(p, s, len);
|
||||
unlock_user(p, arg0, len);
|
||||
free(s);
|
||||
common_semi_set_ret(cs, 0);
|
||||
break;
|
||||
}
|
||||
|
||||
case TARGET_SYS_REMOVE:
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
semihost_sys_remove(cs, common_semi_cb, arg0, arg1 + 1);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_RENAME:
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
GET_ARG(2);
|
||||
GET_ARG(3);
|
||||
semihost_sys_rename(cs, common_semi_cb, arg0, arg1 + 1, arg2, arg3 + 1);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_CLOCK:
|
||||
common_semi_set_ret(cs, clock() / (CLOCKS_PER_SEC / 100));
|
||||
break;
|
||||
|
||||
case TARGET_SYS_TIME:
|
||||
ul_ret = time(NULL);
|
||||
common_semi_cb(cs, ul_ret, ul_ret == -1 ? errno : 0);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_SYSTEM:
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
semihost_sys_system(cs, common_semi_cb, arg0, arg1 + 1);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_ERRNO:
|
||||
common_semi_set_ret(cs, get_swi_errno(cs));
|
||||
break;
|
||||
|
||||
case TARGET_SYS_GET_CMDLINE:
|
||||
{
|
||||
/* Build a command-line from the original argv.
|
||||
*
|
||||
* The inputs are:
|
||||
* * arg0, pointer to a buffer of at least the size
|
||||
* specified in arg1.
|
||||
* * arg1, size of the buffer pointed to by arg0 in
|
||||
* bytes.
|
||||
*
|
||||
* The outputs are:
|
||||
* * arg0, pointer to null-terminated string of the
|
||||
* command line.
|
||||
* * arg1, length of the string pointed to by arg0.
|
||||
*/
|
||||
|
||||
char *output_buffer;
|
||||
size_t input_size;
|
||||
size_t output_size;
|
||||
int status = 0;
|
||||
#if !defined(CONFIG_USER_ONLY)
|
||||
const char *cmdline;
|
||||
#else
|
||||
TaskState *ts = get_task_state(cs);
|
||||
#endif
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
input_size = arg1;
|
||||
/* Compute the size of the output string. */
|
||||
#if !defined(CONFIG_USER_ONLY)
|
||||
cmdline = semihosting_get_cmdline();
|
||||
if (cmdline == NULL) {
|
||||
cmdline = ""; /* Default to an empty line. */
|
||||
}
|
||||
output_size = strlen(cmdline) + 1; /* Count terminating 0. */
|
||||
#else
|
||||
unsigned int i;
|
||||
|
||||
output_size = ts->info->env_strings - ts->info->arg_strings;
|
||||
if (!output_size) {
|
||||
/*
|
||||
* We special-case the "empty command line" case (argc==0).
|
||||
* Just provide the terminating 0.
|
||||
*/
|
||||
output_size = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (output_size > input_size) {
|
||||
/* Not enough space to store command-line arguments. */
|
||||
common_semi_cb(cs, -1, E2BIG);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Adjust the command-line length. */
|
||||
if (SET_ARG(1, output_size - 1)) {
|
||||
/* Couldn't write back to argument block */
|
||||
goto do_fault;
|
||||
}
|
||||
|
||||
/* Lock the buffer on the ARM side. */
|
||||
output_buffer = lock_user(VERIFY_WRITE, arg0, output_size, 0);
|
||||
if (!output_buffer) {
|
||||
goto do_fault;
|
||||
}
|
||||
|
||||
/* Copy the command-line arguments. */
|
||||
#if !defined(CONFIG_USER_ONLY)
|
||||
pstrcpy(output_buffer, output_size, cmdline);
|
||||
#else
|
||||
if (output_size == 1) {
|
||||
/* Empty command-line. */
|
||||
output_buffer[0] = '\0';
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (copy_from_user(output_buffer, ts->info->arg_strings,
|
||||
output_size)) {
|
||||
unlock_user(output_buffer, arg0, 0);
|
||||
goto do_fault;
|
||||
}
|
||||
|
||||
/* Separate arguments by white spaces. */
|
||||
for (i = 0; i < output_size - 1; i++) {
|
||||
if (output_buffer[i] == 0) {
|
||||
output_buffer[i] = ' ';
|
||||
}
|
||||
}
|
||||
out:
|
||||
#endif
|
||||
/* Unlock the buffer on the ARM side. */
|
||||
unlock_user(output_buffer, arg0, output_size);
|
||||
common_semi_cb(cs, status, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case TARGET_SYS_HEAPINFO:
|
||||
{
|
||||
uint64_t retvals[4];
|
||||
int i;
|
||||
#ifdef CONFIG_USER_ONLY
|
||||
TaskState *ts = get_task_state(cs);
|
||||
static abi_ulong heapbase, heaplimit;
|
||||
#else
|
||||
LayoutInfo info = common_semi_find_bases(cs);
|
||||
#endif
|
||||
|
||||
GET_ARG(0);
|
||||
|
||||
#ifdef CONFIG_USER_ONLY
|
||||
/*
|
||||
* Some C libraries assume the heap immediately follows .bss, so
|
||||
* allocate it using sbrk.
|
||||
*/
|
||||
if (!heaplimit) {
|
||||
heapbase = do_brk(0);
|
||||
/* Try a big heap, and reduce the size if that fails. */
|
||||
for (abi_ulong size = COMMON_SEMI_HEAP_SIZE; ; size >>= 1) {
|
||||
abi_ulong limit = heapbase + size;
|
||||
abi_ulong ret = do_brk(limit);
|
||||
if (ret >= limit) {
|
||||
heaplimit = limit;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
retvals[0] = heapbase;
|
||||
retvals[1] = heaplimit;
|
||||
/*
|
||||
* Note that semihosting is *not* thread aware.
|
||||
* Always return the stack base of the main thread.
|
||||
*/
|
||||
retvals[2] = ts->info->start_stack;
|
||||
retvals[3] = 0; /* Stack limit. */
|
||||
#else
|
||||
retvals[0] = info.heapbase; /* Heap Base */
|
||||
retvals[1] = info.heaplimit; /* Heap Limit */
|
||||
retvals[2] = info.heaplimit; /* Stack base */
|
||||
retvals[3] = info.heapbase; /* Stack limit. */
|
||||
#endif
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(retvals); i++) {
|
||||
bool fail;
|
||||
|
||||
if (is_64bit_semihosting(env)) {
|
||||
fail = put_user_u64(retvals[i], arg0 + i * 8);
|
||||
} else {
|
||||
fail = put_user_u32(retvals[i], arg0 + i * 4);
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
/* Couldn't write back to argument block */
|
||||
goto do_fault;
|
||||
}
|
||||
}
|
||||
common_semi_set_ret(cs, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
case TARGET_SYS_EXIT:
|
||||
case TARGET_SYS_EXIT_EXTENDED:
|
||||
{
|
||||
uint32_t ret;
|
||||
|
||||
if (nr == TARGET_SYS_EXIT_EXTENDED ||
|
||||
common_semi_sys_exit_is_extended(cs)) {
|
||||
/*
|
||||
* The A64 version of SYS_EXIT takes a parameter block,
|
||||
* so the application-exit type can return a subcode which
|
||||
* is the exit status code from the application.
|
||||
* SYS_EXIT_EXTENDED is an a new-in-v2.0 optional function
|
||||
* which allows A32/T32 guests to also provide a status code.
|
||||
*/
|
||||
GET_ARG(0);
|
||||
GET_ARG(1);
|
||||
|
||||
if (arg0 == ADP_Stopped_ApplicationExit) {
|
||||
ret = arg1;
|
||||
} else {
|
||||
ret = 1;
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* The A32/T32 version of SYS_EXIT specifies only
|
||||
* Stopped_ApplicationExit as normal exit, but does not
|
||||
* allow the guest to specify the exit status code.
|
||||
* Everything else is considered an error.
|
||||
*/
|
||||
ret = (args == ADP_Stopped_ApplicationExit) ? 0 : 1;
|
||||
}
|
||||
gdb_exit(ret);
|
||||
exit(ret);
|
||||
}
|
||||
|
||||
case TARGET_SYS_ELAPSED:
|
||||
elapsed = get_clock() - clock_start;
|
||||
if (is_64bit_semihosting(env)) {
|
||||
if (SET_ARG(0, elapsed)) {
|
||||
goto do_fault;
|
||||
}
|
||||
} else {
|
||||
if (SET_ARG(0, (uint32_t) elapsed) ||
|
||||
SET_ARG(1, (uint32_t) (elapsed >> 32))) {
|
||||
goto do_fault;
|
||||
}
|
||||
}
|
||||
common_semi_set_ret(cs, 0);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_TICKFREQ:
|
||||
/* qemu always uses nsec */
|
||||
common_semi_set_ret(cs, 1000000000);
|
||||
break;
|
||||
|
||||
case TARGET_SYS_SYNCCACHE:
|
||||
/*
|
||||
* Clean the D-cache and invalidate the I-cache for the specified
|
||||
* virtual address range. This is a nop for us since we don't
|
||||
* implement caches. This is only present on A64.
|
||||
*/
|
||||
if (common_semi_has_synccache(env)) {
|
||||
common_semi_set_ret(cs, 0);
|
||||
break;
|
||||
}
|
||||
/* fall through */
|
||||
default:
|
||||
fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
|
||||
cpu_dump_state(cs, stderr, 0);
|
||||
abort();
|
||||
|
||||
do_fault:
|
||||
common_semi_cb(cs, -1, EFAULT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* 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
|
||||
*
|
||||
* Semihosting is slightly strange in that it is also supported by some
|
||||
* linux-user targets. However in that use case no configuration of
|
||||
* the outputs and command lines is supported.
|
||||
*
|
||||
* The config module is common to all system targets however as vl.c
|
||||
* 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"
|
||||
#include "semihosting/semihost.h"
|
||||
#include "chardev/char.h"
|
||||
|
||||
QemuOptsList qemu_semihosting_config_opts = {
|
||||
.name = "semihosting-config",
|
||||
.merge_lists = true,
|
||||
.implied_opt_name = "enable",
|
||||
.head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
|
||||
.desc = {
|
||||
{
|
||||
.name = "enable",
|
||||
.type = QEMU_OPT_BOOL,
|
||||
}, {
|
||||
.name = "userspace",
|
||||
.type = QEMU_OPT_BOOL,
|
||||
}, {
|
||||
.name = "target",
|
||||
.type = QEMU_OPT_STRING,
|
||||
}, {
|
||||
.name = "chardev",
|
||||
.type = QEMU_OPT_STRING,
|
||||
}, {
|
||||
.name = "arg",
|
||||
.type = QEMU_OPT_STRING,
|
||||
},
|
||||
{ /* end of list */ }
|
||||
},
|
||||
};
|
||||
|
||||
typedef struct SemihostingConfig {
|
||||
bool enabled;
|
||||
bool userspace_enabled;
|
||||
SemihostingTarget target;
|
||||
char **argv;
|
||||
int argc;
|
||||
const char *cmdline; /* concatenated argv */
|
||||
} SemihostingConfig;
|
||||
|
||||
static SemihostingConfig semihosting;
|
||||
static const char *semihost_chardev;
|
||||
|
||||
bool semihosting_enabled(bool is_user)
|
||||
{
|
||||
return semihosting.enabled && (!is_user || semihosting.userspace_enabled);
|
||||
}
|
||||
|
||||
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 */
|
||||
s->argv = g_renew(char *, s->argv, s->argc + 1);
|
||||
s->argv[s->argc - 1] = g_strdup(val);
|
||||
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;
|
||||
g_autofree char *cmd_dup = g_strdup(cmd);
|
||||
|
||||
/* argv[0] */
|
||||
add_semihosting_arg(&semihosting, "arg", file, NULL);
|
||||
|
||||
/* split -append and initialize argv[1..n] */
|
||||
cmd_token = strtok(cmd_dup, " ");
|
||||
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;
|
||||
}
|
||||
|
||||
int qemu_semihosting_config_options(const char *optstr)
|
||||
{
|
||||
QemuOptsList *opt_list = qemu_find_opts("semihosting-config");
|
||||
QemuOpts *opts = qemu_opts_parse_noisily(opt_list, optstr, false);
|
||||
|
||||
semihosting.enabled = true;
|
||||
|
||||
if (opts != NULL) {
|
||||
semihosting.enabled = qemu_opt_get_bool(opts, "enable",
|
||||
true);
|
||||
semihosting.userspace_enabled = qemu_opt_get_bool(opts, "userspace",
|
||||
false);
|
||||
const char *target = qemu_opt_get(opts, "target");
|
||||
/* setup of chardev is deferred until they are initialised */
|
||||
semihost_chardev = qemu_opt_get(opts, "chardev");
|
||||
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",
|
||||
optstr);
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
semihosting.target = SEMIHOSTING_TARGET_AUTO;
|
||||
}
|
||||
/* Set semihosting argument count and vector */
|
||||
qemu_opt_foreach(opts, add_semihosting_arg,
|
||||
&semihosting, NULL);
|
||||
} else {
|
||||
error_report("unsupported semihosting-config %s", optstr);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* We had to defer this until chardevs were created */
|
||||
void qemu_semihosting_chardev_init(void)
|
||||
{
|
||||
Chardev *chr = NULL;
|
||||
|
||||
if (semihost_chardev) {
|
||||
chr = qemu_chr_find(semihost_chardev);
|
||||
if (chr == NULL) {
|
||||
error_report("semihosting chardev '%s' not found",
|
||||
semihost_chardev);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
qemu_semihosting_console_init(chr);
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/*
|
||||
* Semihosting Console Support
|
||||
*
|
||||
* Copyright (c) 2015 Imagination Technologies
|
||||
* Copyright (c) 2019 Linaro Ltd
|
||||
*
|
||||
* This provides support for outputting to a semihosting console.
|
||||
*
|
||||
* While most semihosting implementations support reading and writing
|
||||
* to arbitrary file descriptors we treat the console as something
|
||||
* specifically for debugging interaction. This means messages can be
|
||||
* re-directed to gdb (if currently being used to debug) or even
|
||||
* re-directed elsewhere.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "semihosting/semihost.h"
|
||||
#include "semihosting/console.h"
|
||||
#include "accel/tcg/cpu-loop.h"
|
||||
#include "exec/cpu-common.h"
|
||||
#include "exec/gdbstub.h"
|
||||
#include "qemu/log.h"
|
||||
#include "chardev/char.h"
|
||||
#include "chardev/char-fe.h"
|
||||
#include "qemu/main-loop.h"
|
||||
#include "qapi/error.h"
|
||||
#include "qemu/fifo8.h"
|
||||
#include "hw/core/cpu.h"
|
||||
|
||||
/* Access to this structure is protected by the BQL */
|
||||
typedef struct SemihostingConsole {
|
||||
CharFrontend frontend;
|
||||
Chardev *chr;
|
||||
GSList *sleeping_cpus;
|
||||
bool got;
|
||||
Fifo8 fifo;
|
||||
} SemihostingConsole;
|
||||
|
||||
static SemihostingConsole console;
|
||||
|
||||
#define FIFO_SIZE 1024
|
||||
|
||||
static int console_can_read(void *opaque)
|
||||
{
|
||||
SemihostingConsole *c = opaque;
|
||||
g_assert(bql_locked());
|
||||
return (int)fifo8_num_free(&c->fifo);
|
||||
}
|
||||
|
||||
static void console_wake_up(gpointer data, gpointer user_data)
|
||||
{
|
||||
CPUState *cs = (CPUState *) data;
|
||||
/* cpu_handle_halt won't know we have work so just unbung here */
|
||||
cs->halted = 0;
|
||||
qemu_cpu_kick(cs);
|
||||
}
|
||||
|
||||
static void console_read(void *opaque, const uint8_t *buf, int size)
|
||||
{
|
||||
SemihostingConsole *c = opaque;
|
||||
g_assert(bql_locked());
|
||||
while (size-- && !fifo8_is_full(&c->fifo)) {
|
||||
fifo8_push(&c->fifo, *buf++);
|
||||
}
|
||||
g_slist_foreach(c->sleeping_cpus, console_wake_up, NULL);
|
||||
c->sleeping_cpus = NULL;
|
||||
}
|
||||
|
||||
bool qemu_semihosting_console_ready(void)
|
||||
{
|
||||
SemihostingConsole *c = &console;
|
||||
|
||||
g_assert(bql_locked());
|
||||
return !fifo8_is_empty(&c->fifo);
|
||||
}
|
||||
|
||||
void qemu_semihosting_console_block_until_ready(CPUState *cs)
|
||||
{
|
||||
SemihostingConsole *c = &console;
|
||||
|
||||
g_assert(bql_locked());
|
||||
|
||||
/* Block if the fifo is completely empty. */
|
||||
if (fifo8_is_empty(&c->fifo)) {
|
||||
c->sleeping_cpus = g_slist_prepend(c->sleeping_cpus, cs);
|
||||
cs->halted = 1;
|
||||
cs->exception_index = EXCP_HALTED;
|
||||
cpu_loop_exit(cs);
|
||||
/* never returns */
|
||||
}
|
||||
}
|
||||
|
||||
int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
|
||||
{
|
||||
SemihostingConsole *c = &console;
|
||||
int ret = 0;
|
||||
|
||||
qemu_semihosting_console_block_until_ready(cs);
|
||||
|
||||
/* Read until buffer full or fifo exhausted. */
|
||||
do {
|
||||
*(char *)(buf + ret) = fifo8_pop(&c->fifo);
|
||||
ret++;
|
||||
} while (ret < len && !fifo8_is_empty(&c->fifo));
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int qemu_semihosting_console_write(void *buf, int len)
|
||||
{
|
||||
if (console.chr) {
|
||||
int r = qemu_chr_write_all(console.chr, (uint8_t *)buf, len);
|
||||
return r < 0 ? 0 : r;
|
||||
} else {
|
||||
return fwrite(buf, 1, len, stderr);
|
||||
}
|
||||
}
|
||||
|
||||
void qemu_semihosting_console_init(Chardev *chr)
|
||||
{
|
||||
console.chr = chr;
|
||||
if (chr) {
|
||||
fifo8_create(&console.fifo, FIFO_SIZE);
|
||||
qemu_chr_fe_init(&console.frontend, chr, &error_abort);
|
||||
qemu_chr_fe_set_handlers(&console.frontend,
|
||||
console_can_read,
|
||||
console_read,
|
||||
NULL, NULL, &console,
|
||||
NULL, true);
|
||||
}
|
||||
|
||||
qemu_semihosting_guestfd_init();
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Hosted file support for semihosting syscalls.
|
||||
*
|
||||
* Copyright (c) 2005, 2007 CodeSourcery.
|
||||
* Copyright (c) 2019 Linaro
|
||||
* Copyright © 2020 by Keith Packard <[email protected]>
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "gdbstub/syscalls.h"
|
||||
#include "semihosting/semihost.h"
|
||||
#include "semihosting/guestfd.h"
|
||||
|
||||
static GArray *guestfd_array;
|
||||
|
||||
void qemu_semihosting_guestfd_init(void)
|
||||
{
|
||||
/* New entries zero-initialized, i.e. type GuestFDUnused */
|
||||
guestfd_array = g_array_new(FALSE, TRUE, sizeof(GuestFD));
|
||||
|
||||
if (semihosting_arm_compatible()) {
|
||||
semihosting_arm_compatible_init();
|
||||
return;
|
||||
}
|
||||
|
||||
/* Out of ARM, the stdio file descriptors apply. */
|
||||
guestfd_array = g_array_set_size(guestfd_array, 3);
|
||||
#ifndef CONFIG_USER_ONLY
|
||||
if (!use_gdb_syscalls()) {
|
||||
GuestFD *gf = &g_array_index(guestfd_array, GuestFD, 0);
|
||||
gf[0].type = GuestFDConsole;
|
||||
gf[1].type = GuestFDConsole;
|
||||
gf[2].type = GuestFDConsole;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
associate_guestfd(0, 0);
|
||||
associate_guestfd(1, 1);
|
||||
associate_guestfd(2, 2);
|
||||
}
|
||||
|
||||
/*
|
||||
* Allocate a new guest file descriptor and return it; if we
|
||||
* couldn't allocate a new fd then return -1.
|
||||
* This is a fairly simplistic implementation because we don't
|
||||
* expect that most semihosting guest programs will make very
|
||||
* heavy use of opening and closing fds.
|
||||
*/
|
||||
int alloc_guestfd(void)
|
||||
{
|
||||
guint i;
|
||||
|
||||
/* SYS_OPEN should return nonzero handle on success. Start guestfd from 1 */
|
||||
for (i = 1; i < guestfd_array->len; i++) {
|
||||
GuestFD *gf = &g_array_index(guestfd_array, GuestFD, i);
|
||||
|
||||
if (gf->type == GuestFDUnused) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
/* All elements already in use: expand the array */
|
||||
g_array_set_size(guestfd_array, i + 1);
|
||||
return i;
|
||||
}
|
||||
|
||||
static void do_dealloc_guestfd(GuestFD *gf)
|
||||
{
|
||||
gf->type = GuestFDUnused;
|
||||
}
|
||||
|
||||
/*
|
||||
* Look up the guestfd in the data structure; return NULL
|
||||
* for out of bounds, but don't check whether the slot is unused.
|
||||
* This is used internally by the other guestfd functions.
|
||||
*/
|
||||
static GuestFD *do_get_guestfd(int guestfd)
|
||||
{
|
||||
if (guestfd < 0 || guestfd >= guestfd_array->len) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return &g_array_index(guestfd_array, GuestFD, guestfd);
|
||||
}
|
||||
|
||||
/*
|
||||
* Given a guest file descriptor, get the associated struct.
|
||||
* If the fd is not valid, return NULL. This is the function
|
||||
* used by the various semihosting calls to validate a handle
|
||||
* from the guest.
|
||||
* Note: calling alloc_guestfd() or dealloc_guestfd() will
|
||||
* invalidate any GuestFD* obtained by calling this function.
|
||||
*/
|
||||
GuestFD *get_guestfd(int guestfd)
|
||||
{
|
||||
GuestFD *gf = do_get_guestfd(guestfd);
|
||||
|
||||
if (!gf || gf->type == GuestFDUnused) {
|
||||
return NULL;
|
||||
}
|
||||
return gf;
|
||||
}
|
||||
|
||||
/*
|
||||
* Associate the specified guest fd (which must have been
|
||||
* allocated via alloc_fd() and not previously used) with
|
||||
* the specified host/gdb fd.
|
||||
*/
|
||||
void associate_guestfd(int guestfd, int hostfd)
|
||||
{
|
||||
GuestFD *gf = do_get_guestfd(guestfd);
|
||||
|
||||
assert(gf);
|
||||
gf->type = use_gdb_syscalls() ? GuestFDGDB : GuestFDHost;
|
||||
gf->hostfd = hostfd;
|
||||
}
|
||||
|
||||
void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len)
|
||||
{
|
||||
GuestFD *gf = do_get_guestfd(guestfd);
|
||||
|
||||
assert(gf);
|
||||
gf->type = GuestFDStatic;
|
||||
gf->staticfile.data = data;
|
||||
gf->staticfile.len = len;
|
||||
gf->staticfile.off = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Deallocate the specified guest file descriptor. This doesn't
|
||||
* close the host fd, it merely undoes the work of alloc_fd().
|
||||
*/
|
||||
void dealloc_guestfd(int guestfd)
|
||||
{
|
||||
GuestFD *gf = do_get_guestfd(guestfd);
|
||||
|
||||
assert(gf);
|
||||
do_dealloc_guestfd(gf);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
stub_ss.add(files('stubs-all.c'))
|
||||
user_ss.add(when: 'CONFIG_SEMIHOSTING', if_true: files(
|
||||
'user.c',
|
||||
'guestfd.c'))
|
||||
system_ss.add(when: 'CONFIG_SEMIHOSTING', if_true: files(
|
||||
'config.c',
|
||||
'console.c',
|
||||
'guestfd.c',
|
||||
'uaccess.c',
|
||||
'syscalls.c',
|
||||
))
|
||||
stub_ss.add(files(
|
||||
'stubs-system.c',
|
||||
))
|
||||
system_ss.add(when: 'CONFIG_ARM_COMPATIBLE_SEMIHOSTING',
|
||||
if_true: files('arm-compat-semi.c'))
|
||||
stub_ss.add(files('arm-compat-semi-stub.c'))
|
||||
|
||||
specific_ss.add(when: ['CONFIG_SEMIHOSTING', 'CONFIG_USER_ONLY'],
|
||||
if_true: files('syscalls.c'))
|
||||
specific_ss.add(when: ['CONFIG_ARM_COMPATIBLE_SEMIHOSTING', 'CONFIG_USER_ONLY'],
|
||||
if_true: files('arm-compat-semi.c'))
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Semihosting Stubs for all targets
|
||||
*
|
||||
* Copyright (c) 2023 Linaro Ltd
|
||||
*
|
||||
* Stubs for all targets that don't actually do semihosting.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "semihosting/semihost.h"
|
||||
|
||||
/* Queries to config status default to off */
|
||||
bool semihosting_enabled(bool is_user)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
SemihostingTarget semihosting_get_target(void)
|
||||
{
|
||||
return SEMIHOSTING_TARGET_AUTO;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Semihosting Stubs for system emulation
|
||||
*
|
||||
* Copyright (c) 2019 Linaro Ltd
|
||||
*
|
||||
* Stubs for system targets that don't actually do semihosting.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/option.h"
|
||||
#include "qemu/error-report.h"
|
||||
#include "semihosting/semihost.h"
|
||||
|
||||
/* Empty config */
|
||||
QemuOptsList qemu_semihosting_config_opts = {
|
||||
.name = "",
|
||||
.head = QTAILQ_HEAD_INITIALIZER(qemu_semihosting_config_opts.head),
|
||||
.desc = {
|
||||
{ /* end of list */ }
|
||||
},
|
||||
};
|
||||
|
||||
/*
|
||||
* All the rest are empty subs. We could g_assert_not_reached() but
|
||||
* that adds extra weight to the final binary. Waste not want not.
|
||||
*/
|
||||
void qemu_semihosting_enable(void)
|
||||
{
|
||||
}
|
||||
|
||||
int qemu_semihosting_config_options(const char *optstr)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *semihosting_get_arg(int i)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int semihosting_get_argc(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *semihosting_get_cmdline(void)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void semihosting_arg_fallback(const char *file, const char *cmd)
|
||||
{
|
||||
}
|
||||
|
||||
void qemu_semihosting_chardev_init(void)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,984 @@
|
||||
/*
|
||||
* Syscall implementations for semihosting.
|
||||
*
|
||||
* Copyright (c) 2022 Linaro
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/log.h"
|
||||
#include "gdbstub/syscalls.h"
|
||||
#include "semihosting/guestfd.h"
|
||||
#include "semihosting/syscalls.h"
|
||||
#include "semihosting/console.h"
|
||||
#ifdef CONFIG_USER_ONLY
|
||||
#include "qemu.h"
|
||||
#else
|
||||
#include "semihosting/uaccess.h"
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Validate or compute the length of the string (including terminator).
|
||||
*/
|
||||
static int validate_strlen(CPUState *cs, vaddr str, uint64_t tlen)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
char c;
|
||||
|
||||
if (tlen == 0) {
|
||||
ssize_t slen = target_strlen(str);
|
||||
|
||||
if (slen < 0) {
|
||||
return -EFAULT;
|
||||
}
|
||||
if (slen >= INT32_MAX) {
|
||||
return -ENAMETOOLONG;
|
||||
}
|
||||
return slen + 1;
|
||||
}
|
||||
if (tlen > INT32_MAX) {
|
||||
return -ENAMETOOLONG;
|
||||
}
|
||||
if (get_user_u8(c, str + tlen - 1)) {
|
||||
return -EFAULT;
|
||||
}
|
||||
if (c != 0) {
|
||||
return -EINVAL;
|
||||
}
|
||||
return tlen;
|
||||
}
|
||||
|
||||
static int validate_lock_user_string(char **pstr, CPUState *cs,
|
||||
vaddr tstr, uint64_t tlen)
|
||||
{
|
||||
int ret = validate_strlen(cs, tstr, tlen);
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
char *str = NULL;
|
||||
|
||||
if (ret > 0) {
|
||||
str = lock_user(VERIFY_READ, tstr, ret, true);
|
||||
ret = str ? 0 : -EFAULT;
|
||||
}
|
||||
*pstr = str;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO: Note that gdb always stores the stat structure big-endian.
|
||||
* So far, that's ok, as the only two targets using this are also
|
||||
* big-endian. Until we do something with gdb, also produce the
|
||||
* same big-endian result from the host.
|
||||
*/
|
||||
static int copy_stat_to_user(CPUState *cs, vaddr addr,
|
||||
const struct stat *s)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
struct gdb_stat *p;
|
||||
|
||||
if (s->st_dev != (uint32_t)s->st_dev ||
|
||||
s->st_ino != (uint32_t)s->st_ino) {
|
||||
return -EOVERFLOW;
|
||||
}
|
||||
|
||||
p = lock_user(VERIFY_WRITE, addr, sizeof(struct gdb_stat), 0);
|
||||
if (!p) {
|
||||
return -EFAULT;
|
||||
}
|
||||
|
||||
p->gdb_st_dev = cpu_to_be32(s->st_dev);
|
||||
p->gdb_st_ino = cpu_to_be32(s->st_ino);
|
||||
p->gdb_st_mode = cpu_to_be32(s->st_mode);
|
||||
p->gdb_st_nlink = cpu_to_be32(s->st_nlink);
|
||||
p->gdb_st_uid = cpu_to_be32(s->st_uid);
|
||||
p->gdb_st_gid = cpu_to_be32(s->st_gid);
|
||||
p->gdb_st_rdev = cpu_to_be32(s->st_rdev);
|
||||
p->gdb_st_size = cpu_to_be64(s->st_size);
|
||||
#ifdef _WIN32
|
||||
/* Windows stat is missing some fields. */
|
||||
p->gdb_st_blksize = 0;
|
||||
p->gdb_st_blocks = 0;
|
||||
#else
|
||||
p->gdb_st_blksize = cpu_to_be64(s->st_blksize);
|
||||
p->gdb_st_blocks = cpu_to_be64(s->st_blocks);
|
||||
#endif
|
||||
p->gdb_st_atime = cpu_to_be32(s->st_atime);
|
||||
p->gdb_st_mtime = cpu_to_be32(s->st_mtime);
|
||||
p->gdb_st_ctime = cpu_to_be32(s->st_ctime);
|
||||
|
||||
unlock_user(p, addr, sizeof(struct gdb_stat));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* GDB semihosting syscall implementations.
|
||||
*/
|
||||
|
||||
static gdb_syscall_complete_cb gdb_open_complete;
|
||||
|
||||
static void gdb_open_cb(CPUState *cs, uint64_t ret, int err)
|
||||
{
|
||||
if (!err) {
|
||||
int guestfd = alloc_guestfd();
|
||||
associate_guestfd(guestfd, ret);
|
||||
ret = guestfd;
|
||||
}
|
||||
gdb_open_complete(cs, ret, err);
|
||||
}
|
||||
|
||||
static void gdb_open(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr fname, uint64_t fname_len,
|
||||
int gdb_flags, int mode)
|
||||
{
|
||||
int len = validate_strlen(cs, fname, fname_len);
|
||||
if (len < 0) {
|
||||
complete(cs, -1, -len);
|
||||
return;
|
||||
}
|
||||
|
||||
gdb_open_complete = complete;
|
||||
gdb_do_syscall(gdb_open_cb, "open,%s,%x,%x",
|
||||
(vaddr)fname, (uint32_t)len,
|
||||
(uint32_t)gdb_flags, (uint32_t)mode);
|
||||
}
|
||||
|
||||
static void gdb_close(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf)
|
||||
{
|
||||
gdb_do_syscall(complete, "close,%x", (uint32_t)gf->hostfd);
|
||||
}
|
||||
|
||||
static void gdb_read(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr buf, uint64_t len)
|
||||
{
|
||||
gdb_do_syscall(complete, "read,%x,%lx,%lx",
|
||||
(uint32_t)gf->hostfd, (vaddr)buf, (uint64_t)len);
|
||||
}
|
||||
|
||||
static void gdb_write(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr buf, uint64_t len)
|
||||
{
|
||||
gdb_do_syscall(complete, "write,%x,%lx,%lx",
|
||||
(uint32_t)gf->hostfd, (vaddr)buf, (uint64_t)len);
|
||||
}
|
||||
|
||||
static void gdb_lseek(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, int64_t off, int gdb_whence)
|
||||
{
|
||||
gdb_do_syscall(complete, "lseek,%x,%lx,%x",
|
||||
(uint32_t)gf->hostfd, off, (uint32_t)gdb_whence);
|
||||
}
|
||||
|
||||
static void gdb_isatty(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf)
|
||||
{
|
||||
gdb_do_syscall(complete, "isatty,%x", (uint32_t)gf->hostfd);
|
||||
}
|
||||
|
||||
static void gdb_fstat(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr addr)
|
||||
{
|
||||
gdb_do_syscall(complete, "fstat,%x,%lx",
|
||||
(uint32_t)gf->hostfd, (vaddr)addr);
|
||||
}
|
||||
|
||||
static void gdb_stat(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr fname, uint64_t fname_len,
|
||||
vaddr addr)
|
||||
{
|
||||
int len = validate_strlen(cs, fname, fname_len);
|
||||
if (len < 0) {
|
||||
complete(cs, -1, -len);
|
||||
return;
|
||||
}
|
||||
|
||||
gdb_do_syscall(complete, "stat,%s,%lx",
|
||||
(vaddr)fname, (uint32_t)len, (vaddr)addr);
|
||||
}
|
||||
|
||||
static void gdb_remove(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr fname, uint64_t fname_len)
|
||||
{
|
||||
int len = validate_strlen(cs, fname, fname_len);
|
||||
if (len < 0) {
|
||||
complete(cs, -1, -len);
|
||||
return;
|
||||
}
|
||||
|
||||
gdb_do_syscall(complete, "unlink,%s", (vaddr)fname, (uint32_t)len);
|
||||
}
|
||||
|
||||
static void gdb_rename(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr oname, uint64_t oname_len,
|
||||
vaddr nname, uint64_t nname_len)
|
||||
{
|
||||
int olen, nlen;
|
||||
|
||||
olen = validate_strlen(cs, oname, oname_len);
|
||||
if (olen < 0) {
|
||||
complete(cs, -1, -olen);
|
||||
return;
|
||||
}
|
||||
nlen = validate_strlen(cs, nname, nname_len);
|
||||
if (nlen < 0) {
|
||||
complete(cs, -1, -nlen);
|
||||
return;
|
||||
}
|
||||
|
||||
gdb_do_syscall(complete, "rename,%s,%s",
|
||||
(vaddr)oname, (uint32_t)olen,
|
||||
(vaddr)nname, (uint32_t)nlen);
|
||||
}
|
||||
|
||||
static void gdb_system(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr cmd, uint64_t cmd_len)
|
||||
{
|
||||
int len = validate_strlen(cs, cmd, cmd_len);
|
||||
if (len < 0) {
|
||||
complete(cs, -1, -len);
|
||||
return;
|
||||
}
|
||||
|
||||
gdb_do_syscall(complete, "system,%s", (vaddr)cmd, (uint32_t)len);
|
||||
}
|
||||
|
||||
static void gdb_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr tv_addr, vaddr tz_addr)
|
||||
{
|
||||
gdb_do_syscall(complete, "gettimeofday,%lx,%lx",
|
||||
(vaddr)tv_addr, (vaddr)tz_addr);
|
||||
}
|
||||
|
||||
/*
|
||||
* Host semihosting syscall implementations.
|
||||
*/
|
||||
|
||||
static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr fname, uint64_t fname_len,
|
||||
int gdb_flags, int mode)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
char *p;
|
||||
int ret, host_flags = O_BINARY;
|
||||
|
||||
ret = validate_lock_user_string(&p, cs, fname, fname_len);
|
||||
if (ret < 0) {
|
||||
complete(cs, -1, -ret);
|
||||
return;
|
||||
}
|
||||
|
||||
if (gdb_flags & GDB_O_WRONLY) {
|
||||
host_flags |= O_WRONLY;
|
||||
} else if (gdb_flags & GDB_O_RDWR) {
|
||||
host_flags |= O_RDWR;
|
||||
} else {
|
||||
host_flags |= O_RDONLY;
|
||||
}
|
||||
if (gdb_flags & GDB_O_CREAT) {
|
||||
host_flags |= O_CREAT;
|
||||
}
|
||||
if (gdb_flags & GDB_O_TRUNC) {
|
||||
host_flags |= O_TRUNC;
|
||||
}
|
||||
if (gdb_flags & GDB_O_EXCL) {
|
||||
host_flags |= O_EXCL;
|
||||
}
|
||||
|
||||
ret = open(p, host_flags, mode);
|
||||
if (ret < 0) {
|
||||
qemu_log_mask(LOG_GUEST_ERROR, "%s: failed to open %s\n", __func__, p);
|
||||
complete(cs, -1, errno);
|
||||
} else {
|
||||
int guestfd = alloc_guestfd();
|
||||
associate_guestfd(guestfd, ret);
|
||||
complete(cs, guestfd, 0);
|
||||
}
|
||||
unlock_user(p, fname, 0);
|
||||
}
|
||||
|
||||
static void host_close(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf)
|
||||
{
|
||||
/*
|
||||
* Only close the underlying host fd if it's one we opened on behalf
|
||||
* of the guest in SYS_OPEN.
|
||||
*/
|
||||
if (gf->hostfd != STDIN_FILENO &&
|
||||
gf->hostfd != STDOUT_FILENO &&
|
||||
gf->hostfd != STDERR_FILENO &&
|
||||
close(gf->hostfd) < 0) {
|
||||
complete(cs, -1, errno);
|
||||
} else {
|
||||
complete(cs, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void host_read(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr buf, uint64_t len)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
void *ptr = lock_user(VERIFY_WRITE, buf, len, 0);
|
||||
ssize_t ret;
|
||||
|
||||
if (!ptr) {
|
||||
complete(cs, -1, EFAULT);
|
||||
return;
|
||||
}
|
||||
ret = RETRY_ON_EINTR(read(gf->hostfd, ptr, len));
|
||||
if (ret == -1) {
|
||||
unlock_user(ptr, buf, 0);
|
||||
complete(cs, -1, errno);
|
||||
} else {
|
||||
unlock_user(ptr, buf, ret);
|
||||
complete(cs, ret, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void host_write(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr buf, uint64_t len)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
void *ptr = lock_user(VERIFY_READ, buf, len, 1);
|
||||
ssize_t ret;
|
||||
|
||||
if (!ptr) {
|
||||
complete(cs, -1, EFAULT);
|
||||
return;
|
||||
}
|
||||
ret = write(gf->hostfd, ptr, len);
|
||||
unlock_user(ptr, buf, 0);
|
||||
complete(cs, ret, ret == -1 ? errno : 0);
|
||||
}
|
||||
|
||||
static void host_lseek(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, int64_t off, int whence)
|
||||
{
|
||||
/* So far, all hosts use the same values. */
|
||||
QEMU_BUILD_BUG_ON(GDB_SEEK_SET != SEEK_SET);
|
||||
QEMU_BUILD_BUG_ON(GDB_SEEK_CUR != SEEK_CUR);
|
||||
QEMU_BUILD_BUG_ON(GDB_SEEK_END != SEEK_END);
|
||||
|
||||
off_t ret = off;
|
||||
int err = 0;
|
||||
|
||||
if (ret == off) {
|
||||
ret = lseek(gf->hostfd, ret, whence);
|
||||
if (ret == -1) {
|
||||
err = errno;
|
||||
}
|
||||
} else {
|
||||
ret = -1;
|
||||
err = EINVAL;
|
||||
}
|
||||
complete(cs, ret, err);
|
||||
}
|
||||
|
||||
static void host_isatty(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf)
|
||||
{
|
||||
int ret = isatty(gf->hostfd);
|
||||
complete(cs, ret, ret ? 0 : errno);
|
||||
}
|
||||
|
||||
static void host_flen(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf)
|
||||
{
|
||||
struct stat buf;
|
||||
|
||||
if (fstat(gf->hostfd, &buf) < 0) {
|
||||
complete(cs, -1, errno);
|
||||
} else {
|
||||
complete(cs, buf.st_size, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void host_fstat(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr addr)
|
||||
{
|
||||
struct stat buf;
|
||||
int ret;
|
||||
|
||||
ret = fstat(gf->hostfd, &buf);
|
||||
if (ret) {
|
||||
complete(cs, -1, errno);
|
||||
return;
|
||||
}
|
||||
ret = copy_stat_to_user(cs, addr, &buf);
|
||||
complete(cs, ret ? -1 : 0, ret ? -ret : 0);
|
||||
}
|
||||
|
||||
static void host_stat(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr fname, uint64_t fname_len,
|
||||
vaddr addr)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
struct stat buf;
|
||||
char *name;
|
||||
int ret, err;
|
||||
|
||||
ret = validate_lock_user_string(&name, cs, fname, fname_len);
|
||||
if (ret < 0) {
|
||||
complete(cs, -1, -ret);
|
||||
return;
|
||||
}
|
||||
|
||||
ret = stat(name, &buf);
|
||||
if (ret) {
|
||||
err = errno;
|
||||
} else {
|
||||
ret = copy_stat_to_user(cs, addr, &buf);
|
||||
err = 0;
|
||||
if (ret < 0) {
|
||||
err = -ret;
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
unlock_user(name, fname, 0);
|
||||
complete(cs, ret, err);
|
||||
}
|
||||
|
||||
static void host_remove(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr fname, uint64_t fname_len)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
char *p;
|
||||
int ret;
|
||||
|
||||
ret = validate_lock_user_string(&p, cs, fname, fname_len);
|
||||
if (ret < 0) {
|
||||
complete(cs, -1, -ret);
|
||||
return;
|
||||
}
|
||||
|
||||
ret = remove(p);
|
||||
unlock_user(p, fname, 0);
|
||||
complete(cs, ret, ret ? errno : 0);
|
||||
}
|
||||
|
||||
static void host_rename(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr oname, uint64_t oname_len,
|
||||
vaddr nname, uint64_t nname_len)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
char *ostr, *nstr;
|
||||
int ret;
|
||||
|
||||
ret = validate_lock_user_string(&ostr, cs, oname, oname_len);
|
||||
if (ret < 0) {
|
||||
complete(cs, -1, -ret);
|
||||
return;
|
||||
}
|
||||
ret = validate_lock_user_string(&nstr, cs, nname, nname_len);
|
||||
if (ret < 0) {
|
||||
unlock_user(ostr, oname, 0);
|
||||
complete(cs, -1, -ret);
|
||||
return;
|
||||
}
|
||||
|
||||
ret = rename(ostr, nstr);
|
||||
unlock_user(ostr, oname, 0);
|
||||
unlock_user(nstr, nname, 0);
|
||||
complete(cs, ret, ret ? errno : 0);
|
||||
}
|
||||
|
||||
static void host_system(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr cmd, uint64_t cmd_len)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
char *p;
|
||||
int ret;
|
||||
|
||||
ret = validate_lock_user_string(&p, cs, cmd, cmd_len);
|
||||
if (ret < 0) {
|
||||
complete(cs, -1, -ret);
|
||||
return;
|
||||
}
|
||||
|
||||
ret = system(p);
|
||||
unlock_user(p, cmd, 0);
|
||||
complete(cs, ret, ret == -1 ? errno : 0);
|
||||
}
|
||||
|
||||
static void host_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr tv_addr, vaddr tz_addr)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
struct gdb_timeval *p;
|
||||
int64_t rt;
|
||||
|
||||
/* GDB fails on non-null TZ, so be consistent. */
|
||||
if (tz_addr != 0) {
|
||||
complete(cs, -1, EINVAL);
|
||||
return;
|
||||
}
|
||||
|
||||
p = lock_user(VERIFY_WRITE, tv_addr, sizeof(struct gdb_timeval), 0);
|
||||
if (!p) {
|
||||
complete(cs, -1, EFAULT);
|
||||
return;
|
||||
}
|
||||
|
||||
/* TODO: Like stat, gdb always produces big-endian results; match it. */
|
||||
rt = g_get_real_time();
|
||||
p->tv_sec = cpu_to_be32(rt / G_USEC_PER_SEC);
|
||||
p->tv_usec = cpu_to_be64(rt % G_USEC_PER_SEC);
|
||||
unlock_user(p, tv_addr, sizeof(struct gdb_timeval));
|
||||
}
|
||||
|
||||
#ifndef CONFIG_USER_ONLY
|
||||
static void host_poll_one(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, GIOCondition cond, int timeout)
|
||||
{
|
||||
/*
|
||||
* Since this is only used by xtensa in system mode, and stdio is
|
||||
* handled through GuestFDConsole, and there are no semihosting
|
||||
* system calls for sockets and the like, that means this descriptor
|
||||
* must be a normal file. Normal files never block and are thus
|
||||
* always ready.
|
||||
*/
|
||||
complete(cs, cond & (G_IO_IN | G_IO_OUT), 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Static file semihosting syscall implementations.
|
||||
*/
|
||||
|
||||
static void staticfile_read(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr buf, uint64_t len)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
uint64_t rest = gf->staticfile.len - gf->staticfile.off;
|
||||
void *ptr;
|
||||
|
||||
if (len > rest) {
|
||||
len = rest;
|
||||
}
|
||||
ptr = lock_user(VERIFY_WRITE, buf, len, 0);
|
||||
if (!ptr) {
|
||||
complete(cs, -1, EFAULT);
|
||||
return;
|
||||
}
|
||||
memcpy(ptr, gf->staticfile.data + gf->staticfile.off, len);
|
||||
gf->staticfile.off += len;
|
||||
unlock_user(ptr, buf, len);
|
||||
complete(cs, len, 0);
|
||||
}
|
||||
|
||||
static void staticfile_lseek(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, int64_t off, int gdb_whence)
|
||||
{
|
||||
int64_t ret;
|
||||
|
||||
switch (gdb_whence) {
|
||||
case GDB_SEEK_SET:
|
||||
ret = off;
|
||||
break;
|
||||
case GDB_SEEK_CUR:
|
||||
ret = gf->staticfile.off + off;
|
||||
break;
|
||||
case GDB_SEEK_END:
|
||||
ret = gf->staticfile.len + off;
|
||||
break;
|
||||
default:
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
if (ret >= 0 && ret <= gf->staticfile.len) {
|
||||
gf->staticfile.off = ret;
|
||||
complete(cs, ret, 0);
|
||||
} else {
|
||||
complete(cs, -1, EINVAL);
|
||||
}
|
||||
}
|
||||
|
||||
static void staticfile_flen(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf)
|
||||
{
|
||||
complete(cs, gf->staticfile.len, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* Console semihosting syscall implementations.
|
||||
*/
|
||||
|
||||
static void console_read(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr buf, uint64_t len)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
char *ptr;
|
||||
int ret;
|
||||
|
||||
ptr = lock_user(VERIFY_WRITE, buf, len, 0);
|
||||
if (!ptr) {
|
||||
complete(cs, -1, EFAULT);
|
||||
return;
|
||||
}
|
||||
ret = qemu_semihosting_console_read(cs, ptr, len);
|
||||
unlock_user(ptr, buf, ret);
|
||||
complete(cs, ret, 0);
|
||||
}
|
||||
|
||||
static void console_write(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr buf, uint64_t len)
|
||||
{
|
||||
CPUArchState *env G_GNUC_UNUSED = cpu_env(cs);
|
||||
char *ptr = lock_user(VERIFY_READ, buf, len, 1);
|
||||
int ret;
|
||||
|
||||
if (!ptr) {
|
||||
complete(cs, -1, EFAULT);
|
||||
return;
|
||||
}
|
||||
ret = qemu_semihosting_console_write(ptr, len);
|
||||
unlock_user(ptr, buf, 0);
|
||||
complete(cs, ret ? ret : -1, ret ? 0 : EIO);
|
||||
}
|
||||
|
||||
static void console_fstat(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr addr)
|
||||
{
|
||||
static const struct stat tty_buf = {
|
||||
.st_mode = 020666, /* S_IFCHR, ugo+rw */
|
||||
.st_rdev = 5, /* makedev(5, 0) -- linux /dev/tty */
|
||||
};
|
||||
int ret;
|
||||
|
||||
ret = copy_stat_to_user(cs, addr, &tty_buf);
|
||||
complete(cs, ret ? -1 : 0, ret ? -ret : 0);
|
||||
}
|
||||
|
||||
#ifndef CONFIG_USER_ONLY
|
||||
static void console_poll_one(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, GIOCondition cond, int timeout)
|
||||
{
|
||||
/* The semihosting console does not support urgent data or errors. */
|
||||
cond &= G_IO_IN | G_IO_OUT;
|
||||
|
||||
/*
|
||||
* Since qemu_semihosting_console_write never blocks, we can
|
||||
* consider output always ready -- leave G_IO_OUT alone.
|
||||
* All that remains is to conditionally signal input ready.
|
||||
* Since output ready causes an immediate return, only block
|
||||
* for G_IO_IN alone.
|
||||
*
|
||||
* TODO: Implement proper timeout. For now, only support
|
||||
* indefinite wait or immediate poll.
|
||||
*/
|
||||
if (cond == G_IO_IN && timeout < 0) {
|
||||
qemu_semihosting_console_block_until_ready(cs);
|
||||
/* We returned -- input must be ready. */
|
||||
} else if ((cond & G_IO_IN) && !qemu_semihosting_console_ready()) {
|
||||
cond &= ~G_IO_IN;
|
||||
}
|
||||
|
||||
complete(cs, cond, 0);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Syscall entry points.
|
||||
*/
|
||||
|
||||
void semihost_sys_open(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr fname, uint64_t fname_len,
|
||||
int gdb_flags, int mode)
|
||||
{
|
||||
if (use_gdb_syscalls()) {
|
||||
gdb_open(cs, complete, fname, fname_len, gdb_flags, mode);
|
||||
} else {
|
||||
host_open(cs, complete, fname, fname_len, gdb_flags, mode);
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_close(CPUState *cs, gdb_syscall_complete_cb complete, int fd)
|
||||
{
|
||||
GuestFD *gf = get_guestfd(fd);
|
||||
|
||||
if (!gf) {
|
||||
complete(cs, -1, EBADF);
|
||||
return;
|
||||
}
|
||||
switch (gf->type) {
|
||||
case GuestFDGDB:
|
||||
gdb_close(cs, complete, gf);
|
||||
break;
|
||||
case GuestFDHost:
|
||||
host_close(cs, complete, gf);
|
||||
break;
|
||||
case GuestFDStatic:
|
||||
case GuestFDConsole:
|
||||
complete(cs, 0, 0);
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
dealloc_guestfd(fd);
|
||||
}
|
||||
|
||||
void semihost_sys_read_gf(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr buf, uint64_t len)
|
||||
{
|
||||
/*
|
||||
* Bound length for 64-bit guests on 32-bit hosts, not overflowing ssize_t.
|
||||
* Note the Linux kernel does this with MAX_RW_COUNT, so it's not a bad
|
||||
* idea to do this unconditionally.
|
||||
*/
|
||||
if (len > INT32_MAX) {
|
||||
len = INT32_MAX;
|
||||
}
|
||||
switch (gf->type) {
|
||||
case GuestFDGDB:
|
||||
gdb_read(cs, complete, gf, buf, len);
|
||||
break;
|
||||
case GuestFDHost:
|
||||
host_read(cs, complete, gf, buf, len);
|
||||
break;
|
||||
case GuestFDStatic:
|
||||
staticfile_read(cs, complete, gf, buf, len);
|
||||
break;
|
||||
case GuestFDConsole:
|
||||
console_read(cs, complete, gf, buf, len);
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_read(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
int fd, vaddr buf, uint64_t len)
|
||||
{
|
||||
GuestFD *gf = get_guestfd(fd);
|
||||
|
||||
if (gf) {
|
||||
semihost_sys_read_gf(cs, complete, gf, buf, len);
|
||||
} else {
|
||||
complete(cs, -1, EBADF);
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_write_gf(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
GuestFD *gf, vaddr buf, uint64_t len)
|
||||
{
|
||||
/*
|
||||
* Bound length for 64-bit guests on 32-bit hosts, not overflowing ssize_t.
|
||||
* Note the Linux kernel does this with MAX_RW_COUNT, so it's not a bad
|
||||
* idea to do this unconditionally.
|
||||
*/
|
||||
if (len > INT32_MAX) {
|
||||
len = INT32_MAX;
|
||||
}
|
||||
switch (gf->type) {
|
||||
case GuestFDGDB:
|
||||
gdb_write(cs, complete, gf, buf, len);
|
||||
break;
|
||||
case GuestFDHost:
|
||||
host_write(cs, complete, gf, buf, len);
|
||||
break;
|
||||
case GuestFDConsole:
|
||||
console_write(cs, complete, gf, buf, len);
|
||||
break;
|
||||
case GuestFDStatic:
|
||||
/* Static files are never open for writing: EBADF. */
|
||||
complete(cs, -1, EBADF);
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_write(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
int fd, vaddr buf, uint64_t len)
|
||||
{
|
||||
GuestFD *gf = get_guestfd(fd);
|
||||
|
||||
if (gf) {
|
||||
semihost_sys_write_gf(cs, complete, gf, buf, len);
|
||||
} else {
|
||||
complete(cs, -1, EBADF);
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_lseek(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
int fd, int64_t off, int gdb_whence)
|
||||
{
|
||||
GuestFD *gf = get_guestfd(fd);
|
||||
|
||||
if (!gf) {
|
||||
complete(cs, -1, EBADF);
|
||||
return;
|
||||
}
|
||||
switch (gf->type) {
|
||||
case GuestFDGDB:
|
||||
gdb_lseek(cs, complete, gf, off, gdb_whence);
|
||||
return;
|
||||
case GuestFDHost:
|
||||
host_lseek(cs, complete, gf, off, gdb_whence);
|
||||
break;
|
||||
case GuestFDStatic:
|
||||
staticfile_lseek(cs, complete, gf, off, gdb_whence);
|
||||
break;
|
||||
case GuestFDConsole:
|
||||
complete(cs, -1, ESPIPE);
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_isatty(CPUState *cs, gdb_syscall_complete_cb complete, int fd)
|
||||
{
|
||||
GuestFD *gf = get_guestfd(fd);
|
||||
|
||||
if (!gf) {
|
||||
complete(cs, 0, EBADF);
|
||||
return;
|
||||
}
|
||||
switch (gf->type) {
|
||||
case GuestFDGDB:
|
||||
gdb_isatty(cs, complete, gf);
|
||||
break;
|
||||
case GuestFDHost:
|
||||
host_isatty(cs, complete, gf);
|
||||
break;
|
||||
case GuestFDStatic:
|
||||
complete(cs, 0, ENOTTY);
|
||||
break;
|
||||
case GuestFDConsole:
|
||||
complete(cs, 1, 0);
|
||||
break;
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_flen(CPUState *cs, gdb_syscall_complete_cb fstat_cb,
|
||||
gdb_syscall_complete_cb flen_cb, int fd,
|
||||
vaddr fstat_addr)
|
||||
{
|
||||
GuestFD *gf = get_guestfd(fd);
|
||||
|
||||
if (!gf) {
|
||||
flen_cb(cs, -1, EBADF);
|
||||
return;
|
||||
}
|
||||
switch (gf->type) {
|
||||
case GuestFDGDB:
|
||||
gdb_fstat(cs, fstat_cb, gf, fstat_addr);
|
||||
break;
|
||||
case GuestFDHost:
|
||||
host_flen(cs, flen_cb, gf);
|
||||
break;
|
||||
case GuestFDStatic:
|
||||
staticfile_flen(cs, flen_cb, gf);
|
||||
break;
|
||||
case GuestFDConsole:
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_fstat(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
int fd, vaddr addr)
|
||||
{
|
||||
GuestFD *gf = get_guestfd(fd);
|
||||
|
||||
if (!gf) {
|
||||
complete(cs, -1, EBADF);
|
||||
return;
|
||||
}
|
||||
switch (gf->type) {
|
||||
case GuestFDGDB:
|
||||
gdb_fstat(cs, complete, gf, addr);
|
||||
break;
|
||||
case GuestFDHost:
|
||||
host_fstat(cs, complete, gf, addr);
|
||||
break;
|
||||
case GuestFDConsole:
|
||||
console_fstat(cs, complete, gf, addr);
|
||||
break;
|
||||
case GuestFDStatic:
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_stat(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr fname, uint64_t fname_len,
|
||||
vaddr addr)
|
||||
{
|
||||
if (use_gdb_syscalls()) {
|
||||
gdb_stat(cs, complete, fname, fname_len, addr);
|
||||
} else {
|
||||
host_stat(cs, complete, fname, fname_len, addr);
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_remove(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr fname, uint64_t fname_len)
|
||||
{
|
||||
if (use_gdb_syscalls()) {
|
||||
gdb_remove(cs, complete, fname, fname_len);
|
||||
} else {
|
||||
host_remove(cs, complete, fname, fname_len);
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_rename(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr oname, uint64_t oname_len,
|
||||
vaddr nname, uint64_t nname_len)
|
||||
{
|
||||
if (use_gdb_syscalls()) {
|
||||
gdb_rename(cs, complete, oname, oname_len, nname, nname_len);
|
||||
} else {
|
||||
host_rename(cs, complete, oname, oname_len, nname, nname_len);
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_system(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr cmd, uint64_t cmd_len)
|
||||
{
|
||||
if (use_gdb_syscalls()) {
|
||||
gdb_system(cs, complete, cmd, cmd_len);
|
||||
} else {
|
||||
host_system(cs, complete, cmd, cmd_len);
|
||||
}
|
||||
}
|
||||
|
||||
void semihost_sys_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
vaddr tv_addr, vaddr tz_addr)
|
||||
{
|
||||
if (use_gdb_syscalls()) {
|
||||
gdb_gettimeofday(cs, complete, tv_addr, tz_addr);
|
||||
} else {
|
||||
host_gettimeofday(cs, complete, tv_addr, tz_addr);
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef CONFIG_USER_ONLY
|
||||
void semihost_sys_poll_one(CPUState *cs, gdb_syscall_complete_cb complete,
|
||||
int fd, GIOCondition cond, int timeout)
|
||||
{
|
||||
GuestFD *gf = get_guestfd(fd);
|
||||
|
||||
if (!gf) {
|
||||
complete(cs, G_IO_NVAL, 1);
|
||||
return;
|
||||
}
|
||||
switch (gf->type) {
|
||||
case GuestFDGDB:
|
||||
complete(cs, G_IO_NVAL, 1);
|
||||
break;
|
||||
case GuestFDHost:
|
||||
host_poll_one(cs, complete, gf, cond, timeout);
|
||||
break;
|
||||
case GuestFDConsole:
|
||||
console_poll_one(cs, complete, gf, cond, timeout);
|
||||
break;
|
||||
case GuestFDStatic:
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Helper routines to provide target memory access for semihosting
|
||||
* syscalls in system emulation mode.
|
||||
*
|
||||
* Copyright (c) 2007 CodeSourcery.
|
||||
*
|
||||
* This code is licensed under the GPL
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "accel/tcg/cpu-mmu-index.h"
|
||||
#include "accel/tcg/probe.h"
|
||||
#include "exec/target_page.h"
|
||||
#include "exec/tlb-flags.h"
|
||||
#include "semihosting/uaccess.h"
|
||||
|
||||
void *uaccess_lock_user(CPUArchState *env, vaddr addr,
|
||||
size_t len, bool copy)
|
||||
{
|
||||
void *p = malloc(len);
|
||||
if (p && copy) {
|
||||
if (cpu_memory_rw_debug(env_cpu(env), addr, p, len, 0)) {
|
||||
free(p);
|
||||
p = NULL;
|
||||
}
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
ssize_t uaccess_strlen_user(CPUArchState *env, vaddr addr)
|
||||
{
|
||||
int mmu_idx = cpu_mmu_index(env_cpu(env), false);
|
||||
size_t len = 0;
|
||||
|
||||
while (1) {
|
||||
size_t left_in_page;
|
||||
int flags;
|
||||
void *h;
|
||||
|
||||
/* Find the number of bytes remaining in the page. */
|
||||
left_in_page = TARGET_PAGE_SIZE - (addr & ~TARGET_PAGE_MASK);
|
||||
|
||||
flags = probe_access_flags(env, addr, 0, MMU_DATA_LOAD,
|
||||
mmu_idx, true, &h, 0);
|
||||
if (flags & TLB_INVALID_MASK) {
|
||||
return -1;
|
||||
}
|
||||
if (flags & TLB_MMIO) {
|
||||
do {
|
||||
uint8_t c;
|
||||
if (cpu_memory_rw_debug(env_cpu(env), addr, &c, 1, 0)) {
|
||||
return -1;
|
||||
}
|
||||
if (c == 0) {
|
||||
return len;
|
||||
}
|
||||
addr++;
|
||||
len++;
|
||||
if (len > INT32_MAX) {
|
||||
return -1;
|
||||
}
|
||||
} while (--left_in_page != 0);
|
||||
} else {
|
||||
char *p = memchr(h, 0, left_in_page);
|
||||
if (p) {
|
||||
len += p - (char *)h;
|
||||
return len <= INT32_MAX ? (ssize_t)len : -1;
|
||||
}
|
||||
addr += left_in_page;
|
||||
len += left_in_page;
|
||||
if (len > INT32_MAX) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char *uaccess_lock_user_string(CPUArchState *env, vaddr addr)
|
||||
{
|
||||
ssize_t len = uaccess_strlen_user(env, addr);
|
||||
if (len < 0) {
|
||||
return NULL;
|
||||
}
|
||||
return uaccess_lock_user(env, addr, len + 1, true);
|
||||
}
|
||||
|
||||
void uaccess_unlock_user(CPUArchState *env, void *p,
|
||||
vaddr addr, size_t len)
|
||||
{
|
||||
if (len) {
|
||||
cpu_memory_rw_debug(env_cpu(env), addr, p, len, 1);
|
||||
}
|
||||
free(p);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Semihosting for user emulation
|
||||
*
|
||||
* Copyright (c) 2019 Linaro Ltd
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "semihosting/semihost.h"
|
||||
|
||||
bool semihosting_enabled(bool is_user)
|
||||
{
|
||||
assert(is_user);
|
||||
return true;
|
||||
}
|
||||
|
||||
SemihostingTarget semihosting_get_target(void)
|
||||
{
|
||||
return SEMIHOSTING_TARGET_AUTO;
|
||||
}
|
||||
Reference in New Issue
Block a user