Loading...
1/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright (C) 2017 Andes Technology Corporation */
3
4#include <linux/init.h>
5#include <linux/linkage.h>
6#include <asm/asm.h>
7#include <asm/csr.h>
8#include <asm/unistd.h>
9#include <asm/thread_info.h>
10#include <asm/asm-offsets.h>
11#include <asm-generic/export.h>
12#include <asm/ftrace.h>
13
14 .text
15
16#define FENTRY_RA_OFFSET 12
17#define ABI_SIZE_ON_STACK 72
18#define ABI_A0 0
19#define ABI_A1 8
20#define ABI_A2 16
21#define ABI_A3 24
22#define ABI_A4 32
23#define ABI_A5 40
24#define ABI_A6 48
25#define ABI_A7 56
26#define ABI_RA 64
27
28 .macro SAVE_ABI
29 addi sp, sp, -SZREG
30 addi sp, sp, -ABI_SIZE_ON_STACK
31
32 REG_S a0, ABI_A0(sp)
33 REG_S a1, ABI_A1(sp)
34 REG_S a2, ABI_A2(sp)
35 REG_S a3, ABI_A3(sp)
36 REG_S a4, ABI_A4(sp)
37 REG_S a5, ABI_A5(sp)
38 REG_S a6, ABI_A6(sp)
39 REG_S a7, ABI_A7(sp)
40 REG_S ra, ABI_RA(sp)
41 .endm
42
43 .macro RESTORE_ABI
44 REG_L a0, ABI_A0(sp)
45 REG_L a1, ABI_A1(sp)
46 REG_L a2, ABI_A2(sp)
47 REG_L a3, ABI_A3(sp)
48 REG_L a4, ABI_A4(sp)
49 REG_L a5, ABI_A5(sp)
50 REG_L a6, ABI_A6(sp)
51 REG_L a7, ABI_A7(sp)
52 REG_L ra, ABI_RA(sp)
53
54 addi sp, sp, ABI_SIZE_ON_STACK
55 addi sp, sp, SZREG
56 .endm
57
58#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
59 .macro SAVE_ALL
60 addi sp, sp, -SZREG
61 addi sp, sp, -PT_SIZE_ON_STACK
62
63 REG_S x1, PT_EPC(sp)
64 addi sp, sp, PT_SIZE_ON_STACK
65 REG_L x1, (sp)
66 addi sp, sp, -PT_SIZE_ON_STACK
67 REG_S x1, PT_RA(sp)
68 REG_L x1, PT_EPC(sp)
69
70 REG_S x2, PT_SP(sp)
71 REG_S x3, PT_GP(sp)
72 REG_S x4, PT_TP(sp)
73 REG_S x5, PT_T0(sp)
74 REG_S x6, PT_T1(sp)
75 REG_S x7, PT_T2(sp)
76 REG_S x8, PT_S0(sp)
77 REG_S x9, PT_S1(sp)
78 REG_S x10, PT_A0(sp)
79 REG_S x11, PT_A1(sp)
80 REG_S x12, PT_A2(sp)
81 REG_S x13, PT_A3(sp)
82 REG_S x14, PT_A4(sp)
83 REG_S x15, PT_A5(sp)
84 REG_S x16, PT_A6(sp)
85 REG_S x17, PT_A7(sp)
86 REG_S x18, PT_S2(sp)
87 REG_S x19, PT_S3(sp)
88 REG_S x20, PT_S4(sp)
89 REG_S x21, PT_S5(sp)
90 REG_S x22, PT_S6(sp)
91 REG_S x23, PT_S7(sp)
92 REG_S x24, PT_S8(sp)
93 REG_S x25, PT_S9(sp)
94 REG_S x26, PT_S10(sp)
95 REG_S x27, PT_S11(sp)
96 REG_S x28, PT_T3(sp)
97 REG_S x29, PT_T4(sp)
98 REG_S x30, PT_T5(sp)
99 REG_S x31, PT_T6(sp)
100 .endm
101
102 .macro RESTORE_ALL
103 REG_L x1, PT_RA(sp)
104 addi sp, sp, PT_SIZE_ON_STACK
105 REG_S x1, (sp)
106 addi sp, sp, -PT_SIZE_ON_STACK
107 REG_L x1, PT_EPC(sp)
108 REG_L x2, PT_SP(sp)
109 REG_L x3, PT_GP(sp)
110 REG_L x4, PT_TP(sp)
111 REG_L x5, PT_T0(sp)
112 REG_L x6, PT_T1(sp)
113 REG_L x7, PT_T2(sp)
114 REG_L x8, PT_S0(sp)
115 REG_L x9, PT_S1(sp)
116 REG_L x10, PT_A0(sp)
117 REG_L x11, PT_A1(sp)
118 REG_L x12, PT_A2(sp)
119 REG_L x13, PT_A3(sp)
120 REG_L x14, PT_A4(sp)
121 REG_L x15, PT_A5(sp)
122 REG_L x16, PT_A6(sp)
123 REG_L x17, PT_A7(sp)
124 REG_L x18, PT_S2(sp)
125 REG_L x19, PT_S3(sp)
126 REG_L x20, PT_S4(sp)
127 REG_L x21, PT_S5(sp)
128 REG_L x22, PT_S6(sp)
129 REG_L x23, PT_S7(sp)
130 REG_L x24, PT_S8(sp)
131 REG_L x25, PT_S9(sp)
132 REG_L x26, PT_S10(sp)
133 REG_L x27, PT_S11(sp)
134 REG_L x28, PT_T3(sp)
135 REG_L x29, PT_T4(sp)
136 REG_L x30, PT_T5(sp)
137 REG_L x31, PT_T6(sp)
138
139 addi sp, sp, PT_SIZE_ON_STACK
140 addi sp, sp, SZREG
141 .endm
142#endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */
143
144ENTRY(ftrace_caller)
145 SAVE_ABI
146
147 addi a0, ra, -FENTRY_RA_OFFSET
148 la a1, function_trace_op
149 REG_L a2, 0(a1)
150 REG_L a1, ABI_SIZE_ON_STACK(sp)
151 mv a3, sp
152
153ftrace_call:
154 .global ftrace_call
155 call ftrace_stub
156
157#ifdef CONFIG_FUNCTION_GRAPH_TRACER
158 addi a0, sp, ABI_SIZE_ON_STACK
159 REG_L a1, ABI_RA(sp)
160 addi a1, a1, -FENTRY_RA_OFFSET
161#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
162 mv a2, s0
163#endif
164ftrace_graph_call:
165 .global ftrace_graph_call
166 call ftrace_stub
167#endif
168 RESTORE_ABI
169 ret
170ENDPROC(ftrace_caller)
171
172#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
173ENTRY(ftrace_regs_caller)
174 SAVE_ALL
175
176 addi a0, ra, -FENTRY_RA_OFFSET
177 la a1, function_trace_op
178 REG_L a2, 0(a1)
179 REG_L a1, PT_SIZE_ON_STACK(sp)
180 mv a3, sp
181
182ftrace_regs_call:
183 .global ftrace_regs_call
184 call ftrace_stub
185
186#ifdef CONFIG_FUNCTION_GRAPH_TRACER
187 addi a0, sp, PT_RA
188 REG_L a1, PT_EPC(sp)
189 addi a1, a1, -FENTRY_RA_OFFSET
190#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
191 mv a2, s0
192#endif
193ftrace_graph_regs_call:
194 .global ftrace_graph_regs_call
195 call ftrace_stub
196#endif
197
198 RESTORE_ALL
199 ret
200ENDPROC(ftrace_regs_caller)
201#endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */
1/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright (C) 2017 Andes Technology Corporation */
3
4#include <linux/init.h>
5#include <linux/linkage.h>
6#include <linux/export.h>
7#include <asm/asm.h>
8#include <asm/csr.h>
9#include <asm/unistd.h>
10#include <asm/thread_info.h>
11#include <asm/asm-offsets.h>
12#include <asm/ftrace.h>
13
14 .text
15
16#define FENTRY_RA_OFFSET 8
17#define ABI_SIZE_ON_STACK 80
18#define ABI_A0 0
19#define ABI_A1 8
20#define ABI_A2 16
21#define ABI_A3 24
22#define ABI_A4 32
23#define ABI_A5 40
24#define ABI_A6 48
25#define ABI_A7 56
26#define ABI_T0 64
27#define ABI_RA 72
28
29 .macro SAVE_ABI
30 addi sp, sp, -ABI_SIZE_ON_STACK
31
32 REG_S a0, ABI_A0(sp)
33 REG_S a1, ABI_A1(sp)
34 REG_S a2, ABI_A2(sp)
35 REG_S a3, ABI_A3(sp)
36 REG_S a4, ABI_A4(sp)
37 REG_S a5, ABI_A5(sp)
38 REG_S a6, ABI_A6(sp)
39 REG_S a7, ABI_A7(sp)
40 REG_S t0, ABI_T0(sp)
41 REG_S ra, ABI_RA(sp)
42 .endm
43
44 .macro RESTORE_ABI
45 REG_L a0, ABI_A0(sp)
46 REG_L a1, ABI_A1(sp)
47 REG_L a2, ABI_A2(sp)
48 REG_L a3, ABI_A3(sp)
49 REG_L a4, ABI_A4(sp)
50 REG_L a5, ABI_A5(sp)
51 REG_L a6, ABI_A6(sp)
52 REG_L a7, ABI_A7(sp)
53 REG_L t0, ABI_T0(sp)
54 REG_L ra, ABI_RA(sp)
55
56 addi sp, sp, ABI_SIZE_ON_STACK
57 .endm
58
59#ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
60
61/**
62* SAVE_ABI_REGS - save regs against the ftrace_regs struct
63*
64* After the stack is established,
65*
66* 0(sp) stores the PC of the traced function which can be accessed
67* by &(fregs)->epc in tracing function. Note that the real
68* function entry address should be computed with -FENTRY_RA_OFFSET.
69*
70* 8(sp) stores the function return address (i.e. parent IP) that
71* can be accessed by &(fregs)->ra in tracing function.
72*
73* The other regs are saved at the respective localtion and accessed
74* by the respective ftrace_regs member.
75*
76* Here is the layout of stack for your reference.
77*
78* PT_SIZE_ON_STACK -> +++++++++
79* + ..... +
80* + a0-a7 + --++++-> ftrace_caller saved
81* + t1 + --++++-> direct tramp address
82* + s0 + --+ // frame pointer
83* + sp + +
84* + ra + --+ // parent IP
85* sp -> + epc + --+ // PC
86* +++++++++
87**/
88 .macro SAVE_ABI_REGS
89 mv t4, sp // Save original SP in T4
90 addi sp, sp, -FREGS_SIZE_ON_STACK
91
92 REG_S t0, FREGS_EPC(sp)
93 REG_S x1, FREGS_RA(sp)
94 REG_S t4, FREGS_SP(sp) // Put original SP on stack
95#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
96 REG_S x8, FREGS_S0(sp)
97#endif
98 REG_S x6, FREGS_T1(sp)
99
100 // save the arguments
101 REG_S x10, FREGS_A0(sp)
102 REG_S x11, FREGS_A1(sp)
103 REG_S x12, FREGS_A2(sp)
104 REG_S x13, FREGS_A3(sp)
105 REG_S x14, FREGS_A4(sp)
106 REG_S x15, FREGS_A5(sp)
107 REG_S x16, FREGS_A6(sp)
108 REG_S x17, FREGS_A7(sp)
109 .endm
110
111 .macro RESTORE_ABI_REGS, all=0
112 REG_L t0, FREGS_EPC(sp)
113 REG_L x1, FREGS_RA(sp)
114#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
115 REG_L x8, FREGS_S0(sp)
116#endif
117 REG_L x6, FREGS_T1(sp)
118
119 // restore the arguments
120 REG_L x10, FREGS_A0(sp)
121 REG_L x11, FREGS_A1(sp)
122 REG_L x12, FREGS_A2(sp)
123 REG_L x13, FREGS_A3(sp)
124 REG_L x14, FREGS_A4(sp)
125 REG_L x15, FREGS_A5(sp)
126 REG_L x16, FREGS_A6(sp)
127 REG_L x17, FREGS_A7(sp)
128
129 addi sp, sp, FREGS_SIZE_ON_STACK
130 .endm
131
132 .macro PREPARE_ARGS
133 addi a0, t0, -FENTRY_RA_OFFSET
134 la a1, function_trace_op
135 REG_L a2, 0(a1)
136 mv a1, ra
137 mv a3, sp
138 .endm
139
140#endif /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */
141
142#ifndef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
143SYM_FUNC_START(ftrace_caller)
144 SAVE_ABI
145
146 addi a0, t0, -FENTRY_RA_OFFSET
147 la a1, function_trace_op
148 REG_L a2, 0(a1)
149 mv a1, ra
150 mv a3, sp
151
152SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
153 call ftrace_stub
154
155#ifdef CONFIG_FUNCTION_GRAPH_TRACER
156 addi a0, sp, ABI_RA
157 REG_L a1, ABI_T0(sp)
158 addi a1, a1, -FENTRY_RA_OFFSET
159#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
160 mv a2, s0
161#endif
162SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL)
163 call ftrace_stub
164#endif
165 RESTORE_ABI
166 jr t0
167SYM_FUNC_END(ftrace_caller)
168
169#else /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */
170SYM_FUNC_START(ftrace_caller)
171 mv t1, zero
172 SAVE_ABI_REGS
173 PREPARE_ARGS
174
175SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
176 call ftrace_stub
177
178 RESTORE_ABI_REGS
179 bnez t1, .Ldirect
180 jr t0
181.Ldirect:
182 jr t1
183SYM_FUNC_END(ftrace_caller)
184
185#endif /* CONFIG_DYNAMIC_FTRACE_WITH_ARGS */
186
187#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
188SYM_CODE_START(ftrace_stub_direct_tramp)
189 jr t0
190SYM_CODE_END(ftrace_stub_direct_tramp)
191#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */