traps.c 20.5 KB
Newer Older
C
Catalin Marinas 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Based on arch/arm/kernel/traps.c
 *
 * Copyright (C) 1995-2009 Russell King
 * Copyright (C) 2012 ARM Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

20
#include <linux/bug.h>
C
Catalin Marinas 已提交
21 22 23 24 25 26 27 28 29 30 31
#include <linux/signal.h>
#include <linux/personality.h>
#include <linux/kallsyms.h>
#include <linux/spinlock.h>
#include <linux/uaccess.h>
#include <linux/hardirq.h>
#include <linux/kdebug.h>
#include <linux/module.h>
#include <linux/kexec.h>
#include <linux/delay.h>
#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>
C
Catalin Marinas 已提交
36
#include <linux/syscalls.h>
37
#include <linux/mm_types.h>
C
Catalin Marinas 已提交
38 39

#include <asm/atomic.h>
40
#include <asm/bug.h>
41
#include <asm/cpufeature.h>
42
#include <asm/daifflags.h>
43
#include <asm/debug-monitors.h>
44
#include <asm/esr.h>
45
#include <asm/insn.h>
C
Catalin Marinas 已提交
46
#include <asm/traps.h>
47
#include <asm/smp.h>
48
#include <asm/stack_pointer.h>
C
Catalin Marinas 已提交
49 50 51
#include <asm/stacktrace.h>
#include <asm/exception.h>
#include <asm/system_misc.h>
52
#include <asm/sysreg.h>
C
Catalin Marinas 已提交
53 54 55 56 57 58 59 60

static const char *handler[]= {
	"Synchronous Abort",
	"IRQ",
	"FIQ",
	"Error"
};

61
int show_unhandled_signals = 0;
C
Catalin Marinas 已提交
62

63
static void dump_backtrace_entry(unsigned long where)
C
Catalin Marinas 已提交
64
{
65
	printk(" %pS\n", (void *)where);
C
Catalin Marinas 已提交
66 67
}

68
static void __dump_instr(const char *lvl, struct pt_regs *regs)
C
Catalin Marinas 已提交
69 70 71 72 73 74 75 76
{
	unsigned long addr = instruction_pointer(regs);
	char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
	int i;

	for (i = -4; i < 1; i++) {
		unsigned int val, bad;

77
		bad = get_user(val, &((u32 *)addr)[i]);
C
Catalin Marinas 已提交
78 79 80 81 82 83 84 85 86

		if (!bad)
			p += sprintf(p, i == 0 ? "(%08x) " : "%08x ", val);
		else {
			p += sprintf(p, "bad PC value");
			break;
		}
	}
	printk("%sCode: %s\n", lvl, str);
87
}
C
Catalin Marinas 已提交
88

89 90 91 92 93 94 95 96 97 98
static void dump_instr(const char *lvl, struct pt_regs *regs)
{
	if (!user_mode(regs)) {
		mm_segment_t fs = get_fs();
		set_fs(KERNEL_DS);
		__dump_instr(lvl, regs);
		set_fs(fs);
	} else {
		__dump_instr(lvl, regs);
	}
C
Catalin Marinas 已提交
99 100
}

101
void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
C
Catalin Marinas 已提交
102 103
{
	struct stackframe frame;
104
	int skip = 0;
C
Catalin Marinas 已提交
105

106 107
	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);

108 109 110 111 112 113
	if (regs) {
		if (user_mode(regs))
			return;
		skip = 1;
	}

114 115 116
	if (!tsk)
		tsk = current;

117 118 119
	if (!try_get_task_stack(tsk))
		return;

120
	if (tsk == current) {
C
Catalin Marinas 已提交
121 122 123 124 125 126 127 128 129
		frame.fp = (unsigned long)__builtin_frame_address(0);
		frame.pc = (unsigned long)dump_backtrace;
	} else {
		/*
		 * task blocked in __switch_to
		 */
		frame.fp = thread_saved_fp(tsk);
		frame.pc = thread_saved_pc(tsk);
	}
130 131 132
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
	frame.graph = tsk->curr_ret_stack;
#endif
C
Catalin Marinas 已提交
133

134
	printk("Call trace:\n");
