traps.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
  3. #include <linux/sched.h>
  4. #include <linux/signal.h>
  5. #include <linux/kernel.h>
  6. #include <linux/mm.h>
  7. #include <linux/module.h>
  8. #include <linux/user.h>
  9. #include <linux/string.h>
  10. #include <linux/linkage.h>
  11. #include <linux/init.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/kallsyms.h>
  14. #include <linux/rtc.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/kdebug.h>
  18. #include <linux/sched/debug.h>
  19. #include <asm/setup.h>
  20. #include <asm/traps.h>
  21. #include <asm/pgalloc.h>
  22. #include <asm/siginfo.h>
  23. #include <asm/mmu_context.h>
  24. #ifdef CONFIG_CPU_HAS_FPU
  25. #include <abi/fpu.h>
  26. #endif
  27. int show_unhandled_signals = 1;
  28. /* Defined in entry.S */
  29. asmlinkage void csky_trap(void);
  30. asmlinkage void csky_systemcall(void);
  31. asmlinkage void csky_cmpxchg(void);
  32. asmlinkage void csky_get_tls(void);
  33. asmlinkage void csky_irq(void);
  34. asmlinkage void csky_pagefault(void);
  35. /* Defined in head.S */
  36. asmlinkage void _start_smp_secondary(void);
  37. void __init pre_trap_init(void)
  38. {
  39. int i;
  40. mtcr("vbr", vec_base);
  41. for (i = 1; i < 128; i++)
  42. VEC_INIT(i, csky_trap);
  43. }
  44. void __init trap_init(void)
  45. {
  46. VEC_INIT(VEC_AUTOVEC, csky_irq);
  47. /* setup trap0 trap2 trap3 */
  48. VEC_INIT(VEC_TRAP0, csky_systemcall);
  49. VEC_INIT(VEC_TRAP2, csky_cmpxchg);
  50. VEC_INIT(VEC_TRAP3, csky_get_tls);
  51. /* setup MMU TLB exception */
  52. VEC_INIT(VEC_TLBINVALIDL, csky_pagefault);
  53. VEC_INIT(VEC_TLBINVALIDS, csky_pagefault);
  54. VEC_INIT(VEC_TLBMODIFIED, csky_pagefault);
  55. #ifdef CONFIG_CPU_HAS_FPU
  56. init_fpu();
  57. #endif
  58. #ifdef CONFIG_SMP
  59. mtcr("cr<28, 0>", virt_to_phys(vec_base));
  60. VEC_INIT(VEC_RESET, (void *)virt_to_phys(_start_smp_secondary));
  61. #endif
  62. }
  63. static DEFINE_SPINLOCK(die_lock);
  64. void die(struct pt_regs *regs, const char *str)
  65. {
  66. static int die_counter;
  67. int ret;
  68. oops_enter();
  69. spin_lock_irq(&die_lock);
  70. console_verbose();
  71. bust_spinlocks(1);
  72. pr_emerg("%s [#%d]\n", str, ++die_counter);
  73. print_modules();
  74. show_regs(regs);
  75. show_stack(current, (unsigned long *)regs->regs[4], KERN_INFO);
  76. ret = notify_die(DIE_OOPS, str, regs, 0, trap_no(regs), SIGSEGV);
  77. bust_spinlocks(0);
  78. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  79. spin_unlock_irq(&die_lock);
  80. oops_exit();
  81. if (in_interrupt())
  82. panic("Fatal exception in interrupt");
  83. if (panic_on_oops)
  84. panic("Fatal exception");
  85. if (ret != NOTIFY_STOP)
  86. make_task_dead(SIGSEGV);
  87. }
  88. void do_trap(struct pt_regs *regs, int signo, int code, unsigned long addr)
  89. {
  90. struct task_struct *tsk = current;
  91. if (show_unhandled_signals && unhandled_signal(tsk, signo)
  92. && printk_ratelimit()) {
  93. pr_info("%s[%d]: unhandled signal %d code 0x%x at 0x%08lx",
  94. tsk->comm, task_pid_nr(tsk), signo, code, addr);
  95. print_vma_addr(KERN_CONT " in ", instruction_pointer(regs));
  96. pr_cont("\n");
  97. show_regs(regs);
  98. }
  99. force_sig_fault(signo, code, (void __user *)addr);
  100. }
  101. static void do_trap_error(struct pt_regs *regs, int signo, int code,
  102. unsigned long addr, const char *str)
  103. {
  104. current->thread.trap_no = trap_no(regs);
  105. if (user_mode(regs)) {
  106. do_trap(regs, signo, code, addr);
  107. } else {
  108. if (!fixup_exception(regs))
  109. die(regs, str);
  110. }
  111. }
  112. #define DO_ERROR_INFO(name, signo, code, str) \
  113. asmlinkage __visible void name(struct pt_regs *regs) \
  114. { \
  115. do_trap_error(regs, signo, code, regs->pc, "Oops - " str); \
  116. }
  117. DO_ERROR_INFO(do_trap_unknown,
  118. SIGILL, ILL_ILLTRP, "unknown exception");
  119. DO_ERROR_INFO(do_trap_zdiv,
  120. SIGFPE, FPE_INTDIV, "error zero div exception");
  121. DO_ERROR_INFO(do_trap_buserr,
  122. SIGSEGV, ILL_ILLADR, "error bus error exception");
  123. asmlinkage void do_trap_misaligned(struct pt_regs *regs)
  124. {
  125. #ifdef CONFIG_CPU_NEED_SOFTALIGN
  126. csky_alignment(regs);
  127. #else
  128. current->thread.trap_no = trap_no(regs);
  129. do_trap_error(regs, SIGBUS, BUS_ADRALN, regs->pc,
  130. "Oops - load/store address misaligned");
  131. #endif
  132. }
  133. asmlinkage void do_trap_bkpt(struct pt_regs *regs)
  134. {
  135. #ifdef CONFIG_KPROBES
  136. if (kprobe_single_step_handler(regs))
  137. return;
  138. #endif
  139. #ifdef CONFIG_UPROBES
  140. if (uprobe_single_step_handler(regs))
  141. return;
  142. #endif
  143. if (user_mode(regs)) {
  144. send_sig(SIGTRAP, current, 0);
  145. return;
  146. }
  147. do_trap_error(regs, SIGILL, ILL_ILLTRP, regs->pc,
  148. "Oops - illegal trap exception");
  149. }
  150. asmlinkage void do_trap_illinsn(struct pt_regs *regs)
  151. {
  152. current->thread.trap_no = trap_no(regs);
  153. #ifdef CONFIG_KPROBES
  154. if (kprobe_breakpoint_handler(regs))
  155. return;
  156. #endif
  157. #ifdef CONFIG_UPROBES
  158. if (uprobe_breakpoint_handler(regs))
  159. return;
  160. #endif
  161. #ifndef CONFIG_CPU_NO_USER_BKPT
  162. if (*(uint16_t *)instruction_pointer(regs) != USR_BKPT) {
  163. send_sig(SIGTRAP, current, 0);
  164. return;
  165. }
  166. #endif
  167. do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->pc,
  168. "Oops - illegal instruction exception");
  169. }
  170. asmlinkage void do_trap_fpe(struct pt_regs *regs)
  171. {
  172. #ifdef CONFIG_CPU_HAS_FPU
  173. return fpu_fpe(regs);
  174. #else
  175. do_trap_error(regs, SIGILL, ILL_ILLOPC, regs->pc,
  176. "Oops - fpu instruction exception");
  177. #endif
  178. }
  179. asmlinkage void do_trap_priv(struct pt_regs *regs)
  180. {
  181. #ifdef CONFIG_CPU_HAS_FPU
  182. if (user_mode(regs) && fpu_libc_helper(regs))
  183. return;
  184. #endif
  185. do_trap_error(regs, SIGILL, ILL_PRVOPC, regs->pc,
  186. "Oops - illegal privileged exception");
  187. }
  188. asmlinkage void trap_c(struct pt_regs *regs)
  189. {
  190. switch (trap_no(regs)) {
  191. case VEC_ZERODIV:
  192. do_trap_zdiv(regs);
  193. break;
  194. case VEC_TRACE:
  195. do_trap_bkpt(regs);
  196. break;
  197. case VEC_ILLEGAL:
  198. do_trap_illinsn(regs);
  199. break;
  200. case VEC_TRAP1:
  201. case VEC_BREAKPOINT:
  202. do_trap_bkpt(regs);
  203. break;
  204. case VEC_ACCESS:
  205. do_trap_buserr(regs);
  206. break;
  207. case VEC_ALIGN:
  208. do_trap_misaligned(regs);
  209. break;
  210. case VEC_FPE:
  211. do_trap_fpe(regs);
  212. break;
  213. case VEC_PRIV:
  214. do_trap_priv(regs);
  215. break;
  216. default:
  217. do_trap_unknown(regs);
  218. break;
  219. }
  220. }