blob: b40614f5afe297f977404b560c3005374678a548 [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{
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -080061 /* insn: must be on special executable page on i386. */
62 p->ainsn.insn = get_insn_slot();
63 if (!p->ainsn.insn)
64 return -ENOMEM;
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Rusty Lynch7e1048b2005-06-23 00:09:25 -070067 p->opcode = *p->addr;
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -080068 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -070069}
70
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070071void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070072{
73 *p->addr = BREAKPOINT_INSTRUCTION;
74 flush_icache_range((unsigned long) p->addr,
75 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
76}
77
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -070078void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -070079{
80 *p->addr = p->opcode;
81 flush_icache_range((unsigned long) p->addr,
82 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -080085void __kprobes arch_remove_kprobe(struct kprobe *p)
86{
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -080087 mutex_lock(&kprobe_mutex);
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -080088 free_insn_slot(p->ainsn.insn);
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -080089 mutex_unlock(&kprobe_mutex);
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -080090}
91
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080092static inline void save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070093{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -080094 kcb->prev_kprobe.kp = kprobe_running();
95 kcb->prev_kprobe.status = kcb->kprobe_status;
96 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
97 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -070098}
99
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800100static inline void restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700101{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800102 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
103 kcb->kprobe_status = kcb->prev_kprobe.status;
104 kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
105 kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700106}
107
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800108static inline void set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
109 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700110{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800111 __get_cpu_var(current_kprobe) = p;
112 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700113 = (regs->eflags & (TF_MASK | IF_MASK));
114 if (is_IF_modifier(p->opcode))
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800115 kcb->kprobe_saved_eflags &= ~IF_MASK;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700116}
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static inline void prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
119{
120 regs->eflags |= TF_MASK;
121 regs->eflags &= ~IF_MASK;
122 /*single step inline if the instruction is an int3*/
123 if (p->opcode == BREAKPOINT_INSTRUCTION)
124 regs->eip = (unsigned long)p->addr;
125 else
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800126 regs->eip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800129/* Called with kretprobe_lock held */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700130void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
131 struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700132{
133 unsigned long *sara = (unsigned long *)&regs->esp;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700134 struct kretprobe_instance *ri;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700135
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700136 if ((ri = get_free_rp_inst(rp)) != NULL) {
137 ri->rp = rp;
138 ri->task = current;
139 ri->ret_addr = (kprobe_opcode_t *) *sara;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700140
Hien Nguyenb94cce92005-06-23 00:09:19 -0700141 /* Replace the return addr with trampoline addr */
142 *sara = (unsigned long) &kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700143
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700144 add_rp_inst(ri);
145 } else {
146 rp->nmissed++;
147 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
151 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
152 * remain disabled thorough out this function.
153 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700154static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct kprobe *p;
157 int ret = 0;
158 kprobe_opcode_t *addr = NULL;
159 unsigned long *lp;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800160 struct kprobe_ctlblk *kcb;
161
162 /*
163 * We don't want to be preempted for the entire
164 * duration of kprobe processing
165 */
166 preempt_disable();
167 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* Check if the application is using LDT entry for its code segment and
170 * calculate the address by reading the base address from the LDT entry.
171 */
172 if ((regs->xcs & 4) && (current->mm)) {
173 lp = (unsigned long *) ((unsigned long)((regs->xcs >> 3) * 8)
174 + (char *) current->mm->context.ldt);
175 addr = (kprobe_opcode_t *) (get_desc_base(lp) + regs->eip -
176 sizeof(kprobe_opcode_t));
177 } else {
178 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
179 }
180 /* Check we're not actually recursing */
181 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 p = get_kprobe(addr);
183 if (p) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800184 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700185 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 regs->eflags &= ~TF_MASK;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800187 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 goto no_kprobe;
189 }
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700190 /* We have reentered the kprobe_handler(), since
191 * another probe was hit while within the handler.
192 * We here save the original kprobes variables and
193 * just single step on the instruction of the new probe
194 * without calling any user handlers.
195 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800196 save_previous_kprobe(kcb);
197 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800198 kprobes_inc_nmissed_count(p);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700199 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800200 kcb->kprobe_status = KPROBE_REENTER;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700201 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800203 if (regs->eflags & VM_MASK) {
204 /* We are in virtual-8086 mode. Return 0 */
205 goto no_kprobe;
206 }
207 if (*addr != BREAKPOINT_INSTRUCTION) {
208 /* The breakpoint instruction was removed by
209 * another cpu right after we hit, no further
210 * handling of this interrupt is appropriate
211 */
212 regs->eip -= sizeof(kprobe_opcode_t);
213 ret = 1;
214 goto no_kprobe;
215 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800216 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (p->break_handler && p->break_handler(p, regs)) {
218 goto ss_probe;
219 }
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto no_kprobe;
222 }
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 p = get_kprobe(addr);
225 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (regs->eflags & VM_MASK) {
227 /* We are in virtual-8086 mode. Return 0 */
228 goto no_kprobe;
229 }
230
231 if (*addr != BREAKPOINT_INSTRUCTION) {
232 /*
233 * The breakpoint instruction was removed right
234 * after we hit it. Another cpu has removed
235 * either a probepoint or a debugger breakpoint
236 * at this address. In either case, no further
237 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700238 * Back up over the (now missing) int3 and run
239 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
Jim Kenistonbce06492005-09-06 15:19:34 -0700241 regs->eip -= sizeof(kprobe_opcode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 ret = 1;
243 }
244 /* Not one of ours: let kernel handle it */
245 goto no_kprobe;
246 }
247
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800248 set_current_kprobe(p, regs, kcb);
249 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 if (p->pre_handler && p->pre_handler(p, regs))
252 /* handler has already set things up, so skip ss setup */
253 return 1;
254
255ss_probe:
256 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800257 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 1;
259
260no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800261 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return ret;
263}
264
265/*
Hien Nguyenb94cce92005-06-23 00:09:19 -0700266 * For function-return probes, init_kprobes() establishes a probepoint
267 * here. When a retprobed function returns, this probe is hit and
268 * trampoline_probe_handler() runs, calling the kretprobe's handler.
269 */
270 void kretprobe_trampoline_holder(void)
271 {
272 asm volatile ( ".global kretprobe_trampoline\n"
273 "kretprobe_trampoline: \n"
274 "nop\n");
275 }
276
277/*
278 * Called when we hit the probe point at kretprobe_trampoline
279 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700280int __kprobes trampoline_probe_handler(struct kprobe *p, struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700281{
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700282 struct kretprobe_instance *ri = NULL;
283 struct hlist_head *head;
284 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800285 unsigned long flags, orig_ret_address = 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700286 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700287
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800288 spin_lock_irqsave(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700289 head = kretprobe_inst_table_head(current);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700290
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700291 /*
292 * It is possible to have multiple instances associated with a given
293 * task either because an multiple functions in the call path
294 * have a return probe installed on them, and/or more then one return
295 * return probe was registered for a target function.
296 *
297 * We can handle this because:
298 * - instances are always inserted at the head of the list
299 * - when multiple return probes are registered for the same
300 * function, the first instance's ret_addr will point to the
301 * real return address, and all the rest will point to
302 * kretprobe_trampoline
303 */
304 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
305 if (ri->task != current)
306 /* another task is sharing our hash bucket */
307 continue;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700308
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700309 if (ri->rp && ri->rp->handler)
310 ri->rp->handler(ri, regs);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700311
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700312 orig_ret_address = (unsigned long)ri->ret_addr;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700313 recycle_rp_inst(ri);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700314
315 if (orig_ret_address != trampoline_address)
316 /*
317 * This is the real return address. Any other
318 * instances associated with this task are for
319 * other calls deeper on the call stack
320 */
321 break;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700322 }
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700323
324 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
325 regs->eip = orig_ret_address;
326
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800327 reset_current_kprobe();
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800328 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700329 preempt_enable_no_resched();
330
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800331 /*
332 * By returning a non-zero value, we are telling
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800333 * kprobe_handler() that we don't want the post_handler
334 * to run (and have re-enabled preemption)
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800335 */
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700336 return 1;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700337}
338
339/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * Called after single-stepping. p->addr is the address of the
341 * instruction whose first byte has been replaced by the "int 3"
342 * instruction. To avoid the SMP problems that can occur when we
343 * temporarily put back the original opcode to single-step, we
344 * single-stepped a copy of the instruction. The address of this
345 * copy is p->ainsn.insn.
346 *
347 * This function prepares to return from the post-single-step
348 * interrupt. We have to fix up the stack as follows:
349 *
350 * 0) Except in the case of absolute or indirect jump or call instructions,
351 * the new eip is relative to the copied instruction. We need to make
352 * it relative to the original instruction.
353 *
354 * 1) If the single-stepped instruction was pushfl, then the TF and IF
355 * flags are set in the just-pushed eflags, and may need to be cleared.
356 *
357 * 2) If the single-stepped instruction was a call, the return address
358 * that is atop the stack is the address following the copied instruction.
359 * We need to make it the address following the original instruction.
360 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800361static void __kprobes resume_execution(struct kprobe *p,
362 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 unsigned long *tos = (unsigned long *)&regs->esp;
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800365 unsigned long copy_eip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned long orig_eip = (unsigned long)p->addr;
367
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800368 regs->eflags &= ~TF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 switch (p->ainsn.insn[0]) {
370 case 0x9c: /* pushfl */
371 *tos &= ~(TF_MASK | IF_MASK);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800372 *tos |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700374 case 0xc3: /* ret/lret */
375 case 0xcb:
376 case 0xc2:
377 case 0xca:
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800378 case 0xea: /* jmp absolute -- eip is correct */
379 /* eip is already adjusted, no more changes required */
380 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case 0xe8: /* call relative - Fix return addr */
382 *tos = orig_eip + (*tos - copy_eip);
383 break;
384 case 0xff:
385 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
386 /* call absolute, indirect */
387 /* Fix return addr; eip is correct. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *tos = orig_eip + (*tos - copy_eip);
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800389 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
391 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
392 /* eip is correct. */
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800393 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 default:
396 break;
397 }
398
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800399 regs->eip = orig_eip + (regs->eip - copy_eip);
400
401no_change:
402 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
405/*
406 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800407 * remain disabled thoroughout this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
409static inline int post_kprobe_handler(struct pt_regs *regs)
410{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800411 struct kprobe *cur = kprobe_running();
412 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
413
414 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 return 0;
416
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800417 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
418 kcb->kprobe_status = KPROBE_HIT_SSDONE;
419 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800422 resume_execution(cur, regs, kcb);
423 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700425 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800426 if (kcb->kprobe_status == KPROBE_REENTER) {
427 restore_previous_kprobe(kcb);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700428 goto out;
429 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800430 reset_current_kprobe();
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700431out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 preempt_enable_no_resched();
433
434 /*
435 * if somebody else is singlestepping across a probe point, eflags
436 * will have TF set, in which case, continue the remaining processing
437 * of do_debug, as if this is not a probe hit.
438 */
439 if (regs->eflags & TF_MASK)
440 return 0;
441
442 return 1;
443}
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445static inline int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
446{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800447 struct kprobe *cur = kprobe_running();
448 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
449
450 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 1;
452
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800453 if (kcb->kprobe_status & KPROBE_HIT_SS) {
454 resume_execution(cur, regs, kcb);
455 regs->eflags |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800457 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 preempt_enable_no_resched();
459 }
460 return 0;
461}
462
463/*
464 * Wrapper routine to for handling exceptions.
465 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700466int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
467 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800470 int ret = NOTIFY_DONE;
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 switch (val) {
473 case DIE_INT3:
474 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800475 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 break;
477 case DIE_DEBUG:
478 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800479 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 break;
481 case DIE_GPF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800483 /* kprobe_running() needs smp_processor_id() */
484 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (kprobe_running() &&
486 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800487 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800488 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 break;
490 default:
491 break;
492 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700496int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct jprobe *jp = container_of(p, struct jprobe, kp);
499 unsigned long addr;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800500 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800502 kcb->jprobe_saved_regs = *regs;
503 kcb->jprobe_saved_esp = &regs->esp;
504 addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 /*
507 * TBD: As Linus pointed out, gcc assumes that the callee
508 * owns the argument space and could overwrite it, e.g.
509 * tailcall optimization. So, to be absolutely safe
510 * we also save and restore enough stack bytes to cover
511 * the argument area.
512 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800513 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
514 MIN_STACK_SIZE(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 regs->eflags &= ~IF_MASK;
516 regs->eip = (unsigned long)(jp->entry);
517 return 1;
518}
519
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700520void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800522 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 asm volatile (" xchgl %%ebx,%%esp \n"
525 " int3 \n"
526 " .globl jprobe_return_end \n"
527 " jprobe_return_end: \n"
528 " nop \n"::"b"
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800529 (kcb->jprobe_saved_esp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700532int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800534 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 u8 *addr = (u8 *) (regs->eip - 1);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800536 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 struct jprobe *jp = container_of(p, struct jprobe, kp);
538
539 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800540 if (&regs->esp != kcb->jprobe_saved_esp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct pt_regs *saved_regs =
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800542 container_of(kcb->jprobe_saved_esp,
543 struct pt_regs, esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 printk("current esp %p does not match saved esp %p\n",
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800545 &regs->esp, kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 printk("Saved registers for jprobe %p\n", jp);
547 show_registers(saved_regs);
548 printk("Current registers\n");
549 show_registers(regs);
550 BUG();
551 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800552 *regs = kcb->jprobe_saved_regs;
553 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 MIN_STACK_SIZE(stack_addr));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800555 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 1;
557 }
558 return 0;
559}
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700560
561static struct kprobe trampoline_p = {
562 .addr = (kprobe_opcode_t *) &kretprobe_trampoline,
563 .pre_handler = trampoline_probe_handler
564};
565
Rusty Lynch67729262005-07-05 18:54:50 -0700566int __init arch_init_kprobes(void)
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700567{
568 return register_kprobe(&trampoline_p);
569}