hw_breakpoint.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. return 0;
  109. }
  110. /*
  111. * Uninstall the breakpoint contained in the given counter.
  112. *
  113. * First we search the debug address register it uses and then we disable
  114. * it.
  115. *
  116. * Atomic: we hold the counter->ctx->lock and we only handle variables
  117. * and registers local to this cpu.
  118. */
  119. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  120. {
  121. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  122. unsigned long *dr7;
  123. int i;
  124. for (i = 0; i < HBP_NUM; i++) {
  125. struct perf_event **slot = this_cpu_ptr(&bp_per_reg[i]);
  126. if (*slot == bp) {
  127. *slot = NULL;
  128. break;
  129. }
  130. }
  131. if (WARN_ONCE(i == HBP_NUM, "Can't find any breakpoint slot"))
  132. return;
  133. dr7 = this_cpu_ptr(&cpu_dr7);
  134. *dr7 &= ~__encode_dr7(i, info->len, info->type);
  135. set_debugreg(*dr7, 7);
  136. }
  137. static int get_hbp_len(u8 hbp_len)
  138. {
  139. unsigned int len_in_bytes = 0;
  140. switch (hbp_len) {
  141. case X86_BREAKPOINT_LEN_1:
  142. len_in_bytes = 1;
  143. break;
  144. case X86_BREAKPOINT_LEN_2:
  145. len_in_bytes = 2;
  146. break;
  147. case X86_BREAKPOINT_LEN_4:
  148. len_in_bytes = 4;
  149. break;
  150. #ifdef CONFIG_X86_64
  151. case X86_BREAKPOINT_LEN_8:
  152. len_in_bytes = 8;
  153. break;
  154. #endif
  155. }
  156. return len_in_bytes;
  157. }
  158. /*
  159. * Check for virtual address in kernel space.
  160. */
  161. int arch_check_bp_in_kernelspace(struct perf_event *bp)
  162. {
  163. unsigned int len;
  164. unsigned long va;
  165. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  166. va = info->address;
  167. len = get_hbp_len(info->len);
  168. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  169. }
  170. int arch_bp_generic_fields(int x86_len, int x86_type,
  171. int *gen_len, int *gen_type)
  172. {
  173. /* Type */
  174. switch (x86_type) {
  175. case X86_BREAKPOINT_EXECUTE:
  176. if (x86_len != X86_BREAKPOINT_LEN_X)
  177. return -EINVAL;
  178. *gen_type = HW_BREAKPOINT_X;
  179. *gen_len = sizeof(long);
  180. return 0;
  181. case X86_BREAKPOINT_WRITE:
  182. *gen_type = HW_BREAKPOINT_W;
  183. break;
  184. case X86_BREAKPOINT_RW:
  185. *gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
  186. break;
  187. default:
  188. return -EINVAL;
  189. }
  190. /* Len */
  191. switch (x86_len) {
  192. case X86_BREAKPOINT_LEN_1:
  193. *gen_len = HW_BREAKPOINT_LEN_1;
  194. break;
  195. case X86_BREAKPOINT_LEN_2:
  196. *gen_len = HW_BREAKPOINT_LEN_2;
  197. break;
  198. case X86_BREAKPOINT_LEN_4:
  199. *gen_len = HW_BREAKPOINT_LEN_4;
  200. break;
  201. #ifdef CONFIG_X86_64
  202. case X86_BREAKPOINT_LEN_8:
  203. *gen_len = HW_BREAKPOINT_LEN_8;
  204. break;
  205. #endif
  206. default:
  207. return -EINVAL;
  208. }
  209. return 0;
  210. }
  211. static int arch_build_bp_info(struct perf_event *bp)
  212. {
  213. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  214. info->address = bp->attr.bp_addr;
  215. /* Type */
  216. switch (bp->attr.bp_type) {
  217. case HW_BREAKPOINT_W:
  218. info->type = X86_BREAKPOINT_WRITE;
  219. break;
  220. case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
  221. info->type = X86_BREAKPOINT_RW;
  222. break;
  223. case HW_BREAKPOINT_X:
  224. info->type = X86_BREAKPOINT_EXECUTE;
  225. /*
  226. * x86 inst breakpoints need to have a specific undefined len.
  227. * But we still need to check userspace is not trying to setup
  228. * an unsupported length, to get a range breakpoint for example.
  229. */
  230. if (bp->attr.bp_len == sizeof(long)) {
  231. info->len = X86_BREAKPOINT_LEN_X;
  232. return 0;
  233. }
  234. default:
  235. return -EINVAL;
  236. }
  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. return 0;
  257. }
  258. /*
  259. * Validate the arch-specific HW Breakpoint register settings
  260. */
  261. int arch_validate_hwbkpt_settings(struct perf_event *bp)
  262. {
  263. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  264. unsigned int align;
  265. int ret;
  266. ret = arch_build_bp_info(bp);
  267. if (ret)
  268. return ret;
  269. ret = -EINVAL;
  270. switch (info->len) {
  271. case X86_BREAKPOINT_LEN_1:
  272. align = 0;
  273. break;
  274. case X86_BREAKPOINT_LEN_2:
  275. align = 1;
  276. break;
  277. case X86_BREAKPOINT_LEN_4:
  278. align = 3;
  279. break;
  280. #ifdef CONFIG_X86_64
  281. case X86_BREAKPOINT_LEN_8:
  282. align = 7;
  283. break;
  284. #endif
  285. default:
  286. return ret;
  287. }
  288. /*
  289. * Check that the low-order bits of the address are appropriate
  290. * for the alignment implied by len.
  291. */
  292. if (info->address & align)
  293. return -EINVAL;
  294. return 0;
  295. }
  296. /*
  297. * Dump the debug register contents to the user.
  298. * We can't dump our per cpu values because it
  299. * may contain cpu wide breakpoint, something that
  300. * doesn't belong to the current task.
  301. *
  302. * TODO: include non-ptrace user breakpoints (perf)
  303. */
  304. void aout_dump_debugregs(struct user *dump)
  305. {
  306. int i;
  307. int dr7 = 0;
  308. struct perf_event *bp;
  309. struct arch_hw_breakpoint *info;
  310. struct thread_struct *thread = &current->thread;
  311. for (i = 0; i < HBP_NUM; i++) {
  312. bp = thread->ptrace_bps[i];
  313. if (bp && !bp->attr.disabled) {
  314. dump->u_debugreg[i] = bp->attr.bp_addr;
  315. info = counter_arch_bp(bp);
  316. dr7 |= encode_dr7(i, info->len, info->type);
  317. } else {
  318. dump->u_debugreg[i] = 0;
  319. }
  320. }
  321. dump->u_debugreg[4] = 0;
  322. dump->u_debugreg[5] = 0;
  323. dump->u_debugreg[6] = current->thread.debugreg6;
  324. dump->u_debugreg[7] = dr7;
  325. }
  326. EXPORT_SYMBOL_GPL(aout_dump_debugregs);
  327. /*
  328. * Release the user breakpoints used by ptrace
  329. */
  330. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  331. {
  332. int i;
  333. struct thread_struct *t = &tsk->thread;
  334. for (i = 0; i < HBP_NUM; i++) {
  335. unregister_hw_breakpoint(t->ptrace_bps[i]);
  336. t->ptrace_bps[i] = NULL;
  337. }
  338. t->debugreg6 = 0;
  339. t->ptrace_dr7 = 0;
  340. }
  341. void hw_breakpoint_restore(void)
  342. {
  343. set_debugreg(__this_cpu_read(cpu_debugreg[0]), 0);
  344. set_debugreg(__this_cpu_read(cpu_debugreg[1]), 1);
  345. set_debugreg(__this_cpu_read(cpu_debugreg[2]), 2);
  346. set_debugreg(__this_cpu_read(cpu_debugreg[3]), 3);
  347. set_debugreg(current->thread.debugreg6, 6);
  348. set_debugreg(__this_cpu_read(cpu_dr7), 7);
  349. }
  350. EXPORT_SYMBOL_GPL(hw_breakpoint_restore);
  351. /*
  352. * Handle debug exception notifications.
  353. *
  354. * Return value is either NOTIFY_STOP or NOTIFY_DONE as explained below.
  355. *
  356. * NOTIFY_DONE returned if one of the following conditions is true.
  357. * i) When the causative address is from user-space and the exception
  358. * is a valid one, i.e. not triggered as a result of lazy debug register
  359. * switching
  360. * ii) When there are more bits than trap<n> set in DR6 register (such
  361. * as BD, BS or BT) indicating that more than one debug condition is
  362. * met and requires some more action in do_debug().
  363. *
  364. * NOTIFY_STOP returned for all other cases
  365. *
  366. */
  367. static int hw_breakpoint_handler(struct die_args *args)
  368. {
  369. int i, cpu, rc = NOTIFY_STOP;
  370. struct perf_event *bp;
  371. unsigned long dr7, dr6;
  372. unsigned long *dr6_p;
  373. /* The DR6 value is pointed by args->err */
  374. dr6_p = (unsigned long *)ERR_PTR(args->err);
  375. dr6 = *dr6_p;
  376. /* If it's a single step, TRAP bits are random */
  377. if (dr6 & DR_STEP)
  378. return NOTIFY_DONE;
  379. /* Do an early return if no trap bits are set in DR6 */
  380. if ((dr6 & DR_TRAP_BITS) == 0)
  381. return NOTIFY_DONE;
  382. get_debugreg(dr7, 7);
  383. /* Disable breakpoints during exception handling */
  384. set_debugreg(0UL, 7);
  385. /*
  386. * Assert that local interrupts are disabled
  387. * Reset the DRn bits in the virtualized register value.
  388. * The ptrace trigger routine will add in whatever is needed.
  389. */
  390. current->thread.debugreg6 &= ~DR_TRAP_BITS;
  391. cpu = get_cpu();
  392. /* Handle all the breakpoints that were triggered */
  393. for (i = 0; i < HBP_NUM; ++i) {
  394. if (likely(!(dr6 & (DR_TRAP0 << i))))
  395. continue;
  396. /*
  397. * The counter may be concurrently released but that can only
  398. * occur from a call_rcu() path. We can then safely fetch
  399. * the breakpoint, use its callback, touch its counter
  400. * while we are in an rcu_read_lock() path.
  401. */
  402. rcu_read_lock();
  403. bp = per_cpu(bp_per_reg[i], cpu);
  404. /*
  405. * Reset the 'i'th TRAP bit in dr6 to denote completion of
  406. * exception handling
  407. */
  408. (*dr6_p) &= ~(DR_TRAP0 << i);
  409. /*
  410. * bp can be NULL due to lazy debug register switching
  411. * or due to concurrent perf counter removing.
  412. */
  413. if (!bp) {
  414. rcu_read_unlock();
  415. break;
  416. }
  417. perf_bp_event(bp, args->regs);
  418. /*
  419. * Set up resume flag to avoid breakpoint recursion when
  420. * returning back to origin.
  421. */
  422. if (bp->hw.info.type == X86_BREAKPOINT_EXECUTE)
  423. args->regs->flags |= X86_EFLAGS_RF;
  424. rcu_read_unlock();
  425. }
  426. /*
  427. * Further processing in do_debug() is needed for a) user-space
  428. * breakpoints (to generate signals) and b) when the system has
  429. * taken exception due to multiple causes
  430. */
  431. if ((current->thread.debugreg6 & DR_TRAP_BITS) ||
  432. (dr6 & (~DR_TRAP_BITS)))
  433. rc = NOTIFY_DONE;
  434. set_debugreg(dr7, 7);
  435. put_cpu();
  436. return rc;
  437. }
  438. /*
  439. * Handle debug exception notifications.
  440. */
  441. int hw_breakpoint_exceptions_notify(
  442. struct notifier_block *unused, unsigned long val, void *data)
  443. {
  444. if (val != DIE_DEBUG)
  445. return NOTIFY_DONE;
  446. return hw_breakpoint_handler(data);
  447. }
  448. void hw_breakpoint_pmu_read(struct perf_event *bp)
  449. {
  450. /* TODO */
  451. }