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