traps.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/asm-prototypes.h>
  20. #include <asm/bug.h>
  21. #include <asm/csr.h>
  22. #include <asm/processor.h>
  23. #include <asm/ptrace.h>
  24. #include <asm/thread_info.h>
  25. int show_unhandled_signals = 1;
  26. static DEFINE_SPINLOCK(die_lock);
  27. void die(struct pt_regs *regs, const char *str)
  28. {
  29. static int die_counter;
  30. int ret;
  31. long cause;
  32. unsigned long flags;
  33. oops_enter();
  34. spin_lock_irqsave(&die_lock, flags);
  35. console_verbose();
  36. bust_spinlocks(1);
  37. pr_emerg("%s [#%d]\n", str, ++die_counter);
  38. print_modules();
  39. if (regs)
  40. show_regs(regs);
  41. cause = regs ? regs->cause : -1;
  42. ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV);
  43. if (kexec_should_crash(current))
  44. crash_kexec(regs);
  45. bust_spinlocks(0);
  46. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  47. spin_unlock_irqrestore(&die_lock, flags);
  48. oops_exit();
  49. if (in_interrupt())
  50. panic("Fatal exception in interrupt");
  51. if (panic_on_oops)
  52. panic("Fatal exception");
  53. if (ret != NOTIFY_STOP)
  54. make_task_dead(SIGSEGV);
  55. }
  56. void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
  57. {
  58. struct task_struct *tsk = current;
  59. if (show_unhandled_signals && unhandled_signal(tsk, signo)
  60. && printk_ratelimit()) {
  61. pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
  62. tsk->comm, task_pid_nr(tsk), signo, code, addr);
  63. print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
  64. pr_cont("\n");
  65. __show_regs(regs);
  66. }
  67. force_sig_fault(signo, code, (void __user *)addr);
  68. }
  69. static void do_trap_error(struct pt_regs *regs, int signo, int code,
  70. unsigned long addr, const char *str)
  71. {
  72. current->thread.bad_cause = regs->cause;
  73. if (user_mode(regs)) {
  74. do_trap(regs, signo, code, addr);
  75. } else {
  76. if (!fixup_exception(regs))
  77. die(regs, str);
  78. }
  79. }
  80. #if defined(CONFIG_XIP_KERNEL) && defined(CONFIG_RISCV_ALTERNATIVE)
  81. #define __trap_section __section(".xip.traps")
  82. #else
  83. #define __trap_section
  84. #endif
  85. #define DO_ERROR_INFO(name, signo, code, str) \
  86. asmlinkage __visible __trap_section void name(struct pt_regs *regs) \
  87. { \
  88. do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
  89. }
  90. DO_ERROR_INFO(do_trap_unknown,
  91. SIGILL, ILL_ILLTRP, "unknown exception");
  92. DO_ERROR_INFO(do_trap_insn_misaligned,
  93. SIGBUS, BUS_ADRALN, "instruction address misaligned");
  94. DO_ERROR_INFO(do_trap_insn_fault,
  95. SIGSEGV, SEGV_ACCERR, "instruction access fault");
  96. DO_ERROR_INFO(do_trap_insn_illegal,
  97. SIGILL, ILL_ILLOPC, "illegal instruction");
  98. DO_ERROR_INFO(do_trap_load_fault,
  99. SIGSEGV, SEGV_ACCERR, "load access fault");
  100. #ifndef CONFIG_RISCV_M_MODE
  101. DO_ERROR_INFO(do_trap_load_misaligned,
  102. SIGBUS, BUS_ADRALN, "Oops - load address misaligned");
  103. DO_ERROR_INFO(do_trap_store_misaligned,
  104. SIGBUS, BUS_ADRALN, "Oops - store (or AMO) address misaligned");
  105. #else
  106. int handle_misaligned_load(struct pt_regs *regs);
  107. int handle_misaligned_store(struct pt_regs *regs);
  108. asmlinkage void __trap_section do_trap_load_misaligned(struct pt_regs *regs)
  109. {
  110. if (!handle_misaligned_load(regs))
  111. return;
  112. do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
  113. "Oops - load address misaligned");
  114. }
  115. asmlinkage void __trap_section do_trap_store_misaligned(struct pt_regs *regs)
  116. {
  117. if (!handle_misaligned_store(regs))
  118. return;
  119. do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
  120. "Oops - store (or AMO) address misaligned");
  121. }
  122. #endif
  123. DO_ERROR_INFO(do_trap_store_fault,
  124. SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
  125. DO_ERROR_INFO(do_trap_ecall_u,
  126. SIGILL, ILL_ILLTRP, "environment call from U-mode");
  127. DO_ERROR_INFO(do_trap_ecall_s,
  128. SIGILL, ILL_ILLTRP, "environment call from S-mode");
  129. DO_ERROR_INFO(do_trap_ecall_m,
  130. SIGILL, ILL_ILLTRP, "environment call from M-mode");
  131. static inline unsigned long get_break_insn_length(unsigned long pc)
  132. {
  133. bug_insn_t insn;
  134. if (get_kernel_nofault(insn, (bug_insn_t *)pc))
  135. return 0;
  136. return GET_INSN_LENGTH(insn);
  137. }
  138. asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
  139. {
  140. #ifdef CONFIG_KPROBES
  141. if (kprobe_single_step_handler(regs))
  142. return;
  143. if (kprobe_breakpoint_handler(regs))
  144. return;
  145. #endif
  146. #ifdef CONFIG_UPROBES
  147. if (uprobe_single_step_handler(regs))
  148. return;
  149. if (uprobe_breakpoint_handler(regs))
  150. return;
  151. #endif
  152. current->thread.bad_cause = regs->cause;
  153. if (user_mode(regs))
  154. force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
  155. #ifdef CONFIG_KGDB
  156. else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
  157. == NOTIFY_STOP)
  158. return;
  159. #endif
  160. else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN)
  161. regs->epc += get_break_insn_length(regs->epc);
  162. else
  163. die(regs, "Kernel BUG");
  164. }
  165. NOKPROBE_SYMBOL(do_trap_break);
  166. #ifdef CONFIG_GENERIC_BUG
  167. int is_valid_bugaddr(unsigned long pc)
  168. {
  169. bug_insn_t insn;
  170. if (pc < VMALLOC_START)
  171. return 0;
  172. if (get_kernel_nofault(insn, (bug_insn_t *)pc))
  173. return 0;
  174. if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
  175. return (insn == __BUG_INSN_32);
  176. else
  177. return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
  178. }
  179. #endif /* CONFIG_GENERIC_BUG */
  180. #ifdef CONFIG_VMAP_STACK
  181. static DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
  182. overflow_stack)__aligned(16);
  183. /*
  184. * shadow stack, handled_ kernel_ stack_ overflow(in kernel/entry.S) is used
  185. * to get per-cpu overflow stack(get_overflow_stack).
  186. */
  187. long shadow_stack[SHADOW_OVERFLOW_STACK_SIZE/sizeof(long)] __aligned(16);
  188. asmlinkage unsigned long get_overflow_stack(void)
  189. {
  190. return (unsigned long)this_cpu_ptr(overflow_stack) +
  191. OVERFLOW_STACK_SIZE;
  192. }
  193. /*
  194. * A pseudo spinlock to protect the shadow stack from being used by multiple
  195. * harts concurrently. This isn't a real spinlock because the lock side must
  196. * be taken without a valid stack and only a single register, it's only taken
  197. * while in the process of panicing anyway so the performance and error
  198. * checking a proper spinlock gives us doesn't matter.
  199. */
  200. unsigned long spin_shadow_stack;
  201. asmlinkage void handle_bad_stack(struct pt_regs *regs)
  202. {
  203. unsigned long tsk_stk = (unsigned long)current->stack;
  204. unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
  205. /*
  206. * We're done with the shadow stack by this point, as we're on the
  207. * overflow stack. Tell any other concurrent overflowing harts that
  208. * they can proceed with panicing by releasing the pseudo-spinlock.
  209. *
  210. * This pairs with an amoswap.aq in handle_kernel_stack_overflow.
  211. */
  212. smp_store_release(&spin_shadow_stack, 0);
  213. console_verbose();
  214. pr_emerg("Insufficient stack space to handle exception!\n");
  215. pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
  216. tsk_stk, tsk_stk + THREAD_SIZE);
  217. pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
  218. ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
  219. __show_regs(regs);
  220. panic("Kernel stack overflow");
  221. for (;;)
  222. wait_for_interrupt();
  223. }
  224. #endif