traps.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 <linux/kasan.h>
  38. #include <asm/atomic.h>
  39. #include <asm/bug.h>
  40. #include <asm/cpufeature.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. #include <mt-plat/aee.h>
  52. static const char *handler[]= {
  53. "Synchronous Abort",
  54. "IRQ",
  55. "FIQ",
  56. "Error"
  57. };
  58. int show_unhandled_signals = 0;
  59. static void dump_backtrace_entry(unsigned long where)
  60. {
  61. printk(" %pS\n", (void *)where);
  62. }
  63. static void __dump_instr(const char *lvl, struct pt_regs *regs)
  64. {
  65. unsigned long addr = instruction_pointer(regs);
  66. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  67. int i;
  68. for (i = -4; i < 1; i++) {
  69. unsigned int val, bad;
  70. bad = get_user(val, &((u32 *)addr)[i]);
  71. if (!bad)
  72. p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
  73. else {
  74. p += sprintf(p, "bad PC value");
  75. break;
  76. }
  77. }
  78. printk("%sCode: %s\n", lvl, str);
  79. }
  80. static void dump_instr(const char *lvl, struct pt_regs *regs)
  81. {
  82. if (!user_mode(regs)) {
  83. mm_segment_t fs = get_fs();
  84. set_fs(KERNEL_DS);
  85. __dump_instr(lvl, regs);
  86. set_fs(fs);
  87. } else {
  88. __dump_instr(lvl, regs);
  89. }
  90. }
  91. void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  92. {
  93. struct stackframe frame;
  94. int skip = 0;
  95. unsigned long prev_fp = 0;
  96. pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
  97. if (regs) {
  98. if (user_mode(regs))
  99. return;
  100. skip = 1;
  101. }
  102. if (!tsk)
  103. tsk = current;
  104. if (!try_get_task_stack(tsk))
  105. return;
  106. if (tsk == current) {
  107. frame.fp = (unsigned long)__builtin_frame_address(0);
  108. frame.pc = (unsigned long)dump_backtrace;
  109. } else {
  110. /*
  111. * task blocked in __switch_to
  112. */
  113. frame.fp = thread_saved_fp(tsk);
  114. frame.pc = thread_saved_pc(tsk);
  115. }
  116. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  117. frame.graph = tsk->curr_ret_stack;
  118. #endif
  119. printk("Call trace:\n");
  120. do {
  121. /* skip until specified stack frame */
  122. if (!skip) {
  123. dump_backtrace_entry(frame.pc);
  124. } else if (frame.fp == regs->regs[29]) {
  125. skip = 0;
  126. /*
  127. * Mostly, this is the case where this function is
  128. * called in panic/abort. As exception handler's
  129. * stack frame does not contain the corresponding pc
  130. * at which an exception has taken place, use regs->pc
  131. * instead.
  132. */
  133. dump_backtrace_entry(regs->pc);
  134. }
  135. if (prev_fp && frame.fp == prev_fp)
  136. break;
  137. if (frame.fp)
  138. prev_fp = frame.fp;
  139. } while (!unwind_frame(tsk, &frame));
  140. put_task_stack(tsk);
  141. }
  142. void show_stack(struct task_struct *tsk, unsigned long *sp)
  143. {
  144. dump_backtrace(NULL, tsk);
  145. barrier();
  146. }
  147. #ifdef CONFIG_PREEMPT
  148. #define S_PREEMPT " PREEMPT"
  149. #else
  150. #define S_PREEMPT ""
  151. #endif
  152. #define S_SMP " SMP"
  153. static int __die(const char *str, int err, struct pt_regs *regs)
  154. {
  155. struct task_struct *tsk = current;
  156. static int die_counter;
  157. int ret;
  158. pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
  159. str, err, ++die_counter);
  160. /* trap and error numbers are mostly meaningless on ARM */
  161. ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
  162. if (ret == NOTIFY_STOP)
  163. return ret;
  164. print_modules();
  165. pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
  166. TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk),
  167. end_of_stack(tsk));
  168. show_regs(regs);
  169. if (!user_mode(regs))
  170. dump_instr(KERN_EMERG, regs);
  171. return ret;
  172. }
  173. static DEFINE_RAW_SPINLOCK(die_lock);
  174. /*
  175. * This function is protected against re-entrancy.
  176. */
  177. void die(const char *str, struct pt_regs *regs, int err)
  178. {
  179. int ret;
  180. unsigned long flags;
  181. struct thread_info *thread = current_thread_info();
  182. int cpu = -1;
  183. static int die_owner = -1;
  184. if (ESR_ELx_EC(err) == ESR_ELx_EC_DABT_CUR)
  185. thread->cpu_excp++;
  186. #ifdef CONFIG_MTK_AEE_IPANIC
  187. if (die_owner == -1)
  188. aee_save_excp_regs(regs);
  189. #endif
  190. oops_enter();
  191. cpu = get_cpu();
  192. if (!raw_spin_trylock_irqsave(&die_lock, flags)) {
  193. if (cpu != die_owner) {
  194. pr_notice("die_lock:cpu:%d trylock failed(owner:%d)\n",
  195. cpu, die_owner);
  196. dump_stack();
  197. put_cpu();
  198. while (1)
  199. cpu_relax();
  200. } else {
  201. pr_notice("die_lock:cpu:%d already locked(owner:%d)\n",
  202. cpu, die_owner);
  203. dump_stack();
  204. }
  205. }
  206. die_owner = cpu;
  207. console_verbose();
  208. bust_spinlocks(1);
  209. ret = __die(str, err, regs);
  210. if (regs && kexec_should_crash(current))
  211. crash_kexec(regs);
  212. bust_spinlocks(0);
  213. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  214. oops_exit();
  215. if (in_interrupt())
  216. panic("Fatal exception in interrupt");
  217. if (panic_on_oops)
  218. panic("Fatal exception");
  219. raw_spin_unlock_irqrestore(&die_lock, flags);
  220. if (ret != NOTIFY_STOP)
  221. do_exit(SIGSEGV);
  222. }
  223. void arm64_notify_die(const char *str, struct pt_regs *regs,
  224. struct siginfo *info, int err)
  225. {
  226. if (user_mode(regs)) {
  227. current->thread.fault_address = 0;
  228. current->thread.fault_code = err;
  229. force_sig_info(info->si_signo, info, current);
  230. } else {
  231. die(str, regs, err);
  232. }
  233. }
  234. void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
  235. {
  236. regs->pc += size;
  237. /*
  238. * If we were single stepping, we want to get the step exception after
  239. * we return from the trap.
  240. */
  241. if (user_mode(regs))
  242. user_fastforward_single_step(current);
  243. }
  244. static LIST_HEAD(undef_hook);
  245. static DEFINE_RAW_SPINLOCK(undef_lock);
  246. void register_undef_hook(struct undef_hook *hook)
  247. {
  248. unsigned long flags;
  249. raw_spin_lock_irqsave(&undef_lock, flags);
  250. list_add(&hook->node, &undef_hook);
  251. raw_spin_unlock_irqrestore(&undef_lock, flags);
  252. }
  253. void unregister_undef_hook(struct undef_hook *hook)
  254. {
  255. unsigned long flags;
  256. raw_spin_lock_irqsave(&undef_lock, flags);
  257. list_del(&hook->node);
  258. raw_spin_unlock_irqrestore(&undef_lock, flags);
  259. }
  260. static int call_undef_hook(struct pt_regs *regs)
  261. {
  262. struct undef_hook *hook;
  263. unsigned long flags;
  264. u32 instr;
  265. int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
  266. void __user *pc = (void __user *)instruction_pointer(regs);
  267. if (!user_mode(regs)) {
  268. __le32 instr_le;
  269. if (probe_kernel_address((__force __le32 *)pc, instr_le))
  270. goto exit;
  271. instr = le32_to_cpu(instr_le);
  272. } else if (compat_thumb_mode(regs)) {
  273. /* 16-bit Thumb instruction */
  274. __le16 instr_le;
  275. if (get_user(instr_le, (__le16 __user *)pc))
  276. goto exit;
  277. instr = le16_to_cpu(instr_le);
  278. if (aarch32_insn_is_wide(instr)) {
  279. u32 instr2;
  280. if (get_user(instr_le, (__le16 __user *)(pc + 2)))
  281. goto exit;
  282. instr2 = le16_to_cpu(instr_le);
  283. instr = (instr << 16) | instr2;
  284. }
  285. } else {
  286. /* 32-bit ARM instruction */
  287. __le32 instr_le;
  288. if (get_user(instr_le, (__le32 __user *)pc))
  289. goto exit;
  290. instr = le32_to_cpu(instr_le);
  291. }
  292. raw_spin_lock_irqsave(&undef_lock, flags);
  293. list_for_each_entry(hook, &undef_hook, node)
  294. if ((instr & hook->instr_mask) == hook->instr_val &&
  295. (regs->pstate & hook->pstate_mask) == hook->pstate_val)
  296. fn = hook->fn;
  297. raw_spin_unlock_irqrestore(&undef_lock, flags);
  298. exit:
  299. return fn ? fn(regs, instr) : 1;
  300. }
  301. static void force_signal_inject(int signal, int code, struct pt_regs *regs,
  302. unsigned long address)
  303. {
  304. siginfo_t info;
  305. void __user *pc = (void __user *)instruction_pointer(regs);
  306. const char *desc;
  307. switch (signal) {
  308. case SIGILL:
  309. desc = "undefined instruction";
  310. break;
  311. case SIGSEGV:
  312. desc = "illegal memory access";
  313. break;
  314. default:
  315. desc = "bad mode";
  316. break;
  317. }
  318. if (unhandled_signal(current, signal) &&
  319. show_unhandled_signals_ratelimited()) {
  320. pr_info("%s[%d]: %s: pc=%p\n",
  321. current->comm, task_pid_nr(current), desc, pc);
  322. dump_instr(KERN_INFO, regs);
  323. }
  324. info.si_signo = signal;
  325. info.si_errno = 0;
  326. info.si_code = code;
  327. info.si_addr = pc;
  328. arm64_notify_die(desc, regs, &info, 0);
  329. }
  330. /*
  331. * Set up process info to signal segmentation fault - called on access error.
  332. */
  333. void arm64_notify_segfault(struct pt_regs *regs, unsigned long addr)
  334. {
  335. int code;
  336. down_read(&current->mm->mmap_sem);
  337. if (find_vma(current->mm, addr) == NULL)
  338. code = SEGV_MAPERR;
  339. else
  340. code = SEGV_ACCERR;
  341. up_read(&current->mm->mmap_sem);
  342. force_signal_inject(SIGSEGV, code, regs, addr);
  343. }
  344. asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
  345. {
  346. /* check for AArch32 breakpoint instructions */
  347. if (!aarch32_break_handler(regs))
  348. return;
  349. if (call_undef_hook(regs) == 0)
  350. return;
  351. BUG_ON(!user_mode(regs));
  352. force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
  353. }
  354. void cpu_enable_cache_maint_trap(const struct arm64_cpu_capabilities *__unused)
  355. {
  356. config_sctlr_el1(SCTLR_EL1_UCI, 0);
  357. }
  358. #define __user_cache_maint(insn, address, res) \
  359. if (address >= user_addr_max()) { \
  360. res = -EFAULT; \
  361. } else { \
  362. uaccess_ttbr0_enable(); \
  363. asm volatile ( \
  364. "1: " insn ", %1\n" \
  365. " mov %w0, #0\n" \
  366. "2:\n" \
  367. " .pushsection .fixup,\"ax\"\n" \
  368. " .align 2\n" \
  369. "3: mov %w0, %w2\n" \
  370. " b 2b\n" \
  371. " .popsection\n" \
  372. _ASM_EXTABLE(1b, 3b) \
  373. : "=r" (res) \
  374. : "r" (address), "i" (-EFAULT)); \
  375. uaccess_ttbr0_disable(); \
  376. }
  377. static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
  378. {
  379. unsigned long address;
  380. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  381. int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
  382. int ret = 0;
  383. address = untagged_addr(pt_regs_read_reg(regs, rt));
  384. switch (crm) {
  385. case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
  386. __user_cache_maint("dc civac", address, ret);
  387. break;
  388. case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
  389. __user_cache_maint("dc civac", address, ret);
  390. break;
  391. case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
  392. __user_cache_maint("sys 3, c7, c12, 1", address, ret);
  393. break;
  394. case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
  395. __user_cache_maint("dc civac", address, ret);
  396. break;
  397. case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
  398. __user_cache_maint("ic ivau", address, ret);
  399. break;
  400. default:
  401. force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
  402. return;
  403. }
  404. if (ret)
  405. arm64_notify_segfault(regs, address);
  406. else
  407. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  408. }
  409. static void ctr_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. unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
  413. pt_regs_write_reg(regs, rt, val);
  414. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  415. }
  416. static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
  417. {
  418. int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
  419. pt_regs_write_reg(regs, rt, arch_counter_get_cntvct());
  420. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  421. }
  422. static void cntfrq_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. pt_regs_write_reg(regs, rt, arch_timer_get_rate());
  426. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  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. /* Trap read access to CNTVCT_EL0 */
  447. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  448. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
  449. .handler = cntvct_read_handler,
  450. },
  451. {
  452. /* Trap read access to CNTFRQ_EL0 */
  453. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  454. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
  455. .handler = cntfrq_read_handler,
  456. },
  457. {},
  458. };
  459. asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
  460. {
  461. struct sys64_hook *hook;
  462. for (hook = sys64_hooks; hook->handler; hook++)
  463. if ((hook->esr_mask & esr) == hook->esr_val) {
  464. hook->handler(esr, regs);
  465. return;
  466. }
  467. /*
  468. * New SYS instructions may previously have been undefined at EL0. Fall
  469. * back to our usual undefined instruction handler so that we handle
  470. * these consistently.
  471. */
  472. do_undefinstr(regs);
  473. }
  474. long compat_arm_syscall(struct pt_regs *regs);
  475. asmlinkage long do_ni_syscall(struct pt_regs *regs)
  476. {
  477. #ifdef CONFIG_COMPAT
  478. long ret;
  479. if (is_compat_task()) {
  480. ret = compat_arm_syscall(regs);
  481. if (ret != -ENOSYS)
  482. return ret;
  483. }
  484. #endif
  485. return sys_ni_syscall();
  486. }
  487. #ifdef CONFIG_MEDIATEK_SOLUTION
  488. static void (*async_abort_handler)(struct pt_regs *regs, void *);
  489. static void *async_abort_priv;
  490. int register_async_abort_handler(
  491. void (*fn)(struct pt_regs *regs, void *), void *priv)
  492. {
  493. async_abort_handler = fn;
  494. async_abort_priv = priv;
  495. return 0;
  496. }
  497. #endif
  498. static const char *esr_class_str[] = {
  499. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  500. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  501. [ESR_ELx_EC_WFx] = "WFI/WFE",
  502. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  503. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  504. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  505. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  506. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  507. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  508. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  509. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  510. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  511. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  512. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  513. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  514. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  515. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  516. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  517. [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
  518. [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
  519. [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
  520. [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
  521. [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
  522. [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
  523. [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
  524. [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
  525. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  526. [ESR_ELx_EC_SERROR] = "SError",
  527. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  528. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  529. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  530. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  531. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  532. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  533. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  534. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  535. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  536. };
  537. const char *esr_get_class_string(u32 esr)
  538. {
  539. return esr_class_str[ESR_ELx_EC(esr)];
  540. }
  541. /*
  542. * bad_mode handles the impossible case in the exception vector. This is always
  543. * fatal.
  544. */
  545. asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
  546. {
  547. console_verbose();
  548. #ifdef CONFIG_MEDIATEK_SOLUTION
  549. /*
  550. * reason is defined in entry.S, 3 means BAD_ERROR,
  551. * which would be triggered by async abort
  552. */
  553. if ((reason == 3) && async_abort_handler)
  554. async_abort_handler(regs, async_abort_priv);
  555. #endif
  556. pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
  557. handler[reason], smp_processor_id(), esr,
  558. esr_get_class_string(esr));
  559. local_irq_disable();
  560. panic("bad mode");
  561. }
  562. /*
  563. * bad_el0_sync handles unexpected, but potentially recoverable synchronous
  564. * exceptions taken from EL0. Unlike bad_mode, this returns.
  565. */
  566. asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
  567. {
  568. siginfo_t info;
  569. void __user *pc = (void __user *)instruction_pointer(regs);
  570. console_verbose();
  571. pr_crit("Bad EL0 synchronous exception detected on CPU%d, code 0x%08x -- %s\n",
  572. smp_processor_id(), esr, esr_get_class_string(esr));
  573. __show_regs(regs);
  574. info.si_signo = SIGILL;
  575. info.si_errno = 0;
  576. info.si_code = ILL_ILLOPC;
  577. info.si_addr = pc;
  578. current->thread.fault_address = 0;
  579. current->thread.fault_code = 0;
  580. force_sig_info(info.si_signo, &info, current);
  581. }
  582. #ifdef CONFIG_VMAP_STACK
  583. DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
  584. __aligned(16);
  585. asmlinkage void handle_bad_stack(struct pt_regs *regs)
  586. {
  587. unsigned long tsk_stk = (unsigned long)current->stack;
  588. unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
  589. unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
  590. unsigned int esr = read_sysreg(esr_el1);
  591. unsigned long far = read_sysreg(far_el1);
  592. console_verbose();
  593. pr_emerg("Insufficient stack space to handle exception!");
  594. pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
  595. pr_emerg("FAR: 0x%016lx\n", far);
  596. pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
  597. tsk_stk, tsk_stk + THREAD_SIZE);
  598. pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
  599. irq_stk, irq_stk + THREAD_SIZE);
  600. pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
  601. ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
  602. __show_regs(regs);
  603. /*
  604. * We use nmi_panic to limit the potential for recusive overflows, and
  605. * to get a better stack trace.
  606. */
  607. nmi_panic(NULL, "kernel stack overflow");
  608. cpu_park_loop();
  609. }
  610. #endif
  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. #ifdef CONFIG_KASAN_SW_TAGS
  663. #define KASAN_ESR_RECOVER 0x20
  664. #define KASAN_ESR_WRITE 0x10
  665. #define KASAN_ESR_SIZE_MASK 0x0f
  666. #define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK))
  667. static int kasan_handler(struct pt_regs *regs, unsigned int esr)
  668. {
  669. bool recover = esr & KASAN_ESR_RECOVER;
  670. bool write = esr & KASAN_ESR_WRITE;
  671. size_t size = KASAN_ESR_SIZE(esr);
  672. u64 addr = regs->regs[0];
  673. u64 pc = regs->pc;
  674. if (user_mode(regs))
  675. return DBG_HOOK_ERROR;
  676. kasan_report(addr, size, write, pc);
  677. /*
  678. * The instrumentation allows to control whether we can proceed after
  679. * a crash was detected. This is done by passing the -recover flag to
  680. * the compiler. Disabling recovery allows to generate more compact
  681. * code.
  682. *
  683. * Unfortunately disabling recovery doesn't work for the kernel right
  684. * now. KASAN reporting is disabled in some contexts (for example when
  685. * the allocator accesses slab object metadata; this is controlled by
  686. * current->kasan_depth). All these accesses are detected by the tool,
  687. * even though the reports for them are not printed.
  688. *
  689. * This is something that might be fixed at some point in the future.
  690. */
  691. if (!recover)
  692. die("Oops - KASAN", regs, 0);
  693. /* If thread survives, skip over the brk instruction and continue: */
  694. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  695. return DBG_HOOK_HANDLED;
  696. }
  697. #define KASAN_ESR_VAL (0xf2000000 | KASAN_BRK_IMM)
  698. #define KASAN_ESR_MASK 0xffffff00
  699. static struct break_hook kasan_break_hook = {
  700. .esr_val = KASAN_ESR_VAL,
  701. .esr_mask = KASAN_ESR_MASK,
  702. .fn = kasan_handler,
  703. };
  704. #endif
  705. /*
  706. * Initial handler for AArch64 BRK exceptions
  707. * This handler only used until debug_traps_init().
  708. */
  709. int __init early_brk64(unsigned long addr, unsigned int esr,
  710. struct pt_regs *regs)
  711. {
  712. #ifdef CONFIG_KASAN_SW_TAGS
  713. if ((esr & KASAN_ESR_MASK) == KASAN_ESR_VAL)
  714. return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
  715. #endif
  716. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  717. }
  718. /* This registration must happen early, before debug_traps_init(). */
  719. void __init trap_init(void)
  720. {
  721. register_break_hook(&bug_break_hook);
  722. #ifdef CONFIG_KASAN_SW_TAGS
  723. register_break_hook(&kasan_break_hook);
  724. #endif
  725. }