ftrace.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2008 Matt Fleming <matt@console-pimps.org>
  3. * Copyright (C) 2008 Paul Mundt <lethal@linux-sh.org>
  4. *
  5. * Code for replacing ftrace calls with jumps.
  6. *
  7. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  8. *
  9. * Thanks goes to Ingo Molnar, for suggesting the idea.
  10. * Mathieu Desnoyers, for suggesting postponing the modifications.
  11. * Arjan van de Ven, for keeping me straight, and explaining to me
  12. * the dangers of modifying code on the run.
  13. */
  14. #include <linux/uaccess.h>
  15. #include <linux/ftrace.h>
  16. #include <linux/string.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <asm/ftrace.h>
  20. #include <asm/cacheflush.h>
  21. static unsigned char ftrace_replaced_code[MCOUNT_INSN_SIZE];
  22. static unsigned char ftrace_nop[4];
  23. /*
  24. * If we're trying to nop out a call to a function, we instead
  25. * place a call to the address after the memory table.
  26. *
  27. * 8c011060 <a>:
  28. * 8c011060: 02 d1 mov.l 8c01106c <a+0xc>,r1
  29. * 8c011062: 22 4f sts.l pr,@-r15
  30. * 8c011064: 02 c7 mova 8c011070 <a+0x10>,r0
  31. * 8c011066: 2b 41 jmp @r1
  32. * 8c011068: 2a 40 lds r0,pr
  33. * 8c01106a: 09 00 nop
  34. * 8c01106c: 68 24 .word 0x2468 <--- ip
  35. * 8c01106e: 1d 8c .word 0x8c1d
  36. * 8c011070: 26 4f lds.l @r15+,pr <--- ip + MCOUNT_INSN_SIZE
  37. *
  38. * We write 0x8c011070 to 0x8c01106c so that on entry to a() we branch
  39. * past the _mcount call and continue executing code like normal.
  40. */
  41. static unsigned char *ftrace_nop_replace(unsigned long ip)
  42. {
  43. __raw_writel(ip + MCOUNT_INSN_SIZE, ftrace_nop);
  44. return ftrace_nop;
  45. }
  46. static unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  47. {
  48. /* Place the address in the memory table. */
  49. __raw_writel(addr, ftrace_replaced_code);
  50. /*
  51. * No locking needed, this must be called via kstop_machine
  52. * which in essence is like running on a uniprocessor machine.
  53. */
  54. return ftrace_replaced_code;
  55. }
  56. static int ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  57. unsigned char *new_code)
  58. {
  59. unsigned char replaced[MCOUNT_INSN_SIZE];
  60. /*
  61. * Note: Due to modules and __init, code can
  62. * disappear and change, we need to protect against faulting
  63. * as well as code changing. We do this by using the
  64. * probe_kernel_* functions.
  65. *
  66. * No real locking needed, this code is run through
  67. * kstop_machine, or before SMP starts.
  68. */
  69. /* read the text we want to modify */
  70. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  71. return -EFAULT;
  72. /* Make sure it is what we expect it to be */
  73. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  74. return -EINVAL;
  75. /* replace the text with the new text */
  76. if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
  77. return -EPERM;
  78. flush_icache_range(ip, ip + MCOUNT_INSN_SIZE);
  79. return 0;
  80. }
  81. int ftrace_update_ftrace_func(ftrace_func_t func)
  82. {
  83. unsigned long ip = (unsigned long)(&ftrace_call) + MCOUNT_INSN_OFFSET;
  84. unsigned char old[MCOUNT_INSN_SIZE], *new;
  85. memcpy(old, (unsigned char *)ip, MCOUNT_INSN_SIZE);
  86. new = ftrace_call_replace(ip, (unsigned long)func);
  87. return ftrace_modify_code(ip, old, new);
  88. }
  89. int ftrace_make_nop(struct module *mod,
  90. struct dyn_ftrace *rec, unsigned long addr)
  91. {
  92. unsigned char *new, *old;
  93. unsigned long ip = rec->ip;
  94. old = ftrace_call_replace(ip, addr);
  95. new = ftrace_nop_replace(ip);
  96. return ftrace_modify_code(rec->ip, old, new);
  97. }
  98. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  99. {
  100. unsigned char *new, *old;
  101. unsigned long ip = rec->ip;
  102. old = ftrace_nop_replace(ip);
  103. new = ftrace_call_replace(ip, addr);
  104. return ftrace_modify_code(rec->ip, old, new);
  105. }
  106. int __init ftrace_dyn_arch_init(void *data)
  107. {
  108. /* The return code is retured via data */
  109. __raw_writel(0, (unsigned long)data);
  110. return 0;
  111. }