traps.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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/init.h>
  21. #include <linux/sched/signal.h>
  22. #include <linux/sched/debug.h>
  23. #include <linux/sched/task_stack.h>
  24. #include <linux/sizes.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/mm_types.h>
  27. #include <linux/kasan.h>
  28. #include <linux/cfi.h>
  29. #include <asm/atomic.h>
  30. #include <asm/bug.h>
  31. #include <asm/cpufeature.h>
  32. #include <asm/daifflags.h>
  33. #include <asm/debug-monitors.h>
  34. #include <asm/esr.h>
  35. #include <asm/exception.h>
  36. #include <asm/extable.h>
  37. #include <asm/insn.h>
  38. #include <asm/kprobes.h>
  39. #include <asm/patching.h>
  40. #include <asm/traps.h>
  41. #include <asm/smp.h>
  42. #include <asm/stack_pointer.h>
  43. #include <asm/stacktrace.h>
  44. #include <asm/system_misc.h>
  45. #include <asm/sysreg.h>
  46. #include <trace/hooks/traps.h>
  47. static bool __kprobes __check_eq(unsigned long pstate)
  48. {
  49. return (pstate & PSR_Z_BIT) != 0;
  50. }
  51. static bool __kprobes __check_ne(unsigned long pstate)
  52. {
  53. return (pstate & PSR_Z_BIT) == 0;
  54. }
  55. static bool __kprobes __check_cs(unsigned long pstate)
  56. {
  57. return (pstate & PSR_C_BIT) != 0;
  58. }
  59. static bool __kprobes __check_cc(unsigned long pstate)
  60. {
  61. return (pstate & PSR_C_BIT) == 0;
  62. }
  63. static bool __kprobes __check_mi(unsigned long pstate)
  64. {
  65. return (pstate & PSR_N_BIT) != 0;
  66. }
  67. static bool __kprobes __check_pl(unsigned long pstate)
  68. {
  69. return (pstate & PSR_N_BIT) == 0;
  70. }
  71. static bool __kprobes __check_vs(unsigned long pstate)
  72. {
  73. return (pstate & PSR_V_BIT) != 0;
  74. }
  75. static bool __kprobes __check_vc(unsigned long pstate)
  76. {
  77. return (pstate & PSR_V_BIT) == 0;
  78. }
  79. static bool __kprobes __check_hi(unsigned long pstate)
  80. {
  81. pstate &= ~(pstate >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
  82. return (pstate & PSR_C_BIT) != 0;
  83. }
  84. static bool __kprobes __check_ls(unsigned long pstate)
  85. {
  86. pstate &= ~(pstate >> 1); /* PSR_C_BIT &= ~PSR_Z_BIT */
  87. return (pstate & PSR_C_BIT) == 0;
  88. }
  89. static bool __kprobes __check_ge(unsigned long pstate)
  90. {
  91. pstate ^= (pstate << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  92. return (pstate & PSR_N_BIT) == 0;
  93. }
  94. static bool __kprobes __check_lt(unsigned long pstate)
  95. {
  96. pstate ^= (pstate << 3); /* PSR_N_BIT ^= PSR_V_BIT */
  97. return (pstate & PSR_N_BIT) != 0;
  98. }
  99. static bool __kprobes __check_gt(unsigned long pstate)
  100. {
  101. /*PSR_N_BIT ^= PSR_V_BIT */
  102. unsigned long temp = pstate ^ (pstate << 3);
  103. temp |= (pstate << 1); /*PSR_N_BIT |= PSR_Z_BIT */
  104. return (temp & PSR_N_BIT) == 0;
  105. }
  106. static bool __kprobes __check_le(unsigned long pstate)
  107. {
  108. /*PSR_N_BIT ^= PSR_V_BIT */
  109. unsigned long temp = pstate ^ (pstate << 3);
  110. temp |= (pstate << 1); /*PSR_N_BIT |= PSR_Z_BIT */
  111. return (temp & PSR_N_BIT) != 0;
  112. }
  113. static bool __kprobes __check_al(unsigned long pstate)
  114. {
  115. return true;
  116. }
  117. /*
  118. * Note that the ARMv8 ARM calls condition code 0b1111 "nv", but states that
  119. * it behaves identically to 0b1110 ("al").
  120. */
  121. pstate_check_t * const aarch32_opcode_cond_checks[16] = {
  122. __check_eq, __check_ne, __check_cs, __check_cc,
  123. __check_mi, __check_pl, __check_vs, __check_vc,
  124. __check_hi, __check_ls, __check_ge, __check_lt,
  125. __check_gt, __check_le, __check_al, __check_al
  126. };
  127. int show_unhandled_signals = 0;
  128. static void dump_kernel_instr(const char *lvl, struct pt_regs *regs)
  129. {
  130. unsigned long addr = instruction_pointer(regs);
  131. char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
  132. int i;
  133. if (user_mode(regs))
  134. return;
  135. for (i = -4; i < 1; i++) {
  136. unsigned int val, bad;
  137. bad = aarch64_insn_read(&((u32 *)addr)[i], &val);
  138. if (!bad)
  139. p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
  140. else {
  141. p += sprintf(p, "bad PC value");
  142. break;
  143. }
  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_mceerr(int code, unsigned long far, short lsb,
  226. const char *str)
  227. {
  228. arm64_show_signal(SIGBUS, str);
  229. force_sig_mceerr(code, (void __user *)far, lsb);
  230. }
  231. void arm64_force_sig_ptrace_errno_trap(int errno, unsigned long far,
  232. const char *str)
  233. {
  234. arm64_show_signal(SIGTRAP, str);
  235. force_sig_ptrace_errno_trap(errno, (void __user *)far);
  236. }
  237. void arm64_notify_die(const char *str, struct pt_regs *regs,
  238. int signo, int sicode, unsigned long far,
  239. unsigned long err)
  240. {
  241. if (user_mode(regs)) {
  242. WARN_ON(regs != current_pt_regs());
  243. current->thread.fault_address = 0;
  244. current->thread.fault_code = err;
  245. arm64_force_sig_fault(signo, sicode, far, str);
  246. } else {
  247. /* FIXME: propagate fault address handlers using orig_x0 */
  248. regs->orig_x0 = far;
  249. die(str, regs, err);
  250. }
  251. }
  252. #ifdef CONFIG_COMPAT
  253. #define PSTATE_IT_1_0_SHIFT 25
  254. #define PSTATE_IT_1_0_MASK (0x3 << PSTATE_IT_1_0_SHIFT)
  255. #define PSTATE_IT_7_2_SHIFT 10
  256. #define PSTATE_IT_7_2_MASK (0x3f << PSTATE_IT_7_2_SHIFT)
  257. static u32 compat_get_it_state(struct pt_regs *regs)
  258. {
  259. u32 it, pstate = regs->pstate;
  260. it = (pstate & PSTATE_IT_1_0_MASK) >> PSTATE_IT_1_0_SHIFT;
  261. it |= ((pstate & PSTATE_IT_7_2_MASK) >> PSTATE_IT_7_2_SHIFT) << 2;
  262. return it;
  263. }
  264. static void compat_set_it_state(struct pt_regs *regs, u32 it)
  265. {
  266. u32 pstate_it;
  267. pstate_it = (it << PSTATE_IT_1_0_SHIFT) & PSTATE_IT_1_0_MASK;
  268. pstate_it |= ((it >> 2) << PSTATE_IT_7_2_SHIFT) & PSTATE_IT_7_2_MASK;
  269. regs->pstate &= ~PSR_AA32_IT_MASK;
  270. regs->pstate |= pstate_it;
  271. }
  272. static void advance_itstate(struct pt_regs *regs)
  273. {
  274. u32 it;
  275. /* ARM mode */
  276. if (!(regs->pstate & PSR_AA32_T_BIT) ||
  277. !(regs->pstate & PSR_AA32_IT_MASK))
  278. return;
  279. it = compat_get_it_state(regs);
  280. /*
  281. * If this is the last instruction of the block, wipe the IT
  282. * state. Otherwise advance it.
  283. */
  284. if (!(it & 7))
  285. it = 0;
  286. else
  287. it = (it & 0xe0) | ((it << 1) & 0x1f);
  288. compat_set_it_state(regs, it);
  289. }
  290. #else
  291. static void advance_itstate(struct pt_regs *regs)
  292. {
  293. }
  294. #endif
  295. void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
  296. {
  297. regs->pc += size;
  298. /*
  299. * If we were single stepping, we want to get the step exception after
  300. * we return from the trap.
  301. */
  302. if (user_mode(regs))
  303. user_fastforward_single_step(current);
  304. if (compat_user_mode(regs))
  305. advance_itstate(regs);
  306. else
  307. regs->pstate &= ~PSR_BTYPE_MASK;
  308. }
  309. static LIST_HEAD(undef_hook);
  310. static DEFINE_RAW_SPINLOCK(undef_lock);
  311. void register_undef_hook(struct undef_hook *hook)
  312. {
  313. unsigned long flags;
  314. raw_spin_lock_irqsave(&undef_lock, flags);
  315. list_add(&hook->node, &undef_hook);
  316. raw_spin_unlock_irqrestore(&undef_lock, flags);
  317. }
  318. void unregister_undef_hook(struct undef_hook *hook)
  319. {
  320. unsigned long flags;
  321. raw_spin_lock_irqsave(&undef_lock, flags);
  322. list_del(&hook->node);
  323. raw_spin_unlock_irqrestore(&undef_lock, flags);
  324. }
  325. static int call_undef_hook(struct pt_regs *regs)
  326. {
  327. struct undef_hook *hook;
  328. unsigned long flags;
  329. u32 instr;
  330. int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
  331. unsigned long pc = instruction_pointer(regs);
  332. if (!user_mode(regs)) {
  333. __le32 instr_le;
  334. if (get_kernel_nofault(instr_le, (__le32 *)pc))
  335. goto exit;
  336. instr = le32_to_cpu(instr_le);
  337. } else if (compat_thumb_mode(regs)) {
  338. /* 16-bit Thumb instruction */
  339. __le16 instr_le;
  340. if (get_user(instr_le, (__le16 __user *)pc))
  341. goto exit;
  342. instr = le16_to_cpu(instr_le);
  343. if (aarch32_insn_is_wide(instr)) {
  344. u32 instr2;
  345. if (get_user(instr_le, (__le16 __user *)(pc + 2)))
  346. goto exit;
  347. instr2 = le16_to_cpu(instr_le);
  348. instr = (instr << 16) | instr2;
  349. }
  350. } else {
  351. /* 32-bit ARM instruction */
  352. __le32 instr_le;
  353. if (get_user(instr_le, (__le32 __user *)pc))
  354. goto exit;
  355. instr = le32_to_cpu(instr_le);
  356. }
  357. raw_spin_lock_irqsave(&undef_lock, flags);
  358. list_for_each_entry(hook, &undef_hook, node)
  359. if ((instr & hook->instr_mask) == hook->instr_val &&
  360. (regs->pstate & hook->pstate_mask) == hook->pstate_val)
  361. fn = hook->fn;
  362. raw_spin_unlock_irqrestore(&undef_lock, flags);
  363. exit:
  364. return fn ? fn(regs, instr) : 1;
  365. }
  366. void force_signal_inject(int signal, int code, unsigned long address, unsigned long err)
  367. {
  368. const char *desc;
  369. struct pt_regs *regs = current_pt_regs();
  370. if (WARN_ON(!user_mode(regs)))
  371. return;
  372. switch (signal) {
  373. case SIGILL:
  374. desc = "undefined instruction";
  375. break;
  376. case SIGSEGV:
  377. desc = "illegal memory access";
  378. break;
  379. default:
  380. desc = "unknown or unrecoverable error";
  381. break;
  382. }
  383. /* Force signals we don't understand to SIGKILL */
  384. if (WARN_ON(signal != SIGKILL &&
  385. siginfo_layout(signal, code) != SIL_FAULT)) {
  386. signal = SIGKILL;
  387. }
  388. arm64_notify_die(desc, regs, signal, code, address, err);
  389. }
  390. /*
  391. * Set up process info to signal segmentation fault - called on access error.
  392. */
  393. void arm64_notify_segfault(unsigned long addr)
  394. {
  395. int code;
  396. mmap_read_lock(current->mm);
  397. if (find_vma(current->mm, untagged_addr(addr)) == NULL)
  398. code = SEGV_MAPERR;
  399. else
  400. code = SEGV_ACCERR;
  401. mmap_read_unlock(current->mm);
  402. force_signal_inject(SIGSEGV, code, addr, 0);
  403. }
  404. void do_undefinstr(struct pt_regs *regs, unsigned long esr)
  405. {
  406. /* check for AArch32 breakpoint instructions */
  407. if (!aarch32_break_handler(regs))
  408. return;
  409. if (call_undef_hook(regs) == 0)
  410. return;
  411. trace_android_rvh_do_undefinstr(regs, esr);
  412. if (!user_mode(regs))
  413. die("Oops - Undefined instruction", regs, esr);
  414. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  415. }
  416. NOKPROBE_SYMBOL(do_undefinstr);
  417. void do_el0_bti(struct pt_regs *regs)
  418. {
  419. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  420. }
  421. void do_el1_bti(struct pt_regs *regs, unsigned long esr)
  422. {
  423. trace_android_rvh_do_el1_bti(regs, esr);
  424. die("Oops - BTI", regs, esr);
  425. }
  426. NOKPROBE_SYMBOL(do_el1_bti);
  427. void do_el0_fpac(struct pt_regs *regs, unsigned long esr)
  428. {
  429. force_signal_inject(SIGILL, ILL_ILLOPN, regs->pc, esr);
  430. }
  431. void do_el1_fpac(struct pt_regs *regs, unsigned long esr)
  432. {
  433. /*
  434. * Unexpected FPAC exception in the kernel: kill the task before it
  435. * does any more harm.
  436. */
  437. trace_android_rvh_do_el1_fpac(regs, esr);
  438. die("Oops - FPAC", regs, esr);
  439. }
  440. NOKPROBE_SYMBOL(do_el1_fpac)
  441. #define __user_cache_maint(insn, address, res) \
  442. if (address >= TASK_SIZE_MAX) { \
  443. res = -EFAULT; \
  444. } else { \
  445. uaccess_ttbr0_enable(); \
  446. asm volatile ( \
  447. "1: " insn ", %1\n" \
  448. " mov %w0, #0\n" \
  449. "2:\n" \
  450. _ASM_EXTABLE_UACCESS_ERR(1b, 2b, %w0) \
  451. : "=r" (res) \
  452. : "r" (address)); \
  453. uaccess_ttbr0_disable(); \
  454. }
  455. static void user_cache_maint_handler(unsigned long esr, struct pt_regs *regs)
  456. {
  457. unsigned long tagged_address, address;
  458. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  459. int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
  460. int ret = 0;
  461. tagged_address = pt_regs_read_reg(regs, rt);
  462. address = untagged_addr(tagged_address);
  463. switch (crm) {
  464. case ESR_ELx_SYS64_ISS_CRM_DC_CVAU: /* DC CVAU, gets promoted */
  465. __user_cache_maint("dc civac", address, ret);
  466. break;
  467. case ESR_ELx_SYS64_ISS_CRM_DC_CVAC: /* DC CVAC, gets promoted */
  468. __user_cache_maint("dc civac", address, ret);
  469. break;
  470. case ESR_ELx_SYS64_ISS_CRM_DC_CVADP: /* DC CVADP */
  471. __user_cache_maint("sys 3, c7, c13, 1", address, ret);
  472. break;
  473. case ESR_ELx_SYS64_ISS_CRM_DC_CVAP: /* DC CVAP */
  474. __user_cache_maint("sys 3, c7, c12, 1", address, ret);
  475. break;
  476. case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC: /* DC CIVAC */
  477. __user_cache_maint("dc civac", address, ret);
  478. break;
  479. case ESR_ELx_SYS64_ISS_CRM_IC_IVAU: /* IC IVAU */
  480. __user_cache_maint("ic ivau", address, ret);
  481. break;
  482. default:
  483. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  484. return;
  485. }
  486. if (ret)
  487. arm64_notify_segfault(tagged_address);
  488. else
  489. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  490. }
  491. static void ctr_read_handler(unsigned long esr, struct pt_regs *regs)
  492. {
  493. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  494. unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
  495. if (cpus_have_const_cap(ARM64_WORKAROUND_1542419)) {
  496. /* Hide DIC so that we can trap the unnecessary maintenance...*/
  497. val &= ~BIT(CTR_EL0_DIC_SHIFT);
  498. /* ... and fake IminLine to reduce the number of traps. */
  499. val &= ~CTR_EL0_IminLine_MASK;
  500. val |= (PAGE_SHIFT - 2) & CTR_EL0_IminLine_MASK;
  501. }
  502. pt_regs_write_reg(regs, rt, val);
  503. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  504. }
  505. static void cntvct_read_handler(unsigned long esr, struct pt_regs *regs)
  506. {
  507. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  508. pt_regs_write_reg(regs, rt, arch_timer_read_counter());
  509. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  510. }
  511. static void cntfrq_read_handler(unsigned long esr, struct pt_regs *regs)
  512. {
  513. int rt = ESR_ELx_SYS64_ISS_RT(esr);
  514. pt_regs_write_reg(regs, rt, arch_timer_get_rate());
  515. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  516. }
  517. static void mrs_handler(unsigned long esr, struct pt_regs *regs)
  518. {
  519. u32 sysreg, rt;
  520. rt = ESR_ELx_SYS64_ISS_RT(esr);
  521. sysreg = esr_sys64_to_sysreg(esr);
  522. if (do_emulate_mrs(regs, sysreg, rt) != 0)
  523. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  524. }
  525. static void wfi_handler(unsigned long esr, struct pt_regs *regs)
  526. {
  527. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  528. }
  529. struct sys64_hook {
  530. unsigned long esr_mask;
  531. unsigned long esr_val;
  532. void (*handler)(unsigned long esr, struct pt_regs *regs);
  533. };
  534. static const struct sys64_hook sys64_hooks[] = {
  535. {
  536. .esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
  537. .esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
  538. .handler = user_cache_maint_handler,
  539. },
  540. {
  541. /* Trap read access to CTR_EL0 */
  542. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  543. .esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
  544. .handler = ctr_read_handler,
  545. },
  546. {
  547. /* Trap read access to CNTVCT_EL0 */
  548. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  549. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
  550. .handler = cntvct_read_handler,
  551. },
  552. {
  553. /* Trap read access to CNTVCTSS_EL0 */
  554. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  555. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCTSS,
  556. .handler = cntvct_read_handler,
  557. },
  558. {
  559. /* Trap read access to CNTFRQ_EL0 */
  560. .esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
  561. .esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
  562. .handler = cntfrq_read_handler,
  563. },
  564. {
  565. /* Trap read access to CPUID registers */
  566. .esr_mask = ESR_ELx_SYS64_ISS_SYS_MRS_OP_MASK,
  567. .esr_val = ESR_ELx_SYS64_ISS_SYS_MRS_OP_VAL,
  568. .handler = mrs_handler,
  569. },
  570. {
  571. /* Trap WFI instructions executed in userspace */
  572. .esr_mask = ESR_ELx_WFx_MASK,
  573. .esr_val = ESR_ELx_WFx_WFI_VAL,
  574. .handler = wfi_handler,
  575. },
  576. {},
  577. };
  578. #ifdef CONFIG_COMPAT
  579. static bool cp15_cond_valid(unsigned long esr, struct pt_regs *regs)
  580. {
  581. int cond;
  582. /* Only a T32 instruction can trap without CV being set */
  583. if (!(esr & ESR_ELx_CV)) {
  584. u32 it;
  585. it = compat_get_it_state(regs);
  586. if (!it)
  587. return true;
  588. cond = it >> 4;
  589. } else {
  590. cond = (esr & ESR_ELx_COND_MASK) >> ESR_ELx_COND_SHIFT;
  591. }
  592. return aarch32_opcode_cond_checks[cond](regs->pstate);
  593. }
  594. static void compat_cntfrq_read_handler(unsigned long esr, struct pt_regs *regs)
  595. {
  596. int reg = (esr & ESR_ELx_CP15_32_ISS_RT_MASK) >> ESR_ELx_CP15_32_ISS_RT_SHIFT;
  597. pt_regs_write_reg(regs, reg, arch_timer_get_rate());
  598. arm64_skip_faulting_instruction(regs, 4);
  599. }
  600. static const struct sys64_hook cp15_32_hooks[] = {
  601. {
  602. .esr_mask = ESR_ELx_CP15_32_ISS_SYS_MASK,
  603. .esr_val = ESR_ELx_CP15_32_ISS_SYS_CNTFRQ,
  604. .handler = compat_cntfrq_read_handler,
  605. },
  606. {},
  607. };
  608. static void compat_cntvct_read_handler(unsigned long esr, struct pt_regs *regs)
  609. {
  610. int rt = (esr & ESR_ELx_CP15_64_ISS_RT_MASK) >> ESR_ELx_CP15_64_ISS_RT_SHIFT;
  611. int rt2 = (esr & ESR_ELx_CP15_64_ISS_RT2_MASK) >> ESR_ELx_CP15_64_ISS_RT2_SHIFT;
  612. u64 val = arch_timer_read_counter();
  613. pt_regs_write_reg(regs, rt, lower_32_bits(val));
  614. pt_regs_write_reg(regs, rt2, upper_32_bits(val));
  615. arm64_skip_faulting_instruction(regs, 4);
  616. }
  617. static const struct sys64_hook cp15_64_hooks[] = {
  618. {
  619. .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
  620. .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCT,
  621. .handler = compat_cntvct_read_handler,
  622. },
  623. {
  624. .esr_mask = ESR_ELx_CP15_64_ISS_SYS_MASK,
  625. .esr_val = ESR_ELx_CP15_64_ISS_SYS_CNTVCTSS,
  626. .handler = compat_cntvct_read_handler,
  627. },
  628. {},
  629. };
  630. void do_cp15instr(unsigned long esr, struct pt_regs *regs)
  631. {
  632. const struct sys64_hook *hook, *hook_base;
  633. if (!cp15_cond_valid(esr, regs)) {
  634. /*
  635. * There is no T16 variant of a CP access, so we
  636. * always advance PC by 4 bytes.
  637. */
  638. arm64_skip_faulting_instruction(regs, 4);
  639. return;
  640. }
  641. switch (ESR_ELx_EC(esr)) {
  642. case ESR_ELx_EC_CP15_32:
  643. hook_base = cp15_32_hooks;
  644. break;
  645. case ESR_ELx_EC_CP15_64:
  646. hook_base = cp15_64_hooks;
  647. break;
  648. default:
  649. do_undefinstr(regs, esr);
  650. return;
  651. }
  652. for (hook = hook_base; hook->handler; hook++)
  653. if ((hook->esr_mask & esr) == hook->esr_val) {
  654. hook->handler(esr, regs);
  655. return;
  656. }
  657. /*
  658. * New cp15 instructions may previously have been undefined at
  659. * EL0. Fall back to our usual undefined instruction handler
  660. * so that we handle these consistently.
  661. */
  662. do_undefinstr(regs, esr);
  663. }
  664. NOKPROBE_SYMBOL(do_cp15instr);
  665. #endif
  666. void do_sysinstr(unsigned long esr, struct pt_regs *regs)
  667. {
  668. const struct sys64_hook *hook;
  669. for (hook = sys64_hooks; hook->handler; hook++)
  670. if ((hook->esr_mask & esr) == hook->esr_val) {
  671. hook->handler(esr, regs);
  672. return;
  673. }
  674. /*
  675. * New SYS instructions may previously have been undefined at EL0. Fall
  676. * back to our usual undefined instruction handler so that we handle
  677. * these consistently.
  678. */
  679. do_undefinstr(regs, esr);
  680. }
  681. NOKPROBE_SYMBOL(do_sysinstr);
  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_FP_EXC32] = "FP (AArch32)",
  715. [ESR_ELx_EC_FP_EXC64] = "FP (AArch64)",
  716. [ESR_ELx_EC_SERROR] = "SError",
  717. [ESR_ELx_EC_BREAKPT_LOW] = "Breakpoint (lower EL)",
  718. [ESR_ELx_EC_BREAKPT_CUR] = "Breakpoint (current EL)",
  719. [ESR_ELx_EC_SOFTSTP_LOW] = "Software Step (lower EL)",
  720. [ESR_ELx_EC_SOFTSTP_CUR] = "Software Step (current EL)",
  721. [ESR_ELx_EC_WATCHPT_LOW] = "Watchpoint (lower EL)",
  722. [ESR_ELx_EC_WATCHPT_CUR] = "Watchpoint (current EL)",
  723. [ESR_ELx_EC_BKPT32] = "BKPT (AArch32)",
  724. [ESR_ELx_EC_VECTOR32] = "Vector catch (AArch32)",
  725. [ESR_ELx_EC_BRK64] = "BRK (AArch64)",
  726. };
  727. const char *esr_get_class_string(unsigned long esr)
  728. {
  729. return esr_class_str[ESR_ELx_EC(esr)];
  730. }
  731. /*
  732. * bad_el0_sync handles unexpected, but potentially recoverable synchronous
  733. * exceptions taken from EL0.
  734. */
  735. void bad_el0_sync(struct pt_regs *regs, int reason, unsigned long esr)
  736. {
  737. unsigned long pc = instruction_pointer(regs);
  738. current->thread.fault_address = 0;
  739. current->thread.fault_code = esr;
  740. arm64_force_sig_fault(SIGILL, ILL_ILLOPC, pc,
  741. "Bad EL0 synchronous exception");
  742. }
  743. #ifdef CONFIG_VMAP_STACK
  744. DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
  745. __aligned(16);
  746. void panic_bad_stack(struct pt_regs *regs, unsigned long esr, unsigned long far)
  747. {
  748. unsigned long tsk_stk = (unsigned long)current->stack;
  749. unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
  750. unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
  751. console_verbose();
  752. pr_emerg("Insufficient stack space to handle exception!");
  753. pr_emerg("ESR: 0x%016lx -- %s\n", esr, esr_get_class_string(esr));
  754. pr_emerg("FAR: 0x%016lx\n", far);
  755. pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
  756. tsk_stk, tsk_stk + THREAD_SIZE);
  757. pr_emerg("IRQ stack: [0x%016lx..0x%016lx]\n",
  758. irq_stk, irq_stk + IRQ_STACK_SIZE);
  759. pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
  760. ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
  761. __show_regs(regs);
  762. /*
  763. * We use nmi_panic to limit the potential for recusive overflows, and
  764. * to get a better stack trace.
  765. */
  766. nmi_panic(NULL, "kernel stack overflow");
  767. cpu_park_loop();
  768. }
  769. #endif
  770. void __noreturn arm64_serror_panic(struct pt_regs *regs, unsigned long esr)
  771. {
  772. console_verbose();
  773. pr_crit("SError Interrupt on CPU%d, code 0x%016lx -- %s\n",
  774. smp_processor_id(), esr, esr_get_class_string(esr));
  775. trace_android_rvh_arm64_serror_panic(regs, esr);
  776. if (regs)
  777. __show_regs(regs);
  778. nmi_panic(regs, "Asynchronous SError Interrupt");
  779. cpu_park_loop();
  780. unreachable();
  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. int is_valid_bugaddr(unsigned long addr)
  818. {
  819. /*
  820. * bug_handler() only called for BRK #BUG_BRK_IMM.
  821. * So the answer is trivial -- any spurious instances with no
  822. * bug table entry will be rejected by report_bug() and passed
  823. * back to the debug-monitors code and handled as a fatal
  824. * unexpected debug exception.
  825. */
  826. return 1;
  827. }
  828. static int bug_handler(struct pt_regs *regs, unsigned long esr)
  829. {
  830. switch (report_bug(regs->pc, regs)) {
  831. case BUG_TRAP_TYPE_BUG:
  832. die("Oops - BUG", regs, esr);
  833. break;
  834. case BUG_TRAP_TYPE_WARN:
  835. break;
  836. default:
  837. /* unknown/unrecognised bug trap type */
  838. return DBG_HOOK_ERROR;
  839. }
  840. /* If thread survives, skip over the BUG instruction and continue: */
  841. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  842. return DBG_HOOK_HANDLED;
  843. }
  844. static struct break_hook bug_break_hook = {
  845. .fn = bug_handler,
  846. .imm = BUG_BRK_IMM,
  847. };
  848. #ifdef CONFIG_CFI_CLANG
  849. static int cfi_handler(struct pt_regs *regs, unsigned long esr)
  850. {
  851. unsigned long target;
  852. u32 type;
  853. target = pt_regs_read_reg(regs, FIELD_GET(CFI_BRK_IMM_TARGET, esr));
  854. type = (u32)pt_regs_read_reg(regs, FIELD_GET(CFI_BRK_IMM_TYPE, esr));
  855. switch (report_cfi_failure(regs, regs->pc, &target, type)) {
  856. case BUG_TRAP_TYPE_BUG:
  857. die("Oops - CFI", regs, esr);
  858. break;
  859. case BUG_TRAP_TYPE_WARN:
  860. break;
  861. default:
  862. return DBG_HOOK_ERROR;
  863. }
  864. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  865. return DBG_HOOK_HANDLED;
  866. }
  867. static struct break_hook cfi_break_hook = {
  868. .fn = cfi_handler,
  869. .imm = CFI_BRK_IMM_BASE,
  870. .mask = CFI_BRK_IMM_MASK,
  871. };
  872. #endif /* CONFIG_CFI_CLANG */
  873. static int reserved_fault_handler(struct pt_regs *regs, unsigned long esr)
  874. {
  875. pr_err("%s generated an invalid instruction at %pS!\n",
  876. "Kernel text patching",
  877. (void *)instruction_pointer(regs));
  878. /* We cannot handle this */
  879. return DBG_HOOK_ERROR;
  880. }
  881. static struct break_hook fault_break_hook = {
  882. .fn = reserved_fault_handler,
  883. .imm = FAULT_BRK_IMM,
  884. };
  885. #ifdef CONFIG_KASAN_SW_TAGS
  886. #define KASAN_ESR_RECOVER 0x20
  887. #define KASAN_ESR_WRITE 0x10
  888. #define KASAN_ESR_SIZE_MASK 0x0f
  889. #define KASAN_ESR_SIZE(esr) (1 << ((esr) & KASAN_ESR_SIZE_MASK))
  890. static int kasan_handler(struct pt_regs *regs, unsigned long esr)
  891. {
  892. bool recover = esr & KASAN_ESR_RECOVER;
  893. bool write = esr & KASAN_ESR_WRITE;
  894. size_t size = KASAN_ESR_SIZE(esr);
  895. u64 addr = regs->regs[0];
  896. u64 pc = regs->pc;
  897. kasan_report(addr, size, write, pc);
  898. /*
  899. * The instrumentation allows to control whether we can proceed after
  900. * a crash was detected. This is done by passing the -recover flag to
  901. * the compiler. Disabling recovery allows to generate more compact
  902. * code.
  903. *
  904. * Unfortunately disabling recovery doesn't work for the kernel right
  905. * now. KASAN reporting is disabled in some contexts (for example when
  906. * the allocator accesses slab object metadata; this is controlled by
  907. * current->kasan_depth). All these accesses are detected by the tool,
  908. * even though the reports for them are not printed.
  909. *
  910. * This is something that might be fixed at some point in the future.
  911. */
  912. if (!recover)
  913. die("Oops - KASAN", regs, esr);
  914. /* If thread survives, skip over the brk instruction and continue: */
  915. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  916. return DBG_HOOK_HANDLED;
  917. }
  918. static struct break_hook kasan_break_hook = {
  919. .fn = kasan_handler,
  920. .imm = KASAN_BRK_IMM,
  921. .mask = KASAN_BRK_MASK,
  922. };
  923. #endif
  924. #define esr_comment(esr) ((esr) & ESR_ELx_BRK64_ISS_COMMENT_MASK)
  925. /*
  926. * Initial handler for AArch64 BRK exceptions
  927. * This handler only used until debug_traps_init().
  928. */
  929. int __init early_brk64(unsigned long addr, unsigned long esr,
  930. struct pt_regs *regs)
  931. {
  932. #ifdef CONFIG_CFI_CLANG
  933. if ((esr_comment(esr) & ~CFI_BRK_IMM_MASK) == CFI_BRK_IMM_BASE)
  934. return cfi_handler(regs, esr) != DBG_HOOK_HANDLED;
  935. #endif
  936. #ifdef CONFIG_KASAN_SW_TAGS
  937. if ((esr_comment(esr) & ~KASAN_BRK_MASK) == KASAN_BRK_IMM)
  938. return kasan_handler(regs, esr) != DBG_HOOK_HANDLED;
  939. #endif
  940. return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
  941. }
  942. void __init trap_init(void)
  943. {
  944. register_kernel_break_hook(&bug_break_hook);
  945. #ifdef CONFIG_CFI_CLANG
  946. register_kernel_break_hook(&cfi_break_hook);
  947. #endif
  948. register_kernel_break_hook(&fault_break_hook);
  949. #ifdef CONFIG_KASAN_SW_TAGS
  950. register_kernel_break_hook(&kasan_break_hook);
  951. #endif
  952. debug_traps_init();
  953. }