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