traps.c 5.6 KB

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