traps.c 5.4 KB

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