blob: b63bc998eb7a44807ffe259898d1b85caea2d70f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/kernel/traps.c
3 *
Russell Kingab72b002009-10-25 15:39:37 +00004 * Copyright (C) 1995-2009 Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/personality.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kallsyms.h>
Russell Kinga9221de2010-01-20 17:02:54 +000018#include <linux/spinlock.h>
Russell King33fa9b12008-09-06 11:35:55 +010019#include <linux/uaccess.h>
Russell Kinga9221de2010-01-20 17:02:54 +000020#include <linux/hardirq.h>
21#include <linux/kdebug.h>
22#include <linux/module.h>
23#include <linux/kexec.h>
Simon Glass87e040b2011-08-16 23:44:26 +010024#include <linux/bug.h>
Russell Kinga9221de2010-01-20 17:02:54 +000025#include <linux/delay.h>
26#include <linux/init.h>
Will Deacon425fc472011-02-14 14:31:09 +010027#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Arun Sharma60063492011-07-26 16:09:06 -070029#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/cacheflush.h>
Jamie Iles5a567d72011-10-08 11:20:42 +010031#include <asm/exception.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/unistd.h>
33#include <asm/traps.h>
Catalin Marinasbff595c2009-02-16 11:41:36 +010034#include <asm/unwind.h>
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010035#include <asm/tls.h>
David Howells9f97da72012-03-28 18:30:01 +010036#include <asm/system_misc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Russell Kinge00d3492005-06-22 20:26:05 +010038#include "signal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Pushkar Joshida3ee182012-09-07 14:22:58 -070040#include <trace/events/exception.h>
41
Russell King10252aa2014-01-03 15:01:39 +000042static const char *handler[]= {
43 "prefetch abort",
44 "data abort",
45 "address exception",
46 "interrupt",
47 "undefined instruction",
48};
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jaeseong GIMb8499412012-06-26 10:36:57 -070050#ifdef CONFIG_LGE_CRASH_HANDLER
51static int first_call_chain = 0;
52static int first_die = 1;
53#endif
Catalin Marinas247055a2010-09-13 16:03:21 +010054void *vectors_page;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#ifdef CONFIG_DEBUG_USER
57unsigned int user_debug;
58
59static int __init user_debug_setup(char *str)
60{
61 get_option(&str, &user_debug);
62 return 1;
63}
64__setup("user_debug=", user_debug_setup);
65#endif
66
Russell Kinge40c2ec2009-10-11 15:17:53 +010067static void dump_mem(const char *, const char *, unsigned long, unsigned long);
Russell King7ab3f8d2007-03-02 15:01:36 +000068
Russell King7ab3f8d2007-03-02 15:01:36 +000069void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
71#ifdef CONFIG_KALLSYMS
Jaeseong GIMb8499412012-06-26 10:36:57 -070072#ifdef CONFIG_LGE_CRASH_HANDLER
73 if (first_call_chain)
74 set_crash_store_enable();
75#endif
Joe Perches69448c22010-11-05 16:12:34 -070076 printk("[<%08lx>] (%pS) from [<%08lx>] (%pS)\n", where, (void *)where, from, (void *)from);
Jaeseong GIMb8499412012-06-26 10:36:57 -070077#ifdef CONFIG_LGE_CRASH_HANDLER
78 set_crash_store_disable();
79#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#else
81 printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
82#endif
Russell King7ab3f8d2007-03-02 15:01:36 +000083
84 if (in_exception_text(where))
Russell Kinge40c2ec2009-10-11 15:17:53 +010085 dump_mem("", "Exception stack", frame + 4, frame + 4 + sizeof(struct pt_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Catalin Marinasbff595c2009-02-16 11:41:36 +010088#ifndef CONFIG_ARM_UNWIND
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * Stack pointers should always be within the kernels view of
91 * physical memory. If it is not there, then we can't dump
92 * out any information relating to the stack.
93 */
94static int verify_stack(unsigned long sp)
95{
Russell King09d9bae2008-09-05 14:08:44 +010096 if (sp < PAGE_OFFSET ||
97 (sp > (unsigned long)high_memory && high_memory != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return -EFAULT;
99
100 return 0;
101}
Catalin Marinasbff595c2009-02-16 11:41:36 +0100102#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104/*
105 * Dump out the contents of some memory nicely...
106 */
Russell Kinge40c2ec2009-10-11 15:17:53 +0100107static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
108 unsigned long top)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Russell Kingd191fe02009-10-11 15:03:11 +0100110 unsigned long first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 mm_segment_t fs;
112 int i;
113
114 /*
115 * We need to switch to kernel mode so that we can use __get_user
116 * to safely read from kernel space. Note that we now dump the
117 * code first, just in case the backtrace kills us.
118 */
119 fs = get_fs();
120 set_fs(KERNEL_DS);
121
Russell Kinge40c2ec2009-10-11 15:17:53 +0100122 printk("%s%s(0x%08lx to 0x%08lx)\n", lvl, str, bottom, top);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Russell Kingd191fe02009-10-11 15:03:11 +0100124 for (first = bottom & ~31; first < top; first += 32) {
125 unsigned long p;
126 char str[sizeof(" 12345678") * 8 + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Russell Kingd191fe02009-10-11 15:03:11 +0100128 memset(str, ' ', sizeof(str));
129 str[sizeof(str) - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Russell Kingd191fe02009-10-11 15:03:11 +0100131 for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
132 if (p >= bottom && p < top) {
133 unsigned long val;
134 if (__get_user(val, (unsigned long *)p) == 0)
135 sprintf(str + i * 9, " %08lx", val);
136 else
137 sprintf(str + i * 9, " ????????");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139 }
Russell Kinge40c2ec2009-10-11 15:17:53 +0100140 printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
143 set_fs(fs);
144}
145
Russell Kinge40c2ec2009-10-11 15:17:53 +0100146static void dump_instr(const char *lvl, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 unsigned long addr = instruction_pointer(regs);
149 const int thumb = thumb_mode(regs);
150 const int width = thumb ? 4 : 8;
151 mm_segment_t fs;
Russell Kingd191fe02009-10-11 15:03:11 +0100152 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int i;
154
155 /*
156 * We need to switch to kernel mode so that we can use __get_user
157 * to safely read from kernel space. Note that we now dump the
158 * code first, just in case the backtrace kills us.
159 */
160 fs = get_fs();
161 set_fs(KERNEL_DS);
162
Russell Kinga9011582011-06-09 23:21:11 +0100163 for (i = -4; i < 1 + !!thumb; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned int val, bad;
165
166 if (thumb)
167 bad = __get_user(val, &((u16 *)addr)[i]);
168 else
169 bad = __get_user(val, &((u32 *)addr)[i]);
170
171 if (!bad)
Russell Kingd191fe02009-10-11 15:03:11 +0100172 p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ",
173 width, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 else {
Russell Kingd191fe02009-10-11 15:03:11 +0100175 p += sprintf(p, "bad PC value");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177 }
178 }
Russell Kinge40c2ec2009-10-11 15:17:53 +0100179 printk("%sCode: %s\n", lvl, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 set_fs(fs);
182}
183
Catalin Marinasbff595c2009-02-16 11:41:36 +0100184#ifdef CONFIG_ARM_UNWIND
185static inline void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
186{
187 unwind_backtrace(regs, tsk);
188}
189#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
191{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100192 unsigned int fp, mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 int ok = 1;
194
195 printk("Backtrace: ");
Catalin Marinas67a94c22009-02-11 13:06:53 +0100196
197 if (!tsk)
198 tsk = current;
199
200 if (regs) {
201 fp = regs->ARM_fp;
202 mode = processor_mode(regs);
203 } else if (tsk != current) {
204 fp = thread_saved_fp(tsk);
205 mode = 0x10;
206 } else {
207 asm("mov %0, fp" : "=r" (fp) : : "cc");
208 mode = 0x10;
209 }
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (!fp) {
212 printk("no frame pointer");
213 ok = 0;
214 } else if (verify_stack(fp)) {
215 printk("invalid frame pointer 0x%08x", fp);
216 ok = 0;
Al Viro55205822006-01-12 01:05:57 -0800217 } else if (fp < (unsigned long)end_of_stack(tsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 printk("frame pointer underflow");
219 printk("\n");
220
221 if (ok)
Catalin Marinas67a94c22009-02-11 13:06:53 +0100222 c_backtrace(fp, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
Catalin Marinasbff595c2009-02-16 11:41:36 +0100224#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226void dump_stack(void)
227{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100228 dump_backtrace(NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231EXPORT_SYMBOL(dump_stack);
232
233void show_stack(struct task_struct *tsk, unsigned long *sp)
234{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100235 dump_backtrace(NULL, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 barrier();
237}
238
Russell Kingd9202422007-06-17 13:38:27 +0100239#ifdef CONFIG_PREEMPT
240#define S_PREEMPT " PREEMPT"
241#else
242#define S_PREEMPT ""
243#endif
244#ifdef CONFIG_SMP
245#define S_SMP " SMP"
246#else
247#define S_SMP ""
248#endif
Russell King8211ca62012-02-04 12:12:11 +0000249#ifdef CONFIG_THUMB2_KERNEL
250#define S_ISA " THUMB2"
251#else
252#define S_ISA " ARM"
253#endif
Russell Kingd9202422007-06-17 13:38:27 +0100254
Russell Kinga9221de2010-01-20 17:02:54 +0000255static int __die(const char *str, int err, struct thread_info *thread, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Russell Kingd3629792005-10-30 19:01:43 +0000257 struct task_struct *tsk = thread->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 static int die_counter;
Russell Kinga9221de2010-01-20 17:02:54 +0000259 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Jaeseong GIMb8499412012-06-26 10:36:57 -0700261#ifdef CONFIG_LGE_CRASH_HANDLER
262 if (first_die) {
263 first_call_chain = 1;
264 first_die = 0;
265 }
266 set_kernel_crash_magic_number();
267 set_crash_store_enable();
268#endif
269
Russell King8211ca62012-02-04 12:12:11 +0000270 printk(KERN_EMERG "Internal error: %s: %x [#%d]" S_PREEMPT S_SMP
271 S_ISA "\n", str, err, ++die_counter);
Russell Kinga9221de2010-01-20 17:02:54 +0000272
Jaeseong GIMb8499412012-06-26 10:36:57 -0700273#ifdef CONFIG_LGE_CRASH_HANDLER
274 set_crash_store_disable();
275#endif
276
Russell Kinga9221de2010-01-20 17:02:54 +0000277 /* trap and error numbers are mostly meaningless on ARM */
278 ret = notify_die(DIE_OOPS, str, regs, err, tsk->thread.trap_no, SIGSEGV);
279 if (ret == NOTIFY_STOP)
280 return ret;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 print_modules();
Russell King652a12e2005-04-17 15:50:36 +0100283 __show_regs(regs);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100284 printk(KERN_EMERG "Process %.*s (pid: %d, stack limit = 0x%p)\n",
285 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 if (!user_mode(regs) || in_interrupt()) {
Russell Kinge40c2ec2009-10-11 15:17:53 +0100288 dump_mem(KERN_EMERG, "Stack: ", regs->ARM_sp,
Al Viro32d39a92006-01-12 01:05:58 -0800289 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 dump_backtrace(regs, tsk);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100291 dump_instr(KERN_EMERG, regs);
Jaeseong GIMb8499412012-06-26 10:36:57 -0700292#ifdef CONFIG_LGE_CRASH_HANDLER
293 first_call_chain = 0;
294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Russell Kinga9221de2010-01-20 17:02:54 +0000296
297 return ret;
Russell Kingd3629792005-10-30 19:01:43 +0000298}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500300static DEFINE_RAW_SPINLOCK(die_lock);
Russell Kingd3629792005-10-30 19:01:43 +0000301
302/*
303 * This function is protected against re-entrancy.
304 */
Russell Kinga9221de2010-01-20 17:02:54 +0000305void die(const char *str, struct pt_regs *regs, int err)
Russell Kingd3629792005-10-30 19:01:43 +0000306{
307 struct thread_info *thread = current_thread_info();
Russell Kinga9221de2010-01-20 17:02:54 +0000308 int ret;
Stephen Boydbdf800c2012-02-07 19:42:33 +0100309 enum bug_trap_type bug_type = BUG_TRAP_TYPE_NONE;
Russell Kingd3629792005-10-30 19:01:43 +0000310
Russell Kingd9202422007-06-17 13:38:27 +0100311 oops_enter();
312
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500313 raw_spin_lock_irq(&die_lock);
Russell King03a6e5b2009-10-11 15:25:05 +0100314 console_verbose();
Russell Kingd3629792005-10-30 19:01:43 +0000315 bust_spinlocks(1);
Simon Glass87e040b2011-08-16 23:44:26 +0100316 if (!user_mode(regs))
Stephen Boydbdf800c2012-02-07 19:42:33 +0100317 bug_type = report_bug(regs->ARM_pc, regs);
318 if (bug_type != BUG_TRAP_TYPE_NONE)
319 str = "Oops - BUG";
Russell Kinga9221de2010-01-20 17:02:54 +0000320 ret = __die(str, err, thread, regs);
321
322 if (regs && kexec_should_crash(thread->task))
323 crash_kexec(regs);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700326 add_taint(TAINT_DIE);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500327 raw_spin_unlock_irq(&die_lock);
Russell King03a6e5b2009-10-11 15:25:05 +0100328 oops_exit();
Russell King31867492006-02-19 19:53:56 +0000329
Russell Kingd9202422007-06-17 13:38:27 +0100330 if (in_interrupt())
331 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700332 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700333 panic("Fatal exception");
Russell Kinga9221de2010-01-20 17:02:54 +0000334 if (ret != NOTIFY_STOP)
335 do_exit(SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700338void arm_notify_die(const char *str, struct pt_regs *regs,
339 struct siginfo *info, unsigned long err, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 if (user_mode(regs)) {
342 current->thread.error_code = err;
343 current->thread.trap_no = trap;
344
345 force_sig_info(info->si_signo, info, current);
346 } else {
347 die(str, regs, err);
348 }
349}
350
Simon Glass87e040b2011-08-16 23:44:26 +0100351#ifdef CONFIG_GENERIC_BUG
352
353int is_valid_bugaddr(unsigned long pc)
354{
355#ifdef CONFIG_THUMB2_KERNEL
356 unsigned short bkpt;
357#else
358 unsigned long bkpt;
359#endif
360
361 if (probe_kernel_address((unsigned *)pc, bkpt))
362 return 0;
363
364 return bkpt == BUG_INSTR_VALUE;
365}
366
367#endif
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static LIST_HEAD(undef_hook);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500370static DEFINE_RAW_SPINLOCK(undef_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372void register_undef_hook(struct undef_hook *hook)
373{
Russell King109d89c2005-07-16 16:43:33 +0100374 unsigned long flags;
375
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500376 raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 list_add(&hook->node, &undef_hook);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500378 raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381void unregister_undef_hook(struct undef_hook *hook)
382{
Russell King109d89c2005-07-16 16:43:33 +0100383 unsigned long flags;
384
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500385 raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 list_del(&hook->node);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500387 raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Russell Kingb03a5b72008-08-11 12:27:16 +0100390static int call_undef_hook(struct pt_regs *regs, unsigned int instr)
391{
392 struct undef_hook *hook;
393 unsigned long flags;
394 int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL;
395
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500396 raw_spin_lock_irqsave(&undef_lock, flags);
Russell Kingb03a5b72008-08-11 12:27:16 +0100397 list_for_each_entry(hook, &undef_hook, node)
398 if ((instr & hook->instr_mask) == hook->instr_val &&
399 (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val)
400 fn = hook->fn;
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500401 raw_spin_unlock_irqrestore(&undef_lock, flags);
Russell Kingb03a5b72008-08-11 12:27:16 +0100402
403 return fn ? fn(regs, instr) : 1;
404}
405
Russell King7ab3f8d2007-03-02 15:01:36 +0000406asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 unsigned int instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 siginfo_t info;
410 void __user *pc;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 pc = (void __user *)instruction_pointer(regs);
Dan Williamsdfc544c2007-02-13 17:11:34 +0100413
414 if (processor_mode(regs) == SVC_MODE) {
Jon Medhurst592201a2011-03-26 19:19:07 +0000415#ifdef CONFIG_THUMB2_KERNEL
416 if (thumb_mode(regs)) {
417 instr = ((u16 *)pc)[0];
418 if (is_wide_instruction(instr)) {
419 instr <<= 16;
420 instr |= ((u16 *)pc)[1];
421 }
422 } else
423#endif
424 instr = *(u32 *) pc;
Dan Williamsdfc544c2007-02-13 17:11:34 +0100425 } else if (thumb_mode(regs)) {
Will Deaconeff605b2012-09-07 18:21:44 +0100426 if (get_user(instr, (u16 __user *)pc))
427 goto die_sig;
Jon Medhurst592201a2011-03-26 19:19:07 +0000428 if (is_wide_instruction(instr)) {
429 unsigned int instr2;
Will Deaconeff605b2012-09-07 18:21:44 +0100430 if (get_user(instr2, (u16 __user *)pc+1))
431 goto die_sig;
Jon Medhurst592201a2011-03-26 19:19:07 +0000432 instr <<= 16;
433 instr |= instr2;
434 }
Will Deaconeff605b2012-09-07 18:21:44 +0100435 } else if (get_user(instr, (u32 __user *)pc)) {
436 goto die_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
438
Russell Kingb03a5b72008-08-11 12:27:16 +0100439 if (call_undef_hook(regs, instr) == 0)
440 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Will Deaconeff605b2012-09-07 18:21:44 +0100442die_sig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443#ifdef CONFIG_DEBUG_USER
444 if (user_debug & UDBG_UNDEFINED) {
445 printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700446 current->comm, task_pid_nr(current), pc);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100447 dump_instr(KERN_INFO, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449#endif
450
451 info.si_signo = SIGILL;
452 info.si_errno = 0;
453 info.si_code = ILL_ILLOPC;
454 info.si_addr = pc;
455
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700456 arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459asmlinkage void do_unexp_fiq (struct pt_regs *regs)
460{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 printk("Hmm. Unexpected FIQ received, but trying to continue\n");
462 printk("You may have a hardware problem...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
465/*
466 * bad_mode handles the impossible case in the vectors. If you see one of
467 * these, then it's extremely serious, and could mean you have buggy hardware.
468 * It never returns, and never tries to sync. We hope that we can at least
469 * dump out some state information...
470 */
Russell Kingae0a8462007-01-09 12:57:37 +0000471asmlinkage void bad_mode(struct pt_regs *regs, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 console_verbose();
474
Russell Kingae0a8462007-01-09 12:57:37 +0000475 printk(KERN_CRIT "Bad mode in %s handler detected\n", handler[reason]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 die("Oops - bad mode", regs, 0);
478 local_irq_disable();
479 panic("bad mode");
480}
481
482static int bad_syscall(int n, struct pt_regs *regs)
483{
484 struct thread_info *thread = current_thread_info();
485 siginfo_t info;
486
Nicolas Pitre88b9ef42011-04-13 05:01:52 +0100487 if ((current->personality & PER_MASK) != PER_LINUX &&
Nicolas Pitrea999cb042005-10-28 16:35:46 +0100488 thread->exec_domain->handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 thread->exec_domain->handler(n, regs);
490 return regs->ARM_r0;
491 }
492
493#ifdef CONFIG_DEBUG_USER
494 if (user_debug & UDBG_SYSCALL) {
495 printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700496 task_pid_nr(current), current->comm, n);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100497 dump_instr(KERN_ERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499#endif
500
501 info.si_signo = SIGILL;
502 info.si_errno = 0;
503 info.si_code = ILL_ILLTRP;
504 info.si_addr = (void __user *)instruction_pointer(regs) -
505 (thumb_mode(regs) ? 2 : 4);
506
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700507 arm_notify_die("Oops - bad syscall", regs, &info, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 return regs->ARM_r0;
510}
511
512static inline void
513do_cache_op(unsigned long start, unsigned long end, int flags)
514{
Russell Kingaa45ee82009-09-28 11:41:51 +0100515 struct mm_struct *mm = current->active_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct vm_area_struct *vma;
517
518 if (end < start || flags)
519 return;
520
Russell Kingaa45ee82009-09-28 11:41:51 +0100521 down_read(&mm->mmap_sem);
522 vma = find_vma(mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (vma && vma->vm_start < end) {
524 if (start < vma->vm_start)
525 start = vma->vm_start;
526 if (end > vma->vm_end)
527 end = vma->vm_end;
528
Dima Zavin96714b52010-04-27 20:57:04 -0700529 up_read(&mm->mmap_sem);
530 flush_cache_user_range(start, end);
531 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Russell Kingaa45ee82009-09-28 11:41:51 +0100533 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/*
537 * Handle all unrecognised system calls.
538 * 0x9f0000 - 0x9fffff are some more esoteric system calls
539 */
540#define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
541asmlinkage int arm_syscall(int no, struct pt_regs *regs)
542{
543 struct thread_info *thread = current_thread_info();
544 siginfo_t info;
545
Nicolas Pitre3f2829a2006-01-14 16:31:29 +0000546 if ((no >> 16) != (__ARM_NR_BASE>> 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return bad_syscall(no, regs);
548
549 switch (no & 0xffff) {
550 case 0: /* branch through 0 */
551 info.si_signo = SIGSEGV;
552 info.si_errno = 0;
553 info.si_code = SEGV_MAPERR;
554 info.si_addr = NULL;
555
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700556 arm_notify_die("branch through zero", regs, &info, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return 0;
558
559 case NR(breakpoint): /* SWI BREAK_POINT */
560 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
561 ptrace_break(current, regs);
562 return regs->ARM_r0;
563
564 /*
565 * Flush a region from virtual address 'r0' to virtual address 'r1'
566 * _exclusive_. There is no alignment requirement on either address;
567 * user space does not need to know the hardware cache layout.
568 *
569 * r2 contains flags. It should ALWAYS be passed as ZERO until it
570 * is defined to be something else. For now we ignore it, but may
571 * the fires of hell burn in your belly if you break this rule. ;)
572 *
573 * (at a later date, we may want to allow this call to not flush
574 * various aspects of the cache. Passing '0' will guarantee that
575 * everything necessary gets flushed to maintain consistency in
576 * the specified region).
577 */
578 case NR(cacheflush):
579 do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
580 return 0;
581
582 case NR(usr26):
583 if (!(elf_hwcap & HWCAP_26BIT))
584 break;
585 regs->ARM_cpsr &= ~MODE32_BIT;
586 return regs->ARM_r0;
587
588 case NR(usr32):
589 if (!(elf_hwcap & HWCAP_26BIT))
590 break;
591 regs->ARM_cpsr |= MODE32_BIT;
592 return regs->ARM_r0;
593
594 case NR(set_tls):
André Hentschelb71c7c22013-06-18 23:23:26 +0100595 thread->tp_value[0] = regs->ARM_r0;
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100596 if (tls_emu)
597 return 0;
598 if (has_tls_reg) {
599 asm ("mcr p15, 0, %0, c13, c0, 3"
600 : : "r" (regs->ARM_r0));
601 } else {
602 /*
603 * User space must never try to access this directly.
604 * Expect your app to break eventually if you do so.
605 * The user helper at 0xffff0fe0 must be used instead.
606 * (see entry-armv.S for details)
607 */
608 *((unsigned int *)0xffff0ff0) = regs->ARM_r0;
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return 0;
611
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100612#ifdef CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG
613 /*
614 * Atomically store r1 in *r2 if *r2 is equal to r0 for user space.
615 * Return zero in r0 if *MEM was changed or non-zero if no exchange
616 * happened. Also set the user C flag accordingly.
617 * If access permissions have to be fixed up then non-zero is
618 * returned and the operation has to be re-attempted.
619 *
620 * *NOTE*: This is a ghost syscall private to the kernel. Only the
621 * __kuser_cmpxchg code in entry-armv.S should be aware of its
622 * existence. Don't ever use this from user code.
623 */
Russell Kingcc20d422009-11-09 23:53:29 +0000624 case NR(cmpxchg):
Nicolas Pitreb49c0f22007-11-20 17:20:29 +0100625 for (;;) {
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100626 extern void do_DataAbort(unsigned long addr, unsigned int fsr,
627 struct pt_regs *regs);
628 unsigned long val;
629 unsigned long addr = regs->ARM_r2;
630 struct mm_struct *mm = current->mm;
631 pgd_t *pgd; pmd_t *pmd; pte_t *pte;
Hugh Dickins69b04752005-10-29 18:16:36 -0700632 spinlock_t *ptl;
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100633
634 regs->ARM_cpsr &= ~PSR_C_BIT;
Hugh Dickins69b04752005-10-29 18:16:36 -0700635 down_read(&mm->mmap_sem);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100636 pgd = pgd_offset(mm, addr);
637 if (!pgd_present(*pgd))
638 goto bad_access;
639 pmd = pmd_offset(pgd, addr);
640 if (!pmd_present(*pmd))
641 goto bad_access;
Hugh Dickins69b04752005-10-29 18:16:36 -0700642 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
Po-Yu Chuang373ce302011-06-09 08:42:17 +0100643 if (!pte_present(*pte) || !pte_write(*pte) || !pte_dirty(*pte)) {
Hugh Dickins69b04752005-10-29 18:16:36 -0700644 pte_unmap_unlock(pte, ptl);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100645 goto bad_access;
Hugh Dickins69b04752005-10-29 18:16:36 -0700646 }
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100647 val = *(unsigned long *)addr;
648 val -= regs->ARM_r0;
649 if (val == 0) {
650 *(unsigned long *)addr = regs->ARM_r1;
651 regs->ARM_cpsr |= PSR_C_BIT;
652 }
Hugh Dickins69b04752005-10-29 18:16:36 -0700653 pte_unmap_unlock(pte, ptl);
654 up_read(&mm->mmap_sem);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100655 return val;
656
657 bad_access:
Hugh Dickins69b04752005-10-29 18:16:36 -0700658 up_read(&mm->mmap_sem);
Nicolas Pitre74f88492005-10-04 23:17:52 +0100659 /* simulate a write access fault */
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100660 do_DataAbort(addr, 15 + (1 << 11), regs);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100661 }
662#endif
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 default:
665 /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
666 if not implemented, rather than raising SIGILL. This
667 way the calling program can gracefully determine whether
668 a feature is supported. */
Russell Kingbfd2e292009-11-08 20:05:28 +0000669 if ((no & 0xffff) <= 0x7ff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return -ENOSYS;
671 break;
672 }
673#ifdef CONFIG_DEBUG_USER
674 /*
675 * experience shows that these seem to indicate that
676 * something catastrophic has happened
677 */
678 if (user_debug & UDBG_SYSCALL) {
679 printk("[%d] %s: arm syscall %d\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700680 task_pid_nr(current), current->comm, no);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100681 dump_instr("", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (user_mode(regs)) {
Russell King652a12e2005-04-17 15:50:36 +0100683 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 c_backtrace(regs->ARM_fp, processor_mode(regs));
685 }
686 }
687#endif
688 info.si_signo = SIGILL;
689 info.si_errno = 0;
690 info.si_code = ILL_ILLTRP;
691 info.si_addr = (void __user *)instruction_pointer(regs) -
692 (thumb_mode(regs) ? 2 : 4);
693
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700694 arm_notify_die("Oops - bad syscall(2)", regs, &info, no, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return 0;
696}
697
Nicolas Pitre4b0e07a2005-05-05 23:24:45 +0100698#ifdef CONFIG_TLS_REG_EMUL
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100699
700/*
701 * We might be running on an ARMv6+ processor which should have the TLS
Nicolas Pitre4b0e07a2005-05-05 23:24:45 +0100702 * register but for some reason we can't use it, or maybe an SMP system
703 * using a pre-ARMv6 processor (there are apparently a few prototypes like
704 * that in existence) and therefore access to that register must be
705 * emulated.
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100706 */
707
708static int get_tp_trap(struct pt_regs *regs, unsigned int instr)
709{
710 int reg = (instr >> 12) & 15;
711 if (reg == 15)
712 return 1;
André Hentschelb71c7c22013-06-18 23:23:26 +0100713 regs->uregs[reg] = current_thread_info()->tp_value[0];
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100714 regs->ARM_pc += 4;
715 return 0;
716}
717
718static struct undef_hook arm_mrc_hook = {
719 .instr_mask = 0x0fff0fff,
720 .instr_val = 0x0e1d0f70,
721 .cpsr_mask = PSR_T_BIT,
722 .cpsr_val = 0,
723 .fn = get_tp_trap,
724};
725
726static int __init arm_mrc_hook_init(void)
727{
728 register_undef_hook(&arm_mrc_hook);
729 return 0;
730}
731
732late_initcall(arm_mrc_hook_init);
733
734#endif
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736void __bad_xchg(volatile void *ptr, int size)
737{
738 printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
739 __builtin_return_address(0), ptr, size);
740 BUG();
741}
742EXPORT_SYMBOL(__bad_xchg);
743
744/*
745 * A data abort trap was taken, but we did not handle the instruction.
746 * Try to abort the user program, or panic if it was the kernel.
747 */
748asmlinkage void
749baddataabort(int code, unsigned long instr, struct pt_regs *regs)
750{
751 unsigned long addr = instruction_pointer(regs);
752 siginfo_t info;
753
754#ifdef CONFIG_DEBUG_USER
755 if (user_debug & UDBG_BADABORT) {
756 printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700757 task_pid_nr(current), current->comm, code, instr);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100758 dump_instr(KERN_ERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 show_pte(current->mm, addr);
760 }
761#endif
762
763 info.si_signo = SIGILL;
764 info.si_errno = 0;
765 info.si_code = ILL_ILLOPC;
766 info.si_addr = (void __user *)addr;
767
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700768 arm_notify_die("unknown data abort code", regs, &info, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771void __readwrite_bug(const char *fn)
772{
773 printk("%s called, but not implemented\n", fn);
774 BUG();
775}
776EXPORT_SYMBOL(__readwrite_bug);
777
Russell King69529c02010-11-16 00:19:55 +0000778void __pte_error(const char *file, int line, pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Will Deacon29a38192011-02-15 14:31:37 +0100780 printk("%s:%d: bad pte %08llx.\n", file, line, (long long)pte_val(pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Russell King69529c02010-11-16 00:19:55 +0000783void __pmd_error(const char *file, int line, pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Will Deacon29a38192011-02-15 14:31:37 +0100785 printk("%s:%d: bad pmd %08llx.\n", file, line, (long long)pmd_val(pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
Russell King69529c02010-11-16 00:19:55 +0000788void __pgd_error(const char *file, int line, pgd_t pgd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Will Deacon29a38192011-02-15 14:31:37 +0100790 printk("%s:%d: bad pgd %08llx.\n", file, line, (long long)pgd_val(pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793asmlinkage void __div0(void)
794{
795 printk("Division by zero in kernel.\n");
796 dump_stack();
797}
798EXPORT_SYMBOL(__div0);
799
800void abort(void)
801{
802 BUG();
803
804 /* if that doesn't kill us, halt */
805 panic("Oops failed to kill thread");
806}
807EXPORT_SYMBOL(abort);
808
809void __init trap_init(void)
810{
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600811 return;
812}
813
Russell King8c45daa2013-07-23 18:37:00 +0100814#ifdef CONFIG_KUSER_HELPERS
815static void __init kuser_init(void *vectors)
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100816{
Russell King8c45daa2013-07-23 18:37:00 +0100817 extern char __kuser_helper_start[], __kuser_helper_end[];
818 int kuser_sz = __kuser_helper_end - __kuser_helper_start;
819
820 memcpy(vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);
821
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100822 /*
823 * vectors + 0xfe0 = __kuser_get_tls
824 * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8
825 */
826 if (tls_emu || has_tls_reg)
Russell King8c45daa2013-07-23 18:37:00 +0100827 memcpy(vectors + 0xfe0, vectors + 0xfe8, 4);
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100828}
Russell King8c45daa2013-07-23 18:37:00 +0100829#else
830static void __init kuser_init(void *vectors)
831{
832}
833#endif
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100834
Russell King94e5a852012-01-18 15:32:49 +0000835void __init early_trap_init(void *vectors_base)
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600836{
Russell King94e5a852012-01-18 15:32:49 +0000837 unsigned long vectors = (unsigned long)vectors_base;
Russell King79335232005-04-26 15:17:42 +0100838 extern char __stubs_start[], __stubs_end[];
839 extern char __vectors_start[], __vectors_end[];
Russell Kinga87ce9b2013-07-04 11:00:23 +0100840 unsigned i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Russell King94e5a852012-01-18 15:32:49 +0000842 vectors_page = vectors_base;
843
Russell King79335232005-04-26 15:17:42 +0100844 /*
Russell Kinga87ce9b2013-07-04 11:00:23 +0100845 * Poison the vectors page with an undefined instruction. This
846 * instruction is chosen to be undefined for both ARM and Thumb
847 * ISAs. The Thumb version is an undefined instruction with a
848 * branch back to the undefined instruction.
849 */
850 for (i = 0; i < PAGE_SIZE / sizeof(u32); i++)
851 ((u32 *)vectors_base)[i] = 0xe7fddef1;
852
853 /*
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100854 * Copy the vectors, stubs and kuser helpers (in entry-armv.S)
855 * into the vector page, mapped at 0xffff0000, and ensure these
856 * are visible to the instruction stream.
Russell King79335232005-04-26 15:17:42 +0100857 */
Hyok S. Choic760fc12006-03-27 15:18:50 +0100858 memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
Russell King47dcfc22013-07-04 11:40:32 +0100859 memcpy((void *)vectors + 0x1000, __stubs_start, __stubs_end - __stubs_start);
Russell Kinge00d3492005-06-22 20:26:05 +0100860
Russell King8c45daa2013-07-23 18:37:00 +0100861 kuser_init(vectors_base);
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100862
Russell King47dcfc22013-07-04 11:40:32 +0100863 flush_icache_range(vectors, vectors + PAGE_SIZE * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
865}