1 /* Motorola m68k native support for GNU/Linux.
3 Copyright 1996, 1998, 2000, 2001, 2002 Free Software Foundation,
6 This file is part of GDB.
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.
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.
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. */
28 #include "gdb_string.h"
31 #include "m68k-tdep.h"
34 #include <sys/types.h>
37 #include <sys/param.h>
40 #include <sys/ptrace.h>
42 #include <sys/ioctl.h>
44 #include <sys/procfs.h>
53 #include "floatformat.h"
57 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
58 static const int regmap
[] =
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
,
63 /* PT_FP0, ..., PT_FP7 */
64 21, 24, 27, 30, 33, 36, 39, 42,
65 /* PT_FPCR, PT_FPSR, PT_FPIAR */
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)
75 getregs_supplies (int regno
)
77 return 0 <= regno
&& regno
< NUM_GREGS
;
81 getfpregs_supplies (int regno
)
83 return FP0_REGNUM
<= regno
&& regno
<= M68K_FPI_REGNUM
;
86 /* Does the current host support the GETREGS request? */
87 int have_ptrace_getregs
=
88 #ifdef HAVE_PTRACE_GETREGS
97 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
101 m68k_linux_register_u_addr (int blockend
, int regnum
)
103 return (blockend
+ 4 * regmap
[regnum
]);
107 /* Fetching registers directly from the U area, one at a time. */
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. */
117 #define PT_READ_U PTRACE_PEEKUSR
120 #define PT_WRITE_U PTRACE_POKEUSR
123 /* Default the type of the ptrace transfer to int. */
124 #ifndef PTRACE_XFER_TYPE
125 #define PTRACE_XFER_TYPE int
128 /* Fetch one register. */
131 fetch_register (int regno
)
133 /* This isn't really an address. But ptrace thinks of it as one. */
135 char mess
[128]; /* For messages */
137 unsigned int offset
; /* Offset of registers within the u area. */
138 char *buf
= alloca (max_register_size (current_gdbarch
));
141 if (CANNOT_FETCH_REGISTER (regno
))
143 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
144 supply_register (regno
, buf
);
148 /* Overload thread id onto process id */
149 if ((tid
= TIDGET (inferior_ptid
)) == 0)
150 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
152 offset
= U_REGS_OFFSET
;
154 regaddr
= register_addr (regno
, offset
);
155 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
158 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
159 (PTRACE_ARG3_TYPE
) regaddr
, 0);
160 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
163 sprintf (mess
, "reading register %s (#%d)",
164 REGISTER_NAME (regno
), regno
);
165 perror_with_name (mess
);
168 supply_register (regno
, buf
);
171 /* Fetch register values from the inferior.
172 If REGNO is negative, do this for all registers.
173 Otherwise, REGNO specifies which register (so we can save time). */
176 old_fetch_inferior_registers (int regno
)
180 fetch_register (regno
);
184 for (regno
= 0; regno
< NUM_REGS
; regno
++)
186 fetch_register (regno
);
191 /* Store one register. */
194 store_register (int regno
)
196 /* This isn't really an address. But ptrace thinks of it as one. */
198 char mess
[128]; /* For messages */
200 unsigned int offset
; /* Offset of registers within the u area. */
202 char *buf
= alloca (MAX_REGISTER_RAW_SIZE
);
204 if (CANNOT_STORE_REGISTER (regno
))
209 /* Overload thread id onto process id */
210 if ((tid
= TIDGET (inferior_ptid
)) == 0)
211 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
213 offset
= U_REGS_OFFSET
;
215 regaddr
= register_addr (regno
, offset
);
217 /* Put the contents of regno into a local buffer */
218 regcache_collect (regno
, buf
);
220 /* Store the local buffer into the inferior a chunk at the time. */
221 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
224 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
225 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
226 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
229 sprintf (mess
, "writing register %s (#%d)",
230 REGISTER_NAME (regno
), regno
);
231 perror_with_name (mess
);
236 /* Store our register values back into the inferior.
237 If REGNO is negative, do this for all registers.
238 Otherwise, REGNO specifies which register (so we can save time). */
241 old_store_inferior_registers (int regno
)
245 store_register (regno
);
249 for (regno
= 0; regno
< NUM_REGS
; regno
++)
251 store_register (regno
);
256 /* Given a pointer to a general register set in /proc format
257 (elf_gregset_t *), unpack the register contents and supply
258 them as gdb's idea of the current register values. */
261 /* Note both m68k-tdep.c and m68klinux-nat.c contain definitions
262 for supply_gregset and supply_fpregset. The definitions
263 in m68k-tdep.c are valid if USE_PROC_FS is defined. Otherwise,
264 the definitions in m68klinux-nat.c will be used. This is a
265 bit of a hack. The supply_* routines do not belong in
266 *_tdep.c files. But, there are several lynx ports that currently
267 depend on these definitions. */
271 /* Prototypes for supply_gregset etc. */
275 supply_gregset (elf_gregset_t
*gregsetp
)
277 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
280 for (regi
= M68K_D0_REGNUM
; regi
<= SP_REGNUM
; regi
++)
281 supply_register (regi
, (char *) ®p
[regmap
[regi
]]);
282 supply_register (PS_REGNUM
, (char *) ®p
[PT_SR
]);
283 supply_register (PC_REGNUM
, (char *) ®p
[PT_PC
]);
286 /* Fill register REGNO (if it is a general-purpose register) in
287 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
288 do this for all registers. */
290 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
292 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
295 for (i
= 0; i
< NUM_GREGS
; i
++)
296 if ((regno
== -1 || regno
== i
))
297 regcache_collect (i
, regp
+ regmap
[i
]);
300 #ifdef HAVE_PTRACE_GETREGS
302 /* Fetch all general-purpose registers from process/thread TID and
303 store their values in GDB's register array. */
310 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
314 /* The kernel we're running on doesn't support the GETREGS
315 request. Reset `have_ptrace_getregs'. */
316 have_ptrace_getregs
= 0;
320 perror_with_name ("Couldn't get registers");
323 supply_gregset (®s
);
326 /* Store all valid general-purpose registers in GDB's register array
327 into the process/thread specified by TID. */
330 store_regs (int tid
, int regno
)
334 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
335 perror_with_name ("Couldn't get registers");
337 fill_gregset (®s
, regno
);
339 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
340 perror_with_name ("Couldn't write registers");
345 static void fetch_regs (int tid
) {}
346 static void store_regs (int tid
, int regno
) {}
351 /* Transfering floating-point registers between GDB, inferiors and cores. */
353 /* What is the address of fpN within the floating-point register set F? */
354 #define FPREG_ADDR(f, n) ((char *) &(f)->fpregs[(n) * 3])
356 /* Fill GDB's register array with the floating-point register values in
360 supply_fpregset (elf_fpregset_t
*fpregsetp
)
364 for (regi
= FP0_REGNUM
; regi
< FP0_REGNUM
+ 8; regi
++)
365 supply_register (regi
, FPREG_ADDR (fpregsetp
, regi
- FP0_REGNUM
));
366 supply_register (M68K_FPC_REGNUM
, (char *) &fpregsetp
->fpcntl
[0]);
367 supply_register (M68K_FPS_REGNUM
, (char *) &fpregsetp
->fpcntl
[1]);
368 supply_register (M68K_FPI_REGNUM
, (char *) &fpregsetp
->fpcntl
[2]);
371 /* Fill register REGNO (if it is a floating-point register) in
372 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
373 do this for all registers. */
376 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
380 /* Fill in the floating-point registers. */
381 for (i
= FP0_REGNUM
; i
< FP0_REGNUM
+ 8; i
++)
382 if (regno
== -1 || regno
== i
)
383 regcache_collect (regno
, FPREG_ADDR (fpregsetp
, regno
- FP0_REGNUM
));
385 /* Fill in the floating-point control registers. */
386 for (i
= M68K_FPC_REGNUM
; i
<= M68K_FPI_REGNUM
; i
++)
387 if (regno
== -1 || regno
== i
)
388 regcache_collect (regno
, (char *) &fpregsetp
->fpcntl
[regno
- M68K_FPC_REGNUM
]);
391 #ifdef HAVE_PTRACE_GETREGS
393 /* Fetch all floating-point registers from process/thread TID and store
394 thier values in GDB's register array. */
397 fetch_fpregs (int tid
)
399 elf_fpregset_t fpregs
;
401 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
402 perror_with_name ("Couldn't get floating point status");
404 supply_fpregset (&fpregs
);
407 /* Store all valid floating-point registers in GDB's register array
408 into the process/thread specified by TID. */
411 store_fpregs (int tid
, int regno
)
413 elf_fpregset_t fpregs
;
415 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
416 perror_with_name ("Couldn't get floating point status");
418 fill_fpregset (&fpregs
, regno
);
420 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
421 perror_with_name ("Couldn't write floating point status");
426 static void fetch_fpregs (int tid
) {}
427 static void store_fpregs (int tid
, int regno
) {}
433 /* Transferring arbitrary registers between GDB and inferior. */
435 /* Fetch register REGNO from the child process. If REGNO is -1, do
436 this for all registers (including the floating point and SSE
440 fetch_inferior_registers (int regno
)
444 /* Use the old method of peeking around in `struct user' if the
445 GETREGS request isn't available. */
446 if (! have_ptrace_getregs
)
448 old_fetch_inferior_registers (regno
);
452 /* GNU/Linux LWP ID's are process ID's. */
453 if ((tid
= TIDGET (inferior_ptid
)) == 0)
454 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
456 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
457 transfers more registers in one system call, and we'll cache the
458 results. But remember that fetch_fpxregs can fail, and return
464 /* The call above might reset `have_ptrace_getregs'. */
465 if (! have_ptrace_getregs
)
467 old_fetch_inferior_registers (-1);
475 if (getregs_supplies (regno
))
481 if (getfpregs_supplies (regno
))
487 internal_error (__FILE__
, __LINE__
,
488 "Got request for bad register number %d.", regno
);
491 /* Store register REGNO back into the child process. If REGNO is -1,
492 do this for all registers (including the floating point and SSE
495 store_inferior_registers (int regno
)
499 /* Use the old method of poking around in `struct user' if the
500 SETREGS request isn't available. */
501 if (! have_ptrace_getregs
)
503 old_store_inferior_registers (regno
);
507 /* GNU/Linux LWP ID's are process ID's. */
508 if ((tid
= TIDGET (inferior_ptid
)) == 0)
509 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
511 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
512 transfers more registers in one system call. But remember that
513 store_fpregs can fail, and return zero. */
516 store_regs (tid
, regno
);
517 store_fpregs (tid
, regno
);
521 if (getregs_supplies (regno
))
523 store_regs (tid
, regno
);
527 if (getfpregs_supplies (regno
))
529 store_fpregs (tid
, regno
);
533 internal_error (__FILE__
, __LINE__
,
534 "Got request to store bad register number %d.", regno
);
537 /* Interpreting register set info found in core files. */
539 /* Provide registers to GDB from a core file.
541 (We can't use the generic version of this function in
542 core-regset.c, because we need to use elf_gregset_t instead of
545 CORE_REG_SECT points to an array of bytes, which are the contents
546 of a `note' from a core file which BFD thinks might contain
547 register contents. CORE_REG_SIZE is its size.
549 WHICH says which register set corelow suspects this is:
550 0 --- the general-purpose register set, in elf_gregset_t format
551 2 --- the floating-point register set, in elf_fpregset_t format
553 REG_ADDR isn't used on GNU/Linux. */
556 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
557 int which
, CORE_ADDR reg_addr
)
559 elf_gregset_t gregset
;
560 elf_fpregset_t fpregset
;
565 if (core_reg_size
!= sizeof (gregset
))
566 warning ("Wrong size gregset in core file.");
569 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
570 supply_gregset (&gregset
);
575 if (core_reg_size
!= sizeof (fpregset
))
576 warning ("Wrong size fpregset in core file.");
579 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
580 supply_fpregset (&fpregset
);
585 /* We've covered all the kinds of registers we know about here,
586 so this must be something we wouldn't know what to do with
587 anyway. Just ignore it. */
596 return (sizeof (struct user
));
599 /* Register that we are able to handle GNU/Linux ELF core file
602 static struct core_fns linux_elf_core_fns
=
604 bfd_target_elf_flavour
, /* core_flavour */
605 default_check_format
, /* check_format */
606 default_core_sniffer
, /* core_sniffer */
607 fetch_core_registers
, /* core_read_registers */
612 _initialize_m68k_linux_nat (void)
614 add_core_fns (&linux_elf_core_fns
);