• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Based on arch/arm/kernel/traps.c
3  *
4  * Copyright (C) 1995-2009 Russell King
5  * Copyright (C) 2012 ARM Ltd.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <linux/bug.h>
21 #include <linux/signal.h>
22 #include <linux/personality.h>
23 #include <linux/kallsyms.h>
24 #include <linux/spinlock.h>
25 #include <linux/uaccess.h>
26 #include <linux/hardirq.h>
27 #include <linux/kdebug.h>
28 #include <linux/module.h>
29 #include <linux/kexec.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <linux/sched/signal.h>
33 #include <linux/sched/debug.h>
34 #include <linux/sched/task_stack.h>
35 #include <linux/sizes.h>
36 #include <linux/syscalls.h>
37 #include <linux/mm_types.h>
38 
39 #include <asm/atomic.h>
40 #include <asm/bug.h>
41 #include <asm/cpufeature.h>
42 #include <asm/debug-monitors.h>
43 #include <asm/esr.h>
44 #include <asm/insn.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/exception.h>
50 #include <asm/system_misc.h>
51 #include <asm/sysreg.h>
52 
53 static const char *handler[]= {
54 	"Synchronous Abort",
55 	"IRQ",
56 	"FIQ",
57 	"Error"
58 };
59 
60 int show_unhandled_signals = 0;
61 
62 /*
63  * Dump out the contents of some kernel memory nicely...
64  */
dump_mem(const char * lvl,const char * str,unsigned long bottom,unsigned long top)65 static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
66 		     unsigned long top)
67 {
68 	unsigned long first;
69 	mm_segment_t fs;
70 	int i;
71 
72 	/*
73 	 * We need to switch to kernel mode so that we can use __get_user
74 	 * to safely read from kernel space.
75 	 */
76 	fs = get_fs();
77 	set_fs(KERNEL_DS);
78 
79 	printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);
80 
81 	for (first = bottom & ~31; first < top; first += 32) {
82 		unsigned long p;
83 		char str[sizeof(" 12345678") * 8 + 1];
84 
85 		memset(str, ' ', sizeof(str));
86 		str[sizeof(str) - 1] = '\0';
87 
88 		for (p = first, i = 0; i < (32 / 8)
89 					&& p < top; i++, p += 8) {
90 			if (p >= bottom && p < top) {
91 				unsigned long val;
92 
93 				if (__get_user(val, (unsigned long *)p) == 0)
94 					sprintf(str + i * 17, " %016lx", val);
95 				else
96 					sprintf(str + i * 17, " ????????????????");
97 			}
98 		}
99 		printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
100 	}
101 
102 	set_fs(fs);
103 }
104 
dump_backtrace_entry(unsigned long where)105 static void dump_backtrace_entry(unsigned long where)
106 {
107 	/*
108 	 * Note that 'where' can have a physical address, but it's not handled.
109 	 */
110 	print_ip_sym(where);
111 }
112 
__dump_instr(const char * lvl,struct pt_regs * regs)113 static void __dump_instr(const char *lvl, struct pt_regs *regs)
114 {
115 	unsigned long addr = instruction_pointer(regs);
116 	char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
117 	int i;
118 
119 	for (i = -4; i < 1; i++) {
120 		unsigned int val, bad;
121 
122 		bad = get_user(val, &((u32 *)addr)[i]);
123 
124 		if (!bad)
125 			p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
126 		else {
127 			p += sprintf(p, "bad PC value");
128 			break;
129 		}
130 	}
131 	printk("%sCode: %s\n", lvl, str);
132 }
133 
dump_instr(const char * lvl,struct pt_regs * regs)134 static void dump_instr(const char *lvl, struct pt_regs *regs)
135 {
136 	if (!user_mode(regs)) {
137 		mm_segment_t fs = get_fs();
138 		set_fs(KERNEL_DS);
139 		__dump_instr(lvl, regs);
140 		set_fs(fs);
141 	} else {
142 		__dump_instr(lvl, regs);
143 	}
144 }
145 
dump_backtrace(struct pt_regs * regs,struct task_struct * tsk)146 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
147 {
148 	struct stackframe frame;
149 	int skip = 0;
150 
151 	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);
152 
153 	if (regs) {
154 		if (user_mode(regs))
155 			return;
156 		skip = 1;
157 	}
158 
159 	if (!tsk)
160 		tsk = current;
161 
162 	if (!try_get_task_stack(tsk))
163 		return;
164 
165 	if (tsk == current) {
166 		frame.fp = (unsigned long)__builtin_frame_address(0);
167 		frame.pc = (unsigned long)dump_backtrace;
168 	} else {
169 		/*
170 		 * task blocked in __switch_to
171 		 */
172 		frame.fp = thread_saved_fp(tsk);
173 		frame.pc = thread_saved_pc(tsk);
174 	}
175 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
176 	frame.graph = tsk->curr_ret_stack;
177 #endif
178 
179 	printk("Call trace:\n");
180 	while (1) {
181 		unsigned long stack;
182 		int ret;
183 
184 		/* skip until specified stack frame */
185 		if (!skip) {
186 			dump_backtrace_entry(frame.pc);
187 		} else if (frame.fp == regs->regs[29]) {
188 			skip = 0;
189 			/*
190 			 * Mostly, this is the case where this function is
191 			 * called in panic/abort. As exception handler's
192 			 * stack frame does not contain the corresponding pc
193 			 * at which an exception has taken place, use regs->pc
194 			 * instead.
195 			 */
196 			dump_backtrace_entry(regs->pc);
197 		}
198 		ret = unwind_frame(tsk, &frame);
199 		if (ret < 0)
200 			break;
201 		if (in_entry_text(frame.pc)) {
202 			stack = frame.fp - offsetof(struct pt_regs, stackframe);
203 
204 			if (on_accessible_stack(tsk, stack))
205 				dump_mem("", "Exception stack", stack,
206 					 stack + sizeof(struct pt_regs));
207 		}
208 	}
209 
210 	put_task_stack(tsk);
211 }
212 
show_stack(struct task_struct * tsk,unsigned long * sp)213 void show_stack(struct task_struct *tsk, unsigned long *sp)
214 {
215 	dump_backtrace(NULL, tsk);
216 	barrier();
217 }
218 
219 #ifdef CONFIG_PREEMPT
220 #define S_PREEMPT " PREEMPT"
221 #else
222 #define S_PREEMPT ""
223 #endif
224 #define S_SMP " SMP"
225 
__die(const char * str,int err,struct pt_regs * regs)226 static int __die(const char *str, int err, struct pt_regs *regs)
227 {
228 	struct task_struct *tsk = current;
229 	static int die_counter;
230 	int ret;
231 
232 	pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
233 		 str, err, ++die_counter);
234 
235 	/* trap and error numbers are mostly meaningless on ARM */
236 	ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
237 	if (ret == NOTIFY_STOP)
238 		return ret;
239 
240 	print_modules();
241 	pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
242 		 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk),
243 		 end_of_stack(tsk));
244 	show_regs(regs);
245 
246 	if (!user_mode(regs))
247 		dump_instr(KERN_EMERG, regs);
248 
249 	return ret;
250 }
251 
252 static DEFINE_RAW_SPINLOCK(die_lock);
253 
254 /*
255  * This function is protected against re-entrancy.
256  */
die(const char * str,struct pt_regs * regs,int err)257 void die(const char *str, struct pt_regs *regs, int err)
258 {
259 	int ret;
260 	unsigned long flags;
261 
262 	raw_spin_lock_irqsave(&die_lock, flags);
263 
264 	oops_enter();
265 
266 	console_verbose();
267 	bust_spinlocks(1);
268 	ret = __die(str, err, regs);
269 
270 	if (regs && kexec_should_crash(current))
271 		crash_kexec(regs);
272 
273 	bust_spinlocks(0);
274 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
275 	oops_exit();
276 
277 	if (in_interrupt())
278 		panic("Fatal exception in interrupt");
279 	if (panic_on_oops)
280 		panic("Fatal exception");
281 
282 	raw_spin_unlock_irqrestore(&die_lock, flags);
283 
284 	if (ret != NOTIFY_STOP)
285 		do_exit(SIGSEGV);
286 }
287 
arm64_notify_die(const char * str,struct pt_regs * regs,struct siginfo * info,int err)288 void arm64_notify_die(const char *str, struct pt_regs *regs,
289 		      struct siginfo *info, int err)
290 {
291 	if (user_mode(regs)) {
292 		current->thread.fault_address = 0;
293 		current->thread.fault_code = err;
294 		force_sig_info(info->si_signo, info, current);
295 	} else {
296 		die(str, regs, err);
297 	}
298 }
299 
arm64_skip_faulting_instruction(struct pt_regs * regs,unsigned long size)300 void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
301 {
302 	regs->pc += size;
303 
304 	/*
305 	 * If we were single stepping, we want to get the step exception after
306 	 * we return from the trap.
307 	 */
308 	if (user_mode(regs))
309 		user_fastforward_single_step(current);
310 }
311 
312 static LIST_HEAD(undef_hook);
313 static DEFINE_RAW_SPINLOCK(undef_lock);
314 
register_undef_hook(struct undef_hook * hook)315 void register_undef_hook(struct undef_hook *hook)
316 {
317 	unsigned long flags;
318 
319 	raw_spin_lock_irqsave(&undef_lock, flags);
320 	list_add(&hook->node, &undef_hook);
321 	raw_spin_unlock_irqrestore(&undef_lock, flags);
322 }
323 
unregister_undef_hook(struct undef_hook * hook)324 void unregister_undef_hook(struct undef_hook *hook)
325 {
326 	unsigned long flags;
327 
328 	raw_spin_lock_irqsave(&undef_lock, flags);
329 	list_del(&hook->node);
330 	raw_spin_unlock_irqrestore(&undef_lock, flags);
331 }
332 
call_undef_hook(struct pt_regs * regs)333 static int call_undef_hook(struct pt_regs *regs)
334 {
335 	struct undef_hook *hook;
336 	unsigned long flags;
337 	u32 instr;
338 	int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
339 	void __user *pc = (void __user *)instruction_pointer(regs);
340 
341 	if (!user_mode(regs))
342 		return 1;
343 
344 	if (compat_thumb_mode(regs)) {
345 		/* 16-bit Thumb instruction */
346 		__le16 instr_le;
347 		if (get_user(instr_le, (__le16 __user *)pc))
348 			goto exit;
349 		instr = le16_to_cpu(instr_le);
350 		if (aarch32_insn_is_wide(instr)) {
351 			u32 instr2;
352 
353 			if (get_user(instr_le, (__le16 __user *)(pc + 2)))
354 				goto exit;
355 			instr2 = le16_to_cpu(instr_le);
356 			instr = (instr << 16) | instr2;
357 		}
358 	} else {
359 		/* 32-bit ARM instruction */
360 		__le32 instr_le;
361 		if (get_user(instr_le, (__le32 __user *)pc))
362 			goto exit;
363 		instr = le32_to_cpu(instr_le);
364 	}
365 
366 	raw_spin_lock_irqsave(&undef_lock, flags);
367 	list_for_each_entry(hook, &undef_hook, node)
368 		if ((instr & hook->instr_mask) == hook->instr_val &&
369 			(regs->pstate & hook->pstate_mask) == hook->pstate_val)
370 			fn = hook->fn;
371 
372 	raw_spin_unlock_irqrestore(&undef_lock, flags);
373 exit:
374 	return fn ? fn(regs, instr) : 1;
375 }
376 
force_signal_inject(int signal,int code,struct pt_regs * regs,unsigned long address)377 static void force_signal_inject(int signal, int code, struct pt_regs *regs,
378 				unsigned long address)
379 {
380 	siginfo_t info;
381 	void __user *pc = (void __user *)instruction_pointer(regs);
382 	const char *desc;
383 
384 	switch (signal) {
385 	case SIGILL:
386 		desc = "undefined instruction";
387 		break;
388 	case SIGSEGV:
389 		desc = "illegal memory access";
390 		break;
391 	default:
392 		desc = "bad mode";
393 		break;
394 	}
395 
396 	if (unhandled_signal(current, signal) &&
397 	    show_unhandled_signals_ratelimited()) {
398 		pr_info("%s[%d]: %s: pc=%p\n",
399 			current->comm, task_pid_nr(current), desc, pc);
400 		dump_instr(KERN_INFO, regs);
401 	}
402 
403 	info.si_signo = signal;
404 	info.si_errno = 0;
405 	info.si_code  = code;
406 	info.si_addr  = pc;
407 
408 	arm64_notify_die(desc, regs, &info, 0);
409 }
410 
411 /*
412  * Set up process info to signal segmentation fault - called on access error.
413  */
arm64_notify_segfault(struct pt_regs * regs,unsigned long addr)414 void arm64_notify_segfault(struct pt_regs *regs, unsigned long addr)
415 {
416 	int code;
417 
418 	down_read(&current->mm->mmap_sem);
419 	if (find_vma(current->mm, addr) == NULL)
420 		code = SEGV_MAPERR;
421 	else
422 		code = SEGV_ACCERR;
423 	up_read(&current->mm->mmap_sem);
424 
425 	force_signal_inject(SIGSEGV, code, regs, addr);
426 }
427 
do_undefinstr(struct pt_regs * regs)428 asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
429 {
430 	/* check for AArch32 breakpoint instructions */
431 	if (!aarch32_break_handler(regs))
432 		return;
433 
434 	if (call_undef_hook(regs) == 0)
435 		return;
436 
437 	force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
438 }
439 
cpu_enable_cache_maint_trap(const struct arm64_cpu_capabilities * __unused)440 void cpu_enable_cache_maint_trap(const struct arm64_cpu_capabilities *__unused)
441 {
442 	config_sctlr_el1(SCTLR_EL1_UCI, 0);
443 }
444 
445 #define __user_cache_maint(insn, address, res)			\
446 	if (address >= user_addr_max()) {			\
447 		res = -EFAULT;					\
448 	} else {						\
449 		uaccess_ttbr0_enable();				\
450 		asm volatile (					\
451 			"1:	" insn ", %1\n"			\
452 			"	mov	%w0, #0\n"		\
453 			"2:\n"					\
454 			"	.pushsection .fixup,\"ax\"\n"	\
455 			"	.align	2\n"			\
456 			"3:	mov	%w0, %w2\n"		\
457 			"	b	2b\n"			\
458 			"	.popsection\n"			\
459 			_ASM_EXTABLE(1b, 3b)			\
460 			: "=r" (res)				\
461 			: "r" (address), "i" (-EFAULT));	\
462 		uaccess_ttbr0_disable();			\
463 	}
464 
user_cache_maint_handler(unsigned int esr,struct pt_regs * regs)465 static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
466 {
467 	unsigned long address;
468 	int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
469 	int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
470 	int ret = 0;
471 
472 	address = untagged_addr(pt_regs_read_reg(regs, rt));
473 
474 	switch (crm) {
475 	case ESR_ELx_SYS64_ISS_CRM_DC_CVAU:	/* DC CVAU, gets promoted */
476 		__user_cache_maint("dc civac", address, ret);
477 		break;
478 	case ESR_ELx_SYS64_ISS_CRM_DC_CVAC:	/* DC CVAC, gets promoted */
479 		__user_cache_maint("dc civac", address, ret);
480 		break;
481 	case ESR_ELx_SYS64_ISS_CRM_DC_CVAP:	/* DC CVAP */
482 		__user_cache_maint("sys 3, c7, c12, 1", address, ret);
483 		break;
484 	case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC:	/* DC CIVAC */
485 		__user_cache_maint("dc civac", address, ret);
486 		break;
487 	case ESR_ELx_SYS64_ISS_CRM_IC_IVAU:	/* IC IVAU */
488 		__user_cache_maint("ic ivau", address, ret);
489 		break;
490 	default:
491 		force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
492 		return;
493 	}
494 
495 	if (ret)
496 		arm64_notify_segfault(regs, address);
497 	else
498 		arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
499 }
500 
ctr_read_handler(unsigned int esr,struct pt_regs * regs)501 static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
502 {
503 	int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
504 	unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);
505 
506 	pt_regs_write_reg(regs, rt, val);
507 
508 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
509 }
510 
cntvct_read_handler(unsigned int esr,struct pt_regs * regs)511 static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
512 {
513 	int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
514 
515 	pt_regs_write_reg(regs, rt, arch_counter_get_cntvct());
516 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
517 }
518 
cntfrq_read_handler(unsigned int esr,struct pt_regs * regs)519 static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
520 {
521 	int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
522 
523 	pt_regs_write_reg(regs, rt, arch_timer_get_rate());
524 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
525 }
526 
527 struct sys64_hook {
528 	unsigned int esr_mask;
529 	unsigned int esr_val;
530 	void (*handler)(unsigned int esr, struct pt_regs *regs);
531 };
532 
533 static struct sys64_hook sys64_hooks[] = {
534 	{
535 		.esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
536 		.esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
537 		.handler = user_cache_maint_handler,
538 	},
539 	{
540 		/* Trap read access to CTR_EL0 */
541 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
542 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
543 		.handler = ctr_read_handler,
544 	},
545 	{
546 		/* Trap read access to CNTVCT_EL0 */
547 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
548 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
549 		.handler = cntvct_read_handler,
550 	},
551 	{
552 		/* Trap read access to CNTFRQ_EL0 */
553 		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
554 		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
555 		.handler = cntfrq_read_handler,
556 	},
557 	{},
558 };
559 
do_sysinstr(unsigned int esr,struct pt_regs * regs)560 asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
561 {
562 	struct sys64_hook *hook;
563 
564 	for (hook = sys64_hooks; hook->handler; hook++)
565 		if ((hook->esr_mask & esr) == hook->esr_val) {
566 			hook->handler(esr, regs);
567 			return;
568 		}
569 
570 	/*
571 	 * New SYS instructions may previously have been undefined at EL0. Fall
572 	 * back to our usual undefined instruction handler so that we handle
573 	 * these consistently.
574 	 */
575 	do_undefinstr(regs);
576 }
577 
578 long compat_arm_syscall(struct pt_regs *regs);
579 
do_ni_syscall(struct pt_regs * regs)580 asmlinkage long do_ni_syscall(struct pt_regs *regs)
581 {
582 #ifdef CONFIG_COMPAT
583 	long ret;
584 	if (is_compat_task()) {
585 		ret = compat_arm_syscall(regs);
586 		if (ret != -ENOSYS)
587 			return ret;
588 	}
589 #endif
590 
591 	return sys_ni_syscall();
592 }
593 
594 static const char *esr_class_str[] = {
595 	[0 ... ESR_ELx_EC_MAX]		= "UNRECOGNIZED EC",
596 	[ESR_ELx_EC_UNKNOWN]		= "Unknown/Uncategorized",
597 	[ESR_ELx_EC_WFx]		= "WFI/WFE",
598 	[ESR_ELx_EC_CP15_32]		= "CP15 MCR/MRC",
599 	[ESR_ELx_EC_CP15_64]		= "CP15 MCRR/MRRC",
600 	[ESR_ELx_EC_CP14_MR]		= "CP14 MCR/MRC",
601 	[ESR_ELx_EC_CP14_LS]		= "CP14 LDC/STC",
602 	[ESR_ELx_EC_FP_ASIMD]		= "ASIMD",
603 	[ESR_ELx_EC_CP10_ID]		= "CP10 MRC/VMRS",
604 	[ESR_ELx_EC_CP14_64]		= "CP14 MCRR/MRRC",
605 	[ESR_ELx_EC_ILL]		= "PSTATE.IL",
606 	[ESR_ELx_EC_SVC32]		= "SVC (AArch32)",
607 	[ESR_ELx_EC_HVC32]		= "HVC (AArch32)",
608 	[ESR_ELx_EC_SMC32]		= "SMC (AArch32)",
609 	[ESR_ELx_EC_SVC64]		= "SVC (AArch64)",
610 	[ESR_ELx_EC_HVC64]		= "HVC (AArch64)",
611 	[ESR_ELx_EC_SMC64]		= "SMC (AArch64)",
612 	[ESR_ELx_EC_SYS64]		= "MSR/MRS (AArch64)",
613 	[ESR_ELx_EC_IMP_DEF]		= "EL3 IMP DEF",
614 	[ESR_ELx_EC_IABT_LOW]		= "IABT (lower EL)",
615 	[ESR_ELx_EC_IABT_CUR]		= "IABT (current EL)",
616 	[ESR_ELx_EC_PC_ALIGN]		= "PC Alignment",
617 	[ESR_ELx_EC_DABT_LOW]		= "DABT (lower EL)",
618 	[ESR_ELx_EC_DABT_CUR]		= "DABT (current EL)",
619 	[ESR_ELx_EC_SP_ALIGN]		= "SP Alignment",
620 	[ESR_ELx_EC_FP_EXC32]		= "FP (AArch32)",
621 	[ESR_ELx_EC_FP_EXC64]		= "FP (AArch64)",
622 	[ESR_ELx_EC_SERROR]		= "SError",
623 	[ESR_ELx_EC_BREAKPT_LOW]	= "Breakpoint (lower EL)",
624 	[ESR_ELx_EC_BREAKPT_CUR]	= "Breakpoint (current EL)",
625 	[ESR_ELx_EC_SOFTSTP_LOW]	= "Software Step (lower EL)",
626 	[ESR_ELx_EC_SOFTSTP_CUR]	= "Software Step (current EL)",
627 	[ESR_ELx_EC_WATCHPT_LOW]	= "Watchpoint (lower EL)",
628 	[ESR_ELx_EC_WATCHPT_CUR]	= "Watchpoint (current EL)",
629 	[ESR_ELx_EC_BKPT32]		= "BKPT (AArch32)",
630 	[ESR_ELx_EC_VECTOR32]		= "Vector catch (AArch32)",
631 	[ESR_ELx_EC_BRK64]		= "BRK (AArch64)",
632 };
633 
esr_get_class_string(u32 esr)634 const char *esr_get_class_string(u32 esr)
635 {
636 	return esr_class_str[ESR_ELx_EC(esr)];
637 }
638 
639 /*
640  * bad_mode handles the impossible case in the exception vector. This is always
641  * fatal.
642  */
bad_mode(struct pt_regs * regs,int reason,unsigned int esr)643 asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
644 {
645 	console_verbose();
646 
647 	pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
648 		handler[reason], smp_processor_id(), esr,
649 		esr_get_class_string(esr));
650 
651 	local_irq_disable();
652 	panic("bad mode");
653 }
654 
655 /*
656  * bad_el0_sync handles unexpected, but potentially recoverable synchronous
657  * exceptions taken from EL0. Unlike bad_mode, this returns.
658  */
bad_el0_sync(struct pt_regs * regs,int reason,unsigned int esr)659 asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
660 {
661 	siginfo_t info;
662 	void __user *pc = (void __user *)instruction_pointer(regs);
663 	console_verbose();
664 
665 	pr_crit("Bad EL0 synchronous exception detected on CPU%d, code 0x%08x -- %s\n",
666 		smp_processor_id(), esr, esr_get_class_string(esr));
667 	__show_regs(regs);
668 
669 	info.si_signo = SIGILL;
670 	info.si_errno = 0;
671 	info.si_code  = ILL_ILLOPC;
672 	info.si_addr  = pc;
673 
674 	current->thread.fault_address = 0;
675 	current->thread.fault_code = 0;
676 
677 	force_sig_info(info.si_signo, &info, current);
678 }
679 
680 #ifdef CONFIG_VMAP_STACK
681 
682 DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
683 	__aligned(16);
684 
handle_bad_stack(struct pt_regs * regs)685 asmlinkage void handle_bad_stack(struct pt_regs *regs)
686 {
687 	unsigned long tsk_stk = (unsigned long)current->stack;
688 	unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
689 	unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
690 	unsigned int esr = read_sysreg(esr_el1);
691 	unsigned long far = read_sysreg(far_el1);
692 
693 	console_verbose();
694 	pr_emerg("Insufficient stack space to handle exception!");
695 
696 	pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
697 	pr_emerg("FAR: 0x%016lx\n", far);
698 
699 	pr_emerg("Task stack:     [0x%016lx..0x%016lx]\n",
700 		 tsk_stk, tsk_stk + THREAD_SIZE);
701 	pr_emerg("IRQ stack:      [0x%016lx..0x%016lx]\n",
702 		 irq_stk, irq_stk + THREAD_SIZE);
703 	pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
704 		 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
705 
706 	__show_regs(regs);
707 
708 	/*
709 	 * We use nmi_panic to limit the potential for recusive overflows, and
710 	 * to get a better stack trace.
711 	 */
712 	nmi_panic(NULL, "kernel stack overflow");
713 	cpu_park_loop();
714 }
715 #endif
716 
__pte_error(const char * file,int line,unsigned long val)717 void __pte_error(const char *file, int line, unsigned long val)
718 {
719 	pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
720 }
721 
__pmd_error(const char * file,int line,unsigned long val)722 void __pmd_error(const char *file, int line, unsigned long val)
723 {
724 	pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
725 }
726 
__pud_error(const char * file,int line,unsigned long val)727 void __pud_error(const char *file, int line, unsigned long val)
728 {
729 	pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
730 }
731 
__pgd_error(const char * file,int line,unsigned long val)732 void __pgd_error(const char *file, int line, unsigned long val)
733 {
734 	pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
735 }
736 
737 /* GENERIC_BUG traps */
738 
is_valid_bugaddr(unsigned long addr)739 int is_valid_bugaddr(unsigned long addr)
740 {
741 	/*
742 	 * bug_handler() only called for BRK #BUG_BRK_IMM.
743 	 * So the answer is trivial -- any spurious instances with no
744 	 * bug table entry will be rejected by report_bug() and passed
745 	 * back to the debug-monitors code and handled as a fatal
746 	 * unexpected debug exception.
747 	 */
748 	return 1;
749 }
750 
bug_handler(struct pt_regs * regs,unsigned int esr)751 static int bug_handler(struct pt_regs *regs, unsigned int esr)
752 {
753 	if (user_mode(regs))
754 		return DBG_HOOK_ERROR;
755 
756 	switch (report_bug(regs->pc, regs)) {
757 	case BUG_TRAP_TYPE_BUG:
758 		die("Oops - BUG", regs, 0);
759 		break;
760 
761 	case BUG_TRAP_TYPE_WARN:
762 		break;
763 
764 	default:
765 		/* unknown/unrecognised bug trap type */
766 		return DBG_HOOK_ERROR;
767 	}
768 
769 	/* If thread survives, skip over the BUG instruction and continue: */
770 	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
771 	return DBG_HOOK_HANDLED;
772 }
773 
774 static struct break_hook bug_break_hook = {
775 	.esr_val = 0xf2000000 | BUG_BRK_IMM,
776 	.esr_mask = 0xffffffff,
777 	.fn = bug_handler,
778 };
779 
780 /*
781  * Initial handler for AArch64 BRK exceptions
782  * This handler only used until debug_traps_init().
783  */
early_brk64(unsigned long addr,unsigned int esr,struct pt_regs * regs)784 int __init early_brk64(unsigned long addr, unsigned int esr,
785 		struct pt_regs *regs)
786 {
787 	return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
788 }
789 
790 /* This registration must happen early, before debug_traps_init(). */
trap_init(void)791 void __init trap_init(void)
792 {
793 	register_break_hook(&bug_break_hook);
794 }
795