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