traps.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. clear_siginfo(&info);
  301. switch (signal) {
  302. case SIGILL:
  303. desc = "undefined instruction";
  304. break;
  305. case SIGSEGV:
  306. desc = "illegal memory access";
  307. break;
  308. default:
  309. desc = "unknown or unrecoverable error";
  310. break;
  311. }
  312. /* Force signals we don't understand to SIGKILL */
  313. if (WARN_ON(signal != SIGKILL &&
  314. siginfo_layout(signal, code) != SIL_FAULT)) {
  315. signal = SIGKILL;
  316. }
  317. info.si_signo = signal;
  318. info.si_errno = 0;
  319. info.si_code = code;
  320. info.si_addr = (void __user *)address;
  321. arm64_notify_die(desc, regs, &info, 0);
  322. }
  323. /*
  324. * Set up process info to signal segmentation fault - called on access error.
  325. */
  326. void arm64_notify_segfault(unsigned long addr)
  327. {
  328. int code;
  329. down_read(&current->mm->mmap_sem);
  330. if (find_vma(current->mm, addr) == NULL)
  331. code = SEGV_MAPERR;
  332. else
  333. code = SEGV_ACCERR;
  334. up_read(&current->mm->mmap_sem);
  335. force_signal_inject(SIGSEGV, code, addr);
  336. }
  337. asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
  338. {
  339. /* check for AArch32 breakpoint instructions */
  340. if (!aarch32_break_handler(regs))
  341. return;
  342. if (call_undef_hook(regs) == 0)
  343. return;
  344. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  345. BUG_ON(!user_mode(regs));
  346. }
  347. void cpu_enable_cache_maint_trap(const struct arm64_cpu_capabilities *__unused)
  348. {
  349. sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCI, 0);
  350. }
  351. #define __user_cache_maint(insn, address, res) \
  352. if (address >= user_addr_max()) { \
  353. res = -EFAULT; \
  354. } else { \
  355. uaccess_ttbr0_enable(); \
  356. asm volatile ( \
  357. "1: " insn ", %1\n" \
  358. " mov %w0, #0\n" \
  359. "2:\n" \
  360. " .pushsection .fixup,\"ax\"\n" \
  361. " .align 2\n" \
  362. "3: mov %w0, %w2\n" \
  363. " b 2b\n" \
  364. " .popsection\n" \
  365. _ASM_EXTABLE(1b, 3b) \
  366. : "=r" (res) \
  367. : "r" (address), "i" (-EFAULT)); \
  368. uaccess_ttbr0_disable(); \
  369. }
  370. static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
  371. {
  372. unsigned long address;
  373. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  374. int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
  375. int ret = 0;
  376. address = untagged_addr(pt_regs_read_reg(regs, rt));
  377. switch (crm) {
  378. case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
  379. __user_cache_maint("dc civac", address, ret);
  380. break;
  381. case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
  382. __user_cache_maint("dc civac", address, ret);
  383. break;
  384. case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
  385. __user_cache_maint("sys 3, c7, c12, 1", address, ret);
  386. break;
  387. case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
  388. __user_cache_maint("dc civac", address, ret);
  389. break;
  390. case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
  391. __user_cache_maint("ic ivau", address, ret);
  392. break;
  393. default:
  394. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  395. return;
  396. }
  397. if (ret)
  398. arm64_notify_segfault(address);
  399. else
  400. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  401. }
  402. static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
  403. {
  404. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  405. unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
  406. pt_regs_write_reg(regs, rt, val);
  407. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  408. }
  409. static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
  410. {
  411. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  412. pt_regs_write_reg(regs, rt, arch_counter_get_cntvct());
  413. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  414. }
  415. static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
  416. {
  417. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  418. pt_regs_write_reg(regs, rt, arch_timer_get_rate());
  419. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  420. }
  421. struct sys64_hook {
  422. unsigned int esr_mask;
  423. unsigned int esr_val;
  424. void (*handler)(unsigned int esr, struct pt_regs *regs);
  425. };
  426. static struct sys64_hook sys64_hooks[] = {
  427. {
  428. .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
  429. .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
  430. .handler = user_cache_maint_handler,
  431. },
  432. {
  433. /* Trap read access to CTR_EL0 */
  434. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  435. .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
  436. .handler = ctr_read_handler,
  437. },
  438. {
  439. /* Trap read access to CNTVCT_EL0 */
  440. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  441. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
  442. .handler = cntvct_read_handler,
  443. },
  444. {
  445. /* Trap read access to CNTFRQ_EL0 */
  446. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  447. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
  448. .handler = cntfrq_read_handler,
  449. },
  450. {},
  451. };
  452. asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
  453. {
  454. struct sys64_hook *hook;
  455. for (hook = sys64_hooks; hook->handler; hook++)
  456. if ((hook->esr_mask & esr) == hook->esr_val) {
  457. hook->handler(esr, regs);
  458. return;
  459. }
  460. /*
  461. * New SYS instructions may previously have been undefined at EL0. Fall
  462. * back to our usual undefined instruction handler so that we handle
  463. * these consistently.
  464. */
  465. do_undefinstr(regs);
  466. }
  467. static const char *esr_class_str[] = {
  468. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  469. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  470. [ESR_ELx_EC_WFx] = "WFI/WFE",
  471. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  472. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  473. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  474. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  475. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  476. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  477. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  478. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  479. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  480. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  481. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  482. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  483. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  484. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  485. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  486. [ESR_ELx_EC_SVE] = "SVE",
  487. [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
  488. [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
  489. [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
  490. [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
  491. [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
  492. [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
  493. [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
  494. [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
  495. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  496. [ESR_ELx_EC_SERROR] = "SError",
  497. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  498. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  499. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  500. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  501. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  502. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  503. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  504. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  505. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  506. };
  507. const char *esr_get_class_string(u32 esr)
  508. {
  509. return esr_class_str[ESR_ELx_EC(esr)];
  510. }
  511. /*
  512. * bad_mode handles the impossible case in the exception vector. This is always
  513. * fatal.
  514. */
  515. asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
  516. {
  517. console_verbose();
  518. pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
  519. handler[reason], smp_processor_id(), esr,
  520. esr_get_class_string(esr));
  521. die("Oops - bad mode", regs, 0);
  522. local_daif_mask();
  523. panic("bad mode");
  524. }
  525. /*
  526. * bad_el0_sync handles unexpected, but potentially recoverable synchronous
  527. * exceptions taken from EL0. Unlike bad_mode, this returns.
  528. */
  529. asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
  530. {
  531. siginfo_t info;
  532. void __user *pc = (void __user *)instruction_pointer(regs);
  533. clear_siginfo(&info);
  534. info.si_signo = SIGILL;
  535. info.si_errno = 0;
  536. info.si_code = ILL_ILLOPC;
  537. info.si_addr = pc;
  538. current->thread.fault_address = 0;
  539. current->thread.fault_code = esr;
  540. arm64_force_sig_info(&info, "Bad EL0 synchronous exception", current);
  541. }
  542. #ifdef CONFIG_VMAP_STACK
  543. DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
  544. __aligned(16);
  545. asmlinkage void handle_bad_stack(struct pt_regs *regs)
  546. {
  547. unsigned long tsk_stk = (unsigned long)current->stack;
  548. unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
  549. unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
  550. unsigned int esr = read_sysreg(esr_el1);
  551. unsigned long far = read_sysreg(far_el1);
  552. console_verbose();
  553. pr_emerg("Insufficient stack space to handle exception!");
  554. pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
  555. pr_emerg("FAR: 0x%016lx\n", far);
  556. pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
  557. tsk_stk, tsk_stk + THREAD_SIZE);
  558. pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
  559. irq_stk, irq_stk + THREAD_SIZE);
  560. pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
  561. ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
  562. __show_regs(regs);
  563. /*
  564. * We use nmi_panic to limit the potential for recusive overflows, and
  565. * to get a better stack trace.
  566. */
  567. nmi_panic(NULL, "kernel stack overflow");
  568. cpu_park_loop();
  569. }
  570. #endif
  571. void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
  572. {
  573. console_verbose();
  574. pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
  575. smp_processor_id(), esr, esr_get_class_string(esr));
  576. if (regs)
  577. __show_regs(regs);
  578. nmi_panic(regs, "Asynchronous SError Interrupt");
  579. cpu_park_loop();
  580. unreachable();
  581. }
  582. bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
  583. {
  584. u32 aet = arm64_ras_serror_get_severity(esr);
  585. switch (aet) {
  586. case ESR_ELx_AET_CE: /* corrected error */
  587. case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
  588. /*
  589. * The CPU can make progress. We may take UEO again as
  590. * a more severe error.
  591. */
  592. return false;
  593. case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
  594. case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
  595. /*
  596. * The CPU can't make progress. The exception may have
  597. * been imprecise.
  598. */
  599. return true;
  600. case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
  601. default:
  602. /* Error has been silently propagated */
  603. arm64_serror_panic(regs, esr);
  604. }
  605. }
  606. asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
  607. {
  608. nmi_enter();
  609. /* non-RAS errors are not containable */
  610. if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
  611. arm64_serror_panic(regs, esr);
  612. nmi_exit();
  613. }
  614. void __pte_error(const char *file, int line, unsigned long val)
  615. {
  616. pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
  617. }
  618. void __pmd_error(const char *file, int line, unsigned long val)
  619. {
  620. pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
  621. }
  622. void __pud_error(const char *file, int line, unsigned long val)
  623. {
  624. pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
  625. }
  626. void __pgd_error(const char *file, int line, unsigned long val)
  627. {
  628. pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
  629. }
  630. /* GENERIC_BUG traps */
  631. int is_valid_bugaddr(unsigned long addr)
  632. {
  633. /*
  634. * bug_handler() only called for BRK #BUG_BRK_IMM.
  635. * So the answer is trivial -- any spurious instances with no
  636. * bug table entry will be rejected by report_bug() and passed
  637. * back to the debug-monitors code and handled as a fatal
  638. * unexpected debug exception.
  639. */
  640. return 1;
  641. }
  642. static int bug_handler(struct pt_regs *regs, unsigned int esr)
  643. {
  644. if (user_mode(regs))
  645. return DBG_HOOK_ERROR;
  646. switch (report_bug(regs->pc, regs)) {
  647. case BUG_TRAP_TYPE_BUG:
  648. die("Oops - BUG", regs, 0);
  649. break;
  650. case BUG_TRAP_TYPE_WARN:
  651. break;
  652. default:
  653. /* unknown/unrecognised bug trap type */
  654. return DBG_HOOK_ERROR;
  655. }
  656. /* If thread survives, skip over the BUG instruction and continue: */
  657. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  658. return DBG_HOOK_HANDLED;
  659. }
  660. static struct break_hook bug_break_hook = {
  661. .esr_val = 0xf2000000 | BUG_BRK_IMM,
  662. .esr_mask = 0xffffffff,
  663. .fn = bug_handler,
  664. };
  665. /*
  666. * Initial handler for AArch64 BRK exceptions
  667. * This handler only used until debug_traps_init().
  668. */
  669. int __init early_brk64(unsigned long addr, unsigned int esr,
  670. struct pt_regs *regs)
  671. {
  672. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  673. }
  674. /* This registration must happen early, before debug_traps_init(). */
  675. void __init trap_init(void)
  676. {
  677. register_break_hook(&bug_break_hook);
  678. }