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
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.
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.
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.
26 * Copyright (C) Samsung Electronics, 2006-2010
28 * @section DESCRIPTION
30 * Arch-dependent uprobe interface implementation for x86.
34 #include <linux/kdebug.h>
36 #include <kprobe/swap_slots.h>
37 #include <kprobe/swap_td_raw.h>
38 #include <uprobe/swap_uprobes.h>
40 #include "swap_uprobes.h"
44 struct pt_regs save_regs;
45 struct pt_regs *ptr_regs;
47 int (*handler)(struct uprobe *, struct pt_regs *);
51 * @struct uprobe_ctlblk
52 * @brief Uprobe control block
54 struct uprobe_ctlblk {
55 unsigned long flags; /**< Flags */
56 struct uprobe *p; /**< Pointer to the uprobe */
58 struct save_context ctx;
62 static struct td_raw td_raw;
65 static unsigned long trampoline_addr(struct uprobe *up)
67 return (unsigned long)(up->ainsn.insn +
68 UPROBES_TRAMP_RET_BREAK_IDX);
71 unsigned long arch_tramp_by_ri(struct uretprobe_instance *ri)
73 return trampoline_addr(&ri->rp->up);
76 static struct uprobe_ctlblk *current_ucb(void)
78 return (struct uprobe_ctlblk *)swap_td_raw(&td_raw, current);
81 static struct save_context *current_ctx(void)
83 return ¤t_ucb()->ctx;
86 static struct uprobe *get_current_probe(void)
88 return current_ucb()->p;
91 static void set_current_probe(struct uprobe *p)
96 static void save_current_flags(struct pt_regs *regs)
98 current_ucb()->flags = regs->flags;
101 static void restore_current_flags(struct pt_regs *regs, unsigned long flags)
103 regs->flags &= ~IF_MASK;
104 regs->flags |= flags & IF_MASK;
108 * @brief Prepares uprobe for x86.
110 * @param up Pointer to the uprobe.
111 * @return 0 on success,\n
114 int arch_prepare_uprobe(struct uprobe *p)
116 struct task_struct *task = p->task;
117 u8 tramp[UPROBES_TRAMP_LEN + BP_INSN_SIZE]; /* BP for uretprobe */
118 enum { call_relative_opcode = 0xe8 };
120 if (!read_proc_vm_atomic(task, (unsigned long)p->addr,
121 tramp, MAX_INSN_SIZE)) {
122 printk(KERN_ERR "failed to read memory %p!\n", p->addr);
125 /* TODO: this is a workaround */
126 if (tramp[0] == call_relative_opcode) {
127 printk(KERN_INFO "cannot install probe: 1st instruction is call\n");
131 tramp[UPROBES_TRAMP_RET_BREAK_IDX] = BREAKPOINT_INSTRUCTION;
133 p->opcode = tramp[0];
134 p->ainsn.boostable = swap_can_boost(tramp) ? 0 : -1;
136 p->ainsn.insn = swap_slot_alloc(p->sm);
137 if (p->ainsn.insn == NULL) {
138 printk(KERN_ERR "trampoline out of memory\n");
142 if (!write_proc_vm_atomic(task, (unsigned long)p->ainsn.insn,
143 tramp, sizeof(tramp))) {
144 swap_slot_free(p->sm, p->ainsn.insn);
145 printk(KERN_INFO "failed to write memory %p!\n", tramp);
156 * @brief Jump pre-handler.
158 * @param p Pointer to the uprobe.
159 * @param regs Pointer to CPU register data.
162 int setjmp_upre_handler(struct uprobe *p, struct pt_regs *regs)
164 struct ujprobe *jp = container_of(p, struct ujprobe, up);
165 entry_point_t entry = (entry_point_t)jp->entry;
166 unsigned long args[6];
168 /* FIXME some user space apps crash if we clean interrupt bit */
169 /* regs->EREG(flags) &= ~IF_MASK; */
170 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
171 trace_hardirqs_off();
174 /* read first 6 args from stack */
175 if (!read_proc_vm_atomic(current, regs->EREG(sp) + 4,
178 "failed to read user space func arguments %lx!\n",
182 entry(args[0], args[1], args[2], args[3], args[4], args[5]);
184 arch_ujprobe_return();
190 * @brief Prepares uretprobe for x86.
192 * @param ri Pointer to the uretprobe instance.
193 * @param regs Pointer to CPU register data.
196 int arch_prepare_uretprobe(struct uretprobe_instance *ri, struct pt_regs *regs)
198 /* Replace the return addr with trampoline addr */
199 unsigned long ra = trampoline_addr(&ri->rp->up);
200 unsigned long ret_addr;
201 ri->sp = (kprobe_opcode_t *)regs->sp;
203 if (get_user(ret_addr, (unsigned long *)regs->sp)) {
204 pr_err("failed to read user space func ra %lx addr=%p!\n",
205 regs->sp, ri->rp->up.addr);
209 if (put_user(ra, (unsigned long *)regs->sp)) {
210 pr_err("failed to write user space func ra %lx!\n", regs->sp);
214 ri->ret_addr = (uprobe_opcode_t *)ret_addr;
219 static bool get_long(struct task_struct *task,
220 unsigned long vaddr, unsigned long *val)
222 return sizeof(*val) != read_proc_vm_atomic(task, vaddr,
226 static bool put_long(struct task_struct *task,
227 unsigned long vaddr, unsigned long *val)
229 return sizeof(*val) != write_proc_vm_atomic(task, vaddr,
234 * @brief Disarms uretprobe on x86 arch.
236 * @param ri Pointer to the uretprobe instance.
237 * @param task Pointer to the task for which the probe.
238 * @return 0 on success,\n
239 * negative error code on error.
241 int arch_disarm_urp_inst(struct uretprobe_instance *ri,
242 struct task_struct *task, unsigned long tr)
244 unsigned long ret_addr;
245 unsigned long sp = (unsigned long)ri->sp;
246 unsigned long tramp_addr;
249 tramp_addr = arch_tramp_by_ri(ri);
251 tramp_addr = tr; /* ri - invalid */
253 if (get_long(task, sp, &ret_addr)) {
254 printk(KERN_INFO "---> %s (%d/%d): failed to read stack from %08lx\n",
255 task->comm, task->tgid, task->pid, sp);
259 if (tramp_addr == ret_addr) {
260 if (put_long(task, sp, (unsigned long *)&ri->ret_addr)) {
261 printk(KERN_INFO "---> %s (%d/%d): failed to write "
262 "orig_ret_addr to %08lx",
263 task->comm, task->tgid, task->pid, sp);
267 printk(KERN_INFO "---> %s (%d/%d): trampoline NOT found at sp = %08lx\n",
268 task->comm, task->tgid, task->pid, sp);
276 * @brief Gets trampoline address.
278 * @param p Pointer to the uprobe.
279 * @param regs Pointer to CPU register data.
280 * @return Trampoline address.
282 unsigned long arch_get_trampoline_addr(struct uprobe *p, struct pt_regs *regs)
284 return trampoline_addr(p);
288 * @brief Restores return address.
290 * @param orig_ret_addr Original return address.
291 * @param regs Pointer to CPU register data.
294 void arch_set_orig_ret_addr(unsigned long orig_ret_addr, struct pt_regs *regs)
296 regs->EREG(ip) = orig_ret_addr;
300 * @brief Removes uprobe.
302 * @param up Pointer to the target uprobe.
305 void arch_remove_uprobe(struct uprobe *p)
307 swap_slot_free(p->sm, p->ainsn.insn);
310 int arch_arm_uprobe(struct uprobe *p)
313 uprobe_opcode_t insn = BREAKPOINT_INSTRUCTION;
314 unsigned long vaddr = (unsigned long)p->addr;
316 ret = write_proc_vm_atomic(p->task, vaddr, &insn, sizeof(insn));
318 pr_err("arch_arm_uprobe: failed to write memory tgid=%u vaddr=%08lx\n",
319 p->task->tgid, vaddr);
327 void arch_disarm_uprobe(struct uprobe *p, struct task_struct *task)
330 unsigned long vaddr = (unsigned long)p->addr;
332 ret = write_proc_vm_atomic(task, vaddr, &p->opcode, sizeof(p->opcode));
334 pr_err("arch_disarm_uprobe: failed to write memory tgid=%u, vaddr=%08lx\n",
339 static void set_user_jmp_op(void *from, void *to)
341 struct __arch_jmp_op {
346 jop.raddr = (long)(to) - ((long)(from) + 5);
347 jop.op = RELATIVEJUMP_INSTRUCTION;
349 if (put_user(jop.op, (char *)from) ||
350 put_user(jop.raddr, (long *)(from + 1)))
351 pr_err("failed to write jump opcode to user space %p\n", from);
354 static void resume_execution(struct uprobe *p,
355 struct pt_regs *regs,
358 unsigned long *tos, tos_dword = 0;
359 unsigned long copy_eip = (unsigned long)p->ainsn.insn;
360 unsigned long orig_eip = (unsigned long)p->addr;
361 uprobe_opcode_t insns[2];
363 regs->EREG(flags) &= ~TF_MASK;
365 tos = (unsigned long *)&tos_dword;
366 if (get_user(tos_dword, (unsigned long *)regs->sp)) {
367 pr_err("failed to read from user space sp=%lx!\n", regs->sp);
371 if (get_user(*(unsigned short *)insns, (unsigned short *)p->ainsn.insn)) {
372 pr_err("failed to read first 2 opcodes %p!\n", p->ainsn.insn);
377 case 0x9c: /* pushfl */
378 *tos &= ~(TF_MASK | IF_MASK);
379 *tos |= flags & (TF_MASK | IF_MASK);
381 case 0xc2: /* iret/ret/lret */
386 case 0xea: /* jmp absolute -- eip is correct */
387 /* eip is already adjusted, no more changes required */
388 p->ainsn.boostable = 1;
390 case 0xe8: /* call relative - Fix return addr */
391 *tos = orig_eip + (*tos - copy_eip);
393 case 0x9a: /* call absolute -- same as call absolute, indirect */
394 *tos = orig_eip + (*tos - copy_eip);
396 if (put_user(tos_dword, (unsigned long *)regs->sp)) {
397 pr_err("failed to write dword to sp=%lx\n", regs->sp);
403 if ((insns[1] & 0x30) == 0x10) {
405 * call absolute, indirect
406 * Fix return addr; eip is correct.
407 * But this is not boostable
409 *tos = orig_eip + (*tos - copy_eip);
411 if (put_user(tos_dword, (unsigned long *)regs->sp)) {
412 pr_err("failed to write dword to sp=%lx\n", regs->sp);
417 } else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute
419 ((insns[1] & 0x31) == 0x21)) {
420 /* jmp far, absolute indirect */
421 /* eip is correct. And this is boostable */
422 p->ainsn.boostable = 1;
426 if (insns[1] == 0xc3)
427 /* repz ret special handling: no more changes */
434 if (put_user(tos_dword, (unsigned long *)regs->sp)) {
435 pr_err("failed to write dword to sp=%lx\n", regs->sp);
439 if (p->ainsn.boostable == 0) {
440 if ((regs->EREG(ip) > copy_eip) && (regs->EREG(ip) - copy_eip) +
443 * These instructions can be executed directly if it
444 * jumps back to correct address.
446 set_user_jmp_op((void *) regs->EREG(ip),
448 (regs->EREG(ip) - copy_eip));
449 p->ainsn.boostable = 1;
451 p->ainsn.boostable = -1;
455 regs->EREG(ip) = orig_eip + (regs->EREG(ip) - copy_eip);
461 static void prepare_tramp(struct uprobe *p, struct pt_regs *regs)
463 regs->ip = (unsigned long)p->ainsn.insn;
466 static void prepare_ss(struct pt_regs *regs)
468 /* set single step mode */
469 regs->flags |= TF_MASK;
470 regs->flags &= ~IF_MASK;
474 static unsigned long resume_userspace_addr;
476 static void __used __up_handler(void)
478 struct pt_regs *regs = current_ctx()->ptr_regs;
479 struct thread_info *tinfo = current_thread_info();
480 struct uprobe *p = current_ucb()->p;
482 /* restore KS regs */
483 *regs = current_ctx()->save_regs;
486 current_ctx()->handler(p, regs);
488 /* resume_userspace */
494 : "r" (regs), "r" (tinfo) , "r" (resume_userspace_addr)
498 void up_handler(void);
501 /* skip hex tractor-driver bytes to make some free space (skip regs) */
506 static int exceptions_handler(struct pt_regs *regs,
507 int (*handler)(struct uprobe *, struct pt_regs *))
510 current_ctx()->save_regs = *regs;
511 current_ctx()->ptr_regs = regs;
514 current_ctx()->handler = handler;
516 /* setup regs to return to KS */
517 regs->ip = (unsigned long)up_handler;
518 regs->ds = __USER_DS;
519 regs->es = __USER_DS;
520 regs->fs = __KERNEL_PERCPU;
521 regs->cs = __KERNEL_CS | get_kernel_rpl();
523 regs->flags = X86_EFLAGS_IF | X86_EFLAGS_FIXED;
528 static int uprobe_handler_part2(struct uprobe *p, struct pt_regs *regs)
530 if (!p->pre_handler(p, regs)) {
531 prepare_tramp(p, regs);
532 if (p->ainsn.boostable == 1 && !p->post_handler)
535 save_current_flags(regs);
536 set_current_probe(p);
543 static int uprobe_handler_atomic(struct pt_regs *regs)
545 pid_t tgid = current->tgid;
546 unsigned long vaddr = regs->ip - 1;
547 struct uprobe *p = get_uprobe((void *)vaddr, tgid);
550 if (p->pre_handler) {
551 set_current_probe(p);
552 exceptions_handler(regs, uprobe_handler_part2);
554 uprobe_handler_part2(p, regs);
557 unsigned long tramp_vaddr;
559 tramp_vaddr = vaddr - UPROBES_TRAMP_RET_BREAK_IDX;
560 p = get_uprobe_by_insn_slot((void *)tramp_vaddr, tgid, regs);
562 pr_info("no_uprobe\n");
566 set_current_probe(p);
567 exceptions_handler(regs, trampoline_uprobe_handler);
573 static int post_uprobe_handler(struct uprobe *p, struct pt_regs *regs)
575 unsigned long flags = current_ucb()->flags;
577 resume_execution(p, regs, flags);
578 restore_current_flags(regs, flags);
580 /* reset current probe */
581 set_current_probe(NULL);
586 static int post_uprobe_handler_atomic(struct pt_regs *regs)
588 struct uprobe *p = get_current_probe();
591 exceptions_handler(regs, post_uprobe_handler);
593 pr_info("task[%u %u %s] current uprobe is not found\n",
594 current->tgid, current->pid, current->comm);
600 static int uprobe_exceptions_notify(struct notifier_block *self,
601 unsigned long val, void *data)
603 struct die_args *args = (struct die_args *)data;
604 int ret = NOTIFY_DONE;
606 if (args->regs == NULL || !user_mode_vm(args->regs))
610 #ifdef CONFIG_KPROBES
615 if (uprobe_handler_atomic(args->regs))
619 if (post_uprobe_handler_atomic(args->regs))
629 static struct notifier_block uprobe_exceptions_nb = {
630 .notifier_call = uprobe_exceptions_notify,
635 * @brief Registers notify.
637 * @return register_die_notifier result.
639 int swap_arch_init_uprobes(void)
643 resume_userspace_addr = swap_ksyms("resume_userspace");
644 if (resume_userspace_addr == 0) {
645 pr_err("symbol 'resume_userspace' not found\n");
649 ret = swap_td_raw_reg(&td_raw, sizeof(struct uprobe_ctlblk));
653 ret = register_die_notifier(&uprobe_exceptions_nb);
655 swap_td_raw_unreg(&td_raw);
661 * @brief Unregisters notify.
665 void swap_arch_exit_uprobes(void)
667 unregister_die_notifier(&uprobe_exceptions_nb);
668 swap_td_raw_unreg(&td_raw);