debug-monitors.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 <asm/debug-monitors.h>
  27. #include <asm/local.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(local_t, mde_ref_count);
  80. static DEFINE_PER_CPU(local_t, kde_ref_count);
  81. void enable_debug_monitors(enum debug_el el)
  82. {
  83. u32 mdscr, enable = 0;
  84. WARN_ON(preemptible());
  85. if (local_inc_return(&__get_cpu_var(mde_ref_count)) == 1)
  86. enable = DBG_MDSCR_MDE;
  87. if (el == DBG_ACTIVE_EL1 &&
  88. local_inc_return(&__get_cpu_var(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 (local_dec_and_test(&__get_cpu_var(mde_ref_count)))
  101. disable = ~DBG_MDSCR_MDE;
  102. if (el == DBG_ACTIVE_EL1 &&
  103. local_dec_and_test(&__get_cpu_var(kde_ref_count)))
  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 mdscr_el1, %0" : : "r" (0));
  117. isb();
  118. asm volatile("msr oslar_el1, %0" : : "r" (0));
  119. isb();
  120. }
  121. static int __cpuinit os_lock_notify(struct notifier_block *self,
  122. unsigned long action, void *data)
  123. {
  124. int cpu = (unsigned long)data;
  125. if (action == CPU_ONLINE)
  126. smp_call_function_single(cpu, clear_os_lock, NULL, 1);
  127. return NOTIFY_OK;
  128. }
  129. static struct notifier_block __cpuinitdata os_lock_nb = {
  130. .notifier_call = os_lock_notify,
  131. };
  132. static int __cpuinit debug_monitors_init(void)
  133. {
  134. /* Clear the OS lock. */
  135. smp_call_function(clear_os_lock, NULL, 1);
  136. clear_os_lock(NULL);
  137. /* Register hotplug handler. */
  138. register_cpu_notifier(&os_lock_nb);
  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. static int single_step_handler(unsigned long addr, unsigned int esr,
  161. struct pt_regs *regs)
  162. {
  163. siginfo_t info;
  164. /*
  165. * If we are stepping a pending breakpoint, call the hw_breakpoint
  166. * handler first.
  167. */
  168. if (!reinstall_suspended_bps(regs))
  169. return 0;
  170. if (user_mode(regs)) {
  171. info.si_signo = SIGTRAP;
  172. info.si_errno = 0;
  173. info.si_code = TRAP_HWBKPT;
  174. info.si_addr = (void __user *)instruction_pointer(regs);
  175. force_sig_info(SIGTRAP, &info, current);
  176. /*
  177. * ptrace will disable single step unless explicitly
  178. * asked to re-enable it. For other clients, it makes
  179. * sense to leave it enabled (i.e. rewind the controls
  180. * to the active-not-pending state).
  181. */
  182. user_rewind_single_step(current);
  183. } else {
  184. /* TODO: route to KGDB */
  185. pr_warning("Unexpected kernel single-step exception at EL1\n");
  186. /*
  187. * Re-enable stepping since we know that we will be
  188. * returning to regs.
  189. */
  190. set_regs_spsr_ss(regs);
  191. }
  192. return 0;
  193. }
  194. static int __init single_step_init(void)
  195. {
  196. hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
  197. TRAP_HWBKPT, "single-step handler");
  198. return 0;
  199. }
  200. arch_initcall(single_step_init);
  201. /* Re-enable single step for syscall restarting. */
  202. void user_rewind_single_step(struct task_struct *task)
  203. {
  204. /*
  205. * If single step is active for this thread, then set SPSR.SS
  206. * to 1 to avoid returning to the active-pending state.
  207. */
  208. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  209. set_regs_spsr_ss(task_pt_regs(task));
  210. }
  211. void user_fastforward_single_step(struct task_struct *task)
  212. {
  213. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  214. clear_regs_spsr_ss(task_pt_regs(task));
  215. }
  216. /* Kernel API */
  217. void kernel_enable_single_step(struct pt_regs *regs)
  218. {
  219. WARN_ON(!irqs_disabled());
  220. set_regs_spsr_ss(regs);
  221. mdscr_write(mdscr_read() | DBG_MDSCR_SS);
  222. enable_debug_monitors(DBG_ACTIVE_EL1);
  223. }
  224. void kernel_disable_single_step(void)
  225. {
  226. WARN_ON(!irqs_disabled());
  227. mdscr_write(mdscr_read() & ~DBG_MDSCR_SS);
  228. disable_debug_monitors(DBG_ACTIVE_EL1);
  229. }
  230. int kernel_active_single_step(void)
  231. {
  232. WARN_ON(!irqs_disabled());
  233. return mdscr_read() & DBG_MDSCR_SS;
  234. }
  235. /* ptrace API */
  236. void user_enable_single_step(struct task_struct *task)
  237. {
  238. set_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  239. set_regs_spsr_ss(task_pt_regs(task));
  240. }
  241. void user_disable_single_step(struct task_struct *task)
  242. {
  243. clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  244. }