[REFACTOR] us_manager: install helper probes
[kernel/swap-modules.git] / writer / kernel_operations_x86.c
1 /*
2  *  SWAP Writer
3  *  modules/writer/kernel_operations_x86.c
4  *
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.
9  *
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.
14  *
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.
18  *
19  * Copyright (C) Samsung Electronics, 2013
20  *
21  * 2013  Alexander Aksenov <a.aksenov@samsung.com>: SWAP Writer module kernel
22  * operaions implement
23  *
24  */
25
26 #include <asm/ptrace.h>
27 #include <asm/uaccess.h>
28 #include <asm/elf.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/mm.h>
32
33 #include "kernel_operations.h"
34
35
36 /* ======================= ARGS ========================== */
37
38 int get_args(unsigned long args[], int cnt, struct pt_regs *regs)
39 {
40         int i, stack_args = 0;
41
42         /* If we're in kernel mode on x86, get arguments from bx, cx, dx, si,
43          * di, bp
44          */
45         if (!user_mode(regs)) {
46                 int args_in_regs;
47                 args_in_regs = cnt < 5 ? cnt : 5;
48                 stack_args = 6;
49
50                 switch (args_in_regs) {
51                         case 5:
52                                 args[5] = regs->bp;
53                         case 4:
54                                 args[4] = regs->di;
55                         case 3:
56                                 args[3] = regs->si;
57                         case 2:
58                                 args[2] = regs->dx;
59                         case 1:
60                                 args[1] = regs->cx;
61                         case 0:
62                                 args[0] = regs->bx;
63                 }
64         }
65
66         /* Get other args from stack */
67         for (i = stack_args; i < cnt; ++i) {
68                 unsigned long *args_in_sp = (unsigned long *)regs->sp +
69                                             1 + i - stack_args;
70                 if (get_user(args[i], args_in_sp))
71                         printk("failed to dereference a pointer, addr=%p\n",
72                                args_in_sp);
73         }
74
75         return 0;
76 }
77
78
79 /* ================== KERNEL SHARED MEM ===================== */
80
81 const char *get_shared_kmem(struct mm_struct *mm, unsigned long *start,
82                             unsigned long *end)
83 {
84         unsigned long vdso;
85         struct vm_area_struct *vma_vdso;
86
87         vdso = (unsigned long)mm->context.vdso;
88         vma_vdso = find_vma_intersection(mm, vdso, vdso + 1);
89
90         if (vma_vdso == NULL) {
91                 print_err("Cannot get VDSO mapping\n");
92                 return NULL;
93         }
94
95         *start = vma_vdso->vm_start;
96         *end = vma_vdso->vm_end;
97
98         return "[vdso]";
99 }