Semihosting syscall reorg:

* Split out semihosting/syscalls.c with common implementations.
   * Reorg arm-compat-semi.c to use syscalls.c.
   * Minor prep cleanups to m68k, mips, nios2.
 -----BEGIN PGP SIGNATURE-----
 
 iQFRBAABCgA7FiEEekgeeIaLTbaoWgXAZN846K9+IV8FAmK6iSodHHJpY2hhcmQu
 aGVuZGVyc29uQGxpbmFyby5vcmcACgkQZN846K9+IV8SEwgAmmowW2oeFA9uCrwz
 gUJo17AJ+RmRF/zXHyu5CPswylvfwH0zJXAm5BV7P/pVdyaL36b8YcgSEf+EWLsf
 rLFHxCshTYEnZSk6yFtWk5bn5azfevHm9/ObPeS9XGL4seQqGy7C/FReoTQ7/zI0
 W3zUDd3bWah3fXw8XYgSzh/RCrC5E2gFFc1G1g+6SIVZ7pbgkre2rRk5WMmylCLd
 jf9pmyswrheaKumCoBxU/S4XDgxVpaf3khiIqdbo8A20MDGnK/SZUWsBwJLK3QB8
 SKKv8o1ovbnl/HykABaszCIkO/LIu6SX3LoK7pF2CujkgSuwEN3WW0DOml6+b3fU
 J7YeZg==
 =sTbM
 -----END PGP SIGNATURE-----

Merge tag 'pull-semi-20220628' of https://gitlab.com/rth7680/qemu into staging

Semihosting syscall reorg:
  * Split out semihosting/syscalls.c with common implementations.
  * Reorg arm-compat-semi.c to use syscalls.c.
  * Minor prep cleanups to m68k, mips, nios2.

# -----BEGIN PGP SIGNATURE-----
#
# iQFRBAABCgA7FiEEekgeeIaLTbaoWgXAZN846K9+IV8FAmK6iSodHHJpY2hhcmQu
# aGVuZGVyc29uQGxpbmFyby5vcmcACgkQZN846K9+IV8SEwgAmmowW2oeFA9uCrwz
# gUJo17AJ+RmRF/zXHyu5CPswylvfwH0zJXAm5BV7P/pVdyaL36b8YcgSEf+EWLsf
# rLFHxCshTYEnZSk6yFtWk5bn5azfevHm9/ObPeS9XGL4seQqGy7C/FReoTQ7/zI0
# W3zUDd3bWah3fXw8XYgSzh/RCrC5E2gFFc1G1g+6SIVZ7pbgkre2rRk5WMmylCLd
# jf9pmyswrheaKumCoBxU/S4XDgxVpaf3khiIqdbo8A20MDGnK/SZUWsBwJLK3QB8
# SKKv8o1ovbnl/HykABaszCIkO/LIu6SX3LoK7pF2CujkgSuwEN3WW0DOml6+b3fU
# J7YeZg==
# =sTbM
# -----END PGP SIGNATURE-----
# gpg: Signature made Tue 28 Jun 2022 10:22:58 AM +0530
# gpg:                using RSA key 7A481E78868B4DB6A85A05C064DF38E8AF7E215F
# gpg:                issuer "richard.henderson@linaro.org"
# gpg: Good signature from "Richard Henderson <richard.henderson@linaro.org>" [ultimate]

* tag 'pull-semi-20220628' of https://gitlab.com/rth7680/qemu: (60 commits)
  target/nios2: Move nios2-semi.c to nios2_softmmu_ss
  target/nios2: Eliminate nios2_semi_is_lseek
  target/mips: Drop pread and pwrite syscalls from semihosting
  target/mips: Add UHI errno values
  target/mips: Use an exception for semihosting
  target/m68k: Make semihosting system only
  target/m68k: Eliminate m68k_semi_is_fseek
  semihosting: Create semihost_sys_poll_one
  semihosting: Remove qemu_semihosting_console_outs
  semihosting: Use console_out_gf for SYS_WRITE0
  semihosting: Remove qemu_semihosting_console_outc
  semihosting: Use console_out_gf for SYS_WRITEC
  semihosting: Use console_in_gf for SYS_READC
  semihosting: Create qemu_semihosting_guestfd_init
  semihosting: Add GuestFDConsole
  semihosting: Create qemu_semihosting_console_write
  semihosting: Cleanup chardev init
  semihosting: Expand qemu_semihosting_console_inc to read
  semihosting: Pass CPUState to qemu_semihosting_console_inc
  semihosting: Fix docs comment for qemu_semihosting_console_inc
  ...

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2022-06-28 10:24:30 +05:30
commit ad4c7f529a
50 changed files with 2194 additions and 1321 deletions

View File

