1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 2008, 2009, 2010, 2011 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 3 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, see <http://www.gnu.org/licenses/>. */
26 #include "gdb_string.h"
29 #include "linux-nat.h"
31 #include "m68k-tdep.h"
33 #include <sys/param.h>
36 #include <sys/ptrace.h>
38 #include <sys/ioctl.h>
40 #include <sys/procfs.h>
49 #include "floatformat.h"
53 /* Prototypes for supply_gregset etc. */
56 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
57 static const int regmap[] =
59 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
60 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
62 /* PT_FP0, ..., PT_FP7 */
63 21, 24, 27, 30, 33, 36, 39, 42,
64 /* PT_FPCR, PT_FPSR, PT_FPIAR */
68 /* Which ptrace request retrieves which registers?
69 These apply to the corresponding SET requests as well. */
70 #define NUM_GREGS (18)
71 #define MAX_NUM_REGS (NUM_GREGS + 11)
74 getregs_supplies (int regno)
76 return 0 <= regno && regno < NUM_GREGS;
80 getfpregs_supplies (int regno)
82 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
85 /* Does the current host support the GETREGS request? */
86 int have_ptrace_getregs =
87 #ifdef HAVE_PTRACE_GETREGS
96 /* Fetching registers directly from the U area, one at a time. */
98 /* Fetch one register. */
101 fetch_register (struct regcache *regcache, int regno)
103 struct gdbarch *gdbarch = get_regcache_arch (regcache);
106 char buf[MAX_REGISTER_SIZE];
109 /* Overload thread id onto process id. */
110 tid = TIDGET (inferior_ptid);
112 tid = PIDGET (inferior_ptid); /* no thread id, just use
115 regaddr = 4 * regmap[regno];
116 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
119 *(long *) &buf[i] = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
120 regaddr += sizeof (long);
122 error (_("Couldn't read register %s (#%d): %s."),
123 gdbarch_register_name (gdbarch, regno),
124 regno, safe_strerror (errno));
126 regcache_raw_supply (regcache, regno, buf);
129 /* Fetch register values from the inferior.
130 If REGNO is negative, do this for all registers.
131 Otherwise, REGNO specifies which register (so we can save time). */
134 old_fetch_inferior_registers (struct regcache *regcache, int regno)
138 fetch_register (regcache, regno);
143 regno < gdbarch_num_regs (get_regcache_arch (regcache));
146 fetch_register (regcache, regno);
151 /* Store one register. */
154 store_register (const struct regcache *regcache, int regno)
156 struct gdbarch *gdbarch = get_regcache_arch (regcache);
160 char buf[MAX_REGISTER_SIZE];
162 /* Overload thread id onto process id. */
163 tid = TIDGET (inferior_ptid);
165 tid = PIDGET (inferior_ptid); /* no thread id, just use
168 regaddr = 4 * regmap[regno];
170 /* Put the contents of regno into a local buffer. */
171 regcache_raw_collect (regcache, regno, buf);
173 /* Store the local buffer into the inferior a chunk at the time. */
174 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
177 ptrace (PTRACE_POKEUSER, tid, regaddr, *(long *) &buf[i]);
178 regaddr += sizeof (long);
180 error (_("Couldn't write register %s (#%d): %s."),
181 gdbarch_register_name (gdbarch, regno),
182 regno, safe_strerror (errno));
186 /* Store our register values back into the inferior.
187 If REGNO is negative, do this for all registers.
188 Otherwise, REGNO specifies which register (so we can save time). */
191 old_store_inferior_registers (const struct regcache *regcache, int regno)
195 store_register (regcache, regno);
200 regno < gdbarch_num_regs (get_regcache_arch (regcache));
203 store_register (regcache, regno);
208 /* Given a pointer to a general register set in /proc format
209 (elf_gregset_t *), unpack the register contents and supply
210 them as gdb's idea of the current register values. */
213 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
215 struct gdbarch *gdbarch = get_regcache_arch (regcache);
216 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
219 for (regi = M68K_D0_REGNUM;
220 regi <= gdbarch_sp_regnum (gdbarch);
222 regcache_raw_supply (regcache, regi, ®p[regmap[regi]]);
223 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
225 regcache_raw_supply (regcache,
226 gdbarch_pc_regnum (gdbarch), ®p[PT_PC]);
229 /* Fill register REGNO (if it is a general-purpose register) in
230 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
231 do this for all registers. */
233 fill_gregset (const struct regcache *regcache,
234 elf_gregset_t *gregsetp, int regno)
236 elf_greg_t *regp = (elf_greg_t *) gregsetp;
239 for (i = 0; i < NUM_GREGS; i++)
240 if (regno == -1 || regno == i)
241 regcache_raw_collect (regcache, i, regp + regmap[i]);
244 #ifdef HAVE_PTRACE_GETREGS
246 /* Fetch all general-purpose registers from process/thread TID and
247 store their values in GDB's register array. */
250 fetch_regs (struct regcache *regcache, int tid)
254 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
258 /* The kernel we're running on doesn't support the GETREGS
259 request. Reset `have_ptrace_getregs'. */
260 have_ptrace_getregs = 0;
264 perror_with_name (_("Couldn't get registers"));
267 supply_gregset (regcache, (const elf_gregset_t *) ®s);
270 /* Store all valid general-purpose registers in GDB's register array
271 into the process/thread specified by TID. */
274 store_regs (const struct regcache *regcache, int tid, int regno)
278 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
279 perror_with_name (_("Couldn't get registers"));
281 fill_gregset (regcache, ®s, regno);
283 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
284 perror_with_name (_("Couldn't write registers"));
289 static void fetch_regs (struct regcache *regcache, int tid)
293 static void store_regs (const struct regcache *regcache, int tid, int regno)
300 /* Transfering floating-point registers between GDB, inferiors and cores. */
302 /* What is the address of fpN within the floating-point register set F? */
303 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
305 /* Fill GDB's register array with the floating-point register values in
309 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
311 struct gdbarch *gdbarch = get_regcache_arch (regcache);
314 for (regi = gdbarch_fp0_regnum (gdbarch);
315 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
316 regcache_raw_supply (regcache, regi,
317 FPREG_ADDR (fpregsetp,
318 regi - gdbarch_fp0_regnum (gdbarch)));
319 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
320 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
321 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
324 /* Fill register REGNO (if it is a floating-point register) in
325 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
326 do this for all registers. */
329 fill_fpregset (const struct regcache *regcache,
330 elf_fpregset_t *fpregsetp, int regno)
332 struct gdbarch *gdbarch = get_regcache_arch (regcache);
335 /* Fill in the floating-point registers. */
336 for (i = gdbarch_fp0_regnum (gdbarch);
337 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
338 if (regno == -1 || regno == i)
339 regcache_raw_collect (regcache, i,
340 FPREG_ADDR (fpregsetp,
341 i - gdbarch_fp0_regnum (gdbarch)));
343 /* Fill in the floating-point control registers. */
344 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
345 if (regno == -1 || regno == i)
346 regcache_raw_collect (regcache, i,
347 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
350 #ifdef HAVE_PTRACE_GETREGS
352 /* Fetch all floating-point registers from process/thread TID and store
353 thier values in GDB's register array. */
356 fetch_fpregs (struct regcache *regcache, int tid)
358 elf_fpregset_t fpregs;
360 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
361 perror_with_name (_("Couldn't get floating point status"));
363 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
366 /* Store all valid floating-point registers in GDB's register array
367 into the process/thread specified by TID. */
370 store_fpregs (const struct regcache *regcache, int tid, int regno)
372 elf_fpregset_t fpregs;
374 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
375 perror_with_name (_("Couldn't get floating point status"));
377 fill_fpregset (regcache, &fpregs, regno);
379 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
380 perror_with_name (_("Couldn't write floating point status"));
385 static void fetch_fpregs (struct regcache *regcache, int tid)
389 static void store_fpregs (const struct regcache *regcache, int tid, int regno)
395 /* Transferring arbitrary registers between GDB and inferior. */
397 /* Fetch register REGNO from the child process. If REGNO is -1, do
398 this for all registers (including the floating point and SSE
402 m68k_linux_fetch_inferior_registers (struct target_ops *ops,
403 struct regcache *regcache, int regno)
407 /* Use the old method of peeking around in `struct user' if the
408 GETREGS request isn't available. */
409 if (! have_ptrace_getregs)
411 old_fetch_inferior_registers (regcache, regno);
415 /* GNU/Linux LWP ID's are process ID's. */
416 tid = TIDGET (inferior_ptid);
418 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
420 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
421 transfers more registers in one system call, and we'll cache the
422 results. But remember that fetch_fpxregs can fail, and return
426 fetch_regs (regcache, tid);
428 /* The call above might reset `have_ptrace_getregs'. */
429 if (! have_ptrace_getregs)
431 old_fetch_inferior_registers (regcache, -1);
435 fetch_fpregs (regcache, tid);
439 if (getregs_supplies (regno))
441 fetch_regs (regcache, tid);
445 if (getfpregs_supplies (regno))
447 fetch_fpregs (regcache, tid);
451 internal_error (__FILE__, __LINE__,
452 _("Got request for bad register number %d."), regno);
455 /* Store register REGNO back into the child process. If REGNO is -1,
456 do this for all registers (including the floating point and SSE
459 m68k_linux_store_inferior_registers (struct target_ops *ops,
460 struct regcache *regcache, int regno)
464 /* Use the old method of poking around in `struct user' if the
465 SETREGS request isn't available. */
466 if (! have_ptrace_getregs)
468 old_store_inferior_registers (regcache, regno);
472 /* GNU/Linux LWP ID's are process ID's. */
473 tid = TIDGET (inferior_ptid);
475 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
477 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
478 transfers more registers in one system call. But remember that
479 store_fpregs can fail, and return zero. */
482 store_regs (regcache, tid, regno);
483 store_fpregs (regcache, tid, regno);
487 if (getregs_supplies (regno))
489 store_regs (regcache, tid, regno);
493 if (getfpregs_supplies (regno))
495 store_fpregs (regcache, tid, regno);
499 internal_error (__FILE__, __LINE__,
500 _("Got request to store bad register number %d."), regno);
503 /* Interpreting register set info found in core files. */
505 /* Provide registers to GDB from a core file.
507 (We can't use the generic version of this function in
508 core-regset.c, because we need to use elf_gregset_t instead of
511 CORE_REG_SECT points to an array of bytes, which are the contents
512 of a `note' from a core file which BFD thinks might contain
513 register contents. CORE_REG_SIZE is its size.
515 WHICH says which register set corelow suspects this is:
516 0 --- the general-purpose register set, in elf_gregset_t format
517 2 --- the floating-point register set, in elf_fpregset_t format
519 REG_ADDR isn't used on GNU/Linux. */
522 fetch_core_registers (struct regcache *regcache,
523 char *core_reg_sect, unsigned core_reg_size,
524 int which, CORE_ADDR reg_addr)
526 elf_gregset_t gregset;
527 elf_fpregset_t fpregset;
532 if (core_reg_size != sizeof (gregset))
533 warning (_("Wrong size gregset in core file."));
536 memcpy (&gregset, core_reg_sect, sizeof (gregset));
537 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
542 if (core_reg_size != sizeof (fpregset))
543 warning (_("Wrong size fpregset in core file."));
546 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
547 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
552 /* We've covered all the kinds of registers we know about here,
553 so this must be something we wouldn't know what to do with
554 anyway. Just ignore it. */
560 /* Register that we are able to handle GNU/Linux ELF core file
563 static struct core_fns linux_elf_core_fns =
565 bfd_target_elf_flavour, /* core_flavour */
566 default_check_format, /* check_format */
567 default_core_sniffer, /* core_sniffer */
568 fetch_core_registers, /* core_read_registers */
572 void _initialize_m68k_linux_nat (void);
575 _initialize_m68k_linux_nat (void)
577 struct target_ops *t;
579 /* Fill in the generic GNU/Linux methods. */
582 /* Add our register access methods. */
583 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
584 t->to_store_registers = m68k_linux_store_inferior_registers;
586 /* Register the target. */
587 linux_nat_add_target (t);
589 deprecated_add_core_fns (&linux_elf_core_fns);