135
	do {
136 137
		/* skip until specified stack frame */
		if (!skip) {
138
			dump_backtrace_entry(frame.pc);
139 140 141 142 143 144 145 146 147 148 149
		} else if (frame.fp == regs->regs[29]) {
			skip = 0;
			/*
			 * Mostly, this is the case where this function is
			 * called in panic/abort. As exception handler's
			 * stack frame does not contain the corresponding pc
			 * at which an exception has taken place, use regs->pc
			 * instead.
			 */
			dump_backtrace_entry(regs->pc);
		}
150
	} while (!unwind_frame(tsk, &frame));
151 152

	put_task_stack(tsk);
C
Catalin Marinas 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
}

void show_stack(struct task_struct *tsk, unsigned long *sp)
{
	dump_backtrace(NULL, tsk);
	barrier();
}

#ifdef CONFIG_PREEMPT
#define S_PREEMPT " PREEMPT"
#else
#define S_PREEMPT ""
#endif
#define S_SMP " SMP"

168
static int __die(const char *str, int err, struct pt_regs *regs)
C
Catalin Marinas 已提交
169
{
170
	struct task_struct *tsk = current;
C
Catalin Marinas 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183
	static int die_counter;
	int ret;

	pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
		 str, err, ++die_counter);

	/* trap and error numbers are mostly meaningless on ARM */
	ret = notify_die(DIE_OOPS, str, regs, err, 0, SIGSEGV);
	if (ret == NOTIFY_STOP)
		return ret;

	print_modules();
	pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
184 185
		 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk),
		 end_of_stack(tsk));
186
	show_regs(regs);
C
Catalin Marinas 已提交
187

188
	if (!user_mode(regs))
C
Catalin Marinas 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201
		dump_instr(KERN_EMERG, regs);

	return ret;
}

static DEFINE_RAW_SPINLOCK(die_lock);

/*
 * This function is protected against re-entrancy.
 */
void die(const char *str, struct pt_regs *regs, int err)
{
	int ret;
Q
Qiao Zhou 已提交
202 203 204
	unsigned long flags;

	raw_spin_lock_irqsave(&die_lock, flags);
C
Catalin Marinas 已提交
205 206 207 208 209

	oops_enter();

	console_verbose();
	bust_spinlocks(1);
210
	ret = __die(str, err, regs);
C
Catalin Marinas 已提交
211

212
	if (regs && kexec_should_crash(current))
C
Catalin Marinas 已提交
213 214 215
		crash_kexec(regs);

	bust_spinlocks(0);
216
	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
C
Catalin Marinas 已提交
217 218 219 220 221 222
	oops_exit();

	if (in_interrupt())
		panic("Fatal exception in interrupt");
	if (panic_on_oops)
		panic("Fatal exception");
Q
Qiao Zhou 已提交
223 224 225

	raw_spin_unlock_irqrestore(&die_lock, flags);

C
Catalin Marinas 已提交
226 227 228 229
	if (ret != NOTIFY_STOP)
		do_exit(SIGSEGV);
}

230 231 232 233 234 235 236
static bool show_unhandled_signals_ratelimited(void)
{
	static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
				      DEFAULT_RATELIMIT_BURST);
	return show_unhandled_signals && __ratelimit(&rs);
}

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
void arm64_force_sig_info(struct siginfo *info, const char *str,
			  struct task_struct *tsk)
{
	unsigned int esr = tsk->thread.fault_code;
	struct pt_regs *regs = task_pt_regs(tsk);

	if (!unhandled_signal(tsk, info->si_signo))
		goto send_sig;

	if (!show_unhandled_signals_ratelimited())
		goto send_sig;

	pr_info("%s[%d]: unhandled exception: ", tsk->comm, task_pid_nr(tsk));
	if (esr)
		pr_cont("%s, ESR 0x%08x, ", esr_get_class_string(esr), esr);

	pr_cont("%s", str);
	print_vma_addr(KERN_CONT " in ", regs->pc);
	pr_cont("\n");
	__show_regs(regs);

send_sig:
	force_sig_info(info->si_signo, info, tsk);
}

C
Catalin Marinas 已提交
262 263 264
void arm64_notify_die(const char *str, struct pt_regs *regs,
		      struct siginfo *info, int err)
{
265
	if (user_mode(regs)) {
266
		WARN_ON(regs != current_pt_regs());
267 268
		current->thread.fault_address = 0;
		current->thread.fault_code = err;
269
		arm64_force_sig_info(info, str, current);
270
	} else {
C
Catalin Marinas 已提交
271
		die(str, regs, err);
272
	}
C
Catalin Marinas 已提交
273 274
}

