traps.c 12 KB

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