hw_breakpoint.c 8.2 KB

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