hw_breakpoint.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * Copyright (C) 2007 Alan Stern
  17. * Copyright (C) IBM Corporation, 2009
  18. * Copyright (C) 2009, Frederic Weisbecker <fweisbec@gmail.com>
  19. */
  20. /*
  21. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  22. * using the CPU's debug registers.
  23. * This file contains the arch-independent routines.
  24. */
  25. #include <linux/irqflags.h>
  26. #include <linux/kallsyms.h>
  27. #include <linux/notifier.h>
  28. #include <linux/kprobes.h>
  29. #include <linux/kdebug.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/percpu.h>
  33. #include <linux/sched.h>
  34. #include <linux/init.h>
  35. #include <linux/smp.h>
  36. #include <linux/hw_breakpoint.h>
  37. #include <asm/processor.h>
  38. #ifdef CONFIG_X86
  39. #include <asm/debugreg.h>
  40. #endif
  41. static atomic_t bp_slot;
  42. int reserve_bp_slot(struct perf_event *bp)
  43. {
  44. if (atomic_inc_return(&bp_slot) == HBP_NUM) {
  45. atomic_dec(&bp_slot);
  46. return -ENOSPC;
  47. }
  48. return 0;
  49. }
  50. void release_bp_slot(struct perf_event *bp)
  51. {
  52. atomic_dec(&bp_slot);
  53. }
  54. int __register_perf_hw_breakpoint(struct perf_event *bp)
  55. {
  56. int ret;
  57. ret = reserve_bp_slot(bp);
  58. if (ret)
  59. return ret;
  60. if (!bp->attr.disabled)
  61. ret = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
  62. return ret;
  63. }
  64. int register_perf_hw_breakpoint(struct perf_event *bp)
  65. {
  66. bp->callback = perf_bp_event;
  67. return __register_perf_hw_breakpoint(bp);
  68. }
  69. /*
  70. * Register a breakpoint bound to a task and a given cpu.
  71. * If cpu is -1, the breakpoint is active for the task in every cpu
  72. * If the task is -1, the breakpoint is active for every tasks in the given
  73. * cpu.
  74. */
  75. static struct perf_event *
  76. register_user_hw_breakpoint_cpu(unsigned long addr,
  77. int len,
  78. int type,
  79. perf_callback_t triggered,
  80. pid_t pid,
  81. int cpu,
  82. bool active)
  83. {
  84. struct perf_event_attr *attr;
  85. struct perf_event *bp;
  86. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  87. if (!attr)
  88. return ERR_PTR(-ENOMEM);
  89. attr->type = PERF_TYPE_BREAKPOINT;
  90. attr->size = sizeof(*attr);
  91. attr->bp_addr = addr;
  92. attr->bp_len = len;
  93. attr->bp_type = type;
  94. /*
  95. * Such breakpoints are used by debuggers to trigger signals when
  96. * we hit the excepted memory op. We can't miss such events, they
  97. * must be pinned.
  98. */
  99. attr->pinned = 1;
  100. if (!active)
  101. attr->disabled = 1;
  102. bp = perf_event_create_kernel_counter(attr, cpu, pid, triggered);
  103. kfree(attr);
  104. return bp;
  105. }
  106. /**
  107. * register_user_hw_breakpoint - register a hardware breakpoint for user space
  108. * @addr: is the memory address that triggers the breakpoint
  109. * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
  110. * @type: the type of the access to the memory (read/write/exec)
  111. * @triggered: callback to trigger when we hit the breakpoint
  112. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  113. * @active: should we activate it while registering it
  114. *
  115. */
  116. struct perf_event *
  117. register_user_hw_breakpoint(unsigned long addr,
  118. int len,
  119. int type,
  120. perf_callback_t triggered,
  121. struct task_struct *tsk,
  122. bool active)
  123. {
  124. return register_user_hw_breakpoint_cpu(addr, len, type, triggered,
  125. tsk->pid, -1, active);
  126. }
  127. EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
  128. /**
  129. * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
  130. * @bp: the breakpoint structure to modify
  131. * @addr: is the memory address that triggers the breakpoint
  132. * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
  133. * @type: the type of the access to the memory (read/write/exec)
  134. * @triggered: callback to trigger when we hit the breakpoint
  135. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  136. * @active: should we activate it while registering it
  137. */
  138. struct perf_event *
  139. modify_user_hw_breakpoint(struct perf_event *bp,
  140. unsigned long addr,
  141. int len,
  142. int type,
  143. perf_callback_t triggered,
  144. struct task_struct *tsk,
  145. bool active)
  146. {
  147. /*
  148. * FIXME: do it without unregistering
  149. * - We don't want to lose our slot
  150. * - If the new bp is incorrect, don't lose the older one
  151. */
  152. unregister_hw_breakpoint(bp);
  153. return register_user_hw_breakpoint(addr, len, type, triggered,
  154. tsk, active);
  155. }
  156. EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
  157. /**
  158. * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
  159. * @bp: the breakpoint structure to unregister
  160. */
  161. void unregister_hw_breakpoint(struct perf_event *bp)
  162. {
  163. if (!bp)
  164. return;
  165. perf_event_release_kernel(bp);
  166. }
  167. EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
  168. static struct perf_event *
  169. register_kernel_hw_breakpoint_cpu(unsigned long addr,
  170. int len,
  171. int type,
  172. perf_callback_t triggered,
  173. int cpu,
  174. bool active)
  175. {
  176. return register_user_hw_breakpoint_cpu(addr, len, type, triggered,
  177. -1, cpu, active);
  178. }
  179. /**
  180. * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
  181. * @addr: is the memory address that triggers the breakpoint
  182. * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
  183. * @type: the type of the access to the memory (read/write/exec)
  184. * @triggered: callback to trigger when we hit the breakpoint
  185. * @active: should we activate it while registering it
  186. *
  187. * @return a set of per_cpu pointers to perf events
  188. */
  189. struct perf_event **
  190. register_wide_hw_breakpoint(unsigned long addr,
  191. int len,
  192. int type,
  193. perf_callback_t triggered,
  194. bool active)
  195. {
  196. struct perf_event **cpu_events, **pevent, *bp;
  197. long err;
  198. int cpu;
  199. cpu_events = alloc_percpu(typeof(*cpu_events));
  200. if (!cpu_events)
  201. return ERR_PTR(-ENOMEM);
  202. for_each_possible_cpu(cpu) {
  203. pevent = per_cpu_ptr(cpu_events, cpu);
  204. bp = register_kernel_hw_breakpoint_cpu(addr, len, type,
  205. triggered, cpu, active);
  206. *pevent = bp;
  207. if (IS_ERR(bp) || !bp) {
  208. err = PTR_ERR(bp);
  209. goto fail;
  210. }
  211. }
  212. return cpu_events;
  213. fail:
  214. for_each_possible_cpu(cpu) {
  215. pevent = per_cpu_ptr(cpu_events, cpu);
  216. if (IS_ERR(*pevent) || !*pevent)
  217. break;
  218. unregister_hw_breakpoint(*pevent);
  219. }
  220. free_percpu(cpu_events);
  221. /* return the error if any */
  222. return ERR_PTR(err);
  223. }
  224. /**
  225. * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
  226. * @cpu_events: the per cpu set of events to unregister
  227. */
  228. void unregister_wide_hw_breakpoint(struct perf_event **cpu_events)
  229. {
  230. int cpu;
  231. struct perf_event **pevent;
  232. for_each_possible_cpu(cpu) {
  233. pevent = per_cpu_ptr(cpu_events, cpu);
  234. unregister_hw_breakpoint(*pevent);
  235. }
  236. free_percpu(cpu_events);
  237. }
  238. static struct notifier_block hw_breakpoint_exceptions_nb = {
  239. .notifier_call = hw_breakpoint_exceptions_notify,
  240. /* we need to be notified first */
  241. .priority = 0x7fffffff
  242. };
  243. static int __init init_hw_breakpoint(void)
  244. {
  245. return register_die_notifier(&hw_breakpoint_exceptions_nb);
  246. }
  247. core_initcall(init_hw_breakpoint);
  248. struct pmu perf_ops_bp = {
  249. .enable = arch_install_hw_breakpoint,
  250. .disable = arch_uninstall_hw_breakpoint,
  251. .read = hw_breakpoint_pmu_read,
  252. .unthrottle = hw_breakpoint_pmu_unthrottle
  253. };