fault.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. #ifdef CONFIG_64BIT
  57. # define VMALLOC_FAULT_TARGET no_context
  58. #else
  59. # define VMALLOC_FAULT_TARGET vmalloc_fault
  60. #endif
  61. if (unlikely(address >= VMALLOC_START && address <= VMALLOC_END))
  62. goto VMALLOC_FAULT_TARGET;
  63. #ifdef MODULE_START
  64. if (unlikely(address >= MODULE_START && address < MODULE_END))
  65. goto VMALLOC_FAULT_TARGET;
  66. #endif
  67. /*
  68. * If we're in an interrupt or have no user
  69. * context, we must not take the fault..
  70. */
  71. if (in_atomic() || !mm)
  72. goto bad_area_nosemaphore;
  73. down_read(&mm->mmap_sem);
  74. vma = find_vma(mm, address);
  75. if (!vma)
  76. goto bad_area;
  77. if (vma->vm_start <= address)
  78. goto good_area;
  79. if (!(vma->vm_flags & VM_GROWSDOWN))
  80. goto bad_area;
  81. if (expand_stack(vma, address))
  82. goto bad_area;
  83. /*
  84. * Ok, we have a good vm_area for this memory access, so
  85. * we can handle it..
  86. */
  87. good_area:
  88. info.si_code = SEGV_ACCERR;
  89. if (write) {
  90. if (!(vma->vm_flags & VM_WRITE))
  91. goto bad_area;
  92. } else {
  93. if (kernel_uses_smartmips_rixi) {
  94. if (address == regs->cp0_epc && !(vma->vm_flags & VM_EXEC)) {
  95. #if 0
  96. pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] XI violation\n",
  97. raw_smp_processor_id(),
  98. current->comm, current->pid,
  99. field, address, write,
  100. field, regs->cp0_epc);
  101. #endif
  102. goto bad_area;
  103. }
  104. if (!(vma->vm_flags & VM_READ)) {
  105. #if 0
  106. pr_notice("Cpu%d[%s:%d:%0*lx:%ld:%0*lx] RI violation\n",
  107. raw_smp_processor_id(),
  108. current->comm, current->pid,
  109. field, address, write,
  110. field, regs->cp0_epc);
  111. #endif
  112. goto bad_area;
  113. }
  114. } else {
  115. if (!(vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)))
  116. goto bad_area;
  117. }
  118. }
  119. /*
  120. * If for any reason at all we couldn't handle the fault,
  121. * make sure we exit gracefully rather than endlessly redo
  122. * the fault.
  123. */
  124. fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
  125. if (unlikely(fault & VM_FAULT_ERROR)) {
  126. if (fault & VM_FAULT_OOM)
  127. goto out_of_memory;
  128. else if (fault & VM_FAULT_SIGBUS)
  129. goto do_sigbus;
  130. BUG();
  131. }
  132. if (fault & VM_FAULT_MAJOR)
  133. tsk->maj_flt++;
  134. else
  135. tsk->min_flt++;
  136. up_read(&mm->mmap_sem);
  137. return;
  138. /*
  139. * Something tried to access memory that isn't in our memory map..
  140. * Fix it, but check if it's kernel or user first..
  141. */
  142. bad_area:
  143. up_read(&mm->mmap_sem);
  144. bad_area_nosemaphore:
  145. /* User mode accesses just cause a SIGSEGV */
  146. if (user_mode(regs)) {
  147. tsk->thread.cp0_badvaddr = address;
  148. tsk->thread.error_code = write;
  149. #if 0
  150. printk("do_page_fault() #2: sending SIGSEGV to %s for "
  151. "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
  152. tsk->comm,
  153. write ? "write access to" : "read access from",
  154. field, address,
  155. field, (unsigned long) regs->cp0_epc,
  156. field, (unsigned long) regs->regs[31]);
  157. #endif
  158. info.si_signo = SIGSEGV;
  159. info.si_errno = 0;
  160. /* info.si_code has been set above */
  161. info.si_addr = (void __user *) address;
  162. force_sig_info(SIGSEGV, &info, tsk);
  163. return;
  164. }
  165. no_context:
  166. /* Are we prepared to handle this kernel fault? */
  167. if (fixup_exception(regs)) {
  168. current->thread.cp0_baduaddr = address;
  169. return;
  170. }
  171. /*
  172. * Oops. The kernel tried to access some bad page. We'll have to
  173. * terminate things with extreme prejudice.
  174. */
  175. bust_spinlocks(1);
  176. printk(KERN_ALERT "CPU %d Unable to handle kernel paging request at "
  177. "virtual address %0*lx, epc == %0*lx, ra == %0*lx\n",
  178. raw_smp_processor_id(), field, address, field, regs->cp0_epc,
  179. field, regs->regs[31]);
  180. die("Oops", regs);
  181. out_of_memory:
  182. /*
  183. * We ran out of memory, call the OOM killer, and return the userspace
  184. * (which will retry the fault, or kill us if we got oom-killed).
  185. */
  186. up_read(&mm->mmap_sem);
  187. pagefault_out_of_memory();
  188. return;
  189. do_sigbus:
  190. up_read(&mm->mmap_sem);
  191. /* Kernel mode? Handle exceptions or die */
  192. if (!user_mode(regs))
  193. goto no_context;
  194. else
  195. /*
  196. * Send a sigbus, regardless of whether we were in kernel
  197. * or user mode.
  198. */
  199. #if 0
  200. printk("do_page_fault() #3: sending SIGBUS to %s for "
  201. "invalid %s\n%0*lx (epc == %0*lx, ra == %0*lx)\n",
  202. tsk->comm,
  203. write ? "write access to" : "read access from",
  204. field, address,
  205. field, (unsigned long) regs->cp0_epc,
  206. field, (unsigned long) regs->regs[31]);
  207. #endif
  208. tsk->thread.cp0_badvaddr = address;
  209. info.si_signo = SIGBUS;
  210. info.si_errno = 0;
  211. info.si_code = BUS_ADRERR;
  212. info.si_addr = (void __user *) address;
  213. force_sig_info(SIGBUS, &info, tsk);
  214. return;
  215. #ifndef CONFIG_64BIT
  216. vmalloc_fault:
  217. {
  218. /*
  219. * Synchronize this task's top level page-table
  220. * with the 'reference' page table.
  221. *
  222. * Do _not_ use "tsk" here. We might be inside
  223. * an interrupt in the middle of a task switch..
  224. */
  225. int offset = __pgd_offset(address);
  226. pgd_t *pgd, *pgd_k;
  227. pud_t *pud, *pud_k;
  228. pmd_t *pmd, *pmd_k;
  229. pte_t *pte_k;
  230. pgd = (pgd_t *) pgd_current[raw_smp_processor_id()] + offset;
  231. pgd_k = init_mm.pgd + offset;
  232. if (!pgd_present(*pgd_k))
  233. goto no_context;
  234. set_pgd(pgd, *pgd_k);
  235. pud = pud_offset(pgd, address);
  236. pud_k = pud_offset(pgd_k, address);
  237. if (!pud_present(*pud_k))
  238. goto no_context;
  239. pmd = pmd_offset(pud, address);
  240. pmd_k = pmd_offset(pud_k, address);
  241. if (!pmd_present(*pmd_k))
  242. goto no_context;
  243. set_pmd(pmd, *pmd_k);
  244. pte_k = pte_offset_kernel(pmd_k, address);
  245. if (!pte_present(*pte_k))
  246. goto no_context;
  247. return;
  248. }
  249. #endif
  250. }