1 /* Intel 387 floating point stuff.
3 Copyright (C) 1988-1989, 1991-1994, 1998-2005, 2007-2012 Free
4 Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "floatformat.h"
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
34 #include "i386-tdep.h"
35 #include "i387-tdep.h"
36 #include "i386-xstate.h"
38 /* Print the floating point number specified by RAW. */
41 print_i387_value (struct gdbarch
*gdbarch
,
42 const gdb_byte
*raw
, struct ui_file
*file
)
46 /* Using extract_typed_floating here might affect the representation
47 of certain numbers such as NaNs, even if GDB is running natively.
48 This is fine since our caller already detects such special
49 numbers and we print the hexadecimal representation anyway. */
50 value
= extract_typed_floating (raw
, i387_ext_type (gdbarch
));
52 /* We try to print 19 digits. The last digit may or may not contain
53 garbage, but we'd better print one too many. We need enough room
54 to print the value, 1 position for the sign, 1 for the decimal
55 point, 19 for the digits and 6 for the exponent adds up to 27. */
56 #ifdef PRINTF_HAS_LONG_DOUBLE
57 fprintf_filtered (file
, " %-+27.19Lg", (long double) value
);
59 fprintf_filtered (file
, " %-+27.19g", (double) value
);
63 /* Print the classification for the register contents RAW. */
66 print_i387_ext (struct gdbarch
*gdbarch
,
67 const gdb_byte
*raw
, struct ui_file
*file
)
71 unsigned int exponent
;
72 unsigned long fraction
[2];
75 integer
= raw
[7] & 0x80;
76 exponent
= (((raw
[9] & 0x7f) << 8) | raw
[8]);
77 fraction
[0] = ((raw
[3] << 24) | (raw
[2] << 16) | (raw
[1] << 8) | raw
[0]);
78 fraction
[1] = (((raw
[7] & 0x7f) << 24) | (raw
[6] << 16)
79 | (raw
[5] << 8) | raw
[4]);
81 if (exponent
== 0x7fff && integer
)
83 if (fraction
[0] == 0x00000000 && fraction
[1] == 0x00000000)
85 fprintf_filtered (file
, " %cInf", (sign
? '-' : '+'));
86 else if (sign
&& fraction
[0] == 0x00000000 && fraction
[1] == 0x40000000)
87 /* Real Indefinite (QNaN). */
88 fputs_unfiltered (" Real Indefinite (QNaN)", file
);
89 else if (fraction
[1] & 0x40000000)
91 fputs_filtered (" QNaN", file
);
94 fputs_filtered (" SNaN", file
);
96 else if (exponent
< 0x7fff && exponent
> 0x0000 && integer
)
98 print_i387_value (gdbarch
, raw
, file
);
99 else if (exponent
== 0x0000)
101 /* Denormal or zero. */
102 print_i387_value (gdbarch
, raw
, file
);
105 /* Pseudo-denormal. */
106 fputs_filtered (" Pseudo-denormal", file
);
107 else if (fraction
[0] || fraction
[1])
109 fputs_filtered (" Denormal", file
);
113 fputs_filtered (" Unsupported", file
);
116 /* Print the status word STATUS. If STATUS_P is false, then STATUS
120 print_i387_status_word (int status_p
,
121 unsigned int status
, struct ui_file
*file
)
123 fprintf_filtered (file
, "Status Word: ");
126 fprintf_filtered (file
, "%s\n", _("<unavailable>"));
130 fprintf_filtered (file
, "%s", hex_string_custom (status
, 4));
131 fputs_filtered (" ", file
);
132 fprintf_filtered (file
, " %s", (status
& 0x0001) ? "IE" : " ");
133 fprintf_filtered (file
, " %s", (status
& 0x0002) ? "DE" : " ");
134 fprintf_filtered (file
, " %s", (status
& 0x0004) ? "ZE" : " ");
135 fprintf_filtered (file
, " %s", (status
& 0x0008) ? "OE" : " ");
136 fprintf_filtered (file
, " %s", (status
& 0x0010) ? "UE" : " ");
137 fprintf_filtered (file
, " %s", (status
& 0x0020) ? "PE" : " ");
138 fputs_filtered (" ", file
);
139 fprintf_filtered (file
, " %s", (status
& 0x0080) ? "ES" : " ");
140 fputs_filtered (" ", file
);
141 fprintf_filtered (file
, " %s", (status
& 0x0040) ? "SF" : " ");
142 fputs_filtered (" ", file
);
143 fprintf_filtered (file
, " %s", (status
& 0x0100) ? "C0" : " ");
144 fprintf_filtered (file
, " %s", (status
& 0x0200) ? "C1" : " ");
145 fprintf_filtered (file
, " %s", (status
& 0x0400) ? "C2" : " ");
146 fprintf_filtered (file
, " %s", (status
& 0x4000) ? "C3" : " ");
148 fputs_filtered ("\n", file
);
150 fprintf_filtered (file
,
151 " TOP: %d\n", ((status
>> 11) & 7));
154 /* Print the control word CONTROL. If CONTROL_P is false, then
155 CONTROL was unavailable. */
158 print_i387_control_word (int control_p
,
159 unsigned int control
, struct ui_file
*file
)
161 fprintf_filtered (file
, "Control Word: ");
164 fprintf_filtered (file
, "%s\n", _("<unavailable>"));
168 fprintf_filtered (file
, "%s", hex_string_custom (control
, 4));
169 fputs_filtered (" ", file
);
170 fprintf_filtered (file
, " %s", (control
& 0x0001) ? "IM" : " ");
171 fprintf_filtered (file
, " %s", (control
& 0x0002) ? "DM" : " ");
172 fprintf_filtered (file
, " %s", (control
& 0x0004) ? "ZM" : " ");
173 fprintf_filtered (file
, " %s", (control
& 0x0008) ? "OM" : " ");
174 fprintf_filtered (file
, " %s", (control
& 0x0010) ? "UM" : " ");
175 fprintf_filtered (file
, " %s", (control
& 0x0020) ? "PM" : " ");
177 fputs_filtered ("\n", file
);
179 fputs_filtered (" PC: ", file
);
180 switch ((control
>> 8) & 3)
183 fputs_filtered ("Single Precision (24-bits)\n", file
);
186 fputs_filtered ("Reserved\n", file
);
189 fputs_filtered ("Double Precision (53-bits)\n", file
);
192 fputs_filtered ("Extended Precision (64-bits)\n", file
);
196 fputs_filtered (" RC: ", file
);
197 switch ((control
>> 10) & 3)
200 fputs_filtered ("Round to nearest\n", file
);
203 fputs_filtered ("Round down\n", file
);
206 fputs_filtered ("Round up\n", file
);
209 fputs_filtered ("Round toward zero\n", file
);
214 /* Print out the i387 floating point state. Note that we ignore FRAME
215 in the code below. That's OK since floating-point registers are
216 never saved on the stack. */
219 i387_print_float_info (struct gdbarch
*gdbarch
, struct ui_file
*file
,
220 struct frame_info
*frame
, const char *args
)
222 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_frame_arch (frame
));
244 gdb_assert (gdbarch
== get_frame_arch (frame
));
246 fctrl_p
= read_frame_register_unsigned (frame
,
247 I387_FCTRL_REGNUM (tdep
), &fctrl
);
248 fstat_p
= read_frame_register_unsigned (frame
,
249 I387_FSTAT_REGNUM (tdep
), &fstat
);
250 ftag_p
= read_frame_register_unsigned (frame
,
251 I387_FTAG_REGNUM (tdep
), &ftag
);
252 fiseg_p
= read_frame_register_unsigned (frame
,
253 I387_FISEG_REGNUM (tdep
), &fiseg
);
254 fioff_p
= read_frame_register_unsigned (frame
,
255 I387_FIOFF_REGNUM (tdep
), &fioff
);
256 foseg_p
= read_frame_register_unsigned (frame
,
257 I387_FOSEG_REGNUM (tdep
), &foseg
);
258 fooff_p
= read_frame_register_unsigned (frame
,
259 I387_FOOFF_REGNUM (tdep
), &fooff
);
260 fop_p
= read_frame_register_unsigned (frame
,
261 I387_FOP_REGNUM (tdep
), &fop
);
265 top
= ((fstat
>> 11) & 7);
267 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
269 struct value
*regval
;
274 fprintf_filtered (file
, "%sR%d: ", fpreg
== top
? "=>" : " ", fpreg
);
278 tag
= (ftag
>> (fpreg
* 2)) & 3;
283 fputs_filtered ("Valid ", file
);
286 fputs_filtered ("Zero ", file
);
289 fputs_filtered ("Special ", file
);
292 fputs_filtered ("Empty ", file
);
297 fputs_filtered ("Unknown ", file
);
299 regnum
= (fpreg
+ 8 - top
) % 8 + I387_ST0_REGNUM (tdep
);
300 regval
= get_frame_register_value (frame
, regnum
);
302 if (value_entirely_available (regval
))
304 const char *raw
= value_contents (regval
);
306 fputs_filtered ("0x", file
);
307 for (i
= 9; i
>= 0; i
--)
308 fprintf_filtered (file
, "%02x", raw
[i
]);
310 if (tag
!= -1 && tag
!= 3)
311 print_i387_ext (gdbarch
, raw
, file
);
314 fprintf_filtered (file
, "%s", _("<unavailable>"));
316 fputs_filtered ("\n", file
);
320 fputs_filtered ("\n", file
);
321 print_i387_status_word (fstat_p
, fstat
, file
);
322 print_i387_control_word (fctrl_p
, fctrl
, file
);
323 fprintf_filtered (file
, "Tag Word: %s\n",
324 ftag_p
? hex_string_custom (ftag
, 4) : _("<unavailable>"));
325 fprintf_filtered (file
, "Instruction Pointer: %s:",
326 fiseg_p
? hex_string_custom (fiseg
, 2) : _("<unavailable>"));
327 fprintf_filtered (file
, "%s\n",
328 fioff_p
? hex_string_custom (fioff
, 8) : _("<unavailable>"));
329 fprintf_filtered (file
, "Operand Pointer: %s:",
330 foseg_p
? hex_string_custom (foseg
, 2) : _("<unavailable>"));
331 fprintf_filtered (file
, "%s\n",
332 fooff_p
? hex_string_custom (fooff
, 8) : _("<unavailable>"));
333 fprintf_filtered (file
, "Opcode: %s\n",
335 ? (hex_string_custom (fop
? (fop
| 0xd800) : 0, 4))
336 : _("<unavailable>"));
340 /* Return nonzero if a value of type TYPE stored in register REGNUM
341 needs any special handling. */
344 i387_convert_register_p (struct gdbarch
*gdbarch
, int regnum
,
347 if (i386_fp_regnum_p (gdbarch
, regnum
))
349 /* Floating point registers must be converted unless we are
350 accessing them in their hardware type. */
351 if (type
== i387_ext_type (gdbarch
))
360 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
361 return its contents in TO. */
364 i387_register_to_value (struct frame_info
*frame
, int regnum
,
365 struct type
*type
, gdb_byte
*to
,
366 int *optimizedp
, int *unavailablep
)
368 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
369 gdb_byte from
[I386_MAX_REGISTER_SIZE
];
371 gdb_assert (i386_fp_regnum_p (gdbarch
, regnum
));
373 /* We only support floating-point values. */
374 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
376 warning (_("Cannot convert floating-point register value "
377 "to non-floating-point type."));
378 *optimizedp
= *unavailablep
= 0;
382 /* Convert to TYPE. */
383 if (!get_frame_register_bytes (frame
, regnum
, 0, TYPE_LENGTH (type
),
384 from
, optimizedp
, unavailablep
))
387 convert_typed_floating (from
, i387_ext_type (gdbarch
), to
, type
);
388 *optimizedp
= *unavailablep
= 0;
392 /* Write the contents FROM of a value of type TYPE into register
393 REGNUM in frame FRAME. */
396 i387_value_to_register (struct frame_info
*frame
, int regnum
,
397 struct type
*type
, const gdb_byte
*from
)
399 struct gdbarch
*gdbarch
= get_frame_arch (frame
);
400 gdb_byte to
[I386_MAX_REGISTER_SIZE
];
402 gdb_assert (i386_fp_regnum_p (gdbarch
, regnum
));
404 /* We only support floating-point values. */
405 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
407 warning (_("Cannot convert non-floating-point type "
408 "to floating-point register value."));
412 /* Convert from TYPE. */
413 convert_typed_floating (from
, type
, to
, i387_ext_type (gdbarch
));
414 put_frame_register (frame
, regnum
, to
);
418 /* Handle FSAVE and FXSAVE formats. */
420 /* At fsave_offset[REGNUM] you'll find the offset to the location in
421 the data structure used by the "fsave" instruction where GDB
422 register REGNUM is stored. */
424 static int fsave_offset
[] =
426 28 + 0 * 10, /* %st(0) ... */
433 28 + 7 * 10, /* ... %st(7). */
434 0, /* `fctrl' (16 bits). */
435 4, /* `fstat' (16 bits). */
436 8, /* `ftag' (16 bits). */
437 16, /* `fiseg' (16 bits). */
439 24, /* `foseg' (16 bits). */
441 18 /* `fop' (bottom 11 bits). */
444 #define FSAVE_ADDR(tdep, fsave, regnum) \
445 (fsave + fsave_offset[regnum - I387_ST0_REGNUM (tdep)])
448 /* Fill register REGNUM in REGCACHE with the appropriate value from
449 *FSAVE. This function masks off any of the reserved bits in
453 i387_supply_fsave (struct regcache
*regcache
, int regnum
, const void *fsave
)
455 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
456 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
457 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
458 const gdb_byte
*regs
= fsave
;
461 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
463 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
464 if (regnum
== -1 || regnum
== i
)
468 regcache_raw_supply (regcache
, i
, NULL
);
472 /* Most of the FPU control registers occupy only 16 bits in the
473 fsave area. Give those a special treatment. */
474 if (i
>= I387_FCTRL_REGNUM (tdep
)
475 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
479 memcpy (val
, FSAVE_ADDR (tdep
, regs
, i
), 2);
481 if (i
== I387_FOP_REGNUM (tdep
))
482 val
[1] &= ((1 << 3) - 1);
483 regcache_raw_supply (regcache
, i
, val
);
486 regcache_raw_supply (regcache
, i
, FSAVE_ADDR (tdep
, regs
, i
));
489 /* Provide dummy values for the SSE registers. */
490 for (i
= I387_XMM0_REGNUM (tdep
); i
< I387_MXCSR_REGNUM (tdep
); i
++)
491 if (regnum
== -1 || regnum
== i
)
492 regcache_raw_supply (regcache
, i
, NULL
);
493 if (regnum
== -1 || regnum
== I387_MXCSR_REGNUM (tdep
))
497 store_unsigned_integer (buf
, 4, byte_order
, 0x1f80);
498 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
), buf
);
502 /* Fill register REGNUM (if it is a floating-point register) in *FSAVE
503 with the value from REGCACHE. If REGNUM is -1, do this for all
504 registers. This function doesn't touch any of the reserved bits in
508 i387_collect_fsave (const struct regcache
*regcache
, int regnum
, void *fsave
)
510 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
511 gdb_byte
*regs
= fsave
;
514 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
516 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
517 if (regnum
== -1 || regnum
== i
)
519 /* Most of the FPU control registers occupy only 16 bits in
520 the fsave area. Give those a special treatment. */
521 if (i
>= I387_FCTRL_REGNUM (tdep
)
522 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
526 regcache_raw_collect (regcache
, i
, buf
);
528 if (i
== I387_FOP_REGNUM (tdep
))
530 /* The opcode occupies only 11 bits. Make sure we
531 don't touch the other bits. */
532 buf
[1] &= ((1 << 3) - 1);
533 buf
[1] |= ((FSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
535 memcpy (FSAVE_ADDR (tdep
, regs
, i
), buf
, 2);
538 regcache_raw_collect (regcache
, i
, FSAVE_ADDR (tdep
, regs
, i
));
543 /* At fxsave_offset[REGNUM] you'll find the offset to the location in
544 the data structure used by the "fxsave" instruction where GDB
545 register REGNUM is stored. */
547 static int fxsave_offset
[] =
549 32, /* %st(0) through ... */
556 144, /* ... %st(7) (80 bits each). */
557 0, /* `fctrl' (16 bits). */
558 2, /* `fstat' (16 bits). */
559 4, /* `ftag' (16 bits). */
560 12, /* `fiseg' (16 bits). */
562 20, /* `foseg' (16 bits). */
564 6, /* `fop' (bottom 11 bits). */
565 160 + 0 * 16, /* %xmm0 through ... */
580 160 + 15 * 16, /* ... %xmm15 (128 bits each). */
583 #define FXSAVE_ADDR(tdep, fxsave, regnum) \
584 (fxsave + fxsave_offset[regnum - I387_ST0_REGNUM (tdep)])
586 /* We made an unfortunate choice in putting %mxcsr after the SSE
587 registers %xmm0-%xmm7 instead of before, since it makes supporting
588 the registers %xmm8-%xmm15 on AMD64 a bit involved. Therefore we
589 don't include the offset for %mxcsr here above. */
591 #define FXSAVE_MXCSR_ADDR(fxsave) (fxsave + 24)
593 static int i387_tag (const gdb_byte
*raw
);
596 /* Fill register REGNUM in REGCACHE with the appropriate
597 floating-point or SSE register value from *FXSAVE. This function
598 masks off any of the reserved bits in *FXSAVE. */
601 i387_supply_fxsave (struct regcache
*regcache
, int regnum
, const void *fxsave
)
603 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
604 const gdb_byte
*regs
= fxsave
;
607 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
608 gdb_assert (tdep
->num_xmm_regs
> 0);
610 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_MXCSR_REGNUM (tdep
); i
++)
611 if (regnum
== -1 || regnum
== i
)
615 regcache_raw_supply (regcache
, i
, NULL
);
619 /* Most of the FPU control registers occupy only 16 bits in
620 the fxsave area. Give those a special treatment. */
621 if (i
>= I387_FCTRL_REGNUM (tdep
) && i
< I387_XMM0_REGNUM (tdep
)
622 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
626 memcpy (val
, FXSAVE_ADDR (tdep
, regs
, i
), 2);
628 if (i
== I387_FOP_REGNUM (tdep
))
629 val
[1] &= ((1 << 3) - 1);
630 else if (i
== I387_FTAG_REGNUM (tdep
))
632 /* The fxsave area contains a simplified version of
633 the tag word. We have to look at the actual 80-bit
634 FP data to recreate the traditional i387 tag word. */
636 unsigned long ftag
= 0;
640 top
= ((FXSAVE_ADDR (tdep
, regs
,
641 I387_FSTAT_REGNUM (tdep
)))[1] >> 3);
644 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
648 if (val
[0] & (1 << fpreg
))
650 int thisreg
= (fpreg
+ 8 - top
) % 8
651 + I387_ST0_REGNUM (tdep
);
652 tag
= i387_tag (FXSAVE_ADDR (tdep
, regs
, thisreg
));
657 ftag
|= tag
<< (2 * fpreg
);
659 val
[0] = ftag
& 0xff;
660 val
[1] = (ftag
>> 8) & 0xff;
662 regcache_raw_supply (regcache
, i
, val
);
665 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
668 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
671 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
), NULL
);
673 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
),
674 FXSAVE_MXCSR_ADDR (regs
));
678 /* Fill register REGNUM (if it is a floating-point or SSE register) in
679 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
680 all registers. This function doesn't touch any of the reserved
684 i387_collect_fxsave (const struct regcache
*regcache
, int regnum
, void *fxsave
)
686 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
687 gdb_byte
*regs
= fxsave
;
690 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
691 gdb_assert (tdep
->num_xmm_regs
> 0);
693 for (i
= I387_ST0_REGNUM (tdep
); i
< I387_MXCSR_REGNUM (tdep
); i
++)
694 if (regnum
== -1 || regnum
== i
)
696 /* Most of the FPU control registers occupy only 16 bits in
697 the fxsave area. Give those a special treatment. */
698 if (i
>= I387_FCTRL_REGNUM (tdep
) && i
< I387_XMM0_REGNUM (tdep
)
699 && i
!= I387_FIOFF_REGNUM (tdep
) && i
!= I387_FOOFF_REGNUM (tdep
))
703 regcache_raw_collect (regcache
, i
, buf
);
705 if (i
== I387_FOP_REGNUM (tdep
))
707 /* The opcode occupies only 11 bits. Make sure we
708 don't touch the other bits. */
709 buf
[1] &= ((1 << 3) - 1);
710 buf
[1] |= ((FXSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
712 else if (i
== I387_FTAG_REGNUM (tdep
))
714 /* Converting back is much easier. */
719 ftag
= (buf
[1] << 8) | buf
[0];
723 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
725 int tag
= (ftag
>> (fpreg
* 2)) & 3;
728 buf
[0] |= (1 << fpreg
);
731 memcpy (FXSAVE_ADDR (tdep
, regs
, i
), buf
, 2);
734 regcache_raw_collect (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
737 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
738 regcache_raw_collect (regcache
, I387_MXCSR_REGNUM (tdep
),
739 FXSAVE_MXCSR_ADDR (regs
));
742 /* `xstate_bv' is at byte offset 512. */
743 #define XSAVE_XSTATE_BV_ADDR(xsave) (xsave + 512)
745 /* At xsave_avxh_offset[REGNUM] you'll find the offset to the location in
746 the upper 128bit of AVX register data structure used by the "xsave"
747 instruction where GDB register REGNUM is stored. */
749 static int xsave_avxh_offset
[] =
751 576 + 0 * 16, /* Upper 128bit of %ymm0 through ... */
766 576 + 15 * 16 /* Upper 128bit of ... %ymm15 (128 bits each). */
769 #define XSAVE_AVXH_ADDR(tdep, xsave, regnum) \
770 (xsave + xsave_avxh_offset[regnum - I387_YMM0H_REGNUM (tdep)])
772 /* Similar to i387_supply_fxsave, but use XSAVE extended state. */
775 i387_supply_xsave (struct regcache
*regcache
, int regnum
,
778 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
779 const gdb_byte
*regs
= xsave
;
781 unsigned int clear_bv
;
782 static const gdb_byte zero
[MAX_REGISTER_SIZE
] = { 0 };
790 all
= x87
| sse
| avxh
793 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
794 gdb_assert (tdep
->num_xmm_regs
> 0);
798 else if (regnum
>= I387_YMM0H_REGNUM (tdep
)
799 && regnum
< I387_YMMENDH_REGNUM (tdep
))
801 else if (regnum
>= I387_XMM0_REGNUM(tdep
)
802 && regnum
< I387_MXCSR_REGNUM (tdep
))
804 else if (regnum
>= I387_ST0_REGNUM (tdep
)
805 && regnum
< I387_FCTRL_REGNUM (tdep
))
810 if (regs
!= NULL
&& regclass
!= none
)
812 /* Get `xstat_bv'. */
813 const gdb_byte
*xstate_bv_p
= XSAVE_XSTATE_BV_ADDR (regs
);
815 /* The supported bits in `xstat_bv' are 1 byte. Clear part in
816 vector registers if its bit in xstat_bv is zero. */
817 clear_bv
= (~(*xstate_bv_p
)) & tdep
->xcr0
;
820 clear_bv
= I386_XSTATE_AVX_MASK
;
822 /* With the delayed xsave mechanism, in between the program
823 starting, and the program accessing the vector registers for the
824 first time, the register's values are invalid. The kernel
825 initializes register states to zero when they are set the first
826 time in a program. This means that from the user-space programs'
827 perspective, it's the same as if the registers have always been
828 zero from the start of the program. Therefore, the debugger
829 should provide the same illusion to the user.
831 Note however, the case when REGS is NULL is a different case.
832 That case means we do not have access to the x87 states, so we
833 should mark the registers as unavailable (by supplying NULL). */
841 if ((clear_bv
& I386_XSTATE_AVX
))
842 regcache_raw_supply (regcache
, regnum
, regs
== NULL
? NULL
: zero
);
844 regcache_raw_supply (regcache
, regnum
,
845 XSAVE_AVXH_ADDR (tdep
, regs
, regnum
));
849 if ((clear_bv
& I386_XSTATE_SSE
))
850 regcache_raw_supply (regcache
, regnum
, regs
== NULL
? NULL
: zero
);
852 regcache_raw_supply (regcache
, regnum
,
853 FXSAVE_ADDR (tdep
, regs
, regnum
));
857 if ((clear_bv
& I386_XSTATE_X87
))
858 regcache_raw_supply (regcache
, regnum
, regs
== NULL
? NULL
: zero
);
860 regcache_raw_supply (regcache
, regnum
,
861 FXSAVE_ADDR (tdep
, regs
, regnum
));
865 /* Handle the upper YMM registers. */
866 if ((tdep
->xcr0
& I386_XSTATE_AVX
))
868 if ((clear_bv
& I386_XSTATE_AVX
))
870 for (i
= I387_YMM0H_REGNUM (tdep
);
871 i
< I387_YMMENDH_REGNUM (tdep
);
873 regcache_raw_supply (regcache
, i
, regs
== NULL
? NULL
: zero
);
877 for (i
= I387_YMM0H_REGNUM (tdep
);
878 i
< I387_YMMENDH_REGNUM (tdep
);
880 regcache_raw_supply (regcache
, i
,
881 XSAVE_AVXH_ADDR (tdep
, regs
, i
));
885 /* Handle the XMM registers. */
886 if ((tdep
->xcr0
& I386_XSTATE_SSE
))
888 if ((clear_bv
& I386_XSTATE_SSE
))
890 for (i
= I387_XMM0_REGNUM (tdep
);
891 i
< I387_MXCSR_REGNUM (tdep
);
893 regcache_raw_supply (regcache
, i
, regs
== NULL
? NULL
: zero
);
897 for (i
= I387_XMM0_REGNUM (tdep
);
898 i
< I387_MXCSR_REGNUM (tdep
); i
++)
899 regcache_raw_supply (regcache
, i
,
900 FXSAVE_ADDR (tdep
, regs
, i
));
904 /* Handle the x87 registers. */
905 if ((tdep
->xcr0
& I386_XSTATE_X87
))
907 if ((clear_bv
& I386_XSTATE_X87
))
909 for (i
= I387_ST0_REGNUM (tdep
);
910 i
< I387_FCTRL_REGNUM (tdep
);
912 regcache_raw_supply (regcache
, i
, regs
== NULL
? NULL
: zero
);
916 for (i
= I387_ST0_REGNUM (tdep
);
917 i
< I387_FCTRL_REGNUM (tdep
);
919 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
925 /* Only handle x87 control registers. */
926 for (i
= I387_FCTRL_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
927 if (regnum
== -1 || regnum
== i
)
931 regcache_raw_supply (regcache
, i
, NULL
);
935 /* Most of the FPU control registers occupy only 16 bits in
936 the xsave extended state. Give those a special treatment. */
937 if (i
!= I387_FIOFF_REGNUM (tdep
)
938 && i
!= I387_FOOFF_REGNUM (tdep
))
942 memcpy (val
, FXSAVE_ADDR (tdep
, regs
, i
), 2);
944 if (i
== I387_FOP_REGNUM (tdep
))
945 val
[1] &= ((1 << 3) - 1);
946 else if (i
== I387_FTAG_REGNUM (tdep
))
948 /* The fxsave area contains a simplified version of
949 the tag word. We have to look at the actual 80-bit
950 FP data to recreate the traditional i387 tag word. */
952 unsigned long ftag
= 0;
956 top
= ((FXSAVE_ADDR (tdep
, regs
,
957 I387_FSTAT_REGNUM (tdep
)))[1] >> 3);
960 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
964 if (val
[0] & (1 << fpreg
))
966 int thisreg
= (fpreg
+ 8 - top
) % 8
967 + I387_ST0_REGNUM (tdep
);
968 tag
= i387_tag (FXSAVE_ADDR (tdep
, regs
, thisreg
));
973 ftag
|= tag
<< (2 * fpreg
);
975 val
[0] = ftag
& 0xff;
976 val
[1] = (ftag
>> 8) & 0xff;
978 regcache_raw_supply (regcache
, i
, val
);
981 regcache_raw_supply (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
984 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
986 p
= regs
== NULL
? NULL
: FXSAVE_MXCSR_ADDR (regs
);
987 regcache_raw_supply (regcache
, I387_MXCSR_REGNUM (tdep
), p
);
991 /* Similar to i387_collect_fxsave, but use XSAVE extended state. */
994 i387_collect_xsave (const struct regcache
*regcache
, int regnum
,
995 void *xsave
, int gcore
)
997 struct gdbarch_tdep
*tdep
= gdbarch_tdep (get_regcache_arch (regcache
));
998 gdb_byte
*regs
= xsave
;
1007 all
= x87
| sse
| avxh
1010 gdb_assert (tdep
->st0_regnum
>= I386_ST0_REGNUM
);
1011 gdb_assert (tdep
->num_xmm_regs
> 0);
1015 else if (regnum
>= I387_YMM0H_REGNUM (tdep
)
1016 && regnum
< I387_YMMENDH_REGNUM (tdep
))
1018 else if (regnum
>= I387_XMM0_REGNUM(tdep
)
1019 && regnum
< I387_MXCSR_REGNUM (tdep
))
1021 else if (regnum
>= I387_ST0_REGNUM (tdep
)
1022 && regnum
< I387_FCTRL_REGNUM (tdep
))
1029 /* Clear XSAVE extended state. */
1030 memset (regs
, 0, I386_XSTATE_SIZE (tdep
->xcr0
));
1032 /* Update XCR0 and `xstate_bv' with XCR0 for gcore. */
1033 if (tdep
->xsave_xcr0_offset
!= -1)
1034 memcpy (regs
+ tdep
->xsave_xcr0_offset
, &tdep
->xcr0
, 8);
1035 memcpy (XSAVE_XSTATE_BV_ADDR (regs
), &tdep
->xcr0
, 8);
1038 if ((regclass
& check
))
1040 gdb_byte raw
[I386_MAX_REGISTER_SIZE
];
1041 gdb_byte
*xstate_bv_p
= XSAVE_XSTATE_BV_ADDR (regs
);
1042 unsigned int xstate_bv
= 0;
1043 /* The supported bits in `xstat_bv' are 1 byte. */
1044 unsigned int clear_bv
= (~(*xstate_bv_p
)) & tdep
->xcr0
;
1047 /* Clear register set if its bit in xstat_bv is zero. */
1050 if ((clear_bv
& I386_XSTATE_AVX
))
1051 for (i
= I387_YMM0H_REGNUM (tdep
);
1052 i
< I387_YMMENDH_REGNUM (tdep
); i
++)
1053 memset (XSAVE_AVXH_ADDR (tdep
, regs
, i
), 0, 16);
1055 if ((clear_bv
& I386_XSTATE_SSE
))
1056 for (i
= I387_XMM0_REGNUM (tdep
);
1057 i
< I387_MXCSR_REGNUM (tdep
); i
++)
1058 memset (FXSAVE_ADDR (tdep
, regs
, i
), 0, 16);
1060 if ((clear_bv
& I386_XSTATE_X87
))
1061 for (i
= I387_ST0_REGNUM (tdep
);
1062 i
< I387_FCTRL_REGNUM (tdep
); i
++)
1063 memset (FXSAVE_ADDR (tdep
, regs
, i
), 0, 10);
1066 if (regclass
== all
)
1068 /* Check if any upper YMM registers are changed. */
1069 if ((tdep
->xcr0
& I386_XSTATE_AVX
))
1070 for (i
= I387_YMM0H_REGNUM (tdep
);
1071 i
< I387_YMMENDH_REGNUM (tdep
); i
++)
1073 regcache_raw_collect (regcache
, i
, raw
);
1074 p
= XSAVE_AVXH_ADDR (tdep
, regs
, i
);
1075 if (memcmp (raw
, p
, 16))
1077 xstate_bv
|= I386_XSTATE_AVX
;
1078 memcpy (p
, raw
, 16);
1082 /* Check if any SSE registers are changed. */
1083 if ((tdep
->xcr0
& I386_XSTATE_SSE
))
1084 for (i
= I387_XMM0_REGNUM (tdep
);
1085 i
< I387_MXCSR_REGNUM (tdep
); i
++)
1087 regcache_raw_collect (regcache
, i
, raw
);
1088 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1089 if (memcmp (raw
, p
, 16))
1091 xstate_bv
|= I386_XSTATE_SSE
;
1092 memcpy (p
, raw
, 16);
1096 /* Check if any X87 registers are changed. */
1097 if ((tdep
->xcr0
& I386_XSTATE_X87
))
1098 for (i
= I387_ST0_REGNUM (tdep
);
1099 i
< I387_FCTRL_REGNUM (tdep
); i
++)
1101 regcache_raw_collect (regcache
, i
, raw
);
1102 p
= FXSAVE_ADDR (tdep
, regs
, i
);
1103 if (memcmp (raw
, p
, 10))
1105 xstate_bv
|= I386_XSTATE_X87
;
1106 memcpy (p
, raw
, 10);
1112 /* Check if REGNUM is changed. */
1113 regcache_raw_collect (regcache
, regnum
, raw
);
1118 internal_error (__FILE__
, __LINE__
,
1119 _("invalid i387 regclass"));
1122 /* This is an upper YMM register. */
1123 p
= XSAVE_AVXH_ADDR (tdep
, regs
, regnum
);
1124 if (memcmp (raw
, p
, 16))
1126 xstate_bv
|= I386_XSTATE_AVX
;
1127 memcpy (p
, raw
, 16);
1132 /* This is an SSE register. */
1133 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
1134 if (memcmp (raw
, p
, 16))
1136 xstate_bv
|= I386_XSTATE_SSE
;
1137 memcpy (p
, raw
, 16);
1142 /* This is an x87 register. */
1143 p
= FXSAVE_ADDR (tdep
, regs
, regnum
);
1144 if (memcmp (raw
, p
, 10))
1146 xstate_bv
|= I386_XSTATE_X87
;
1147 memcpy (p
, raw
, 10);
1153 /* Update the corresponding bits in `xstate_bv' if any SSE/AVX
1154 registers are changed. */
1157 /* The supported bits in `xstat_bv' are 1 byte. */
1158 *xstate_bv_p
|= (gdb_byte
) xstate_bv
;
1163 internal_error (__FILE__
, __LINE__
,
1164 _("invalid i387 regclass"));
1172 /* Register REGNUM has been updated. Return. */
1178 /* Return if REGNUM isn't changed. */
1179 if (regclass
!= all
)
1184 /* Only handle x87 control registers. */
1185 for (i
= I387_FCTRL_REGNUM (tdep
); i
< I387_XMM0_REGNUM (tdep
); i
++)
1186 if (regnum
== -1 || regnum
== i
)
1188 /* Most of the FPU control registers occupy only 16 bits in
1189 the xsave extended state. Give those a special treatment. */
1190 if (i
!= I387_FIOFF_REGNUM (tdep
)
1191 && i
!= I387_FOOFF_REGNUM (tdep
))
1195 regcache_raw_collect (regcache
, i
, buf
);
1197 if (i
== I387_FOP_REGNUM (tdep
))
1199 /* The opcode occupies only 11 bits. Make sure we
1200 don't touch the other bits. */
1201 buf
[1] &= ((1 << 3) - 1);
1202 buf
[1] |= ((FXSAVE_ADDR (tdep
, regs
, i
))[1] & ~((1 << 3) - 1));
1204 else if (i
== I387_FTAG_REGNUM (tdep
))
1206 /* Converting back is much easier. */
1208 unsigned short ftag
;
1211 ftag
= (buf
[1] << 8) | buf
[0];
1215 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
1217 int tag
= (ftag
>> (fpreg
* 2)) & 3;
1220 buf
[0] |= (1 << fpreg
);
1223 memcpy (FXSAVE_ADDR (tdep
, regs
, i
), buf
, 2);
1226 regcache_raw_collect (regcache
, i
, FXSAVE_ADDR (tdep
, regs
, i
));
1229 if (regnum
== I387_MXCSR_REGNUM (tdep
) || regnum
== -1)
1230 regcache_raw_collect (regcache
, I387_MXCSR_REGNUM (tdep
),
1231 FXSAVE_MXCSR_ADDR (regs
));
1234 /* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
1238 i387_tag (const gdb_byte
*raw
)
1241 unsigned int exponent
;
1242 unsigned long fraction
[2];
1244 integer
= raw
[7] & 0x80;
1245 exponent
= (((raw
[9] & 0x7f) << 8) | raw
[8]);
1246 fraction
[0] = ((raw
[3] << 24) | (raw
[2] << 16) | (raw
[1] << 8) | raw
[0]);
1247 fraction
[1] = (((raw
[7] & 0x7f) << 24) | (raw
[6] << 16)
1248 | (raw
[5] << 8) | raw
[4]);
1250 if (exponent
== 0x7fff)
1255 else if (exponent
== 0x0000)
1257 if (fraction
[0] == 0x0000 && fraction
[1] == 0x0000 && !integer
)
1283 /* Prepare the FPU stack in REGCACHE for a function return. */
1286 i387_return_value (struct gdbarch
*gdbarch
, struct regcache
*regcache
)
1288 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1291 /* Set the top of the floating-point register stack to 7. The
1292 actual value doesn't really matter, but 7 is what a normal
1293 function return would end up with if the program started out with
1294 a freshly initialized FPU. */
1295 regcache_raw_read_unsigned (regcache
, I387_FSTAT_REGNUM (tdep
), &fstat
);
1297 regcache_raw_write_unsigned (regcache
, I387_FSTAT_REGNUM (tdep
), fstat
);
1299 /* Mark %st(1) through %st(7) as empty. Since we set the top of the
1300 floating-point register stack to 7, the appropriate value for the
1301 tag word is 0x3fff. */
1302 regcache_raw_write_unsigned (regcache
, I387_FTAG_REGNUM (tdep
), 0x3fff);
This page took 0.058168 seconds and 5 git commands to generate.