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. break;
  102. case NOTIFY_BAD:
  103. die("Fatal Non-Maskable Interrupt", regs, SIGINT);
  104. default:
  105. printk(KERN_ALERT "Got NMI, but nobody cared. Disabling...\n");
  106. nmi_disable();
  107. break;
  108. }
  109. nmi_exit();
  110. }
  111. asmlinkage void do_critical_exception(unsigned long ecr, struct pt_regs *regs)
  112. {
  113. die("Critical exception", regs, SIGKILL);
  114. }
  115. asmlinkage void do_address_exception(unsigned long ecr, struct pt_regs *regs)
  116. {
  117. _exception(SIGBUS, regs, BUS_ADRALN, regs->pc);
  118. }
  119. /* This way of handling undefined instructions is stolen from ARM */
  120. static LIST_HEAD(undef_hook);
  121. static DEFINE_SPINLOCK(undef_lock);
  122. void register_undef_hook(struct undef_hook *hook)
  123. {
  124. spin_lock_irq(&undef_lock);
  125. list_add(&hook->node, &undef_hook);
  126. spin_unlock_irq(&undef_lock);
  127. }
  128. void unregister_undef_hook(struct undef_hook *hook)
  129. {
  130. spin_lock_irq(&undef_lock);
  131. list_del(&hook->node);
  132. spin_unlock_irq(&undef_lock);
  133. }
  134. static int do_cop_absent(u32 insn)
  135. {
  136. int cop_nr;
  137. u32 cpucr;
  138. if ((insn & 0xfdf00000) == 0xf1900000)
  139. /* LDC0 */
  140. cop_nr = 0;
  141. else
  142. cop_nr = (insn >> 13) & 0x7;
  143. /* Try enabling the coprocessor */
  144. cpucr = sysreg_read(CPUCR);
  145. cpucr |= (1 << (24 + cop_nr));
  146. sysreg_write(CPUCR, cpucr);
  147. cpucr = sysreg_read(CPUCR);
  148. if (!(cpucr & (1 << (24 + cop_nr))))
  149. return -ENODEV;
  150. return 0;
  151. }
  152. #ifdef CONFIG_BUG
  153. int is_valid_bugaddr(unsigned long pc)
  154. {
  155. unsigned short opcode;
  156. if (pc < PAGE_OFFSET)
  157. return 0;
  158. if (probe_kernel_address((u16 *)pc, opcode))
  159. return 0;
  160. return opcode == AVR32_BUG_OPCODE;
  161. }
  162. #endif
  163. asmlinkage void do_illegal_opcode(unsigned long ecr, struct pt_regs *regs)
  164. {
  165. u32 insn;
  166. struct undef_hook *hook;
  167. void __user *pc;
  168. long code;
  169. #ifdef CONFIG_BUG
  170. if (!user_mode(regs) && (ecr == ECR_ILLEGAL_OPCODE)) {
  171. enum bug_trap_type type;
  172. type = report_bug(regs->pc, regs);
  173. switch (type) {
  174. case BUG_TRAP_TYPE_NONE:
  175. break;
  176. case BUG_TRAP_TYPE_WARN:
  177. regs->pc += 2;
  178. return;
  179. case BUG_TRAP_TYPE_BUG:
  180. die("Kernel BUG", regs, SIGKILL);
  181. }
  182. }
  183. #endif
  184. local_irq_enable();
  185. if (user_mode(regs)) {
  186. pc = (void __user *)instruction_pointer(regs);
  187. if (get_user(insn, (u32 __user *)pc))
  188. goto invalid_area;
  189. if (ecr == ECR_COPROC_ABSENT && !do_cop_absent(insn))
  190. return;
  191. spin_lock_irq(&undef_lock);
  192. list_for_each_entry(hook, &undef_hook, node) {
  193. if ((insn & hook->insn_mask) == hook->insn_val) {
  194. if (hook->fn(regs, insn) == 0) {
  195. spin_unlock_irq(&undef_lock);
  196. return;
  197. }
  198. }
  199. }
  200. spin_unlock_irq(&undef_lock);
  201. }
  202. switch (ecr) {
  203. case ECR_PRIVILEGE_VIOLATION:
  204. code = ILL_PRVOPC;
  205. break;
  206. case ECR_COPROC_ABSENT:
  207. code = ILL_COPROC;
  208. break;
  209. default:
  210. code = ILL_ILLOPC;
  211. break;
  212. }
  213. _exception(SIGILL, regs, code, regs->pc);
  214. return;
  215. invalid_area:
  216. _exception(SIGSEGV, regs, SEGV_MAPERR, regs->pc);
  217. }
  218. asmlinkage void do_fpe(unsigned long ecr, struct pt_regs *regs)
  219. {
  220. /* We have no FPU yet */
  221. _exception(SIGILL, regs, ILL_COPROC, regs->pc);
  222. }
  223. void __init trap_init(void)
  224. {
  225. }