[v3,6/8] Windows gdb: Get available XState features

Message ID 20260829145823.1034821-6-ssbssa@yahoo.de
State New
Headers
Series [v3,1/8] gdb/testsuite: Add Windows replacement for aligned_alloc |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Test passed

Commit Message

Hannes Domani Aug. 29, 2026, 2:49 p.m. UTC
  Also prepares the thread context for the additional registers.
---
v2:
  - Remove PKRU from the implemented features mask
v3:
  - use throw_winerror_with_name instead of error
  - fix formatting and value of xstate_features
---
 gdb/nat/windows-nat.c | 57 ++++++++++++++++++++++++++++++++++++++++++-
 gdb/nat/windows-nat.h |  5 ++++
 2 files changed, 61 insertions(+), 1 deletion(-)
  

Comments

Tom Tromey Sept. 1, 2026, 5:38 p.m. UTC | #1
>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:

Hannes> Also prepares the thread context for the additional registers.
Hannes> ---
Hannes> v2:
Hannes>   - Remove PKRU from the implemented features mask
Hannes> v3:
Hannes>   - use throw_winerror_with_name instead of error
Hannes>   - fix formatting and value of xstate_features

Thanks.

Hannes> +      /* Available XState features masked with implemented features.  */
Hannes> +      xstate_features = (GetEnabledXStateFeatures ()
Hannes> +			 & X86_XSTATE_SSE_MASK);
Hannes> +      /* The extended XState functions are only needed if the available
Hannes> +	 features exceed SSE.  */
Hannes> +      if ((xstate_features & ~X86_XSTATE_SSE_MASK) == 0)
Hannes> +	xstate_features = 0;

Won't this condition always be true?
Resulting in xstate_features==0 always?

Tom
  
Hannes Domani Sept. 1, 2026, 5:44 p.m. UTC | #2
Am Dienstag, 1. September 2026 um 19:38:06 MESZ hat Tom Tromey <tom@tromey.com> Folgendes geschrieben:

> >>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:

> Hannes> Also prepares the thread context for the additional registers.
> Hannes> ---
> Hannes> v2:
> Hannes>  - Remove PKRU from the implemented features mask
> Hannes> v3:
> Hannes>  - use throw_winerror_with_name instead of error
> Hannes>  - fix formatting and value of xstate_features

> Thanks.

> Hannes> +      /* Available XState features masked with implemented features.  */
> Hannes> +      xstate_features = (GetEnabledXStateFeatures ()
> Hannes> +            & X86_XSTATE_SSE_MASK);
> Hannes> +      /* The extended XState functions are only needed if the available
> Hannes> +    features exceed SSE.  */
> Hannes> +      if ((xstate_features & ~X86_XSTATE_SSE_MASK) == 0)
> Hannes> +    xstate_features = 0;

> Won't this condition always be true?
> Resulting in xstate_features==0 always?

Yes, with this patch xstate_features is still 0.
The next patch then changes the 'and' of the first xstate_features to use
X86_XSTATE_AVX_MASK instead, so it will be possible to have other values.


Hannes
  
Tom Tromey Sept. 1, 2026, 5:51 p.m. UTC | #3
>>>>> "Hannes" == Hannes Domani <ssbssa@yahoo.de> writes:

Hannes> Yes, with this patch xstate_features is still 0.
Hannes> The next patch then changes the 'and' of the first xstate_features to use
Hannes> X86_XSTATE_AVX_MASK instead, so it will be possible to have other values.

Thanks.  Eventually I figured it out... normally I read the patches in
order; sometimes I look ahead but I didn't really think of it this time.

Anyway I think this patch is ok.
Approved-By: Tom Tromey <tom@tromey.com>

Tom
  

Patch

diff --git a/gdb/nat/windows-nat.c b/gdb/nat/windows-nat.c
index 8f91e755334..8930536f3ba 100644
--- a/gdb/nat/windows-nat.c
+++ b/gdb/nat/windows-nat.c
@@ -20,6 +20,7 @@ 
 #include "gdbsupport/common-debug.h"
 #include "gdbsupport/gdb_signals.h"
 #include "gdbsupport/gdb_wait.h"
