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