fault.c 5.3 KB

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