traps.c 4.3 KB

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