@ -21,6 +21,13 @@ void tlb_set_dirty(CPUState *cpu, target_ulong vaddr)
{
}
int probe_access_flags(CPUArchState *env, target_ulong addr,
MMUAccessType access_type, int mmu_idx,
bool nonfault, void **phost, uintptr_t retaddr)
{
g_assert_not_reached();
}
void *probe_access(CPUArchState *env, target_ulong addr, int size,
MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
{

View File

@ -2,4 +2,5 @@ TARGET_ARCH=aarch64
TARGET_BASE_ARCH=arm
TARGET_XML_FILES= gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml
TARGET_HAS_BFLT=y
CONFIG_SEMIHOSTING=y
CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y

View File

@ -3,4 +3,5 @@ TARGET_BASE_ARCH=arm
TARGET_BIG_ENDIAN=y
TARGET_XML_FILES= gdb-xml/aarch64-core.xml gdb-xml/aarch64-fpu.xml
TARGET_HAS_BFLT=y
CONFIG_SEMIHOSTING=y
CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y

View File

@ -3,4 +3,5 @@ TARGET_SYSTBL_ABI=common,oabi
TARGET_SYSTBL=syscall.tbl
TARGET_XML_FILES= gdb-xml/arm-core.xml gdb-xml/arm-vfp.xml gdb-xml/arm-vfp3.xml gdb-xml/arm-vfp-sysregs.xml gdb-xml/arm-neon.xml gdb-xml/arm-m-profile.xml gdb-xml/arm-m-profile-mve.xml
TARGET_HAS_BFLT=y
CONFIG_SEMIHOSTING=y
CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y

View File

@ -4,4 +4,5 @@ TARGET_SYSTBL=syscall.tbl
TARGET_BIG_ENDIAN=y
TARGET_XML_FILES= gdb-xml/arm-core.xml gdb-xml/arm-vfp.xml gdb-xml/arm-vfp3.xml gdb-xml/arm-vfp-sysregs.xml gdb-xml/arm-neon.xml gdb-xml/arm-m-profile.xml gdb-xml/arm-m-profile-mve.xml
TARGET_HAS_BFLT=y
CONFIG_SEMIHOSTING=y
CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y

View File

@ -2,4 +2,5 @@ TARGET_ARCH=riscv32
TARGET_BASE_ARCH=riscv
TARGET_ABI_DIR=riscv
TARGET_XML_FILES= gdb-xml/riscv-32bit-cpu.xml gdb-xml/riscv-32bit-fpu.xml gdb-xml/riscv-64bit-fpu.xml gdb-xml/riscv-32bit-virtual.xml
CONFIG_SEMIHOSTING=y
CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y

View File

@ -2,4 +2,5 @@ TARGET_ARCH=riscv64
TARGET_BASE_ARCH=riscv
TARGET_ABI_DIR=riscv
TARGET_XML_FILES= gdb-xml/riscv-64bit-cpu.xml gdb-xml/riscv-32bit-fpu.xml gdb-xml/riscv-64bit-fpu.xml gdb-xml/riscv-64bit-virtual.xml
CONFIG_SEMIHOSTING=y
CONFIG_ARM_COMPATIBLE_SEMIHOSTING=y

View File

@ -1878,14 +1878,46 @@ static void handle_read_all_regs(GArray *params, void *user_ctx)
static void handle_file_io(GArray *params, void *user_ctx)
{
if (params->len >= 1 && gdbserver_state.current_syscall_cb) {
target_ulong ret, err;
uint64_t ret;
int err;
ret = (target_ulong)get_param(params, 0)->val_ull;
ret = get_param(params, 0)->val_ull;
if (params->len >= 2) {
err = (target_ulong)get_param(params, 1)->val_ull;
err = get_param(params, 1)->val_ull;
} else {
err = 0;
}
/* Convert GDB error numbers back to host error numbers. */
#define E(X) case GDB_E##X: err = E##X; break
switch (err) {
case 0:
break;
E(PERM);
E(NOENT);
E(INTR);
E(BADF);
E(ACCES);
E(FAULT);
E(BUSY);
E(EXIST);
E(NODEV);
E(NOTDIR);
E(ISDIR);
E(INVAL);
E(NFILE);
E(MFILE);
E(FBIG);
E(NOSPC);
E(SPIPE);
E(ROFS);
E(NAMETOOLONG);
default:
err = EINVAL;
break;
}
#undef E
gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err);
gdbserver_state.current_syscall_cb = NULL;
}

View File

@ -10,11 +10,71 @@
#define GDB_WATCHPOINT_READ 3
#define GDB_WATCHPOINT_ACCESS 4
/* For gdb file i/o remote protocol open flags. */
#define GDB_O_RDONLY 0
#define GDB_O_WRONLY 1
#define GDB_O_RDWR 2
#define GDB_O_APPEND 8
#define GDB_O_CREAT 0x200
#define GDB_O_TRUNC 0x400
#define GDB_O_EXCL 0x800
/* For gdb file i/o remote protocol errno values */
#define GDB_EPERM 1
#define GDB_ENOENT 2
#define GDB_EINTR 4
#define GDB_EBADF 9
#define GDB_EACCES 13
#define GDB_EFAULT 14
#define GDB_EBUSY 16
#define GDB_EEXIST 17
#define GDB_ENODEV 19
#define GDB_ENOTDIR 20
#define GDB_EISDIR 21
#define GDB_EINVAL 22
#define GDB_ENFILE 23
#define GDB_EMFILE 24
#define GDB_EFBIG 27
#define GDB_ENOSPC 28
#define GDB_ESPIPE 29
#define GDB_EROFS 30
#define GDB_ENAMETOOLONG 91
#define GDB_EUNKNOWN 9999
/* For gdb file i/o remote protocol lseek whence. */
#define GDB_SEEK_SET 0
#define GDB_SEEK_CUR 1
#define GDB_SEEK_END 2
/* For gdb file i/o stat/fstat. */
typedef uint32_t gdb_mode_t;
typedef uint32_t gdb_time_t;
struct gdb_stat {
uint32_t gdb_st_dev; /* device */
uint32_t gdb_st_ino; /* inode */
gdb_mode_t gdb_st_mode; /* protection */
uint32_t gdb_st_nlink; /* number of hard links */
uint32_t gdb_st_uid; /* user ID of owner */
uint32_t gdb_st_gid; /* group ID of owner */
uint32_t gdb_st_rdev; /* device type (if inode device) */
uint64_t gdb_st_size; /* total size, in bytes */
uint64_t gdb_st_blksize; /* blocksize for filesystem I/O */
uint64_t gdb_st_blocks; /* number of blocks allocated */
gdb_time_t gdb_st_atime; /* time of last access */
gdb_time_t gdb_st_mtime; /* time of last modification */
gdb_time_t gdb_st_ctime; /* time of last change */
} QEMU_PACKED;
struct gdb_timeval {
gdb_time_t tv_sec; /* second */
uint64_t tv_usec; /* microsecond */
} QEMU_PACKED;
#ifdef NEED_CPU_H
#include "cpu.h"
typedef void (*gdb_syscall_complete_cb)(CPUState *cpu,
target_ulong ret, target_ulong err);
typedef void (*gdb_syscall_complete_cb)(CPUState *cpu, uint64_t ret, int err);
/**
* gdb_do_syscall:

View File

@ -1,101 +0,0 @@
/*
* 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
*/
#ifndef SOFTMMU_SEMI_H
#define SOFTMMU_SEMI_H
#include "cpu.h"
static inline uint64_t softmmu_tget64(CPUArchState *env, target_ulong addr)
{
uint64_t val;
cpu_memory_rw_debug(env_cpu(env), addr, (uint8_t *)&val, 8, 0);
return tswap64(val);
}
static inline uint32_t softmmu_tget32(CPUArchState *env, target_ulong addr)
{
uint32_t val;
cpu_memory_rw_debug(env_cpu(env), addr, (uint8_t *)&val, 4, 0);
return tswap32(val);
}
static inline uint32_t softmmu_tget8(CPUArchState *env, target_ulong addr)
{
uint8_t val;
cpu_memory_rw_debug(env_cpu(env), addr, &val, 1, 0);
return val;
}
#define get_user_u64(arg, p) ({ arg = softmmu_tget64(env, p); 0; })
#define get_user_u32(arg, p) ({ arg = softmmu_tget32(env, p) ; 0; })
#define get_user_u8(arg, p) ({ arg = softmmu_tget8(env, p) ; 0; })
#define get_user_ual(arg, p) get_user_u32(arg, p)
static inline void softmmu_tput64(CPUArchState *env,
target_ulong addr, uint64_t val)
{
val = tswap64(val);
cpu_memory_rw_debug(env_cpu(env), addr, (uint8_t *)&val, 8, 1);
}
static inline void softmmu_tput32(CPUArchState *env,
target_ulong addr, uint32_t val)
{
val = tswap32(val);
cpu_memory_rw_debug(env_cpu(env), addr, (uint8_t *)&val, 4, 1);
}
#define put_user_u64(arg, p) ({ softmmu_tput64(env, p, arg) ; 0; })
#define put_user_u32(arg, p) ({ softmmu_tput32(env, p, arg) ; 0; })
#define put_user_ual(arg, p) put_user_u32(arg, p)
static void *softmmu_lock_user(CPUArchState *env,
target_ulong addr, target_ulong len, int copy)
{
uint8_t *p;
/* TODO: Make this something that isn't fixed size. */
p = malloc(len);
if (p && copy) {
cpu_memory_rw_debug(env_cpu(env), addr, p, len, 0);
}
return p;
}
#define lock_user(type, p, len, copy) softmmu_lock_user(env, p, len, copy)
static char *softmmu_lock_user_string(CPUArchState *env, target_ulong addr)
{
char *p;
char *s;
uint8_t c;
/* TODO: Make this something that isn't fixed size. */
s = p = malloc(1024);
if (!s) {
return NULL;
}
do {
cpu_memory_rw_debug(env_cpu(env), addr, &c, 1, 0);
addr++;
*(p++) = c;
} while (c);
return s;
}
#define lock_user_string(p) softmmu_lock_user_string(env, p)
static void softmmu_unlock_user(CPUArchState *env, void *p, target_ulong addr,
target_ulong len)
{
if (len) {
cpu_memory_rw_debug(env_cpu(env), addr, p, len, 1);
}
free(p);
}
#define unlock_user(s, args, len) softmmu_unlock_user(env, s, args, len)
#endif

View File

@ -34,6 +34,6 @@
#ifndef COMMON_SEMI_H
#define COMMON_SEMI_H
target_ulong do_common_semihosting(CPUState *cs);
void do_common_semihosting(CPUState *cs);
#endif /* COMMON_SEMI_H */

View File

@ -12,46 +12,33 @@
#include "cpu.h"
/**
* qemu_semihosting_console_outs:
* @env: CPUArchState
* @s: host address of null terminated guest string
* qemu_semihosting_console_read:
* @cs: CPUState
* @buf: host buffer
* @len: buffer size
*
* Send a null terminated guest string to the debug console. This may
* be the remote gdb session if a softmmu guest is currently being
* debugged.
* Receive at least one character from debug console. As this call may
* block if no data is available we suspend the CPU and will re-execute the
* instruction when data is there. Therefore two conditions must be met:
*
* Returns: number of bytes written.
*/
int qemu_semihosting_console_outs(CPUArchState *env, target_ulong s);
/**
* qemu_semihosting_console_outc:
* @env: CPUArchState
* @s: host address of null terminated guest string
*
* Send single character from guest memory to the debug console. This
* may be the remote gdb session if a softmmu guest is currently being
* debugged.
*
* Returns: nothing
*/
void qemu_semihosting_console_outc(CPUArchState *env, target_ulong c);
/**
* qemu_semihosting_console_inc:
* @env: CPUArchState
*
* Receive single character from debug console. This may be the remote
* gdb session if a softmmu guest is currently being debugged. As this
* call may block if no data is available we suspend the CPU and will
* re-execute the instruction when data is there. Therefore two
* conditions must be met:
* - CPUState is synchronized before calling this function
* - pc is only updated once the character is successfully returned
*
* Returns: character read OR cpu_loop_exit!
* Returns: number of characters read, OR cpu_loop_exit!
*/
target_ulong qemu_semihosting_console_inc(CPUArchState *env);
int qemu_semihosting_console_read(CPUState *cs, void *buf, int len);
/**
* qemu_semihosting_console_write:
* @buf: host buffer
* @len: buffer size
*
* Write len bytes from buf to the debug console.
*
* Returns: number of bytes written -- this should only ever be short
* on some sort of i/o error.
*/
int qemu_semihosting_console_write(void *buf, int len);
/**
* qemu_semihosting_log_out:
@ -66,4 +53,20 @@ target_ulong qemu_semihosting_console_inc(CPUArchState *env);
*/
int qemu_semihosting_log_out(const char *s, int len);
/*
* qemu_semihosting_console_block_until_ready:
* @cs: CPUState
*
* If no data is available we suspend the CPU and will re-execute the
* instruction when data is available.
*/
void qemu_semihosting_console_block_until_ready(CPUState *cs);
/**
* qemu_semihosting_console_ready:
*
* Return true if characters are available for read; does not block.
*/
bool qemu_semihosting_console_ready(void);
#endif /* SEMIHOST_CONSOLE_H */

View File

@ -0,0 +1,91 @@
/*
* Hosted file support for semihosting syscalls.
*
* Copyright (c) 2005, 2007 CodeSourcery.
* Copyright (c) 2019 Linaro
* Copyright © 2020 by Keith Packard <keithp@keithp.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef SEMIHOSTING_GUESTFD_H
#define SEMIHOSTING_GUESTFD_H
typedef enum GuestFDType {
GuestFDUnused = 0,
GuestFDHost,
GuestFDGDB,
GuestFDStatic,
GuestFDConsole,
} GuestFDType;
/*
* Guest file descriptors are integer indexes into an array of
* these structures (we will dynamically resize as necessary).
*/
typedef struct GuestFD {
GuestFDType type;
union {
int hostfd;
struct {
const uint8_t *data;
size_t len;
size_t off;
} staticfile;
};
} GuestFD;
/*
* For ARM semihosting, we have a separate structure for routing
* data for the console which is outside the guest fd address space.
*/
extern GuestFD console_in_gf;
extern GuestFD console_out_gf;
/**
* alloc_guestfd:
*
* Allocate an unused GuestFD index. The associated guestfd index
* will still be GuestFDUnused until it is initialized.
*/
int alloc_guestfd(void);
/**
* dealloc_guestfd:
* @guestfd: GuestFD index
*
* Deallocate a GuestFD index. The associated GuestFD structure
* will be recycled for a subsequent allocation.
*/
void dealloc_guestfd(int guestfd);
/**
* get_guestfd:
* @guestfd: GuestFD index
*
* Return the GuestFD structure associated with an initialized @guestfd,
* or NULL if it has not been allocated, or hasn't been initialized.
*/
GuestFD *get_guestfd(int guestfd);
/**
* associate_guestfd:
* @guestfd: GuestFD index
* @hostfd: host file descriptor
*
* Initialize the GuestFD for @guestfd to GuestFDHost using @hostfd.
*/
void associate_guestfd(int guestfd, int hostfd);
/**
* staticfile_guestfd:
* @guestfd: GuestFD index
* @data: data to be read
* @len: length of @data
*
* Initialize the GuestFD for @guestfd to GuestFDStatic.
* The @len bytes at @data will be returned to the guest on reads.
*/
void staticfile_guestfd(int guestfd, const uint8_t *data, size_t len);
#endif /* SEMIHOSTING_GUESTFD_H */

View File

@ -51,14 +51,6 @@ static inline const char *semihosting_get_cmdline(void)
{
return NULL;
}
static inline Chardev *semihosting_get_chardev(void)
{
return NULL;
}
static inline void qemu_semihosting_console_init(void)
{
}
#else /* !CONFIG_USER_ONLY */
bool semihosting_enabled(void);
SemihostingTarget semihosting_get_target(void);
@ -66,12 +58,12 @@ const char *semihosting_get_arg(int i);
int semihosting_get_argc(void);
const char *semihosting_get_cmdline(void);
void semihosting_arg_fallback(const char *file, const char *cmd);
Chardev *semihosting_get_chardev(void);
/* for vl.c hooks */
void qemu_semihosting_enable(void);
int qemu_semihosting_config_options(const char *opt);
void qemu_semihosting_connect_chardevs(void);
void qemu_semihosting_console_init(void);
void qemu_semihosting_chardev_init(void);
void qemu_semihosting_console_init(Chardev *);
#endif /* CONFIG_USER_ONLY */
void qemu_semihosting_guestfd_init(void);
#endif /* SEMIHOST_H */

View File

@ -0,0 +1,59 @@
/*
* 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
*/
#ifndef SEMIHOSTING_SOFTMMU_UACCESS_H
#define SEMIHOSTING_SOFTMMU_UACCESS_H
#include "cpu.h"
#define get_user_u64(val, addr) \
({ uint64_t val_ = 0; \
int ret_ = cpu_memory_rw_debug(env_cpu(env), (addr), \
&val_, sizeof(val_), 0); \
(val) = tswap64(val_); ret_; })
#define get_user_u32(val, addr) \
({ uint32_t val_ = 0; \
int ret_ = cpu_memory_rw_debug(env_cpu(env), (addr), \
&val_, sizeof(val_), 0); \
(val) = tswap32(val_); ret_; })
#define get_user_u8(val, addr) \
({ uint8_t val_ = 0; \
int ret_ = cpu_memory_rw_debug(env_cpu(env), (addr), \
&val_, sizeof(val_), 0); \
(val) = val_; ret_; })
#define get_user_ual(arg, p) get_user_u32(arg, p)
#define put_user_u64(val, addr) \
({ uint64_t val_ = tswap64(val); \
cpu_memory_rw_debug(env_cpu(env), (addr), &val_, sizeof(val_), 1); })
#define put_user_u32(val, addr) \
({ uint32_t val_ = tswap32(val); \
cpu_memory_rw_debug(env_cpu(env), (addr), &val_, sizeof(val_), 1); })
#define put_user_ual(arg, p) put_user_u32(arg, p)
void *softmmu_lock_user(CPUArchState *env, target_ulong addr,
target_ulong len, bool copy);
#define lock_user(type, p, len, copy) softmmu_lock_user(env, p, len, copy)
char *softmmu_lock_user_string(CPUArchState *env, target_ulong addr);
#define lock_user_string(p) softmmu_lock_user_string(env, p)
void softmmu_unlock_user(CPUArchState *env, void *p,
target_ulong addr, target_ulong len);
#define unlock_user(s, args, len) softmmu_unlock_user(env, s, args, len)
ssize_t softmmu_strlen_user(CPUArchState *env, target_ulong addr);
#define target_strlen(p) softmmu_strlen_user(env, p)
#endif /* SEMIHOSTING_SOFTMMU_UACCESS_H */

View File

@ -0,0 +1,75 @@
/*
* Syscall implementations for semihosting.
*
* Copyright (c) 2022 Linaro
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef SEMIHOSTING_SYSCALLS_H
#define SEMIHOSTING_SYSCALLS_H
/*
* Argument loading from the guest is performed by the caller;
* results are returned via the 'complete' callback.
*
* String operands are in address/len pairs. The len argument may be 0
* (when the semihosting abi does not already provide the length),
* or non-zero (where it should include the terminating zero).
*/
typedef struct GuestFD GuestFD;
void semihost_sys_open(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len,
int gdb_flags, int mode);
void semihost_sys_close(CPUState *cs, gdb_syscall_complete_cb complete,
int fd);
void semihost_sys_read(CPUState *cs, gdb_syscall_complete_cb complete,
int fd, target_ulong buf, target_ulong len);
void semihost_sys_read_gf(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len);
void semihost_sys_write(CPUState *cs, gdb_syscall_complete_cb complete,
int fd, target_ulong buf, target_ulong len);
void semihost_sys_write_gf(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len);
void semihost_sys_lseek(CPUState *cs, gdb_syscall_complete_cb complete,
int fd, int64_t off, int gdb_whence);
void semihost_sys_isatty(CPUState *cs, gdb_syscall_complete_cb complete,
int fd);
void semihost_sys_flen(CPUState *cs, gdb_syscall_complete_cb fstat_cb,
gdb_syscall_complete_cb flen_cb,
int fd, target_ulong fstat_addr);
void semihost_sys_fstat(CPUState *cs, gdb_syscall_complete_cb complete,
int fd, target_ulong addr);
void semihost_sys_stat(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len,
target_ulong addr);
void semihost_sys_remove(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len);
void semihost_sys_rename(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong oname, target_ulong oname_len,
target_ulong nname, target_ulong nname_len);
void semihost_sys_system(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong cmd, target_ulong cmd_len);
void semihost_sys_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong tv_addr, target_ulong tz_addr);
void semihost_sys_poll_one(CPUState *cs, gdb_syscall_complete_cb complete,
int fd, GIOCondition cond, int timeout);
#endif /* SEMIHOSTING_SYSCALLS_H */

View File

@ -154,7 +154,7 @@ void cpu_loop(CPUARMState *env)
force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
break;
case EXCP_SEMIHOST:
env->xregs[0] = do_common_semihosting(cs);
do_common_semihosting(cs);
env->pc += 4;
break;
case EXCP_YIELD:

View File

@ -449,7 +449,7 @@ void cpu_loop(CPUARMState *env)
}
break;
case EXCP_SEMIHOST:
env->regs[0] = do_common_semihosting(cs);
do_common_semihosting(cs);
env->regs[15] += env->thumb ? 2 : 4;
break;
case EXCP_INTERRUPT:

