traps.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Based on arch/arm/kernel/traps.c
  3. *
  4. * Copyright (C) 1995-2009 Russell King
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/signal.h>
  20. #include <linux/personality.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/kdebug.h>
  26. #include <linux/module.h>
  27. #include <linux/kexec.h>
  28. #include <linux/delay.h>
  29. #include <linux/init.h>
  30. #include <linux/sched.h>
  31. #include <linux/syscalls.h>
  32. #include <asm/atomic.h>
  33. #include <asm/traps.h>
  34. #include <asm/stacktrace.h>
  35. #include <asm/exception.h>
  36. #include <asm/system_misc.h>
  37. static const char *handler[]= {
  38. "Synchronous Abort",
  39. "IRQ",
  40. "FIQ",
  41. "Error"
  42. };
  43. int show_unhandled_signals = 1;
  44. /*
  45. * Dump out the contents of some memory nicely...
  46. */
  47. static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
  48. unsigned long top)
  49. {
  50. unsigned long first;
  51. mm_segment_t fs;
  52. int i;
  53. /*
  54. * We need to switch to kernel mode so that we can use __get_user
  55. * to safely read from kernel space. Note that we now dump the
  56. * code first, just in case the backtrace kills us.
  57. */
  58. fs = get_fs();
  59. set_fs(KERNEL_DS);
  60. printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);
  61. for (first = bottom & ~31; first < top; first += 32) {
  62. unsigned long p;
  63. char str[sizeof(" 12345678") * 8 + 1];
  64. memset(str, ' ', sizeof(str));
  65. str[sizeof(str) - 1] = '\0';
  66. for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
  67. if (p >= bottom && p < top) {
  68. unsigned int val;
  69. if (__get_user(val, (unsigned int *)p) == 0)
  70. sprintf(str + i * 9, " %08x", val);
  71. else
  72. sprintf(str + i * 9, " ????????");
  73. }
  74. }
  75. printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
  76. }
  77. set_fs(fs);
  78. }
  79. static void dump_backtrace_entry(unsigned long where, unsigned long stack)
  80. {
  81. print_ip_sym(where);
  82. if (in_exception_text(where))
  83. dump_mem("", "Exception stack", stack,
  84. stack + sizeof(struct pt_regs));
  85. }
  86. static void dump_instr(const char *lvl, struct pt_regs *regs)
  87. {
  88. unsigned long addr = instruction_pointer(regs);
  89. mm_segment_t fs;
  90. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  91. int i;
  92. /*
  93. * We need to switch to kernel mode so that we can use __get_user
  94. * to safely read from kernel space. Note that we now dump the
  95. * code first, just in case the backtrace kills us.
  96. */
  97. fs = get_fs();
  98. set_fs(KERNEL_DS);
  99. for (i = -4; i < 1; i++) {
  100. unsigned int val, bad;
  101. bad = __get_user(val, &((u32 *)addr)[i]);
  102. if (!bad)
  103. p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
  104. else {
  105. p += sprintf(p, "bad PC value");
  106. break;
  107. }
  108. }
  109. printk("%sCode: %s\n", lvl, str);
  110. set_fs(fs);
  111. }
  112. static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  113. {
  114. struct stackframe frame;
  115. const register unsigned long current_sp asm ("sp");
  116. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  117. if (!tsk)
  118. tsk = current;
  119. if (regs) {
  120. frame.fp = regs->regs[29];
  121. frame.sp = regs->sp;
  122. frame.pc = regs->pc;
  123. } else if (tsk == current) {
  124. frame.fp = (unsigned long)__builtin_frame_address(0);
  125. frame.sp = current_sp;
  126. frame.pc = (unsigned long)dump_backtrace;
  127. } else {
  128. /*
  129. * task blocked in __switch_to
  130. */
  131. frame.fp = thread_saved_fp(tsk);
  132. frame.sp = thread_saved_sp(tsk);
  133. frame.pc = thread_saved_pc(tsk);
  134. }
  135. printk("Call trace:\n");
  136. while (1) {
  137. unsigned long where = frame.pc;
  138. int ret;
  139. ret = unwind_frame(&frame);
  140. if (ret < 0)
  141. break;
  142. dump_backtrace_entry(where, frame.sp);
  143. }
  144. }
  145. void dump_stack(void)
  146. {
  147. dump_backtrace(NULL, NULL);
  148. }
  149. EXPORT_SYMBOL(dump_stack);
  150. void show_stack(struct task_struct *tsk, unsigned long *sp)
  151. {
  152. dump_backtrace(NULL, tsk);
  153. barrier();
  154. }
  155. #ifdef CONFIG_PREEMPT
  156. #define S_PREEMPT " PREEMPT"
  157. #else
  158. #define S_PREEMPT ""
  159. #endif
  160. #ifdef CONFIG_SMP
  161. #define S_SMP " SMP"
  162. #else
  163. #define S_SMP ""
  164. #endif
  165. static int __die(const char *str, int err, struct thread_info *thread,
  166. struct pt_regs *regs)
  167. {
  168. struct task_struct *tsk = thread->task;
  169. static int die_counter;
  170. int ret;
  171. pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
  172. str, err, ++die_counter);
  173. /* trap and error numbers are mostly meaningless on ARM */
  174. ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
  175. if (ret == NOTIFY_STOP)
  176. return ret;
  177. print_modules();
  178. __show_regs(regs);
  179. pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
  180. TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
  181. if (!user_mode(regs) || in_interrupt()) {
  182. dump_mem(KERN_EMERG, "Stack: ", regs->sp,
  183. THREAD_SIZE + (unsigned long)task_stack_page(tsk));
  184. dump_backtrace(regs, tsk);
  185. dump_instr(KERN_EMERG, regs);
  186. }
  187. return ret;
  188. }
  189. static DEFINE_RAW_SPINLOCK(die_lock);
  190. /*
  191. * This function is protected against re-entrancy.
  192. */
  193. void die(const char *str, struct pt_regs *regs, int err)
  194. {
  195. struct thread_info *thread = current_thread_info();
  196. int ret;
  197. oops_enter();
  198. raw_spin_lock_irq(&die_lock);
  199. console_verbose();
  200. bust_spinlocks(1);
  201. ret = __die(str, err, thread, regs);
  202. if (regs && kexec_should_crash(thread->task))
  203. crash_kexec(regs);
  204. bust_spinlocks(0);
  205. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  206. raw_spin_unlock_irq(&die_lock);
  207. oops_exit();
  208. if (in_interrupt())
  209. panic("Fatal exception in interrupt");
  210. if (panic_on_oops)
  211. panic("Fatal exception");
  212. if (ret != NOTIFY_STOP)
  213. do_exit(SIGSEGV);
  214. }
  215. void arm64_notify_die(const char *str, struct pt_regs *regs,
  216. struct siginfo *info, int err)
  217. {
  218. if (user_mode(regs))
  219. force_sig_info(info->si_signo, info, current);
  220. else
  221. die(str, regs, err);
  222. }
  223. asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
  224. {
  225. siginfo_t info;
  226. void __user *pc = (void __user *)instruction_pointer(regs);
  227. #ifdef CONFIG_COMPAT
  228. /* check for AArch32 breakpoint instructions */
  229. if (compat_user_mode(regs) && aarch32_break_trap(regs) == 0)
  230. return;
  231. #endif
  232. if (show_unhandled_signals) {
  233. pr_info("%s[%d]: undefined instruction: pc=%p\n",
  234. current->comm, task_pid_nr(current), pc);
  235. dump_instr(KERN_INFO, regs);
  236. }
  237. info.si_signo = SIGILL;
  238. info.si_errno = 0;
  239. info.si_code = ILL_ILLOPC;
  240. info.si_addr = pc;
  241. arm64_notify_die("Oops - undefined instruction", regs, &info, 0);
  242. }
  243. long compat_arm_syscall(struct pt_regs *regs);
  244. asmlinkage long do_ni_syscall(struct pt_regs *regs)
  245. {
  246. #ifdef CONFIG_COMPAT
  247. long ret;
  248. if (is_compat_task()) {
  249. ret = compat_arm_syscall(regs);
  250. if (ret != -ENOSYS)
  251. return ret;
  252. }
  253. #endif
  254. if (show_unhandled_signals) {
  255. pr_info("%s[%d]: syscall %d\n", current->comm,
  256. task_pid_nr(current), (int)regs->syscallno);
  257. dump_instr("", regs);
  258. if (user_mode(regs))
  259. __show_regs(regs);
  260. }
  261. return sys_ni_syscall();
  262. }
  263. /*
  264. * bad_mode handles the impossible case in the exception vector.
  265. */
  266. asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
  267. {
  268. console_verbose();
  269. pr_crit("Bad mode in %s handler detected, code 0x%08x\n",
  270. handler[reason], esr);
  271. die("Oops - bad mode", regs, 0);
  272. local_irq_disable();
  273. panic("bad mode");
  274. }
  275. void __pte_error(const char *file, int line, unsigned long val)
  276. {
  277. printk("%s:%d: bad pte %016lx.\n", file, line, val);
  278. }
  279. void __pmd_error(const char *file, int line, unsigned long val)
  280. {
  281. printk("%s:%d: bad pmd %016lx.\n", file, line, val);
  282. }
  283. void __pgd_error(const char *file, int line, unsigned long val)
  284. {
  285. printk("%s:%d: bad pgd %016lx.\n", file, line, val);
  286. }
  287. void __init trap_init(void)
  288. {
  289. return;
  290. }