275 276 277 278 279 280 281 282
void arm64_skip_faulting_instruction(struct pt_regs *regs, unsigned long size)
{
	regs->pc += size;

	/*
	 * If we were single stepping, we want to get the step exception after
	 * we return from the trap.
	 */
283 284
	if (user_mode(regs))
		user_fastforward_single_step(current);
285 286
}

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
static LIST_HEAD(undef_hook);
static DEFINE_RAW_SPINLOCK(undef_lock);

void register_undef_hook(struct undef_hook *hook)
{
	unsigned long flags;

	raw_spin_lock_irqsave(&undef_lock, flags);
	list_add(&hook->node, &undef_hook);
	raw_spin_unlock_irqrestore(&undef_lock, flags);
}

void unregister_undef_hook(struct undef_hook *hook)
{
	unsigned long flags;

	raw_spin_lock_irqsave(&undef_lock, flags);
	list_del(&hook->node);
	raw_spin_unlock_irqrestore(&undef_lock, flags);
}

static int call_undef_hook(struct pt_regs *regs)
{
	struct undef_hook *hook;
	unsigned long flags;
	u32 instr;
	int (*fn)(struct pt_regs *regs, u32 instr) = NULL;
	void __user *pc = (void __user *)instruction_pointer(regs);

316 317 318 319 320 321
	if (!user_mode(regs)) {
		__le32 instr_le;
		if (probe_kernel_address((__force __le32 *)pc, instr_le))
			goto exit;
		instr = le32_to_cpu(instr_le);
	} else if (compat_thumb_mode(regs)) {
322
		/* 16-bit Thumb instruction */
323 324
		__le16 instr_le;
		if (get_user(instr_le, (__le16 __user *)pc))
325
			goto exit;
326
		instr = le16_to_cpu(instr_le);
327 328 329
		if (aarch32_insn_is_wide(instr)) {
			u32 instr2;

330
			if (get_user(instr_le, (__le16 __user *)(pc + 2)))
331
				goto exit;
332
			instr2 = le16_to_cpu(instr_le);
333 334 335 336
			instr = (instr << 16) | instr2;
		}
	} else {
		/* 32-bit ARM instruction */
337 338
		__le32 instr_le;
		if (get_user(instr_le, (__le32 __user *)pc))
339
			goto exit;
340
		instr = le32_to_cpu(instr_le);
341 342 343 344 345 346 347 348 349 350 351 352 353
	}

	raw_spin_lock_irqsave(&undef_lock, flags);
	list_for_each_entry(hook, &undef_hook, node)
		if ((instr & hook->instr_mask) == hook->instr_val &&
			(regs->pstate & hook->pstate_mask) == hook->pstate_val)
			fn = hook->fn;

	raw_spin_unlock_irqrestore(&undef_lock, flags);
exit:
	return fn ? fn(regs, instr) : 1;
}

354
void force_signal_inject(int signal, int code, unsigned long address)
C
Catalin Marinas 已提交
355 356
{
	siginfo_t info;
357
	const char *desc;
358 359 360
	struct pt_regs *regs = current_pt_regs();

	clear_siginfo(&info);
361 362 363 364 365 366 367 368 369

	switch (signal) {
	case SIGILL:
		desc = "undefined instruction";
		break;
	case SIGSEGV:
		desc = "illegal memory access";
		break;
	default:
370
		desc = "unknown or unrecoverable error";
371 372 373
		break;
	}

374
	/* Force signals we don't understand to SIGKILL */
375
	if (WARN_ON(signal != SIGKILL &&
376 377 378 379
		    siginfo_layout(signal, code) != SIL_FAULT)) {
		signal = SIGKILL;
	}

380 381 382
	info.si_signo = signal;
	info.si_errno = 0;
	info.si_code  = code;
383
	info.si_addr  = (void __user *)address;
384 385 386 387 388 389 390

	arm64_notify_die(desc, regs, &info, 0);
}

/*
 * Set up process info to signal segmentation fault - called on access error.
 */
