traps.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. */
  5. #include <linux/cpu.h>
  6. #include <linux/kernel.h>
  7. #include <linux/init.h>
  8. #include <linux/sched.h>
  9. #include <linux/sched/debug.h>
  10. #include <linux/sched/signal.h>
  11. #include <linux/signal.h>
  12. #include <linux/kdebug.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/kprobes.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/irq.h>
  18. #include <linux/kexec.h>
  19. #include <asm/processor.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/csr.h>
  22. int show_unhandled_signals = 1;
  23. extern asmlinkage void handle_exception(void);
  24. static DEFINE_SPINLOCK(die_lock);
  25. void die(struct pt_regs *regs, const char *str)
  26. {
  27. static int die_counter;
  28. int ret;
  29. oops_enter();
  30. spin_lock_irq(&die_lock);
  31. console_verbose();
  32. bust_spinlocks(1);
  33. pr_emerg("%s [#%d]\n", str, ++die_counter);
  34. print_modules();
  35. show_regs(regs);
  36. ret = notify_die(DIE_OOPS, str, regs, 0, regs->cause, SIGSEGV);
  37. if (regs && kexec_should_crash(current))
  38. crash_kexec(regs);
  39. bust_spinlocks(0);
  40. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  41. spin_unlock_irq(&die_lock);
  42. oops_exit();
  43. if (in_interrupt())
  44. panic("Fatal exception in interrupt");
  45. if (panic_on_oops)
  46. panic("Fatal exception");
  47. if (ret != NOTIFY_STOP)
  48. do_exit(SIGSEGV);
  49. }
  50. #ifdef CONFIG_VECTOR_EMU
  51. extern bool decode_exec_insn(struct pt_regs *regs, uint64_t insn);
  52. #endif
  53. void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
  54. {
  55. struct task_struct *tsk = current;
  56. #ifdef CONFIG_VECTOR_EMU
  57. if (signo == SIGILL)
  58. if (decode_exec_insn(regs, regs->badaddr))
  59. return;
  60. #endif
  61. if (show_unhandled_signals && unhandled_signal(tsk, signo)
  62. && printk_ratelimit()) {
  63. pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
  64. tsk->comm, task_pid_nr(tsk), signo, code, addr);
  65. print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
  66. pr_cont("\n");
  67. show_regs(regs);
  68. }
  69. force_sig_fault(signo, code, (void __user *)addr);
  70. }
  71. static void do_trap_error(struct pt_regs *regs, int signo, int code,
  72. unsigned long addr, const char *str)
  73. {
  74. current->thread.bad_cause = regs->cause;
  75. if (user_mode(regs)) {
  76. do_trap(regs, signo, code, addr);
  77. } else {
  78. if (!fixup_exception(regs))
  79. die(regs, str);
  80. }
  81. }
  82. #define DO_ERROR_INFO(name, signo, code, str) \
  83. asmlinkage __visible void name(struct pt_regs *regs) \
  84. { \
  85. do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
  86. }
  87. DO_ERROR_INFO(do_trap_unknown,
  88. SIGILL, ILL_ILLTRP, "unknown exception");
  89. DO_ERROR_INFO(do_trap_insn_misaligned,
  90. SIGBUS, BUS_ADRALN, "instruction address misaligned");
  91. DO_ERROR_INFO(do_trap_insn_fault,
  92. SIGSEGV, SEGV_ACCERR, "instruction access fault");
  93. DO_ERROR_INFO(do_trap_insn_illegal,
  94. SIGILL, ILL_ILLOPC, "illegal instruction");
  95. DO_ERROR_INFO(do_trap_load_fault,
  96. SIGSEGV, SEGV_ACCERR, "load access fault");
  97. #ifndef CONFIG_RISCV_M_MODE
  98. DO_ERROR_INFO(do_trap_load_misaligned,
  99. SIGBUS, BUS_ADRALN, "Oops - load address misaligned");
  100. DO_ERROR_INFO(do_trap_store_misaligned,
  101. SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned");
  102. #else
  103. int handle_misaligned_load(struct pt_regs *regs);
  104. int handle_misaligned_store(struct pt_regs *regs);
  105. asmlinkage void do_trap_load_misaligned(struct pt_regs *regs)
  106. {
  107. if (!handle_misaligned_load(regs))
  108. return;
  109. do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
  110. "Oops - load address misaligned");
  111. }
  112. asmlinkage void do_trap_store_misaligned(struct pt_regs *regs)
  113. {
  114. if (!handle_misaligned_store(regs))
  115. return;
  116. do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
  117. "Oops - store (or AMO) address misaligned");
  118. }
  119. #endif
  120. DO_ERROR_INFO(do_trap_store_fault,
  121. SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
  122. DO_ERROR_INFO(do_trap_ecall_u,
  123. SIGILL, ILL_ILLTRP, "environment call from U-mode");
  124. DO_ERROR_INFO(do_trap_ecall_s,
  125. SIGILL, ILL_ILLTRP, "environment call from S-mode");
  126. DO_ERROR_INFO(do_trap_ecall_m,
  127. SIGILL, ILL_ILLTRP, "environment call from M-mode");
  128. static inline unsigned long get_break_insn_length(unsigned long pc)
  129. {
  130. bug_insn_t insn;
  131. if (get_kernel_nofault(insn, (bug_insn_t *)pc))
  132. return 0;
  133. return GET_INSN_LENGTH(insn);
  134. }
  135. asmlinkage __visible void do_trap_break(struct pt_regs *regs)
  136. {
  137. #ifdef CONFIG_KPROBES
  138. if (kprobe_single_step_handler(regs))
  139. return;
  140. if (kprobe_breakpoint_handler(regs))
  141. return;
  142. #endif
  143. #ifdef CONFIG_UPROBES
  144. if (uprobe_single_step_handler(regs))
  145. return;
  146. if (uprobe_breakpoint_handler(regs))
  147. return;
  148. #endif
  149. current->thread.bad_cause = regs->cause;
  150. if (user_mode(regs))
  151. force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
  152. #ifdef CONFIG_KGDB
  153. else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
  154. == NOTIFY_STOP)
  155. return;
  156. #endif
  157. else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
  158. regs->epc += get_break_insn_length(regs->epc);
  159. else
  160. die(regs, "Kernel BUG");
  161. }
  162. #ifdef CONFIG_GENERIC_BUG
  163. int is_valid_bugaddr(unsigned long pc)
  164. {
  165. bug_insn_t insn;
  166. if (pc < VMALLOC_START)
  167. return 0;
  168. if (get_kernel_nofault(insn, (bug_insn_t *)pc))
  169. return 0;
  170. if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
  171. return (insn == __BUG_INSN_32);
  172. else
  173. return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
  174. }
  175. #endif /* CONFIG_GENERIC_BUG */
  176. /* stvec & scratch is already set from head.S */
  177. void trap_init(void)
  178. {
  179. }