[STYLE] Code style
[kernel/swap-modules.git] / uprobe / arch / asm-x86 / swap_uprobes.c
1 /*
2  *  Dynamic Binary Instrumentation Module based on KProbes
3  *  modules/uprobe/arch/asm-x86/swap_uprobes.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, 2006-2010
20  *
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
24  *
25  */
26
27 #include <linux/kdebug.h>
28 #include <asm/dbi_kprobes.h>
29 #include <swap_uprobes.h>
30 #include <asm/swap_uprobes.h>
31 #include <dbi_insn_slots.h>
32
33 struct uprobe_ctlblk {
34         unsigned long flags;
35         struct kprobe *p;
36 };
37
38 static DEFINE_PER_CPU(struct uprobe_ctlblk, ucb) = { 0, NULL };
39
40 int arch_prepare_uprobe(struct uprobe *up, struct hlist_head *page_list)
41 {
42         int ret = 0;
43         struct kprobe *p = &up->kp;
44         struct task_struct *task = up->task;
45         kprobe_opcode_t insns[UPROBES_TRAMP_LEN];
46
47         if (!ret) {
48                 kprobe_opcode_t insn[MAX_INSN_SIZE];
49                 struct arch_specific_insn ainsn;
50
51                 if (!read_proc_vm_atomic(task, (unsigned long)p->addr, &insn, MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
52                         panic("failed to read memory %p!\n", p->addr);
53
54                 ainsn.insn = insn;
55                 ret = arch_check_insn(&ainsn);
56                 if (!ret) {
57                         p->opcode = insn[0];
58                         p->ainsn.insn = alloc_insn_slot(up->sm);
59                         if (!p->ainsn.insn)
60                                 return -ENOMEM;
61
62                         if (can_boost(insn))
63                                 p->ainsn.boostable = 0;
64                         else
65                                 p->ainsn.boostable = -1;
66
67                         memcpy(&insns[UPROBES_TRAMP_INSN_IDX], insn, MAX_INSN_SIZE*sizeof(kprobe_opcode_t));
68                         insns[UPROBES_TRAMP_RET_BREAK_IDX] = BREAKPOINT_INSTRUCTION;
69
70                         if (!write_proc_vm_atomic(task, (unsigned long)p->ainsn.insn, insns, sizeof(insns))) {
71                                 free_insn_slot(up->sm, p->ainsn.insn);
72                                 panic("failed to write memory %p!\n", p->ainsn.insn);
73                                 return -EINVAL;
74                         }
75                 }
76         }
77
78         return ret;
79 }
80
81 int setjmp_upre_handler(struct kprobe *p, struct pt_regs *regs)
82 {
83         struct uprobe *up = container_of(p, struct uprobe, kp);
84         struct ujprobe *jp = container_of(up, struct ujprobe, up);
85         kprobe_pre_entry_handler_t pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
86         entry_point_t entry = (entry_point_t)jp->entry;
87         unsigned long addr, args[6];
88
89         /* FIXME some user space apps crash if we clean interrupt bit */
90         //regs->EREG(flags) &= ~IF_MASK;
91 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
92         trace_hardirqs_off();
93 #endif
94
95         /* read first 6 args from stack */
96         if (!read_proc_vm_atomic(current, regs->EREG(sp) + 4, args, sizeof(args)))
97                 panic("failed to read user space func arguments %lx!\n", regs->EREG(sp) + 4);
98
99         if (pre_entry)
100                 p->ss_addr = pre_entry(jp->priv_arg, regs);
101
102         if (entry)
103                 entry(args[0], args[1], args[2], args[3], args[4], args[5]);
104         else
105                 arch_ujprobe_return();
106
107         return 0;
108 }
109
110 void arch_prepare_uretprobe(struct uretprobe_instance *ri, struct pt_regs *regs)
111 {
112         /* Replace the return addr with trampoline addr */
113         unsigned long ra = (unsigned long)(ri->rp->up.kp.ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
114
115         if (!read_proc_vm_atomic(current, regs->EREG(sp), &(ri->ret_addr), sizeof(ri->ret_addr)))
116                 panic("failed to read user space func ra %lx!\n", regs->EREG(sp));
117
118         if (!write_proc_vm_atomic(current, regs->EREG(sp), &ra, sizeof(ra)))
119                 panic("failed to write user space func ra %lx!\n", regs->EREG(sp));
120 }
121
122 unsigned long arch_get_trampoline_addr(struct kprobe *p, struct pt_regs *regs)
123 {
124         return (unsigned long)(p->ainsn.insn + UPROBES_TRAMP_RET_BREAK_IDX);
125 }
126
127 void arch_set_orig_ret_addr(unsigned long orig_ret_addr, struct pt_regs *regs)
128 {
129         regs->EREG(ip) = orig_ret_addr;
130 }
131
132 static void set_user_jmp_op(void *from, void *to)
133 {
134         struct __arch_jmp_op
135         {
136                 char op;
137                 long raddr;
138         } __attribute__ ((packed)) jop;
139
140         jop.raddr = (long)(to) - ((long)(from) + 5);
141         jop.op = RELATIVEJUMP_INSTRUCTION;
142
143         if (!write_proc_vm_atomic(current, (unsigned long)from, &jop, sizeof(jop)))
144                 panic("failed to write jump opcode to user space %p!\n", from);
145 }
146
147 static void resume_execution(struct kprobe *p, struct pt_regs *regs, unsigned long flags)
148 {
149         unsigned long *tos, tos_dword = 0;
150         unsigned long copy_eip = (unsigned long)p->ainsn.insn;
151         unsigned long orig_eip = (unsigned long)p->addr;
152         kprobe_opcode_t insns[2];
153
154         regs->EREG(flags) &= ~TF_MASK;
155
156         tos = (unsigned long *)&tos_dword;
157         if (!read_proc_vm_atomic(current, regs->EREG(sp), &tos_dword, sizeof(tos_dword)))
158                 panic("failed to read dword from top of the user space stack %lx!\n", regs->EREG(sp));
159
160         if (!read_proc_vm_atomic(current, (unsigned long)p->ainsn.insn, insns, 2 * sizeof(kprobe_opcode_t)))
161                 panic("failed to read first 2 opcodes of instruction copy from user space %p!\n", p->ainsn.insn);
162
163         switch (insns[0]) {
164                 case 0x9c:              /* pushfl */
165                         *tos &= ~(TF_MASK | IF_MASK);
166                         *tos |= flags;
167                         break;
168                 case 0xc2:              /* iret/ret/lret */
169                 case 0xc3:
170                 case 0xca:
171                 case 0xcb:
172                 case 0xcf:
173                 case 0xea:              /* jmp absolute -- eip is correct */
174                         /* eip is already adjusted, no more changes required */
175                         p->ainsn.boostable = 1;
176                         goto no_change;
177                 case 0xe8:              /* call relative - Fix return addr */
178                         *tos = orig_eip + (*tos - copy_eip);
179                         break;
180                 case 0x9a:              /* call absolute -- same as call absolute, indirect */
181                         *tos = orig_eip + (*tos - copy_eip);
182
183                         if (!write_proc_vm_atomic(current, regs->EREG (sp), &tos_dword, sizeof(tos_dword)))
184                                 panic("failed to write dword to top of the user space stack %lx!\n", regs->EREG (sp));
185
186                         goto no_change;
187                 case 0xff:
188                         if ((insns[1] & 0x30) == 0x10) {
189                                 /*
190                                  * call absolute, indirect
191                                  * Fix return addr; eip is correct.
192                                  * But this is not boostable
193                                  */
194                                 *tos = orig_eip + (*tos - copy_eip);
195
196                                 if (!write_proc_vm_atomic(current, regs->EREG(sp), &tos_dword, sizeof(tos_dword)))
197                                         panic("failed to write dword to top of the user space stack %lx!\n", regs->EREG(sp));
198
199                                 goto no_change;
200                         } else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
201                                    ((insns[1] & 0x31) == 0x21)) {
202                                 /* jmp far, absolute indirect */
203                                 /* eip is correct. And this is boostable */
204                                 p->ainsn.boostable = 1;
205                                 goto no_change;
206                         }
207                 default:
208                         break;
209         }
210
211         if (!write_proc_vm_atomic(current, regs->EREG(sp), &tos_dword, sizeof(tos_dword)))
212                 panic("failed to write dword to top of the user space stack %lx!\n", regs->EREG(sp));
213
214         if (p->ainsn.boostable == 0) {
215                 if ((regs->EREG(ip) > copy_eip) && (regs->EREG(ip) - copy_eip) + 5 < MAX_INSN_SIZE) {
216                         /*
217                          * These instructions can be executed directly if it
218                          * jumps back to correct address.
219                          */
220                         set_user_jmp_op((void *) regs->EREG(ip), (void *)orig_eip + (regs->EREG(ip) - copy_eip));
221                         p->ainsn.boostable = 1;
222                 } else {
223                         p->ainsn.boostable = -1;
224                 }
225         }
226
227         regs->EREG(ip) = orig_eip + (regs->EREG(ip) - copy_eip);
228
229 no_change:
230         return;
231 }
232
233 static int uprobe_handler(struct pt_regs *regs)
234 {
235         struct kprobe *p;
236         kprobe_opcode_t *addr;
237         struct task_struct *task = current;
238         pid_t tgid = task->tgid;
239
240         addr = (kprobe_opcode_t *)(regs->EREG(ip) - sizeof(kprobe_opcode_t));
241         p = get_ukprobe(addr, tgid);
242
243         if (p == NULL) {
244                 p = get_ukprobe_by_insn_slot(addr, tgid, regs);
245
246                 if (p == NULL) {
247                         printk("no_uprobe\n");
248                         return 0;
249                 }
250
251                 trampoline_uprobe_handler(p, regs);
252         } else {
253                 if (!p->pre_handler || !p->pre_handler(p, regs))
254                         prepare_singlestep(p, regs);
255         }
256
257         __get_cpu_var(ucb).p = p;
258         __get_cpu_var(ucb).flags = (regs->EREG(flags) & (TF_MASK | IF_MASK));
259
260         return 1;
261 }
262
263 static int post_uprobe_handler(struct pt_regs *regs)
264 {
265         struct kprobe *p = __get_cpu_var(ucb).p;
266         unsigned long flags = __get_cpu_var(ucb).flags;
267
268         resume_execution(p, regs, flags);
269
270         return 1;
271 }
272
273 static int uprobe_exceptions_notify(struct notifier_block *self, unsigned long val, void *data)
274 {
275         struct die_args *args = (struct die_args *)data;
276         int ret = NOTIFY_DONE;
277
278         if (args->regs && !user_mode_vm(args->regs))
279                 return ret;
280
281         switch (val) {
282 #ifdef CONFIG_KPROBES
283                 case DIE_INT3:
284 #else
285                 case DIE_TRAP:
286 #endif
287                         if (uprobe_handler(args->regs))
288                                 ret = NOTIFY_STOP;
289                         break;
290                 case DIE_DEBUG:
291                         if (post_uprobe_handler(args->regs))
292                                 ret = NOTIFY_STOP;
293                         break;
294                 default:
295                         break;
296         }
297
298         return ret;
299 }
300
301 static struct notifier_block uprobe_exceptions_nb = {
302         .notifier_call = uprobe_exceptions_notify,
303         .priority = INT_MAX
304 };
305
306 int swap_arch_init_uprobes(void)
307 {
308         return register_die_notifier(&uprobe_exceptions_nb);
309 }
310
311 void swap_arch_exit_uprobes(void)
312 {
313         unregister_die_notifier(&uprobe_exceptions_nb);
314 }
315