ftrace.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. static 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 *data)
  70. {
  71. unsigned long *p = data;
  72. *p = 0;
  73. return 0;
  74. }
  75. #endif
  76. #ifdef CONFIG_FTRACE_SYSCALLS
  77. extern unsigned int sys_call_table[];
  78. unsigned long __init arch_syscall_addr(int nr)
  79. {
  80. return (unsigned long)sys_call_table[nr];
  81. }
  82. #endif