gdb/gdbserver/linux-crisv32-low.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* GNU/Linux/CRIS specific low level interface, for the remote server for GDB.
  2.    Copyright (C) 1995-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 "server.h"
  15. #include "linux-low.h"
  16. #include <sys/ptrace.h>

  17. /* Defined in auto-generated file reg-crisv32.c.  */
  18. void init_registers_crisv32 (void);
  19. extern const struct target_desc *tdesc_crisv32;

  20. /* CRISv32 */
  21. #define cris_num_regs 49

  22. #ifndef PTRACE_GET_THREAD_AREA
  23. #define PTRACE_GET_THREAD_AREA 25
  24. #endif

  25. /* Note: Ignoring USP (having the stack pointer in two locations causes trouble
  26.    without any significant gain).  */

  27. /* Locations need to match <include/asm/arch/ptrace.h>.  */
  28. static int cris_regmap[] = {
  29.   1*4, 2*4, 3*4, 4*4,
  30.   5*4, 6*4, 7*4, 8*4,
  31.   9*4, 10*4, 11*4, 12*4,
  32.   13*4, 14*4, 24*4, 15*4,

  33.   -1, -1, -1, 16*4,
  34.   -1, 22*4, 23*4, 17*4,
  35.   -1, -1, 21*4, 20*4,
  36.   -1, 19*4, -1, 18*4,

  37.   25*4,

  38.   26*4, -1,   -1,   29*4,
  39.   30*4, 31*4, 32*4, 33*4,
  40.   34*4, 35*4, 36*4, 37*4,
  41.   38*4, 39*4, 40*4, -1

  42. };

  43. extern int debug_threads;

  44. static CORE_ADDR
  45. cris_get_pc (struct regcache *regcache)
  46. {
  47.   unsigned long pc;
  48.   collect_register_by_name (regcache, "pc", &pc);
  49.   if (debug_threads)
  50.     debug_printf ("stop pc is %08lx\n", pc);
  51.   return pc;
  52. }

  53. static void
  54. cris_set_pc (struct regcache *regcache, CORE_ADDR pc)
  55. {
  56.   unsigned long newpc = pc;
  57.   supply_register_by_name (regcache, "pc", &newpc);
  58. }

  59. static const unsigned short cris_breakpoint = 0xe938;
  60. #define cris_breakpoint_len 2

  61. static int
  62. cris_breakpoint_at (CORE_ADDR where)
  63. {
  64.   unsigned short insn;

  65.   (*the_target->read_memory) (where, (unsigned char *) &insn,
  66.                               cris_breakpoint_len);
  67.   if (insn == cris_breakpoint)
  68.     return 1;

  69.   /* If necessary, recognize more trap instructions here.  GDB only uses the
  70.      one.  */
  71.   return 0;
  72. }

  73. /* We only place breakpoints in empty marker functions, and thread locking
  74.    is outside of the function.  So rather than importing software single-step,
  75.    we can just run until exit.  */

  76. /* FIXME: This function should not be needed, since we have PTRACE_SINGLESTEP
  77.    for CRISv32.  Without it, td_ta_event_getmsg in thread_db_create_event
  78.    will fail when debugging multi-threaded applications.  */

  79. static CORE_ADDR
  80. cris_reinsert_addr (void)
  81. {
  82.   struct regcache *regcache = get_thread_regcache (current_thread, 1);
  83.   unsigned long pc;
  84.   collect_register_by_name (regcache, "srp", &pc);
  85.   return pc;
  86. }

  87. static void
  88. cris_write_data_breakpoint (struct regcache *regcache,
  89.                             int bp, unsigned long start, unsigned long end)
  90. {
  91.   switch (bp)
  92.     {
  93.     case 0:
  94.       supply_register_by_name (regcache, "s3", &start);
  95.       supply_register_by_name (regcache, "s4", &end);
  96.       break;
  97.     case 1:
  98.       supply_register_by_name (regcache, "s5", &start);
  99.       supply_register_by_name (regcache, "s6", &end);
  100.       break;
  101.     case 2:
  102.       supply_register_by_name (regcache, "s7", &start);
  103.       supply_register_by_name (regcache, "s8", &end);
  104.       break;
  105.     case 3:
  106.       supply_register_by_name (regcache, "s9", &start);
  107.       supply_register_by_name (regcache, "s10", &end);
  108.       break;
  109.     case 4:
  110.       supply_register_by_name (regcache, "s11", &start);
  111.       supply_register_by_name (regcache, "s12", &end);
  112.       break;
  113.     case 5:
  114.       supply_register_by_name (regcache, "s13", &start);
  115.       supply_register_by_name (regcache, "s14", &end);
  116.       break;
  117.     }
  118. }

  119. static int
  120. cris_supports_z_point_type (char z_type)
  121. {
  122.   switch (z_type)
  123.     {
  124.     case Z_PACKET_WRITE_WP:
  125.     case Z_PACKET_READ_WP:
  126.     case Z_PACKET_ACCESS_WP:
  127.       return 1;
  128.     default:
  129.       return 0;
  130.     }
  131. }

  132. static int
  133. cris_insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
  134.                    int len, struct raw_breakpoint *bp)
  135. {
  136.   int bp;
  137.   unsigned long bp_ctrl;
  138.   unsigned long start, end;
  139.   unsigned long ccs;
  140.   struct regcache *regcache;

  141.   regcache = get_thread_regcache (current_thread, 1);

  142.   /* Read watchpoints are set as access watchpoints, because of GDB's
  143.      inability to deal with pure read watchpoints.  */
  144.   if (type == raw_bkpt_type_read_wp)
  145.     type = raw_bkpt_type_access_wp;

  146.   /* Get the configuration register.  */
  147.   collect_register_by_name (regcache, "s0", &bp_ctrl);

  148.   /* The watchpoint allocation scheme is the simplest possible.
  149.      For example, if a region is watched for read and
  150.      a write watch is requested, a new watchpoint will
  151.      be used.  Also, if a watch for a region that is already
  152.      covered by one or more existing watchpoints, a new
  153.      watchpoint will be used.  */

  154.   /* First, find a free data watchpoint.  */
  155.   for (bp = 0; bp < 6; bp++)
  156.     {
  157.       /* Each data watchpoint's control registers occupy 2 bits
  158.          (hence the 3), starting at bit 2 for D0 (hence the 2)
  159.          with 4 bits between for each watchpoint (yes, the 4).  */
  160.       if (!(bp_ctrl & (0x3 << (2 + (bp * 4)))))
  161.         break;
  162.     }

  163.   if (bp > 5)
  164.     {
  165.       /* We're out of watchpoints.  */
  166.       return -1;
  167.     }

  168.   /* Configure the control register first.  */
  169.   if (type == raw_bkpt_type_read_wp || type == raw_bkpt_type_access_wp)
  170.     {
  171.       /* Trigger on read.  */
  172.       bp_ctrl |= (1 << (2 + bp * 4));
  173.     }
  174.   if (type == raw_bkpt_type_write_wp || type == raw_bkpt_type_access_wp)
  175.     {
  176.       /* Trigger on write.  */
  177.       bp_ctrl |= (2 << (2 + bp * 4));
  178.     }

  179.   /* Setup the configuration register.  */
  180.   supply_register_by_name (regcache, "s0", &bp_ctrl);

  181.   /* Setup the range.  */
  182.   start = addr;
  183.   end = addr + len - 1;

  184.   /* Configure the watchpoint register.  */
  185.   cris_write_data_breakpoint (regcache, bp, start, end);

  186.   collect_register_by_name (regcache, "ccs", &ccs);
  187.   /* Set the S1 flag to enable watchpoints.  */
  188.   ccs |= (1 << 19);
  189.   supply_register_by_name (regcache, "ccs", &ccs);

  190.   return 0;
  191. }

  192. static int
  193. cris_remove_point (enum raw_bkpt_type type, CORE_ADDR addr, int len,
  194.                    struct raw_breakpoint *bp)
  195. {
  196.   int bp;
  197.   unsigned long bp_ctrl;
  198.   unsigned long start, end;
  199.   struct regcache *regcache;
  200.   unsigned long bp_d_regs[12];

  201.   regcache = get_thread_regcache (current_thread, 1);

  202.   /* Read watchpoints are set as access watchpoints, because of GDB's
  203.      inability to deal with pure read watchpoints.  */
  204.   if (type == raw_bkpt_type_read_wp)
  205.     type = raw_bkpt_type_access_wp;

  206.   /* Get the configuration register.  */
  207.   collect_register_by_name (regcache, "s0", &bp_ctrl);

  208.   /* Try to find a watchpoint that is configured for the
  209.      specified range, then check that read/write also matches.  */

  210.   /* Ugly pointer arithmetic, since I cannot rely on a
  211.      single switch (addr) as there may be several watchpoints with
  212.      the same start address for example.  */

  213.   /* Get all range registers to simplify search.  */
  214.   collect_register_by_name (regcache, "s3", &bp_d_regs[0]);
  215.   collect_register_by_name (regcache, "s4", &bp_d_regs[1]);
  216.   collect_register_by_name (regcache, "s5", &bp_d_regs[2]);
  217.   collect_register_by_name (regcache, "s6", &bp_d_regs[3]);
  218.   collect_register_by_name (regcache, "s7", &bp_d_regs[4]);
  219.   collect_register_by_name (regcache, "s8", &bp_d_regs[5]);
  220.   collect_register_by_name (regcache, "s9", &bp_d_regs[6]);
  221.   collect_register_by_name (regcache, "s10", &bp_d_regs[7]);
  222.   collect_register_by_name (regcache, "s11", &bp_d_regs[8]);
  223.   collect_register_by_name (regcache, "s12", &bp_d_regs[9]);
  224.   collect_register_by_name (regcache, "s13", &bp_d_regs[10]);
  225.   collect_register_by_name (regcache, "s14", &bp_d_regs[11]);

  226.   for (bp = 0; bp < 6; bp++)
  227.     {
  228.       if (bp_d_regs[bp * 2] == addr
  229.           && bp_d_regs[bp * 2 + 1] == (addr + len - 1)) {
  230.         /* Matching range.  */
  231.         int bitpos = 2 + bp * 4;
  232.         int rw_bits;

  233.         /* Read/write bits for this BP.  */
  234.         rw_bits = (bp_ctrl & (0x3 << bitpos)) >> bitpos;

  235.         if ((type == raw_bkpt_type_read_wp && rw_bits == 0x1)
  236.             || (type == raw_bkpt_type_write_wp && rw_bits == 0x2)
  237.             || (type == raw_bkpt_type_access_wp && rw_bits == 0x3))
  238.           {
  239.             /* Read/write matched.  */
  240.             break;
  241.           }
  242.       }
  243.     }

  244.   if (bp > 5)
  245.     {
  246.       /* No watchpoint matched.  */
  247.       return -1;
  248.     }

  249.   /* Found a matching watchpoint.  Now, deconfigure it by
  250.      both disabling read/write in bp_ctrl and zeroing its
  251.      start/end addresses.  */
  252.   bp_ctrl &= ~(3 << (2 + (bp * 4)));
  253.   /* Setup the configuration register.  */
  254.   supply_register_by_name (regcache, "s0", &bp_ctrl);

  255.   start = end = 0;
  256.   /* Configure the watchpoint register.  */
  257.   cris_write_data_breakpoint (regcache, bp, start, end);

  258.   /* Note that we don't clear the S1 flag here.  It's done when continuing.  */
  259.   return 0;
  260. }

  261. static int
  262. cris_stopped_by_watchpoint (void)
  263. {
  264.   unsigned long exs;
  265.   struct regcache *regcache = get_thread_regcache (current_thread, 1);

  266.   collect_register_by_name (regcache, "exs", &exs);

  267.   return (((exs & 0xff00) >> 8) == 0xc);
  268. }

  269. static CORE_ADDR
  270. cris_stopped_data_address (void)
  271. {
  272.   unsigned long eda;
  273.   struct regcache *regcache = get_thread_regcache (current_thread, 1);

  274.   collect_register_by_name (regcache, "eda", &eda);

  275.   /* FIXME: Possibly adjust to match watched range.  */
  276.   return eda;
  277. }

  278. ps_err_e
  279. ps_get_thread_area (const struct ps_prochandle *ph,
  280.                     lwpid_t lwpid, int idx, void **base)
  281. {
  282.   if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  283.     return PS_ERR;

  284.   /* IDX is the bias from the thread pointer to the beginning of the
  285.      thread descriptor.  It has to be subtracted due to implementation
  286.      quirks in libthread_db.  */
  287.   *base = (void *) ((char *) *base - idx);
  288.   return PS_OK;
  289. }

  290. static void
  291. cris_fill_gregset (struct regcache *regcache, void *buf)
  292. {
  293.   int i;

  294.   for (i = 0; i < cris_num_regs; i++)
  295.     {
  296.       if (cris_regmap[i] != -1)
  297.         collect_register (regcache, i, ((char *) buf) + cris_regmap[i]);
  298.     }
  299. }

  300. static void
  301. cris_store_gregset (struct regcache *regcache, const void *buf)
  302. {
  303.   int i;

  304.   for (i = 0; i < cris_num_regs; i++)
  305.     {
  306.       if (cris_regmap[i] != -1)
  307.         supply_register (regcache, i, ((char *) buf) + cris_regmap[i]);
  308.     }
  309. }

  310. static void
  311. cris_arch_setup (void)
  312. {
  313.   current_process ()->tdesc = tdesc_crisv32;
  314. }

  315. static struct regset_info cris_regsets[] = {
  316.   { PTRACE_GETREGS, PTRACE_SETREGS, 0, cris_num_regs * 4,
  317.     GENERAL_REGS, cris_fill_gregset, cris_store_gregset },
  318.   { 0, 0, 0, -1, -1, NULL, NULL }
  319. };


  320. static struct regsets_info cris_regsets_info =
  321.   {
  322.     cris_regsets, /* regsets */
  323.     0, /* num_regsets */
  324.     NULL, /* disabled_regsets */
  325.   };

  326. static struct usrregs_info cris_usrregs_info =
  327.   {
  328.     cris_num_regs,
  329.     cris_regmap,
  330.   };

  331. static struct regs_info regs_info =
  332.   {
  333.     NULL, /* regset_bitmap */
  334.     &cris_usrregs_info,
  335.     &cris_regsets_info
  336.   };

  337. static const struct regs_info *
  338. cris_regs_info (void)
  339. {
  340.   return &regs_info;
  341. }

  342. struct linux_target_ops the_low_target = {
  343.   cris_arch_setup,
  344.   cris_regs_info,
  345.   NULL,
  346.   NULL,
  347.   NULL, /* fetch_register */
  348.   cris_get_pc,
  349.   cris_set_pc,
  350.   (const unsigned char *) &cris_breakpoint,
  351.   cris_breakpoint_len,
  352.   cris_reinsert_addr,
  353.   0,
  354.   cris_breakpoint_at,
  355.   cris_supports_z_point_type,
  356.   cris_insert_point,
  357.   cris_remove_point,
  358.   cris_stopped_by_watchpoint,
  359.   cris_stopped_data_address,
  360. };

  361. void
  362. initialize_low_arch (void)
  363. {
  364.   init_registers_crisv32 ();

  365.   initialize_regsets_info (&cris_regsets_info);
  366. }