fault.c 5.3 KB

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