2012-03-05 20:32:22 +07:00
|
|
|
/*
|
|
|
|
* Kernel Probes Jump Optimization (Optprobes)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* Copyright (C) IBM Corporation, 2002, 2004
|
|
|
|
* Copyright (C) Hitachi Ltd., 2012
|
|
|
|
*/
|
|
|
|
#include <linux/kprobes.h>
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/hardirq.h>
|
|
|
|
#include <linux/preempt.h>
|
2016-09-20 04:04:18 +07:00
|
|
|
#include <linux/extable.h>
|
2012-03-05 20:32:22 +07:00
|
|
|
#include <linux/kdebug.h>
|
|
|
|
#include <linux/kallsyms.h>
|
|
|
|
#include <linux/ftrace.h>
|
2017-06-28 22:11:06 +07:00
|
|
|
#include <linux/frame.h>
|
2012-03-05 20:32:22 +07:00
|
|
|
|
2016-04-27 02:23:24 +07:00
|
|
|
#include <asm/text-patching.h>
|
2012-03-05 20:32:22 +07:00
|
|
|
#include <asm/cacheflush.h>
|
|
|
|
#include <asm/desc.h>
|
|
|
|
#include <asm/pgtable.h>
|
2016-12-25 02:46:01 +07:00
|
|
|
#include <linux/uaccess.h>
|
2012-03-05 20:32:22 +07:00
|
|
|
#include <asm/alternative.h>
|
|
|
|
#include <asm/insn.h>
|
|
|
|
#include <asm/debugreg.h>
|
2017-05-09 05:58:47 +07:00
|
|
|
#include <asm/set_memory.h>
|
2017-08-03 09:39:26 +07:00
|
|
|
#include <asm/sections.h>
|
2018-01-18 23:15:20 +07:00
|
|
|
#include <asm/nospec-branch.h>
|
2012-03-05 20:32:22 +07:00
|
|
|
|
2012-09-28 15:15:22 +07:00
|
|
|
#include "common.h"
|
2012-03-05 20:32:22 +07:00
|
|
|
|
|
|
|
unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsigned long addr)
|
|
|
|
{
|
|
|
|
struct optimized_kprobe *op;
|
|
|
|
struct kprobe *kp;
|
|
|
|
long offs;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < RELATIVEJUMP_SIZE; i++) {
|
|
|
|
kp = get_kprobe((void *)addr - i);
|
|
|
|
/* This function only handles jump-optimized kprobe */
|
|
|
|
if (kp && kprobe_optimized(kp)) {
|
|
|
|
op = container_of(kp, struct optimized_kprobe, kp);
|
|
|
|
/* If op->list is not empty, op is under optimizing */
|
|
|
|
if (list_empty(&op->list))
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
found:
|
|
|
|
/*
|
|
|
|
* If the kprobe can be optimized, original bytes which can be
|
|
|
|
* overwritten by jump destination address. In this case, original
|
|
|
|
* bytes must be recovered from op->optinsn.copied_insn buffer.
|
|
|
|
*/
|
2017-03-29 12:03:56 +07:00
|
|
|
if (probe_kernel_read(buf, (void *)addr,
|
|
|
|
MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
|
|
|
|
return 0UL;
|
|
|
|
|
2012-03-05 20:32:22 +07:00
|
|
|
if (addr == (unsigned long)kp->addr) {
|
|
|
|
buf[0] = kp->opcode;
|
|
|
|
memcpy(buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
|
|
|
|
} else {
|
|
|
|
offs = addr - (unsigned long)kp->addr - 1;
|
|
|
|
memcpy(buf, op->optinsn.copied_insn + offs, RELATIVE_ADDR_SIZE - offs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (unsigned long)buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert a move instruction which sets a pointer to eax/rdi (1st arg). */
|
2014-04-17 15:17:47 +07:00
|
|
|
static void synthesize_set_arg1(kprobe_opcode_t *addr, unsigned long val)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
*addr++ = 0x48;
|
|
|
|
*addr++ = 0xbf;
|
|
|
|
#else
|
|
|
|
*addr++ = 0xb8;
|
|
|
|
#endif
|
|
|
|
*(unsigned long *)addr = val;
|
|
|
|
}
|
|
|
|
|
2013-08-06 05:02:41 +07:00
|
|
|
asm (
|
2019-02-12 23:11:47 +07:00
|
|
|
".pushsection .rodata\n"
|
2017-06-28 22:11:06 +07:00
|
|
|
"optprobe_template_func:\n"
|
2012-03-05 20:32:22 +07:00
|
|
|
".global optprobe_template_entry\n"
|
|
|
|
"optprobe_template_entry:\n"
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
/* We don't bother saving the ss register */
|
|
|
|
" pushq %rsp\n"
|
|
|
|
" pushfq\n"
|
|
|
|
SAVE_REGS_STRING
|
|
|
|
" movq %rsp, %rsi\n"
|
|
|
|
".global optprobe_template_val\n"
|
|
|
|
"optprobe_template_val:\n"
|
|
|
|
ASM_NOP5
|
|
|
|
ASM_NOP5
|
|
|
|
".global optprobe_template_call\n"
|
|
|
|
"optprobe_template_call:\n"
|
|
|
|
ASM_NOP5
|
|
|
|
/* Move flags to rsp */
|
|
|
|
" movq 144(%rsp), %rdx\n"
|
|
|
|
" movq %rdx, 152(%rsp)\n"
|
|
|
|
RESTORE_REGS_STRING
|
|
|
|
/* Skip flags entry */
|
|
|
|
" addq $8, %rsp\n"
|
|
|
|
" popfq\n"
|
|
|
|
#else /* CONFIG_X86_32 */
|
|
|
|
" pushf\n"
|
|
|
|
SAVE_REGS_STRING
|
|
|
|
" movl %esp, %edx\n"
|
|
|
|
".global optprobe_template_val\n"
|
|
|
|
"optprobe_template_val:\n"
|
|
|
|
ASM_NOP5
|
|
|
|
".global optprobe_template_call\n"
|
|
|
|
"optprobe_template_call:\n"
|
|
|
|
ASM_NOP5
|
|
|
|
RESTORE_REGS_STRING
|
|
|
|
" addl $4, %esp\n" /* skip cs */
|
|
|
|
" popf\n"
|
|
|
|
#endif
|
|
|
|
".global optprobe_template_end\n"
|
2017-06-28 22:11:06 +07:00
|
|
|
"optprobe_template_end:\n"
|
2019-02-12 23:11:47 +07:00
|
|
|
".popsection\n");
|
2017-06-28 22:11:06 +07:00
|
|
|
|
|
|
|
void optprobe_template_func(void);
|
|
|
|
STACK_FRAME_NON_STANDARD(optprobe_template_func);
|
2012-03-05 20:32:22 +07:00
|
|
|
|
|
|
|
#define TMPL_MOVE_IDX \
|
2017-08-18 15:25:08 +07:00
|
|
|
((long)optprobe_template_val - (long)optprobe_template_entry)
|
2012-03-05 20:32:22 +07:00
|
|
|
#define TMPL_CALL_IDX \
|
2017-08-18 15:25:08 +07:00
|
|
|
((long)optprobe_template_call - (long)optprobe_template_entry)
|
2012-03-05 20:32:22 +07:00
|
|
|
#define TMPL_END_IDX \
|
2017-08-18 15:25:08 +07:00
|
|
|
((long)optprobe_template_end - (long)optprobe_template_entry)
|
2012-03-05 20:32:22 +07:00
|
|
|
|
|
|
|
#define INT3_SIZE sizeof(kprobe_opcode_t)
|
|
|
|
|
|
|
|
/* Optimized kprobe call back function: called from optinsn */
|
2014-04-17 15:18:14 +07:00
|
|
|
static void
|
|
|
|
optimized_callback(struct optimized_kprobe *op, struct pt_regs *regs)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
|
|
|
/* This is possible if op is under delayed unoptimizing */
|
|
|
|
if (kprobe_disabled(&op->kp))
|
|
|
|
return;
|
|
|
|
|
2017-09-19 17:00:59 +07:00
|
|
|
preempt_disable();
|
2012-03-05 20:32:22 +07:00
|
|
|
if (kprobe_running()) {
|
|
|
|
kprobes_inc_nmissed_count(&op->kp);
|
|
|
|
} else {
|
2017-09-19 16:59:39 +07:00
|
|
|
struct kprobe_ctlblk *kcb = get_kprobe_ctlblk();
|
2012-03-05 20:32:22 +07:00
|
|
|
/* Save skipped registers */
|
|
|
|
#ifdef CONFIG_X86_64
|
|
|
|
regs->cs = __KERNEL_CS;
|
|
|
|
#else
|
|
|
|
regs->cs = __KERNEL_CS | get_kernel_rpl();
|
|
|
|
regs->gs = 0;
|
|
|
|
#endif
|
|
|
|
regs->ip = (unsigned long)op->kp.addr + INT3_SIZE;
|
|
|
|
regs->orig_ax = ~0UL;
|
|
|
|
|
|
|
|
__this_cpu_write(current_kprobe, &op->kp);
|
|
|
|
kcb->kprobe_status = KPROBE_HIT_ACTIVE;
|
|
|
|
opt_pre_handler(&op->kp, regs);
|
|
|
|
__this_cpu_write(current_kprobe, NULL);
|
|
|
|
}
|
2018-10-20 16:47:53 +07:00
|
|
|
preempt_enable();
|
2012-03-05 20:32:22 +07:00
|
|
|
}
|
2014-04-17 15:18:14 +07:00
|
|
|
NOKPROBE_SYMBOL(optimized_callback);
|
2012-03-05 20:32:22 +07:00
|
|
|
|
2017-08-18 15:24:00 +07:00
|
|
|
static int copy_optimized_instructions(u8 *dest, u8 *src, u8 *real)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
2017-03-29 12:05:06 +07:00
|
|
|
struct insn insn;
|
2012-03-05 20:32:22 +07:00
|
|
|
int len = 0, ret;
|
|
|
|
|
|
|
|
while (len < RELATIVEJUMP_SIZE) {
|
kprobes/x86: Fix instruction patching corruption when copying more than one RIP-relative instruction
After copy_optimized_instructions() copies several instructions
to the working buffer it tries to fix up the real RIP address, but it
adjusts the RIP-relative instruction with an incorrect RIP address
for the 2nd and subsequent instructions due to a bug in the logic.
This will break the kernel pretty badly (with likely outcomes such as
a kernel freeze, a crash, or worse) because probed instructions can refer
to the wrong data.
For example putting kprobes on cpumask_next() typically hits this bug.
cpumask_next() is normally like below if CONFIG_CPUMASK_OFFSTACK=y
(in this case nr_cpumask_bits is an alias of nr_cpu_ids):
<cpumask_next>:
48 89 f0 mov %rsi,%rax
8b 35 7b fb e2 00 mov 0xe2fb7b(%rip),%esi # ffffffff82db9e64 <nr_cpu_ids>
55 push %rbp
...
If we put a kprobe on it and it gets jump-optimized, it gets
patched by the kprobes code like this:
<cpumask_next>:
e9 95 7d 07 1e jmpq 0xffffffffa000207a
7b fb jnp 0xffffffff81f8a2e2 <cpumask_next+2>
e2 00 loop 0xffffffff81f8a2e9 <cpumask_next+9>
55 push %rbp
This shows that the first two MOV instructions were copied to a
trampoline buffer at 0xffffffffa000207a.
Here is the disassembled result of the trampoline, skipping
the optprobe template instructions:
# Dump of assembly code from 0xffffffffa000207a to 0xffffffffa00020ea:
54 push %rsp
...
48 83 c4 08 add $0x8,%rsp
9d popfq
48 89 f0 mov %rsi,%rax
8b 35 82 7d db e2 mov -0x1d24827e(%rip),%esi # 0xffffffff82db9e67 <nr_cpu_ids+3>
This dump shows that the second MOV accesses *(nr_cpu_ids+3) instead of
the original *nr_cpu_ids. This leads to a kernel freeze because
cpumask_next() always returns 0 and for_each_cpu() never ends.
Fix this by adding 'len' correctly to the real RIP address while
copying.
[ mingo: Improved the changelog. ]
Reported-by: Michael Rodin <michael@rodin.online>
Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Reviewed-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
Cc: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: stable@vger.kernel.org # v4.15+
Fixes: 63fef14fc98a ("kprobes/x86: Make insn buffer always ROX and use text_poke()")
Link: http://lkml.kernel.org/r/153504457253.22602.1314289671019919596.stgit@devbox
Signed-off-by: Ingo Molnar <mingo@kernel.org>
2018-08-24 00:16:12 +07:00
|
|
|
ret = __copy_instruction(dest + len, src + len, real + len, &insn);
|
2017-03-29 12:05:06 +07:00
|
|
|
if (!ret || !can_boost(&insn, src + len))
|
2012-03-05 20:32:22 +07:00
|
|
|
return -EINVAL;
|
|
|
|
len += ret;
|
|
|
|
}
|
|
|
|
/* Check whether the address range is reserved */
|
|
|
|
if (ftrace_text_reserved(src, src + len - 1) ||
|
|
|
|
alternatives_text_reserved(src, src + len - 1) ||
|
|
|
|
jump_label_text_reserved(src, src + len - 1))
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether insn is indirect jump */
|
2018-01-18 23:15:20 +07:00
|
|
|
static int __insn_is_indirect_jump(struct insn *insn)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
|
|
|
return ((insn->opcode.bytes[0] == 0xff &&
|
|
|
|
(X86_MODRM_REG(insn->modrm.value) & 6) == 4) || /* Jump */
|
|
|
|
insn->opcode.bytes[0] == 0xea); /* Segment based jump */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether insn jumps into specified address range */
|
|
|
|
static int insn_jump_into_range(struct insn *insn, unsigned long start, int len)
|
|
|
|
{
|
|
|
|
unsigned long target = 0;
|
|
|
|
|
|
|
|
switch (insn->opcode.bytes[0]) {
|
|
|
|
case 0xe0: /* loopne */
|
|
|
|
case 0xe1: /* loope */
|
|
|
|
case 0xe2: /* loop */
|
|
|
|
case 0xe3: /* jcxz */
|
|
|
|
case 0xe9: /* near relative jump */
|
|
|
|
case 0xeb: /* short relative jump */
|
|
|
|
break;
|
|
|
|
case 0x0f:
|
|
|
|
if ((insn->opcode.bytes[1] & 0xf0) == 0x80) /* jcc near */
|
|
|
|
break;
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
if ((insn->opcode.bytes[0] & 0xf0) == 0x70) /* jcc short */
|
|
|
|
break;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
target = (unsigned long)insn->next_byte + insn->immediate.value;
|
|
|
|
|
|
|
|
return (start <= target && target <= start + len);
|
|
|
|
}
|
|
|
|
|
2018-01-18 23:15:20 +07:00
|
|
|
static int insn_is_indirect_jump(struct insn *insn)
|
|
|
|
{
|
|
|
|
int ret = __insn_is_indirect_jump(insn);
|
|
|
|
|
|
|
|
#ifdef CONFIG_RETPOLINE
|
|
|
|
/*
|
|
|
|
* Jump to x86_indirect_thunk_* is treated as an indirect jump.
|
|
|
|
* Note that even with CONFIG_RETPOLINE=y, the kernel compiled with
|
|
|
|
* older gcc may use indirect jump. So we add this check instead of
|
|
|
|
* replace indirect-jump check.
|
|
|
|
*/
|
|
|
|
if (!ret)
|
|
|
|
ret = insn_jump_into_range(insn,
|
|
|
|
(unsigned long)__indirect_thunk_start,
|
|
|
|
(unsigned long)__indirect_thunk_end -
|
|
|
|
(unsigned long)__indirect_thunk_start);
|
|
|
|
#endif
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-03-05 20:32:22 +07:00
|
|
|
/* Decode whole function to ensure any instructions don't jump into target */
|
2014-04-17 15:17:47 +07:00
|
|
|
static int can_optimize(unsigned long paddr)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
|
|
|
unsigned long addr, size = 0, offset = 0;
|
|
|
|
struct insn insn;
|
|
|
|
kprobe_opcode_t buf[MAX_INSN_SIZE];
|
|
|
|
|
|
|
|
/* Lookup symbol including addr */
|
|
|
|
if (!kallsyms_lookup_size_offset(paddr, &size, &offset))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do not optimize in the entry code due to the unstable
|
2017-08-03 09:39:26 +07:00
|
|
|
* stack handling and registers setup.
|
2012-03-05 20:32:22 +07:00
|
|
|
*/
|
2017-08-03 09:39:26 +07:00
|
|
|
if (((paddr >= (unsigned long)__entry_text_start) &&
|
|
|
|
(paddr < (unsigned long)__entry_text_end)) ||
|
|
|
|
((paddr >= (unsigned long)__irqentry_text_start) &&
|
|
|
|
(paddr < (unsigned long)__irqentry_text_end)))
|
2012-03-05 20:32:22 +07:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Check there is enough space for a relative jump. */
|
|
|
|
if (size - offset < RELATIVEJUMP_SIZE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Decode instructions */
|
|
|
|
addr = paddr - offset;
|
|
|
|
while (addr < paddr - offset + size) { /* Decode until function end */
|
2014-11-14 22:39:57 +07:00
|
|
|
unsigned long recovered_insn;
|
2012-03-05 20:32:22 +07:00
|
|
|
if (search_exception_tables(addr))
|
|
|
|
/*
|
|
|
|
* Since some fixup code will jumps into this function,
|
|
|
|
* we can't optimize kprobe in this function.
|
|
|
|
*/
|
|
|
|
return 0;
|
2014-11-14 22:39:57 +07:00
|
|
|
recovered_insn = recover_probed_instruction(buf, addr);
|
2015-02-20 21:07:30 +07:00
|
|
|
if (!recovered_insn)
|
|
|
|
return 0;
|
2014-11-14 22:39:57 +07:00
|
|
|
kernel_insn_init(&insn, (void *)recovered_insn, MAX_INSN_SIZE);
|
2012-03-05 20:32:22 +07:00
|
|
|
insn_get_length(&insn);
|
|
|
|
/* Another subsystem puts a breakpoint */
|
|
|
|
if (insn.opcode.bytes[0] == BREAKPOINT_INSTRUCTION)
|
|
|
|
return 0;
|
|
|
|
/* Recover address */
|
|
|
|
insn.kaddr = (void *)addr;
|
|
|
|
insn.next_byte = (void *)(addr + insn.length);
|
|
|
|
/* Check any instructions don't jump into target */
|
|
|
|
if (insn_is_indirect_jump(&insn) ||
|
|
|
|
insn_jump_into_range(&insn, paddr + INT3_SIZE,
|
|
|
|
RELATIVE_ADDR_SIZE))
|
|
|
|
return 0;
|
|
|
|
addr += insn.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check optimized_kprobe can actually be optimized. */
|
2014-04-17 15:17:47 +07:00
|
|
|
int arch_check_optimized_kprobe(struct optimized_kprobe *op)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct kprobe *p;
|
|
|
|
|
|
|
|
for (i = 1; i < op->optinsn.size; i++) {
|
|
|
|
p = get_kprobe(op->kp.addr + i);
|
|
|
|
if (p && !kprobe_disabled(p))
|
|
|
|
return -EEXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the addr is within the optimized instructions. */
|
2014-04-17 15:17:47 +07:00
|
|
|
int arch_within_optimized_kprobe(struct optimized_kprobe *op,
|
|
|
|
unsigned long addr)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
|
|
|
return ((unsigned long)op->kp.addr <= addr &&
|
|
|
|
(unsigned long)op->kp.addr + op->optinsn.size > addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free optimized instruction slot */
|
2014-04-17 15:17:47 +07:00
|
|
|
static
|
2012-03-05 20:32:22 +07:00
|
|
|
void __arch_remove_optimized_kprobe(struct optimized_kprobe *op, int dirty)
|
|
|
|
{
|
|
|
|
if (op->optinsn.insn) {
|
|
|
|
free_optinsn_slot(op->optinsn.insn, dirty);
|
|
|
|
op->optinsn.insn = NULL;
|
|
|
|
op->optinsn.size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 15:17:47 +07:00
|
|
|
void arch_remove_optimized_kprobe(struct optimized_kprobe *op)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
|
|
|
__arch_remove_optimized_kprobe(op, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy replacing target instructions
|
|
|
|
* Target instructions MUST be relocatable (checked inside)
|
|
|
|
* This is called when new aggr(opt)probe is allocated or reused.
|
|
|
|
*/
|
2015-01-05 18:29:32 +07:00
|
|
|
int arch_prepare_optimized_kprobe(struct optimized_kprobe *op,
|
|
|
|
struct kprobe *__unused)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
2017-08-18 15:24:00 +07:00
|
|
|
u8 *buf = NULL, *slot;
|
|
|
|
int ret, len;
|
2012-03-05 20:32:22 +07:00
|
|
|
long rel;
|
|
|
|
|
|
|
|
if (!can_optimize((unsigned long)op->kp.addr))
|
|
|
|
return -EILSEQ;
|
|
|
|
|
2017-08-18 15:24:00 +07:00
|
|
|
buf = kzalloc(MAX_OPTINSN_SIZE, GFP_KERNEL);
|
|
|
|
if (!buf)
|
2012-03-05 20:32:22 +07:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2017-08-18 15:24:00 +07:00
|
|
|
op->optinsn.insn = slot = get_optinsn_slot();
|
|
|
|
if (!slot) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-03-05 20:32:22 +07:00
|
|
|
/*
|
|
|
|
* Verify if the address gap is in 2GB range, because this uses
|
|
|
|
* a relative jump.
|
|
|
|
*/
|
2017-08-18 15:24:00 +07:00
|
|
|
rel = (long)slot - (long)op->kp.addr + RELATIVEJUMP_SIZE;
|
2014-07-28 19:20:19 +07:00
|
|
|
if (abs(rel) > 0x7fffffff) {
|
2017-08-18 15:24:00 +07:00
|
|
|
ret = -ERANGE;
|
|
|
|
goto err;
|
2014-07-28 19:20:19 +07:00
|
|
|
}
|
2012-03-05 20:32:22 +07:00
|
|
|
|
2017-08-18 15:24:00 +07:00
|
|
|
/* Copy arch-dep-instance from template */
|
2017-08-18 15:25:08 +07:00
|
|
|
memcpy(buf, optprobe_template_entry, TMPL_END_IDX);
|
2012-03-05 20:32:22 +07:00
|
|
|
|
|
|
|
/* Copy instructions into the out-of-line buffer */
|
2017-08-18 15:24:00 +07:00
|
|
|
ret = copy_optimized_instructions(buf + TMPL_END_IDX, op->kp.addr,
|
|
|
|
slot + TMPL_END_IDX);
|
|
|
|
if (ret < 0)
|
|
|
|
goto err;
|
2012-03-05 20:32:22 +07:00
|
|
|
op->optinsn.size = ret;
|
2017-08-18 15:24:00 +07:00
|
|
|
len = TMPL_END_IDX + op->optinsn.size;
|
2012-03-05 20:32:22 +07:00
|
|
|
|
|
|
|
/* Set probe information */
|
|
|
|
synthesize_set_arg1(buf + TMPL_MOVE_IDX, (unsigned long)op);
|
|
|
|
|
|
|
|
/* Set probe function call */
|
2017-08-18 15:24:00 +07:00
|
|
|
synthesize_relcall(buf + TMPL_CALL_IDX,
|
|
|
|
slot + TMPL_CALL_IDX, optimized_callback);
|
2012-03-05 20:32:22 +07:00
|
|
|
|
|
|
|
/* Set returning jmp instruction at the tail of out-of-line buffer */
|
2017-08-18 15:24:00 +07:00
|
|
|
synthesize_reljump(buf + len, slot + len,
|
2012-03-05 20:32:22 +07:00
|
|
|
(u8 *)op->kp.addr + op->optinsn.size);
|
2017-08-18 15:24:00 +07:00
|
|
|
len += RELATIVEJUMP_SIZE;
|
|
|
|
|
|
|
|
/* We have to use text_poke for instuction buffer because it is RO */
|
|
|
|
text_poke(slot, buf, len);
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
kfree(buf);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
err:
|
|
|
|
__arch_remove_optimized_kprobe(op, 0);
|
|
|
|
goto out;
|
2012-03-05 20:32:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace breakpoints (int3) with relative jumps.
|
|
|
|
* Caller must call with locking kprobe_mutex and text_mutex.
|
|
|
|
*/
|
2014-04-17 15:17:47 +07:00
|
|
|
void arch_optimize_kprobes(struct list_head *oplist)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
|
|
|
struct optimized_kprobe *op, *tmp;
|
2013-07-18 18:47:50 +07:00
|
|
|
u8 insn_buf[RELATIVEJUMP_SIZE];
|
2012-03-05 20:32:22 +07:00
|
|
|
|
|
|
|
list_for_each_entry_safe(op, tmp, oplist, list) {
|
2013-07-18 18:47:50 +07:00
|
|
|
s32 rel = (s32)((long)op->optinsn.insn -
|
|
|
|
((long)op->kp.addr + RELATIVEJUMP_SIZE));
|
|
|
|
|
2012-03-05 20:32:22 +07:00
|
|
|
WARN_ON(kprobe_disabled(&op->kp));
|
2013-07-18 18:47:50 +07:00
|
|
|
|
|
|
|
/* Backup instructions which will be replaced by jump address */
|
|
|
|
memcpy(op->optinsn.copied_insn, op->kp.addr + INT3_SIZE,
|
|
|
|
RELATIVE_ADDR_SIZE);
|
|
|
|
|
|
|
|
insn_buf[0] = RELATIVEJUMP_OPCODE;
|
|
|
|
*(s32 *)(&insn_buf[1]) = rel;
|
|
|
|
|
|
|
|
text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
|
|
|
|
op->optinsn.insn);
|
|
|
|
|
2012-03-05 20:32:22 +07:00
|
|
|
list_del_init(&op->list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 18:47:50 +07:00
|
|
|
/* Replace a relative jump with a breakpoint (int3). */
|
2014-04-17 15:17:47 +07:00
|
|
|
void arch_unoptimize_kprobe(struct optimized_kprobe *op)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
2013-07-18 18:47:50 +07:00
|
|
|
u8 insn_buf[RELATIVEJUMP_SIZE];
|
|
|
|
|
2012-03-05 20:32:22 +07:00
|
|
|
/* Set int3 to first byte for kprobes */
|
|
|
|
insn_buf[0] = BREAKPOINT_INSTRUCTION;
|
|
|
|
memcpy(insn_buf + 1, op->optinsn.copied_insn, RELATIVE_ADDR_SIZE);
|
2013-07-18 18:47:50 +07:00
|
|
|
text_poke_bp(op->kp.addr, insn_buf, RELATIVEJUMP_SIZE,
|
|
|
|
op->optinsn.insn);
|
2012-03-05 20:32:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Recover original instructions and breakpoints from relative jumps.
|
|
|
|
* Caller must call with locking kprobe_mutex.
|
|
|
|
*/
|
|
|
|
extern void arch_unoptimize_kprobes(struct list_head *oplist,
|
|
|
|
struct list_head *done_list)
|
|
|
|
{
|
|
|
|
struct optimized_kprobe *op, *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(op, tmp, oplist, list) {
|
2013-07-18 18:47:50 +07:00
|
|
|
arch_unoptimize_kprobe(op);
|
2012-03-05 20:32:22 +07:00
|
|
|
list_move(&op->list, done_list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-17 15:18:14 +07:00
|
|
|
int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter)
|
2012-03-05 20:32:22 +07:00
|
|
|
{
|
|
|
|
struct optimized_kprobe *op;
|
|
|
|
|
|
|
|
if (p->flags & KPROBE_FLAG_OPTIMIZED) {
|
|
|
|
/* This kprobe is really able to run optimized path. */
|
|
|
|
op = container_of(p, struct optimized_kprobe, kp);
|
|
|
|
/* Detour through copied instructions */
|
|
|
|
regs->ip = (unsigned long)op->optinsn.insn + TMPL_END_IDX;
|
|
|
|
if (!reenter)
|
|
|
|
reset_current_kprobe();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-04-17 15:18:14 +07:00
|
|
|
NOKPROBE_SYMBOL(setup_detour_execution);
|