hw_breakpoint.c 13 KB

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