View File

@ -36,11 +36,6 @@ void cpu_loop(CPUM68KState *env)
process_queued_cpu_work(cs);
switch(trapnr) {
case EXCP_HALT_INSN:
/* Semihosing syscall. */
env->pc += 4;
do_m68k_semihosting(env, env->dregs[0]);
break;
case EXCP_ILLEGAL:
case EXCP_LINEA:
case EXCP_LINEF:

View File

@ -54,6 +54,10 @@
#include "loader.h"
#include "user-mmap.h"
#ifdef CONFIG_SEMIHOSTING
#include "semihosting/semihost.h"
#endif
#ifndef AT_FLAGS_PRESERVE_ARGV0
#define AT_FLAGS_PRESERVE_ARGV0_BIT 0
#define AT_FLAGS_PRESERVE_ARGV0 (1 << AT_FLAGS_PRESERVE_ARGV0_BIT)
@ -906,6 +910,11 @@ int main(int argc, char **argv, char **envp)
}
gdb_handlesig(cpu, 0);
}
#ifdef CONFIG_SEMIHOSTING
qemu_semihosting_guestfd_init();
#endif
cpu_loop(env);
/* never exits */
return 0;

View File

@ -81,7 +81,7 @@ void cpu_loop(CPURISCVState *env)
force_sig_fault(TARGET_SIGTRAP, TARGET_TRAP_BRKPT, env->pc);
break;
case RISCV_EXCP_SEMIHOST:
env->gpr[xA0] = do_common_semihosting(cs);
do_common_semihosting(cs);
env->pc += 4;
break;
default:

View File

@ -16,39 +16,6 @@
#include "user-internals.h"
#include <termios.h>
int qemu_semihosting_console_outs(CPUArchState *env, target_ulong addr)
{
int len = target_strlen(addr);
void *s;
if (len < 0){
qemu_log_mask(LOG_GUEST_ERROR,
"%s: passed inaccessible address " TARGET_FMT_lx,
__func__, addr);
return 0;
}
s = lock_user(VERIFY_READ, addr, (long)(len + 1), 1);
g_assert(s); /* target_strlen has already verified this will work */
len = write(STDERR_FILENO, s, len);
unlock_user(s, addr, 0);
return len;
}
void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr)
{
char c;
if (get_user_u8(c, addr)) {
qemu_log_mask(LOG_GUEST_ERROR,
"%s: passed inaccessible address " TARGET_FMT_lx,
__func__, addr);
} else {
if (write(STDERR_FILENO, &c, 1) != 1) {
qemu_log_mask(LOG_UNIMP, "%s: unexpected write to stdout failure",
__func__);
}
}
}
/*
* For linux-user we can safely block. However as we want to return as
* soon as a character is read we need to tweak the termio to disable
@ -56,21 +23,28 @@ void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr)
* program is expecting more normal behaviour. This is slow but
* nothing using semihosting console reading is expecting to be fast.
*/
target_ulong qemu_semihosting_console_inc(CPUArchState *env)
int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
{
uint8_t c;
int ret;
struct termios old_tio, new_tio;
/* Disable line-buffering and echo */
tcgetattr(STDIN_FILENO, &old_tio);
new_tio = old_tio;
new_tio.c_lflag &= (~ICANON & ~ECHO);
new_tio.c_cc[VMIN] = 1;
new_tio.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);
c = getchar();
ret = fread(buf, 1, len, stdin);
/* restore config */
tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
return (target_ulong) c;
return ret;
}
int qemu_semihosting_console_write(void *buf, int len)
{
return fwrite(buf, 1, len, stderr);
}

