[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 29/49] semihosting: Split out semihost_sys_isatty
From: |
Richard Henderson |
Subject: |
[PATCH v3 29/49] semihosting: Split out semihost_sys_isatty |
Date: |
Fri, 20 May 2022 17:03:40 -0700 |
Split out the non-ARM specific portions of SYS_ISTTY to a
reusable function. This handles all GuestFD.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
include/exec/gdbstub.h | 1 +
include/semihosting/syscalls.h | 3 +++
semihosting/arm-compat-semi.c | 27 +------------------------
semihosting/syscalls.c | 36 ++++++++++++++++++++++++++++++++++
4 files changed, 41 insertions(+), 26 deletions(-)
diff --git a/include/exec/gdbstub.h b/include/exec/gdbstub.h
index 056db1a0d0..fd4bc4e937 100644
--- a/include/exec/gdbstub.h
+++ b/include/exec/gdbstub.h
@@ -41,6 +41,7 @@
#define GDB_EROFS 30
#define GDB_ENAMETOOLONG 91
#define GDB_EUNKNOWN 9999
+#define GDB_ENOTTY GDB_EUNKNOWN
/* For gdb file i/o remote protocol lseek whence. */
#define GDB_SEEK_SET 0
diff --git a/include/semihosting/syscalls.h b/include/semihosting/syscalls.h
index 841a93d25b..c60ebafb85 100644
--- a/include/semihosting/syscalls.h
+++ b/include/semihosting/syscalls.h
@@ -42,4 +42,7 @@ void semihost_sys_write_gf(CPUState *cs,
gdb_syscall_complete_cb complete,
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);
+
#endif /* SEMIHOSTING_SYSCALLS_H */
diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c
index e7e568a77e..973b0a68ab 100644
--- a/semihosting/arm-compat-semi.c
+++ b/semihosting/arm-compat-semi.c
@@ -291,14 +291,8 @@ common_semi_flen_cb(CPUState *cs, target_ulong ret,
target_ulong err)
* do the work and return the required return value to the guest
* via common_semi_cb.
*/
-typedef void sys_isattyfn(CPUState *cs, GuestFD *gf);
typedef void sys_flenfn(CPUState *cs, GuestFD *gf);
-static void host_isattyfn(CPUState *cs, GuestFD *gf)
-{
- common_semi_cb(cs, isatty(gf->hostfd), 0);
-}
-
static void host_flenfn(CPUState *cs, GuestFD *gf)
{
struct stat buf;
@@ -310,11 +304,6 @@ static void host_flenfn(CPUState *cs, GuestFD *gf)
}
}
-static void gdb_isattyfn(CPUState *cs, GuestFD *gf)
-{
- gdb_do_syscall(common_semi_cb, "isatty,%x", gf->hostfd);
-}
-
static void gdb_flenfn(CPUState *cs, GuestFD *gf)
{
gdb_do_syscall(common_semi_flen_cb, "fstat,%x,%x",
@@ -338,32 +327,23 @@ static const uint8_t featurefile_data[] = {
SH_EXT_EXIT_EXTENDED | SH_EXT_STDOUT_STDERR, /* Feature byte 0 */
};
-static void staticfile_isattyfn(CPUState *cs, GuestFD *gf)
-{
- common_semi_cb(cs, 0, 0);
-}
-
static void staticfile_flenfn(CPUState *cs, GuestFD *gf)
{
common_semi_cb(cs, gf->staticfile.len, 0);
}
typedef struct GuestFDFunctions {
- sys_isattyfn *isattyfn;
sys_flenfn *flenfn;
} GuestFDFunctions;
static const GuestFDFunctions guestfd_fns[] = {
[GuestFDHost] = {
- .isattyfn = host_isattyfn,
.flenfn = host_flenfn,
},
[GuestFDGDB] = {
- .isattyfn = gdb_isattyfn,
.flenfn = gdb_flenfn,
},
[GuestFDStatic] = {
- .isattyfn = staticfile_isattyfn,
.flenfn = staticfile_flenfn,
},
};
@@ -488,12 +468,7 @@ void do_common_semihosting(CPUState *cs)
case TARGET_SYS_ISTTY:
GET_ARG(0);
-
- gf = get_guestfd(arg0);
- if (!gf) {
- goto do_badf;
- }
- guestfd_fns[gf->type].isattyfn(cs, gf);
+ semihost_sys_isatty(cs, common_semi_cb, arg0);
break;
case TARGET_SYS_SEEK:
diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c
index db8891535b..702541b270 100644
--- a/semihosting/syscalls.c
+++ b/semihosting/syscalls.c
@@ -151,6 +151,12 @@ static void gdb_lseek(CPUState *cs,
gdb_syscall_complete_cb complete,
(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);
+}
+
/*
* Host semihosting syscall implementations.
*/
@@ -276,6 +282,13 @@ static void host_lseek(CPUState *cs,
gdb_syscall_complete_cb complete,
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_for_gdb());
+}
+
/*
* Static file semihosting syscall implementations.
*/
@@ -467,3 +480,26 @@ void semihost_sys_lseek(CPUState *cs,
gdb_syscall_complete_cb complete,
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, GDB_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, GDB_ENOTTY);
+ break;
+ default:
+ g_assert_not_reached();
+ }
+}
--
2.34.1
- [PATCH v3 38/49] semihosting: Pass CPUState to qemu_semihosting_console_inc, (continued)
- [PATCH v3 38/49] semihosting: Pass CPUState to qemu_semihosting_console_inc, Richard Henderson, 2022/05/20
- [PATCH v3 32/49] semihosting: Split out semihost_sys_rename, Richard Henderson, 2022/05/20
- [PATCH v3 22/49] semihosting: Move GET_ARG/SET_ARG earlier in the file, Richard Henderson, 2022/05/20
- [PATCH v3 30/49] semihosting: Split out semihost_sys_flen, Richard Henderson, 2022/05/20
- [PATCH v3 15/49] include/exec: Move gdb_stat and gdb_timeval to gdbstub.h, Richard Henderson, 2022/05/20
- [PATCH v3 37/49] semihosting: Fix docs comment for qemu_semihosting_console_inc, Richard Henderson, 2022/05/20
- [PATCH v3 29/49] semihosting: Split out semihost_sys_isatty,
Richard Henderson <=
- [PATCH v3 31/49] semihosting: Split out semihost_sys_remove, Richard Henderson, 2022/05/20
- [PATCH v3 17/49] semihosting: Use struct gdb_stat in common_semi_flen_cb, Richard Henderson, 2022/05/20
- [PATCH v3 21/49] semihosting: Use env more often in do_common_semihosting, Richard Henderson, 2022/05/20
- [PATCH v3 28/49] semihosting: Split out semihost_sys_lseek, Richard Henderson, 2022/05/20
- [PATCH v3 34/49] semihosting: Create semihost_sys_{stat,fstat}, Richard Henderson, 2022/05/20
- [PATCH v3 35/49] semihosting: Create semihost_sys_gettimeofday, Richard Henderson, 2022/05/20
- [PATCH v3 14/49] include/exec: Move gdb open flags to gdbstub.h, Richard Henderson, 2022/05/20