ftrace.c 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Code for replacing ftrace calls with jumps.
 *
 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
 *
 * Thanks goes to Ingo Molnar, for suggesting the idea.
 * Mathieu Desnoyers, for suggesting postponing the modifications.
 * Arjan van de Ven, for keeping me straight, and explaining to me
 * the dangers of modifying code on the run.
 */

#include <linux/spinlock.h>
#include <linux/hardirq.h>
14
#include <linux/uaccess.h>
15 16 17 18 19
#include <linux/ftrace.h>
#include <linux/percpu.h>
#include <linux/init.h>
#include <linux/list.h>

20
#include <asm/ftrace.h>
21
#include <asm/nops.h>
22 23


S
Steven Rostedt 已提交
24
/* Long is fine, even if it is only 4 bytes ;-) */
25
static unsigned long *ftrace_nop;
26 27

union ftrace_code_union {
28
	char code[MCOUNT_INSN_SIZE];
29 30 31 32 33 34
	struct {
		char e8;
		int offset;
	} __attribute__((packed));
};

35

36 37 38 39
static int notrace ftrace_calc_offset(long ip, long addr)
{
	return (int)(addr - ip);
}
40

41 42 43 44 45 46 47 48
notrace unsigned char *ftrace_nop_replace(void)
{
	return (char *)ftrace_nop;
}

notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
{
	static union ftrace_code_union calc;
49

50
	calc.e8		= 0xe8;
51
	calc.offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
52 53 54 55 56 57

	/*
	 * No locking needed, this must be called via kstop_machine
	 * which in essence is like running on a uniprocessor machine.
	 */
	return calc.code;
58 59
}

60
notrace int
61 62 63
ftrace_modify_code(unsigned long ip, unsigned char *old_code,
		   unsigned char *new_code)
{
64
	unsigned char replaced[MCOUNT_INSN_SIZE];
65 66 67 68

	/*
	 * Note: Due to modules and __init, code can
	 *  disappear and change, we need to protect against faulting
S
Steven Rostedt 已提交
69
	 *  as well as code changing. We do this by using the
S
Steven Rostedt 已提交
70
	 *  probe_kernel_* functions.
71 72
	 *
	 * No real locking needed, this code is run through
73
	 * kstop_machine, or before SMP starts.
74
	 */
S
Steven Rostedt 已提交
75 76

	/* read the text we want to modify */
S
Steven Rostedt 已提交
77
	if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
78
		return -EFAULT;
79

S
Steven Rostedt 已提交
80
	/* Make sure it is what we expect it to be */
81
	if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
82
		return -EINVAL;
83

S
Steven Rostedt 已提交
84
	/* replace the text with the new text */
S
Steven Rostedt 已提交
85
	if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
86
		return -EPERM;
87 88

	sync_core();
89

90
	return 0;
91 92
}

93 94 95
notrace int ftrace_update_ftrace_func(ftrace_func_t func)
{
	unsigned long ip = (unsigned long)(&ftrace_call);
96
	unsigned char old[MCOUNT_INSN_SIZE], *new;
97 98
	int ret;

99
	memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
100 101 102 103 104 105 106
	new = ftrace_call_replace(ip, (unsigned long)func);
	ret = ftrace_modify_code(ip, old, new);

	return ret;
}

int __init ftrace_dyn_arch_init(void *data)
107
{
108 109 110 111
	extern const unsigned char ftrace_test_p6nop[];
	extern const unsigned char ftrace_test_nop5[];
	extern const unsigned char ftrace_test_jmp[];
	int faulted = 0;
112

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
	/*
	 * There is no good nop for all x86 archs.
	 * We will default to using the P6_NOP5, but first we
	 * will test to make sure that the nop will actually
	 * work on this CPU. If it faults, we will then
	 * go to a lesser efficient 5 byte nop. If that fails
	 * we then just use a jmp as our nop. This isn't the most
	 * efficient nop, but we can not use a multi part nop
	 * since we would then risk being preempted in the middle
	 * of that nop, and if we enabled tracing then, it might
	 * cause a system crash.
	 *
	 * TODO: check the cpuid to determine the best nop.
	 */
	asm volatile (
		"jmp ftrace_test_jmp\n"
		/* This code needs to stay around */
		".section .text, \"ax\"\n"
		"ftrace_test_jmp:"
		"jmp ftrace_test_p6nop\n"
133 134 135
		"nop\n"
		"nop\n"
		"nop\n"  /* 2 byte jmp + 3 bytes */
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		"ftrace_test_p6nop:"
		P6_NOP5
		"jmp 1f\n"
		"ftrace_test_nop5:"
		".byte 0x66,0x66,0x66,0x66,0x90\n"
		"jmp 1f\n"
		".previous\n"
		"1:"
		".section .fixup, \"ax\"\n"
		"2:	movl $1, %0\n"
		"	jmp ftrace_test_nop5\n"
		"3:	movl $2, %0\n"
		"	jmp 1b\n"
		".previous\n"
		_ASM_EXTABLE(ftrace_test_p6nop, 2b)
		_ASM_EXTABLE(ftrace_test_nop5, 3b)
		: "=r"(faulted) : "0" (faulted));

	switch (faulted) {
	case 0:
		pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
		ftrace_nop = (unsigned long *)ftrace_test_p6nop;
		break;
	case 1:
		pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
		ftrace_nop = (unsigned long *)ftrace_test_nop5;
		break;
	case 2:
164
		pr_info("ftrace: converting mcount calls to jmp . + 5\n");
165 166 167 168 169 170
		ftrace_nop = (unsigned long *)ftrace_test_jmp;
		break;
	}

	/* The return code is retured via data */
	*(unsigned long *)data = 0;
S
Steven Rostedt 已提交
171

172 173
	return 0;
}