blob: 137bf612141b38b8808586677c4ae1c0ca427341 [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;
206 kprobe_opcode_t *addr = NULL;
207 unsigned long *lp;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800208 struct kprobe_ctlblk *kcb;
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800209#ifdef CONFIG_PREEMPT
210 unsigned pre_preempt_count = preempt_count();
211#endif /* CONFIG_PREEMPT */
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800212
213 /*
214 * We don't want to be preempted for the entire
215 * duration of kprobe processing
216 */
217 preempt_disable();
218 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /* Check if the application is using LDT entry for its code segment and
221 * calculate the address by reading the base address from the LDT entry.
222 */
223 if ((regs->xcs & 4) && (current->mm)) {
224 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
225 + (char *) current->mm->context.ldt);
226 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
227 sizeof(kprobe_opcode_t));
228 } else {
229 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
230 }
231 /* Check we're not actually recursing */
232 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 p = get_kprobe(addr);
234 if (p) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800235 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700236 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 regs->eflags &= ~TF_MASK;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800238 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 goto no_kprobe;
240 }
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700241 /* We have reentered the kprobe_handler(), since
242 * another probe was hit while within the handler.
243 * We here save the original kprobes variables and
244 * just single step on the instruction of the new probe
245 * without calling any user handlers.
246 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800247 save_previous_kprobe(kcb);
248 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800249 kprobes_inc_nmissed_count(p);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700250 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800251 kcb->kprobe_status = KPROBE_REENTER;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700252 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800254 if (regs->eflags & VM_MASK) {
255 /* We are in virtual-8086 mode. Return 0 */
256 goto no_kprobe;
257 }
258 if (*addr != BREAKPOINT_INSTRUCTION) {
259 /* The breakpoint instruction was removed by
260 * another cpu right after we hit, no further
261 * handling of this interrupt is appropriate
262 */
263 regs->eip -= sizeof(kprobe_opcode_t);
264 ret = 1;
265 goto no_kprobe;
266 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800267 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (p->break_handler && p->break_handler(p, regs)) {
269 goto ss_probe;
270 }
271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 goto no_kprobe;
273 }
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 p = get_kprobe(addr);
276 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (regs->eflags & VM_MASK) {
278 /* We are in virtual-8086 mode. Return 0 */
279 goto no_kprobe;
280 }
281
282 if (*addr != BREAKPOINT_INSTRUCTION) {
283 /*
284 * The breakpoint instruction was removed right
285 * after we hit it. Another cpu has removed
286 * either a probepoint or a debugger breakpoint
287 * at this address. In either case, no further
288 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700289 * Back up over the (now missing) int3 and run
290 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
Jim Kenistonbce06492005-09-06 15:19:34 -0700292 regs->eip -= sizeof(kprobe_opcode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 ret = 1;
294 }
295 /* Not one of ours: let kernel handle it */
296 goto no_kprobe;
297 }
298
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800299 set_current_kprobe(p, regs, kcb);
300 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (p->pre_handler && p->pre_handler(p, regs))
303 /* handler has already set things up, so skip ss setup */
304 return 1;
305
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800306 if (p->ainsn.boostable == 1 &&
307#ifdef CONFIG_PREEMPT
308 !(pre_preempt_count) && /*
309 * This enables booster when the direct
310 * execution path aren't preempted.
311 */
312#endif /* CONFIG_PREEMPT */
313 !p->post_handler && !p->break_handler ) {
314 /* Boost up -- we can execute copied instructions directly */
315 reset_current_kprobe();
316 regs->eip = (unsigned long)p->ainsn.insn;
317 preempt_enable_no_resched();
318 return 1;
319 }
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321ss_probe:
322 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800323 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return 1;
325
326no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800327 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return ret;
329}
330
331/*
Hien Nguyenb94cce92005-06-23 00:09:19 -0700332 * For function-return probes, init_kprobes() establishes a probepoint
333 * here. When a retprobed function returns, this probe is hit and
334 * trampoline_probe_handler() runs, calling the kretprobe's handler.
335 */
336 void kretprobe_trampoline_holder(void)
337 {
338 asm volatile ( ".global kretprobe_trampoline\n"
339 "kretprobe_trampoline: \n"
340 "nop\n");
341 }
342
343/*
344 * Called when we hit the probe point at kretprobe_trampoline
345 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700346int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700347{
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700348 struct kretprobe_instance *ri = NULL;
349 struct hlist_head *head;
350 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800351 unsigned long flags, orig_ret_address = 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700352 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700353
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800354 spin_lock_irqsave(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700355 head = kretprobe_inst_table_head(current);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700356
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700357 /*
358 * It is possible to have multiple instances associated with a given
359 * task either because an multiple functions in the call path
360 * have a return probe installed on them, and/or more then one return
361 * return probe was registered for a target function.
362 *
363 * We can handle this because:
364 * - instances are always inserted at the head of the list
365 * - when multiple return probes are registered for the same
366 * function, the first instance's ret_addr will point to the
367 * real return address, and all the rest will point to
368 * kretprobe_trampoline
369 */
370 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
371 if (ri->task != current)
372 /* another task is sharing our hash bucket */
373 continue;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700374
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700375 if (ri->rp && ri->rp->handler)
376 ri->rp->handler(ri, regs);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700377
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700378 orig_ret_address = (unsigned long)ri->ret_addr;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700379 recycle_rp_inst(ri);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700380
381 if (orig_ret_address != trampoline_address)
382 /*
383 * This is the real return address. Any other
384 * instances associated with this task are for
385 * other calls deeper on the call stack
386 */
387 break;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700388 }
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700389
390 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
391 regs->eip = orig_ret_address;
392
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800393 reset_current_kprobe();
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800394 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700395 preempt_enable_no_resched();
396
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800397 /*
398 * By returning a non-zero value, we are telling
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800399 * kprobe_handler() that we don't want the post_handler
400 * to run (and have re-enabled preemption)
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800401 */
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700402 return 1;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700403}
404
405/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * Called after single-stepping. p->addr is the address of the
407 * instruction whose first byte has been replaced by the "int 3"
408 * instruction. To avoid the SMP problems that can occur when we
409 * temporarily put back the original opcode to single-step, we
410 * single-stepped a copy of the instruction. The address of this
411 * copy is p->ainsn.insn.
412 *
413 * This function prepares to return from the post-single-step
414 * interrupt. We have to fix up the stack as follows:
415 *
416 * 0) Except in the case of absolute or indirect jump or call instructions,
417 * the new eip is relative to the copied instruction. We need to make
418 * it relative to the original instruction.
419 *
420 * 1) If the single-stepped instruction was pushfl, then the TF and IF
421 * flags are set in the just-pushed eflags, and may need to be cleared.
422 *
423 * 2) If the single-stepped instruction was a call, the return address
424 * that is atop the stack is the address following the copied instruction.
425 * We need to make it the address following the original instruction.
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800426 *
427 * This function also checks instruction size for preparing direct execution.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800429static void __kprobes resume_execution(struct kprobe *p,
430 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 unsigned long *tos = (unsigned long *)&regs->esp;
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800433 unsigned long copy_eip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 unsigned long orig_eip = (unsigned long)p->addr;
435
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800436 regs->eflags &= ~TF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 switch (p->ainsn.insn[0]) {
438 case 0x9c: /* pushfl */
439 *tos &= ~(TF_MASK | IF_MASK);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800440 *tos |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 break;
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700442 case 0xc3: /* ret/lret */
443 case 0xcb:
444 case 0xc2:
445 case 0xca:
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800446 case 0xea: /* jmp absolute -- eip is correct */
447 /* eip is already adjusted, no more changes required */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800448 p->ainsn.boostable = 1;
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800449 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 case 0xe8: /* call relative - Fix return addr */
451 *tos = orig_eip + (*tos - copy_eip);
452 break;
453 case 0xff:
454 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
455 /* call absolute, indirect */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800456 /*
457 * Fix return addr; eip is correct.
458 * But this is not boostable
459 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *tos = orig_eip + (*tos - copy_eip);
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800461 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
463 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800464 /* eip is correct. And this is boostable */
465 p->ainsn.boostable = 1;
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800466 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 default:
469 break;
470 }
471
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800472 if (p->ainsn.boostable == 0) {
473 if ((regs->eip > copy_eip) &&
474 (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
475 /*
476 * These instructions can be executed directly if it
477 * jumps back to correct address.
478 */
479 set_jmp_op((void *)regs->eip,
480 (void *)orig_eip + (regs->eip - copy_eip));
481 p->ainsn.boostable = 1;
482 } else {
483 p->ainsn.boostable = -1;
484 }
485 }
486
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800487 regs->eip = orig_eip + (regs->eip - copy_eip);
488
489no_change:
490 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/*
494 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800495 * remain disabled thoroughout this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 */
497static inline int post_kprobe_handler(struct pt_regs *regs)
498{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800499 struct kprobe *cur = kprobe_running();
500 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
501
502 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return 0;
504
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800505 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
506 kcb->kprobe_status = KPROBE_HIT_SSDONE;
507 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800510 resume_execution(cur, regs, kcb);
511 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700513 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800514 if (kcb->kprobe_status == KPROBE_REENTER) {
515 restore_previous_kprobe(kcb);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700516 goto out;
517 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800518 reset_current_kprobe();
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700519out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 preempt_enable_no_resched();
521
522 /*
523 * if somebody else is singlestepping across a probe point, eflags
524 * will have TF set, in which case, continue the remaining processing
525 * of do_debug, as if this is not a probe hit.
526 */
527 if (regs->eflags & TF_MASK)
528 return 0;
529
530 return 1;
531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
534{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800535 struct kprobe *cur = kprobe_running();
536 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
537
538 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return 1;
540
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800541 if (kcb->kprobe_status & KPROBE_HIT_SS) {
542 resume_execution(cur, regs, kcb);
543 regs->eflags |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800545 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 preempt_enable_no_resched();
547 }
548 return 0;
549}
550
551/*
552 * Wrapper routine to for handling exceptions.
553 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700554int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
555 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800558 int ret = NOTIFY_DONE;
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 switch (val) {
561 case DIE_INT3:
562 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800563 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565 case DIE_DEBUG:
566 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800567 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 break;
569 case DIE_GPF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800571 /* kprobe_running() needs smp_processor_id() */
572 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (kprobe_running() &&
574 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800575 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800576 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 break;
578 default:
579 break;
580 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800581 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700584int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
586 struct jprobe *jp = container_of(p, struct jprobe, kp);
587 unsigned long addr;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800588 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800590 kcb->jprobe_saved_regs = *regs;
591 kcb->jprobe_saved_esp = &regs->esp;
592 addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /*
595 * TBD: As Linus pointed out, gcc assumes that the callee
596 * owns the argument space and could overwrite it, e.g.
597 * tailcall optimization. So, to be absolutely safe
598 * we also save and restore enough stack bytes to cover
599 * the argument area.
600 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800601 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
602 MIN_STACK_SIZE(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 regs->eflags &= ~IF_MASK;
604 regs->eip = (unsigned long)(jp->entry);
605 return 1;
606}
607
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700608void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800610 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 asm volatile (" xchgl %%ebx,%%esp \n"
613 " int3 \n"
614 " .globl jprobe_return_end \n"
615 " jprobe_return_end: \n"
616 " nop \n"::"b"
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800617 (kcb->jprobe_saved_esp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700620int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800622 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 u8 *addr = (u8 *) (regs->eip - 1);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800624 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct jprobe *jp = container_of(p, struct jprobe, kp);
626
627 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800628 if (&regs->esp != kcb->jprobe_saved_esp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct pt_regs *saved_regs =
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800630 container_of(kcb->jprobe_saved_esp,
631 struct pt_regs, esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 printk("current esp %p does not match saved esp %p\n",
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800633 &regs->esp, kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 printk("Saved registers for jprobe %p\n", jp);
635 show_registers(saved_regs);
636 printk("Current registers\n");
637 show_registers(regs);
638 BUG();
639 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800640 *regs = kcb->jprobe_saved_regs;
641 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 MIN_STACK_SIZE(stack_addr));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800643 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 1;
645 }
646 return 0;
647}
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700648
649static struct kprobe trampoline_p = {
650 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
651 .pre_handler = trampoline_probe_handler
652};
653
Rusty Lynch67729262005-07-05 18:54:50 -0700654int __init arch_init_kprobes(void)
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700655{
656 return register_kprobe(&trampoline_p);
657}