2001-06-13 Michael Snyder <msnyder@redhat.com>
[deliverable/binutils-gdb.git] / gdb / i386m3-nat.c
1 /* Low level interface to I386 running mach 3.0.
2 Copyright 1992, 1993, 1994, 1996, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include "floatformat.h"
25 #include "regcache.h"
26
27 #include <stdio.h>
28
29 #include <mach.h>
30 #include <mach/message.h>
31 #include <mach/exception.h>
32 #include <mach_error.h>
33
34 /* Hmmm... Should this not be here?
35 * Now for i386_float_info() target_has_execution
36 */
37 #include <target.h>
38
39 /* This mess is duplicated in bfd/i386mach3.h
40
41 * This is an ugly way to hack around the incorrect
42 * definition of UPAGES in i386/machparam.h.
43 *
44 * The definition should specify the size reserved
45 * for "struct user" in core files in PAGES,
46 * but instead it gives it in 512-byte core-clicks
47 * for i386 and i860.
48 */
49 #include <sys/param.h>
50 #if UPAGES == 16
51 #define UAREA_SIZE ctob(UPAGES)
52 #elif UPAGES == 2
53 #define UAREA_SIZE (NBPG*UPAGES)
54 #else
55 FIXME ! !UPAGES is neither 2 nor 16
56 #endif
57
58 /* @@@ Should move print_387_status() to i387-tdep.c */
59 extern void print_387_control_word (); /* i387-tdep.h */
60 extern void print_387_status_word ();
61
62 #define private static
63 \f
64
65 /* Find offsets to thread states at compile time.
66 * If your compiler does not grok this, calculate offsets
67 * offsets yourself and use them (or get a compatible compiler :-)
68 */
69
70 #define REG_OFFSET(reg) (int)(&((struct i386_thread_state *)0)->reg)
71
72 /* at reg_offset[i] is the offset to the i386_thread_state
73 * location where the gdb registers[i] is stored.
74 */
75
76 static int reg_offset[] =
77 {
78 REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
79 REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
80 REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
81 REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
82 };
83
84 #define REG_ADDRESS(state,regnum) ((char *)(state)+reg_offset[regnum])
85
86 /* Fetch COUNT contiguous registers from thread STATE starting from REGNUM
87 * Caller knows that the regs handled in one transaction are of same size.
88 */
89 #define FETCH_REGS(state, regnum, count) \
90 memcpy (&registers[REGISTER_BYTE (regnum)], \
91 REG_ADDRESS (state, regnum), \
92 count*REGISTER_SIZE)
93
94 /* Store COUNT contiguous registers to thread STATE starting from REGNUM */
95 #define STORE_REGS(state, regnum, count) \
96 memcpy (REG_ADDRESS (state, regnum), \
97 &registers[REGISTER_BYTE (regnum)], \
98 count*REGISTER_SIZE)
99 \f
100 /*
101 * Fetch inferiors registers for gdb.
102 * REGNO specifies which (as gdb views it) register, -1 for all.
103 */
104
105 void
106 fetch_inferior_registers (int regno)
107 {
108 kern_return_t ret;
109 thread_state_data_t state;
110 unsigned int stateCnt = i386_THREAD_STATE_COUNT;
111 int index;
112
113 if (!MACH_PORT_VALID (current_thread))
114 error ("fetch inferior registers: Invalid thread");
115
116 if (must_suspend_thread)
117 setup_thread (current_thread, 1);
118
119 ret = thread_get_state (current_thread,
120 i386_THREAD_STATE,
121 state,
122 &stateCnt);
123
124 if (ret != KERN_SUCCESS)
125 warning ("fetch_inferior_registers: %s ",
126 mach_error_string (ret));
127 #if 0
128 /* It may be more effective to store validate all of them,
129 * since we fetched them all anyway
130 */
131 else if (regno != -1)
132 supply_register (regno, (char *) state + reg_offset[regno]);
133 #endif
134 else
135 {
136 for (index = 0; index < NUM_REGS; index++)
137 supply_register (index, (char *) state + reg_offset[index]);
138 }
139
140 if (must_suspend_thread)
141 setup_thread (current_thread, 0);
142 }
143 \f
144 /* Store our register values back into the inferior.
145 * If REGNO is -1, do this for all registers.
146 * Otherwise, REGNO specifies which register
147 *
148 * On mach3 all registers are always saved in one call.
149 */
150 void
151 store_inferior_registers (int regno)
152 {
153 kern_return_t ret;
154 thread_state_data_t state;
155 unsigned int stateCnt = i386_THREAD_STATE_COUNT;
156 register int index;
157
158 if (!MACH_PORT_VALID (current_thread))
159 error ("store inferior registers: Invalid thread");
160
161 if (must_suspend_thread)
162 setup_thread (current_thread, 1);
163
164 /* Fetch the state of the current thread */
165 ret = thread_get_state (current_thread,
166 i386_THREAD_STATE,
167 state,
168 &stateCnt);
169
170 if (ret != KERN_SUCCESS)
171 {
172 warning ("store_inferior_registers (get): %s",
173 mach_error_string (ret));
174 if (must_suspend_thread)
175 setup_thread (current_thread, 0);
176 return;
177 }
178
179 /* move gdb's registers to thread's state
180
181 * Since we save all registers anyway, save the ones
182 * that gdb thinks are valid (e.g. ignore the regno
183 * parameter)
184 */
185 #if 0
186 if (regno != -1)
187 STORE_REGS (state, regno, 1);
188 else
189 #endif
190 {
191 for (index = 0; index < NUM_REGS; index++)
192 STORE_REGS (state, index, 1);
193 }
194
195 /* Write gdb's current view of register to the thread
196 */
197 ret = thread_set_state (current_thread,
198 i386_THREAD_STATE,
199 state,
200 i386_THREAD_STATE_COUNT);
201
202 if (ret != KERN_SUCCESS)
203 warning ("store_inferior_registers (set): %s",
204 mach_error_string (ret));
205
206 if (must_suspend_thread)
207 setup_thread (current_thread, 0);
208 }
209 \f
210
211
212 /* Return the address in the core dump or inferior of register REGNO.
213 * BLOCKEND should be the address of the end of the UPAGES area read
214 * in memory, but it's not?
215 *
216 * Currently our UX server dumps the whole thread state to the
217 * core file. If your UX does something else, adapt the routine
218 * below to return the offset to the given register.
219 *
220 * Called by core-aout.c(fetch_core_registers)
221 */
222
223 CORE_ADDR
224 register_addr (int regno, CORE_ADDR blockend)
225 {
226 CORE_ADDR addr;
227
228 if (regno < 0 || regno >= NUM_REGS)
229 error ("Invalid register number %d.", regno);
230
231 /* UAREA_SIZE == 8 kB in i386 */
232 addr = (unsigned int) REG_ADDRESS (UAREA_SIZE - sizeof (struct i386_thread_state), regno);
233
234 return addr;
235 }
236
237 /* jtv@hut.fi: I copied and modified this 387 code from
238 * gdb/i386-xdep.c. Modifications for Mach 3.0.
239 *
240 * i387 status dumper. See also i387-tdep.c
241 */
242 struct env387
243 {
244 unsigned short control;
245 unsigned short r0;
246 unsigned short status;
247 unsigned short r1;
248 unsigned short tag;
249 unsigned short r2;
250 unsigned long eip;
251 unsigned short code_seg;
252 unsigned short opcode;
253 unsigned long operand;
254 unsigned short operand_seg;
255 unsigned short r3;
256 unsigned char regs[8][10];
257 };
258 /* This routine is machine independent?
259 * Should move it to i387-tdep.c but you need to export struct env387
260 */
261 private
262 print_387_status (unsigned short status, struct env387 *ep)
263 {
264 int i;
265 int bothstatus;
266 int top;
267 int fpreg;
268 unsigned char *p;
269
270 bothstatus = ((status != 0) && (ep->status != 0));
271 if (status != 0)
272 {
273 if (bothstatus)
274 printf_unfiltered ("u: ");
275 print_387_status_word (status);
276 }
277
278 if (ep->status != 0)
279 {
280 if (bothstatus)
281 printf_unfiltered ("e: ");
282 print_387_status_word (ep->status);
283 }
284
285 print_387_control_word (ep->control);
286 printf_unfiltered ("last exception: ");
287 printf_unfiltered ("opcode %s; ", local_hex_string (ep->opcode));
288 printf_unfiltered ("pc %s:", local_hex_string (ep->code_seg));
289 printf_unfiltered ("%s; ", local_hex_string (ep->eip));
290 printf_unfiltered ("operand %s", local_hex_string (ep->operand_seg));
291 printf_unfiltered (":%s\n", local_hex_string (ep->operand));
292
293 top = (ep->status >> 11) & 7;
294
295 printf_unfiltered ("regno tag msb lsb value\n");
296 for (fpreg = 7; fpreg >= 0; fpreg--)
297 {
298 double val;
299
300 printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
301
302 switch ((ep->tag >> (fpreg * 2)) & 3)
303 {
304 case 0:
305 printf_unfiltered ("valid ");
306 break;
307 case 1:
308 printf_unfiltered ("zero ");
309 break;
310 case 2:
311 printf_unfiltered ("trap ");
312 break;
313 case 3:
314 printf_unfiltered ("empty ");
315 break;
316 }
317 for (i = 9; i >= 0; i--)
318 printf_unfiltered ("%02x", ep->regs[fpreg][i]);
319
320 floatformat_to_double (&floatformat_i387_ext, (char *) ep->regs[fpreg],
321 &val);
322 printf_unfiltered (" %g\n", val);
323 }
324 if (ep->r0)
325 printf_unfiltered ("warning: reserved0 is %s\n", local_hex_string (ep->r0));
326 if (ep->r1)
327 printf_unfiltered ("warning: reserved1 is %s\n", local_hex_string (ep->r1));
328 if (ep->r2)
329 printf_unfiltered ("warning: reserved2 is %s\n", local_hex_string (ep->r2));
330 if (ep->r3)
331 printf_unfiltered ("warning: reserved3 is %s\n", local_hex_string (ep->r3));
332 }
333
334 /*
335 * values that go into fp_kind (from <i386/fpreg.h>)
336 */
337 #define FP_NO 0 /* no fp chip, no emulator (no fp support) */
338 #define FP_SW 1 /* no fp chip, using software emulator */
339 #define FP_HW 2 /* chip present bit */
340 #define FP_287 2 /* 80287 chip present */
341 #define FP_387 3 /* 80387 chip present */
342
343 typedef struct fpstate
344 {
345 #if 1
346 unsigned char state[FP_STATE_BYTES]; /* "hardware" state */
347 #else
348 struct env387 state; /* Actually this */
349 #endif
350 int status; /* Duplicate status */
351 }
352 *fpstate_t;
353
354 /* Mach 3 specific routines.
355 */
356 private boolean_t
357 get_i387_state (struct fpstate *fstate)
358 {
359 kern_return_t ret;
360 thread_state_data_t state;
361 unsigned int fsCnt = i386_FLOAT_STATE_COUNT;
362 struct i386_float_state *fsp;
363
364 ret = thread_get_state (current_thread,
365 i386_FLOAT_STATE,
366 state,
367 &fsCnt);
368
369 if (ret != KERN_SUCCESS)
370 {
371 warning ("Can not get live floating point state: %s",
372 mach_error_string (ret));
373 return FALSE;
374 }
375
376 fsp = (struct i386_float_state *) state;
377 /* The 387 chip (also 486 counts) or a software emulator? */
378 if (!fsp->initialized || (fsp->fpkind != FP_387 && fsp->fpkind != FP_SW))
379 return FALSE;
380
381 /* Clear the target then copy thread's float state there.
382 Make a copy of the status word, for some reason?
383 */
384 memset (fstate, 0, sizeof (struct fpstate));
385
386 fstate->status = fsp->exc_status;
387
388 memcpy (fstate->state, (char *) &fsp->hw_state, FP_STATE_BYTES);
389
390 return TRUE;
391 }
392
393 private boolean_t
394 get_i387_core_state (struct fpstate *fstate)
395 {
396 /* Not implemented yet. Core files do not contain float state. */
397 return FALSE;
398 }
399
400 /*
401 * This is called by "info float" command
402 */
403 void
404 i386_mach3_float_info (void)
405 {
406 char buf[sizeof (struct fpstate) + 2 * sizeof (int)];
407 boolean_t valid = FALSE;
408 fpstate_t fps;
409
410 if (target_has_execution)
411 valid = get_i387_state (buf);
412 #if 0
413 else if (WE HAVE CORE FILE) /* @@@@ Core files not supported */
414 valid = get_i387_core_state (buf);
415 #endif
416
417 if (!valid)
418 {
419 warning ("no floating point status saved");
420 return;
421 }
422
423 fps = (fpstate_t) buf;
424
425 print_387_status (fps->status, (struct env387 *) fps->state);
426 }
This page took 0.042235 seconds and 4 git commands to generate.