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