traps.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. void do_trap(struct pt_regs *regs, int signo, int code,
  54. unsigned long addr, struct task_struct *tsk)
  55. {
  56. if (show_unhandled_signals && unhandled_signal(tsk, signo)
  57. && printk_ratelimit()) {
  58. pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x" REG_FMT,
  59. tsk->comm, task_pid_nr(tsk), signo, code, addr);
  60. print_vma_addr(KERN_CONT " in ", GET_IP(regs));
  61. pr_cont("\n");
  62. show_regs(regs);
  63. }
  64. force_sig_fault(signo, code, (void __user *)addr, tsk);
  65. }
  66. static void do_trap_error(struct pt_regs *regs, int signo, int code,
  67. unsigned long addr, const char *str)
  68. {
  69. if (user_mode(regs)) {
  70. do_trap(regs, signo, code, addr, current);
  71. } else {
  72. if (!fixup_exception(regs))
  73. die(regs, str);
  74. }
  75. }
  76. #define DO_ERROR_INFO(name, signo, code, str) \
  77. asmlinkage void name(struct pt_regs *regs) \
  78. { \
  79. do_trap_error(regs, signo, code, regs->sepc, "Oops - " str); \
  80. }
  81. DO_ERROR_INFO(do_trap_unknown,
  82. SIGILL, ILL_ILLTRP, "unknown exception");
  83. DO_ERROR_INFO(do_trap_insn_misaligned,
  84. SIGBUS, BUS_ADRALN, "instruction address misaligned");
  85. DO_ERROR_INFO(do_trap_insn_fault,
  86. SIGSEGV, SEGV_ACCERR, "instruction access fault");
  87. DO_ERROR_INFO(do_trap_insn_illegal,
  88. SIGILL, ILL_ILLOPC, "illegal instruction");
  89. DO_ERROR_INFO(do_trap_load_misaligned,
  90. SIGBUS, BUS_ADRALN, "load address misaligned");
  91. DO_ERROR_INFO(do_trap_load_fault,
  92. SIGSEGV, SEGV_ACCERR, "load access fault");
  93. DO_ERROR_INFO(do_trap_store_misaligned,
  94. SIGBUS, BUS_ADRALN, "store (or AMO) address misaligned");
  95. DO_ERROR_INFO(do_trap_store_fault,
  96. SIGSEGV, SEGV_ACCERR, "store (or AMO) access fault");
  97. DO_ERROR_INFO(do_trap_ecall_u,
  98. SIGILL, ILL_ILLTRP, "environment call from U-mode");
  99. DO_ERROR_INFO(do_trap_ecall_s,
  100. SIGILL, ILL_ILLTRP, "environment call from S-mode");
  101. DO_ERROR_INFO(do_trap_ecall_m,
  102. SIGILL, ILL_ILLTRP, "environment call from M-mode");
  103. asmlinkage void do_trap_break(struct pt_regs *regs)
  104. {
  105. #ifdef CONFIG_GENERIC_BUG
  106. if (!user_mode(regs)) {
  107. enum bug_trap_type type;
  108. type = report_bug(regs->sepc, regs);
  109. switch (type) {
  110. case BUG_TRAP_TYPE_NONE:
  111. break;
  112. case BUG_TRAP_TYPE_WARN:
  113. regs->sepc += sizeof(bug_insn_t);
  114. return;
  115. case BUG_TRAP_TYPE_BUG:
  116. die(regs, "Kernel BUG");
  117. }
  118. }
  119. #endif /* CONFIG_GENERIC_BUG */
  120. force_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *)(regs->sepc), current);
  121. }
  122. #ifdef CONFIG_GENERIC_BUG
  123. int is_valid_bugaddr(unsigned long pc)
  124. {
  125. bug_insn_t insn;
  126. if (pc < PAGE_OFFSET)
  127. return 0;
  128. if (probe_kernel_address((bug_insn_t *)pc, insn))
  129. return 0;
  130. return (insn == __BUG_INSN);
  131. }
  132. #endif /* CONFIG_GENERIC_BUG */
  133. void __init trap_init(void)
  134. {
  135. /*
  136. * Set sup0 scratch register to 0, indicating to exception vector
  137. * that we are presently executing in the kernel
  138. */
  139. csr_write(sscratch, 0);
  140. /* Set the exception vector address */
  141. csr_write(stvec, &handle_exception);
  142. /* Enable all interrupts */
  143. csr_write(sie, -1);
  144. }