ftrace.c 4.1 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


24
static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
25 26

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

34

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

40
unsigned char *ftrace_nop_replace(void)
41
{
42
	return ftrace_nop;
43 44
}

45
unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
46 47
{
	static union ftrace_code_union calc;
48

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

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

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

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

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

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

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

	sync_core();
88

89
	return 0;
90 91
}

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

98
	memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
99 100 101 102 103 104 105
	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)
106
{
107 108 109 110
	extern const unsigned char ftrace_test_p6nop[];
	extern const unsigned char ftrace_test_nop5[];
	extern const unsigned char ftrace_test_jmp[];
	int faulted = 0;
111

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	/*
	 * 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 (
		"ftrace_test_jmp:"
		"jmp ftrace_test_p6nop\n"
129 130 131
		"nop\n"
		"nop\n"
		"nop\n"  /* 2 byte jmp + 3 bytes */
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		"ftrace_test_p6nop:"
		P6_NOP5
		"jmp 1f\n"
		"ftrace_test_nop5:"
		".byte 0x66,0x66,0x66,0x66,0x90\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");
151
		memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
152 153 154
		break;
	case 1:
		pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
155
		memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
156 157
		break;
	case 2:
158
		pr_info("ftrace: converting mcount calls to jmp . + 5\n");
159
		memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
160 161 162 163 164
		break;
	}

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

166 167
	return 0;
}