]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
sim: syscall: add common sim_syscall helpers
authorMike Frysinger <vapier@gentoo.org>
Mon, 15 Jun 2015 13:37:38 +0000 (19:22 +0545)
committerMike Frysinger <vapier@gentoo.org>
Wed, 17 Jun 2015 17:19:51 +0000 (13:19 -0400)
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.

15 files changed:
sim/common/ChangeLog
sim/common/sim-syscall.c
sim/common/sim-syscall.h
sim/common/syscall.c
sim/lm32/ChangeLog
sim/lm32/traps.c
sim/m32r/ChangeLog
sim/m32r/traps-linux.c
sim/m32r/traps.c
sim/mcore/ChangeLog
sim/mcore/interp.c
sim/mn10300/ChangeLog
sim/mn10300/op_utils.c
sim/msp430/ChangeLog
sim/msp430/msp430-sim.c

index 2c67e49be937a697d4b35cf8635364f008ce116d..59d92f4ea1279c8b2be4589fcbe99927751a05db 100644 (file)
@@ -1,3 +1,10 @@
+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.
index 76812d358ea5cef9f5f1aa53494006a90b091f8c..6f4c8a3fc88039d6f237f67d29a43892c932d4dd 100644 (file)
 
 #include "config.h"
 
+#include <errno.h>
+
 #include "sim-main.h"
 #include "sim-syscall.h"
+#include "targ-vals.h"
 \f
 /* 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);
 }
+\f
+/* 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;
+}
index 3f231fe058fa5bcb47b0980635693d64a1e2371a..7e74661f0f0790fc989ad41393bacd9060a240bd 100644 (file)
 #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,
index 29b73c24a2d168215073d843b03858e19320c117..0c37428804884a1cc13204e13326b9b7b4d31155 100644 (file)
@@ -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 :
index 4cee5daa7e58434843ac879e81371b3052910a01..7442ba3010e66e8f108fdb184bac593207982cb4 100644 (file)
@@ -1,3 +1,8 @@
+2015-06-17  Mike Frysinger  <vapier@gentoo.org>
+
+       * traps.c (lm32bf_scall_insn): Replace call to cb_syscall with
+       sim_syscall_multi.
+
 2015-06-17  Mike Frysinger  <vapier@gentoo.org>
 
        * traps.c: Include sim-syscall.h.
index 810ddf7d47e534781c37dd7f2f41fca9a4089daf..9de09106b037f634f72eefdedbd9ed52f7434317 100644 (file)
@@ -132,26 +132,18 @@ lm32bf_scall_insn (SIM_CPU * current_cpu, IADDR pc)
       || (GET_H_GR (8) == TARGET_SYS_exit))
     {
       /* Delegate system call to host O/S.  */
-      CB_SYSCALL s;
-      CB_SYSCALL_INIT (&s);
-      s.p1 = (PTR) sd;
-      s.p2 = (PTR) current_cpu;
-      s.read_mem = sim_syscall_read_mem;
-      s.write_mem = sim_syscall_write_mem;
-      /* Extract parameters.  */
-      s.func = GET_H_GR (8);
-      s.arg1 = GET_H_GR (1);
-      s.arg2 = GET_H_GR (2);
-      s.arg3 = GET_H_GR (3);
-      /* Halt the simulator if the requested system call is _exit.  */
-      if (s.func == TARGET_SYS_exit)
-       sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, s.arg1);
+      long result, result2;
+      int errcode;
+
       /* Perform the system call.  */
-      cb_syscall (cb, &s);
+      sim_syscall_multi (current_cpu, GET_H_GR (8), GET_H_GR (1), GET_H_GR (2),
+                        GET_H_GR (3), GET_H_GR (4), &result, &result2,
+                        &errcode);
       /* Store the return value in the CPU's registers.  */
-      SET_H_GR (1, s.result);
-      SET_H_GR (2, s.result2);
-      SET_H_GR (3, s.errcode);
+      SET_H_GR (1, result);
+      SET_H_GR (2, result2);
+      SET_H_GR (3, errcode);
+
       /* Skip over scall instruction.  */
       return pc + 4;
     }
index b915747f6b4ad2905bb75b6687de318eeb454693..43aa13d0e7bb8efb77e3105fbdd754ebd1903099 100644 (file)
@@ -1,3 +1,9 @@
+2015-06-17  Mike Frysinger  <vapier@gentoo.org>
+
+       * traps.c (m32r_trap): Replace call to cb_syscall with
+       sim_syscall_multi.
+       * traps-linux.c (m32r_trap): Likewise.
+
 2015-06-17  Mike Frysinger  <vapier@gentoo.org>
 
        * traps-linux.c: Include sim-syscall.h.
