debug-monitors.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. * ARMv8 single-step debug support and mdscr context switching.
  3. *
  4. * Copyright (C) 2012 ARM Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Author: Will Deacon <will.deacon@arm.com>
  19. */
  20. #include <linux/cpu.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/hardirq.h>
  23. #include <linux/init.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/kprobes.h>
  26. #include <linux/stat.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/sched/task_stack.h>
  29. #include <asm/cpufeature.h>
  30. #include <asm/cputype.h>
  31. #include <asm/daifflags.h>
  32. #include <asm/debug-monitors.h>
  33. #include <asm/system_misc.h>
  34. #include <asm/traps.h>
  35. /* Determine debug architecture. */
  36. u8 debug_monitors_arch(void)
  37. {
  38. return cpuid_feature_extract_unsigned_field(read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1),
  39. ID_AA64DFR0_DEBUGVER_SHIFT);
  40. }
  41. /*
  42. * MDSCR access routines.
  43. */
  44. static void mdscr_write(u32 mdscr)
  45. {
  46. unsigned long flags;
  47. flags = local_daif_save();
  48. write_sysreg(mdscr, mdscr_el1);
  49. local_daif_restore(flags);
  50. }
  51. NOKPROBE_SYMBOL(mdscr_write);
  52. static u32 mdscr_read(void)
  53. {
  54. return read_sysreg(mdscr_el1);
  55. }
  56. NOKPROBE_SYMBOL(mdscr_read);
  57. /*
  58. * Allow root to disable self-hosted debug from userspace.
  59. * This is useful if you want to connect an external JTAG debugger.
  60. */
  61. static bool debug_enabled = true;
  62. static int create_debug_debugfs_entry(void)
  63. {
  64. debugfs_create_bool("debug_enabled", 0644, NULL, &debug_enabled);
  65. return 0;
  66. }
  67. fs_initcall(create_debug_debugfs_entry);
  68. static int __init early_debug_disable(char *buf)
  69. {
  70. debug_enabled = false;
  71. return 0;
  72. }
  73. early_param("nodebugmon", early_debug_disable);
  74. /*
  75. * Keep track of debug users on each core.
  76. * The ref counts are per-cpu so we use a local_t type.
  77. */
  78. static DEFINE_PER_CPU(int, mde_ref_count);
  79. static DEFINE_PER_CPU(int, kde_ref_count);
  80. void enable_debug_monitors(enum dbg_active_el el)
  81. {
  82. u32 mdscr, enable = 0;
  83. WARN_ON(preemptible());
  84. if (this_cpu_inc_return(mde_ref_count) == 1)
  85. enable = DBG_MDSCR_MDE;
  86. if (el == DBG_ACTIVE_EL1 &&
  87. this_cpu_inc_return(kde_ref_count) == 1)
  88. enable |= DBG_MDSCR_KDE;
  89. if (enable && debug_enabled) {
  90. mdscr = mdscr_read();
  91. mdscr |= enable;
  92. mdscr_write(mdscr);
  93. }
  94. }
  95. NOKPROBE_SYMBOL(enable_debug_monitors);
  96. void disable_debug_monitors(enum dbg_active_el el)
  97. {
  98. u32 mdscr, disable = 0;
  99. WARN_ON(preemptible());
  100. if (this_cpu_dec_return(mde_ref_count) == 0)
  101. disable = ~DBG_MDSCR_MDE;
  102. if (el == DBG_ACTIVE_EL1 &&
  103. this_cpu_dec_return(kde_ref_count) == 0)
  104. disable &= ~DBG_MDSCR_KDE;
  105. if (disable) {
  106. mdscr = mdscr_read();
  107. mdscr &= disable;
  108. mdscr_write(mdscr);
  109. }
  110. }
  111. NOKPROBE_SYMBOL(disable_debug_monitors);
  112. /*
  113. * OS lock clearing.
  114. */
  115. static int clear_os_lock(unsigned int cpu)
  116. {
  117. write_sysreg(0, osdlr_el1);
  118. write_sysreg(0, oslar_el1);
  119. isb();
  120. return 0;
  121. }
  122. static int debug_monitors_init(void)
  123. {
  124. return cpuhp_setup_state(CPUHP_AP_ARM64_DEBUG_MONITORS_STARTING,
  125. "arm64/debug_monitors:starting",
  126. clear_os_lock, NULL);
  127. }
  128. postcore_initcall(debug_monitors_init);
  129. /*
  130. * Single step API and exception handling.
  131. */
  132. static void set_user_regs_spsr_ss(struct user_pt_regs *regs)
  133. {
  134. regs->pstate |= DBG_SPSR_SS;
  135. }
  136. NOKPROBE_SYMBOL(set_user_regs_spsr_ss);
  137. static void clear_user_regs_spsr_ss(struct user_pt_regs *regs)
  138. {
  139. regs->pstate &= ~DBG_SPSR_SS;
  140. }
  141. NOKPROBE_SYMBOL(clear_user_regs_spsr_ss);
  142. #define set_regs_spsr_ss(r) set_user_regs_spsr_ss(&(r)->user_regs)
  143. #define clear_regs_spsr_ss(r) clear_user_regs_spsr_ss(&(r)->user_regs)
  144. /* EL1 Single Step Handler hooks */
  145. static LIST_HEAD(step_hook);
  146. static DEFINE_SPINLOCK(step_hook_lock);
  147. void register_step_hook(struct step_hook *hook)
  148. {
  149. spin_lock(&step_hook_lock);
  150. list_add_rcu(&hook->node, &step_hook);
  151. spin_unlock(&step_hook_lock);
  152. }
  153. void unregister_step_hook(struct step_hook *hook)
  154. {
  155. spin_lock(&step_hook_lock);
  156. list_del_rcu(&hook->node);
  157. spin_unlock(&step_hook_lock);
  158. synchronize_rcu();
  159. }
  160. /*
  161. * Call registered single step handlers
  162. * There is no Syndrome info to check for determining the handler.
  163. * So we call all the registered handlers, until the right handler is
  164. * found which returns zero.
  165. */
  166. static int call_step_hook(struct pt_regs *regs, unsigned int esr)
  167. {
  168. struct step_hook *hook;
  169. int retval = DBG_HOOK_ERROR;
  170. rcu_read_lock();
  171. list_for_each_entry_rcu(hook, &step_hook, node) {
  172. retval = hook->fn(regs, esr);
  173. if (retval == DBG_HOOK_HANDLED)
  174. break;
  175. }
  176. rcu_read_unlock();
  177. return retval;
  178. }
  179. NOKPROBE_SYMBOL(call_step_hook);
  180. static void send_user_sigtrap(int si_code)
  181. {
  182. struct pt_regs *regs = current_pt_regs();
  183. siginfo_t info;
  184. clear_siginfo(&info);
  185. info.si_signo = SIGTRAP;
  186. info.si_errno = 0;
  187. info.si_code = si_code;
  188. info.si_addr = (void __user *)instruction_pointer(regs);
  189. if (WARN_ON(!user_mode(regs)))
  190. return;
  191. if (interrupts_enabled(regs))
  192. local_irq_enable();
  193. arm64_force_sig_info(&info, "User debug trap", current);
  194. }
  195. static int single_step_handler(unsigned long addr, unsigned int esr,
  196. struct pt_regs *regs)
  197. {
  198. bool handler_found = false;
  199. /*
  200. * If we are stepping a pending breakpoint, call the hw_breakpoint
  201. * handler first.
  202. */
  203. if (!reinstall_suspended_bps(regs))
  204. return 0;
  205. #ifdef CONFIG_KPROBES
  206. if (kprobe_single_step_handler(regs, esr) == DBG_HOOK_HANDLED)
  207. handler_found = true;
  208. #endif
  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. /*
  232. * Breakpoint handler is re-entrant as another breakpoint can
  233. * hit within breakpoint handler, especically in kprobes.
  234. * Use reader/writer locks instead of plain spinlock.
  235. */
  236. static LIST_HEAD(break_hook);
  237. static DEFINE_SPINLOCK(break_hook_lock);
  238. void register_break_hook(struct break_hook *hook)
  239. {
  240. spin_lock(&break_hook_lock);
  241. list_add_rcu(&hook->node, &break_hook);
  242. spin_unlock(&break_hook_lock);
  243. }
  244. void unregister_break_hook(struct break_hook *hook)
  245. {
  246. spin_lock(&break_hook_lock);
  247. list_del_rcu(&hook->node);
  248. spin_unlock(&break_hook_lock);
  249. synchronize_rcu();
  250. }
  251. static int call_break_hook(struct pt_regs *regs, unsigned int esr)
  252. {
  253. struct break_hook *hook;
  254. int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
  255. rcu_read_lock();
  256. list_for_each_entry_rcu(hook, &break_hook, node)
  257. if ((esr & hook->esr_mask) == hook->esr_val)
  258. fn = hook->fn;
  259. rcu_read_unlock();
  260. return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
  261. }
  262. NOKPROBE_SYMBOL(call_break_hook);
  263. static int brk_handler(unsigned long addr, unsigned int esr,
  264. struct pt_regs *regs)
  265. {
  266. bool handler_found = false;
  267. #ifdef CONFIG_KPROBES
  268. if ((esr & BRK64_ESR_MASK) == BRK64_ESR_KPROBES) {
  269. if (kprobe_breakpoint_handler(regs, esr) == DBG_HOOK_HANDLED)
  270. handler_found = true;
  271. }
  272. #endif
  273. if (!handler_found && call_break_hook(regs, esr) == DBG_HOOK_HANDLED)
  274. handler_found = true;
  275. if (!handler_found && user_mode(regs)) {
  276. send_user_sigtrap(TRAP_BRKPT);
  277. } else if (!handler_found) {
  278. pr_warn("Unexpected kernel BRK exception at EL1\n");
  279. return -EFAULT;
  280. }
  281. return 0;
  282. }
  283. NOKPROBE_SYMBOL(brk_handler);
  284. int aarch32_break_handler(struct pt_regs *regs)
  285. {
  286. u32 arm_instr;
  287. u16 thumb_instr;
  288. bool bp = false;
  289. void __user *pc = (void __user *)instruction_pointer(regs);
  290. if (!compat_user_mode(regs))
  291. return -EFAULT;
  292. if (compat_thumb_mode(regs)) {
  293. /* get 16-bit Thumb instruction */
  294. __le16 instr;
  295. get_user(instr, (__le16 __user *)pc);
  296. thumb_instr = le16_to_cpu(instr);
  297. if (thumb_instr == AARCH32_BREAK_THUMB2_LO) {
  298. /* get second half of 32-bit Thumb-2 instruction */
  299. get_user(instr, (__le16 __user *)(pc + 2));
  300. thumb_instr = le16_to_cpu(instr);
  301. bp = thumb_instr == AARCH32_BREAK_THUMB2_HI;
  302. } else {
  303. bp = thumb_instr == AARCH32_BREAK_THUMB;
  304. }
  305. } else {
  306. /* 32-bit ARM instruction */
  307. __le32 instr;
  308. get_user(instr, (__le32 __user *)pc);
  309. arm_instr = le32_to_cpu(instr);
  310. bp = (arm_instr & ~0xf0000000) == AARCH32_BREAK_ARM;
  311. }
  312. if (!bp)
  313. return -EFAULT;
  314. send_user_sigtrap(TRAP_BRKPT);
  315. return 0;
  316. }
  317. NOKPROBE_SYMBOL(aarch32_break_handler);
  318. static int __init debug_traps_init(void)
  319. {
  320. hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
  321. TRAP_TRACE, "single-step handler");
  322. hook_debug_fault_code(DBG_ESR_EVT_BRK, brk_handler, SIGTRAP,
  323. TRAP_BRKPT, "ptrace BRK handler");
  324. return 0;
  325. }
  326. arch_initcall(debug_traps_init);
  327. /* Re-enable single step for syscall restarting. */
  328. void user_rewind_single_step(struct task_struct *task)
  329. {
  330. /*
  331. * If single step is active for this thread, then set SPSR.SS
  332. * to 1 to avoid returning to the active-pending state.
  333. */
  334. if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
  335. set_regs_spsr_ss(task_pt_regs(task));
  336. }
  337. NOKPROBE_SYMBOL(user_rewind_single_step);
  338. void user_fastforward_single_step(struct task_struct *task)
  339. {
  340. if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
  341. clear_regs_spsr_ss(task_pt_regs(task));
  342. }
  343. void user_regs_reset_single_step(struct user_pt_regs *regs,
  344. struct task_struct *task)
  345. {
  346. if (test_tsk_thread_flag(task, TIF_SINGLESTEP))
  347. set_user_regs_spsr_ss(regs);
  348. else
  349. clear_user_regs_spsr_ss(regs);
  350. }
  351. /* Kernel API */
  352. void kernel_enable_single_step(struct pt_regs *regs)
  353. {
  354. WARN_ON(!irqs_disabled());
  355. set_regs_spsr_ss(regs);
  356. mdscr_write(mdscr_read() | DBG_MDSCR_SS);
  357. enable_debug_monitors(DBG_ACTIVE_EL1);
  358. }
  359. NOKPROBE_SYMBOL(kernel_enable_single_step);
  360. void kernel_disable_single_step(void)
  361. {
  362. WARN_ON(!irqs_disabled());
  363. mdscr_write(mdscr_read() & ~DBG_MDSCR_SS);
  364. disable_debug_monitors(DBG_ACTIVE_EL1);
  365. }
  366. NOKPROBE_SYMBOL(kernel_disable_single_step);
  367. int kernel_active_single_step(void)
  368. {
  369. WARN_ON(!irqs_disabled());
  370. return mdscr_read() & DBG_MDSCR_SS;
  371. }
  372. NOKPROBE_SYMBOL(kernel_active_single_step);
  373. /* ptrace API */
  374. void user_enable_single_step(struct task_struct *task)
  375. {
  376. struct thread_info *ti = task_thread_info(task);
  377. if (!test_and_set_ti_thread_flag(ti, TIF_SINGLESTEP))
  378. set_regs_spsr_ss(task_pt_regs(task));
  379. }
  380. NOKPROBE_SYMBOL(user_enable_single_step);
  381. void user_disable_single_step(struct task_struct *task)
  382. {
  383. clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  384. }
  385. NOKPROBE_SYMBOL(user_disable_single_step);