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

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