* i387-fp.c (struct i387_fsave, struct i387_fxsave): Make 16-bit
[deliverable/binutils-gdb.git] / gdb / gdbserver / i387-fp.c
1 /* i387-specific utility functions, for the remote server for GDB.
2 Copyright (C) 2000, 2001, 2002, 2005, 2007 Free Software Foundation, Inc.
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
8 the Free Software Foundation; either version 2 of the License, or
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
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. */
20
21 #include "server.h"
22 #include "i387-fp.h"
23
24 int num_xmm_registers = 8;
25
26 /* Note: These functions preserve the reserved bits in control registers.
27 However, gdbserver promptly throws away that information. */
28
29 /* These structs should have the proper sizes and alignment on both
30 i386 and x86-64 machines. */
31
32 struct i387_fsave {
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). */
35 unsigned short fctrl;
36 unsigned short pad1;
37 unsigned short fstat;
38 unsigned short pad2;
39 unsigned short ftag;
40 unsigned short pad3;
41 unsigned int fioff;
42 unsigned short fiseg;
43 unsigned short fop;
44 unsigned int fooff;
45 unsigned short foseg;
46 unsigned short pad4;
47
48 /* Space for eight 80-bit FP values. */
49 unsigned char st_space[80];
50 };
51
52 struct i387_fxsave {
53 /* All these are only sixteen bits, plus padding, except for fop (which
54 is only eleven bits), and fooff / fioff (which are 32 bits each). */
55 unsigned short fctrl;
56 unsigned short fstat;
57 unsigned short ftag;
58 unsigned short fop;
59 unsigned int fioff;
60 unsigned short fiseg;
61 unsigned short pad1;
62 unsigned int fooff;
63 unsigned short foseg;
64 unsigned short pad12;
65
66 unsigned int mxcsr;
67 unsigned int pad3;
68
69 /* Space for eight 80-bit FP values in 128-bit spaces. */
70 unsigned char st_space[128];
71
72 /* Space for eight 128-bit XMM values, or 16 on x86-64. */
73 unsigned char xmm_space[256];
74 };
75
76 void
77 i387_cache_to_fsave (void *buf)
78 {
79 struct i387_fsave *fp = (struct i387_fsave *) buf;
80 int i;
81 int st0_regnum = find_regno ("st0");
82 unsigned long val, val2;
83
84 for (i = 0; i < 8; i++)
85 collect_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 10);
86
87 collect_register_by_name ("fioff", &fp->fioff);
88 collect_register_by_name ("fooff", &fp->fooff);
89
90 /* This one's 11 bits... */
91 collect_register_by_name ("fop", &val2);
92 fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800);
93
94 /* Some registers are 16-bit. */
95 collect_register_by_name ("fctrl", &val);
96 fp->fctrl = val;
97
98 collect_register_by_name ("fstat", &val);
99 val &= 0xFFFF;
100 fp->fstat = val;
101
102 collect_register_by_name ("ftag", &val);
103 val &= 0xFFFF;
104 fp->ftag = val;
105
106 collect_register_by_name ("fiseg", &val);
107 val &= 0xFFFF;
108 fp->fiseg = val;
109
110 collect_register_by_name ("foseg", &val);
111 val &= 0xFFFF;
112 fp->foseg = val;
113 }
114
115 void
116 i387_fsave_to_cache (const void *buf)
117 {
118 struct i387_fsave *fp = (struct i387_fsave *) buf;
119 int i;
120 int st0_regnum = find_regno ("st0");
121 unsigned long val;
122
123 for (i = 0; i < 8; i++)
124 supply_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 10);
125
126 supply_register_by_name ("fioff", &fp->fioff);
127 supply_register_by_name ("fooff", &fp->fooff);
128
129 /* Some registers are 16-bit. */
130 val = fp->fctrl & 0xFFFF;
131 supply_register_by_name ("fctrl", &val);
132
133 val = fp->fstat & 0xFFFF;
134 supply_register_by_name ("fstat", &val);
135
136 val = fp->ftag & 0xFFFF;
137 supply_register_by_name ("ftag", &val);
138
139 val = fp->fiseg & 0xFFFF;
140 supply_register_by_name ("fiseg", &val);
141
142 val = fp->foseg & 0xFFFF;
143 supply_register_by_name ("foseg", &val);
144
145 /* fop has only 11 valid bits. */
146 val = (fp->fop) & 0x7FF;
147 supply_register_by_name ("fop", &val);
148 }
149
150 void
151 i387_cache_to_fxsave (void *buf)
152 {
153 struct i387_fxsave *fp = (struct i387_fxsave *) buf;
154 int i;
155 int st0_regnum = find_regno ("st0");
156 int xmm0_regnum = find_regno ("xmm0");
157 unsigned long val, val2;
158
159 for (i = 0; i < 8; i++)
160 collect_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 16);
161 for (i = 0; i < num_xmm_registers; i++)
162 collect_register (i + xmm0_regnum, ((char *) &fp->xmm_space[0]) + i * 16);
163
164 collect_register_by_name ("fioff", &fp->fioff);
165 collect_register_by_name ("fooff", &fp->fooff);
166 collect_register_by_name ("mxcsr", &fp->mxcsr);
167
168 /* This one's 11 bits... */
169 collect_register_by_name ("fop", &val2);
170 fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800);
171
172 /* Some registers are 16-bit. */
173 collect_register_by_name ("fctrl", &val);
174 fp->fctrl = val;
175
176 collect_register_by_name ("fstat", &val);
177 fp->fstat = val;
178
179 /* Convert to the simplifed tag form stored in fxsave data. */
180 collect_register_by_name ("ftag", &val);
181 val &= 0xFFFF;
182 val2 = 0;
183 for (i = 7; i >= 0; i--)
184 {
185 int tag = (val >> (i * 2)) & 3;
186
187 if (tag != 3)
188 val2 |= (1 << i);
189 }
190 fp->ftag = val2;
191
192 collect_register_by_name ("fiseg", &val);
193 fp->fiseg = val;
194
195 collect_register_by_name ("foseg", &val);
196 fp->foseg = val;
197 }
198
199 static int
200 i387_ftag (struct i387_fxsave *fp, int regno)
201 {
202 unsigned char *raw = &fp->st_space[regno * 16];
203 unsigned int exponent;
204 unsigned long fraction[2];
205 int integer;
206
207 integer = raw[7] & 0x80;
208 exponent = (((raw[9] & 0x7f) << 8) | raw[8]);
209 fraction[0] = ((raw[3] << 24) | (raw[2] << 16) | (raw[1] << 8) | raw[0]);
210 fraction[1] = (((raw[7] & 0x7f) << 24) | (raw[6] << 16)
211 | (raw[5] << 8) | raw[4]);
212
213 if (exponent == 0x7fff)
214 {
215 /* Special. */
216 return (2);
217 }
218 else if (exponent == 0x0000)
219 {
220 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
221 {
222 /* Zero. */
223 return (1);
224 }
225 else
226 {
227 /* Special. */
228 return (2);
229 }
230 }
231 else
232 {
233 if (integer)
234 {
235 /* Valid. */
236 return (0);
237 }
238 else
239 {
240 /* Special. */
241 return (2);
242 }
243 }
244 }
245
246 void
247 i387_fxsave_to_cache (const void *buf)
248 {
249 struct i387_fxsave *fp = (struct i387_fxsave *) buf;
250 int i, top;
251 int st0_regnum = find_regno ("st0");
252 int xmm0_regnum = find_regno ("xmm0");
253 unsigned long val;
254
255 for (i = 0; i < 8; i++)
256 supply_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 16);
257 for (i = 0; i < num_xmm_registers; i++)
258 supply_register (i + xmm0_regnum, ((char *) &fp->xmm_space[0]) + i * 16);
259
260 supply_register_by_name ("fioff", &fp->fioff);
261 supply_register_by_name ("fooff", &fp->fooff);
262 supply_register_by_name ("mxcsr", &fp->mxcsr);
263
264 /* Some registers are 16-bit. */
265 val = fp->fctrl & 0xFFFF;
266 supply_register_by_name ("fctrl", &val);
267
268 val = fp->fstat & 0xFFFF;
269 supply_register_by_name ("fstat", &val);
270
271 /* Generate the form of ftag data that GDB expects. */
272 top = (fp->fstat >> 11) & 0x7;
273 val = 0;
274 for (i = 7; i >= 0; i--)
275 {
276 int tag;
277 if (fp->ftag & (1 << i))
278 tag = i387_ftag (fp, (i + 8 - top) % 8);
279 else
280 tag = 3;
281 val |= tag << (2 * i);
282 }
283 supply_register_by_name ("ftag", &val);
284
285 val = fp->fiseg & 0xFFFF;
286 supply_register_by_name ("fiseg", &val);
287
288 val = fp->foseg & 0xFFFF;
289 supply_register_by_name ("foseg", &val);
290
291 val = (fp->fop) & 0x7FF;
292 supply_register_by_name ("fop", &val);
293 }
This page took 0.036515 seconds and 5 git commands to generate.