1
2
3
4
5
6
7
8
9
10
11
12
13
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
34extern void c_backtrace (unsigned long fp, int pmode);
35extern void show_pte(struct mm_struct *mm, unsigned long addr);
36
37const 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
44static const char *handler[]= { "prefetch abort", "data abort", "address exception", "interrupt" };
45
46#ifdef CONFIG_DEBUG_USER
47unsigned int user_debug;
48
49static 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
57void 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
71
72
73
74static 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
84
85static 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
93
94
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
120static 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
130
131
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
157static 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
178void dump_stack(void)
179{
180#ifdef CONFIG_DEBUG_ERRORS
181 __backtrace();
182#endif
183}
184
185EXPORT_SYMBOL(dump_stack);
186
187void 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
203DEFINE_SPINLOCK(die_lock);
204
205
206
207
208NORET_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
235void 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
243static LIST_HEAD(undef_hook);
244static DEFINE_SPINLOCK(undef_lock);
245
246void register_undef_hook(struct undef_hook *hook)
247{
248 spin_lock_irq(&undef_lock);
249 list_add(&hook->node, &undef_hook);
250 spin_unlock_irq(&undef_lock);
251}
252
253void unregister_undef_hook(struct undef_hook *hook)
254{
255 spin_lock_irq(&undef_lock);
256 list_del(&hook->node);
257 spin_unlock_irq(&undef_lock);
258}
259
260asmlinkage void do_undefinstr(struct pt_regs *regs)
261{
262 unsigned int correction = thumb_mode(regs) ? 2 : 4;
263 unsigned int instr;
264 struct undef_hook *hook;
265 siginfo_t info;
266 void __user *pc;
267
268
269
270
271
272
273 regs->ARM_pc -= correction;
274
275 pc = (void __user *)instruction_pointer(regs);
276 if (thumb_mode(regs)) {
277 get_user(instr, (u16 __user *)pc);
278 } else {
279 get_user(instr, (u32 __user *)pc);
280 }
281
282 spin_lock_irq(&undef_lock);
283 list_for_each_entry(hook, &undef_hook, node) {
284 if ((instr & hook->instr_mask) == hook->instr_val &&
285 (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val) {
286 if (hook->fn(regs, instr) == 0) {
287 spin_unlock_irq(&undef_lock);
288 return;
289 }
290 }
291 }
292 spin_unlock_irq(&undef_lock);
293
294#ifdef CONFIG_DEBUG_USER
295 if (user_debug & UDBG_UNDEFINED) {
296 printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n",
297 current->comm, current->pid, pc);
298 dump_instr(regs);
299 }
300#endif
301
302 current->thread.error_code = 0;
303 current->thread.trap_no = 6;
304
305 info.si_signo = SIGILL;
306 info.si_errno = 0;
307 info.si_code = ILL_ILLOPC;
308 info.si_addr = pc;
309
310 force_sig_info(SIGILL, &info, current);
311
312 die_if_kernel("Oops - undefined instruction", regs, 0);
313}
314
315asmlinkage void do_unexp_fiq (struct pt_regs *regs)
316{
317#ifndef CONFIG_IGNORE_FIQ
318 printk("Hmm. Unexpected FIQ received, but trying to continue\n");
319 printk("You may have a hardware problem...\n");
320#endif
321}
322
323
324
325
326
327
328
329asmlinkage void bad_mode(struct pt_regs *regs, int reason, int proc_mode)
330{
331 console_verbose();
332
333 printk(KERN_CRIT "Bad mode in %s handler detected: mode %s\n",
334 handler[reason], processor_modes[proc_mode]);
335
336 die("Oops - bad mode", regs, 0);
337 local_irq_disable();
338 panic("bad mode");
339}
340
341static int bad_syscall(int n, struct pt_regs *regs)
342{
343 struct thread_info *thread = current_thread_info();
344 siginfo_t info;
345
346 if (current->personality != PER_LINUX && thread->exec_domain->handler) {
347 thread->exec_domain->handler(n, regs);
348 return regs->ARM_r0;
349 }
350
351#ifdef CONFIG_DEBUG_USER
352 if (user_debug & UDBG_SYSCALL) {
353 printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n",
354 current->pid, current->comm, n);
355 dump_instr(regs);
356 }
357#endif
358
359 info.si_signo = SIGILL;
360 info.si_errno = 0;
361 info.si_code = ILL_ILLTRP;
362 info.si_addr = (void __user *)instruction_pointer(regs) -
363 (thumb_mode(regs) ? 2 : 4);
364
365 force_sig_info(SIGILL, &info, current);
366 die_if_kernel("Oops - bad syscall", regs, n);
367 return regs->ARM_r0;
368}
369
370static inline void
371do_cache_op(unsigned long start, unsigned long end, int flags)
372{
373 struct vm_area_struct *vma;
374
375 if (end < start || flags)
376 return;
377
378 vma = find_vma(current->active_mm, start);
379 if (vma && vma->vm_start < end) {
380 if (start < vma->vm_start)
381 start = vma->vm_start;
382 if (end > vma->vm_end)
383 end = vma->vm_end;
384
385 flush_cache_user_range(vma, start, end);
386 }
387}
388
389
390
391
392
393#define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
394asmlinkage int arm_syscall(int no, struct pt_regs *regs)
395{
396 struct thread_info *thread = current_thread_info();
397 siginfo_t info;
398
399 if ((no >> 16) != 0x9f)
400 return bad_syscall(no, regs);
401
402 switch (no & 0xffff) {
403 case 0:
404 info.si_signo = SIGSEGV;
405 info.si_errno = 0;
406 info.si_code = SEGV_MAPERR;
407 info.si_addr = NULL;
408
409 force_sig_info(SIGSEGV, &info, current);
410
411 die_if_kernel("branch through zero", regs, 0);
412 return 0;
413
414 case NR(breakpoint):
415 regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
416 ptrace_break(current, regs);
417 return regs->ARM_r0;
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433 case NR(cacheflush):
434 do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
435 return 0;
436
437 case NR(usr26):
438 if (!(elf_hwcap & HWCAP_26BIT))
439 break;
440 regs->ARM_cpsr &= ~MODE32_BIT;
441 return regs->ARM_r0;
442
443 case NR(usr32):
444 if (!(elf_hwcap & HWCAP_26BIT))
445 break;
446 regs->ARM_cpsr |= MODE32_BIT;
447 return regs->ARM_r0;
448
449 case NR(set_tls):
450 thread->tp_value = regs->ARM_r0;
451
452
453
454
455
456
457 *((unsigned long *)0xffff0ffc) = thread->tp_value;
458 return 0;
459
460 default:
461
462
463
464
465 if (no <= 0x7ff)
466 return -ENOSYS;
467 break;
468 }
469#ifdef CONFIG_DEBUG_USER
470
471
472
473
474 if (user_debug & UDBG_SYSCALL) {
475 printk("[%d] %s: arm syscall %d\n",
476 current->pid, current->comm, no);
477 dump_instr(regs);
478 if (user_mode(regs)) {
479 show_regs(regs);
480 c_backtrace(regs->ARM_fp, processor_mode(regs));
481 }
482 }
483#endif
484 info.si_signo = SIGILL;
485 info.si_errno = 0;
486 info.si_code = ILL_ILLTRP;
487 info.si_addr = (void __user *)instruction_pointer(regs) -
488 (thumb_mode(regs) ? 2 : 4);
489
490 force_sig_info(SIGILL, &info, current);
491 die_if_kernel("Oops - bad syscall(2)", regs, no);
492 return 0;
493}
494
495void __bad_xchg(volatile void *ptr, int size)
496{
497 printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
498 __builtin_return_address(0), ptr, size);
499 BUG();
500}
501EXPORT_SYMBOL(__bad_xchg);
502
503
504
505
506
507asmlinkage void
508baddataabort(int code, unsigned long instr, struct pt_regs *regs)
509{
510 unsigned long addr = instruction_pointer(regs);
511 siginfo_t info;
512
513#ifdef CONFIG_DEBUG_USER
514 if (user_debug & UDBG_BADABORT) {
515 printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n",
516 current->pid, current->comm, code, instr);
517 dump_instr(regs);
518 show_pte(current->mm, addr);
519 }
520#endif
521
522 info.si_signo = SIGILL;
523 info.si_errno = 0;
524 info.si_code = ILL_ILLOPC;
525 info.si_addr = (void __user *)addr;
526
527 force_sig_info(SIGILL, &info, current);
528 die_if_kernel("unknown data abort code", regs, instr);
529}
530
531volatile void __bug(const char *file, int line, void *data)
532{
533 printk(KERN_CRIT"kernel BUG at %s:%d!", file, line);
534 if (data)
535 printk(" - extra data = %p", data);
536 printk("\n");
537 *(int *)0 = 0;
538}
539EXPORT_SYMBOL(__bug);
540
541void __readwrite_bug(const char *fn)
542{
543 printk("%s called, but not implemented\n", fn);
544 BUG();
545}
546EXPORT_SYMBOL(__readwrite_bug);
547
548void __pte_error(const char *file, int line, unsigned long val)
549{
550 printk("%s:%d: bad pte %08lx.\n", file, line, val);
551}
552
553void __pmd_error(const char *file, int line, unsigned long val)
554{
555 printk("%s:%d: bad pmd %08lx.\n", file, line, val);
556}
557
558void __pgd_error(const char *file, int line, unsigned long val)
559{
560 printk("%s:%d: bad pgd %08lx.\n", file, line, val);
561}
562
563asmlinkage void __div0(void)
564{
565 printk("Division by zero in kernel.\n");
566 dump_stack();
567}
568EXPORT_SYMBOL(__div0);
569
570void abort(void)
571{
572 BUG();
573
574
575 panic("Oops failed to kill thread");
576}
577EXPORT_SYMBOL(abort);
578
579void __init trap_init(void)
580{
581 extern void __trap_init(void);
582
583 __trap_init();
584 flush_icache_range(0xffff0000, 0xffff0000 + PAGE_SIZE);
585 modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
586}
587