traps.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2004-2006 Atmel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/bug.h>
  9. #include <linux/hardirq.h>
  10. #include <linux/init.h>
  11. #include <linux/kallsyms.h>
  12. #include <linux/kdebug.h>
  13. #include <linux/module.h>
  14. #include <linux/notifier.h>
  15. #include <linux/sched.h>
  16. #include <linux/uaccess.h>
  17. #include <asm/addrspace.h>
  18. #include <asm/mmu_context.h>
  19. #include <asm/ocd.h>
  20. #include <asm/sysreg.h>
  21. #include <asm/traps.h>
  22. static DEFINE_SPINLOCK(die_lock);
  23. void NORET_TYPE die(const char *str, struct pt_regs *regs, long err)
  24. {
  25. static int die_counter;
  26. console_verbose();
  27. spin_lock_irq(&die_lock);
  28. bust_spinlocks(1);
  29. printk(KERN_ALERT "Oops: %s, sig: %ld [#%d]\n",
  30. str, err, ++die_counter);
  31. printk(KERN_EMERG);
  32. #ifdef CONFIG_PREEMPT
  33. printk(KERN_CONT "PREEMPT ");
  34. #endif
  35. #ifdef CONFIG_FRAME_POINTER
  36. printk(KERN_CONT "FRAME_POINTER ");
  37. #endif
  38. if (current_cpu_data.features & AVR32_FEATURE_OCD) {
  39. unsigned long did = ocd_read(DID);
  40. printk(KERN_CONT "chip: 0x%03lx:0x%04lx rev %lu\n",
  41. (did >> 1) & 0x7ff,
  42. (did >> 12) & 0x7fff,
  43. (did >> 28) & 0xf);
  44. } else {
  45. printk(KERN_CONT "cpu: arch %u r%u / core %u r%u\n",
  46. current_cpu_data.arch_type,
  47. current_cpu_data.arch_revision,
  48. current_cpu_data.cpu_type,
  49. current_cpu_data.cpu_revision);
  50. }
  51. print_modules();
  52. show_regs_log_lvl(regs, KERN_EMERG);
  53. show_stack_log_lvl(current, regs->sp, regs, KERN_EMERG);
  54. bust_spinlocks(0);
  55. add_taint(TAINT_DIE);
  56. spin_unlock_irq(&die_lock);
  57. if (in_interrupt())
  58. panic("Fatal exception in interrupt");
  59. if (panic_on_oops)
  60. panic("Fatal exception");
  61. do_exit(err);
  62. }
  63. void _exception(long signr, struct pt_regs *regs, int code,
  64. unsigned long addr)
  65. {
  66. siginfo_t info;
  67. if (!user_mode(regs)) {
  68. const struct exception_table_entry *fixup;
  69. /* Are we prepared to handle this kernel fault? */
  70. fixup = search_exception_tables(regs->pc);
  71. if (fixup) {
  72. regs->pc = fixup->fixup;
  73. return;
  74. }
  75. die("Unhandled exception in kernel mode", regs, signr);
  76. }
  77. memset(&info, 0, sizeof(info));
  78. info.si_signo = signr;
  79. info.si_code = code;
  80. info.si_addr = (void __user *)addr;
  81. force_sig_info(signr, &info, current);
  82. /*
  83. * Init gets no signals that it doesn't have a handler for.
  84. * That's all very well, but if it has caused a synchronous
  85. * exception and we ignore the resulting signal, it will just
  86. * generate the same exception over and over again and we get
  87. * nowhere. Better to kill it and let the kernel panic.
  88. */
  89. if (is_global_init(current)) {
  90. __sighandler_t handler;
  91. spin_lock_irq(&current->sighand->siglock);
  92. handler = current->sighand->action[signr-1].sa.sa_handler;
  93. spin_unlock_irq(&current->sighand->siglock);
  94. if (handler == SIG_DFL) {
  95. /* init has generated a synchronous exception
  96. and it doesn't have a handler for the signal */
  97. printk(KERN_CRIT "init has generated signal %ld "
  98. "but has no handler for it\n", signr);
  99. do_exit(signr);
  100. }
  101. }
  102. }
  103. asmlinkage void do_nmi(unsigned long ecr, struct pt_regs *regs)
  104. {
  105. int ret;
  106. nmi_enter();
  107. ret = notify_die(DIE_NMI, "NMI", regs, 0, ecr, SIGINT);
  108. switch (ret) {
  109. case NOTIFY_OK:
  110. case NOTIFY_STOP:
  111. break;
  112. case NOTIFY_BAD:
  113. die("Fatal Non-Maskable Interrupt", regs, SIGINT);
  114. default:
  115. printk(KERN_ALERT "Got NMI, but nobody cared. Disabling...\n");
  116. nmi_disable();
  117. break;
  118. }
  119. nmi_exit();
  120. }
  121. asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
  122. {
  123. die("Critical exception", regs, SIGKILL);
  124. }
  125. asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
  126. {
  127. _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
  128. }
  129. /* This way of handling undefined instructions is stolen from ARM */
  130. static LIST_HEAD(undef_hook);
  131. static DEFINE_SPINLOCK(undef_lock);
  132. void register_undef_hook(struct undef_hook *hook)
  133. {
  134. spin_lock_irq(&undef_lock);
  135. list_add(&hook->node, &undef_hook);
  136. spin_unlock_irq(&undef_lock);
  137. }
  138. void unregister_undef_hook(struct undef_hook *hook)
  139. {
  140. spin_lock_irq(&undef_lock);
  141. list_del(&hook->node);
  142. spin_unlock_irq(&undef_lock);
  143. }
  144. static int do_cop_absent(u32 insn)
  145. {
  146. int cop_nr;
  147. u32 cpucr;
  148. if ((insn & 0xfdf00000) == 0xf1900000)
  149. /* LDC0 */
  150. cop_nr = 0;
  151. else
  152. cop_nr = (insn >> 13) & 0x7;
  153. /* Try enabling the coprocessor */
  154. cpucr = sysreg_read(CPUCR);
  155. cpucr |= (1 << (24 + cop_nr));
  156. sysreg_write(CPUCR, cpucr);
  157. cpucr = sysreg_read(CPUCR);
  158. if (!(cpucr & (1 << (24 + cop_nr))))
  159. return -ENODEV;
  160. return 0;
  161. }
  162. #ifdef CONFIG_BUG
  163. int is_valid_bugaddr(unsigned long pc)
  164. {
  165. unsigned short opcode;
  166. if (pc < PAGE_OFFSET)
  167. return 0;
  168. if (probe_kernel_address((u16 *)pc, opcode))
  169. return 0;
  170. return opcode == AVR32_BUG_OPCODE;
  171. }
  172. #endif
  173. asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
  174. {
  175. u32 insn;
  176. struct undef_hook *hook;
  177. void __user *pc;
  178. long code;
  179. #ifdef CONFIG_BUG
  180. if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
  181. enum bug_trap_type type;
  182. type = report_bug(regs->pc, regs);
  183. switch (type) {
  184. case BUG_TRAP_TYPE_NONE:
  185. break;
  186. case BUG_TRAP_TYPE_WARN:
  187. regs->pc += 2;
  188. return;
  189. case BUG_TRAP_TYPE_BUG:
  190. die("Kernel BUG", regs, SIGKILL);
  191. }
  192. }
  193. #endif
  194. local_irq_enable();
  195. if (user_mode(regs)) {
  196. pc = (void __user *)instruction_pointer(regs);
  197. if (get_user(insn, (u32 __user *)pc))
  198. goto invalid_area;
  199. if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
  200. return;
  201. spin_lock_irq(&undef_lock);
  202. list_for_each_entry(hook, &undef_hook, node) {
  203. if ((insn & hook->insn_mask) == hook->insn_val) {
  204. if (hook->fn(regs, insn) == 0) {
  205. spin_unlock_irq(&undef_lock);
  206. return;
  207. }
  208. }
  209. }
  210. spin_unlock_irq(&undef_lock);
  211. }
  212. switch (ecr) {
  213. case ECR_PRIVILEGE_VIOLATION:
  214. code = ILL_PRVOPC;
  215. break;
  216. case ECR_COPROC_ABSENT:
  217. code = ILL_COPROC;
  218. break;
  219. default:
  220. code = ILL_ILLOPC;
  221. break;
  222. }
  223. _exception(SIGILL, regs, code, regs->pc);
  224. return;
  225. invalid_area:
  226. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
  227. }
  228. asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
  229. {
  230. /* We have no FPU yet */
  231. _exception(SIGILL, regs, ILL_COPROC, regs->pc);
  232. }
  233. void __init trap_init(void)
  234. {
  235. }