File diff suppressed because it is too large Load Diff

View File

@ -51,7 +51,6 @@ QemuOptsList qemu_semihosting_config_opts = {
typedef struct SemihostingConfig {
bool enabled;
SemihostingTarget target;
Chardev *chardev;
char **argv;
int argc;
const char *cmdline; /* concatenated argv */
@ -122,11 +121,6 @@ void semihosting_arg_fallback(const char *file, const char *cmd)
}
}
Chardev *semihosting_get_chardev(void)
{
return semihosting.chardev;
}
void qemu_semihosting_enable(void)
{
semihosting.enabled = true;
@ -172,16 +166,19 @@ int qemu_semihosting_config_options(const char *optarg)
return 0;
}
void qemu_semihosting_connect_chardevs(void)
{
/* We had to defer this until chardevs were created */
void qemu_semihosting_chardev_init(void)
{
Chardev *chr = NULL;
if (semihost_chardev) {
Chardev *chr = qemu_chr_find(semihost_chardev);
chr = qemu_chr_find(semihost_chardev);
if (chr == NULL) {
error_report("semihosting chardev '%s' not found",
semihost_chardev);
exit(1);
}
semihosting.chardev = chr;
}
qemu_semihosting_console_init(chr);
}

View File

@ -27,89 +27,10 @@
#include "qapi/error.h"
#include "qemu/fifo8.h"
int qemu_semihosting_log_out(const char *s, int len)
{
Chardev *chardev = semihosting_get_chardev();
if (chardev) {
return qemu_chr_write_all(chardev, (uint8_t *) s, len);
} else {
return write(STDERR_FILENO, s, len);
}
}
/*
* A re-implementation of lock_user_string that we can use locally
* instead of relying on softmmu-semi. Hopefully we can deprecate that
* in time. Copy string until we find a 0 or address error.
*/
static GString *copy_user_string(CPUArchState *env, target_ulong addr)
{
CPUState *cpu = env_cpu(env);
GString *s = g_string_sized_new(128);
uint8_t c;
do {
if (cpu_memory_rw_debug(cpu, addr++, &c, 1, 0) == 0) {
if (c) {
s = g_string_append_c(s, c);
}
} else {
qemu_log_mask(LOG_GUEST_ERROR,
"%s: passed inaccessible address " TARGET_FMT_lx,
__func__, addr);
break;
}
} while (c!=0);
return s;
}
static void semihosting_cb(CPUState *cs, target_ulong ret, target_ulong err)
{
if (ret == (target_ulong) -1) {
qemu_log("%s: gdb console output failed ("TARGET_FMT_ld")",
__func__, err);
}
}
int qemu_semihosting_console_outs(CPUArchState *env, target_ulong addr)
{
GString *s = copy_user_string(env, addr);
int out = s->len;
if (use_gdb_syscalls()) {
gdb_do_syscall(semihosting_cb, "write,2,%x,%x", addr, s->len);
} else {
out = qemu_semihosting_log_out(s->str, s->len);
}
g_string_free(s, true);
return out;
}
void qemu_semihosting_console_outc(CPUArchState *env, target_ulong addr)
{
CPUState *cpu = env_cpu(env);
uint8_t c;
if (cpu_memory_rw_debug(cpu, addr, &c, 1, 0) == 0) {
if (use_gdb_syscalls()) {
gdb_do_syscall(semihosting_cb, "write,2,%x,%x", addr, 1);
} else {
qemu_semihosting_log_out((const char *) &c, 1);
}
} else {
qemu_log_mask(LOG_GUEST_ERROR,
"%s: passed inaccessible address " TARGET_FMT_lx,
__func__, addr);
}
}
#define FIFO_SIZE 1024
/* Access to this structure is protected by the BQL */
typedef struct SemihostingConsole {
CharBackend backend;
Chardev *chr;
GSList *sleeping_cpus;
bool got;
Fifo8 fifo;
@ -117,6 +38,17 @@ typedef struct SemihostingConsole {
static SemihostingConsole console;
int qemu_semihosting_log_out(const char *s, int len)
{
if (console.chr) {
return qemu_chr_write_all(console.chr, (uint8_t *) s, len);
} else {
return write(STDERR_FILENO, s, len);
}
}
#define FIFO_SIZE 1024
static int console_can_read(void *opaque)
{
SemihostingConsole *c = opaque;
@ -145,27 +77,58 @@ static void console_read(void *opaque, const uint8_t *buf, int size)
c->sleeping_cpus = NULL;
}
target_ulong qemu_semihosting_console_inc(CPUArchState *env)
bool qemu_semihosting_console_ready(void)
{
uint8_t ch;
SemihostingConsole *c = &console;
g_assert(qemu_mutex_iothread_locked());
g_assert(current_cpu);
return !fifo8_is_empty(&c->fifo);
}
void qemu_semihosting_console_block_until_ready(CPUState *cs)
{
SemihostingConsole *c = &console;
g_assert(qemu_mutex_iothread_locked());
/* Block if the fifo is completely empty. */
if (fifo8_is_empty(&c->fifo)) {
c->sleeping_cpus = g_slist_prepend(c->sleeping_cpus, current_cpu);
current_cpu->halted = 1;
current_cpu->exception_index = EXCP_HALTED;
cpu_loop_exit(current_cpu);
c->sleeping_cpus = g_slist_prepend(c->sleeping_cpus, cs);
cs->halted = 1;
cs->exception_index = EXCP_HALTED;
cpu_loop_exit(cs);
/* never returns */
}
ch = fifo8_pop(&c->fifo);
return (target_ulong) ch;
}
void qemu_semihosting_console_init(void)
int qemu_semihosting_console_read(CPUState *cs, void *buf, int len)
{
Chardev *chr = semihosting_get_chardev();
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) {
return qemu_chr_write_all(console.chr, (uint8_t *)buf, len);
} 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.backend, chr, &error_abort);
@ -175,4 +138,6 @@ void qemu_semihosting_console_init(void)
NULL, NULL, &console,
NULL, true);
}
qemu_semihosting_guestfd_init();
}

160
semihosting/guestfd.c Normal file
View File

