hw_breakpoint.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  3. * using the CPU's debug registers. Derived from
  4. * "arch/x86/kernel/hw_breakpoint.c"
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. * Copyright 2010 IBM Corporation
  21. * Author: K.Prasad <prasad@linux.vnet.ibm.com>
  22. *
  23. */
  24. #include <linux/hw_breakpoint.h>
  25. #include <linux/notifier.h>
  26. #include <linux/kprobes.h>
  27. #include <linux/percpu.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/smp.h>
  31. #include <asm/hw_breakpoint.h>
  32. #include <asm/processor.h>
  33. #include <asm/sstep.h>
  34. #include <asm/uaccess.h>
  35. /*
  36. * Stores the breakpoints currently in use on each breakpoint address
  37. * register for every cpu
  38. */
  39. static DEFINE_PER_CPU(struct perf_event *, bp_per_reg);
  40. /*
  41. * Returns total number of data or instruction breakpoints available.
  42. */
  43. int hw_breakpoint_slots(int type)
  44. {
  45. if (type == TYPE_DATA)
  46. return HBP_NUM;
  47. return 0; /* no instruction breakpoints available */
  48. }
  49. /*
  50. * Install a perf counter breakpoint.
  51. *
  52. * We seek a free debug address register and use it for this
  53. * breakpoint.
  54. *
  55. * Atomic: we hold the counter->ctx->lock and we only handle variables
  56. * and registers local to this cpu.
  57. */
  58. int arch_install_hw_breakpoint(struct perf_event *bp)
  59. {
  60. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  61. struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
  62. *slot = bp;
  63. /*
  64. * Do not install DABR values if the instruction must be single-stepped.
  65. * If so, DABR will be populated in single_step_dabr_instruction().
  66. */
  67. if (current->thread.last_hit_ubp != bp)
  68. __set_breakpoint(info);
  69. return 0;
  70. }
  71. /*
  72. * Uninstall the breakpoint contained in the given counter.
  73. *
  74. * First we search the debug address register it uses and then we disable
  75. * it.
  76. *
  77. * Atomic: we hold the counter->ctx->lock and we only handle variables
  78. * and registers local to this cpu.
  79. */
  80. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  81. {
  82. struct perf_event **slot = this_cpu_ptr(&bp_per_reg);
  83. if (*slot != bp) {
  84. WARN_ONCE(1, "Can't find the breakpoint");
  85. return;
  86. }
  87. *slot = NULL;
  88. hw_breakpoint_disable();
  89. }
  90. /*
  91. * Perform cleanup of arch-specific counters during unregistration
  92. * of the perf-event
  93. */
  94. void arch_unregister_hw_breakpoint(struct perf_event *bp)
  95. {
  96. /*
  97. * If the breakpoint is unregistered between a hw_breakpoint_handler()
  98. * and the single_step_dabr_instruction(), then cleanup the breakpoint
  99. * restoration variables to prevent dangling pointers.
  100. */
  101. if (bp->ctx && bp->ctx->task)
  102. bp->ctx->task->thread.last_hit_ubp = NULL;
  103. }
  104. /*
  105. * Check for virtual address in kernel space.
  106. */
  107. int arch_check_bp_in_kernelspace(struct perf_event *bp)
  108. {
  109. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  110. return is_kernel_addr(info->address);
  111. }
  112. int arch_bp_generic_fields(int type, int *gen_bp_type)
  113. {
  114. *gen_bp_type = 0;
  115. if (type & HW_BRK_TYPE_READ)
  116. *gen_bp_type |= HW_BREAKPOINT_R;
  117. if (type & HW_BRK_TYPE_WRITE)
  118. *gen_bp_type |= HW_BREAKPOINT_W;
  119. if (*gen_bp_type == 0)
  120. return -EINVAL;
  121. return 0;
  122. }
  123. /*
  124. * Validate the arch-specific HW Breakpoint register settings
  125. */
  126. int arch_validate_hwbkpt_settings(struct perf_event *bp)
  127. {
  128. int ret = -EINVAL, length_max;
  129. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  130. if (!bp)
  131. return ret;
  132. info->type = HW_BRK_TYPE_TRANSLATE;
  133. if (bp->attr.bp_type & HW_BREAKPOINT_R)
  134. info->type |= HW_BRK_TYPE_READ;
  135. if (bp->attr.bp_type & HW_BREAKPOINT_W)
  136. info->type |= HW_BRK_TYPE_WRITE;
  137. if (info->type == HW_BRK_TYPE_TRANSLATE)
  138. /* must set alteast read or write */
  139. return ret;
  140. if (!(bp->attr.exclude_user))
  141. info->type |= HW_BRK_TYPE_USER;
  142. if (!(bp->attr.exclude_kernel))
  143. info->type |= HW_BRK_TYPE_KERNEL;
  144. if (!(bp->attr.exclude_hv))
  145. info->type |= HW_BRK_TYPE_HYP;
  146. info->address = bp->attr.bp_addr;
  147. info->len = bp->attr.bp_len;
  148. /*
  149. * Since breakpoint length can be a maximum of HW_BREAKPOINT_LEN(8)
  150. * and breakpoint addresses are aligned to nearest double-word
  151. * HW_BREAKPOINT_ALIGN by rounding off to the lower address, the
  152. * 'symbolsize' should satisfy the check below.
  153. */
  154. length_max = 8; /* DABR */
  155. if (cpu_has_feature(CPU_FTR_DAWR)) {
  156. length_max = 512 ; /* 64 doublewords */
  157. /* DAWR region can't cross 512 boundary */
  158. if ((bp->attr.bp_addr >> 9) !=
  159. ((bp->attr.bp_addr + bp->attr.bp_len - 1) >> 9))
  160. return -EINVAL;
  161. }
  162. if (info->len >
  163. (length_max - (info->address & HW_BREAKPOINT_ALIGN)))
  164. return -EINVAL;
  165. return 0;
  166. }
  167. /*
  168. * Restores the breakpoint on the debug registers.
  169. * Invoke this function if it is known that the execution context is
  170. * about to change to cause loss of MSR_SE settings.
  171. */
  172. void thread_change_pc(struct task_struct *tsk, struct pt_regs *regs)
  173. {
  174. struct arch_hw_breakpoint *info;
  175. if (likely(!tsk->thread.last_hit_ubp))
  176. return;
  177. info = counter_arch_bp(tsk->thread.last_hit_ubp);
  178. regs->msr &= ~MSR_SE;
  179. __set_breakpoint(info);
  180. tsk->thread.last_hit_ubp = NULL;
  181. }
  182. /*
  183. * Handle debug exception notifications.
  184. */
  185. int __kprobes hw_breakpoint_handler(struct die_args *args)
  186. {
  187. int rc = NOTIFY_STOP;
  188. struct perf_event *bp;
  189. struct pt_regs *regs = args->regs;
  190. int stepped = 1;
  191. struct arch_hw_breakpoint *info;
  192. unsigned int instr;
  193. unsigned long dar = regs->dar;
  194. /* Disable breakpoints during exception handling */
  195. hw_breakpoint_disable();
  196. /*
  197. * The counter may be concurrently released but that can only
  198. * occur from a call_rcu() path. We can then safely fetch
  199. * the breakpoint, use its callback, touch its counter
  200. * while we are in an rcu_read_lock() path.
  201. */
  202. rcu_read_lock();
  203. bp = __this_cpu_read(bp_per_reg);
  204. if (!bp) {
  205. rc = NOTIFY_DONE;
  206. goto out;
  207. }
  208. info = counter_arch_bp(bp);
  209. /*
  210. * Return early after invoking user-callback function without restoring
  211. * DABR if the breakpoint is from ptrace which always operates in
  212. * one-shot mode. The ptrace-ed process will receive the SIGTRAP signal
  213. * generated in do_dabr().
  214. */
  215. if (bp->overflow_handler == ptrace_triggered) {
  216. perf_bp_event(bp, regs);
  217. rc = NOTIFY_DONE;
  218. goto out;
  219. }
  220. /*
  221. * Verify if dar lies within the address range occupied by the symbol
  222. * being watched to filter extraneous exceptions. If it doesn't,
  223. * we still need to single-step the instruction, but we don't
  224. * generate an event.
  225. */
  226. info->type &= ~HW_BRK_TYPE_EXTRANEOUS_IRQ;
  227. if (!((bp->attr.bp_addr <= dar) &&
  228. (dar - bp->attr.bp_addr < bp->attr.bp_len)))
  229. info->type |= HW_BRK_TYPE_EXTRANEOUS_IRQ;
  230. /* Do not emulate user-space instructions, instead single-step them */
  231. if (user_mode(regs)) {
  232. current->thread.last_hit_ubp = bp;
  233. regs->msr |= MSR_SE;
  234. goto out;
  235. }
  236. stepped = 0;
  237. instr = 0;
  238. if (!__get_user_inatomic(instr, (unsigned int *) regs->nip))
  239. stepped = emulate_step(regs, instr);
  240. /*
  241. * emulate_step() could not execute it. We've failed in reliably
  242. * handling the hw-breakpoint. Unregister it and throw a warning
  243. * message to let the user know about it.
  244. */
  245. if (!stepped) {
  246. WARN(1, "Unable to handle hardware breakpoint. Breakpoint at "
  247. "0x%lx will be disabled.", info->address);
  248. perf_event_disable(bp);
  249. goto out;
  250. }
  251. /*
  252. * As a policy, the callback is invoked in a 'trigger-after-execute'
  253. * fashion
  254. */
  255. if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
  256. perf_bp_event(bp, regs);
  257. __set_breakpoint(info);
  258. out:
  259. rcu_read_unlock();
  260. return rc;
  261. }
  262. /*
  263. * Handle single-step exceptions following a DABR hit.
  264. */
  265. static int __kprobes single_step_dabr_instruction(struct die_args *args)
  266. {
  267. struct pt_regs *regs = args->regs;
  268. struct perf_event *bp = NULL;
  269. struct arch_hw_breakpoint *info;
  270. bp = current->thread.last_hit_ubp;
  271. /*
  272. * Check if we are single-stepping as a result of a
  273. * previous HW Breakpoint exception
  274. */
  275. if (!bp)
  276. return NOTIFY_DONE;
  277. info = counter_arch_bp(bp);
  278. /*
  279. * We shall invoke the user-defined callback function in the single
  280. * stepping handler to confirm to 'trigger-after-execute' semantics
  281. */
  282. if (!(info->type & HW_BRK_TYPE_EXTRANEOUS_IRQ))
  283. perf_bp_event(bp, regs);
  284. __set_breakpoint(info);
  285. current->thread.last_hit_ubp = NULL;
  286. /*
  287. * If the process was being single-stepped by ptrace, let the
  288. * other single-step actions occur (e.g. generate SIGTRAP).
  289. */
  290. if (test_thread_flag(TIF_SINGLESTEP))
  291. return NOTIFY_DONE;
  292. return NOTIFY_STOP;
  293. }
  294. /*
  295. * Handle debug exception notifications.
  296. */
  297. int __kprobes hw_breakpoint_exceptions_notify(
  298. struct notifier_block *unused, unsigned long val, void *data)
  299. {
  300. int ret = NOTIFY_DONE;
  301. switch (val) {
  302. case DIE_DABR_MATCH:
  303. ret = hw_breakpoint_handler(data);
  304. break;
  305. case DIE_SSTEP:
  306. ret = single_step_dabr_instruction(data);
  307. break;
  308. }
  309. return ret;
  310. }
  311. /*
  312. * Release the user breakpoints used by ptrace
  313. */
  314. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  315. {
  316. struct thread_struct *t = &tsk->thread;
  317. unregister_hw_breakpoint(t->ptrace_bps[0]);
  318. t->ptrace_bps[0] = NULL;
  319. }
  320. void hw_breakpoint_pmu_read(struct perf_event *bp)
  321. {
  322. /* TODO */
  323. }