public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: John Baldwin <jhb@FreeBSD.org>
To: gdb-patches@sourceware.org
Subject: [RFC PATCH 3/4] Update x86 FreeBSD architectures to support XSAVE offsets.
Date: Wed, 16 Mar 2022 12:46:07 -0700	[thread overview]
Message-ID: <20220316194608.89528-4-jhb@FreeBSD.org> (raw)
In-Reply-To: <20220316194608.89528-1-jhb@FreeBSD.org>

Add an implementation of gdbarch_core_xfer_x86_xsave_offsets which
uses the size of an existing NT_X86_XSTATE core dump to determine
the offsets via i387_set_xsave_offsets.

The XSAVE register set is now also marked as variable size.  This is
not quite correct (and in particular, the maximum ("collect") size
still assumes the Intel layout.
---
 gdb/amd64-fbsd-tdep.c |  8 ++++++--
 gdb/i386-fbsd-tdep.c  | 33 +++++++++++++++++++++++++++++++--
 gdb/i386-fbsd-tdep.h  |  6 ++++++
 3 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/gdb/amd64-fbsd-tdep.c b/gdb/amd64-fbsd-tdep.c
index da5c297902d..19a86d40203 100644
--- a/gdb/amd64-fbsd-tdep.c
+++ b/gdb/amd64-fbsd-tdep.c
@@ -28,6 +28,7 @@
 
 #include "amd64-tdep.h"
 #include "amd64-fbsd-tdep.h"
+#include "i387-tdep.h"
 #include "fbsd-tdep.h"
 #include "solib-svr4.h"
 #include "inferior.h"
@@ -236,7 +237,8 @@ static const struct regset amd64fbsd_xstateregset =
   {
     NULL,
     amd64fbsd_supply_xstateregset,
-    amd64fbsd_collect_xstateregset
+    amd64fbsd_collect_xstateregset,
+    REGSET_VARIABLE_SIZE
   };
 
 /* Iterate over core file register note sections.  */
@@ -253,7 +255,7 @@ amd64fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
       &amd64_fbsd_gregset, NULL, cb_data);
   cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, &amd64_fpregset,
       NULL, cb_data);
-  cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0), X86_XSTATE_SIZE (tdep->xcr0),
+  cb (".reg-xstate", X86_XSTATE_SSE_SIZE, X86_XSTATE_SIZE (tdep->xcr0),
       &amd64fbsd_xstateregset, "XSAVE extended state", cb_data);
 }
 
@@ -295,6 +297,8 @@ amd64fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   tramp_frame_prepend_unwinder (gdbarch, &amd64_fbsd_sigframe);
 
   tdep->xsave_xcr0_offset = I386_FBSD_XSAVE_XCR0_OFFSET;
+  set_gdbarch_core_xfer_x86_xsave_offsets
+    (gdbarch, i386fbsd_core_xfer_x86_xsave_offsets);
 
   /* Iterate over core file register note sections.  */
   set_gdbarch_iterate_over_regset_sections
diff --git a/gdb/i386-fbsd-tdep.c b/gdb/i386-fbsd-tdep.c
index 16ffd576323..577eed06321 100644
--- a/gdb/i386-fbsd-tdep.c
+++ b/gdb/i386-fbsd-tdep.c
@@ -18,6 +18,7 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
+#include "gdbcore.h"
 #include "osabi.h"
 #include "regcache.h"
 #include "regset.h"
@@ -263,6 +264,31 @@ i386fbsd_core_read_xcr0 (bfd *abfd)
   return xcr0;
 }
 
+/* Implement the core_xfer_x86_xsave_offsets gdbarch method.  */
+
+LONGEST
+i386fbsd_core_xfer_x86_xsave_offsets (struct gdbarch *gdbarch,
+				      gdb_byte *readbuf, ULONGEST offset,
+				      ULONGEST len)
+{
+  asection *xstate = bfd_get_section_by_name (core_bfd, ".reg-xstate");
+  if (xstate == nullptr)
+    return -1;
+
+  if (offset > sizeof (struct xsave_offsets))
+    return -1;
+
+  i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
+  struct xsave_offsets xsave_offsets;
+  i387_set_xsave_offsets (tdep->xcr0, bfd_section_size (xstate), &xsave_offsets);
+
+  if (offset + len > sizeof (struct xsave_offsets))
+    len = sizeof (struct xsave_offsets) - offset;
+
+  memcpy (readbuf, ((gdb_byte *) &xsave_offsets) + offset, len);
+  return len;
+}
+
 /* Implement the core_read_description gdbarch method.  */
 
 static const struct target_desc *
@@ -299,7 +325,8 @@ static const struct regset i386fbsd_xstateregset =
   {
     NULL,
     i386fbsd_supply_xstateregset,
-    i386fbsd_collect_xstateregset
+    i386fbsd_collect_xstateregset,
+    REGSET_VARIABLE_SIZE
   };
 
 /* Iterate over core file register note sections.  */
@@ -318,7 +345,7 @@ i386fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
       NULL, cb_data);
 
   if (tdep->xcr0 & X86_XSTATE_AVX)
-    cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0),
+    cb (".reg-xstate", X86_XSTATE_SSE_SIZE,
 	X86_XSTATE_SIZE (tdep->xcr0), &i386fbsd_xstateregset,
 	"XSAVE extended state", cb_data);
 }
@@ -372,6 +399,8 @@ i386fbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
   i386_elf_init_abi (info, gdbarch);
 
   tdep->xsave_xcr0_offset = I386_FBSD_XSAVE_XCR0_OFFSET;
+  set_gdbarch_core_xfer_x86_xsave_offsets
+    (gdbarch, i386fbsd_core_xfer_x86_xsave_offsets);
 
   /* Iterate over core file register note sections.  */
   set_gdbarch_iterate_over_regset_sections
diff --git a/gdb/i386-fbsd-tdep.h b/gdb/i386-fbsd-tdep.h
index 76f4c20f657..ed8e8285f88 100644
--- a/gdb/i386-fbsd-tdep.h
+++ b/gdb/i386-fbsd-tdep.h
@@ -25,6 +25,12 @@
 /* Get XSAVE extended state xcr0 from core dump.  */
 extern uint64_t i386fbsd_core_read_xcr0 (bfd *abfd);
 
+/* Implement the core_xfer_x86_xsave_offsets gdbarch method.  */
+extern LONGEST i386fbsd_core_xfer_x86_xsave_offsets (struct gdbarch *gdbarch,
+						     gdb_byte *readbuf,
+						     ULONGEST offset,
+						     ULONGEST len);
+
 /* The format of the XSAVE extended area is determined by hardware.
    Cores store the XSAVE extended area in a NT_X86_XSTATE note that
    matches the layout on Linux.  */
-- 
2.34.1


  parent reply	other threads:[~2022-03-16 19:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-16 19:46 [RFC PATCH 0/4] Handle variable XSAVE layouts John Baldwin
2022-03-16 19:46 ` [RFC PATCH 1/4] x86: Add an xsave_offsets structure to handle " John Baldwin
2022-03-16 19:46 ` [RFC PATCH 2/4] core: Support fetching TARGET_OBJECT_X86_XSAVE_OFFSETS from architectures John Baldwin
2022-03-16 19:46 ` John Baldwin [this message]
2022-03-16 19:46 ` [RFC PATCH 4/4] Support XSAVE layouts for the current host in the FreeBSD/amd64 target John Baldwin
2022-03-17 13:17 ` [RFC PATCH 0/4] Handle variable XSAVE layouts Willgerodt, Felix
2022-03-17 16:20   ` John Baldwin
2022-03-17 18:03     ` John Baldwin
2022-03-18 13:49       ` Willgerodt, Felix
2022-03-18 17:27         ` John Baldwin

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=20220316194608.89528-4-jhb@FreeBSD.org \
    --to=jhb@freebsd.org \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).