1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2022 Loongson Technology Corporation Limited
6 #include <asm/export.h>
7 #include <asm/ftrace.h>
8 #include <asm/regdef.h>
9 #include <asm/stackframe.h>
13 * Due to -fpatchable-function-entry=2: the compiler inserted 2 NOPs before the
14 * regular C function prologue. When PC arrived here, the last 2 instructions
17 * bl callsite (for modules, callsite is a tramplione)
19 * modules trampoline is as follows:
20 * lu12i.w t1, callsite[31:12]
21 * lu32i.d t1, callsite[51:32]
22 * lu52i.d t1, t1, callsite[63:52]
23 * jirl zero, t1, callsite[11:0] >> 2
25 * See arch/loongarch/kernel/ftrace_dyn.c for details. Here, pay attention to
26 * that the T series regs are available and safe because each C functions
27 * follows the LoongArch's psABI as well.
30 .macro ftrace_regs_entry allregs=0
31 PTR_ADDI sp, sp, -PT_SIZE
32 PTR_S t0, sp, PT_R1 /* Save parent ra at PT_R1(RA) */
63 /* Clear it for later use as a flag sometimes. */
66 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)
76 PTR_ADDI a0, ra, -8 /* arg0: ip */
77 move a1, t0 /* arg1: parent_ip */
78 la.pcrel t1, function_trace_op
79 PTR_L a2, t1, 0 /* arg2: op */
80 move a3, sp /* arg3: regs */
82 SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
84 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
85 SYM_INNER_LABEL(ftrace_graph_call, SYM_L_GLOBAL)
86 nop /* b ftrace_graph_caller */
90 * As we didn't use S series regs in this assmembly code and all calls
91 * are C function which will save S series regs by themselves, there is
92 * no need to restore S series regs. The T series is available and safe
93 * at the callsite, so there is no need to restore the T series regs.
107 PTR_ADDI sp, sp, PT_SIZE
109 SYM_CODE_END(ftrace_common)
111 SYM_CODE_START(ftrace_caller)
112 ftrace_regs_entry allregs=0
114 SYM_CODE_END(ftrace_caller)
116 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
117 SYM_CODE_START(ftrace_regs_caller)
118 ftrace_regs_entry allregs=1
120 SYM_CODE_END(ftrace_regs_caller)
123 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
124 SYM_CODE_START(ftrace_graph_caller)
126 PTR_ADDI a0, a0, -8 /* arg0: self_addr */
127 PTR_ADDI a1, sp, PT_R1 /* arg1: parent */
128 bl prepare_ftrace_return
129 b ftrace_common_return
130 SYM_CODE_END(ftrace_graph_caller)
132 SYM_CODE_START(return_to_handler)
133 /* Save return value regs */
134 PTR_ADDI sp, sp, -2 * SZREG
139 bl ftrace_return_to_handler
142 /* Restore return value regs */
145 PTR_ADDI sp, sp, 2 * SZREG
148 SYM_CODE_END(return_to_handler)