1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2022 Loongson Technology Corporation Limited
4  */
5 
6 #ifndef _ASM_LOONGARCH_FTRACE_H
7 #define _ASM_LOONGARCH_FTRACE_H
8 
9 #define FTRACE_PLT_IDX		0
10 #define FTRACE_REGS_PLT_IDX	1
11 #define NR_FTRACE_PLTS		2
12 
13 #ifdef CONFIG_FUNCTION_TRACER
14 
15 #define MCOUNT_INSN_SIZE 4		/* sizeof mcount call */
16 
17 #ifndef __ASSEMBLY__
18 
19 #ifndef CONFIG_DYNAMIC_FTRACE
20 
21 #define mcount _mcount
22 extern void _mcount(void);
23 extern void prepare_ftrace_return(unsigned long self_addr, unsigned long callsite_sp, unsigned long old);
24 
25 #else
26 
27 struct dyn_ftrace;
28 struct dyn_arch_ftrace { };
29 
30 #define ARCH_SUPPORTS_FTRACE_OPS 1
31 #define HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
32 
33 #define ftrace_init_nop ftrace_init_nop
34 int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec);
35 
ftrace_call_adjust(unsigned long addr)36 static inline unsigned long ftrace_call_adjust(unsigned long addr)
37 {
38 	return addr;
39 }
40 
41 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent);
42 
43 #endif /* CONFIG_DYNAMIC_FTRACE */
44 
45 #ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
46 struct ftrace_ops;
47 
48 struct ftrace_regs {
49 	struct pt_regs regs;
50 };
51 
arch_ftrace_get_regs(struct ftrace_regs * fregs)52 static __always_inline struct pt_regs *arch_ftrace_get_regs(struct ftrace_regs *fregs)
53 {
54 	return &fregs->regs;
55 }
56 
57 static __always_inline unsigned long
ftrace_regs_get_instruction_pointer(struct ftrace_regs * fregs)58 ftrace_regs_get_instruction_pointer(struct ftrace_regs *fregs)
59 {
60 	return instruction_pointer(&fregs->regs);
61 }
62 
63 static __always_inline void
ftrace_regs_set_instruction_pointer(struct ftrace_regs * fregs,unsigned long ip)64 ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs, unsigned long ip)
65 {
66 	regs_set_return_value(&fregs->regs, ip);
67 }
68 
69 #define ftrace_regs_get_argument(fregs, n) \
70 	regs_get_kernel_argument(&(fregs)->regs, n)
71 #define ftrace_regs_get_stack_pointer(fregs) \
72 	kernel_stack_pointer(&(fregs)->regs)
73 #define ftrace_regs_return_value(fregs) \
74 	regs_return_value(&(fregs)->regs)
75 #define ftrace_regs_set_return_value(fregs, ret) \
76 	regs_set_return_value(&(fregs)->regs, ret)
77 #define ftrace_override_function_with_return(fregs) \
78 	override_function_with_return(&(fregs)->regs)
79 #define ftrace_regs_query_register_offset(name) \
80 	regs_query_register_offset(name)
81 
82 #define ftrace_graph_func ftrace_graph_func
83 void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
84 		       struct ftrace_ops *op, struct ftrace_regs *fregs);
85 
86 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
87 static inline void
__arch_ftrace_set_direct_caller(struct pt_regs * regs,unsigned long addr)88 __arch_ftrace_set_direct_caller(struct pt_regs *regs, unsigned long addr)
89 {
90 	regs->regs[13] = addr;	/* t1 */
91 }
92 
93 #define arch_ftrace_set_direct_caller(fregs, addr) \
94 	__arch_ftrace_set_direct_caller(&(fregs)->regs, addr)
95 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
96 
97 #endif
98 
99 #endif /* __ASSEMBLY__ */
100 
101 #endif /* CONFIG_FUNCTION_TRACER */
102 
103 #ifndef __ASSEMBLY__
104 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
105 struct fgraph_ret_regs {
106 	/* a0 - a1 */
107 	unsigned long regs[2];
108 
109 	unsigned long fp;
110 	unsigned long __unused;
111 };
112 
fgraph_ret_regs_return_value(struct fgraph_ret_regs * ret_regs)113 static inline unsigned long fgraph_ret_regs_return_value(struct fgraph_ret_regs *ret_regs)
114 {
115 	return ret_regs->regs[0];
116 }
117 
fgraph_ret_regs_frame_pointer(struct fgraph_ret_regs * ret_regs)118 static inline unsigned long fgraph_ret_regs_frame_pointer(struct fgraph_ret_regs *ret_regs)
119 {
120 	return ret_regs->fp;
121 }
122 #endif /* ifdef CONFIG_FUNCTION_GRAPH_TRACER */
123 #endif
124 
125 #endif /* _ASM_LOONGARCH_FTRACE_H */
126