linux-or1k-low.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* GNU/Linux/OR1K specific low level interface for the GDB server.
  2. Copyright (C) 2021-2022 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 "elf/common.h"
  17. #include "nat/gdb_ptrace.h"
  18. #include <endian.h>
  19. #include "gdb_proc_service.h"
  20. #include <asm/ptrace.h>
  21. #ifndef PTRACE_GET_THREAD_AREA
  22. #define PTRACE_GET_THREAD_AREA 25
  23. #endif
  24. /* Linux target op definitions for the OpenRISC architecture. */
  25. class or1k_target : public linux_process_target
  26. {
  27. public:
  28. const regs_info *get_regs_info () override;
  29. const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
  30. protected:
  31. void low_arch_setup () override;
  32. bool low_cannot_fetch_register (int regno) override;
  33. bool low_cannot_store_register (int regno) override;
  34. bool low_supports_breakpoints () override;
  35. CORE_ADDR low_get_pc (regcache *regcache) override;
  36. void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
  37. bool low_breakpoint_at (CORE_ADDR pc) override;
  38. };
  39. /* The singleton target ops object. */
  40. static or1k_target the_or1k_target;
  41. bool
  42. or1k_target::low_supports_breakpoints ()
  43. {
  44. return true;
  45. }
  46. CORE_ADDR
  47. or1k_target::low_get_pc (regcache *regcache)
  48. {
  49. return linux_get_pc_32bit (regcache);
  50. }
  51. void
  52. or1k_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
  53. {
  54. linux_set_pc_32bit (regcache, pc);
  55. }
  56. /* The following definition must agree with the number of registers
  57. defined in "struct user_regs" in GLIBC
  58. (sysdeps/unix/sysv/linux/or1k/sys/ucontext.h), and also with
  59. OR1K_NUM_REGS in GDB proper. */
  60. #define or1k_num_regs 35
  61. /* Defined in auto-generated file or1k-linux.c. */
  62. void init_registers_or1k_linux (void);
  63. extern const struct target_desc *tdesc_or1k_linux;
  64. /* This union is used to convert between int and byte buffer
  65. representations of register contents. */
  66. union or1k_register
  67. {
  68. unsigned char buf[4];
  69. int reg32;
  70. };
  71. /* Return the ptrace ``address'' of register REGNO. */
  72. static int or1k_regmap[] = {
  73. -1, 1, 2, 3, 4, 5, 6, 7,
  74. 8, 9, 10, 11, 12, 13, 14, 15,
  75. 16, 17, 18, 19, 20, 21, 22, 23,
  76. 24, 25, 26, 27, 28, 29, 30, 31,
  77. -1, /* PC */
  78. -1, /* ORIGINAL R11 */
  79. -1 /* SYSCALL NO */
  80. };
  81. /* Implement the low_arch_setup linux target ops method. */
  82. void
  83. or1k_target::low_arch_setup ()
  84. {
  85. current_process ()->tdesc = tdesc_or1k_linux;
  86. }
  87. /* Implement the low_cannot_fetch_register linux target ops method. */
  88. bool
  89. or1k_target::low_cannot_fetch_register (int regno)
  90. {
  91. return (or1k_regmap[regno] == -1);
  92. }
  93. /* Implement the low_cannot_store_register linux target ops method. */
  94. bool
  95. or1k_target::low_cannot_store_register (int regno)
  96. {
  97. return (or1k_regmap[regno] == -1);
  98. }
  99. /* Breakpoint support. */
  100. static const unsigned int or1k_breakpoint = 0x21000001;
  101. #define or1k_breakpoint_len 4
  102. /* Implementation of target ops method "sw_breakpoint_from_kind". */
  103. const gdb_byte *
  104. or1k_target::sw_breakpoint_from_kind (int kind, int *size)
  105. {
  106. *size = or1k_breakpoint_len;
  107. return (const gdb_byte *) &or1k_breakpoint;
  108. }
  109. /* Implement the low_breakpoint_at linux target ops method. */
  110. bool
  111. or1k_target::low_breakpoint_at (CORE_ADDR where)
  112. {
  113. unsigned int insn;
  114. read_memory (where, (unsigned char *) &insn, or1k_breakpoint_len);
  115. if (insn == or1k_breakpoint)
  116. return true;
  117. return false;
  118. }
  119. /* Fetch the thread-local storage pointer for libthread_db. */
  120. ps_err_e
  121. ps_get_thread_area (struct ps_prochandle *ph,
  122. lwpid_t lwpid, int idx, void **base)
  123. {
  124. if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  125. return PS_ERR;
  126. /* IDX is the bias from the thread pointer to the beginning of the
  127. thread descriptor. It has to be subtracted due to implementation
  128. quirks in libthread_db. */
  129. *base = (void *) ((char *) *base - idx);
  130. return PS_OK;
  131. }
  132. /* Helper functions to collect/supply a single register REGNO. */
  133. static void
  134. or1k_collect_register (struct regcache *regcache, int regno,
  135. union or1k_register *reg)
  136. {
  137. union or1k_register tmp_reg;
  138. collect_register (regcache, regno, &tmp_reg.reg32);
  139. reg->reg32 = tmp_reg.reg32;
  140. }
  141. static void
  142. or1k_supply_register (struct regcache *regcache, int regno,
  143. const union or1k_register *reg)
  144. {
  145. supply_register (regcache, regno, reg->buf);
  146. }
  147. /* We have only a single register set on OpenRISC. */
  148. static void
  149. or1k_fill_gregset (struct regcache *regcache, void *buf)
  150. {
  151. union or1k_register *regset = (union or1k_register *) buf;
  152. int i;
  153. for (i = 1; i < or1k_num_regs; i++)
  154. or1k_collect_register (regcache, i, regset + i);
  155. }
  156. static void
  157. or1k_store_gregset (struct regcache *regcache, const void *buf)
  158. {
  159. const union or1k_register *regset = (union or1k_register *) buf;
  160. int i;
  161. for (i = 0; i < or1k_num_regs; i++)
  162. or1k_supply_register (regcache, i, regset + i);
  163. }
  164. static struct regset_info or1k_regsets[] =
  165. {
  166. { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
  167. or1k_num_regs * 4, GENERAL_REGS,
  168. or1k_fill_gregset, or1k_store_gregset },
  169. NULL_REGSET
  170. };
  171. static struct regsets_info or1k_regsets_info =
  172. {
  173. or1k_regsets, /* regsets */
  174. 0, /* num_regsets */
  175. NULL, /* disabled_regsets */
  176. };
  177. static struct usrregs_info or1k_usrregs_info =
  178. {
  179. or1k_num_regs,
  180. or1k_regmap,
  181. };
  182. static struct regs_info or1k_regs =
  183. {
  184. NULL, /* regset_bitmap */
  185. &or1k_usrregs_info,
  186. &or1k_regsets_info
  187. };
  188. const regs_info *
  189. or1k_target::get_regs_info ()
  190. {
  191. return &or1k_regs;
  192. }
  193. /* The linux target ops object. */
  194. linux_process_target *the_linux_target = &the_or1k_target;
  195. void
  196. initialize_low_arch (void)
  197. {
  198. init_registers_or1k_linux ();
  199. initialize_regsets_info (&or1k_regsets_info);
  200. }