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