blob: 6483eeb1a4e809f3fef395af0dd151528520c9a4 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * returns non-zero if opcode modifies the interrupt flag.
46 */
47static inline int is_IF_modifier(kprobe_opcode_t opcode)
48{
49 switch (opcode) {
50 case 0xfa: /* cli */
51 case 0xfb: /* sti */
52 case 0xcf: /* iret/iretd */
53 case 0x9d: /* popf/popfd */
54 return 1;
55 }
56 return 0;
57}
58
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070059int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Rusty Lynch7e1048b2005-06-23 00:09:25 -070062 p->opcode = *p->addr;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -080063 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -070064}
65
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070066void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070067{
68 *p->addr = BREAKPOINT_INSTRUCTION;
69 flush_icache_range((unsigned long) p->addr,
70 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
71}
72
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070073void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070074{
75 *p->addr = p->opcode;
76 flush_icache_range((unsigned long) p->addr,
77 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080080static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070081{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080082 kcb->prev_kprobe.kp = kprobe_running();
83 kcb->prev_kprobe.status = kcb->kprobe_status;
84 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
85 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070086}
87
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080088static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070089{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080090 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
91 kcb->kprobe_status = kcb->prev_kprobe.status;
92 kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
93 kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070094}
95
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080096static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
97 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070098{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080099 __get_cpu_var(current_kprobe) = p;
100 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700101 = (regs->eflags & (TF_MASK | IF_MASK));
102 if (is_IF_modifier(p->opcode))
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800103 kcb->kprobe_saved_eflags &= ~IF_MASK;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
107{
108 regs->eflags |= TF_MASK;
109 regs->eflags &= ~IF_MASK;
110 /*single step inline if the instruction is an int3*/
111 if (p->opcode == BREAKPOINT_INSTRUCTION)
112 regs->eip = (unsigned long)p->addr;
113 else
114 regs->eip = (unsigned long)&p->ainsn.insn;
115}
116
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800117/* Called with kretprobe_lock held */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700118void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
119 struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700120{
121 unsigned long *sara = (unsigned long *)&regs->esp;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700122 struct kretprobe_instance *ri;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700123
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700124 if ((ri = get_free_rp_inst(rp)) != NULL) {
125 ri->rp = rp;
126 ri->task = current;
127 ri->ret_addr = (kprobe_opcode_t *) *sara;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700128
Hien Nguyenb94cce92005-06-23 00:09:19 -0700129 /* Replace the return addr with trampoline addr */
130 *sara = (unsigned long) &kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700131
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700132 add_rp_inst(ri);
133 } else {
134 rp->nmissed++;
135 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
140 * remain disabled thorough out this function.
141 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700142static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct kprobe *p;
145 int ret = 0;
146 kprobe_opcode_t *addr = NULL;
147 unsigned long *lp;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800148 struct kprobe_ctlblk *kcb;
149
150 /*
151 * We don't want to be preempted for the entire
152 * duration of kprobe processing
153 */
154 preempt_disable();
155 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 /* Check if the application is using LDT entry for its code segment and
158 * calculate the address by reading the base address from the LDT entry.
159 */
160 if ((regs->xcs & 4) && (current->mm)) {
161 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
162 + (char *) current->mm->context.ldt);
163 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
164 sizeof(kprobe_opcode_t));
165 } else {
166 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
167 }
168 /* Check we're not actually recursing */
169 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 p = get_kprobe(addr);
171 if (p) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800172 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700173 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 regs->eflags &= ~TF_MASK;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800175 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 goto no_kprobe;
177 }
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700178 /* We have reentered the kprobe_handler(), since
179 * another probe was hit while within the handler.
180 * We here save the original kprobes variables and
181 * just single step on the instruction of the new probe
182 * without calling any user handlers.
183 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800184 save_previous_kprobe(kcb);
185 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800186 kprobes_inc_nmissed_count(p);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700187 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800188 kcb->kprobe_status = KPROBE_REENTER;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700189 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800191 if (regs->eflags & VM_MASK) {
192 /* We are in virtual-8086 mode. Return 0 */
193 goto no_kprobe;
194 }
195 if (*addr != BREAKPOINT_INSTRUCTION) {
196 /* The breakpoint instruction was removed by
197 * another cpu right after we hit, no further
198 * handling of this interrupt is appropriate
199 */
200 regs->eip -= sizeof(kprobe_opcode_t);
201 ret = 1;
202 goto no_kprobe;
203 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800204 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (p->break_handler && p->break_handler(p, regs)) {
206 goto ss_probe;
207 }
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto no_kprobe;
210 }
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 p = get_kprobe(addr);
213 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (regs->eflags & VM_MASK) {
215 /* We are in virtual-8086 mode. Return 0 */
216 goto no_kprobe;
217 }
218
219 if (*addr != BREAKPOINT_INSTRUCTION) {
220 /*
221 * The breakpoint instruction was removed right
222 * after we hit it. Another cpu has removed
223 * either a probepoint or a debugger breakpoint
224 * at this address. In either case, no further
225 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700226 * Back up over the (now missing) int3 and run
227 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
Jim Kenistonbce06492005-09-06 15:19:34 -0700229 regs->eip -= sizeof(kprobe_opcode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 ret = 1;
231 }
232 /* Not one of ours: let kernel handle it */
233 goto no_kprobe;
234 }
235
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800236 set_current_kprobe(p, regs, kcb);
237 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (p->pre_handler && p->pre_handler(p, regs))
240 /* handler has already set things up, so skip ss setup */
241 return 1;
242
243ss_probe:
244 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800245 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 1;
247
248no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800249 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return ret;
251}
252
253/*
Hien Nguyenb94cce92005-06-23 00:09:19 -0700254 * For function-return probes, init_kprobes() establishes a probepoint
255 * here. When a retprobed function returns, this probe is hit and
256 * trampoline_probe_handler() runs, calling the kretprobe's handler.
257 */
258 void kretprobe_trampoline_holder(void)
259 {
260 asm volatile ( ".global kretprobe_trampoline\n"
261 "kretprobe_trampoline: \n"
262 "nop\n");
263 }
264
265/*
266 * Called when we hit the probe point at kretprobe_trampoline
267 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700268int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700269{
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700270 struct kretprobe_instance *ri = NULL;
271 struct hlist_head *head;
272 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800273 unsigned long flags, orig_ret_address = 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700274 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700275
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800276 spin_lock_irqsave(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700277 head = kretprobe_inst_table_head(current);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700278
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700279 /*
280 * It is possible to have multiple instances associated with a given
281 * task either because an multiple functions in the call path
282 * have a return probe installed on them, and/or more then one return
283 * return probe was registered for a target function.
284 *
285 * We can handle this because:
286 * - instances are always inserted at the head of the list
287 * - when multiple return probes are registered for the same
288 * function, the first instance's ret_addr will point to the
289 * real return address, and all the rest will point to
290 * kretprobe_trampoline
291 */
292 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
293 if (ri->task != current)
294 /* another task is sharing our hash bucket */
295 continue;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700296
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700297 if (ri->rp && ri->rp->handler)
298 ri->rp->handler(ri, regs);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700299
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700300 orig_ret_address = (unsigned long)ri->ret_addr;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700301 recycle_rp_inst(ri);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700302
303 if (orig_ret_address != trampoline_address)
304 /*
305 * This is the real return address. Any other
306 * instances associated with this task are for
307 * other calls deeper on the call stack
308 */
309 break;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700310 }
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700311
312 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
313 regs->eip = orig_ret_address;
314
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800315 reset_current_kprobe();
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800316 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700317 preempt_enable_no_resched();
318
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800319 /*
320 * By returning a non-zero value, we are telling
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800321 * kprobe_handler() that we don't want the post_handler
322 * to run (and have re-enabled preemption)
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800323 */
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700324 return 1;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700325}
326
327/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 * Called after single-stepping. p->addr is the address of the
329 * instruction whose first byte has been replaced by the "int 3"
330 * instruction. To avoid the SMP problems that can occur when we
331 * temporarily put back the original opcode to single-step, we
332 * single-stepped a copy of the instruction. The address of this
333 * copy is p->ainsn.insn.
334 *
335 * This function prepares to return from the post-single-step
336 * interrupt. We have to fix up the stack as follows:
337 *
338 * 0) Except in the case of absolute or indirect jump or call instructions,
339 * the new eip is relative to the copied instruction. We need to make
340 * it relative to the original instruction.
341 *
342 * 1) If the single-stepped instruction was pushfl, then the TF and IF
343 * flags are set in the just-pushed eflags, and may need to be cleared.
344 *
345 * 2) If the single-stepped instruction was a call, the return address
346 * that is atop the stack is the address following the copied instruction.
347 * We need to make it the address following the original instruction.
348 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800349static void __kprobes resume_execution(struct kprobe *p,
350 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unsigned long *tos = (unsigned long *)&regs->esp;
353 unsigned long next_eip = 0;
354 unsigned long copy_eip = (unsigned long)&p->ainsn.insn;
355 unsigned long orig_eip = (unsigned long)p->addr;
356
357 switch (p->ainsn.insn[0]) {
358 case 0x9c: /* pushfl */
359 *tos &= ~(TF_MASK | IF_MASK);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800360 *tos |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 break;
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700362 case 0xc3: /* ret/lret */
363 case 0xcb:
364 case 0xc2:
365 case 0xca:
366 regs->eflags &= ~TF_MASK;
367 /* eip is already adjusted, no more changes required*/
368 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 case 0xe8: /* call relative - Fix return addr */
370 *tos = orig_eip + (*tos - copy_eip);
371 break;
372 case 0xff:
373 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
374 /* call absolute, indirect */
375 /* Fix return addr; eip is correct. */
376 next_eip = regs->eip;
377 *tos = orig_eip + (*tos - copy_eip);
378 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
379 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
380 /* eip is correct. */
381 next_eip = regs->eip;
382 }
383 break;
384 case 0xea: /* jmp absolute -- eip is correct */
385 next_eip = regs->eip;
386 break;
387 default:
388 break;
389 }
390
391 regs->eflags &= ~TF_MASK;
392 if (next_eip) {
393 regs->eip = next_eip;
394 } else {
395 regs->eip = orig_eip + (regs->eip - copy_eip);
396 }
397}
398
399/*
400 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800401 * remain disabled thoroughout this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 */
403static inline int post_kprobe_handler(struct pt_regs *regs)
404{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800405 struct kprobe *cur = kprobe_running();
406 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
407
408 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
410
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800411 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
412 kcb->kprobe_status = KPROBE_HIT_SSDONE;
413 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800416 resume_execution(cur, regs, kcb);
417 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700419 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800420 if (kcb->kprobe_status == KPROBE_REENTER) {
421 restore_previous_kprobe(kcb);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700422 goto out;
423 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800424 reset_current_kprobe();
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700425out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 preempt_enable_no_resched();
427
428 /*
429 * if somebody else is singlestepping across a probe point, eflags
430 * will have TF set, in which case, continue the remaining processing
431 * of do_debug, as if this is not a probe hit.
432 */
433 if (regs->eflags & TF_MASK)
434 return 0;
435
436 return 1;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
440{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800441 struct kprobe *cur = kprobe_running();
442 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
443
444 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 1;
446
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800447 if (kcb->kprobe_status & KPROBE_HIT_SS) {
448 resume_execution(cur, regs, kcb);
449 regs->eflags |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800451 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 preempt_enable_no_resched();
453 }
454 return 0;
455}
456
457/*
458 * Wrapper routine to for handling exceptions.
459 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700460int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
461 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800464 int ret = NOTIFY_DONE;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 switch (val) {
467 case DIE_INT3:
468 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800469 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 break;
471 case DIE_DEBUG:
472 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800473 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case DIE_GPF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800477 /* kprobe_running() needs smp_processor_id() */
478 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (kprobe_running() &&
480 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800481 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800482 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 break;
484 default:
485 break;
486 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800487 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700490int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct jprobe *jp = container_of(p, struct jprobe, kp);
493 unsigned long addr;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800494 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800496 kcb->jprobe_saved_regs = *regs;
497 kcb->jprobe_saved_esp = &regs->esp;
498 addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 /*
501 * TBD: As Linus pointed out, gcc assumes that the callee
502 * owns the argument space and could overwrite it, e.g.
503 * tailcall optimization. So, to be absolutely safe
504 * we also save and restore enough stack bytes to cover
505 * the argument area.
506 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800507 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
508 MIN_STACK_SIZE(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 regs->eflags &= ~IF_MASK;
510 regs->eip = (unsigned long)(jp->entry);
511 return 1;
512}
513
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700514void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800516 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 asm volatile (" xchgl %%ebx,%%esp \n"
519 " int3 \n"
520 " .globl jprobe_return_end \n"
521 " jprobe_return_end: \n"
522 " nop \n"::"b"
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800523 (kcb->jprobe_saved_esp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700526int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800528 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 u8 *addr = (u8 *) (regs->eip - 1);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800530 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct jprobe *jp = container_of(p, struct jprobe, kp);
532
533 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800534 if (&regs->esp != kcb->jprobe_saved_esp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct pt_regs *saved_regs =
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800536 container_of(kcb->jprobe_saved_esp,
537 struct pt_regs, esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 printk("current esp %p does not match saved esp %p\n",
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800539 &regs->esp, kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 printk("Saved registers for jprobe %p\n", jp);
541 show_registers(saved_regs);
542 printk("Current registers\n");
543 show_registers(regs);
544 BUG();
545 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800546 *regs = kcb->jprobe_saved_regs;
547 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 MIN_STACK_SIZE(stack_addr));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800549 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return 1;
551 }
552 return 0;
553}
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700554
555static struct kprobe trampoline_p = {
556 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
557 .pre_handler = trampoline_probe_handler
558};
559
Rusty Lynch67729262005-07-05 18:54:50 -0700560int __init arch_init_kprobes(void)
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700561{
562 return register_kprobe(&trampoline_p);
563}