Tizen 2.4.0 rev3 SDK Public Release
[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 unsigned long arch_tramp_by_ri(struct uretprobe_instance *ri)
58 {
59         return trampoline_addr(&ri->rp->up);
60 }
61
62 static struct uprobe_ctlblk *current_ucb(void)
63 {
64         /* FIXME hardcoded offset */
65         return (struct uprobe_ctlblk *)(end_of_stack(current) + 20);
66 }
67
68 static struct kprobe *get_current_probe(void)
69 {
70         return current_ucb()->p;
71 }
72
73 static void set_current_probe(struct kprobe *p)
74 {
75         current_ucb()->p = p;
76 }
77
78 static void save_current_flags(struct pt_regs *regs)
79 {
80         current_ucb()->flags = regs->flags;
81 }
82
83 static void restore_current_flags(struct pt_regs *regs, unsigned long flags)
84 {
85         regs->flags &= ~IF_MASK;
86         regs->flags |= flags & IF_MASK;
87 }
88
89 /**
90  * @brief Prepares uprobe for x86.
91  *
92  * @param up Pointer to the uprobe.
93  * @return 0 on success,\n
94  * -1 on error.
95  */
96 int arch_prepare_uprobe(struct uprobe *up)
97 {
98         struct kprobe *p = up2kp(up);
99         struct task_struct *task = up->task;
100         u8 *tramp = up->atramp.tramp;
101         enum { call_relative_opcode = 0xe8 };
102
103         if (!read_proc_vm_atomic(task, (unsigned long)p->addr,
104                                  tramp, MAX_INSN_SIZE)) {
105                 printk(KERN_ERR "failed to read memory %p!\n", p->addr);
106                 return -EINVAL;
107         }
108         /* TODO: this is a workaround */
109         if (tramp[0] == call_relative_opcode) {
110                 printk(KERN_INFO "cannot install probe: 1st instruction is call\n");
111                 return -EINVAL;
112         }
113
114         tramp[UPROBES_TRAMP_RET_BREAK_IDX] = BREAKPOINT_INSTRUCTION;
115
116         /* TODO: remove dual info */
117         p->opcode = tramp[0];
118
119         p->ainsn.boostable = swap_can_boost(tramp) ? 0 : -1;
120
121         p->ainsn.insn = swap_slot_alloc(up->sm);
122         if (p->ainsn.insn == NULL) {
123                 printk(KERN_ERR "trampoline out of memory\n");
124                 return -ENOMEM;
125         }
126
127         if (!write_proc_vm_atomic(task, (unsigned long)p->ainsn.insn,
128                                   tramp, sizeof(up->atramp.tramp))) {
129                 swap_slot_free(up->sm, p->ainsn.insn);
130                 printk(KERN_INFO "failed to write memory %p!\n", tramp);
131                 return -EINVAL;
132         }
133
134         /* for uretprobe */
135         add_uprobe_table(p);
136
137         return 0;
138 }
139
140 /**
141  * @brief Jump pre-handler.
142  *
143  * @param p Pointer to the uprobe's kprobe.
144  * @param regs Pointer to CPU register data.
145  * @return 0.
146  */
147 int setjmp_upre_handler(struct kprobe *p, struct pt_regs *regs)
148 {
149         struct uprobe *up = container_of(p, struct uprobe, kp);
150         struct ujprobe *jp = container_of(up, struct ujprobe, up);
151         kprobe_pre_entry_handler_t pre_entry =
152                 (kprobe_pre_entry_handler_t)jp->pre_entry;
153         entry_point_t entry = (entry_point_t)jp->entry;
154         unsigned long args[6];
155
156         /* FIXME some user space apps crash if we clean interrupt bit */
157         /* regs->EREG(flags) &= ~IF_MASK; */
158 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
159         trace_hardirqs_off();
160 #endif
161
162         /* read first 6 args from stack */
163         if (!read_proc_vm_atomic(current, regs->EREG(sp) + 4,
164                                  args, sizeof(args)))
165                 printk(KERN_WARNING
166                        "failed to read user space func arguments %lx!\n",
167                        regs->sp + 4);
168
169         if (pre_entry)
170                 p->ss_addr[smp_processor_id()] = (kprobe_opcode_t *)
171                                                  pre_entry(jp->priv_arg, regs);
172
173         if (entry)
174                 entry(args[0], args[1], args[2], args[3], args[4], args[5]);
175         else
176                 arch_ujprobe_return();
177
178         return 0;
179 }
180
181 /**
182  * @brief Prepares uretprobe for x86.
183  *
184  * @param ri Pointer to the uretprobe instance.
185  * @param regs Pointer to CPU register data.
186  * @return Void.
187  */
188 int arch_prepare_uretprobe(struct uretprobe_instance *ri, struct pt_regs *regs)
189 {
190         /* Replace the return addr with trampoline addr */
191         unsigned long ra = trampoline_addr(&ri->rp->up);
192         unsigned long ret_addr;
193         ri->sp = (kprobe_opcode_t *)regs->sp;
194
195         if (get_user(ret_addr, (unsigned long *)regs->sp)) {
196                 pr_err("failed to read user space func ra %lx addr=%p!\n",
197                        regs->sp, ri->rp->up.kp.addr);
198                 return -EINVAL;
199         }
200
201         if (put_user(ra, (unsigned long *)regs->sp)) {
202                 pr_err("failed to write user space func ra %lx!\n", regs->sp);
203                 return -EINVAL;
204         }
205
206         ri->ret_addr = (kprobe_opcode_t *)ret_addr;
207
208         return 0;
209 }
210
211 /**
212  * @brief Disarms uretprobe on x86 arch.
213  *
214  * @param ri Pointer to the uretprobe instance.
215  * @param task Pointer to the task for which the probe.
216  * @return 0 on success,\n
217  * negative error code on error.
218  */
219 int arch_disarm_urp_inst(struct uretprobe_instance *ri,
220                          struct task_struct *task, unsigned long tr)
221 {
222         unsigned long ret_addr;
223         unsigned long sp = (unsigned long)ri->sp;
224         unsigned long tramp_addr;
225
226         if (tr == 0)
227                 tramp_addr = arch_tramp_by_ri(ri);
228         else
229                 tramp_addr = tr; /* ri - invalid */
230
231         if (get_user(ret_addr, (unsigned long *)sp)) {
232                 printk(KERN_INFO "---> %s (%d/%d): failed to read stack from %08lx\n",
233                        task->comm, task->tgid, task->pid, sp);
234                 return -EFAULT;
235         }
236
237         if (tramp_addr == ret_addr) {
238                 if (put_user((unsigned long)ri->ret_addr, (unsigned long *)sp)) {
239                         printk(KERN_INFO "---> %s (%d/%d): failed to write "
240                                "orig_ret_addr to %08lx",
241                                task->comm, task->tgid, task->pid, sp);
242                         return -EFAULT;
243                 }
244         } else {
245                 printk(KERN_INFO "---> %s (%d/%d): trampoline NOT found at sp = %08lx\n",
246                        task->comm, task->tgid, task->pid, sp);
247                 return -ENOENT;
248         }
249
250         return 0;
251 }
252
253 /**
254  * @brief Gets trampoline address.
255  *
256  * @param p Pointer to the uprobe's kprobe.
257  * @param regs Pointer to CPU register data.
258  * @return Trampoline address.
259  */
260 unsigned long arch_get_trampoline_addr(struct kprobe *p, struct pt_regs *regs)
261 {
262         return trampoline_addr(kp2up(p));
263 }
264
265 /**
266  * @brief Restores return address.
267  *
268  * @param orig_ret_addr Original return address.
269  * @param regs Pointer to CPU register data.
270  * @return Void.
271  */
272 void arch_set_orig_ret_addr(unsigned long orig_ret_addr, struct pt_regs *regs)
273 {
274         regs->EREG(ip) = orig_ret_addr;
275 }
276
277 /**
278  * @brief Removes uprobe.
279  *
280  * @param up Pointer to the target uprobe.
281  * @return Void.
282  */
283 void arch_remove_uprobe(struct uprobe *up)
284 {
285         struct kprobe *p = up2kp(up);
286
287         swap_slot_free(up->sm, p->ainsn.insn);
288 }
289
290 int arch_arm_uprobe(struct uprobe *p)
291 {
292         int ret;
293         kprobe_opcode_t insn = BREAKPOINT_INSTRUCTION;
294         unsigned long vaddr = (unsigned long)p->kp.addr;
295
296         ret = write_proc_vm_atomic(p->task, vaddr, &insn, sizeof(insn));
297         if (!ret) {
298                 pr_err("arch_arm_uprobe: failed to write memory tgid=%u vaddr=%08lx\n",
299                        p->task->tgid, vaddr);
300
301                 return -EACCES;
302         }
303
304         return 0;
305 }
306
307 void arch_disarm_uprobe(struct kprobe *p, struct task_struct *task)
308 {
309         int ret;
310         unsigned long vaddr = (unsigned long)p->addr;
311
312         ret = write_proc_vm_atomic(task, vaddr, &p->opcode, sizeof(p->opcode));
313         if (!ret) {
314                 pr_err("arch_disarm_uprobe: failed to write memory tgid=%u, vaddr=%08lx\n",
315                        task->tgid, vaddr);
316         }
317 }
318
319 static void set_user_jmp_op(void *from, void *to)
320 {
321         struct __arch_jmp_op {
322                 char op;
323                 long raddr;
324         } __packed jop;
325
326         jop.raddr = (long)(to) - ((long)(from) + 5);
327         jop.op = RELATIVEJUMP_INSTRUCTION;
328
329         if (put_user(jop.op, (char *)from) ||
330             put_user(jop.raddr, (long *)(from + 1)))
331                 pr_err("failed to write jump opcode to user space %p\n", from);
332 }
333
334 static void resume_execution(struct kprobe *p,
335                              struct pt_regs *regs,
336                              unsigned long flags)
337 {
338         unsigned long *tos, tos_dword = 0;
339         unsigned long copy_eip = (unsigned long)p->ainsn.insn;
340         unsigned long orig_eip = (unsigned long)p->addr;
341         kprobe_opcode_t insns[2];
342
343         regs->EREG(flags) &= ~TF_MASK;
344
345         tos = (unsigned long *)&tos_dword;
346         if (get_user(tos_dword, (unsigned long *)regs->sp)) {
347                 pr_err("failed to read from user space sp=%lx!\n", regs->sp);
348                 return;
349         }
350
351         if (get_user(*(unsigned short *)insns, (unsigned short *)p->ainsn.insn)) {
352                 pr_err("failed to read first 2 opcodes %p!\n", p->ainsn.insn);
353                 return;
354         }
355
356         switch (insns[0]) {
357         case 0x9c: /* pushfl */
358                 *tos &= ~(TF_MASK | IF_MASK);
359                 *tos |= flags & (TF_MASK | IF_MASK);
360                 break;
361         case 0xc2: /* iret/ret/lret */
362         case 0xc3:
363         case 0xca:
364         case 0xcb:
365         case 0xcf:
366         case 0xea: /* jmp absolute -- eip is correct */
367                 /* eip is already adjusted, no more changes required */
368                 p->ainsn.boostable = 1;
369                 goto no_change;
370         case 0xe8: /* call relative - Fix return addr */
371                 *tos = orig_eip + (*tos - copy_eip);
372                 break;
373         case 0x9a: /* call absolute -- same as call absolute, indirect */
374                 *tos = orig_eip + (*tos - copy_eip);
375
376                 if (put_user(tos_dword, (unsigned long *)regs->sp)) {
377                         pr_err("failed to write dword to sp=%lx\n", regs->sp);
378                         return;
379                 }
380
381                 goto no_change;
382         case 0xff:
383                 if ((insns[1] & 0x30) == 0x10) {
384                         /*
385                          * call absolute, indirect
386                          * Fix return addr; eip is correct.
387                          * But this is not boostable
388                          */
389                         *tos = orig_eip + (*tos - copy_eip);
390
391                         if (put_user(tos_dword, (unsigned long *)regs->sp)) {
392                                 pr_err("failed to write dword to sp=%lx\n", regs->sp);
393                                 return;
394                         }
395
396                         goto no_change;
397                 } else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute
398                                                            * indirect */
399                            ((insns[1] & 0x31) == 0x21)) {
400                         /* jmp far, absolute indirect */
401                         /* eip is correct. And this is boostable */
402                         p->ainsn.boostable = 1;
403                         goto no_change;
404                 }
405         case 0xf3:
406                 if (insns[1] == 0xc3)
407                         /* repz ret special handling: no more changes */
408                         goto no_change;
409                 break;
410         default:
411                 break;
412         }
413
414         if (put_user(tos_dword, (unsigned long *)regs->sp)) {
415                 pr_err("failed to write dword to sp=%lx\n", regs->sp);
416                 return;
417         }
418
419         if (p->ainsn.boostable == 0) {
420                 if ((regs->EREG(ip) > copy_eip) && (regs->EREG(ip) - copy_eip) +
421                     5 < MAX_INSN_SIZE) {
422                         /*
423                          * These instructions can be executed directly if it
424                          * jumps back to correct address.
425                          */
426                         set_user_jmp_op((void *) regs->EREG(ip),
427                                         (void *)orig_eip +
428                                         (regs->EREG(ip) - copy_eip));
429                         p->ainsn.boostable = 1;
430                 } else {
431                         p->ainsn.boostable = -1;
432                 }
433         }
434
435         regs->EREG(ip) = orig_eip + (regs->EREG(ip) - copy_eip);
436
437 no_change:
438         return;
439 }
440
441 static bool prepare_ss_addr(struct kprobe *p, struct pt_regs *regs)
442 {
443         unsigned long *ss_addr = (long *)&p->ss_addr[smp_processor_id()];
444
445         if (*ss_addr) {
446                 regs->ip = *ss_addr;
447                 *ss_addr = 0;
448                 return true;
449         } else {
450                 regs->ip = (unsigned long)p->ainsn.insn;
451                 return false;
452         }
453 }
454
455 static void prepare_ss(struct pt_regs *regs)
456 {
457         /* set single step mode */
458         regs->flags |= TF_MASK;
459         regs->flags &= ~IF_MASK;
460 }
461
462 static int uprobe_handler(struct pt_regs *regs)
463 {
464         struct kprobe *p;
465         kprobe_opcode_t *addr;
466         struct task_struct *task = current;
467         pid_t tgid = task->tgid;
468
469         save_current_flags(regs);
470
471         addr = (kprobe_opcode_t *)(regs->EREG(ip) - sizeof(kprobe_opcode_t));
472         p = get_ukprobe(addr, tgid);
473
474         if (p == NULL) {
475                 void *tramp_addr = (void *)addr - UPROBES_TRAMP_RET_BREAK_IDX;
476
477                 p = get_ukprobe_by_insn_slot(tramp_addr, tgid, regs);
478                 if (p == NULL) {
479                         printk(KERN_INFO "no_uprobe\n");
480                         return 0;
481                 }
482
483                 trampoline_uprobe_handler(p, regs);
484                 return 1;
485         } else {
486                 if (!p->pre_handler || !p->pre_handler(p, regs)) {
487                         if (p->ainsn.boostable == 1 && !p->post_handler) {
488                                 prepare_ss_addr(p, regs);
489                                 return 1;
490                         }
491
492                         if (prepare_ss_addr(p, regs) == false) {
493                                 set_current_probe(p);
494                                 prepare_ss(regs);
495                         }
496                 }
497         }
498
499         return 1;
500 }
501
502 static int post_uprobe_handler(struct pt_regs *regs)
503 {
504         struct kprobe *p = get_current_probe();
505         unsigned long flags = current_ucb()->flags;
506
507         if (p == NULL) {
508                 printk("task[%u %u %s] current uprobe is not found\n",
509                        current->tgid, current->pid, current->comm);
510                 return 0;
511         }
512
513         resume_execution(p, regs, flags);
514         restore_current_flags(regs, flags);
515
516         /* clean stack */
517         current_ucb()->p = 0;
518         current_ucb()->flags = 0;
519
520         return 1;
521 }
522
523 static int uprobe_exceptions_notify(struct notifier_block *self,
524                                     unsigned long val, void *data)
525 {
526         struct die_args *args = (struct die_args *)data;
527         int ret = NOTIFY_DONE;
528
529         if (args->regs == NULL || !user_mode_vm(args->regs))
530                 return ret;
531
532         switch (val) {
533 #ifdef CONFIG_KPROBES
534         case DIE_INT3:
535 #else
536         case DIE_TRAP:
537 #endif
538                 if (uprobe_handler(args->regs))
539                         ret = NOTIFY_STOP;
540                 break;
541         case DIE_DEBUG:
542                 if (post_uprobe_handler(args->regs))
543                         ret = NOTIFY_STOP;
544                 break;
545         default:
546                 break;
547         }
548
549         return ret;
550 }
551
552 static struct notifier_block uprobe_exceptions_nb = {
553         .notifier_call = uprobe_exceptions_notify,
554         .priority = INT_MAX
555 };
556
557 /**
558  * @brief Registers notify.
559  *
560  * @return register_die_notifier result.
561  */
562 int swap_arch_init_uprobes(void)
563 {
564         return register_die_notifier(&uprobe_exceptions_nb);
565 }
566
567 /**
568  * @brief Unregisters notify.
569  *
570  * @return Void.
571  */
572 void swap_arch_exit_uprobes(void)
573 {
574         unregister_die_notifier(&uprobe_exceptions_nb);
575 }
576