gdb/xtensa-linux-nat.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Xtensa GNU/Linux native support.

  2.    Copyright (C) 2007-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 "gdbcore.h"
  18. #include "regcache.h"
  19. #include "target.h"
  20. #include "linux-nat.h"

  21. #include <stdint.h>
  22. #include <sys/types.h>
  23. #include <signal.h>
  24. #include <sys/user.h>
  25. #include <sys/ioctl.h>
  26. #include "gdb_wait.h"
  27. #include <fcntl.h>
  28. #include <sys/procfs.h>
  29. #include <sys/ptrace.h>
  30. #include <asm/ptrace.h>

  31. #include "gregset.h"
  32. #include "xtensa-tdep.h"

  33. /* Extended register set depends on hardware configs.
  34.    Keeping these definitions separately allows to introduce
  35.    hardware-specific overlays.  */
  36. #include "xtensa-xtregs.c"

  37. static int
  38. get_thread_id (ptid_t ptid)
  39. {
  40.   int tid = ptid_get_lwp (ptid);
  41.   if (0 == tid)
  42.     tid = ptid_get_pid (ptid);
  43.   return tid;
  44. }
  45. #define GET_THREAD_ID(PTID)        get_thread_id (PTID)

  46. void
  47. fill_gregset (const struct regcache *regcache,
  48.               gdb_gregset_t *gregsetp, int regnum)
  49. {
  50.   int i;
  51.   xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;
  52.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  53.   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
  54.     regcache_raw_collect (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
  55.   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
  56.     regcache_raw_collect (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);

  57.   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
  58.     regcache_raw_collect (regcache,
  59.                           gdbarch_tdep (gdbarch)->wb_regnum,
  60.                           &regs->windowbase);
  61.   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
  62.     regcache_raw_collect (regcache,
  63.                           gdbarch_tdep (gdbarch)->ws_regnum,
  64.                           &regs->windowstart);
  65.   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
  66.     regcache_raw_collect (regcache,
  67.                           gdbarch_tdep (gdbarch)->lbeg_regnum,
  68.                           &regs->lbeg);
  69.   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
  70.     regcache_raw_collect (regcache,
  71.                           gdbarch_tdep (gdbarch)->lend_regnum,
  72.                           &regs->lend);
  73.   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
  74.     regcache_raw_collect (regcache,
  75.                           gdbarch_tdep (gdbarch)->lcount_regnum,
  76.                           &regs->lcount);
  77.   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
  78.     regcache_raw_collect (regcache,
  79.                           gdbarch_tdep (gdbarch)->sar_regnum,
  80.                           &regs->sar);
  81.   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
  82.       && regnum < gdbarch_tdep (gdbarch)->ar_base
  83.                     + gdbarch_tdep (gdbarch)->num_aregs)
  84.     regcache_raw_collect (regcache,regnum,
  85.                           &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
  86.   else if (regnum == -1)
  87.     {
  88.       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
  89.         regcache_raw_collect (regcache,
  90.                               gdbarch_tdep (gdbarch)->ar_base + i,
  91.                               &regs->ar[i]);
  92.     }
  93. }

  94. void
  95. supply_gregset_reg (struct regcache *regcache,
  96.                     const gdb_gregset_t *gregsetp, int regnum)
  97. {
  98.   int i;
  99.   xtensa_elf_gregset_t *regs = (xtensa_elf_gregset_t *) gregsetp;

  100.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  101.   if (regnum == gdbarch_pc_regnum (gdbarch) || regnum == -1)
  102.     regcache_raw_supply (regcache, gdbarch_pc_regnum (gdbarch), &regs->pc);
  103.   if (regnum == gdbarch_ps_regnum (gdbarch) || regnum == -1)
  104.     regcache_raw_supply (regcache, gdbarch_ps_regnum (gdbarch), &regs->ps);

  105.   if (regnum == gdbarch_tdep (gdbarch)->wb_regnum || regnum == -1)
  106.     regcache_raw_supply (regcache,
  107.                           gdbarch_tdep (gdbarch)->wb_regnum,
  108.                           &regs->windowbase);
  109.   if (regnum == gdbarch_tdep (gdbarch)->ws_regnum || regnum == -1)
  110.     regcache_raw_supply (regcache,
  111.                           gdbarch_tdep (gdbarch)->ws_regnum,
  112.                           &regs->windowstart);
  113.   if (regnum == gdbarch_tdep (gdbarch)->lbeg_regnum || regnum == -1)
  114.     regcache_raw_supply (regcache,
  115.                           gdbarch_tdep (gdbarch)->lbeg_regnum,
  116.                           &regs->lbeg);
  117.   if (regnum == gdbarch_tdep (gdbarch)->lend_regnum || regnum == -1)
  118.     regcache_raw_supply (regcache,
  119.                           gdbarch_tdep (gdbarch)->lend_regnum,
  120.                           &regs->lend);
  121.   if (regnum == gdbarch_tdep (gdbarch)->lcount_regnum || regnum == -1)
  122.     regcache_raw_supply (regcache,
  123.                           gdbarch_tdep (gdbarch)->lcount_regnum,
  124.                           &regs->lcount);
  125.   if (regnum == gdbarch_tdep (gdbarch)->sar_regnum || regnum == -1)
  126.     regcache_raw_supply (regcache,
  127.                           gdbarch_tdep (gdbarch)->sar_regnum,
  128.                           &regs->sar);
  129.   if (regnum >=gdbarch_tdep (gdbarch)->ar_base
  130.       && regnum < gdbarch_tdep (gdbarch)->ar_base
  131.                     + gdbarch_tdep (gdbarch)->num_aregs)
  132.     regcache_raw_supply (regcache,regnum,
  133.                           &regs->ar[regnum - gdbarch_tdep (gdbarch)->ar_base]);
  134.   else if (regnum == -1)
  135.     {
  136.       for (i = 0; i < gdbarch_tdep (gdbarch)->num_aregs; ++i)
  137.         regcache_raw_supply (regcache,
  138.                               gdbarch_tdep (gdbarch)->ar_base + i,
  139.                               &regs->ar[i]);
  140.     }
  141. }

  142. void
  143. supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregsetp)
  144. {
  145.   supply_gregset_reg (regcache, gregsetp, -1);
  146. }

  147. void
  148. fill_fpregset (const struct regcache *regcache,
  149.                gdb_fpregset_t *fpregsetp, int regnum)
  150. {
  151.   return;
  152. }

  153. void
  154. supply_fpregset (struct regcache *regcache,
  155.                  const gdb_fpregset_t *fpregsetp)
  156. {
  157.   return;
  158. }

  159. /* Fetch greg-register(s) from process/thread TID
  160.    and store value(s) in GDB's register array.  */

  161. static void
  162. fetch_gregs (struct regcache *regcache, int regnum)
  163. {
  164.   int tid = GET_THREAD_ID (inferior_ptid);
  165.   const gdb_gregset_t regs;
  166.   int areg;

  167.   if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
  168.     {
  169.       perror_with_name (_("Couldn't get registers"));
  170.       return;
  171.     }

  172.   supply_gregset_reg (regcache, &regs, regnum);
  173. }

  174. /* Store greg-register(s) in GDB's register
  175.    array into the process/thread specified by TID.  */

  176. static void
  177. store_gregs (struct regcache *regcache, int regnum)
  178. {
  179.   int tid = GET_THREAD_ID (inferior_ptid);
  180.   gdb_gregset_t regs;
  181.   int areg;

  182.   if (ptrace (PTRACE_GETREGS, tid, 0, (long) &regs) < 0)
  183.     {
  184.       perror_with_name (_("Couldn't get registers"));
  185.       return;
  186.     }

  187.   fill_gregset (regcache, &regs, regnum);

  188.   if (ptrace (PTRACE_SETREGS, tid, 0, (long) &regs) < 0)
  189.     {
  190.       perror_with_name (_("Couldn't write registers"));
  191.       return;
  192.     }
  193. }

  194. static int xtreg_lo;
  195. static int xtreg_high;

  196. /* Fetch/Store Xtensa TIE registers.  Xtensa GNU/Linux PTRACE
  197.    interface provides special requests for this.  */

  198. static void
  199. fetch_xtregs (struct regcache *regcache, int regnum)
  200. {
  201.   int tid = GET_THREAD_ID (inferior_ptid);
  202.   const xtensa_regtable_t *ptr;
  203.   char xtregs [XTENSA_ELF_XTREG_SIZE];

  204.   if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
  205.     perror_with_name (_("Couldn't get extended registers"));

  206.   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
  207.     if (regnum == ptr->gdb_regnum || regnum == -1)
  208.       regcache_raw_supply (regcache, ptr->gdb_regnum,
  209.                            xtregs + ptr->ptrace_offset);
  210. }

  211. static void
  212. store_xtregs (struct regcache *regcache, int regnum)
  213. {
  214.   int tid = GET_THREAD_ID (inferior_ptid);
  215.   const xtensa_regtable_t *ptr;
  216.   char xtregs [XTENSA_ELF_XTREG_SIZE];

  217.   if (ptrace (PTRACE_GETXTREGS, tid, 0, (long)&xtregs) < 0)
  218.     perror_with_name (_("Couldn't get extended registers"));

  219.   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
  220.     if (regnum == ptr->gdb_regnum || regnum == -1)
  221.       regcache_raw_collect (regcache, ptr->gdb_regnum,
  222.                             xtregs + ptr->ptrace_offset);

  223.   if (ptrace (PTRACE_SETXTREGS, tid, 0, (long)&xtregs) < 0)
  224.     perror_with_name (_("Couldn't write extended registers"));
  225. }

  226. void
  227. xtensa_linux_fetch_inferior_registers (struct target_ops *ops,
  228.                                        struct regcache *regcache, int regnum)
  229. {
  230.   if (regnum == -1)
  231.     {
  232.       fetch_gregs (regcache, regnum);
  233.       fetch_xtregs (regcache, regnum);
  234.     }
  235.   else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
  236.     fetch_gregs (regcache, regnum);
  237.   else
  238.     fetch_xtregs (regcache, regnum);
  239. }

  240. void
  241. xtensa_linux_store_inferior_registers (struct target_ops *ops,
  242.                                        struct regcache *regcache, int regnum)
  243. {
  244.   if (regnum == -1)
  245.     {
  246.       store_gregs (regcache, regnum);
  247.       store_xtregs (regcache, regnum);
  248.     }
  249.   else if ((regnum < xtreg_lo) || (regnum > xtreg_high))
  250.     store_gregs (regcache, regnum);
  251.   else
  252.     store_xtregs (regcache, regnum);
  253. }

  254. void _initialize_xtensa_linux_nat (void);

  255. void
  256. _initialize_xtensa_linux_nat (void)
  257. {
  258.   struct target_ops *t;
  259.   const xtensa_regtable_t *ptr;

  260.   /* Calculate the number range for extended registers.  */
  261.   xtreg_lo = 1000000000;
  262.   xtreg_high = -1;
  263.   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
  264.     {
  265.       if (ptr->gdb_regnum < xtreg_lo)
  266.         xtreg_lo = ptr->gdb_regnum;
  267.       if (ptr->gdb_regnum > xtreg_high)
  268.         xtreg_high = ptr->gdb_regnum;
  269.     }

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

  272.   /* Add our register access methods.  */
  273.   t->to_fetch_registers = xtensa_linux_fetch_inferior_registers;
  274.   t->to_store_registers = xtensa_linux_store_inferior_registers;

  275.   linux_nat_add_target (t);
  276. }