1 /* Intel 387 floating point stuff.
3 Copyright (C) 1988-2017 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/>. */
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 floatformat
*fmt
44 = floatformat_from_type (i387_ext_type (gdbarch
));
45 std::string str
= floatformat_to_string (fmt
, raw
, " %-+27.19g");
46 fprintf_filtered (file
, "%s", str
.c_str ());
49 /* Print the classification for the register contents RAW. */
52 print_i387_ext (struct gdbarch
*gdbarch
,
53 const gdb_byte
*raw
, struct ui_file
*file
)
57 unsigned int exponent
;
58 unsigned long fraction
[2];
61 integer
= raw
[7] & 0x80;
62 exponent
= (((raw
[9] & 0x7f) << 8) | raw
[8]);
63 fraction
[0] = ((raw
[3] << 24) | (raw
[2] << 16) | (raw
[1] << 8) | raw
[0]);
64 fraction
[1] = (((raw
[7] & 0x7f) << 24) | (raw
[6] << 16)
65 | (raw
[5] << 8) | raw
[4]);
67 if (exponent
== 0x7fff && integer
)
69 if (fraction
[0] == 0x00000000 && fraction
[1] == 0x00000000)
71 fprintf_filtered (file
, " %cInf", (sign
? '-' : '+'));
72 else if (sign
&& fraction
[0] == 0x00000000 && fraction
[1] == 0x40000000)
73 /* Real Indefinite (QNaN). */
74 fputs_unfiltered (" Real Indefinite (QNaN)", file
);
75 else if (fraction
[1] & 0x40000000)
77 fputs_filtered (" QNaN", file
);
80 fputs_filtered (" SNaN", file
);
82 else if (exponent
< 0x7fff && exponent
> 0x0000 && integer
)
84 print_i387_value (gdbarch
, raw
, file
);
85 else if (exponent
== 0x0000)
87 /* Denormal or zero. */
88 print_i387_value (gdbarch
, raw
, file
);
91 /* Pseudo-denormal. */
92 fputs_filtered (" Pseudo-denormal", file
);
93 else if (fraction
[0] || fraction
[1])
95 fputs_filtered (" Denormal", file
);
99 fputs_filtered (" Unsupported", file
);
102 /* Print the status word STATUS. If STATUS_P is false, then STATUS
106 print_i387_status_word (int status_p
,
107 unsigned int status
, struct ui_file
*file
)
109 fprintf_filtered (file
, "Status Word: ");
112 fprintf_filtered (file
, "%s\n", _("<unavailable>"));
116 fprintf_filtered (file
, "%s", hex_string_custom (status
, 4));
117 fputs_filtered (" ", file
);
118 fprintf_filtered (file
, " %s", (status
& 0x0001) ? "IE" : " ");
119 fprintf_filtered (file
, " %s", (status
& 0x0002) ? "DE" : " ");
120 fprintf_filtered (file
, " %s", (status
& 0x0004) ? "ZE" : " ");
121 fprintf_filtered (file
, " %s", (status
& 0x0008) ? "OE" : " ");
122 fprintf_filtered (file
, " %s", (status
& 0x0010) ? "UE" : " ");
123 fprintf_filtered (file
, " %s", (status
& 0x0020) ? "PE" : " ");
124 fputs_filtered (" ", file
);
125 fprintf_filtered (file
, " %s", (status
& 0x0080) ? "ES" : " ");
126 fputs_filtered (" ", file
);
127 fprintf_filtered (file
, " %s", (status
& 0x0040) ? "SF" : " ");
128 fputs_filtered (" ", file
);
129 fprintf_filtered (file
, " %s", (status
& 0x0100) ? "C0" : " ");
130 fprintf_filtered (file
, " %s", (status
& 0x0200) ? "C1" : " ");
131 fprintf_filtered (file
, " %s", (status
& 0x0400) ? "C2" : " ");
132 fprintf_filtered (file
, " %s", (status
& 0x4000) ? "C3" : " ");
134 fputs_filtered ("\n", file
);
136 fprintf_filtered (file
,
137 " TOP: %d\n", ((status
>> 11) & 7));
140 /* Print the control word CONTROL. If CONTROL_P is false, then
141 CONTROL was unavailable. */
144 print_i387_control_word (int control_p
,
145 unsigned int control
, struct ui_file
*file
)
147 fprintf_filtered (file
, "Control Word: ");
150 fprintf_filtered (file
, "%s\n", _("<unavailable>"));
154 fprintf_filtered (file
, "%s", hex_string_custom (control
, 4));
155 fputs_filtered (" ", file
);
156 fprintf_filtered (file
, " %s", (control
& 0x0001) ? "IM" : " ");
157 fprintf_filtered (file
, " %s", (control
& 0x0002) ? "DM" : " ");
158 fprintf_filtered (file
, " %s", (control
& 0x0004) ? "ZM" : " ");
159 fprintf_filtered (file
, " %s", (control
& 0x0008) ? "OM" : " ");
160 fprintf_filtered (file
, " %s", (control
& 0x0010) ? "UM" : " ");
161 fprintf_filtered (file
, " %s", (control
& 0x0020) ? "PM" : " ");
163 fputs_filtered ("\n", file
);
165 fputs_filtered (" PC: ", file
);
166 switch ((control
>> 8) & 3)
169 fputs_filtered ("Single Precision (24-bits)\n", file
);
172 fputs_filtered ("Reserved\n", file
);
175 fputs_filtered ("Double Precision (53-bits)\n", file
);
178 fputs_filtered ("Extended Precision (64-bits)\n", file
);
182 fputs_filtered (" RC: ", file
);
183 switch ((control
>> 10) & 3)
186 fputs_filtered ("Round to nearest\n", file
);
189 fputs_filtered ("Round down\n", file
);
192 fputs_filtered ("Round up\n", file
);
195 fputs_filtered ("Round toward zero\n", file
);
200 /* Print out the i387 floating point state. Note that we ignore FRAME
201 in the code below. That's OK since floating-point registers are
202 never saved on the stack. */
205 i387_print_float_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
206 struct frame_info
*frame
, const char *args
)
208 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_frame_arch (frame
));
228 gdb_assert (gdbarch
== get_frame_arch (frame
));
230 fctrl_p
= read_frame_register_unsigned (frame
,
231 I387_FCTRL_REGNUM (tdep
), &fctrl
);
232 fstat_p
= read_frame_register_unsigned (frame
,
233 I387_FSTAT_REGNUM (tdep
), &fstat
);
234 ftag_p
= read_frame_register_unsigned (frame
,
235 I387_FTAG_REGNUM (tdep
), &ftag
);
236 fiseg_p
= read_frame_register_unsigned (frame
,
237 I387_FISEG_REGNUM (tdep
), &fiseg
);
238 fioff_p
= read_frame_register_unsigned (frame
,
239 I387_FIOFF_REGNUM (tdep
), &fioff
);
240 foseg_p
= read_frame_register_unsigned (frame
,
241 I387_FOSEG_REGNUM (tdep
), &foseg
);
242 fooff_p
= read_frame_register_unsigned (frame
,
243 I387_FOOFF_REGNUM (tdep
), &fooff
);
244 fop_p
= read_frame_register_unsigned (frame
,
245 I387_FOP_REGNUM (tdep
), &fop
);
249 top
= ((fstat
>> 11) & 7);
251 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
253 struct value
*regval
;
258 fprintf_filtered (file
, "%sR%d: ", fpreg
== top
? "=>" : " ", fpreg
);
262 tag
= (ftag
>> (fpreg
* 2)) & 3;
267 fputs_filtered ("Valid ", file
);
270 fputs_filtered ("Zero ", file
);
273 fputs_filtered ("Special ", file
);
276 fputs_filtered ("Empty ", file
);
281 fputs_filtered ("Unknown ", file
);
283 regnum
= (fpreg
+ 8 - top
) % 8 + I387_ST0_REGNUM (tdep
);
284 regval
= get_frame_register_value (frame
, regnum
);
286 if (value_entirely_available (regval
))
288 const gdb_byte
*raw
= value_contents (regval
);
290 fputs_filtered ("0x", file
);
291 for (i
= 9; i
>= 0; i
--)
292 fprintf_filtered (file
, "%02x", raw
[i
]);
294 if (tag
!= -1 && tag
!= 3)
295 print_i387_ext (gdbarch
, raw
, file
);
298 fprintf_filtered (file
, "%s", _("<unavailable>"));
300 fputs_filtered ("\n", file
);
304 fputs_filtered ("\n", file
);
305 print_i387_status_word (fstat_p
, fstat
, file
);
306 print_i387_control_word (fctrl_p
, fctrl
, file
);
307 fprintf_filtered (file
, "Tag Word: %s\n",
308 ftag_p
? hex_string_custom (ftag
, 4) : _("<unavailable>"));
309 fprintf_filtered (file
, "Instruction Pointer: %s:",
310 fiseg_p
? hex_string_custom (fiseg
, 2) : _("<unavailable>"));
311 fprintf_filtered (file
, "%s\n",
312 fioff_p
? hex_string_custom (fioff
, 8) : _("<unavailable>"));
313 fprintf_filtered (file
, "Operand Pointer: %s:",
314 foseg_p
? hex_string_custom (foseg
, 2) : _("<unavailable>"));
315 fprintf_filtered (file
, "%s\n",
316 fooff_p
? hex_string_custom (fooff
, 8) : _("<unavailable>"));
317 fprintf_filtered (file
, "Opcode: %s\n",
319 ? (hex_string_custom (fop
? (fop
| 0xd800) : 0, 4))
320 : _("<unavailable>"));
324 /* Return nonzero if a value of type TYPE stored in register REGNUM
325 needs any special handling. */
328 i387_convert_register_p (struct gdbarch
*gdbarch
, int regnum
,
331 if (i386_fp_regnum_p (gdbarch
, regnum
))
333 /* Floating point registers must be converted unless we are
334 accessing them in their hardware type or TYPE is not float. */
335 if (type
== i387_ext_type (gdbarch
)
336 || TYPE_CODE (type
) != TYPE_CODE_FLT
)
345 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
346 return its contents in TO. */
349 i387_register_to_value (struct frame_info
*frame
, int regnum
,
350 struct type
*type
, gdb_byte
*to
,
351 int *optimizedp
, int *unavailablep
)
353 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
354 gdb_byte from
[I386_MAX_REGISTER_SIZE
];
356 gdb_assert (i386_fp_regnum_p (gdbarch
, regnum
));
358 /* We only support floating-point values. */
359 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
361 warning (_("Cannot convert floating-point register value "
362 "to non-floating-point type."));
363 *optimizedp
= *unavailablep
= 0;
367 /* Convert to TYPE. */
368 if (!get_frame_register_bytes (frame
, regnum
, 0,
369 register_size (gdbarch
, regnum
),
370 from
, optimizedp
, unavailablep
))
373 convert_typed_floating (from
, i387_ext_type (gdbarch
), to
, type
);
374 *optimizedp
= *unavailablep
= 0;
378 /* Write the contents FROM of a value of type TYPE into register
379 REGNUM in frame FRAME. */
382 i387_value_to_register (struct frame_info
*frame
, int regnum
,
383 struct type
*type
, const gdb_byte
*from
)
385 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
386 gdb_byte to
[I386_MAX_REGISTER_SIZE
];
388 gdb_assert (i386_fp_regnum_p (gdbarch
, regnum
));
390 /* We only support floating-point values. */
391 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
393 warning (_("Cannot convert non-floating-point type "
394 "to floating-point register value."));
398 /* Convert from TYPE. */
399 convert_typed_floating (from
, type
, to
, i387_ext_type (gdbarch
));
400 put_frame_register (frame
, regnum
, to
);
404 /* Handle FSAVE and FXSAVE formats. */
406 /* At fsave_offset[REGNUM] you'll find the offset to the location in
407 the data structure used by the "fsave" instruction where GDB
408 register REGNUM is stored. */
410 static int fsave_offset
[] =
412 28 + 0 * 10, /* %st(0) ... */
419 28 + 7 * 10, /* ... %st(7). */
420 0, /* `fctrl' (16 bits). */
421 4, /* `fstat' (16 bits). */
422 8, /* `ftag' (16 bits). */
423 16, /* `fiseg' (16 bits). */
425 24, /* `foseg' (16 bits). */
427 18 /* `fop' (bottom 11 bits). */
430 #define FSAVE_ADDR(tdep, fsave, regnum) \
431 (fsave + fsave_offset[regnum - I387_ST0_REGNUM (tdep)])
434 /* Fill register REGNUM in REGCACHE with the appropriate value from
435 *FSAVE. This function masks off any of the reserved bits in
439 i387_supply_fsave (struct regcache
*regcache
, int regnum
, const void *fsave
)
441 struct gdbarch
*gdbarch
= regcache
->arch ();
442 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
443 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
444 const gdb_byte
*regs
= (const gdb_byte
*) fsave
;
447 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
449 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
450 if (regnum
== -1 || regnum
== i
)
454 regcache_raw_supply (regcache
, i
, NULL
);
458 /* Most of the FPU control registers occupy only 16 bits in the
459 fsave area. Give those a special treatment. */
460 if (i
>= I387_FCTRL_REGNUM (tdep
)
461 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
465 memcpy (val
, FSAVE_ADDR (tdep
, regs
, i
), 2);
467 if (i
== I387_FOP_REGNUM (tdep
))
468 val
[1] &= ((1 << 3) - 1);
469 regcache_raw_supply (regcache
, i
, val
);
472 regcache_raw_supply (regcache
, i
, FSAVE_ADDR (tdep
, regs
, i
));
475 /* Provide dummy values for the SSE registers. */
476 for (i
= I387_XMM0_REGNUM (tdep
); i
< I387_MXCSR_REGNUM (tdep
); i
++)
477 if (regnum
== -1 || regnum
== i
)
478 regcache_raw_supply (regcache
, i
, NULL
);
479 if (regnum
== -1 || regnum
== I387_MXCSR_REGNUM (tdep
))
483 store_unsigned_integer (buf
, 4, byte_order
, 0x1f80);
484 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
), buf
);
488 /* Fill register REGNUM (if it is a floating-point register) in *FSAVE
489 with the value from REGCACHE. If REGNUM is -1, do this for all
490 registers. This function doesn't touch any of the reserved bits in
494 i387_collect_fsave (const struct regcache
*regcache
, int regnum
, void *fsave
)
496 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
497 gdb_byte
*regs
= (gdb_byte
*) fsave
;
500 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
502 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
503 if (regnum
== -1 || regnum
== i
)
505 /* Most of the FPU control registers occupy only 16 bits in
506 the fsave area. Give those a special treatment. */
507 if (i
>= I387_FCTRL_REGNUM (tdep
)
508 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
512 regcache_raw_collect (regcache
, i
, buf
);
514 if (i
== I387_FOP_REGNUM (tdep
))
516 /* The opcode occupies only 11 bits. Make sure we
517 don't touch the other bits. */
518 buf
[1] &= ((1 << 3) - 1);
519 buf
[1] |= ((FSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
521 memcpy (FSAVE_ADDR (tdep
, regs
, i
), buf
, 2);
524 regcache_raw_collect (regcache
, i
, FSAVE_ADDR (tdep
, regs
, i
));
529 /* At fxsave_offset[REGNUM] you'll find the offset to the location in
530 the data structure used by the "fxsave" instruction where GDB
531 register REGNUM is stored. */
533 static int fxsave_offset
[] =
535 32, /* %st(0) through ... */
542 144, /* ... %st(7) (80 bits each). */
543 0, /* `fctrl' (16 bits). */
544 2, /* `fstat' (16 bits). */
545 4, /* `ftag' (16 bits). */
546 12, /* `fiseg' (16 bits). */
548 20, /* `foseg' (16 bits). */
550 6, /* `fop' (bottom 11 bits). */
551 160 + 0 * 16, /* %xmm0 through ... */
566 160 + 15 * 16, /* ... %xmm15 (128 bits each). */
569 #define FXSAVE_ADDR(tdep, fxsave, regnum) \
570 (fxsave + fxsave_offset[regnum - I387_ST0_REGNUM (tdep)])
572 /* We made an unfortunate choice in putting %mxcsr after the SSE
573 registers %xmm0-%xmm7 instead of before, since it makes supporting
574 the registers %xmm8-%xmm15 on AMD64 a bit involved. Therefore we
575 don't include the offset for %mxcsr here above. */
577 #define FXSAVE_MXCSR_ADDR(fxsave) (fxsave + 24)
579 static int i387_tag (const gdb_byte
*raw
);
582 /* Fill register REGNUM in REGCACHE with the appropriate
583 floating-point or SSE register value from *FXSAVE. This function
584 masks off any of the reserved bits in *FXSAVE. */
587 i387_supply_fxsave (struct regcache
*regcache
, int regnum
, const void *fxsave
)
589 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
590 const gdb_byte
*regs
= (const gdb_byte
*) fxsave
;
593 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
594 gdb_assert (tdep
->num_xmm_regs
> 0);
596 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_MXCSR_REGNUM (tdep
); i
++)
597 if (regnum
== -1 || regnum
== i
)
601 regcache_raw_supply (regcache
, i
, NULL
);
605 /* Most of the FPU control registers occupy only 16 bits in
606 the fxsave area. Give those a special treatment. */
607 if (i
>= I387_FCTRL_REGNUM (tdep
) && i
< I387_XMM0_REGNUM (tdep
)
608 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
612 memcpy (val
, FXSAVE_ADDR (tdep
, regs
, i
), 2);
614 if (i
== I387_FOP_REGNUM (tdep
))
615 val
[1] &= ((1 << 3) - 1);
616 else if (i
== I387_FTAG_REGNUM (tdep
))
618 /* The fxsave area contains a simplified version of
619 the tag word. We have to look at the actual 80-bit
620 FP data to recreate the traditional i387 tag word. */
622 unsigned long ftag
= 0;
626 top
= ((FXSAVE_ADDR (tdep
, regs
,
627 I387_FSTAT_REGNUM (tdep
)))[1] >> 3);
630 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
634 if (val
[0] & (1 << fpreg
))
636 int thisreg
= (fpreg
+ 8 - top
) % 8
637 + I387_ST0_REGNUM (tdep
);
638 tag
= i387_tag (FXSAVE_ADDR (tdep
, regs
, thisreg
));
643 ftag
|= tag
<< (2 * fpreg
);
645 val
[0] = ftag
& 0xff;
646 val
[1] = (ftag
>> 8) & 0xff;
648 regcache_raw_supply (regcache
, i
, val
);
651 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
654 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
657 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
), NULL
);
659 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
),
660 FXSAVE_MXCSR_ADDR (regs
));
664 /* Fill register REGNUM (if it is a floating-point or SSE register) in
665 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
666 all registers. This function doesn't touch any of the reserved
670 i387_collect_fxsave (const struct regcache
*regcache
, int regnum
, void *fxsave
)
672 struct gdbarch_tdep
*tdep
= gdbarch_tdep (regcache
->arch ());
673 gdb_byte
*regs
= (gdb_byte
*) fxsave
;
676 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
677 gdb_assert (tdep
->num_xmm_regs
> 0);
679 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_MXCSR_REGNUM (tdep
); i
++)
680 if (regnum
== -1 || regnum
== i
)
682 /* Most of the FPU control registers occupy only 16 bits in
683 the fxsave area. Give those a special treatment. */
684 if (i
>= I387_FCTRL_REGNUM (tdep
) && i
< I387_XMM0_REGNUM (tdep
)
685 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
689 regcache_raw_collect (regcache
, i
, buf
);
691 if (i
== I387_FOP_REGNUM (tdep
))
693 /* The opcode occupies only 11 bits. Make sure we
694 don't touch the other bits. */
695 buf
[1] &= ((1 << 3) - 1);
696 buf
[1] |= ((FXSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
698 else if (i
== I387_FTAG_REGNUM (tdep
))
700 /* Converting back is much easier. */
705 ftag
= (buf
[1] << 8) | buf
[0];
709 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
711 int tag
= (ftag
>> (fpreg
* 2)) & 3;
714 buf
[0] |= (1 << fpreg
);
717 memcpy (FXSAVE_ADDR (tdep
, regs
, i
), buf
, 2);
720 regcache_raw_collect (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
723 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
724 regcache_raw_collect (regcache
, I387_MXCSR_REGNUM (tdep
),
725 FXSAVE_MXCSR_ADDR (regs
));
728 /* `xstate_bv' is at byte offset 512. */
729 #define XSAVE_XSTATE_BV_ADDR(xsave) (xsave + 512)
731 /* At xsave_avxh_offset[REGNUM] you'll find the offset to the location in
732 the upper 128bit of AVX register data structure used by the "xsave"
733 instruction where GDB register REGNUM is stored. */
735 static int xsave_avxh_offset
[] =
737 576 + 0 * 16, /* Upper 128bit of %ymm0 through ... */
752 576 + 15 * 16 /* Upper 128bit of ... %ymm15 (128 bits each). */
755 #define XSAVE_AVXH_ADDR(tdep, xsave, regnum) \
756 (xsave + xsave_avxh_offset[regnum - I387_YMM0H_REGNUM (tdep)])
758 /* At xsave_ymm_avx512_offset[REGNUM] you'll find the offset to the location in
759 the upper 128bit of ZMM register data structure used by the "xsave"
760 instruction where GDB register REGNUM is stored. */
762 static int xsave_ymm_avx512_offset
[] =
764 /* HI16_ZMM_area + 16 bytes + regnum* 64 bytes. */
765 1664 + 16 + 0 * 64, /* %ymm16 through... */
780 1664 + 16 + 15 * 64 /* ... %ymm31 (128 bits each). */
783 #define XSAVE_YMM_AVX512_ADDR(tdep, xsave, regnum) \
784 (xsave + xsave_ymm_avx512_offset[regnum - I387_YMM16H_REGNUM (tdep)])
786 static int xsave_xmm_avx512_offset
[] =
788 1664 + 0 * 64, /* %ymm16 through... */
803 1664 + 15 * 64 /* ... %ymm31 (128 bits each). */
806 #define XSAVE_XMM_AVX512_ADDR(tdep, xsave, regnum) \
807 (xsave + xsave_xmm_avx512_offset[regnum - I387_XMM16_REGNUM (tdep)])
809 static int xsave_mpx_offset
[] = {
810 960 + 0 * 16, /* bnd0r...bnd3r registers. */
814 1024 + 0 * 8, /* bndcfg ... bndstatus. */
818 #define XSAVE_MPX_ADDR(tdep, xsave, regnum) \
819 (xsave + xsave_mpx_offset[regnum - I387_BND0R_REGNUM (tdep)])
821 /* At xsave_avx512__h_offset[REGNUM] you find the offset to the location
822 of the AVX512 opmask register data structure used by the "xsave"
823 instruction where GDB register REGNUM is stored. */
825 static int xsave_avx512_k_offset
[] =
827 1088 + 0 * 8, /* %k0 through... */
834 1088 + 7 * 8 /* %k7 (64 bits each). */
837 #define XSAVE_AVX512_K_ADDR(tdep, xsave, regnum) \
838 (xsave + xsave_avx512_k_offset[regnum - I387_K0_REGNUM (tdep)])
840 /* At xsave_avx512_zmm_h_offset[REGNUM] you find the offset to the location in
841 the upper 256bit of AVX512 ZMMH register data structure used by the "xsave"
842 instruction where GDB register REGNUM is stored. */
844 static int xsave_avx512_zmm_h_offset
[] =
847 1152 + 1 * 32, /* Upper 256bit of %zmmh0 through... */
861 1152 + 15 * 32, /* Upper 256bit of... %zmmh15 (256 bits each). */
862 1664 + 32 + 0 * 64, /* Upper 256bit of... %zmmh16 (256 bits each). */
877 1664 + 32 + 15 * 64 /* Upper 256bit of... %zmmh31 (256 bits each). */
880 #define XSAVE_AVX512_ZMM_H_ADDR(tdep, xsave, regnum) \
881 (xsave + xsave_avx512_zmm_h_offset[regnum - I387_ZMM0H_REGNUM (tdep)])
883 /* At xsave_pkeys_offset[REGNUM] you find the offset to the location
884 of the PKRU register data structure used by the "xsave"
885 instruction where GDB register REGNUM is stored. */
887 static int xsave_pkeys_offset
[] =
889 2688 + 0 * 8 /* %pkru (64 bits in XSTATE, 32-bit actually used by
890 instructions and applications). */
893 #define XSAVE_PKEYS_ADDR(tdep, xsave, regnum) \
894 (xsave + xsave_pkeys_offset[regnum - I387_PKRU_REGNUM (tdep)])
896 /* Similar to i387_supply_fxsave, but use XSAVE extended state. */
899 i387_supply_xsave (struct regcache
*regcache
, int regnum
,
902 struct gdbarch
*gdbarch
= regcache
->arch ();
903 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
904 const gdb_byte
*regs
= (const gdb_byte
*) xsave
;
907 static const gdb_byte zero
[I386_MAX_REGISTER_SIZE
] = { 0 };
917 avx512_ymmh_avx512
= 0x40,
918 avx512_xmm_avx512
= 0x80,
920 all
= x87
| sse
| avxh
| mpx
| avx512_k
| avx512_zmm_h
921 | avx512_ymmh_avx512
| avx512_xmm_avx512
| pkeys
924 gdb_assert (regs
!= NULL
);
925 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
926 gdb_assert (tdep
->num_xmm_regs
> 0);
930 else if (regnum
>= I387_PKRU_REGNUM (tdep
)
931 && regnum
< I387_PKEYSEND_REGNUM (tdep
))
933 else if (regnum
>= I387_ZMM0H_REGNUM (tdep
)
934 && regnum
< I387_ZMMENDH_REGNUM (tdep
))
935 regclass
= avx512_zmm_h
;
936 else if (regnum
>= I387_K0_REGNUM (tdep
)
937 && regnum
< I387_KEND_REGNUM (tdep
))
939 else if (regnum
>= I387_YMM16H_REGNUM (tdep
)
940 && regnum
< I387_YMMH_AVX512_END_REGNUM (tdep
))
941 regclass
= avx512_ymmh_avx512
;
942 else if (regnum
>= I387_XMM16_REGNUM (tdep
)
943 && regnum
< I387_XMM_AVX512_END_REGNUM (tdep
))
944 regclass
= avx512_xmm_avx512
;
945 else if (regnum
>= I387_YMM0H_REGNUM (tdep
)
946 && regnum
< I387_YMMENDH_REGNUM (tdep
))
948 else if (regnum
>= I387_BND0R_REGNUM (tdep
)
949 && regnum
< I387_MPXEND_REGNUM (tdep
))
951 else if (regnum
>= I387_XMM0_REGNUM (tdep
)
952 && regnum
< I387_MXCSR_REGNUM (tdep
))
954 else if (regnum
>= I387_ST0_REGNUM (tdep
)
955 && regnum
< I387_FCTRL_REGNUM (tdep
))
960 if (regclass
!= none
)
962 /* Get `xstat_bv'. The supported bits in `xstat_bv' are 8 bytes. */
963 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
964 ULONGEST xstate_bv
= 0;
966 xstate_bv
= extract_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs
),
969 /* Clear part in vector registers if its bit in xstat_bv is zero. */
970 clear_bv
= (~(xstate_bv
)) & tdep
->xcr0
;
973 clear_bv
= X86_XSTATE_ALL_MASK
;
975 /* With the delayed xsave mechanism, in between the program
976 starting, and the program accessing the vector registers for the
977 first time, the register's values are invalid. The kernel
978 initializes register states to zero when they are set the first
979 time in a program. This means that from the user-space programs'
980 perspective, it's the same as if the registers have always been
981 zero from the start of the program. Therefore, the debugger
982 should provide the same illusion to the user. */
990 if ((clear_bv
& X86_XSTATE_PKRU
))
991 regcache_raw_supply (regcache
, regnum
, zero
);
993 regcache_raw_supply (regcache
, regnum
,
994 XSAVE_PKEYS_ADDR (tdep
, regs
, regnum
));
998 if ((clear_bv
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
999 regcache_raw_supply (regcache
, regnum
, zero
);
1001 regcache_raw_supply (regcache
, regnum
,
1002 XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, regnum
));
1006 if ((clear_bv
& X86_XSTATE_K
))
1007 regcache_raw_supply (regcache
, regnum
, zero
);
1009 regcache_raw_supply (regcache
, regnum
,
1010 XSAVE_AVX512_K_ADDR (tdep
, regs
, regnum
));
1013 case avx512_ymmh_avx512
:
1014 if ((clear_bv
& X86_XSTATE_ZMM
))
1015 regcache_raw_supply (regcache
, regnum
, zero
);
1017 regcache_raw_supply (regcache
, regnum
,
1018 XSAVE_YMM_AVX512_ADDR (tdep
, regs
, regnum
));
1021 case avx512_xmm_avx512
:
1022 if ((clear_bv
& X86_XSTATE_ZMM
))
1023 regcache_raw_supply (regcache
, regnum
, zero
);
1025 regcache_raw_supply (regcache
, regnum
,
1026 XSAVE_XMM_AVX512_ADDR (tdep
, regs
, regnum
));
1030 if ((clear_bv
& X86_XSTATE_AVX
))
1031 regcache_raw_supply (regcache
, regnum
, zero
);
1033 regcache_raw_supply (regcache
, regnum
,
1034 XSAVE_AVXH_ADDR (tdep
, regs
, regnum
));
1038 if ((clear_bv
& X86_XSTATE_BNDREGS
))
1039 regcache_raw_supply (regcache
, regnum
, zero
);
1041 regcache_raw_supply (regcache
, regnum
,
1042 XSAVE_MPX_ADDR (tdep
, regs
, regnum
));
1046 if ((clear_bv
& X86_XSTATE_SSE
))
1047 regcache_raw_supply (regcache
, regnum
, zero
);
1049 regcache_raw_supply (regcache
, regnum
,
1050 FXSAVE_ADDR (tdep
, regs
, regnum
));
1054 if ((clear_bv
& X86_XSTATE_X87
))
1055 regcache_raw_supply (regcache
, regnum
, zero
);
1057 regcache_raw_supply (regcache
, regnum
,
1058 FXSAVE_ADDR (tdep
, regs
, regnum
));
1062 /* Handle PKEYS registers. */
1063 if ((tdep
->xcr0
& X86_XSTATE_PKRU
))
1065 if ((clear_bv
& X86_XSTATE_PKRU
))
1067 for (i
= I387_PKRU_REGNUM (tdep
);
1068 i
< I387_PKEYSEND_REGNUM (tdep
);
1070 regcache_raw_supply (regcache
, i
, zero
);
1074 for (i
= I387_PKRU_REGNUM (tdep
);
1075 i
< I387_PKEYSEND_REGNUM (tdep
);
1077 regcache_raw_supply (regcache
, i
,
1078 XSAVE_PKEYS_ADDR (tdep
, regs
, i
));
1082 /* Handle the upper ZMM registers. */
1083 if ((tdep
->xcr0
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
1085 if ((clear_bv
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
1087 for (i
= I387_ZMM0H_REGNUM (tdep
);
1088 i
< I387_ZMMENDH_REGNUM (tdep
);
1090 regcache_raw_supply (regcache
, i
, zero
);
1094 for (i
= I387_ZMM0H_REGNUM (tdep
);
1095 i
< I387_ZMMENDH_REGNUM (tdep
);
1097 regcache_raw_supply (regcache
, i
,
1098 XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, i
));
1102 /* Handle AVX512 OpMask registers. */
1103 if ((tdep
->xcr0
& X86_XSTATE_K
))
1105 if ((clear_bv
& X86_XSTATE_K
))
1107 for (i
= I387_K0_REGNUM (tdep
);
1108 i
< I387_KEND_REGNUM (tdep
);
1110 regcache_raw_supply (regcache
, i
, zero
);
1114 for (i
= I387_K0_REGNUM (tdep
);
1115 i
< I387_KEND_REGNUM (tdep
);
1117 regcache_raw_supply (regcache
, i
,
1118 XSAVE_AVX512_K_ADDR (tdep
, regs
, i
));
1122 /* Handle the YMM_AVX512 registers. */
1123 if ((tdep
->xcr0
& X86_XSTATE_ZMM
))
1125 if ((clear_bv
& X86_XSTATE_ZMM
))
1127 for (i
= I387_YMM16H_REGNUM (tdep
);
1128 i
< I387_YMMH_AVX512_END_REGNUM (tdep
);
1130 regcache_raw_supply (regcache
, i
, zero
);
1131 for (i
= I387_XMM16_REGNUM (tdep
);
1132 i
< I387_XMM_AVX512_END_REGNUM (tdep
);
1134 regcache_raw_supply (regcache
, i
, zero
);
1138 for (i
= I387_YMM16H_REGNUM (tdep
);
1139 i
< I387_YMMH_AVX512_END_REGNUM (tdep
);
1141 regcache_raw_supply (regcache
, i
,
1142 XSAVE_YMM_AVX512_ADDR (tdep
, regs
, i
));
1143 for (i
= I387_XMM16_REGNUM (tdep
);
1144 i
< I387_XMM_AVX512_END_REGNUM (tdep
);
1146 regcache_raw_supply (regcache
, i
,
1147 XSAVE_XMM_AVX512_ADDR (tdep
, regs
, i
));
1150 /* Handle the upper YMM registers. */
1151 if ((tdep
->xcr0
& X86_XSTATE_AVX
))
1153 if ((clear_bv
& X86_XSTATE_AVX
))
1155 for (i
= I387_YMM0H_REGNUM (tdep
);
1156 i
< I387_YMMENDH_REGNUM (tdep
);
1158 regcache_raw_supply (regcache
, i
, zero
);
1162 for (i
= I387_YMM0H_REGNUM (tdep
);
1163 i
< I387_YMMENDH_REGNUM (tdep
);
1165 regcache_raw_supply (regcache
, i
,
1166 XSAVE_AVXH_ADDR (tdep
, regs
, i
));
1170 /* Handle the MPX registers. */
1171 if ((tdep
->xcr0
& X86_XSTATE_BNDREGS
))
1173 if (clear_bv
& X86_XSTATE_BNDREGS
)
1175 for (i
= I387_BND0R_REGNUM (tdep
);
1176 i
< I387_BNDCFGU_REGNUM (tdep
); i
++)
1177 regcache_raw_supply (regcache
, i
, zero
);
1181 for (i
= I387_BND0R_REGNUM (tdep
);
1182 i
< I387_BNDCFGU_REGNUM (tdep
); i
++)
1183 regcache_raw_supply (regcache
, i
,
1184 XSAVE_MPX_ADDR (tdep
, regs
, i
));
1188 /* Handle the MPX registers. */
1189 if ((tdep
->xcr0
& X86_XSTATE_BNDCFG
))
1191 if (clear_bv
& X86_XSTATE_BNDCFG
)
1193 for (i
= I387_BNDCFGU_REGNUM (tdep
);
1194 i
< I387_MPXEND_REGNUM (tdep
); i
++)
1195 regcache_raw_supply (regcache
, i
, zero
);
1199 for (i
= I387_BNDCFGU_REGNUM (tdep
);
1200 i
< I387_MPXEND_REGNUM (tdep
); i
++)
1201 regcache_raw_supply (regcache
, i
,
1202 XSAVE_MPX_ADDR (tdep
, regs
, i
));
1206 /* Handle the XMM registers. */
1207 if ((tdep
->xcr0
& X86_XSTATE_SSE
))
1209 if ((clear_bv
& X86_XSTATE_SSE
))
1211 for (i
= I387_XMM0_REGNUM (tdep
);
1212 i
< I387_MXCSR_REGNUM (tdep
);
1214 regcache_raw_supply (regcache
, i
, zero
);
1218 for (i
= I387_XMM0_REGNUM (tdep
);
1219 i
< I387_MXCSR_REGNUM (tdep
); i
++)
1220 regcache_raw_supply (regcache
, i
,
1221 FXSAVE_ADDR (tdep
, regs
, i
));
1225 /* Handle the x87 registers. */
1226 if ((tdep
->xcr0
& X86_XSTATE_X87
))
1228 if ((clear_bv
& X86_XSTATE_X87
))
1230 for (i
= I387_ST0_REGNUM (tdep
);
1231 i
< I387_FCTRL_REGNUM (tdep
);
1233 regcache_raw_supply (regcache
, i
, zero
);
1237 for (i
= I387_ST0_REGNUM (tdep
);
1238 i
< I387_FCTRL_REGNUM (tdep
);
1240 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
1246 /* Only handle x87 control registers. */
1247 for (i
= I387_FCTRL_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
1248 if (regnum
== -1 || regnum
== i
)
1250 /* Most of the FPU control registers occupy only 16 bits in
1251 the xsave extended state. Give those a special treatment. */
1252 if (i
!= I387_FIOFF_REGNUM (tdep
)
1253 && i
!= I387_FOOFF_REGNUM (tdep
))
1257 memcpy (val
, FXSAVE_ADDR (tdep
, regs
, i
), 2);
1258 val
[2] = val
[3] = 0;
1259 if (i
== I387_FOP_REGNUM (tdep
))
1260 val
[1] &= ((1 << 3) - 1);
1261 else if (i
== I387_FTAG_REGNUM (tdep
))
1263 /* The fxsave area contains a simplified version of
1264 the tag word. We have to look at the actual 80-bit
1265 FP data to recreate the traditional i387 tag word. */
1267 unsigned long ftag
= 0;
1271 top
= ((FXSAVE_ADDR (tdep
, regs
,
1272 I387_FSTAT_REGNUM (tdep
)))[1] >> 3);
1275 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
1279 if (val
[0] & (1 << fpreg
))
1281 int thisreg
= (fpreg
+ 8 - top
) % 8
1282 + I387_ST0_REGNUM (tdep
);
1283 tag
= i387_tag (FXSAVE_ADDR (tdep
, regs
, thisreg
));
1286 tag
= 3; /* Empty */
1288 ftag
|= tag
<< (2 * fpreg
);
1290 val
[0] = ftag
& 0xff;
1291 val
[1] = (ftag
>> 8) & 0xff;
1293 regcache_raw_supply (regcache
, i
, val
);
1296 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
1299 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
1300 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
),
1301 FXSAVE_MXCSR_ADDR (regs
));
1304 /* Similar to i387_collect_fxsave, but use XSAVE extended state. */
1307 i387_collect_xsave (const struct regcache
*regcache
, int regnum
,
1308 void *xsave
, int gcore
)
1310 struct gdbarch
*gdbarch
= regcache
->arch ();
1311 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1312 gdb_byte
*regs
= (gdb_byte
*) xsave
;
1322 avx512_k
= 0x20 | check
,
1323 avx512_zmm_h
= 0x40 | check
,
1324 avx512_ymmh_avx512
= 0x80 | check
,
1325 avx512_xmm_avx512
= 0x100 | check
,
1326 pkeys
= 0x200 | check
,
1327 all
= x87
| sse
| avxh
| mpx
| avx512_k
| avx512_zmm_h
1328 | avx512_ymmh_avx512
| avx512_xmm_avx512
| pkeys
1331 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
1332 gdb_assert (tdep
->num_xmm_regs
> 0);
1336 else if (regnum
>= I387_PKRU_REGNUM (tdep
)
1337 && regnum
< I387_PKEYSEND_REGNUM (tdep
))
1339 else if (regnum
>= I387_ZMM0H_REGNUM (tdep
)
1340 && regnum
< I387_ZMMENDH_REGNUM (tdep
))
1341 regclass
= avx512_zmm_h
;
1342 else if (regnum
>= I387_K0_REGNUM (tdep
)
1343 && regnum
< I387_KEND_REGNUM (tdep
))
1344 regclass
= avx512_k
;
1345 else if (regnum
>= I387_YMM16H_REGNUM (tdep
)
1346 && regnum
< I387_YMMH_AVX512_END_REGNUM (tdep
))
1347 regclass
= avx512_ymmh_avx512
;
1348 else if (regnum
>= I387_XMM16_REGNUM (tdep
)
1349 && regnum
< I387_XMM_AVX512_END_REGNUM (tdep
))
1350 regclass
= avx512_xmm_avx512
;
1351 else if (regnum
>= I387_YMM0H_REGNUM (tdep
)
1352 && regnum
< I387_YMMENDH_REGNUM (tdep
))
1354 else if (regnum
>= I387_BND0R_REGNUM (tdep
)
1355 && regnum
< I387_MPXEND_REGNUM (tdep
))
1357 else if (regnum
>= I387_XMM0_REGNUM (tdep
)
1358 && regnum
< I387_MXCSR_REGNUM (tdep
))
1360 else if (regnum
>= I387_ST0_REGNUM (tdep
)
1361 && regnum
< I387_FCTRL_REGNUM (tdep
))
1368 /* Clear XSAVE extended state. */
1369 memset (regs
, 0, X86_XSTATE_SIZE (tdep
->xcr0
));
1371 /* Update XCR0 and `xstate_bv' with XCR0 for gcore. */
1372 if (tdep
->xsave_xcr0_offset
!= -1)
1373 memcpy (regs
+ tdep
->xsave_xcr0_offset
, &tdep
->xcr0
, 8);
1374 memcpy (XSAVE_XSTATE_BV_ADDR (regs
), &tdep
->xcr0
, 8);
1377 if ((regclass
& check
))
1379 gdb_byte raw
[I386_MAX_REGISTER_SIZE
];
1380 ULONGEST initial_xstate_bv
, clear_bv
, xstate_bv
= 0;
1382 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1384 /* The supported bits in `xstat_bv' are 8 bytes. */
1385 initial_xstate_bv
= extract_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs
),
1387 clear_bv
= (~(initial_xstate_bv
)) & tdep
->xcr0
;
1389 /* Clear register set if its bit in xstat_bv is zero. */
1392 if ((clear_bv
& X86_XSTATE_PKRU
))
1393 for (i
= I387_PKRU_REGNUM (tdep
);
1394 i
< I387_PKEYSEND_REGNUM (tdep
); i
++)
1395 memset (XSAVE_PKEYS_ADDR (tdep
, regs
, i
), 0, 4);
1397 if ((clear_bv
& X86_XSTATE_BNDREGS
))
1398 for (i
= I387_BND0R_REGNUM (tdep
);
1399 i
< I387_BNDCFGU_REGNUM (tdep
); i
++)
1400 memset (XSAVE_MPX_ADDR (tdep
, regs
, i
), 0, 16);
1402 if ((clear_bv
& X86_XSTATE_BNDCFG
))
1403 for (i
= I387_BNDCFGU_REGNUM (tdep
);
1404 i
< I387_MPXEND_REGNUM (tdep
); i
++)
1405 memset (XSAVE_MPX_ADDR (tdep
, regs
, i
), 0, 8);
1407 if ((clear_bv
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
1408 for (i
= I387_ZMM0H_REGNUM (tdep
);
1409 i
< I387_ZMMENDH_REGNUM (tdep
); i
++)
1410 memset (XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, i
), 0, 32);
1412 if ((clear_bv
& X86_XSTATE_K
))
1413 for (i
= I387_K0_REGNUM (tdep
);
1414 i
< I387_KEND_REGNUM (tdep
); i
++)
1415 memset (XSAVE_AVX512_K_ADDR (tdep
, regs
, i
), 0, 8);
1417 if ((clear_bv
& X86_XSTATE_ZMM
))
1419 for (i
= I387_YMM16H_REGNUM (tdep
);
1420 i
< I387_YMMH_AVX512_END_REGNUM (tdep
); i
++)
1421 memset (XSAVE_YMM_AVX512_ADDR (tdep
, regs
, i
), 0, 16);
1422 for (i
= I387_XMM16_REGNUM (tdep
);
1423 i
< I387_XMM_AVX512_END_REGNUM (tdep
); i
++)
1424 memset (XSAVE_XMM_AVX512_ADDR (tdep
, regs
, i
), 0, 16);
1427 if ((clear_bv
& X86_XSTATE_AVX
))
1428 for (i
= I387_YMM0H_REGNUM (tdep
);
1429 i
< I387_YMMENDH_REGNUM (tdep
); i
++)
1430 memset (XSAVE_AVXH_ADDR (tdep
, regs
, i
), 0, 16);
1432 if ((clear_bv
& X86_XSTATE_SSE
))
1433 for (i
= I387_XMM0_REGNUM (tdep
);
1434 i
< I387_MXCSR_REGNUM (tdep
); i
++)
1435 memset (FXSAVE_ADDR (tdep
, regs
, i
), 0, 16);
1437 if ((clear_bv
& X86_XSTATE_X87
))
1438 for (i
= I387_ST0_REGNUM (tdep
);
1439 i
< I387_FCTRL_REGNUM (tdep
); i
++)
1440 memset (FXSAVE_ADDR (tdep
, regs
, i
), 0, 10);
1443 if (regclass
== all
)
1445 /* Check if any PKEYS registers are changed. */
1446 if ((tdep
->xcr0
& X86_XSTATE_PKRU
))
1447 for (i
= I387_PKRU_REGNUM (tdep
);
1448 i
< I387_PKEYSEND_REGNUM (tdep
); i
++)
1450 regcache_raw_collect (regcache
, i
, raw
);
1451 p
= XSAVE_PKEYS_ADDR (tdep
, regs
, i
);
1452 if (memcmp (raw
, p
, 4) != 0)
1454 xstate_bv
|= X86_XSTATE_PKRU
;
1459 /* Check if any ZMMH registers are changed. */
1460 if ((tdep
->xcr0
& (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
)))
1461 for (i
= I387_ZMM0H_REGNUM (tdep
);
1462 i
< I387_ZMMENDH_REGNUM (tdep
); i
++)
1464 regcache_raw_collect (regcache
, i
, raw
);
1465 p
= XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, i
);
1466 if (memcmp (raw
, p
, 32) != 0)
1468 xstate_bv
|= (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
);
1469 memcpy (p
, raw
, 32);
1473 /* Check if any K registers are changed. */
1474 if ((tdep
->xcr0
& X86_XSTATE_K
))
1475 for (i
= I387_K0_REGNUM (tdep
);
1476 i
< I387_KEND_REGNUM (tdep
); i
++)
1478 regcache_raw_collect (regcache
, i
, raw
);
1479 p
= XSAVE_AVX512_K_ADDR (tdep
, regs
, i
);
1480 if (memcmp (raw
, p
, 8) != 0)
1482 xstate_bv
|= X86_XSTATE_K
;
1487 /* Check if any XMM or upper YMM registers are changed. */
1488 if ((tdep
->xcr0
& X86_XSTATE_ZMM
))
1490 for (i
= I387_YMM16H_REGNUM (tdep
);
1491 i
< I387_YMMH_AVX512_END_REGNUM (tdep
); i
++)
1493 regcache_raw_collect (regcache
, i
, raw
);
1494 p
= XSAVE_YMM_AVX512_ADDR (tdep
, regs
, i
);
1495 if (memcmp (raw
, p
, 16) != 0)
1497 xstate_bv
|= X86_XSTATE_ZMM
;
1498 memcpy (p
, raw
, 16);
1501 for (i
= I387_XMM16_REGNUM (tdep
);
1502 i
< I387_XMM_AVX512_END_REGNUM (tdep
); i
++)
1504 regcache_raw_collect (regcache
, i
, raw
);
1505 p
= XSAVE_XMM_AVX512_ADDR (tdep
, regs
, i
);
1506 if (memcmp (raw
, p
, 16) != 0)
1508 xstate_bv
|= X86_XSTATE_ZMM
;
1509 memcpy (p
, raw
, 16);
1514 /* Check if any upper YMM registers are changed. */
1515 if ((tdep
->xcr0
& X86_XSTATE_AVX
))
1516 for (i
= I387_YMM0H_REGNUM (tdep
);
1517 i
< I387_YMMENDH_REGNUM (tdep
); i
++)
1519 regcache_raw_collect (regcache
, i
, raw
);
1520 p
= XSAVE_AVXH_ADDR (tdep
, regs
, i
);
1521 if (memcmp (raw
, p
, 16))
1523 xstate_bv
|= X86_XSTATE_AVX
;
1524 memcpy (p
, raw
, 16);
1527 /* Check if any upper MPX registers are changed. */
1528 if ((tdep
->xcr0
& X86_XSTATE_BNDREGS
))
1529 for (i
= I387_BND0R_REGNUM (tdep
);
1530 i
< I387_BNDCFGU_REGNUM (tdep
); i
++)
1532 regcache_raw_collect (regcache
, i
, raw
);
1533 p
= XSAVE_MPX_ADDR (tdep
, regs
, i
);
1534 if (memcmp (raw
, p
, 16))
1536 xstate_bv
|= X86_XSTATE_BNDREGS
;
1537 memcpy (p
, raw
, 16);
1541 /* Check if any upper MPX registers are changed. */
1542 if ((tdep
->xcr0
& X86_XSTATE_BNDCFG
))
1543 for (i
= I387_BNDCFGU_REGNUM (tdep
);
1544 i
< I387_MPXEND_REGNUM (tdep
); i
++)
1546 regcache_raw_collect (regcache
, i
, raw
);
1547 p
= XSAVE_MPX_ADDR (tdep
, regs
, i
);
1548 if (memcmp (raw
, p
, 8))
1550 xstate_bv
|= X86_XSTATE_BNDCFG
;
1555 /* Check if any SSE registers are changed. */
1556 if ((tdep
->xcr0
& X86_XSTATE_SSE
))
1557 for (i
= I387_XMM0_REGNUM (tdep
);
1558 i
< I387_MXCSR_REGNUM (tdep
); i
++)
1560 regcache_raw_collect (regcache
, i
, raw
);
1561 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1562 if (memcmp (raw
, p
, 16))
1564 xstate_bv
|= X86_XSTATE_SSE
;
1565 memcpy (p
, raw
, 16);
1569 /* Check if any X87 registers are changed. */
1570 if ((tdep
->xcr0
& X86_XSTATE_X87
))
1571 for (i
= I387_ST0_REGNUM (tdep
);
1572 i
< I387_FCTRL_REGNUM (tdep
); i
++)
1574 regcache_raw_collect (regcache
, i
, raw
);
1575 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1576 if (memcmp (raw
, p
, 10))
1578 xstate_bv
|= X86_XSTATE_X87
;
1579 memcpy (p
, raw
, 10);
1585 /* Check if REGNUM is changed. */
1586 regcache_raw_collect (regcache
, regnum
, raw
);
1591 internal_error (__FILE__
, __LINE__
,
1592 _("invalid i387 regclass"));
1595 /* This is a PKEYS register. */
1596 p
= XSAVE_PKEYS_ADDR (tdep
, regs
, regnum
);
1597 if (memcmp (raw
, p
, 4) != 0)
1599 xstate_bv
|= X86_XSTATE_PKRU
;
1605 /* This is a ZMM register. */
1606 p
= XSAVE_AVX512_ZMM_H_ADDR (tdep
, regs
, regnum
);
1607 if (memcmp (raw
, p
, 32) != 0)
1609 xstate_bv
|= (X86_XSTATE_ZMM_H
| X86_XSTATE_ZMM
);
1610 memcpy (p
, raw
, 32);
1614 /* This is a AVX512 mask register. */
1615 p
= XSAVE_AVX512_K_ADDR (tdep
, regs
, regnum
);
1616 if (memcmp (raw
, p
, 8) != 0)
1618 xstate_bv
|= X86_XSTATE_K
;
1623 case avx512_ymmh_avx512
:
1624 /* This is an upper YMM16-31 register. */
1625 p
= XSAVE_YMM_AVX512_ADDR (tdep
, regs
, regnum
);
1626 if (memcmp (raw
, p
, 16) != 0)
1628 xstate_bv
|= X86_XSTATE_ZMM
;
1629 memcpy (p
, raw
, 16);
1633 case avx512_xmm_avx512
:
1634 /* This is an upper XMM16-31 register. */
1635 p
= XSAVE_XMM_AVX512_ADDR (tdep
, regs
, regnum
);
1636 if (memcmp (raw
, p
, 16) != 0)
1638 xstate_bv
|= X86_XSTATE_ZMM
;
1639 memcpy (p
, raw
, 16);
1644 /* This is an upper YMM register. */
1645 p
= XSAVE_AVXH_ADDR (tdep
, regs
, regnum
);
1646 if (memcmp (raw
, p
, 16))
1648 xstate_bv
|= X86_XSTATE_AVX
;
1649 memcpy (p
, raw
, 16);
1654 if (regnum
< I387_BNDCFGU_REGNUM (tdep
))
1656 regcache_raw_collect (regcache
, regnum
, raw
);
1657 p
= XSAVE_MPX_ADDR (tdep
, regs
, regnum
);
1658 if (memcmp (raw
, p
, 16))
1660 xstate_bv
|= X86_XSTATE_BNDREGS
;
1661 memcpy (p
, raw
, 16);
1666 p
= XSAVE_MPX_ADDR (tdep
, regs
, regnum
);
1667 xstate_bv
|= X86_XSTATE_BNDCFG
;
1673 /* This is an SSE register. */
1674 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
1675 if (memcmp (raw
, p
, 16))
1677 xstate_bv
|= X86_XSTATE_SSE
;
1678 memcpy (p
, raw
, 16);
1683 /* This is an x87 register. */
1684 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
1685 if (memcmp (raw
, p
, 10))
1687 xstate_bv
|= X86_XSTATE_X87
;
1688 memcpy (p
, raw
, 10);
1694 /* Update the corresponding bits in `xstate_bv' if any SSE/AVX
1695 registers are changed. */
1698 /* The supported bits in `xstat_bv' are 8 bytes. */
1699 initial_xstate_bv
|= xstate_bv
;
1700 store_unsigned_integer (XSAVE_XSTATE_BV_ADDR (regs
),
1707 internal_error (__FILE__
, __LINE__
,
1708 _("invalid i387 regclass"));
1719 case avx512_ymmh_avx512
:
1720 case avx512_xmm_avx512
:
1722 /* Register REGNUM has been updated. Return. */
1728 /* Return if REGNUM isn't changed. */
1729 if (regclass
!= all
)
1734 /* Only handle x87 control registers. */
1735 for (i
= I387_FCTRL_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
1736 if (regnum
== -1 || regnum
== i
)
1738 /* Most of the FPU control registers occupy only 16 bits in
1739 the xsave extended state. Give those a special treatment. */
1740 if (i
!= I387_FIOFF_REGNUM (tdep
)
1741 && i
!= I387_FOOFF_REGNUM (tdep
))
1745 regcache_raw_collect (regcache
, i
, buf
);
1747 if (i
== I387_FOP_REGNUM (tdep
))
1749 /* The opcode occupies only 11 bits. Make sure we
1750 don't touch the other bits. */
1751 buf
[1] &= ((1 << 3) - 1);
1752 buf
[1] |= ((FXSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
1754 else if (i
== I387_FTAG_REGNUM (tdep
))
1756 /* Converting back is much easier. */
1758 unsigned short ftag
;
1761 ftag
= (buf
[1] << 8) | buf
[0];
1765 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
1767 int tag
= (ftag
>> (fpreg
* 2)) & 3;
1770 buf
[0] |= (1 << fpreg
);
1773 memcpy (FXSAVE_ADDR (tdep
, regs
, i
), buf
, 2);
1776 regcache_raw_collect (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
1779 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
1780 regcache_raw_collect (regcache
, I387_MXCSR_REGNUM (tdep
),
1781 FXSAVE_MXCSR_ADDR (regs
));
1784 /* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
1788 i387_tag (const gdb_byte
*raw
)
1791 unsigned int exponent
;
1792 unsigned long fraction
[2];
1794 integer
= raw
[7] & 0x80;
1795 exponent
= (((raw
[9] & 0x7f) << 8) | raw
[8]);
1796 fraction
[0] = ((raw
[3] << 24) | (raw
[2] << 16) | (raw
[1] << 8) | raw
[0]);
1797 fraction
[1] = (((raw
[7] & 0x7f) << 24) | (raw
[6] << 16)
1798 | (raw
[5] << 8) | raw
[4]);
1800 if (exponent
== 0x7fff)
1805 else if (exponent
== 0x0000)
1807 if (fraction
[0] == 0x0000 && fraction
[1] == 0x0000 && !integer
)
1833 /* Prepare the FPU stack in REGCACHE for a function return. */
1836 i387_return_value (struct gdbarch
*gdbarch
, struct regcache
*regcache
)
1838 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1841 /* Set the top of the floating-point register stack to 7. The
1842 actual value doesn't really matter, but 7 is what a normal
1843 function return would end up with if the program started out with
1844 a freshly initialized FPU. */
1845 regcache_raw_read_unsigned (regcache
, I387_FSTAT_REGNUM (tdep
), &fstat
);
1847 regcache_raw_write_unsigned (regcache
, I387_FSTAT_REGNUM (tdep
), fstat
);
1849 /* Mark %st(1) through %st(7) as empty. Since we set the top of the
1850 floating-point register stack to 7, the appropriate value for the
1851 tag word is 0x3fff. */
1852 regcache_raw_write_unsigned (regcache
, I387_FTAG_REGNUM (tdep
), 0x3fff);
1856 /* See i387-tdep.h. */
1859 i387_reset_bnd_regs (struct gdbarch
*gdbarch
, struct regcache
*regcache
)
1861 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1863 if (I387_BND0R_REGNUM (tdep
) > 0)
1865 gdb_byte bnd_buf
[16];
1867 memset (bnd_buf
, 0, 16);
1868 for (int i
= 0; i
< I387_NUM_BND_REGS
; i
++)
1869 regcache_raw_write (regcache
, I387_BND0R_REGNUM (tdep
) + i
, bnd_buf
);