traps.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/irqflags.h>
  9. #include <linux/randomize_kstack.h>
  10. #include <linux/sched.h>
  11. #include <linux/sched/debug.h>
  12. #include <linux/sched/signal.h>
  13. #include <linux/signal.h>
  14. #include <linux/kdebug.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/uprobes.h>
  18. #include <asm/uprobes.h>
  19. #include <linux/mm.h>
  20. #include <linux/module.h>
  21. #include <linux/irq.h>
  22. #include <linux/kexec.h>
  23. #include <linux/entry-common.h>
  24. #include <asm/asm-prototypes.h>
  25. #include <asm/bug.h>
  26. #include <asm/cfi.h>
  27. #include <asm/csr.h>
  28. #include <asm/processor.h>
  29. #include <asm/ptrace.h>
  30. #include <asm/syscall.h>
  31. #include <asm/thread_info.h>
  32. #include <asm/vector.h>
  33. #include <asm/irq_stack.h>
  34. int show_unhandled_signals = 1;
  35. static DEFINE_RAW_SPINLOCK(die_lock);
  36. static int copy_code(struct pt_regs *regs, u16 *val, const u16 *insns)
  37. {
  38. const void __user *uaddr = (__force const void __user *)insns;
  39. if (!user_mode(regs))
  40. return get_kernel_nofault(*val, insns);
  41. /* The user space code from other tasks cannot be accessed. */
  42. if (regs != task_pt_regs(current))
  43. return -EPERM;
  44. return copy_from_user_nofault(val, uaddr, sizeof(*val));
  45. }
  46. static void dump_instr(const char *loglvl, struct pt_regs *regs)
  47. {
  48. char str[sizeof("0000 ") * 12 + 2 + 1], *p = str;
  49. const u16 *insns = (u16 *)instruction_pointer(regs);
  50. long bad;
  51. u16 val;
  52. int i;
  53. for (i = -10; i < 2; i++) {
  54. bad = copy_code(regs, &val, &insns[i]);
  55. if (!bad) {
  56. p += sprintf(p, i == 0 ? "(%04hx) " : "%04hx ", val);
  57. } else {
  58. printk("%sCode: Unable to access instruction at 0x%px.\n",
  59. loglvl, &insns[i]);
  60. return;
  61. }
  62. }
  63. printk("%sCode: %s\n", loglvl, str);
  64. }
  65. void die(struct pt_regs *regs, const char *str)
  66. {
  67. static int die_counter;
  68. int ret;
  69. long cause;
  70. unsigned long flags;
  71. oops_enter();
  72. raw_spin_lock_irqsave(&die_lock, flags);
  73. console_verbose();
  74. bust_spinlocks(1);
  75. pr_emerg("%s [#%d]\n", str, ++die_counter);
  76. print_modules();
  77. if (regs) {
  78. show_regs(regs);
  79. dump_instr(KERN_EMERG, regs);
  80. }
  81. cause = regs ? regs->cause : -1;
  82. ret = notify_die(DIE_OOPS, str, regs, 0, cause, SIGSEGV);
  83. if (kexec_should_crash(current))
  84. crash_kexec(regs);
  85. bust_spinlocks(0);
  86. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  87. raw_spin_unlock_irqrestore(&die_lock, flags);
  88. oops_exit();
  89. if (in_interrupt())
  90. panic("Fatal exception in interrupt");
  91. if (panic_on_oops)
  92. panic("Fatal exception");
  93. if (ret != NOTIFY_STOP)
  94. make_task_dead(SIGSEGV);
  95. }
  96. void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
  97. {
  98. struct task_struct *tsk = current;
  99. if (show_unhandled_signals && unhandled_signal(tsk, signo)
  100. && printk_ratelimit()) {
  101. pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
  102. tsk->comm, task_pid_nr(tsk), signo, code, addr);
  103. print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
  104. pr_cont("\n");
  105. __show_regs(regs);
  106. dump_instr(KERN_INFO, regs);
  107. }
  108. force_sig_fault(signo, code, (void __user *)addr);
  109. }
  110. static void do_trap_error(struct pt_regs *regs, int signo, int code,
  111. unsigned long addr, const char *str)
  112. {
  113. current->thread.bad_cause = regs->cause;
  114. if (user_mode(regs)) {
  115. do_trap(regs, signo, code, addr);
  116. } else {
  117. if (!fixup_exception(regs))
  118. die(regs, str);
  119. }
  120. }
  121. #if defined(CONFIG_XIP_KERNEL) && defined(CONFIG_RISCV_ALTERNATIVE)
  122. #define __trap_section __noinstr_section(".xip.traps")
  123. #else
  124. #define __trap_section noinstr
  125. #endif
  126. #define DO_ERROR_INFO(name, signo, code, str) \
  127. asmlinkage __visible __trap_section void name(struct pt_regs *regs) \
  128. { \
  129. if (user_mode(regs)) { \
  130. irqentry_enter_from_user_mode(regs); \
  131. local_irq_enable(); \
  132. do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
  133. local_irq_disable(); \
  134. irqentry_exit_to_user_mode(regs); \
  135. } else { \
  136. irqentry_state_t state = irqentry_nmi_enter(regs); \
  137. do_trap_error(regs, signo, code, regs->epc, "Oops - " str); \
  138. irqentry_nmi_exit(regs, state); \
  139. } \
  140. }
  141. DO_ERROR_INFO(do_trap_unknown,
  142. SIGILL, ILL_ILLTRP, "unknown exception");
  143. DO_ERROR_INFO(do_trap_insn_misaligned,
  144. SIGBUS, BUS_ADRALN, "instruction address misaligned");
  145. DO_ERROR_INFO(do_trap_insn_fault,
  146. SIGSEGV, SEGV_ACCERR, "instruction access fault");
  147. asmlinkage __visible __trap_section void do_trap_insn_illegal(struct pt_regs *regs)
  148. {
  149. bool handled;
  150. if (user_mode(regs)) {
  151. irqentry_enter_from_user_mode(regs);
  152. local_irq_enable();
  153. handled = riscv_v_first_use_handler(regs);
  154. if (!handled)
  155. do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
  156. "Oops - illegal instruction");
  157. local_irq_disable();
  158. irqentry_exit_to_user_mode(regs);
  159. } else {
  160. irqentry_state_t state = irqentry_nmi_enter(regs);
  161. do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->epc,
  162. "Oops - illegal instruction");
  163. irqentry_nmi_exit(regs, state);
  164. }
  165. }
  166. DO_ERROR_INFO(do_trap_load_fault,
  167. SIGSEGV, SEGV_ACCERR, "load access fault");
  168. enum misaligned_access_type {
  169. MISALIGNED_STORE,
  170. MISALIGNED_LOAD,
  171. };
  172. static const struct {
  173. const char *type_str;
  174. int (*handler)(struct pt_regs *regs);
  175. } misaligned_handler[] = {
  176. [MISALIGNED_STORE] = {
  177. .type_str = "Oops - store (or AMO) address misaligned",
  178. .handler = handle_misaligned_store,
  179. },
  180. [MISALIGNED_LOAD] = {
  181. .type_str = "Oops - load address misaligned",
  182. .handler = handle_misaligned_load,
  183. },
  184. };
  185. static void do_trap_misaligned(struct pt_regs *regs, enum misaligned_access_type type)
  186. {
  187. irqentry_state_t state;
  188. if (user_mode(regs)) {
  189. irqentry_enter_from_user_mode(regs);
  190. local_irq_enable();
  191. } else {
  192. state = irqentry_nmi_enter(regs);
  193. }
  194. if (misaligned_handler[type].handler(regs))
  195. do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->epc,
  196. misaligned_handler[type].type_str);
  197. if (user_mode(regs)) {
  198. local_irq_disable();
  199. irqentry_exit_to_user_mode(regs);
  200. } else {
  201. irqentry_nmi_exit(regs, state);
  202. }
  203. }
  204. asmlinkage __visible __trap_section void do_trap_load_misaligned(struct pt_regs *regs)
  205. {
  206. do_trap_misaligned(regs, MISALIGNED_LOAD);
  207. }
  208. asmlinkage __visible __trap_section void do_trap_store_misaligned(struct pt_regs *regs)
  209. {
  210. do_trap_misaligned(regs, MISALIGNED_STORE);
  211. }
  212. DO_ERROR_INFO(do_trap_store_fault,
  213. SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
  214. DO_ERROR_INFO(do_trap_ecall_s,
  215. SIGILL, ILL_ILLTRP, "environment call from S-mode");
  216. DO_ERROR_INFO(do_trap_ecall_m,
  217. SIGILL, ILL_ILLTRP, "environment call from M-mode");
  218. static inline unsigned long get_break_insn_length(unsigned long pc)
  219. {
  220. bug_insn_t insn;
  221. if (get_kernel_nofault(insn, (bug_insn_t *)pc))
  222. return 0;
  223. return GET_INSN_LENGTH(insn);
  224. }
  225. static bool probe_single_step_handler(struct pt_regs *regs)
  226. {
  227. bool user = user_mode(regs);
  228. return user ? uprobe_single_step_handler(regs) : kprobe_single_step_handler(regs);
  229. }
  230. static bool probe_breakpoint_handler(struct pt_regs *regs)
  231. {
  232. bool user = user_mode(regs);
  233. return user ? uprobe_breakpoint_handler(regs) : kprobe_breakpoint_handler(regs);
  234. }
  235. void handle_break(struct pt_regs *regs)
  236. {
  237. if (probe_single_step_handler(regs))
  238. return;
  239. if (probe_breakpoint_handler(regs))
  240. return;
  241. current->thread.bad_cause = regs->cause;
  242. if (user_mode(regs))
  243. force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)regs->epc);
  244. #ifdef CONFIG_KGDB
  245. else if (notify_die(DIE_TRAP, "EBREAK", regs, 0, regs->cause, SIGTRAP)
  246. == NOTIFY_STOP)
  247. return;
  248. #endif
  249. else if (report_bug(regs->epc, regs) == BUG_TRAP_TYPE_WARN ||
  250. handle_cfi_failure(regs) == BUG_TRAP_TYPE_WARN)
  251. regs->epc += get_break_insn_length(regs->epc);
  252. else
  253. die(regs, "Kernel BUG");
  254. }
  255. asmlinkage __visible __trap_section void do_trap_break(struct pt_regs *regs)
  256. {
  257. if (user_mode(regs)) {
  258. irqentry_enter_from_user_mode(regs);
  259. local_irq_enable();
  260. handle_break(regs);
  261. local_irq_disable();
  262. irqentry_exit_to_user_mode(regs);
  263. } else {
  264. irqentry_state_t state = irqentry_nmi_enter(regs);
  265. handle_break(regs);
  266. irqentry_nmi_exit(regs, state);
  267. }
  268. }
  269. asmlinkage __visible __trap_section __no_stack_protector
  270. void do_trap_ecall_u(struct pt_regs *regs)
  271. {
  272. if (user_mode(regs)) {
  273. long syscall = regs->a7;
  274. regs->epc += 4;
  275. regs->orig_a0 = regs->a0;
  276. regs->a0 = -ENOSYS;
  277. riscv_v_vstate_discard(regs);
  278. syscall = syscall_enter_from_user_mode(regs, syscall);
  279. add_random_kstack_offset();
  280. if (syscall >= 0 && syscall < NR_syscalls)
  281. syscall_handler(regs, syscall);
  282. /*
  283. * Ultimately, this value will get limited by KSTACK_OFFSET_MAX(),
  284. * so the maximum stack offset is 1k bytes (10 bits).
  285. *
  286. * The actual entropy will be further reduced by the compiler when
  287. * applying stack alignment constraints: 16-byte (i.e. 4-bit) aligned
  288. * for RV32I or RV64I.
  289. *
  290. * The resulting 6 bits of entropy is seen in SP[9:4].
  291. */
  292. choose_random_kstack_offset(get_random_u16());
  293. syscall_exit_to_user_mode(regs);
  294. } else {
  295. irqentry_state_t state = irqentry_nmi_enter(regs);
  296. do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->epc,
  297. "Oops - environment call from U-mode");
  298. irqentry_nmi_exit(regs, state);
  299. }
  300. }
  301. #ifdef CONFIG_MMU
  302. asmlinkage __visible noinstr void do_page_fault(struct pt_regs *regs)
  303. {
  304. irqentry_state_t state = irqentry_enter(regs);
  305. handle_page_fault(regs);
  306. local_irq_disable();
  307. irqentry_exit(regs, state);
  308. }
  309. #endif
  310. static void noinstr handle_riscv_irq(struct pt_regs *regs)
  311. {
  312. struct pt_regs *old_regs;
  313. irq_enter_rcu();
  314. old_regs = set_irq_regs(regs);
  315. handle_arch_irq(regs);
  316. set_irq_regs(old_regs);
  317. irq_exit_rcu();
  318. }
  319. asmlinkage void noinstr do_irq(struct pt_regs *regs)
  320. {
  321. irqentry_state_t state = irqentry_enter(regs);
  322. if (IS_ENABLED(CONFIG_IRQ_STACKS) && on_thread_stack())
  323. call_on_irq_stack(regs, handle_riscv_irq);
  324. else
  325. handle_riscv_irq(regs);
  326. irqentry_exit(regs, state);
  327. }
  328. #ifdef CONFIG_GENERIC_BUG
  329. int is_valid_bugaddr(unsigned long pc)
  330. {
  331. bug_insn_t insn;
  332. if (pc < VMALLOC_START)
  333. return 0;
  334. if (get_kernel_nofault(insn, (bug_insn_t *)pc))
  335. return 0;
  336. if ((insn & __INSN_LENGTH_MASK) == __INSN_LENGTH_32)
  337. return (insn == __BUG_INSN_32);
  338. else
  339. return ((insn & __COMPRESSED_INSN_MASK) == __BUG_INSN_16);
  340. }
  341. #endif /* CONFIG_GENERIC_BUG */
  342. #ifdef CONFIG_VMAP_STACK
  343. DEFINE_PER_CPU(unsigned long [OVERFLOW_STACK_SIZE/sizeof(long)],
  344. overflow_stack)__aligned(16);
  345. asmlinkage void handle_bad_stack(struct pt_regs *regs)
  346. {
  347. unsigned long tsk_stk = (unsigned long)current->stack;
  348. unsigned long ovf_stk = (unsigned long)this_cpu_ptr(overflow_stack);
  349. console_verbose();
  350. pr_emerg("Insufficient stack space to handle exception!\n");
  351. pr_emerg("Task stack: [0x%016lx..0x%016lx]\n",
  352. tsk_stk, tsk_stk + THREAD_SIZE);
  353. pr_emerg("Overflow stack: [0x%016lx..0x%016lx]\n",
  354. ovf_stk, ovf_stk + OVERFLOW_STACK_SIZE);
  355. __show_regs(regs);
  356. panic("Kernel stack overflow");
  357. for (;;)
  358. wait_for_interrupt();
  359. }
  360. #endif