Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / arch / arm26 / kernel / traps.c
1 /*
2  *  linux/arch/arm26/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  *  Copyright (C) 2003 Ian Molton (ARM26)
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  *  'traps.c' handles hardware exceptions after we have saved some state in
13  *  'linux/arch/arm26/lib/traps.S'.  Mostly a debugging aid, but will probably
14  *  kill the offending process.
15  */
16
17 #include <linux/module.h>
18 #include <linux/config.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/mm.h>
24 #include <linux/spinlock.h>
25 #include <linux/personality.h>
26 #include <linux/ptrace.h>
27 #include <linux/elf.h>
28 #include <linux/interrupt.h>
29 #include <linux/init.h>
30
31 #include <asm/atomic.h>
32 #include <asm/io.h>
33 #include <asm/pgtable.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <asm/unistd.h>
37 #include <linux/mutex.h>
38
39 #include "ptrace.h"
40
41 extern void c_backtrace (unsigned long fp, int pmode);
42 extern void show_pte(struct mm_struct *mm, unsigned long addr);
43
44 const char *processor_modes[] = { "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" };
45
46 static const char *handler[]= { "prefetch abort", "data abort", "address exception", "interrupt" "*bad reason*"};
47
48 /*
49  * Stack pointers should always be within the kernels view of
50  * physical memory.  If it is not there, then we can't dump
51  * out any information relating to the stack.
52  */
53 static int verify_stack(unsigned long sp)
54 {
55         if (sp < PAGE_OFFSET || (sp > (unsigned long)high_memory && high_memory != 0))
56                 return -EFAULT;
57
58         return 0;
59 }
60
61 /*
62  * Dump out the contents of some memory nicely...
63  */
64 static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
65 {
66         unsigned long p = bottom & ~31;
67         mm_segment_t fs;
68         int i;
69
70         /*
71          * We need to switch to kernel mode so that we can use __get_user
72          * to safely read from kernel space.  Note that we now dump the
73          * code first, just in case the backtrace kills us.
74          */
75         fs = get_fs();
76         set_fs(KERNEL_DS);
77
78         printk("%s", str);
79         printk("(0x%08lx to 0x%08lx)\n", bottom, top);
80
81         for (p = bottom & ~31; p < top;) {
82                 printk("%04lx: ", p & 0xffff);
83
84                 for (i = 0; i < 8; i++, p += 4) {
85                         unsigned int val;
86
87                         if (p < bottom || p >= top)
88                                 printk("         ");
89                         else {
90                                 __get_user(val, (unsigned long *)p);
91                                 printk("%08x ", val);
92                         }
93                 }
94                 printk ("\n");
95         }
96
97         set_fs(fs);
98 }
99
100 static void dump_instr(struct pt_regs *regs)
101 {
102         unsigned long addr = instruction_pointer(regs);
103         const int width = 8;
104         mm_segment_t fs;
105         int i;
106
107         /*
108          * We need to switch to kernel mode so that we can use __get_user
109          * to safely read from kernel space.  Note that we now dump the
110          * code first, just in case the backtrace kills us.
111          */
112         fs = get_fs();
113         set_fs(KERNEL_DS);
114
115         printk("Code: ");
116         for (i = -4; i < 1; i++) {
117                 unsigned int val, bad;
118
119                 bad = __get_user(val, &((u32 *)addr)[i]);
120
121                 if (!bad)
122                         printk(i == 0 ? "(%0*x) " : "%0*x ", width, val);
123                 else {
124                         printk("bad PC value.");
125                         break;
126                 }
127         }
128         printk("\n");
129
130         set_fs(fs);
131 }
132
133 /*static*/ void __dump_stack(struct task_struct *tsk, unsigned long sp)
134 {
135         dump_mem("Stack: ", sp, 8192+(unsigned long)task_stack_page(tsk));
136 }
137
138 void dump_stack(void)
139 {
140 #ifdef CONFIG_DEBUG_ERRORS
141         __backtrace();
142 #endif
143 }
144
145 EXPORT_SYMBOL(dump_stack);
146
147 //FIXME - was a static fn
148 void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
149 {
150         unsigned int fp;
151         int ok = 1;
152
153         printk("Backtrace: ");
154         fp = regs->ARM_fp;
155         if (!fp) {
156                 printk("no frame pointer");
157                 ok = 0;
158         } else if (verify_stack(fp)) {
159                 printk("invalid frame pointer 0x%08x", fp);
160                 ok = 0;
161         } else if (fp < (unsigned long)end_of_stack(tsk))
162                 printk("frame pointer underflow");
163         printk("\n");
164
165         if (ok)
166                 c_backtrace(fp, processor_mode(regs));
167 }
168
169 /* FIXME - this is probably wrong.. */
170 void show_stack(struct task_struct *task, unsigned long *sp) {
171         dump_mem("Stack: ", (unsigned long)sp, 8192+(unsigned long)task_stack_page(task));
172 }
173
174 DEFINE_SPINLOCK(die_lock);
175
176 /*
177  * This function is protected against re-entrancy.
178  */
179 NORET_TYPE void die(const char *str, struct pt_regs *regs, int err)
180 {
181         struct task_struct *tsk = current;
182
183         console_verbose();
184         spin_lock_irq(&die_lock);
185
186         printk("Internal error: %s: %x\n", str, err);
187         printk("CPU: %d\n", smp_processor_id());
188         show_regs(regs);
189         printk("Process %s (pid: %d[#%u], stack limit = 0x%p)\n",
190                 current->comm, current->pid,
191                 current->xid, end_of_stack(tsk));
192
193         if (!user_mode(regs) || in_interrupt()) {
194                 __dump_stack(tsk, (unsigned long)(regs + 1));
195                 dump_backtrace(regs, tsk);
196                 dump_instr(regs);
197         }
198 while(1);
199         spin_unlock_irq(&die_lock);
200         do_exit(SIGSEGV);
201 }
202
203 void die_if_kernel(const char *str, struct pt_regs *regs, int err)
204 {
205         if (user_mode(regs))
206                 return;
207
208         die(str, regs, err);
209 }
210
211 static DEFINE_MUTEX(undef_mutex);
212 static int (*undef_hook)(struct pt_regs *);
213
214 int request_undef_hook(int (*fn)(struct pt_regs *))
215 {
216         int ret = -EBUSY;
217
218         mutex_lock(&undef_mutex);
219         if (undef_hook == NULL) {
220                 undef_hook = fn;
221                 ret = 0;
222         }
223         mutex_unlock(&undef_mutex);
224
225         return ret;
226 }
227
228 int release_undef_hook(int (*fn)(struct pt_regs *))
229 {
230         int ret = -EINVAL;
231
232         mutex_lock(&undef_mutex);
233         if (undef_hook == fn) {
234                 undef_hook = NULL;
235                 ret = 0;
236         }
237         mutex_unlock(&undef_mutex);
238
239         return ret;
240 }
241
242 static int undefined_extension(struct pt_regs *regs, unsigned int op)
243 {
244         switch (op) {
245         case 1: /* 0xde01 / 0x?7f001f0 */
246                 ptrace_break(current, regs);
247                 return 0;
248         }
249         return 1;
250 }
251
252 asmlinkage void do_undefinstr(struct pt_regs *regs)
253 {
254         siginfo_t info;
255         void *pc;
256
257         regs->ARM_pc -= 4;
258
259         pc = (unsigned long *)instruction_pointer(regs); /* strip PSR */
260
261         if (user_mode(regs)) {
262                 u32 instr;
263
264                 get_user(instr, (u32 *)pc);
265
266                 if ((instr & 0x0fff00ff) == 0x07f000f0 &&
267                     undefined_extension(regs, (instr >> 8) & 255) == 0) {
268                         regs->ARM_pc += 4;
269                         return;
270                 }
271         } else {
272                 if (undef_hook && undef_hook(regs) == 0) {
273                         regs->ARM_pc += 4;
274                         return;
275                 }
276         }
277
278 #ifdef CONFIG_DEBUG_USER
279         printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n",
280                 current->comm, current->pid, pc);
281         dump_instr(regs);
282 #endif
283
284         current->thread.error_code = 0;
285         current->thread.trap_no = 6;
286
287         info.si_signo = SIGILL;
288         info.si_errno = 0;
289         info.si_code  = ILL_ILLOPC;
290         info.si_addr  = pc;
291
292         force_sig_info(SIGILL, &info, current);
293
294         die_if_kernel("Oops - undefined instruction", regs, 0);
295 }
296
297 asmlinkage void do_excpt(unsigned long address, struct pt_regs *regs, int mode)
298 {
299         siginfo_t info;
300
301 #ifdef CONFIG_DEBUG_USER
302         printk(KERN_INFO "%s (%d): address exception: pc=%08lx\n",
303                 current->comm, current->pid, instruction_pointer(regs));
304         dump_instr(regs);
305 #endif
306
307         current->thread.error_code = 0;
308         current->thread.trap_no = 11;
309
310         info.si_signo = SIGBUS;
311         info.si_errno = 0;
312         info.si_code  = BUS_ADRERR;
313         info.si_addr  = (void *)address;
314
315         force_sig_info(SIGBUS, &info, current);
316
317         die_if_kernel("Oops - address exception", regs, mode);
318 }
319
320 asmlinkage void do_unexp_fiq (struct pt_regs *regs)
321 {
322 #ifndef CONFIG_IGNORE_FIQ
323         printk("Hmm.  Unexpected FIQ received, but trying to continue\n");
324         printk("You may have a hardware problem...\n");
325 #endif
326 }
327
328 /*
329  * bad_mode handles the impossible case in the vectors.  If you see one of
330  * these, then it's extremely serious, and could mean you have buggy hardware.
331  * It never returns, and never tries to sync.  We hope that we can at least
332  * dump out some state information...
333  */
334 asmlinkage void bad_mode(struct pt_regs *regs, int reason, int proc_mode)
335 {
336         unsigned int vectors = vectors_base();
337
338         console_verbose();
339
340         printk(KERN_CRIT "Bad mode in %s handler detected: mode %s\n",
341                 handler[reason<5?reason:4], processor_modes[proc_mode]);
342
343         /*
344          * Dump out the vectors and stub routines.  Maybe a better solution
345          * would be to dump them out only if we detect that they are corrupted.
346          */
347         dump_mem(KERN_CRIT "Vectors: ", vectors, vectors + 0x40);
348         dump_mem(KERN_CRIT "Stubs: ", vectors + 0x200, vectors + 0x4b8);
349
350         die("Oops", regs, 0);
351         local_irq_disable();
352         panic("bad mode");
353 }
354
355 static int bad_syscall(int n, struct pt_regs *regs)
356 {
357         struct thread_info *thread = current_thread_info();
358         siginfo_t info;
359
360         if (current->personality != PER_LINUX && thread->exec_domain->handler) {
361                 thread->exec_domain->handler(n, regs);
362                 return regs->ARM_r0;
363         }
364
365 #ifdef CONFIG_DEBUG_USER
366         printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n",
367                 current->pid, current->comm, n);
368         dump_instr(regs);
369 #endif
370
371         info.si_signo = SIGILL;
372         info.si_errno = 0;
373         info.si_code  = ILL_ILLTRP;
374         info.si_addr  = (void *)instruction_pointer(regs) - 4;
375
376         force_sig_info(SIGILL, &info, current);
377         die_if_kernel("Oops", regs, n);
378         return regs->ARM_r0;
379 }
380
381 static inline void
382 do_cache_op(unsigned long start, unsigned long end, int flags)
383 {
384         struct vm_area_struct *vma;
385
386         if (end < start)
387                 return;
388
389         vma = find_vma(current->active_mm, start);
390         if (vma && vma->vm_start < end) {
391                 if (start < vma->vm_start)
392                         start = vma->vm_start;
393                 if (end > vma->vm_end)
394                         end = vma->vm_end;
395         }
396 }
397
398 /*
399  * Handle all unrecognised system calls.
400  *  0x9f0000 - 0x9fffff are some more esoteric system calls
401  */
402 #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
403 asmlinkage int arm_syscall(int no, struct pt_regs *regs)
404 {
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                 force_sig_info(SIGSEGV, &info, current);
418
419                 die_if_kernel("branch through zero", regs, 0);
420                 return 0;
421
422         case NR(breakpoint): /* SWI BREAK_POINT */
423                 ptrace_break(current, regs);
424                 return regs->ARM_r0;
425
426         case NR(cacheflush):
427                 return 0;
428
429         case NR(usr26):
430                 break;
431
432         default:
433                 /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
434                    if not implemented, rather than raising SIGILL.  This
435                    way the calling program can gracefully determine whether
436                    a feature is supported.  */
437                 if (no <= 0x7ff)
438                         return -ENOSYS;
439                 break;
440         }
441 #ifdef CONFIG_DEBUG_USER
442         /*
443          * experience shows that these seem to indicate that
444          * something catastrophic has happened
445          */
446         printk("[%d] %s: arm syscall %d\n", current->pid, current->comm, no);
447         dump_instr(regs);
448         if (user_mode(regs)) {
449                 show_regs(regs);
450                 c_backtrace(regs->ARM_fp, processor_mode(regs));
451         }
452 #endif
453         info.si_signo = SIGILL;
454         info.si_errno = 0;
455         info.si_code  = ILL_ILLTRP;
456         info.si_addr  = (void *)instruction_pointer(regs) - 4;
457
458         force_sig_info(SIGILL, &info, current);
459         die_if_kernel("Oops", regs, no);
460         return 0;
461 }
462
463 void __bad_xchg(volatile void *ptr, int size)
464 {
465         printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
466                 __builtin_return_address(0), ptr, size);
467         BUG();
468 }
469
470 /*
471  * A data abort trap was taken, but we did not handle the instruction.
472  * Try to abort the user program, or panic if it was the kernel.
473  */
474 asmlinkage void
475 baddataabort(int code, unsigned long instr, struct pt_regs *regs)
476 {
477         unsigned long addr = instruction_pointer(regs);
478         siginfo_t info;
479
480 #ifdef CONFIG_DEBUG_USER
481         printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n",
482                 current->pid, current->comm, code, instr);
483         dump_instr(regs);
484         show_pte(current->mm, addr);
485 #endif
486
487         info.si_signo = SIGILL;
488         info.si_errno = 0;
489         info.si_code  = ILL_ILLOPC;
490         info.si_addr  = (void *)addr;
491
492         force_sig_info(SIGILL, &info, current);
493         die_if_kernel("unknown data abort code", regs, instr);
494 }
495
496 volatile void __bug(const char *file, int line, void *data)
497 {
498         printk(KERN_CRIT"kernel BUG at %s:%d!", file, line);
499         if (data)
500                 printk(KERN_CRIT" - extra data = %p", data);
501         printk("\n");
502         *(int *)0 = 0;
503 }
504
505 void __readwrite_bug(const char *fn)
506 {
507         printk("%s called, but not implemented", fn);
508         BUG();
509 }
510
511 void __pte_error(const char *file, int line, unsigned long val)
512 {
513         printk("%s:%d: bad pte %08lx.\n", file, line, val);
514 }
515
516 void __pmd_error(const char *file, int line, unsigned long val)
517 {
518         printk("%s:%d: bad pmd %08lx.\n", file, line, val);
519 }
520
521 void __pgd_error(const char *file, int line, unsigned long val)
522 {
523         printk("%s:%d: bad pgd %08lx.\n", file, line, val);
524 }
525
526 asmlinkage void __div0(void)
527 {
528         printk("Division by zero in kernel.\n");
529         dump_stack();
530 }
531
532 void abort(void)
533 {
534         BUG();
535
536         /* if that doesn't kill us, halt */
537         panic("Oops failed to kill thread");
538 }
539
540 void __init trap_init(void)
541 {
542         extern void __trap_init(unsigned long);
543         unsigned long base = vectors_base();
544
545         __trap_init(base);
546         if (base != 0)
547                 printk(KERN_DEBUG "Relocating machine vectors to 0x%08lx\n",
548                         base);
549 }