OSDN Git Service

cce8790dc2ac5e96cfd2217aa1795ddfca89adc8
[pf3gnuchains/pf3gnuchains4x.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   for (i = 7; i >= 0; i--)
178     {
179       int tag = (val >> (i * 2)) & 3;
180
181       if (tag != 3)
182         val2 |= (1 << i);
183     }
184   *(unsigned short *) &fp->ftag = val2;
185
186   collect_register_by_name ("fiseg", &val);
187   val &= 0xFFFF;
188   *(unsigned short *) &fp->fiseg = val;
189
190   collect_register_by_name ("foseg", &val);
191   val &= 0xFFFF;
192   *(unsigned short *) &fp->foseg = val;
193 }
194
195 static int
196 i387_ftag (struct i387_fxsave *fp, int regno)
197 {
198   unsigned char *raw = &fp->st_space[regno * 16];
199   unsigned int exponent;
200   unsigned long fraction[2];
201   int integer;
202
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]);
208
209   if (exponent == 0x7fff)
210     {
211       /* Special.  */
212       return (2);
213     }
214   else if (exponent == 0x0000)
215     {
216       if (fraction[0] == 0x0000 && fraction[1] == 0x0000 && !integer)
217         {
218           /* Zero.  */
219           return (1);
220         }
221       else
222         {
223           /* Special.  */
224           return (2);
225         }
226     }
227   else
228     {
229       if (integer)
230         {
231           /* Valid.  */
232           return (0);
233         }
234       else
235         {
236           /* Special.  */
237           return (2);
238         }
239     }
240 }
241
242 void
243 i387_fxsave_to_cache (const void *buf)
244 {
245   struct i387_fxsave *fp = (struct i387_fxsave *) buf;
246   int i, top;
247   int st0_regnum = find_regno ("st0");
248   int xmm0_regnum = find_regno ("xmm0");
249   unsigned long val;
250
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);
255
256   supply_register_by_name ("fioff", &fp->fioff);
257   supply_register_by_name ("fooff", &fp->fooff);
258   supply_register_by_name ("mxcsr", &fp->mxcsr);
259   
260   /* Some registers are 16-bit.  */
261   val = fp->fctrl & 0xFFFF;
262   supply_register_by_name ("fctrl", &val);
263
264   val = fp->fstat & 0xFFFF;
265   supply_register_by_name ("fstat", &val);
266
267   /* Generate the form of ftag data that GDB expects.  */
268   top = (fp->fstat >> 11) & 0x7;
269   val = 0;
270   for (i = 7; i >= 0; i--)
271     {
272       int tag;
273       if (val & (1 << i))
274         tag = i387_ftag (fp, (i + 8 - top) % 8);
275       else
276         tag = 3;
277       val |= tag << (2 * i);
278     }
279   supply_register_by_name ("ftag", &val);
280
281   val = fp->fiseg & 0xFFFF;
282   supply_register_by_name ("fiseg", &val);
283
284   val = fp->foseg & 0xFFFF;
285   supply_register_by_name ("foseg", &val);
286
287   val = (fp->fop) & 0x7FF;
288   supply_register_by_name ("fop", &val);
289 }