ftrace.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2008 Matt Fleming <mjf@gentoo.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_nop[] = {
  22. 0x09, 0x00, /* nop */
  23. 0x09, 0x00, /* nop */
  24. };
  25. static unsigned char ftrace_replaced_code[MCOUNT_INSN_SIZE];
  26. unsigned char *ftrace_nop_replace(void)
  27. {
  28. return ftrace_nop;
  29. }
  30. static int is_sh_nop(unsigned char *ip)
  31. {
  32. return strncmp(ip, ftrace_nop, sizeof(ftrace_nop));
  33. }
  34. unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  35. {
  36. /* Place the address in the memory table. */
  37. if (addr == CALLER_ADDR)
  38. __raw_writel(addr + MCOUNT_INSN_OFFSET, ftrace_replaced_code);
  39. else
  40. __raw_writel(addr, ftrace_replaced_code);
  41. /*
  42. * No locking needed, this must be called via kstop_machine
  43. * which in essence is like running on a uniprocessor machine.
  44. */
  45. return ftrace_replaced_code;
  46. }
  47. int ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  48. unsigned char *new_code)
  49. {
  50. unsigned char replaced[MCOUNT_INSN_SIZE];
  51. /*
  52. * Note: Due to modules and __init, code can
  53. * disappear and change, we need to protect against faulting
  54. * as well as code changing. We do this by using the
  55. * probe_kernel_* functions.
  56. *
  57. * No real locking needed, this code is run through
  58. * kstop_machine, or before SMP starts.
  59. */
  60. /*
  61. * If we're trying to nop out a call to a function, we instead
  62. * place a call to the address after the memory table.
  63. */
  64. if (is_sh_nop(new_code) == 0)
  65. __raw_writel(ip + MCOUNT_INSN_SIZE, (unsigned long)new_code);
  66. /* read the text we want to modify */
  67. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  68. return -EFAULT;
  69. /* Make sure it is what we expect it to be */
  70. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  71. return -EINVAL;
  72. /* replace the text with the new text */
  73. if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
  74. return -EPERM;
  75. flush_icache_range(ip, ip + MCOUNT_INSN_SIZE);
  76. return 0;
  77. }
  78. int ftrace_update_ftrace_func(ftrace_func_t func)
  79. {
  80. unsigned long ip = (unsigned long)(&ftrace_call);
  81. unsigned char old[MCOUNT_INSN_SIZE], *new;
  82. memcpy(old, (unsigned char *)(ip + MCOUNT_INSN_OFFSET), MCOUNT_INSN_SIZE);
  83. new = ftrace_call_replace(ip, (unsigned long)func);
  84. return ftrace_modify_code(ip + MCOUNT_INSN_OFFSET, old, new);
  85. }
  86. int ftrace_make_nop(struct module *mod,
  87. struct dyn_ftrace *rec, unsigned long addr)
  88. {
  89. unsigned char *new, *old;
  90. unsigned long ip = rec->ip;
  91. old = ftrace_call_replace(ip, addr);
  92. new = ftrace_nop_replace();
  93. return ftrace_modify_code(rec->ip, old, new);
  94. }
  95. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  96. {
  97. unsigned char *new, *old;
  98. unsigned long ip = rec->ip;
  99. old = ftrace_nop_replace();
  100. new = ftrace_call_replace(ip, addr);
  101. return ftrace_modify_code(rec->ip, old, new);
  102. }
  103. int __init ftrace_dyn_arch_init(void *data)
  104. {
  105. /* The return code is retured via data */
  106. __raw_writel(0, (unsigned long)data);
  107. return 0;
  108. }