fault.c 7.9 KB

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