ftrace.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. static const u32 ftrace_nop = 0x01000000;
  8. notrace int ftrace_ip_converted(unsigned long ip)
  9. {
  10. u32 insn = *(u32 *) ip;
  11. return (insn == ftrace_nop);
  12. }
  13. notrace unsigned char *ftrace_nop_replace(void)
  14. {
  15. return (char *)&ftrace_nop;
  16. }
  17. notrace unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  18. {
  19. static u32 call;
  20. s32 off;
  21. off = ((s32)addr - (s32)ip);
  22. call = 0x40000000 | ((u32)off >> 2);
  23. return (unsigned char *) &call;
  24. }
  25. notrace int
  26. ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  27. unsigned char *new_code)
  28. {
  29. u32 old = *(u32 *)old_code;
  30. u32 new = *(u32 *)new_code;
  31. u32 replaced;
  32. int faulted;
  33. __asm__ __volatile__(
  34. "1: cas [%[ip]], %[old], %[new]\n"
  35. " flush %[ip]\n"
  36. " mov 0, %[faulted]\n"
  37. "2:\n"
  38. " .section .fixup,#alloc,#execinstr\n"
  39. " .align 4\n"
  40. "3: sethi %%hi(2b), %[faulted]\n"
  41. " jmpl %[faulted] + %%lo(2b), %%g0\n"
  42. " mov 1, %[faulted]\n"
  43. " .previous\n"
  44. " .section __ex_table,\"a\"\n"
  45. " .align 4\n"
  46. " .word 1b, 3b\n"
  47. " .previous\n"
  48. : "=r" (replaced), [faulted] "=r" (faulted)
  49. : [new] "0" (new), [old] "r" (old), [ip] "r" (ip)
  50. : "memory");
  51. if (replaced != old && replaced != new)
  52. faulted = 2;
  53. return faulted;
  54. }
  55. notrace int ftrace_update_ftrace_func(ftrace_func_t func)
  56. {
  57. unsigned long ip = (unsigned long)(&ftrace_call);
  58. unsigned char old[4], *new;
  59. memcpy(old, &ftrace_call, 4);
  60. new = ftrace_call_replace(ip, (unsigned long)func);
  61. return ftrace_modify_code(ip, old, new);
  62. }
  63. notrace int ftrace_mcount_set(unsigned long *data)
  64. {
  65. unsigned long ip = (long)(&mcount_call);
  66. unsigned long *addr = data;
  67. unsigned char old[4], *new;
  68. /*
  69. * Replace the mcount stub with a pointer to the
  70. * ip recorder function.
  71. */
  72. memcpy(old, &mcount_call, 4);
  73. new = ftrace_call_replace(ip, *addr);
  74. *addr = ftrace_modify_code(ip, old, new);
  75. return 0;
  76. }
  77. int __init ftrace_dyn_arch_init(void *data)
  78. {
  79. ftrace_mcount_set(data);
  80. return 0;
  81. }