blob: afe6505ca0b3a78af53db084d60574540ea7361d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kprobes.h>
32#include <linux/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/preempt.h>
Rusty Lynch7e1048b2005-06-23 00:09:25 -070034#include <asm/cacheflush.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/kdebug.h>
36#include <asm/desc.h>
Prasanna S Panchamukhib4026512006-03-26 01:38:22 -080037#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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 */
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -070045static __always_inline void set_jmp_op(void *from, void *to)
Masami Hiramatsu311ac882006-03-26 01:38:17 -080046{
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 */
Masami Hiramatsu585deac2006-06-26 00:25:19 -070059static __always_inline int can_boost(kprobe_opcode_t *opcodes)
Masami Hiramatsu311ac882006-03-26 01:38:17 -080060{
Masami Hiramatsu585deac2006-06-26 00:25:19 -070061#define W(row,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,ba,bb,bc,bd,be,bf) \
62 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
63 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
64 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
65 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
66 << (row % 32))
67 /*
68 * Undefined/reserved opcodes, conditional jump, Opcode Extension
69 * Groups, and some special opcodes can not be boost.
70 */
71 static const unsigned long twobyte_is_boostable[256 / 32] = {
72 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
73 /* ------------------------------- */
74 W(0x00, 0,0,1,1,0,0,1,0,1,1,0,0,0,0,0,0)| /* 00 */
75 W(0x10, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 10 */
76 W(0x20, 1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0)| /* 20 */
77 W(0x30, 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 30 */
78 W(0x40, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)| /* 40 */
79 W(0x50, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), /* 50 */
80 W(0x60, 1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1)| /* 60 */
81 W(0x70, 0,0,0,0,1,1,1,1,0,0,0,0,0,0,1,1), /* 70 */
82 W(0x80, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)| /* 80 */
83 W(0x90, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1), /* 90 */
84 W(0xa0, 1,1,0,1,1,1,0,0,1,1,0,1,1,1,0,1)| /* a0 */
85 W(0xb0, 1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1), /* b0 */
86 W(0xc0, 1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1)| /* c0 */
87 W(0xd0, 0,1,1,1,0,1,0,0,1,1,0,1,1,1,0,1), /* d0 */
88 W(0xe0, 0,1,1,0,0,1,0,0,1,1,0,1,1,1,0,1)| /* e0 */
89 W(0xf0, 0,1,1,1,0,1,0,0,1,1,1,0,1,1,1,0) /* f0 */
90 /* ------------------------------- */
91 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
92 };
93#undef W
94 kprobe_opcode_t opcode;
95 kprobe_opcode_t *orig_opcodes = opcodes;
96retry:
97 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
98 return 0;
99 opcode = *(opcodes++);
100
101 /* 2nd-byte opcode */
102 if (opcode == 0x0f) {
103 if (opcodes - orig_opcodes > MAX_INSN_SIZE - 1)
104 return 0;
105 return test_bit(*opcodes, twobyte_is_boostable);
106 }
107
108 switch (opcode & 0xf0) {
109 case 0x60:
110 if (0x63 < opcode && opcode < 0x67)
111 goto retry; /* prefixes */
112 /* can't boost Address-size override and bound */
113 return (opcode != 0x62 && opcode != 0x67);
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800114 case 0x70:
115 return 0; /* can't boost conditional jump */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800116 case 0xc0:
Masami Hiramatsu585deac2006-06-26 00:25:19 -0700117 /* can't boost software-interruptions */
118 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800119 case 0xd0:
120 /* can boost AA* and XLAT */
121 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
122 case 0xe0:
Masami Hiramatsu585deac2006-06-26 00:25:19 -0700123 /* can boost in/out and absolute jmps */
124 return ((opcode & 0x04) || opcode == 0xea);
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800125 case 0xf0:
Masami Hiramatsu585deac2006-06-26 00:25:19 -0700126 if ((opcode & 0x0c) == 0 && opcode != 0xf1)
127 goto retry; /* lock/rep(ne) prefix */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800128 /* clear and set flags can be boost */
129 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
130 default:
Masami Hiramatsu585deac2006-06-26 00:25:19 -0700131 if (opcode == 0x26 || opcode == 0x36 || opcode == 0x3e)
132 goto retry; /* prefixes */
133 /* can't boost CS override and call */
134 return (opcode != 0x2e && opcode != 0x9a);
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800135 }
136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * returns non-zero if opcode modifies the interrupt flag.
140 */
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700141static int __kprobes is_IF_modifier(kprobe_opcode_t opcode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 switch (opcode) {
144 case 0xfa: /* cli */
145 case 0xfb: /* sti */
146 case 0xcf: /* iret/iretd */
147 case 0x9d: /* popf/popfd */
148 return 1;
149 }
150 return 0;
151}
152
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700153int __kprobes arch_prepare_kprobe(struct kprobe *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800155 /* insn: must be on special executable page on i386. */
156 p->ainsn.insn = get_insn_slot();
157 if (!p->ainsn.insn)
158 return -ENOMEM;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 memcpy(p->ainsn.insn, p->addr, MAX_INSN_SIZE * sizeof(kprobe_opcode_t));
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700161 p->opcode = *p->addr;
Masami Hiramatsu585deac2006-06-26 00:25:19 -0700162 if (can_boost(p->addr)) {
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800163 p->ainsn.boostable = 0;
164 } else {
165 p->ainsn.boostable = -1;
166 }
Anil S Keshavamurthy49a2a1b2006-01-09 20:52:43 -0800167 return 0;
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700168}
169
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700170void __kprobes arch_arm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700171{
172 *p->addr = BREAKPOINT_INSTRUCTION;
173 flush_icache_range((unsigned long) p->addr,
174 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
175}
176
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700177void __kprobes arch_disarm_kprobe(struct kprobe *p)
Rusty Lynch7e1048b2005-06-23 00:09:25 -0700178{
179 *p->addr = p->opcode;
180 flush_icache_range((unsigned long) p->addr,
181 (unsigned long) p->addr + sizeof(kprobe_opcode_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800184void __kprobes arch_remove_kprobe(struct kprobe *p)
185{
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800186 mutex_lock(&kprobe_mutex);
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800187 free_insn_slot(p->ainsn.insn);
Ingo Molnar7a7d1cf2006-03-23 03:00:35 -0800188 mutex_unlock(&kprobe_mutex);
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800189}
190
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700191static void __kprobes save_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700192{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800193 kcb->prev_kprobe.kp = kprobe_running();
194 kcb->prev_kprobe.status = kcb->kprobe_status;
195 kcb->prev_kprobe.old_eflags = kcb->kprobe_old_eflags;
196 kcb->prev_kprobe.saved_eflags = kcb->kprobe_saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700197}
198
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700199static void __kprobes restore_previous_kprobe(struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700200{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800201 __get_cpu_var(current_kprobe) = kcb->prev_kprobe.kp;
202 kcb->kprobe_status = kcb->prev_kprobe.status;
203 kcb->kprobe_old_eflags = kcb->prev_kprobe.old_eflags;
204 kcb->kprobe_saved_eflags = kcb->prev_kprobe.saved_eflags;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700205}
206
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700207static void __kprobes set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800208 struct kprobe_ctlblk *kcb)
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700209{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800210 __get_cpu_var(current_kprobe) = p;
211 kcb->kprobe_saved_eflags = kcb->kprobe_old_eflags
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700212 = (regs->eflags & (TF_MASK | IF_MASK));
213 if (is_IF_modifier(p->opcode))
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800214 kcb->kprobe_saved_eflags &= ~IF_MASK;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700215}
216
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700217static void __kprobes prepare_singlestep(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 regs->eflags |= TF_MASK;
220 regs->eflags &= ~IF_MASK;
221 /*single step inline if the instruction is an int3*/
222 if (p->opcode == BREAKPOINT_INSTRUCTION)
223 regs->eip = (unsigned long)p->addr;
224 else
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800225 regs->eip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800228/* Called with kretprobe_lock held */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700229void __kprobes arch_prepare_kretprobe(struct kretprobe *rp,
230 struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700231{
232 unsigned long *sara = (unsigned long *)&regs->esp;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700233 struct kretprobe_instance *ri;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700234
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700235 if ((ri = get_free_rp_inst(rp)) != NULL) {
236 ri->rp = rp;
237 ri->task = current;
238 ri->ret_addr = (kprobe_opcode_t *) *sara;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700239
Hien Nguyenb94cce92005-06-23 00:09:19 -0700240 /* Replace the return addr with trampoline addr */
241 *sara = (unsigned long) &kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700242
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700243 add_rp_inst(ri);
244 } else {
245 rp->nmissed++;
246 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700247}
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249/*
250 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
251 * remain disabled thorough out this function.
252 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700253static int __kprobes kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct kprobe *p;
256 int ret = 0;
bibo,mao2326c772006-03-26 01:38:21 -0800257 kprobe_opcode_t *addr;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800258 struct kprobe_ctlblk *kcb;
259
bibo,mao2326c772006-03-26 01:38:21 -0800260 addr = (kprobe_opcode_t *)(regs->eip - sizeof(kprobe_opcode_t));
261
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800262 /*
263 * We don't want to be preempted for the entire
264 * duration of kprobe processing
265 */
266 preempt_disable();
267 kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 /* Check we're not actually recursing */
270 if (kprobe_running()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 p = get_kprobe(addr);
272 if (p) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800273 if (kcb->kprobe_status == KPROBE_HIT_SS &&
Keshavamurthy Anil Sdeac66a2005-09-06 15:19:35 -0700274 *p->ainsn.insn == BREAKPOINT_INSTRUCTION) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 regs->eflags &= ~TF_MASK;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800276 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 goto no_kprobe;
278 }
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700279 /* We have reentered the kprobe_handler(), since
280 * another probe was hit while within the handler.
281 * We here save the original kprobes variables and
282 * just single step on the instruction of the new probe
283 * without calling any user handlers.
284 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800285 save_previous_kprobe(kcb);
286 set_current_kprobe(p, regs, kcb);
Keshavamurthy Anil Sbf8d5c52005-12-12 00:37:34 -0800287 kprobes_inc_nmissed_count(p);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700288 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800289 kcb->kprobe_status = KPROBE_REENTER;
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700290 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 } else {
Keshavamurthy Anil Seb3a7292006-01-11 12:17:42 -0800292 if (*addr != BREAKPOINT_INSTRUCTION) {
293 /* The breakpoint instruction was removed by
294 * another cpu right after we hit, no further
295 * handling of this interrupt is appropriate
296 */
297 regs->eip -= sizeof(kprobe_opcode_t);
298 ret = 1;
299 goto no_kprobe;
300 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800301 p = __get_cpu_var(current_kprobe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (p->break_handler && p->break_handler(p, regs)) {
303 goto ss_probe;
304 }
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 goto no_kprobe;
307 }
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 p = get_kprobe(addr);
310 if (!p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (*addr != BREAKPOINT_INSTRUCTION) {
312 /*
313 * The breakpoint instruction was removed right
314 * after we hit it. Another cpu has removed
315 * either a probepoint or a debugger breakpoint
316 * at this address. In either case, no further
317 * handling of this interrupt is appropriate.
Jim Kenistonbce06492005-09-06 15:19:34 -0700318 * Back up over the (now missing) int3 and run
319 * the original instruction.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
Jim Kenistonbce06492005-09-06 15:19:34 -0700321 regs->eip -= sizeof(kprobe_opcode_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 ret = 1;
323 }
324 /* Not one of ours: let kernel handle it */
325 goto no_kprobe;
326 }
327
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800328 set_current_kprobe(p, regs, kcb);
329 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (p->pre_handler && p->pre_handler(p, regs))
332 /* handler has already set things up, so skip ss setup */
333 return 1;
334
mao, bibo36721652006-06-26 00:25:22 -0700335ss_probe:
Masami Hiramatsuc39df472006-07-30 03:03:21 -0700336#ifndef CONFIG_PREEMPT
337 if (p->ainsn.boostable == 1 && !p->post_handler){
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800338 /* Boost up -- we can execute copied instructions directly */
339 reset_current_kprobe();
340 regs->eip = (unsigned long)p->ainsn.insn;
341 preempt_enable_no_resched();
342 return 1;
343 }
Masami Hiramatsuc39df472006-07-30 03:03:21 -0700344#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 prepare_singlestep(p, regs);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800346 kcb->kprobe_status = KPROBE_HIT_SS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return 1;
348
349no_kprobe:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800350 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return ret;
352}
353
354/*
Hien Nguyenb94cce92005-06-23 00:09:19 -0700355 * For function-return probes, init_kprobes() establishes a probepoint
356 * here. When a retprobed function returns, this probe is hit and
357 * trampoline_probe_handler() runs, calling the kretprobe's handler.
358 */
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800359 void __kprobes kretprobe_trampoline_holder(void)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700360 {
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800361 asm volatile ( ".global kretprobe_trampoline\n"
Hien Nguyenb94cce92005-06-23 00:09:19 -0700362 "kretprobe_trampoline: \n"
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800363 " pushf\n"
364 /* skip cs, eip, orig_eax, es, ds */
365 " subl $20, %esp\n"
366 " pushl %eax\n"
367 " pushl %ebp\n"
368 " pushl %edi\n"
369 " pushl %esi\n"
370 " pushl %edx\n"
371 " pushl %ecx\n"
372 " pushl %ebx\n"
373 " movl %esp, %eax\n"
374 " call trampoline_handler\n"
375 /* move eflags to cs */
376 " movl 48(%esp), %edx\n"
377 " movl %edx, 44(%esp)\n"
378 /* save true return address on eflags */
379 " movl %eax, 48(%esp)\n"
380 " popl %ebx\n"
381 " popl %ecx\n"
382 " popl %edx\n"
383 " popl %esi\n"
384 " popl %edi\n"
385 " popl %ebp\n"
386 " popl %eax\n"
387 /* skip eip, orig_eax, es, ds */
388 " addl $16, %esp\n"
389 " popf\n"
390 " ret\n");
391}
Hien Nguyenb94cce92005-06-23 00:09:19 -0700392
393/*
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800394 * Called from kretprobe_trampoline
Hien Nguyenb94cce92005-06-23 00:09:19 -0700395 */
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800396fastcall void *__kprobes trampoline_handler(struct pt_regs *regs)
Hien Nguyenb94cce92005-06-23 00:09:19 -0700397{
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700398 struct kretprobe_instance *ri = NULL;
399 struct hlist_head *head;
400 struct hlist_node *node, *tmp;
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800401 unsigned long flags, orig_ret_address = 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700402 unsigned long trampoline_address =(unsigned long)&kretprobe_trampoline;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700403
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800404 spin_lock_irqsave(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700405 head = kretprobe_inst_table_head(current);
Hien Nguyenb94cce92005-06-23 00:09:19 -0700406
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700407 /*
408 * It is possible to have multiple instances associated with a given
409 * task either because an multiple functions in the call path
410 * have a return probe installed on them, and/or more then one return
411 * return probe was registered for a target function.
412 *
413 * We can handle this because:
414 * - instances are always inserted at the head of the list
415 * - when multiple return probes are registered for the same
416 * function, the first instance's ret_addr will point to the
417 * real return address, and all the rest will point to
418 * kretprobe_trampoline
419 */
420 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
421 if (ri->task != current)
422 /* another task is sharing our hash bucket */
423 continue;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700424
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800425 if (ri->rp && ri->rp->handler){
426 __get_cpu_var(current_kprobe) = &ri->rp->kp;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700427 ri->rp->handler(ri, regs);
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800428 __get_cpu_var(current_kprobe) = NULL;
429 }
Hien Nguyenb94cce92005-06-23 00:09:19 -0700430
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700431 orig_ret_address = (unsigned long)ri->ret_addr;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700432 recycle_rp_inst(ri);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700433
434 if (orig_ret_address != trampoline_address)
435 /*
436 * This is the real return address. Any other
437 * instances associated with this task are for
438 * other calls deeper on the call stack
439 */
440 break;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700441 }
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700442
443 BUG_ON(!orig_ret_address || (orig_ret_address == trampoline_address));
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700444
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800445 spin_unlock_irqrestore(&kretprobe_lock, flags);
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700446
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800447 return (void*)orig_ret_address;
Hien Nguyenb94cce92005-06-23 00:09:19 -0700448}
449
450/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 * Called after single-stepping. p->addr is the address of the
452 * instruction whose first byte has been replaced by the "int 3"
453 * instruction. To avoid the SMP problems that can occur when we
454 * temporarily put back the original opcode to single-step, we
455 * single-stepped a copy of the instruction. The address of this
456 * copy is p->ainsn.insn.
457 *
458 * This function prepares to return from the post-single-step
459 * interrupt. We have to fix up the stack as follows:
460 *
461 * 0) Except in the case of absolute or indirect jump or call instructions,
462 * the new eip is relative to the copied instruction. We need to make
463 * it relative to the original instruction.
464 *
465 * 1) If the single-stepped instruction was pushfl, then the TF and IF
466 * flags are set in the just-pushed eflags, and may need to be cleared.
467 *
468 * 2) If the single-stepped instruction was a call, the return address
469 * that is atop the stack is the address following the copied instruction.
470 * We need to make it the address following the original instruction.
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800471 *
472 * This function also checks instruction size for preparing direct execution.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800474static void __kprobes resume_execution(struct kprobe *p,
475 struct pt_regs *regs, struct kprobe_ctlblk *kcb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 unsigned long *tos = (unsigned long *)&regs->esp;
Prasanna S Panchamukhi124d90b2006-02-24 13:04:08 -0800478 unsigned long copy_eip = (unsigned long)p->ainsn.insn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 unsigned long orig_eip = (unsigned long)p->addr;
480
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800481 regs->eflags &= ~TF_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 switch (p->ainsn.insn[0]) {
483 case 0x9c: /* pushfl */
484 *tos &= ~(TF_MASK | IF_MASK);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800485 *tos |= kcb->kprobe_old_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
Masami Hiramatsubcff5cd2006-04-27 18:39:55 -0700487 case 0xc2: /* iret/ret/lret */
488 case 0xc3:
Prasanna S Panchamukhi0b9e2ca2005-05-05 16:15:40 -0700489 case 0xca:
Masami Hiramatsubcff5cd2006-04-27 18:39:55 -0700490 case 0xcb:
491 case 0xcf:
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800492 case 0xea: /* jmp absolute -- eip is correct */
493 /* eip is already adjusted, no more changes required */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800494 p->ainsn.boostable = 1;
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800495 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 case 0xe8: /* call relative - Fix return addr */
497 *tos = orig_eip + (*tos - copy_eip);
498 break;
Masami Hiramatsubcff5cd2006-04-27 18:39:55 -0700499 case 0x9a: /* call absolute -- same as call absolute, indirect */
500 *tos = orig_eip + (*tos - copy_eip);
501 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 case 0xff:
503 if ((p->ainsn.insn[1] & 0x30) == 0x10) {
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800504 /*
Masami Hiramatsubcff5cd2006-04-27 18:39:55 -0700505 * call absolute, indirect
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800506 * Fix return addr; eip is correct.
507 * But this is not boostable
508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *tos = orig_eip + (*tos - copy_eip);
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800510 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else if (((p->ainsn.insn[1] & 0x31) == 0x20) || /* jmp near, absolute indirect */
512 ((p->ainsn.insn[1] & 0x31) == 0x21)) { /* jmp far, absolute indirect */
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800513 /* eip is correct. And this is boostable */
514 p->ainsn.boostable = 1;
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800515 goto no_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 default:
518 break;
519 }
520
Masami Hiramatsu311ac882006-03-26 01:38:17 -0800521 if (p->ainsn.boostable == 0) {
522 if ((regs->eip > copy_eip) &&
523 (regs->eip - copy_eip) + 5 < MAX_INSN_SIZE) {
524 /*
525 * These instructions can be executed directly if it
526 * jumps back to correct address.
527 */
528 set_jmp_op((void *)regs->eip,
529 (void *)orig_eip + (regs->eip - copy_eip));
530 p->ainsn.boostable = 1;
531 } else {
532 p->ainsn.boostable = -1;
533 }
534 }
535
Masami Hiramatsub50ea742006-03-26 01:38:13 -0800536 regs->eip = orig_eip + (regs->eip - copy_eip);
537
538no_change:
539 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542/*
543 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
Ananth N Mavinakayanahalli991a51d2005-11-07 01:00:14 -0800544 * remain disabled thoroughout this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 */
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700546static int __kprobes post_kprobe_handler(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800548 struct kprobe *cur = kprobe_running();
549 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
550
551 if (!cur)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return 0;
553
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800554 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
555 kcb->kprobe_status = KPROBE_HIT_SSDONE;
556 cur->post_handler(cur, regs, 0);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800559 resume_execution(cur, regs, kcb);
560 regs->eflags |= kcb->kprobe_saved_eflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700562 /*Restore back the original saved kprobes variables and continue. */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800563 if (kcb->kprobe_status == KPROBE_REENTER) {
564 restore_previous_kprobe(kcb);
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700565 goto out;
566 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800567 reset_current_kprobe();
Prasanna S Panchamukhi417c8da2005-06-23 00:09:37 -0700568out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 preempt_enable_no_resched();
570
571 /*
572 * if somebody else is singlestepping across a probe point, eflags
573 * will have TF set, in which case, continue the remaining processing
574 * of do_debug, as if this is not a probe hit.
575 */
576 if (regs->eflags & TF_MASK)
577 return 0;
578
579 return 1;
580}
581
Prasanna S Panchamukhi34c37e12006-04-18 22:21:59 -0700582static int __kprobes kprobe_fault_handler(struct pt_regs *regs, int trapnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800584 struct kprobe *cur = kprobe_running();
585 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
586
Prasanna S Panchamukhib4026512006-03-26 01:38:22 -0800587 switch(kcb->kprobe_status) {
588 case KPROBE_HIT_SS:
589 case KPROBE_REENTER:
590 /*
591 * We are here because the instruction being single
592 * stepped caused a page fault. We reset the current
593 * kprobe and the eip points back to the probe address
594 * and allow the page fault handler to continue as a
595 * normal page fault.
596 */
597 regs->eip = (unsigned long)cur->addr;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800598 regs->eflags |= kcb->kprobe_old_eflags;
Prasanna S Panchamukhib4026512006-03-26 01:38:22 -0800599 if (kcb->kprobe_status == KPROBE_REENTER)
600 restore_previous_kprobe(kcb);
601 else
602 reset_current_kprobe();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 preempt_enable_no_resched();
Prasanna S Panchamukhib4026512006-03-26 01:38:22 -0800604 break;
605 case KPROBE_HIT_ACTIVE:
606 case KPROBE_HIT_SSDONE:
607 /*
608 * We increment the nmissed count for accounting,
609 * we can also use npre/npostfault count for accouting
610 * these specific fault cases.
611 */
612 kprobes_inc_nmissed_count(cur);
613
614 /*
615 * We come here because instructions in the pre/post
616 * handler caused the page_fault, this could happen
617 * if handler tries to access user space by
618 * copy_from_user(), get_user() etc. Let the
619 * user-specified handler try to fix it first.
620 */
621 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
622 return 1;
623
624 /*
625 * In case the user-specified fault handler returned
626 * zero, try to fix up.
627 */
628 if (fixup_exception(regs))
629 return 1;
630
631 /*
632 * fixup_exception() could not handle it,
633 * Let do_page_fault() fix it.
634 */
635 break;
636 default:
637 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 return 0;
640}
641
642/*
643 * Wrapper routine to for handling exceptions.
644 */
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700645int __kprobes kprobe_exceptions_notify(struct notifier_block *self,
646 unsigned long val, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 struct die_args *args = (struct die_args *)data;
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800649 int ret = NOTIFY_DONE;
650
Zachary Amsden64445412006-06-23 02:04:30 -0700651 if (args->regs && user_mode_vm(args->regs))
bibo,mao2326c772006-03-26 01:38:21 -0800652 return ret;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 switch (val) {
655 case DIE_INT3:
656 if (kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800657 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
659 case DIE_DEBUG:
660 if (post_kprobe_handler(args->regs))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800661 ret = NOTIFY_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 case DIE_GPF:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 case DIE_PAGE_FAULT:
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800665 /* kprobe_running() needs smp_processor_id() */
666 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (kprobe_running() &&
668 kprobe_fault_handler(args->regs, args->trapnr))
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800669 ret = NOTIFY_STOP;
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800670 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 break;
672 default:
673 break;
674 }
Ananth N Mavinakayanahalli66ff2d02005-11-07 01:00:07 -0800675 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700678int __kprobes setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 struct jprobe *jp = container_of(p, struct jprobe, kp);
681 unsigned long addr;
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800682 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800684 kcb->jprobe_saved_regs = *regs;
685 kcb->jprobe_saved_esp = &regs->esp;
686 addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /*
689 * TBD: As Linus pointed out, gcc assumes that the callee
690 * owns the argument space and could overwrite it, e.g.
691 * tailcall optimization. So, to be absolutely safe
692 * we also save and restore enough stack bytes to cover
693 * the argument area.
694 */
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800695 memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr,
696 MIN_STACK_SIZE(addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 regs->eflags &= ~IF_MASK;
698 regs->eip = (unsigned long)(jp->entry);
699 return 1;
700}
701
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700702void __kprobes jprobe_return(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800704 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 asm volatile (" xchgl %%ebx,%%esp \n"
707 " int3 \n"
708 " .globl jprobe_return_end \n"
709 " jprobe_return_end: \n"
710 " nop \n"::"b"
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800711 (kcb->jprobe_saved_esp):"memory");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Prasanna S Panchamukhi3d97ae52005-09-06 15:19:27 -0700714int __kprobes longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800716 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 u8 *addr = (u8 *) (regs->eip - 1);
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800718 unsigned long stack_addr = (unsigned long)(kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 struct jprobe *jp = container_of(p, struct jprobe, kp);
720
721 if ((addr > (u8 *) jprobe_return) && (addr < (u8 *) jprobe_return_end)) {
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800722 if (&regs->esp != kcb->jprobe_saved_esp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct pt_regs *saved_regs =
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800724 container_of(kcb->jprobe_saved_esp,
725 struct pt_regs, esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 printk("current esp %p does not match saved esp %p\n",
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800727 &regs->esp, kcb->jprobe_saved_esp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 printk("Saved registers for jprobe %p\n", jp);
729 show_registers(saved_regs);
730 printk("Current registers\n");
731 show_registers(regs);
732 BUG();
733 }
Ananth N Mavinakayanahalli9a0e3a82005-11-07 01:00:08 -0800734 *regs = kcb->jprobe_saved_regs;
735 memcpy((kprobe_opcode_t *) stack_addr, kcb->jprobes_stack,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 MIN_STACK_SIZE(stack_addr));
Ananth N Mavinakayanahallid217d542005-11-07 01:00:14 -0800737 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return 1;
739 }
740 return 0;
741}
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700742
Rusty Lynch67729262005-07-05 18:54:50 -0700743int __init arch_init_kprobes(void)
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700744{
Masami Hiramatsuc9becf52006-03-26 01:38:19 -0800745 return 0;
Rusty Lynch4bdbd372005-06-27 15:17:09 -0700746}