fault.c 5.2 KB

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