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

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Low level interface to ptrace, 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. #include "gdb_proc_service.h"

  18. /* The stack pointer is offset from the stack frame by a BIAS of 2047
  19.    (0x7ff) for 64-bit codeBIAS is likely to be defined on SPARC
  20.    hosts, so undefine it first.  */
  21. #undef BIAS
  22. #define BIAS 2047

  23. #ifdef HAVE_SYS_REG_H
  24. #include <sys/reg.h>
  25. #endif

  26. #define INSN_SIZE 4

  27. #define SPARC_R_REGS_NUM 32
  28. #define SPARC_F_REGS_NUM 48
  29. #define SPARC_CONTROL_REGS_NUM 6

  30. #define sparc_num_regs \
  31.   (SPARC_R_REGS_NUM + SPARC_F_REGS_NUM + SPARC_CONTROL_REGS_NUM)

  32. /* Each offset is multiplied by 8, because of the register size.
  33.    These offsets apply to the buffer sent/filled by ptrace.
  34.    Additionally, the array elements order corresponds to the .dat file, and the
  35.    gdb's registers enumeration order.  */

  36. static int sparc_regmap[] = {
  37.   /* These offsets correspond to GET/SETREGSET.  */
  38.         -10*81*82*83*84*85*86*8,         /* g0 .. g7 */
  39.         7*88*89*8, 10*8, 11*8, 12*8, 13*8, 14*8,         /* o0 .. o5, sp, o7 */
  40.         -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,         /* l0 .. l7 */
  41.         -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,         /* i0 .. i5, fp, i7 */

  42.   /* Floating point registers offsets correspond to GET/SETFPREGSET.  */
  43.     0*41*42*43*44*45*46*47*4,           /*  f0 ..  f7 */
  44.     8*49*4, 10*4, 11*4, 12*4, 13*4, 14*4, 15*4,           /*  f8 .. f15 */
  45.    16*4, 17*4, 18*4, 19*4, 20*4, 21*4, 22*4, 23*4,           /* f16 .. f23 */
  46.    24*4, 25*4, 26*4, 27*4, 28*4, 29*4, 30*4, 31*4,           /* f24 .. f31 */

  47.   /* F32 offset starts next to f31: 31*4+4 = 16 * 8.  */
  48.    16*8, 17*8, 18*8, 19*8, 20*8, 21*8, 22*8, 23*8,           /* f32 .. f46 */
  49.    24*8, 25*8, 26*8, 27*8, 28*8, 29*8, 30*8, 31*8,           /* f48 .. f62 */

  50.    17 *8, /*    pc */
  51.    18 *8, /*   npc */
  52.    16 *8, /* state */
  53.   /* FSR offset also corresponds to GET/SETFPREGSET, ans is placed
  54.      next to f62.  */
  55.    32 *8, /*   fsr */
  56.       -1, /*  fprs */
  57.   /* Y register is 32-bits length, but gdb takes care of that.  */
  58.    19 *8, /*     y */

  59. };


  60. struct regs_range_t
  61. {
  62.   int regno_start;
  63.   int regno_end;
  64. };

  65. static const struct regs_range_t gregs_ranges[] = {
  66. 0, 31 }, /*   g0 .. i7  */
  67. { 80, 82 }, /*   pc .. state */
  68. { 84, 85/* fprs .. y */
  69. };

  70. #define N_GREGS_RANGES (sizeof (gregs_ranges) / sizeof (struct regs_range_t))

  71. static const struct regs_range_t fpregs_ranges[] = {
  72. { 32, 79 }, /* f0 .. f62  */
  73. { 83, 83/* fsr */
  74. };

  75. #define N_FPREGS_RANGES (sizeof (fpregs_ranges) / sizeof (struct regs_range_t))

  76. /* Defined in auto-generated file reg-sparc64.c.  */
  77. void init_registers_sparc64 (void);
  78. extern const struct target_desc *tdesc_sparc64;

  79. static int
  80. sparc_cannot_store_register (int regno)
  81. {
  82.   return (regno >= sparc_num_regs || sparc_regmap[regno] == -1);
  83. }

  84. static int
  85. sparc_cannot_fetch_register (int regno)
  86. {
  87.   return (regno >= sparc_num_regs || sparc_regmap[regno] == -1);
  88. }

  89. static void
  90. sparc_fill_gregset_to_stack (struct regcache *regcache, const void *buf)
  91. {
  92.   int i;
  93.   CORE_ADDR addr = 0;
  94.   unsigned char tmp_reg_buf[8];
  95.   const int l0_regno = find_regno (regcache->tdesc, "l0");
  96.   const int i7_regno = l0_regno + 15;

  97.   /* These registers have to be stored in the stack.  */
  98.   memcpy (&addr,
  99.           ((char *) buf) + sparc_regmap[find_regno (regcache->tdesc, "sp")],
  100.           sizeof (addr));

  101.   addr += BIAS;

  102.   for (i = l0_regno; i <= i7_regno; i++)
  103.     {
  104.       collect_register (regcache, i, tmp_reg_buf);
  105.       (*the_target->write_memory) (addr, tmp_reg_buf, sizeof (tmp_reg_buf));
  106.       addr += sizeof (tmp_reg_buf);
  107.     }
  108. }

  109. static void
  110. sparc_fill_gregset (struct regcache *regcache, void *buf)
  111. {
  112.   int i;
  113.   int range;

  114.   for (range = 0; range < N_GREGS_RANGES; range++)
  115.     for (i = gregs_ranges[range].regno_start;
  116.          i <= gregs_ranges[range].regno_end; i++)
  117.       if (sparc_regmap[i] != -1)
  118.         collect_register (regcache, i, ((char *) buf) + sparc_regmap[i]);

  119.   sparc_fill_gregset_to_stack (regcache, buf);
  120. }

  121. static void
  122. sparc_fill_fpregset (struct regcache *regcache, void *buf)
  123. {
  124.   int i;
  125.   int range;

  126.   for (range = 0; range < N_FPREGS_RANGES; range++)
  127.     for (i = fpregs_ranges[range].regno_start;
  128.          i <= fpregs_ranges[range].regno_end; i++)
  129.       collect_register (regcache, i, ((char *) buf) + sparc_regmap[i]);

  130. }

  131. static void
  132. sparc_store_gregset_from_stack (struct regcache *regcache, const void *buf)
  133. {
  134.   int i;
  135.   CORE_ADDR addr = 0;
  136.   unsigned char tmp_reg_buf[8];
  137.   const int l0_regno = find_regno (regcache->tdesc, "l0");
  138.   const int i7_regno = l0_regno + 15;

  139.   /* These registers have to be obtained from the stack.  */
  140.   memcpy (&addr,
  141.           ((char *) buf) + sparc_regmap[find_regno (regcache->tdesc, "sp")],
  142.           sizeof (addr));

  143.   addr += BIAS;

  144.   for (i = l0_regno; i <= i7_regno; i++)
  145.     {
  146.       (*the_target->read_memory) (addr, tmp_reg_buf, sizeof (tmp_reg_buf));
  147.       supply_register (regcache, i, tmp_reg_buf);
  148.       addr += sizeof (tmp_reg_buf);
  149.     }
  150. }

  151. static void
  152. sparc_store_gregset (struct regcache *regcache, const void *buf)
  153. {
  154.   int i;
  155.   char zerobuf[8];
  156.   int range;

  157.   memset (zerobuf, 0, sizeof (zerobuf));

  158.   for (range = 0; range < N_GREGS_RANGES; range++)
  159.     for (i = gregs_ranges[range].regno_start;
  160.          i <= gregs_ranges[range].regno_end; i++)
  161.       if (sparc_regmap[i] != -1)
  162.         supply_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
  163.       else
  164.         supply_register (regcache, i, zerobuf);

  165.   sparc_store_gregset_from_stack (regcache, buf);
  166. }

  167. static void
  168. sparc_store_fpregset (struct regcache *regcache, const void *buf)
  169. {
  170.   int i;
  171.   int range;

  172.   for (range = 0; range < N_FPREGS_RANGES; range++)
  173.     for (i = fpregs_ranges[range].regno_start;
  174.          i <= fpregs_ranges[range].regno_end;
  175.          i++)
  176.       supply_register (regcache, i, ((char *) buf) + sparc_regmap[i]);
  177. }

  178. extern int debug_threads;

  179. static CORE_ADDR
  180. sparc_get_pc (struct regcache *regcache)
  181. {
  182.   unsigned long pc;
  183.   collect_register_by_name (regcache, "pc", &pc);
  184.   if (debug_threads)
  185.     debug_printf ("stop pc is %08lx\n", pc);
  186.   return pc;
  187. }

  188. static const unsigned char sparc_breakpoint[INSN_SIZE] = {
  189.   0x91, 0xd0, 0x20, 0x01
  190. };
  191. #define sparc_breakpoint_len INSN_SIZE


  192. static int
  193. sparc_breakpoint_at (CORE_ADDR where)
  194. {
  195.   unsigned char insn[INSN_SIZE];

  196.   (*the_target->read_memory) (where, (unsigned char *) insn, sizeof (insn));

  197.   if (memcmp (sparc_breakpoint, insn, sizeof (insn)) == 0)
  198.     return 1;

  199.   /* If necessary, recognize more trap instructions here.  GDB only
  200.      uses TRAP Always.  */

  201.   return 0;
  202. }

  203. /* We only place breakpoints in empty marker functions, and thread locking
  204.    is outside of the function.  So rather than importing software single-step,
  205.    we can just run until exit.  */
  206. static CORE_ADDR
  207. sparc_reinsert_addr (void)
  208. {
  209.   struct regcache *regcache = get_thread_regcache (current_thread, 1);
  210.   CORE_ADDR lr;
  211.   /* O7 is the equivalent to the 'lr' of other archs.  */
  212.   collect_register_by_name (regcache, "o7", &lr);
  213.   return lr;
  214. }

  215. static void
  216. sparc_arch_setup (void)
  217. {
  218.   current_process ()->tdesc = tdesc_sparc64;
  219. }

  220. static struct regset_info sparc_regsets[] = {
  221.   { PTRACE_GETREGS, PTRACE_SETREGS, 0, sizeof (elf_gregset_t),
  222.     GENERAL_REGS,
  223.     sparc_fill_gregset, sparc_store_gregset },
  224.   { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, sizeof (fpregset_t),
  225.     FP_REGS,
  226.     sparc_fill_fpregset, sparc_store_fpregset },
  227.   { 0, 0, 0, -1, -1, NULL, NULL }
  228. };

  229. static struct regsets_info sparc_regsets_info =
  230.   {
  231.     sparc_regsets, /* regsets */
  232.     0, /* num_regsets */
  233.     NULL, /* disabled_regsets */
  234.   };

  235. static struct usrregs_info sparc_usrregs_info =
  236.   {
  237.     sparc_num_regs,
  238.     /* No regmap needs to be provided since this impl. doesn't use
  239.        USRREGS.  */
  240.     NULL
  241.   };

  242. static struct regs_info regs_info =
  243.   {
  244.     NULL, /* regset_bitmap */
  245.     &sparc_usrregs_info,
  246.     &sparc_regsets_info
  247.   };

  248. static const struct regs_info *
  249. sparc_regs_info (void)
  250. {
  251.   return &regs_info;
  252. }

  253. struct linux_target_ops the_low_target = {
  254.   sparc_arch_setup,
  255.   sparc_regs_info,
  256.   sparc_cannot_fetch_register,
  257.   sparc_cannot_store_register,
  258.   NULL, /* fetch_register */
  259.   sparc_get_pc,
  260.   /* No sparc_set_pc is needed.  */
  261.   NULL,
  262.   (const unsigned char *) sparc_breakpoint,
  263.   sparc_breakpoint_len,
  264.   sparc_reinsert_addr,
  265.   0,
  266.   sparc_breakpoint_at,
  267.   NULL/* supports_z_point_type */
  268.   NULL, NULL, NULL, NULL,
  269.   NULL, NULL
  270. };

  271. void
  272. initialize_low_arch (void)
  273. {
  274.   /* Initialize the Linux target descriptions.  */
  275.   init_registers_sparc64 ();

  276.   initialize_regsets_info (&sparc_regsets_info);
  277. }