blob: 87abca018054c07e5a1e23c722077887ca996ade [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>
24#include <linux/delay.h>
25#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <asm/atomic.h>
28#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/unistd.h>
31#include <asm/traps.h>
Catalin Marinasbff595c2009-02-16 11:41:36 +010032#include <asm/unwind.h>
Tony Lindgrenf159f4e2010-07-05 14:53:10 +010033#include <asm/tls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include "ptrace.h"
Russell Kinge00d3492005-06-22 20:26:05 +010036#include "signal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static const char *handler[]= { "prefetch abort", "data abort", "address exception", "interrupt" };
39
Catalin Marinas247055a2010-09-13 16:03:21 +010040void *vectors_page;
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#ifdef CONFIG_DEBUG_USER
43unsigned int user_debug;
44
45static int __init user_debug_setup(char *str)
46{
47 get_option(&str, &user_debug);
48 return 1;
49}
50__setup("user_debug=", user_debug_setup);
51#endif
52
Russell Kinge40c2ec2009-10-11 15:17:53 +010053static void dump_mem(const char *, const char *, unsigned long, unsigned long);
Russell King7ab3f8d2007-03-02 15:01:36 +000054
Russell King7ab3f8d2007-03-02 15:01:36 +000055void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57#ifdef CONFIG_KALLSYMS
Russell Kingd191fe02009-10-11 15:03:11 +010058 char sym1[KSYM_SYMBOL_LEN], sym2[KSYM_SYMBOL_LEN];
59 sprint_symbol(sym1, where);
60 sprint_symbol(sym2, from);
61 printk("[<%08lx>] (%s) from [<%08lx>] (%s)\n", where, sym1, from, sym2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#else
63 printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
64#endif
Russell King7ab3f8d2007-03-02 15:01:36 +000065
66 if (in_exception_text(where))
Russell Kinge40c2ec2009-10-11 15:17:53 +010067 dump_mem("", "Exception stack", frame + 4, frame + 4 + sizeof(struct pt_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Catalin Marinasbff595c2009-02-16 11:41:36 +010070#ifndef CONFIG_ARM_UNWIND
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * Stack pointers should always be within the kernels view of
73 * physical memory. If it is not there, then we can't dump
74 * out any information relating to the stack.
75 */
76static int verify_stack(unsigned long sp)
77{
Russell King09d9bae2008-09-05 14:08:44 +010078 if (sp < PAGE_OFFSET ||
79 (sp > (unsigned long)high_memory && high_memory != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EFAULT;
81
82 return 0;
83}
Catalin Marinasbff595c2009-02-16 11:41:36 +010084#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/*
87 * Dump out the contents of some memory nicely...
88 */
Russell Kinge40c2ec2009-10-11 15:17:53 +010089static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
90 unsigned long top)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Russell Kingd191fe02009-10-11 15:03:11 +010092 unsigned long first;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 mm_segment_t fs;
94 int i;
95
96 /*
97 * We need to switch to kernel mode so that we can use __get_user
98 * to safely read from kernel space. Note that we now dump the
99 * code first, just in case the backtrace kills us.
100 */
101 fs = get_fs();
102 set_fs(KERNEL_DS);
103
Russell Kinge40c2ec2009-10-11 15:17:53 +0100104 printk("%s%s(0x%08lx to 0x%08lx)\n", lvl, str, bottom, top);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Russell Kingd191fe02009-10-11 15:03:11 +0100106 for (first = bottom & ~31; first < top; first += 32) {
107 unsigned long p;
108 char str[sizeof(" 12345678") * 8 + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Russell Kingd191fe02009-10-11 15:03:11 +0100110 memset(str, ' ', sizeof(str));
111 str[sizeof(str) - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Russell Kingd191fe02009-10-11 15:03:11 +0100113 for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
114 if (p >= bottom && p < top) {
115 unsigned long val;
116 if (__get_user(val, (unsigned long *)p) == 0)
117 sprintf(str + i * 9, " %08lx", val);
118 else
119 sprintf(str + i * 9, " ????????");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 }
Russell Kinge40c2ec2009-10-11 15:17:53 +0100122 printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
125 set_fs(fs);
126}
127
Russell Kinge40c2ec2009-10-11 15:17:53 +0100128static void dump_instr(const char *lvl, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 unsigned long addr = instruction_pointer(regs);
131 const int thumb = thumb_mode(regs);
132 const int width = thumb ? 4 : 8;
133 mm_segment_t fs;
Russell Kingd191fe02009-10-11 15:03:11 +0100134 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int i;
136
137 /*
138 * We need to switch to kernel mode so that we can use __get_user
139 * to safely read from kernel space. Note that we now dump the
140 * code first, just in case the backtrace kills us.
141 */
142 fs = get_fs();
143 set_fs(KERNEL_DS);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 for (i = -4; i < 1; i++) {
146 unsigned int val, bad;
147
148 if (thumb)
149 bad = __get_user(val, &((u16 *)addr)[i]);
150 else
151 bad = __get_user(val, &((u32 *)addr)[i]);
152
153 if (!bad)
Russell Kingd191fe02009-10-11 15:03:11 +0100154 p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ",
155 width, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 else {
Russell Kingd191fe02009-10-11 15:03:11 +0100157 p += sprintf(p, "bad PC value");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159 }
160 }
Russell Kinge40c2ec2009-10-11 15:17:53 +0100161 printk("%sCode: %s\n", lvl, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 set_fs(fs);
164}
165
Catalin Marinasbff595c2009-02-16 11:41:36 +0100166#ifdef CONFIG_ARM_UNWIND
167static inline void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
168{
169 unwind_backtrace(regs, tsk);
170}
171#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
173{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100174 unsigned int fp, mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 int ok = 1;
176
177 printk("Backtrace: ");
Catalin Marinas67a94c22009-02-11 13:06:53 +0100178
179 if (!tsk)
180 tsk = current;
181
182 if (regs) {
183 fp = regs->ARM_fp;
184 mode = processor_mode(regs);
185 } else if (tsk != current) {
186 fp = thread_saved_fp(tsk);
187 mode = 0x10;
188 } else {
189 asm("mov %0, fp" : "=r" (fp) : : "cc");
190 mode = 0x10;
191 }
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!fp) {
194 printk("no frame pointer");
195 ok = 0;
196 } else if (verify_stack(fp)) {
197 printk("invalid frame pointer 0x%08x", fp);
198 ok = 0;
Al Viro55205822006-01-12 01:05:57 -0800199 } else if (fp < (unsigned long)end_of_stack(tsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 printk("frame pointer underflow");
201 printk("\n");
202
203 if (ok)
Catalin Marinas67a94c22009-02-11 13:06:53 +0100204 c_backtrace(fp, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
Catalin Marinasbff595c2009-02-16 11:41:36 +0100206#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208void dump_stack(void)
209{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100210 dump_backtrace(NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
213EXPORT_SYMBOL(dump_stack);
214
215void show_stack(struct task_struct *tsk, unsigned long *sp)
216{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100217 dump_backtrace(NULL, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 barrier();
219}
220
Russell Kingd9202422007-06-17 13:38:27 +0100221#ifdef CONFIG_PREEMPT
222#define S_PREEMPT " PREEMPT"
223#else
224#define S_PREEMPT ""
225#endif
226#ifdef CONFIG_SMP
227#define S_SMP " SMP"
228#else
229#define S_SMP ""
230#endif
231
Russell Kinga9221de2010-01-20 17:02:54 +0000232static int __die(const char *str, int err, struct thread_info *thread, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Russell Kingd3629792005-10-30 19:01:43 +0000234 struct task_struct *tsk = thread->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 static int die_counter;
Russell Kinga9221de2010-01-20 17:02:54 +0000236 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Russell Kinge40c2ec2009-10-11 15:17:53 +0100238 printk(KERN_EMERG "Internal error: %s: %x [#%d]" S_PREEMPT S_SMP "\n",
Russell Kingd9202422007-06-17 13:38:27 +0100239 str, err, ++die_counter);
Russell King03a6e5b2009-10-11 15:25:05 +0100240 sysfs_printk_last_file();
Russell Kinga9221de2010-01-20 17:02:54 +0000241
242 /* trap and error numbers are mostly meaningless on ARM */
243 ret = notify_die(DIE_OOPS, str, regs, err, tsk->thread.trap_no, SIGSEGV);
244 if (ret == NOTIFY_STOP)
245 return ret;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 print_modules();
Russell King652a12e2005-04-17 15:50:36 +0100248 __show_regs(regs);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100249 printk(KERN_EMERG "Process %.*s (pid: %d, stack limit = 0x%p)\n",
250 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (!user_mode(regs) || in_interrupt()) {
Russell Kinge40c2ec2009-10-11 15:17:53 +0100253 dump_mem(KERN_EMERG, "Stack: ", regs->ARM_sp,
Al Viro32d39a92006-01-12 01:05:58 -0800254 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 dump_backtrace(regs, tsk);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100256 dump_instr(KERN_EMERG, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Russell Kinga9221de2010-01-20 17:02:54 +0000258
259 return ret;
Russell Kingd3629792005-10-30 19:01:43 +0000260}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Russell Kingd3629792005-10-30 19:01:43 +0000262DEFINE_SPINLOCK(die_lock);
263
264/*
265 * This function is protected against re-entrancy.
266 */
Russell Kinga9221de2010-01-20 17:02:54 +0000267void die(const char *str, struct pt_regs *regs, int err)
Russell Kingd3629792005-10-30 19:01:43 +0000268{
269 struct thread_info *thread = current_thread_info();
Russell Kinga9221de2010-01-20 17:02:54 +0000270 int ret;
Russell Kingd3629792005-10-30 19:01:43 +0000271
Russell Kingd9202422007-06-17 13:38:27 +0100272 oops_enter();
273
Russell Kingd3629792005-10-30 19:01:43 +0000274 spin_lock_irq(&die_lock);
Russell King03a6e5b2009-10-11 15:25:05 +0100275 console_verbose();
Russell Kingd3629792005-10-30 19:01:43 +0000276 bust_spinlocks(1);
Russell Kinga9221de2010-01-20 17:02:54 +0000277 ret = __die(str, err, thread, regs);
278
279 if (regs && kexec_should_crash(thread->task))
280 crash_kexec(regs);
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700283 add_taint(TAINT_DIE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 spin_unlock_irq(&die_lock);
Russell King03a6e5b2009-10-11 15:25:05 +0100285 oops_exit();
Russell King31867492006-02-19 19:53:56 +0000286
Russell Kingd9202422007-06-17 13:38:27 +0100287 if (in_interrupt())
288 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700289 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700290 panic("Fatal exception");
Russell Kinga9221de2010-01-20 17:02:54 +0000291 if (ret != NOTIFY_STOP)
292 do_exit(SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700295void arm_notify_die(const char *str, struct pt_regs *regs,
296 struct siginfo *info, unsigned long err, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 if (user_mode(regs)) {
299 current->thread.error_code = err;
300 current->thread.trap_no = trap;
301
302 force_sig_info(info->si_signo, info, current);
303 } else {
304 die(str, regs, err);
305 }
306}
307
308static LIST_HEAD(undef_hook);
309static DEFINE_SPINLOCK(undef_lock);
310
311void register_undef_hook(struct undef_hook *hook)
312{
Russell King109d89c2005-07-16 16:43:33 +0100313 unsigned long flags;
314
315 spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 list_add(&hook->node, &undef_hook);
Russell King109d89c2005-07-16 16:43:33 +0100317 spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320void unregister_undef_hook(struct undef_hook *hook)
321{
Russell King109d89c2005-07-16 16:43:33 +0100322 unsigned long flags;
323
324 spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 list_del(&hook->node);
Russell King109d89c2005-07-16 16:43:33 +0100326 spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Russell Kingb03a5b72008-08-11 12:27:16 +0100329static int call_undef_hook(struct pt_regs *regs, unsigned int instr)
330{
331 struct undef_hook *hook;
332 unsigned long flags;
333 int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL;
334
335 spin_lock_irqsave(&undef_lock, flags);
336 list_for_each_entry(hook, &undef_hook, node)
337 if ((instr & hook->instr_mask) == hook->instr_val &&
338 (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val)
339 fn = hook->fn;
340 spin_unlock_irqrestore(&undef_lock, flags);
341
342 return fn ? fn(regs, instr) : 1;
343}
344
Russell King7ab3f8d2007-03-02 15:01:36 +0000345asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 unsigned int correction = thumb_mode(regs) ? 2 : 4;
348 unsigned int instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 siginfo_t info;
350 void __user *pc;
351
352 /*
353 * According to the ARM ARM, PC is 2 or 4 bytes ahead,
354 * depending whether we're in Thumb mode or not.
355 * Correct this offset.
356 */
357 regs->ARM_pc -= correction;
358
359 pc = (void __user *)instruction_pointer(regs);
Dan Williamsdfc544c2007-02-13 17:11:34 +0100360
361 if (processor_mode(regs) == SVC_MODE) {
362 instr = *(u32 *) pc;
363 } else if (thumb_mode(regs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 get_user(instr, (u16 __user *)pc);
365 } else {
366 get_user(instr, (u32 __user *)pc);
367 }
368
Russell Kingb03a5b72008-08-11 12:27:16 +0100369 if (call_undef_hook(regs, instr) == 0)
370 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372#ifdef CONFIG_DEBUG_USER
373 if (user_debug & UDBG_UNDEFINED) {
374 printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700375 current->comm, task_pid_nr(current), pc);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100376 dump_instr(KERN_INFO, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
378#endif
379
380 info.si_signo = SIGILL;
381 info.si_errno = 0;
382 info.si_code = ILL_ILLOPC;
383 info.si_addr = pc;
384
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700385 arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388asmlinkage void do_unexp_fiq (struct pt_regs *regs)
389{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 printk("Hmm. Unexpected FIQ received, but trying to continue\n");
391 printk("You may have a hardware problem...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
395 * bad_mode handles the impossible case in the vectors. If you see one of
396 * these, then it's extremely serious, and could mean you have buggy hardware.
397 * It never returns, and never tries to sync. We hope that we can at least
398 * dump out some state information...
399 */
Russell Kingae0a8462007-01-09 12:57:37 +0000400asmlinkage void bad_mode(struct pt_regs *regs, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 console_verbose();
403
Russell Kingae0a8462007-01-09 12:57:37 +0000404 printk(KERN_CRIT "Bad mode in %s handler detected\n", handler[reason]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 die("Oops - bad mode", regs, 0);
407 local_irq_disable();
408 panic("bad mode");
409}
410
411static int bad_syscall(int n, struct pt_regs *regs)
412{
413 struct thread_info *thread = current_thread_info();
414 siginfo_t info;
415
Nicolas Pitrea999cb02005-10-28 16:35:46 +0100416 if (current->personality != PER_LINUX &&
417 current->personality != PER_LINUX_32BIT &&
418 thread->exec_domain->handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 thread->exec_domain->handler(n, regs);
420 return regs->ARM_r0;
421 }
422
423#ifdef CONFIG_DEBUG_USER
424 if (user_debug & UDBG_SYSCALL) {
425 printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700426 task_pid_nr(current), current->comm, n);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100427 dump_instr(KERN_ERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429#endif
430
431 info.si_signo = SIGILL;
432 info.si_errno = 0;
433 info.si_code = ILL_ILLTRP;
434 info.si_addr = (void __user *)instruction_pointer(regs) -
435 (thumb_mode(regs) ? 2 : 4);
436
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700437 arm_notify_die("Oops - bad syscall", regs, &info, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 return regs->ARM_r0;
440}
441
442static inline void
443do_cache_op(unsigned long start, unsigned long end, int flags)
444{
Russell Kingaa45ee8f2009-09-28 11:41:51 +0100445 struct mm_struct *mm = current->active_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct vm_area_struct *vma;
447
448 if (end < start || flags)
449 return;
450
Russell Kingaa45ee8f2009-09-28 11:41:51 +0100451 down_read(&mm->mmap_sem);
452 vma = find_vma(mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (vma && vma->vm_start < end) {
454 if (start < vma->vm_start)
455 start = vma->vm_start;
456 if (end > vma->vm_end)
457 end = vma->vm_end;
458
459 flush_cache_user_range(vma, start, end);
460 }
Russell Kingaa45ee8f2009-09-28 11:41:51 +0100461 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464/*
465 * Handle all unrecognised system calls.
466 * 0x9f0000 - 0x9fffff are some more esoteric system calls
467 */
468#define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
469asmlinkage int arm_syscall(int no, struct pt_regs *regs)
470{
471 struct thread_info *thread = current_thread_info();
472 siginfo_t info;
473
Nicolas Pitre3f2829a2006-01-14 16:31:29 +0000474 if ((no >> 16) != (__ARM_NR_BASE>> 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return bad_syscall(no, regs);
476
477 switch (no & 0xffff) {
478 case 0: /* branch through 0 */
479 info.si_signo = SIGSEGV;
480 info.si_errno = 0;
481 info.si_code = SEGV_MAPERR;
482 info.si_addr = NULL;
483
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700484 arm_notify_die("branch through zero", regs, &info, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
486
487 case NR(breakpoint): /* SWI BREAK_POINT */
488 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
489 ptrace_break(current, regs);
490 return regs->ARM_r0;
491
492 /*
493 * Flush a region from virtual address 'r0' to virtual address 'r1'
494 * _exclusive_. There is no alignment requirement on either address;
495 * user space does not need to know the hardware cache layout.
496 *
497 * r2 contains flags. It should ALWAYS be passed as ZERO until it
498 * is defined to be something else. For now we ignore it, but may
499 * the fires of hell burn in your belly if you break this rule. ;)
500 *
501 * (at a later date, we may want to allow this call to not flush
502 * various aspects of the cache. Passing '0' will guarantee that
503 * everything necessary gets flushed to maintain consistency in
504 * the specified region).
505 */
506 case NR(cacheflush):
507 do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
508 return 0;
509
510 case NR(usr26):
511 if (!(elf_hwcap & HWCAP_26BIT))
512 break;
513 regs->ARM_cpsr &= ~MODE32_BIT;
514 return regs->ARM_r0;
515
516 case NR(usr32):
517 if (!(elf_hwcap & HWCAP_26BIT))
518 break;
519 regs->ARM_cpsr |= MODE32_BIT;
520 return regs->ARM_r0;
521
522 case NR(set_tls):
523 thread->tp_value = regs->ARM_r0;
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100524 if (tls_emu)
525 return 0;
526 if (has_tls_reg) {
527 asm ("mcr p15, 0, %0, c13, c0, 3"
528 : : "r" (regs->ARM_r0));
529 } else {
530 /*
531 * User space must never try to access this directly.
532 * Expect your app to break eventually if you do so.
533 * The user helper at 0xffff0fe0 must be used instead.
534 * (see entry-armv.S for details)
535 */
536 *((unsigned int *)0xffff0ff0) = regs->ARM_r0;
537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return 0;
539
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100540#ifdef CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG
541 /*
542 * Atomically store r1 in *r2 if *r2 is equal to r0 for user space.
543 * Return zero in r0 if *MEM was changed or non-zero if no exchange
544 * happened. Also set the user C flag accordingly.
545 * If access permissions have to be fixed up then non-zero is
546 * returned and the operation has to be re-attempted.
547 *
548 * *NOTE*: This is a ghost syscall private to the kernel. Only the
549 * __kuser_cmpxchg code in entry-armv.S should be aware of its
550 * existence. Don't ever use this from user code.
551 */
Russell Kingcc20d422009-11-09 23:53:29 +0000552 case NR(cmpxchg):
Nicolas Pitreb49c0f22007-11-20 17:20:29 +0100553 for (;;) {
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100554 extern void do_DataAbort(unsigned long addr, unsigned int fsr,
555 struct pt_regs *regs);
556 unsigned long val;
557 unsigned long addr = regs->ARM_r2;
558 struct mm_struct *mm = current->mm;
559 pgd_t *pgd; pmd_t *pmd; pte_t *pte;
Hugh Dickins69b04752005-10-29 18:16:36 -0700560 spinlock_t *ptl;
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100561
562 regs->ARM_cpsr &= ~PSR_C_BIT;
Hugh Dickins69b04752005-10-29 18:16:36 -0700563 down_read(&mm->mmap_sem);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100564 pgd = pgd_offset(mm, addr);
565 if (!pgd_present(*pgd))
566 goto bad_access;
567 pmd = pmd_offset(pgd, addr);
568 if (!pmd_present(*pmd))
569 goto bad_access;
Hugh Dickins69b04752005-10-29 18:16:36 -0700570 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
Nicolas Pitre2ce98042006-03-25 22:44:05 +0000571 if (!pte_present(*pte) || !pte_dirty(*pte)) {
Hugh Dickins69b04752005-10-29 18:16:36 -0700572 pte_unmap_unlock(pte, ptl);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100573 goto bad_access;
Hugh Dickins69b04752005-10-29 18:16:36 -0700574 }
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100575 val = *(unsigned long *)addr;
576 val -= regs->ARM_r0;
577 if (val == 0) {
578 *(unsigned long *)addr = regs->ARM_r1;
579 regs->ARM_cpsr |= PSR_C_BIT;
580 }
Hugh Dickins69b04752005-10-29 18:16:36 -0700581 pte_unmap_unlock(pte, ptl);
582 up_read(&mm->mmap_sem);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100583 return val;
584
585 bad_access:
Hugh Dickins69b04752005-10-29 18:16:36 -0700586 up_read(&mm->mmap_sem);
Nicolas Pitre74f88492005-10-04 23:17:52 +0100587 /* simulate a write access fault */
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100588 do_DataAbort(addr, 15 + (1 << 11), regs);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100589 }
590#endif
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 default:
593 /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
594 if not implemented, rather than raising SIGILL. This
595 way the calling program can gracefully determine whether
596 a feature is supported. */
Russell Kingbfd2e292009-11-08 20:05:28 +0000597 if ((no & 0xffff) <= 0x7ff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return -ENOSYS;
599 break;
600 }
601#ifdef CONFIG_DEBUG_USER
602 /*
603 * experience shows that these seem to indicate that
604 * something catastrophic has happened
605 */
606 if (user_debug & UDBG_SYSCALL) {
607 printk("[%d] %s: arm syscall %d\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700608 task_pid_nr(current), current->comm, no);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100609 dump_instr("", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (user_mode(regs)) {
Russell King652a12e2005-04-17 15:50:36 +0100611 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 c_backtrace(regs->ARM_fp, processor_mode(regs));
613 }
614 }
615#endif
616 info.si_signo = SIGILL;
617 info.si_errno = 0;
618 info.si_code = ILL_ILLTRP;
619 info.si_addr = (void __user *)instruction_pointer(regs) -
620 (thumb_mode(regs) ? 2 : 4);
621
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700622 arm_notify_die("Oops - bad syscall(2)", regs, &info, no, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return 0;
624}
625
Nicolas Pitre4b0e07a2005-05-05 23:24:45 +0100626#ifdef CONFIG_TLS_REG_EMUL
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100627
628/*
629 * We might be running on an ARMv6+ processor which should have the TLS
Nicolas Pitre4b0e07a2005-05-05 23:24:45 +0100630 * register but for some reason we can't use it, or maybe an SMP system
631 * using a pre-ARMv6 processor (there are apparently a few prototypes like
632 * that in existence) and therefore access to that register must be
633 * emulated.
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100634 */
635
636static int get_tp_trap(struct pt_regs *regs, unsigned int instr)
637{
638 int reg = (instr >> 12) & 15;
639 if (reg == 15)
640 return 1;
641 regs->uregs[reg] = current_thread_info()->tp_value;
642 regs->ARM_pc += 4;
643 return 0;
644}
645
646static struct undef_hook arm_mrc_hook = {
647 .instr_mask = 0x0fff0fff,
648 .instr_val = 0x0e1d0f70,
649 .cpsr_mask = PSR_T_BIT,
650 .cpsr_val = 0,
651 .fn = get_tp_trap,
652};
653
654static int __init arm_mrc_hook_init(void)
655{
656 register_undef_hook(&arm_mrc_hook);
657 return 0;
658}
659
660late_initcall(arm_mrc_hook_init);
661
662#endif
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664void __bad_xchg(volatile void *ptr, int size)
665{
666 printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
667 __builtin_return_address(0), ptr, size);
668 BUG();
669}
670EXPORT_SYMBOL(__bad_xchg);
671
672/*
673 * A data abort trap was taken, but we did not handle the instruction.
674 * Try to abort the user program, or panic if it was the kernel.
675 */
676asmlinkage void
677baddataabort(int code, unsigned long instr, struct pt_regs *regs)
678{
679 unsigned long addr = instruction_pointer(regs);
680 siginfo_t info;
681
682#ifdef CONFIG_DEBUG_USER
683 if (user_debug & UDBG_BADABORT) {
684 printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700685 task_pid_nr(current), current->comm, code, instr);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100686 dump_instr(KERN_ERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 show_pte(current->mm, addr);
688 }
689#endif
690
691 info.si_signo = SIGILL;
692 info.si_errno = 0;
693 info.si_code = ILL_ILLOPC;
694 info.si_addr = (void __user *)addr;
695
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700696 arm_notify_die("unknown data abort code", regs, &info, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Nicolas Pitre7174d852006-12-07 19:09:20 +0100699void __attribute__((noreturn)) __bug(const char *file, int line)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Nicolas Pitre7174d852006-12-07 19:09:20 +0100701 printk(KERN_CRIT"kernel BUG at %s:%d!\n", file, line);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *(int *)0 = 0;
Catalin Marinas6a1ced52005-09-21 22:14:05 +0100703
704 /* Avoid "noreturn function does return" */
705 for (;;);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707EXPORT_SYMBOL(__bug);
708
709void __readwrite_bug(const char *fn)
710{
711 printk("%s called, but not implemented\n", fn);
712 BUG();
713}
714EXPORT_SYMBOL(__readwrite_bug);
715
716void __pte_error(const char *file, int line, unsigned long val)
717{
718 printk("%s:%d: bad pte %08lx.\n", file, line, val);
719}
720
721void __pmd_error(const char *file, int line, unsigned long val)
722{
723 printk("%s:%d: bad pmd %08lx.\n", file, line, val);
724}
725
726void __pgd_error(const char *file, int line, unsigned long val)
727{
728 printk("%s:%d: bad pgd %08lx.\n", file, line, val);
729}
730
731asmlinkage void __div0(void)
732{
733 printk("Division by zero in kernel.\n");
734 dump_stack();
735}
736EXPORT_SYMBOL(__div0);
737
738void abort(void)
739{
740 BUG();
741
742 /* if that doesn't kill us, halt */
743 panic("Oops failed to kill thread");
744}
745EXPORT_SYMBOL(abort);
746
747void __init trap_init(void)
748{
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600749 return;
750}
751
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100752static void __init kuser_get_tls_init(unsigned long vectors)
753{
754 /*
755 * vectors + 0xfe0 = __kuser_get_tls
756 * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8
757 */
758 if (tls_emu || has_tls_reg)
759 memcpy((void *)vectors + 0xfe0, (void *)vectors + 0xfe8, 4);
760}
761
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600762void __init early_trap_init(void)
763{
Catalin Marinas247055a2010-09-13 16:03:21 +0100764#if defined(CONFIG_CPU_USE_DOMAINS)
Hyok S. Choic760fc12006-03-27 15:18:50 +0100765 unsigned long vectors = CONFIG_VECTORS_BASE;
Catalin Marinas247055a2010-09-13 16:03:21 +0100766#else
767 unsigned long vectors = (unsigned long)vectors_page;
768#endif
Russell King79335232005-04-26 15:17:42 +0100769 extern char __stubs_start[], __stubs_end[];
770 extern char __vectors_start[], __vectors_end[];
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100771 extern char __kuser_helper_start[], __kuser_helper_end[];
772 int kuser_sz = __kuser_helper_end - __kuser_helper_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Russell King79335232005-04-26 15:17:42 +0100774 /*
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100775 * Copy the vectors, stubs and kuser helpers (in entry-armv.S)
776 * into the vector page, mapped at 0xffff0000, and ensure these
777 * are visible to the instruction stream.
Russell King79335232005-04-26 15:17:42 +0100778 */
Hyok S. Choic760fc12006-03-27 15:18:50 +0100779 memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
780 memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start);
781 memcpy((void *)vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);
Russell Kinge00d3492005-06-22 20:26:05 +0100782
783 /*
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100784 * Do processor specific fixups for the kuser helpers
785 */
786 kuser_get_tls_init(vectors);
787
788 /*
Russell Kinge00d3492005-06-22 20:26:05 +0100789 * Copy signal return handlers into the vector page, and
790 * set sigreturn to be a pointer to these.
791 */
Catalin Marinas247055a2010-09-13 16:03:21 +0100792 memcpy((void *)(vectors + KERN_SIGRETURN_CODE - CONFIG_VECTORS_BASE),
793 sigreturn_codes, sizeof(sigreturn_codes));
794 memcpy((void *)(vectors + KERN_RESTART_CODE - CONFIG_VECTORS_BASE),
795 syscall_restart_code, sizeof(syscall_restart_code));
Russell Kinge00d3492005-06-22 20:26:05 +0100796
Hyok S. Choic760fc12006-03-27 15:18:50 +0100797 flush_icache_range(vectors, vectors + PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
799}