aboutsummaryrefslogtreecommitdiff
path: root/semihosting/syscalls.c
diff options
context:
space:
mode:
Diffstat (limited to 'semihosting/syscalls.c')
-rw-r--r--semihosting/syscalls.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/semihosting/syscalls.c b/semihosting/syscalls.c
index 9f9d19a59a..3648b9dd49 100644
--- a/semihosting/syscalls.c
+++ b/semihosting/syscalls.c
@@ -94,6 +94,12 @@ static void gdb_open(CPUState *cs, gdb_syscall_complete_cb complete,
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);
+}
+
/*
* Host semihosting syscall implementations.
*/
@@ -140,6 +146,23 @@ static void host_open(CPUState *cs, gdb_syscall_complete_cb complete,
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);
+ }
+}
+
/*
* Syscall entry points.
*/
@@ -154,3 +177,27 @@ void semihost_sys_open(CPUState *cs, gdb_syscall_complete_cb complete,
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:
+ complete(cs, 0, 0);
+ break;
+ default:
+ g_assert_not_reached();
+ }
+ dealloc_guestfd(fd);
+}