1 /* Intel 387 floating point stuff.
3 Copyright (C) 1988-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "target-float.h"
29 #include "i386-tdep.h"
30 #include "i387-tdep.h"
31 #include "x86-xstate.h"
33 /* Print the floating point number specified by RAW. */
36 print_i387_value (struct gdbarch *gdbarch,
37 const gdb_byte *raw, struct ui_file *file)
39 /* We try to print 19 digits. The last digit may or may not contain
40 garbage, but we'd better print one too many. We need enough room
41 to print the value, 1 position for the sign, 1 for the decimal
42 point, 19 for the digits and 6 for the exponent adds up to 27. */
43 const struct type *type = i387_ext_type (gdbarch);
44 std::string str = target_float_to_string (raw, type, " %-+27.19g");
45 fprintf_filtered (file, "%s", str.c_str ());
48 /* Print the classification for the register contents RAW. */
51 print_i387_ext (struct gdbarch *gdbarch,
52 const gdb_byte *raw, struct ui_file *file)
56 unsigned int exponent;
57 unsigned long fraction[2];
60 integer = raw[7] & 0x80;
61 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
62 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
63 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
64 | (raw[5] << 8) | raw[4]);
66 if (exponent == 0x7fff && integer)
68 if (fraction[0] == 0x00000000 && fraction[1] == 0x00000000)
70 fprintf_filtered (file, " %cInf", (sign ? '-' : '+'));
71 else if (sign && fraction[0] == 0x00000000 && fraction[1] == 0x40000000)
72 /* Real Indefinite (QNaN). */
73 fputs_unfiltered (" Real Indefinite (QNaN)", file);
74 else if (fraction[1] & 0x40000000)
76 fputs_filtered (" QNaN", file);
79 fputs_filtered (" SNaN", file);
81 else if (exponent < 0x7fff && exponent > 0x0000 && integer)
83 print_i387_value (gdbarch, raw, file);
84 else if (exponent == 0x0000)
86 /* Denormal or zero. */
87 print_i387_value (gdbarch, raw, file);
90 /* Pseudo-denormal. */
91 fputs_filtered (" Pseudo-denormal", file);
92 else if (fraction[0] || fraction[1])
94 fputs_filtered (" Denormal", file);
98 fputs_filtered (" Unsupported", file);
101 /* Print the status word STATUS. If STATUS_P is false, then STATUS
105 print_i387_status_word (int status_p,
106 unsigned int status, struct ui_file *file)
108 fprintf_filtered (file, "Status Word: ");
111 fprintf_filtered (file, "%s\n", _("<unavailable>"));
115 fprintf_filtered (file, "%s", hex_string_custom (status, 4));
116 fputs_filtered (" ", file);
117 fprintf_filtered (file, " %s", (status & 0x0001) ? "IE" : " ");
118 fprintf_filtered (file, " %s", (status & 0x0002) ? "DE" : " ");
119 fprintf_filtered (file, " %s", (status & 0x0004) ? "ZE" : " ");
120 fprintf_filtered (file, " %s", (status & 0x0008) ? "OE" : " ");
121 fprintf_filtered (file, " %s", (status & 0x0010) ? "UE" : " ");
122 fprintf_filtered (file, " %s", (status & 0x0020) ? "PE" : " ");
123 fputs_filtered (" ", file);
124 fprintf_filtered (file, " %s", (status & 0x0080) ? "ES" : " ");
125 fputs_filtered (" ", file);
126 fprintf_filtered (file, " %s", (status & 0x0040) ? "SF" : " ");
127 fputs_filtered (" ", file);
128 fprintf_filtered (file, " %s", (status & 0x0100) ? "C0" : " ");
129 fprintf_filtered (file, " %s", (status & 0x0200) ? "C1" : " ");
130 fprintf_filtered (file, " %s", (status & 0x0400) ? "C2" : " ");
131 fprintf_filtered (file, " %s", (status & 0x4000) ? "C3" : " ");
133 fputs_filtered ("\n", file);
135 fprintf_filtered (file,
136 " TOP: %d\n", ((status >> 11) & 7));
139 /* Print the control word CONTROL. If CONTROL_P is false, then
140 CONTROL was unavailable. */
143 print_i387_control_word (int control_p,
144 unsigned int control, struct ui_file *file)
146 fprintf_filtered (file, "Control Word: ");
149 fprintf_filtered (file, "%s\n", _("<unavailable>"));
153 fprintf_filtered (file, "%s", hex_string_custom (control, 4));
154 fputs_filtered (" ", file);
155 fprintf_filtered (file, " %s", (control & 0x0001) ? "IM" : " ");
156 fprintf_filtered (file, " %s", (control & 0x0002) ? "DM" : " ");
157 fprintf_filtered (file, " %s", (control & 0x0004) ? "ZM" : " ");
158 fprintf_filtered (file, " %s", (control & 0x0008) ? "OM" : " ");
159 fprintf_filtered (file, " %s", (control & 0x0010) ? "UM" : " ");
160 fprintf_filtered (file, " %s", (control & 0x0020) ? "PM" : " ");
162 fputs_filtered ("\n", file);
164 fputs_filtered (" PC: ", file);
165 switch ((control >> 8) & 3)
168 fputs_filtered ("Single Precision (24-bits)\n", file);
171 fputs_filtered ("Reserved\n", file);
174 fputs_filtered ("Double Precision (53-bits)\n", file);
177 fputs_filtered ("Extended Precision (64-bits)\n", file);
181 fputs_filtered (" RC: ", file);
182 switch ((control >> 10) & 3)
185 fputs_filtered ("Round to nearest\n", file);
188 fputs_filtered ("Round down\n", file);
191 fputs_filtered ("Round up\n", file);
194 fputs_filtered ("Round toward zero\n", file);
199 /* Print out the i387 floating point state. Note that we ignore FRAME
200 in the code below. That's OK since floating-point registers are
201 never saved on the stack. */
204 i387_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
205 struct frame_info *frame, const char *args)
207 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame));
227 gdb_assert (gdbarch == get_frame_arch (frame));
229 fctrl_p = read_frame_register_unsigned (frame,
230 I387_FCTRL_REGNUM (tdep), &fctrl);
231 fstat_p = read_frame_register_unsigned (frame,
232 I387_FSTAT_REGNUM (tdep), &fstat);
233 ftag_p = read_frame_register_unsigned (frame,
234 I387_FTAG_REGNUM (tdep), &ftag);
235 fiseg_p = read_frame_register_unsigned (frame,
236 I387_FISEG_REGNUM (tdep), &fiseg);
237 fioff_p = read_frame_register_unsigned (frame,
238 I387_FIOFF_REGNUM (tdep), &fioff);
239 foseg_p = read_frame_register_unsigned (frame,
240 I387_FOSEG_REGNUM (tdep), &foseg);
241 fooff_p = read_frame_register_unsigned (frame,
242 I387_FOOFF_REGNUM (tdep), &fooff);
243 fop_p = read_frame_register_unsigned (frame,
244 I387_FOP_REGNUM (tdep), &fop);
248 top = ((fstat >> 11) & 7);
250 for (fpreg = 7; fpreg >= 0; fpreg--)
252 struct value *regval;
257 fprintf_filtered (file, "%sR%d: ", fpreg == top ? "=>" : " ", fpreg);
261 tag = (ftag >> (fpreg * 2)) & 3;
266 fputs_filtered ("Valid ", file);
269 fputs_filtered ("Zero ", file);
272 fputs_filtered ("Special ", file);
275 fputs_filtered ("Empty ", file);
280 fputs_filtered ("Unknown ", file);
282 regnum = (fpreg + 8 - top) % 8 + I387_ST0_REGNUM (tdep);
283 regval = get_frame_register_value (frame, regnum);
285 if (value_entirely_available (regval))
287 const gdb_byte *raw = value_contents (regval);
289 fputs_filtered ("0x", file);
290 for (i = 9; i >= 0; i--)
291 fprintf_filtered (file, "%02x", raw[i]);
293 if (tag != -1 && tag != 3)
294 print_i387_ext (gdbarch, raw, file);
297 fprintf_filtered (file, "%s", _("<unavailable>"));
299 fputs_filtered ("\n", file);
303 fputs_filtered ("\n", file);
304 print_i387_status_word (fstat_p, fstat, file);
305 print_i387_control_word (fctrl_p, fctrl, file);
306 fprintf_filtered (file, "Tag Word: %s\n",
307 ftag_p ? hex_string_custom (ftag, 4) : _("<unavailable>"));
308 fprintf_filtered (file, "Instruction Pointer: %s:",
309 fiseg_p ? hex_string_custom (fiseg, 2) : _("<unavailable>"));
310 fprintf_filtered (file, "%s\n",
311 fioff_p ? hex_string_custom (fioff, 8) : _("<unavailable>"));
312 fprintf_filtered (file, "Operand Pointer: %s:",
313 foseg_p ? hex_string_custom (foseg, 2) : _("<unavailable>"));
314 fprintf_filtered (file, "%s\n",
315 fooff_p ? hex_string_custom (fooff, 8) : _("<unavailable>"));
316 fprintf_filtered (file, "Opcode: %s\n",
318 ? (hex_string_custom (fop ? (fop | 0xd800) : 0, 4))
319 : _("<unavailable>"));
323 /* Return nonzero if a value of type TYPE stored in register REGNUM
324 needs any special handling. */
327 i387_convert_register_p (struct gdbarch *gdbarch, int regnum,
330 if (i386_fp_regnum_p (gdbarch, regnum))
332 /* Floating point registers must be converted unless we are
333 accessing them in their hardware type or TYPE is not float. */
334 if (type == i387_ext_type (gdbarch)
335 || TYPE_CODE (type) != TYPE_CODE_FLT)
344 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
345 return its contents in TO. */
348 i387_register_to_value (struct frame_info *frame, int regnum,
349 struct type *type, gdb_byte *to,
350 int *optimizedp, int *unavailablep)
352 struct gdbarch *gdbarch = get_frame_arch (frame);
353 gdb_byte from[I386_MAX_REGISTER_SIZE];
355 gdb_assert (i386_fp_regnum_p (gdbarch, regnum));
357 /* We only support floating-point values. */
358 if (TYPE_CODE (type) != TYPE_CODE_FLT)
360 warning (_("Cannot convert floating-point register value "
361 "to non-floating-point type."));
362 *optimizedp = *unavailablep = 0;
366 /* Convert to TYPE. */
367 if (!get_frame_register_bytes (frame, regnum, 0,
368 register_size (gdbarch, regnum),
369 from, optimizedp, unavailablep))
372 target_float_convert (from, i387_ext_type (gdbarch), to, type);
373 *optimizedp = *unavailablep = 0;
377 /* Write the contents FROM of a value of type TYPE into register
378 REGNUM in frame FRAME. */
381 i387_value_to_register (struct frame_info *frame, int regnum,
382 struct type *type, const gdb_byte *from)
384 struct gdbarch *gdbarch = get_frame_arch (frame);
385 gdb_byte to[I386_MAX_REGISTER_SIZE];
387 gdb_assert (i386_fp_regnum_p (gdbarch, regnum));
389 /* We only support floating-point values. */
390 if (TYPE_CODE (type) != TYPE_CODE_FLT)
392 warning (_("Cannot convert non-floating-point type "
393 "to floating-point register value."));
397 /* Convert from TYPE. */
398 target_float_convert (from, type, to, i387_ext_type (gdbarch));
399 put_frame_register (frame, regnum, to);
403 /* Handle FSAVE and FXSAVE formats. */
405 /* At fsave_offset[REGNUM] you'll find the offset to the location in
406 the data structure used by the "fsave" instruction where GDB
407 register REGNUM is stored. */
409 static int fsave_offset[] =
411 28 + 0 * 10, /* %st(0) ... */
418 28 + 7 * 10, /* ... %st(7). */
419 0, /* `fctrl' (16 bits). */
420 4, /* `fstat' (16 bits). */
421 8, /* `ftag' (16 bits). */
422 16, /* `fiseg' (16 bits). */
424 24, /* `foseg' (16 bits). */
426 18 /* `fop' (bottom 11 bits). */
429 #define FSAVE_ADDR(tdep, fsave, regnum) \
430 (fsave + fsave_offset[regnum - I387_ST0_REGNUM (tdep)])
433 /* Fill register REGNUM in REGCACHE with the appropriate value from
434 *FSAVE. This function masks off any of the reserved bits in
438 i387_supply_fsave (struct regcache *regcache, int regnum, const void *fsave)
440 struct gdbarch *gdbarch = regcache->arch ();
441 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
442 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
443 const gdb_byte *regs = (const gdb_byte *) fsave;
446 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
448 for (i = I387_ST0_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
449 if (regnum == -1 || regnum == i)
453 regcache_raw_supply (regcache, i, NULL);
457 /* Most of the FPU control registers occupy only 16 bits in the
458 fsave area. Give those a special treatment. */
459 if (i >= I387_FCTRL_REGNUM (tdep)
460 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
464 memcpy (val, FSAVE_ADDR (tdep, regs, i), 2);
466 if (i == I387_FOP_REGNUM (tdep))
467 val[1] &= ((1 << 3) - 1);
468 regcache_raw_supply (regcache, i, val);
471 regcache_raw_supply (regcache, i, FSAVE_ADDR (tdep, regs, i));
474 /* Provide dummy values for the SSE registers. */
475 for (i = I387_XMM0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
476 if (regnum == -1 || regnum == i)
477 regcache_raw_supply (regcache, i, NULL);
478 if (regnum == -1 || regnum == I387_MXCSR_REGNUM (tdep))
482 store_unsigned_integer (buf, 4, byte_order, 0x1f80);
483 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep), buf);
487 /* Fill register REGNUM (if it is a floating-point register) in *FSAVE
488 with the value from REGCACHE. If REGNUM is -1, do this for all
489 registers. This function doesn't touch any of the reserved bits in
493 i387_collect_fsave (const struct regcache *regcache, int regnum, void *fsave)
495 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
496 gdb_byte *regs = (gdb_byte *) fsave;
499 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
501 for (i = I387_ST0_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
502 if (regnum == -1 || regnum == i)
504 /* Most of the FPU control registers occupy only 16 bits in
505 the fsave area. Give those a special treatment. */
506 if (i >= I387_FCTRL_REGNUM (tdep)
507 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
511 regcache_raw_collect (regcache, i, buf);
513 if (i == I387_FOP_REGNUM (tdep))
515 /* The opcode occupies only 11 bits. Make sure we
516 don't touch the other bits. */
517 buf[1] &= ((1 << 3) - 1);
518 buf[1] |= ((FSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
520 memcpy (FSAVE_ADDR (tdep, regs, i), buf, 2);
523 regcache_raw_collect (regcache, i, FSAVE_ADDR (tdep, regs, i));
528 /* At fxsave_offset[REGNUM] you'll find the offset to the location in
529 the data structure used by the "fxsave" instruction where GDB
530 register REGNUM is stored. */
532 static int fxsave_offset[] =
534 32, /* %st(0) through ... */
541 144, /* ... %st(7) (80 bits each). */
542 0, /* `fctrl' (16 bits). */
543 2, /* `fstat' (16 bits). */
544 4, /* `ftag' (16 bits). */
545 12, /* `fiseg' (16 bits). */
547 20, /* `foseg' (16 bits). */
549 6, /* `fop' (bottom 11 bits). */
550 160 + 0 * 16, /* %xmm0 through ... */
565 160 + 15 * 16, /* ... %xmm15 (128 bits each). */
568 #define FXSAVE_ADDR(tdep, fxsave, regnum) \
569 (fxsave + fxsave_offset[regnum - I387_ST0_REGNUM (tdep)])
571 /* We made an unfortunate choice in putting %mxcsr after the SSE
572 registers %xmm0-%xmm7 instead of before, since it makes supporting
573 the registers %xmm8-%xmm15 on AMD64 a bit involved. Therefore we
574 don't include the offset for %mxcsr here above. */
576 #define FXSAVE_MXCSR_ADDR(fxsave) (fxsave + 24)
578 static int i387_tag (const gdb_byte *raw);
581 /* Fill register REGNUM in REGCACHE with the appropriate
582 floating-point or SSE register value from *FXSAVE. This function
583 masks off any of the reserved bits in *FXSAVE. */
586 i387_supply_fxsave (struct regcache *regcache, int regnum, const void *fxsave)
588 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
589 const gdb_byte *regs = (const gdb_byte *) fxsave;
592 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
593 gdb_assert (tdep->num_xmm_regs > 0);
595 for (i = I387_ST0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
596 if (regnum == -1 || regnum == i)
600 regcache_raw_supply (regcache, i, NULL);
604 /* Most of the FPU control registers occupy only 16 bits in
605 the fxsave area. Give those a special treatment. */
606 if (i >= I387_FCTRL_REGNUM (tdep) && i < I387_XMM0_REGNUM (tdep)
607 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
611 memcpy (val, FXSAVE_ADDR (tdep, regs, i), 2);
613 if (i == I387_FOP_REGNUM (tdep))
614 val[1] &= ((1 << 3) - 1);
615 else if (i== I387_FTAG_REGNUM (tdep))
617 /* The fxsave area contains a simplified version of
618 the tag word. We have to look at the actual 80-bit
619 FP data to recreate the traditional i387 tag word. */
621 unsigned long ftag = 0;
625 top = ((FXSAVE_ADDR (tdep, regs,
626 I387_FSTAT_REGNUM (tdep)))[1] >> 3);
629 for (fpreg = 7; fpreg >= 0; fpreg--)
633 if (val[0] & (1 << fpreg))
635 int thisreg = (fpreg + 8 - top) % 8
636 + I387_ST0_REGNUM (tdep);
637 tag = i387_tag (FXSAVE_ADDR (tdep, regs, thisreg));
642 ftag |= tag << (2 * fpreg);
644 val[0] = ftag & 0xff;
645 val[1] = (ftag >> 8) & 0xff;
647 regcache_raw_supply (regcache, i, val);
650 regcache_raw_supply (regcache, i, FXSAVE_ADDR (tdep, regs, i));
653 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
656 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep), NULL);
658 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep),
659 FXSAVE_MXCSR_ADDR (regs));
663 /* Fill register REGNUM (if it is a floating-point or SSE register) in
664 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
665 all registers. This function doesn't touch any of the reserved
669 i387_collect_fxsave (const struct regcache *regcache, int regnum, void *fxsave)
671 struct gdbarch_tdep *tdep = gdbarch_tdep (regcache->arch ());
672 gdb_byte *regs = (gdb_byte *) fxsave;
675 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
676 gdb_assert (tdep->num_xmm_regs > 0);
678 for (i = I387_ST0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
679 if (regnum == -1 || regnum == i)
681 /* Most of the FPU control registers occupy only 16 bits in
682 the fxsave area. Give those a special treatment. */
683 if (i >= I387_FCTRL_REGNUM (tdep) && i < I387_XMM0_REGNUM (tdep)
684 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
688 regcache_raw_collect (regcache, i, buf);
690 if (i == I387_FOP_REGNUM (tdep))
692 /* The opcode occupies only 11 bits. Make sure we
693 don't touch the other bits. */
694 buf[1] &= ((1 << 3) - 1);
695 buf[1] |= ((FXSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
697 else if (i == I387_FTAG_REGNUM (tdep))
699 /* Converting back is much easier. */
704 ftag = (buf[1] << 8) | buf[0];
708 for (fpreg = 7; fpreg >= 0; fpreg--)
710 int tag = (ftag >> (fpreg * 2)) & 3;
713 buf[0] |= (1 << fpreg);
716 memcpy (FXSAVE_ADDR (tdep, regs, i), buf, 2);
719 regcache_raw_collect (regcache, i, FXSAVE_ADDR (tdep, regs, i));
722 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
723 regcache_raw_collect (regcache, I387_MXCSR_REGNUM (tdep),
724 FXSAVE_MXCSR_ADDR (regs));
727 /* `xstate_bv' is at byte offset 512. */
728 #define XSAVE_XSTATE_BV_ADDR(xsave) (xsave + 512)
730 /* At xsave_avxh_offset[REGNUM] you'll find the offset to the location in
731 the upper 128bit of AVX register data structure used by the "xsave"
732 instruction where GDB register REGNUM is stored. */
734 static int xsave_avxh_offset[] =
736 576 + 0 * 16, /* Upper 128bit of %ymm0 through ... */
751 576 + 15 * 16 /* Upper 128bit of ... %ymm15 (128 bits each). */
754 #define XSAVE_AVXH_ADDR(tdep, xsave, regnum) \
755 (xsave + xsave_avxh_offset[regnum - I387_YMM0H_REGNUM (tdep)])
757 /* At xsave_ymm_avx512_offset[REGNUM] you'll find the offset to the location in
758 the upper 128bit of ZMM register data structure used by the "xsave"
759 instruction where GDB register REGNUM is stored. */
761 static int xsave_ymm_avx512_offset[] =
763 /* HI16_ZMM_area + 16 bytes + regnum* 64 bytes. */
764 1664 + 16 + 0 * 64, /* %ymm16 through... */
779 1664 + 16 + 15 * 64 /* ... %ymm31 (128 bits each). */
782 #define XSAVE_YMM_AVX512_ADDR(tdep, xsave, regnum) \
783 (xsave + xsave_ymm_avx512_offset[regnum - I387_YMM16H_REGNUM (tdep)])
785 static int xsave_xmm_avx512_offset[] =
787 1664 + 0 * 64, /* %ymm16 through... */
802 1664 + 15 * 64 /* ... %ymm31 (128 bits each). */
805 #define XSAVE_XMM_AVX512_ADDR(tdep, xsave, regnum) \
806 (xsave + xsave_xmm_avx512_offset[regnum - I387_XMM16_REGNUM (tdep)])
808 static int xsave_mpx_offset[] = {
809 960 + 0 * 16, /* bnd0r...bnd3r registers. */
813 1024 + 0 * 8, /* bndcfg ... bndstatus. */
817 #define XSAVE_MPX_ADDR(tdep, xsave, regnum) \
818 (xsave + xsave_mpx_offset[regnum - I387_BND0R_REGNUM (tdep)])
820 /* At xsave_avx512__h_offset[REGNUM] you find the offset to the location
821 of the AVX512 opmask register data structure used by the "xsave"
822 instruction where GDB register REGNUM is stored. */
824 static int xsave_avx512_k_offset[] =
826 1088 + 0 * 8, /* %k0 through... */
833 1088 + 7 * 8 /* %k7 (64 bits each). */
836 #define XSAVE_AVX512_K_ADDR(tdep, xsave, regnum) \
837 (xsave + xsave_avx512_k_offset[regnum - I387_K0_REGNUM (tdep)])
839 /* At xsave_avx512_zmm_h_offset[REGNUM] you find the offset to the location in
840 the upper 256bit of AVX512 ZMMH register data structure used by the "xsave"
841 instruction where GDB register REGNUM is stored. */
843 static int xsave_avx512_zmm_h_offset[] =
846 1152 + 1 * 32, /* Upper 256bit of %zmmh0 through... */
860 1152 + 15 * 32, /* Upper 256bit of... %zmmh15 (256 bits each). */
861 1664 + 32 + 0 * 64, /* Upper 256bit of... %zmmh16 (256 bits each). */
876 1664 + 32 + 15 * 64 /* Upper 256bit of... %zmmh31 (256 bits each). */
879 #define XSAVE_AVX512_ZMM_H_ADDR(tdep, xsave, regnum) \
880 (xsave + xsave_avx512_zmm_h_offset[regnum - I387_ZMM0H_REGNUM (tdep)])
882 /* At xsave_pkeys_offset[REGNUM] you find the offset to the location
883 of the PKRU register data structure used by the "xsave"
884 instruction where GDB register REGNUM is stored. */
886 static int xsave_pkeys_offset[] =
888 2688 + 0 * 8 /* %pkru (64 bits in XSTATE, 32-bit actually used by
889 instructions and applications). */
892 #define XSAVE_PKEYS_ADDR(tdep, xsave, regnum) \
893 (xsave + xsave_pkeys_offset[regnum - I387_PKRU_REGNUM (tdep)])
895 /* Similar to i387_supply_fxsave, but use XSAVE extended state. */
898 i387_supply_xsave (struct regcache *regcache, int regnum,
901 struct gdbarch *gdbarch = regcache->arch ();
902 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
903 const gdb_byte *regs = (const gdb_byte *) xsave;
906 static const gdb_byte zero[I386_MAX_REGISTER_SIZE] = { 0 };
916 avx512_ymmh_avx512 = 0x40,
917 avx512_xmm_avx512 = 0x80,
919 all = x87 | sse | avxh | mpx | avx512_k | avx512_zmm_h
920 | avx512_ymmh_avx512 | avx512_xmm_avx512 | pkeys
923 gdb_assert (regs != NULL);
924 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
925 gdb_assert (tdep->num_xmm_regs > 0);
929 else if (regnum >= I387_PKRU_REGNUM (tdep)
930 && regnum < I387_PKEYSEND_REGNUM (tdep))
932 else if (regnum >= I387_ZMM0H_REGNUM (tdep)
933 && regnum < I387_ZMMENDH_REGNUM (tdep))
934 regclass = avx512_zmm_h;
935 else if (regnum >= I387_K0_REGNUM (tdep)
936 && regnum < I387_KEND_REGNUM (tdep))
938 else if (regnum >= I387_YMM16H_REGNUM (tdep)
939 && regnum < I387_YMMH_AVX512_END_REGNUM (tdep))
940 regclass = avx512_ymmh_avx512;
941 else if (regnum >= I387_XMM16_REGNUM (tdep)
942 && regnum < I387_XMM_AVX512_END_REGNUM (tdep))
943 regclass = avx512_xmm_avx512;
944 else if (regnum >= I387_YMM0H_REGNUM (tdep)
945 && regnum < I387_YMMENDH_REGNUM (tdep))
947 else if (regnum >= I387_BND0R_REGNUM (tdep)
948 && regnum < I387_MPXEND_REGNUM (tdep))
950 else if (regnum >= I387_XMM0_REGNUM (tdep)
951 && regnum < I387_MXCSR_REGNUM (tdep))
953 else if (regnum >= I387_ST0_REGNUM (tdep)
954 && regnum < I387_FCTRL_REGNUM (tdep))
959 if (regclass != none)
961 /* Get `xstat_bv'. The supported bits in `xstat_bv' are 8 bytes. */
962 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
963 ULONGEST xstate_bv = 0;
965 xstate_bv = extract_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs),
968 /* Clear part in vector registers if its bit in xstat_bv is zero. */
969 clear_bv = (~(xstate_bv)) & tdep->xcr0;
972 clear_bv = X86_XSTATE_ALL_MASK;
974 /* With the delayed xsave mechanism, in between the program
975 starting, and the program accessing the vector registers for the
976 first time, the register's values are invalid. The kernel
977 initializes register states to zero when they are set the first
978 time in a program. This means that from the user-space programs'
979 perspective, it's the same as if the registers have always been
980 zero from the start of the program. Therefore, the debugger
981 should provide the same illusion to the user. */
989 if ((clear_bv & X86_XSTATE_PKRU))
990 regcache_raw_supply (regcache, regnum, zero);
992 regcache_raw_supply (regcache, regnum,
993 XSAVE_PKEYS_ADDR (tdep, regs, regnum));
997 if ((clear_bv & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
998 regcache_raw_supply (regcache, regnum, zero);
1000 regcache_raw_supply (regcache, regnum,
1001 XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, regnum));
1005 if ((clear_bv & X86_XSTATE_K))
1006 regcache_raw_supply (regcache, regnum, zero);
1008 regcache_raw_supply (regcache, regnum,
1009 XSAVE_AVX512_K_ADDR (tdep, regs, regnum));
1012 case avx512_ymmh_avx512:
1013 if ((clear_bv & X86_XSTATE_ZMM))
1014 regcache_raw_supply (regcache, regnum, zero);
1016 regcache_raw_supply (regcache, regnum,
1017 XSAVE_YMM_AVX512_ADDR (tdep, regs, regnum));
1020 case avx512_xmm_avx512:
1021 if ((clear_bv & X86_XSTATE_ZMM))
1022 regcache_raw_supply (regcache, regnum, zero);
1024 regcache_raw_supply (regcache, regnum,
1025 XSAVE_XMM_AVX512_ADDR (tdep, regs, regnum));
1029 if ((clear_bv & X86_XSTATE_AVX))
1030 regcache_raw_supply (regcache, regnum, zero);
1032 regcache_raw_supply (regcache, regnum,
1033 XSAVE_AVXH_ADDR (tdep, regs, regnum));
1037 if ((clear_bv & X86_XSTATE_BNDREGS))
1038 regcache_raw_supply (regcache, regnum, zero);
1040 regcache_raw_supply (regcache, regnum,
1041 XSAVE_MPX_ADDR (tdep, regs, regnum));
1045 if ((clear_bv & X86_XSTATE_SSE))
1046 regcache_raw_supply (regcache, regnum, zero);
1048 regcache_raw_supply (regcache, regnum,
1049 FXSAVE_ADDR (tdep, regs, regnum));
1053 if ((clear_bv & X86_XSTATE_X87))
1054 regcache_raw_supply (regcache, regnum, zero);
1056 regcache_raw_supply (regcache, regnum,
1057 FXSAVE_ADDR (tdep, regs, regnum));
1061 /* Handle PKEYS registers. */
1062 if ((tdep->xcr0 & X86_XSTATE_PKRU))
1064 if ((clear_bv & X86_XSTATE_PKRU))
1066 for (i = I387_PKRU_REGNUM (tdep);
1067 i < I387_PKEYSEND_REGNUM (tdep);
1069 regcache_raw_supply (regcache, i, zero);
1073 for (i = I387_PKRU_REGNUM (tdep);
1074 i < I387_PKEYSEND_REGNUM (tdep);
1076 regcache_raw_supply (regcache, i,
1077 XSAVE_PKEYS_ADDR (tdep, regs, i));
1081 /* Handle the upper ZMM registers. */
1082 if ((tdep->xcr0 & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
1084 if ((clear_bv & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
1086 for (i = I387_ZMM0H_REGNUM (tdep);
1087 i < I387_ZMMENDH_REGNUM (tdep);
1089 regcache_raw_supply (regcache, i, zero);
1093 for (i = I387_ZMM0H_REGNUM (tdep);
1094 i < I387_ZMMENDH_REGNUM (tdep);
1096 regcache_raw_supply (regcache, i,
1097 XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, i));
1101 /* Handle AVX512 OpMask registers. */
1102 if ((tdep->xcr0 & X86_XSTATE_K))
1104 if ((clear_bv & X86_XSTATE_K))
1106 for (i = I387_K0_REGNUM (tdep);
1107 i < I387_KEND_REGNUM (tdep);
1109 regcache_raw_supply (regcache, i, zero);
1113 for (i = I387_K0_REGNUM (tdep);
1114 i < I387_KEND_REGNUM (tdep);
1116 regcache_raw_supply (regcache, i,
1117 XSAVE_AVX512_K_ADDR (tdep, regs, i));
1121 /* Handle the YMM_AVX512 registers. */
1122 if ((tdep->xcr0 & X86_XSTATE_ZMM))
1124 if ((clear_bv & X86_XSTATE_ZMM))
1126 for (i = I387_YMM16H_REGNUM (tdep);
1127 i < I387_YMMH_AVX512_END_REGNUM (tdep);
1129 regcache_raw_supply (regcache, i, zero);
1130 for (i = I387_XMM16_REGNUM (tdep);
1131 i < I387_XMM_AVX512_END_REGNUM (tdep);
1133 regcache_raw_supply (regcache, i, zero);
1137 for (i = I387_YMM16H_REGNUM (tdep);
1138 i < I387_YMMH_AVX512_END_REGNUM (tdep);
1140 regcache_raw_supply (regcache, i,
1141 XSAVE_YMM_AVX512_ADDR (tdep, regs, i));
1142 for (i = I387_XMM16_REGNUM (tdep);
1143 i < I387_XMM_AVX512_END_REGNUM (tdep);
1145 regcache_raw_supply (regcache, i,
1146 XSAVE_XMM_AVX512_ADDR (tdep, regs, i));
1149 /* Handle the upper YMM registers. */
1150 if ((tdep->xcr0 & X86_XSTATE_AVX))
1152 if ((clear_bv & X86_XSTATE_AVX))
1154 for (i = I387_YMM0H_REGNUM (tdep);
1155 i < I387_YMMENDH_REGNUM (tdep);
1157 regcache_raw_supply (regcache, i, zero);
1161 for (i = I387_YMM0H_REGNUM (tdep);
1162 i < I387_YMMENDH_REGNUM (tdep);
1164 regcache_raw_supply (regcache, i,
1165 XSAVE_AVXH_ADDR (tdep, regs, i));
1169 /* Handle the MPX registers. */
1170 if ((tdep->xcr0 & X86_XSTATE_BNDREGS))
1172 if (clear_bv & X86_XSTATE_BNDREGS)
1174 for (i = I387_BND0R_REGNUM (tdep);
1175 i < I387_BNDCFGU_REGNUM (tdep); i++)
1176 regcache_raw_supply (regcache, i, zero);
1180 for (i = I387_BND0R_REGNUM (tdep);
1181 i < I387_BNDCFGU_REGNUM (tdep); i++)
1182 regcache_raw_supply (regcache, i,
1183 XSAVE_MPX_ADDR (tdep, regs, i));
1187 /* Handle the MPX registers. */
1188 if ((tdep->xcr0 & X86_XSTATE_BNDCFG))
1190 if (clear_bv & X86_XSTATE_BNDCFG)
1192 for (i = I387_BNDCFGU_REGNUM (tdep);
1193 i < I387_MPXEND_REGNUM (tdep); i++)
1194 regcache_raw_supply (regcache, i, zero);
1198 for (i = I387_BNDCFGU_REGNUM (tdep);
1199 i < I387_MPXEND_REGNUM (tdep); i++)
1200 regcache_raw_supply (regcache, i,
1201 XSAVE_MPX_ADDR (tdep, regs, i));
1205 /* Handle the XMM registers. */
1206 if ((tdep->xcr0 & X86_XSTATE_SSE))
1208 if ((clear_bv & X86_XSTATE_SSE))
1210 for (i = I387_XMM0_REGNUM (tdep);
1211 i < I387_MXCSR_REGNUM (tdep);
1213 regcache_raw_supply (regcache, i, zero);
1217 for (i = I387_XMM0_REGNUM (tdep);
1218 i < I387_MXCSR_REGNUM (tdep); i++)
1219 regcache_raw_supply (regcache, i,
1220 FXSAVE_ADDR (tdep, regs, i));
1224 /* Handle the x87 registers. */
1225 if ((tdep->xcr0 & X86_XSTATE_X87))
1227 if ((clear_bv & X86_XSTATE_X87))
1229 for (i = I387_ST0_REGNUM (tdep);
1230 i < I387_FCTRL_REGNUM (tdep);
1232 regcache_raw_supply (regcache, i, zero);
1236 for (i = I387_ST0_REGNUM (tdep);
1237 i < I387_FCTRL_REGNUM (tdep);
1239 regcache_raw_supply (regcache, i, FXSAVE_ADDR (tdep, regs, i));
1245 /* Only handle x87 control registers. */
1246 for (i = I387_FCTRL_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
1247 if (regnum == -1 || regnum == i)
1249 /* Most of the FPU control registers occupy only 16 bits in
1250 the xsave extended state. Give those a special treatment. */
1251 if (i != I387_FIOFF_REGNUM (tdep)
1252 && i != I387_FOOFF_REGNUM (tdep))
1256 memcpy (val, FXSAVE_ADDR (tdep, regs, i), 2);
1257 val[2] = val[3] = 0;
1258 if (i == I387_FOP_REGNUM (tdep))
1259 val[1] &= ((1 << 3) - 1);
1260 else if (i== I387_FTAG_REGNUM (tdep))
1262 /* The fxsave area contains a simplified version of
1263 the tag word. We have to look at the actual 80-bit
1264 FP data to recreate the traditional i387 tag word. */
1266 unsigned long ftag = 0;
1270 top = ((FXSAVE_ADDR (tdep, regs,
1271 I387_FSTAT_REGNUM (tdep)))[1] >> 3);
1274 for (fpreg = 7; fpreg >= 0; fpreg--)
1278 if (val[0] & (1 << fpreg))
1280 int thisreg = (fpreg + 8 - top) % 8
1281 + I387_ST0_REGNUM (tdep);
1282 tag = i387_tag (FXSAVE_ADDR (tdep, regs, thisreg));
1285 tag = 3; /* Empty */
1287 ftag |= tag << (2 * fpreg);
1289 val[0] = ftag & 0xff;
1290 val[1] = (ftag >> 8) & 0xff;
1292 regcache_raw_supply (regcache, i, val);
1295 regcache_raw_supply (regcache, i, FXSAVE_ADDR (tdep, regs, i));
1298 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
1299 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep),
1300 FXSAVE_MXCSR_ADDR (regs));
1303 /* Similar to i387_collect_fxsave, but use XSAVE extended state. */
1306 i387_collect_xsave (const struct regcache *regcache, int regnum,
1307 void *xsave, int gcore)
1309 struct gdbarch *gdbarch = regcache->arch ();
1310 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1311 gdb_byte *regs = (gdb_byte *) xsave;
1321 avx512_k = 0x20 | check,
1322 avx512_zmm_h = 0x40 | check,
1323 avx512_ymmh_avx512 = 0x80 | check,
1324 avx512_xmm_avx512 = 0x100 | check,
1325 pkeys = 0x200 | check,
1326 all = x87 | sse | avxh | mpx | avx512_k | avx512_zmm_h
1327 | avx512_ymmh_avx512 | avx512_xmm_avx512 | pkeys
1330 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
1331 gdb_assert (tdep->num_xmm_regs > 0);
1335 else if (regnum >= I387_PKRU_REGNUM (tdep)
1336 && regnum < I387_PKEYSEND_REGNUM (tdep))
1338 else if (regnum >= I387_ZMM0H_REGNUM (tdep)
1339 && regnum < I387_ZMMENDH_REGNUM (tdep))
1340 regclass = avx512_zmm_h;
1341 else if (regnum >= I387_K0_REGNUM (tdep)
1342 && regnum < I387_KEND_REGNUM (tdep))
1343 regclass = avx512_k;
1344 else if (regnum >= I387_YMM16H_REGNUM (tdep)
1345 && regnum < I387_YMMH_AVX512_END_REGNUM (tdep))
1346 regclass = avx512_ymmh_avx512;
1347 else if (regnum >= I387_XMM16_REGNUM (tdep)
1348 && regnum < I387_XMM_AVX512_END_REGNUM (tdep))
1349 regclass = avx512_xmm_avx512;
1350 else if (regnum >= I387_YMM0H_REGNUM (tdep)
1351 && regnum < I387_YMMENDH_REGNUM (tdep))
1353 else if (regnum >= I387_BND0R_REGNUM (tdep)
1354 && regnum < I387_MPXEND_REGNUM (tdep))
1356 else if (regnum >= I387_XMM0_REGNUM (tdep)
1357 && regnum < I387_MXCSR_REGNUM (tdep))
1359 else if (regnum >= I387_ST0_REGNUM (tdep)
1360 && regnum < I387_FCTRL_REGNUM (tdep))
1367 /* Clear XSAVE extended state. */
1368 memset (regs, 0, X86_XSTATE_SIZE (tdep->xcr0));
1370 /* Update XCR0 and `xstate_bv' with XCR0 for gcore. */
1371 if (tdep->xsave_xcr0_offset != -1)
1372 memcpy (regs + tdep->xsave_xcr0_offset, &tdep->xcr0, 8);
1373 memcpy (XSAVE_XSTATE_BV_ADDR (regs), &tdep->xcr0, 8);
1376 if ((regclass & check))
1378 gdb_byte raw[I386_MAX_REGISTER_SIZE];
1379 ULONGEST initial_xstate_bv, clear_bv, xstate_bv = 0;
1381 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1383 /* The supported bits in `xstat_bv' are 8 bytes. */
1384 initial_xstate_bv = extract_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs),
1386 clear_bv = (~(initial_xstate_bv)) & tdep->xcr0;
1388 /* Clear register set if its bit in xstat_bv is zero. */
1391 if ((clear_bv & X86_XSTATE_PKRU))
1392 for (i = I387_PKRU_REGNUM (tdep);
1393 i < I387_PKEYSEND_REGNUM (tdep); i++)
1394 memset (XSAVE_PKEYS_ADDR (tdep, regs, i), 0, 4);
1396 if ((clear_bv & X86_XSTATE_BNDREGS))
1397 for (i = I387_BND0R_REGNUM (tdep);
1398 i < I387_BNDCFGU_REGNUM (tdep); i++)
1399 memset (XSAVE_MPX_ADDR (tdep, regs, i), 0, 16);
1401 if ((clear_bv & X86_XSTATE_BNDCFG))
1402 for (i = I387_BNDCFGU_REGNUM (tdep);
1403 i < I387_MPXEND_REGNUM (tdep); i++)
1404 memset (XSAVE_MPX_ADDR (tdep, regs, i), 0, 8);
1406 if ((clear_bv & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
1407 for (i = I387_ZMM0H_REGNUM (tdep);
1408 i < I387_ZMMENDH_REGNUM (tdep); i++)
1409 memset (XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, i), 0, 32);
1411 if ((clear_bv & X86_XSTATE_K))
1412 for (i = I387_K0_REGNUM (tdep);
1413 i < I387_KEND_REGNUM (tdep); i++)
1414 memset (XSAVE_AVX512_K_ADDR (tdep, regs, i), 0, 8);
1416 if ((clear_bv & X86_XSTATE_ZMM))
1418 for (i = I387_YMM16H_REGNUM (tdep);
1419 i < I387_YMMH_AVX512_END_REGNUM (tdep); i++)
1420 memset (XSAVE_YMM_AVX512_ADDR (tdep, regs, i), 0, 16);
1421 for (i = I387_XMM16_REGNUM (tdep);
1422 i < I387_XMM_AVX512_END_REGNUM (tdep); i++)
1423 memset (XSAVE_XMM_AVX512_ADDR (tdep, regs, i), 0, 16);
1426 if ((clear_bv & X86_XSTATE_AVX))
1427 for (i = I387_YMM0H_REGNUM (tdep);
1428 i < I387_YMMENDH_REGNUM (tdep); i++)
1429 memset (XSAVE_AVXH_ADDR (tdep, regs, i), 0, 16);
1431 if ((clear_bv & X86_XSTATE_SSE))
1432 for (i = I387_XMM0_REGNUM (tdep);
1433 i < I387_MXCSR_REGNUM (tdep); i++)
1434 memset (FXSAVE_ADDR (tdep, regs, i), 0, 16);
1436 if ((clear_bv & X86_XSTATE_X87))
1437 for (i = I387_ST0_REGNUM (tdep);
1438 i < I387_FCTRL_REGNUM (tdep); i++)
1439 memset (FXSAVE_ADDR (tdep, regs, i), 0, 10);
1442 if (regclass == all)
1444 /* Check if any PKEYS registers are changed. */
1445 if ((tdep->xcr0 & X86_XSTATE_PKRU))
1446 for (i = I387_PKRU_REGNUM (tdep);
1447 i < I387_PKEYSEND_REGNUM (tdep); i++)
1449 regcache_raw_collect (regcache, i, raw);
1450 p = XSAVE_PKEYS_ADDR (tdep, regs, i);
1451 if (memcmp (raw, p, 4) != 0)
1453 xstate_bv |= X86_XSTATE_PKRU;
1458 /* Check if any ZMMH registers are changed. */
1459 if ((tdep->xcr0 & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
1460 for (i = I387_ZMM0H_REGNUM (tdep);
1461 i < I387_ZMMENDH_REGNUM (tdep); i++)
1463 regcache_raw_collect (regcache, i, raw);
1464 p = XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, i);
1465 if (memcmp (raw, p, 32) != 0)
1467 xstate_bv |= (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM);
1468 memcpy (p, raw, 32);
1472 /* Check if any K registers are changed. */
1473 if ((tdep->xcr0 & X86_XSTATE_K))
1474 for (i = I387_K0_REGNUM (tdep);
1475 i < I387_KEND_REGNUM (tdep); i++)
1477 regcache_raw_collect (regcache, i, raw);
1478 p = XSAVE_AVX512_K_ADDR (tdep, regs, i);
1479 if (memcmp (raw, p, 8) != 0)
1481 xstate_bv |= X86_XSTATE_K;
1486 /* Check if any XMM or upper YMM registers are changed. */
1487 if ((tdep->xcr0 & X86_XSTATE_ZMM))
1489 for (i = I387_YMM16H_REGNUM (tdep);
1490 i < I387_YMMH_AVX512_END_REGNUM (tdep); i++)
1492 regcache_raw_collect (regcache, i, raw);
1493 p = XSAVE_YMM_AVX512_ADDR (tdep, regs, i);
1494 if (memcmp (raw, p, 16) != 0)
1496 xstate_bv |= X86_XSTATE_ZMM;
1497 memcpy (p, raw, 16);
1500 for (i = I387_XMM16_REGNUM (tdep);
1501 i < I387_XMM_AVX512_END_REGNUM (tdep); i++)
1503 regcache_raw_collect (regcache, i, raw);
1504 p = XSAVE_XMM_AVX512_ADDR (tdep, regs, i);
1505 if (memcmp (raw, p, 16) != 0)
1507 xstate_bv |= X86_XSTATE_ZMM;
1508 memcpy (p, raw, 16);
1513 /* Check if any upper YMM registers are changed. */
1514 if ((tdep->xcr0 & X86_XSTATE_AVX))
1515 for (i = I387_YMM0H_REGNUM (tdep);
1516 i < I387_YMMENDH_REGNUM (tdep); i++)
1518 regcache_raw_collect (regcache, i, raw);
1519 p = XSAVE_AVXH_ADDR (tdep, regs, i);
1520 if (memcmp (raw, p, 16))
1522 xstate_bv |= X86_XSTATE_AVX;
1523 memcpy (p, raw, 16);
1526 /* Check if any upper MPX registers are changed. */
1527 if ((tdep->xcr0 & X86_XSTATE_BNDREGS))
1528 for (i = I387_BND0R_REGNUM (tdep);
1529 i < I387_BNDCFGU_REGNUM (tdep); i++)
1531 regcache_raw_collect (regcache, i, raw);
1532 p = XSAVE_MPX_ADDR (tdep, regs, i);
1533 if (memcmp (raw, p, 16))
1535 xstate_bv |= X86_XSTATE_BNDREGS;
1536 memcpy (p, raw, 16);
1540 /* Check if any upper MPX registers are changed. */
1541 if ((tdep->xcr0 & X86_XSTATE_BNDCFG))
1542 for (i = I387_BNDCFGU_REGNUM (tdep);
1543 i < I387_MPXEND_REGNUM (tdep); i++)
1545 regcache_raw_collect (regcache, i, raw);
1546 p = XSAVE_MPX_ADDR (tdep, regs, i);
1547 if (memcmp (raw, p, 8))
1549 xstate_bv |= X86_XSTATE_BNDCFG;
1554 /* Check if any SSE registers are changed. */
1555 if ((tdep->xcr0 & X86_XSTATE_SSE))
1556 for (i = I387_XMM0_REGNUM (tdep);
1557 i < I387_MXCSR_REGNUM (tdep); i++)
1559 regcache_raw_collect (regcache, i, raw);
1560 p = FXSAVE_ADDR (tdep, regs, i);
1561 if (memcmp (raw, p, 16))
1563 xstate_bv |= X86_XSTATE_SSE;
1564 memcpy (p, raw, 16);
1568 /* Check if any X87 registers are changed. */
1569 if ((tdep->xcr0 & X86_XSTATE_X87))
1570 for (i = I387_ST0_REGNUM (tdep);
1571 i < I387_FCTRL_REGNUM (tdep); i++)
1573 regcache_raw_collect (regcache, i, raw);
1574 p = FXSAVE_ADDR (tdep, regs, i);
1575 if (memcmp (raw, p, 10))
1577 xstate_bv |= X86_XSTATE_X87;
1578 memcpy (p, raw, 10);
1584 /* Check if REGNUM is changed. */
1585 regcache_raw_collect (regcache, regnum, raw);
1590 internal_error (__FILE__, __LINE__,
1591 _("invalid i387 regclass"));
1594 /* This is a PKEYS register. */
1595 p = XSAVE_PKEYS_ADDR (tdep, regs, regnum);
1596 if (memcmp (raw, p, 4) != 0)
1598 xstate_bv |= X86_XSTATE_PKRU;
1604 /* This is a ZMM register. */
1605 p = XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, regnum);
1606 if (memcmp (raw, p, 32) != 0)
1608 xstate_bv |= (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM);
1609 memcpy (p, raw, 32);
1613 /* This is a AVX512 mask register. */
1614 p = XSAVE_AVX512_K_ADDR (tdep, regs, regnum);
1615 if (memcmp (raw, p, 8) != 0)
1617 xstate_bv |= X86_XSTATE_K;
1622 case avx512_ymmh_avx512:
1623 /* This is an upper YMM16-31 register. */
1624 p = XSAVE_YMM_AVX512_ADDR (tdep, regs, regnum);
1625 if (memcmp (raw, p, 16) != 0)
1627 xstate_bv |= X86_XSTATE_ZMM;
1628 memcpy (p, raw, 16);
1632 case avx512_xmm_avx512:
1633 /* This is an upper XMM16-31 register. */
1634 p = XSAVE_XMM_AVX512_ADDR (tdep, regs, regnum);
1635 if (memcmp (raw, p, 16) != 0)
1637 xstate_bv |= X86_XSTATE_ZMM;
1638 memcpy (p, raw, 16);
1643 /* This is an upper YMM register. */
1644 p = XSAVE_AVXH_ADDR (tdep, regs, regnum);
1645 if (memcmp (raw, p, 16))
1647 xstate_bv |= X86_XSTATE_AVX;
1648 memcpy (p, raw, 16);
1653 if (regnum < I387_BNDCFGU_REGNUM (tdep))
1655 regcache_raw_collect (regcache, regnum, raw);
1656 p = XSAVE_MPX_ADDR (tdep, regs, regnum);
1657 if (memcmp (raw, p, 16))
1659 xstate_bv |= X86_XSTATE_BNDREGS;
1660 memcpy (p, raw, 16);
1665 p = XSAVE_MPX_ADDR (tdep, regs, regnum);
1666 xstate_bv |= X86_XSTATE_BNDCFG;
1672 /* This is an SSE register. */
1673 p = FXSAVE_ADDR (tdep, regs, regnum);
1674 if (memcmp (raw, p, 16))
1676 xstate_bv |= X86_XSTATE_SSE;
1677 memcpy (p, raw, 16);
1682 /* This is an x87 register. */
1683 p = FXSAVE_ADDR (tdep, regs, regnum);
1684 if (memcmp (raw, p, 10))
1686 xstate_bv |= X86_XSTATE_X87;
1687 memcpy (p, raw, 10);
1693 /* Update the corresponding bits in `xstate_bv' if any SSE/AVX
1694 registers are changed. */
1697 /* The supported bits in `xstat_bv' are 8 bytes. */
1698 initial_xstate_bv |= xstate_bv;
1699 store_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs),
1706 internal_error (__FILE__, __LINE__,
1707 _("invalid i387 regclass"));
1718 case avx512_ymmh_avx512:
1719 case avx512_xmm_avx512:
1721 /* Register REGNUM has been updated. Return. */
1727 /* Return if REGNUM isn't changed. */
1728 if (regclass != all)
1733 /* Only handle x87 control registers. */
1734 for (i = I387_FCTRL_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
1735 if (regnum == -1 || regnum == i)
1737 /* Most of the FPU control registers occupy only 16 bits in
1738 the xsave extended state. Give those a special treatment. */
1739 if (i != I387_FIOFF_REGNUM (tdep)
1740 && i != I387_FOOFF_REGNUM (tdep))
1744 regcache_raw_collect (regcache, i, buf);
1746 if (i == I387_FOP_REGNUM (tdep))
1748 /* The opcode occupies only 11 bits. Make sure we
1749 don't touch the other bits. */
1750 buf[1] &= ((1 << 3) - 1);
1751 buf[1] |= ((FXSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
1753 else if (i == I387_FTAG_REGNUM (tdep))
1755 /* Converting back is much easier. */
1757 unsigned short ftag;
1760 ftag = (buf[1] << 8) | buf[0];
1764 for (fpreg = 7; fpreg >= 0; fpreg--)
1766 int tag = (ftag >> (fpreg * 2)) & 3;
1769 buf[0] |= (1 << fpreg);
1772 memcpy (FXSAVE_ADDR (tdep, regs, i), buf, 2);
1775 regcache_raw_collect (regcache, i, FXSAVE_ADDR (tdep, regs, i));
1778 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
1779 regcache_raw_collect (regcache, I387_MXCSR_REGNUM (tdep),
1780 FXSAVE_MXCSR_ADDR (regs));
1783 /* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
1787 i387_tag (const gdb_byte *raw)
1790 unsigned int exponent;
1791 unsigned long fraction[2];
1793 integer = raw[7] & 0x80;
1794 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
1795 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
1796 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
1797 | (raw[5] << 8) | raw[4]);
1799 if (exponent == 0x7fff)
1804 else if (exponent == 0x0000)
1806 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
1832 /* Prepare the FPU stack in REGCACHE for a function return. */
1835 i387_return_value (struct gdbarch *gdbarch, struct regcache *regcache)
1837 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1840 /* Set the top of the floating-point register stack to 7. The
1841 actual value doesn't really matter, but 7 is what a normal
1842 function return would end up with if the program started out with
1843 a freshly initialized FPU. */
1844 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
1846 regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
1848 /* Mark %st(1) through %st(7) as empty. Since we set the top of the
1849 floating-point register stack to 7, the appropriate value for the
1850 tag word is 0x3fff. */
1851 regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
1855 /* See i387-tdep.h. */
1858 i387_reset_bnd_regs (struct gdbarch *gdbarch, struct regcache *regcache)
1860 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1862 if (I387_BND0R_REGNUM (tdep) > 0)
1864 gdb_byte bnd_buf[16];
1866 memset (bnd_buf, 0, 16);
1867 for (int i = 0; i < I387_NUM_BND_REGS; i++)
1868 regcache_raw_write (regcache, I387_BND0R_REGNUM (tdep) + i, bnd_buf);