traps.c 5.6 KB

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