traps.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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 void arm64_show_signal(int signo, const char *str)
  192. {
  193. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
  194. DEFAULT_RATELIMIT_BURST);
  195. struct task_struct *tsk = current;
  196. unsigned int esr = tsk->thread.fault_code;
  197. struct pt_regs *regs = task_pt_regs(tsk);
  198. /* Leave if the signal won't be shown */
  199. if (!show_unhandled_signals ||
  200. !unhandled_signal(tsk, signo) ||
  201. !__ratelimit(&rs))
  202. return;
  203. pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
  204. if (esr)
  205. pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr);
  206. pr_cont("%s", str);
  207. print_vma_addr(KERN_CONT " in ", regs->pc);
  208. pr_cont("\n");
  209. __show_regs(regs);
  210. }
  211. void arm64_force_sig_fault(int signo, int code, void __user *addr,
  212. const char *str)
  213. {
  214. arm64_show_signal(signo, str);
  215. force_sig_fault(signo, code, addr, current);
  216. }
  217. void arm64_force_sig_mceerr(int code, void __user *addr, short lsb,
  218. const char *str)
  219. {
  220. arm64_show_signal(SIGBUS, str);
  221. force_sig_mceerr(code, addr, lsb, current);
  222. }
  223. void arm64_force_sig_ptrace_errno_trap(int errno, void __user *addr,
  224. const char *str)
  225. {
  226. arm64_show_signal(SIGTRAP, str);
  227. force_sig_ptrace_errno_trap(errno, addr);
  228. }
  229. void arm64_notify_die(const char *str, struct pt_regs *regs,
  230. int signo, int sicode, void __user *addr,
  231. int err)
  232. {
  233. if (user_mode(regs)) {
  234. WARN_ON(regs != current_pt_regs());
  235. current->thread.fault_address = 0;
  236. current->thread.fault_code = err;
  237. arm64_force_sig_fault(signo, sicode, addr, str);
  238. } else {
  239. die(str, regs, err);
  240. }
  241. }
  242. void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
  243. {
  244. regs->pc += size;
  245. /*
  246. * If we were single stepping, we want to get the step exception after
  247. * we return from the trap.
  248. */
  249. if (user_mode(regs))
  250. user_fastforward_single_step(current);
  251. }
  252. static LIST_HEAD(undef_hook);
  253. static DEFINE_RAW_SPINLOCK(undef_lock);
  254. void register_undef_hook(struct undef_hook *hook)
  255. {
  256. unsigned long flags;
  257. raw_spin_lock_irqsave(&undef_lock, flags);
  258. list_add(&hook->node, &undef_hook);
  259. raw_spin_unlock_irqrestore(&undef_lock, flags);
  260. }
  261. void unregister_undef_hook(struct undef_hook *hook)
  262. {
  263. unsigned long flags;
  264. raw_spin_lock_irqsave(&undef_lock, flags);
  265. list_del(&hook->node);
  266. raw_spin_unlock_irqrestore(&undef_lock, flags);
  267. }
  268. static int call_undef_hook(struct pt_regs *regs)
  269. {
  270. struct undef_hook *hook;
  271. unsigned long flags;
  272. u32 instr;
  273. int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
  274. void __user *pc = (void __user *)instruction_pointer(regs);
  275. if (!user_mode(regs)) {
  276. __le32 instr_le;
  277. if (probe_kernel_address((__force __le32 *)pc, instr_le))
  278. goto exit;
  279. instr = le32_to_cpu(instr_le);
  280. } else if (compat_thumb_mode(regs)) {
  281. /* 16-bit Thumb instruction */
  282. __le16 instr_le;
  283. if (get_user(instr_le, (__le16 __user *)pc))
  284. goto exit;
  285. instr = le16_to_cpu(instr_le);
  286. if (aarch32_insn_is_wide(instr)) {
  287. u32 instr2;
  288. if (get_user(instr_le, (__le16 __user *)(pc + 2)))
  289. goto exit;
  290. instr2 = le16_to_cpu(instr_le);
  291. instr = (instr << 16) | instr2;
  292. }
  293. } else {
  294. /* 32-bit ARM instruction */
  295. __le32 instr_le;
  296. if (get_user(instr_le, (__le32 __user *)pc))
  297. goto exit;
  298. instr = le32_to_cpu(instr_le);
  299. }
  300. raw_spin_lock_irqsave(&undef_lock, flags);
  301. list_for_each_entry(hook, &undef_hook, node)
  302. if ((instr & hook->instr_mask) == hook->instr_val &&
  303. (regs->pstate & hook->pstate_mask) == hook->pstate_val)
  304. fn = hook->fn;
  305. raw_spin_unlock_irqrestore(&undef_lock, flags);
  306. exit:
  307. return fn ? fn(regs, instr) : 1;
  308. }
  309. void force_signal_inject(int signal, int code, unsigned long address)
  310. {
  311. const char *desc;
  312. struct pt_regs *regs = current_pt_regs();
  313. if (WARN_ON(!user_mode(regs)))
  314. return;
  315. switch (signal) {
  316. case SIGILL:
  317. desc = "undefined instruction";
  318. break;
  319. case SIGSEGV:
  320. desc = "illegal memory access";
  321. break;
  322. default:
  323. desc = "unknown or unrecoverable error";
  324. break;
  325. }
  326. /* Force signals we don't understand to SIGKILL */
  327. if (WARN_ON(signal != SIGKILL &&
  328. siginfo_layout(signal, code) != SIL_FAULT)) {
  329. signal = SIGKILL;
  330. }
  331. arm64_notify_die(desc, regs, signal, code, (void __user *)address, 0);
  332. }
  333. /*
  334. * Set up process info to signal segmentation fault - called on access error.
  335. */
  336. void arm64_notify_segfault(unsigned long addr)
  337. {
  338. int code;
  339. down_read(&current->mm->mmap_sem);
  340. if (find_vma(current->mm, addr) == NULL)
  341. code = SEGV_MAPERR;
  342. else
  343. code = SEGV_ACCERR;
  344. up_read(&current->mm->mmap_sem);
  345. force_signal_inject(SIGSEGV, code, addr);
  346. }
  347. asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
  348. {
  349. /* check for AArch32 breakpoint instructions */
  350. if (!aarch32_break_handler(regs))
  351. return;
  352. if (call_undef_hook(regs) == 0)
  353. return;
  354. BUG_ON(!user_mode(regs));
  355. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  356. }
  357. #define __user_cache_maint(insn, address, res) \
  358. if (address >= user_addr_max()) { \
  359. res = -EFAULT; \
  360. } else { \
  361. uaccess_ttbr0_enable(); \
  362. asm volatile ( \
  363. "1: " insn ", %1\n" \
  364. " mov %w0, #0\n" \
  365. "2:\n" \
  366. " .pushsection .fixup,\"ax\"\n" \
  367. " .align 2\n" \
  368. "3: mov %w0, %w2\n" \
  369. " b 2b\n" \
  370. " .popsection\n" \
  371. _ASM_EXTABLE(1b, 3b) \
  372. : "=r" (res) \
  373. : "r" (address), "i" (-EFAULT)); \
  374. uaccess_ttbr0_disable(); \
  375. }
  376. static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
  377. {
  378. unsigned long address;
  379. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  380. int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
  381. int ret = 0;
  382. address = untagged_addr(pt_regs_read_reg(regs, rt));
  383. switch (crm) {
  384. case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
  385. __user_cache_maint("dc civac", address, ret);
  386. break;
  387. case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
  388. __user_cache_maint("dc civac", address, ret);
  389. break;
  390. case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
  391. __user_cache_maint("sys 3, c7, c12, 1", address, ret);
  392. break;
  393. case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
  394. __user_cache_maint("dc civac", address, ret);
  395. break;
  396. case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
  397. __user_cache_maint("ic ivau", address, ret);
  398. break;
  399. default:
  400. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  401. return;
  402. }
  403. if (ret)
  404. arm64_notify_segfault(address);
  405. else
  406. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  407. }
  408. static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
  409. {
  410. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  411. unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
  412. pt_regs_write_reg(regs, rt, val);
  413. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  414. }
  415. static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
  416. {
  417. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  418. pt_regs_write_reg(regs, rt, arch_counter_get_cntvct());
  419. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  420. }
  421. static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
  422. {
  423. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  424. pt_regs_write_reg(regs, rt, arch_timer_get_rate());
  425. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  426. }
  427. static void mrs_handler(unsigned int esr, struct pt_regs *regs)
  428. {
  429. u32 sysreg, rt;
  430. rt = ESR_ELx_SYS64_ISS_RT(esr);
  431. sysreg = esr_sys64_to_sysreg(esr);
  432. if (do_emulate_mrs(regs, sysreg, rt) != 0)
  433. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  434. }
  435. static void wfi_handler(unsigned int esr, struct pt_regs *regs)
  436. {
  437. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  438. }
  439. struct sys64_hook {
  440. unsigned int esr_mask;
  441. unsigned int esr_val;
  442. void (*handler)(unsigned int esr, struct pt_regs *regs);
  443. };
  444. static struct sys64_hook sys64_hooks[] = {
  445. {
  446. .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
  447. .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
  448. .handler = user_cache_maint_handler,
  449. },
  450. {
  451. /* Trap read access to CTR_EL0 */
  452. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  453. .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
  454. .handler = ctr_read_handler,
  455. },
  456. {
  457. /* Trap read access to CNTVCT_EL0 */
  458. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  459. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
  460. .handler = cntvct_read_handler,
  461. },
  462. {
  463. /* Trap read access to CNTFRQ_EL0 */
  464. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  465. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
  466. .handler = cntfrq_read_handler,
  467. },
  468. {
  469. /* Trap read access to CPUID registers */
  470. .esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
  471. .esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
  472. .handler = mrs_handler,
  473. },
  474. {
  475. /* Trap WFI instructions executed in userspace */
  476. .esr_mask = ESR_ELx_WFx_MASK,
  477. .esr_val = ESR_ELx_WFx_WFI_VAL,
  478. .handler = wfi_handler,
  479. },
  480. {},
  481. };
  482. #ifdef CONFIG_COMPAT
  483. #define PSTATE_IT_1_0_SHIFT 25
  484. #define PSTATE_IT_1_0_MASK (0x3 << PSTATE_IT_1_0_SHIFT)
  485. #define PSTATE_IT_7_2_SHIFT 10
  486. #define PSTATE_IT_7_2_MASK (0x3f << PSTATE_IT_7_2_SHIFT)
  487. static u32 compat_get_it_state(struct pt_regs *regs)
  488. {
  489. u32 it, pstate = regs->pstate;
  490. it = (pstate & PSTATE_IT_1_0_MASK) >> PSTATE_IT_1_0_SHIFT;
  491. it |= ((pstate & PSTATE_IT_7_2_MASK) >> PSTATE_IT_7_2_SHIFT) << 2;
  492. return it;
  493. }
  494. static void compat_set_it_state(struct pt_regs *regs, u32 it)
  495. {
  496. u32 pstate_it;
  497. pstate_it = (it << PSTATE_IT_1_0_SHIFT) & PSTATE_IT_1_0_MASK;
  498. pstate_it |= ((it >> 2) << PSTATE_IT_7_2_SHIFT) & PSTATE_IT_7_2_MASK;
  499. regs->pstate &= ~PSR_AA32_IT_MASK;
  500. regs->pstate |= pstate_it;
  501. }
  502. static bool cp15_cond_valid(unsigned int esr, struct pt_regs *regs)
  503. {
  504. int cond;
  505. /* Only a T32 instruction can trap without CV being set */
  506. if (!(esr & ESR_ELx_CV)) {
  507. u32 it;
  508. it = compat_get_it_state(regs);
  509. if (!it)
  510. return true;
  511. cond = it >> 4;
  512. } else {
  513. cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
  514. }
  515. return aarch32_opcode_cond_checks[cond](regs->pstate);
  516. }
  517. static void advance_itstate(struct pt_regs *regs)
  518. {
  519. u32 it;
  520. /* ARM mode */
  521. if (!(regs->pstate & PSR_AA32_T_BIT) ||
  522. !(regs->pstate & PSR_AA32_IT_MASK))
  523. return;
  524. it = compat_get_it_state(regs);
  525. /*
  526. * If this is the last instruction of the block, wipe the IT
  527. * state. Otherwise advance it.
  528. */
  529. if (!(it & 7))
  530. it = 0;
  531. else
  532. it = (it & 0xe0) | ((it << 1) & 0x1f);
  533. compat_set_it_state(regs, it);
  534. }
  535. static void arm64_compat_skip_faulting_instruction(struct pt_regs *regs,
  536. unsigned int sz)
  537. {
  538. advance_itstate(regs);
  539. arm64_skip_faulting_instruction(regs, sz);
  540. }
  541. static void compat_cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
  542. {
  543. int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
  544. pt_regs_write_reg(regs, reg, arch_timer_get_rate());
  545. arm64_compat_skip_faulting_instruction(regs, 4);
  546. }
  547. static struct sys64_hook cp15_32_hooks[] = {
  548. {
  549. .esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
  550. .esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
  551. .handler = compat_cntfrq_read_handler,
  552. },
  553. {},
  554. };
  555. static void compat_cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
  556. {
  557. int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
  558. int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
  559. u64 val = arch_counter_get_cntvct();
  560. pt_regs_write_reg(regs, rt, lower_32_bits(val));
  561. pt_regs_write_reg(regs, rt2, upper_32_bits(val));
  562. arm64_compat_skip_faulting_instruction(regs, 4);
  563. }
  564. static struct sys64_hook cp15_64_hooks[] = {
  565. {
  566. .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
  567. .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
  568. .handler = compat_cntvct_read_handler,
  569. },
  570. {},
  571. };
  572. asmlinkage void __exception do_cp15instr(unsigned int esr, struct pt_regs *regs)
  573. {
  574. struct sys64_hook *hook, *hook_base;
  575. if (!cp15_cond_valid(esr, regs)) {
  576. /*
  577. * There is no T16 variant of a CP access, so we
  578. * always advance PC by 4 bytes.
  579. */
  580. arm64_compat_skip_faulting_instruction(regs, 4);
  581. return;
  582. }
  583. switch (ESR_ELx_EC(esr)) {
  584. case ESR_ELx_EC_CP15_32:
  585. hook_base = cp15_32_hooks;
  586. break;
  587. case ESR_ELx_EC_CP15_64:
  588. hook_base = cp15_64_hooks;
  589. break;
  590. default:
  591. do_undefinstr(regs);
  592. return;
  593. }
  594. for (hook = hook_base; hook->handler; hook++)
  595. if ((hook->esr_mask & esr) == hook->esr_val) {
  596. hook->handler(esr, regs);
  597. return;
  598. }
  599. /*
  600. * New cp15 instructions may previously have been undefined at
  601. * EL0. Fall back to our usual undefined instruction handler
  602. * so that we handle these consistently.
  603. */
  604. do_undefinstr(regs);
  605. }
  606. #endif
  607. asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
  608. {
  609. struct sys64_hook *hook;
  610. for (hook = sys64_hooks; hook->handler; hook++)
  611. if ((hook->esr_mask & esr) == hook->esr_val) {
  612. hook->handler(esr, regs);
  613. return;
  614. }
  615. /*
  616. * New SYS instructions may previously have been undefined at EL0. Fall
  617. * back to our usual undefined instruction handler so that we handle
  618. * these consistently.
  619. */
  620. do_undefinstr(regs);
  621. }
  622. static const char *esr_class_str[] = {
  623. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  624. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  625. [ESR_ELx_EC_WFx] = "WFI/WFE",
  626. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  627. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  628. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  629. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  630. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  631. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  632. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  633. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  634. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  635. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  636. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  637. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  638. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  639. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  640. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  641. [ESR_ELx_EC_SVE] = "SVE",
  642. [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
  643. [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
  644. [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
  645. [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
  646. [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
  647. [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
  648. [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
  649. [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
  650. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  651. [ESR_ELx_EC_SERROR] = "SError",
  652. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  653. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  654. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  655. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  656. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  657. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  658. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  659. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  660. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  661. };
  662. const char *esr_get_class_string(u32 esr)
  663. {
  664. return esr_class_str[ESR_ELx_EC(esr)];
  665. }
  666. /*
  667. * bad_mode handles the impossible case in the exception vector. This is always
  668. * fatal.
  669. */
  670. asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
  671. {
  672. console_verbose();
  673. pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
  674. handler[reason], smp_processor_id(), esr,
  675. esr_get_class_string(esr));
  676. local_daif_mask();
  677. panic("bad mode");
  678. }
  679. /*
  680. * bad_el0_sync handles unexpected, but potentially recoverable synchronous
  681. * exceptions taken from EL0. Unlike bad_mode, this returns.
  682. */
  683. asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
  684. {
  685. void __user *pc = (void __user *)instruction_pointer(regs);
  686. current->thread.fault_address = 0;
  687. current->thread.fault_code = esr;
  688. arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
  689. "Bad EL0 synchronous exception");
  690. }
  691. #ifdef CONFIG_VMAP_STACK
  692. DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
  693. __aligned(16);
  694. asmlinkage void handle_bad_stack(struct pt_regs *regs)
  695. {
  696. unsigned long tsk_stk = (unsigned long)current->stack;
  697. unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
  698. unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
  699. unsigned int esr = read_sysreg(esr_el1);
  700. unsigned long far = read_sysreg(far_el1);
  701. console_verbose();
  702. pr_emerg("Insufficient stack space to handle exception!");
  703. pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
  704. pr_emerg("FAR: 0x%016lx\n", far);
  705. pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
  706. tsk_stk, tsk_stk + THREAD_SIZE);
  707. pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
  708. irq_stk, irq_stk + THREAD_SIZE);
  709. pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
  710. ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
  711. __show_regs(regs);
  712. /*
  713. * We use nmi_panic to limit the potential for recusive overflows, and
  714. * to get a better stack trace.
  715. */
  716. nmi_panic(NULL, "kernel stack overflow");
  717. cpu_park_loop();
  718. }
  719. #endif
  720. void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
  721. {
  722. console_verbose();
  723. pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
  724. smp_processor_id(), esr, esr_get_class_string(esr));
  725. if (regs)
  726. __show_regs(regs);
  727. nmi_panic(regs, "Asynchronous SError Interrupt");
  728. cpu_park_loop();
  729. unreachable();
  730. }
  731. bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
  732. {
  733. u32 aet = arm64_ras_serror_get_severity(esr);
  734. switch (aet) {
  735. case ESR_ELx_AET_CE: /* corrected error */
  736. case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
  737. /*
  738. * The CPU can make progress. We may take UEO again as
  739. * a more severe error.
  740. */
  741. return false;
  742. case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
  743. case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
  744. /*
  745. * The CPU can't make progress. The exception may have
  746. * been imprecise.
  747. */
  748. return true;
  749. case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
  750. default:
  751. /* Error has been silently propagated */
  752. arm64_serror_panic(regs, esr);
  753. }
  754. }
  755. asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
  756. {
  757. nmi_enter();
  758. /* non-RAS errors are not containable */
  759. if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
  760. arm64_serror_panic(regs, esr);
  761. nmi_exit();
  762. }
  763. void __pte_error(const char *file, int line, unsigned long val)
  764. {
  765. pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
  766. }
  767. void __pmd_error(const char *file, int line, unsigned long val)
  768. {
  769. pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
  770. }
  771. void __pud_error(const char *file, int line, unsigned long val)
  772. {
  773. pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
  774. }
  775. void __pgd_error(const char *file, int line, unsigned long val)
  776. {
  777. pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
  778. }
  779. /* GENERIC_BUG traps */
  780. int is_valid_bugaddr(unsigned long addr)
  781. {
  782. /*
  783. * bug_handler() only called for BRK #BUG_BRK_IMM.
  784. * So the answer is trivial -- any spurious instances with no
  785. * bug table entry will be rejected by report_bug() and passed
  786. * back to the debug-monitors code and handled as a fatal
  787. * unexpected debug exception.
  788. */
  789. return 1;
  790. }
  791. static int bug_handler(struct pt_regs *regs, unsigned int esr)
  792. {
  793. if (user_mode(regs))
  794. return DBG_HOOK_ERROR;
  795. switch (report_bug(regs->pc, regs)) {
  796. case BUG_TRAP_TYPE_BUG:
  797. die("Oops - BUG", regs, 0);
  798. break;
  799. case BUG_TRAP_TYPE_WARN:
  800. break;
  801. default:
  802. /* unknown/unrecognised bug trap type */
  803. return DBG_HOOK_ERROR;
  804. }
  805. /* If thread survives, skip over the BUG instruction and continue: */
  806. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  807. return DBG_HOOK_HANDLED;
  808. }
  809. static struct break_hook bug_break_hook = {
  810. .esr_val = 0xf2000000 | BUG_BRK_IMM,
  811. .esr_mask = 0xffffffff,
  812. .fn = bug_handler,
  813. };
  814. /*
  815. * Initial handler for AArch64 BRK exceptions
  816. * This handler only used until debug_traps_init().
  817. */
  818. int __init early_brk64(unsigned long addr, unsigned int esr,
  819. struct pt_regs *regs)
  820. {
  821. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  822. }
  823. /* This registration must happen early, before debug_traps_init(). */
  824. void __init trap_init(void)
  825. {
  826. register_break_hook(&bug_break_hook);
  827. }