1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996-2013 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"
34 #include <sys/ptrace.h>
36 #include <sys/ioctl.h>
38 #include <sys/procfs.h>
47 #include "floatformat.h"
51 /* Prototypes for supply_gregset etc. */
54 /* Defines ps_err_e, struct ps_prochandle. */
55 #include "gdb_proc_service.h"
57 #ifndef PTRACE_GET_THREAD_AREA
58 #define PTRACE_GET_THREAD_AREA 25
61 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
62 static const int regmap[] =
64 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
65 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
67 /* PT_FP0, ..., PT_FP7 */
68 21, 24, 27, 30, 33, 36, 39, 42,
69 /* PT_FPCR, PT_FPSR, PT_FPIAR */
73 /* Which ptrace request retrieves which registers?
74 These apply to the corresponding SET requests as well. */
75 #define NUM_GREGS (18)
76 #define MAX_NUM_REGS (NUM_GREGS + 11)
79 getregs_supplies (int regno)
81 return 0 <= regno && regno < NUM_GREGS;
85 getfpregs_supplies (int regno)
87 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
90 /* Does the current host support the GETREGS request? */
91 static int have_ptrace_getregs =
92 #ifdef HAVE_PTRACE_GETREGS
101 /* Fetching registers directly from the U area, one at a time. */
103 /* Fetch one register. */
106 fetch_register (struct regcache *regcache, int regno)
108 struct gdbarch *gdbarch = get_regcache_arch (regcache);
111 gdb_byte buf[MAX_REGISTER_SIZE];
114 /* Overload thread id onto process id. */
115 tid = ptid_get_lwp (inferior_ptid);
117 tid = ptid_get_pid (inferior_ptid); /* no thread id, just use
120 regaddr = 4 * regmap[regno];
121 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
124 val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
125 memcpy (&buf[i], &val, sizeof (long));
126 regaddr += sizeof (long);
128 error (_("Couldn't read register %s (#%d): %s."),
129 gdbarch_register_name (gdbarch, regno),
130 regno, safe_strerror (errno));
132 regcache_raw_supply (regcache, regno, buf);
135 /* Fetch register values from the inferior.
136 If REGNO is negative, do this for all registers.
137 Otherwise, REGNO specifies which register (so we can save time). */
140 old_fetch_inferior_registers (struct regcache *regcache, int regno)
144 fetch_register (regcache, regno);
149 regno < gdbarch_num_regs (get_regcache_arch (regcache));
152 fetch_register (regcache, regno);
157 /* Store one register. */
160 store_register (const struct regcache *regcache, int regno)
162 struct gdbarch *gdbarch = get_regcache_arch (regcache);
166 gdb_byte buf[MAX_REGISTER_SIZE];
168 /* Overload thread id onto process id. */
169 tid = ptid_get_lwp (inferior_ptid);
171 tid = ptid_get_pid (inferior_ptid); /* no thread id, just use
174 regaddr = 4 * regmap[regno];
176 /* Put the contents of regno into a local buffer. */
177 regcache_raw_collect (regcache, regno, buf);
179 /* Store the local buffer into the inferior a chunk at the time. */
180 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
183 memcpy (&val, &buf[i], sizeof (long));
184 ptrace (PTRACE_POKEUSER, tid, regaddr, val);
185 regaddr += sizeof (long);
187 error (_("Couldn't write register %s (#%d): %s."),
188 gdbarch_register_name (gdbarch, regno),
189 regno, safe_strerror (errno));
193 /* Store our register values back into the inferior.
194 If REGNO is negative, do this for all registers.
195 Otherwise, REGNO specifies which register (so we can save time). */
198 old_store_inferior_registers (const struct regcache *regcache, int regno)
202 store_register (regcache, regno);
207 regno < gdbarch_num_regs (get_regcache_arch (regcache));
210 store_register (regcache, regno);
215 /* Given a pointer to a general register set in /proc format
216 (elf_gregset_t *), unpack the register contents and supply
217 them as gdb's idea of the current register values. */
220 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
222 struct gdbarch *gdbarch = get_regcache_arch (regcache);
223 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
226 for (regi = M68K_D0_REGNUM;
227 regi <= gdbarch_sp_regnum (gdbarch);
229 regcache_raw_supply (regcache, regi, ®p[regmap[regi]]);
230 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
232 regcache_raw_supply (regcache,
233 gdbarch_pc_regnum (gdbarch), ®p[PT_PC]);
236 /* Fill register REGNO (if it is a general-purpose register) in
237 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
238 do this for all registers. */
240 fill_gregset (const struct regcache *regcache,
241 elf_gregset_t *gregsetp, int regno)
243 elf_greg_t *regp = (elf_greg_t *) gregsetp;
246 for (i = 0; i < NUM_GREGS; i++)
247 if (regno == -1 || regno == i)
248 regcache_raw_collect (regcache, i, regp + regmap[i]);
251 #ifdef HAVE_PTRACE_GETREGS
253 /* Fetch all general-purpose registers from process/thread TID and
254 store their values in GDB's register array. */
257 fetch_regs (struct regcache *regcache, int tid)
261 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
265 /* The kernel we're running on doesn't support the GETREGS
266 request. Reset `have_ptrace_getregs'. */
267 have_ptrace_getregs = 0;
271 perror_with_name (_("Couldn't get registers"));
274 supply_gregset (regcache, (const elf_gregset_t *) ®s);
277 /* Store all valid general-purpose registers in GDB's register array
278 into the process/thread specified by TID. */
281 store_regs (const struct regcache *regcache, int tid, int regno)
285 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
286 perror_with_name (_("Couldn't get registers"));
288 fill_gregset (regcache, ®s, regno);
290 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
291 perror_with_name (_("Couldn't write registers"));
296 static void fetch_regs (struct regcache *regcache, int tid)
300 static void store_regs (const struct regcache *regcache, int tid, int regno)
307 /* Transfering floating-point registers between GDB, inferiors and cores. */
309 /* What is the address of fpN within the floating-point register set F? */
310 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
312 /* Fill GDB's register array with the floating-point register values in
316 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
318 struct gdbarch *gdbarch = get_regcache_arch (regcache);
321 for (regi = gdbarch_fp0_regnum (gdbarch);
322 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
323 regcache_raw_supply (regcache, regi,
324 FPREG_ADDR (fpregsetp,
325 regi - gdbarch_fp0_regnum (gdbarch)));
326 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
327 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
328 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
331 /* Fill register REGNO (if it is a floating-point register) in
332 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
333 do this for all registers. */
336 fill_fpregset (const struct regcache *regcache,
337 elf_fpregset_t *fpregsetp, int regno)
339 struct gdbarch *gdbarch = get_regcache_arch (regcache);
342 /* Fill in the floating-point registers. */
343 for (i = gdbarch_fp0_regnum (gdbarch);
344 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
345 if (regno == -1 || regno == i)
346 regcache_raw_collect (regcache, i,
347 FPREG_ADDR (fpregsetp,
348 i - gdbarch_fp0_regnum (gdbarch)));
350 /* Fill in the floating-point control registers. */
351 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
352 if (regno == -1 || regno == i)
353 regcache_raw_collect (regcache, i,
354 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
357 #ifdef HAVE_PTRACE_GETREGS
359 /* Fetch all floating-point registers from process/thread TID and store
360 thier values in GDB's register array. */
363 fetch_fpregs (struct regcache *regcache, int tid)
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 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
373 /* Store all valid floating-point registers in GDB's register array
374 into the process/thread specified by TID. */
377 store_fpregs (const struct regcache *regcache, int tid, int regno)
379 elf_fpregset_t fpregs;
381 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
382 perror_with_name (_("Couldn't get floating point status"));
384 fill_fpregset (regcache, &fpregs, regno);
386 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
387 perror_with_name (_("Couldn't write floating point status"));
392 static void fetch_fpregs (struct regcache *regcache, int tid)
396 static void store_fpregs (const struct regcache *regcache, int tid, int regno)
402 /* Transferring arbitrary registers between GDB and inferior. */
404 /* Fetch register REGNO from the child process. If REGNO is -1, do
405 this for all registers (including the floating point and SSE
409 m68k_linux_fetch_inferior_registers (struct target_ops *ops,
410 struct regcache *regcache, int regno)
414 /* Use the old method of peeking around in `struct user' if the
415 GETREGS request isn't available. */
416 if (! have_ptrace_getregs)
418 old_fetch_inferior_registers (regcache, regno);
422 /* GNU/Linux LWP ID's are process ID's. */
423 tid = ptid_get_lwp (inferior_ptid);
425 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
427 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
428 transfers more registers in one system call, and we'll cache the
429 results. But remember that fetch_fpxregs can fail, and return
433 fetch_regs (regcache, tid);
435 /* The call above might reset `have_ptrace_getregs'. */
436 if (! have_ptrace_getregs)
438 old_fetch_inferior_registers (regcache, -1);
442 fetch_fpregs (regcache, tid);
446 if (getregs_supplies (regno))
448 fetch_regs (regcache, tid);
452 if (getfpregs_supplies (regno))
454 fetch_fpregs (regcache, tid);
458 internal_error (__FILE__, __LINE__,
459 _("Got request for bad register number %d."), regno);
462 /* Store register REGNO back into the child process. If REGNO is -1,
463 do this for all registers (including the floating point and SSE
466 m68k_linux_store_inferior_registers (struct target_ops *ops,
467 struct regcache *regcache, int regno)
471 /* Use the old method of poking around in `struct user' if the
472 SETREGS request isn't available. */
473 if (! have_ptrace_getregs)
475 old_store_inferior_registers (regcache, regno);
479 /* GNU/Linux LWP ID's are process ID's. */
480 tid = ptid_get_lwp (inferior_ptid);
482 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
484 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
485 transfers more registers in one system call. But remember that
486 store_fpregs can fail, and return zero. */
489 store_regs (regcache, tid, regno);
490 store_fpregs (regcache, tid, regno);
494 if (getregs_supplies (regno))
496 store_regs (regcache, tid, regno);
500 if (getfpregs_supplies (regno))
502 store_fpregs (regcache, tid, regno);
506 internal_error (__FILE__, __LINE__,
507 _("Got request to store bad register number %d."), regno);
510 /* Interpreting register set info found in core files. */
512 /* Provide registers to GDB from a core file.
514 (We can't use the generic version of this function in
515 core-regset.c, because we need to use elf_gregset_t instead of
518 CORE_REG_SECT points to an array of bytes, which are the contents
519 of a `note' from a core file which BFD thinks might contain
520 register contents. CORE_REG_SIZE is its size.
522 WHICH says which register set corelow suspects this is:
523 0 --- the general-purpose register set, in elf_gregset_t format
524 2 --- the floating-point register set, in elf_fpregset_t format
526 REG_ADDR isn't used on GNU/Linux. */
529 fetch_core_registers (struct regcache *regcache,
530 char *core_reg_sect, unsigned core_reg_size,
531 int which, CORE_ADDR reg_addr)
533 elf_gregset_t gregset;
534 elf_fpregset_t fpregset;
539 if (core_reg_size != sizeof (gregset))
540 warning (_("Wrong size gregset in core file."));
543 memcpy (&gregset, core_reg_sect, sizeof (gregset));
544 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
549 if (core_reg_size != sizeof (fpregset))
550 warning (_("Wrong size fpregset in core file."));
553 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
554 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
559 /* We've covered all the kinds of registers we know about here,
560 so this must be something we wouldn't know what to do with
561 anyway. Just ignore it. */
567 /* Fetch the thread-local storage pointer for libthread_db. */
570 ps_get_thread_area (const struct ps_prochandle *ph,
571 lwpid_t lwpid, int idx, void **base)
573 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
576 /* IDX is the bias from the thread pointer to the beginning of the
577 thread descriptor. It has to be subtracted due to implementation
578 quirks in libthread_db. */
579 *base = (char *) *base - idx;
585 /* Register that we are able to handle GNU/Linux ELF core file
588 static struct core_fns linux_elf_core_fns =
590 bfd_target_elf_flavour, /* core_flavour */
591 default_check_format, /* check_format */
592 default_core_sniffer, /* core_sniffer */
593 fetch_core_registers, /* core_read_registers */
597 void _initialize_m68k_linux_nat (void);
600 _initialize_m68k_linux_nat (void)
602 struct target_ops *t;
604 /* Fill in the generic GNU/Linux methods. */
607 /* Add our register access methods. */
608 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
609 t->to_store_registers = m68k_linux_store_inferior_registers;
611 /* Register the target. */
612 linux_nat_add_target (t);
614 deprecated_add_core_fns (&linux_elf_core_fns);