traps.c 6.1 KB

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