debug-monitors.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/local.h>
  29. #include <asm/cputype.h>
  30. #include <asm/system_misc.h>
  31. /* Low-level stepping controls. */
  32. #define DBG_MDSCR_SS (1 << 0)
  33. #define DBG_SPSR_SS (1 << 21)
  34. /* MDSCR_EL1 enabling bits */
  35. #define DBG_MDSCR_KDE (1 << 13)
  36. #define DBG_MDSCR_MDE (1 << 15)
  37. #define DBG_MDSCR_MASK ~(DBG_MDSCR_KDE | DBG_MDSCR_MDE)
  38. /* Determine debug architecture. */
  39. u8 debug_monitors_arch(void)
  40. {
  41. return read_cpuid(ID_AA64DFR0_EL1) & 0xf;
  42. }
  43. /*
  44. * MDSCR access routines.
  45. */
  46. static void mdscr_write(u32 mdscr)
  47. {
  48. unsigned long flags;
  49. local_dbg_save(flags);
  50. asm volatile("msr mdscr_el1, %0" :: "r" (mdscr));
  51. local_dbg_restore(flags);
  52. }
  53. static u32 mdscr_read(void)
  54. {
  55. u32 mdscr;
  56. asm volatile("mrs %0, mdscr_el1" : "=r" (mdscr));
  57. return mdscr;
  58. }
  59. /*
  60. * Allow root to disable self-hosted debug from userspace.
  61. * This is useful if you want to connect an external JTAG debugger.
  62. */
  63. static u32 debug_enabled = 1;
  64. static int create_debug_debugfs_entry(void)
  65. {
  66. debugfs_create_bool("debug_enabled", 0644, NULL, &debug_enabled);
  67. return 0;
  68. }
  69. fs_initcall(create_debug_debugfs_entry);
  70. static int __init early_debug_disable(char *buf)
  71. {
  72. debug_enabled = 0;
  73. return 0;
  74. }
  75. early_param("nodebugmon", early_debug_disable);
  76. /*
  77. * Keep track of debug users on each core.
  78. * The ref counts are per-cpu so we use a local_t type.
  79. */
  80. static DEFINE_PER_CPU(local_t, mde_ref_count);
  81. static DEFINE_PER_CPU(local_t, kde_ref_count);
  82. void enable_debug_monitors(enum debug_el el)
  83. {
  84. u32 mdscr, enable = 0;
  85. WARN_ON(preemptible());
  86. if (local_inc_return(&__get_cpu_var(mde_ref_count)) == 1)
  87. enable = DBG_MDSCR_MDE;
  88. if (el == DBG_ACTIVE_EL1 &&
  89. local_inc_return(&__get_cpu_var(kde_ref_count)) == 1)
  90. enable |= DBG_MDSCR_KDE;
  91. if (enable && debug_enabled) {
  92. mdscr = mdscr_read();
  93. mdscr |= enable;
  94. mdscr_write(mdscr);
  95. }
  96. }
  97. void disable_debug_monitors(enum debug_el el)
  98. {
  99. u32 mdscr, disable = 0;
  100. WARN_ON(preemptible());
  101. if (local_dec_and_test(&__get_cpu_var(mde_ref_count)))
  102. disable = ~DBG_MDSCR_MDE;
  103. if (el == DBG_ACTIVE_EL1 &&
  104. local_dec_and_test(&__get_cpu_var(kde_ref_count)))
  105. disable &= ~DBG_MDSCR_KDE;
  106. if (disable) {
  107. mdscr = mdscr_read();
  108. mdscr &= disable;
  109. mdscr_write(mdscr);
  110. }
  111. }
  112. /*
  113. * OS lock clearing.
  114. */
  115. static void clear_os_lock(void *unused)
  116. {
  117. asm volatile("msr oslar_el1, %0" : : "r" (0));
  118. isb();
  119. }
  120. static int os_lock_notify(struct notifier_block *self,
  121. unsigned long action, void *data)
  122. {
  123. int cpu = (unsigned long)data;
  124. if (action == CPU_ONLINE)
  125. smp_call_function_single(cpu, clear_os_lock, NULL, 1);
  126. return NOTIFY_OK;
  127. }
  128. static struct notifier_block os_lock_nb = {
  129. .notifier_call = os_lock_notify,
  130. };
  131. static int debug_monitors_init(void)
  132. {
  133. /* Clear the OS lock. */
  134. smp_call_function(clear_os_lock, NULL, 1);
  135. clear_os_lock(NULL);
  136. /* Register hotplug handler. */
  137. register_cpu_notifier(&os_lock_nb);
  138. return 0;
  139. }
  140. postcore_initcall(debug_monitors_init);
  141. /*
  142. * Single step API and exception handling.
  143. */
  144. static void set_regs_spsr_ss(struct pt_regs *regs)
  145. {
  146. unsigned long spsr;
  147. spsr = regs->pstate;
  148. spsr &= ~DBG_SPSR_SS;
  149. spsr |= DBG_SPSR_SS;
  150. regs->pstate = spsr;
  151. }
  152. static void clear_regs_spsr_ss(struct pt_regs *regs)
  153. {
  154. unsigned long spsr;
  155. spsr = regs->pstate;
  156. spsr &= ~DBG_SPSR_SS;
  157. regs->pstate = spsr;
  158. }
  159. static int single_step_handler(unsigned long addr, unsigned int esr,
  160. struct pt_regs *regs)
  161. {
  162. siginfo_t info;
  163. /*
  164. * If we are stepping a pending breakpoint, call the hw_breakpoint
  165. * handler first.
  166. */
  167. if (!reinstall_suspended_bps(regs))
  168. return 0;
  169. if (user_mode(regs)) {
  170. info.si_signo = SIGTRAP;
  171. info.si_errno = 0;
  172. info.si_code = TRAP_HWBKPT;
  173. info.si_addr = (void __user *)instruction_pointer(regs);
  174. force_sig_info(SIGTRAP, &info, current);
  175. /*
  176. * ptrace will disable single step unless explicitly
  177. * asked to re-enable it. For other clients, it makes
  178. * sense to leave it enabled (i.e. rewind the controls
  179. * to the active-not-pending state).
  180. */
  181. user_rewind_single_step(current);
  182. } else {
  183. /* TODO: route to KGDB */
  184. pr_warning("Unexpected kernel single-step exception at EL1\n");
  185. /*
  186. * Re-enable stepping since we know that we will be
  187. * returning to regs.
  188. */
  189. set_regs_spsr_ss(regs);
  190. }
  191. return 0;
  192. }
  193. static int brk_handler(unsigned long addr, unsigned int esr,
  194. struct pt_regs *regs)
  195. {
  196. siginfo_t info;
  197. if (!user_mode(regs))
  198. return -EFAULT;
  199. info = (siginfo_t) {
  200. .si_signo = SIGTRAP,
  201. .si_errno = 0,
  202. .si_code = TRAP_BRKPT,
  203. .si_addr = (void __user *)instruction_pointer(regs),
  204. };
  205. force_sig_info(SIGTRAP, &info, current);
  206. return 0;
  207. }
  208. int aarch32_break_handler(struct pt_regs *regs)
  209. {
  210. siginfo_t info;
  211. unsigned int instr;
  212. bool bp = false;
  213. void __user *pc = (void __user *)instruction_pointer(regs);
  214. if (!compat_user_mode(regs))
  215. return -EFAULT;
  216. if (compat_thumb_mode(regs)) {
  217. /* get 16-bit Thumb instruction */
  218. get_user(instr, (u16 __user *)pc);
  219. if (instr == AARCH32_BREAK_THUMB2_LO) {
  220. /* get second half of 32-bit Thumb-2 instruction */
  221. get_user(instr, (u16 __user *)(pc + 2));
  222. bp = instr == AARCH32_BREAK_THUMB2_HI;
  223. } else {
  224. bp = instr == AARCH32_BREAK_THUMB;
  225. }
  226. } else {
  227. /* 32-bit ARM instruction */
  228. get_user(instr, (u32 __user *)pc);
  229. bp = (instr & ~0xf0000000) == AARCH32_BREAK_ARM;
  230. }
  231. if (!bp)
  232. return -EFAULT;
  233. info = (siginfo_t) {
  234. .si_signo = SIGTRAP,
  235. .si_errno = 0,
  236. .si_code = TRAP_BRKPT,
  237. .si_addr = pc,
  238. };
  239. force_sig_info(SIGTRAP, &info, current);
  240. return 0;
  241. }
  242. static int __init debug_traps_init(void)
  243. {
  244. hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
  245. TRAP_HWBKPT, "single-step handler");
  246. hook_debug_fault_code(DBG_ESR_EVT_BRK, brk_handler, SIGTRAP,
  247. TRAP_BRKPT, "ptrace BRK handler");
  248. return 0;
  249. }
  250. arch_initcall(debug_traps_init);
  251. /* Re-enable single step for syscall restarting. */
  252. void user_rewind_single_step(struct task_struct *task)
  253. {
  254. /*
  255. * If single step is active for this thread, then set SPSR.SS
  256. * to 1 to avoid returning to the active-pending state.
  257. */
  258. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  259. set_regs_spsr_ss(task_pt_regs(task));
  260. }
  261. void user_fastforward_single_step(struct task_struct *task)
  262. {
  263. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  264. clear_regs_spsr_ss(task_pt_regs(task));
  265. }
  266. /* Kernel API */
  267. void kernel_enable_single_step(struct pt_regs *regs)
  268. {
  269. WARN_ON(!irqs_disabled());
  270. set_regs_spsr_ss(regs);
  271. mdscr_write(mdscr_read() | DBG_MDSCR_SS);
  272. enable_debug_monitors(DBG_ACTIVE_EL1);
  273. }
  274. void kernel_disable_single_step(void)
  275. {
  276. WARN_ON(!irqs_disabled());
  277. mdscr_write(mdscr_read() & ~DBG_MDSCR_SS);
  278. disable_debug_monitors(DBG_ACTIVE_EL1);
  279. }
  280. int kernel_active_single_step(void)
  281. {
  282. WARN_ON(!irqs_disabled());
  283. return mdscr_read() & DBG_MDSCR_SS;
  284. }
  285. /* ptrace API */
  286. void user_enable_single_step(struct task_struct *task)
  287. {
  288. set_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  289. set_regs_spsr_ss(task_pt_regs(task));
  290. }
  291. void user_disable_single_step(struct task_struct *task)
  292. {
  293. clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  294. }