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