xref: /openbmc/linux/arch/x86/kernel/ftrace.c (revision 3bb258bf430d29a24350fe4f44f8bf07b7b7a8f6)
13d083395SSteven Rostedt /*
23d083395SSteven Rostedt  * Code for replacing ftrace calls with jumps.
33d083395SSteven Rostedt  *
43d083395SSteven Rostedt  * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
53d083395SSteven Rostedt  *
63d083395SSteven Rostedt  * Thanks goes to Ingo Molnar, for suggesting the idea.
73d083395SSteven Rostedt  * Mathieu Desnoyers, for suggesting postponing the modifications.
83d083395SSteven Rostedt  * Arjan van de Ven, for keeping me straight, and explaining to me
93d083395SSteven Rostedt  * the dangers of modifying code on the run.
103d083395SSteven Rostedt  */
113d083395SSteven Rostedt 
12*3bb258bfSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13*3bb258bfSJoe Perches 
143d083395SSteven Rostedt #include <linux/spinlock.h>
153d083395SSteven Rostedt #include <linux/hardirq.h>
166f93fc07SSteven Rostedt #include <linux/uaccess.h>
173d083395SSteven Rostedt #include <linux/ftrace.h>
183d083395SSteven Rostedt #include <linux/percpu.h>
1919b3e967SIngo Molnar #include <linux/sched.h>
203d083395SSteven Rostedt #include <linux/init.h>
213d083395SSteven Rostedt #include <linux/list.h>
223d083395SSteven Rostedt 
2347788c58SFrederic Weisbecker #include <trace/syscall.h>
2447788c58SFrederic Weisbecker 
2516239630SSteven Rostedt #include <asm/cacheflush.h>
26395a59d0SAbhishek Sagar #include <asm/ftrace.h>
27732f3ca7SSteven Rostedt #include <asm/nops.h>
28caf4b323SFrederic Weisbecker #include <asm/nmi.h>
29dfa60abaSSteven Rostedt 
303d083395SSteven Rostedt 
31caf4b323SFrederic Weisbecker #ifdef CONFIG_DYNAMIC_FTRACE
323d083395SSteven Rostedt 
3316239630SSteven Rostedt int ftrace_arch_code_modify_prepare(void)
3416239630SSteven Rostedt {
3516239630SSteven Rostedt 	set_kernel_text_rw();
3616239630SSteven Rostedt 	return 0;
3716239630SSteven Rostedt }
3816239630SSteven Rostedt 
3916239630SSteven Rostedt int ftrace_arch_code_modify_post_process(void)
4016239630SSteven Rostedt {
4116239630SSteven Rostedt 	set_kernel_text_ro();
4216239630SSteven Rostedt 	return 0;
4316239630SSteven Rostedt }
4416239630SSteven Rostedt 
453d083395SSteven Rostedt union ftrace_code_union {
46395a59d0SAbhishek Sagar 	char code[MCOUNT_INSN_SIZE];
473d083395SSteven Rostedt 	struct {
483d083395SSteven Rostedt 		char e8;
493d083395SSteven Rostedt 		int offset;
503d083395SSteven Rostedt 	} __attribute__((packed));
513d083395SSteven Rostedt };
523d083395SSteven Rostedt 
5315adc048SSteven Rostedt static int ftrace_calc_offset(long ip, long addr)
543c1720f0SSteven Rostedt {
553c1720f0SSteven Rostedt 	return (int)(addr - ip);
563d083395SSteven Rostedt }
573d083395SSteven Rostedt 
5831e88909SSteven Rostedt static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
593c1720f0SSteven Rostedt {
603c1720f0SSteven Rostedt 	static union ftrace_code_union calc;
613c1720f0SSteven Rostedt 
623c1720f0SSteven Rostedt 	calc.e8		= 0xe8;
63395a59d0SAbhishek Sagar 	calc.offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
643c1720f0SSteven Rostedt 
653c1720f0SSteven Rostedt 	/*
663c1720f0SSteven Rostedt 	 * No locking needed, this must be called via kstop_machine
673c1720f0SSteven Rostedt 	 * which in essence is like running on a uniprocessor machine.
683c1720f0SSteven Rostedt 	 */
693c1720f0SSteven Rostedt 	return calc.code;
703c1720f0SSteven Rostedt }
713c1720f0SSteven Rostedt 
7217666f02SSteven Rostedt /*
7317666f02SSteven Rostedt  * Modifying code must take extra care. On an SMP machine, if
7417666f02SSteven Rostedt  * the code being modified is also being executed on another CPU
7517666f02SSteven Rostedt  * that CPU will have undefined results and possibly take a GPF.
7617666f02SSteven Rostedt  * We use kstop_machine to stop other CPUS from exectuing code.
7717666f02SSteven Rostedt  * But this does not stop NMIs from happening. We still need
7817666f02SSteven Rostedt  * to protect against that. We separate out the modification of
7917666f02SSteven Rostedt  * the code to take care of this.
8017666f02SSteven Rostedt  *
8117666f02SSteven Rostedt  * Two buffers are added: An IP buffer and a "code" buffer.
8217666f02SSteven Rostedt  *
83a26a2a27SSteven Rostedt  * 1) Put the instruction pointer into the IP buffer
8417666f02SSteven Rostedt  *    and the new code into the "code" buffer.
85e9d9df44SLai Jiangshan  * 2) Wait for any running NMIs to finish and set a flag that says
86e9d9df44SLai Jiangshan  *    we are modifying code, it is done in an atomic operation.
87e9d9df44SLai Jiangshan  * 3) Write the code
88e9d9df44SLai Jiangshan  * 4) clear the flag.
89e9d9df44SLai Jiangshan  * 5) Wait for any running NMIs to finish.
9017666f02SSteven Rostedt  *
9117666f02SSteven Rostedt  * If an NMI is executed, the first thing it does is to call
9217666f02SSteven Rostedt  * "ftrace_nmi_enter". This will check if the flag is set to write
9317666f02SSteven Rostedt  * and if it is, it will write what is in the IP and "code" buffers.
9417666f02SSteven Rostedt  *
9517666f02SSteven Rostedt  * The trick is, it does not matter if everyone is writing the same
9617666f02SSteven Rostedt  * content to the code location. Also, if a CPU is executing code
9717666f02SSteven Rostedt  * it is OK to write to that code location if the contents being written
9817666f02SSteven Rostedt  * are the same as what exists.
9917666f02SSteven Rostedt  */
10017666f02SSteven Rostedt 
101e9d9df44SLai Jiangshan #define MOD_CODE_WRITE_FLAG (1 << 31)	/* set when NMI should do the write */
1024e6ea144SSteven Rostedt static atomic_t nmi_running = ATOMIC_INIT(0);
103a26a2a27SSteven Rostedt static int mod_code_status;		/* holds return value of text write */
104a26a2a27SSteven Rostedt static void *mod_code_ip;		/* holds the IP to write to */
105a26a2a27SSteven Rostedt static void *mod_code_newcode;		/* holds the text to write to the IP */
10617666f02SSteven Rostedt 
107a26a2a27SSteven Rostedt static unsigned nmi_wait_count;
108a26a2a27SSteven Rostedt static atomic_t nmi_update_count = ATOMIC_INIT(0);
109b807c3d0SSteven Rostedt 
110b807c3d0SSteven Rostedt int ftrace_arch_read_dyn_info(char *buf, int size)
111b807c3d0SSteven Rostedt {
112b807c3d0SSteven Rostedt 	int r;
113b807c3d0SSteven Rostedt 
114b807c3d0SSteven Rostedt 	r = snprintf(buf, size, "%u %u",
115b807c3d0SSteven Rostedt 		     nmi_wait_count,
116b807c3d0SSteven Rostedt 		     atomic_read(&nmi_update_count));
117b807c3d0SSteven Rostedt 	return r;
118b807c3d0SSteven Rostedt }
119b807c3d0SSteven Rostedt 
120e9d9df44SLai Jiangshan static void clear_mod_flag(void)
121e9d9df44SLai Jiangshan {
122e9d9df44SLai Jiangshan 	int old = atomic_read(&nmi_running);
123e9d9df44SLai Jiangshan 
124e9d9df44SLai Jiangshan 	for (;;) {
125e9d9df44SLai Jiangshan 		int new = old & ~MOD_CODE_WRITE_FLAG;
126e9d9df44SLai Jiangshan 
127e9d9df44SLai Jiangshan 		if (old == new)
128e9d9df44SLai Jiangshan 			break;
129e9d9df44SLai Jiangshan 
130e9d9df44SLai Jiangshan 		old = atomic_cmpxchg(&nmi_running, old, new);
131e9d9df44SLai Jiangshan 	}
132e9d9df44SLai Jiangshan }
133e9d9df44SLai Jiangshan 
13417666f02SSteven Rostedt static void ftrace_mod_code(void)
13517666f02SSteven Rostedt {
13617666f02SSteven Rostedt 	/*
13717666f02SSteven Rostedt 	 * Yes, more than one CPU process can be writing to mod_code_status.
13817666f02SSteven Rostedt 	 *    (and the code itself)
13917666f02SSteven Rostedt 	 * But if one were to fail, then they all should, and if one were
14017666f02SSteven Rostedt 	 * to succeed, then they all should.
14117666f02SSteven Rostedt 	 */
14217666f02SSteven Rostedt 	mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
14317666f02SSteven Rostedt 					     MCOUNT_INSN_SIZE);
14490c7ac49SSteven Rostedt 
14590c7ac49SSteven Rostedt 	/* if we fail, then kill any new writers */
14690c7ac49SSteven Rostedt 	if (mod_code_status)
147e9d9df44SLai Jiangshan 		clear_mod_flag();
14817666f02SSteven Rostedt }
14917666f02SSteven Rostedt 
150a81bd80aSSteven Rostedt void ftrace_nmi_enter(void)
15117666f02SSteven Rostedt {
152e9d9df44SLai Jiangshan 	if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
153e9d9df44SLai Jiangshan 		smp_rmb();
15417666f02SSteven Rostedt 		ftrace_mod_code();
155b807c3d0SSteven Rostedt 		atomic_inc(&nmi_update_count);
156b807c3d0SSteven Rostedt 	}
157e9d9df44SLai Jiangshan 	/* Must have previous changes seen before executions */
158e9d9df44SLai Jiangshan 	smp_mb();
15917666f02SSteven Rostedt }
16017666f02SSteven Rostedt 
161a81bd80aSSteven Rostedt void ftrace_nmi_exit(void)
16217666f02SSteven Rostedt {
1634e6ea144SSteven Rostedt 	/* Finish all executions before clearing nmi_running */
164e9d9df44SLai Jiangshan 	smp_mb();
1654e6ea144SSteven Rostedt 	atomic_dec(&nmi_running);
16617666f02SSteven Rostedt }
16717666f02SSteven Rostedt 
168e9d9df44SLai Jiangshan static void wait_for_nmi_and_set_mod_flag(void)
169e9d9df44SLai Jiangshan {
170e9d9df44SLai Jiangshan 	if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
171e9d9df44SLai Jiangshan 		return;
172e9d9df44SLai Jiangshan 
173e9d9df44SLai Jiangshan 	do {
174e9d9df44SLai Jiangshan 		cpu_relax();
175e9d9df44SLai Jiangshan 	} while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
176e9d9df44SLai Jiangshan 
177e9d9df44SLai Jiangshan 	nmi_wait_count++;
178e9d9df44SLai Jiangshan }
179e9d9df44SLai Jiangshan 
18017666f02SSteven Rostedt static void wait_for_nmi(void)
18117666f02SSteven Rostedt {
1824e6ea144SSteven Rostedt 	if (!atomic_read(&nmi_running))
18389025282SCyrill Gorcunov 		return;
184b807c3d0SSteven Rostedt 
18589025282SCyrill Gorcunov 	do {
18617666f02SSteven Rostedt 		cpu_relax();
1874e6ea144SSteven Rostedt 	} while (atomic_read(&nmi_running));
18817666f02SSteven Rostedt 
189b807c3d0SSteven Rostedt 	nmi_wait_count++;
190b807c3d0SSteven Rostedt }
191b807c3d0SSteven Rostedt 
19217666f02SSteven Rostedt static int
19317666f02SSteven Rostedt do_ftrace_mod_code(unsigned long ip, void *new_code)
19417666f02SSteven Rostedt {
19517666f02SSteven Rostedt 	mod_code_ip = (void *)ip;
19617666f02SSteven Rostedt 	mod_code_newcode = new_code;
19717666f02SSteven Rostedt 
19817666f02SSteven Rostedt 	/* The buffers need to be visible before we let NMIs write them */
19917666f02SSteven Rostedt 	smp_mb();
20017666f02SSteven Rostedt 
201e9d9df44SLai Jiangshan 	wait_for_nmi_and_set_mod_flag();
20217666f02SSteven Rostedt 
20317666f02SSteven Rostedt 	/* Make sure all running NMIs have finished before we write the code */
20417666f02SSteven Rostedt 	smp_mb();
20517666f02SSteven Rostedt 
20617666f02SSteven Rostedt 	ftrace_mod_code();
20717666f02SSteven Rostedt 
20817666f02SSteven Rostedt 	/* Make sure the write happens before clearing the bit */
20917666f02SSteven Rostedt 	smp_mb();
21017666f02SSteven Rostedt 
211e9d9df44SLai Jiangshan 	clear_mod_flag();
21217666f02SSteven Rostedt 	wait_for_nmi();
21317666f02SSteven Rostedt 
21417666f02SSteven Rostedt 	return mod_code_status;
21517666f02SSteven Rostedt }
21617666f02SSteven Rostedt 
21717666f02SSteven Rostedt 
218caf4b323SFrederic Weisbecker 
219caf4b323SFrederic Weisbecker 
220caf4b323SFrederic Weisbecker static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
221caf4b323SFrederic Weisbecker 
22231e88909SSteven Rostedt static unsigned char *ftrace_nop_replace(void)
223caf4b323SFrederic Weisbecker {
224caf4b323SFrederic Weisbecker 	return ftrace_nop;
225caf4b323SFrederic Weisbecker }
226caf4b323SFrederic Weisbecker 
22731e88909SSteven Rostedt static int
2283d083395SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned char *old_code,
2293d083395SSteven Rostedt 		   unsigned char *new_code)
2303d083395SSteven Rostedt {
2316f93fc07SSteven Rostedt 	unsigned char replaced[MCOUNT_INSN_SIZE];
2323d083395SSteven Rostedt 
2333d083395SSteven Rostedt 	/*
2343d083395SSteven Rostedt 	 * Note: Due to modules and __init, code can
2353d083395SSteven Rostedt 	 *  disappear and change, we need to protect against faulting
23676aefee5SSteven Rostedt 	 *  as well as code changing. We do this by using the
237ab9a0918SSteven Rostedt 	 *  probe_kernel_* functions.
2383d083395SSteven Rostedt 	 *
2393d083395SSteven Rostedt 	 * No real locking needed, this code is run through
2406f93fc07SSteven Rostedt 	 * kstop_machine, or before SMP starts.
2413d083395SSteven Rostedt 	 */
24276aefee5SSteven Rostedt 
24376aefee5SSteven Rostedt 	/* read the text we want to modify */
244ab9a0918SSteven Rostedt 	if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
245593eb8a2SSteven Rostedt 		return -EFAULT;
2466f93fc07SSteven Rostedt 
24776aefee5SSteven Rostedt 	/* Make sure it is what we expect it to be */
2486f93fc07SSteven Rostedt 	if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
249593eb8a2SSteven Rostedt 		return -EINVAL;
2506f93fc07SSteven Rostedt 
25176aefee5SSteven Rostedt 	/* replace the text with the new text */
25217666f02SSteven Rostedt 	if (do_ftrace_mod_code(ip, new_code))
253593eb8a2SSteven Rostedt 		return -EPERM;
2546f93fc07SSteven Rostedt 
2553d083395SSteven Rostedt 	sync_core();
2563d083395SSteven Rostedt 
2576f93fc07SSteven Rostedt 	return 0;
2583d083395SSteven Rostedt }
2593d083395SSteven Rostedt 
26031e88909SSteven Rostedt int ftrace_make_nop(struct module *mod,
26131e88909SSteven Rostedt 		    struct dyn_ftrace *rec, unsigned long addr)
26231e88909SSteven Rostedt {
26331e88909SSteven Rostedt 	unsigned char *new, *old;
26431e88909SSteven Rostedt 	unsigned long ip = rec->ip;
26531e88909SSteven Rostedt 
26631e88909SSteven Rostedt 	old = ftrace_call_replace(ip, addr);
26731e88909SSteven Rostedt 	new = ftrace_nop_replace();
26831e88909SSteven Rostedt 
26931e88909SSteven Rostedt 	return ftrace_modify_code(rec->ip, old, new);
27031e88909SSteven Rostedt }
27131e88909SSteven Rostedt 
27231e88909SSteven Rostedt int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
27331e88909SSteven Rostedt {
27431e88909SSteven Rostedt 	unsigned char *new, *old;
27531e88909SSteven Rostedt 	unsigned long ip = rec->ip;
27631e88909SSteven Rostedt 
27731e88909SSteven Rostedt 	old = ftrace_nop_replace();
27831e88909SSteven Rostedt 	new = ftrace_call_replace(ip, addr);
27931e88909SSteven Rostedt 
28031e88909SSteven Rostedt 	return ftrace_modify_code(rec->ip, old, new);
28131e88909SSteven Rostedt }
28231e88909SSteven Rostedt 
28315adc048SSteven Rostedt int ftrace_update_ftrace_func(ftrace_func_t func)
284d61f82d0SSteven Rostedt {
285d61f82d0SSteven Rostedt 	unsigned long ip = (unsigned long)(&ftrace_call);
286395a59d0SAbhishek Sagar 	unsigned char old[MCOUNT_INSN_SIZE], *new;
287d61f82d0SSteven Rostedt 	int ret;
288d61f82d0SSteven Rostedt 
289395a59d0SAbhishek Sagar 	memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
290d61f82d0SSteven Rostedt 	new = ftrace_call_replace(ip, (unsigned long)func);
291d61f82d0SSteven Rostedt 	ret = ftrace_modify_code(ip, old, new);
292d61f82d0SSteven Rostedt 
293d61f82d0SSteven Rostedt 	return ret;
294d61f82d0SSteven Rostedt }
295d61f82d0SSteven Rostedt 
296d61f82d0SSteven Rostedt int __init ftrace_dyn_arch_init(void *data)
2973d083395SSteven Rostedt {
298732f3ca7SSteven Rostedt 	extern const unsigned char ftrace_test_p6nop[];
299732f3ca7SSteven Rostedt 	extern const unsigned char ftrace_test_nop5[];
300732f3ca7SSteven Rostedt 	extern const unsigned char ftrace_test_jmp[];
301732f3ca7SSteven Rostedt 	int faulted = 0;
3023d083395SSteven Rostedt 
303732f3ca7SSteven Rostedt 	/*
304732f3ca7SSteven Rostedt 	 * There is no good nop for all x86 archs.
305732f3ca7SSteven Rostedt 	 * We will default to using the P6_NOP5, but first we
306732f3ca7SSteven Rostedt 	 * will test to make sure that the nop will actually
307732f3ca7SSteven Rostedt 	 * work on this CPU. If it faults, we will then
308732f3ca7SSteven Rostedt 	 * go to a lesser efficient 5 byte nop. If that fails
309732f3ca7SSteven Rostedt 	 * we then just use a jmp as our nop. This isn't the most
310732f3ca7SSteven Rostedt 	 * efficient nop, but we can not use a multi part nop
311732f3ca7SSteven Rostedt 	 * since we would then risk being preempted in the middle
312732f3ca7SSteven Rostedt 	 * of that nop, and if we enabled tracing then, it might
313732f3ca7SSteven Rostedt 	 * cause a system crash.
314732f3ca7SSteven Rostedt 	 *
315732f3ca7SSteven Rostedt 	 * TODO: check the cpuid to determine the best nop.
316732f3ca7SSteven Rostedt 	 */
317732f3ca7SSteven Rostedt 	asm volatile (
318732f3ca7SSteven Rostedt 		"ftrace_test_jmp:"
319732f3ca7SSteven Rostedt 		"jmp ftrace_test_p6nop\n"
3208b27386aSAnders Kaseorg 		"nop\n"
3218b27386aSAnders Kaseorg 		"nop\n"
3228b27386aSAnders Kaseorg 		"nop\n"  /* 2 byte jmp + 3 bytes */
323732f3ca7SSteven Rostedt 		"ftrace_test_p6nop:"
324732f3ca7SSteven Rostedt 		P6_NOP5
325732f3ca7SSteven Rostedt 		"jmp 1f\n"
326732f3ca7SSteven Rostedt 		"ftrace_test_nop5:"
327732f3ca7SSteven Rostedt 		".byte 0x66,0x66,0x66,0x66,0x90\n"
328732f3ca7SSteven Rostedt 		"1:"
329732f3ca7SSteven Rostedt 		".section .fixup, \"ax\"\n"
330732f3ca7SSteven Rostedt 		"2:	movl $1, %0\n"
331732f3ca7SSteven Rostedt 		"	jmp ftrace_test_nop5\n"
332732f3ca7SSteven Rostedt 		"3:	movl $2, %0\n"
333732f3ca7SSteven Rostedt 		"	jmp 1b\n"
334732f3ca7SSteven Rostedt 		".previous\n"
335732f3ca7SSteven Rostedt 		_ASM_EXTABLE(ftrace_test_p6nop, 2b)
336732f3ca7SSteven Rostedt 		_ASM_EXTABLE(ftrace_test_nop5, 3b)
337732f3ca7SSteven Rostedt 		: "=r"(faulted) : "0" (faulted));
338d61f82d0SSteven Rostedt 
339732f3ca7SSteven Rostedt 	switch (faulted) {
340732f3ca7SSteven Rostedt 	case 0:
341*3bb258bfSJoe Perches 		pr_info("converting mcount calls to 0f 1f 44 00 00\n");
3428115f3f0SSteven Rostedt 		memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
343732f3ca7SSteven Rostedt 		break;
344732f3ca7SSteven Rostedt 	case 1:
345*3bb258bfSJoe Perches 		pr_info("converting mcount calls to 66 66 66 66 90\n");
3468115f3f0SSteven Rostedt 		memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
347732f3ca7SSteven Rostedt 		break;
348732f3ca7SSteven Rostedt 	case 2:
349*3bb258bfSJoe Perches 		pr_info("converting mcount calls to jmp . + 5\n");
3508115f3f0SSteven Rostedt 		memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
351732f3ca7SSteven Rostedt 		break;
352732f3ca7SSteven Rostedt 	}
353d61f82d0SSteven Rostedt 
354732f3ca7SSteven Rostedt 	/* The return code is retured via data */
355732f3ca7SSteven Rostedt 	*(unsigned long *)data = 0;
356dfa60abaSSteven Rostedt 
3573d083395SSteven Rostedt 	return 0;
3583d083395SSteven Rostedt }
359caf4b323SFrederic Weisbecker #endif
360e7d3737eSFrederic Weisbecker 
361fb52607aSFrederic Weisbecker #ifdef CONFIG_FUNCTION_GRAPH_TRACER
362e7d3737eSFrederic Weisbecker 
3635a45cfe1SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE
3645a45cfe1SSteven Rostedt extern void ftrace_graph_call(void);
3655a45cfe1SSteven Rostedt 
3665a45cfe1SSteven Rostedt static int ftrace_mod_jmp(unsigned long ip,
3675a45cfe1SSteven Rostedt 			  int old_offset, int new_offset)
3685a45cfe1SSteven Rostedt {
3695a45cfe1SSteven Rostedt 	unsigned char code[MCOUNT_INSN_SIZE];
3705a45cfe1SSteven Rostedt 
3715a45cfe1SSteven Rostedt 	if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
3725a45cfe1SSteven Rostedt 		return -EFAULT;
3735a45cfe1SSteven Rostedt 
3745a45cfe1SSteven Rostedt 	if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
3755a45cfe1SSteven Rostedt 		return -EINVAL;
3765a45cfe1SSteven Rostedt 
3775a45cfe1SSteven Rostedt 	*(int *)(&code[1]) = new_offset;
3785a45cfe1SSteven Rostedt 
3795a45cfe1SSteven Rostedt 	if (do_ftrace_mod_code(ip, &code))
3805a45cfe1SSteven Rostedt 		return -EPERM;
3815a45cfe1SSteven Rostedt 
3825a45cfe1SSteven Rostedt 	return 0;
3835a45cfe1SSteven Rostedt }
3845a45cfe1SSteven Rostedt 
3855a45cfe1SSteven Rostedt int ftrace_enable_ftrace_graph_caller(void)
3865a45cfe1SSteven Rostedt {
3875a45cfe1SSteven Rostedt 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
3885a45cfe1SSteven Rostedt 	int old_offset, new_offset;
3895a45cfe1SSteven Rostedt 
3905a45cfe1SSteven Rostedt 	old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
3915a45cfe1SSteven Rostedt 	new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
3925a45cfe1SSteven Rostedt 
3935a45cfe1SSteven Rostedt 	return ftrace_mod_jmp(ip, old_offset, new_offset);
3945a45cfe1SSteven Rostedt }
3955a45cfe1SSteven Rostedt 
3965a45cfe1SSteven Rostedt int ftrace_disable_ftrace_graph_caller(void)
3975a45cfe1SSteven Rostedt {
3985a45cfe1SSteven Rostedt 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
3995a45cfe1SSteven Rostedt 	int old_offset, new_offset;
4005a45cfe1SSteven Rostedt 
4015a45cfe1SSteven Rostedt 	old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
4025a45cfe1SSteven Rostedt 	new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
4035a45cfe1SSteven Rostedt 
4045a45cfe1SSteven Rostedt 	return ftrace_mod_jmp(ip, old_offset, new_offset);
4055a45cfe1SSteven Rostedt }
4065a45cfe1SSteven Rostedt 
407e7d3737eSFrederic Weisbecker #endif /* !CONFIG_DYNAMIC_FTRACE */
408e7d3737eSFrederic Weisbecker 
409e7d3737eSFrederic Weisbecker /*
410e7d3737eSFrederic Weisbecker  * Hook the return address and push it in the stack of return addrs
411e7d3737eSFrederic Weisbecker  * in current thread info.
412e7d3737eSFrederic Weisbecker  */
41371e308a2SSteven Rostedt void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
41471e308a2SSteven Rostedt 			   unsigned long frame_pointer)
415e7d3737eSFrederic Weisbecker {
416e7d3737eSFrederic Weisbecker 	unsigned long old;
417e7d3737eSFrederic Weisbecker 	int faulted;
418287b6e68SFrederic Weisbecker 	struct ftrace_graph_ent trace;
419e7d3737eSFrederic Weisbecker 	unsigned long return_hooker = (unsigned long)
420e7d3737eSFrederic Weisbecker 				&return_to_handler;
421e7d3737eSFrederic Weisbecker 
422380c4b14SFrederic Weisbecker 	if (unlikely(atomic_read(&current->tracing_graph_pause)))
423e7d3737eSFrederic Weisbecker 		return;
424e7d3737eSFrederic Weisbecker 
425e7d3737eSFrederic Weisbecker 	/*
426e7d3737eSFrederic Weisbecker 	 * Protect against fault, even if it shouldn't
427e7d3737eSFrederic Weisbecker 	 * happen. This tool is too much intrusive to
428e7d3737eSFrederic Weisbecker 	 * ignore such a protection.
429e7d3737eSFrederic Weisbecker 	 */
430e7d3737eSFrederic Weisbecker 	asm volatile(
43196665788SSteven Rostedt 		"1: " _ASM_MOV " (%[parent]), %[old]\n"
43296665788SSteven Rostedt 		"2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
433e7d3737eSFrederic Weisbecker 		"   movl $0, %[faulted]\n"
434e3944bfaSSteven Rostedt 		"3:\n"
435e7d3737eSFrederic Weisbecker 
436e7d3737eSFrederic Weisbecker 		".section .fixup, \"ax\"\n"
437e3944bfaSSteven Rostedt 		"4: movl $1, %[faulted]\n"
438e3944bfaSSteven Rostedt 		"   jmp 3b\n"
439e7d3737eSFrederic Weisbecker 		".previous\n"
440e7d3737eSFrederic Weisbecker 
441e3944bfaSSteven Rostedt 		_ASM_EXTABLE(1b, 4b)
442e3944bfaSSteven Rostedt 		_ASM_EXTABLE(2b, 4b)
443e7d3737eSFrederic Weisbecker 
444aa512a27SSteven Rostedt 		: [old] "=&r" (old), [faulted] "=r" (faulted)
44596665788SSteven Rostedt 		: [parent] "r" (parent), [return_hooker] "r" (return_hooker)
446e7d3737eSFrederic Weisbecker 		: "memory"
447e7d3737eSFrederic Weisbecker 	);
448e7d3737eSFrederic Weisbecker 
44914a866c5SSteven Rostedt 	if (unlikely(faulted)) {
45014a866c5SSteven Rostedt 		ftrace_graph_stop();
45114a866c5SSteven Rostedt 		WARN_ON(1);
452e7d3737eSFrederic Weisbecker 		return;
453e7d3737eSFrederic Weisbecker 	}
454e7d3737eSFrederic Weisbecker 
45571e308a2SSteven Rostedt 	if (ftrace_push_return_trace(old, self_addr, &trace.depth,
45671e308a2SSteven Rostedt 		    frame_pointer) == -EBUSY) {
457e7d3737eSFrederic Weisbecker 		*parent = old;
458287b6e68SFrederic Weisbecker 		return;
459287b6e68SFrederic Weisbecker 	}
460287b6e68SFrederic Weisbecker 
461287b6e68SFrederic Weisbecker 	trace.func = self_addr;
462287b6e68SFrederic Weisbecker 
463e49dc19cSSteven Rostedt 	/* Only trace if the calling function expects to */
464e49dc19cSSteven Rostedt 	if (!ftrace_graph_entry(&trace)) {
465e49dc19cSSteven Rostedt 		current->curr_ret_stack--;
466e49dc19cSSteven Rostedt 		*parent = old;
467e49dc19cSSteven Rostedt 	}
468e7d3737eSFrederic Weisbecker }
469fb52607aSFrederic Weisbecker #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
470f58ba100SFrederic Weisbecker 
471f58ba100SFrederic Weisbecker #ifdef CONFIG_FTRACE_SYSCALLS
472f58ba100SFrederic Weisbecker 
473f58ba100SFrederic Weisbecker extern unsigned long __start_syscalls_metadata[];
474f58ba100SFrederic Weisbecker extern unsigned long __stop_syscalls_metadata[];
475f58ba100SFrederic Weisbecker extern unsigned long *sys_call_table;
476f58ba100SFrederic Weisbecker 
477f58ba100SFrederic Weisbecker static struct syscall_metadata **syscalls_metadata;
478f58ba100SFrederic Weisbecker 
479f58ba100SFrederic Weisbecker static struct syscall_metadata *find_syscall_meta(unsigned long *syscall)
480f58ba100SFrederic Weisbecker {
481f58ba100SFrederic Weisbecker 	struct syscall_metadata *start;
482f58ba100SFrederic Weisbecker 	struct syscall_metadata *stop;
483f58ba100SFrederic Weisbecker 	char str[KSYM_SYMBOL_LEN];
484f58ba100SFrederic Weisbecker 
485f58ba100SFrederic Weisbecker 
486f58ba100SFrederic Weisbecker 	start = (struct syscall_metadata *)__start_syscalls_metadata;
487f58ba100SFrederic Weisbecker 	stop = (struct syscall_metadata *)__stop_syscalls_metadata;
488f58ba100SFrederic Weisbecker 	kallsyms_lookup((unsigned long) syscall, NULL, NULL, NULL, str);
489f58ba100SFrederic Weisbecker 
490f58ba100SFrederic Weisbecker 	for ( ; start < stop; start++) {
491f58ba100SFrederic Weisbecker 		if (start->name && !strcmp(start->name, str))
492f58ba100SFrederic Weisbecker 			return start;
493f58ba100SFrederic Weisbecker 	}
494f58ba100SFrederic Weisbecker 	return NULL;
495f58ba100SFrederic Weisbecker }
496f58ba100SFrederic Weisbecker 
497f58ba100SFrederic Weisbecker struct syscall_metadata *syscall_nr_to_meta(int nr)
498f58ba100SFrederic Weisbecker {
49957421dbbSJason Baron 	if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
500f58ba100SFrederic Weisbecker 		return NULL;
501f58ba100SFrederic Weisbecker 
502f58ba100SFrederic Weisbecker 	return syscalls_metadata[nr];
503f58ba100SFrederic Weisbecker }
504f58ba100SFrederic Weisbecker 
505eeac19a7SJason Baron int syscall_name_to_nr(char *name)
506eeac19a7SJason Baron {
507eeac19a7SJason Baron 	int i;
508eeac19a7SJason Baron 
509eeac19a7SJason Baron 	if (!syscalls_metadata)
510eeac19a7SJason Baron 		return -1;
511eeac19a7SJason Baron 
51257421dbbSJason Baron 	for (i = 0; i < NR_syscalls; i++) {
513eeac19a7SJason Baron 		if (syscalls_metadata[i]) {
514eeac19a7SJason Baron 			if (!strcmp(syscalls_metadata[i]->name, name))
515eeac19a7SJason Baron 				return i;
516eeac19a7SJason Baron 		}
517eeac19a7SJason Baron 	}
518eeac19a7SJason Baron 	return -1;
519eeac19a7SJason Baron }
520eeac19a7SJason Baron 
52164c12e04SJason Baron void set_syscall_enter_id(int num, int id)
52264c12e04SJason Baron {
52364c12e04SJason Baron 	syscalls_metadata[num]->enter_id = id;
52464c12e04SJason Baron }
52564c12e04SJason Baron 
52664c12e04SJason Baron void set_syscall_exit_id(int num, int id)
52764c12e04SJason Baron {
52864c12e04SJason Baron 	syscalls_metadata[num]->exit_id = id;
52964c12e04SJason Baron }
53064c12e04SJason Baron 
531066e0378SJason Baron static int __init arch_init_ftrace_syscalls(void)
532f58ba100SFrederic Weisbecker {
533f58ba100SFrederic Weisbecker 	int i;
534f58ba100SFrederic Weisbecker 	struct syscall_metadata *meta;
535f58ba100SFrederic Weisbecker 	unsigned long **psys_syscall_table = &sys_call_table;
536f58ba100SFrederic Weisbecker 
537f58ba100SFrederic Weisbecker 	syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
53857421dbbSJason Baron 					NR_syscalls, GFP_KERNEL);
539f58ba100SFrederic Weisbecker 	if (!syscalls_metadata) {
540f58ba100SFrederic Weisbecker 		WARN_ON(1);
541066e0378SJason Baron 		return -ENOMEM;
542f58ba100SFrederic Weisbecker 	}
543f58ba100SFrederic Weisbecker 
54457421dbbSJason Baron 	for (i = 0; i < NR_syscalls; i++) {
545f58ba100SFrederic Weisbecker 		meta = find_syscall_meta(psys_syscall_table[i]);
546f58ba100SFrederic Weisbecker 		syscalls_metadata[i] = meta;
547f58ba100SFrederic Weisbecker 	}
548066e0378SJason Baron 	return 0;
549f58ba100SFrederic Weisbecker }
550066e0378SJason Baron arch_initcall(arch_init_ftrace_syscalls);
551f58ba100SFrederic Weisbecker #endif
552