Skip to content

Commit 89a3927

Browse files
committed
csky: Implement ftrace with regs
This patch implements FTRACE_WITH_REGS for csky, 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/or modified. Signed-off-by: Guo Ren <[email protected]>
1 parent 9866d14 commit 89a3927

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

arch/csky/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ config CSKY
3838
select HAVE_ARCH_AUDITSYSCALL
3939
select HAVE_COPY_THREAD_TLS
4040
select HAVE_DYNAMIC_FTRACE
41+
select HAVE_DYNAMIC_FTRACE_WITH_REGS
4142
select HAVE_FUNCTION_TRACER
4243
select HAVE_FUNCTION_GRAPH_TRACER
4344
select HAVE_FTRACE_MCOUNT_RECORD

arch/csky/abiv2/inc/abi/entry.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,66 @@
100100
rte
101101
.endm
102102

103+
.macro SAVE_REGS_FTRACE
104+
subi sp, 152
105+
stw tls, (sp, 0)
106+
stw lr, (sp, 4)
107+
108+
mfcr lr, psr
109+
stw lr, (sp, 12)
110+
111+
addi lr, sp, 152
112+
stw lr, (sp, 16)
113+
114+
stw a0, (sp, 20)
115+
stw a0, (sp, 24)
116+
stw a1, (sp, 28)
117+
stw a2, (sp, 32)
118+
stw a3, (sp, 36)
119+
120+
addi sp, 40
121+
stm r4-r13, (sp)
122+
123+
addi sp, 40
124+
stm r16-r30, (sp)
125+
#ifdef CONFIG_CPU_HAS_HILO
126+
mfhi lr
127+
stw lr, (sp, 60)
128+
mflo lr
129+
stw lr, (sp, 64)
130+
mfcr lr, cr14
131+
stw lr, (sp, 68)
132+
#endif
133+
subi sp, 80
134+
.endm
135+
136+
.macro RESTORE_REGS_FTRACE
137+
ldw tls, (sp, 0)
138+
ldw a0, (sp, 16)
139+
mtcr a0, ss0
140+
141+
#ifdef CONFIG_CPU_HAS_HILO
142+
ldw a0, (sp, 140)
143+
mthi a0
144+
ldw a0, (sp, 144)
145+
mtlo a0
146+
ldw a0, (sp, 148)
147+
mtcr a0, cr14
148+
#endif
149+
150+
ldw a0, (sp, 24)
151+
ldw a1, (sp, 28)
152+
ldw a2, (sp, 32)
153+
ldw a3, (sp, 36)
154+
155+
addi sp, 40
156+
ldm r4-r13, (sp)
157+
addi sp, 40
158+
ldm r16-r30, (sp)
159+
addi sp, 72
160+
mfcr sp, ss0
161+
.endm
162+
103163
.macro SAVE_SWITCH_STACK
104164
subi sp, 64
105165
stm r4-r11, (sp)

arch/csky/abiv2/mcount.S

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <linux/linkage.h>
55
#include <asm/ftrace.h>
6+
#include <abi/entry.h>
7+
#include <asm/asm-offsets.h>
68

79
/*
810
* csky-gcc with -pg will put the following asm after prologue:
@@ -44,6 +46,22 @@
4446
jmp t1
4547
.endm
4648

49+
.macro mcount_enter_regs
50+
subi sp, 8
51+
stw lr, (sp, 0)
52+
stw r8, (sp, 4)
53+
SAVE_REGS_FTRACE
54+
.endm
55+
56+
.macro mcount_exit_regs
57+
RESTORE_REGS_FTRACE
58+
ldw t1, (sp, 0)
59+
ldw r8, (sp, 4)
60+
ldw lr, (sp, 8)
61+
addi sp, 12
62+
jmp t1
63+
.endm
64+
4765
.macro save_return_regs
4866
subi sp, 16
4967
stw a0, (sp, 0)
@@ -122,6 +140,8 @@ ENTRY(ftrace_caller)
122140
ldw a0, (sp, 16)
123141
subi a0, 4
124142
ldw a1, (sp, 24)
143+
lrw a2, function_trace_op
144+
ldw a2, (a2, 0)
125145

126146
nop
127147
GLOBAL(ftrace_call)
@@ -157,3 +177,31 @@ ENTRY(return_to_handler)
157177
jmp lr
158178
END(return_to_handler)
159179
#endif
180+
181+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
182+
ENTRY(ftrace_regs_caller)
183+
mcount_enter_regs
184+
185+
lrw t1, PT_FRAME_SIZE
186+
add t1, sp
187+
188+
ldw a0, (t1, 0)
189+
subi a0, 4
190+
ldw a1, (t1, 8)
191+
lrw a2, function_trace_op
192+
ldw a2, (a2, 0)
193+
mov a3, sp
194+
195+
nop
196+
GLOBAL(ftrace_regs_call)
197+
nop32_stub
198+
199+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
200+
nop
201+
GLOBAL(ftrace_graph_regs_call)
202+
nop32_stub
203+
#endif
204+
205+
mcount_exit_regs
206+
ENDPROC(ftrace_regs_caller)
207+
#endif /* CONFIG_DYNAMIC_FTRACE */

arch/csky/include/asm/ftrace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#define HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
1212

13+
#define ARCH_SUPPORTS_FTRACE_OPS 1
14+
1315
#define MCOUNT_ADDR ((unsigned long)_mcount)
1416

1517
#ifndef __ASSEMBLY__

arch/csky/kernel/asm-offsets.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ int main(void)
7272
DEFINE(PT_RLO, offsetof(struct pt_regs, rlo));
7373
#endif
7474
DEFINE(PT_USP, offsetof(struct pt_regs, usp));
75+
DEFINE(PT_FRAME_SIZE, sizeof(struct pt_regs));
7576

7677
/* offsets into the irq_cpustat_t struct */
7778
DEFINE(CPUSTAT_SOFTIRQ_PENDING, offsetof(irq_cpustat_t,

arch/csky/kernel/ftrace.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ int ftrace_update_ftrace_func(ftrace_func_t func)
126126
{
127127
int ret = ftrace_modify_code((unsigned long)&ftrace_call,
128128
(unsigned long)func, true, true);
129+
if (!ret)
130+
ret = ftrace_modify_code((unsigned long)&ftrace_regs_call,
131+
(unsigned long)func, true, true);
129132
return ret;
130133
}
131134

@@ -135,6 +138,14 @@ int __init ftrace_dyn_arch_init(void)
135138
}
136139
#endif /* CONFIG_DYNAMIC_FTRACE */
137140

141+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
142+
int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
143+
unsigned long addr)
144+
{
145+
return ftrace_modify_code(rec->ip, addr, true, true);
146+
}
147+
#endif
148+
138149
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
139150
void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
140151
unsigned long frame_pointer)

0 commit comments

Comments
 (0)