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;
41 unsigned long exit_addr;
43 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
44 static struct mm_struct* init_mm_ptr;
45 struct mm_struct init_mm;
49 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36)
50 static inline void *dbi_kmap_atomic(struct page *page)
52 return kmap_atomic(page);
54 static inline void dbi_kunmap_atomic(void *kvaddr)
56 kunmap_atomic(kvaddr);
58 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36) */
59 static inline void *dbi_kmap_atomic(struct page *page)
61 return kmap_atomic(page, KM_USER0);
64 static inline void dbi_kunmap_atomic(void *kvaddr)
66 kunmap_atomic(kvaddr, KM_USER0);
68 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 36) */
71 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0)
72 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);
73 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)
74 IMP_MOD_DEP_WRAPPER(do_mmap_pgoff, file, addr, len, prot, flags, pgoff)
75 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0) */
77 /* copy_to_user_page */
78 #ifndef copy_to_user_page
79 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);
80 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)
81 IMP_MOD_DEP_WRAPPER(copy_to_user_page, vma, page, uaddr, dst, src, len)
82 #endif /* copy_to_user_page */
85 static DECLARE_MOD_FUNC_DEP(access_process_vm, int, struct task_struct * tsk, unsigned long addr, void *buf, int len, int write);
87 static DECLARE_MOD_FUNC_DEP(find_extend_vma, struct vm_area_struct *, struct mm_struct * mm, unsigned long addr);
89 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30)
90 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
91 static DECLARE_MOD_FUNC_DEP(handle_mm_fault, int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, int write_access);
94 static DECLARE_MOD_FUNC_DEP(handle_mm_fault, int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags);
95 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30) */
97 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
98 static DECLARE_MOD_FUNC_DEP(get_gate_vma, struct vm_area_struct *, struct mm_struct *mm);
99 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
100 static DECLARE_MOD_FUNC_DEP(get_gate_vma, struct vm_area_struct *, struct task_struct *tsk);
101 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
103 #ifdef CONFIG_HUGETLB_PAGE
104 DECLARE_MOD_FUNC_DEP(follow_hugetlb_page, int, struct mm_struct *mm, \
105 struct vm_area_struct *vma, struct page **pages, \
106 struct vm_area_struct **vmas, unsigned long *position, int *length, \
110 #ifdef __HAVE_ARCH_GATE_AREA
111 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
112 DECLARE_MOD_FUNC_DEP(in_gate_area, int, struct mm_struct *mm, unsigned long addr);
113 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
114 DECLARE_MOD_FUNC_DEP(in_gate_area, int, struct task_struct *task, unsigned long addr);
115 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
116 #endif /* __HAVE_ARCH_GATE_AREA */
118 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
119 static DECLARE_MOD_FUNC_DEP(in_gate_area_no_mm, int, unsigned long addr);
120 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
121 static DECLARE_MOD_FUNC_DEP(in_gate_area_no_task, int, unsigned long addr);
122 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
125 static DECLARE_MOD_FUNC_DEP(follow_page, \
126 struct page *, struct vm_area_struct * vma, \
127 unsigned long address, unsigned int foll_flags);
128 static DECLARE_MOD_FUNC_DEP(__flush_anon_page, \
129 void, struct vm_area_struct *vma, struct page *page, \
130 unsigned long vmaddr);
131 static DECLARE_MOD_FUNC_DEP(vm_normal_page, \
132 struct page *, struct vm_area_struct *vma, \
133 unsigned long addr, pte_t pte);
134 static DECLARE_MOD_FUNC_DEP(flush_ptrace_access, \
135 void, struct vm_area_struct *vma, struct page *page, \
136 unsigned long uaddr, void *kaddr, unsigned long len, int write);
139 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 16))
140 static DECLARE_MOD_FUNC_DEP(put_task_struct, \
141 void, struct task_struct *tsk);
143 static DECLARE_MOD_FUNC_DEP(put_task_struct, \
144 void, struct rcu_head * rhp);
147 DECLARE_MOD_DEP_WRAPPER(access_process_vm, int, struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
148 IMP_MOD_DEP_WRAPPER (access_process_vm, tsk, addr, buf, len, write)
150 DECLARE_MOD_DEP_WRAPPER (find_extend_vma, struct vm_area_struct *, struct mm_struct * mm, unsigned long addr)
151 IMP_MOD_DEP_WRAPPER (find_extend_vma, mm, addr)
153 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30)
154 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
155 DECLARE_MOD_DEP_WRAPPER (handle_mm_fault, \
156 int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, int write_access)
157 IMP_MOD_DEP_WRAPPER (handle_mm_fault, mm, vma, address, write_access)
160 DECLARE_MOD_DEP_WRAPPER (handle_mm_fault, \
161 int, struct mm_struct *mm, struct vm_area_struct *vma, unsigned long address, unsigned int flags)
162 IMP_MOD_DEP_WRAPPER (handle_mm_fault, mm, vma, address, flags)
165 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
166 DECLARE_MOD_DEP_WRAPPER (get_gate_vma, \
167 struct vm_area_struct *, struct mm_struct *mm)
168 IMP_MOD_DEP_WRAPPER (get_gate_vma, mm)
169 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
170 DECLARE_MOD_DEP_WRAPPER (get_gate_vma, \
171 struct vm_area_struct *, struct task_struct *tsk)
172 IMP_MOD_DEP_WRAPPER (get_gate_vma, tsk)
173 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
175 #ifdef CONFIG_HUGETLB_PAGE
176 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)
177 IMP_MOD_DEP_WRAPPER (follow_hugetlb_page, mm, vma, pages, vmas, position, length, i, write)
180 static inline int dbi_in_gate_area(struct task_struct *task, unsigned long addr)
182 #ifdef __HAVE_ARCH_GATE_AREA
183 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
184 struct mm_struct *mm = task->mm;
185 IMP_MOD_DEP_WRAPPER (in_gate_area, mm, addr)
186 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
187 IMP_MOD_DEP_WRAPPER (in_gate_area, task, addr)
188 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
189 #else /*__HAVE_ARCH_GATE_AREA */
190 return in_gate_area(task, addr);
191 #endif/*__HAVE_ARCH_GATE_AREA */
195 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
196 DECLARE_MOD_DEP_WRAPPER(in_gate_area_no_mm, int, unsigned long addr)
197 IMP_MOD_DEP_WRAPPER(in_gate_area_no_mm, addr)
198 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
199 DECLARE_MOD_DEP_WRAPPER(in_gate_area_no_task, int, unsigned long addr)
200 IMP_MOD_DEP_WRAPPER(in_gate_area_no_task, addr)
201 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
203 static inline int dbi_in_gate_area_no_xxx(unsigned long addr)
205 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
206 return in_gate_area_no_mm(addr);
207 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
208 return in_gate_area_no_task(addr);
209 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
213 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 11))
214 DECLARE_MOD_DEP_WRAPPER (follow_page, \
215 struct page *, struct vm_area_struct * vma, \
216 unsigned long address, unsigned int foll_flags)
217 IMP_MOD_DEP_WRAPPER (follow_page, vma, address, foll_flags)
219 DECLARE_MOD_DEP_WRAPPER (__flush_anon_page, \
220 void, struct vm_area_struct *vma, \
221 struct page *page, unsigned long vmaddr)
222 IMP_MOD_DEP_WRAPPER (__flush_anon_page, vma, page, vmaddr)
224 DECLARE_MOD_DEP_WRAPPER(vm_normal_page, \
225 struct page *, struct vm_area_struct *vma, \
226 unsigned long addr, pte_t pte)
227 IMP_MOD_DEP_WRAPPER (vm_normal_page, vma, addr, pte)
229 DECLARE_MOD_DEP_WRAPPER(flush_ptrace_access, \
230 void, struct vm_area_struct *vma, struct page *page, \
231 unsigned long uaddr, void *kaddr, unsigned long len, int write)
232 IMP_MOD_DEP_WRAPPER(flush_ptrace_access, vma, page, uaddr, kaddr, len, write)
235 int init_module_dependencies(void)
238 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 29)
239 init_mm_ptr = (struct mm_struct*)swap_ksyms("init_mm");
240 // memcmp(init_mm_ptr, &init_mm, sizeof(struct mm_struct));
243 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 18)
244 INIT_MOD_DEP_VAR(handle_mm_fault, handle_mm_fault);
247 #ifndef copy_to_user_page
248 INIT_MOD_DEP_VAR(copy_to_user_page, copy_to_user_page);
249 #endif /* copy_to_user_page */
251 INIT_MOD_DEP_VAR(find_extend_vma, find_extend_vma);
252 INIT_MOD_DEP_VAR(get_gate_vma, get_gate_vma);
253 INIT_MOD_DEP_VAR(flush_ptrace_access, flush_ptrace_access);
255 #ifdef CONFIG_HUGETLB_PAGE
256 INIT_MOD_DEP_VAR(follow_hugetlb_page, follow_hugetlb_page);
259 #ifdef __HAVE_ARCH_GATE_AREA
260 INIT_MOD_DEP_VAR(in_gate_area, in_gate_area);
263 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38))
264 INIT_MOD_DEP_VAR(in_gate_area_no_mm, in_gate_area_no_mm);
265 #else /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)) */
266 INIT_MOD_DEP_VAR(in_gate_area_no_task, in_gate_area_no_task);
267 #endif /* (LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)) */
269 INIT_MOD_DEP_VAR(follow_page, follow_page);
271 INIT_MOD_DEP_VAR(__flush_anon_page, __flush_anon_page);
272 INIT_MOD_DEP_VAR(vm_normal_page, vm_normal_page);
273 INIT_MOD_DEP_VAR(access_process_vm, access_process_vm);
275 #if (LINUX_VERSION_CODE != KERNEL_VERSION(2, 6, 16))
276 # if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 11))
277 INIT_MOD_DEP_VAR(put_task_struct, put_task_struct);
279 INIT_MOD_DEP_VAR(put_task_struct, __put_task_struct);
282 INIT_MOD_DEP_VAR(put_task_struct, __put_task_struct_cb);
285 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 4, 0))
286 INIT_MOD_DEP_VAR(do_mmap_pgoff, do_mmap_pgoff);
292 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38) /* FIXME: must be < 32 */
293 #define GUP_FLAGS_WRITE 0x1
294 #define GUP_FLAGS_FORCE 0x2
295 #define GUP_FLAGS_IGNORE_VMA_PERMISSIONS 0x4
296 #define GUP_FLAGS_IGNORE_SIGKILL 0x8
297 #endif /* LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38) */
299 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
300 static inline int use_zero_page(struct vm_area_struct *vma)
303 * We don't want to optimize FOLL_ANON for make_pages_present()
304 * when it tries to page in a VM_LOCKED region. As to VM_SHARED,
305 * we want to get the page from the page tables to make sure
306 * that we serialize and update with any other user of that
309 if (vma->vm_flags & (VM_LOCKED | VM_SHARED))
312 * And if we have a fault routine, it's not an anonymous region.
314 return !vma->vm_ops || !vma->vm_ops->fault;
318 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
319 static unsigned long zero_pfn __read_mostly;
322 static inline int is_zero_pfn(unsigned long pfn)
324 return pfn == zero_pfn;
328 static inline int stack_guard_page(struct vm_area_struct *vma, unsigned long addr)
330 return stack_guard_page_start(vma, addr) ||
331 stack_guard_page_end(vma, addr+PAGE_SIZE);
334 static int __get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
335 unsigned long start, int nr_pages, unsigned int gup_flags,
336 struct page **pages, struct vm_area_struct **vmas,
340 unsigned long vm_flags;
346 VM_BUG_ON(!!pages != !!(gup_flags & FOLL_GET));
349 * Require read or write permissions.
350 * If FOLL_FORCE is set, we only require the "MAY" flags.
352 vm_flags = (gup_flags & FOLL_WRITE) ?
353 (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
354 vm_flags &= (gup_flags & FOLL_FORCE) ?
355 (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
359 struct vm_area_struct *vma;
361 vma = find_extend_vma(mm, start);
362 if (!vma && dbi_in_gate_area_no_xxx(start)) {
363 unsigned long pg = start & PAGE_MASK;
369 /* user gate pages are read-only */
370 if (gup_flags & FOLL_WRITE) {
371 return i ? : -EFAULT;
374 pgd = pgd_offset_k(pg);
376 pgd = pgd_offset_gate(mm, pg);
377 BUG_ON(pgd_none(*pgd));
378 pud = pud_offset(pgd, pg);
379 BUG_ON(pud_none(*pud));
380 pmd = pmd_offset(pud, pg);
381 if (pmd_none(*pmd)) {
382 return i ? : -EFAULT;
384 VM_BUG_ON(pmd_trans_huge(*pmd));
385 pte = pte_offset_map(pmd, pg);
386 if (pte_none(*pte)) {
388 return i ? : -EFAULT;
390 vma = get_gate_vma(mm);
394 page = vm_normal_page(vma, start, *pte);
396 if (!(gup_flags & FOLL_DUMP) &&
397 is_zero_pfn(pte_pfn(*pte)))
398 page = pte_page(*pte);
401 return i ? : -EFAULT;
412 (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
413 !(vm_flags & vma->vm_flags)) {
414 return i ? : -EFAULT;
417 if (is_vm_hugetlb_page(vma)) {
418 i = follow_hugetlb_page(mm, vma, pages, vmas,
419 &start, &nr_pages, i, gup_flags);
425 unsigned int foll_flags = gup_flags;
428 * If we have a pending SIGKILL, don't keep faulting
429 * pages and potentially allocating memory.
431 if (unlikely(fatal_signal_pending(current))) {
432 return i ? i : -ERESTARTSYS;
435 /* cond_resched(); */
436 while (!(page = follow_page(vma, start, foll_flags))) {
438 unsigned int fault_flags = 0;
440 /* For mlock, just skip the stack guard page. */
441 if (foll_flags & FOLL_MLOCK) {
442 if (stack_guard_page(vma, start))
445 if (foll_flags & FOLL_WRITE)
446 fault_flags |= FAULT_FLAG_WRITE;
448 fault_flags |= FAULT_FLAG_ALLOW_RETRY;
449 if (foll_flags & FOLL_NOWAIT)
450 fault_flags |= (FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_RETRY_NOWAIT);
452 ret = handle_mm_fault(mm, vma, start,
455 if (ret & VM_FAULT_ERROR) {
456 if (ret & VM_FAULT_OOM) {
457 return i ? i : -ENOMEM;
459 if (ret & (VM_FAULT_HWPOISON |
460 VM_FAULT_HWPOISON_LARGE)) {
464 else if (gup_flags & FOLL_HWPOISON) {
471 if (ret & VM_FAULT_SIGBUS) {
472 return i ? i : -EFAULT;
478 if (ret & VM_FAULT_MAJOR)
484 if (ret & VM_FAULT_RETRY) {
491 * The VM_FAULT_WRITE bit tells us that
492 * do_wp_page has broken COW when necessary,
493 * even if maybe_mkwrite decided not to set
494 * pte_write. We can thus safely do subsequent
495 * page lookups as if they were reads. But only
496 * do so when looping for pte_write is futile:
497 * in some cases userspace may also be wanting
498 * to write to the gotten user page, which a
499 * read fault here might prevent (a readonly
500 * page might get reCOWed by userspace write).
502 if ((ret & VM_FAULT_WRITE) &&
503 !(vma->vm_flags & VM_WRITE))
504 foll_flags &= ~FOLL_WRITE;
506 /* cond_resched(); */
509 return i ? i : PTR_ERR(page);
514 flush_anon_page(vma, page, start);
515 flush_dcache_page(page);
523 } while (nr_pages && start < vma->vm_end);
528 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
530 static int __get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
531 unsigned long start, int len, int flags,
532 struct page **pages, struct vm_area_struct **vmas)
535 unsigned int vm_flags = 0;
536 int write = !!(flags & GUP_FLAGS_WRITE);
537 int force = !!(flags & GUP_FLAGS_FORCE);
538 int ignore = !!(flags & GUP_FLAGS_IGNORE_VMA_PERMISSIONS);
539 int ignore_sigkill = !!(flags & GUP_FLAGS_IGNORE_SIGKILL);
544 * Require read or write permissions.
545 * If 'force' is set, we only require the "MAY" flags.
547 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
548 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
552 struct vm_area_struct *vma;
553 unsigned int foll_flags;
555 vma = find_vma(mm, start);
556 if (!vma && dbi_in_gate_area(tsk, start)) {
557 unsigned long pg = start & PAGE_MASK;
558 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
564 /* user gate pages are read-only */
565 if (!ignore && write)
566 return i ? : -EFAULT;
568 pgd = pgd_offset_k(pg);
570 pgd = pgd_offset_gate(mm, pg);
571 BUG_ON(pgd_none(*pgd));
572 pud = pud_offset(pgd, pg);
573 BUG_ON(pud_none(*pud));
574 pmd = pmd_offset(pud, pg);
576 return i ? : -EFAULT;
577 pte = pte_offset_map(pmd, pg);
578 if (pte_none(*pte)) {
580 return i ? : -EFAULT;
583 struct page *page = vm_normal_page(gate_vma, start, *pte);
598 (vma->vm_flags & (VM_IO | VM_PFNMAP)) ||
599 (!ignore && !(vm_flags & vma->vm_flags)))
600 return i ? : -EFAULT;
602 if (is_vm_hugetlb_page(vma)) {
603 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
604 i = follow_hugetlb_page(mm, vma, pages, vmas,
607 i = follow_hugetlb_page(mm, vma, pages, vmas,
608 &start, &len, i, write);
613 foll_flags = FOLL_TOUCH;
615 foll_flags |= FOLL_GET;
617 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
618 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,30)
619 if (!write && use_zero_page(vma))
620 foll_flags |= FOLL_ANON;
629 * If we have a pending SIGKILL, don't keep faulting
630 * pages and potentially allocating memory, unless
631 * current is handling munlock--e.g., on exit. In
632 * that case, we are not allocating memory. Rather,
633 * we're only unlocking already resident/mapped pages.
635 if (unlikely(!ignore_sigkill &&
636 fatal_signal_pending(current)))
637 return i ? i : -ERESTARTSYS;
641 foll_flags |= FOLL_WRITE;
646 DBPRINTF ("pages = %p vma = %p\n", pages, vma);
647 while (!(page = follow_page(vma, start, foll_flags))) {
649 ret = handle_mm_fault(mm, vma, start,
650 foll_flags & FOLL_WRITE);
652 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
653 if (ret & VM_FAULT_WRITE)
654 foll_flags &= ~FOLL_WRITE;
656 switch (ret & ~VM_FAULT_WRITE) {
663 case VM_FAULT_SIGBUS:
664 return i ? i : -EFAULT;
666 return i ? i : -ENOMEM;
672 if (ret & VM_FAULT_ERROR) {
673 if (ret & VM_FAULT_OOM)
674 return i ? i : -ENOMEM;
675 else if (ret & VM_FAULT_SIGBUS)
676 return i ? i : -EFAULT;
679 if (ret & VM_FAULT_MAJOR)
685 * The VM_FAULT_WRITE bit tells us that
686 * do_wp_page has broken COW when necessary,
687 * even if maybe_mkwrite decided not to set
688 * pte_write. We can thus safely do subsequent
689 * page lookups as if they were reads. But only
690 * do so when looping for pte_write is futile:
691 * in some cases userspace may also be wanting
692 * to write to the gotten user page, which a
693 * read fault here might prevent (a readonly
694 * page might get reCOWed by userspace write).
696 if ((ret & VM_FAULT_WRITE) &&
697 !(vma->vm_flags & VM_WRITE))
698 foll_flags &= ~FOLL_WRITE;
706 return i ? i : PTR_ERR(page);
710 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
711 flush_anon_page(page, start);
713 flush_anon_page(vma, page, start);
715 flush_dcache_page(page);
722 } while (len && start < vma->vm_end);
727 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
729 int get_user_pages_uprobe(struct task_struct *tsk, struct mm_struct *mm,
730 unsigned long start, int len, int write, int force,
731 struct page **pages, struct vm_area_struct **vmas)
733 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 29)
734 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) /* FIXME: must be >= 32! */
735 int flags = FOLL_TOUCH;
743 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
747 flags |= GUP_FLAGS_WRITE;
749 flags |= GUP_FLAGS_FORCE;
750 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
752 return __get_user_pages_uprobe(tsk, mm,
754 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38)
756 #else /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
758 #endif /* LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 38) */
760 return get_user_pages(tsk, mm, start, len, write, force, pages, vmas);
764 #define ACCESS_PROCESS_OPTIMIZATION 0
766 #if ACCESS_PROCESS_OPTIMIZATION
768 #define GET_STEP_X(LEN, STEP) (((LEN) >= (STEP)) ? (STEP) : (LEN) % (STEP))
769 #define GET_STEP_4(LEN) GET_STEP_X((LEN), 4)
771 static void read_data_current(unsigned long addr, void *buf, int len)
776 for (step = GET_STEP_4(len); len; len -= step) {
777 switch (GET_STEP_4(len)) {
779 get_user(*(u8 *)(buf + pos), (unsigned long *)(addr + pos));
785 get_user(*(u16 *)(buf + pos), (unsigned long *)(addr + pos));
790 get_user(*(u32 *)(buf + pos), (unsigned long *)(addr + pos));
800 static void write_data_current(unsigned long addr, void *buf, int len)
805 for (step = GET_STEP_4(len); len; len -= step) {
806 switch (GET_STEP_4(len)) {
808 put_user(*(u8 *)(buf + pos), (unsigned long *)(addr + pos));
814 put_user(*(u16 *)(buf + pos), (unsigned long *)(addr + pos));
819 put_user(*(u32 *)(buf + pos), (unsigned long *)(addr + pos));
829 int access_process_vm_atomic(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write)
831 struct mm_struct *mm;
832 struct vm_area_struct *vma;
839 #if ACCESS_PROCESS_OPTIMIZATION
840 if (write == 0 && tsk == current) {
841 read_data_current(addr, buf, len);
846 mm = tsk->mm; /* function 'get_task_mm' is to be called */
850 /* ignore errors, just check how much was successfully transferred */
852 int bytes, ret, offset;
854 struct page *page = NULL;
856 ret = get_user_pages_uprobe(tsk, mm, addr, 1,
857 write, 1, &page, &vma);
861 * Check if this is a VM_IO | VM_PFNMAP VMA, which
862 * we can access using slightly different code.
864 #ifdef CONFIG_HAVE_IOREMAP_PROT
865 vma = find_vma(mm, addr);
868 if (vma->vm_ops && vma->vm_ops->access)
869 ret = vma->vm_ops->access(vma, addr, buf,
877 offset = addr & (PAGE_SIZE-1);
878 if (bytes > PAGE_SIZE-offset)
879 bytes = PAGE_SIZE-offset;
881 maddr = dbi_kmap_atomic(page);
884 copy_to_user_page(vma, page, addr,
885 maddr + offset, buf, bytes);
886 set_page_dirty_lock(page);
888 copy_from_user_page(vma, page, addr,
889 buf, maddr + offset, bytes);
892 dbi_kunmap_atomic(maddr);
893 page_cache_release(page);
900 return buf - old_buf;
903 int page_present (struct mm_struct *mm, unsigned long address)
911 pgd = pgd_offset(mm, address);
912 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
915 pud = pud_offset(pgd, address);
916 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
919 pmd = pmd_offset(pud, address);
920 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
923 ptep = pte_offset_map(pmd, address);
929 if (pte_present(pte)) {
931 if (pfn_valid(pfn)) {
941 EXPORT_SYMBOL_GPL (page_present);
942 EXPORT_SYMBOL_GPL (get_user_pages_uprobe);
943 EXPORT_SYMBOL_GPL (access_process_vm_atomic);