blob: 70a10ca100f68273e0371a58f8236ff5d3075acf [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>
Steven Rostedt732f3ca2008-08-14 18:05:05 -040023#include <asm/nops.h>
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010024#include <asm/nmi.h>
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020025
Steven Rostedt3d083392008-05-12 21:20:42 +020026
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010027#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt3d083392008-05-12 21:20:42 +020028
Steven Rostedt16239632009-02-17 17:57:30 -050029int ftrace_arch_code_modify_prepare(void)
30{
31 set_kernel_text_rw();
32 return 0;
33}
34
35int ftrace_arch_code_modify_post_process(void)
36{
37 set_kernel_text_ro();
38 return 0;
39}
40
Steven Rostedt3d083392008-05-12 21:20:42 +020041union ftrace_code_union {
Abhishek Sagar395a59d2008-06-21 23:47:27 +053042 char code[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +020043 struct {
44 char e8;
45 int offset;
46 } __attribute__((packed));
47};
48
Steven Rostedt15adc042008-10-23 09:33:08 -040049static int ftrace_calc_offset(long ip, long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020050{
51 return (int)(addr - ip);
52}
53
Steven Rostedt31e88902008-11-14 16:21:19 -080054static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020055{
56 static union ftrace_code_union calc;
57
58 calc.e8 = 0xe8;
Abhishek Sagar395a59d2008-06-21 23:47:27 +053059 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
Steven Rostedt3c1720f2008-05-12 21:20:43 +020060
61 /*
62 * No locking needed, this must be called via kstop_machine
63 * which in essence is like running on a uniprocessor machine.
64 */
65 return calc.code;
66}
67
Steven Rostedt17666f02008-10-30 16:08:32 -040068/*
69 * Modifying code must take extra care. On an SMP machine, if
70 * the code being modified is also being executed on another CPU
71 * that CPU will have undefined results and possibly take a GPF.
72 * We use kstop_machine to stop other CPUS from exectuing code.
73 * But this does not stop NMIs from happening. We still need
74 * to protect against that. We separate out the modification of
75 * the code to take care of this.
76 *
77 * Two buffers are added: An IP buffer and a "code" buffer.
78 *
Steven Rostedta26a2a22008-10-31 00:03:22 -040079 * 1) Put the instruction pointer into the IP buffer
Steven Rostedt17666f02008-10-30 16:08:32 -040080 * and the new code into the "code" buffer.
Lai Jiangshane9d9df42009-03-18 16:42:57 +080081 * 2) Wait for any running NMIs to finish and set a flag that says
82 * we are modifying code, it is done in an atomic operation.
83 * 3) Write the code
84 * 4) clear the flag.
85 * 5) Wait for any running NMIs to finish.
Steven Rostedt17666f02008-10-30 16:08:32 -040086 *
87 * If an NMI is executed, the first thing it does is to call
88 * "ftrace_nmi_enter". This will check if the flag is set to write
89 * and if it is, it will write what is in the IP and "code" buffers.
90 *
91 * The trick is, it does not matter if everyone is writing the same
92 * content to the code location. Also, if a CPU is executing code
93 * it is OK to write to that code location if the contents being written
94 * are the same as what exists.
95 */
96
Lai Jiangshane9d9df42009-03-18 16:42:57 +080097#define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
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 */
Steven Rostedta26a2a22008-10-31 00:03:22 -0400100static void *mod_code_ip; /* holds the IP to write to */
101static void *mod_code_newcode; /* holds the text to write to the IP */
Steven Rostedt17666f02008-10-30 16:08:32 -0400102
Steven Rostedta26a2a22008-10-31 00:03:22 -0400103static unsigned nmi_wait_count;
104static atomic_t nmi_update_count = ATOMIC_INIT(0);
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400105
106int ftrace_arch_read_dyn_info(char *buf, int size)
107{
108 int r;
109
110 r = snprintf(buf, size, "%u %u",
111 nmi_wait_count,
112 atomic_read(&nmi_update_count));
113 return r;
114}
115
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800116static void clear_mod_flag(void)
117{
118 int old = atomic_read(&nmi_running);
119
120 for (;;) {
121 int new = old & ~MOD_CODE_WRITE_FLAG;
122
123 if (old == new)
124 break;
125
126 old = atomic_cmpxchg(&nmi_running, old, new);
127 }
128}
129
Steven Rostedt17666f02008-10-30 16:08:32 -0400130static void ftrace_mod_code(void)
131{
132 /*
133 * Yes, more than one CPU process can be writing to mod_code_status.
134 * (and the code itself)
135 * But if one were to fail, then they all should, and if one were
136 * to succeed, then they all should.
137 */
138 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
139 MCOUNT_INSN_SIZE);
Steven Rostedt90c7ac42009-02-19 13:32:57 -0500140
141 /* if we fail, then kill any new writers */
142 if (mod_code_status)
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800143 clear_mod_flag();
Steven Rostedt17666f02008-10-30 16:08:32 -0400144}
145
Steven Rostedta81bd802009-02-06 01:45:16 -0500146void ftrace_nmi_enter(void)
Steven Rostedt17666f02008-10-30 16:08:32 -0400147{
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800148 if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
149 smp_rmb();
Steven Rostedt17666f02008-10-30 16:08:32 -0400150 ftrace_mod_code();
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400151 atomic_inc(&nmi_update_count);
152 }
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800153 /* Must have previous changes seen before executions */
154 smp_mb();
Steven Rostedt17666f02008-10-30 16:08:32 -0400155}
156
Steven Rostedta81bd802009-02-06 01:45:16 -0500157void ftrace_nmi_exit(void)
Steven Rostedt17666f02008-10-30 16:08:32 -0400158{
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500159 /* Finish all executions before clearing nmi_running */
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800160 smp_mb();
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500161 atomic_dec(&nmi_running);
Steven Rostedt17666f02008-10-30 16:08:32 -0400162}
163
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800164static void wait_for_nmi_and_set_mod_flag(void)
165{
166 if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
167 return;
168
169 do {
170 cpu_relax();
171 } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
172
173 nmi_wait_count++;
174}
175
Steven Rostedt17666f02008-10-30 16:08:32 -0400176static void wait_for_nmi(void)
177{
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500178 if (!atomic_read(&nmi_running))
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300179 return;
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400180
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300181 do {
Steven Rostedt17666f02008-10-30 16:08:32 -0400182 cpu_relax();
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500183 } while (atomic_read(&nmi_running));
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400184
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300185 nmi_wait_count++;
Steven Rostedt17666f02008-10-30 16:08:32 -0400186}
187
188static int
189do_ftrace_mod_code(unsigned long ip, void *new_code)
190{
191 mod_code_ip = (void *)ip;
192 mod_code_newcode = new_code;
193
194 /* The buffers need to be visible before we let NMIs write them */
Steven Rostedt17666f02008-10-30 16:08:32 -0400195 smp_mb();
196
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800197 wait_for_nmi_and_set_mod_flag();
Steven Rostedt17666f02008-10-30 16:08:32 -0400198
199 /* Make sure all running NMIs have finished before we write the code */
200 smp_mb();
201
202 ftrace_mod_code();
203
204 /* Make sure the write happens before clearing the bit */
Steven Rostedt17666f02008-10-30 16:08:32 -0400205 smp_mb();
206
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800207 clear_mod_flag();
Steven Rostedt17666f02008-10-30 16:08:32 -0400208 wait_for_nmi();
209
210 return mod_code_status;
211}
212
213
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100214
215
216static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
217
Steven Rostedt31e88902008-11-14 16:21:19 -0800218static unsigned char *ftrace_nop_replace(void)
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100219{
220 return ftrace_nop;
221}
222
Steven Rostedt31e88902008-11-14 16:21:19 -0800223static int
Steven Rostedt3d083392008-05-12 21:20:42 +0200224ftrace_modify_code(unsigned long ip, unsigned char *old_code,
225 unsigned char *new_code)
226{
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400227 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +0200228
229 /*
230 * Note: Due to modules and __init, code can
231 * disappear and change, we need to protect against faulting
Steven Rostedt76aefee2008-10-23 09:33:00 -0400232 * as well as code changing. We do this by using the
Steven Rostedtab9a0912008-10-23 09:33:01 -0400233 * probe_kernel_* functions.
Steven Rostedt3d083392008-05-12 21:20:42 +0200234 *
235 * No real locking needed, this code is run through
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400236 * kstop_machine, or before SMP starts.
Steven Rostedt3d083392008-05-12 21:20:42 +0200237 */
Steven Rostedt76aefee2008-10-23 09:33:00 -0400238
239 /* read the text we want to modify */
Steven Rostedtab9a0912008-10-23 09:33:01 -0400240 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400241 return -EFAULT;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400242
Steven Rostedt76aefee2008-10-23 09:33:00 -0400243 /* Make sure it is what we expect it to be */
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400244 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400245 return -EINVAL;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400246
Steven Rostedt76aefee2008-10-23 09:33:00 -0400247 /* replace the text with the new text */
Steven Rostedt17666f02008-10-30 16:08:32 -0400248 if (do_ftrace_mod_code(ip, new_code))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400249 return -EPERM;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400250
Steven Rostedt3d083392008-05-12 21:20:42 +0200251 sync_core();
252
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400253 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200254}
255
Steven Rostedt31e88902008-11-14 16:21:19 -0800256int ftrace_make_nop(struct module *mod,
257 struct dyn_ftrace *rec, unsigned long addr)
258{
259 unsigned char *new, *old;
260 unsigned long ip = rec->ip;
261
262 old = ftrace_call_replace(ip, addr);
263 new = ftrace_nop_replace();
264
265 return ftrace_modify_code(rec->ip, old, new);
266}
267
268int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
269{
270 unsigned char *new, *old;
271 unsigned long ip = rec->ip;
272
273 old = ftrace_nop_replace();
274 new = ftrace_call_replace(ip, addr);
275
276 return ftrace_modify_code(rec->ip, old, new);
277}
278
Steven Rostedt15adc042008-10-23 09:33:08 -0400279int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200280{
281 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530282 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200283 int ret;
284
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530285 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200286 new = ftrace_call_replace(ip, (unsigned long)func);
287 ret = ftrace_modify_code(ip, old, new);
288
289 return ret;
290}
291
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200292int __init ftrace_dyn_arch_init(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200293{
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400294 extern const unsigned char ftrace_test_p6nop[];
295 extern const unsigned char ftrace_test_nop5[];
296 extern const unsigned char ftrace_test_jmp[];
297 int faulted = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200298
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400299 /*
300 * There is no good nop for all x86 archs.
301 * We will default to using the P6_NOP5, but first we
302 * will test to make sure that the nop will actually
303 * work on this CPU. If it faults, we will then
304 * go to a lesser efficient 5 byte nop. If that fails
305 * we then just use a jmp as our nop. This isn't the most
306 * efficient nop, but we can not use a multi part nop
307 * since we would then risk being preempted in the middle
308 * of that nop, and if we enabled tracing then, it might
309 * cause a system crash.
310 *
311 * TODO: check the cpuid to determine the best nop.
312 */
313 asm volatile (
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400314 "ftrace_test_jmp:"
315 "jmp ftrace_test_p6nop\n"
Anders Kaseorg8b273862008-10-09 22:19:08 -0400316 "nop\n"
317 "nop\n"
318 "nop\n" /* 2 byte jmp + 3 bytes */
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400319 "ftrace_test_p6nop:"
320 P6_NOP5
321 "jmp 1f\n"
322 "ftrace_test_nop5:"
323 ".byte 0x66,0x66,0x66,0x66,0x90\n"
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400324 "1:"
325 ".section .fixup, \"ax\"\n"
326 "2: movl $1, %0\n"
327 " jmp ftrace_test_nop5\n"
328 "3: movl $2, %0\n"
329 " jmp 1b\n"
330 ".previous\n"
331 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
332 _ASM_EXTABLE(ftrace_test_nop5, 3b)
333 : "=r"(faulted) : "0" (faulted));
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200334
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400335 switch (faulted) {
336 case 0:
337 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400338 memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400339 break;
340 case 1:
341 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400342 memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400343 break;
344 case 2:
Anders Kaseorg8b273862008-10-09 22:19:08 -0400345 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400346 memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400347 break;
348 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200349
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400350 /* The return code is retured via data */
351 *(unsigned long *)data = 0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200352
Steven Rostedt3d083392008-05-12 21:20:42 +0200353 return 0;
354}
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100355#endif
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100356
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100357#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100358
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500359#ifdef CONFIG_DYNAMIC_FTRACE
360extern void ftrace_graph_call(void);
361
362static int ftrace_mod_jmp(unsigned long ip,
363 int old_offset, int new_offset)
364{
365 unsigned char code[MCOUNT_INSN_SIZE];
366
367 if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
368 return -EFAULT;
369
370 if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
371 return -EINVAL;
372
373 *(int *)(&code[1]) = new_offset;
374
375 if (do_ftrace_mod_code(ip, &code))
376 return -EPERM;
377
378 return 0;
379}
380
381int ftrace_enable_ftrace_graph_caller(void)
382{
383 unsigned long ip = (unsigned long)(&ftrace_graph_call);
384 int old_offset, new_offset;
385
386 old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
387 new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
388
389 return ftrace_mod_jmp(ip, old_offset, new_offset);
390}
391
392int ftrace_disable_ftrace_graph_caller(void)
393{
394 unsigned long ip = (unsigned long)(&ftrace_graph_call);
395 int old_offset, new_offset;
396
397 old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
398 new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
399
400 return ftrace_mod_jmp(ip, old_offset, new_offset);
401}
402
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100403#endif /* !CONFIG_DYNAMIC_FTRACE */
404
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100405/*
406 * Hook the return address and push it in the stack of return addrs
407 * in current thread info.
408 */
409void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
410{
411 unsigned long old;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100412 int faulted;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100413 struct ftrace_graph_ent trace;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100414 unsigned long return_hooker = (unsigned long)
415 &return_to_handler;
416
417 /* Nmi's are currently unsupported */
Steven Rostedt9a5fd902009-02-06 01:14:26 -0500418 if (unlikely(in_nmi()))
Frederic Weisbecker380c4b12008-12-06 03:43:41 +0100419 return;
420
421 if (unlikely(atomic_read(&current->tracing_graph_pause)))
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100422 return;
423
424 /*
425 * Protect against fault, even if it shouldn't
426 * happen. This tool is too much intrusive to
427 * ignore such a protection.
428 */
429 asm volatile(
Steven Rostedt96665782009-02-10 11:53:23 -0500430 "1: " _ASM_MOV " (%[parent]), %[old]\n"
431 "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100432 " movl $0, %[faulted]\n"
Steven Rostedte3944bf2009-02-10 13:07:13 -0500433 "3:\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100434
435 ".section .fixup, \"ax\"\n"
Steven Rostedte3944bf2009-02-10 13:07:13 -0500436 "4: movl $1, %[faulted]\n"
437 " jmp 3b\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100438 ".previous\n"
439
Steven Rostedte3944bf2009-02-10 13:07:13 -0500440 _ASM_EXTABLE(1b, 4b)
441 _ASM_EXTABLE(2b, 4b)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100442
Steven Rostedt96665782009-02-10 11:53:23 -0500443 : [old] "=r" (old), [faulted] "=r" (faulted)
444 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100445 : "memory"
446 );
447
Steven Rostedt14a866c2008-12-02 23:50:02 -0500448 if (unlikely(faulted)) {
449 ftrace_graph_stop();
450 WARN_ON(1);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100451 return;
452 }
453
Steven Rostedt5d1a03d2009-03-23 23:38:49 -0400454 if (ftrace_push_return_trace(old, self_addr, &trace.depth) == -EBUSY) {
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100455 *parent = old;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100456 return;
457 }
458
459 trace.func = self_addr;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100460
Steven Rostedte49dc192008-12-02 23:50:05 -0500461 /* Only trace if the calling function expects to */
462 if (!ftrace_graph_entry(&trace)) {
463 current->curr_ret_stack--;
464 *parent = old;
465 }
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100466}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100467#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100468
469#ifdef CONFIG_FTRACE_SYSCALLS
470
471extern unsigned long __start_syscalls_metadata[];
472extern unsigned long __stop_syscalls_metadata[];
473extern unsigned long *sys_call_table;
474
475static struct syscall_metadata **syscalls_metadata;
476
477static struct syscall_metadata *find_syscall_meta(unsigned long *syscall)
478{
479 struct syscall_metadata *start;
480 struct syscall_metadata *stop;
481 char str[KSYM_SYMBOL_LEN];
482
483
484 start = (struct syscall_metadata *)__start_syscalls_metadata;
485 stop = (struct syscall_metadata *)__stop_syscalls_metadata;
486 kallsyms_lookup((unsigned long) syscall, NULL, NULL, NULL, str);
487
488 for ( ; start < stop; start++) {
489 if (start->name && !strcmp(start->name, str))
490 return start;
491 }
492 return NULL;
493}
494
495struct syscall_metadata *syscall_nr_to_meta(int nr)
496{
497 if (!syscalls_metadata || nr >= FTRACE_SYSCALL_MAX || nr < 0)
498 return NULL;
499
500 return syscalls_metadata[nr];
501}
502
503void arch_init_ftrace_syscalls(void)
504{
505 int i;
506 struct syscall_metadata *meta;
507 unsigned long **psys_syscall_table = &sys_call_table;
508 static atomic_t refs;
509
510 if (atomic_inc_return(&refs) != 1)
511 goto end;
512
513 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
514 FTRACE_SYSCALL_MAX, GFP_KERNEL);
515 if (!syscalls_metadata) {
516 WARN_ON(1);
517 return;
518 }
519
520 for (i = 0; i < FTRACE_SYSCALL_MAX; i++) {
521 meta = find_syscall_meta(psys_syscall_table[i]);
522 syscalls_metadata[i] = meta;
523 }
524 return;
525
526 /* Paranoid: avoid overflow */
527end:
528 atomic_dec(&refs);
529}
530#endif