1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2022 Loongson Technology Corporation Limited
6 #include <asm/ftrace.h>
7 #include <asm/regdef.h>
8 #include <asm/stackframe.h>
12 * Due to -fpatchable-function-entry=2: the compiler inserted 2 NOPs before the
13 * regular C function prologue. When PC arrived here, the last 2 instructions
16 * bl callsite (for modules, callsite is a tramplione)
18 * modules trampoline is as follows:
19 * lu12i.w t1, callsite[31:12]
20 * lu32i.d t1, callsite[51:32]
21 * lu52i.d t1, t1, callsite[63:52]
22 * jirl zero, t1, callsite[11:0] >> 2
24 * See arch/loongarch/kernel/ftrace_dyn.c for details. Here, pay attention to
25 * that the T series regs are available and safe because each C functions
26 * follows the LoongArch's psABI as well.
29 .macro ftrace_regs_entry allregs=0
30 PTR_ADDI sp, sp, -PT_SIZE
31 PTR_S t0, sp, PT_R1 /* Save parent ra at PT_R1(RA) */
61 /* Clear it for later use as a flag sometimes. */
64 PTR_S ra, sp, PT_ERA /* Save trace function ra at PT_ERA */
67 PTR_ADDI t8, sp, PT_SIZE
71 SYM_FUNC_START(ftrace_stub)
73 SYM_FUNC_END(ftrace_stub)
75 SYM_CODE_START(ftrace_common)
77 PTR_ADDI a0, ra, -8 /* arg0: ip */
78 move a1, t0 /* arg1: parent_ip */
79 la.pcrel t1, function_trace_op
80 PTR_L a2, t1, 0 /* arg2: op */
81 move a3, sp /* arg3: regs */
83 SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
85 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
86 SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL)
87 nop /* b ftrace_graph_caller */
91 * As we didn't use S series regs in this assmembly code and all calls
92 * are C function which will save S series regs by themselves, there is
93 * no need to restore S series regs. The T series is available and safe
94 * at the callsite, so there is no need to restore the T series regs.
109 PTR_ADDI sp, sp, PT_SIZE
114 SYM_CODE_END(ftrace_common)
116 SYM_CODE_START(ftrace_caller)
117 UNWIND_HINT_UNDEFINED
118 ftrace_regs_entry allregs=0
120 SYM_CODE_END(ftrace_caller)
122 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
123 SYM_CODE_START(ftrace_regs_caller)
124 UNWIND_HINT_UNDEFINED
125 ftrace_regs_entry allregs=1
127 SYM_CODE_END(ftrace_regs_caller)
130 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
131 SYM_CODE_START(ftrace_graph_caller)
132 UNWIND_HINT_UNDEFINED
134 PTR_ADDI a0, a0, -8 /* arg0: self_addr */
135 PTR_ADDI a1, sp, PT_R1 /* arg1: parent */
136 bl prepare_ftrace_return
137 b ftrace_common_return
138 SYM_CODE_END(ftrace_graph_caller)
140 SYM_CODE_START(return_to_handler)
141 UNWIND_HINT_UNDEFINED
142 /* Save return value regs */
143 PTR_ADDI sp, sp, -FGRET_REGS_SIZE
144 PTR_S a0, sp, FGRET_REGS_A0
145 PTR_S a1, sp, FGRET_REGS_A1
146 PTR_S zero, sp, FGRET_REGS_FP
149 bl ftrace_return_to_handler
152 /* Restore return value regs */
153 PTR_L a0, sp, FGRET_REGS_A0
154 PTR_L a1, sp, FGRET_REGS_A1
155 PTR_ADDI sp, sp, FGRET_REGS_SIZE
158 SYM_CODE_END(return_to_handler)
161 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
162 SYM_CODE_START(ftrace_stub_direct_tramp)
163 UNWIND_HINT_UNDEFINED
165 SYM_CODE_END(ftrace_stub_direct_tramp)
166 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */