Home | History | Annotate | Line # | Download | only in gdb
      1   1.1  christos /* Intel 387 floating point stuff.
      2   1.1  christos 
      3  1.11  christos    Copyright (C) 1988-2024 Free Software Foundation, Inc.
      4   1.1  christos 
      5   1.1  christos    This file is part of GDB.
      6   1.1  christos 
      7   1.1  christos    This program is free software; you can redistribute it and/or modify
      8   1.1  christos    it under the terms of the GNU General Public License as published by
      9   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10   1.1  christos    (at your option) any later version.
     11   1.1  christos 
     12   1.1  christos    This program is distributed in the hope that it will be useful,
     13   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   1.1  christos    GNU General Public License for more details.
     16   1.1  christos 
     17   1.1  christos    You should have received a copy of the GNU General Public License
     18   1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19   1.1  christos 
     20  1.11  christos #include "extract-store-integer.h"
     21   1.1  christos #include "frame.h"
     22   1.1  christos #include "gdbcore.h"
     23   1.1  christos #include "inferior.h"
     24   1.1  christos #include "language.h"
     25   1.1  christos #include "regcache.h"
     26   1.8  christos #include "target-float.h"
     27   1.1  christos #include "value.h"
     28   1.1  christos 
     29   1.1  christos #include "i386-tdep.h"
     30   1.1  christos #include "i387-tdep.h"
     31   1.9  christos #include "gdbsupport/x86-xstate.h"
     32   1.1  christos 
     33   1.1  christos /* Print the floating point number specified by RAW.  */
     34   1.1  christos 
     35   1.1  christos static void
     36   1.1  christos print_i387_value (struct gdbarch *gdbarch,
     37   1.1  christos 		  const gdb_byte *raw, struct ui_file *file)
     38   1.1  christos {
     39   1.1  christos   /* We try to print 19 digits.  The last digit may or may not contain
     40   1.1  christos      garbage, but we'd better print one too many.  We need enough room
     41   1.1  christos      to print the value, 1 position for the sign, 1 for the decimal
     42   1.1  christos      point, 19 for the digits and 6 for the exponent adds up to 27.  */
     43   1.8  christos   const struct type *type = i387_ext_type (gdbarch);
     44   1.8  christos   std::string str = target_float_to_string (raw, type, " %-+27.19g");
     45  1.10  christos   gdb_printf (file, "%s", str.c_str ());
     46   1.1  christos }
     47   1.1  christos 
     48   1.1  christos /* Print the classification for the register contents RAW.  */
     49   1.1  christos 
     50   1.1  christos static void
     51   1.1  christos print_i387_ext (struct gdbarch *gdbarch,
     52   1.1  christos 		const gdb_byte *raw, struct ui_file *file)
     53   1.1  christos {
     54   1.1  christos   int sign;
     55   1.1  christos   int integer;
     56   1.1  christos   unsigned int exponent;
     57   1.1  christos   unsigned long fraction[2];
     58   1.1  christos 
     59   1.1  christos   sign = raw[9] & 0x80;
     60   1.1  christos   integer = raw[7] & 0x80;
     61   1.1  christos   exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
     62   1.1  christos   fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
     63   1.1  christos   fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
     64   1.1  christos 		 | (raw[5] << 8) | raw[4]);
     65   1.1  christos 
     66   1.1  christos   if (exponent == 0x7fff && integer)
     67   1.1  christos     {
     68   1.1  christos       if (fraction[0] == 0x00000000 && fraction[1] == 0x00000000)
     69   1.1  christos 	/* Infinity.  */
     70  1.10  christos 	gdb_printf (file, " %cInf", (sign ? '-' : '+'));
     71   1.1  christos       else if (sign && fraction[0] == 0x00000000 && fraction[1] == 0x40000000)
     72   1.1  christos 	/* Real Indefinite (QNaN).  */
     73  1.10  christos 	gdb_puts (" Real Indefinite (QNaN)", file);
     74   1.1  christos       else if (fraction[1] & 0x40000000)
     75   1.1  christos 	/* QNaN.  */
     76  1.10  christos 	gdb_puts (" QNaN", file);
     77   1.1  christos       else
     78   1.1  christos 	/* SNaN.  */
     79  1.10  christos 	gdb_puts (" SNaN", file);
     80   1.1  christos     }
     81   1.1  christos   else if (exponent < 0x7fff && exponent > 0x0000 && integer)
     82   1.1  christos     /* Normal.  */
     83   1.1  christos     print_i387_value (gdbarch, raw, file);
     84   1.1  christos   else if (exponent == 0x0000)
     85   1.1  christos     {
     86   1.1  christos       /* Denormal or zero.  */
     87   1.1  christos       print_i387_value (gdbarch, raw, file);
     88   1.1  christos 
     89   1.1  christos       if (integer)
     90   1.1  christos 	/* Pseudo-denormal.  */
     91  1.10  christos 	gdb_puts (" Pseudo-denormal", file);
     92   1.1  christos       else if (fraction[0] || fraction[1])
     93   1.1  christos 	/* Denormal.  */
     94  1.10  christos 	gdb_puts (" Denormal", file);
     95   1.1  christos     }
     96   1.1  christos   else
     97   1.1  christos     /* Unsupported.  */
     98  1.10  christos     gdb_puts (" Unsupported", file);
     99   1.1  christos }
    100   1.1  christos 
    101   1.1  christos /* Print the status word STATUS.  If STATUS_P is false, then STATUS
    102   1.1  christos    was unavailable.  */
    103   1.1  christos 
    104   1.1  christos static void
    105   1.1  christos print_i387_status_word (int status_p,
    106   1.1  christos 			unsigned int status, struct ui_file *file)
    107   1.1  christos {
    108  1.10  christos   gdb_printf (file, "Status Word:         ");
    109   1.1  christos   if (!status_p)
    110   1.1  christos     {
    111  1.10  christos       gdb_printf (file, "%s\n", _("<unavailable>"));
    112   1.1  christos       return;
    113   1.1  christos     }
    114   1.1  christos 
    115  1.10  christos   gdb_printf (file, "%s", hex_string_custom (status, 4));
    116  1.10  christos   gdb_puts ("  ", file);
    117  1.10  christos   gdb_printf (file, " %s", (status & 0x0001) ? "IE" : "  ");
    118  1.10  christos   gdb_printf (file, " %s", (status & 0x0002) ? "DE" : "  ");
    119  1.10  christos   gdb_printf (file, " %s", (status & 0x0004) ? "ZE" : "  ");
    120  1.10  christos   gdb_printf (file, " %s", (status & 0x0008) ? "OE" : "  ");
    121  1.10  christos   gdb_printf (file, " %s", (status & 0x0010) ? "UE" : "  ");
    122  1.10  christos   gdb_printf (file, " %s", (status & 0x0020) ? "PE" : "  ");
    123  1.10  christos   gdb_puts ("  ", file);
    124  1.10  christos   gdb_printf (file, " %s", (status & 0x0080) ? "ES" : "  ");
    125  1.10  christos   gdb_puts ("  ", file);
    126  1.10  christos   gdb_printf (file, " %s", (status & 0x0040) ? "SF" : "  ");
    127  1.10  christos   gdb_puts ("  ", file);
    128  1.10  christos   gdb_printf (file, " %s", (status & 0x0100) ? "C0" : "  ");
    129  1.10  christos   gdb_printf (file, " %s", (status & 0x0200) ? "C1" : "  ");
    130  1.10  christos   gdb_printf (file, " %s", (status & 0x0400) ? "C2" : "  ");
    131  1.10  christos   gdb_printf (file, " %s", (status & 0x4000) ? "C3" : "  ");
    132   1.1  christos 
    133  1.10  christos   gdb_puts ("\n", file);
    134   1.1  christos 
    135  1.10  christos   gdb_printf (file,
    136  1.10  christos 	      "                       TOP: %d\n", ((status >> 11) & 7));
    137   1.1  christos }
    138   1.1  christos 
    139   1.1  christos /* Print the control word CONTROL.  If CONTROL_P is false, then
    140   1.1  christos    CONTROL was unavailable.  */
    141   1.1  christos 
    142   1.1  christos static void
    143   1.1  christos print_i387_control_word (int control_p,
    144   1.1  christos 			 unsigned int control, struct ui_file *file)
    145   1.1  christos {
    146  1.10  christos   gdb_printf (file, "Control Word:        ");
    147   1.1  christos   if (!control_p)
    148   1.1  christos     {
    149  1.10  christos       gdb_printf (file, "%s\n", _("<unavailable>"));
    150   1.1  christos       return;
    151   1.1  christos     }
    152   1.1  christos 
    153  1.10  christos   gdb_printf (file, "%s", hex_string_custom (control, 4));
    154  1.10  christos   gdb_puts ("  ", file);
    155  1.10  christos   gdb_printf (file, " %s", (control & 0x0001) ? "IM" : "  ");
    156  1.10  christos   gdb_printf (file, " %s", (control & 0x0002) ? "DM" : "  ");
    157  1.10  christos   gdb_printf (file, " %s", (control & 0x0004) ? "ZM" : "  ");
    158  1.10  christos   gdb_printf (file, " %s", (control & 0x0008) ? "OM" : "  ");
    159  1.10  christos   gdb_printf (file, " %s", (control & 0x0010) ? "UM" : "  ");
    160  1.10  christos   gdb_printf (file, " %s", (control & 0x0020) ? "PM" : "  ");
    161   1.1  christos 
    162  1.10  christos   gdb_puts ("\n", file);
    163   1.1  christos 
    164  1.10  christos   gdb_puts ("                       PC: ", file);
    165   1.1  christos   switch ((control >> 8) & 3)
    166   1.1  christos     {
    167   1.1  christos     case 0:
    168  1.10  christos       gdb_puts ("Single Precision (24-bits)\n", file);
    169   1.1  christos       break;
    170   1.1  christos     case 1:
    171  1.10  christos       gdb_puts ("Reserved\n", file);
    172   1.1  christos       break;
    173   1.1  christos     case 2:
    174  1.10  christos       gdb_puts ("Double Precision (53-bits)\n", file);
    175   1.1  christos       break;
    176   1.1  christos     case 3:
    177  1.10  christos       gdb_puts ("Extended Precision (64-bits)\n", file);
    178   1.1  christos       break;
    179   1.1  christos     }
    180   1.1  christos 
    181  1.10  christos   gdb_puts ("                       RC: ", file);
    182   1.1  christos   switch ((control >> 10) & 3)
    183   1.1  christos     {
    184   1.1  christos     case 0:
    185  1.10  christos       gdb_puts ("Round to nearest\n", file);
    186   1.1  christos       break;
    187   1.1  christos     case 1:
    188  1.10  christos       gdb_puts ("Round down\n", file);
    189   1.1  christos       break;
    190   1.1  christos     case 2:
    191  1.10  christos       gdb_puts ("Round up\n", file);
    192   1.1  christos       break;
    193   1.1  christos     case 3:
    194  1.10  christos       gdb_puts ("Round toward zero\n", file);
    195   1.1  christos       break;
    196   1.1  christos     }
    197   1.1  christos }
    198   1.1  christos 
    199   1.1  christos /* Print out the i387 floating point state.  Note that we ignore FRAME
    200   1.1  christos    in the code below.  That's OK since floating-point registers are
    201   1.1  christos    never saved on the stack.  */
    202   1.1  christos 
    203   1.1  christos void
    204   1.1  christos i387_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
    205  1.11  christos 		       const frame_info_ptr &frame, const char *args)
    206   1.1  christos {
    207  1.10  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
    208   1.1  christos   ULONGEST fctrl;
    209   1.1  christos   int fctrl_p;
    210   1.1  christos   ULONGEST fstat;
    211   1.1  christos   int fstat_p;
    212   1.1  christos   ULONGEST ftag;
    213   1.1  christos   int ftag_p;
    214   1.1  christos   ULONGEST fiseg;
    215   1.1  christos   int fiseg_p;
    216   1.1  christos   ULONGEST fioff;
    217   1.1  christos   int fioff_p;
    218   1.1  christos   ULONGEST foseg;
    219   1.1  christos   int foseg_p;
    220   1.1  christos   ULONGEST fooff;
    221   1.1  christos   int fooff_p;
    222   1.1  christos   ULONGEST fop;
    223   1.1  christos   int fop_p;
    224   1.1  christos   int fpreg;
    225   1.1  christos   int top;
    226   1.1  christos 
    227   1.1  christos   gdb_assert (gdbarch == get_frame_arch (frame));
    228   1.1  christos 
    229   1.1  christos   fctrl_p = read_frame_register_unsigned (frame,
    230   1.1  christos 					  I387_FCTRL_REGNUM (tdep), &fctrl);
    231   1.1  christos   fstat_p = read_frame_register_unsigned (frame,
    232   1.1  christos 					  I387_FSTAT_REGNUM (tdep), &fstat);
    233   1.1  christos   ftag_p = read_frame_register_unsigned (frame,
    234   1.1  christos 					 I387_FTAG_REGNUM (tdep), &ftag);
    235   1.1  christos   fiseg_p = read_frame_register_unsigned (frame,
    236   1.1  christos 					  I387_FISEG_REGNUM (tdep), &fiseg);
    237   1.1  christos   fioff_p = read_frame_register_unsigned (frame,
    238   1.1  christos 					  I387_FIOFF_REGNUM (tdep), &fioff);
    239   1.1  christos   foseg_p = read_frame_register_unsigned (frame,
    240   1.1  christos 					  I387_FOSEG_REGNUM (tdep), &foseg);
    241   1.1  christos   fooff_p = read_frame_register_unsigned (frame,
    242   1.1  christos 					  I387_FOOFF_REGNUM (tdep), &fooff);
    243   1.1  christos   fop_p = read_frame_register_unsigned (frame,
    244   1.1  christos 					I387_FOP_REGNUM (tdep), &fop);
    245   1.1  christos 
    246   1.1  christos   if (fstat_p)
    247   1.1  christos     {
    248   1.1  christos       top = ((fstat >> 11) & 7);
    249   1.1  christos 
    250   1.1  christos       for (fpreg = 7; fpreg >= 0; fpreg--)
    251   1.1  christos 	{
    252   1.1  christos 	  struct value *regval;
    253   1.1  christos 	  int regnum;
    254   1.1  christos 	  int i;
    255   1.1  christos 	  int tag = -1;
    256   1.1  christos 
    257  1.10  christos 	  gdb_printf (file, "%sR%d: ", fpreg == top ? "=>" : "  ", fpreg);
    258   1.1  christos 
    259   1.1  christos 	  if (ftag_p)
    260   1.1  christos 	    {
    261   1.1  christos 	      tag = (ftag >> (fpreg * 2)) & 3;
    262   1.1  christos 
    263   1.1  christos 	      switch (tag)
    264   1.1  christos 		{
    265   1.1  christos 		case 0:
    266  1.10  christos 		  gdb_puts ("Valid   ", file);
    267   1.1  christos 		  break;
    268   1.1  christos 		case 1:
    269  1.10  christos 		  gdb_puts ("Zero    ", file);
    270   1.1  christos 		  break;
    271   1.1  christos 		case 2:
    272  1.10  christos 		  gdb_puts ("Special ", file);
    273   1.1  christos 		  break;
    274   1.1  christos 		case 3:
    275  1.10  christos 		  gdb_puts ("Empty   ", file);
    276   1.1  christos 		  break;
    277   1.1  christos 		}
    278   1.1  christos 	    }
    279   1.1  christos 	  else
    280  1.10  christos 	    gdb_puts ("Unknown ", file);
    281   1.1  christos 
    282   1.1  christos 	  regnum = (fpreg + 8 - top) % 8 + I387_ST0_REGNUM (tdep);
    283   1.1  christos 	  regval = get_frame_register_value (frame, regnum);
    284   1.1  christos 
    285  1.11  christos 	  if (regval->entirely_available ())
    286   1.1  christos 	    {
    287  1.11  christos 	      const gdb_byte *raw = regval->contents ().data ();
    288   1.1  christos 
    289  1.10  christos 	      gdb_puts ("0x", file);
    290   1.1  christos 	      for (i = 9; i >= 0; i--)
    291  1.10  christos 		gdb_printf (file, "%02x", raw[i]);
    292   1.1  christos 
    293   1.1  christos 	      if (tag != -1 && tag != 3)
    294   1.1  christos 		print_i387_ext (gdbarch, raw, file);
    295   1.1  christos 	    }
    296   1.1  christos 	  else
    297  1.10  christos 	    gdb_printf (file, "%s", _("<unavailable>"));
    298   1.1  christos 
    299  1.10  christos 	  gdb_puts ("\n", file);
    300   1.1  christos 	}
    301   1.1  christos     }
    302   1.1  christos 
    303  1.10  christos   gdb_puts ("\n", file);
    304   1.1  christos   print_i387_status_word (fstat_p, fstat, file);
    305   1.1  christos   print_i387_control_word (fctrl_p, fctrl, file);
    306  1.10  christos   gdb_printf (file, "Tag Word:            %s\n",
    307  1.10  christos 	      ftag_p ? hex_string_custom (ftag, 4) : _("<unavailable>"));
    308  1.10  christos   gdb_printf (file, "Instruction Pointer: %s:",
    309  1.10  christos 	      fiseg_p ? hex_string_custom (fiseg, 2) : _("<unavailable>"));
    310  1.10  christos   gdb_printf (file, "%s\n",
    311  1.10  christos 	      fioff_p ? hex_string_custom (fioff, 8) : _("<unavailable>"));
    312  1.10  christos   gdb_printf (file, "Operand Pointer:     %s:",
    313  1.10  christos 	      foseg_p ? hex_string_custom (foseg, 2) : _("<unavailable>"));
    314  1.10  christos   gdb_printf (file, "%s\n",
    315  1.10  christos 	      fooff_p ? hex_string_custom (fooff, 8) : _("<unavailable>"));
    316  1.10  christos   gdb_printf (file, "Opcode:              %s\n",
    317  1.10  christos 	      fop_p
    318  1.10  christos 	      ? (hex_string_custom (fop ? (fop | 0xd800) : 0, 4))
    319  1.10  christos 	      : _("<unavailable>"));
    320   1.1  christos }
    321   1.1  christos 
    322   1.1  christos 
    324   1.1  christos /* Return nonzero if a value of type TYPE stored in register REGNUM
    325   1.1  christos    needs any special handling.  */
    326   1.1  christos 
    327   1.1  christos int
    328   1.1  christos i387_convert_register_p (struct gdbarch *gdbarch, int regnum,
    329   1.1  christos 			 struct type *type)
    330   1.1  christos {
    331   1.1  christos   if (i386_fp_regnum_p (gdbarch, regnum))
    332   1.1  christos     {
    333   1.8  christos       /* Floating point registers must be converted unless we are
    334   1.8  christos 	 accessing them in their hardware type or TYPE is not float.  */
    335   1.9  christos       if (type == i387_ext_type (gdbarch)
    336   1.1  christos 	  || type->code () != TYPE_CODE_FLT)
    337   1.1  christos 	return 0;
    338   1.1  christos       else
    339   1.1  christos 	return 1;
    340   1.1  christos     }
    341   1.1  christos 
    342   1.1  christos   return 0;
    343   1.1  christos }
    344   1.1  christos 
    345   1.1  christos /* Read a value of type TYPE from register REGNUM in frame FRAME, and
    346   1.1  christos    return its contents in TO.  */
    347   1.1  christos 
    348  1.11  christos int
    349   1.1  christos i387_register_to_value (const frame_info_ptr &frame, int regnum,
    350   1.1  christos 			struct type *type, gdb_byte *to,
    351   1.1  christos 			int *optimizedp, int *unavailablep)
    352   1.1  christos {
    353   1.1  christos   struct gdbarch *gdbarch = get_frame_arch (frame);
    354   1.1  christos   gdb_byte from[I386_MAX_REGISTER_SIZE];
    355   1.1  christos 
    356   1.1  christos   gdb_assert (i386_fp_regnum_p (gdbarch, regnum));
    357   1.1  christos 
    358   1.9  christos   /* We only support floating-point values.  */
    359   1.1  christos   if (type->code () != TYPE_CODE_FLT)
    360   1.1  christos     {
    361   1.1  christos       warning (_("Cannot convert floating-point register value "
    362   1.1  christos 	       "to non-floating-point type."));
    363   1.1  christos       *optimizedp = *unavailablep = 0;
    364   1.1  christos       return 0;
    365   1.1  christos     }
    366   1.1  christos 
    367  1.11  christos   /* Convert to TYPE.  */
    368  1.11  christos   auto from_view
    369  1.11  christos     = gdb::make_array_view (from, register_size (gdbarch, regnum));
    370  1.11  christos   frame_info_ptr next_frame = get_next_frame_sentinel_okay (frame);
    371  1.11  christos   if (!get_frame_register_bytes (next_frame, regnum, 0, from_view, optimizedp,
    372   1.1  christos 				 unavailablep))
    373   1.1  christos     return 0;
    374   1.8  christos 
    375   1.1  christos   target_float_convert (from, i387_ext_type (gdbarch), to, type);
    376   1.1  christos   *optimizedp = *unavailablep = 0;
    377   1.1  christos   return 1;
    378   1.1  christos }
    379   1.1  christos 
    380   1.1  christos /* Write the contents FROM of a value of type TYPE into register
    381   1.1  christos    REGNUM in frame FRAME.  */
    382   1.1  christos 
    383  1.11  christos void
    384   1.1  christos i387_value_to_register (const frame_info_ptr &frame, int regnum,
    385   1.1  christos 			struct type *type, const gdb_byte *from)
    386   1.1  christos {
    387   1.1  christos   struct gdbarch *gdbarch = get_frame_arch (frame);
    388   1.1  christos   gdb_byte to[I386_MAX_REGISTER_SIZE];
    389   1.1  christos 
    390   1.1  christos   gdb_assert (i386_fp_regnum_p (gdbarch, regnum));
    391   1.1  christos 
    392   1.9  christos   /* We only support floating-point values.  */
    393   1.1  christos   if (type->code () != TYPE_CODE_FLT)
    394   1.1  christos     {
    395   1.1  christos       warning (_("Cannot convert non-floating-point type "
    396   1.1  christos 	       "to floating-point register value."));
    397   1.1  christos       return;
    398   1.1  christos     }
    399   1.1  christos 
    400  1.11  christos   /* Convert from TYPE.  */
    401  1.11  christos   struct type *to_type = i387_ext_type (gdbarch);
    402  1.11  christos   target_float_convert (from, type, to, to_type);
    403  1.11  christos   auto to_view = gdb::make_array_view (to, to_type->length ());
    404   1.1  christos   put_frame_register (get_next_frame_sentinel_okay (frame), regnum, to_view);
    405   1.1  christos }
    406   1.1  christos 
    407   1.1  christos 
    409   1.1  christos /* Handle FSAVE and FXSAVE formats.  */
    410   1.1  christos 
    411   1.1  christos /* At fsave_offset[REGNUM] you'll find the offset to the location in
    412   1.1  christos    the data structure used by the "fsave" instruction where GDB
    413   1.1  christos    register REGNUM is stored.  */
    414   1.1  christos 
    415   1.1  christos static int fsave_offset[] =
    416   1.1  christos {
    417   1.1  christos   28 + 0 * 10,			/* %st(0) ...  */
    418   1.1  christos   28 + 1 * 10,
    419   1.1  christos   28 + 2 * 10,
    420   1.1  christos   28 + 3 * 10,
    421   1.1  christos   28 + 4 * 10,
    422   1.1  christos   28 + 5 * 10,
    423   1.1  christos   28 + 6 * 10,
    424   1.1  christos   28 + 7 * 10,			/* ... %st(7).  */
    425   1.1  christos   0,				/* `fctrl' (16 bits).  */
    426   1.1  christos   4,				/* `fstat' (16 bits).  */
    427   1.1  christos   8,				/* `ftag' (16 bits).  */
    428   1.1  christos   16,				/* `fiseg' (16 bits).  */
    429   1.1  christos   12,				/* `fioff'.  */
    430   1.1  christos   24,				/* `foseg' (16 bits).  */
    431   1.1  christos   20,				/* `fooff'.  */
    432   1.1  christos   18				/* `fop' (bottom 11 bits).  */
    433   1.1  christos };
    434   1.1  christos 
    435   1.1  christos #define FSAVE_ADDR(tdep, fsave, regnum) \
    436   1.1  christos   (fsave + fsave_offset[regnum - I387_ST0_REGNUM (tdep)])
    437   1.1  christos 
    438   1.1  christos 
    440   1.1  christos /* Fill register REGNUM in REGCACHE with the appropriate value from
    441   1.1  christos    *FSAVE.  This function masks off any of the reserved bits in
    442   1.1  christos    *FSAVE.  */
    443   1.1  christos 
    444   1.8  christos void
    445  1.10  christos i387_supply_fsave (struct regcache *regcache, int regnum, const void *fsave)
    446   1.1  christos {
    447   1.6  christos   struct gdbarch *gdbarch = regcache->arch ();
    448   1.1  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
    449   1.1  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
    450   1.1  christos   const gdb_byte *regs = (const gdb_byte *) fsave;
    451   1.1  christos   int i;
    452   1.1  christos 
    453   1.1  christos   gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
    454   1.1  christos 
    455   1.1  christos   for (i = I387_ST0_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
    456   1.1  christos     if (regnum == -1 || regnum == i)
    457   1.8  christos       {
    458   1.1  christos 	if (fsave == NULL)
    459   1.1  christos 	  {
    460   1.1  christos 	    regcache->raw_supply (i, NULL);
    461   1.1  christos 	    continue;
    462   1.1  christos 	  }
    463   1.1  christos 
    464   1.1  christos 	/* Most of the FPU control registers occupy only 16 bits in the
    465   1.1  christos 	   fsave area.  Give those a special treatment.  */
    466   1.1  christos 	if (i >= I387_FCTRL_REGNUM (tdep)
    467   1.1  christos 	    && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
    468   1.1  christos 	  {
    469   1.1  christos 	    gdb_byte val[4];
    470   1.1  christos 
    471   1.1  christos 	    memcpy (val, FSAVE_ADDR (tdep, regs, i), 2);
    472   1.8  christos 	    val[2] = val[3] = 0;
    473   1.1  christos 	    if (i == I387_FOP_REGNUM (tdep))
    474   1.1  christos 	      val[1] &= ((1 << 3) - 1);
    475   1.8  christos 	    regcache->raw_supply (i, val);
    476   1.1  christos 	  }
    477   1.1  christos 	else
    478   1.1  christos 	  regcache->raw_supply (i, FSAVE_ADDR (tdep, regs, i));
    479   1.1  christos       }
    480   1.1  christos 
    481   1.8  christos   /* Provide dummy values for the SSE registers.  */
    482   1.1  christos   for (i = I387_XMM0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
    483   1.1  christos     if (regnum == -1 || regnum == i)
    484   1.1  christos       regcache->raw_supply (i, NULL);
    485   1.1  christos   if (regnum == -1 || regnum == I387_MXCSR_REGNUM (tdep))
    486   1.8  christos     {
    487   1.8  christos       gdb_byte buf[4];
    488   1.1  christos 
    489   1.1  christos       store_unsigned_integer (buf, 4, byte_order, I387_MXCSR_INIT_VAL);
    490   1.1  christos       regcache->raw_supply (I387_MXCSR_REGNUM (tdep), buf);
    491   1.1  christos     }
    492   1.1  christos }
    493   1.1  christos 
    494   1.1  christos /* Fill register REGNUM (if it is a floating-point register) in *FSAVE
    495   1.1  christos    with the value from REGCACHE.  If REGNUM is -1, do this for all
    496   1.1  christos    registers.  This function doesn't touch any of the reserved bits in
    497   1.1  christos    *FSAVE.  */
    498   1.1  christos 
    499  1.10  christos void
    500  1.10  christos i387_collect_fsave (const struct regcache *regcache, int regnum, void *fsave)
    501   1.6  christos {
    502   1.1  christos   gdbarch *arch = regcache->arch ();
    503   1.1  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
    504   1.1  christos   gdb_byte *regs = (gdb_byte *) fsave;
    505   1.1  christos   int i;
    506   1.1  christos 
    507   1.1  christos   gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
    508   1.1  christos 
    509   1.1  christos   for (i = I387_ST0_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
    510  1.10  christos     if (regnum == -1 || regnum == i)
    511   1.1  christos       {
    512   1.1  christos 	/* Most of the FPU control registers occupy only 16 bits in
    513   1.1  christos 	   the fsave area.  Give those a special treatment.  */
    514   1.1  christos 	if (i >= I387_FCTRL_REGNUM (tdep)
    515   1.1  christos 	    && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
    516   1.8  christos 	  {
    517   1.1  christos 	    gdb_byte buf[4];
    518   1.1  christos 
    519   1.1  christos 	    regcache->raw_collect (i, buf);
    520   1.1  christos 
    521  1.10  christos 	    if (i == I387_FOP_REGNUM (tdep))
    522   1.1  christos 	      {
    523   1.1  christos 		/* The opcode occupies only 11 bits.  Make sure we
    524   1.1  christos 		   don't touch the other bits.  */
    525   1.1  christos 		buf[1] &= ((1 << 3) - 1);
    526   1.1  christos 		buf[1] |= ((FSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
    527   1.1  christos 	      }
    528   1.8  christos 	    memcpy (FSAVE_ADDR (tdep, regs, i), buf, 2);
    529   1.1  christos 	  }
    530   1.1  christos 	else
    531   1.1  christos 	  regcache->raw_collect (i, FSAVE_ADDR (tdep, regs, i));
    532   1.1  christos       }
    533   1.1  christos }
    534   1.1  christos 
    535   1.1  christos 
    537   1.1  christos /* At fxsave_offset[REGNUM] you'll find the offset to the location in
    538   1.1  christos    the data structure used by the "fxsave" instruction where GDB
    539   1.1  christos    register REGNUM is stored.  */
    540   1.1  christos 
    541   1.1  christos static int fxsave_offset[] =
    542   1.1  christos {
    543   1.1  christos   32,				/* %st(0) through ...  */
    544   1.1  christos   48,
    545   1.1  christos   64,
    546   1.1  christos   80,
    547   1.1  christos   96,
    548   1.1  christos   112,
    549   1.1  christos   128,
    550   1.1  christos   144,				/* ... %st(7) (80 bits each).  */
    551   1.1  christos   0,				/* `fctrl' (16 bits).  */
    552   1.1  christos   2,				/* `fstat' (16 bits).  */
    553   1.1  christos   4,				/* `ftag' (16 bits).  */
    554   1.1  christos   12,				/* `fiseg' (16 bits).  */
    555   1.1  christos   8,				/* `fioff'.  */
    556   1.1  christos   20,				/* `foseg' (16 bits).  */
    557   1.1  christos   16,				/* `fooff'.  */
    558   1.1  christos   6,				/* `fop' (bottom 11 bits).  */
    559   1.1  christos   160 + 0 * 16,			/* %xmm0 through ...  */
    560   1.1  christos   160 + 1 * 16,
    561   1.1  christos   160 + 2 * 16,
    562   1.1  christos   160 + 3 * 16,
    563   1.1  christos   160 + 4 * 16,
    564   1.1  christos   160 + 5 * 16,
    565   1.1  christos   160 + 6 * 16,
    566   1.1  christos   160 + 7 * 16,
    567   1.1  christos   160 + 8 * 16,
    568   1.1  christos   160 + 9 * 16,
    569   1.1  christos   160 + 10 * 16,
    570   1.1  christos   160 + 11 * 16,
    571   1.1  christos   160 + 12 * 16,
    572   1.1  christos   160 + 13 * 16,
    573   1.1  christos   160 + 14 * 16,
    574   1.1  christos   160 + 15 * 16,		/* ... %xmm15 (128 bits each).  */
    575   1.1  christos };
    576   1.1  christos 
    577   1.1  christos #define FXSAVE_ADDR(tdep, fxsave, regnum) \
    578   1.1  christos   (fxsave + fxsave_offset[regnum - I387_ST0_REGNUM (tdep)])
    579   1.1  christos 
    580   1.1  christos /* We made an unfortunate choice in putting %mxcsr after the SSE
    581   1.1  christos    registers %xmm0-%xmm7 instead of before, since it makes supporting
    582   1.1  christos    the registers %xmm8-%xmm15 on AMD64 a bit involved.  Therefore we
    583   1.1  christos    don't include the offset for %mxcsr here above.  */
    584   1.1  christos 
    585   1.1  christos #define FXSAVE_MXCSR_ADDR(fxsave) (fxsave + 24)
    586   1.1  christos 
    587   1.1  christos static int i387_tag (const gdb_byte *raw);
    588   1.1  christos 
    589   1.1  christos 
    591   1.1  christos /* Fill register REGNUM in REGCACHE with the appropriate
    592   1.1  christos    floating-point or SSE register value from *FXSAVE.  This function
    593  1.10  christos    masks off any of the reserved bits in *FXSAVE.  */
    594  1.10  christos 
    595   1.6  christos void
    596   1.1  christos i387_supply_fxsave (struct regcache *regcache, int regnum, const void *fxsave)
    597   1.1  christos {
    598   1.1  christos   gdbarch *arch = regcache->arch ();
    599   1.1  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
    600   1.1  christos   const gdb_byte *regs = (const gdb_byte *) fxsave;
    601   1.1  christos   int i;
    602   1.1  christos 
    603   1.1  christos   gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
    604   1.1  christos   gdb_assert (tdep->num_xmm_regs > 0);
    605   1.1  christos 
    606   1.8  christos   for (i = I387_ST0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
    607   1.1  christos     if (regnum == -1 || regnum == i)
    608   1.1  christos       {
    609   1.1  christos 	if (regs == NULL)
    610   1.1  christos 	  {
    611   1.1  christos 	    regcache->raw_supply (i, NULL);
    612   1.1  christos 	    continue;
    613   1.1  christos 	  }
    614   1.1  christos 
    615   1.1  christos 	/* Most of the FPU control registers occupy only 16 bits in
    616   1.1  christos 	   the fxsave area.  Give those a special treatment.  */
    617   1.1  christos 	if (i >= I387_FCTRL_REGNUM (tdep) && i < I387_XMM0_REGNUM (tdep)
    618   1.1  christos 	    && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
    619   1.1  christos 	  {
    620   1.1  christos 	    gdb_byte val[4];
    621   1.1  christos 
    622   1.1  christos 	    memcpy (val, FXSAVE_ADDR (tdep, regs, i), 2);
    623   1.1  christos 	    val[2] = val[3] = 0;
    624   1.1  christos 	    if (i == I387_FOP_REGNUM (tdep))
    625   1.1  christos 	      val[1] &= ((1 << 3) - 1);
    626   1.1  christos 	    else if (i== I387_FTAG_REGNUM (tdep))
    627   1.1  christos 	      {
    628   1.1  christos 		/* The fxsave area contains a simplified version of
    629   1.1  christos 		   the tag word.  We have to look at the actual 80-bit
    630   1.1  christos 		   FP data to recreate the traditional i387 tag word.  */
    631   1.1  christos 
    632   1.1  christos 		unsigned long ftag = 0;
    633   1.1  christos 		int fpreg;
    634   1.1  christos 		int top;
    635   1.1  christos 
    636   1.1  christos 		top = ((FXSAVE_ADDR (tdep, regs,
    637   1.1  christos 				     I387_FSTAT_REGNUM (tdep)))[1] >> 3);
    638   1.1  christos 		top &= 0x7;
    639   1.1  christos 
    640   1.1  christos 		for (fpreg = 7; fpreg >= 0; fpreg--)
    641   1.1  christos 		  {
    642  1.10  christos 		    int tag;
    643   1.1  christos 
    644   1.1  christos 		    if (val[0] & (1 << fpreg))
    645   1.1  christos 		      {
    646   1.1  christos 			int thisreg = (fpreg + 8 - top) % 8
    647   1.1  christos 				       + I387_ST0_REGNUM (tdep);
    648   1.1  christos 			tag = i387_tag (FXSAVE_ADDR (tdep, regs, thisreg));
    649   1.1  christos 		      }
    650   1.1  christos 		    else
    651   1.1  christos 		      tag = 3;		/* Empty */
    652   1.1  christos 
    653   1.8  christos 		    ftag |= tag << (2 * fpreg);
    654   1.1  christos 		  }
    655   1.1  christos 		val[0] = ftag & 0xff;
    656   1.8  christos 		val[1] = (ftag >> 8) & 0xff;
    657   1.1  christos 	      }
    658   1.1  christos 	    regcache->raw_supply (i, val);
    659   1.1  christos 	  }
    660   1.1  christos 	else
    661   1.1  christos 	  regcache->raw_supply (i, FXSAVE_ADDR (tdep, regs, i));
    662   1.8  christos       }
    663   1.1  christos 
    664   1.8  christos   if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
    665   1.1  christos     {
    666   1.1  christos       if (regs == NULL)
    667   1.1  christos 	regcache->raw_supply (I387_MXCSR_REGNUM (tdep), NULL);
    668   1.1  christos       else
    669   1.1  christos 	regcache->raw_supply (I387_MXCSR_REGNUM (tdep),
    670   1.1  christos 			     FXSAVE_MXCSR_ADDR (regs));
    671   1.1  christos     }
    672   1.1  christos }
    673   1.1  christos 
    674   1.1  christos /* Fill register REGNUM (if it is a floating-point or SSE register) in
    675   1.1  christos    *FXSAVE with the value from REGCACHE.  If REGNUM is -1, do this for
    676   1.1  christos    all registers.  This function doesn't touch any of the reserved
    677  1.10  christos    bits in *FXSAVE.  */
    678  1.10  christos 
    679   1.6  christos void
    680   1.1  christos i387_collect_fxsave (const struct regcache *regcache, int regnum, void *fxsave)
    681   1.1  christos {
    682   1.1  christos   gdbarch *arch = regcache->arch ();
    683   1.1  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (arch);
    684   1.1  christos   gdb_byte *regs = (gdb_byte *) fxsave;
    685   1.1  christos   int i;
    686   1.1  christos 
    687   1.1  christos   gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
    688   1.1  christos   gdb_assert (tdep->num_xmm_regs > 0);
    689  1.10  christos 
    690   1.1  christos   for (i = I387_ST0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
    691   1.1  christos     if (regnum == -1 || regnum == i)
    692   1.1  christos       {
    693   1.1  christos 	/* Most of the FPU control registers occupy only 16 bits in
    694   1.1  christos 	   the fxsave area.  Give those a special treatment.  */
    695   1.8  christos 	if (i >= I387_FCTRL_REGNUM (tdep) && i < I387_XMM0_REGNUM (tdep)
    696   1.1  christos 	    && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
    697   1.1  christos 	  {
    698   1.1  christos 	    gdb_byte buf[4];
    699   1.1  christos 
    700  1.10  christos 	    regcache->raw_collect (i, buf);
    701   1.1  christos 
    702   1.1  christos 	    if (i == I387_FOP_REGNUM (tdep))
    703   1.1  christos 	      {
    704   1.1  christos 		/* The opcode occupies only 11 bits.  Make sure we
    705   1.1  christos 		   don't touch the other bits.  */
    706   1.1  christos 		buf[1] &= ((1 << 3) - 1);
    707   1.1  christos 		buf[1] |= ((FXSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
    708   1.1  christos 	      }
    709   1.1  christos 	    else if (i == I387_FTAG_REGNUM (tdep))
    710   1.1  christos 	      {
    711   1.1  christos 		/* Converting back is much easier.  */
    712   1.1  christos 
    713   1.1  christos 		unsigned short ftag;
    714   1.1  christos 		int fpreg;
    715   1.1  christos 
    716   1.1  christos 		ftag = (buf[1] << 8) | buf[0];
    717   1.1  christos 		buf[0] = 0;
    718   1.1  christos 		buf[1] = 0;
    719   1.1  christos 
    720   1.1  christos 		for (fpreg = 7; fpreg >= 0; fpreg--)
    721   1.1  christos 		  {
    722   1.1  christos 		    int tag = (ftag >> (fpreg * 2)) & 3;
    723   1.1  christos 
    724   1.1  christos 		    if (tag != 3)
    725   1.1  christos 		      buf[0] |= (1 << fpreg);
    726   1.8  christos 		  }
    727   1.1  christos 	      }
    728   1.1  christos 	    memcpy (FXSAVE_ADDR (tdep, regs, i), buf, 2);
    729   1.1  christos 	  }
    730   1.8  christos 	else
    731   1.1  christos 	  regcache->raw_collect (i, FXSAVE_ADDR (tdep, regs, i));
    732   1.1  christos       }
    733   1.1  christos 
    734   1.1  christos   if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
    735   1.1  christos     regcache->raw_collect (I387_MXCSR_REGNUM (tdep),
    736   1.1  christos 			  FXSAVE_MXCSR_ADDR (regs));
    737  1.11  christos }
    738  1.11  christos 
    739  1.11  christos /* `xstate_bv' is at byte offset 512.  */
    740   1.1  christos #define XSAVE_XSTATE_BV_ADDR(xsave) (xsave + 512)
    741   1.1  christos 
    742   1.1  christos /* At xsave_avxh_offset[REGNUM] you'll find the relative offset within
    743  1.11  christos    the AVX region of the XSAVE extended state where the upper 128bits
    744  1.11  christos    of GDB register YMM0 + REGNUM is stored.  */
    745  1.11  christos 
    746  1.11  christos static int xsave_avxh_offset[] =
    747  1.11  christos {
    748  1.11  christos   0 * 16,		/* Upper 128bit of %ymm0 through ...  */
    749  1.11  christos   1 * 16,
    750  1.11  christos   2 * 16,
    751  1.11  christos   3 * 16,
    752  1.11  christos   4 * 16,
    753  1.11  christos   5 * 16,
    754  1.11  christos   6 * 16,
    755  1.11  christos   7 * 16,
    756  1.11  christos   8 * 16,
    757  1.11  christos   9 * 16,
    758  1.11  christos   10 * 16,
    759   1.1  christos   11 * 16,
    760   1.1  christos   12 * 16,
    761  1.11  christos   13 * 16,
    762  1.11  christos   14 * 16,
    763  1.11  christos   15 * 16		/* Upper 128bit of ... %ymm15 (128 bits each).  */
    764  1.11  christos };
    765  1.11  christos 
    766  1.11  christos #define XSAVE_AVXH_ADDR(tdep, xsave, regnum)			\
    767  1.11  christos   (xsave + (tdep)->xsave_layout.avx_offset			\
    768   1.3  christos    + xsave_avxh_offset[regnum - I387_YMM0H_REGNUM (tdep)])
    769   1.3  christos 
    770   1.3  christos /* At xsave_ymm_avx512_offset[REGNUM] you'll find the relative offset
    771  1.11  christos    within the ZMM region of the XSAVE extended state where the second
    772  1.11  christos    128bits of GDB register YMM16 + REGNUM is stored.  */
    773  1.11  christos 
    774  1.11  christos static int xsave_ymm_avx512_offset[] =
    775  1.11  christos {
    776  1.11  christos   16 + 0 * 64,		/* %ymm16 through...  */
    777  1.11  christos   16 + 1 * 64,
    778  1.11  christos   16 + 2 * 64,
    779  1.11  christos   16 + 3 * 64,
    780  1.11  christos   16 + 4 * 64,
    781  1.11  christos   16 + 5 * 64,
    782  1.11  christos   16 + 6 * 64,
    783  1.11  christos   16 + 7 * 64,
    784  1.11  christos   16 + 8 * 64,
    785  1.11  christos   16 + 9 * 64,
    786  1.11  christos   16 + 10 * 64,
    787   1.3  christos   16 + 11 * 64,
    788   1.3  christos   16 + 12 * 64,
    789  1.11  christos   16 + 13 * 64,
    790  1.11  christos   16 + 14 * 64,
    791  1.11  christos   16 + 15 * 64		/* ...  %ymm31 (128 bits each).  */
    792  1.11  christos };
    793  1.11  christos 
    794  1.11  christos #define XSAVE_YMM_AVX512_ADDR(tdep, xsave, regnum)			\
    795  1.11  christos   (xsave + (tdep)->xsave_layout.zmm_offset				\
    796   1.3  christos    + xsave_ymm_avx512_offset[regnum - I387_YMM16H_REGNUM (tdep)])
    797   1.3  christos 
    798   1.1  christos /* At xsave_xmm_avx512_offset[REGNUM] you'll find the relative offset
    799  1.11  christos    within the ZMM region of the XSAVE extended state where the first
    800  1.11  christos    128bits of GDB register XMM16 + REGNUM is stored.  */
    801  1.11  christos 
    802  1.11  christos static int xsave_xmm_avx512_offset[] =
    803  1.11  christos {
    804  1.11  christos   0 * 64,			/* %xmm16 through...  */
    805  1.11  christos   1 * 64,
    806  1.11  christos   2 * 64,
    807  1.11  christos   3 * 64,
    808  1.11  christos   4 * 64,
    809  1.11  christos   5 * 64,
    810  1.11  christos   6 * 64,
    811  1.11  christos   7 * 64,
    812  1.11  christos   8 * 64,
    813  1.11  christos   9 * 64,
    814  1.11  christos   10 * 64,
    815   1.3  christos   11 * 64,
    816   1.3  christos   12 * 64,
    817  1.11  christos   13 * 64,
    818  1.11  christos   14 * 64,
    819  1.11  christos   15 * 64			/* ...  %xmm31 (128 bits each).  */
    820  1.11  christos };
    821  1.11  christos 
    822  1.11  christos #define XSAVE_XMM_AVX512_ADDR(tdep, xsave, regnum)			\
    823  1.11  christos   (xsave + (tdep)->xsave_layout.zmm_offset				\
    824  1.11  christos    + xsave_xmm_avx512_offset[regnum - I387_XMM16_REGNUM (tdep)])
    825  1.11  christos 
    826  1.11  christos /* At xsave_bndregs_offset[REGNUM] you'll find the relative offset
    827  1.11  christos    within the BNDREGS region of the XSAVE extended state where the GDB
    828  1.11  christos    register BND0R + REGNUM is stored.  */
    829  1.11  christos 
    830  1.11  christos static int xsave_bndregs_offset[] = {
    831   1.3  christos   0 * 16,			/* bnd0r...bnd3r registers.  */
    832  1.11  christos   1 * 16,
    833  1.11  christos   2 * 16,
    834  1.11  christos   3 * 16
    835  1.11  christos };
    836  1.11  christos 
    837  1.11  christos #define XSAVE_BNDREGS_ADDR(tdep, xsave, regnum)				\
    838  1.11  christos   (xsave + (tdep)->xsave_layout.bndregs_offset				\
    839   1.1  christos    + xsave_bndregs_offset[regnum - I387_BND0R_REGNUM (tdep)])
    840   1.1  christos 
    841  1.11  christos static int xsave_bndcfg_offset[] = {
    842  1.11  christos   0 * 8,			/* bndcfg ... bndstatus.  */
    843  1.11  christos   1 * 8,
    844  1.11  christos };
    845  1.11  christos 
    846  1.11  christos #define XSAVE_BNDCFG_ADDR(tdep, xsave, regnum)			\
    847  1.11  christos   (xsave + (tdep)->xsave_layout.bndcfg_offset			\
    848   1.3  christos    + xsave_bndcfg_offset[regnum - I387_BNDCFGU_REGNUM (tdep)])
    849   1.3  christos 
    850   1.3  christos /* At xsave_avx512_k_offset[REGNUM] you'll find the relative offset
    851  1.11  christos    within the K region of the XSAVE extended state where the AVX512
    852  1.11  christos    opmask register K0 + REGNUM is stored.  */
    853  1.11  christos 
    854  1.11  christos static int xsave_avx512_k_offset[] =
    855  1.11  christos {
    856  1.11  christos   0 * 8,			/* %k0 through...  */
    857  1.11  christos   1 * 8,
    858  1.11  christos   2 * 8,
    859   1.3  christos   3 * 8,
    860   1.3  christos   4 * 8,
    861  1.11  christos   5 * 8,
    862  1.11  christos   6 * 8,
    863  1.11  christos   7 * 8				/* %k7 (64 bits each).	*/
    864  1.11  christos };
    865  1.11  christos 
    866  1.11  christos #define XSAVE_AVX512_K_ADDR(tdep, xsave, regnum)		\
    867  1.11  christos   (xsave + (tdep)->xsave_layout.k_offset			\
    868  1.11  christos    + xsave_avx512_k_offset[regnum - I387_K0_REGNUM (tdep)])
    869  1.11  christos 
    870  1.11  christos 
    871  1.11  christos /* At xsave_avx512_zmm0_h_offset[REGNUM] you find the relative offset
    872  1.11  christos    within the ZMM_H region of the XSAVE extended state where the upper
    873  1.11  christos    256bits of the GDB register ZMM0 + REGNUM is stored.  */
    874  1.11  christos 
    875  1.11  christos static int xsave_avx512_zmm0_h_offset[] =
    876  1.11  christos {
    877  1.11  christos   0 * 32,		/* Upper 256bit of %zmmh0 through...  */
    878  1.11  christos   1 * 32,
    879  1.11  christos   2 * 32,
    880  1.11  christos   3 * 32,
    881  1.11  christos   4 * 32,
    882  1.11  christos   5 * 32,
    883  1.11  christos   6 * 32,
    884  1.11  christos   7 * 32,
    885  1.11  christos   8 * 32,
    886  1.11  christos   9 * 32,
    887  1.11  christos   10 * 32,
    888  1.11  christos   11 * 32,
    889   1.3  christos   12 * 32,
    890  1.11  christos   13 * 32,
    891  1.11  christos   14 * 32,
    892  1.11  christos   15 * 32		/* Upper 256bit of...  %zmmh15 (256 bits each).  */
    893  1.11  christos };
    894  1.11  christos 
    895  1.11  christos #define XSAVE_AVX512_ZMM0_H_ADDR(tdep, xsave, regnum)			\
    896  1.11  christos   (xsave + (tdep)->xsave_layout.zmm_h_offset				\
    897  1.11  christos    + xsave_avx512_zmm0_h_offset[regnum - I387_ZMM0H_REGNUM (tdep)])
    898  1.11  christos 
    899  1.11  christos /* At xsave_avx512_zmm16_h_offset[REGNUM] you find the relative offset
    900  1.11  christos    within the ZMM_H region of the XSAVE extended state where the upper
    901  1.11  christos    256bits of the GDB register ZMM16 + REGNUM is stored.  */
    902  1.11  christos 
    903  1.11  christos static int xsave_avx512_zmm16_h_offset[] =
    904  1.11  christos {
    905  1.11  christos   32 + 0 * 64,		/* Upper 256bit of...  %zmmh16 (256 bits each).  */
    906  1.11  christos   32 + 1 * 64,
    907  1.11  christos   32 + 2 * 64,
    908  1.11  christos   32 + 3 * 64,
    909  1.11  christos   32 + 4 * 64,
    910  1.11  christos   32 + 5 * 64,
    911  1.11  christos   32 + 6 * 64,
    912  1.11  christos   32 + 7 * 64,
    913  1.11  christos   32 + 8 * 64,
    914  1.11  christos   32 + 9 * 64,
    915  1.11  christos   32 + 10 * 64,
    916   1.3  christos   32 + 11 * 64,
    917   1.3  christos   32 + 12 * 64,
    918  1.11  christos   32 + 13 * 64,
    919  1.11  christos   32 + 14 * 64,
    920  1.11  christos   32 + 15 * 64		/* Upper 256bit of... %zmmh31 (256 bits each).  */
    921  1.11  christos };
    922  1.11  christos 
    923  1.11  christos #define XSAVE_AVX512_ZMM16_H_ADDR(tdep, xsave, regnum)			\
    924  1.11  christos   (xsave + (tdep)->xsave_layout.zmm_offset				\
    925   1.7  christos    + xsave_avx512_zmm16_h_offset[regnum - I387_ZMM16H_REGNUM (tdep)])
    926   1.7  christos 
    927   1.7  christos /* At xsave_pkeys_offset[REGNUM] you'll find the relative offset
    928  1.11  christos    within the PKEYS region of the XSAVE extended state where the PKRU
    929   1.7  christos    register is stored.  */
    930   1.7  christos 
    931   1.7  christos static int xsave_pkeys_offset[] =
    932  1.11  christos {
    933  1.11  christos   0 * 8			/* %pkru (64 bits in XSTATE, 32-bit actually used by
    934  1.11  christos 			   instructions and applications).  */
    935  1.11  christos };
    936  1.11  christos 
    937  1.11  christos #define XSAVE_PKEYS_ADDR(tdep, xsave, regnum)				\
    938  1.11  christos   (xsave + (tdep)->xsave_layout.pkru_offset				\
    939  1.11  christos    + xsave_pkeys_offset[regnum - I387_PKRU_REGNUM (tdep)])
    940  1.11  christos 
    941  1.11  christos 
    942  1.11  christos /* See i387-tdep.h.  */
    943  1.11  christos 
    944  1.11  christos bool
    945  1.11  christos i387_guess_xsave_layout (uint64_t xcr0, size_t xsave_size,
    946  1.11  christos 			 x86_xsave_layout &layout)
    947  1.11  christos {
    948  1.11  christos   if (HAS_PKRU (xcr0) && xsave_size == 2696)
    949  1.11  christos     {
    950  1.11  christos       /* Intel CPUs supporting PKRU.  */
    951  1.11  christos       layout.avx_offset = 576;
    952  1.11  christos       layout.bndregs_offset = 960;
    953  1.11  christos       layout.bndcfg_offset = 1024;
    954  1.11  christos       layout.k_offset = 1088;
    955  1.11  christos       layout.zmm_h_offset = 1152;
    956  1.11  christos       layout.zmm_offset = 1664;
    957  1.11  christos       layout.pkru_offset = 2688;
    958  1.11  christos     }
    959  1.11  christos   else if (HAS_PKRU (xcr0) && xsave_size == 2440)
    960  1.11  christos     {
    961  1.11  christos       /* AMD CPUs supporting PKRU.  */
    962  1.11  christos       layout.avx_offset = 576;
    963  1.11  christos       layout.k_offset = 832;
    964  1.11  christos       layout.zmm_h_offset = 896;
    965  1.11  christos       layout.zmm_offset = 1408;
    966  1.11  christos       layout.pkru_offset = 2432;
    967  1.11  christos     }
    968  1.11  christos   else if (HAS_AVX512 (xcr0) && xsave_size == 2688)
    969  1.11  christos     {
    970  1.11  christos       /* Intel CPUs supporting AVX512.  */
    971  1.11  christos       layout.avx_offset = 576;
    972  1.11  christos       layout.bndregs_offset = 960;
    973  1.11  christos       layout.bndcfg_offset = 1024;
    974  1.11  christos       layout.k_offset = 1088;
    975  1.11  christos       layout.zmm_h_offset = 1152;
    976  1.11  christos       layout.zmm_offset = 1664;
    977  1.11  christos     }
    978  1.11  christos   else if (HAS_MPX (xcr0) && xsave_size == 1088)
    979  1.11  christos     {
    980  1.11  christos       /* Intel CPUs supporting MPX.  */
    981  1.11  christos       layout.avx_offset = 576;
    982  1.11  christos       layout.bndregs_offset = 960;
    983  1.11  christos       layout.bndcfg_offset = 1024;
    984  1.11  christos     }
    985  1.11  christos   else if (HAS_AVX (xcr0) && xsave_size == 832)
    986  1.11  christos     {
    987  1.11  christos       /* Intel and AMD CPUs supporting AVX.  */
    988  1.11  christos       layout.avx_offset = 576;
    989  1.11  christos     }
    990  1.11  christos   else
    991  1.11  christos     return false;
    992  1.11  christos 
    993  1.11  christos   layout.sizeof_xsave = xsave_size;
    994  1.11  christos   return true;
    995  1.11  christos }
    996  1.11  christos 
    997  1.11  christos /* See i387-tdep.h.  */
    998   1.7  christos 
    999  1.11  christos x86_xsave_layout
   1000  1.11  christos i387_fallback_xsave_layout (uint64_t xcr0)
   1001  1.11  christos {
   1002  1.11  christos   x86_xsave_layout layout;
   1003  1.11  christos 
   1004  1.11  christos   if (HAS_PKRU (xcr0))
   1005  1.11  christos     {
   1006  1.11  christos       /* Intel CPUs supporting PKRU.  */
   1007  1.11  christos       layout.avx_offset = 576;
   1008  1.11  christos       layout.bndregs_offset = 960;
   1009  1.11  christos       layout.bndcfg_offset = 1024;
   1010  1.11  christos       layout.k_offset = 1088;
   1011  1.11  christos       layout.zmm_h_offset = 1152;
   1012  1.11  christos       layout.zmm_offset = 1664;
   1013  1.11  christos       layout.pkru_offset = 2688;
   1014  1.11  christos       layout.sizeof_xsave = 2696;
   1015  1.11  christos     }
   1016  1.11  christos   else if (HAS_AVX512 (xcr0))
   1017  1.11  christos     {
   1018  1.11  christos       /* Intel CPUs supporting AVX512.  */
   1019  1.11  christos       layout.avx_offset = 576;
   1020  1.11  christos       layout.bndregs_offset = 960;
   1021  1.11  christos       layout.bndcfg_offset = 1024;
   1022  1.11  christos       layout.k_offset = 1088;
   1023  1.11  christos       layout.zmm_h_offset = 1152;
   1024  1.11  christos       layout.zmm_offset = 1664;
   1025  1.11  christos       layout.sizeof_xsave = 2688;
   1026  1.11  christos     }
   1027  1.11  christos   else if (HAS_MPX (xcr0))
   1028  1.11  christos     {
   1029  1.11  christos       /* Intel CPUs supporting MPX.  */
   1030  1.11  christos       layout.avx_offset = 576;
   1031  1.11  christos       layout.bndregs_offset = 960;
   1032  1.11  christos       layout.bndcfg_offset = 1024;
   1033  1.11  christos       layout.sizeof_xsave = 1088;
   1034  1.11  christos     }
   1035  1.11  christos   else if (HAS_AVX (xcr0))
   1036  1.11  christos     {
   1037  1.11  christos       /* Intel and AMD CPUs supporting AVX.  */
   1038  1.11  christos       layout.avx_offset = 576;
   1039   1.8  christos       layout.sizeof_xsave = 832;
   1040   1.8  christos     }
   1041   1.8  christos 
   1042   1.8  christos   return layout;
   1043   1.8  christos }
   1044   1.8  christos 
   1045   1.8  christos /* Extract from XSAVE a bitset of the features that are available on the
   1046   1.8  christos    target, but which have not yet been enabled.  */
   1047   1.8  christos 
   1048  1.10  christos ULONGEST
   1049   1.8  christos i387_xsave_get_clear_bv (struct gdbarch *gdbarch, const void *xsave)
   1050   1.8  christos {
   1051   1.8  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1052   1.8  christos   const gdb_byte *regs = (const gdb_byte *) xsave;
   1053   1.8  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
   1054   1.8  christos 
   1055   1.8  christos   /* Get `xstat_bv'.  The supported bits in `xstat_bv' are 8 bytes.  */
   1056   1.8  christos   ULONGEST xstate_bv = extract_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs),
   1057   1.8  christos 						 8, byte_order);
   1058   1.8  christos 
   1059   1.8  christos   /* Clear part in vector registers if its bit in xstat_bv is zero.  */
   1060   1.1  christos   ULONGEST clear_bv = (~(xstate_bv)) & tdep->xcr0;
   1061   1.1  christos 
   1062   1.1  christos   return clear_bv;
   1063   1.1  christos }
   1064   1.1  christos 
   1065   1.1  christos /* Similar to i387_supply_fxsave, but use XSAVE extended state.  */
   1066   1.8  christos 
   1067   1.8  christos void
   1068  1.10  christos i387_supply_xsave (struct regcache *regcache, int regnum,
   1069   1.6  christos 		   const void *xsave)
   1070   1.1  christos {
   1071   1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
   1072   1.8  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1073   1.8  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
   1074   1.8  christos   const gdb_byte *regs = (const gdb_byte *) xsave;
   1075   1.8  christos   int i;
   1076   1.8  christos   /* In 64-bit mode the split between "low" and "high" ZMM registers is at
   1077   1.7  christos      ZMM16.  Outside of 64-bit mode there are no "high" ZMM registers at all.
   1078   1.7  christos      Precalculate the number to be used for the split point, with the all
   1079   1.1  christos      registers in the "low" portion outside of 64-bit mode.  */
   1080   1.1  christos   unsigned int zmm_endlo_regnum = I387_ZMM0H_REGNUM (tdep)
   1081   1.1  christos 				  + std::min (tdep->num_zmm_regs, 16);
   1082   1.1  christos   ULONGEST clear_bv;
   1083   1.1  christos   static const gdb_byte zero[I386_MAX_REGISTER_SIZE] = { 0 };
   1084   1.1  christos   enum
   1085  1.11  christos     {
   1086  1.11  christos       none = 0x0,
   1087  1.11  christos       x87 = 0x1,
   1088  1.11  christos       sse = 0x2,
   1089  1.11  christos       avxh = 0x4,
   1090  1.11  christos       bndregs = 0x8,
   1091  1.11  christos       bndcfg = 0x10,
   1092  1.11  christos       avx512_k = 0x20,
   1093  1.11  christos       avx512_zmm0_h = 0x40,
   1094  1.11  christos       avx512_zmm16_h = 0x80,
   1095   1.1  christos       avx512_ymmh_avx512 = 0x100,
   1096   1.1  christos       avx512_xmm_avx512 = 0x200,
   1097   1.1  christos       pkeys = 0x400,
   1098   1.1  christos       all = x87 | sse | avxh | bndregs | bndcfg | avx512_k | avx512_zmm0_h
   1099   1.1  christos 	    | avx512_zmm16_h | avx512_ymmh_avx512 | avx512_xmm_avx512 | pkeys
   1100   1.1  christos     } regclass;
   1101   1.1  christos 
   1102   1.1  christos   gdb_assert (regs != NULL);
   1103   1.7  christos   gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
   1104   1.7  christos   gdb_assert (tdep->num_xmm_regs > 0);
   1105   1.7  christos 
   1106   1.3  christos   if (regnum == -1)
   1107  1.11  christos     regclass = all;
   1108  1.11  christos   else if (regnum >= I387_PKRU_REGNUM (tdep)
   1109  1.11  christos 	   && regnum < I387_PKEYSEND_REGNUM (tdep))
   1110   1.3  christos     regclass = pkeys;
   1111  1.11  christos   else if (regnum >= I387_ZMM0H_REGNUM (tdep)
   1112   1.3  christos 	   && regnum < I387_ZMM16H_REGNUM (tdep))
   1113   1.3  christos     regclass = avx512_zmm0_h;
   1114   1.3  christos   else if (regnum >= I387_ZMM16H_REGNUM (tdep)
   1115   1.3  christos 	   && regnum < I387_ZMMENDH_REGNUM (tdep))
   1116   1.3  christos     regclass = avx512_zmm16_h;
   1117   1.3  christos   else if (regnum >= I387_K0_REGNUM (tdep)
   1118   1.3  christos 	   && regnum < I387_KEND_REGNUM (tdep))
   1119   1.3  christos     regclass = avx512_k;
   1120   1.3  christos   else if (regnum >= I387_YMM16H_REGNUM (tdep)
   1121   1.1  christos 	   && regnum < I387_YMMH_AVX512_END_REGNUM (tdep))
   1122   1.1  christos     regclass = avx512_ymmh_avx512;
   1123   1.1  christos   else if (regnum >= I387_XMM16_REGNUM (tdep)
   1124   1.1  christos 	   && regnum < I387_XMM_AVX512_END_REGNUM (tdep))
   1125  1.11  christos     regclass = avx512_xmm_avx512;
   1126  1.11  christos   else if (regnum >= I387_YMM0H_REGNUM (tdep)
   1127  1.11  christos 	   && regnum < I387_YMMENDH_REGNUM (tdep))
   1128   1.1  christos     regclass = avxh;
   1129  1.11  christos   else if (regnum >= I387_BND0R_REGNUM (tdep)
   1130   1.3  christos 	   && regnum < I387_BNDCFGU_REGNUM (tdep))
   1131   1.1  christos     regclass = bndregs;
   1132   1.1  christos   else if (regnum >= I387_BNDCFGU_REGNUM (tdep)
   1133   1.1  christos 	   && regnum < I387_MPXEND_REGNUM (tdep))
   1134   1.1  christos     regclass = bndcfg;
   1135   1.1  christos   else if (regnum >= I387_XMM0_REGNUM (tdep)
   1136   1.1  christos 	   && regnum < I387_MXCSR_REGNUM (tdep))
   1137   1.1  christos     regclass = sse;
   1138   1.1  christos   else if (regnum >= I387_ST0_REGNUM (tdep)
   1139   1.8  christos 	   && regnum < I387_FCTRL_REGNUM (tdep))
   1140   1.1  christos     regclass = x87;
   1141   1.1  christos   else
   1142   1.1  christos     regclass = none;
   1143   1.1  christos 
   1144   1.1  christos   clear_bv = i387_xsave_get_clear_bv (gdbarch, xsave);
   1145   1.1  christos 
   1146   1.1  christos   /* With the delayed xsave mechanism, in between the program
   1147   1.1  christos      starting, and the program accessing the vector registers for the
   1148   1.1  christos      first time, the register's values are invalid.  The kernel
   1149   1.1  christos      initializes register states to zero when they are set the first
   1150   1.1  christos      time in a program.  This means that from the user-space programs'
   1151   1.1  christos      perspective, it's the same as if the registers have always been
   1152   1.1  christos      zero from the start of the program.  Therefore, the debugger
   1153   1.1  christos      should provide the same illusion to the user.  */
   1154   1.1  christos 
   1155   1.7  christos   switch (regclass)
   1156   1.7  christos     {
   1157   1.8  christos     case none:
   1158   1.7  christos       break;
   1159   1.8  christos 
   1160   1.7  christos     case pkeys:
   1161   1.7  christos       if ((clear_bv & X86_XSTATE_PKRU))
   1162  1.11  christos 	regcache->raw_supply (regnum, zero);
   1163  1.11  christos       else
   1164   1.8  christos 	regcache->raw_supply (regnum, XSAVE_PKEYS_ADDR (tdep, regs, regnum));
   1165   1.3  christos       return;
   1166   1.8  christos 
   1167  1.11  christos     case avx512_zmm0_h:
   1168  1.11  christos       if ((clear_bv & X86_XSTATE_ZMM_H))
   1169  1.11  christos 	regcache->raw_supply (regnum, zero);
   1170  1.11  christos       else
   1171  1.11  christos 	regcache->raw_supply (regnum,
   1172  1.11  christos 			      XSAVE_AVX512_ZMM0_H_ADDR (tdep, regs, regnum));
   1173  1.11  christos       return;
   1174  1.11  christos 
   1175  1.11  christos     case avx512_zmm16_h:
   1176   1.3  christos       if ((clear_bv & X86_XSTATE_ZMM))
   1177   1.3  christos 	regcache->raw_supply (regnum, zero);
   1178   1.3  christos       else
   1179   1.3  christos 	regcache->raw_supply (regnum,
   1180   1.8  christos 			      XSAVE_AVX512_ZMM16_H_ADDR (tdep, regs, regnum));
   1181   1.3  christos       return;
   1182   1.8  christos 
   1183   1.3  christos     case avx512_k:
   1184   1.3  christos       if ((clear_bv & X86_XSTATE_K))
   1185   1.3  christos 	regcache->raw_supply (regnum, zero);
   1186   1.3  christos       else
   1187   1.8  christos 	regcache->raw_supply (regnum, XSAVE_AVX512_K_ADDR (tdep, regs, regnum));
   1188   1.3  christos       return;
   1189   1.8  christos 
   1190   1.8  christos     case avx512_ymmh_avx512:
   1191   1.3  christos       if ((clear_bv & X86_XSTATE_ZMM))
   1192   1.3  christos 	regcache->raw_supply (regnum, zero);
   1193   1.3  christos       else
   1194   1.3  christos 	regcache->raw_supply (regnum,
   1195   1.8  christos 			      XSAVE_YMM_AVX512_ADDR (tdep, regs, regnum));
   1196   1.3  christos       return;
   1197   1.8  christos 
   1198   1.8  christos     case avx512_xmm_avx512:
   1199   1.3  christos       if ((clear_bv & X86_XSTATE_ZMM))
   1200   1.3  christos 	regcache->raw_supply (regnum, zero);
   1201   1.1  christos       else
   1202   1.3  christos 	regcache->raw_supply (regnum,
   1203   1.8  christos 			      XSAVE_XMM_AVX512_ADDR (tdep, regs, regnum));
   1204   1.1  christos       return;
   1205   1.8  christos 
   1206   1.1  christos     case avxh:
   1207   1.1  christos       if ((clear_bv & X86_XSTATE_AVX))
   1208  1.11  christos 	regcache->raw_supply (regnum, zero);
   1209  1.11  christos       else
   1210  1.11  christos 	regcache->raw_supply (regnum, XSAVE_AVXH_ADDR (tdep, regs, regnum));
   1211  1.11  christos       return;
   1212  1.11  christos 
   1213  1.11  christos     case bndcfg:
   1214  1.11  christos       if ((clear_bv & X86_XSTATE_BNDCFG))
   1215  1.11  christos 	regcache->raw_supply (regnum, zero);
   1216   1.3  christos       else
   1217   1.8  christos 	regcache->raw_supply (regnum, XSAVE_BNDCFG_ADDR (tdep, regs, regnum));
   1218   1.1  christos       return;
   1219  1.11  christos 
   1220   1.1  christos     case bndregs:
   1221   1.1  christos       if ((clear_bv & X86_XSTATE_BNDREGS))
   1222   1.1  christos 	regcache->raw_supply (regnum, zero);
   1223   1.3  christos       else
   1224   1.8  christos 	regcache->raw_supply (regnum, XSAVE_BNDREGS_ADDR (tdep, regs, regnum));
   1225   1.1  christos       return;
   1226   1.8  christos 
   1227   1.1  christos     case sse:
   1228   1.1  christos       if ((clear_bv & X86_XSTATE_SSE))
   1229   1.1  christos 	regcache->raw_supply (regnum, zero);
   1230   1.3  christos       else
   1231   1.8  christos 	regcache->raw_supply (regnum, FXSAVE_ADDR (tdep, regs, regnum));
   1232   1.1  christos       return;
   1233   1.8  christos 
   1234   1.1  christos     case x87:
   1235   1.1  christos       if ((clear_bv & X86_XSTATE_X87))
   1236   1.1  christos 	regcache->raw_supply (regnum, zero);
   1237   1.7  christos       else
   1238   1.7  christos 	regcache->raw_supply (regnum, FXSAVE_ADDR (tdep, regs, regnum));
   1239   1.7  christos       return;
   1240   1.7  christos 
   1241   1.7  christos     case all:
   1242   1.7  christos       /* Handle PKEYS registers.  */
   1243   1.7  christos       if ((tdep->xcr0 & X86_XSTATE_PKRU))
   1244   1.7  christos 	{
   1245   1.8  christos 	  if ((clear_bv & X86_XSTATE_PKRU))
   1246   1.7  christos 	    {
   1247   1.7  christos 	      for (i = I387_PKRU_REGNUM (tdep);
   1248   1.7  christos 		   i < I387_PKEYSEND_REGNUM (tdep);
   1249   1.7  christos 		   i++)
   1250   1.7  christos 		regcache->raw_supply (i, zero);
   1251   1.7  christos 	    }
   1252   1.8  christos 	  else
   1253   1.7  christos 	    {
   1254   1.7  christos 	      for (i = I387_PKRU_REGNUM (tdep);
   1255   1.7  christos 		   i < I387_PKEYSEND_REGNUM (tdep);
   1256   1.8  christos 		   i++)
   1257   1.8  christos 		regcache->raw_supply (i, XSAVE_PKEYS_ADDR (tdep, regs, i));
   1258   1.3  christos 	    }
   1259   1.8  christos 	}
   1260   1.3  christos 
   1261   1.8  christos       /* Handle the upper halves of the low 8/16 ZMM registers.  */
   1262   1.8  christos       if ((tdep->xcr0 & X86_XSTATE_ZMM_H))
   1263   1.3  christos 	{
   1264   1.3  christos 	  if ((clear_bv & X86_XSTATE_ZMM_H))
   1265   1.3  christos 	    {
   1266   1.8  christos 	      for (i = I387_ZMM0H_REGNUM (tdep); i < zmm_endlo_regnum; i++)
   1267   1.8  christos 		regcache->raw_supply (i, zero);
   1268  1.11  christos 	    }
   1269   1.3  christos 	  else
   1270   1.3  christos 	    {
   1271   1.3  christos 	      for (i = I387_ZMM0H_REGNUM (tdep); i < zmm_endlo_regnum; i++)
   1272   1.3  christos 		regcache->raw_supply (i,
   1273   1.3  christos 				      XSAVE_AVX512_ZMM0_H_ADDR (tdep, regs, i));
   1274   1.3  christos 	    }
   1275   1.3  christos 	}
   1276   1.3  christos 
   1277   1.3  christos       /* Handle AVX512 OpMask registers.  */
   1278   1.3  christos       if ((tdep->xcr0 & X86_XSTATE_K))
   1279   1.3  christos 	{
   1280   1.8  christos 	  if ((clear_bv & X86_XSTATE_K))
   1281   1.3  christos 	    {
   1282   1.3  christos 	      for (i = I387_K0_REGNUM (tdep);
   1283   1.3  christos 		   i < I387_KEND_REGNUM (tdep);
   1284   1.3  christos 		   i++)
   1285   1.3  christos 		regcache->raw_supply (i, zero);
   1286   1.3  christos 	    }
   1287   1.8  christos 	  else
   1288   1.3  christos 	    {
   1289   1.3  christos 	      for (i = I387_K0_REGNUM (tdep);
   1290   1.3  christos 		   i < I387_KEND_REGNUM (tdep);
   1291   1.8  christos 		   i++)
   1292   1.3  christos 		regcache->raw_supply (i, XSAVE_AVX512_K_ADDR (tdep, regs, i));
   1293   1.3  christos 	    }
   1294   1.3  christos 	}
   1295   1.3  christos 
   1296  1.11  christos       /* Handle the upper 16 ZMM/YMM/XMM registers (if any).  */
   1297  1.11  christos       if ((tdep->xcr0 & X86_XSTATE_ZMM))
   1298   1.8  christos 	{
   1299   1.3  christos 	  if ((clear_bv & X86_XSTATE_ZMM))
   1300   1.3  christos 	    {
   1301   1.3  christos 	      for (i = I387_ZMM16H_REGNUM (tdep);
   1302   1.8  christos 		   i < I387_ZMMENDH_REGNUM (tdep); i++)
   1303   1.3  christos 		regcache->raw_supply (i, zero);
   1304   1.3  christos 	      for (i = I387_YMM16H_REGNUM (tdep);
   1305   1.3  christos 		   i < I387_YMMH_AVX512_END_REGNUM (tdep);
   1306   1.8  christos 		   i++)
   1307   1.3  christos 		regcache->raw_supply (i, zero);
   1308   1.3  christos 	      for (i = I387_XMM16_REGNUM (tdep);
   1309   1.3  christos 		   i < I387_XMM_AVX512_END_REGNUM (tdep);
   1310  1.11  christos 		   i++)
   1311  1.11  christos 		regcache->raw_supply (i, zero);
   1312   1.8  christos 	    }
   1313  1.11  christos 	  else
   1314   1.3  christos 	    {
   1315   1.3  christos 	      for (i = I387_ZMM16H_REGNUM (tdep);
   1316   1.3  christos 		   i < I387_ZMMENDH_REGNUM (tdep); i++)
   1317   1.8  christos 		regcache->raw_supply (i,
   1318   1.3  christos 				      XSAVE_AVX512_ZMM16_H_ADDR (tdep, regs, i));
   1319   1.3  christos 	      for (i = I387_YMM16H_REGNUM (tdep);
   1320   1.3  christos 		   i < I387_YMMH_AVX512_END_REGNUM (tdep);
   1321   1.8  christos 		   i++)
   1322   1.3  christos 		regcache->raw_supply (i, XSAVE_YMM_AVX512_ADDR (tdep, regs, i));
   1323   1.3  christos 	      for (i = I387_XMM16_REGNUM (tdep);
   1324   1.1  christos 		   i < I387_XMM_AVX512_END_REGNUM (tdep);
   1325   1.3  christos 		   i++)
   1326   1.1  christos 		regcache->raw_supply (i, XSAVE_XMM_AVX512_ADDR (tdep, regs, i));
   1327   1.3  christos 	    }
   1328   1.1  christos 	}
   1329   1.1  christos       /* Handle the upper YMM registers.  */
   1330   1.1  christos       if ((tdep->xcr0 & X86_XSTATE_AVX))
   1331   1.1  christos 	{
   1332   1.8  christos 	  if ((clear_bv & X86_XSTATE_AVX))
   1333   1.1  christos 	    {
   1334   1.1  christos 	      for (i = I387_YMM0H_REGNUM (tdep);
   1335   1.1  christos 		   i < I387_YMMENDH_REGNUM (tdep);
   1336   1.1  christos 		   i++)
   1337   1.1  christos 		regcache->raw_supply (i, zero);
   1338   1.1  christos 	    }
   1339   1.8  christos 	  else
   1340   1.1  christos 	    {
   1341   1.1  christos 	      for (i = I387_YMM0H_REGNUM (tdep);
   1342   1.1  christos 		   i < I387_YMMENDH_REGNUM (tdep);
   1343   1.1  christos 		   i++)
   1344   1.3  christos 		regcache->raw_supply (i, XSAVE_AVXH_ADDR (tdep, regs, i));
   1345   1.1  christos 	    }
   1346   1.3  christos 	}
   1347   1.1  christos 
   1348   1.1  christos       /* Handle the MPX registers.  */
   1349   1.1  christos       if ((tdep->xcr0 & X86_XSTATE_BNDREGS))
   1350   1.8  christos 	{
   1351   1.1  christos 	  if (clear_bv & X86_XSTATE_BNDREGS)
   1352   1.1  christos 	    {
   1353   1.1  christos 	      for (i = I387_BND0R_REGNUM (tdep);
   1354   1.1  christos 		   i < I387_BNDCFGU_REGNUM (tdep); i++)
   1355   1.1  christos 		regcache->raw_supply (i, zero);
   1356  1.11  christos 	    }
   1357   1.1  christos 	  else
   1358   1.1  christos 	    {
   1359   1.1  christos 	      for (i = I387_BND0R_REGNUM (tdep);
   1360   1.1  christos 		   i < I387_BNDCFGU_REGNUM (tdep); i++)
   1361   1.3  christos 		regcache->raw_supply (i, XSAVE_BNDREGS_ADDR (tdep, regs, i));
   1362   1.1  christos 	    }
   1363   1.3  christos 	}
   1364   1.1  christos 
   1365   1.1  christos       /* Handle the MPX registers.  */
   1366   1.1  christos       if ((tdep->xcr0 & X86_XSTATE_BNDCFG))
   1367   1.8  christos 	{
   1368   1.1  christos 	  if (clear_bv & X86_XSTATE_BNDCFG)
   1369   1.1  christos 	    {
   1370   1.1  christos 	      for (i = I387_BNDCFGU_REGNUM (tdep);
   1371   1.1  christos 		   i < I387_MPXEND_REGNUM (tdep); i++)
   1372   1.1  christos 		regcache->raw_supply (i, zero);
   1373  1.11  christos 	    }
   1374   1.1  christos 	  else
   1375   1.1  christos 	    {
   1376   1.1  christos 	      for (i = I387_BNDCFGU_REGNUM (tdep);
   1377   1.1  christos 		   i < I387_MPXEND_REGNUM (tdep); i++)
   1378   1.3  christos 		regcache->raw_supply (i, XSAVE_BNDCFG_ADDR (tdep, regs, i));
   1379   1.1  christos 	    }
   1380   1.3  christos 	}
   1381   1.1  christos 
   1382   1.1  christos       /* Handle the XMM registers.  */
   1383   1.1  christos       if ((tdep->xcr0 & X86_XSTATE_SSE))
   1384   1.1  christos 	{
   1385   1.8  christos 	  if ((clear_bv & X86_XSTATE_SSE))
   1386   1.1  christos 	    {
   1387   1.1  christos 	      for (i = I387_XMM0_REGNUM (tdep);
   1388   1.1  christos 		   i < I387_MXCSR_REGNUM (tdep);
   1389   1.1  christos 		   i++)
   1390   1.1  christos 		regcache->raw_supply (i, zero);
   1391   1.8  christos 	    }
   1392   1.1  christos 	  else
   1393   1.1  christos 	    {
   1394   1.1  christos 	      for (i = I387_XMM0_REGNUM (tdep);
   1395   1.1  christos 		   i < I387_MXCSR_REGNUM (tdep); i++)
   1396   1.3  christos 		regcache->raw_supply (i, FXSAVE_ADDR (tdep, regs, i));
   1397   1.1  christos 	    }
   1398   1.3  christos 	}
   1399   1.1  christos 
   1400   1.1  christos       /* Handle the x87 registers.  */
   1401   1.1  christos       if ((tdep->xcr0 & X86_XSTATE_X87))
   1402   1.1  christos 	{
   1403   1.8  christos 	  if ((clear_bv & X86_XSTATE_X87))
   1404   1.1  christos 	    {
   1405   1.1  christos 	      for (i = I387_ST0_REGNUM (tdep);
   1406   1.1  christos 		   i < I387_FCTRL_REGNUM (tdep);
   1407   1.1  christos 		   i++)
   1408   1.1  christos 		regcache->raw_supply (i, zero);
   1409   1.1  christos 	    }
   1410   1.8  christos 	  else
   1411   1.1  christos 	    {
   1412   1.1  christos 	      for (i = I387_ST0_REGNUM (tdep);
   1413   1.1  christos 		   i < I387_FCTRL_REGNUM (tdep);
   1414   1.1  christos 		   i++)
   1415   1.1  christos 		regcache->raw_supply (i, FXSAVE_ADDR (tdep, regs, i));
   1416   1.1  christos 	    }
   1417   1.1  christos 	}
   1418   1.1  christos       break;
   1419   1.1  christos     }
   1420   1.8  christos 
   1421   1.8  christos   /* Only handle x87 control registers.  */
   1422   1.8  christos   for (i = I387_FCTRL_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
   1423   1.8  christos     if (regnum == -1 || regnum == i)
   1424   1.8  christos       {
   1425   1.8  christos 	if (clear_bv & X86_XSTATE_X87)
   1426   1.8  christos 	  {
   1427   1.8  christos 	    if (i == I387_FCTRL_REGNUM (tdep))
   1428   1.8  christos 	      {
   1429   1.8  christos 		gdb_byte buf[4];
   1430   1.8  christos 
   1431   1.8  christos 		store_unsigned_integer (buf, 4, byte_order,
   1432   1.8  christos 					I387_FCTRL_INIT_VAL);
   1433   1.8  christos 		regcache->raw_supply (i, buf);
   1434   1.8  christos 	      }
   1435   1.8  christos 	    else if (i == I387_FTAG_REGNUM (tdep))
   1436   1.8  christos 	      {
   1437   1.8  christos 		gdb_byte buf[4];
   1438   1.8  christos 
   1439   1.8  christos 		store_unsigned_integer (buf, 4, byte_order, 0xffff);
   1440   1.1  christos 		regcache->raw_supply (i, buf);
   1441   1.1  christos 	      }
   1442   1.8  christos 	    else
   1443   1.8  christos 	      regcache->raw_supply (i, zero);
   1444   1.1  christos 	  }
   1445   1.1  christos 	/* Most of the FPU control registers occupy only 16 bits in
   1446   1.1  christos 	   the xsave extended state.  Give those a special treatment.  */
   1447   1.1  christos 	else if (i != I387_FIOFF_REGNUM (tdep)
   1448   1.1  christos 		 && i != I387_FOOFF_REGNUM (tdep))
   1449   1.1  christos 	  {
   1450   1.1  christos 	    gdb_byte val[4];
   1451   1.8  christos 
   1452   1.1  christos 	    memcpy (val, FXSAVE_ADDR (tdep, regs, i), 2);
   1453   1.1  christos 	    val[2] = val[3] = 0;
   1454   1.1  christos 	    if (i == I387_FOP_REGNUM (tdep))
   1455   1.1  christos 	      val[1] &= ((1 << 3) - 1);
   1456   1.1  christos 	    else if (i == I387_FTAG_REGNUM (tdep))
   1457   1.1  christos 	      {
   1458   1.1  christos 		/* The fxsave area contains a simplified version of
   1459   1.1  christos 		   the tag word.  We have to look at the actual 80-bit
   1460   1.1  christos 		   FP data to recreate the traditional i387 tag word.  */
   1461   1.1  christos 
   1462   1.1  christos 		unsigned long ftag = 0;
   1463   1.1  christos 		int fpreg;
   1464   1.1  christos 		int top;
   1465   1.1  christos 
   1466   1.1  christos 		top = ((FXSAVE_ADDR (tdep, regs,
   1467   1.1  christos 				     I387_FSTAT_REGNUM (tdep)))[1] >> 3);
   1468   1.1  christos 		top &= 0x7;
   1469   1.1  christos 
   1470   1.1  christos 		for (fpreg = 7; fpreg >= 0; fpreg--)
   1471   1.1  christos 		  {
   1472   1.1  christos 		    int tag;
   1473   1.1  christos 
   1474   1.1  christos 		    if (val[0] & (1 << fpreg))
   1475   1.1  christos 		      {
   1476   1.1  christos 			int thisreg = (fpreg + 8 - top) % 8
   1477   1.1  christos 				       + I387_ST0_REGNUM (tdep);
   1478   1.1  christos 			tag = i387_tag (FXSAVE_ADDR (tdep, regs, thisreg));
   1479   1.1  christos 		      }
   1480   1.1  christos 		    else
   1481   1.1  christos 		      tag = 3;		/* Empty */
   1482   1.1  christos 
   1483   1.8  christos 		    ftag |= tag << (2 * fpreg);
   1484   1.1  christos 		  }
   1485   1.8  christos 		val[0] = ftag & 0xff;
   1486   1.8  christos 		val[1] = (ftag >> 8) & 0xff;
   1487   1.1  christos 	      }
   1488   1.1  christos 	    regcache->raw_supply (i, val);
   1489   1.1  christos 	  }
   1490   1.8  christos 	else
   1491   1.8  christos 	  regcache->raw_supply (i, FXSAVE_ADDR (tdep, regs, i));
   1492   1.8  christos       }
   1493   1.8  christos 
   1494   1.8  christos   if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
   1495   1.8  christos     {
   1496   1.8  christos       /* The MXCSR register is placed into the xsave buffer if either the
   1497   1.8  christos 	 AVX or SSE features are enabled.  */
   1498   1.8  christos       if ((clear_bv & (X86_XSTATE_AVX | X86_XSTATE_SSE))
   1499   1.8  christos 	  == (X86_XSTATE_AVX | X86_XSTATE_SSE))
   1500   1.8  christos 	{
   1501   1.8  christos 	  gdb_byte buf[4];
   1502   1.8  christos 
   1503   1.8  christos 	  store_unsigned_integer (buf, 4, byte_order, I387_MXCSR_INIT_VAL);
   1504   1.8  christos 	  regcache->raw_supply (I387_MXCSR_REGNUM (tdep), buf);
   1505   1.1  christos 	}
   1506   1.1  christos       else
   1507   1.1  christos 	regcache->raw_supply (I387_MXCSR_REGNUM (tdep),
   1508   1.1  christos 			      FXSAVE_MXCSR_ADDR (regs));
   1509   1.1  christos     }
   1510   1.1  christos }
   1511   1.1  christos 
   1512   1.1  christos /* Similar to i387_collect_fxsave, but use XSAVE extended state.  */
   1513   1.8  christos 
   1514   1.8  christos void
   1515  1.10  christos i387_collect_xsave (const struct regcache *regcache, int regnum,
   1516   1.8  christos 		    void *xsave, int gcore)
   1517   1.8  christos {
   1518   1.8  christos   struct gdbarch *gdbarch = regcache->arch ();
   1519   1.8  christos   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
   1520   1.8  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
   1521   1.8  christos   gdb_byte *p, *regs = (gdb_byte *) xsave;
   1522   1.8  christos   gdb_byte raw[I386_MAX_REGISTER_SIZE];
   1523   1.1  christos   ULONGEST initial_xstate_bv, clear_bv, xstate_bv = 0;
   1524   1.1  christos   unsigned int i;
   1525   1.8  christos   /* See the comment in i387_supply_xsave().  */
   1526   1.8  christos   unsigned int zmm_endlo_regnum = I387_ZMM0H_REGNUM (tdep)
   1527   1.8  christos 				  + std::min (tdep->num_zmm_regs, 16);
   1528   1.8  christos   enum
   1529  1.11  christos     {
   1530  1.11  christos       x87_ctrl_or_mxcsr = 0x1,
   1531  1.11  christos       x87 = 0x2,
   1532  1.11  christos       sse = 0x4,
   1533  1.11  christos       avxh = 0x8,
   1534  1.11  christos       bndregs = 0x10,
   1535  1.11  christos       bndcfg = 0x20,
   1536  1.11  christos       avx512_k = 0x40,
   1537  1.11  christos       avx512_zmm0_h = 0x80,
   1538  1.11  christos       avx512_zmm16_h = 0x100,
   1539   1.1  christos       avx512_ymmh_avx512 = 0x200,
   1540   1.1  christos       avx512_xmm_avx512 = 0x400,
   1541   1.1  christos       pkeys = 0x800,
   1542   1.1  christos       all = x87 | sse | avxh | bndregs | bndcfg | avx512_k | avx512_zmm0_h
   1543   1.1  christos 	    | avx512_zmm16_h | avx512_ymmh_avx512 | avx512_xmm_avx512 | pkeys
   1544   1.1  christos     } regclass;
   1545   1.1  christos 
   1546   1.7  christos   gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
   1547   1.7  christos   gdb_assert (tdep->num_xmm_regs > 0);
   1548   1.7  christos 
   1549   1.3  christos   if (regnum == -1)
   1550  1.11  christos     regclass = all;
   1551  1.11  christos   else if (regnum >= I387_PKRU_REGNUM (tdep)
   1552  1.11  christos 	   && regnum < I387_PKEYSEND_REGNUM (tdep))
   1553   1.3  christos     regclass = pkeys;
   1554  1.11  christos   else if (regnum >= I387_ZMM0H_REGNUM (tdep)
   1555   1.3  christos 	   && regnum < I387_ZMM16H_REGNUM (tdep))
   1556   1.3  christos     regclass = avx512_zmm0_h;
   1557   1.3  christos   else if (regnum >= I387_ZMM16H_REGNUM (tdep)
   1558   1.3  christos 	   && regnum < I387_ZMMENDH_REGNUM (tdep))
   1559   1.3  christos     regclass = avx512_zmm16_h;
   1560   1.3  christos   else if (regnum >= I387_K0_REGNUM (tdep)
   1561   1.3  christos 	   && regnum < I387_KEND_REGNUM (tdep))
   1562   1.3  christos     regclass = avx512_k;
   1563   1.3  christos   else if (regnum >= I387_YMM16H_REGNUM (tdep)
   1564   1.1  christos 	   && regnum < I387_YMMH_AVX512_END_REGNUM (tdep))
   1565   1.1  christos     regclass = avx512_ymmh_avx512;
   1566   1.1  christos   else if (regnum >= I387_XMM16_REGNUM (tdep)
   1567   1.1  christos 	   && regnum < I387_XMM_AVX512_END_REGNUM (tdep))
   1568  1.11  christos     regclass = avx512_xmm_avx512;
   1569  1.11  christos   else if (regnum >= I387_YMM0H_REGNUM (tdep)
   1570  1.11  christos 	   && regnum < I387_YMMENDH_REGNUM (tdep))
   1571   1.1  christos     regclass = avxh;
   1572  1.11  christos   else if (regnum >= I387_BND0R_REGNUM (tdep)
   1573   1.1  christos 	   && regnum < I387_BNDCFGU_REGNUM (tdep))
   1574   1.1  christos     regclass = bndregs;
   1575   1.1  christos   else if (regnum >= I387_BNDCFGU_REGNUM (tdep)
   1576   1.1  christos 	   && regnum < I387_MPXEND_REGNUM (tdep))
   1577   1.1  christos     regclass = bndcfg;
   1578   1.1  christos   else if (regnum >= I387_XMM0_REGNUM (tdep)
   1579   1.8  christos 	   && regnum < I387_MXCSR_REGNUM (tdep))
   1580   1.8  christos     regclass = sse;
   1581   1.8  christos   else if (regnum >= I387_ST0_REGNUM (tdep)
   1582   1.8  christos 	   && regnum < I387_FCTRL_REGNUM (tdep))
   1583   1.1  christos     regclass = x87;
   1584  1.10  christos   else if ((regnum >= I387_FCTRL_REGNUM (tdep)
   1585   1.1  christos 	    && regnum < I387_XMM0_REGNUM (tdep))
   1586   1.1  christos 	   || regnum == I387_MXCSR_REGNUM (tdep))
   1587   1.1  christos     regclass = x87_ctrl_or_mxcsr;
   1588   1.1  christos   else
   1589  1.11  christos     internal_error (_("invalid i387 regnum %d"), regnum);
   1590   1.1  christos 
   1591   1.1  christos   if (gcore)
   1592   1.1  christos     {
   1593   1.1  christos       /* Clear XSAVE extended state.  */
   1594   1.1  christos       memset (regs, 0, tdep->xsave_layout.sizeof_xsave);
   1595   1.1  christos 
   1596   1.1  christos       /* Update XCR0 and `xstate_bv' with XCR0 for gcore.  */
   1597   1.8  christos       if (tdep->xsave_xcr0_offset != -1)
   1598   1.8  christos 	memcpy (regs + tdep->xsave_xcr0_offset, &tdep->xcr0, 8);
   1599   1.8  christos       memcpy (XSAVE_XSTATE_BV_ADDR (regs), &tdep->xcr0, 8);
   1600   1.8  christos     }
   1601   1.8  christos 
   1602   1.8  christos   /* The supported bits in `xstat_bv' are 8 bytes.  */
   1603   1.8  christos   initial_xstate_bv = extract_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs),
   1604   1.8  christos 						8, byte_order);
   1605   1.8  christos   clear_bv = (~(initial_xstate_bv)) & tdep->xcr0;
   1606   1.8  christos 
   1607   1.8  christos   /* The XSAVE buffer was filled lazily by the kernel.  Only those
   1608   1.8  christos      features that are enabled were written into the buffer, disabled
   1609   1.8  christos      features left the buffer uninitialised.  In order to identify if any
   1610   1.8  christos      registers have changed we will be comparing the register cache
   1611   1.8  christos      version to the version in the XSAVE buffer, it is important then that
   1612   1.8  christos      at this point we initialise to the default values any features in
   1613   1.8  christos      XSAVE that are not yet initialised.
   1614   1.8  christos 
   1615   1.1  christos      This could be made more efficient, we know which features (from
   1616   1.8  christos      REGNUM) we will be potentially updating, and could limit ourselves to
   1617   1.8  christos      only clearing that feature.  However, the extra complexity does not
   1618   1.8  christos      seem justified at this point.  */
   1619   1.8  christos   if (clear_bv)
   1620   1.8  christos     {
   1621   1.8  christos       if ((clear_bv & X86_XSTATE_PKRU))
   1622   1.8  christos 	for (i = I387_PKRU_REGNUM (tdep);
   1623   1.8  christos 	     i < I387_PKEYSEND_REGNUM (tdep); i++)
   1624  1.11  christos 	  memset (XSAVE_PKEYS_ADDR (tdep, regs, i), 0, 4);
   1625   1.8  christos 
   1626   1.8  christos       if ((clear_bv & X86_XSTATE_BNDREGS))
   1627   1.8  christos 	for (i = I387_BND0R_REGNUM (tdep);
   1628   1.8  christos 	     i < I387_BNDCFGU_REGNUM (tdep); i++)
   1629  1.11  christos 	  memset (XSAVE_BNDREGS_ADDR (tdep, regs, i), 0, 16);
   1630   1.8  christos 
   1631   1.8  christos       if ((clear_bv & X86_XSTATE_BNDCFG))
   1632   1.8  christos 	for (i = I387_BNDCFGU_REGNUM (tdep);
   1633  1.11  christos 	     i < I387_MPXEND_REGNUM (tdep); i++)
   1634   1.7  christos 	  memset (XSAVE_BNDCFG_ADDR (tdep, regs, i), 0, 8);
   1635   1.8  christos 
   1636   1.8  christos       if ((clear_bv & X86_XSTATE_ZMM_H))
   1637   1.8  christos 	for (i = I387_ZMM0H_REGNUM (tdep); i < zmm_endlo_regnum; i++)
   1638   1.8  christos 	  memset (XSAVE_AVX512_ZMM0_H_ADDR (tdep, regs, i), 0, 32);
   1639   1.1  christos 
   1640   1.8  christos       if ((clear_bv & X86_XSTATE_K))
   1641   1.1  christos 	for (i = I387_K0_REGNUM (tdep);
   1642  1.11  christos 	     i < I387_KEND_REGNUM (tdep); i++)
   1643  1.11  christos 	  memset (XSAVE_AVX512_K_ADDR (tdep, regs, i), 0, 8);
   1644  1.11  christos 
   1645   1.8  christos       if ((clear_bv & X86_XSTATE_ZMM))
   1646   1.8  christos 	{
   1647   1.8  christos 	  for (i = I387_ZMM16H_REGNUM (tdep); i < I387_ZMMENDH_REGNUM (tdep);
   1648   1.8  christos 	       i++)
   1649   1.8  christos 	    memset (XSAVE_AVX512_ZMM16_H_ADDR (tdep, regs, i), 0, 32);
   1650   1.8  christos 	  for (i = I387_YMM16H_REGNUM (tdep);
   1651   1.8  christos 	       i < I387_YMMH_AVX512_END_REGNUM (tdep); i++)
   1652   1.8  christos 	    memset (XSAVE_YMM_AVX512_ADDR (tdep, regs, i), 0, 16);
   1653   1.8  christos 	  for (i = I387_XMM16_REGNUM (tdep);
   1654   1.8  christos 	       i < I387_XMM_AVX512_END_REGNUM (tdep); i++)
   1655   1.8  christos 	    memset (XSAVE_XMM_AVX512_ADDR (tdep, regs, i), 0, 16);
   1656   1.8  christos 	}
   1657   1.3  christos 
   1658   1.8  christos       if ((clear_bv & X86_XSTATE_AVX))
   1659   1.8  christos 	for (i = I387_YMM0H_REGNUM (tdep);
   1660   1.8  christos 	     i < I387_YMMENDH_REGNUM (tdep); i++)
   1661   1.8  christos 	  memset (XSAVE_AVXH_ADDR (tdep, regs, i), 0, 16);
   1662   1.8  christos 
   1663   1.8  christos       if ((clear_bv & X86_XSTATE_SSE))
   1664   1.8  christos 	for (i = I387_XMM0_REGNUM (tdep);
   1665   1.8  christos 	     i < I387_MXCSR_REGNUM (tdep); i++)
   1666   1.8  christos 	  memset (FXSAVE_ADDR (tdep, regs, i), 0, 16);
   1667   1.8  christos 
   1668   1.8  christos       /* The mxcsr register is written into the xsave buffer if either AVX
   1669   1.8  christos 	 or SSE is enabled, so only clear it if both of those features
   1670   1.3  christos 	 require clearing.  */
   1671   1.8  christos       if ((clear_bv & (X86_XSTATE_AVX | X86_XSTATE_SSE))
   1672   1.8  christos 	  == (X86_XSTATE_AVX | X86_XSTATE_SSE))
   1673   1.8  christos 	store_unsigned_integer (FXSAVE_MXCSR_ADDR (regs), 2, byte_order,
   1674   1.8  christos 				I387_MXCSR_INIT_VAL);
   1675   1.8  christos 
   1676   1.8  christos       if ((clear_bv & X86_XSTATE_X87))
   1677   1.8  christos 	{
   1678   1.8  christos 	  for (i = I387_ST0_REGNUM (tdep);
   1679   1.8  christos 	       i < I387_FCTRL_REGNUM (tdep); i++)
   1680   1.8  christos 	    memset (FXSAVE_ADDR (tdep, regs, i), 0, 10);
   1681   1.8  christos 
   1682   1.8  christos 	  for (i = I387_FCTRL_REGNUM (tdep);
   1683   1.8  christos 	       i < I387_XMM0_REGNUM (tdep); i++)
   1684   1.8  christos 	    {
   1685   1.8  christos 	      if (i == I387_FCTRL_REGNUM (tdep))
   1686   1.3  christos 		store_unsigned_integer (FXSAVE_ADDR (tdep, regs, i), 2,
   1687   1.1  christos 					byte_order, I387_FCTRL_INIT_VAL);
   1688   1.8  christos 	      else
   1689   1.1  christos 		memset (FXSAVE_ADDR (tdep, regs, i), 0,
   1690   1.8  christos 			regcache_register_size (regcache, i));
   1691   1.8  christos 	    }
   1692   1.8  christos 	}
   1693   1.8  christos     }
   1694   1.8  christos 
   1695   1.8  christos   if (regclass == all)
   1696   1.8  christos     {
   1697   1.8  christos       /* Check if any PKEYS registers are changed.  */
   1698   1.8  christos       if ((tdep->xcr0 & X86_XSTATE_PKRU))
   1699   1.8  christos 	for (i = I387_PKRU_REGNUM (tdep);
   1700   1.7  christos 	     i < I387_PKEYSEND_REGNUM (tdep); i++)
   1701   1.8  christos 	  {
   1702   1.8  christos 	    regcache->raw_collect (i, raw);
   1703   1.7  christos 	    p = XSAVE_PKEYS_ADDR (tdep, regs, i);
   1704   1.8  christos 	    if (memcmp (raw, p, 4) != 0)
   1705   1.7  christos 	      {
   1706   1.8  christos 		xstate_bv |= X86_XSTATE_PKRU;
   1707  1.11  christos 		memcpy (p, raw, 4);
   1708  1.11  christos 	      }
   1709   1.8  christos 	  }
   1710   1.8  christos 
   1711   1.8  christos       /* Check if any ZMMH registers are changed.  */
   1712  1.11  christos       if ((tdep->xcr0 & X86_XSTATE_ZMM))
   1713   1.8  christos 	for (i = I387_ZMM16H_REGNUM (tdep);
   1714   1.3  christos 	     i < I387_ZMMENDH_REGNUM (tdep); i++)
   1715  1.11  christos 	  {
   1716  1.11  christos 	    regcache->raw_collect (i, raw);
   1717  1.11  christos 	    p = XSAVE_AVX512_ZMM16_H_ADDR (tdep, regs, i);
   1718  1.11  christos 	    if (memcmp (raw, p, 32) != 0)
   1719  1.11  christos 	      {
   1720  1.11  christos 		xstate_bv |= X86_XSTATE_ZMM;
   1721  1.11  christos 		memcpy (p, raw, 32);
   1722  1.11  christos 	      }
   1723  1.11  christos 	  }
   1724  1.11  christos 
   1725  1.11  christos       if ((tdep->xcr0 & X86_XSTATE_ZMM_H))
   1726  1.11  christos 	for (i = I387_ZMM0H_REGNUM (tdep); i < zmm_endlo_regnum; i++)
   1727  1.11  christos 	  {
   1728   1.8  christos 	    regcache->raw_collect (i, raw);
   1729   1.3  christos 	    p = XSAVE_AVX512_ZMM0_H_ADDR (tdep, regs, i);
   1730   1.8  christos 	    if (memcmp (raw, p, 32) != 0)
   1731   1.3  christos 	      {
   1732   1.8  christos 		xstate_bv |= X86_XSTATE_ZMM_H;
   1733   1.8  christos 		memcpy (p, raw, 32);
   1734   1.8  christos 	      }
   1735   1.8  christos 	  }
   1736   1.8  christos 
   1737   1.8  christos       /* Check if any K registers are changed.  */
   1738   1.8  christos       if ((tdep->xcr0 & X86_XSTATE_K))
   1739   1.8  christos 	for (i = I387_K0_REGNUM (tdep);
   1740   1.3  christos 	     i < I387_KEND_REGNUM (tdep); i++)
   1741   1.8  christos 	  {
   1742   1.8  christos 	    regcache->raw_collect (i, raw);
   1743   1.3  christos 	    p = XSAVE_AVX512_K_ADDR (tdep, regs, i);
   1744   1.8  christos 	    if (memcmp (raw, p, 8) != 0)
   1745   1.3  christos 	      {
   1746   1.8  christos 		xstate_bv |= X86_XSTATE_K;
   1747   1.8  christos 		memcpy (p, raw, 8);
   1748   1.8  christos 	      }
   1749   1.8  christos 	  }
   1750   1.8  christos 
   1751   1.3  christos       /* Check if any XMM or upper YMM registers are changed.  */
   1752   1.8  christos       if ((tdep->xcr0 & X86_XSTATE_ZMM))
   1753   1.8  christos 	{
   1754   1.8  christos 	  for (i = I387_YMM16H_REGNUM (tdep);
   1755   1.3  christos 	       i < I387_YMMH_AVX512_END_REGNUM (tdep); i++)
   1756   1.8  christos 	    {
   1757   1.8  christos 	      regcache->raw_collect (i, raw);
   1758   1.3  christos 	      p = XSAVE_YMM_AVX512_ADDR (tdep, regs, i);
   1759   1.8  christos 	      if (memcmp (raw, p, 16) != 0)
   1760   1.8  christos 		{
   1761   1.8  christos 		  xstate_bv |= X86_XSTATE_ZMM;
   1762   1.8  christos 		  memcpy (p, raw, 16);
   1763   1.8  christos 		}
   1764   1.8  christos 	    }
   1765   1.8  christos 	  for (i = I387_XMM16_REGNUM (tdep);
   1766   1.3  christos 	       i < I387_XMM_AVX512_END_REGNUM (tdep); i++)
   1767   1.8  christos 	    {
   1768   1.8  christos 	      regcache->raw_collect (i, raw);
   1769   1.3  christos 	      p = XSAVE_XMM_AVX512_ADDR (tdep, regs, i);
   1770   1.3  christos 	      if (memcmp (raw, p, 16) != 0)
   1771   1.8  christos 		{
   1772   1.3  christos 		  xstate_bv |= X86_XSTATE_ZMM;
   1773   1.8  christos 		  memcpy (p, raw, 16);
   1774   1.8  christos 		}
   1775   1.8  christos 	    }
   1776   1.8  christos 	}
   1777   1.8  christos 
   1778   1.8  christos       /* Check if any upper MPX registers are changed.  */
   1779  1.11  christos       if ((tdep->xcr0 & X86_XSTATE_BNDREGS))
   1780   1.8  christos 	for (i = I387_BND0R_REGNUM (tdep);
   1781   1.1  christos 	     i < I387_BNDCFGU_REGNUM (tdep); i++)
   1782   1.8  christos 	  {
   1783   1.8  christos 	    regcache->raw_collect (i, raw);
   1784   1.1  christos 	    p = XSAVE_BNDREGS_ADDR (tdep, regs, i);
   1785   1.8  christos 	    if (memcmp (raw, p, 16))
   1786   1.8  christos 	      {
   1787   1.8  christos 		xstate_bv |= X86_XSTATE_BNDREGS;
   1788   1.8  christos 		memcpy (p, raw, 16);
   1789   1.8  christos 	      }
   1790   1.8  christos 	  }
   1791   1.8  christos 
   1792   1.8  christos       /* Check if any upper MPX registers are changed.  */
   1793  1.11  christos       if ((tdep->xcr0 & X86_XSTATE_BNDCFG))
   1794   1.8  christos 	for (i = I387_BNDCFGU_REGNUM (tdep);
   1795   1.1  christos 	     i < I387_MPXEND_REGNUM (tdep); i++)
   1796   1.8  christos 	  {
   1797   1.8  christos 	    regcache->raw_collect (i, raw);
   1798   1.1  christos 	    p = XSAVE_BNDCFG_ADDR (tdep, regs, i);
   1799   1.8  christos 	    if (memcmp (raw, p, 8))
   1800   1.1  christos 	      {
   1801   1.8  christos 		xstate_bv |= X86_XSTATE_BNDCFG;
   1802   1.8  christos 		memcpy (p, raw, 8);
   1803   1.8  christos 	      }
   1804   1.8  christos 	  }
   1805   1.8  christos 
   1806   1.8  christos       /* Check if any upper YMM registers are changed.  */
   1807   1.8  christos       if ((tdep->xcr0 & X86_XSTATE_AVX))
   1808   1.8  christos 	for (i = I387_YMM0H_REGNUM (tdep);
   1809   1.1  christos 	     i < I387_YMMENDH_REGNUM (tdep); i++)
   1810   1.8  christos 	  {
   1811   1.8  christos 	    regcache->raw_collect (i, raw);
   1812   1.1  christos 	    p = XSAVE_AVXH_ADDR (tdep, regs, i);
   1813   1.8  christos 	    if (memcmp (raw, p, 16))
   1814   1.1  christos 	      {
   1815   1.8  christos 		xstate_bv |= X86_XSTATE_AVX;
   1816   1.8  christos 		memcpy (p, raw, 16);
   1817   1.8  christos 	      }
   1818   1.8  christos 	  }
   1819   1.8  christos 
   1820   1.8  christos       /* Check if any SSE registers are changed.  */
   1821   1.8  christos       if ((tdep->xcr0 & X86_XSTATE_SSE))
   1822   1.8  christos 	for (i = I387_XMM0_REGNUM (tdep);
   1823   1.1  christos 	     i < I387_MXCSR_REGNUM (tdep); i++)
   1824   1.8  christos 	  {
   1825   1.8  christos 	    regcache->raw_collect (i, raw);
   1826   1.1  christos 	    p = FXSAVE_ADDR (tdep, regs, i);
   1827   1.8  christos 	    if (memcmp (raw, p, 16))
   1828   1.1  christos 	      {
   1829   1.8  christos 		xstate_bv |= X86_XSTATE_SSE;
   1830   1.8  christos 		memcpy (p, raw, 16);
   1831   1.8  christos 	      }
   1832   1.8  christos 	  }
   1833   1.8  christos 
   1834   1.8  christos       if ((tdep->xcr0 & X86_XSTATE_AVX) || (tdep->xcr0 & X86_XSTATE_SSE))
   1835   1.8  christos 	{
   1836   1.8  christos 	  i = I387_MXCSR_REGNUM (tdep);
   1837   1.8  christos 	  regcache->raw_collect (i, raw);
   1838   1.8  christos 	  p = FXSAVE_MXCSR_ADDR (regs);
   1839   1.8  christos 	  if (memcmp (raw, p, 4))
   1840   1.8  christos 	    {
   1841   1.8  christos 	      /* Now, we need to mark one of either SSE of AVX as enabled.
   1842   1.8  christos 		 We could pick either.  What we do is check to see if one
   1843   1.8  christos 		 of the features is already enabled, if it is then we leave
   1844   1.8  christos 		 it at that, otherwise we pick SSE.  */
   1845   1.8  christos 	      if ((xstate_bv & (X86_XSTATE_SSE | X86_XSTATE_AVX)) == 0)
   1846   1.8  christos 		xstate_bv |= X86_XSTATE_SSE;
   1847   1.8  christos 	      memcpy (p, raw, 4);
   1848   1.8  christos 	    }
   1849   1.8  christos 	}
   1850   1.8  christos 
   1851   1.8  christos       /* Check if any X87 registers are changed.  Only the non-control
   1852   1.8  christos 	 registers are handled here, the control registers are all handled
   1853   1.8  christos 	 later on in this function.  */
   1854   1.8  christos       if ((tdep->xcr0 & X86_XSTATE_X87))
   1855   1.8  christos 	for (i = I387_ST0_REGNUM (tdep);
   1856   1.1  christos 	     i < I387_FCTRL_REGNUM (tdep); i++)
   1857   1.8  christos 	  {
   1858   1.8  christos 	    regcache->raw_collect (i, raw);
   1859   1.1  christos 	    p = FXSAVE_ADDR (tdep, regs, i);
   1860   1.8  christos 	    if (memcmp (raw, p, 10))
   1861   1.8  christos 	      {
   1862   1.8  christos 		xstate_bv |= X86_XSTATE_X87;
   1863   1.8  christos 		memcpy (p, raw, 10);
   1864   1.8  christos 	      }
   1865   1.8  christos 	  }
   1866   1.8  christos     }
   1867   1.8  christos   else
   1868   1.1  christos     {
   1869   1.8  christos       /* Check if REGNUM is changed.  */
   1870  1.10  christos       regcache->raw_collect (regnum, raw);
   1871   1.8  christos 
   1872   1.8  christos       switch (regclass)
   1873   1.8  christos 	{
   1874   1.8  christos 	default:
   1875   1.8  christos 	  internal_error (_("invalid i387 regclass"));
   1876   1.8  christos 
   1877   1.8  christos 	case pkeys:
   1878   1.8  christos 	  /* This is a PKEYS register.  */
   1879   1.8  christos 	  p = XSAVE_PKEYS_ADDR (tdep, regs, regnum);
   1880   1.8  christos 	  if (memcmp (raw, p, 4) != 0)
   1881   1.1  christos 	    {
   1882  1.11  christos 	      xstate_bv |= X86_XSTATE_PKRU;
   1883  1.11  christos 	      memcpy (p, raw, 4);
   1884  1.11  christos 	    }
   1885   1.8  christos 	  break;
   1886   1.8  christos 
   1887  1.11  christos 	case avx512_zmm16_h:
   1888   1.8  christos 	  /* This is a ZMM16-31 register.  */
   1889   1.8  christos 	  p = XSAVE_AVX512_ZMM16_H_ADDR (tdep, regs, regnum);
   1890   1.8  christos 	  if (memcmp (raw, p, 32) != 0)
   1891  1.11  christos 	    {
   1892  1.11  christos 	      xstate_bv |= X86_XSTATE_ZMM;
   1893  1.11  christos 	      memcpy (p, raw, 32);
   1894  1.11  christos 	    }
   1895  1.11  christos 	  break;
   1896  1.11  christos 
   1897  1.11  christos 	case avx512_zmm0_h:
   1898  1.11  christos 	  /* This is a ZMM0-15 register.  */
   1899  1.11  christos 	  p = XSAVE_AVX512_ZMM0_H_ADDR (tdep, regs, regnum);
   1900  1.11  christos 	  if (memcmp (raw, p, 32) != 0)
   1901  1.11  christos 	    {
   1902   1.8  christos 	      xstate_bv |= X86_XSTATE_ZMM_H;
   1903   1.8  christos 	      memcpy (p, raw, 32);
   1904   1.8  christos 	    }
   1905   1.8  christos 	  break;
   1906   1.1  christos 
   1907   1.8  christos 	case avx512_k:
   1908   1.8  christos 	  /* This is a AVX512 mask register.  */
   1909   1.8  christos 	  p = XSAVE_AVX512_K_ADDR (tdep, regs, regnum);
   1910   1.8  christos 	  if (memcmp (raw, p, 8) != 0)
   1911   1.7  christos 	    {
   1912   1.8  christos 	      xstate_bv |= X86_XSTATE_K;
   1913   1.8  christos 	      memcpy (p, raw, 8);
   1914   1.8  christos 	    }
   1915   1.8  christos 	  break;
   1916   1.8  christos 
   1917   1.8  christos 	case avx512_ymmh_avx512:
   1918   1.8  christos 	  /* This is an upper YMM16-31 register.  */
   1919   1.8  christos 	  p = XSAVE_YMM_AVX512_ADDR (tdep, regs, regnum);
   1920   1.8  christos 	  if (memcmp (raw, p, 16) != 0)
   1921   1.3  christos 	    {
   1922   1.8  christos 	      xstate_bv |= X86_XSTATE_ZMM;
   1923   1.8  christos 	      memcpy (p, raw, 16);
   1924   1.8  christos 	    }
   1925   1.8  christos 	  break;
   1926   1.8  christos 
   1927   1.8  christos 	case avx512_xmm_avx512:
   1928   1.8  christos 	  /* This is an upper XMM16-31 register.  */
   1929   1.8  christos 	  p = XSAVE_XMM_AVX512_ADDR (tdep, regs, regnum);
   1930   1.8  christos 	  if (memcmp (raw, p, 16) != 0)
   1931   1.3  christos 	    {
   1932   1.8  christos 	      xstate_bv |= X86_XSTATE_ZMM;
   1933   1.8  christos 	      memcpy (p, raw, 16);
   1934   1.8  christos 	    }
   1935   1.8  christos 	  break;
   1936   1.8  christos 
   1937   1.8  christos 	case avxh:
   1938   1.8  christos 	  /* This is an upper YMM register.  */
   1939   1.8  christos 	  p = XSAVE_AVXH_ADDR (tdep, regs, regnum);
   1940   1.8  christos 	  if (memcmp (raw, p, 16))
   1941   1.3  christos 	    {
   1942  1.11  christos 	      xstate_bv |= X86_XSTATE_AVX;
   1943  1.11  christos 	      memcpy (p, raw, 16);
   1944  1.11  christos 	    }
   1945  1.11  christos 	  break;
   1946   1.8  christos 
   1947  1.11  christos 	case bndregs:
   1948  1.11  christos 	  regcache->raw_collect (regnum, raw);
   1949   1.8  christos 	  p = XSAVE_BNDREGS_ADDR (tdep, regs, regnum);
   1950  1.11  christos 	  if (memcmp (raw, p, 16))
   1951  1.11  christos 	    {
   1952  1.11  christos 	      xstate_bv |= X86_XSTATE_BNDREGS;
   1953  1.11  christos 	      memcpy (p, raw, 16);
   1954  1.11  christos 	    }
   1955  1.11  christos 	  break;
   1956   1.8  christos 
   1957   1.1  christos 	case bndcfg:
   1958   1.8  christos 	  p = XSAVE_BNDCFG_ADDR (tdep, regs, regnum);
   1959   1.8  christos 	  xstate_bv |= X86_XSTATE_BNDCFG;
   1960   1.8  christos 	  memcpy (p, raw, 8);
   1961   1.8  christos 	  break;
   1962   1.8  christos 
   1963   1.8  christos 	case sse:
   1964   1.8  christos 	  /* This is an SSE register.  */
   1965   1.8  christos 	  p = FXSAVE_ADDR (tdep, regs, regnum);
   1966   1.8  christos 	  if (memcmp (raw, p, 16))
   1967   1.1  christos 	    {
   1968   1.8  christos 	      xstate_bv |= X86_XSTATE_SSE;
   1969   1.8  christos 	      memcpy (p, raw, 16);
   1970   1.8  christos 	    }
   1971   1.8  christos 	  break;
   1972   1.8  christos 
   1973   1.8  christos 	case x87:
   1974   1.8  christos 	  /* This is an x87 register.  */
   1975   1.8  christos 	  p = FXSAVE_ADDR (tdep, regs, regnum);
   1976   1.8  christos 	  if (memcmp (raw, p, 10))
   1977   1.1  christos 	    {
   1978   1.8  christos 	      xstate_bv |= X86_XSTATE_X87;
   1979   1.8  christos 	      memcpy (p, raw, 10);
   1980   1.8  christos 	    }
   1981   1.8  christos 	  break;
   1982   1.8  christos 
   1983   1.8  christos 	case x87_ctrl_or_mxcsr:
   1984   1.8  christos 	  /* We only handle MXCSR here.  All other x87 control registers
   1985   1.1  christos 	     are handled separately below.  */
   1986   1.8  christos 	  if (regnum == I387_MXCSR_REGNUM (tdep))
   1987   1.8  christos 	    {
   1988   1.8  christos 	      p = FXSAVE_MXCSR_ADDR (regs);
   1989   1.8  christos 	      if (memcmp (raw, p, 2))
   1990   1.8  christos 		{
   1991   1.8  christos 		  /* We're only setting MXCSR, so check the initial state
   1992   1.8  christos 		     to see if either of AVX or SSE are already enabled.
   1993   1.8  christos 		     If they are then we'll attribute this changed MXCSR to
   1994   1.8  christos 		     that feature.  If neither feature is enabled, then
   1995   1.8  christos 		     we'll attribute this change to the SSE feature.  */
   1996   1.1  christos 		  xstate_bv |= (initial_xstate_bv
   1997   1.1  christos 				& (X86_XSTATE_AVX | X86_XSTATE_SSE));
   1998   1.1  christos 		  if ((xstate_bv & (X86_XSTATE_AVX | X86_XSTATE_SSE)) == 0)
   1999   1.1  christos 		    xstate_bv |= X86_XSTATE_SSE;
   2000   1.1  christos 		  memcpy (p, raw, 2);
   2001   1.1  christos 		}
   2002   1.1  christos 	    }
   2003   1.1  christos 	}
   2004   1.1  christos     }
   2005   1.1  christos 
   2006   1.1  christos   /* Only handle x87 control registers.  */
   2007   1.1  christos   for (i = I387_FCTRL_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
   2008   1.1  christos     if (regnum == -1 || regnum == i)
   2009   1.1  christos       {
   2010   1.1  christos 	/* Most of the FPU control registers occupy only 16 bits in
   2011   1.1  christos 	   the xsave extended state.  Give those a special treatment.  */
   2012   1.8  christos 	if (i != I387_FIOFF_REGNUM (tdep)
   2013   1.1  christos 	    && i != I387_FOOFF_REGNUM (tdep))
   2014   1.1  christos 	  {
   2015   1.1  christos 	    gdb_byte buf[4];
   2016   1.1  christos 
   2017   1.1  christos 	    regcache->raw_collect (i, buf);
   2018   1.1  christos 
   2019   1.1  christos 	    if (i == I387_FOP_REGNUM (tdep))
   2020   1.1  christos 	      {
   2021   1.1  christos 		/* The opcode occupies only 11 bits.  Make sure we
   2022   1.1  christos 		   don't touch the other bits.  */
   2023   1.1  christos 		buf[1] &= ((1 << 3) - 1);
   2024   1.1  christos 		buf[1] |= ((FXSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
   2025   1.1  christos 	      }
   2026   1.1  christos 	    else if (i == I387_FTAG_REGNUM (tdep))
   2027   1.1  christos 	      {
   2028   1.1  christos 		/* Converting back is much easier.  */
   2029   1.1  christos 
   2030   1.1  christos 		unsigned short ftag;
   2031   1.1  christos 		int fpreg;
   2032   1.1  christos 
   2033   1.1  christos 		ftag = (buf[1] << 8) | buf[0];
   2034   1.1  christos 		buf[0] = 0;
   2035   1.1  christos 		buf[1] = 0;
   2036   1.1  christos 
   2037   1.1  christos 		for (fpreg = 7; fpreg >= 0; fpreg--)
   2038   1.1  christos 		  {
   2039   1.1  christos 		    int tag = (ftag >> (fpreg * 2)) & 3;
   2040   1.8  christos 
   2041   1.8  christos 		    if (tag != 3)
   2042   1.8  christos 		      buf[0] |= (1 << fpreg);
   2043   1.8  christos 		  }
   2044   1.8  christos 	      }
   2045   1.8  christos 	    p = FXSAVE_ADDR (tdep, regs, i);
   2046   1.1  christos 	    if (memcmp (p, buf, 2))
   2047   1.1  christos 	      {
   2048   1.8  christos 		xstate_bv |= X86_XSTATE_X87;
   2049   1.8  christos 		memcpy (p, buf, 2);
   2050   1.8  christos 	      }
   2051   1.8  christos 	  }
   2052   1.8  christos 	else
   2053   1.8  christos 	  {
   2054   1.8  christos 	    int regsize;
   2055   1.8  christos 
   2056   1.8  christos 	    regcache->raw_collect (i, raw);
   2057   1.8  christos 	    regsize = regcache_register_size (regcache, i);
   2058   1.8  christos 	    p = FXSAVE_ADDR (tdep, regs, i);
   2059   1.8  christos 	    if (memcmp (raw, p, regsize))
   2060   1.1  christos 	      {
   2061   1.1  christos 		xstate_bv |= X86_XSTATE_X87;
   2062   1.8  christos 		memcpy (p, raw, regsize);
   2063   1.8  christos 	      }
   2064   1.8  christos 	  }
   2065   1.8  christos       }
   2066   1.8  christos 
   2067   1.8  christos   /* Update the corresponding bits in `xstate_bv' if any
   2068   1.8  christos      registers are changed.  */
   2069   1.8  christos   if (xstate_bv)
   2070   1.8  christos     {
   2071   1.8  christos       /* The supported bits in `xstat_bv' are 8 bytes.  */
   2072   1.1  christos       initial_xstate_bv |= xstate_bv;
   2073   1.1  christos       store_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs),
   2074   1.1  christos 			      8, byte_order,
   2075   1.1  christos 			      initial_xstate_bv);
   2076   1.1  christos     }
   2077   1.1  christos }
   2078   1.1  christos 
   2079   1.1  christos /* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
   2080   1.1  christos    *RAW.  */
   2081   1.1  christos 
   2082   1.1  christos static int
   2083   1.1  christos i387_tag (const gdb_byte *raw)
   2084   1.1  christos {
   2085   1.1  christos   int integer;
   2086   1.1  christos   unsigned int exponent;
   2087   1.1  christos   unsigned long fraction[2];
   2088   1.1  christos 
   2089   1.1  christos   integer = raw[7] & 0x80;
   2090   1.1  christos   exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
   2091   1.1  christos   fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
   2092   1.1  christos   fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
   2093   1.1  christos 		 | (raw[5] << 8) | raw[4]);
   2094   1.1  christos 
   2095   1.1  christos   if (exponent == 0x7fff)
   2096   1.1  christos     {
   2097   1.1  christos       /* Special.  */
   2098   1.1  christos       return (2);
   2099   1.1  christos     }
   2100   1.1  christos   else if (exponent == 0x0000)
   2101   1.1  christos     {
   2102   1.1  christos       if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
   2103   1.1  christos 	{
   2104   1.1  christos 	  /* Zero.  */
   2105   1.1  christos 	  return (1);
   2106   1.1  christos 	}
   2107   1.1  christos       else
   2108   1.1  christos 	{
   2109   1.1  christos 	  /* Special.  */
   2110   1.1  christos 	  return (2);
   2111   1.1  christos 	}
   2112   1.1  christos     }
   2113   1.1  christos   else
   2114   1.1  christos     {
   2115   1.1  christos       if (integer)
   2116   1.1  christos 	{
   2117   1.1  christos 	  /* Valid.  */
   2118   1.1  christos 	  return (0);
   2119   1.1  christos 	}
   2120   1.1  christos       else
   2121   1.1  christos 	{
   2122   1.1  christos 	  /* Special.  */
   2123   1.1  christos 	  return (2);
   2124   1.1  christos 	}
   2125   1.1  christos     }
   2126   1.1  christos }
   2127   1.1  christos 
   2128  1.10  christos /* Prepare the FPU stack in REGCACHE for a function return.  */
   2129   1.1  christos 
   2130   1.1  christos void
   2131   1.1  christos i387_return_value (struct gdbarch *gdbarch, struct regcache *regcache)
   2132   1.1  christos {
   2133   1.1  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
   2134   1.1  christos   ULONGEST fstat;
   2135   1.1  christos 
   2136   1.1  christos   /* Set the top of the floating-point register stack to 7.  The
   2137   1.1  christos      actual value doesn't really matter, but 7 is what a normal
   2138   1.1  christos      function return would end up with if the program started out with
   2139   1.1  christos      a freshly initialized FPU.  */
   2140   1.1  christos   regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
   2141   1.1  christos   fstat |= (7 << 11);
   2142   1.1  christos   regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
   2143   1.1  christos 
   2144   1.1  christos   /* Mark %st(1) through %st(7) as empty.  Since we set the top of the
   2145   1.7  christos      floating-point register stack to 7, the appropriate value for the
   2146   1.7  christos      tag word is 0x3fff.  */
   2147   1.7  christos   regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
   2148   1.7  christos 
   2149   1.7  christos }
   2150   1.7  christos 
   2151  1.10  christos /* See i387-tdep.h.  */
   2152   1.7  christos 
   2153   1.7  christos void
   2154   1.7  christos i387_reset_bnd_regs (struct gdbarch *gdbarch, struct regcache *regcache)
   2155   1.7  christos {
   2156   1.7  christos   i386_gdbarch_tdep *tdep = gdbarch_tdep<i386_gdbarch_tdep> (gdbarch);
   2157   1.7  christos 
   2158   1.7  christos   if (I387_BND0R_REGNUM (tdep) > 0)
   2159   1.8  christos     {
   2160   1.7  christos       gdb_byte bnd_buf[16];
   2161   1.7  christos 
   2162                       memset (bnd_buf, 0, 16);
   2163                       for (int i = 0; i < I387_NUM_BND_REGS; i++)
   2164                 	regcache->raw_write (I387_BND0R_REGNUM (tdep) + i, bnd_buf);
   2165                     }
   2166                 }
   2167