xref: /openbmc/linux/arch/x86/kernel/ftrace.c (revision 0231022cc32d5f2e7f3c06b75691dda0ad6aec33)
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 
123d083395SSteven Rostedt #include <linux/spinlock.h>
133d083395SSteven Rostedt #include <linux/hardirq.h>
146f93fc07SSteven Rostedt #include <linux/uaccess.h>
153d083395SSteven Rostedt #include <linux/ftrace.h>
163d083395SSteven Rostedt #include <linux/percpu.h>
1719b3e967SIngo Molnar #include <linux/sched.h>
183d083395SSteven Rostedt #include <linux/init.h>
193d083395SSteven Rostedt #include <linux/list.h>
203d083395SSteven Rostedt 
21395a59d0SAbhishek Sagar #include <asm/ftrace.h>
22caf4b323SFrederic Weisbecker #include <linux/ftrace.h>
23732f3ca7SSteven Rostedt #include <asm/nops.h>
24caf4b323SFrederic Weisbecker #include <asm/nmi.h>
25dfa60abaSSteven Rostedt 
263d083395SSteven Rostedt 
27caf4b323SFrederic Weisbecker #ifdef CONFIG_DYNAMIC_FTRACE
283d083395SSteven Rostedt 
293d083395SSteven Rostedt union ftrace_code_union {
30395a59d0SAbhishek Sagar 	char code[MCOUNT_INSN_SIZE];
313d083395SSteven Rostedt 	struct {
323d083395SSteven Rostedt 		char e8;
333d083395SSteven Rostedt 		int offset;
343d083395SSteven Rostedt 	} __attribute__((packed));
353d083395SSteven Rostedt };
363d083395SSteven Rostedt 
3715adc048SSteven Rostedt static int ftrace_calc_offset(long ip, long addr)
383c1720f0SSteven Rostedt {
393c1720f0SSteven Rostedt 	return (int)(addr - ip);
403d083395SSteven Rostedt }
413d083395SSteven Rostedt 
4231e88909SSteven Rostedt static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
433c1720f0SSteven Rostedt {
443c1720f0SSteven Rostedt 	static union ftrace_code_union calc;
453c1720f0SSteven Rostedt 
463c1720f0SSteven Rostedt 	calc.e8		= 0xe8;
47395a59d0SAbhishek Sagar 	calc.offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
483c1720f0SSteven Rostedt 
493c1720f0SSteven Rostedt 	/*
503c1720f0SSteven Rostedt 	 * No locking needed, this must be called via kstop_machine
513c1720f0SSteven Rostedt 	 * which in essence is like running on a uniprocessor machine.
523c1720f0SSteven Rostedt 	 */
533c1720f0SSteven Rostedt 	return calc.code;
543c1720f0SSteven Rostedt }
553c1720f0SSteven Rostedt 
5617666f02SSteven Rostedt /*
5717666f02SSteven Rostedt  * Modifying code must take extra care. On an SMP machine, if
5817666f02SSteven Rostedt  * the code being modified is also being executed on another CPU
5917666f02SSteven Rostedt  * that CPU will have undefined results and possibly take a GPF.
6017666f02SSteven Rostedt  * We use kstop_machine to stop other CPUS from exectuing code.
6117666f02SSteven Rostedt  * But this does not stop NMIs from happening. We still need
6217666f02SSteven Rostedt  * to protect against that. We separate out the modification of
6317666f02SSteven Rostedt  * the code to take care of this.
6417666f02SSteven Rostedt  *
6517666f02SSteven Rostedt  * Two buffers are added: An IP buffer and a "code" buffer.
6617666f02SSteven Rostedt  *
67a26a2a27SSteven Rostedt  * 1) Put the instruction pointer into the IP buffer
6817666f02SSteven Rostedt  *    and the new code into the "code" buffer.
6917666f02SSteven Rostedt  * 2) Set a flag that says we are modifying code
7017666f02SSteven Rostedt  * 3) Wait for any running NMIs to finish.
7117666f02SSteven Rostedt  * 4) Write the code
7217666f02SSteven Rostedt  * 5) clear the flag.
7317666f02SSteven Rostedt  * 6) Wait for any running NMIs to finish.
7417666f02SSteven Rostedt  *
7517666f02SSteven Rostedt  * If an NMI is executed, the first thing it does is to call
7617666f02SSteven Rostedt  * "ftrace_nmi_enter". This will check if the flag is set to write
7717666f02SSteven Rostedt  * and if it is, it will write what is in the IP and "code" buffers.
7817666f02SSteven Rostedt  *
7917666f02SSteven Rostedt  * The trick is, it does not matter if everyone is writing the same
8017666f02SSteven Rostedt  * content to the code location. Also, if a CPU is executing code
8117666f02SSteven Rostedt  * it is OK to write to that code location if the contents being written
8217666f02SSteven Rostedt  * are the same as what exists.
8317666f02SSteven Rostedt  */
8417666f02SSteven Rostedt 
85a26a2a27SSteven Rostedt static atomic_t in_nmi = ATOMIC_INIT(0);
86a26a2a27SSteven Rostedt static int mod_code_status;		/* holds return value of text write */
87a26a2a27SSteven Rostedt static int mod_code_write;		/* set when NMI should do the write */
88a26a2a27SSteven Rostedt static void *mod_code_ip;		/* holds the IP to write to */
89a26a2a27SSteven Rostedt static void *mod_code_newcode;		/* holds the text to write to the IP */
9017666f02SSteven Rostedt 
91a26a2a27SSteven Rostedt static unsigned nmi_wait_count;
92a26a2a27SSteven Rostedt static atomic_t nmi_update_count = ATOMIC_INIT(0);
93b807c3d0SSteven Rostedt 
94b807c3d0SSteven Rostedt int ftrace_arch_read_dyn_info(char *buf, int size)
95b807c3d0SSteven Rostedt {
96b807c3d0SSteven Rostedt 	int r;
97b807c3d0SSteven Rostedt 
98b807c3d0SSteven Rostedt 	r = snprintf(buf, size, "%u %u",
99b807c3d0SSteven Rostedt 		     nmi_wait_count,
100b807c3d0SSteven Rostedt 		     atomic_read(&nmi_update_count));
101b807c3d0SSteven Rostedt 	return r;
102b807c3d0SSteven Rostedt }
103b807c3d0SSteven Rostedt 
10417666f02SSteven Rostedt static void ftrace_mod_code(void)
10517666f02SSteven Rostedt {
10617666f02SSteven Rostedt 	/*
10717666f02SSteven Rostedt 	 * Yes, more than one CPU process can be writing to mod_code_status.
10817666f02SSteven Rostedt 	 *    (and the code itself)
10917666f02SSteven Rostedt 	 * But if one were to fail, then they all should, and if one were
11017666f02SSteven Rostedt 	 * to succeed, then they all should.
11117666f02SSteven Rostedt 	 */
11217666f02SSteven Rostedt 	mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
11317666f02SSteven Rostedt 					     MCOUNT_INSN_SIZE);
11417666f02SSteven Rostedt 
11517666f02SSteven Rostedt }
11617666f02SSteven Rostedt 
11717666f02SSteven Rostedt void ftrace_nmi_enter(void)
11817666f02SSteven Rostedt {
11917666f02SSteven Rostedt 	atomic_inc(&in_nmi);
12017666f02SSteven Rostedt 	/* Must have in_nmi seen before reading write flag */
12117666f02SSteven Rostedt 	smp_mb();
122b807c3d0SSteven Rostedt 	if (mod_code_write) {
12317666f02SSteven Rostedt 		ftrace_mod_code();
124b807c3d0SSteven Rostedt 		atomic_inc(&nmi_update_count);
125b807c3d0SSteven Rostedt 	}
12617666f02SSteven Rostedt }
12717666f02SSteven Rostedt 
12817666f02SSteven Rostedt void ftrace_nmi_exit(void)
12917666f02SSteven Rostedt {
13017666f02SSteven Rostedt 	/* Finish all executions before clearing in_nmi */
13117666f02SSteven Rostedt 	smp_wmb();
13217666f02SSteven Rostedt 	atomic_dec(&in_nmi);
13317666f02SSteven Rostedt }
13417666f02SSteven Rostedt 
13517666f02SSteven Rostedt static void wait_for_nmi(void)
13617666f02SSteven Rostedt {
137b807c3d0SSteven Rostedt 	int waited = 0;
138b807c3d0SSteven Rostedt 
139b807c3d0SSteven Rostedt 	while (atomic_read(&in_nmi)) {
140b807c3d0SSteven Rostedt 		waited = 1;
14117666f02SSteven Rostedt 		cpu_relax();
14217666f02SSteven Rostedt 	}
14317666f02SSteven Rostedt 
144b807c3d0SSteven Rostedt 	if (waited)
145b807c3d0SSteven Rostedt 		nmi_wait_count++;
146b807c3d0SSteven Rostedt }
147b807c3d0SSteven Rostedt 
14817666f02SSteven Rostedt static int
14917666f02SSteven Rostedt do_ftrace_mod_code(unsigned long ip, void *new_code)
15017666f02SSteven Rostedt {
15117666f02SSteven Rostedt 	mod_code_ip = (void *)ip;
15217666f02SSteven Rostedt 	mod_code_newcode = new_code;
15317666f02SSteven Rostedt 
15417666f02SSteven Rostedt 	/* The buffers need to be visible before we let NMIs write them */
15517666f02SSteven Rostedt 	smp_wmb();
15617666f02SSteven Rostedt 
15717666f02SSteven Rostedt 	mod_code_write = 1;
15817666f02SSteven Rostedt 
15917666f02SSteven Rostedt 	/* Make sure write bit is visible before we wait on NMIs */
16017666f02SSteven Rostedt 	smp_mb();
16117666f02SSteven Rostedt 
16217666f02SSteven Rostedt 	wait_for_nmi();
16317666f02SSteven Rostedt 
16417666f02SSteven Rostedt 	/* Make sure all running NMIs have finished before we write the code */
16517666f02SSteven Rostedt 	smp_mb();
16617666f02SSteven Rostedt 
16717666f02SSteven Rostedt 	ftrace_mod_code();
16817666f02SSteven Rostedt 
16917666f02SSteven Rostedt 	/* Make sure the write happens before clearing the bit */
17017666f02SSteven Rostedt 	smp_wmb();
17117666f02SSteven Rostedt 
17217666f02SSteven Rostedt 	mod_code_write = 0;
17317666f02SSteven Rostedt 
17417666f02SSteven Rostedt 	/* make sure NMIs see the cleared bit */
17517666f02SSteven Rostedt 	smp_mb();
17617666f02SSteven Rostedt 
17717666f02SSteven Rostedt 	wait_for_nmi();
17817666f02SSteven Rostedt 
17917666f02SSteven Rostedt 	return mod_code_status;
18017666f02SSteven Rostedt }
18117666f02SSteven Rostedt 
18217666f02SSteven Rostedt 
183caf4b323SFrederic Weisbecker 
184caf4b323SFrederic Weisbecker 
185caf4b323SFrederic Weisbecker static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
186caf4b323SFrederic Weisbecker 
18731e88909SSteven Rostedt static unsigned char *ftrace_nop_replace(void)
188caf4b323SFrederic Weisbecker {
189caf4b323SFrederic Weisbecker 	return ftrace_nop;
190caf4b323SFrederic Weisbecker }
191caf4b323SFrederic Weisbecker 
19231e88909SSteven Rostedt static int
1933d083395SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned char *old_code,
1943d083395SSteven Rostedt 		   unsigned char *new_code)
1953d083395SSteven Rostedt {
1966f93fc07SSteven Rostedt 	unsigned char replaced[MCOUNT_INSN_SIZE];
1973d083395SSteven Rostedt 
1983d083395SSteven Rostedt 	/*
1993d083395SSteven Rostedt 	 * Note: Due to modules and __init, code can
2003d083395SSteven Rostedt 	 *  disappear and change, we need to protect against faulting
20176aefee5SSteven Rostedt 	 *  as well as code changing. We do this by using the
202ab9a0918SSteven Rostedt 	 *  probe_kernel_* functions.
2033d083395SSteven Rostedt 	 *
2043d083395SSteven Rostedt 	 * No real locking needed, this code is run through
2056f93fc07SSteven Rostedt 	 * kstop_machine, or before SMP starts.
2063d083395SSteven Rostedt 	 */
20776aefee5SSteven Rostedt 
20876aefee5SSteven Rostedt 	/* read the text we want to modify */
209ab9a0918SSteven Rostedt 	if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
210593eb8a2SSteven Rostedt 		return -EFAULT;
2116f93fc07SSteven Rostedt 
21276aefee5SSteven Rostedt 	/* Make sure it is what we expect it to be */
2136f93fc07SSteven Rostedt 	if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
214593eb8a2SSteven Rostedt 		return -EINVAL;
2156f93fc07SSteven Rostedt 
21676aefee5SSteven Rostedt 	/* replace the text with the new text */
21717666f02SSteven Rostedt 	if (do_ftrace_mod_code(ip, new_code))
218593eb8a2SSteven Rostedt 		return -EPERM;
2196f93fc07SSteven Rostedt 
2203d083395SSteven Rostedt 	sync_core();
2213d083395SSteven Rostedt 
2226f93fc07SSteven Rostedt 	return 0;
2233d083395SSteven Rostedt }
2243d083395SSteven Rostedt 
22531e88909SSteven Rostedt int ftrace_make_nop(struct module *mod,
22631e88909SSteven Rostedt 		    struct dyn_ftrace *rec, unsigned long addr)
22731e88909SSteven Rostedt {
22831e88909SSteven Rostedt 	unsigned char *new, *old;
22931e88909SSteven Rostedt 	unsigned long ip = rec->ip;
23031e88909SSteven Rostedt 
23131e88909SSteven Rostedt 	old = ftrace_call_replace(ip, addr);
23231e88909SSteven Rostedt 	new = ftrace_nop_replace();
23331e88909SSteven Rostedt 
23431e88909SSteven Rostedt 	return ftrace_modify_code(rec->ip, old, new);
23531e88909SSteven Rostedt }
23631e88909SSteven Rostedt 
23731e88909SSteven Rostedt int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
23831e88909SSteven Rostedt {
23931e88909SSteven Rostedt 	unsigned char *new, *old;
24031e88909SSteven Rostedt 	unsigned long ip = rec->ip;
24131e88909SSteven Rostedt 
24231e88909SSteven Rostedt 	old = ftrace_nop_replace();
24331e88909SSteven Rostedt 	new = ftrace_call_replace(ip, addr);
24431e88909SSteven Rostedt 
24531e88909SSteven Rostedt 	return ftrace_modify_code(rec->ip, old, new);
24631e88909SSteven Rostedt }
24731e88909SSteven Rostedt 
24815adc048SSteven Rostedt int ftrace_update_ftrace_func(ftrace_func_t func)
249d61f82d0SSteven Rostedt {
250d61f82d0SSteven Rostedt 	unsigned long ip = (unsigned long)(&ftrace_call);
251395a59d0SAbhishek Sagar 	unsigned char old[MCOUNT_INSN_SIZE], *new;
252d61f82d0SSteven Rostedt 	int ret;
253d61f82d0SSteven Rostedt 
254395a59d0SAbhishek Sagar 	memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
255d61f82d0SSteven Rostedt 	new = ftrace_call_replace(ip, (unsigned long)func);
256d61f82d0SSteven Rostedt 	ret = ftrace_modify_code(ip, old, new);
257d61f82d0SSteven Rostedt 
258d61f82d0SSteven Rostedt 	return ret;
259d61f82d0SSteven Rostedt }
260d61f82d0SSteven Rostedt 
261d61f82d0SSteven Rostedt int __init ftrace_dyn_arch_init(void *data)
2623d083395SSteven Rostedt {
263732f3ca7SSteven Rostedt 	extern const unsigned char ftrace_test_p6nop[];
264732f3ca7SSteven Rostedt 	extern const unsigned char ftrace_test_nop5[];
265732f3ca7SSteven Rostedt 	extern const unsigned char ftrace_test_jmp[];
266732f3ca7SSteven Rostedt 	int faulted = 0;
2673d083395SSteven Rostedt 
268732f3ca7SSteven Rostedt 	/*
269732f3ca7SSteven Rostedt 	 * There is no good nop for all x86 archs.
270732f3ca7SSteven Rostedt 	 * We will default to using the P6_NOP5, but first we
271732f3ca7SSteven Rostedt 	 * will test to make sure that the nop will actually
272732f3ca7SSteven Rostedt 	 * work on this CPU. If it faults, we will then
273732f3ca7SSteven Rostedt 	 * go to a lesser efficient 5 byte nop. If that fails
274732f3ca7SSteven Rostedt 	 * we then just use a jmp as our nop. This isn't the most
275732f3ca7SSteven Rostedt 	 * efficient nop, but we can not use a multi part nop
276732f3ca7SSteven Rostedt 	 * since we would then risk being preempted in the middle
277732f3ca7SSteven Rostedt 	 * of that nop, and if we enabled tracing then, it might
278732f3ca7SSteven Rostedt 	 * cause a system crash.
279732f3ca7SSteven Rostedt 	 *
280732f3ca7SSteven Rostedt 	 * TODO: check the cpuid to determine the best nop.
281732f3ca7SSteven Rostedt 	 */
282732f3ca7SSteven Rostedt 	asm volatile (
283732f3ca7SSteven Rostedt 		"ftrace_test_jmp:"
284732f3ca7SSteven Rostedt 		"jmp ftrace_test_p6nop\n"
2858b27386aSAnders Kaseorg 		"nop\n"
2868b27386aSAnders Kaseorg 		"nop\n"
2878b27386aSAnders Kaseorg 		"nop\n"  /* 2 byte jmp + 3 bytes */
288732f3ca7SSteven Rostedt 		"ftrace_test_p6nop:"
289732f3ca7SSteven Rostedt 		P6_NOP5
290732f3ca7SSteven Rostedt 		"jmp 1f\n"
291732f3ca7SSteven Rostedt 		"ftrace_test_nop5:"
292732f3ca7SSteven Rostedt 		".byte 0x66,0x66,0x66,0x66,0x90\n"
293732f3ca7SSteven Rostedt 		"1:"
294732f3ca7SSteven Rostedt 		".section .fixup, \"ax\"\n"
295732f3ca7SSteven Rostedt 		"2:	movl $1, %0\n"
296732f3ca7SSteven Rostedt 		"	jmp ftrace_test_nop5\n"
297732f3ca7SSteven Rostedt 		"3:	movl $2, %0\n"
298732f3ca7SSteven Rostedt 		"	jmp 1b\n"
299732f3ca7SSteven Rostedt 		".previous\n"
300732f3ca7SSteven Rostedt 		_ASM_EXTABLE(ftrace_test_p6nop, 2b)
301732f3ca7SSteven Rostedt 		_ASM_EXTABLE(ftrace_test_nop5, 3b)
302732f3ca7SSteven Rostedt 		: "=r"(faulted) : "0" (faulted));
303d61f82d0SSteven Rostedt 
304732f3ca7SSteven Rostedt 	switch (faulted) {
305732f3ca7SSteven Rostedt 	case 0:
306732f3ca7SSteven Rostedt 		pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
3078115f3f0SSteven Rostedt 		memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
308732f3ca7SSteven Rostedt 		break;
309732f3ca7SSteven Rostedt 	case 1:
310732f3ca7SSteven Rostedt 		pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
3118115f3f0SSteven Rostedt 		memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
312732f3ca7SSteven Rostedt 		break;
313732f3ca7SSteven Rostedt 	case 2:
3148b27386aSAnders Kaseorg 		pr_info("ftrace: converting mcount calls to jmp . + 5\n");
3158115f3f0SSteven Rostedt 		memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
316732f3ca7SSteven Rostedt 		break;
317732f3ca7SSteven Rostedt 	}
318d61f82d0SSteven Rostedt 
319732f3ca7SSteven Rostedt 	/* The return code is retured via data */
320732f3ca7SSteven Rostedt 	*(unsigned long *)data = 0;
321dfa60abaSSteven Rostedt 
3223d083395SSteven Rostedt 	return 0;
3233d083395SSteven Rostedt }
324caf4b323SFrederic Weisbecker #endif
325e7d3737eSFrederic Weisbecker 
326e7d3737eSFrederic Weisbecker #ifdef CONFIG_FUNCTION_RET_TRACER
327e7d3737eSFrederic Weisbecker 
328e7d3737eSFrederic Weisbecker #ifndef CONFIG_DYNAMIC_FTRACE
329e7d3737eSFrederic Weisbecker 
330e7d3737eSFrederic Weisbecker /*
331e7d3737eSFrederic Weisbecker  * These functions are picked from those used on
332e7d3737eSFrederic Weisbecker  * this page for dynamic ftrace. They have been
333e7d3737eSFrederic Weisbecker  * simplified to ignore all traces in NMI context.
334e7d3737eSFrederic Weisbecker  */
335e7d3737eSFrederic Weisbecker static atomic_t in_nmi;
336e7d3737eSFrederic Weisbecker 
337e7d3737eSFrederic Weisbecker void ftrace_nmi_enter(void)
338e7d3737eSFrederic Weisbecker {
339e7d3737eSFrederic Weisbecker 	atomic_inc(&in_nmi);
340e7d3737eSFrederic Weisbecker }
341e7d3737eSFrederic Weisbecker 
342e7d3737eSFrederic Weisbecker void ftrace_nmi_exit(void)
343e7d3737eSFrederic Weisbecker {
344e7d3737eSFrederic Weisbecker 	atomic_dec(&in_nmi);
345e7d3737eSFrederic Weisbecker }
346e7d3737eSFrederic Weisbecker #endif /* !CONFIG_DYNAMIC_FTRACE */
347e7d3737eSFrederic Weisbecker 
348e7d3737eSFrederic Weisbecker /* Add a function return address to the trace stack on thread info.*/
349e7d3737eSFrederic Weisbecker static int push_return_trace(unsigned long ret, unsigned long long time,
350e7d3737eSFrederic Weisbecker 				unsigned long func)
351e7d3737eSFrederic Weisbecker {
352e7d3737eSFrederic Weisbecker 	int index;
353e7d3737eSFrederic Weisbecker 	struct thread_info *ti = current_thread_info();
354e7d3737eSFrederic Weisbecker 
355e7d3737eSFrederic Weisbecker 	/* The return trace stack is full */
356*0231022cSFrederic Weisbecker 	if (ti->curr_ret_stack == FTRACE_RET_STACK_SIZE - 1) {
357*0231022cSFrederic Weisbecker 		atomic_inc(&ti->trace_overrun);
358e7d3737eSFrederic Weisbecker 		return -EBUSY;
359*0231022cSFrederic Weisbecker 	}
360e7d3737eSFrederic Weisbecker 
361e7d3737eSFrederic Weisbecker 	index = ++ti->curr_ret_stack;
362e7d3737eSFrederic Weisbecker 	barrier();
363e7d3737eSFrederic Weisbecker 	ti->ret_stack[index].ret = ret;
364e7d3737eSFrederic Weisbecker 	ti->ret_stack[index].func = func;
365e7d3737eSFrederic Weisbecker 	ti->ret_stack[index].calltime = time;
366e7d3737eSFrederic Weisbecker 
367e7d3737eSFrederic Weisbecker 	return 0;
368e7d3737eSFrederic Weisbecker }
369e7d3737eSFrederic Weisbecker 
370e7d3737eSFrederic Weisbecker /* Retrieve a function return address to the trace stack on thread info.*/
371e7d3737eSFrederic Weisbecker static void pop_return_trace(unsigned long *ret, unsigned long long *time,
372*0231022cSFrederic Weisbecker 				unsigned long *func, unsigned long *overrun)
373e7d3737eSFrederic Weisbecker {
374e7d3737eSFrederic Weisbecker 	int index;
375e7d3737eSFrederic Weisbecker 
376e7d3737eSFrederic Weisbecker 	struct thread_info *ti = current_thread_info();
377e7d3737eSFrederic Weisbecker 	index = ti->curr_ret_stack;
378e7d3737eSFrederic Weisbecker 	*ret = ti->ret_stack[index].ret;
379e7d3737eSFrederic Weisbecker 	*func = ti->ret_stack[index].func;
380e7d3737eSFrederic Weisbecker 	*time = ti->ret_stack[index].calltime;
381*0231022cSFrederic Weisbecker 	*overrun = atomic_read(&ti->trace_overrun);
382e7d3737eSFrederic Weisbecker 	ti->curr_ret_stack--;
383e7d3737eSFrederic Weisbecker }
384e7d3737eSFrederic Weisbecker 
385e7d3737eSFrederic Weisbecker /*
386e7d3737eSFrederic Weisbecker  * Send the trace to the ring-buffer.
387e7d3737eSFrederic Weisbecker  * @return the original return address.
388e7d3737eSFrederic Weisbecker  */
389e7d3737eSFrederic Weisbecker unsigned long ftrace_return_to_handler(void)
390e7d3737eSFrederic Weisbecker {
391e7d3737eSFrederic Weisbecker 	struct ftrace_retfunc trace;
392*0231022cSFrederic Weisbecker 	pop_return_trace(&trace.ret, &trace.calltime, &trace.func,
393*0231022cSFrederic Weisbecker 			&trace.overrun);
394e7d3737eSFrederic Weisbecker 	trace.rettime = cpu_clock(raw_smp_processor_id());
395e7d3737eSFrederic Weisbecker 	ftrace_function_return(&trace);
396e7d3737eSFrederic Weisbecker 
397e7d3737eSFrederic Weisbecker 	return trace.ret;
398e7d3737eSFrederic Weisbecker }
399e7d3737eSFrederic Weisbecker 
400e7d3737eSFrederic Weisbecker /*
401e7d3737eSFrederic Weisbecker  * Hook the return address and push it in the stack of return addrs
402e7d3737eSFrederic Weisbecker  * in current thread info.
403e7d3737eSFrederic Weisbecker  */
404e7d3737eSFrederic Weisbecker void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
405e7d3737eSFrederic Weisbecker {
406e7d3737eSFrederic Weisbecker 	unsigned long old;
407e7d3737eSFrederic Weisbecker 	unsigned long long calltime;
408e7d3737eSFrederic Weisbecker 	int faulted;
409e7d3737eSFrederic Weisbecker 	unsigned long return_hooker = (unsigned long)
410e7d3737eSFrederic Weisbecker 				&return_to_handler;
411e7d3737eSFrederic Weisbecker 
412e7d3737eSFrederic Weisbecker 	/* Nmi's are currently unsupported */
413e7d3737eSFrederic Weisbecker 	if (atomic_read(&in_nmi))
414e7d3737eSFrederic Weisbecker 		return;
415e7d3737eSFrederic Weisbecker 
416e7d3737eSFrederic Weisbecker 	/*
417e7d3737eSFrederic Weisbecker 	 * Protect against fault, even if it shouldn't
418e7d3737eSFrederic Weisbecker 	 * happen. This tool is too much intrusive to
419e7d3737eSFrederic Weisbecker 	 * ignore such a protection.
420e7d3737eSFrederic Weisbecker 	 */
421e7d3737eSFrederic Weisbecker 	asm volatile(
422e7d3737eSFrederic Weisbecker 		"1: movl (%[parent_old]), %[old]\n"
423e7d3737eSFrederic Weisbecker 		"2: movl %[return_hooker], (%[parent_replaced])\n"
424e7d3737eSFrederic Weisbecker 		"   movl $0, %[faulted]\n"
425e7d3737eSFrederic Weisbecker 
426e7d3737eSFrederic Weisbecker 		".section .fixup, \"ax\"\n"
427e7d3737eSFrederic Weisbecker 		"3: movl $1, %[faulted]\n"
428e7d3737eSFrederic Weisbecker 		".previous\n"
429e7d3737eSFrederic Weisbecker 
430e7d3737eSFrederic Weisbecker 		".section __ex_table, \"a\"\n"
431e7d3737eSFrederic Weisbecker 		"   .long 1b, 3b\n"
432e7d3737eSFrederic Weisbecker 		"   .long 2b, 3b\n"
433e7d3737eSFrederic Weisbecker 		".previous\n"
434e7d3737eSFrederic Weisbecker 
435e7d3737eSFrederic Weisbecker 		: [parent_replaced] "=r" (parent), [old] "=r" (old),
436e7d3737eSFrederic Weisbecker 		  [faulted] "=r" (faulted)
437e7d3737eSFrederic Weisbecker 		: [parent_old] "0" (parent), [return_hooker] "r" (return_hooker)
438e7d3737eSFrederic Weisbecker 		: "memory"
439e7d3737eSFrederic Weisbecker 	);
440e7d3737eSFrederic Weisbecker 
441e7d3737eSFrederic Weisbecker 	if (WARN_ON(faulted)) {
442e7d3737eSFrederic Weisbecker 		unregister_ftrace_return();
443e7d3737eSFrederic Weisbecker 		return;
444e7d3737eSFrederic Weisbecker 	}
445e7d3737eSFrederic Weisbecker 
446e7d3737eSFrederic Weisbecker 	if (WARN_ON(!__kernel_text_address(old))) {
447e7d3737eSFrederic Weisbecker 		unregister_ftrace_return();
448e7d3737eSFrederic Weisbecker 		*parent = old;
449e7d3737eSFrederic Weisbecker 		return;
450e7d3737eSFrederic Weisbecker 	}
451e7d3737eSFrederic Weisbecker 
452e7d3737eSFrederic Weisbecker 	calltime = cpu_clock(raw_smp_processor_id());
453e7d3737eSFrederic Weisbecker 
454e7d3737eSFrederic Weisbecker 	if (push_return_trace(old, calltime, self_addr) == -EBUSY)
455e7d3737eSFrederic Weisbecker 		*parent = old;
456e7d3737eSFrederic Weisbecker }
457e7d3737eSFrederic Weisbecker 
458e7d3737eSFrederic Weisbecker #endif /* CONFIG_FUNCTION_RET_TRACER */
459