1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2017 Andes Technology Corporation */
3 
4 #ifndef _ASM_RISCV_FTRACE_H
5 #define _ASM_RISCV_FTRACE_H
6 
7 /*
8  * The graph frame test is not possible if CONFIG_FRAME_POINTER is not enabled.
9  * Check arch/riscv/kernel/mcount.S for detail.
10  */
11 #if defined(CONFIG_FUNCTION_GRAPH_TRACER) && defined(CONFIG_FRAME_POINTER)
12 #define HAVE_FUNCTION_GRAPH_FP_TEST
13 #endif
14 
15 #define ARCH_SUPPORTS_FTRACE_OPS 1
16 #ifndef __ASSEMBLY__
17 
18 extern void *return_address(unsigned int level);
19 
20 #define ftrace_return_address(n) return_address(n)
21 
22 void _mcount(void);
ftrace_call_adjust(unsigned long addr)23 static inline unsigned long ftrace_call_adjust(unsigned long addr)
24 {
25 	return addr;
26 }
27 
28 /*
29  * Let's do like x86/arm64 and ignore the compat syscalls.
30  */
31 #define ARCH_TRACE_IGNORE_COMPAT_SYSCALLS
arch_trace_is_compat_syscall(struct pt_regs * regs)32 static inline bool arch_trace_is_compat_syscall(struct pt_regs *regs)
33 {
34 	return is_compat_task();
35 }
36 
37 #define ARCH_HAS_SYSCALL_MATCH_SYM_NAME
arch_syscall_match_sym_name(const char * sym,const char * name)38 static inline bool arch_syscall_match_sym_name(const char *sym,
39 					       const char *name)
40 {
41 	/*
42 	 * Since all syscall functions have __riscv_ prefix, we must skip it.
43 	 * However, as we described above, we decided to ignore compat
44 	 * syscalls, so we don't care about __riscv_compat_ prefix here.
45 	 */
46 	return !strcmp(sym + 8, name);
47 }
48 
49 struct dyn_arch_ftrace {
50 };
51 #endif
52 
53 #ifdef CONFIG_DYNAMIC_FTRACE
54 /*
55  * A general call in RISC-V is a pair of insts:
56  * 1) auipc: setting high-20 pc-related bits to ra register
57  * 2) jalr: setting low-12 offset to ra, jump to ra, and set ra to
58  *          return address (original pc + 4)
59  *
60  *<ftrace enable>:
61  * 0: auipc  t0/ra, 0x?
62  * 4: jalr   t0/ra, ?(t0/ra)
63  *
64  *<ftrace disable>:
65  * 0: nop
66  * 4: nop
67  *
68  * Dynamic ftrace generates probes to call sites, so we must deal with
69  * both auipc and jalr at the same time.
70  */
71 
72 #define MCOUNT_ADDR		((unsigned long)_mcount)
73 #define JALR_SIGN_MASK		(0x00000800)
74 #define JALR_OFFSET_MASK	(0x00000fff)
75 #define AUIPC_OFFSET_MASK	(0xfffff000)
76 #define AUIPC_PAD		(0x00001000)
77 #define JALR_SHIFT		20
78 #define JALR_RA			(0x000080e7)
79 #define AUIPC_RA		(0x00000097)
80 #define JALR_T0			(0x000282e7)
81 #define AUIPC_T0		(0x00000297)
82 
83 #define to_jalr_t0(offset)						\
84 	(((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_T0)
85 
86 #define to_auipc_t0(offset)						\
87 	((offset & JALR_SIGN_MASK) ?					\
88 	(((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_T0) :	\
89 	((offset & AUIPC_OFFSET_MASK) | AUIPC_T0))
90 
91 #define make_call_t0(caller, callee, call)				\
92 do {									\
93 	unsigned int offset =						\
94 		(unsigned long) (callee) - (unsigned long) (caller);	\
95 	call[0] = to_auipc_t0(offset);					\
96 	call[1] = to_jalr_t0(offset);					\
97 } while (0)
98 
99 #define to_jalr_ra(offset)						\
100 	(((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_RA)
101 
102 #define to_auipc_ra(offset)						\
103 	((offset & JALR_SIGN_MASK) ?					\
104 	(((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_RA) :	\
105 	((offset & AUIPC_OFFSET_MASK) | AUIPC_RA))
106 
107 #define make_call_ra(caller, callee, call)				\
108 do {									\
109 	unsigned int offset =						\
110 		(unsigned long) (callee) - (unsigned long) (caller);	\
111 	call[0] = to_auipc_ra(offset);					\
112 	call[1] = to_jalr_ra(offset);					\
113 } while (0)
114 
115 /*
116  * Let auipc+jalr be the basic *mcount unit*, so we make it 8 bytes here.
117  */
118 #define MCOUNT_INSN_SIZE 8
119 
120 #ifndef __ASSEMBLY__
121 struct dyn_ftrace;
122 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec);
123 #define ftrace_init_nop ftrace_init_nop
124 
125 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
126 #define arch_ftrace_get_regs(regs) NULL
127 #define HAVE_ARCH_FTRACE_REGS
128 struct ftrace_ops;
129 struct ftrace_regs;
130 #define arch_ftrace_regs(fregs) ((struct __arch_ftrace_regs *)(fregs))
131 
132 struct __arch_ftrace_regs {
133 	unsigned long epc;
134 	unsigned long ra;
135 	unsigned long sp;
136 	unsigned long s0;
137 	unsigned long t1;
138 	union {
139 		unsigned long args[8];
140 		struct {
141 			unsigned long a0;
142 			unsigned long a1;
143 			unsigned long a2;
144 			unsigned long a3;
145 			unsigned long a4;
146 			unsigned long a5;
147 			unsigned long a6;
148 			unsigned long a7;
149 		};
150 	};
151 };
152 
ftrace_regs_get_instruction_pointer(const struct ftrace_regs * fregs)153 static __always_inline unsigned long ftrace_regs_get_instruction_pointer(const struct ftrace_regs
154 									 *fregs)
155 {
156 	return arch_ftrace_regs(fregs)->epc;
157 }
158 
ftrace_regs_set_instruction_pointer(struct ftrace_regs * fregs,unsigned long pc)159 static __always_inline void ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs,
160 								unsigned long pc)
161 {
162 	arch_ftrace_regs(fregs)->epc = pc;
163 }
164 
ftrace_regs_get_stack_pointer(const struct ftrace_regs * fregs)165 static __always_inline unsigned long ftrace_regs_get_stack_pointer(const struct ftrace_regs *fregs)
166 {
167 	return arch_ftrace_regs(fregs)->sp;
168 }
169 
ftrace_regs_get_frame_pointer(const struct ftrace_regs * fregs)170 static __always_inline unsigned long ftrace_regs_get_frame_pointer(const struct ftrace_regs *fregs)
171 {
172 	return arch_ftrace_regs(fregs)->s0;
173 }
174 
ftrace_regs_get_argument(struct ftrace_regs * fregs,unsigned int n)175 static __always_inline unsigned long ftrace_regs_get_argument(struct ftrace_regs *fregs,
176 							      unsigned int n)
177 {
178 	if (n < 8)
179 		return arch_ftrace_regs(fregs)->args[n];
180 	return 0;
181 }
182 
ftrace_regs_get_return_value(const struct ftrace_regs * fregs)183 static __always_inline unsigned long ftrace_regs_get_return_value(const struct ftrace_regs *fregs)
184 {
185 	return arch_ftrace_regs(fregs)->a0;
186 }
187 
ftrace_regs_get_return_address(const struct ftrace_regs * fregs)188 static __always_inline unsigned long ftrace_regs_get_return_address(const struct ftrace_regs *fregs)
189 {
190 	return arch_ftrace_regs(fregs)->ra;
191 }
192 
ftrace_regs_set_return_value(struct ftrace_regs * fregs,unsigned long ret)193 static __always_inline void ftrace_regs_set_return_value(struct ftrace_regs *fregs,
194 							 unsigned long ret)
195 {
196 	arch_ftrace_regs(fregs)->a0 = ret;
197 }
198 
ftrace_override_function_with_return(struct ftrace_regs * fregs)199 static __always_inline void ftrace_override_function_with_return(struct ftrace_regs *fregs)
200 {
201 	arch_ftrace_regs(fregs)->epc = arch_ftrace_regs(fregs)->ra;
202 }
203 
204 static __always_inline struct pt_regs *
ftrace_partial_regs(const struct ftrace_regs * fregs,struct pt_regs * regs)205 ftrace_partial_regs(const struct ftrace_regs *fregs, struct pt_regs *regs)
206 {
207 	struct __arch_ftrace_regs *afregs = arch_ftrace_regs(fregs);
208 
209 	memcpy(&regs->a_regs, afregs->args, sizeof(afregs->args));
210 	regs->epc = afregs->epc;
211 	regs->ra = afregs->ra;
212 	regs->sp = afregs->sp;
213 	regs->s0 = afregs->s0;
214 	regs->t1 = afregs->t1;
215 	return regs;
216 }
217 
218 int ftrace_regs_query_register_offset(const char *name);
219 
220 void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
221 		       struct ftrace_ops *op, struct ftrace_regs *fregs);
222 #define ftrace_graph_func ftrace_graph_func
223 
arch_ftrace_set_direct_caller(struct ftrace_regs * fregs,unsigned long addr)224 static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs, unsigned long addr)
225 {
226 	arch_ftrace_regs(fregs)->t1 = addr;
227 }
228 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */
229 
230 #endif /* __ASSEMBLY__ */
231 
232 #endif /* CONFIG_DYNAMIC_FTRACE */
233 
234 #endif /* _ASM_RISCV_FTRACE_H */
235