[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 32/74] semihosting: Create semihost_sys_{stat,fstat}
From: |
Richard Henderson |
Subject: |
[PATCH v2 32/74] semihosting: Create semihost_sys_{stat,fstat} |
Date: |
Tue, 3 May 2022 12:48:01 -0700 |
These syscalls will be used by m68k and nios2 semihosting.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
include/semihosting/syscalls.h | 7 ++
semihosting/syscalls.c | 137 +++++++++++++++++++++++++++++++++
2 files changed, 144 insertions(+)
diff --git a/include/semihosting/syscalls.h b/include/semihosting/syscalls.h
index c9f9e66be1..ecc97751a9 100644
--- a/include/semihosting/syscalls.h
+++ b/include/semihosting/syscalls.h
@@ -49,6 +49,13 @@ 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);
diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c
index b6c395af22..7bc2640512 100644
--- a/semihosting/syscalls.c
+++ b/semihosting/syscalls.c
@@ -93,6 +93,52 @@ static int errno_for_gdb(void)
#undef E
}
+/*
+ * 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 -GDB_EOVERFLOW;
+ }
+
+ p = lock_user(VERIFY_WRITE, addr, sizeof(struct gdb_stat), 0);
+ if (!p) {
+ return -GDB_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.
*/
@@ -163,6 +209,19 @@ static void gdb_fstat(CPUState *cs,
gdb_syscall_complete_cb complete,
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)
{
@@ -351,6 +410,51 @@ static void host_flen(CPUState *cs,
gdb_syscall_complete_cb complete,
}
}
+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_for_gdb());
+ 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_for_gdb();
+ } 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)
{
@@ -659,6 +763,39 @@ void semihost_sys_flen(CPUState *cs,
gdb_syscall_complete_cb fstat_cb,
}
}
+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, GDB_EBADF);
+ return;
+ }
+ switch (gf->type) {
+ case GuestFDGDB:
+ gdb_fstat(cs, complete, gf, addr);
+ break;
+ case GuestFDHost:
+ host_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)
{
--
2.34.1
- Re: [PATCH v2 18/74] semihosting: Split out common_semi_has_synccache, (continued)
- [PATCH v2 11/74] semihosting: Move common-semi.h to include/semihosting/, Richard Henderson, 2022/05/03
- [PATCH v2 26/74] semihosting: Split out semihost_sys_lseek, Richard Henderson, 2022/05/03
- [PATCH v2 31/74] semihosting: Split out semihost_sys_system, Richard Henderson, 2022/05/03
- [PATCH v2 24/74] semihosting: Split out semihost_sys_write, Richard Henderson, 2022/05/03
- [PATCH v2 25/74] semihosting: Bound length for semihost_sys_{read, write}, Richard Henderson, 2022/05/03
- [PATCH v2 29/74] semihosting: Split out semihost_sys_remove, Richard Henderson, 2022/05/03
- [PATCH v2 30/74] semihosting: Split out semihost_sys_rename, Richard Henderson, 2022/05/03
- [PATCH v2 28/74] semihosting: Split out semihost_sys_flen, Richard Henderson, 2022/05/03
- [PATCH v2 34/74] gdbstub: Widen gdb_syscall_complete_cb return value, Richard Henderson, 2022/05/03
- [PATCH v2 32/74] semihosting: Create semihost_sys_{stat,fstat},
Richard Henderson <=
- [PATCH v2 33/74] semihosting: Create semihost_sys_gettimeofday, Richard Henderson, 2022/05/03
- [PATCH v2 35/74] semihosting: Fix docs comment for qemu_semihosting_console_inc, Richard Henderson, 2022/05/03
- [PATCH v2 38/74] semihosting: Cleanup chardev init, Richard Henderson, 2022/05/03
- [PATCH v2 37/74] semihosting: Expand qemu_semihosting_console_inc to read, Richard Henderson, 2022/05/03
- [PATCH v2 36/74] semihosting: Pass CPUState to qemu_semihosting_console_inc, Richard Henderson, 2022/05/03
- [PATCH v2 39/74] semihosting: Create qemu_semihosting_console_write, Richard Henderson, 2022/05/03
- [PATCH v2 40/74] semihosting: Add GuestFDConsole, Richard Henderson, 2022/05/03
- [PATCH v2 41/74] semihosting: Create qemu_semihosting_guestfd_init, Richard Henderson, 2022/05/03
- [PATCH v2 42/74] semihosting: Use console_in_gf for SYS_READC, Richard Henderson, 2022/05/03
- [PATCH v2 46/74] semihosting: Remove qemu_semihosting_console_outs, Richard Henderson, 2022/05/03