blob: ff5d87396b08ac9a7d4998b0160665bd378b7bb8 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static const char *handler[]= { "prefetch abort", "data abort", "address exception", "interrupt" };
41
Jaeseong GIMb8499412012-06-26 10:36:57 -070042#ifdef CONFIG_LGE_CRASH_HANDLER
43static int first_call_chain = 0;
44static int first_die = 1;
45#endif
Catalin Marinas247055a2010-09-13 16:03:21 +010046void *vectors_page;
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#ifdef CONFIG_DEBUG_USER
49unsigned int user_debug;
50
51static int __init user_debug_setup(char *str)
52{
53 get_option(&str, &user_debug);
54 return 1;
55}
56__setup("user_debug=", user_debug_setup);
57#endif
58
Russell Kinge40c2ec2009-10-11 15:17:53 +010059static void dump_mem(const char *, const char *, unsigned long, unsigned long);
Russell King7ab3f8d2007-03-02 15:01:36 +000060
Russell King7ab3f8d2007-03-02 15:01:36 +000061void dump_backtrace_entry(unsigned long where, unsigned long from, unsigned long frame)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63#ifdef CONFIG_KALLSYMS
Jaeseong GIMb8499412012-06-26 10:36:57 -070064#ifdef CONFIG_LGE_CRASH_HANDLER
65 if (first_call_chain)
66 set_crash_store_enable();
67#endif
Joe Perches69448c22010-11-05 16:12:34 -070068 printk("[<%08lx>] (%pS) from [<%08lx>] (%pS)\n", where, (void *)where, from, (void *)from);
Jaeseong GIMb8499412012-06-26 10:36:57 -070069#ifdef CONFIG_LGE_CRASH_HANDLER
70 set_crash_store_disable();
71#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#else
73 printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
74#endif
Russell King7ab3f8d2007-03-02 15:01:36 +000075
76 if (in_exception_text(where))
Russell Kinge40c2ec2009-10-11 15:17:53 +010077 dump_mem("", "Exception stack", frame + 4, frame + 4 + sizeof(struct pt_regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Catalin Marinasbff595c2009-02-16 11:41:36 +010080#ifndef CONFIG_ARM_UNWIND
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Stack pointers should always be within the kernels view of
83 * physical memory. If it is not there, then we can't dump
84 * out any information relating to the stack.
85 */
86static int verify_stack(unsigned long sp)
87{
Russell King09d9bae2008-09-05 14:08:44 +010088 if (sp < PAGE_OFFSET ||
89 (sp > (unsigned long)high_memory && high_memory != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return -EFAULT;
91
92 return 0;
93}
Catalin Marinasbff595c2009-02-16 11:41:36 +010094#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/*
97 * Dump out the contents of some memory nicely...
98 */
Russell Kinge40c2ec2009-10-11 15:17:53 +010099static void dump_mem(const char *lvl, const char *str, unsigned long bottom,
100 unsigned long top)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Russell Kingd191fe02009-10-11 15:03:11 +0100102 unsigned long first;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 mm_segment_t fs;
104 int i;
105
106 /*
107 * We need to switch to kernel mode so that we can use __get_user
108 * to safely read from kernel space. Note that we now dump the
109 * code first, just in case the backtrace kills us.
110 */
111 fs = get_fs();
112 set_fs(KERNEL_DS);
113
Russell Kinge40c2ec2009-10-11 15:17:53 +0100114 printk("%s%s(0x%08lx to 0x%08lx)\n", lvl, str, bottom, top);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Russell Kingd191fe02009-10-11 15:03:11 +0100116 for (first = bottom & ~31; first < top; first += 32) {
117 unsigned long p;
118 char str[sizeof(" 12345678") * 8 + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Russell Kingd191fe02009-10-11 15:03:11 +0100120 memset(str, ' ', sizeof(str));
121 str[sizeof(str) - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Russell Kingd191fe02009-10-11 15:03:11 +0100123 for (p = first, i = 0; i < 8 && p < top; i++, p += 4) {
124 if (p >= bottom && p < top) {
125 unsigned long val;
126 if (__get_user(val, (unsigned long *)p) == 0)
127 sprintf(str + i * 9, " %08lx", val);
128 else
129 sprintf(str + i * 9, " ????????");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
131 }
Russell Kinge40c2ec2009-10-11 15:17:53 +0100132 printk("%s%04lx:%s\n", lvl, first & 0xffff, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
135 set_fs(fs);
136}
137
Russell Kinge40c2ec2009-10-11 15:17:53 +0100138static void dump_instr(const char *lvl, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 unsigned long addr = instruction_pointer(regs);
141 const int thumb = thumb_mode(regs);
142 const int width = thumb ? 4 : 8;
143 mm_segment_t fs;
Russell Kingd191fe02009-10-11 15:03:11 +0100144 char str[sizeof("00000000 ") * 5 + 2 + 1], *p = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int i;
146
147 /*
148 * We need to switch to kernel mode so that we can use __get_user
149 * to safely read from kernel space. Note that we now dump the
150 * code first, just in case the backtrace kills us.
151 */
152 fs = get_fs();
153 set_fs(KERNEL_DS);
154
Russell Kinga9011582011-06-09 23:21:11 +0100155 for (i = -4; i < 1 + !!thumb; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 unsigned int val, bad;
157
158 if (thumb)
159 bad = __get_user(val, &((u16 *)addr)[i]);
160 else
161 bad = __get_user(val, &((u32 *)addr)[i]);
162
163 if (!bad)
Russell Kingd191fe02009-10-11 15:03:11 +0100164 p += sprintf(p, i == 0 ? "(%0*x) " : "%0*x ",
165 width, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 else {
Russell Kingd191fe02009-10-11 15:03:11 +0100167 p += sprintf(p, "bad PC value");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 break;
169 }
170 }
Russell Kinge40c2ec2009-10-11 15:17:53 +0100171 printk("%sCode: %s\n", lvl, str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 set_fs(fs);
174}
175
Catalin Marinasbff595c2009-02-16 11:41:36 +0100176#ifdef CONFIG_ARM_UNWIND
177static inline void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
178{
179 unwind_backtrace(regs, tsk);
180}
181#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
183{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100184 unsigned int fp, mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 int ok = 1;
186
187 printk("Backtrace: ");
Catalin Marinas67a94c22009-02-11 13:06:53 +0100188
189 if (!tsk)
190 tsk = current;
191
192 if (regs) {
193 fp = regs->ARM_fp;
194 mode = processor_mode(regs);
195 } else if (tsk != current) {
196 fp = thread_saved_fp(tsk);
197 mode = 0x10;
198 } else {
199 asm("mov %0, fp" : "=r" (fp) : : "cc");
200 mode = 0x10;
201 }
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (!fp) {
204 printk("no frame pointer");
205 ok = 0;
206 } else if (verify_stack(fp)) {
207 printk("invalid frame pointer 0x%08x", fp);
208 ok = 0;
Al Viro55205822006-01-12 01:05:57 -0800209 } else if (fp < (unsigned long)end_of_stack(tsk))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 printk("frame pointer underflow");
211 printk("\n");
212
213 if (ok)
Catalin Marinas67a94c22009-02-11 13:06:53 +0100214 c_backtrace(fp, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
Catalin Marinasbff595c2009-02-16 11:41:36 +0100216#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218void dump_stack(void)
219{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100220 dump_backtrace(NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223EXPORT_SYMBOL(dump_stack);
224
225void show_stack(struct task_struct *tsk, unsigned long *sp)
226{
Catalin Marinas67a94c22009-02-11 13:06:53 +0100227 dump_backtrace(NULL, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 barrier();
229}
230
Russell Kingd9202422007-06-17 13:38:27 +0100231#ifdef CONFIG_PREEMPT
232#define S_PREEMPT " PREEMPT"
233#else
234#define S_PREEMPT ""
235#endif
236#ifdef CONFIG_SMP
237#define S_SMP " SMP"
238#else
239#define S_SMP ""
240#endif
Russell King8211ca62012-02-04 12:12:11 +0000241#ifdef CONFIG_THUMB2_KERNEL
242#define S_ISA " THUMB2"
243#else
244#define S_ISA " ARM"
245#endif
Russell Kingd9202422007-06-17 13:38:27 +0100246
Russell Kinga9221de2010-01-20 17:02:54 +0000247static int __die(const char *str, int err, struct thread_info *thread, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Russell Kingd3629792005-10-30 19:01:43 +0000249 struct task_struct *tsk = thread->task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 static int die_counter;
Russell Kinga9221de2010-01-20 17:02:54 +0000251 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Jaeseong GIMb8499412012-06-26 10:36:57 -0700253#ifdef CONFIG_LGE_CRASH_HANDLER
254 if (first_die) {
255 first_call_chain = 1;
256 first_die = 0;
257 }
258 set_kernel_crash_magic_number();
259 set_crash_store_enable();
260#endif
261
Russell King8211ca62012-02-04 12:12:11 +0000262 printk(KERN_EMERG "Internal error: %s: %x [#%d]" S_PREEMPT S_SMP
263 S_ISA "\n", str, err, ++die_counter);
Russell Kinga9221de2010-01-20 17:02:54 +0000264
Jaeseong GIMb8499412012-06-26 10:36:57 -0700265#ifdef CONFIG_LGE_CRASH_HANDLER
266 set_crash_store_disable();
267#endif
268
Russell Kinga9221de2010-01-20 17:02:54 +0000269 /* trap and error numbers are mostly meaningless on ARM */
270 ret = notify_die(DIE_OOPS, str, regs, err, tsk->thread.trap_no, SIGSEGV);
271 if (ret == NOTIFY_STOP)
272 return ret;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 print_modules();
Russell King652a12e2005-04-17 15:50:36 +0100275 __show_regs(regs);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100276 printk(KERN_EMERG "Process %.*s (pid: %d, stack limit = 0x%p)\n",
277 TASK_COMM_LEN, tsk->comm, task_pid_nr(tsk), thread + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (!user_mode(regs) || in_interrupt()) {
Russell Kinge40c2ec2009-10-11 15:17:53 +0100280 dump_mem(KERN_EMERG, "Stack: ", regs->ARM_sp,
Al Viro32d39a92006-01-12 01:05:58 -0800281 THREAD_SIZE + (unsigned long)task_stack_page(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 dump_backtrace(regs, tsk);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100283 dump_instr(KERN_EMERG, regs);
Jaeseong GIMb8499412012-06-26 10:36:57 -0700284#ifdef CONFIG_LGE_CRASH_HANDLER
285 first_call_chain = 0;
286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Russell Kinga9221de2010-01-20 17:02:54 +0000288
289 return ret;
Russell Kingd3629792005-10-30 19:01:43 +0000290}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500292static DEFINE_RAW_SPINLOCK(die_lock);
Russell Kingd3629792005-10-30 19:01:43 +0000293
294/*
295 * This function is protected against re-entrancy.
296 */
Russell Kinga9221de2010-01-20 17:02:54 +0000297void die(const char *str, struct pt_regs *regs, int err)
Russell Kingd3629792005-10-30 19:01:43 +0000298{
299 struct thread_info *thread = current_thread_info();
Russell Kinga9221de2010-01-20 17:02:54 +0000300 int ret;
Stephen Boydbdf800c2012-02-07 19:42:33 +0100301 enum bug_trap_type bug_type = BUG_TRAP_TYPE_NONE;
Russell Kingd3629792005-10-30 19:01:43 +0000302
Russell Kingd9202422007-06-17 13:38:27 +0100303 oops_enter();
304
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500305 raw_spin_lock_irq(&die_lock);
Russell King03a6e5b2009-10-11 15:25:05 +0100306 console_verbose();
Russell Kingd3629792005-10-30 19:01:43 +0000307 bust_spinlocks(1);
Simon Glass87e040b2011-08-16 23:44:26 +0100308 if (!user_mode(regs))
Stephen Boydbdf800c2012-02-07 19:42:33 +0100309 bug_type = report_bug(regs->ARM_pc, regs);
310 if (bug_type != BUG_TRAP_TYPE_NONE)
311 str = "Oops - BUG";
Russell Kinga9221de2010-01-20 17:02:54 +0000312 ret = __die(str, err, thread, regs);
313
314 if (regs && kexec_should_crash(thread->task))
315 crash_kexec(regs);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 bust_spinlocks(0);
Pavel Emelianovbcdcd8e2007-07-17 04:03:42 -0700318 add_taint(TAINT_DIE);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500319 raw_spin_unlock_irq(&die_lock);
Russell King03a6e5b2009-10-11 15:25:05 +0100320 oops_exit();
Russell King31867492006-02-19 19:53:56 +0000321
Russell Kingd9202422007-06-17 13:38:27 +0100322 if (in_interrupt())
323 panic("Fatal exception in interrupt");
Hormscea6a4b2006-07-30 03:03:34 -0700324 if (panic_on_oops)
Horms012c4372006-08-13 23:24:22 -0700325 panic("Fatal exception");
Russell Kinga9221de2010-01-20 17:02:54 +0000326 if (ret != NOTIFY_STOP)
327 do_exit(SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700330void arm_notify_die(const char *str, struct pt_regs *regs,
331 struct siginfo *info, unsigned long err, unsigned long trap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 if (user_mode(regs)) {
334 current->thread.error_code = err;
335 current->thread.trap_no = trap;
336
337 force_sig_info(info->si_signo, info, current);
338 } else {
339 die(str, regs, err);
340 }
341}
342
Simon Glass87e040b2011-08-16 23:44:26 +0100343#ifdef CONFIG_GENERIC_BUG
344
345int is_valid_bugaddr(unsigned long pc)
346{
347#ifdef CONFIG_THUMB2_KERNEL
348 unsigned short bkpt;
349#else
350 unsigned long bkpt;
351#endif
352
353 if (probe_kernel_address((unsigned *)pc, bkpt))
354 return 0;
355
356 return bkpt == BUG_INSTR_VALUE;
357}
358
359#endif
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361static LIST_HEAD(undef_hook);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500362static DEFINE_RAW_SPINLOCK(undef_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364void register_undef_hook(struct undef_hook *hook)
365{
Russell King109d89c2005-07-16 16:43:33 +0100366 unsigned long flags;
367
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500368 raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 list_add(&hook->node, &undef_hook);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500370 raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373void unregister_undef_hook(struct undef_hook *hook)
374{
Russell King109d89c2005-07-16 16:43:33 +0100375 unsigned long flags;
376
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500377 raw_spin_lock_irqsave(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 list_del(&hook->node);
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500379 raw_spin_unlock_irqrestore(&undef_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Russell Kingb03a5b72008-08-11 12:27:16 +0100382static int call_undef_hook(struct pt_regs *regs, unsigned int instr)
383{
384 struct undef_hook *hook;
385 unsigned long flags;
386 int (*fn)(struct pt_regs *regs, unsigned int instr) = NULL;
387
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500388 raw_spin_lock_irqsave(&undef_lock, flags);
Russell Kingb03a5b72008-08-11 12:27:16 +0100389 list_for_each_entry(hook, &undef_hook, node)
390 if ((instr & hook->instr_mask) == hook->instr_val &&
391 (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val)
392 fn = hook->fn;
Thomas Gleixnerbd31b852009-07-03 08:44:46 -0500393 raw_spin_unlock_irqrestore(&undef_lock, flags);
Russell Kingb03a5b72008-08-11 12:27:16 +0100394
395 return fn ? fn(regs, instr) : 1;
396}
397
Russell King7ab3f8d2007-03-02 15:01:36 +0000398asmlinkage void __exception do_undefinstr(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 unsigned int correction = thumb_mode(regs) ? 2 : 4;
401 unsigned int instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 siginfo_t info;
403 void __user *pc;
404
405 /*
406 * According to the ARM ARM, PC is 2 or 4 bytes ahead,
407 * depending whether we're in Thumb mode or not.
408 * Correct this offset.
409 */
410 regs->ARM_pc -= correction;
411
412 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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 get_user(instr, (u16 __user *)pc);
Jon Medhurst592201a2011-03-26 19:19:07 +0000427 if (is_wide_instruction(instr)) {
428 unsigned int instr2;
429 get_user(instr2, (u16 __user *)pc+1);
430 instr <<= 16;
431 instr |= instr2;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 } else {
434 get_user(instr, (u32 __user *)pc);
435 }
436
Russell Kingb03a5b72008-08-11 12:27:16 +0100437 if (call_undef_hook(regs, instr) == 0)
438 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440#ifdef CONFIG_DEBUG_USER
441 if (user_debug & UDBG_UNDEFINED) {
442 printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700443 current->comm, task_pid_nr(current), pc);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100444 dump_instr(KERN_INFO, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 }
446#endif
447
448 info.si_signo = SIGILL;
449 info.si_errno = 0;
450 info.si_code = ILL_ILLOPC;
451 info.si_addr = pc;
452
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700453 arm_notify_die("Oops - undefined instruction", regs, &info, 0, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456asmlinkage void do_unexp_fiq (struct pt_regs *regs)
457{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 printk("Hmm. Unexpected FIQ received, but trying to continue\n");
459 printk("You may have a hardware problem...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462/*
463 * bad_mode handles the impossible case in the vectors. If you see one of
464 * these, then it's extremely serious, and could mean you have buggy hardware.
465 * It never returns, and never tries to sync. We hope that we can at least
466 * dump out some state information...
467 */
Russell Kingae0a8462007-01-09 12:57:37 +0000468asmlinkage void bad_mode(struct pt_regs *regs, int reason)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 console_verbose();
471
Russell Kingae0a8462007-01-09 12:57:37 +0000472 printk(KERN_CRIT "Bad mode in %s handler detected\n", handler[reason]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 die("Oops - bad mode", regs, 0);
475 local_irq_disable();
476 panic("bad mode");
477}
478
479static int bad_syscall(int n, struct pt_regs *regs)
480{
481 struct thread_info *thread = current_thread_info();
482 siginfo_t info;
483
Nicolas Pitre88b9ef42011-04-13 05:01:52 +0100484 if ((current->personality & PER_MASK) != PER_LINUX &&
Nicolas Pitrea999cb042005-10-28 16:35:46 +0100485 thread->exec_domain->handler) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 thread->exec_domain->handler(n, regs);
487 return regs->ARM_r0;
488 }
489
490#ifdef CONFIG_DEBUG_USER
491 if (user_debug & UDBG_SYSCALL) {
492 printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700493 task_pid_nr(current), current->comm, n);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100494 dump_instr(KERN_ERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
496#endif
497
498 info.si_signo = SIGILL;
499 info.si_errno = 0;
500 info.si_code = ILL_ILLTRP;
501 info.si_addr = (void __user *)instruction_pointer(regs) -
502 (thumb_mode(regs) ? 2 : 4);
503
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700504 arm_notify_die("Oops - bad syscall", regs, &info, n, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 return regs->ARM_r0;
507}
508
509static inline void
510do_cache_op(unsigned long start, unsigned long end, int flags)
511{
Russell Kingaa45ee82009-09-28 11:41:51 +0100512 struct mm_struct *mm = current->active_mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct vm_area_struct *vma;
514
515 if (end < start || flags)
516 return;
517
Russell Kingaa45ee82009-09-28 11:41:51 +0100518 down_read(&mm->mmap_sem);
519 vma = find_vma(mm, start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (vma && vma->vm_start < end) {
521 if (start < vma->vm_start)
522 start = vma->vm_start;
523 if (end > vma->vm_end)
524 end = vma->vm_end;
525
Dima Zavin96714b52010-04-27 20:57:04 -0700526 up_read(&mm->mmap_sem);
527 flush_cache_user_range(start, end);
528 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Russell Kingaa45ee82009-09-28 11:41:51 +0100530 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533/*
534 * Handle all unrecognised system calls.
535 * 0x9f0000 - 0x9fffff are some more esoteric system calls
536 */
537#define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
538asmlinkage int arm_syscall(int no, struct pt_regs *regs)
539{
540 struct thread_info *thread = current_thread_info();
541 siginfo_t info;
542
Nicolas Pitre3f2829a2006-01-14 16:31:29 +0000543 if ((no >> 16) != (__ARM_NR_BASE>> 16))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return bad_syscall(no, regs);
545
546 switch (no & 0xffff) {
547 case 0: /* branch through 0 */
548 info.si_signo = SIGSEGV;
549 info.si_errno = 0;
550 info.si_code = SEGV_MAPERR;
551 info.si_addr = NULL;
552
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700553 arm_notify_die("branch through zero", regs, &info, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return 0;
555
556 case NR(breakpoint): /* SWI BREAK_POINT */
557 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
558 ptrace_break(current, regs);
559 return regs->ARM_r0;
560
561 /*
562 * Flush a region from virtual address 'r0' to virtual address 'r1'
563 * _exclusive_. There is no alignment requirement on either address;
564 * user space does not need to know the hardware cache layout.
565 *
566 * r2 contains flags. It should ALWAYS be passed as ZERO until it
567 * is defined to be something else. For now we ignore it, but may
568 * the fires of hell burn in your belly if you break this rule. ;)
569 *
570 * (at a later date, we may want to allow this call to not flush
571 * various aspects of the cache. Passing '0' will guarantee that
572 * everything necessary gets flushed to maintain consistency in
573 * the specified region).
574 */
575 case NR(cacheflush):
576 do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
577 return 0;
578
579 case NR(usr26):
580 if (!(elf_hwcap & HWCAP_26BIT))
581 break;
582 regs->ARM_cpsr &= ~MODE32_BIT;
583 return regs->ARM_r0;
584
585 case NR(usr32):
586 if (!(elf_hwcap & HWCAP_26BIT))
587 break;
588 regs->ARM_cpsr |= MODE32_BIT;
589 return regs->ARM_r0;
590
591 case NR(set_tls):
592 thread->tp_value = regs->ARM_r0;
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100593 if (tls_emu)
594 return 0;
595 if (has_tls_reg) {
596 asm ("mcr p15, 0, %0, c13, c0, 3"
597 : : "r" (regs->ARM_r0));
598 } else {
599 /*
600 * User space must never try to access this directly.
601 * Expect your app to break eventually if you do so.
602 * The user helper at 0xffff0fe0 must be used instead.
603 * (see entry-armv.S for details)
604 */
605 *((unsigned int *)0xffff0ff0) = regs->ARM_r0;
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return 0;
608
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100609#ifdef CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG
610 /*
611 * Atomically store r1 in *r2 if *r2 is equal to r0 for user space.
612 * Return zero in r0 if *MEM was changed or non-zero if no exchange
613 * happened. Also set the user C flag accordingly.
614 * If access permissions have to be fixed up then non-zero is
615 * returned and the operation has to be re-attempted.
616 *
617 * *NOTE*: This is a ghost syscall private to the kernel. Only the
618 * __kuser_cmpxchg code in entry-armv.S should be aware of its
619 * existence. Don't ever use this from user code.
620 */
Russell Kingcc20d422009-11-09 23:53:29 +0000621 case NR(cmpxchg):
Nicolas Pitreb49c0f22007-11-20 17:20:29 +0100622 for (;;) {
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100623 extern void do_DataAbort(unsigned long addr, unsigned int fsr,
624 struct pt_regs *regs);
625 unsigned long val;
626 unsigned long addr = regs->ARM_r2;
627 struct mm_struct *mm = current->mm;
628 pgd_t *pgd; pmd_t *pmd; pte_t *pte;
Hugh Dickins69b04752005-10-29 18:16:36 -0700629 spinlock_t *ptl;
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100630
631 regs->ARM_cpsr &= ~PSR_C_BIT;
Hugh Dickins69b04752005-10-29 18:16:36 -0700632 down_read(&mm->mmap_sem);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100633 pgd = pgd_offset(mm, addr);
634 if (!pgd_present(*pgd))
635 goto bad_access;
636 pmd = pmd_offset(pgd, addr);
637 if (!pmd_present(*pmd))
638 goto bad_access;
Hugh Dickins69b04752005-10-29 18:16:36 -0700639 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
Po-Yu Chuang373ce302011-06-09 08:42:17 +0100640 if (!pte_present(*pte) || !pte_write(*pte) || !pte_dirty(*pte)) {
Hugh Dickins69b04752005-10-29 18:16:36 -0700641 pte_unmap_unlock(pte, ptl);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100642 goto bad_access;
Hugh Dickins69b04752005-10-29 18:16:36 -0700643 }
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100644 val = *(unsigned long *)addr;
645 val -= regs->ARM_r0;
646 if (val == 0) {
647 *(unsigned long *)addr = regs->ARM_r1;
648 regs->ARM_cpsr |= PSR_C_BIT;
649 }
Hugh Dickins69b04752005-10-29 18:16:36 -0700650 pte_unmap_unlock(pte, ptl);
651 up_read(&mm->mmap_sem);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100652 return val;
653
654 bad_access:
Hugh Dickins69b04752005-10-29 18:16:36 -0700655 up_read(&mm->mmap_sem);
Nicolas Pitre74f88492005-10-04 23:17:52 +0100656 /* simulate a write access fault */
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100657 do_DataAbort(addr, 15 + (1 << 11), regs);
Nicolas Pitredcef1f62005-06-08 19:00:47 +0100658 }
659#endif
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 default:
662 /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
663 if not implemented, rather than raising SIGILL. This
664 way the calling program can gracefully determine whether
665 a feature is supported. */
Russell Kingbfd2e292009-11-08 20:05:28 +0000666 if ((no & 0xffff) <= 0x7ff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return -ENOSYS;
668 break;
669 }
670#ifdef CONFIG_DEBUG_USER
671 /*
672 * experience shows that these seem to indicate that
673 * something catastrophic has happened
674 */
675 if (user_debug & UDBG_SYSCALL) {
676 printk("[%d] %s: arm syscall %d\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700677 task_pid_nr(current), current->comm, no);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100678 dump_instr("", regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (user_mode(regs)) {
Russell King652a12e2005-04-17 15:50:36 +0100680 __show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 c_backtrace(regs->ARM_fp, processor_mode(regs));
682 }
683 }
684#endif
685 info.si_signo = SIGILL;
686 info.si_errno = 0;
687 info.si_code = ILL_ILLTRP;
688 info.si_addr = (void __user *)instruction_pointer(regs) -
689 (thumb_mode(regs) ? 2 : 4);
690
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700691 arm_notify_die("Oops - bad syscall(2)", regs, &info, no, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693}
694
Nicolas Pitre4b0e07a2005-05-05 23:24:45 +0100695#ifdef CONFIG_TLS_REG_EMUL
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100696
697/*
698 * We might be running on an ARMv6+ processor which should have the TLS
Nicolas Pitre4b0e07a2005-05-05 23:24:45 +0100699 * register but for some reason we can't use it, or maybe an SMP system
700 * using a pre-ARMv6 processor (there are apparently a few prototypes like
701 * that in existence) and therefore access to that register must be
702 * emulated.
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100703 */
704
705static int get_tp_trap(struct pt_regs *regs, unsigned int instr)
706{
707 int reg = (instr >> 12) & 15;
708 if (reg == 15)
709 return 1;
710 regs->uregs[reg] = current_thread_info()->tp_value;
711 regs->ARM_pc += 4;
712 return 0;
713}
714
715static struct undef_hook arm_mrc_hook = {
716 .instr_mask = 0x0fff0fff,
717 .instr_val = 0x0e1d0f70,
718 .cpsr_mask = PSR_T_BIT,
719 .cpsr_val = 0,
720 .fn = get_tp_trap,
721};
722
723static int __init arm_mrc_hook_init(void)
724{
725 register_undef_hook(&arm_mrc_hook);
726 return 0;
727}
728
729late_initcall(arm_mrc_hook_init);
730
731#endif
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733void __bad_xchg(volatile void *ptr, int size)
734{
735 printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
736 __builtin_return_address(0), ptr, size);
737 BUG();
738}
739EXPORT_SYMBOL(__bad_xchg);
740
741/*
742 * A data abort trap was taken, but we did not handle the instruction.
743 * Try to abort the user program, or panic if it was the kernel.
744 */
745asmlinkage void
746baddataabort(int code, unsigned long instr, struct pt_regs *regs)
747{
748 unsigned long addr = instruction_pointer(regs);
749 siginfo_t info;
750
751#ifdef CONFIG_DEBUG_USER
752 if (user_debug & UDBG_BADABORT) {
753 printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n",
Alexey Dobriyan19c58702007-10-18 23:40:41 -0700754 task_pid_nr(current), current->comm, code, instr);
Russell Kinge40c2ec2009-10-11 15:17:53 +0100755 dump_instr(KERN_ERR, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 show_pte(current->mm, addr);
757 }
758#endif
759
760 info.si_signo = SIGILL;
761 info.si_errno = 0;
762 info.si_code = ILL_ILLOPC;
763 info.si_addr = (void __user *)addr;
764
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -0700765 arm_notify_die("unknown data abort code", regs, &info, instr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768void __readwrite_bug(const char *fn)
769{
770 printk("%s called, but not implemented\n", fn);
771 BUG();
772}
773EXPORT_SYMBOL(__readwrite_bug);
774
Russell King69529c02010-11-16 00:19:55 +0000775void __pte_error(const char *file, int line, pte_t pte)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Will Deacon29a38192011-02-15 14:31:37 +0100777 printk("%s:%d: bad pte %08llx.\n", file, line, (long long)pte_val(pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Russell King69529c02010-11-16 00:19:55 +0000780void __pmd_error(const char *file, int line, pmd_t pmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Will Deacon29a38192011-02-15 14:31:37 +0100782 printk("%s:%d: bad pmd %08llx.\n", file, line, (long long)pmd_val(pmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Russell King69529c02010-11-16 00:19:55 +0000785void __pgd_error(const char *file, int line, pgd_t pgd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Will Deacon29a38192011-02-15 14:31:37 +0100787 printk("%s:%d: bad pgd %08llx.\n", file, line, (long long)pgd_val(pgd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790asmlinkage void __div0(void)
791{
792 printk("Division by zero in kernel.\n");
793 dump_stack();
794}
795EXPORT_SYMBOL(__div0);
796
797void abort(void)
798{
799 BUG();
800
801 /* if that doesn't kill us, halt */
802 panic("Oops failed to kill thread");
803}
804EXPORT_SYMBOL(abort);
805
806void __init trap_init(void)
807{
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600808 return;
809}
810
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100811static void __init kuser_get_tls_init(unsigned long vectors)
812{
813 /*
814 * vectors + 0xfe0 = __kuser_get_tls
815 * vectors + 0xfe8 = hardware TLS instruction at 0xffff0fe8
816 */
817 if (tls_emu || has_tls_reg)
818 memcpy((void *)vectors + 0xfe0, (void *)vectors + 0xfe8, 4);
819}
820
Russell King94e5a852012-01-18 15:32:49 +0000821void __init early_trap_init(void *vectors_base)
Jason Wessel5cbad0e2008-02-20 13:33:40 -0600822{
Russell King94e5a852012-01-18 15:32:49 +0000823 unsigned long vectors = (unsigned long)vectors_base;
Russell King79335232005-04-26 15:17:42 +0100824 extern char __stubs_start[], __stubs_end[];
825 extern char __vectors_start[], __vectors_end[];
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100826 extern char __kuser_helper_start[], __kuser_helper_end[];
827 int kuser_sz = __kuser_helper_end - __kuser_helper_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Russell King94e5a852012-01-18 15:32:49 +0000829 vectors_page = vectors_base;
830
Russell King79335232005-04-26 15:17:42 +0100831 /*
Nicolas Pitre2d2669b2005-04-29 22:08:33 +0100832 * Copy the vectors, stubs and kuser helpers (in entry-armv.S)
833 * into the vector page, mapped at 0xffff0000, and ensure these
834 * are visible to the instruction stream.
Russell King79335232005-04-26 15:17:42 +0100835 */
Hyok S. Choic760fc12006-03-27 15:18:50 +0100836 memcpy((void *)vectors, __vectors_start, __vectors_end - __vectors_start);
837 memcpy((void *)vectors + 0x200, __stubs_start, __stubs_end - __stubs_start);
838 memcpy((void *)vectors + 0x1000 - kuser_sz, __kuser_helper_start, kuser_sz);
Russell Kinge00d3492005-06-22 20:26:05 +0100839
840 /*
Tony Lindgrenf159f4e2010-07-05 14:53:10 +0100841 * Do processor specific fixups for the kuser helpers
842 */
843 kuser_get_tls_init(vectors);
844
845 /*
Russell Kinge00d3492005-06-22 20:26:05 +0100846 * Copy signal return handlers into the vector page, and
847 * set sigreturn to be a pointer to these.
848 */
Catalin Marinas247055a2010-09-13 16:03:21 +0100849 memcpy((void *)(vectors + KERN_SIGRETURN_CODE - CONFIG_VECTORS_BASE),
850 sigreturn_codes, sizeof(sigreturn_codes));
851 memcpy((void *)(vectors + KERN_RESTART_CODE - CONFIG_VECTORS_BASE),
852 syscall_restart_code, sizeof(syscall_restart_code));
Russell Kinge00d3492005-06-22 20:26:05 +0100853
Hyok S. Choic760fc12006-03-27 15:18:50 +0100854 flush_icache_range(vectors, vectors + PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
856}