* config/tc-mips.c (HAVE_64BIT_ADDRESS_CONSTANTS): Remove.
[deliverable/binutils-gdb.git] / gdb / m68klinux-nat.c
CommitLineData
a4b6fc86
AC
1/* Motorola m68k native support for GNU/Linux.
2
3 Copyright 1996, 1998, 2000, 2001, 2002 Free Software Foundation,
4 Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23#include "defs.h"
24#include "frame.h"
25#include "inferior.h"
26#include "language.h"
27#include "gdbcore.h"
32eeb91a 28#include "gdb_string.h"
4e052eda 29#include "regcache.h"
c906108c 30
32eeb91a
AS
31#include "m68k-tdep.h"
32
c906108c
SS
33#ifdef USG
34#include <sys/types.h>
35#endif
36
37#include <sys/param.h>
38#include <sys/dir.h>
39#include <signal.h>
0280a90a 40#include <sys/ptrace.h>
c906108c
SS
41#include <sys/user.h>
42#include <sys/ioctl.h>
43#include <fcntl.h>
44#include <sys/procfs.h>
45
0280a90a
AS
46#ifdef HAVE_SYS_REG_H
47#include <sys/reg.h>
48#endif
49
c906108c
SS
50#include <sys/file.h>
51#include "gdb_stat.h"
52
53#include "floatformat.h"
54
55#include "target.h"
c906108c
SS
56\f
57/* This table must line up with REGISTER_NAMES in tm-m68k.h */
c5aa993b 58static const int regmap[] =
c906108c
SS
59{
60 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
61 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
62 PT_SR, PT_PC,
63 /* PT_FP0, ..., PT_FP7 */
64 21, 24, 27, 30, 33, 36, 39, 42,
65 /* PT_FPCR, PT_FPSR, PT_FPIAR */
66 45, 46, 47
67};
68
0280a90a
AS
69/* Which ptrace request retrieves which registers?
70 These apply to the corresponding SET requests as well. */
71#define NUM_GREGS (18)
72#define MAX_NUM_REGS (NUM_GREGS + 11)
73
74int
75getregs_supplies (int regno)
76{
77 return 0 <= regno && regno < NUM_GREGS;
78}
79
80int
81getfpregs_supplies (int regno)
82{
32eeb91a 83 return FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
0280a90a
AS
84}
85
86/* Does the current host support the GETREGS request? */
87int have_ptrace_getregs =
88#ifdef HAVE_PTRACE_GETREGS
89 1
90#else
91 0
92#endif
93;
94
95\f
96
c906108c
SS
97/* BLOCKEND is the value of u.u_ar0, and points to the place where GS
98 is stored. */
99
100int
fba45db2 101m68k_linux_register_u_addr (int blockend, int regnum)
c906108c 102{
c5aa993b 103 return (blockend + 4 * regmap[regnum]);
c906108c 104}
0280a90a
AS
105\f
106
107/* Fetching registers directly from the U area, one at a time. */
108
109/* FIXME: This duplicates code from `inptrace.c'. The problem is that we
110 define FETCH_INFERIOR_REGISTERS since we want to use our own versions
111 of {fetch,store}_inferior_registers that use the GETREGS request. This
112 means that the code in `infptrace.c' is #ifdef'd out. But we need to
113 fall back on that code when GDB is running on top of a kernel that
114 doesn't support the GETREGS request. */
115
116#ifndef PT_READ_U
117#define PT_READ_U PTRACE_PEEKUSR
118#endif
119#ifndef PT_WRITE_U
120#define PT_WRITE_U PTRACE_POKEUSR
121#endif
122
123/* Default the type of the ptrace transfer to int. */
124#ifndef PTRACE_XFER_TYPE
125#define PTRACE_XFER_TYPE int
126#endif
127
128/* Fetch one register. */
129
130static void
131fetch_register (int regno)
132{
133 /* This isn't really an address. But ptrace thinks of it as one. */
134 CORE_ADDR regaddr;
135 char mess[128]; /* For messages */
136 register int i;
137 unsigned int offset; /* Offset of registers within the u area. */
123a958e 138 char buf[MAX_REGISTER_SIZE];
0280a90a
AS
139 int tid;
140
141 if (CANNOT_FETCH_REGISTER (regno))
142 {
8de307e0 143 memset (buf, '\0', register_size (current_gdbarch, regno)); /* Supply zeroes */
0280a90a
AS
144 supply_register (regno, buf);
145 return;
146 }
147
148 /* Overload thread id onto process id */
8de307e0
AS
149 tid = TIDGET (inferior_ptid);
150 if (tid == 0)
0280a90a
AS
151 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
152
153 offset = U_REGS_OFFSET;
154
155 regaddr = register_addr (regno, offset);
8de307e0
AS
156 for (i = 0; i < register_size (current_gdbarch, regno);
157 i += sizeof (PTRACE_XFER_TYPE))
0280a90a
AS
158 {
159 errno = 0;
8de307e0
AS
160 *(PTRACE_XFER_TYPE *) &buf[i] = ptrace (PT_READ_U, tid,
161 (PTRACE_ARG3_TYPE) regaddr, 0);
0280a90a
AS
162 regaddr += sizeof (PTRACE_XFER_TYPE);
163 if (errno != 0)
164 {
165 sprintf (mess, "reading register %s (#%d)",
166 REGISTER_NAME (regno), regno);
167 perror_with_name (mess);
168 }
169 }
170 supply_register (regno, buf);
171}
172
173/* Fetch register values from the inferior.
174 If REGNO is negative, do this for all registers.
175 Otherwise, REGNO specifies which register (so we can save time). */
c906108c 176
0280a90a
AS
177void
178old_fetch_inferior_registers (int regno)
179{
180 if (regno >= 0)
181 {
182 fetch_register (regno);
183 }
184 else
185 {
186 for (regno = 0; regno < NUM_REGS; regno++)
187 {
188 fetch_register (regno);
189 }
190 }
191}
192
193/* Store one register. */
194
195static void
196store_register (int regno)
197{
198 /* This isn't really an address. But ptrace thinks of it as one. */
199 CORE_ADDR regaddr;
200 char mess[128]; /* For messages */
201 register int i;
202 unsigned int offset; /* Offset of registers within the u area. */
203 int tid;
d9d9c31f 204 char buf[MAX_REGISTER_SIZE];
0280a90a
AS
205
206 if (CANNOT_STORE_REGISTER (regno))
207 {
208 return;
209 }
210
211 /* Overload thread id onto process id */
8de307e0
AS
212 tid = TIDGET (inferior_ptid);
213 if (tid == 0)
0280a90a
AS
214 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
215
216 offset = U_REGS_OFFSET;
217
218 regaddr = register_addr (regno, offset);
9852326a
AS
219
220 /* Put the contents of regno into a local buffer */
221 regcache_collect (regno, buf);
222
223 /* Store the local buffer into the inferior a chunk at the time. */
8de307e0
AS
224 for (i = 0; i < register_size (current_gdbarch, regno);
225 i += sizeof (PTRACE_XFER_TYPE))
0280a90a
AS
226 {
227 errno = 0;
228 ptrace (PT_WRITE_U, tid, (PTRACE_ARG3_TYPE) regaddr,
9852326a 229 *(PTRACE_XFER_TYPE *) (buf + i));
0280a90a
AS
230 regaddr += sizeof (PTRACE_XFER_TYPE);
231 if (errno != 0)
232 {
233 sprintf (mess, "writing register %s (#%d)",
234 REGISTER_NAME (regno), regno);
235 perror_with_name (mess);
236 }
237 }
238}
239
240/* Store our register values back into the inferior.
241 If REGNO is negative, do this for all registers.
242 Otherwise, REGNO specifies which register (so we can save time). */
243
244void
245old_store_inferior_registers (int regno)
246{
247 if (regno >= 0)
248 {
249 store_register (regno);
250 }
251 else
252 {
253 for (regno = 0; regno < NUM_REGS; regno++)
254 {
255 store_register (regno);
256 }
257 }
258}
259\f
f175af98
DJ
260/* Given a pointer to a general register set in /proc format
261 (elf_gregset_t *), unpack the register contents and supply
262 them as gdb's idea of the current register values. */
c906108c
SS
263
264
265/* Note both m68k-tdep.c and m68klinux-nat.c contain definitions
266 for supply_gregset and supply_fpregset. The definitions
267 in m68k-tdep.c are valid if USE_PROC_FS is defined. Otherwise,
268 the definitions in m68klinux-nat.c will be used. This is a
269 bit of a hack. The supply_* routines do not belong in
270 *_tdep.c files. But, there are several lynx ports that currently
c5aa993b 271 depend on these definitions. */
c906108c
SS
272
273#ifndef USE_PROC_FS
274
c60c0f5f
MS
275/* Prototypes for supply_gregset etc. */
276#include "gregset.h"
277
c906108c 278void
f175af98 279supply_gregset (elf_gregset_t *gregsetp)
c906108c 280{
0280a90a 281 elf_greg_t *regp = (elf_greg_t *) gregsetp;
c906108c
SS
282 int regi;
283
32eeb91a 284 for (regi = M68K_D0_REGNUM; regi <= SP_REGNUM; regi++)
0280a90a
AS
285 supply_register (regi, (char *) &regp[regmap[regi]]);
286 supply_register (PS_REGNUM, (char *) &regp[PT_SR]);
287 supply_register (PC_REGNUM, (char *) &regp[PT_PC]);
288}
289
290/* Fill register REGNO (if it is a general-purpose register) in
291 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
292 do this for all registers. */
293void
294fill_gregset (elf_gregset_t *gregsetp, int regno)
295{
296 elf_greg_t *regp = (elf_greg_t *) gregsetp;
297 int i;
298
299 for (i = 0; i < NUM_GREGS; i++)
8de307e0 300 if (regno == -1 || regno == i)
0280a90a
AS
301 regcache_collect (i, regp + regmap[i]);
302}
303
304#ifdef HAVE_PTRACE_GETREGS
305
306/* Fetch all general-purpose registers from process/thread TID and
307 store their values in GDB's register array. */
308
309static void
310fetch_regs (int tid)
311{
312 elf_gregset_t regs;
313
314 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
315 {
316 if (errno == EIO)
317 {
318 /* The kernel we're running on doesn't support the GETREGS
319 request. Reset `have_ptrace_getregs'. */
320 have_ptrace_getregs = 0;
321 return;
322 }
323
324 perror_with_name ("Couldn't get registers");
325 }
326
327 supply_gregset (&regs);
c906108c
SS
328}
329
0280a90a
AS
330/* Store all valid general-purpose registers in GDB's register array
331 into the process/thread specified by TID. */
332
333static void
334store_regs (int tid, int regno)
335{
336 elf_gregset_t regs;
337
338 if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
339 perror_with_name ("Couldn't get registers");
340
341 fill_gregset (&regs, regno);
342
343 if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
344 perror_with_name ("Couldn't write registers");
345}
346
347#else
348
349static void fetch_regs (int tid) {}
350static void store_regs (int tid, int regno) {}
351
352#endif
353
354\f
355/* Transfering floating-point registers between GDB, inferiors and cores. */
356
357/* What is the address of fpN within the floating-point register set F? */
358#define FPREG_ADDR(f, n) ((char *) &(f)->fpregs[(n) * 3])
359
360/* Fill GDB's register array with the floating-point register values in
361 *FPREGSETP. */
c906108c 362
c5aa993b 363void
f175af98 364supply_fpregset (elf_fpregset_t *fpregsetp)
c906108c
SS
365{
366 int regi;
367
32eeb91a 368 for (regi = FP0_REGNUM; regi < FP0_REGNUM + 8; regi++)
0280a90a 369 supply_register (regi, FPREG_ADDR (fpregsetp, regi - FP0_REGNUM));
32eeb91a
AS
370 supply_register (M68K_FPC_REGNUM, (char *) &fpregsetp->fpcntl[0]);
371 supply_register (M68K_FPS_REGNUM, (char *) &fpregsetp->fpcntl[1]);
372 supply_register (M68K_FPI_REGNUM, (char *) &fpregsetp->fpcntl[2]);
c906108c
SS
373}
374
0280a90a
AS
375/* Fill register REGNO (if it is a floating-point register) in
376 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
377 do this for all registers. */
378
379void
380fill_fpregset (elf_fpregset_t *fpregsetp, int regno)
381{
382 int i;
383
384 /* Fill in the floating-point registers. */
385 for (i = FP0_REGNUM; i < FP0_REGNUM + 8; i++)
386 if (regno == -1 || regno == i)
a731b831 387 regcache_collect (i, FPREG_ADDR (fpregsetp, i - FP0_REGNUM));
0280a90a
AS
388
389 /* Fill in the floating-point control registers. */
32eeb91a 390 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
0280a90a 391 if (regno == -1 || regno == i)
a731b831 392 regcache_collect (i, (char *) &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
0280a90a
AS
393}
394
395#ifdef HAVE_PTRACE_GETREGS
396
397/* Fetch all floating-point registers from process/thread TID and store
398 thier values in GDB's register array. */
399
400static void
401fetch_fpregs (int tid)
402{
403 elf_fpregset_t fpregs;
404
405 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
406 perror_with_name ("Couldn't get floating point status");
407
408 supply_fpregset (&fpregs);
409}
410
411/* Store all valid floating-point registers in GDB's register array
412 into the process/thread specified by TID. */
413
414static void
415store_fpregs (int tid, int regno)
416{
417 elf_fpregset_t fpregs;
418
419 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
420 perror_with_name ("Couldn't get floating point status");
421
422 fill_fpregset (&fpregs, regno);
423
424 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
425 perror_with_name ("Couldn't write floating point status");
426}
427
428#else
429
430static void fetch_fpregs (int tid) {}
431static void store_fpregs (int tid, int regno) {}
432
433#endif
434
c906108c 435#endif
0280a90a
AS
436\f
437/* Transferring arbitrary registers between GDB and inferior. */
438
439/* Fetch register REGNO from the child process. If REGNO is -1, do
440 this for all registers (including the floating point and SSE
441 registers). */
442
443void
444fetch_inferior_registers (int regno)
445{
446 int tid;
447
448 /* Use the old method of peeking around in `struct user' if the
449 GETREGS request isn't available. */
450 if (! have_ptrace_getregs)
451 {
452 old_fetch_inferior_registers (regno);
453 return;
454 }
455
a4b6fc86 456 /* GNU/Linux LWP ID's are process ID's. */
8de307e0
AS
457 tid = TIDGET (inferior_ptid);
458 if (tid == 0)
0280a90a 459 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
f175af98 460
0280a90a
AS
461 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
462 transfers more registers in one system call, and we'll cache the
463 results. But remember that fetch_fpxregs can fail, and return
464 zero. */
465 if (regno == -1)
466 {
467 fetch_regs (tid);
468
469 /* The call above might reset `have_ptrace_getregs'. */
470 if (! have_ptrace_getregs)
471 {
472 old_fetch_inferior_registers (-1);
473 return;
474 }
475
476 fetch_fpregs (tid);
477 return;
478 }
479
480 if (getregs_supplies (regno))
481 {
482 fetch_regs (tid);
483 return;
484 }
485
486 if (getfpregs_supplies (regno))
487 {
488 fetch_fpregs (tid);
489 return;
490 }
491
492 internal_error (__FILE__, __LINE__,
493 "Got request for bad register number %d.", regno);
494}
495
496/* Store register REGNO back into the child process. If REGNO is -1,
497 do this for all registers (including the floating point and SSE
498 registers). */
499void
500store_inferior_registers (int regno)
501{
502 int tid;
503
504 /* Use the old method of poking around in `struct user' if the
505 SETREGS request isn't available. */
506 if (! have_ptrace_getregs)
507 {
508 old_store_inferior_registers (regno);
509 return;
510 }
511
a4b6fc86 512 /* GNU/Linux LWP ID's are process ID's. */
8de307e0
AS
513 tid = TIDGET (inferior_ptid);
514 if (tid == 0)
0280a90a
AS
515 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
516
517 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
518 transfers more registers in one system call. But remember that
519 store_fpregs can fail, and return zero. */
520 if (regno == -1)
521 {
522 store_regs (tid, regno);
523 store_fpregs (tid, regno);
524 return;
525 }
526
527 if (getregs_supplies (regno))
528 {
529 store_regs (tid, regno);
530 return;
531 }
532
533 if (getfpregs_supplies (regno))
534 {
535 store_fpregs (tid, regno);
536 return;
537 }
538
539 internal_error (__FILE__, __LINE__,
540 "Got request to store bad register number %d.", regno);
541}
f175af98
DJ
542\f
543/* Interpreting register set info found in core files. */
544
545/* Provide registers to GDB from a core file.
546
547 (We can't use the generic version of this function in
548 core-regset.c, because we need to use elf_gregset_t instead of
549 gregset_t.)
550
551 CORE_REG_SECT points to an array of bytes, which are the contents
552 of a `note' from a core file which BFD thinks might contain
553 register contents. CORE_REG_SIZE is its size.
554
555 WHICH says which register set corelow suspects this is:
556 0 --- the general-purpose register set, in elf_gregset_t format
557 2 --- the floating-point register set, in elf_fpregset_t format
558
a4b6fc86 559 REG_ADDR isn't used on GNU/Linux. */
f175af98
DJ
560
561static void
562fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
563 int which, CORE_ADDR reg_addr)
564{
565 elf_gregset_t gregset;
566 elf_fpregset_t fpregset;
567
568 switch (which)
569 {
570 case 0:
571 if (core_reg_size != sizeof (gregset))
572 warning ("Wrong size gregset in core file.");
573 else
574 {
575 memcpy (&gregset, core_reg_sect, sizeof (gregset));
576 supply_gregset (&gregset);
577 }
578 break;
579
580 case 2:
581 if (core_reg_size != sizeof (fpregset))
582 warning ("Wrong size fpregset in core file.");
583 else
584 {
585 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
586 supply_fpregset (&fpregset);
587 }
588 break;
589
590 default:
591 /* We've covered all the kinds of registers we know about here,
592 so this must be something we wouldn't know what to do with
593 anyway. Just ignore it. */
594 break;
595 }
596}
c906108c 597\f
c5aa993b 598
c906108c 599int
fba45db2 600kernel_u_size (void)
c906108c
SS
601{
602 return (sizeof (struct user));
603}
604\f
a4b6fc86
AC
605/* Register that we are able to handle GNU/Linux ELF core file
606 formats. */
f175af98
DJ
607
608static struct core_fns linux_elf_core_fns =
609{
610 bfd_target_elf_flavour, /* core_flavour */
611 default_check_format, /* check_format */
612 default_core_sniffer, /* core_sniffer */
613 fetch_core_registers, /* core_read_registers */
614 NULL /* next */
615};
616
617void
5ae5f592 618_initialize_m68k_linux_nat (void)
f175af98
DJ
619{
620 add_core_fns (&linux_elf_core_fns);
621}
This page took 0.703656 seconds and 4 git commands to generate.