[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 26/43] semihosting: Split out semihost_sys_isatty
From: |
Richard Henderson |
Subject: |
[PATCH 26/43] semihosting: Split out semihost_sys_isatty |
Date: |
Sat, 30 Apr 2022 06:29:15 -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/semihosting/guestfd.h | 3 +++
semihosting/arm-compat-semi.c | 27 +-----------------------
semihosting/guestfd.c | 39 +++++++++++++++++++++++++++++++++++
3 files changed, 43 insertions(+), 26 deletions(-)
diff --git a/include/semihosting/guestfd.h b/include/semihosting/guestfd.h
index 9c6967ac53..d3dd081e81 100644
--- a/include/semihosting/guestfd.h
+++ b/include/semihosting/guestfd.h
@@ -65,4 +65,7 @@ void semihost_sys_write(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_GUESTFD_H */
diff --git a/semihosting/arm-compat-semi.c b/semihosting/arm-compat-semi.c
index 0cab721a50..3844d0e376 100644
--- a/semihosting/arm-compat-semi.c
+++ b/semihosting/arm-compat-semi.c
@@ -290,14 +290,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;
@@ -309,11 +303,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",
@@ -337,32 +326,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,
},
};
@@ -487,12 +467,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/guestfd.c b/semihosting/guestfd.c
index e33d226534..6ed4a9162d 100644
--- a/semihosting/guestfd.c
+++ b/semihosting/guestfd.c
@@ -523,3 +523,42 @@ void semihost_sys_lseek(CPUState *cs,
gdb_syscall_complete_cb complete,
g_assert_not_reached();
}
}
+
+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 host_isatty(CPUState *cs, gdb_syscall_complete_cb complete,
+ GuestFD *gf)
+{
+ if (isatty(gf->hostfd)) {
+ complete(cs, 1, 0);
+ } else {
+ complete(cs, 0, errno);
+ }
+}
+
+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;
+ default:
+ g_assert_not_reached();
+ }
+}
--
2.34.1
- [PATCH 19/43] semihosting: Move GET_ARG/SET_ARG earlier in the file, (continued)
- [PATCH 19/43] semihosting: Move GET_ARG/SET_ARG earlier in the file, Richard Henderson, 2022/04/30
- [PATCH 24/43] semihosting: Bound length for semihost_sys_{read, write}, Richard Henderson, 2022/04/30
- [PATCH 33/43] gdbstub: Widen gdb_syscall_complete_cb return value, Richard Henderson, 2022/04/30
- [PATCH 35/43] target/m68k: Make semihosting system only, Richard Henderson, 2022/04/30
- [PATCH 39/43] target/m68k: Remove EXCP_HALT_INSN, Richard Henderson, 2022/04/30
- [PATCH 09/43] semihosting: Return void from do_common_semihosting, Richard Henderson, 2022/04/30
- [PATCH 15/43] semihosting: Split is_64bit_semihosting per target, Richard Henderson, 2022/04/30
- [PATCH 14/43] semihosting: Use struct gdb_stat in common_semi_flen_cb, Richard Henderson, 2022/04/30
- [PATCH 22/43] semihosting: Split out semihost_sys_read, Richard Henderson, 2022/04/30
- [PATCH 21/43] semihosting: Split out semihost_sys_close, Richard Henderson, 2022/04/30
- [PATCH 26/43] semihosting: Split out semihost_sys_isatty,
Richard Henderson <=
- [PATCH 27/43] semihosting: Split out semihost_sys_flen, Richard Henderson, 2022/04/30
- [PATCH 30/43] semihosting: Split out semihost_sys_system, Richard Henderson, 2022/04/30
- [PATCH 31/43] semihosting: Create semihost_sys_{stat,fstat}, Richard Henderson, 2022/04/30
- [PATCH 38/43] target/m68k: Enable semihosting for non-coldfire, Richard Henderson, 2022/04/30
- [PATCH 34/43] target/m68k: Eliminate m68k_semi_is_fseek, Richard Henderson, 2022/04/30
- [PATCH 16/43] semihosting: Split common_semi_flen_buf per target, Richard Henderson, 2022/04/30
- [PATCH 23/43] semihosting: Split out semihost_sys_write, Richard Henderson, 2022/04/30
- [PATCH 28/43] semihosting: Split out semihost_sys_remove, Richard Henderson, 2022/04/30
- [PATCH 29/43] semihosting: Split out semihost_sys_rename, Richard Henderson, 2022/04/30
- [PATCH 37/43] target/m68k: Do semihosting call as a normal helper, Richard Henderson, 2022/04/30