traps.c 18 KB

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