debug-monitors.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 oslar_el1, %0" : : "r" (0));
  117. isb();
  118. }
  119. static int __cpuinit 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 __cpuinitdata os_lock_nb = {
  128. .notifier_call = os_lock_notify,
  129. };
  130. static int __cpuinit 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. static int single_step_handler(unsigned long addr, unsigned int esr,
  159. struct pt_regs *regs)
  160. {
  161. siginfo_t info;
  162. /*
  163. * If we are stepping a pending breakpoint, call the hw_breakpoint
  164. * handler first.
  165. */
  166. if (!reinstall_suspended_bps(regs))
  167. return 0;
  168. if (user_mode(regs)) {
  169. info.si_signo = SIGTRAP;
  170. info.si_errno = 0;
  171. info.si_code = TRAP_HWBKPT;
  172. info.si_addr = (void __user *)instruction_pointer(regs);
  173. force_sig_info(SIGTRAP, &info, current);
  174. /*
  175. * ptrace will disable single step unless explicitly
  176. * asked to re-enable it. For other clients, it makes
  177. * sense to leave it enabled (i.e. rewind the controls
  178. * to the active-not-pending state).
  179. */
  180. user_rewind_single_step(current);
  181. } else {
  182. /* TODO: route to KGDB */
  183. pr_warning("Unexpected kernel single-step exception at EL1\n");
  184. /*
  185. * Re-enable stepping since we know that we will be
  186. * returning to regs.
  187. */
  188. set_regs_spsr_ss(regs);
  189. }
  190. return 0;
  191. }
  192. static int __init single_step_init(void)
  193. {
  194. hook_debug_fault_code(DBG_ESR_EVT_HWSS, single_step_handler, SIGTRAP,
  195. TRAP_HWBKPT, "single-step handler");
  196. return 0;
  197. }
  198. arch_initcall(single_step_init);
  199. /* Re-enable single step for syscall restarting. */
  200. void user_rewind_single_step(struct task_struct *task)
  201. {
  202. /*
  203. * If single step is active for this thread, then set SPSR.SS
  204. * to 1 to avoid returning to the active-pending state.
  205. */
  206. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  207. set_regs_spsr_ss(task_pt_regs(task));
  208. }
  209. void user_fastforward_single_step(struct task_struct *task)
  210. {
  211. if (test_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP))
  212. clear_regs_spsr_ss(task_pt_regs(task));
  213. }
  214. /* Kernel API */
  215. void kernel_enable_single_step(struct pt_regs *regs)
  216. {
  217. WARN_ON(!irqs_disabled());
  218. set_regs_spsr_ss(regs);
  219. mdscr_write(mdscr_read() | DBG_MDSCR_SS);
  220. enable_debug_monitors(DBG_ACTIVE_EL1);
  221. }
  222. void kernel_disable_single_step(void)
  223. {
  224. WARN_ON(!irqs_disabled());
  225. mdscr_write(mdscr_read() & ~DBG_MDSCR_SS);
  226. disable_debug_monitors(DBG_ACTIVE_EL1);
  227. }
  228. int kernel_active_single_step(void)
  229. {
  230. WARN_ON(!irqs_disabled());
  231. return mdscr_read() & DBG_MDSCR_SS;
  232. }
  233. /* ptrace API */
  234. void user_enable_single_step(struct task_struct *task)
  235. {
  236. set_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  237. set_regs_spsr_ss(task_pt_regs(task));
  238. }
  239. void user_disable_single_step(struct task_struct *task)
  240. {
  241. clear_ti_thread_flag(task_thread_info(task), TIF_SINGLESTEP);
  242. }