index 4b6384e48d6f9ef93773e8581299e5298f18e713..8fee2a04b54a48d57e2b309ee2d6b5ac262b19a2 100644 (file)
@@ -213,28 +213,21 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
     {
     case TRAP_ELF_SYSCALL :
       {
-        CB_SYSCALL s;
-        CB_SYSCALL_INIT (&s);
-        s.func = m32rbf_h_gr_get (current_cpu, 0);
-        s.arg1 = m32rbf_h_gr_get (current_cpu, 1);
-        s.arg2 = m32rbf_h_gr_get (current_cpu, 2);
-        s.arg3 = m32rbf_h_gr_get (current_cpu, 3);
-        if (s.func == TARGET_SYS_exit)
-          {
-            sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, s.arg1);
-          }
-        s.p1 = (PTR) sd;
-        s.p2 = (PTR) current_cpu;
-        s.read_mem = sim_syscall_read_mem;
-        s.write_mem = sim_syscall_write_mem;
-        cb_syscall (cb, &s);
-        m32rbf_h_gr_set (current_cpu, 2, s.errcode);
-        m32rbf_h_gr_set (current_cpu, 0, s.result);
-        m32rbf_h_gr_set (current_cpu, 1, s.result2);
-        break;
+       long result, result2;
+       int errcode;
+
+       sim_syscall_multi (current_cpu,
+                          m32rbf_h_gr_get (current_cpu, 0),
+                          m32rbf_h_gr_get (current_cpu, 1),
+                          m32rbf_h_gr_get (current_cpu, 2),
+                          m32rbf_h_gr_get (current_cpu, 3),
+                          m32rbf_h_gr_get (current_cpu, 4),
+                          &result, &result2, &errcode);
+
+       m32rbf_h_gr_set (current_cpu, 2, errcode);
+       m32rbf_h_gr_set (current_cpu, 0, result);
+       m32rbf_h_gr_set (current_cpu, 1, result2);
+       break;
       }
 
     case TRAP_LINUX_SYSCALL :
