fault.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/system.h>
  15. #include <asm/uaccess.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. siginfo_t siginfo = { 0, 0, 0, };
  21. siginfo.si_signo = current->thread.signo;
  22. siginfo.si_code = current->thread.code;
  23. siginfo.si_addr = (void *)current->thread.faddr;
  24. #ifdef DEBUG
  25. printk("send_fault_sig: %p,%d,%d\n", siginfo.si_addr, siginfo.si_signo, siginfo.si_code);
  26. #endif
  27. if (user_mode(regs)) {
  28. force_sig_info(siginfo.si_signo,
  29. &siginfo, current);
  30. } else {
  31. if (handle_kernel_fault(regs))
  32. return -1;
  33. //if (siginfo.si_signo == SIGBUS)
  34. // force_sig_info(siginfo.si_signo,
  35. // &siginfo, current);
  36. /*
  37. * Oops. The kernel tried to access some bad page. We'll have to
  38. * terminate things with extreme prejudice.
  39. */
  40. if ((unsigned long)siginfo.si_addr < PAGE_SIZE)
  41. printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
  42. else
  43. printk(KERN_ALERT "Unable to handle kernel access");
  44. printk(" at virtual address %p\n", siginfo.si_addr);
  45. die_if_kernel("Oops", regs, 0 /*error_code*/);
  46. do_exit(SIGKILL);
  47. }
  48. return 1;
  49. }
  50. /*
  51. * This routine handles page faults. It determines the problem, and
  52. * then passes it off to one of the appropriate routines.
  53. *
  54. * error_code:
  55. * bit 0 == 0 means no page found, 1 means protection fault
  56. * bit 1 == 0 means read, 1 means write
  57. *
  58. * If this routine detects a bad access, it returns 1, otherwise it
  59. * returns 0.
  60. */
  61. int do_page_fault(struct pt_regs *regs, unsigned long address,
  62. unsigned long error_code)
  63. {
  64. struct mm_struct *mm = current->mm;
  65. struct vm_area_struct * vma;
  66. int write, fault;
  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. down_read(&mm->mmap_sem);
  79. vma = find_vma(mm, address);
  80. if (!vma)
  81. goto map_err;
  82. if (vma->vm_flags & VM_IO)
  83. goto acc_err;
  84. if (vma->vm_start <= address)
  85. goto good_area;
  86. if (!(vma->vm_flags & VM_GROWSDOWN))
  87. goto map_err;
  88. if (user_mode(regs)) {
  89. /* Accessing the stack below usp is always a bug. The
  90. "+ 256" is there due to some instructions doing
  91. pre-decrement on the stack and that doesn't show up
  92. until later. */
  93. if (address + 256 < rdusp())
  94. goto map_err;
  95. }
  96. if (expand_stack(vma, address))
  97. goto map_err;
  98. /*
  99. * Ok, we have a good vm_area for this memory access, so
  100. * we can handle it..
  101. */
  102. good_area:
  103. #ifdef DEBUG
  104. printk("do_page_fault: good_area\n");
  105. #endif
  106. write = 0;
  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. 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, write ? FAULT_FLAG_WRITE : 0);
  127. #ifdef DEBUG
  128. printk("handle_mm_fault returns %d\n",fault);
  129. #endif
  130. if (unlikely(fault & VM_FAULT_ERROR)) {
  131. if (fault & VM_FAULT_OOM)
  132. goto out_of_memory;
  133. else if (fault & VM_FAULT_SIGBUS)
  134. goto bus_err;
  135. BUG();
  136. }
  137. if (fault & VM_FAULT_MAJOR)
  138. current->maj_flt++;
  139. else
  140. current->min_flt++;
  141. up_read(&mm->mmap_sem);
  142. return 0;
  143. /*
  144. * We ran out of memory, or some other thing happened to us that made
  145. * us unable to handle the page fault gracefully.
  146. */
  147. out_of_memory:
  148. up_read(&mm->mmap_sem);
  149. if (!user_mode(regs))
  150. goto no_context;
  151. pagefault_out_of_memory();
  152. return 0;
  153. no_context:
  154. current->thread.signo = SIGBUS;
  155. current->thread.faddr = address;
  156. return send_fault_sig(regs);
  157. bus_err:
  158. current->thread.signo = SIGBUS;
  159. current->thread.code = BUS_ADRERR;
  160. current->thread.faddr = address;
  161. goto send_sig;
  162. map_err:
  163. current->thread.signo = SIGSEGV;
  164. current->thread.code = SEGV_MAPERR;
  165. current->thread.faddr = address;
  166. goto send_sig;
  167. acc_err:
  168. current->thread.signo = SIGSEGV;
  169. current->thread.code = SEGV_ACCERR;
  170. current->thread.faddr = address;
  171. send_sig:
  172. up_read(&mm->mmap_sem);
  173. return send_fault_sig(regs);
  174. }