traps.c 14 KB

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