1 /* Low level interface to I386 running the GNU Hurd
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "floatformat.h"
27 #include <mach/message.h>
28 #include <mach/exception.h>
29 #include <mach_error.h>
33 /* Hmmm... Should this not be here?
34 * Now for i386_float_info() target_has_execution
38 /* @@@ Should move print_387_status() to i387-tdep.c */
39 extern void print_387_control_word (); /* i387-tdep.h */
40 extern void print_387_status_word ();
42 /* Find offsets to thread states at compile time.
43 * If your compiler does not grok this, calculate offsets
44 * offsets yourself and use them (or get a compatible compiler :-)
47 #define REG_OFFSET(reg) (int)(&((struct i386_thread_state *)0)->reg)
49 /* at reg_offset[i] is the offset to the i386_thread_state
50 * location where the gdb registers[i] is stored.
53 static int reg_offset
[] =
55 REG_OFFSET(eax
), REG_OFFSET(ecx
), REG_OFFSET(edx
), REG_OFFSET(ebx
),
56 REG_OFFSET(uesp
), REG_OFFSET(ebp
), REG_OFFSET(esi
), REG_OFFSET(edi
),
57 REG_OFFSET(eip
), REG_OFFSET(efl
), REG_OFFSET(cs
), REG_OFFSET(ss
),
58 REG_OFFSET(ds
), REG_OFFSET(es
), REG_OFFSET(fs
), REG_OFFSET(gs
)
61 #define REG_ADDR(state,regnum) ((char *)(state)+reg_offset[regnum])
63 /* Fetch COUNT contiguous registers from thread STATE starting from REGNUM
64 * Caller knows that the regs handled in one transaction are of same size.
66 #define FETCH_REGS(state, regnum, count) \
67 memcpy (®isters[REGISTER_BYTE (regnum)], \
68 REG_ADDR (state, regnum), \
69 count * REGISTER_RAW_SIZE (regnum))
71 /* Store COUNT contiguous registers to thread STATE starting from REGNUM */
72 #define STORE_REGS(state, regnum, count) \
73 memcpy (REG_ADDR (state, regnum), \
74 ®isters[REGISTER_BYTE (regnum)], \
75 count * REGISTER_RAW_SIZE (regnum))
78 * Fetch inferiors registers for gdb.
79 * REG specifies which (as gdb views it) register, -1 for all.
82 gnu_fetch_registers (int reg
)
85 struct proc
*thread
= inf_tid_to_thread (current_inferior
, inferior_pid
);
88 error ("fetch inferior registers: %d: Invalid thread", inferior_pid
);
90 state
= proc_get_state (thread
, 0);
93 warning ("Couldn't fetch register %s.", reg_names
[reg
]);
96 proc_debug (thread
, "fetching register: %s", reg_names
[reg
]);
97 supply_register (reg
, REG_ADDR(state
, reg
));
98 thread
->fetched_regs
|= (1 << reg
);
102 proc_debug (thread
, "fetching all registers");
103 for (reg
= 0; reg
< NUM_REGS
; reg
++)
104 supply_register (reg
, REG_ADDR(state
, reg
));
105 thread
->fetched_regs
= ~0;
109 /* Store our register values back into the inferior.
110 * If REG is -1, do this for all registers.
111 * Otherwise, REG specifies which register
113 * On mach3 all registers are always saved in one call.
116 gnu_store_registers (reg
)
119 int was_aborted
, was_valid
;
120 thread_state_t state
;
121 thread_state_data_t old_state
;
122 struct proc
*thread
= inf_tid_to_thread (current_inferior
, inferior_pid
);
125 error ("store inferior registers: %d: Invalid thread", inferior_pid
);
127 proc_debug (thread
, "storing register %s.", reg_names
[reg
]);
129 was_aborted
= thread
->aborted
;
130 was_valid
= thread
->state_valid
;
131 if (! was_aborted
&& was_valid
)
132 bcopy (&thread
->state
, &old_state
, sizeof (old_state
));
134 state
= proc_get_state (thread
, 1);
137 warning ("Couldn't store register %s.", reg_names
[reg
]);
140 if (! was_aborted
&& was_valid
)
141 /* See which registers have changed after aborting the thread. */
144 for (check_reg
= 0; check_reg
< NUM_REGS
; check_reg
++)
145 if ((thread
->fetched_regs
& (1 << check_reg
))
146 && bcmp (REG_ADDR (&old_state
, check_reg
),
147 REG_ADDR (state
, check_reg
),
148 REGISTER_RAW_SIZE (check_reg
)))
149 /* Register CHECK_REG has changed! Ack! */
151 warning ("Register %s changed after thread was aborted.",
152 reg_names
[check_reg
]);
153 if (reg
>= 0 && reg
!= check_reg
)
154 /* Update gdb's copy of the register. */
155 supply_register (check_reg
, REG_ADDR (state
, check_reg
));
157 warning ("... also writing this register! Suspicious...");
163 proc_debug (thread
, "storing register: %s", reg_names
[reg
]);
164 STORE_REGS (state
, reg
, 1);
168 proc_debug (thread
, "storing all registers");
169 for (reg
= 0; reg
< NUM_REGS
; reg
++)
170 STORE_REGS (state
, reg
, 1);
175 /* jtv@hut.fi: I copied and modified this 387 code from
176 * gdb/i386-xdep.c. Modifications for Mach 3.0.
178 * i387 status dumper. See also i387-tdep.c
182 unsigned short control
;
184 unsigned short status
;
189 unsigned short code_seg
;
190 unsigned short opcode
;
191 unsigned long operand
;
192 unsigned short operand_seg
;
194 unsigned char regs
[8][10];
196 /* This routine is machine independent?
197 * Should move it to i387-tdep.c but you need to export struct env387
200 print_387_status (status
, ep
)
201 unsigned short status
;
210 bothstatus
= ((status
!= 0) && (ep
->status
!= 0));
214 printf_unfiltered ("u: ");
215 print_387_status_word (status
);
221 printf_unfiltered ("e: ");
222 print_387_status_word (ep
->status
);
225 print_387_control_word (ep
->control
);
226 printf_unfiltered ("last exception: ");
227 printf_unfiltered ("opcode %s; ", local_hex_string(ep
->opcode
));
228 printf_unfiltered ("pc %s:", local_hex_string(ep
->code_seg
));
229 printf_unfiltered ("%s; ", local_hex_string(ep
->eip
));
230 printf_unfiltered ("operand %s", local_hex_string(ep
->operand_seg
));
231 printf_unfiltered (":%s\n", local_hex_string(ep
->operand
));
233 top
= (ep
->status
>> 11) & 7;
235 printf_unfiltered ("regno tag msb lsb value\n");
236 for (fpreg
= 7; fpreg
>= 0; fpreg
--)
240 printf_unfiltered ("%s %d: ", fpreg
== top
? "=>" : " ", fpreg
);
242 switch ((ep
->tag
>> (fpreg
* 2)) & 3)
244 case 0: printf_unfiltered ("valid "); break;
245 case 1: printf_unfiltered ("zero "); break;
246 case 2: printf_unfiltered ("trap "); break;
247 case 3: printf_unfiltered ("empty "); break;
249 for (i
= 9; i
>= 0; i
--)
250 printf_unfiltered ("%02x", ep
->regs
[fpreg
][i
]);
252 floatformat_to_double (&floatformat_i387_ext
, (char *)ep
->regs
[fpreg
],
254 printf_unfiltered (" %g\n", val
);
257 printf_unfiltered ("warning: reserved0 is %s\n", local_hex_string(ep
->r0
));
259 printf_unfiltered ("warning: reserved1 is %s\n", local_hex_string(ep
->r1
));
261 printf_unfiltered ("warning: reserved2 is %s\n", local_hex_string(ep
->r2
));
263 printf_unfiltered ("warning: reserved3 is %s\n", local_hex_string(ep
->r3
));
267 * values that go into fp_kind (from <i386/fpreg.h>)
269 #define FP_NO 0 /* no fp chip, no emulator (no fp support) */
270 #define FP_SW 1 /* no fp chip, using software emulator */
271 #define FP_HW 2 /* chip present bit */
272 #define FP_287 2 /* 80287 chip present */
273 #define FP_387 3 /* 80387 chip present */
275 typedef struct fpstate
{
277 unsigned char state
[FP_STATE_BYTES
]; /* "hardware" state */
279 struct env387 state
; /* Actually this */
281 int status
; /* Duplicate status */
284 /* Mach 3 specific routines.
287 get_i387_state (fstate
)
288 struct fpstate
*fstate
;
291 thread_state_data_t state
;
292 unsigned int fsCnt
= i386_FLOAT_STATE_COUNT
;
293 struct i386_float_state
*fsp
;
294 struct proc
*thread
= inf_tid_to_thread (current_inferior
, inferior_pid
);
297 error ("get_i387_state: Invalid thread");
299 proc_abort (thread
, 0); /* Make sure THREAD's in a reasonable state. */
301 err
= thread_get_state (thread
->port
, i386_FLOAT_STATE
, state
, &fsCnt
);
304 warning ("Can not get live floating point state: %s",
305 mach_error_string (err
));
309 fsp
= (struct i386_float_state
*)state
;
310 /* The 387 chip (also 486 counts) or a software emulator? */
311 if (!fsp
->initialized
|| (fsp
->fpkind
!= FP_387
&& fsp
->fpkind
!= FP_SW
))
314 /* Clear the target then copy thread's float state there.
315 Make a copy of the status word, for some reason?
317 memset (fstate
, 0, sizeof (struct fpstate
));
319 fstate
->status
= fsp
->exc_status
;
321 memcpy (fstate
->state
, (char *)&fsp
->hw_state
, FP_STATE_BYTES
);
327 * This is called by "info float" command
330 i386_mach3_float_info()
332 char buf
[sizeof (struct fpstate
) + 2 * sizeof (int)];
336 if (target_has_execution
)
337 valid
= get_i387_state (buf
);
341 warning ("no floating point status saved");
345 fps
= (fpstate_t
) buf
;
347 print_387_status (fps
->status
, (struct env387
*)fps
->state
);