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