fault.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 write, fault;
  66. #ifdef DEBUG
  67. printk ("do page fault:\nregs->sr=%#x, regs->pc=%#lx, address=%#lx, %ld, %p\n",
  68. regs->sr, regs->pc, address, error_code,
  69. current->mm->pgd);
  70. #endif
  71. /*
  72. * If we're in an interrupt or have no user
  73. * context, we must not take the fault..
  74. */
  75. if (in_atomic() || !mm)
  76. goto no_context;
  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. #ifdef DEBUG
  103. printk("do_page_fault: good_area\n");
  104. #endif
  105. write = 0;
  106. switch (error_code & 3) {
  107. default: /* 3: write, present */
  108. /* fall through */
  109. case 2: /* write, not present */
  110. if (!(vma->vm_flags & VM_WRITE))
  111. goto acc_err;
  112. write++;
  113. break;
  114. case 1: /* read, present */
  115. goto acc_err;
  116. case 0: /* read, not present */
  117. if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
  118. goto acc_err;
  119. }
  120. /*
  121. * If for any reason at all we couldn't handle the fault,
  122. * make sure we exit gracefully rather than endlessly redo
  123. * the fault.
  124. */
  125. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  126. #ifdef DEBUG
  127. printk("handle_mm_fault returns %d\n",fault);
  128. #endif
  129. if (unlikely(fault & VM_FAULT_ERROR)) {
  130. if (fault & VM_FAULT_OOM)
  131. goto out_of_memory;
  132. else if (fault & VM_FAULT_SIGSEGV)
  133. goto map_err;
  134. else if (fault & VM_FAULT_SIGBUS)
  135. goto bus_err;
  136. BUG();
  137. }
  138. if (fault & VM_FAULT_MAJOR)
  139. current->maj_flt++;
  140. else
  141. current->min_flt++;
  142. up_read(&mm->mmap_sem);
  143. return 0;
  144. /*
  145. * We ran out of memory, or some other thing happened to us that made
  146. * us unable to handle the page fault gracefully.
  147. */
  148. out_of_memory:
  149. up_read(&mm->mmap_sem);
  150. if (!user_mode(regs))
  151. goto no_context;
  152. pagefault_out_of_memory();
  153. return 0;
  154. no_context:
  155. current->thread.signo = SIGBUS;
  156. current->thread.faddr = address;
  157. return send_fault_sig(regs);
  158. bus_err:
  159. current->thread.signo = SIGBUS;
  160. current->thread.code = BUS_ADRERR;
  161. current->thread.faddr = address;
  162. goto send_sig;
  163. map_err:
  164. current->thread.signo = SIGSEGV;
  165. current->thread.code = SEGV_MAPERR;
  166. current->thread.faddr = address;
  167. goto send_sig;
  168. acc_err:
  169. current->thread.signo = SIGSEGV;
  170. current->thread.code = SEGV_ACCERR;
  171. current->thread.faddr = address;
  172. send_sig:
  173. up_read(&mm->mmap_sem);
  174. return send_fault_sig(regs);
  175. }