xref: /linux/arch/arm64/include/asm/ftrace.h (revision f49040c7aaa5532a1f94355ef5073c49e6b32349)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * arch/arm64/include/asm/ftrace.h
4  *
5  * Copyright (C) 2013 Linaro Limited
6  * Author: AKASHI Takahiro <takahiro.akashi@linaro.org>
7  */
8 #ifndef __ASM_FTRACE_H
9 #define __ASM_FTRACE_H
10 
11 #include <asm/insn.h>
12 
13 #define HAVE_FUNCTION_GRAPH_FP_TEST
14 
15 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
16 #define ARCH_SUPPORTS_FTRACE_OPS 1
17 #else
18 #define MCOUNT_ADDR		((unsigned long)_mcount)
19 #endif
20 
21 /* The BL at the callsite's adjusted rec->ip */
22 #define MCOUNT_INSN_SIZE	AARCH64_INSN_SIZE
23 
24 #define FTRACE_PLT_IDX		0
25 #define NR_FTRACE_PLTS		1
26 
27 /*
28  * Currently, gcc tends to save the link register after the local variables
29  * on the stack. This causes the max stack tracer to report the function
30  * frame sizes for the wrong functions. By defining
31  * ARCH_FTRACE_SHIFT_STACK_TRACER, it will tell the stack tracer to expect
32  * to find the return address on the stack after the local variables have
33  * been set up.
34  *
35  * Note, this may change in the future, and we will need to deal with that
36  * if it were to happen.
37  */
38 #define ARCH_FTRACE_SHIFT_STACK_TRACER 1
39 
40 #ifndef __ASSEMBLY__
41 #include <linux/compat.h>
42 
43 extern void _mcount(unsigned long);
44 extern void *return_address(unsigned int);
45 
46 struct dyn_arch_ftrace {
47 	/* No extra data needed for arm64 */
48 };
49 
50 extern unsigned long ftrace_graph_call;
51 
52 extern void return_to_handler(void);
53 
54 unsigned long ftrace_call_adjust(unsigned long addr);
55 unsigned long arch_ftrace_get_symaddr(unsigned long fentry_ip);
56 #define ftrace_get_symaddr(fentry_ip) arch_ftrace_get_symaddr(fentry_ip)
57 
58 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
59 #define HAVE_ARCH_FTRACE_REGS
60 struct dyn_ftrace;
61 struct ftrace_ops;
62 struct ftrace_regs;
63 #define arch_ftrace_regs(fregs) ((struct __arch_ftrace_regs *)(fregs))
64 
65 #define arch_ftrace_get_regs(regs) NULL
66 
67 /*
68  * Note: sizeof(struct ftrace_regs) must be a multiple of 16 to ensure correct
69  * stack alignment
70  */
71 struct __arch_ftrace_regs {
72 	/* x0 - x8 */
73 	unsigned long regs[9];
74 
75 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
76 	unsigned long direct_tramp;
77 #else
78 	unsigned long __unused;
79 #endif
80 
81 	unsigned long fp;
82 	unsigned long lr;
83 
84 	unsigned long sp;
85 	unsigned long pc;
86 };
87 
88 static __always_inline unsigned long
ftrace_regs_get_instruction_pointer(const struct ftrace_regs * fregs)89 ftrace_regs_get_instruction_pointer(const struct ftrace_regs *fregs)
90 {
91 	return arch_ftrace_regs(fregs)->pc;
92 }
93 
94 static __always_inline void
ftrace_regs_set_instruction_pointer(struct ftrace_regs * fregs,unsigned long pc)95 ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs,
96 				    unsigned long pc)
97 {
98 	arch_ftrace_regs(fregs)->pc = pc;
99 }
100 
101 static __always_inline unsigned long
ftrace_regs_get_stack_pointer(const struct ftrace_regs * fregs)102 ftrace_regs_get_stack_pointer(const struct ftrace_regs *fregs)
103 {
104 	return arch_ftrace_regs(fregs)->sp;
105 }
106 
107 static __always_inline unsigned long
ftrace_regs_get_argument(struct ftrace_regs * fregs,unsigned int n)108 ftrace_regs_get_argument(struct ftrace_regs *fregs, unsigned int n)
109 {
110 	if (n < 8)
111 		return arch_ftrace_regs(fregs)->regs[n];
112 	return 0;
113 }
114 
115 static __always_inline unsigned long
ftrace_regs_get_return_value(const struct ftrace_regs * fregs)116 ftrace_regs_get_return_value(const struct ftrace_regs *fregs)
117 {
118 	return arch_ftrace_regs(fregs)->regs[0];
119 }
120 
121 static __always_inline void
ftrace_regs_set_return_value(struct ftrace_regs * fregs,unsigned long ret)122 ftrace_regs_set_return_value(struct ftrace_regs *fregs,
123 			     unsigned long ret)
124 {
125 	arch_ftrace_regs(fregs)->regs[0] = ret;
126 }
127 
128 static __always_inline void
ftrace_override_function_with_return(struct ftrace_regs * fregs)129 ftrace_override_function_with_return(struct ftrace_regs *fregs)
130 {
131 	arch_ftrace_regs(fregs)->pc = arch_ftrace_regs(fregs)->lr;
132 }
133 
134 static __always_inline unsigned long
ftrace_regs_get_frame_pointer(const struct ftrace_regs * fregs)135 ftrace_regs_get_frame_pointer(const struct ftrace_regs *fregs)
136 {
137 	return arch_ftrace_regs(fregs)->fp;
138 }
139 
140 static __always_inline unsigned long
ftrace_regs_get_return_address(const struct ftrace_regs * fregs)141 ftrace_regs_get_return_address(const struct ftrace_regs *fregs)
142 {
143 	return arch_ftrace_regs(fregs)->lr;
144 }
145 
146 static __always_inline struct pt_regs *
ftrace_partial_regs(const struct ftrace_regs * fregs,struct pt_regs * regs)147 ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)
148 {
149 	struct __arch_ftrace_regs *afregs = arch_ftrace_regs(fregs);
150 
151 	memcpy(regs->regs, afregs->regs, sizeof(afregs->regs));
152 	regs->sp = afregs->sp;
153 	regs->pc = afregs->pc;
154 	regs->regs[29] = afregs->fp;
155 	regs->regs[30] = afregs->lr;
156 	return regs;
157 }
158 
159 #define arch_ftrace_fill_perf_regs(fregs, _regs) do {		\
160 		(_regs)->pc = arch_ftrace_regs(fregs)->pc;			\
161 		(_regs)->regs[29] = arch_ftrace_regs(fregs)->fp;		\
162 		(_regs)->sp = arch_ftrace_regs(fregs)->sp;			\
163 		(_regs)->pstate = PSR_MODE_EL1h;		\
164 	} while (0)
165 
166 int ftrace_regs_query_register_offset(const char *name);
167 
168 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec);
169 #define ftrace_init_nop ftrace_init_nop
170 
171 void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
172 		       struct ftrace_ops *op, struct ftrace_regs *fregs);
173 #define ftrace_graph_func ftrace_graph_func
174 
175 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
arch_ftrace_set_direct_caller(struct ftrace_regs * fregs,unsigned long addr)176 static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs,
177 						 unsigned long addr)
178 {
179 	/*
180 	 * The ftrace trampoline will return to this address instead of the
181 	 * instrumented function.
182 	 */
183 	arch_ftrace_regs(fregs)->direct_tramp = addr;
184 }
185 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
186 
187 #endif
188 
189 #define ftrace_return_address(n) return_address(n)
190 
191 /*
192  * Because AArch32 mode does not share the same syscall table with AArch64,
193  * tracing compat syscalls may result in reporting bogus syscalls or even
194  * hang-up, so just do not trace them.
195  * See kernel/trace/trace_syscalls.c
196  *
197  * x86 code says:
198  * If the user really wants these, then they should use the
199  * raw syscall tracepoints with filtering.
200  */
201 #define ARCH_TRACE_IGNORE_COMPAT_SYSCALLS
arch_trace_is_compat_syscall(struct pt_regs * regs)202 static inline bool arch_trace_is_compat_syscall(struct pt_regs *regs)
203 {
204 	return is_compat_task();
205 }
206 
207 #define ARCH_HAS_SYSCALL_MATCH_SYM_NAME
208 
arch_syscall_match_sym_name(const char * sym,const char * name)209 static inline bool arch_syscall_match_sym_name(const char *sym,
210 					       const char *name)
211 {
212 	/*
213 	 * Since all syscall functions have __arm64_ prefix, we must skip it.
214 	 * However, as we described above, we decided to ignore compat
215 	 * syscalls, so we don't care about __arm64_compat_ prefix here.
216 	 */
217 	return !strcmp(sym + 8, name);
218 }
219 #endif /* ifndef __ASSEMBLY__ */
220 
221 #ifndef __ASSEMBLY__
222 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
223 
224 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
225 			   unsigned long frame_pointer);
226 
227 #endif /* ifdef CONFIG_FUNCTION_GRAPH_TRACER  */
228 #endif
229 
230 #endif /* __ASM_FTRACE_H */
231