8892e1670234b30a9bfb81e3a14015b2827f6ae1
[binutils-gdb.git] / gdb / i386m3-nat.c
1 /* Low level interface to I386 running mach 3.0.
2 Copyright (C) 1992 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
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
51 FIXME!! UPAGES is neither 2 nor 16
52 #endif
53
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 ();
57
58 #define private static
59
60 \f
61 /* Find offsets to thread states at compile time.
62 * If your compiler does not grok this, calculate offsets
63 * offsets yourself and use them (or get a compatible compiler :-)
64 */
65
66 #define REG_OFFSET(reg) (int)(&((struct i386_thread_state *)0)->reg)
67
68 /* at reg_offset[i] is the offset to the i386_thread_state
69 * location where the gdb registers[i] is stored.
70 */
71
72 static int reg_offset[] =
73 {
74 REG_OFFSET(eax), REG_OFFSET(ecx), REG_OFFSET(edx), REG_OFFSET(ebx),
75 REG_OFFSET(uesp), REG_OFFSET(ebp), REG_OFFSET(esi), REG_OFFSET(edi),
76 REG_OFFSET(eip), REG_OFFSET(efl), REG_OFFSET(cs), REG_OFFSET(ss),
77 REG_OFFSET(ds), REG_OFFSET(es), REG_OFFSET(fs), REG_OFFSET(gs)
78 };
79
80 #define REG_ADDRESS(state,regnum) ((char *)(state)+reg_offset[regnum])
81
82 /* Fetch COUNT contiguous registers from thread STATE starting from REGNUM
83 * Caller knows that the regs handled in one transaction are of same size.
84 */
85 #define FETCH_REGS(state, regnum, count) \
86 memcpy (&registers[REGISTER_BYTE (regnum)], \
87 REG_ADDRESS (state, regnum), \
88 count*REGISTER_SIZE)
89
90 /* Store COUNT contiguous registers to thread STATE starting from REGNUM */
91 #define STORE_REGS(state, regnum, count) \
92 memcpy (REG_ADDRESS (state, regnum), \
93 &registers[REGISTER_BYTE (regnum)], \
94 count*REGISTER_SIZE)
95 \f
96 /*
97 * Fetch inferiors registers for gdb.
98 * REGNO specifies which (as gdb views it) register, -1 for all.
99 */
100
101 void
102 fetch_inferior_registers (regno)
103 int regno;
104 {
105 kern_return_t ret;
106 thread_state_data_t state;
107 unsigned int stateCnt = i386_THREAD_STATE_COUNT;
108 int index;
109
110 if (! MACH_PORT_VALID (current_thread))
111 error ("fetch inferior registers: Invalid thread");
112
113 if (must_suspend_thread)
114 setup_thread (current_thread, 1);
115
116 ret = thread_get_state (current_thread,
117 i386_THREAD_STATE,
118 state,
119 &stateCnt);
120
121 if (ret != KERN_SUCCESS)
122 message ("fetch_inferior_registers: %s ",
123 mach_error_string (ret));
124 #if 0
125 /* It may be more effective to store validate all of them,
126 * since we fetched them all anyway
127 */
128 else if (regno != -1)
129 supply_register (regno, (char *)state+reg_offset[regno]);
130 #endif
131 else
132 {
133 for (index = 0; index < NUM_REGS; index++)
134 supply_register (index, (char *)state+reg_offset[index]);
135 }
136
137 if (must_suspend_thread)
138 setup_thread (current_thread, 0);
139 }
140 \f
141 /* Store our register values back into the inferior.
142 * If REGNO is -1, do this for all registers.
143 * Otherwise, REGNO specifies which register
144 *
145 * On mach3 all registers are always saved in one call.
146 */
147 void
148 store_inferior_registers (regno)
149 int regno;
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
156 if (! MACH_PORT_VALID (current_thread))
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
168 if (ret != KERN_SUCCESS)
169 {
170 message ("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
178 *
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 {
189 for (index = 0; index < NUM_REGS; index++)
190 STORE_REGS (state, index, 1);
191 }
192
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);
199
200 if (ret != KERN_SUCCESS)
201 message ("store_inferior_registers (set): %s",
202 mach_error_string (ret));
203
204 if (must_suspend_thread)
205 setup_thread (current_thread, 0);
206 }
207
208 \f
209
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 coredep.c(fetch_core_registers)
219 */
220
221 unsigned int
222 register_addr (regno, blockend)
223 int regno;
224 int blockend;
225 {
226 unsigned int addr;
227
228 if (regno < 0 || regno >= NUM_REGS)
229 error ("Invalid register number %d.", regno);
230
231 /* UAREA_SIZE == 8 kB in i386 */
232 addr = (unsigned int)REG_ADDRESS (UAREA_SIZE - sizeof(struct i386_thread_state), regno);
233
234 return addr;
235 }
236
237 /* jtv@hut.fi: I copied and modified this 387 code from
238 * gdb/i386-xdep.c. Modifications for Mach 3.0.
239 *
240 * i387 status dumper. See also i387-tdep.c
241 */
242 struct env387
243 {
244 unsigned short control;
245 unsigned short r0;
246 unsigned short status;
247 unsigned short r1;
248 unsigned short tag;
249 unsigned short r2;
250 unsigned long eip;
251 unsigned short code_seg;
252 unsigned short opcode;
253 unsigned long operand;
254 unsigned short operand_seg;
255 unsigned short r3;
256 unsigned char regs[8][10];
257 };
258 /* This routine is machine independent?
259 * Should move it to i387-tdep.c but you need to export struct env387
260 */
261 private
262 print_387_status (status, ep)
263 unsigned short status;
264 struct env387 *ep;
265 {
266 int i;
267 int bothstatus;
268 int top;
269 int fpreg;
270 unsigned char *p;
271
272 bothstatus = ((status != 0) && (ep->status != 0));
273 if (status != 0)
274 {
275 if (bothstatus)
276 printf_unfiltered ("u: ");
277 print_387_status_word (status);
278 }
279
280 if (ep->status != 0)
281 {
282 if (bothstatus)
283 printf_unfiltered ("e: ");
284 print_387_status_word (ep->status);
285 }
286
287 print_387_control_word (ep->control);
288 printf_unfiltered ("last exception: ");
289 printf_unfiltered ("opcode %s; ", local_hex_string(ep->opcode));
290 printf_unfiltered ("pc %s:", local_hex_string(ep->code_seg));
291 printf_unfiltered ("%s; ", local_hex_string(ep->eip));
292 printf_unfiltered ("operand %s", local_hex_string(ep->operand_seg));
293 printf_unfiltered (":%s\n", local_hex_string(ep->operand));
294
295 top = (ep->status >> 11) & 7;
296
297 printf_unfiltered ("regno tag msb lsb value\n");
298 for (fpreg = 7; fpreg >= 0; fpreg--)
299 {
300 double val;
301
302 printf_unfiltered ("%s %d: ", fpreg == top ? "=>" : " ", fpreg);
303
304 switch ((ep->tag >> (fpreg * 2)) & 3)
305 {
306 case 0: printf_unfiltered ("valid "); break;
307 case 1: printf_unfiltered ("zero "); break;
308 case 2: printf_unfiltered ("trap "); break;
309 case 3: printf_unfiltered ("empty "); break;
310 }
311 for (i = 9; i >= 0; i--)
312 printf_unfiltered ("%02x", ep->regs[fpreg][i]);
313
314 floatformat_to_double (&floatformat_i387_ext, (char *)ep->regs[fpreg],
315 &val);
316 printf_unfiltered (" %g\n", val);
317 }
318 if (ep->r0)
319 printf_unfiltered ("warning: reserved0 is %s\n", local_hex_string(ep->r0));
320 if (ep->r1)
321 printf_unfiltered ("warning: reserved1 is %s\n", local_hex_string(ep->r1));
322 if (ep->r2)
323 printf_unfiltered ("warning: reserved2 is %s\n", local_hex_string(ep->r2));
324 if (ep->r3)
325 printf_unfiltered ("warning: reserved3 is %s\n", local_hex_string(ep->r3));
326 }
327
328 /*
329 * values that go into fp_kind (from <i386/fpreg.h>)
330 */
331 #define FP_NO 0 /* no fp chip, no emulator (no fp support) */
332 #define FP_SW 1 /* no fp chip, using software emulator */
333 #define FP_HW 2 /* chip present bit */
334 #define FP_287 2 /* 80287 chip present */
335 #define FP_387 3 /* 80387 chip present */
336
337 typedef struct fpstate {
338 #if 1
339 unsigned char state[FP_STATE_BYTES]; /* "hardware" state */
340 #else
341 struct env387 state; /* Actually this */
342 #endif
343 int status; /* Duplicate status */
344 } *fpstate_t;
345
346 /* Mach 3 specific routines.
347 */
348 private boolean_t
349 get_i387_state (fstate)
350 struct fpstate *fstate;
351 {
352 kern_return_t ret;
353 thread_state_data_t state;
354 unsigned int fsCnt = i386_FLOAT_STATE_COUNT;
355 struct i386_float_state *fsp;
356
357 ret = thread_get_state (current_thread,
358 i386_FLOAT_STATE,
359 state,
360 &fsCnt);
361
362 if (ret != KERN_SUCCESS)
363 {
364 message ("Can not get live floating point state: %s",
365 mach_error_string (ret));
366 return FALSE;
367 }
368
369 fsp = (struct i386_float_state *)state;
370 /* The 387 chip (also 486 counts) or a software emulator? */
371 if (!fsp->initialized || (fsp->fpkind != FP_387 && fsp->fpkind != FP_SW))
372 return FALSE;
373
374 /* Clear the target then copy thread's float state there.
375 Make a copy of the status word, for some reason?
376 */
377 bzero (fstate, sizeof(struct fpstate));
378
379 fstate->status = fsp->exc_status;
380
381 memcpy (fstate->state, (char *)&fsp->hw_state, FP_STATE_BYTES);
382
383 return TRUE;
384 }
385
386 private boolean_t
387 get_i387_core_state (fstate)
388 struct fpstate *fstate;
389 {
390 /* Not implemented yet. Core files do not contain float state. */
391 return FALSE;
392 }
393
394 /*
395 * This is called by "info float" command
396 */
397 void
398 i386_mach3_float_info()
399 {
400 char buf [sizeof (struct fpstate) + 2 * sizeof (int)];
401 boolean_t valid = FALSE;
402 fpstate_t fps;
403
404 if (target_has_execution)
405 valid = get_i387_state (buf);
406 #if 0
407 else if (WE HAVE CORE FILE) /* @@@@ Core files not supported */
408 valid = get_i387_core_state (buf);
409 #endif
410
411 if (!valid)
412 {
413 message("no floating point status saved");
414 return;
415 }
416
417 fps = (fpstate_t) buf;
418
419 print_387_status (fps->status, (struct env387 *)fps->state);
420 }