fault.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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/vt_kern.h> /* For unblank_screen() */
  20. #include <linux/module.h>
  21. #include <asm/branch.h>
  22. #include <asm/mmu_context.h>
  23. #include <asm/system.h>
  24. #include <asm/uaccess.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/highmem.h> /* For VMALLOC_END */
  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. int fault;
  41. #if 0
  42. printk("Cpu%d[%s:%d:%0*lx:%ld:%0*lx]\n", raw_smp_processor_id(),
  43. current->comm, current->pid, field, address, write,
  44. field, regs->cp0_epc);
  45. #endif
  46. info.si_code = SEGV_MAPERR;
  47. /*
  48. * We fault-in kernel-space virtual memory on-demand. The
  49. * 'reference' page table is init_mm.pgd.
  50. *
  51. * NOTE! We MUST NOT take any locks for this case. We may
  52. * be in an interrupt or a critical region, and should
  53. * only copy the information from the master page table,
  54. * nothing more.
  55. */
  56. if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
  57. goto vmalloc_fault;
  58. #ifdef MODULE_START
  59. if (unlikely(address >= MODULE_START && address < MODULE_END))
  60. goto vmalloc_fault;
  61. #endif
  62. /*
  63. * If we're in an interrupt or have no user
  64. * context, we must not take the fault..
  65. */
  66. if (in_atomic() || !mm)
  67. goto bad_area_nosemaphore;
  68. down_read(&mm->mmap_sem);
  69. vma = find_vma(mm, address);
  70. if (!vma)
  71. goto bad_area;
  72. if (vma->vm_start <= address)
  73. goto good_area;
  74. if (!(vma->vm_flags & VM_GROWSDOWN))
  75. goto bad_area;
  76. if (expand_stack(vma, address))
  77. goto bad_area;
  78. /*
  79. * Ok, we have a good vm_area for this memory access, so
  80. * we can handle it..
  81. */
  82. good_area:
  83. info.si_code = SEGV_ACCERR;
  84. if (write) {
  85. if (!(vma->vm_flags & VM_WRITE))
  86. goto bad_area;
  87. } else {
  88. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  89. goto bad_area;
  90. }
  91. survive:
  92. /*
  93. * If for any reason at all we couldn't handle the fault,
  94. * make sure we exit gracefully rather than endlessly redo
  95. * the fault.
  96. */
  97. fault = handle_mm_fault(mm, vma, address, write);
  98. if (unlikely(fault & VM_FAULT_ERROR)) {
  99. if (fault & VM_FAULT_OOM)
  100. goto out_of_memory;
  101. else if (fault & VM_FAULT_SIGBUS)
  102. goto do_sigbus;
  103. BUG();
  104. }
  105. if (fault & VM_FAULT_MAJOR)
  106. tsk->maj_flt++;
  107. else
  108. tsk->min_flt++;
  109. up_read(&mm->mmap_sem);
  110. return;
  111. /*
  112. * Something tried to access memory that isn't in our memory map..
  113. * Fix it, but check if it's kernel or user first..
  114. */
  115. bad_area:
  116. up_read(&mm->mmap_sem);
  117. bad_area_nosemaphore:
  118. /* User mode accesses just cause a SIGSEGV */
  119. if (user_mode(regs)) {
  120. tsk->thread.cp0_badvaddr = address;
  121. tsk->thread.error_code = write;
  122. #if 0
  123. printk("do_page_fault() #2: sending SIGSEGV to %s for "
  124. "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
  125. tsk->comm,
  126. write ? "write access to" : "read access from",
  127. field, address,
  128. field, (unsigned long) regs->cp0_epc,
  129. field, (unsigned long) regs->regs[31]);
  130. #endif
  131. info.si_signo = SIGSEGV;
  132. info.si_errno = 0;
  133. /* info.si_code has been set above */
  134. info.si_addr = (void __user *) address;
  135. force_sig_info(SIGSEGV, &info, tsk);
  136. return;
  137. }
  138. no_context:
  139. /* Are we prepared to handle this kernel fault? */
  140. if (fixup_exception(regs)) {
  141. current->thread.cp0_baduaddr = address;
  142. return;
  143. }
  144. /*
  145. * Oops. The kernel tried to access some bad page. We'll have to
  146. * terminate things with extreme prejudice.
  147. */
  148. bust_spinlocks(1);
  149. printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
  150. "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
  151. raw_smp_processor_id(), field, address, field, regs->cp0_epc,
  152. field, regs->regs[31]);
  153. die("Oops", regs);
  154. /*
  155. * We ran out of memory, or some other thing happened to us that made
  156. * us unable to handle the page fault gracefully.
  157. */
  158. out_of_memory:
  159. up_read(&mm->mmap_sem);
  160. if (is_global_init(tsk)) {
  161. yield();
  162. down_read(&mm->mmap_sem);
  163. goto survive;
  164. }
  165. printk("VM: killing process %s\n", tsk->comm);
  166. if (user_mode(regs))
  167. do_group_exit(SIGKILL);
  168. goto no_context;
  169. do_sigbus:
  170. up_read(&mm->mmap_sem);
  171. /* Kernel mode? Handle exceptions or die */
  172. if (!user_mode(regs))
  173. goto no_context;
  174. else
  175. /*
  176. * Send a sigbus, regardless of whether we were in kernel
  177. * or user mode.
  178. */
  179. #if 0
  180. printk("do_page_fault() #3: sending SIGBUS to %s for "
  181. "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
  182. tsk->comm,
  183. write ? "write access to" : "read access from",
  184. field, address,
  185. field, (unsigned long) regs->cp0_epc,
  186. field, (unsigned long) regs->regs[31]);
  187. #endif
  188. tsk->thread.cp0_badvaddr = address;
  189. info.si_signo = SIGBUS;
  190. info.si_errno = 0;
  191. info.si_code = BUS_ADRERR;
  192. info.si_addr = (void __user *) address;
  193. force_sig_info(SIGBUS, &info, tsk);
  194. return;
  195. vmalloc_fault:
  196. {
  197. /*
  198. * Synchronize this task's top level page-table
  199. * with the 'reference' page table.
  200. *
  201. * Do _not_ use "tsk" here. We might be inside
  202. * an interrupt in the middle of a task switch..
  203. */
  204. int offset = __pgd_offset(address);
  205. pgd_t *pgd, *pgd_k;
  206. pud_t *pud, *pud_k;
  207. pmd_t *pmd, *pmd_k;
  208. pte_t *pte_k;
  209. pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
  210. pgd_k = init_mm.pgd + offset;
  211. if (!pgd_present(*pgd_k))
  212. goto no_context;
  213. set_pgd(pgd, *pgd_k);
  214. pud = pud_offset(pgd, address);
  215. pud_k = pud_offset(pgd_k, address);
  216. if (!pud_present(*pud_k))
  217. goto no_context;
  218. pmd = pmd_offset(pud, address);
  219. pmd_k = pmd_offset(pud_k, address);
  220. if (!pmd_present(*pmd_k))
  221. goto no_context;
  222. set_pmd(pmd, *pmd_k);
  223. pte_k = pte_offset_kernel(pmd_k, address);
  224. if (!pte_present(*pte_k))
  225. goto no_context;
  226. return;
  227. }
  228. }