Blame SOURCES/gdb-rhbz2019936-gdbserver-avx512-m32.patch

42b06b
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
42b06b
From: Tom de Vries <tdevries@suse.de>
42b06b
Date: Mon, 6 Dec 2021 15:39:03 -0500
42b06b
Subject: gdb-rhbz2019936-gdbserver-avx512-m32.patch
42b06b
42b06b
;; Backport "Fix avx512 -m32 support in gdbserver"
42b06b
;; (Tom de Vries, RHBZ 2019936)
42b06b
42b06b
    PR27257 reports a problem that can be reproduced as follows:
42b06b
    - use x86_64 machine with avx512 support
42b06b
    - compile a hello world with -m32 to a.out
42b06b
    - start a gdbserver session with a.out
42b06b
    - use gdb to connect to the gdbserver session
42b06b
42b06b
    This makes us run into:
42b06b
    ...
42b06b
    Listening on port 2346
42b06b
    Remote debugging from host ::1, port 34940
42b06b
    src/gdbserver/regcache.cc:257: \
42b06b
      A problem internal to GDBserver has been detected.
42b06b
    Unknown register zmm16h requested
42b06b
    ...
42b06b
42b06b
    The problem is that i387_xsave_to_cache in gdbserver/i387-fp.cc can't find a
42b06b
    register zmm16h in the register cache.
42b06b
42b06b
    To understand how this happens, first some background.
42b06b
42b06b
    SSE has 16 128-bit wide xmm registers.
42b06b
42b06b
    AVX extends the SSE registers set as follows:
42b06b
    - it extends the 16 existing 128-bit wide xmm registers to 256-bit wide ymm
42b06b
      registers.
42b06b
42b06b
    AVX512 extends the AVX register set as follows:
42b06b
    - it extends the 16 existing 256-bit wide ymm registers to 512-bit wide zmm
42b06b
      registers.
42b06b
    - it adds 16 additional 512-bit wide zmm registers (with corresponding ymm and
42b06b
      xmm subregisters added as well)
42b06b
42b06b
    However, in 32-bit mode, there are only 8 xmm/ymm/zmm registers.
42b06b
42b06b
    The problem we're running into is that gdbserver/i387-fp.cc uses these
42b06b
    constants to describe the size of the register file:
42b06b
    ...
42b06b
    static const int num_avx512_zmmh_low_registers = 16;
42b06b
    static const int num_avx512_zmmh_high_registers = 16;
42b06b
    static const int num_avx512_ymmh_registers = 16;
42b06b
    static const int num_avx512_xmm_registers = 16;
42b06b
    ...
42b06b
    which are all incorrect for the 32-bit case.
42b06b
42b06b
    Fix this by replacing the constants with variables that have the appropriate
42b06b
    values in 64-bit and 32-bit mode.
42b06b
42b06b
    Tested on x86_64-linux with native and unix/-m32.
42b06b
42b06b
diff --git a/gdbserver/i387-fp.cc b/gdbserver/i387-fp.cc
42b06b
--- a/gdbserver/i387-fp.cc
42b06b
+++ b/gdbserver/i387-fp.cc
42b06b
@@ -23,10 +23,6 @@
42b06b
 static const int num_mpx_bnd_registers = 4;
42b06b
 static const int num_mpx_cfg_registers = 2;
42b06b
 static const int num_avx512_k_registers = 8;
42b06b
-static const int num_avx512_zmmh_low_registers = 16;
42b06b
-static const int num_avx512_zmmh_high_registers = 16;
42b06b
-static const int num_avx512_ymmh_registers = 16;
42b06b
-static const int num_avx512_xmm_registers = 16;
42b06b
 static const int num_pkeys_registers = 1;
42b06b
 
42b06b
 /* Note: These functions preserve the reserved bits in control registers.
42b06b
@@ -256,14 +252,22 @@ void
42b06b
 i387_cache_to_xsave (struct regcache *regcache, void *buf)
42b06b
 {
42b06b
   struct i387_xsave *fp = (struct i387_xsave *) buf;
42b06b
+  bool amd64 = register_size (regcache->tdesc, 0) == 8;
42b06b
   int i;
42b06b
   unsigned long val, val2;
42b06b
   unsigned long long xstate_bv = 0;
42b06b
   unsigned long long clear_bv = 0;
42b06b
   char raw[64];
42b06b
   char *p;
42b06b
+
42b06b
   /* Amd64 has 16 xmm regs; I386 has 8 xmm regs.  */
42b06b
-  int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8;
42b06b
+  int num_xmm_registers = amd64 ? 16 : 8;
42b06b
+  /* AVX512 extends the existing xmm/ymm registers to a wider mode: zmm.  */
42b06b
+  int num_avx512_zmmh_low_registers = num_xmm_registers;
42b06b
+  /* AVX512 adds 16 extra regs in Amd64 mode, but none in I386 mode.*/
42b06b
+  int num_avx512_zmmh_high_registers = amd64 ? 16 : 0;
42b06b
+  int num_avx512_ymmh_registers = amd64 ? 16 : 0;
42b06b
+  int num_avx512_xmm_registers = amd64 ? 16 : 0;
42b06b
 