391
void arm64_notify_segfault(unsigned long addr)
392 393 394 395 396 397 398 399 400
{
	int code;

	down_read(&current->mm->mmap_sem);
	if (find_vma(current->mm, addr) == NULL)
		code = SEGV_MAPERR;
	else
		code = SEGV_ACCERR;
	up_read(&current->mm->mmap_sem);
C
Catalin Marinas 已提交
401

402
	force_signal_inject(SIGSEGV, code, addr);
403 404 405 406
}

asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
{
C
Catalin Marinas 已提交
407
	/* check for AArch32 breakpoint instructions */
408
	if (!aarch32_break_handler(regs))
C
Catalin Marinas 已提交
409 410
		return;

411 412 413
	if (call_undef_hook(regs) == 0)
		return;

414
	force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
415
	BUG_ON(!user_mode(regs));
C
Catalin Marinas 已提交
416 417
}

418
void cpu_enable_cache_maint_trap(const struct arm64_cpu_capabilities *__unused)
419
{
M
Mark Rutland 已提交
420
	sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCI, 0);
421 422 423
}

#define __user_cache_maint(insn, address, res)			\
424
	if (address >= user_addr_max()) {			\
425
		res = -EFAULT;					\
426 427
	} else {						\
		uaccess_ttbr0_enable();				\
428 429 430 431 432 433 434 435 436 437 438
		asm volatile (					\
			"1:	" insn ", %1\n"			\
			"	mov	%w0, #0\n"		\
			"2:\n"					\
			"	.pushsection .fixup,\"ax\"\n"	\
			"	.align	2\n"			\
			"3:	mov	%w0, %w2\n"		\
			"	b	2b\n"			\
			"	.popsection\n"			\
			_ASM_EXTABLE(1b, 3b)			\
			: "=r" (res)				\
439 440 441
			: "r" (address), "i" (-EFAULT));	\
		uaccess_ttbr0_disable();			\
	}
442

443
static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
444 445
{
	unsigned long address;
446 447 448
	int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
	int crm = (esr & ESR_ELx_SYS64_ISS_CRM_MASK) >> ESR_ELx_SYS64_ISS_CRM_SHIFT;
	int ret = 0;
449

450
	address = untagged_addr(pt_regs_read_reg(regs, rt));
451

452 453 454 455 456 457 458
	switch (crm) {
	case ESR_ELx_SYS64_ISS_CRM_DC_CVAU:	/* DC CVAU, gets promoted */
		__user_cache_maint("dc civac", address, ret);
		break;
	case ESR_ELx_SYS64_ISS_CRM_DC_CVAC:	/* DC CVAC, gets promoted */
		__user_cache_maint("dc civac", address, ret);
		break;
R
Robin Murphy 已提交
459 460 461
	case ESR_ELx_SYS64_ISS_CRM_DC_CVAP:	/* DC CVAP */
		__user_cache_maint("sys 3, c7, c12, 1", address, ret);
		break;
462 463 464 465 466 467 468
	case ESR_ELx_SYS64_ISS_CRM_DC_CIVAC:	/* DC CIVAC */
		__user_cache_maint("dc civac", address, ret);
		break;
	case ESR_ELx_SYS64_ISS_CRM_IC_IVAU:	/* IC IVAU */
		__user_cache_maint("ic ivau", address, ret);
		break;
	default:
469
		force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
470 471 472 473
		return;
	}

	if (ret)
474
		arm64_notify_segfault(address);
475
	else
476
		arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
477 478
}

479 480 481
static void ctr_read_handler(unsigned int esr, struct pt_regs *regs)
{
	int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;
482 483
	unsigned long val = arm64_ftr_reg_user_value(&arm64_ftr_reg_ctrel0);

484 485
	if (cpus_have_const_cap(ARM64_WORKAROUND_1542419)) {
		/* Hide DIC so that we can trap the unnecessary maintenance...*/
486 487
		val &= ~BIT(CTR_DIC_SHIFT);

488 489 490 491 492
		/* ... and fake IminLine to reduce the number of traps. */
		val &= ~CTR_IMINLINE_MASK;
		val |= (PAGE_SHIFT - 2) & CTR_IMINLINE_MASK;
	}

493
	pt_regs_write_reg(regs, rt, val);
494

495
	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
496 497
}

498 499 500 501 502
static void cntvct_read_handler(unsigned int esr, struct pt_regs *regs)
{
	int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;

	pt_regs_write_reg(regs, rt, arch_counter_get_cntvct());
503
	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
504 505
}

