hw_breakpoint.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * arch/sh/kernel/hw_breakpoint.c
  3. *
  4. * Unified kernel/user-space hardware breakpoint facility for the on-chip UBC.
  5. *
  6. * Copyright (C) 2009 - 2010 Paul Mundt
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/perf_event.h>
  14. #include <linux/hw_breakpoint.h>
  15. #include <linux/percpu.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/notifier.h>
  18. #include <linux/kprobes.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/io.h>
  21. #include <linux/clk.h>
  22. #include <asm/hw_breakpoint.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/ptrace.h>
  25. /*
  26. * Stores the breakpoints currently in use on each breakpoint address
  27. * register for each cpus
  28. */
  29. static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]);
  30. /*
  31. * A dummy placeholder for early accesses until the CPUs get a chance to
  32. * register their UBCs later in the boot process.
  33. */
  34. static struct sh_ubc ubc_dummy = { .num_events = 0 };
  35. static struct sh_ubc *sh_ubc __read_mostly = &ubc_dummy;
  36. /*
  37. * Install a perf counter breakpoint.
  38. *
  39. * We seek a free UBC channel and use it for this breakpoint.
  40. *
  41. * Atomic: we hold the counter->ctx->lock and we only handle variables
  42. * and registers local to this cpu.
  43. */
  44. int arch_install_hw_breakpoint(struct perf_event *bp)
  45. {
  46. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  47. int i;
  48. for (i = 0; i < sh_ubc->num_events; i++) {
  49. struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]);
  50. if (!*slot) {
  51. *slot = bp;
  52. break;
  53. }
  54. }
  55. if (WARN_ONCE(i == sh_ubc->num_events, "Can't find any breakpoint slot"))
  56. return -EBUSY;
  57. clk_enable(sh_ubc->clk);
  58. sh_ubc->enable(info, i);
  59. return 0;
  60. }
  61. /*
  62. * Uninstall the breakpoint contained in the given counter.
  63. *
  64. * First we search the debug address register it uses and then we disable
  65. * it.
  66. *
  67. * Atomic: we hold the counter->ctx->lock and we only handle variables
  68. * and registers local to this cpu.
  69. */
  70. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  71. {
  72. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  73. int i;
  74. for (i = 0; i < sh_ubc->num_events; i++) {
  75. struct perf_event **slot = &__get_cpu_var(bp_per_reg[i]);
  76. if (*slot == bp) {
  77. *slot = NULL;
  78. break;
  79. }
  80. }
  81. if (WARN_ONCE(i == sh_ubc->num_events, "Can't find any breakpoint slot"))
  82. return;
  83. sh_ubc->disable(info, i);
  84. clk_disable(sh_ubc->clk);
  85. }
  86. static int get_hbp_len(u16 hbp_len)
  87. {
  88. unsigned int len_in_bytes = 0;
  89. switch (hbp_len) {
  90. case SH_BREAKPOINT_LEN_1:
  91. len_in_bytes = 1;
  92. break;
  93. case SH_BREAKPOINT_LEN_2:
  94. len_in_bytes = 2;
  95. break;
  96. case SH_BREAKPOINT_LEN_4:
  97. len_in_bytes = 4;
  98. break;
  99. case SH_BREAKPOINT_LEN_8:
  100. len_in_bytes = 8;
  101. break;
  102. }
  103. return len_in_bytes;
  104. }
  105. /*
  106. * Check for virtual address in user space.
  107. */
  108. int arch_check_va_in_userspace(unsigned long va, u16 hbp_len)
  109. {
  110. unsigned int len;
  111. len = get_hbp_len(hbp_len);
  112. return (va <= TASK_SIZE - len);
  113. }
  114. /*
  115. * Check for virtual address in kernel space.
  116. */
  117. static int arch_check_va_in_kernelspace(unsigned long va, u8 hbp_len)
  118. {
  119. unsigned int len;
  120. len = get_hbp_len(hbp_len);
  121. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  122. }
  123. /*
  124. * Store a breakpoint's encoded address, length, and type.
  125. */
  126. static int arch_store_info(struct perf_event *bp)
  127. {
  128. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  129. /*
  130. * User-space requests will always have the address field populated
  131. * For kernel-addresses, either the address or symbol name can be
  132. * specified.
  133. */
  134. if (info->name)
  135. info->address = (unsigned long)kallsyms_lookup_name(info->name);
  136. if (info->address)
  137. return 0;
  138. return -EINVAL;
  139. }
  140. int arch_bp_generic_fields(int sh_len, int sh_type,
  141. int *gen_len, int *gen_type)
  142. {
  143. /* Len */
  144. switch (sh_len) {
  145. case SH_BREAKPOINT_LEN_1:
  146. *gen_len = HW_BREAKPOINT_LEN_1;
  147. break;
  148. case SH_BREAKPOINT_LEN_2:
  149. *gen_len = HW_BREAKPOINT_LEN_2;
  150. break;
  151. case SH_BREAKPOINT_LEN_4:
  152. *gen_len = HW_BREAKPOINT_LEN_4;
  153. break;
  154. case SH_BREAKPOINT_LEN_8:
  155. *gen_len = HW_BREAKPOINT_LEN_8;
  156. break;
  157. default:
  158. return -EINVAL;
  159. }
  160. /* Type */
  161. switch (sh_type) {
  162. case SH_BREAKPOINT_READ:
  163. *gen_type = HW_BREAKPOINT_R;
  164. case SH_BREAKPOINT_WRITE:
  165. *gen_type = HW_BREAKPOINT_W;
  166. break;
  167. case SH_BREAKPOINT_RW:
  168. *gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
  169. break;
  170. default:
  171. return -EINVAL;
  172. }
  173. return 0;
  174. }
  175. static int arch_build_bp_info(struct perf_event *bp)
  176. {
  177. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  178. info->address = bp->attr.bp_addr;
  179. /* Len */
  180. switch (bp->attr.bp_len) {
  181. case HW_BREAKPOINT_LEN_1:
  182. info->len = SH_BREAKPOINT_LEN_1;
  183. break;
  184. case HW_BREAKPOINT_LEN_2:
  185. info->len = SH_BREAKPOINT_LEN_2;
  186. break;
  187. case HW_BREAKPOINT_LEN_4:
  188. info->len = SH_BREAKPOINT_LEN_4;
  189. break;
  190. case HW_BREAKPOINT_LEN_8:
  191. info->len = SH_BREAKPOINT_LEN_8;
  192. break;
  193. default:
  194. return -EINVAL;
  195. }
  196. /* Type */
  197. switch (bp->attr.bp_type) {
  198. case HW_BREAKPOINT_R:
  199. info->type = SH_BREAKPOINT_READ;
  200. break;
  201. case HW_BREAKPOINT_W:
  202. info->type = SH_BREAKPOINT_WRITE;
  203. break;
  204. case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
  205. info->type = SH_BREAKPOINT_RW;
  206. break;
  207. default:
  208. return -EINVAL;
  209. }
  210. return 0;
  211. }
  212. /*
  213. * Validate the arch-specific HW Breakpoint register settings
  214. */
  215. int arch_validate_hwbkpt_settings(struct perf_event *bp,
  216. struct task_struct *tsk)
  217. {
  218. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  219. unsigned int align;
  220. int ret;
  221. ret = arch_build_bp_info(bp);
  222. if (ret)
  223. return ret;
  224. ret = -EINVAL;
  225. switch (info->len) {
  226. case SH_BREAKPOINT_LEN_1:
  227. align = 0;
  228. break;
  229. case SH_BREAKPOINT_LEN_2:
  230. align = 1;
  231. break;
  232. case SH_BREAKPOINT_LEN_4:
  233. align = 3;
  234. break;
  235. case SH_BREAKPOINT_LEN_8:
  236. align = 7;
  237. break;
  238. default:
  239. return ret;
  240. }
  241. ret = arch_store_info(bp);
  242. if (ret < 0)
  243. return ret;
  244. /*
  245. * Check that the low-order bits of the address are appropriate
  246. * for the alignment implied by len.
  247. */
  248. if (info->address & align)
  249. return -EINVAL;
  250. /* Check that the virtual address is in the proper range */
  251. if (tsk) {
  252. if (!arch_check_va_in_userspace(info->address, info->len))
  253. return -EFAULT;
  254. } else {
  255. if (!arch_check_va_in_kernelspace(info->address, info->len))
  256. return -EFAULT;
  257. }
  258. return 0;
  259. }
  260. /*
  261. * Release the user breakpoints used by ptrace
  262. */
  263. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  264. {
  265. int i;
  266. struct thread_struct *t = &tsk->thread;
  267. for (i = 0; i < sh_ubc->num_events; i++) {
  268. unregister_hw_breakpoint(t->ptrace_bps[i]);
  269. t->ptrace_bps[i] = NULL;
  270. }
  271. }
  272. static int __kprobes hw_breakpoint_handler(struct die_args *args)
  273. {
  274. int cpu, i, rc = NOTIFY_STOP;
  275. struct perf_event *bp;
  276. unsigned int cmf, resume_mask;
  277. /*
  278. * Do an early return if none of the channels triggered.
  279. */
  280. cmf = sh_ubc->triggered_mask();
  281. if (unlikely(!cmf))
  282. return NOTIFY_DONE;
  283. /*
  284. * By default, resume all of the active channels.
  285. */
  286. resume_mask = sh_ubc->active_mask();
  287. /*
  288. * Disable breakpoints during exception handling.
  289. */
  290. sh_ubc->disable_all();
  291. cpu = get_cpu();
  292. for (i = 0; i < sh_ubc->num_events; i++) {
  293. unsigned long event_mask = (1 << i);
  294. if (likely(!(cmf & event_mask)))
  295. continue;
  296. /*
  297. * The counter may be concurrently released but that can only
  298. * occur from a call_rcu() path. We can then safely fetch
  299. * the breakpoint, use its callback, touch its counter
  300. * while we are in an rcu_read_lock() path.
  301. */
  302. rcu_read_lock();
  303. bp = per_cpu(bp_per_reg[i], cpu);
  304. if (bp)
  305. rc = NOTIFY_DONE;
  306. /*
  307. * Reset the condition match flag to denote completion of
  308. * exception handling.
  309. */
  310. sh_ubc->clear_triggered_mask(event_mask);
  311. /*
  312. * bp can be NULL due to concurrent perf counter
  313. * removing.
  314. */
  315. if (!bp) {
  316. rcu_read_unlock();
  317. break;
  318. }
  319. /*
  320. * Don't restore the channel if the breakpoint is from
  321. * ptrace, as it always operates in one-shot mode.
  322. */
  323. if (bp->overflow_handler == ptrace_triggered)
  324. resume_mask &= ~(1 << i);
  325. perf_bp_event(bp, args->regs);
  326. /* Deliver the signal to userspace */
  327. if (arch_check_va_in_userspace(bp->attr.bp_addr,
  328. bp->attr.bp_len)) {
  329. siginfo_t info;
  330. info.si_signo = args->signr;
  331. info.si_errno = notifier_to_errno(rc);
  332. info.si_code = TRAP_HWBKPT;
  333. force_sig_info(args->signr, &info, current);
  334. }
  335. rcu_read_unlock();
  336. }
  337. if (cmf == 0)
  338. rc = NOTIFY_DONE;
  339. sh_ubc->enable_all(resume_mask);
  340. put_cpu();
  341. return rc;
  342. }
  343. BUILD_TRAP_HANDLER(breakpoint)
  344. {
  345. unsigned long ex = lookup_exception_vector();
  346. TRAP_HANDLER_DECL;
  347. notify_die(DIE_BREAKPOINT, "breakpoint", regs, 0, ex, SIGTRAP);
  348. }
  349. /*
  350. * Handle debug exception notifications.
  351. */
  352. int __kprobes hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  353. unsigned long val, void *data)
  354. {
  355. struct die_args *args = data;
  356. if (val != DIE_BREAKPOINT)
  357. return NOTIFY_DONE;
  358. /*
  359. * If the breakpoint hasn't been triggered by the UBC, it's
  360. * probably from a debugger, so don't do anything more here.
  361. *
  362. * This also permits the UBC interface clock to remain off for
  363. * non-UBC breakpoints, as we don't need to check the triggered
  364. * or active channel masks.
  365. */
  366. if (args->trapnr != sh_ubc->trap_nr)
  367. return NOTIFY_DONE;
  368. return hw_breakpoint_handler(data);
  369. }
  370. void hw_breakpoint_pmu_read(struct perf_event *bp)
  371. {
  372. /* TODO */
  373. }
  374. void hw_breakpoint_pmu_unthrottle(struct perf_event *bp)
  375. {
  376. /* TODO */
  377. }
  378. int register_sh_ubc(struct sh_ubc *ubc)
  379. {
  380. /* Bail if it's already assigned */
  381. if (sh_ubc != &ubc_dummy)
  382. return -EBUSY;
  383. sh_ubc = ubc;
  384. pr_info("HW Breakpoints: %s UBC support registered\n", ubc->name);
  385. WARN_ON(ubc->num_events > HBP_NUM);
  386. return 0;
  387. }