fault.c 7.5 KB

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