Skip to content

Commit 0942769

Browse files
ARCv2: Support ftrace and function graph tracer
Added support for both FTRACE and FUNCTION_GRAPH_TRACER (GCC > 14) in ARCv2 Signed-off-by: Bruno Mauricio <[email protected]>
1 parent b42b437 commit 0942769

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

arch/arc/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ config ARC
3232
select HAVE_ARCH_TRANSPARENT_HUGEPAGE if ARC_MMU_V4
3333
select HAVE_DEBUG_STACKOVERFLOW
3434
select HAVE_DEBUG_KMEMLEAK
35+
select HAVE_FUNCTION_TRACER if ISA_ARCV2
36+
select HAVE_FUNCTION_GRAPH_TRACER if (ISA_ARCV2 && GCC_VERSION >= 140000)
3537
select HAVE_FUTEX_CMPXCHG if FUTEX
3638
select HAVE_IOREMAP_PROT if !(ISA_ARCV3 && !64BIT)
3739
select HAVE_KERNEL_GZIP

arch/arc/kernel/Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ obj-y := arcksyms.o setup.o irq.o reset.o ptrace.o process.o devtree.o
77
obj-y += signal.o traps.o sys.o troubleshoot.o stacktrace.o disasm.o
88
obj-y += ctx_sw_asm.o
99

10+
ifdef CONFIG_FTRACE
11+
# ftrace can't be traced (infinite loop)
12+
CFLAGS_REMOVE_ftrace.o = -pg
13+
CFLAGS_REMOVE_mcount.o = -pg
14+
15+
obj-$(CONFIG_FUNCTION_TRACER) += mcount.o
16+
obj-$(CONFIG_FUNCTION_GRAPH_TRACER) += ftrace.o
17+
18+
endif
19+
1020
ifdef CONFIG_ISA_ARCOMPACT
1121
obj-y += intc-compact.o
1222
else

arch/arc/kernel/ftrace.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Function tracing support for ARC
4+
*
5+
* Copyright (C) 2023 Synopsys, Inc. (www.synopsys.com)
6+
*/
7+
8+
#include <linux/ftrace.h>
9+
10+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
11+
12+
/*
13+
* Setup return hook in traced routine
14+
* Function copied from riscv
15+
*/
16+
void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
17+
unsigned long frame_pointer)
18+
{
19+
unsigned long return_hooker = (unsigned long)&return_to_handler;
20+
unsigned long old;
21+
22+
if (unlikely(atomic_read(&current->tracing_graph_pause)))
23+
return;
24+
25+
old = *parent;
26+
27+
if (!function_graph_enter(old, self_addr, frame_pointer, parent))
28+
*parent = return_hooker;
29+
}
30+
31+
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */

arch/arc/kernel/mcount.S

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Function tracing support for ARC
4+
*
5+
* Copyright (C) 2023 Synopsys, Inc. (www.synopsys.com)
6+
*/
7+
8+
#include <linux/linkage.h>
9+
#include <asm-generic/export.h>
10+
11+
; ftrace placeholder, just return to caller
12+
ENTRY(ftrace_stub)
13+
j_s [blink]
14+
ENDPROC(ftrace_stub)
15+
16+
.macro SAVE_ABI
17+
push blink
18+
.endm
19+
20+
.macro LOAD_ABI
21+
pop blink
22+
.endm
23+
24+
.macro SAVE_ABI_RET
25+
push r0
26+
push r1
27+
.endm
28+
29+
.macro LOAD_ABI_RET
30+
pop r1
31+
pop r0
32+
.endm
33+
34+
; r0 has the frompc (targets parent ip)
35+
; blink has the selfpc (target ip)
36+
ENTRY(_mcount)
37+
38+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
39+
40+
; *ftrace_graph_return != ftrace_stub
41+
ld r3, [ftrace_graph_return]
42+
brne r3, @ftrace_stub, @do_ftrace_graph_caller
43+
; *ftrace_graph_entry != *ftrace_graph_entry_stub
44+
ld r3, [ftrace_graph_entry]
45+
brne r3, @ftrace_graph_entry_stub, @do_ftrace_graph_caller
46+
47+
#endif
48+
49+
; *ftrace_trace_function != ftrace_stub
50+
ld r2, [ftrace_trace_function]
51+
brne r2, @ftrace_stub, @do_trace
52+
; Return
53+
j_s [blink]
54+
55+
ENDPROC(_mcount)
56+
57+
#ifdef CONFIG_FUNCTION_GRAPH_TRACER
58+
59+
; Return to the actual caller
60+
ENTRY(return_to_handler)
61+
; Save return value (if any) from handled routine
62+
SAVE_ABI_RET
63+
; Will return true blink on r0
64+
jl ftrace_return_to_handler
65+
mov r2, r0
66+
LOAD_ABI_RET
67+
j [r2]
68+
69+
ENDPROC(return_to_handler)
70+
71+
do_ftrace_graph_caller:
72+
; ABI does not allow us to infer blink location
73+
; ARC GCC port inserts into r1 the delta between the pushed blink and
74+
; the sp at call time
75+
; We perform calculation before any push (sp change) happens
76+
add r0, r1, sp
77+
SAVE_ABI
78+
sub r0, r0, 4
79+
mov r1, blink
80+
jl prepare_ftrace_return
81+
LOAD_ABI
82+
j_s [blink]
83+
#endif
84+
85+
do_trace:
86+
SAVE_ABI
87+
mov r1, r0
88+
mov r0, blink
89+
jl [r2]
90+
; load ABI state and jump to blink (in stack)
91+
LOAD_ABI
92+
j_s [blink]
93+
94+
EXPORT_SYMBOL(_mcount)

0 commit comments

Comments
 (0)