traps.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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/bug.h>
  20. #include <linux/signal.h>
  21. #include <linux/personality.h>
  22. #include <linux/kallsyms.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/hardirq.h>
  26. #include <linux/kdebug.h>
  27. #include <linux/module.h>
  28. #include <linux/kexec.h>
  29. #include <linux/delay.h>
  30. #include <linux/init.h>
  31. #include <linux/sched.h>
  32. #include <linux/syscalls.h>
  33. #include <asm/atomic.h>
  34. #include <asm/bug.h>
  35. #include <asm/debug-monitors.h>
  36. #include <asm/esr.h>
  37. #include <asm/insn.h>
  38. #include <asm/traps.h>
  39. #include <asm/stacktrace.h>
  40. #include <asm/exception.h>
  41. #include <asm/system_misc.h>
  42. static const char *handler[]= {
  43. "Synchronous Abort",
  44. "IRQ",
  45. "FIQ",
  46. "Error"
  47. };
  48. int show_unhandled_signals = 1;
  49. /*
  50. * Dump out the contents of some memory nicely...
  51. */
  52. static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
  53. unsigned long top, bool compat)
  54. {
  55. unsigned long first;
  56. mm_segment_t fs;
  57. int i;
  58. unsigned int width = compat ? 4 : 8;
  59. /*
  60. * We need to switch to kernel mode so that we can use __get_user
  61. * to safely read from kernel space. Note that we now dump the
  62. * code first, just in case the backtrace kills us.
  63. */
  64. fs = get_fs();
  65. set_fs(KERNEL_DS);
  66. printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);
  67. for (first = bottom & ~31; first < top; first += 32) {
  68. unsigned long p;
  69. char str[sizeof(" 12345678") * 8 + 1];
  70. memset(str, ' ', sizeof(str));
  71. str[sizeof(str) - 1] = '\0';
  72. for (p = first, i = 0; i < (32 / width)
  73. && p < top; i++, p += width) {
  74. if (p >= bottom && p < top) {
  75. unsigned long val;
  76. if (width == 8) {
  77. if (__get_user(val, (unsigned long *)p) == 0)
  78. sprintf(str + i * 17, " %016lx", val);
  79. else
  80. sprintf(str + i * 17, " ????????????????");
  81. } else {
  82. if (__get_user(val, (unsigned int *)p) == 0)
  83. sprintf(str + i * 9, " %08lx", val);
  84. else
  85. sprintf(str + i * 9, " ????????");
  86. }
  87. }
  88. }
  89. printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
  90. }
  91. set_fs(fs);
  92. }
  93. static void dump_backtrace_entry(unsigned long where)
  94. {
  95. /*
  96. * Note that 'where' can have a physical address, but it's not handled.
  97. */
  98. print_ip_sym(where);
  99. }
  100. static void dump_instr(const char *lvl, struct pt_regs *regs)
  101. {
  102. unsigned long addr = instruction_pointer(regs);
  103. mm_segment_t fs;
  104. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  105. int i;
  106. /*
  107. * We need to switch to kernel mode so that we can use __get_user
  108. * to safely read from kernel space. Note that we now dump the
  109. * code first, just in case the backtrace kills us.
  110. */
  111. fs = get_fs();
  112. set_fs(KERNEL_DS);
  113. for (i = -4; i < 1; i++) {
  114. unsigned int val, bad;
  115. bad = __get_user(val, &((u32 *)addr)[i]);
  116. if (!bad)
  117. p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
  118. else {
  119. p += sprintf(p, "bad PC value");
  120. break;
  121. }
  122. }
  123. printk("%sCode: %s\n", lvl, str);
  124. set_fs(fs);
  125. }
  126. static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  127. {
  128. struct stackframe frame;
  129. unsigned long irq_stack_ptr = IRQ_STACK_PTR(smp_processor_id());
  130. int skip;
  131. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  132. if (!tsk)
  133. tsk = current;
  134. if (tsk == current) {
  135. frame.fp = (unsigned long)__builtin_frame_address(0);
  136. frame.sp = current_stack_pointer;
  137. frame.pc = (unsigned long)dump_backtrace;
  138. } else {
  139. /*
  140. * task blocked in __switch_to
  141. */
  142. frame.fp = thread_saved_fp(tsk);
  143. frame.sp = thread_saved_sp(tsk);
  144. frame.pc = thread_saved_pc(tsk);
  145. }
  146. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  147. frame.graph = tsk->curr_ret_stack;
  148. #endif
  149. skip = !!regs;
  150. printk("Call trace:\n");
  151. while (1) {
  152. unsigned long where = frame.pc;
  153. unsigned long stack;
  154. int ret;
  155. /* skip until specified stack frame */
  156. if (!skip) {
  157. dump_backtrace_entry(where);
  158. } else if (frame.fp == regs->regs[29]) {
  159. skip = 0;
  160. /*
  161. * Mostly, this is the case where this function is
  162. * called in panic/abort. As exception handler's
  163. * stack frame does not contain the corresponding pc
  164. * at which an exception has taken place, use regs->pc
  165. * instead.
  166. */
  167. dump_backtrace_entry(regs->pc);
  168. }
  169. ret = unwind_frame(tsk, &frame);
  170. if (ret < 0)
  171. break;
  172. stack = frame.sp;
  173. if (in_exception_text(where)) {
  174. /*
  175. * If we switched to the irq_stack before calling this
  176. * exception handler, then the pt_regs will be on the
  177. * task stack. The easiest way to tell is if the large
  178. * pt_regs would overlap with the end of the irq_stack.
  179. */
  180. if (stack < irq_stack_ptr &&
  181. (stack + sizeof(struct pt_regs)) > irq_stack_ptr)
  182. stack = IRQ_STACK_TO_TASK_STACK(irq_stack_ptr);
  183. dump_mem("", "Exception stack", stack,
  184. stack + sizeof(struct pt_regs), false);
  185. }
  186. }
  187. }
  188. void show_stack(struct task_struct *tsk, unsigned long *sp)
  189. {
  190. dump_backtrace(NULL, tsk);
  191. barrier();
  192. }
  193. #ifdef CONFIG_PREEMPT
  194. #define S_PREEMPT " PREEMPT"
  195. #else
  196. #define S_PREEMPT ""
  197. #endif
  198. #define S_SMP " SMP"
  199. static int __die(const char *str, int err, struct thread_info *thread,
  200. struct pt_regs *regs)
  201. {
  202. struct task_struct *tsk = thread->task;
  203. static int die_counter;
  204. int ret;
  205. pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
  206. str, err, ++die_counter);
  207. /* trap and error numbers are mostly meaningless on ARM */
  208. ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
  209. if (ret == NOTIFY_STOP)
  210. return ret;
  211. print_modules();
  212. __show_regs(regs);
  213. pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
  214. TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
  215. if (!user_mode(regs) || in_interrupt()) {
  216. dump_mem(KERN_EMERG, "Stack: ", regs->sp,
  217. THREAD_SIZE + (unsigned long)task_stack_page(tsk),
  218. compat_user_mode(regs));
  219. dump_backtrace(regs, tsk);
  220. dump_instr(KERN_EMERG, regs);
  221. }
  222. return ret;
  223. }
  224. static DEFINE_RAW_SPINLOCK(die_lock);
  225. /*
  226. * This function is protected against re-entrancy.
  227. */
  228. void die(const char *str, struct pt_regs *regs, int err)
  229. {
  230. struct thread_info *thread = current_thread_info();
  231. int ret;
  232. oops_enter();
  233. raw_spin_lock_irq(&die_lock);
  234. console_verbose();
  235. bust_spinlocks(1);
  236. ret = __die(str, err, thread, regs);
  237. if (regs && kexec_should_crash(thread->task))
  238. crash_kexec(regs);
  239. bust_spinlocks(0);
  240. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  241. raw_spin_unlock_irq(&die_lock);
  242. oops_exit();
  243. if (in_interrupt())
  244. panic("Fatal exception in interrupt");
  245. if (panic_on_oops)
  246. panic("Fatal exception");
  247. if (ret != NOTIFY_STOP)
  248. do_exit(SIGSEGV);
  249. }
  250. void arm64_notify_die(const char *str, struct pt_regs *regs,
  251. struct siginfo *info, int err)
  252. {
  253. if (user_mode(regs)) {
  254. current->thread.fault_address = 0;
  255. current->thread.fault_code = err;
  256. force_sig_info(info->si_signo, info, current);
  257. } else {
  258. die(str, regs, err);
  259. }
  260. }
  261. static LIST_HEAD(undef_hook);
  262. static DEFINE_RAW_SPINLOCK(undef_lock);
  263. void register_undef_hook(struct undef_hook *hook)
  264. {
  265. unsigned long flags;
  266. raw_spin_lock_irqsave(&undef_lock, flags);
  267. list_add(&hook->node, &undef_hook);
  268. raw_spin_unlock_irqrestore(&undef_lock, flags);
  269. }
  270. void unregister_undef_hook(struct undef_hook *hook)
  271. {
  272. unsigned long flags;
  273. raw_spin_lock_irqsave(&undef_lock, flags);
  274. list_del(&hook->node);
  275. raw_spin_unlock_irqrestore(&undef_lock, flags);
  276. }
  277. static int call_undef_hook(struct pt_regs *regs)
  278. {
  279. struct undef_hook *hook;
  280. unsigned long flags;
  281. u32 instr;
  282. int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
  283. void __user *pc = (void __user *)instruction_pointer(regs);
  284. if (!user_mode(regs))
  285. return 1;
  286. if (compat_thumb_mode(regs)) {
  287. /* 16-bit Thumb instruction */
  288. if (get_user(instr, (u16 __user *)pc))
  289. goto exit;
  290. instr = le16_to_cpu(instr);
  291. if (aarch32_insn_is_wide(instr)) {
  292. u32 instr2;
  293. if (get_user(instr2, (u16 __user *)(pc + 2)))
  294. goto exit;
  295. instr2 = le16_to_cpu(instr2);
  296. instr = (instr << 16) | instr2;
  297. }
  298. } else {
  299. /* 32-bit ARM instruction */
  300. if (get_user(instr, (u32 __user *)pc))
  301. goto exit;
  302. instr = le32_to_cpu(instr);
  303. }
  304. raw_spin_lock_irqsave(&undef_lock, flags);
  305. list_for_each_entry(hook, &undef_hook, node)
  306. if ((instr & hook->instr_mask) == hook->instr_val &&
  307. (regs->pstate & hook->pstate_mask) == hook->pstate_val)
  308. fn = hook->fn;
  309. raw_spin_unlock_irqrestore(&undef_lock, flags);
  310. exit:
  311. return fn ? fn(regs, instr) : 1;
  312. }
  313. asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
  314. {
  315. siginfo_t info;
  316. void __user *pc = (void __user *)instruction_pointer(regs);
  317. /* check for AArch32 breakpoint instructions */
  318. if (!aarch32_break_handler(regs))
  319. return;
  320. if (call_undef_hook(regs) == 0)
  321. return;
  322. if (unhandled_signal(current, SIGILL) && show_unhandled_signals_ratelimited()) {
  323. pr_info("%s[%d]: undefined instruction: pc=%p\n",
  324. current->comm, task_pid_nr(current), pc);
  325. dump_instr(KERN_INFO, regs);
  326. }
  327. info.si_signo = SIGILL;
  328. info.si_errno = 0;
  329. info.si_code = ILL_ILLOPC;
  330. info.si_addr = pc;
  331. arm64_notify_die("Oops - undefined instruction", regs, &info, 0);
  332. }
  333. long compat_arm_syscall(struct pt_regs *regs);
  334. asmlinkage long do_ni_syscall(struct pt_regs *regs)
  335. {
  336. #ifdef CONFIG_COMPAT
  337. long ret;
  338. if (is_compat_task()) {
  339. ret = compat_arm_syscall(regs);
  340. if (ret != -ENOSYS)
  341. return ret;
  342. }
  343. #endif
  344. if (show_unhandled_signals_ratelimited()) {
  345. pr_info("%s[%d]: syscall %d\n", current->comm,
  346. task_pid_nr(current), (int)regs->syscallno);
  347. dump_instr("", regs);
  348. if (user_mode(regs))
  349. __show_regs(regs);
  350. }
  351. return sys_ni_syscall();
  352. }
  353. static const char *esr_class_str[] = {
  354. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  355. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  356. [ESR_ELx_EC_WFx] = "WFI/WFE",
  357. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  358. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  359. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  360. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  361. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  362. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  363. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  364. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  365. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  366. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  367. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  368. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  369. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  370. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  371. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  372. [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
  373. [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
  374. [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
  375. [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
  376. [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
  377. [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
  378. [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
  379. [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
  380. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  381. [ESR_ELx_EC_SERROR] = "SError",
  382. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  383. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  384. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  385. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  386. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  387. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  388. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  389. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  390. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  391. };
  392. const char *esr_get_class_string(u32 esr)
  393. {
  394. return esr_class_str[esr >> ESR_ELx_EC_SHIFT];
  395. }
  396. /*
  397. * bad_mode handles the impossible case in the exception vector.
  398. */
  399. asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
  400. {
  401. siginfo_t info;
  402. void __user *pc = (void __user *)instruction_pointer(regs);
  403. console_verbose();
  404. pr_crit("Bad mode in %s handler detected, code 0x%08x -- %s\n",
  405. handler[reason], esr, esr_get_class_string(esr));
  406. __show_regs(regs);
  407. info.si_signo = SIGILL;
  408. info.si_errno = 0;
  409. info.si_code = ILL_ILLOPC;
  410. info.si_addr = pc;
  411. arm64_notify_die("Oops - bad mode", regs, &info, 0);
  412. }
  413. void __pte_error(const char *file, int line, unsigned long val)
  414. {
  415. pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
  416. }
  417. void __pmd_error(const char *file, int line, unsigned long val)
  418. {
  419. pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
  420. }
  421. void __pud_error(const char *file, int line, unsigned long val)
  422. {
  423. pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
  424. }
  425. void __pgd_error(const char *file, int line, unsigned long val)
  426. {
  427. pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
  428. }
  429. /* GENERIC_BUG traps */
  430. int is_valid_bugaddr(unsigned long addr)
  431. {
  432. /*
  433. * bug_handler() only called for BRK #BUG_BRK_IMM.
  434. * So the answer is trivial -- any spurious instances with no
  435. * bug table entry will be rejected by report_bug() and passed
  436. * back to the debug-monitors code and handled as a fatal
  437. * unexpected debug exception.
  438. */
  439. return 1;
  440. }
  441. static int bug_handler(struct pt_regs *regs, unsigned int esr)
  442. {
  443. if (user_mode(regs))
  444. return DBG_HOOK_ERROR;
  445. switch (report_bug(regs->pc, regs)) {
  446. case BUG_TRAP_TYPE_BUG:
  447. die("Oops - BUG", regs, 0);
  448. break;
  449. case BUG_TRAP_TYPE_WARN:
  450. /* Ideally, report_bug() should backtrace for us... but no. */
  451. dump_backtrace(regs, NULL);
  452. break;
  453. default:
  454. /* unknown/unrecognised bug trap type */
  455. return DBG_HOOK_ERROR;
  456. }
  457. /* If thread survives, skip over the BUG instruction and continue: */
  458. regs->pc += AARCH64_INSN_SIZE; /* skip BRK and resume */
  459. return DBG_HOOK_HANDLED;
  460. }
  461. static struct break_hook bug_break_hook = {
  462. .esr_val = 0xf2000000 | BUG_BRK_IMM,
  463. .esr_mask = 0xffffffff,
  464. .fn = bug_handler,
  465. };
  466. /*
  467. * Initial handler for AArch64 BRK exceptions
  468. * This handler only used until debug_traps_init().
  469. */
  470. int __init early_brk64(unsigned long addr, unsigned int esr,
  471. struct pt_regs *regs)
  472. {
  473. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  474. }
  475. /* This registration must happen early, before debug_traps_init(). */
  476. void __init trap_init(void)
  477. {
  478. register_break_hook(&bug_break_hook);
  479. }