1 /* Low level interface to I386 running mach 3.0.
2 Copyright (C) 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include <mach/message.h>
27 #include <mach/exception.h>
28 #include <mach_error.h>
30 /* Hmmm... Should this not be here?
31 * Now for i386_float_info() target_has_execution
35 /* This mess is duplicated in bfd/i386mach3.h
37 * This is an ugly way to hack around the incorrect
38 * definition of UPAGES in i386/machparam.h.
40 * The definition should specify the size reserved
41 * for "struct user" in core files in PAGES,
42 * but instead it gives it in 512-byte core-clicks
45 #include <sys/param.h>
47 #define UAREA_SIZE ctob(UPAGES)
49 #define UAREA_SIZE (NBPG*UPAGES)
51 FIXME!! UPAGES is neither 2 nor 16
54 /* @@@ Should move print_387_status() to i387-tdep.c */
55 extern void print_387_control_word (); /* i387-tdep.h */
56 extern void print_387_status_word ();
58 extern struct ext_format ext_format_i387;
60 #define private static
63 /* Find offsets to thread states at compile time.
64 * If your compiler does not grok this, calculate offsets
65 * offsets yourself and use them (or get a compatible compiler :-)
68 #define REG_OFFSET(reg) (int)(&((struct i386_thread_state *)0)->reg)
70 /* at reg_offset[i] is the offset to the i386_thread_state
71 * location where the gdb registers[i] is stored.
74 static int reg_offset[] =
76 REG_OFFSET(eax), REG_OFFSET(ecx), REG_OFFSET(edx), REG_OFFSET(ebx),
77 REG_OFFSET(uesp), REG_OFFSET(ebp), REG_OFFSET(esi), REG_OFFSET(edi),
78 REG_OFFSET(eip), REG_OFFSET(efl), REG_OFFSET(cs), REG_OFFSET(ss),
79 REG_OFFSET(ds), REG_OFFSET(es), REG_OFFSET(fs), REG_OFFSET(gs)
82 #define REG_ADDRESS(state,regnum) ((char *)(state)+reg_offset[regnum])
84 /* Fetch COUNT contiguous registers from thread STATE starting from REGNUM
85 * Caller knows that the regs handled in one transaction are of same size.
87 #define FETCH_REGS(state, regnum, count) \
88 bcopy (REG_ADDRESS (state, regnum), \
89 ®isters[REGISTER_BYTE (regnum)], \
90 count*sizeof (REGISTER_TYPE))
92 /* Store COUNT contiguous registers to thread STATE starting from REGNUM */
93 #define STORE_REGS(state, regnum, count) \
94 bcopy (®isters[REGISTER_BYTE (regnum)], \
95 REG_ADDRESS (state, regnum), \
96 count*sizeof (REGISTER_TYPE))
99 * Fetch inferiors registers for gdb.
100 * REGNO specifies which (as gdb views it) register, -1 for all.
104 fetch_inferior_registers (regno)
108 thread_state_data_t state;
109 unsigned int stateCnt = i386_THREAD_STATE_COUNT;
112 if (! MACH_PORT_VALID (current_thread))
113 error ("fetch inferior registers: Invalid thread");
115 if (must_suspend_thread)
116 setup_thread (current_thread, 1);
118 ret = thread_get_state (current_thread,
123 if (ret != KERN_SUCCESS)
124 message ("fetch_inferior_registers: %s ",
125 mach_error_string (ret));
127 /* It may be more effective to store validate all of them,
128 * since we fetched them all anyway
130 else if (regno != -1)
131 supply_register (regno, (char *)state+reg_offset[regno]);
135 for (index = 0; index < NUM_REGS; index++)
136 supply_register (index, (char *)state+reg_offset[index]);
139 if (must_suspend_thread)
140 setup_thread (current_thread, 0);
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
147 * On mach3 all registers are always saved in one call.
150 store_inferior_registers (regno)
154 thread_state_data_t state;
155 unsigned int stateCnt = i386_THREAD_STATE_COUNT;
158 if (! MACH_PORT_VALID (current_thread))
159 error ("store inferior registers: Invalid thread");
161 if (must_suspend_thread)
162 setup_thread (current_thread, 1);
164 /* Fetch the state of the current thread */
165 ret = thread_get_state (current_thread,
170 if (ret != KERN_SUCCESS)
172 message ("store_inferior_registers (get): %s",
173 mach_error_string (ret));
174 if (must_suspend_thread)
175 setup_thread (current_thread, 0);
179 /* move gdb's registers to thread's state
181 * Since we save all registers anyway, save the ones
182 * that gdb thinks are valid (e.g. ignore the regno
187 STORE_REGS (state, regno, 1);
191 for (index = 0; index < NUM_REGS; index++)
192 STORE_REGS (state, index, 1);
195 /* Write gdb's current view of register to the thread
197 ret = thread_set_state (current_thread,
200 i386_THREAD_STATE_COUNT);
202 if (ret != KERN_SUCCESS)
203 message ("store_inferior_registers (set): %s",
204 mach_error_string (ret));
206 if (must_suspend_thread)
207 setup_thread (current_thread, 0);
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?
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.
220 * Called by coredep.c(fetch_core_registers)
224 register_addr (regno, blockend)
230 if (regno < 0 || regno >= NUM_REGS)
231 error ("Invalid register number %d.", regno);
233 /* UAREA_SIZE == 8 kB in i386 */
234 addr = (unsigned int)REG_ADDRESS (UAREA_SIZE - sizeof(struct i386_thread_state), regno);
240 * gdb/i386-xdep.c. Modifications for Mach 3.0.
242 * i387 status dumper. See also i387-tdep.c
246 unsigned short control;
248 unsigned short status;
253 unsigned short code_seg;
254 unsigned short opcode;
255 unsigned long operand;
256 unsigned short operand_seg;
258 unsigned char regs[8][10];
260 /* This routine is machine independent?
261 * Should move it to i387-tdep.c but you need to export struct env387
264 print_387_status (status, ep)
265 unsigned short status;
274 bothstatus = ((status != 0) && (ep->status != 0));
279 print_387_status_word (status);
286 print_387_status_word (ep->status);
289 print_387_control_word (ep->control);
290 printf ("last exception: ");
291 printf ("opcode %s; ", local_hex_string(ep->opcode));
292 printf ("pc %s:", local_hex_string(ep->code_seg));
293 printf ("%s; ", local_hex_string(ep->eip));
294 printf ("operand %s", local_hex_string(ep->operand_seg));
295 printf (":%s\n", local_hex_string(ep->operand));
297 top = (ep->status >> 11) & 7;
299 printf ("regno tag msb lsb value\n");
300 for (fpreg = 7; fpreg >= 0; fpreg--)
304 printf ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
306 switch ((ep->tag >> (fpreg * 2)) & 3)
308 case 0: printf ("valid "); break;
309 case 1: printf ("zero "); break;
310 case 2: printf ("trap "); break;
311 case 3: printf ("empty "); break;
313 for (i = 9; i >= 0; i--)
314 printf ("%02x", ep->regs[fpreg][i]);
316 ieee_extended_to_double (&ext_format_i387, (char *)ep->regs[fpreg],
318 printf (" %g\n", val);
321 printf ("warning: reserved0 is %s\n", local_hex_string(ep->r0));
323 printf ("warning: reserved1 is %s\n", local_hex_string(ep->r1));
325 printf ("warning: reserved2 is %s\n", local_hex_string(ep->r2));
327 printf ("warning: reserved3 is %s\n", local_hex_string(ep->r3));
331 * values that go into fp_kind (from <i386/fpreg.h>)
333 #define FP_NO 0 /* no fp chip, no emulator (no fp support) */
334 #define FP_SW 1 /* no fp chip, using software emulator */
335 #define FP_HW 2 /* chip present bit */
336 #define FP_287 2 /* 80287 chip present */
337 #define FP_387 3 /* 80387 chip present */
339 typedef struct fpstate {
341 unsigned char state[FP_STATE_BYTES]; /* "hardware" state */
343 struct env387 state; /* Actually this */
345 int status; /* Duplicate status */
348 /* Mach 3 specific routines.
351 get_i387_state (fstate)
352 struct fpstate *fstate;
355 thread_state_data_t state;
356 unsigned int fsCnt = i386_FLOAT_STATE_COUNT;
357 struct i386_float_state *fsp;
359 ret = thread_get_state (current_thread,
364 if (ret != KERN_SUCCESS)
366 message ("Can not get live floating point state: %s",
367 mach_error_string (ret));
371 fsp = (struct i386_float_state *)state;
372 /* The 387 chip (also 486 counts) or a software emulator? */
373 if (!fsp->initialized || (fsp->fpkind != FP_387 && fsp->fpkind != FP_SW))
376 /* Clear the target then copy thread's float state there.
377 Make a copy of the status word, for some reason?
379 bzero (fstate, sizeof(struct fpstate));
381 fstate->status = fsp->exc_status;
383 bcopy ((char *)&fsp->hw_state, fstate->state, FP_STATE_BYTES);
389 get_i387_core_state (fstate)
390 struct fpstate *fstate;
392 /* Not implemented yet. Core files do not contain float state. */
397 * This is called by "info float" command
400 i386_mach3_float_info()
402 char buf [sizeof (struct fpstate) + 2 * sizeof (int)];
403 boolean_t valid = FALSE;
406 if (target_has_execution)
407 valid = get_i387_state (buf);
409 else if (WE HAVE CORE FILE) /* @@@@ Core files not supported */
410 valid = get_i387_core_state (buf);
415 message("no floating point status saved");
419 fps = (fpstate_t) buf;
421 print_387_status (fps->status, (struct env387 *)fps->state);