Skip to content

Commit dfd9f7a

Browse files
heicarstMartin Schwidefsky
authored andcommitted
[S390] ftrace: add dynamic ftrace support
Dynamic ftrace support for s390. Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent a2b5367 commit dfd9f7a

10 files changed

Lines changed: 276 additions & 29 deletions

File tree

‎arch/s390/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ config S390
8282
select USE_GENERIC_SMP_HELPERS if SMP
8383
select HAVE_SYSCALL_WRAPPERS
8484
select HAVE_FUNCTION_TRACER
85+
select HAVE_FTRACE_MCOUNT_RECORD
86+
select HAVE_DYNAMIC_FTRACE
8587
select HAVE_DEFAULT_NO_SPIN_MUTEXES
8688
select HAVE_OPROFILE
8789
select HAVE_KPROBES

‎arch/s390/include/asm/ftrace.h‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@
22
#define _ASM_S390_FTRACE_H
33

44
#ifndef __ASSEMBLY__
5+
56
extern void _mcount(void);
7+
extern unsigned long ftrace_dyn_func;
8+
9+
struct dyn_arch_ftrace { };
10+
11+
#define MCOUNT_ADDR ((long)_mcount)
12+
13+
#ifdef CONFIG_64BIT
14+
#define MCOUNT_INSN_SIZE 24
15+
#define MCOUNT_OFFSET 14
16+
#else
17+
#define MCOUNT_INSN_SIZE 30
18+
#define MCOUNT_OFFSET 8
619
#endif
720

21+
static inline unsigned long ftrace_call_adjust(unsigned long addr)
22+
{
23+
return addr - MCOUNT_OFFSET;
24+
}
25+
26+
#endif /* __ASSEMBLY__ */
827
#endif /* _ASM_S390_FTRACE_H */

‎arch/s390/include/asm/lowcore.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#define __LC_CPUID 0x02b0
6969
#define __LC_INT_CLOCK 0x02c8
7070
#define __LC_MACHINE_FLAGS 0x02d8
71+
#define __LC_FTRACE_FUNC 0x02dc
7172
#define __LC_IRB 0x0300
7273
#define __LC_PFAULT_INTPARM 0x0080
7374
#define __LC_CPU_TIMER_SAVE_AREA 0x00d8
@@ -113,6 +114,7 @@
113114
#define __LC_INT_CLOCK 0x0340
114115
#define __LC_VDSO_PER_CPU 0x0350
115116
#define __LC_MACHINE_FLAGS 0x0358
117+
#define __LC_FTRACE_FUNC 0x0360
116118
#define __LC_IRB 0x0380
117119
#define __LC_PASTE 0x03c0
118120
#define __LC_PFAULT_INTPARM 0x11b8
@@ -281,7 +283,8 @@ struct _lowcore
281283
__u64 int_clock; /* 0x02c8 */
282284
__u64 clock_comparator; /* 0x02d0 */
283285
__u32 machine_flags; /* 0x02d8 */
284-
__u8 pad_0x02dc[0x0300-0x02dc]; /* 0x02dc */
286+
__u32 ftrace_func; /* 0x02dc */
287+
__u8 pad_0x02f0[0x0300-0x02f0]; /* 0x02f0 */
285288

286289
/* Interrupt response block */
287290
__u8 irb[64]; /* 0x0300 */
@@ -386,7 +389,8 @@ struct _lowcore
386389
__u64 clock_comparator; /* 0x0348 */
387390
__u64 vdso_per_cpu_data; /* 0x0350 */
388391
__u64 machine_flags; /* 0x0358 */
389-
__u8 pad_0x0360[0x0380-0x0360]; /* 0x0360 */
392+
__u64 ftrace_func; /* 0x0360 */
393+
__u8 pad_0x0368[0x0380-0x0368]; /* 0x0368 */
390394

391395
/* Interrupt response block. */
392396
__u8 irb[64]; /* 0x0380 */

‎arch/s390/kernel/Makefile‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ ifdef CONFIG_FUNCTION_TRACER
77
CFLAGS_REMOVE_early.o = -pg
88
endif
99

10+
ifdef CONFIG_DYNAMIC_FTRACE
11+
CFLAGS_REMOVE_ftrace.o = -pg
12+
endif
13+
1014
#
1115
# Passing null pointers is ok for smp code, since we access the lowcore here.
1216
#
@@ -41,6 +45,7 @@ obj-$(CONFIG_COMPAT) += compat_linux.o compat_signal.o \
4145
obj-$(CONFIG_STACKTRACE) += stacktrace.o
4246
obj-$(CONFIG_KPROBES) += kprobes.o
4347
obj-$(CONFIG_FUNCTION_TRACER) += mcount.o
48+
obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
4449

4550
# Kexec part
4651
S390_KEXEC_OBJS := machine_kexec.o crash.o

