MPX for amd64
[deliverable/binutils-gdb.git] / gdb / gdbserver / i387-fp.c
CommitLineData
58caa3dc 1/* i387-specific utility functions, for the remote server for GDB.
28e7fd62 2 Copyright (C) 2000-2013 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"
1570b33e 21#include "i386-xstate.h"
58caa3dc 22
58caa3dc
DJ
23/* Note: These functions preserve the reserved bits in control registers.
24 However, gdbserver promptly throws away that information. */
25
26/* These structs should have the proper sizes and alignment on both
27 i386 and x86-64 machines. */
28
29struct i387_fsave {
30 /* All these are only sixteen bits, plus padding, except for fop (which
31 is only eleven bits), and fooff / fioff (which are 32 bits each). */
0c2ead7e
DJ
32 unsigned short fctrl;
33 unsigned short pad1;
34 unsigned short fstat;
35 unsigned short pad2;
36 unsigned short ftag;
37 unsigned short pad3;
58caa3dc
DJ
38 unsigned int fioff;
39 unsigned short fiseg;
40 unsigned short fop;
41 unsigned int fooff;
0c2ead7e
DJ
42 unsigned short foseg;
43 unsigned short pad4;
58caa3dc
DJ
44
45 /* Space for eight 80-bit FP values. */
f450004a 46 unsigned char st_space[80];
58caa3dc
DJ
47};
48
49struct i387_fxsave {
50 /* All these are only sixteen bits, plus padding, except for fop (which
51 is only eleven bits), and fooff / fioff (which are 32 bits each). */
52 unsigned short fctrl;
53 unsigned short fstat;
54 unsigned short ftag;
55 unsigned short fop;
56 unsigned int fioff;
0c2ead7e
DJ
57 unsigned short fiseg;
58 unsigned short pad1;
58caa3dc 59 unsigned int fooff;
0c2ead7e
DJ
60 unsigned short foseg;
61 unsigned short pad12;
58caa3dc
DJ
62
63 unsigned int mxcsr;
0c2ead7e 64 unsigned int pad3;
58caa3dc
DJ
65
66 /* Space for eight 80-bit FP values in 128-bit spaces. */
f450004a 67 unsigned char st_space[128];
58caa3dc
DJ
68
69 /* Space for eight 128-bit XMM values, or 16 on x86-64. */
f450004a 70 unsigned char xmm_space[256];
58caa3dc
DJ
71};
72
1570b33e
L
73struct i387_xsave {
74 /* All these are only sixteen bits, plus padding, except for fop (which
75 is only eleven bits), and fooff / fioff (which are 32 bits each). */
76 unsigned short fctrl;
77 unsigned short fstat;
78 unsigned short ftag;
79 unsigned short fop;
80 unsigned int fioff;
81 unsigned short fiseg;
82 unsigned short pad1;
83 unsigned int fooff;
84 unsigned short foseg;
85 unsigned short pad12;
86
87 unsigned int mxcsr;
88 unsigned int mxcsr_mask;
89
90 /* Space for eight 80-bit FP values in 128-bit spaces. */
91 unsigned char st_space[128];
92
93 /* Space for eight 128-bit XMM values, or 16 on x86-64. */
94 unsigned char xmm_space[256];
95
96 unsigned char reserved1[48];
97
98 /* The extended control register 0 (the XFEATURE_ENABLED_MASK
99 register). */
100 unsigned long long xcr0;
101
102 unsigned char reserved2[40];
103
104 /* The XSTATE_BV bit vector. */
105 unsigned long long xstate_bv;
106
107 unsigned char reserved3[56];
108
109 /* Space for eight upper 128-bit YMM values, or 16 on x86-64. */
110 unsigned char ymmh_space[256];
111};
112
58caa3dc 113void
442ea881 114i387_cache_to_fsave (struct regcache *regcache, void *buf)
58caa3dc
DJ
115{
116 struct i387_fsave *fp = (struct i387_fsave *) buf;
117 int i;
3aee8918 118 int st0_regnum = find_regno (regcache->tdesc, "st0");
58caa3dc
DJ
119 unsigned long val, val2;
120
121 for (i = 0; i < 8; i++)
442ea881
PA
122 collect_register (regcache, i + st0_regnum,
123 ((char *) &fp->st_space[0]) + i * 10);
58caa3dc 124
442ea881
PA
125 collect_register_by_name (regcache, "fioff", &fp->fioff);
126 collect_register_by_name (regcache, "fooff", &fp->fooff);
58caa3dc
DJ
127
128 /* This one's 11 bits... */
442ea881 129 collect_register_by_name (regcache, "fop", &val2);
58caa3dc
DJ
130 fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800);
131
132 /* Some registers are 16-bit. */
442ea881 133 collect_register_by_name (regcache, "fctrl", &val);
0c2ead7e 134 fp->fctrl = val;
58caa3dc 135
442ea881 136 collect_register_by_name (regcache, "fstat", &val);
58caa3dc 137 val &= 0xFFFF;
0c2ead7e 138 fp->fstat = val;
58caa3dc 139
442ea881 140 collect_register_by_name (regcache, "ftag", &val);
58caa3dc 141 val &= 0xFFFF;
0c2ead7e 142 fp->ftag = val;
58caa3dc 143
442ea881 144 collect_register_by_name (regcache, "fiseg", &val);
58caa3dc 145 val &= 0xFFFF;
0c2ead7e 146 fp->fiseg = val;
58caa3dc 147
442ea881 148 collect_register_by_name (regcache, "foseg", &val);
58caa3dc 149 val &= 0xFFFF;
0c2ead7e 150 fp->foseg = val;
58caa3dc
DJ
151}
152
153void
442ea881 154i387_fsave_to_cache (struct regcache *regcache, const void *buf)
58caa3dc
DJ
155{
156 struct i387_fsave *fp = (struct i387_fsave *) buf;
157 int i;
3aee8918 158 int st0_regnum = find_regno (regcache->tdesc, "st0");
58caa3dc
DJ
159 unsigned long val;
160
161 for (i = 0; i < 8; i++)
442ea881
PA
162 supply_register (regcache, i + st0_regnum,
163 ((char *) &fp->st_space[0]) + i * 10);
58caa3dc 164
442ea881
PA
165 supply_register_by_name (regcache, "fioff", &fp->fioff);
166 supply_register_by_name (regcache, "fooff", &fp->fooff);
1b3f6016 167
58caa3dc
DJ
168 /* Some registers are 16-bit. */
169 val = fp->fctrl & 0xFFFF;
442ea881 170 supply_register_by_name (regcache, "fctrl", &val);
58caa3dc
DJ
171
172 val = fp->fstat & 0xFFFF;
442ea881 173 supply_register_by_name (regcache, "fstat", &val);
58caa3dc
DJ
174
175 val = fp->ftag & 0xFFFF;
442ea881 176 supply_register_by_name (regcache, "ftag", &val);
58caa3dc
DJ
177
178 val = fp->fiseg & 0xFFFF;
442ea881 179 supply_register_by_name (regcache, "fiseg", &val);
58caa3dc
DJ
180
181 val = fp->foseg & 0xFFFF;
442ea881 182 supply_register_by_name (regcache, "foseg", &val);
58caa3dc 183
0c2ead7e 184 /* fop has only 11 valid bits. */
58caa3dc 185 val = (fp->fop) & 0x7FF;
442ea881 186 supply_register_by_name (regcache, "fop", &val);
58caa3dc
DJ
187}
188
189void
442ea881 190i387_cache_to_fxsave (struct regcache *regcache, void *buf)
58caa3dc
DJ
191{
192 struct i387_fxsave *fp = (struct i387_fxsave *) buf;
193 int i;
3aee8918
PA
194 int st0_regnum = find_regno (regcache->tdesc, "st0");
195 int xmm0_regnum = find_regno (regcache->tdesc, "xmm0");
58caa3dc 196 unsigned long val, val2;
3aee8918
PA
197 /* Amd64 has 16 xmm regs; I386 has 8 xmm regs. */
198 int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8;
58caa3dc
DJ
199
200 for (i = 0; i < 8; i++)
442ea881
PA
201 collect_register (regcache, i + st0_regnum,
202 ((char *) &fp->st_space[0]) + i * 16);
58caa3dc 203 for (i = 0; i < num_xmm_registers; i++)
442ea881
PA
204 collect_register (regcache, i + xmm0_regnum,
205 ((char *) &fp->xmm_space[0]) + i * 16);
58caa3dc 206
442ea881
PA
207 collect_register_by_name (regcache, "fioff", &fp->fioff);
208 collect_register_by_name (regcache, "fooff", &fp->fooff);
209 collect_register_by_name (regcache, "mxcsr", &fp->mxcsr);
1b3f6016 210
58caa3dc 211 /* This one's 11 bits... */
442ea881 212 collect_register_by_name (regcache, "fop", &val2);
58caa3dc
DJ
213 fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800);
214
215 /* Some registers are 16-bit. */
442ea881 216 collect_register_by_name (regcache, "fctrl", &val);
0c2ead7e 217 fp->fctrl = val;
58caa3dc 218
442ea881 219 collect_register_by_name (regcache, "fstat", &val);
0c2ead7e 220 fp->fstat = val;
58caa3dc
DJ
221
222 /* Convert to the simplifed tag form stored in fxsave data. */
442ea881 223 collect_register_by_name (regcache, "ftag", &val);
58caa3dc 224 val &= 0xFFFF;
73725ff3 225 val2 = 0;
58caa3dc
DJ
226 for (i = 7; i >= 0; i--)
227 {
228 int tag = (val >> (i * 2)) & 3;
229
230 if (tag != 3)
231 val2 |= (1 << i);
232 }
0c2ead7e 233 fp->ftag = val2;
58caa3dc 234
442ea881 235 collect_register_by_name (regcache, "fiseg", &val);
0c2ead7e 236 fp->fiseg = val;
58caa3dc 237
442ea881 238 collect_register_by_name (regcache, "foseg", &val);
0c2ead7e 239 fp->foseg = val;
58caa3dc
DJ
240}
241
1570b33e
L
242void
243i387_cache_to_xsave (struct regcache *regcache, void *buf)
244{
245 struct i387_xsave *fp = (struct i387_xsave *) buf;
246 int i;
247 unsigned long val, val2;
248 unsigned int clear_bv;
249 unsigned long long xstate_bv = 0;
250 char raw[16];
251 char *p;
3aee8918
PA
252 /* Amd64 has 16 xmm regs; I386 has 8 xmm regs. */
253 int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8;
1570b33e
L
254
255 /* The supported bits in `xstat_bv' are 1 byte. Clear part in
256 vector registers if its bit in xstat_bv is zero. */
257 clear_bv = (~fp->xstate_bv) & x86_xcr0;
258
259 /* Clear part in x87 and vector registers if its bit in xstat_bv is
260 zero. */
261 if (clear_bv)
262 {
263 if ((clear_bv & I386_XSTATE_X87))
264 for (i = 0; i < 8; i++)
265 memset (((char *) &fp->st_space[0]) + i * 16, 0, 10);
266
267 if ((clear_bv & I386_XSTATE_SSE))
268 for (i = 0; i < num_xmm_registers; i++)
269 memset (((char *) &fp->xmm_space[0]) + i * 16, 0, 16);
270
271 if ((clear_bv & I386_XSTATE_AVX))
272 for (i = 0; i < num_xmm_registers; i++)
273 memset (((char *) &fp->ymmh_space[0]) + i * 16, 0, 16);
274 }
275
276 /* Check if any x87 registers are changed. */
277 if ((x86_xcr0 & I386_XSTATE_X87))
278 {
3aee8918 279 int st0_regnum = find_regno (regcache->tdesc, "st0");
1570b33e
L
280
281 for (i = 0; i < 8; i++)
282 {
283 collect_register (regcache, i + st0_regnum, raw);
284 p = ((char *) &fp->st_space[0]) + i * 16;
285 if (memcmp (raw, p, 10))
286 {
287 xstate_bv |= I386_XSTATE_X87;
288 memcpy (p, raw, 10);
289 }
290 }
291 }
292
293 /* Check if any SSE registers are changed. */
294 if ((x86_xcr0 & I386_XSTATE_SSE))
295 {
3aee8918 296 int xmm0_regnum = find_regno (regcache->tdesc, "xmm0");
1570b33e
L
297
298 for (i = 0; i < num_xmm_registers; i++)
299 {
300 collect_register (regcache, i + xmm0_regnum, raw);
301 p = ((char *) &fp->xmm_space[0]) + i * 16;
302 if (memcmp (raw, p, 16))
303 {
304 xstate_bv |= I386_XSTATE_SSE;
305 memcpy (p, raw, 16);
306 }
307 }
308 }
309
310 /* Check if any AVX registers are changed. */
311 if ((x86_xcr0 & I386_XSTATE_AVX))
312 {
3aee8918 313 int ymm0h_regnum = find_regno (regcache->tdesc, "ymm0h");
1570b33e
L
314
315 for (i = 0; i < num_xmm_registers; i++)
316 {
317 collect_register (regcache, i + ymm0h_regnum, raw);
318 p = ((char *) &fp->ymmh_space[0]) + i * 16;
319 if (memcmp (raw, p, 16))
320 {
321 xstate_bv |= I386_XSTATE_AVX;
322 memcpy (p, raw, 16);
323 }
324 }
325 }
326
327 /* Update the corresponding bits in xstate_bv if any SSE/AVX
328 registers are changed. */
329 fp->xstate_bv |= xstate_bv;
330
331 collect_register_by_name (regcache, "fioff", &fp->fioff);
332 collect_register_by_name (regcache, "fooff", &fp->fooff);
333 collect_register_by_name (regcache, "mxcsr", &fp->mxcsr);
334
335 /* This one's 11 bits... */
336 collect_register_by_name (regcache, "fop", &val2);
337 fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800);
338
339 /* Some registers are 16-bit. */
340 collect_register_by_name (regcache, "fctrl", &val);
341 fp->fctrl = val;
342
343 collect_register_by_name (regcache, "fstat", &val);
344 fp->fstat = val;
345
346 /* Convert to the simplifed tag form stored in fxsave data. */
347 collect_register_by_name (regcache, "ftag", &val);
348 val &= 0xFFFF;
349 val2 = 0;
350 for (i = 7; i >= 0; i--)
351 {
352 int tag = (val >> (i * 2)) & 3;
353
354 if (tag != 3)
355 val2 |= (1 << i);
356 }
357 fp->ftag = val2;
358
359 collect_register_by_name (regcache, "fiseg", &val);
360 fp->fiseg = val;
361
362 collect_register_by_name (regcache, "foseg", &val);
363 fp->foseg = val;
364}
365
58caa3dc
DJ
366static int
367i387_ftag (struct i387_fxsave *fp, int regno)
368{
369 unsigned char *raw = &fp->st_space[regno * 16];
370 unsigned int exponent;
371 unsigned long fraction[2];
372 int integer;
373
374 integer = raw[7] & 0x80;
375 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
376 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
377 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
1b3f6016 378 | (raw[5] << 8) | raw[4]);
58caa3dc
DJ
379
380 if (exponent == 0x7fff)
381 {
382 /* Special. */
383 return (2);
384 }
385 else if (exponent == 0x0000)
386 {
387 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
1b3f6016
PA
388 {
389 /* Zero. */
390 return (1);
391 }
58caa3dc 392 else
1b3f6016
PA
393 {
394 /* Special. */
395 return (2);
396 }
58caa3dc
DJ
397 }
398 else
399 {
400 if (integer)
1b3f6016
PA
401 {
402 /* Valid. */
403 return (0);
404 }
58caa3dc 405 else
1b3f6016
PA
406 {
407 /* Special. */
408 return (2);
409 }
58caa3dc
DJ
410 }
411}
412
413void
442ea881 414i387_fxsave_to_cache (struct regcache *regcache, const void *buf)
58caa3dc
DJ
415{
416 struct i387_fxsave *fp = (struct i387_fxsave *) buf;
417 int i, top;
3aee8918
PA
418 int st0_regnum = find_regno (regcache->tdesc, "st0");
419 int xmm0_regnum = find_regno (regcache->tdesc, "xmm0");
58caa3dc 420 unsigned long val;
3aee8918
PA
421 /* Amd64 has 16 xmm regs; I386 has 8 xmm regs. */
422 int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8;
58caa3dc
DJ
423
424 for (i = 0; i < 8; i++)
442ea881
PA
425 supply_register (regcache, i + st0_regnum,
426 ((char *) &fp->st_space[0]) + i * 16);
58caa3dc 427 for (i = 0; i < num_xmm_registers; i++)
442ea881
PA
428 supply_register (regcache, i + xmm0_regnum,
429 ((char *) &fp->xmm_space[0]) + i * 16);
58caa3dc 430
442ea881
PA
431 supply_register_by_name (regcache, "fioff", &fp->fioff);
432 supply_register_by_name (regcache, "fooff", &fp->fooff);
433 supply_register_by_name (regcache, "mxcsr", &fp->mxcsr);
1b3f6016 434
58caa3dc
DJ
435 /* Some registers are 16-bit. */
436 val = fp->fctrl & 0xFFFF;
442ea881 437 supply_register_by_name (regcache, "fctrl", &val);
58caa3dc
DJ
438
439 val = fp->fstat & 0xFFFF;
442ea881 440 supply_register_by_name (regcache, "fstat", &val);
58caa3dc
DJ
441
442 /* Generate the form of ftag data that GDB expects. */
443 top = (fp->fstat >> 11) & 0x7;
444 val = 0;
445 for (i = 7; i >= 0; i--)
446 {
447 int tag;
73725ff3 448 if (fp->ftag & (1 << i))
58caa3dc
DJ
449 tag = i387_ftag (fp, (i + 8 - top) % 8);
450 else
451 tag = 3;
452 val |= tag << (2 * i);
453 }
442ea881 454 supply_register_by_name (regcache, "ftag", &val);
58caa3dc
DJ
455
456 val = fp->fiseg & 0xFFFF;
442ea881 457 supply_register_by_name (regcache, "fiseg", &val);
58caa3dc
DJ
458
459 val = fp->foseg & 0xFFFF;
442ea881 460 supply_register_by_name (regcache, "foseg", &val);
58caa3dc
DJ
461
462 val = (fp->fop) & 0x7FF;
442ea881 463 supply_register_by_name (regcache, "fop", &val);
58caa3dc 464}
1570b33e
L
465
466void
467i387_xsave_to_cache (struct regcache *regcache, const void *buf)
468{
469 struct i387_xsave *fp = (struct i387_xsave *) buf;
470 struct i387_fxsave *fxp = (struct i387_fxsave *) buf;
471 int i, top;
472 unsigned long val;
473 unsigned int clear_bv;
85724a0e 474 gdb_byte *p;
3aee8918
PA
475 /* Amd64 has 16 xmm regs; I386 has 8 xmm regs. */
476 int num_xmm_registers = register_size (regcache->tdesc, 0) == 8 ? 16 : 8;
1570b33e
L
477
478 /* The supported bits in `xstat_bv' are 1 byte. Clear part in
479 vector registers if its bit in xstat_bv is zero. */
480 clear_bv = (~fp->xstate_bv) & x86_xcr0;
481
482 /* Check if any x87 registers are changed. */
85724a0e 483 if ((x86_xcr0 & I386_XSTATE_X87) != 0)
1570b33e 484 {
3aee8918 485 int st0_regnum = find_regno (regcache->tdesc, "st0");
1570b33e 486
85724a0e
PA
487 if ((clear_bv & I386_XSTATE_X87) != 0)
488 {
489 for (i = 0; i < 8; i++)
1c79eb8a 490 supply_register_zeroed (regcache, i + st0_regnum);
85724a0e 491 }
1570b33e 492 else
1570b33e 493 {
85724a0e
PA
494 p = (gdb_byte *) &fp->st_space[0];
495 for (i = 0; i < 8; i++)
496 supply_register (regcache, i + st0_regnum, p + i * 16);
1570b33e
L
497 }
498 }
499
85724a0e 500 if ((x86_xcr0 & I386_XSTATE_SSE) != 0)
1570b33e 501 {
3aee8918 502 int xmm0_regnum = find_regno (regcache->tdesc, "xmm0");
1570b33e
L
503
504 if ((clear_bv & I386_XSTATE_SSE))
85724a0e
PA
505 {
506 for (i = 0; i < num_xmm_registers; i++)
1c79eb8a 507 supply_register_zeroed (regcache, i + xmm0_regnum);
85724a0e 508 }
1570b33e 509 else
1570b33e 510 {
85724a0e
PA
511 p = (gdb_byte *) &fp->xmm_space[0];
512 for (i = 0; i < num_xmm_registers; i++)
513 supply_register (regcache, i + xmm0_regnum, p + i * 16);
1570b33e
L
514 }
515 }
516
85724a0e 517 if ((x86_xcr0 & I386_XSTATE_AVX) != 0)
1570b33e 518 {
3aee8918 519 int ymm0h_regnum = find_regno (regcache->tdesc, "ymm0h");
1570b33e 520
85724a0e
PA
521 if ((clear_bv & I386_XSTATE_AVX) != 0)
522 {
523 for (i = 0; i < num_xmm_registers; i++)
1c79eb8a 524 supply_register_zeroed (regcache, i + ymm0h_regnum);
85724a0e 525 }
1570b33e 526 else
1570b33e 527 {
85724a0e
PA
528 p = (gdb_byte *) &fp->ymmh_space[0];
529 for (i = 0; i < num_xmm_registers; i++)
530 supply_register (regcache, i + ymm0h_regnum, p + i * 16);
1570b33e
L
531 }
532 }
533
534 supply_register_by_name (regcache, "fioff", &fp->fioff);
535 supply_register_by_name (regcache, "fooff", &fp->fooff);
536 supply_register_by_name (regcache, "mxcsr", &fp->mxcsr);
537
538 /* Some registers are 16-bit. */
539 val = fp->fctrl & 0xFFFF;
540 supply_register_by_name (regcache, "fctrl", &val);
541
542 val = fp->fstat & 0xFFFF;
543 supply_register_by_name (regcache, "fstat", &val);
544
545 /* Generate the form of ftag data that GDB expects. */
546 top = (fp->fstat >> 11) & 0x7;
547 val = 0;
548 for (i = 7; i >= 0; i--)
549 {
550 int tag;
551 if (fp->ftag & (1 << i))
552 tag = i387_ftag (fxp, (i + 8 - top) % 8);
553 else
554 tag = 3;
555 val |= tag << (2 * i);
556 }
557 supply_register_by_name (regcache, "ftag", &val);
558
559 val = fp->fiseg & 0xFFFF;
560 supply_register_by_name (regcache, "fiseg", &val);
561
562 val = fp->foseg & 0xFFFF;
563 supply_register_by_name (regcache, "foseg", &val);
564
565 val = (fp->fop) & 0x7FF;
566 supply_register_by_name (regcache, "fop", &val);
567}
568
569/* Default to SSE. */
570unsigned long long x86_xcr0 = I386_XSTATE_SSE_MASK;
This page took 0.878254 seconds and 4 git commands to generate.