1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
4 #include <linux/ftrace.h>
5 #include <linux/uaccess.h>
6 #include <asm/cacheflush.h>
8 #ifdef CONFIG_DYNAMIC_FTRACE
11 #define NOP32_HI 0xc400
12 #define NOP32_LO 0x4820
13 #define PUSH_LR 0x14d0
14 #define MOVIH_LINK 0xea3a
15 #define ORI_LINK 0xef5a
16 #define JSR_LINK 0xe8fa
17 #define BSR_LINK 0xe000
20 * Gcc-csky with -pg will insert stub in function prologue:
26 * If the (callee - current_pc) is less then 64MB, we'll use bsr:
31 * else we'll use (movih + ori + jsr):
37 * (r26 is our reserved link-reg)
40 static inline void make_jbsr(unsigned long callee
, unsigned long pc
,
41 uint16_t *call
, bool nolr
)
45 call
[0] = nolr
? NOP
: PUSH_LR
;
47 offset
= (long) callee
- (long) pc
;
49 if (unlikely(offset
< -67108864 || offset
> 67108864)) {
51 call
[2] = callee
>> 16;
53 call
[4] = callee
& 0xffff;
60 ((uint16_t)((unsigned long) offset
>> 16) & 0x3ff);
61 call
[2] = (uint16_t)((unsigned long) offset
& 0xffff);
62 call
[3] = call
[5] = NOP32_HI
;
63 call
[4] = call
[6] = NOP32_LO
;
67 static uint16_t nops
[7] = {NOP
, NOP32_HI
, NOP32_LO
, NOP32_HI
, NOP32_LO
,
69 static int ftrace_check_current_nop(unsigned long hook
)
72 unsigned long hook_pos
= hook
- 2;
74 if (probe_kernel_read((void *)olds
, (void *)hook_pos
, sizeof(nops
)))
77 if (memcmp((void *)nops
, (void *)olds
, sizeof(nops
))) {
78 pr_err("%p: nop but get (%04x %04x %04x %04x %04x %04x %04x)\n",
80 olds
[0], olds
[1], olds
[2], olds
[3], olds
[4], olds
[5],
89 static int ftrace_modify_code(unsigned long hook
, unsigned long target
,
90 bool enable
, bool nolr
)
94 unsigned long hook_pos
= hook
- 2;
97 make_jbsr(target
, hook
, call
, nolr
);
99 ret
= probe_kernel_write((void *)hook_pos
, enable
? call
: nops
,
104 flush_icache_range(hook_pos
, hook_pos
+ MCOUNT_INSN_SIZE
);
109 int ftrace_make_call(struct dyn_ftrace
*rec
, unsigned long addr
)
111 int ret
= ftrace_check_current_nop(rec
->ip
);
116 return ftrace_modify_code(rec
->ip
, addr
, true, false);
119 int ftrace_make_nop(struct module
*mod
, struct dyn_ftrace
*rec
,
122 return ftrace_modify_code(rec
->ip
, addr
, false, false);
125 int ftrace_update_ftrace_func(ftrace_func_t func
)
127 int ret
= ftrace_modify_code((unsigned long)&ftrace_call
,
128 (unsigned long)func
, true, true);
132 int __init
ftrace_dyn_arch_init(void)
136 #endif /* CONFIG_DYNAMIC_FTRACE */
138 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
139 void prepare_ftrace_return(unsigned long *parent
, unsigned long self_addr
,
140 unsigned long frame_pointer
)
142 unsigned long return_hooker
= (unsigned long)&return_to_handler
;
145 if (unlikely(atomic_read(¤t
->tracing_graph_pause
)))
150 if (!function_graph_enter(old
, self_addr
,
151 *(unsigned long *)frame_pointer
, parent
)) {
153 * For csky-gcc function has sub-call:
157 * st.w r15, (sp, 0x4)
160 * We only need set *parent for resume
162 * For csky-gcc function has no sub-call:
168 * We need set *parent and *(frame_pointer + 4) for resume,
169 * because lr is resumed twice.
171 *parent
= return_hooker
;
173 if (*(unsigned long *)frame_pointer
== old
)
174 *(unsigned long *)frame_pointer
= return_hooker
;
178 #ifdef CONFIG_DYNAMIC_FTRACE
179 int ftrace_enable_ftrace_graph_caller(void)
181 return ftrace_modify_code((unsigned long)&ftrace_graph_call
,
182 (unsigned long)&ftrace_graph_caller
, true, true);
185 int ftrace_disable_ftrace_graph_caller(void)
187 return ftrace_modify_code((unsigned long)&ftrace_graph_call
,
188 (unsigned long)&ftrace_graph_caller
, false, true);
190 #endif /* CONFIG_DYNAMIC_FTRACE */
191 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
193 /* _mcount is defined in abi's mcount.S */
194 EXPORT_SYMBOL(_mcount
);