fault.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <asm/setup.h>
  15. #include <asm/traps.h>
  16. #include <asm/pgalloc.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, current);
  28. } else {
  29. if (fixup_exception(regs))
  30. return -1;
  31. //if (signo == SIGBUS)
  32. // force_sig_fault(si_signo, si_code, addr, current);
  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. do_exit(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_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
  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. retry:
  76. down_read(&mm->mmap_sem);
  77. vma = find_vma(mm, address);
  78. if (!vma)
  79. goto map_err;
  80. if (vma->vm_flags & VM_IO)
  81. goto acc_err;
  82. if (vma->vm_start <= address)
  83. goto good_area;
  84. if (!(vma->vm_flags & VM_GROWSDOWN))
  85. goto map_err;
  86. if (user_mode(regs)) {
  87. /* Accessing the stack below usp is always a bug. The
  88. "+ 256" is there due to some instructions doing
  89. pre-decrement on the stack and that doesn't show up
  90. until later. */
  91. if (address + 256 < rdusp())
  92. goto map_err;
  93. }
  94. if (expand_stack(vma, address))
  95. goto map_err;
  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. /* fall through */
  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 (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  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);
  122. pr_debug("handle_mm_fault returns %x\n", fault);
  123. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  124. return 0;
  125. if (unlikely(fault & VM_FAULT_ERROR)) {
  126. if (fault & VM_FAULT_OOM)
  127. goto out_of_memory;
  128. else if (fault & VM_FAULT_SIGSEGV)
  129. goto map_err;
  130. else if (fault & VM_FAULT_SIGBUS)
  131. goto bus_err;
  132. BUG();
  133. }
  134. /*
  135. * Major/minor page fault accounting is only done on the
  136. * initial attempt. If we go through a retry, it is extremely
  137. * likely that the page will be found in page cache at that point.
  138. */
  139. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  140. if (fault & VM_FAULT_MAJOR)
  141. current->maj_flt++;
  142. else
  143. current->min_flt++;
  144. if (fault & VM_FAULT_RETRY) {
  145. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  146. * of starvation. */
  147. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  148. flags |= FAULT_FLAG_TRIED;
  149. /*
  150. * No need to up_read(&mm->mmap_sem) as we would
  151. * have already released it in __lock_page_or_retry
  152. * in mm/filemap.c.
  153. */
  154. goto retry;
  155. }
  156. }
  157. up_read(&mm->mmap_sem);
  158. return 0;
  159. /*
  160. * We ran out of memory, or some other thing happened to us that made
  161. * us unable to handle the page fault gracefully.
  162. */
  163. out_of_memory:
  164. up_read(&mm->mmap_sem);
  165. if (!user_mode(regs))
  166. goto no_context;
  167. pagefault_out_of_memory();
  168. return 0;
  169. no_context:
  170. current->thread.signo = SIGBUS;
  171. current->thread.faddr = address;
  172. return send_fault_sig(regs);
  173. bus_err:
  174. current->thread.signo = SIGBUS;
  175. current->thread.code = BUS_ADRERR;
  176. current->thread.faddr = address;
  177. goto send_sig;
  178. map_err:
  179. current->thread.signo = SIGSEGV;
  180. current->thread.code = SEGV_MAPERR;
  181. current->thread.faddr = address;
  182. goto send_sig;
  183. acc_err:
  184. current->thread.signo = SIGSEGV;
  185. current->thread.code = SEGV_ACCERR;
  186. current->thread.faddr = address;
  187. send_sig:
  188. up_read(&mm->mmap_sem);
  189. return send_fault_sig(regs);
  190. }