gdb/frv-linux-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for GNU/Linux running on the Fujitsu FR-V,
  2.    for GDB.

  3.    Copyright (C) 2004-2015 Free Software Foundation, Inc.

  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 "target.h"
  18. #include "frame.h"
  19. #include "osabi.h"
  20. #include "regcache.h"
  21. #include "elf-bfd.h"
  22. #include "elf/frv.h"
  23. #include "frv-tdep.h"
  24. #include "trad-frame.h"
  25. #include "frame-unwind.h"
  26. #include "regset.h"
  27. #include "linux-tdep.h"

  28. /* Define the size (in bytes) of an FR-V instruction.  */
  29. static const int frv_instr_size = 4;

  30. enum {
  31.   NORMAL_SIGTRAMP = 1,
  32.   RT_SIGTRAMP = 2
  33. };

  34. static int
  35. frv_linux_pc_in_sigtramp (struct gdbarch *gdbarch, CORE_ADDR pc,
  36.                           const char *name)
  37. {
  38.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  39.   gdb_byte buf[frv_instr_size];
  40.   LONGEST instr;
  41.   int retval = 0;

  42.   if (target_read_memory (pc, buf, sizeof buf) != 0)
  43.     return 0;

  44.   instr = extract_unsigned_integer (buf, sizeof buf, byte_order);

  45.   if (instr == 0x8efc0077)        /* setlos #__NR_sigreturn, gr7 */
  46.     retval = NORMAL_SIGTRAMP;
  47.   else if (instr == 0x8efc00ad)        /* setlos #__NR_rt_sigreturn, gr7 */
  48.     retval = RT_SIGTRAMP;
  49.   else
  50.     return 0;

  51.   if (target_read_memory (pc + frv_instr_size, buf, sizeof buf) != 0)
  52.     return 0;
  53.   instr = extract_unsigned_integer (buf, sizeof buf, byte_order);
  54.   if (instr != 0xc0700000)        /* tira        gr0, 0 */
  55.     return 0;

  56.   /* If we get this far, we'll return a non-zero value, either
  57.      NORMAL_SIGTRAMP (1) or RT_SIGTRAMP (2).  */
  58.   return retval;
  59. }

  60. /* Given NEXT_FRAME, the "callee" frame of the sigtramp frame that we
  61.    wish to decode, and REGNO, one of the frv register numbers defined
  62.    in frv-tdep.h, return the address of the saved register (corresponding
  63.    to REGNO) in the sigtramp frame.  Return -1 if the register is not
  64.    found in the sigtramp frame.  The magic numbers in the code below
  65.    were computed by examining the following kernel structs:

  66.    From arch/frv/kernel/signal.c:

  67.       struct sigframe
  68.       {
  69.               void (*pretcode)(void);
  70.               int sig;
  71.               struct sigcontext sc;
  72.               unsigned long extramask[_NSIG_WORDS-1];
  73.               uint32_t retcode[2];
  74.       };

  75.       struct rt_sigframe
  76.       {
  77.               void (*pretcode)(void);
  78.               int sig;
  79.               struct siginfo *pinfo;
  80.               void *puc;
  81.               struct siginfo info;
  82.               struct ucontext uc;
  83.               uint32_t retcode[2];
  84.       };

  85.    From include/asm-frv/ucontext.h:

  86.       struct ucontext {
  87.               unsigned long                uc_flags;
  88.               struct ucontext                *uc_link;
  89.               stack_t                        uc_stack;
  90.               struct sigcontext        uc_mcontext;
  91.               sigset_t                uc_sigmask;
  92.       };

  93.    From include/asm-frv/signal.h:

  94.       typedef struct sigaltstack {
  95.               void *ss_sp;
  96.               int ss_flags;
  97.               size_t ss_size;
  98.       } stack_t;

  99.    From include/asm-frv/sigcontext.h:

  100.       struct sigcontext {
  101.               struct user_context        sc_context;
  102.               unsigned long                sc_oldmask;
  103.       } __attribute__((aligned(8)));

  104.    From include/asm-frv/registers.h:
  105.       struct user_int_regs
  106.       {
  107.               unsigned long                psr;
  108.               unsigned long                isr;
  109.               unsigned long                ccr;
  110.               unsigned long                cccr;
  111.               unsigned long                lr;
  112.               unsigned long                lcr;
  113.               unsigned long                pc;
  114.               unsigned long                __status;
  115.               unsigned long                syscallno;
  116.               unsigned long                orig_gr8;
  117.               unsigned long                gner[2];
  118.               unsigned long long        iacc[1];

  119.               union {
  120.                       unsigned long        tbr;
  121.                       unsigned long        gr[64];
  122.               };
  123.       };

  124.       struct user_fpmedia_regs
  125.       {
  126.               unsigned long        fr[64];
  127.               unsigned long        fner[2];
  128.               unsigned long        msr[2];
  129.               unsigned long        acc[8];
  130.               unsigned char        accg[8];
  131.               unsigned long        fsr[1];
  132.       };

  133.       struct user_context
  134.       {
  135.               struct user_int_regs                i;
  136.               struct user_fpmedia_regs        f;

  137.               void *extension;
  138.       } __attribute__((aligned(8)));  */

  139. static LONGEST
  140. frv_linux_sigcontext_reg_addr (struct frame_info *this_frame, int regno,
  141.                                CORE_ADDR *sc_addr_cache_ptr)
  142. {
  143.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  144.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  145.   CORE_ADDR sc_addr;

  146.   if (sc_addr_cache_ptr && *sc_addr_cache_ptr)
  147.     {
  148.       sc_addr = *sc_addr_cache_ptr;
  149.     }
  150.   else
  151.     {
  152.       CORE_ADDR pc, sp;
  153.       gdb_byte buf[4];
  154.       int tramp_type;

  155.       pc = get_frame_pc (this_frame);
  156.       tramp_type = frv_linux_pc_in_sigtramp (gdbarch, pc, 0);

  157.       get_frame_register (this_frame, sp_regnum, buf);
  158.       sp = extract_unsigned_integer (buf, sizeof buf, byte_order);

  159.       if (tramp_type == NORMAL_SIGTRAMP)
  160.         {
  161.           /* For a normal sigtramp frame, the sigcontext struct starts
  162.              at SP + 8.  */
  163.           sc_addr = sp + 8;
  164.         }
  165.       else if (tramp_type == RT_SIGTRAMP)
  166.         {
  167.           /* For a realtime sigtramp frame, SP + 12 contains a pointer
  168.               to a ucontext struct.  The ucontext struct contains a
  169.               sigcontext struct starting 24 bytes in.  (The offset of
  170.               uc_mcontext within struct ucontext is derived as follows:
  171.               stack_t is a 12-byte struct and struct sigcontext is
  172.               8-byte aligned.  This gives an offset of 8 + 12 + 4 (for
  173.               padding) = 24.)  */
  174.           if (target_read_memory (sp + 12, buf, sizeof buf) != 0)
  175.             {
  176.               warning (_("Can't read realtime sigtramp frame."));
  177.               return 0;
  178.             }
  179.           sc_addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
  180.            sc_addr += 24;
  181.         }
  182.       else
  183.         internal_error (__FILE__, __LINE__, _("not a signal trampoline"));

  184.       if (sc_addr_cache_ptr)
  185.         *sc_addr_cache_ptr = sc_addr;
  186.     }

  187.   switch (regno)
  188.     {
  189.     case psr_regnum :
  190.       return sc_addr + 0;
  191.     /* sc_addr + 4 has "isr", the Integer Status Register.  */
  192.     case ccr_regnum :
  193.       return sc_addr + 8;
  194.     case cccr_regnum :
  195.       return sc_addr + 12;
  196.     case lr_regnum :
  197.       return sc_addr + 16;
  198.     case lcr_regnum :
  199.       return sc_addr + 20;
  200.     case pc_regnum :
  201.       return sc_addr + 24;
  202.     /* sc_addr + 28 is __status, the exception status.
  203.        sc_addr + 32 is syscallno, the syscall number or -1.
  204.        sc_addr + 36 is orig_gr8, the original syscall arg #1.
  205.        sc_addr + 40 is gner[0].
  206.        sc_addr + 44 is gner[1].  */
  207.     case iacc0h_regnum :
  208.       return sc_addr + 48;
  209.     case iacc0l_regnum :
  210.       return sc_addr + 52;
  211.     default :
  212.       if (first_gpr_regnum <= regno && regno <= last_gpr_regnum)
  213.         return sc_addr + 56 + 4 * (regno - first_gpr_regnum);
  214.       else if (first_fpr_regnum <= regno && regno <= last_fpr_regnum)
  215.         return sc_addr + 312 + 4 * (regno - first_fpr_regnum);
  216.       else
  217.         return -1/* not saved.  */
  218.     }
  219. }

  220. /* Signal trampolines.  */

  221. static struct trad_frame_cache *
  222. frv_linux_sigtramp_frame_cache (struct frame_info *this_frame,
  223.                                 void **this_cache)
  224. {
  225.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  226.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  227.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  228.   struct trad_frame_cache *cache;
  229.   CORE_ADDR addr;
  230.   gdb_byte buf[4];
  231.   int regnum;
  232.   CORE_ADDR sc_addr_cache_val = 0;
  233.   struct frame_id this_id;

  234.   if (*this_cache)
  235.     return *this_cache;

  236.   cache = trad_frame_cache_zalloc (this_frame);

  237.   /* FIXME: cagney/2004-05-01: This is is long standing broken code.
  238.      The frame ID's code address should be the start-address of the
  239.      signal trampoline and not the current PC within that
  240.      trampoline.  */
  241.   get_frame_register (this_frame, sp_regnum, buf);
  242.   addr = extract_unsigned_integer (buf, sizeof buf, byte_order);
  243.   this_id = frame_id_build (addr, get_frame_pc (this_frame));
  244.   trad_frame_set_id (cache, this_id);

  245.   for (regnum = 0; regnum < frv_num_regs; regnum++)
  246.     {
  247.       LONGEST reg_addr = frv_linux_sigcontext_reg_addr (this_frame, regnum,
  248.                                                         &sc_addr_cache_val);
  249.       if (reg_addr != -1)
  250.         trad_frame_set_reg_addr (cache, regnum, reg_addr);
  251.     }

  252.   *this_cache = cache;
  253.   return cache;
  254. }

  255. static void
  256. frv_linux_sigtramp_frame_this_id (struct frame_info *this_frame,
  257.                                   void **this_cache,
  258.                                   struct frame_id *this_id)
  259. {
  260.   struct trad_frame_cache *cache
  261.     = frv_linux_sigtramp_frame_cache (this_frame, this_cache);
  262.   trad_frame_get_id (cache, this_id);
  263. }

  264. static struct value *
  265. frv_linux_sigtramp_frame_prev_register (struct frame_info *this_frame,
  266.                                         void **this_cache, int regnum)
  267. {
  268.   /* Make sure we've initialized the cache.  */
  269.   struct trad_frame_cache *cache
  270.     = frv_linux_sigtramp_frame_cache (this_frame, this_cache);
  271.   return trad_frame_get_register (cache, this_frame, regnum);
  272. }

  273. static int
  274. frv_linux_sigtramp_frame_sniffer (const struct frame_unwind *self,
  275.                                   struct frame_info *this_frame,
  276.                                   void **this_cache)
  277. {
  278.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  279.   CORE_ADDR pc = get_frame_pc (this_frame);
  280.   const char *name;

  281.   find_pc_partial_function (pc, &name, NULL, NULL);
  282.   if (frv_linux_pc_in_sigtramp (gdbarch, pc, name))
  283.     return 1;

  284.   return 0;
  285. }

  286. static const struct frame_unwind frv_linux_sigtramp_frame_unwind =
  287. {
  288.   SIGTRAMP_FRAME,
  289.   default_frame_unwind_stop_reason,
  290.   frv_linux_sigtramp_frame_this_id,
  291.   frv_linux_sigtramp_frame_prev_register,
  292.   NULL,
  293.   frv_linux_sigtramp_frame_sniffer
  294. };

  295. /* The FRV kernel defines ELF_NGREG as 46.  We add 2 in order to include
  296.    the loadmap addresses in the register set.  (See below for more info.)  */
  297. #define FRV_ELF_NGREG (46 + 2)
  298. typedef unsigned char frv_elf_greg_t[4];
  299. typedef struct { frv_elf_greg_t reg[FRV_ELF_NGREG]; } frv_elf_gregset_t;

  300. typedef unsigned char frv_elf_fpreg_t[4];
  301. typedef struct
  302. {
  303.   frv_elf_fpreg_t fr[64];
  304.   frv_elf_fpreg_t fner[2];
  305.   frv_elf_fpreg_t msr[2];
  306.   frv_elf_fpreg_t acc[8];
  307.   unsigned char accg[8];
  308.   frv_elf_fpreg_t fsr[1];
  309. } frv_elf_fpregset_t;

  310. /* Register maps.  */

  311. static const struct regcache_map_entry frv_linux_gregmap[] =
  312.   {
  313.     { 1, psr_regnum, 4 },
  314.     { 1, REGCACHE_MAP_SKIP, 4 }, /* isr */
  315.     { 1, ccr_regnum, 4 },
  316.     { 1, cccr_regnum, 4 },
  317.     { 1, lr_regnum, 4 },
  318.     { 1, lcr_regnum, 4 },
  319.     { 1, pc_regnum, 4 },
  320.     { 1, REGCACHE_MAP_SKIP, 4 }, /* __status */
  321.     { 1, REGCACHE_MAP_SKIP, 4 }, /* syscallno */
  322.     { 1, REGCACHE_MAP_SKIP, 4 }, /* orig_gr8 */
  323.     { 1, gner0_regnum, 4 },
  324.     { 1, gner1_regnum, 4 },
  325.     { 1, REGCACHE_MAP_SKIP, 8 }, /* iacc0 */
  326.     { 1, tbr_regnum, 4 },
  327.     { 31, first_gpr_regnum + 1, 4 }, /* gr1 ... gr31 */

  328.     /* Technically, the loadmap addresses are not part of `pr_reg' as
  329.        found in the elf_prstatus struct.  The fields which communicate
  330.        the loadmap address appear (by design) immediately after
  331.        `pr_reg' though, and the BFD function elf32_frv_grok_prstatus()
  332.        has been implemented to include these fields in the register
  333.        section that it extracts from the core file.  So, for our
  334.        purposes, they may be viewed as registers.  */

  335.     { 1, fdpic_loadmap_exec_regnum, 4 },
  336.     { 1, fdpic_loadmap_interp_regnum, 4 },
  337.     { 0 }
  338.   };

  339. static const struct regcache_map_entry frv_linux_fpregmap[] =
  340.   {
  341.     { 64, first_fpr_regnum, 4 }, /* fr0 ... fr63 */
  342.     { 1, fner0_regnum, 4 },
  343.     { 1, fner1_regnum, 4 },
  344.     { 1, msr0_regnum, 4 },
  345.     { 1, msr1_regnum, 4 },
  346.     { 8, acc0_regnum, 4 },        /* acc0 ... acc7 */
  347.     { 1, accg0123_regnum, 4 },
  348.     { 1, accg4567_regnum, 4 },
  349.     { 1, fsr0_regnum, 4 },
  350.     { 0 }
  351.   };

  352. /* Unpack an frv_elf_gregset_t into GDB's register cache.  */

  353. static void
  354. frv_linux_supply_gregset (const struct regset *regset,
  355.                           struct regcache *regcache,
  356.                           int regnum, const void *gregs, size_t len)
  357. {
  358.   int regi;
  359.   char zerobuf[MAX_REGISTER_SIZE];

  360.   memset (zerobuf, 0, MAX_REGISTER_SIZE);

  361.   /* gr0 always contains 0.  Also, the kernel passes the TBR value in
  362.      this slot.  */
  363.   regcache_raw_supply (regcache, first_gpr_regnum, zerobuf);

  364.   /* Fill gr32, ..., gr63 with zeros. */
  365.   for (regi = first_gpr_regnum + 32; regi <= last_gpr_regnum; regi++)
  366.     regcache_raw_supply (regcache, regi, zerobuf);

  367.   regcache_supply_regset (regset, regcache, regnum, gregs, len);
  368. }

  369. /* FRV Linux kernel register sets.  */

  370. static const struct regset frv_linux_gregset =
  371. {
  372.   frv_linux_gregmap,
  373.   frv_linux_supply_gregset, regcache_collect_regset
  374. };

  375. static const struct regset frv_linux_fpregset =
  376. {
  377.   frv_linux_fpregmap,
  378.   regcache_supply_regset, regcache_collect_regset
  379. };

  380. static void
  381. frv_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  382.                                         iterate_over_regset_sections_cb *cb,
  383.                                         void *cb_data,
  384.                                         const struct regcache *regcache)
  385. {
  386.   cb (".reg", sizeof (frv_elf_gregset_t), &frv_linux_gregset,
  387.       NULL, cb_data);
  388.   cb (".reg2", sizeof (frv_elf_fpregset_t), &frv_linux_fpregset,
  389.       NULL, cb_data);
  390. }


  391. static void
  392. frv_linux_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
  393. {
  394.   linux_init_abi (info, gdbarch);

  395.   /* Set the sigtramp frame sniffer.  */
  396.   frame_unwind_append_unwinder (gdbarch, &frv_linux_sigtramp_frame_unwind);

  397.   set_gdbarch_iterate_over_regset_sections
  398.     (gdbarch, frv_linux_iterate_over_regset_sections);
  399. }

  400. static enum gdb_osabi
  401. frv_linux_elf_osabi_sniffer (bfd *abfd)
  402. {
  403.   int elf_flags;

  404.   elf_flags = elf_elfheader (abfd)->e_flags;

  405.   /* Assume GNU/Linux if using the FDPIC ABI.  If/when another OS shows
  406.      up that uses this ABI, we'll need to start using .note sections
  407.      or some such.  */
  408.   if (elf_flags & EF_FRV_FDPIC)
  409.     return GDB_OSABI_LINUX;
  410.   else
  411.     return GDB_OSABI_UNKNOWN;
  412. }

  413. /* Provide a prototype to silence -Wmissing-prototypes.  */
  414. void _initialize_frv_linux_tdep (void);

  415. void
  416. _initialize_frv_linux_tdep (void)
  417. {
  418.   gdbarch_register_osabi (bfd_arch_frv, 0, GDB_OSABI_LINUX,
  419.                           frv_linux_init_abi);
  420.   gdbarch_register_osabi_sniffer (bfd_arch_frv,
  421.                                   bfd_target_elf_flavour,
  422.                                   frv_linux_elf_osabi_sniffer);
  423. }