hw_breakpoint.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. case SH_BREAKPOINT_WRITE:
  143. *gen_type = HW_BREAKPOINT_W;
  144. break;
  145. case SH_BREAKPOINT_RW:
  146. *gen_type = HW_BREAKPOINT_W | HW_BREAKPOINT_R;
  147. break;
  148. default:
  149. return -EINVAL;
  150. }
  151. return 0;
  152. }
  153. static int arch_build_bp_info(struct perf_event *bp,
  154. const struct perf_event_attr *attr,
  155. struct arch_hw_breakpoint *hw)
  156. {
  157. hw->address = attr->bp_addr;
  158. /* Len */
  159. switch (attr->bp_len) {
  160. case HW_BREAKPOINT_LEN_1:
  161. hw->len = SH_BREAKPOINT_LEN_1;
  162. break;
  163. case HW_BREAKPOINT_LEN_2:
  164. hw->len = SH_BREAKPOINT_LEN_2;
  165. break;
  166. case HW_BREAKPOINT_LEN_4:
  167. hw->len = SH_BREAKPOINT_LEN_4;
  168. break;
  169. case HW_BREAKPOINT_LEN_8:
  170. hw->len = SH_BREAKPOINT_LEN_8;
  171. break;
  172. default:
  173. return -EINVAL;
  174. }
  175. /* Type */
  176. switch (attr->bp_type) {
  177. case HW_BREAKPOINT_R:
  178. hw->type = SH_BREAKPOINT_READ;
  179. break;
  180. case HW_BREAKPOINT_W:
  181. hw->type = SH_BREAKPOINT_WRITE;
  182. break;
  183. case HW_BREAKPOINT_W | HW_BREAKPOINT_R:
  184. hw->type = SH_BREAKPOINT_RW;
  185. break;
  186. default:
  187. return -EINVAL;
  188. }
  189. return 0;
  190. }
  191. /*
  192. * Validate the arch-specific HW Breakpoint register settings
  193. */
  194. int hw_breakpoint_arch_parse(struct perf_event *bp,
  195. const struct perf_event_attr *attr,
  196. struct arch_hw_breakpoint *hw)
  197. {
  198. unsigned int align;
  199. int ret;
  200. ret = arch_build_bp_info(bp, attr, hw);
  201. if (ret)
  202. return ret;
  203. ret = -EINVAL;
  204. switch (hw->len) {
  205. case SH_BREAKPOINT_LEN_1:
  206. align = 0;
  207. break;
  208. case SH_BREAKPOINT_LEN_2:
  209. align = 1;
  210. break;
  211. case SH_BREAKPOINT_LEN_4:
  212. align = 3;
  213. break;
  214. case SH_BREAKPOINT_LEN_8:
  215. align = 7;
  216. break;
  217. default:
  218. return ret;
  219. }
  220. /*
  221. * Check that the low-order bits of the address are appropriate
  222. * for the alignment implied by len.
  223. */
  224. if (hw->address & align)
  225. return -EINVAL;
  226. return 0;
  227. }
  228. /*
  229. * Release the user breakpoints used by ptrace
  230. */
  231. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  232. {
  233. int i;
  234. struct thread_struct *t = &tsk->thread;
  235. for (i = 0; i < sh_ubc->num_events; i++) {
  236. unregister_hw_breakpoint(t->ptrace_bps[i]);
  237. t->ptrace_bps[i] = NULL;
  238. }
  239. }
  240. static int __kprobes hw_breakpoint_handler(struct die_args *args)
  241. {
  242. int cpu, i, rc = NOTIFY_STOP;
  243. struct perf_event *bp;
  244. unsigned int cmf, resume_mask;
  245. /*
  246. * Do an early return if none of the channels triggered.
  247. */
  248. cmf = sh_ubc->triggered_mask();
  249. if (unlikely(!cmf))
  250. return NOTIFY_DONE;
  251. /*
  252. * By default, resume all of the active channels.
  253. */
  254. resume_mask = sh_ubc->active_mask();
  255. /*
  256. * Disable breakpoints during exception handling.
  257. */
  258. sh_ubc->disable_all();
  259. cpu = get_cpu();
  260. for (i = 0; i < sh_ubc->num_events; i++) {
  261. unsigned long event_mask = (1 << i);
  262. if (likely(!(cmf & event_mask)))
  263. continue;
  264. /*
  265. * The counter may be concurrently released but that can only
  266. * occur from a call_rcu() path. We can then safely fetch
  267. * the breakpoint, use its callback, touch its counter
  268. * while we are in an rcu_read_lock() path.
  269. */
  270. rcu_read_lock();
  271. bp = per_cpu(bp_per_reg[i], cpu);
  272. if (bp)
  273. rc = NOTIFY_DONE;
  274. /*
  275. * Reset the condition match flag to denote completion of
  276. * exception handling.
  277. */
  278. sh_ubc->clear_triggered_mask(event_mask);
  279. /*
  280. * bp can be NULL due to concurrent perf counter
  281. * removing.
  282. */
  283. if (!bp) {
  284. rcu_read_unlock();
  285. break;
  286. }
  287. /*
  288. * Don't restore the channel if the breakpoint is from
  289. * ptrace, as it always operates in one-shot mode.
  290. */
  291. if (bp->overflow_handler == ptrace_triggered)
  292. resume_mask &= ~(1 << i);
  293. perf_bp_event(bp, args->regs);
  294. /* Deliver the signal to userspace */
  295. if (!arch_check_bp_in_kernelspace(&bp->hw.info)) {
  296. force_sig_fault(SIGTRAP, TRAP_HWBKPT,
  297. (void __user *)NULL, current);
  298. }
  299. rcu_read_unlock();
  300. }
  301. if (cmf == 0)
  302. rc = NOTIFY_DONE;
  303. sh_ubc->enable_all(resume_mask);
  304. put_cpu();
  305. return rc;
  306. }
  307. BUILD_TRAP_HANDLER(breakpoint)
  308. {
  309. unsigned long ex = lookup_exception_vector();
  310. TRAP_HANDLER_DECL;
  311. notify_die(DIE_BREAKPOINT, "breakpoint", regs, 0, ex, SIGTRAP);
  312. }
  313. /*
  314. * Handle debug exception notifications.
  315. */
  316. int __kprobes hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  317. unsigned long val, void *data)
  318. {
  319. struct die_args *args = data;
  320. if (val != DIE_BREAKPOINT)
  321. return NOTIFY_DONE;
  322. /*
  323. * If the breakpoint hasn't been triggered by the UBC, it's
  324. * probably from a debugger, so don't do anything more here.
  325. *
  326. * This also permits the UBC interface clock to remain off for
  327. * non-UBC breakpoints, as we don't need to check the triggered
  328. * or active channel masks.
  329. */
  330. if (args->trapnr != sh_ubc->trap_nr)
  331. return NOTIFY_DONE;
  332. return hw_breakpoint_handler(data);
  333. }
  334. void hw_breakpoint_pmu_read(struct perf_event *bp)
  335. {
  336. /* TODO */
  337. }
  338. int register_sh_ubc(struct sh_ubc *ubc)
  339. {
  340. /* Bail if it's already assigned */
  341. if (sh_ubc != &ubc_dummy)
  342. return -EBUSY;
  343. sh_ubc = ubc;
  344. pr_info("HW Breakpoints: %s UBC support registered\n", ubc->name);
  345. WARN_ON(ubc->num_events > HBP_NUM);
  346. return 0;
  347. }