traps.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * linux/arch/arm/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. *
  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/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. #include <asm/atomic.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/io.h>
  26. #include <asm/system.h>
  27. #include <asm/uaccess.h>
  28. #include <asm/unistd.h>
  29. #include <asm/traps.h>
  30. #include "ptrace.h"
  31. const char *processor_modes[]=
  32. { "USER_26", "FIQ_26" , "IRQ_26" , "SVC_26" , "UK4_26" , "UK5_26" , "UK6_26" , "UK7_26" ,
  33. "UK8_26" , "UK9_26" , "UK10_26", "UK11_26", "UK12_26", "UK13_26", "UK14_26", "UK15_26",
  34. "USER_32", "FIQ_32" , "IRQ_32" , "SVC_32" , "UK4_32" , "UK5_32" , "UK6_32" , "ABT_32" ,
  35. "UK8_32" , "UK9_32" , "UK10_32", "UND_32" , "UK12_32", "UK13_32", "UK14_32", "SYS_32"
  36. };
  37. static const char *handler[]= { "prefetch abort", "data abort", "address exception", "interrupt" };
  38. #ifdef CONFIG_DEBUG_USER
  39. unsigned int user_debug;
  40. static int __init user_debug_setup(char *str)
  41. {
  42. get_option(&str, &user_debug);
  43. return 1;
  44. }
  45. __setup("user_debug=", user_debug_setup);
  46. #endif
  47. void dump_backtrace_entry(unsigned long where, unsigned long from)
  48. {
  49. #ifdef CONFIG_KALLSYMS
  50. printk("[<%08lx>] ", where);
  51. print_symbol("(%s) ", where);
  52. printk("from [<%08lx>] ", from);
  53. print_symbol("(%s)\n", from);
  54. #else
  55. printk("Function entered at [<%08lx>] from [<%08lx>]\n", where, from);
  56. #endif
  57. }
  58. /*
  59. * Stack pointers should always be within the kernels view of
  60. * physical memory. If it is not there, then we can't dump
  61. * out any information relating to the stack.
  62. */
  63. static int verify_stack(unsigned long sp)
  64. {
  65. if (sp < PAGE_OFFSET || (sp > (unsigned long)high_memory && high_memory != 0))
  66. return -EFAULT;
  67. return 0;
  68. }
  69. /*
  70. * Dump out the contents of some memory nicely...
  71. */
  72. static void dump_mem(const char *str, unsigned long bottom, unsigned long top)
  73. {
  74. unsigned long p = bottom & ~31;
  75. mm_segment_t fs;
  76. int i;
  77. /*
  78. * We need to switch to kernel mode so that we can use __get_user
  79. * to safely read from kernel space. Note that we now dump the
  80. * code first, just in case the backtrace kills us.
  81. */
  82. fs = get_fs();
  83. set_fs(KERNEL_DS);
  84. printk("%s(0x%08lx to 0x%08lx)\n", str, bottom, top);
  85. for (p = bottom & ~31; p < top;) {
  86. printk("%04lx: ", p & 0xffff);
  87. for (i = 0; i < 8; i++, p += 4) {
  88. unsigned int val;
  89. if (p < bottom || p >= top)
  90. printk(" ");
  91. else {
  92. __get_user(val, (unsigned long *)p);
  93. printk("%08x ", val);
  94. }
  95. }
  96. printk ("\n");
  97. }
  98. set_fs(fs);
  99. }
  100. static void dump_instr(struct pt_regs *regs)
  101. {
  102. unsigned long addr = instruction_pointer(regs);
  103. const int thumb = thumb_mode(regs);
  104. const int width = thumb ? 4 : 8;
  105. mm_segment_t fs;
  106. int i;
  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. printk("Code: ");
  115. for (i = -4; i < 1; i++) {
  116. unsigned int val, bad;
  117. if (thumb)
  118. bad = __get_user(val, &((u16 *)addr)[i]);
  119. else
  120. bad = __get_user(val, &((u32 *)addr)[i]);
  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. set_fs(fs);
  130. }
  131. static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)
  132. {
  133. unsigned int fp;
  134. int ok = 1;
  135. printk("Backtrace: ");
  136. fp = regs->ARM_fp;
  137. if (!fp) {
  138. printk("no frame pointer");
  139. ok = 0;
  140. } else if (verify_stack(fp)) {
  141. printk("invalid frame pointer 0x%08x", fp);
  142. ok = 0;
  143. } else if (fp < (unsigned long)(tsk->thread_info + 1))
  144. printk("frame pointer underflow");
  145. printk("\n");
  146. if (ok)
  147. c_backtrace(fp, processor_mode(regs));
  148. }
  149. void dump_stack(void)
  150. {
  151. #ifdef CONFIG_DEBUG_ERRORS
  152. __backtrace();
  153. #endif
  154. }
  155. EXPORT_SYMBOL(dump_stack);
  156. void show_stack(struct task_struct *tsk, unsigned long *sp)
  157. {
  158. unsigned long fp;
  159. if (!tsk)
  160. tsk = current;
  161. if (tsk != current)
  162. fp = thread_saved_fp(tsk);
  163. else
  164. asm("mov%? %0, fp" : "=r" (fp));
  165. c_backtrace(fp, 0x10);
  166. barrier();
  167. }
  168. DEFINE_SPINLOCK(die_lock);
  169. /*
  170. * This function is protected against re-entrancy.
  171. */
  172. NORET_TYPE void die(const char *str, struct pt_regs *regs, int err)
  173. {
  174. struct task_struct *tsk = current;
  175. static int die_counter;
  176. console_verbose();
  177. spin_lock_irq(&die_lock);
  178. bust_spinlocks(1);
  179. printk("Internal error: %s: %x [#%d]\n", str, err, ++die_counter);
  180. print_modules();
  181. __show_regs(regs);
  182. printk("Process %s (pid: %d, stack limit = 0x%p)\n",
  183. tsk->comm, tsk->pid, tsk->thread_info + 1);
  184. if (!user_mode(regs) || in_interrupt()) {
  185. dump_mem("Stack: ", regs->ARM_sp, 8192+(unsigned long)tsk->thread_info);
  186. dump_backtrace(regs, tsk);
  187. dump_instr(regs);
  188. }
  189. bust_spinlocks(0);
  190. spin_unlock_irq(&die_lock);
  191. do_exit(SIGSEGV);
  192. }
  193. void die_if_kernel(const char *str, struct pt_regs *regs, int err)
  194. {
  195. if (user_mode(regs))
  196. return;
  197. die(str, regs, err);
  198. }
  199. static void notify_die(const char *str, struct pt_regs *regs, siginfo_t *info,
  200. unsigned long err, unsigned long trap)
  201. {
  202. if (user_mode(regs)) {
  203. current->thread.error_code = err;
  204. current->thread.trap_no = trap;
  205. force_sig_info(info->si_signo, info, current);
  206. } else {
  207. die(str, regs, err);
  208. }
  209. }
  210. static LIST_HEAD(undef_hook);
  211. static DEFINE_SPINLOCK(undef_lock);
  212. void register_undef_hook(struct undef_hook *hook)
  213. {
  214. spin_lock_irq(&undef_lock);
  215. list_add(&hook->node, &undef_hook);
  216. spin_unlock_irq(&undef_lock);
  217. }
  218. void unregister_undef_hook(struct undef_hook *hook)
  219. {
  220. spin_lock_irq(&undef_lock);
  221. list_del(&hook->node);
  222. spin_unlock_irq(&undef_lock);
  223. }
  224. asmlinkage void do_undefinstr(struct pt_regs *regs)
  225. {
  226. unsigned int correction = thumb_mode(regs) ? 2 : 4;
  227. unsigned int instr;
  228. struct undef_hook *hook;
  229. siginfo_t info;
  230. void __user *pc;
  231. /*
  232. * According to the ARM ARM, PC is 2 or 4 bytes ahead,
  233. * depending whether we're in Thumb mode or not.
  234. * Correct this offset.
  235. */
  236. regs->ARM_pc -= correction;
  237. pc = (void __user *)instruction_pointer(regs);
  238. if (thumb_mode(regs)) {
  239. get_user(instr, (u16 __user *)pc);
  240. } else {
  241. get_user(instr, (u32 __user *)pc);
  242. }
  243. spin_lock_irq(&undef_lock);
  244. list_for_each_entry(hook, &undef_hook, node) {
  245. if ((instr & hook->instr_mask) == hook->instr_val &&
  246. (regs->ARM_cpsr & hook->cpsr_mask) == hook->cpsr_val) {
  247. if (hook->fn(regs, instr) == 0) {
  248. spin_unlock_irq(&undef_lock);
  249. return;
  250. }
  251. }
  252. }
  253. spin_unlock_irq(&undef_lock);
  254. #ifdef CONFIG_DEBUG_USER
  255. if (user_debug & UDBG_UNDEFINED) {
  256. printk(KERN_INFO "%s (%d): undefined instruction: pc=%p\n",
  257. current->comm, current->pid, pc);
  258. dump_instr(regs);
  259. }
  260. #endif
  261. info.si_signo = SIGILL;
  262. info.si_errno = 0;
  263. info.si_code = ILL_ILLOPC;
  264. info.si_addr = pc;
  265. notify_die("Oops - undefined instruction", regs, &info, 0, 6);
  266. }
  267. asmlinkage void do_unexp_fiq (struct pt_regs *regs)
  268. {
  269. #ifndef CONFIG_IGNORE_FIQ
  270. printk("Hmm. Unexpected FIQ received, but trying to continue\n");
  271. printk("You may have a hardware problem...\n");
  272. #endif
  273. }
  274. /*
  275. * bad_mode handles the impossible case in the vectors. If you see one of
  276. * these, then it's extremely serious, and could mean you have buggy hardware.
  277. * It never returns, and never tries to sync. We hope that we can at least
  278. * dump out some state information...
  279. */
  280. asmlinkage void bad_mode(struct pt_regs *regs, int reason, int proc_mode)
  281. {
  282. console_verbose();
  283. printk(KERN_CRIT "Bad mode in %s handler detected: mode %s\n",
  284. handler[reason], processor_modes[proc_mode]);
  285. die("Oops - bad mode", regs, 0);
  286. local_irq_disable();
  287. panic("bad mode");
  288. }
  289. static int bad_syscall(int n, struct pt_regs *regs)
  290. {
  291. struct thread_info *thread = current_thread_info();
  292. siginfo_t info;
  293. if (current->personality != PER_LINUX && thread->exec_domain->handler) {
  294. thread->exec_domain->handler(n, regs);
  295. return regs->ARM_r0;
  296. }
  297. #ifdef CONFIG_DEBUG_USER
  298. if (user_debug & UDBG_SYSCALL) {
  299. printk(KERN_ERR "[%d] %s: obsolete system call %08x.\n",
  300. current->pid, current->comm, n);
  301. dump_instr(regs);
  302. }
  303. #endif
  304. info.si_signo = SIGILL;
  305. info.si_errno = 0;
  306. info.si_code = ILL_ILLTRP;
  307. info.si_addr = (void __user *)instruction_pointer(regs) -
  308. (thumb_mode(regs) ? 2 : 4);
  309. notify_die("Oops - bad syscall", regs, &info, n, 0);
  310. return regs->ARM_r0;
  311. }
  312. static inline void
  313. do_cache_op(unsigned long start, unsigned long end, int flags)
  314. {
  315. struct vm_area_struct *vma;
  316. if (end < start || flags)
  317. return;
  318. vma = find_vma(current->active_mm, start);
  319. if (vma && vma->vm_start < end) {
  320. if (start < vma->vm_start)
  321. start = vma->vm_start;
  322. if (end > vma->vm_end)
  323. end = vma->vm_end;
  324. flush_cache_user_range(vma, start, end);
  325. }
  326. }
  327. /*
  328. * Handle all unrecognised system calls.
  329. * 0x9f0000 - 0x9fffff are some more esoteric system calls
  330. */
  331. #define NR(x) ((__ARM_NR_##x) - __ARM_NR_BASE)
  332. asmlinkage int arm_syscall(int no, struct pt_regs *regs)
  333. {
  334. struct thread_info *thread = current_thread_info();
  335. siginfo_t info;
  336. if ((no >> 16) != 0x9f)
  337. return bad_syscall(no, regs);
  338. switch (no & 0xffff) {
  339. case 0: /* branch through 0 */
  340. info.si_signo = SIGSEGV;
  341. info.si_errno = 0;
  342. info.si_code = SEGV_MAPERR;
  343. info.si_addr = NULL;
  344. notify_die("branch through zero", regs, &info, 0, 0);
  345. return 0;
  346. case NR(breakpoint): /* SWI BREAK_POINT */
  347. regs->ARM_pc -= thumb_mode(regs) ? 2 : 4;
  348. ptrace_break(current, regs);
  349. return regs->ARM_r0;
  350. /*
  351. * Flush a region from virtual address 'r0' to virtual address 'r1'
  352. * _exclusive_. There is no alignment requirement on either address;
  353. * user space does not need to know the hardware cache layout.
  354. *
  355. * r2 contains flags. It should ALWAYS be passed as ZERO until it
  356. * is defined to be something else. For now we ignore it, but may
  357. * the fires of hell burn in your belly if you break this rule. ;)
  358. *
  359. * (at a later date, we may want to allow this call to not flush
  360. * various aspects of the cache. Passing '0' will guarantee that
  361. * everything necessary gets flushed to maintain consistency in
  362. * the specified region).
  363. */
  364. case NR(cacheflush):
  365. do_cache_op(regs->ARM_r0, regs->ARM_r1, regs->ARM_r2);
  366. return 0;
  367. case NR(usr26):
  368. if (!(elf_hwcap & HWCAP_26BIT))
  369. break;
  370. regs->ARM_cpsr &= ~MODE32_BIT;
  371. return regs->ARM_r0;
  372. case NR(usr32):
  373. if (!(elf_hwcap & HWCAP_26BIT))
  374. break;
  375. regs->ARM_cpsr |= MODE32_BIT;
  376. return regs->ARM_r0;
  377. case NR(set_tls):
  378. thread->tp_value = regs->ARM_r0;
  379. /*
  380. * Our user accessible TLS ptr is located at 0xffff0ffc.
  381. * On SMP read access to this address must raise a fault
  382. * and be emulated from the data abort handler.
  383. * m
  384. */
  385. *((unsigned long *)0xffff0ffc) = thread->tp_value;
  386. return 0;
  387. default:
  388. /* Calls 9f00xx..9f07ff are defined to return -ENOSYS
  389. if not implemented, rather than raising SIGILL. This
  390. way the calling program can gracefully determine whether
  391. a feature is supported. */
  392. if (no <= 0x7ff)
  393. return -ENOSYS;
  394. break;
  395. }
  396. #ifdef CONFIG_DEBUG_USER
  397. /*
  398. * experience shows that these seem to indicate that
  399. * something catastrophic has happened
  400. */
  401. if (user_debug & UDBG_SYSCALL) {
  402. printk("[%d] %s: arm syscall %d\n",
  403. current->pid, current->comm, no);
  404. dump_instr(regs);
  405. if (user_mode(regs)) {
  406. __show_regs(regs);
  407. c_backtrace(regs->ARM_fp, processor_mode(regs));
  408. }
  409. }
  410. #endif
  411. info.si_signo = SIGILL;
  412. info.si_errno = 0;
  413. info.si_code = ILL_ILLTRP;
  414. info.si_addr = (void __user *)instruction_pointer(regs) -
  415. (thumb_mode(regs) ? 2 : 4);
  416. notify_die("Oops - bad syscall(2)", regs, &info, no, 0);
  417. return 0;
  418. }
  419. void __bad_xchg(volatile void *ptr, int size)
  420. {
  421. printk("xchg: bad data size: pc 0x%p, ptr 0x%p, size %d\n",
  422. __builtin_return_address(0), ptr, size);
  423. BUG();
  424. }
  425. EXPORT_SYMBOL(__bad_xchg);
  426. /*
  427. * A data abort trap was taken, but we did not handle the instruction.
  428. * Try to abort the user program, or panic if it was the kernel.
  429. */
  430. asmlinkage void
  431. baddataabort(int code, unsigned long instr, struct pt_regs *regs)
  432. {
  433. unsigned long addr = instruction_pointer(regs);
  434. siginfo_t info;
  435. #ifdef CONFIG_DEBUG_USER
  436. if (user_debug & UDBG_BADABORT) {
  437. printk(KERN_ERR "[%d] %s: bad data abort: code %d instr 0x%08lx\n",
  438. current->pid, current->comm, code, instr);
  439. dump_instr(regs);
  440. show_pte(current->mm, addr);
  441. }
  442. #endif
  443. info.si_signo = SIGILL;
  444. info.si_errno = 0;
  445. info.si_code = ILL_ILLOPC;
  446. info.si_addr = (void __user *)addr;
  447. notify_die("unknown data abort code", regs, &info, instr, 0);
  448. }
  449. volatile void __bug(const char *file, int line, void *data)
  450. {
  451. printk(KERN_CRIT"kernel BUG at %s:%d!", file, line);
  452. if (data)
  453. printk(" - extra data = %p", data);
  454. printk("\n");
  455. *(int *)0 = 0;
  456. }
  457. EXPORT_SYMBOL(__bug);
  458. void __readwrite_bug(const char *fn)
  459. {
  460. printk("%s called, but not implemented\n", fn);
  461. BUG();
  462. }
  463. EXPORT_SYMBOL(__readwrite_bug);
  464. void __pte_error(const char *file, int line, unsigned long val)
  465. {
  466. printk("%s:%d: bad pte %08lx.\n", file, line, val);
  467. }
  468. void __pmd_error(const char *file, int line, unsigned long val)
  469. {
  470. printk("%s:%d: bad pmd %08lx.\n", file, line, val);
  471. }
  472. void __pgd_error(const char *file, int line, unsigned long val)
  473. {
  474. printk("%s:%d: bad pgd %08lx.\n", file, line, val);
  475. }
  476. asmlinkage void __div0(void)
  477. {
  478. printk("Division by zero in kernel.\n");
  479. dump_stack();
  480. }
  481. EXPORT_SYMBOL(__div0);
  482. void abort(void)
  483. {
  484. BUG();
  485. /* if that doesn't kill us, halt */
  486. panic("Oops failed to kill thread");
  487. }
  488. EXPORT_SYMBOL(abort);
  489. void __init trap_init(void)
  490. {
  491. extern void __trap_init(void);
  492. __trap_init();
  493. flush_icache_range(0xffff0000, 0xffff0000 + PAGE_SIZE);
  494. modify_domain(DOMAIN_USER, DOMAIN_CLIENT);
  495. }