Skip to content

Commit c652d78

Browse files
mflemingpmundt
authored andcommitted
sh: Add ftrace syscall tracing support
Now that I've added TIF_SYSCALL_FTRACE the thread flags do not fit into a single byte any more. Code testing them now needs to be aware of the upper and lower bytes. Signed-off-by: Matt Fleming <matt@console-pimps.org> Signed-off-by: Paul Mundt <lethal@linux-sh.org>
1 parent c1340c0 commit c652d78

8 files changed

Lines changed: 100 additions & 10 deletions

File tree

‎arch/sh/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ config SUPERH32
3232
select HAVE_FTRACE_MCOUNT_RECORD
3333
select HAVE_DYNAMIC_FTRACE
3434
select HAVE_FUNCTION_TRACE_MCOUNT_TEST
35+
select HAVE_FTRACE_SYSCALLS
3536
select HAVE_ARCH_KGDB
3637
select ARCH_HIBERNATION_POSSIBLE if MMU
3738

‎arch/sh/include/asm/syscall_32.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ static inline void syscall_get_arguments(struct task_struct *task,
6565
case 3: args[2] = regs->regs[6];
6666
case 2: args[1] = regs->regs[5];
6767
case 1: args[0] = regs->regs[4];
68+
case 0:
6869
break;
6970
default:
7071
BUG();

‎arch/sh/include/asm/thread_info.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static inline struct thread_info *current_thread_info(void)
9797

9898
extern struct thread_info *alloc_thread_info(struct task_struct *tsk);
9999
extern void free_thread_info(struct thread_info *ti);
100-
100+
101101
#endif /* THREAD_SHIFT < PAGE_SHIFT */
102102

103103
#endif /* __ASSEMBLY__ */
@@ -116,6 +116,7 @@ extern void free_thread_info(struct thread_info *ti);
116116
#define TIF_SYSCALL_AUDIT 5 /* syscall auditing active */
117117
#define TIF_SECCOMP 6 /* secure computing */
118118
#define TIF_NOTIFY_RESUME 7 /* callback before returning to user */
119+
#define TIF_SYSCALL_FTRACE 8 /* for ftrace syscall instrumentation */
119120
#define TIF_USEDFPU 16 /* FPU was used by this task this quantum (SMP) */
120121
#define TIF_POLLING_NRFLAG 17 /* true if poll_idle() is polling TIF_NEED_RESCHED */
121122
#define TIF_MEMDIE 18
@@ -129,25 +130,27 @@ extern void free_thread_info(struct thread_info *ti);
129130
#define _TIF_SYSCALL_AUDIT (1 << TIF_SYSCALL_AUDIT)
130131
#define _TIF_SECCOMP (1 << TIF_SECCOMP)
131132
#define _TIF_NOTIFY_RESUME (1 << TIF_NOTIFY_RESUME)
133+
#define _TIF_SYSCALL_FTRACE (1 << TIF_SYSCALL_FTRACE)
132134
#define _TIF_USEDFPU (1 << TIF_USEDFPU)
133135
#define _TIF_POLLING_NRFLAG (1 << TIF_POLLING_NRFLAG)
134136
#define _TIF_FREEZE (1 << TIF_FREEZE)
135137

136138
/*
137-
* _TIF_ALLWORK_MASK and _TIF_WORK_MASK need to fit within a byte, or we
139+
* _TIF_ALLWORK_MASK and _TIF_WORK_MASK need to fit within 2 bytes, or we
138140
* blow the tst immediate size constraints and need to fix up
139141
* arch/sh/kernel/entry-common.S.
140142
*/
141143

142144
/* work to do in syscall trace */
143145
#define _TIF_WORK_SYSCALL_MASK (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP | \
144-
_TIF_SYSCALL_AUDIT | _TIF_SECCOMP)
146+
_TIF_SYSCALL_AUDIT | _TIF_SECCOMP | \
147+
_TIF_SYSCALL_FTRACE)
145148

