hw_breakpoint.c 12 KB

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