1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Kernel Probes (KProbes)
5 * Copyright (C) IBM Corporation, 2002, 2004
7 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
8 * Probes initial implementation ( includes contributions from
10 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
11 * interface to access function arguments.
12 * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
13 * <prasanna@in.ibm.com> adapted for x86_64 from i386.
14 * 2005-Mar Roland McGrath <roland@redhat.com>
15 * Fixed to handle %rip-relative addressing mode correctly.
16 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
17 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
18 * <prasanna@in.ibm.com> added function-return probes.
19 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
20 * Added function return probes functionality
21 * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
22 * kprobe-booster and kretprobe-booster for i386.
23 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
24 * and kretprobe-booster for x86-64
25 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
26 * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
27 * unified x86 kprobes code.
29 #include <linux/kprobes.h>
30 #include <linux/ptrace.h>
31 #include <linux/string.h>
32 #include <linux/slab.h>
33 #include <linux/hardirq.h>
34 #include <linux/preempt.h>
35 #include <linux/sched/debug.h>
36 #include <linux/extable.h>
37 #include <linux/kdebug.h>
38 #include <linux/kallsyms.h>
39 #include <linux/ftrace.h>
40 #include <linux/frame.h>
41 #include <linux/kasan.h>
42 #include <linux/moduleloader.h>
43 #include <linux/vmalloc.h>
45 #include <asm/text-patching.h>
46 #include <asm/cacheflush.h>
48 #include <asm/pgtable.h>
49 #include <linux/uaccess.h>
50 #include <asm/alternative.h>
52 #include <asm/debugreg.h>
53 #include <asm/set_memory.h>
57 DEFINE_PER_CPU(struct kprobe
*, current_kprobe
) = NULL
;
58 DEFINE_PER_CPU(struct kprobe_ctlblk
, kprobe_ctlblk
);
60 #define stack_addr(regs) ((unsigned long *)regs->sp)
62 #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
63 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
64 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
65 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
66 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
69 * Undefined/reserved opcodes, conditional jump, Opcode Extension
70 * Groups, and some special opcodes can not boost.
71 * This is non-const and volatile to keep gcc from statically
72 * optimizing it out, as variable_test_bit makes gcc think only
73 * *(unsigned long*) is used.
75 static volatile u32 twobyte_is_boostable
[256 / 32] = {
76 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
77 /* ---------------------------------------------- */
78 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
79 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) , /* 10 */
80 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
81 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
82 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
83 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
84 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
85 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
86 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
87 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
88 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
89 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
90 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
91 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
92 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
93 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
94 /* ----------------------------------------------- */
95 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
99 struct kretprobe_blackpoint kretprobe_blacklist
[] = {
100 {"__switch_to", }, /* This function switches only current task, but
101 doesn't switch kernel stack.*/
102 {NULL
, NULL
} /* Terminator */
105 const int kretprobe_blacklist_size
= ARRAY_SIZE(kretprobe_blacklist
);
107 static nokprobe_inline
void
108 __synthesize_relative_insn(void *dest
, void *from
, void *to
, u8 op
)
110 struct __arch_relative_insn
{
115 insn
= (struct __arch_relative_insn
*)dest
;
116 insn
->raddr
= (s32
)((long)(to
) - ((long)(from
) + 5));
120 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
121 void synthesize_reljump(void *dest
, void *from
, void *to
)
123 __synthesize_relative_insn(dest
, from
, to
, JMP32_INSN_OPCODE
);
125 NOKPROBE_SYMBOL(synthesize_reljump
);
127 /* Insert a call instruction at address 'from', which calls address 'to'.*/
128 void synthesize_relcall(void *dest
, void *from
, void *to
)
130 __synthesize_relative_insn(dest
, from
, to
, CALL_INSN_OPCODE
);
132 NOKPROBE_SYMBOL(synthesize_relcall
);
135 * Skip the prefixes of the instruction.
137 static kprobe_opcode_t
*skip_prefixes(kprobe_opcode_t
*insn
)
141 attr
= inat_get_opcode_attribute((insn_byte_t
)*insn
);
142 while (inat_is_legacy_prefix(attr
)) {
144 attr
= inat_get_opcode_attribute((insn_byte_t
)*insn
);
147 if (inat_is_rex_prefix(attr
))
152 NOKPROBE_SYMBOL(skip_prefixes
);
155 * Returns non-zero if INSN is boostable.
156 * RIP relative instructions are adjusted at copying time in 64 bits mode
158 int can_boost(struct insn
*insn
, void *addr
)
160 kprobe_opcode_t opcode
;
162 if (search_exception_tables((unsigned long)addr
))
163 return 0; /* Page fault may occur on this address. */
165 /* 2nd-byte opcode */
166 if (insn
->opcode
.nbytes
== 2)
167 return test_bit(insn
->opcode
.bytes
[1],
168 (unsigned long *)twobyte_is_boostable
);
170 if (insn
->opcode
.nbytes
!= 1)
173 /* Can't boost Address-size override prefix */
174 if (unlikely(inat_is_address_size_prefix(insn
->attr
)))
177 opcode
= insn
->opcode
.bytes
[0];
179 switch (opcode
& 0xf0) {
181 /* can't boost "bound" */
182 return (opcode
!= 0x62);
184 return 0; /* can't boost conditional jump */
186 return opcode
!= 0x9a; /* can't boost call far */
188 /* can't boost software-interruptions */
189 return (0xc1 < opcode
&& opcode
< 0xcc) || opcode
== 0xcf;
191 /* can boost AA* and XLAT */
192 return (opcode
== 0xd4 || opcode
== 0xd5 || opcode
== 0xd7);
194 /* can boost in/out and absolute jmps */
195 return ((opcode
& 0x04) || opcode
== 0xea);
197 /* clear and set flags are boostable */
198 return (opcode
== 0xf5 || (0xf7 < opcode
&& opcode
< 0xfe));
200 /* CS override prefix and call are not boostable */
201 return (opcode
!= 0x2e && opcode
!= 0x9a);
206 __recover_probed_insn(kprobe_opcode_t
*buf
, unsigned long addr
)
211 kp
= get_kprobe((void *)addr
);
212 faddr
= ftrace_location(addr
);
214 * Addresses inside the ftrace location are refused by
215 * arch_check_ftrace_location(). Something went terribly wrong
216 * if such an address is checked here.
218 if (WARN_ON(faddr
&& faddr
!= addr
))
221 * Use the current code if it is not modified by Kprobe
222 * and it cannot be modified by ftrace.
228 * Basically, kp->ainsn.insn has an original instruction.
229 * However, RIP-relative instruction can not do single-stepping
230 * at different place, __copy_instruction() tweaks the displacement of
231 * that instruction. In that case, we can't recover the instruction
232 * from the kp->ainsn.insn.
234 * On the other hand, in case on normal Kprobe, kp->opcode has a copy
235 * of the first byte of the probed instruction, which is overwritten
236 * by int3. And the instruction at kp->addr is not modified by kprobes
237 * except for the first byte, we can recover the original instruction
238 * from it and kp->opcode.
240 * In case of Kprobes using ftrace, we do not have a copy of
241 * the original instruction. In fact, the ftrace location might
242 * be modified at anytime and even could be in an inconsistent state.
243 * Fortunately, we know that the original code is the ideal 5-byte
246 if (probe_kernel_read(buf
, (void *)addr
,
247 MAX_INSN_SIZE
* sizeof(kprobe_opcode_t
)))
251 memcpy(buf
, ideal_nops
[NOP_ATOMIC5
], 5);
254 return (unsigned long)buf
;
258 * Recover the probed instruction at addr for further analysis.
259 * Caller must lock kprobes by kprobe_mutex, or disable preemption
260 * for preventing to release referencing kprobes.
261 * Returns zero if the instruction can not get recovered (or access failed).
263 unsigned long recover_probed_instruction(kprobe_opcode_t
*buf
, unsigned long addr
)
265 unsigned long __addr
;
267 __addr
= __recover_optprobed_insn(buf
, addr
);
271 return __recover_probed_insn(buf
, addr
);
274 /* Check if paddr is at an instruction boundary */
275 static int can_probe(unsigned long paddr
)
277 unsigned long addr
, __addr
, offset
= 0;
279 kprobe_opcode_t buf
[MAX_INSN_SIZE
];
281 if (!kallsyms_lookup_size_offset(paddr
, NULL
, &offset
))
284 /* Decode instructions */
285 addr
= paddr
- offset
;
286 while (addr
< paddr
) {
288 * Check if the instruction has been modified by another
289 * kprobe, in which case we replace the breakpoint by the
290 * original instruction in our buffer.
291 * Also, jump optimization will change the breakpoint to
292 * relative-jump. Since the relative-jump itself is
293 * normally used, we just go through if there is no kprobe.
295 __addr
= recover_probed_instruction(buf
, addr
);
298 kernel_insn_init(&insn
, (void *)__addr
, MAX_INSN_SIZE
);
299 insn_get_length(&insn
);
302 * Another debugging subsystem might insert this breakpoint.
303 * In that case, we can't recover it.
305 if (insn
.opcode
.bytes
[0] == INT3_INSN_OPCODE
)
310 return (addr
== paddr
);
314 * Returns non-zero if opcode modifies the interrupt flag.
316 static int is_IF_modifier(kprobe_opcode_t
*insn
)
319 insn
= skip_prefixes(insn
);
324 case 0xcf: /* iret/iretd */
325 case 0x9d: /* popf/popfd */
333 * Copy an instruction with recovering modified instruction by kprobes
334 * and adjust the displacement if the instruction uses the %rip-relative
335 * addressing mode. Note that since @real will be the final place of copied
336 * instruction, displacement must be adjust by @real, not @dest.
337 * This returns the length of copied instruction, or 0 if it has an error.
339 int __copy_instruction(u8
*dest
, u8
*src
, u8
*real
, struct insn
*insn
)
341 kprobe_opcode_t buf
[MAX_INSN_SIZE
];
342 unsigned long recovered_insn
=
343 recover_probed_instruction(buf
, (unsigned long)src
);
345 if (!recovered_insn
|| !insn
)
348 /* This can access kernel text if given address is not recovered */
349 if (probe_kernel_read(dest
, (void *)recovered_insn
, MAX_INSN_SIZE
))
352 kernel_insn_init(insn
, dest
, MAX_INSN_SIZE
);
353 insn_get_length(insn
);
355 /* We can not probe force emulate prefixed instruction */
356 if (insn_has_emulate_prefix(insn
))
359 /* Another subsystem puts a breakpoint, failed to recover */
360 if (insn
->opcode
.bytes
[0] == INT3_INSN_OPCODE
)
363 /* We should not singlestep on the exception masking instructions */
364 if (insn_masking_exception(insn
))
368 /* Only x86_64 has RIP relative instructions */
369 if (insn_rip_relative(insn
)) {
373 * The copied instruction uses the %rip-relative addressing
374 * mode. Adjust the displacement for the difference between
375 * the original location of this instruction and the location
376 * of the copy that will actually be run. The tricky bit here
377 * is making sure that the sign extension happens correctly in
378 * this calculation, since we need a signed 32-bit result to
379 * be sign-extended to 64 bits when it's added to the %rip
380 * value and yield the same 64-bit result that the sign-
381 * extension of the original signed 32-bit displacement would
384 newdisp
= (u8
*) src
+ (s64
) insn
->displacement
.value
386 if ((s64
) (s32
) newdisp
!= newdisp
) {
387 pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp
);
390 disp
= (u8
*) dest
+ insn_offset_displacement(insn
);
391 *(s32
*) disp
= (s32
) newdisp
;
397 /* Prepare reljump right after instruction to boost */
398 static int prepare_boost(kprobe_opcode_t
*buf
, struct kprobe
*p
,
401 int len
= insn
->length
;
403 if (can_boost(insn
, p
->addr
) &&
404 MAX_INSN_SIZE
- len
>= JMP32_INSN_SIZE
) {
406 * These instructions can be executed directly if it
407 * jumps back to correct address.
409 synthesize_reljump(buf
+ len
, p
->ainsn
.insn
+ len
,
410 p
->addr
+ insn
->length
);
411 len
+= JMP32_INSN_SIZE
;
412 p
->ainsn
.boostable
= true;
414 p
->ainsn
.boostable
= false;
420 /* Make page to RO mode when allocate it */
421 void *alloc_insn_page(void)
425 page
= module_alloc(PAGE_SIZE
);
429 set_vm_flush_reset_perms(page
);
431 * First make the page read-only, and only then make it executable to
432 * prevent it from being W+X in between.
434 set_memory_ro((unsigned long)page
, 1);
437 * TODO: Once additional kernel code protection mechanisms are set, ensure
438 * that the page was not maliciously altered and it is still zeroed.
440 set_memory_x((unsigned long)page
, 1);
445 /* Recover page to RW mode before releasing it */
446 void free_insn_page(void *page
)
448 module_memfree(page
);
451 static int arch_copy_kprobe(struct kprobe
*p
)
454 kprobe_opcode_t buf
[MAX_INSN_SIZE
];
457 /* Copy an instruction with recovering if other optprobe modifies it.*/
458 len
= __copy_instruction(buf
, p
->addr
, p
->ainsn
.insn
, &insn
);
463 * __copy_instruction can modify the displacement of the instruction,
464 * but it doesn't affect boostable check.
466 len
= prepare_boost(buf
, p
, &insn
);
468 /* Check whether the instruction modifies Interrupt Flag or not */
469 p
->ainsn
.if_modifier
= is_IF_modifier(buf
);
471 /* Also, displacement change doesn't affect the first byte */
474 /* OK, write back the instruction(s) into ROX insn buffer */
475 text_poke(p
->ainsn
.insn
, buf
, len
);
480 int arch_prepare_kprobe(struct kprobe
*p
)
484 if (alternatives_text_reserved(p
->addr
, p
->addr
))
487 if (!can_probe((unsigned long)p
->addr
))
489 /* insn: must be on special executable page on x86. */
490 p
->ainsn
.insn
= get_insn_slot();
494 ret
= arch_copy_kprobe(p
);
496 free_insn_slot(p
->ainsn
.insn
, 0);
497 p
->ainsn
.insn
= NULL
;
503 void arch_arm_kprobe(struct kprobe
*p
)
505 text_poke(p
->addr
, ((unsigned char []){INT3_INSN_OPCODE
}), 1);
509 void arch_disarm_kprobe(struct kprobe
*p
)
511 text_poke(p
->addr
, &p
->opcode
, 1);
515 void arch_remove_kprobe(struct kprobe
*p
)
518 free_insn_slot(p
->ainsn
.insn
, p
->ainsn
.boostable
);
519 p
->ainsn
.insn
= NULL
;
523 static nokprobe_inline
void
524 save_previous_kprobe(struct kprobe_ctlblk
*kcb
)
526 kcb
->prev_kprobe
.kp
= kprobe_running();
527 kcb
->prev_kprobe
.status
= kcb
->kprobe_status
;
528 kcb
->prev_kprobe
.old_flags
= kcb
->kprobe_old_flags
;
529 kcb
->prev_kprobe
.saved_flags
= kcb
->kprobe_saved_flags
;
532 static nokprobe_inline
void
533 restore_previous_kprobe(struct kprobe_ctlblk
*kcb
)
535 __this_cpu_write(current_kprobe
, kcb
->prev_kprobe
.kp
);
536 kcb
->kprobe_status
= kcb
->prev_kprobe
.status
;
537 kcb
->kprobe_old_flags
= kcb
->prev_kprobe
.old_flags
;
538 kcb
->kprobe_saved_flags
= kcb
->prev_kprobe
.saved_flags
;
541 static nokprobe_inline
void
542 set_current_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
543 struct kprobe_ctlblk
*kcb
)
545 __this_cpu_write(current_kprobe
, p
);
546 kcb
->kprobe_saved_flags
= kcb
->kprobe_old_flags
547 = (regs
->flags
& (X86_EFLAGS_TF
| X86_EFLAGS_IF
));
548 if (p
->ainsn
.if_modifier
)
549 kcb
->kprobe_saved_flags
&= ~X86_EFLAGS_IF
;
552 static nokprobe_inline
void clear_btf(void)
554 if (test_thread_flag(TIF_BLOCKSTEP
)) {
555 unsigned long debugctl
= get_debugctlmsr();
557 debugctl
&= ~DEBUGCTLMSR_BTF
;
558 update_debugctlmsr(debugctl
);
562 static nokprobe_inline
void restore_btf(void)
564 if (test_thread_flag(TIF_BLOCKSTEP
)) {
565 unsigned long debugctl
= get_debugctlmsr();
567 debugctl
|= DEBUGCTLMSR_BTF
;
568 update_debugctlmsr(debugctl
);
572 void arch_prepare_kretprobe(struct kretprobe_instance
*ri
, struct pt_regs
*regs
)
574 unsigned long *sara
= stack_addr(regs
);
576 ri
->ret_addr
= (kprobe_opcode_t
*) *sara
;
579 /* Replace the return addr with trampoline addr */
580 *sara
= (unsigned long) &kretprobe_trampoline
;
582 NOKPROBE_SYMBOL(arch_prepare_kretprobe
);
584 static void setup_singlestep(struct kprobe
*p
, struct pt_regs
*regs
,
585 struct kprobe_ctlblk
*kcb
, int reenter
)
587 if (setup_detour_execution(p
, regs
, reenter
))
590 #if !defined(CONFIG_PREEMPTION)
591 if (p
->ainsn
.boostable
&& !p
->post_handler
) {
592 /* Boost up -- we can execute copied instructions directly */
594 reset_current_kprobe();
596 * Reentering boosted probe doesn't reset current_kprobe,
597 * nor set current_kprobe, because it doesn't use single
600 regs
->ip
= (unsigned long)p
->ainsn
.insn
;
605 save_previous_kprobe(kcb
);
606 set_current_kprobe(p
, regs
, kcb
);
607 kcb
->kprobe_status
= KPROBE_REENTER
;
609 kcb
->kprobe_status
= KPROBE_HIT_SS
;
610 /* Prepare real single stepping */
612 regs
->flags
|= X86_EFLAGS_TF
;
613 regs
->flags
&= ~X86_EFLAGS_IF
;
614 /* single step inline if the instruction is an int3 */
615 if (p
->opcode
== INT3_INSN_OPCODE
)
616 regs
->ip
= (unsigned long)p
->addr
;
618 regs
->ip
= (unsigned long)p
->ainsn
.insn
;
620 NOKPROBE_SYMBOL(setup_singlestep
);
623 * We have reentered the kprobe_handler(), since another probe was hit while
624 * within the handler. We save the original kprobes variables and just single
625 * step on the instruction of the new probe without calling any user handlers.
627 static int reenter_kprobe(struct kprobe
*p
, struct pt_regs
*regs
,
628 struct kprobe_ctlblk
*kcb
)
630 switch (kcb
->kprobe_status
) {
631 case KPROBE_HIT_SSDONE
:
632 case KPROBE_HIT_ACTIVE
:
634 kprobes_inc_nmissed_count(p
);
635 setup_singlestep(p
, regs
, kcb
, 1);
638 /* A probe has been hit in the codepath leading up to, or just
639 * after, single-stepping of a probed instruction. This entire
640 * codepath should strictly reside in .kprobes.text section.
641 * Raise a BUG or we'll continue in an endless reentering loop
642 * and eventually a stack overflow.
644 pr_err("Unrecoverable kprobe detected.\n");
648 /* impossible cases */
655 NOKPROBE_SYMBOL(reenter_kprobe
);
658 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
659 * remain disabled throughout this function.
661 int kprobe_int3_handler(struct pt_regs
*regs
)
663 kprobe_opcode_t
*addr
;
665 struct kprobe_ctlblk
*kcb
;
670 addr
= (kprobe_opcode_t
*)(regs
->ip
- sizeof(kprobe_opcode_t
));
672 * We don't want to be preempted for the entire duration of kprobe
673 * processing. Since int3 and debug trap disables irqs and we clear
674 * IF while singlestepping, it must be no preemptible.
677 kcb
= get_kprobe_ctlblk();
678 p
= get_kprobe(addr
);
681 if (kprobe_running()) {
682 if (reenter_kprobe(p
, regs
, kcb
))
685 set_current_kprobe(p
, regs
, kcb
);
686 kcb
->kprobe_status
= KPROBE_HIT_ACTIVE
;
689 * If we have no pre-handler or it returned 0, we
690 * continue with normal processing. If we have a
691 * pre-handler and it returned non-zero, that means
692 * user handler setup registers to exit to another
693 * instruction, we must skip the single stepping.
695 if (!p
->pre_handler
|| !p
->pre_handler(p
, regs
))
696 setup_singlestep(p
, regs
, kcb
, 0);
698 reset_current_kprobe();
701 } else if (*addr
!= INT3_INSN_OPCODE
) {
703 * The breakpoint instruction was removed right
704 * after we hit it. Another cpu has removed
705 * either a probepoint or a debugger breakpoint
706 * at this address. In either case, no further
707 * handling of this interrupt is appropriate.
708 * Back up over the (now missing) int3 and run
709 * the original instruction.
711 regs
->ip
= (unsigned long)addr
;
713 } /* else: not a kprobe fault; let the kernel handle it */
717 NOKPROBE_SYMBOL(kprobe_int3_handler
);
720 * When a retprobed function returns, this code saves registers and
721 * calls trampoline_handler() runs, which calls the kretprobe's handler.
725 ".global kretprobe_trampoline\n"
726 ".type kretprobe_trampoline, @function\n"
727 "kretprobe_trampoline:\n"
728 /* We don't bother saving the ss register */
734 " call trampoline_handler\n"
735 /* Replace saved sp with true return address. */
736 " movq %rax, 19*8(%rsp)\n"
744 " call trampoline_handler\n"
745 /* Replace saved sp with true return address. */
746 " movl %eax, 15*4(%esp)\n"
751 ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
753 NOKPROBE_SYMBOL(kretprobe_trampoline
);
754 STACK_FRAME_NON_STANDARD(kretprobe_trampoline
);
757 * Called from kretprobe_trampoline
759 __used __visible
void *trampoline_handler(struct pt_regs
*regs
)
761 struct kretprobe_instance
*ri
= NULL
;
762 struct hlist_head
*head
, empty_rp
;
763 struct hlist_node
*tmp
;
764 unsigned long flags
, orig_ret_address
= 0;
765 unsigned long trampoline_address
= (unsigned long)&kretprobe_trampoline
;
766 kprobe_opcode_t
*correct_ret_addr
= NULL
;
768 bool skipped
= false;
771 * Set a dummy kprobe for avoiding kretprobe recursion.
772 * Since kretprobe never run in kprobe handler, kprobe must not
773 * be running at this point.
777 INIT_HLIST_HEAD(&empty_rp
);
778 kretprobe_hash_lock(current
, &head
, &flags
);
779 /* fixup registers */
780 regs
->cs
= __KERNEL_CS
;
782 regs
->cs
|= get_kernel_rpl();
785 /* We use pt_regs->sp for return address holder. */
786 frame_pointer
= ®s
->sp
;
787 regs
->ip
= trampoline_address
;
788 regs
->orig_ax
= ~0UL;
791 * It is possible to have multiple instances associated with a given
792 * task either because multiple functions in the call path have
793 * return probes installed on them, and/or more than one
794 * return probe was registered for a target function.
796 * We can handle this because:
797 * - instances are always pushed into the head of the list
798 * - when multiple return probes are registered for the same
799 * function, the (chronologically) first instance's ret_addr
800 * will be the real return address, and all the rest will
801 * point to kretprobe_trampoline.
803 hlist_for_each_entry(ri
, head
, hlist
) {
804 if (ri
->task
!= current
)
805 /* another task is sharing our hash bucket */
808 * Return probes must be pushed on this hash list correct
809 * order (same as return order) so that it can be popped
810 * correctly. However, if we find it is pushed it incorrect
811 * order, this means we find a function which should not be
812 * probed, because the wrong order entry is pushed on the
813 * path of processing other kretprobe itself.
815 if (ri
->fp
!= frame_pointer
) {
817 pr_warn("kretprobe is stacked incorrectly. Trying to fixup.\n");
822 orig_ret_address
= (unsigned long)ri
->ret_addr
;
824 pr_warn("%ps must be blacklisted because of incorrect kretprobe order\n",
827 if (orig_ret_address
!= trampoline_address
)
829 * This is the real return address. Any other
830 * instances associated with this task are for
831 * other calls deeper on the call stack
836 kretprobe_assert(ri
, orig_ret_address
, trampoline_address
);
838 correct_ret_addr
= ri
->ret_addr
;
839 hlist_for_each_entry_safe(ri
, tmp
, head
, hlist
) {
840 if (ri
->task
!= current
)
841 /* another task is sharing our hash bucket */
843 if (ri
->fp
!= frame_pointer
)
846 orig_ret_address
= (unsigned long)ri
->ret_addr
;
847 if (ri
->rp
&& ri
->rp
->handler
) {
848 __this_cpu_write(current_kprobe
, &ri
->rp
->kp
);
849 ri
->ret_addr
= correct_ret_addr
;
850 ri
->rp
->handler(ri
, regs
);
851 __this_cpu_write(current_kprobe
, &kprobe_busy
);
854 recycle_rp_inst(ri
, &empty_rp
);
856 if (orig_ret_address
!= trampoline_address
)
858 * This is the real return address. Any other
859 * instances associated with this task are for
860 * other calls deeper on the call stack
865 kretprobe_hash_unlock(current
, &flags
);
869 hlist_for_each_entry_safe(ri
, tmp
, &empty_rp
, hlist
) {
870 hlist_del(&ri
->hlist
);
873 return (void *)orig_ret_address
;
875 NOKPROBE_SYMBOL(trampoline_handler
);
878 * Called after single-stepping. p->addr is the address of the
879 * instruction whose first byte has been replaced by the "int 3"
880 * instruction. To avoid the SMP problems that can occur when we
881 * temporarily put back the original opcode to single-step, we
882 * single-stepped a copy of the instruction. The address of this
883 * copy is p->ainsn.insn.
885 * This function prepares to return from the post-single-step
886 * interrupt. We have to fix up the stack as follows:
888 * 0) Except in the case of absolute or indirect jump or call instructions,
889 * the new ip is relative to the copied instruction. We need to make
890 * it relative to the original instruction.
892 * 1) If the single-stepped instruction was pushfl, then the TF and IF
893 * flags are set in the just-pushed flags, and may need to be cleared.
895 * 2) If the single-stepped instruction was a call, the return address
896 * that is atop the stack is the address following the copied instruction.
897 * We need to make it the address following the original instruction.
899 * If this is the first time we've single-stepped the instruction at
900 * this probepoint, and the instruction is boostable, boost it: add a
901 * jump instruction after the copied instruction, that jumps to the next
902 * instruction after the probepoint.
904 static void resume_execution(struct kprobe
*p
, struct pt_regs
*regs
,
905 struct kprobe_ctlblk
*kcb
)
907 unsigned long *tos
= stack_addr(regs
);
908 unsigned long copy_ip
= (unsigned long)p
->ainsn
.insn
;
909 unsigned long orig_ip
= (unsigned long)p
->addr
;
910 kprobe_opcode_t
*insn
= p
->ainsn
.insn
;
913 insn
= skip_prefixes(insn
);
915 regs
->flags
&= ~X86_EFLAGS_TF
;
917 case 0x9c: /* pushfl */
918 *tos
&= ~(X86_EFLAGS_TF
| X86_EFLAGS_IF
);
919 *tos
|= kcb
->kprobe_old_flags
;
921 case 0xc2: /* iret/ret/lret */
926 case 0xea: /* jmp absolute -- ip is correct */
927 /* ip is already adjusted, no more changes required */
928 p
->ainsn
.boostable
= true;
930 case 0xe8: /* call relative - Fix return addr */
931 *tos
= orig_ip
+ (*tos
- copy_ip
);
934 case 0x9a: /* call absolute -- same as call absolute, indirect */
935 *tos
= orig_ip
+ (*tos
- copy_ip
);
939 if ((insn
[1] & 0x30) == 0x10) {
941 * call absolute, indirect
942 * Fix return addr; ip is correct.
943 * But this is not boostable
945 *tos
= orig_ip
+ (*tos
- copy_ip
);
947 } else if (((insn
[1] & 0x31) == 0x20) ||
948 ((insn
[1] & 0x31) == 0x21)) {
950 * jmp near and far, absolute indirect
951 * ip is correct. And this is boostable
953 p
->ainsn
.boostable
= true;
960 regs
->ip
+= orig_ip
- copy_ip
;
965 NOKPROBE_SYMBOL(resume_execution
);
968 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
969 * remain disabled throughout this function.
971 int kprobe_debug_handler(struct pt_regs
*regs
)
973 struct kprobe
*cur
= kprobe_running();
974 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
979 resume_execution(cur
, regs
, kcb
);
980 regs
->flags
|= kcb
->kprobe_saved_flags
;
982 if ((kcb
->kprobe_status
!= KPROBE_REENTER
) && cur
->post_handler
) {
983 kcb
->kprobe_status
= KPROBE_HIT_SSDONE
;
984 cur
->post_handler(cur
, regs
, 0);
987 /* Restore back the original saved kprobes variables and continue. */
988 if (kcb
->kprobe_status
== KPROBE_REENTER
) {
989 restore_previous_kprobe(kcb
);
992 reset_current_kprobe();
995 * if somebody else is singlestepping across a probe point, flags
996 * will have TF set, in which case, continue the remaining processing
997 * of do_debug, as if this is not a probe hit.
999 if (regs
->flags
& X86_EFLAGS_TF
)
1004 NOKPROBE_SYMBOL(kprobe_debug_handler
);
1006 int kprobe_fault_handler(struct pt_regs
*regs
, int trapnr
)
1008 struct kprobe
*cur
= kprobe_running();
1009 struct kprobe_ctlblk
*kcb
= get_kprobe_ctlblk();
1011 if (unlikely(regs
->ip
== (unsigned long)cur
->ainsn
.insn
)) {
1012 /* This must happen on single-stepping */
1013 WARN_ON(kcb
->kprobe_status
!= KPROBE_HIT_SS
&&
1014 kcb
->kprobe_status
!= KPROBE_REENTER
);
1016 * We are here because the instruction being single
1017 * stepped caused a page fault. We reset the current
1018 * kprobe and the ip points back to the probe address
1019 * and allow the page fault handler to continue as a
1020 * normal page fault.
1022 regs
->ip
= (unsigned long)cur
->addr
;
1024 * Trap flag (TF) has been set here because this fault
1025 * happened where the single stepping will be done.
1026 * So clear it by resetting the current kprobe:
1028 regs
->flags
&= ~X86_EFLAGS_TF
;
1031 * If the TF flag was set before the kprobe hit,
1034 regs
->flags
|= kcb
->kprobe_old_flags
;
1036 if (kcb
->kprobe_status
== KPROBE_REENTER
)
1037 restore_previous_kprobe(kcb
);
1039 reset_current_kprobe();
1040 } else if (kcb
->kprobe_status
== KPROBE_HIT_ACTIVE
||
1041 kcb
->kprobe_status
== KPROBE_HIT_SSDONE
) {
1043 * We increment the nmissed count for accounting,
1044 * we can also use npre/npostfault count for accounting
1045 * these specific fault cases.
1047 kprobes_inc_nmissed_count(cur
);
1050 * We come here because instructions in the pre/post
1051 * handler caused the page_fault, this could happen
1052 * if handler tries to access user space by
1053 * copy_from_user(), get_user() etc. Let the
1054 * user-specified handler try to fix it first.
1056 if (cur
->fault_handler
&& cur
->fault_handler(cur
, regs
, trapnr
))
1062 NOKPROBE_SYMBOL(kprobe_fault_handler
);
1064 int __init
arch_populate_kprobe_blacklist(void)
1068 ret
= kprobe_add_area_blacklist((unsigned long)__irqentry_text_start
,
1069 (unsigned long)__irqentry_text_end
);
1073 return kprobe_add_area_blacklist((unsigned long)__entry_text_start
,
1074 (unsigned long)__entry_text_end
);
1077 int __init
arch_init_kprobes(void)
1082 int arch_trampoline_kprobe(struct kprobe
*p
)