traps.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Traps/Non-MMU Exception handling for ARC
  4. *
  5. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  6. *
  7. * vineetg: May 2011
  8. * -user-space unaligned access emulation
  9. *
  10. * Rahul Trivedi: Codito Technologies 2004
  11. */
  12. #include <linux/sched/signal.h>
  13. #include <linux/kdebug.h>
  14. #include <linux/uaccess.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/kprobes.h>
  17. #include <linux/kgdb.h>
  18. #include <asm/setup.h>
  19. #include <asm/unaligned.h>
  20. #include <asm/kprobes.h>
  21. void __init trap_init(void)
  22. {
  23. return;
  24. }
  25. void die(const char *str, struct pt_regs *regs, unsigned long address)
  26. {
  27. show_kernel_fault_diag(str, regs, address);
  28. /* DEAD END */
  29. __asm__("flag 1");
  30. }
  31. /*
  32. * Helper called for bulk of exceptions NOT needing specific handling
  33. * -for user faults enqueues requested signal
  34. * -for kernel, chk if due to copy_(to|from)_user, otherwise die()
  35. */
  36. static noinline int
  37. unhandled_exception(const char *str, struct pt_regs *regs,
  38. int signo, int si_code, void __user *addr)
  39. {
  40. if (user_mode(regs)) {
  41. struct task_struct *tsk = current;
  42. tsk->thread.fault_address = (__force unsigned int)addr;
  43. force_sig_fault(signo, si_code, addr);
  44. } else {
  45. /* If not due to copy_(to|from)_user, we are doomed */
  46. if (fixup_exception(regs))
  47. return 0;
  48. die(str, regs, (unsigned long)addr);
  49. }
  50. return 1;
  51. }
  52. #define DO_ERROR_INFO(signr, str, name, sicode) \
  53. int name(unsigned long address, struct pt_regs *regs) \
  54. { \
  55. return unhandled_exception(str, regs, signr, sicode, \
  56. (void __user *)address); \
  57. }
  58. /*
  59. * Entry points for exceptions NOT needing specific handling
  60. */
  61. DO_ERROR_INFO(SIGILL, "Priv Op/Disabled Extn", do_privilege_fault, ILL_PRVOPC)
  62. DO_ERROR_INFO(SIGILL, "Invalid Extn Insn", do_extension_fault, ILL_ILLOPC)
  63. DO_ERROR_INFO(SIGILL, "Illegal Insn (or Seq)", insterror_is_error, ILL_ILLOPC)
  64. DO_ERROR_INFO(SIGBUS, "Invalid Mem Access", __weak do_memory_error, BUS_ADRERR)
  65. DO_ERROR_INFO(SIGTRAP, "Breakpoint Set", trap_is_brkpt, TRAP_BRKPT)
  66. DO_ERROR_INFO(SIGBUS, "Misaligned Access", do_misaligned_error, BUS_ADRALN)
  67. DO_ERROR_INFO(SIGSEGV, "gcc generated __builtin_trap", do_trap5_error, 0)
  68. /*
  69. * Entry Point for Misaligned Data access Exception, for emulating in software
  70. */
  71. int do_misaligned_access(unsigned long address, struct pt_regs *regs,
  72. struct callee_regs *cregs)
  73. {
  74. /* If emulation not enabled, or failed, kill the task */
  75. if (misaligned_fixup(address, regs, cregs) != 0)
  76. return do_misaligned_error(address, regs);
  77. return 0;
  78. }
  79. /*
  80. * Entry point for miscll errors such as Nested Exceptions
  81. * -Duplicate TLB entry is handled seperately though
  82. */
  83. void do_machine_check_fault(unsigned long address, struct pt_regs *regs)
  84. {
  85. die("Unhandled Machine Check Exception", regs, address);
  86. }
  87. /*
  88. * Entry point for traps induced by ARCompact TRAP_S <n> insn
  89. * This is same family as TRAP0/SWI insn (use the same vector).
  90. * The only difference being SWI insn take no operand, while TRAP_S does
  91. * which reflects in ECR Reg as 8 bit param.
  92. * Thus TRAP_S <n> can be used for specific purpose
  93. * -1 used for software breakpointing (gdb)
  94. * -2 used by kprobes
  95. * -5 __builtin_trap() generated by gcc (2018.03 onwards) for toggle such as
  96. * -fno-isolate-erroneous-paths-dereference
  97. */
  98. void do_non_swi_trap(unsigned long address, struct pt_regs *regs)
  99. {
  100. unsigned int param = regs->ecr_param;
  101. switch (param) {
  102. case 1:
  103. trap_is_brkpt(address, regs);
  104. break;
  105. case 2:
  106. trap_is_kprobe(address, regs);
  107. break;
  108. case 3:
  109. case 4:
  110. kgdb_trap(regs);
  111. break;
  112. case 5:
  113. do_trap5_error(address, regs);
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. /*
  120. * Entry point for Instruction Error Exception
  121. * -For a corner case, ARC kprobes implementation resorts to using
  122. * this exception, hence the check
  123. */
  124. void do_insterror_or_kprobe(unsigned long address, struct pt_regs *regs)
  125. {
  126. int rc;
  127. /* Check if this exception is caused by kprobes */
  128. rc = notify_die(DIE_IERR, "kprobe_ierr", regs, address, 0, SIGILL);
  129. if (rc == NOTIFY_STOP)
  130. return;
  131. insterror_is_error(address, regs);
  132. }
  133. /*
  134. * abort() call generated by older gcc for __builtin_trap()
  135. */
  136. void abort(void)
  137. {
  138. __asm__ __volatile__("trap_s 5\n");
  139. }