hw_breakpoint.c 9.0 KB

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