fault.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1995 - 2000 by Ralf Baechle
  7. */
  8. #include <linux/signal.h>
  9. #include <linux/sched.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/string.h>
  14. #include <linux/types.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/mman.h>
  17. #include <linux/mm.h>
  18. #include <linux/smp.h>
  19. #include <linux/smp_lock.h>
  20. #include <linux/vt_kern.h> /* For unblank_screen() */
  21. #include <linux/module.h>
  22. #include <asm/branch.h>
  23. #include <asm/mmu_context.h>
  24. #include <asm/system.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/ptrace.h>
  27. /*
  28. * This routine handles page faults. It determines the address,
  29. * and the problem, and then passes it off to one of the appropriate
  30. * routines.
  31. */
  32. asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
  33. unsigned long address)
  34. {
  35. struct vm_area_struct * vma = NULL;
  36. struct task_struct *tsk = current;
  37. struct mm_struct *mm = tsk->mm;
  38. const int field = sizeof(unsigned long) * 2;
  39. siginfo_t info;
  40. #if 0
  41. printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", smp_processor_id(),
  42. current->comm, current->pid, field, address, write,
  43. field, regs->cp0_epc);
  44. #endif
  45. info.si_code = SEGV_MAPERR;
  46. /*
  47. * We fault-in kernel-space virtual memory on-demand. The
  48. * 'reference' page table is init_mm.pgd.
  49. *
  50. * NOTE! We MUST NOT take any locks for this case. We may
  51. * be in an interrupt or a critical region, and should
  52. * only copy the information from the master page table,
  53. * nothing more.
  54. */
  55. if (unlikely(address >= VMALLOC_START))
  56. goto vmalloc_fault;
  57. /*
  58. * If we're in an interrupt or have no user
  59. * context, we must not take the fault..
  60. */
  61. if (in_atomic() || !mm)
  62. goto bad_area_nosemaphore;
  63. down_read(&mm->mmap_sem);
  64. vma = find_vma(mm, address);
  65. if (!vma)
  66. goto bad_area;
  67. if (vma->vm_start <= address)
  68. goto good_area;
  69. if (!(vma->vm_flags & VM_GROWSDOWN))
  70. goto bad_area;
  71. if (expand_stack(vma, address))
  72. goto bad_area;
  73. /*
  74. * Ok, we have a good vm_area for this memory access, so
  75. * we can handle it..
  76. */
  77. good_area:
  78. info.si_code = SEGV_ACCERR;
  79. if (write) {
  80. if (!(vma->vm_flags & VM_WRITE))
  81. goto bad_area;
  82. } else {
  83. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  84. goto bad_area;
  85. }
  86. survive:
  87. /*
  88. * If for any reason at all we couldn't handle the fault,
  89. * make sure we exit gracefully rather than endlessly redo
  90. * the fault.
  91. */
  92. switch (handle_mm_fault(mm, vma, address, write)) {
  93. case VM_FAULT_MINOR:
  94. tsk->min_flt++;
  95. break;
  96. case VM_FAULT_MAJOR:
  97. tsk->maj_flt++;
  98. break;
  99. case VM_FAULT_SIGBUS:
  100. goto do_sigbus;
  101. case VM_FAULT_OOM:
  102. goto out_of_memory;
  103. default:
  104. BUG();
  105. }
  106. up_read(&mm->mmap_sem);
  107. return;
  108. /*
  109. * Something tried to access memory that isn't in our memory map..
  110. * Fix it, but check if it's kernel or user first..
  111. */
  112. bad_area:
  113. up_read(&mm->mmap_sem);
  114. bad_area_nosemaphore:
  115. /* User mode accesses just cause a SIGSEGV */
  116. if (user_mode(regs)) {
  117. tsk->thread.cp0_badvaddr = address;
  118. tsk->thread.error_code = write;
  119. #if 0
  120. printk("do_page_fault() #2: sending SIGSEGV to %s for "
  121. "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
  122. tsk->comm,
  123. write ? "write access to" : "read access from",
  124. field, address,
  125. field, (unsigned long) regs->cp0_epc,
  126. field, (unsigned long) regs->regs[31]);
  127. #endif
  128. info.si_signo = SIGSEGV;
  129. info.si_errno = 0;
  130. /* info.si_code has been set above */
  131. info.si_addr = (void *) address;
  132. force_sig_info(SIGSEGV, &info, tsk);
  133. return;
  134. }
  135. no_context:
  136. /* Are we prepared to handle this kernel fault? */
  137. if (fixup_exception(regs)) {
  138. current->thread.cp0_baduaddr = address;
  139. return;
  140. }
  141. /*
  142. * Oops. The kernel tried to access some bad page. We'll have to
  143. * terminate things with extreme prejudice.
  144. */
  145. bust_spinlocks(1);
  146. printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
  147. "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
  148. smp_processor_id(), field, address, field, regs->cp0_epc,
  149. field, regs->regs[31]);
  150. die("Oops", regs);
  151. /*
  152. * We ran out of memory, or some other thing happened to us that made
  153. * us unable to handle the page fault gracefully.
  154. */
  155. out_of_memory:
  156. up_read(&mm->mmap_sem);
  157. if (tsk->pid == 1) {
  158. yield();
  159. down_read(&mm->mmap_sem);
  160. goto survive;
  161. }
  162. printk("VM: killing process %s\n", tsk->comm);
  163. if (user_mode(regs))
  164. do_exit(SIGKILL);
  165. goto no_context;
  166. do_sigbus:
  167. up_read(&mm->mmap_sem);
  168. /* Kernel mode? Handle exceptions or die */
  169. if (!user_mode(regs))
  170. goto no_context;
  171. /*
  172. * Send a sigbus, regardless of whether we were in kernel
  173. * or user mode.
  174. */
  175. tsk->thread.cp0_badvaddr = address;
  176. info.si_signo = SIGBUS;
  177. info.si_errno = 0;
  178. info.si_code = BUS_ADRERR;
  179. info.si_addr = (void *) address;
  180. force_sig_info(SIGBUS, &info, tsk);
  181. return;
  182. vmalloc_fault:
  183. {
  184. /*
  185. * Synchronize this task's top level page-table
  186. * with the 'reference' page table.
  187. *
  188. * Do _not_ use "tsk" here. We might be inside
  189. * an interrupt in the middle of a task switch..
  190. */
  191. int offset = __pgd_offset(address);
  192. pgd_t *pgd, *pgd_k;
  193. pmd_t *pmd, *pmd_k;
  194. pte_t *pte_k;
  195. pgd = (pgd_t *) pgd_current[smp_processor_id()] + offset;
  196. pgd_k = init_mm.pgd + offset;
  197. if (!pgd_present(*pgd_k))
  198. goto no_context;
  199. set_pgd(pgd, *pgd_k);
  200. pmd = pmd_offset(pgd, address);
  201. pmd_k = pmd_offset(pgd_k, address);
  202. if (!pmd_present(*pmd_k))
  203. goto no_context;
  204. set_pmd(pmd, *pmd_k);
  205. pte_k = pte_offset_kernel(pmd_k, address);
  206. if (!pte_present(*pte_k))
  207. goto no_context;
  208. return;
  209. }
  210. }