146149
/* work to do on any return to u-space */
147150
#define _TIF_ALLWORK_MASK (_TIF_SYSCALL_TRACE | _TIF_SIGPENDING | \
148151
_TIF_NEED_RESCHED | _TIF_SYSCALL_AUDIT | \
149152
_TIF_SINGLESTEP | _TIF_RESTORE_SIGMASK | \
150-
_TIF_NOTIFY_RESUME)
153+
_TIF_NOTIFY_RESUME | _TIF_SYSCALL_FTRACE)
151154

152155
/* work to do on interrupt/exception return */
153156
#define _TIF_WORK_MASK (_TIF_ALLWORK_MASK & ~(_TIF_SYSCALL_TRACE | \

‎arch/sh/kernel/Makefile_32‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ obj-$(CONFIG_IO_TRAPPED) += io_trapped.o
2929
obj-$(CONFIG_KPROBES) += kprobes.o
3030
obj-$(CONFIG_GENERIC_GPIO) += gpio.o
3131
obj-$(CONFIG_DYNAMIC_FTRACE) += ftrace.o
32+
obj-$(CONFIG_FTRACE_SYSCALLS) += ftrace.o
3233
obj-$(CONFIG_DUMP_CODE) += disassemble.o
3334
obj-$(CONFIG_HIBERNATION) += swsusp.o
3435

‎arch/sh/kernel/entry-common.S‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ ENTRY(resume_userspace)
131131
nop
132132
#endif
133133
mov.l @(TI_FLAGS,r8), r0 ! current_thread_info->flags
134-
tst #_TIF_WORK_MASK, r0
134+
tst #(_TIF_WORK_MASK & 0xff), r0
135135
bt/s __restore_all
136136
tst #_TIF_NEED_RESCHED, r0
137137

@@ -163,7 +163,7 @@ work_resched:
163163
#endif
164164
!
165165
mov.l @(TI_FLAGS,r8), r0 ! current_thread_info->flags
166-
tst #_TIF_WORK_MASK, r0
166+
tst #(_TIF_WORK_MASK & 0xff), r0
167167
bt __restore_all
168168
bra work_pending
169169
tst #_TIF_NEED_RESCHED, r0
@@ -181,7 +181,7 @@ work_resched:
181181
syscall_exit_work:
182182
! r0: current_thread_info->flags
183183
! r8: current_thread_info
184-
tst #_TIF_WORK_SYSCALL_MASK, r0
184+
tst #(_TIF_WORK_SYSCALL_MASK & 0xff), r0
185185
bt/s work_pending
186186
tst #_TIF_NEED_RESCHED, r0
187187
#ifdef CONFIG_TRACE_IRQFLAGS
@@ -331,8 +331,12 @@ ENTRY(system_call)
331331
!
332332
get_current_thread_info r8, r10
333333
mov.l @(TI_FLAGS,r8), r8
334-
mov #_TIF_WORK_SYSCALL_MASK, r10
334+
mov #(_TIF_WORK_SYSCALL_MASK & 0xff), r10
335+
mov #(_TIF_WORK_SYSCALL_MASK >> 8), r9
335336
tst r10, r8
337+
shll8 r9
338+
bf syscall_trace_entry
339+
tst r9, r8
336340
bf syscall_trace_entry
337341
!
338342
mov.l 2f, r8 ! Number of syscalls
@@ -359,7 +363,11 @@ syscall_exit:
359363
!
360364
get_current_thread_info r8, r0
361365
mov.l @(TI_FLAGS,r8), r0 ! current_thread_info->flags
362-
tst #_TIF_ALLWORK_MASK, r0
366+
tst #(_TIF_ALLWORK_MASK & 0xff), r0
367+
mov #(_TIF_ALLWORK_MASK >> 8), r1
368+
bf syscall_exit_work
369+
shlr8 r0
370+
tst r0, r1
363371
bf syscall_exit_work
364372
bra __restore_all
365373
nop

‎arch/sh/kernel/ftrace.c‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <linux/io.h>
1919
#include <asm/ftrace.h>
2020
#include <asm/cacheflush.h>
21+
#include <asm/unistd.h>
22+
#include <trace/syscall.h>
2123

