windows-nat: Don't change current_event.dwThreadId in handle_output_debug_string()
[deliverable/binutils-gdb.git] / gdb / i387-tdep.c
CommitLineData
c906108c 1/* Intel 387 floating point stuff.
38edeab8 2
32d0add0 3 Copyright (C) 1988-2015 Free Software Foundation, Inc.
c906108c 4
c5aa993b 5 This file is part of GDB.
c906108c 6
c5aa993b
JM
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
c5aa993b 10 (at your option) any later version.
c906108c 11
c5aa993b
JM
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.
c906108c 16
c5aa993b 17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
19
20#include "defs.h"
786a90bb
MK
21#include "doublest.h"
22#include "floatformat.h"
c906108c 23#include "frame.h"
786a90bb 24#include "gdbcore.h"
c906108c
SS
25#include "inferior.h"
26#include "language.h"
4e052eda 27#include "regcache.h"
786a90bb
MK
28#include "value.h"
29
9a82579f 30#include "i386-tdep.h"
42c466d7 31#include "i387-tdep.h"
df7e5265 32#include "x86-xstate.h"
c906108c 33
de57eccd 34/* Print the floating point number specified by RAW. */
786a90bb 35
de57eccd 36static void
27067745
UW
37print_i387_value (struct gdbarch *gdbarch,
38 const gdb_byte *raw, struct ui_file *file)
de57eccd
JM
39{
40 DOUBLEST value;
4583280c
MK
41
42 /* Using extract_typed_floating here might affect the representation
43 of certain numbers such as NaNs, even if GDB is running natively.
44 This is fine since our caller already detects such special
45 numbers and we print the hexadecimal representation anyway. */
27067745 46 value = extract_typed_floating (raw, i387_ext_type (gdbarch));
de57eccd
JM
47
48 /* We try to print 19 digits. The last digit may or may not contain
49 garbage, but we'd better print one too many. We need enough room
50 to print the value, 1 position for the sign, 1 for the decimal
51 point, 19 for the digits and 6 for the exponent adds up to 27. */
52#ifdef PRINTF_HAS_LONG_DOUBLE
61113f8b 53 fprintf_filtered (file, " %-+27.19Lg", (long double) value);
de57eccd 54#else
61113f8b 55 fprintf_filtered (file, " %-+27.19g", (double) value);
de57eccd
JM
56#endif
57}
58
59/* Print the classification for the register contents RAW. */
786a90bb 60
de57eccd 61static void
27067745
UW
62print_i387_ext (struct gdbarch *gdbarch,
63 const gdb_byte *raw, struct ui_file *file)
de57eccd
JM
64{
65 int sign;
66 int integer;
67 unsigned int exponent;
68 unsigned long fraction[2];
69
70 sign = raw[9] & 0x80;
71 integer = raw[7] & 0x80;
72 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
73 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
74 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
75 | (raw[5] << 8) | raw[4]);
76
77 if (exponent == 0x7fff && integer)
78 {
79 if (fraction[0] == 0x00000000 && fraction[1] == 0x00000000)
80 /* Infinity. */
61113f8b 81 fprintf_filtered (file, " %cInf", (sign ? '-' : '+'));
de57eccd
JM
82 else if (sign && fraction[0] == 0x00000000 && fraction[1] == 0x40000000)
83 /* Real Indefinite (QNaN). */
61113f8b 84 fputs_unfiltered (" Real Indefinite (QNaN)", file);
de57eccd
JM
85 else if (fraction[1] & 0x40000000)
86 /* QNaN. */
61113f8b 87 fputs_filtered (" QNaN", file);
de57eccd
JM
88 else
89 /* SNaN. */
61113f8b 90 fputs_filtered (" SNaN", file);
de57eccd
JM
91 }
92 else if (exponent < 0x7fff && exponent > 0x0000 && integer)
93 /* Normal. */
27067745 94 print_i387_value (gdbarch, raw, file);
de57eccd
JM
95 else if (exponent == 0x0000)
96 {
97 /* Denormal or zero. */
27067745 98 print_i387_value (gdbarch, raw, file);
de57eccd
JM
99
100 if (integer)
101 /* Pseudo-denormal. */
61113f8b 102 fputs_filtered (" Pseudo-denormal", file);
de57eccd
JM
103 else if (fraction[0] || fraction[1])
104 /* Denormal. */
61113f8b 105 fputs_filtered (" Denormal", file);
de57eccd
JM
106 }
107 else
108 /* Unsupported. */
61113f8b 109 fputs_filtered (" Unsupported", file);
de57eccd
JM
110}
111
ad5f7d6e
PA
112/* Print the status word STATUS. If STATUS_P is false, then STATUS
113 was unavailable. */
786a90bb 114
de57eccd 115static void
ad5f7d6e
PA
116print_i387_status_word (int status_p,
117 unsigned int status, struct ui_file *file)
de57eccd 118{
ad5f7d6e
PA
119 fprintf_filtered (file, "Status Word: ");
120 if (!status_p)
121 {
122 fprintf_filtered (file, "%s\n", _("<unavailable>"));
123 return;
124 }
125
126 fprintf_filtered (file, "%s", hex_string_custom (status, 4));
61113f8b
MK
127 fputs_filtered (" ", file);
128 fprintf_filtered (file, " %s", (status & 0x0001) ? "IE" : " ");
129 fprintf_filtered (file, " %s", (status & 0x0002) ? "DE" : " ");
130 fprintf_filtered (file, " %s", (status & 0x0004) ? "ZE" : " ");
131 fprintf_filtered (file, " %s", (status & 0x0008) ? "OE" : " ");
132 fprintf_filtered (file, " %s", (status & 0x0010) ? "UE" : " ");
133 fprintf_filtered (file, " %s", (status & 0x0020) ? "PE" : " ");
134 fputs_filtered (" ", file);
135 fprintf_filtered (file, " %s", (status & 0x0080) ? "ES" : " ");
136 fputs_filtered (" ", file);
137 fprintf_filtered (file, " %s", (status & 0x0040) ? "SF" : " ");
138 fputs_filtered (" ", file);
139 fprintf_filtered (file, " %s", (status & 0x0100) ? "C0" : " ");
140 fprintf_filtered (file, " %s", (status & 0x0200) ? "C1" : " ");
141 fprintf_filtered (file, " %s", (status & 0x0400) ? "C2" : " ");
142 fprintf_filtered (file, " %s", (status & 0x4000) ? "C3" : " ");
143
144 fputs_filtered ("\n", file);
145
146 fprintf_filtered (file,
147 " TOP: %d\n", ((status >> 11) & 7));
de57eccd
JM
148}
149
ad5f7d6e
PA
150/* Print the control word CONTROL. If CONTROL_P is false, then
151 CONTROL was unavailable. */
786a90bb 152
de57eccd 153static void
ad5f7d6e
PA
154print_i387_control_word (int control_p,
155 unsigned int control, struct ui_file *file)
de57eccd 156{
ad5f7d6e
PA
157 fprintf_filtered (file, "Control Word: ");
158 if (!control_p)
159 {
160 fprintf_filtered (file, "%s\n", _("<unavailable>"));
161 return;
162 }
163
164 fprintf_filtered (file, "%s", hex_string_custom (control, 4));
61113f8b
MK
165 fputs_filtered (" ", file);
166 fprintf_filtered (file, " %s", (control & 0x0001) ? "IM" : " ");
167 fprintf_filtered (file, " %s", (control & 0x0002) ? "DM" : " ");
168 fprintf_filtered (file, " %s", (control & 0x0004) ? "ZM" : " ");
169 fprintf_filtered (file, " %s", (control & 0x0008) ? "OM" : " ");
170 fprintf_filtered (file, " %s", (control & 0x0010) ? "UM" : " ");
171 fprintf_filtered (file, " %s", (control & 0x0020) ? "PM" : " ");
de57eccd 172
61113f8b 173 fputs_filtered ("\n", file);
de57eccd 174
61113f8b 175 fputs_filtered (" PC: ", file);
de57eccd
JM
176 switch ((control >> 8) & 3)
177 {
178 case 0:
61113f8b 179 fputs_filtered ("Single Precision (24-bits)\n", file);
de57eccd
JM
180 break;
181 case 1:
61113f8b 182 fputs_filtered ("Reserved\n", file);
de57eccd
JM
183 break;
184 case 2:
61113f8b 185 fputs_filtered ("Double Precision (53-bits)\n", file);
de57eccd
JM
186 break;
187 case 3:
61113f8b 188 fputs_filtered ("Extended Precision (64-bits)\n", file);
de57eccd
JM
189 break;
190 }
191
61113f8b 192 fputs_filtered (" RC: ", file);
de57eccd
JM
193 switch ((control >> 10) & 3)
194 {
195 case 0:
61113f8b 196 fputs_filtered ("Round to nearest\n", file);
de57eccd
JM
197 break;
198 case 1:
61113f8b 199 fputs_filtered ("Round down\n", file);
de57eccd
JM
200 break;
201 case 2:
61113f8b 202 fputs_filtered ("Round up\n", file);
de57eccd
JM
203 break;
204 case 3:
61113f8b 205 fputs_filtered ("Round toward zero\n", file);
de57eccd
JM
206 break;
207 }
208}
209
9b949a49 210/* Print out the i387 floating point state. Note that we ignore FRAME
7d8d2918
MK
211 in the code below. That's OK since floating-point registers are
212 never saved on the stack. */
213
de57eccd 214void
61113f8b 215i387_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
8e186fd6 216 struct frame_info *frame, const char *args)
de57eccd 217{
5716833c 218 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame));
1d70089a 219 ULONGEST fctrl;
ad5f7d6e 220 int fctrl_p;
1d70089a 221 ULONGEST fstat;
ad5f7d6e 222 int fstat_p;
1d70089a 223 ULONGEST ftag;
ad5f7d6e 224 int ftag_p;
1d70089a 225 ULONGEST fiseg;
ad5f7d6e 226 int fiseg_p;
1d70089a 227 ULONGEST fioff;
ad5f7d6e 228 int fioff_p;
1d70089a 229 ULONGEST foseg;
ad5f7d6e 230 int foseg_p;
1d70089a 231 ULONGEST fooff;
ad5f7d6e 232 int fooff_p;
1d70089a 233 ULONGEST fop;
ad5f7d6e 234 int fop_p;
de57eccd
JM
235 int fpreg;
236 int top;
237
5716833c
MK
238 gdb_assert (gdbarch == get_frame_arch (frame));
239
ad5f7d6e
PA
240 fctrl_p = read_frame_register_unsigned (frame,
241 I387_FCTRL_REGNUM (tdep), &fctrl);
242 fstat_p = read_frame_register_unsigned (frame,
243 I387_FSTAT_REGNUM (tdep), &fstat);
244 ftag_p = read_frame_register_unsigned (frame,
245 I387_FTAG_REGNUM (tdep), &ftag);
246 fiseg_p = read_frame_register_unsigned (frame,
247 I387_FISEG_REGNUM (tdep), &fiseg);
248 fioff_p = read_frame_register_unsigned (frame,
249 I387_FIOFF_REGNUM (tdep), &fioff);
250 foseg_p = read_frame_register_unsigned (frame,
251 I387_FOSEG_REGNUM (tdep), &foseg);
252 fooff_p = read_frame_register_unsigned (frame,
253 I387_FOOFF_REGNUM (tdep), &fooff);
254 fop_p = read_frame_register_unsigned (frame,
255 I387_FOP_REGNUM (tdep), &fop);
256
257 if (fstat_p)
de57eccd 258 {
ad5f7d6e 259 top = ((fstat >> 11) & 7);
de57eccd 260
ad5f7d6e 261 for (fpreg = 7; fpreg >= 0; fpreg--)
de57eccd 262 {
ad5f7d6e
PA
263 struct value *regval;
264 int regnum;
265 int i;
266 int tag = -1;
267
268 fprintf_filtered (file, "%sR%d: ", fpreg == top ? "=>" : " ", fpreg);
269
270 if (ftag_p)
271 {
272 tag = (ftag >> (fpreg * 2)) & 3;
273
274 switch (tag)
275 {
276 case 0:
277 fputs_filtered ("Valid ", file);
278 break;
279 case 1:
280 fputs_filtered ("Zero ", file);
281 break;
282 case 2:
283 fputs_filtered ("Special ", file);
284 break;
285 case 3:
286 fputs_filtered ("Empty ", file);
287 break;
288 }
289 }
290 else
291 fputs_filtered ("Unknown ", file);
292
293 regnum = (fpreg + 8 - top) % 8 + I387_ST0_REGNUM (tdep);
294 regval = get_frame_register_value (frame, regnum);
295
296 if (value_entirely_available (regval))
297 {
433730c9 298 const gdb_byte *raw = value_contents (regval);
ad5f7d6e
PA
299
300 fputs_filtered ("0x", file);
301 for (i = 9; i >= 0; i--)
302 fprintf_filtered (file, "%02x", raw[i]);
303
304 if (tag != -1 && tag != 3)
305 print_i387_ext (gdbarch, raw, file);
306 }
307 else
308 fprintf_filtered (file, "%s", _("<unavailable>"));
309
310 fputs_filtered ("\n", file);
de57eccd 311 }
de57eccd
JM
312 }
313
f16a25ae 314 fputs_filtered ("\n", file);
ad5f7d6e
PA
315 print_i387_status_word (fstat_p, fstat, file);
316 print_i387_control_word (fctrl_p, fctrl, file);
61113f8b 317 fprintf_filtered (file, "Tag Word: %s\n",
ad5f7d6e 318 ftag_p ? hex_string_custom (ftag, 4) : _("<unavailable>"));
61113f8b 319 fprintf_filtered (file, "Instruction Pointer: %s:",
ad5f7d6e
PA
320 fiseg_p ? hex_string_custom (fiseg, 2) : _("<unavailable>"));
321 fprintf_filtered (file, "%s\n",
322 fioff_p ? hex_string_custom (fioff, 8) : _("<unavailable>"));
61113f8b 323 fprintf_filtered (file, "Operand Pointer: %s:",
ad5f7d6e
PA
324 foseg_p ? hex_string_custom (foseg, 2) : _("<unavailable>"));
325 fprintf_filtered (file, "%s\n",
326 fooff_p ? hex_string_custom (fooff, 8) : _("<unavailable>"));
61113f8b 327 fprintf_filtered (file, "Opcode: %s\n",
ad5f7d6e
PA
328 fop_p
329 ? (hex_string_custom (fop ? (fop | 0xd800) : 0, 4))
330 : _("<unavailable>"));
de57eccd 331}
d532c08f
MK
332\f
333
83acabca
DJ
334/* Return nonzero if a value of type TYPE stored in register REGNUM
335 needs any special handling. */
336
337int
1777feb0
MS
338i387_convert_register_p (struct gdbarch *gdbarch, int regnum,
339 struct type *type)
83acabca 340{
20a6ec49 341 if (i386_fp_regnum_p (gdbarch, regnum))
83acabca
DJ
342 {
343 /* Floating point registers must be converted unless we are
344 accessing them in their hardware type. */
27067745 345 if (type == i387_ext_type (gdbarch))
83acabca
DJ
346 return 0;
347 else
348 return 1;
349 }
350
351 return 0;
352}
353
d532c08f
MK
354/* Read a value of type TYPE from register REGNUM in frame FRAME, and
355 return its contents in TO. */
356
8dccd430 357int
d532c08f 358i387_register_to_value (struct frame_info *frame, int regnum,
8dccd430
PA
359 struct type *type, gdb_byte *to,
360 int *optimizedp, int *unavailablep)
d532c08f 361{
27067745 362 struct gdbarch *gdbarch = get_frame_arch (frame);
b4ad899f 363 gdb_byte from[I386_MAX_REGISTER_SIZE];
d532c08f 364
27067745 365 gdb_assert (i386_fp_regnum_p (gdbarch, regnum));
d532c08f
MK
366
367 /* We only support floating-point values. */
368 if (TYPE_CODE (type) != TYPE_CODE_FLT)
369 {
8a3fe4f8
AC
370 warning (_("Cannot convert floating-point register value "
371 "to non-floating-point type."));
8dccd430
PA
372 *optimizedp = *unavailablep = 0;
373 return 0;
d532c08f
MK
374 }
375
83acabca 376 /* Convert to TYPE. */
8dccd430
PA
377 if (!get_frame_register_bytes (frame, regnum, 0, TYPE_LENGTH (type),
378 from, optimizedp, unavailablep))
379 return 0;
380
27067745 381 convert_typed_floating (from, i387_ext_type (gdbarch), to, type);
8dccd430
PA
382 *optimizedp = *unavailablep = 0;
383 return 1;
d532c08f
MK
384}
385
386/* Write the contents FROM of a value of type TYPE into register
387 REGNUM in frame FRAME. */
388
389void
390i387_value_to_register (struct frame_info *frame, int regnum,
42835c2b 391 struct type *type, const gdb_byte *from)
d532c08f 392{
27067745 393 struct gdbarch *gdbarch = get_frame_arch (frame);
b4ad899f 394 gdb_byte to[I386_MAX_REGISTER_SIZE];
d532c08f 395
27067745 396 gdb_assert (i386_fp_regnum_p (gdbarch, regnum));
d532c08f
MK
397
398 /* We only support floating-point values. */
399 if (TYPE_CODE (type) != TYPE_CODE_FLT)
400 {
8a3fe4f8
AC
401 warning (_("Cannot convert non-floating-point type "
402 "to floating-point register value."));
d532c08f
MK
403 return;
404 }
405
83acabca 406 /* Convert from TYPE. */
27067745 407 convert_typed_floating (from, type, to, i387_ext_type (gdbarch));
d532c08f
MK
408 put_frame_register (frame, regnum, to);
409}
410\f
e750d25e 411
786a90bb 412/* Handle FSAVE and FXSAVE formats. */
e750d25e
JT
413
414/* At fsave_offset[REGNUM] you'll find the offset to the location in
415 the data structure used by the "fsave" instruction where GDB
416 register REGNUM is stored. */
417
418static int fsave_offset[] =
419{
5716833c
MK
420 28 + 0 * 10, /* %st(0) ... */
421 28 + 1 * 10,
422 28 + 2 * 10,
423 28 + 3 * 10,
424 28 + 4 * 10,
425 28 + 5 * 10,
426 28 + 6 * 10,
427 28 + 7 * 10, /* ... %st(7). */
428 0, /* `fctrl' (16 bits). */
429 4, /* `fstat' (16 bits). */
430 8, /* `ftag' (16 bits). */
431 16, /* `fiseg' (16 bits). */
432 12, /* `fioff'. */
433 24, /* `foseg' (16 bits). */
434 20, /* `fooff'. */
435 18 /* `fop' (bottom 11 bits). */
e750d25e
JT
436};
437
20a6ec49
MD
438#define FSAVE_ADDR(tdep, fsave, regnum) \
439 (fsave + fsave_offset[regnum - I387_ST0_REGNUM (tdep)])
e750d25e
JT
440\f
441
41d041d6
MK
442/* Fill register REGNUM in REGCACHE with the appropriate value from
443 *FSAVE. This function masks off any of the reserved bits in
444 *FSAVE. */
e750d25e
JT
445
446void
41d041d6 447i387_supply_fsave (struct regcache *regcache, int regnum, const void *fsave)
e750d25e 448{
e17a4113
UW
449 struct gdbarch *gdbarch = get_regcache_arch (regcache);
450 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
451 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
b4ad899f 452 const gdb_byte *regs = fsave;
e750d25e
JT
453 int i;
454
5716833c
MK
455 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
456
20a6ec49 457 for (i = I387_ST0_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
ed504bdf
MK
458 if (regnum == -1 || regnum == i)
459 {
460 if (fsave == NULL)
461 {
5716833c
MK
462 regcache_raw_supply (regcache, i, NULL);
463 continue;
ed504bdf
MK
464 }
465
466 /* Most of the FPU control registers occupy only 16 bits in the
467 fsave area. Give those a special treatment. */
20a6ec49
MD
468 if (i >= I387_FCTRL_REGNUM (tdep)
469 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
ed504bdf 470 {
b4ad899f 471 gdb_byte val[4];
ed504bdf 472
20a6ec49 473 memcpy (val, FSAVE_ADDR (tdep, regs, i), 2);
ed504bdf 474 val[2] = val[3] = 0;
20a6ec49 475 if (i == I387_FOP_REGNUM (tdep))
ed504bdf 476 val[1] &= ((1 << 3) - 1);
5716833c 477 regcache_raw_supply (regcache, i, val);
ed504bdf
MK
478 }
479 else
20a6ec49 480 regcache_raw_supply (regcache, i, FSAVE_ADDR (tdep, regs, i));
ed504bdf 481 }
b87bc0d8
MK
482
483 /* Provide dummy values for the SSE registers. */
20a6ec49 484 for (i = I387_XMM0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
b87bc0d8
MK
485 if (regnum == -1 || regnum == i)
486 regcache_raw_supply (regcache, i, NULL);
20a6ec49 487 if (regnum == -1 || regnum == I387_MXCSR_REGNUM (tdep))
b87bc0d8 488 {
b4ad899f 489 gdb_byte buf[4];
b87bc0d8 490
e17a4113 491 store_unsigned_integer (buf, 4, byte_order, 0x1f80);
20a6ec49 492 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep), buf);
b87bc0d8 493 }
e750d25e
JT
494}
495
496/* Fill register REGNUM (if it is a floating-point register) in *FSAVE
63b6c53f
MK
497 with the value from REGCACHE. If REGNUM is -1, do this for all
498 registers. This function doesn't touch any of the reserved bits in
499 *FSAVE. */
e750d25e
JT
500
501void
63b6c53f 502i387_collect_fsave (const struct regcache *regcache, int regnum, void *fsave)
e750d25e 503{
e071d1f6 504 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
b4ad899f 505 gdb_byte *regs = fsave;
e750d25e
JT
506 int i;
507
5716833c
MK
508 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
509
20a6ec49 510 for (i = I387_ST0_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
e750d25e
JT
511 if (regnum == -1 || regnum == i)
512 {
513 /* Most of the FPU control registers occupy only 16 bits in
514 the fsave area. Give those a special treatment. */
20a6ec49
MD
515 if (i >= I387_FCTRL_REGNUM (tdep)
516 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
e750d25e 517 {
b4ad899f 518 gdb_byte buf[4];
e750d25e 519
5716833c 520 regcache_raw_collect (regcache, i, buf);
e750d25e 521
20a6ec49 522 if (i == I387_FOP_REGNUM (tdep))
e750d25e
JT
523 {
524 /* The opcode occupies only 11 bits. Make sure we
525 don't touch the other bits. */
526 buf[1] &= ((1 << 3) - 1);
20a6ec49 527 buf[1] |= ((FSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
e750d25e 528 }
20a6ec49 529 memcpy (FSAVE_ADDR (tdep, regs, i), buf, 2);
e750d25e
JT
530 }
531 else
20a6ec49 532 regcache_raw_collect (regcache, i, FSAVE_ADDR (tdep, regs, i));
e750d25e
JT
533 }
534}
535\f
536
537/* At fxsave_offset[REGNUM] you'll find the offset to the location in
538 the data structure used by the "fxsave" instruction where GDB
539 register REGNUM is stored. */
540
541static int fxsave_offset[] =
542{
5716833c 543 32, /* %st(0) through ... */
e750d25e
JT
544 48,
545 64,
546 80,
547 96,
548 112,
549 128,
5716833c
MK
550 144, /* ... %st(7) (80 bits each). */
551 0, /* `fctrl' (16 bits). */
552 2, /* `fstat' (16 bits). */
553 4, /* `ftag' (16 bits). */
554 12, /* `fiseg' (16 bits). */
555 8, /* `fioff'. */
556 20, /* `foseg' (16 bits). */
557 16, /* `fooff'. */
558 6, /* `fop' (bottom 11 bits). */
559 160 + 0 * 16, /* %xmm0 through ... */
04c8243f
MK
560 160 + 1 * 16,
561 160 + 2 * 16,
562 160 + 3 * 16,
563 160 + 4 * 16,
564 160 + 5 * 16,
565 160 + 6 * 16,
566 160 + 7 * 16,
567 160 + 8 * 16,
568 160 + 9 * 16,
569 160 + 10 * 16,
570 160 + 11 * 16,
571 160 + 12 * 16,
572 160 + 13 * 16,
573 160 + 14 * 16,
5716833c 574 160 + 15 * 16, /* ... %xmm15 (128 bits each). */
e750d25e
JT
575};
576
20a6ec49
MD
577#define FXSAVE_ADDR(tdep, fxsave, regnum) \
578 (fxsave + fxsave_offset[regnum - I387_ST0_REGNUM (tdep)])
5716833c
MK
579
580/* We made an unfortunate choice in putting %mxcsr after the SSE
581 registers %xmm0-%xmm7 instead of before, since it makes supporting
582 the registers %xmm8-%xmm15 on AMD64 a bit involved. Therefore we
583 don't include the offset for %mxcsr here above. */
584
585#define FXSAVE_MXCSR_ADDR(fxsave) (fxsave + 24)
e750d25e 586
b4ad899f 587static int i387_tag (const gdb_byte *raw);
e750d25e
JT
588\f
589
41d041d6 590/* Fill register REGNUM in REGCACHE with the appropriate
ed504bdf
MK
591 floating-point or SSE register value from *FXSAVE. This function
592 masks off any of the reserved bits in *FXSAVE. */
e750d25e
JT
593
594void
41d041d6 595i387_supply_fxsave (struct regcache *regcache, int regnum, const void *fxsave)
e750d25e 596{
41d041d6 597 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
b4ad899f 598 const gdb_byte *regs = fxsave;
5716833c
MK
599 int i;
600
601 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
602 gdb_assert (tdep->num_xmm_regs > 0);
dff95cc7 603
20a6ec49 604 for (i = I387_ST0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
ed504bdf
MK
605 if (regnum == -1 || regnum == i)
606 {
5716833c 607 if (regs == NULL)
ed504bdf 608 {
5716833c 609 regcache_raw_supply (regcache, i, NULL);
ed504bdf
MK
610 continue;
611 }
932bb524 612
ed504bdf
MK
613 /* Most of the FPU control registers occupy only 16 bits in
614 the fxsave area. Give those a special treatment. */
20a6ec49
MD
615 if (i >= I387_FCTRL_REGNUM (tdep) && i < I387_XMM0_REGNUM (tdep)
616 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
ed504bdf 617 {
b4ad899f 618 gdb_byte val[4];
ed504bdf 619
20a6ec49 620 memcpy (val, FXSAVE_ADDR (tdep, regs, i), 2);
ed504bdf 621 val[2] = val[3] = 0;
20a6ec49 622 if (i == I387_FOP_REGNUM (tdep))
ed504bdf 623 val[1] &= ((1 << 3) - 1);
20a6ec49 624 else if (i== I387_FTAG_REGNUM (tdep))
ed504bdf
MK
625 {
626 /* The fxsave area contains a simplified version of
627 the tag word. We have to look at the actual 80-bit
628 FP data to recreate the traditional i387 tag word. */
629
630 unsigned long ftag = 0;
631 int fpreg;
632 int top;
633
20a6ec49
MD
634 top = ((FXSAVE_ADDR (tdep, regs,
635 I387_FSTAT_REGNUM (tdep)))[1] >> 3);
5716833c 636 top &= 0x7;
ed504bdf
MK
637
638 for (fpreg = 7; fpreg >= 0; fpreg--)
639 {
640 int tag;
641
642 if (val[0] & (1 << fpreg))
643 {
6d5e094a
MS
644 int thisreg = (fpreg + 8 - top) % 8
645 + I387_ST0_REGNUM (tdep);
646 tag = i387_tag (FXSAVE_ADDR (tdep, regs, thisreg));
ed504bdf
MK
647 }
648 else
649 tag = 3; /* Empty */
650
651 ftag |= tag << (2 * fpreg);
652 }
653 val[0] = ftag & 0xff;
654 val[1] = (ftag >> 8) & 0xff;
655 }
5716833c 656 regcache_raw_supply (regcache, i, val);
ed504bdf
MK
657 }
658 else
20a6ec49 659 regcache_raw_supply (regcache, i, FXSAVE_ADDR (tdep, regs, i));
ed504bdf 660 }
5716833c 661
20a6ec49 662 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
5716833c
MK
663 {
664 if (regs == NULL)
20a6ec49 665 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep), NULL);
5716833c 666 else
20a6ec49 667 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep),
5716833c
MK
668 FXSAVE_MXCSR_ADDR (regs));
669 }
e750d25e
JT
670}
671
672/* Fill register REGNUM (if it is a floating-point or SSE register) in
80571bff
MK
673 *FXSAVE with the value from REGCACHE. If REGNUM is -1, do this for
674 all registers. This function doesn't touch any of the reserved
675 bits in *FXSAVE. */
e750d25e
JT
676
677void
80571bff 678i387_collect_fxsave (const struct regcache *regcache, int regnum, void *fxsave)
e750d25e 679{
e071d1f6 680 struct gdbarch_tdep *tdep = gdbarch_tdep (get_regcache_arch (regcache));
b4ad899f 681 gdb_byte *regs = fxsave;
5716833c
MK
682 int i;
683
684 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
685 gdb_assert (tdep->num_xmm_regs > 0);
dff95cc7 686
20a6ec49 687 for (i = I387_ST0_REGNUM (tdep); i < I387_MXCSR_REGNUM (tdep); i++)
e750d25e
JT
688 if (regnum == -1 || regnum == i)
689 {
690 /* Most of the FPU control registers occupy only 16 bits in
691 the fxsave area. Give those a special treatment. */
20a6ec49
MD
692 if (i >= I387_FCTRL_REGNUM (tdep) && i < I387_XMM0_REGNUM (tdep)
693 && i != I387_FIOFF_REGNUM (tdep) && i != I387_FOOFF_REGNUM (tdep))
e750d25e 694 {
b4ad899f 695 gdb_byte buf[4];
e750d25e 696
5716833c 697 regcache_raw_collect (regcache, i, buf);
e750d25e 698
31aeac78
L
699 if (i == I387_FOP_REGNUM (tdep))
700 {
701 /* The opcode occupies only 11 bits. Make sure we
702 don't touch the other bits. */
703 buf[1] &= ((1 << 3) - 1);
704 buf[1] |= ((FXSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
705 }
706 else if (i == I387_FTAG_REGNUM (tdep))
707 {
708 /* Converting back is much easier. */
709
710 unsigned short ftag;
711 int fpreg;
712
713 ftag = (buf[1] << 8) | buf[0];
714 buf[0] = 0;
715 buf[1] = 0;
716
717 for (fpreg = 7; fpreg >= 0; fpreg--)
718 {
719 int tag = (ftag >> (fpreg * 2)) & 3;
720
721 if (tag != 3)
722 buf[0] |= (1 << fpreg);
723 }
724 }
725 memcpy (FXSAVE_ADDR (tdep, regs, i), buf, 2);
726 }
727 else
728 regcache_raw_collect (regcache, i, FXSAVE_ADDR (tdep, regs, i));
729 }
730
731 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
732 regcache_raw_collect (regcache, I387_MXCSR_REGNUM (tdep),
733 FXSAVE_MXCSR_ADDR (regs));
734}
735
736/* `xstate_bv' is at byte offset 512. */
737#define XSAVE_XSTATE_BV_ADDR(xsave) (xsave + 512)
738
739/* At xsave_avxh_offset[REGNUM] you'll find the offset to the location in
740 the upper 128bit of AVX register data structure used by the "xsave"
741 instruction where GDB register REGNUM is stored. */
742
743static int xsave_avxh_offset[] =
744{
745 576 + 0 * 16, /* Upper 128bit of %ymm0 through ... */
746 576 + 1 * 16,
747 576 + 2 * 16,
748 576 + 3 * 16,
749 576 + 4 * 16,
750 576 + 5 * 16,
751 576 + 6 * 16,
752 576 + 7 * 16,
753 576 + 8 * 16,
754 576 + 9 * 16,
755 576 + 10 * 16,
756 576 + 11 * 16,
757 576 + 12 * 16,
758 576 + 13 * 16,
759 576 + 14 * 16,
760 576 + 15 * 16 /* Upper 128bit of ... %ymm15 (128 bits each). */
761};
762
01f9f808
MS
763#define XSAVE_AVXH_ADDR(tdep, xsave, regnum) \
764 (xsave + xsave_avxh_offset[regnum - I387_YMM0H_REGNUM (tdep)])
765
766/* At xsave_ymm_avx512_offset[REGNUM] you'll find the offset to the location in
767 the upper 128bit of ZMM register data structure used by the "xsave"
768 instruction where GDB register REGNUM is stored. */
769
770static int xsave_ymm_avx512_offset[] =
771{
772 /* HI16_ZMM_area + 16 bytes + regnum* 64 bytes. */
773 1664 + 16 + 0 * 64, /* %ymm16 through... */
774 1664 + 16 + 1 * 64,
775 1664 + 16 + 2 * 64,
776 1664 + 16 + 3 * 64,
777 1664 + 16 + 4 * 64,
778 1664 + 16 + 5 * 64,
779 1664 + 16 + 6 * 64,
780 1664 + 16 + 7 * 64,
781 1664 + 16 + 8 * 64,
782 1664 + 16 + 9 * 64,
783 1664 + 16 + 10 * 64,
784 1664 + 16 + 11 * 64,
785 1664 + 16 + 12 * 64,
786 1664 + 16 + 13 * 64,
787 1664 + 16 + 14 * 64,
788 1664 + 16 + 15 * 64 /* ... %ymm31 (128 bits each). */
789};
790
791#define XSAVE_YMM_AVX512_ADDR(tdep, xsave, regnum) \
792 (xsave + xsave_ymm_avx512_offset[regnum - I387_YMM16H_REGNUM (tdep)])
793
794static int xsave_xmm_avx512_offset[] =
1dbcd68c 795{
01f9f808
MS
796 1664 + 0 * 64, /* %ymm16 through... */
797 1664 + 1 * 64,
798 1664 + 2 * 64,
799 1664 + 3 * 64,
800 1664 + 4 * 64,
801 1664 + 5 * 64,
802 1664 + 6 * 64,
803 1664 + 7 * 64,
804 1664 + 8 * 64,
805 1664 + 9 * 64,
806 1664 + 10 * 64,
807 1664 + 11 * 64,
808 1664 + 12 * 64,
809 1664 + 13 * 64,
810 1664 + 14 * 64,
811 1664 + 15 * 64 /* ... %ymm31 (128 bits each). */
812};
813
814#define XSAVE_XMM_AVX512_ADDR(tdep, xsave, regnum) \
815 (xsave + xsave_xmm_avx512_offset[regnum - I387_XMM16_REGNUM (tdep)])
816
817static int xsave_mpx_offset[] = {
1dbcd68c
WT
818 960 + 0 * 16, /* bnd0r...bnd3r registers. */
819 960 + 1 * 16,
820 960 + 2 * 16,
821 960 + 3 * 16,
822 1024 + 0 * 8, /* bndcfg ... bndstatus. */
823 1024 + 1 * 8,
824};
825
1dbcd68c
WT
826#define XSAVE_MPX_ADDR(tdep, xsave, regnum) \
827 (xsave + xsave_mpx_offset[regnum - I387_BND0R_REGNUM (tdep)])
828
01f9f808
MS
829 /* At xsave_avx512__h_offset[REGNUM] you find the offset to the location
830 of the AVX512 opmask register data structure used by the "xsave"
831 instruction where GDB register REGNUM is stored. */
832
833static int xsave_avx512_k_offset[] =
834{
835 1088 + 0 * 8, /* %k0 through... */
836 1088 + 1 * 8,
837 1088 + 2 * 8,
838 1088 + 3 * 8,
839 1088 + 4 * 8,
840 1088 + 5 * 8,
841 1088 + 6 * 8,
842 1088 + 7 * 8 /* %k7 (64 bits each). */
843};
844
845#define XSAVE_AVX512_K_ADDR(tdep, xsave, regnum) \
846 (xsave + xsave_avx512_k_offset[regnum - I387_K0_REGNUM (tdep)])
847
848/* At xsave_avx512_zmm_h_offset[REGNUM] you find the offset to the location in
849 the upper 256bit of AVX512 ZMMH register data structure used by the "xsave"
850 instruction where GDB register REGNUM is stored. */
851
852static int xsave_avx512_zmm_h_offset[] =
853{
854 1152 + 0 * 32,
855 1152 + 1 * 32, /* Upper 256bit of %zmmh0 through... */
856 1152 + 2 * 32,
857 1152 + 3 * 32,
858 1152 + 4 * 32,
859 1152 + 5 * 32,
860 1152 + 6 * 32,
861 1152 + 7 * 32,
862 1152 + 8 * 32,
863 1152 + 9 * 32,
864 1152 + 10 * 32,
865 1152 + 11 * 32,
866 1152 + 12 * 32,
867 1152 + 13 * 32,
868 1152 + 14 * 32,
869 1152 + 15 * 32, /* Upper 256bit of... %zmmh15 (256 bits each). */
870 1664 + 32 + 0 * 64, /* Upper 256bit of... %zmmh16 (256 bits each). */
871 1664 + 32 + 1 * 64,
872 1664 + 32 + 2 * 64,
873 1664 + 32 + 3 * 64,
874 1664 + 32 + 4 * 64,
875 1664 + 32 + 5 * 64,
876 1664 + 32 + 6 * 64,
877 1664 + 32 + 7 * 64,
878 1664 + 32 + 8 * 64,
879 1664 + 32 + 9 * 64,
880 1664 + 32 + 10 * 64,
881 1664 + 32 + 11 * 64,
882 1664 + 32 + 12 * 64,
883 1664 + 32 + 13 * 64,
884 1664 + 32 + 14 * 64,
885 1664 + 32 + 15 * 64 /* Upper 256bit of... %zmmh31 (256 bits each). */
886};
887
888#define XSAVE_AVX512_ZMM_H_ADDR(tdep, xsave, regnum) \
889 (xsave + xsave_avx512_zmm_h_offset[regnum - I387_ZMM0H_REGNUM (tdep)])
890
31aeac78
L
891/* Similar to i387_supply_fxsave, but use XSAVE extended state. */
892
893void
894i387_supply_xsave (struct regcache *regcache, int regnum,
895 const void *xsave)
896{
01f9f808
MS
897 struct gdbarch *gdbarch = get_regcache_arch (regcache);
898 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
31aeac78
L
899 const gdb_byte *regs = xsave;
900 int i;
901 unsigned int clear_bv;
b4d36fb8 902 static const gdb_byte zero[MAX_REGISTER_SIZE] = { 0 };
31aeac78
L
903 enum
904 {
905 none = 0x0,
906 x87 = 0x1,
907 sse = 0x2,
908 avxh = 0x4,
1dbcd68c 909 mpx = 0x8,
01f9f808
MS
910 avx512_k = 0x10,
911 avx512_zmm_h = 0x20,
912 avx512_ymmh_avx512 = 0x40,
913 avx512_xmm_avx512 = 0x80,
914 all = x87 | sse | avxh | mpx | avx512_k | avx512_zmm_h
915 | avx512_ymmh_avx512 | avx512_xmm_avx512
31aeac78
L
916 } regclass;
917
275418ae 918 gdb_assert (regs != NULL);
31aeac78
L
919 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
920 gdb_assert (tdep->num_xmm_regs > 0);
921
922 if (regnum == -1)
923 regclass = all;
01f9f808
MS
924 else if (regnum >= I387_ZMM0H_REGNUM (tdep)
925 && regnum < I387_ZMMENDH_REGNUM (tdep))
926 regclass = avx512_zmm_h;
927 else if (regnum >= I387_K0_REGNUM (tdep)
928 && regnum < I387_KEND_REGNUM (tdep))
929 regclass = avx512_k;
930 else if (regnum >= I387_YMM16H_REGNUM (tdep)
931 && regnum < I387_YMMH_AVX512_END_REGNUM (tdep))
932 regclass = avx512_ymmh_avx512;
933 else if (regnum >= I387_XMM16_REGNUM (tdep)
934 && regnum < I387_XMM_AVX512_END_REGNUM (tdep))
935 regclass = avx512_xmm_avx512;
31aeac78
L
936 else if (regnum >= I387_YMM0H_REGNUM (tdep)
937 && regnum < I387_YMMENDH_REGNUM (tdep))
938 regclass = avxh;
1dbcd68c
WT
939 else if (regnum >= I387_BND0R_REGNUM (tdep)
940 && regnum < I387_MPXEND_REGNUM (tdep))
941 regclass = mpx;
01f9f808 942 else if (regnum >= I387_XMM0_REGNUM (tdep)
31aeac78
L
943 && regnum < I387_MXCSR_REGNUM (tdep))
944 regclass = sse;
945 else if (regnum >= I387_ST0_REGNUM (tdep)
946 && regnum < I387_FCTRL_REGNUM (tdep))
947 regclass = x87;
948 else
949 regclass = none;
950
275418ae 951 if (regclass != none)
31aeac78
L
952 {
953 /* Get `xstat_bv'. */
954 const gdb_byte *xstate_bv_p = XSAVE_XSTATE_BV_ADDR (regs);
955
956 /* The supported bits in `xstat_bv' are 1 byte. Clear part in
957 vector registers if its bit in xstat_bv is zero. */
958 clear_bv = (~(*xstate_bv_p)) & tdep->xcr0;
959 }
960 else
df7e5265 961 clear_bv = X86_XSTATE_ALL_MASK;
31aeac78 962
b4d36fb8
PA
963 /* With the delayed xsave mechanism, in between the program
964 starting, and the program accessing the vector registers for the
965 first time, the register's values are invalid. The kernel
966 initializes register states to zero when they are set the first
967 time in a program. This means that from the user-space programs'
968 perspective, it's the same as if the registers have always been
969 zero from the start of the program. Therefore, the debugger
275418ae 970 should provide the same illusion to the user. */
b4d36fb8 971
31aeac78
L
972 switch (regclass)
973 {
974 case none:
975 break;
976
01f9f808 977 case avx512_zmm_h:
df7e5265 978 if ((clear_bv & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
01f9f808
MS
979 regcache_raw_supply (regcache, regnum, zero);
980 else
981 regcache_raw_supply (regcache, regnum,
982 XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, regnum));
983 return;
984
985 case avx512_k:
df7e5265 986 if ((clear_bv & X86_XSTATE_K))
01f9f808
MS
987 regcache_raw_supply (regcache, regnum, zero);
988 else
989 regcache_raw_supply (regcache, regnum,
990 XSAVE_AVX512_K_ADDR (tdep, regs, regnum));
991 return;
992
993 case avx512_ymmh_avx512:
df7e5265 994 if ((clear_bv & X86_XSTATE_ZMM))
01f9f808
MS
995 regcache_raw_supply (regcache, regnum, zero);
996 else
997 regcache_raw_supply (regcache, regnum,
998 XSAVE_YMM_AVX512_ADDR (tdep, regs, regnum));
999 return;
1000
1001 case avx512_xmm_avx512:
df7e5265 1002 if ((clear_bv & X86_XSTATE_ZMM))
01f9f808
MS
1003 regcache_raw_supply (regcache, regnum, zero);
1004 else
1005 regcache_raw_supply (regcache, regnum,
1006 XSAVE_XMM_AVX512_ADDR (tdep, regs, regnum));
1007 return;
1008
31aeac78 1009 case avxh:
df7e5265 1010 if ((clear_bv & X86_XSTATE_AVX))
275418ae 1011 regcache_raw_supply (regcache, regnum, zero);
31aeac78 1012 else
b4d36fb8
PA
1013 regcache_raw_supply (regcache, regnum,
1014 XSAVE_AVXH_ADDR (tdep, regs, regnum));
31aeac78
L
1015 return;
1016
1dbcd68c 1017 case mpx:
df7e5265 1018 if ((clear_bv & X86_XSTATE_BNDREGS))
1dbcd68c
WT
1019 regcache_raw_supply (regcache, regnum, zero);
1020 else
1021 regcache_raw_supply (regcache, regnum,
1022 XSAVE_MPX_ADDR (tdep, regs, regnum));
1023 return;
1024
31aeac78 1025 case sse:
df7e5265 1026 if ((clear_bv & X86_XSTATE_SSE))
275418ae 1027 regcache_raw_supply (regcache, regnum, zero);
31aeac78 1028 else
b4d36fb8
PA
1029 regcache_raw_supply (regcache, regnum,
1030 FXSAVE_ADDR (tdep, regs, regnum));
31aeac78
L
1031 return;
1032
1033 case x87:
df7e5265 1034 if ((clear_bv & X86_XSTATE_X87))
275418ae 1035 regcache_raw_supply (regcache, regnum, zero);
31aeac78 1036 else
b4d36fb8
PA
1037 regcache_raw_supply (regcache, regnum,
1038 FXSAVE_ADDR (tdep, regs, regnum));
31aeac78
L
1039 return;
1040
1041 case all:
01f9f808 1042 /* Handle the upper ZMM registers. */
df7e5265 1043 if ((tdep->xcr0 & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
01f9f808 1044 {
df7e5265 1045 if ((clear_bv & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
01f9f808
MS
1046 {
1047 for (i = I387_ZMM0H_REGNUM (tdep);
1048 i < I387_ZMMENDH_REGNUM (tdep);
1049 i++)
1050 regcache_raw_supply (regcache, i, zero);
1051 }
1052 else
1053 {
1054 for (i = I387_ZMM0H_REGNUM (tdep);
1055 i < I387_ZMMENDH_REGNUM (tdep);
1056 i++)
1057 regcache_raw_supply (regcache, i,
1058 XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, i));
1059 }
1060 }
1061
1062 /* Handle AVX512 OpMask registers. */
df7e5265 1063 if ((tdep->xcr0 & X86_XSTATE_K))
01f9f808 1064 {
df7e5265 1065 if ((clear_bv & X86_XSTATE_K))
01f9f808
MS
1066 {
1067 for (i = I387_K0_REGNUM (tdep);
1068 i < I387_KEND_REGNUM (tdep);
1069 i++)
1070 regcache_raw_supply (regcache, i, zero);
1071 }
1072 else
1073 {
1074 for (i = I387_K0_REGNUM (tdep);
1075 i < I387_KEND_REGNUM (tdep);
1076 i++)
1077 regcache_raw_supply (regcache, i,
1078 XSAVE_AVX512_K_ADDR (tdep, regs, i));
1079 }
1080 }
1081
1082 /* Handle the YMM_AVX512 registers. */
df7e5265 1083 if ((tdep->xcr0 & X86_XSTATE_ZMM))
01f9f808 1084 {
df7e5265 1085 if ((clear_bv & X86_XSTATE_ZMM))
01f9f808
MS
1086 {
1087 for (i = I387_YMM16H_REGNUM (tdep);
1088 i < I387_YMMH_AVX512_END_REGNUM (tdep);
1089 i++)
1090 regcache_raw_supply (regcache, i, zero);
1091 for (i = I387_XMM16_REGNUM (tdep);
1092 i < I387_XMM_AVX512_END_REGNUM (tdep);
1093 i++)
1094 regcache_raw_supply (regcache, i, zero);
1095 }
1096 else
1097 {
1098 for (i = I387_YMM16H_REGNUM (tdep);
1099 i < I387_YMMH_AVX512_END_REGNUM (tdep);
1100 i++)
1101 regcache_raw_supply (regcache, i,
1102 XSAVE_YMM_AVX512_ADDR (tdep, regs, i));
1103 for (i = I387_XMM16_REGNUM (tdep);
1104 i < I387_XMM_AVX512_END_REGNUM (tdep);
1105 i++)
1106 regcache_raw_supply (regcache, i,
1107 XSAVE_XMM_AVX512_ADDR (tdep, regs, i));
1108 }
1109 }
86d31898 1110 /* Handle the upper YMM registers. */
df7e5265 1111 if ((tdep->xcr0 & X86_XSTATE_AVX))
31aeac78 1112 {
df7e5265 1113 if ((clear_bv & X86_XSTATE_AVX))
b4d36fb8
PA
1114 {
1115 for (i = I387_YMM0H_REGNUM (tdep);
1116 i < I387_YMMENDH_REGNUM (tdep);
1117 i++)
275418ae 1118 regcache_raw_supply (regcache, i, zero);
b4d36fb8 1119 }
31aeac78 1120 else
31aeac78 1121 {
b4d36fb8
PA
1122 for (i = I387_YMM0H_REGNUM (tdep);
1123 i < I387_YMMENDH_REGNUM (tdep);
1124 i++)
1125 regcache_raw_supply (regcache, i,
1126 XSAVE_AVXH_ADDR (tdep, regs, i));
31aeac78
L
1127 }
1128 }
1129
1dbcd68c 1130 /* Handle the MPX registers. */
df7e5265 1131 if ((tdep->xcr0 & X86_XSTATE_BNDREGS))
1dbcd68c 1132 {
df7e5265 1133 if (clear_bv & X86_XSTATE_BNDREGS)
1dbcd68c
WT
1134 {
1135 for (i = I387_BND0R_REGNUM (tdep);
1136 i < I387_BNDCFGU_REGNUM (tdep); i++)
1137 regcache_raw_supply (regcache, i, zero);
1138 }
1139 else
1140 {
1141 for (i = I387_BND0R_REGNUM (tdep);
1142 i < I387_BNDCFGU_REGNUM (tdep); i++)
1143 regcache_raw_supply (regcache, i,
1144 XSAVE_MPX_ADDR (tdep, regs, i));
1145 }
1146 }
1147
1148 /* Handle the MPX registers. */
df7e5265 1149 if ((tdep->xcr0 & X86_XSTATE_BNDCFG))
1dbcd68c 1150 {
df7e5265 1151 if (clear_bv & X86_XSTATE_BNDCFG)
1dbcd68c
WT
1152 {
1153 for (i = I387_BNDCFGU_REGNUM (tdep);
1154 i < I387_MPXEND_REGNUM (tdep); i++)
1155 regcache_raw_supply (regcache, i, zero);
1156 }
1157 else
1158 {
1159 for (i = I387_BNDCFGU_REGNUM (tdep);
1160 i < I387_MPXEND_REGNUM (tdep); i++)
1161 regcache_raw_supply (regcache, i,
1162 XSAVE_MPX_ADDR (tdep, regs, i));
1163 }
1164 }
1165
31aeac78 1166 /* Handle the XMM registers. */
df7e5265 1167 if ((tdep->xcr0 & X86_XSTATE_SSE))
31aeac78 1168 {
df7e5265 1169 if ((clear_bv & X86_XSTATE_SSE))
b4d36fb8
PA
1170 {
1171 for (i = I387_XMM0_REGNUM (tdep);
1172 i < I387_MXCSR_REGNUM (tdep);
1173 i++)
275418ae 1174 regcache_raw_supply (regcache, i, zero);
b4d36fb8 1175 }
31aeac78 1176 else
31aeac78 1177 {
b4d36fb8
PA
1178 for (i = I387_XMM0_REGNUM (tdep);
1179 i < I387_MXCSR_REGNUM (tdep); i++)
1180 regcache_raw_supply (regcache, i,
1181 FXSAVE_ADDR (tdep, regs, i));
31aeac78
L
1182 }
1183 }
1184
1185 /* Handle the x87 registers. */
df7e5265 1186 if ((tdep->xcr0 & X86_XSTATE_X87))
31aeac78 1187 {
df7e5265 1188 if ((clear_bv & X86_XSTATE_X87))
b4d36fb8
PA
1189 {
1190 for (i = I387_ST0_REGNUM (tdep);
1191 i < I387_FCTRL_REGNUM (tdep);
1192 i++)
275418ae 1193 regcache_raw_supply (regcache, i, zero);
b4d36fb8 1194 }
31aeac78 1195 else
31aeac78 1196 {
b4d36fb8
PA
1197 for (i = I387_ST0_REGNUM (tdep);
1198 i < I387_FCTRL_REGNUM (tdep);
1199 i++)
1200 regcache_raw_supply (regcache, i, FXSAVE_ADDR (tdep, regs, i));
31aeac78
L
1201 }
1202 }
1203 break;
1204 }
1205
1206 /* Only handle x87 control registers. */
1207 for (i = I387_FCTRL_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
1208 if (regnum == -1 || regnum == i)
1209 {
31aeac78
L
1210 /* Most of the FPU control registers occupy only 16 bits in
1211 the xsave extended state. Give those a special treatment. */
1212 if (i != I387_FIOFF_REGNUM (tdep)
1213 && i != I387_FOOFF_REGNUM (tdep))
1214 {
1215 gdb_byte val[4];
1216
1217 memcpy (val, FXSAVE_ADDR (tdep, regs, i), 2);
1218 val[2] = val[3] = 0;
1219 if (i == I387_FOP_REGNUM (tdep))
1220 val[1] &= ((1 << 3) - 1);
1221 else if (i== I387_FTAG_REGNUM (tdep))
1222 {
1223 /* The fxsave area contains a simplified version of
1224 the tag word. We have to look at the actual 80-bit
1225 FP data to recreate the traditional i387 tag word. */
1226
1227 unsigned long ftag = 0;
1228 int fpreg;
1229 int top;
1230
1231 top = ((FXSAVE_ADDR (tdep, regs,
1232 I387_FSTAT_REGNUM (tdep)))[1] >> 3);
1233 top &= 0x7;
1234
1235 for (fpreg = 7; fpreg >= 0; fpreg--)
1236 {
1237 int tag;
1238
1239 if (val[0] & (1 << fpreg))
1240 {
e5b3d7d6 1241 int thisreg = (fpreg + 8 - top) % 8
31aeac78 1242 + I387_ST0_REGNUM (tdep);
e5b3d7d6 1243 tag = i387_tag (FXSAVE_ADDR (tdep, regs, thisreg));
31aeac78
L
1244 }
1245 else
1246 tag = 3; /* Empty */
1247
1248 ftag |= tag << (2 * fpreg);
1249 }
1250 val[0] = ftag & 0xff;
1251 val[1] = (ftag >> 8) & 0xff;
1252 }
1253 regcache_raw_supply (regcache, i, val);
1254 }
1255 else
1256 regcache_raw_supply (regcache, i, FXSAVE_ADDR (tdep, regs, i));
1257 }
1258
1259 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
275418ae
PA
1260 regcache_raw_supply (regcache, I387_MXCSR_REGNUM (tdep),
1261 FXSAVE_MXCSR_ADDR (regs));
31aeac78
L
1262}
1263
1264/* Similar to i387_collect_fxsave, but use XSAVE extended state. */
1265
1266void
1267i387_collect_xsave (const struct regcache *regcache, int regnum,
1268 void *xsave, int gcore)
1269{
01f9f808
MS
1270 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1271 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
31aeac78
L
1272 gdb_byte *regs = xsave;
1273 int i;
1274 enum
1275 {
1276 none = 0x0,
1277 check = 0x1,
1278 x87 = 0x2 | check,
1279 sse = 0x4 | check,
1280 avxh = 0x8 | check,
1dbcd68c 1281 mpx = 0x10 | check,
01f9f808
MS
1282 avx512_k = 0x20 | check,
1283 avx512_zmm_h = 0x40 | check,
1284 avx512_ymmh_avx512 = 0x80 | check,
1285 avx512_xmm_avx512 = 0x100 | check,
1286 all = x87 | sse | avxh | mpx | avx512_k | avx512_zmm_h
1287 | avx512_ymmh_avx512 | avx512_xmm_avx512
31aeac78
L
1288 } regclass;
1289
1290 gdb_assert (tdep->st0_regnum >= I386_ST0_REGNUM);
1291 gdb_assert (tdep->num_xmm_regs > 0);
1292
1293 if (regnum == -1)
1294 regclass = all;
01f9f808
MS
1295 else if (regnum >= I387_ZMM0H_REGNUM (tdep)
1296 && regnum < I387_ZMMENDH_REGNUM (tdep))
1297 regclass = avx512_zmm_h;
1298 else if (regnum >= I387_K0_REGNUM (tdep)
1299 && regnum < I387_KEND_REGNUM (tdep))
1300 regclass = avx512_k;
1301 else if (regnum >= I387_YMM16H_REGNUM (tdep)
1302 && regnum < I387_YMMH_AVX512_END_REGNUM (tdep))
1303 regclass = avx512_ymmh_avx512;
1304 else if (regnum >= I387_XMM16_REGNUM (tdep)
1305 && regnum < I387_XMM_AVX512_END_REGNUM (tdep))
1306 regclass = avx512_xmm_avx512;
31aeac78
L
1307 else if (regnum >= I387_YMM0H_REGNUM (tdep)
1308 && regnum < I387_YMMENDH_REGNUM (tdep))
1309 regclass = avxh;
1dbcd68c
WT
1310 else if (regnum >= I387_BND0R_REGNUM (tdep)
1311 && regnum < I387_MPXEND_REGNUM (tdep))
1312 regclass = mpx;
1313 else if (regnum >= I387_XMM0_REGNUM (tdep)
31aeac78
L
1314 && regnum < I387_MXCSR_REGNUM (tdep))
1315 regclass = sse;
1316 else if (regnum >= I387_ST0_REGNUM (tdep)
1317 && regnum < I387_FCTRL_REGNUM (tdep))
1318 regclass = x87;
1319 else
1320 regclass = none;
1321
1322 if (gcore)
1323 {
1324 /* Clear XSAVE extended state. */
df7e5265 1325 memset (regs, 0, X86_XSTATE_SIZE (tdep->xcr0));
31aeac78
L
1326
1327 /* Update XCR0 and `xstate_bv' with XCR0 for gcore. */
1328 if (tdep->xsave_xcr0_offset != -1)
1329 memcpy (regs + tdep->xsave_xcr0_offset, &tdep->xcr0, 8);
1330 memcpy (XSAVE_XSTATE_BV_ADDR (regs), &tdep->xcr0, 8);
1331 }
1332
1333 if ((regclass & check))
1334 {
1335 gdb_byte raw[I386_MAX_REGISTER_SIZE];
1336 gdb_byte *xstate_bv_p = XSAVE_XSTATE_BV_ADDR (regs);
1337 unsigned int xstate_bv = 0;
1777feb0 1338 /* The supported bits in `xstat_bv' are 1 byte. */
31aeac78
L
1339 unsigned int clear_bv = (~(*xstate_bv_p)) & tdep->xcr0;
1340 gdb_byte *p;
1341
1342 /* Clear register set if its bit in xstat_bv is zero. */
1343 if (clear_bv)
1344 {
df7e5265 1345 if ((clear_bv & X86_XSTATE_BNDREGS))
1dbcd68c
WT
1346 for (i = I387_BND0R_REGNUM (tdep);
1347 i < I387_BNDCFGU_REGNUM (tdep); i++)
1348 memset (XSAVE_MPX_ADDR (tdep, regs, i), 0, 16);
1349
df7e5265 1350 if ((clear_bv & X86_XSTATE_BNDCFG))
1dbcd68c
WT
1351 for (i = I387_BNDCFGU_REGNUM (tdep);
1352 i < I387_MPXEND_REGNUM (tdep); i++)
1353 memset (XSAVE_MPX_ADDR (tdep, regs, i), 0, 8);
1354
df7e5265 1355 if ((clear_bv & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
01f9f808
MS
1356 for (i = I387_ZMM0H_REGNUM (tdep);
1357 i < I387_ZMMENDH_REGNUM (tdep); i++)
1358 memset (XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, i), 0, 32);
1359
df7e5265 1360 if ((clear_bv & X86_XSTATE_K))
01f9f808
MS
1361 for (i = I387_K0_REGNUM (tdep);
1362 i < I387_KEND_REGNUM (tdep); i++)
1363 memset (XSAVE_AVX512_K_ADDR (tdep, regs, i), 0, 8);
1364
df7e5265 1365 if ((clear_bv & X86_XSTATE_ZMM))
01f9f808
MS
1366 {
1367 for (i = I387_YMM16H_REGNUM (tdep);
1368 i < I387_YMMH_AVX512_END_REGNUM (tdep); i++)
1369 memset (XSAVE_YMM_AVX512_ADDR (tdep, regs, i), 0, 16);
1370 for (i = I387_XMM16_REGNUM (tdep);
1371 i < I387_XMM_AVX512_END_REGNUM (tdep); i++)
1372 memset (XSAVE_XMM_AVX512_ADDR (tdep, regs, i), 0, 16);
1373 }
1374
df7e5265 1375 if ((clear_bv & X86_XSTATE_AVX))
31aeac78
L
1376 for (i = I387_YMM0H_REGNUM (tdep);
1377 i < I387_YMMENDH_REGNUM (tdep); i++)
1378 memset (XSAVE_AVXH_ADDR (tdep, regs, i), 0, 16);
1379
df7e5265 1380 if ((clear_bv & X86_XSTATE_SSE))
31aeac78
L
1381 for (i = I387_XMM0_REGNUM (tdep);
1382 i < I387_MXCSR_REGNUM (tdep); i++)
1383 memset (FXSAVE_ADDR (tdep, regs, i), 0, 16);
1384
df7e5265 1385 if ((clear_bv & X86_XSTATE_X87))
31aeac78
L
1386 for (i = I387_ST0_REGNUM (tdep);
1387 i < I387_FCTRL_REGNUM (tdep); i++)
1388 memset (FXSAVE_ADDR (tdep, regs, i), 0, 10);
1389 }
1390
1391 if (regclass == all)
1392 {
01f9f808 1393 /* Check if any ZMMH registers are changed. */
df7e5265 1394 if ((tdep->xcr0 & (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM)))
01f9f808
MS
1395 for (i = I387_ZMM0H_REGNUM (tdep);
1396 i < I387_ZMMENDH_REGNUM (tdep); i++)
1397 {
1398 regcache_raw_collect (regcache, i, raw);
1399 p = XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, i);
1400 if (memcmp (raw, p, 32) != 0)
1401 {
df7e5265 1402 xstate_bv |= (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM);
01f9f808
MS
1403 memcpy (p, raw, 32);
1404 }
1405 }
1406
1407 /* Check if any K registers are changed. */
df7e5265 1408 if ((tdep->xcr0 & X86_XSTATE_K))
01f9f808
MS
1409 for (i = I387_K0_REGNUM (tdep);
1410 i < I387_KEND_REGNUM (tdep); i++)
1411 {
1412 regcache_raw_collect (regcache, i, raw);
1413 p = XSAVE_AVX512_K_ADDR (tdep, regs, i);
1414 if (memcmp (raw, p, 8) != 0)
1415 {
df7e5265 1416 xstate_bv |= X86_XSTATE_K;
01f9f808
MS
1417 memcpy (p, raw, 8);
1418 }
1419 }
1420
1421 /* Check if any XMM or upper YMM registers are changed. */
df7e5265 1422 if ((tdep->xcr0 & X86_XSTATE_ZMM))
01f9f808
MS
1423 {
1424 for (i = I387_YMM16H_REGNUM (tdep);
1425 i < I387_YMMH_AVX512_END_REGNUM (tdep); i++)
1426 {
1427 regcache_raw_collect (regcache, i, raw);
1428 p = XSAVE_YMM_AVX512_ADDR (tdep, regs, i);
1429 if (memcmp (raw, p, 16) != 0)
1430 {
df7e5265 1431 xstate_bv |= X86_XSTATE_ZMM;
01f9f808
MS
1432 memcpy (p, raw, 16);
1433 }
1434 }
1435 for (i = I387_XMM16_REGNUM (tdep);
1436 i < I387_XMM_AVX512_END_REGNUM (tdep); i++)
1437 {
1438 regcache_raw_collect (regcache, i, raw);
1439 p = XSAVE_XMM_AVX512_ADDR (tdep, regs, i);
1440 if (memcmp (raw, p, 16) != 0)
1441 {
df7e5265 1442 xstate_bv |= X86_XSTATE_ZMM;
01f9f808
MS
1443 memcpy (p, raw, 16);
1444 }
1445 }
1446 }
1447
31aeac78 1448 /* Check if any upper YMM registers are changed. */
df7e5265 1449 if ((tdep->xcr0 & X86_XSTATE_AVX))
31aeac78
L
1450 for (i = I387_YMM0H_REGNUM (tdep);
1451 i < I387_YMMENDH_REGNUM (tdep); i++)
1452 {
1453 regcache_raw_collect (regcache, i, raw);
1454 p = XSAVE_AVXH_ADDR (tdep, regs, i);
1455 if (memcmp (raw, p, 16))
1456 {
df7e5265 1457 xstate_bv |= X86_XSTATE_AVX;
31aeac78
L
1458 memcpy (p, raw, 16);
1459 }
1460 }
1dbcd68c 1461 /* Check if any upper MPX registers are changed. */
df7e5265 1462 if ((tdep->xcr0 & X86_XSTATE_BNDREGS))
1dbcd68c
WT
1463 for (i = I387_BND0R_REGNUM (tdep);
1464 i < I387_BNDCFGU_REGNUM (tdep); i++)
1465 {
1466 regcache_raw_collect (regcache, i, raw);
1467 p = XSAVE_MPX_ADDR (tdep, regs, i);
1468 if (memcmp (raw, p, 16))
1469 {
df7e5265 1470 xstate_bv |= X86_XSTATE_BNDREGS;
1dbcd68c
WT
1471 memcpy (p, raw, 16);
1472 }
1473 }
1474
1475 /* Check if any upper MPX registers are changed. */
df7e5265 1476 if ((tdep->xcr0 & X86_XSTATE_BNDCFG))
1dbcd68c
WT
1477 for (i = I387_BNDCFGU_REGNUM (tdep);
1478 i < I387_MPXEND_REGNUM (tdep); i++)
1479 {
1480 regcache_raw_collect (regcache, i, raw);
1481 p = XSAVE_MPX_ADDR (tdep, regs, i);
1482 if (memcmp (raw, p, 8))
1483 {
df7e5265 1484 xstate_bv |= X86_XSTATE_BNDCFG;
1dbcd68c
WT
1485 memcpy (p, raw, 8);
1486 }
1487 }
31aeac78
L
1488
1489 /* Check if any SSE registers are changed. */
df7e5265 1490 if ((tdep->xcr0 & X86_XSTATE_SSE))
31aeac78
L
1491 for (i = I387_XMM0_REGNUM (tdep);
1492 i < I387_MXCSR_REGNUM (tdep); i++)
1493 {
1494 regcache_raw_collect (regcache, i, raw);
1495 p = FXSAVE_ADDR (tdep, regs, i);
1496 if (memcmp (raw, p, 16))
1497 {
df7e5265 1498 xstate_bv |= X86_XSTATE_SSE;
31aeac78
L
1499 memcpy (p, raw, 16);
1500 }
1501 }
1502
1503 /* Check if any X87 registers are changed. */
df7e5265 1504 if ((tdep->xcr0 & X86_XSTATE_X87))
31aeac78
L
1505 for (i = I387_ST0_REGNUM (tdep);
1506 i < I387_FCTRL_REGNUM (tdep); i++)
1507 {
1508 regcache_raw_collect (regcache, i, raw);
1509 p = FXSAVE_ADDR (tdep, regs, i);
1510 if (memcmp (raw, p, 10))
1511 {
df7e5265 1512 xstate_bv |= X86_XSTATE_X87;
31aeac78
L
1513 memcpy (p, raw, 10);
1514 }
1515 }
1516 }
1517 else
1518 {
1519 /* Check if REGNUM is changed. */
1520 regcache_raw_collect (regcache, regnum, raw);
1521
1522 switch (regclass)
1523 {
1524 default:
4e4d8374
L
1525 internal_error (__FILE__, __LINE__,
1526 _("invalid i387 regclass"));
31aeac78 1527
01f9f808
MS
1528 case avx512_zmm_h:
1529 /* This is a ZMM register. */
1530 p = XSAVE_AVX512_ZMM_H_ADDR (tdep, regs, regnum);
1531 if (memcmp (raw, p, 32) != 0)
1532 {
df7e5265 1533 xstate_bv |= (X86_XSTATE_ZMM_H | X86_XSTATE_ZMM);
01f9f808
MS
1534 memcpy (p, raw, 32);
1535 }
1536 break;
1537 case avx512_k:
1538 /* This is a AVX512 mask register. */
1539 p = XSAVE_AVX512_K_ADDR (tdep, regs, regnum);
1540 if (memcmp (raw, p, 8) != 0)
1541 {
df7e5265 1542 xstate_bv |= X86_XSTATE_K;
01f9f808
MS
1543 memcpy (p, raw, 8);
1544 }
1545 break;
1546
1547 case avx512_ymmh_avx512:
1548 /* This is an upper YMM16-31 register. */
1549 p = XSAVE_YMM_AVX512_ADDR (tdep, regs, regnum);
1550 if (memcmp (raw, p, 16) != 0)
1551 {
df7e5265 1552 xstate_bv |= X86_XSTATE_ZMM;
01f9f808
MS
1553 memcpy (p, raw, 16);
1554 }
1555 break;
1556
1557 case avx512_xmm_avx512:
1558 /* This is an upper XMM16-31 register. */
1559 p = XSAVE_XMM_AVX512_ADDR (tdep, regs, regnum);
1560 if (memcmp (raw, p, 16) != 0)
1561 {
df7e5265 1562 xstate_bv |= X86_XSTATE_ZMM;
01f9f808
MS
1563 memcpy (p, raw, 16);
1564 }
1565 break;
1566
40936b0d
L
1567 case avxh:
1568 /* This is an upper YMM register. */
1569 p = XSAVE_AVXH_ADDR (tdep, regs, regnum);
1570 if (memcmp (raw, p, 16))
31aeac78 1571 {
df7e5265 1572 xstate_bv |= X86_XSTATE_AVX;
40936b0d
L
1573 memcpy (p, raw, 16);
1574 }
1575 break;
31aeac78 1576
1dbcd68c
WT
1577 case mpx:
1578 if (regnum < I387_BNDCFGU_REGNUM (tdep))
1579 {
1580 regcache_raw_collect (regcache, regnum, raw);
1581 p = XSAVE_MPX_ADDR (tdep, regs, regnum);
1582 if (memcmp (raw, p, 16))
1583 {
df7e5265 1584 xstate_bv |= X86_XSTATE_BNDREGS;
1dbcd68c
WT
1585 memcpy (p, raw, 16);
1586 }
1587 }
1588 else
1589 {
1590 p = XSAVE_MPX_ADDR (tdep, regs, regnum);
df7e5265 1591 xstate_bv |= X86_XSTATE_BNDCFG;
1dbcd68c
WT
1592 memcpy (p, raw, 8);
1593 }
1594 break;
1595
40936b0d
L
1596 case sse:
1597 /* This is an SSE register. */
1598 p = FXSAVE_ADDR (tdep, regs, regnum);
1599 if (memcmp (raw, p, 16))
1600 {
df7e5265 1601 xstate_bv |= X86_XSTATE_SSE;
40936b0d
L
1602 memcpy (p, raw, 16);
1603 }
1604 break;
31aeac78 1605
40936b0d
L
1606 case x87:
1607 /* This is an x87 register. */
1608 p = FXSAVE_ADDR (tdep, regs, regnum);
1609 if (memcmp (raw, p, 10))
1610 {
df7e5265 1611 xstate_bv |= X86_XSTATE_X87;
40936b0d 1612 memcpy (p, raw, 10);
31aeac78 1613 }
40936b0d 1614 break;
31aeac78 1615 }
40936b0d
L
1616 }
1617
1618 /* Update the corresponding bits in `xstate_bv' if any SSE/AVX
1619 registers are changed. */
1620 if (xstate_bv)
1621 {
1622 /* The supported bits in `xstat_bv' are 1 byte. */
1623 *xstate_bv_p |= (gdb_byte) xstate_bv;
1624
1625 switch (regclass)
31aeac78 1626 {
40936b0d 1627 default:
4e4d8374
L
1628 internal_error (__FILE__, __LINE__,
1629 _("invalid i387 regclass"));
40936b0d
L
1630
1631 case all:
1632 break;
1633
1634 case x87:
1635 case sse:
1636 case avxh:
1dbcd68c 1637 case mpx:
01f9f808
MS
1638 case avx512_k:
1639 case avx512_zmm_h:
1640 case avx512_ymmh_avx512:
1641 case avx512_xmm_avx512:
40936b0d
L
1642 /* Register REGNUM has been updated. Return. */
1643 return;
31aeac78 1644 }
40936b0d
L
1645 }
1646 else
1647 {
1648 /* Return if REGNUM isn't changed. */
1649 if (regclass != all)
1650 return;
1651 }
31aeac78
L
1652 }
1653
1654 /* Only handle x87 control registers. */
1655 for (i = I387_FCTRL_REGNUM (tdep); i < I387_XMM0_REGNUM (tdep); i++)
1656 if (regnum == -1 || regnum == i)
1657 {
1658 /* Most of the FPU control registers occupy only 16 bits in
1659 the xsave extended state. Give those a special treatment. */
1660 if (i != I387_FIOFF_REGNUM (tdep)
1661 && i != I387_FOOFF_REGNUM (tdep))
1662 {
1663 gdb_byte buf[4];
1664
1665 regcache_raw_collect (regcache, i, buf);
1666
20a6ec49 1667 if (i == I387_FOP_REGNUM (tdep))
e750d25e
JT
1668 {
1669 /* The opcode occupies only 11 bits. Make sure we
40936b0d 1670 don't touch the other bits. */
e750d25e 1671 buf[1] &= ((1 << 3) - 1);
20a6ec49 1672 buf[1] |= ((FXSAVE_ADDR (tdep, regs, i))[1] & ~((1 << 3) - 1));
e750d25e 1673 }
20a6ec49 1674 else if (i == I387_FTAG_REGNUM (tdep))
e750d25e
JT
1675 {
1676 /* Converting back is much easier. */
1677
1678 unsigned short ftag;
1679 int fpreg;
1680
1681 ftag = (buf[1] << 8) | buf[0];
1682 buf[0] = 0;
1683 buf[1] = 0;
1684
1685 for (fpreg = 7; fpreg >= 0; fpreg--)
1686 {
1687 int tag = (ftag >> (fpreg * 2)) & 3;
1688
1689 if (tag != 3)
1690 buf[0] |= (1 << fpreg);
1691 }
1692 }
20a6ec49 1693 memcpy (FXSAVE_ADDR (tdep, regs, i), buf, 2);
e750d25e
JT
1694 }
1695 else
20a6ec49 1696 regcache_raw_collect (regcache, i, FXSAVE_ADDR (tdep, regs, i));
e750d25e 1697 }
5716833c 1698
20a6ec49
MD
1699 if (regnum == I387_MXCSR_REGNUM (tdep) || regnum == -1)
1700 regcache_raw_collect (regcache, I387_MXCSR_REGNUM (tdep),
5716833c 1701 FXSAVE_MXCSR_ADDR (regs));
e750d25e
JT
1702}
1703
1704/* Recreate the FTW (tag word) valid bits from the 80-bit FP data in
1705 *RAW. */
1706
1707static int
b4ad899f 1708i387_tag (const gdb_byte *raw)
e750d25e
JT
1709{
1710 int integer;
1711 unsigned int exponent;
1712 unsigned long fraction[2];
1713
1714 integer = raw[7] & 0x80;
1715 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
1716 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
1717 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
1718 | (raw[5] << 8) | raw[4]);
1719
1720 if (exponent == 0x7fff)
1721 {
1722 /* Special. */
1723 return (2);
1724 }
1725 else if (exponent == 0x0000)
1726 {
1727 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
1728 {
1729 /* Zero. */
1730 return (1);
1731 }
1732 else
1733 {
1734 /* Special. */
1735 return (2);
1736 }
1737 }
1738 else
1739 {
1740 if (integer)
1741 {
1742 /* Valid. */
1743 return (0);
1744 }
1745 else
1746 {
1747 /* Special. */
1748 return (2);
1749 }
1750 }
1751}
efb1c01c
MK
1752
1753/* Prepare the FPU stack in REGCACHE for a function return. */
1754
1755void
1756i387_return_value (struct gdbarch *gdbarch, struct regcache *regcache)
1757{
1758 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1759 ULONGEST fstat;
1760
efb1c01c
MK
1761 /* Set the top of the floating-point register stack to 7. The
1762 actual value doesn't really matter, but 7 is what a normal
1763 function return would end up with if the program started out with
1764 a freshly initialized FPU. */
20a6ec49 1765 regcache_raw_read_unsigned (regcache, I387_FSTAT_REGNUM (tdep), &fstat);
efb1c01c 1766 fstat |= (7 << 11);
20a6ec49 1767 regcache_raw_write_unsigned (regcache, I387_FSTAT_REGNUM (tdep), fstat);
efb1c01c
MK
1768
1769 /* Mark %st(1) through %st(7) as empty. Since we set the top of the
1770 floating-point register stack to 7, the appropriate value for the
1771 tag word is 0x3fff. */
20a6ec49 1772 regcache_raw_write_unsigned (regcache, I387_FTAG_REGNUM (tdep), 0x3fff);
efb1c01c 1773
efb1c01c 1774}
This page took 2.188765 seconds and 4 git commands to generate.