index 869b5966431a7197e4df987ade6fe57cb30db6ef..225c37fb2fc0c8e340edc89bf2bfa476366c8d47 100644 (file)
@@ -129,27 +129,20 @@ m32r_trap (SIM_CPU *current_cpu, PCADDR pc, int num)
     {
     case TRAP_SYSCALL :
       {
-       CB_SYSCALL s;
-
-       CB_SYSCALL_INIT (&s);
-       s.func = m32rbf_h_gr_get (current_cpu, 0);
-       s.arg1 = m32rbf_h_gr_get (current_cpu, 1);
-       s.arg2 = m32rbf_h_gr_get (current_cpu, 2);
-       s.arg3 = m32rbf_h_gr_get (current_cpu, 3);
-
-       if (s.func == TARGET_SYS_exit)
-         {
-           sim_engine_halt (sd, current_cpu, NULL, pc, sim_exited, s.arg1);
-         }
-
-       s.p1 = (PTR) sd;
-       s.p2 = (PTR) current_cpu;
-       s.read_mem = sim_syscall_read_mem;
-       s.write_mem = sim_syscall_write_mem;
-       cb_syscall (cb, &s);
-       m32rbf_h_gr_set (current_cpu, 2, s.errcode);
-       m32rbf_h_gr_set (current_cpu, 0, s.result);
-       m32rbf_h_gr_set (current_cpu, 1, s.result2);
+       long result, result2;
+       int errcode;
+
+       sim_syscall_multi (current_cpu,
+                          m32rbf_h_gr_get (current_cpu, 0),
+                          m32rbf_h_gr_get (current_cpu, 1),
+                          m32rbf_h_gr_get (current_cpu, 2),
+                          m32rbf_h_gr_get (current_cpu, 3),
+                          m32rbf_h_gr_get (current_cpu, 4),
+                          &result, &result2, &errcode);
+
+       m32rbf_h_gr_set (current_cpu, 2, errcode);
+       m32rbf_h_gr_set (current_cpu, 0, result);
+       m32rbf_h_gr_set (current_cpu, 1, result2);
        break;
       }
 
index 389983d2605a7b5b889ea493feddc0900526d3e0..bc1598a4323fc7138899427e4eb4b958244d7b32 100644 (file)
@@ -1,3 +1,8 @@
+2015-06-17  Mike Frysinger  <vapier@gentoo.org>
+
+       * interp.c (handle_trap1): Replace call to cb_syscall with
+       sim_syscall.
+
 2015-06-17  Mike Frysinger  <vapier@gentoo.org>
 
        * interp.c: Include sim-syscall.h.
index e77535b8ae5d77d93f0f36a068c7f5a6eed9d481..64d148a10b03257fc7f0aad66ad9aa9523edfb80 100644 (file)
@@ -203,26 +203,10 @@ set_initial_gprs (SIM_CPU *scpu)
 static void
 handle_trap1 (SIM_DESC sd)
 {
-  host_callback *cb = STATE_CALLBACK (sd);
-  CB_SYSCALL sc;
-
-  CB_SYSCALL_INIT (&sc);
-
-  sc.func = cpu.gr[TRAPCODE];
-  sc.arg1 = cpu.gr[PARM1];
-  sc.arg2 = cpu.gr[PARM2];
-  sc.arg3 = cpu.gr[PARM3];
-  sc.arg4 = cpu.gr[PARM4];
-
-  sc.p1 = (PTR) sd;
-  sc.p2 = (PTR) STATE_CPU (sd, 0);
-  sc.read_mem = sim_syscall_read_mem;
-  sc.write_mem = sim_syscall_write_mem;
-
-  cb_syscall (cb, &sc);
-
   /* XXX: We don't pass back the actual errno value.  */
-  cpu.gr[RET1] = sc.result;
+  cpu.gr[RET1] = sim_syscall (STATE_CPU (sd, 0), cpu.gr[TRAPCODE],
+                             cpu.gr[PARM1], cpu.gr[PARM2], cpu.gr[PARM3],
+                             cpu.gr[PARM4]);
 }
 
 static void
index 1965d5fd365f1d9612cfa84e064ca28dad7aa374..ce1d326460cd38998a4414a1072fe0a249c18dc9 100644 (file)
@@ -1,3 +1,8 @@
+2015-06-17  Mike Frysinger  <vapier@gentoo.org>
+
+       * op_utils.c (do_syscall): Replace call to cb_syscall with
+       sim_syscall_multi.
+
 2015-06-17  Mike Frysinger  <vapier@gentoo.org>
 
        * mn10300_sim.h (syscall_read_mem, syscall_write_mem): Delete.
index 7b156f8b73a24e647487f751431baa6153cf860d..67bf9a34f68457d273785a308a0f1fd2ee5fa8d1 100644 (file)
@@ -145,6 +145,15 @@ genericBtst(unsigned32 leftOpnd, unsigned32 rightOpnd)
 INLINE_SIM_MAIN (void)
 do_syscall (void)
 {
+  /* Registers passed to trap 0.  */
+
+  /* Function number.  */
+  reg_t func = State.regs[0];
+  /* Parameters.  */
+  reg_t parm1 = State.regs[1];
+  reg_t parm2 = load_word (State.regs[REG_SP] + 12);
+  reg_t parm3 = load_word (State.regs[REG_SP] + 16);
+  reg_t parm4 = load_word (State.regs[REG_SP] + 20);
 
   /* We use this for simulated system calls; we may need to change
      it to a reserved instruction if we conflict with uses at
@@ -152,46 +161,24 @@ do_syscall (void)
   int save_errno = errno;      
   errno = 0;
 
-/* Registers passed to trap 0 */
-
-/* Function number.  */
-#define FUNC   (State.regs[0])
-
-/* Parameters.  */
-#define PARM1   (State.regs[1])
-#define PARM2   (load_word (State.regs[REG_SP] + 12))
-#define PARM3   (load_word (State.regs[REG_SP] + 16))
-
-/* Registers set by trap 0 */
-
-#define RETVAL State.regs[0]   /* return value */
-#define RETERR State.regs[1]   /* return error code */
-
-  if ( FUNC == TARGET_SYS_exit )
+  if (func == TARGET_SYS_exit)
     {
-      /* EXIT - caller can look in PARM1 to work out the reason */
+      /* EXIT - caller can look in parm1 to work out the reason */
       sim_engine_halt (simulator, STATE_CPU (simulator, 0), NULL, PC,
-                      (PARM1 == 0xdead ? SIM_SIGABRT : sim_exited), PARM1);
+                      (parm1 == 0xdead ? SIM_SIGABRT : sim_exited), parm1);
     }
   else
     {
-      CB_SYSCALL syscall;
-
-      CB_SYSCALL_INIT (&syscall);
-      syscall.arg1 = PARM1;
-      syscall.arg2 = PARM2;
-      syscall.arg3 = PARM3;
-      syscall.func = FUNC;
-      syscall.p1 = (PTR) simulator;
-      syscall.p2 = (PTR) STATE_CPU (simulator, 0);
-      syscall.read_mem = sim_syscall_read_mem;
-      syscall.write_mem = sim_syscall_write_mem;
-      cb_syscall (STATE_CALLBACK (simulator), &syscall);
-      RETERR = syscall.errcode;
-      RETVAL = syscall.result;
-    }
+      long result, result2;
+      int errcode;
 
+      sim_syscall_multi (STATE_CPU (simulator, 0), func, parm1, parm2,
+                        parm3, parm4, &result, &result2, &errcode);
+
+      /* Registers set by trap 0.  */
+      State.regs[0] = errcode;
+      State.regs[1] = result;
+    }
 
   errno = save_errno;
 }
