traps.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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, unsigned long stack)
  94. {
  95. print_ip_sym(where);
  96. if (in_exception_text(where))
  97. dump_mem("", "Exception stack", stack,
  98. stack + sizeof(struct pt_regs), false);
  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. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  130. if (!tsk)
  131. tsk = current;
  132. if (regs) {
  133. frame.fp = regs->regs[29];
  134. frame.sp = regs->sp;
  135. frame.pc = regs->pc;
  136. } else if (tsk == current) {
  137. frame.fp = (unsigned long)__builtin_frame_address(0);
  138. frame.sp = current_stack_pointer;
  139. frame.pc = (unsigned long)dump_backtrace;
  140. } else {
  141. /*
  142. * task blocked in __switch_to
  143. */
  144. frame.fp = thread_saved_fp(tsk);
  145. frame.sp = thread_saved_sp(tsk);
  146. frame.pc = thread_saved_pc(tsk);
  147. }
  148. pr_emerg("Call trace:\n");
  149. while (1) {
  150. unsigned long where = frame.pc;
  151. int ret;
  152. ret = unwind_frame(&frame);
  153. if (ret < 0)
  154. break;
  155. dump_backtrace_entry(where, frame.sp);
  156. }
  157. }
  158. void show_stack(struct task_struct *tsk, unsigned long *sp)
  159. {
  160. dump_backtrace(NULL, tsk);
  161. barrier();
  162. }
  163. #ifdef CONFIG_PREEMPT
  164. #define S_PREEMPT " PREEMPT"
  165. #else
  166. #define S_PREEMPT ""
  167. #endif
  168. #define S_SMP " SMP"
  169. static int __die(const char *str, int err, struct thread_info *thread,
  170. struct pt_regs *regs)
  171. {
  172. struct task_struct *tsk = thread->task;
  173. static int die_counter;
  174. int ret;
  175. pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
  176. str, err, ++die_counter);
  177. /* trap and error numbers are mostly meaningless on ARM */
  178. ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
  179. if (ret == NOTIFY_STOP)
  180. return ret;
  181. print_modules();
  182. __show_regs(regs);
  183. pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
  184. TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
  185. if (!user_mode(regs) || in_interrupt()) {
  186. dump_mem(KERN_EMERG, "Stack: ", regs->sp,
  187. THREAD_SIZE + (unsigned long)task_stack_page(tsk),
  188. compat_user_mode(regs));
  189. dump_backtrace(regs, tsk);
  190. dump_instr(KERN_EMERG, regs);
  191. }
  192. return ret;
  193. }
  194. static DEFINE_RAW_SPINLOCK(die_lock);
  195. /*
  196. * This function is protected against re-entrancy.
  197. */
  198. void die(const char *str, struct pt_regs *regs, int err)
  199. {
  200. struct thread_info *thread = current_thread_info();
  201. int ret;
  202. oops_enter();
  203. raw_spin_lock_irq(&die_lock);
  204. console_verbose();
  205. bust_spinlocks(1);
  206. ret = __die(str, err, thread, regs);
  207. if (regs && kexec_should_crash(thread->task))
  208. crash_kexec(regs);
  209. bust_spinlocks(0);
  210. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  211. raw_spin_unlock_irq(&die_lock);
  212. oops_exit();
  213. if (in_interrupt())
  214. panic("Fatal exception in interrupt");
  215. if (panic_on_oops)
  216. panic("Fatal exception");
  217. if (ret != NOTIFY_STOP)
  218. do_exit(SIGSEGV);
  219. }
  220. void arm64_notify_die(const char *str, struct pt_regs *regs,
  221. struct siginfo *info, int err)
  222. {
  223. if (user_mode(regs)) {
  224. current->thread.fault_address = 0;
  225. current->thread.fault_code = err;
  226. force_sig_info(info->si_signo, info, current);
  227. } else {
  228. die(str, regs, err);
  229. }
  230. }
  231. static LIST_HEAD(undef_hook);
  232. static DEFINE_RAW_SPINLOCK(undef_lock);
  233. void register_undef_hook(struct undef_hook *hook)
  234. {
  235. unsigned long flags;
  236. raw_spin_lock_irqsave(&undef_lock, flags);
  237. list_add(&hook->node, &undef_hook);
  238. raw_spin_unlock_irqrestore(&undef_lock, flags);
  239. }
  240. void unregister_undef_hook(struct undef_hook *hook)
  241. {
  242. unsigned long flags;
  243. raw_spin_lock_irqsave(&undef_lock, flags);
  244. list_del(&hook->node);
  245. raw_spin_unlock_irqrestore(&undef_lock, flags);
  246. }
  247. static int call_undef_hook(struct pt_regs *regs)
  248. {
  249. struct undef_hook *hook;
  250. unsigned long flags;
  251. u32 instr;
  252. int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
  253. void __user *pc = (void __user *)instruction_pointer(regs);
  254. if (!user_mode(regs))
  255. return 1;
  256. if (compat_thumb_mode(regs)) {
  257. /* 16-bit Thumb instruction */
  258. if (get_user(instr, (u16 __user *)pc))
  259. goto exit;
  260. instr = le16_to_cpu(instr);
  261. if (aarch32_insn_is_wide(instr)) {
  262. u32 instr2;
  263. if (get_user(instr2, (u16 __user *)(pc + 2)))
  264. goto exit;
  265. instr2 = le16_to_cpu(instr2);
  266. instr = (instr << 16) | instr2;
  267. }
  268. } else {
  269. /* 32-bit ARM instruction */
  270. if (get_user(instr, (u32 __user *)pc))
  271. goto exit;
  272. instr = le32_to_cpu(instr);
  273. }
  274. raw_spin_lock_irqsave(&undef_lock, flags);
  275. list_for_each_entry(hook, &undef_hook, node)
  276. if ((instr & hook->instr_mask) == hook->instr_val &&
  277. (regs->pstate & hook->pstate_mask) == hook->pstate_val)
  278. fn = hook->fn;
  279. raw_spin_unlock_irqrestore(&undef_lock, flags);
  280. exit:
  281. return fn ? fn(regs, instr) : 1;
  282. }
  283. asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
  284. {
  285. siginfo_t info;
  286. void __user *pc = (void __user *)instruction_pointer(regs);
  287. /* check for AArch32 breakpoint instructions */
  288. if (!aarch32_break_handler(regs))
  289. return;
  290. if (call_undef_hook(regs) == 0)
  291. return;
  292. if (unhandled_signal(current, SIGILL) && show_unhandled_signals_ratelimited()) {
  293. pr_info("%s[%d]: undefined instruction: pc=%p\n",
  294. current->comm, task_pid_nr(current), pc);
  295. dump_instr(KERN_INFO, regs);
  296. }
  297. info.si_signo = SIGILL;
  298. info.si_errno = 0;
  299. info.si_code = ILL_ILLOPC;
  300. info.si_addr = pc;
  301. arm64_notify_die("Oops - undefined instruction", regs, &info, 0);
  302. }
  303. long compat_arm_syscall(struct pt_regs *regs);
  304. asmlinkage long do_ni_syscall(struct pt_regs *regs)
  305. {
  306. #ifdef CONFIG_COMPAT
  307. long ret;
  308. if (is_compat_task()) {
  309. ret = compat_arm_syscall(regs);
  310. if (ret != -ENOSYS)
  311. return ret;
  312. }
  313. #endif
  314. if (show_unhandled_signals_ratelimited()) {
  315. pr_info("%s[%d]: syscall %d\n", current->comm,
  316. task_pid_nr(current), (int)regs->syscallno);
  317. dump_instr("", regs);
  318. if (user_mode(regs))
  319. __show_regs(regs);
  320. }
  321. return sys_ni_syscall();
  322. }
  323. static const char *esr_class_str[] = {
  324. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  325. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  326. [ESR_ELx_EC_WFx] = "WFI/WFE",
  327. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  328. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  329. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  330. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  331. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  332. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  333. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  334. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  335. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  336. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  337. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  338. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  339. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  340. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  341. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  342. [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
  343. [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
  344. [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
  345. [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
  346. [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
  347. [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
  348. [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
  349. [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
  350. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  351. [ESR_ELx_EC_SERROR] = "SError",
  352. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  353. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  354. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  355. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  356. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  357. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  358. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  359. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  360. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  361. };
  362. const char *esr_get_class_string(u32 esr)
  363. {
  364. return esr_class_str[esr >> ESR_ELx_EC_SHIFT];
  365. }
  366. /*
  367. * bad_mode handles the impossible case in the exception vector.
  368. */
  369. asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
  370. {
  371. siginfo_t info;
  372. void __user *pc = (void __user *)instruction_pointer(regs);
  373. console_verbose();
  374. pr_crit("Bad mode in %s handler detected, code 0x%08x -- %s\n",
  375. handler[reason], esr, esr_get_class_string(esr));
  376. __show_regs(regs);
  377. info.si_signo = SIGILL;
  378. info.si_errno = 0;
  379. info.si_code = ILL_ILLOPC;
  380. info.si_addr = pc;
  381. arm64_notify_die("Oops - bad mode", regs, &info, 0);
  382. }
  383. void __pte_error(const char *file, int line, unsigned long val)
  384. {
  385. pr_crit("%s:%d: bad pte %016lx.\n", file, line, val);
  386. }
  387. void __pmd_error(const char *file, int line, unsigned long val)
  388. {
  389. pr_crit("%s:%d: bad pmd %016lx.\n", file, line, val);
  390. }
  391. void __pud_error(const char *file, int line, unsigned long val)
  392. {
  393. pr_crit("%s:%d: bad pud %016lx.\n", file, line, val);
  394. }
  395. void __pgd_error(const char *file, int line, unsigned long val)
  396. {
  397. pr_crit("%s:%d: bad pgd %016lx.\n", file, line, val);
  398. }
  399. /* GENERIC_BUG traps */
  400. int is_valid_bugaddr(unsigned long addr)
  401. {
  402. /*
  403. * bug_handler() only called for BRK #BUG_BRK_IMM.
  404. * So the answer is trivial -- any spurious instances with no
  405. * bug table entry will be rejected by report_bug() and passed
  406. * back to the debug-monitors code and handled as a fatal
  407. * unexpected debug exception.
  408. */
  409. return 1;
  410. }
  411. static int bug_handler(struct pt_regs *regs, unsigned int esr)
  412. {
  413. if (user_mode(regs))
  414. return DBG_HOOK_ERROR;
  415. switch (report_bug(regs->pc, regs)) {
  416. case BUG_TRAP_TYPE_BUG:
  417. die("Oops - BUG", regs, 0);
  418. break;
  419. case BUG_TRAP_TYPE_WARN:
  420. /* Ideally, report_bug() should backtrace for us... but no. */
  421. dump_backtrace(regs, NULL);
  422. break;
  423. default:
  424. /* unknown/unrecognised bug trap type */
  425. return DBG_HOOK_ERROR;
  426. }
  427. /* If thread survives, skip over the BUG instruction and continue: */
  428. regs->pc += AARCH64_INSN_SIZE; /* skip BRK and resume */
  429. return DBG_HOOK_HANDLED;
  430. }
  431. static struct break_hook bug_break_hook = {
  432. .esr_val = 0xf2000000 | BUG_BRK_IMM,
  433. .esr_mask = 0xffffffff,
  434. .fn = bug_handler,
  435. };
  436. /*
  437. * Initial handler for AArch64 BRK exceptions
  438. * This handler only used until debug_traps_init().
  439. */
  440. int __init early_brk64(unsigned long addr, unsigned int esr,
  441. struct pt_regs *regs)
  442. {
  443. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  444. }
  445. /* This registration must happen early, before debug_traps_init(). */
  446. void __init trap_init(void)
  447. {
  448. register_break_hook(&bug_break_hook);
  449. }