[FIX] unregister_uretprobe() for x86
[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 <kprobe/arch/asm/dbi_kprobes.h>
29 #include <uprobe/swap_uprobes.h>
30 #include <uprobe/arch/asm/swap_uprobes.h>
31 #include <kprobe/dbi_insn_slots.h>
32
33 struct uprobe_ctlblk {
34         unsigned long flags;
35         struct kprobe *p;
36 };
37
38 static unsigned long trampoline_addr(struct uprobe *up)
39 {
40         return (unsigned long)(up->kp.ainsn.insn +
41                                UPROBES_TRAMP_RET_BREAK_IDX);
42 }
43
44 static DEFINE_PER_CPU(struct uprobe_ctlblk, ucb) = { 0, NULL };
45
46 static struct kprobe *get_current_probe(void)
47 {
48         return __get_cpu_var(ucb).p;
49 }
50
51 static void set_current_probe(struct kprobe *p)
52 {
53         __get_cpu_var(ucb).p = p;
54 }
55
56 static void reset_current_probe(void)
57 {
58         set_current_probe(NULL);
59 }
60
61 static void save_current_flags(struct pt_regs *regs)
62 {
63         __get_cpu_var(ucb).flags = regs->EREG(flags);
64 }
65
66 static void restore_current_flags(struct pt_regs *regs)
67 {
68         regs->EREG(flags) &= ~IF_MASK;
69         regs->EREG(flags) |= __get_cpu_var(ucb).flags & IF_MASK;
70 }
71
72 int arch_prepare_uprobe(struct uprobe *up)
73 {
74         int ret = 0;
75         struct kprobe *p = up2kp(up);
76         struct task_struct *task = up->task;
77         u8 *tramp = up->atramp.tramp;
78
79         if (!read_proc_vm_atomic(task, (unsigned long)p->addr,
80                                  tramp, MAX_INSN_SIZE))
81                 panic("failed to read memory %p!\n", p->addr);
82
83         tramp[UPROBES_TRAMP_RET_BREAK_IDX] = BREAKPOINT_INSTRUCTION;
84
85         /* TODO: remove dual info */
86         p->opcode = tramp[0];
87
88         p->ainsn.boostable = can_boost(tramp) ? 0 : -1;
89
90         return ret;
91 }
92
93 int setjmp_upre_handler(struct kprobe *p, struct pt_regs *regs)
94 {
95         struct uprobe *up = container_of(p, struct uprobe, kp);
96         struct ujprobe *jp = container_of(up, struct ujprobe, up);
97         kprobe_pre_entry_handler_t pre_entry = (kprobe_pre_entry_handler_t)jp->pre_entry;
98         entry_point_t entry = (entry_point_t)jp->entry;
99         unsigned long args[6];
100
101         /* FIXME some user space apps crash if we clean interrupt bit */
102         //regs->EREG(flags) &= ~IF_MASK;
103 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
104         trace_hardirqs_off();
105 #endif
106
107         /* read first 6 args from stack */
108         if (!read_proc_vm_atomic(current, regs->EREG(sp) + 4, args, sizeof(args)))
109                 panic("failed to read user space func arguments %lx!\n", regs->EREG(sp) + 4);
110
111         if (pre_entry)
112                 p->ss_addr = (kprobe_opcode_t *)pre_entry(jp->priv_arg, regs);
113
114         if (entry)
115                 entry(args[0], args[1], args[2], args[3], args[4], args[5]);
116         else
117                 arch_ujprobe_return();
118
119         return 0;
120 }
121
122 void arch_prepare_uretprobe(struct uretprobe_instance *ri, struct pt_regs *regs)
123 {
124         /* Replace the return addr with trampoline addr */
125         unsigned long ra = trampoline_addr(&ri->rp->up);
126         ri->sp = (kprobe_opcode_t *)regs->sp;
127
128         if (!read_proc_vm_atomic(current, regs->EREG(sp), &(ri->ret_addr), sizeof(ri->ret_addr)))
129                 panic("failed to read user space func ra %lx!\n", regs->EREG(sp));
130
131         if (!write_proc_vm_atomic(current, regs->EREG(sp), &ra, sizeof(ra)))
132                 panic("failed to write user space func ra %lx!\n", regs->EREG(sp));
133 }
134
135 int arch_disarm_urp_inst(struct uretprobe_instance *ri,
136                          struct task_struct *task)
137 {
138         int len;
139         unsigned long ret_addr;
140         unsigned long sp = (unsigned long)ri->sp;
141         unsigned long tramp_addr = trampoline_addr(&ri->rp->up);
142         len = read_proc_vm_atomic(task, sp, &ret_addr, sizeof(ret_addr));
143         if (len != sizeof(ret_addr)) {
144                 printk("---> %s (%d/%d): failed to read stack from %08lx\n",
145                        task->comm, task->tgid, task->pid, sp);
146                 return -EFAULT;
147         }
148
149         if (tramp_addr == ret_addr) {
150                 len = write_proc_vm_atomic(task, sp, &ri->ret_addr,
151                                            sizeof(ri->ret_addr));
152                 if (len != sizeof(ri->ret_addr)) {
153                         printk("---> %s (%d/%d): failed to write "
154                                "orig_ret_addr to %08lx",
155                                task->comm, task->tgid, task->pid, sp);
156                         return -EFAULT;
157                 }
158         } else {
159                 printk("---> %s (%d/%d): trampoline NOT found at sp = %08lx\n",
160                        task->comm, task->tgid, task->pid, sp);
161                 return -ENOENT;
162         }
163
164         return 0;
165 }
166
167 unsigned long arch_get_trampoline_addr(struct kprobe *p, struct pt_regs *regs)
168 {
169         return trampoline_addr(kp2up(p));
170 }
171
172 void arch_set_orig_ret_addr(unsigned long orig_ret_addr, struct pt_regs *regs)
173 {
174         regs->EREG(ip) = orig_ret_addr;
175 }
176
177 static void set_user_jmp_op(void *from, void *to)
178 {
179         struct __arch_jmp_op
180         {
181                 char op;
182                 long raddr;
183         } __attribute__ ((packed)) jop;
184
185         jop.raddr = (long)(to) - ((long)(from) + 5);
186         jop.op = RELATIVEJUMP_INSTRUCTION;
187
188         if (!write_proc_vm_atomic(current, (unsigned long)from, &jop, sizeof(jop)))
189                 panic("failed to write jump opcode to user space %p!\n", from);
190 }
191
192 static void resume_execution(struct kprobe *p, struct pt_regs *regs, unsigned long flags)
193 {
194         unsigned long *tos, tos_dword = 0;
195         unsigned long copy_eip = (unsigned long)p->ainsn.insn;
196         unsigned long orig_eip = (unsigned long)p->addr;
197         kprobe_opcode_t insns[2];
198
199         regs->EREG(flags) &= ~TF_MASK;
200
201         tos = (unsigned long *)&tos_dword;
202         if (!read_proc_vm_atomic(current, regs->EREG(sp), &tos_dword, sizeof(tos_dword)))
203                 panic("failed to read dword from top of the user space stack %lx!\n", regs->EREG(sp));
204
205         if (!read_proc_vm_atomic(current, (unsigned long)p->ainsn.insn, insns, 2 * sizeof(kprobe_opcode_t)))
206                 panic("failed to read first 2 opcodes of instruction copy from user space %p!\n", p->ainsn.insn);
207
208         switch (insns[0]) {
209                 case 0x9c:              /* pushfl */
210                         *tos &= ~(TF_MASK | IF_MASK);
211                         *tos |= flags & (TF_MASK | IF_MASK);
212                         break;
213                 case 0xc2:              /* iret/ret/lret */
214                 case 0xc3:
215                 case 0xca:
216                 case 0xcb:
217                 case 0xcf:
218                 case 0xea:              /* jmp absolute -- eip is correct */
219                         /* eip is already adjusted, no more changes required */
220                         p->ainsn.boostable = 1;
221                         goto no_change;
222                 case 0xe8:              /* call relative - Fix return addr */
223                         *tos = orig_eip + (*tos - copy_eip);
224                         break;
225                 case 0x9a:              /* call absolute -- same as call absolute, indirect */
226                         *tos = orig_eip + (*tos - copy_eip);
227
228                         if (!write_proc_vm_atomic(current, regs->EREG (sp), &tos_dword, sizeof(tos_dword)))
229                                 panic("failed to write dword to top of the user space stack %lx!\n", regs->EREG (sp));
230
231                         goto no_change;
232                 case 0xff:
233                         if ((insns[1] & 0x30) == 0x10) {
234                                 /*
235                                  * call absolute, indirect
236                                  * Fix return addr; eip is correct.
237                                  * But this is not boostable
238                                  */
239                                 *tos = orig_eip + (*tos - copy_eip);
240
241                                 if (!write_proc_vm_atomic(current, regs->EREG(sp), &tos_dword, sizeof(tos_dword)))
242                                         panic("failed to write dword to top of the user space stack %lx!\n", regs->EREG(sp));
243
244                                 goto no_change;
245                         } else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
246                                    ((insns[1] & 0x31) == 0x21)) {
247                                 /* jmp far, absolute indirect */
248                                 /* eip is correct. And this is boostable */
249                                 p->ainsn.boostable = 1;
250                                 goto no_change;
251                         }
252                 default:
253                         break;
254         }
255
256         if (!write_proc_vm_atomic(current, regs->EREG(sp), &tos_dword, sizeof(tos_dword)))
257                 panic("failed to write dword to top of the user space stack %lx!\n", regs->EREG(sp));
258
259         if (p->ainsn.boostable == 0) {
260                 if ((regs->EREG(ip) > copy_eip) && (regs->EREG(ip) - copy_eip) + 5 < MAX_INSN_SIZE) {
261                         /*
262                          * These instructions can be executed directly if it
263                          * jumps back to correct address.
264                          */
265                         set_user_jmp_op((void *) regs->EREG(ip), (void *)orig_eip + (regs->EREG(ip) - copy_eip));
266                         p->ainsn.boostable = 1;
267                 } else {
268                         p->ainsn.boostable = -1;
269                 }
270         }
271
272         regs->EREG(ip) = orig_eip + (regs->EREG(ip) - copy_eip);
273
274 no_change:
275         return;
276 }
277
278 static int make_trampoline(struct uprobe *up)
279 {
280         struct kprobe *p = up2kp(up);
281         struct task_struct *task = up->task;
282         void *tramp;
283
284         tramp = alloc_insn_slot(up->sm);
285         if (tramp == 0) {
286                 printk("trampoline out of memory\n");
287                 return -ENOMEM;
288         }
289
290         if (!write_proc_vm_atomic(task, (unsigned long)tramp,
291                                   up->atramp.tramp,
292                                   sizeof(up->atramp.tramp))) {
293                 free_insn_slot(up->sm, tramp);
294                 panic("failed to write memory %p!\n", tramp);
295                 return -EINVAL;
296         }
297
298         p->ainsn.insn = tramp;
299
300         return 0;
301 }
302
303 static int uprobe_handler(struct pt_regs *regs)
304 {
305         struct kprobe *p;
306         kprobe_opcode_t *addr;
307         struct task_struct *task = current;
308         pid_t tgid = task->tgid;
309
310         save_current_flags(regs);
311
312         addr = (kprobe_opcode_t *)(regs->EREG(ip) - sizeof(kprobe_opcode_t));
313         p = get_ukprobe(addr, tgid);
314
315         if (p == NULL) {
316                 void *tramp_addr = (void *)addr - UPROBES_TRAMP_RET_BREAK_IDX;
317
318                 p = get_ukprobe_by_insn_slot(tramp_addr, tgid, regs);
319                 if (p == NULL) {
320                         printk("no_uprobe\n");
321                         return 0;
322                 }
323
324                 trampoline_uprobe_handler(p, regs);
325                 return 1;
326         } else {
327                 if (p->ainsn.insn == NULL) {
328                         struct uprobe *up = kp2up(p);
329
330                         make_trampoline(up);
331
332                         /* for uretprobe */
333                         add_uprobe_table(p);
334                 }
335
336                 if (!p->pre_handler || !p->pre_handler(p, regs)) {
337                         if (p->ainsn.boostable == 1 && !p->post_handler) {
338                                 regs->EREG(ip) = (unsigned long)p->ainsn.insn;
339                                 return 1;
340                         }
341
342                         prepare_singlestep(p, regs);
343                 }
344         }
345
346         set_current_probe(p);
347
348         return 1;
349 }
350
351 static int post_uprobe_handler(struct pt_regs *regs)
352 {
353         struct kprobe *p = get_current_probe();
354         unsigned long flags = __get_cpu_var(ucb).flags;
355
356         if (p == NULL)
357                 return 0;
358
359         resume_execution(p, regs, flags);
360         restore_current_flags(regs);
361
362         reset_current_probe();
363
364         return 1;
365 }
366
367 static int uprobe_exceptions_notify(struct notifier_block *self, unsigned long val, void *data)
368 {
369         struct die_args *args = (struct die_args *)data;
370         int ret = NOTIFY_DONE;
371
372         if (args->regs && !user_mode_vm(args->regs))
373                 return ret;
374
375         switch (val) {
376 #ifdef CONFIG_KPROBES
377                 case DIE_INT3:
378 #else
379                 case DIE_TRAP:
380 #endif
381                         if (uprobe_handler(args->regs))
382                                 ret = NOTIFY_STOP;
383                         break;
384                 case DIE_DEBUG:
385                         if (post_uprobe_handler(args->regs))
386                                 ret = NOTIFY_STOP;
387                         break;
388                 default:
389                         break;
390         }
391
392         return ret;
393 }
394
395 static struct notifier_block uprobe_exceptions_nb = {
396         .notifier_call = uprobe_exceptions_notify,
397         .priority = INT_MAX
398 };
399
400 int swap_arch_init_uprobes(void)
401 {
402         return register_die_notifier(&uprobe_exceptions_nb);
403 }
404
405 void swap_arch_exit_uprobes(void)
406 {
407         unregister_die_notifier(&uprobe_exceptions_nb);
408 }
409