gdb/m68klinux-nat.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Motorola m68k native support for GNU/Linux.

  2.    Copyright (C) 1996-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "inferior.h"
  17. #include "language.h"
  18. #include "gdbcore.h"
  19. #include "regcache.h"
  20. #include "target.h"
  21. #include "linux-nat.h"

  22. #include "m68k-tdep.h"

  23. #include <sys/dir.h>
  24. #include <signal.h>
  25. #include <sys/ptrace.h>
  26. #include <sys/user.h>
  27. #include <sys/ioctl.h>
  28. #include <fcntl.h>
  29. #include <sys/procfs.h>

  30. #ifdef HAVE_SYS_REG_H
  31. #include <sys/reg.h>
  32. #endif

  33. #include <sys/file.h>
  34. #include <sys/stat.h>

  35. #include "floatformat.h"

  36. /* Prototypes for supply_gregset etc.  */
  37. #include "gregset.h"

  38. /* Defines ps_err_e, struct ps_prochandle.  */
  39. #include "gdb_proc_service.h"

  40. #ifndef PTRACE_GET_THREAD_AREA
  41. #define PTRACE_GET_THREAD_AREA 25
  42. #endif

  43. /* This table must line up with gdbarch_register_name in "m68k-tdep.c".  */
  44. static const int regmap[] =
  45. {
  46.   PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
  47.   PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
  48.   PT_SR, PT_PC,
  49.   /* PT_FP0, ..., PT_FP7 */
  50.   21, 24, 27, 30, 33, 36, 39, 42,
  51.   /* PT_FPCR, PT_FPSR, PT_FPIAR */
  52.   45, 46, 47
  53. };

  54. /* Which ptrace request retrieves which registers?
  55.    These apply to the corresponding SET requests as well.  */
  56. #define NUM_GREGS (18)
  57. #define MAX_NUM_REGS (NUM_GREGS + 11)

  58. static int
  59. getregs_supplies (int regno)
  60. {
  61.   return 0 <= regno && regno < NUM_GREGS;
  62. }

  63. static int
  64. getfpregs_supplies (int regno)
  65. {
  66.   return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
  67. }

  68. /* Does the current host support the GETREGS request?  */
  69. static int have_ptrace_getregs =
  70. #ifdef HAVE_PTRACE_GETREGS
  71.   1
  72. #else
  73.   0
  74. #endif
  75. ;



  76. /* Fetching registers directly from the U area, one at a time.  */

  77. /* Fetch one register.  */

  78. static void
  79. fetch_register (struct regcache *regcache, int regno)
  80. {
  81.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  82.   long regaddr, val;
  83.   int i;
  84.   gdb_byte buf[MAX_REGISTER_SIZE];
  85.   int tid;

  86.   /* Overload thread id onto process id.  */
  87.   tid = ptid_get_lwp (inferior_ptid);
  88.   if (tid == 0)
  89.     tid = ptid_get_pid (inferior_ptid);        /* no thread id, just use
  90.                                            process id.  */

  91.   regaddr = 4 * regmap[regno];
  92.   for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
  93.     {
  94.       errno = 0;
  95.       val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
  96.       memcpy (&buf[i], &val, sizeof (long));
  97.       regaddr += sizeof (long);
  98.       if (errno != 0)
  99.         error (_("Couldn't read register %s (#%d): %s."),
  100.                gdbarch_register_name (gdbarch, regno),
  101.                regno, safe_strerror (errno));
  102.     }
  103.   regcache_raw_supply (regcache, regno, buf);
  104. }

  105. /* Fetch register values from the inferior.
  106.    If REGNO is negative, do this for all registers.
  107.    Otherwise, REGNO specifies which register (so we can save time).  */

  108. static void
  109. old_fetch_inferior_registers (struct regcache *regcache, int regno)
  110. {
  111.   if (regno >= 0)
  112.     {
  113.       fetch_register (regcache, regno);
  114.     }
  115.   else
  116.     {
  117.       for (regno = 0;
  118.            regno < gdbarch_num_regs (get_regcache_arch (regcache));
  119.            regno++)
  120.         {
  121.           fetch_register (regcache, regno);
  122.         }
  123.     }
  124. }

  125. /* Store one register.  */

  126. static void
  127. store_register (const struct regcache *regcache, int regno)
  128. {
  129.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  130.   long regaddr, val;
  131.   int i;
  132.   int tid;
  133.   gdb_byte buf[MAX_REGISTER_SIZE];

  134.   /* Overload thread id onto process id.  */
  135.   tid = ptid_get_lwp (inferior_ptid);
  136.   if (tid == 0)
  137.     tid = ptid_get_pid (inferior_ptid);        /* no thread id, just use
  138.                                            process id.  */

  139.   regaddr = 4 * regmap[regno];

  140.   /* Put the contents of regno into a local buffer.  */
  141.   regcache_raw_collect (regcache, regno, buf);

  142.   /* Store the local buffer into the inferior a chunk at the time.  */
  143.   for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
  144.     {
  145.       errno = 0;
  146.       memcpy (&val, &buf[i], sizeof (long));
  147.       ptrace (PTRACE_POKEUSER, tid, regaddr, val);
  148.       regaddr += sizeof (long);
  149.       if (errno != 0)
  150.         error (_("Couldn't write register %s (#%d): %s."),
  151.                gdbarch_register_name (gdbarch, regno),
  152.                regno, safe_strerror (errno));
  153.     }
  154. }

  155. /* Store our register values back into the inferior.
  156.    If REGNO is negative, do this for all registers.
  157.    Otherwise, REGNO specifies which register (so we can save time).  */

  158. static void
  159. old_store_inferior_registers (const struct regcache *regcache, int regno)
  160. {
  161.   if (regno >= 0)
  162.     {
  163.       store_register (regcache, regno);
  164.     }
  165.   else
  166.     {
  167.       for (regno = 0;
  168.            regno < gdbarch_num_regs (get_regcache_arch (regcache));
  169.            regno++)
  170.         {
  171.           store_register (regcache, regno);
  172.         }
  173.     }
  174. }

  175. /*  Given a pointer to a general register set in /proc format
  176.    (elf_gregset_t *), unpack the register contents and supply
  177.    them as gdb's idea of the current register values.  */

  178. void
  179. supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
  180. {
  181.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  182.   const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
  183.   int regi;

  184.   for (regi = M68K_D0_REGNUM;
  185.        regi <= gdbarch_sp_regnum (gdbarch);
  186.        regi++)
  187.     regcache_raw_supply (regcache, regi, &regp[regmap[regi]]);
  188.   regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch),
  189.                        &regp[PT_SR]);
  190.   regcache_raw_supply (regcache,
  191.                        gdbarch_pc_regnum (gdbarch), &regp[PT_PC]);
  192. }

  193. /* Fill register REGNO (if it is a general-purpose register) in
  194.    *GREGSETPS with the value in GDB's register array.  If REGNO is -1,
  195.    do this for all registers.  */
  196. void
  197. fill_gregset (const struct regcache *regcache,
  198.               elf_gregset_t *gregsetp, int regno)
  199. {
  200.   elf_greg_t *regp = (elf_greg_t *) gregsetp;
  201.   int i;

  202.   for (i = 0; i < NUM_GREGS; i++)
  203.     if (regno == -1 || regno == i)
  204.       regcache_raw_collect (regcache, i, regp + regmap[i]);
  205. }

  206. #ifdef HAVE_PTRACE_GETREGS

  207. /* Fetch all general-purpose registers from process/thread TID and
  208.    store their values in GDB's register array.  */

  209. static void
  210. fetch_regs (struct regcache *regcache, int tid)
  211. {
  212.   elf_gregset_t regs;

  213.   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
  214.     {
  215.       if (errno == EIO)
  216.         {
  217.           /* The kernel we're running on doesn't support the GETREGS
  218.              request.  Reset `have_ptrace_getregs'.  */
  219.           have_ptrace_getregs = 0;
  220.           return;
  221.         }

  222.       perror_with_name (_("Couldn't get registers"));
  223.     }

  224.   supply_gregset (regcache, (const elf_gregset_t *) &regs);
  225. }

  226. /* Store all valid general-purpose registers in GDB's register array
  227.    into the process/thread specified by TID.  */

  228. static void
  229. store_regs (const struct regcache *regcache, int tid, int regno)
  230. {
  231.   elf_gregset_t regs;

  232.   if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
  233.     perror_with_name (_("Couldn't get registers"));

  234.   fill_gregset (regcache, &regs, regno);

  235.   if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
  236.     perror_with_name (_("Couldn't write registers"));
  237. }

  238. #else

  239. static void fetch_regs (struct regcache *regcache, int tid)
  240. {
  241. }

  242. static void store_regs (const struct regcache *regcache, int tid, int regno)
  243. {
  244. }

  245. #endif


  246. /* Transfering floating-point registers between GDB, inferiors and cores.  */

  247. /* What is the address of fpN within the floating-point register set F?  */
  248. #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])

  249. /* Fill GDB's register array with the floating-point register values in
  250.    *FPREGSETP.  */

  251. void
  252. supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
  253. {
  254.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  255.   int regi;

  256.   for (regi = gdbarch_fp0_regnum (gdbarch);
  257.        regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
  258.     regcache_raw_supply (regcache, regi,
  259.                          FPREG_ADDR (fpregsetp,
  260.                                      regi - gdbarch_fp0_regnum (gdbarch)));
  261.   regcache_raw_supply (regcache, M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
  262.   regcache_raw_supply (regcache, M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
  263.   regcache_raw_supply (regcache, M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
  264. }

  265. /* Fill register REGNO (if it is a floating-point register) in
  266.    *FPREGSETP with the value in GDB's register array.  If REGNO is -1,
  267.    do this for all registers.  */

  268. void
  269. fill_fpregset (const struct regcache *regcache,
  270.                elf_fpregset_t *fpregsetp, int regno)
  271. {
  272.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  273.   int i;

  274.   /* Fill in the floating-point registers.  */
  275.   for (i = gdbarch_fp0_regnum (gdbarch);
  276.        i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
  277.     if (regno == -1 || regno == i)
  278.       regcache_raw_collect (regcache, i,
  279.                             FPREG_ADDR (fpregsetp,
  280.                                         i - gdbarch_fp0_regnum (gdbarch)));

  281.   /* Fill in the floating-point control registers.  */
  282.   for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
  283.     if (regno == -1 || regno == i)
  284.       regcache_raw_collect (regcache, i,
  285.                             &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
  286. }

  287. #ifdef HAVE_PTRACE_GETREGS

  288. /* Fetch all floating-point registers from process/thread TID and store
  289.    thier values in GDB's register array.  */

  290. static void
  291. fetch_fpregs (struct regcache *regcache, int tid)
  292. {
  293.   elf_fpregset_t fpregs;

  294.   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
  295.     perror_with_name (_("Couldn't get floating point status"));

  296.   supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
  297. }

  298. /* Store all valid floating-point registers in GDB's register array
  299.    into the process/thread specified by TID.  */

  300. static void
  301. store_fpregs (const struct regcache *regcache, int tid, int regno)
  302. {
  303.   elf_fpregset_t fpregs;

  304.   if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
  305.     perror_with_name (_("Couldn't get floating point status"));

  306.   fill_fpregset (regcache, &fpregs, regno);

  307.   if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
  308.     perror_with_name (_("Couldn't write floating point status"));
  309. }

  310. #else

  311. static void fetch_fpregs (struct regcache *regcache, int tid)
  312. {
  313. }

  314. static void store_fpregs (const struct regcache *regcache, int tid, int regno)
  315. {
  316. }

  317. #endif

  318. /* Transferring arbitrary registers between GDB and inferior.  */

  319. /* Fetch register REGNO from the child process.  If REGNO is -1, do
  320.    this for all registers (including the floating point and SSE
  321.    registers).  */

  322. static void
  323. m68k_linux_fetch_inferior_registers (struct target_ops *ops,
  324.                                      struct regcache *regcache, int regno)
  325. {
  326.   int tid;

  327.   /* Use the old method of peeking around in `struct user' if the
  328.      GETREGS request isn't available.  */
  329.   if (! have_ptrace_getregs)
  330.     {
  331.       old_fetch_inferior_registers (regcache, regno);
  332.       return;
  333.     }

  334.   /* GNU/Linux LWP ID's are process ID's.  */
  335.   tid = ptid_get_lwp (inferior_ptid);
  336.   if (tid == 0)
  337.     tid = ptid_get_pid (inferior_ptid);        /* Not a threaded program.  */

  338.   /* Use the PTRACE_GETFPXREGS request whenever possible, since it
  339.      transfers more registers in one system call, and we'll cache the
  340.      results.  But remember that fetch_fpxregs can fail, and return
  341.      zero.  */
  342.   if (regno == -1)
  343.     {
  344.       fetch_regs (regcache, tid);

  345.       /* The call above might reset `have_ptrace_getregs'.  */
  346.       if (! have_ptrace_getregs)
  347.         {
  348.           old_fetch_inferior_registers (regcache, -1);
  349.           return;
  350.         }

  351.       fetch_fpregs (regcache, tid);
  352.       return;
  353.     }

  354.   if (getregs_supplies (regno))
  355.     {
  356.       fetch_regs (regcache, tid);
  357.       return;
  358.     }

  359.   if (getfpregs_supplies (regno))
  360.     {
  361.       fetch_fpregs (regcache, tid);
  362.       return;
  363.     }

  364.   internal_error (__FILE__, __LINE__,
  365.                   _("Got request for bad register number %d."), regno);
  366. }

  367. /* Store register REGNO back into the child process.  If REGNO is -1,
  368.    do this for all registers (including the floating point and SSE
  369.    registers).  */
  370. static void
  371. m68k_linux_store_inferior_registers (struct target_ops *ops,
  372.                                      struct regcache *regcache, int regno)
  373. {
  374.   int tid;

  375.   /* Use the old method of poking around in `struct user' if the
  376.      SETREGS request isn't available.  */
  377.   if (! have_ptrace_getregs)
  378.     {
  379.       old_store_inferior_registers (regcache, regno);
  380.       return;
  381.     }

  382.   /* GNU/Linux LWP ID's are process ID's.  */
  383.   tid = ptid_get_lwp (inferior_ptid);
  384.   if (tid == 0)
  385.     tid = ptid_get_pid (inferior_ptid);        /* Not a threaded program.  */

  386.   /* Use the PTRACE_SETFPREGS requests whenever possible, since it
  387.      transfers more registers in one system call.  But remember that
  388.      store_fpregs can fail, and return zero.  */
  389.   if (regno == -1)
  390.     {
  391.       store_regs (regcache, tid, regno);
  392.       store_fpregs (regcache, tid, regno);
  393.       return;
  394.     }

  395.   if (getregs_supplies (regno))
  396.     {
  397.       store_regs (regcache, tid, regno);
  398.       return;
  399.     }

  400.   if (getfpregs_supplies (regno))
  401.     {
  402.       store_fpregs (regcache, tid, regno);
  403.       return;
  404.     }

  405.   internal_error (__FILE__, __LINE__,
  406.                   _("Got request to store bad register number %d."), regno);
  407. }


  408. /* Fetch the thread-local storage pointer for libthread_db.  */

  409. ps_err_e
  410. ps_get_thread_area (const struct ps_prochandle *ph,
  411.                     lwpid_t lwpid, int idx, void **base)
  412. {
  413.   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
  414.     return PS_ERR;

  415.   /* IDX is the bias from the thread pointer to the beginning of the
  416.      thread descriptor.  It has to be subtracted due to implementation
  417.      quirks in libthread_db.  */
  418.   *base = (char *) *base - idx;

  419.   return PS_OK;
  420. }


  421. void _initialize_m68k_linux_nat (void);

  422. void
  423. _initialize_m68k_linux_nat (void)
  424. {
  425.   struct target_ops *t;

  426.   /* Fill in the generic GNU/Linux methods.  */
  427.   t = linux_target ();

  428.   /* Add our register access methods.  */
  429.   t->to_fetch_registers = m68k_linux_fetch_inferior_registers;
  430.   t->to_store_registers = m68k_linux_store_inferior_registers;

  431.   /* Register the target.  */
  432.   linux_nat_add_target (t);
  433. }