xref: /linux/arch/arm/kernel/traps.c (revision daec8d408308ee7322d86cdd2dc3332e9cdbedf9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/arch/arm/kernel/traps.c
4  *
5  *  Copyright (C) 1995-2009 Russell King
6  *  Fragments that appear the same as linux/arch/i386/kernel/traps.c (C) Linus Torvalds
7  *
8  *  'traps.c' handles hardware exceptions after we have saved some state in
9  *  'linux/arch/arm/lib/traps.S'.  Mostly a debugging aid, but will probably
10  *  kill the offending process.
11  */
12 #include <linux/signal.h>
13 #include <linux/personality.h>
14 #include <linux/kallsyms.h>
15 #include <linux/spinlock.h>
16 #include <linux/uaccess.h>
17 #include <linux/hardirq.h>
18 #include <linux/kdebug.h>
19 #include <linux/kprobes.h>
20 #include <linux/module.h>
21 #include <linux/kexec.h>
22 #include <linux/bug.h>
23 #include <linux/delay.h>
24 #include <linux/init.h>
25 #include <linux/sched/signal.h>
26 #include <linux/sched/debug.h>
27 #include <linux/sched/task_stack.h>
28 #include <linux/irq.h>
29 
30 #include <linux/atomic.h>
31 #include <asm/cacheflush.h>
32 #include <asm/exception.h>
33 #include <asm/unistd.h>
34 #include <asm/traps.h>
35 #include <asm/ptrace.h>
36 #include <asm/unwind.h>
37 #include <asm/tls.h>
38 #include <asm/system_misc.h>
39 #include <asm/opcodes.h>
40 
41 
42 static const char *handler[]= {
43 	"prefetch abort",
44 	"data abort",
45 	"address exception",
46 	"interrupt",
47 	"undefined instruction",
48 };
49 
50 void *vectors_page;
51 
52 #ifdef CONFIG_DEBUG_USER
53 unsigned int user_debug;
54 
55 static int __init user_debug_setup(char *str)
56 {
57 	get_option(&str, &user_debug);
58 	return 1;
59 }
60 __setup("user_debug=", user_debug_setup);
61 #endif
62 
63 static void dump_mem(const char *, const char *, unsigned long, unsigned long);
64 
65 void dump_backtrace_entry(unsigned long where, unsigned long from,
66 			  unsigned long frame, const char *loglvl)
67 {
68 	unsigned long end = frame + 4 + sizeof(struct pt_regs);
69 
70 #ifndef CONFIG_KALLSYMS
71 	printk("%sFunction entered at [<%08lx>] from [<%08lx>]\n",
72 		loglvl, where, from);
73 #elif defined CONFIG_BACKTRACE_VERBOSE
74 	printk("%s[<%08lx>] (%ps) from [<%08lx>] (%pS)\n",
75 		loglvl, where, (void *)where, from, (void *)from);
76 #else
77 	printk("%s %ps from %pS\n", loglvl, (void *)where, (void *)from);
78 #endif
79 
80 	if (in_entry_text(from) && end <= ALIGN(frame, THREAD_SIZE))
81 		dump_mem(loglvl, "Exception stack", frame + 4, end);
82 }
83 
84 void dump_backtrace_stm(u32 *stack, u32 instruction, const char *loglvl)
85 {
86 	char str[80], *p;
87 	unsigned int x;
88 	int reg;
89 
90 	for (reg = 10, x = 0, p = str; reg >= 0; reg--) {
91 		if (instruction & BIT(reg)) {
92 			p += sprintf(p, " r%d:%08x", reg, *stack--);
93 			if (++x == 6) {
94 				x = 0;
95 				p = str;
96 				printk("%s%s\n", loglvl, str);
97 			}
98 		}
99 	}
100 	if (p != str)
101 		printk("%s%s\n", loglvl, str);
102 }
103 
104 #ifndef CONFIG_ARM_UNWIND
105 /*
106  * Stack pointers should always be within the kernels view of
107  * physical memory.  If it is not there, then we can't dump
108  * out any information relating to the stack.
109  */
110 static int verify_stack(unsigned long sp)
111 {
112 	if (sp < PAGE_OFFSET ||
113 	    (sp > (unsigned long)high_memory && high_memory != NULL))
114 		return -EFAULT;
115 
116 	return 0;
117 }
118 #endif
119 
120 /*
121  * Dump out the contents of some memory nicely...
122  */
123 static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
124 		     unsigned long top)
125 {
126 	unsigned long first;
127 	int i;
128 
129 	printk("%s%s(0x%08lx to 0x%08lx)\n", lvl, str, bottom, top);
130 
131 	for (first = bottom & ~31; first < top; first += 32) {
132 		unsigned long p;
133 		char str[sizeof(" 12345678") * 8 + 1];
134 
135 		memset(str, ' ', sizeof(str));
136 		str[sizeof(str) - 1] = '\0';
137 
138 		for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
139 			if (p >= bottom && p < top) {
140 				unsigned long val;
141 				if (!get_kernel_nofault(val, (unsigned long *)p))
142 					sprintf(str + i * 9, " %08lx", val);
143 				else
144 					sprintf(str + i * 9, " ????????");
145 			}
146 		}
147 		printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
148 	}
149 }
150 
151 static void dump_instr(const char *lvl, struct pt_regs *regs)
152 {
153 	unsigned long addr = instruction_pointer(regs);
154 	const int thumb = thumb_mode(regs);
155 	const int width = thumb ? 4 : 8;
156 	char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
157 	int i;
158 
159 	/*
160 	 * Note that we now dump the code first, just in case the backtrace
161 	 * kills us.
162 	 */
163 
164 	for (i = -4; i < 1 + !!thumb; i++) {
165 		unsigned int val, bad;
166 
167 		if (!user_mode(regs)) {
168 			if (thumb) {
169 				u16 val16;
170 				bad = get_kernel_nofault(val16, &((u16 *)addr)[i]);
171 				val = val16;
172 			} else {
173 				bad = get_kernel_nofault(val, &((u32 *)addr)[i]);
174 			}
175 		} else {
176 			if (thumb)
177 				bad = get_user(val, &((u16 *)addr)[i]);
178 			else
179 				bad = get_user(val, &((u32 *)addr)[i]);
180 		}
181 
182 		if (!bad)
183 			p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ",
184 					width, val);
185 		else {
186 			p += sprintf(p, "bad PC value");
187 			break;
188 		}
189 	}
190 	printk("%sCode: %s\n", lvl, str);
191 }
192 
193 #ifdef CONFIG_ARM_UNWIND
194 static inline void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
195 				  const char *loglvl)
196 {
197 	unwind_backtrace(regs, tsk, loglvl);
198 }
199 #else
200 static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk,
201 			   const char *loglvl)
202 {
203 	unsigned int fp, mode;
204 	int ok = 1;
205 
206 	printk("%sBacktrace: ", loglvl);
207 
208 	if (!tsk)
209 		tsk = current;
210 
211 	if (regs) {
212 		fp = frame_pointer(regs);
213 		mode = processor_mode(regs);
214 	} else if (tsk != current) {
215 		fp = thread_saved_fp(tsk);
216 		mode = 0x10;
217 	} else {
218 		asm("mov %0, fp" : "=r" (fp) : : "cc");
219 		mode = 0x10;
220 	}
221 
222 	if (!fp) {
223 		pr_cont("no frame pointer");
224 		ok = 0;
225 	} else if (verify_stack(fp)) {
226 		pr_cont("invalid frame pointer 0x%08x", fp);
227 		ok = 0;
228 	} else if (fp < (unsigned long)end_of_stack(tsk))
229 		pr_cont("frame pointer underflow");
230 	pr_cont("\n");
231 
232 	if (ok)
233 		c_backtrace(fp, mode, loglvl);
234 }
235 #endif
236 
237 void show_stack(struct task_struct *tsk, unsigned long *sp, const char *loglvl)
238 {
239 	dump_backtrace(NULL, tsk, loglvl);
240 	barrier();
241 }
242 
243 #ifdef CONFIG_PREEMPT
244 #define S_PREEMPT " PREEMPT"
245 #elif defined(CONFIG_PREEMPT_RT)
246 #define S_PREEMPT " PREEMPT_RT"
247 #else
248 #define S_PREEMPT ""
249 #endif
250 #ifdef CONFIG_SMP
251 #define S_SMP " SMP"
252 #else
253 #define S_SMP ""
254 #endif
255 #ifdef CONFIG_THUMB2_KERNEL
256 #define S_ISA " THUMB2"
257 #else
258 #define S_ISA " ARM"
259 #endif
260 
261 static int __die(const char *str, int err, struct pt_regs *regs)
262 {
263 	struct task_struct *tsk = current;
264 	static int die_counter;
265 	int ret;
266 
267 	pr_emerg("Internal error: %s: %x [#%d]" S_PREEMPT S_SMP S_ISA "\n",
268 	         str, err, ++die_counter);
269 
270 	/* trap and error numbers are mostly meaningless on ARM */
271 	ret = notify_die(DIE_OOPS, str, regs, err, tsk->thread.trap_no, SIGSEGV);
272 	if (ret == NOTIFY_STOP)
273 		return 1;
274 
275 	print_modules();
276 	__show_regs(regs);
277 	__show_regs_alloc_free(regs);
278 	pr_emerg("Process %.*s (pid: %d, stack limit = 0x%p)\n",
279 		 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), end_of_stack(tsk));
280 
281 	if (!user_mode(regs) || in_interrupt()) {
282 		dump_mem(KERN_EMERG, "Stack: ", regs->ARM_sp,
283 			 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
284 		dump_backtrace(regs, tsk, KERN_EMERG);
285 		dump_instr(KERN_EMERG, regs);
286 	}
287 
288 	return 0;
289 }
290 
291 static arch_spinlock_t die_lock = __ARCH_SPIN_LOCK_UNLOCKED;
292 static int die_owner = -1;
293 static unsigned int die_nest_count;
294 
295 static unsigned long oops_begin(void)
296 {
297 	int cpu;
298 	unsigned long flags;
299 
300 	oops_enter();
301 
302 	/* racy, but better than risking deadlock. */
303 	raw_local_irq_save(flags);
304 	cpu = smp_processor_id();
305 	if (!arch_spin_trylock(&die_lock)) {
306 		if (cpu == die_owner)
307 			/* nested oops. should stop eventually */;
308 		else
309 			arch_spin_lock(&die_lock);
310 	}
311 	die_nest_count++;
312 	die_owner = cpu;
313 	console_verbose();
314 	bust_spinlocks(1);
315 	return flags;
316 }
317 
318 static void oops_end(unsigned long flags, struct pt_regs *regs, int signr)
319 {
320 	if (regs && kexec_should_crash(current))
321 		crash_kexec(regs);
322 
323 	bust_spinlocks(0);
324 	die_owner = -1;
325 	add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
326 	die_nest_count--;
327 	if (!die_nest_count)
328 		/* Nest count reaches zero, release the lock. */
329 		arch_spin_unlock(&die_lock);
330 	raw_local_irq_restore(flags);
331 	oops_exit();
332 
333 	if (in_interrupt())
334 		panic("Fatal exception in interrupt");
335 	if (panic_on_oops)
336 		panic("Fatal exception");
337 	if (signr)
338 		make_task_dead(signr);
339 }
340 
341 /*
342  * This function is protected against re-entrancy.
343  */
344 void die(const char *str, struct pt_regs *regs, int err)
345 {
346 	enum bug_trap_type bug_type = BUG_TRAP_TYPE_NONE;
347 	unsigned long flags = oops_begin();
348 	int sig = SIGSEGV;
349 
350 	if (!user_mode(regs))
351 		bug_type = report_bug(regs->ARM_pc, regs);
352 	if (bug_type != BUG_TRAP_TYPE_NONE)
353 		str = "Oops - BUG";
354 
355 	if (__die(str, err, regs))
356 		sig = 0;
357 
358 	oops_end(flags, regs, sig);
359 }
360 
361 void arm_notify_die(const char *str, struct pt_regs *regs,
362 		int signo, int si_code, void __user *addr,
363 		unsigned long err, unsigned long trap)
364 {
365 	if (user_mode(regs)) {
366 		current->thread.error_code = err;
367 		current->thread.trap_no = trap;
368 
369 		force_sig_fault(signo, si_code, addr);
370 	} else {
371 		die(str, regs, err);
372 	}
373 }
374 
375 #ifdef CONFIG_GENERIC_BUG
376 
377 int is_valid_bugaddr(unsigned long pc)
378 {
379 #ifdef CONFIG_THUMB2_KERNEL
380 	u16 bkpt;
381 	u16 insn = __opcode_to_mem_thumb16(BUG_INSTR_VALUE);
382 #else
383 	u32 bkpt;
384 	u32 insn = __opcode_to_mem_arm(BUG_INSTR_VALUE);
385 #endif
386 
387 	if (get_kernel_nofault(bkpt, (void *)pc))
388 		return 0;
389 
390 	return bkpt == insn;
391 }
392 
393 #endif
394 
395 static LIST_HEAD(undef_hook);
396 static DEFINE_RAW_SPINLOCK(undef_lock);
397 
398 void register_undef_hook(struct undef_hook *hook)
399 {
400 	unsigned long flags;
401 
402 	raw_spin_lock_irqsave(&undef_lock, flags);
403 	list_add(&hook->node, &undef_hook);
404 	raw_spin_unlock_irqrestore(&undef_lock, flags);
405 }
406 
407 void unregister_undef_hook(struct undef_hook *hook)
408 {
409 	unsigned long flags;
410 
411 	raw_spin_lock_irqsave(&undef_lock, flags);
412 	list_del(&hook->node);
413 	raw_spin_unlock_irqrestore(&undef_lock, flags);
414 }
415 
416 static nokprobe_inline
417 int call_undef_hook(struct pt_regs *regs, unsigned int instr)
418 {
419 	struct undef_hook *hook;
420 	unsigned long flags;
421 	int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL;
422 
423 	raw_spin_lock_irqsave(&undef_lock, flags);
424 	list_for_each_entry(hook, &undef_hook, node)
425 		if ((instr & hook->instr_mask) == hook->instr_val &&
426 		    (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val)
427 			fn = hook->fn;
428 	raw_spin_unlock_irqrestore(&undef_lock, flags);
429 
430 	return fn ? fn(regs, instr) : 1;
431 }
432 
433 asmlinkage void do_undefinstr(struct pt_regs *regs)
434 {
435 	unsigned int instr;
436 	void __user *pc;
437 
438 	pc = (void __user *)instruction_pointer(regs);
439 
440 	if (processor_mode(regs) == SVC_MODE) {
441 #ifdef CONFIG_THUMB2_KERNEL
442 		if (thumb_mode(regs)) {
443 			instr = __mem_to_opcode_thumb16(((u16 *)pc)[0]);
444 			if (is_wide_instruction(instr)) {
445 				u16 inst2;
446 				inst2 = __mem_to_opcode_thumb16(((u16 *)pc)[1]);
447 				instr = __opcode_thumb32_compose(instr, inst2);
448 			}
449 		} else
450 #endif
451 			instr = __mem_to_opcode_arm(*(u32 *) pc);
452 	} else if (thumb_mode(regs)) {
453 		if (get_user(instr, (u16 __user *)pc))
454 			goto die_sig;
455 		instr = __mem_to_opcode_thumb16(instr);
456 		if (is_wide_instruction(instr)) {
457 			unsigned int instr2;
458 			if (get_user(instr2, (u16 __user *)pc+1))
459 				goto die_sig;
460 			instr2 = __mem_to_opcode_thumb16(instr2);
461 			instr = __opcode_thumb32_compose(instr, instr2);
462 		}
463 	} else {
464 		if (get_user(instr, (u32 __user *)pc))
465 			goto die_sig;
466 		instr = __mem_to_opcode_arm(instr);
467 	}
468 
469 	if (call_undef_hook(regs, instr) == 0)
470 		return;
471 
472 die_sig:
473 #ifdef CONFIG_DEBUG_USER
474 	if (user_debug & UDBG_UNDEFINED) {
475 		pr_info("%s (%d): undefined instruction: pc=%p\n",
476 			current->comm, task_pid_nr(current), pc);
477 		__show_regs(regs);
478 		dump_instr(KERN_INFO, regs);
479 	}
480 #endif
481 	arm_notify_die("Oops - undefined instruction", regs,
482 		       SIGILL, ILL_ILLOPC, pc, 0, 6);
483 }
484 NOKPROBE_SYMBOL(do_undefinstr)
485 
486 /*
487  * Handle FIQ similarly to NMI on x86 systems.
488  *
489  * The runtime environment for NMIs is extremely restrictive
490  * (NMIs can pre-empt critical sections meaning almost all locking is
491  * forbidden) meaning this default FIQ handling must only be used in
492  * circumstances where non-maskability improves robustness, such as
493  * watchdog or debug logic.
494  *
495  * This handler is not appropriate for general purpose use in drivers
496  * platform code and can be overrideen using set_fiq_handler.
497  */
498 asmlinkage void __exception_irq_entry handle_fiq_as_nmi(struct pt_regs *regs)
499 {
500 	struct pt_regs *old_regs = set_irq_regs(regs);
501 
502 	nmi_enter();
503 
504 	/* nop. FIQ handlers for special arch/arm features can be added here. */
505 
506 	nmi_exit();
507 
508 	set_irq_regs(old_regs);
509 }
510 
511 /*
512  * bad_mode handles the impossible case in the vectors.  If you see one of
513  * these, then it's extremely serious, and could mean you have buggy hardware.
514  * It never returns, and never tries to sync.  We hope that we can at least
515  * dump out some state information...
516  */
517 asmlinkage void bad_mode(struct pt_regs *regs, int reason)
518 {
519 	console_verbose();
520 
521 	pr_crit("Bad mode in %s handler detected\n", handler[reason]);
522 
523 	die("Oops - bad mode", regs, 0);
524 	local_irq_disable();
525 	panic("bad mode");
526 }
527 
528 static int bad_syscall(int n, struct pt_regs *regs)
529 {
530 	if ((current->personality & PER_MASK) != PER_LINUX) {
531 		send_sig(SIGSEGV, current, 1);
532 		return regs->ARM_r0;
533 	}
534 
535 #ifdef CONFIG_DEBUG_USER
536 	if (user_debug & UDBG_SYSCALL) {
537 		pr_err("[%d] %s: obsolete system call %08x.\n",
538 			task_pid_nr(current), current->comm, n);
539 		dump_instr(KERN_ERR, regs);
540 	}
541 #endif
542 
543 	arm_notify_die("Oops - bad syscall", regs, SIGILL, ILL_ILLTRP,
544 		       (void __user *)instruction_pointer(regs) -
545 			 (thumb_mode(regs) ? 2 : 4),
546 		       n, 0);
547 
548 	return regs->ARM_r0;
549 }
550 
551 static inline int
552 __do_cache_op(unsigned long start, unsigned long end)
553 {
554 	int ret;
555 
556 	do {
557 		unsigned long chunk = min(PAGE_SIZE, end - start);
558 
559 		if (fatal_signal_pending(current))
560 			return 0;
561 
562 		ret = flush_icache_user_range(start, start + chunk);
563 		if (ret)
564 			return ret;
565 
566 		cond_resched();
567 		start += chunk;
568 	} while (start < end);
569 
570 	return 0;
571 }
572 
573 static inline int
574 do_cache_op(unsigned long start, unsigned long end, int flags)
575 {
576 	if (end < start || flags)
577 		return -EINVAL;
578 
579 	if (!access_ok(start, end - start))
580 		return -EFAULT;
581 
582 	return __do_cache_op(start, end);
583 }
584 
585 /*
586  * Handle all unrecognised system calls.
587  *  0x9f0000 - 0x9fffff are some more esoteric system calls
588  */
589 #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
590 asmlinkage int arm_syscall(int no, struct pt_regs *regs)
591 {
592 	if ((no >> 16) != (__ARM_NR_BASE>> 16))
593 		return bad_syscall(no, regs);
594 
595 	switch (no & 0xffff) {
596 	case 0: /* branch through 0 */
597 		arm_notify_die("branch through zero", regs,
598 			       SIGSEGV, SEGV_MAPERR, NULL, 0, 0);
599 		return 0;
600 
601 	case NR(breakpoint): /* SWI BREAK_POINT */
602 		regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
603 		ptrace_break(regs);
604 		return regs->ARM_r0;
605 
606 	/*
607 	 * Flush a region from virtual address 'r0' to virtual address 'r1'
608 	 * _exclusive_.  There is no alignment requirement on either address;
609 	 * user space does not need to know the hardware cache layout.
610 	 *
611 	 * r2 contains flags.  It should ALWAYS be passed as ZERO until it
612 	 * is defined to be something else.  For now we ignore it, but may
613 	 * the fires of hell burn in your belly if you break this rule. ;)
614 	 *
615 	 * (at a later date, we may want to allow this call to not flush
616 	 * various aspects of the cache.  Passing '0' will guarantee that
617 	 * everything necessary gets flushed to maintain consistency in
618 	 * the specified region).
619 	 */
620 	case NR(cacheflush):
621 		return do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
622 
623 	case NR(usr26):
624 		if (!(elf_hwcap & HWCAP_26BIT))
625 			break;
626 		regs->ARM_cpsr &= ~MODE32_BIT;
627 		return regs->ARM_r0;
628 
629 	case NR(usr32):
630 		if (!(elf_hwcap & HWCAP_26BIT))
631 			break;
632 		regs->ARM_cpsr |= MODE32_BIT;
633 		return regs->ARM_r0;
634 
635 	case NR(set_tls):
636 		set_tls(regs->ARM_r0);
637 		return 0;
638 
639 	case NR(get_tls):
640 		return current_thread_info()->tp_value[0];
641 
642 	default:
643 		/* Calls 9f00xx..9f07ff are defined to return -ENOSYS
644 		   if not implemented, rather than raising SIGILL.  This
645 		   way the calling program can gracefully determine whether
646 		   a feature is supported.  */
647 		if ((no & 0xffff) <= 0x7ff)
648 			return -ENOSYS;
649 		break;
650 	}
651 #ifdef CONFIG_DEBUG_USER
652 	/*
653 	 * experience shows that these seem to indicate that
654 	 * something catastrophic has happened
655 	 */
656 	if (user_debug & UDBG_SYSCALL) {
657 		pr_err("[%d] %s: arm syscall %d\n",
658 		       task_pid_nr(current), current->comm, no);
659 		dump_instr(KERN_ERR, regs);
660 		if (user_mode(regs)) {
661 			__show_regs(regs);
662 			c_backtrace(frame_pointer(regs), processor_mode(regs), KERN_ERR);
663 		}
664 	}
665 #endif
666 	arm_notify_die("Oops - bad syscall(2)", regs, SIGILL, ILL_ILLTRP,
667 		       (void __user *)instruction_pointer(regs) -
668 			 (thumb_mode(regs) ? 2 : 4),
669 		       no, 0);
670 	return 0;
671 }
672 
673 #ifdef CONFIG_TLS_REG_EMUL
674 
675 /*
676  * We might be running on an ARMv6+ processor which should have the TLS
677  * register but for some reason we can't use it, or maybe an SMP system
678  * using a pre-ARMv6 processor (there are apparently a few prototypes like
679  * that in existence) and therefore access to that register must be
680  * emulated.
681  */
682 
683 static int get_tp_trap(struct pt_regs *regs, unsigned int instr)
684 {
685 	int reg = (instr >> 12) & 15;
686 	if (reg == 15)
687 		return 1;
688 	regs->uregs[reg] = current_thread_info()->tp_value[0];
689 	regs->ARM_pc += 4;
690 	return 0;
691 }
692 
693 static struct undef_hook arm_mrc_hook = {
694 	.instr_mask	= 0x0fff0fff,
695 	.instr_val	= 0x0e1d0f70,
696 	.cpsr_mask	= PSR_T_BIT,
697 	.cpsr_val	= 0,
698 	.fn		= get_tp_trap,
699 };
700 
701 static int __init arm_mrc_hook_init(void)
702 {
703 	register_undef_hook(&arm_mrc_hook);
704 	return 0;
705 }
706 
707 late_initcall(arm_mrc_hook_init);
708 
709 #endif
710 
711 /*
712  * A data abort trap was taken, but we did not handle the instruction.
713  * Try to abort the user program, or panic if it was the kernel.
714  */
715 asmlinkage void
716 baddataabort(int code, unsigned long instr, struct pt_regs *regs)
717 {
718 	unsigned long addr = instruction_pointer(regs);
719 
720 #ifdef CONFIG_DEBUG_USER
721 	if (user_debug & UDBG_BADABORT) {
722 		pr_err("8<--- cut here ---\n");
723 		pr_err("[%d] %s: bad data abort: code %d instr 0x%08lx\n",
724 		       task_pid_nr(current), current->comm, code, instr);
725 		dump_instr(KERN_ERR, regs);
726 		show_pte(KERN_ERR, current->mm, addr);
727 	}
728 #endif
729 
730 	arm_notify_die("unknown data abort code", regs,
731 		       SIGILL, ILL_ILLOPC, (void __user *)addr, instr, 0);
732 }
733 
734 void __readwrite_bug(const char *fn)
735 {
736 	pr_err("%s called, but not implemented\n", fn);
737 	BUG();
738 }
739 EXPORT_SYMBOL(__readwrite_bug);
740 
741 void __pte_error(const char *file, int line, pte_t pte)
742 {
743 	pr_err("%s:%d: bad pte %08llx.\n", file, line, (long long)pte_val(pte));
744 }
745 
746 void __pmd_error(const char *file, int line, pmd_t pmd)
747 {
748 	pr_err("%s:%d: bad pmd %08llx.\n", file, line, (long long)pmd_val(pmd));
749 }
750 
751 void __pgd_error(const char *file, int line, pgd_t pgd)
752 {
753 	pr_err("%s:%d: bad pgd %08llx.\n", file, line, (long long)pgd_val(pgd));
754 }
755 
756 asmlinkage void __div0(void)
757 {
758 	pr_err("Division by zero in kernel.\n");
759 	dump_stack();
760 }
761 EXPORT_SYMBOL(__div0);
762 
763 void abort(void)
764 {
765 	BUG();
766 
767 	/* if that doesn't kill us, halt */
768 	panic("Oops failed to kill thread");
769 }
770 
771 #ifdef CONFIG_KUSER_HELPERS
772 static void __init kuser_init(void *vectors)
773 {
774 	extern char __kuser_helper_start[], __kuser_helper_end[];
775 	int kuser_sz = __kuser_helper_end - __kuser_helper_start;
776 
777 	memcpy(vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);
778 
779 	/*
780 	 * vectors + 0xfe0 = __kuser_get_tls
781 	 * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8
782 	 */
783 	if (tls_emu || has_tls_reg)
784 		memcpy(vectors + 0xfe0, vectors + 0xfe8, 4);
785 }
786 #else
787 static inline void __init kuser_init(void *vectors)
788 {
789 }
790 #endif
791 
792 void __init early_trap_init(void *vectors_base)
793 {
794 #ifndef CONFIG_CPU_V7M
795 	unsigned long vectors = (unsigned long)vectors_base;
796 	extern char __stubs_start[], __stubs_end[];
797 	extern char __vectors_start[], __vectors_end[];
798 	unsigned i;
799 
800 	vectors_page = vectors_base;
801 
802 	/*
803 	 * Poison the vectors page with an undefined instruction.  This
804 	 * instruction is chosen to be undefined for both ARM and Thumb
805 	 * ISAs.  The Thumb version is an undefined instruction with a
806 	 * branch back to the undefined instruction.
807 	 */
808 	for (i = 0; i < PAGE_SIZE / sizeof(u32); i++)
809 		((u32 *)vectors_base)[i] = 0xe7fddef1;
810 
811 	/*
812 	 * Copy the vectors, stubs and kuser helpers (in entry-armv.S)
813 	 * into the vector page, mapped at 0xffff0000, and ensure these
814 	 * are visible to the instruction stream.
815 	 */
816 	memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
817 	memcpy((void *)vectors + 0x1000, __stubs_start, __stubs_end - __stubs_start);
818 
819 	kuser_init(vectors_base);
820 
821 	flush_icache_range(vectors, vectors + PAGE_SIZE * 2);
822 #else /* ifndef CONFIG_CPU_V7M */
823 	/*
824 	 * on V7-M there is no need to copy the vector table to a dedicated
825 	 * memory area. The address is configurable and so a table in the kernel
826 	 * image can be used.
827 	 */
828 #endif
829 }
830