aboutsummaryrefslogtreecommitdiff
path: root/sim/common
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2015-06-15 19:22:38 +0545
committerMike Frysinger <vapier@gentoo.org>2015-06-17 13:19:51 -0400
commit7d5c6c43ca8a5dd5491f4a58e977ec5501386ee3 (patch)
treebc347f539bd5eb379bd6b8ce9adef4cdff2a9248 /sim/common
parent61a0c964e611eaf72489c3049ba206b2f91ea4a9 (diff)
downloadgdb-7d5c6c43ca8a5dd5491f4a58e977ec5501386ee3.zip
gdb-7d5c6c43ca8a5dd5491f4a58e977ec5501386ee3.tar.gz
gdb-7d5c6c43ca8a5dd5491f4a58e977ec5501386ee3.tar.bz2
sim: syscall: add common sim_syscall helpers
Many ports have the same sim syscall logic, so add some helpers to handle all the common details. The arches still have to deal with the unpacking and packing of the syscall arguments, but the rest of the sim<->callback glue is now shared.
Diffstat (limited to 'sim/common')
-rw-r--r--sim/common/ChangeLog7
-rw-r--r--sim/common/sim-syscall.c77
-rw-r--r--sim/common/sim-syscall.h15
-rw-r--r--sim/common/syscall.c2
4 files changed, 100 insertions, 1 deletions
diff --git a/sim/common/ChangeLog b/sim/common/ChangeLog
index 2c67e49..59d92f4 100644
--- a/sim/common/ChangeLog
+++ b/sim/common/ChangeLog
@@ -1,5 +1,12 @@
2015-06-17 Mike Frysinger <vapier@gentoo.org>
+ * sim-syscall.c: Include errno.h and targ-vals.h.
+ (sim_syscall_multi, sim_syscall): Define.
+ * sim-syscall.h (sim_syscall_multi, sim_syscall): Declare.
+ * syscall.c (cb_syscall): Extend comment.
+
+2015-06-17 Mike Frysinger <vapier@gentoo.org>
+
* Make-common.in (SIM_NEW_COMMON_OBJS): Add sim-syscall.o.
* sim-syscall.c: New file.
* sim-syscall.h: New file.
diff --git a/sim/common/sim-syscall.c b/sim/common/sim-syscall.c
index 76812d3..6f4c8a3 100644
--- a/sim/common/sim-syscall.c
+++ b/sim/common/sim-syscall.c
@@ -19,8 +19,11 @@
#include "config.h"
+#include <errno.h>
+
#include "sim-main.h"
#include "sim-syscall.h"
+#include "targ-vals.h"
/* Read/write functions for system call interface. */
@@ -47,3 +50,77 @@ sim_syscall_write_mem (host_callback *cb ATTRIBUTE_UNUSED, struct cb_syscall *sc
return sim_core_write_buffer (sd, cpu, write_map, buf, taddr, bytes);
}
+
+/* Main syscall callback for simulators. */
+
+void
+sim_syscall_multi (SIM_CPU *cpu, int func, long arg1, long arg2, long arg3,
+ long arg4, long *result, long *result2, int *errcode)
+{
+ SIM_DESC sd = CPU_STATE (cpu);
+ host_callback *cb = STATE_CALLBACK (sd);
+ CB_SYSCALL sc;
+ char unknown_syscall[30];
+ const char *syscall;
+
+ CB_SYSCALL_INIT (&sc);
+
+ sc.func = func;
+ sc.arg1 = arg1;
+ sc.arg2 = arg2;
+ sc.arg3 = arg3;
+ sc.arg4 = arg4;
+
+ sc.p1 = (PTR) sd;
+ sc.p2 = (PTR) cpu;
+ sc.read_mem = sim_syscall_read_mem;
+ sc.write_mem = sim_syscall_write_mem;
+
+ if (cb_syscall (cb, &sc) != CB_RC_OK)
+ {
+ /* The cb_syscall func never returns an error, so this is more of a
+ sanity check. */
+ sim_engine_abort (sd, cpu, sim_pc_get (cpu), "cb_syscall failed");
+ }
+
+ syscall = cb_target_str_syscall (cb, func);
+ if (!syscall)
+ {
+ sprintf (unknown_syscall, "syscall_%i", func);
+ syscall = unknown_syscall;
+ }
+
+ if (sc.result == -1)
+ TRACE_SYSCALL (cpu, "%s[%i](%#lx, %#lx, %#lx) = %li (error = %s[%i])",
+ syscall, func, arg1, arg2, arg3, sc.result,
+ cb_target_str_errno (cb, sc.errcode), sc.errcode);
+ else
+ TRACE_SYSCALL (cpu, "%s[%i](%#lx, %#lx, %#lx) = %li",
+ syscall, func, arg1, arg2, arg3, sc.result);
+
+ if (cb_target_to_host_syscall (cb, func) == CB_SYS_exit)
+ sim_engine_halt (sd, cpu, NULL, sim_pc_get (cpu), sim_exited, arg1);
+ else if (sc.result == -1)
+ {
+ cb->last_errno = errno;
+ sc.errcode = cb->get_errno (cb);
+ }
+
+ *result = sc.result;
+ *result2 = sc.result2;
+ *errcode = sc.errcode;
+}
+
+long
+sim_syscall (SIM_CPU *cpu, int func, long arg1, long arg2, long arg3, long arg4)
+{
+ long result, result2;
+ int errcode;
+
+ sim_syscall_multi (cpu, func, arg1, arg2, arg3, arg4, &result, &result2,
+ &errcode);
+ if (result == -1)
+ return -errcode;
+ else
+ return result;
+}
diff --git a/sim/common/sim-syscall.h b/sim/common/sim-syscall.h
index 3f231fe..7e74661 100644
--- a/sim/common/sim-syscall.h
+++ b/sim/common/sim-syscall.h
@@ -20,6 +20,21 @@
#ifndef SIM_SYSCALL_H
#define SIM_SYSCALL_H
+/* Perform a syscall on the behalf of the target program. The error/result are
+ normalized into a single value (like a lot of operating systems do). If you
+ want the split values, see the other function below.
+
+ Note: While cb_syscall requires you handle the exit syscall yourself, that is
+ not the case with these helpers.
+
+ Note: Types here match the gdb callback interface. */
+long sim_syscall (SIM_CPU *, int func, long arg1, long arg2, long arg3,
+ long arg4);
+
+/* Same as sim_syscall, but return the split values by referenced. */
+void sim_syscall_multi (SIM_CPU *, int func, long arg1, long arg2, long arg3,
+ long arg4, long *result, long *result2, int *errcode);
+
/* Simple memory callbacks for cb_syscall's read_mem/write_mem that assume
cb_syscall's p1 and p2 are set to the SIM_DESC and SIM_CPU respectively. */
int sim_syscall_read_mem (host_callback *, struct cb_syscall *, unsigned long,
diff --git a/sim/common/syscall.c b/sim/common/syscall.c
index 29b73c2..0c37428 100644
--- a/sim/common/syscall.c
+++ b/sim/common/syscall.c
@@ -240,7 +240,7 @@ cb_syscall (host_callback *cb, CB_SYSCALL *sc)
#endif /* wip */
case CB_SYS_exit :
- /* Caller must catch and handle. */
+ /* Caller must catch and handle; see sim_syscall as an example. */
break;
case CB_SYS_open :