debug-monitors.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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/stat.h>
  26. #include <linux/uaccess.h>
  27. #include <asm/debug-monitors.h>
  28. #include <asm/cputype.h>
  29. #include <asm/system_misc.h>
  30. /* Determine debug architecture. */
  31. u8 debug_monitors_arch(void)
  32. {
  33. return read_cpuid(ID_AA64DFR0_EL1) & 0xf;
  34. }
  35. /*
  36. * MDSCR access routines.
  37. */
  38. static void mdscr_write(u32 mdscr)
  39. {
  40. unsigned long flags;
  41. local_dbg_save(flags);
  42. asm volatile("msr mdscr_el1, %0" :: "r" (mdscr));
  43. local_dbg_restore(flags);
  44. }
  45. static u32 mdscr_read(void)
  46. {
  47. u32 mdscr;
  48. asm volatile("mrs %0, mdscr_el1" : "=r" (mdscr));
  49. return mdscr;
  50. }
  51. /*
  52. * Allow root to disable self-hosted debug from userspace.
  53. * This is useful if you want to connect an external JTAG debugger.
  54. */
  55. static u32 debug_enabled = 1;
  56. static int create_debug_debugfs_entry(void)
  57. {
  58. debugfs_create_bool("debug_enabled", 0644, NULL, &debug_enabled);
  59. return 0;
  60. }
  61. fs_initcall(create_debug_debugfs_entry);
  62. static int __init early_debug_disable(char *buf)
  63. {
  64. debug_enabled = 0;
  65. return 0;
  66. }
  67. early_param("nodebugmon", early_debug_disable);
  68. /*
  69. * Keep track of debug users on each core.
  70. * The ref counts are per-cpu so we use a local_t type.
  71. */
  72. static DEFINE_PER_CPU(int, mde_ref_count);
  73. static DEFINE_PER_CPU(int, kde_ref_count);
  74. void enable_debug_monitors(enum debug_el el)
  75. {
  76. u32 mdscr, enable = 0;
  77. WARN_ON(preemptible());
  78. if (this_cpu_inc_return(mde_ref_count) == 1)
  79. enable = DBG_MDSCR_MDE;
  80. if (el == DBG_ACTIVE_EL1 &&
  81. this_cpu_inc_return(kde_ref_count) == 1)
  82. enable |= DBG_MDSCR_KDE;
  83. if (enable && debug_enabled) {
  84. mdscr = mdscr_read();
  85. mdscr |= enable;
  86. mdscr_write(mdscr);
  87. }
  88. }
  89. void disable_debug_monitors(enum debug_el el)
  90. {
  91. u32 mdscr, disable = 0;
  92. WARN_ON(preemptible());
  93. if (this_cpu_dec_return(mde_ref_count) == 0)
  94. disable = ~DBG_MDSCR_MDE;
  95. if (el == DBG_ACTIVE_EL1 &&
  96. this_cpu_dec_return(kde_ref_count) == 0)
  97. disable &= ~DBG_MDSCR_KDE;
  98. if (disable) {
  99. mdscr = mdscr_read();
  100. mdscr &= disable;
  101. mdscr_write(mdscr);
  102. }
  103. }
  104. /*
  105. * OS lock clearing.
  106. */
  107. static void clear_os_lock(void *unused)
  108. {
  109. asm volatile("msr oslar_el1, %0" : : "r" (0));
  110. }
  111. static int os_lock_notify(struct notifier_block *self,
  112. unsigned long action, void *data)
  113. {
  114. int cpu = (unsigned long)data;
  115. if (action == CPU_ONLINE)
  116. smp_call_function_single(cpu, clear_os_lock, NULL, 1);
  117. return NOTIFY_OK;
  118. }
  119. static struct notifier_block os_lock_nb = {
  120. .notifier_call = os_lock_notify,
  121. };
  122. static int debug_monitors_init(void)
  123. {
  124. cpu_notifier_register_begin();
  125. /* Clear the OS lock. */
  126. on_each_cpu(clear_os_lock, NULL, 1);
  127. isb();
  128. local_dbg_enable();
  129. /* Register hotplug handler. */
  130. __register_cpu_notifier(&os_lock_nb);
  131. cpu_notifier_register_done();
  132. return 0;
  133. }
  134. postcore_initcall(debug_monitors_init);
  135. /*
  136. * Single step API and exception handling.
  137. */
  138. static void set_regs_spsr_ss(struct pt_regs *regs)
  139. {
  140. unsigned long spsr;
  141. spsr = regs->pstate;
  142. spsr &= ~DBG_SPSR_SS;
  143. spsr |= DBG_SPSR_SS;
  144. regs->pstate = spsr;
  145. }
  146. static void clear_regs_spsr_ss(struct pt_regs *regs)
  147. {
  148. unsigned long spsr;
  149. spsr = regs->pstate;
  150. spsr &= ~DBG_SPSR_SS;
  151. regs->pstate = spsr;
  152. }
  153. /* EL1 Single Step Handler hooks */
  154. static LIST_HEAD(step_hook);
  155. static DEFINE_RWLOCK(step_hook_lock);
  156. void register_step_hook(struct step_hook *hook)
  157. {
  158. write_lock(&step_hook_lock);
  159. list_add(&hook->node, &step_hook);
  160. write_unlock(&step_hook_lock);
  161. }
  162. void unregister_step_hook(struct step_hook *hook)
  163. {
  164. write_lock(&step_hook_lock);
  165. list_del(&hook->node);
  166. write_unlock(&step_hook_lock);
  167. }
  168. /*
  169. * Call registered single step handers
  170. * There is no Syndrome info to check for determining the handler.
  171. * So we call all the registered handlers, until the right handler is
  172. * found which returns zero.
  173. */
  174. static int call_step_hook(struct pt_regs *regs, unsigned int esr)
  175. {
  176. struct step_hook *hook;
  177. int retval = DBG_HOOK_ERROR;
  178. read_lock(&step_hook_lock);
  179. list_for_each_entry(hook, &step_hook, node) {
  180. retval = hook->fn(regs, esr);
  181. if (retval == DBG_HOOK_HANDLED)
  182. break;
  183. }
  184. read_unlock(&step_hook_lock);
  185. return retval;
  186. }
  187. static int single_step_handler(unsigned long addr, unsigned int esr,
  188. struct pt_regs *regs)
  189. {
  190. siginfo_t info;
  191. /*
  192. * If we are stepping a pending breakpoint, call the hw_breakpoint
  193. * handler first.
  194. */
  195. if (!reinstall_suspended_bps(regs))
  196. return 0;
  197. if (user_mode(regs)) {
  198. info.si_signo = SIGTRAP;
  199. info.si_errno = 0;
  200. info.si_code = TRAP_HWBKPT;
  201. info.si_addr = (void __user *)instruction_pointer(regs);
  202. force_sig_info(SIGTRAP, &info, current);
  203. /*
  204. * ptrace will disable single step unless explicitly
  205. * asked to re-enable it. For other clients, it makes
  206. * sense to leave it enabled (i.e. rewind the controls
  207. * to the active-not-pending state).
  208. */
  209. user_rewind_single_step(current);
  210. } else {
  211. if (call_step_hook(regs, esr) == DBG_HOOK_HANDLED)
  212. return 0;
  213. pr_warning("Unexpected kernel single-step exception at EL1\n");
  214. /*
  215. * Re-enable stepping since we know that we will be
  216. * returning to regs.
  217. */
  218. set_regs_spsr_ss(regs);
  219. }
  220. return 0;
  221. }
  222. /*
  223. * Breakpoint handler is re-entrant as another breakpoint can
  224. * hit within breakpoint handler, especically in kprobes.
  225. * Use reader/writer locks instead of plain spinlock.
  226. */
  227. static LIST_HEAD(break_hook);
  228. static DEFINE_RWLOCK(break_hook_lock);
  229. void register_break_hook(struct break_hook *hook)
  230. {
  231. write_lock(&break_hook_lock);
  232. list_add(&hook->node, &break_hook);
  233. write_unlock(&break_hook_lock);
  234. }
  235. void unregister_break_hook(struct break_hook *hook)
  236. {
  237. write_lock(&break_hook_lock);
  238. list_del(&hook->node);
  239. write_unlock(&break_hook_lock);
  240. }
  241. static int call_break_hook(struct pt_regs *regs, unsigned int esr)
  242. {
  243. struct break_hook *hook;
  244. int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
  245. read_lock(&break_hook_lock);
  246. list_for_each_entry(hook, &break_hook, node)
  247. if ((esr & hook->esr_mask) == hook->esr_val)
  248. fn = hook->fn;
  249. read_unlock(&break_hook_lock);
  250. return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
  251. }
  252. static int brk_handler(unsigned long addr, unsigned int esr,
  253. struct pt_regs *regs)
  254. {
  255. siginfo_t info;
  256. if (user_mode(regs)) {
  257. info = (siginfo_t) {
  258. .si_signo = SIGTRAP,
  259. .si_errno = 0,
  260. .si_code = TRAP_BRKPT,
  261. .si_addr = (void __user *)instruction_pointer(regs),
  262. };
  263. force_sig_info(SIGTRAP, &info, current);
  264. } else if (call_break_hook(regs, esr) != DBG_HOOK_HANDLED) {
  265. pr_warning("Unexpected kernel BRK exception at EL1\n");
  266. return -EFAULT;
  267. }
  268. return 0;
  269. }
  270. int aarch32_break_handler(struct pt_regs *regs)
  271. {
  272. siginfo_t info;
  273. u32 arm_instr;
  274. u16 thumb_instr;
  275. bool bp = false;
  276. void __user *pc = (void __user *)instruction_pointer(regs);
  277. if (!compat_user_mode(regs))
  278. return -EFAULT;
  279. if (compat_thumb_mode(regs)) {
  280. /* get 16-bit Thumb instruction */
  281. get_user(thumb_instr, (u16 __user *)pc);
  282. thumb_instr = le16_to_cpu(thumb_instr);
  283. if (thumb_instr == AARCH32_BREAK_THUMB2_LO) {
  284. /* get second half of 32-bit Thumb-2 instruction */
  285. get_user(thumb_instr, (u16 __user *)(pc + 2));
  286. thumb_instr = le16_to_cpu(thumb_instr);
  287. bp = thumb_instr == AARCH32_BREAK_THUMB2_HI;
  288. } else {
  289. bp = thumb_instr == AARCH32_BREAK_THUMB;
  290. }
  291. } else {
  292. /* 32-bit ARM instruction */
  293. get_user(arm_instr, (u32 __user *)pc);
  294. arm_instr = le32_to_cpu(arm_instr);
  295. bp = (arm_instr & ~0xf0000000) == AARCH32_BREAK_ARM;
  296. }
  297. if (!bp)
  298. return -EFAULT;
  299. info = (siginfo_t) {
  300. .si_signo = SIGTRAP,
  301. .si_errno = 0,
  302. .si_code = TRAP_BRKPT,
  303. .si_addr = pc,
  304. };
  305. force_sig_info(SIGTRAP, &info, current);
  306. return 0;
  307. }
  308. static int __init debug_traps_init(void)
  309. {
  310. hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
  311. TRAP_HWBKPT, "single-step handler");
  312. hook_debug_fault_code(DBG_ESR_EVT_BRK, brk_handler, SIGTRAP,
  313. TRAP_BRKPT, "ptrace BRK handler");
  314. return 0;
  315. }
  316. arch_initcall(debug_traps_init);
  317. /* Re-enable single step for syscall restarting. */
  318. void user_rewind_single_step(struct task_struct *task)
  319. {
  320. /*
  321. * If single step is active for this thread, then set SPSR.SS
  322. * to 1 to avoid returning to the active-pending state.
  323. */
  324. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  325. set_regs_spsr_ss(task_pt_regs(task));
  326. }
  327. void user_fastforward_single_step(struct task_struct *task)
  328. {
  329. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  330. clear_regs_spsr_ss(task_pt_regs(task));
  331. }
  332. /* Kernel API */
  333. void kernel_enable_single_step(struct pt_regs *regs)
  334. {
  335. WARN_ON(!irqs_disabled());
  336. set_regs_spsr_ss(regs);
  337. mdscr_write(mdscr_read() | DBG_MDSCR_SS);
  338. enable_debug_monitors(DBG_ACTIVE_EL1);
  339. }
  340. void kernel_disable_single_step(void)
  341. {
  342. WARN_ON(!irqs_disabled());
  343. mdscr_write(mdscr_read() & ~DBG_MDSCR_SS);
  344. disable_debug_monitors(DBG_ACTIVE_EL1);
  345. }
  346. int kernel_active_single_step(void)
  347. {
  348. WARN_ON(!irqs_disabled());
  349. return mdscr_read() & DBG_MDSCR_SS;
  350. }
  351. /* ptrace API */
  352. void user_enable_single_step(struct task_struct *task)
  353. {
  354. set_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  355. set_regs_spsr_ss(task_pt_regs(task));
  356. }
  357. void user_disable_single_step(struct task_struct *task)
  358. {
  359. clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  360. }