‎arch/s390/kernel/early.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/errno.h>
1212
#include <linux/string.h>
1313
#include <linux/ctype.h>
14+
#include <linux/ftrace.h>
1415
#include <linux/lockdep.h>
1516
#include <linux/module.h>
1617
#include <linux/pfn.h>
@@ -410,5 +411,8 @@ void __init startup_init(void)
410411
sclp_facilities_detect();
411412
detect_memory_layout(memory_chunk);
412413
S390_lowcore.machine_flags = machine_flags;
414+
#ifdef CONFIG_DYNAMIC_FTRACE
415+
S390_lowcore.ftrace_func = (unsigned long)ftrace_caller;
416+
#endif
413417
lockdep_on();
414418
}

‎arch/s390/kernel/ftrace.c‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Dynamic function tracer architecture backend.
3+
*
4+
* Copyright IBM Corp. 2009
5+
*
6+
* Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
7+
*
8+
*/
9+
10+
#include <linux/uaccess.h>
11+
#include <linux/ftrace.h>
12+
#include <linux/kernel.h>
13+
#include <linux/types.h>
14+
#include <asm/lowcore.h>
15+
16+
void ftrace_disable_code(void);
17+
void ftrace_call_code(void);
18+
void ftrace_nop_code(void);
19+
20+
#define FTRACE_INSN_SIZE 4
21+
22+
#ifdef CONFIG_64BIT
23+
24+
asm(
25+
" .align 4\n"
26+
"ftrace_disable_code:\n"
27+
" j 0f\n"
28+
" .word 0x0024\n"
29+
" lg %r1,"__stringify(__LC_FTRACE_FUNC)"\n"
30+
" basr %r14,%r1\n"
31+
" lg %r14,8(15)\n"
32+
" lgr %r0,%r0\n"
33+
"0:\n");
34+
35+
asm(
36+
" .align 4\n"
37+
"ftrace_nop_code:\n"
38+
" j .+"__stringify(MCOUNT_INSN_SIZE)"\n");
39+
40+
asm(
41+
" .align 4\n"
42+
"ftrace_call_code:\n"
43+
" stg %r14,8(%r15)\n");
44+
45+
#else /* CONFIG_64BIT */
46+
47+
asm(
48+
" .align 4\n"
49+
"ftrace_disable_code:\n"
50+
" j 0f\n"
51+
" l %r1,"__stringify(__LC_FTRACE_FUNC)"\n"
52+
" basr %r14,%r1\n"
53+
" l %r14,4(%r15)\n"
54+
" j 0f\n"
55+
" bcr 0,%r7\n"
56+
" bcr 0,%r7\n"
57+
" bcr 0,%r7\n"
58+
" bcr 0,%r7\n"
59+
" bcr 0,%r7\n"
60+
" bcr 0,%r7\n"
61+
"0:\n");
62+
63+
asm(
64+
" .align 4\n"
65+
"ftrace_nop_code:\n"
66+
" j .+"__stringify(MCOUNT_INSN_SIZE)"\n");
67+
68+
asm(
69+
" .align 4\n"
70+
"ftrace_call_code:\n"
71+
" st %r14,4(%r15)\n");
72+
73+
#endif /* CONFIG_64BIT */
74+
75+
static int ftrace_modify_code(unsigned long ip,
76+
void *old_code, int old_size,
77+
void *new_code, int new_size)
78+
{
79+
unsigned char replaced[MCOUNT_INSN_SIZE];
80+
81+
/*
82+
* Note: Due to modules code can disappear and change.
83+
* We need to protect against faulting as well as code
84+
* changing. We do this by using the probe_kernel_*
85+
* functions.
86+
* This however is just a simple sanity check.
87+
*/
88+
if (probe_kernel_read(replaced, (void *)ip, old_size))
89+
return -EFAULT;
90+
if (memcmp(replaced, old_code, old_size) != 0)
91+
return -EINVAL;
92+
if (probe_kernel_write((void *)ip, new_code, new_size))
93+
return -EPERM;
94+
return 0;
95+
}
96+
97+
static int ftrace_make_initial_nop(struct module *mod, struct dyn_ftrace *rec,
98+
unsigned long addr)
99+
{
100+
return ftrace_modify_code(rec->ip,
101+
ftrace_call_code, FTRACE_INSN_SIZE,
102+
ftrace_disable_code, MCOUNT_INSN_SIZE);
103+
}
104+
105+
int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec,
106+
unsigned long addr)
107+
{
108+
if (addr == MCOUNT_ADDR)
109+
return ftrace_make_initial_nop(mod, rec, addr);
110+
return ftrace_modify_code(rec->ip,
111+
ftrace_call_code, FTRACE_INSN_SIZE,
112+
ftrace_nop_code, FTRACE_INSN_SIZE);
113+
}
114+
115+
int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
116+
{
117+
return ftrace_modify_code(rec->ip,
118+
ftrace_nop_code, FTRACE_INSN_SIZE,
119+
ftrace_call_code, FTRACE_INSN_SIZE);
120+
}
121+
122+
int ftrace_update_ftrace_func(ftrace_func_t func)
123+
{
124+
ftrace_dyn_func = (unsigned long)func;
125+
return 0;
126+
}
127+
128+
int __init ftrace_dyn_arch_init(void *data)
129+
{
130+
*(unsigned long *)data = 0;
131+
return 0;
132+
}

