traps.c 12 KB

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