506 507 508 509
static void cntfrq_read_handler(unsigned int esr, struct pt_regs *regs)
{
	int rt = (esr & ESR_ELx_SYS64_ISS_RT_MASK) >> ESR_ELx_SYS64_ISS_RT_SHIFT;

510
	pt_regs_write_reg(regs, rt, arch_timer_get_rate());
511
	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
512 513
}

514 515 516 517 518 519 520 521 522 523 524 525
struct sys64_hook {
	unsigned int esr_mask;
	unsigned int esr_val;
	void (*handler)(unsigned int esr, struct pt_regs *regs);
};

static struct sys64_hook sys64_hooks[] = {
	{
		.esr_mask = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_MASK,
		.esr_val = ESR_ELx_SYS64_ISS_EL0_CACHE_OP_VAL,
		.handler = user_cache_maint_handler,
	},
526 527 528 529 530 531
	{
		/* Trap read access to CTR_EL0 */
		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
		.esr_val = ESR_ELx_SYS64_ISS_SYS_CTR_READ,
		.handler = ctr_read_handler,
	},
532 533 534 535 536 537
	{
		/* Trap read access to CNTVCT_EL0 */
		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTVCT,
		.handler = cntvct_read_handler,
	},
538 539 540 541 542 543
	{
		/* Trap read access to CNTFRQ_EL0 */
		.esr_mask = ESR_ELx_SYS64_ISS_SYS_OP_MASK,
		.esr_val = ESR_ELx_SYS64_ISS_SYS_CNTFRQ,
		.handler = cntfrq_read_handler,
	},
544 545 546 547 548 549 550 551 552 553 554 555 556
	{},
};

asmlinkage void __exception do_sysinstr(unsigned int esr, struct pt_regs *regs)
{
	struct sys64_hook *hook;

	for (hook = sys64_hooks; hook->handler; hook++)
		if ((hook->esr_mask & esr) == hook->esr_val) {
			hook->handler(esr, regs);
			return;
		}

557 558 559 560 561 562
	/*
	 * New SYS instructions may previously have been undefined at EL0. Fall
	 * back to our usual undefined instruction handler so that we handle
	 * these consistently.
	 */
	do_undefinstr(regs);
563 564
}

565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
static const char *esr_class_str[] = {
	[0 ... ESR_ELx_EC_MAX]		= "UNRECOGNIZED EC",
	[ESR_ELx_EC_UNKNOWN]		= "Unknown/Uncategorized",
	[ESR_ELx_EC_WFx]		= "WFI/WFE",
	[ESR_ELx_EC_CP15_32]		= "CP15 MCR/MRC",
	[ESR_ELx_EC_CP15_64]		= "CP15 MCRR/MRRC",
	[ESR_ELx_EC_CP14_MR]		= "CP14 MCR/MRC",
	[ESR_ELx_EC_CP14_LS]		= "CP14 LDC/STC",
	[ESR_ELx_EC_FP_ASIMD]		= "ASIMD",
	[ESR_ELx_EC_CP10_ID]		= "CP10 MRC/VMRS",
	[ESR_ELx_EC_CP14_64]		= "CP14 MCRR/MRRC",
	[ESR_ELx_EC_ILL]		= "PSTATE.IL",
	[ESR_ELx_EC_SVC32]		= "SVC (AArch32)",
	[ESR_ELx_EC_HVC32]		= "HVC (AArch32)",
	[ESR_ELx_EC_SMC32]		= "SMC (AArch32)",
	[ESR_ELx_EC_SVC64]		= "SVC (AArch64)",
	[ESR_ELx_EC_HVC64]		= "HVC (AArch64)",
	[ESR_ELx_EC_SMC64]		= "SMC (AArch64)",
	[ESR_ELx_EC_SYS64]		= "MSR/MRS (AArch64)",
584
	[ESR_ELx_EC_SVE]		= "SVE",
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	[ESR_ELx_EC_IMP_DEF]		= "EL3 IMP DEF",
	[ESR_ELx_EC_IABT_LOW]		= "IABT (lower EL)",
	[ESR_ELx_EC_IABT_CUR]		= "IABT (current EL)",
	[ESR_ELx_EC_PC_ALIGN]		= "PC Alignment",
	[ESR_ELx_EC_DABT_LOW]		= "DABT (lower EL)",
	[ESR_ELx_EC_DABT_CUR]		= "DABT (current EL)",
	[ESR_ELx_EC_SP_ALIGN]		= "SP Alignment",
	[ESR_ELx_EC_FP_EXC32]		= "FP (AArch32)",
	[ESR_ELx_EC_FP_EXC64]		= "FP (AArch64)",
	[ESR_ELx_EC_SERROR]		= "SError",
	[ESR_ELx_EC_BREAKPT_LOW]	= "Breakpoint (lower EL)",
	[ESR_ELx_EC_BREAKPT_CUR]	= "Breakpoint (current EL)",
	[ESR_ELx_EC_SOFTSTP_LOW]	= "Software Step (lower EL)",
	[ESR_ELx_EC_SOFTSTP_CUR]	= "Software Step (current EL)",
	[ESR_ELx_EC_WATCHPT_LOW]	= "Watchpoint (lower EL)",
	[ESR_ELx_EC_WATCHPT_CUR]	= "Watchpoint (current EL)",
	[ESR_ELx_EC_BKPT32]		= "BKPT (AArch32)",
	[ESR_ELx_EC_VECTOR32]		= "Vector catch (AArch32)",
	[ESR_ELx_EC_BRK64]		= "BRK (AArch64)",
};

