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