42b06b
   /* The supported bits in `xstat_bv' are 8 bytes.  Clear part in
42b06b
      vector registers if its bit in xstat_bv is zero.  */
42b06b
@@ -452,7 +456,9 @@ i387_cache_to_xsave (struct regcache *regcache, void *buf)
42b06b
   /* Check if any of ZMM16H-ZMM31H registers are changed.  */
42b06b
   if ((x86_xcr0 & X86_XSTATE_ZMM))
42b06b
     {
42b06b
-      int zmm16h_regnum = find_regno (regcache->tdesc, "zmm16h");
42b06b
+      int zmm16h_regnum = (num_avx512_zmmh_high_registers == 0
42b06b
+			   ? -1
42b06b
+			   : find_regno (regcache->tdesc, "zmm16h"));
42b06b
 
42b06b
       for (i = 0; i < num_avx512_zmmh_high_registers; i++)
42b06b
 	{
42b06b
@@ -469,7 +475,9 @@ i387_cache_to_xsave (struct regcache *regcache, void *buf)
42b06b
   /* Check if any XMM_AVX512 registers are changed.  */
42b06b
   if ((x86_xcr0 & X86_XSTATE_ZMM))
42b06b
     {
42b06b
-      int xmm_avx512_regnum = find_regno (regcache->tdesc, "xmm16");
42b06b
+      int xmm_avx512_regnum = (num_avx512_xmm_registers == 0
42b06b
+			       ? -1
42b06b
+			       : find_regno (regcache->tdesc, "xmm16"));
42b06b
 
42b06b
       for (i = 0; i < num_avx512_xmm_registers; i++)
42b06b
 	{
42b06b
@@ -486,7 +494,9 @@ i387_cache_to_xsave (struct regcache *regcache, void *buf)
42b06b
   /* Check if any YMMH_AVX512 registers are changed.  */
42b06b
   if ((x86_xcr0 & X86_XSTATE_ZMM))
42b06b
     {
42b06b
-      int ymmh_avx512_regnum = find_regno (regcache->tdesc, "ymm16h");
42b06b
+      int ymmh_avx512_regnum = (num_avx512_ymmh_registers == 0
42b06b
+				? -1
42b06b
+				: find_regno (regcache->tdesc, "ymm16h"));
42b06b
 
42b06b
       for (i = 0; i < num_avx512_ymmh_registers; i++)
42b06b
 	{
42b06b
@@ -710,12 +720,20 @@ i387_xsave_to_cache (struct regcache *regcache, const void *buf)
42b06b
 {
42b06b
   struct i387_xsave *fp = (struct i387_xsave *) buf;
42b06b
   struct i387_fxsave *fxp = (struct i387_fxsave *) buf;
42b06b
+  bool amd64 = register_size (regcache->tdesc, 0) == 8;
42b06b
   int i, top;
42b06b
   unsigned long val;
42b06b
   unsigned long long clear_bv;
42b06b
   gdb_byte *p;
42b06b
-  /* Amd64 has 16 xmm regs; I386 has 8 xmm regs.  */
42b06b
-  int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8;
42b06b
+
42b06b
+   /* Amd64 has 16 xmm regs; I386 has 8 xmm regs.  */
42b06b
+  int num_xmm_registers = amd64 ? 16 : 8;
42b06b
+  /* AVX512 extends the existing xmm/ymm registers to a wider mode: zmm.  */
42b06b
+  int num_avx512_zmmh_low_registers = num_xmm_registers;
42b06b
+  /* AVX512 adds 16 extra regs in Amd64 mode, but none in I386 mode.*/
42b06b
+  int num_avx512_zmmh_high_registers = amd64 ? 16 : 0;
42b06b
+  int num_avx512_ymmh_registers = amd64 ? 16 : 0;
42b06b
+  int num_avx512_xmm_registers = amd64 ? 16 : 0;
42b06b
 
42b06b
   /* The supported bits in `xstat_bv' are 8 bytes.  Clear part in
42b06b
      vector registers if its bit in xstat_bv is zero.  */
42b06b
@@ -845,9 +863,15 @@ i387_xsave_to_cache (struct regcache *regcache, const void *buf)
42b06b
 
42b06b
   if ((x86_xcr0 & X86_XSTATE_ZMM) != 0)
42b06b
     {
42b06b
-      int zmm16h_regnum = find_regno (regcache->tdesc, "zmm16h");
42b06b
-      int ymm16h_regnum = find_regno (regcache->tdesc, "ymm16h");
42b06b
-      int xmm16_regnum = find_regno (regcache->tdesc, "xmm16");
42b06b
+      int zmm16h_regnum = (num_avx512_zmmh_high_registers == 0
42b06b
+			   ? -1
42b06b
+			   : find_regno (regcache->tdesc, "zmm16h"));
42b06b
+      int ymm16h_regnum = (num_avx512_ymmh_registers == 0
42b06b
+			   ? -1
42b06b
+			   : find_regno (regcache->tdesc, "ymm16h"));
42b06b
+      int xmm16_regnum = (num_avx512_xmm_registers == 0
42b06b
+			  ? -1
42b06b
+			  : find_regno (regcache->tdesc, "xmm16"));
42b06b
 
42b06b
       if ((clear_bv & X86_XSTATE_ZMM) != 0)
42b06b
 	{