Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Faraz Shahbazker <fshahbazker@wavecomp.com>
To: 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
Date: Sun, 28 Mar 2021 02:32:06 -0700	[thread overview]
Message-ID: <20210328093206.15977-1-fshahbazker@wavecomp.com> (raw)

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


             reply	other threads:[~2021-03-28  9:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-28  9:32 Faraz Shahbazker [this message]
2021-03-28 14:20 ` Faraz Shahbazker
2021-03-28 14:54 ` Mike Frysinger via Gdb-patches
2021-03-30 21:20   ` [EXTERNAL]Re: " Faraz Shahbazker
2021-03-31  2:17     ` Mike Frysinger via Gdb-patches
2021-03-30 21:21 ` [PATCH v1] " Faraz Shahbazker
2021-03-31  2:19   ` Mike Frysinger via Gdb-patches
2021-03-31  7:10     ` [PATCH v3] " Faraz Shahbazker
2021-04-01  2:33       ` Mike Frysinger via Gdb-patches
2021-04-02 20:36         ` Maciej W. Rozycki
2021-03-30 21:40 ` [PATCH v2] " Faraz Shahbazker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210328093206.15977-1-fshahbazker@wavecomp.com \
    --to=fshahbazker@wavecomp.com \
    --cc=cfu@wavecomp.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox