Skip to content

Commit 8778ba2

Browse files
zhangqingmychenhuacai
authored andcommitted
LoongArch/ftrace: Add HAVE_DYNAMIC_FTRACE_WITH_REGS support
This patch implements CONFIG_DYNAMIC_FTRACE_WITH_REGS on LoongArch, which allows a traced function's arguments (and some other registers) to be captured into a struct pt_regs, allowing these to be inspected and modified. Co-developed-by: Jinyang He <[email protected]> Signed-off-by: Jinyang He <[email protected]> Signed-off-by: Qing Zhang <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent 5fcfad3 commit 8778ba2

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ config LOONGARCH
8989
select HAVE_DEBUG_STACKOVERFLOW
9090
select HAVE_DMA_CONTIGUOUS
9191
select HAVE_DYNAMIC_FTRACE
92+
select HAVE_DYNAMIC_FTRACE_WITH_REGS
9293
select HAVE_EBPF_JIT
9394
select HAVE_EXIT_THREAD
9495
select HAVE_FAST_GUP

arch/loongarch/include/asm/ftrace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ extern void prepare_ftrace_return(unsigned long self_addr, unsigned long callsit
2323
struct dyn_ftrace;
2424
struct dyn_arch_ftrace { };
2525

26+
#define ARCH_SUPPORTS_FTRACE_OPS 1
27+
2628
#define ftrace_init_nop ftrace_init_nop
2729
int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec);
2830

arch/loongarch/kernel/ftrace_dyn.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ static int ftrace_modify_code(unsigned long pc, u32 old, u32 new, bool validate)
2828
return 0;
2929
}
3030

31+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
32+
int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr, unsigned long addr)
33+
{
34+
u32 old, new;
35+
unsigned long pc;
36+
37+
pc = rec->ip + LOONGARCH_INSN_SIZE;
38+
39+
new = larch_insn_gen_bl(pc, addr);
40+
old = larch_insn_gen_bl(pc, old_addr);
41+
42+
return ftrace_modify_code(pc, old, new, true);
43+
}
44+
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */
45+
3146
int ftrace_update_ftrace_func(ftrace_func_t func)
3247
{
3348
u32 new;

arch/loongarch/kernel/mcount_dyn.S

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* follows the LoongArch's psABI as well.
2828
*/
2929

30-
.macro ftrace_regs_entry
30+
.macro ftrace_regs_entry allregs=0
3131
PTR_ADDI sp, sp, -PT_SIZE
3232
PTR_S t0, sp, PT_R1 /* Save parent ra at PT_R1(RA) */
3333
PTR_S a0, sp, PT_R4
@@ -39,6 +39,30 @@
3939
PTR_S a6, sp, PT_R10
4040
PTR_S a7, sp, PT_R11
4141
PTR_S fp, sp, PT_R22
42+
.if \allregs
43+
PTR_S tp, sp, PT_R2
44+
PTR_S t0, sp, PT_R12
45+
PTR_S t1, sp, PT_R13
46+
PTR_S t2, sp, PT_R14
47+
PTR_S t3, sp, PT_R15
48+
PTR_S t4, sp, PT_R16
49+
PTR_S t5, sp, PT_R17
50+
PTR_S t6, sp, PT_R18
51+
PTR_S t7, sp, PT_R19
52+
PTR_S t8, sp, PT_R20
53+
PTR_S u0, sp, PT_R21
54+
PTR_S s0, sp, PT_R23
55+
PTR_S s1, sp, PT_R24
56+
PTR_S s2, sp, PT_R25
57+
PTR_S s3, sp, PT_R26
58+
PTR_S s4, sp, PT_R27
59+
PTR_S s5, sp, PT_R28
60+
PTR_S s6, sp, PT_R29
61+
PTR_S s7, sp, PT_R30
62+
PTR_S s8, sp, PT_R31
63+
/* Clear it for later use as a flag sometimes. */
64+
PTR_S zero, sp, PT_R0
65+
.endif
4266
PTR_S ra, sp, PT_ERA /* Save trace function ra at PT_ERA */
4367
PTR_ADDI t8, sp, PT_SIZE
4468
PTR_S t8, sp, PT_R3
@@ -85,10 +109,17 @@ ftrace_common_return:
85109
SYM_CODE_END(ftrace_common)
86110

87111
SYM_CODE_START(ftrace_caller)
88-
ftrace_regs_entry
112+
ftrace_regs_entry allregs=0
89113
b ftrace_common
90114
SYM_CODE_END(ftrace_caller)
91115

116+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
117+
SYM_CODE_START(ftrace_regs_caller)
118+
ftrace_regs_entry allregs=1
119+
b ftrace_common
120+
SYM_CODE_END(ftrace_regs_caller)
121+
#endif
122+
92123
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
93124
SYM_CODE_START(ftrace_graph_caller)
94125
PTR_L a0, sp, PT_ERA

0 commit comments

Comments
 (0)