ftrace.c 3.2 KB

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