kprobes/x86: Use probe_kernel_read() instead of memcpy()
[linux/fpc-iii.git] / arch / x86 / kernel / kprobes / core.c
blob722f54440e7e364b1ab0c4340b71f64cc7289634
1 /*
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
22 * Rusty Russell).
23 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
24 * interface to access function arguments.
25 * 2004-Oct Jim Keniston <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
26 * <prasanna@in.ibm.com> adapted for x86_64 from i386.
27 * 2005-Mar Roland McGrath <roland@redhat.com>
28 * Fixed to handle %rip-relative addressing mode correctly.
29 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
30 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
31 * <prasanna@in.ibm.com> added function-return probes.
32 * 2005-May Rusty Lynch <rusty.lynch@intel.com>
33 * Added function return probes functionality
34 * 2006-Feb Masami Hiramatsu <hiramatu@sdl.hitachi.co.jp> added
35 * kprobe-booster and kretprobe-booster for i386.
36 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com> added kprobe-booster
37 * and kretprobe-booster for x86-64
38 * 2007-Dec Masami Hiramatsu <mhiramat@redhat.com>, Arjan van de Ven
39 * <arjan@infradead.org> and Jim Keniston <jkenisto@us.ibm.com>
40 * unified x86 kprobes code.
42 #include <linux/kprobes.h>
43 #include <linux/ptrace.h>
44 #include <linux/string.h>
45 #include <linux/slab.h>
46 #include <linux/hardirq.h>
47 #include <linux/preempt.h>
48 #include <linux/sched/debug.h>
49 #include <linux/extable.h>
50 #include <linux/kdebug.h>
51 #include <linux/kallsyms.h>
52 #include <linux/ftrace.h>
53 #include <linux/frame.h>
54 #include <linux/kasan.h>
56 #include <asm/text-patching.h>
57 #include <asm/cacheflush.h>
58 #include <asm/desc.h>
59 #include <asm/pgtable.h>
60 #include <linux/uaccess.h>
61 #include <asm/alternative.h>
62 #include <asm/insn.h>
63 #include <asm/debugreg.h>
65 #include "common.h"
67 void jprobe_return_end(void);
69 DEFINE_PER_CPU(struct kprobe *, current_kprobe) = NULL;
70 DEFINE_PER_CPU(struct kprobe_ctlblk, kprobe_ctlblk);
72 #define stack_addr(regs) ((unsigned long *)kernel_stack_pointer(regs))
74 #define W(row, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf)\
75 (((b0##UL << 0x0)|(b1##UL << 0x1)|(b2##UL << 0x2)|(b3##UL << 0x3) | \
76 (b4##UL << 0x4)|(b5##UL << 0x5)|(b6##UL << 0x6)|(b7##UL << 0x7) | \
77 (b8##UL << 0x8)|(b9##UL << 0x9)|(ba##UL << 0xa)|(bb##UL << 0xb) | \
78 (bc##UL << 0xc)|(bd##UL << 0xd)|(be##UL << 0xe)|(bf##UL << 0xf)) \
79 << (row % 32))
81 * Undefined/reserved opcodes, conditional jump, Opcode Extension
82 * Groups, and some special opcodes can not boost.
83 * This is non-const and volatile to keep gcc from statically
84 * optimizing it out, as variable_test_bit makes gcc think only
85 * *(unsigned long*) is used.
87 static volatile u32 twobyte_is_boostable[256 / 32] = {
88 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
89 /* ---------------------------------------------- */
90 W(0x00, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0) | /* 00 */
91 W(0x10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) , /* 10 */
92 W(0x20, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 20 */
93 W(0x30, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 30 */
94 W(0x40, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) | /* 40 */
95 W(0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) , /* 50 */
96 W(0x60, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1) | /* 60 */
97 W(0x70, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1) , /* 70 */
98 W(0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | /* 80 */
99 W(0x90, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) , /* 90 */
100 W(0xa0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* a0 */
101 W(0xb0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1) , /* b0 */
102 W(0xc0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1) | /* c0 */
103 W(0xd0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) , /* d0 */
104 W(0xe0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1) | /* e0 */
105 W(0xf0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0) /* f0 */
106 /* ----------------------------------------------- */
107 /* 0 1 2 3 4 5 6 7 8 9 a b c d e f */
109 #undef W
111 struct kretprobe_blackpoint kretprobe_blacklist[] = {
112 {"__switch_to", }, /* This function switches only current task, but
113 doesn't switch kernel stack.*/
114 {NULL, NULL} /* Terminator */
117 const int kretprobe_blacklist_size = ARRAY_SIZE(kretprobe_blacklist);
119 static nokprobe_inline void
120 __synthesize_relative_insn(void *from, void *to, u8 op)
122 struct __arch_relative_insn {
123 u8 op;
124 s32 raddr;
125 } __packed *insn;
127 insn = (struct __arch_relative_insn *)from;
128 insn->raddr = (s32)((long)(to) - ((long)(from) + 5));
129 insn->op = op;
132 /* Insert a jump instruction at address 'from', which jumps to address 'to'.*/
133 void synthesize_reljump(void *from, void *to)
135 __synthesize_relative_insn(from, to, RELATIVEJUMP_OPCODE);
137 NOKPROBE_SYMBOL(synthesize_reljump);
139 /* Insert a call instruction at address 'from', which calls address 'to'.*/
140 void synthesize_relcall(void *from, void *to)
142 __synthesize_relative_insn(from, to, RELATIVECALL_OPCODE);
144 NOKPROBE_SYMBOL(synthesize_relcall);
147 * Skip the prefixes of the instruction.
149 static kprobe_opcode_t *skip_prefixes(kprobe_opcode_t *insn)
151 insn_attr_t attr;
153 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
154 while (inat_is_legacy_prefix(attr)) {
155 insn++;
156 attr = inat_get_opcode_attribute((insn_byte_t)*insn);
158 #ifdef CONFIG_X86_64
159 if (inat_is_rex_prefix(attr))
160 insn++;
161 #endif
162 return insn;
164 NOKPROBE_SYMBOL(skip_prefixes);
167 * Returns non-zero if opcode is boostable.
168 * RIP relative instructions are adjusted at copying time in 64 bits mode
170 int can_boost(kprobe_opcode_t *opcodes, void *addr)
172 struct insn insn;
173 kprobe_opcode_t opcode;
175 if (search_exception_tables((unsigned long)addr))
176 return 0; /* Page fault may occur on this address. */
178 kernel_insn_init(&insn, (void *)opcodes, MAX_INSN_SIZE);
179 insn_get_opcode(&insn);
181 /* 2nd-byte opcode */
182 if (insn.opcode.nbytes == 2)
183 return test_bit(insn.opcode.bytes[1],
184 (unsigned long *)twobyte_is_boostable);
186 if (insn.opcode.nbytes != 1)
187 return 0;
189 /* Can't boost Address-size override prefix */
190 if (unlikely(inat_is_address_size_prefix(insn.attr)))
191 return 0;
193 opcode = insn.opcode.bytes[0];
195 switch (opcode & 0xf0) {
196 case 0x60:
197 /* can't boost "bound" */
198 return (opcode != 0x62);
199 case 0x70:
200 return 0; /* can't boost conditional jump */
201 case 0x90:
202 return opcode != 0x9a; /* can't boost call far */
203 case 0xc0:
204 /* can't boost software-interruptions */
205 return (0xc1 < opcode && opcode < 0xcc) || opcode == 0xcf;
206 case 0xd0:
207 /* can boost AA* and XLAT */
208 return (opcode == 0xd4 || opcode == 0xd5 || opcode == 0xd7);
209 case 0xe0:
210 /* can boost in/out and absolute jmps */
211 return ((opcode & 0x04) || opcode == 0xea);
212 case 0xf0:
213 /* clear and set flags are boostable */
214 return (opcode == 0xf5 || (0xf7 < opcode && opcode < 0xfe));
215 default:
216 /* CS override prefix and call are not boostable */
217 return (opcode != 0x2e && opcode != 0x9a);
221 static unsigned long
222 __recover_probed_insn(kprobe_opcode_t *buf, unsigned long addr)
224 struct kprobe *kp;
225 unsigned long faddr;
227 kp = get_kprobe((void *)addr);
228 faddr = ftrace_location(addr);
230 * Addresses inside the ftrace location are refused by
231 * arch_check_ftrace_location(). Something went terribly wrong
232 * if such an address is checked here.
234 if (WARN_ON(faddr && faddr != addr))
235 return 0UL;
237 * Use the current code if it is not modified by Kprobe
238 * and it cannot be modified by ftrace.
240 if (!kp && !faddr)
241 return addr;
244 * Basically, kp->ainsn.insn has an original instruction.
245 * However, RIP-relative instruction can not do single-stepping
246 * at different place, __copy_instruction() tweaks the displacement of
247 * that instruction. In that case, we can't recover the instruction
248 * from the kp->ainsn.insn.
250 * On the other hand, in case on normal Kprobe, kp->opcode has a copy
251 * of the first byte of the probed instruction, which is overwritten
252 * by int3. And the instruction at kp->addr is not modified by kprobes
253 * except for the first byte, we can recover the original instruction
254 * from it and kp->opcode.
256 * In case of Kprobes using ftrace, we do not have a copy of
257 * the original instruction. In fact, the ftrace location might
258 * be modified at anytime and even could be in an inconsistent state.
259 * Fortunately, we know that the original code is the ideal 5-byte
260 * long NOP.
262 if (probe_kernel_read(buf, (void *)addr,
263 MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
264 return 0UL;
266 if (faddr)
267 memcpy(buf, ideal_nops[NOP_ATOMIC5], 5);
268 else
269 buf[0] = kp->opcode;
270 return (unsigned long)buf;
274 * Recover the probed instruction at addr for further analysis.
275 * Caller must lock kprobes by kprobe_mutex, or disable preemption
276 * for preventing to release referencing kprobes.
277 * Returns zero if the instruction can not get recovered (or access failed).
279 unsigned long recover_probed_instruction(kprobe_opcode_t *buf, unsigned long addr)
281 unsigned long __addr;
283 __addr = __recover_optprobed_insn(buf, addr);
284 if (__addr != addr)
285 return __addr;
287 return __recover_probed_insn(buf, addr);
290 /* Check if paddr is at an instruction boundary */
291 static int can_probe(unsigned long paddr)
293 unsigned long addr, __addr, offset = 0;
294 struct insn insn;
295 kprobe_opcode_t buf[MAX_INSN_SIZE];
297 if (!kallsyms_lookup_size_offset(paddr, NULL, &offset))
298 return 0;
300 /* Decode instructions */
301 addr = paddr - offset;
302 while (addr < paddr) {
304 * Check if the instruction has been modified by another
305 * kprobe, in which case we replace the breakpoint by the
306 * original instruction in our buffer.
307 * Also, jump optimization will change the breakpoint to
308 * relative-jump. Since the relative-jump itself is
309 * normally used, we just go through if there is no kprobe.
311 __addr = recover_probed_instruction(buf, addr);
312 if (!__addr)
313 return 0;
314 kernel_insn_init(&insn, (void *)__addr, MAX_INSN_SIZE);
315 insn_get_length(&insn);
318 * Another debugging subsystem might insert this breakpoint.
319 * In that case, we can't recover it.
321 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
322 return 0;
323 addr += insn.length;
326 return (addr == paddr);
330 * Returns non-zero if opcode modifies the interrupt flag.
332 static int is_IF_modifier(kprobe_opcode_t *insn)
334 /* Skip prefixes */
335 insn = skip_prefixes(insn);
337 switch (*insn) {
338 case 0xfa: /* cli */
339 case 0xfb: /* sti */
340 case 0xcf: /* iret/iretd */
341 case 0x9d: /* popf/popfd */
342 return 1;
345 return 0;
349 * Copy an instruction with recovering modified instruction by kprobes
350 * and adjust the displacement if the instruction uses the %rip-relative
351 * addressing mode.
352 * This returns the length of copied instruction, or 0 if it has an error.
354 int __copy_instruction(u8 *dest, u8 *src)
356 struct insn insn;
357 kprobe_opcode_t buf[MAX_INSN_SIZE];
358 int length;
359 unsigned long recovered_insn =
360 recover_probed_instruction(buf, (unsigned long)src);
362 if (!recovered_insn)
363 return 0;
364 kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
365 insn_get_length(&insn);
366 length = insn.length;
368 /* Another subsystem puts a breakpoint, failed to recover */
369 if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
370 return 0;
372 /* This can access kernel text if given address is not recovered */
373 if (kernel_probe_read(dest, insn.kaddr, length))
374 return 0;
376 #ifdef CONFIG_X86_64
377 /* Only x86_64 has RIP relative instructions */
378 if (insn_rip_relative(&insn)) {
379 s64 newdisp;
380 u8 *disp;
381 kernel_insn_init(&insn, dest, length);
382 insn_get_displacement(&insn);
384 * The copied instruction uses the %rip-relative addressing
385 * mode. Adjust the displacement for the difference between
386 * the original location of this instruction and the location
387 * of the copy that will actually be run. The tricky bit here
388 * is making sure that the sign extension happens correctly in
389 * this calculation, since we need a signed 32-bit result to
390 * be sign-extended to 64 bits when it's added to the %rip
391 * value and yield the same 64-bit result that the sign-
392 * extension of the original signed 32-bit displacement would
393 * have given.
395 newdisp = (u8 *) src + (s64) insn.displacement.value - (u8 *) dest;
396 if ((s64) (s32) newdisp != newdisp) {
397 pr_err("Kprobes error: new displacement does not fit into s32 (%llx)\n", newdisp);
398 pr_err("\tSrc: %p, Dest: %p, old disp: %x\n", src, dest, insn.displacement.value);
399 return 0;
401 disp = (u8 *) dest + insn_offset_displacement(&insn);
402 *(s32 *) disp = (s32) newdisp;
404 #endif
405 return length;
408 /* Prepare reljump right after instruction to boost */
409 static void prepare_boost(struct kprobe *p, int length)
411 if (can_boost(p->ainsn.insn, p->addr) &&
412 MAX_INSN_SIZE - length >= RELATIVEJUMP_SIZE) {
414 * These instructions can be executed directly if it
415 * jumps back to correct address.
417 synthesize_reljump(p->ainsn.insn + length, p->addr + length);
418 p->ainsn.boostable = true;
419 } else {
420 p->ainsn.boostable = false;
424 static int arch_copy_kprobe(struct kprobe *p)
426 int len;
428 set_memory_rw((unsigned long)p->ainsn.insn & PAGE_MASK, 1);
430 /* Copy an instruction with recovering if other optprobe modifies it.*/
431 len = __copy_instruction(p->ainsn.insn, p->addr);
432 if (!len)
433 return -EINVAL;
436 * __copy_instruction can modify the displacement of the instruction,
437 * but it doesn't affect boostable check.
439 prepare_boost(p, len);
441 set_memory_ro((unsigned long)p->ainsn.insn & PAGE_MASK, 1);
443 /* Check whether the instruction modifies Interrupt Flag or not */
444 p->ainsn.if_modifier = is_IF_modifier(p->ainsn.insn);
446 /* Also, displacement change doesn't affect the first byte */
447 p->opcode = p->ainsn.insn[0];
449 return 0;
452 int arch_prepare_kprobe(struct kprobe *p)
454 if (alternatives_text_reserved(p->addr, p->addr))
455 return -EINVAL;
457 if (!can_probe((unsigned long)p->addr))
458 return -EILSEQ;
459 /* insn: must be on special executable page on x86. */
460 p->ainsn.insn = get_insn_slot();
461 if (!p->ainsn.insn)
462 return -ENOMEM;
464 return arch_copy_kprobe(p);
467 void arch_arm_kprobe(struct kprobe *p)
469 text_poke(p->addr, ((unsigned char []){BREAKPOINT_INSTRUCTION}), 1);
472 void arch_disarm_kprobe(struct kprobe *p)
474 text_poke(p->addr, &p->opcode, 1);
477 void arch_remove_kprobe(struct kprobe *p)
479 if (p->ainsn.insn) {
480 free_insn_slot(p->ainsn.insn, p->ainsn.boostable);
481 p->ainsn.insn = NULL;
485 static nokprobe_inline void
486 save_previous_kprobe(struct kprobe_ctlblk *kcb)
488 kcb->prev_kprobe.kp = kprobe_running();
489 kcb->prev_kprobe.status = kcb->kprobe_status;
490 kcb->prev_kprobe.old_flags = kcb->kprobe_old_flags;
491 kcb->prev_kprobe.saved_flags = kcb->kprobe_saved_flags;
494 static nokprobe_inline void
495 restore_previous_kprobe(struct kprobe_ctlblk *kcb)
497 __this_cpu_write(current_kprobe, kcb->prev_kprobe.kp);
498 kcb->kprobe_status = kcb->prev_kprobe.status;
499 kcb->kprobe_old_flags = kcb->prev_kprobe.old_flags;
500 kcb->kprobe_saved_flags = kcb->prev_kprobe.saved_flags;
503 static nokprobe_inline void
504 set_current_kprobe(struct kprobe *p, struct pt_regs *regs,
505 struct kprobe_ctlblk *kcb)
507 __this_cpu_write(current_kprobe, p);
508 kcb->kprobe_saved_flags = kcb->kprobe_old_flags
509 = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
510 if (p->ainsn.if_modifier)
511 kcb->kprobe_saved_flags &= ~X86_EFLAGS_IF;
514 static nokprobe_inline void clear_btf(void)
516 if (test_thread_flag(TIF_BLOCKSTEP)) {
517 unsigned long debugctl = get_debugctlmsr();
519 debugctl &= ~DEBUGCTLMSR_BTF;
520 update_debugctlmsr(debugctl);
524 static nokprobe_inline void restore_btf(void)
526 if (test_thread_flag(TIF_BLOCKSTEP)) {
527 unsigned long debugctl = get_debugctlmsr();
529 debugctl |= DEBUGCTLMSR_BTF;
530 update_debugctlmsr(debugctl);
534 void arch_prepare_kretprobe(struct kretprobe_instance *ri, struct pt_regs *regs)
536 unsigned long *sara = stack_addr(regs);
538 ri->ret_addr = (kprobe_opcode_t *) *sara;
540 /* Replace the return addr with trampoline addr */
541 *sara = (unsigned long) &kretprobe_trampoline;
543 NOKPROBE_SYMBOL(arch_prepare_kretprobe);
545 static void setup_singlestep(struct kprobe *p, struct pt_regs *regs,
546 struct kprobe_ctlblk *kcb, int reenter)
548 if (setup_detour_execution(p, regs, reenter))
549 return;
551 #if !defined(CONFIG_PREEMPT)
552 if (p->ainsn.boostable && !p->post_handler) {
553 /* Boost up -- we can execute copied instructions directly */
554 if (!reenter)
555 reset_current_kprobe();
557 * Reentering boosted probe doesn't reset current_kprobe,
558 * nor set current_kprobe, because it doesn't use single
559 * stepping.
561 regs->ip = (unsigned long)p->ainsn.insn;
562 preempt_enable_no_resched();
563 return;
565 #endif
566 if (reenter) {
567 save_previous_kprobe(kcb);
568 set_current_kprobe(p, regs, kcb);
569 kcb->kprobe_status = KPROBE_REENTER;
570 } else
571 kcb->kprobe_status = KPROBE_HIT_SS;
572 /* Prepare real single stepping */
573 clear_btf();
574 regs->flags |= X86_EFLAGS_TF;
575 regs->flags &= ~X86_EFLAGS_IF;
576 /* single step inline if the instruction is an int3 */
577 if (p->opcode == BREAKPOINT_INSTRUCTION)
578 regs->ip = (unsigned long)p->addr;
579 else
580 regs->ip = (unsigned long)p->ainsn.insn;
582 NOKPROBE_SYMBOL(setup_singlestep);
585 * We have reentered the kprobe_handler(), since another probe was hit while
586 * within the handler. We save the original kprobes variables and just single
587 * step on the instruction of the new probe without calling any user handlers.
589 static int reenter_kprobe(struct kprobe *p, struct pt_regs *regs,
590 struct kprobe_ctlblk *kcb)
592 switch (kcb->kprobe_status) {
593 case KPROBE_HIT_SSDONE:
594 case KPROBE_HIT_ACTIVE:
595 case KPROBE_HIT_SS:
596 kprobes_inc_nmissed_count(p);
597 setup_singlestep(p, regs, kcb, 1);
598 break;
599 case KPROBE_REENTER:
600 /* A probe has been hit in the codepath leading up to, or just
601 * after, single-stepping of a probed instruction. This entire
602 * codepath should strictly reside in .kprobes.text section.
603 * Raise a BUG or we'll continue in an endless reentering loop
604 * and eventually a stack overflow.
606 printk(KERN_WARNING "Unrecoverable kprobe detected at %p.\n",
607 p->addr);
608 dump_kprobe(p);
609 BUG();
610 default:
611 /* impossible cases */
612 WARN_ON(1);
613 return 0;
616 return 1;
618 NOKPROBE_SYMBOL(reenter_kprobe);
621 * Interrupts are disabled on entry as trap3 is an interrupt gate and they
622 * remain disabled throughout this function.
624 int kprobe_int3_handler(struct pt_regs *regs)
626 kprobe_opcode_t *addr;
627 struct kprobe *p;
628 struct kprobe_ctlblk *kcb;
630 if (user_mode(regs))
631 return 0;
633 addr = (kprobe_opcode_t *)(regs->ip - sizeof(kprobe_opcode_t));
635 * We don't want to be preempted for the entire
636 * duration of kprobe processing. We conditionally
637 * re-enable preemption at the end of this function,
638 * and also in reenter_kprobe() and setup_singlestep().
640 preempt_disable();
642 kcb = get_kprobe_ctlblk();
643 p = get_kprobe(addr);
645 if (p) {
646 if (kprobe_running()) {
647 if (reenter_kprobe(p, regs, kcb))
648 return 1;
649 } else {
650 set_current_kprobe(p, regs, kcb);
651 kcb->kprobe_status = KPROBE_HIT_ACTIVE;
654 * If we have no pre-handler or it returned 0, we
655 * continue with normal processing. If we have a
656 * pre-handler and it returned non-zero, it prepped
657 * for calling the break_handler below on re-entry
658 * for jprobe processing, so get out doing nothing
659 * more here.
661 if (!p->pre_handler || !p->pre_handler(p, regs))
662 setup_singlestep(p, regs, kcb, 0);
663 return 1;
665 } else if (*addr != BREAKPOINT_INSTRUCTION) {
667 * The breakpoint instruction was removed right
668 * after we hit it. Another cpu has removed
669 * either a probepoint or a debugger breakpoint
670 * at this address. In either case, no further
671 * handling of this interrupt is appropriate.
672 * Back up over the (now missing) int3 and run
673 * the original instruction.
675 regs->ip = (unsigned long)addr;
676 preempt_enable_no_resched();
677 return 1;
678 } else if (kprobe_running()) {
679 p = __this_cpu_read(current_kprobe);
680 if (p->break_handler && p->break_handler(p, regs)) {
681 if (!skip_singlestep(p, regs, kcb))
682 setup_singlestep(p, regs, kcb, 0);
683 return 1;
685 } /* else: not a kprobe fault; let the kernel handle it */
687 preempt_enable_no_resched();
688 return 0;
690 NOKPROBE_SYMBOL(kprobe_int3_handler);
693 * When a retprobed function returns, this code saves registers and
694 * calls trampoline_handler() runs, which calls the kretprobe's handler.
696 asm(
697 ".global kretprobe_trampoline\n"
698 ".type kretprobe_trampoline, @function\n"
699 "kretprobe_trampoline:\n"
700 #ifdef CONFIG_X86_64
701 /* We don't bother saving the ss register */
702 " pushq %rsp\n"
703 " pushfq\n"
704 SAVE_REGS_STRING
705 " movq %rsp, %rdi\n"
706 " call trampoline_handler\n"
707 /* Replace saved sp with true return address. */
708 " movq %rax, 152(%rsp)\n"
709 RESTORE_REGS_STRING
710 " popfq\n"
711 #else
712 " pushf\n"
713 SAVE_REGS_STRING
714 " movl %esp, %eax\n"
715 " call trampoline_handler\n"
716 /* Move flags to cs */
717 " movl 56(%esp), %edx\n"
718 " movl %edx, 52(%esp)\n"
719 /* Replace saved flags with true return address. */
720 " movl %eax, 56(%esp)\n"
721 RESTORE_REGS_STRING
722 " popf\n"
723 #endif
724 " ret\n"
725 ".size kretprobe_trampoline, .-kretprobe_trampoline\n"
727 NOKPROBE_SYMBOL(kretprobe_trampoline);
728 STACK_FRAME_NON_STANDARD(kretprobe_trampoline);
731 * Called from kretprobe_trampoline
733 __visible __used void *trampoline_handler(struct pt_regs *regs)
735 struct kretprobe_instance *ri = NULL;
736 struct hlist_head *head, empty_rp;
737 struct hlist_node *tmp;
738 unsigned long flags, orig_ret_address = 0;
739 unsigned long trampoline_address = (unsigned long)&kretprobe_trampoline;
740 kprobe_opcode_t *correct_ret_addr = NULL;
742 INIT_HLIST_HEAD(&empty_rp);
743 kretprobe_hash_lock(current, &head, &flags);
744 /* fixup registers */
745 #ifdef CONFIG_X86_64
746 regs->cs = __KERNEL_CS;
747 #else
748 regs->cs = __KERNEL_CS | get_kernel_rpl();
749 regs->gs = 0;
750 #endif
751 regs->ip = trampoline_address;
752 regs->orig_ax = ~0UL;
755 * It is possible to have multiple instances associated with a given
756 * task either because multiple functions in the call path have
757 * return probes installed on them, and/or more than one
758 * return probe was registered for a target function.
760 * We can handle this because:
761 * - instances are always pushed into the head of the list
762 * - when multiple return probes are registered for the same
763 * function, the (chronologically) first instance's ret_addr
764 * will be the real return address, and all the rest will
765 * point to kretprobe_trampoline.
767 hlist_for_each_entry(ri, head, hlist) {
768 if (ri->task != current)
769 /* another task is sharing our hash bucket */
770 continue;
772 orig_ret_address = (unsigned long)ri->ret_addr;
774 if (orig_ret_address != trampoline_address)
776 * This is the real return address. Any other
777 * instances associated with this task are for
778 * other calls deeper on the call stack
780 break;
783 kretprobe_assert(ri, orig_ret_address, trampoline_address);
785 correct_ret_addr = ri->ret_addr;
786 hlist_for_each_entry_safe(ri, tmp, head, hlist) {
787 if (ri->task != current)
788 /* another task is sharing our hash bucket */
789 continue;
791 orig_ret_address = (unsigned long)ri->ret_addr;
792 if (ri->rp && ri->rp->handler) {
793 __this_cpu_write(current_kprobe, &ri->rp->kp);
794 get_kprobe_ctlblk()->kprobe_status = KPROBE_HIT_ACTIVE;
795 ri->ret_addr = correct_ret_addr;
796 ri->rp->handler(ri, regs);
797 __this_cpu_write(current_kprobe, NULL);
800 recycle_rp_inst(ri, &empty_rp);
802 if (orig_ret_address != trampoline_address)
804 * This is the real return address. Any other
805 * instances associated with this task are for
806 * other calls deeper on the call stack
808 break;
811 kretprobe_hash_unlock(current, &flags);
813 hlist_for_each_entry_safe(ri, tmp, &empty_rp, hlist) {
814 hlist_del(&ri->hlist);
815 kfree(ri);
817 return (void *)orig_ret_address;
819 NOKPROBE_SYMBOL(trampoline_handler);
822 * Called after single-stepping. p->addr is the address of the
823 * instruction whose first byte has been replaced by the "int 3"
824 * instruction. To avoid the SMP problems that can occur when we
825 * temporarily put back the original opcode to single-step, we
826 * single-stepped a copy of the instruction. The address of this
827 * copy is p->ainsn.insn.
829 * This function prepares to return from the post-single-step
830 * interrupt. We have to fix up the stack as follows:
832 * 0) Except in the case of absolute or indirect jump or call instructions,
833 * the new ip is relative to the copied instruction. We need to make
834 * it relative to the original instruction.
836 * 1) If the single-stepped instruction was pushfl, then the TF and IF
837 * flags are set in the just-pushed flags, and may need to be cleared.
839 * 2) If the single-stepped instruction was a call, the return address
840 * that is atop the stack is the address following the copied instruction.
841 * We need to make it the address following the original instruction.
843 * If this is the first time we've single-stepped the instruction at
844 * this probepoint, and the instruction is boostable, boost it: add a
845 * jump instruction after the copied instruction, that jumps to the next
846 * instruction after the probepoint.
848 static void resume_execution(struct kprobe *p, struct pt_regs *regs,
849 struct kprobe_ctlblk *kcb)
851 unsigned long *tos = stack_addr(regs);
852 unsigned long copy_ip = (unsigned long)p->ainsn.insn;
853 unsigned long orig_ip = (unsigned long)p->addr;
854 kprobe_opcode_t *insn = p->ainsn.insn;
856 /* Skip prefixes */
857 insn = skip_prefixes(insn);
859 regs->flags &= ~X86_EFLAGS_TF;
860 switch (*insn) {
861 case 0x9c: /* pushfl */
862 *tos &= ~(X86_EFLAGS_TF | X86_EFLAGS_IF);
863 *tos |= kcb->kprobe_old_flags;
864 break;
865 case 0xc2: /* iret/ret/lret */
866 case 0xc3:
867 case 0xca:
868 case 0xcb:
869 case 0xcf:
870 case 0xea: /* jmp absolute -- ip is correct */
871 /* ip is already adjusted, no more changes required */
872 p->ainsn.boostable = true;
873 goto no_change;
874 case 0xe8: /* call relative - Fix return addr */
875 *tos = orig_ip + (*tos - copy_ip);
876 break;
877 #ifdef CONFIG_X86_32
878 case 0x9a: /* call absolute -- same as call absolute, indirect */
879 *tos = orig_ip + (*tos - copy_ip);
880 goto no_change;
881 #endif
882 case 0xff:
883 if ((insn[1] & 0x30) == 0x10) {
885 * call absolute, indirect
886 * Fix return addr; ip is correct.
887 * But this is not boostable
889 *tos = orig_ip + (*tos - copy_ip);
890 goto no_change;
891 } else if (((insn[1] & 0x31) == 0x20) ||
892 ((insn[1] & 0x31) == 0x21)) {
894 * jmp near and far, absolute indirect
895 * ip is correct. And this is boostable
897 p->ainsn.boostable = true;
898 goto no_change;
900 default:
901 break;
904 regs->ip += orig_ip - copy_ip;
906 no_change:
907 restore_btf();
909 NOKPROBE_SYMBOL(resume_execution);
912 * Interrupts are disabled on entry as trap1 is an interrupt gate and they
913 * remain disabled throughout this function.
915 int kprobe_debug_handler(struct pt_regs *regs)
917 struct kprobe *cur = kprobe_running();
918 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
920 if (!cur)
921 return 0;
923 resume_execution(cur, regs, kcb);
924 regs->flags |= kcb->kprobe_saved_flags;
926 if ((kcb->kprobe_status != KPROBE_REENTER) && cur->post_handler) {
927 kcb->kprobe_status = KPROBE_HIT_SSDONE;
928 cur->post_handler(cur, regs, 0);
931 /* Restore back the original saved kprobes variables and continue. */
932 if (kcb->kprobe_status == KPROBE_REENTER) {
933 restore_previous_kprobe(kcb);
934 goto out;
936 reset_current_kprobe();
937 out:
938 preempt_enable_no_resched();
941 * if somebody else is singlestepping across a probe point, flags
942 * will have TF set, in which case, continue the remaining processing
943 * of do_debug, as if this is not a probe hit.
945 if (regs->flags & X86_EFLAGS_TF)
946 return 0;
948 return 1;
950 NOKPROBE_SYMBOL(kprobe_debug_handler);
952 int kprobe_fault_handler(struct pt_regs *regs, int trapnr)
954 struct kprobe *cur = kprobe_running();
955 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
957 if (unlikely(regs->ip == (unsigned long)cur->ainsn.insn)) {
958 /* This must happen on single-stepping */
959 WARN_ON(kcb->kprobe_status != KPROBE_HIT_SS &&
960 kcb->kprobe_status != KPROBE_REENTER);
962 * We are here because the instruction being single
963 * stepped caused a page fault. We reset the current
964 * kprobe and the ip points back to the probe address
965 * and allow the page fault handler to continue as a
966 * normal page fault.
968 regs->ip = (unsigned long)cur->addr;
970 * Trap flag (TF) has been set here because this fault
971 * happened where the single stepping will be done.
972 * So clear it by resetting the current kprobe:
974 regs->flags &= ~X86_EFLAGS_TF;
977 * If the TF flag was set before the kprobe hit,
978 * don't touch it:
980 regs->flags |= kcb->kprobe_old_flags;
982 if (kcb->kprobe_status == KPROBE_REENTER)
983 restore_previous_kprobe(kcb);
984 else
985 reset_current_kprobe();
986 preempt_enable_no_resched();
987 } else if (kcb->kprobe_status == KPROBE_HIT_ACTIVE ||
988 kcb->kprobe_status == KPROBE_HIT_SSDONE) {
990 * We increment the nmissed count for accounting,
991 * we can also use npre/npostfault count for accounting
992 * these specific fault cases.
994 kprobes_inc_nmissed_count(cur);
997 * We come here because instructions in the pre/post
998 * handler caused the page_fault, this could happen
999 * if handler tries to access user space by
1000 * copy_from_user(), get_user() etc. Let the
1001 * user-specified handler try to fix it first.
1003 if (cur->fault_handler && cur->fault_handler(cur, regs, trapnr))
1004 return 1;
1007 * In case the user-specified fault handler returned
1008 * zero, try to fix up.
1010 if (fixup_exception(regs, trapnr))
1011 return 1;
1014 * fixup routine could not handle it,
1015 * Let do_page_fault() fix it.
1019 return 0;
1021 NOKPROBE_SYMBOL(kprobe_fault_handler);
1024 * Wrapper routine for handling exceptions.
1026 int kprobe_exceptions_notify(struct notifier_block *self, unsigned long val,
1027 void *data)
1029 struct die_args *args = data;
1030 int ret = NOTIFY_DONE;
1032 if (args->regs && user_mode(args->regs))
1033 return ret;
1035 if (val == DIE_GPF) {
1037 * To be potentially processing a kprobe fault and to
1038 * trust the result from kprobe_running(), we have
1039 * be non-preemptible.
1041 if (!preemptible() && kprobe_running() &&
1042 kprobe_fault_handler(args->regs, args->trapnr))
1043 ret = NOTIFY_STOP;
1045 return ret;
1047 NOKPROBE_SYMBOL(kprobe_exceptions_notify);
1049 int setjmp_pre_handler(struct kprobe *p, struct pt_regs *regs)
1051 struct jprobe *jp = container_of(p, struct jprobe, kp);
1052 unsigned long addr;
1053 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1055 kcb->jprobe_saved_regs = *regs;
1056 kcb->jprobe_saved_sp = stack_addr(regs);
1057 addr = (unsigned long)(kcb->jprobe_saved_sp);
1060 * As Linus pointed out, gcc assumes that the callee
1061 * owns the argument space and could overwrite it, e.g.
1062 * tailcall optimization. So, to be absolutely safe
1063 * we also save and restore enough stack bytes to cover
1064 * the argument area.
1065 * Use __memcpy() to avoid KASAN stack out-of-bounds reports as we copy
1066 * raw stack chunk with redzones:
1068 __memcpy(kcb->jprobes_stack, (kprobe_opcode_t *)addr, MIN_STACK_SIZE(addr));
1069 regs->flags &= ~X86_EFLAGS_IF;
1070 trace_hardirqs_off();
1071 regs->ip = (unsigned long)(jp->entry);
1074 * jprobes use jprobe_return() which skips the normal return
1075 * path of the function, and this messes up the accounting of the
1076 * function graph tracer to get messed up.
1078 * Pause function graph tracing while performing the jprobe function.
1080 pause_graph_tracing();
1081 return 1;
1083 NOKPROBE_SYMBOL(setjmp_pre_handler);
1085 void jprobe_return(void)
1087 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1089 /* Unpoison stack redzones in the frames we are going to jump over. */
1090 kasan_unpoison_stack_above_sp_to(kcb->jprobe_saved_sp);
1092 asm volatile (
1093 #ifdef CONFIG_X86_64
1094 " xchg %%rbx,%%rsp \n"
1095 #else
1096 " xchgl %%ebx,%%esp \n"
1097 #endif
1098 " int3 \n"
1099 " .globl jprobe_return_end\n"
1100 " jprobe_return_end: \n"
1101 " nop \n"::"b"
1102 (kcb->jprobe_saved_sp):"memory");
1104 NOKPROBE_SYMBOL(jprobe_return);
1105 NOKPROBE_SYMBOL(jprobe_return_end);
1107 int longjmp_break_handler(struct kprobe *p, struct pt_regs *regs)
1109 struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
1110 u8 *addr = (u8 *) (regs->ip - 1);
1111 struct jprobe *jp = container_of(p, struct jprobe, kp);
1112 void *saved_sp = kcb->jprobe_saved_sp;
1114 if ((addr > (u8 *) jprobe_return) &&
1115 (addr < (u8 *) jprobe_return_end)) {
1116 if (stack_addr(regs) != saved_sp) {
1117 struct pt_regs *saved_regs = &kcb->jprobe_saved_regs;
1118 printk(KERN_ERR
1119 "current sp %p does not match saved sp %p\n",
1120 stack_addr(regs), saved_sp);
1121 printk(KERN_ERR "Saved registers for jprobe %p\n", jp);
1122 show_regs(saved_regs);
1123 printk(KERN_ERR "Current registers\n");
1124 show_regs(regs);
1125 BUG();
1127 /* It's OK to start function graph tracing again */
1128 unpause_graph_tracing();
1129 *regs = kcb->jprobe_saved_regs;
1130 __memcpy(saved_sp, kcb->jprobes_stack, MIN_STACK_SIZE(saved_sp));
1131 preempt_enable_no_resched();
1132 return 1;
1134 return 0;
1136 NOKPROBE_SYMBOL(longjmp_break_handler);
1138 bool arch_within_kprobe_blacklist(unsigned long addr)
1140 return (addr >= (unsigned long)__kprobes_text_start &&
1141 addr < (unsigned long)__kprobes_text_end) ||
1142 (addr >= (unsigned long)__entry_text_start &&
1143 addr < (unsigned long)__entry_text_end);
1146 int __init arch_init_kprobes(void)
1148 return 0;
1151 int arch_trampoline_kprobe(struct kprobe *p)
1153 return 0;