1/* SPDX-License-Identifier: GPL-2.0 */ 2// Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd. 3 4#include <linux/linkage.h> 5#include <asm/ftrace.h> 6 7/* 8 * csky-gcc with -pg will put the following asm after prologue: 9 * push r15 10 * jsri _mcount 11 * 12 * stack layout after mcount_enter in _mcount(): 13 * 14 * current sp => 0:+-------+ 15 * | a0-a3 | -> must save all argument regs 16 * +16:+-------+ 17 * | lr | -> _mcount lr (instrumente function's pc) 18 * +20:+-------+ 19 * | fp=r8 | -> instrumented function fp 20 * +24:+-------+ 21 * | plr | -> instrumented function lr (parent's pc) 22 * +-------+ 23 */ 24 25.macro mcount_enter 26 subi sp, 24 27 stw a0, (sp, 0) 28 stw a1, (sp, 4) 29 stw a2, (sp, 8) 30 stw a3, (sp, 12) 31 stw lr, (sp, 16) 32 stw r8, (sp, 20) 33.endm 34 35.macro mcount_exit 36 ldw a0, (sp, 0) 37 ldw a1, (sp, 4) 38 ldw a2, (sp, 8) 39 ldw a3, (sp, 12) 40 ldw t1, (sp, 16) 41 ldw r8, (sp, 20) 42 ldw lr, (sp, 24) 43 addi sp, 28 44 jmp t1 45.endm 46 47.macro save_return_regs 48 subi sp, 16 49 stw a0, (sp, 0) 50 stw a1, (sp, 4) 51 stw a2, (sp, 8) 52 stw a3, (sp, 12) 53.endm 54 55.macro restore_return_regs 56 mov lr, a0 57 ldw a0, (sp, 0) 58 ldw a1, (sp, 4) 59 ldw a2, (sp, 8) 60 ldw a3, (sp, 12) 61 addi sp, 16 62.endm 63 64ENTRY(ftrace_stub) 65 jmp lr 66END(ftrace_stub) 67 68ENTRY(_mcount) 69 mcount_enter 70 71 /* r26 is link register, only used with jsri translation */ 72 lrw r26, ftrace_trace_function 73 ldw r26, (r26, 0) 74 lrw a1, ftrace_stub 75 cmpne r26, a1 76 bf skip_ftrace 77 78 mov a0, lr 79 subi a0, MCOUNT_INSN_SIZE 80 ldw a1, (sp, 24) 81 82 jsr r26 83 84#ifndef CONFIG_FUNCTION_GRAPH_TRACER 85skip_ftrace: 86 mcount_exit 87#else 88skip_ftrace: 89 lrw a0, ftrace_graph_return 90 ldw a0, (a0, 0) 91 lrw a1, ftrace_stub 92 cmpne a0, a1 93 bt ftrace_graph_caller 94 95 lrw a0, ftrace_graph_entry 96 ldw a0, (a0, 0) 97 lrw a1, ftrace_graph_entry_stub 98 cmpne a0, a1 99 bt ftrace_graph_caller 100 101 mcount_exit 102#endif 103END(_mcount) 104 105#ifdef CONFIG_FUNCTION_GRAPH_TRACER 106ENTRY(ftrace_graph_caller) 107 mov a0, sp 108 addi a0, 24 109 ldw a1, (sp, 16) 110 subi a1, MCOUNT_INSN_SIZE 111 mov a2, r8 112 lrw r26, prepare_ftrace_return 113 jsr r26 114 mcount_exit 115END(ftrace_graph_caller) 116 117ENTRY(return_to_handler) 118 save_return_regs 119 mov a0, r8 120 jsri ftrace_return_to_handler 121 restore_return_regs 122 jmp lr 123END(return_to_handler) 124#endif 125