Linux-2.6.12-rc2
[sfrench/cifs-2.6.git] / arch / arm / kernel / traps.c
1 /*
2  *  linux/arch/arm/kernel/traps.c
3  *
4  *  Copyright (C) 1995-2002 Russell King
5  *  Fragments that appear the same as linux/arch/i386/kernel/traps.c (C) Linus Torvalds
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  *  'traps.c' handles hardware exceptions after we have saved some state in
12  *  'linux/arch/arm/lib/traps.S'.  Mostly a debugging aid, but will probably
13  *  kill the offending process.
14  */
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/signal.h>
18 #include <linux/spinlock.h>
19 #include <linux/personality.h>
20 #include <linux/ptrace.h>
21 #include <linux/kallsyms.h>
22 #include <linux/init.h>
23
24 #include <asm/atomic.h>
25 #include <asm/cacheflush.h>
26 #include <asm/io.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/unistd.h>
30 #include <asm/traps.h>
31
32 #include "ptrace.h"
33
34 extern void c_backtrace (unsigned long fp, int pmode);
35 extern void show_pte(struct mm_struct *mm, unsigned long addr);
36
37 const char *processor_modes[]=
38 { "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" ,
39   "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26",
40   "USER_32", "FIQ_32" , "IRQ_32" , "SVC_32" , "UK4_32" , "UK5_32" , "UK6_32" , "ABT_32" ,
41   "UK8_32" , "UK9_32" , "UK10_32", "UND_32" , "UK12_32", "UK13_32", "UK14_32", "SYS_32"
42 };
43
44 static const char *handler[]= { "prefetch abort", "data abort", "address exception", "interrupt" };
45
46 #ifdef CONFIG_DEBUG_USER
47 unsigned int user_debug;
48
49 static int __init user_debug_setup(char *str)
50 {
51         get_option(&str, &user_debug);
52         return 1;
53 }
54 __setup("user_debug=", user_debug_setup);
55 #endif
56
57 void dump_backtrace_entry(unsigned long where, unsigned long from)
58 {
59 #ifdef CONFIG_KALLSYMS
60         printk("[<%08lx>] ", where);
61         print_symbol("(%s) ", where);
62         printk("from [<%08lx>] ", from);
63         print_symbol("(%s)\n", from);
64 #else
65         printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
66 #endif
67 }
68
69 /*
70  * Stack pointers should always be within the kernels view of
71  * physical memory.  If it is not there, then we can't dump
72  * out any information relating to the stack.
73  */
74 static int verify_stack(unsigned long sp)
75 {
76         if (sp < PAGE_OFFSET || (sp > (unsigned long)high_memory && high_memory != 0))
77                 return -EFAULT;
78
79         return 0;
80 }
81
82 /*
83  * Dump out the contents of some memory nicely...
84  */
85 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
86 {
87         unsigned long p = bottom & ~31;
88         mm_segment_t fs;
89         int i;
90
91         /*
92          * We need to switch to kernel mode so that we can use __get_user
93          * to safely read from kernel space.  Note that we now dump the
94          * code first, just in case the backtrace kills us.
95          */
96         fs = get_fs();
97         set_fs(KERNEL_DS);
98
99         printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
100
101         for (p = bottom & ~31; p < top;) {
102                 printk("%04lx: ", p & 0xffff);
103
104                 for (i = 0; i < 8; i++, p += 4) {
105                         unsigned int val;
106
107                         if (p < bottom || p >= top)
108                                 printk("         ");
109                         else {
110                                 __get_user(val, (unsigned long *)p);
111                                 printk("%08x ", val);
112                         }
113                 }
114                 printk ("\n");
115         }
116
117         set_fs(fs);
118 }
119
120 static void dump_instr(struct pt_regs *regs)
121 {
122         unsigned long addr = instruction_pointer(regs);
123         const int thumb = thumb_mode(regs);
124         const int width = thumb ? 4 : 8;
125         mm_segment_t fs;
126         int i;
127
128         /*
129          * We need to switch to kernel mode so that we can use __get_user
130          * to safely read from kernel space.  Note that we now dump the
131          * code first, just in case the backtrace kills us.
132          */
133         fs = get_fs();
134         set_fs(KERNEL_DS);
135
136         printk("Code: ");
137         for (i = -4; i < 1; i++) {
138                 unsigned int val, bad;
139
140                 if (thumb)
141                         bad = __get_user(val, &((u16 *)addr)[i]);
142                 else
143                         bad = __get_user(val, &((u32 *)addr)[i]);
144
145                 if (!bad)
146                         printk(i == 0 ? "(%0*x) " : "%0*x ", width, val);
147                 else {
148                         printk("bad PC value.");
149                         break;
150                 }
151         }
152         printk("\n");
153
154         set_fs(fs);
155 }
156
157 static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
158 {
159         unsigned int fp;
160         int ok = 1;
161
162         printk("Backtrace: ");
163         fp = regs->ARM_fp;
164         if (!fp) {
165                 printk("no frame pointer");
166                 ok = 0;
167         } else if (verify_stack(fp)) {
168                 printk("invalid frame pointer 0x%08x", fp);
169                 ok = 0;
170         } else if (fp < (unsigned long)(tsk->thread_info + 1))
171                 printk("frame pointer underflow");
172         printk("\n");
173
174         if (ok)
175                 c_backtrace(fp, processor_mode(regs));
176 }
177
178 void dump_stack(void)
179 {
180 #ifdef CONFIG_DEBUG_ERRORS
181         __backtrace();
182 #endif
183 }
184
185 EXPORT_SYMBOL(dump_stack);
186
187 void show_stack(struct task_struct *tsk, unsigned long *sp)
188 {
189         unsigned long fp;
190
191         if (!tsk)
192                 tsk = current;
193
194         if (tsk != current)
195                 fp = thread_saved_fp(tsk);
196         else
197                 asm("mov%? %0, fp" : "=r" (fp));
198
199         c_backtrace(fp, 0x10);
200         barrier();
201 }
202
203 DEFINE_SPINLOCK(die_lock);
204
205 /*
206  * This function is protected against re-entrancy.
207  */
208 NORET_TYPE void die(const char *str, struct pt_regs *regs, int err)
209 {
210         struct task_struct *tsk = current;
211         static int die_counter;
212
213         console_verbose();
214         spin_lock_irq(&die_lock);
215         bust_spinlocks(1);
216
217         printk("Internal error: %s: %x [#%d]\n", str, err, ++die_counter);
218         print_modules();
219         printk("CPU: %d\n", smp_processor_id());
220         show_regs(regs);
221         printk("Process %s (pid: %d, stack limit = 0x%p)\n",
222                 tsk->comm, tsk->pid, tsk->thread_info + 1);
223
224         if (!user_mode(regs) || in_interrupt()) {
225                 dump_mem("Stack: ", regs->ARM_sp, 8192+(unsigned long)tsk->thread_info);
226                 dump_backtrace(regs, tsk);
227                 dump_instr(regs);
228         }
229
230         bust_spinlocks(0);
231         spin_unlock_irq(&die_lock);
232         do_exit(SIGSEGV);
233 }
234
235 void die_if_kernel(const char *str, struct pt_regs *regs, int err)
236 {
237         if (user_mode(regs))
238                 return;
239
240         die(str, regs, err);
241 }
242
243 static void notify_die(const char *str, struct pt_regs *regs, siginfo_t *info,
244                        unsigned long err, unsigned long trap)
245 {
246         if (user_mode(regs)) {
247                 current->thread.error_code = err;
248                 current->thread.trap_no = trap;
249
250                 force_sig_info(info->si_signo, info, current);
251         } else {
252                 die(str, regs, err);
253         }
254 }
255
256 static LIST_HEAD(undef_hook);
257 static DEFINE_SPINLOCK(undef_lock);
258
259 void register_undef_hook(struct undef_hook *hook)
260 {
261         spin_lock_irq(&undef_lock);
262         list_add(&hook->node, &undef_hook);
263         spin_unlock_irq(&undef_lock);
264 }
265
266 void unregister_undef_hook(struct undef_hook *hook)
267 {
268         spin_lock_irq(&undef_lock);
269         list_del(&hook->node);
270         spin_unlock_irq(&undef_lock);
271 }
272
273 asmlinkage void do_undefinstr(struct pt_regs *regs)
274 {
275         unsigned int correction = thumb_mode(regs) ? 2 : 4;
276         unsigned int instr;
277         struct undef_hook *hook;
278         siginfo_t info;
279         void __user *pc;
280
281         /*
282          * According to the ARM ARM, PC is 2 or 4 bytes ahead,
283          * depending whether we're in Thumb mode or not.
284          * Correct this offset.
285          */
286         regs->ARM_pc -= correction;
287
288         pc = (void __user *)instruction_pointer(regs);
289         if (thumb_mode(regs)) {
290                 get_user(instr, (u16 __user *)pc);
291         } else {
292                 get_user(instr, (u32 __user *)pc);
293         }
294
295         spin_lock_irq(&undef_lock);
296         list_for_each_entry(hook, &undef_hook, node) {
297                 if ((instr & hook->instr_mask) == hook->instr_val &&
298                     (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val) {
299                         if (hook->fn(regs, instr) == 0) {
300                                 spin_unlock_irq(&undef_lock);
301                                 return;
302                         }
303                 }
304         }
305         spin_unlock_irq(&undef_lock);
306
307 #ifdef CONFIG_DEBUG_USER
308         if (user_debug & UDBG_UNDEFINED) {
309                 printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n",
310                         current->comm, current->pid, pc);
311                 dump_instr(regs);
312         }
313 #endif
314
315         info.si_signo = SIGILL;
316         info.si_errno = 0;
317         info.si_code  = ILL_ILLOPC;
318         info.si_addr  = pc;
319
320         notify_die("Oops - undefined instruction", regs, &info, 0, 6);
321 }
322
323 asmlinkage void do_unexp_fiq (struct pt_regs *regs)
324 {
325 #ifndef CONFIG_IGNORE_FIQ
326         printk("Hmm.  Unexpected FIQ received, but trying to continue\n");
327         printk("You may have a hardware problem...\n");
328 #endif
329 }
330
331 /*
332  * bad_mode handles the impossible case in the vectors.  If you see one of
333  * these, then it's extremely serious, and could mean you have buggy hardware.
334  * It never returns, and never tries to sync.  We hope that we can at least
335  * dump out some state information...
336  */
337 asmlinkage void bad_mode(struct pt_regs *regs, int reason, int proc_mode)
338 {
339         console_verbose();
340
341         printk(KERN_CRIT "Bad mode in %s handler detected: mode %s\n",
342                 handler[reason], processor_modes[proc_mode]);
343
344         die("Oops - bad mode", regs, 0);
345         local_irq_disable();
346         panic("bad mode");
347 }
348
349 static int bad_syscall(int n, struct pt_regs *regs)
350 {
351         struct thread_info *thread = current_thread_info();
352         siginfo_t info;
353
354         if (current->personality != PER_LINUX && thread->exec_domain->handler) {
355                 thread->exec_domain->handler(n, regs);
356                 return regs->ARM_r0;
357         }
358
359 #ifdef CONFIG_DEBUG_USER
360         if (user_debug & UDBG_SYSCALL) {
361                 printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n",
362                         current->pid, current->comm, n);
363                 dump_instr(regs);
364         }
365 #endif
366
367         info.si_signo = SIGILL;
368         info.si_errno = 0;
369         info.si_code  = ILL_ILLTRP;
370         info.si_addr  = (void __user *)instruction_pointer(regs) -
371                          (thumb_mode(regs) ? 2 : 4);
372
373         notify_die("Oops - bad syscall", regs, &info, n, 0);
374
375         return regs->ARM_r0;
376 }
377
378 static inline void
379 do_cache_op(unsigned long start, unsigned long end, int flags)
380 {
381         struct vm_area_struct *vma;
382
383         if (end < start || flags)
384                 return;
385
386         vma = find_vma(current->active_mm, start);
387         if (vma && vma->vm_start < end) {
388                 if (start < vma->vm_start)
389                         start = vma->vm_start;
390                 if (end > vma->vm_end)
391                         end = vma->vm_end;
392
393                 flush_cache_user_range(vma, start, end);
394         }
395 }
396
397 /*
398  * Handle all unrecognised system calls.
399  *  0x9f0000 - 0x9fffff are some more esoteric system calls
400  */
401 #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
402 asmlinkage int arm_syscall(int no, struct pt_regs *regs)
403 {
404         struct thread_info *thread = current_thread_info();
405         siginfo_t info;
406
407         if ((no >> 16) != 0x9f)
408                 return bad_syscall(no, regs);
409
410         switch (no & 0xffff) {
411         case 0: /* branch through 0 */
412                 info.si_signo = SIGSEGV;
413                 info.si_errno = 0;
414                 info.si_code  = SEGV_MAPERR;
415                 info.si_addr  = NULL;
416
417                 notify_die("branch through zero", regs, &info, 0, 0);
418                 return 0;
419
420         case NR(breakpoint): /* SWI BREAK_POINT */
421                 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
422                 ptrace_break(current, regs);
423                 return regs->ARM_r0;
424
425         /*
426          * Flush a region from virtual address 'r0' to virtual address 'r1'
427          * _exclusive_.  There is no alignment requirement on either address;
428          * user space does not need to know the hardware cache layout.
429          *
430          * r2 contains flags.  It should ALWAYS be passed as ZERO until it
431          * is defined to be something else.  For now we ignore it, but may
432          * the fires of hell burn in your belly if you break this rule. ;)
433          *
434          * (at a later date, we may want to allow this call to not flush
435          * various aspects of the cache.  Passing '0' will guarantee that
436          * everything necessary gets flushed to maintain consistency in
437          * the specified region).
438          */
439         case NR(cacheflush):
440                 do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
441                 return 0;
442
443         case NR(usr26):
444                 if (!(elf_hwcap & HWCAP_26BIT))
445                         break;
446                 regs->ARM_cpsr &= ~MODE32_BIT;
447                 return regs->ARM_r0;
448
449         case NR(usr32):
450                 if (!(elf_hwcap & HWCAP_26BIT))
451                         break;
452                 regs->ARM_cpsr |= MODE32_BIT;
453                 return regs->ARM_r0;
454
455         case NR(set_tls):
456                 thread->tp_value = regs->ARM_r0;
457                 /*
458                  * Our user accessible TLS ptr is located at 0xffff0ffc.
459                  * On SMP read access to this address must raise a fault
460                  * and be emulated from the data abort handler.
461                  * m
462                  */
463                 *((unsigned long *)0xffff0ffc) = thread->tp_value;
464                 return 0;
465
466         default:
467                 /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
468                    if not implemented, rather than raising SIGILL.  This
469                    way the calling program can gracefully determine whether
470                    a feature is supported.  */
471                 if (no <= 0x7ff)
472                         return -ENOSYS;
473                 break;
474         }
475 #ifdef CONFIG_DEBUG_USER
476         /*
477          * experience shows that these seem to indicate that
478          * something catastrophic has happened
479          */
480         if (user_debug & UDBG_SYSCALL) {
481                 printk("[%d] %s: arm syscall %d\n",
482                        current->pid, current->comm, no);
483                 dump_instr(regs);
484                 if (user_mode(regs)) {
485                         show_regs(regs);
486                         c_backtrace(regs->ARM_fp, processor_mode(regs));
487                 }
488         }
489 #endif
490         info.si_signo = SIGILL;
491         info.si_errno = 0;
492         info.si_code  = ILL_ILLTRP;
493         info.si_addr  = (void __user *)instruction_pointer(regs) -
494                          (thumb_mode(regs) ? 2 : 4);
495
496         notify_die("Oops - bad syscall(2)", regs, &info, no, 0);
497         return 0;
498 }
499
500 void __bad_xchg(volatile void *ptr, int size)
501 {
502         printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
503                 __builtin_return_address(0), ptr, size);
504         BUG();
505 }
506 EXPORT_SYMBOL(__bad_xchg);
507
508 /*
509  * A data abort trap was taken, but we did not handle the instruction.
510  * Try to abort the user program, or panic if it was the kernel.
511  */
512 asmlinkage void
513 baddataabort(int code, unsigned long instr, struct pt_regs *regs)
514 {
515         unsigned long addr = instruction_pointer(regs);
516         siginfo_t info;
517
518 #ifdef CONFIG_DEBUG_USER
519         if (user_debug & UDBG_BADABORT) {
520                 printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n",
521                         current->pid, current->comm, code, instr);
522                 dump_instr(regs);
523                 show_pte(current->mm, addr);
524         }
525 #endif
526
527         info.si_signo = SIGILL;
528         info.si_errno = 0;
529         info.si_code  = ILL_ILLOPC;
530         info.si_addr  = (void __user *)addr;
531
532         notify_die("unknown data abort code", regs, &info, instr, 0);
533 }
534
535 volatile void __bug(const char *file, int line, void *data)
536 {
537         printk(KERN_CRIT"kernel BUG at %s:%d!", file, line);
538         if (data)
539                 printk(" - extra data = %p", data);
540         printk("\n");
541         *(int *)0 = 0;
542 }
543 EXPORT_SYMBOL(__bug);
544
545 void __readwrite_bug(const char *fn)
546 {
547         printk("%s called, but not implemented\n", fn);
548         BUG();
549 }
550 EXPORT_SYMBOL(__readwrite_bug);
551
552 void __pte_error(const char *file, int line, unsigned long val)
553 {
554         printk("%s:%d: bad pte %08lx.\n", file, line, val);
555 }
556
557 void __pmd_error(const char *file, int line, unsigned long val)
558 {
559         printk("%s:%d: bad pmd %08lx.\n", file, line, val);
560 }
561
562 void __pgd_error(const char *file, int line, unsigned long val)
563 {
564         printk("%s:%d: bad pgd %08lx.\n", file, line, val);
565 }
566
567 asmlinkage void __div0(void)
568 {
569         printk("Division by zero in kernel.\n");
570         dump_stack();
571 }
572 EXPORT_SYMBOL(__div0);
573
574 void abort(void)
575 {
576         BUG();
577
578         /* if that doesn't kill us, halt */
579         panic("Oops failed to kill thread");
580 }
581 EXPORT_SYMBOL(abort);
582
583 void __init trap_init(void)
584 {
585         extern void __trap_init(void);
586
587         __trap_init();
588         flush_icache_range(0xffff0000, 0xffff0000 + PAGE_SIZE);
589         modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
590 }