traps.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/kernel/traps.c
  4. *
  5. * Copyright (C) 1995-2009 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #include <linux/bug.h>
  9. #include <linux/context_tracking.h>
  10. #include <linux/signal.h>
  11. #include <linux/kallsyms.h>
  12. #include <linux/kprobes.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/hardirq.h>
  16. #include <linux/kdebug.h>
  17. #include <linux/module.h>
  18. #include <linux/kexec.h>
  19. #include <linux/delay.h>
  20. #include <linux/efi.h>
  21. #include <linux/init.h>
  22. #include <linux/sched/signal.h>
  23. #include <linux/sched/debug.h>
  24. #include <linux/sched/task_stack.h>
  25. #include <linux/sizes.h>
  26. #include <linux/syscalls.h>
  27. #include <linux/mm_types.h>
  28. #include <linux/kasan.h>
  29. #include <linux/ubsan.h>
  30. #include <linux/cfi.h>
  31. #include <asm/atomic.h>
  32. #include <asm/bug.h>
  33. #include <asm/cpufeature.h>
  34. #include <asm/daifflags.h>
  35. #include <asm/debug-monitors.h>
  36. #include <asm/efi.h>
  37. #include <asm/esr.h>
  38. #include <asm/exception.h>
  39. #include <asm/extable.h>
  40. #include <asm/insn.h>
  41. #include <asm/kprobes.h>
  42. #include <asm/patching.h>
  43. #include <asm/traps.h>
  44. #include <asm/smp.h>
  45. #include <asm/stack_pointer.h>
  46. #include <asm/stacktrace.h>
  47. #include <asm/system_misc.h>
  48. #include <asm/sysreg.h>
  49. static bool __kprobes __check_eq(unsigned long pstate)
  50. {
  51. return (pstate & PSR_Z_BIT) != 0;
  52. }
  53. static bool __kprobes __check_ne(unsigned long pstate)
  54. {
  55. return (pstate & PSR_Z_BIT) == 0;
  56. }
  57. static bool __kprobes __check_cs(unsigned long pstate)
  58. {
  59. return (pstate & PSR_C_BIT) != 0;
  60. }
  61. static bool __kprobes __check_cc(unsigned long pstate)
  62. {
  63. return (pstate & PSR_C_BIT) == 0;
  64. }
  65. static bool __kprobes __check_mi(unsigned long pstate)
  66. {
  67. return (pstate & PSR_N_BIT) != 0;
  68. }
  69. static bool __kprobes __check_pl(unsigned long pstate)
  70. {
  71. return (pstate & PSR_N_BIT) == 0;
  72. }
  73. static bool __kprobes __check_vs(unsigned long pstate)
  74. {
  75. return (pstate & PSR_V_BIT) != 0;
  76. }
  77. static bool __kprobes __check_vc(unsigned long pstate)
  78. {
  79. return (pstate & PSR_V_BIT) == 0;
  80. }
  81. static bool __kprobes __check_hi(unsigned long pstate)
  82. {
  83. pstate &= ~(pstate >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
  84. return (pstate & PSR_C_BIT) != 0;
  85. }
  86. static bool __kprobes __check_ls(unsigned long pstate)
  87. {
  88. pstate &= ~(pstate >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
  89. return (pstate & PSR_C_BIT) == 0;
  90. }
  91. static bool __kprobes __check_ge(unsigned long pstate)
  92. {
  93. pstate ^= (pstate << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  94. return (pstate & PSR_N_BIT) == 0;
  95. }
  96. static bool __kprobes __check_lt(unsigned long pstate)
  97. {
  98. pstate ^= (pstate << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  99. return (pstate & PSR_N_BIT) != 0;
  100. }
  101. static bool __kprobes __check_gt(unsigned long pstate)
  102. {
  103. /*PSR_N_BIT ^= PSR_V_BIT */
  104. unsigned long temp = pstate ^ (pstate << 3);
  105. temp |= (pstate << 1); /*PSR_N_BIT |= PSR_Z_BIT */
  106. return (temp & PSR_N_BIT) == 0;
  107. }
  108. static bool __kprobes __check_le(unsigned long pstate)
  109. {
  110. /*PSR_N_BIT ^= PSR_V_BIT */
  111. unsigned long temp = pstate ^ (pstate << 3);
  112. temp |= (pstate << 1); /*PSR_N_BIT |= PSR_Z_BIT */
  113. return (temp & PSR_N_BIT) != 0;
  114. }
  115. static bool __kprobes __check_al(unsigned long pstate)
  116. {
  117. return true;
  118. }
  119. /*
  120. * Note that the ARMv8 ARM calls condition code 0b1111 "nv", but states that
  121. * it behaves identically to 0b1110 ("al").
  122. */
  123. pstate_check_t * const aarch32_opcode_cond_checks[16] = {
  124. __check_eq, __check_ne, __check_cs, __check_cc,
  125. __check_mi, __check_pl, __check_vs, __check_vc,
  126. __check_hi, __check_ls, __check_ge, __check_lt,
  127. __check_gt, __check_le, __check_al, __check_al
  128. };
  129. int show_unhandled_signals = 0;
  130. static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
  131. {
  132. unsigned long addr = instruction_pointer(regs);
  133. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  134. int i;
  135. if (user_mode(regs))
  136. return;
  137. for (i = -4; i < 1; i++) {
  138. unsigned int val, bad;
  139. bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
  140. if (!bad)
  141. p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
  142. else
  143. p += sprintf(p, i == 0 ? "(????????) " : "???????? ");
  144. }
  145. printk("%sCode: %s\n", lvl, str);
  146. }
  147. #ifdef CONFIG_PREEMPT
  148. #define S_PREEMPT " PREEMPT"
  149. #elif defined(CONFIG_PREEMPT_RT)
  150. #define S_PREEMPT " PREEMPT_RT"
  151. #else
  152. #define S_PREEMPT ""
  153. #endif
  154. #define S_SMP " SMP"
  155. static int __die(const char *str, long err, struct pt_regs *regs)
  156. {
  157. static int die_counter;
  158. int ret;
  159. pr_emerg("Internal error: %s: %016lx [#%d]" S_PREEMPT S_SMP "\n",
  160. str, err, ++die_counter);
  161. /* trap and error numbers are mostly meaningless on ARM */
  162. ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
  163. if (ret == NOTIFY_STOP)
  164. return ret;
  165. print_modules();
  166. show_regs(regs);
  167. dump_kernel_instr(KERN_EMERG, regs);
  168. return ret;
  169. }
  170. static DEFINE_RAW_SPINLOCK(die_lock);
  171. /*
  172. * This function is protected against re-entrancy.
  173. */
  174. void die(const char *str, struct pt_regs *regs, long err)
  175. {
  176. int ret;
  177. unsigned long flags;
  178. raw_spin_lock_irqsave(&die_lock, flags);
  179. oops_enter();
  180. console_verbose();
  181. bust_spinlocks(1);
  182. ret = __die(str, err, regs);
  183. if (regs && kexec_should_crash(current))
  184. crash_kexec(regs);
  185. bust_spinlocks(0);
  186. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  187. oops_exit();
  188. if (in_interrupt())
  189. panic("%s: Fatal exception in interrupt", str);
  190. if (panic_on_oops)
  191. panic("%s: Fatal exception", str);
  192. raw_spin_unlock_irqrestore(&die_lock, flags);
  193. if (ret != NOTIFY_STOP)
  194. make_task_dead(SIGSEGV);
  195. }
  196. static void arm64_show_signal(int signo, const char *str)
  197. {
  198. static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
  199. DEFAULT_RATELIMIT_BURST);
  200. struct task_struct *tsk = current;
  201. unsigned long esr = tsk->thread.fault_code;
  202. struct pt_regs *regs = task_pt_regs(tsk);
  203. /* Leave if the signal won't be shown */
  204. if (!show_unhandled_signals ||
  205. !unhandled_signal(tsk, signo) ||
  206. !__ratelimit(&rs))
  207. return;
  208. pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
  209. if (esr)
  210. pr_cont("%s, ESR 0x%016lx, ", 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. }
  216. void arm64_force_sig_fault(int signo, int code, unsigned long far,
  217. const char *str)
  218. {
  219. arm64_show_signal(signo, str);
  220. if (signo == SIGKILL)
  221. force_sig(SIGKILL);
  222. else
  223. force_sig_fault(signo, code, (void __user *)far);
  224. }
  225. void arm64_force_sig_fault_pkey(unsigned long far, const char *str, int pkey)
  226. {
  227. arm64_show_signal(SIGSEGV, str);
  228. force_sig_pkuerr((void __user *)far, pkey);
  229. }
  230. void arm64_force_sig_mceerr(int code, unsigned long far, short lsb,
  231. const char *str)
  232. {
  233. arm64_show_signal(SIGBUS, str);
  234. force_sig_mceerr(code, (void __user *)far, lsb);
  235. }
  236. void arm64_force_sig_ptrace_errno_trap(int errno, unsigned long far,
  237. const char *str)
  238. {
  239. arm64_show_signal(SIGTRAP, str);
  240. force_sig_ptrace_errno_trap(errno, (void __user *)far);
  241. }
  242. void arm64_notify_die(const char *str, struct pt_regs *regs,
  243. int signo, int sicode, unsigned long far,
  244. unsigned long err)
  245. {
  246. if (user_mode(regs)) {
  247. WARN_ON(regs != current_pt_regs());
  248. current->thread.fault_address = 0;
  249. current->thread.fault_code = err;
  250. arm64_force_sig_fault(signo, sicode, far, str);
  251. } else {
  252. die(str, regs, err);
  253. }
  254. }
  255. #ifdef CONFIG_COMPAT
  256. #define PSTATE_IT_1_0_SHIFT 25
  257. #define PSTATE_IT_1_0_MASK (0x3 << PSTATE_IT_1_0_SHIFT)
  258. #define PSTATE_IT_7_2_SHIFT 10
  259. #define PSTATE_IT_7_2_MASK (0x3f << PSTATE_IT_7_2_SHIFT)
  260. static u32 compat_get_it_state(struct pt_regs *regs)
  261. {
  262. u32 it, pstate = regs->pstate;
  263. it = (pstate & PSTATE_IT_1_0_MASK) >> PSTATE_IT_1_0_SHIFT;
  264. it |= ((pstate & PSTATE_IT_7_2_MASK) >> PSTATE_IT_7_2_SHIFT) << 2;
  265. return it;
  266. }
  267. static void compat_set_it_state(struct pt_regs *regs, u32 it)
  268. {
  269. u32 pstate_it;
  270. pstate_it = (it << PSTATE_IT_1_0_SHIFT) & PSTATE_IT_1_0_MASK;
  271. pstate_it |= ((it >> 2) << PSTATE_IT_7_2_SHIFT) & PSTATE_IT_7_2_MASK;
  272. regs->pstate &= ~PSR_AA32_IT_MASK;
  273. regs->pstate |= pstate_it;
  274. }
  275. static void advance_itstate(struct pt_regs *regs)
  276. {
  277. u32 it;
  278. /* ARM mode */
  279. if (!(regs->pstate & PSR_AA32_T_BIT) ||
  280. !(regs->pstate & PSR_AA32_IT_MASK))
  281. return;
  282. it = compat_get_it_state(regs);
  283. /*
  284. * If this is the last instruction of the block, wipe the IT
  285. * state. Otherwise advance it.
  286. */
  287. if (!(it & 7))
  288. it = 0;
  289. else
  290. it = (it & 0xe0) | ((it << 1) & 0x1f);
  291. compat_set_it_state(regs, it);
  292. }
  293. #else
  294. static void advance_itstate(struct pt_regs *regs)
  295. {
  296. }
  297. #endif
  298. void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
  299. {
  300. regs->pc += size;
  301. /*
  302. * If we were single stepping, we want to get the step exception after
  303. * we return from the trap.
  304. */
  305. if (user_mode(regs))
  306. user_fastforward_single_step(current);
  307. if (compat_user_mode(regs))
  308. advance_itstate(regs);
  309. else
  310. regs->pstate &= ~PSR_BTYPE_MASK;
  311. }
  312. static int user_insn_read(struct pt_regs *regs, u32 *insnp)
  313. {
  314. u32 instr;
  315. unsigned long pc = instruction_pointer(regs);
  316. if (compat_thumb_mode(regs)) {
  317. /* 16-bit Thumb instruction */
  318. __le16 instr_le;
  319. if (get_user(instr_le, (__le16 __user *)pc))
  320. return -EFAULT;
  321. instr = le16_to_cpu(instr_le);
  322. if (aarch32_insn_is_wide(instr)) {
  323. u32 instr2;
  324. if (get_user(instr_le, (__le16 __user *)(pc + 2)))
  325. return -EFAULT;
  326. instr2 = le16_to_cpu(instr_le);
  327. instr = (instr << 16) | instr2;
  328. }
  329. } else {
  330. /* 32-bit ARM instruction */
  331. __le32 instr_le;
  332. if (get_user(instr_le, (__le32 __user *)pc))
  333. return -EFAULT;
  334. instr = le32_to_cpu(instr_le);
  335. }
  336. *insnp = instr;
  337. return 0;
  338. }
  339. void force_signal_inject(int signal, int code, unsigned long address, unsigned long err)
  340. {
  341. const char *desc;
  342. struct pt_regs *regs = current_pt_regs();
  343. if (WARN_ON(!user_mode(regs)))
  344. return;
  345. switch (signal) {
  346. case SIGILL:
  347. desc = "undefined instruction";
  348. break;
  349. case SIGSEGV:
  350. desc = "illegal memory access";
  351. break;
  352. default:
  353. desc = "unknown or unrecoverable error";
  354. break;
  355. }
  356. /* Force signals we don't understand to SIGKILL */
  357. if (WARN_ON(signal != SIGKILL &&
  358. siginfo_layout(signal, code) != SIL_FAULT)) {
  359. signal = SIGKILL;
  360. }
  361. arm64_notify_die(desc, regs, signal, code, address, err);
  362. }
  363. /*
  364. * Set up process info to signal segmentation fault - called on access error.
  365. */
  366. void arm64_notify_segfault(unsigned long addr)
  367. {
  368. int code;
  369. mmap_read_lock(current->mm);
  370. if (find_vma(current->mm, untagged_addr(addr)) == NULL)
  371. code = SEGV_MAPERR;
  372. else
  373. code = SEGV_ACCERR;
  374. mmap_read_unlock(current->mm);
  375. force_signal_inject(SIGSEGV, code, addr, 0);
  376. }
  377. void do_el0_undef(struct pt_regs *regs, unsigned long esr)
  378. {
  379. u32 insn;
  380. /* check for AArch32 breakpoint instructions */
  381. if (!aarch32_break_handler(regs))
  382. return;
  383. if (user_insn_read(regs, &insn))
  384. goto out_err;
  385. if (try_emulate_mrs(regs, insn))
  386. return;
  387. if (try_emulate_armv8_deprecated(regs, insn))
  388. return;
  389. out_err:
  390. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  391. }
  392. void do_el1_undef(struct pt_regs *regs, unsigned long esr)
  393. {
  394. u32 insn;
  395. if (aarch64_insn_read((void *)regs->pc, &insn))
  396. goto out_err;
  397. if (try_emulate_el1_ssbs(regs, insn))
  398. return;
  399. out_err:
  400. die("Oops - Undefined instruction", regs, esr);
  401. }
  402. void do_el0_bti(struct pt_regs *regs)
  403. {
  404. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  405. }
  406. void do_el1_bti(struct pt_regs *regs, unsigned long esr)
  407. {
  408. if (efi_runtime_fixup_exception(regs, "BTI violation")) {
  409. regs->pstate &= ~PSR_BTYPE_MASK;
  410. return;
  411. }
  412. die("Oops - BTI", regs, esr);
  413. }
  414. void do_el0_fpac(struct pt_regs *regs, unsigned long esr)
  415. {
  416. force_signal_inject(SIGILL, ILL_ILLOPN, regs->pc, esr);
  417. }
  418. void do_el1_fpac(struct pt_regs *regs, unsigned long esr)
  419. {
  420. /*
  421. * Unexpected FPAC exception in the kernel: kill the task before it
  422. * does any more harm.
  423. */
  424. die("Oops - FPAC", regs, esr);
  425. }
  426. void do_el0_mops(struct pt_regs *regs, unsigned long esr)
  427. {
  428. arm64_mops_reset_regs(&regs->user_regs, esr);
  429. /*
  430. * If single stepping then finish the step before executing the
  431. * prologue instruction.
  432. */
  433. user_fastforward_single_step(current);
  434. }
  435. #define __user_cache_maint(insn, address, res) \
  436. if (address >= TASK_SIZE_MAX) { \
  437. res = -EFAULT; \
  438. } else { \
  439. uaccess_ttbr0_enable(); \
  440. asm volatile ( \
  441. "1: " insn ", %1\n" \
  442. " mov %w0, #0\n" \
  443. "2:\n" \
  444. _ASM_EXTABLE_UACCESS_ERR(1b, 2b, %w0) \
  445. : "=r" (res) \
  446. : "r" (address)); \
  447. uaccess_ttbr0_disable(); \
  448. }
  449. static void user_cache_maint_handler(unsigned long esr, struct pt_regs *regs)
  450. {
  451. unsigned long tagged_address, address;
  452. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  453. int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
  454. int ret = 0;
  455. tagged_address = pt_regs_read_reg(regs, rt);
  456. address = untagged_addr(tagged_address);
  457. switch (crm) {
  458. case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
  459. __user_cache_maint("dc civac", address, ret);
  460. break;
  461. case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
  462. __user_cache_maint("dc civac", address, ret);
  463. break;
  464. case ESR_ELx_SYS64_ISS_CRM_DC_CVADP: /* DC CVADP */
  465. __user_cache_maint("sys 3, c7, c13, 1", address, ret);
  466. break;
  467. case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
  468. __user_cache_maint("sys 3, c7, c12, 1", address, ret);
  469. break;
  470. case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
  471. __user_cache_maint("dc civac", address, ret);
  472. break;
  473. case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
  474. __user_cache_maint("ic ivau", address, ret);
  475. break;
  476. default:
  477. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  478. return;
  479. }
  480. if (ret)
  481. arm64_notify_segfault(tagged_address);
  482. else
  483. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  484. }
  485. static void ctr_read_handler(unsigned long esr, struct pt_regs *regs)
  486. {
  487. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  488. unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
  489. if (cpus_have_final_cap(ARM64_WORKAROUND_1542419)) {
  490. /* Hide DIC so that we can trap the unnecessary maintenance...*/
  491. val &= ~BIT(CTR_EL0_DIC_SHIFT);
  492. /* ... and fake IminLine to reduce the number of traps. */
  493. val &= ~CTR_EL0_IminLine_MASK;
  494. val |= (PAGE_SHIFT - 2) & CTR_EL0_IminLine_MASK;
  495. }
  496. pt_regs_write_reg(regs, rt, val);
  497. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  498. }
  499. static void cntvct_read_handler(unsigned long esr, struct pt_regs *regs)
  500. {
  501. if (test_thread_flag(TIF_TSC_SIGSEGV)) {
  502. force_sig(SIGSEGV);
  503. } else {
  504. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  505. pt_regs_write_reg(regs, rt, arch_timer_read_counter());
  506. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  507. }
  508. }
  509. static void cntfrq_read_handler(unsigned long esr, struct pt_regs *regs)
  510. {
  511. if (test_thread_flag(TIF_TSC_SIGSEGV)) {
  512. force_sig(SIGSEGV);
  513. } else {
  514. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  515. pt_regs_write_reg(regs, rt, arch_timer_get_rate());
  516. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  517. }
  518. }
  519. static void mrs_handler(unsigned long esr, struct pt_regs *regs)
  520. {
  521. u32 sysreg, rt;
  522. rt = ESR_ELx_SYS64_ISS_RT(esr);
  523. sysreg = esr_sys64_to_sysreg(esr);
  524. if (do_emulate_mrs(regs, sysreg, rt) != 0)
  525. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  526. }
  527. static void wfi_handler(unsigned long esr, struct pt_regs *regs)
  528. {
  529. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  530. }
  531. struct sys64_hook {
  532. unsigned long esr_mask;
  533. unsigned long esr_val;
  534. void (*handler)(unsigned long esr, struct pt_regs *regs);
  535. };
  536. static const struct sys64_hook sys64_hooks[] = {
  537. {
  538. .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
  539. .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
  540. .handler = user_cache_maint_handler,
  541. },
  542. {
  543. /* Trap read access to CTR_EL0 */
  544. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  545. .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
  546. .handler = ctr_read_handler,
  547. },
  548. {
  549. /* Trap read access to CNTVCT_EL0 */
  550. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  551. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
  552. .handler = cntvct_read_handler,
  553. },
  554. {
  555. /* Trap read access to CNTVCTSS_EL0 */
  556. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  557. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCTSS,
  558. .handler = cntvct_read_handler,
  559. },
  560. {
  561. /* Trap read access to CNTFRQ_EL0 */
  562. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  563. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
  564. .handler = cntfrq_read_handler,
  565. },
  566. {
  567. /* Trap read access to CPUID registers */
  568. .esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
  569. .esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
  570. .handler = mrs_handler,
  571. },
  572. {
  573. /* Trap WFI instructions executed in userspace */
  574. .esr_mask = ESR_ELx_WFx_MASK,
  575. .esr_val = ESR_ELx_WFx_WFI_VAL,
  576. .handler = wfi_handler,
  577. },
  578. {},
  579. };
  580. #ifdef CONFIG_COMPAT
  581. static bool cp15_cond_valid(unsigned long esr, struct pt_regs *regs)
  582. {
  583. int cond;
  584. /* Only a T32 instruction can trap without CV being set */
  585. if (!(esr & ESR_ELx_CV)) {
  586. u32 it;
  587. it = compat_get_it_state(regs);
  588. if (!it)
  589. return true;
  590. cond = it >> 4;
  591. } else {
  592. cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
  593. }
  594. return aarch32_opcode_cond_checks[cond](regs->pstate);
  595. }
  596. static void compat_cntfrq_read_handler(unsigned long esr, struct pt_regs *regs)
  597. {
  598. int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
  599. pt_regs_write_reg(regs, reg, arch_timer_get_rate());
  600. arm64_skip_faulting_instruction(regs, 4);
  601. }
  602. static const struct sys64_hook cp15_32_hooks[] = {
  603. {
  604. .esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
  605. .esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
  606. .handler = compat_cntfrq_read_handler,
  607. },
  608. {},
  609. };
  610. static void compat_cntvct_read_handler(unsigned long esr, struct pt_regs *regs)
  611. {
  612. int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
  613. int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
  614. u64 val = arch_timer_read_counter();
  615. pt_regs_write_reg(regs, rt, lower_32_bits(val));
  616. pt_regs_write_reg(regs, rt2, upper_32_bits(val));
  617. arm64_skip_faulting_instruction(regs, 4);
  618. }
  619. static const struct sys64_hook cp15_64_hooks[] = {
  620. {
  621. .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
  622. .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
  623. .handler = compat_cntvct_read_handler,
  624. },
  625. {
  626. .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
  627. .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCTSS,
  628. .handler = compat_cntvct_read_handler,
  629. },
  630. {},
  631. };
  632. void do_el0_cp15(unsigned long esr, struct pt_regs *regs)
  633. {
  634. const struct sys64_hook *hook, *hook_base;
  635. if (!cp15_cond_valid(esr, regs)) {
  636. /*
  637. * There is no T16 variant of a CP access, so we
  638. * always advance PC by 4 bytes.
  639. */
  640. arm64_skip_faulting_instruction(regs, 4);
  641. return;
  642. }
  643. switch (ESR_ELx_EC(esr)) {
  644. case ESR_ELx_EC_CP15_32:
  645. hook_base = cp15_32_hooks;
  646. break;
  647. case ESR_ELx_EC_CP15_64:
  648. hook_base = cp15_64_hooks;
  649. break;
  650. default:
  651. do_el0_undef(regs, esr);
  652. return;
  653. }
  654. for (hook = hook_base; hook->handler; hook++)
  655. if ((hook->esr_mask & esr) == hook->esr_val) {
  656. hook->handler(esr, regs);
  657. return;
  658. }
  659. /*
  660. * New cp15 instructions may previously have been undefined at
  661. * EL0. Fall back to our usual undefined instruction handler
  662. * so that we handle these consistently.
  663. */
  664. do_el0_undef(regs, esr);
  665. }
  666. #endif
  667. void do_el0_sys(unsigned long esr, struct pt_regs *regs)
  668. {
  669. const struct sys64_hook *hook;
  670. for (hook = sys64_hooks; hook->handler; hook++)
  671. if ((hook->esr_mask & esr) == hook->esr_val) {
  672. hook->handler(esr, regs);
  673. return;
  674. }
  675. /*
  676. * New SYS instructions may previously have been undefined at EL0. Fall
  677. * back to our usual undefined instruction handler so that we handle
  678. * these consistently.
  679. */
  680. do_el0_undef(regs, esr);
  681. }
  682. static const char *esr_class_str[] = {
  683. [0 ... ESR_ELx_EC_MAX] = "UNRECOGNIZED EC",
  684. [ESR_ELx_EC_UNKNOWN] = "Unknown/Uncategorized",
  685. [ESR_ELx_EC_WFx] = "WFI/WFE",
  686. [ESR_ELx_EC_CP15_32] = "CP15 MCR/MRC",
  687. [ESR_ELx_EC_CP15_64] = "CP15 MCRR/MRRC",
  688. [ESR_ELx_EC_CP14_MR] = "CP14 MCR/MRC",
  689. [ESR_ELx_EC_CP14_LS] = "CP14 LDC/STC",
  690. [ESR_ELx_EC_FP_ASIMD] = "ASIMD",
  691. [ESR_ELx_EC_CP10_ID] = "CP10 MRC/VMRS",
  692. [ESR_ELx_EC_PAC] = "PAC",
  693. [ESR_ELx_EC_CP14_64] = "CP14 MCRR/MRRC",
  694. [ESR_ELx_EC_BTI] = "BTI",
  695. [ESR_ELx_EC_ILL] = "PSTATE.IL",
  696. [ESR_ELx_EC_SVC32] = "SVC (AArch32)",
  697. [ESR_ELx_EC_HVC32] = "HVC (AArch32)",
  698. [ESR_ELx_EC_SMC32] = "SMC (AArch32)",
  699. [ESR_ELx_EC_SVC64] = "SVC (AArch64)",
  700. [ESR_ELx_EC_HVC64] = "HVC (AArch64)",
  701. [ESR_ELx_EC_SMC64] = "SMC (AArch64)",
  702. [ESR_ELx_EC_SYS64] = "MSR/MRS (AArch64)",
  703. [ESR_ELx_EC_SVE] = "SVE",
  704. [ESR_ELx_EC_ERET] = "ERET/ERETAA/ERETAB",
  705. [ESR_ELx_EC_FPAC] = "FPAC",
  706. [ESR_ELx_EC_SME] = "SME",
  707. [ESR_ELx_EC_IMP_DEF] = "EL3 IMP DEF",
  708. [ESR_ELx_EC_IABT_LOW] = "IABT (lower EL)",
  709. [ESR_ELx_EC_IABT_CUR] = "IABT (current EL)",
  710. [ESR_ELx_EC_PC_ALIGN] = "PC Alignment",
  711. [ESR_ELx_EC_DABT_LOW] = "DABT (lower EL)",
  712. [ESR_ELx_EC_DABT_CUR] = "DABT (current EL)",
  713. [ESR_ELx_EC_SP_ALIGN] = "SP Alignment",
  714. [ESR_ELx_EC_MOPS] = "MOPS",
  715. [ESR_ELx_EC_FP_EXC32] = "FP (AArch32)",
  716. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  717. [ESR_ELx_EC_SERROR] = "SError",
  718. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  719. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  720. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  721. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  722. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  723. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  724. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  725. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  726. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  727. };
  728. const char *esr_get_class_string(unsigned long esr)
  729. {
  730. return esr_class_str[ESR_ELx_EC(esr)];
  731. }
  732. /*
  733. * bad_el0_sync handles unexpected, but potentially recoverable synchronous
  734. * exceptions taken from EL0.
  735. */
  736. void bad_el0_sync(struct pt_regs *regs, int reason, unsigned long esr)
  737. {
  738. unsigned long pc = instruction_pointer(regs);
  739. current->thread.fault_address = 0;
  740. current->thread.fault_code = esr;
  741. arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
  742. "Bad EL0 synchronous exception");
  743. }
  744. #ifdef CONFIG_VMAP_STACK
  745. DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
  746. __aligned(16);
  747. void __noreturn panic_bad_stack(struct pt_regs *regs, unsigned long esr, unsigned long far)
  748. {
  749. unsigned long tsk_stk = (unsigned long)current->stack;
  750. unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
  751. unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
  752. console_verbose();
  753. pr_emerg("Insufficient stack space to handle exception!");
  754. pr_emerg("ESR: 0x%016lx -- %s\n", esr, esr_get_class_string(esr));
  755. pr_emerg("FAR: 0x%016lx\n", far);
  756. pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
  757. tsk_stk, tsk_stk + THREAD_SIZE);
  758. pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
  759. irq_stk, irq_stk + IRQ_STACK_SIZE);
  760. pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
  761. ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
  762. __show_regs(regs);
  763. /*
  764. * We use nmi_panic to limit the potential for recusive overflows, and
  765. * to get a better stack trace.
  766. */
  767. nmi_panic(NULL, "kernel stack overflow");
  768. cpu_park_loop();
  769. }
  770. #endif
  771. void __noreturn arm64_serror_panic(struct pt_regs *regs, unsigned long esr)
  772. {
  773. add_taint(TAINT_MACHINE_CHECK, LOCKDEP_STILL_OK);
  774. console_verbose();
  775. pr_crit("SError Interrupt on CPU%d, code 0x%016lx -- %s\n",
  776. smp_processor_id(), esr, esr_get_class_string(esr));
  777. if (regs)
  778. __show_regs(regs);
  779. nmi_panic(regs, "Asynchronous SError Interrupt");
  780. cpu_park_loop();
  781. }
  782. bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned long esr)
  783. {
  784. unsigned long aet = arm64_ras_serror_get_severity(esr);
  785. switch (aet) {
  786. case ESR_ELx_AET_CE: /* corrected error */
  787. case ESR_ELx_AET_UEO: /* restartable, not yet consumed */
  788. /*
  789. * The CPU can make progress. We may take UEO again as
  790. * a more severe error.
  791. */
  792. return false;
  793. case ESR_ELx_AET_UEU: /* Uncorrected Unrecoverable */
  794. case ESR_ELx_AET_UER: /* Uncorrected Recoverable */
  795. /*
  796. * The CPU can't make progress. The exception may have
  797. * been imprecise.
  798. *
  799. * Neoverse-N1 #1349291 means a non-KVM SError reported as
  800. * Unrecoverable should be treated as Uncontainable. We
  801. * call arm64_serror_panic() in both cases.
  802. */
  803. return true;
  804. case ESR_ELx_AET_UC: /* Uncontainable or Uncategorized error */
  805. default:
  806. /* Error has been silently propagated */
  807. arm64_serror_panic(regs, esr);
  808. }
  809. }
  810. void do_serror(struct pt_regs *regs, unsigned long esr)
  811. {
  812. /* non-RAS errors are not containable */
  813. if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
  814. arm64_serror_panic(regs, esr);
  815. }
  816. /* GENERIC_BUG traps */
  817. #ifdef CONFIG_GENERIC_BUG
  818. int is_valid_bugaddr(unsigned long addr)
  819. {
  820. /*
  821. * bug_handler() only called for BRK #BUG_BRK_IMM.
  822. * So the answer is trivial -- any spurious instances with no
  823. * bug table entry will be rejected by report_bug() and passed
  824. * back to the debug-monitors code and handled as a fatal
  825. * unexpected debug exception.
  826. */
  827. return 1;
  828. }
  829. #endif
  830. static int bug_handler(struct pt_regs *regs, unsigned long esr)
  831. {
  832. switch (report_bug(regs->pc, regs)) {
  833. case BUG_TRAP_TYPE_BUG:
  834. die("Oops - BUG", regs, esr);
  835. break;
  836. case BUG_TRAP_TYPE_WARN:
  837. break;
  838. default:
  839. /* unknown/unrecognised bug trap type */
  840. return DBG_HOOK_ERROR;
  841. }
  842. /* If thread survives, skip over the BUG instruction and continue: */
  843. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  844. return DBG_HOOK_HANDLED;
  845. }
  846. static struct break_hook bug_break_hook = {
  847. .fn = bug_handler,
  848. .imm = BUG_BRK_IMM,
  849. };
  850. #ifdef CONFIG_CFI_CLANG
  851. static int cfi_handler(struct pt_regs *regs, unsigned long esr)
  852. {
  853. unsigned long target;
  854. u32 type;
  855. target = pt_regs_read_reg(regs, FIELD_GET(CFI_BRK_IMM_TARGET, esr));
  856. type = (u32)pt_regs_read_reg(regs, FIELD_GET(CFI_BRK_IMM_TYPE, esr));
  857. switch (report_cfi_failure(regs, regs->pc, &target, type)) {
  858. case BUG_TRAP_TYPE_BUG:
  859. die("Oops - CFI", regs, esr);
  860. break;
  861. case BUG_TRAP_TYPE_WARN:
  862. break;
  863. default:
  864. return DBG_HOOK_ERROR;
  865. }
  866. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  867. return DBG_HOOK_HANDLED;
  868. }
  869. static struct break_hook cfi_break_hook = {
  870. .fn = cfi_handler,
  871. .imm = CFI_BRK_IMM_BASE,
  872. .mask = CFI_BRK_IMM_MASK,
  873. };
  874. #endif /* CONFIG_CFI_CLANG */
  875. static int reserved_fault_handler(struct pt_regs *regs, unsigned long esr)
  876. {
  877. pr_err("%s generated an invalid instruction at %pS!\n",
  878. "Kernel text patching",
  879. (void *)instruction_pointer(regs));
  880. /* We cannot handle this */
  881. return DBG_HOOK_ERROR;
  882. }
  883. static struct break_hook fault_break_hook = {
  884. .fn = reserved_fault_handler,
  885. .imm = FAULT_BRK_IMM,
  886. };
  887. #ifdef CONFIG_KASAN_SW_TAGS
  888. #define KASAN_ESR_RECOVER 0x20
  889. #define KASAN_ESR_WRITE 0x10
  890. #define KASAN_ESR_SIZE_MASK 0x0f
  891. #define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK))
  892. static int kasan_handler(struct pt_regs *regs, unsigned long esr)
  893. {
  894. bool recover = esr & KASAN_ESR_RECOVER;
  895. bool write = esr & KASAN_ESR_WRITE;
  896. size_t size = KASAN_ESR_SIZE(esr);
  897. void *addr = (void *)regs->regs[0];
  898. u64 pc = regs->pc;
  899. kasan_report(addr, size, write, pc);
  900. /*
  901. * The instrumentation allows to control whether we can proceed after
  902. * a crash was detected. This is done by passing the -recover flag to
  903. * the compiler. Disabling recovery allows to generate more compact
  904. * code.
  905. *
  906. * Unfortunately disabling recovery doesn't work for the kernel right
  907. * now. KASAN reporting is disabled in some contexts (for example when
  908. * the allocator accesses slab object metadata; this is controlled by
  909. * current->kasan_depth). All these accesses are detected by the tool,
  910. * even though the reports for them are not printed.
  911. *
  912. * This is something that might be fixed at some point in the future.
  913. */
  914. if (!recover)
  915. die("Oops - KASAN", regs, esr);
  916. /* If thread survives, skip over the brk instruction and continue: */
  917. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  918. return DBG_HOOK_HANDLED;
  919. }
  920. static struct break_hook kasan_break_hook = {
  921. .fn = kasan_handler,
  922. .imm = KASAN_BRK_IMM,
  923. .mask = KASAN_BRK_MASK,
  924. };
  925. #endif
  926. #ifdef CONFIG_UBSAN_TRAP
  927. static int ubsan_handler(struct pt_regs *regs, unsigned long esr)
  928. {
  929. die(report_ubsan_failure(regs, esr & UBSAN_BRK_MASK), regs, esr);
  930. return DBG_HOOK_HANDLED;
  931. }
  932. static struct break_hook ubsan_break_hook = {
  933. .fn = ubsan_handler,
  934. .imm = UBSAN_BRK_IMM,
  935. .mask = UBSAN_BRK_MASK,
  936. };
  937. #endif
  938. /*
  939. * Initial handler for AArch64 BRK exceptions
  940. * This handler only used until debug_traps_init().
  941. */
  942. int __init early_brk64(unsigned long addr, unsigned long esr,
  943. struct pt_regs *regs)
  944. {
  945. #ifdef CONFIG_CFI_CLANG
  946. if (esr_is_cfi_brk(esr))
  947. return cfi_handler(regs, esr) != DBG_HOOK_HANDLED;
  948. #endif
  949. #ifdef CONFIG_KASAN_SW_TAGS
  950. if ((esr_brk_comment(esr) & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
  951. return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
  952. #endif
  953. #ifdef CONFIG_UBSAN_TRAP
  954. if ((esr_brk_comment(esr) & ~UBSAN_BRK_MASK) == UBSAN_BRK_IMM)
  955. return ubsan_handler(regs, esr) != DBG_HOOK_HANDLED;
  956. #endif
  957. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  958. }
  959. void __init trap_init(void)
  960. {
  961. register_kernel_break_hook(&bug_break_hook);
  962. #ifdef CONFIG_CFI_CLANG
  963. register_kernel_break_hook(&cfi_break_hook);
  964. #endif
  965. register_kernel_break_hook(&fault_break_hook);
  966. #ifdef CONFIG_KASAN_SW_TAGS
  967. register_kernel_break_hook(&kasan_break_hook);
  968. #endif
  969. #ifdef CONFIG_UBSAN_TRAP
  970. register_kernel_break_hook(&ubsan_break_hook);
  971. #endif
  972. debug_traps_init();
  973. }