b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* Copyright (C) 2017 Andes Technology Corporation */ |
| 3 | |
| 4 | /* |
| 5 | * The graph frame test is not possible if CONFIG_FRAME_POINTER is not enabled. |
| 6 | * Check arch/riscv/kernel/mcount.S for detail. |
| 7 | */ |
| 8 | #if defined(CONFIG_FUNCTION_GRAPH_TRACER) && defined(CONFIG_FRAME_POINTER) |
| 9 | #define HAVE_FUNCTION_GRAPH_FP_TEST |
| 10 | #endif |
| 11 | #define HAVE_FUNCTION_GRAPH_RET_ADDR_PTR |
| 12 | |
| 13 | /* |
| 14 | * Clang prior to 13 had "mcount" instead of "_mcount": |
| 15 | * https://reviews.llvm.org/D98881 |
| 16 | */ |
| 17 | #if defined(CONFIG_CC_IS_GCC) || CONFIG_CLANG_VERSION >= 130000 |
| 18 | #define MCOUNT_NAME _mcount |
| 19 | #else |
| 20 | #define MCOUNT_NAME mcount |
| 21 | #endif |
| 22 | |
| 23 | #define ARCH_SUPPORTS_FTRACE_OPS 1 |
| 24 | #ifndef __ASSEMBLY__ |
| 25 | void MCOUNT_NAME(void); |
| 26 | static inline unsigned long ftrace_call_adjust(unsigned long addr) |
| 27 | { |
| 28 | return addr; |
| 29 | } |
| 30 | |
| 31 | struct dyn_arch_ftrace { |
| 32 | }; |
| 33 | #endif |
| 34 | |
| 35 | #ifdef CONFIG_DYNAMIC_FTRACE |
| 36 | /* |
| 37 | * A general call in RISC-V is a pair of insts: |
| 38 | * 1) auipc: setting high-20 pc-related bits to ra register |
| 39 | * 2) jalr: setting low-12 offset to ra, jump to ra, and set ra to |
| 40 | * return address (original pc + 4) |
| 41 | * |
| 42 | * Dynamic ftrace generates probes to call sites, so we must deal with |
| 43 | * both auipc and jalr at the same time. |
| 44 | */ |
| 45 | |
| 46 | #define MCOUNT_ADDR ((unsigned long)MCOUNT_NAME) |
| 47 | #define JALR_SIGN_MASK (0x00000800) |
| 48 | #define JALR_OFFSET_MASK (0x00000fff) |
| 49 | #define AUIPC_OFFSET_MASK (0xfffff000) |
| 50 | #define AUIPC_PAD (0x00001000) |
| 51 | #define JALR_SHIFT 20 |
| 52 | #define JALR_BASIC (0x000080e7) |
| 53 | #define AUIPC_BASIC (0x00000097) |
| 54 | #define NOP4 (0x00000013) |
| 55 | |
| 56 | #define make_call(caller, callee, call) \ |
| 57 | do { \ |
| 58 | call[0] = to_auipc_insn((unsigned int)((unsigned long)callee - \ |
| 59 | (unsigned long)caller)); \ |
| 60 | call[1] = to_jalr_insn((unsigned int)((unsigned long)callee - \ |
| 61 | (unsigned long)caller)); \ |
| 62 | } while (0) |
| 63 | |
| 64 | #define to_jalr_insn(offset) \ |
| 65 | (((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_BASIC) |
| 66 | |
| 67 | #define to_auipc_insn(offset) \ |
| 68 | ((offset & JALR_SIGN_MASK) ? \ |
| 69 | (((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_BASIC) : \ |
| 70 | ((offset & AUIPC_OFFSET_MASK) | AUIPC_BASIC)) |
| 71 | |
| 72 | /* |
| 73 | * Let auipc+jalr be the basic *mcount unit*, so we make it 8 bytes here. |
| 74 | */ |
| 75 | #define MCOUNT_INSN_SIZE 8 |
| 76 | |
| 77 | #ifndef __ASSEMBLY__ |
| 78 | struct dyn_ftrace; |
| 79 | int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec); |
| 80 | #define ftrace_init_nop ftrace_init_nop |
| 81 | #endif |
| 82 | |
| 83 | #endif |