xref: /openbmc/linux/arch/x86/kernel/ftrace.c (revision 92550405c493a3c2fa14bf37d1d60cd6c7d0f585)
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 
123bb258bfSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
133bb258bfSJoe 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>
2284e1c6bbSmatthieu castet #include <linux/module.h>
233d083395SSteven Rostedt 
2447788c58SFrederic Weisbecker #include <trace/syscall.h>
2547788c58SFrederic Weisbecker 
2616239630SSteven Rostedt #include <asm/cacheflush.h>
2759a094c9SSteven Rostedt #include <asm/kprobes.h>
28395a59d0SAbhishek Sagar #include <asm/ftrace.h>
29732f3ca7SSteven Rostedt #include <asm/nops.h>
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();
3684e1c6bbSmatthieu castet 	set_all_modules_text_rw();
3716239630SSteven Rostedt 	return 0;
3816239630SSteven Rostedt }
3916239630SSteven Rostedt 
4016239630SSteven Rostedt int ftrace_arch_code_modify_post_process(void)
4116239630SSteven Rostedt {
4284e1c6bbSmatthieu castet 	set_all_modules_text_ro();
4316239630SSteven Rostedt 	set_kernel_text_ro();
4416239630SSteven Rostedt 	return 0;
4516239630SSteven Rostedt }
4616239630SSteven Rostedt 
473d083395SSteven Rostedt union ftrace_code_union {
48395a59d0SAbhishek Sagar 	char code[MCOUNT_INSN_SIZE];
493d083395SSteven Rostedt 	struct {
503d083395SSteven Rostedt 		char e8;
513d083395SSteven Rostedt 		int offset;
523d083395SSteven Rostedt 	} __attribute__((packed));
533d083395SSteven Rostedt };
543d083395SSteven Rostedt 
5515adc048SSteven Rostedt static int ftrace_calc_offset(long ip, long addr)
563c1720f0SSteven Rostedt {
573c1720f0SSteven Rostedt 	return (int)(addr - ip);
583d083395SSteven Rostedt }
593d083395SSteven Rostedt 
6031e88909SSteven Rostedt static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
613c1720f0SSteven Rostedt {
623c1720f0SSteven Rostedt 	static union ftrace_code_union calc;
633c1720f0SSteven Rostedt 
643c1720f0SSteven Rostedt 	calc.e8		= 0xe8;
65395a59d0SAbhishek Sagar 	calc.offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
663c1720f0SSteven Rostedt 
673c1720f0SSteven Rostedt 	/*
683c1720f0SSteven Rostedt 	 * No locking needed, this must be called via kstop_machine
693c1720f0SSteven Rostedt 	 * which in essence is like running on a uniprocessor machine.
703c1720f0SSteven Rostedt 	 */
713c1720f0SSteven Rostedt 	return calc.code;
723c1720f0SSteven Rostedt }
733c1720f0SSteven Rostedt 
7455ca3cc1SSuresh Siddha static inline int
7555ca3cc1SSuresh Siddha within(unsigned long addr, unsigned long start, unsigned long end)
7655ca3cc1SSuresh Siddha {
7755ca3cc1SSuresh Siddha 	return addr >= start && addr < end;
7855ca3cc1SSuresh Siddha }
7955ca3cc1SSuresh Siddha 
8087fbb2acSSteven Rostedt (Red Hat) static unsigned long text_ip_addr(unsigned long ip)
8117666f02SSteven Rostedt {
8255ca3cc1SSuresh Siddha 	/*
8355ca3cc1SSuresh Siddha 	 * On x86_64, kernel text mappings are mapped read-only with
8455ca3cc1SSuresh Siddha 	 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
8555ca3cc1SSuresh Siddha 	 * of the kernel text mapping to modify the kernel text.
8655ca3cc1SSuresh Siddha 	 *
8755ca3cc1SSuresh Siddha 	 * For 32bit kernels, these mappings are same and we can use
8855ca3cc1SSuresh Siddha 	 * kernel identity mapping to modify code.
8955ca3cc1SSuresh Siddha 	 */
9055ca3cc1SSuresh Siddha 	if (within(ip, (unsigned long)_text, (unsigned long)_etext))
91217f155eSAlexander Duyck 		ip = (unsigned long)__va(__pa_symbol(ip));
9255ca3cc1SSuresh Siddha 
9387fbb2acSSteven Rostedt (Red Hat) 	return ip;
9417666f02SSteven Rostedt }
9517666f02SSteven Rostedt 
96dc326fcaSH. Peter Anvin static const unsigned char *ftrace_nop_replace(void)
97caf4b323SFrederic Weisbecker {
98dc326fcaSH. Peter Anvin 	return ideal_nops[NOP_ATOMIC5];
99caf4b323SFrederic Weisbecker }
100caf4b323SFrederic Weisbecker 
10131e88909SSteven Rostedt static int
1028a4d0a68SSteven Rostedt ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code,
1030d098a7dSRakib Mullick 		   unsigned const char *new_code)
1043d083395SSteven Rostedt {
1056f93fc07SSteven Rostedt 	unsigned char replaced[MCOUNT_INSN_SIZE];
1063d083395SSteven Rostedt 
1073d083395SSteven Rostedt 	/*
1083d083395SSteven Rostedt 	 * Note: Due to modules and __init, code can
1093d083395SSteven Rostedt 	 *  disappear and change, we need to protect against faulting
11076aefee5SSteven Rostedt 	 *  as well as code changing. We do this by using the
111ab9a0918SSteven Rostedt 	 *  probe_kernel_* functions.
1123d083395SSteven Rostedt 	 *
1133d083395SSteven Rostedt 	 * No real locking needed, this code is run through
1146f93fc07SSteven Rostedt 	 * kstop_machine, or before SMP starts.
1153d083395SSteven Rostedt 	 */
11676aefee5SSteven Rostedt 
11776aefee5SSteven Rostedt 	/* read the text we want to modify */
118ab9a0918SSteven Rostedt 	if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
119593eb8a2SSteven Rostedt 		return -EFAULT;
1206f93fc07SSteven Rostedt 
12176aefee5SSteven Rostedt 	/* Make sure it is what we expect it to be */
1226f93fc07SSteven Rostedt 	if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
123593eb8a2SSteven Rostedt 		return -EINVAL;
1246f93fc07SSteven Rostedt 
12587fbb2acSSteven Rostedt (Red Hat) 	ip = text_ip_addr(ip);
12687fbb2acSSteven Rostedt (Red Hat) 
12776aefee5SSteven Rostedt 	/* replace the text with the new text */
12887fbb2acSSteven Rostedt (Red Hat) 	if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
129593eb8a2SSteven Rostedt 		return -EPERM;
1306f93fc07SSteven Rostedt 
1313d083395SSteven Rostedt 	sync_core();
1323d083395SSteven Rostedt 
1336f93fc07SSteven Rostedt 	return 0;
1343d083395SSteven Rostedt }
1353d083395SSteven Rostedt 
13631e88909SSteven Rostedt int ftrace_make_nop(struct module *mod,
13731e88909SSteven Rostedt 		    struct dyn_ftrace *rec, unsigned long addr)
13831e88909SSteven Rostedt {
1390d098a7dSRakib Mullick 	unsigned const char *new, *old;
14031e88909SSteven Rostedt 	unsigned long ip = rec->ip;
14131e88909SSteven Rostedt 
14231e88909SSteven Rostedt 	old = ftrace_call_replace(ip, addr);
14331e88909SSteven Rostedt 	new = ftrace_nop_replace();
14431e88909SSteven Rostedt 
1458a4d0a68SSteven Rostedt 	/*
1468a4d0a68SSteven Rostedt 	 * On boot up, and when modules are loaded, the MCOUNT_ADDR
1478a4d0a68SSteven Rostedt 	 * is converted to a nop, and will never become MCOUNT_ADDR
1488a4d0a68SSteven Rostedt 	 * again. This code is either running before SMP (on boot up)
1498a4d0a68SSteven Rostedt 	 * or before the code will ever be executed (module load).
1508a4d0a68SSteven Rostedt 	 * We do not want to use the breakpoint version in this case,
1518a4d0a68SSteven Rostedt 	 * just modify the code directly.
1528a4d0a68SSteven Rostedt 	 */
1538a4d0a68SSteven Rostedt 	if (addr == MCOUNT_ADDR)
1548a4d0a68SSteven Rostedt 		return ftrace_modify_code_direct(rec->ip, old, new);
1558a4d0a68SSteven Rostedt 
1568a4d0a68SSteven Rostedt 	/* Normal cases use add_brk_on_nop */
1578a4d0a68SSteven Rostedt 	WARN_ONCE(1, "invalid use of ftrace_make_nop");
1588a4d0a68SSteven Rostedt 	return -EINVAL;
15931e88909SSteven Rostedt }
16031e88909SSteven Rostedt 
16131e88909SSteven Rostedt int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
16231e88909SSteven Rostedt {
1630d098a7dSRakib Mullick 	unsigned const char *new, *old;
16431e88909SSteven Rostedt 	unsigned long ip = rec->ip;
16531e88909SSteven Rostedt 
16631e88909SSteven Rostedt 	old = ftrace_nop_replace();
16731e88909SSteven Rostedt 	new = ftrace_call_replace(ip, addr);
16831e88909SSteven Rostedt 
1698a4d0a68SSteven Rostedt 	/* Should only be called when module is loaded */
1708a4d0a68SSteven Rostedt 	return ftrace_modify_code_direct(rec->ip, old, new);
171d61f82d0SSteven Rostedt }
172d61f82d0SSteven Rostedt 
173a192cd04SSteven Rostedt /*
174a192cd04SSteven Rostedt  * The modifying_ftrace_code is used to tell the breakpoint
175a192cd04SSteven Rostedt  * handler to call ftrace_int3_handler(). If it fails to
176a192cd04SSteven Rostedt  * call this handler for a breakpoint added by ftrace, then
177a192cd04SSteven Rostedt  * the kernel may crash.
178a192cd04SSteven Rostedt  *
179a192cd04SSteven Rostedt  * As atomic_writes on x86 do not need a barrier, we do not
180a192cd04SSteven Rostedt  * need to add smp_mb()s for this to work. It is also considered
181a192cd04SSteven Rostedt  * that we can not read the modifying_ftrace_code before
182a192cd04SSteven Rostedt  * executing the breakpoint. That would be quite remarkable if
183a192cd04SSteven Rostedt  * it could do that. Here's the flow that is required:
184a192cd04SSteven Rostedt  *
185a192cd04SSteven Rostedt  *   CPU-0                          CPU-1
186a192cd04SSteven Rostedt  *
187a192cd04SSteven Rostedt  * atomic_inc(mfc);
188a192cd04SSteven Rostedt  * write int3s
189a192cd04SSteven Rostedt  *				<trap-int3> // implicit (r)mb
190a192cd04SSteven Rostedt  *				if (atomic_read(mfc))
191a192cd04SSteven Rostedt  *					call ftrace_int3_handler()
192a192cd04SSteven Rostedt  *
193a192cd04SSteven Rostedt  * Then when we are finished:
194a192cd04SSteven Rostedt  *
195a192cd04SSteven Rostedt  * atomic_dec(mfc);
196a192cd04SSteven Rostedt  *
197a192cd04SSteven Rostedt  * If we hit a breakpoint that was not set by ftrace, it does not
198a192cd04SSteven Rostedt  * matter if ftrace_int3_handler() is called or not. It will
199a192cd04SSteven Rostedt  * simply be ignored. But it is crucial that a ftrace nop/caller
200a192cd04SSteven Rostedt  * breakpoint is handled. No other user should ever place a
201a192cd04SSteven Rostedt  * breakpoint on an ftrace nop/caller location. It must only
202a192cd04SSteven Rostedt  * be done by this code.
203a192cd04SSteven Rostedt  */
204a192cd04SSteven Rostedt atomic_t modifying_ftrace_code __read_mostly;
20508d636b6SSteven Rostedt 
2068a4d0a68SSteven Rostedt static int
2078a4d0a68SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
2088a4d0a68SSteven Rostedt 		   unsigned const char *new_code);
2098a4d0a68SSteven Rostedt 
21008f6fba5SSteven Rostedt /*
21108f6fba5SSteven Rostedt  * Should never be called:
21208f6fba5SSteven Rostedt  *  As it is only called by __ftrace_replace_code() which is called by
21308f6fba5SSteven Rostedt  *  ftrace_replace_code() that x86 overrides, and by ftrace_update_code()
21408f6fba5SSteven Rostedt  *  which is called to turn mcount into nops or nops into function calls
21508f6fba5SSteven Rostedt  *  but not to convert a function from not using regs to one that uses
21608f6fba5SSteven Rostedt  *  regs, which ftrace_modify_call() is for.
21708f6fba5SSteven Rostedt  */
21808f6fba5SSteven Rostedt int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
21908f6fba5SSteven Rostedt 				 unsigned long addr)
22008f6fba5SSteven Rostedt {
22108f6fba5SSteven Rostedt 	WARN_ON(1);
22208f6fba5SSteven Rostedt 	return -EINVAL;
22308f6fba5SSteven Rostedt }
22408f6fba5SSteven Rostedt 
22587fbb2acSSteven Rostedt (Red Hat) static unsigned long ftrace_update_func;
22687fbb2acSSteven Rostedt (Red Hat) 
22787fbb2acSSteven Rostedt (Red Hat) static int update_ftrace_func(unsigned long ip, void *new)
2288a4d0a68SSteven Rostedt {
22987fbb2acSSteven Rostedt (Red Hat) 	unsigned char old[MCOUNT_INSN_SIZE];
2308a4d0a68SSteven Rostedt 	int ret;
2318a4d0a68SSteven Rostedt 
23287fbb2acSSteven Rostedt (Red Hat) 	memcpy(old, (void *)ip, MCOUNT_INSN_SIZE);
23387fbb2acSSteven Rostedt (Red Hat) 
23487fbb2acSSteven Rostedt (Red Hat) 	ftrace_update_func = ip;
23587fbb2acSSteven Rostedt (Red Hat) 	/* Make sure the breakpoints see the ftrace_update_func update */
23687fbb2acSSteven Rostedt (Red Hat) 	smp_wmb();
2378a4d0a68SSteven Rostedt 
2388a4d0a68SSteven Rostedt 	/* See comment above by declaration of modifying_ftrace_code */
2398a4d0a68SSteven Rostedt 	atomic_inc(&modifying_ftrace_code);
2408a4d0a68SSteven Rostedt 
2418a4d0a68SSteven Rostedt 	ret = ftrace_modify_code(ip, old, new);
2428a4d0a68SSteven Rostedt 
24387fbb2acSSteven Rostedt (Red Hat) 	atomic_dec(&modifying_ftrace_code);
24487fbb2acSSteven Rostedt (Red Hat) 
24587fbb2acSSteven Rostedt (Red Hat) 	return ret;
24687fbb2acSSteven Rostedt (Red Hat) }
24787fbb2acSSteven Rostedt (Red Hat) 
24887fbb2acSSteven Rostedt (Red Hat) int ftrace_update_ftrace_func(ftrace_func_t func)
24987fbb2acSSteven Rostedt (Red Hat) {
25087fbb2acSSteven Rostedt (Red Hat) 	unsigned long ip = (unsigned long)(&ftrace_call);
25187fbb2acSSteven Rostedt (Red Hat) 	unsigned char *new;
25287fbb2acSSteven Rostedt (Red Hat) 	int ret;
25387fbb2acSSteven Rostedt (Red Hat) 
25487fbb2acSSteven Rostedt (Red Hat) 	new = ftrace_call_replace(ip, (unsigned long)func);
25587fbb2acSSteven Rostedt (Red Hat) 	ret = update_ftrace_func(ip, new);
25687fbb2acSSteven Rostedt (Red Hat) 
25708f6fba5SSteven Rostedt 	/* Also update the regs callback function */
25808f6fba5SSteven Rostedt 	if (!ret) {
25908f6fba5SSteven Rostedt 		ip = (unsigned long)(&ftrace_regs_call);
26008f6fba5SSteven Rostedt 		new = ftrace_call_replace(ip, (unsigned long)func);
26187fbb2acSSteven Rostedt (Red Hat) 		ret = update_ftrace_func(ip, new);
26208f6fba5SSteven Rostedt 	}
26308f6fba5SSteven Rostedt 
2648a4d0a68SSteven Rostedt 	return ret;
2658a4d0a68SSteven Rostedt }
2668a4d0a68SSteven Rostedt 
267ab4ead02SKevin Hao static int is_ftrace_caller(unsigned long ip)
268ab4ead02SKevin Hao {
26987fbb2acSSteven Rostedt (Red Hat) 	if (ip == ftrace_update_func)
270ab4ead02SKevin Hao 		return 1;
271ab4ead02SKevin Hao 
272ab4ead02SKevin Hao 	return 0;
273ab4ead02SKevin Hao }
274ab4ead02SKevin Hao 
27508d636b6SSteven Rostedt /*
27608d636b6SSteven Rostedt  * A breakpoint was added to the code address we are about to
27708d636b6SSteven Rostedt  * modify, and this is the handle that will just skip over it.
27808d636b6SSteven Rostedt  * We are either changing a nop into a trace call, or a trace
27908d636b6SSteven Rostedt  * call to a nop. While the change is taking place, we treat
28008d636b6SSteven Rostedt  * it just like it was a nop.
28108d636b6SSteven Rostedt  */
28208d636b6SSteven Rostedt int ftrace_int3_handler(struct pt_regs *regs)
28308d636b6SSteven Rostedt {
284ab4ead02SKevin Hao 	unsigned long ip;
285ab4ead02SKevin Hao 
28608d636b6SSteven Rostedt 	if (WARN_ON_ONCE(!regs))
28708d636b6SSteven Rostedt 		return 0;
28808d636b6SSteven Rostedt 
289ab4ead02SKevin Hao 	ip = regs->ip - 1;
290ab4ead02SKevin Hao 	if (!ftrace_location(ip) && !is_ftrace_caller(ip))
29108d636b6SSteven Rostedt 		return 0;
29208d636b6SSteven Rostedt 
29308d636b6SSteven Rostedt 	regs->ip += MCOUNT_INSN_SIZE - 1;
29408d636b6SSteven Rostedt 
29508d636b6SSteven Rostedt 	return 1;
29608d636b6SSteven Rostedt }
29708d636b6SSteven Rostedt 
29808d636b6SSteven Rostedt static int ftrace_write(unsigned long ip, const char *val, int size)
29908d636b6SSteven Rostedt {
30008d636b6SSteven Rostedt 	/*
30108d636b6SSteven Rostedt 	 * On x86_64, kernel text mappings are mapped read-only with
30208d636b6SSteven Rostedt 	 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
30308d636b6SSteven Rostedt 	 * of the kernel text mapping to modify the kernel text.
30408d636b6SSteven Rostedt 	 *
30508d636b6SSteven Rostedt 	 * For 32bit kernels, these mappings are same and we can use
30608d636b6SSteven Rostedt 	 * kernel identity mapping to modify code.
30708d636b6SSteven Rostedt 	 */
30808d636b6SSteven Rostedt 	if (within(ip, (unsigned long)_text, (unsigned long)_etext))
309217f155eSAlexander Duyck 		ip = (unsigned long)__va(__pa_symbol(ip));
31008d636b6SSteven Rostedt 
311*92550405SSteven Rostedt (Red Hat) 	if (probe_kernel_write((void *)ip, val, size))
312*92550405SSteven Rostedt (Red Hat) 		return -EPERM;
313*92550405SSteven Rostedt (Red Hat) 
314*92550405SSteven Rostedt (Red Hat) 	return 0;
31508d636b6SSteven Rostedt }
31608d636b6SSteven Rostedt 
31708d636b6SSteven Rostedt static int add_break(unsigned long ip, const char *old)
31808d636b6SSteven Rostedt {
31908d636b6SSteven Rostedt 	unsigned char replaced[MCOUNT_INSN_SIZE];
32008d636b6SSteven Rostedt 	unsigned char brk = BREAKPOINT_INSTRUCTION;
32108d636b6SSteven Rostedt 
32208d636b6SSteven Rostedt 	if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
32308d636b6SSteven Rostedt 		return -EFAULT;
32408d636b6SSteven Rostedt 
32508d636b6SSteven Rostedt 	/* Make sure it is what we expect it to be */
32608d636b6SSteven Rostedt 	if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0)
32708d636b6SSteven Rostedt 		return -EINVAL;
32808d636b6SSteven Rostedt 
329*92550405SSteven Rostedt (Red Hat) 	return ftrace_write(ip, &brk, 1);
33008d636b6SSteven Rostedt }
33108d636b6SSteven Rostedt 
33208d636b6SSteven Rostedt static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr)
33308d636b6SSteven Rostedt {
33408d636b6SSteven Rostedt 	unsigned const char *old;
33508d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
33608d636b6SSteven Rostedt 
33708d636b6SSteven Rostedt 	old = ftrace_call_replace(ip, addr);
33808d636b6SSteven Rostedt 
33908d636b6SSteven Rostedt 	return add_break(rec->ip, old);
34008d636b6SSteven Rostedt }
34108d636b6SSteven Rostedt 
34208d636b6SSteven Rostedt 
34308d636b6SSteven Rostedt static int add_brk_on_nop(struct dyn_ftrace *rec)
34408d636b6SSteven Rostedt {
34508d636b6SSteven Rostedt 	unsigned const char *old;
34608d636b6SSteven Rostedt 
34708d636b6SSteven Rostedt 	old = ftrace_nop_replace();
34808d636b6SSteven Rostedt 
34908d636b6SSteven Rostedt 	return add_break(rec->ip, old);
35008d636b6SSteven Rostedt }
35108d636b6SSteven Rostedt 
35208f6fba5SSteven Rostedt /*
35308f6fba5SSteven Rostedt  * If the record has the FTRACE_FL_REGS set, that means that it
35408f6fba5SSteven Rostedt  * wants to convert to a callback that saves all regs. If FTRACE_FL_REGS
35508f6fba5SSteven Rostedt  * is not not set, then it wants to convert to the normal callback.
35608f6fba5SSteven Rostedt  */
35708f6fba5SSteven Rostedt static unsigned long get_ftrace_addr(struct dyn_ftrace *rec)
35808f6fba5SSteven Rostedt {
35908f6fba5SSteven Rostedt 	if (rec->flags & FTRACE_FL_REGS)
36008f6fba5SSteven Rostedt 		return (unsigned long)FTRACE_REGS_ADDR;
36108f6fba5SSteven Rostedt 	else
36208f6fba5SSteven Rostedt 		return (unsigned long)FTRACE_ADDR;
36308f6fba5SSteven Rostedt }
36408f6fba5SSteven Rostedt 
36508f6fba5SSteven Rostedt /*
36608f6fba5SSteven Rostedt  * The FTRACE_FL_REGS_EN is set when the record already points to
36708f6fba5SSteven Rostedt  * a function that saves all the regs. Basically the '_EN' version
36808f6fba5SSteven Rostedt  * represents the current state of the function.
36908f6fba5SSteven Rostedt  */
37008f6fba5SSteven Rostedt static unsigned long get_ftrace_old_addr(struct dyn_ftrace *rec)
37108f6fba5SSteven Rostedt {
37208f6fba5SSteven Rostedt 	if (rec->flags & FTRACE_FL_REGS_EN)
37308f6fba5SSteven Rostedt 		return (unsigned long)FTRACE_REGS_ADDR;
37408f6fba5SSteven Rostedt 	else
37508f6fba5SSteven Rostedt 		return (unsigned long)FTRACE_ADDR;
37608f6fba5SSteven Rostedt }
37708f6fba5SSteven Rostedt 
37808d636b6SSteven Rostedt static int add_breakpoints(struct dyn_ftrace *rec, int enable)
37908d636b6SSteven Rostedt {
38008d636b6SSteven Rostedt 	unsigned long ftrace_addr;
38108d636b6SSteven Rostedt 	int ret;
38208d636b6SSteven Rostedt 
38308d636b6SSteven Rostedt 	ret = ftrace_test_record(rec, enable);
38408d636b6SSteven Rostedt 
38508f6fba5SSteven Rostedt 	ftrace_addr = get_ftrace_addr(rec);
38608d636b6SSteven Rostedt 
38708d636b6SSteven Rostedt 	switch (ret) {
38808d636b6SSteven Rostedt 	case FTRACE_UPDATE_IGNORE:
38908d636b6SSteven Rostedt 		return 0;
39008d636b6SSteven Rostedt 
39108d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_CALL:
39208d636b6SSteven Rostedt 		/* converting nop to call */
39308d636b6SSteven Rostedt 		return add_brk_on_nop(rec);
39408d636b6SSteven Rostedt 
39508f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL_REGS:
39608f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL:
39708f6fba5SSteven Rostedt 		ftrace_addr = get_ftrace_old_addr(rec);
39808f6fba5SSteven Rostedt 		/* fall through */
39908d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_NOP:
40008d636b6SSteven Rostedt 		/* converting a call to a nop */
40108d636b6SSteven Rostedt 		return add_brk_on_call(rec, ftrace_addr);
40208d636b6SSteven Rostedt 	}
40308d636b6SSteven Rostedt 	return 0;
40408d636b6SSteven Rostedt }
40508d636b6SSteven Rostedt 
40608d636b6SSteven Rostedt /*
40708d636b6SSteven Rostedt  * On error, we need to remove breakpoints. This needs to
40808d636b6SSteven Rostedt  * be done caefully. If the address does not currently have a
40908d636b6SSteven Rostedt  * breakpoint, we know we are done. Otherwise, we look at the
41008d636b6SSteven Rostedt  * remaining 4 bytes of the instruction. If it matches a nop
41108d636b6SSteven Rostedt  * we replace the breakpoint with the nop. Otherwise we replace
41208d636b6SSteven Rostedt  * it with the call instruction.
41308d636b6SSteven Rostedt  */
41408d636b6SSteven Rostedt static int remove_breakpoint(struct dyn_ftrace *rec)
41508d636b6SSteven Rostedt {
41608d636b6SSteven Rostedt 	unsigned char ins[MCOUNT_INSN_SIZE];
41708d636b6SSteven Rostedt 	unsigned char brk = BREAKPOINT_INSTRUCTION;
41808d636b6SSteven Rostedt 	const unsigned char *nop;
41908d636b6SSteven Rostedt 	unsigned long ftrace_addr;
42008d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
42108d636b6SSteven Rostedt 
42208d636b6SSteven Rostedt 	/* If we fail the read, just give up */
42308d636b6SSteven Rostedt 	if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
42408d636b6SSteven Rostedt 		return -EFAULT;
42508d636b6SSteven Rostedt 
42608d636b6SSteven Rostedt 	/* If this does not have a breakpoint, we are done */
42708d636b6SSteven Rostedt 	if (ins[0] != brk)
42808d636b6SSteven Rostedt 		return -1;
42908d636b6SSteven Rostedt 
43008d636b6SSteven Rostedt 	nop = ftrace_nop_replace();
43108d636b6SSteven Rostedt 
43208d636b6SSteven Rostedt 	/*
43308d636b6SSteven Rostedt 	 * If the last 4 bytes of the instruction do not match
43408d636b6SSteven Rostedt 	 * a nop, then we assume that this is a call to ftrace_addr.
43508d636b6SSteven Rostedt 	 */
43608d636b6SSteven Rostedt 	if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
43708d636b6SSteven Rostedt 		/*
43808d636b6SSteven Rostedt 		 * For extra paranoidism, we check if the breakpoint is on
43908d636b6SSteven Rostedt 		 * a call that would actually jump to the ftrace_addr.
44008d636b6SSteven Rostedt 		 * If not, don't touch the breakpoint, we make just create
44108d636b6SSteven Rostedt 		 * a disaster.
44208d636b6SSteven Rostedt 		 */
44308f6fba5SSteven Rostedt 		ftrace_addr = get_ftrace_addr(rec);
44408f6fba5SSteven Rostedt 		nop = ftrace_call_replace(ip, ftrace_addr);
44508f6fba5SSteven Rostedt 
44608f6fba5SSteven Rostedt 		if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0)
44708f6fba5SSteven Rostedt 			goto update;
44808f6fba5SSteven Rostedt 
44908f6fba5SSteven Rostedt 		/* Check both ftrace_addr and ftrace_old_addr */
45008f6fba5SSteven Rostedt 		ftrace_addr = get_ftrace_old_addr(rec);
45108d636b6SSteven Rostedt 		nop = ftrace_call_replace(ip, ftrace_addr);
45208d636b6SSteven Rostedt 
45308d636b6SSteven Rostedt 		if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
45408d636b6SSteven Rostedt 			return -EINVAL;
45508d636b6SSteven Rostedt 	}
45608d636b6SSteven Rostedt 
45708f6fba5SSteven Rostedt  update:
458c932c6b7SSteven Rostedt (Red Hat) 	return ftrace_write(ip, nop, 1);
45908d636b6SSteven Rostedt }
46008d636b6SSteven Rostedt 
46108d636b6SSteven Rostedt static int add_update_code(unsigned long ip, unsigned const char *new)
46208d636b6SSteven Rostedt {
46308d636b6SSteven Rostedt 	/* skip breakpoint */
46408d636b6SSteven Rostedt 	ip++;
46508d636b6SSteven Rostedt 	new++;
466*92550405SSteven Rostedt (Red Hat) 	return ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1);
46708d636b6SSteven Rostedt }
46808d636b6SSteven Rostedt 
46908d636b6SSteven Rostedt static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
47008d636b6SSteven Rostedt {
47108d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
47208d636b6SSteven Rostedt 	unsigned const char *new;
47308d636b6SSteven Rostedt 
47408d636b6SSteven Rostedt 	new = ftrace_call_replace(ip, addr);
47508d636b6SSteven Rostedt 	return add_update_code(ip, new);
47608d636b6SSteven Rostedt }
47708d636b6SSteven Rostedt 
47808d636b6SSteven Rostedt static int add_update_nop(struct dyn_ftrace *rec)
47908d636b6SSteven Rostedt {
48008d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
48108d636b6SSteven Rostedt 	unsigned const char *new;
48208d636b6SSteven Rostedt 
48308d636b6SSteven Rostedt 	new = ftrace_nop_replace();
48408d636b6SSteven Rostedt 	return add_update_code(ip, new);
48508d636b6SSteven Rostedt }
48608d636b6SSteven Rostedt 
48708d636b6SSteven Rostedt static int add_update(struct dyn_ftrace *rec, int enable)
48808d636b6SSteven Rostedt {
48908d636b6SSteven Rostedt 	unsigned long ftrace_addr;
49008d636b6SSteven Rostedt 	int ret;
49108d636b6SSteven Rostedt 
49208d636b6SSteven Rostedt 	ret = ftrace_test_record(rec, enable);
49308d636b6SSteven Rostedt 
49408f6fba5SSteven Rostedt 	ftrace_addr  = get_ftrace_addr(rec);
49508d636b6SSteven Rostedt 
49608d636b6SSteven Rostedt 	switch (ret) {
49708d636b6SSteven Rostedt 	case FTRACE_UPDATE_IGNORE:
49808d636b6SSteven Rostedt 		return 0;
49908d636b6SSteven Rostedt 
50008f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL_REGS:
50108f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL:
50208d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_CALL:
50308d636b6SSteven Rostedt 		/* converting nop to call */
50408d636b6SSteven Rostedt 		return add_update_call(rec, ftrace_addr);
50508d636b6SSteven Rostedt 
50608d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_NOP:
50708d636b6SSteven Rostedt 		/* converting a call to a nop */
50808d636b6SSteven Rostedt 		return add_update_nop(rec);
50908d636b6SSteven Rostedt 	}
51008d636b6SSteven Rostedt 
51108d636b6SSteven Rostedt 	return 0;
51208d636b6SSteven Rostedt }
51308d636b6SSteven Rostedt 
51408d636b6SSteven Rostedt static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
51508d636b6SSteven Rostedt {
51608d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
51708d636b6SSteven Rostedt 	unsigned const char *new;
51808d636b6SSteven Rostedt 
51908d636b6SSteven Rostedt 	new = ftrace_call_replace(ip, addr);
52008d636b6SSteven Rostedt 
521*92550405SSteven Rostedt (Red Hat) 	return ftrace_write(ip, new, 1);
52208d636b6SSteven Rostedt }
52308d636b6SSteven Rostedt 
52408d636b6SSteven Rostedt static int finish_update_nop(struct dyn_ftrace *rec)
52508d636b6SSteven Rostedt {
52608d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
52708d636b6SSteven Rostedt 	unsigned const char *new;
52808d636b6SSteven Rostedt 
52908d636b6SSteven Rostedt 	new = ftrace_nop_replace();
53008d636b6SSteven Rostedt 
531*92550405SSteven Rostedt (Red Hat) 	return ftrace_write(ip, new, 1);
53208d636b6SSteven Rostedt }
53308d636b6SSteven Rostedt 
53408d636b6SSteven Rostedt static int finish_update(struct dyn_ftrace *rec, int enable)
53508d636b6SSteven Rostedt {
53608d636b6SSteven Rostedt 	unsigned long ftrace_addr;
53708d636b6SSteven Rostedt 	int ret;
53808d636b6SSteven Rostedt 
53908d636b6SSteven Rostedt 	ret = ftrace_update_record(rec, enable);
54008d636b6SSteven Rostedt 
54108f6fba5SSteven Rostedt 	ftrace_addr = get_ftrace_addr(rec);
54208d636b6SSteven Rostedt 
54308d636b6SSteven Rostedt 	switch (ret) {
54408d636b6SSteven Rostedt 	case FTRACE_UPDATE_IGNORE:
54508d636b6SSteven Rostedt 		return 0;
54608d636b6SSteven Rostedt 
54708f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL_REGS:
54808f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL:
54908d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_CALL:
55008d636b6SSteven Rostedt 		/* converting nop to call */
55108d636b6SSteven Rostedt 		return finish_update_call(rec, ftrace_addr);
55208d636b6SSteven Rostedt 
55308d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_NOP:
55408d636b6SSteven Rostedt 		/* converting a call to a nop */
55508d636b6SSteven Rostedt 		return finish_update_nop(rec);
55608d636b6SSteven Rostedt 	}
55708d636b6SSteven Rostedt 
55808d636b6SSteven Rostedt 	return 0;
55908d636b6SSteven Rostedt }
56008d636b6SSteven Rostedt 
56108d636b6SSteven Rostedt static void do_sync_core(void *data)
56208d636b6SSteven Rostedt {
56308d636b6SSteven Rostedt 	sync_core();
56408d636b6SSteven Rostedt }
56508d636b6SSteven Rostedt 
56608d636b6SSteven Rostedt static void run_sync(void)
56708d636b6SSteven Rostedt {
56808d636b6SSteven Rostedt 	int enable_irqs = irqs_disabled();
56908d636b6SSteven Rostedt 
57008d636b6SSteven Rostedt 	/* We may be called with interrupts disbled (on bootup). */
57108d636b6SSteven Rostedt 	if (enable_irqs)
57208d636b6SSteven Rostedt 		local_irq_enable();
57308d636b6SSteven Rostedt 	on_each_cpu(do_sync_core, NULL, 1);
57408d636b6SSteven Rostedt 	if (enable_irqs)
57508d636b6SSteven Rostedt 		local_irq_disable();
57608d636b6SSteven Rostedt }
57708d636b6SSteven Rostedt 
578e4f5d544SSteven Rostedt void ftrace_replace_code(int enable)
57908d636b6SSteven Rostedt {
58008d636b6SSteven Rostedt 	struct ftrace_rec_iter *iter;
58108d636b6SSteven Rostedt 	struct dyn_ftrace *rec;
58208d636b6SSteven Rostedt 	const char *report = "adding breakpoints";
58308d636b6SSteven Rostedt 	int count = 0;
58408d636b6SSteven Rostedt 	int ret;
58508d636b6SSteven Rostedt 
58608d636b6SSteven Rostedt 	for_ftrace_rec_iter(iter) {
58708d636b6SSteven Rostedt 		rec = ftrace_rec_iter_record(iter);
58808d636b6SSteven Rostedt 
58908d636b6SSteven Rostedt 		ret = add_breakpoints(rec, enable);
59008d636b6SSteven Rostedt 		if (ret)
59108d636b6SSteven Rostedt 			goto remove_breakpoints;
59208d636b6SSteven Rostedt 		count++;
59308d636b6SSteven Rostedt 	}
59408d636b6SSteven Rostedt 
59508d636b6SSteven Rostedt 	run_sync();
59608d636b6SSteven Rostedt 
59708d636b6SSteven Rostedt 	report = "updating code";
59808d636b6SSteven Rostedt 
59908d636b6SSteven Rostedt 	for_ftrace_rec_iter(iter) {
60008d636b6SSteven Rostedt 		rec = ftrace_rec_iter_record(iter);
60108d636b6SSteven Rostedt 
60208d636b6SSteven Rostedt 		ret = add_update(rec, enable);
60308d636b6SSteven Rostedt 		if (ret)
60408d636b6SSteven Rostedt 			goto remove_breakpoints;
60508d636b6SSteven Rostedt 	}
60608d636b6SSteven Rostedt 
60708d636b6SSteven Rostedt 	run_sync();
60808d636b6SSteven Rostedt 
60908d636b6SSteven Rostedt 	report = "removing breakpoints";
61008d636b6SSteven Rostedt 
61108d636b6SSteven Rostedt 	for_ftrace_rec_iter(iter) {
61208d636b6SSteven Rostedt 		rec = ftrace_rec_iter_record(iter);
61308d636b6SSteven Rostedt 
61408d636b6SSteven Rostedt 		ret = finish_update(rec, enable);
61508d636b6SSteven Rostedt 		if (ret)
61608d636b6SSteven Rostedt 			goto remove_breakpoints;
61708d636b6SSteven Rostedt 	}
61808d636b6SSteven Rostedt 
61908d636b6SSteven Rostedt 	run_sync();
62008d636b6SSteven Rostedt 
62108d636b6SSteven Rostedt 	return;
62208d636b6SSteven Rostedt 
62308d636b6SSteven Rostedt  remove_breakpoints:
62408d636b6SSteven Rostedt 	ftrace_bug(ret, rec ? rec->ip : 0);
62508d636b6SSteven Rostedt 	printk(KERN_WARNING "Failed on %s (%d):\n", report, count);
62608d636b6SSteven Rostedt 	for_ftrace_rec_iter(iter) {
62708d636b6SSteven Rostedt 		rec = ftrace_rec_iter_record(iter);
62808d636b6SSteven Rostedt 		remove_breakpoint(rec);
62908d636b6SSteven Rostedt 	}
630c932c6b7SSteven Rostedt (Red Hat) 	run_sync();
63108d636b6SSteven Rostedt }
63208d636b6SSteven Rostedt 
6338a4d0a68SSteven Rostedt static int
6348a4d0a68SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
6358a4d0a68SSteven Rostedt 		   unsigned const char *new_code)
6368a4d0a68SSteven Rostedt {
6378a4d0a68SSteven Rostedt 	int ret;
6388a4d0a68SSteven Rostedt 
6398a4d0a68SSteven Rostedt 	ret = add_break(ip, old_code);
6408a4d0a68SSteven Rostedt 	if (ret)
6418a4d0a68SSteven Rostedt 		goto out;
6428a4d0a68SSteven Rostedt 
6438a4d0a68SSteven Rostedt 	run_sync();
6448a4d0a68SSteven Rostedt 
6458a4d0a68SSteven Rostedt 	ret = add_update_code(ip, new_code);
6468a4d0a68SSteven Rostedt 	if (ret)
6478a4d0a68SSteven Rostedt 		goto fail_update;
6488a4d0a68SSteven Rostedt 
6498a4d0a68SSteven Rostedt 	run_sync();
6508a4d0a68SSteven Rostedt 
6518a4d0a68SSteven Rostedt 	ret = ftrace_write(ip, new_code, 1);
6528a4d0a68SSteven Rostedt  out:
65312729f14SPetr Mladek 	run_sync();
6548a4d0a68SSteven Rostedt 	return ret;
6558a4d0a68SSteven Rostedt 
6568a4d0a68SSteven Rostedt  fail_update:
657c932c6b7SSteven Rostedt (Red Hat) 	ftrace_write(ip, old_code, 1);
6588a4d0a68SSteven Rostedt 	goto out;
6598a4d0a68SSteven Rostedt }
6608a4d0a68SSteven Rostedt 
66108d636b6SSteven Rostedt void arch_ftrace_update_code(int command)
66208d636b6SSteven Rostedt {
663a192cd04SSteven Rostedt 	/* See comment above by declaration of modifying_ftrace_code */
664a192cd04SSteven Rostedt 	atomic_inc(&modifying_ftrace_code);
66508d636b6SSteven Rostedt 
666e4f5d544SSteven Rostedt 	ftrace_modify_all_code(command);
66708d636b6SSteven Rostedt 
668a192cd04SSteven Rostedt 	atomic_dec(&modifying_ftrace_code);
66908d636b6SSteven Rostedt }
67008d636b6SSteven Rostedt 
671d61f82d0SSteven Rostedt int __init ftrace_dyn_arch_init(void *data)
6723d083395SSteven Rostedt {
673732f3ca7SSteven Rostedt 	/* The return code is retured via data */
674732f3ca7SSteven Rostedt 	*(unsigned long *)data = 0;
675dfa60abaSSteven Rostedt 
6763d083395SSteven Rostedt 	return 0;
6773d083395SSteven Rostedt }
678caf4b323SFrederic Weisbecker #endif
679e7d3737eSFrederic Weisbecker 
680fb52607aSFrederic Weisbecker #ifdef CONFIG_FUNCTION_GRAPH_TRACER
681e7d3737eSFrederic Weisbecker 
6825a45cfe1SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE
6835a45cfe1SSteven Rostedt extern void ftrace_graph_call(void);
6845a45cfe1SSteven Rostedt 
68587fbb2acSSteven Rostedt (Red Hat) static unsigned char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
6865a45cfe1SSteven Rostedt {
68787fbb2acSSteven Rostedt (Red Hat) 	static union ftrace_code_union calc;
6885a45cfe1SSteven Rostedt 
68987fbb2acSSteven Rostedt (Red Hat) 	/* Jmp not a call (ignore the .e8) */
69087fbb2acSSteven Rostedt (Red Hat) 	calc.e8		= 0xe9;
69187fbb2acSSteven Rostedt (Red Hat) 	calc.offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
6925a45cfe1SSteven Rostedt 
69387fbb2acSSteven Rostedt (Red Hat) 	/*
69487fbb2acSSteven Rostedt (Red Hat) 	 * ftrace external locks synchronize the access to the static variable.
69587fbb2acSSteven Rostedt (Red Hat) 	 */
69687fbb2acSSteven Rostedt (Red Hat) 	return calc.code;
69787fbb2acSSteven Rostedt (Red Hat) }
6985a45cfe1SSteven Rostedt 
69987fbb2acSSteven Rostedt (Red Hat) static int ftrace_mod_jmp(unsigned long ip, void *func)
70087fbb2acSSteven Rostedt (Red Hat) {
70187fbb2acSSteven Rostedt (Red Hat) 	unsigned char *new;
7025a45cfe1SSteven Rostedt 
70387fbb2acSSteven Rostedt (Red Hat) 	new = ftrace_jmp_replace(ip, (unsigned long)func);
7045a45cfe1SSteven Rostedt 
70587fbb2acSSteven Rostedt (Red Hat) 	return update_ftrace_func(ip, new);
7065a45cfe1SSteven Rostedt }
7075a45cfe1SSteven Rostedt 
7085a45cfe1SSteven Rostedt int ftrace_enable_ftrace_graph_caller(void)
7095a45cfe1SSteven Rostedt {
7105a45cfe1SSteven Rostedt 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
7115a45cfe1SSteven Rostedt 
71287fbb2acSSteven Rostedt (Red Hat) 	return ftrace_mod_jmp(ip, &ftrace_graph_caller);
7135a45cfe1SSteven Rostedt }
7145a45cfe1SSteven Rostedt 
7155a45cfe1SSteven Rostedt int ftrace_disable_ftrace_graph_caller(void)
7165a45cfe1SSteven Rostedt {
7175a45cfe1SSteven Rostedt 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
7185a45cfe1SSteven Rostedt 
71987fbb2acSSteven Rostedt (Red Hat) 	return ftrace_mod_jmp(ip, &ftrace_stub);
7205a45cfe1SSteven Rostedt }
7215a45cfe1SSteven Rostedt 
722e7d3737eSFrederic Weisbecker #endif /* !CONFIG_DYNAMIC_FTRACE */
723e7d3737eSFrederic Weisbecker 
724e7d3737eSFrederic Weisbecker /*
725e7d3737eSFrederic Weisbecker  * Hook the return address and push it in the stack of return addrs
726e7d3737eSFrederic Weisbecker  * in current thread info.
727e7d3737eSFrederic Weisbecker  */
72871e308a2SSteven Rostedt void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
72971e308a2SSteven Rostedt 			   unsigned long frame_pointer)
730e7d3737eSFrederic Weisbecker {
731e7d3737eSFrederic Weisbecker 	unsigned long old;
732e7d3737eSFrederic Weisbecker 	int faulted;
733287b6e68SFrederic Weisbecker 	struct ftrace_graph_ent trace;
734e7d3737eSFrederic Weisbecker 	unsigned long return_hooker = (unsigned long)
735e7d3737eSFrederic Weisbecker 				&return_to_handler;
736e7d3737eSFrederic Weisbecker 
737380c4b14SFrederic Weisbecker 	if (unlikely(atomic_read(&current->tracing_graph_pause)))
738e7d3737eSFrederic Weisbecker 		return;
739e7d3737eSFrederic Weisbecker 
740e7d3737eSFrederic Weisbecker 	/*
741e7d3737eSFrederic Weisbecker 	 * Protect against fault, even if it shouldn't
742e7d3737eSFrederic Weisbecker 	 * happen. This tool is too much intrusive to
743e7d3737eSFrederic Weisbecker 	 * ignore such a protection.
744e7d3737eSFrederic Weisbecker 	 */
745e7d3737eSFrederic Weisbecker 	asm volatile(
74696665788SSteven Rostedt 		"1: " _ASM_MOV " (%[parent]), %[old]\n"
74796665788SSteven Rostedt 		"2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
748e7d3737eSFrederic Weisbecker 		"   movl $0, %[faulted]\n"
749e3944bfaSSteven Rostedt 		"3:\n"
750e7d3737eSFrederic Weisbecker 
751e7d3737eSFrederic Weisbecker 		".section .fixup, \"ax\"\n"
752e3944bfaSSteven Rostedt 		"4: movl $1, %[faulted]\n"
753e3944bfaSSteven Rostedt 		"   jmp 3b\n"
754e7d3737eSFrederic Weisbecker 		".previous\n"
755e7d3737eSFrederic Weisbecker 
756e3944bfaSSteven Rostedt 		_ASM_EXTABLE(1b, 4b)
757e3944bfaSSteven Rostedt 		_ASM_EXTABLE(2b, 4b)
758e7d3737eSFrederic Weisbecker 
759aa512a27SSteven Rostedt 		: [old] "=&r" (old), [faulted] "=r" (faulted)
76096665788SSteven Rostedt 		: [parent] "r" (parent), [return_hooker] "r" (return_hooker)
761e7d3737eSFrederic Weisbecker 		: "memory"
762e7d3737eSFrederic Weisbecker 	);
763e7d3737eSFrederic Weisbecker 
76414a866c5SSteven Rostedt 	if (unlikely(faulted)) {
76514a866c5SSteven Rostedt 		ftrace_graph_stop();
76614a866c5SSteven Rostedt 		WARN_ON(1);
767e7d3737eSFrederic Weisbecker 		return;
768e7d3737eSFrederic Weisbecker 	}
769e7d3737eSFrederic Weisbecker 
770722b3c74SSteven Rostedt 	trace.func = self_addr;
771722b3c74SSteven Rostedt 	trace.depth = current->curr_ret_stack + 1;
772722b3c74SSteven Rostedt 
773722b3c74SSteven Rostedt 	/* Only trace if the calling function expects to */
774722b3c74SSteven Rostedt 	if (!ftrace_graph_entry(&trace)) {
775e7d3737eSFrederic Weisbecker 		*parent = old;
776287b6e68SFrederic Weisbecker 		return;
777287b6e68SFrederic Weisbecker 	}
778287b6e68SFrederic Weisbecker 
779722b3c74SSteven Rostedt 	if (ftrace_push_return_trace(old, self_addr, &trace.depth,
780722b3c74SSteven Rostedt 		    frame_pointer) == -EBUSY) {
781e49dc19cSSteven Rostedt 		*parent = old;
782722b3c74SSteven Rostedt 		return;
783e49dc19cSSteven Rostedt 	}
784e7d3737eSFrederic Weisbecker }
785fb52607aSFrederic Weisbecker #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
786