traps.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <asm/setup.h>
  17. #include <asm/traps.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/siginfo.h>
  20. #include <asm/mmu_context.h>
  21. #ifdef CONFIG_CPU_HAS_FPU
  22. #include <abi/fpu.h>
  23. #endif
  24. /* Defined in entry.S */
  25. asmlinkage void csky_trap(void);
  26. asmlinkage void csky_systemcall(void);
  27. asmlinkage void csky_cmpxchg(void);
  28. asmlinkage void csky_get_tls(void);
  29. asmlinkage void csky_irq(void);
  30. asmlinkage void csky_tlbinvalidl(void);
  31. asmlinkage void csky_tlbinvalids(void);
  32. asmlinkage void csky_tlbmodified(void);
  33. /* Defined in head.S */
  34. asmlinkage void _start_smp_secondary(void);
  35. void __init pre_trap_init(void)
  36. {
  37. int i;
  38. mtcr("vbr", vec_base);
  39. for (i = 1; i < 128; i++)
  40. VEC_INIT(i, csky_trap);
  41. }
  42. void __init trap_init(void)
  43. {
  44. VEC_INIT(VEC_AUTOVEC, csky_irq);
  45. /* setup trap0 trap2 trap3 */
  46. VEC_INIT(VEC_TRAP0, csky_systemcall);
  47. VEC_INIT(VEC_TRAP2, csky_cmpxchg);
  48. VEC_INIT(VEC_TRAP3, csky_get_tls);
  49. /* setup MMU TLB exception */
  50. VEC_INIT(VEC_TLBINVALIDL, csky_tlbinvalidl);
  51. VEC_INIT(VEC_TLBINVALIDS, csky_tlbinvalids);
  52. VEC_INIT(VEC_TLBMODIFIED, csky_tlbmodified);
  53. #ifdef CONFIG_CPU_HAS_FPU
  54. init_fpu();
  55. #endif
  56. #ifdef CONFIG_SMP
  57. mtcr("cr<28, 0>", virt_to_phys(vec_base));
  58. VEC_INIT(VEC_RESET, (void *)virt_to_phys(_start_smp_secondary));
  59. #endif
  60. }
  61. void die_if_kernel(char *str, struct pt_regs *regs, int nr)
  62. {
  63. if (user_mode(regs))
  64. return;
  65. console_verbose();
  66. pr_err("%s: %08x\n", str, nr);
  67. show_regs(regs);
  68. add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE);
  69. do_exit(SIGSEGV);
  70. }
  71. void buserr(struct pt_regs *regs)
  72. {
  73. #ifdef CONFIG_CPU_CK810
  74. static unsigned long prev_pc;
  75. if ((regs->pc == prev_pc) && prev_pc != 0) {
  76. prev_pc = 0;
  77. } else {
  78. prev_pc = regs->pc;
  79. return;
  80. }
  81. #endif
  82. die_if_kernel("Kernel mode BUS error", regs, 0);
  83. pr_err("User mode Bus Error\n");
  84. show_regs(regs);
  85. current->thread.esp0 = (unsigned long) regs;
  86. force_sig_fault(SIGSEGV, 0, (void __user *)regs->pc, current);
  87. }
  88. #define USR_BKPT 0x1464
  89. asmlinkage void trap_c(struct pt_regs *regs)
  90. {
  91. int sig;
  92. unsigned long vector;
  93. siginfo_t info;
  94. vector = (mfcr("psr") >> 16) & 0xff;
  95. switch (vector) {
  96. case VEC_ZERODIV:
  97. sig = SIGFPE;
  98. break;
  99. /* ptrace */
  100. case VEC_TRACE:
  101. info.si_code = TRAP_TRACE;
  102. sig = SIGTRAP;
  103. break;
  104. case VEC_ILLEGAL:
  105. #ifndef CONFIG_CPU_NO_USER_BKPT
  106. if (*(uint16_t *)instruction_pointer(regs) != USR_BKPT)
  107. #endif
  108. {
  109. sig = SIGILL;
  110. break;
  111. }
  112. /* gdbserver breakpoint */
  113. case VEC_TRAP1:
  114. /* jtagserver breakpoint */
  115. case VEC_BREAKPOINT:
  116. info.si_code = TRAP_BRKPT;
  117. sig = SIGTRAP;
  118. break;
  119. case VEC_ACCESS:
  120. return buserr(regs);
  121. #ifdef CONFIG_CPU_NEED_SOFTALIGN
  122. case VEC_ALIGN:
  123. return csky_alignment(regs);
  124. #endif
  125. #ifdef CONFIG_CPU_HAS_FPU
  126. case VEC_FPE:
  127. return fpu_fpe(regs);
  128. case VEC_PRIV:
  129. if (fpu_libc_helper(regs))
  130. return;
  131. #endif
  132. default:
  133. sig = SIGSEGV;
  134. break;
  135. }
  136. send_sig(sig, current, 0);
  137. }
  138. asmlinkage void set_esp0(unsigned long ssp)
  139. {
  140. current->thread.esp0 = ssp;
  141. }