debug-monitors.c 9.9 KB

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