fault.c 7.2 KB

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