debug-monitors.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. isb();
  118. }
  119. static int os_lock_notify(struct notifier_block *self,
  120. unsigned long action, void *data)
  121. {
  122. int cpu = (unsigned long)data;
  123. if (action == CPU_ONLINE)
  124. smp_call_function_single(cpu, clear_os_lock, NULL, 1);
  125. return NOTIFY_OK;
  126. }
  127. static struct notifier_block os_lock_nb = {
  128. .notifier_call = os_lock_notify,
  129. };
  130. static int debug_monitors_init(void)
  131. {
  132. /* Clear the OS lock. */
  133. smp_call_function(clear_os_lock, NULL, 1);
  134. clear_os_lock(NULL);
  135. /* Register hotplug handler. */
  136. register_cpu_notifier(&os_lock_nb);
  137. return 0;
  138. }
  139. postcore_initcall(debug_monitors_init);
  140. /*
  141. * Single step API and exception handling.
  142. */
  143. static void set_regs_spsr_ss(struct pt_regs *regs)
  144. {
  145. unsigned long spsr;
  146. spsr = regs->pstate;
  147. spsr &= ~DBG_SPSR_SS;
  148. spsr |= DBG_SPSR_SS;
  149. regs->pstate = spsr;
  150. }
  151. static void clear_regs_spsr_ss(struct pt_regs *regs)
  152. {
  153. unsigned long spsr;
  154. spsr = regs->pstate;
  155. spsr &= ~DBG_SPSR_SS;
  156. regs->pstate = spsr;
  157. }
  158. /* EL1 Single Step Handler hooks */
  159. static LIST_HEAD(step_hook);
  160. DEFINE_RWLOCK(step_hook_lock);
  161. void register_step_hook(struct step_hook *hook)
  162. {
  163. write_lock(&step_hook_lock);
  164. list_add(&hook->node, &step_hook);
  165. write_unlock(&step_hook_lock);
  166. }
  167. void unregister_step_hook(struct step_hook *hook)
  168. {
  169. write_lock(&step_hook_lock);
  170. list_del(&hook->node);
  171. write_unlock(&step_hook_lock);
  172. }
  173. /*
  174. * Call registered single step handers
  175. * There is no Syndrome info to check for determining the handler.
  176. * So we call all the registered handlers, until the right handler is
  177. * found which returns zero.
  178. */
  179. static int call_step_hook(struct pt_regs *regs, unsigned int esr)
  180. {
  181. struct step_hook *hook;
  182. int retval = DBG_HOOK_ERROR;
  183. read_lock(&step_hook_lock);
  184. list_for_each_entry(hook, &step_hook, node) {
  185. retval = hook->fn(regs, esr);
  186. if (retval == DBG_HOOK_HANDLED)
  187. break;
  188. }
  189. read_unlock(&step_hook_lock);
  190. return retval;
  191. }
  192. static int single_step_handler(unsigned long addr, unsigned int esr,
  193. struct pt_regs *regs)
  194. {
  195. siginfo_t info;
  196. /*
  197. * If we are stepping a pending breakpoint, call the hw_breakpoint
  198. * handler first.
  199. */
  200. if (!reinstall_suspended_bps(regs))
  201. return 0;
  202. if (user_mode(regs)) {
  203. info.si_signo = SIGTRAP;
  204. info.si_errno = 0;
  205. info.si_code = TRAP_HWBKPT;
  206. info.si_addr = (void __user *)instruction_pointer(regs);
  207. force_sig_info(SIGTRAP, &info, current);
  208. /*
  209. * ptrace will disable single step unless explicitly
  210. * asked to re-enable it. For other clients, it makes
  211. * sense to leave it enabled (i.e. rewind the controls
  212. * to the active-not-pending state).
  213. */
  214. user_rewind_single_step(current);
  215. } else {
  216. if (call_step_hook(regs, esr) == DBG_HOOK_HANDLED)
  217. return 0;
  218. pr_warning("Unexpected kernel single-step exception at EL1\n");
  219. /*
  220. * Re-enable stepping since we know that we will be
  221. * returning to regs.
  222. */
  223. set_regs_spsr_ss(regs);
  224. }
  225. return 0;
  226. }
  227. /*
  228. * Breakpoint handler is re-entrant as another breakpoint can
  229. * hit within breakpoint handler, especically in kprobes.
  230. * Use reader/writer locks instead of plain spinlock.
  231. */
  232. static LIST_HEAD(break_hook);
  233. DEFINE_RWLOCK(break_hook_lock);
  234. void register_break_hook(struct break_hook *hook)
  235. {
  236. write_lock(&break_hook_lock);
  237. list_add(&hook->node, &break_hook);
  238. write_unlock(&break_hook_lock);
  239. }
  240. void unregister_break_hook(struct break_hook *hook)
  241. {
  242. write_lock(&break_hook_lock);
  243. list_del(&hook->node);
  244. write_unlock(&break_hook_lock);
  245. }
  246. static int call_break_hook(struct pt_regs *regs, unsigned int esr)
  247. {
  248. struct break_hook *hook;
  249. int (*fn)(struct pt_regs *regs, unsigned int esr) = NULL;
  250. read_lock(&break_hook_lock);
  251. list_for_each_entry(hook, &break_hook, node)
  252. if ((esr & hook->esr_mask) == hook->esr_val)
  253. fn = hook->fn;
  254. read_unlock(&break_hook_lock);
  255. return fn ? fn(regs, esr) : DBG_HOOK_ERROR;
  256. }
  257. static int brk_handler(unsigned long addr, unsigned int esr,
  258. struct pt_regs *regs)
  259. {
  260. siginfo_t info;
  261. if (call_break_hook(regs, esr) == DBG_HOOK_HANDLED)
  262. return 0;
  263. pr_warn("unexpected brk exception at %lx, esr=0x%x\n",
  264. (long)instruction_pointer(regs), esr);
  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. }