2224
static unsigned char ftrace_replaced_code[MCOUNT_INSN_SIZE];
2325

@@ -131,3 +133,69 @@ int __init ftrace_dyn_arch_init(void *data)
131133

132134
return 0;
133135
}
136+
137+
#ifdef CONFIG_FTRACE_SYSCALLS
138+
139+
extern unsigned long __start_syscalls_metadata[];
140+
extern unsigned long __stop_syscalls_metadata[];
141+
extern unsigned long *sys_call_table;
142+
143+
static struct syscall_metadata **syscalls_metadata;
144+
145+
static struct syscall_metadata *find_syscall_meta(unsigned long *syscall)
146+
{
147+
struct syscall_metadata *start;
148+
struct syscall_metadata *stop;
149+
char str[KSYM_SYMBOL_LEN];
150+
151+
152+
start = (struct syscall_metadata *)__start_syscalls_metadata;
153+
stop = (struct syscall_metadata *)__stop_syscalls_metadata;
154+
kallsyms_lookup((unsigned long) syscall, NULL, NULL, NULL, str);
155+
156+
for ( ; start < stop; start++) {
157+
if (start->name && !strcmp(start->name, str))
158+
return start;
159+
}
160+
161+
return NULL;
162+
}
163+
164+
#define FTRACE_SYSCALL_MAX (NR_syscalls - 1)
165+
166+
struct syscall_metadata *syscall_nr_to_meta(int nr)
167+
{
168+
if (!syscalls_metadata || nr >= FTRACE_SYSCALL_MAX || nr < 0)
169+
return NULL;
170+
171+
return syscalls_metadata[nr];
172+
}
173+
174+
void arch_init_ftrace_syscalls(void)
175+
{
176+
int i;
177+
struct syscall_metadata *meta;
178+
unsigned long **psys_syscall_table = &sys_call_table;
179+
static atomic_t refs;
180+
181+
if (atomic_inc_return(&refs) != 1)
182+
goto end;
183+
184+
syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
185+
FTRACE_SYSCALL_MAX, GFP_KERNEL);
186+
if (!syscalls_metadata) {
187+
WARN_ON(1);
188+
return;
189+
}
190+
191+
for (i = 0; i < FTRACE_SYSCALL_MAX; i++) {
192+
meta = find_syscall_meta(psys_syscall_table[i]);
193+
syscalls_metadata[i] = meta;
194+
}
195+
return;
196+
197+
/* Paranoid: avoid overflow */
198+
end:
199+
atomic_dec(&refs);
200+
}
201+
#endif /* CONFIG_FTRACE_SYSCALLS */

‎arch/sh/kernel/ptrace_32.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <asm/syscalls.h>
3535
#include <asm/fpu.h>
3636

37+
#include <trace/syscall.h>
38+
3739
/*
3840
* This routine will get a word off of the process kernel stack.
3941
*/
@@ -459,6 +461,9 @@ asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
459461
*/
460462
ret = -1L;
461463

464+
if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
465+
ftrace_syscall_enter(regs);
466+
462467
if (unlikely(current->audit_context))
463468
audit_syscall_entry(audit_arch(), regs->regs[3],
464469
regs->regs[4], regs->regs[5],
@@ -475,6 +480,9 @@ asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
475480
audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
476481
regs->regs[0]);
477482

483+
if (unlikely(test_thread_flag(TIF_SYSCALL_FTRACE)))
484+
ftrace_syscall_exit(regs);
485+
478486
step = test_thread_flag(TIF_SINGLESTEP);
479487
if (step || test_thread_flag(TIF_SYSCALL_TRACE))
480488
tracehook_report_syscall_exit(regs, step);

‎arch/sh/lib/mcount.S‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ ftrace_caller:
7272
mov.l @r0, r0
7373
tst r0, r0
7474
bf ftrace_stub
75-
75+
7676
MCOUNT_ENTER()
7777

7878
.globl ftrace_call

0 commit comments

Comments
 (0)