1 // SPDX-License-Identifier: GPL-2.0
3 * Dynamic function tracing support.
5 * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
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.
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/spinlock.h>
16 #include <linux/hardirq.h>
17 #include <linux/uaccess.h>
18 #include <linux/ftrace.h>
19 #include <linux/percpu.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/memory.h>
27 #include <trace/syscall.h>
29 #include <asm/set_memory.h>
30 #include <asm/kprobes.h>
31 #include <asm/sections.h>
32 #include <asm/ftrace.h>
34 #include <asm/text-patching.h>
36 #ifdef CONFIG_DYNAMIC_FTRACE
38 int ftrace_arch_code_modify_prepare(void)
39 __acquires(&text_mutex)
41 mutex_lock(&text_mutex);
43 set_all_modules_text_rw();
47 int ftrace_arch_code_modify_post_process(void)
48 __releases(&text_mutex)
50 set_all_modules_text_ro();
52 mutex_unlock(&text_mutex);
56 union ftrace_code_union {
57 char code[MCOUNT_INSN_SIZE];
61 } __attribute__((packed));
64 static int ftrace_calc_offset(long ip, long addr)
66 return (int)(addr - ip);
69 static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
71 static union ftrace_code_union calc;
74 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
77 * No locking needed, this must be called via kstop_machine
78 * which in essence is like running on a uniprocessor machine.
84 within(unsigned long addr, unsigned long start, unsigned long end)
86 return addr >= start && addr < end;
89 static unsigned long text_ip_addr(unsigned long ip)
92 * On x86_64, kernel text mappings are mapped read-only, so we use
93 * the kernel identity mapping instead of the kernel text mapping
94 * to modify the kernel text.
96 * For 32bit kernels, these mappings are same and we can use
97 * kernel identity mapping to modify code.
99 if (within(ip, (unsigned long)_text, (unsigned long)_etext))
100 ip = (unsigned long)__va(__pa_symbol(ip));
105 static const unsigned char *ftrace_nop_replace(void)
107 return ideal_nops[NOP_ATOMIC5];
111 ftrace_modify_code_direct(unsigned long ip, unsigned const char *old_code,
112 unsigned const char *new_code)
114 unsigned char replaced[MCOUNT_INSN_SIZE];
116 ftrace_expected = old_code;
120 * We are paranoid about modifying text, as if a bug was to happen, it
121 * could cause us to read or write to someplace that could cause harm.
122 * Carefully read and modify the code with probe_kernel_*(), and make
123 * sure what we read is what we expected it to be before modifying it.
126 /* read the text we want to modify */
127 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
130 /* Make sure it is what we expect it to be */
131 if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
134 ip = text_ip_addr(ip);
136 /* replace the text with the new text */
137 if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
145 int ftrace_make_nop(struct module *mod,
146 struct dyn_ftrace *rec, unsigned long addr)
148 unsigned const char *new, *old;
149 unsigned long ip = rec->ip;
151 old = ftrace_call_replace(ip, addr);
152 new = ftrace_nop_replace();
155 * On boot up, and when modules are loaded, the MCOUNT_ADDR
156 * is converted to a nop, and will never become MCOUNT_ADDR
157 * again. This code is either running before SMP (on boot up)
158 * or before the code will ever be executed (module load).
159 * We do not want to use the breakpoint version in this case,
160 * just modify the code directly.
162 if (addr == MCOUNT_ADDR)
163 return ftrace_modify_code_direct(rec->ip, old, new);
165 ftrace_expected = NULL;
167 /* Normal cases use add_brk_on_nop */
168 WARN_ONCE(1, "invalid use of ftrace_make_nop");
172 int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
174 unsigned const char *new, *old;
175 unsigned long ip = rec->ip;
177 old = ftrace_nop_replace();
178 new = ftrace_call_replace(ip, addr);
180 /* Should only be called when module is loaded */
181 return ftrace_modify_code_direct(rec->ip, old, new);
185 * The modifying_ftrace_code is used to tell the breakpoint
186 * handler to call ftrace_int3_handler(). If it fails to
187 * call this handler for a breakpoint added by ftrace, then
188 * the kernel may crash.
190 * As atomic_writes on x86 do not need a barrier, we do not
191 * need to add smp_mb()s for this to work. It is also considered
192 * that we can not read the modifying_ftrace_code before
193 * executing the breakpoint. That would be quite remarkable if
194 * it could do that. Here's the flow that is required:
200 * <trap-int3> // implicit (r)mb
201 * if (atomic_read(mfc))
202 * call ftrace_int3_handler()
204 * Then when we are finished:
208 * If we hit a breakpoint that was not set by ftrace, it does not
209 * matter if ftrace_int3_handler() is called or not. It will
210 * simply be ignored. But it is crucial that a ftrace nop/caller
211 * breakpoint is handled. No other user should ever place a
212 * breakpoint on an ftrace nop/caller location. It must only
213 * be done by this code.
215 atomic_t modifying_ftrace_code __read_mostly;
218 ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
219 unsigned const char *new_code);
222 * Should never be called:
223 * As it is only called by __ftrace_replace_code() which is called by
224 * ftrace_replace_code() that x86 overrides, and by ftrace_update_code()
225 * which is called to turn mcount into nops or nops into function calls
226 * but not to convert a function from not using regs to one that uses
227 * regs, which ftrace_modify_call() is for.
229 int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
233 ftrace_expected = NULL;
237 static unsigned long ftrace_update_func;
238 static unsigned long ftrace_update_func_call;
240 static int update_ftrace_func(unsigned long ip, void *new)
242 unsigned char old[MCOUNT_INSN_SIZE];
245 memcpy(old, (void *)ip, MCOUNT_INSN_SIZE);
247 ftrace_update_func = ip;
248 /* Make sure the breakpoints see the ftrace_update_func update */
251 /* See comment above by declaration of modifying_ftrace_code */
252 atomic_inc(&modifying_ftrace_code);
254 ret = ftrace_modify_code(ip, old, new);
256 atomic_dec(&modifying_ftrace_code);
261 int ftrace_update_ftrace_func(ftrace_func_t func)
263 unsigned long ip = (unsigned long)(&ftrace_call);
267 ftrace_update_func_call = (unsigned long)func;
269 new = ftrace_call_replace(ip, (unsigned long)func);
270 ret = update_ftrace_func(ip, new);
272 /* Also update the regs callback function */
274 ip = (unsigned long)(&ftrace_regs_call);
275 new = ftrace_call_replace(ip, (unsigned long)func);
276 ret = update_ftrace_func(ip, new);
282 static int is_ftrace_caller(unsigned long ip)
284 if (ip == ftrace_update_func)
291 * A breakpoint was added to the code address we are about to
292 * modify, and this is the handle that will just skip over it.
293 * We are either changing a nop into a trace call, or a trace
294 * call to a nop. While the change is taking place, we treat
295 * it just like it was a nop.
297 int ftrace_int3_handler(struct pt_regs *regs)
301 if (WARN_ON_ONCE(!regs))
304 ip = regs->ip - INT3_INSN_SIZE;
307 if (ftrace_location(ip)) {
308 int3_emulate_call(regs, (unsigned long)ftrace_regs_caller);
310 } else if (is_ftrace_caller(ip)) {
311 if (!ftrace_update_func_call) {
312 int3_emulate_jmp(regs, ip + CALL_INSN_SIZE);
315 int3_emulate_call(regs, ftrace_update_func_call);
319 if (ftrace_location(ip) || is_ftrace_caller(ip)) {
320 int3_emulate_jmp(regs, ip + CALL_INSN_SIZE);
328 static int ftrace_write(unsigned long ip, const char *val, int size)
330 ip = text_ip_addr(ip);
332 if (probe_kernel_write((void *)ip, val, size))
338 static int add_break(unsigned long ip, const char *old)
340 unsigned char replaced[MCOUNT_INSN_SIZE];
341 unsigned char brk = BREAKPOINT_INSTRUCTION;
343 if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
346 ftrace_expected = old;
348 /* Make sure it is what we expect it to be */
349 if (memcmp(replaced, old, MCOUNT_INSN_SIZE) != 0)
352 return ftrace_write(ip, &brk, 1);
355 static int add_brk_on_call(struct dyn_ftrace *rec, unsigned long addr)
357 unsigned const char *old;
358 unsigned long ip = rec->ip;
360 old = ftrace_call_replace(ip, addr);
362 return add_break(rec->ip, old);
366 static int add_brk_on_nop(struct dyn_ftrace *rec)
368 unsigned const char *old;
370 old = ftrace_nop_replace();
372 return add_break(rec->ip, old);
375 static int add_breakpoints(struct dyn_ftrace *rec, int enable)
377 unsigned long ftrace_addr;
380 ftrace_addr = ftrace_get_addr_curr(rec);
382 ret = ftrace_test_record(rec, enable);
385 case FTRACE_UPDATE_IGNORE:
388 case FTRACE_UPDATE_MAKE_CALL:
389 /* converting nop to call */
390 return add_brk_on_nop(rec);
392 case FTRACE_UPDATE_MODIFY_CALL:
393 case FTRACE_UPDATE_MAKE_NOP:
394 /* converting a call to a nop */
395 return add_brk_on_call(rec, ftrace_addr);
401 * On error, we need to remove breakpoints. This needs to
402 * be done caefully. If the address does not currently have a
403 * breakpoint, we know we are done. Otherwise, we look at the
404 * remaining 4 bytes of the instruction. If it matches a nop
405 * we replace the breakpoint with the nop. Otherwise we replace
406 * it with the call instruction.
408 static int remove_breakpoint(struct dyn_ftrace *rec)
410 unsigned char ins[MCOUNT_INSN_SIZE];
411 unsigned char brk = BREAKPOINT_INSTRUCTION;
412 const unsigned char *nop;
413 unsigned long ftrace_addr;
414 unsigned long ip = rec->ip;
416 /* If we fail the read, just give up */
417 if (probe_kernel_read(ins, (void *)ip, MCOUNT_INSN_SIZE))
420 /* If this does not have a breakpoint, we are done */
424 nop = ftrace_nop_replace();
427 * If the last 4 bytes of the instruction do not match
428 * a nop, then we assume that this is a call to ftrace_addr.
430 if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0) {
432 * For extra paranoidism, we check if the breakpoint is on
433 * a call that would actually jump to the ftrace_addr.
434 * If not, don't touch the breakpoint, we make just create
437 ftrace_addr = ftrace_get_addr_new(rec);
438 nop = ftrace_call_replace(ip, ftrace_addr);
440 if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) == 0)
443 /* Check both ftrace_addr and ftrace_old_addr */
444 ftrace_addr = ftrace_get_addr_curr(rec);
445 nop = ftrace_call_replace(ip, ftrace_addr);
447 ftrace_expected = nop;
449 if (memcmp(&ins[1], &nop[1], MCOUNT_INSN_SIZE - 1) != 0)
454 return ftrace_write(ip, nop, 1);
457 static int add_update_code(unsigned long ip, unsigned const char *new)
459 /* skip breakpoint */
462 return ftrace_write(ip, new, MCOUNT_INSN_SIZE - 1);
465 static int add_update_call(struct dyn_ftrace *rec, unsigned long addr)
467 unsigned long ip = rec->ip;
468 unsigned const char *new;
470 new = ftrace_call_replace(ip, addr);
471 return add_update_code(ip, new);
474 static int add_update_nop(struct dyn_ftrace *rec)
476 unsigned long ip = rec->ip;
477 unsigned const char *new;
479 new = ftrace_nop_replace();
480 return add_update_code(ip, new);
483 static int add_update(struct dyn_ftrace *rec, int enable)
485 unsigned long ftrace_addr;
488 ret = ftrace_test_record(rec, enable);
490 ftrace_addr = ftrace_get_addr_new(rec);
493 case FTRACE_UPDATE_IGNORE:
496 case FTRACE_UPDATE_MODIFY_CALL:
497 case FTRACE_UPDATE_MAKE_CALL:
498 /* converting nop to call */
499 return add_update_call(rec, ftrace_addr);
501 case FTRACE_UPDATE_MAKE_NOP:
502 /* converting a call to a nop */
503 return add_update_nop(rec);
509 static int finish_update_call(struct dyn_ftrace *rec, unsigned long addr)
511 unsigned long ip = rec->ip;
512 unsigned const char *new;
514 new = ftrace_call_replace(ip, addr);
516 return ftrace_write(ip, new, 1);
519 static int finish_update_nop(struct dyn_ftrace *rec)
521 unsigned long ip = rec->ip;
522 unsigned const char *new;
524 new = ftrace_nop_replace();
526 return ftrace_write(ip, new, 1);
529 static int finish_update(struct dyn_ftrace *rec, int enable)
531 unsigned long ftrace_addr;
534 ret = ftrace_update_record(rec, enable);
536 ftrace_addr = ftrace_get_addr_new(rec);
539 case FTRACE_UPDATE_IGNORE:
542 case FTRACE_UPDATE_MODIFY_CALL:
543 case FTRACE_UPDATE_MAKE_CALL:
544 /* converting nop to call */
545 return finish_update_call(rec, ftrace_addr);
547 case FTRACE_UPDATE_MAKE_NOP:
548 /* converting a call to a nop */
549 return finish_update_nop(rec);
555 static void do_sync_core(void *data)
560 static void run_sync(void)
564 /* No need to sync if there's only one CPU */
565 if (num_online_cpus() == 1)
568 enable_irqs = irqs_disabled();
570 /* We may be called with interrupts disabled (on bootup). */
573 on_each_cpu(do_sync_core, NULL, 1);
578 void ftrace_replace_code(int enable)
580 struct ftrace_rec_iter *iter;
581 struct dyn_ftrace *rec;
582 const char *report = "adding breakpoints";
586 for_ftrace_rec_iter(iter) {
587 rec = ftrace_rec_iter_record(iter);
589 ret = add_breakpoints(rec, enable);
591 goto remove_breakpoints;
597 report = "updating code";
600 for_ftrace_rec_iter(iter) {
601 rec = ftrace_rec_iter_record(iter);
603 ret = add_update(rec, enable);
605 goto remove_breakpoints;
611 report = "removing breakpoints";
614 for_ftrace_rec_iter(iter) {
615 rec = ftrace_rec_iter_record(iter);
617 ret = finish_update(rec, enable);
619 goto remove_breakpoints;
628 pr_warn("Failed on %s (%d):\n", report, count);
629 ftrace_bug(ret, rec);
630 for_ftrace_rec_iter(iter) {
631 rec = ftrace_rec_iter_record(iter);
633 * Breakpoints are handled only when this function is in
634 * progress. The system could not work with them.
636 if (remove_breakpoint(rec))
643 ftrace_modify_code(unsigned long ip, unsigned const char *old_code,
644 unsigned const char *new_code)
648 ret = add_break(ip, old_code);
654 ret = add_update_code(ip, new_code);
660 ret = ftrace_write(ip, new_code, 1);
662 * The breakpoint is handled only when this function is in progress.
663 * The system could not work if we could not remove it.
671 /* Also here the system could not work with the breakpoint */
672 if (ftrace_write(ip, old_code, 1))
677 void arch_ftrace_update_code(int command)
679 /* See comment above by declaration of modifying_ftrace_code */
680 atomic_inc(&modifying_ftrace_code);
682 ftrace_modify_all_code(command);
684 atomic_dec(&modifying_ftrace_code);
687 int __init ftrace_dyn_arch_init(void)
692 #if defined(CONFIG_X86_64) || defined(CONFIG_FUNCTION_GRAPH_TRACER)
693 static unsigned char *ftrace_jmp_replace(unsigned long ip, unsigned long addr)
695 static union ftrace_code_union calc;
697 /* Jmp not a call (ignore the .e8) */
699 calc.offset = ftrace_calc_offset(ip + MCOUNT_INSN_SIZE, addr);
702 * ftrace external locks synchronize the access to the static variable.
708 /* Currently only x86_64 supports dynamic trampolines */
711 #ifdef CONFIG_MODULES
712 #include <linux/moduleloader.h>
713 /* Module allocation simplifies allocating memory for code */
714 static inline void *alloc_tramp(unsigned long size)
716 return module_alloc(size);
718 static inline void tramp_free(void *tramp, int size)
720 int npages = PAGE_ALIGN(size) >> PAGE_SHIFT;
722 set_memory_nx((unsigned long)tramp, npages);
723 set_memory_rw((unsigned long)tramp, npages);
724 module_memfree(tramp);
727 /* Trampolines can only be created if modules are supported */
728 static inline void *alloc_tramp(unsigned long size)
732 static inline void tramp_free(void *tramp, int size) { }
735 /* Defined as markers to the end of the ftrace default trampolines */
736 extern void ftrace_regs_caller_end(void);
737 extern void ftrace_epilogue(void);
738 extern void ftrace_caller_op_ptr(void);
739 extern void ftrace_regs_caller_op_ptr(void);
741 /* movq function_trace_op(%rip), %rdx */
742 /* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */
743 #define OP_REF_SIZE 7
746 * The ftrace_ops is passed to the function callback. Since the
747 * trampoline only services a single ftrace_ops, we can pass in
750 * The ftrace_op_code_union is used to create a pointer to the
751 * ftrace_ops that will be passed to the callback function.
753 union ftrace_op_code_union {
754 char code[OP_REF_SIZE];
758 } __attribute__((packed));
762 create_trampoline(struct ftrace_ops *ops, unsigned int *tramp_size)
764 unsigned const char *jmp;
765 unsigned long start_offset;
766 unsigned long end_offset;
767 unsigned long op_offset;
768 unsigned long offset;
773 /* 48 8b 15 <offset> is movq <offset>(%rip), %rdx */
774 unsigned const char op_ref[] = { 0x48, 0x8b, 0x15 };
775 union ftrace_op_code_union op_ptr;
778 if (ops->flags & FTRACE_OPS_FL_SAVE_REGS) {
779 start_offset = (unsigned long)ftrace_regs_caller;
780 end_offset = (unsigned long)ftrace_regs_caller_end;
781 op_offset = (unsigned long)ftrace_regs_caller_op_ptr;
783 start_offset = (unsigned long)ftrace_caller;
784 end_offset = (unsigned long)ftrace_epilogue;
785 op_offset = (unsigned long)ftrace_caller_op_ptr;
788 size = end_offset - start_offset;
791 * Allocate enough size to store the ftrace_caller code,
792 * the jmp to ftrace_epilogue, as well as the address of
793 * the ftrace_ops this trampoline is used for.
795 trampoline = alloc_tramp(size + MCOUNT_INSN_SIZE + sizeof(void *));
799 *tramp_size = size + MCOUNT_INSN_SIZE + sizeof(void *);
801 /* Copy ftrace_caller onto the trampoline memory */
802 ret = probe_kernel_read(trampoline, (void *)start_offset, size);
803 if (WARN_ON(ret < 0)) {
804 tramp_free(trampoline, *tramp_size);
808 ip = (unsigned long)trampoline + size;
810 /* The trampoline ends with a jmp to ftrace_epilogue */
811 jmp = ftrace_jmp_replace(ip, (unsigned long)ftrace_epilogue);
812 memcpy(trampoline + size, jmp, MCOUNT_INSN_SIZE);
815 * The address of the ftrace_ops that is used for this trampoline
816 * is stored at the end of the trampoline. This will be used to
817 * load the third parameter for the callback. Basically, that
818 * location at the end of the trampoline takes the place of
819 * the global function_trace_op variable.
822 ptr = (unsigned long *)(trampoline + size + MCOUNT_INSN_SIZE);
823 *ptr = (unsigned long)ops;
825 op_offset -= start_offset;
826 memcpy(&op_ptr, trampoline + op_offset, OP_REF_SIZE);
828 /* Are we pointing to the reference? */
829 if (WARN_ON(memcmp(op_ptr.op, op_ref, 3) != 0)) {
830 tramp_free(trampoline, *tramp_size);
834 /* Load the contents of ptr into the callback parameter */
835 offset = (unsigned long)ptr;
836 offset -= (unsigned long)trampoline + op_offset + OP_REF_SIZE;
838 op_ptr.offset = offset;
840 /* put in the new offset to the ftrace_ops */
841 memcpy(trampoline + op_offset, &op_ptr, OP_REF_SIZE);
843 /* ALLOC_TRAMP flags lets us know we created it */
844 ops->flags |= FTRACE_OPS_FL_ALLOC_TRAMP;
846 return (unsigned long)trampoline;
849 static unsigned long calc_trampoline_call_offset(bool save_regs)
851 unsigned long start_offset;
852 unsigned long call_offset;
855 start_offset = (unsigned long)ftrace_regs_caller;
856 call_offset = (unsigned long)ftrace_regs_call;
858 start_offset = (unsigned long)ftrace_caller;
859 call_offset = (unsigned long)ftrace_call;
862 return call_offset - start_offset;
865 void arch_ftrace_update_trampoline(struct ftrace_ops *ops)
869 unsigned long offset;
874 if (ops->trampoline) {
876 * The ftrace_ops caller may set up its own trampoline.
877 * In such a case, this code must not modify it.
879 if (!(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
881 npages = PAGE_ALIGN(ops->trampoline_size) >> PAGE_SHIFT;
882 set_memory_rw(ops->trampoline, npages);
884 ops->trampoline = create_trampoline(ops, &size);
885 if (!ops->trampoline)
887 ops->trampoline_size = size;
888 npages = PAGE_ALIGN(size) >> PAGE_SHIFT;
891 offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
892 ip = ops->trampoline + offset;
894 func = ftrace_ops_get_func(ops);
896 ftrace_update_func_call = (unsigned long)func;
898 /* Do a safe modify in case the trampoline is executing */
899 new = ftrace_call_replace(ip, (unsigned long)func);
900 ret = update_ftrace_func(ip, new);
901 set_memory_ro(ops->trampoline, npages);
903 /* The update should never fail */
907 /* Return the address of the function the trampoline calls */
908 static void *addr_from_call(void *ptr)
910 union ftrace_code_union calc;
913 ret = probe_kernel_read(&calc, ptr, MCOUNT_INSN_SIZE);
914 if (WARN_ON_ONCE(ret < 0))
917 /* Make sure this is a call */
918 if (WARN_ON_ONCE(calc.e8 != 0xe8)) {
919 pr_warn("Expected e8, got %x\n", calc.e8);
923 return ptr + MCOUNT_INSN_SIZE + calc.offset;
926 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
927 unsigned long frame_pointer);
930 * If the ops->trampoline was not allocated, then it probably
931 * has a static trampoline func, or is the ftrace caller itself.
933 static void *static_tramp_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
935 unsigned long offset;
936 bool save_regs = rec->flags & FTRACE_FL_REGS_EN;
939 if (ops && ops->trampoline) {
940 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
942 * We only know about function graph tracer setting as static
945 if (ops->trampoline == FTRACE_GRAPH_ADDR)
946 return (void *)prepare_ftrace_return;
951 offset = calc_trampoline_call_offset(save_regs);
954 ptr = (void *)FTRACE_REGS_ADDR + offset;
956 ptr = (void *)FTRACE_ADDR + offset;
958 return addr_from_call(ptr);
961 void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec)
963 unsigned long offset;
965 /* If we didn't allocate this trampoline, consider it static */
966 if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
967 return static_tramp_func(ops, rec);
969 offset = calc_trampoline_call_offset(ops->flags & FTRACE_OPS_FL_SAVE_REGS);
970 return addr_from_call((void *)ops->trampoline + offset);
973 void arch_ftrace_trampoline_free(struct ftrace_ops *ops)
975 if (!ops || !(ops->flags & FTRACE_OPS_FL_ALLOC_TRAMP))
978 tramp_free((void *)ops->trampoline, ops->trampoline_size);
982 #endif /* CONFIG_X86_64 */
983 #endif /* CONFIG_DYNAMIC_FTRACE */
985 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
987 #ifdef CONFIG_DYNAMIC_FTRACE
988 extern void ftrace_graph_call(void);
990 static int ftrace_mod_jmp(unsigned long ip, void *func)
994 ftrace_update_func_call = 0UL;
995 new = ftrace_jmp_replace(ip, (unsigned long)func);
997 return update_ftrace_func(ip, new);
1000 int ftrace_enable_ftrace_graph_caller(void)
1002 unsigned long ip = (unsigned long)(&ftrace_graph_call);
1004 return ftrace_mod_jmp(ip, &ftrace_graph_caller);
1007 int ftrace_disable_ftrace_graph_caller(void)
1009 unsigned long ip = (unsigned long)(&ftrace_graph_call);
1011 return ftrace_mod_jmp(ip, &ftrace_stub);
1014 #endif /* !CONFIG_DYNAMIC_FTRACE */
1017 * Hook the return address and push it in the stack of return addrs
1018 * in current thread info.
1020 void prepare_ftrace_return(unsigned long self_addr, unsigned long *parent,
1021 unsigned long frame_pointer)
1025 struct ftrace_graph_ent trace;
1026 unsigned long return_hooker = (unsigned long)
1030 * When resuming from suspend-to-ram, this function can be indirectly
1031 * called from early CPU startup code while the CPU is in real mode,
1032 * which would fail miserably. Make sure the stack pointer is a
1035 * This check isn't as accurate as virt_addr_valid(), but it should be
1036 * good enough for this purpose, and it's fast.
1038 if (unlikely((long)__builtin_frame_address(0) >= 0))
1041 if (unlikely(ftrace_graph_is_dead()))
1044 if (unlikely(atomic_read(¤t->tracing_graph_pause)))
1048 * Protect against fault, even if it shouldn't
1049 * happen. This tool is too much intrusive to
1050 * ignore such a protection.
1053 "1: " _ASM_MOV " (%[parent]), %[old]\n"
1054 "2: " _ASM_MOV " %[return_hooker], (%[parent])\n"
1055 " movl $0, %[faulted]\n"
1058 ".section .fixup, \"ax\"\n"
1059 "4: movl $1, %[faulted]\n"
1063 _ASM_EXTABLE(1b, 4b)
1064 _ASM_EXTABLE(2b, 4b)
1066 : [old] "=&r" (old), [faulted] "=r" (faulted)
1067 : [parent] "r" (parent), [return_hooker] "r" (return_hooker)
1071 if (unlikely(faulted)) {
1072 ftrace_graph_stop();
1077 trace.func = self_addr;
1078 trace.depth = current->curr_ret_stack + 1;
1080 /* Only trace if the calling function expects to */
1081 if (!ftrace_graph_entry(&trace)) {
1086 if (ftrace_push_return_trace(old, self_addr, &trace.depth,
1087 frame_pointer, parent) == -EBUSY) {
1092 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */