traps.c 17.0 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 32 33 34 35
#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>
#include <linux/sched.h>
#include <linux/syscalls.h>

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

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

int show_unhandled_signals = 1;

/*
M
Mark Rutland 已提交
57
 * Dump out the contents of some kernel memory nicely...
C
Catalin Marinas 已提交
58 59
 */
static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
M
Mark Rutland 已提交
60
		     unsigned long top)
C
Catalin Marinas 已提交
61 62 63 64 65 66 67
{
	unsigned long first;
	mm_segment_t fs;
	int i;

	/*
	 * We need to switch to kernel mode so that we can use __get_user
68
	 * to safely read from kernel space.
C
Catalin Marinas 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81
	 */
	fs = get_fs();
	set_fs(KERNEL_DS);

	printk("%s%s(0x%016lx to 0x%016lx)\n", lvl, str, bottom, top);

	for (first = bottom & ~31; first < top; first += 32) {
		unsigned long p;
		char str[sizeof(" 12345678") * 8 + 1];

		memset(str, ' ', sizeof(str));
		str[sizeof(str) - 1] = '\0';

M
Mark Rutland 已提交
82 83
		for (p = first, i = 0; i < (32 / 8)
					&& p < top; i++, p += 8) {
C
Catalin Marinas 已提交
84
			if (p >= bottom && p < top) {
85 86
				unsigned long val;

M
Mark Rutland 已提交
87 88 89 90
				if (__get_user(val, (unsigned long *)p) == 0)
					sprintf(str + i * 17, " %016lx", val);
				else
					sprintf(str + i * 17, " ????????????????");
C
Catalin Marinas 已提交
91 92 93 94 95 96 97 98
			}
		}
		printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
	}

	set_fs(fs);
}

99
static void dump_backtrace_entry(unsigned long where)
C
Catalin Marinas 已提交
100
{
101 102 103
	/*
	 * Note that 'where' can have a physical address, but it's not handled.
	 */
C
Catalin Marinas 已提交
104 105 106
	print_ip_sym(where);
}

107
static void __dump_instr(const char *lvl, struct pt_regs *regs)
C
Catalin Marinas 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
{
	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;

		bad = __get_user(val, &((u32 *)addr)[i]);

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

128 129 130 131 132 133 134 135 136 137
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 已提交
138 139 140 141 142
}

static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
{
	struct stackframe frame;
Y
Yang Shi 已提交
143
	unsigned long irq_stack_ptr;
144
	int skip;
C
Catalin Marinas 已提交
145

146 147 148 149 150
	pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);

	if (!tsk)
		tsk = current;

Y
Yang Shi 已提交
151 152 153 154 155 156 157 158 159
	/*
	 * Switching between stacks is valid when tracing current and in
	 * non-preemptible context.
	 */
	if (tsk == current && !preemptible())
		irq_stack_ptr = IRQ_STACK_PTR(smp_processor_id());
	else
		irq_stack_ptr = 0;

160
	if (tsk == current) {
C
Catalin Marinas 已提交
161
		frame.fp = (unsigned long)__builtin_frame_address(0);
162
		frame.sp = current_stack_pointer;
C
Catalin Marinas 已提交
163 164 165 166 167 168 169 170 171
		frame.pc = (unsigned long)dump_backtrace;
	} else {
		/*
		 * task blocked in __switch_to
		 */
		frame.fp = thread_saved_fp(tsk);
		frame.sp = thread_saved_sp(tsk);
		frame.pc = thread_saved_pc(tsk);
	}
172 173 174
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
	frame.graph = tsk->curr_ret_stack;
#endif
C
Catalin Marinas 已提交
175

176
	skip = !!regs;
177
	printk("Call trace:\n");
C
Catalin Marinas 已提交
178 179
	while (1) {
		unsigned long where = frame.pc;
180
		unsigned long stack;
C
Catalin Marinas 已提交
181 182
		int ret;

183 184 185 186 187 188 189 190 191 192 193 194 195 196
		/* skip until specified stack frame */
		if (!skip) {
			dump_backtrace_entry(where);
		} 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);
		}
197
		ret = unwind_frame(tsk, &frame);
C
Catalin Marinas 已提交
198 199
		if (ret < 0)
			break;
200
		stack = frame.sp;
