2 * Dynamic Binary Instrumentation Module based on KProbes
3 * modules/kprobe/dbi_kprobes_deps.h
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * Copyright (C) Samsung Electronics, 2006-2010
21 * 2008-2009 Alexey Gerenkov <a.gerenkov@samsung.com> User-Space
22 * Probes initial implementation; Support x86/ARM/MIPS for both user and kernel spaces.
23 * 2010 Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for separating core and arch parts
27 #include <linux/module.h>
28 #include <linux/sched.h>
30 #include <asm/pgtable.h>
32 #include "dbi_kprobes_deps.h"
33 #include "dbi_kdebug.h"
36 #include <linux/slab.h>
39 unsigned long sched_addr;
40 unsigned long fork_addr;
42 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
43 static struct mm_struct* init_mm_ptr;
44 struct mm_struct init_mm;
48 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36)
49 static inline void *dbi_kmap_atomic(struct page *page)
51 return kmap_atomic(page);
53 static inline void dbi_kunmap_atomic(void *kvaddr)
55 kunmap_atomic(kvaddr);
57 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36) */
58 static inline void *dbi_kmap_atomic(struct page *page)
60 return kmap_atomic(page, KM_USER0);
63 static inline void dbi_kunmap_atomic(void *kvaddr)
65 kunmap_atomic(kvaddr, KM_USER0);
67 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36) */
70 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
71 DECLARE_MOD_FUNC_DEP(do_mmap_pgoff, unsigned long, struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, unsigned long pgoff);
72 DECLARE_MOD_DEP_WRAPPER(do_mmap_pgoff, unsigned long, struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, unsigned long pgoff)
73 IMP_MOD_DEP_WRAPPER(do_mmap_pgoff, file, addr, len, prot, flags, pgoff)
74 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0) */
76 /* copy_to_user_page */
77 #ifndef copy_to_user_page
78 static DECLARE_MOD_FUNC_DEP(copy_to_user_page, void, struct vm_area_struct *vma, struct page *page, unsigned long uaddr, void *dst, const void *src, unsigned long len);
79 DECLARE_MOD_DEP_WRAPPER(copy_to_user_page, void, struct vm_area_struct *vma, struct page *page, unsigned long uaddr, void *dst, const void *src, unsigned long len)
80 IMP_MOD_DEP_WRAPPER(copy_to_user_page, vma, page, uaddr, dst, src, len)
81 #endif /* copy_to_user_page */
84 static DECLARE_MOD_FUNC_DEP(access_process_vm, int, struct task_struct * tsk, unsigned long addr, void *buf, int len, int write);
86 static DECLARE_MOD_FUNC_DEP(find_extend_vma, struct vm_area_struct *, struct mm_struct * mm, unsigned long addr);
88 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30)
89 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
90 static DECLARE_MOD_FUNC_DEP(handle_mm_fault, int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, int write_access);
93 static DECLARE_MOD_FUNC_DEP(handle_mm_fault, int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags);
94 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30) */
96 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
97 static DECLARE_MOD_FUNC_DEP(get_gate_vma, struct vm_area_struct *, struct mm_struct *mm);
98 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
99 static DECLARE_MOD_FUNC_DEP(get_gate_vma, struct vm_area_struct *, struct task_struct *tsk);
100 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
102 #ifdef CONFIG_HUGETLB_PAGE
103 DECLARE_MOD_FUNC_DEP(follow_hugetlb_page, int, struct mm_struct *mm, \
104 struct vm_area_struct *vma, struct page **pages, \
105 struct vm_area_struct **vmas, unsigned long *position, int *length, \
109 #ifdef __HAVE_ARCH_GATE_AREA
110 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
111 DECLARE_MOD_FUNC_DEP(in_gate_area, int, struct mm_struct *mm, unsigned long addr);
112 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
113 DECLARE_MOD_FUNC_DEP(in_gate_area, int, struct task_struct *task, unsigned long addr);
114 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
115 #endif /* __HAVE_ARCH_GATE_AREA */
117 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
118 static DECLARE_MOD_FUNC_DEP(in_gate_area_no_mm, int, unsigned long addr);
119 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
120 static DECLARE_MOD_FUNC_DEP(in_gate_area_no_task, int, unsigned long addr);
121 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
124 static DECLARE_MOD_FUNC_DEP(follow_page, \
125 struct page *, struct vm_area_struct * vma, \
126 unsigned long address, unsigned int foll_flags);
127 static DECLARE_MOD_FUNC_DEP(__flush_anon_page, \
128 void, struct vm_area_struct *vma, struct page *page, \
129 unsigned long vmaddr);
130 static DECLARE_MOD_FUNC_DEP(vm_normal_page, \
131 struct page *, struct vm_area_struct *vma, \
132 unsigned long addr, pte_t pte);
133 static DECLARE_MOD_FUNC_DEP(flush_ptrace_access, \
134 void, struct vm_area_struct *vma, struct page *page, \
135 unsigned long uaddr, void *kaddr, unsigned long len, int write);
138 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 16))
139 static DECLARE_MOD_FUNC_DEP(put_task_struct, \
140 void, struct task_struct *tsk);
142 static DECLARE_MOD_FUNC_DEP(put_task_struct, \
143 void, struct rcu_head * rhp);
146 DECLARE_MOD_DEP_WRAPPER(access_process_vm, int, struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
147 IMP_MOD_DEP_WRAPPER (access_process_vm, tsk, addr, buf, len, write)
149 DECLARE_MOD_DEP_WRAPPER (find_extend_vma, struct vm_area_struct *, struct mm_struct * mm, unsigned long addr)
150 IMP_MOD_DEP_WRAPPER (find_extend_vma, mm, addr)
152 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30)
153 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
154 DECLARE_MOD_DEP_WRAPPER (handle_mm_fault, \
155 int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, int write_access)
156 IMP_MOD_DEP_WRAPPER (handle_mm_fault, mm, vma, address, write_access)
159 DECLARE_MOD_DEP_WRAPPER (handle_mm_fault, \
160 int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags)
161 IMP_MOD_DEP_WRAPPER (handle_mm_fault, mm, vma, address, flags)
164 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
165 DECLARE_MOD_DEP_WRAPPER (get_gate_vma, \
166 struct vm_area_struct *, struct mm_struct *mm)
167 IMP_MOD_DEP_WRAPPER (get_gate_vma, mm)
168 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
169 DECLARE_MOD_DEP_WRAPPER (get_gate_vma, \
170 struct vm_area_struct *, struct task_struct *tsk)
171 IMP_MOD_DEP_WRAPPER (get_gate_vma, tsk)
172 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
174 #ifdef CONFIG_HUGETLB_PAGE
175 DECLARE_MOD_DEP_WRAPPER (follow_hugetlb_page, int, struct mm_struct *mm, struct vm_area_struct *vma, struct page **pages, struct vm_area_struct **vmas, unsigned long *position, int *length, int i, unsigned int write)
176 IMP_MOD_DEP_WRAPPER (follow_hugetlb_page, mm, vma, pages, vmas, position, length, i, write)
179 static inline int dbi_in_gate_area(struct task_struct *task, unsigned long addr)
181 #ifdef __HAVE_ARCH_GATE_AREA
182 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
183 struct mm_struct *mm = task->mm;
184 IMP_MOD_DEP_WRAPPER (in_gate_area, mm, addr)
185 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
186 IMP_MOD_DEP_WRAPPER (in_gate_area, task, addr)
187 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
188 #else /*__HAVE_ARCH_GATE_AREA */
189 return in_gate_area(task, addr);
190 #endif/*__HAVE_ARCH_GATE_AREA */
194 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
195 DECLARE_MOD_DEP_WRAPPER(in_gate_area_no_mm, int, unsigned long addr)
196 IMP_MOD_DEP_WRAPPER(in_gate_area_no_mm, addr)
197 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
198 DECLARE_MOD_DEP_WRAPPER(in_gate_area_no_task, int, unsigned long addr)
199 IMP_MOD_DEP_WRAPPER(in_gate_area_no_task, addr)
200 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
202 static inline int dbi_in_gate_area_no_xxx(unsigned long addr)
204 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
205 return in_gate_area_no_mm(addr);
206 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
207 return in_gate_area_no_task(addr);
208 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
212 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 11))
213 DECLARE_MOD_DEP_WRAPPER (follow_page, \
214 struct page *, struct vm_area_struct * vma, \
215 unsigned long address, unsigned int foll_flags)
216 IMP_MOD_DEP_WRAPPER (follow_page, vma, address, foll_flags)
218 DECLARE_MOD_DEP_WRAPPER (__flush_anon_page, \
219 void, struct vm_area_struct *vma, \
220 struct page *page, unsigned long vmaddr)
221 IMP_MOD_DEP_WRAPPER (__flush_anon_page, vma, page, vmaddr)
223 DECLARE_MOD_DEP_WRAPPER(vm_normal_page, \
224 struct page *, struct vm_area_struct *vma, \
225 unsigned long addr, pte_t pte)
226 IMP_MOD_DEP_WRAPPER (vm_normal_page, vma, addr, pte)
228 DECLARE_MOD_DEP_WRAPPER(flush_ptrace_access, \
229 void, struct vm_area_struct *vma, struct page *page, \
230 unsigned long uaddr, void *kaddr, unsigned long len, int write)
231 IMP_MOD_DEP_WRAPPER(flush_ptrace_access, vma, page, uaddr, kaddr, len, write)
234 int init_module_dependencies(void)
237 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
238 init_mm_ptr = (struct mm_struct*)swap_ksyms("init_mm");
239 // memcmp(init_mm_ptr, &init_mm, sizeof(struct mm_struct));
242 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
243 INIT_MOD_DEP_VAR(handle_mm_fault, handle_mm_fault);
246 #ifndef copy_to_user_page
247 INIT_MOD_DEP_VAR(copy_to_user_page, copy_to_user_page);
248 #endif /* copy_to_user_page */
250 INIT_MOD_DEP_VAR(find_extend_vma, find_extend_vma);
251 INIT_MOD_DEP_VAR(get_gate_vma, get_gate_vma);
252 INIT_MOD_DEP_VAR(flush_ptrace_access, flush_ptrace_access);
254 #ifdef CONFIG_HUGETLB_PAGE
255 INIT_MOD_DEP_VAR(follow_hugetlb_page, follow_hugetlb_page);
258 #ifdef __HAVE_ARCH_GATE_AREA
259 INIT_MOD_DEP_VAR(in_gate_area, in_gate_area);
262 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38))
263 INIT_MOD_DEP_VAR(in_gate_area_no_mm, in_gate_area_no_mm);
264 #else /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)) */
265 INIT_MOD_DEP_VAR(in_gate_area_no_task, in_gate_area_no_task);
266 #endif /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)) */
268 INIT_MOD_DEP_VAR(follow_page, follow_page);
270 INIT_MOD_DEP_VAR(__flush_anon_page, __flush_anon_page);
271 INIT_MOD_DEP_VAR(vm_normal_page, vm_normal_page);
272 INIT_MOD_DEP_VAR(access_process_vm, access_process_vm);
274 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 16))
275 # if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11))
276 INIT_MOD_DEP_VAR(put_task_struct, put_task_struct);
278 INIT_MOD_DEP_VAR(put_task_struct, __put_task_struct);
281 INIT_MOD_DEP_VAR(put_task_struct, __put_task_struct_cb);
284 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
285 INIT_MOD_DEP_VAR(do_mmap_pgoff, do_mmap_pgoff);
291 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38) /* FIXME: must be < 32 */
292 #define GUP_FLAGS_WRITE 0x1
293 #define GUP_FLAGS_FORCE 0x2
294 #define GUP_FLAGS_IGNORE_VMA_PERMISSIONS 0x4
295 #define GUP_FLAGS_IGNORE_SIGKILL 0x8
296 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38) */
298 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
299 static inline int use_zero_page(struct vm_area_struct *vma)
302 * We don't want to optimize FOLL_ANON for make_pages_present()
303 * when it tries to page in a VM_LOCKED region. As to VM_SHARED,
304 * we want to get the page from the page tables to make sure
305 * that we serialize and update with any other user of that
308 if (vma->vm_flags & (VM_LOCKED | VM_SHARED))
311 * And if we have a fault routine, it's not an anonymous region.
313 return !vma->vm_ops || !vma->vm_ops->fault;
317 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
318 static unsigned long zero_pfn __read_mostly;
321 static inline int is_zero_pfn(unsigned long pfn)
323 return pfn == zero_pfn;
327 static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
329 return stack_guard_page_start(vma, addr) ||
330 stack_guard_page_end(vma, addr+PAGE_SIZE);
333 static int __get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
334 unsigned long start, int nr_pages, unsigned int gup_flags,
335 struct page **pages, struct vm_area_struct **vmas,
339 unsigned long vm_flags;
345 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
348 * Require read or write permissions.
349 * If FOLL_FORCE is set, we only require the "MAY" flags.
351 vm_flags = (gup_flags & FOLL_WRITE) ?
352 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
353 vm_flags &= (gup_flags & FOLL_FORCE) ?
354 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
358 struct vm_area_struct *vma;
360 vma = find_extend_vma(mm, start);
361 if (!vma && dbi_in_gate_area_no_xxx(start)) {
362 unsigned long pg = start & PAGE_MASK;
368 /* user gate pages are read-only */
369 if (gup_flags & FOLL_WRITE) {
370 return i ? : -EFAULT;
373 pgd = pgd_offset_k(pg);
375 pgd = pgd_offset_gate(mm, pg);
376 BUG_ON(pgd_none(*pgd));
377 pud = pud_offset(pgd, pg);
378 BUG_ON(pud_none(*pud));
379 pmd = pmd_offset(pud, pg);
380 if (pmd_none(*pmd)) {
381 return i ? : -EFAULT;
383 VM_BUG_ON(pmd_trans_huge(*pmd));
384 pte = pte_offset_map(pmd, pg);
385 if (pte_none(*pte)) {
387 return i ? : -EFAULT;
389 vma = get_gate_vma(mm);
393 page = vm_normal_page(vma, start, *pte);
395 if (!(gup_flags & FOLL_DUMP) &&
396 is_zero_pfn(pte_pfn(*pte)))
397 page = pte_page(*pte);
400 return i ? : -EFAULT;
411 (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
412 !(vm_flags & vma->vm_flags)) {
413 return i ? : -EFAULT;
416 if (is_vm_hugetlb_page(vma)) {
417 i = follow_hugetlb_page(mm, vma, pages, vmas,
418 &start, &nr_pages, i, gup_flags);
424 unsigned int foll_flags = gup_flags;
427 * If we have a pending SIGKILL, don't keep faulting
428 * pages and potentially allocating memory.
430 if (unlikely(fatal_signal_pending(current))) {
431 return i ? i : -ERESTARTSYS;
434 /* cond_resched(); */
435 while (!(page = follow_page(vma, start, foll_flags))) {
437 unsigned int fault_flags = 0;
439 /* For mlock, just skip the stack guard page. */
440 if (foll_flags & FOLL_MLOCK) {
441 if (stack_guard_page(vma, start))
444 if (foll_flags & FOLL_WRITE)
445 fault_flags |= FAULT_FLAG_WRITE;
447 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
448 if (foll_flags & FOLL_NOWAIT)
449 fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
451 ret = handle_mm_fault(mm, vma, start,
454 if (ret & VM_FAULT_ERROR) {
455 if (ret & VM_FAULT_OOM) {
456 return i ? i : -ENOMEM;
458 if (ret & (VM_FAULT_HWPOISON |
459 VM_FAULT_HWPOISON_LARGE)) {
463 else if (gup_flags & FOLL_HWPOISON) {
470 if (ret & VM_FAULT_SIGBUS) {
471 return i ? i : -EFAULT;
477 if (ret & VM_FAULT_MAJOR)
483 if (ret & VM_FAULT_RETRY) {
490 * The VM_FAULT_WRITE bit tells us that
491 * do_wp_page has broken COW when necessary,
492 * even if maybe_mkwrite decided not to set
493 * pte_write. We can thus safely do subsequent
494 * page lookups as if they were reads. But only
495 * do so when looping for pte_write is futile:
496 * in some cases userspace may also be wanting
497 * to write to the gotten user page, which a
498 * read fault here might prevent (a readonly
499 * page might get reCOWed by userspace write).
501 if ((ret & VM_FAULT_WRITE) &&
502 !(vma->vm_flags & VM_WRITE))
503 foll_flags &= ~FOLL_WRITE;
505 /* cond_resched(); */
508 return i ? i : PTR_ERR(page);
513 flush_anon_page(vma, page, start);
514 flush_dcache_page(page);
522 } while (nr_pages && start < vma->vm_end);
527 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
529 static int __get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
530 unsigned long start, int len, int flags,
531 struct page **pages, struct vm_area_struct **vmas)
534 unsigned int vm_flags = 0;
535 int write = !!(flags & GUP_FLAGS_WRITE);
536 int force = !!(flags & GUP_FLAGS_FORCE);
537 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
538 int ignore_sigkill = !!(flags & GUP_FLAGS_IGNORE_SIGKILL);
543 * Require read or write permissions.
544 * If 'force' is set, we only require the "MAY" flags.
546 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
547 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
551 struct vm_area_struct *vma;
552 unsigned int foll_flags;
554 vma = find_vma(mm, start);
555 if (!vma && dbi_in_gate_area(tsk, start)) {
556 unsigned long pg = start & PAGE_MASK;
557 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
563 /* user gate pages are read-only */
564 if (!ignore && write)
565 return i ? : -EFAULT;
567 pgd = pgd_offset_k(pg);
569 pgd = pgd_offset_gate(mm, pg);
570 BUG_ON(pgd_none(*pgd));
571 pud = pud_offset(pgd, pg);
572 BUG_ON(pud_none(*pud));
573 pmd = pmd_offset(pud, pg);
575 return i ? : -EFAULT;
576 pte = pte_offset_map(pmd, pg);
577 if (pte_none(*pte)) {
579 return i ? : -EFAULT;
582 struct page *page = vm_normal_page(gate_vma, start, *pte);
597 (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
598 (!ignore && !(vm_flags & vma->vm_flags)))
599 return i ? : -EFAULT;
601 if (is_vm_hugetlb_page(vma)) {
602 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
603 i = follow_hugetlb_page(mm, vma, pages, vmas,
606 i = follow_hugetlb_page(mm, vma, pages, vmas,
607 &start, &len, i, write);
612 foll_flags = FOLL_TOUCH;
614 foll_flags |= FOLL_GET;
616 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
617 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,30)
618 if (!write && use_zero_page(vma))
619 foll_flags |= FOLL_ANON;
628 * If we have a pending SIGKILL, don't keep faulting
629 * pages and potentially allocating memory, unless
630 * current is handling munlock--e.g., on exit. In
631 * that case, we are not allocating memory. Rather,
632 * we're only unlocking already resident/mapped pages.
634 if (unlikely(!ignore_sigkill &&
635 fatal_signal_pending(current)))
636 return i ? i : -ERESTARTSYS;
640 foll_flags |= FOLL_WRITE;
645 DBPRINTF ("pages = %p vma = %p\n", pages, vma);
646 while (!(page = follow_page(vma, start, foll_flags))) {
648 ret = handle_mm_fault(mm, vma, start,
649 foll_flags & FOLL_WRITE);
651 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
652 if (ret & VM_FAULT_WRITE)
653 foll_flags &= ~FOLL_WRITE;
655 switch (ret & ~VM_FAULT_WRITE) {
662 case VM_FAULT_SIGBUS:
663 return i ? i : -EFAULT;
665 return i ? i : -ENOMEM;
671 if (ret & VM_FAULT_ERROR) {
672 if (ret & VM_FAULT_OOM)
673 return i ? i : -ENOMEM;
674 else if (ret & VM_FAULT_SIGBUS)
675 return i ? i : -EFAULT;
678 if (ret & VM_FAULT_MAJOR)
684 * The VM_FAULT_WRITE bit tells us that
685 * do_wp_page has broken COW when necessary,
686 * even if maybe_mkwrite decided not to set
687 * pte_write. We can thus safely do subsequent
688 * page lookups as if they were reads. But only
689 * do so when looping for pte_write is futile:
690 * in some cases userspace may also be wanting
691 * to write to the gotten user page, which a
692 * read fault here might prevent (a readonly
693 * page might get reCOWed by userspace write).
695 if ((ret & VM_FAULT_WRITE) &&
696 !(vma->vm_flags & VM_WRITE))
697 foll_flags &= ~FOLL_WRITE;
705 return i ? i : PTR_ERR(page);
709 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
710 flush_anon_page(page, start);
712 flush_anon_page(vma, page, start);
714 flush_dcache_page(page);
721 } while (len && start < vma->vm_end);
726 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
728 int get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
729 unsigned long start, int len, int write, int force,
730 struct page **pages, struct vm_area_struct **vmas)
732 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
733 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) /* FIXME: must be >= 32! */
734 int flags = FOLL_TOUCH;
742 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
746 flags |= GUP_FLAGS_WRITE;
748 flags |= GUP_FLAGS_FORCE;
749 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
751 return __get_user_pages_uprobe(tsk, mm,
753 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
755 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
757 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
759 return get_user_pages(tsk, mm, start, len, write, force, pages, vmas);
763 #define ACCESS_PROCESS_OPTIMIZATION 0
765 #if ACCESS_PROCESS_OPTIMIZATION
767 #define GET_STEP_X(LEN, STEP) (((LEN) >= (STEP)) ? (STEP) : (LEN) % (STEP))
768 #define GET_STEP_4(LEN) GET_STEP_X((LEN), 4)
770 static void read_data_current(unsigned long addr, void *buf, int len)
775 for (step = GET_STEP_4(len); len; len -= step) {
776 switch (GET_STEP_4(len)) {
778 get_user(*(u8 *)(buf + pos), (unsigned long *)(addr + pos));
784 get_user(*(u16 *)(buf + pos), (unsigned long *)(addr + pos));
789 get_user(*(u32 *)(buf + pos), (unsigned long *)(addr + pos));
799 static void write_data_current(unsigned long addr, void *buf, int len)
804 for (step = GET_STEP_4(len); len; len -= step) {
805 switch (GET_STEP_4(len)) {
807 put_user(*(u8 *)(buf + pos), (unsigned long *)(addr + pos));
813 put_user(*(u16 *)(buf + pos), (unsigned long *)(addr + pos));
818 put_user(*(u32 *)(buf + pos), (unsigned long *)(addr + pos));
828 int access_process_vm_atomic(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
830 struct mm_struct *mm;
831 struct vm_area_struct *vma;
838 #if ACCESS_PROCESS_OPTIMIZATION
839 if (write == 0 && tsk == current) {
840 read_data_current(addr, buf, len);
845 mm = tsk->mm; /* function 'get_task_mm' is to be called */
849 /* ignore errors, just check how much was successfully transferred */
851 int bytes, ret, offset;
853 struct page *page = NULL;
855 ret = get_user_pages_uprobe(tsk, mm, addr, 1,
856 write, 1, &page, &vma);
860 * Check if this is a VM_IO | VM_PFNMAP VMA, which
861 * we can access using slightly different code.
863 #ifdef CONFIG_HAVE_IOREMAP_PROT
864 vma = find_vma(mm, addr);
867 if (vma->vm_ops && vma->vm_ops->access)
868 ret = vma->vm_ops->access(vma, addr, buf,
876 offset = addr & (PAGE_SIZE-1);
877 if (bytes > PAGE_SIZE-offset)
878 bytes = PAGE_SIZE-offset;
880 maddr = dbi_kmap_atomic(page);
883 copy_to_user_page(vma, page, addr,
884 maddr + offset, buf, bytes);
885 set_page_dirty_lock(page);
887 copy_from_user_page(vma, page, addr,
888 buf, maddr + offset, bytes);
891 dbi_kunmap_atomic(maddr);
892 page_cache_release(page);
899 return buf - old_buf;
902 int page_present (struct mm_struct *mm, unsigned long address)
910 pgd = pgd_offset(mm, address);
911 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
914 pud = pud_offset(pgd, address);
915 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
918 pmd = pmd_offset(pud, address);
919 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
922 ptep = pte_offset_map(pmd, address);
928 if (pte_present(pte)) {
930 if (pfn_valid(pfn)) {
940 EXPORT_SYMBOL_GPL (page_present);
941 EXPORT_SYMBOL_GPL (get_user_pages_uprobe);
942 EXPORT_SYMBOL_GPL (access_process_vm_atomic);