ftrace.c 4.1 KB
Newer Older
S
Steven Rostedt 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Code for replacing ftrace calls with jumps.
 *
 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
 *
 * Thanks goes out to P.A. Semi, Inc for supplying me with a PPC64 box.
 *
 */

#include <linux/spinlock.h>
#include <linux/hardirq.h>
12
#include <linux/uaccess.h>
S
Steven Rostedt 已提交
13 14 15 16 17 18
#include <linux/ftrace.h>
#include <linux/percpu.h>
#include <linux/init.h>
#include <linux/list.h>

#include <asm/cacheflush.h>
19
#include <asm/ftrace.h>
S
Steven Rostedt 已提交
20 21 22 23 24 25 26 27 28 29 30


static unsigned int ftrace_nop = 0x60000000;

#ifdef CONFIG_PPC32
# define GET_ADDR(addr) addr
#else
/* PowerPC64's functions are data that points to the functions */
# define GET_ADDR(addr) *(unsigned long *)addr
#endif

31

32
static unsigned int ftrace_calc_offset(long ip, long addr)
S
Steven Rostedt 已提交
33
{
34
	return (int)(addr - ip);
S
Steven Rostedt 已提交
35 36
}

37
static unsigned char *ftrace_nop_replace(void)
S
Steven Rostedt 已提交
38 39 40 41
{
	return (char *)&ftrace_nop;
}

42
static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
S
Steven Rostedt 已提交
43 44 45
{
	static unsigned int op;

S
Steven Rostedt 已提交
46 47 48 49 50 51
	/*
	 * It would be nice to just use create_function_call, but that will
	 * update the code itself. Here we need to just return the
	 * instruction that is going to be modified, without modifying the
	 * code.
	 */
S
Steven Rostedt 已提交
52 53 54
	addr = GET_ADDR(addr);

	/* Set to "bl addr" */
S
Steven Rostedt 已提交
55
	op = 0x48000001 | (ftrace_calc_offset(ip, addr) & 0x03fffffc);
S
Steven Rostedt 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

	/*
	 * No locking needed, this must be called via kstop_machine
	 * which in essence is like running on a uniprocessor machine.
	 */
	return (unsigned char *)&op;
}

#ifdef CONFIG_PPC64
# define _ASM_ALIGN	" .align 3 "
# define _ASM_PTR	" .llong "
#else
# define _ASM_ALIGN	" .align 2 "
# define _ASM_PTR	" .long "
#endif

72
static int
S
Steven Rostedt 已提交
73 74 75
ftrace_modify_code(unsigned long ip, unsigned char *old_code,
		   unsigned char *new_code)
{
76
	unsigned char replaced[MCOUNT_INSN_SIZE];
S
Steven Rostedt 已提交
77 78 79 80

	/*
	 * Note: Due to modules and __init, code can
	 *  disappear and change, we need to protect against faulting
81 82
	 *  as well as code changing. We do this by using the
	 *  probe_kernel_* functions.
S
Steven Rostedt 已提交
83 84
	 *
	 * No real locking needed, this code is run through
85
	 * kstop_machine, or before SMP starts.
S
Steven Rostedt 已提交
86
	 */
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

	/* read the text we want to modify */
	if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
		return -EFAULT;

	/* Make sure it is what we expect it to be */
	if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
		return -EINVAL;

	/* replace the text with the new text */
	if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
		return -EPERM;

	flush_icache_range(ip, ip + 8);

	return 0;
S
Steven Rostedt 已提交
103 104
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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
static int test_24bit_addr(unsigned long ip, unsigned long addr)
{
	long diff;

	/*
	 * Can we get to addr from ip in 24 bits?
	 *  (26 really, since we mulitply by 4 for 4 byte alignment)
	 */
	diff = addr - ip;

	/*
	 * Return true if diff is less than 1 << 25
	 *  and greater than -1 << 26.
	 */
	return (diff < (1 << 25)) && (diff > (-1 << 26));
}

int ftrace_make_nop(struct module *mod,
		    struct dyn_ftrace *rec, unsigned long addr)
{
	unsigned char *old, *new;

	/*
	 * If the calling address is more that 24 bits away,
	 * then we had to use a trampoline to make the call.
	 * Otherwise just update the call site.
	 */
	if (test_24bit_addr(rec->ip, addr)) {
		/* within range */
		old = ftrace_call_replace(rec->ip, addr);
		new = ftrace_nop_replace();
		return ftrace_modify_code(rec->ip, old, new);
	}

	return 0;
}

int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
{
	unsigned char *old, *new;

	/*
	 * If the calling address is more that 24 bits away,
	 * then we had to use a trampoline to make the call.
	 * Otherwise just update the call site.
	 */
	if (test_24bit_addr(rec->ip, addr)) {
		/* within range */
		old = ftrace_nop_replace();
		new = ftrace_call_replace(rec->ip, addr);
		return ftrace_modify_code(rec->ip, old, new);
	}

	return 0;
}

161
int ftrace_update_ftrace_func(ftrace_func_t func)
S
Steven Rostedt 已提交
162 163
{
	unsigned long ip = (unsigned long)(&ftrace_call);
164
	unsigned char old[MCOUNT_INSN_SIZE], *new;
S
Steven Rostedt 已提交
165 166
	int ret;

167
	memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
S
Steven Rostedt 已提交
168 169 170 171 172 173 174 175
	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)
{
176 177
	/* caller expects data to be zero */
	unsigned long *p = data;
S
Steven Rostedt 已提交
178

179
	*p = 0;
S
Steven Rostedt 已提交
180 181 182 183

	return 0;
}