blob: 1d0d7f42efe37486be9789a96b875020fd53f1cc [file] [log] [blame]
Steven Rostedt3d083392008-05-12 21:20:42 +02001/*
2 * Code for replacing ftrace calls with jumps.
3 *
4 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
5 *
6 * Thanks goes to Ingo Molnar, for suggesting the idea.
7 * Mathieu Desnoyers, for suggesting postponing the modifications.
8 * Arjan van de Ven, for keeping me straight, and explaining to me
9 * the dangers of modifying code on the run.
10 */
11
12#include <linux/spinlock.h>
13#include <linux/hardirq.h>
Steven Rostedt6f93fc02008-08-20 12:55:07 -040014#include <linux/uaccess.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020015#include <linux/ftrace.h>
16#include <linux/percpu.h>
Ingo Molnar19b3e962008-11-11 11:57:02 +010017#include <linux/sched.h>
Steven Rostedt3d083392008-05-12 21:20:42 +020018#include <linux/init.h>
19#include <linux/list.h>
20
Steven Rostedt16239632009-02-17 17:57:30 -050021#include <asm/cacheflush.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053022#include <asm/ftrace.h>
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010023#include <linux/ftrace.h>
Steven Rostedt732f3ca2008-08-14 18:05:05 -040024#include <asm/nops.h>
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010025#include <asm/nmi.h>
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020026
Steven Rostedt3d083392008-05-12 21:20:42 +020027
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010028#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt3d083392008-05-12 21:20:42 +020029
Steven Rostedt16239632009-02-17 17:57:30 -050030int ftrace_arch_code_modify_prepare(void)
31{
32 set_kernel_text_rw();
33 return 0;
34}
35
36int ftrace_arch_code_modify_post_process(void)
37{
38 set_kernel_text_ro();
39 return 0;
40}
41
Steven Rostedt3d083392008-05-12 21:20:42 +020042union ftrace_code_union {
Abhishek Sagar395a59d2008-06-21 23:47:27 +053043 char code[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +020044 struct {
45 char e8;
46 int offset;
47 } __attribute__((packed));
48};
49
Steven Rostedt15adc042008-10-23 09:33:08 -040050static int ftrace_calc_offset(long ip, long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020051{
52 return (int)(addr - ip);
53}
54
Steven Rostedt31e88902008-11-14 16:21:19 -080055static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020056{
57 static union ftrace_code_union calc;
58
59 calc.e8 = 0xe8;
Abhishek Sagar395a59d2008-06-21 23:47:27 +053060 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
Steven Rostedt3c1720f2008-05-12 21:20:43 +020061
62 /*
63 * No locking needed, this must be called via kstop_machine
64 * which in essence is like running on a uniprocessor machine.
65 */
66 return calc.code;
67}
68
Steven Rostedt17666f02008-10-30 16:08:32 -040069/*
70 * Modifying code must take extra care. On an SMP machine, if
71 * the code being modified is also being executed on another CPU
72 * that CPU will have undefined results and possibly take a GPF.
73 * We use kstop_machine to stop other CPUS from exectuing code.
74 * But this does not stop NMIs from happening. We still need
75 * to protect against that. We separate out the modification of
76 * the code to take care of this.
77 *
78 * Two buffers are added: An IP buffer and a "code" buffer.
79 *
Steven Rostedta26a2a22008-10-31 00:03:22 -040080 * 1) Put the instruction pointer into the IP buffer
Steven Rostedt17666f02008-10-30 16:08:32 -040081 * and the new code into the "code" buffer.
82 * 2) Set a flag that says we are modifying code
83 * 3) Wait for any running NMIs to finish.
84 * 4) Write the code
85 * 5) clear the flag.
86 * 6) Wait for any running NMIs to finish.
87 *
88 * If an NMI is executed, the first thing it does is to call
89 * "ftrace_nmi_enter". This will check if the flag is set to write
90 * and if it is, it will write what is in the IP and "code" buffers.
91 *
92 * The trick is, it does not matter if everyone is writing the same
93 * content to the code location. Also, if a CPU is executing code
94 * it is OK to write to that code location if the contents being written
95 * are the same as what exists.
96 */
97
Steven Rostedt4e6ea142009-02-05 22:30:07 -050098static atomic_t nmi_running = ATOMIC_INIT(0);
Steven Rostedta26a2a22008-10-31 00:03:22 -040099static int mod_code_status; /* holds return value of text write */
100static int mod_code_write; /* set when NMI should do the write */
101static void *mod_code_ip; /* holds the IP to write to */
102static void *mod_code_newcode; /* holds the text to write to the IP */
Steven Rostedt17666f02008-10-30 16:08:32 -0400103
Steven Rostedta26a2a22008-10-31 00:03:22 -0400104static unsigned nmi_wait_count;
105static atomic_t nmi_update_count = ATOMIC_INIT(0);
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400106
107int ftrace_arch_read_dyn_info(char *buf, int size)
108{
109 int r;
110
111 r = snprintf(buf, size, "%u %u",
112 nmi_wait_count,
113 atomic_read(&nmi_update_count));
114 return r;
115}
116
Steven Rostedt17666f02008-10-30 16:08:32 -0400117static void ftrace_mod_code(void)
118{
119 /*
120 * Yes, more than one CPU process can be writing to mod_code_status.
121 * (and the code itself)
122 * But if one were to fail, then they all should, and if one were
123 * to succeed, then they all should.
124 */
125 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
126 MCOUNT_INSN_SIZE);
Steven Rostedt90c7ac42009-02-19 13:32:57 -0500127
128 /* if we fail, then kill any new writers */
129 if (mod_code_status)
130 mod_code_write = 0;
Steven Rostedt17666f02008-10-30 16:08:32 -0400131}
132
Steven Rostedta81bd802009-02-06 01:45:16 -0500133void ftrace_nmi_enter(void)
Steven Rostedt17666f02008-10-30 16:08:32 -0400134{
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500135 atomic_inc(&nmi_running);
136 /* Must have nmi_running seen before reading write flag */
Steven Rostedt17666f02008-10-30 16:08:32 -0400137 smp_mb();
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400138 if (mod_code_write) {
Steven Rostedt17666f02008-10-30 16:08:32 -0400139 ftrace_mod_code();
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400140 atomic_inc(&nmi_update_count);
141 }
Steven Rostedt17666f02008-10-30 16:08:32 -0400142}
143
Steven Rostedta81bd802009-02-06 01:45:16 -0500144void ftrace_nmi_exit(void)
Steven Rostedt17666f02008-10-30 16:08:32 -0400145{
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500146 /* Finish all executions before clearing nmi_running */
Steven Rostedt17666f02008-10-30 16:08:32 -0400147 smp_wmb();
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500148 atomic_dec(&nmi_running);
Steven Rostedt17666f02008-10-30 16:08:32 -0400149}
150
151static void wait_for_nmi(void)
152{
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500153 if (!atomic_read(&nmi_running))
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300154 return;
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400155
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300156 do {
Steven Rostedt17666f02008-10-30 16:08:32 -0400157 cpu_relax();
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500158 } while (atomic_read(&nmi_running));
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400159
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300160 nmi_wait_count++;
Steven Rostedt17666f02008-10-30 16:08:32 -0400161}
162
163static int
164do_ftrace_mod_code(unsigned long ip, void *new_code)
165{
166 mod_code_ip = (void *)ip;
167 mod_code_newcode = new_code;
168
169 /* The buffers need to be visible before we let NMIs write them */
170 smp_wmb();
171
172 mod_code_write = 1;
173
174 /* Make sure write bit is visible before we wait on NMIs */
175 smp_mb();
176
177 wait_for_nmi();
178
179 /* Make sure all running NMIs have finished before we write the code */
180 smp_mb();
181
182 ftrace_mod_code();
183
184 /* Make sure the write happens before clearing the bit */
185 smp_wmb();
186
187 mod_code_write = 0;
188
189 /* make sure NMIs see the cleared bit */
190 smp_mb();
191
192 wait_for_nmi();
193
194 return mod_code_status;
195}
196
197
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100198
199
200static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
201
Steven Rostedt31e88902008-11-14 16:21:19 -0800202static unsigned char *ftrace_nop_replace(void)
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100203{
204 return ftrace_nop;
205}
206
Steven Rostedt31e88902008-11-14 16:21:19 -0800207static int
Steven Rostedt3d083392008-05-12 21:20:42 +0200208ftrace_modify_code(unsigned long ip, unsigned char *old_code,
209 unsigned char *new_code)
210{
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400211 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +0200212
213 /*
214 * Note: Due to modules and __init, code can
215 * disappear and change, we need to protect against faulting
Steven Rostedt76aefee2008-10-23 09:33:00 -0400216 * as well as code changing. We do this by using the
Steven Rostedtab9a0912008-10-23 09:33:01 -0400217 * probe_kernel_* functions.
Steven Rostedt3d083392008-05-12 21:20:42 +0200218 *
219 * No real locking needed, this code is run through
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400220 * kstop_machine, or before SMP starts.
Steven Rostedt3d083392008-05-12 21:20:42 +0200221 */
Steven Rostedt76aefee2008-10-23 09:33:00 -0400222
223 /* read the text we want to modify */
Steven Rostedtab9a0912008-10-23 09:33:01 -0400224 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400225 return -EFAULT;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400226
Steven Rostedt76aefee2008-10-23 09:33:00 -0400227 /* Make sure it is what we expect it to be */
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400228 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400229 return -EINVAL;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400230
Steven Rostedt76aefee2008-10-23 09:33:00 -0400231 /* replace the text with the new text */
Steven Rostedt17666f02008-10-30 16:08:32 -0400232 if (do_ftrace_mod_code(ip, new_code))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400233 return -EPERM;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400234
Steven Rostedt3d083392008-05-12 21:20:42 +0200235 sync_core();
236
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400237 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200238}
239
Steven Rostedt31e88902008-11-14 16:21:19 -0800240int ftrace_make_nop(struct module *mod,
241 struct dyn_ftrace *rec, unsigned long addr)
242{
243 unsigned char *new, *old;
244 unsigned long ip = rec->ip;
245
246 old = ftrace_call_replace(ip, addr);
247 new = ftrace_nop_replace();
248
249 return ftrace_modify_code(rec->ip, old, new);
250}
251
252int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
253{
254 unsigned char *new, *old;
255 unsigned long ip = rec->ip;
256
257 old = ftrace_nop_replace();
258 new = ftrace_call_replace(ip, addr);
259
260 return ftrace_modify_code(rec->ip, old, new);
261}
262
Steven Rostedt15adc042008-10-23 09:33:08 -0400263int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200264{
265 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530266 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200267 int ret;
268
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530269 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200270 new = ftrace_call_replace(ip, (unsigned long)func);
271 ret = ftrace_modify_code(ip, old, new);
272
273 return ret;
274}
275
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200276int __init ftrace_dyn_arch_init(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200277{
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400278 extern const unsigned char ftrace_test_p6nop[];
279 extern const unsigned char ftrace_test_nop5[];
280 extern const unsigned char ftrace_test_jmp[];
281 int faulted = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200282
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400283 /*
284 * There is no good nop for all x86 archs.
285 * We will default to using the P6_NOP5, but first we
286 * will test to make sure that the nop will actually
287 * work on this CPU. If it faults, we will then
288 * go to a lesser efficient 5 byte nop. If that fails
289 * we then just use a jmp as our nop. This isn't the most
290 * efficient nop, but we can not use a multi part nop
291 * since we would then risk being preempted in the middle
292 * of that nop, and if we enabled tracing then, it might
293 * cause a system crash.
294 *
295 * TODO: check the cpuid to determine the best nop.
296 */
297 asm volatile (
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400298 "ftrace_test_jmp:"
299 "jmp ftrace_test_p6nop\n"
Anders Kaseorg8b273862008-10-09 22:19:08 -0400300 "nop\n"
301 "nop\n"
302 "nop\n" /* 2 byte jmp + 3 bytes */
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400303 "ftrace_test_p6nop:"
304 P6_NOP5
305 "jmp 1f\n"
306 "ftrace_test_nop5:"
307 ".byte 0x66,0x66,0x66,0x66,0x90\n"
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400308 "1:"
309 ".section .fixup, \"ax\"\n"
310 "2: movl $1, %0\n"
311 " jmp ftrace_test_nop5\n"
312 "3: movl $2, %0\n"
313 " jmp 1b\n"
314 ".previous\n"
315 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
316 _ASM_EXTABLE(ftrace_test_nop5, 3b)
317 : "=r"(faulted) : "0" (faulted));
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200318
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400319 switch (faulted) {
320 case 0:
321 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400322 memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400323 break;
324 case 1:
325 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400326 memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400327 break;
328 case 2:
Anders Kaseorg8b273862008-10-09 22:19:08 -0400329 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400330 memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400331 break;
332 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200333
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400334 /* The return code is retured via data */
335 *(unsigned long *)data = 0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200336
Steven Rostedt3d083392008-05-12 21:20:42 +0200337 return 0;
338}
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100339#endif
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100340
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100341#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100342
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500343#ifdef CONFIG_DYNAMIC_FTRACE
344extern void ftrace_graph_call(void);
345
346static int ftrace_mod_jmp(unsigned long ip,
347 int old_offset, int new_offset)
348{
349 unsigned char code[MCOUNT_INSN_SIZE];
350
351 if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
352 return -EFAULT;
353
354 if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
355 return -EINVAL;
356
357 *(int *)(&code[1]) = new_offset;
358
359 if (do_ftrace_mod_code(ip, &code))
360 return -EPERM;
361
362 return 0;
363}
364
365int ftrace_enable_ftrace_graph_caller(void)
366{
367 unsigned long ip = (unsigned long)(&ftrace_graph_call);
368 int old_offset, new_offset;
369
370 old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
371 new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
372
373 return ftrace_mod_jmp(ip, old_offset, new_offset);
374}
375
376int ftrace_disable_ftrace_graph_caller(void)
377{
378 unsigned long ip = (unsigned long)(&ftrace_graph_call);
379 int old_offset, new_offset;
380
381 old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
382 new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
383
384 return ftrace_mod_jmp(ip, old_offset, new_offset);
385}
386
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100387#endif /* !CONFIG_DYNAMIC_FTRACE */
388
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100389/*
390 * Hook the return address and push it in the stack of return addrs
391 * in current thread info.
392 */
393void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
394{
395 unsigned long old;
396 unsigned long long calltime;
397 int faulted;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100398 struct ftrace_graph_ent trace;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100399 unsigned long return_hooker = (unsigned long)
400 &return_to_handler;
401
402 /* Nmi's are currently unsupported */
Steven Rostedt9a5fd902009-02-06 01:14:26 -0500403 if (unlikely(in_nmi()))
Frederic Weisbecker380c4b12008-12-06 03:43:41 +0100404 return;
405
406 if (unlikely(atomic_read(&current->tracing_graph_pause)))
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100407 return;
408
409 /*
410 * Protect against fault, even if it shouldn't
411 * happen. This tool is too much intrusive to
412 * ignore such a protection.
413 */
414 asm volatile(
Steven Rostedt96665782009-02-10 11:53:23 -0500415 "1: " _ASM_MOV " (%[parent]), %[old]\n"
416 "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100417 " movl $0, %[faulted]\n"
Steven Rostedte3944bf2009-02-10 13:07:13 -0500418 "3:\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100419
420 ".section .fixup, \"ax\"\n"
Steven Rostedte3944bf2009-02-10 13:07:13 -0500421 "4: movl $1, %[faulted]\n"
422 " jmp 3b\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100423 ".previous\n"
424
Steven Rostedte3944bf2009-02-10 13:07:13 -0500425 _ASM_EXTABLE(1b, 4b)
426 _ASM_EXTABLE(2b, 4b)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100427
Steven Rostedt96665782009-02-10 11:53:23 -0500428 : [old] "=r" (old), [faulted] "=r" (faulted)
429 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100430 : "memory"
431 );
432
Steven Rostedt14a866c2008-12-02 23:50:02 -0500433 if (unlikely(faulted)) {
434 ftrace_graph_stop();
435 WARN_ON(1);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100436 return;
437 }
438
Frederic Weisbecker00126932009-03-05 01:49:22 +0100439 calltime = trace_clock_local();
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100440
Steven Rostedt712406a2009-02-09 10:54:03 -0800441 if (ftrace_push_return_trace(old, calltime,
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100442 self_addr, &trace.depth) == -EBUSY) {
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100443 *parent = old;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100444 return;
445 }
446
447 trace.func = self_addr;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100448
Steven Rostedte49dc192008-12-02 23:50:05 -0500449 /* Only trace if the calling function expects to */
450 if (!ftrace_graph_entry(&trace)) {
451 current->curr_ret_stack--;
452 *parent = old;
453 }
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100454}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100455#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100456
457#ifdef CONFIG_FTRACE_SYSCALLS
458
459extern unsigned long __start_syscalls_metadata[];
460extern unsigned long __stop_syscalls_metadata[];
461extern unsigned long *sys_call_table;
462
463static struct syscall_metadata **syscalls_metadata;
464
465static struct syscall_metadata *find_syscall_meta(unsigned long *syscall)
466{
467 struct syscall_metadata *start;
468 struct syscall_metadata *stop;
469 char str[KSYM_SYMBOL_LEN];
470
471
472 start = (struct syscall_metadata *)__start_syscalls_metadata;
473 stop = (struct syscall_metadata *)__stop_syscalls_metadata;
474 kallsyms_lookup((unsigned long) syscall, NULL, NULL, NULL, str);
475
476 for ( ; start < stop; start++) {
477 if (start->name && !strcmp(start->name, str))
478 return start;
479 }
480 return NULL;
481}
482
483struct syscall_metadata *syscall_nr_to_meta(int nr)
484{
485 if (!syscalls_metadata || nr >= FTRACE_SYSCALL_MAX || nr < 0)
486 return NULL;
487
488 return syscalls_metadata[nr];
489}
490
491void arch_init_ftrace_syscalls(void)
492{
493 int i;
494 struct syscall_metadata *meta;
495 unsigned long **psys_syscall_table = &sys_call_table;
496 static atomic_t refs;
497
498 if (atomic_inc_return(&refs) != 1)
499 goto end;
500
501 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
502 FTRACE_SYSCALL_MAX, GFP_KERNEL);
503 if (!syscalls_metadata) {
504 WARN_ON(1);
505 return;
506 }
507
508 for (i = 0; i < FTRACE_SYSCALL_MAX; i++) {
509 meta = find_syscall_meta(psys_syscall_table[i]);
510 syscalls_metadata[i] = meta;
511 }
512 return;
513
514 /* Paranoid: avoid overflow */
515end:
516 atomic_dec(&refs);
517}
518#endif