traps.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:13k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/arch/arm/kernel/traps.c
  3.  *
  4.  *  Copyright (C) 1995, 1996 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/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/signal.h>
  19. #include <linux/sched.h>
  20. #include <linux/mm.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/personality.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/elf.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/init.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/system.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/unistd.h>
  31. #include "ptrace.h"
  32. extern void c_backtrace (unsigned long fp, int pmode);
  33. extern void show_pte(struct mm_struct *mm, unsigned long addr);
  34. const char *processor_modes[]=
  35. { "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" ,
  36.   "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26",
  37.   "USER_32", "FIQ_32" , "IRQ_32" , "SVC_32" , "UK4_32" , "UK5_32" , "UK6_32" , "ABT_32" ,
  38.   "UK8_32" , "UK9_32" , "UK10_32", "UND_32" , "UK12_32", "UK13_32", "UK14_32", "SYS_32"
  39. };
  40. static const char *handler[]= { "prefetch abort", "data abort", "address exception", "interrupt" };
  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. int i;
  59. printk("%s(0x%08lx to 0x%08lx)n", str, bottom, top);
  60. for (p = bottom & ~31; p < top;) {
  61. printk("%04lx: ", p & 0xffff);
  62. for (i = 0; i < 8; i++, p += 4) {
  63. unsigned int val;
  64. if (p < bottom || p >= top)
  65. printk("         ");
  66. else {
  67. __get_user(val, (unsigned long *)p);
  68. printk("%08x ", val);
  69. }
  70. }
  71. printk ("n");
  72. }
  73. }
  74. static void dump_instr(struct pt_regs *regs)
  75. {
  76. unsigned long addr = instruction_pointer(regs);
  77. const int thumb = thumb_mode(regs);
  78. const int width = thumb ? 4 : 8;
  79. int i;
  80. printk("Code: ");
  81. for (i = -4; i < 1; i++) {
  82. unsigned int val, bad;
  83. if (thumb)
  84. bad = __get_user(val, &((u16 *)addr)[i]);
  85. else
  86. bad = __get_user(val, &((u32 *)addr)[i]);
  87. if (!bad)
  88. printk(i == 0 ? "(%0*x) " : "%0*x ", width, val);
  89. else {
  90. printk("bad PC value.");
  91. break;
  92. }
  93. }
  94. printk("n");
  95. }
  96. static void dump_stack(struct task_struct *tsk, unsigned long sp)
  97. {
  98. dump_mem("Stack: ", sp - 16, 8192+(unsigned long)tsk);
  99. }
  100. static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  101. {
  102. unsigned int fp = regs->ARM_fp;
  103. char *msg = "";
  104. int ok = 1;
  105. #ifdef CONFIG_FRAME_POINTER
  106. if (!fp) {
  107. msg = "no frame pointer";
  108. ok = 0;
  109. } else if (verify_stack(fp)) {
  110. msg = "invalid frame pointer";
  111. ok = 0;
  112. } else if (fp < 4096+(unsigned long)tsk)
  113. msg = "frame pointer underflow";
  114. #else
  115. msg = "not available";
  116. ok = 0;
  117. #endif
  118. printk("Backtrace: %sn", msg);
  119. if (ok)
  120. c_backtrace(fp, processor_mode(regs));
  121. }
  122. /*
  123.  * This is called from SysRq-T (show_task) to display the current
  124.  * call trace for each process.  Very useful.
  125.  */
  126. void show_trace_task(struct task_struct *tsk)
  127. {
  128. if (tsk != current) {
  129. unsigned int fp = tsk->thread.save->fp;
  130. c_backtrace(fp, 0x10);
  131. }
  132. }
  133. spinlock_t die_lock = SPIN_LOCK_UNLOCKED;
  134. /*
  135.  * This function is protected against re-entrancy.
  136.  */
  137. NORET_TYPE void die(const char *str, struct pt_regs *regs, int err)
  138. {
  139. struct task_struct *tsk = current;
  140. console_verbose();
  141. spin_lock_irq(&die_lock);
  142. printk("Internal error: %s: %xn", str, err);
  143. printk("CPU: %dn", smp_processor_id());
  144. show_regs(regs);
  145. printk("Process %s (pid: %d, stackpage=%08lx)n",
  146. current->comm, current->pid, 4096+(unsigned long)tsk);
  147. if (!user_mode(regs) || in_interrupt()) {
  148. mm_segment_t fs;
  149. /*
  150.  * We need to switch to kernel mode so that we can
  151.  * use __get_user to safely read from kernel space.
  152.  * Note that we now dump the code first, just in case
  153.  * the backtrace kills us.
  154.  */
  155. fs = get_fs();
  156. set_fs(KERNEL_DS);
  157. dump_stack(tsk, (unsigned long)(regs + 1));
  158. dump_backtrace(regs, tsk);
  159. dump_instr(regs);
  160. set_fs(fs);
  161. }
  162. spin_unlock_irq(&die_lock);
  163. do_exit(SIGSEGV);
  164. }
  165. void die_if_kernel(const char *str, struct pt_regs *regs, int err)
  166. {
  167. if (user_mode(regs))
  168.      return;
  169.      die(str, regs, err);
  170. }
  171. asmlinkage void do_undefinstr(int address, struct pt_regs *regs, int mode)
  172. {
  173. unsigned long *pc;
  174. siginfo_t info;
  175. /*
  176.  * According to the ARM ARM, PC is 2 or 4 bytes ahead, depending
  177.  * whether we're in Thumb mode or not.
  178.  */
  179. regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
  180. pc = (unsigned long *)instruction_pointer(regs);
  181. #ifdef CONFIG_DEBUG_USER
  182. printk(KERN_INFO "%s (%d): undefined instruction: pc=%pn",
  183. current->comm, current->pid, pc);
  184. dump_instr(regs);
  185. #endif
  186. current->thread.error_code = 0;
  187. current->thread.trap_no = 6;
  188. info.si_signo = SIGILL;
  189. info.si_errno = 0;
  190. info.si_code  = ILL_ILLOPC;
  191. info.si_addr  = pc;
  192. force_sig_info(SIGILL, &info, current);
  193. die_if_kernel("Oops - undefined instruction", regs, mode);
  194. }
  195. #ifdef CONFIG_CPU_26
  196. asmlinkage void do_excpt(int address, struct pt_regs *regs, int mode)
  197. {
  198. siginfo_t info;
  199. #ifdef CONFIG_DEBUG_USER
  200. printk(KERN_INFO "%s (%d): address exception: pc=%08lxn",
  201. current->comm, current->pid, instruction_pointer(regs));
  202. dump_instr(regs);
  203. #endif
  204. current->thread.error_code = 0;
  205. current->thread.trap_no = 11;
  206. info.si_signo = SIGBUS;
  207. info.si_errno = 0;
  208. info.si_code  = BUS_ADRERR;
  209. info.si_addr  = (void *)address;
  210. force_sig_info(SIGBUS, &info, current);
  211. die_if_kernel("Oops - address exception", regs, mode);
  212. }
  213. #endif
  214. asmlinkage void do_unexp_fiq (struct pt_regs *regs)
  215. {
  216. #ifndef CONFIG_IGNORE_FIQ
  217. printk("Hmm.  Unexpected FIQ received, but trying to continuen");
  218. printk("You may have a hardware problem...n");
  219. #endif
  220. }
  221. /*
  222.  * bad_mode handles the impossible case in the vectors.  If you see one of
  223.  * these, then it's extremely serious, and could mean you have buggy hardware.
  224.  * It never returns, and never tries to sync.  We hope that we can at least
  225.  * dump out some state information...
  226.  */
  227. asmlinkage void bad_mode(struct pt_regs *regs, int reason, int proc_mode)
  228. {
  229. unsigned int vectors = vectors_base();
  230. mm_segment_t fs;
  231. console_verbose();
  232. printk(KERN_CRIT "Bad mode in %s handler detected: mode %sn",
  233. handler[reason], processor_modes[proc_mode]);
  234. /*
  235.  * We need to switch to kernel mode so that we can use __get_user
  236.  * to safely read from kernel space.  Note that we now dump the
  237.  * code first, just in case the backtrace kills us.
  238.  */
  239. fs = get_fs();
  240. set_fs(KERNEL_DS);
  241. /*
  242.  * Dump out the vectors and stub routines.  Maybe a better solution
  243.  * would be to dump them out only if we detect that they are corrupted.
  244.  */
  245. dump_mem(KERN_CRIT "Vectors: ", vectors, vectors + 0x40);
  246. dump_mem(KERN_CRIT "Stubs: ", vectors + 0x200, vectors + 0x4b8);
  247. set_fs(fs);
  248. die("Oops", regs, 0);
  249. cli();
  250. panic("bad mode");
  251. }
  252. static int bad_syscall(int n, struct pt_regs *regs)
  253. {
  254. siginfo_t info;
  255. /* You might think just testing `handler' would be enough, but PER_LINUX
  256.  * points it to no_lcall7 to catch undercover SVr4 binaries.  Gutted.
  257.  */
  258. if (current->personality != PER_LINUX && current->exec_domain->handler) {
  259. /* Hand it off to iBCS.  The extra parameter and consequent type 
  260.  * forcing is necessary because of the weird ARM calling convention.
  261.  */
  262. current->exec_domain->handler(n, regs);
  263. return regs->ARM_r0;
  264. }
  265. #ifdef CONFIG_DEBUG_USER
  266. printk(KERN_ERR "[%d] %s: obsolete system call %08x.n",
  267. current->pid, current->comm, n);
  268. dump_instr(regs);
  269. #endif
  270. info.si_signo = SIGILL;
  271. info.si_errno = 0;
  272. info.si_code  = ILL_ILLTRP;
  273. info.si_addr  = (void *)instruction_pointer(regs) -
  274.  (thumb_mode(regs) ? 2 : 4);
  275. force_sig_info(SIGILL, &info, current);
  276. die_if_kernel("Oops", regs, n);
  277. return regs->ARM_r0;
  278. }
  279. /*
  280.  * Handle all unrecognised system calls.
  281.  *  0x9f0000 - 0x9fffff are some more esoteric system calls
  282.  */
  283. #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
  284. asmlinkage int arm_syscall(int no, struct pt_regs *regs)
  285. {
  286. siginfo_t info;
  287. if ((no >> 16) != 0x9f)
  288. return bad_syscall(no, regs);
  289. switch (no & 0xffff) {
  290. case 0: /* branch through 0 */
  291. info.si_signo = SIGSEGV;
  292. info.si_errno = 0;
  293. info.si_code  = SEGV_MAPERR;
  294. info.si_addr  = NULL;
  295. force_sig_info(SIGSEGV, &info, current);
  296. die_if_kernel("branch through zero", regs, 0);
  297. return 0;
  298. case NR(breakpoint): /* SWI BREAK_POINT */
  299. /*
  300.  * The PC is always left pointing at the next
  301.  * instruction.  Fix this.
  302.  */
  303. regs->ARM_pc -= 4;
  304. __ptrace_cancel_bpt(current);
  305. info.si_signo = SIGTRAP;
  306. info.si_errno = 0;
  307. info.si_code  = TRAP_BRKPT;
  308. info.si_addr  = (void *)instruction_pointer(regs) -
  309.  (thumb_mode(regs) ? 2 : 4);
  310. force_sig_info(SIGTRAP, &info, current);
  311. return regs->ARM_r0;
  312. #ifdef CONFIG_CPU_32
  313. /*
  314.  * Flush a region from virtual address 'r0' to virtual address 'r1'
  315.  * _inclusive_.  There is no alignment requirement on either address;
  316.  * user space does not need to know the hardware cache layout.
  317.  *
  318.  * r2 contains flags.  It should ALWAYS be passed as ZERO until it
  319.  * is defined to be something else.  For now we ignore it, but may
  320.  * the fires of hell burn in your belly if you break this rule. ;)
  321.  *
  322.  * (at a later date, we may want to allow this call to not flush
  323.  * various aspects of the cache.  Passing '0' will guarantee that
  324.  * everything necessary gets flushed to maintain consistency in
  325.  * the specified region).
  326.  */
  327. case NR(cacheflush):
  328. cpu_cache_clean_invalidate_range(regs->ARM_r0, regs->ARM_r1, 1);
  329. return 0;
  330. case NR(usr26):
  331. if (!(elf_hwcap & HWCAP_26BIT))
  332. break;
  333. regs->ARM_cpsr &= ~0x10;
  334. return regs->ARM_r0;
  335. case NR(usr32):
  336. if (!(elf_hwcap & HWCAP_26BIT))
  337. break;
  338. regs->ARM_cpsr |= 0x10;
  339. return regs->ARM_r0;
  340. #else
  341. case NR(cacheflush):
  342. return 0;
  343. case NR(usr26):
  344. case NR(usr32):
  345. break;
  346. #endif
  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 %dn", 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) -
  372.  (thumb_mode(regs) ? 2 : 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 %dn",
  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%08lxn",
  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. 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. __backtrace();
  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(void *);
  444. __trap_init((void *)vectors_base());
  445. if (vectors_base() != 0)
  446. printk(KERN_DEBUG "Relocating machine vectors to 0x%08xn",
  447. vectors_base());
  448. #ifdef CONFIG_CPU_32
  449. modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
  450. #endif
  451. }