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 4/4] Support XSAVE layouts for the current host in the FreeBSD/amd64 target.
Date: Wed, 16 Mar 2022 12:46:08 -0700	[thread overview]
Message-ID: <20220316194608.89528-5-jhb@FreeBSD.org> (raw)
In-Reply-To: <20220316194608.89528-1-jhb@FreeBSD.org>

Use the CPUID instruction to fetch the offsets of supported state
components.
---
 gdb/amd64-fbsd-nat.c | 73 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)

diff --git a/gdb/amd64-fbsd-nat.c b/gdb/amd64-fbsd-nat.c
index 98a1af03a66..f749a022611 100644
--- a/gdb/amd64-fbsd-nat.c
+++ b/gdb/amd64-fbsd-nat.c
@@ -28,11 +28,15 @@
 #include <sys/sysctl.h>
 #include <sys/user.h>
 #include <machine/reg.h>
+#ifdef PT_GETXSTATE_INFO
+#include <cpuid.h>
+#endif
 
 #include "fbsd-nat.h"
 #include "amd64-tdep.h"
 #include "amd64-fbsd-tdep.h"
 #include "amd64-nat.h"
+#include "i387-tdep.h"
 #include "x86-nat.h"
 #include "gdbsupport/x86-xstate.h"
 #include "x86-bsd-nat.h"
@@ -41,6 +45,15 @@ class amd64_fbsd_nat_target final
   : public x86bsd_nat_target<fbsd_nat_target>
 {
 public:
+#ifdef PT_GETXSTATE_INFO
+  enum target_xfer_status xfer_partial (enum target_object object,
+					const char *annex,
+					gdb_byte *readbuf,
+					const gdb_byte *writebuf,
+					ULONGEST offset, ULONGEST len,
+					ULONGEST *xfered_len) override;
+#endif
+
   void fetch_registers (struct regcache *, int) override;
   void store_registers (struct regcache *, int) override;
 
@@ -55,6 +68,37 @@ static amd64_fbsd_nat_target the_amd64_fbsd_nat_target;
 
 #ifdef PT_GETXSTATE_INFO
 static size_t xsave_len;
+static struct xsave_offsets xsave_offsets;
+
+/* Implement the "xfer_partial" target_ops method.  */
+
+enum target_xfer_status
+amd64_fbsd_nat_target::xfer_partial (enum target_object object,
+				     const char *annex, gdb_byte *readbuf,
+				     const gdb_byte *writebuf,
+				     ULONGEST offset, ULONGEST len,
+				     ULONGEST *xfered_len)
+{
+  switch (object)
+    {
+    case TARGET_OBJECT_X86_XSAVE_OFFSETS:
+      if (xsave_len == 0)
+	return TARGET_XFER_E_IO;
+
+      if (offset > sizeof (xsave_offsets))
+	return TARGET_XFER_E_IO;
+
+      if (offset + len > sizeof (xsave_offsets))
+	len = sizeof (xsave_offsets) - offset;
+
+      memcpy (readbuf, ((gdb_byte *) &xsave_offsets) + offset, len);
+      *xfered_len = len;
+      return len == 0 ? TARGET_XFER_EOF : TARGET_XFER_OK;
+    default:
+      return fbsd_nat_target::xfer_partial (object, annex, readbuf, writebuf,
+					    offset, len, xfered_len);
+    }
+}
 #endif
 
 /* This is a layout of the amd64 'struct reg' but with i386
@@ -304,6 +348,34 @@ amd64fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
 }
 \f
 
+#ifdef PT_GETXSTATE_INFO
+/* Fetch the offset a specific XSAVE extended region.  */
+static int
+xsave_leaf_offset (uint64_t xcr0, int leaf)
+{
+  uint32_t eax, ebx, ecx, edx;
+
+  if ((xcr0 & (1ULL << leaf)) == 0)
+    return -1;
+
+  __cpuid_count(0xd, leaf, eax, ebx, ecx, edx);
+  return ebx;
+}
+
+/* Fetch the offsets of the XSAVE extended regions on the running host.  */
+static void
+probe_xsave_layout (uint64_t xcr0)
+{
+  xsave_offsets.avx_offset = xsave_leaf_offset(xcr0, 2);
+  xsave_offsets.bndregs_offset = xsave_leaf_offset(xcr0, 3);
+  xsave_offsets.bndcfg_offset = xsave_leaf_offset(xcr0, 4);
+  xsave_offsets.avx512_k_offset = xsave_leaf_offset(xcr0, 5);
+  xsave_offsets.avx512_zmm_h_offset = xsave_leaf_offset(xcr0, 6);
+  xsave_offsets.avx512_zmm_offset = xsave_leaf_offset(xcr0, 7);
+  xsave_offsets.pkru_offset = xsave_leaf_offset(xcr0, 9);
+}
+#endif
+
 /* Implement the read_description method.  */
 
 const struct target_desc *
@@ -330,6 +402,7 @@ amd64_fbsd_nat_target::read_description ()
 	{
 	  xsave_len = info.xsave_len;
 	  xcr0 = info.xsave_mask;
+	  probe_xsave_layout (xcr0);
 	}
       xsave_probed = 1;
     }
-- 
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 ` [RFC PATCH 3/4] Update x86 FreeBSD architectures to support XSAVE offsets John Baldwin
2022-03-16 19:46 ` John Baldwin [this message]
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-5-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).