traps.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (C) 2012 Regents of the University of California
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/debug.h>
  17. #include <linux/sched/signal.h>
  18. #include <linux/signal.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/irq.h>
  24. #include <asm/processor.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/csr.h>
  27. int show_unhandled_signals = 1;
  28. extern asmlinkage void handle_exception(void);
  29. static DEFINE_SPINLOCK(die_lock);
  30. void die(struct pt_regs *regs, const char *str)
  31. {
  32. static int die_counter;
  33. int ret;
  34. oops_enter();
  35. spin_lock_irq(&die_lock);
  36. console_verbose();
  37. bust_spinlocks(1);
  38. pr_emerg("%s [#%d]\n", str, ++die_counter);
  39. print_modules();
  40. show_regs(regs);
  41. ret = notify_die(DIE_OOPS, str, regs, 0, regs->scause, SIGSEGV);
  42. bust_spinlocks(0);
  43. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  44. spin_unlock_irq(&die_lock);
  45. oops_exit();
  46. if (in_interrupt())
  47. panic("Fatal exception in interrupt");
  48. if (panic_on_oops)
  49. panic("Fatal exception");
  50. if (ret != NOTIFY_STOP)
  51. do_exit(SIGSEGV);
  52. }
  53. static inline void do_trap_siginfo(int signo, int code,
  54. unsigned long addr, struct task_struct *tsk)
  55. {
  56. siginfo_t info;
  57. info.si_signo = signo;
  58. info.si_errno = 0;
  59. info.si_code = code;
  60. info.si_addr = (void __user *)addr;
  61. force_sig_info(signo, &info, tsk);
  62. }
  63. void do_trap(struct pt_regs *regs, int signo, int code,
  64. unsigned long addr, struct task_struct *tsk)
  65. {
  66. if (show_unhandled_signals && unhandled_signal(tsk, signo)
  67. && printk_ratelimit()) {
  68. pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
  69. tsk->comm, task_pid_nr(tsk), signo, code, addr);
  70. print_vma_addr(KERN_CONT " in ", GET_IP(regs));
  71. pr_cont("\n");
  72. show_regs(regs);
  73. }
  74. do_trap_siginfo(signo, code, addr, tsk);
  75. }
  76. static void do_trap_error(struct pt_regs *regs, int signo, int code,
  77. unsigned long addr, const char *str)
  78. {
  79. if (user_mode(regs)) {
  80. do_trap(regs, signo, code, addr, current);
  81. } else {
  82. if (!fixup_exception(regs))
  83. die(regs, str);
  84. }
  85. }
  86. #define DO_ERROR_INFO(name, signo, code, str) \
  87. asmlinkage void name(struct pt_regs *regs) \
  88. { \
  89. do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
  90. }
  91. DO_ERROR_INFO(do_trap_unknown,
  92. SIGILL, ILL_ILLTRP, "unknown exception");
  93. DO_ERROR_INFO(do_trap_insn_misaligned,
  94. SIGBUS, BUS_ADRALN, "instruction address misaligned");
  95. DO_ERROR_INFO(do_trap_insn_fault,
  96. SIGSEGV, SEGV_ACCERR, "instruction access fault");
  97. DO_ERROR_INFO(do_trap_insn_illegal,
  98. SIGILL, ILL_ILLOPC, "illegal instruction");
  99. DO_ERROR_INFO(do_trap_load_misaligned,
  100. SIGBUS, BUS_ADRALN, "load address misaligned");
  101. DO_ERROR_INFO(do_trap_load_fault,
  102. SIGSEGV, SEGV_ACCERR, "load access fault");
  103. DO_ERROR_INFO(do_trap_store_misaligned,
  104. SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned");
  105. DO_ERROR_INFO(do_trap_store_fault,
  106. SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
  107. DO_ERROR_INFO(do_trap_ecall_u,
  108. SIGILL, ILL_ILLTRP, "environment call from U-mode");
  109. DO_ERROR_INFO(do_trap_ecall_s,
  110. SIGILL, ILL_ILLTRP, "environment call from S-mode");
  111. DO_ERROR_INFO(do_trap_ecall_m,
  112. SIGILL, ILL_ILLTRP, "environment call from M-mode");
  113. asmlinkage void do_trap_break(struct pt_regs *regs)
  114. {
  115. #ifdef CONFIG_GENERIC_BUG
  116. if (!user_mode(regs)) {
  117. enum bug_trap_type type;
  118. type = report_bug(regs->sepc, regs);
  119. switch (type) {
  120. case BUG_TRAP_TYPE_NONE:
  121. break;
  122. case BUG_TRAP_TYPE_WARN:
  123. regs->sepc += sizeof(bug_insn_t);
  124. return;
  125. case BUG_TRAP_TYPE_BUG:
  126. die(regs, "Kernel BUG");
  127. }
  128. }
  129. #endif /* CONFIG_GENERIC_BUG */
  130. do_trap_siginfo(SIGTRAP, TRAP_BRKPT, regs->sepc, current);
  131. regs->sepc += 0x4;
  132. }
  133. #ifdef CONFIG_GENERIC_BUG
  134. int is_valid_bugaddr(unsigned long pc)
  135. {
  136. bug_insn_t insn;
  137. if (pc < PAGE_OFFSET)
  138. return 0;
  139. if (probe_kernel_address((bug_insn_t __user *)pc, insn))
  140. return 0;
  141. return (insn == __BUG_INSN);
  142. }
  143. #endif /* CONFIG_GENERIC_BUG */
  144. void __init trap_init(void)
  145. {
  146. /*
  147. * Set sup0 scratch register to 0, indicating to exception vector
  148. * that we are presently executing in the kernel
  149. */
  150. csr_write(sscratch, 0);
  151. /* Set the exception vector address */
  152. csr_write(stvec, &handle_exception);
  153. /* Enable all interrupts */
  154. csr_write(sie, -1);
  155. }