201 202 203 204 205 206 207 208 209 210 211
		if (in_exception_text(where)) {
			/*
			 * If we switched to the irq_stack before calling this
			 * exception handler, then the pt_regs will be on the
			 * task stack. The easiest way to tell is if the large
			 * pt_regs would overlap with the end of the irq_stack.
			 */
			if (stack < irq_stack_ptr &&
			    (stack + sizeof(struct pt_regs)) > irq_stack_ptr)
				stack = IRQ_STACK_TO_TASK_STACK(irq_stack_ptr);

212
			dump_mem("", "Exception stack", stack,
M
Mark Rutland 已提交
213
				 stack + sizeof(struct pt_regs));
214
		}
C
Catalin Marinas 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	}
}

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"

static int __die(const char *str, int err, struct thread_info *thread,
		 struct pt_regs *regs)
{
	struct task_struct *tsk = thread->task;
	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();
	__show_regs(regs);
	pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
		 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);

M
Mark Rutland 已提交
251
	if (!user_mode(regs)) {
C
Catalin Marinas 已提交
252
		dump_mem(KERN_EMERG, "Stack: ", regs->sp,
M
Mark Rutland 已提交
253
			 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
C
Catalin Marinas 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
		dump_backtrace(regs, tsk);
		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)
{
	struct thread_info *thread = current_thread_info();
	int ret;

	oops_enter();

	raw_spin_lock_irq(&die_lock);
	console_verbose();
	bust_spinlocks(1);
	ret = __die(str, err, thread, regs);

	if (regs && kexec_should_crash(thread->task))
		crash_kexec(regs);

	bust_spinlocks(0);
282
	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
C
Catalin Marinas 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296
	raw_spin_unlock_irq(&die_lock);
	oops_exit();

	if (in_interrupt())
		panic("Fatal exception in interrupt");
	if (panic_on_oops)
		panic("Fatal exception");
	if (ret != NOTIFY_STOP)
		do_exit(SIGSEGV);
}

void arm64_notify_die(const char *str, struct pt_regs *regs,
		      struct siginfo *info, int err)
{
297 298 299
	if (user_mode(regs)) {
		current->thread.fault_address = 0;
		current->thread.fault_code = err;
C
Catalin Marinas 已提交
300
		force_sig_info(info->si_signo, info, current);
301
	} else {
C
Catalin Marinas 已提交
302
		die(str, regs, err);
303
	}
C
Catalin Marinas 已提交
304 305
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
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);

	if (!user_mode(regs))
		return 1;

	if (compat_thumb_mode(regs)) {
		/* 16-bit Thumb instruction */
		if (get_user(instr, (u16 __user *)pc))
			goto exit;
		instr = le16_to_cpu(instr);
		if (aarch32_insn_is_wide(instr)) {
			u32 instr2;

			if (get_user(instr2, (u16 __user *)(pc + 2)))
				goto exit;
			instr2 = le16_to_cpu(instr2);
			instr = (instr << 16) | instr2;
		}
	} else {
		/* 32-bit ARM instruction */
		if (get_user(instr, (u32 __user *)pc))
			goto exit;
		instr = le32_to_cpu(instr);
	}

	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;
}

369 370
static void force_signal_inject(int signal, int code, struct pt_regs *regs,
				unsigned long address)
C
Catalin Marinas 已提交
371 372 373
{
	siginfo_t info;
	void __user *pc = (void __user *)instruction_pointer(regs);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
	const char *desc;

	switch (signal) {
	case SIGILL:
		desc = "undefined instruction";
		break;
	case SIGSEGV:
		desc = "illegal memory access";
		break;
	default:
		desc = "bad mode";
		break;
	}

	if (unhandled_signal(current, signal) &&
	    show_unhandled_signals_ratelimited()) {
		pr_info("%s[%d]: %s: pc=%p\n",
			current->comm, task_pid_nr(current), desc, pc);
		dump_instr(KERN_INFO, regs);
	}

	info.si_signo = signal;
	info.si_errno = 0;
	info.si_code  = code;
	info.si_addr  = pc;

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

/*
 * Set up process info to signal segmentation fault - called on access error.
 */
void arm64_notify_segfault(struct pt_regs *regs, unsigned long addr)
{
	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 已提交
416

417 418 419 420 421
	force_signal_inject(SIGSEGV, code, regs, addr);
}

asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
{
C
Catalin Marinas 已提交
422
	/* check for AArch32 breakpoint instructions */
423
	if (!aarch32_break_handler(regs))
C
Catalin Marinas 已提交
424 425
		return;

426 427 428
	if (call_undef_hook(regs) == 0)
		return;

429
	force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
C
Catalin Marinas 已提交
430 431
}

432
int cpu_enable_cache_maint_trap(void *__unused)
433 434
{
	config_sctlr_el1(SCTLR_EL1_UCI, 0);
435
	return 0;
436 437 438
}

#define __user_cache_maint(insn, address, res)			\
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
	if (untagged_addr(address) >= user_addr_max())		\
		res = -EFAULT;					\
	else							\
		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)				\
			: "r" (address), "i" (-EFAULT) )
