fault.c 7.9 KB

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