blob: df1b346d36ffe3c8a4604401b905f8df63b90a25 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Kernel Probes (KProbes)
3 * arch/i386/kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation ( includes contributions from
23 * Rusty Russell).
24 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
25 * interface to access function arguments.
Hien Nguyenb94cce92005-06-23 00:09:19 -070026 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
27 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
28 * <prasanna@in.ibm.com> added function-return probes.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30
31#include <linux/config.h>
32#include <linux/kprobes.h>
33#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/preempt.h>
Rusty Lynch7e1048b2005-06-23 00:09:25 -070035#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm/kdebug.h>
37#include <asm/desc.h>
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039void jprobe_return_end(void);
40
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080041DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
42DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
43
Masami Hiramatsu311ac882006-03-26 01:38:17 -080044/* insert a jmp code */
45static inline void set_jmp_op(void *from, void *to)
46{
47 struct __arch_jmp_op {
48 char op;
49 long raddr;
50 } __attribute__((packed)) *jop;
51 jop = (struct __arch_jmp_op *)from;
52 jop->raddr = (long)(to) - ((long)(from) + 5);
53 jop->op = RELATIVEJUMP_INSTRUCTION;
54}
55
56/*
57 * returns non-zero if opcodes can be boosted.
58 */
59static inline int can_boost(kprobe_opcode_t opcode)
60{
61 switch (opcode & 0xf0 ) {
62 case 0x70:
63 return 0; /* can't boost conditional jump */
64 case 0x90:
65 /* can't boost call and pushf */
66 return opcode != 0x9a && opcode != 0x9c;
67 case 0xc0:
68 /* can't boost undefined opcodes and soft-interruptions */
69 return (0xc1 < opcode && opcode < 0xc6) ||
70 (0xc7 < opcode && opcode < 0xcc) || opcode == 0xcf;
71 case 0xd0:
72 /* can boost AA* and XLAT */
73 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
74 case 0xe0:
75 /* can boost in/out and (may be) jmps */
76 return (0xe3 < opcode && opcode != 0xe8);
77 case 0xf0:
78 /* clear and set flags can be boost */
79 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
80 default:
81 /* currently, can't boost 2 bytes opcodes */
82 return opcode != 0x0f;
83 }
84}
85
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/*
88 * returns non-zero if opcode modifies the interrupt flag.
89 */
90static inline int is_IF_modifier(kprobe_opcode_t opcode)
91{
92 switch (opcode) {
93 case 0xfa: /* cli */
94 case 0xfb: /* sti */
95 case 0xcf: /* iret/iretd */
96 case 0x9d: /* popf/popfd */
97 return 1;
98 }
99 return 0;
100}
101
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700102int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103{
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800104 /* insn: must be on special executable page on i386. */
105 p->ainsn.insn = get_insn_slot();
106 if (!p->ainsn.insn)
107 return -ENOMEM;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700110 p->opcode = *p->addr;
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800111 if (can_boost(p->opcode)) {
112 p->ainsn.boostable = 0;
113 } else {
114 p->ainsn.boostable = -1;
115 }
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800116 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700117}
118
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700119void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700120{
121 *p->addr = BREAKPOINT_INSTRUCTION;
122 flush_icache_range((unsigned long) p->addr,
123 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
124}
125
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700126void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700127{
128 *p->addr = p->opcode;
129 flush_icache_range((unsigned long) p->addr,
130 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800133void __kprobes arch_remove_kprobe(struct kprobe *p)
134{
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800135 mutex_lock(&kprobe_mutex);
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800136 free_insn_slot(p->ainsn.insn);
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800137 mutex_unlock(&kprobe_mutex);
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800138}
139
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800140static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700141{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800142 kcb->prev_kprobe.kp = kprobe_running();
143 kcb->prev_kprobe.status = kcb->kprobe_status;
144 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
145 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700146}
147
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800148static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700149{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800150 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
151 kcb->kprobe_status = kcb->prev_kprobe.status;
152 kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
153 kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700154}
155
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800156static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
157 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700158{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800159 __get_cpu_var(current_kprobe) = p;
160 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700161 = (regs->eflags & (TF_MASK | IF_MASK));
162 if (is_IF_modifier(p->opcode))
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800163 kcb->kprobe_saved_eflags &= ~IF_MASK;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
167{
168 regs->eflags |= TF_MASK;
169 regs->eflags &= ~IF_MASK;
170 /*single step inline if the instruction is an int3*/
171 if (p->opcode == BREAKPOINT_INSTRUCTION)
172 regs->eip = (unsigned long)p->addr;
173 else
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800174 regs->eip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800177/* Called with kretprobe_lock held */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700178void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
179 struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700180{
181 unsigned long *sara = (unsigned long *)&regs->esp;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700182 struct kretprobe_instance *ri;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700183
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700184 if ((ri = get_free_rp_inst(rp)) != NULL) {
185 ri->rp = rp;
186 ri->task = current;
187 ri->ret_addr = (kprobe_opcode_t *) *sara;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700188
Hien Nguyenb94cce92005-06-23 00:09:19 -0700189 /* Replace the return addr with trampoline addr */
190 *sara = (unsigned long) &kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700191
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700192 add_rp_inst(ri);
193 } else {
194 rp->nmissed++;
195 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/*
199 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
200 * remain disabled thorough out this function.
201 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700202static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 struct kprobe *p;
205 int ret = 0;
bibo,mao2326c772006-03-26 01:38:21 -0800206 kprobe_opcode_t *addr;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800207 struct kprobe_ctlblk *kcb;
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800208#ifdef CONFIG_PREEMPT
209 unsigned pre_preempt_count = preempt_count();
210#endif /* CONFIG_PREEMPT */
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800211
bibo,mao2326c772006-03-26 01:38:21 -0800212 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
213
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800214 /*
215 * We don't want to be preempted for the entire
216 * duration of kprobe processing
217 */
218 preempt_disable();
219 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 /* Check we're not actually recursing */
222 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 p = get_kprobe(addr);
224 if (p) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800225 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700226 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 regs->eflags &= ~TF_MASK;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800228 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 goto no_kprobe;
230 }
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700231 /* We have reentered the kprobe_handler(), since
232 * another probe was hit while within the handler.
233 * We here save the original kprobes variables and
234 * just single step on the instruction of the new probe
235 * without calling any user handlers.
236 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800237 save_previous_kprobe(kcb);
238 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800239 kprobes_inc_nmissed_count(p);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700240 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800241 kcb->kprobe_status = KPROBE_REENTER;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700242 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800244 if (regs->eflags & VM_MASK) {
245 /* We are in virtual-8086 mode. Return 0 */
246 goto no_kprobe;
247 }
248 if (*addr != BREAKPOINT_INSTRUCTION) {
249 /* The breakpoint instruction was removed by
250 * another cpu right after we hit, no further
251 * handling of this interrupt is appropriate
252 */
253 regs->eip -= sizeof(kprobe_opcode_t);
254 ret = 1;
255 goto no_kprobe;
256 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800257 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (p->break_handler && p->break_handler(p, regs)) {
259 goto ss_probe;
260 }
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto no_kprobe;
263 }
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 p = get_kprobe(addr);
266 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (regs->eflags & VM_MASK) {
268 /* We are in virtual-8086 mode. Return 0 */
269 goto no_kprobe;
270 }
271
272 if (*addr != BREAKPOINT_INSTRUCTION) {
273 /*
274 * The breakpoint instruction was removed right
275 * after we hit it. Another cpu has removed
276 * either a probepoint or a debugger breakpoint
277 * at this address. In either case, no further
278 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700279 * Back up over the (now missing) int3 and run
280 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Jim Kenistonbce06492005-09-06 15:19:34 -0700282 regs->eip -= sizeof(kprobe_opcode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 ret = 1;
284 }
285 /* Not one of ours: let kernel handle it */
286 goto no_kprobe;
287 }
288
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800289 set_current_kprobe(p, regs, kcb);
290 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 if (p->pre_handler && p->pre_handler(p, regs))
293 /* handler has already set things up, so skip ss setup */
294 return 1;
295
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800296 if (p->ainsn.boostable == 1 &&
297#ifdef CONFIG_PREEMPT
298 !(pre_preempt_count) && /*
299 * This enables booster when the direct
300 * execution path aren't preempted.
301 */
302#endif /* CONFIG_PREEMPT */
303 !p->post_handler && !p->break_handler ) {
304 /* Boost up -- we can execute copied instructions directly */
305 reset_current_kprobe();
306 regs->eip = (unsigned long)p->ainsn.insn;
307 preempt_enable_no_resched();
308 return 1;
309 }
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311ss_probe:
312 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800313 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 1;
315
316no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800317 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return ret;
319}
320
321/*
Hien Nguyenb94cce92005-06-23 00:09:19 -0700322 * For function-return probes, init_kprobes() establishes a probepoint
323 * here. When a retprobed function returns, this probe is hit and
324 * trampoline_probe_handler() runs, calling the kretprobe's handler.
325 */
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800326 void __kprobes kretprobe_trampoline_holder(void)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700327 {
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800328 asm volatile ( ".global kretprobe_trampoline\n"
Hien Nguyenb94cce92005-06-23 00:09:19 -0700329 "kretprobe_trampoline: \n"
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800330 " pushf\n"
331 /* skip cs, eip, orig_eax, es, ds */
332 " subl $20, %esp\n"
333 " pushl %eax\n"
334 " pushl %ebp\n"
335 " pushl %edi\n"
336 " pushl %esi\n"
337 " pushl %edx\n"
338 " pushl %ecx\n"
339 " pushl %ebx\n"
340 " movl %esp, %eax\n"
341 " call trampoline_handler\n"
342 /* move eflags to cs */
343 " movl 48(%esp), %edx\n"
344 " movl %edx, 44(%esp)\n"
345 /* save true return address on eflags */
346 " movl %eax, 48(%esp)\n"
347 " popl %ebx\n"
348 " popl %ecx\n"
349 " popl %edx\n"
350 " popl %esi\n"
351 " popl %edi\n"
352 " popl %ebp\n"
353 " popl %eax\n"
354 /* skip eip, orig_eax, es, ds */
355 " addl $16, %esp\n"
356 " popf\n"
357 " ret\n");
358}
Hien Nguyenb94cce92005-06-23 00:09:19 -0700359
360/*
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800361 * Called from kretprobe_trampoline
Hien Nguyenb94cce92005-06-23 00:09:19 -0700362 */
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800363fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700364{
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700365 struct kretprobe_instance *ri = NULL;
366 struct hlist_head *head;
367 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800368 unsigned long flags, orig_ret_address = 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700369 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700370
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800371 spin_lock_irqsave(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700372 head = kretprobe_inst_table_head(current);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700373
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700374 /*
375 * It is possible to have multiple instances associated with a given
376 * task either because an multiple functions in the call path
377 * have a return probe installed on them, and/or more then one return
378 * return probe was registered for a target function.
379 *
380 * We can handle this because:
381 * - instances are always inserted at the head of the list
382 * - when multiple return probes are registered for the same
383 * function, the first instance's ret_addr will point to the
384 * real return address, and all the rest will point to
385 * kretprobe_trampoline
386 */
387 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
388 if (ri->task != current)
389 /* another task is sharing our hash bucket */
390 continue;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700391
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800392 if (ri->rp && ri->rp->handler){
393 __get_cpu_var(current_kprobe) = &ri->rp->kp;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700394 ri->rp->handler(ri, regs);
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800395 __get_cpu_var(current_kprobe) = NULL;
396 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700397
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700398 orig_ret_address = (unsigned long)ri->ret_addr;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700399 recycle_rp_inst(ri);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700400
401 if (orig_ret_address != trampoline_address)
402 /*
403 * This is the real return address. Any other
404 * instances associated with this task are for
405 * other calls deeper on the call stack
406 */
407 break;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700408 }
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700409
410 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700411
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800412 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700413
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800414 return (void*)orig_ret_address;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700415}
416
417/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * Called after single-stepping. p->addr is the address of the
419 * instruction whose first byte has been replaced by the "int 3"
420 * instruction. To avoid the SMP problems that can occur when we
421 * temporarily put back the original opcode to single-step, we
422 * single-stepped a copy of the instruction. The address of this
423 * copy is p->ainsn.insn.
424 *
425 * This function prepares to return from the post-single-step
426 * interrupt. We have to fix up the stack as follows:
427 *
428 * 0) Except in the case of absolute or indirect jump or call instructions,
429 * the new eip is relative to the copied instruction. We need to make
430 * it relative to the original instruction.
431 *
432 * 1) If the single-stepped instruction was pushfl, then the TF and IF
433 * flags are set in the just-pushed eflags, and may need to be cleared.
434 *
435 * 2) If the single-stepped instruction was a call, the return address
436 * that is atop the stack is the address following the copied instruction.
437 * We need to make it the address following the original instruction.
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800438 *
439 * This function also checks instruction size for preparing direct execution.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800441static void __kprobes resume_execution(struct kprobe *p,
442 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 unsigned long *tos = (unsigned long *)&regs->esp;
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800445 unsigned long copy_eip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 unsigned long orig_eip = (unsigned long)p->addr;
447
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800448 regs->eflags &= ~TF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 switch (p->ainsn.insn[0]) {
450 case 0x9c: /* pushfl */
451 *tos &= ~(TF_MASK | IF_MASK);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800452 *tos |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700454 case 0xc3: /* ret/lret */
455 case 0xcb:
456 case 0xc2:
457 case 0xca:
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800458 case 0xea: /* jmp absolute -- eip is correct */
459 /* eip is already adjusted, no more changes required */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800460 p->ainsn.boostable = 1;
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800461 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 case 0xe8: /* call relative - Fix return addr */
463 *tos = orig_eip + (*tos - copy_eip);
464 break;
465 case 0xff:
466 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
467 /* call absolute, indirect */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800468 /*
469 * Fix return addr; eip is correct.
470 * But this is not boostable
471 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 *tos = orig_eip + (*tos - copy_eip);
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800473 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
475 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800476 /* eip is correct. And this is boostable */
477 p->ainsn.boostable = 1;
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800478 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 default:
481 break;
482 }
483
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800484 if (p->ainsn.boostable == 0) {
485 if ((regs->eip > copy_eip) &&
486 (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
487 /*
488 * These instructions can be executed directly if it
489 * jumps back to correct address.
490 */
491 set_jmp_op((void *)regs->eip,
492 (void *)orig_eip + (regs->eip - copy_eip));
493 p->ainsn.boostable = 1;
494 } else {
495 p->ainsn.boostable = -1;
496 }
497 }
498
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800499 regs->eip = orig_eip + (regs->eip - copy_eip);
500
501no_change:
502 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/*
506 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800507 * remain disabled thoroughout this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 */
509static inline int post_kprobe_handler(struct pt_regs *regs)
510{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800511 struct kprobe *cur = kprobe_running();
512 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
513
514 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
516
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800517 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
518 kcb->kprobe_status = KPROBE_HIT_SSDONE;
519 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800522 resume_execution(cur, regs, kcb);
523 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700525 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800526 if (kcb->kprobe_status == KPROBE_REENTER) {
527 restore_previous_kprobe(kcb);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700528 goto out;
529 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800530 reset_current_kprobe();
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700531out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 preempt_enable_no_resched();
533
534 /*
535 * if somebody else is singlestepping across a probe point, eflags
536 * will have TF set, in which case, continue the remaining processing
537 * of do_debug, as if this is not a probe hit.
538 */
539 if (regs->eflags & TF_MASK)
540 return 0;
541
542 return 1;
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
546{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800547 struct kprobe *cur = kprobe_running();
548 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
549
550 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return 1;
552
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800553 if (kcb->kprobe_status & KPROBE_HIT_SS) {
554 resume_execution(cur, regs, kcb);
555 regs->eflags |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800557 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 preempt_enable_no_resched();
559 }
560 return 0;
561}
562
563/*
564 * Wrapper routine to for handling exceptions.
565 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700566int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
567 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800570 int ret = NOTIFY_DONE;
571
bibo,mao2326c772006-03-26 01:38:21 -0800572 if (args->regs && user_mode(args->regs))
573 return ret;
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 switch (val) {
576 case DIE_INT3:
577 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800578 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 break;
580 case DIE_DEBUG:
581 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800582 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
584 case DIE_GPF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800586 /* kprobe_running() needs smp_processor_id() */
587 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (kprobe_running() &&
589 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800590 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800591 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
593 default:
594 break;
595 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700599int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct jprobe *jp = container_of(p, struct jprobe, kp);
602 unsigned long addr;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800603 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800605 kcb->jprobe_saved_regs = *regs;
606 kcb->jprobe_saved_esp = &regs->esp;
607 addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /*
610 * TBD: As Linus pointed out, gcc assumes that the callee
611 * owns the argument space and could overwrite it, e.g.
612 * tailcall optimization. So, to be absolutely safe
613 * we also save and restore enough stack bytes to cover
614 * the argument area.
615 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800616 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
617 MIN_STACK_SIZE(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 regs->eflags &= ~IF_MASK;
619 regs->eip = (unsigned long)(jp->entry);
620 return 1;
621}
622
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700623void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800625 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 asm volatile (" xchgl %%ebx,%%esp \n"
628 " int3 \n"
629 " .globl jprobe_return_end \n"
630 " jprobe_return_end: \n"
631 " nop \n"::"b"
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800632 (kcb->jprobe_saved_esp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700635int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800637 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 u8 *addr = (u8 *) (regs->eip - 1);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800639 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct jprobe *jp = container_of(p, struct jprobe, kp);
641
642 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800643 if (&regs->esp != kcb->jprobe_saved_esp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct pt_regs *saved_regs =
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800645 container_of(kcb->jprobe_saved_esp,
646 struct pt_regs, esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 printk("current esp %p does not match saved esp %p\n",
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800648 &regs->esp, kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 printk("Saved registers for jprobe %p\n", jp);
650 show_registers(saved_regs);
651 printk("Current registers\n");
652 show_registers(regs);
653 BUG();
654 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800655 *regs = kcb->jprobe_saved_regs;
656 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 MIN_STACK_SIZE(stack_addr));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800658 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 1;
660 }
661 return 0;
662}
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700663
Rusty Lynch67729262005-07-05 18:54:50 -0700664int __init arch_init_kprobes(void)
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700665{
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800666 return 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700667}