blob: f01ad2992a20b56c78227e58675754e5c80853e9 [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
Russell King10252aa2014-01-03 15:01:39 +000040static const char *handler[]= {
41 "prefetch abort",
42 "data abort",
43 "address exception",
44 "interrupt",
45 "undefined instruction",
46};
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Catalin Marinas247055a2010-09-13 16:03:21 +010048void *vectors_page;
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#ifdef CONFIG_DEBUG_USER
51unsigned int user_debug;
52
53static int __init user_debug_setup(char *str)
54{
55 get_option(&str, &user_debug);
56 return 1;
57}
58__setup("user_debug=", user_debug_setup);
59#endif
60
Russell Kinge40c2ec2009-10-11 15:17:53 +010061static void dump_mem(const char *, const char *, unsigned long, unsigned long);
Russell King7ab3f8d2007-03-02 15:01:36 +000062
Russell King7ab3f8d2007-03-02 15:01:36 +000063void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65#ifdef CONFIG_KALLSYMS
Joe Perches69448c22010-11-05 16:12:34 -070066 printk("[<%08lx>] (%pS) from [<%08lx>] (%pS)\n", where, (void *)where, from, (void *)from);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#else
68 printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
69#endif
Russell King7ab3f8d2007-03-02 15:01:36 +000070
71 if (in_exception_text(where))
Russell Kinge40c2ec2009-10-11 15:17:53 +010072 dump_mem("", "Exception stack", frame + 4, frame + 4 + sizeof(struct pt_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Catalin Marinasbff595c2009-02-16 11:41:36 +010075#ifndef CONFIG_ARM_UNWIND
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
77 * Stack pointers should always be within the kernels view of
78 * physical memory. If it is not there, then we can't dump
79 * out any information relating to the stack.
80 */
81static int verify_stack(unsigned long sp)
82{
Russell King09d9bae2008-09-05 14:08:44 +010083 if (sp < PAGE_OFFSET ||
84 (sp > (unsigned long)high_memory && high_memory != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return -EFAULT;
86
87 return 0;
88}
Catalin Marinasbff595c2009-02-16 11:41:36 +010089#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 * Dump out the contents of some memory nicely...
93 */
Russell Kinge40c2ec2009-10-11 15:17:53 +010094static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
95 unsigned long top)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Russell Kingd191fe02009-10-11 15:03:11 +010097 unsigned long first;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 mm_segment_t fs;
99 int i;
100
101 /*
102 * We need to switch to kernel mode so that we can use __get_user
103 * to safely read from kernel space. Note that we now dump the
104 * code first, just in case the backtrace kills us.
105 */
106 fs = get_fs();
107 set_fs(KERNEL_DS);
108
Russell Kinge40c2ec2009-10-11 15:17:53 +0100109 printk("%s%s(0x%08lx to 0x%08lx)\n", lvl, str, bottom, top);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Russell Kingd191fe02009-10-11 15:03:11 +0100111 for (first = bottom & ~31; first < top; first += 32) {
112 unsigned long p;
113 char str[sizeof(" 12345678") * 8 + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Russell Kingd191fe02009-10-11 15:03:11 +0100115 memset(str, ' ', sizeof(str));
116 str[sizeof(str) - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Russell Kingd191fe02009-10-11 15:03:11 +0100118 for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
119 if (p >= bottom && p < top) {
120 unsigned long val;
121 if (__get_user(val, (unsigned long *)p) == 0)
122 sprintf(str + i * 9, " %08lx", val);
123 else
124 sprintf(str + i * 9, " ????????");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126 }
Russell Kinge40c2ec2009-10-11 15:17:53 +0100127 printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 set_fs(fs);
131}
132
Russell Kinge40c2ec2009-10-11 15:17:53 +0100133static void dump_instr(const char *lvl, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 unsigned long addr = instruction_pointer(regs);
136 const int thumb = thumb_mode(regs);
137 const int width = thumb ? 4 : 8;
138 mm_segment_t fs;
Russell Kingd191fe02009-10-11 15:03:11 +0100139 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int i;
141
142 /*
143 * We need to switch to kernel mode so that we can use __get_user
144 * to safely read from kernel space. Note that we now dump the
145 * code first, just in case the backtrace kills us.
146 */
147 fs = get_fs();
148 set_fs(KERNEL_DS);
149
Russell Kinga9011582011-06-09 23:21:11 +0100150 for (i = -4; i < 1 + !!thumb; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 unsigned int val, bad;
152
153 if (thumb)
154 bad = __get_user(val, &((u16 *)addr)[i]);
155 else
156 bad = __get_user(val, &((u32 *)addr)[i]);
157
158 if (!bad)
Russell Kingd191fe02009-10-11 15:03:11 +0100159 p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ",
160 width, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 else {
Russell Kingd191fe02009-10-11 15:03:11 +0100162 p += sprintf(p, "bad PC value");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 break;
164 }
165 }
Russell Kinge40c2ec2009-10-11 15:17:53 +0100166 printk("%sCode: %s\n", lvl, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 set_fs(fs);
169}
170
Catalin Marinasbff595c2009-02-16 11:41:36 +0100171#ifdef CONFIG_ARM_UNWIND
172static inline void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
173{
174 unwind_backtrace(regs, tsk);
175}
176#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
178{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100179 unsigned int fp, mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 int ok = 1;
181
182 printk("Backtrace: ");
Catalin Marinas67a94c22009-02-11 13:06:53 +0100183
184 if (!tsk)
185 tsk = current;
186
187 if (regs) {
188 fp = regs->ARM_fp;
189 mode = processor_mode(regs);
190 } else if (tsk != current) {
191 fp = thread_saved_fp(tsk);
192 mode = 0x10;
193 } else {
194 asm("mov %0, fp" : "=r" (fp) : : "cc");
195 mode = 0x10;
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!fp) {
199 printk("no frame pointer");
200 ok = 0;
201 } else if (verify_stack(fp)) {
202 printk("invalid frame pointer 0x%08x", fp);
203 ok = 0;
Al Viro55205822006-01-12 01:05:57 -0800204 } else if (fp < (unsigned long)end_of_stack(tsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 printk("frame pointer underflow");
206 printk("\n");
207
208 if (ok)
Catalin Marinas67a94c22009-02-11 13:06:53 +0100209 c_backtrace(fp, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
Catalin Marinasbff595c2009-02-16 11:41:36 +0100211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213void dump_stack(void)
214{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100215 dump_backtrace(NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218EXPORT_SYMBOL(dump_stack);
219
220void show_stack(struct task_struct *tsk, unsigned long *sp)
221{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100222 dump_backtrace(NULL, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 barrier();
224}
225
Russell Kingd9202422007-06-17 13:38:27 +0100226#ifdef CONFIG_PREEMPT
227#define S_PREEMPT " PREEMPT"
228#else
229#define S_PREEMPT ""
230#endif
231#ifdef CONFIG_SMP
232#define S_SMP " SMP"
233#else
234#define S_SMP ""
235#endif
Russell King8211ca62012-02-04 12:12:11 +0000236#ifdef CONFIG_THUMB2_KERNEL
237#define S_ISA " THUMB2"
238#else
239#define S_ISA " ARM"
240#endif
Russell Kingd9202422007-06-17 13:38:27 +0100241
Russell Kinga9221de2010-01-20 17:02:54 +0000242static int __die(const char *str, int err, struct thread_info *thread, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Russell Kingd3629792005-10-30 19:01:43 +0000244 struct task_struct *tsk = thread->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 static int die_counter;
Russell Kinga9221de2010-01-20 17:02:54 +0000246 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Russell King8211ca62012-02-04 12:12:11 +0000248 printk(KERN_EMERG "Internal error: %s: %x [#%d]" S_PREEMPT S_SMP
249 S_ISA "\n", str, err, ++die_counter);
Russell Kinga9221de2010-01-20 17:02:54 +0000250
251 /* trap and error numbers are mostly meaningless on ARM */
252 ret = notify_die(DIE_OOPS, str, regs, err, tsk->thread.trap_no, SIGSEGV);
253 if (ret == NOTIFY_STOP)
254 return ret;
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 print_modules();
Russell King652a12e2005-04-17 15:50:36 +0100257 __show_regs(regs);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100258 printk(KERN_EMERG "Process %.*s (pid: %d, stack limit = 0x%p)\n",
259 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (!user_mode(regs) || in_interrupt()) {
Russell Kinge40c2ec2009-10-11 15:17:53 +0100262 dump_mem(KERN_EMERG, "Stack: ", regs->ARM_sp,
Al Viro32d39a92006-01-12 01:05:58 -0800263 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 dump_backtrace(regs, tsk);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100265 dump_instr(KERN_EMERG, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
Russell Kinga9221de2010-01-20 17:02:54 +0000267
268 return ret;
Russell Kingd3629792005-10-30 19:01:43 +0000269}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500271static DEFINE_RAW_SPINLOCK(die_lock);
Russell Kingd3629792005-10-30 19:01:43 +0000272
273/*
274 * This function is protected against re-entrancy.
275 */
Russell Kinga9221de2010-01-20 17:02:54 +0000276void die(const char *str, struct pt_regs *regs, int err)
Russell Kingd3629792005-10-30 19:01:43 +0000277{
278 struct thread_info *thread = current_thread_info();
Russell Kinga9221de2010-01-20 17:02:54 +0000279 int ret;
Stephen Boydbdf800c2012-02-07 19:42:33 +0100280 enum bug_trap_type bug_type = BUG_TRAP_TYPE_NONE;
Russell Kingd3629792005-10-30 19:01:43 +0000281
Russell Kingd9202422007-06-17 13:38:27 +0100282 oops_enter();
283
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500284 raw_spin_lock_irq(&die_lock);
Russell King03a6e5b2009-10-11 15:25:05 +0100285 console_verbose();
Russell Kingd3629792005-10-30 19:01:43 +0000286 bust_spinlocks(1);
Simon Glass87e040b2011-08-16 23:44:26 +0100287 if (!user_mode(regs))
Stephen Boydbdf800c2012-02-07 19:42:33 +0100288 bug_type = report_bug(regs->ARM_pc, regs);
289 if (bug_type != BUG_TRAP_TYPE_NONE)
290 str = "Oops - BUG";
Russell Kinga9221de2010-01-20 17:02:54 +0000291 ret = __die(str, err, thread, regs);
292
293 if (regs && kexec_should_crash(thread->task))
294 crash_kexec(regs);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700297 add_taint(TAINT_DIE);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500298 raw_spin_unlock_irq(&die_lock);
Russell King03a6e5b2009-10-11 15:25:05 +0100299 oops_exit();
Russell King31867492006-02-19 19:53:56 +0000300
Russell Kingd9202422007-06-17 13:38:27 +0100301 if (in_interrupt())
302 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700303 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700304 panic("Fatal exception");
Russell Kinga9221de2010-01-20 17:02:54 +0000305 if (ret != NOTIFY_STOP)
306 do_exit(SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700309void arm_notify_die(const char *str, struct pt_regs *regs,
310 struct siginfo *info, unsigned long err, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 if (user_mode(regs)) {
313 current->thread.error_code = err;
314 current->thread.trap_no = trap;
315
316 force_sig_info(info->si_signo, info, current);
317 } else {
318 die(str, regs, err);
319 }
320}
321
Simon Glass87e040b2011-08-16 23:44:26 +0100322#ifdef CONFIG_GENERIC_BUG
323
324int is_valid_bugaddr(unsigned long pc)
325{
326#ifdef CONFIG_THUMB2_KERNEL
327 unsigned short bkpt;
328#else
329 unsigned long bkpt;
330#endif
331
332 if (probe_kernel_address((unsigned *)pc, bkpt))
333 return 0;
334
335 return bkpt == BUG_INSTR_VALUE;
336}
337
338#endif
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static LIST_HEAD(undef_hook);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500341static DEFINE_RAW_SPINLOCK(undef_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343void register_undef_hook(struct undef_hook *hook)
344{
Russell King109d89c2005-07-16 16:43:33 +0100345 unsigned long flags;
346
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500347 raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 list_add(&hook->node, &undef_hook);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500349 raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352void unregister_undef_hook(struct undef_hook *hook)
353{
Russell King109d89c2005-07-16 16:43:33 +0100354 unsigned long flags;
355
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500356 raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 list_del(&hook->node);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500358 raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Russell Kingb03a5b72008-08-11 12:27:16 +0100361static int call_undef_hook(struct pt_regs *regs, unsigned int instr)
362{
363 struct undef_hook *hook;
364 unsigned long flags;
365 int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL;
366
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500367 raw_spin_lock_irqsave(&undef_lock, flags);
Russell Kingb03a5b72008-08-11 12:27:16 +0100368 list_for_each_entry(hook, &undef_hook, node)
369 if ((instr & hook->instr_mask) == hook->instr_val &&
370 (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val)
371 fn = hook->fn;
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500372 raw_spin_unlock_irqrestore(&undef_lock, flags);
Russell Kingb03a5b72008-08-11 12:27:16 +0100373
374 return fn ? fn(regs, instr) : 1;
375}
376
Russell King7ab3f8d2007-03-02 15:01:36 +0000377asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned int instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 siginfo_t info;
381 void __user *pc;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 pc = (void __user *)instruction_pointer(regs);
Dan Williamsdfc544c2007-02-13 17:11:34 +0100384
385 if (processor_mode(regs) == SVC_MODE) {
Jon Medhurst592201a2011-03-26 19:19:07 +0000386#ifdef CONFIG_THUMB2_KERNEL
387 if (thumb_mode(regs)) {
388 instr = ((u16 *)pc)[0];
389 if (is_wide_instruction(instr)) {
390 instr <<= 16;
391 instr |= ((u16 *)pc)[1];
392 }
393 } else
394#endif
395 instr = *(u32 *) pc;
Dan Williamsdfc544c2007-02-13 17:11:34 +0100396 } else if (thumb_mode(regs)) {
Will Deaconeff605b2012-09-07 18:21:44 +0100397 if (get_user(instr, (u16 __user *)pc))
398 goto die_sig;
Jon Medhurst592201a2011-03-26 19:19:07 +0000399 if (is_wide_instruction(instr)) {
400 unsigned int instr2;
Will Deaconeff605b2012-09-07 18:21:44 +0100401 if (get_user(instr2, (u16 __user *)pc+1))
402 goto die_sig;
Jon Medhurst592201a2011-03-26 19:19:07 +0000403 instr <<= 16;
404 instr |= instr2;
405 }
Will Deaconeff605b2012-09-07 18:21:44 +0100406 } else if (get_user(instr, (u32 __user *)pc)) {
407 goto die_sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
Russell Kingb03a5b72008-08-11 12:27:16 +0100410 if (call_undef_hook(regs, instr) == 0)
411 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Will Deaconeff605b2012-09-07 18:21:44 +0100413die_sig:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414#ifdef CONFIG_DEBUG_USER
415 if (user_debug & UDBG_UNDEFINED) {
416 printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700417 current->comm, task_pid_nr(current), pc);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100418 dump_instr(KERN_INFO, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420#endif
421
422 info.si_signo = SIGILL;
423 info.si_errno = 0;
424 info.si_code = ILL_ILLOPC;
425 info.si_addr = pc;
426
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700427 arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
430asmlinkage void do_unexp_fiq (struct pt_regs *regs)
431{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 printk("Hmm. Unexpected FIQ received, but trying to continue\n");
433 printk("You may have a hardware problem...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/*
437 * bad_mode handles the impossible case in the vectors. If you see one of
438 * these, then it's extremely serious, and could mean you have buggy hardware.
439 * It never returns, and never tries to sync. We hope that we can at least
440 * dump out some state information...
441 */
Russell Kingae0a8462007-01-09 12:57:37 +0000442asmlinkage void bad_mode(struct pt_regs *regs, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 console_verbose();
445
Russell Kingae0a8462007-01-09 12:57:37 +0000446 printk(KERN_CRIT "Bad mode in %s handler detected\n", handler[reason]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 die("Oops - bad mode", regs, 0);
449 local_irq_disable();
450 panic("bad mode");
451}
452
453static int bad_syscall(int n, struct pt_regs *regs)
454{
455 struct thread_info *thread = current_thread_info();
456 siginfo_t info;
457
Nicolas Pitre88b9ef42011-04-13 05:01:52 +0100458 if ((current->personality & PER_MASK) != PER_LINUX &&
Nicolas Pitrea999cb042005-10-28 16:35:46 +0100459 thread->exec_domain->handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 thread->exec_domain->handler(n, regs);
461 return regs->ARM_r0;
462 }
463
464#ifdef CONFIG_DEBUG_USER
465 if (user_debug & UDBG_SYSCALL) {
466 printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700467 task_pid_nr(current), current->comm, n);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100468 dump_instr(KERN_ERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470#endif
471
472 info.si_signo = SIGILL;
473 info.si_errno = 0;
474 info.si_code = ILL_ILLTRP;
475 info.si_addr = (void __user *)instruction_pointer(regs) -
476 (thumb_mode(regs) ? 2 : 4);
477
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700478 arm_notify_die("Oops - bad syscall", regs, &info, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 return regs->ARM_r0;
481}
482
483static inline void
484do_cache_op(unsigned long start, unsigned long end, int flags)
485{
Russell Kingaa45ee82009-09-28 11:41:51 +0100486 struct mm_struct *mm = current->active_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 struct vm_area_struct *vma;
488
489 if (end < start || flags)
490 return;
491
Russell Kingaa45ee82009-09-28 11:41:51 +0100492 down_read(&mm->mmap_sem);
493 vma = find_vma(mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (vma && vma->vm_start < end) {
495 if (start < vma->vm_start)
496 start = vma->vm_start;
497 if (end > vma->vm_end)
498 end = vma->vm_end;
499
Dima Zavin8209f1c2012-04-30 10:26:14 +0100500 up_read(&mm->mmap_sem);
Dima Zavin6019ae72012-03-29 20:44:06 +0100501 flush_cache_user_range(start, end);
Dima Zavin8209f1c2012-04-30 10:26:14 +0100502 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Russell Kingaa45ee82009-09-28 11:41:51 +0100504 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/*
508 * Handle all unrecognised system calls.
509 * 0x9f0000 - 0x9fffff are some more esoteric system calls
510 */
511#define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
512asmlinkage int arm_syscall(int no, struct pt_regs *regs)
513{
514 struct thread_info *thread = current_thread_info();
515 siginfo_t info;
516
Nicolas Pitre3f2829a2006-01-14 16:31:29 +0000517 if ((no >> 16) != (__ARM_NR_BASE>> 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return bad_syscall(no, regs);
519
520 switch (no & 0xffff) {
521 case 0: /* branch through 0 */
522 info.si_signo = SIGSEGV;
523 info.si_errno = 0;
524 info.si_code = SEGV_MAPERR;
525 info.si_addr = NULL;
526
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700527 arm_notify_die("branch through zero", regs, &info, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return 0;
529
530 case NR(breakpoint): /* SWI BREAK_POINT */
531 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
532 ptrace_break(current, regs);
533 return regs->ARM_r0;
534
535 /*
536 * Flush a region from virtual address 'r0' to virtual address 'r1'
537 * _exclusive_. There is no alignment requirement on either address;
538 * user space does not need to know the hardware cache layout.
539 *
540 * r2 contains flags. It should ALWAYS be passed as ZERO until it
541 * is defined to be something else. For now we ignore it, but may
542 * the fires of hell burn in your belly if you break this rule. ;)
543 *
544 * (at a later date, we may want to allow this call to not flush
545 * various aspects of the cache. Passing '0' will guarantee that
546 * everything necessary gets flushed to maintain consistency in
547 * the specified region).
548 */
549 case NR(cacheflush):
550 do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
551 return 0;
552
553 case NR(usr26):
554 if (!(elf_hwcap & HWCAP_26BIT))
555 break;
556 regs->ARM_cpsr &= ~MODE32_BIT;
557 return regs->ARM_r0;
558
559 case NR(usr32):
560 if (!(elf_hwcap & HWCAP_26BIT))
561 break;
562 regs->ARM_cpsr |= MODE32_BIT;
563 return regs->ARM_r0;
564
565 case NR(set_tls):
566 thread->tp_value = regs->ARM_r0;
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100567 if (tls_emu)
568 return 0;
569 if (has_tls_reg) {
570 asm ("mcr p15, 0, %0, c13, c0, 3"
571 : : "r" (regs->ARM_r0));
572 } else {
573 /*
574 * User space must never try to access this directly.
575 * Expect your app to break eventually if you do so.
576 * The user helper at 0xffff0fe0 must be used instead.
577 * (see entry-armv.S for details)
578 */
579 *((unsigned int *)0xffff0ff0) = regs->ARM_r0;
580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return 0;
582
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100583#ifdef CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG
584 /*
585 * Atomically store r1 in *r2 if *r2 is equal to r0 for user space.
586 * Return zero in r0 if *MEM was changed or non-zero if no exchange
587 * happened. Also set the user C flag accordingly.
588 * If access permissions have to be fixed up then non-zero is
589 * returned and the operation has to be re-attempted.
590 *
591 * *NOTE*: This is a ghost syscall private to the kernel. Only the
592 * __kuser_cmpxchg code in entry-armv.S should be aware of its
593 * existence. Don't ever use this from user code.
594 */
Russell Kingcc20d422009-11-09 23:53:29 +0000595 case NR(cmpxchg):
Nicolas Pitreb49c0f22007-11-20 17:20:29 +0100596 for (;;) {
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100597 extern void do_DataAbort(unsigned long addr, unsigned int fsr,
598 struct pt_regs *regs);
599 unsigned long val;
600 unsigned long addr = regs->ARM_r2;
601 struct mm_struct *mm = current->mm;
602 pgd_t *pgd; pmd_t *pmd; pte_t *pte;
Hugh Dickins69b04752005-10-29 18:16:36 -0700603 spinlock_t *ptl;
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100604
605 regs->ARM_cpsr &= ~PSR_C_BIT;
Hugh Dickins69b04752005-10-29 18:16:36 -0700606 down_read(&mm->mmap_sem);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100607 pgd = pgd_offset(mm, addr);
608 if (!pgd_present(*pgd))
609 goto bad_access;
610 pmd = pmd_offset(pgd, addr);
611 if (!pmd_present(*pmd))
612 goto bad_access;
Hugh Dickins69b04752005-10-29 18:16:36 -0700613 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
Po-Yu Chuang373ce302011-06-09 08:42:17 +0100614 if (!pte_present(*pte) || !pte_write(*pte) || !pte_dirty(*pte)) {
Hugh Dickins69b04752005-10-29 18:16:36 -0700615 pte_unmap_unlock(pte, ptl);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100616 goto bad_access;
Hugh Dickins69b04752005-10-29 18:16:36 -0700617 }
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100618 val = *(unsigned long *)addr;
619 val -= regs->ARM_r0;
620 if (val == 0) {
621 *(unsigned long *)addr = regs->ARM_r1;
622 regs->ARM_cpsr |= PSR_C_BIT;
623 }
Hugh Dickins69b04752005-10-29 18:16:36 -0700624 pte_unmap_unlock(pte, ptl);
625 up_read(&mm->mmap_sem);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100626 return val;
627
628 bad_access:
Hugh Dickins69b04752005-10-29 18:16:36 -0700629 up_read(&mm->mmap_sem);
Nicolas Pitre74f88492005-10-04 23:17:52 +0100630 /* simulate a write access fault */
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100631 do_DataAbort(addr, 15 + (1 << 11), regs);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100632 }
633#endif
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 default:
636 /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
637 if not implemented, rather than raising SIGILL. This
638 way the calling program can gracefully determine whether
639 a feature is supported. */
Russell Kingbfd2e292009-11-08 20:05:28 +0000640 if ((no & 0xffff) <= 0x7ff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return -ENOSYS;
642 break;
643 }
644#ifdef CONFIG_DEBUG_USER
645 /*
646 * experience shows that these seem to indicate that
647 * something catastrophic has happened
648 */
649 if (user_debug & UDBG_SYSCALL) {
650 printk("[%d] %s: arm syscall %d\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700651 task_pid_nr(current), current->comm, no);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100652 dump_instr("", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (user_mode(regs)) {
Russell King652a12e2005-04-17 15:50:36 +0100654 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 c_backtrace(regs->ARM_fp, processor_mode(regs));
656 }
657 }
658#endif
659 info.si_signo = SIGILL;
660 info.si_errno = 0;
661 info.si_code = ILL_ILLTRP;
662 info.si_addr = (void __user *)instruction_pointer(regs) -
663 (thumb_mode(regs) ? 2 : 4);
664
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700665 arm_notify_die("Oops - bad syscall(2)", regs, &info, no, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667}
668
Nicolas Pitre4b0e07a2005-05-05 23:24:45 +0100669#ifdef CONFIG_TLS_REG_EMUL
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100670
671/*
672 * We might be running on an ARMv6+ processor which should have the TLS
Nicolas Pitre4b0e07a2005-05-05 23:24:45 +0100673 * register but for some reason we can't use it, or maybe an SMP system
674 * using a pre-ARMv6 processor (there are apparently a few prototypes like
675 * that in existence) and therefore access to that register must be
676 * emulated.
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100677 */
678
679static int get_tp_trap(struct pt_regs *regs, unsigned int instr)
680{
681 int reg = (instr >> 12) & 15;
682 if (reg == 15)
683 return 1;
684 regs->uregs[reg] = current_thread_info()->tp_value;
685 regs->ARM_pc += 4;
686 return 0;
687}
688
689static struct undef_hook arm_mrc_hook = {
690 .instr_mask = 0x0fff0fff,
691 .instr_val = 0x0e1d0f70,
692 .cpsr_mask = PSR_T_BIT,
693 .cpsr_val = 0,
694 .fn = get_tp_trap,
695};
696
697static int __init arm_mrc_hook_init(void)
698{
699 register_undef_hook(&arm_mrc_hook);
700 return 0;
701}
702
703late_initcall(arm_mrc_hook_init);
704
705#endif
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707void __bad_xchg(volatile void *ptr, int size)
708{
709 printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
710 __builtin_return_address(0), ptr, size);
711 BUG();
712}
713EXPORT_SYMBOL(__bad_xchg);
714
715/*
716 * A data abort trap was taken, but we did not handle the instruction.
717 * Try to abort the user program, or panic if it was the kernel.
718 */
719asmlinkage void
720baddataabort(int code, unsigned long instr, struct pt_regs *regs)
721{
722 unsigned long addr = instruction_pointer(regs);
723 siginfo_t info;
724
725#ifdef CONFIG_DEBUG_USER
726 if (user_debug & UDBG_BADABORT) {
727 printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700728 task_pid_nr(current), current->comm, code, instr);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100729 dump_instr(KERN_ERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 show_pte(current->mm, addr);
731 }
732#endif
733
734 info.si_signo = SIGILL;
735 info.si_errno = 0;
736 info.si_code = ILL_ILLOPC;
737 info.si_addr = (void __user *)addr;
738
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700739 arm_notify_die("unknown data abort code", regs, &info, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742void __readwrite_bug(const char *fn)
743{
744 printk("%s called, but not implemented\n", fn);
745 BUG();
746}
747EXPORT_SYMBOL(__readwrite_bug);
748
Russell King69529c02010-11-16 00:19:55 +0000749void __pte_error(const char *file, int line, pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Will Deacon29a38192011-02-15 14:31:37 +0100751 printk("%s:%d: bad pte %08llx.\n", file, line, (long long)pte_val(pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Russell King69529c02010-11-16 00:19:55 +0000754void __pmd_error(const char *file, int line, pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Will Deacon29a38192011-02-15 14:31:37 +0100756 printk("%s:%d: bad pmd %08llx.\n", file, line, (long long)pmd_val(pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Russell King69529c02010-11-16 00:19:55 +0000759void __pgd_error(const char *file, int line, pgd_t pgd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Will Deacon29a38192011-02-15 14:31:37 +0100761 printk("%s:%d: bad pgd %08llx.\n", file, line, (long long)pgd_val(pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764asmlinkage void __div0(void)
765{
766 printk("Division by zero in kernel.\n");
767 dump_stack();
768}
769EXPORT_SYMBOL(__div0);
770
771void abort(void)
772{
773 BUG();
774
775 /* if that doesn't kill us, halt */
776 panic("Oops failed to kill thread");
777}
778EXPORT_SYMBOL(abort);
779
780void __init trap_init(void)
781{
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600782 return;
783}
784
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100785static void __init kuser_get_tls_init(unsigned long vectors)
786{
787 /*
788 * vectors + 0xfe0 = __kuser_get_tls
789 * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8
790 */
791 if (tls_emu || has_tls_reg)
792 memcpy((void *)vectors + 0xfe0, (void *)vectors + 0xfe8, 4);
793}
794
Russell King94e5a852012-01-18 15:32:49 +0000795void __init early_trap_init(void *vectors_base)
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600796{
Russell King94e5a852012-01-18 15:32:49 +0000797 unsigned long vectors = (unsigned long)vectors_base;
Russell King79335232005-04-26 15:17:42 +0100798 extern char __stubs_start[], __stubs_end[];
799 extern char __vectors_start[], __vectors_end[];
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100800 extern char __kuser_helper_start[], __kuser_helper_end[];
801 int kuser_sz = __kuser_helper_end - __kuser_helper_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Russell King94e5a852012-01-18 15:32:49 +0000803 vectors_page = vectors_base;
804
Russell King79335232005-04-26 15:17:42 +0100805 /*
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100806 * Copy the vectors, stubs and kuser helpers (in entry-armv.S)
807 * into the vector page, mapped at 0xffff0000, and ensure these
808 * are visible to the instruction stream.
Russell King79335232005-04-26 15:17:42 +0100809 */
Hyok S. Choic760fc12006-03-27 15:18:50 +0100810 memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
811 memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start);
812 memcpy((void *)vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);
Russell Kinge00d3492005-06-22 20:26:05 +0100813
814 /*
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100815 * Do processor specific fixups for the kuser helpers
816 */
817 kuser_get_tls_init(vectors);
818
819 /*
Russell Kinge00d3492005-06-22 20:26:05 +0100820 * Copy signal return handlers into the vector page, and
821 * set sigreturn to be a pointer to these.
822 */
Catalin Marinas247055a2010-09-13 16:03:21 +0100823 memcpy((void *)(vectors + KERN_SIGRETURN_CODE - CONFIG_VECTORS_BASE),
824 sigreturn_codes, sizeof(sigreturn_codes));
825 memcpy((void *)(vectors + KERN_RESTART_CODE - CONFIG_VECTORS_BASE),
826 syscall_restart_code, sizeof(syscall_restart_code));
Russell Kinge00d3492005-06-22 20:26:05 +0100827
Hyok S. Choic760fc12006-03-27 15:18:50 +0100828 flush_icache_range(vectors, vectors + PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
830}