1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
23d083395SSteven Rostedt /*
39d2099abSLi Bin * Dynamic function tracing support.
43d083395SSteven Rostedt *
53d083395SSteven Rostedt * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
63d083395SSteven Rostedt *
73d083395SSteven Rostedt * Thanks goes to Ingo Molnar, for suggesting the idea.
83d083395SSteven Rostedt * Mathieu Desnoyers, for suggesting postponing the modifications.
93d083395SSteven Rostedt * Arjan van de Ven, for keeping me straight, and explaining to me
103d083395SSteven Rostedt * the dangers of modifying code on the run.
113d083395SSteven Rostedt */
123d083395SSteven Rostedt
133bb258bfSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
143bb258bfSJoe Perches
153d083395SSteven Rostedt #include <linux/spinlock.h>
163d083395SSteven Rostedt #include <linux/hardirq.h>
176f93fc07SSteven Rostedt #include <linux/uaccess.h>
183d083395SSteven Rostedt #include <linux/ftrace.h>
193d083395SSteven Rostedt #include <linux/percpu.h>
2019b3e967SIngo Molnar #include <linux/sched.h>
21f3bea491SSteven Rostedt (Red Hat) #include <linux/slab.h>
223d083395SSteven Rostedt #include <linux/init.h>
233d083395SSteven Rostedt #include <linux/list.h>
2484e1c6bbSmatthieu castet #include <linux/module.h>
25d5b844a2SPetr Mladek #include <linux/memory.h>
26ac0b14dcSSean Christopherson #include <linux/vmalloc.h>
27*d48567c9SPeter Zijlstra #include <linux/set_memory.h>
283d083395SSteven Rostedt
2947788c58SFrederic Weisbecker #include <trace/syscall.h>
3047788c58SFrederic Weisbecker
3159a094c9SSteven Rostedt #include <asm/kprobes.h>
32395a59d0SAbhishek Sagar #include <asm/ftrace.h>
33732f3ca7SSteven Rostedt #include <asm/nops.h>
349e298e86SPeter Zijlstra #include <asm/text-patching.h>
353d083395SSteven Rostedt
36caf4b323SFrederic Weisbecker #ifdef CONFIG_DYNAMIC_FTRACE
373d083395SSteven Rostedt
38768ae440SPeter Zijlstra static int ftrace_poke_late = 0;
39768ae440SPeter Zijlstra
ftrace_arch_code_modify_prepare(void)403a2bfec0SLi kunyu void ftrace_arch_code_modify_prepare(void)
41074376acSJiri Kosina __acquires(&text_mutex)
4216239630SSteven Rostedt {
4339611265SSteven Rostedt (VMware) /*
4439611265SSteven Rostedt (VMware) * Need to grab text_mutex to prevent a race from module loading
4539611265SSteven Rostedt (VMware) * and live kernel patching from changing the text permissions while
4639611265SSteven Rostedt (VMware) * ftrace has it set to "read/write".
4739611265SSteven Rostedt (VMware) */
48d5b844a2SPetr Mladek mutex_lock(&text_mutex);
49768ae440SPeter Zijlstra ftrace_poke_late = 1;
5016239630SSteven Rostedt }
5116239630SSteven Rostedt
ftrace_arch_code_modify_post_process(void)523a2bfec0SLi kunyu void ftrace_arch_code_modify_post_process(void)
53074376acSJiri Kosina __releases(&text_mutex)
5416239630SSteven Rostedt {
55768ae440SPeter Zijlstra /*
56768ae440SPeter Zijlstra * ftrace_make_{call,nop}() may be called during
57768ae440SPeter Zijlstra * module load, and we need to finish the text_poke_queue()
58768ae440SPeter Zijlstra * that they do, here.
59768ae440SPeter Zijlstra */
60768ae440SPeter Zijlstra text_poke_finish();
61768ae440SPeter Zijlstra ftrace_poke_late = 0;
62d5b844a2SPetr Mladek mutex_unlock(&text_mutex);
6316239630SSteven Rostedt }
6416239630SSteven Rostedt
ftrace_nop_replace(void)65768ae440SPeter Zijlstra static const char *ftrace_nop_replace(void)
66caf4b323SFrederic Weisbecker {
67a89dfde3SPeter Zijlstra return x86_nops[5];
68caf4b323SFrederic Weisbecker }
69caf4b323SFrederic Weisbecker
ftrace_call_replace(unsigned long ip,unsigned long addr)70768ae440SPeter Zijlstra static const char *ftrace_call_replace(unsigned long ip, unsigned long addr)
713d083395SSteven Rostedt {
72ee3e2469SPeter Zijlstra /*
73ee3e2469SPeter Zijlstra * No need to translate into a callthunk. The trampoline does
74ee3e2469SPeter Zijlstra * the depth accounting itself.
75ee3e2469SPeter Zijlstra */
7667c1d4a2SPeter Zijlstra return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr);
77768ae440SPeter Zijlstra }
783d083395SSteven Rostedt
ftrace_verify_code(unsigned long ip,const char * old_code)79768ae440SPeter Zijlstra static int ftrace_verify_code(unsigned long ip, const char *old_code)
80768ae440SPeter Zijlstra {
81768ae440SPeter Zijlstra char cur_code[MCOUNT_INSN_SIZE];
82b05086c7SSteven Rostedt (Red Hat)
833d083395SSteven Rostedt /*
84c5d641f9SLi Bin * Note:
85c5d641f9SLi Bin * We are paranoid about modifying text, as if a bug was to happen, it
86c5d641f9SLi Bin * could cause us to read or write to someplace that could cause harm.
87c5d641f9SLi Bin * Carefully read and modify the code with probe_kernel_*(), and make
88c5d641f9SLi Bin * sure what we read is what we expected it to be before modifying it.
893d083395SSteven Rostedt */
9076aefee5SSteven Rostedt /* read the text we want to modify */
91fe557319SChristoph Hellwig if (copy_from_kernel_nofault(cur_code, (void *)ip, MCOUNT_INSN_SIZE)) {
92768ae440SPeter Zijlstra WARN_ON(1);
93593eb8a2SSteven Rostedt return -EFAULT;
94768ae440SPeter Zijlstra }
956f93fc07SSteven Rostedt
9676aefee5SSteven Rostedt /* Make sure it is what we expect it to be */
97768ae440SPeter Zijlstra if (memcmp(cur_code, old_code, MCOUNT_INSN_SIZE) != 0) {
98ac6c1b2cSSteven Rostedt (Google) ftrace_expected = old_code;
99768ae440SPeter Zijlstra WARN_ON(1);
100593eb8a2SSteven Rostedt return -EINVAL;
101768ae440SPeter Zijlstra }
1023d083395SSteven Rostedt
1036f93fc07SSteven Rostedt return 0;
1043d083395SSteven Rostedt }
1053d083395SSteven Rostedt
10638ebd8d1SBorislav Petkov /*
10738ebd8d1SBorislav Petkov * Marked __ref because it calls text_poke_early() which is .init.text. That is
10838ebd8d1SBorislav Petkov * ok because that call will happen early, during boot, when .init sections are
10938ebd8d1SBorislav Petkov * still present.
11038ebd8d1SBorislav Petkov */
11138ebd8d1SBorislav Petkov static int __ref
ftrace_modify_code_direct(unsigned long ip,const char * old_code,const char * new_code)112768ae440SPeter Zijlstra ftrace_modify_code_direct(unsigned long ip, const char *old_code,
113768ae440SPeter Zijlstra const char *new_code)
11431e88909SSteven Rostedt {
115768ae440SPeter Zijlstra int ret = ftrace_verify_code(ip, old_code);
116768ae440SPeter Zijlstra if (ret)
117768ae440SPeter Zijlstra return ret;
118768ae440SPeter Zijlstra
119768ae440SPeter Zijlstra /* replace the text with the new text */
120768ae440SPeter Zijlstra if (ftrace_poke_late)
121768ae440SPeter Zijlstra text_poke_queue((void *)ip, new_code, MCOUNT_INSN_SIZE, NULL);
122768ae440SPeter Zijlstra else
123768ae440SPeter Zijlstra text_poke_early((void *)ip, new_code, MCOUNT_INSN_SIZE);
124768ae440SPeter Zijlstra return 0;
125768ae440SPeter Zijlstra }
126768ae440SPeter Zijlstra
ftrace_make_nop(struct module * mod,struct dyn_ftrace * rec,unsigned long addr)127768ae440SPeter Zijlstra int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
128768ae440SPeter Zijlstra {
12931e88909SSteven Rostedt unsigned long ip = rec->ip;
130768ae440SPeter Zijlstra const char *new, *old;
13131e88909SSteven Rostedt
13231e88909SSteven Rostedt old = ftrace_call_replace(ip, addr);
13331e88909SSteven Rostedt new = ftrace_nop_replace();
13431e88909SSteven Rostedt
1358a4d0a68SSteven Rostedt /*
1368a4d0a68SSteven Rostedt * On boot up, and when modules are loaded, the MCOUNT_ADDR
1378a4d0a68SSteven Rostedt * is converted to a nop, and will never become MCOUNT_ADDR
1388a4d0a68SSteven Rostedt * again. This code is either running before SMP (on boot up)
1398a4d0a68SSteven Rostedt * or before the code will ever be executed (module load).
1408a4d0a68SSteven Rostedt * We do not want to use the breakpoint version in this case,
1418a4d0a68SSteven Rostedt * just modify the code directly.
1428a4d0a68SSteven Rostedt */
1438a4d0a68SSteven Rostedt if (addr == MCOUNT_ADDR)
144768ae440SPeter Zijlstra return ftrace_modify_code_direct(ip, old, new);
1458a4d0a68SSteven Rostedt
146768ae440SPeter Zijlstra /*
147768ae440SPeter Zijlstra * x86 overrides ftrace_replace_code -- this function will never be used
148768ae440SPeter Zijlstra * in this case.
149768ae440SPeter Zijlstra */
1508a4d0a68SSteven Rostedt WARN_ONCE(1, "invalid use of ftrace_make_nop");
1518a4d0a68SSteven Rostedt return -EINVAL;
15231e88909SSteven Rostedt }
15331e88909SSteven Rostedt
ftrace_make_call(struct dyn_ftrace * rec,unsigned long addr)15431e88909SSteven Rostedt int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
15531e88909SSteven Rostedt {
15631e88909SSteven Rostedt unsigned long ip = rec->ip;
157768ae440SPeter Zijlstra const char *new, *old;
15831e88909SSteven Rostedt
15931e88909SSteven Rostedt old = ftrace_nop_replace();
16031e88909SSteven Rostedt new = ftrace_call_replace(ip, addr);
16131e88909SSteven Rostedt
1628a4d0a68SSteven Rostedt /* Should only be called when module is loaded */
1638a4d0a68SSteven Rostedt return ftrace_modify_code_direct(rec->ip, old, new);
164d61f82d0SSteven Rostedt }
165d61f82d0SSteven Rostedt
166a192cd04SSteven Rostedt /*
16708f6fba5SSteven Rostedt * Should never be called:
16808f6fba5SSteven Rostedt * As it is only called by __ftrace_replace_code() which is called by
16908f6fba5SSteven Rostedt * ftrace_replace_code() that x86 overrides, and by ftrace_update_code()
17008f6fba5SSteven Rostedt * which is called to turn mcount into nops or nops into function calls
17108f6fba5SSteven Rostedt * but not to convert a function from not using regs to one that uses
17208f6fba5SSteven Rostedt * regs, which ftrace_modify_call() is for.
17308f6fba5SSteven Rostedt */
ftrace_modify_call(struct dyn_ftrace * rec,unsigned long old_addr,unsigned long addr)17408f6fba5SSteven Rostedt int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
17508f6fba5SSteven Rostedt unsigned long addr)
17608f6fba5SSteven Rostedt {
17708f6fba5SSteven Rostedt WARN_ON(1);
17808f6fba5SSteven Rostedt return -EINVAL;
17908f6fba5SSteven Rostedt }
18008f6fba5SSteven Rostedt
ftrace_update_ftrace_func(ftrace_func_t func)18187fbb2acSSteven Rostedt (Red Hat) int ftrace_update_ftrace_func(ftrace_func_t func)
18287fbb2acSSteven Rostedt (Red Hat) {
183768ae440SPeter Zijlstra unsigned long ip;
184768ae440SPeter Zijlstra const char *new;
18587fbb2acSSteven Rostedt (Red Hat)
186768ae440SPeter Zijlstra ip = (unsigned long)(&ftrace_call);
18787fbb2acSSteven Rostedt (Red Hat) new = ftrace_call_replace(ip, (unsigned long)func);
188768ae440SPeter Zijlstra text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
18987fbb2acSSteven Rostedt (Red Hat)
19008f6fba5SSteven Rostedt ip = (unsigned long)(&ftrace_regs_call);
19108f6fba5SSteven Rostedt new = ftrace_call_replace(ip, (unsigned long)func);
192768ae440SPeter Zijlstra text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
193ab4ead02SKevin Hao
194ab4ead02SKevin Hao return 0;
195ab4ead02SKevin Hao }
196ab4ead02SKevin Hao
ftrace_replace_code(int enable)197e4f5d544SSteven Rostedt void ftrace_replace_code(int enable)
19808d636b6SSteven Rostedt {
19908d636b6SSteven Rostedt struct ftrace_rec_iter *iter;
20008d636b6SSteven Rostedt struct dyn_ftrace *rec;
201768ae440SPeter Zijlstra const char *new, *old;
20208d636b6SSteven Rostedt int ret;
20308d636b6SSteven Rostedt
20408d636b6SSteven Rostedt for_ftrace_rec_iter(iter) {
20508d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter);
20608d636b6SSteven Rostedt
207768ae440SPeter Zijlstra switch (ftrace_test_record(rec, enable)) {
208768ae440SPeter Zijlstra case FTRACE_UPDATE_IGNORE:
209768ae440SPeter Zijlstra default:
210768ae440SPeter Zijlstra continue;
211768ae440SPeter Zijlstra
212768ae440SPeter Zijlstra case FTRACE_UPDATE_MAKE_CALL:
213768ae440SPeter Zijlstra old = ftrace_nop_replace();
214768ae440SPeter Zijlstra break;
215768ae440SPeter Zijlstra
216768ae440SPeter Zijlstra case FTRACE_UPDATE_MODIFY_CALL:
217768ae440SPeter Zijlstra case FTRACE_UPDATE_MAKE_NOP:
218768ae440SPeter Zijlstra old = ftrace_call_replace(rec->ip, ftrace_get_addr_curr(rec));
219768ae440SPeter Zijlstra break;
22008d636b6SSteven Rostedt }
22108d636b6SSteven Rostedt
222768ae440SPeter Zijlstra ret = ftrace_verify_code(rec->ip, old);
223768ae440SPeter Zijlstra if (ret) {
224fd3dc562SSteven Rostedt (Google) ftrace_expected = old;
2254fd3279bSSteven Rostedt (Red Hat) ftrace_bug(ret, rec);
226fd3dc562SSteven Rostedt (Google) ftrace_expected = NULL;
227768ae440SPeter Zijlstra return;
228768ae440SPeter Zijlstra }
229768ae440SPeter Zijlstra }
230768ae440SPeter Zijlstra
23108d636b6SSteven Rostedt for_ftrace_rec_iter(iter) {
23208d636b6SSteven Rostedt rec = ftrace_rec_iter_record(iter);
233768ae440SPeter Zijlstra
234768ae440SPeter Zijlstra switch (ftrace_test_record(rec, enable)) {
235768ae440SPeter Zijlstra case FTRACE_UPDATE_IGNORE:
236768ae440SPeter Zijlstra default:
237768ae440SPeter Zijlstra continue;
238768ae440SPeter Zijlstra
239768ae440SPeter Zijlstra case FTRACE_UPDATE_MAKE_CALL:
240768ae440SPeter Zijlstra case FTRACE_UPDATE_MODIFY_CALL:
241768ae440SPeter Zijlstra new = ftrace_call_replace(rec->ip, ftrace_get_addr_new(rec));
242768ae440SPeter Zijlstra break;
243768ae440SPeter Zijlstra
244768ae440SPeter Zijlstra case FTRACE_UPDATE_MAKE_NOP:
245768ae440SPeter Zijlstra new = ftrace_nop_replace();
246768ae440SPeter Zijlstra break;
24708d636b6SSteven Rostedt }
24808d636b6SSteven Rostedt
249768ae440SPeter Zijlstra text_poke_queue((void *)rec->ip, new, MCOUNT_INSN_SIZE, NULL);
250768ae440SPeter Zijlstra ftrace_update_record(rec, enable);
251768ae440SPeter Zijlstra }
252768ae440SPeter Zijlstra text_poke_finish();
2538a4d0a68SSteven Rostedt }
2548a4d0a68SSteven Rostedt
arch_ftrace_update_code(int command)25508d636b6SSteven Rostedt void arch_ftrace_update_code(int command)
25608d636b6SSteven Rostedt {
257e4f5d544SSteven Rostedt ftrace_modify_all_code(command);
25808d636b6SSteven Rostedt }
25908d636b6SSteven Rostedt
260f3bea491SSteven Rostedt (Red Hat) /* Currently only x86_64 supports dynamic trampolines */
261f3bea491SSteven Rostedt (Red Hat) #ifdef CONFIG_X86_64
262f3bea491SSteven Rostedt (Red Hat)
263f3bea491SSteven Rostedt (Red Hat) #ifdef CONFIG_MODULES
264f3bea491SSteven Rostedt (Red Hat) #include <linux/moduleloader.h>
265f3bea491SSteven Rostedt (Red Hat) /* Module allocation simplifies allocating memory for code */
alloc_tramp(unsigned long size)266f3bea491SSteven Rostedt (Red Hat) static inline void *alloc_tramp(unsigned long size)
267f3bea491SSteven Rostedt (Red Hat) {
268f3bea491SSteven Rostedt (Red Hat) return module_alloc(size);
269f3bea491SSteven Rostedt (Red Hat) }
tramp_free(void * tramp)2707fdfe1e4SRick Edgecombe static inline void tramp_free(void *tramp)
271f3bea491SSteven Rostedt (Red Hat) {
272be1f221cSRusty Russell module_memfree(tramp);
273f3bea491SSteven Rostedt (Red Hat) }
274f3bea491SSteven Rostedt (Red Hat) #else
275f3bea491SSteven Rostedt (Red Hat) /* Trampolines can only be created if modules are supported */
alloc_tramp(unsigned long size)276f3bea491SSteven Rostedt (Red Hat) static inline void *alloc_tramp(unsigned long size)
277f3bea491SSteven Rostedt (Red Hat) {
278f3bea491SSteven Rostedt (Red Hat) return NULL;
279f3bea491SSteven Rostedt (Red Hat) }
tramp_free(void * tramp)2807fdfe1e4SRick Edgecombe static inline void tramp_free(void *tramp) { }
281f3bea491SSteven Rostedt (Red Hat) #endif
282f3bea491SSteven Rostedt (Red Hat)
283f3bea491SSteven Rostedt (Red Hat) /* Defined as markers to the end of the ftrace default trampolines */
284f3bea491SSteven Rostedt (Red Hat) extern void ftrace_regs_caller_end(void);
2850298739bSPeter Zijlstra extern void ftrace_caller_end(void);
286f3bea491SSteven Rostedt (Red Hat) extern void ftrace_caller_op_ptr(void);
287f3bea491SSteven Rostedt (Red Hat) extern void ftrace_regs_caller_op_ptr(void);
288fe58acefSSteven Rostedt (VMware) extern void ftrace_regs_caller_jmp(void);
289f3bea491SSteven Rostedt (Red Hat)
290f3bea491SSteven Rostedt (Red Hat) /* movq function_trace_op(%rip), %rdx */
291f3bea491SSteven Rostedt (Red Hat) /* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */
292f3bea491SSteven Rostedt (Red Hat) #define OP_REF_SIZE 7
293f3bea491SSteven Rostedt (Red Hat)
294f3bea491SSteven Rostedt (Red Hat) /*
295f3bea491SSteven Rostedt (Red Hat) * The ftrace_ops is passed to the function callback. Since the
296f3bea491SSteven Rostedt (Red Hat) * trampoline only services a single ftrace_ops, we can pass in
297f3bea491SSteven Rostedt (Red Hat) * that ops directly.
298f3bea491SSteven Rostedt (Red Hat) *
299f3bea491SSteven Rostedt (Red Hat) * The ftrace_op_code_union is used to create a pointer to the
300f3bea491SSteven Rostedt (Red Hat) * ftrace_ops that will be passed to the callback function.
301f3bea491SSteven Rostedt (Red Hat) */
302f3bea491SSteven Rostedt (Red Hat) union ftrace_op_code_union {
303f3bea491SSteven Rostedt (Red Hat) char code[OP_REF_SIZE];
304f3bea491SSteven Rostedt (Red Hat) struct {
305f3bea491SSteven Rostedt (Red Hat) char op[3];
306f3bea491SSteven Rostedt (Red Hat) int offset;
307f3bea491SSteven Rostedt (Red Hat) } __attribute__((packed));
308f3bea491SSteven Rostedt (Red Hat) };
309f3bea491SSteven Rostedt (Red Hat)
3101f001e9dSPeter Zijlstra #define RET_SIZE (IS_ENABLED(CONFIG_RETPOLINE) ? 5 : 1 + IS_ENABLED(CONFIG_SLS))
311d2a68c4eSSteven Rostedt (VMware)
312aec0be2dSSteven Rostedt (Red Hat) static unsigned long
create_trampoline(struct ftrace_ops * ops,unsigned int * tramp_size)313aec0be2dSSteven Rostedt (Red Hat) create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
314f3bea491SSteven Rostedt (Red Hat) {
315f3bea491SSteven Rostedt (Red Hat) unsigned long start_offset;
316f3bea491SSteven Rostedt (Red Hat) unsigned long end_offset;
317f3bea491SSteven Rostedt (Red Hat) unsigned long op_offset;
318768ae440SPeter Zijlstra unsigned long call_offset;
319fe58acefSSteven Rostedt (VMware) unsigned long jmp_offset;
320f3bea491SSteven Rostedt (Red Hat) unsigned long offset;
3213c0dab44SNadav Amit unsigned long npages;
322f3bea491SSteven Rostedt (Red Hat) unsigned long size;
323f3bea491SSteven Rostedt (Red Hat) unsigned long *ptr;
324f3bea491SSteven Rostedt (Red Hat) void *trampoline;
325ee3e2469SPeter Zijlstra void *ip, *dest;
326f3bea491SSteven Rostedt (Red Hat) /* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */
327f3bea491SSteven Rostedt (Red Hat) unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 };
328e52fc2cfSPeter Zijlstra unsigned const char retq[] = { RET_INSN_OPCODE, INT3_INSN_OPCODE };
329f3bea491SSteven Rostedt (Red Hat) union ftrace_op_code_union op_ptr;
330f3bea491SSteven Rostedt (Red Hat) int ret;
331f3bea491SSteven Rostedt (Red Hat)
332f3bea491SSteven Rostedt (Red Hat) if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
333f3bea491SSteven Rostedt (Red Hat) start_offset = (unsigned long)ftrace_regs_caller;
334f3bea491SSteven Rostedt (Red Hat) end_offset = (unsigned long)ftrace_regs_caller_end;
335f3bea491SSteven Rostedt (Red Hat) op_offset = (unsigned long)ftrace_regs_caller_op_ptr;
336768ae440SPeter Zijlstra call_offset = (unsigned long)ftrace_regs_call;
337fe58acefSSteven Rostedt (VMware) jmp_offset = (unsigned long)ftrace_regs_caller_jmp;
338f3bea491SSteven Rostedt (Red Hat) } else {
339f3bea491SSteven Rostedt (Red Hat) start_offset = (unsigned long)ftrace_caller;
3400298739bSPeter Zijlstra end_offset = (unsigned long)ftrace_caller_end;
341f3bea491SSteven Rostedt (Red Hat) op_offset = (unsigned long)ftrace_caller_op_ptr;
342768ae440SPeter Zijlstra call_offset = (unsigned long)ftrace_call;
343fe58acefSSteven Rostedt (VMware) jmp_offset = 0;
344f3bea491SSteven Rostedt (Red Hat) }
345f3bea491SSteven Rostedt (Red Hat)
346f3bea491SSteven Rostedt (Red Hat) size = end_offset - start_offset;
347f3bea491SSteven Rostedt (Red Hat)
348f3bea491SSteven Rostedt (Red Hat) /*
349f3bea491SSteven Rostedt (Red Hat) * Allocate enough size to store the ftrace_caller code,
350d2a68c4eSSteven Rostedt (VMware) * the iret , as well as the address of the ftrace_ops this
351d2a68c4eSSteven Rostedt (VMware) * trampoline is used for.
352f3bea491SSteven Rostedt (Red Hat) */
353d2a68c4eSSteven Rostedt (VMware) trampoline = alloc_tramp(size + RET_SIZE + sizeof(void *));
354f3bea491SSteven Rostedt (Red Hat) if (!trampoline)
355f3bea491SSteven Rostedt (Red Hat) return 0;
356f3bea491SSteven Rostedt (Red Hat)
357d2a68c4eSSteven Rostedt (VMware) *tramp_size = size + RET_SIZE + sizeof(void *);
3583c0dab44SNadav Amit npages = DIV_ROUND_UP(*tramp_size, PAGE_SIZE);
359aec0be2dSSteven Rostedt (Red Hat)
360f3bea491SSteven Rostedt (Red Hat) /* Copy ftrace_caller onto the trampoline memory */
361fe557319SChristoph Hellwig ret = copy_from_kernel_nofault(trampoline, (void *)start_offset, size);
362d2a68c4eSSteven Rostedt (VMware) if (WARN_ON(ret < 0))
363d2a68c4eSSteven Rostedt (VMware) goto fail;
364f3bea491SSteven Rostedt (Red Hat)
365d2a68c4eSSteven Rostedt (VMware) ip = trampoline + size;
3661f001e9dSPeter Zijlstra if (cpu_feature_enabled(X86_FEATURE_RETHUNK))
367770ae1b7SPeter Zijlstra __text_gen_insn(ip, JMP32_INSN_OPCODE, ip, x86_return_thunk, JMP32_INSN_SIZE);
3681f001e9dSPeter Zijlstra else
3691f001e9dSPeter Zijlstra memcpy(ip, retq, sizeof(retq));
370f3bea491SSteven Rostedt (Red Hat)
371fe58acefSSteven Rostedt (VMware) /* No need to test direct calls on created trampolines */
372fe58acefSSteven Rostedt (VMware) if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
373fe58acefSSteven Rostedt (VMware) /* NOP the jnz 1f; but make sure it's a 2 byte jnz */
374fe58acefSSteven Rostedt (VMware) ip = trampoline + (jmp_offset - start_offset);
375fe58acefSSteven Rostedt (VMware) if (WARN_ON(*(char *)ip != 0x75))
376fe58acefSSteven Rostedt (VMware) goto fail;
377a89dfde3SPeter Zijlstra ret = copy_from_kernel_nofault(ip, x86_nops[2], 2);
378fe58acefSSteven Rostedt (VMware) if (ret < 0)
379fe58acefSSteven Rostedt (VMware) goto fail;
380fe58acefSSteven Rostedt (VMware) }
381fe58acefSSteven Rostedt (VMware)
382f3bea491SSteven Rostedt (Red Hat) /*
383f3bea491SSteven Rostedt (Red Hat) * The address of the ftrace_ops that is used for this trampoline
384f3bea491SSteven Rostedt (Red Hat) * is stored at the end of the trampoline. This will be used to
385f3bea491SSteven Rostedt (Red Hat) * load the third parameter for the callback. Basically, that
386f3bea491SSteven Rostedt (Red Hat) * location at the end of the trampoline takes the place of
387f3bea491SSteven Rostedt (Red Hat) * the global function_trace_op variable.
388f3bea491SSteven Rostedt (Red Hat) */
389f3bea491SSteven Rostedt (Red Hat)
390d2a68c4eSSteven Rostedt (VMware) ptr = (unsigned long *)(trampoline + size + RET_SIZE);
391f3bea491SSteven Rostedt (Red Hat) *ptr = (unsigned long)ops;
392f3bea491SSteven Rostedt (Red Hat)
393f3bea491SSteven Rostedt (Red Hat) op_offset -= start_offset;
394f3bea491SSteven Rostedt (Red Hat) memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE);
395f3bea491SSteven Rostedt (Red Hat)
396f3bea491SSteven Rostedt (Red Hat) /* Are we pointing to the reference? */
397d2a68c4eSSteven Rostedt (VMware) if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0))
398d2a68c4eSSteven Rostedt (VMware) goto fail;
399f3bea491SSteven Rostedt (Red Hat)
400f3bea491SSteven Rostedt (Red Hat) /* Load the contents of ptr into the callback parameter */
401f3bea491SSteven Rostedt (Red Hat) offset = (unsigned long)ptr;
402f3bea491SSteven Rostedt (Red Hat) offset -= (unsigned long)trampoline + op_offset + OP_REF_SIZE;
403f3bea491SSteven Rostedt (Red Hat)
404f3bea491SSteven Rostedt (Red Hat) op_ptr.offset = offset;
405f3bea491SSteven Rostedt (Red Hat)
406f3bea491SSteven Rostedt (Red Hat) /* put in the new offset to the ftrace_ops */
407f3bea491SSteven Rostedt (Red Hat) memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE);
408f3bea491SSteven Rostedt (Red Hat)
409768ae440SPeter Zijlstra /* put in the call to the function */
410768ae440SPeter Zijlstra mutex_lock(&text_mutex);
411768ae440SPeter Zijlstra call_offset -= start_offset;
412ee3e2469SPeter Zijlstra /*
413ee3e2469SPeter Zijlstra * No need to translate into a callthunk. The trampoline does
414ee3e2469SPeter Zijlstra * the depth accounting before the call already.
415ee3e2469SPeter Zijlstra */
416ee3e2469SPeter Zijlstra dest = ftrace_ops_get_func(ops);
417768ae440SPeter Zijlstra memcpy(trampoline + call_offset,
418ee3e2469SPeter Zijlstra text_gen_insn(CALL_INSN_OPCODE, trampoline + call_offset, dest),
419ee3e2469SPeter Zijlstra CALL_INSN_SIZE);
420768ae440SPeter Zijlstra mutex_unlock(&text_mutex);
421768ae440SPeter Zijlstra
422f3bea491SSteven Rostedt (Red Hat) /* ALLOC_TRAMP flags lets us know we created it */
423f3bea491SSteven Rostedt (Red Hat) ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
424f3bea491SSteven Rostedt (Red Hat)
425*d48567c9SPeter Zijlstra set_memory_rox((unsigned long)trampoline, npages);
426f3bea491SSteven Rostedt (Red Hat) return (unsigned long)trampoline;
427d2a68c4eSSteven Rostedt (VMware) fail:
4287fdfe1e4SRick Edgecombe tramp_free(trampoline);
429d2a68c4eSSteven Rostedt (VMware) return 0;
430f3bea491SSteven Rostedt (Red Hat) }
431f3bea491SSteven Rostedt (Red Hat)
set_ftrace_ops_ro(void)43259566b0bSSteven Rostedt (VMware) void set_ftrace_ops_ro(void)
43359566b0bSSteven Rostedt (VMware) {
43459566b0bSSteven Rostedt (VMware) struct ftrace_ops *ops;
43559566b0bSSteven Rostedt (VMware) unsigned long start_offset;
43659566b0bSSteven Rostedt (VMware) unsigned long end_offset;
43759566b0bSSteven Rostedt (VMware) unsigned long npages;
43859566b0bSSteven Rostedt (VMware) unsigned long size;
43959566b0bSSteven Rostedt (VMware)
44059566b0bSSteven Rostedt (VMware) do_for_each_ftrace_op(ops, ftrace_ops_list) {
44159566b0bSSteven Rostedt (VMware) if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
44259566b0bSSteven Rostedt (VMware) continue;
44359566b0bSSteven Rostedt (VMware)
44459566b0bSSteven Rostedt (VMware) if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
44559566b0bSSteven Rostedt (VMware) start_offset = (unsigned long)ftrace_regs_caller;
44659566b0bSSteven Rostedt (VMware) end_offset = (unsigned long)ftrace_regs_caller_end;
44759566b0bSSteven Rostedt (VMware) } else {
44859566b0bSSteven Rostedt (VMware) start_offset = (unsigned long)ftrace_caller;
4497c0577f4SIngo Molnar end_offset = (unsigned long)ftrace_caller_end;
45059566b0bSSteven Rostedt (VMware) }
45159566b0bSSteven Rostedt (VMware) size = end_offset - start_offset;
45259566b0bSSteven Rostedt (VMware) size = size + RET_SIZE + sizeof(void *);
45359566b0bSSteven Rostedt (VMware) npages = DIV_ROUND_UP(size, PAGE_SIZE);
45459566b0bSSteven Rostedt (VMware) set_memory_ro((unsigned long)ops->trampoline, npages);
45559566b0bSSteven Rostedt (VMware) } while_for_each_ftrace_op(ops);
45659566b0bSSteven Rostedt (VMware) }
45759566b0bSSteven Rostedt (VMware)
calc_trampoline_call_offset(bool save_regs)45815d5b02cSSteven Rostedt (Red Hat) static unsigned long calc_trampoline_call_offset(bool save_regs)
45915d5b02cSSteven Rostedt (Red Hat) {
46015d5b02cSSteven Rostedt (Red Hat) unsigned long start_offset;
46115d5b02cSSteven Rostedt (Red Hat) unsigned long call_offset;
46215d5b02cSSteven Rostedt (Red Hat)
46315d5b02cSSteven Rostedt (Red Hat) if (save_regs) {
46415d5b02cSSteven Rostedt (Red Hat) start_offset = (unsigned long)ftrace_regs_caller;
46515d5b02cSSteven Rostedt (Red Hat) call_offset = (unsigned long)ftrace_regs_call;
46615d5b02cSSteven Rostedt (Red Hat) } else {
46715d5b02cSSteven Rostedt (Red Hat) start_offset = (unsigned long)ftrace_caller;
46815d5b02cSSteven Rostedt (Red Hat) call_offset = (unsigned long)ftrace_call;
46915d5b02cSSteven Rostedt (Red Hat) }
47015d5b02cSSteven Rostedt (Red Hat)
47115d5b02cSSteven Rostedt (Red Hat) return call_offset - start_offset;
47215d5b02cSSteven Rostedt (Red Hat) }
47315d5b02cSSteven Rostedt (Red Hat)
arch_ftrace_update_trampoline(struct ftrace_ops * ops)474f3bea491SSteven Rostedt (Red Hat) void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
475f3bea491SSteven Rostedt (Red Hat) {
476f3bea491SSteven Rostedt (Red Hat) ftrace_func_t func;
477f3bea491SSteven Rostedt (Red Hat) unsigned long offset;
478f3bea491SSteven Rostedt (Red Hat) unsigned long ip;
479aec0be2dSSteven Rostedt (Red Hat) unsigned int size;
480768ae440SPeter Zijlstra const char *new;
481f3bea491SSteven Rostedt (Red Hat)
482768ae440SPeter Zijlstra if (!ops->trampoline) {
483768ae440SPeter Zijlstra ops->trampoline = create_trampoline(ops, &size);
484768ae440SPeter Zijlstra if (!ops->trampoline)
485768ae440SPeter Zijlstra return;
486768ae440SPeter Zijlstra ops->trampoline_size = size;
487768ae440SPeter Zijlstra return;
488768ae440SPeter Zijlstra }
489768ae440SPeter Zijlstra
490f3bea491SSteven Rostedt (Red Hat) /*
491f3bea491SSteven Rostedt (Red Hat) * The ftrace_ops caller may set up its own trampoline.
492f3bea491SSteven Rostedt (Red Hat) * In such a case, this code must not modify it.
493f3bea491SSteven Rostedt (Red Hat) */
494f3bea491SSteven Rostedt (Red Hat) if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
495f3bea491SSteven Rostedt (Red Hat) return;
496f3bea491SSteven Rostedt (Red Hat)
49715d5b02cSSteven Rostedt (Red Hat) offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
498f3bea491SSteven Rostedt (Red Hat) ip = ops->trampoline + offset;
499f3bea491SSteven Rostedt (Red Hat) func = ftrace_ops_get_func(ops);
500f3bea491SSteven Rostedt (Red Hat)
501768ae440SPeter Zijlstra mutex_lock(&text_mutex);
502f3bea491SSteven Rostedt (Red Hat) /* Do a safe modify in case the trampoline is executing */
503f3bea491SSteven Rostedt (Red Hat) new = ftrace_call_replace(ip, (unsigned long)func);
504768ae440SPeter Zijlstra text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
505768ae440SPeter Zijlstra mutex_unlock(&text_mutex);
506f3bea491SSteven Rostedt (Red Hat) }
50715d5b02cSSteven Rostedt (Red Hat)
50815d5b02cSSteven Rostedt (Red Hat) /* Return the address of the function the trampoline calls */
addr_from_call(void * ptr)50915d5b02cSSteven Rostedt (Red Hat) static void *addr_from_call(void *ptr)
51015d5b02cSSteven Rostedt (Red Hat) {
51167c1d4a2SPeter Zijlstra union text_poke_insn call;
51215d5b02cSSteven Rostedt (Red Hat) int ret;
51315d5b02cSSteven Rostedt (Red Hat)
514fe557319SChristoph Hellwig ret = copy_from_kernel_nofault(&call, ptr, CALL_INSN_SIZE);
51515d5b02cSSteven Rostedt (Red Hat) if (WARN_ON_ONCE(ret < 0))
51615d5b02cSSteven Rostedt (Red Hat) return NULL;
51715d5b02cSSteven Rostedt (Red Hat)
51815d5b02cSSteven Rostedt (Red Hat) /* Make sure this is a call */
51967c1d4a2SPeter Zijlstra if (WARN_ON_ONCE(call.opcode != CALL_INSN_OPCODE)) {
52067c1d4a2SPeter Zijlstra pr_warn("Expected E8, got %x\n", call.opcode);
52115d5b02cSSteven Rostedt (Red Hat) return NULL;
52215d5b02cSSteven Rostedt (Red Hat) }
52315d5b02cSSteven Rostedt (Red Hat)
52467c1d4a2SPeter Zijlstra return ptr + CALL_INSN_SIZE + call.disp;
52515d5b02cSSteven Rostedt (Red Hat) }
52615d5b02cSSteven Rostedt (Red Hat)
52715d5b02cSSteven Rostedt (Red Hat) /*
52815d5b02cSSteven Rostedt (Red Hat) * If the ops->trampoline was not allocated, then it probably
52915d5b02cSSteven Rostedt (Red Hat) * has a static trampoline func, or is the ftrace caller itself.
53015d5b02cSSteven Rostedt (Red Hat) */
static_tramp_func(struct ftrace_ops * ops,struct dyn_ftrace * rec)53115d5b02cSSteven Rostedt (Red Hat) static void *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
53215d5b02cSSteven Rostedt (Red Hat) {
53315d5b02cSSteven Rostedt (Red Hat) unsigned long offset;
53415d5b02cSSteven Rostedt (Red Hat) bool save_regs = rec->flags & FTRACE_FL_REGS_EN;
53515d5b02cSSteven Rostedt (Red Hat) void *ptr;
53615d5b02cSSteven Rostedt (Red Hat)
53715d5b02cSSteven Rostedt (Red Hat) if (ops && ops->trampoline) {
5380c0593b4SSteven Rostedt (VMware) #if !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS) && \
5390c0593b4SSteven Rostedt (VMware) defined(CONFIG_FUNCTION_GRAPH_TRACER)
54015d5b02cSSteven Rostedt (Red Hat) /*
54115d5b02cSSteven Rostedt (Red Hat) * We only know about function graph tracer setting as static
54215d5b02cSSteven Rostedt (Red Hat) * trampoline.
54315d5b02cSSteven Rostedt (Red Hat) */
54415d5b02cSSteven Rostedt (Red Hat) if (ops->trampoline == FTRACE_GRAPH_ADDR)
54515d5b02cSSteven Rostedt (Red Hat) return (void *)prepare_ftrace_return;
54615d5b02cSSteven Rostedt (Red Hat) #endif
54715d5b02cSSteven Rostedt (Red Hat) return NULL;
54815d5b02cSSteven Rostedt (Red Hat) }
54915d5b02cSSteven Rostedt (Red Hat)
55015d5b02cSSteven Rostedt (Red Hat) offset = calc_trampoline_call_offset(save_regs);
55115d5b02cSSteven Rostedt (Red Hat)
55215d5b02cSSteven Rostedt (Red Hat) if (save_regs)
55315d5b02cSSteven Rostedt (Red Hat) ptr = (void *)FTRACE_REGS_ADDR + offset;
55415d5b02cSSteven Rostedt (Red Hat) else
55515d5b02cSSteven Rostedt (Red Hat) ptr = (void *)FTRACE_ADDR + offset;
55615d5b02cSSteven Rostedt (Red Hat)
55715d5b02cSSteven Rostedt (Red Hat) return addr_from_call(ptr);
55815d5b02cSSteven Rostedt (Red Hat) }
55915d5b02cSSteven Rostedt (Red Hat)
arch_ftrace_trampoline_func(struct ftrace_ops * ops,struct dyn_ftrace * rec)56015d5b02cSSteven Rostedt (Red Hat) void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
56115d5b02cSSteven Rostedt (Red Hat) {
56215d5b02cSSteven Rostedt (Red Hat) unsigned long offset;
56315d5b02cSSteven Rostedt (Red Hat)
56415d5b02cSSteven Rostedt (Red Hat) /* If we didn't allocate this trampoline, consider it static */
56515d5b02cSSteven Rostedt (Red Hat) if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
56615d5b02cSSteven Rostedt (Red Hat) return static_tramp_func(ops, rec);
56715d5b02cSSteven Rostedt (Red Hat)
56815d5b02cSSteven Rostedt (Red Hat) offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
56915d5b02cSSteven Rostedt (Red Hat) return addr_from_call((void *)ops->trampoline + offset);
57015d5b02cSSteven Rostedt (Red Hat) }
57115d5b02cSSteven Rostedt (Red Hat)
arch_ftrace_trampoline_free(struct ftrace_ops * ops)57212cce594SSteven Rostedt (Red Hat) void arch_ftrace_trampoline_free(struct ftrace_ops *ops)
57312cce594SSteven Rostedt (Red Hat) {
57412cce594SSteven Rostedt (Red Hat) if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
57512cce594SSteven Rostedt (Red Hat) return;
57612cce594SSteven Rostedt (Red Hat)
5777fdfe1e4SRick Edgecombe tramp_free((void *)ops->trampoline);
57812cce594SSteven Rostedt (Red Hat) ops->trampoline = 0;
57912cce594SSteven Rostedt (Red Hat) }
58015d5b02cSSteven Rostedt (Red Hat)
581f3bea491SSteven Rostedt (Red Hat) #endif /* CONFIG_X86_64 */
582f3bea491SSteven Rostedt (Red Hat) #endif /* CONFIG_DYNAMIC_FTRACE */
583f3bea491SSteven Rostedt (Red Hat)
584f3bea491SSteven Rostedt (Red Hat) #ifdef CONFIG_FUNCTION_GRAPH_TRACER
585f3bea491SSteven Rostedt (Red Hat)
586e999995cSChengming Zhou #if defined(CONFIG_DYNAMIC_FTRACE) && !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS)
5870c0593b4SSteven Rostedt (VMware) extern void ftrace_graph_call(void);
ftrace_jmp_replace(unsigned long ip,unsigned long addr)588768ae440SPeter Zijlstra static const char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
589745cfeaaSSteven Rostedt (VMware) {
59067c1d4a2SPeter Zijlstra return text_gen_insn(JMP32_INSN_OPCODE, (void *)ip, (void *)addr);
591745cfeaaSSteven Rostedt (VMware) }
592745cfeaaSSteven Rostedt (VMware)
ftrace_mod_jmp(unsigned long ip,void * func)59387fbb2acSSteven Rostedt (Red Hat) static int ftrace_mod_jmp(unsigned long ip, void *func)
59487fbb2acSSteven Rostedt (Red Hat) {
595768ae440SPeter Zijlstra const char *new;
5965a45cfe1SSteven Rostedt
59787fbb2acSSteven Rostedt (Red Hat) new = ftrace_jmp_replace(ip, (unsigned long)func);
598768ae440SPeter Zijlstra text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
599768ae440SPeter Zijlstra return 0;
6005a45cfe1SSteven Rostedt }
6015a45cfe1SSteven Rostedt
ftrace_enable_ftrace_graph_caller(void)6025a45cfe1SSteven Rostedt int ftrace_enable_ftrace_graph_caller(void)
6035a45cfe1SSteven Rostedt {
6045a45cfe1SSteven Rostedt unsigned long ip = (unsigned long)(&ftrace_graph_call);
6055a45cfe1SSteven Rostedt
60687fbb2acSSteven Rostedt (Red Hat) return ftrace_mod_jmp(ip, &ftrace_graph_caller);
6075a45cfe1SSteven Rostedt }
6085a45cfe1SSteven Rostedt
ftrace_disable_ftrace_graph_caller(void)6095a45cfe1SSteven Rostedt int ftrace_disable_ftrace_graph_caller(void)
6105a45cfe1SSteven Rostedt {
6115a45cfe1SSteven Rostedt unsigned long ip = (unsigned long)(&ftrace_graph_call);
6125a45cfe1SSteven Rostedt
61387fbb2acSSteven Rostedt (Red Hat) return ftrace_mod_jmp(ip, &ftrace_stub);
6145a45cfe1SSteven Rostedt }
615e999995cSChengming Zhou #endif /* CONFIG_DYNAMIC_FTRACE && !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
616e7d3737eSFrederic Weisbecker
617e7d3737eSFrederic Weisbecker /*
618e7d3737eSFrederic Weisbecker * Hook the return address and push it in the stack of return addrs
619e7d3737eSFrederic Weisbecker * in current thread info.
620e7d3737eSFrederic Weisbecker */
prepare_ftrace_return(unsigned long ip,unsigned long * parent,unsigned long frame_pointer)6218646698aSSteven Rostedt (VMware) void prepare_ftrace_return(unsigned long ip, unsigned long *parent,
62271e308a2SSteven Rostedt unsigned long frame_pointer)
623e7d3737eSFrederic Weisbecker {
624768ae440SPeter Zijlstra unsigned long return_hooker = (unsigned long)&return_to_handler;
6250c0593b4SSteven Rostedt (VMware) int bit;
626e7d3737eSFrederic Weisbecker
62734a477e5SJosh Poimboeuf /*
62834a477e5SJosh Poimboeuf * When resuming from suspend-to-ram, this function can be indirectly
62934a477e5SJosh Poimboeuf * called from early CPU startup code while the CPU is in real mode,
63034a477e5SJosh Poimboeuf * which would fail miserably. Make sure the stack pointer is a
63134a477e5SJosh Poimboeuf * virtual address.
63234a477e5SJosh Poimboeuf *
63334a477e5SJosh Poimboeuf * This check isn't as accurate as virt_addr_valid(), but it should be
63434a477e5SJosh Poimboeuf * good enough for this purpose, and it's fast.
63534a477e5SJosh Poimboeuf */
63634a477e5SJosh Poimboeuf if (unlikely((long)__builtin_frame_address(0) >= 0))
63734a477e5SJosh Poimboeuf return;
63834a477e5SJosh Poimboeuf
63984b2bc7fSSteven Rostedt (Red Hat) if (unlikely(ftrace_graph_is_dead()))
64084b2bc7fSSteven Rostedt (Red Hat) return;
64184b2bc7fSSteven Rostedt (Red Hat)
642380c4b14SFrederic Weisbecker if (unlikely(atomic_read(¤t->tracing_graph_pause)))
643e7d3737eSFrederic Weisbecker return;
644e7d3737eSFrederic Weisbecker
6450c0593b4SSteven Rostedt (VMware) bit = ftrace_test_recursion_trylock(ip, *parent);
6460c0593b4SSteven Rostedt (VMware) if (bit < 0)
6470c0593b4SSteven Rostedt (VMware) return;
6480c0593b4SSteven Rostedt (VMware)
6498646698aSSteven Rostedt (VMware) if (!function_graph_enter(*parent, ip, frame_pointer, parent))
6508646698aSSteven Rostedt (VMware) *parent = return_hooker;
6510c0593b4SSteven Rostedt (VMware)
6520c0593b4SSteven Rostedt (VMware) ftrace_test_recursion_unlock(bit);
653e7d3737eSFrederic Weisbecker }
6540c0593b4SSteven Rostedt (VMware)
6550c0593b4SSteven Rostedt (VMware) #ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
ftrace_graph_func(unsigned long ip,unsigned long parent_ip,struct ftrace_ops * op,struct ftrace_regs * fregs)6560c0593b4SSteven Rostedt (VMware) void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
6570c0593b4SSteven Rostedt (VMware) struct ftrace_ops *op, struct ftrace_regs *fregs)
6580c0593b4SSteven Rostedt (VMware) {
6590c0593b4SSteven Rostedt (VMware) struct pt_regs *regs = &fregs->regs;
6600c0593b4SSteven Rostedt (VMware) unsigned long *stack = (unsigned long *)kernel_stack_pointer(regs);
6610c0593b4SSteven Rostedt (VMware)
6620c0593b4SSteven Rostedt (VMware) prepare_ftrace_return(ip, (unsigned long *)stack, 0);
6630c0593b4SSteven Rostedt (VMware) }
6640c0593b4SSteven Rostedt (VMware) #endif
6650c0593b4SSteven Rostedt (VMware)
666fb52607aSFrederic Weisbecker #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
667