1 /* Motorola m68k native support for Linux
2 Copyright 1996, 1998, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
29 #include <sys/types.h>
32 #include <sys/param.h>
35 #include <sys/ptrace.h>
37 #include <sys/ioctl.h>
39 #include <sys/procfs.h>
48 #include "floatformat.h"
53 /* This table must line up with REGISTER_NAMES in tm-m68k.h */
54 static const int regmap
[] =
56 PT_D0
, PT_D1
, PT_D2
, PT_D3
, PT_D4
, PT_D5
, PT_D6
, PT_D7
,
57 PT_A0
, PT_A1
, PT_A2
, PT_A3
, PT_A4
, PT_A5
, PT_A6
, PT_USP
,
59 /* PT_FP0, ..., PT_FP7 */
60 21, 24, 27, 30, 33, 36, 39, 42,
61 /* PT_FPCR, PT_FPSR, PT_FPIAR */
65 /* Which ptrace request retrieves which registers?
66 These apply to the corresponding SET requests as well. */
67 #define NUM_GREGS (18)
68 #define MAX_NUM_REGS (NUM_GREGS + 11)
71 getregs_supplies (int regno
)
73 return 0 <= regno
&& regno
< NUM_GREGS
;
77 getfpregs_supplies (int regno
)
79 return FP0_REGNUM
<= regno
&& regno
<= FPI_REGNUM
;
82 /* Does the current host support the GETREGS request? */
83 int have_ptrace_getregs
=
84 #ifdef HAVE_PTRACE_GETREGS
93 /* BLOCKEND is the value of u.u_ar0, and points to the place where GS
97 m68k_linux_register_u_addr (int blockend
, int regnum
)
99 return (blockend
+ 4 * regmap
[regnum
]);
103 /* Fetching registers directly from the U area, one at a time. */
105 /* FIXME: This duplicates code from `inptrace.c'. The problem is that we
106 define FETCH_INFERIOR_REGISTERS since we want to use our own versions
107 of {fetch,store}_inferior_registers that use the GETREGS request. This
108 means that the code in `infptrace.c' is #ifdef'd out. But we need to
109 fall back on that code when GDB is running on top of a kernel that
110 doesn't support the GETREGS request. */
113 #define PT_READ_U PTRACE_PEEKUSR
116 #define PT_WRITE_U PTRACE_POKEUSR
119 /* Default the type of the ptrace transfer to int. */
120 #ifndef PTRACE_XFER_TYPE
121 #define PTRACE_XFER_TYPE int
124 /* Fetch one register. */
127 fetch_register (int regno
)
129 /* This isn't really an address. But ptrace thinks of it as one. */
131 char mess
[128]; /* For messages */
133 unsigned int offset
; /* Offset of registers within the u area. */
134 char buf
[MAX_REGISTER_RAW_SIZE
];
137 if (CANNOT_FETCH_REGISTER (regno
))
139 memset (buf
, '\0', REGISTER_RAW_SIZE (regno
)); /* Supply zeroes */
140 supply_register (regno
, buf
);
144 /* Overload thread id onto process id */
145 if ((tid
= TIDGET (inferior_ptid
)) == 0)
146 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
148 offset
= U_REGS_OFFSET
;
150 regaddr
= register_addr (regno
, offset
);
151 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
154 *(PTRACE_XFER_TYPE
*) & buf
[i
] = ptrace (PT_READ_U
, tid
,
155 (PTRACE_ARG3_TYPE
) regaddr
, 0);
156 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
159 sprintf (mess
, "reading register %s (#%d)",
160 REGISTER_NAME (regno
), regno
);
161 perror_with_name (mess
);
164 supply_register (regno
, buf
);
167 /* Fetch register values from the inferior.
168 If REGNO is negative, do this for all registers.
169 Otherwise, REGNO specifies which register (so we can save time). */
172 old_fetch_inferior_registers (int regno
)
176 fetch_register (regno
);
180 for (regno
= 0; regno
< NUM_REGS
; regno
++)
182 fetch_register (regno
);
187 /* Store one register. */
190 store_register (int regno
)
192 /* This isn't really an address. But ptrace thinks of it as one. */
194 char mess
[128]; /* For messages */
196 unsigned int offset
; /* Offset of registers within the u area. */
199 if (CANNOT_STORE_REGISTER (regno
))
204 /* Overload thread id onto process id */
205 if ((tid
= TIDGET (inferior_ptid
)) == 0)
206 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
208 offset
= U_REGS_OFFSET
;
210 regaddr
= register_addr (regno
, offset
);
211 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
214 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
215 *(PTRACE_XFER_TYPE
*) & registers
[REGISTER_BYTE (regno
) + i
]);
216 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
219 sprintf (mess
, "writing register %s (#%d)",
220 REGISTER_NAME (regno
), regno
);
221 perror_with_name (mess
);
226 /* Store our register values back into the inferior.
227 If REGNO is negative, do this for all registers.
228 Otherwise, REGNO specifies which register (so we can save time). */
231 old_store_inferior_registers (int regno
)
235 store_register (regno
);
239 for (regno
= 0; regno
< NUM_REGS
; regno
++)
241 store_register (regno
);
246 /* Given a pointer to a general register set in /proc format
247 (elf_gregset_t *), unpack the register contents and supply
248 them as gdb's idea of the current register values. */
251 /* Note both m68k-tdep.c and m68klinux-nat.c contain definitions
252 for supply_gregset and supply_fpregset. The definitions
253 in m68k-tdep.c are valid if USE_PROC_FS is defined. Otherwise,
254 the definitions in m68klinux-nat.c will be used. This is a
255 bit of a hack. The supply_* routines do not belong in
256 *_tdep.c files. But, there are several lynx ports that currently
257 depend on these definitions. */
261 /* Prototypes for supply_gregset etc. */
265 supply_gregset (elf_gregset_t
*gregsetp
)
267 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
270 for (regi
= D0_REGNUM
; regi
<= SP_REGNUM
; regi
++)
271 supply_register (regi
, (char *) ®p
[regmap
[regi
]]);
272 supply_register (PS_REGNUM
, (char *) ®p
[PT_SR
]);
273 supply_register (PC_REGNUM
, (char *) ®p
[PT_PC
]);
276 /* Fill register REGNO (if it is a general-purpose register) in
277 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
278 do this for all registers. */
280 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
282 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
285 for (i
= 0; i
< NUM_GREGS
; i
++)
286 if ((regno
== -1 || regno
== i
))
287 regcache_collect (i
, regp
+ regmap
[i
]);
290 #ifdef HAVE_PTRACE_GETREGS
292 /* Fetch all general-purpose registers from process/thread TID and
293 store their values in GDB's register array. */
300 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
304 /* The kernel we're running on doesn't support the GETREGS
305 request. Reset `have_ptrace_getregs'. */
306 have_ptrace_getregs
= 0;
310 perror_with_name ("Couldn't get registers");
313 supply_gregset (®s
);
316 /* Store all valid general-purpose registers in GDB's register array
317 into the process/thread specified by TID. */
320 store_regs (int tid
, int regno
)
324 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
325 perror_with_name ("Couldn't get registers");
327 fill_gregset (®s
, regno
);
329 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
330 perror_with_name ("Couldn't write registers");
335 static void fetch_regs (int tid
) {}
336 static void store_regs (int tid
, int regno
) {}
341 /* Transfering floating-point registers between GDB, inferiors and cores. */
343 /* What is the address of fpN within the floating-point register set F? */
344 #define FPREG_ADDR(f, n) ((char *) &(f)->fpregs[(n) * 3])
346 /* Fill GDB's register array with the floating-point register values in
350 supply_fpregset (elf_fpregset_t
*fpregsetp
)
354 for (regi
= FP0_REGNUM
; regi
< FPC_REGNUM
; regi
++)
355 supply_register (regi
, FPREG_ADDR (fpregsetp
, regi
- FP0_REGNUM
));
356 supply_register (FPC_REGNUM
, (char *) &fpregsetp
->fpcntl
[0]);
357 supply_register (FPS_REGNUM
, (char *) &fpregsetp
->fpcntl
[1]);
358 supply_register (FPI_REGNUM
, (char *) &fpregsetp
->fpcntl
[2]);
361 /* Fill register REGNO (if it is a floating-point register) in
362 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
363 do this for all registers. */
366 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
370 /* Fill in the floating-point registers. */
371 for (i
= FP0_REGNUM
; i
< FP0_REGNUM
+ 8; i
++)
372 if (regno
== -1 || regno
== i
)
373 memcpy (FPREG_ADDR (fpregsetp
, regno
- FP0_REGNUM
),
374 ®isters
[REGISTER_BYTE (regno
)],
375 REGISTER_RAW_SIZE(regno
));
377 /* Fill in the floating-point control registers. */
378 for (i
= FPC_REGNUM
; i
<= FPI_REGNUM
; i
++)
379 if (regno
== -1 || regno
== i
)
380 fpregsetp
->fpcntl
[regno
- FPC_REGNUM
]
381 = *(int *) ®isters
[REGISTER_BYTE (regno
)];
384 #ifdef HAVE_PTRACE_GETREGS
386 /* Fetch all floating-point registers from process/thread TID and store
387 thier values in GDB's register array. */
390 fetch_fpregs (int tid
)
392 elf_fpregset_t fpregs
;
394 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
395 perror_with_name ("Couldn't get floating point status");
397 supply_fpregset (&fpregs
);
400 /* Store all valid floating-point registers in GDB's register array
401 into the process/thread specified by TID. */
404 store_fpregs (int tid
, int regno
)
406 elf_fpregset_t fpregs
;
408 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
409 perror_with_name ("Couldn't get floating point status");
411 fill_fpregset (&fpregs
, regno
);
413 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
414 perror_with_name ("Couldn't write floating point status");
419 static void fetch_fpregs (int tid
) {}
420 static void store_fpregs (int tid
, int regno
) {}
426 /* Transferring arbitrary registers between GDB and inferior. */
428 /* Fetch register REGNO from the child process. If REGNO is -1, do
429 this for all registers (including the floating point and SSE
433 fetch_inferior_registers (int regno
)
437 /* Use the old method of peeking around in `struct user' if the
438 GETREGS request isn't available. */
439 if (! have_ptrace_getregs
)
441 old_fetch_inferior_registers (regno
);
445 /* Linux LWP ID's are process ID's. */
446 if ((tid
= TIDGET (inferior_ptid
)) == 0)
447 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
449 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
450 transfers more registers in one system call, and we'll cache the
451 results. But remember that fetch_fpxregs can fail, and return
457 /* The call above might reset `have_ptrace_getregs'. */
458 if (! have_ptrace_getregs
)
460 old_fetch_inferior_registers (-1);
468 if (getregs_supplies (regno
))
474 if (getfpregs_supplies (regno
))
480 internal_error (__FILE__
, __LINE__
,
481 "Got request for bad register number %d.", regno
);
484 /* Store register REGNO back into the child process. If REGNO is -1,
485 do this for all registers (including the floating point and SSE
488 store_inferior_registers (int regno
)
492 /* Use the old method of poking around in `struct user' if the
493 SETREGS request isn't available. */
494 if (! have_ptrace_getregs
)
496 old_store_inferior_registers (regno
);
500 /* Linux LWP ID's are process ID's. */
501 if ((tid
= TIDGET (inferior_ptid
)) == 0)
502 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
504 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
505 transfers more registers in one system call. But remember that
506 store_fpregs can fail, and return zero. */
509 store_regs (tid
, regno
);
510 store_fpregs (tid
, regno
);
514 if (getregs_supplies (regno
))
516 store_regs (tid
, regno
);
520 if (getfpregs_supplies (regno
))
522 store_fpregs (tid
, regno
);
526 internal_error (__FILE__
, __LINE__
,
527 "Got request to store bad register number %d.", regno
);
530 /* Interpreting register set info found in core files. */
532 /* Provide registers to GDB from a core file.
534 (We can't use the generic version of this function in
535 core-regset.c, because we need to use elf_gregset_t instead of
538 CORE_REG_SECT points to an array of bytes, which are the contents
539 of a `note' from a core file which BFD thinks might contain
540 register contents. CORE_REG_SIZE is its size.
542 WHICH says which register set corelow suspects this is:
543 0 --- the general-purpose register set, in elf_gregset_t format
544 2 --- the floating-point register set, in elf_fpregset_t format
546 REG_ADDR isn't used on Linux. */
549 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
550 int which
, CORE_ADDR reg_addr
)
552 elf_gregset_t gregset
;
553 elf_fpregset_t fpregset
;
558 if (core_reg_size
!= sizeof (gregset
))
559 warning ("Wrong size gregset in core file.");
562 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
563 supply_gregset (&gregset
);
568 if (core_reg_size
!= sizeof (fpregset
))
569 warning ("Wrong size fpregset in core file.");
572 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
573 supply_fpregset (&fpregset
);
578 /* We've covered all the kinds of registers we know about here,
579 so this must be something we wouldn't know what to do with
580 anyway. Just ignore it. */
589 return (sizeof (struct user
));
592 /* Return non-zero if PC points into the signal trampoline. */
595 in_sigtramp (CORE_ADDR pc
)
598 char buf
[TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
];
601 sp
= read_register (SP_REGNUM
);
605 if (read_memory_nobpt (pc
, buf
, sizeof (buf
)))
607 insn
= extract_unsigned_integer (buf
, sizeof (buf
));
608 if (insn
== 0xdefc /* addaw #,sp */
609 || insn
== 0x7077 /* moveq #119,d0 */
610 || insn
== 0x4e40 /* trap #0 */
611 || insn
== 0x203c /* movel #,d0 */ )
614 if (read_memory_nobpt (pc
- 2, buf
, sizeof (buf
)))
616 insn
= extract_unsigned_integer (buf
, sizeof (buf
));
617 if (insn
== 0xdefc /* addaw #,sp */
618 || insn
== 0x7077 /* moveq #119,d0 */
619 || insn
== 0x4e40 /* trap #0 */
620 || insn
== 0x203c /* movel #,d0 */ )
627 /* Register that we are able to handle Linux ELF core file formats. */
629 static struct core_fns linux_elf_core_fns
=
631 bfd_target_elf_flavour
, /* core_flavour */
632 default_check_format
, /* check_format */
633 default_core_sniffer
, /* core_sniffer */
634 fetch_core_registers
, /* core_read_registers */
639 _initialize_m68k_linux_nat ()
641 add_core_fns (&linux_elf_core_fns
);