traps.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 = 0;
  94. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  95. if (regs) {
  96. if (user_mode(regs))
  97. return;
  98. skip = 1;
  99. }
  100. if (!tsk)
  101. tsk = current;
  102. if (!try_get_task_stack(tsk))
  103. return;
  104. if (tsk == current) {
  105. frame.fp = (unsigned long)__builtin_frame_address(0);
  106. frame.pc = (unsigned long)dump_backtrace;
  107. } else {
  108. /*
  109. * task blocked in __switch_to
  110. */
  111. frame.fp = thread_saved_fp(tsk);
  112. frame.pc = thread_saved_pc(tsk);
  113. }
  114. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  115. frame.graph = tsk->curr_ret_stack;
  116. #endif
  117. printk("Call trace:\n");
  118. do {
  119. /* skip until specified stack frame */
  120. if (!skip) {
  121. dump_backtrace_entry(frame.pc);
  122. } else if (frame.fp == regs->regs[29]) {
  123. skip = 0;
  124. /*
  125. * Mostly, this is the case where this function is
  126. * called in panic/abort. As exception handler's
  127. * stack frame does not contain the corresponding pc
  128. * at which an exception has taken place, use regs->pc
  129. * instead.
  130. */
  131. dump_backtrace_entry(regs->pc);
  132. }
  133. } while (!unwind_frame(tsk, &frame));
  134. put_task_stack(tsk);
  135. }
  136. void show_stack(struct task_struct *tsk, unsigned long *sp)
  137. {
  138. dump_backtrace(NULL, tsk);
  139. barrier();
  140. }
  141. #ifdef CONFIG_PREEMPT
  142. #define S_PREEMPT " PREEMPT"
  143. #else
  144. #define S_PREEMPT ""
  145. #endif
  146. #define S_SMP " SMP"
  147. static int __die(const char *str, int err, struct pt_regs *regs)
  148. {
  149. struct task_struct *tsk = current;
  150. static int die_counter;
  151. int ret;
  152. pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
  153. str, err, ++die_counter);
  154. /* trap and error numbers are mostly meaningless on ARM */
  155. ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
  156. if (ret == NOTIFY_STOP)
  157. return ret;
  158. print_modules();
  159. pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
  160. TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk),
  161. end_of_stack(tsk));
  162. show_regs(regs);
  163. if (!user_mode(regs))
  164. dump_instr(KERN_EMERG, regs);
  165. return ret;
  166. }
  167. static DEFINE_RAW_SPINLOCK(die_lock);
  168. /*
  169. * This function is protected against re-entrancy.
  170. */
  171. void die(const char *str, struct pt_regs *regs, int err)
  172. {
  173. int ret;
  174. unsigned long flags;
  175. raw_spin_lock_irqsave(&die_lock, flags);
  176. oops_enter();
  177. console_verbose();
  178. bust_spinlocks(1);
  179. ret = __die(str, err, regs);
  180. if (regs && kexec_should_crash(current))
  181. crash_kexec(regs);
  182. bust_spinlocks(0);
  183. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  184. oops_exit();
  185. if (in_interrupt())
  186. panic("Fatal exception in interrupt");
  187. if (panic_on_oops)
  188. panic("Fatal exception");
  189. raw_spin_unlock_irqrestore(&die_lock, flags);
  190. if (ret != NOTIFY_STOP)
  191. do_exit(SIGSEGV);
  192. }
  193. static bool show_unhandled_signals_ratelimited(void)
  194. {
  195. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
  196. DEFAULT_RATELIMIT_BURST);
  197. return show_unhandled_signals && __ratelimit(&rs);
  198. }
  199. void arm64_force_sig_info(struct siginfo *info, const char *str,
  200. struct task_struct *tsk)
  201. {
  202. unsigned int esr = tsk->thread.fault_code;
  203. struct pt_regs *regs = task_pt_regs(tsk);
  204. if (!unhandled_signal(tsk, info->si_signo))
  205. goto send_sig;
  206. if (!show_unhandled_signals_ratelimited())
  207. goto send_sig;
  208. pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
  209. if (esr)
  210. pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr);
  211. pr_cont("%s", str);
  212. print_vma_addr(KERN_CONT " in ", regs->pc);
  213. pr_cont("\n");
  214. __show_regs(regs);
  215. send_sig:
  216. force_sig_info(info->si_signo, info, tsk);
  217. }
  218. void arm64_notify_die(const char *str, struct pt_regs *regs,
  219. struct siginfo *info, int err)
  220. {
  221. if (user_mode(regs)) {
  222. WARN_ON(regs != current_pt_regs());
  223. current->thread.fault_address = 0;
  224. current->thread.fault_code = err;
  225. arm64_force_sig_info(info, str, current);
  226. } else {
  227. die(str, regs, err);
  228. }
  229. }
  230. void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
  231. {
  232. regs->pc += size;
  233. /*
  234. * If we were single stepping, we want to get the step exception after
  235. * we return from the trap.
  236. */
  237. if (user_mode(regs))
  238. user_fastforward_single_step(current);
  239. }
  240. static LIST_HEAD(undef_hook);
  241. static DEFINE_RAW_SPINLOCK(undef_lock);
  242. void register_undef_hook(struct undef_hook *hook)
  243. {
  244. unsigned long flags;
  245. raw_spin_lock_irqsave(&undef_lock, flags);
  246. list_add(&hook->node, &undef_hook);
  247. raw_spin_unlock_irqrestore(&undef_lock, flags);
  248. }
  249. void unregister_undef_hook(struct undef_hook *hook)
  250. {
  251. unsigned long flags;
  252. raw_spin_lock_irqsave(&undef_lock, flags);
  253. list_del(&hook->node);
  254. raw_spin_unlock_irqrestore(&undef_lock, flags);
  255. }
  256. static int call_undef_hook(struct pt_regs *regs)
  257. {
  258. struct undef_hook *hook;
  259. unsigned long flags;
  260. u32 instr;
  261. int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
  262. void __user *pc = (void __user *)instruction_pointer(regs);
  263. if (!user_mode(regs)) {
  264. __le32 instr_le;
  265. if (probe_kernel_address((__force __le32 *)pc, instr_le))
  266. goto exit;
  267. instr = le32_to_cpu(instr_le);
  268. } else if (compat_thumb_mode(regs)) {
  269. /* 16-bit Thumb instruction */
  270. __le16 instr_le;
  271. if (get_user(instr_le, (__le16 __user *)pc))
  272. goto exit;
  273. instr = le16_to_cpu(instr_le);
  274. if (aarch32_insn_is_wide(instr)) {
  275. u32 instr2;
  276. if (get_user(instr_le, (__le16 __user *)(pc + 2)))
  277. goto exit;
  278. instr2 = le16_to_cpu(instr_le);
  279. instr = (instr << 16) | instr2;
  280. }
  281. } else {
  282. /* 32-bit ARM instruction */
  283. __le32 instr_le;
  284. if (get_user(instr_le, (__le32 __user *)pc))
  285. goto exit;
  286. instr = le32_to_cpu(instr_le);
  287. }
  288. raw_spin_lock_irqsave(&undef_lock, flags);
  289. list_for_each_entry(hook, &undef_hook, node)
  290. if ((instr & hook->instr_mask) == hook->instr_val &&
  291. (regs->pstate & hook->pstate_mask) == hook->pstate_val)
  292. fn = hook->fn;
  293. raw_spin_unlock_irqrestore(&undef_lock, flags);
  294. exit:
  295. return fn ? fn(regs, instr) : 1;
  296. }
  297. void force_signal_inject(int signal, int code, unsigned long address)
  298. {
  299. siginfo_t info;
  300. const char *desc;
  301. struct pt_regs *regs = current_pt_regs();
  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. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  347. BUG_ON(!user_mode(regs));
  348. }
  349. void cpu_enable_cache_maint_trap(const struct arm64_cpu_capabilities *__unused)
  350. {
  351. sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCI, 0);
  352. }
  353. #define __user_cache_maint(insn, address, res) \
  354. if (address >= user_addr_max()) { \
  355. res = -EFAULT; \
  356. } else { \
  357. uaccess_ttbr0_enable(); \
  358. asm volatile ( \
  359. "1: " insn ", %1\n" \
  360. " mov %w0, #0\n" \
  361. "2:\n" \
  362. " .pushsection .fixup,\"ax\"\n" \
  363. " .align 2\n" \
  364. "3: mov %w0, %w2\n" \
  365. " b 2b\n" \
  366. " .popsection\n" \
  367. _ASM_EXTABLE(1b, 3b) \
  368. : "=r" (res) \
  369. : "r" (address), "i" (-EFAULT)); \
  370. uaccess_ttbr0_disable(); \
  371. }
  372. static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
  373. {
  374. unsigned long address;
  375. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  376. int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
  377. int ret = 0;
  378. address = untagged_addr(pt_regs_read_reg(regs, rt));
  379. switch (crm) {
  380. case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
  381. __user_cache_maint("dc civac", address, ret);
  382. break;
  383. case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
  384. __user_cache_maint("dc civac", address, ret);
  385. break;
  386. case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
  387. __user_cache_maint("sys 3, c7, c12, 1", address, ret);
  388. break;
  389. case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
  390. __user_cache_maint("dc civac", address, ret);
  391. break;
  392. case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
  393. __user_cache_maint("ic ivau", address, ret);
  394. break;
  395. default:
  396. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  397. return;
  398. }
  399. if (ret)
  400. arm64_notify_segfault(address);
  401. else
  402. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  403. }
  404. static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
  405. {
  406. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  407. unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
  408. if (cpus_have_const_cap(ARM64_WORKAROUND_1542419)) {
  409. /* Hide DIC so that we can trap the unnecessary maintenance...*/
  410. val &= ~BIT(CTR_DIC_SHIFT);
  411. /* ... and fake IminLine to reduce the number of traps. */
  412. val &= ~CTR_IMINLINE_MASK;
  413. val |= (PAGE_SHIFT - 2) & CTR_IMINLINE_MASK;
  414. }
  415. pt_regs_write_reg(regs, rt, val);
  416. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  417. }
  418. static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
  419. {
  420. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  421. pt_regs_write_reg(regs, rt, arch_counter_get_cntvct());
  422. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  423. }
  424. static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
  425. {
  426. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  427. pt_regs_write_reg(regs, rt, arch_timer_get_rate());
  428. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  429. }
  430. struct sys64_hook {
  431. unsigned int esr_mask;
  432. unsigned int esr_val;
  433. void (*handler)(unsigned int esr, struct pt_regs *regs);
  434. };
  435. static struct sys64_hook sys64_hooks[] = {
  436. {
  437. .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
  438. .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
  439. .handler = user_cache_maint_handler,
  440. },
  441. {
  442. /* Trap read access to CTR_EL0 */
  443. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  444. .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
  445. .handler = ctr_read_handler,
  446. },
  447. {
  448. /* Trap read access to CNTVCT_EL0 */
  449. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  450. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
  451. .handler = cntvct_read_handler,
  452. },
  453. {
  454. /* Trap read access to CNTFRQ_EL0 */
  455. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  456. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
  457. .handler = cntfrq_read_handler,
  458. },
  459. {},
  460. };
  461. asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
  462. {
  463. struct sys64_hook *hook;
  464. for (hook = sys64_hooks; hook->handler; hook++)
  465. if ((hook->esr_mask & esr) == hook->esr_val) {
  466. hook->handler(esr, regs);
  467. return;
  468. }
  469. /*
  470. * New SYS instructions may previously have been undefined at EL0. Fall
  471. * back to our usual undefined instruction handler so that we handle
  472. * these consistently.
  473. */
  474. do_undefinstr(regs);
  475. }
  476. static const char *esr_class_str[] = {
  477. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  478. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  479. [ESR_ELx_EC_WFx] = "WFI/WFE",
  480. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  481. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  482. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  483. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  484. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  485. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  486. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  487. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  488. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  489. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  490. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  491. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  492. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  493. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  494. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  495. [ESR_ELx_EC_SVE] = "SVE",
  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. local_daif_mask();
  531. panic("bad mode");
  532. }
  533. /*
  534. * bad_el0_sync handles unexpected, but potentially recoverable synchronous
  535. * exceptions taken from EL0. Unlike bad_mode, this returns.
  536. */
  537. asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
  538. {
  539. siginfo_t info;
  540. void __user *pc = (void __user *)instruction_pointer(regs);
  541. clear_siginfo(&info);
  542. info.si_signo = SIGILL;
  543. info.si_errno = 0;
  544. info.si_code = ILL_ILLOPC;
  545. info.si_addr = pc;
  546. current->thread.fault_address = 0;
  547. current->thread.fault_code = esr;
  548. arm64_force_sig_info(&info, "Bad EL0 synchronous exception", current);
  549. }
  550. #ifdef CONFIG_VMAP_STACK
  551. DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
  552. __aligned(16);
  553. asmlinkage void handle_bad_stack(struct pt_regs *regs)
  554. {
  555. unsigned long tsk_stk = (unsigned long)current->stack;
  556. unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
  557. unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
  558. unsigned int esr = read_sysreg(esr_el1);
  559. unsigned long far = read_sysreg(far_el1);
  560. console_verbose();
  561. pr_emerg("Insufficient stack space to handle exception!");
  562. pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
  563. pr_emerg("FAR: 0x%016lx\n", far);
  564. pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
  565. tsk_stk, tsk_stk + THREAD_SIZE);
  566. pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
  567. irq_stk, irq_stk + THREAD_SIZE);
  568. pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
  569. ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
  570. __show_regs(regs);
  571. /*
  572. * We use nmi_panic to limit the potential for recusive overflows, and
  573. * to get a better stack trace.
  574. */
  575. nmi_panic(NULL, "kernel stack overflow");
  576. cpu_park_loop();
  577. }
  578. #endif
  579. void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
  580. {
  581. console_verbose();
  582. pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
  583. smp_processor_id(), esr, esr_get_class_string(esr));
  584. if (regs)
  585. __show_regs(regs);
  586. nmi_panic(regs, "Asynchronous SError Interrupt");
  587. cpu_park_loop();
  588. unreachable();
  589. }
  590. bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
  591. {
  592. u32 aet = arm64_ras_serror_get_severity(esr);
  593. switch (aet) {
  594. case ESR_ELx_AET_CE: /* corrected error */
  595. case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
  596. /*
  597. * The CPU can make progress. We may take UEO again as
  598. * a more severe error.
  599. */
  600. return false;
  601. case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
  602. case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
  603. /*
  604. * The CPU can't make progress. The exception may have
  605. * been imprecise.
  606. */
  607. return true;
  608. case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
  609. default:
  610. /* Error has been silently propagated */
  611. arm64_serror_panic(regs, esr);
  612. }
  613. }
  614. asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
  615. {
  616. nmi_enter();
  617. /* non-RAS errors are not containable */
  618. if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
  619. arm64_serror_panic(regs, esr);
  620. nmi_exit();
  621. }
  622. void __pte_error(const char *file, int line, unsigned long val)
  623. {
  624. pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
  625. }
  626. void __pmd_error(const char *file, int line, unsigned long val)
  627. {
  628. pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
  629. }
  630. void __pud_error(const char *file, int line, unsigned long val)
  631. {
  632. pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
  633. }
  634. void __pgd_error(const char *file, int line, unsigned long val)
  635. {
  636. pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
  637. }
  638. /* GENERIC_BUG traps */
  639. int is_valid_bugaddr(unsigned long addr)
  640. {
  641. /*
  642. * bug_handler() only called for BRK #BUG_BRK_IMM.
  643. * So the answer is trivial -- any spurious instances with no
  644. * bug table entry will be rejected by report_bug() and passed
  645. * back to the debug-monitors code and handled as a fatal
  646. * unexpected debug exception.
  647. */
  648. return 1;
  649. }
  650. static int bug_handler(struct pt_regs *regs, unsigned int esr)
  651. {
  652. if (user_mode(regs))
  653. return DBG_HOOK_ERROR;
  654. switch (report_bug(regs->pc, regs)) {
  655. case BUG_TRAP_TYPE_BUG:
  656. die("Oops - BUG", regs, 0);
  657. break;
  658. case BUG_TRAP_TYPE_WARN:
  659. break;
  660. default:
  661. /* unknown/unrecognised bug trap type */
  662. return DBG_HOOK_ERROR;
  663. }
  664. /* If thread survives, skip over the BUG instruction and continue: */
  665. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  666. return DBG_HOOK_HANDLED;
  667. }
  668. static struct break_hook bug_break_hook = {
  669. .esr_val = 0xf2000000 | BUG_BRK_IMM,
  670. .esr_mask = 0xffffffff,
  671. .fn = bug_handler,
  672. };
  673. /*
  674. * Initial handler for AArch64 BRK exceptions
  675. * This handler only used until debug_traps_init().
  676. */
  677. int __init early_brk64(unsigned long addr, unsigned int esr,
  678. struct pt_regs *regs)
  679. {
  680. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  681. }
  682. /* This registration must happen early, before debug_traps_init(). */
  683. void __init trap_init(void)
  684. {
  685. register_break_hook(&bug_break_hook);
  686. }