traps.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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. #include <asm/sysreg.h>
  43. static const char *handler[]= {
  44. "Synchronous Abort",
  45. "IRQ",
  46. "FIQ",
  47. "Error"
  48. };
  49. int show_unhandled_signals = 0;
  50. /*
  51. * Dump out the contents of some kernel memory nicely...
  52. */
  53. static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
  54. unsigned long top)
  55. {
  56. unsigned long first;
  57. mm_segment_t fs;
  58. int i;
  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 / 8)
  72. && p < top; i++, p += 8) {
  73. if (p >= bottom && p < top) {
  74. unsigned long val;
  75. if (__get_user(val, (unsigned long *)p) == 0)
  76. sprintf(str + i * 17, " %016lx", val);
  77. else
  78. sprintf(str + i * 17, " ????????????????");
  79. }
  80. }
  81. printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
  82. }
  83. set_fs(fs);
  84. }
  85. static void dump_backtrace_entry(unsigned long where)
  86. {
  87. /*
  88. * Note that 'where' can have a physical address, but it's not handled.
  89. */
  90. print_ip_sym(where);
  91. }
  92. static void __dump_instr(const char *lvl, struct pt_regs *regs)
  93. {
  94. unsigned long addr = instruction_pointer(regs);
  95. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  96. int i;
  97. for (i = -4; i < 1; i++) {
  98. unsigned int val, bad;
  99. bad = get_user(val, &((u32 *)addr)[i]);
  100. if (!bad)
  101. p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
  102. else {
  103. p += sprintf(p, "bad PC value");
  104. break;
  105. }
  106. }
  107. printk("%sCode: %s\n", lvl, str);
  108. }
  109. static void dump_instr(const char *lvl, struct pt_regs *regs)
  110. {
  111. if (!user_mode(regs)) {
  112. mm_segment_t fs = get_fs();
  113. set_fs(KERNEL_DS);
  114. __dump_instr(lvl, regs);
  115. set_fs(fs);
  116. } else {
  117. __dump_instr(lvl, regs);
  118. }
  119. }
  120. static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  121. {
  122. struct stackframe frame;
  123. unsigned long irq_stack_ptr;
  124. int skip;
  125. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  126. if (!tsk)
  127. tsk = current;
  128. /*
  129. * Switching between stacks is valid when tracing current and in
  130. * non-preemptible context.
  131. */
  132. if (tsk == current && !preemptible())
  133. irq_stack_ptr = IRQ_STACK_PTR(smp_processor_id());
  134. else
  135. irq_stack_ptr = 0;
  136. 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. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  149. frame.graph = tsk->curr_ret_stack;
  150. #endif
  151. skip = !!regs;
  152. printk("Call trace:\n");
  153. while (1) {
  154. unsigned long where = frame.pc;
  155. unsigned long stack;
  156. int ret;
  157. /* skip until specified stack frame */
  158. if (!skip) {
  159. dump_backtrace_entry(where);
  160. } else if (frame.fp == regs->regs[29]) {
  161. skip = 0;
  162. /*
  163. * Mostly, this is the case where this function is
  164. * called in panic/abort. As exception handler's
  165. * stack frame does not contain the corresponding pc
  166. * at which an exception has taken place, use regs->pc
  167. * instead.
  168. */
  169. dump_backtrace_entry(regs->pc);
  170. }
  171. ret = unwind_frame(tsk, &frame);
  172. if (ret < 0)
  173. break;
  174. stack = frame.sp;
  175. if (in_exception_text(where)) {
  176. /*
  177. * If we switched to the irq_stack before calling this
  178. * exception handler, then the pt_regs will be on the
  179. * task stack. The easiest way to tell is if the large
  180. * pt_regs would overlap with the end of the irq_stack.
  181. */
  182. if (stack < irq_stack_ptr &&
  183. (stack + sizeof(struct pt_regs)) > irq_stack_ptr)
  184. stack = IRQ_STACK_TO_TASK_STACK(irq_stack_ptr);
  185. dump_mem("", "Exception stack", stack,
  186. stack + sizeof(struct pt_regs));
  187. }
  188. }
  189. }
  190. void show_stack(struct task_struct *tsk, unsigned long *sp)
  191. {
  192. dump_backtrace(NULL, tsk);
  193. barrier();
  194. }
  195. #ifdef CONFIG_PREEMPT
  196. #define S_PREEMPT " PREEMPT"
  197. #else
  198. #define S_PREEMPT ""
  199. #endif
  200. #define S_SMP " SMP"
  201. static int __die(const char *str, int err, struct thread_info *thread,
  202. struct pt_regs *regs)
  203. {
  204. struct task_struct *tsk = thread->task;
  205. static int die_counter;
  206. int ret;
  207. pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
  208. str, err, ++die_counter);
  209. /* trap and error numbers are mostly meaningless on ARM */
  210. ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
  211. if (ret == NOTIFY_STOP)
  212. return ret;
  213. print_modules();
  214. __show_regs(regs);
  215. pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
  216. TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
  217. if (!user_mode(regs)) {
  218. dump_mem(KERN_EMERG, "Stack: ", regs->sp,
  219. THREAD_SIZE + (unsigned long)task_stack_page(tsk));
  220. dump_backtrace(regs, tsk);
  221. dump_instr(KERN_EMERG, regs);
  222. }
  223. return ret;
  224. }
  225. static DEFINE_RAW_SPINLOCK(die_lock);
  226. /*
  227. * This function is protected against re-entrancy.
  228. */
  229. void die(const char *str, struct pt_regs *regs, int err)
  230. {
  231. struct thread_info *thread = current_thread_info();
  232. int ret;
  233. unsigned long flags;
  234. raw_spin_lock_irqsave(&die_lock, flags);
  235. oops_enter();
  236. console_verbose();
  237. bust_spinlocks(1);
  238. ret = __die(str, err, thread, regs);
  239. if (regs && kexec_should_crash(thread->task))
  240. crash_kexec(regs);
  241. bust_spinlocks(0);
  242. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  243. oops_exit();
  244. if (in_interrupt())
  245. panic("Fatal exception in interrupt");
  246. if (panic_on_oops)
  247. panic("Fatal exception");
  248. raw_spin_unlock_irqrestore(&die_lock, flags);
  249. if (ret != NOTIFY_STOP)
  250. do_exit(SIGSEGV);
  251. }
  252. void arm64_notify_die(const char *str, struct pt_regs *regs,
  253. struct siginfo *info, int err)
  254. {
  255. if (user_mode(regs)) {
  256. current->thread.fault_address = 0;
  257. current->thread.fault_code = err;
  258. force_sig_info(info->si_signo, info, current);
  259. } else {
  260. die(str, regs, err);
  261. }
  262. }
  263. static LIST_HEAD(undef_hook);
  264. static DEFINE_RAW_SPINLOCK(undef_lock);
  265. void register_undef_hook(struct undef_hook *hook)
  266. {
  267. unsigned long flags;
  268. raw_spin_lock_irqsave(&undef_lock, flags);
  269. list_add(&hook->node, &undef_hook);
  270. raw_spin_unlock_irqrestore(&undef_lock, flags);
  271. }
  272. void unregister_undef_hook(struct undef_hook *hook)
  273. {
  274. unsigned long flags;
  275. raw_spin_lock_irqsave(&undef_lock, flags);
  276. list_del(&hook->node);
  277. raw_spin_unlock_irqrestore(&undef_lock, flags);
  278. }
  279. static int call_undef_hook(struct pt_regs *regs)
  280. {
  281. struct undef_hook *hook;
  282. unsigned long flags;
  283. u32 instr;
  284. int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
  285. void __user *pc = (void __user *)instruction_pointer(regs);
  286. if (!user_mode(regs))
  287. return 1;
  288. if (compat_thumb_mode(regs)) {
  289. /* 16-bit Thumb instruction */
  290. if (get_user(instr, (u16 __user *)pc))
  291. goto exit;
  292. instr = le16_to_cpu(instr);
  293. if (aarch32_insn_is_wide(instr)) {
  294. u32 instr2;
  295. if (get_user(instr2, (u16 __user *)(pc + 2)))
  296. goto exit;
  297. instr2 = le16_to_cpu(instr2);
  298. instr = (instr << 16) | instr2;
  299. }
  300. } else {
  301. /* 32-bit ARM instruction */
  302. if (get_user(instr, (u32 __user *)pc))
  303. goto exit;
  304. instr = le32_to_cpu(instr);
  305. }
  306. raw_spin_lock_irqsave(&undef_lock, flags);
  307. list_for_each_entry(hook, &undef_hook, node)
  308. if ((instr & hook->instr_mask) == hook->instr_val &&
  309. (regs->pstate & hook->pstate_mask) == hook->pstate_val)
  310. fn = hook->fn;
  311. raw_spin_unlock_irqrestore(&undef_lock, flags);
  312. exit:
  313. return fn ? fn(regs, instr) : 1;
  314. }
  315. static void force_signal_inject(int signal, int code, struct pt_regs *regs,
  316. unsigned long address)
  317. {
  318. siginfo_t info;
  319. void __user *pc = (void __user *)instruction_pointer(regs);
  320. const char *desc;
  321. switch (signal) {
  322. case SIGILL:
  323. desc = "undefined instruction";
  324. break;
  325. case SIGSEGV:
  326. desc = "illegal memory access";
  327. break;
  328. default:
  329. desc = "bad mode";
  330. break;
  331. }
  332. if (unhandled_signal(current, signal) &&
  333. show_unhandled_signals_ratelimited()) {
  334. pr_info("%s[%d]: %s: pc=%p\n",
  335. current->comm, task_pid_nr(current), desc, pc);
  336. dump_instr(KERN_INFO, regs);
  337. }
  338. info.si_signo = signal;
  339. info.si_errno = 0;
  340. info.si_code = code;
  341. info.si_addr = pc;
  342. arm64_notify_die(desc, regs, &info, 0);
  343. }
  344. /*
  345. * Set up process info to signal segmentation fault - called on access error.
  346. */
  347. void arm64_notify_segfault(struct pt_regs *regs, unsigned long addr)
  348. {
  349. int code;
  350. down_read(&current->mm->mmap_sem);
  351. if (find_vma(current->mm, addr) == NULL)
  352. code = SEGV_MAPERR;
  353. else
  354. code = SEGV_ACCERR;
  355. up_read(&current->mm->mmap_sem);
  356. force_signal_inject(SIGSEGV, code, regs, addr);
  357. }
  358. asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
  359. {
  360. /* check for AArch32 breakpoint instructions */
  361. if (!aarch32_break_handler(regs))
  362. return;
  363. if (call_undef_hook(regs) == 0)
  364. return;
  365. force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
  366. }
  367. int cpu_enable_cache_maint_trap(void *__unused)
  368. {
  369. config_sctlr_el1(SCTLR_EL1_UCI, 0);
  370. return 0;
  371. }
  372. #define __user_cache_maint(insn, address, res) \
  373. if (address >= user_addr_max()) \
  374. res = -EFAULT; \
  375. else \
  376. asm volatile ( \
  377. "1: " insn ", %1\n" \
  378. " mov %w0, #0\n" \
  379. "2:\n" \
  380. " .pushsection .fixup,\"ax\"\n" \
  381. " .align 2\n" \
  382. "3: mov %w0, %w2\n" \
  383. " b 2b\n" \
  384. " .popsection\n" \
  385. _ASM_EXTABLE(1b, 3b) \
  386. : "=r" (res) \
  387. : "r" (address), "i" (-EFAULT) )
  388. static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
  389. {
  390. unsigned long address;
  391. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  392. int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
  393. int ret = 0;
  394. address = (rt == 31) ? 0 : untagged_addr(regs->regs[rt]);
  395. switch (crm) {
  396. case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
  397. __user_cache_maint("dc civac", address, ret);
  398. break;
  399. case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
  400. __user_cache_maint("dc civac", address, ret);
  401. break;
  402. case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
  403. __user_cache_maint("dc civac", address, ret);
  404. break;
  405. case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
  406. __user_cache_maint("ic ivau", address, ret);
  407. break;
  408. default:
  409. force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
  410. return;
  411. }
  412. if (ret)
  413. arm64_notify_segfault(regs, address);
  414. else
  415. regs->pc += 4;
  416. }
  417. static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
  418. {
  419. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  420. regs->regs[rt] = arm64_ftr_reg_ctrel0.sys_val;
  421. regs->pc += 4;
  422. }
  423. struct sys64_hook {
  424. unsigned int esr_mask;
  425. unsigned int esr_val;
  426. void (*handler)(unsigned int esr, struct pt_regs *regs);
  427. };
  428. static struct sys64_hook sys64_hooks[] = {
  429. {
  430. .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
  431. .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
  432. .handler = user_cache_maint_handler,
  433. },
  434. {
  435. /* Trap read access to CTR_EL0 */
  436. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  437. .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
  438. .handler = ctr_read_handler,
  439. },
  440. {},
  441. };
  442. asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
  443. {
  444. struct sys64_hook *hook;
  445. for (hook = sys64_hooks; hook->handler; hook++)
  446. if ((hook->esr_mask & esr) == hook->esr_val) {
  447. hook->handler(esr, regs);
  448. return;
  449. }
  450. force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
  451. }
  452. long compat_arm_syscall(struct pt_regs *regs);
  453. asmlinkage long do_ni_syscall(struct pt_regs *regs)
  454. {
  455. #ifdef CONFIG_COMPAT
  456. long ret;
  457. if (is_compat_task()) {
  458. ret = compat_arm_syscall(regs);
  459. if (ret != -ENOSYS)
  460. return ret;
  461. }
  462. #endif
  463. if (show_unhandled_signals_ratelimited()) {
  464. pr_info("%s[%d]: syscall %d\n", current->comm,
  465. task_pid_nr(current), (int)regs->syscallno);
  466. dump_instr("", regs);
  467. if (user_mode(regs))
  468. __show_regs(regs);
  469. }
  470. return sys_ni_syscall();
  471. }
  472. static const char *esr_class_str[] = {
  473. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  474. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  475. [ESR_ELx_EC_WFx] = "WFI/WFE",
  476. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  477. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  478. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  479. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  480. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  481. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  482. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  483. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  484. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  485. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  486. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  487. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  488. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  489. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  490. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  491. [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
  492. [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
  493. [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
  494. [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
  495. [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
  496. [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
  497. [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
  498. [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
  499. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  500. [ESR_ELx_EC_SERROR] = "SError",
  501. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  502. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  503. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  504. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  505. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  506. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  507. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  508. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  509. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  510. };
  511. const char *esr_get_class_string(u32 esr)
  512. {
  513. return esr_class_str[ESR_ELx_EC(esr)];
  514. }
  515. /*
  516. * bad_mode handles the impossible case in the exception vector. This is always
  517. * fatal.
  518. */
  519. asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
  520. {
  521. console_verbose();
  522. pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
  523. handler[reason], smp_processor_id(), esr,
  524. esr_get_class_string(esr));
  525. die("Oops - bad mode", regs, 0);
  526. local_irq_disable();
  527. panic("bad mode");
  528. }
  529. /*
  530. * bad_el0_sync handles unexpected, but potentially recoverable synchronous
  531. * exceptions taken from EL0. Unlike bad_mode, this returns.
  532. */
  533. asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
  534. {
  535. siginfo_t info;
  536. void __user *pc = (void __user *)instruction_pointer(regs);
  537. console_verbose();
  538. pr_crit("Bad EL0 synchronous exception detected on CPU%d, code 0x%08x -- %s\n",
  539. smp_processor_id(), esr, esr_get_class_string(esr));
  540. __show_regs(regs);
  541. info.si_signo = SIGILL;
  542. info.si_errno = 0;
  543. info.si_code = ILL_ILLOPC;
  544. info.si_addr = pc;
  545. current->thread.fault_address = 0;
  546. current->thread.fault_code = 0;
  547. force_sig_info(info.si_signo, &info, current);
  548. }
  549. void __pte_error(const char *file, int line, unsigned long val)
  550. {
  551. pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
  552. }
  553. void __pmd_error(const char *file, int line, unsigned long val)
  554. {
  555. pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
  556. }
  557. void __pud_error(const char *file, int line, unsigned long val)
  558. {
  559. pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
  560. }
  561. void __pgd_error(const char *file, int line, unsigned long val)
  562. {
  563. pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
  564. }
  565. /* GENERIC_BUG traps */
  566. int is_valid_bugaddr(unsigned long addr)
  567. {
  568. /*
  569. * bug_handler() only called for BRK #BUG_BRK_IMM.
  570. * So the answer is trivial -- any spurious instances with no
  571. * bug table entry will be rejected by report_bug() and passed
  572. * back to the debug-monitors code and handled as a fatal
  573. * unexpected debug exception.
  574. */
  575. return 1;
  576. }
  577. static int bug_handler(struct pt_regs *regs, unsigned int esr)
  578. {
  579. if (user_mode(regs))
  580. return DBG_HOOK_ERROR;
  581. switch (report_bug(regs->pc, regs)) {
  582. case BUG_TRAP_TYPE_BUG:
  583. die("Oops - BUG", regs, 0);
  584. break;
  585. case BUG_TRAP_TYPE_WARN:
  586. /* Ideally, report_bug() should backtrace for us... but no. */
  587. dump_backtrace(regs, NULL);
  588. break;
  589. default:
  590. /* unknown/unrecognised bug trap type */
  591. return DBG_HOOK_ERROR;
  592. }
  593. /* If thread survives, skip over the BUG instruction and continue: */
  594. regs->pc += AARCH64_INSN_SIZE; /* skip BRK and resume */
  595. return DBG_HOOK_HANDLED;
  596. }
  597. static struct break_hook bug_break_hook = {
  598. .esr_val = 0xf2000000 | BUG_BRK_IMM,
  599. .esr_mask = 0xffffffff,
  600. .fn = bug_handler,
  601. };
  602. /*
  603. * Initial handler for AArch64 BRK exceptions
  604. * This handler only used until debug_traps_init().
  605. */
  606. int __init early_brk64(unsigned long addr, unsigned int esr,
  607. struct pt_regs *regs)
  608. {
  609. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  610. }
  611. /* This registration must happen early, before debug_traps_init(). */
  612. void __init trap_init(void)
  613. {
  614. register_break_hook(&bug_break_hook);
  615. }