ring_buffer: fix ring_buffer_read_page()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / x86 / kernel / ftrace.c
CommitLineData
3d083395
SR
1/*
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>
6f93fc07 14#include <linux/uaccess.h>
3d083395
SR
15#include <linux/ftrace.h>
16#include <linux/percpu.h>
19b3e967 17#include <linux/sched.h>
3d083395
SR
18#include <linux/init.h>
19#include <linux/list.h>
20
395a59d0 21#include <asm/ftrace.h>
caf4b323 22#include <linux/ftrace.h>
732f3ca7 23#include <asm/nops.h>
caf4b323 24#include <asm/nmi.h>
3d083395 25
3d083395 26
caf4b323 27#ifdef CONFIG_DYNAMIC_FTRACE
3d083395 28
3d083395 29union ftrace_code_union {
395a59d0 30 char code[MCOUNT_INSN_SIZE];
3d083395
SR
31 struct {
32 char e8;
33 int offset;
34 } __attribute__((packed));
35};
36
15adc048 37static int ftrace_calc_offset(long ip, long addr)
3c1720f0
SR
38{
39 return (int)(addr - ip);
40}
3d083395 41
31e88909 42static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
3c1720f0
SR
43{
44 static union ftrace_code_union calc;
3d083395 45
3c1720f0 46 calc.e8 = 0xe8;
395a59d0 47 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
3c1720f0
SR
48
49 /*
50 * No locking needed, this must be called via kstop_machine
51 * which in essence is like running on a uniprocessor machine.
52 */
53 return calc.code;
3d083395
SR
54}
55
17666f02
SR
56/*
57 * Modifying code must take extra care. On an SMP machine, if
58 * the code being modified is also being executed on another CPU
59 * that CPU will have undefined results and possibly take a GPF.
60 * We use kstop_machine to stop other CPUS from exectuing code.
61 * But this does not stop NMIs from happening. We still need
62 * to protect against that. We separate out the modification of
63 * the code to take care of this.
64 *
65 * Two buffers are added: An IP buffer and a "code" buffer.
66 *
a26a2a27 67 * 1) Put the instruction pointer into the IP buffer
17666f02
SR
68 * and the new code into the "code" buffer.
69 * 2) Set a flag that says we are modifying code
70 * 3) Wait for any running NMIs to finish.
71 * 4) Write the code
72 * 5) clear the flag.
73 * 6) Wait for any running NMIs to finish.
74 *
75 * If an NMI is executed, the first thing it does is to call
76 * "ftrace_nmi_enter". This will check if the flag is set to write
77 * and if it is, it will write what is in the IP and "code" buffers.
78 *
79 * The trick is, it does not matter if everyone is writing the same
80 * content to the code location. Also, if a CPU is executing code
81 * it is OK to write to that code location if the contents being written
82 * are the same as what exists.
83 */
84
4e6ea144 85static atomic_t nmi_running = ATOMIC_INIT(0);
a26a2a27
SR
86static int mod_code_status; /* holds return value of text write */
87static int mod_code_write; /* set when NMI should do the write */
88static void *mod_code_ip; /* holds the IP to write to */
89static void *mod_code_newcode; /* holds the text to write to the IP */
17666f02 90
a26a2a27
SR
91static unsigned nmi_wait_count;
92static atomic_t nmi_update_count = ATOMIC_INIT(0);
b807c3d0
SR
93
94int ftrace_arch_read_dyn_info(char *buf, int size)
95{
96 int r;
97
98 r = snprintf(buf, size, "%u %u",
99 nmi_wait_count,
100 atomic_read(&nmi_update_count));
101 return r;
102}
103
17666f02
SR
104static void ftrace_mod_code(void)
105{
106 /*
107 * Yes, more than one CPU process can be writing to mod_code_status.
108 * (and the code itself)
109 * But if one were to fail, then they all should, and if one were
110 * to succeed, then they all should.
111 */
112 mod_code_status = probe_kernel_write(mod_code_ip, mod_code_newcode,
113 MCOUNT_INSN_SIZE);
17666f02
SR
114}
115
a81bd80a 116void ftrace_nmi_enter(void)
17666f02 117{
4e6ea144
SR
118 atomic_inc(&nmi_running);
119 /* Must have nmi_running seen before reading write flag */
17666f02 120 smp_mb();
b807c3d0 121 if (mod_code_write) {
17666f02 122 ftrace_mod_code();
b807c3d0
SR
123 atomic_inc(&nmi_update_count);
124 }
17666f02
SR
125}
126
a81bd80a 127void ftrace_nmi_exit(void)
17666f02 128{
4e6ea144 129 /* Finish all executions before clearing nmi_running */
17666f02 130 smp_wmb();
4e6ea144 131 atomic_dec(&nmi_running);
17666f02
SR
132}
133
134static void wait_for_nmi(void)
135{
4e6ea144 136 if (!atomic_read(&nmi_running))
89025282 137 return;
b807c3d0 138
89025282 139 do {
17666f02 140 cpu_relax();
4e6ea144 141 } while (atomic_read(&nmi_running));
b807c3d0 142
89025282 143 nmi_wait_count++;
17666f02
SR
144}
145
146static int
147do_ftrace_mod_code(unsigned long ip, void *new_code)
148{
149 mod_code_ip = (void *)ip;
150 mod_code_newcode = new_code;
151
152 /* The buffers need to be visible before we let NMIs write them */
153 smp_wmb();
154
155 mod_code_write = 1;
156
157 /* Make sure write bit is visible before we wait on NMIs */
158 smp_mb();
159
160 wait_for_nmi();
161
162 /* Make sure all running NMIs have finished before we write the code */
163 smp_mb();
164
165 ftrace_mod_code();
166
167 /* Make sure the write happens before clearing the bit */
168 smp_wmb();
169
170 mod_code_write = 0;
171
172 /* make sure NMIs see the cleared bit */
173 smp_mb();
174
175 wait_for_nmi();
176
177 return mod_code_status;
178}
179
180
caf4b323
FW
181
182
183static unsigned char ftrace_nop[MCOUNT_INSN_SIZE];
184
31e88909 185static unsigned char *ftrace_nop_replace(void)
caf4b323
FW
186{
187 return ftrace_nop;
188}
189
31e88909 190static int
3d083395
SR
191ftrace_modify_code(unsigned long ip, unsigned char *old_code,
192 unsigned char *new_code)
193{
6f93fc07 194 unsigned char replaced[MCOUNT_INSN_SIZE];
3d083395
SR
195
196 /*
197 * Note: Due to modules and __init, code can
198 * disappear and change, we need to protect against faulting
76aefee5 199 * as well as code changing. We do this by using the
ab9a0918 200 * probe_kernel_* functions.
3d083395
SR
201 *
202 * No real locking needed, this code is run through
6f93fc07 203 * kstop_machine, or before SMP starts.
3d083395 204 */
76aefee5
SR
205
206 /* read the text we want to modify */
ab9a0918 207 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
593eb8a2 208 return -EFAULT;
6f93fc07 209
76aefee5 210 /* Make sure it is what we expect it to be */
6f93fc07 211 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
593eb8a2 212 return -EINVAL;
3d083395 213
76aefee5 214 /* replace the text with the new text */
17666f02 215 if (do_ftrace_mod_code(ip, new_code))
593eb8a2 216 return -EPERM;
6f93fc07
SR
217
218 sync_core();
3d083395 219
6f93fc07 220 return 0;
3d083395
SR
221}
222
31e88909
SR
223int ftrace_make_nop(struct module *mod,
224 struct dyn_ftrace *rec, unsigned long addr)
225{
226 unsigned char *new, *old;
227 unsigned long ip = rec->ip;
228
229 old = ftrace_call_replace(ip, addr);
230 new = ftrace_nop_replace();
231
232 return ftrace_modify_code(rec->ip, old, new);
233}
234
235int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
236{
237 unsigned char *new, *old;
238 unsigned long ip = rec->ip;
239
240 old = ftrace_nop_replace();
241 new = ftrace_call_replace(ip, addr);
242
243 return ftrace_modify_code(rec->ip, old, new);
244}
245
15adc048 246int ftrace_update_ftrace_func(ftrace_func_t func)
d61f82d0
SR
247{
248 unsigned long ip = (unsigned long)(&ftrace_call);
395a59d0 249 unsigned char old[MCOUNT_INSN_SIZE], *new;
d61f82d0
SR
250 int ret;
251
395a59d0 252 memcpy(old, &ftrace_call, MCOUNT_INSN_SIZE);
d61f82d0
SR
253 new = ftrace_call_replace(ip, (unsigned long)func);
254 ret = ftrace_modify_code(ip, old, new);
255
256 return ret;
257}
258
d61f82d0 259int __init ftrace_dyn_arch_init(void *data)
3d083395 260{
732f3ca7
SR
261 extern const unsigned char ftrace_test_p6nop[];
262 extern const unsigned char ftrace_test_nop5[];
263 extern const unsigned char ftrace_test_jmp[];
264 int faulted = 0;
d61f82d0 265
732f3ca7
SR
266 /*
267 * There is no good nop for all x86 archs.
268 * We will default to using the P6_NOP5, but first we
269 * will test to make sure that the nop will actually
270 * work on this CPU. If it faults, we will then
271 * go to a lesser efficient 5 byte nop. If that fails
272 * we then just use a jmp as our nop. This isn't the most
273 * efficient nop, but we can not use a multi part nop
274 * since we would then risk being preempted in the middle
275 * of that nop, and if we enabled tracing then, it might
276 * cause a system crash.
277 *
278 * TODO: check the cpuid to determine the best nop.
279 */
280 asm volatile (
732f3ca7
SR
281 "ftrace_test_jmp:"
282 "jmp ftrace_test_p6nop\n"
8b27386a
AK
283 "nop\n"
284 "nop\n"
285 "nop\n" /* 2 byte jmp + 3 bytes */
732f3ca7
SR
286 "ftrace_test_p6nop:"
287 P6_NOP5
288 "jmp 1f\n"
289 "ftrace_test_nop5:"
290 ".byte 0x66,0x66,0x66,0x66,0x90\n"
732f3ca7
SR
291 "1:"
292 ".section .fixup, \"ax\"\n"
293 "2: movl $1, %0\n"
294 " jmp ftrace_test_nop5\n"
295 "3: movl $2, %0\n"
296 " jmp 1b\n"
297 ".previous\n"
298 _ASM_EXTABLE(ftrace_test_p6nop, 2b)
299 _ASM_EXTABLE(ftrace_test_nop5, 3b)
300 : "=r"(faulted) : "0" (faulted));
301
302 switch (faulted) {
303 case 0:
304 pr_info("ftrace: converting mcount calls to 0f 1f 44 00 00\n");
8115f3f0 305 memcpy(ftrace_nop, ftrace_test_p6nop, MCOUNT_INSN_SIZE);
732f3ca7
SR
306 break;
307 case 1:
308 pr_info("ftrace: converting mcount calls to 66 66 66 66 90\n");
8115f3f0 309 memcpy(ftrace_nop, ftrace_test_nop5, MCOUNT_INSN_SIZE);
732f3ca7
SR
310 break;
311 case 2:
8b27386a 312 pr_info("ftrace: converting mcount calls to jmp . + 5\n");
8115f3f0 313 memcpy(ftrace_nop, ftrace_test_jmp, MCOUNT_INSN_SIZE);
732f3ca7
SR
314 break;
315 }
316
317 /* The return code is retured via data */
318 *(unsigned long *)data = 0;
dfa60aba 319
3d083395
SR
320 return 0;
321}
caf4b323 322#endif
e7d3737e 323
fb52607a 324#ifdef CONFIG_FUNCTION_GRAPH_TRACER
e7d3737e 325
5a45cfe1
SR
326#ifdef CONFIG_DYNAMIC_FTRACE
327extern void ftrace_graph_call(void);
328
329static int ftrace_mod_jmp(unsigned long ip,
330 int old_offset, int new_offset)
331{
332 unsigned char code[MCOUNT_INSN_SIZE];
333
334 if (probe_kernel_read(code, (void *)ip, MCOUNT_INSN_SIZE))
335 return -EFAULT;
336
337 if (code[0] != 0xe9 || old_offset != *(int *)(&code[1]))
338 return -EINVAL;
339
340 *(int *)(&code[1]) = new_offset;
341
342 if (do_ftrace_mod_code(ip, &code))
343 return -EPERM;
344
345 return 0;
346}
347
348int ftrace_enable_ftrace_graph_caller(void)
349{
350 unsigned long ip = (unsigned long)(&ftrace_graph_call);
351 int old_offset, new_offset;
352
353 old_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
354 new_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
355
356 return ftrace_mod_jmp(ip, old_offset, new_offset);
357}
358
359int ftrace_disable_ftrace_graph_caller(void)
360{
361 unsigned long ip = (unsigned long)(&ftrace_graph_call);
362 int old_offset, new_offset;
363
364 old_offset = (unsigned long)(&ftrace_graph_caller) - (ip + MCOUNT_INSN_SIZE);
365 new_offset = (unsigned long)(&ftrace_stub) - (ip + MCOUNT_INSN_SIZE);
366
367 return ftrace_mod_jmp(ip, old_offset, new_offset);
368}
369
e7d3737e
FW
370#endif /* !CONFIG_DYNAMIC_FTRACE */
371
372/* Add a function return address to the trace stack on thread info.*/
373static int push_return_trace(unsigned long ret, unsigned long long time,
287b6e68 374 unsigned long func, int *depth)
e7d3737e
FW
375{
376 int index;
f201ae23
FW
377
378 if (!current->ret_stack)
379 return -EBUSY;
e7d3737e
FW
380
381 /* The return trace stack is full */
f201ae23
FW
382 if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
383 atomic_inc(&current->trace_overrun);
e7d3737e 384 return -EBUSY;
0231022c 385 }
e7d3737e 386
f201ae23 387 index = ++current->curr_ret_stack;
e7d3737e 388 barrier();
f201ae23
FW
389 current->ret_stack[index].ret = ret;
390 current->ret_stack[index].func = func;
391 current->ret_stack[index].calltime = time;
287b6e68 392 *depth = index;
e7d3737e
FW
393
394 return 0;
395}
396
397/* Retrieve a function return address to the trace stack on thread info.*/
287b6e68 398static void pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret)
e7d3737e
FW
399{
400 int index;
401
f201ae23 402 index = current->curr_ret_stack;
62679efe
SR
403
404 if (unlikely(index < 0)) {
405 ftrace_graph_stop();
406 WARN_ON(1);
407 /* Might as well panic, otherwise we have no where to go */
408 *ret = (unsigned long)panic;
409 return;
410 }
411
f201ae23 412 *ret = current->ret_stack[index].ret;
287b6e68
FW
413 trace->func = current->ret_stack[index].func;
414 trace->calltime = current->ret_stack[index].calltime;
415 trace->overrun = atomic_read(&current->trace_overrun);
416 trace->depth = index;
e49dc19c 417 barrier();
f201ae23 418 current->curr_ret_stack--;
62679efe 419
e7d3737e
FW
420}
421
422/*
423 * Send the trace to the ring-buffer.
424 * @return the original return address.
425 */
426unsigned long ftrace_return_to_handler(void)
427{
fb52607a 428 struct ftrace_graph_ret trace;
287b6e68
FW
429 unsigned long ret;
430
431 pop_return_trace(&trace, &ret);
e7d3737e 432 trace.rettime = cpu_clock(raw_smp_processor_id());
287b6e68 433 ftrace_graph_return(&trace);
e7d3737e 434
62679efe
SR
435 if (unlikely(!ret)) {
436 ftrace_graph_stop();
437 WARN_ON(1);
438 /* Might as well panic. What else to do? */
439 ret = (unsigned long)panic;
440 }
441
287b6e68 442 return ret;
e7d3737e
FW
443}
444
445/*
446 * Hook the return address and push it in the stack of return addrs
447 * in current thread info.
448 */
449void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr)
450{
451 unsigned long old;
452 unsigned long long calltime;
453 int faulted;
287b6e68 454 struct ftrace_graph_ent trace;
e7d3737e
FW
455 unsigned long return_hooker = (unsigned long)
456 &return_to_handler;
457
458 /* Nmi's are currently unsupported */
9a5fd902 459 if (unlikely(in_nmi()))
380c4b14
FW
460 return;
461
462 if (unlikely(atomic_read(&current->tracing_graph_pause)))
e7d3737e
FW
463 return;
464
465 /*
466 * Protect against fault, even if it shouldn't
467 * happen. This tool is too much intrusive to
468 * ignore such a protection.
469 */
470 asm volatile(
347fdd9d
SR
471 "1: " _ASM_MOV " (%[parent_old]), %[old]\n"
472 "2: " _ASM_MOV " %[return_hooker], (%[parent_replaced])\n"
e7d3737e
FW
473 " movl $0, %[faulted]\n"
474
475 ".section .fixup, \"ax\"\n"
476 "3: movl $1, %[faulted]\n"
477 ".previous\n"
478
347fdd9d
SR
479 _ASM_EXTABLE(1b, 3b)
480 _ASM_EXTABLE(2b, 3b)
e7d3737e
FW
481
482 : [parent_replaced] "=r" (parent), [old] "=r" (old),
483 [faulted] "=r" (faulted)
484 : [parent_old] "0" (parent), [return_hooker] "r" (return_hooker)
485 : "memory"
486 );
487
14a866c5
SR
488 if (unlikely(faulted)) {
489 ftrace_graph_stop();
490 WARN_ON(1);
e7d3737e
FW
491 return;
492 }
493
e7d3737e
FW
494 calltime = cpu_clock(raw_smp_processor_id());
495
287b6e68
FW
496 if (push_return_trace(old, calltime,
497 self_addr, &trace.depth) == -EBUSY) {
e7d3737e 498 *parent = old;
287b6e68
FW
499 return;
500 }
501
502 trace.func = self_addr;
287b6e68 503
e49dc19c
SR
504 /* Only trace if the calling function expects to */
505 if (!ftrace_graph_entry(&trace)) {
506 current->curr_ret_stack--;
507 *parent = old;
508 }
e7d3737e 509}
fb52607a 510#endif /* CONFIG_FUNCTION_GRAPH_TRACER */