@ -0,0 +1,160 @@
/*
* Hosted file support for semihosting syscalls.
*
* Copyright (c) 2005, 2007 CodeSourcery.
* Copyright (c) 2019 Linaro
* Copyright © 2020 by Keith Packard <keithp@keithp.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "exec/gdbstub.h"
#include "semihosting/semihost.h"
#include "semihosting/guestfd.h"
#ifdef CONFIG_USER_ONLY
#include "qemu.h"
#else
#include "semihosting/softmmu-uaccess.h"
#include CONFIG_DEVICES
#endif
static GArray *guestfd_array;
#ifdef CONFIG_ARM_COMPATIBLE_SEMIHOSTING
GuestFD console_in_gf;
GuestFD console_out_gf;
#endif
void qemu_semihosting_guestfd_init(void)
{
/* New entries zero-initialized, i.e. type GuestFDUnused */
guestfd_array = g_array_new(FALSE, TRUE, sizeof(GuestFD));
#ifdef CONFIG_ARM_COMPATIBLE_SEMIHOSTING
/* 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;
}
#else
/* Otherwise, 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);
#endif
}
/*
* 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);
}

View File

@ -1,6 +1,12 @@
specific_ss.add(when: 'CONFIG_SEMIHOSTING', if_true: files(
'guestfd.c',
'syscalls.c',
))
specific_ss.add(when: ['CONFIG_SEMIHOSTING', 'CONFIG_SOFTMMU'], if_true: files(
'config.c',
'console.c',
'uaccess.c',
))
specific_ss.add(when: ['CONFIG_ARM_COMPATIBLE_SEMIHOSTING'],

978
semihosting/syscalls.c Normal file
View File

@ -0,0 +1,978 @@
/*
* Syscall implementations for semihosting.
*
* Copyright (c) 2022 Linaro
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "qemu/osdep.h"
#include "exec/gdbstub.h"
#include "semihosting/guestfd.h"
#include "semihosting/syscalls.h"
#include "semihosting/console.h"
#ifdef CONFIG_USER_ONLY
#include "qemu.h"
#else
#include "semihosting/softmmu-uaccess.h"
#endif
/*
* Validate or compute the length of the string (including terminator).
*/
static int validate_strlen(CPUState *cs, target_ulong str, target_ulong tlen)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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,
target_ulong tstr, target_ulong tlen)
{
int ret = validate_strlen(cs, tstr, tlen);
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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, target_ulong addr,
const struct stat *s)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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,
target_ulong fname, target_ulong 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",
fname, len, (target_ulong)gdb_flags, (target_ulong)mode);
}
static void gdb_close(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf)
{
gdb_do_syscall(complete, "close,%x", (target_ulong)gf->hostfd);
}
static void gdb_read(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
gdb_do_syscall(complete, "read,%x,%x,%x",
(target_ulong)gf->hostfd, buf, len);
}
static void gdb_write(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
gdb_do_syscall(complete, "write,%x,%x,%x",
(target_ulong)gf->hostfd, buf, 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",
(target_ulong)gf->hostfd, off, (target_ulong)gdb_whence);
}
static void gdb_isatty(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf)
{
gdb_do_syscall(complete, "isatty,%x", (target_ulong)gf->hostfd);
}
static void gdb_fstat(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong addr)
{
gdb_do_syscall(complete, "fstat,%x,%x", (target_ulong)gf->hostfd, addr);
}
static void gdb_stat(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len,
target_ulong addr)
{
int len = validate_strlen(cs, fname, fname_len);
if (len < 0) {
complete(cs, -1, -len);
return;
}
gdb_do_syscall(complete, "stat,%s,%x", fname, len, addr);
}
static void gdb_remove(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len)
{
int len = validate_strlen(cs, fname, fname_len);
if (len < 0) {
complete(cs, -1, -len);
return;
}
gdb_do_syscall(complete, "unlink,%s", fname, len);
}
static void gdb_rename(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong oname, target_ulong oname_len,
target_ulong nname, target_ulong 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", oname, olen, nname, nlen);
}
static void gdb_system(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong cmd, target_ulong cmd_len)
{
int len = validate_strlen(cs, cmd, cmd_len);
if (len < 0) {
complete(cs, -1, -len);
return;
}
gdb_do_syscall(complete, "system,%s", cmd, len);
}
static void gdb_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong tv_addr, target_ulong tz_addr)
{
gdb_do_syscall(complete, "gettimeofday,%x,%x", tv_addr, tz_addr);
}
/*
* Host semihosting syscall implementations.
*/
static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len,
int gdb_flags, int mode)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
char *p;
int ret, host_flags;
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) {
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, target_ulong buf, target_ulong len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
void *ptr = lock_user(VERIFY_WRITE, buf, len, 0);
ssize_t ret;
if (!ptr) {
complete(cs, -1, EFAULT);
return;
}
do {
ret = read(gf->hostfd, ptr, len);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
complete(cs, -1, errno);
unlock_user(ptr, buf, 0);
} else {
complete(cs, ret, 0);
unlock_user(ptr, buf, ret);
}
}
static void host_write(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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);
complete(cs, ret, ret == -1 ? errno : 0);
unlock_user(ptr, buf, 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, target_ulong 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,
target_ulong fname, target_ulong fname_len,
target_ulong addr)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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;
}
}
complete(cs, ret, err);
unlock_user(name, fname, 0);
}
static void host_remove(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong fname, target_ulong fname_len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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);
complete(cs, ret, ret ? errno : 0);
unlock_user(p, fname, 0);
}
static void host_rename(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong oname, target_ulong oname_len,
target_ulong nname, target_ulong nname_len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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);
complete(cs, ret, ret ? errno : 0);
unlock_user(ostr, oname, 0);
unlock_user(nstr, nname, 0);
}
static void host_system(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong cmd, target_ulong cmd_len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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);
complete(cs, ret, ret == -1 ? errno : 0);
unlock_user(p, cmd, 0);
}
static void host_gettimeofday(CPUState *cs, gdb_syscall_complete_cb complete,
target_ulong tv_addr, target_ulong tz_addr)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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, target_ulong buf, target_ulong len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
target_ulong 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;
complete(cs, len, 0);
unlock_user(ptr, buf, len);
}
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, target_ulong buf, target_ulong len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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);
complete(cs, ret, 0);
unlock_user(ptr, buf, ret);
}
static void console_write(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong buf, target_ulong len)
{
CPUArchState *env G_GNUC_UNUSED = cs->env_ptr;
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);
complete(cs, ret ? ret : -1, ret ? 0 : EIO);
unlock_user(ptr, buf, ret);
}
static void console_fstat(CPUState *cs, gdb_syscall_complete_cb complete,
GuestFD *gf, target_ulong 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,
target_ulong fname, target_ulong 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, target_ulong buf, target_ulong len)
{
/*
* Bound length for 64-bit guests on 32-bit hosts, not overlowing 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, target_ulong buf, target_ulong 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, target_ulong buf, target_ulong len)
{
/*
* Bound length for 64-bit guests on 32-bit hosts, not overlowing 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, target_ulong buf, target_ulong 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,
target_ulong 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, target_ulong 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,
target_ulong fname, target_ulong fname_len,
target_ulong 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,
target_ulong fname, target_ulong 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,
target_ulong oname, target_ulong oname_len,
target_ulong nname, target_ulong 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,
target_ulong cmd, target_ulong 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,
target_ulong tv_addr, target_ulong 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

91
semihosting/uaccess.c Normal file
View File

@ -0,0 +1,91 @@
/*
* 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 "exec/exec-all.h"
#include "semihosting/softmmu-uaccess.h"
void *softmmu_lock_user(CPUArchState *env, target_ulong addr,
target_ulong 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 softmmu_strlen_user(CPUArchState *env, target_ulong addr)
{
int mmu_idx = cpu_mmu_index(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, 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 *softmmu_lock_user_string(CPUArchState *env, target_ulong addr)
{
ssize_t len = softmmu_strlen_user(env, addr);
if (len < 0) {
return NULL;
}
return softmmu_lock_user(env, addr, len + 1, true);
}
void softmmu_unlock_user(CPUArchState *env, void *p,
target_ulong addr, target_ulong len)
{
if (len) {
cpu_memory_rw_debug(env_cpu(env), addr, p, len, 1);
}
free(p);
}

View File

@ -1917,8 +1917,7 @@ static void qemu_create_late_backends(void)
exit(1);
/* now chardevs have been created we may have semihosting to connect */
qemu_semihosting_connect_chardevs();
qemu_semihosting_console_init();
qemu_semihosting_chardev_init();
}
static void qemu_resolve_machine_memdev(void)

View File

@ -65,10 +65,6 @@ void semihosting_arg_fallback(const char *file, const char *cmd)
{
}
void qemu_semihosting_connect_chardevs(void)
{
}
void qemu_semihosting_console_init(void)
void qemu_semihosting_chardev_init(void)
{
}

View File

@ -0,0 +1,62 @@
/*
* Target-specific parts of semihosting/arm-compat-semi.c.
*
* Copyright (c) 2005, 2007 CodeSourcery.
* Copyright (c) 2019, 2022 Linaro
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef TARGET_ARM_COMMON_SEMI_TARGET_H
#define TARGET_ARM_COMMON_SEMI_TARGET_H
#ifndef CONFIG_USER_ONLY
#include "hw/arm/boot.h"
#endif
static inline target_ulong common_semi_arg(CPUState *cs, int argno)
{
ARMCPU *cpu = ARM_CPU(cs);
CPUARMState *env = &cpu->env;
if (is_a64(env)) {
return env->xregs[argno];
} else {
return env->regs[argno];
}
}
static inline void common_semi_set_ret(CPUState *cs, target_ulong ret)
{
ARMCPU *cpu = ARM_CPU(cs);
CPUARMState *env = &cpu->env;
if (is_a64(env)) {
env->xregs[0] = ret;
} else {
env->regs[0] = ret;
}
}
static inline bool common_semi_sys_exit_extended(CPUState *cs, int nr)
{
return (nr == TARGET_SYS_EXIT_EXTENDED || is_a64(cs->env_ptr));
}
static inline bool is_64bit_semihosting(CPUArchState *env)
{
return is_a64(env);
}
static inline target_ulong common_semi_stack_bottom(CPUState *cs)
{
ARMCPU *cpu = ARM_CPU(cs);
CPUARMState *env = &cpu->env;
return is_a64(env) ? env->xregs[31] : env->regs[13];
}
static inline bool common_semi_has_synccache(CPUArchState *env)
{
/* Ok for A64, invalid for A32/T32 */
return is_a64(env);
}
#endif

View File

@ -10515,13 +10515,13 @@ static void handle_semihosting(CPUState *cs)
qemu_log_mask(CPU_LOG_INT,
"...handling as semihosting call 0x%" PRIx64 "\n",
env->xregs[0]);
env->xregs[0] = do_common_semihosting(cs);
do_common_semihosting(cs);
env->pc += 4;
} else {
qemu_log_mask(CPU_LOG_INT,
"...handling as semihosting call 0x%x\n",
env->regs[0]);
env->regs[0] = do_common_semihosting(cs);
do_common_semihosting(cs);
env->regs[15] += env->thumb ? 2 : 4;
}
}

View File

@ -2373,7 +2373,7 @@ void arm_v7m_cpu_do_interrupt(CPUState *cs)
"...handling as semihosting call 0x%x\n",
env->regs[0]);
#ifdef CONFIG_TCG
env->regs[0] = do_common_semihosting(cs);
do_common_semihosting(cs);
#else
g_assert_not_reached();
#endif

View File

