fault.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. retry:
  79. down_read(&mm->mmap_sem);
  80. vma = find_vma(mm, address);
  81. if (!vma)
  82. goto map_err;
  83. if (vma->vm_flags & VM_IO)
  84. goto acc_err;
  85. if (vma->vm_start <= address)
  86. goto good_area;
  87. if (!(vma->vm_flags & VM_GROWSDOWN))
  88. goto map_err;
  89. if (user_mode(regs)) {
  90. /* Accessing the stack below usp is always a bug. The
  91. "+ 256" is there due to some instructions doing
  92. pre-decrement on the stack and that doesn't show up
  93. until later. */
  94. if (address + 256 < rdusp())
  95. goto map_err;
  96. }
  97. if (expand_stack(vma, address))
  98. goto map_err;
  99. /*
  100. * Ok, we have a good vm_area for this memory access, so
  101. * we can handle it..
  102. */
  103. good_area:
  104. #ifdef DEBUG
  105. printk("do_page_fault: good_area\n");
  106. #endif
  107. switch (error_code & 3) {
  108. default: /* 3: write, present */
  109. /* fall through */
  110. case 2: /* write, not present */
  111. if (!(vma->vm_flags & VM_WRITE))
  112. goto acc_err;
  113. flags |= FAULT_FLAG_WRITE;
  114. break;
  115. case 1: /* read, present */
  116. goto acc_err;
  117. case 0: /* read, not present */
  118. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  119. goto acc_err;
  120. }
  121. /*
  122. * If for any reason at all we couldn't handle the fault,
  123. * make sure we exit gracefully rather than endlessly redo
  124. * the fault.
  125. */
  126. fault = handle_mm_fault(mm, vma, address, flags);
  127. #ifdef DEBUG
  128. printk("handle_mm_fault returns %d\n",fault);
  129. #endif
  130. if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(current))
  131. return 0;
  132. if (unlikely(fault & VM_FAULT_ERROR)) {
  133. if (fault & VM_FAULT_OOM)
  134. goto out_of_memory;
  135. else if (fault & VM_FAULT_SIGBUS)
  136. goto bus_err;
  137. BUG();
  138. }
  139. /*
  140. * Major/minor page fault accounting is only done on the
  141. * initial attempt. If we go through a retry, it is extremely
  142. * likely that the page will be found in page cache at that point.
  143. */
  144. if (flags & FAULT_FLAG_ALLOW_RETRY) {
  145. if (fault & VM_FAULT_MAJOR)
  146. current->maj_flt++;
  147. else
  148. current->min_flt++;
  149. if (fault & VM_FAULT_RETRY) {
  150. /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
  151. * of starvation. */
  152. flags &= ~FAULT_FLAG_ALLOW_RETRY;
  153. /*
  154. * No need to up_read(&mm->mmap_sem) as we would
  155. * have already released it in __lock_page_or_retry
  156. * in mm/filemap.c.
  157. */
  158. goto retry;
  159. }
  160. }
  161. up_read(&mm->mmap_sem);
  162. return 0;
  163. /*
  164. * We ran out of memory, or some other thing happened to us that made
  165. * us unable to handle the page fault gracefully.
  166. */
  167. out_of_memory:
  168. up_read(&mm->mmap_sem);
  169. if (!user_mode(regs))
  170. goto no_context;
  171. pagefault_out_of_memory();
  172. return 0;
  173. no_context:
  174. current->thread.signo = SIGBUS;
  175. current->thread.faddr = address;
  176. return send_fault_sig(regs);
  177. bus_err:
  178. current->thread.signo = SIGBUS;
  179. current->thread.code = BUS_ADRERR;
  180. current->thread.faddr = address;
  181. goto send_sig;
  182. map_err:
  183. current->thread.signo = SIGSEGV;
  184. current->thread.code = SEGV_MAPERR;
  185. current->thread.faddr = address;
  186. goto send_sig;
  187. acc_err:
  188. current->thread.signo = SIGSEGV;
  189. current->thread.code = SEGV_ACCERR;
  190. current->thread.faddr = address;
  191. send_sig:
  192. up_read(&mm->mmap_sem);
  193. return send_fault_sig(regs);
  194. }