xref: /openbmc/linux/arch/riscv/include/asm/ftrace.h (revision 6db6b729)
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 #define HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
15 
16 /*
17  * Clang prior to 13 had "mcount" instead of "_mcount":
18  * https://reviews.llvm.org/D98881
19  */
20 #if defined(CONFIG_CC_IS_GCC) || CONFIG_CLANG_VERSION >= 130000
21 #define MCOUNT_NAME _mcount
22 #else
23 #define MCOUNT_NAME mcount
24 #endif
25 
26 #define ARCH_SUPPORTS_FTRACE_OPS 1
27 #ifndef __ASSEMBLY__
28 void MCOUNT_NAME(void);
29 static inline unsigned long ftrace_call_adjust(unsigned long addr)
30 {
31 	return addr;
32 }
33 
34 /*
35  * Let's do like x86/arm64 and ignore the compat syscalls.
36  */
37 #define ARCH_TRACE_IGNORE_COMPAT_SYSCALLS
38 static inline bool arch_trace_is_compat_syscall(struct pt_regs *regs)
39 {
40 	return is_compat_task();
41 }
42 
43 #define ARCH_HAS_SYSCALL_MATCH_SYM_NAME
44 static inline bool arch_syscall_match_sym_name(const char *sym,
45 					       const char *name)
46 {
47 	/*
48 	 * Since all syscall functions have __riscv_ prefix, we must skip it.
49 	 * However, as we described above, we decided to ignore compat
50 	 * syscalls, so we don't care about __riscv_compat_ prefix here.
51 	 */
52 	return !strcmp(sym + 8, name);
53 }
54 
55 struct dyn_arch_ftrace {
56 };
57 #endif
58 
59 #ifdef CONFIG_DYNAMIC_FTRACE
60 /*
61  * A general call in RISC-V is a pair of insts:
62  * 1) auipc: setting high-20 pc-related bits to ra register
63  * 2) jalr: setting low-12 offset to ra, jump to ra, and set ra to
64  *          return address (original pc + 4)
65  *
66  *<ftrace enable>:
67  * 0: auipc  t0/ra, 0x?
68  * 4: jalr   t0/ra, ?(t0/ra)
69  *
70  *<ftrace disable>:
71  * 0: nop
72  * 4: nop
73  *
74  * Dynamic ftrace generates probes to call sites, so we must deal with
75  * both auipc and jalr at the same time.
76  */
77 
78 #define MCOUNT_ADDR		((unsigned long)MCOUNT_NAME)
79 #define JALR_SIGN_MASK		(0x00000800)
80 #define JALR_OFFSET_MASK	(0x00000fff)
81 #define AUIPC_OFFSET_MASK	(0xfffff000)
82 #define AUIPC_PAD		(0x00001000)
83 #define JALR_SHIFT		20
84 #define JALR_RA			(0x000080e7)
85 #define AUIPC_RA		(0x00000097)
86 #define JALR_T0			(0x000282e7)
87 #define AUIPC_T0		(0x00000297)
88 #define NOP4			(0x00000013)
89 
90 #define to_jalr_t0(offset)						\
91 	(((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_T0)
92 
93 #define to_auipc_t0(offset)						\
94 	((offset & JALR_SIGN_MASK) ?					\
95 	(((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_T0) :	\
96 	((offset & AUIPC_OFFSET_MASK) | AUIPC_T0))
97 
98 #define make_call_t0(caller, callee, call)				\
99 do {									\
100 	unsigned int offset =						\
101 		(unsigned long) callee - (unsigned long) caller;	\
102 	call[0] = to_auipc_t0(offset);					\
103 	call[1] = to_jalr_t0(offset);					\
104 } while (0)
105 
106 #define to_jalr_ra(offset)						\
107 	(((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_RA)
108 
109 #define to_auipc_ra(offset)						\
110 	((offset & JALR_SIGN_MASK) ?					\
111 	(((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_RA) :	\
112 	((offset & AUIPC_OFFSET_MASK) | AUIPC_RA))
113 
114 #define make_call_ra(caller, callee, call)				\
115 do {									\
116 	unsigned int offset =						\
117 		(unsigned long) callee - (unsigned long) caller;	\
118 	call[0] = to_auipc_ra(offset);					\
119 	call[1] = to_jalr_ra(offset);					\
120 } while (0)
121 
122 /*
123  * Let auipc+jalr be the basic *mcount unit*, so we make it 8 bytes here.
124  */
125 #define MCOUNT_INSN_SIZE 8
126 
127 #ifndef __ASSEMBLY__
128 struct dyn_ftrace;
129 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec);
130 #define ftrace_init_nop ftrace_init_nop
131 #endif
132 
133 #endif /* CONFIG_DYNAMIC_FTRACE */
134 
135 #ifndef __ASSEMBLY__
136 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
137 struct fgraph_ret_regs {
138 	unsigned long a1;
139 	unsigned long a0;
140 	unsigned long s0;
141 	unsigned long ra;
142 };
143 
144 static inline unsigned long fgraph_ret_regs_return_value(struct fgraph_ret_regs *ret_regs)
145 {
146 	return ret_regs->a0;
147 }
148 
149 static inline unsigned long fgraph_ret_regs_frame_pointer(struct fgraph_ret_regs *ret_regs)
150 {
151 	return ret_regs->s0;
152 }
153 #endif /* ifdef CONFIG_FUNCTION_GRAPH_TRACER */
154 #endif
155 
156 #endif /* _ASM_RISCV_FTRACE_H */
157