hw_breakpoint.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Xtensa hardware breakpoints/watchpoints handling functions
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2016 Cadence Design Systems Inc.
  9. */
  10. #include <linux/hw_breakpoint.h>
  11. #include <linux/log2.h>
  12. #include <linux/percpu.h>
  13. #include <linux/perf_event.h>
  14. #include <asm/core.h>
  15. /* Breakpoint currently in use for each IBREAKA. */
  16. static DEFINE_PER_CPU(struct perf_event *, bp_on_reg[XCHAL_NUM_IBREAK]);
  17. /* Watchpoint currently in use for each DBREAKA. */
  18. static DEFINE_PER_CPU(struct perf_event *, wp_on_reg[XCHAL_NUM_DBREAK]);
  19. int hw_breakpoint_slots(int type)
  20. {
  21. switch (type) {
  22. case TYPE_INST:
  23. return XCHAL_NUM_IBREAK;
  24. case TYPE_DATA:
  25. return XCHAL_NUM_DBREAK;
  26. default:
  27. pr_warn("unknown slot type: %d\n", type);
  28. return 0;
  29. }
  30. }
  31. int arch_check_bp_in_kernelspace(struct arch_hw_breakpoint *hw)
  32. {
  33. unsigned int len;
  34. unsigned long va;
  35. va = hw->address;
  36. len = hw->len;
  37. return (va >= TASK_SIZE) && ((va + len - 1) >= TASK_SIZE);
  38. }
  39. /*
  40. * Construct an arch_hw_breakpoint from a perf_event.
  41. */
  42. int hw_breakpoint_arch_parse(struct perf_event *bp,
  43. const struct perf_event_attr *attr,
  44. struct arch_hw_breakpoint *hw)
  45. {
  46. /* Type */
  47. switch (attr->bp_type) {
  48. case HW_BREAKPOINT_X:
  49. hw->type = XTENSA_BREAKPOINT_EXECUTE;
  50. break;
  51. case HW_BREAKPOINT_R:
  52. hw->type = XTENSA_BREAKPOINT_LOAD;
  53. break;
  54. case HW_BREAKPOINT_W:
  55. hw->type = XTENSA_BREAKPOINT_STORE;
  56. break;
  57. case HW_BREAKPOINT_RW:
  58. hw->type = XTENSA_BREAKPOINT_LOAD | XTENSA_BREAKPOINT_STORE;
  59. break;
  60. default:
  61. return -EINVAL;
  62. }
  63. /* Len */
  64. hw->len = attr->bp_len;
  65. if (hw->len < 1 || hw->len > 64 || !is_power_of_2(hw->len))
  66. return -EINVAL;
  67. /* Address */
  68. hw->address = attr->bp_addr;
  69. if (hw->address & (hw->len - 1))
  70. return -EINVAL;
  71. return 0;
  72. }
  73. int hw_breakpoint_exceptions_notify(struct notifier_block *unused,
  74. unsigned long val, void *data)
  75. {
  76. return NOTIFY_DONE;
  77. }
  78. static void xtensa_wsr(unsigned long v, u8 sr)
  79. {
  80. /* We don't have indexed wsr and creating instruction dynamically
  81. * doesn't seem worth it given how small XCHAL_NUM_IBREAK and
  82. * XCHAL_NUM_DBREAK are. Thus the switch. In case build breaks here
  83. * the switch below needs to be extended.
  84. */
  85. BUILD_BUG_ON(XCHAL_NUM_IBREAK > 2);
  86. BUILD_BUG_ON(XCHAL_NUM_DBREAK > 2);
  87. switch (sr) {
  88. #if XCHAL_NUM_IBREAK > 0
  89. case SREG_IBREAKA + 0:
  90. xtensa_set_sr(v, SREG_IBREAKA + 0);
  91. break;
  92. #endif
  93. #if XCHAL_NUM_IBREAK > 1
  94. case SREG_IBREAKA + 1:
  95. xtensa_set_sr(v, SREG_IBREAKA + 1);
  96. break;
  97. #endif
  98. #if XCHAL_NUM_DBREAK > 0
  99. case SREG_DBREAKA + 0:
  100. xtensa_set_sr(v, SREG_DBREAKA + 0);
  101. break;
  102. case SREG_DBREAKC + 0:
  103. xtensa_set_sr(v, SREG_DBREAKC + 0);
  104. break;
  105. #endif
  106. #if XCHAL_NUM_DBREAK > 1
  107. case SREG_DBREAKA + 1:
  108. xtensa_set_sr(v, SREG_DBREAKA + 1);
  109. break;
  110. case SREG_DBREAKC + 1:
  111. xtensa_set_sr(v, SREG_DBREAKC + 1);
  112. break;
  113. #endif
  114. }
  115. }
  116. static int alloc_slot(struct perf_event **slot, size_t n,
  117. struct perf_event *bp)
  118. {
  119. size_t i;
  120. for (i = 0; i < n; ++i) {
  121. if (!slot[i]) {
  122. slot[i] = bp;
  123. return i;
  124. }
  125. }
  126. return -EBUSY;
  127. }
  128. static void set_ibreak_regs(int reg, struct perf_event *bp)
  129. {
  130. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  131. unsigned long ibreakenable;
  132. xtensa_wsr(info->address, SREG_IBREAKA + reg);
  133. ibreakenable = xtensa_get_sr(SREG_IBREAKENABLE);
  134. xtensa_set_sr(ibreakenable | (1 << reg), SREG_IBREAKENABLE);
  135. }
  136. static void set_dbreak_regs(int reg, struct perf_event *bp)
  137. {
  138. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  139. unsigned long dbreakc = DBREAKC_MASK_MASK & -info->len;
  140. if (info->type & XTENSA_BREAKPOINT_LOAD)
  141. dbreakc |= DBREAKC_LOAD_MASK;
  142. if (info->type & XTENSA_BREAKPOINT_STORE)
  143. dbreakc |= DBREAKC_STOR_MASK;
  144. xtensa_wsr(info->address, SREG_DBREAKA + reg);
  145. xtensa_wsr(dbreakc, SREG_DBREAKC + reg);
  146. }
  147. int arch_install_hw_breakpoint(struct perf_event *bp)
  148. {
  149. int i;
  150. if (counter_arch_bp(bp)->type == XTENSA_BREAKPOINT_EXECUTE) {
  151. /* Breakpoint */
  152. i = alloc_slot(this_cpu_ptr(bp_on_reg), XCHAL_NUM_IBREAK, bp);
  153. if (i < 0)
  154. return i;
  155. set_ibreak_regs(i, bp);
  156. } else {
  157. /* Watchpoint */
  158. i = alloc_slot(this_cpu_ptr(wp_on_reg), XCHAL_NUM_DBREAK, bp);
  159. if (i < 0)
  160. return i;
  161. set_dbreak_regs(i, bp);
  162. }
  163. return 0;
  164. }
  165. static int free_slot(struct perf_event **slot, size_t n,
  166. struct perf_event *bp)
  167. {
  168. size_t i;
  169. for (i = 0; i < n; ++i) {
  170. if (slot[i] == bp) {
  171. slot[i] = NULL;
  172. return i;
  173. }
  174. }
  175. return -EBUSY;
  176. }
  177. void arch_uninstall_hw_breakpoint(struct perf_event *bp)
  178. {
  179. struct arch_hw_breakpoint *info = counter_arch_bp(bp);
  180. int i;
  181. if (info->type == XTENSA_BREAKPOINT_EXECUTE) {
  182. unsigned long ibreakenable;
  183. /* Breakpoint */
  184. i = free_slot(this_cpu_ptr(bp_on_reg), XCHAL_NUM_IBREAK, bp);
  185. if (i >= 0) {
  186. ibreakenable = xtensa_get_sr(SREG_IBREAKENABLE);
  187. xtensa_set_sr(ibreakenable & ~(1 << i),
  188. SREG_IBREAKENABLE);
  189. }
  190. } else {
  191. /* Watchpoint */
  192. i = free_slot(this_cpu_ptr(wp_on_reg), XCHAL_NUM_DBREAK, bp);
  193. if (i >= 0)
  194. xtensa_wsr(0, SREG_DBREAKC + i);
  195. }
  196. }
  197. void hw_breakpoint_pmu_read(struct perf_event *bp)
  198. {
  199. }
  200. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  201. {
  202. int i;
  203. struct thread_struct *t = &tsk->thread;
  204. for (i = 0; i < XCHAL_NUM_IBREAK; ++i) {
  205. if (t->ptrace_bp[i]) {
  206. unregister_hw_breakpoint(t->ptrace_bp[i]);
  207. t->ptrace_bp[i] = NULL;
  208. }
  209. }
  210. for (i = 0; i < XCHAL_NUM_DBREAK; ++i) {
  211. if (t->ptrace_wp[i]) {
  212. unregister_hw_breakpoint(t->ptrace_wp[i]);
  213. t->ptrace_wp[i] = NULL;
  214. }
  215. }
  216. }
  217. /*
  218. * Set ptrace breakpoint pointers to zero for this task.
  219. * This is required in order to prevent child processes from unregistering
  220. * breakpoints held by their parent.
  221. */
  222. void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
  223. {
  224. memset(tsk->thread.ptrace_bp, 0, sizeof(tsk->thread.ptrace_bp));
  225. memset(tsk->thread.ptrace_wp, 0, sizeof(tsk->thread.ptrace_wp));
  226. }
  227. void restore_dbreak(void)
  228. {
  229. int i;
  230. for (i = 0; i < XCHAL_NUM_DBREAK; ++i) {
  231. struct perf_event *bp = this_cpu_ptr(wp_on_reg)[i];
  232. if (bp)
  233. set_dbreak_regs(i, bp);
  234. }
  235. clear_thread_flag(TIF_DB_DISABLED);
  236. }
  237. int check_hw_breakpoint(struct pt_regs *regs)
  238. {
  239. if (regs->debugcause & BIT(DEBUGCAUSE_IBREAK_BIT)) {
  240. int i;
  241. struct perf_event **bp = this_cpu_ptr(bp_on_reg);
  242. for (i = 0; i < XCHAL_NUM_IBREAK; ++i) {
  243. if (bp[i] && !bp[i]->attr.disabled &&
  244. regs->pc == bp[i]->attr.bp_addr)
  245. perf_bp_event(bp[i], regs);
  246. }
  247. return 0;
  248. } else if (regs->debugcause & BIT(DEBUGCAUSE_DBREAK_BIT)) {
  249. struct perf_event **bp = this_cpu_ptr(wp_on_reg);
  250. int dbnum = (regs->debugcause & DEBUGCAUSE_DBNUM_MASK) >>
  251. DEBUGCAUSE_DBNUM_SHIFT;
  252. if (dbnum < XCHAL_NUM_DBREAK && bp[dbnum]) {
  253. if (user_mode(regs)) {
  254. perf_bp_event(bp[dbnum], regs);
  255. } else {
  256. set_thread_flag(TIF_DB_DISABLED);
  257. xtensa_wsr(0, SREG_DBREAKC + dbnum);
  258. }
  259. } else {
  260. WARN_ONCE(1,
  261. "Wrong/unconfigured DBNUM reported in DEBUGCAUSE: %d\n",
  262. dbnum);
  263. }
  264. return 0;
  265. }
  266. return -ENOENT;
  267. }