454

455
static void user_cache_maint_handler(unsigned int esr, struct pt_regs *regs)
456 457
{
	unsigned long address;
458 459 460
	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;
461

462
	address = (rt == 31) ? 0 : regs->regs[rt];
463

464 465 466 467 468 469 470 471 472 473 474 475 476 477
	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;
	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:
478 479 480 481 482 483 484 485 486 487
		force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
		return;
	}

	if (ret)
		arm64_notify_segfault(regs, address);
	else
		regs->pc += 4;
}

488 489 490 491 492 493 494 495
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;

	regs->regs[rt] = arm64_ftr_reg_ctrel0.sys_val;
	regs->pc += 4;
}

496 497 498 499 500 501 502 503 504 505 506 507
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,
	},
508 509 510 511 512 513
	{
		/* 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,
	},
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
	{},
};

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;
		}

	force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
}

C
Catalin Marinas 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542
long compat_arm_syscall(struct pt_regs *regs);

asmlinkage long do_ni_syscall(struct pt_regs *regs)
{
#ifdef CONFIG_COMPAT
	long ret;
	if (is_compat_task()) {
		ret = compat_arm_syscall(regs);
		if (ret != -ENOSYS)
			return ret;
	}
#endif

543
	if (show_unhandled_signals_ratelimited()) {
C
Catalin Marinas 已提交
544 545 546 547 548 549 550 551 552 553
		pr_info("%s[%d]: syscall %d\n", current->comm,
			task_pid_nr(current), (int)regs->syscallno);
		dump_instr("", regs);
		if (user_mode(regs))
			__show_regs(regs);
	}

	return sys_ni_syscall();
}

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
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)",
	[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)
{
596
	return esr_class_str[ESR_ELx_EC(esr)];
597 598
}

C
Catalin Marinas 已提交
599 600 601 602 603
/*
 * bad_mode handles the impossible case in the exception vector.
 */
asmlinkage void bad_mode(struct pt_regs *regs, int reason, unsigned int esr)
{
604 605
	siginfo_t info;
	void __user *pc = (void __user *)instruction_pointer(regs);
C
Catalin Marinas 已提交
606 607
	console_verbose();

608 609 610
	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));
611 612 613 614 615 616
	__show_regs(regs);

	info.si_signo = SIGILL;
	info.si_errno = 0;
	info.si_code  = ILL_ILLOPC;
	info.si_addr  = pc;
C
Catalin Marinas 已提交
617

618
	arm64_notify_die("Oops - bad mode", regs, &info, 0);
C
Catalin Marinas 已提交
619 620 621 622
}

void __pte_error(const char *file, int line, unsigned long val)
{
623
	pr_err("%s:%d: bad pte %016lx.\n", file, line, val);
C
Catalin Marinas 已提交
624 625 626 627
}

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

631 632
void __pud_error(const char *file, int line, unsigned long val)
{
633
	pr_err("%s:%d: bad pud %016lx.\n", file, line, val);
634 635
}

C
Catalin Marinas 已提交
636 637
void __pgd_error(const char *file, int line, unsigned long val)
{
638
	pr_err("%s:%d: bad pgd %016lx.\n", file, line, val);
C
Catalin Marinas 已提交
639 640
}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
/* 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:
666 667
		/* Ideally, report_bug() should backtrace for us... but no. */
		dump_backtrace(regs, NULL);
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
		break;

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

	/* If thread survives, skip over the BUG instruction and continue: */
	regs->pc += AARCH64_INSN_SIZE;	/* skip BRK and resume */
	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 已提交
697 698
void __init trap_init(void)
{
699
	register_break_hook(&bug_break_hook);
C
Catalin Marinas 已提交
700
}