aarch64-linux-hw-point.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* Copyright (C) 2009-2022 Free Software Foundation, Inc.
  2. Contributed by ARM Ltd.
  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 "gdbsupport/common-defs.h"
  15. #include "gdbsupport/break-common.h"
  16. #include "gdbsupport/common-regcache.h"
  17. #include "nat/linux-nat.h"
  18. #include "aarch64-linux-hw-point.h"
  19. #include <sys/uio.h>
  20. /* The order in which <sys/ptrace.h> and <asm/ptrace.h> are included
  21. can be important. <sys/ptrace.h> often declares various PTRACE_*
  22. enums. <asm/ptrace.h> often defines preprocessor constants for
  23. these very same symbols. When that's the case, build errors will
  24. result when <asm/ptrace.h> is included before <sys/ptrace.h>. */
  25. #include <sys/ptrace.h>
  26. #include <asm/ptrace.h>
  27. #include <elf.h>
  28. /* See aarch64-linux-hw-point.h */
  29. bool kernel_supports_any_contiguous_range = true;
  30. /* Helper for aarch64_notify_debug_reg_change. Records the
  31. information about the change of one hardware breakpoint/watchpoint
  32. setting for the thread LWP.
  33. N.B. The actual updating of hardware debug registers is not
  34. carried out until the moment the thread is resumed. */
  35. static int
  36. debug_reg_change_callback (struct lwp_info *lwp, int is_watchpoint,
  37. unsigned int idx)
  38. {
  39. int tid = ptid_of_lwp (lwp).lwp ();
  40. struct arch_lwp_info *info = lwp_arch_private_info (lwp);
  41. dr_changed_t *dr_changed_ptr;
  42. dr_changed_t dr_changed;
  43. if (info == NULL)
  44. {
  45. info = XCNEW (struct arch_lwp_info);
  46. lwp_set_arch_private_info (lwp, info);
  47. }
  48. if (show_debug_regs)
  49. {
  50. debug_printf ("debug_reg_change_callback: \n\tOn entry:\n");
  51. debug_printf ("\ttid%d, dr_changed_bp=0x%s, "
  52. "dr_changed_wp=0x%s\n", tid,
  53. phex (info->dr_changed_bp, 8),
  54. phex (info->dr_changed_wp, 8));
  55. }
  56. dr_changed_ptr = is_watchpoint ? &info->dr_changed_wp
  57. : &info->dr_changed_bp;
  58. dr_changed = *dr_changed_ptr;
  59. gdb_assert (idx >= 0
  60. && (idx <= (is_watchpoint ? aarch64_num_wp_regs
  61. : aarch64_num_bp_regs)));
  62. /* The actual update is done later just before resuming the lwp,
  63. we just mark that one register pair needs updating. */
  64. DR_MARK_N_CHANGED (dr_changed, idx);
  65. *dr_changed_ptr = dr_changed;
  66. /* If the lwp isn't stopped, force it to momentarily pause, so
  67. we can update its debug registers. */
  68. if (!lwp_is_stopped (lwp))
  69. linux_stop_lwp (lwp);
  70. if (show_debug_regs)
  71. {
  72. debug_printf ("\tOn exit:\n\ttid%d, dr_changed_bp=0x%s, "
  73. "dr_changed_wp=0x%s\n", tid,
  74. phex (info->dr_changed_bp, 8),
  75. phex (info->dr_changed_wp, 8));
  76. }
  77. return 0;
  78. }
  79. /* Notify each thread that their IDXth breakpoint/watchpoint register
  80. pair needs to be updated. The message will be recorded in each
  81. thread's arch-specific data area, the actual updating will be done
  82. when the thread is resumed. */
  83. void
  84. aarch64_notify_debug_reg_change (ptid_t ptid,
  85. int is_watchpoint, unsigned int idx)
  86. {
  87. ptid_t pid_ptid = ptid_t (ptid.pid ());
  88. iterate_over_lwps (pid_ptid, [=] (struct lwp_info *info)
  89. {
  90. return debug_reg_change_callback (info,
  91. is_watchpoint,
  92. idx);
  93. });
  94. }
  95. /* Reconfigure STATE to be compatible with Linux kernels with the PR
  96. external/20207 bug. This is called when
  97. KERNEL_SUPPORTS_ANY_CONTIGUOUS_RANGE transitions to false. Note we
  98. don't try to support combining watchpoints with matching (and thus
  99. shared) masks, as it's too late when we get here. On buggy
  100. kernels, GDB will try to first setup the perfect matching ranges,
  101. which will run out of registers before this function can merge
  102. them. It doesn't look like worth the effort to improve that, given
  103. eventually buggy kernels will be phased out. */
  104. static void
  105. aarch64_downgrade_regs (struct aarch64_debug_reg_state *state)
  106. {
  107. for (int i = 0; i < aarch64_num_wp_regs; ++i)
  108. if ((state->dr_ctrl_wp[i] & 1) != 0)
  109. {
  110. gdb_assert (state->dr_ref_count_wp[i] != 0);
  111. uint8_t mask_orig = (state->dr_ctrl_wp[i] >> 5) & 0xff;
  112. gdb_assert (mask_orig != 0);
  113. static const uint8_t old_valid[] = { 0x01, 0x03, 0x0f, 0xff };
  114. uint8_t mask = 0;
  115. for (const uint8_t old_mask : old_valid)
  116. if (mask_orig <= old_mask)
  117. {
  118. mask = old_mask;
  119. break;
  120. }
  121. gdb_assert (mask != 0);
  122. /* No update needed for this watchpoint? */
  123. if (mask == mask_orig)
  124. continue;
  125. state->dr_ctrl_wp[i] |= mask << 5;
  126. state->dr_addr_wp[i]
  127. = align_down (state->dr_addr_wp[i], AARCH64_HWP_ALIGNMENT);
  128. /* Try to match duplicate entries. */
  129. for (int j = 0; j < i; ++j)
  130. if ((state->dr_ctrl_wp[j] & 1) != 0
  131. && state->dr_addr_wp[j] == state->dr_addr_wp[i]
  132. && state->dr_addr_orig_wp[j] == state->dr_addr_orig_wp[i]
  133. && state->dr_ctrl_wp[j] == state->dr_ctrl_wp[i])
  134. {
  135. state->dr_ref_count_wp[j] += state->dr_ref_count_wp[i];
  136. state->dr_ref_count_wp[i] = 0;
  137. state->dr_addr_wp[i] = 0;
  138. state->dr_addr_orig_wp[i] = 0;
  139. state->dr_ctrl_wp[i] &= ~1;
  140. break;
  141. }
  142. aarch64_notify_debug_reg_change (current_lwp_ptid (),
  143. 1 /* is_watchpoint */, i);
  144. }
  145. }
  146. /* Call ptrace to set the thread TID's hardware breakpoint/watchpoint
  147. registers with data from *STATE. */
  148. void
  149. aarch64_linux_set_debug_regs (struct aarch64_debug_reg_state *state,
  150. int tid, int watchpoint)
  151. {
  152. int i, count;
  153. struct iovec iov;
  154. struct user_hwdebug_state regs;
  155. const CORE_ADDR *addr;
  156. const unsigned int *ctrl;
  157. memset (&regs, 0, sizeof (regs));
  158. iov.iov_base = &regs;
  159. count = watchpoint ? aarch64_num_wp_regs : aarch64_num_bp_regs;
  160. addr = watchpoint ? state->dr_addr_wp : state->dr_addr_bp;
  161. ctrl = watchpoint ? state->dr_ctrl_wp : state->dr_ctrl_bp;
  162. if (count == 0)
  163. return;
  164. iov.iov_len = (offsetof (struct user_hwdebug_state, dbg_regs)
  165. + count * sizeof (regs.dbg_regs[0]));
  166. for (i = 0; i < count; i++)
  167. {
  168. regs.dbg_regs[i].addr = addr[i];
  169. regs.dbg_regs[i].ctrl = ctrl[i];
  170. }
  171. if (ptrace (PTRACE_SETREGSET, tid,
  172. watchpoint ? NT_ARM_HW_WATCH : NT_ARM_HW_BREAK,
  173. (void *) &iov))
  174. {
  175. /* Handle Linux kernels with the PR external/20207 bug. */
  176. if (watchpoint && errno == EINVAL
  177. && kernel_supports_any_contiguous_range)
  178. {
  179. kernel_supports_any_contiguous_range = false;
  180. aarch64_downgrade_regs (state);
  181. aarch64_linux_set_debug_regs (state, tid, watchpoint);
  182. return;
  183. }
  184. error (_("Unexpected error setting hardware debug registers"));
  185. }
  186. }
  187. /* Return true if debug arch level is compatible for hw watchpoints
  188. and breakpoints. */
  189. static bool
  190. compatible_debug_arch (unsigned int debug_arch)
  191. {
  192. if (debug_arch == AARCH64_DEBUG_ARCH_V8)
  193. return true;
  194. if (debug_arch == AARCH64_DEBUG_ARCH_V8_1)
  195. return true;
  196. if (debug_arch == AARCH64_DEBUG_ARCH_V8_2)
  197. return true;
  198. if (debug_arch == AARCH64_DEBUG_ARCH_V8_4)
  199. return true;
  200. return false;
  201. }
  202. /* Get the hardware debug register capacity information from the
  203. process represented by TID. */
  204. void
  205. aarch64_linux_get_debug_reg_capacity (int tid)
  206. {
  207. struct iovec iov;
  208. struct user_hwdebug_state dreg_state;
  209. iov.iov_base = &dreg_state;
  210. iov.iov_len = sizeof (dreg_state);
  211. /* Get hardware watchpoint register info. */
  212. if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_HW_WATCH, &iov) == 0
  213. && compatible_debug_arch (AARCH64_DEBUG_ARCH (dreg_state.dbg_info)))
  214. {
  215. aarch64_num_wp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info);
  216. if (aarch64_num_wp_regs > AARCH64_HWP_MAX_NUM)
  217. {
  218. warning (_("Unexpected number of hardware watchpoint registers"
  219. " reported by ptrace, got %d, expected %d."),
  220. aarch64_num_wp_regs, AARCH64_HWP_MAX_NUM);
  221. aarch64_num_wp_regs = AARCH64_HWP_MAX_NUM;
  222. }
  223. }
  224. else
  225. {
  226. warning (_("Unable to determine the number of hardware watchpoints"
  227. " available."));
  228. aarch64_num_wp_regs = 0;
  229. }
  230. /* Get hardware breakpoint register info. */
  231. if (ptrace (PTRACE_GETREGSET, tid, NT_ARM_HW_BREAK, &iov) == 0
  232. && compatible_debug_arch (AARCH64_DEBUG_ARCH (dreg_state.dbg_info)))
  233. {
  234. aarch64_num_bp_regs = AARCH64_DEBUG_NUM_SLOTS (dreg_state.dbg_info);
  235. if (aarch64_num_bp_regs > AARCH64_HBP_MAX_NUM)
  236. {
  237. warning (_("Unexpected number of hardware breakpoint registers"
  238. " reported by ptrace, got %d, expected %d."),
  239. aarch64_num_bp_regs, AARCH64_HBP_MAX_NUM);
  240. aarch64_num_bp_regs = AARCH64_HBP_MAX_NUM;
  241. }
  242. }
  243. else
  244. {
  245. warning (_("Unable to determine the number of hardware breakpoints"
  246. " available."));
  247. aarch64_num_bp_regs = 0;
  248. }
  249. }