hw_breakpoint.c 8.4 KB

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