hw_breakpoint.c 13 KB

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