public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/3] sim: callback: add a kill interface
@ 2021-06-22 23:41 Mike Frysinger
  2021-06-22 23:41 ` [PATCH 2/3] sim: cris: override getpid callback Mike Frysinger
  2021-06-22 23:41 ` [PATCH 3/3] sim: syscall: handle killing the sim itself Mike Frysinger
  0 siblings, 2 replies; 3+ messages in thread
From: Mike Frysinger @ 2021-06-22 23:41 UTC (permalink / raw)
  To: gdb-patches

This will make it easier to emulate the syscall.  If the kill target
is the sim itself, don't do anything.  This forces the higher layers
to make a decision as to how to handle this event: like halting the
overall engine process.
---
 include/sim/callback.h |  1 +
 sim/common/callback.c  | 11 +++++++++++
 sim/common/syscall.c   | 18 ++++++++++++++++++
 3 files changed, 30 insertions(+)

diff --git a/include/sim/callback.h b/include/sim/callback.h
index a6c536b1be1c..8d61ebb879e8 100644
--- a/include/sim/callback.h
+++ b/include/sim/callback.h
@@ -92,6 +92,7 @@ struct host_callback_struct
   int (*ftruncate) (host_callback *, int, int64_t);
   int (*truncate) (host_callback *, const char *, int64_t);
   int (*getpid) (host_callback *);
+  int (*kill) (host_callback *, int, int);
   int (*pipe) (host_callback *, int *);
 
   /* Called by the framework when a read call has emptied a pipe buffer.  */
diff --git a/sim/common/callback.c b/sim/common/callback.c
index 06d76b477245..c0ace6e4c8e5 100644
--- a/sim/common/callback.c
+++ b/sim/common/callback.c
@@ -569,6 +569,16 @@ os_getpid (host_callback *p)
   return result;
 }
 
+static int
+os_kill (host_callback *p, int pid, int signum)
+{
+  int result;
+
+  result = kill (pid, signum);
+  p->last_errno = errno;
+  return result;
+}
+
 static int
 os_pipe (host_callback *p, int *filedes)
 {
@@ -752,6 +762,7 @@ host_callback default_callback =
   os_truncate,
 
   os_getpid,
+  os_kill,
 
   os_pipe,
   os_pipe_empty,
diff --git a/sim/common/syscall.c b/sim/common/syscall.c
index 7ef34b95e9cf..6efddcfecde8 100644
--- a/sim/common/syscall.c
+++ b/sim/common/syscall.c
@@ -583,6 +583,24 @@ cb_syscall (host_callback *cb, CB_SYSCALL *sc)
       result = (*cb->getpid) (cb);
       break;
 
+    case CB_SYS_kill:
+      /* If killing self, leave it to the caller to process so it can send the
+	 signal to the engine.  */
+      if (sc->arg1 == (*cb->getpid) (cb))
+	{
+	  result = -1;
+	  errcode = ENOSYS;
+	}
+      else
+	{
+	  int signum = cb_target_to_host_signal (cb, sc->arg2);
+
+	  result = (*cb->kill) (cb, sc->arg1, signum);
+	  cb->last_errno = errno;
+	  goto ErrorFinish;
+	}
+      break;
+
     case CB_SYS_time :
       {
 	/* FIXME: May wish to change CB_SYS_time to something else.
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 2/3] sim: cris: override getpid callback
  2021-06-22 23:41 [PATCH 1/3] sim: callback: add a kill interface Mike Frysinger
@ 2021-06-22 23:41 ` Mike Frysinger
  2021-06-22 23:41 ` [PATCH 3/3] sim: syscall: handle killing the sim itself Mike Frysinger
  1 sibling, 0 replies; 3+ messages in thread
From: Mike Frysinger @ 2021-06-22 23:41 UTC (permalink / raw)
  To: gdb-patches

The cris linux syscall layers assume getpid returns a constant,
so add a custom function to provide that.
---
 sim/cris/traps.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/sim/cris/traps.c b/sim/cris/traps.c
index f92bb7cdebe2..fd7f9bc7ae29 100644
--- a/sim/cris/traps.c
+++ b/sim/cris/traps.c
@@ -3336,6 +3336,12 @@ cris_time (host_callback *cb ATTRIBUTE_UNUSED)
   return TARGET_TIME (current_cpu_for_cb_callback);
 }
 
+static int
+cris_getpid (host_callback *cb ATTRIBUTE_UNUSED)
+{
+  return TARGET_PID;
+}
+
 /* Set target-specific callback data. */
 
 void
@@ -3345,6 +3351,8 @@ cris_set_callbacks (host_callback *cb)
   cb->syscall_map = (CB_TARGET_DEFS_MAP *) syscall_map;
   cb->errno_map = (CB_TARGET_DEFS_MAP *) errno_map;
 
+  cb->getpid = cris_getpid;
+
   /* The kernel stat64 layout.  If we see a file > 2G, the "long"
      parameter to cb_store_target_endian will make st_size negative.
      Similarly for st_ino.  FIXME: Find a 64-bit type, and use it
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 3/3] sim: syscall: handle killing the sim itself
  2021-06-22 23:41 [PATCH 1/3] sim: callback: add a kill interface Mike Frysinger
  2021-06-22 23:41 ` [PATCH 2/3] sim: cris: override getpid callback Mike Frysinger
@ 2021-06-22 23:41 ` Mike Frysinger
  1 sibling, 0 replies; 3+ messages in thread
From: Mike Frysinger @ 2021-06-22 23:41 UTC (permalink / raw)
  To: gdb-patches

If code tries to send a signal to itself, the callback layer ignores
it and forces the caller to handle it.  This allows the sim to turn
that into an engine halt rather than actually killing the sim.
---
 sim/common/sim-syscall.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/sim/common/sim-syscall.c b/sim/common/sim-syscall.c
index be3ff8f82e2c..1bc646703f4a 100644
--- a/sim/common/sim-syscall.c
+++ b/sim/common/sim-syscall.c
@@ -97,8 +97,20 @@ sim_syscall_multi (SIM_CPU *cpu, int func, long arg1, long arg2, long arg3,
     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);
+  /* Handle syscalls that affect engine behavior.  */
+  switch (cb_target_to_host_syscall (cb, func))
+    {
+    case CB_SYS_exit:
+      sim_engine_halt (sd, cpu, NULL, sim_pc_get (cpu), sim_exited, arg1);
+      break;
+
+    case CB_SYS_kill:
+      /* TODO: Need to translate target syscall to sim syscall, but the sim
+	 doesn't yet have such a mapping layer.  */
+      if (arg1 == (*cb->getpid) (cb))
+	sim_engine_halt (sd, cpu, NULL, sim_pc_get (cpu), sim_signalled, arg2);
+      break;
+    }
 
   *result = sc.result;
   *result2 = sc.result2;
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-06-22 23:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-22 23:41 [PATCH 1/3] sim: callback: add a kill interface Mike Frysinger
2021-06-22 23:41 ` [PATCH 2/3] sim: cris: override getpid callback Mike Frysinger
2021-06-22 23:41 ` [PATCH 3/3] sim: syscall: handle killing the sim itself Mike Frysinger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).