[FIX] uprobe build for x86
[kernel/swap-modules.git] / uprobe / arch / x86 / swap-asm / swap_uprobes.c
1 /**
2  * uprobe/arch/asm-x86/swap_uprobes.c
3  * @author Alexey Gerenkov <a.gerenkov@samsung.com> User-Space Probes initial
4  * implementation; Support x86/ARM/MIPS for both user and kernel spaces.
5  * @author Ekaterina Gorelkina <e.gorelkina@samsung.com>: redesign module for
6  * separating core and arch parts
7  *
8  * @section LICENSE
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  *
24  * @section COPYRIGHT
25  *
26  * Copyright (C) Samsung Electronics, 2006-2010
27  *
28  * @section DESCRIPTION
29  *
30  * Arch-dependent uprobe interface implementation for x86.
31  */
32
33
34 #include <linux/kdebug.h>
35
36 #include <kprobe/swap_slots.h>
37 #include <uprobe/swap_uprobes.h>
38
39 #include "swap_uprobes.h"
40
41
42 /**
43  * @struct uprobe_ctlblk
44  * @brief Uprobe control block
45  */
46 struct uprobe_ctlblk {
47         unsigned long flags;            /**< Flags */
48         struct kprobe *p;               /**< Pointer to the uprobe's kprobe */
49 };
50
51 static unsigned long trampoline_addr(struct uprobe *up)
52 {
53         return (unsigned long)(up->kp.ainsn.insn +
54                                UPROBES_TRAMP_RET_BREAK_IDX);
55 }
56
57 static DEFINE_PER_CPU(struct uprobe_ctlblk, ucb) = { 0, NULL };
58
59 static struct kprobe *get_current_probe(void)
60 {
61         return __get_cpu_var(ucb).p;
62 }
63
64 static void set_current_probe(struct kprobe *p)
65 {
66         __get_cpu_var(ucb).p = p;
67 }
68
69 static void reset_current_probe(void)
70 {
71         set_current_probe(NULL);
72 }
73
74 static void save_current_flags(struct pt_regs *regs)
75 {
76         __get_cpu_var(ucb).flags = regs->EREG(flags);
77 }
78
79 static void restore_current_flags(struct pt_regs *regs)
80 {
81         regs->EREG(flags) &= ~IF_MASK;
82         regs->EREG(flags) |= __get_cpu_var(ucb).flags & IF_MASK;
83 }
84
85 /**
86  * @brief Prepares uprobe for x86.
87  *
88  * @param up Pointer to the uprobe.
89  * @return 0 on success,\n
90  * -1 on error.
91  */
92 int arch_prepare_uprobe(struct uprobe *up)
93 {
94         struct kprobe *p = up2kp(up);
95         struct task_struct *task = up->task;
96         u8 *tramp = up->atramp.tramp;
97         enum { call_relative_opcode = 0xe8 };
98
99         if (!read_proc_vm_atomic(task, (unsigned long)p->addr,
100                                  tramp, MAX_INSN_SIZE)) {
101                 printk(KERN_ERR "failed to read memory %p!\n", p->addr);
102                 return -EINVAL;
103         }
104         /* TODO: this is a workaround */
105         if (tramp[0] == call_relative_opcode) {
106                 printk(KERN_INFO "cannot install probe: 1st instruction is call\n");
107                 return -EINVAL;
108         }
109
110         tramp[UPROBES_TRAMP_RET_BREAK_IDX] = BREAKPOINT_INSTRUCTION;
111
112         /* TODO: remove dual info */
113         p->opcode = tramp[0];
114
115         p->ainsn.boostable = swap_can_boost(tramp) ? 0 : -1;
116
117         p->ainsn.insn = swap_slot_alloc(up->sm);
118         if (p->ainsn.insn == NULL) {
119                 printk(KERN_ERR "trampoline out of memory\n");
120                 return -ENOMEM;
121         }
122
123         if (!write_proc_vm_atomic(task, (unsigned long)p->ainsn.insn,
124                                   tramp, sizeof(up->atramp.tramp))) {
125                 swap_slot_free(up->sm, p->ainsn.insn);
126                 printk(KERN_INFO "failed to write memory %p!\n", tramp);
127                 return -EINVAL;
128         }
129
130         return 0;
131 }
132
133 /**
134  * @brief Jump pre-handler.
135  *
136  * @param p Pointer to the uprobe's kprobe.
137  * @param regs Pointer to CPU register data.
138  * @return 0.
139  */
140 int setjmp_upre_handler(struct kprobe *p, struct pt_regs *regs)
141 {
142         struct uprobe *up = container_of(p, struct uprobe, kp);
143         struct ujprobe *jp = container_of(up, struct ujprobe, up);
144         kprobe_pre_entry_handler_t pre_entry =
145                 (kprobe_pre_entry_handler_t)jp->pre_entry;
146         entry_point_t entry = (entry_point_t)jp->entry;
147         unsigned long args[6];
148
149         /* FIXME some user space apps crash if we clean interrupt bit */
150         /* regs->EREG(flags) &= ~IF_MASK; */
151 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
152         trace_hardirqs_off();
153 #endif
154
155         /* read first 6 args from stack */
156         if (!read_proc_vm_atomic(current, regs->EREG(sp) + 4,
157                                  args, sizeof(args)))
158                 printk(KERN_WARNING
159                        "failed to read user space func arguments %lx!\n",
160                        regs->sp + 4);
161
162         if (pre_entry)
163                 p->ss_addr[smp_processor_id()] = (kprobe_opcode_t *)
164                                                  pre_entry(jp->priv_arg, regs);
165
166         if (entry)
167                 entry(args[0], args[1], args[2], args[3], args[4], args[5]);
168         else
169                 arch_ujprobe_return();
170
171         return 0;
172 }
173
174 /**
175  * @brief Prepares uretprobe for x86.
176  *
177  * @param ri Pointer to the uretprobe instance.
178  * @param regs Pointer to CPU register data.
179  * @return Void.
180  */
181 int arch_prepare_uretprobe(struct uretprobe_instance *ri, struct pt_regs *regs)
182 {
183         /* Replace the return addr with trampoline addr */
184         unsigned long ra = trampoline_addr(&ri->rp->up);
185         ri->sp = (kprobe_opcode_t *)regs->sp;
186
187         if (!read_proc_vm_atomic(current, regs->EREG(sp), &(ri->ret_addr),
188                                  sizeof(ri->ret_addr))) {
189                 printk(KERN_ERR "failed to read user space func ra %lx addr=%p!\n",
190                                 regs->EREG(sp), ri->rp->up.kp.addr);
191                 return -EINVAL;
192         }
193
194         if (!write_proc_vm_atomic(current, regs->EREG(sp), &ra, sizeof(ra))) {
195                 printk(KERN_ERR "failed to write user space func ra %lx!\n",
196                        regs->EREG(sp));
197                 return -EINVAL;
198         }
199
200         add_uprobe_table(&ri->rp->up.kp);
201
202         return 0;
203 }
204
205 /**
206  * @brief Disarms uretprobe on x86 arch.
207  *
208  * @param ri Pointer to the uretprobe instance.
209  * @param task Pointer to the task for which the probe.
210  * @return 0 on success,\n
211  * negative error code on error.
212  */
213 int arch_disarm_urp_inst(struct uretprobe_instance *ri,
214                          struct task_struct *task)
215 {
216         int len;
217         unsigned long ret_addr;
218         unsigned long sp = (unsigned long)ri->sp;
219         unsigned long tramp_addr = trampoline_addr(&ri->rp->up);
220         len = read_proc_vm_atomic(task, sp, &ret_addr, sizeof(ret_addr));
221         if (len != sizeof(ret_addr)) {
222                 printk(KERN_INFO "---> %s (%d/%d): failed to read stack from %08lx\n",
223                        task->comm, task->tgid, task->pid, sp);
224                 return -EFAULT;
225         }
226
227         if (tramp_addr == ret_addr) {
228                 len = write_proc_vm_atomic(task, sp, &ri->ret_addr,
229                                            sizeof(ri->ret_addr));
230                 if (len != sizeof(ri->ret_addr)) {
231                         printk(KERN_INFO "---> %s (%d/%d): failed to write "
232                                "orig_ret_addr to %08lx",
233                                task->comm, task->tgid, task->pid, sp);
234                         return -EFAULT;
235                 }
236         } else {
237                 printk(KERN_INFO "---> %s (%d/%d): trampoline NOT found at sp = %08lx\n",
238                        task->comm, task->tgid, task->pid, sp);
239                 return -ENOENT;
240         }
241
242         return 0;
243 }
244
245 /**
246  * @brief Gets trampoline address.
247  *
248  * @param p Pointer to the uprobe's kprobe.
249  * @param regs Pointer to CPU register data.
250  * @return Trampoline address.
251  */
252 unsigned long arch_get_trampoline_addr(struct kprobe *p, struct pt_regs *regs)
253 {
254         return trampoline_addr(kp2up(p));
255 }
256
257 /**
258  * @brief Restores return address.
259  *
260  * @param orig_ret_addr Original return address.
261  * @param regs Pointer to CPU register data.
262  * @return Void.
263  */
264 void arch_set_orig_ret_addr(unsigned long orig_ret_addr, struct pt_regs *regs)
265 {
266         regs->EREG(ip) = orig_ret_addr;
267 }
268
269 /**
270  * @brief Removes uprobe.
271  *
272  * @param up Pointer to the target uprobe.
273  * @return Void.
274  */
275 void arch_remove_uprobe(struct uprobe *up)
276 {
277         struct kprobe *p = up2kp(up);
278
279         swap_slot_free(up->sm, p->ainsn.insn);
280 }
281
282 static void set_user_jmp_op(void *from, void *to)
283 {
284         struct __arch_jmp_op {
285                 char op;
286                 long raddr;
287         } __packed jop;
288
289         jop.raddr = (long)(to) - ((long)(from) + 5);
290         jop.op = RELATIVEJUMP_INSTRUCTION;
291
292         if (!write_proc_vm_atomic(current, (unsigned long)from, &jop,
293                                   sizeof(jop)))
294                 printk(KERN_WARNING
295                        "failed to write jump opcode to user space %p\n", from);
296 }
297
298 static void resume_execution(struct kprobe *p,
299                              struct pt_regs *regs,
300                              unsigned long flags)
301 {
302         unsigned long *tos, tos_dword = 0;
303         unsigned long copy_eip = (unsigned long)p->ainsn.insn;
304         unsigned long orig_eip = (unsigned long)p->addr;
305         kprobe_opcode_t insns[2];
306
307         regs->EREG(flags) &= ~TF_MASK;
308
309         tos = (unsigned long *)&tos_dword;
310         if (!read_proc_vm_atomic(current, regs->EREG(sp), &tos_dword,
311                                  sizeof(tos_dword))) {
312                 printk(KERN_WARNING
313                        "failed to read dword from top of the user space stack %lx!\n",
314                        regs->sp);
315                 return;
316         }
317
318         if (!read_proc_vm_atomic(current, (unsigned long)p->ainsn.insn, insns,
319                                  2 * sizeof(kprobe_opcode_t))) {
320                 printk(KERN_WARNING
321                        "failed to read first 2 opcodes of instruction copy from user space %p!\n",
322                        p->ainsn.insn);
323                 return;
324         }
325
326         switch (insns[0]) {
327         case 0x9c: /* pushfl */
328                 *tos &= ~(TF_MASK | IF_MASK);
329                 *tos |= flags & (TF_MASK | IF_MASK);
330                 break;
331         case 0xc2: /* iret/ret/lret */
332         case 0xc3:
333         case 0xca:
334         case 0xcb:
335         case 0xcf:
336         case 0xea: /* jmp absolute -- eip is correct */
337                 /* eip is already adjusted, no more changes required */
338                 p->ainsn.boostable = 1;
339                 goto no_change;
340         case 0xe8: /* call relative - Fix return addr */
341                 *tos = orig_eip + (*tos - copy_eip);
342                 break;
343         case 0x9a: /* call absolute -- same as call absolute, indirect */
344                 *tos = orig_eip + (*tos - copy_eip);
345
346                 if (!write_proc_vm_atomic(current,
347                                           regs->EREG(sp),
348                                           &tos_dword,
349                                           sizeof(tos_dword))) {
350                         printk(KERN_WARNING
351                                "failed to write dword to top of the user space stack %lx!\n",
352                                regs->sp);
353                         return;
354                 }
355
356                 goto no_change;
357         case 0xff:
358                 if ((insns[1] & 0x30) == 0x10) {
359                         /*
360                          * call absolute, indirect
361                          * Fix return addr; eip is correct.
362                          * But this is not boostable
363                          */
364                         *tos = orig_eip + (*tos - copy_eip);
365
366                         if (!write_proc_vm_atomic(current, regs->EREG(sp),
367                                                   &tos_dword,
368                                                   sizeof(tos_dword))) {
369                                 printk(KERN_WARNING
370                                        "failed to write dword to top of the user space stack %lx!\n",
371                                        regs->EREG(sp));
372                                 return;
373                         }
374
375                         goto no_change;
376                 } else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute
377                                                            * indirect */
378                            ((insns[1] & 0x31) == 0x21)) {
379                         /* jmp far, absolute indirect */
380                         /* eip is correct. And this is boostable */
381                         p->ainsn.boostable = 1;
382                         goto no_change;
383                 }
384         case 0xf3:
385                 if (insns[1] == 0xc3)
386                         /* repz ret special handling: no more changes */
387                         goto no_change;
388                 break;
389         default:
390                 break;
391         }
392
393         if (!write_proc_vm_atomic(current, regs->EREG(sp), &tos_dword,
394                                   sizeof(tos_dword))) {
395                 printk(KERN_WARNING
396                        "failed to write dword to top of the user space stack %lx!\n",
397                        regs->EREG(sp));
398                 return;
399         }
400
401         if (p->ainsn.boostable == 0) {
402                 if ((regs->EREG(ip) > copy_eip) && (regs->EREG(ip) - copy_eip) +
403                     5 < MAX_INSN_SIZE) {
404                         /*
405                          * These instructions can be executed directly if it
406                          * jumps back to correct address.
407                          */
408                         set_user_jmp_op((void *) regs->EREG(ip),
409                                         (void *)orig_eip +
410                                         (regs->EREG(ip) - copy_eip));
411                         p->ainsn.boostable = 1;
412                 } else {
413                         p->ainsn.boostable = -1;
414                 }
415         }
416
417         regs->EREG(ip) = orig_eip + (regs->EREG(ip) - copy_eip);
418
419 no_change:
420         return;
421 }
422
423 static int uprobe_handler(struct pt_regs *regs)
424 {
425         struct kprobe *p;
426         kprobe_opcode_t *addr;
427         struct task_struct *task = current;
428         pid_t tgid = task->tgid;
429
430         save_current_flags(regs);
431
432         addr = (kprobe_opcode_t *)(regs->EREG(ip) - sizeof(kprobe_opcode_t));
433         p = get_ukprobe(addr, tgid);
434
435         if (p == NULL) {
436                 void *tramp_addr = (void *)addr - UPROBES_TRAMP_RET_BREAK_IDX;
437
438                 p = get_ukprobe_by_insn_slot(tramp_addr, tgid, regs);
439                 if (p == NULL) {
440                         printk(KERN_INFO "no_uprobe\n");
441                         return 0;
442                 }
443
444                 trampoline_uprobe_handler(p, regs);
445                 return 1;
446         } else {
447                 if (!p->pre_handler || !p->pre_handler(p, regs)) {
448
449                         if (p->ainsn.boostable == 1 && !p->post_handler) {
450                                 if (p->ss_addr[smp_processor_id()]) {
451                                         regs->EREG(ip) = (unsigned long)p->ss_addr[smp_processor_id()];
452                                         p->ss_addr[smp_processor_id()] = NULL;
453                                 } else {
454                                         regs->EREG(ip) = (unsigned long)p->ainsn.insn;
455                                 }
456                                 return 1;
457                         }
458
459                         prepare_singlestep(p, regs);
460                 }
461         }
462
463         set_current_probe(p);
464
465         return 1;
466 }
467
468 static int post_uprobe_handler(struct pt_regs *regs)
469 {
470         struct kprobe *p = get_current_probe();
471         unsigned long flags = __get_cpu_var(ucb).flags;
472
473         if (p == NULL)
474                 return 0;
475
476         resume_execution(p, regs, flags);
477         restore_current_flags(regs);
478
479         reset_current_probe();
480
481         return 1;
482 }
483
484 static int uprobe_exceptions_notify(struct notifier_block *self,
485                                     unsigned long val, void *data)
486 {
487         struct die_args *args = (struct die_args *)data;
488         int ret = NOTIFY_DONE;
489
490         if (args->regs == NULL || !user_mode_vm(args->regs))
491                 return ret;
492
493         switch (val) {
494 #ifdef CONFIG_KPROBES
495         case DIE_INT3:
496 #else
497         case DIE_TRAP:
498 #endif
499                 if (uprobe_handler(args->regs))
500                         ret = NOTIFY_STOP;
501                 break;
502         case DIE_DEBUG:
503                 if (post_uprobe_handler(args->regs))
504                         ret = NOTIFY_STOP;
505                 break;
506         default:
507                 break;
508         }
509
510         return ret;
511 }
512
513 static struct notifier_block uprobe_exceptions_nb = {
514         .notifier_call = uprobe_exceptions_notify,
515         .priority = INT_MAX
516 };
517
518 /**
519  * @brief Registers notify.
520  *
521  * @return register_die_notifier result.
522  */
523 int swap_arch_init_uprobes(void)
524 {
525         return register_die_notifier(&uprobe_exceptions_nb);
526 }
527
528 /**
529  * @brief Unregisters notify.
530  *
531  * @return Void.
532  */
533 void swap_arch_exit_uprobes(void)
534 {
535         unregister_die_notifier(&uprobe_exceptions_nb);
536 }
537