‎arch/s390/kernel/mcount.S‎

Lines changed: 92 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,52 @@
11
/*
2-
* Copyright IBM Corp. 2008
2+
* Copyright IBM Corp. 2008,2009
33
*
44
* Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
55
*
66
*/
77

88
#include <asm/asm-offsets.h>
99

10-
#ifndef CONFIG_64BIT
11-
.globl _mcount
10+
.globl ftrace_stub
11+
ftrace_stub:
12+
br %r14
13+
14+
#ifdef CONFIG_64BIT
15+
16+
#ifdef CONFIG_DYNAMIC_FTRACE
17+
18+
.globl _mcount
1219
_mcount:
13-
stm %r0,%r5,8(%r15)
14-
st %r14,56(%r15)
15-
lr %r1,%r15
16-
ahi %r15,-96
17-
l %r3,100(%r15)
18-
la %r2,0(%r14)
19-
st %r1,__SF_BACKCHAIN(%r15)
20-
la %r3,0(%r3)
21-
bras %r14,0f
22-
.long ftrace_trace_function
23-
0: l %r14,0(%r14)
24-
l %r14,0(%r14)
25-
basr %r14,%r14
26-
ahi %r15,96
27-
lm %r0,%r5,8(%r15)
28-
l %r14,56(%r15)
2920
br %r14
3021

31-
.globl ftrace_stub
32-
ftrace_stub:
22+
.globl ftrace_caller
23+
ftrace_caller:
24+
stmg %r2,%r5,32(%r15)
25+
stg %r14,112(%r15)
26+
lgr %r1,%r15
27+
aghi %r15,-160
28+
stg %r1,__SF_BACKCHAIN(%r15)
29+
lgr %r2,%r14
30+
lg %r3,168(%r15)
31+
larl %r14,ftrace_dyn_func
32+
lg %r14,0(%r14)
33+
basr %r14,%r14
34+
aghi %r15,160
35+
lmg %r2,%r5,32(%r15)
36+
lg %r14,112(%r15)
3337
br %r14
3438

35-
#else /* CONFIG_64BIT */
39+
.data
40+
.globl ftrace_dyn_func
41+
ftrace_dyn_func:
42+
.quad ftrace_stub
43+
.previous
44+
45+
#else /* CONFIG_DYNAMIC_FTRACE */
3646

37-
.globl _mcount
47+
.globl _mcount
3848
_mcount:
39-
stmg %r0,%r5,16(%r15)
49+
stmg %r2,%r5,32(%r15)
4050
stg %r14,112(%r15)
4151
lgr %r1,%r15
4252
aghi %r15,-160
@@ -47,12 +57,67 @@ _mcount:
4757
lg %r14,0(%r14)
4858
basr %r14,%r14
4959
aghi %r15,160
50-
lmg %r0,%r5,16(%r15)
60+
lmg %r2,%r5,32(%r15)
5161
lg %r14,112(%r15)
5262
br %r14
5363

54-
.globl ftrace_stub
55-
ftrace_stub:
64+
#endif /* CONFIG_DYNAMIC_FTRACE */
65+
66+
#else /* CONFIG_64BIT */
67+
68+
#ifdef CONFIG_DYNAMIC_FTRACE
69+
70+
.globl _mcount
71+
_mcount:
72+
br %r14
73+
74+
.globl ftrace_caller
75+
ftrace_caller:
76+
stm %r2,%r5,16(%r15)
77+
st %r14,56(%r15)
78+
lr %r1,%r15
79+
ahi %r15,-96
80+
l %r3,100(%r15)
81+
la %r2,0(%r14)
82+
st %r1,__SF_BACKCHAIN(%r15)
83+
la %r3,0(%r3)
84+
bras %r14,0f
85+
.long ftrace_dyn_func
86+
0: l %r14,0(%r14)
87+
l %r14,0(%r14)
88+
basr %r14,%r14
89+
ahi %r15,96
90+
lm %r2,%r5,16(%r15)
91+
l %r14,56(%r15)
92+
br %r14
93+
94+
.data
95+
.globl ftrace_dyn_func
96+
ftrace_dyn_func:
97+
.long ftrace_stub
98+
.previous
99+
100+
#else /* CONFIG_DYNAMIC_FTRACE */
101+
102+
.globl _mcount
103+
_mcount:
104+
stm %r2,%r5,16(%r15)
105+
st %r14,56(%r15)
106+
lr %r1,%r15
107+
ahi %r15,-96
108+
l %r3,100(%r15)
109+
la %r2,0(%r14)
110+
st %r1,__SF_BACKCHAIN(%r15)
111+
la %r3,0(%r3)
112+
bras %r14,0f
113+
.long ftrace_trace_function
114+
0: l %r14,0(%r14)
115+
l %r14,0(%r14)
116+
basr %r14,%r14
117+
ahi %r15,96
118+
lm %r2,%r5,16(%r15)
119+
l %r14,56(%r15)
56120
br %r14
57121

122+
#endif /* CONFIG_DYNAMIC_FTRACE */
58123
#endif /* CONFIG_64BIT */

0 commit comments

Comments
 (0)