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 dbg_active_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 dbg_active_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_TASKS_FROZEN) == 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 handlers
  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_SPINLOCK(break_hook_lock);
  229. void register_break_hook(struct break_hook *hook)
  230. {
  231. spin_lock(&break_hook_lock);
  232. list_add_rcu(&hook->node, &break_hook);
  233. spin_unlock(&break_hook_lock);
  234. }
  235. void unregister_break_hook(struct break_hook *hook)
  236. {
  237. spin_lock(&break_hook_lock);
  238. list_del_rcu(&hook->node);
  239. spin_unlock(&break_hook_lock);
  240. synchronize_rcu();
  241. }
  242. static int call_break_hook(struct pt_regs *regs, unsigned int esr)
  243. {
  244. struct break_hook *hook;
  245. int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
  246. rcu_read_lock();
  247. list_for_each_entry_rcu(hook, &break_hook, node)
  248. if ((esr & hook->esr_mask) == hook->esr_val)
  249. fn = hook->fn;
  250. rcu_read_unlock();
  251. return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
  252. }
  253. static int brk_handler(unsigned long addr, unsigned int esr,
  254. struct pt_regs *regs)
  255. {
  256. siginfo_t info;
  257. if (user_mode(regs)) {
  258. info = (siginfo_t) {
  259. .si_signo = SIGTRAP,
  260. .si_errno = 0,
  261. .si_code = TRAP_BRKPT,
  262. .si_addr = (void __user *)instruction_pointer(regs),
  263. };
  264. force_sig_info(SIGTRAP, &info, current);
  265. } else if (call_break_hook(regs, esr) != DBG_HOOK_HANDLED) {
  266. pr_warning("Unexpected kernel BRK exception at EL1\n");
  267. return -EFAULT;
  268. }
  269. return 0;
  270. }
  271. int aarch32_break_handler(struct pt_regs *regs)
  272. {
  273. siginfo_t info;
  274. u32 arm_instr;
  275. u16 thumb_instr;
  276. bool bp = false;
  277. void __user *pc = (void __user *)instruction_pointer(regs);
  278. if (!compat_user_mode(regs))
  279. return -EFAULT;
  280. if (compat_thumb_mode(regs)) {
  281. /* get 16-bit Thumb instruction */
  282. get_user(thumb_instr, (u16 __user *)pc);
  283. thumb_instr = le16_to_cpu(thumb_instr);
  284. if (thumb_instr == AARCH32_BREAK_THUMB2_LO) {
  285. /* get second half of 32-bit Thumb-2 instruction */
  286. get_user(thumb_instr, (u16 __user *)(pc + 2));
  287. thumb_instr = le16_to_cpu(thumb_instr);
  288. bp = thumb_instr == AARCH32_BREAK_THUMB2_HI;
  289. } else {
  290. bp = thumb_instr == AARCH32_BREAK_THUMB;
  291. }
  292. } else {
  293. /* 32-bit ARM instruction */
  294. get_user(arm_instr, (u32 __user *)pc);
  295. arm_instr = le32_to_cpu(arm_instr);
  296. bp = (arm_instr & ~0xf0000000) == AARCH32_BREAK_ARM;
  297. }
  298. if (!bp)
  299. return -EFAULT;
  300. info = (siginfo_t) {
  301. .si_signo = SIGTRAP,
  302. .si_errno = 0,
  303. .si_code = TRAP_BRKPT,
  304. .si_addr = pc,
  305. };
  306. force_sig_info(SIGTRAP, &info, current);
  307. return 0;
  308. }
  309. static int __init debug_traps_init(void)
  310. {
  311. hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
  312. TRAP_HWBKPT, "single-step handler");
  313. hook_debug_fault_code(DBG_ESR_EVT_BRK, brk_handler, SIGTRAP,
  314. TRAP_BRKPT, "ptrace BRK handler");
  315. return 0;
  316. }
  317. arch_initcall(debug_traps_init);
  318. /* Re-enable single step for syscall restarting. */
  319. void user_rewind_single_step(struct task_struct *task)
  320. {
  321. /*
  322. * If single step is active for this thread, then set SPSR.SS
  323. * to 1 to avoid returning to the active-pending state.
  324. */
  325. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  326. set_regs_spsr_ss(task_pt_regs(task));
  327. }
  328. void user_fastforward_single_step(struct task_struct *task)
  329. {
  330. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  331. clear_regs_spsr_ss(task_pt_regs(task));
  332. }
  333. /* Kernel API */
  334. void kernel_enable_single_step(struct pt_regs *regs)
  335. {
  336. WARN_ON(!irqs_disabled());
  337. set_regs_spsr_ss(regs);
  338. mdscr_write(mdscr_read() | DBG_MDSCR_SS);
  339. enable_debug_monitors(DBG_ACTIVE_EL1);
  340. }
  341. void kernel_disable_single_step(void)
  342. {
  343. WARN_ON(!irqs_disabled());
  344. mdscr_write(mdscr_read() & ~DBG_MDSCR_SS);
  345. disable_debug_monitors(DBG_ACTIVE_EL1);
  346. }
  347. int kernel_active_single_step(void)
  348. {
  349. WARN_ON(!irqs_disabled());
  350. return mdscr_read() & DBG_MDSCR_SS;
  351. }
  352. /* ptrace API */
  353. void user_enable_single_step(struct task_struct *task)
  354. {
  355. set_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  356. set_regs_spsr_ss(task_pt_regs(task));
  357. }
  358. void user_disable_single_step(struct task_struct *task)
  359. {
  360. clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  361. }