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