traps.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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/signal.h>
  32. #include <linux/sched/debug.h>
  33. #include <linux/sched/task_stack.h>
  34. #include <linux/sizes.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/mm_types.h>
  37. #include <asm/atomic.h>
  38. #include <asm/bug.h>
  39. #include <asm/cpufeature.h>
  40. #include <asm/daifflags.h>
  41. #include <asm/debug-monitors.h>
  42. #include <asm/esr.h>
  43. #include <asm/insn.h>
  44. #include <asm/traps.h>
  45. #include <asm/smp.h>
  46. #include <asm/stack_pointer.h>
  47. #include <asm/stacktrace.h>
  48. #include <asm/exception.h>
  49. #include <asm/system_misc.h>
  50. #include <asm/sysreg.h>
  51. static const char *handler[]= {
  52. "Synchronous Abort",
  53. "IRQ",
  54. "FIQ",
  55. "Error"
  56. };
  57. int show_unhandled_signals = 0;
  58. static void dump_backtrace_entry(unsigned long where)
  59. {
  60. printk(" %pS\n", (void *)where);
  61. }
  62. static void __dump_instr(const char *lvl, struct pt_regs *regs)
  63. {
  64. unsigned long addr = instruction_pointer(regs);
  65. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  66. int i;
  67. for (i = -4; i < 1; i++) {
  68. unsigned int val, bad;
  69. bad = get_user(val, &((u32 *)addr)[i]);
  70. if (!bad)
  71. p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
  72. else {
  73. p += sprintf(p, "bad PC value");
  74. break;
  75. }
  76. }
  77. printk("%sCode: %s\n", lvl, str);
  78. }
  79. static void dump_instr(const char *lvl, struct pt_regs *regs)
  80. {
  81. if (!user_mode(regs)) {
  82. mm_segment_t fs = get_fs();
  83. set_fs(KERNEL_DS);
  84. __dump_instr(lvl, regs);
  85. set_fs(fs);
  86. } else {
  87. __dump_instr(lvl, regs);
  88. }
  89. }
  90. void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  91. {
  92. struct stackframe frame;
  93. int skip;
  94. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  95. if (!tsk)
  96. tsk = current;
  97. if (!try_get_task_stack(tsk))
  98. return;
  99. if (tsk == current) {
  100. frame.fp = (unsigned long)__builtin_frame_address(0);
  101. frame.pc = (unsigned long)dump_backtrace;
  102. } else {
  103. /*
  104. * task blocked in __switch_to
  105. */
  106. frame.fp = thread_saved_fp(tsk);
  107. frame.pc = thread_saved_pc(tsk);
  108. }
  109. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  110. frame.graph = tsk->curr_ret_stack;
  111. #endif
  112. skip = !!regs;
  113. printk("Call trace:\n");
  114. do {
  115. /* skip until specified stack frame */
  116. if (!skip) {
  117. dump_backtrace_entry(frame.pc);
  118. } else if (frame.fp == regs->regs[29]) {
  119. skip = 0;
  120. /*
  121. * Mostly, this is the case where this function is
  122. * called in panic/abort. As exception handler's
  123. * stack frame does not contain the corresponding pc
  124. * at which an exception has taken place, use regs->pc
  125. * instead.
  126. */
  127. dump_backtrace_entry(regs->pc);
  128. }
  129. } while (!unwind_frame(tsk, &frame));
  130. put_task_stack(tsk);
  131. }
  132. void show_stack(struct task_struct *tsk, unsigned long *sp)
  133. {
  134. dump_backtrace(NULL, tsk);
  135. barrier();
  136. }
  137. #ifdef CONFIG_PREEMPT
  138. #define S_PREEMPT " PREEMPT"
  139. #else
  140. #define S_PREEMPT ""
  141. #endif
  142. #define S_SMP " SMP"
  143. static int __die(const char *str, int err, struct pt_regs *regs)
  144. {
  145. struct task_struct *tsk = current;
  146. static int die_counter;
  147. int ret;
  148. pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
  149. str, err, ++die_counter);
  150. /* trap and error numbers are mostly meaningless on ARM */
  151. ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
  152. if (ret == NOTIFY_STOP)
  153. return ret;
  154. print_modules();
  155. __show_regs(regs);
  156. pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
  157. TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk),
  158. end_of_stack(tsk));
  159. if (!user_mode(regs)) {
  160. dump_backtrace(regs, tsk);
  161. dump_instr(KERN_EMERG, regs);
  162. }
  163. return ret;
  164. }
  165. static DEFINE_RAW_SPINLOCK(die_lock);
  166. /*
  167. * This function is protected against re-entrancy.
  168. */
  169. void die(const char *str, struct pt_regs *regs, int err)
  170. {
  171. int ret;
  172. unsigned long flags;
  173. raw_spin_lock_irqsave(&die_lock, flags);
  174. oops_enter();
  175. console_verbose();
  176. bust_spinlocks(1);
  177. ret = __die(str, err, regs);
  178. if (regs && kexec_should_crash(current))
  179. crash_kexec(regs);
  180. bust_spinlocks(0);
  181. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  182. oops_exit();
  183. if (in_interrupt())
  184. panic("Fatal exception in interrupt");
  185. if (panic_on_oops)
  186. panic("Fatal exception");
  187. raw_spin_unlock_irqrestore(&die_lock, flags);
  188. if (ret != NOTIFY_STOP)
  189. do_exit(SIGSEGV);
  190. }
  191. static bool show_unhandled_signals_ratelimited(void)
  192. {
  193. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
  194. DEFAULT_RATELIMIT_BURST);
  195. return show_unhandled_signals && __ratelimit(&rs);
  196. }
  197. void arm64_force_sig_info(struct siginfo *info, const char *str,
  198. struct task_struct *tsk)
  199. {
  200. unsigned int esr = tsk->thread.fault_code;
  201. struct pt_regs *regs = task_pt_regs(tsk);
  202. if (!unhandled_signal(tsk, info->si_signo))
  203. goto send_sig;
  204. if (!show_unhandled_signals_ratelimited())
  205. goto send_sig;
  206. pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
  207. if (esr)
  208. pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr);
  209. pr_cont("%s", str);
  210. print_vma_addr(KERN_CONT " in ", regs->pc);
  211. pr_cont("\n");
  212. __show_regs(regs);
  213. send_sig:
  214. force_sig_info(info->si_signo, info, tsk);
  215. }
  216. void arm64_notify_die(const char *str, struct pt_regs *regs,
  217. struct siginfo *info, int err)
  218. {
  219. if (user_mode(regs)) {
  220. WARN_ON(regs != current_pt_regs());
  221. current->thread.fault_address = 0;
  222. current->thread.fault_code = err;
  223. arm64_force_sig_info(info, str, current);
  224. } else {
  225. die(str, regs, err);
  226. }
  227. }
  228. void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
  229. {
  230. regs->pc += size;
  231. /*
  232. * If we were single stepping, we want to get the step exception after
  233. * we return from the trap.
  234. */
  235. if (user_mode(regs))
  236. user_fastforward_single_step(current);
  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. __le32 instr_le;
  263. if (probe_kernel_address((__force __le32 *)pc, instr_le))
  264. goto exit;
  265. instr = le32_to_cpu(instr_le);
  266. } else if (compat_thumb_mode(regs)) {
  267. /* 16-bit Thumb instruction */
  268. __le16 instr_le;
  269. if (get_user(instr_le, (__le16 __user *)pc))
  270. goto exit;
  271. instr = le16_to_cpu(instr_le);
  272. if (aarch32_insn_is_wide(instr)) {
  273. u32 instr2;
  274. if (get_user(instr_le, (__le16 __user *)(pc + 2)))
  275. goto exit;
  276. instr2 = le16_to_cpu(instr_le);
  277. instr = (instr << 16) | instr2;
  278. }
  279. } else {
  280. /* 32-bit ARM instruction */
  281. __le32 instr_le;
  282. if (get_user(instr_le, (__le32 __user *)pc))
  283. goto exit;
  284. instr = le32_to_cpu(instr_le);
  285. }
  286. raw_spin_lock_irqsave(&undef_lock, flags);
  287. list_for_each_entry(hook, &undef_hook, node)
  288. if ((instr & hook->instr_mask) == hook->instr_val &&
  289. (regs->pstate & hook->pstate_mask) == hook->pstate_val)
  290. fn = hook->fn;
  291. raw_spin_unlock_irqrestore(&undef_lock, flags);
  292. exit:
  293. return fn ? fn(regs, instr) : 1;
  294. }
  295. void force_signal_inject(int signal, int code, unsigned long address)
  296. {
  297. siginfo_t info;
  298. const char *desc;
  299. struct pt_regs *regs = current_pt_regs();
  300. if (WARN_ON(!user_mode(regs)))
  301. return;
  302. clear_siginfo(&info);
  303. switch (signal) {
  304. case SIGILL:
  305. desc = "undefined instruction";
  306. break;
  307. case SIGSEGV:
  308. desc = "illegal memory access";
  309. break;
  310. default:
  311. desc = "unknown or unrecoverable error";
  312. break;
  313. }
  314. /* Force signals we don't understand to SIGKILL */
  315. if (WARN_ON(signal != SIGKILL &&
  316. siginfo_layout(signal, code) != SIL_FAULT)) {
  317. signal = SIGKILL;
  318. }
  319. info.si_signo = signal;
  320. info.si_errno = 0;
  321. info.si_code = code;
  322. info.si_addr = (void __user *)address;
  323. arm64_notify_die(desc, regs, &info, 0);
  324. }
  325. /*
  326. * Set up process info to signal segmentation fault - called on access error.
  327. */
  328. void arm64_notify_segfault(unsigned long addr)
  329. {
  330. int code;
  331. down_read(&current->mm->mmap_sem);
  332. if (find_vma(current->mm, addr) == NULL)
  333. code = SEGV_MAPERR;
  334. else
  335. code = SEGV_ACCERR;
  336. up_read(&current->mm->mmap_sem);
  337. force_signal_inject(SIGSEGV, code, addr);
  338. }
  339. asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
  340. {
  341. /* check for AArch32 breakpoint instructions */
  342. if (!aarch32_break_handler(regs))
  343. return;
  344. if (call_undef_hook(regs) == 0)
  345. return;
  346. BUG_ON(!user_mode(regs));
  347. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  348. }
  349. #define __user_cache_maint(insn, address, res) \
  350. if (address >= user_addr_max()) { \
  351. res = -EFAULT; \
  352. } else { \
  353. uaccess_ttbr0_enable(); \
  354. asm volatile ( \
  355. "1: " insn ", %1\n" \
  356. " mov %w0, #0\n" \
  357. "2:\n" \
  358. " .pushsection .fixup,\"ax\"\n" \
  359. " .align 2\n" \
  360. "3: mov %w0, %w2\n" \
  361. " b 2b\n" \
  362. " .popsection\n" \
  363. _ASM_EXTABLE(1b, 3b) \
  364. : "=r" (res) \
  365. : "r" (address), "i" (-EFAULT)); \
  366. uaccess_ttbr0_disable(); \
  367. }
  368. static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
  369. {
  370. unsigned long address;
  371. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  372. int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
  373. int ret = 0;
  374. address = untagged_addr(pt_regs_read_reg(regs, rt));
  375. switch (crm) {
  376. case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
  377. __user_cache_maint("dc civac", address, ret);
  378. break;
  379. case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
  380. __user_cache_maint("dc civac", address, ret);
  381. break;
  382. case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
  383. __user_cache_maint("sys 3, c7, c12, 1", address, ret);
  384. break;
  385. case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
  386. __user_cache_maint("dc civac", address, ret);
  387. break;
  388. case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
  389. __user_cache_maint("ic ivau", address, ret);
  390. break;
  391. default:
  392. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  393. return;
  394. }
  395. if (ret)
  396. arm64_notify_segfault(address);
  397. else
  398. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  399. }
  400. static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
  401. {
  402. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  403. unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
  404. pt_regs_write_reg(regs, rt, val);
  405. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  406. }
  407. static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
  408. {
  409. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  410. pt_regs_write_reg(regs, rt, arch_counter_get_cntvct());
  411. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  412. }
  413. static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
  414. {
  415. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  416. pt_regs_write_reg(regs, rt, arch_timer_get_rate());
  417. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  418. }
  419. struct sys64_hook {
  420. unsigned int esr_mask;
  421. unsigned int esr_val;
  422. void (*handler)(unsigned int esr, struct pt_regs *regs);
  423. };
  424. static struct sys64_hook sys64_hooks[] = {
  425. {
  426. .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
  427. .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
  428. .handler = user_cache_maint_handler,
  429. },
  430. {
  431. /* Trap read access to CTR_EL0 */
  432. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  433. .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
  434. .handler = ctr_read_handler,
  435. },
  436. {
  437. /* Trap read access to CNTVCT_EL0 */
  438. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  439. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
  440. .handler = cntvct_read_handler,
  441. },
  442. {
  443. /* Trap read access to CNTFRQ_EL0 */
  444. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  445. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
  446. .handler = cntfrq_read_handler,
  447. },
  448. {},
  449. };
  450. asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
  451. {
  452. struct sys64_hook *hook;
  453. for (hook = sys64_hooks; hook->handler; hook++)
  454. if ((hook->esr_mask & esr) == hook->esr_val) {
  455. hook->handler(esr, regs);
  456. return;
  457. }
  458. /*
  459. * New SYS instructions may previously have been undefined at EL0. Fall
  460. * back to our usual undefined instruction handler so that we handle
  461. * these consistently.
  462. */
  463. do_undefinstr(regs);
  464. }
  465. static const char *esr_class_str[] = {
  466. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  467. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  468. [ESR_ELx_EC_WFx] = "WFI/WFE",
  469. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  470. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  471. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  472. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  473. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  474. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  475. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  476. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  477. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  478. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  479. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  480. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  481. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  482. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  483. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  484. [ESR_ELx_EC_SVE] = "SVE",
  485. [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
  486. [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
  487. [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
  488. [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
  489. [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
  490. [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
  491. [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
  492. [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
  493. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  494. [ESR_ELx_EC_SERROR] = "SError",
  495. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  496. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  497. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  498. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  499. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  500. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  501. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  502. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  503. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  504. };
  505. const char *esr_get_class_string(u32 esr)
  506. {
  507. return esr_class_str[ESR_ELx_EC(esr)];
  508. }
  509. /*
  510. * bad_mode handles the impossible case in the exception vector. This is always
  511. * fatal.
  512. */
  513. asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
  514. {
  515. console_verbose();
  516. pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
  517. handler[reason], smp_processor_id(), esr,
  518. esr_get_class_string(esr));
  519. local_daif_mask();
  520. panic("bad mode");
  521. }
  522. /*
  523. * bad_el0_sync handles unexpected, but potentially recoverable synchronous
  524. * exceptions taken from EL0. Unlike bad_mode, this returns.
  525. */
  526. asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
  527. {
  528. siginfo_t info;
  529. void __user *pc = (void __user *)instruction_pointer(regs);
  530. clear_siginfo(&info);
  531. info.si_signo = SIGILL;
  532. info.si_errno = 0;
  533. info.si_code = ILL_ILLOPC;
  534. info.si_addr = pc;
  535. current->thread.fault_address = 0;
  536. current->thread.fault_code = esr;
  537. arm64_force_sig_info(&info, "Bad EL0 synchronous exception", current);
  538. }
  539. #ifdef CONFIG_VMAP_STACK
  540. DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
  541. __aligned(16);
  542. asmlinkage void handle_bad_stack(struct pt_regs *regs)
  543. {
  544. unsigned long tsk_stk = (unsigned long)current->stack;
  545. unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
  546. unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
  547. unsigned int esr = read_sysreg(esr_el1);
  548. unsigned long far = read_sysreg(far_el1);
  549. console_verbose();
  550. pr_emerg("Insufficient stack space to handle exception!");
  551. pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
  552. pr_emerg("FAR: 0x%016lx\n", far);
  553. pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
  554. tsk_stk, tsk_stk + THREAD_SIZE);
  555. pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
  556. irq_stk, irq_stk + THREAD_SIZE);
  557. pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
  558. ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
  559. __show_regs(regs);
  560. /*
  561. * We use nmi_panic to limit the potential for recusive overflows, and
  562. * to get a better stack trace.
  563. */
  564. nmi_panic(NULL, "kernel stack overflow");
  565. cpu_park_loop();
  566. }
  567. #endif
  568. void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
  569. {
  570. console_verbose();
  571. pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
  572. smp_processor_id(), esr, esr_get_class_string(esr));
  573. if (regs)
  574. __show_regs(regs);
  575. nmi_panic(regs, "Asynchronous SError Interrupt");
  576. cpu_park_loop();
  577. unreachable();
  578. }
  579. bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
  580. {
  581. u32 aet = arm64_ras_serror_get_severity(esr);
  582. switch (aet) {
  583. case ESR_ELx_AET_CE: /* corrected error */
  584. case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
  585. /*
  586. * The CPU can make progress. We may take UEO again as
  587. * a more severe error.
  588. */
  589. return false;
  590. case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
  591. case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
  592. /*
  593. * The CPU can't make progress. The exception may have
  594. * been imprecise.
  595. */
  596. return true;
  597. case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
  598. default:
  599. /* Error has been silently propagated */
  600. arm64_serror_panic(regs, esr);
  601. }
  602. }
  603. asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
  604. {
  605. nmi_enter();
  606. /* non-RAS errors are not containable */
  607. if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
  608. arm64_serror_panic(regs, esr);
  609. nmi_exit();
  610. }
  611. void __pte_error(const char *file, int line, unsigned long val)
  612. {
  613. pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
  614. }
  615. void __pmd_error(const char *file, int line, unsigned long val)
  616. {
  617. pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
  618. }
  619. void __pud_error(const char *file, int line, unsigned long val)
  620. {
  621. pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
  622. }
  623. void __pgd_error(const char *file, int line, unsigned long val)
  624. {
  625. pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
  626. }
  627. /* GENERIC_BUG traps */
  628. int is_valid_bugaddr(unsigned long addr)
  629. {
  630. /*
  631. * bug_handler() only called for BRK #BUG_BRK_IMM.
  632. * So the answer is trivial -- any spurious instances with no
  633. * bug table entry will be rejected by report_bug() and passed
  634. * back to the debug-monitors code and handled as a fatal
  635. * unexpected debug exception.
  636. */
  637. return 1;
  638. }
  639. static int bug_handler(struct pt_regs *regs, unsigned int esr)
  640. {
  641. if (user_mode(regs))
  642. return DBG_HOOK_ERROR;
  643. switch (report_bug(regs->pc, regs)) {
  644. case BUG_TRAP_TYPE_BUG:
  645. die("Oops - BUG", regs, 0);
  646. break;
  647. case BUG_TRAP_TYPE_WARN:
  648. break;
  649. default:
  650. /* unknown/unrecognised bug trap type */
  651. return DBG_HOOK_ERROR;
  652. }
  653. /* If thread survives, skip over the BUG instruction and continue: */
  654. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  655. return DBG_HOOK_HANDLED;
  656. }
  657. static struct break_hook bug_break_hook = {
  658. .esr_val = 0xf2000000 | BUG_BRK_IMM,
  659. .esr_mask = 0xffffffff,
  660. .fn = bug_handler,
  661. };
  662. /*
  663. * Initial handler for AArch64 BRK exceptions
  664. * This handler only used until debug_traps_init().
  665. */
  666. int __init early_brk64(unsigned long addr, unsigned int esr,
  667. struct pt_regs *regs)
  668. {
  669. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  670. }
  671. /* This registration must happen early, before debug_traps_init(). */
  672. void __init trap_init(void)
  673. {
  674. register_break_hook(&bug_break_hook);
  675. }