ftrace.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/spinlock.h>
  3. #include <linux/hardirq.h>
  4. #include <linux/ftrace.h>
  5. #include <linux/percpu.h>
  6. #include <linux/init.h>
  7. #include <linux/list.h>
  8. #include <trace/syscall.h>
  9. #include <asm/ftrace.h>
  10. #ifdef CONFIG_DYNAMIC_FTRACE
  11. static const u32 ftrace_nop = 0x01000000;
  12. static u32 ftrace_call_replace(unsigned long ip, unsigned long addr)
  13. {
  14. u32 call;
  15. s32 off;
  16. off = ((s32)addr - (s32)ip);
  17. call = 0x40000000 | ((u32)off >> 2);
  18. return call;
  19. }
  20. static int ftrace_modify_code(unsigned long ip, u32 old, u32 new)
  21. {
  22. u32 replaced;
  23. int faulted;
  24. __asm__ __volatile__(
  25. "1: cas [%[ip]], %[old], %[new]\n"
  26. " flush %[ip]\n"
  27. " mov 0, %[faulted]\n"
  28. "2:\n"
  29. " .section .fixup,#alloc,#execinstr\n"
  30. " .align 4\n"
  31. "3: sethi %%hi(2b), %[faulted]\n"
  32. " jmpl %[faulted] + %%lo(2b), %%g0\n"
  33. " mov 1, %[faulted]\n"
  34. " .previous\n"
  35. " .section __ex_table,\"a\"\n"
  36. " .align 4\n"
  37. " .word 1b, 3b\n"
  38. " .previous\n"
  39. : "=r" (replaced), [faulted] "=r" (faulted)
  40. : [new] "0" (new), [old] "r" (old), [ip] "r" (ip)
  41. : "memory");
  42. if (replaced != old && replaced != new)
  43. faulted = 2;
  44. return faulted;
  45. }
  46. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
  47. {
  48. unsigned long ip = rec->ip;
  49. u32 old, new;
  50. old = ftrace_call_replace(ip, addr);
  51. new = ftrace_nop;
  52. return ftrace_modify_code(ip, old, new);
  53. }
  54. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  55. {
  56. unsigned long ip = rec->ip;
  57. u32 old, new;
  58. old = ftrace_nop;
  59. new = ftrace_call_replace(ip, addr);
  60. return ftrace_modify_code(ip, old, new);
  61. }
  62. int ftrace_update_ftrace_func(ftrace_func_t func)
  63. {
  64. unsigned long ip = (unsigned long)(&ftrace_call);
  65. u32 old, new;
  66. old = *(u32 *) &ftrace_call;
  67. new = ftrace_call_replace(ip, (unsigned long)func);
  68. return ftrace_modify_code(ip, old, new);
  69. }
  70. int __init ftrace_dyn_arch_init(void)
  71. {
  72. return 0;
  73. }
  74. #endif
  75. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  76. #ifdef CONFIG_DYNAMIC_FTRACE
  77. extern void ftrace_graph_call(void);
  78. int ftrace_enable_ftrace_graph_caller(void)
  79. {
  80. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  81. u32 old, new;
  82. old = *(u32 *) &ftrace_graph_call;
  83. new = ftrace_call_replace(ip, (unsigned long) &ftrace_graph_caller);
  84. return ftrace_modify_code(ip, old, new);
  85. }
  86. int ftrace_disable_ftrace_graph_caller(void)
  87. {
  88. unsigned long ip = (unsigned long)(&ftrace_graph_call);
  89. u32 old, new;
  90. old = *(u32 *) &ftrace_graph_call;
  91. new = ftrace_call_replace(ip, (unsigned long) &ftrace_stub);
  92. return ftrace_modify_code(ip, old, new);
  93. }
  94. #endif /* !CONFIG_DYNAMIC_FTRACE */
  95. /*
  96. * Hook the return address and push it in the stack of return addrs
  97. * in current thread info.
  98. */
  99. unsigned long prepare_ftrace_return(unsigned long parent,
  100. unsigned long self_addr,
  101. unsigned long frame_pointer)
  102. {
  103. unsigned long return_hooker = (unsigned long) &return_to_handler;
  104. if (unlikely(atomic_read(&current->tracing_graph_pause)))
  105. return parent + 8UL;
  106. if (function_graph_enter(parent, self_addr, frame_pointer, NULL))
  107. return parent + 8UL;
  108. return return_hooker;
  109. }
  110. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */