1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 2008, 2009 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 process id */
114 regaddr = 4 * regmap[regno];
115 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
118 *(long *) &buf[i] = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
119 regaddr += sizeof (long);
121 error (_("Couldn't read register %s (#%d): %s."),
122 gdbarch_register_name (gdbarch, regno),
123 regno, safe_strerror (errno));
125 regcache_raw_supply (regcache, regno, buf);
128 /* Fetch register values from the inferior.
129 If REGNO is negative, do this for all registers.
130 Otherwise, REGNO specifies which register (so we can save time). */
133 old_fetch_inferior_registers (struct regcache *regcache, int regno)
137 fetch_register (regcache, regno);
142 regno < gdbarch_num_regs (get_regcache_arch (regcache));
145 fetch_register (regcache, regno);
150 /* Store one register. */
153 store_register (const struct regcache *regcache, int regno)
155 struct gdbarch *gdbarch = get_regcache_arch (regcache);
159 char buf[MAX_REGISTER_SIZE];
161 /* Overload thread id onto process id */
162 tid = TIDGET (inferior_ptid);
164 tid = PIDGET (inferior_ptid); /* no thread id, just use process id */
166 regaddr = 4 * regmap[regno];
168 /* Put the contents of regno into a local buffer */
169 regcache_raw_collect (regcache, regno, buf);
171 /* Store the local buffer into the inferior a chunk at the time. */
172 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
175 ptrace (PTRACE_POKEUSER, tid, regaddr, *(long *) &buf[i]);
176 regaddr += sizeof (long);
178 error (_("Couldn't write register %s (#%d): %s."),
179 gdbarch_register_name (gdbarch, regno),
180 regno, safe_strerror (errno));
184 /* Store our register values back into the inferior.
185 If REGNO is negative, do this for all registers.
186 Otherwise, REGNO specifies which register (so we can save time). */
189 old_store_inferior_registers (const struct regcache *regcache, int regno)
193 store_register (regcache, regno);
198 regno < gdbarch_num_regs (get_regcache_arch (regcache));
201 store_register (regcache, regno);
206 /* Given a pointer to a general register set in /proc format
207 (elf_gregset_t *), unpack the register contents and supply
208 them as gdb's idea of the current register values. */
211 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
213 struct gdbarch *gdbarch = get_regcache_arch (regcache);
214 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
217 for (regi = M68K_D0_REGNUM;
218 regi <= gdbarch_sp_regnum (gdbarch);
220 regcache_raw_supply (regcache, regi, ®p[regmap[regi]]);
221 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
223 regcache_raw_supply (regcache,
224 gdbarch_pc_regnum (gdbarch), ®p[PT_PC]);
227 /* Fill register REGNO (if it is a general-purpose register) in
228 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
229 do this for all registers. */
231 fill_gregset (const struct regcache *regcache,
232 elf_gregset_t *gregsetp, int regno)
234 elf_greg_t *regp = (elf_greg_t *) gregsetp;
237 for (i = 0; i < NUM_GREGS; i++)
238 if (regno == -1 || regno == i)
239 regcache_raw_collect (regcache, i, regp + regmap[i]);
242 #ifdef HAVE_PTRACE_GETREGS
244 /* Fetch all general-purpose registers from process/thread TID and
245 store their values in GDB's register array. */
248 fetch_regs (struct regcache *regcache, int tid)
252 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
256 /* The kernel we're running on doesn't support the GETREGS
257 request. Reset `have_ptrace_getregs'. */
258 have_ptrace_getregs = 0;
262 perror_with_name (_("Couldn't get registers"));
265 supply_gregset (regcache, (const elf_gregset_t *) ®s);
268 /* Store all valid general-purpose registers in GDB's register array
269 into the process/thread specified by TID. */
272 store_regs (const struct regcache *regcache, int tid, int regno)
276 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
277 perror_with_name (_("Couldn't get registers"));
279 fill_gregset (regcache, ®s, regno);
281 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
282 perror_with_name (_("Couldn't write registers"));
287 static void fetch_regs (struct regcache *regcache, int tid) {}
288 static void store_regs (const struct regcache *regcache, int tid, int regno) {}
293 /* Transfering floating-point registers between GDB, inferiors and cores. */
295 /* What is the address of fpN within the floating-point register set F? */
296 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
298 /* Fill GDB's register array with the floating-point register values in
302 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
304 struct gdbarch *gdbarch = get_regcache_arch (regcache);
307 for (regi = gdbarch_fp0_regnum (gdbarch);
308 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
309 regcache_raw_supply (regcache, regi,
310 FPREG_ADDR (fpregsetp,
311 regi - gdbarch_fp0_regnum (gdbarch)));
312 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
313 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
314 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
317 /* Fill register REGNO (if it is a floating-point register) in
318 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
319 do this for all registers. */
322 fill_fpregset (const struct regcache *regcache,
323 elf_fpregset_t *fpregsetp, int regno)
325 struct gdbarch *gdbarch = get_regcache_arch (regcache);
328 /* Fill in the floating-point registers. */
329 for (i = gdbarch_fp0_regnum (gdbarch);
330 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
331 if (regno == -1 || regno == i)
332 regcache_raw_collect (regcache, i,
333 FPREG_ADDR (fpregsetp,
334 i - gdbarch_fp0_regnum (gdbarch)));
336 /* Fill in the floating-point control registers. */
337 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
338 if (regno == -1 || regno == i)
339 regcache_raw_collect (regcache, i,
340 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
343 #ifdef HAVE_PTRACE_GETREGS
345 /* Fetch all floating-point registers from process/thread TID and store
346 thier values in GDB's register array. */
349 fetch_fpregs (struct regcache *regcache, int tid)
351 elf_fpregset_t fpregs;
353 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
354 perror_with_name (_("Couldn't get floating point status"));
356 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
359 /* Store all valid floating-point registers in GDB's register array
360 into the process/thread specified by TID. */
363 store_fpregs (const struct regcache *regcache, int tid, int regno)
365 elf_fpregset_t fpregs;
367 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
368 perror_with_name (_("Couldn't get floating point status"));
370 fill_fpregset (regcache, &fpregs, regno);
372 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
373 perror_with_name (_("Couldn't write floating point status"));
378 static void fetch_fpregs (struct regcache *regcache, int tid) {}
379 static void store_fpregs (const struct regcache *regcache, int tid, int regno) {}
383 /* Transferring arbitrary registers between GDB and inferior. */
385 /* Fetch register REGNO from the child process. If REGNO is -1, do
386 this for all registers (including the floating point and SSE
390 m68k_linux_fetch_inferior_registers (struct target_ops *ops,
391 struct regcache *regcache, int regno)
395 /* Use the old method of peeking around in `struct user' if the
396 GETREGS request isn't available. */
397 if (! have_ptrace_getregs)
399 old_fetch_inferior_registers (regcache, regno);
403 /* GNU/Linux LWP ID's are process ID's. */
404 tid = TIDGET (inferior_ptid);
406 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
408 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
409 transfers more registers in one system call, and we'll cache the
410 results. But remember that fetch_fpxregs can fail, and return
414 fetch_regs (regcache, tid);
416 /* The call above might reset `have_ptrace_getregs'. */
417 if (! have_ptrace_getregs)
419 old_fetch_inferior_registers (regcache, -1);
423 fetch_fpregs (regcache, tid);
427 if (getregs_supplies (regno))
429 fetch_regs (regcache, tid);
433 if (getfpregs_supplies (regno))
435 fetch_fpregs (regcache, tid);
439 internal_error (__FILE__, __LINE__,
440 _("Got request for bad register number %d."), regno);
443 /* Store register REGNO back into the child process. If REGNO is -1,
444 do this for all registers (including the floating point and SSE
447 m68k_linux_store_inferior_registers (struct target_ops *ops,
448 struct regcache *regcache, int regno)
452 /* Use the old method of poking around in `struct user' if the
453 SETREGS request isn't available. */
454 if (! have_ptrace_getregs)
456 old_store_inferior_registers (regcache, regno);
460 /* GNU/Linux LWP ID's are process ID's. */
461 tid = TIDGET (inferior_ptid);
463 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
465 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
466 transfers more registers in one system call. But remember that
467 store_fpregs can fail, and return zero. */
470 store_regs (regcache, tid, regno);
471 store_fpregs (regcache, tid, regno);
475 if (getregs_supplies (regno))
477 store_regs (regcache, tid, regno);
481 if (getfpregs_supplies (regno))
483 store_fpregs (regcache, tid, regno);
487 internal_error (__FILE__, __LINE__,
488 _("Got request to store bad register number %d."), regno);
491 /* Interpreting register set info found in core files. */
493 /* Provide registers to GDB from a core file.
495 (We can't use the generic version of this function in
496 core-regset.c, because we need to use elf_gregset_t instead of
499 CORE_REG_SECT points to an array of bytes, which are the contents
500 of a `note' from a core file which BFD thinks might contain
501 register contents. CORE_REG_SIZE is its size.
503 WHICH says which register set corelow suspects this is:
504 0 --- the general-purpose register set, in elf_gregset_t format
505 2 --- the floating-point register set, in elf_fpregset_t format
507 REG_ADDR isn't used on GNU/Linux. */
510 fetch_core_registers (struct regcache *regcache,
511 char *core_reg_sect, unsigned core_reg_size,
512 int which, CORE_ADDR reg_addr)
514 elf_gregset_t gregset;
515 elf_fpregset_t fpregset;
520 if (core_reg_size != sizeof (gregset))
521 warning (_("Wrong size gregset in core file."));
524 memcpy (&gregset, core_reg_sect, sizeof (gregset));
525 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
530 if (core_reg_size != sizeof (fpregset))
531 warning (_("Wrong size fpregset in core file."));
534 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
535 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
540 /* We've covered all the kinds of registers we know about here,
541 so this must be something we wouldn't know what to do with
542 anyway. Just ignore it. */
548 /* Register that we are able to handle GNU/Linux ELF core file
551 static struct core_fns linux_elf_core_fns =
553 bfd_target_elf_flavour, /* core_flavour */
554 default_check_format, /* check_format */
555 default_core_sniffer, /* core_sniffer */
556 fetch_core_registers, /* core_read_registers */
560 void _initialize_m68k_linux_nat (void);
563 _initialize_m68k_linux_nat (void)
565 struct target_ops *t;
567 /* Fill in the generic GNU/Linux methods. */
570 /* Add our register access methods. */
571 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
572 t->to_store_registers = m68k_linux_store_inferior_registers;
574 /* Register the target. */
575 linux_nat_add_target (t);
577 deprecated_add_core_fns (&linux_elf_core_fns);