const char *esr_get_class_string(u32 esr)
{
608
	return esr_class_str[ESR_ELx_EC(esr)];
609 610
}

C
Catalin Marinas 已提交
611
/*
612 613
 * bad_mode handles the impossible case in the exception vector. This is always
 * fatal.
C
Catalin Marinas 已提交
614 615 616 617 618
 */
asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
{
	console_verbose();

619 620 621
	pr_crit("Bad mode in %s handler detected on CPU%d, code 0x%08x -- %s\n",
		handler[reason], smp_processor_id(), esr,
		esr_get_class_string(esr));
622

623
	local_daif_mask();
624 625 626 627 628 629 630 631 632 633 634
	panic("bad mode");
}

/*
 * bad_el0_sync handles unexpected, but potentially recoverable synchronous
 * exceptions taken from EL0. Unlike bad_mode, this returns.
 */
asmlinkage void bad_el0_sync(struct pt_regs *regs, int reason, unsigned int esr)
{
	siginfo_t info;
	void __user *pc = (void __user *)instruction_pointer(regs);
635

636
	clear_siginfo(&info);
637 638 639 640
	info.si_signo = SIGILL;
	info.si_errno = 0;
	info.si_code  = ILL_ILLOPC;
	info.si_addr  = pc;
C
Catalin Marinas 已提交
641

642
	current->thread.fault_address = 0;
643
	current->thread.fault_code = esr;
644

645
	arm64_force_sig_info(&info, "Bad EL0 synchronous exception", current);
C
Catalin Marinas 已提交
646 647
}

648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
#ifdef CONFIG_VMAP_STACK

DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)], overflow_stack)
	__aligned(16);

asmlinkage void handle_bad_stack(struct pt_regs *regs)
{
	unsigned long tsk_stk = (unsigned long)current->stack;
	unsigned long irq_stk = (unsigned long)this_cpu_read(irq_stack_ptr);
	unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
	unsigned int esr = read_sysreg(esr_el1);
	unsigned long far = read_sysreg(far_el1);

	console_verbose();
	pr_emerg("Insufficient stack space to handle exception!");

	pr_emerg("ESR: 0x%08x -- %s\n", esr, esr_get_class_string(esr));
	pr_emerg("FAR: 0x%016lx\n", far);

	pr_emerg("Task stack:     [0x%016lx..0x%016lx]\n",
		 tsk_stk, tsk_stk + THREAD_SIZE);
	pr_emerg("IRQ stack:      [0x%016lx..0x%016lx]\n",
		 irq_stk, irq_stk + THREAD_SIZE);
	pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
		 ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);

	__show_regs(regs);

	/*
	 * We use nmi_panic to limit the potential for recusive overflows, and
	 * to get a better stack trace.
	 */
	nmi_panic(NULL, "kernel stack overflow");
	cpu_park_loop();
}
#endif

