Merge tag 'selinux-pr-20220523' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.git] / arch / x86 / kernel / ftrace.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
3d083395 2/*
9d2099ab 3 * Dynamic function tracing support.
3d083395
SR
4 *
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
6 *
7 * Thanks goes to Ingo Molnar, for suggesting the idea.
8 * Mathieu Desnoyers, for suggesting postponing the modifications.
9 * Arjan van de Ven, for keeping me straight, and explaining to me
10 * the dangers of modifying code on the run.
11 */
12
3bb258bf
JP
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
3d083395
SR
15#include <linux/spinlock.h>
16#include <linux/hardirq.h>
6f93fc07 17#include <linux/uaccess.h>
3d083395
SR
18#include <linux/ftrace.h>
19#include <linux/percpu.h>
19b3e967 20#include <linux/sched.h>
f3bea491 21#include <linux/slab.h>
3d083395
SR
22#include <linux/init.h>
23#include <linux/list.h>
84e1c6bb 24#include <linux/module.h>
d5b844a2 25#include <linux/memory.h>
ac0b14dc 26#include <linux/vmalloc.h>
3d083395 27
47788c58
FW
28#include <trace/syscall.h>
29
d1163651 30#include <asm/set_memory.h>
59a094c9 31#include <asm/kprobes.h>
395a59d0 32#include <asm/ftrace.h>
732f3ca7 33#include <asm/nops.h>
9e298e86 34#include <asm/text-patching.h>
3d083395 35
caf4b323 36#ifdef CONFIG_DYNAMIC_FTRACE
3d083395 37
768ae440
PZ
38static int ftrace_poke_late = 0;
39
16239630 40int ftrace_arch_code_modify_prepare(void)
074376ac 41 __acquires(&text_mutex)
16239630 42{
39611265
SRV
43 /*
44 * Need to grab text_mutex to prevent a race from module loading
45 * and live kernel patching from changing the text permissions while
46 * ftrace has it set to "read/write".
47 */
d5b844a2 48 mutex_lock(&text_mutex);
768ae440 49 ftrace_poke_late = 1;
16239630
SR
50 return 0;
51}
52
53int ftrace_arch_code_modify_post_process(void)
074376ac 54 __releases(&text_mutex)
16239630 55{
768ae440
PZ
56 /*
57 * ftrace_make_{call,nop}() may be called during
58 * module load, and we need to finish the text_poke_queue()
59 * that they do, here.
60 */
61 text_poke_finish();
62 ftrace_poke_late = 0;
d5b844a2 63 mutex_unlock(&text_mutex);
16239630
SR
64 return 0;
65}
66
768ae440 67static const char *ftrace_nop_replace(void)
17666f02 68{
a89dfde3 69 return x86_nops[5];
17666f02
SR
70}
71
768ae440 72static const char *ftrace_call_replace(unsigned long ip, unsigned long addr)
caf4b323 73{
67c1d4a2 74 return text_gen_insn(CALL_INSN_OPCODE, (void *)ip, (void *)addr);
caf4b323
FW
75}
76
768ae440 77static int ftrace_verify_code(unsigned long ip, const char *old_code)
3d083395 78{
768ae440 79 char cur_code[MCOUNT_INSN_SIZE];
b05086c7 80
3d083395 81 /*
c5d641f9
LB
82 * Note:
83 * We are paranoid about modifying text, as if a bug was to happen, it
84 * could cause us to read or write to someplace that could cause harm.
85 * Carefully read and modify the code with probe_kernel_*(), and make
86 * sure what we read is what we expected it to be before modifying it.
3d083395 87 */
76aefee5 88 /* read the text we want to modify */
fe557319 89 if (copy_from_kernel_nofault(cur_code, (void *)ip, MCOUNT_INSN_SIZE)) {
768ae440 90 WARN_ON(1);
593eb8a2 91 return -EFAULT;
768ae440 92 }
6f93fc07 93
76aefee5 94 /* Make sure it is what we expect it to be */
768ae440
PZ
95 if (memcmp(cur_code, old_code, MCOUNT_INSN_SIZE) != 0) {
96 WARN_ON(1);
593eb8a2 97 return -EINVAL;
768ae440 98 }
3d083395 99
768ae440
PZ
100 return 0;
101}
6f93fc07 102
38ebd8d1
BP
103/*
104 * Marked __ref because it calls text_poke_early() which is .init.text. That is
105 * ok because that call will happen early, during boot, when .init sections are
106 * still present.
107 */
108static int __ref
768ae440
PZ
109ftrace_modify_code_direct(unsigned long ip, const char *old_code,
110 const char *new_code)
111{
112 int ret = ftrace_verify_code(ip, old_code);
113 if (ret)
114 return ret;
3d083395 115
768ae440
PZ
116 /* replace the text with the new text */
117 if (ftrace_poke_late)
118 text_poke_queue((void *)ip, new_code, MCOUNT_INSN_SIZE, NULL);
119 else
120 text_poke_early((void *)ip, new_code, MCOUNT_INSN_SIZE);
6f93fc07 121 return 0;
3d083395
SR
122}
123
768ae440 124int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
31e88909 125{
31e88909 126 unsigned long ip = rec->ip;
768ae440 127 const char *new, *old;
31e88909
SR
128
129 old = ftrace_call_replace(ip, addr);
130 new = ftrace_nop_replace();
131
8a4d0a68
SR
132 /*
133 * On boot up, and when modules are loaded, the MCOUNT_ADDR
134 * is converted to a nop, and will never become MCOUNT_ADDR
135 * again. This code is either running before SMP (on boot up)
136 * or before the code will ever be executed (module load).
137 * We do not want to use the breakpoint version in this case,
138 * just modify the code directly.
139 */
140 if (addr == MCOUNT_ADDR)
768ae440 141 return ftrace_modify_code_direct(ip, old, new);
b05086c7 142
768ae440
PZ
143 /*
144 * x86 overrides ftrace_replace_code -- this function will never be used
145 * in this case.
146 */
8a4d0a68
SR
147 WARN_ONCE(1, "invalid use of ftrace_make_nop");
148 return -EINVAL;
31e88909
SR
149}
150
151int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
152{
31e88909 153 unsigned long ip = rec->ip;
768ae440 154 const char *new, *old;
31e88909
SR
155
156 old = ftrace_nop_replace();
157 new = ftrace_call_replace(ip, addr);
158
8a4d0a68
SR
159 /* Should only be called when module is loaded */
160 return ftrace_modify_code_direct(rec->ip, old, new);
d61f82d0
SR
161}
162
08f6fba5
SR
163/*
164 * Should never be called:
165 * As it is only called by __ftrace_replace_code() which is called by
166 * ftrace_replace_code() that x86 overrides, and by ftrace_update_code()
167 * which is called to turn mcount into nops or nops into function calls
168 * but not to convert a function from not using regs to one that uses
169 * regs, which ftrace_modify_call() is for.
170 */
171int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
172 unsigned long addr)
173{
174 WARN_ON(1);
175 return -EINVAL;
176}
08f6fba5 177
87fbb2ac 178int ftrace_update_ftrace_func(ftrace_func_t func)
08d636b6 179{
ab4ead02 180 unsigned long ip;
768ae440 181 const char *new;
ab4ead02 182
768ae440
PZ
183 ip = (unsigned long)(&ftrace_call);
184 new = ftrace_call_replace(ip, (unsigned long)func);
185 text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
08d636b6 186
768ae440
PZ
187 ip = (unsigned long)(&ftrace_regs_call);
188 new = ftrace_call_replace(ip, (unsigned long)func);
189 text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
08d636b6
SR
190
191 return 0;
192}
193
e4f5d544 194void ftrace_replace_code(int enable)
08d636b6
SR
195{
196 struct ftrace_rec_iter *iter;
197 struct dyn_ftrace *rec;
768ae440 198 const char *new, *old;
08d636b6
SR
199 int ret;
200
201 for_ftrace_rec_iter(iter) {
202 rec = ftrace_rec_iter_record(iter);
203
768ae440
PZ
204 switch (ftrace_test_record(rec, enable)) {
205 case FTRACE_UPDATE_IGNORE:
206 default:
207 continue;
08d636b6 208
768ae440
PZ
209 case FTRACE_UPDATE_MAKE_CALL:
210 old = ftrace_nop_replace();
211 break;
08d636b6 212
768ae440
PZ
213 case FTRACE_UPDATE_MODIFY_CALL:
214 case FTRACE_UPDATE_MAKE_NOP:
215 old = ftrace_call_replace(rec->ip, ftrace_get_addr_curr(rec));
216 break;
217 }
08d636b6 218
768ae440
PZ
219 ret = ftrace_verify_code(rec->ip, old);
220 if (ret) {
221 ftrace_bug(ret, rec);
222 return;
223 }
08d636b6
SR
224 }
225
08d636b6
SR
226 for_ftrace_rec_iter(iter) {
227 rec = ftrace_rec_iter_record(iter);
228
768ae440
PZ
229 switch (ftrace_test_record(rec, enable)) {
230 case FTRACE_UPDATE_IGNORE:
231 default:
232 continue;
08d636b6 233
768ae440
PZ
234 case FTRACE_UPDATE_MAKE_CALL:
235 case FTRACE_UPDATE_MODIFY_CALL:
236 new = ftrace_call_replace(rec->ip, ftrace_get_addr_new(rec));
237 break;
08d636b6 238
768ae440
PZ
239 case FTRACE_UPDATE_MAKE_NOP:
240 new = ftrace_nop_replace();
241 break;
242 }
08d636b6 243
768ae440
PZ
244 text_poke_queue((void *)rec->ip, new, MCOUNT_INSN_SIZE, NULL);
245 ftrace_update_record(rec, enable);
08d636b6 246 }
768ae440 247 text_poke_finish();
8a4d0a68
SR
248}
249
08d636b6
SR
250void arch_ftrace_update_code(int command)
251{
e4f5d544 252 ftrace_modify_all_code(command);
08d636b6
SR
253}
254
f3bea491
SRRH
255/* Currently only x86_64 supports dynamic trampolines */
256#ifdef CONFIG_X86_64
257
258#ifdef CONFIG_MODULES
259#include <linux/moduleloader.h>
260/* Module allocation simplifies allocating memory for code */
261static inline void *alloc_tramp(unsigned long size)
262{
263 return module_alloc(size);
264}
7fdfe1e4 265static inline void tramp_free(void *tramp)
f3bea491 266{
be1f221c 267 module_memfree(tramp);
f3bea491
SRRH
268}
269#else
270/* Trampolines can only be created if modules are supported */
271static inline void *alloc_tramp(unsigned long size)
272{
273 return NULL;
274}
7fdfe1e4 275static inline void tramp_free(void *tramp) { }
f3bea491
SRRH
276#endif
277
278/* Defined as markers to the end of the ftrace default trampolines */
f3bea491 279extern void ftrace_regs_caller_end(void);
0298739b
PZ
280extern void ftrace_regs_caller_ret(void);
281extern void ftrace_caller_end(void);
f3bea491
SRRH
282extern void ftrace_caller_op_ptr(void);
283extern void ftrace_regs_caller_op_ptr(void);
fe58acef 284extern void ftrace_regs_caller_jmp(void);
f3bea491
SRRH
285
286/* movq function_trace_op(%rip), %rdx */
287/* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */
288#define OP_REF_SIZE 7
289
290/*
291 * The ftrace_ops is passed to the function callback. Since the
292 * trampoline only services a single ftrace_ops, we can pass in
293 * that ops directly.
294 *
295 * The ftrace_op_code_union is used to create a pointer to the
296 * ftrace_ops that will be passed to the callback function.
297 */
298union ftrace_op_code_union {
299 char code[OP_REF_SIZE];
300 struct {
301 char op[3];
302 int offset;
303 } __attribute__((packed));
304};
305
e463a09a 306#define RET_SIZE 1 + IS_ENABLED(CONFIG_SLS)
d2a68c4e 307
aec0be2d
SRRH
308static unsigned long
309create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
f3bea491 310{
f3bea491
SRRH
311 unsigned long start_offset;
312 unsigned long end_offset;
313 unsigned long op_offset;
768ae440 314 unsigned long call_offset;
fe58acef 315 unsigned long jmp_offset;
f3bea491 316 unsigned long offset;
3c0dab44 317 unsigned long npages;
f3bea491 318 unsigned long size;
f3bea491
SRRH
319 unsigned long *ptr;
320 void *trampoline;
d2a68c4e 321 void *ip;
f3bea491
SRRH
322 /* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */
323 unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 };
e52fc2cf 324 unsigned const char retq[] = { RET_INSN_OPCODE, INT3_INSN_OPCODE };
f3bea491
SRRH
325 union ftrace_op_code_union op_ptr;
326 int ret;
327
328 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
329 start_offset = (unsigned long)ftrace_regs_caller;
330 end_offset = (unsigned long)ftrace_regs_caller_end;
331 op_offset = (unsigned long)ftrace_regs_caller_op_ptr;
768ae440 332 call_offset = (unsigned long)ftrace_regs_call;
fe58acef 333 jmp_offset = (unsigned long)ftrace_regs_caller_jmp;
f3bea491
SRRH
334 } else {
335 start_offset = (unsigned long)ftrace_caller;
0298739b 336 end_offset = (unsigned long)ftrace_caller_end;
f3bea491 337 op_offset = (unsigned long)ftrace_caller_op_ptr;
768ae440 338 call_offset = (unsigned long)ftrace_call;
fe58acef 339 jmp_offset = 0;
f3bea491
SRRH
340 }
341
342 size = end_offset - start_offset;
343
344 /*
345 * Allocate enough size to store the ftrace_caller code,
d2a68c4e
SRV
346 * the iret , as well as the address of the ftrace_ops this
347 * trampoline is used for.
f3bea491 348 */
d2a68c4e 349 trampoline = alloc_tramp(size + RET_SIZE + sizeof(void *));
f3bea491
SRRH
350 if (!trampoline)
351 return 0;
352
d2a68c4e 353 *tramp_size = size + RET_SIZE + sizeof(void *);
3c0dab44 354 npages = DIV_ROUND_UP(*tramp_size, PAGE_SIZE);
aec0be2d 355
f3bea491 356 /* Copy ftrace_caller onto the trampoline memory */
fe557319 357 ret = copy_from_kernel_nofault(trampoline, (void *)start_offset, size);
d2a68c4e
SRV
358 if (WARN_ON(ret < 0))
359 goto fail;
f3bea491 360
d2a68c4e 361 ip = trampoline + size;
e52fc2cf 362 memcpy(ip, retq, RET_SIZE);
f3bea491 363
fe58acef
SRV
364 /* No need to test direct calls on created trampolines */
365 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
366 /* NOP the jnz 1f; but make sure it's a 2 byte jnz */
367 ip = trampoline + (jmp_offset - start_offset);
368 if (WARN_ON(*(char *)ip != 0x75))
369 goto fail;
a89dfde3 370 ret = copy_from_kernel_nofault(ip, x86_nops[2], 2);
fe58acef
SRV
371 if (ret < 0)
372 goto fail;
373 }
374
f3bea491
SRRH
375 /*
376 * The address of the ftrace_ops that is used for this trampoline
377 * is stored at the end of the trampoline. This will be used to
378 * load the third parameter for the callback. Basically, that
379 * location at the end of the trampoline takes the place of
380 * the global function_trace_op variable.
381 */
382
d2a68c4e 383 ptr = (unsigned long *)(trampoline + size + RET_SIZE);
f3bea491
SRRH
384 *ptr = (unsigned long)ops;
385
386 op_offset -= start_offset;
387 memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE);
388
389 /* Are we pointing to the reference? */
d2a68c4e
SRV
390 if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0))
391 goto fail;
f3bea491
SRRH
392
393 /* Load the contents of ptr into the callback parameter */
394 offset = (unsigned long)ptr;
395 offset -= (unsigned long)trampoline + op_offset + OP_REF_SIZE;
396
397 op_ptr.offset = offset;
398
399 /* put in the new offset to the ftrace_ops */
400 memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE);
401
768ae440
PZ
402 /* put in the call to the function */
403 mutex_lock(&text_mutex);
404 call_offset -= start_offset;
405 memcpy(trampoline + call_offset,
406 text_gen_insn(CALL_INSN_OPCODE,
407 trampoline + call_offset,
408 ftrace_ops_get_func(ops)), CALL_INSN_SIZE);
409 mutex_unlock(&text_mutex);
410
f3bea491
SRRH
411 /* ALLOC_TRAMP flags lets us know we created it */
412 ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
413
7fdfe1e4
RE
414 set_vm_flush_reset_perms(trampoline);
415
59566b0b
SRV
416 if (likely(system_state != SYSTEM_BOOTING))
417 set_memory_ro((unsigned long)trampoline, npages);
3c0dab44 418 set_memory_x((unsigned long)trampoline, npages);
f3bea491 419 return (unsigned long)trampoline;
d2a68c4e 420fail:
7fdfe1e4 421 tramp_free(trampoline);
d2a68c4e 422 return 0;
f3bea491
SRRH
423}
424
59566b0b
SRV
425void set_ftrace_ops_ro(void)
426{
427 struct ftrace_ops *ops;
428 unsigned long start_offset;
429 unsigned long end_offset;
430 unsigned long npages;
431 unsigned long size;
432
433 do_for_each_ftrace_op(ops, ftrace_ops_list) {
434 if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
435 continue;
436
437 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
438 start_offset = (unsigned long)ftrace_regs_caller;
439 end_offset = (unsigned long)ftrace_regs_caller_end;
440 } else {
441 start_offset = (unsigned long)ftrace_caller;
7c0577f4 442 end_offset = (unsigned long)ftrace_caller_end;
59566b0b
SRV
443 }
444 size = end_offset - start_offset;
445 size = size + RET_SIZE + sizeof(void *);
446 npages = DIV_ROUND_UP(size, PAGE_SIZE);
447 set_memory_ro((unsigned long)ops->trampoline, npages);
448 } while_for_each_ftrace_op(ops);
449}
450
15d5b02c
SRRH
451static unsigned long calc_trampoline_call_offset(bool save_regs)
452{
453 unsigned long start_offset;
454 unsigned long call_offset;
455
456 if (save_regs) {
457 start_offset = (unsigned long)ftrace_regs_caller;
458 call_offset = (unsigned long)ftrace_regs_call;
459 } else {
460 start_offset = (unsigned long)ftrace_caller;
461 call_offset = (unsigned long)ftrace_call;
462 }
463
464 return call_offset - start_offset;
465}
466
f3bea491
SRRH
467void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
468{
469 ftrace_func_t func;
f3bea491
SRRH
470 unsigned long offset;
471 unsigned long ip;
aec0be2d 472 unsigned int size;
768ae440 473 const char *new;
f3bea491 474
768ae440 475 if (!ops->trampoline) {
aec0be2d 476 ops->trampoline = create_trampoline(ops, &size);
f3bea491
SRRH
477 if (!ops->trampoline)
478 return;
aec0be2d 479 ops->trampoline_size = size;
768ae440 480 return;
f3bea491
SRRH
481 }
482
768ae440
PZ
483 /*
484 * The ftrace_ops caller may set up its own trampoline.
485 * In such a case, this code must not modify it.
486 */
487 if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
488 return;
489
15d5b02c 490 offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
f3bea491 491 ip = ops->trampoline + offset;
f3bea491
SRRH
492 func = ftrace_ops_get_func(ops);
493
768ae440 494 mutex_lock(&text_mutex);
f3bea491
SRRH
495 /* Do a safe modify in case the trampoline is executing */
496 new = ftrace_call_replace(ip, (unsigned long)func);
768ae440
PZ
497 text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
498 mutex_unlock(&text_mutex);
f3bea491 499}
15d5b02c
SRRH
500
501/* Return the address of the function the trampoline calls */
502static void *addr_from_call(void *ptr)
503{
67c1d4a2 504 union text_poke_insn call;
15d5b02c
SRRH
505 int ret;
506
fe557319 507 ret = copy_from_kernel_nofault(&call, ptr, CALL_INSN_SIZE);
15d5b02c
SRRH
508 if (WARN_ON_ONCE(ret < 0))
509 return NULL;
510
511 /* Make sure this is a call */
67c1d4a2
PZ
512 if (WARN_ON_ONCE(call.opcode != CALL_INSN_OPCODE)) {
513 pr_warn("Expected E8, got %x\n", call.opcode);
15d5b02c
SRRH
514 return NULL;
515 }
516
67c1d4a2 517 return ptr + CALL_INSN_SIZE + call.disp;
15d5b02c
SRRH
518}
519
0c0593b4 520void prepare_ftrace_return(unsigned long ip, unsigned long *parent,
15d5b02c
SRRH
521 unsigned long frame_pointer);
522
523/*
524 * If the ops->trampoline was not allocated, then it probably
525 * has a static trampoline func, or is the ftrace caller itself.
526 */
527static void *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
528{
529 unsigned long offset;
530 bool save_regs = rec->flags & FTRACE_FL_REGS_EN;
531 void *ptr;
532
533 if (ops && ops->trampoline) {
0c0593b4
SRV
534#if !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS) && \
535 defined(CONFIG_FUNCTION_GRAPH_TRACER)
15d5b02c
SRRH
536 /*
537 * We only know about function graph tracer setting as static
538 * trampoline.
539 */
540 if (ops->trampoline == FTRACE_GRAPH_ADDR)
541 return (void *)prepare_ftrace_return;
542#endif
543 return NULL;
544 }
545
546 offset = calc_trampoline_call_offset(save_regs);
547
548 if (save_regs)
549 ptr = (void *)FTRACE_REGS_ADDR + offset;
550 else
551 ptr = (void *)FTRACE_ADDR + offset;
552
553 return addr_from_call(ptr);
554}
555
556void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
557{
558 unsigned long offset;
559
560 /* If we didn't allocate this trampoline, consider it static */
561 if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
562 return static_tramp_func(ops, rec);
563
564 offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
565 return addr_from_call((void *)ops->trampoline + offset);
566}
567
12cce594
SRRH
568void arch_ftrace_trampoline_free(struct ftrace_ops *ops)
569{
570 if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
571 return;
572
7fdfe1e4 573 tramp_free((void *)ops->trampoline);
12cce594
SRRH
574 ops->trampoline = 0;
575}
15d5b02c 576
f3bea491
SRRH
577#endif /* CONFIG_X86_64 */
578#endif /* CONFIG_DYNAMIC_FTRACE */
579
580#ifdef CONFIG_FUNCTION_GRAPH_TRACER
581
e999995c 582#if defined(CONFIG_DYNAMIC_FTRACE) && !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS)
0c0593b4 583extern void ftrace_graph_call(void);
768ae440 584static const char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
745cfeaa 585{
67c1d4a2 586 return text_gen_insn(JMP32_INSN_OPCODE, (void *)ip, (void *)addr);
745cfeaa
SRV
587}
588
87fbb2ac
SRRH
589static int ftrace_mod_jmp(unsigned long ip, void *func)
590{
768ae440 591 const char *new;
5a45cfe1 592
87fbb2ac 593 new = ftrace_jmp_replace(ip, (unsigned long)func);
768ae440
PZ
594 text_poke_bp((void *)ip, new, MCOUNT_INSN_SIZE, NULL);
595 return 0;
5a45cfe1
SR
596}
597
598int ftrace_enable_ftrace_graph_caller(void)
599{
600 unsigned long ip = (unsigned long)(&ftrace_graph_call);
5a45cfe1 601
87fbb2ac 602 return ftrace_mod_jmp(ip, &ftrace_graph_caller);
5a45cfe1
SR
603}
604
605int ftrace_disable_ftrace_graph_caller(void)
606{
607 unsigned long ip = (unsigned long)(&ftrace_graph_call);
5a45cfe1 608
87fbb2ac 609 return ftrace_mod_jmp(ip, &ftrace_stub);
5a45cfe1 610}
e999995c 611#endif /* CONFIG_DYNAMIC_FTRACE && !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
e7d3737e 612
e7d3737e
FW
613/*
614 * Hook the return address and push it in the stack of return addrs
615 * in current thread info.
616 */
8646698a 617void prepare_ftrace_return(unsigned long ip, unsigned long *parent,
71e308a2 618 unsigned long frame_pointer)
e7d3737e 619{
768ae440 620 unsigned long return_hooker = (unsigned long)&return_to_handler;
0c0593b4 621 int bit;
e7d3737e 622
34a477e5
JP
623 /*
624 * When resuming from suspend-to-ram, this function can be indirectly
625 * called from early CPU startup code while the CPU is in real mode,
626 * which would fail miserably. Make sure the stack pointer is a
627 * virtual address.
628 *
629 * This check isn't as accurate as virt_addr_valid(), but it should be
630 * good enough for this purpose, and it's fast.
631 */
632 if (unlikely((long)__builtin_frame_address(0) >= 0))
633 return;
634
84b2bc7f
SRRH
635 if (unlikely(ftrace_graph_is_dead()))
636 return;
637
380c4b14 638 if (unlikely(atomic_read(&current->tracing_graph_pause)))
e7d3737e
FW
639 return;
640
0c0593b4
SRV
641 bit = ftrace_test_recursion_trylock(ip, *parent);
642 if (bit < 0)
643 return;
644
8646698a
SRV
645 if (!function_graph_enter(*parent, ip, frame_pointer, parent))
646 *parent = return_hooker;
0c0593b4
SRV
647
648 ftrace_test_recursion_unlock(bit);
649}
650
651#ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
652void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
653 struct ftrace_ops *op, struct ftrace_regs *fregs)
654{
655 struct pt_regs *regs = &fregs->regs;
656 unsigned long *stack = (unsigned long *)kernel_stack_pointer(regs);
657
658 prepare_ftrace_return(ip, (unsigned long *)stack, 0);
e7d3737e 659}
0c0593b4
SRV
660#endif
661
fb52607a 662#endif /* CONFIG_FUNCTION_GRAPH_TRACER */