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