debug-monitors.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ARMv8 single-step debug support and mdscr context switching.
  4. *
  5. * Copyright (C) 2012 ARM Limited
  6. *
  7. * Author: Will Deacon <will.deacon@arm.com>
  8. */
  9. #include <linux/cpu.h>
  10. #include <linux/debugfs.h>
  11. #include <linux/hardirq.h>
  12. #include <linux/init.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/stat.h>
  16. #include <linux/uaccess.h>
  17. #include <linux/sched/task_stack.h>
  18. #include <asm/cpufeature.h>
  19. #include <asm/cputype.h>
  20. #include <asm/daifflags.h>
  21. #include <asm/debug-monitors.h>
  22. #include <asm/system_misc.h>
  23. #include <asm/traps.h>
  24. /* Determine debug architecture. */
  25. u8 debug_monitors_arch(void)
  26. {
  27. return cpuid_feature_extract_unsigned_field(read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1),
  28. ID_AA64DFR0_DEBUGVER_SHIFT);
  29. }
  30. /*
  31. * MDSCR access routines.
  32. */
  33. static void mdscr_write(u32 mdscr)
  34. {
  35. unsigned long flags;
  36. flags = local_daif_save();
  37. write_sysreg(mdscr, mdscr_el1);
  38. local_daif_restore(flags);
  39. }
  40. NOKPROBE_SYMBOL(mdscr_write);
  41. static u32 mdscr_read(void)
  42. {
  43. return read_sysreg(mdscr_el1);
  44. }
  45. NOKPROBE_SYMBOL(mdscr_read);
  46. /*
  47. * Allow root to disable self-hosted debug from userspace.
  48. * This is useful if you want to connect an external JTAG debugger.
  49. */
  50. static bool debug_enabled = true;
  51. static int create_debug_debugfs_entry(void)
  52. {
  53. debugfs_create_bool("debug_enabled", 0644, NULL, &debug_enabled);
  54. return 0;
  55. }
  56. fs_initcall(create_debug_debugfs_entry);
  57. static int __init early_debug_disable(char *buf)
  58. {
  59. debug_enabled = false;
  60. return 0;
  61. }
  62. early_param("nodebugmon", early_debug_disable);
  63. /*
  64. * Keep track of debug users on each core.
  65. * The ref counts are per-cpu so we use a local_t type.
  66. */
  67. static DEFINE_PER_CPU(int, mde_ref_count);
  68. static DEFINE_PER_CPU(int, kde_ref_count);
  69. void enable_debug_monitors(enum dbg_active_el el)
  70. {
  71. u32 mdscr, enable = 0;
  72. WARN_ON(preemptible());
  73. if (this_cpu_inc_return(mde_ref_count) == 1)
  74. enable = DBG_MDSCR_MDE;
  75. if (el == DBG_ACTIVE_EL1 &&
  76. this_cpu_inc_return(kde_ref_count) == 1)
  77. enable |= DBG_MDSCR_KDE;
  78. if (enable && debug_enabled) {
  79. mdscr = mdscr_read();
  80. mdscr |= enable;
  81. mdscr_write(mdscr);
  82. }
  83. }
  84. NOKPROBE_SYMBOL(enable_debug_monitors);
  85. void disable_debug_monitors(enum dbg_active_el el)
  86. {
  87. u32 mdscr, disable = 0;
  88. WARN_ON(preemptible());
  89. if (this_cpu_dec_return(mde_ref_count) == 0)
  90. disable = ~DBG_MDSCR_MDE;
  91. if (el == DBG_ACTIVE_EL1 &&
  92. this_cpu_dec_return(kde_ref_count) == 0)
  93. disable &= ~DBG_MDSCR_KDE;
  94. if (disable) {
  95. mdscr = mdscr_read();
  96. mdscr &= disable;
  97. mdscr_write(mdscr);
  98. }
  99. }
  100. NOKPROBE_SYMBOL(disable_debug_monitors);
  101. /*
  102. * OS lock clearing.
  103. */
  104. static int clear_os_lock(unsigned int cpu)
  105. {
  106. write_sysreg(0, osdlr_el1);
  107. write_sysreg(0, oslar_el1);
  108. isb();
  109. return 0;
  110. }
  111. static int __init debug_monitors_init(void)
  112. {
  113. return cpuhp_setup_state(CPUHP_AP_ARM64_DEBUG_MONITORS_STARTING,
  114. "arm64/debug_monitors:starting",
  115. clear_os_lock, NULL);
  116. }
  117. postcore_initcall(debug_monitors_init);
  118. /*
  119. * Single step API and exception handling.
  120. */
  121. static void set_user_regs_spsr_ss(struct user_pt_regs *regs)
  122. {
  123. regs->pstate |= DBG_SPSR_SS;
  124. }
  125. NOKPROBE_SYMBOL(set_user_regs_spsr_ss);
  126. static void clear_user_regs_spsr_ss(struct user_pt_regs *regs)
  127. {
  128. regs->pstate &= ~DBG_SPSR_SS;
  129. }
  130. NOKPROBE_SYMBOL(clear_user_regs_spsr_ss);
  131. #define set_regs_spsr_ss(r) set_user_regs_spsr_ss(&(r)->user_regs)
  132. #define clear_regs_spsr_ss(r) clear_user_regs_spsr_ss(&(r)->user_regs)
  133. static DEFINE_SPINLOCK(debug_hook_lock);
  134. static LIST_HEAD(user_step_hook);
  135. static LIST_HEAD(kernel_step_hook);
  136. static void register_debug_hook(struct list_head *node, struct list_head *list)
  137. {
  138. spin_lock(&debug_hook_lock);
  139. list_add_rcu(node, list);
  140. spin_unlock(&debug_hook_lock);
  141. }
  142. static void unregister_debug_hook(struct list_head *node)
  143. {
  144. spin_lock(&debug_hook_lock);
  145. list_del_rcu(node);
  146. spin_unlock(&debug_hook_lock);
  147. synchronize_rcu();
  148. }
  149. void register_user_step_hook(struct step_hook *hook)
  150. {
  151. register_debug_hook(&hook->node, &user_step_hook);
  152. }
  153. void unregister_user_step_hook(struct step_hook *hook)
  154. {
  155. unregister_debug_hook(&hook->node);
  156. }
  157. void register_kernel_step_hook(struct step_hook *hook)
  158. {
  159. register_debug_hook(&hook->node, &kernel_step_hook);
  160. }
  161. void unregister_kernel_step_hook(struct step_hook *hook)
  162. {
  163. unregister_debug_hook(&hook->node);
  164. }
  165. /*
  166. * Call registered single step handlers
  167. * There is no Syndrome info to check for determining the handler.
  168. * So we call all the registered handlers, until the right handler is
  169. * found which returns zero.
  170. */
  171. static int call_step_hook(struct pt_regs *regs, unsigned int esr)
  172. {
  173. struct step_hook *hook;
  174. struct list_head *list;
  175. int retval = DBG_HOOK_ERROR;
  176. list = user_mode(regs) ? &user_step_hook : &kernel_step_hook;
  177. /*
  178. * Since single-step exception disables interrupt, this function is
  179. * entirely not preemptible, and we can use rcu list safely here.
  180. */
  181. list_for_each_entry_rcu(hook, list, node) {
  182. retval = hook->fn(regs, esr);
  183. if (retval == DBG_HOOK_HANDLED)
  184. break;
  185. }
  186. return retval;
  187. }
  188. NOKPROBE_SYMBOL(call_step_hook);
  189. static void send_user_sigtrap(int si_code)
  190. {
  191. struct pt_regs *regs = current_pt_regs();
  192. if (WARN_ON(!user_mode(regs)))
  193. return;
  194. if (interrupts_enabled(regs))
  195. local_irq_enable();
  196. arm64_force_sig_fault(SIGTRAP, si_code, instruction_pointer(regs),
  197. "User debug trap");
  198. }
  199. static int single_step_handler(unsigned long unused, unsigned int esr,
  200. struct pt_regs *regs)
  201. {
  202. bool handler_found = false;
  203. /*
  204. * If we are stepping a pending breakpoint, call the hw_breakpoint
  205. * handler first.
  206. */
  207. if (!reinstall_suspended_bps(regs))
  208. return 0;
  209. if (!handler_found && call_step_hook(regs, esr) == DBG_HOOK_HANDLED)
  210. handler_found = true;
  211. if (!handler_found && user_mode(regs)) {
  212. send_user_sigtrap(TRAP_TRACE);
  213. /*
  214. * ptrace will disable single step unless explicitly
  215. * asked to re-enable it. For other clients, it makes
  216. * sense to leave it enabled (i.e. rewind the controls
  217. * to the active-not-pending state).
  218. */
  219. user_rewind_single_step(current);
  220. } else if (!handler_found) {
  221. pr_warn("Unexpected kernel single-step exception at EL1\n");
  222. /*
  223. * Re-enable stepping since we know that we will be
  224. * returning to regs.
  225. */
  226. set_regs_spsr_ss(regs);
  227. }
  228. return 0;
  229. }
  230. NOKPROBE_SYMBOL(single_step_handler);
  231. static LIST_HEAD(user_break_hook);
  232. static LIST_HEAD(kernel_break_hook);
  233. void register_user_break_hook(struct break_hook *hook)
  234. {
  235. register_debug_hook(&hook->node, &user_break_hook);
  236. }
  237. EXPORT_SYMBOL_GPL(register_user_break_hook);
  238. void unregister_user_break_hook(struct break_hook *hook)
  239. {
  240. unregister_debug_hook(&hook->node);
  241. }
  242. EXPORT_SYMBOL_GPL(unregister_user_break_hook);
  243. void register_kernel_break_hook(struct break_hook *hook)
  244. {
  245. register_debug_hook(&hook->node, &kernel_break_hook);
  246. }
  247. EXPORT_SYMBOL_GPL(register_kernel_break_hook);
  248. void unregister_kernel_break_hook(struct break_hook *hook)
  249. {
  250. unregister_debug_hook(&hook->node);
  251. }
  252. EXPORT_SYMBOL_GPL(unregister_kernel_break_hook);
  253. static int call_break_hook(struct pt_regs *regs, unsigned int esr)
  254. {
  255. struct break_hook *hook;
  256. struct list_head *list;
  257. int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
  258. list = user_mode(regs) ? &user_break_hook : &kernel_break_hook;
  259. /*
  260. * Since brk exception disables interrupt, this function is
  261. * entirely not preemptible, and we can use rcu list safely here.
  262. */
  263. list_for_each_entry_rcu(hook, list, node) {
  264. unsigned int comment = esr & ESR_ELx_BRK64_ISS_COMMENT_MASK;
  265. if ((comment & ~hook->mask) == hook->imm)
  266. fn = hook->fn;
  267. }
  268. return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
  269. }
  270. NOKPROBE_SYMBOL(call_break_hook);
  271. static int brk_handler(unsigned long unused, unsigned int esr,
  272. struct pt_regs *regs)
  273. {
  274. if (call_break_hook(regs, esr) == DBG_HOOK_HANDLED)
  275. return 0;
  276. if (user_mode(regs)) {
  277. send_user_sigtrap(TRAP_BRKPT);
  278. } else {
  279. pr_warn("Unexpected kernel BRK exception at EL1\n");
  280. return -EFAULT;
  281. }
  282. return 0;
  283. }
  284. NOKPROBE_SYMBOL(brk_handler);
  285. int aarch32_break_handler(struct pt_regs *regs)
  286. {
  287. u32 arm_instr;
  288. u16 thumb_instr;
  289. bool bp = false;
  290. void __user *pc = (void __user *)instruction_pointer(regs);
  291. if (!compat_user_mode(regs))
  292. return -EFAULT;
  293. if (compat_thumb_mode(regs)) {
  294. /* get 16-bit Thumb instruction */
  295. __le16 instr;
  296. get_user(instr, (__le16 __user *)pc);
  297. thumb_instr = le16_to_cpu(instr);
  298. if (thumb_instr == AARCH32_BREAK_THUMB2_LO) {
  299. /* get second half of 32-bit Thumb-2 instruction */
  300. get_user(instr, (__le16 __user *)(pc + 2));
  301. thumb_instr = le16_to_cpu(instr);
  302. bp = thumb_instr == AARCH32_BREAK_THUMB2_HI;
  303. } else {
  304. bp = thumb_instr == AARCH32_BREAK_THUMB;
  305. }
  306. } else {
  307. /* 32-bit ARM instruction */
  308. __le32 instr;
  309. get_user(instr, (__le32 __user *)pc);
  310. arm_instr = le32_to_cpu(instr);
  311. bp = (arm_instr & ~0xf0000000) == AARCH32_BREAK_ARM;
  312. }
  313. if (!bp)
  314. return -EFAULT;
  315. send_user_sigtrap(TRAP_BRKPT);
  316. return 0;
  317. }
  318. NOKPROBE_SYMBOL(aarch32_break_handler);
  319. void __init debug_traps_init(void)
  320. {
  321. hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
  322. TRAP_TRACE, "single-step handler");
  323. hook_debug_fault_code(DBG_ESR_EVT_BRK, brk_handler, SIGTRAP,
  324. TRAP_BRKPT, "BRK handler");
  325. }
  326. /* Re-enable single step for syscall restarting. */
  327. void user_rewind_single_step(struct task_struct *task)
  328. {
  329. /*
  330. * If single step is active for this thread, then set SPSR.SS
  331. * to 1 to avoid returning to the active-pending state.
  332. */
  333. if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
  334. set_regs_spsr_ss(task_pt_regs(task));
  335. }
  336. NOKPROBE_SYMBOL(user_rewind_single_step);
  337. void user_fastforward_single_step(struct task_struct *task)
  338. {
  339. if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
  340. clear_regs_spsr_ss(task_pt_regs(task));
  341. }
  342. void user_regs_reset_single_step(struct user_pt_regs *regs,
  343. struct task_struct *task)
  344. {
  345. if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
  346. set_user_regs_spsr_ss(regs);
  347. else
  348. clear_user_regs_spsr_ss(regs);
  349. }
  350. /* Kernel API */
  351. void kernel_enable_single_step(struct pt_regs *regs)
  352. {
  353. WARN_ON(!irqs_disabled());
  354. set_regs_spsr_ss(regs);
  355. mdscr_write(mdscr_read() | DBG_MDSCR_SS);
  356. enable_debug_monitors(DBG_ACTIVE_EL1);
  357. }
  358. NOKPROBE_SYMBOL(kernel_enable_single_step);
  359. void kernel_disable_single_step(void)
  360. {
  361. WARN_ON(!irqs_disabled());
  362. mdscr_write(mdscr_read() & ~DBG_MDSCR_SS);
  363. disable_debug_monitors(DBG_ACTIVE_EL1);
  364. }
  365. NOKPROBE_SYMBOL(kernel_disable_single_step);
  366. int kernel_active_single_step(void)
  367. {
  368. WARN_ON(!irqs_disabled());
  369. return mdscr_read() & DBG_MDSCR_SS;
  370. }
  371. NOKPROBE_SYMBOL(kernel_active_single_step);
  372. /* ptrace API */
  373. void user_enable_single_step(struct task_struct *task)
  374. {
  375. struct thread_info *ti = task_thread_info(task);
  376. if (!test_and_set_ti_thread_flag(ti, TIF_SINGLESTEP))
  377. set_regs_spsr_ss(task_pt_regs(task));
  378. }
  379. NOKPROBE_SYMBOL(user_enable_single_step);
  380. void user_disable_single_step(struct task_struct *task)
  381. {
  382. clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  383. }
  384. NOKPROBE_SYMBOL(user_disable_single_step);