gdb/spu-multiarch.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Cell SPU GNU/Linux multi-architecture debugging support.
  2.    Copyright (C) 2009-2015 Free Software Foundation, Inc.

  3.    Contributed by Ulrich Weigand <uweigand@de.ibm.com>.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "gdbcore.h"
  17. #include "gdbcmd.h"
  18. #include "arch-utils.h"
  19. #include "observer.h"
  20. #include "inferior.h"
  21. #include "regcache.h"
  22. #include "symfile.h"
  23. #include "objfiles.h"
  24. #include "solib.h"
  25. #include "solist.h"

  26. #include "ppc-tdep.h"
  27. #include "ppc-linux-tdep.h"
  28. #include "spu-tdep.h"

  29. /* This module's target vector.  */
  30. static struct target_ops spu_ops;

  31. /* Number of SPE objects loaded into the current inferior.  */
  32. static int spu_nr_solib;

  33. /* Stand-alone SPE executable?  */
  34. #define spu_standalone_p() \
  35.   (symfile_objfile && symfile_objfile->obfd \
  36.    && bfd_get_arch (symfile_objfile->obfd) == bfd_arch_spu)

  37. /* PPU side system calls.  */
  38. #define INSTR_SC        0x44000002
  39. #define NR_spu_run        0x0116

  40. /* If the PPU thread is currently stopped on a spu_run system call,
  41.    return to FD and ADDR the file handle and NPC parameter address
  42.    used with the system call.  Return non-zero if successful.  */
  43. static int
  44. parse_spufs_run (ptid_t ptid, int *fd, CORE_ADDR *addr)
  45. {
  46.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  47.   struct gdbarch_tdep *tdep;
  48.   struct regcache *regcache;
  49.   gdb_byte buf[4];
  50.   ULONGEST regval;

  51.   /* If we're not on PPU, there's nothing to detect.  */
  52.   if (gdbarch_bfd_arch_info (target_gdbarch ())->arch != bfd_arch_powerpc)
  53.     return 0;

  54.   /* Get PPU-side registers.  */
  55.   regcache = get_thread_arch_regcache (ptid, target_gdbarch ());
  56.   tdep = gdbarch_tdep (target_gdbarch ());

  57.   /* Fetch instruction preceding current NIP.  */
  58.   if (target_read_memory (regcache_read_pc (regcache) - 4, buf, 4) != 0)
  59.     return 0;
  60.   /* It should be a "sc" instruction.  */
  61.   if (extract_unsigned_integer (buf, 4, byte_order) != INSTR_SC)
  62.     return 0;
  63.   /* System call number should be NR_spu_run.  */
  64.   regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum, &regval);
  65.   if (regval != NR_spu_run)
  66.     return 0;

  67.   /* Register 3 contains fd, register 4 the NPC param pointer.  */
  68.   regcache_cooked_read_unsigned (regcache, PPC_ORIG_R3_REGNUM, &regval);
  69.   *fd = (int) regval;
  70.   regcache_cooked_read_unsigned (regcache, tdep->ppc_gp0_regnum + 4, &regval);
  71.   *addr = (CORE_ADDR) regval;
  72.   return 1;
  73. }

  74. /* Find gdbarch for SPU context SPUFS_FD.  */
  75. static struct gdbarch *
  76. spu_gdbarch (int spufs_fd)
  77. {
  78.   struct gdbarch_info info;
  79.   gdbarch_info_init (&info);
  80.   info.bfd_arch_info = bfd_lookup_arch (bfd_arch_spu, bfd_mach_spu);
  81.   info.byte_order = BFD_ENDIAN_BIG;
  82.   info.osabi = GDB_OSABI_LINUX;
  83.   info.tdep_info = (void *) &spufs_fd;
  84.   return gdbarch_find_by_info (info);
  85. }

  86. /* Override the to_thread_architecture routine.  */
  87. static struct gdbarch *
  88. spu_thread_architecture (struct target_ops *ops, ptid_t ptid)
  89. {
  90.   int spufs_fd;
  91.   CORE_ADDR spufs_addr;

  92.   if (parse_spufs_run (ptid, &spufs_fd, &spufs_addr))
  93.     return spu_gdbarch (spufs_fd);

  94.   return target_gdbarch ();
  95. }

  96. /* Override the to_region_ok_for_hw_watchpoint routine.  */
  97. static int
  98. spu_region_ok_for_hw_watchpoint (struct target_ops *self,
  99.                                  CORE_ADDR addr, int len)
  100. {
  101.   struct target_ops *ops_beneath = find_target_beneath (self);

  102.   /* We cannot watch SPU local store.  */
  103.   if (SPUADDR_SPU (addr) != -1)
  104.     return 0;

  105.   return ops_beneath->to_region_ok_for_hw_watchpoint (ops_beneath, addr, len);
  106. }

  107. /* Override the to_fetch_registers routine.  */
  108. static void
  109. spu_fetch_registers (struct target_ops *ops,
  110.                      struct regcache *regcache, int regno)
  111. {
  112.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  113.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  114.   struct target_ops *ops_beneath = find_target_beneath (ops);
  115.   int spufs_fd;
  116.   CORE_ADDR spufs_addr;

  117.   /* This version applies only if we're currently in spu_run.  */
  118.   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
  119.     {
  120.       ops_beneath->to_fetch_registers (ops_beneath, regcache, regno);
  121.       return;
  122.     }

  123.   /* We must be stopped on a spu_run system call.  */
  124.   if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
  125.     return;

  126.   /* The ID register holds the spufs file handle.  */
  127.   if (regno == -1 || regno == SPU_ID_REGNUM)
  128.     {
  129.       gdb_byte buf[4];
  130.       store_unsigned_integer (buf, 4, byte_order, spufs_fd);
  131.       regcache_raw_supply (regcache, SPU_ID_REGNUM, buf);
  132.     }

  133.   /* The NPC register is found in PPC memory at SPUFS_ADDR.  */
  134.   if (regno == -1 || regno == SPU_PC_REGNUM)
  135.     {
  136.       gdb_byte buf[4];

  137.       if (target_read (ops_beneath, TARGET_OBJECT_MEMORY, NULL,
  138.                        buf, spufs_addr, sizeof buf) == sizeof buf)
  139.         regcache_raw_supply (regcache, SPU_PC_REGNUM, buf);
  140.     }

  141.   /* The GPRs are found in the "regs" spufs file.  */
  142.   if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
  143.     {
  144.       gdb_byte buf[16 * SPU_NUM_GPRS];
  145.       char annex[32];
  146.       int i;

  147.       xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
  148.       if (target_read (ops_beneath, TARGET_OBJECT_SPU, annex,
  149.                        buf, 0, sizeof buf) == sizeof buf)
  150.         for (i = 0; i < SPU_NUM_GPRS; i++)
  151.           regcache_raw_supply (regcache, i, buf + i*16);
  152.     }
  153. }

  154. /* Override the to_store_registers routine.  */
  155. static void
  156. spu_store_registers (struct target_ops *ops,
  157.                      struct regcache *regcache, int regno)
  158. {
  159.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  160.   struct target_ops *ops_beneath = find_target_beneath (ops);
  161.   int spufs_fd;
  162.   CORE_ADDR spufs_addr;

  163.   /* This version applies only if we're currently in spu_run.  */
  164.   if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
  165.     {
  166.       ops_beneath->to_store_registers (ops_beneath, regcache, regno);
  167.       return;
  168.     }

  169.   /* We must be stopped on a spu_run system call.  */
  170.   if (!parse_spufs_run (inferior_ptid, &spufs_fd, &spufs_addr))
  171.     return;

  172.   /* The NPC register is found in PPC memory at SPUFS_ADDR.  */
  173.   if (regno == -1 || regno == SPU_PC_REGNUM)
  174.     {
  175.       gdb_byte buf[4];
  176.       regcache_raw_collect (regcache, SPU_PC_REGNUM, buf);

  177.       target_write (ops_beneath, TARGET_OBJECT_MEMORY, NULL,
  178.                     buf, spufs_addr, sizeof buf);
  179.     }

  180.   /* The GPRs are found in the "regs" spufs file.  */
  181.   if (regno == -1 || (regno >= 0 && regno < SPU_NUM_GPRS))
  182.     {
  183.       gdb_byte buf[16 * SPU_NUM_GPRS];
  184.       char annex[32];
  185.       int i;

  186.       for (i = 0; i < SPU_NUM_GPRS; i++)
  187.         regcache_raw_collect (regcache, i, buf + i*16);

  188.       xsnprintf (annex, sizeof annex, "%d/regs", spufs_fd);
  189.       target_write (ops_beneath, TARGET_OBJECT_SPU, annex,
  190.                     buf, 0, sizeof buf);
  191.     }
  192. }

  193. /* Override the to_xfer_partial routine.  */
  194. static enum target_xfer_status
  195. spu_xfer_partial (struct target_ops *ops, enum target_object object,
  196.                   const char *annex, gdb_byte *readbuf,
  197.                   const gdb_byte *writebuf, ULONGEST offset, ULONGEST len,
  198.                   ULONGEST *xfered_len)
  199. {
  200.   struct target_ops *ops_beneath = find_target_beneath (ops);

  201.   /* Use the "mem" spufs file to access SPU local store.  */
  202.   if (object == TARGET_OBJECT_MEMORY)
  203.     {
  204.       int fd = SPUADDR_SPU (offset);
  205.       CORE_ADDR addr = SPUADDR_ADDR (offset);
  206.       char mem_annex[32], lslr_annex[32];
  207.       gdb_byte buf[32];
  208.       ULONGEST lslr;
  209.       enum target_xfer_status ret;

  210.       if (fd >= 0)
  211.         {
  212.           xsnprintf (mem_annex, sizeof mem_annex, "%d/mem", fd);
  213.           ret = ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
  214.                                               mem_annex, readbuf, writebuf,
  215.                                               addr, len, xfered_len);
  216.           if (ret == TARGET_XFER_OK)
  217.             return ret;

  218.           /* SPU local store access wraps the address around at the
  219.              local store limit.  We emulate this here.  To avoid needing
  220.              an extra access to retrieve the LSLR, we only do that after
  221.              trying the original address first, and getting end-of-file.  */
  222.           xsnprintf (lslr_annex, sizeof lslr_annex, "%d/lslr", fd);
  223.           memset (buf, 0, sizeof buf);
  224.           if (ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
  225.                                             lslr_annex, buf, NULL,
  226.                                             0, sizeof buf, xfered_len)
  227.               != TARGET_XFER_OK)
  228.             return ret;

  229.           lslr = strtoulst ((char *) buf, NULL, 16);
  230.           return ops_beneath->to_xfer_partial (ops_beneath, TARGET_OBJECT_SPU,
  231.                                                mem_annex, readbuf, writebuf,
  232.                                                addr & lslr, len, xfered_len);
  233.         }
  234.     }

  235.   return ops_beneath->to_xfer_partial (ops_beneath, object, annex,
  236.                                        readbuf, writebuf, offset, len, xfered_len);
  237. }

  238. /* Override the to_search_memory routine.  */
  239. static int
  240. spu_search_memory (struct target_ops* ops,
  241.                    CORE_ADDR start_addr, ULONGEST search_space_len,
  242.                    const gdb_byte *pattern, ULONGEST pattern_len,
  243.                    CORE_ADDR *found_addrp)
  244. {
  245.   struct target_ops *ops_beneath = find_target_beneath (ops);

  246.   /* For SPU local store, always fall back to the simple method.  */
  247.   if (SPUADDR_SPU (start_addr) >= 0)
  248.     return simple_search_memory (ops,
  249.                                  start_addr, search_space_len,
  250.                                  pattern, pattern_len, found_addrp);

  251.   return ops_beneath->to_search_memory (ops_beneath,
  252.                                         start_addr, search_space_len,
  253.                                         pattern, pattern_len, found_addrp);
  254. }


  255. /* Push and pop the SPU multi-architecture support target.  */

  256. static void
  257. spu_multiarch_activate (void)
  258. {
  259.   /* If GDB was configured without SPU architecture support,
  260.      we cannot install SPU multi-architecture support either.  */
  261.   if (spu_gdbarch (-1) == NULL)
  262.     return;

  263.   push_target (&spu_ops);

  264.   /* Make sure the thread architecture is re-evaluated.  */
  265.   registers_changed ();
  266. }

  267. static void
  268. spu_multiarch_deactivate (void)
  269. {
  270.   unpush_target (&spu_ops);

  271.   /* Make sure the thread architecture is re-evaluated.  */
  272.   registers_changed ();
  273. }

  274. static void
  275. spu_multiarch_inferior_created (struct target_ops *ops, int from_tty)
  276. {
  277.   if (spu_standalone_p ())
  278.     spu_multiarch_activate ();
  279. }

  280. static void
  281. spu_multiarch_solib_loaded (struct so_list *so)
  282. {
  283.   if (!spu_standalone_p ())
  284.     if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
  285.       if (spu_nr_solib++ == 0)
  286.         spu_multiarch_activate ();
  287. }

  288. static void
  289. spu_multiarch_solib_unloaded (struct so_list *so)
  290. {
  291.   if (!spu_standalone_p ())
  292.     if (so->abfd && bfd_get_arch (so->abfd) == bfd_arch_spu)
  293.       if (--spu_nr_solib == 0)
  294.         spu_multiarch_deactivate ();
  295. }

  296. static void
  297. spu_mourn_inferior (struct target_ops *ops)
  298. {
  299.   struct target_ops *ops_beneath = find_target_beneath (ops);

  300.   ops_beneath->to_mourn_inferior (ops_beneath);
  301.   spu_multiarch_deactivate ();
  302. }


  303. /* Initialize the SPU multi-architecture support target.  */

  304. static void
  305. init_spu_ops (void)
  306. {
  307.   spu_ops.to_shortname = "spu";
  308.   spu_ops.to_longname = "SPU multi-architecture support.";
  309.   spu_ops.to_doc = "SPU multi-architecture support.";
  310.   spu_ops.to_mourn_inferior = spu_mourn_inferior;
  311.   spu_ops.to_fetch_registers = spu_fetch_registers;
  312.   spu_ops.to_store_registers = spu_store_registers;
  313.   spu_ops.to_xfer_partial = spu_xfer_partial;
  314.   spu_ops.to_search_memory = spu_search_memory;
  315.   spu_ops.to_region_ok_for_hw_watchpoint = spu_region_ok_for_hw_watchpoint;
  316.   spu_ops.to_thread_architecture = spu_thread_architecture;
  317.   spu_ops.to_stratum = arch_stratum;
  318.   spu_ops.to_magic = OPS_MAGIC;
  319. }

  320. /* -Wmissing-prototypes */
  321. extern initialize_file_ftype _initialize_spu_multiarch;

  322. void
  323. _initialize_spu_multiarch (void)
  324. {
  325.   /* Install ourselves on the target stack.  */
  326.   init_spu_ops ();
  327.   complete_target_initialization (&spu_ops);

  328.   /* Install observers to watch for SPU objects.  */
  329.   observer_attach_inferior_created (spu_multiarch_inferior_created);
  330.   observer_attach_solib_loaded (spu_multiarch_solib_loaded);
  331.   observer_attach_solib_unloaded (spu_multiarch_solib_unloaded);
  332. }