1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
28 #include "gdb_string.h"
31 #include "linux-nat.h"
33 #include "m68k-tdep.h"
35 #include <sys/param.h>
38 #include <sys/ptrace.h>
40 #include <sys/ioctl.h>
42 #include <sys/procfs.h>
51 #include "floatformat.h"
55 /* Prototypes for supply_gregset etc. */
58 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
59 static const int regmap[] =
61 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
62 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
64 /* PT_FP0, ..., PT_FP7 */
65 21, 24, 27, 30, 33, 36, 39, 42,
66 /* PT_FPCR, PT_FPSR, PT_FPIAR */
70 /* Which ptrace request retrieves which registers?
71 These apply to the corresponding SET requests as well. */
72 #define NUM_GREGS (18)
73 #define MAX_NUM_REGS (NUM_GREGS + 11)
76 getregs_supplies (int regno)
78 return 0 <= regno && regno < NUM_GREGS;
82 getfpregs_supplies (int regno)
84 return gdbarch_fp0_regnum (current_gdbarch) <= regno
85 && regno <= M68K_FPI_REGNUM;
88 /* Does the current host support the GETREGS request? */
89 int have_ptrace_getregs =
90 #ifdef HAVE_PTRACE_GETREGS
99 /* Fetching registers directly from the U area, one at a time. */
101 /* FIXME: This duplicates code from `inptrace.c'. The problem is that we
102 define FETCH_INFERIOR_REGISTERS since we want to use our own versions
103 of {fetch,store}_inferior_registers that use the GETREGS request. This
104 means that the code in `infptrace.c' is #ifdef'd out. But we need to
105 fall back on that code when GDB is running on top of a kernel that
106 doesn't support the GETREGS request. */
109 #define PT_READ_U PTRACE_PEEKUSR
112 #define PT_WRITE_U PTRACE_POKEUSR
115 /* Fetch one register. */
118 fetch_register (struct regcache *regcache, int regno)
120 /* This isn't really an address. But ptrace thinks of it as one. */
122 char mess[128]; /* For messages */
124 char buf[MAX_REGISTER_SIZE];
127 if (gdbarch_cannot_fetch_register (current_gdbarch, regno))
129 memset (buf, '\0', register_size (current_gdbarch, regno)); /* Supply zeroes */
130 regcache_raw_supply (regcache, regno, buf);
134 /* Overload thread id onto process id */
135 tid = TIDGET (inferior_ptid);
137 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
139 regaddr = 4 * regmap[regno];
140 for (i = 0; i < register_size (current_gdbarch, regno);
141 i += sizeof (PTRACE_TYPE_RET))
144 *(PTRACE_TYPE_RET *) &buf[i] = ptrace (PT_READ_U, tid,
145 (PTRACE_TYPE_ARG3) regaddr, 0);
146 regaddr += sizeof (PTRACE_TYPE_RET);
149 sprintf (mess, "reading register %s (#%d)",
150 gdbarch_register_name (current_gdbarch, regno), regno);
151 perror_with_name (mess);
154 regcache_raw_supply (regcache, regno, buf);
157 /* Fetch register values from the inferior.
158 If REGNO is negative, do this for all registers.
159 Otherwise, REGNO specifies which register (so we can save time). */
162 old_fetch_inferior_registers (struct regcache *regcache, int regno)
166 fetch_register (regcache, regno);
170 for (regno = 0; regno < gdbarch_num_regs (current_gdbarch); regno++)
172 fetch_register (regcache, regno);
177 /* Store one register. */
180 store_register (const struct regcache *regcache, int regno)
182 /* This isn't really an address. But ptrace thinks of it as one. */
184 char mess[128]; /* For messages */
187 char buf[MAX_REGISTER_SIZE];
189 if (gdbarch_cannot_store_register (current_gdbarch, regno))
192 /* Overload thread id onto process id */
193 tid = TIDGET (inferior_ptid);
195 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
197 regaddr = 4 * regmap[regno];
199 /* Put the contents of regno into a local buffer */
200 regcache_raw_collect (regcache, regno, buf);
202 /* Store the local buffer into the inferior a chunk at the time. */
203 for (i = 0; i < register_size (current_gdbarch, regno);
204 i += sizeof (PTRACE_TYPE_RET))
207 ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) regaddr,
208 *(PTRACE_TYPE_RET *) (buf + i));
209 regaddr += sizeof (PTRACE_TYPE_RET);
212 sprintf (mess, "writing register %s (#%d)",
213 gdbarch_register_name (current_gdbarch, regno), regno);
214 perror_with_name (mess);
219 /* Store our register values back into the inferior.
220 If REGNO is negative, do this for all registers.
221 Otherwise, REGNO specifies which register (so we can save time). */
224 old_store_inferior_registers (const struct regcache *regcache, int regno)
228 store_register (regcache, regno);
232 for (regno = 0; regno < gdbarch_num_regs (current_gdbarch); regno++)
234 store_register (regcache, regno);
239 /* Given a pointer to a general register set in /proc format
240 (elf_gregset_t *), unpack the register contents and supply
241 them as gdb's idea of the current register values. */
244 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
246 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
249 for (regi = M68K_D0_REGNUM;
250 regi <= gdbarch_sp_regnum (current_gdbarch);
252 regcache_raw_supply (regcache, regi, ®p[regmap[regi]]);
253 regcache_raw_supply (regcache, gdbarch_ps_regnum (current_gdbarch),
255 regcache_raw_supply (regcache,
256 gdbarch_pc_regnum (current_gdbarch), ®p[PT_PC]);
259 /* Fill register REGNO (if it is a general-purpose register) in
260 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
261 do this for all registers. */
263 fill_gregset (const struct regcache *regcache,
264 elf_gregset_t *gregsetp, int regno)
266 elf_greg_t *regp = (elf_greg_t *) gregsetp;
269 for (i = 0; i < NUM_GREGS; i++)
270 if (regno == -1 || regno == i)
271 regcache_raw_collect (regcache, i, regp + regmap[i]);
274 #ifdef HAVE_PTRACE_GETREGS
276 /* Fetch all general-purpose registers from process/thread TID and
277 store their values in GDB's register array. */
280 fetch_regs (struct regcache *regcache, int tid)
284 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
288 /* The kernel we're running on doesn't support the GETREGS
289 request. Reset `have_ptrace_getregs'. */
290 have_ptrace_getregs = 0;
294 perror_with_name (_("Couldn't get registers"));
297 supply_gregset (regcache, (const elf_gregset_t *) ®s);
300 /* Store all valid general-purpose registers in GDB's register array
301 into the process/thread specified by TID. */
304 store_regs (const struct regcache *regcache, int tid, int regno)
308 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
309 perror_with_name (_("Couldn't get registers"));
311 fill_gregset (regcache, ®s, regno);
313 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
314 perror_with_name (_("Couldn't write registers"));
319 static void fetch_regs (struct regcache *regcache, int tid) {}
320 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
325 /* Transfering floating-point registers between GDB, inferiors and cores. */
327 /* What is the address of fpN within the floating-point register set F? */
328 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
330 /* Fill GDB's register array with the floating-point register values in
334 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
338 for (regi = gdbarch_fp0_regnum (current_gdbarch);
339 regi < gdbarch_fp0_regnum (current_gdbarch) + 8; regi++)
340 regcache_raw_supply (regcache, regi,
341 FPREG_ADDR (fpregsetp,
342 regi - gdbarch_fp0_regnum
344 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
345 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
346 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
349 /* Fill register REGNO (if it is a floating-point register) in
350 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
351 do this for all registers. */
354 fill_fpregset (const struct regcache *regcache,
355 elf_fpregset_t *fpregsetp, int regno)
359 /* Fill in the floating-point registers. */
360 for (i = gdbarch_fp0_regnum (current_gdbarch);
361 i < gdbarch_fp0_regnum (current_gdbarch) + 8; i++)
362 if (regno == -1 || regno == i)
363 regcache_raw_collect (regcache, i,
364 FPREG_ADDR (fpregsetp,
365 i - gdbarch_fp0_regnum
368 /* Fill in the floating-point control registers. */
369 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
370 if (regno == -1 || regno == i)
371 regcache_raw_collect (regcache, i,
372 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
375 #ifdef HAVE_PTRACE_GETREGS
377 /* Fetch all floating-point registers from process/thread TID and store
378 thier values in GDB's register array. */
381 fetch_fpregs (struct regcache *regcache, int tid)
383 elf_fpregset_t fpregs;
385 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
386 perror_with_name (_("Couldn't get floating point status"));
388 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
391 /* Store all valid floating-point registers in GDB's register array
392 into the process/thread specified by TID. */
395 store_fpregs (const struct regcache *regcache, int tid, int regno)
397 elf_fpregset_t fpregs;
399 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
400 perror_with_name (_("Couldn't get floating point status"));
402 fill_fpregset (regcache, &fpregs, regno);
404 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
405 perror_with_name (_("Couldn't write floating point status"));
410 static void fetch_fpregs (struct regcache *regcache, int tid) {}
411 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
415 /* Transferring arbitrary registers between GDB and inferior. */
417 /* Fetch register REGNO from the child process. If REGNO is -1, do
418 this for all registers (including the floating point and SSE
422 m68k_linux_fetch_inferior_registers (struct regcache *regcache, int regno)
426 /* Use the old method of peeking around in `struct user' if the
427 GETREGS request isn't available. */
428 if (! have_ptrace_getregs)
430 old_fetch_inferior_registers (regcache, regno);
434 /* GNU/Linux LWP ID's are process ID's. */
435 tid = TIDGET (inferior_ptid);
437 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
439 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
440 transfers more registers in one system call, and we'll cache the
441 results. But remember that fetch_fpxregs can fail, and return
445 fetch_regs (regcache, tid);
447 /* The call above might reset `have_ptrace_getregs'. */
448 if (! have_ptrace_getregs)
450 old_fetch_inferior_registers (regcache, -1);
454 fetch_fpregs (regcache, tid);
458 if (getregs_supplies (regno))
460 fetch_regs (regcache, tid);
464 if (getfpregs_supplies (regno))
466 fetch_fpregs (regcache, tid);
470 internal_error (__FILE__, __LINE__,
471 _("Got request for bad register number %d."), regno);
474 /* Store register REGNO back into the child process. If REGNO is -1,
475 do this for all registers (including the floating point and SSE
478 m68k_linux_store_inferior_registers (struct regcache *regcache, int regno)
482 /* Use the old method of poking around in `struct user' if the
483 SETREGS request isn't available. */
484 if (! have_ptrace_getregs)
486 old_store_inferior_registers (regcache, regno);
490 /* GNU/Linux LWP ID's are process ID's. */
491 tid = TIDGET (inferior_ptid);
493 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
495 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
496 transfers more registers in one system call. But remember that
497 store_fpregs can fail, and return zero. */
500 store_regs (regcache, tid, regno);
501 store_fpregs (regcache, tid, regno);
505 if (getregs_supplies (regno))
507 store_regs (regcache, tid, regno);
511 if (getfpregs_supplies (regno))
513 store_fpregs (regcache, tid, regno);
517 internal_error (__FILE__, __LINE__,
518 _("Got request to store bad register number %d."), regno);
521 /* Interpreting register set info found in core files. */
523 /* Provide registers to GDB from a core file.
525 (We can't use the generic version of this function in
526 core-regset.c, because we need to use elf_gregset_t instead of
529 CORE_REG_SECT points to an array of bytes, which are the contents
530 of a `note' from a core file which BFD thinks might contain
531 register contents. CORE_REG_SIZE is its size.
533 WHICH says which register set corelow suspects this is:
534 0 --- the general-purpose register set, in elf_gregset_t format
535 2 --- the floating-point register set, in elf_fpregset_t format
537 REG_ADDR isn't used on GNU/Linux. */
540 fetch_core_registers (struct regcache *regcache,
541 char *core_reg_sect, unsigned core_reg_size,
542 int which, CORE_ADDR reg_addr)
544 elf_gregset_t gregset;
545 elf_fpregset_t fpregset;
550 if (core_reg_size != sizeof (gregset))
551 warning (_("Wrong size gregset in core file."));
554 memcpy (&gregset, core_reg_sect, sizeof (gregset));
555 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
560 if (core_reg_size != sizeof (fpregset))
561 warning (_("Wrong size fpregset in core file."));
564 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
565 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
570 /* We've covered all the kinds of registers we know about here,
571 so this must be something we wouldn't know what to do with
572 anyway. Just ignore it. */
578 /* Register that we are able to handle GNU/Linux ELF core file
581 static struct core_fns linux_elf_core_fns =
583 bfd_target_elf_flavour, /* core_flavour */
584 default_check_format, /* check_format */
585 default_core_sniffer, /* core_sniffer */
586 fetch_core_registers, /* core_read_registers */
590 void _initialize_m68k_linux_nat (void);
593 _initialize_m68k_linux_nat (void)
595 struct target_ops *t;
597 /* Fill in the generic GNU/Linux methods. */
600 /* Add our register access methods. */
601 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
602 t->to_store_registers = m68k_linux_store_inferior_registers;
604 /* Register the target. */
605 linux_nat_add_target (t);
607 deprecated_add_core_fns (&linux_elf_core_fns);