|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Based on arch/arm64/kernel/ftrace.c |
| 4 | + * |
| 5 | + * Copyright (C) 2022 Loongson Technology Corporation Limited |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/ftrace.h> |
| 9 | +#include <linux/uaccess.h> |
| 10 | + |
| 11 | +#include <asm/inst.h> |
| 12 | + |
| 13 | +static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate) |
| 14 | +{ |
| 15 | + u32 replaced; |
| 16 | + |
| 17 | + if (validate) { |
| 18 | + if (larch_insn_read((void *)pc, &replaced)) |
| 19 | + return -EFAULT; |
| 20 | + |
| 21 | + if (replaced != old) |
| 22 | + return -EINVAL; |
| 23 | + } |
| 24 | + |
| 25 | + if (larch_insn_patch_text((void *)pc, new)) |
| 26 | + return -EPERM; |
| 27 | + |
| 28 | + return 0; |
| 29 | +} |
| 30 | + |
| 31 | +int ftrace_update_ftrace_func(ftrace_func_t func) |
| 32 | +{ |
| 33 | + u32 new; |
| 34 | + unsigned long pc; |
| 35 | + |
| 36 | + pc = (unsigned long)&ftrace_call; |
| 37 | + new = larch_insn_gen_bl(pc, (unsigned long)func); |
| 38 | + |
| 39 | + return ftrace_modify_code(pc, 0, new, false); |
| 40 | +} |
| 41 | + |
| 42 | +/* |
| 43 | + * The compiler has inserted 2 NOPs before the regular function prologue. |
| 44 | + * T series registers are available and safe because of LoongArch's psABI. |
| 45 | + * |
| 46 | + * At runtime, we can replace nop with bl to enable ftrace call and replace bl |
| 47 | + * with nop to disable ftrace call. The bl requires us to save the original RA |
| 48 | + * value, so it saves RA at t0 here. |
| 49 | + * |
| 50 | + * Details are: |
| 51 | + * |
| 52 | + * | Compiled | Disabled | Enabled | |
| 53 | + * +------------+------------------------+------------------------+ |
| 54 | + * | nop | move t0, ra | move t0, ra | |
| 55 | + * | nop | nop | bl ftrace_caller | |
| 56 | + * | func_body | func_body | func_body | |
| 57 | + * |
| 58 | + * The RA value will be recovered by ftrace_regs_entry, and restored into RA |
| 59 | + * before returning to the regular function prologue. When a function is not |
| 60 | + * being traced, the "move t0, ra" is not harmful. |
| 61 | + */ |
| 62 | + |
| 63 | +int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec) |
| 64 | +{ |
| 65 | + u32 old, new; |
| 66 | + unsigned long pc; |
| 67 | + |
| 68 | + pc = rec->ip; |
| 69 | + old = larch_insn_gen_nop(); |
| 70 | + new = larch_insn_gen_move(LOONGARCH_GPR_T0, LOONGARCH_GPR_RA); |
| 71 | + |
| 72 | + return ftrace_modify_code(pc, old, new, true); |
| 73 | +} |
| 74 | + |
| 75 | +int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) |
| 76 | +{ |
| 77 | + u32 old, new; |
| 78 | + unsigned long pc; |
| 79 | + |
| 80 | + pc = rec->ip + LOONGARCH_INSN_SIZE; |
| 81 | + |
| 82 | + old = larch_insn_gen_nop(); |
| 83 | + new = larch_insn_gen_bl(pc, addr); |
| 84 | + |
| 85 | + return ftrace_modify_code(pc, old, new, true); |
| 86 | +} |
| 87 | + |
| 88 | +int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr) |
| 89 | +{ |
| 90 | + u32 old, new; |
| 91 | + unsigned long pc; |
| 92 | + |
| 93 | + pc = rec->ip + LOONGARCH_INSN_SIZE; |
| 94 | + |
| 95 | + new = larch_insn_gen_nop(); |
| 96 | + old = larch_insn_gen_bl(pc, addr); |
| 97 | + |
| 98 | + return ftrace_modify_code(pc, old, new, true); |
| 99 | +} |
| 100 | + |
| 101 | +void arch_ftrace_update_code(int command) |
| 102 | +{ |
| 103 | + command |= FTRACE_MAY_SLEEP; |
| 104 | + ftrace_modify_all_code(command); |
| 105 | +} |
| 106 | + |
| 107 | +int __init ftrace_dyn_arch_init(void) |
| 108 | +{ |
| 109 | + return 0; |
| 110 | +} |
0 commit comments