@ -21,13 +21,8 @@
#include "cpu.h"
#include "exec/gdbstub.h"
#if defined(CONFIG_USER_ONLY)
#include "qemu.h"
#define SEMIHOSTING_HEAP_SIZE (128 * 1024 * 1024)
#else
#include "exec/softmmu-semi.h"
#include "semihosting/softmmu-uaccess.h"
#include "hw/boards.h"
#endif
#include "qemu/log.h"
#define HOSTED_EXIT 0
@ -45,38 +40,6 @@
#define HOSTED_ISATTY 12
#define HOSTED_SYSTEM 13
typedef uint32_t gdb_mode_t;
typedef uint32_t gdb_time_t;
struct m68k_gdb_stat {
uint32_t gdb_st_dev; /* device */
uint32_t gdb_st_ino; /* inode */
gdb_mode_t gdb_st_mode; /* protection */
uint32_t gdb_st_nlink; /* number of hard links */
uint32_t gdb_st_uid; /* user ID of owner */
uint32_t gdb_st_gid; /* group ID of owner */
uint32_t gdb_st_rdev; /* device type (if inode device) */
uint64_t gdb_st_size; /* total size, in bytes */
uint64_t gdb_st_blksize; /* blocksize for filesystem I/O */
uint64_t gdb_st_blocks; /* number of blocks allocated */
gdb_time_t gdb_st_atime; /* time of last access */
gdb_time_t gdb_st_mtime; /* time of last modification */
gdb_time_t gdb_st_ctime; /* time of last change */
} QEMU_PACKED;
struct gdb_timeval {
gdb_time_t tv_sec; /* second */
uint64_t tv_usec; /* microsecond */
} QEMU_PACKED;
#define GDB_O_RDONLY 0x0
#define GDB_O_WRONLY 0x1
#define GDB_O_RDWR 0x2
#define GDB_O_APPEND 0x8
#define GDB_O_CREAT 0x200
#define GDB_O_TRUNC 0x400
#define GDB_O_EXCL 0x800
static int translate_openflags(int flags)
{
int hf;
@ -98,11 +61,13 @@ static int translate_openflags(int flags)
static void translate_stat(CPUM68KState *env, target_ulong addr, struct stat *s)
{
struct m68k_gdb_stat *p;
struct gdb_stat *p;
if (!(p = lock_user(VERIFY_WRITE, addr, sizeof(struct m68k_gdb_stat), 0)))
p = lock_user(VERIFY_WRITE, addr, sizeof(struct gdb_stat), 0);
if (!p) {
/* FIXME - should this return an error code? */
return;
}
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);
@ -122,11 +87,14 @@ static void translate_stat(CPUM68KState *env, target_ulong addr, struct stat *s)
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 m68k_gdb_stat));
unlock_user(p, addr, sizeof(struct gdb_stat));
}
static void m68k_semi_return_u32(CPUM68KState *env, uint32_t ret, uint32_t err)
static void m68k_semi_u32_cb(CPUState *cs, uint64_t ret, int err)
{
M68kCPU *cpu = M68K_CPU(cs);
CPUM68KState *env = &cpu->env;
target_ulong args = env->dregs[1];
if (put_user_u32(ret, args) ||
put_user_u32(err, args + 4)) {
@ -140,8 +108,11 @@ static void m68k_semi_return_u32(CPUM68KState *env, uint32_t ret, uint32_t err)
}
}
static void m68k_semi_return_u64(CPUM68KState *env, uint64_t ret, uint32_t err)
static void m68k_semi_u64_cb(CPUState *cs, uint64_t ret, int err)
{
M68kCPU *cpu = M68K_CPU(cs);
CPUM68KState *env = &cpu->env;
target_ulong args = env->dregs[1];
if (put_user_u32(ret >> 32, args) ||
put_user_u32(ret, args + 4) ||
@ -152,25 +123,6 @@ static void m68k_semi_return_u64(CPUM68KState *env, uint64_t ret, uint32_t err)
}
}
static int m68k_semi_is_fseek;
static void m68k_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
{
M68kCPU *cpu = M68K_CPU(cs);
CPUM68KState *env = &cpu->env;
if (m68k_semi_is_fseek) {
/*
* FIXME: We've already lost the high bits of the fseek
* return value.
*/
m68k_semi_return_u64(env, ret, err);
m68k_semi_is_fseek = 0;
} else {
m68k_semi_return_u32(env, ret, err);
}
}
/*
* Read the input value from the argument block; fail the semihosting
* call if the memory read fails.
@ -185,6 +137,7 @@ static void m68k_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
void do_m68k_semihosting(CPUM68KState *env, int nr)
{
CPUState *cs = env_cpu(env);
uint32_t args;
target_ulong arg0, arg1, arg2, arg3;
void *p;
@ -203,7 +156,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(2);
GET_ARG(3);
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "open,%s,%x,%x", arg0, (int)arg1,
gdb_do_syscall(m68k_semi_u32_cb, "open,%s,%x,%x", arg0, (int)arg1,
arg2, arg3);
return;
} else {
@ -224,7 +177,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
int fd = arg0;
if (fd > 2) {
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "close,%x", arg0);
gdb_do_syscall(m68k_semi_u32_cb, "close,%x", arg0);
return;
} else {
result = close(fd);
@ -240,7 +193,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(2);
len = arg2;
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "read,%x,%x,%x",
gdb_do_syscall(m68k_semi_u32_cb, "read,%x,%x,%x",
arg0, arg1, len);
return;
} else {
@ -260,7 +213,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(2);
len = arg2;
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "write,%x,%x,%x",
gdb_do_syscall(m68k_semi_u32_cb, "write,%x,%x,%x",
arg0, arg1, len);
return;
} else {
@ -283,12 +236,11 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(3);
off = (uint32_t)arg2 | ((uint64_t)arg1 << 32);
if (use_gdb_syscalls()) {
m68k_semi_is_fseek = 1;
gdb_do_syscall(m68k_semi_cb, "fseek,%x,%lx,%x",
gdb_do_syscall(m68k_semi_u64_cb, "fseek,%x,%lx,%x",
arg0, off, arg3);
} else {
off = lseek(arg0, off, arg3);
m68k_semi_return_u64(env, off, errno);
m68k_semi_u64_cb(cs, off, errno);
}
return;
}
@ -298,7 +250,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(2);
GET_ARG(3);
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "rename,%s,%s",
gdb_do_syscall(m68k_semi_u32_cb, "rename,%s,%s",
arg0, (int)arg1, arg2, (int)arg3);
return;
} else {
@ -318,7 +270,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(0);
GET_ARG(1);
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "unlink,%s",
gdb_do_syscall(m68k_semi_u32_cb, "unlink,%s",
arg0, (int)arg1);
return;
} else {
@ -337,7 +289,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(1);
GET_ARG(2);
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "stat,%s,%x",
gdb_do_syscall(m68k_semi_u32_cb, "stat,%s,%x",
arg0, (int)arg1, arg2);
return;
} else {
@ -359,7 +311,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(0);
GET_ARG(1);
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "fstat,%x,%x",
gdb_do_syscall(m68k_semi_u32_cb, "fstat,%x,%x",
arg0, arg1);
return;
} else {
@ -374,7 +326,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(0);
GET_ARG(1);
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "gettimeofday,%x,%x",
gdb_do_syscall(m68k_semi_u32_cb, "gettimeofday,%x,%x",
arg0, arg1);
return;
} else {
@ -395,7 +347,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
case HOSTED_ISATTY:
GET_ARG(0);
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "isatty,%x", arg0);
gdb_do_syscall(m68k_semi_u32_cb, "isatty,%x", arg0);
return;
} else {
result = isatty(arg0);
@ -405,7 +357,7 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
GET_ARG(0);
GET_ARG(1);
if (use_gdb_syscalls()) {
gdb_do_syscall(m68k_semi_cb, "system,%s",
gdb_do_syscall(m68k_semi_u32_cb, "system,%s",
arg0, (int)arg1);
return;
} else {
@ -420,48 +372,17 @@ void do_m68k_semihosting(CPUM68KState *env, int nr)
}
break;
case HOSTED_INIT_SIM:
#if defined(CONFIG_USER_ONLY)
{
CPUState *cs = env_cpu(env);
TaskState *ts = cs->opaque;
/* Allocate the heap using sbrk. */
if (!ts->heap_limit) {
abi_ulong ret;
uint32_t size;
uint32_t base;
base = do_brk(0);
size = SEMIHOSTING_HEAP_SIZE;
/* Try a big heap, and reduce the size if that fails. */
for (;;) {
ret = do_brk(base + size);
if (ret >= (base + size)) {
break;
}
size >>= 1;
}
ts->heap_limit = base + size;
}
/*
* This call may happen before we have writable memory, so return
* values directly in registers.
*/
env->dregs[1] = ts->heap_limit;
env->aregs[7] = ts->stack_base;
}
#else
/*
* FIXME: This is wrong for boards where RAM does not start at
* address zero.
*/
env->dregs[1] = current_machine->ram_size;
env->aregs[7] = current_machine->ram_size;
#endif
return;
default:
cpu_abort(env_cpu(env), "Unsupported semihosting syscall %d\n", nr);
result = 0;
}
failed:
m68k_semi_return_u32(env, result, errno);
m68k_semi_u32_cb(cs, result, errno);
}

View File

@ -4,14 +4,16 @@ m68k_ss.add(files(
'fpu_helper.c',
'gdbstub.c',
'helper.c',
'm68k-semi.c',
'op_helper.c',
'softfloat.c',
'translate.c',
))
m68k_softmmu_ss = ss.source_set()
m68k_softmmu_ss.add(files('monitor.c'))
m68k_softmmu_ss.add(files(
'm68k-semi.c',
'monitor.c'
))
target_arch += {'m68k': m68k_ss}
target_softmmu_arch += {'m68k': m68k_softmmu_ss}

View File

