traps.c 5.9 KB

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