685
void __noreturn arm64_serror_panic(struct pt_regs *regs, u32 esr)
686 687 688 689 690
{
	console_verbose();

	pr_crit("SError Interrupt on CPU%d, code 0x%08x -- %s\n",
		smp_processor_id(), esr, esr_get_class_string(esr));
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
	if (regs)
		__show_regs(regs);

	nmi_panic(regs, "Asynchronous SError Interrupt");

	cpu_park_loop();
	unreachable();
}

bool arm64_is_fatal_ras_serror(struct pt_regs *regs, unsigned int esr)
{
	u32 aet = arm64_ras_serror_get_severity(esr);

	switch (aet) {
	case ESR_ELx_AET_CE:	/* corrected error */
	case ESR_ELx_AET_UEO:	/* restartable, not yet consumed */
		/*
		 * The CPU can make progress. We may take UEO again as
		 * a more severe error.
		 */
		return false;

	case ESR_ELx_AET_UEU:	/* Uncorrected Unrecoverable */
	case ESR_ELx_AET_UER:	/* Uncorrected Recoverable */
		/*
		 * The CPU can't make progress. The exception may have
		 * been imprecise.
718 719 720 721
		 *
		 * Neoverse-N1 #1349291 means a non-KVM SError reported as
		 * Unrecoverable should be treated as Uncontainable. We
		 * call arm64_serror_panic() in both cases.
722 723 724 725 726 727 728 729 730 731 732 733
		 */
		return true;

	case ESR_ELx_AET_UC:	/* Uncontainable or Uncategorized error */
	default:
		/* Error has been silently propagated */
		arm64_serror_panic(regs, esr);
	}
}

asmlinkage void do_serror(struct pt_regs *regs, unsigned int esr)
{
734 735 736 737
	const bool was_in_nmi = in_nmi();

	if (!was_in_nmi)
		nmi_enter();
738 739 740 741

	/* non-RAS errors are not containable */
	if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(regs, esr))
		arm64_serror_panic(regs, esr);
742

743 744
	if (!was_in_nmi)
		nmi_exit();
745 746
}

C
Catalin Marinas 已提交
747 748
void __pte_error(const char *file, int line, unsigned long val)
{
749
	pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
C
Catalin Marinas 已提交
750 751 752 753
}

void __pmd_error(const char *file, int line, unsigned long val)
{
754
	pr_err("%s:%d: bad pmd %016lx.\n", file, line, val);
C
Catalin Marinas 已提交
755 756
}

757 758
void __pud_error(const char *file, int line, unsigned long val)
{
759
	pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
760 761
}

C
Catalin Marinas 已提交
762 763
void __pgd_error(const char *file, int line, unsigned long val)
{
764
	pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
C
Catalin Marinas 已提交
765 766
}

767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
/* GENERIC_BUG traps */

int is_valid_bugaddr(unsigned long addr)
{
	/*
	 * bug_handler() only called for BRK #BUG_BRK_IMM.
	 * So the answer is trivial -- any spurious instances with no
	 * bug table entry will be rejected by report_bug() and passed
	 * back to the debug-monitors code and handled as a fatal
	 * unexpected debug exception.
	 */
	return 1;
}

static int bug_handler(struct pt_regs *regs, unsigned int esr)
{
	if (user_mode(regs))
		return DBG_HOOK_ERROR;

	switch (report_bug(regs->pc, regs)) {
	case BUG_TRAP_TYPE_BUG:
		die("Oops - BUG", regs, 0);
		break;

	case BUG_TRAP_TYPE_WARN:
		break;

	default:
		/* unknown/unrecognised bug trap type */
		return DBG_HOOK_ERROR;
	}

	/* If thread survives, skip over the BUG instruction and continue: */
800
	arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
	return DBG_HOOK_HANDLED;
}

static struct break_hook bug_break_hook = {
	.esr_val = 0xf2000000 | BUG_BRK_IMM,
	.esr_mask = 0xffffffff,
	.fn = bug_handler,
};

/*
 * Initial handler for AArch64 BRK exceptions
 * This handler only used until debug_traps_init().
 */
int __init early_brk64(unsigned long addr, unsigned int esr,
		struct pt_regs *regs)
{
	return bug_handler(regs, esr) != DBG_HOOK_HANDLED;
}

/* This registration must happen early, before debug_traps_init(). */
C
Catalin Marinas 已提交
821 822
void __init trap_init(void)
{
823
	register_break_hook(&bug_break_hook);
C
Catalin Marinas 已提交
824
}