fault.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/arch/m68k/mm/fault.c
  4. *
  5. * Copyright (C) 1995 Hamish Macdonald
  6. */
  7. #include <linux/mman.h>
  8. #include <linux/mm.h>
  9. #include <linux/kernel.h>
  10. #include <linux/ptrace.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/perf_event.h>
  15. #include <asm/setup.h>
  16. #include <asm/traps.h>
  17. extern void die_if_kernel(char *, struct pt_regs *, long);
  18. int send_fault_sig(struct pt_regs *regs)
  19. {
  20. int signo, si_code;
  21. void __user *addr;
  22. signo = current->thread.signo;
  23. si_code = current->thread.code;
  24. addr = (void __user *)current->thread.faddr;
  25. pr_debug("send_fault_sig: %p,%d,%d\n", addr, signo, si_code);
  26. if (user_mode(regs)) {
  27. force_sig_fault(signo, si_code, addr);
  28. } else {
  29. if (fixup_exception(regs))
  30. return -1;
  31. //if (signo == SIGBUS)
  32. // force_sig_fault(si_signo, si_code, addr);
  33. /*
  34. * Oops. The kernel tried to access some bad page. We'll have to
  35. * terminate things with extreme prejudice.
  36. */
  37. if ((unsigned long)addr < PAGE_SIZE)
  38. pr_alert("Unable to handle kernel NULL pointer dereference");
  39. else
  40. pr_alert("Unable to handle kernel access");
  41. pr_cont(" at virtual address %p\n", addr);
  42. die_if_kernel("Oops", regs, 0 /*error_code*/);
  43. make_task_dead(SIGKILL);
  44. }
  45. return 1;
  46. }
  47. /*
  48. * This routine handles page faults. It determines the problem, and
  49. * then passes it off to one of the appropriate routines.
  50. *
  51. * error_code:
  52. * bit 0 == 0 means no page found, 1 means protection fault
  53. * bit 1 == 0 means read, 1 means write
  54. *
  55. * If this routine detects a bad access, it returns 1, otherwise it
  56. * returns 0.
  57. */
  58. int do_page_fault(struct pt_regs *regs, unsigned long address,
  59. unsigned long error_code)
  60. {
  61. struct mm_struct *mm = current->mm;
  62. struct vm_area_struct * vma;
  63. vm_fault_t fault;
  64. unsigned int flags = FAULT_FLAG_DEFAULT;
  65. pr_debug("do page fault:\nregs->sr=%#x, regs->pc=%#lx, address=%#lx, %ld, %p\n",
  66. regs->sr, regs->pc, address, error_code, mm ? mm->pgd : NULL);
  67. /*
  68. * If we're in an interrupt or have no user
  69. * context, we must not take the fault..
  70. */
  71. if (faulthandler_disabled() || !mm)
  72. goto no_context;
  73. if (user_mode(regs))
  74. flags |= FAULT_FLAG_USER;
  75. perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
  76. retry:
  77. mmap_read_lock(mm);
  78. vma = find_vma(mm, address);
  79. if (!vma)
  80. goto map_err;
  81. if (vma->vm_start <= address)
  82. goto good_area;
  83. if (!(vma->vm_flags & VM_GROWSDOWN))
  84. goto map_err;
  85. if (user_mode(regs)) {
  86. /* Accessing the stack below usp is always a bug. The
  87. "+ 256" is there due to some instructions doing
  88. pre-decrement on the stack and that doesn't show up
  89. until later. */
  90. if (address + 256 < rdusp())
  91. goto map_err;
  92. }
  93. vma = expand_stack(mm, address);
  94. if (!vma)
  95. goto map_err_nosemaphore;
  96. /*
  97. * Ok, we have a good vm_area for this memory access, so
  98. * we can handle it..
  99. */
  100. good_area:
  101. pr_debug("do_page_fault: good_area\n");
  102. switch (error_code & 3) {
  103. default: /* 3: write, present */
  104. fallthrough;
  105. case 2: /* write, not present */
  106. if (!(vma->vm_flags & VM_WRITE))
  107. goto acc_err;
  108. flags |= FAULT_FLAG_WRITE;
  109. break;
  110. case 1: /* read, present */
  111. goto acc_err;
  112. case 0: /* read, not present */
  113. if (unlikely(!vma_is_accessible(vma)))
  114. goto acc_err;
  115. }
  116. /*
  117. * If for any reason at all we couldn't handle the fault,
  118. * make sure we exit gracefully rather than endlessly redo
  119. * the fault.
  120. */
  121. fault = handle_mm_fault(vma, address, flags, regs);
  122. pr_debug("handle_mm_fault returns %x\n", fault);
  123. if (fault_signal_pending(fault, regs))
  124. return 0;
  125. /* The fault is fully completed (including releasing mmap lock) */
  126. if (fault & VM_FAULT_COMPLETED)
  127. return 0;
  128. if (unlikely(fault & VM_FAULT_ERROR)) {
  129. if (fault & VM_FAULT_OOM)
  130. goto out_of_memory;
  131. else if (fault & VM_FAULT_SIGSEGV)
  132. goto map_err;
  133. else if (fault & VM_FAULT_SIGBUS)
  134. goto bus_err;
  135. BUG();
  136. }
  137. if (fault & VM_FAULT_RETRY) {
  138. flags |= FAULT_FLAG_TRIED;
  139. /*
  140. * No need to mmap_read_unlock(mm) as we would
  141. * have already released it in __lock_page_or_retry
  142. * in mm/filemap.c.
  143. */
  144. goto retry;
  145. }
  146. mmap_read_unlock(mm);
  147. return 0;
  148. /*
  149. * We ran out of memory, or some other thing happened to us that made
  150. * us unable to handle the page fault gracefully.
  151. */
  152. out_of_memory:
  153. mmap_read_unlock(mm);
  154. if (!user_mode(regs))
  155. goto no_context;
  156. pagefault_out_of_memory();
  157. return 0;
  158. no_context:
  159. current->thread.signo = SIGBUS;
  160. current->thread.faddr = address;
  161. return send_fault_sig(regs);
  162. bus_err:
  163. current->thread.signo = SIGBUS;
  164. current->thread.code = BUS_ADRERR;
  165. current->thread.faddr = address;
  166. goto send_sig;
  167. map_err:
  168. mmap_read_unlock(mm);
  169. map_err_nosemaphore:
  170. current->thread.signo = SIGSEGV;
  171. current->thread.code = SEGV_MAPERR;
  172. current->thread.faddr = address;
  173. return send_fault_sig(regs);
  174. acc_err:
  175. current->thread.signo = SIGSEGV;
  176. current->thread.code = SEGV_ACCERR;
  177. current->thread.faddr = address;
  178. send_sig:
  179. mmap_read_unlock(mm);
  180. return send_fault_sig(regs);
  181. }