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 <uprobe/swap_uprobes.h>
39 #include "swap_uprobes.h"
43 * @struct uprobe_ctlblk
44 * @brief Uprobe control block
46 struct uprobe_ctlblk {
47 unsigned long flags; /**< Flags */
48 struct kprobe *p; /**< Pointer to the uprobe's kprobe */
51 static unsigned long trampoline_addr(struct uprobe *up)
53 return (unsigned long)(up->kp.ainsn.insn +
54 UPROBES_TRAMP_RET_BREAK_IDX);
57 static DEFINE_PER_CPU(struct uprobe_ctlblk, ucb) = { 0, NULL };
59 static struct kprobe *get_current_probe(void)
61 return __get_cpu_var(ucb).p;
64 static void set_current_probe(struct kprobe *p)
66 __get_cpu_var(ucb).p = p;
69 static void reset_current_probe(void)
71 set_current_probe(NULL);
74 static void save_current_flags(struct pt_regs *regs)
76 __get_cpu_var(ucb).flags = regs->EREG(flags);
79 static void restore_current_flags(struct pt_regs *regs)
81 regs->EREG(flags) &= ~IF_MASK;
82 regs->EREG(flags) |= __get_cpu_var(ucb).flags & IF_MASK;
86 * @brief Prepares uprobe for x86.
88 * @param up Pointer to the uprobe.
89 * @return 0 on success,\n
92 int arch_prepare_uprobe(struct uprobe *up)
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 };
99 if (!read_proc_vm_atomic(task, (unsigned long)p->addr,
100 tramp, MAX_INSN_SIZE))
101 panic("failed to read memory %p!\n", p->addr);
102 /* TODO: this is a workaround */
103 if (tramp[0] == call_relative_opcode) {
104 printk(KERN_INFO "cannot install probe: 1st instruction is call\n");
108 tramp[UPROBES_TRAMP_RET_BREAK_IDX] = BREAKPOINT_INSTRUCTION;
110 /* TODO: remove dual info */
111 p->opcode = tramp[0];
113 p->ainsn.boostable = swap_can_boost(tramp) ? 0 : -1;
115 p->ainsn.insn = swap_slot_alloc(up->sm);
116 if (p->ainsn.insn == NULL) {
117 printk(KERN_INFO "trampoline out of memory\n");
121 if (!write_proc_vm_atomic(task, (unsigned long)p->ainsn.insn,
122 tramp, sizeof(up->atramp.tramp))) {
123 swap_slot_free(up->sm, p->ainsn.insn);
124 printk("failed to write memory %p!\n", tramp);
132 * @brief Jump pre-handler.
134 * @param p Pointer to the uprobe's kprobe.
135 * @param regs Pointer to CPU register data.
138 int setjmp_upre_handler(struct kprobe *p, struct pt_regs *regs)
140 struct uprobe *up = container_of(p, struct uprobe, kp);
141 struct ujprobe *jp = container_of(up, struct ujprobe, up);
142 kprobe_pre_entry_handler_t pre_entry =
143 (kprobe_pre_entry_handler_t)jp->pre_entry;
144 entry_point_t entry = (entry_point_t)jp->entry;
145 unsigned long args[6];
147 /* FIXME some user space apps crash if we clean interrupt bit */
148 /* regs->EREG(flags) &= ~IF_MASK; */
149 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
150 trace_hardirqs_off();
153 /* read first 6 args from stack */
154 if (!read_proc_vm_atomic(current, regs->EREG(sp) + 4,
156 panic("failed to read user space func arguments %lx!\n",
160 p->ss_addr[smp_processor_id()] = (kprobe_opcode_t *)
161 pre_entry(jp->priv_arg, regs);
164 entry(args[0], args[1], args[2], args[3], args[4], args[5]);
166 arch_ujprobe_return();
172 * @brief Prepares uretprobe for x86.
174 * @param ri Pointer to the uretprobe instance.
175 * @param regs Pointer to CPU register data.
178 void arch_prepare_uretprobe(struct uretprobe_instance *ri, struct pt_regs *regs)
180 /* Replace the return addr with trampoline addr */
181 unsigned long ra = trampoline_addr(&ri->rp->up);
182 ri->sp = (kprobe_opcode_t *)regs->sp;
184 if (!read_proc_vm_atomic(current, regs->EREG(sp), &(ri->ret_addr),
185 sizeof(ri->ret_addr)))
186 panic("failed to read user space func ra %lx!\n",
189 if (!write_proc_vm_atomic(current, regs->EREG(sp), &ra, sizeof(ra)))
190 panic("failed to write user space func ra %lx!\n",
193 add_uprobe_table(&ri->rp->up.kp);
197 * @brief Disarms uretprobe on x86 arch.
199 * @param ri Pointer to the uretprobe instance.
200 * @param task Pointer to the task for which the probe.
201 * @return 0 on success,\n
202 * negative error code on error.
204 int arch_disarm_urp_inst(struct uretprobe_instance *ri,
205 struct task_struct *task)
208 unsigned long ret_addr;
209 unsigned long sp = (unsigned long)ri->sp;
210 unsigned long tramp_addr = trampoline_addr(&ri->rp->up);
211 len = read_proc_vm_atomic(task, sp, &ret_addr, sizeof(ret_addr));
212 if (len != sizeof(ret_addr)) {
213 printk(KERN_INFO "---> %s (%d/%d): failed to read stack from %08lx\n",
214 task->comm, task->tgid, task->pid, sp);
218 if (tramp_addr == ret_addr) {
219 len = write_proc_vm_atomic(task, sp, &ri->ret_addr,
220 sizeof(ri->ret_addr));
221 if (len != sizeof(ri->ret_addr)) {
222 printk(KERN_INFO "---> %s (%d/%d): failed to write "
223 "orig_ret_addr to %08lx",
224 task->comm, task->tgid, task->pid, sp);
228 printk(KERN_INFO "---> %s (%d/%d): trampoline NOT found at sp = %08lx\n",
229 task->comm, task->tgid, task->pid, sp);
237 * @brief Gets trampoline address.
239 * @param p Pointer to the uprobe's kprobe.
240 * @param regs Pointer to CPU register data.
241 * @return Trampoline address.
243 unsigned long arch_get_trampoline_addr(struct kprobe *p, struct pt_regs *regs)
245 return trampoline_addr(kp2up(p));
249 * @brief Restores return address.
251 * @param orig_ret_addr Original return address.
252 * @param regs Pointer to CPU register data.
255 void arch_set_orig_ret_addr(unsigned long orig_ret_addr, struct pt_regs *regs)
257 regs->EREG(ip) = orig_ret_addr;
261 * @brief Removes uprobe.
263 * @param up Pointer to the target uprobe.
266 void arch_remove_uprobe(struct uprobe *up)
268 struct kprobe *p = up2kp(up);
270 swap_slot_free(up->sm, p->ainsn.insn);
273 static void set_user_jmp_op(void *from, void *to)
275 struct __arch_jmp_op {
280 jop.raddr = (long)(to) - ((long)(from) + 5);
281 jop.op = RELATIVEJUMP_INSTRUCTION;
283 if (!write_proc_vm_atomic(current, (unsigned long)from, &jop,
285 panic("failed to write jump opcode to user space %p!\n", from);
288 static void resume_execution(struct kprobe *p,
289 struct pt_regs *regs,
292 unsigned long *tos, tos_dword = 0;
293 unsigned long copy_eip = (unsigned long)p->ainsn.insn;
294 unsigned long orig_eip = (unsigned long)p->addr;
295 kprobe_opcode_t insns[2];
297 regs->EREG(flags) &= ~TF_MASK;
299 tos = (unsigned long *)&tos_dword;
300 if (!read_proc_vm_atomic(current, regs->EREG(sp), &tos_dword,
302 panic("failed to read dword from top of the user space stack "
303 "%lx!\n", regs->EREG(sp));
305 if (!read_proc_vm_atomic(current, (unsigned long)p->ainsn.insn, insns,
306 2 * sizeof(kprobe_opcode_t)))
307 panic("failed to read first 2 opcodes of instruction copy "
308 "from user space %p!\n", p->ainsn.insn);
311 case 0x9c: /* pushfl */
312 *tos &= ~(TF_MASK | IF_MASK);
313 *tos |= flags & (TF_MASK | IF_MASK);
315 case 0xc2: /* iret/ret/lret */
320 case 0xea: /* jmp absolute -- eip is correct */
321 /* eip is already adjusted, no more changes required */
322 p->ainsn.boostable = 1;
324 case 0xe8: /* call relative - Fix return addr */
325 *tos = orig_eip + (*tos - copy_eip);
327 case 0x9a: /* call absolute -- same as call absolute, indirect */
328 *tos = orig_eip + (*tos - copy_eip);
330 if (!write_proc_vm_atomic(current,
334 panic("failed to write dword to top of the"
335 " user space stack %lx!\n",
340 if ((insns[1] & 0x30) == 0x10) {
342 * call absolute, indirect
343 * Fix return addr; eip is correct.
344 * But this is not boostable
346 *tos = orig_eip + (*tos - copy_eip);
348 if (!write_proc_vm_atomic(current, regs->EREG(sp),
351 panic("failed to write dword to top of the "
352 "user space stack %lx!\n",
356 } else if (((insns[1] & 0x31) == 0x20) || /* jmp near, absolute
358 ((insns[1] & 0x31) == 0x21)) {
359 /* jmp far, absolute indirect */
360 /* eip is correct. And this is boostable */
361 p->ainsn.boostable = 1;
365 if (insns[1] == 0xc3)
366 /* repz ret special handling: no more changes */
373 if (!write_proc_vm_atomic(current, regs->EREG(sp), &tos_dword,
375 panic("failed to write dword to top of the user space stack "
376 "%lx!\n", regs->EREG(sp));
378 if (p->ainsn.boostable == 0) {
379 if ((regs->EREG(ip) > copy_eip) && (regs->EREG(ip) - copy_eip) +
382 * These instructions can be executed directly if it
383 * jumps back to correct address.
385 set_user_jmp_op((void *) regs->EREG(ip),
387 (regs->EREG(ip) - copy_eip));
388 p->ainsn.boostable = 1;
390 p->ainsn.boostable = -1;
394 regs->EREG(ip) = orig_eip + (regs->EREG(ip) - copy_eip);
400 static int uprobe_handler(struct pt_regs *regs)
403 kprobe_opcode_t *addr;
404 struct task_struct *task = current;
405 pid_t tgid = task->tgid;
407 save_current_flags(regs);
409 addr = (kprobe_opcode_t *)(regs->EREG(ip) - sizeof(kprobe_opcode_t));
410 p = get_ukprobe(addr, tgid);
413 void *tramp_addr = (void *)addr - UPROBES_TRAMP_RET_BREAK_IDX;
415 p = get_ukprobe_by_insn_slot(tramp_addr, tgid, regs);
417 printk(KERN_INFO "no_uprobe\n");
421 trampoline_uprobe_handler(p, regs);
424 if (!p->pre_handler || !p->pre_handler(p, regs)) {
426 if (p->ainsn.boostable == 1 && !p->post_handler) {
427 if (p->ss_addr[smp_processor_id()]) {
428 regs->EREG(ip) = (unsigned long)p->ss_addr[smp_processor_id()];
429 p->ss_addr[smp_processor_id()] = NULL;
431 regs->EREG(ip) = (unsigned long)p->ainsn.insn;
436 prepare_singlestep(p, regs);
440 set_current_probe(p);
445 static int post_uprobe_handler(struct pt_regs *regs)
447 struct kprobe *p = get_current_probe();
448 unsigned long flags = __get_cpu_var(ucb).flags;
453 resume_execution(p, regs, flags);
454 restore_current_flags(regs);
456 reset_current_probe();
461 static int uprobe_exceptions_notify(struct notifier_block *self,
462 unsigned long val, void *data)
464 struct die_args *args = (struct die_args *)data;
465 int ret = NOTIFY_DONE;
467 if (args->regs == NULL || !user_mode_vm(args->regs))
471 #ifdef CONFIG_KPROBES
476 if (uprobe_handler(args->regs))
480 if (post_uprobe_handler(args->regs))
490 static struct notifier_block uprobe_exceptions_nb = {
491 .notifier_call = uprobe_exceptions_notify,
496 * @brief Registers notify.
498 * @return register_die_notifier result.
500 int swap_arch_init_uprobes(void)
502 return register_die_notifier(&uprobe_exceptions_nb);
506 * @brief Unregisters notify.
510 void swap_arch_exit_uprobes(void)
512 unregister_die_notifier(&uprobe_exceptions_nb);