From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Brian Gianforcaro <b.gianfo@gmail.com>
Date: Tue, 28 Dec 2021 04:35:47 -0800
Subject: [PATCH] minerva: Add basic ptrace based native target for
Minerva/i386
---
gdb/configure.host | 3 ++
gdb/configure.nat | 11 +++++
gdb/configure.tgt | 4 ++
gdb/i386-minerva-nat.c | 101 +++++++++++++++++++++++++++++++++++++++
gdb/i386-minerva-tdep.c | 43 +++++++++++++++++
gdb/i386-minerva-tdep.h | 8 ++++
gdb/osabi.c | 1 +
gdb/osabi.h | 1 +
gdb/minerva-nat.c | 13 +++++
gdb/minerva-nat.h | 16 +++++++
gdb/minerva-tdep.c | 28 +++++++++++
gdb/minerva-tdep.h | 24 ++++++++++
12 files changed, 253 insertions(+)
create mode 100644 gdb/i386-minerva-nat.c
create mode 100644 gdb/i386-minerva-tdep.c
create mode 100644 gdb/i386-minerva-tdep.h
create mode 100644 gdb/minerva-nat.c
create mode 100644 gdb/minerva-nat.h
create mode 100644 gdb/minerva-tdep.c
create mode 100644 gdb/minerva-tdep.h
diff --git a/gdb/configure.host b/gdb/configure.host
index e94a19b..06b87ca 100644
--- a/gdb/configure.host
+++ b/gdb/configure.host
@@ -85,6 +85,7 @@ case "${host}" in
*-*-darwin*) gdb_host=darwin ;;
aarch64*-*-linux*) gdb_host=linux ;;
+aarch64*-*-minerva*) gdb_host=minerva ;;
aarch64*-*-freebsd*) gdb_host=fbsd ;;
alpha*-*-linux*) gdb_host=alpha-linux ;;
@@ -117,6 +118,7 @@ i[34567]86-*-linux*) gdb_host=linux ;;
i[34567]86-*-gnu*) gdb_host=i386gnu ;;
i[3456]86-*-nto*) gdb_host=nto ;;
i[34567]86-*-openbsd*) gdb_host=obsd ;;
+i[34567]86-*-minerva*) gdb_host=minerva ;;
i[34567]86-*-solaris2* | x86_64-*-solaris2*)
gdb_host=sol2 ;;
i[34567]86-*-cygwin*) gdb_host=cygwin ;;
@@ -185,6 +187,7 @@ x86_64-*-mingw*) gdb_host=mingw64
gdb_host_obs=mingw-hdep.o
;;
x86_64-*-cygwin*) gdb_host=cygwin64 ;;
+x86_64-*-minerva*) gdb_host=minerva ;;
m32r*-*-linux*) gdb_host=linux ;;
xtensa*-*-linux*) gdb_host=linux ;;
diff --git a/gdb/configure.nat b/gdb/configure.nat
index f48fbda..6a0115f 100644
--- a/gdb/configure.nat
+++ b/gdb/configure.nat
@@ -86,6 +86,9 @@ case ${gdb_host} in
darwin)
NATDEPFILES='fork-child.o nat/fork-inferior.o darwin-nat.o \
darwin-nat-info.o'
+ ;;
+ minerva)
+ NATDEPFILES='fork-child.o nat/fork-inferior.o inf-ptrace.o'
;;
sol2)
NATDEPFILES='fork-child.o nat/fork-inferior.o \
@@ -477,6 +480,14 @@ case ${gdb_host} in
;;
esac
;;
+ minerva)
+ case ${gdb_host_cpu} in
+ i386)
+ # Host: Minerva/x86_64 ELF
+ NATDEPFILES="${NATDEPFILES} amd64-nat.o minerva-nat.o i386-minerva-nat.o"
+ ;;
+ esac
+ ;;
sol2)
case ${gdb_host_cpu} in
i386)
diff --git a/gdb/configure.tgt b/gdb/configure.tgt
index 97a5a57..886542f 100644
--- a/gdb/configure.tgt
+++ b/gdb/configure.tgt
@@ -291,6 +291,10 @@ i[34567]86-*-nto*)
gdb_target_obs="solib-svr4.o \
i386-nto-tdep.o nto-tdep.o"
;;
+i[34567]86-*-minerva*)
+ # Target: Minerva/i386
+ gdb_target_obs="i386-minerva-tdep.o minerva-tdep.o"
+ ;;
i[34567]86-*-solaris2* | x86_64-*-solaris2*)
# Target: Solaris x86_64
gdb_target_obs="${i386_tobjs} ${amd64_tobjs} \
diff --git a/gdb/i386-minerva-nat.c b/gdb/i386-minerva-nat.c
new file mode 100644
index 0000000..034252a
--- /dev/null
+++ b/gdb/i386-minerva-nat.c
@@ -0,0 +1,101 @@
+/* Native-dependent code for Minerva/i386.
+
+ Copyright (C) 2002-2021 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "gdbcore.h"
+#include "regcache.h"
+#include "regset.h"
+#include "target.h"
+
+#include <sys/arch/regs.h>
+#include <sys/ptrace.h>
+
+#include "i386-tdep.h"
+#include "minerva-nat.h"
+
+/* Register maps. */
+
+static const struct regcache_map_entry i386_minerva_gregmap[] =
+{
+ { 1, I386_EAX_REGNUM, 0 },
+ { 1, I386_ECX_REGNUM, 0 },
+ { 1, I386_EDX_REGNUM, 0 },
+ { 1, I386_EBX_REGNUM, 0 },
+ { 1, I386_ESP_REGNUM, 0 },
+ { 1, I386_EBP_REGNUM, 0 },
+ { 1, I386_ESI_REGNUM, 0 },
+ { 1, I386_EDI_REGNUM, 0 },
+ { 1, I386_EIP_REGNUM, 0 },
+ { 1, I386_EFLAGS_REGNUM, 0 },
+ { 1, I386_CS_REGNUM, 0 },
+ { 1, I386_SS_REGNUM, 0 },
+ { 1, I386_DS_REGNUM, 0 },
+ { 1, I386_ES_REGNUM, 0 },
+ { 1, I386_FS_REGNUM, 0 },
+ { 1, I386_GS_REGNUM, 0 },
+ { 0 },
+};
+
+const struct regset i386_minerva_gregset =
+{
+ i386_minerva_gregmap, regcache_supply_regset, regcache_collect_regset
+};
+
+class i386_minerva_nat_target final : public minerva_nat_target
+{
+ void fetch_registers (struct regcache* cache, int regnum) override
+ {
+ if (regnum == -1) {
+ pid_t pid = get_ptrace_pid (cache->ptid ());
+ PtraceRegisters regs;
+
+ if (ptrace (PT_GETREGS, pid, ®s, 0) == -1)
+ perror_with_name (_("Couldn't get registers"));
+
+ cache->supply_regset (&i386_minerva_gregset, regnum, ®s,
+ sizeof (regs));
+ }
+ };
+
+ void store_registers (struct regcache* cache, int regnum) override
+ {
+ if (regnum == -1) {
+ pid_t pid = get_ptrace_pid (cache->ptid ());
+ PtraceRegisters regs {};
+
+ if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1)
+ perror_with_name (_("Couldn't get registers"));
+
+ cache->collect_regset (&i386_minerva_gregset, regnum, ®s,
+ sizeof (regs));
+
+ if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) ®s, 0) == -1)
+ perror_with_name (_("Couldn't write registers"));
+ }
+ };
+};
+
+static i386_minerva_nat_target the_i386_minerva_nat_target;
+
+void _initialize_i386_minerva_nat ();
+void
+_initialize_i386_minerva_nat ()
+{
+ add_inf_child_target (&the_i386_minerva_nat_target);
+}
diff --git a/gdb/i386-minerva-tdep.c b/gdb/i386-minerva-tdep.c
new file mode 100644
index 0000000..d384061
--- /dev/null
+++ b/gdb/i386-minerva-tdep.c
@@ -0,0 +1,43 @@
+/* Target-dependent code for Minerva/i386.
+
+ Copyright (C) 2003-2021 Free Software Foundation, Inc.
+
+ This file is part of GDB.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "arch-utils.h"
+#include "gdbcore.h"
+#include "osabi.h"
+#include "regcache.h"
+
+#include "minerva-tdep.h"
+
+
+/* Implement the 'init_osabi' method of struct gdb_osabi_handler. */
+static void
+i386_minerva_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
+{
+ /* Generic Minerva support. */
+ minerva_init_abi (info, gdbarch);
+}
+
+void _initialize_i386_minerva_tdep ();
+void
+_initialize_i386_minerva_tdep ()
+{
+ gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_MINERVA,
+ i386_minerva_init_abi);
+}
diff --git a/gdb/i386-minerva-tdep.h b/gdb/i386-minerva-tdep.h
new file mode 100644
index 0000000..4ee3fba
--- /dev/null
+++ b/gdb/i386-minerva-tdep.h
@@ -0,0 +1,8 @@
+/* Target-dependent code for SernityOS i386. */
+
+#ifndef I386_MINERVA_TDEP_H
+#define I386_MINERVA_TDEP_H
+
+//extern const struct regset i386_minerva_gregset;
+
+#endif /* i386-minerva-tdep.h */
diff --git a/gdb/osabi.c b/gdb/osabi.c
index 40c86e7..2f4423c 100644
--- a/gdb/osabi.c
+++ b/gdb/osabi.c
@@ -82,6 +82,7 @@ static const struct osabi_names gdb_osabi_names[] =
{ "Newlib", NULL },
{ "SDE", NULL },
{ "PikeOS", NULL },
+ { "Minerva", NULL },
{ "<invalid>", NULL }
};
diff --git a/gdb/osabi.h b/gdb/osabi.h
index be01673..36471d8 100644
--- a/gdb/osabi.h
+++ b/gdb/osabi.h
@@ -46,6 +46,7 @@ enum gdb_osabi
GDB_OSABI_NEWLIB,
GDB_OSABI_SDE,
GDB_OSABI_PIKEOS,
+ GDB_OSABI_MINERVA,
GDB_OSABI_INVALID /* keep this last */
};
diff --git a/gdb/minerva-nat.c b/gdb/minerva-nat.c
new file mode 100644
index 0000000..ff740d4
--- /dev/null
+++ b/gdb/minerva-nat.c
@@ -0,0 +1,13 @@
+/* Native-dependent code for Minerva */
+
+#include "defs.h"
+#include "gdbthread.h"
+#include "inferior.h"
+#include "target.h"
+
+#include <sys/types.h>
+#include <sys/ptrace.h>
+#include "gdbsupport/gdb_wait.h"
+
+#include "inf-child.h"
+#include "minerva-nat.h"
\ No newline at end of file
diff --git a/gdb/minerva-nat.h b/gdb/minerva-nat.h
new file mode 100644
index 0000000..dcd24ce
--- /dev/null
+++ b/gdb/minerva-nat.h
@@ -0,0 +1,16 @@
+/* Native-dependent code for Minerva. */
+
+#ifndef MINERVA_NAT_H
+#define MINERVA_NAT_H
+
+#include "inf-ptrace.h"
+
+/* A prototype generic Minerva target.
+ A concrete instance should override it with local methods.
+*/
+
+class minerva_nat_target : public inf_ptrace_target
+{
+};
+
+#endif /* minerva-nat.h */
diff --git a/gdb/minerva-tdep.c b/gdb/minerva-tdep.c
new file mode 100644
index 0000000..45981b2
--- /dev/null
+++ b/gdb/minerva-tdep.c
@@ -0,0 +1,28 @@
+/* Target-dependent code for Minerva, architecture-independent.
+
+ Copyright (C) 2002-2021 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+
+#include "defs.h"
+#include "arch-utils.h"
+#include "gdbcore.h"
+
+#include "minerva-tdep.h"
+
+void minerva_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
+{
+ /* NOP for now */
+}
diff --git a/gdb/minerva-tdep.h b/gdb/minerva-tdep.h
new file mode 100644
index 0000000..43048c6
--- /dev/null
+++ b/gdb/minerva-tdep.h
@@ -0,0 +1,24 @@
+/* Target-dependent code for Minerva, architecture independent.
+
+ Copyright (C) 2009-2021 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+
+#ifndef MINERVA_TDEP_H
+#define MINERVA_TDEP_H
+
+extern void minerva_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch);
+
+#endif /* minerva-tdep.h */