public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat
@ 2021-03-28  9:32 Faraz Shahbazker
  2021-03-28 14:20 ` Faraz Shahbazker
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Faraz Shahbazker @ 2021-03-28  9:32 UTC (permalink / raw)
  To: gdb-patches; +Cc: Faraz Shahbazker, Chao-ying Fu

sim/mips/ChangeLog
	* interp.c (sim_monitor): Add switch entries for unlink (13),
	lseek (14), and stat (15).

Derived from patch authored by Steve Ellcey <sellcey@mips.com>
---
 sim/mips/ChangeLog |  6 +++++
 sim/mips/interp.c  | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/sim/mips/ChangeLog b/sim/mips/ChangeLog
index 12347a7..88dd7dc 100644
--- a/sim/mips/ChangeLog
+++ b/sim/mips/ChangeLog
@@ -1,3 +1,9 @@
+2021-03-28  Steve Ellcey  <sellcey@mips.com>
+	    Faraz Shahbazker  <fshahbazker@wavecomp.com>
+
+	* interp.c (sim_monitor): Add switch entries for unlink (13),
+	lseek (14), and stat (15).
+
 2021-02-28  Mike Frysinger  <vapier@gentoo.org>
 
 	* configure: Regenerate.
diff --git a/sim/mips/interp.c b/sim/mips/interp.c
index fd93a12..13e9129 100644
--- a/sim/mips/interp.c
+++ b/sim/mips/interp.c
@@ -40,6 +40,7 @@ code on the hardware.
 #include <ansidecl.h>
 #include <ctype.h>
 #include <limits.h>
+#include <byteswap.h>
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
@@ -1167,7 +1168,7 @@ sim_monitor (SIM_DESC sd,
 
   /* The following callback functions are available, however the
      monitor we are simulating does not make use of them: get_errno,
-     isatty, lseek, rename, system, time and unlink */
+     isatty, rename, system and time.  */
   switch (reason)
     {
 
@@ -1241,6 +1242,71 @@ sim_monitor (SIM_DESC sd,
 	break;
       }
 
+    case 13: /* int unlink(const char *path) */
+      {
+	char *path = fetch_str (sd, A0);
+	V0 = sim_io_unlink (sd, path);
+	free (path);
+	break;
+      }
+
+    case 14: /* int lseek(int fd, int offset, int whence) */
+      {
+	V0 = sim_io_lseek (sd, A0, A1, A2);
+	break;
+      }
+
+/* We may need to swap stat data around before passing it on to the
+   program being run.  */
+#define copy16(x) (BigEndianMem ? bswap_16 (x) : (x))
+#define copy32(x) (BigEndianMem ? bswap_32 (x) : (x))
+#define copy64(x) (BigEndianMem ? bswap_64 (x) : (x))
+
+    case 15: /* int stat(const char *path, struct stat *buf); */
+      {
+	/* We need to put the data into the type of stat structure
+	   that MIPS uses and make sure it has the correct endianness.
+	   We are assuming that the host and MIPS agree on what the bits
+	   in st_mode mean.  That appears to be true for x86 linux and
+	   MIPS.  */
+	struct stat host_stat;
+	struct __attribute__ ((__packed__)) mips_stat {
+	  short st_dev;
+	  unsigned short st_ino;
+	  unsigned int st_mode;
+	  unsigned short st_nlink;
+	  unsigned short st_uid;
+	  unsigned short st_gid;
+	  short st_rdev;
+	  union {
+	    int b32;
+	    long b64;
+	  } st_size;
+	} mips_stat;
+	char *buf;
+	char *path = fetch_str (sd, A0);
+	bfd *prog_bfd = STATE_PROG_BFD (sd);
+	int is_elf32bit = (elf_elfheader(prog_bfd)->e_ident[EI_CLASS] ==
+			   ELFCLASS32);
+	buf = (char *) A1;
+	V0 = sim_io_stat (sd, path, &host_stat);
+	free (path);
+	mips_stat.st_dev = copy16 ((short) host_stat.st_dev);
+	mips_stat.st_ino = copy16 ((unsigned short) host_stat.st_ino);
+	mips_stat.st_mode = copy32 ((int) host_stat.st_mode);
+	mips_stat.st_nlink = copy16 ((unsigned short) host_stat.st_nlink);
+	mips_stat.st_uid = copy16 ((unsigned short) host_stat.st_uid);
+	mips_stat.st_gid = copy16 ((unsigned short) host_stat.st_gid);
+	mips_stat.st_rdev = copy16 ((short) host_stat.st_rdev);
+	if (is_elf32bit)
+	  mips_stat.st_size.b32 = copy32 ((int) host_stat.st_size);
+	else
+	  mips_stat.st_size.b64 = copy64 ((long) host_stat.st_size);
+	sim_write (sd, A1, (char *) &mips_stat,
+		   sizeof(mips_stat) - (is_elf32bit? 4 : 0));
+	break;
+      }
+
     case 17: /* void _exit() */
       {
 	sim_io_eprintf (sd, "sim_monitor(17): _exit(int reason) to be coded\n");
-- 
2.9.5


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

* Re: [PATCH] sim: mips: Add handlers to simulator monitor for unlink,  lseek and stat
  2021-03-28  9:32 [PATCH] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat Faraz Shahbazker
@ 2021-03-28 14:20 ` Faraz Shahbazker
  2021-03-28 14:54 ` Mike Frysinger
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Faraz Shahbazker @ 2021-03-28 14:20 UTC (permalink / raw)
  To: gdb-patches, Andrew Burgess, Mike Frysinger; +Cc: Chao-ying Fu

Tagging global maintainers.
________________________________
From: Faraz Shahbazker <fshahbazker@wavecomp.com>
Sent: Sunday, March 28, 2021 2:32 AM
To: gdb-patches@sourceware.org <gdb-patches@sourceware.org>
Cc: Faraz Shahbazker <fshahbazker@wavecomp.com>; Chao-ying Fu <cfu@wavecomp.com>
Subject: [PATCH] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat

sim/mips/ChangeLog
        * interp.c (sim_monitor): Add switch entries for unlink (13),
        lseek (14), and stat (15).

Derived from patch authored by Steve Ellcey <sellcey@mips.com>
---
 sim/mips/ChangeLog |  6 +++++
 sim/mips/interp.c  | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 73 insertions(+), 1 deletion(-)

diff --git a/sim/mips/ChangeLog b/sim/mips/ChangeLog
index 12347a7..88dd7dc 100644
--- a/sim/mips/ChangeLog
+++ b/sim/mips/ChangeLog
@@ -1,3 +1,9 @@
+2021-03-28  Steve Ellcey  <sellcey@mips.com>
+           Faraz Shahbazker  <fshahbazker@wavecomp.com>
+
+       * interp.c (sim_monitor): Add switch entries for unlink (13),
+       lseek (14), and stat (15).
+
 2021-02-28  Mike Frysinger  <vapier@gentoo.org>

         * configure: Regenerate.
diff --git a/sim/mips/interp.c b/sim/mips/interp.c
index fd93a12..13e9129 100644
--- a/sim/mips/interp.c
+++ b/sim/mips/interp.c
@@ -40,6 +40,7 @@ code on the hardware.
 #include <ansidecl.h>
 #include <ctype.h>
 #include <limits.h>
+#include <byteswap.h>
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
@@ -1167,7 +1168,7 @@ sim_monitor (SIM_DESC sd,

   /* The following callback functions are available, however the
      monitor we are simulating does not make use of them: get_errno,
-     isatty, lseek, rename, system, time and unlink */
+     isatty, rename, system and time.  */
   switch (reason)
     {

@@ -1241,6 +1242,71 @@ sim_monitor (SIM_DESC sd,
         break;
       }

+    case 13: /* int unlink(const char *path) */
+      {
+       char *path = fetch_str (sd, A0);
+       V0 = sim_io_unlink (sd, path);
+       free (path);
+       break;
+      }
+
+    case 14: /* int lseek(int fd, int offset, int whence) */
+      {
+       V0 = sim_io_lseek (sd, A0, A1, A2);
+       break;
+      }
+
+/* We may need to swap stat data around before passing it on to the
+   program being run.  */
+#define copy16(x) (BigEndianMem ? bswap_16 (x) : (x))
+#define copy32(x) (BigEndianMem ? bswap_32 (x) : (x))
+#define copy64(x) (BigEndianMem ? bswap_64 (x) : (x))
+
+    case 15: /* int stat(const char *path, struct stat *buf); */
+      {
+       /* We need to put the data into the type of stat structure
+          that MIPS uses and make sure it has the correct endianness.
+          We are assuming that the host and MIPS agree on what the bits
+          in st_mode mean.  That appears to be true for x86 linux and
+          MIPS.  */
+       struct stat host_stat;
+       struct __attribute__ ((__packed__)) mips_stat {
+         short st_dev;
+         unsigned short st_ino;
+         unsigned int st_mode;
+         unsigned short st_nlink;
+         unsigned short st_uid;
+         unsigned short st_gid;
+         short st_rdev;
+         union {
+           int b32;
+           long b64;
+         } st_size;
+       } mips_stat;
+       char *buf;
+       char *path = fetch_str (sd, A0);
+       bfd *prog_bfd = STATE_PROG_BFD (sd);
+       int is_elf32bit = (elf_elfheader(prog_bfd)->e_ident[EI_CLASS] ==
+                          ELFCLASS32);
+       buf = (char *) A1;
+       V0 = sim_io_stat (sd, path, &host_stat);
+       free (path);
+       mips_stat.st_dev = copy16 ((short) host_stat.st_dev);
+       mips_stat.st_ino = copy16 ((unsigned short) host_stat.st_ino);
+       mips_stat.st_mode = copy32 ((int) host_stat.st_mode);
+       mips_stat.st_nlink = copy16 ((unsigned short) host_stat.st_nlink);
+       mips_stat.st_uid = copy16 ((unsigned short) host_stat.st_uid);
+       mips_stat.st_gid = copy16 ((unsigned short) host_stat.st_gid);
+       mips_stat.st_rdev = copy16 ((short) host_stat.st_rdev);
+       if (is_elf32bit)
+         mips_stat.st_size.b32 = copy32 ((int) host_stat.st_size);
+       else
+         mips_stat.st_size.b64 = copy64 ((long) host_stat.st_size);
+       sim_write (sd, A1, (char *) &mips_stat,
+                  sizeof(mips_stat) - (is_elf32bit? 4 : 0));
+       break;
+      }
+
     case 17: /* void _exit() */
       {
         sim_io_eprintf (sd, "sim_monitor(17): _exit(int reason) to be coded\n");
--
2.9.5


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

* Re: [PATCH] sim: mips: Add handlers to simulator monitor for unlink,  lseek and stat
  2021-03-28  9:32 [PATCH] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat Faraz Shahbazker
  2021-03-28 14:20 ` Faraz Shahbazker
@ 2021-03-28 14:54 ` Mike Frysinger
  2021-03-30 21:20   ` [EXTERNAL]Re: " Faraz Shahbazker
  2021-03-30 21:21 ` [PATCH v1] " Faraz Shahbazker
  2021-03-30 21:40 ` [PATCH v2] " Faraz Shahbazker
  3 siblings, 1 reply; 11+ messages in thread
From: Mike Frysinger @ 2021-03-28 14:54 UTC (permalink / raw)
  To: Faraz Shahbazker; +Cc: gdb-patches, Chao-ying Fu

On 28 Mar 2021 02:32, Faraz Shahbazker wrote:
> sim/mips/ChangeLog
> 	* interp.c (sim_monitor): Add switch entries for unlink (13),
> 	lseek (14), and stat (15).

what ABI is sim_monitor supposed to be supporting ?  it seems to be confused
between IDT monitor & PMON, or at least out of sync with the libgloss code.

it would be nice if this were cleaned up to define the ABIs using constant
maps.  cris/traps.c is one example.

> +#include <byteswap.h>

this is not portable.  we have sim-endian.h APIs already you should use.

> +    case 15: /* int stat(const char *path, struct stat *buf); */
> +      {
> +	/* We need to put the data into the type of stat structure
> +	   that MIPS uses and make sure it has the correct endianness.
> +	   We are assuming that the host and MIPS agree on what the bits
> +	   in st_mode mean.  That appears to be true for x86 linux and
> +	   MIPS.  */
> +	struct stat host_stat;
> +	struct __attribute__ ((__packed__)) mips_stat {
> +	  short st_dev;
> +	  unsigned short st_ino;
> +	  unsigned int st_mode;
> +	  unsigned short st_nlink;
> +	  unsigned short st_uid;
> +	  unsigned short st_gid;
> +	  short st_rdev;
> +	  union {
> +	    int b32;
> +	    long b64;
> +	  } st_size;
> +	} mips_stat;

the sim core provides APIs to automate this.  grep for "stat_map" in the bfin,
cris, and common code.
-mike

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

* Re: [EXTERNAL]Re: [PATCH] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat
@ 2021-03-30 21:20   ` Faraz Shahbazker
  2021-03-31  2:17     ` Mike Frysinger
  0 siblings, 1 reply; 11+ messages in thread
From: Faraz Shahbazker @ 2021-03-30 21:20 UTC (permalink / raw)
  To: gdb-patches, Chao-ying Fu, Mike Frysinger

On 3/28/21 8:24 PM, Mike Frysinger wrote:
> On 28 Mar 2021 02:32, Faraz Shahbazker wrote:
>> sim/mips/ChangeLog * interp.c (sim_monitor): Add switch entries
>> for unlink (13), lseek (14), and stat (15).
> 
> what ABI is sim_monitor supposed to be supporting ?  it seems to be 
> confused between IDT monitor & PMON, or at least out of sync with
> the libgloss code.

Its a mixed bag but it seems to be in-sync with libgloss, as far as I understand.
PMON is partially re-mapped to IDT in interp.c:sim_open. MIPS moved sharply
to its own Unified Hosting Interface Layer in the recent years and this particular
patch arises out of the desire to maintain feature parity with UHI. 

> the sim core provides APIs to automate this.  grep for "stat_map" in 
> the bfin, cris, and common code. -mike

Updated patch.

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

* [PATCH v1] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat
  2021-03-28  9:32 [PATCH] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat Faraz Shahbazker
  2021-03-28 14:20 ` Faraz Shahbazker
  2021-03-28 14:54 ` Mike Frysinger
@ 2021-03-30 21:21 ` Faraz Shahbazker
  2021-03-31  2:19   ` Mike Frysinger
  2021-03-30 21:40 ` [PATCH v2] " Faraz Shahbazker
  3 siblings, 1 reply; 11+ messages in thread
From: Faraz Shahbazker @ 2021-03-30 21:21 UTC (permalink / raw)
  To: gdb-patches; +Cc: Faraz Shahbazker, Chao-ying Fu

sim/mips/ChangeLog
	* interp.c (sim_monitor): Add switch entries for unlink (13),
	lseek (14), and stat (15).

Derived from patch authored by Steve Ellcey <sellcey@mips.com>
---
 sim/mips/ChangeLog |  6 ++++
 sim/mips/interp.c  | 73 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/sim/mips/ChangeLog b/sim/mips/ChangeLog
index 12347a7e3e2..56464eb6727 100644
--- a/sim/mips/ChangeLog
+++ b/sim/mips/ChangeLog
@@ -1,3 +1,9 @@
+2021-03-31  Steve Ellcey  <sellcey@mips.com>
+	    Faraz Shahbazker  <fshahbazker@wavecomp.com>
+
+	* interp.c (sim_monitor): Add switch entries for unlink (13),
+	lseek (14), and stat (15).
+
 2021-02-28  Mike Frysinger  <vapier@gentoo.org>
 
 	* configure: Regenerate.
diff --git a/sim/mips/interp.c b/sim/mips/interp.c
index fd93a12f69a..bcc4a3f91a4 100644
--- a/sim/mips/interp.c
+++ b/sim/mips/interp.c
@@ -40,6 +40,7 @@ code on the hardware.
 #include <ansidecl.h>
 #include <ctype.h>
 #include <limits.h>
+
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
@@ -50,6 +51,7 @@ code on the hardware.
 #include "elf-bfd.h"
 #include "gdb/callback.h"   /* GDB simulator callback interface */
 #include "gdb/remote-sim.h" /* GDB simulator interface */
+#include "sim-syscall.h"   /* Simulator system call support */
 
 char* pr_addr (SIM_ADDR addr);
 char* pr_uword64 (uword64 addr);
@@ -1147,6 +1149,23 @@ Recognized firmware names are: `idt', `pmon', `lsipmon', and `none'.\n",
   return SIM_RC_OK;
 }
 
+/* stat structures from MIPS32/64.  */
+static const char stat32_map[] =
+"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
+":st_rdev,2:st_size,4:st_atime,4:st_spare1,4:st_mtime,4:st_spare2,4"
+":st_ctime,4:st_spare3,4:st_blksize,4:st_blocks,4:st_spare4,8";
+
+static const char stat64_map[] =
+"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
+":st_rdev,2:st_size,8:st_atime,8:st_spare1,8:st_mtime,8:st_spare2,8"
+":st_ctime,8:st_spare3,8:st_blksize,8:st_blocks,8:st_spare4,16";
+
+/* Map for calls using the host struct stat.  */
+static const CB_TARGET_DEFS_MAP CB_stat_map[] =
+{
+  { "stat", CB_SYS_stat, 15 },
+  { 0, -1, -1 }
+};
 
 
 /* Simple monitor interface (currently setup for the IDT and PMON monitors) */
@@ -1167,7 +1186,7 @@ sim_monitor (SIM_DESC sd,
 
   /* The following callback functions are available, however the
      monitor we are simulating does not make use of them: get_errno,
-     isatty, lseek, rename, system, time and unlink */
+     isatty, rename, system and time.  */
   switch (reason)
     {
 
@@ -1241,6 +1260,58 @@ sim_monitor (SIM_DESC sd,
 	break;
       }
 
+    case 13: /* int unlink(const char *path) */
+      {
+	char *path = fetch_str (sd, A0);
+	V0 = sim_io_unlink (sd, path);
+	free (path);
+	break;
+      }
+
+    case 14: /* int lseek(int fd, int offset, int whence) */
+      {
+	V0 = sim_io_lseek (sd, A0, A1, A2);
+	break;
+      }
+
+    case 15: /* int stat(const char *path, struct stat *buf); */
+      {
+	/* As long as the infrastructure doesn't cache anything
+	   related to the stat mapping, this trick gets us a dual
+	   "struct stat"-type mapping in the least error-prone way.  */
+	host_callback *cb = STATE_CALLBACK (sd);
+	const char *saved_map = cb->stat_map;
+	CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
+	bfd *prog_bfd = STATE_PROG_BFD (sd);
+	int is_elf32bit = (elf_elfheader(prog_bfd)->e_ident[EI_CLASS] ==
+			   ELFCLASS32);
+	static CB_SYSCALL s;
+	CB_SYSCALL_INIT (&s);
+	s.func = 15;
+	/* Mask out the sign extension part for 64-bit targets because the
+	   MIPS simulator memory model is still 32-bit.  */
+	s.arg1 = A0 & 0xFFFFFFFF
+	s.arg2 = A1 & 0xFFFFFFFF;
+	s.p1 = (PTR) sd;
+	s.p2 = (PTR) cpu;
+	s.read_mem = sim_syscall_read_mem;
+	s.write_mem = sim_syscall_write_mem;
+
+	cb->syscall_map = (CB_TARGET_DEFS_MAP *) CB_stat_map;
+	cb->stat_map = is_elf32bit ? stat32_map : stat64_map;
+
+	if (cb_syscall (cb, &s) != CB_RC_OK)
+	  {
+	    abort ();
+	    sim_engine_halt (sd, cpu, NULL, mips_pc_get (cpu),
+			     sim_stopped, SIM_SIGILL);
+	  }
+
+	cb->stat_map = saved_map;
+	cb->syscall_map = saved_syscall_map;
+	break;
+      }
+
     case 17: /* void _exit() */
       {
 	sim_io_eprintf (sd, "sim_monitor(17): _exit(int reason) to be coded\n");
-- 
2.25.1


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

* [PATCH v2] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat
  2021-03-28  9:32 [PATCH] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat Faraz Shahbazker
                   ` (2 preceding siblings ...)
  2021-03-30 21:21 ` [PATCH v1] " Faraz Shahbazker
@ 2021-03-30 21:40 ` Faraz Shahbazker
  3 siblings, 0 replies; 11+ messages in thread
From: Faraz Shahbazker @ 2021-03-30 21:40 UTC (permalink / raw)
  To: gdb-patches, Mike Frysinger; +Cc: Faraz Shahbazker, Chao-ying Fu

sim/mips/ChangeLog
	* interp.c (sim_monitor): Add switch entries for unlink (13),
	lseek (14), and stat (15).

Derived from patch authored by Steve Ellcey <sellcey@mips.com>
---

Notes:
    v1 is broken due to a typo. This update uses stat_map API for
    the stat callback.

 sim/mips/ChangeLog |  6 ++++
 sim/mips/interp.c  | 73 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 78 insertions(+), 1 deletion(-)

diff --git a/sim/mips/ChangeLog b/sim/mips/ChangeLog
index 12347a7e3e2..56464eb6727 100644
--- a/sim/mips/ChangeLog
+++ b/sim/mips/ChangeLog
@@ -1,3 +1,9 @@
+2021-03-31  Steve Ellcey  <sellcey@mips.com>
+	    Faraz Shahbazker  <fshahbazker@wavecomp.com>
+
+	* interp.c (sim_monitor): Add switch entries for unlink (13),
+	lseek (14), and stat (15).
+
 2021-02-28  Mike Frysinger  <vapier@gentoo.org>
 
 	* configure: Regenerate.
diff --git a/sim/mips/interp.c b/sim/mips/interp.c
index fd93a12f69a..29ffecd6ee9 100644
--- a/sim/mips/interp.c
+++ b/sim/mips/interp.c
@@ -40,6 +40,7 @@ code on the hardware.
 #include <ansidecl.h>
 #include <ctype.h>
 #include <limits.h>
+
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
@@ -50,6 +51,7 @@ code on the hardware.
 #include "elf-bfd.h"
 #include "gdb/callback.h"   /* GDB simulator callback interface */
 #include "gdb/remote-sim.h" /* GDB simulator interface */
+#include "sim-syscall.h"   /* Simulator system call support */
 
 char* pr_addr (SIM_ADDR addr);
 char* pr_uword64 (uword64 addr);
@@ -1147,6 +1149,23 @@ Recognized firmware names are: `idt', `pmon', `lsipmon', and `none'.\n",
   return SIM_RC_OK;
 }
 
+/* stat structures from MIPS32/64.  */
+static const char stat32_map[] =
+"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
+":st_rdev,2:st_size,4:st_atime,4:st_spare1,4:st_mtime,4:st_spare2,4"
+":st_ctime,4:st_spare3,4:st_blksize,4:st_blocks,4:st_spare4,8";
+
+static const char stat64_map[] =
+"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
+":st_rdev,2:st_size,8:st_atime,8:st_spare1,8:st_mtime,8:st_spare2,8"
+":st_ctime,8:st_spare3,8:st_blksize,8:st_blocks,8:st_spare4,16";
+
+/* Map for calls using the host struct stat.  */
+static const CB_TARGET_DEFS_MAP CB_stat_map[] =
+{
+  { "stat", CB_SYS_stat, 15 },
+  { 0, -1, -1 }
+};
 
 
 /* Simple monitor interface (currently setup for the IDT and PMON monitors) */
@@ -1167,7 +1186,7 @@ sim_monitor (SIM_DESC sd,
 
   /* The following callback functions are available, however the
      monitor we are simulating does not make use of them: get_errno,
-     isatty, lseek, rename, system, time and unlink */
+     isatty, rename, system and time.  */
   switch (reason)
     {
 
@@ -1241,6 +1260,58 @@ sim_monitor (SIM_DESC sd,
 	break;
       }
 
+    case 13: /* int unlink(const char *path) */
+      {
+	char *path = fetch_str (sd, A0);
+	V0 = sim_io_unlink (sd, path);
+	free (path);
+	break;
+      }
+
+    case 14: /* int lseek(int fd, int offset, int whence) */
+      {
+	V0 = sim_io_lseek (sd, A0, A1, A2);
+	break;
+      }
+
+    case 15: /* int stat(const char *path, struct stat *buf); */
+      {
+	/* As long as the infrastructure doesn't cache anything
+	   related to the stat mapping, this trick gets us a dual
+	   "struct stat"-type mapping in the least error-prone way.  */
+	host_callback *cb = STATE_CALLBACK (sd);
+	const char *saved_map = cb->stat_map;
+	CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
+	bfd *prog_bfd = STATE_PROG_BFD (sd);
+	int is_elf32bit = (elf_elfheader(prog_bfd)->e_ident[EI_CLASS] ==
+			   ELFCLASS32);
+	static CB_SYSCALL s;
+	CB_SYSCALL_INIT (&s);
+	s.func = 15;
+	/* Mask out the sign extension part for 64-bit targets because the
+	   MIPS simulator's memory model is still 32-bit.  */
+	s.arg1 = A0 & 0xFFFFFFFF;
+	s.arg2 = A1 & 0xFFFFFFFF;
+	s.p1 = (PTR) sd;
+	s.p2 = (PTR) cpu;
+	s.read_mem = sim_syscall_read_mem;
+	s.write_mem = sim_syscall_write_mem;
+
+	cb->syscall_map = (CB_TARGET_DEFS_MAP *) CB_stat_map;
+	cb->stat_map = is_elf32bit ? stat32_map : stat64_map;
+
+	if (cb_syscall (cb, &s) != CB_RC_OK)
+	  {
+	    abort ();
+	    sim_engine_halt (sd, cpu, NULL, mips_pc_get (cpu),
+			     sim_stopped, SIM_SIGILL);
+	  }
+
+	cb->stat_map = saved_map;
+	cb->syscall_map = saved_syscall_map;
+	break;
+      }
+
     case 17: /* void _exit() */
       {
 	sim_io_eprintf (sd, "sim_monitor(17): _exit(int reason) to be coded\n");
-- 
2.25.1


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

* Re: [EXTERNAL]Re: [PATCH] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat
  2021-03-30 21:20   ` [EXTERNAL]Re: " Faraz Shahbazker
@ 2021-03-31  2:17     ` Mike Frysinger
  0 siblings, 0 replies; 11+ messages in thread
From: Mike Frysinger @ 2021-03-31  2:17 UTC (permalink / raw)
  To: Faraz Shahbazker; +Cc: gdb-patches, Chao-ying Fu

On 30 Mar 2021 21:20, Faraz Shahbazker wrote:
> On 3/28/21 8:24 PM, Mike Frysinger wrote:
> > On 28 Mar 2021 02:32, Faraz Shahbazker wrote:
> >> sim/mips/ChangeLog * interp.c (sim_monitor): Add switch entries
> >> for unlink (13), lseek (14), and stat (15).
> > 
> > what ABI is sim_monitor supposed to be supporting ?  it seems to be 
> > confused between IDT monitor & PMON, or at least out of sync with
> > the libgloss code.
> 
> Its a mixed bag but it seems to be in-sync with libgloss, as far as I understand.

the IDT & PMON syscalls in libgloss conflict:
idtmon.S:INDIRECT(open,6)
idtmon.S:INDIRECT(read,7)
idtmon.S:INDIRECT(write,8)
idtmon.S:INDIRECT(close,10)
pmon.S:INDIRECT(read,0)
pmon.S:INDIRECT(write,1)
pmon.S:INDIRECT(open,2)
pmon.S:INDIRECT(close,3)
pmon.S:INDIRECT(mon_ioctl,4)
pmon.S:INDIRECT(mon_printf,5)
pmon.S:INDIRECT(mon_vsprintf,6)
pmon.S:INDIRECT(mon_ttctl,7)
pmon.S:INDIRECT(mon_cliexit,8)

so normally it'd be impossible to support both

> PMON is partially re-mapped to IDT in interp.c:sim_open. MIPS moved sharply
> to its own Unified Hosting Interface Layer in the recent years and this particular
> patch arises out of the desire to maintain feature parity with UHI. 

i guess this is the trick that i missed: the PMON syscalls get hot patched
to reroute to the IDT syscall table.  thanks for the pointer.
-mike

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

* Re: [PATCH v1] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat
  2021-03-30 21:21 ` [PATCH v1] " Faraz Shahbazker
@ 2021-03-31  2:19   ` Mike Frysinger
  2021-03-31  7:10     ` [PATCH v3] " Faraz Shahbazker
  0 siblings, 1 reply; 11+ messages in thread
From: Mike Frysinger @ 2021-03-31  2:19 UTC (permalink / raw)
  To: Faraz Shahbazker; +Cc: gdb-patches, Chao-ying Fu

On 31 Mar 2021 02:51, Faraz Shahbazker wrote:
> sim/mips/ChangeLog
> 	* interp.c (sim_monitor): Add switch entries for unlink (13),
> 	lseek (14), and stat (15).
> 
> Derived from patch authored by Steve Ellcey <sellcey@mips.com>

mostly looks good.  just one thing ...

> --- a/sim/mips/interp.c
> +++ b/sim/mips/interp.c
> +	if (cb_syscall (cb, &s) != CB_RC_OK)
> +	  {
> +	    abort ();
> +	    sim_engine_halt (sd, cpu, NULL, mips_pc_get (cpu),
> +			     sim_stopped, SIM_SIGILL);
> +	  }

why the abort ?  sim_engine_halt should be more than sufficient to throw an
error & shut everything down.  abort will kill the process which is bad when
we're using the sim as a library embedded into another program/runtime.
-mike

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

* [PATCH v3] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat
  2021-03-31  2:19   ` Mike Frysinger
@ 2021-03-31  7:10     ` Faraz Shahbazker
  2021-04-01  2:33       ` Mike Frysinger
  0 siblings, 1 reply; 11+ messages in thread
From: Faraz Shahbazker @ 2021-03-31  7:10 UTC (permalink / raw)
  To: gdb-patches, Mike Frysinger; +Cc: Faraz Shahbazker, Chao-ying Fu

sim/mips/ChangeLog
	* interp.c (sim_monitor): Add switch entries for unlink (13),
	lseek (14), and stat (15).

Derived from patch authored by Steve Ellcey <sellcey@mips.com>
---

Notes:
    Changes from v2:
    - remove call to abort() on cb_syscall failure
    - capture return value of call in V0

 sim/mips/ChangeLog |  6 ++++
 sim/mips/interp.c  | 70 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/sim/mips/ChangeLog b/sim/mips/ChangeLog
index 12347a7e3e2..56464eb6727 100644
--- a/sim/mips/ChangeLog
+++ b/sim/mips/ChangeLog
@@ -1,3 +1,9 @@
+2021-03-31  Steve Ellcey  <sellcey@mips.com>
+	    Faraz Shahbazker  <fshahbazker@wavecomp.com>
+
+	* interp.c (sim_monitor): Add switch entries for unlink (13),
+	lseek (14), and stat (15).
+
 2021-02-28  Mike Frysinger  <vapier@gentoo.org>
 
 	* configure: Regenerate.
diff --git a/sim/mips/interp.c b/sim/mips/interp.c
index fd93a12f69a..55b089dbee9 100644
--- a/sim/mips/interp.c
+++ b/sim/mips/interp.c
@@ -50,6 +50,7 @@ code on the hardware.
 #include "elf-bfd.h"
 #include "gdb/callback.h"   /* GDB simulator callback interface */
 #include "gdb/remote-sim.h" /* GDB simulator interface */
+#include "sim-syscall.h"   /* Simulator system call support */
 
 char* pr_addr (SIM_ADDR addr);
 char* pr_uword64 (uword64 addr);
@@ -1147,6 +1148,23 @@ Recognized firmware names are: `idt', `pmon', `lsipmon', and `none'.\n",
   return SIM_RC_OK;
 }
 
+/* stat structures from MIPS32/64.  */
+static const char stat32_map[] =
+"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
+":st_rdev,2:st_size,4:st_atime,4:st_spare1,4:st_mtime,4:st_spare2,4"
+":st_ctime,4:st_spare3,4:st_blksize,4:st_blocks,4:st_spare4,8";
+
+static const char stat64_map[] =
+"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
+":st_rdev,2:st_size,8:st_atime,8:st_spare1,8:st_mtime,8:st_spare2,8"
+":st_ctime,8:st_spare3,8:st_blksize,8:st_blocks,8:st_spare4,16";
+
+/* Map for calls using the host struct stat.  */
+static const CB_TARGET_DEFS_MAP CB_stat_map[] =
+{
+  { "stat", CB_SYS_stat, 15 },
+  { 0, -1, -1 }
+};
 
 
 /* Simple monitor interface (currently setup for the IDT and PMON monitors) */
@@ -1167,7 +1185,7 @@ sim_monitor (SIM_DESC sd,
 
   /* The following callback functions are available, however the
      monitor we are simulating does not make use of them: get_errno,
-     isatty, lseek, rename, system, time and unlink */
+     isatty, rename, system and time.  */
   switch (reason)
     {
 
@@ -1241,6 +1259,56 @@ sim_monitor (SIM_DESC sd,
 	break;
       }
 
+    case 13: /* int unlink(const char *path) */
+      {
+	char *path = fetch_str (sd, A0);
+	V0 = sim_io_unlink (sd, path);
+	free (path);
+	break;
+      }
+
+    case 14: /* int lseek(int fd, int offset, int whence) */
+      {
+	V0 = sim_io_lseek (sd, A0, A1, A2);
+	break;
+      }
+
+    case 15: /* int stat(const char *path, struct stat *buf); */
+      {
+	/* As long as the infrastructure doesn't cache anything
+	   related to the stat mapping, this trick gets us a dual
+	   "struct stat"-type mapping in the least error-prone way.  */
+	host_callback *cb = STATE_CALLBACK (sd);
+	const char *saved_map = cb->stat_map;
+	CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
+	bfd *prog_bfd = STATE_PROG_BFD (sd);
+	int is_elf32bit = (elf_elfheader(prog_bfd)->e_ident[EI_CLASS] ==
+			   ELFCLASS32);
+	static CB_SYSCALL s;
+	CB_SYSCALL_INIT (&s);
+	s.func = 15;
+	/* Mask out the sign extension part for 64-bit targets because the
+	   MIPS simulator's memory model is still 32-bit.  */
+	s.arg1 = A0 & 0xFFFFFFFF;
+	s.arg2 = A1 & 0xFFFFFFFF;
+	s.p1 = (PTR) sd;
+	s.p2 = (PTR) cpu;
+	s.read_mem = sim_syscall_read_mem;
+	s.write_mem = sim_syscall_write_mem;
+
+	cb->syscall_map = (CB_TARGET_DEFS_MAP *) CB_stat_map;
+	cb->stat_map = is_elf32bit ? stat32_map : stat64_map;
+
+	if (cb_syscall (cb, &s) != CB_RC_OK)
+	  sim_engine_halt (sd, cpu, NULL, mips_pc_get (cpu),
+			   sim_stopped, SIM_SIGILL);
+
+	V0 = s.result;
+	cb->stat_map = saved_map;
+	cb->syscall_map = saved_syscall_map;
+	break;
+      }
+
     case 17: /* void _exit() */
       {
 	sim_io_eprintf (sd, "sim_monitor(17): _exit(int reason) to be coded\n");
-- 
2.25.1


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

* Re: [PATCH v3] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat
  2021-03-31  7:10     ` [PATCH v3] " Faraz Shahbazker
@ 2021-04-01  2:33       ` Mike Frysinger
  2021-04-02 20:36         ` Maciej W. Rozycki
  0 siblings, 1 reply; 11+ messages in thread
From: Mike Frysinger @ 2021-04-01  2:33 UTC (permalink / raw)
  To: Faraz Shahbazker; +Cc: gdb-patches, Chao-ying Fu, macro

lgtm!  not sure if Maciej wants to take a pass.
-mike

On 31 Mar 2021 12:40, Faraz Shahbazker wrote:
> sim/mips/ChangeLog
> 	* interp.c (sim_monitor): Add switch entries for unlink (13),
> 	lseek (14), and stat (15).
> 
> Derived from patch authored by Steve Ellcey <sellcey@mips.com>
> ---
> 
> Notes:
>     Changes from v2:
>     - remove call to abort() on cb_syscall failure
>     - capture return value of call in V0
> 
>  sim/mips/ChangeLog |  6 ++++
>  sim/mips/interp.c  | 70 +++++++++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 75 insertions(+), 1 deletion(-)
> 
> diff --git a/sim/mips/ChangeLog b/sim/mips/ChangeLog
> index 12347a7e3e2..56464eb6727 100644
> --- a/sim/mips/ChangeLog
> +++ b/sim/mips/ChangeLog
> @@ -1,3 +1,9 @@
> +2021-03-31  Steve Ellcey  <sellcey@mips.com>
> +	    Faraz Shahbazker  <fshahbazker@wavecomp.com>
> +
> +	* interp.c (sim_monitor): Add switch entries for unlink (13),
> +	lseek (14), and stat (15).
> +
>  2021-02-28  Mike Frysinger  <vapier@gentoo.org>
>  
>  	* configure: Regenerate.
> diff --git a/sim/mips/interp.c b/sim/mips/interp.c
> index fd93a12f69a..55b089dbee9 100644
> --- a/sim/mips/interp.c
> +++ b/sim/mips/interp.c
> @@ -50,6 +50,7 @@ code on the hardware.
>  #include "elf-bfd.h"
>  #include "gdb/callback.h"   /* GDB simulator callback interface */
>  #include "gdb/remote-sim.h" /* GDB simulator interface */
> +#include "sim-syscall.h"   /* Simulator system call support */
>  
>  char* pr_addr (SIM_ADDR addr);
>  char* pr_uword64 (uword64 addr);
> @@ -1147,6 +1148,23 @@ Recognized firmware names are: `idt', `pmon', `lsipmon', and `none'.\n",
>    return SIM_RC_OK;
>  }
>  
> +/* stat structures from MIPS32/64.  */
> +static const char stat32_map[] =
> +"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
> +":st_rdev,2:st_size,4:st_atime,4:st_spare1,4:st_mtime,4:st_spare2,4"
> +":st_ctime,4:st_spare3,4:st_blksize,4:st_blocks,4:st_spare4,8";
> +
> +static const char stat64_map[] =
> +"st_dev,2:st_ino,2:st_mode,4:st_nlink,2:st_uid,2:st_gid,2"
> +":st_rdev,2:st_size,8:st_atime,8:st_spare1,8:st_mtime,8:st_spare2,8"
> +":st_ctime,8:st_spare3,8:st_blksize,8:st_blocks,8:st_spare4,16";
> +
> +/* Map for calls using the host struct stat.  */
> +static const CB_TARGET_DEFS_MAP CB_stat_map[] =
> +{
> +  { "stat", CB_SYS_stat, 15 },
> +  { 0, -1, -1 }
> +};
>  
>  
>  /* Simple monitor interface (currently setup for the IDT and PMON monitors) */
> @@ -1167,7 +1185,7 @@ sim_monitor (SIM_DESC sd,
>  
>    /* The following callback functions are available, however the
>       monitor we are simulating does not make use of them: get_errno,
> -     isatty, lseek, rename, system, time and unlink */
> +     isatty, rename, system and time.  */
>    switch (reason)
>      {
>  
> @@ -1241,6 +1259,56 @@ sim_monitor (SIM_DESC sd,
>  	break;
>        }
>  
> +    case 13: /* int unlink(const char *path) */
> +      {
> +	char *path = fetch_str (sd, A0);
> +	V0 = sim_io_unlink (sd, path);
> +	free (path);
> +	break;
> +      }
> +
> +    case 14: /* int lseek(int fd, int offset, int whence) */
> +      {
> +	V0 = sim_io_lseek (sd, A0, A1, A2);
> +	break;
> +      }
> +
> +    case 15: /* int stat(const char *path, struct stat *buf); */
> +      {
> +	/* As long as the infrastructure doesn't cache anything
> +	   related to the stat mapping, this trick gets us a dual
> +	   "struct stat"-type mapping in the least error-prone way.  */
> +	host_callback *cb = STATE_CALLBACK (sd);
> +	const char *saved_map = cb->stat_map;
> +	CB_TARGET_DEFS_MAP *saved_syscall_map = cb->syscall_map;
> +	bfd *prog_bfd = STATE_PROG_BFD (sd);
> +	int is_elf32bit = (elf_elfheader(prog_bfd)->e_ident[EI_CLASS] ==
> +			   ELFCLASS32);
> +	static CB_SYSCALL s;
> +	CB_SYSCALL_INIT (&s);
> +	s.func = 15;
> +	/* Mask out the sign extension part for 64-bit targets because the
> +	   MIPS simulator's memory model is still 32-bit.  */
> +	s.arg1 = A0 & 0xFFFFFFFF;
> +	s.arg2 = A1 & 0xFFFFFFFF;
> +	s.p1 = (PTR) sd;
> +	s.p2 = (PTR) cpu;
> +	s.read_mem = sim_syscall_read_mem;
> +	s.write_mem = sim_syscall_write_mem;
> +
> +	cb->syscall_map = (CB_TARGET_DEFS_MAP *) CB_stat_map;
> +	cb->stat_map = is_elf32bit ? stat32_map : stat64_map;
> +
> +	if (cb_syscall (cb, &s) != CB_RC_OK)
> +	  sim_engine_halt (sd, cpu, NULL, mips_pc_get (cpu),
> +			   sim_stopped, SIM_SIGILL);
> +
> +	V0 = s.result;
> +	cb->stat_map = saved_map;
> +	cb->syscall_map = saved_syscall_map;
> +	break;
> +      }
> +
>      case 17: /* void _exit() */
>        {
>  	sim_io_eprintf (sd, "sim_monitor(17): _exit(int reason) to be coded\n");
> -- 
> 2.25.1
> 

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

* Re: [PATCH v3] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat
  2021-04-01  2:33       ` Mike Frysinger
@ 2021-04-02 20:36         ` Maciej W. Rozycki
  0 siblings, 0 replies; 11+ messages in thread
From: Maciej W. Rozycki @ 2021-04-02 20:36 UTC (permalink / raw)
  To: Mike Frysinger, Faraz Shahbazker; +Cc: gdb-patches, Chao-ying Fu

On Wed, 31 Mar 2021, Mike Frysinger wrote:

> lgtm!  not sure if Maciej wants to take a pass.

 It's not an area I am familiar with, so I've merely skimmed over only to 
find a couple of very minor style consistency issues.  Which I'll turn my 
blind eye to on this occasion, so I'm fine with this change as it stands.

 BTW, Faraz, good to see you're still onboard!

  Maciej

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

end of thread, other threads:[~2021-04-02 20:36 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-28  9:32 [PATCH] sim: mips: Add handlers to simulator monitor for unlink, lseek and stat Faraz Shahbazker
2021-03-28 14:20 ` Faraz Shahbazker
2021-03-28 14:54 ` Mike Frysinger
2021-03-30 21:20   ` [EXTERNAL]Re: " Faraz Shahbazker
2021-03-31  2:17     ` Mike Frysinger
2021-03-30 21:21 ` [PATCH v1] " Faraz Shahbazker
2021-03-31  2:19   ` Mike Frysinger
2021-03-31  7:10     ` [PATCH v3] " Faraz Shahbazker
2021-04-01  2:33       ` Mike Frysinger
2021-04-02 20:36         ` Maciej W. Rozycki
2021-03-30 21:40 ` [PATCH v2] " Faraz Shahbazker

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).