xref: /linux/arch/x86/include/asm/ftrace.h (revision 9557b4376d02088a33e5f4116bcc324d35a3b64c)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_FTRACE_H
3 #define _ASM_X86_FTRACE_H
4 
5 #ifdef CONFIG_FUNCTION_TRACER
6 #ifndef CC_USING_FENTRY
7 # error Compiler does not support fentry?
8 #endif
9 # define MCOUNT_ADDR		((unsigned long)(__fentry__))
10 #define MCOUNT_INSN_SIZE	5 /* sizeof mcount call */
11 
12 /* Ignore unused weak functions which will have non zero offsets */
13 #ifdef CONFIG_HAVE_FENTRY
14 # include <asm/ibt.h>
15 /* Add offset for endbr64 if IBT enabled */
16 # define FTRACE_MCOUNT_MAX_OFFSET	ENDBR_INSN_SIZE
17 #endif
18 
19 #ifdef CONFIG_DYNAMIC_FTRACE
20 #define ARCH_SUPPORTS_FTRACE_OPS 1
21 #endif
22 
23 #ifndef __ASSEMBLY__
24 extern void __fentry__(void);
25 
26 static inline unsigned long ftrace_call_adjust(unsigned long addr)
27 {
28 	/*
29 	 * addr is the address of the mcount call instruction.
30 	 * recordmcount does the necessary offset calculation.
31 	 */
32 	return addr;
33 }
34 
35 #ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
36 struct ftrace_regs {
37 	struct pt_regs		regs;
38 };
39 
40 static __always_inline struct pt_regs *
41 arch_ftrace_get_regs(struct ftrace_regs *fregs)
42 {
43 	/* Only when FL_SAVE_REGS is set, cs will be non zero */
44 	if (!fregs->regs.cs)
45 		return NULL;
46 	return &fregs->regs;
47 }
48 
49 #define ftrace_regs_set_instruction_pointer(fregs, _ip)	\
50 	do { (fregs)->regs.ip = (_ip); } while (0)
51 
52 #define ftrace_regs_get_instruction_pointer(fregs) \
53 	((fregs)->regs.ip)
54 
55 #define ftrace_regs_get_argument(fregs, n) \
56 	regs_get_kernel_argument(&(fregs)->regs, n)
57 #define ftrace_regs_get_stack_pointer(fregs) \
58 	kernel_stack_pointer(&(fregs)->regs)
59 #define ftrace_regs_return_value(fregs) \
60 	regs_return_value(&(fregs)->regs)
61 #define ftrace_regs_set_return_value(fregs, ret) \
62 	regs_set_return_value(&(fregs)->regs, ret)
63 #define ftrace_override_function_with_return(fregs) \
64 	override_function_with_return(&(fregs)->regs)
65 #define ftrace_regs_query_register_offset(name) \
66 	regs_query_register_offset(name)
67 
68 struct ftrace_ops;
69 #define ftrace_graph_func ftrace_graph_func
70 void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
71 		       struct ftrace_ops *op, struct ftrace_regs *fregs);
72 #else
73 #define FTRACE_GRAPH_TRAMP_ADDR FTRACE_GRAPH_ADDR
74 #endif
75 
76 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
77 /*
78  * When a ftrace registered caller is tracing a function that is
79  * also set by a register_ftrace_direct() call, it needs to be
80  * differentiated in the ftrace_caller trampoline. To do this, we
81  * place the direct caller in the ORIG_AX part of pt_regs. This
82  * tells the ftrace_caller that there's a direct caller.
83  */
84 static inline void
85 __arch_ftrace_set_direct_caller(struct pt_regs *regs, unsigned long addr)
86 {
87 	/* Emulate a call */
88 	regs->orig_ax = addr;
89 }
90 #define arch_ftrace_set_direct_caller(fregs, addr) \
91 	__arch_ftrace_set_direct_caller(&(fregs)->regs, addr)
92 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
93 
94 #ifdef CONFIG_DYNAMIC_FTRACE
95 
96 struct dyn_arch_ftrace {
97 	/* No extra data needed for x86 */
98 };
99 
100 #endif /*  CONFIG_DYNAMIC_FTRACE */
101 #endif /* __ASSEMBLY__ */
102 #endif /* CONFIG_FUNCTION_TRACER */
103 
104 
105 #ifndef __ASSEMBLY__
106 
107 void prepare_ftrace_return(unsigned long ip, unsigned long *parent,
108 			   unsigned long frame_pointer);
109 
110 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
111 extern void set_ftrace_ops_ro(void);
112 #else
113 static inline void set_ftrace_ops_ro(void) { }
114 #endif
115 
116 #define ARCH_HAS_SYSCALL_MATCH_SYM_NAME
117 static inline bool arch_syscall_match_sym_name(const char *sym, const char *name)
118 {
119 	/*
120 	 * Compare the symbol name with the system call name. Skip the
121 	 * "__x64_sys", "__ia32_sys", "__do_sys" or simple "sys" prefix.
122 	 */
123 	return !strcmp(sym + 3, name + 3) ||
124 		(!strncmp(sym, "__x64_", 6) && !strcmp(sym + 9, name + 3)) ||
125 		(!strncmp(sym, "__ia32_", 7) && !strcmp(sym + 10, name + 3)) ||
126 		(!strncmp(sym, "__do_sys", 8) && !strcmp(sym + 8, name + 3));
127 }
128 
129 #ifndef COMPILE_OFFSETS
130 
131 #if defined(CONFIG_FTRACE_SYSCALLS) && defined(CONFIG_IA32_EMULATION)
132 #include <linux/compat.h>
133 
134 /*
135  * Because ia32 syscalls do not map to x86_64 syscall numbers
136  * this screws up the trace output when tracing a ia32 task.
137  * Instead of reporting bogus syscalls, just do not trace them.
138  *
139  * If the user really wants these, then they should use the
140  * raw syscall tracepoints with filtering.
141  */
142 #define ARCH_TRACE_IGNORE_COMPAT_SYSCALLS 1
143 static inline bool arch_trace_is_compat_syscall(struct pt_regs *regs)
144 {
145 	return in_32bit_syscall();
146 }
147 #endif /* CONFIG_FTRACE_SYSCALLS && CONFIG_IA32_EMULATION */
148 #endif /* !COMPILE_OFFSETS */
149 #endif /* !__ASSEMBLY__ */
150 
151 #ifndef __ASSEMBLY__
152 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
153 struct fgraph_ret_regs {
154 	unsigned long ax;
155 	unsigned long dx;
156 	unsigned long bp;
157 };
158 
159 static inline unsigned long fgraph_ret_regs_return_value(struct fgraph_ret_regs *ret_regs)
160 {
161 	return ret_regs->ax;
162 }
163 
164 static inline unsigned long fgraph_ret_regs_frame_pointer(struct fgraph_ret_regs *ret_regs)
165 {
166 	return ret_regs->bp;
167 }
168 #endif /* ifdef CONFIG_FUNCTION_GRAPH_TRACER */
169 #endif
170 
171 #endif /* _ASM_X86_FTRACE_H */
172