-
index d26f3f401d218507a6f163b4482092ff7b96e422..146796cd4953341edd26049917967c3804a013e9 100644 (file)
@@ -1,3 +1,8 @@
+2015-06-17  Mike Frysinger  <vapier@gentoo.org>
+
+       * msp430-sim.c (maybe_perform_syscall): Replace call to cb_syscall
+       with sim_syscall.
+
 2015-06-17  Mike Frysinger  <vapier@gentoo.org>
 
        * msp430-sim.c: Include sim-syscall.h.
index 650584a666655220e8eb0ddf2a7c3bae2b843a88..931573e1afe99f226d48b5a35ed3599d1059a1dc 100644 (file)
@@ -1022,62 +1022,14 @@ maybe_perform_syscall (SIM_DESC sd, int call_addr)
     {
       /* Syscall!  */
       int syscall_num = call_addr & 0x3f;
-      host_callback *cb = STATE_CALLBACK (sd);
-      CB_SYSCALL sc;
-
-      CB_SYSCALL_INIT (&sc);
-
-      sc.func = syscall_num;
-      sc.arg1 = MSP430_CPU (sd)->state.regs[12];
-      sc.arg2 = MSP430_CPU (sd)->state.regs[13];
-      sc.arg3 = MSP430_CPU (sd)->state.regs[14];
-      sc.arg4 = MSP430_CPU (sd)->state.regs[15];
-
-      if (TRACE_SYSCALL_P (MSP430_CPU (sd)))
-       {
-         const char *syscall_name = "*unknown*";
-
-         switch (syscall_num)
-           {
-           case TARGET_SYS_exit:
-             syscall_name = "exit(%d)";
-             break;
-           case TARGET_SYS_open:
-             syscall_name = "open(%#x,%#x)";
-             break;
-           case TARGET_SYS_close:
-             syscall_name = "close(%d)";
-             break;
-           case TARGET_SYS_read:
-             syscall_name = "read(%d,%#x,%d)";
-             break;
-           case TARGET_SYS_write:
-             syscall_name = "write(%d,%#x,%d)";
-             break;
-           }
-         trace_generic (sd, MSP430_CPU (sd), TRACE_SYSCALL_IDX,
-                        syscall_name, sc.arg1, sc.arg2, sc.arg3, sc.arg4);
-       }
-
-      /* Handle SYS_exit here.  */
-      if (syscall_num == 1)
-       {
-         sim_engine_halt (sd, MSP430_CPU (sd), NULL,
-                          MSP430_CPU (sd)->state.regs[0],
-                          sim_exited, sc.arg1);
-         return 1;
-       }
-
-      sc.p1 = sd;
-      sc.p2 = MSP430_CPU (sd);
-      sc.read_mem = sim_syscall_read_mem;
-      sc.write_mem = sim_syscall_write_mem;
-
-      cb_syscall (cb, &sc);
-
-      TRACE_SYSCALL (MSP430_CPU (sd), "returns %ld", sc.result);
-
-      MSP430_CPU (sd)->state.regs[12] = sc.result;
+      int arg1 = MSP430_CPU (sd)->state.regs[12];
+      int arg2 = MSP430_CPU (sd)->state.regs[13];
+      int arg3 = MSP430_CPU (sd)->state.regs[14];
+      int arg4 = MSP430_CPU (sd)->state.regs[15];
+
+      MSP430_CPU (sd)->state.regs[12] = sim_syscall (MSP430_CPU (sd),
+                                                    syscall_num, arg1, arg2,
+                                                    arg3, arg4);
       return 1;
     }