No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gdb6 / gdb / gdbserver / i387-fp.c
blob830729b4f7238328ece07da32531f7cf723afd54
1 /* i387-specific utility functions, for the remote server for GDB.
2 Copyright (C) 2000, 2001, 2002, 2005
3 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "server.h"
23 #include "i387-fp.h"
25 int num_xmm_registers = 8;
27 /* Note: These functions preserve the reserved bits in control registers.
28 However, gdbserver promptly throws away that information. */
30 /* These structs should have the proper sizes and alignment on both
31 i386 and x86-64 machines. */
33 struct i387_fsave {
34 /* All these are only sixteen bits, plus padding, except for fop (which
35 is only eleven bits), and fooff / fioff (which are 32 bits each). */
36 unsigned int fctrl;
37 unsigned int fstat;
38 unsigned int ftag;
39 unsigned int fioff;
40 unsigned short fiseg;
41 unsigned short fop;
42 unsigned int fooff;
43 unsigned int foseg;
45 /* Space for eight 80-bit FP values. */
46 unsigned char st_space[80];
49 struct 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;
57 unsigned int fiseg;
58 unsigned int fooff;
59 unsigned int foseg;
61 unsigned int mxcsr;
63 unsigned int _pad1;
65 /* Space for eight 80-bit FP values in 128-bit spaces. */
66 unsigned char st_space[128];
68 /* Space for eight 128-bit XMM values, or 16 on x86-64. */
69 unsigned char xmm_space[256];
72 void
73 i387_cache_to_fsave (void *buf)
75 struct i387_fsave *fp = (struct i387_fsave *) buf;
76 int i;
77 int st0_regnum = find_regno ("st0");
78 unsigned long val, val2;
80 for (i = 0; i < 8; i++)
81 collect_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 10);
83 collect_register_by_name ("fioff", &fp->fioff);
84 collect_register_by_name ("fooff", &fp->fooff);
86 /* This one's 11 bits... */
87 collect_register_by_name ("fop", &val2);
88 fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800);
90 /* Some registers are 16-bit. */
91 collect_register_by_name ("fctrl", &val);
92 *(unsigned short *) &fp->fctrl = val;
94 collect_register_by_name ("fstat", &val);
95 val &= 0xFFFF;
96 *(unsigned short *) &fp->fstat = val;
98 collect_register_by_name ("ftag", &val);
99 val &= 0xFFFF;
100 *(unsigned short *) &fp->ftag = val;
102 collect_register_by_name ("fiseg", &val);
103 val &= 0xFFFF;
104 *(unsigned short *) &fp->fiseg = val;
106 collect_register_by_name ("foseg", &val);
107 val &= 0xFFFF;
108 *(unsigned short *) &fp->foseg = val;
111 void
112 i387_fsave_to_cache (const void *buf)
114 struct i387_fsave *fp = (struct i387_fsave *) buf;
115 int i;
116 int st0_regnum = find_regno ("st0");
117 unsigned long val;
119 for (i = 0; i < 8; i++)
120 supply_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 10);
122 supply_register_by_name ("fioff", &fp->fioff);
123 supply_register_by_name ("fooff", &fp->fooff);
125 /* Some registers are 16-bit. */
126 val = fp->fctrl & 0xFFFF;
127 supply_register_by_name ("fctrl", &val);
129 val = fp->fstat & 0xFFFF;
130 supply_register_by_name ("fstat", &val);
132 val = fp->ftag & 0xFFFF;
133 supply_register_by_name ("ftag", &val);
135 val = fp->fiseg & 0xFFFF;
136 supply_register_by_name ("fiseg", &val);
138 val = fp->foseg & 0xFFFF;
139 supply_register_by_name ("foseg", &val);
141 val = (fp->fop) & 0x7FF;
142 supply_register_by_name ("fop", &val);
145 void
146 i387_cache_to_fxsave (void *buf)
148 struct i387_fxsave *fp = (struct i387_fxsave *) buf;
149 int i;
150 int st0_regnum = find_regno ("st0");
151 int xmm0_regnum = find_regno ("xmm0");
152 unsigned long val, val2;
154 for (i = 0; i < 8; i++)
155 collect_register (i + st0_regnum, ((char *) &fp->st_space[0]) + i * 16);
156 for (i = 0; i < num_xmm_registers; i++)
157 collect_register (i + xmm0_regnum, ((char *) &fp->xmm_space[0]) + i * 16);
159 collect_register_by_name ("fioff", &fp->fioff);
160 collect_register_by_name ("fooff", &fp->fooff);
161 collect_register_by_name ("mxcsr", &fp->mxcsr);
163 /* This one's 11 bits... */
164 collect_register_by_name ("fop", &val2);
165 fp->fop = (val2 & 0x7FF) | (fp->fop & 0xF800);
167 /* Some registers are 16-bit. */
168 collect_register_by_name ("fctrl", &val);
169 *(unsigned short *) &fp->fctrl = val;
171 collect_register_by_name ("fstat", &val);
172 val &= 0xFFFF;
173 *(unsigned short *) &fp->fstat = val;
175 /* Convert to the simplifed tag form stored in fxsave data. */
176 collect_register_by_name ("ftag", &val);
177 val &= 0xFFFF;
178 for (i = 7; i >= 0; i--)
180 int tag = (val >> (i * 2)) & 3;
182 if (tag != 3)
183 val2 |= (1 << i);
185 *(unsigned short *) &fp->ftag = val2;
187 collect_register_by_name ("fiseg", &val);
188 val &= 0xFFFF;
189 *(unsigned short *) &fp->fiseg = val;
191 collect_register_by_name ("foseg", &val);
192 val &= 0xFFFF;
193 *(unsigned short *) &fp->foseg = val;
196 static int
197 i387_ftag (struct i387_fxsave *fp, int regno)
199 unsigned char *raw = &fp->st_space[regno * 16];
200 unsigned int exponent;
201 unsigned long fraction[2];
202 int integer;
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]);
210 if (exponent == 0x7fff)
212 /* Special. */
213 return (2);
215 else if (exponent == 0x0000)
217 if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
219 /* Zero. */
220 return (1);
222 else
224 /* Special. */
225 return (2);
228 else
230 if (integer)
232 /* Valid. */
233 return (0);
235 else
237 /* Special. */
238 return (2);
243 void
244 i387_fxsave_to_cache (const void *buf)
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;
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);
257 supply_register_by_name ("fioff", &fp->fioff);
258 supply_register_by_name ("fooff", &fp->fooff);
259 supply_register_by_name ("mxcsr", &fp->mxcsr);
261 /* Some registers are 16-bit. */
262 val = fp->fctrl & 0xFFFF;
263 supply_register_by_name ("fctrl", &val);
265 val = fp->fstat & 0xFFFF;
266 supply_register_by_name ("fstat", &val);
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--)
273 int tag;
274 if (val & (1 << i))
275 tag = i387_ftag (fp, (i + 8 - top) % 8);
276 else
277 tag = 3;
278 val |= tag << (2 * i);
280 supply_register_by_name ("ftag", &val);
282 val = fp->fiseg & 0xFFFF;
283 supply_register_by_name ("fiseg", &val);
285 val = fp->foseg & 0xFFFF;
286 supply_register_by_name ("foseg", &val);
288 val = (fp->fop) & 0x7FF;
289 supply_register_by_name ("fop", &val);