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. */
198 char *buf
= alloca (MAX_REGISTER_RAW_SIZE
);
200 if (CANNOT_STORE_REGISTER (regno
))
205 /* Overload thread id onto process id */
206 if ((tid
= TIDGET (inferior_ptid
)) == 0)
207 tid
= PIDGET (inferior_ptid
); /* no thread id, just use process id */
209 offset
= U_REGS_OFFSET
;
211 regaddr
= register_addr (regno
, offset
);
213 /* Put the contents of regno into a local buffer */
214 regcache_collect (regno
, buf
);
216 /* Store the local buffer into the inferior a chunk at the time. */
217 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
+= sizeof (PTRACE_XFER_TYPE
))
220 ptrace (PT_WRITE_U
, tid
, (PTRACE_ARG3_TYPE
) regaddr
,
221 *(PTRACE_XFER_TYPE
*) (buf
+ i
));
222 regaddr
+= sizeof (PTRACE_XFER_TYPE
);
225 sprintf (mess
, "writing register %s (#%d)",
226 REGISTER_NAME (regno
), regno
);
227 perror_with_name (mess
);
232 /* Store our register values back into the inferior.
233 If REGNO is negative, do this for all registers.
234 Otherwise, REGNO specifies which register (so we can save time). */
237 old_store_inferior_registers (int regno
)
241 store_register (regno
);
245 for (regno
= 0; regno
< NUM_REGS
; regno
++)
247 store_register (regno
);
252 /* Given a pointer to a general register set in /proc format
253 (elf_gregset_t *), unpack the register contents and supply
254 them as gdb's idea of the current register values. */
257 /* Note both m68k-tdep.c and m68klinux-nat.c contain definitions
258 for supply_gregset and supply_fpregset. The definitions
259 in m68k-tdep.c are valid if USE_PROC_FS is defined. Otherwise,
260 the definitions in m68klinux-nat.c will be used. This is a
261 bit of a hack. The supply_* routines do not belong in
262 *_tdep.c files. But, there are several lynx ports that currently
263 depend on these definitions. */
267 /* Prototypes for supply_gregset etc. */
271 supply_gregset (elf_gregset_t
*gregsetp
)
273 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
276 for (regi
= D0_REGNUM
; regi
<= SP_REGNUM
; regi
++)
277 supply_register (regi
, (char *) ®p
[regmap
[regi
]]);
278 supply_register (PS_REGNUM
, (char *) ®p
[PT_SR
]);
279 supply_register (PC_REGNUM
, (char *) ®p
[PT_PC
]);
282 /* Fill register REGNO (if it is a general-purpose register) in
283 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
284 do this for all registers. */
286 fill_gregset (elf_gregset_t
*gregsetp
, int regno
)
288 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
291 for (i
= 0; i
< NUM_GREGS
; i
++)
292 if ((regno
== -1 || regno
== i
))
293 regcache_collect (i
, regp
+ regmap
[i
]);
296 #ifdef HAVE_PTRACE_GETREGS
298 /* Fetch all general-purpose registers from process/thread TID and
299 store their values in GDB's register array. */
306 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
310 /* The kernel we're running on doesn't support the GETREGS
311 request. Reset `have_ptrace_getregs'. */
312 have_ptrace_getregs
= 0;
316 perror_with_name ("Couldn't get registers");
319 supply_gregset (®s
);
322 /* Store all valid general-purpose registers in GDB's register array
323 into the process/thread specified by TID. */
326 store_regs (int tid
, int regno
)
330 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
331 perror_with_name ("Couldn't get registers");
333 fill_gregset (®s
, regno
);
335 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
336 perror_with_name ("Couldn't write registers");
341 static void fetch_regs (int tid
) {}
342 static void store_regs (int tid
, int regno
) {}
347 /* Transfering floating-point registers between GDB, inferiors and cores. */
349 /* What is the address of fpN within the floating-point register set F? */
350 #define FPREG_ADDR(f, n) ((char *) &(f)->fpregs[(n) * 3])
352 /* Fill GDB's register array with the floating-point register values in
356 supply_fpregset (elf_fpregset_t
*fpregsetp
)
360 for (regi
= FP0_REGNUM
; regi
< FPC_REGNUM
; regi
++)
361 supply_register (regi
, FPREG_ADDR (fpregsetp
, regi
- FP0_REGNUM
));
362 supply_register (FPC_REGNUM
, (char *) &fpregsetp
->fpcntl
[0]);
363 supply_register (FPS_REGNUM
, (char *) &fpregsetp
->fpcntl
[1]);
364 supply_register (FPI_REGNUM
, (char *) &fpregsetp
->fpcntl
[2]);
367 /* Fill register REGNO (if it is a floating-point register) in
368 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
369 do this for all registers. */
372 fill_fpregset (elf_fpregset_t
*fpregsetp
, int regno
)
376 /* Fill in the floating-point registers. */
377 for (i
= FP0_REGNUM
; i
< FP0_REGNUM
+ 8; i
++)
378 if (regno
== -1 || regno
== i
)
379 regcache_collect (regno
, FPREG_ADDR (fpregsetp
, regno
- FP0_REGNUM
));
381 /* Fill in the floating-point control registers. */
382 for (i
= FPC_REGNUM
; i
<= FPI_REGNUM
; i
++)
383 if (regno
== -1 || regno
== i
)
384 regcache_collect (regno
, fpregsetp
->fpcntl
[regno
- FPC_REGNUM
]);
387 #ifdef HAVE_PTRACE_GETREGS
389 /* Fetch all floating-point registers from process/thread TID and store
390 thier values in GDB's register array. */
393 fetch_fpregs (int tid
)
395 elf_fpregset_t fpregs
;
397 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
398 perror_with_name ("Couldn't get floating point status");
400 supply_fpregset (&fpregs
);
403 /* Store all valid floating-point registers in GDB's register array
404 into the process/thread specified by TID. */
407 store_fpregs (int tid
, int regno
)
409 elf_fpregset_t fpregs
;
411 if (ptrace (PTRACE_GETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
412 perror_with_name ("Couldn't get floating point status");
414 fill_fpregset (&fpregs
, regno
);
416 if (ptrace (PTRACE_SETFPREGS
, tid
, 0, (int) &fpregs
) < 0)
417 perror_with_name ("Couldn't write floating point status");
422 static void fetch_fpregs (int tid
) {}
423 static void store_fpregs (int tid
, int regno
) {}
429 /* Transferring arbitrary registers between GDB and inferior. */
431 /* Fetch register REGNO from the child process. If REGNO is -1, do
432 this for all registers (including the floating point and SSE
436 fetch_inferior_registers (int regno
)
440 /* Use the old method of peeking around in `struct user' if the
441 GETREGS request isn't available. */
442 if (! have_ptrace_getregs
)
444 old_fetch_inferior_registers (regno
);
448 /* Linux LWP ID's are process ID's. */
449 if ((tid
= TIDGET (inferior_ptid
)) == 0)
450 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
452 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
453 transfers more registers in one system call, and we'll cache the
454 results. But remember that fetch_fpxregs can fail, and return
460 /* The call above might reset `have_ptrace_getregs'. */
461 if (! have_ptrace_getregs
)
463 old_fetch_inferior_registers (-1);
471 if (getregs_supplies (regno
))
477 if (getfpregs_supplies (regno
))
483 internal_error (__FILE__
, __LINE__
,
484 "Got request for bad register number %d.", regno
);
487 /* Store register REGNO back into the child process. If REGNO is -1,
488 do this for all registers (including the floating point and SSE
491 store_inferior_registers (int regno
)
495 /* Use the old method of poking around in `struct user' if the
496 SETREGS request isn't available. */
497 if (! have_ptrace_getregs
)
499 old_store_inferior_registers (regno
);
503 /* Linux LWP ID's are process ID's. */
504 if ((tid
= TIDGET (inferior_ptid
)) == 0)
505 tid
= PIDGET (inferior_ptid
); /* Not a threaded program. */
507 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
508 transfers more registers in one system call. But remember that
509 store_fpregs can fail, and return zero. */
512 store_regs (tid
, regno
);
513 store_fpregs (tid
, regno
);
517 if (getregs_supplies (regno
))
519 store_regs (tid
, regno
);
523 if (getfpregs_supplies (regno
))
525 store_fpregs (tid
, regno
);
529 internal_error (__FILE__
, __LINE__
,
530 "Got request to store bad register number %d.", regno
);
533 /* Interpreting register set info found in core files. */
535 /* Provide registers to GDB from a core file.
537 (We can't use the generic version of this function in
538 core-regset.c, because we need to use elf_gregset_t instead of
541 CORE_REG_SECT points to an array of bytes, which are the contents
542 of a `note' from a core file which BFD thinks might contain
543 register contents. CORE_REG_SIZE is its size.
545 WHICH says which register set corelow suspects this is:
546 0 --- the general-purpose register set, in elf_gregset_t format
547 2 --- the floating-point register set, in elf_fpregset_t format
549 REG_ADDR isn't used on Linux. */
552 fetch_core_registers (char *core_reg_sect
, unsigned core_reg_size
,
553 int which
, CORE_ADDR reg_addr
)
555 elf_gregset_t gregset
;
556 elf_fpregset_t fpregset
;
561 if (core_reg_size
!= sizeof (gregset
))
562 warning ("Wrong size gregset in core file.");
565 memcpy (&gregset
, core_reg_sect
, sizeof (gregset
));
566 supply_gregset (&gregset
);
571 if (core_reg_size
!= sizeof (fpregset
))
572 warning ("Wrong size fpregset in core file.");
575 memcpy (&fpregset
, core_reg_sect
, sizeof (fpregset
));
576 supply_fpregset (&fpregset
);
581 /* We've covered all the kinds of registers we know about here,
582 so this must be something we wouldn't know what to do with
583 anyway. Just ignore it. */
592 return (sizeof (struct user
));
595 /* Return non-zero if PC points into the signal trampoline. */
598 in_sigtramp (CORE_ADDR pc
)
601 char buf
[TARGET_SHORT_BIT
/ TARGET_CHAR_BIT
];
604 sp
= read_register (SP_REGNUM
);
608 if (read_memory_nobpt (pc
, buf
, sizeof (buf
)))
610 insn
= extract_unsigned_integer (buf
, sizeof (buf
));
611 if (insn
== 0xdefc /* addaw #,sp */
612 || insn
== 0x7077 /* moveq #119,d0 */
613 || insn
== 0x4e40 /* trap #0 */
614 || insn
== 0x203c /* movel #,d0 */ )
617 if (read_memory_nobpt (pc
- 2, buf
, sizeof (buf
)))
619 insn
= extract_unsigned_integer (buf
, sizeof (buf
));
620 if (insn
== 0xdefc /* addaw #,sp */
621 || insn
== 0x7077 /* moveq #119,d0 */
622 || insn
== 0x4e40 /* trap #0 */
623 || insn
== 0x203c /* movel #,d0 */ )
630 /* Register that we are able to handle Linux ELF core file formats. */
632 static struct core_fns linux_elf_core_fns
=
634 bfd_target_elf_flavour
, /* core_flavour */
635 default_check_format
, /* check_format */
636 default_core_sniffer
, /* core_sniffer */
637 fetch_core_registers
, /* core_read_registers */
642 _initialize_m68k_linux_nat ()
644 add_core_fns (&linux_elf_core_fns
);