ftrace.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <asm/ftrace.h>
  8. #ifdef CONFIG_DYNAMIC_FTRACE
  9. static const u32 ftrace_nop = 0x01000000;
  10. static u32 ftrace_call_replace(unsigned long ip, unsigned long addr)
  11. {
  12. static u32 call;
  13. s32 off;
  14. off = ((s32)addr - (s32)ip);
  15. call = 0x40000000 | ((u32)off >> 2);
  16. return call;
  17. }
  18. static int ftrace_modify_code(unsigned long ip, u32 old, u32 new)
  19. {
  20. u32 replaced;
  21. int faulted;
  22. __asm__ __volatile__(
  23. "1: cas [%[ip]], %[old], %[new]\n"
  24. " flush %[ip]\n"
  25. " mov 0, %[faulted]\n"
  26. "2:\n"
  27. " .section .fixup,#alloc,#execinstr\n"
  28. " .align 4\n"
  29. "3: sethi %%hi(2b), %[faulted]\n"
  30. " jmpl %[faulted] + %%lo(2b), %%g0\n"
  31. " mov 1, %[faulted]\n"
  32. " .previous\n"
  33. " .section __ex_table,\"a\"\n"
  34. " .align 4\n"
  35. " .word 1b, 3b\n"
  36. " .previous\n"
  37. : "=r" (replaced), [faulted] "=r" (faulted)
  38. : [new] "0" (new), [old] "r" (old), [ip] "r" (ip)
  39. : "memory");
  40. if (replaced != old && replaced != new)
  41. faulted = 2;
  42. return faulted;
  43. }
  44. int ftrace_make_nop(struct module *mod, struct dyn_ftrace *rec, unsigned long addr)
  45. {
  46. unsigned long ip = rec->ip;
  47. u32 old, new;
  48. old = ftrace_call_replace(ip, addr);
  49. new = ftrace_nop;
  50. return ftrace_modify_code(ip, old, new);
  51. }
  52. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  53. {
  54. unsigned long ip = rec->ip;
  55. u32 old, new;
  56. old = ftrace_nop;
  57. new = ftrace_call_replace(ip, addr);
  58. return ftrace_modify_code(ip, old, new);
  59. }
  60. int ftrace_update_ftrace_func(ftrace_func_t func)
  61. {
  62. unsigned long ip = (unsigned long)(&ftrace_call);
  63. u32 old, new;
  64. old = *(u32 *) &ftrace_call;
  65. new = ftrace_call_replace(ip, (unsigned long)func);
  66. return ftrace_modify_code(ip, old, new);
  67. }
  68. int __init ftrace_dyn_arch_init(void *data)
  69. {
  70. unsigned long *p = data;
  71. *p = 0;
  72. return 0;
  73. }
  74. #endif