bpf-tdep.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* Target-dependent code for BPF.
  2. Copyright (C) 2020-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 "defs.h"
  15. #include "arch-utils.h"
  16. #include "dis-asm.h"
  17. #include "frame.h"
  18. #include "frame-unwind.h"
  19. #include "trad-frame.h"
  20. #include "symtab.h"
  21. #include "value.h"
  22. #include "gdbcmd.h"
  23. #include "breakpoint.h"
  24. #include "inferior.h"
  25. #include "regcache.h"
  26. #include "target.h"
  27. #include "dwarf2/frame.h"
  28. #include "osabi.h"
  29. #include "target-descriptions.h"
  30. #include "remote.h"
  31. #include "gdbarch.h"
  32. /* eBPF registers. */
  33. enum bpf_regnum
  34. {
  35. BPF_R0_REGNUM, /* return value */
  36. BPF_R1_REGNUM,
  37. BPF_R2_REGNUM,
  38. BPF_R3_REGNUM,
  39. BPF_R4_REGNUM,
  40. BPF_R5_REGNUM,
  41. BPF_R6_REGNUM,
  42. BPF_R7_REGNUM,
  43. BPF_R8_REGNUM,
  44. BPF_R9_REGNUM,
  45. BPF_R10_REGNUM, /* sp */
  46. BPF_PC_REGNUM,
  47. };
  48. #define BPF_NUM_REGS (BPF_PC_REGNUM + 1)
  49. /* Target-dependent structure in gdbarch. */
  50. struct bpf_gdbarch_tdep : gdbarch_tdep
  51. {
  52. };
  53. /* Internal debugging facilities. */
  54. /* When this is set to non-zero debugging information will be
  55. printed. */
  56. static unsigned int bpf_debug_flag = 0;
  57. /* The show callback for 'show debug bpf'. */
  58. static void
  59. show_bpf_debug (struct ui_file *file, int from_tty,
  60. struct cmd_list_element *c, const char *value)
  61. {
  62. gdb_printf (file, _("Debugging of BPF is %s.\n"), value);
  63. }
  64. /* BPF registers. */
  65. static const char *bpf_register_names[] =
  66. {
  67. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  68. "r8", "r9", "r10", "pc"
  69. };
  70. /* Return the name of register REGNUM. */
  71. static const char *
  72. bpf_register_name (struct gdbarch *gdbarch, int reg)
  73. {
  74. if (reg >= 0 && reg < BPF_NUM_REGS)
  75. return bpf_register_names[reg];
  76. return NULL;
  77. }
  78. /* Return the GDB type of register REGNUM. */
  79. static struct type *
  80. bpf_register_type (struct gdbarch *gdbarch, int reg)
  81. {
  82. if (reg == BPF_R10_REGNUM)
  83. return builtin_type (gdbarch)->builtin_data_ptr;
  84. else if (reg == BPF_PC_REGNUM)
  85. return builtin_type (gdbarch)->builtin_func_ptr;
  86. return builtin_type (gdbarch)->builtin_int64;
  87. }
  88. /* Return the GDB register number corresponding to DWARF's REG. */
  89. static int
  90. bpf_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  91. {
  92. if (reg >= 0 && reg < BPF_NUM_REGS)
  93. return reg;
  94. return -1;
  95. }
  96. /* Implement the "print_insn" gdbarch method. */
  97. static int
  98. bpf_gdb_print_insn (bfd_vma memaddr, disassemble_info *info)
  99. {
  100. info->symbols = NULL;
  101. return default_print_insn (memaddr, info);
  102. }
  103. /* Return PC of first real instruction of the function starting at
  104. START_PC. */
  105. static CORE_ADDR
  106. bpf_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  107. {
  108. gdb_printf (gdb_stdlog,
  109. "Skipping prologue: start_pc=%s\n",
  110. paddress (gdbarch, start_pc));
  111. /* XXX: to be completed. */
  112. return start_pc + 0;
  113. }
  114. /* Frame unwinder.
  115. XXX it is not clear how to unwind in eBPF, since the stack is not
  116. guaranteed to be contiguous, and therefore no relative stack
  117. addressing can be done in the callee in order to access the
  118. caller's stack frame. To explore with xBPF, which will relax this
  119. restriction. */
  120. /* Given THIS_FRAME, return its ID. */
  121. static void
  122. bpf_frame_this_id (struct frame_info *this_frame,
  123. void **this_prologue_cache,
  124. struct frame_id *this_id)
  125. {
  126. /* Note that THIS_ID defaults to the outermost frame if we don't set
  127. anything here. See frame.c:compute_frame_id. */
  128. }
  129. /* Return the reason why we can't unwind past THIS_FRAME. */
  130. static enum unwind_stop_reason
  131. bpf_frame_unwind_stop_reason (struct frame_info *this_frame,
  132. void **this_cache)
  133. {
  134. return UNWIND_OUTERMOST;
  135. }
  136. /* Ask THIS_FRAME to unwind its register. */
  137. static struct value *
  138. bpf_frame_prev_register (struct frame_info *this_frame,
  139. void **this_prologue_cache, int regnum)
  140. {
  141. return frame_unwind_got_register (this_frame, regnum, regnum);
  142. }
  143. /* Frame unwinder machinery for BPF. */
  144. static const struct frame_unwind bpf_frame_unwind =
  145. {
  146. "bpf prologue",
  147. NORMAL_FRAME,
  148. bpf_frame_unwind_stop_reason,
  149. bpf_frame_this_id,
  150. bpf_frame_prev_register,
  151. NULL,
  152. default_frame_sniffer
  153. };
  154. /* Breakpoints. */
  155. /* Enum describing the different kinds of breakpoints. We currently
  156. just support one, implemented by the brkpt xbpf instruction. */
  157. enum bpf_breakpoint_kinds
  158. {
  159. BPF_BP_KIND_BRKPT = 0,
  160. };
  161. /* Implement the breakpoint_kind_from_pc gdbarch method. */
  162. static int
  163. bpf_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *start_pc)
  164. {
  165. /* We support just one kind of breakpoint. */
  166. return BPF_BP_KIND_BRKPT;
  167. }
  168. /* Implement the sw_breakpoint_from_kind gdbarch method. */
  169. static const gdb_byte *
  170. bpf_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
  171. {
  172. static unsigned char brkpt_insn[]
  173. = {0x8c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  174. switch (kind)
  175. {
  176. case BPF_BP_KIND_BRKPT:
  177. *size = 8;
  178. return brkpt_insn;
  179. default:
  180. gdb_assert_not_reached ("unexpected BPF breakpoint kind");
  181. }
  182. }
  183. /* Assuming THIS_FRAME is a dummy frame, return its frame ID. */
  184. static struct frame_id
  185. bpf_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  186. {
  187. CORE_ADDR sp = get_frame_register_unsigned (this_frame,
  188. gdbarch_sp_regnum (gdbarch));
  189. return frame_id_build (sp, get_frame_pc (this_frame));
  190. }
  191. /* Implement the push dummy call gdbarch callback. */
  192. static CORE_ADDR
  193. bpf_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  194. struct regcache *regcache, CORE_ADDR bp_addr,
  195. int nargs, struct value **args, CORE_ADDR sp,
  196. function_call_return_method return_method,
  197. CORE_ADDR struct_addr)
  198. {
  199. gdb_printf (gdb_stdlog, "Pushing dummy call: sp=%s\n",
  200. paddress (gdbarch, sp));
  201. /* XXX writeme */
  202. return sp;
  203. }
  204. /* Extract a function return value of TYPE from REGCACHE,
  205. and copy it into VALBUF. */
  206. static void
  207. bpf_extract_return_value (struct type *type, struct regcache *regcache,
  208. gdb_byte *valbuf)
  209. {
  210. int len = TYPE_LENGTH (type);
  211. gdb_byte vbuf[8];
  212. gdb_assert (len <= 8);
  213. regcache->cooked_read (BPF_R0_REGNUM, vbuf);
  214. memcpy (valbuf, vbuf + 8 - len, len);
  215. }
  216. /* Store the function return value of type TYPE from VALBUF into REGNAME. */
  217. static void
  218. bpf_store_return_value (struct type *type, struct regcache *regcache,
  219. const gdb_byte *valbuf)
  220. {
  221. int len = TYPE_LENGTH (type);
  222. gdb_byte vbuf[8];
  223. gdb_assert (len <= 8);
  224. memset (vbuf, 0, sizeof (vbuf));
  225. memcpy (vbuf + 8 - len, valbuf, len);
  226. regcache->cooked_write (BPF_R0_REGNUM, vbuf);
  227. }
  228. /* Handle function's return value. */
  229. static enum return_value_convention
  230. bpf_return_value (struct gdbarch *gdbarch, struct value *function,
  231. struct type *type, struct regcache *regcache,
  232. gdb_byte *readbuf, const gdb_byte *writebuf)
  233. {
  234. int len = TYPE_LENGTH (type);
  235. if (len > 8)
  236. return RETURN_VALUE_STRUCT_CONVENTION;
  237. if (readbuf != NULL)
  238. bpf_extract_return_value (type, regcache, readbuf);
  239. if (writebuf != NULL)
  240. bpf_store_return_value (type, regcache, writebuf);
  241. return RETURN_VALUE_REGISTER_CONVENTION;
  242. }
  243. /* Initialize the current architecture based on INFO. If possible, re-use an
  244. architecture from ARCHES, which is a list of architectures already created
  245. during this debugging session. */
  246. static struct gdbarch *
  247. bpf_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  248. {
  249. /* If there is already a candidate, use it. */
  250. arches = gdbarch_list_lookup_by_info (arches, &info);
  251. if (arches != NULL)
  252. return arches->gdbarch;
  253. /* Allocate space for the new architecture. */
  254. bpf_gdbarch_tdep *tdep = new bpf_gdbarch_tdep;
  255. struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep);
  256. /* Information about registers, etc. */
  257. set_gdbarch_num_regs (gdbarch, BPF_NUM_REGS);
  258. set_gdbarch_register_name (gdbarch, bpf_register_name);
  259. set_gdbarch_register_type (gdbarch, bpf_register_type);
  260. /* Register numbers of various important registers. */
  261. set_gdbarch_sp_regnum (gdbarch, BPF_R10_REGNUM);
  262. set_gdbarch_pc_regnum (gdbarch, BPF_PC_REGNUM);
  263. /* Map DWARF2 registers to GDB registers. */
  264. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, bpf_dwarf2_reg_to_regnum);
  265. /* Call dummy code. */
  266. set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
  267. set_gdbarch_dummy_id (gdbarch, bpf_dummy_id);
  268. set_gdbarch_push_dummy_call (gdbarch, bpf_push_dummy_call);
  269. /* Returning results. */
  270. set_gdbarch_return_value (gdbarch, bpf_return_value);
  271. /* Advance PC across function entry code. */
  272. set_gdbarch_skip_prologue (gdbarch, bpf_skip_prologue);
  273. /* Stack grows downward. */
  274. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  275. /* Breakpoint manipulation. */
  276. set_gdbarch_breakpoint_kind_from_pc (gdbarch, bpf_breakpoint_kind_from_pc);
  277. set_gdbarch_sw_breakpoint_from_kind (gdbarch, bpf_sw_breakpoint_from_kind);
  278. /* Frame handling. */
  279. set_gdbarch_frame_args_skip (gdbarch, 8);
  280. /* Disassembly. */
  281. set_gdbarch_print_insn (gdbarch, bpf_gdb_print_insn);
  282. /* Hook in ABI-specific overrides, if they have been registered. */
  283. gdbarch_init_osabi (info, gdbarch);
  284. /* Install unwinders. */
  285. frame_unwind_append_unwinder (gdbarch, &bpf_frame_unwind);
  286. return gdbarch;
  287. }
  288. void _initialize_bpf_tdep ();
  289. void
  290. _initialize_bpf_tdep ()
  291. {
  292. register_gdbarch_init (bfd_arch_bpf, bpf_gdbarch_init);
  293. /* Add commands 'set/show debug bpf'. */
  294. add_setshow_zuinteger_cmd ("bpf", class_maintenance,
  295. &bpf_debug_flag,
  296. _("Set BPF debugging."),
  297. _("Show BPF debugging."),
  298. _("Enables BPF specific debugging output."),
  299. NULL,
  300. &show_bpf_debug,
  301. &setdebuglist, &showdebuglist);
  302. }