traps.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/debug-monitors.h>
  34. #include <asm/esr.h>
  35. #include <asm/traps.h>
  36. #include <asm/stacktrace.h>
  37. #include <asm/exception.h>
  38. #include <asm/system_misc.h>
  39. static const char *handler[]= {
  40. "Synchronous Abort",
  41. "IRQ",
  42. "FIQ",
  43. "Error"
  44. };
  45. int show_unhandled_signals = 1;
  46. /*
  47. * Dump out the contents of some memory nicely...
  48. */
  49. static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
  50. unsigned long top)
  51. {
  52. unsigned long first;
  53. mm_segment_t fs;
  54. int i;
  55. /*
  56. * We need to switch to kernel mode so that we can use __get_user
  57. * to safely read from kernel space. Note that we now dump the
  58. * code first, just in case the backtrace kills us.
  59. */
  60. fs = get_fs();
  61. set_fs(KERNEL_DS);
  62. printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);
  63. for (first = bottom & ~31; first < top; first += 32) {
  64. unsigned long p;
  65. char str[sizeof(" 12345678") * 8 + 1];
  66. memset(str, ' ', sizeof(str));
  67. str[sizeof(str) - 1] = '\0';
  68. for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
  69. if (p >= bottom && p < top) {
  70. unsigned int val;
  71. if (__get_user(val, (unsigned int *)p) == 0)
  72. sprintf(str + i * 9, " %08x", val);
  73. else
  74. sprintf(str + i * 9, " ????????");
  75. }
  76. }
  77. printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
  78. }
  79. set_fs(fs);
  80. }
  81. static void dump_backtrace_entry(unsigned long where, unsigned long stack)
  82. {
  83. print_ip_sym(where);
  84. if (in_exception_text(where))
  85. dump_mem("", "Exception stack", stack,
  86. stack + sizeof(struct pt_regs));
  87. }
  88. static void dump_instr(const char *lvl, struct pt_regs *regs)
  89. {
  90. unsigned long addr = instruction_pointer(regs);
  91. mm_segment_t fs;
  92. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  93. int i;
  94. /*
  95. * We need to switch to kernel mode so that we can use __get_user
  96. * to safely read from kernel space. Note that we now dump the
  97. * code first, just in case the backtrace kills us.
  98. */
  99. fs = get_fs();
  100. set_fs(KERNEL_DS);
  101. for (i = -4; i < 1; i++) {
  102. unsigned int val, bad;
  103. bad = __get_user(val, &((u32 *)addr)[i]);
  104. if (!bad)
  105. p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
  106. else {
  107. p += sprintf(p, "bad PC value");
  108. break;
  109. }
  110. }
  111. printk("%sCode: %s\n", lvl, str);
  112. set_fs(fs);
  113. }
  114. static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  115. {
  116. struct stackframe frame;
  117. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  118. if (!tsk)
  119. tsk = current;
  120. if (regs) {
  121. frame.fp = regs->regs[29];
  122. frame.sp = regs->sp;
  123. frame.pc = regs->pc;
  124. } else if (tsk == current) {
  125. frame.fp = (unsigned long)__builtin_frame_address(0);
  126. frame.sp = current_stack_pointer;
  127. frame.pc = (unsigned long)dump_backtrace;
  128. } else {
  129. /*
  130. * task blocked in __switch_to
  131. */
  132. frame.fp = thread_saved_fp(tsk);
  133. frame.sp = thread_saved_sp(tsk);
  134. frame.pc = thread_saved_pc(tsk);
  135. }
  136. pr_emerg("Call trace:\n");
  137. while (1) {
  138. unsigned long where = frame.pc;
  139. int ret;
  140. ret = unwind_frame(&frame);
  141. if (ret < 0)
  142. break;
  143. dump_backtrace_entry(where, frame.sp);
  144. }
  145. }
  146. void show_stack(struct task_struct *tsk, unsigned long *sp)
  147. {
  148. dump_backtrace(NULL, tsk);
  149. barrier();
  150. }
  151. #ifdef CONFIG_PREEMPT
  152. #define S_PREEMPT " PREEMPT"
  153. #else
  154. #define S_PREEMPT ""
  155. #endif
  156. #ifdef CONFIG_SMP
  157. #define S_SMP " SMP"
  158. #else
  159. #define S_SMP ""
  160. #endif
  161. static int __die(const char *str, int err, struct thread_info *thread,
  162. struct pt_regs *regs)
  163. {
  164. struct task_struct *tsk = thread->task;
  165. static int die_counter;
  166. int ret;
  167. pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
  168. str, err, ++die_counter);
  169. /* trap and error numbers are mostly meaningless on ARM */
  170. ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
  171. if (ret == NOTIFY_STOP)
  172. return ret;
  173. print_modules();
  174. __show_regs(regs);
  175. pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
  176. TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
  177. if (!user_mode(regs) || in_interrupt()) {
  178. dump_mem(KERN_EMERG, "Stack: ", regs->sp,
  179. THREAD_SIZE + (unsigned long)task_stack_page(tsk));
  180. dump_backtrace(regs, tsk);
  181. dump_instr(KERN_EMERG, regs);
  182. }
  183. return ret;
  184. }
  185. static DEFINE_RAW_SPINLOCK(die_lock);
  186. /*
  187. * This function is protected against re-entrancy.
  188. */
  189. void die(const char *str, struct pt_regs *regs, int err)
  190. {
  191. struct thread_info *thread = current_thread_info();
  192. int ret;
  193. oops_enter();
  194. raw_spin_lock_irq(&die_lock);
  195. console_verbose();
  196. bust_spinlocks(1);
  197. ret = __die(str, err, thread, regs);
  198. if (regs && kexec_should_crash(thread->task))
  199. crash_kexec(regs);
  200. bust_spinlocks(0);
  201. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  202. raw_spin_unlock_irq(&die_lock);
  203. oops_exit();
  204. if (in_interrupt())
  205. panic("Fatal exception in interrupt");
  206. if (panic_on_oops)
  207. panic("Fatal exception");
  208. if (ret != NOTIFY_STOP)
  209. do_exit(SIGSEGV);
  210. }
  211. void arm64_notify_die(const char *str, struct pt_regs *regs,
  212. struct siginfo *info, int err)
  213. {
  214. if (user_mode(regs)) {
  215. current->thread.fault_address = 0;
  216. current->thread.fault_code = err;
  217. force_sig_info(info->si_signo, info, current);
  218. } else {
  219. die(str, regs, err);
  220. }
  221. }
  222. static LIST_HEAD(undef_hook);
  223. static DEFINE_RAW_SPINLOCK(undef_lock);
  224. void register_undef_hook(struct undef_hook *hook)
  225. {
  226. unsigned long flags;
  227. raw_spin_lock_irqsave(&undef_lock, flags);
  228. list_add(&hook->node, &undef_hook);
  229. raw_spin_unlock_irqrestore(&undef_lock, flags);
  230. }
  231. void unregister_undef_hook(struct undef_hook *hook)
  232. {
  233. unsigned long flags;
  234. raw_spin_lock_irqsave(&undef_lock, flags);
  235. list_del(&hook->node);
  236. raw_spin_unlock_irqrestore(&undef_lock, flags);
  237. }
  238. static int call_undef_hook(struct pt_regs *regs)
  239. {
  240. struct undef_hook *hook;
  241. unsigned long flags;
  242. u32 instr;
  243. int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
  244. void __user *pc = (void __user *)instruction_pointer(regs);
  245. if (!user_mode(regs))
  246. return 1;
  247. if (compat_thumb_mode(regs)) {
  248. /* 16-bit Thumb instruction */
  249. if (get_user(instr, (u16 __user *)pc))
  250. goto exit;
  251. instr = le16_to_cpu(instr);
  252. if (aarch32_insn_is_wide(instr)) {
  253. u32 instr2;
  254. if (get_user(instr2, (u16 __user *)(pc + 2)))
  255. goto exit;
  256. instr2 = le16_to_cpu(instr2);
  257. instr = (instr << 16) | instr2;
  258. }
  259. } else {
  260. /* 32-bit ARM instruction */
  261. if (get_user(instr, (u32 __user *)pc))
  262. goto exit;
  263. instr = le32_to_cpu(instr);
  264. }
  265. raw_spin_lock_irqsave(&undef_lock, flags);
  266. list_for_each_entry(hook, &undef_hook, node)
  267. if ((instr & hook->instr_mask) == hook->instr_val &&
  268. (regs->pstate & hook->pstate_mask) == hook->pstate_val)
  269. fn = hook->fn;
  270. raw_spin_unlock_irqrestore(&undef_lock, flags);
  271. exit:
  272. return fn ? fn(regs, instr) : 1;
  273. }
  274. asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
  275. {
  276. siginfo_t info;
  277. void __user *pc = (void __user *)instruction_pointer(regs);
  278. /* check for AArch32 breakpoint instructions */
  279. if (!aarch32_break_handler(regs))
  280. return;
  281. if (call_undef_hook(regs) == 0)
  282. return;
  283. if (unhandled_signal(current, SIGILL) && show_unhandled_signals_ratelimited()) {
  284. pr_info("%s[%d]: undefined instruction: pc=%p\n",
  285. current->comm, task_pid_nr(current), pc);
  286. dump_instr(KERN_INFO, regs);
  287. }
  288. info.si_signo = SIGILL;
  289. info.si_errno = 0;
  290. info.si_code = ILL_ILLOPC;
  291. info.si_addr = pc;
  292. arm64_notify_die("Oops - undefined instruction", regs, &info, 0);
  293. }
  294. long compat_arm_syscall(struct pt_regs *regs);
  295. asmlinkage long do_ni_syscall(struct pt_regs *regs)
  296. {
  297. #ifdef CONFIG_COMPAT
  298. long ret;
  299. if (is_compat_task()) {
  300. ret = compat_arm_syscall(regs);
  301. if (ret != -ENOSYS)
  302. return ret;
  303. }
  304. #endif
  305. if (show_unhandled_signals_ratelimited()) {
  306. pr_info("%s[%d]: syscall %d\n", current->comm,
  307. task_pid_nr(current), (int)regs->syscallno);
  308. dump_instr("", regs);
  309. if (user_mode(regs))
  310. __show_regs(regs);
  311. }
  312. return sys_ni_syscall();
  313. }
  314. static const char *esr_class_str[] = {
  315. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  316. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  317. [ESR_ELx_EC_WFx] = "WFI/WFE",
  318. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  319. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  320. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  321. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  322. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  323. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  324. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  325. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  326. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  327. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  328. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  329. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  330. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  331. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  332. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  333. [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
  334. [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
  335. [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
  336. [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
  337. [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
  338. [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
  339. [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
  340. [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
  341. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  342. [ESR_ELx_EC_SERROR] = "SError",
  343. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  344. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  345. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  346. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  347. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  348. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  349. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  350. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  351. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  352. };
  353. const char *esr_get_class_string(u32 esr)
  354. {
  355. return esr_class_str[esr >> ESR_ELx_EC_SHIFT];
  356. }
  357. /*
  358. * bad_mode handles the impossible case in the exception vector.
  359. */
  360. asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
  361. {
  362. siginfo_t info;
  363. void __user *pc = (void __user *)instruction_pointer(regs);
  364. console_verbose();
  365. pr_crit("Bad mode in %s handler detected, code 0x%08x -- %s\n",
  366. handler[reason], esr, esr_get_class_string(esr));
  367. __show_regs(regs);
  368. info.si_signo = SIGILL;
  369. info.si_errno = 0;
  370. info.si_code = ILL_ILLOPC;
  371. info.si_addr = pc;
  372. arm64_notify_die("Oops - bad mode", regs, &info, 0);
  373. }
  374. void __pte_error(const char *file, int line, unsigned long val)
  375. {
  376. pr_crit("%s:%d: bad pte %016lx.\n", file, line, val);
  377. }
  378. void __pmd_error(const char *file, int line, unsigned long val)
  379. {
  380. pr_crit("%s:%d: bad pmd %016lx.\n", file, line, val);
  381. }
  382. void __pud_error(const char *file, int line, unsigned long val)
  383. {
  384. pr_crit("%s:%d: bad pud %016lx.\n", file, line, val);
  385. }
  386. void __pgd_error(const char *file, int line, unsigned long val)
  387. {
  388. pr_crit("%s:%d: bad pgd %016lx.\n", file, line, val);
  389. }
  390. void __init trap_init(void)
  391. {
  392. return;
  393. }