xref: /openbmc/linux/arch/x86/kernel/ftrace.c (revision 94792ea07ce2cceef48803c4df3cb5efacb21c9a)
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 
31192550405SSteven Rostedt (Red Hat) 	if (probe_kernel_write((void *)ip, val, size))
31292550405SSteven Rostedt (Red Hat) 		return -EPERM;
31392550405SSteven Rostedt (Red Hat) 
31492550405SSteven 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 
32992550405SSteven 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 
383*94792ea0SSteven Rostedt (Red Hat) 	ftrace_addr = get_ftrace_old_addr(rec);
38408d636b6SSteven Rostedt 
385*94792ea0SSteven Rostedt (Red Hat) 	ret = ftrace_test_record(rec, enable);
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:
39708d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_NOP:
39808d636b6SSteven Rostedt 		/* converting a call to a nop */
39908d636b6SSteven Rostedt 		return add_brk_on_call(rec, ftrace_addr);
40008d636b6SSteven Rostedt 	}
40108d636b6SSteven Rostedt 	return 0;
40208d636b6SSteven Rostedt }
40308d636b6SSteven Rostedt 
40408d636b6SSteven Rostedt /*
40508d636b6SSteven Rostedt  * On error, we need to remove breakpoints. This needs to
40608d636b6SSteven Rostedt  * be done caefully. If the address does not currently have a
40708d636b6SSteven Rostedt  * breakpoint, we know we are done. Otherwise, we look at the
40808d636b6SSteven Rostedt  * remaining 4 bytes of the instruction. If it matches a nop
40908d636b6SSteven Rostedt  * we replace the breakpoint with the nop. Otherwise we replace
41008d636b6SSteven Rostedt  * it with the call instruction.
41108d636b6SSteven Rostedt  */
41208d636b6SSteven Rostedt static int remove_breakpoint(struct dyn_ftrace *rec)
41308d636b6SSteven Rostedt {
41408d636b6SSteven Rostedt 	unsigned char ins[MCOUNT_INSN_SIZE];
41508d636b6SSteven Rostedt 	unsigned char brk = BREAKPOINT_INSTRUCTION;
41608d636b6SSteven Rostedt 	const unsigned char *nop;
41708d636b6SSteven Rostedt 	unsigned long ftrace_addr;
41808d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
41908d636b6SSteven Rostedt 
42008d636b6SSteven Rostedt 	/* If we fail the read, just give up */
42108d636b6SSteven Rostedt 	if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
42208d636b6SSteven Rostedt 		return -EFAULT;
42308d636b6SSteven Rostedt 
42408d636b6SSteven Rostedt 	/* If this does not have a breakpoint, we are done */
42508d636b6SSteven Rostedt 	if (ins[0] != brk)
4267f11f5ecSPetr Mladek 		return 0;
42708d636b6SSteven Rostedt 
42808d636b6SSteven Rostedt 	nop = ftrace_nop_replace();
42908d636b6SSteven Rostedt 
43008d636b6SSteven Rostedt 	/*
43108d636b6SSteven Rostedt 	 * If the last 4 bytes of the instruction do not match
43208d636b6SSteven Rostedt 	 * a nop, then we assume that this is a call to ftrace_addr.
43308d636b6SSteven Rostedt 	 */
43408d636b6SSteven Rostedt 	if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
43508d636b6SSteven Rostedt 		/*
43608d636b6SSteven Rostedt 		 * For extra paranoidism, we check if the breakpoint is on
43708d636b6SSteven Rostedt 		 * a call that would actually jump to the ftrace_addr.
43808d636b6SSteven Rostedt 		 * If not, don't touch the breakpoint, we make just create
43908d636b6SSteven Rostedt 		 * a disaster.
44008d636b6SSteven Rostedt 		 */
44108f6fba5SSteven Rostedt 		ftrace_addr = get_ftrace_addr(rec);
44208f6fba5SSteven Rostedt 		nop = ftrace_call_replace(ip, ftrace_addr);
44308f6fba5SSteven Rostedt 
44408f6fba5SSteven Rostedt 		if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0)
44508f6fba5SSteven Rostedt 			goto update;
44608f6fba5SSteven Rostedt 
44708f6fba5SSteven Rostedt 		/* Check both ftrace_addr and ftrace_old_addr */
44808f6fba5SSteven Rostedt 		ftrace_addr = get_ftrace_old_addr(rec);
44908d636b6SSteven Rostedt 		nop = ftrace_call_replace(ip, ftrace_addr);
45008d636b6SSteven Rostedt 
45108d636b6SSteven Rostedt 		if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
45208d636b6SSteven Rostedt 			return -EINVAL;
45308d636b6SSteven Rostedt 	}
45408d636b6SSteven Rostedt 
45508f6fba5SSteven Rostedt  update:
456c932c6b7SSteven Rostedt (Red Hat) 	return ftrace_write(ip, nop, 1);
45708d636b6SSteven Rostedt }
45808d636b6SSteven Rostedt 
45908d636b6SSteven Rostedt static int add_update_code(unsigned long ip, unsigned const char *new)
46008d636b6SSteven Rostedt {
46108d636b6SSteven Rostedt 	/* skip breakpoint */
46208d636b6SSteven Rostedt 	ip++;
46308d636b6SSteven Rostedt 	new++;
46492550405SSteven Rostedt (Red Hat) 	return ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1);
46508d636b6SSteven Rostedt }
46608d636b6SSteven Rostedt 
46708d636b6SSteven Rostedt static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
46808d636b6SSteven Rostedt {
46908d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
47008d636b6SSteven Rostedt 	unsigned const char *new;
47108d636b6SSteven Rostedt 
47208d636b6SSteven Rostedt 	new = ftrace_call_replace(ip, addr);
47308d636b6SSteven Rostedt 	return add_update_code(ip, new);
47408d636b6SSteven Rostedt }
47508d636b6SSteven Rostedt 
47608d636b6SSteven Rostedt static int add_update_nop(struct dyn_ftrace *rec)
47708d636b6SSteven Rostedt {
47808d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
47908d636b6SSteven Rostedt 	unsigned const char *new;
48008d636b6SSteven Rostedt 
48108d636b6SSteven Rostedt 	new = ftrace_nop_replace();
48208d636b6SSteven Rostedt 	return add_update_code(ip, new);
48308d636b6SSteven Rostedt }
48408d636b6SSteven Rostedt 
48508d636b6SSteven Rostedt static int add_update(struct dyn_ftrace *rec, int enable)
48608d636b6SSteven Rostedt {
48708d636b6SSteven Rostedt 	unsigned long ftrace_addr;
48808d636b6SSteven Rostedt 	int ret;
48908d636b6SSteven Rostedt 
49008d636b6SSteven Rostedt 	ret = ftrace_test_record(rec, enable);
49108d636b6SSteven Rostedt 
49208f6fba5SSteven Rostedt 	ftrace_addr  = get_ftrace_addr(rec);
49308d636b6SSteven Rostedt 
49408d636b6SSteven Rostedt 	switch (ret) {
49508d636b6SSteven Rostedt 	case FTRACE_UPDATE_IGNORE:
49608d636b6SSteven Rostedt 		return 0;
49708d636b6SSteven Rostedt 
49808f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL_REGS:
49908f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL:
50008d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_CALL:
50108d636b6SSteven Rostedt 		/* converting nop to call */
50208d636b6SSteven Rostedt 		return add_update_call(rec, ftrace_addr);
50308d636b6SSteven Rostedt 
50408d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_NOP:
50508d636b6SSteven Rostedt 		/* converting a call to a nop */
50608d636b6SSteven Rostedt 		return add_update_nop(rec);
50708d636b6SSteven Rostedt 	}
50808d636b6SSteven Rostedt 
50908d636b6SSteven Rostedt 	return 0;
51008d636b6SSteven Rostedt }
51108d636b6SSteven Rostedt 
51208d636b6SSteven Rostedt static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
51308d636b6SSteven Rostedt {
51408d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
51508d636b6SSteven Rostedt 	unsigned const char *new;
51608d636b6SSteven Rostedt 
51708d636b6SSteven Rostedt 	new = ftrace_call_replace(ip, addr);
51808d636b6SSteven Rostedt 
51992550405SSteven Rostedt (Red Hat) 	return ftrace_write(ip, new, 1);
52008d636b6SSteven Rostedt }
52108d636b6SSteven Rostedt 
52208d636b6SSteven Rostedt static int finish_update_nop(struct dyn_ftrace *rec)
52308d636b6SSteven Rostedt {
52408d636b6SSteven Rostedt 	unsigned long ip = rec->ip;
52508d636b6SSteven Rostedt 	unsigned const char *new;
52608d636b6SSteven Rostedt 
52708d636b6SSteven Rostedt 	new = ftrace_nop_replace();
52808d636b6SSteven Rostedt 
52992550405SSteven Rostedt (Red Hat) 	return ftrace_write(ip, new, 1);
53008d636b6SSteven Rostedt }
53108d636b6SSteven Rostedt 
53208d636b6SSteven Rostedt static int finish_update(struct dyn_ftrace *rec, int enable)
53308d636b6SSteven Rostedt {
53408d636b6SSteven Rostedt 	unsigned long ftrace_addr;
53508d636b6SSteven Rostedt 	int ret;
53608d636b6SSteven Rostedt 
53708d636b6SSteven Rostedt 	ret = ftrace_update_record(rec, enable);
53808d636b6SSteven Rostedt 
53908f6fba5SSteven Rostedt 	ftrace_addr = get_ftrace_addr(rec);
54008d636b6SSteven Rostedt 
54108d636b6SSteven Rostedt 	switch (ret) {
54208d636b6SSteven Rostedt 	case FTRACE_UPDATE_IGNORE:
54308d636b6SSteven Rostedt 		return 0;
54408d636b6SSteven Rostedt 
54508f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL_REGS:
54608f6fba5SSteven Rostedt 	case FTRACE_UPDATE_MODIFY_CALL:
54708d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_CALL:
54808d636b6SSteven Rostedt 		/* converting nop to call */
54908d636b6SSteven Rostedt 		return finish_update_call(rec, ftrace_addr);
55008d636b6SSteven Rostedt 
55108d636b6SSteven Rostedt 	case FTRACE_UPDATE_MAKE_NOP:
55208d636b6SSteven Rostedt 		/* converting a call to a nop */
55308d636b6SSteven Rostedt 		return finish_update_nop(rec);
55408d636b6SSteven Rostedt 	}
55508d636b6SSteven Rostedt 
55608d636b6SSteven Rostedt 	return 0;
55708d636b6SSteven Rostedt }
55808d636b6SSteven Rostedt 
55908d636b6SSteven Rostedt static void do_sync_core(void *data)
56008d636b6SSteven Rostedt {
56108d636b6SSteven Rostedt 	sync_core();
56208d636b6SSteven Rostedt }
56308d636b6SSteven Rostedt 
56408d636b6SSteven Rostedt static void run_sync(void)
56508d636b6SSteven Rostedt {
56608d636b6SSteven Rostedt 	int enable_irqs = irqs_disabled();
56708d636b6SSteven Rostedt 
56808d636b6SSteven Rostedt 	/* We may be called with interrupts disbled (on bootup). */
56908d636b6SSteven Rostedt 	if (enable_irqs)
57008d636b6SSteven Rostedt 		local_irq_enable();
57108d636b6SSteven Rostedt 	on_each_cpu(do_sync_core, NULL, 1);
57208d636b6SSteven Rostedt 	if (enable_irqs)
57308d636b6SSteven Rostedt 		local_irq_disable();
57408d636b6SSteven Rostedt }
57508d636b6SSteven Rostedt 
576e4f5d544SSteven Rostedt void ftrace_replace_code(int enable)
57708d636b6SSteven Rostedt {
57808d636b6SSteven Rostedt 	struct ftrace_rec_iter *iter;
57908d636b6SSteven Rostedt 	struct dyn_ftrace *rec;
58008d636b6SSteven Rostedt 	const char *report = "adding breakpoints";
58108d636b6SSteven Rostedt 	int count = 0;
58208d636b6SSteven Rostedt 	int ret;
58308d636b6SSteven Rostedt 
58408d636b6SSteven Rostedt 	for_ftrace_rec_iter(iter) {
58508d636b6SSteven Rostedt 		rec = ftrace_rec_iter_record(iter);
58608d636b6SSteven Rostedt 
58708d636b6SSteven Rostedt 		ret = add_breakpoints(rec, enable);
58808d636b6SSteven Rostedt 		if (ret)
58908d636b6SSteven Rostedt 			goto remove_breakpoints;
59008d636b6SSteven Rostedt 		count++;
59108d636b6SSteven Rostedt 	}
59208d636b6SSteven Rostedt 
59308d636b6SSteven Rostedt 	run_sync();
59408d636b6SSteven Rostedt 
59508d636b6SSteven Rostedt 	report = "updating code";
59608d636b6SSteven Rostedt 
59708d636b6SSteven Rostedt 	for_ftrace_rec_iter(iter) {
59808d636b6SSteven Rostedt 		rec = ftrace_rec_iter_record(iter);
59908d636b6SSteven Rostedt 
60008d636b6SSteven Rostedt 		ret = add_update(rec, enable);
60108d636b6SSteven Rostedt 		if (ret)
60208d636b6SSteven Rostedt 			goto remove_breakpoints;
60308d636b6SSteven Rostedt 	}
60408d636b6SSteven Rostedt 
60508d636b6SSteven Rostedt 	run_sync();
60608d636b6SSteven Rostedt 
60708d636b6SSteven Rostedt 	report = "removing breakpoints";
60808d636b6SSteven Rostedt 
60908d636b6SSteven Rostedt 	for_ftrace_rec_iter(iter) {
61008d636b6SSteven Rostedt 		rec = ftrace_rec_iter_record(iter);
61108d636b6SSteven Rostedt 
61208d636b6SSteven Rostedt 		ret = finish_update(rec, enable);
61308d636b6SSteven Rostedt 		if (ret)
61408d636b6SSteven Rostedt 			goto remove_breakpoints;
61508d636b6SSteven Rostedt 	}
61608d636b6SSteven Rostedt 
61708d636b6SSteven Rostedt 	run_sync();
61808d636b6SSteven Rostedt 
61908d636b6SSteven Rostedt 	return;
62008d636b6SSteven Rostedt 
62108d636b6SSteven Rostedt  remove_breakpoints:
62274bb8c45SPetr Mladek 	pr_warn("Failed on %s (%d):\n", report, count);
62308d636b6SSteven Rostedt 	ftrace_bug(ret, rec ? rec->ip : 0);
62408d636b6SSteven Rostedt 	for_ftrace_rec_iter(iter) {
62508d636b6SSteven Rostedt 		rec = ftrace_rec_iter_record(iter);
6267f11f5ecSPetr Mladek 		/*
6277f11f5ecSPetr Mladek 		 * Breakpoints are handled only when this function is in
6287f11f5ecSPetr Mladek 		 * progress. The system could not work with them.
6297f11f5ecSPetr Mladek 		 */
6307f11f5ecSPetr Mladek 		if (remove_breakpoint(rec))
6317f11f5ecSPetr Mladek 			BUG();
63208d636b6SSteven Rostedt 	}
633c932c6b7SSteven Rostedt (Red Hat) 	run_sync();
63408d636b6SSteven Rostedt }
63508d636b6SSteven Rostedt 
6368a4d0a68SSteven Rostedt static int
6378a4d0a68SSteven Rostedt ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
6388a4d0a68SSteven Rostedt 		   unsigned const char *new_code)
6398a4d0a68SSteven Rostedt {
6408a4d0a68SSteven Rostedt 	int ret;
6418a4d0a68SSteven Rostedt 
6428a4d0a68SSteven Rostedt 	ret = add_break(ip, old_code);
6438a4d0a68SSteven Rostedt 	if (ret)
6448a4d0a68SSteven Rostedt 		goto out;
6458a4d0a68SSteven Rostedt 
6468a4d0a68SSteven Rostedt 	run_sync();
6478a4d0a68SSteven Rostedt 
6488a4d0a68SSteven Rostedt 	ret = add_update_code(ip, new_code);
6498a4d0a68SSteven Rostedt 	if (ret)
6508a4d0a68SSteven Rostedt 		goto fail_update;
6518a4d0a68SSteven Rostedt 
6528a4d0a68SSteven Rostedt 	run_sync();
6538a4d0a68SSteven Rostedt 
6548a4d0a68SSteven Rostedt 	ret = ftrace_write(ip, new_code, 1);
6557f11f5ecSPetr Mladek 	/*
6567f11f5ecSPetr Mladek 	 * The breakpoint is handled only when this function is in progress.
6577f11f5ecSPetr Mladek 	 * The system could not work if we could not remove it.
6587f11f5ecSPetr Mladek 	 */
6597f11f5ecSPetr Mladek 	BUG_ON(ret);
6608a4d0a68SSteven Rostedt  out:
66112729f14SPetr Mladek 	run_sync();
6628a4d0a68SSteven Rostedt 	return ret;
6638a4d0a68SSteven Rostedt 
6648a4d0a68SSteven Rostedt  fail_update:
6657f11f5ecSPetr Mladek 	/* Also here the system could not work with the breakpoint */
6667f11f5ecSPetr Mladek 	if (ftrace_write(ip, old_code, 1))
6677f11f5ecSPetr Mladek 		BUG();
6688a4d0a68SSteven Rostedt 	goto out;
6698a4d0a68SSteven Rostedt }
6708a4d0a68SSteven Rostedt 
67108d636b6SSteven Rostedt void arch_ftrace_update_code(int command)
67208d636b6SSteven Rostedt {
673a192cd04SSteven Rostedt 	/* See comment above by declaration of modifying_ftrace_code */
674a192cd04SSteven Rostedt 	atomic_inc(&modifying_ftrace_code);
67508d636b6SSteven Rostedt 
676e4f5d544SSteven Rostedt 	ftrace_modify_all_code(command);
67708d636b6SSteven Rostedt 
678a192cd04SSteven Rostedt 	atomic_dec(&modifying_ftrace_code);
67908d636b6SSteven Rostedt }
68008d636b6SSteven Rostedt 
6813a36cb11SJiri Slaby int __init ftrace_dyn_arch_init(void)
6823d083395SSteven Rostedt {
6833d083395SSteven Rostedt 	return 0;
6843d083395SSteven Rostedt }
685caf4b323SFrederic Weisbecker #endif
686e7d3737eSFrederic Weisbecker 
687fb52607aSFrederic Weisbecker #ifdef CONFIG_FUNCTION_GRAPH_TRACER
688e7d3737eSFrederic Weisbecker 
6895a45cfe1SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE
6905a45cfe1SSteven Rostedt extern void ftrace_graph_call(void);
6915a45cfe1SSteven Rostedt 
69287fbb2acSSteven Rostedt (Red Hat) static unsigned char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
6935a45cfe1SSteven Rostedt {
69487fbb2acSSteven Rostedt (Red Hat) 	static union ftrace_code_union calc;
6955a45cfe1SSteven Rostedt 
69687fbb2acSSteven Rostedt (Red Hat) 	/* Jmp not a call (ignore the .e8) */
69787fbb2acSSteven Rostedt (Red Hat) 	calc.e8		= 0xe9;
69887fbb2acSSteven Rostedt (Red Hat) 	calc.offset	= ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
6995a45cfe1SSteven Rostedt 
70087fbb2acSSteven Rostedt (Red Hat) 	/*
70187fbb2acSSteven Rostedt (Red Hat) 	 * ftrace external locks synchronize the access to the static variable.
70287fbb2acSSteven Rostedt (Red Hat) 	 */
70387fbb2acSSteven Rostedt (Red Hat) 	return calc.code;
70487fbb2acSSteven Rostedt (Red Hat) }
7055a45cfe1SSteven Rostedt 
70687fbb2acSSteven Rostedt (Red Hat) static int ftrace_mod_jmp(unsigned long ip, void *func)
70787fbb2acSSteven Rostedt (Red Hat) {
70887fbb2acSSteven Rostedt (Red Hat) 	unsigned char *new;
7095a45cfe1SSteven Rostedt 
71087fbb2acSSteven Rostedt (Red Hat) 	new = ftrace_jmp_replace(ip, (unsigned long)func);
7115a45cfe1SSteven Rostedt 
71287fbb2acSSteven Rostedt (Red Hat) 	return update_ftrace_func(ip, new);
7135a45cfe1SSteven Rostedt }
7145a45cfe1SSteven Rostedt 
7155a45cfe1SSteven Rostedt int ftrace_enable_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_graph_caller);
7205a45cfe1SSteven Rostedt }
7215a45cfe1SSteven Rostedt 
7225a45cfe1SSteven Rostedt int ftrace_disable_ftrace_graph_caller(void)
7235a45cfe1SSteven Rostedt {
7245a45cfe1SSteven Rostedt 	unsigned long ip = (unsigned long)(&ftrace_graph_call);
7255a45cfe1SSteven Rostedt 
72687fbb2acSSteven Rostedt (Red Hat) 	return ftrace_mod_jmp(ip, &ftrace_stub);
7275a45cfe1SSteven Rostedt }
7285a45cfe1SSteven Rostedt 
729e7d3737eSFrederic Weisbecker #endif /* !CONFIG_DYNAMIC_FTRACE */
730e7d3737eSFrederic Weisbecker 
731e7d3737eSFrederic Weisbecker /*
732e7d3737eSFrederic Weisbecker  * Hook the return address and push it in the stack of return addrs
733e7d3737eSFrederic Weisbecker  * in current thread info.
734e7d3737eSFrederic Weisbecker  */
73571e308a2SSteven Rostedt void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
73671e308a2SSteven Rostedt 			   unsigned long frame_pointer)
737e7d3737eSFrederic Weisbecker {
738e7d3737eSFrederic Weisbecker 	unsigned long old;
739e7d3737eSFrederic Weisbecker 	int faulted;
740287b6e68SFrederic Weisbecker 	struct ftrace_graph_ent trace;
741e7d3737eSFrederic Weisbecker 	unsigned long return_hooker = (unsigned long)
742e7d3737eSFrederic Weisbecker 				&return_to_handler;
743e7d3737eSFrederic Weisbecker 
744380c4b14SFrederic Weisbecker 	if (unlikely(atomic_read(&current->tracing_graph_pause)))
745e7d3737eSFrederic Weisbecker 		return;
746e7d3737eSFrederic Weisbecker 
747e7d3737eSFrederic Weisbecker 	/*
748e7d3737eSFrederic Weisbecker 	 * Protect against fault, even if it shouldn't
749e7d3737eSFrederic Weisbecker 	 * happen. This tool is too much intrusive to
750e7d3737eSFrederic Weisbecker 	 * ignore such a protection.
751e7d3737eSFrederic Weisbecker 	 */
752e7d3737eSFrederic Weisbecker 	asm volatile(
75396665788SSteven Rostedt 		"1: " _ASM_MOV " (%[parent]), %[old]\n"
75496665788SSteven Rostedt 		"2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
755e7d3737eSFrederic Weisbecker 		"   movl $0, %[faulted]\n"
756e3944bfaSSteven Rostedt 		"3:\n"
757e7d3737eSFrederic Weisbecker 
758e7d3737eSFrederic Weisbecker 		".section .fixup, \"ax\"\n"
759e3944bfaSSteven Rostedt 		"4: movl $1, %[faulted]\n"
760e3944bfaSSteven Rostedt 		"   jmp 3b\n"
761e7d3737eSFrederic Weisbecker 		".previous\n"
762e7d3737eSFrederic Weisbecker 
763e3944bfaSSteven Rostedt 		_ASM_EXTABLE(1b, 4b)
764e3944bfaSSteven Rostedt 		_ASM_EXTABLE(2b, 4b)
765e7d3737eSFrederic Weisbecker 
766aa512a27SSteven Rostedt 		: [old] "=&r" (old), [faulted] "=r" (faulted)
76796665788SSteven Rostedt 		: [parent] "r" (parent), [return_hooker] "r" (return_hooker)
768e7d3737eSFrederic Weisbecker 		: "memory"
769e7d3737eSFrederic Weisbecker 	);
770e7d3737eSFrederic Weisbecker 
77114a866c5SSteven Rostedt 	if (unlikely(faulted)) {
77214a866c5SSteven Rostedt 		ftrace_graph_stop();
77314a866c5SSteven Rostedt 		WARN_ON(1);
774e7d3737eSFrederic Weisbecker 		return;
775e7d3737eSFrederic Weisbecker 	}
776e7d3737eSFrederic Weisbecker 
777722b3c74SSteven Rostedt 	trace.func = self_addr;
778722b3c74SSteven Rostedt 	trace.depth = current->curr_ret_stack + 1;
779722b3c74SSteven Rostedt 
780722b3c74SSteven Rostedt 	/* Only trace if the calling function expects to */
781722b3c74SSteven Rostedt 	if (!ftrace_graph_entry(&trace)) {
782e7d3737eSFrederic Weisbecker 		*parent = old;
783287b6e68SFrederic Weisbecker 		return;
784287b6e68SFrederic Weisbecker 	}
785287b6e68SFrederic Weisbecker 
786722b3c74SSteven Rostedt 	if (ftrace_push_return_trace(old, self_addr, &trace.depth,
787722b3c74SSteven Rostedt 		    frame_pointer) == -EBUSY) {
788e49dc19cSSteven Rostedt 		*parent = old;
789722b3c74SSteven Rostedt 		return;
790e49dc19cSSteven Rostedt 	}
791e7d3737eSFrederic Weisbecker }
792fb52607aSFrederic Weisbecker #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
793