hw_breakpoint.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. * Thanks to Ingo Molnar for his many suggestions.
  21. */
  22. /*
  23. * HW_breakpoint: a unified kernel/user-space hardware breakpoint facility,
  24. * using the CPU's debug registers.
  25. * This file contains the arch-independent routines.
  26. */
  27. #include <linux/irqflags.h>
  28. #include <linux/kallsyms.h>
  29. #include <linux/notifier.h>
  30. #include <linux/kprobes.h>
  31. #include <linux/kdebug.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/percpu.h>
  35. #include <linux/sched.h>
  36. #include <linux/init.h>
  37. #include <linux/smp.h>
  38. #include <linux/hw_breakpoint.h>
  39. #include <asm/processor.h>
  40. #ifdef CONFIG_X86
  41. #include <asm/debugreg.h>
  42. #endif
  43. /*
  44. * Constraints data
  45. */
  46. /* Number of pinned cpu breakpoints in a cpu */
  47. static DEFINE_PER_CPU(unsigned int, nr_cpu_bp_pinned);
  48. /* Number of pinned task breakpoints in a cpu */
  49. static DEFINE_PER_CPU(unsigned int, task_bp_pinned[HBP_NUM]);
  50. /* Number of non-pinned cpu/task breakpoints in a cpu */
  51. static DEFINE_PER_CPU(unsigned int, nr_bp_flexible);
  52. /* Gather the number of total pinned and un-pinned bp in a cpuset */
  53. struct bp_busy_slots {
  54. unsigned int pinned;
  55. unsigned int flexible;
  56. };
  57. /* Serialize accesses to the above constraints */
  58. static DEFINE_MUTEX(nr_bp_mutex);
  59. /*
  60. * Report the maximum number of pinned breakpoints a task
  61. * have in this cpu
  62. */
  63. static unsigned int max_task_bp_pinned(int cpu)
  64. {
  65. int i;
  66. unsigned int *tsk_pinned = per_cpu(task_bp_pinned, cpu);
  67. for (i = HBP_NUM -1; i >= 0; i--) {
  68. if (tsk_pinned[i] > 0)
  69. return i + 1;
  70. }
  71. return 0;
  72. }
  73. /*
  74. * Report the number of pinned/un-pinned breakpoints we have in
  75. * a given cpu (cpu > -1) or in all of them (cpu = -1).
  76. */
  77. static void fetch_bp_busy_slots(struct bp_busy_slots *slots, int cpu)
  78. {
  79. if (cpu >= 0) {
  80. slots->pinned = per_cpu(nr_cpu_bp_pinned, cpu);
  81. slots->pinned += max_task_bp_pinned(cpu);
  82. slots->flexible = per_cpu(nr_bp_flexible, cpu);
  83. return;
  84. }
  85. for_each_online_cpu(cpu) {
  86. unsigned int nr;
  87. nr = per_cpu(nr_cpu_bp_pinned, cpu);
  88. nr += max_task_bp_pinned(cpu);
  89. if (nr > slots->pinned)
  90. slots->pinned = nr;
  91. nr = per_cpu(nr_bp_flexible, cpu);
  92. if (nr > slots->flexible)
  93. slots->flexible = nr;
  94. }
  95. }
  96. /*
  97. * Add a pinned breakpoint for the given task in our constraint table
  98. */
  99. static void toggle_bp_task_slot(struct task_struct *tsk, int cpu, bool enable)
  100. {
  101. int count = 0;
  102. struct perf_event *bp;
  103. struct perf_event_context *ctx = tsk->perf_event_ctxp;
  104. unsigned int *task_bp_pinned;
  105. struct list_head *list;
  106. unsigned long flags;
  107. if (WARN_ONCE(!ctx, "No perf context for this task"))
  108. return;
  109. list = &ctx->event_list;
  110. spin_lock_irqsave(&ctx->lock, flags);
  111. /*
  112. * The current breakpoint counter is not included in the list
  113. * at the open() callback time
  114. */
  115. list_for_each_entry(bp, list, event_entry) {
  116. if (bp->attr.type == PERF_TYPE_BREAKPOINT)
  117. count++;
  118. }
  119. spin_unlock_irqrestore(&ctx->lock, flags);
  120. if (WARN_ONCE(count < 0, "No breakpoint counter found in the counter list"))
  121. return;
  122. task_bp_pinned = per_cpu(task_bp_pinned, cpu);
  123. if (enable) {
  124. task_bp_pinned[count]++;
  125. if (count > 0)
  126. task_bp_pinned[count-1]--;
  127. } else {
  128. task_bp_pinned[count]--;
  129. if (count > 0)
  130. task_bp_pinned[count-1]++;
  131. }
  132. }
  133. /*
  134. * Add/remove the given breakpoint in our constraint table
  135. */
  136. static void toggle_bp_slot(struct perf_event *bp, bool enable)
  137. {
  138. int cpu = bp->cpu;
  139. struct task_struct *tsk = bp->ctx->task;
  140. /* Pinned counter task profiling */
  141. if (tsk) {
  142. if (cpu >= 0) {
  143. toggle_bp_task_slot(tsk, cpu, enable);
  144. return;
  145. }
  146. for_each_online_cpu(cpu)
  147. toggle_bp_task_slot(tsk, cpu, enable);
  148. return;
  149. }
  150. /* Pinned counter cpu profiling */
  151. if (enable)
  152. per_cpu(nr_cpu_bp_pinned, bp->cpu)++;
  153. else
  154. per_cpu(nr_cpu_bp_pinned, bp->cpu)--;
  155. }
  156. /*
  157. * Contraints to check before allowing this new breakpoint counter:
  158. *
  159. * == Non-pinned counter == (Considered as pinned for now)
  160. *
  161. * - If attached to a single cpu, check:
  162. *
  163. * (per_cpu(nr_bp_flexible, cpu) || (per_cpu(nr_cpu_bp_pinned, cpu)
  164. * + max(per_cpu(task_bp_pinned, cpu)))) < HBP_NUM
  165. *
  166. * -> If there are already non-pinned counters in this cpu, it means
  167. * there is already a free slot for them.
  168. * Otherwise, we check that the maximum number of per task
  169. * breakpoints (for this cpu) plus the number of per cpu breakpoint
  170. * (for this cpu) doesn't cover every registers.
  171. *
  172. * - If attached to every cpus, check:
  173. *
  174. * (per_cpu(nr_bp_flexible, *) || (max(per_cpu(nr_cpu_bp_pinned, *))
  175. * + max(per_cpu(task_bp_pinned, *)))) < HBP_NUM
  176. *
  177. * -> This is roughly the same, except we check the number of per cpu
  178. * bp for every cpu and we keep the max one. Same for the per tasks
  179. * breakpoints.
  180. *
  181. *
  182. * == Pinned counter ==
  183. *
  184. * - If attached to a single cpu, check:
  185. *
  186. * ((per_cpu(nr_bp_flexible, cpu) > 1) + per_cpu(nr_cpu_bp_pinned, cpu)
  187. * + max(per_cpu(task_bp_pinned, cpu))) < HBP_NUM
  188. *
  189. * -> Same checks as before. But now the nr_bp_flexible, if any, must keep
  190. * one register at least (or they will never be fed).
  191. *
  192. * - If attached to every cpus, check:
  193. *
  194. * ((per_cpu(nr_bp_flexible, *) > 1) + max(per_cpu(nr_cpu_bp_pinned, *))
  195. * + max(per_cpu(task_bp_pinned, *))) < HBP_NUM
  196. */
  197. int reserve_bp_slot(struct perf_event *bp)
  198. {
  199. struct bp_busy_slots slots = {0};
  200. int ret = 0;
  201. mutex_lock(&nr_bp_mutex);
  202. fetch_bp_busy_slots(&slots, bp->cpu);
  203. /* Flexible counters need to keep at least one slot */
  204. if (slots.pinned + (!!slots.flexible) == HBP_NUM) {
  205. ret = -ENOSPC;
  206. goto end;
  207. }
  208. toggle_bp_slot(bp, true);
  209. end:
  210. mutex_unlock(&nr_bp_mutex);
  211. return ret;
  212. }
  213. void release_bp_slot(struct perf_event *bp)
  214. {
  215. mutex_lock(&nr_bp_mutex);
  216. toggle_bp_slot(bp, false);
  217. mutex_unlock(&nr_bp_mutex);
  218. }
  219. int __register_perf_hw_breakpoint(struct perf_event *bp)
  220. {
  221. int ret;
  222. ret = reserve_bp_slot(bp);
  223. if (ret)
  224. return ret;
  225. if (!bp->attr.disabled)
  226. ret = arch_validate_hwbkpt_settings(bp, bp->ctx->task);
  227. return ret;
  228. }
  229. int register_perf_hw_breakpoint(struct perf_event *bp)
  230. {
  231. bp->callback = perf_bp_event;
  232. return __register_perf_hw_breakpoint(bp);
  233. }
  234. /*
  235. * Register a breakpoint bound to a task and a given cpu.
  236. * If cpu is -1, the breakpoint is active for the task in every cpu
  237. * If the task is -1, the breakpoint is active for every tasks in the given
  238. * cpu.
  239. */
  240. static struct perf_event *
  241. register_user_hw_breakpoint_cpu(unsigned long addr,
  242. int len,
  243. int type,
  244. perf_callback_t triggered,
  245. pid_t pid,
  246. int cpu,
  247. bool active)
  248. {
  249. struct perf_event_attr *attr;
  250. struct perf_event *bp;
  251. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  252. if (!attr)
  253. return ERR_PTR(-ENOMEM);
  254. attr->type = PERF_TYPE_BREAKPOINT;
  255. attr->size = sizeof(*attr);
  256. attr->bp_addr = addr;
  257. attr->bp_len = len;
  258. attr->bp_type = type;
  259. /*
  260. * Such breakpoints are used by debuggers to trigger signals when
  261. * we hit the excepted memory op. We can't miss such events, they
  262. * must be pinned.
  263. */
  264. attr->pinned = 1;
  265. if (!active)
  266. attr->disabled = 1;
  267. bp = perf_event_create_kernel_counter(attr, cpu, pid, triggered);
  268. kfree(attr);
  269. return bp;
  270. }
  271. /**
  272. * register_user_hw_breakpoint - register a hardware breakpoint for user space
  273. * @addr: is the memory address that triggers the breakpoint
  274. * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
  275. * @type: the type of the access to the memory (read/write/exec)
  276. * @triggered: callback to trigger when we hit the breakpoint
  277. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  278. * @active: should we activate it while registering it
  279. *
  280. */
  281. struct perf_event *
  282. register_user_hw_breakpoint(unsigned long addr,
  283. int len,
  284. int type,
  285. perf_callback_t triggered,
  286. struct task_struct *tsk,
  287. bool active)
  288. {
  289. return register_user_hw_breakpoint_cpu(addr, len, type, triggered,
  290. tsk->pid, -1, active);
  291. }
  292. EXPORT_SYMBOL_GPL(register_user_hw_breakpoint);
  293. /**
  294. * modify_user_hw_breakpoint - modify a user-space hardware breakpoint
  295. * @bp: the breakpoint structure to modify
  296. * @addr: is the memory address that triggers the breakpoint
  297. * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
  298. * @type: the type of the access to the memory (read/write/exec)
  299. * @triggered: callback to trigger when we hit the breakpoint
  300. * @tsk: pointer to 'task_struct' of the process to which the address belongs
  301. * @active: should we activate it while registering it
  302. */
  303. struct perf_event *
  304. modify_user_hw_breakpoint(struct perf_event *bp,
  305. unsigned long addr,
  306. int len,
  307. int type,
  308. perf_callback_t triggered,
  309. struct task_struct *tsk,
  310. bool active)
  311. {
  312. /*
  313. * FIXME: do it without unregistering
  314. * - We don't want to lose our slot
  315. * - If the new bp is incorrect, don't lose the older one
  316. */
  317. unregister_hw_breakpoint(bp);
  318. return register_user_hw_breakpoint(addr, len, type, triggered,
  319. tsk, active);
  320. }
  321. EXPORT_SYMBOL_GPL(modify_user_hw_breakpoint);
  322. /**
  323. * unregister_hw_breakpoint - unregister a user-space hardware breakpoint
  324. * @bp: the breakpoint structure to unregister
  325. */
  326. void unregister_hw_breakpoint(struct perf_event *bp)
  327. {
  328. if (!bp)
  329. return;
  330. perf_event_release_kernel(bp);
  331. }
  332. EXPORT_SYMBOL_GPL(unregister_hw_breakpoint);
  333. static struct perf_event *
  334. register_kernel_hw_breakpoint_cpu(unsigned long addr,
  335. int len,
  336. int type,
  337. perf_callback_t triggered,
  338. int cpu,
  339. bool active)
  340. {
  341. return register_user_hw_breakpoint_cpu(addr, len, type, triggered,
  342. -1, cpu, active);
  343. }
  344. /**
  345. * register_wide_hw_breakpoint - register a wide breakpoint in the kernel
  346. * @addr: is the memory address that triggers the breakpoint
  347. * @len: the length of the access to the memory (1 byte, 2 bytes etc...)
  348. * @type: the type of the access to the memory (read/write/exec)
  349. * @triggered: callback to trigger when we hit the breakpoint
  350. * @active: should we activate it while registering it
  351. *
  352. * @return a set of per_cpu pointers to perf events
  353. */
  354. struct perf_event **
  355. register_wide_hw_breakpoint(unsigned long addr,
  356. int len,
  357. int type,
  358. perf_callback_t triggered,
  359. bool active)
  360. {
  361. struct perf_event **cpu_events, **pevent, *bp;
  362. long err;
  363. int cpu;
  364. cpu_events = alloc_percpu(typeof(*cpu_events));
  365. if (!cpu_events)
  366. return ERR_PTR(-ENOMEM);
  367. for_each_possible_cpu(cpu) {
  368. pevent = per_cpu_ptr(cpu_events, cpu);
  369. bp = register_kernel_hw_breakpoint_cpu(addr, len, type,
  370. triggered, cpu, active);
  371. *pevent = bp;
  372. if (IS_ERR(bp) || !bp) {
  373. err = PTR_ERR(bp);
  374. goto fail;
  375. }
  376. }
  377. return cpu_events;
  378. fail:
  379. for_each_possible_cpu(cpu) {
  380. pevent = per_cpu_ptr(cpu_events, cpu);
  381. if (IS_ERR(*pevent) || !*pevent)
  382. break;
  383. unregister_hw_breakpoint(*pevent);
  384. }
  385. free_percpu(cpu_events);
  386. /* return the error if any */
  387. return ERR_PTR(err);
  388. }
  389. EXPORT_SYMBOL_GPL(register_wide_hw_breakpoint);
  390. /**
  391. * unregister_wide_hw_breakpoint - unregister a wide breakpoint in the kernel
  392. * @cpu_events: the per cpu set of events to unregister
  393. */
  394. void unregister_wide_hw_breakpoint(struct perf_event **cpu_events)
  395. {
  396. int cpu;
  397. struct perf_event **pevent;
  398. for_each_possible_cpu(cpu) {
  399. pevent = per_cpu_ptr(cpu_events, cpu);
  400. unregister_hw_breakpoint(*pevent);
  401. }
  402. free_percpu(cpu_events);
  403. }
  404. EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint);
  405. static struct notifier_block hw_breakpoint_exceptions_nb = {
  406. .notifier_call = hw_breakpoint_exceptions_notify,
  407. /* we need to be notified first */
  408. .priority = 0x7fffffff
  409. };
  410. static int __init init_hw_breakpoint(void)
  411. {
  412. return register_die_notifier(&hw_breakpoint_exceptions_nb);
  413. }
  414. core_initcall(init_hw_breakpoint);
  415. struct pmu perf_ops_bp = {
  416. .enable = arch_install_hw_breakpoint,
  417. .disable = arch_uninstall_hw_breakpoint,
  418. .read = hw_breakpoint_pmu_read,
  419. .unthrottle = hw_breakpoint_pmu_unthrottle
  420. };