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
, I387_MXCSR_INIT_VAL
);
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)])
896 /* Extract from XSAVE a bitset of the features that are available on the
897 target, but which have not yet been enabled. */
900 i387_xsave_get_clear_bv (struct gdbarch
*gdbarch
, const void *xsave
)
902 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
903 const gdb_byte
*regs
= (const gdb_byte
*) xsave
;
904 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
906 /* Get `xstat_bv'. The supported bits in `xstat_bv' are 8 bytes. */
907 ULONGEST xstate_bv
= extract_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs
),
910 /* Clear part in vector registers if its bit in xstat_bv is zero. */
911 ULONGEST clear_bv
= (~(xstate_bv
)) & tdep
->xcr0
;
916 /* Similar to i387_supply_fxsave, but use XSAVE extended state. */
919 i387_supply_xsave (struct regcache
*regcache
, int regnum
,
922 struct gdbarch
*gdbarch
= regcache
->arch ();
923 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
924 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
925 const gdb_byte
*regs
= (const gdb_byte
*) xsave
;
928 static const gdb_byte zero
[I386_MAX_REGISTER_SIZE
] = { 0 };
938 avx512_ymmh_avx512
= 0x40,
939 avx512_xmm_avx512
= 0x80,
941 all
= x87
| sse
| avxh
| mpx
| avx512_k
| avx512_zmm_h
942 | avx512_ymmh_avx512
| avx512_xmm_avx512
| pkeys
945 gdb_assert (regs
!= NULL
);
946 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
947 gdb_assert (tdep
->num_xmm_regs
> 0);
951 else if (regnum
>= I387_PKRU_REGNUM (tdep
)
952 && regnum
< I387_PKEYSEND_REGNUM (tdep
))
954 else if (regnum
>= I387_ZMM0H_REGNUM (tdep
)
955 && regnum
< I387_ZMMENDH_REGNUM (tdep
))
956 regclass
= avx512_zmm_h
;
957 else if (regnum
>= I387_K0_REGNUM (tdep
)
958 && regnum
< I387_KEND_REGNUM (tdep
))
960 else if (regnum
>= I387_YMM16H_REGNUM (tdep
)
961 && regnum
< I387_YMMH_AVX512_END_REGNUM (tdep
))
962 regclass
= avx512_ymmh_avx512
;
963 else if (regnum
>= I387_XMM16_REGNUM (tdep
)
964 && regnum
< I387_XMM_AVX512_END_REGNUM (tdep
))
965 regclass
= avx512_xmm_avx512
;
966 else if (regnum
>= I387_YMM0H_REGNUM (tdep
)
967 && regnum
< I387_YMMENDH_REGNUM (tdep
))
969 else if (regnum
>= I387_BND0R_REGNUM (tdep
)
970 && regnum
< I387_MPXEND_REGNUM (tdep
))
972 else if (regnum
>= I387_XMM0_REGNUM (tdep
)
973 && regnum
< I387_MXCSR_REGNUM (tdep
))
975 else if (regnum
>= I387_ST0_REGNUM (tdep
)
976 && regnum
< I387_FCTRL_REGNUM (tdep
))
981 clear_bv
= i387_xsave_get_clear_bv (gdbarch
, xsave
);
983 /* With the delayed xsave mechanism, in between the program
984 starting, and the program accessing the vector registers for the
985 first time, the register's values are invalid. The kernel
986 initializes register states to zero when they are set the first
987 time in a program. This means that from the user-space programs'
988 perspective, it's the same as if the registers have always been
989 zero from the start of the program. Therefore, the debugger
990 should provide the same illusion to the user. */
998 if ((clear_bv
& X86_XSTATE_PKRU
))
999 regcache_raw_supply (regcache
, regnum
, zero
);
1001 regcache_raw_supply (regcache
, regnum
,
1002 XSAVE_PKEYS_ADDR (tdep
, regs
, regnum
));
1006 if ((clear_bv
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
1007 regcache_raw_supply (regcache
, regnum
, zero
);
1009 regcache_raw_supply (regcache
, regnum
,
1010 XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, regnum
));
1014 if ((clear_bv
& X86_XSTATE_K
))
1015 regcache_raw_supply (regcache
, regnum
, zero
);
1017 regcache_raw_supply (regcache
, regnum
,
1018 XSAVE_AVX512_K_ADDR (tdep
, regs
, regnum
));
1021 case avx512_ymmh_avx512
:
1022 if ((clear_bv
& X86_XSTATE_ZMM
))
1023 regcache_raw_supply (regcache
, regnum
, zero
);
1025 regcache_raw_supply (regcache
, regnum
,
1026 XSAVE_YMM_AVX512_ADDR (tdep
, regs
, regnum
));
1029 case avx512_xmm_avx512
:
1030 if ((clear_bv
& X86_XSTATE_ZMM
))
1031 regcache_raw_supply (regcache
, regnum
, zero
);
1033 regcache_raw_supply (regcache
, regnum
,
1034 XSAVE_XMM_AVX512_ADDR (tdep
, regs
, regnum
));
1038 if ((clear_bv
& X86_XSTATE_AVX
))
1039 regcache_raw_supply (regcache
, regnum
, zero
);
1041 regcache_raw_supply (regcache
, regnum
,
1042 XSAVE_AVXH_ADDR (tdep
, regs
, regnum
));
1046 if ((clear_bv
& X86_XSTATE_BNDREGS
))
1047 regcache_raw_supply (regcache
, regnum
, zero
);
1049 regcache_raw_supply (regcache
, regnum
,
1050 XSAVE_MPX_ADDR (tdep
, regs
, regnum
));
1054 if ((clear_bv
& X86_XSTATE_SSE
))
1055 regcache_raw_supply (regcache
, regnum
, zero
);
1057 regcache_raw_supply (regcache
, regnum
,
1058 FXSAVE_ADDR (tdep
, regs
, regnum
));
1062 if ((clear_bv
& X86_XSTATE_X87
))
1063 regcache_raw_supply (regcache
, regnum
, zero
);
1065 regcache_raw_supply (regcache
, regnum
,
1066 FXSAVE_ADDR (tdep
, regs
, regnum
));
1070 /* Handle PKEYS registers. */
1071 if ((tdep
->xcr0
& X86_XSTATE_PKRU
))
1073 if ((clear_bv
& X86_XSTATE_PKRU
))
1075 for (i
= I387_PKRU_REGNUM (tdep
);
1076 i
< I387_PKEYSEND_REGNUM (tdep
);
1078 regcache_raw_supply (regcache
, i
, zero
);
1082 for (i
= I387_PKRU_REGNUM (tdep
);
1083 i
< I387_PKEYSEND_REGNUM (tdep
);
1085 regcache_raw_supply (regcache
, i
,
1086 XSAVE_PKEYS_ADDR (tdep
, regs
, i
));
1090 /* Handle the upper ZMM registers. */
1091 if ((tdep
->xcr0
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
1093 if ((clear_bv
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
1095 for (i
= I387_ZMM0H_REGNUM (tdep
);
1096 i
< I387_ZMMENDH_REGNUM (tdep
);
1098 regcache_raw_supply (regcache
, i
, zero
);
1102 for (i
= I387_ZMM0H_REGNUM (tdep
);
1103 i
< I387_ZMMENDH_REGNUM (tdep
);
1105 regcache_raw_supply (regcache
, i
,
1106 XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, i
));
1110 /* Handle AVX512 OpMask registers. */
1111 if ((tdep
->xcr0
& X86_XSTATE_K
))
1113 if ((clear_bv
& X86_XSTATE_K
))
1115 for (i
= I387_K0_REGNUM (tdep
);
1116 i
< I387_KEND_REGNUM (tdep
);
1118 regcache_raw_supply (regcache
, i
, zero
);
1122 for (i
= I387_K0_REGNUM (tdep
);
1123 i
< I387_KEND_REGNUM (tdep
);
1125 regcache_raw_supply (regcache
, i
,
1126 XSAVE_AVX512_K_ADDR (tdep
, regs
, i
));
1130 /* Handle the YMM_AVX512 registers. */
1131 if ((tdep
->xcr0
& X86_XSTATE_ZMM
))
1133 if ((clear_bv
& X86_XSTATE_ZMM
))
1135 for (i
= I387_YMM16H_REGNUM (tdep
);
1136 i
< I387_YMMH_AVX512_END_REGNUM (tdep
);
1138 regcache_raw_supply (regcache
, i
, zero
);
1139 for (i
= I387_XMM16_REGNUM (tdep
);
1140 i
< I387_XMM_AVX512_END_REGNUM (tdep
);
1142 regcache_raw_supply (regcache
, i
, zero
);
1146 for (i
= I387_YMM16H_REGNUM (tdep
);
1147 i
< I387_YMMH_AVX512_END_REGNUM (tdep
);
1149 regcache_raw_supply (regcache
, i
,
1150 XSAVE_YMM_AVX512_ADDR (tdep
, regs
, i
));
1151 for (i
= I387_XMM16_REGNUM (tdep
);
1152 i
< I387_XMM_AVX512_END_REGNUM (tdep
);
1154 regcache_raw_supply (regcache
, i
,
1155 XSAVE_XMM_AVX512_ADDR (tdep
, regs
, i
));
1158 /* Handle the upper YMM registers. */
1159 if ((tdep
->xcr0
& X86_XSTATE_AVX
))
1161 if ((clear_bv
& X86_XSTATE_AVX
))
1163 for (i
= I387_YMM0H_REGNUM (tdep
);
1164 i
< I387_YMMENDH_REGNUM (tdep
);
1166 regcache_raw_supply (regcache
, i
, zero
);
1170 for (i
= I387_YMM0H_REGNUM (tdep
);
1171 i
< I387_YMMENDH_REGNUM (tdep
);
1173 regcache_raw_supply (regcache
, i
,
1174 XSAVE_AVXH_ADDR (tdep
, regs
, i
));
1178 /* Handle the MPX registers. */
1179 if ((tdep
->xcr0
& X86_XSTATE_BNDREGS
))
1181 if (clear_bv
& X86_XSTATE_BNDREGS
)
1183 for (i
= I387_BND0R_REGNUM (tdep
);
1184 i
< I387_BNDCFGU_REGNUM (tdep
); i
++)
1185 regcache_raw_supply (regcache
, i
, zero
);
1189 for (i
= I387_BND0R_REGNUM (tdep
);
1190 i
< I387_BNDCFGU_REGNUM (tdep
); i
++)
1191 regcache_raw_supply (regcache
, i
,
1192 XSAVE_MPX_ADDR (tdep
, regs
, i
));
1196 /* Handle the MPX registers. */
1197 if ((tdep
->xcr0
& X86_XSTATE_BNDCFG
))
1199 if (clear_bv
& X86_XSTATE_BNDCFG
)
1201 for (i
= I387_BNDCFGU_REGNUM (tdep
);
1202 i
< I387_MPXEND_REGNUM (tdep
); i
++)
1203 regcache_raw_supply (regcache
, i
, zero
);
1207 for (i
= I387_BNDCFGU_REGNUM (tdep
);
1208 i
< I387_MPXEND_REGNUM (tdep
); i
++)
1209 regcache_raw_supply (regcache
, i
,
1210 XSAVE_MPX_ADDR (tdep
, regs
, i
));
1214 /* Handle the XMM registers. */
1215 if ((tdep
->xcr0
& X86_XSTATE_SSE
))
1217 if ((clear_bv
& X86_XSTATE_SSE
))
1219 for (i
= I387_XMM0_REGNUM (tdep
);
1220 i
< I387_MXCSR_REGNUM (tdep
);
1222 regcache_raw_supply (regcache
, i
, zero
);
1226 for (i
= I387_XMM0_REGNUM (tdep
);
1227 i
< I387_MXCSR_REGNUM (tdep
); i
++)
1228 regcache_raw_supply (regcache
, i
,
1229 FXSAVE_ADDR (tdep
, regs
, i
));
1233 /* Handle the x87 registers. */
1234 if ((tdep
->xcr0
& X86_XSTATE_X87
))
1236 if ((clear_bv
& X86_XSTATE_X87
))
1238 for (i
= I387_ST0_REGNUM (tdep
);
1239 i
< I387_FCTRL_REGNUM (tdep
);
1241 regcache_raw_supply (regcache
, i
, zero
);
1245 for (i
= I387_ST0_REGNUM (tdep
);
1246 i
< I387_FCTRL_REGNUM (tdep
);
1248 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
1254 /* Only handle x87 control registers. */
1255 for (i
= I387_FCTRL_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
1256 if (regnum
== -1 || regnum
== i
)
1258 if (clear_bv
& X86_XSTATE_X87
)
1260 if (i
== I387_FCTRL_REGNUM (tdep
))
1264 store_unsigned_integer (buf
, 4, byte_order
,
1265 I387_FCTRL_INIT_VAL
);
1266 regcache_raw_supply (regcache
, i
, buf
);
1268 else if (i
== I387_FTAG_REGNUM (tdep
))
1272 store_unsigned_integer (buf
, 4, byte_order
, 0xffff);
1273 regcache_raw_supply (regcache
, i
, buf
);
1276 regcache_raw_supply (regcache
, i
, zero
);
1278 /* Most of the FPU control registers occupy only 16 bits in
1279 the xsave extended state. Give those a special treatment. */
1280 else if (i
!= I387_FIOFF_REGNUM (tdep
)
1281 && i
!= I387_FOOFF_REGNUM (tdep
))
1285 memcpy (val
, FXSAVE_ADDR (tdep
, regs
, i
), 2);
1286 val
[2] = val
[3] = 0;
1287 if (i
== I387_FOP_REGNUM (tdep
))
1288 val
[1] &= ((1 << 3) - 1);
1289 else if (i
== I387_FTAG_REGNUM (tdep
))
1291 /* The fxsave area contains a simplified version of
1292 the tag word. We have to look at the actual 80-bit
1293 FP data to recreate the traditional i387 tag word. */
1295 unsigned long ftag
= 0;
1299 top
= ((FXSAVE_ADDR (tdep
, regs
,
1300 I387_FSTAT_REGNUM (tdep
)))[1] >> 3);
1303 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
1307 if (val
[0] & (1 << fpreg
))
1309 int thisreg
= (fpreg
+ 8 - top
) % 8
1310 + I387_ST0_REGNUM (tdep
);
1311 tag
= i387_tag (FXSAVE_ADDR (tdep
, regs
, thisreg
));
1314 tag
= 3; /* Empty */
1316 ftag
|= tag
<< (2 * fpreg
);
1318 val
[0] = ftag
& 0xff;
1319 val
[1] = (ftag
>> 8) & 0xff;
1321 regcache_raw_supply (regcache
, i
, val
);
1324 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
1327 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
1329 /* The MXCSR register is placed into the xsave buffer if either the
1330 AVX or SSE features are enabled. */
1331 if ((clear_bv
& (X86_XSTATE_AVX
| X86_XSTATE_SSE
))
1332 == (X86_XSTATE_AVX
| X86_XSTATE_SSE
))
1336 store_unsigned_integer (buf
, 4, byte_order
, I387_MXCSR_INIT_VAL
);
1337 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
), buf
);
1340 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
),
1341 FXSAVE_MXCSR_ADDR (regs
));
1345 /* Similar to i387_collect_fxsave, but use XSAVE extended state. */
1348 i387_collect_xsave (const struct regcache
*regcache
, int regnum
,
1349 void *xsave
, int gcore
)
1351 struct gdbarch
*gdbarch
= regcache
->arch ();
1352 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1353 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1354 gdb_byte
*p
, *regs
= (gdb_byte
*) xsave
;
1355 gdb_byte raw
[I386_MAX_REGISTER_SIZE
];
1356 ULONGEST initial_xstate_bv
, clear_bv
, xstate_bv
= 0;
1360 x87_ctrl_or_mxcsr
= 0x1,
1366 avx512_zmm_h
= 0x40,
1367 avx512_ymmh_avx512
= 0x80,
1368 avx512_xmm_avx512
= 0x100,
1370 all
= x87
| sse
| avxh
| mpx
| avx512_k
| avx512_zmm_h
1371 | avx512_ymmh_avx512
| avx512_xmm_avx512
| pkeys
1374 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
1375 gdb_assert (tdep
->num_xmm_regs
> 0);
1379 else if (regnum
>= I387_PKRU_REGNUM (tdep
)
1380 && regnum
< I387_PKEYSEND_REGNUM (tdep
))
1382 else if (regnum
>= I387_ZMM0H_REGNUM (tdep
)
1383 && regnum
< I387_ZMMENDH_REGNUM (tdep
))
1384 regclass
= avx512_zmm_h
;
1385 else if (regnum
>= I387_K0_REGNUM (tdep
)
1386 && regnum
< I387_KEND_REGNUM (tdep
))
1387 regclass
= avx512_k
;
1388 else if (regnum
>= I387_YMM16H_REGNUM (tdep
)
1389 && regnum
< I387_YMMH_AVX512_END_REGNUM (tdep
))
1390 regclass
= avx512_ymmh_avx512
;
1391 else if (regnum
>= I387_XMM16_REGNUM (tdep
)
1392 && regnum
< I387_XMM_AVX512_END_REGNUM (tdep
))
1393 regclass
= avx512_xmm_avx512
;
1394 else if (regnum
>= I387_YMM0H_REGNUM (tdep
)
1395 && regnum
< I387_YMMENDH_REGNUM (tdep
))
1397 else if (regnum
>= I387_BND0R_REGNUM (tdep
)
1398 && regnum
< I387_MPXEND_REGNUM (tdep
))
1400 else if (regnum
>= I387_XMM0_REGNUM (tdep
)
1401 && regnum
< I387_MXCSR_REGNUM (tdep
))
1403 else if (regnum
>= I387_ST0_REGNUM (tdep
)
1404 && regnum
< I387_FCTRL_REGNUM (tdep
))
1406 else if ((regnum
>= I387_FCTRL_REGNUM (tdep
)
1407 && regnum
< I387_XMM0_REGNUM (tdep
))
1408 || regnum
== I387_MXCSR_REGNUM (tdep
))
1409 regclass
= x87_ctrl_or_mxcsr
;
1411 internal_error (__FILE__
, __LINE__
, _("invalid i387 regnum %d"), regnum
);
1415 /* Clear XSAVE extended state. */
1416 memset (regs
, 0, X86_XSTATE_SIZE (tdep
->xcr0
));
1418 /* Update XCR0 and `xstate_bv' with XCR0 for gcore. */
1419 if (tdep
->xsave_xcr0_offset
!= -1)
1420 memcpy (regs
+ tdep
->xsave_xcr0_offset
, &tdep
->xcr0
, 8);
1421 memcpy (XSAVE_XSTATE_BV_ADDR (regs
), &tdep
->xcr0
, 8);
1424 /* The supported bits in `xstat_bv' are 8 bytes. */
1425 initial_xstate_bv
= extract_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs
),
1427 clear_bv
= (~(initial_xstate_bv
)) & tdep
->xcr0
;
1429 /* The XSAVE buffer was filled lazily by the kernel. Only those
1430 features that are enabled were written into the buffer, disabled
1431 features left the buffer uninitialised. In order to identify if any
1432 registers have changed we will be comparing the register cache
1433 version to the version in the XSAVE buffer, it is important then that
1434 at this point we initialise to the default values any features in
1435 XSAVE that are not yet initialised.
1437 This could be made more efficient, we know which features (from
1438 REGNUM) we will be potentially updating, and could limit ourselves to
1439 only clearing that feature. However, the extra complexity does not
1440 seem justified at this point. */
1443 if ((clear_bv
& X86_XSTATE_PKRU
))
1444 for (i
= I387_PKRU_REGNUM (tdep
);
1445 i
< I387_PKEYSEND_REGNUM (tdep
); i
++)
1446 memset (XSAVE_PKEYS_ADDR (tdep
, regs
, i
), 0, 4);
1448 if ((clear_bv
& X86_XSTATE_BNDREGS
))
1449 for (i
= I387_BND0R_REGNUM (tdep
);
1450 i
< I387_BNDCFGU_REGNUM (tdep
); i
++)
1451 memset (XSAVE_MPX_ADDR (tdep
, regs
, i
), 0, 16);
1453 if ((clear_bv
& X86_XSTATE_BNDCFG
))
1454 for (i
= I387_BNDCFGU_REGNUM (tdep
);
1455 i
< I387_MPXEND_REGNUM (tdep
); i
++)
1456 memset (XSAVE_MPX_ADDR (tdep
, regs
, i
), 0, 8);
1458 if ((clear_bv
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
1459 for (i
= I387_ZMM0H_REGNUM (tdep
);
1460 i
< I387_ZMMENDH_REGNUM (tdep
); i
++)
1461 memset (XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, i
), 0, 32);
1463 if ((clear_bv
& X86_XSTATE_K
))
1464 for (i
= I387_K0_REGNUM (tdep
);
1465 i
< I387_KEND_REGNUM (tdep
); i
++)
1466 memset (XSAVE_AVX512_K_ADDR (tdep
, regs
, i
), 0, 8);
1468 if ((clear_bv
& X86_XSTATE_ZMM
))
1470 for (i
= I387_YMM16H_REGNUM (tdep
);
1471 i
< I387_YMMH_AVX512_END_REGNUM (tdep
); i
++)
1472 memset (XSAVE_YMM_AVX512_ADDR (tdep
, regs
, i
), 0, 16);
1473 for (i
= I387_XMM16_REGNUM (tdep
);
1474 i
< I387_XMM_AVX512_END_REGNUM (tdep
); i
++)
1475 memset (XSAVE_XMM_AVX512_ADDR (tdep
, regs
, i
), 0, 16);
1478 if ((clear_bv
& X86_XSTATE_AVX
))
1479 for (i
= I387_YMM0H_REGNUM (tdep
);
1480 i
< I387_YMMENDH_REGNUM (tdep
); i
++)
1481 memset (XSAVE_AVXH_ADDR (tdep
, regs
, i
), 0, 16);
1483 if ((clear_bv
& X86_XSTATE_SSE
))
1484 for (i
= I387_XMM0_REGNUM (tdep
);
1485 i
< I387_MXCSR_REGNUM (tdep
); i
++)
1486 memset (FXSAVE_ADDR (tdep
, regs
, i
), 0, 16);
1488 /* The mxcsr register is written into the xsave buffer if either AVX
1489 or SSE is enabled, so only clear it if both of those features
1490 require clearing. */
1491 if ((clear_bv
& (X86_XSTATE_AVX
| X86_XSTATE_SSE
))
1492 == (X86_XSTATE_AVX
| X86_XSTATE_SSE
))
1493 store_unsigned_integer (FXSAVE_MXCSR_ADDR (regs
), 2, byte_order
,
1494 I387_MXCSR_INIT_VAL
);
1496 if ((clear_bv
& X86_XSTATE_X87
))
1498 for (i
= I387_ST0_REGNUM (tdep
);
1499 i
< I387_FCTRL_REGNUM (tdep
); i
++)
1500 memset (FXSAVE_ADDR (tdep
, regs
, i
), 0, 10);
1502 for (i
= I387_FCTRL_REGNUM (tdep
);
1503 i
< I387_XMM0_REGNUM (tdep
); i
++)
1505 if (i
== I387_FCTRL_REGNUM (tdep
))
1506 store_unsigned_integer (FXSAVE_ADDR (tdep
, regs
, i
), 2,
1507 byte_order
, I387_FCTRL_INIT_VAL
);
1509 memset (FXSAVE_ADDR (tdep
, regs
, i
), 0,
1510 regcache_register_size (regcache
, i
));
1515 if (regclass
== all
)
1517 /* Check if any PKEYS registers are changed. */
1518 if ((tdep
->xcr0
& X86_XSTATE_PKRU
))
1519 for (i
= I387_PKRU_REGNUM (tdep
);
1520 i
< I387_PKEYSEND_REGNUM (tdep
); i
++)
1522 regcache_raw_collect (regcache
, i
, raw
);
1523 p
= XSAVE_PKEYS_ADDR (tdep
, regs
, i
);
1524 if (memcmp (raw
, p
, 4) != 0)
1526 xstate_bv
|= X86_XSTATE_PKRU
;
1531 /* Check if any ZMMH registers are changed. */
1532 if ((tdep
->xcr0
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
1533 for (i
= I387_ZMM0H_REGNUM (tdep
);
1534 i
< I387_ZMMENDH_REGNUM (tdep
); i
++)
1536 regcache_raw_collect (regcache
, i
, raw
);
1537 p
= XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, i
);
1538 if (memcmp (raw
, p
, 32) != 0)
1540 xstate_bv
|= (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
);
1541 memcpy (p
, raw
, 32);
1545 /* Check if any K registers are changed. */
1546 if ((tdep
->xcr0
& X86_XSTATE_K
))
1547 for (i
= I387_K0_REGNUM (tdep
);
1548 i
< I387_KEND_REGNUM (tdep
); i
++)
1550 regcache_raw_collect (regcache
, i
, raw
);
1551 p
= XSAVE_AVX512_K_ADDR (tdep
, regs
, i
);
1552 if (memcmp (raw
, p
, 8) != 0)
1554 xstate_bv
|= X86_XSTATE_K
;
1559 /* Check if any XMM or upper YMM registers are changed. */
1560 if ((tdep
->xcr0
& X86_XSTATE_ZMM
))
1562 for (i
= I387_YMM16H_REGNUM (tdep
);
1563 i
< I387_YMMH_AVX512_END_REGNUM (tdep
); i
++)
1565 regcache_raw_collect (regcache
, i
, raw
);
1566 p
= XSAVE_YMM_AVX512_ADDR (tdep
, regs
, i
);
1567 if (memcmp (raw
, p
, 16) != 0)
1569 xstate_bv
|= X86_XSTATE_ZMM
;
1570 memcpy (p
, raw
, 16);
1573 for (i
= I387_XMM16_REGNUM (tdep
);
1574 i
< I387_XMM_AVX512_END_REGNUM (tdep
); i
++)
1576 regcache_raw_collect (regcache
, i
, raw
);
1577 p
= XSAVE_XMM_AVX512_ADDR (tdep
, regs
, i
);
1578 if (memcmp (raw
, p
, 16) != 0)
1580 xstate_bv
|= X86_XSTATE_ZMM
;
1581 memcpy (p
, raw
, 16);
1586 /* Check if any upper MPX registers are changed. */
1587 if ((tdep
->xcr0
& X86_XSTATE_BNDREGS
))
1588 for (i
= I387_BND0R_REGNUM (tdep
);
1589 i
< I387_BNDCFGU_REGNUM (tdep
); i
++)
1591 regcache_raw_collect (regcache
, i
, raw
);
1592 p
= XSAVE_MPX_ADDR (tdep
, regs
, i
);
1593 if (memcmp (raw
, p
, 16))
1595 xstate_bv
|= X86_XSTATE_BNDREGS
;
1596 memcpy (p
, raw
, 16);
1600 /* Check if any upper MPX registers are changed. */
1601 if ((tdep
->xcr0
& X86_XSTATE_BNDCFG
))
1602 for (i
= I387_BNDCFGU_REGNUM (tdep
);
1603 i
< I387_MPXEND_REGNUM (tdep
); i
++)
1605 regcache_raw_collect (regcache
, i
, raw
);
1606 p
= XSAVE_MPX_ADDR (tdep
, regs
, i
);
1607 if (memcmp (raw
, p
, 8))
1609 xstate_bv
|= X86_XSTATE_BNDCFG
;
1614 /* Check if any upper YMM registers are changed. */
1615 if ((tdep
->xcr0
& X86_XSTATE_AVX
))
1616 for (i
= I387_YMM0H_REGNUM (tdep
);
1617 i
< I387_YMMENDH_REGNUM (tdep
); i
++)
1619 regcache_raw_collect (regcache
, i
, raw
);
1620 p
= XSAVE_AVXH_ADDR (tdep
, regs
, i
);
1621 if (memcmp (raw
, p
, 16))
1623 xstate_bv
|= X86_XSTATE_AVX
;
1624 memcpy (p
, raw
, 16);
1628 /* Check if any SSE registers are changed. */
1629 if ((tdep
->xcr0
& X86_XSTATE_SSE
))
1630 for (i
= I387_XMM0_REGNUM (tdep
);
1631 i
< I387_MXCSR_REGNUM (tdep
); i
++)
1633 regcache_raw_collect (regcache
, i
, raw
);
1634 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1635 if (memcmp (raw
, p
, 16))
1637 xstate_bv
|= X86_XSTATE_SSE
;
1638 memcpy (p
, raw
, 16);
1642 if ((tdep
->xcr0
& X86_XSTATE_AVX
) || (tdep
->xcr0
& X86_XSTATE_SSE
))
1644 i
= I387_MXCSR_REGNUM (tdep
);
1645 regcache_raw_collect (regcache
, i
, raw
);
1646 p
= FXSAVE_MXCSR_ADDR (regs
);
1647 if (memcmp (raw
, p
, 4))
1649 /* Now, we need to mark one of either SSE of AVX as enabled.
1650 We could pick either. What we do is check to see if one
1651 of the features is already enabled, if it is then we leave
1652 it at that, otherwise we pick SSE. */
1653 if ((xstate_bv
& (X86_XSTATE_SSE
| X86_XSTATE_AVX
)) == 0)
1654 xstate_bv
|= X86_XSTATE_SSE
;
1659 /* Check if any X87 registers are changed. Only the non-control
1660 registers are handled here, the control registers are all handled
1661 later on in this function. */
1662 if ((tdep
->xcr0
& X86_XSTATE_X87
))
1663 for (i
= I387_ST0_REGNUM (tdep
);
1664 i
< I387_FCTRL_REGNUM (tdep
); i
++)
1666 regcache_raw_collect (regcache
, i
, raw
);
1667 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1668 if (memcmp (raw
, p
, 10))
1670 xstate_bv
|= X86_XSTATE_X87
;
1671 memcpy (p
, raw
, 10);
1677 /* Check if REGNUM is changed. */
1678 regcache_raw_collect (regcache
, regnum
, raw
);
1683 internal_error (__FILE__
, __LINE__
,
1684 _("invalid i387 regclass"));
1687 /* This is a PKEYS register. */
1688 p
= XSAVE_PKEYS_ADDR (tdep
, regs
, regnum
);
1689 if (memcmp (raw
, p
, 4) != 0)
1691 xstate_bv
|= X86_XSTATE_PKRU
;
1697 /* This is a ZMM register. */
1698 p
= XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, regnum
);
1699 if (memcmp (raw
, p
, 32) != 0)
1701 xstate_bv
|= (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
);
1702 memcpy (p
, raw
, 32);
1706 /* This is a AVX512 mask register. */
1707 p
= XSAVE_AVX512_K_ADDR (tdep
, regs
, regnum
);
1708 if (memcmp (raw
, p
, 8) != 0)
1710 xstate_bv
|= X86_XSTATE_K
;
1715 case avx512_ymmh_avx512
:
1716 /* This is an upper YMM16-31 register. */
1717 p
= XSAVE_YMM_AVX512_ADDR (tdep
, regs
, regnum
);
1718 if (memcmp (raw
, p
, 16) != 0)
1720 xstate_bv
|= X86_XSTATE_ZMM
;
1721 memcpy (p
, raw
, 16);
1725 case avx512_xmm_avx512
:
1726 /* This is an upper XMM16-31 register. */
1727 p
= XSAVE_XMM_AVX512_ADDR (tdep
, regs
, regnum
);
1728 if (memcmp (raw
, p
, 16) != 0)
1730 xstate_bv
|= X86_XSTATE_ZMM
;
1731 memcpy (p
, raw
, 16);
1736 /* This is an upper YMM register. */
1737 p
= XSAVE_AVXH_ADDR (tdep
, regs
, regnum
);
1738 if (memcmp (raw
, p
, 16))
1740 xstate_bv
|= X86_XSTATE_AVX
;
1741 memcpy (p
, raw
, 16);
1746 if (regnum
< I387_BNDCFGU_REGNUM (tdep
))
1748 regcache_raw_collect (regcache
, regnum
, raw
);
1749 p
= XSAVE_MPX_ADDR (tdep
, regs
, regnum
);
1750 if (memcmp (raw
, p
, 16))
1752 xstate_bv
|= X86_XSTATE_BNDREGS
;
1753 memcpy (p
, raw
, 16);
1758 p
= XSAVE_MPX_ADDR (tdep
, regs
, regnum
);
1759 xstate_bv
|= X86_XSTATE_BNDCFG
;
1765 /* This is an SSE register. */
1766 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
1767 if (memcmp (raw
, p
, 16))
1769 xstate_bv
|= X86_XSTATE_SSE
;
1770 memcpy (p
, raw
, 16);
1775 /* This is an x87 register. */
1776 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
1777 if (memcmp (raw
, p
, 10))
1779 xstate_bv
|= X86_XSTATE_X87
;
1780 memcpy (p
, raw
, 10);
1784 case x87_ctrl_or_mxcsr
:
1785 /* We only handle MXCSR here. All other x87 control registers
1786 are handled separately below. */
1787 if (regnum
== I387_MXCSR_REGNUM (tdep
))
1789 p
= FXSAVE_MXCSR_ADDR (regs
);
1790 if (memcmp (raw
, p
, 2))
1792 /* We're only setting MXCSR, so check the initial state
1793 to see if either of AVX or SSE are already enabled.
1794 If they are then we'll attribute this changed MXCSR to
1795 that feature. If neither feature is enabled, then
1796 we'll attribute this change to the SSE feature. */
1797 xstate_bv
|= (initial_xstate_bv
1798 & (X86_XSTATE_AVX
| X86_XSTATE_SSE
));
1799 if ((xstate_bv
& (X86_XSTATE_AVX
| X86_XSTATE_SSE
)) == 0)
1800 xstate_bv
|= X86_XSTATE_SSE
;
1807 /* Only handle x87 control registers. */
1808 for (i
= I387_FCTRL_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
1809 if (regnum
== -1 || regnum
== i
)
1811 /* Most of the FPU control registers occupy only 16 bits in
1812 the xsave extended state. Give those a special treatment. */
1813 if (i
!= I387_FIOFF_REGNUM (tdep
)
1814 && i
!= I387_FOOFF_REGNUM (tdep
))
1818 regcache_raw_collect (regcache
, i
, buf
);
1820 if (i
== I387_FOP_REGNUM (tdep
))
1822 /* The opcode occupies only 11 bits. Make sure we
1823 don't touch the other bits. */
1824 buf
[1] &= ((1 << 3) - 1);
1825 buf
[1] |= ((FXSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
1827 else if (i
== I387_FTAG_REGNUM (tdep
))
1829 /* Converting back is much easier. */
1831 unsigned short ftag
;
1834 ftag
= (buf
[1] << 8) | buf
[0];
1838 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
1840 int tag
= (ftag
>> (fpreg
* 2)) & 3;
1843 buf
[0] |= (1 << fpreg
);
1846 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1847 if (memcmp (p
, buf
, 2))
1849 xstate_bv
|= X86_XSTATE_X87
;
1857 regcache_raw_collect (regcache
, i
, raw
);
1858 regsize
= regcache_register_size (regcache
, i
);
1859 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1860 if (memcmp (raw
, p
, regsize
))
1862 xstate_bv
|= X86_XSTATE_X87
;
1863 memcpy (p
, raw
, regsize
);
1868 /* Update the corresponding bits in `xstate_bv' if any
1869 registers are changed. */
1872 /* The supported bits in `xstat_bv' are 8 bytes. */
1873 initial_xstate_bv
|= xstate_bv
;
1874 store_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs
),
1880 /* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
1884 i387_tag (const gdb_byte
*raw
)
1887 unsigned int exponent
;
1888 unsigned long fraction
[2];
1890 integer
= raw
[7] & 0x80;
1891 exponent
= (((raw
[9] & 0x7f) << 8) | raw
[8]);
1892 fraction
[0] = ((raw
[3] << 24) | (raw
[2] << 16) | (raw
[1] << 8) | raw
[0]);
1893 fraction
[1] = (((raw
[7] & 0x7f) << 24) | (raw
[6] << 16)
1894 | (raw
[5] << 8) | raw
[4]);
1896 if (exponent
== 0x7fff)
1901 else if (exponent
== 0x0000)
1903 if (fraction
[0] == 0x0000 && fraction
[1] == 0x0000 && !integer
)
1929 /* Prepare the FPU stack in REGCACHE for a function return. */
1932 i387_return_value (struct gdbarch
*gdbarch
, struct regcache
*regcache
)
1934 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1937 /* Set the top of the floating-point register stack to 7. The
1938 actual value doesn't really matter, but 7 is what a normal
1939 function return would end up with if the program started out with
1940 a freshly initialized FPU. */
1941 regcache_raw_read_unsigned (regcache
, I387_FSTAT_REGNUM (tdep
), &fstat
);
1943 regcache_raw_write_unsigned (regcache
, I387_FSTAT_REGNUM (tdep
), fstat
);
1945 /* Mark %st(1) through %st(7) as empty. Since we set the top of the
1946 floating-point register stack to 7, the appropriate value for the
1947 tag word is 0x3fff. */
1948 regcache_raw_write_unsigned (regcache
, I387_FTAG_REGNUM (tdep
), 0x3fff);
1952 /* See i387-tdep.h. */
1955 i387_reset_bnd_regs (struct gdbarch
*gdbarch
, struct regcache
*regcache
)
1957 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1959 if (I387_BND0R_REGNUM (tdep
) > 0)
1961 gdb_byte bnd_buf
[16];
1963 memset (bnd_buf
, 0, 16);
1964 for (int i
= 0; i
< I387_NUM_BND_REGS
; i
++)
1965 regcache_raw_write (regcache
, I387_BND0R_REGNUM (tdep
) + i
, bnd_buf
);