@ -1252,8 +1252,9 @@ enum {
EXCP_MSAFPE,
EXCP_TLBXI,
EXCP_TLBRI,
EXCP_SEMIHOST,
EXCP_LAST = EXCP_TLBRI,
EXCP_LAST = EXCP_SEMIHOST,
};
/*

View File

@ -125,6 +125,7 @@ static const char * const excp_names[EXCP_LAST + 1] = {
[EXCP_TLBRI] = "TLB read-inhibit",
[EXCP_MSADIS] = "MSA disabled",
[EXCP_MSAFPE] = "MSA floating point",
[EXCP_SEMIHOST] = "Semihosting",
};
const char *mips_exception_name(int32_t exception)

View File

@ -826,7 +826,7 @@ static void gen_pool16c_insn(DisasContext *ctx)
break;
case SDBBP16:
if (is_uhi(extract32(ctx->opcode, 0, 4))) {
gen_helper_do_semihosting(cpu_env);
generate_exception_end(ctx, EXCP_SEMIHOST);
} else {
/*
* XXX: not clear which exception should be raised
@ -942,7 +942,7 @@ static void gen_pool16c_r6_insn(DisasContext *ctx)
case R6_SDBBP16:
/* SDBBP16 */
if (is_uhi(extract32(ctx->opcode, 6, 4))) {
gen_helper_do_semihosting(cpu_env);
generate_exception_end(ctx, EXCP_SEMIHOST);
} else {
if (ctx->hflags & MIPS_HFLAG_SBRI) {
generate_exception(ctx, EXCP_RI);
@ -1311,7 +1311,7 @@ static void gen_pool32axf(CPUMIPSState *env, DisasContext *ctx, int rt, int rs)
break;
case SDBBP:
if (is_uhi(extract32(ctx->opcode, 16, 10))) {
gen_helper_do_semihosting(cpu_env);
generate_exception_end(ctx, EXCP_SEMIHOST);
} else {
check_insn(ctx, ISA_MIPS_R1);
if (ctx->hflags & MIPS_HFLAG_SBRI) {

View File

@ -952,7 +952,7 @@ static int decode_ase_mips16e(CPUMIPSState *env, DisasContext *ctx)
break;
case RR_SDBBP:
if (is_uhi(extract32(ctx->opcode, 5, 6))) {
gen_helper_do_semihosting(cpu_env);
generate_exception_end(ctx, EXCP_SEMIHOST);
} else {
/*
* XXX: not clear which exception should be raised

View File

@ -3695,7 +3695,7 @@ static int decode_nanomips_32_48_opc(CPUMIPSState *env, DisasContext *ctx)
break;
case NM_SDBBP:
if (is_uhi(extract32(ctx->opcode, 0, 19))) {
gen_helper_do_semihosting(cpu_env);
generate_exception_end(ctx, EXCP_SEMIHOST);
} else {
if (ctx->hflags & MIPS_HFLAG_SBRI) {
gen_reserved_instruction(ctx);
@ -4634,7 +4634,7 @@ static int decode_isa_nanomips(CPUMIPSState *env, DisasContext *ctx)
break;
case NM_SDBBP16:
if (is_uhi(extract32(ctx->opcode, 0, 3))) {
gen_helper_do_semihosting(cpu_env);
generate_exception_end(ctx, EXCP_SEMIHOST);
} else {
if (ctx->hflags & MIPS_HFLAG_SBRI) {
gen_reserved_instruction(ctx);

View File

@ -20,10 +20,10 @@
#include "qemu/osdep.h"
#include "cpu.h"
#include "qemu/log.h"
#include "exec/helper-proto.h"
#include "exec/softmmu-semi.h"
#include "semihosting/softmmu-uaccess.h"
#include "semihosting/semihost.h"
#include "semihosting/console.h"
#include "internal.h"
typedef enum UHIOp {
UHI_exit = 1,
@ -74,6 +74,46 @@ enum UHIOpenFlags {
UHIOpen_EXCL = 0x800
};
enum UHIErrno {
UHI_EACCESS = 13,
UHI_EAGAIN = 11,
UHI_EBADF = 9,
UHI_EBADMSG = 77,
UHI_EBUSY = 16,
UHI_ECONNRESET = 104,
UHI_EEXIST = 17,
UHI_EFBIG = 27,
UHI_EINTR = 4,
UHI_EINVAL = 22,
UHI_EIO = 5,
UHI_EISDIR = 21,
UHI_ELOOP = 92,
UHI_EMFILE = 24,
UHI_EMLINK = 31,
UHI_ENAMETOOLONG = 91,
UHI_ENETDOWN = 115,
UHI_ENETUNREACH = 114,
UHI_ENFILE = 23,
UHI_ENOBUFS = 105,
UHI_ENOENT = 2,
UHI_ENOMEM = 12,
UHI_ENOSPC = 28,
UHI_ENOSR = 63,
UHI_ENOTCONN = 128,
UHI_ENOTDIR = 20,
UHI_ENXIO = 6,
UHI_EOVERFLOW = 139,
UHI_EPERM = 1,
UHI_EPIPE = 32,
UHI_ERANGE = 34,
UHI_EROFS = 30,
UHI_ESPIPE = 29,
UHI_ETIMEDOUT = 116,
UHI_ETXTBSY = 26,
UHI_EWOULDBLOCK = 11,
UHI_EXDEV = 18,
};
static int errno_mips(int host_errno)
{
/* Errno values taken from asm-mips/errno.h */
@ -142,8 +182,8 @@ static int get_open_flags(target_ulong target_flags)
return open_flags;
}
static int write_to_file(CPUMIPSState *env, target_ulong fd, target_ulong vaddr,
target_ulong len, target_ulong offset)
static int write_to_file(CPUMIPSState *env, target_ulong fd,
target_ulong vaddr, target_ulong len)
{
int num_of_bytes;
void *dst = lock_user(VERIFY_READ, vaddr, len, 1);
@ -152,23 +192,14 @@ static int write_to_file(CPUMIPSState *env, target_ulong fd, target_ulong vaddr,
return -1;
}
if (offset) {
#ifdef _WIN32
num_of_bytes = 0;
#else
num_of_bytes = pwrite(fd, dst, len, offset);
#endif
} else {
num_of_bytes = write(fd, dst, len);
}
unlock_user(dst, vaddr, 0);
return num_of_bytes;
}
static int read_from_file(CPUMIPSState *env, target_ulong fd,
target_ulong vaddr, target_ulong len,
target_ulong offset)
target_ulong vaddr, target_ulong len)
{
int num_of_bytes;
void *dst = lock_user(VERIFY_WRITE, vaddr, len, 0);
@ -177,15 +208,7 @@ static int read_from_file(CPUMIPSState *env, target_ulong fd,
return -1;
}
if (offset) {
#ifdef _WIN32
num_of_bytes = 0;
#else
num_of_bytes = pread(fd, dst, len, offset);
#endif
} else {
num_of_bytes = read(fd, dst, len);
}
unlock_user(dst, vaddr, len);
return num_of_bytes;
@ -238,7 +261,7 @@ static int copy_argn_to_target(CPUMIPSState *env, int arg_num,
unlock_user(p, gpr, 0); \
} while (0)
void helper_do_semihosting(CPUMIPSState *env)
void mips_semihosting(CPUMIPSState *env)
{
target_ulong *gpr = env->active_tc.gpr;
const UHIOp op = gpr[25];
@ -272,11 +295,11 @@ void helper_do_semihosting(CPUMIPSState *env)
gpr[3] = errno_mips(errno);
break;
case UHI_read:
gpr[2] = read_from_file(env, gpr[4], gpr[5], gpr[6], 0);
gpr[2] = read_from_file(env, gpr[4], gpr[5], gpr[6]);
gpr[3] = errno_mips(errno);
break;
case UHI_write:
gpr[2] = write_to_file(env, gpr[4], gpr[5], gpr[6], 0);
gpr[2] = write_to_file(env, gpr[4], gpr[5], gpr[6]);
gpr[3] = errno_mips(errno);
break;
case UHI_lseek:
@ -342,14 +365,6 @@ void helper_do_semihosting(CPUMIPSState *env)
FREE_TARGET_STRING(p, gpr[4]);
abort();
break;
case UHI_pread:
gpr[2] = read_from_file(env, gpr[4], gpr[5], gpr[6], gpr[7]);
gpr[3] = errno_mips(errno);
break;
case UHI_pwrite:
gpr[2] = write_to_file(env, gpr[4], gpr[5], gpr[6], gpr[7]);
gpr[3] = errno_mips(errno);
break;
#ifndef _WIN32
case UHI_link:
GET_TARGET_STRINGS_2(p, gpr[4], p2, gpr[5]);

View File

@ -1053,6 +1053,10 @@ void mips_cpu_do_interrupt(CPUState *cs)
}
offset = 0x180;
switch (cs->exception_index) {
case EXCP_SEMIHOST:
cs->exception_index = EXCP_NONE;
mips_semihosting(env);
return;
case EXCP_DSS:
env->CP0_Debug |= 1 << CP0DB_DSS;
/*

View File

@ -9,8 +9,6 @@
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
DEF_HELPER_1(do_semihosting, void, env)
/* CP0 helpers */
DEF_HELPER_1(mfc0_mvpcontrol, tl, env)
DEF_HELPER_1(mfc0_mvpconf0, tl, env)

View File

@ -62,6 +62,8 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
MMUAccessType access_type, int mmu_idx,
bool probe, uintptr_t retaddr);
void mips_semihosting(CPUMIPSState *env);
#endif /* !CONFIG_USER_ONLY */
#endif

View File

@ -12094,14 +12094,6 @@ static inline bool is_uhi(int sdbbp_code)
#endif
}
#ifdef CONFIG_USER_ONLY
/* The above should dead-code away any calls to this..*/
static inline void gen_helper_do_semihosting(void *env)
{
g_assert_not_reached();
}
#endif
void gen_ldxs(DisasContext *ctx, int base, int index, int rd)
{
TCGv t0 = tcg_temp_new();
@ -13910,7 +13902,7 @@ static void decode_opc_special_r6(CPUMIPSState *env, DisasContext *ctx)
break;
case R6_OPC_SDBBP:
if (is_uhi(extract32(ctx->opcode, 6, 20))) {
gen_helper_do_semihosting(cpu_env);
generate_exception_end(ctx, EXCP_SEMIHOST);
} else {
if (ctx->hflags & MIPS_HFLAG_SBRI) {
gen_reserved_instruction(ctx);
@ -14322,7 +14314,7 @@ static void decode_opc_special2_legacy(CPUMIPSState *env, DisasContext *ctx)
break;
case OPC_SDBBP:
if (is_uhi(extract32(ctx->opcode, 6, 20))) {
gen_helper_do_semihosting(cpu_env);
generate_exception_end(ctx, EXCP_SEMIHOST);
} else {
/*
* XXX: not clear which exception should be raised

View File

@ -1,7 +1,6 @@
nios2_ss = ss.source_set()
nios2_ss.add(files(
'cpu.c',
'nios2-semi.c',
'op_helper.c',
'translate.c',
))
@ -10,7 +9,8 @@ nios2_softmmu_ss = ss.source_set()
nios2_softmmu_ss.add(files(
'helper.c',
'monitor.c',
'mmu.c'
'mmu.c',
'nios2-semi.c',
))
target_arch += {'nios2': nios2_ss}

View File

@ -22,14 +22,9 @@
*/
#include "qemu/osdep.h"
#include "cpu.h"
#include "exec/gdbstub.h"
#if defined(CONFIG_USER_ONLY)
#include "qemu.h"
#else
#include "exec/softmmu-semi.h"
#endif
#include "semihosting/softmmu-uaccess.h"
#include "qemu/log.h"
#define HOSTED_EXIT 0
@ -47,38 +42,6 @@
#define HOSTED_ISATTY 12
#define HOSTED_SYSTEM 13
typedef uint32_t gdb_mode_t;
typedef uint32_t gdb_time_t;
struct nios2_gdb_stat {
uint32_t gdb_st_dev; /* device */
uint32_t gdb_st_ino; /* inode */
gdb_mode_t gdb_st_mode; /* protection */
uint32_t gdb_st_nlink; /* number of hard links */
uint32_t gdb_st_uid; /* user ID of owner */
uint32_t gdb_st_gid; /* group ID of owner */
uint32_t gdb_st_rdev; /* device type (if inode device) */
uint64_t gdb_st_size; /* total size, in bytes */
uint64_t gdb_st_blksize; /* blocksize for filesystem I/O */
uint64_t gdb_st_blocks; /* number of blocks allocated */
gdb_time_t gdb_st_atime; /* time of last access */
gdb_time_t gdb_st_mtime; /* time of last modification */
gdb_time_t gdb_st_ctime; /* time of last change */
} QEMU_PACKED;
struct gdb_timeval {
gdb_time_t tv_sec; /* second */
uint64_t tv_usec; /* microsecond */
} QEMU_PACKED;
#define GDB_O_RDONLY 0x0
#define GDB_O_WRONLY 0x1
#define GDB_O_RDWR 0x2
#define GDB_O_APPEND 0x8
#define GDB_O_CREAT 0x200
#define GDB_O_TRUNC 0x400
#define GDB_O_EXCL 0x800
static int translate_openflags(int flags)
{
int hf;
@ -110,9 +73,9 @@ static int translate_openflags(int flags)
static bool translate_stat(CPUNios2State *env, target_ulong addr,
struct stat *s)
{
struct nios2_gdb_stat *p;
struct gdb_stat *p;
p = lock_user(VERIFY_WRITE, addr, sizeof(struct nios2_gdb_stat), 0);
p = lock_user(VERIFY_WRITE, addr, sizeof(struct gdb_stat), 0);
if (!p) {
return false;
@ -136,14 +99,16 @@ static bool translate_stat(CPUNios2State *env, target_ulong addr,
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 nios2_gdb_stat));
unlock_user(p, addr, sizeof(struct gdb_stat));
return true;
}
static void nios2_semi_return_u32(CPUNios2State *env, uint32_t ret,
uint32_t err)
static void nios2_semi_u32_cb(CPUState *cs, uint64_t ret, int err)
{
Nios2CPU *cpu = NIOS2_CPU(cs);
CPUNios2State *env = &cpu->env;
target_ulong args = env->regs[R_ARG1];
if (put_user_u32(ret, args) ||
put_user_u32(err, args + 4)) {
/*
@ -156,10 +121,12 @@ static void nios2_semi_return_u32(CPUNios2State *env, uint32_t ret,
}
}
static void nios2_semi_return_u64(CPUNios2State *env, uint64_t ret,
uint32_t err)
static void nios2_semi_u64_cb(CPUState *cs, uint64_t ret, int err)
{
Nios2CPU *cpu = NIOS2_CPU(cs);
CPUNios2State *env = &cpu->env;
target_ulong args = env->regs[R_ARG1];
if (put_user_u32(ret >> 32, args) ||
put_user_u32(ret, args + 4) ||
put_user_u32(err, args + 8)) {
@ -169,25 +136,6 @@ static void nios2_semi_return_u64(CPUNios2State *env, uint64_t ret,
}
}
static int nios2_semi_is_lseek;
static void nios2_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
{
Nios2CPU *cpu = NIOS2_CPU(cs);
CPUNios2State *env = &cpu->env;
if (nios2_semi_is_lseek) {
/*
* FIXME: We've already lost the high bits of the lseek
* return value.
*/
nios2_semi_return_u64(env, ret, err);
nios2_semi_is_lseek = 0;
} else {
nios2_semi_return_u32(env, ret, err);
}
}
/*
* Read the input value from the argument block; fail the semihosting
* call if the memory read fails.
@ -202,6 +150,7 @@ static void nios2_semi_cb(CPUState *cs, target_ulong ret, target_ulong err)
void do_nios2_semihosting(CPUNios2State *env)
{
CPUState *cs = env_cpu(env);
int nr;
uint32_t args;
target_ulong arg0, arg1, arg2, arg3;
@ -222,7 +171,7 @@ void do_nios2_semihosting(CPUNios2State *env)
GET_ARG(2);
GET_ARG(3);
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "open,%s,%x,%x", arg0, (int)arg1,
gdb_do_syscall(nios2_semi_u32_cb, "open,%s,%x,%x", arg0, (int)arg1,
arg2, arg3);
return;
} else {
@ -243,7 +192,7 @@ void do_nios2_semihosting(CPUNios2State *env)
int fd = arg0;
if (fd > 2) {
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "close,%x", arg0);
gdb_do_syscall(nios2_semi_u32_cb, "close,%x", arg0);
return;
} else {
result = close(fd);
@ -259,7 +208,7 @@ void do_nios2_semihosting(CPUNios2State *env)
GET_ARG(2);
len = arg2;
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "read,%x,%x,%x",
gdb_do_syscall(nios2_semi_u32_cb, "read,%x,%x,%x",
arg0, arg1, len);
return;
} else {
@ -279,7 +228,7 @@ void do_nios2_semihosting(CPUNios2State *env)
GET_ARG(2);
len = arg2;
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "write,%x,%x,%x",
gdb_do_syscall(nios2_semi_u32_cb, "write,%x,%x,%x",
arg0, arg1, len);
return;
} else {
@ -302,12 +251,11 @@ void do_nios2_semihosting(CPUNios2State *env)
GET_ARG(3);
off = (uint32_t)arg2 | ((uint64_t)arg1 << 32);
if (use_gdb_syscalls()) {
nios2_semi_is_lseek = 1;
gdb_do_syscall(nios2_semi_cb, "lseek,%x,%lx,%x",
gdb_do_syscall(nios2_semi_u64_cb, "lseek,%x,%lx,%x",
arg0, off, arg3);
} else {
off = lseek(arg0, off, arg3);
nios2_semi_return_u64(env, off, errno);
nios2_semi_u64_cb(cs, off, errno);
}
return;
}
@ -317,7 +265,7 @@ void do_nios2_semihosting(CPUNios2State *env)
GET_ARG(2);
GET_ARG(3);
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "rename,%s,%s",
gdb_do_syscall(nios2_semi_u32_cb, "rename,%s,%s",
arg0, (int)arg1, arg2, (int)arg3);
return;
} else {
@ -337,7 +285,7 @@ void do_nios2_semihosting(CPUNios2State *env)
GET_ARG(0);
GET_ARG(1);
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "unlink,%s",
gdb_do_syscall(nios2_semi_u32_cb, "unlink,%s",
arg0, (int)arg1);
return;
} else {
@ -356,7 +304,7 @@ void do_nios2_semihosting(CPUNios2State *env)
GET_ARG(1);
GET_ARG(2);
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "stat,%s,%x",
gdb_do_syscall(nios2_semi_u32_cb, "stat,%s,%x",
arg0, (int)arg1, arg2);
return;
} else {
@ -379,7 +327,7 @@ void do_nios2_semihosting(CPUNios2State *env)
GET_ARG(0);
GET_ARG(1);
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "fstat,%x,%x",
gdb_do_syscall(nios2_semi_u32_cb, "fstat,%x,%x",
arg0, arg1);
return;
} else {
@ -395,7 +343,7 @@ void do_nios2_semihosting(CPUNios2State *env)
/* Only the tv parameter is used. tz is assumed NULL. */
GET_ARG(0);
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "gettimeofday,%x,%x",
gdb_do_syscall(nios2_semi_u32_cb, "gettimeofday,%x,%x",
arg0, 0);
return;
} else {
@ -416,7 +364,7 @@ void do_nios2_semihosting(CPUNios2State *env)
case HOSTED_ISATTY:
GET_ARG(0);
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "isatty,%x", arg0);
gdb_do_syscall(nios2_semi_u32_cb, "isatty,%x", arg0);
return;
} else {
result = isatty(arg0);
@ -426,7 +374,7 @@ void do_nios2_semihosting(CPUNios2State *env)
GET_ARG(0);
GET_ARG(1);
if (use_gdb_syscalls()) {
gdb_do_syscall(nios2_semi_cb, "system,%s",
gdb_do_syscall(nios2_semi_u32_cb, "system,%s",
arg0, (int)arg1);
return;
} else {
@ -446,5 +394,5 @@ void do_nios2_semihosting(CPUNios2State *env)
result = 0;
}
failed:
nios2_semi_return_u32(env, result, errno);
nios2_semi_u32_cb(cs, result, errno);
}

View File

@ -0,0 +1,50 @@
/*
* Target-specific parts of semihosting/arm-compat-semi.c.
*
* Copyright (c) 2005, 2007 CodeSourcery.
* Copyright (c) 2019, 2022 Linaro
* Copyright © 2020 by Keith Packard <keithp@keithp.com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef TARGET_RISCV_COMMON_SEMI_TARGET_H
#define TARGET_RISCV_COMMON_SEMI_TARGET_H
static inline target_ulong common_semi_arg(CPUState *cs, int argno)
{
RISCVCPU *cpu = RISCV_CPU(cs);
CPURISCVState *env = &cpu->env;
return env->gpr[xA0 + argno];
}
static inline void common_semi_set_ret(CPUState *cs, target_ulong ret)
{
RISCVCPU *cpu = RISCV_CPU(cs);
CPURISCVState *env = &cpu->env;
env->gpr[xA0] = ret;
}
static inline bool common_semi_sys_exit_extended(CPUState *cs, int nr)
{
return (nr == TARGET_SYS_EXIT_EXTENDED || sizeof(target_ulong) == 8);
}
static inline bool is_64bit_semihosting(CPUArchState *env)
{
return riscv_cpu_mxl(env) != MXL_RV32;
}
static inline target_ulong common_semi_stack_bottom(CPUState *cs)
{
RISCVCPU *cpu = RISCV_CPU(cs);
CPURISCVState *env = &cpu->env;
return env->gpr[xSP];
}
static inline bool common_semi_has_synccache(CPUArchState *env)
{
return true;
}
#endif

View File

@ -1347,7 +1347,7 @@ void riscv_cpu_do_interrupt(CPUState *cs)
if (cause == RISCV_EXCP_SEMIHOST) {
if (env->priv >= PRV_S) {
env->gpr[xA0] = do_common_semihosting(cs);
do_common_semihosting(cs);
env->pc += 4;
return;
}