hw_breakpoint.c 13 KB

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