traps.c 6.0 KB

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