gdb/sparc64obsd-tdep.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for OpenBSD/sparc64.

  2.    Copyright (C) 2004-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 "frame-unwind.h"
  17. #include "gdbcore.h"
  18. #include "osabi.h"
  19. #include "regcache.h"
  20. #include "regset.h"
  21. #include "symtab.h"
  22. #include "objfiles.h"
  23. #include "trad-frame.h"

  24. #include "obsd-tdep.h"
  25. #include "sparc64-tdep.h"
  26. #include "solib-svr4.h"
  27. #include "bsd-uthread.h"

  28. /* Older OpenBSD versions used the traditional NetBSD core file
  29.    format, even for ports that use ELF.  These core files don't use
  30.    multiple register sets.  Instead, the general-purpose and
  31.    floating-point registers are lumped together in a single section.
  32.    Unlike on NetBSD, OpenBSD uses a different layout for its
  33.    general-purpose registers than the layout used for ptrace(2).

  34.    Newer OpenBSD versions use ELF core files.  Here the register sets
  35.    match the ptrace(2) layout.  */

  36. /* From <machine/reg.h>.  */
  37. const struct sparc_gregmap sparc64obsd_gregmap =
  38. {
  39.   0 * 8,                        /* "tstate" */
  40.   1 * 8,                        /* %pc */
  41.   2 * 8,                        /* %npc */
  42.   3 * 8,                        /* %y */
  43.   -1,                                /* %fprs */
  44.   -1,
  45.   5 * 8,                        /* %g1 */
  46.   20 * 8,                        /* %l0 */
  47.   4                                /* sizeof (%y) */
  48. };

  49. const struct sparc_gregmap sparc64obsd_core_gregmap =
  50. {
  51.   0 * 8,                        /* "tstate" */
  52.   1 * 8,                        /* %pc */
  53.   2 * 8,                        /* %npc */
  54.   3 * 8,                        /* %y */
  55.   -1,                                /* %fprs */
  56.   -1,
  57.   7 * 8,                        /* %g1 */
  58.   22 * 8,                        /* %l0 */
  59.   4                                /* sizeof (%y) */
  60. };

  61. static void
  62. sparc64obsd_supply_gregset (const struct regset *regset,
  63.                             struct regcache *regcache,
  64.                             int regnum, const void *gregs, size_t len)
  65. {
  66.   const void *fpregs = (char *)gregs + 288;

  67.   if (len < 832)
  68.     {
  69.       sparc64_supply_gregset (&sparc64obsd_gregmap, regcache, regnum, gregs);
  70.       return;
  71.     }

  72.   sparc64_supply_gregset (&sparc64obsd_core_gregmap, regcache, regnum, gregs);
  73.   sparc64_supply_fpregset (&sparc64_bsd_fpregmap, regcache, regnum, fpregs);
  74. }

  75. static void
  76. sparc64obsd_supply_fpregset (const struct regset *regset,
  77.                              struct regcache *regcache,
  78.                              int regnum, const void *fpregs, size_t len)
  79. {
  80.   sparc64_supply_fpregset (&sparc64_bsd_fpregmap, regcache, regnum, fpregs);
  81. }


  82. /* Signal trampolines.  */

  83. /* Since OpenBSD 3.2, the sigtramp routine is mapped at a random page
  84.    in virtual memory.  The randomness makes it somewhat tricky to
  85.    detect it, but fortunately we can rely on the fact that the start
  86.    of the sigtramp routine is page-aligned.  We recognize the
  87.    trampoline by looking for the code that invokes the sigreturn
  88.    system call.  The offset where we can find that code varies from
  89.    release to release.

  90.    By the way, the mapping mentioned above is read-only, so you cannot
  91.    place a breakpoint in the signal trampoline.  */

  92. /* Default page size.  */
  93. static const int sparc64obsd_page_size = 8192;

  94. /* Offset for sigreturn(2).  */
  95. static const int sparc64obsd_sigreturn_offset[] = {
  96.   0xf0,                                /* OpenBSD 3.8 */
  97.   0xec,                                /* OpenBSD 3.6 */
  98.   0xe8,                                /* OpenBSD 3.2 */
  99.   -1
  100. };

  101. static int
  102. sparc64obsd_pc_in_sigtramp (CORE_ADDR pc, const char *name)
  103. {
  104.   CORE_ADDR start_pc = (pc & ~(sparc64obsd_page_size - 1));
  105.   unsigned long insn;
  106.   const int *offset;

  107.   if (name)
  108.     return 0;

  109.   for (offset = sparc64obsd_sigreturn_offset; *offset != -1; offset++)
  110.     {
  111.       /* Check for "restore %g0, SYS_sigreturn, %g1".  */
  112.       insn = sparc_fetch_instruction (start_pc + *offset);
  113.       if (insn != 0x83e82067)
  114.         continue;

  115.       /* Check for "t ST_SYSCALL".  */
  116.       insn = sparc_fetch_instruction (start_pc + *offset + 8);
  117.       if (insn != 0x91d02000)
  118.         continue;

  119.       return 1;
  120.     }

  121.   return 0;
  122. }

  123. static struct sparc_frame_cache *
  124. sparc64obsd_frame_cache (struct frame_info *this_frame, void **this_cache)
  125. {
  126.   struct sparc_frame_cache *cache;
  127.   CORE_ADDR addr;

  128.   if (*this_cache)
  129.     return *this_cache;

  130.   cache = sparc_frame_cache (this_frame, this_cache);
  131.   gdb_assert (cache == *this_cache);

  132.   /* If we couldn't find the frame's function, we're probably dealing
  133.      with an on-stack signal trampoline.  */
  134.   if (cache->pc == 0)
  135.     {
  136.       cache->pc = get_frame_pc (this_frame);
  137.       cache->pc &= ~(sparc64obsd_page_size - 1);

  138.       /* Since we couldn't find the frame's function, the cache was
  139.          initialized under the assumption that we're frameless.  */
  140.       sparc_record_save_insn (cache);
  141.       addr = get_frame_register_unsigned (this_frame, SPARC_FP_REGNUM);
  142.       if (addr & 1)
  143.         addr += BIAS;
  144.       cache->base = addr;
  145.     }

  146.   /* We find the appropriate instance of `struct sigcontext' at a
  147.      fixed offset in the signal frame.  */
  148.   addr = cache->base + 128 + 16;
  149.   cache->saved_regs = sparc64nbsd_sigcontext_saved_regs (addr, this_frame);

  150.   return cache;
  151. }

  152. static void
  153. sparc64obsd_frame_this_id (struct frame_info *this_frame, void **this_cache,
  154.                            struct frame_id *this_id)
  155. {
  156.   struct sparc_frame_cache *cache =
  157.     sparc64obsd_frame_cache (this_frame, this_cache);

  158.   (*this_id) = frame_id_build (cache->base, cache->pc);
  159. }

  160. static struct value *
  161. sparc64obsd_frame_prev_register (struct frame_info *this_frame,
  162.                                  void **this_cache, int regnum)
  163. {
  164.   struct sparc_frame_cache *cache =
  165.     sparc64obsd_frame_cache (this_frame, this_cache);

  166.   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  167. }

  168. static int
  169. sparc64obsd_sigtramp_frame_sniffer (const struct frame_unwind *self,
  170.                                     struct frame_info *this_frame,
  171.                                     void **this_cache)
  172. {
  173.   CORE_ADDR pc = get_frame_pc (this_frame);
  174.   const char *name;

  175.   find_pc_partial_function (pc, &name, NULL, NULL);
  176.   if (sparc64obsd_pc_in_sigtramp (pc, name))
  177.     return 1;

  178.   return 0;
  179. }

  180. static const struct frame_unwind sparc64obsd_frame_unwind =
  181. {
  182.   SIGTRAMP_FRAME,
  183.   default_frame_unwind_stop_reason,
  184.   sparc64obsd_frame_this_id,
  185.   sparc64obsd_frame_prev_register,
  186.   NULL,
  187.   sparc64obsd_sigtramp_frame_sniffer
  188. };

  189. /* Kernel debugging support.  */

  190. static struct sparc_frame_cache *
  191. sparc64obsd_trapframe_cache (struct frame_info *this_frame, void **this_cache)
  192. {
  193.   struct sparc_frame_cache *cache;
  194.   CORE_ADDR sp, trapframe_addr;
  195.   int regnum;

  196.   if (*this_cache)
  197.     return *this_cache;

  198.   cache = sparc_frame_cache (this_frame, this_cache);
  199.   gdb_assert (cache == *this_cache);

  200.   sp = get_frame_register_unsigned (this_frame, SPARC_SP_REGNUM);
  201.   trapframe_addr = sp + BIAS + 176;

  202.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  203.   cache->saved_regs[SPARC64_STATE_REGNUM].addr = trapframe_addr;
  204.   cache->saved_regs[SPARC64_PC_REGNUM].addr = trapframe_addr + 8;
  205.   cache->saved_regs[SPARC64_NPC_REGNUM].addr = trapframe_addr + 16;

  206.   for (regnum = SPARC_G0_REGNUM; regnum <= SPARC_I7_REGNUM; regnum++)
  207.     cache->saved_regs[regnum].addr =
  208.       trapframe_addr + 48 + (regnum - SPARC_G0_REGNUM) * 8;

  209.   return cache;
  210. }

  211. static void
  212. sparc64obsd_trapframe_this_id (struct frame_info *this_frame,
  213.                                void **this_cache, struct frame_id *this_id)
  214. {
  215.   struct sparc_frame_cache *cache =
  216.     sparc64obsd_trapframe_cache (this_frame, this_cache);

  217.   (*this_id) = frame_id_build (cache->base, cache->pc);
  218. }

  219. static struct value *
  220. sparc64obsd_trapframe_prev_register (struct frame_info *this_frame,
  221.                                      void **this_cache, int regnum)
  222. {
  223.   struct sparc_frame_cache *cache =
  224.     sparc64obsd_trapframe_cache (this_frame, this_cache);

  225.   return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
  226. }

  227. static int
  228. sparc64obsd_trapframe_sniffer (const struct frame_unwind *self,
  229.                                struct frame_info *this_frame,
  230.                                void **this_cache)
  231. {
  232.   CORE_ADDR pc;
  233.   ULONGEST pstate;
  234.   const char *name;

  235.   /* Check whether we are in privileged mode, and bail out if we're not.  */
  236.   pstate = get_frame_register_unsigned (this_frame, SPARC64_PSTATE_REGNUM);
  237.   if ((pstate & SPARC64_PSTATE_PRIV) == 0)
  238.     return 0;

  239.   pc = get_frame_address_in_block (this_frame);
  240.   find_pc_partial_function (pc, &name, NULL, NULL);
  241.   if (name && strcmp (name, "Lslowtrap_reenter") == 0)
  242.     return 1;

  243.   return 0;
  244. }

  245. static const struct frame_unwind sparc64obsd_trapframe_unwind =
  246. {
  247.   NORMAL_FRAME,
  248.   default_frame_unwind_stop_reason,
  249.   sparc64obsd_trapframe_this_id,
  250.   sparc64obsd_trapframe_prev_register,
  251.   NULL,
  252.   sparc64obsd_trapframe_sniffer
  253. };


  254. /* Threads support.  */

  255. /* Offset wthin the thread structure where we can find %fp and %i7.  */
  256. #define SPARC64OBSD_UTHREAD_FP_OFFSET        232
  257. #define SPARC64OBSD_UTHREAD_PC_OFFSET        240

  258. static void
  259. sparc64obsd_supply_uthread (struct regcache *regcache,
  260.                             int regnum, CORE_ADDR addr)
  261. {
  262.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  263.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  264.   CORE_ADDR fp, fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET;
  265.   gdb_byte buf[8];

  266.   gdb_assert (regnum >= -1);

  267.   fp = read_memory_unsigned_integer (fp_addr, 8, byte_order);
  268.   if (regnum == SPARC_SP_REGNUM || regnum == -1)
  269.     {
  270.       store_unsigned_integer (buf, 8, byte_order, fp);
  271.       regcache_raw_supply (regcache, SPARC_SP_REGNUM, buf);

  272.       if (regnum == SPARC_SP_REGNUM)
  273.         return;
  274.     }

  275.   if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM
  276.       || regnum == -1)
  277.     {
  278.       CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET;

  279.       i7 = read_memory_unsigned_integer (i7_addr, 8, byte_order);
  280.       if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  281.         {
  282.           store_unsigned_integer (buf, 8, byte_order, i7 + 8);
  283.           regcache_raw_supply (regcache, SPARC64_PC_REGNUM, buf);
  284.         }
  285.       if (regnum == SPARC64_NPC_REGNUM || regnum == -1)
  286.         {
  287.           store_unsigned_integer (buf, 8, byte_order, i7 + 12);
  288.           regcache_raw_supply (regcache, SPARC64_NPC_REGNUM, buf);
  289.         }

  290.       if (regnum == SPARC64_PC_REGNUM || regnum == SPARC64_NPC_REGNUM)
  291.         return;
  292.     }

  293.   sparc_supply_rwindow (regcache, fp, regnum);
  294. }

  295. static void
  296. sparc64obsd_collect_uthread(const struct regcache *regcache,
  297.                             int regnum, CORE_ADDR addr)
  298. {
  299.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  300.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  301.   CORE_ADDR sp;
  302.   gdb_byte buf[8];

  303.   gdb_assert (regnum >= -1);

  304.   if (regnum == SPARC_SP_REGNUM || regnum == -1)
  305.     {
  306.       CORE_ADDR fp_addr = addr + SPARC64OBSD_UTHREAD_FP_OFFSET;

  307.       regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
  308.       write_memory (fp_addr,buf, 8);
  309.     }

  310.   if (regnum == SPARC64_PC_REGNUM || regnum == -1)
  311.     {
  312.       CORE_ADDR i7, i7_addr = addr + SPARC64OBSD_UTHREAD_PC_OFFSET;

  313.       regcache_raw_collect (regcache, SPARC64_PC_REGNUM, buf);
  314.       i7 = extract_unsigned_integer (buf, 8, byte_order) - 8;
  315.       write_memory_unsigned_integer (i7_addr, 8, byte_order, i7);

  316.       if (regnum == SPARC64_PC_REGNUM)
  317.         return;
  318.     }

  319.   regcache_raw_collect (regcache, SPARC_SP_REGNUM, buf);
  320.   sp = extract_unsigned_integer (buf, 8, byte_order);
  321.   sparc_collect_rwindow (regcache, sp, regnum);
  322. }


  323. static const struct regset sparc64obsd_gregset =
  324.   {
  325.     NULL, sparc64obsd_supply_gregset, NULL
  326.   };

  327. static const struct regset sparc64obsd_fpregset =
  328.   {
  329.     NULL, sparc64obsd_supply_fpregset, NULL
  330.   };

  331. static void
  332. sparc64obsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  333. {
  334.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  335.   tdep->gregset = &sparc64obsd_gregset;
  336.   tdep->sizeof_gregset = 288;
  337.   tdep->fpregset = &sparc64obsd_fpregset;
  338.   tdep->sizeof_fpregset = 272;

  339.   /* Make sure we can single-step "new" syscalls.  */
  340.   tdep->step_trap = sparcnbsd_step_trap;

  341.   frame_unwind_append_unwinder (gdbarch, &sparc64obsd_frame_unwind);
  342.   frame_unwind_append_unwinder (gdbarch, &sparc64obsd_trapframe_unwind);

  343.   sparc64_init_abi (info, gdbarch);
  344.   obsd_init_abi (info, gdbarch);

  345.   /* OpenBSD/sparc64 has SVR4-style shared libraries.  */
  346.   set_solib_svr4_fetch_link_map_offsets
  347.     (gdbarch, svr4_lp64_fetch_link_map_offsets);
  348.   set_gdbarch_skip_solib_resolver (gdbarch, obsd_skip_solib_resolver);

  349.   /* OpenBSD provides a user-level threads implementation.  */
  350.   bsd_uthread_set_supply_uthread (gdbarch, sparc64obsd_supply_uthread);
  351.   bsd_uthread_set_collect_uthread (gdbarch, sparc64obsd_collect_uthread);
  352. }


  353. /* Provide a prototype to silence -Wmissing-prototypes.  */
  354. void _initialize_sparc64obsd_tdep (void);

  355. void
  356. _initialize_sparc64obsd_tdep (void)
  357. {
  358.   gdbarch_register_osabi (bfd_arch_sparc, bfd_mach_sparc_v9,
  359.                           GDB_OSABI_OPENBSD_ELF, sparc64obsd_init_abi);
  360. }