hw_breakpoint.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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) 2009 IBM Corporation
  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. */
  24. #include <linux/perf_event.h>
  25. #include <linux/hw_breakpoint.h>
  26. #include <linux/irqflags.h>
  27. #include <linux/notifier.h>
  28. #include <linux/kallsyms.h>
  29. #include <linux/kprobes.h>
  30. #include <linux/percpu.h>
  31. #include <linux/kdebug.h>
  32. #include <linux/kernel.h>
  33. #include <linux/module.h>
  34. #include <linux/sched.h>
  35. #include <linux/init.h>
  36. #include <linux/smp.h>
  37. #include <asm/hw_breakpoint.h>
  38. #include <asm/processor.h>
  39. #include <asm/debugreg.h>
  40. /* Per cpu debug control register value */
  41. DEFINE_PER_CPU(unsigned long, dr7);
  42. /* Per cpu debug address registers values */
  43. static DEFINE_PER_CPU(unsigned long, cpu_debugreg[HBP_NUM]);
  44. /*
  45. * Stores the breakpoints currently in use on each breakpoint address
  46. * register for each cpus
  47. */
  48. static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]);
  49. /*
  50. * Encode the length, type, Exact, and Enable bits for a particular breakpoint
  51. * as stored in debug register 7.
  52. */
  53. unsigned long encode_dr7(int drnum, unsigned int len, unsigned int type)
  54. {
  55. unsigned long bp_info;
  56. bp_info = (len | type) & 0xf;
  57. bp_info <<= (DR_CONTROL_SHIFT + drnum * DR_CONTROL_SIZE);
  58. bp_info |= (DR_GLOBAL_ENABLE << (drnum * DR_ENABLE_SIZE)) |
  59. DR_GLOBAL_SLOWDOWN;
  60. return bp_info;
  61. }
  62. /*
  63. * Decode the length and type bits for a particular breakpoint as
  64. * stored in debug register 7. Return the "enabled" status.
  65. */
  66. int decode_dr7(unsigned long dr7, int bpnum, unsigned *len, unsigned *type)
  67. {
  68. int bp_info = dr7 >> (DR_CONTROL_SHIFT + bpnum * DR_CONTROL_SIZE);
  69. *len = (bp_info & 0xc) | 0x40;
  70. *type = (bp_info & 0x3) | 0x80;
  71. return (dr7 >> (bpnum * DR_ENABLE_SIZE)) & 0x3;
  72. }
  73. /*
  74. * Install a perf counter breakpoint.
  75. *
  76. * We seek a free debug address register and use it for this
  77. * breakpoint. Eventually we enable it in the debug control register.
  78. *
  79. * Atomic: we hold the counter->ctx->lock and we only handle variables
  80. * and registers local to this cpu.
  81. */
  82. int arch_install_hw_breakpoint(struct perf_event *bp)
  83. {
  84. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  85. unsigned long *dr7;
  86. int i;
  87. for (i = 0; i < HBP_NUM; i++) {
  88. struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]);
  89. if (!*slot) {
  90. *slot = bp;
  91. break;
  92. }
  93. }
  94. if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
  95. return -EBUSY;
  96. set_debugreg(info->address, i);
  97. __get_cpu_var(cpu_debugreg[i]) = info->address;
  98. dr7 = &__get_cpu_var(dr7);
  99. *dr7 |= encode_dr7(i, info->len, info->type);
  100. set_debugreg(*dr7, 7);
  101. return 0;
  102. }
  103. /*
  104. * Uninstall the breakpoint contained in the given counter.
  105. *
  106. * First we search the debug address register it uses and then we disable
  107. * it.
  108. *
  109. * Atomic: we hold the counter->ctx->lock and we only handle variables
  110. * and registers local to this cpu.
  111. */
  112. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  113. {
  114. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  115. unsigned long *dr7;
  116. int i;
  117. for (i = 0; i < HBP_NUM; i++) {
  118. struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]);
  119. if (*slot == bp) {
  120. *slot = NULL;
  121. break;
  122. }
  123. }
  124. if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
  125. return;
  126. dr7 = &__get_cpu_var(dr7);
  127. *dr7 &= ~encode_dr7(i, info->len, info->type);
  128. set_debugreg(*dr7, 7);
  129. }
  130. static int get_hbp_len(u8 hbp_len)
  131. {
  132. unsigned int len_in_bytes = 0;
  133. switch (hbp_len) {
  134. case X86_BREAKPOINT_LEN_1:
  135. len_in_bytes = 1;
  136. break;
  137. case X86_BREAKPOINT_LEN_2:
  138. len_in_bytes = 2;
  139. break;
  140. case X86_BREAKPOINT_LEN_4:
  141. len_in_bytes = 4;
  142. break;
  143. #ifdef CONFIG_X86_64
  144. case X86_BREAKPOINT_LEN_8:
  145. len_in_bytes = 8;
  146. break;
  147. #endif
  148. }
  149. return len_in_bytes;
  150. }
  151. /*
  152. * Check for virtual address in user space.
  153. */
  154. int arch_check_va_in_userspace(unsigned long va, u8 hbp_len)
  155. {
  156. unsigned int len;
  157. len = get_hbp_len(hbp_len);
  158. return (va <= TASK_SIZE - len);
  159. }
  160. /*
  161. * Check for virtual address in kernel space.
  162. */
  163. static int arch_check_va_in_kernelspace(unsigned long va, u8 hbp_len)
  164. {
  165. unsigned int len;
  166. len = get_hbp_len(hbp_len);
  167. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  168. }
  169. /*
  170. * Store a breakpoint's encoded address, length, and type.
  171. */
  172. static int arch_store_info(struct perf_event *bp)
  173. {
  174. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  175. /*
  176. * For kernel-addresses, either the address or symbol name can be
  177. * specified.
  178. */
  179. if (info->name)
  180. info->address = (unsigned long)
  181. kallsyms_lookup_name(info->name);
  182. if (info->address)
  183. return 0;
  184. return -EINVAL;
  185. }
  186. int arch_bp_generic_fields(int x86_len, int x86_type,
  187. int *gen_len, int *gen_type)
  188. {
  189. /* Len */
  190. switch (x86_len) {
  191. case X86_BREAKPOINT_LEN_1:
  192. *gen_len = HW_BREAKPOINT_LEN_1;
  193. break;
  194. case X86_BREAKPOINT_LEN_2:
  195. *gen_len = HW_BREAKPOINT_LEN_2;
  196. break;
  197. case X86_BREAKPOINT_LEN_4:
  198. *gen_len = HW_BREAKPOINT_LEN_4;
  199. break;
  200. #ifdef CONFIG_X86_64
  201. case X86_BREAKPOINT_LEN_8:
  202. *gen_len = HW_BREAKPOINT_LEN_8;
  203. break;
  204. #endif
  205. default:
  206. return -EINVAL;
  207. }
  208. /* Type */
  209. switch (x86_type) {
  210. case X86_BREAKPOINT_EXECUTE:
  211. *gen_type = HW_BREAKPOINT_X;
  212. break;
  213. case X86_BREAKPOINT_WRITE:
  214. *gen_type = HW_BREAKPOINT_W;
  215. break;
  216. case X86_BREAKPOINT_RW:
  217. *gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
  218. break;
  219. default:
  220. return -EINVAL;
  221. }
  222. return 0;
  223. }
  224. static int arch_build_bp_info(struct perf_event *bp)
  225. {
  226. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  227. info->address = bp->attr.bp_addr;
  228. /* Len */
  229. switch (bp->attr.bp_len) {
  230. case HW_BREAKPOINT_LEN_1:
  231. info->len = X86_BREAKPOINT_LEN_1;
  232. break;
  233. case HW_BREAKPOINT_LEN_2:
  234. info->len = X86_BREAKPOINT_LEN_2;
  235. break;
  236. case HW_BREAKPOINT_LEN_4:
  237. info->len = X86_BREAKPOINT_LEN_4;
  238. break;
  239. #ifdef CONFIG_X86_64
  240. case HW_BREAKPOINT_LEN_8:
  241. info->len = X86_BREAKPOINT_LEN_8;
  242. break;
  243. #endif
  244. default:
  245. return -EINVAL;
  246. }
  247. /* Type */
  248. switch (bp->attr.bp_type) {
  249. case HW_BREAKPOINT_W:
  250. info->type = X86_BREAKPOINT_WRITE;
  251. break;
  252. case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
  253. info->type = X86_BREAKPOINT_RW;
  254. break;
  255. case HW_BREAKPOINT_X:
  256. info->type = X86_BREAKPOINT_EXECUTE;
  257. break;
  258. default:
  259. return -EINVAL;
  260. }
  261. return 0;
  262. }
  263. /*
  264. * Validate the arch-specific HW Breakpoint register settings
  265. */
  266. int arch_validate_hwbkpt_settings(struct perf_event *bp,
  267. struct task_struct *tsk)
  268. {
  269. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  270. unsigned int align;
  271. int ret;
  272. ret = arch_build_bp_info(bp);
  273. if (ret)
  274. return ret;
  275. ret = -EINVAL;
  276. if (info->type == X86_BREAKPOINT_EXECUTE)
  277. /*
  278. * Ptrace-refactoring code
  279. * For now, we'll allow instruction breakpoint only for user-space
  280. * addresses
  281. */
  282. if ((!arch_check_va_in_userspace(info->address, info->len)) &&
  283. info->len != X86_BREAKPOINT_EXECUTE)
  284. return ret;
  285. switch (info->len) {
  286. case X86_BREAKPOINT_LEN_1:
  287. align = 0;
  288. break;
  289. case X86_BREAKPOINT_LEN_2:
  290. align = 1;
  291. break;
  292. case X86_BREAKPOINT_LEN_4:
  293. align = 3;
  294. break;
  295. #ifdef CONFIG_X86_64
  296. case X86_BREAKPOINT_LEN_8:
  297. align = 7;
  298. break;
  299. #endif
  300. default:
  301. return ret;
  302. }
  303. if (bp->callback)
  304. ret = arch_store_info(bp);
  305. if (ret < 0)
  306. return ret;
  307. /*
  308. * Check that the low-order bits of the address are appropriate
  309. * for the alignment implied by len.
  310. */
  311. if (info->address & align)
  312. return -EINVAL;
  313. /* Check that the virtual address is in the proper range */
  314. if (tsk) {
  315. if (!arch_check_va_in_userspace(info->address, info->len))
  316. return -EFAULT;
  317. } else {
  318. if (!arch_check_va_in_kernelspace(info->address, info->len))
  319. return -EFAULT;
  320. }
  321. return 0;
  322. }
  323. /*
  324. * Release the user breakpoints used by ptrace
  325. */
  326. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  327. {
  328. int i;
  329. struct thread_struct *t = &tsk->thread;
  330. for (i = 0; i < HBP_NUM; i++) {
  331. unregister_hw_breakpoint(t->ptrace_bps[i]);
  332. t->ptrace_bps[i] = NULL;
  333. }
  334. }
  335. #ifdef CONFIG_KVM
  336. void hw_breakpoint_restore(void)
  337. {
  338. set_debugreg(__get_cpu_var(cpu_debugreg[0]), 0);
  339. set_debugreg(__get_cpu_var(cpu_debugreg[1]), 1);
  340. set_debugreg(__get_cpu_var(cpu_debugreg[2]), 2);
  341. set_debugreg(__get_cpu_var(cpu_debugreg[3]), 3);
  342. set_debugreg(current->thread.debugreg6, 6);
  343. set_debugreg(__get_cpu_var(dr7), 7);
  344. }
  345. EXPORT_SYMBOL_GPL(hw_breakpoint_restore);
  346. #endif
  347. /*
  348. * Handle debug exception notifications.
  349. *
  350. * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below.
  351. *
  352. * NOTIFY_DONE returned if one of the following conditions is true.
  353. * i) When the causative address is from user-space and the exception
  354. * is a valid one, i.e. not triggered as a result of lazy debug register
  355. * switching
  356. * ii) When there are more bits than trap<n> set in DR6 register (such
  357. * as BD, BS or BT) indicating that more than one debug condition is
  358. * met and requires some more action in do_debug().
  359. *
  360. * NOTIFY_STOP returned for all other cases
  361. *
  362. */
  363. static int __kprobes hw_breakpoint_handler(struct die_args *args)
  364. {
  365. int i, cpu, rc = NOTIFY_STOP;
  366. struct perf_event *bp;
  367. unsigned long dr7, dr6;
  368. unsigned long *dr6_p;
  369. /* The DR6 value is pointed by args->err */
  370. dr6_p = (unsigned long *)ERR_PTR(args->err);
  371. dr6 = *dr6_p;
  372. /* Do an early return if no trap bits are set in DR6 */
  373. if ((dr6 & DR_TRAP_BITS) == 0)
  374. return NOTIFY_DONE;
  375. get_debugreg(dr7, 7);
  376. /* Disable breakpoints during exception handling */
  377. set_debugreg(0UL, 7);
  378. /*
  379. * Assert that local interrupts are disabled
  380. * Reset the DRn bits in the virtualized register value.
  381. * The ptrace trigger routine will add in whatever is needed.
  382. */
  383. current->thread.debugreg6 &= ~DR_TRAP_BITS;
  384. cpu = get_cpu();
  385. /* Handle all the breakpoints that were triggered */
  386. for (i = 0; i < HBP_NUM; ++i) {
  387. if (likely(!(dr6 & (DR_TRAP0 << i))))
  388. continue;
  389. /*
  390. * The counter may be concurrently released but that can only
  391. * occur from a call_rcu() path. We can then safely fetch
  392. * the breakpoint, use its callback, touch its counter
  393. * while we are in an rcu_read_lock() path.
  394. */
  395. rcu_read_lock();
  396. bp = per_cpu(bp_per_reg[i], cpu);
  397. if (bp)
  398. rc = NOTIFY_DONE;
  399. /*
  400. * Reset the 'i'th TRAP bit in dr6 to denote completion of
  401. * exception handling
  402. */
  403. (*dr6_p) &= ~(DR_TRAP0 << i);
  404. /*
  405. * bp can be NULL due to lazy debug register switching
  406. * or due to concurrent perf counter removing.
  407. */
  408. if (!bp) {
  409. rcu_read_unlock();
  410. break;
  411. }
  412. (bp->callback)(bp, args->regs);
  413. rcu_read_unlock();
  414. }
  415. if (dr6 & (~DR_TRAP_BITS))
  416. rc = NOTIFY_DONE;
  417. set_debugreg(dr7, 7);
  418. put_cpu();
  419. return rc;
  420. }
  421. /*
  422. * Handle debug exception notifications.
  423. */
  424. int __kprobes hw_breakpoint_exceptions_notify(
  425. struct notifier_block *unused, unsigned long val, void *data)
  426. {
  427. if (val != DIE_DEBUG)
  428. return NOTIFY_DONE;
  429. return hw_breakpoint_handler(data);
  430. }
  431. void hw_breakpoint_pmu_read(struct perf_event *bp)
  432. {
  433. /* TODO */
  434. }
  435. void hw_breakpoint_pmu_unthrottle(struct perf_event *bp)
  436. {
  437. /* TODO */
  438. }