2 * Kernel Probes (KProbes)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) IBM Corporation, 2002, 2004
20 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
21 * Probes initial implementation ( includes contributions from
23 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24 * interface to access function arguments.
25 * 2004-Oct Jim Keniston <kenistoj@us.ibm.com> and Prasanna S Panchamukhi
26 * <prasanna@in.ibm.com> adapted for x86_64
27 * 2005-Mar Roland McGrath <roland@redhat.com>
28 * Fixed to handle %rip-relative addressing mode correctly.
29 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
30 * Added function return probes functionality
31 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
32 * and kretprobe-booster for x86-64
35 #include <linux/kprobes.h>
36 #include <linux/ptrace.h>
37 #include <linux/string.h>
38 #include <linux/slab.h>
39 #include <linux/preempt.h>
40 #include <linux/module.h>
41 #include <linux/kdebug.h>
43 #include <asm/cacheflush.h>
45 #include <asm/pgtable.h>
46 #include <asm/uaccess.h>
47 #include <asm/alternative.h>
49 void jprobe_return_end(void);
51 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
) = NULL
;
52 DEFINE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
54 #define stack_addr(regs) ((unsigned long *)regs->sp)
56 #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
57 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
58 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
59 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
60 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
63 * Undefined/reserved opcodes, conditional jump, Opcode Extension
64 * Groups, and some special opcodes can not boost.
66 static const u32 twobyte_is_boostable
[256 / 32] = {
67 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
68 /* ---------------------------------------------- */
69 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
70 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 10 */
71 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
72 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
73 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
74 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
75 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
76 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
77 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
78 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
79 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
80 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
81 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
82 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
83 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
84 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
85 /* ----------------------------------------------- */
86 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
88 static const u32 onebyte_has_modrm
[256 / 32] = {
89 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
90 /* ----------------------------------------------- */
91 W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 00 */
92 W(0x10, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 10 */
93 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) | /* 20 */
94 W(0x30, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0) , /* 30 */
95 W(0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 40 */
96 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
97 W(0x60, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0) | /* 60 */
98 W(0x70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 70 */
99 W(0x80, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 80 */
100 W(0x90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 90 */
101 W(0xa0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* a0 */
102 W(0xb0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* b0 */
103 W(0xc0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* c0 */
104 W(0xd0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) , /* d0 */
105 W(0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* e0 */
106 W(0xf0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) /* f0 */
107 /* ----------------------------------------------- */
108 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
110 static const u32 twobyte_has_modrm
[256 / 32] = {
111 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
112 /* ----------------------------------------------- */
113 W(0x00, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1) | /* 0f */
114 W(0x10, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0) , /* 1f */
115 W(0x20, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* 2f */
116 W(0x30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 3f */
117 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 4f */
118 W(0x50, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 5f */
119 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 6f */
120 W(0x70, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1) , /* 7f */
121 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 8f */
122 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 9f */
123 W(0xa0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) | /* af */
124 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1) , /* bf */
125 W(0xc0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) | /* cf */
126 W(0xd0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* df */
127 W(0xe0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* ef */
128 W(0xf0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0) /* ff */
129 /* ----------------------------------------------- */
130 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
134 struct kretprobe_blackpoint kretprobe_blacklist
[] = {
135 {"__switch_to", }, /* This function switches only current task, but
136 doesn't switch kernel stack.*/
137 {NULL
, NULL
} /* Terminator */
139 const int kretprobe_blacklist_size
= ARRAY_SIZE(kretprobe_blacklist
);
141 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
142 static __always_inline
void set_jmp_op(void *from
, void *to
)
144 struct __arch_jmp_op
{
147 } __attribute__((packed
)) * jop
;
148 jop
= (struct __arch_jmp_op
*)from
;
149 jop
->raddr
= (s32
)((long)(to
) - ((long)(from
) + 5));
150 jop
->op
= RELATIVEJUMP_INSTRUCTION
;
154 * returns non-zero if opcode is boostable.
155 * RIP relative instructions are adjusted at copying time
157 static __always_inline
int can_boost(kprobe_opcode_t
*opcodes
)
159 kprobe_opcode_t opcode
;
160 kprobe_opcode_t
*orig_opcodes
= opcodes
;
163 if (opcodes
- orig_opcodes
> MAX_INSN_SIZE
- 1)
165 opcode
= *(opcodes
++);
167 /* 2nd-byte opcode */
168 if (opcode
== 0x0f) {
169 if (opcodes
- orig_opcodes
> MAX_INSN_SIZE
- 1)
171 return test_bit(*opcodes
,
172 (unsigned long *)twobyte_is_boostable
);
175 switch (opcode
& 0xf0) {
177 goto retry
; /* REX prefix is boostable */
179 if (0x63 < opcode
&& opcode
< 0x67)
180 goto retry
; /* prefixes */
181 /* can't boost Address-size override and bound */
182 return (opcode
!= 0x62 && opcode
!= 0x67);
184 return 0; /* can't boost conditional jump */
186 /* can't boost software-interruptions */
187 return (0xc1 < opcode
&& opcode
< 0xcc) || opcode
== 0xcf;
189 /* can boost AA* and XLAT */
190 return (opcode
== 0xd4 || opcode
== 0xd5 || opcode
== 0xd7);
192 /* can boost in/out and absolute jmps */
193 return ((opcode
& 0x04) || opcode
== 0xea);
195 if ((opcode
& 0x0c) == 0 && opcode
!= 0xf1)
196 goto retry
; /* lock/rep(ne) prefix */
197 /* clear and set flags are boostable */
198 return (opcode
== 0xf5 || (0xf7 < opcode
&& opcode
< 0xfe));
200 /* segment override prefixes are boostable */
201 if (opcode
== 0x26 || opcode
== 0x36 || opcode
== 0x3e)
202 goto retry
; /* prefixes */
203 /* CS override prefix and call are not boostable */
204 return (opcode
!= 0x2e && opcode
!= 0x9a);
209 * returns non-zero if opcode modifies the interrupt flag.
211 static int __kprobes
is_IF_modifier(kprobe_opcode_t
*insn
)
216 case 0xcf: /* iret/iretd */
217 case 0x9d: /* popf/popfd */
222 * on 64 bit x86, 0x40-0x4f are prefixes so we need to look
223 * at the next byte instead.. but of course not recurse infinitely
225 if (*insn
>= 0x40 && *insn
<= 0x4f)
226 return is_IF_modifier(++insn
);
231 * Adjust the displacement if the instruction uses the %rip-relative
233 * If it does, Return the address of the 32-bit displacement word.
234 * If not, return null.
236 static void __kprobes
fix_riprel(struct kprobe
*p
)
238 u8
*insn
= p
->ainsn
.insn
;
242 /* Skip legacy instruction prefixes. */
262 /* Skip REX instruction prefix. */
263 if ((*insn
& 0xf0) == 0x40)
266 if (*insn
== 0x0f) { /* Two-byte opcode. */
268 need_modrm
= test_bit(*insn
,
269 (unsigned long *)twobyte_has_modrm
);
270 } else /* One-byte opcode. */
271 need_modrm
= test_bit(*insn
,
272 (unsigned long *)onebyte_has_modrm
);
276 if ((modrm
& 0xc7) == 0x05) { /* %rip+disp32 addressing mode */
277 /* Displacement follows ModRM byte. */
280 * The copied instruction uses the %rip-relative
281 * addressing mode. Adjust the displacement for the
282 * difference between the original location of this
283 * instruction and the location of the copy that will
284 * actually be run. The tricky bit here is making sure
285 * that the sign extension happens correctly in this
286 * calculation, since we need a signed 32-bit result to
287 * be sign-extended to 64 bits when it's added to the
288 * %rip value and yield the same 64-bit result that the
289 * sign-extension of the original signed 32-bit
290 * displacement would have given.
292 disp
= (u8
*) p
->addr
+ *((s32
*) insn
) -
293 (u8
*) p
->ainsn
.insn
;
294 BUG_ON((s64
) (s32
) disp
!= disp
); /* Sanity check. */
295 *(s32
*)insn
= (s32
) disp
;
300 static void __kprobes
arch_copy_kprobe(struct kprobe
*p
)
302 memcpy(p
->ainsn
.insn
, p
->addr
, MAX_INSN_SIZE
* sizeof(kprobe_opcode_t
));
304 if (can_boost(p
->addr
))
305 p
->ainsn
.boostable
= 0;
307 p
->ainsn
.boostable
= -1;
309 p
->opcode
= *p
->addr
;
312 int __kprobes
arch_prepare_kprobe(struct kprobe
*p
)
314 /* insn: must be on special executable page on x86. */
315 p
->ainsn
.insn
= get_insn_slot();
322 void __kprobes
arch_arm_kprobe(struct kprobe
*p
)
324 text_poke(p
->addr
, ((unsigned char []){BREAKPOINT_INSTRUCTION
}), 1);
327 void __kprobes
arch_disarm_kprobe(struct kprobe
*p
)
329 text_poke(p
->addr
, &p
->opcode
, 1);
332 void __kprobes
arch_remove_kprobe(struct kprobe
*p
)
334 mutex_lock(&kprobe_mutex
);
335 free_insn_slot(p
->ainsn
.insn
, (p
->ainsn
.boostable
== 1));
336 mutex_unlock(&kprobe_mutex
);
339 static void __kprobes
save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
341 kcb
->prev_kprobe
.kp
= kprobe_running();
342 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
343 kcb
->prev_kprobe
.old_flags
= kcb
->kprobe_old_flags
;
344 kcb
->prev_kprobe
.saved_flags
= kcb
->kprobe_saved_flags
;
347 static void __kprobes
restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
349 __get_cpu_var(current_kprobe
) = kcb
->prev_kprobe
.kp
;
350 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
351 kcb
->kprobe_old_flags
= kcb
->prev_kprobe
.old_flags
;
352 kcb
->kprobe_saved_flags
= kcb
->prev_kprobe
.saved_flags
;
355 static void __kprobes
set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
356 struct kprobe_ctlblk
*kcb
)
358 __get_cpu_var(current_kprobe
) = p
;
359 kcb
->kprobe_saved_flags
= kcb
->kprobe_old_flags
360 = (regs
->flags
& (TF_MASK
| IF_MASK
));
361 if (is_IF_modifier(p
->ainsn
.insn
))
362 kcb
->kprobe_saved_flags
&= ~IF_MASK
;
365 static __always_inline
void clear_btf(void)
367 if (test_thread_flag(TIF_DEBUGCTLMSR
))
368 wrmsrl(MSR_IA32_DEBUGCTLMSR
, 0);
371 static __always_inline
void restore_btf(void)
373 if (test_thread_flag(TIF_DEBUGCTLMSR
))
374 wrmsrl(MSR_IA32_DEBUGCTLMSR
, current
->thread
.debugctlmsr
);
377 static void __kprobes
prepare_singlestep(struct kprobe
*p
, struct pt_regs
*regs
)
380 regs
->flags
|= TF_MASK
;
381 regs
->flags
&= ~IF_MASK
;
382 /*single step inline if the instruction is an int3*/
383 if (p
->opcode
== BREAKPOINT_INSTRUCTION
)
384 regs
->ip
= (unsigned long)p
->addr
;
386 regs
->ip
= (unsigned long)p
->ainsn
.insn
;
389 /* Called with kretprobe_lock held */
390 void __kprobes
arch_prepare_kretprobe(struct kretprobe_instance
*ri
,
391 struct pt_regs
*regs
)
393 unsigned long *sara
= stack_addr(regs
);
395 ri
->ret_addr
= (kprobe_opcode_t
*) *sara
;
397 /* Replace the return addr with trampoline addr */
398 *sara
= (unsigned long) &kretprobe_trampoline
;
402 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
403 * remain disabled thorough out this function.
405 static int __kprobes
kprobe_handler(struct pt_regs
*regs
)
409 kprobe_opcode_t
*addr
;
410 struct kprobe_ctlblk
*kcb
;
412 addr
= (kprobe_opcode_t
*)(regs
->ip
- sizeof(kprobe_opcode_t
));
415 * We don't want to be preempted for the entire
416 * duration of kprobe processing
419 kcb
= get_kprobe_ctlblk();
421 /* Check we're not actually recursing */
422 if (kprobe_running()) {
423 p
= get_kprobe(addr
);
425 if (kcb
->kprobe_status
== KPROBE_HIT_SS
&&
426 *p
->ainsn
.insn
== BREAKPOINT_INSTRUCTION
) {
427 regs
->flags
&= ~TF_MASK
;
428 regs
->flags
|= kcb
->kprobe_saved_flags
;
430 } else if (kcb
->kprobe_status
== KPROBE_HIT_SSDONE
) {
431 /* TODO: Provide re-entrancy from
432 * post_kprobes_handler() and avoid exception
433 * stack corruption while single-stepping on
434 * the instruction of the new probe.
436 arch_disarm_kprobe(p
);
437 regs
->ip
= (unsigned long)p
->addr
;
438 reset_current_kprobe();
441 /* We have reentered the kprobe_handler(), since
442 * another probe was hit while within the handler.
443 * We here save the original kprobes variables and
444 * just single step on the instruction of the new probe
445 * without calling any user handlers.
447 save_previous_kprobe(kcb
);
448 set_current_kprobe(p
, regs
, kcb
);
449 kprobes_inc_nmissed_count(p
);
450 prepare_singlestep(p
, regs
);
451 kcb
->kprobe_status
= KPROBE_REENTER
;
454 if (*addr
!= BREAKPOINT_INSTRUCTION
) {
455 /* The breakpoint instruction was removed by
456 * another cpu right after we hit, no further
457 * handling of this interrupt is appropriate
459 regs
->ip
= (unsigned long)addr
;
463 p
= __get_cpu_var(current_kprobe
);
464 if (p
->break_handler
&& p
->break_handler(p
, regs
)) {
471 p
= get_kprobe(addr
);
473 if (*addr
!= BREAKPOINT_INSTRUCTION
) {
475 * The breakpoint instruction was removed right
476 * after we hit it. Another cpu has removed
477 * either a probepoint or a debugger breakpoint
478 * at this address. In either case, no further
479 * handling of this interrupt is appropriate.
480 * Back up over the (now missing) int3 and run
481 * the original instruction.
483 regs
->ip
= (unsigned long)addr
;
486 /* Not one of ours: let kernel handle it */
490 set_current_kprobe(p
, regs
, kcb
);
491 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
493 if (p
->pre_handler
&& p
->pre_handler(p
, regs
))
494 /* handler has already set things up, so skip ss setup */
498 #if !defined(CONFIG_PREEMPT) || defined(CONFIG_PM)
499 if (p
->ainsn
.boostable
== 1 && !p
->post_handler
) {
500 /* Boost up -- we can execute copied instructions directly */
501 reset_current_kprobe();
502 regs
->ip
= (unsigned long)p
->ainsn
.insn
;
503 preempt_enable_no_resched();
507 prepare_singlestep(p
, regs
);
508 kcb
->kprobe_status
= KPROBE_HIT_SS
;
512 preempt_enable_no_resched();
517 * When a retprobed function returns, this code saves registers and
518 * calls trampoline_handler() runs, which calls the kretprobe's handler.
520 void __kprobes
kretprobe_trampoline_holder(void)
522 asm volatile ( ".global kretprobe_trampoline\n"
523 "kretprobe_trampoline: \n"
524 /* We don't bother saving the ss register */
528 * Skip cs, ip, orig_ax.
529 * trampoline_handler() will plug in these values
548 " call trampoline_handler\n"
549 /* Replace saved sp with true return address. */
550 " movq %rax, 152(%rsp)\n"
566 /* Skip orig_ax, ip, cs */
573 * Called from kretprobe_trampoline
575 fastcall
void * __kprobes
trampoline_handler(struct pt_regs
*regs
)
577 struct kretprobe_instance
*ri
= NULL
;
578 struct hlist_head
*head
, empty_rp
;
579 struct hlist_node
*node
, *tmp
;
580 unsigned long flags
, orig_ret_address
= 0;
581 unsigned long trampoline_address
=(unsigned long)&kretprobe_trampoline
;
583 INIT_HLIST_HEAD(&empty_rp
);
584 spin_lock_irqsave(&kretprobe_lock
, flags
);
585 head
= kretprobe_inst_table_head(current
);
586 /* fixup registers */
587 regs
->cs
= __KERNEL_CS
;
588 regs
->ip
= trampoline_address
;
589 regs
->orig_ax
= ~0UL;
592 * It is possible to have multiple instances associated with a given
593 * task either because multiple functions in the call path have
594 * return probes installed on them, and/or more then one
595 * return probe was registered for a target function.
597 * We can handle this because:
598 * - instances are always pushed into the head of the list
599 * - when multiple return probes are registered for the same
600 * function, the (chronologically) first instance's ret_addr
601 * will be the real return address, and all the rest will
602 * point to kretprobe_trampoline.
604 hlist_for_each_entry_safe(ri
, node
, tmp
, head
, hlist
) {
605 if (ri
->task
!= current
)
606 /* another task is sharing our hash bucket */
609 if (ri
->rp
&& ri
->rp
->handler
) {
610 __get_cpu_var(current_kprobe
) = &ri
->rp
->kp
;
611 get_kprobe_ctlblk()->kprobe_status
= KPROBE_HIT_ACTIVE
;
612 ri
->rp
->handler(ri
, regs
);
613 __get_cpu_var(current_kprobe
) = NULL
;
616 orig_ret_address
= (unsigned long)ri
->ret_addr
;
617 recycle_rp_inst(ri
, &empty_rp
);
619 if (orig_ret_address
!= trampoline_address
)
621 * This is the real return address. Any other
622 * instances associated with this task are for
623 * other calls deeper on the call stack
628 kretprobe_assert(ri
, orig_ret_address
, trampoline_address
);
630 spin_unlock_irqrestore(&kretprobe_lock
, flags
);
632 hlist_for_each_entry_safe(ri
, node
, tmp
, &empty_rp
, hlist
) {
633 hlist_del(&ri
->hlist
);
636 return (void *)orig_ret_address
;
640 * Called after single-stepping. p->addr is the address of the
641 * instruction whose first byte has been replaced by the "int 3"
642 * instruction. To avoid the SMP problems that can occur when we
643 * temporarily put back the original opcode to single-step, we
644 * single-stepped a copy of the instruction. The address of this
645 * copy is p->ainsn.insn.
647 * This function prepares to return from the post-single-step
648 * interrupt. We have to fix up the stack as follows:
650 * 0) Except in the case of absolute or indirect jump or call instructions,
651 * the new ip is relative to the copied instruction. We need to make
652 * it relative to the original instruction.
654 * 1) If the single-stepped instruction was pushfl, then the TF and IF
655 * flags are set in the just-pushed flags, and may need to be cleared.
657 * 2) If the single-stepped instruction was a call, the return address
658 * that is atop the stack is the address following the copied instruction.
659 * We need to make it the address following the original instruction.
661 * If this is the first time we've single-stepped the instruction at
662 * this probepoint, and the instruction is boostable, boost it: add a
663 * jump instruction after the copied instruction, that jumps to the next
664 * instruction after the probepoint.
666 static void __kprobes
resume_execution(struct kprobe
*p
,
667 struct pt_regs
*regs
, struct kprobe_ctlblk
*kcb
)
669 unsigned long *tos
= stack_addr(regs
);
670 unsigned long copy_ip
= (unsigned long)p
->ainsn
.insn
;
671 unsigned long orig_ip
= (unsigned long)p
->addr
;
672 kprobe_opcode_t
*insn
= p
->ainsn
.insn
;
674 /*skip the REX prefix*/
675 if (*insn
>= 0x40 && *insn
<= 0x4f)
678 regs
->flags
&= ~TF_MASK
;
680 case 0x9c: /* pushfl */
681 *tos
&= ~(TF_MASK
| IF_MASK
);
682 *tos
|= kcb
->kprobe_old_flags
;
684 case 0xc2: /* iret/ret/lret */
689 case 0xea: /* jmp absolute -- ip is correct */
690 /* ip is already adjusted, no more changes required */
691 p
->ainsn
.boostable
= 1;
693 case 0xe8: /* call relative - Fix return addr */
694 *tos
= orig_ip
+ (*tos
- copy_ip
);
697 if ((insn
[1] & 0x30) == 0x10) {
699 * call absolute, indirect
700 * Fix return addr; ip is correct.
701 * But this is not boostable
703 *tos
= orig_ip
+ (*tos
- copy_ip
);
705 } else if (((insn
[1] & 0x31) == 0x20) ||
706 ((insn
[1] & 0x31) == 0x21)) {
708 * jmp near and far, absolute indirect
709 * ip is correct. And this is boostable
711 p
->ainsn
.boostable
= 1;
718 if (p
->ainsn
.boostable
== 0) {
719 if ((regs
->ip
> copy_ip
) &&
720 (regs
->ip
- copy_ip
) + 5 < MAX_INSN_SIZE
) {
722 * These instructions can be executed directly if it
723 * jumps back to correct address.
725 set_jmp_op((void *)regs
->ip
,
726 (void *)orig_ip
+ (regs
->ip
- copy_ip
));
727 p
->ainsn
.boostable
= 1;
729 p
->ainsn
.boostable
= -1;
733 regs
->ip
+= orig_ip
- copy_ip
;
742 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
743 * remain disabled thoroughout this function.
745 static int __kprobes
post_kprobe_handler(struct pt_regs
*regs
)
747 struct kprobe
*cur
= kprobe_running();
748 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
753 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
754 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
755 cur
->post_handler(cur
, regs
, 0);
758 resume_execution(cur
, regs
, kcb
);
759 regs
->flags
|= kcb
->kprobe_saved_flags
;
760 trace_hardirqs_fixup_flags(regs
->flags
);
762 /* Restore back the original saved kprobes variables and continue. */
763 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
764 restore_previous_kprobe(kcb
);
767 reset_current_kprobe();
769 preempt_enable_no_resched();
772 * if somebody else is singlestepping across a probe point, flags
773 * will have TF set, in which case, continue the remaining processing
774 * of do_debug, as if this is not a probe hit.
776 if (regs
->flags
& TF_MASK
)
782 int __kprobes
kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
784 struct kprobe
*cur
= kprobe_running();
785 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
786 const struct exception_table_entry
*fixup
;
788 switch(kcb
->kprobe_status
) {
792 * We are here because the instruction being single
793 * stepped caused a page fault. We reset the current
794 * kprobe and the ip points back to the probe address
795 * and allow the page fault handler to continue as a
798 regs
->ip
= (unsigned long)cur
->addr
;
799 regs
->flags
|= kcb
->kprobe_old_flags
;
800 if (kcb
->kprobe_status
== KPROBE_REENTER
)
801 restore_previous_kprobe(kcb
);
803 reset_current_kprobe();
804 preempt_enable_no_resched();
806 case KPROBE_HIT_ACTIVE
:
807 case KPROBE_HIT_SSDONE
:
809 * We increment the nmissed count for accounting,
810 * we can also use npre/npostfault count for accounting
811 * these specific fault cases.
813 kprobes_inc_nmissed_count(cur
);
816 * We come here because instructions in the pre/post
817 * handler caused the page_fault, this could happen
818 * if handler tries to access user space by
819 * copy_from_user(), get_user() etc. Let the
820 * user-specified handler try to fix it first.
822 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
826 * In case the user-specified fault handler returned
827 * zero, try to fix up.
829 fixup
= search_exception_tables(regs
->ip
);
831 regs
->ip
= fixup
->fixup
;
836 * fixup routine could not handle it,
837 * Let do_page_fault() fix it.
847 * Wrapper routine for handling exceptions.
849 int __kprobes
kprobe_exceptions_notify(struct notifier_block
*self
,
850 unsigned long val
, void *data
)
852 struct die_args
*args
= (struct die_args
*)data
;
853 int ret
= NOTIFY_DONE
;
855 if (args
->regs
&& user_mode_vm(args
->regs
))
860 if (kprobe_handler(args
->regs
))
864 if (post_kprobe_handler(args
->regs
))
868 /* kprobe_running() needs smp_processor_id() */
870 if (kprobe_running() &&
871 kprobe_fault_handler(args
->regs
, args
->trapnr
))
881 int __kprobes
setjmp_pre_handler(struct kprobe
*p
, struct pt_regs
*regs
)
883 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
885 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
887 kcb
->jprobe_saved_regs
= *regs
;
888 kcb
->jprobe_saved_sp
= stack_addr(regs
);
889 addr
= (unsigned long)(kcb
->jprobe_saved_sp
);
892 * As Linus pointed out, gcc assumes that the callee
893 * owns the argument space and could overwrite it, e.g.
894 * tailcall optimization. So, to be absolutely safe
895 * we also save and restore enough stack bytes to cover
898 memcpy(kcb
->jprobes_stack
, (kprobe_opcode_t
*)addr
,
899 MIN_STACK_SIZE(addr
));
900 regs
->flags
&= ~IF_MASK
;
901 trace_hardirqs_off();
902 regs
->ip
= (unsigned long)(jp
->entry
);
906 void __kprobes
jprobe_return(void)
908 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
910 asm volatile (" xchg %%rbx,%%rsp \n"
912 " .globl jprobe_return_end \n"
913 " jprobe_return_end: \n"
915 (kcb
->jprobe_saved_sp
):"memory");
918 int __kprobes
longjmp_break_handler(struct kprobe
*p
, struct pt_regs
*regs
)
920 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
921 u8
*addr
= (u8
*) (regs
->ip
- 1);
922 struct jprobe
*jp
= container_of(p
, struct jprobe
, kp
);
924 if ((addr
> (u8
*) jprobe_return
) && (addr
< (u8
*) jprobe_return_end
)) {
925 if (stack_addr(regs
) != kcb
->jprobe_saved_sp
) {
926 struct pt_regs
*saved_regs
= &kcb
->jprobe_saved_regs
;
927 printk("current sp %p does not match saved sp %p\n",
928 stack_addr(regs
), kcb
->jprobe_saved_sp
);
929 printk("Saved registers for jprobe %p\n", jp
);
930 show_registers(saved_regs
);
931 printk("Current registers\n");
932 show_registers(regs
);
935 *regs
= kcb
->jprobe_saved_regs
;
936 memcpy((kprobe_opcode_t
*)(kcb
->jprobe_saved_sp
),
938 MIN_STACK_SIZE(kcb
->jprobe_saved_sp
));
939 preempt_enable_no_resched();
945 int __init
arch_init_kprobes(void)
950 int __kprobes
arch_trampoline_kprobe(struct kprobe
*p
)