public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: John Baldwin <jhb@FreeBSD.org>
To: gdb-patches@sourceware.org
Cc: Willgerodt, Felix <felix.willgerodt@intel.com>
Subject: [RFC PATCH v2 4/5] Support XSAVE layouts for the current host in the FreeBSD/amd64 target.
Date: Thu, 17 Mar 2022 11:36:02 -0700	[thread overview]
Message-ID: <20220317183603.34789-5-jhb@FreeBSD.org> (raw)
In-Reply-To: <20220317183603.34789-1-jhb@FreeBSD.org>

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

diff --git a/gdb/amd64-fbsd-nat.c b/gdb/amd64-fbsd-nat.c
index 98a1af03a66..9de5dee7a62 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;
 
@@ -54,7 +67,37 @@ class amd64_fbsd_nat_target final
 static amd64_fbsd_nat_target the_amd64_fbsd_nat_target;
 
 #ifdef PT_GETXSTATE_INFO
-static size_t xsave_len;
+static x86_xsave_layout xsave_layout;
+
+/* 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_LAYOUT:
+      if (xsave_layout.sizeof_xsave == 0)
+	return TARGET_XFER_E_IO;
+
+      if (offset > sizeof (xsave_layout))
+	return TARGET_XFER_E_IO;
+
+      if (offset + len > sizeof (xsave_layout))
+	len = sizeof (xsave_layout) - offset;
+
+      memcpy (readbuf, ((gdb_byte *) &xsave_layout) + 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
@@ -152,9 +195,9 @@ amd64_fbsd_nat_target::fetch_registers (struct regcache *regcache, int regnum)
      fetching the FPU/XSAVE state unnecessarily.  */
 
 #ifdef PT_GETXSTATE_INFO
-  if (xsave_len != 0)
+  if (xsave_layout.sizeof_xsave != 0)
     {
-      void *xstateregs = alloca (xsave_len);
+      void *xstateregs = alloca (xsave_layout.sizeof_xsave);
 
       if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
 	perror_with_name (_("Couldn't get extended state status"));
@@ -229,9 +272,9 @@ amd64_fbsd_nat_target::store_registers (struct regcache *regcache, int regnum)
      fetching the FPU/XSAVE state unnecessarily.  */
 
 #ifdef PT_GETXSTATE_INFO
-  if (xsave_len != 0)
+  if (xsave_layout.sizeof_xsave != 0)
     {
-      void *xstateregs = alloca (xsave_len);
+      void *xstateregs = alloca (xsave_layout.sizeof_xsave);
 
       if (ptrace (PT_GETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs, 0) == -1)
 	perror_with_name (_("Couldn't get extended state status"));
@@ -239,7 +282,7 @@ amd64_fbsd_nat_target::store_registers (struct regcache *regcache, int regnum)
       amd64_collect_xsave (regcache, regnum, xstateregs, 0);
 
       if (ptrace (PT_SETXSTATE, pid, (PTRACE_TYPE_ARG3) xstateregs,
-		  xsave_len) == -1)
+		  xsave_layout.sizeof_xsave) == -1)
 	perror_with_name (_("Couldn't write extended state status"));
       return;
     }
@@ -304,6 +347,37 @@ amd64fbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
 }
 \f
 
+#ifdef PT_GETXSTATE_INFO
+/* Fetch the offset of 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 0;
+
+  __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 (const struct ptrace_xstate_info &info)
+{
+  if (info.xsave_len == 0)
+    return;
+  xsave_layout.sizeof_xsave = info.xsave_len;
+  xsave_layout.avx_offset = xsave_leaf_offset(info.xsave_mask, 2);
+  xsave_layout.bndregs_offset = xsave_leaf_offset(info.xsave_mask, 3);
+  xsave_layout.bndcfg_offset = xsave_leaf_offset(info.xsave_mask, 4);
+  xsave_layout.avx512_k_offset = xsave_leaf_offset(info.xsave_mask, 5);
+  xsave_layout.avx512_zmm_h_offset = xsave_leaf_offset(info.xsave_mask, 6);
+  xsave_layout.avx512_zmm_offset = xsave_leaf_offset(info.xsave_mask, 7);
+  xsave_layout.pkru_offset = xsave_leaf_offset(info.xsave_mask, 9);
+}
+#endif
+
 /* Implement the read_description method.  */
 
 const struct target_desc *
@@ -328,13 +402,13 @@ amd64_fbsd_nat_target::read_description ()
       if (ptrace (PT_GETXSTATE_INFO, inferior_ptid.pid (),
 		  (PTRACE_TYPE_ARG3) &info, sizeof (info)) == 0)
 	{
-	  xsave_len = info.xsave_len;
 	  xcr0 = info.xsave_mask;
+	  probe_xsave_layout (info);
 	}
       xsave_probed = 1;
     }
 
-  if (xsave_len != 0)
+  if (xsave_layout.sizeof_xsave != 0)
     {
       if (is64)
 	return amd64_target_description (xcr0, true);
-- 
2.34.1


  parent reply	other threads:[~2022-03-17 18:37 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-17 18:35 [RFC PATCH v2 0/5] Handle variable XSAVE layouts John Baldwin
2022-03-17 18:35 ` [RFC PATCH v2 1/5] x86: Add an x86_xsave_layout structure to handle " John Baldwin
2022-03-17 18:36 ` [RFC PATCH v2 2/5] core: Support fetching TARGET_OBJECT_X86_XSAVE_LAYOUT from architectures John Baldwin
2022-03-28 11:28   ` George, Jini Susan
2022-03-17 18:36 ` [RFC PATCH v2 3/5] Update x86 FreeBSD architectures to support XSAVE layouts John Baldwin
2022-03-17 18:36 ` John Baldwin [this message]
2022-03-17 18:36 ` [RFC PATCH v2 5/5] x86: Use x86_xstate_layout to parse the XSAVE extended state area 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=20220317183603.34789-5-jhb@FreeBSD.org \
    --to=jhb@freebsd.org \
    --cc=felix.willgerodt@intel.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).