+#include "gdbsupport/x86-xstate.h"
 #include "target/target.h"
 
 #undef GetModuleFileNameEx
@@ -84,6 +85,8 @@  RtlGetExtendedFeaturesMask_ftype *RtlGetExtendedFeaturesMask;
 RtlSetExtendedFeaturesMask_ftype *RtlSetExtendedFeaturesMask;
 RtlLocateExtendedFeature_ftype *RtlLocateExtendedFeature;
 #endif
+
+DWORD64 xstate_features;
 #endif
 
 /* Note that 'debug_events' must be locally defined in the relevant
@@ -99,12 +102,41 @@  windows_thread_info::windows_thread_info (windows_process_info *proc_,
     h (h_),
     thread_local_base (tlb)
 {
+#if defined __i386__ || defined __x86_64__
+  if (xstate_features != 0)
+    {
+      DWORD context_flags = proc->with_context (nullptr, [] (auto *context)
+	{
+	  return WindowsContext<decltype(context)>::all;
+	});
+      context_flags |= CONTEXT_XSTATE_FLAG;
+      DWORD xstate_size = 0;
+      InitializeContext (NULL, context_flags, NULL, &xstate_size);
+      context_buffer.reset (xmalloc (xstate_size));
+      CONTEXT *c = nullptr;
+      if (!InitializeContext (context_buffer.get (),
+			      context_flags, &c, &xstate_size))
+	{
+	  unsigned err = (unsigned) GetLastError ();
+	  throw_winerror_with_name (_("InitializeContext failed"), err);
+	}
 #ifdef __x86_64__
-  if (proc->wow64_process)
+      /* InitializeContext actually initializes a WOW64_CONTEXT when
+	 context_flags contains a WOW64_CONTEXT_* value, so a cast is needed.
+	 */
+      if (proc->wow64_process)
+	wow64_context = (WOW64_CONTEXT *) c;
+      else
+#endif
+	context = c;
+    }
+#ifdef __x86_64__
+  else if (proc->wow64_process)
     {
       context_buffer.reset (xmalloc (sizeof (WOW64_CONTEXT)));
       wow64_context = (WOW64_CONTEXT *) context_buffer.get ();
     }
+#endif
   else
 #endif
     {
@@ -1292,6 +1324,29 @@  initialize_loadable ()
 
 #undef GPA
 
+#if defined __i386__ || defined __x86_64__
+  if (GetEnabledXStateFeatures != nullptr
+      && InitializeContext != nullptr
+      && GetXStateFeaturesMask != nullptr
+      && SetXStateFeaturesMask != nullptr
+      && LocateXStateFeature != nullptr
+#ifdef __x86_64__
+      && RtlGetExtendedFeaturesMask != nullptr
+      && RtlSetExtendedFeaturesMask != nullptr
+      && RtlLocateExtendedFeature != nullptr
+#endif
+  )
+    {
+      /* Available XState features masked with implemented features.  */
+      xstate_features = (GetEnabledXStateFeatures ()
+			 & X86_XSTATE_SSE_MASK);
+      /* The extended XState functions are only needed if the available
+	 features exceed SSE.  */
+      if ((xstate_features & ~X86_XSTATE_SSE_MASK) == 0)
+	xstate_features = 0;
+    }
+#endif
+
   return result;
 }
 
diff --git a/gdb/nat/windows-nat.h b/gdb/nat/windows-nat.h
index 53023859b9a..f0a55d40f05 100644
--- a/gdb/nat/windows-nat.h
+++ b/gdb/nat/windows-nat.h
@@ -750,6 +750,11 @@  locate_xstate_feature (WOW64_CONTEXT *context, DWORD feature, DWORD *length)
 }
 #endif
 
+#if defined __i386__ || defined __x86_64__
+/* Available XState features.  */
+extern DWORD64 xstate_features;
+#endif
+
 /* This is available starting with Windows 10.  */
 #ifndef DBG_REPLY_LATER
 # define DBG_REPLY_LATER 0x40010001L