blob: 33c2a5b769dc45ea69f61ec79b865e5bf93fad93 [file] [log] [blame]
Frederic Weisbecker47788c52009-04-08 20:40:59 +02001#include <trace/syscall.h>
Josh Stone1c569f02009-08-24 14:43:14 -07002#include <trace/events/syscalls.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01003#include <linux/kernel.h>
Jason Baronfb34a082009-08-10 16:52:47 -04004#include <linux/ftrace.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +02005#include <linux/perf_event.h>
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +01006#include <asm/syscall.h>
7
8#include "trace_output.h"
9#include "trace.h"
10
Frederic Weisbecker5be71b62009-03-15 22:10:37 +010011static DEFINE_MUTEX(syscall_trace_lock);
Jason Baronfb34a082009-08-10 16:52:47 -040012static int sys_refcount_enter;
13static int sys_refcount_exit;
Jason Baron57421db2009-08-24 17:40:22 -040014static DECLARE_BITMAP(enabled_enter_syscalls, NR_syscalls);
15static DECLARE_BITMAP(enabled_exit_syscalls, NR_syscalls);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +010016
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +020017extern unsigned long __start_syscalls_metadata[];
18extern unsigned long __stop_syscalls_metadata[];
19
20static struct syscall_metadata **syscalls_metadata;
21
22static struct syscall_metadata *find_syscall_meta(unsigned long syscall)
23{
24 struct syscall_metadata *start;
25 struct syscall_metadata *stop;
26 char str[KSYM_SYMBOL_LEN];
27
28
29 start = (struct syscall_metadata *)__start_syscalls_metadata;
30 stop = (struct syscall_metadata *)__stop_syscalls_metadata;
31 kallsyms_lookup(syscall, NULL, NULL, NULL, str);
32
33 for ( ; start < stop; start++) {
34 /*
35 * Only compare after the "sys" prefix. Archs that use
36 * syscall wrappers may have syscalls symbols aliases prefixed
37 * with "SyS" instead of "sys", leading to an unwanted
38 * mismatch.
39 */
40 if (start->name && !strcmp(start->name + 3, str + 3))
41 return start;
42 }
43 return NULL;
44}
45
46static struct syscall_metadata *syscall_nr_to_meta(int nr)
47{
48 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
49 return NULL;
50
51 return syscalls_metadata[nr];
52}
53
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010054enum print_line_t
55print_syscall_enter(struct trace_iterator *iter, int flags)
56{
57 struct trace_seq *s = &iter->seq;
58 struct trace_entry *ent = iter->ent;
59 struct syscall_trace_enter *trace;
60 struct syscall_metadata *entry;
61 int i, ret, syscall;
62
Jason Baron64c12e02009-08-10 16:52:53 -040063 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010064 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010065 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -040066
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010067 if (!entry)
68 goto end;
69
Lai Jiangshanfcc19432009-12-01 16:23:36 +080070 if (entry->enter_event->id != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -040071 WARN_ON_ONCE(1);
72 goto end;
73 }
74
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010075 ret = trace_seq_printf(s, "%s(", entry->name);
76 if (!ret)
77 return TRACE_TYPE_PARTIAL_LINE;
78
79 for (i = 0; i < entry->nb_args; i++) {
80 /* parameter types */
Li Zefanba8b3a42009-08-17 16:55:18 +080081 if (trace_flags & TRACE_ITER_VERBOSE) {
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010082 ret = trace_seq_printf(s, "%s ", entry->types[i]);
83 if (!ret)
84 return TRACE_TYPE_PARTIAL_LINE;
85 }
86 /* parameter values */
Li Zefan4539f072009-08-20 16:13:35 +080087 ret = trace_seq_printf(s, "%s: %lx%s", entry->args[i],
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010088 trace->args[i],
Li Zefan4539f072009-08-20 16:13:35 +080089 i == entry->nb_args - 1 ? "" : ", ");
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010090 if (!ret)
91 return TRACE_TYPE_PARTIAL_LINE;
92 }
93
Li Zefan4539f072009-08-20 16:13:35 +080094 ret = trace_seq_putc(s, ')');
95 if (!ret)
96 return TRACE_TYPE_PARTIAL_LINE;
97
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +010098end:
Li Zefan4539f072009-08-20 16:13:35 +080099 ret = trace_seq_putc(s, '\n');
100 if (!ret)
101 return TRACE_TYPE_PARTIAL_LINE;
102
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100103 return TRACE_TYPE_HANDLED;
104}
105
106enum print_line_t
107print_syscall_exit(struct trace_iterator *iter, int flags)
108{
109 struct trace_seq *s = &iter->seq;
110 struct trace_entry *ent = iter->ent;
111 struct syscall_trace_exit *trace;
112 int syscall;
113 struct syscall_metadata *entry;
114 int ret;
115
Jason Baron64c12e02009-08-10 16:52:53 -0400116 trace = (typeof(trace))ent;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100117 syscall = trace->nr;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100118 entry = syscall_nr_to_meta(syscall);
Jason Baron64c12e02009-08-10 16:52:53 -0400119
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100120 if (!entry) {
121 trace_seq_printf(s, "\n");
122 return TRACE_TYPE_HANDLED;
123 }
124
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800125 if (entry->exit_event->id != ent->type) {
Jason Baron64c12e02009-08-10 16:52:53 -0400126 WARN_ON_ONCE(1);
127 return TRACE_TYPE_UNHANDLED;
128 }
129
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100130 ret = trace_seq_printf(s, "%s -> 0x%lx\n", entry->name,
131 trace->ret);
132 if (!ret)
133 return TRACE_TYPE_PARTIAL_LINE;
134
135 return TRACE_TYPE_HANDLED;
136}
137
Li Zefane6971962009-08-19 15:52:25 +0800138extern char *__bad_type_size(void);
139
140#define SYSCALL_FIELD(type, name) \
141 sizeof(type) != sizeof(trace.name) ? \
142 __bad_type_size() : \
Tom Zanussi26a50742009-10-06 01:09:50 -0500143 #type, #name, offsetof(typeof(trace), name), \
144 sizeof(trace.name), is_signed_type(type)
Li Zefane6971962009-08-19 15:52:25 +0800145
Lai Jiangshan50307a42009-12-15 15:39:45 +0800146static
147int __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len)
148{
149 int i;
150 int pos = 0;
151
152 /* When len=0, we just calculate the needed length */
153#define LEN_OR_ZERO (len ? len - pos : 0)
154
155 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
156 for (i = 0; i < entry->nb_args; i++) {
157 pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s",
158 entry->args[i], sizeof(unsigned long),
159 i == entry->nb_args - 1 ? "" : ", ");
160 }
161 pos += snprintf(buf + pos, LEN_OR_ZERO, "\"");
162
163 for (i = 0; i < entry->nb_args; i++) {
164 pos += snprintf(buf + pos, LEN_OR_ZERO,
165 ", ((unsigned long)(REC->%s))", entry->args[i]);
166 }
167
168#undef LEN_OR_ZERO
169
170 /* return the length of print_fmt */
171 return pos;
172}
173
174static int set_syscall_print_fmt(struct ftrace_event_call *call)
175{
176 char *print_fmt;
177 int len;
178 struct syscall_metadata *entry = call->data;
179
180 if (entry->enter_event != call) {
181 call->print_fmt = "\"0x%lx\", REC->ret";
182 return 0;
183 }
184
185 /* First: called with 0 length to calculate the needed length */
186 len = __set_enter_print_fmt(entry, NULL, 0);
187
188 print_fmt = kmalloc(len + 1, GFP_KERNEL);
189 if (!print_fmt)
190 return -ENOMEM;
191
192 /* Second: actually write the @print_fmt */
193 __set_enter_print_fmt(entry, print_fmt, len + 1);
194 call->print_fmt = print_fmt;
195
196 return 0;
197}
198
199static void free_syscall_print_fmt(struct ftrace_event_call *call)
200{
201 struct syscall_metadata *entry = call->data;
202
203 if (entry->enter_event == call)
204 kfree(call->print_fmt);
205}
206
Li Zefan540b7b82009-08-19 15:54:51 +0800207int syscall_enter_define_fields(struct ftrace_event_call *call)
208{
209 struct syscall_trace_enter trace;
Lai Jiangshan31c16b12009-12-01 16:23:30 +0800210 struct syscall_metadata *meta = call->data;
Li Zefan540b7b82009-08-19 15:54:51 +0800211 int ret;
Li Zefan540b7b82009-08-19 15:54:51 +0800212 int i;
213 int offset = offsetof(typeof(trace), args);
214
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800215 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
216 if (ret)
217 return ret;
218
Li Zefan540b7b82009-08-19 15:54:51 +0800219 for (i = 0; i < meta->nb_args; i++) {
Frederic Weisbeckeraeaeae12009-08-27 05:09:51 +0200220 ret = trace_define_field(call, meta->types[i],
221 meta->args[i], offset,
Li Zefan43b51ea2009-08-07 10:33:22 +0800222 sizeof(unsigned long), 0,
223 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800224 offset += sizeof(unsigned long);
225 }
226
227 return ret;
228}
229
230int syscall_exit_define_fields(struct ftrace_event_call *call)
231{
232 struct syscall_trace_exit trace;
233 int ret;
234
Lai Jiangshan0f1ef512009-11-26 15:49:33 +0800235 ret = trace_define_field(call, SYSCALL_FIELD(int, nr), FILTER_OTHER);
236 if (ret)
237 return ret;
238
Tom Zanussi26a50742009-10-06 01:09:50 -0500239 ret = trace_define_field(call, SYSCALL_FIELD(long, ret),
Li Zefan43b51ea2009-08-07 10:33:22 +0800240 FILTER_OTHER);
Li Zefan540b7b82009-08-19 15:54:51 +0800241
242 return ret;
243}
244
Jason Baronfb34a082009-08-10 16:52:47 -0400245void ftrace_syscall_enter(struct pt_regs *regs, long id)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100246{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100247 struct syscall_trace_enter *entry;
248 struct syscall_metadata *sys_data;
249 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400250 struct ring_buffer *buffer;
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100251 int size;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100252 int syscall_nr;
253
254 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200255 if (syscall_nr < 0)
256 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400257 if (!test_bit(syscall_nr, enabled_enter_syscalls))
258 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100259
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100260 sys_data = syscall_nr_to_meta(syscall_nr);
261 if (!sys_data)
262 return;
263
264 size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args;
265
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800266 event = trace_current_buffer_lock_reserve(&buffer,
267 sys_data->enter_event->id, size, 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100268 if (!event)
269 return;
270
271 entry = ring_buffer_event_data(event);
272 entry->nr = syscall_nr;
273 syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args);
274
Steven Rostedte77405a2009-09-02 14:17:06 -0400275 if (!filter_current_check_discard(buffer, sys_data->enter_event,
276 entry, event))
277 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100278}
279
Jason Baronfb34a082009-08-10 16:52:47 -0400280void ftrace_syscall_exit(struct pt_regs *regs, long ret)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100281{
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100282 struct syscall_trace_exit *entry;
283 struct syscall_metadata *sys_data;
284 struct ring_buffer_event *event;
Steven Rostedte77405a2009-09-02 14:17:06 -0400285 struct ring_buffer *buffer;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100286 int syscall_nr;
287
288 syscall_nr = syscall_get_nr(current, regs);
Hendrik Bruecknercd0980f2009-08-25 14:50:27 +0200289 if (syscall_nr < 0)
290 return;
Jason Baronfb34a082009-08-10 16:52:47 -0400291 if (!test_bit(syscall_nr, enabled_exit_syscalls))
292 return;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100293
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100294 sys_data = syscall_nr_to_meta(syscall_nr);
295 if (!sys_data)
296 return;
297
Lai Jiangshanfcc19432009-12-01 16:23:36 +0800298 event = trace_current_buffer_lock_reserve(&buffer,
299 sys_data->exit_event->id, sizeof(*entry), 0, 0);
Frederic Weisbeckerbed1ffc2009-03-13 15:42:11 +0100300 if (!event)
301 return;
302
303 entry = ring_buffer_event_data(event);
304 entry->nr = syscall_nr;
305 entry->ret = syscall_get_return_value(current, regs);
306
Steven Rostedte77405a2009-09-02 14:17:06 -0400307 if (!filter_current_check_discard(buffer, sys_data->exit_event,
308 entry, event))
309 trace_current_buffer_unlock_commit(buffer, event, 0, 0);
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100310}
311
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400312int reg_event_syscall_enter(struct ftrace_event_call *call)
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100313{
Jason Baronfb34a082009-08-10 16:52:47 -0400314 int ret = 0;
315 int num;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100316
Lai Jiangshanc252f652009-12-01 16:23:47 +0800317 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400318 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400319 return -ENOSYS;
320 mutex_lock(&syscall_trace_lock);
321 if (!sys_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700322 ret = register_trace_sys_enter(ftrace_syscall_enter);
Li Zefan3b8e4272009-12-08 11:14:52 +0800323 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400324 set_bit(num, enabled_enter_syscalls);
325 sys_refcount_enter++;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100326 }
Jason Baronfb34a082009-08-10 16:52:47 -0400327 mutex_unlock(&syscall_trace_lock);
328 return ret;
Frederic Weisbeckeree08c6e2009-03-07 05:52:59 +0100329}
Jason Baronfb34a082009-08-10 16:52:47 -0400330
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400331void unreg_event_syscall_enter(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400332{
333 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400334
Lai Jiangshanc252f652009-12-01 16:23:47 +0800335 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400336 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400337 return;
338 mutex_lock(&syscall_trace_lock);
339 sys_refcount_enter--;
340 clear_bit(num, enabled_enter_syscalls);
341 if (!sys_refcount_enter)
Josh Stone1c569f02009-08-24 14:43:14 -0700342 unregister_trace_sys_enter(ftrace_syscall_enter);
Jason Baronfb34a082009-08-10 16:52:47 -0400343 mutex_unlock(&syscall_trace_lock);
344}
345
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400346int reg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400347{
348 int ret = 0;
349 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400350
Lai Jiangshanc252f652009-12-01 16:23:47 +0800351 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400352 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400353 return -ENOSYS;
354 mutex_lock(&syscall_trace_lock);
355 if (!sys_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700356 ret = register_trace_sys_exit(ftrace_syscall_exit);
Li Zefan3b8e4272009-12-08 11:14:52 +0800357 if (!ret) {
Jason Baronfb34a082009-08-10 16:52:47 -0400358 set_bit(num, enabled_exit_syscalls);
359 sys_refcount_exit++;
360 }
361 mutex_unlock(&syscall_trace_lock);
362 return ret;
363}
364
Masami Hiramatsubd1a5c82009-08-13 16:34:53 -0400365void unreg_event_syscall_exit(struct ftrace_event_call *call)
Jason Baronfb34a082009-08-10 16:52:47 -0400366{
367 int num;
Jason Baronfb34a082009-08-10 16:52:47 -0400368
Lai Jiangshanc252f652009-12-01 16:23:47 +0800369 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baron57421db2009-08-24 17:40:22 -0400370 if (num < 0 || num >= NR_syscalls)
Jason Baronfb34a082009-08-10 16:52:47 -0400371 return;
372 mutex_lock(&syscall_trace_lock);
373 sys_refcount_exit--;
374 clear_bit(num, enabled_exit_syscalls);
375 if (!sys_refcount_exit)
Josh Stone1c569f02009-08-24 14:43:14 -0700376 unregister_trace_sys_exit(ftrace_syscall_exit);
Jason Baronfb34a082009-08-10 16:52:47 -0400377 mutex_unlock(&syscall_trace_lock);
378}
379
Lai Jiangshana1301da2009-12-01 16:23:55 +0800380int init_syscall_trace(struct ftrace_event_call *call)
381{
382 int id;
383
Lai Jiangshan50307a42009-12-15 15:39:45 +0800384 if (set_syscall_print_fmt(call) < 0)
385 return -ENOMEM;
386
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500387 id = trace_event_raw_init(call);
388
389 if (id < 0) {
Lai Jiangshan50307a42009-12-15 15:39:45 +0800390 free_syscall_print_fmt(call);
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500391 return id;
Lai Jiangshan50307a42009-12-15 15:39:45 +0800392 }
Steven Rostedtc7ef3a92009-12-28 21:13:59 -0500393
394 return id;
Lai Jiangshana1301da2009-12-01 16:23:55 +0800395}
396
Mike Frysingere7b8e672010-01-26 04:40:03 -0500397unsigned long __init arch_syscall_addr(int nr)
398{
399 return (unsigned long)sys_call_table[nr];
400}
401
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200402int __init init_ftrace_syscalls(void)
403{
404 struct syscall_metadata *meta;
405 unsigned long addr;
406 int i;
407
408 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
409 NR_syscalls, GFP_KERNEL);
410 if (!syscalls_metadata) {
411 WARN_ON(1);
412 return -ENOMEM;
413 }
414
415 for (i = 0; i < NR_syscalls; i++) {
416 addr = arch_syscall_addr(i);
417 meta = find_syscall_meta(addr);
Lai Jiangshanc252f652009-12-01 16:23:47 +0800418 if (!meta)
419 continue;
420
421 meta->syscall_nr = i;
Frederic Weisbeckerc44fc772009-09-19 06:50:42 +0200422 syscalls_metadata[i] = meta;
423 }
424
425 return 0;
426}
427core_initcall(init_ftrace_syscalls);
428
Li Zefan07b139c2009-12-21 14:27:35 +0800429#ifdef CONFIG_PERF_EVENTS
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200430
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100431static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls);
432static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls);
433static int sys_perf_refcount_enter;
434static int sys_perf_refcount_exit;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400435
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100436static void perf_syscall_enter(struct pt_regs *regs, long id)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400437{
438 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200439 struct syscall_trace_enter *rec;
440 unsigned long flags;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400441 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100442 int rctx;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200443 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400444
445 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100446 if (!test_bit(syscall_nr, enabled_perf_enter_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400447 return;
448
449 sys_data = syscall_nr_to_meta(syscall_nr);
450 if (!sys_data)
451 return;
452
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200453 /* get the size after alignment with the u32 buffer size field */
454 size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec);
455 size = ALIGN(size + sizeof(u32), sizeof(u64));
456 size -= sizeof(u32);
457
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100458 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
459 "perf buffer not large enough"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200460 return;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200461
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100462 rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size,
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800463 sys_data->enter_event->id, &rctx, &flags);
464 if (!rec)
465 return;
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200466
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200467 rec->nr = syscall_nr;
468 syscall_get_arguments(current, regs, 0, sys_data->nb_args,
469 (unsigned long *)&rec->args);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100470 perf_trace_buf_submit(rec, size, rctx, 0, 1, flags, regs);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400471}
472
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100473int perf_sysenter_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400474{
475 int ret = 0;
476 int num;
477
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800478 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400479
480 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100481 if (!sys_perf_refcount_enter)
482 ret = register_trace_sys_enter(perf_syscall_enter);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400483 if (ret) {
484 pr_info("event trace: Could not activate"
485 "syscall entry trace point");
486 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100487 set_bit(num, enabled_perf_enter_syscalls);
488 sys_perf_refcount_enter++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400489 }
490 mutex_unlock(&syscall_trace_lock);
491 return ret;
492}
493
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100494void perf_sysenter_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400495{
496 int num;
497
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800498 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400499
500 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100501 sys_perf_refcount_enter--;
502 clear_bit(num, enabled_perf_enter_syscalls);
503 if (!sys_perf_refcount_enter)
504 unregister_trace_sys_enter(perf_syscall_enter);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400505 mutex_unlock(&syscall_trace_lock);
506}
507
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100508static void perf_syscall_exit(struct pt_regs *regs, long ret)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400509{
510 struct syscall_metadata *sys_data;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200511 struct syscall_trace_exit *rec;
512 unsigned long flags;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400513 int syscall_nr;
Peter Zijlstra4ed7c922009-11-23 11:37:29 +0100514 int rctx;
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200515 int size;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400516
517 syscall_nr = syscall_get_nr(current, regs);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100518 if (!test_bit(syscall_nr, enabled_perf_exit_syscalls))
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400519 return;
520
521 sys_data = syscall_nr_to_meta(syscall_nr);
522 if (!sys_data)
523 return;
524
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200525 /* We can probably do that at build time */
526 size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64));
527 size -= sizeof(u32);
Frederic Weisbecker19007a62009-08-11 20:22:53 +0200528
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200529 /*
530 * Impossible, but be paranoid with the future
531 * How to put this check outside runtime?
532 */
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100533 if (WARN_ONCE(size > PERF_MAX_TRACE_SIZE,
534 "exit event has grown above perf buffer size"))
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200535 return;
536
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100537 rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size,
Xiao Guangrong430ad5a2010-01-28 09:32:29 +0800538 sys_data->exit_event->id, &rctx, &flags);
539 if (!rec)
540 return;
Frederic Weisbeckerce71b9d2009-11-22 05:26:55 +0100541
Frederic Weisbecker20ab44252009-09-18 06:10:28 +0200542 rec->nr = syscall_nr;
543 rec->ret = syscall_get_return_value(current, regs);
544
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100545 perf_trace_buf_submit(rec, size, rctx, 0, 1, flags, regs);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400546}
547
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100548int perf_sysexit_enable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400549{
550 int ret = 0;
551 int num;
552
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800553 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400554
555 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100556 if (!sys_perf_refcount_exit)
557 ret = register_trace_sys_exit(perf_syscall_exit);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400558 if (ret) {
559 pr_info("event trace: Could not activate"
Wenji Huang65746582010-02-24 15:40:22 +0800560 "syscall exit trace point");
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400561 } else {
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100562 set_bit(num, enabled_perf_exit_syscalls);
563 sys_perf_refcount_exit++;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400564 }
565 mutex_unlock(&syscall_trace_lock);
566 return ret;
567}
568
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100569void perf_sysexit_disable(struct ftrace_event_call *call)
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400570{
571 int num;
572
Lai Jiangshan3bbe84e2009-12-01 16:24:01 +0800573 num = ((struct syscall_metadata *)call->data)->syscall_nr;
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400574
575 mutex_lock(&syscall_trace_lock);
Frederic Weisbecker97d5a222010-03-05 05:35:37 +0100576 sys_perf_refcount_exit--;
577 clear_bit(num, enabled_perf_exit_syscalls);
578 if (!sys_perf_refcount_exit)
579 unregister_trace_sys_exit(perf_syscall_exit);
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400580 mutex_unlock(&syscall_trace_lock);
581}
582
Li Zefan07b139c2009-12-21 14:27:35 +0800583#endif /* CONFIG_PERF_EVENTS */
Jason Baronf4b5ffc2009-08-10 16:53:02 -0400584