/* SPDX-License-Identifier: GPL-2.0 */1/* Copyright (C) 2017 Andes Technology Corporation */23#include <linux/init.h>4#include <linux/linkage.h>5#include <linux/export.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/ftrace.h>1213.text1415#define ABI_SIZE_ON_STACK 8016#define ABI_A0 017#define ABI_A1 818#define ABI_A2 1619#define ABI_A3 2420#define ABI_A4 3221#define ABI_A5 4022#define ABI_A6 4823#define ABI_A7 5624#define ABI_T0 6425#define ABI_RA 722627.macro SAVE_ABI28addi sp, sp, -ABI_SIZE_ON_STACK2930REG_S a0, ABI_A0(sp)31REG_S a1, ABI_A1(sp)32REG_S a2, ABI_A2(sp)33REG_S a3, ABI_A3(sp)34REG_S a4, ABI_A4(sp)35REG_S a5, ABI_A5(sp)36REG_S a6, ABI_A6(sp)37REG_S a7, ABI_A7(sp)38REG_S t0, ABI_T0(sp)39REG_S ra, ABI_RA(sp)40.endm4142.macro RESTORE_ABI43REG_L a0, ABI_A0(sp)44REG_L a1, ABI_A1(sp)45REG_L a2, ABI_A2(sp)46REG_L a3, ABI_A3(sp)47REG_L a4, ABI_A4(sp)48REG_L a5, ABI_A5(sp)49REG_L a6, ABI_A6(sp)50REG_L a7, ABI_A7(sp)51REG_L t0, ABI_T0(sp)52REG_L ra, ABI_RA(sp)5354addi sp, sp, ABI_SIZE_ON_STACK55.endm5657/**58* SAVE_ABI_REGS - save regs against the ftrace_regs struct59*60* After the stack is established,61*62* 0(sp) stores the PC of the traced function which can be accessed63* by &(fregs)->epc in tracing function.64*65* 8(sp) stores the function return address (i.e. parent IP) that66* can be accessed by &(fregs)->ra in tracing function.67*68* The other regs are saved at the respective localtion and accessed69* by the respective ftrace_regs member.70*71* Here is the layout of stack for your reference.72*73* PT_SIZE_ON_STACK -> +++++++++74* + ..... +75* + a0-a7 + --++++-> ftrace_caller saved76* + t1 + --++++-> direct tramp address77* + s0 + --+ // frame pointer78* + sp + +79* + ra + --+ // parent IP80* sp -> + epc + --+ // PC81* +++++++++82**/83.macro SAVE_ABI_REGS84addi sp, sp, -FREGS_SIZE_ON_STACK85REG_S t0, FREGS_EPC(sp)86REG_S x1, FREGS_RA(sp)87#ifdef HAVE_FUNCTION_GRAPH_FP_TEST88REG_S x8, FREGS_S0(sp)89#endif90REG_S x6, FREGS_T1(sp)91#ifdef CONFIG_CC_IS_CLANG92REG_S x7, FREGS_T2(sp)93REG_S x28, FREGS_T3(sp)94REG_S x29, FREGS_T4(sp)95REG_S x30, FREGS_T5(sp)96REG_S x31, FREGS_T6(sp)97#endif98// save the arguments99REG_S x10, FREGS_A0(sp)100REG_S x11, FREGS_A1(sp)101REG_S x12, FREGS_A2(sp)102REG_S x13, FREGS_A3(sp)103REG_S x14, FREGS_A4(sp)104REG_S x15, FREGS_A5(sp)105REG_S x16, FREGS_A6(sp)106REG_S x17, FREGS_A7(sp)107mv a0, sp108addi a0, a0, FREGS_SIZE_ON_STACK109REG_S a0, FREGS_SP(sp) // Put original SP on stack110.endm111112.macro RESTORE_ABI_REGS113REG_L t0, FREGS_EPC(sp)114REG_L x1, FREGS_RA(sp)115#ifdef HAVE_FUNCTION_GRAPH_FP_TEST116REG_L x8, FREGS_S0(sp)117#endif118REG_L x6, FREGS_T1(sp)119#ifdef CONFIG_CC_IS_CLANG120REG_L x7, FREGS_T2(sp)121REG_L x28, FREGS_T3(sp)122REG_L x29, FREGS_T4(sp)123REG_L x30, FREGS_T5(sp)124REG_L x31, FREGS_T6(sp)125#endif126// restore the arguments127REG_L x10, FREGS_A0(sp)128REG_L x11, FREGS_A1(sp)129REG_L x12, FREGS_A2(sp)130REG_L x13, FREGS_A3(sp)131REG_L x14, FREGS_A4(sp)132REG_L x15, FREGS_A5(sp)133REG_L x16, FREGS_A6(sp)134REG_L x17, FREGS_A7(sp)135136addi sp, sp, FREGS_SIZE_ON_STACK137.endm138139.macro PREPARE_ARGS140addi a0, t0, -MCOUNT_JALR_SIZE // ip (callsite's jalr insn)141#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS142mv a1, ra // parent_ip143REG_L a2, -16(t0) // op144REG_L ra, FTRACE_OPS_FUNC(a2) // op->func145#else146la a1, function_trace_op147REG_L a2, 0(a1) // op148mv a1, ra // parent_ip149#endif150mv a3, sp // regs151.endm152153SYM_FUNC_START(ftrace_caller)154#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS155/*156* When CALL_OPS is enabled (2 or 4) nops [8B] are placed before the157* function entry, these are later overwritten with the pointer to the158* associated struct ftrace_ops.159*160* -8: &ftrace_ops of the associated tracer function.161*<ftrace enable>:162* 0: auipc t0/ra, 0x?163* 4: jalr t0/ra, ?(t0/ra)164*165* -8: &ftrace_nop_ops166*<ftrace disable>:167* 0: nop168* 4: nop169*170* t0 is set to ip+8 after the jalr is executed at the callsite,171* so we find the associated op at t0-16.172*/173REG_L t1, -16(t0) // op Should be SZ_REG instead of 16174175#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS176/*177* If the op has a direct call, handle it immediately without178* saving/restoring registers.179*/180REG_L t1, FTRACE_OPS_DIRECT_CALL(t1)181bnez t1, ftrace_caller_direct182#endif183#endif184SAVE_ABI_REGS185PREPARE_ARGS186187#ifdef CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS188jalr ra189#else190SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)191REG_L ra, ftrace_call_dest192jalr ra, 0(ra)193#endif194RESTORE_ABI_REGS195#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS196bnez t1, ftrace_caller_direct197#endif198jr t0199#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS200SYM_INNER_LABEL(ftrace_caller_direct, SYM_L_LOCAL)201jr t1202#endif203SYM_FUNC_END(ftrace_caller)204205#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS206SYM_CODE_START(ftrace_stub_direct_tramp)207jr t0208SYM_CODE_END(ftrace_stub_direct_tramp)209#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */210211212