1 /* Motorola m68k native support for GNU/Linux.
3 Copyright (C) 1996-2014 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/>. */
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"
49 /* Prototypes for supply_gregset etc. */
52 /* Defines ps_err_e, struct ps_prochandle. */
53 #include "gdb_proc_service.h"
55 #ifndef PTRACE_GET_THREAD_AREA
56 #define PTRACE_GET_THREAD_AREA 25
59 /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
60 static const int regmap[] =
62 PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
63 PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
65 /* PT_FP0, ..., PT_FP7 */
66 21, 24, 27, 30, 33, 36, 39, 42,
67 /* PT_FPCR, PT_FPSR, PT_FPIAR */
71 /* Which ptrace request retrieves which registers?
72 These apply to the corresponding SET requests as well. */
73 #define NUM_GREGS (18)
74 #define MAX_NUM_REGS (NUM_GREGS + 11)
77 getregs_supplies (int regno)
79 return 0 <= regno && regno < NUM_GREGS;
83 getfpregs_supplies (int regno)
85 return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
88 /* Does the current host support the GETREGS request? */
89 static int have_ptrace_getregs =
90 #ifdef HAVE_PTRACE_GETREGS
99 /* Fetching registers directly from the U area, one at a time. */
101 /* Fetch one register. */
104 fetch_register (struct regcache *regcache, int regno)
106 struct gdbarch *gdbarch = get_regcache_arch (regcache);
109 gdb_byte buf[MAX_REGISTER_SIZE];
112 /* Overload thread id onto process id. */
113 tid = ptid_get_lwp (inferior_ptid);
115 tid = ptid_get_pid (inferior_ptid); /* no thread id, just use
118 regaddr = 4 * regmap[regno];
119 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
122 val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
123 memcpy (&buf[i], &val, sizeof (long));
124 regaddr += sizeof (long);
126 error (_("Couldn't read register %s (#%d): %s."),
127 gdbarch_register_name (gdbarch, regno),
128 regno, safe_strerror (errno));
130 regcache_raw_supply (regcache, regno, buf);
133 /* Fetch register values from the inferior.
134 If REGNO is negative, do this for all registers.
135 Otherwise, REGNO specifies which register (so we can save time). */
138 old_fetch_inferior_registers (struct regcache *regcache, int regno)
142 fetch_register (regcache, regno);
147 regno < gdbarch_num_regs (get_regcache_arch (regcache));
150 fetch_register (regcache, regno);
155 /* Store one register. */
158 store_register (const struct regcache *regcache, int regno)
160 struct gdbarch *gdbarch = get_regcache_arch (regcache);
164 gdb_byte buf[MAX_REGISTER_SIZE];
166 /* Overload thread id onto process id. */
167 tid = ptid_get_lwp (inferior_ptid);
169 tid = ptid_get_pid (inferior_ptid); /* no thread id, just use
172 regaddr = 4 * regmap[regno];
174 /* Put the contents of regno into a local buffer. */
175 regcache_raw_collect (regcache, regno, buf);
177 /* Store the local buffer into the inferior a chunk at the time. */
178 for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
181 memcpy (&val, &buf[i], sizeof (long));
182 ptrace (PTRACE_POKEUSER, tid, regaddr, val);
183 regaddr += sizeof (long);
185 error (_("Couldn't write register %s (#%d): %s."),
186 gdbarch_register_name (gdbarch, regno),
187 regno, safe_strerror (errno));
191 /* Store our register values back into the inferior.
192 If REGNO is negative, do this for all registers.
193 Otherwise, REGNO specifies which register (so we can save time). */
196 old_store_inferior_registers (const struct regcache *regcache, int regno)
200 store_register (regcache, regno);
205 regno < gdbarch_num_regs (get_regcache_arch (regcache));
208 store_register (regcache, regno);
213 /* Given a pointer to a general register set in /proc format
214 (elf_gregset_t *), unpack the register contents and supply
215 them as gdb's idea of the current register values. */
218 supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
220 struct gdbarch *gdbarch = get_regcache_arch (regcache);
221 const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
224 for (regi = M68K_D0_REGNUM;
225 regi <= gdbarch_sp_regnum (gdbarch);
227 regcache_raw_supply (regcache, regi, ®p[regmap[regi]]);
228 regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
230 regcache_raw_supply (regcache,
231 gdbarch_pc_regnum (gdbarch), ®p[PT_PC]);
234 /* Fill register REGNO (if it is a general-purpose register) in
235 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
236 do this for all registers. */
238 fill_gregset (const struct regcache *regcache,
239 elf_gregset_t *gregsetp, int regno)
241 elf_greg_t *regp = (elf_greg_t *) gregsetp;
244 for (i = 0; i < NUM_GREGS; i++)
245 if (regno == -1 || regno == i)
246 regcache_raw_collect (regcache, i, regp + regmap[i]);
249 #ifdef HAVE_PTRACE_GETREGS
251 /* Fetch all general-purpose registers from process/thread TID and
252 store their values in GDB's register array. */
255 fetch_regs (struct regcache *regcache, int tid)
259 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
263 /* The kernel we're running on doesn't support the GETREGS
264 request. Reset `have_ptrace_getregs'. */
265 have_ptrace_getregs = 0;
269 perror_with_name (_("Couldn't get registers"));
272 supply_gregset (regcache, (const elf_gregset_t *) ®s);
275 /* Store all valid general-purpose registers in GDB's register array
276 into the process/thread specified by TID. */
279 store_regs (const struct regcache *regcache, int tid, int regno)
283 if (ptrace (PTRACE_GETREGS, tid, 0, (int) ®s) < 0)
284 perror_with_name (_("Couldn't get registers"));
286 fill_gregset (regcache, ®s, regno);
288 if (ptrace (PTRACE_SETREGS, tid, 0, (int) ®s) < 0)
289 perror_with_name (_("Couldn't write registers"));
294 static void fetch_regs (struct regcache *regcache, int tid)
298 static void store_regs (const struct regcache *regcache, int tid, int regno)
305 /* Transfering floating-point registers between GDB, inferiors and cores. */
307 /* What is the address of fpN within the floating-point register set F? */
308 #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
310 /* Fill GDB's register array with the floating-point register values in
314 supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
316 struct gdbarch *gdbarch = get_regcache_arch (regcache);
319 for (regi = gdbarch_fp0_regnum (gdbarch);
320 regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
321 regcache_raw_supply (regcache, regi,
322 FPREG_ADDR (fpregsetp,
323 regi - gdbarch_fp0_regnum (gdbarch)));
324 regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
325 regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
326 regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
329 /* Fill register REGNO (if it is a floating-point register) in
330 *FPREGSETP with the value in GDB's register array. If REGNO is -1,
331 do this for all registers. */
334 fill_fpregset (const struct regcache *regcache,
335 elf_fpregset_t *fpregsetp, int regno)
337 struct gdbarch *gdbarch = get_regcache_arch (regcache);
340 /* Fill in the floating-point registers. */
341 for (i = gdbarch_fp0_regnum (gdbarch);
342 i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
343 if (regno == -1 || regno == i)
344 regcache_raw_collect (regcache, i,
345 FPREG_ADDR (fpregsetp,
346 i - gdbarch_fp0_regnum (gdbarch)));
348 /* Fill in the floating-point control registers. */
349 for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
350 if (regno == -1 || regno == i)
351 regcache_raw_collect (regcache, i,
352 &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
355 #ifdef HAVE_PTRACE_GETREGS
357 /* Fetch all floating-point registers from process/thread TID and store
358 thier values in GDB's register array. */
361 fetch_fpregs (struct regcache *regcache, int tid)
363 elf_fpregset_t fpregs;
365 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
366 perror_with_name (_("Couldn't get floating point status"));
368 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
371 /* Store all valid floating-point registers in GDB's register array
372 into the process/thread specified by TID. */
375 store_fpregs (const struct regcache *regcache, int tid, int regno)
377 elf_fpregset_t fpregs;
379 if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
380 perror_with_name (_("Couldn't get floating point status"));
382 fill_fpregset (regcache, &fpregs, regno);
384 if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
385 perror_with_name (_("Couldn't write floating point status"));
390 static void fetch_fpregs (struct regcache *regcache, int tid)
394 static void store_fpregs (const struct regcache *regcache, int tid, int regno)
400 /* Transferring arbitrary registers between GDB and inferior. */
402 /* Fetch register REGNO from the child process. If REGNO is -1, do
403 this for all registers (including the floating point and SSE
407 m68k_linux_fetch_inferior_registers (struct target_ops *ops,
408 struct regcache *regcache, int regno)
412 /* Use the old method of peeking around in `struct user' if the
413 GETREGS request isn't available. */
414 if (! have_ptrace_getregs)
416 old_fetch_inferior_registers (regcache, regno);
420 /* GNU/Linux LWP ID's are process ID's. */
421 tid = ptid_get_lwp (inferior_ptid);
423 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
425 /* Use the PTRACE_GETFPXREGS request whenever possible, since it
426 transfers more registers in one system call, and we'll cache the
427 results. But remember that fetch_fpxregs can fail, and return
431 fetch_regs (regcache, tid);
433 /* The call above might reset `have_ptrace_getregs'. */
434 if (! have_ptrace_getregs)
436 old_fetch_inferior_registers (regcache, -1);
440 fetch_fpregs (regcache, tid);
444 if (getregs_supplies (regno))
446 fetch_regs (regcache, tid);
450 if (getfpregs_supplies (regno))
452 fetch_fpregs (regcache, tid);
456 internal_error (__FILE__, __LINE__,
457 _("Got request for bad register number %d."), regno);
460 /* Store register REGNO back into the child process. If REGNO is -1,
461 do this for all registers (including the floating point and SSE
464 m68k_linux_store_inferior_registers (struct target_ops *ops,
465 struct regcache *regcache, int regno)
469 /* Use the old method of poking around in `struct user' if the
470 SETREGS request isn't available. */
471 if (! have_ptrace_getregs)
473 old_store_inferior_registers (regcache, regno);
477 /* GNU/Linux LWP ID's are process ID's. */
478 tid = ptid_get_lwp (inferior_ptid);
480 tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */
482 /* Use the PTRACE_SETFPREGS requests whenever possible, since it
483 transfers more registers in one system call. But remember that
484 store_fpregs can fail, and return zero. */
487 store_regs (regcache, tid, regno);
488 store_fpregs (regcache, tid, regno);
492 if (getregs_supplies (regno))
494 store_regs (regcache, tid, regno);
498 if (getfpregs_supplies (regno))
500 store_fpregs (regcache, tid, regno);
504 internal_error (__FILE__, __LINE__,
505 _("Got request to store bad register number %d."), regno);
508 /* Interpreting register set info found in core files. */
510 /* Provide registers to GDB from a core file.
512 (We can't use the generic version of this function in
513 core-regset.c, because we need to use elf_gregset_t instead of
516 CORE_REG_SECT points to an array of bytes, which are the contents
517 of a `note' from a core file which BFD thinks might contain
518 register contents. CORE_REG_SIZE is its size.
520 WHICH says which register set corelow suspects this is:
521 0 --- the general-purpose register set, in elf_gregset_t format
522 2 --- the floating-point register set, in elf_fpregset_t format
524 REG_ADDR isn't used on GNU/Linux. */
527 fetch_core_registers (struct regcache *regcache,
528 char *core_reg_sect, unsigned core_reg_size,
529 int which, CORE_ADDR reg_addr)
531 elf_gregset_t gregset;
532 elf_fpregset_t fpregset;
537 if (core_reg_size != sizeof (gregset))
538 warning (_("Wrong size gregset in core file."));
541 memcpy (&gregset, core_reg_sect, sizeof (gregset));
542 supply_gregset (regcache, (const elf_gregset_t *) &gregset);
547 if (core_reg_size != sizeof (fpregset))
548 warning (_("Wrong size fpregset in core file."));
551 memcpy (&fpregset, core_reg_sect, sizeof (fpregset));
552 supply_fpregset (regcache, (const elf_fpregset_t *) &fpregset);
557 /* We've covered all the kinds of registers we know about here,
558 so this must be something we wouldn't know what to do with
559 anyway. Just ignore it. */
565 /* Fetch the thread-local storage pointer for libthread_db. */
568 ps_get_thread_area (const struct ps_prochandle *ph,
569 lwpid_t lwpid, int idx, void **base)
571 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
574 /* IDX is the bias from the thread pointer to the beginning of the
575 thread descriptor. It has to be subtracted due to implementation
576 quirks in libthread_db. */
577 *base = (char *) *base - idx;
583 /* Register that we are able to handle GNU/Linux ELF core file
586 static struct core_fns linux_elf_core_fns =
588 bfd_target_elf_flavour, /* core_flavour */
589 default_check_format, /* check_format */
590 default_core_sniffer, /* core_sniffer */
591 fetch_core_registers, /* core_read_registers */
595 void _initialize_m68k_linux_nat (void);
598 _initialize_m68k_linux_nat (void)
600 struct target_ops *t;
602 /* Fill in the generic GNU/Linux methods. */
605 /* Add our register access methods. */
606 t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
607 t->to_store_registers = m68k_linux_store_inferior_registers;
609 /* Register the target. */
610 linux_nat_add_target (t);
612 deprecated_add_core_fns (&linux_elf_core_fns);