blob: 944e9820b4b53a70e0ff2e2c4fb725c4477657d9 [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
Frederic Weisbecker47788c52009-04-08 20:40:59 +020021#include <trace/syscall.h>
22
Steven Rostedt16239632009-02-17 17:57:30 -050023#include <asm/cacheflush.h>
Abhishek Sagar395a59d2008-06-21 23:47:27 +053024#include <asm/ftrace.h>
Steven Rostedt732f3ca2008-08-14 18:05:05 -040025#include <asm/nops.h>
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010026#include <asm/nmi.h>
Steven Rostedtdfa60ab2008-05-12 21:20:43 +020027
Steven Rostedt3d083392008-05-12 21:20:42 +020028
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +010029#ifdef CONFIG_DYNAMIC_FTRACE
Steven Rostedt3d083392008-05-12 21:20:42 +020030
Steven Rostedt16239632009-02-17 17:57:30 -050031int ftrace_arch_code_modify_prepare(void)
32{
33 set_kernel_text_rw();
34 return 0;
35}
36
37int ftrace_arch_code_modify_post_process(void)
38{
39 set_kernel_text_ro();
40 return 0;
41}
42
Steven Rostedt3d083392008-05-12 21:20:42 +020043union ftrace_code_union {
Abhishek Sagar395a59d2008-06-21 23:47:27 +053044 char code[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +020045 struct {
46 char e8;
47 int offset;
48 } __attribute__((packed));
49};
50
Steven Rostedt15adc042008-10-23 09:33:08 -040051static int ftrace_calc_offset(long ip, long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020052{
53 return (int)(addr - ip);
54}
55
Steven Rostedt31e88902008-11-14 16:21:19 -080056static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
Steven Rostedt3c1720f2008-05-12 21:20:43 +020057{
58 static union ftrace_code_union calc;
59
60 calc.e8 = 0xe8;
Abhishek Sagar395a59d2008-06-21 23:47:27 +053061 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
Steven Rostedt3c1720f2008-05-12 21:20:43 +020062
63 /*
64 * No locking needed, this must be called via kstop_machine
65 * which in essence is like running on a uniprocessor machine.
66 */
67 return calc.code;
68}
69
Steven Rostedt17666f02008-10-30 16:08:32 -040070/*
71 * Modifying code must take extra care. On an SMP machine, if
72 * the code being modified is also being executed on another CPU
73 * that CPU will have undefined results and possibly take a GPF.
74 * We use kstop_machine to stop other CPUS from exectuing code.
75 * But this does not stop NMIs from happening. We still need
76 * to protect against that. We separate out the modification of
77 * the code to take care of this.
78 *
79 * Two buffers are added: An IP buffer and a "code" buffer.
80 *
Steven Rostedta26a2a22008-10-31 00:03:22 -040081 * 1) Put the instruction pointer into the IP buffer
Steven Rostedt17666f02008-10-30 16:08:32 -040082 * and the new code into the "code" buffer.
Lai Jiangshane9d9df42009-03-18 16:42:57 +080083 * 2) Wait for any running NMIs to finish and set a flag that says
84 * we are modifying code, it is done in an atomic operation.
85 * 3) Write the code
86 * 4) clear the flag.
87 * 5) Wait for any running NMIs to finish.
Steven Rostedt17666f02008-10-30 16:08:32 -040088 *
89 * If an NMI is executed, the first thing it does is to call
90 * "ftrace_nmi_enter". This will check if the flag is set to write
91 * and if it is, it will write what is in the IP and "code" buffers.
92 *
93 * The trick is, it does not matter if everyone is writing the same
94 * content to the code location. Also, if a CPU is executing code
95 * it is OK to write to that code location if the contents being written
96 * are the same as what exists.
97 */
98
Lai Jiangshane9d9df42009-03-18 16:42:57 +080099#define MOD_CODE_WRITE_FLAG (1 << 31) /* set when NMI should do the write */
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500100static atomic_t nmi_running = ATOMIC_INIT(0);
Steven Rostedta26a2a22008-10-31 00:03:22 -0400101static int mod_code_status; /* holds return value of text write */
Steven Rostedta26a2a22008-10-31 00:03:22 -0400102static void *mod_code_ip; /* holds the IP to write to */
103static void *mod_code_newcode; /* holds the text to write to the IP */
Steven Rostedt17666f02008-10-30 16:08:32 -0400104
Steven Rostedta26a2a22008-10-31 00:03:22 -0400105static unsigned nmi_wait_count;
106static atomic_t nmi_update_count = ATOMIC_INIT(0);
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400107
108int ftrace_arch_read_dyn_info(char *buf, int size)
109{
110 int r;
111
112 r = snprintf(buf, size, "%u %u",
113 nmi_wait_count,
114 atomic_read(&nmi_update_count));
115 return r;
116}
117
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800118static void clear_mod_flag(void)
119{
120 int old = atomic_read(&nmi_running);
121
122 for (;;) {
123 int new = old & ~MOD_CODE_WRITE_FLAG;
124
125 if (old == new)
126 break;
127
128 old = atomic_cmpxchg(&nmi_running, old, new);
129 }
130}
131
Steven Rostedt17666f02008-10-30 16:08:32 -0400132static void ftrace_mod_code(void)
133{
134 /*
135 * Yes, more than one CPU process can be writing to mod_code_status.
136 * (and the code itself)
137 * But if one were to fail, then they all should, and if one were
138 * to succeed, then they all should.
139 */
140 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
141 MCOUNT_INSN_SIZE);
Steven Rostedt90c7ac42009-02-19 13:32:57 -0500142
143 /* if we fail, then kill any new writers */
144 if (mod_code_status)
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800145 clear_mod_flag();
Steven Rostedt17666f02008-10-30 16:08:32 -0400146}
147
Steven Rostedta81bd802009-02-06 01:45:16 -0500148void ftrace_nmi_enter(void)
Steven Rostedt17666f02008-10-30 16:08:32 -0400149{
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800150 if (atomic_inc_return(&nmi_running) & MOD_CODE_WRITE_FLAG) {
151 smp_rmb();
Steven Rostedt17666f02008-10-30 16:08:32 -0400152 ftrace_mod_code();
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400153 atomic_inc(&nmi_update_count);
154 }
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800155 /* Must have previous changes seen before executions */
156 smp_mb();
Steven Rostedt17666f02008-10-30 16:08:32 -0400157}
158
Steven Rostedta81bd802009-02-06 01:45:16 -0500159void ftrace_nmi_exit(void)
Steven Rostedt17666f02008-10-30 16:08:32 -0400160{
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500161 /* Finish all executions before clearing nmi_running */
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800162 smp_mb();
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500163 atomic_dec(&nmi_running);
Steven Rostedt17666f02008-10-30 16:08:32 -0400164}
165
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800166static void wait_for_nmi_and_set_mod_flag(void)
167{
168 if (!atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG))
169 return;
170
171 do {
172 cpu_relax();
173 } while (atomic_cmpxchg(&nmi_running, 0, MOD_CODE_WRITE_FLAG));
174
175 nmi_wait_count++;
176}
177
Steven Rostedt17666f02008-10-30 16:08:32 -0400178static void wait_for_nmi(void)
179{
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500180 if (!atomic_read(&nmi_running))
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300181 return;
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400182
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300183 do {
Steven Rostedt17666f02008-10-30 16:08:32 -0400184 cpu_relax();
Steven Rostedt4e6ea142009-02-05 22:30:07 -0500185 } while (atomic_read(&nmi_running));
Steven Rostedtb807c3d2008-10-30 16:08:33 -0400186
Cyrill Gorcunov89025282009-01-26 18:28:02 +0300187 nmi_wait_count++;
Steven Rostedt17666f02008-10-30 16:08:32 -0400188}
189
Suresh Siddha55ca3cc2009-10-28 18:46:57 -0800190static inline int
191within(unsigned long addr, unsigned long start, unsigned long end)
192{
193 return addr >= start && addr < end;
194}
195
Steven Rostedt17666f02008-10-30 16:08:32 -0400196static int
197do_ftrace_mod_code(unsigned long ip, void *new_code)
198{
Suresh Siddha55ca3cc2009-10-28 18:46:57 -0800199 /*
200 * On x86_64, kernel text mappings are mapped read-only with
201 * CONFIG_DEBUG_RODATA. So we use the kernel identity mapping instead
202 * of the kernel text mapping to modify the kernel text.
203 *
204 * For 32bit kernels, these mappings are same and we can use
205 * kernel identity mapping to modify code.
206 */
207 if (within(ip, (unsigned long)_text, (unsigned long)_etext))
208 ip = (unsigned long)__va(__pa(ip));
209
Steven Rostedt17666f02008-10-30 16:08:32 -0400210 mod_code_ip = (void *)ip;
211 mod_code_newcode = new_code;
212
213 /* The buffers need to be visible before we let NMIs write them */
Steven Rostedt17666f02008-10-30 16:08:32 -0400214 smp_mb();
215
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800216 wait_for_nmi_and_set_mod_flag();
Steven Rostedt17666f02008-10-30 16:08:32 -0400217
218 /* Make sure all running NMIs have finished before we write the code */
219 smp_mb();
220
221 ftrace_mod_code();
222
223 /* Make sure the write happens before clearing the bit */
Steven Rostedt17666f02008-10-30 16:08:32 -0400224 smp_mb();
225
Lai Jiangshane9d9df42009-03-18 16:42:57 +0800226 clear_mod_flag();
Steven Rostedt17666f02008-10-30 16:08:32 -0400227 wait_for_nmi();
228
229 return mod_code_status;
230}
231
232
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100233
234
235static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
236
Steven Rostedt31e88902008-11-14 16:21:19 -0800237static unsigned char *ftrace_nop_replace(void)
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100238{
239 return ftrace_nop;
240}
241
Steven Rostedt31e88902008-11-14 16:21:19 -0800242static int
Steven Rostedt3d083392008-05-12 21:20:42 +0200243ftrace_modify_code(unsigned long ip, unsigned char *old_code,
244 unsigned char *new_code)
245{
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400246 unsigned char replaced[MCOUNT_INSN_SIZE];
Steven Rostedt3d083392008-05-12 21:20:42 +0200247
248 /*
249 * Note: Due to modules and __init, code can
250 * disappear and change, we need to protect against faulting
Steven Rostedt76aefee2008-10-23 09:33:00 -0400251 * as well as code changing. We do this by using the
Steven Rostedtab9a0912008-10-23 09:33:01 -0400252 * probe_kernel_* functions.
Steven Rostedt3d083392008-05-12 21:20:42 +0200253 *
254 * No real locking needed, this code is run through
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400255 * kstop_machine, or before SMP starts.
Steven Rostedt3d083392008-05-12 21:20:42 +0200256 */
Steven Rostedt76aefee2008-10-23 09:33:00 -0400257
258 /* read the text we want to modify */
Steven Rostedtab9a0912008-10-23 09:33:01 -0400259 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400260 return -EFAULT;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400261
Steven Rostedt76aefee2008-10-23 09:33:00 -0400262 /* Make sure it is what we expect it to be */
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400263 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400264 return -EINVAL;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400265
Steven Rostedt76aefee2008-10-23 09:33:00 -0400266 /* replace the text with the new text */
Steven Rostedt17666f02008-10-30 16:08:32 -0400267 if (do_ftrace_mod_code(ip, new_code))
Steven Rostedt593eb8a2008-10-23 09:32:59 -0400268 return -EPERM;
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400269
Steven Rostedt3d083392008-05-12 21:20:42 +0200270 sync_core();
271
Steven Rostedt6f93fc02008-08-20 12:55:07 -0400272 return 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200273}
274
Steven Rostedt31e88902008-11-14 16:21:19 -0800275int ftrace_make_nop(struct module *mod,
276 struct dyn_ftrace *rec, unsigned long addr)
277{
278 unsigned char *new, *old;
279 unsigned long ip = rec->ip;
280
281 old = ftrace_call_replace(ip, addr);
282 new = ftrace_nop_replace();
283
284 return ftrace_modify_code(rec->ip, old, new);
285}
286
287int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
288{
289 unsigned char *new, *old;
290 unsigned long ip = rec->ip;
291
292 old = ftrace_nop_replace();
293 new = ftrace_call_replace(ip, addr);
294
295 return ftrace_modify_code(rec->ip, old, new);
296}
297
Steven Rostedt15adc042008-10-23 09:33:08 -0400298int ftrace_update_ftrace_func(ftrace_func_t func)
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200299{
300 unsigned long ip = (unsigned long)(&ftrace_call);
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530301 unsigned char old[MCOUNT_INSN_SIZE], *new;
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200302 int ret;
303
Abhishek Sagar395a59d2008-06-21 23:47:27 +0530304 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200305 new = ftrace_call_replace(ip, (unsigned long)func);
306 ret = ftrace_modify_code(ip, old, new);
307
308 return ret;
309}
310
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200311int __init ftrace_dyn_arch_init(void *data)
Steven Rostedt3d083392008-05-12 21:20:42 +0200312{
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400313 extern const unsigned char ftrace_test_p6nop[];
314 extern const unsigned char ftrace_test_nop5[];
315 extern const unsigned char ftrace_test_jmp[];
316 int faulted = 0;
Steven Rostedt3d083392008-05-12 21:20:42 +0200317
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400318 /*
319 * There is no good nop for all x86 archs.
320 * We will default to using the P6_NOP5, but first we
321 * will test to make sure that the nop will actually
322 * work on this CPU. If it faults, we will then
323 * go to a lesser efficient 5 byte nop. If that fails
324 * we then just use a jmp as our nop. This isn't the most
325 * efficient nop, but we can not use a multi part nop
326 * since we would then risk being preempted in the middle
327 * of that nop, and if we enabled tracing then, it might
328 * cause a system crash.
329 *
330 * TODO: check the cpuid to determine the best nop.
331 */
332 asm volatile (
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400333 "ftrace_test_jmp:"
334 "jmp ftrace_test_p6nop\n"
Anders Kaseorg8b273862008-10-09 22:19:08 -0400335 "nop\n"
336 "nop\n"
337 "nop\n" /* 2 byte jmp + 3 bytes */
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400338 "ftrace_test_p6nop:"
339 P6_NOP5
340 "jmp 1f\n"
341 "ftrace_test_nop5:"
342 ".byte 0x66,0x66,0x66,0x66,0x90\n"
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400343 "1:"
344 ".section .fixup, \"ax\"\n"
345 "2: movl $1, %0\n"
346 " jmp ftrace_test_nop5\n"
347 "3: movl $2, %0\n"
348 " jmp 1b\n"
349 ".previous\n"
350 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
351 _ASM_EXTABLE(ftrace_test_nop5, 3b)
352 : "=r"(faulted) : "0" (faulted));
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200353
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400354 switch (faulted) {
355 case 0:
356 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400357 memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400358 break;
359 case 1:
360 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400361 memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400362 break;
363 case 2:
Anders Kaseorg8b273862008-10-09 22:19:08 -0400364 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
Steven Rostedt8115f3f2008-10-24 09:12:17 -0400365 memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400366 break;
367 }
Steven Rostedtd61f82d2008-05-12 21:20:43 +0200368
Steven Rostedt732f3ca2008-08-14 18:05:05 -0400369 /* The return code is retured via data */
370 *(unsigned long *)data = 0;
Steven Rostedtdfa60ab2008-05-12 21:20:43 +0200371
Steven Rostedt3d083392008-05-12 21:20:42 +0200372 return 0;
373}
Frederic Weisbeckercaf4b322008-11-11 07:03:45 +0100374#endif
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100375
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100376#ifdef CONFIG_FUNCTION_GRAPH_TRACER
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100377
Steven Rostedt5a45cfe2008-11-26 00:16:24 -0500378#ifdef CONFIG_DYNAMIC_FTRACE
379extern void ftrace_graph_call(void);
380
381static int ftrace_mod_jmp(unsigned long ip,
382 int old_offset, int new_offset)
383{
384 unsigned char code[MCOUNT_INSN_SIZE];
385
386 if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
387 return -EFAULT;
388
389 if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
390 return -EINVAL;
391
392 *(int *)(&code[1]) = new_offset;
393
394 if (do_ftrace_mod_code(ip, &code))
395 return -EPERM;
396
397 return 0;
398}
399
400int ftrace_enable_ftrace_graph_caller(void)
401{
402 unsigned long ip = (unsigned long)(&ftrace_graph_call);
403 int old_offset, new_offset;
404
405 old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
406 new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
407
408 return ftrace_mod_jmp(ip, old_offset, new_offset);
409}
410
411int ftrace_disable_ftrace_graph_caller(void)
412{
413 unsigned long ip = (unsigned long)(&ftrace_graph_call);
414 int old_offset, new_offset;
415
416 old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
417 new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
418
419 return ftrace_mod_jmp(ip, old_offset, new_offset);
420}
421
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100422#endif /* !CONFIG_DYNAMIC_FTRACE */
423
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100424/*
425 * Hook the return address and push it in the stack of return addrs
426 * in current thread info.
427 */
Steven Rostedt71e308a2009-06-18 12:45:08 -0400428void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
429 unsigned long frame_pointer)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100430{
431 unsigned long old;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100432 int faulted;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100433 struct ftrace_graph_ent trace;
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100434 unsigned long return_hooker = (unsigned long)
435 &return_to_handler;
436
Frederic Weisbecker380c4b12008-12-06 03:43:41 +0100437 if (unlikely(atomic_read(&current->tracing_graph_pause)))
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100438 return;
439
440 /*
441 * Protect against fault, even if it shouldn't
442 * happen. This tool is too much intrusive to
443 * ignore such a protection.
444 */
445 asm volatile(
Steven Rostedt96665782009-02-10 11:53:23 -0500446 "1: " _ASM_MOV " (%[parent]), %[old]\n"
447 "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100448 " movl $0, %[faulted]\n"
Steven Rostedte3944bf2009-02-10 13:07:13 -0500449 "3:\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100450
451 ".section .fixup, \"ax\"\n"
Steven Rostedte3944bf2009-02-10 13:07:13 -0500452 "4: movl $1, %[faulted]\n"
453 " jmp 3b\n"
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100454 ".previous\n"
455
Steven Rostedte3944bf2009-02-10 13:07:13 -0500456 _ASM_EXTABLE(1b, 4b)
457 _ASM_EXTABLE(2b, 4b)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100458
Steven Rostedtaa512a22009-05-13 13:52:19 -0400459 : [old] "=&r" (old), [faulted] "=r" (faulted)
Steven Rostedt96665782009-02-10 11:53:23 -0500460 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100461 : "memory"
462 );
463
Steven Rostedt14a866c2008-12-02 23:50:02 -0500464 if (unlikely(faulted)) {
465 ftrace_graph_stop();
466 WARN_ON(1);
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100467 return;
468 }
469
Steven Rostedt71e308a2009-06-18 12:45:08 -0400470 if (ftrace_push_return_trace(old, self_addr, &trace.depth,
471 frame_pointer) == -EBUSY) {
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100472 *parent = old;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100473 return;
474 }
475
476 trace.func = self_addr;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100477
Steven Rostedte49dc192008-12-02 23:50:05 -0500478 /* Only trace if the calling function expects to */
479 if (!ftrace_graph_entry(&trace)) {
480 current->curr_ret_stack--;
481 *parent = old;
482 }
Frederic Weisbeckere7d37372008-11-16 06:02:06 +0100483}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100484#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100485
486#ifdef CONFIG_FTRACE_SYSCALLS
487
488extern unsigned long __start_syscalls_metadata[];
489extern unsigned long __stop_syscalls_metadata[];
490extern unsigned long *sys_call_table;
491
492static struct syscall_metadata **syscalls_metadata;
493
494static struct syscall_metadata *find_syscall_meta(unsigned long *syscall)
495{
496 struct syscall_metadata *start;
497 struct syscall_metadata *stop;
498 char str[KSYM_SYMBOL_LEN];
499
500
501 start = (struct syscall_metadata *)__start_syscalls_metadata;
502 stop = (struct syscall_metadata *)__stop_syscalls_metadata;
503 kallsyms_lookup((unsigned long) syscall, NULL, NULL, NULL, str);
504
505 for ( ; start < stop; start++) {
506 if (start->name && !strcmp(start->name, str))
507 return start;
508 }
509 return NULL;
510}
511
512struct syscall_metadata *syscall_nr_to_meta(int nr)
513{
Jason Baron57421db2009-08-24 17:40:22 -0400514 if (!syscalls_metadata || nr >= NR_syscalls || nr < 0)
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100515 return NULL;
516
517 return syscalls_metadata[nr];
518}
519
Jason Baroneeac19a2009-08-10 16:52:13 -0400520int syscall_name_to_nr(char *name)
521{
522 int i;
523
524 if (!syscalls_metadata)
525 return -1;
526
Jason Baron57421db2009-08-24 17:40:22 -0400527 for (i = 0; i < NR_syscalls; i++) {
Jason Baroneeac19a2009-08-10 16:52:13 -0400528 if (syscalls_metadata[i]) {
529 if (!strcmp(syscalls_metadata[i]->name, name))
530 return i;
531 }
532 }
533 return -1;
534}
535
Jason Baron64c12e02009-08-10 16:52:53 -0400536void set_syscall_enter_id(int num, int id)
537{
538 syscalls_metadata[num]->enter_id = id;
539}
540
541void set_syscall_exit_id(int num, int id)
542{
543 syscalls_metadata[num]->exit_id = id;
544}
545
Jason Baron066e0372009-08-10 16:52:23 -0400546static int __init arch_init_ftrace_syscalls(void)
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100547{
548 int i;
549 struct syscall_metadata *meta;
550 unsigned long **psys_syscall_table = &sys_call_table;
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100551
552 syscalls_metadata = kzalloc(sizeof(*syscalls_metadata) *
Jason Baron57421db2009-08-24 17:40:22 -0400553 NR_syscalls, GFP_KERNEL);
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100554 if (!syscalls_metadata) {
555 WARN_ON(1);
Jason Baron066e0372009-08-10 16:52:23 -0400556 return -ENOMEM;
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100557 }
558
Jason Baron57421db2009-08-24 17:40:22 -0400559 for (i = 0; i < NR_syscalls; i++) {
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100560 meta = find_syscall_meta(psys_syscall_table[i]);
561 syscalls_metadata[i] = meta;
562 }
Jason Baron066e0372009-08-10 16:52:23 -0400563 return 0;
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100564}
Jason Baron066e0372009-08-10 16:52:23 -0400565arch_initcall(arch_init_ftrace_syscalls);
Frederic Weisbeckerf58ba102009-03-13 15:42:12 +0100566#endif