1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996, 1998, 2000-2012 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdb_string.h"
28 #include "linux-nat.h"
30 #include "m68k-tdep.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"
52 /* Prototypes for supply_gregset etc. */
55 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
56 static const int regmap[] =
58 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
59 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
61 /* PT_FP0, ..., PT_FP7 */
62 21, 24, 27, 30, 33, 36, 39, 42,
63 /* PT_FPCR, PT_FPSR, PT_FPIAR */
67 /* Which ptrace request retrieves which registers?
68 These apply to the corresponding SET requests as well. */
69 #define NUM_GREGS (18)
70 #define MAX_NUM_REGS (NUM_GREGS + 11)
73 getregs_supplies (int regno)
75 return 0 <= regno && regno < NUM_GREGS;
79 getfpregs_supplies (int regno)
81 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
84 /* Does the current host support the GETREGS request? */
85 int have_ptrace_getregs =
86 #ifdef HAVE_PTRACE_GETREGS
95 /* Fetching registers directly from the U area, one at a time. */
97 /* Fetch one register. */
100 fetch_register (struct regcache *regcache, int regno)
102 struct gdbarch *gdbarch = get_regcache_arch (regcache);
105 char buf[MAX_REGISTER_SIZE];
108 /* Overload thread id onto process id. */
109 tid = TIDGET (inferior_ptid);
111 tid = PIDGET (inferior_ptid); /* no thread id, just use
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
167 regaddr = 4 * regmap[regno];
169 /* Put the contents of regno into a local buffer. */
170 regcache_raw_collect (regcache, regno, buf);
172 /* Store the local buffer into the inferior a chunk at the time. */
173 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
176 ptrace (PTRACE_POKEUSER, tid, regaddr, *(long *) &buf[i]);
177 regaddr += sizeof (long);
179 error (_("Couldn't write register %s (#%d): %s."),
180 gdbarch_register_name (gdbarch, regno),
181 regno, safe_strerror (errno));
185 /* Store our register values back into the inferior.
186 If REGNO is negative, do this for all registers.
187 Otherwise, REGNO specifies which register (so we can save time). */
190 old_store_inferior_registers (const struct regcache *regcache, int regno)
194 store_register (regcache, regno);
199 regno < gdbarch_num_regs (get_regcache_arch (regcache));
202 store_register (regcache, regno);
207 /* Given a pointer to a general register set in /proc format
208 (elf_gregset_t *), unpack the register contents and supply
209 them as gdb's idea of the current register values. */
212 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
214 struct gdbarch *gdbarch = get_regcache_arch (regcache);
215 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
218 for (regi = M68K_D0_REGNUM;
219 regi <= gdbarch_sp_regnum (gdbarch);
221 regcache_raw_supply (regcache, regi, ®p[regmap[regi]]);
222 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
224 regcache_raw_supply (regcache,
225 gdbarch_pc_regnum (gdbarch), ®p[PT_PC]);
228 /* Fill register REGNO (if it is a general-purpose register) in
229 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
230 do this for all registers. */
232 fill_gregset (const struct regcache *regcache,
233 elf_gregset_t *gregsetp, int regno)
235 elf_greg_t *regp = (elf_greg_t *) gregsetp;
238 for (i = 0; i < NUM_GREGS; i++)
239 if (regno == -1 || regno == i)
240 regcache_raw_collect (regcache, i, regp + regmap[i]);
243 #ifdef HAVE_PTRACE_GETREGS
245 /* Fetch all general-purpose registers from process/thread TID and
246 store their values in GDB's register array. */
249 fetch_regs (struct regcache *regcache, int tid)
253 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
257 /* The kernel we're running on doesn't support the GETREGS
258 request. Reset `have_ptrace_getregs'. */
259 have_ptrace_getregs = 0;
263 perror_with_name (_("Couldn't get registers"));
266 supply_gregset (regcache, (const elf_gregset_t *) ®s);
269 /* Store all valid general-purpose registers in GDB's register array
270 into the process/thread specified by TID. */
273 store_regs (const struct regcache *regcache, int tid, int regno)
277 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
278 perror_with_name (_("Couldn't get registers"));
280 fill_gregset (regcache, ®s, regno);
282 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
283 perror_with_name (_("Couldn't write registers"));
288 static void fetch_regs (struct regcache *regcache, int tid)
292 static void store_regs (const struct regcache *regcache, int tid, int regno)
299 /* Transfering floating-point registers between GDB, inferiors and cores. */
301 /* What is the address of fpN within the floating-point register set F? */
302 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
304 /* Fill GDB's register array with the floating-point register values in
308 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
310 struct gdbarch *gdbarch = get_regcache_arch (regcache);
313 for (regi = gdbarch_fp0_regnum (gdbarch);
314 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
315 regcache_raw_supply (regcache, regi,
316 FPREG_ADDR (fpregsetp,
317 regi - gdbarch_fp0_regnum (gdbarch)));
318 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
319 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
320 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
323 /* Fill register REGNO (if it is a floating-point register) in
324 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
325 do this for all registers. */
328 fill_fpregset (const struct regcache *regcache,
329 elf_fpregset_t *fpregsetp, int regno)
331 struct gdbarch *gdbarch = get_regcache_arch (regcache);
334 /* Fill in the floating-point registers. */
335 for (i = gdbarch_fp0_regnum (gdbarch);
336 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
337 if (regno == -1 || regno == i)
338 regcache_raw_collect (regcache, i,
339 FPREG_ADDR (fpregsetp,
340 i - gdbarch_fp0_regnum (gdbarch)));
342 /* Fill in the floating-point control registers. */
343 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
344 if (regno == -1 || regno == i)
345 regcache_raw_collect (regcache, i,
346 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
349 #ifdef HAVE_PTRACE_GETREGS
351 /* Fetch all floating-point registers from process/thread TID and store
352 thier values in GDB's register array. */
355 fetch_fpregs (struct regcache *regcache, int tid)
357 elf_fpregset_t fpregs;
359 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
360 perror_with_name (_("Couldn't get floating point status"));
362 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
365 /* Store all valid floating-point registers in GDB's register array
366 into the process/thread specified by TID. */
369 store_fpregs (const struct regcache *regcache, int tid, int regno)
371 elf_fpregset_t fpregs;
373 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
374 perror_with_name (_("Couldn't get floating point status"));
376 fill_fpregset (regcache, &fpregs, regno);
378 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
379 perror_with_name (_("Couldn't write floating point status"));
384 static void fetch_fpregs (struct regcache *regcache, int tid)
388 static void store_fpregs (const struct regcache *regcache, int tid, int regno)
394 /* Transferring arbitrary registers between GDB and inferior. */
396 /* Fetch register REGNO from the child process. If REGNO is -1, do
397 this for all registers (including the floating point and SSE
401 m68k_linux_fetch_inferior_registers (struct target_ops *ops,
402 struct regcache *regcache, int regno)
406 /* Use the old method of peeking around in `struct user' if the
407 GETREGS request isn't available. */
408 if (! have_ptrace_getregs)
410 old_fetch_inferior_registers (regcache, regno);
414 /* GNU/Linux LWP ID's are process ID's. */
415 tid = TIDGET (inferior_ptid);
417 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
419 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
420 transfers more registers in one system call, and we'll cache the
421 results. But remember that fetch_fpxregs can fail, and return
425 fetch_regs (regcache, tid);
427 /* The call above might reset `have_ptrace_getregs'. */
428 if (! have_ptrace_getregs)
430 old_fetch_inferior_registers (regcache, -1);
434 fetch_fpregs (regcache, tid);
438 if (getregs_supplies (regno))
440 fetch_regs (regcache, tid);
444 if (getfpregs_supplies (regno))
446 fetch_fpregs (regcache, tid);
450 internal_error (__FILE__, __LINE__,
451 _("Got request for bad register number %d."), regno);
454 /* Store register REGNO back into the child process. If REGNO is -1,
455 do this for all registers (including the floating point and SSE
458 m68k_linux_store_inferior_registers (struct target_ops *ops,
459 struct regcache *regcache, int regno)
463 /* Use the old method of poking around in `struct user' if the
464 SETREGS request isn't available. */
465 if (! have_ptrace_getregs)
467 old_store_inferior_registers (regcache, regno);
471 /* GNU/Linux LWP ID's are process ID's. */
472 tid = TIDGET (inferior_ptid);
474 tid = PIDGET (inferior_ptid); /* Not a threaded program. */
476 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
477 transfers more registers in one system call. But remember that
478 store_fpregs can fail, and return zero. */
481 store_regs (regcache, tid, regno);
482 store_fpregs (regcache, tid, regno);
486 if (getregs_supplies (regno))
488 store_regs (regcache, tid, regno);
492 if (getfpregs_supplies (regno))
494 store_fpregs (regcache, tid, regno);
498 internal_error (__FILE__, __LINE__,
499 _("Got request to store bad register number %d."), regno);
502 /* Interpreting register set info found in core files. */
504 /* Provide registers to GDB from a core file.
506 (We can't use the generic version of this function in
507 core-regset.c, because we need to use elf_gregset_t instead of
510 CORE_REG_SECT points to an array of bytes, which are the contents
511 of a `note' from a core file which BFD thinks might contain
512 register contents. CORE_REG_SIZE is its size.
514 WHICH says which register set corelow suspects this is:
515 0 --- the general-purpose register set, in elf_gregset_t format
516 2 --- the floating-point register set, in elf_fpregset_t format
518 REG_ADDR isn't used on GNU/Linux. */
521 fetch_core_registers (struct regcache *regcache,
522 char *core_reg_sect, unsigned core_reg_size,
523 int which, CORE_ADDR reg_addr)
525 elf_gregset_t gregset;
526 elf_fpregset_t fpregset;
531 if (core_reg_size != sizeof (gregset))
532 warning (_("Wrong size gregset in core file."));
535 memcpy (&gregset, core_reg_sect, sizeof (gregset));
536 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
541 if (core_reg_size != sizeof (fpregset))
542 warning (_("Wrong size fpregset in core file."));
545 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
546 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
551 /* We've covered all the kinds of registers we know about here,
552 so this must be something we wouldn't know what to do with
553 anyway. Just ignore it. */
559 /* Register that we are able to handle GNU/Linux ELF core file
562 static struct core_fns linux_elf_core_fns =
564 bfd_target_elf_flavour, /* core_flavour */
565 default_check_format, /* check_format */
566 default_core_sniffer, /* core_sniffer */
567 fetch_core_registers, /* core_read_registers */
571 void _initialize_m68k_linux_nat (void);
574 _initialize_m68k_linux_nat (void)
576 struct target_ops *t;
578 /* Fill in the generic GNU/Linux methods. */
581 /* Add our register access methods. */
582 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
583 t->to_store_registers = m68k_linux_store_inferior_registers;
585 /* Register the target. */
586 linux_nat_add_target (t);
588 deprecated_add_core_fns (&linux_elf_core_fns);