Commit | Line | Data |
---|---|---|
58caa3dc | 1 | /* i387-specific utility functions, for the remote server for GDB. |
ecd75fc8 | 2 | Copyright (C) 2000-2014 Free Software Foundation, Inc. |
58caa3dc DJ |
3 | |
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 8 | the Free Software Foundation; either version 3 of the License, or |
58caa3dc DJ |
9 | (at your option) any later version. |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
58caa3dc DJ |
18 | |
19 | #include "server.h" | |
0d62e5e8 | 20 | #include "i387-fp.h" |
df7e5265 | 21 | #include "x86-xstate.h" |
58caa3dc | 22 | |
a196ebeb WT |
23 | static const int num_mpx_bnd_registers = 4; |
24 | static const int num_mpx_cfg_registers = 2; | |
01f9f808 MS |
25 | static const int num_avx512_k_registers = 8; |
26 | static const int num_avx512_zmmh_low_registers = 16; | |
27 | static const int num_avx512_zmmh_high_registers = 16; | |
28 | static const int num_avx512_ymmh_registers = 16; | |
29 | static const int num_avx512_xmm_registers = 16; | |
a196ebeb | 30 | |
58caa3dc DJ |
31 | /* Note: These functions preserve the reserved bits in control registers. |
32 | However, gdbserver promptly throws away that information. */ | |
33 | ||
34 | /* These structs should have the proper sizes and alignment on both | |
35 | i386 and x86-64 machines. */ | |
36 | ||
37 | struct i387_fsave { | |
38 | /* All these are only sixteen bits, plus padding, except for fop (which | |
39 | is only eleven bits), and fooff / fioff (which are 32 bits each). */ | |
0c2ead7e DJ |
40 | unsigned short fctrl; |
41 | unsigned short pad1; | |
42 | unsigned short fstat; | |
43 | unsigned short pad2; | |
44 | unsigned short ftag; | |
45 | unsigned short pad3; | |
58caa3dc DJ |
46 | unsigned int fioff; |
47 | unsigned short fiseg; | |
48 | unsigned short fop; | |
49 | unsigned int fooff; | |
0c2ead7e DJ |
50 | unsigned short foseg; |
51 | unsigned short pad4; | |
58caa3dc DJ |
52 | |
53 | /* Space for eight 80-bit FP values. */ | |
f450004a | 54 | unsigned char st_space[80]; |
58caa3dc DJ |
55 | }; |
56 | ||
57 | struct i387_fxsave { | |
58 | /* All these are only sixteen bits, plus padding, except for fop (which | |
59 | is only eleven bits), and fooff / fioff (which are 32 bits each). */ | |
60 | unsigned short fctrl; | |
61 | unsigned short fstat; | |
62 | unsigned short ftag; | |
63 | unsigned short fop; | |
64 | unsigned int fioff; | |
0c2ead7e DJ |
65 | unsigned short fiseg; |
66 | unsigned short pad1; | |
58caa3dc | 67 | unsigned int fooff; |
0c2ead7e DJ |
68 | unsigned short foseg; |
69 | unsigned short pad12; | |
58caa3dc DJ |
70 | |
71 | unsigned int mxcsr; | |
0c2ead7e | 72 | unsigned int pad3; |
58caa3dc DJ |
73 | |
74 | /* Space for eight 80-bit FP values in 128-bit spaces. */ | |
f450004a | 75 | unsigned char st_space[128]; |
58caa3dc DJ |
76 | |
77 | /* Space for eight 128-bit XMM values, or 16 on x86-64. */ | |
f450004a | 78 | unsigned char xmm_space[256]; |
58caa3dc DJ |
79 | }; |
80 | ||
1570b33e L |
81 | struct i387_xsave { |
82 | /* All these are only sixteen bits, plus padding, except for fop (which | |
83 | is only eleven bits), and fooff / fioff (which are 32 bits each). */ | |
84 | unsigned short fctrl; | |
85 | unsigned short fstat; | |
86 | unsigned short ftag; | |
87 | unsigned short fop; | |
88 | unsigned int fioff; | |
89 | unsigned short fiseg; | |
90 | unsigned short pad1; | |
91 | unsigned int fooff; | |
92 | unsigned short foseg; | |
93 | unsigned short pad12; | |
94 | ||
95 | unsigned int mxcsr; | |
96 | unsigned int mxcsr_mask; | |
97 | ||
98 | /* Space for eight 80-bit FP values in 128-bit spaces. */ | |
99 | unsigned char st_space[128]; | |
100 | ||
101 | /* Space for eight 128-bit XMM values, or 16 on x86-64. */ | |
102 | unsigned char xmm_space[256]; | |
103 | ||
104 | unsigned char reserved1[48]; | |
105 | ||
106 | /* The extended control register 0 (the XFEATURE_ENABLED_MASK | |
107 | register). */ | |
108 | unsigned long long xcr0; | |
109 | ||
110 | unsigned char reserved2[40]; | |
111 | ||
112 | /* The XSTATE_BV bit vector. */ | |
113 | unsigned long long xstate_bv; | |
114 | ||
115 | unsigned char reserved3[56]; | |
116 | ||
117 | /* Space for eight upper 128-bit YMM values, or 16 on x86-64. */ | |
118 | unsigned char ymmh_space[256]; | |
a196ebeb WT |
119 | |
120 | unsigned char reserved4[128]; | |
121 | ||
122 | /* Space for 4 bound registers values of 128 bits. */ | |
123 | unsigned char mpx_bnd_space[64]; | |
124 | ||
125 | /* Space for 2 MPX configuration registers of 64 bits | |
126 | plus reserved space. */ | |
127 | unsigned char mpx_cfg_space[16]; | |
01f9f808 MS |
128 | |
129 | unsigned char reserved5[48]; | |
130 | ||
131 | /* Space for 8 OpMask register values of 64 bits. */ | |
132 | unsigned char k_space[64]; | |
133 | ||
134 | /* Space for 16 256-bit zmm0-15. */ | |
135 | unsigned char zmmh_low_space[512]; | |
136 | ||
137 | /* Space for 16 512-bit zmm16-31 values. */ | |
138 | unsigned char zmmh_high_space[1024]; | |
1570b33e L |
139 | }; |
140 | ||
58caa3dc | 141 | void |
442ea881 | 142 | i387_cache_to_fsave (struct regcache *regcache, void *buf) |
58caa3dc DJ |
143 | { |
144 | struct i387_fsave *fp = (struct i387_fsave *) buf; | |
145 | int i; | |
3aee8918 | 146 | int st0_regnum = find_regno (regcache->tdesc, "st0"); |
58caa3dc DJ |
147 | unsigned long val, val2; |
148 | ||
149 | for (i = 0; i < 8; i++) | |
442ea881 PA |
150 | collect_register (regcache, i + st0_regnum, |
151 | ((char *) &fp->st_space[0]) + i * 10); | |
58caa3dc | 152 | |
442ea881 PA |
153 | collect_register_by_name (regcache, "fioff", &fp->fioff); |
154 | collect_register_by_name (regcache, "fooff", &fp->fooff); | |
58caa3dc DJ |
155 | |
156 | /* This one's 11 bits... */ | |
442ea881 | 157 | collect_register_by_name (regcache, "fop", &val2); |
58caa3dc DJ |
158 | fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800); |
159 | ||
160 | /* Some registers are 16-bit. */ | |
442ea881 | 161 | collect_register_by_name (regcache, "fctrl", &val); |
0c2ead7e | 162 | fp->fctrl = val; |
58caa3dc | 163 | |
442ea881 | 164 | collect_register_by_name (regcache, "fstat", &val); |
58caa3dc | 165 | val &= 0xFFFF; |
0c2ead7e | 166 | fp->fstat = val; |
58caa3dc | 167 | |
442ea881 | 168 | collect_register_by_name (regcache, "ftag", &val); |
58caa3dc | 169 | val &= 0xFFFF; |
0c2ead7e | 170 | fp->ftag = val; |
58caa3dc | 171 | |
442ea881 | 172 | collect_register_by_name (regcache, "fiseg", &val); |
58caa3dc | 173 | val &= 0xFFFF; |
0c2ead7e | 174 | fp->fiseg = val; |
58caa3dc | 175 | |
442ea881 | 176 | collect_register_by_name (regcache, "foseg", &val); |
58caa3dc | 177 | val &= 0xFFFF; |
0c2ead7e | 178 | fp->foseg = val; |
58caa3dc DJ |
179 | } |
180 | ||
181 | void | |
442ea881 | 182 | i387_fsave_to_cache (struct regcache *regcache, const void *buf) |
58caa3dc DJ |
183 | { |
184 | struct i387_fsave *fp = (struct i387_fsave *) buf; | |
185 | int i; | |
3aee8918 | 186 | int st0_regnum = find_regno (regcache->tdesc, "st0"); |
58caa3dc DJ |
187 | unsigned long val; |
188 | ||
189 | for (i = 0; i < 8; i++) | |
442ea881 PA |
190 | supply_register (regcache, i + st0_regnum, |
191 | ((char *) &fp->st_space[0]) + i * 10); | |
58caa3dc | 192 | |
442ea881 PA |
193 | supply_register_by_name (regcache, "fioff", &fp->fioff); |
194 | supply_register_by_name (regcache, "fooff", &fp->fooff); | |
1b3f6016 | 195 | |
58caa3dc DJ |
196 | /* Some registers are 16-bit. */ |
197 | val = fp->fctrl & 0xFFFF; | |
442ea881 | 198 | supply_register_by_name (regcache, "fctrl", &val); |
58caa3dc DJ |
199 | |
200 | val = fp->fstat & 0xFFFF; | |
442ea881 | 201 | supply_register_by_name (regcache, "fstat", &val); |
58caa3dc DJ |
202 | |
203 | val = fp->ftag & 0xFFFF; | |
442ea881 | 204 | supply_register_by_name (regcache, "ftag", &val); |
58caa3dc DJ |
205 | |
206 | val = fp->fiseg & 0xFFFF; | |
442ea881 | 207 | supply_register_by_name (regcache, "fiseg", &val); |
58caa3dc DJ |
208 | |
209 | val = fp->foseg & 0xFFFF; | |
442ea881 | 210 | supply_register_by_name (regcache, "foseg", &val); |
58caa3dc | 211 | |
0c2ead7e | 212 | /* fop has only 11 valid bits. */ |
58caa3dc | 213 | val = (fp->fop) & 0x7FF; |
442ea881 | 214 | supply_register_by_name (regcache, "fop", &val); |
58caa3dc DJ |
215 | } |
216 | ||
217 | void | |
442ea881 | 218 | i387_cache_to_fxsave (struct regcache *regcache, void *buf) |
58caa3dc DJ |
219 | { |
220 | struct i387_fxsave *fp = (struct i387_fxsave *) buf; | |
221 | int i; | |
3aee8918 PA |
222 | int st0_regnum = find_regno (regcache->tdesc, "st0"); |
223 | int xmm0_regnum = find_regno (regcache->tdesc, "xmm0"); | |
58caa3dc | 224 | unsigned long val, val2; |
3aee8918 PA |
225 | /* Amd64 has 16 xmm regs; I386 has 8 xmm regs. */ |
226 | int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8; | |
58caa3dc DJ |
227 | |
228 | for (i = 0; i < 8; i++) | |
442ea881 PA |
229 | collect_register (regcache, i + st0_regnum, |
230 | ((char *) &fp->st_space[0]) + i * 16); | |
58caa3dc | 231 | for (i = 0; i < num_xmm_registers; i++) |
442ea881 PA |
232 | collect_register (regcache, i + xmm0_regnum, |
233 | ((char *) &fp->xmm_space[0]) + i * 16); | |
58caa3dc | 234 | |
442ea881 PA |
235 | collect_register_by_name (regcache, "fioff", &fp->fioff); |
236 | collect_register_by_name (regcache, "fooff", &fp->fooff); | |
237 | collect_register_by_name (regcache, "mxcsr", &fp->mxcsr); | |
1b3f6016 | 238 | |
58caa3dc | 239 | /* This one's 11 bits... */ |
442ea881 | 240 | collect_register_by_name (regcache, "fop", &val2); |
58caa3dc DJ |
241 | fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800); |
242 | ||
243 | /* Some registers are 16-bit. */ | |
442ea881 | 244 | collect_register_by_name (regcache, "fctrl", &val); |
0c2ead7e | 245 | fp->fctrl = val; |
58caa3dc | 246 | |
442ea881 | 247 | collect_register_by_name (regcache, "fstat", &val); |
0c2ead7e | 248 | fp->fstat = val; |
58caa3dc DJ |
249 | |
250 | /* Convert to the simplifed tag form stored in fxsave data. */ | |
442ea881 | 251 | collect_register_by_name (regcache, "ftag", &val); |
58caa3dc | 252 | val &= 0xFFFF; |
73725ff3 | 253 | val2 = 0; |
58caa3dc DJ |
254 | for (i = 7; i >= 0; i--) |
255 | { | |
256 | int tag = (val >> (i * 2)) & 3; | |
257 | ||
258 | if (tag != 3) | |
259 | val2 |= (1 << i); | |
260 | } | |
0c2ead7e | 261 | fp->ftag = val2; |
58caa3dc | 262 | |
442ea881 | 263 | collect_register_by_name (regcache, "fiseg", &val); |
0c2ead7e | 264 | fp->fiseg = val; |
58caa3dc | 265 | |
442ea881 | 266 | collect_register_by_name (regcache, "foseg", &val); |
0c2ead7e | 267 | fp->foseg = val; |
58caa3dc DJ |
268 | } |
269 | ||
1570b33e L |
270 | void |
271 | i387_cache_to_xsave (struct regcache *regcache, void *buf) | |
272 | { | |
273 | struct i387_xsave *fp = (struct i387_xsave *) buf; | |
274 | int i; | |
275 | unsigned long val, val2; | |
276 | unsigned int clear_bv; | |
277 | unsigned long long xstate_bv = 0; | |
01f9f808 | 278 | char raw[64]; |
1570b33e | 279 | char *p; |
3aee8918 PA |
280 | /* Amd64 has 16 xmm regs; I386 has 8 xmm regs. */ |
281 | int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8; | |
1570b33e L |
282 | |
283 | /* The supported bits in `xstat_bv' are 1 byte. Clear part in | |
284 | vector registers if its bit in xstat_bv is zero. */ | |
285 | clear_bv = (~fp->xstate_bv) & x86_xcr0; | |
286 | ||
287 | /* Clear part in x87 and vector registers if its bit in xstat_bv is | |
288 | zero. */ | |
289 | if (clear_bv) | |
290 | { | |
df7e5265 | 291 | if ((clear_bv & X86_XSTATE_X87)) |
1570b33e L |
292 | for (i = 0; i < 8; i++) |
293 | memset (((char *) &fp->st_space[0]) + i * 16, 0, 10); | |
294 | ||
df7e5265 | 295 | if ((clear_bv & X86_XSTATE_SSE)) |
1570b33e L |
296 | for (i = 0; i < num_xmm_registers; i++) |
297 | memset (((char *) &fp->xmm_space[0]) + i * 16, 0, 16); | |
298 | ||
df7e5265 | 299 | if ((clear_bv & X86_XSTATE_AVX)) |
1570b33e L |
300 | for (i = 0; i < num_xmm_registers; i++) |
301 | memset (((char *) &fp->ymmh_space[0]) + i * 16, 0, 16); | |
a196ebeb | 302 | |
df7e5265 | 303 | if ((clear_bv & X86_XSTATE_BNDREGS)) |
a196ebeb WT |
304 | for (i = 0; i < num_mpx_bnd_registers; i++) |
305 | memset (((char *) &fp->mpx_bnd_space[0]) + i * 16, 0, 16); | |
306 | ||
df7e5265 | 307 | if ((clear_bv & X86_XSTATE_BNDCFG)) |
a196ebeb WT |
308 | for (i = 0; i < num_mpx_cfg_registers; i++) |
309 | memset (((char *) &fp->mpx_cfg_space[0]) + i * 8, 0, 8); | |
01f9f808 | 310 | |
df7e5265 | 311 | if ((clear_bv & X86_XSTATE_K)) |
01f9f808 MS |
312 | for (i = 0; i < num_avx512_k_registers; i++) |
313 | memset (((char *) &fp->k_space[0]) + i * 8, 0, 8); | |
314 | ||
df7e5265 | 315 | if ((clear_bv & X86_XSTATE_ZMM_H)) |
01f9f808 MS |
316 | for (i = 0; i < num_avx512_zmmh_low_registers; i++) |
317 | memset (((char *) &fp->zmmh_low_space[0]) + i * 32, 0, 32); | |
318 | ||
df7e5265 | 319 | if ((clear_bv & X86_XSTATE_ZMM)) |
01f9f808 MS |
320 | { |
321 | for (i = 0; i < num_avx512_zmmh_high_registers; i++) | |
322 | memset (((char *) &fp->zmmh_low_space[0]) + 32 + i * 64, 0, 32); | |
323 | for (i = 0; i < num_avx512_xmm_registers; i++) | |
324 | memset (((char *) &fp->zmmh_high_space[0]) + i * 64, 0, 16); | |
325 | for (i = 0; i < num_avx512_ymmh_registers; i++) | |
326 | memset (((char *) &fp->zmmh_high_space[0]) + 16 + i * 64, 0, 16); | |
327 | } | |
1570b33e L |
328 | } |
329 | ||
330 | /* Check if any x87 registers are changed. */ | |
df7e5265 | 331 | if ((x86_xcr0 & X86_XSTATE_X87)) |
1570b33e | 332 | { |
3aee8918 | 333 | int st0_regnum = find_regno (regcache->tdesc, "st0"); |
1570b33e L |
334 | |
335 | for (i = 0; i < 8; i++) | |
336 | { | |
337 | collect_register (regcache, i + st0_regnum, raw); | |
338 | p = ((char *) &fp->st_space[0]) + i * 16; | |
339 | if (memcmp (raw, p, 10)) | |
340 | { | |
df7e5265 | 341 | xstate_bv |= X86_XSTATE_X87; |
1570b33e L |
342 | memcpy (p, raw, 10); |
343 | } | |
344 | } | |
345 | } | |
346 | ||
347 | /* Check if any SSE registers are changed. */ | |
df7e5265 | 348 | if ((x86_xcr0 & X86_XSTATE_SSE)) |
1570b33e | 349 | { |
3aee8918 | 350 | int xmm0_regnum = find_regno (regcache->tdesc, "xmm0"); |
1570b33e L |
351 | |
352 | for (i = 0; i < num_xmm_registers; i++) | |
353 | { | |
354 | collect_register (regcache, i + xmm0_regnum, raw); | |
355 | p = ((char *) &fp->xmm_space[0]) + i * 16; | |
356 | if (memcmp (raw, p, 16)) | |
357 | { | |
df7e5265 | 358 | xstate_bv |= X86_XSTATE_SSE; |
1570b33e L |
359 | memcpy (p, raw, 16); |
360 | } | |
361 | } | |
362 | } | |
363 | ||
364 | /* Check if any AVX registers are changed. */ | |
df7e5265 | 365 | if ((x86_xcr0 & X86_XSTATE_AVX)) |
1570b33e | 366 | { |
3aee8918 | 367 | int ymm0h_regnum = find_regno (regcache->tdesc, "ymm0h"); |
1570b33e L |
368 | |
369 | for (i = 0; i < num_xmm_registers; i++) | |
370 | { | |
371 | collect_register (regcache, i + ymm0h_regnum, raw); | |
372 | p = ((char *) &fp->ymmh_space[0]) + i * 16; | |
373 | if (memcmp (raw, p, 16)) | |
374 | { | |
df7e5265 | 375 | xstate_bv |= X86_XSTATE_AVX; |
1570b33e L |
376 | memcpy (p, raw, 16); |
377 | } | |
378 | } | |
379 | } | |
380 | ||
a196ebeb | 381 | /* Check if any bound register has changed. */ |
df7e5265 | 382 | if ((x86_xcr0 & X86_XSTATE_BNDREGS)) |
a196ebeb WT |
383 | { |
384 | int bnd0r_regnum = find_regno (regcache->tdesc, "bnd0raw"); | |
385 | ||
386 | for (i = 0; i < num_mpx_bnd_registers; i++) | |
387 | { | |
388 | collect_register (regcache, i + bnd0r_regnum, raw); | |
389 | p = ((char *) &fp->mpx_bnd_space[0]) + i * 16; | |
390 | if (memcmp (raw, p, 16)) | |
391 | { | |
df7e5265 | 392 | xstate_bv |= X86_XSTATE_BNDREGS; |
a196ebeb WT |
393 | memcpy (p, raw, 16); |
394 | } | |
395 | } | |
396 | } | |
397 | ||
398 | /* Check if any status register has changed. */ | |
df7e5265 | 399 | if ((x86_xcr0 & X86_XSTATE_BNDCFG)) |
a196ebeb WT |
400 | { |
401 | int bndcfg_regnum = find_regno (regcache->tdesc, "bndcfgu"); | |
402 | ||
403 | for (i = 0; i < num_mpx_cfg_registers; i++) | |
404 | { | |
405 | collect_register (regcache, i + bndcfg_regnum, raw); | |
406 | p = ((char *) &fp->mpx_cfg_space[0]) + i * 8; | |
407 | if (memcmp (raw, p, 8)) | |
408 | { | |
df7e5265 | 409 | xstate_bv |= X86_XSTATE_BNDCFG; |
a196ebeb WT |
410 | memcpy (p, raw, 8); |
411 | } | |
412 | } | |
413 | } | |
414 | ||
01f9f808 | 415 | /* Check if any K registers are changed. */ |
df7e5265 | 416 | if ((x86_xcr0 & X86_XSTATE_K)) |
01f9f808 MS |
417 | { |
418 | int k0_regnum = find_regno (regcache->tdesc, "k0"); | |
419 | ||
420 | for (i = 0; i < num_avx512_k_registers; i++) | |
421 | { | |
422 | collect_register (regcache, i + k0_regnum, raw); | |
423 | p = ((char *) &fp->k_space[0]) + i * 8; | |
424 | if (memcmp (raw, p, 8) != 0) | |
425 | { | |
df7e5265 | 426 | xstate_bv |= X86_XSTATE_K; |
01f9f808 MS |
427 | memcpy (p, raw, 8); |
428 | } | |
429 | } | |
430 | } | |
431 | ||
432 | /* Check if any of ZMM0H-ZMM15H registers are changed. */ | |
df7e5265 | 433 | if ((x86_xcr0 & X86_XSTATE_ZMM_H)) |
01f9f808 MS |
434 | { |
435 | int zmm0h_regnum = find_regno (regcache->tdesc, "zmm0h"); | |
436 | ||
437 | for (i = 0; i < num_avx512_zmmh_low_registers; i++) | |
438 | { | |
439 | collect_register (regcache, i + zmm0h_regnum, raw); | |
440 | p = ((char *) &fp->zmmh_low_space[0]) + i * 32; | |
441 | if (memcmp (raw, p, 32) != 0) | |
442 | { | |
df7e5265 | 443 | xstate_bv |= X86_XSTATE_ZMM_H; |
01f9f808 MS |
444 | memcpy (p, raw, 32); |
445 | } | |
446 | } | |
447 | } | |
448 | ||
449 | /* Check if any of ZMM16H-ZMM31H registers are changed. */ | |
df7e5265 | 450 | if ((x86_xcr0 & X86_XSTATE_ZMM)) |
01f9f808 MS |
451 | { |
452 | int zmm16h_regnum = find_regno (regcache->tdesc, "zmm16h"); | |
453 | ||
454 | for (i = 0; i < num_avx512_zmmh_high_registers; i++) | |
455 | { | |
456 | collect_register (regcache, i + zmm16h_regnum, raw); | |
457 | p = ((char *) &fp->zmmh_low_space[0]) + 32 + i * 64; | |
458 | if (memcmp (raw, p, 32) != 0) | |
459 | { | |
df7e5265 | 460 | xstate_bv |= X86_XSTATE_ZMM; |
01f9f808 MS |
461 | memcpy (p, raw, 32); |
462 | } | |
463 | } | |
464 | } | |
465 | ||
466 | /* Check if any XMM_AVX512 registers are changed. */ | |
df7e5265 | 467 | if ((x86_xcr0 & X86_XSTATE_ZMM)) |
01f9f808 MS |
468 | { |
469 | int xmm_avx512_regnum = find_regno (regcache->tdesc, "xmm16"); | |
470 | ||
471 | for (i = 0; i < num_avx512_xmm_registers; i++) | |
472 | { | |
473 | collect_register (regcache, i + xmm_avx512_regnum, raw); | |
474 | p = ((char *) &fp->zmmh_high_space[0]) + i * 64; | |
475 | if (memcmp (raw, p, 16) != 0) | |
476 | { | |
df7e5265 | 477 | xstate_bv |= X86_XSTATE_ZMM; |
01f9f808 MS |
478 | memcpy (p, raw, 16); |
479 | } | |
480 | } | |
481 | } | |
482 | ||
483 | /* Check if any YMMH_AVX512 registers are changed. */ | |
df7e5265 | 484 | if ((x86_xcr0 & X86_XSTATE_ZMM)) |
01f9f808 MS |
485 | { |
486 | int ymmh_avx512_regnum = find_regno (regcache->tdesc, "ymm16h"); | |
487 | ||
488 | for (i = 0; i < num_avx512_ymmh_registers; i++) | |
489 | { | |
490 | collect_register (regcache, i + ymmh_avx512_regnum, raw); | |
491 | p = ((char *) &fp->zmmh_high_space[0]) + 16 + i * 64; | |
492 | if (memcmp (raw, p, 16) != 0) | |
493 | { | |
df7e5265 | 494 | xstate_bv |= X86_XSTATE_ZMM; |
01f9f808 MS |
495 | memcpy (p, raw, 16); |
496 | } | |
497 | } | |
498 | } | |
499 | ||
1570b33e L |
500 | /* Update the corresponding bits in xstate_bv if any SSE/AVX |
501 | registers are changed. */ | |
502 | fp->xstate_bv |= xstate_bv; | |
503 | ||
504 | collect_register_by_name (regcache, "fioff", &fp->fioff); | |
505 | collect_register_by_name (regcache, "fooff", &fp->fooff); | |
506 | collect_register_by_name (regcache, "mxcsr", &fp->mxcsr); | |
507 | ||
508 | /* This one's 11 bits... */ | |
509 | collect_register_by_name (regcache, "fop", &val2); | |
510 | fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800); | |
511 | ||
512 | /* Some registers are 16-bit. */ | |
513 | collect_register_by_name (regcache, "fctrl", &val); | |
514 | fp->fctrl = val; | |
515 | ||
516 | collect_register_by_name (regcache, "fstat", &val); | |
517 | fp->fstat = val; | |
518 | ||
519 | /* Convert to the simplifed tag form stored in fxsave data. */ | |
520 | collect_register_by_name (regcache, "ftag", &val); | |
521 | val &= 0xFFFF; | |
522 | val2 = 0; | |
523 | for (i = 7; i >= 0; i--) | |
524 | { | |
525 | int tag = (val >> (i * 2)) & 3; | |
526 | ||
527 | if (tag != 3) | |
528 | val2 |= (1 << i); | |
529 | } | |
530 | fp->ftag = val2; | |
531 | ||
532 | collect_register_by_name (regcache, "fiseg", &val); | |
533 | fp->fiseg = val; | |
534 | ||
535 | collect_register_by_name (regcache, "foseg", &val); | |
536 | fp->foseg = val; | |
537 | } | |
538 | ||
58caa3dc DJ |
539 | static int |
540 | i387_ftag (struct i387_fxsave *fp, int regno) | |
541 | { | |
542 | unsigned char *raw = &fp->st_space[regno * 16]; | |
543 | unsigned int exponent; | |
544 | unsigned long fraction[2]; | |
545 | int integer; | |
546 | ||
547 | integer = raw[7] & 0x80; | |
548 | exponent = (((raw[9] & 0x7f) << 8) | raw[8]); | |
549 | fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]); | |
550 | fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16) | |
1b3f6016 | 551 | | (raw[5] << 8) | raw[4]); |
58caa3dc DJ |
552 | |
553 | if (exponent == 0x7fff) | |
554 | { | |
555 | /* Special. */ | |
556 | return (2); | |
557 | } | |
558 | else if (exponent == 0x0000) | |
559 | { | |
560 | if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer) | |
1b3f6016 PA |
561 | { |
562 | /* Zero. */ | |
563 | return (1); | |
564 | } | |
58caa3dc | 565 | else |
1b3f6016 PA |
566 | { |
567 | /* Special. */ | |
568 | return (2); | |
569 | } | |
58caa3dc DJ |
570 | } |
571 | else | |
572 | { | |
573 | if (integer) | |
1b3f6016 PA |
574 | { |
575 | /* Valid. */ | |
576 | return (0); | |
577 | } | |
58caa3dc | 578 | else |
1b3f6016 PA |
579 | { |
580 | /* Special. */ | |
581 | return (2); | |
582 | } | |
58caa3dc DJ |
583 | } |
584 | } | |
585 | ||
586 | void | |
442ea881 | 587 | i387_fxsave_to_cache (struct regcache *regcache, const void *buf) |
58caa3dc DJ |
588 | { |
589 | struct i387_fxsave *fp = (struct i387_fxsave *) buf; | |
590 | int i, top; | |
3aee8918 PA |
591 | int st0_regnum = find_regno (regcache->tdesc, "st0"); |
592 | int xmm0_regnum = find_regno (regcache->tdesc, "xmm0"); | |
58caa3dc | 593 | unsigned long val; |
3aee8918 PA |
594 | /* Amd64 has 16 xmm regs; I386 has 8 xmm regs. */ |
595 | int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8; | |
58caa3dc DJ |
596 | |
597 | for (i = 0; i < 8; i++) | |
442ea881 PA |
598 | supply_register (regcache, i + st0_regnum, |
599 | ((char *) &fp->st_space[0]) + i * 16); | |
58caa3dc | 600 | for (i = 0; i < num_xmm_registers; i++) |
442ea881 PA |
601 | supply_register (regcache, i + xmm0_regnum, |
602 | ((char *) &fp->xmm_space[0]) + i * 16); | |
58caa3dc | 603 | |
442ea881 PA |
604 | supply_register_by_name (regcache, "fioff", &fp->fioff); |
605 | supply_register_by_name (regcache, "fooff", &fp->fooff); | |
606 | supply_register_by_name (regcache, "mxcsr", &fp->mxcsr); | |
1b3f6016 | 607 | |
58caa3dc DJ |
608 | /* Some registers are 16-bit. */ |
609 | val = fp->fctrl & 0xFFFF; | |
442ea881 | 610 | supply_register_by_name (regcache, "fctrl", &val); |
58caa3dc DJ |
611 | |
612 | val = fp->fstat & 0xFFFF; | |
442ea881 | 613 | supply_register_by_name (regcache, "fstat", &val); |
58caa3dc DJ |
614 | |
615 | /* Generate the form of ftag data that GDB expects. */ | |
616 | top = (fp->fstat >> 11) & 0x7; | |
617 | val = 0; | |
618 | for (i = 7; i >= 0; i--) | |
619 | { | |
620 | int tag; | |
73725ff3 | 621 | if (fp->ftag & (1 << i)) |
58caa3dc DJ |
622 | tag = i387_ftag (fp, (i + 8 - top) % 8); |
623 | else | |
624 | tag = 3; | |
625 | val |= tag << (2 * i); | |
626 | } | |
442ea881 | 627 | supply_register_by_name (regcache, "ftag", &val); |
58caa3dc DJ |
628 | |
629 | val = fp->fiseg & 0xFFFF; | |
442ea881 | 630 | supply_register_by_name (regcache, "fiseg", &val); |
58caa3dc DJ |
631 | |
632 | val = fp->foseg & 0xFFFF; | |
442ea881 | 633 | supply_register_by_name (regcache, "foseg", &val); |
58caa3dc DJ |
634 | |
635 | val = (fp->fop) & 0x7FF; | |
442ea881 | 636 | supply_register_by_name (regcache, "fop", &val); |
58caa3dc | 637 | } |
1570b33e L |
638 | |
639 | void | |
640 | i387_xsave_to_cache (struct regcache *regcache, const void *buf) | |
641 | { | |
642 | struct i387_xsave *fp = (struct i387_xsave *) buf; | |
643 | struct i387_fxsave *fxp = (struct i387_fxsave *) buf; | |
644 | int i, top; | |
645 | unsigned long val; | |
646 | unsigned int clear_bv; | |
85724a0e | 647 | gdb_byte *p; |
3aee8918 PA |
648 | /* Amd64 has 16 xmm regs; I386 has 8 xmm regs. */ |
649 | int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8; | |
1570b33e L |
650 | |
651 | /* The supported bits in `xstat_bv' are 1 byte. Clear part in | |
652 | vector registers if its bit in xstat_bv is zero. */ | |
653 | clear_bv = (~fp->xstate_bv) & x86_xcr0; | |
654 | ||
655 | /* Check if any x87 registers are changed. */ | |
df7e5265 | 656 | if ((x86_xcr0 & X86_XSTATE_X87) != 0) |
1570b33e | 657 | { |
3aee8918 | 658 | int st0_regnum = find_regno (regcache->tdesc, "st0"); |
1570b33e | 659 | |
df7e5265 | 660 | if ((clear_bv & X86_XSTATE_X87) != 0) |
85724a0e PA |
661 | { |
662 | for (i = 0; i < 8; i++) | |
1c79eb8a | 663 | supply_register_zeroed (regcache, i + st0_regnum); |
85724a0e | 664 | } |
1570b33e | 665 | else |
1570b33e | 666 | { |
85724a0e PA |
667 | p = (gdb_byte *) &fp->st_space[0]; |
668 | for (i = 0; i < 8; i++) | |
669 | supply_register (regcache, i + st0_regnum, p + i * 16); | |
1570b33e L |
670 | } |
671 | } | |
672 | ||
df7e5265 | 673 | if ((x86_xcr0 & X86_XSTATE_SSE) != 0) |
1570b33e | 674 | { |
3aee8918 | 675 | int xmm0_regnum = find_regno (regcache->tdesc, "xmm0"); |
1570b33e | 676 | |
df7e5265 | 677 | if ((clear_bv & X86_XSTATE_SSE)) |
85724a0e PA |
678 | { |
679 | for (i = 0; i < num_xmm_registers; i++) | |
1c79eb8a | 680 | supply_register_zeroed (regcache, i + xmm0_regnum); |
85724a0e | 681 | } |
1570b33e | 682 | else |
1570b33e | 683 | { |
85724a0e PA |
684 | p = (gdb_byte *) &fp->xmm_space[0]; |
685 | for (i = 0; i < num_xmm_registers; i++) | |
686 | supply_register (regcache, i + xmm0_regnum, p + i * 16); | |
1570b33e L |
687 | } |
688 | } | |
689 | ||
df7e5265 | 690 | if ((x86_xcr0 & X86_XSTATE_AVX) != 0) |
1570b33e | 691 | { |
3aee8918 | 692 | int ymm0h_regnum = find_regno (regcache->tdesc, "ymm0h"); |
1570b33e | 693 | |
df7e5265 | 694 | if ((clear_bv & X86_XSTATE_AVX) != 0) |
85724a0e PA |
695 | { |
696 | for (i = 0; i < num_xmm_registers; i++) | |
1c79eb8a | 697 | supply_register_zeroed (regcache, i + ymm0h_regnum); |
85724a0e | 698 | } |
1570b33e | 699 | else |
1570b33e | 700 | { |
85724a0e PA |
701 | p = (gdb_byte *) &fp->ymmh_space[0]; |
702 | for (i = 0; i < num_xmm_registers; i++) | |
703 | supply_register (regcache, i + ymm0h_regnum, p + i * 16); | |
1570b33e L |
704 | } |
705 | } | |
706 | ||
df7e5265 | 707 | if ((x86_xcr0 & X86_XSTATE_BNDREGS)) |
a196ebeb WT |
708 | { |
709 | int bnd0r_regnum = find_regno (regcache->tdesc, "bnd0raw"); | |
710 | ||
711 | ||
df7e5265 | 712 | if ((clear_bv & X86_XSTATE_BNDREGS) != 0) |
a196ebeb WT |
713 | { |
714 | for (i = 0; i < num_mpx_bnd_registers; i++) | |
715 | supply_register_zeroed (regcache, i + bnd0r_regnum); | |
716 | } | |
717 | else | |
718 | { | |
719 | p = (gdb_byte *) &fp->mpx_bnd_space[0]; | |
720 | for (i = 0; i < num_mpx_bnd_registers; i++) | |
721 | supply_register (regcache, i + bnd0r_regnum, p + i * 16); | |
722 | } | |
723 | ||
724 | } | |
725 | ||
df7e5265 | 726 | if ((x86_xcr0 & X86_XSTATE_BNDCFG)) |
a196ebeb WT |
727 | { |
728 | int bndcfg_regnum = find_regno (regcache->tdesc, "bndcfgu"); | |
729 | ||
df7e5265 | 730 | if ((clear_bv & X86_XSTATE_BNDCFG) != 0) |
a196ebeb WT |
731 | { |
732 | for (i = 0; i < num_mpx_cfg_registers; i++) | |
733 | supply_register_zeroed (regcache, i + bndcfg_regnum); | |
734 | } | |
735 | else | |
736 | { | |
737 | p = (gdb_byte *) &fp->mpx_cfg_space[0]; | |
738 | for (i = 0; i < num_mpx_cfg_registers; i++) | |
739 | supply_register (regcache, i + bndcfg_regnum, p + i * 8); | |
740 | } | |
741 | } | |
742 | ||
df7e5265 | 743 | if ((x86_xcr0 & X86_XSTATE_K) != 0) |
01f9f808 MS |
744 | { |
745 | int k0_regnum = find_regno (regcache->tdesc, "k0"); | |
746 | ||
df7e5265 | 747 | if ((clear_bv & X86_XSTATE_K) != 0) |
01f9f808 MS |
748 | { |
749 | for (i = 0; i < num_avx512_k_registers; i++) | |
750 | supply_register_zeroed (regcache, i + k0_regnum); | |
751 | } | |
752 | else | |
753 | { | |
754 | p = (gdb_byte *) &fp->k_space[0]; | |
755 | for (i = 0; i < num_avx512_k_registers; i++) | |
756 | supply_register (regcache, i + k0_regnum, p + i * 8); | |
757 | } | |
758 | } | |
759 | ||
df7e5265 | 760 | if ((x86_xcr0 & X86_XSTATE_ZMM_H) != 0) |
01f9f808 MS |
761 | { |
762 | int zmm0h_regnum = find_regno (regcache->tdesc, "zmm0h"); | |
763 | ||
df7e5265 | 764 | if ((clear_bv & X86_XSTATE_ZMM_H) != 0) |
01f9f808 MS |
765 | { |
766 | for (i = 0; i < num_avx512_zmmh_low_registers; i++) | |
767 | supply_register_zeroed (regcache, i + zmm0h_regnum); | |
768 | } | |
769 | else | |
770 | { | |
771 | p = (gdb_byte *) &fp->zmmh_low_space[0]; | |
772 | for (i = 0; i < num_avx512_zmmh_low_registers; i++) | |
773 | supply_register (regcache, i + zmm0h_regnum, p + i * 32); | |
774 | } | |
775 | } | |
776 | ||
df7e5265 | 777 | if ((x86_xcr0 & X86_XSTATE_ZMM) != 0) |
01f9f808 MS |
778 | { |
779 | int zmm16h_regnum = find_regno (regcache->tdesc, "zmm16h"); | |
780 | int ymm16h_regnum = find_regno (regcache->tdesc, "ymm16h"); | |
781 | int xmm16_regnum = find_regno (regcache->tdesc, "xmm16"); | |
782 | ||
df7e5265 | 783 | if ((clear_bv & X86_XSTATE_ZMM) != 0) |
01f9f808 MS |
784 | { |
785 | for (i = 0; i < num_avx512_zmmh_high_registers; i++) | |
786 | supply_register_zeroed (regcache, i + zmm16h_regnum); | |
787 | for (i = 0; i < num_avx512_ymmh_registers; i++) | |
788 | supply_register_zeroed (regcache, i + ymm16h_regnum); | |
789 | for (i = 0; i < num_avx512_xmm_registers; i++) | |
790 | supply_register_zeroed (regcache, i + xmm16_regnum); | |
791 | } | |
792 | else | |
793 | { | |
794 | p = (gdb_byte *) &fp->zmmh_high_space[0]; | |
795 | for (i = 0; i < num_avx512_zmmh_high_registers; i++) | |
796 | supply_register (regcache, i + zmm16h_regnum, p + 32 + i * 64); | |
797 | for (i = 0; i < num_avx512_ymmh_registers; i++) | |
798 | supply_register (regcache, i + ymm16h_regnum, p + 16 + i * 64); | |
799 | for (i = 0; i < num_avx512_xmm_registers; i++) | |
800 | supply_register (regcache, i + xmm16_regnum, p + i * 64); | |
801 | } | |
802 | } | |
803 | ||
1570b33e L |
804 | supply_register_by_name (regcache, "fioff", &fp->fioff); |
805 | supply_register_by_name (regcache, "fooff", &fp->fooff); | |
806 | supply_register_by_name (regcache, "mxcsr", &fp->mxcsr); | |
807 | ||
808 | /* Some registers are 16-bit. */ | |
809 | val = fp->fctrl & 0xFFFF; | |
810 | supply_register_by_name (regcache, "fctrl", &val); | |
811 | ||
812 | val = fp->fstat & 0xFFFF; | |
813 | supply_register_by_name (regcache, "fstat", &val); | |
814 | ||
815 | /* Generate the form of ftag data that GDB expects. */ | |
816 | top = (fp->fstat >> 11) & 0x7; | |
817 | val = 0; | |
818 | for (i = 7; i >= 0; i--) | |
819 | { | |
820 | int tag; | |
821 | if (fp->ftag & (1 << i)) | |
822 | tag = i387_ftag (fxp, (i + 8 - top) % 8); | |
823 | else | |
824 | tag = 3; | |
825 | val |= tag << (2 * i); | |
826 | } | |
827 | supply_register_by_name (regcache, "ftag", &val); | |
828 | ||
829 | val = fp->fiseg & 0xFFFF; | |
830 | supply_register_by_name (regcache, "fiseg", &val); | |
831 | ||
832 | val = fp->foseg & 0xFFFF; | |
833 | supply_register_by_name (regcache, "foseg", &val); | |
834 | ||
835 | val = (fp->fop) & 0x7FF; | |
836 | supply_register_by_name (regcache, "fop", &val); | |
837 | } | |
838 | ||
839 | /* Default to SSE. */ | |
df7e5265 | 840 | unsigned long long x86_xcr0 = X86_XSTATE_SSE_MASK; |