1 /* i387-specific utility functions, for the remote server for GDB.
2 Copyright (C) 2000, 2001, 2002, 2005, 2007 Free Software Foundation, Inc.
4 This file is part of GDB.
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
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA. */
24 int num_xmm_registers = 8;
26 /* Note: These functions preserve the reserved bits in control registers.
27 However, gdbserver promptly throws away that information. */
29 /* These structs should have the proper sizes and alignment on both
30 i386 and x86-64 machines. */
33 /* All these are only sixteen bits, plus padding, except for fop (which
34 is only eleven bits), and fooff / fioff (which are 32 bits each). */
44 /* Space for eight 80-bit FP values. */
45 unsigned char st_space[80];
49 /* All these are only sixteen bits, plus padding, except for fop (which
50 is only eleven bits), and fooff / fioff (which are 32 bits each). */
64 /* Space for eight 80-bit FP values in 128-bit spaces. */
65 unsigned char st_space[128];
67 /* Space for eight 128-bit XMM values, or 16 on x86-64. */
68 unsigned char xmm_space[256];
72 i387_cache_to_fsave (void *buf)
74 struct i387_fsave *fp = (struct i387_fsave *) buf;
76 int st0_regnum = find_regno ("st0");
77 unsigned long val, val2;
79 for (i = 0; i < 8; i++)
80 collect_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 10);
82 collect_register_by_name ("fioff", &fp->fioff);
83 collect_register_by_name ("fooff", &fp->fooff);
85 /* This one's 11 bits... */
86 collect_register_by_name ("fop", &val2);
87 fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800);
89 /* Some registers are 16-bit. */
90 collect_register_by_name ("fctrl", &val);
91 *(unsigned short *) &fp->fctrl = val;
93 collect_register_by_name ("fstat", &val);
95 *(unsigned short *) &fp->fstat = val;
97 collect_register_by_name ("ftag", &val);
99 *(unsigned short *) &fp->ftag = val;
101 collect_register_by_name ("fiseg", &val);
103 *(unsigned short *) &fp->fiseg = val;
105 collect_register_by_name ("foseg", &val);
107 *(unsigned short *) &fp->foseg = val;
111 i387_fsave_to_cache (const void *buf)
113 struct i387_fsave *fp = (struct i387_fsave *) buf;
115 int st0_regnum = find_regno ("st0");
118 for (i = 0; i < 8; i++)
119 supply_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 10);
121 supply_register_by_name ("fioff", &fp->fioff);
122 supply_register_by_name ("fooff", &fp->fooff);
124 /* Some registers are 16-bit. */
125 val = fp->fctrl & 0xFFFF;
126 supply_register_by_name ("fctrl", &val);
128 val = fp->fstat & 0xFFFF;
129 supply_register_by_name ("fstat", &val);
131 val = fp->ftag & 0xFFFF;
132 supply_register_by_name ("ftag", &val);
134 val = fp->fiseg & 0xFFFF;
135 supply_register_by_name ("fiseg", &val);
137 val = fp->foseg & 0xFFFF;
138 supply_register_by_name ("foseg", &val);
140 val = (fp->fop) & 0x7FF;
141 supply_register_by_name ("fop", &val);
145 i387_cache_to_fxsave (void *buf)
147 struct i387_fxsave *fp = (struct i387_fxsave *) buf;
149 int st0_regnum = find_regno ("st0");
150 int xmm0_regnum = find_regno ("xmm0");
151 unsigned long val, val2;
153 for (i = 0; i < 8; i++)
154 collect_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 16);
155 for (i = 0; i < num_xmm_registers; i++)
156 collect_register (i + xmm0_regnum, ((char *) &fp->xmm_space[0]) + i * 16);
158 collect_register_by_name ("fioff", &fp->fioff);
159 collect_register_by_name ("fooff", &fp->fooff);
160 collect_register_by_name ("mxcsr", &fp->mxcsr);
162 /* This one's 11 bits... */
163 collect_register_by_name ("fop", &val2);
164 fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800);
166 /* Some registers are 16-bit. */
167 collect_register_by_name ("fctrl", &val);
168 *(unsigned short *) &fp->fctrl = val;
170 collect_register_by_name ("fstat", &val);
172 *(unsigned short *) &fp->fstat = val;
174 /* Convert to the simplifed tag form stored in fxsave data. */
175 collect_register_by_name ("ftag", &val);
177 for (i = 7; i >= 0; i--)
179 int tag = (val >> (i * 2)) & 3;
184 *(unsigned short *) &fp->ftag = val2;
186 collect_register_by_name ("fiseg", &val);
188 *(unsigned short *) &fp->fiseg = val;
190 collect_register_by_name ("foseg", &val);
192 *(unsigned short *) &fp->foseg = val;
196 i387_ftag (struct i387_fxsave *fp, int regno)
198 unsigned char *raw = &fp->st_space[regno * 16];
199 unsigned int exponent;
200 unsigned long fraction[2];
203 integer = raw[7] & 0x80;
204 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
205 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
206 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
207 | (raw[5] << 8) | raw[4]);
209 if (exponent == 0x7fff)
214 else if (exponent == 0x0000)
216 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
243 i387_fxsave_to_cache (const void *buf)
245 struct i387_fxsave *fp = (struct i387_fxsave *) buf;
247 int st0_regnum = find_regno ("st0");
248 int xmm0_regnum = find_regno ("xmm0");
251 for (i = 0; i < 8; i++)
252 supply_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 16);
253 for (i = 0; i < num_xmm_registers; i++)
254 supply_register (i + xmm0_regnum, ((char *) &fp->xmm_space[0]) + i * 16);
256 supply_register_by_name ("fioff", &fp->fioff);
257 supply_register_by_name ("fooff", &fp->fooff);
258 supply_register_by_name ("mxcsr", &fp->mxcsr);
260 /* Some registers are 16-bit. */
261 val = fp->fctrl & 0xFFFF;
262 supply_register_by_name ("fctrl", &val);
264 val = fp->fstat & 0xFFFF;
265 supply_register_by_name ("fstat", &val);
267 /* Generate the form of ftag data that GDB expects. */
268 top = (fp->fstat >> 11) & 0x7;
270 for (i = 7; i >= 0; i--)
274 tag = i387_ftag (fp, (i + 8 - top) % 8);
277 val |= tag << (2 * i);
279 supply_register_by_name ("ftag", &val);
281 val = fp->fiseg & 0xFFFF;
282 supply_register_by_name ("fiseg", &val);
284 val = fp->foseg & 0xFFFF;
285 supply_register_by_name ("foseg", &val);
287 val = (fp->fop) & 0x7FF;
288 supply_register_by_name ("fop", &val);