* config/m68k/nm-hp300bsd.h: Correctly identify 4.3BSD vs 4.4BSD.
[deliverable/binutils-gdb.git] / gdb / i386m3-nat.c
CommitLineData
c2d751d5
JK
1/* Low level interface to I386 running mach 3.0.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "defs.h"
21#include "inferior.h"
22
23#include <stdio.h>
24
25#include <mach.h>
26#include <mach/message.h>
27#include <mach/exception.h>
28#include <mach_error.h>
29
30/* Hmmm... Should this not be here?
31 * Now for i386_float_info() target_has_execution
32 */
33#include <target.h>
34
35/* This mess is duplicated in bfd/i386mach3.h
36 *
37 * This is an ugly way to hack around the incorrect
38 * definition of UPAGES in i386/machparam.h.
39 *
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
43 * for i386 and i860.
44 */
45#include <sys/param.h>
46#if UPAGES == 16
47#define UAREA_SIZE ctob(UPAGES)
48#elif UPAGES == 2
49#define UAREA_SIZE (NBPG*UPAGES)
50#else
51FIXME!! UPAGES is neither 2 nor 16
52#endif
53
54/* @@@ Should move print_387_status() to i387-tdep.c */
55extern void print_387_control_word (); /* i387-tdep.h */
56extern void print_387_status_word ();
57
58extern struct ext_format ext_format_i387;
59
60#define private static
61
62\f
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 :-)
66 */
67
68#define REG_OFFSET(reg) (int)(&((struct i386_thread_state *)0)->reg)
69
70/* at reg_offset[i] is the offset to the i386_thread_state
71 * location where the gdb registers[i] is stored.
72 */
73
74static int reg_offset[] =
75{
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)
80};
81
82#define REG_ADDRESS(state,regnum) ((char *)(state)+reg_offset[regnum])
83
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.
86 */
87#define FETCH_REGS(state, regnum, count) \
a6e0dae9
JK
88 memcpy (&registers[REGISTER_BYTE (regnum)], \
89 REG_ADDRESS (state, regnum), \
90 count*sizeof (REGISTER_TYPE))
c2d751d5
JK
91
92/* Store COUNT contiguous registers to thread STATE starting from REGNUM */
93#define STORE_REGS(state, regnum, count) \
a6e0dae9
JK
94 memcpy (REG_ADDRESS (state, regnum), \
95 &registers[REGISTER_BYTE (regnum)], \
96 count*sizeof (REGISTER_TYPE))
c2d751d5
JK
97\f
98/*
99 * Fetch inferiors registers for gdb.
100 * REGNO specifies which (as gdb views it) register, -1 for all.
101 */
102
103void
104fetch_inferior_registers (regno)
105 int regno;
106{
107 kern_return_t ret;
108 thread_state_data_t state;
109 unsigned int stateCnt = i386_THREAD_STATE_COUNT;
110 int index;
111
112 if (! MACH_PORT_VALID (current_thread))
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 message ("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)
131 supply_register (regno, (char *)state+reg_offset[regno]);
132#endif
133 else
134 {
135 for (index = 0; index < NUM_REGS; index++)
136 supply_register (index, (char *)state+reg_offset[index]);
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
150store_inferior_registers (regno)
151 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 message ("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 message ("store_inferior_registers (set): %s",
204 mach_error_string (ret));
205
206 if (must_suspend_thread)
207 setup_thread (current_thread, 0);
208}
209
210\f
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 coredep.c(fetch_core_registers)
221 */
222
223unsigned int
224register_addr (regno, blockend)
225 int regno;
226 int blockend;
227{
228 unsigned int addr;
229
230 if (regno < 0 || regno >= NUM_REGS)
231 error ("Invalid register number %d.", regno);
232
233 /* UAREA_SIZE == 8 kB in i386 */
234 addr = (unsigned int)REG_ADDRESS (UAREA_SIZE - sizeof(struct i386_thread_state), regno);
235
236 return addr;
237}
238
239/* jtv@hut.fi: I copied and modified this 387 code from
240 * gdb/i386-xdep.c. Modifications for Mach 3.0.
241 *
242 * i387 status dumper. See also i387-tdep.c
243 */
244struct env387
245{
246 unsigned short control;
247 unsigned short r0;
248 unsigned short status;
249 unsigned short r1;
250 unsigned short tag;
251 unsigned short r2;
252 unsigned long eip;
253 unsigned short code_seg;
254 unsigned short opcode;
255 unsigned long operand;
256 unsigned short operand_seg;
257 unsigned short r3;
258 unsigned char regs[8][10];
259};
260/* This routine is machine independent?
261 * Should move it to i387-tdep.c but you need to export struct env387
262 */
263private
264print_387_status (status, ep)
265 unsigned short status;
266 struct env387 *ep;
267{
268 int i;
269 int bothstatus;
270 int top;
271 int fpreg;
272 unsigned char *p;
273
274 bothstatus = ((status != 0) && (ep->status != 0));
275 if (status != 0)
276 {
277 if (bothstatus)
199b2450 278 printf_unfiltered ("u: ");
c2d751d5
JK
279 print_387_status_word (status);
280 }
281
282 if (ep->status != 0)
283 {
284 if (bothstatus)
199b2450 285 printf_unfiltered ("e: ");
c2d751d5
JK
286 print_387_status_word (ep->status);
287 }
288
289 print_387_control_word (ep->control);
199b2450
TL
290 printf_unfiltered ("last exception: ");
291 printf_unfiltered ("opcode %s; ", local_hex_string(ep->opcode));
292 printf_unfiltered ("pc %s:", local_hex_string(ep->code_seg));
293 printf_unfiltered ("%s; ", local_hex_string(ep->eip));
294 printf_unfiltered ("operand %s", local_hex_string(ep->operand_seg));
295 printf_unfiltered (":%s\n", local_hex_string(ep->operand));
c2d751d5
JK
296
297 top = (ep->status >> 11) & 7;
298
199b2450 299 printf_unfiltered ("regno tag msb lsb value\n");
c2d751d5
JK
300 for (fpreg = 7; fpreg >= 0; fpreg--)
301 {
302 double val;
303
199b2450 304 printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
c2d751d5
JK
305
306 switch ((ep->tag >> (fpreg * 2)) & 3)
307 {
199b2450
TL
308 case 0: printf_unfiltered ("valid "); break;
309 case 1: printf_unfiltered ("zero "); break;
310 case 2: printf_unfiltered ("trap "); break;
311 case 3: printf_unfiltered ("empty "); break;
c2d751d5
JK
312 }
313 for (i = 9; i >= 0; i--)
199b2450 314 printf_unfiltered ("%02x", ep->regs[fpreg][i]);
c2d751d5
JK
315
316 ieee_extended_to_double (&ext_format_i387, (char *)ep->regs[fpreg],
317 &val);
199b2450 318 printf_unfiltered (" %g\n", val);
c2d751d5
JK
319 }
320 if (ep->r0)
199b2450 321 printf_unfiltered ("warning: reserved0 is %s\n", local_hex_string(ep->r0));
c2d751d5 322 if (ep->r1)
199b2450 323 printf_unfiltered ("warning: reserved1 is %s\n", local_hex_string(ep->r1));
c2d751d5 324 if (ep->r2)
199b2450 325 printf_unfiltered ("warning: reserved2 is %s\n", local_hex_string(ep->r2));
c2d751d5 326 if (ep->r3)
199b2450 327 printf_unfiltered ("warning: reserved3 is %s\n", local_hex_string(ep->r3));
c2d751d5
JK
328}
329
330/*
331 * values that go into fp_kind (from <i386/fpreg.h>)
332 */
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 */
338
339typedef struct fpstate {
340#if 1
341 unsigned char state[FP_STATE_BYTES]; /* "hardware" state */
342#else
343 struct env387 state; /* Actually this */
344#endif
345 int status; /* Duplicate status */
346} *fpstate_t;
347
348/* Mach 3 specific routines.
349 */
350private boolean_t
351get_i387_state (fstate)
352 struct fpstate *fstate;
353{
354 kern_return_t ret;
355 thread_state_data_t state;
356 unsigned int fsCnt = i386_FLOAT_STATE_COUNT;
357 struct i386_float_state *fsp;
358
359 ret = thread_get_state (current_thread,
360 i386_FLOAT_STATE,
361 state,
362 &fsCnt);
363
364 if (ret != KERN_SUCCESS)
365 {
366 message ("Can not get live floating point state: %s",
367 mach_error_string (ret));
368 return FALSE;
369 }
370
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))
374 return FALSE;
375
376 /* Clear the target then copy thread's float state there.
377 Make a copy of the status word, for some reason?
378 */
379 bzero (fstate, sizeof(struct fpstate));
380
381 fstate->status = fsp->exc_status;
382
a6e0dae9 383 memcpy (fstate->state, (char *)&fsp->hw_state, FP_STATE_BYTES);
c2d751d5
JK
384
385 return TRUE;
386}
387
388private boolean_t
389get_i387_core_state (fstate)
390 struct fpstate *fstate;
391{
392 /* Not implemented yet. Core files do not contain float state. */
393 return FALSE;
394}
395
396/*
397 * This is called by "info float" command
398 */
399void
400i386_mach3_float_info()
401{
402 char buf [sizeof (struct fpstate) + 2 * sizeof (int)];
403 boolean_t valid = FALSE;
404 fpstate_t fps;
405
406 if (target_has_execution)
407 valid = get_i387_state (buf);
408#if 0
409 else if (WE HAVE CORE FILE) /* @@@@ Core files not supported */
410 valid = get_i387_core_state (buf);
411#endif
412
413 if (!valid)
414 {
415 message("no floating point status saved");
416 return;
417 }
418
419 fps = (fpstate_t) buf;
420
421 print_387_status (fps->status, (struct env387 *)fps->state);
422}
This page took 0.053205 seconds and 4 git commands to generate.