]>
Commit | Line | Data |
---|---|---|
47788c58 | 1 | #include <trace/syscall.h> |
1c569f02 | 2 | #include <trace/events/syscalls.h> |
f431b634 | 3 | #include <linux/syscalls.h> |
5a0e3ad6 | 4 | #include <linux/slab.h> |
ee08c6ec | 5 | #include <linux/kernel.h> |
56d82e00 | 6 | #include <linux/module.h> /* for MODULE_NAME_LEN via KSYM_SYMBOL_LEN */ |
fb34a08c | 7 | #include <linux/ftrace.h> |
cdd6c482 | 8 | #include <linux/perf_event.h> |
ee08c6ec FW |
9 | #include <asm/syscall.h> |
10 | ||
11 | #include "trace_output.h" | |
12 | #include "trace.h" | |
13 | ||
5be71b61 | 14 | static DEFINE_MUTEX(syscall_trace_lock); |
ee08c6ec | 15 | |
2425bcb9 | 16 | static int syscall_enter_register(struct trace_event_call *event, |
ceec0b6f | 17 | enum trace_reg type, void *data); |
2425bcb9 | 18 | static int syscall_exit_register(struct trace_event_call *event, |
ceec0b6f | 19 | enum trace_reg type, void *data); |
2239291a | 20 | |
2e33af02 | 21 | static struct list_head * |
2425bcb9 | 22 | syscall_get_enter_fields(struct trace_event_call *call) |
2e33af02 SR |
23 | { |
24 | struct syscall_metadata *entry = call->data; | |
25 | ||
26 | return &entry->enter_fields; | |
27 | } | |
28 | ||
3d56e331 SR |
29 | extern struct syscall_metadata *__start_syscalls_metadata[]; |
30 | extern struct syscall_metadata *__stop_syscalls_metadata[]; | |
c44fc770 FW |
31 | |
32 | static struct syscall_metadata **syscalls_metadata; | |
33 | ||
b2d55496 IM |
34 | #ifndef ARCH_HAS_SYSCALL_MATCH_SYM_NAME |
35 | static inline bool arch_syscall_match_sym_name(const char *sym, const char *name) | |
36 | { | |
37 | /* | |
38 | * Only compare after the "sys" prefix. Archs that use | |
39 | * syscall wrappers may have syscalls symbols aliases prefixed | |
36a78e9e | 40 | * with ".SyS" or ".sys" instead of "sys", leading to an unwanted |
b2d55496 IM |
41 | * mismatch. |
42 | */ | |
43 | return !strcmp(sym + 3, name + 3); | |
44 | } | |
45 | #endif | |
46 | ||
f431b634 SR |
47 | #ifdef ARCH_TRACE_IGNORE_COMPAT_SYSCALLS |
48 | /* | |
49 | * Some architectures that allow for 32bit applications | |
50 | * to run on a 64bit kernel, do not map the syscalls for | |
51 | * the 32bit tasks the same as they do for 64bit tasks. | |
52 | * | |
53 | * *cough*x86*cough* | |
54 | * | |
55 | * In such a case, instead of reporting the wrong syscalls, | |
56 | * simply ignore them. | |
57 | * | |
58 | * For an arch to ignore the compat syscalls it needs to | |
59 | * define ARCH_TRACE_IGNORE_COMPAT_SYSCALLS as well as | |
60 | * define the function arch_trace_is_compat_syscall() to let | |
61 | * the tracing system know that it should ignore it. | |
62 | */ | |
63 | static int | |
64 | trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs) | |
65 | { | |
66 | if (unlikely(arch_trace_is_compat_syscall(regs))) | |
67 | return -1; | |
68 | ||
69 | return syscall_get_nr(task, regs); | |
70 | } | |
71 | #else | |
72 | static inline int | |
73 | trace_get_syscall_nr(struct task_struct *task, struct pt_regs *regs) | |
74 | { | |
75 | return syscall_get_nr(task, regs); | |
76 | } | |
77 | #endif /* ARCH_TRACE_IGNORE_COMPAT_SYSCALLS */ | |
78 | ||
3d56e331 SR |
79 | static __init struct syscall_metadata * |
80 | find_syscall_meta(unsigned long syscall) | |
c44fc770 | 81 | { |
3d56e331 SR |
82 | struct syscall_metadata **start; |
83 | struct syscall_metadata **stop; | |
c44fc770 FW |
84 | char str[KSYM_SYMBOL_LEN]; |
85 | ||
86 | ||
3d56e331 SR |
87 | start = __start_syscalls_metadata; |
88 | stop = __stop_syscalls_metadata; | |
c44fc770 FW |
89 | kallsyms_lookup(syscall, NULL, NULL, NULL, str); |
90 | ||
ae07f551 IM |
91 | if (arch_syscall_match_sym_name(str, "sys_ni_syscall")) |
92 | return NULL; | |
93 | ||
c44fc770 | 94 | for ( ; start < stop; start++) { |
b2d55496 | 95 | if ((*start)->name && arch_syscall_match_sym_name(str, (*start)->name)) |
3d56e331 | 96 | return *start; |
c44fc770 FW |
97 | } |
98 | return NULL; | |
99 | } | |
100 | ||
101 | static struct syscall_metadata *syscall_nr_to_meta(int nr) | |
102 | { | |
103 | if (!syscalls_metadata || nr >= NR_syscalls || nr < 0) | |
104 | return NULL; | |
105 | ||
106 | return syscalls_metadata[nr]; | |
107 | } | |
108 | ||
6aea49cb | 109 | static enum print_line_t |
a9a57763 SR |
110 | print_syscall_enter(struct trace_iterator *iter, int flags, |
111 | struct trace_event *event) | |
bed1ffca | 112 | { |
983f938a | 113 | struct trace_array *tr = iter->tr; |
bed1ffca FW |
114 | struct trace_seq *s = &iter->seq; |
115 | struct trace_entry *ent = iter->ent; | |
116 | struct syscall_trace_enter *trace; | |
117 | struct syscall_metadata *entry; | |
183742f0 | 118 | int i, syscall; |
bed1ffca | 119 | |
64c12e04 | 120 | trace = (typeof(trace))ent; |
bed1ffca | 121 | syscall = trace->nr; |
bed1ffca | 122 | entry = syscall_nr_to_meta(syscall); |
64c12e04 | 123 | |
bed1ffca FW |
124 | if (!entry) |
125 | goto end; | |
126 | ||
32c0edae | 127 | if (entry->enter_event->event.type != ent->type) { |
64c12e04 JB |
128 | WARN_ON_ONCE(1); |
129 | goto end; | |
130 | } | |
131 | ||
183742f0 | 132 | trace_seq_printf(s, "%s(", entry->name); |
bed1ffca FW |
133 | |
134 | for (i = 0; i < entry->nb_args; i++) { | |
183742f0 SRRH |
135 | |
136 | if (trace_seq_has_overflowed(s)) | |
137 | goto end; | |
138 | ||
bed1ffca | 139 | /* parameter types */ |
983f938a | 140 | if (tr->trace_flags & TRACE_ITER_VERBOSE) |
183742f0 SRRH |
141 | trace_seq_printf(s, "%s ", entry->types[i]); |
142 | ||
bed1ffca | 143 | /* parameter values */ |
183742f0 SRRH |
144 | trace_seq_printf(s, "%s: %lx%s", entry->args[i], |
145 | trace->args[i], | |
146 | i == entry->nb_args - 1 ? "" : ", "); | |
bed1ffca FW |
147 | } |
148 | ||
183742f0 | 149 | trace_seq_putc(s, ')'); |
bed1ffca | 150 | end: |
183742f0 | 151 | trace_seq_putc(s, '\n'); |
4539f077 | 152 | |
183742f0 | 153 | return trace_handle_return(s); |
bed1ffca FW |
154 | } |
155 | ||
6aea49cb | 156 | static enum print_line_t |
a9a57763 SR |
157 | print_syscall_exit(struct trace_iterator *iter, int flags, |
158 | struct trace_event *event) | |
bed1ffca FW |
159 | { |
160 | struct trace_seq *s = &iter->seq; | |
161 | struct trace_entry *ent = iter->ent; | |
162 | struct syscall_trace_exit *trace; | |
163 | int syscall; | |
164 | struct syscall_metadata *entry; | |
bed1ffca | 165 | |
64c12e04 | 166 | trace = (typeof(trace))ent; |
bed1ffca | 167 | syscall = trace->nr; |
bed1ffca | 168 | entry = syscall_nr_to_meta(syscall); |
64c12e04 | 169 | |
bed1ffca | 170 | if (!entry) { |
146c3442 | 171 | trace_seq_putc(s, '\n'); |
183742f0 | 172 | goto out; |
bed1ffca FW |
173 | } |
174 | ||
32c0edae | 175 | if (entry->exit_event->event.type != ent->type) { |
64c12e04 JB |
176 | WARN_ON_ONCE(1); |
177 | return TRACE_TYPE_UNHANDLED; | |
178 | } | |
179 | ||
183742f0 | 180 | trace_seq_printf(s, "%s -> 0x%lx\n", entry->name, |
bed1ffca | 181 | trace->ret); |
bed1ffca | 182 | |
183742f0 SRRH |
183 | out: |
184 | return trace_handle_return(s); | |
bed1ffca FW |
185 | } |
186 | ||
e6971969 LZ |
187 | extern char *__bad_type_size(void); |
188 | ||
026842d1 TS |
189 | #define SYSCALL_FIELD(type, field, name) \ |
190 | sizeof(type) != sizeof(trace.field) ? \ | |
e6971969 | 191 | __bad_type_size() : \ |
026842d1 TS |
192 | #type, #name, offsetof(typeof(trace), field), \ |
193 | sizeof(trace.field), is_signed_type(type) | |
e6971969 | 194 | |
3ddc77f6 LZ |
195 | static int __init |
196 | __set_enter_print_fmt(struct syscall_metadata *entry, char *buf, int len) | |
50307a45 LJ |
197 | { |
198 | int i; | |
199 | int pos = 0; | |
200 | ||
201 | /* When len=0, we just calculate the needed length */ | |
202 | #define LEN_OR_ZERO (len ? len - pos : 0) | |
203 | ||
204 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); | |
205 | for (i = 0; i < entry->nb_args; i++) { | |
206 | pos += snprintf(buf + pos, LEN_OR_ZERO, "%s: 0x%%0%zulx%s", | |
207 | entry->args[i], sizeof(unsigned long), | |
208 | i == entry->nb_args - 1 ? "" : ", "); | |
209 | } | |
210 | pos += snprintf(buf + pos, LEN_OR_ZERO, "\""); | |
211 | ||
212 | for (i = 0; i < entry->nb_args; i++) { | |
213 | pos += snprintf(buf + pos, LEN_OR_ZERO, | |
214 | ", ((unsigned long)(REC->%s))", entry->args[i]); | |
215 | } | |
216 | ||
217 | #undef LEN_OR_ZERO | |
218 | ||
219 | /* return the length of print_fmt */ | |
220 | return pos; | |
221 | } | |
222 | ||
2425bcb9 | 223 | static int __init set_syscall_print_fmt(struct trace_event_call *call) |
50307a45 LJ |
224 | { |
225 | char *print_fmt; | |
226 | int len; | |
227 | struct syscall_metadata *entry = call->data; | |
228 | ||
229 | if (entry->enter_event != call) { | |
230 | call->print_fmt = "\"0x%lx\", REC->ret"; | |
231 | return 0; | |
232 | } | |
233 | ||
234 | /* First: called with 0 length to calculate the needed length */ | |
235 | len = __set_enter_print_fmt(entry, NULL, 0); | |
236 | ||
237 | print_fmt = kmalloc(len + 1, GFP_KERNEL); | |
238 | if (!print_fmt) | |
239 | return -ENOMEM; | |
240 | ||
241 | /* Second: actually write the @print_fmt */ | |
242 | __set_enter_print_fmt(entry, print_fmt, len + 1); | |
243 | call->print_fmt = print_fmt; | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
2425bcb9 | 248 | static void __init free_syscall_print_fmt(struct trace_event_call *call) |
50307a45 LJ |
249 | { |
250 | struct syscall_metadata *entry = call->data; | |
251 | ||
252 | if (entry->enter_event == call) | |
253 | kfree(call->print_fmt); | |
254 | } | |
255 | ||
2425bcb9 | 256 | static int __init syscall_enter_define_fields(struct trace_event_call *call) |
540b7b8d LZ |
257 | { |
258 | struct syscall_trace_enter trace; | |
31c16b13 | 259 | struct syscall_metadata *meta = call->data; |
540b7b8d | 260 | int ret; |
540b7b8d LZ |
261 | int i; |
262 | int offset = offsetof(typeof(trace), args); | |
263 | ||
026842d1 TS |
264 | ret = trace_define_field(call, SYSCALL_FIELD(int, nr, __syscall_nr), |
265 | FILTER_OTHER); | |
0f1ef51d LJ |
266 | if (ret) |
267 | return ret; | |
268 | ||
540b7b8d | 269 | for (i = 0; i < meta->nb_args; i++) { |
aeaeae11 FW |
270 | ret = trace_define_field(call, meta->types[i], |
271 | meta->args[i], offset, | |
43b51ead LZ |
272 | sizeof(unsigned long), 0, |
273 | FILTER_OTHER); | |
540b7b8d LZ |
274 | offset += sizeof(unsigned long); |
275 | } | |
276 | ||
277 | return ret; | |
278 | } | |
279 | ||
2425bcb9 | 280 | static int __init syscall_exit_define_fields(struct trace_event_call *call) |
540b7b8d LZ |
281 | { |
282 | struct syscall_trace_exit trace; | |
283 | int ret; | |
284 | ||
026842d1 TS |
285 | ret = trace_define_field(call, SYSCALL_FIELD(int, nr, __syscall_nr), |
286 | FILTER_OTHER); | |
0f1ef51d LJ |
287 | if (ret) |
288 | return ret; | |
289 | ||
026842d1 | 290 | ret = trace_define_field(call, SYSCALL_FIELD(long, ret, ret), |
43b51ead | 291 | FILTER_OTHER); |
540b7b8d LZ |
292 | |
293 | return ret; | |
294 | } | |
295 | ||
12ab74ee | 296 | static void ftrace_syscall_enter(void *data, struct pt_regs *regs, long id) |
ee08c6ec | 297 | { |
12ab74ee | 298 | struct trace_array *tr = data; |
7f1d2f82 | 299 | struct trace_event_file *trace_file; |
bed1ffca FW |
300 | struct syscall_trace_enter *entry; |
301 | struct syscall_metadata *sys_data; | |
302 | struct ring_buffer_event *event; | |
e77405ad | 303 | struct ring_buffer *buffer; |
11034ae9 J |
304 | unsigned long irq_flags; |
305 | int pc; | |
ee08c6ec | 306 | int syscall_nr; |
f431b634 | 307 | int size; |
ee08c6ec | 308 | |
f431b634 | 309 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 310 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
cd0980fc | 311 | return; |
d562aff9 TZ |
312 | |
313 | /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE) */ | |
7f1d2f82 SRRH |
314 | trace_file = rcu_dereference_sched(tr->enter_syscall_files[syscall_nr]); |
315 | if (!trace_file) | |
d562aff9 TZ |
316 | return; |
317 | ||
09a5059a | 318 | if (trace_trigger_soft_disabled(trace_file)) |
13a1e4ae | 319 | return; |
ee08c6ec | 320 | |
bed1ffca FW |
321 | sys_data = syscall_nr_to_meta(syscall_nr); |
322 | if (!sys_data) | |
323 | return; | |
324 | ||
325 | size = sizeof(*entry) + sizeof(unsigned long) * sys_data->nb_args; | |
326 | ||
11034ae9 J |
327 | local_save_flags(irq_flags); |
328 | pc = preempt_count(); | |
329 | ||
12883efb | 330 | buffer = tr->trace_buffer.buffer; |
12ab74ee | 331 | event = trace_buffer_lock_reserve(buffer, |
11034ae9 | 332 | sys_data->enter_event->event.type, size, irq_flags, pc); |
bed1ffca FW |
333 | if (!event) |
334 | return; | |
335 | ||
336 | entry = ring_buffer_event_data(event); | |
337 | entry->nr = syscall_nr; | |
338 | syscall_get_arguments(current, regs, 0, sys_data->nb_args, entry->args); | |
339 | ||
7f1d2f82 | 340 | event_trigger_unlock_commit(trace_file, buffer, event, entry, |
13a1e4ae | 341 | irq_flags, pc); |
ee08c6ec FW |
342 | } |
343 | ||
12ab74ee | 344 | static void ftrace_syscall_exit(void *data, struct pt_regs *regs, long ret) |
ee08c6ec | 345 | { |
12ab74ee | 346 | struct trace_array *tr = data; |
7f1d2f82 | 347 | struct trace_event_file *trace_file; |
bed1ffca FW |
348 | struct syscall_trace_exit *entry; |
349 | struct syscall_metadata *sys_data; | |
350 | struct ring_buffer_event *event; | |
e77405ad | 351 | struct ring_buffer *buffer; |
11034ae9 J |
352 | unsigned long irq_flags; |
353 | int pc; | |
ee08c6ec FW |
354 | int syscall_nr; |
355 | ||
f431b634 | 356 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 357 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
cd0980fc | 358 | return; |
d562aff9 TZ |
359 | |
360 | /* Here we're inside tp handler's rcu_read_lock_sched (__DO_TRACE()) */ | |
7f1d2f82 SRRH |
361 | trace_file = rcu_dereference_sched(tr->exit_syscall_files[syscall_nr]); |
362 | if (!trace_file) | |
d562aff9 TZ |
363 | return; |
364 | ||
09a5059a | 365 | if (trace_trigger_soft_disabled(trace_file)) |
13a1e4ae | 366 | return; |
ee08c6ec | 367 | |
bed1ffca FW |
368 | sys_data = syscall_nr_to_meta(syscall_nr); |
369 | if (!sys_data) | |
370 | return; | |
371 | ||
11034ae9 J |
372 | local_save_flags(irq_flags); |
373 | pc = preempt_count(); | |
374 | ||
12883efb | 375 | buffer = tr->trace_buffer.buffer; |
12ab74ee | 376 | event = trace_buffer_lock_reserve(buffer, |
11034ae9 J |
377 | sys_data->exit_event->event.type, sizeof(*entry), |
378 | irq_flags, pc); | |
bed1ffca FW |
379 | if (!event) |
380 | return; | |
381 | ||
382 | entry = ring_buffer_event_data(event); | |
383 | entry->nr = syscall_nr; | |
384 | entry->ret = syscall_get_return_value(current, regs); | |
385 | ||
7f1d2f82 | 386 | event_trigger_unlock_commit(trace_file, buffer, event, entry, |
13a1e4ae | 387 | irq_flags, pc); |
ee08c6ec FW |
388 | } |
389 | ||
7f1d2f82 | 390 | static int reg_event_syscall_enter(struct trace_event_file *file, |
2425bcb9 | 391 | struct trace_event_call *call) |
ee08c6ec | 392 | { |
12ab74ee | 393 | struct trace_array *tr = file->tr; |
fb34a08c JB |
394 | int ret = 0; |
395 | int num; | |
fb34a08c | 396 | |
c252f657 | 397 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 398 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
399 | return -ENOSYS; |
400 | mutex_lock(&syscall_trace_lock); | |
12ab74ee SR |
401 | if (!tr->sys_refcount_enter) |
402 | ret = register_trace_sys_enter(ftrace_syscall_enter, tr); | |
3b8e4273 | 403 | if (!ret) { |
d562aff9 | 404 | rcu_assign_pointer(tr->enter_syscall_files[num], file); |
12ab74ee | 405 | tr->sys_refcount_enter++; |
fb34a08c JB |
406 | } |
407 | mutex_unlock(&syscall_trace_lock); | |
408 | return ret; | |
ee08c6ec FW |
409 | } |
410 | ||
7f1d2f82 | 411 | static void unreg_event_syscall_enter(struct trace_event_file *file, |
2425bcb9 | 412 | struct trace_event_call *call) |
ee08c6ec | 413 | { |
12ab74ee | 414 | struct trace_array *tr = file->tr; |
fb34a08c | 415 | int num; |
ee08c6ec | 416 | |
c252f657 | 417 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 418 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
419 | return; |
420 | mutex_lock(&syscall_trace_lock); | |
12ab74ee | 421 | tr->sys_refcount_enter--; |
fb5a613b | 422 | RCU_INIT_POINTER(tr->enter_syscall_files[num], NULL); |
12ab74ee SR |
423 | if (!tr->sys_refcount_enter) |
424 | unregister_trace_sys_enter(ftrace_syscall_enter, tr); | |
fb34a08c JB |
425 | mutex_unlock(&syscall_trace_lock); |
426 | } | |
ee08c6ec | 427 | |
7f1d2f82 | 428 | static int reg_event_syscall_exit(struct trace_event_file *file, |
2425bcb9 | 429 | struct trace_event_call *call) |
ee08c6ec | 430 | { |
12ab74ee | 431 | struct trace_array *tr = file->tr; |
fb34a08c JB |
432 | int ret = 0; |
433 | int num; | |
fb34a08c | 434 | |
c252f657 | 435 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 436 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
437 | return -ENOSYS; |
438 | mutex_lock(&syscall_trace_lock); | |
12ab74ee SR |
439 | if (!tr->sys_refcount_exit) |
440 | ret = register_trace_sys_exit(ftrace_syscall_exit, tr); | |
3b8e4273 | 441 | if (!ret) { |
d562aff9 | 442 | rcu_assign_pointer(tr->exit_syscall_files[num], file); |
12ab74ee | 443 | tr->sys_refcount_exit++; |
ee08c6ec | 444 | } |
fb34a08c JB |
445 | mutex_unlock(&syscall_trace_lock); |
446 | return ret; | |
447 | } | |
ee08c6ec | 448 | |
7f1d2f82 | 449 | static void unreg_event_syscall_exit(struct trace_event_file *file, |
2425bcb9 | 450 | struct trace_event_call *call) |
fb34a08c | 451 | { |
12ab74ee | 452 | struct trace_array *tr = file->tr; |
fb34a08c | 453 | int num; |
ee08c6ec | 454 | |
c252f657 | 455 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
3773b389 | 456 | if (WARN_ON_ONCE(num < 0 || num >= NR_syscalls)) |
fb34a08c JB |
457 | return; |
458 | mutex_lock(&syscall_trace_lock); | |
12ab74ee | 459 | tr->sys_refcount_exit--; |
fb5a613b | 460 | RCU_INIT_POINTER(tr->exit_syscall_files[num], NULL); |
12ab74ee SR |
461 | if (!tr->sys_refcount_exit) |
462 | unregister_trace_sys_exit(ftrace_syscall_exit, tr); | |
fb34a08c | 463 | mutex_unlock(&syscall_trace_lock); |
ee08c6ec | 464 | } |
fb34a08c | 465 | |
2425bcb9 | 466 | static int __init init_syscall_trace(struct trace_event_call *call) |
a1301da0 LJ |
467 | { |
468 | int id; | |
ba976970 IM |
469 | int num; |
470 | ||
471 | num = ((struct syscall_metadata *)call->data)->syscall_nr; | |
472 | if (num < 0 || num >= NR_syscalls) { | |
473 | pr_debug("syscall %s metadata not mapped, disabling ftrace event\n", | |
474 | ((struct syscall_metadata *)call->data)->name); | |
475 | return -ENOSYS; | |
476 | } | |
a1301da0 | 477 | |
50307a45 LJ |
478 | if (set_syscall_print_fmt(call) < 0) |
479 | return -ENOMEM; | |
480 | ||
c7ef3a90 SR |
481 | id = trace_event_raw_init(call); |
482 | ||
483 | if (id < 0) { | |
50307a45 | 484 | free_syscall_print_fmt(call); |
c7ef3a90 | 485 | return id; |
50307a45 | 486 | } |
c7ef3a90 SR |
487 | |
488 | return id; | |
a1301da0 LJ |
489 | } |
490 | ||
6f86ab9f VN |
491 | struct trace_event_functions enter_syscall_print_funcs = { |
492 | .trace = print_syscall_enter, | |
493 | }; | |
494 | ||
495 | struct trace_event_functions exit_syscall_print_funcs = { | |
496 | .trace = print_syscall_exit, | |
497 | }; | |
498 | ||
2425bcb9 | 499 | struct trace_event_class __refdata event_class_syscall_enter = { |
6f86ab9f VN |
500 | .system = "syscalls", |
501 | .reg = syscall_enter_register, | |
502 | .define_fields = syscall_enter_define_fields, | |
503 | .get_fields = syscall_get_enter_fields, | |
504 | .raw_init = init_syscall_trace, | |
505 | }; | |
506 | ||
2425bcb9 | 507 | struct trace_event_class __refdata event_class_syscall_exit = { |
6f86ab9f VN |
508 | .system = "syscalls", |
509 | .reg = syscall_exit_register, | |
510 | .define_fields = syscall_exit_define_fields, | |
511 | .fields = LIST_HEAD_INIT(event_class_syscall_exit.fields), | |
512 | .raw_init = init_syscall_trace, | |
513 | }; | |
514 | ||
c763ba06 | 515 | unsigned long __init __weak arch_syscall_addr(int nr) |
e7b8e675 MF |
516 | { |
517 | return (unsigned long)sys_call_table[nr]; | |
518 | } | |
519 | ||
5f893b26 | 520 | void __init init_ftrace_syscalls(void) |
c44fc770 FW |
521 | { |
522 | struct syscall_metadata *meta; | |
523 | unsigned long addr; | |
524 | int i; | |
525 | ||
47b0edcb TM |
526 | syscalls_metadata = kcalloc(NR_syscalls, sizeof(*syscalls_metadata), |
527 | GFP_KERNEL); | |
c44fc770 FW |
528 | if (!syscalls_metadata) { |
529 | WARN_ON(1); | |
5f893b26 | 530 | return; |
c44fc770 FW |
531 | } |
532 | ||
533 | for (i = 0; i < NR_syscalls; i++) { | |
534 | addr = arch_syscall_addr(i); | |
535 | meta = find_syscall_meta(addr); | |
c252f657 LJ |
536 | if (!meta) |
537 | continue; | |
538 | ||
539 | meta->syscall_nr = i; | |
c44fc770 FW |
540 | syscalls_metadata[i] = meta; |
541 | } | |
c44fc770 | 542 | } |
c44fc770 | 543 | |
07b139c8 | 544 | #ifdef CONFIG_PERF_EVENTS |
19007a67 | 545 | |
97d5a220 FW |
546 | static DECLARE_BITMAP(enabled_perf_enter_syscalls, NR_syscalls); |
547 | static DECLARE_BITMAP(enabled_perf_exit_syscalls, NR_syscalls); | |
548 | static int sys_perf_refcount_enter; | |
549 | static int sys_perf_refcount_exit; | |
f4b5ffcc | 550 | |
38516ab5 | 551 | static void perf_syscall_enter(void *ignore, struct pt_regs *regs, long id) |
f4b5ffcc JB |
552 | { |
553 | struct syscall_metadata *sys_data; | |
20ab4425 | 554 | struct syscall_trace_enter *rec; |
1c024eca | 555 | struct hlist_head *head; |
f4b5ffcc | 556 | int syscall_nr; |
4ed7c92d | 557 | int rctx; |
19007a67 | 558 | int size; |
f4b5ffcc | 559 | |
f431b634 | 560 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 561 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
60916a93 | 562 | return; |
97d5a220 | 563 | if (!test_bit(syscall_nr, enabled_perf_enter_syscalls)) |
f4b5ffcc JB |
564 | return; |
565 | ||
566 | sys_data = syscall_nr_to_meta(syscall_nr); | |
567 | if (!sys_data) | |
568 | return; | |
569 | ||
421c7860 ON |
570 | head = this_cpu_ptr(sys_data->enter_event->perf_events); |
571 | if (hlist_empty(head)) | |
572 | return; | |
573 | ||
19007a67 FW |
574 | /* get the size after alignment with the u32 buffer size field */ |
575 | size = sizeof(unsigned long) * sys_data->nb_args + sizeof(*rec); | |
576 | size = ALIGN(size + sizeof(u32), sizeof(u64)); | |
577 | size -= sizeof(u32); | |
578 | ||
97d5a220 | 579 | rec = (struct syscall_trace_enter *)perf_trace_buf_prepare(size, |
86038c5e | 580 | sys_data->enter_event->event.type, NULL, &rctx); |
430ad5a6 XG |
581 | if (!rec) |
582 | return; | |
20ab4425 | 583 | |
20ab4425 FW |
584 | rec->nr = syscall_nr; |
585 | syscall_get_arguments(current, regs, 0, sys_data->nb_args, | |
586 | (unsigned long *)&rec->args); | |
e6dab5ff | 587 | perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL); |
f4b5ffcc JB |
588 | } |
589 | ||
2425bcb9 | 590 | static int perf_sysenter_enable(struct trace_event_call *call) |
f4b5ffcc JB |
591 | { |
592 | int ret = 0; | |
593 | int num; | |
594 | ||
3bbe84e9 | 595 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
596 | |
597 | mutex_lock(&syscall_trace_lock); | |
97d5a220 | 598 | if (!sys_perf_refcount_enter) |
38516ab5 | 599 | ret = register_trace_sys_enter(perf_syscall_enter, NULL); |
f4b5ffcc JB |
600 | if (ret) { |
601 | pr_info("event trace: Could not activate" | |
602 | "syscall entry trace point"); | |
603 | } else { | |
97d5a220 FW |
604 | set_bit(num, enabled_perf_enter_syscalls); |
605 | sys_perf_refcount_enter++; | |
f4b5ffcc JB |
606 | } |
607 | mutex_unlock(&syscall_trace_lock); | |
608 | return ret; | |
609 | } | |
610 | ||
2425bcb9 | 611 | static void perf_sysenter_disable(struct trace_event_call *call) |
f4b5ffcc JB |
612 | { |
613 | int num; | |
614 | ||
3bbe84e9 | 615 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
616 | |
617 | mutex_lock(&syscall_trace_lock); | |
97d5a220 FW |
618 | sys_perf_refcount_enter--; |
619 | clear_bit(num, enabled_perf_enter_syscalls); | |
620 | if (!sys_perf_refcount_enter) | |
38516ab5 | 621 | unregister_trace_sys_enter(perf_syscall_enter, NULL); |
f4b5ffcc JB |
622 | mutex_unlock(&syscall_trace_lock); |
623 | } | |
624 | ||
38516ab5 | 625 | static void perf_syscall_exit(void *ignore, struct pt_regs *regs, long ret) |
f4b5ffcc JB |
626 | { |
627 | struct syscall_metadata *sys_data; | |
20ab4425 | 628 | struct syscall_trace_exit *rec; |
1c024eca | 629 | struct hlist_head *head; |
f4b5ffcc | 630 | int syscall_nr; |
4ed7c92d | 631 | int rctx; |
20ab4425 | 632 | int size; |
f4b5ffcc | 633 | |
f431b634 | 634 | syscall_nr = trace_get_syscall_nr(current, regs); |
086ba77a | 635 | if (syscall_nr < 0 || syscall_nr >= NR_syscalls) |
60916a93 | 636 | return; |
97d5a220 | 637 | if (!test_bit(syscall_nr, enabled_perf_exit_syscalls)) |
f4b5ffcc JB |
638 | return; |
639 | ||
640 | sys_data = syscall_nr_to_meta(syscall_nr); | |
641 | if (!sys_data) | |
642 | return; | |
643 | ||
421c7860 ON |
644 | head = this_cpu_ptr(sys_data->exit_event->perf_events); |
645 | if (hlist_empty(head)) | |
646 | return; | |
647 | ||
20ab4425 FW |
648 | /* We can probably do that at build time */ |
649 | size = ALIGN(sizeof(*rec) + sizeof(u32), sizeof(u64)); | |
650 | size -= sizeof(u32); | |
19007a67 | 651 | |
97d5a220 | 652 | rec = (struct syscall_trace_exit *)perf_trace_buf_prepare(size, |
86038c5e | 653 | sys_data->exit_event->event.type, NULL, &rctx); |
430ad5a6 XG |
654 | if (!rec) |
655 | return; | |
20ab4425 | 656 | |
20ab4425 FW |
657 | rec->nr = syscall_nr; |
658 | rec->ret = syscall_get_return_value(current, regs); | |
e6dab5ff | 659 | perf_trace_buf_submit(rec, size, rctx, 0, 1, regs, head, NULL); |
f4b5ffcc JB |
660 | } |
661 | ||
2425bcb9 | 662 | static int perf_sysexit_enable(struct trace_event_call *call) |
f4b5ffcc JB |
663 | { |
664 | int ret = 0; | |
665 | int num; | |
666 | ||
3bbe84e9 | 667 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
668 | |
669 | mutex_lock(&syscall_trace_lock); | |
97d5a220 | 670 | if (!sys_perf_refcount_exit) |
38516ab5 | 671 | ret = register_trace_sys_exit(perf_syscall_exit, NULL); |
f4b5ffcc JB |
672 | if (ret) { |
673 | pr_info("event trace: Could not activate" | |
6574658b | 674 | "syscall exit trace point"); |
f4b5ffcc | 675 | } else { |
97d5a220 FW |
676 | set_bit(num, enabled_perf_exit_syscalls); |
677 | sys_perf_refcount_exit++; | |
f4b5ffcc JB |
678 | } |
679 | mutex_unlock(&syscall_trace_lock); | |
680 | return ret; | |
681 | } | |
682 | ||
2425bcb9 | 683 | static void perf_sysexit_disable(struct trace_event_call *call) |
f4b5ffcc JB |
684 | { |
685 | int num; | |
686 | ||
3bbe84e9 | 687 | num = ((struct syscall_metadata *)call->data)->syscall_nr; |
f4b5ffcc JB |
688 | |
689 | mutex_lock(&syscall_trace_lock); | |
97d5a220 FW |
690 | sys_perf_refcount_exit--; |
691 | clear_bit(num, enabled_perf_exit_syscalls); | |
692 | if (!sys_perf_refcount_exit) | |
38516ab5 | 693 | unregister_trace_sys_exit(perf_syscall_exit, NULL); |
f4b5ffcc JB |
694 | mutex_unlock(&syscall_trace_lock); |
695 | } | |
696 | ||
07b139c8 | 697 | #endif /* CONFIG_PERF_EVENTS */ |
f4b5ffcc | 698 | |
2425bcb9 | 699 | static int syscall_enter_register(struct trace_event_call *event, |
ceec0b6f | 700 | enum trace_reg type, void *data) |
2239291a | 701 | { |
7f1d2f82 | 702 | struct trace_event_file *file = data; |
12ab74ee | 703 | |
2239291a SR |
704 | switch (type) { |
705 | case TRACE_REG_REGISTER: | |
12ab74ee | 706 | return reg_event_syscall_enter(file, event); |
2239291a | 707 | case TRACE_REG_UNREGISTER: |
12ab74ee | 708 | unreg_event_syscall_enter(file, event); |
2239291a SR |
709 | return 0; |
710 | ||
711 | #ifdef CONFIG_PERF_EVENTS | |
712 | case TRACE_REG_PERF_REGISTER: | |
713 | return perf_sysenter_enable(event); | |
714 | case TRACE_REG_PERF_UNREGISTER: | |
715 | perf_sysenter_disable(event); | |
716 | return 0; | |
ceec0b6f JO |
717 | case TRACE_REG_PERF_OPEN: |
718 | case TRACE_REG_PERF_CLOSE: | |
489c75c3 JO |
719 | case TRACE_REG_PERF_ADD: |
720 | case TRACE_REG_PERF_DEL: | |
ceec0b6f | 721 | return 0; |
2239291a SR |
722 | #endif |
723 | } | |
724 | return 0; | |
725 | } | |
726 | ||
2425bcb9 | 727 | static int syscall_exit_register(struct trace_event_call *event, |
ceec0b6f | 728 | enum trace_reg type, void *data) |
2239291a | 729 | { |
7f1d2f82 | 730 | struct trace_event_file *file = data; |
12ab74ee | 731 | |
2239291a SR |
732 | switch (type) { |
733 | case TRACE_REG_REGISTER: | |
12ab74ee | 734 | return reg_event_syscall_exit(file, event); |
2239291a | 735 | case TRACE_REG_UNREGISTER: |
12ab74ee | 736 | unreg_event_syscall_exit(file, event); |
2239291a SR |
737 | return 0; |
738 | ||
739 | #ifdef CONFIG_PERF_EVENTS | |
740 | case TRACE_REG_PERF_REGISTER: | |
741 | return perf_sysexit_enable(event); | |
742 | case TRACE_REG_PERF_UNREGISTER: | |
743 | perf_sysexit_disable(event); | |
744 | return 0; | |
ceec0b6f JO |
745 | case TRACE_REG_PERF_OPEN: |
746 | case TRACE_REG_PERF_CLOSE: | |
489c75c3 JO |
747 | case TRACE_REG_PERF_ADD: |
748 | case TRACE_REG_PERF_DEL: | |
ceec0b6f | 749 | return 0; |
2239291a SR |
750 | #endif |
751 | } | |
752 | return 0; | |
753 | } |