ftrace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2008 Matt Fleming <mjf@gentoo.org>
  3. *
  4. * Code for replacing ftrace calls with jumps.
  5. *
  6. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  7. *
  8. * Thanks goes to Ingo Molnar, for suggesting the idea.
  9. * Mathieu Desnoyers, for suggesting postponing the modifications.
  10. * Arjan van de Ven, for keeping me straight, and explaining to me
  11. * the dangers of modifying code on the run.
  12. */
  13. #include <linux/uaccess.h>
  14. #include <linux/ftrace.h>
  15. #include <linux/string.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <asm/ftrace.h>
  19. #include <asm/cacheflush.h>
  20. static unsigned char ftrace_nop[] = {
  21. 0x09, 0x00, /* nop */
  22. 0x09, 0x00, /* nop */
  23. };
  24. static unsigned char ftrace_replaced_code[MCOUNT_INSN_SIZE];
  25. unsigned char *ftrace_nop_replace(void)
  26. {
  27. return ftrace_nop;
  28. }
  29. static int is_sh_nop(unsigned char *ip)
  30. {
  31. return strncmp(ip, ftrace_nop, sizeof(ftrace_nop));
  32. }
  33. unsigned char *ftrace_call_replace(unsigned long ip, unsigned long addr)
  34. {
  35. /* Place the address in the memory table. */
  36. if (addr == CALLER_ADDR)
  37. __raw_writel(addr + MCOUNT_INSN_OFFSET, ftrace_replaced_code);
  38. else
  39. __raw_writel(addr, ftrace_replaced_code);
  40. /*
  41. * No locking needed, this must be called via kstop_machine
  42. * which in essence is like running on a uniprocessor machine.
  43. */
  44. return ftrace_replaced_code;
  45. }
  46. int ftrace_modify_code(unsigned long ip, unsigned char *old_code,
  47. unsigned char *new_code)
  48. {
  49. unsigned char replaced[MCOUNT_INSN_SIZE];
  50. /*
  51. * Note: Due to modules and __init, code can
  52. * disappear and change, we need to protect against faulting
  53. * as well as code changing. We do this by using the
  54. * probe_kernel_* functions.
  55. *
  56. * No real locking needed, this code is run through
  57. * kstop_machine, or before SMP starts.
  58. */
  59. /*
  60. * If we're trying to nop out a call to a function, we instead
  61. * place a call to the address after the memory table.
  62. */
  63. if (is_sh_nop(new_code) == 0)
  64. __raw_writel(ip + MCOUNT_INSN_SIZE, (unsigned long)new_code);
  65. /* read the text we want to modify */
  66. if (probe_kernel_read(replaced, (void *)ip, MCOUNT_INSN_SIZE))
  67. return -EFAULT;
  68. /* Make sure it is what we expect it to be */
  69. if (memcmp(replaced, old_code, MCOUNT_INSN_SIZE) != 0)
  70. return -EINVAL;
  71. /* replace the text with the new text */
  72. if (probe_kernel_write((void *)ip, new_code, MCOUNT_INSN_SIZE))
  73. return -EPERM;
  74. flush_icache_range(ip, ip + MCOUNT_INSN_SIZE);
  75. return 0;
  76. }
  77. int ftrace_update_ftrace_func(ftrace_func_t func)
  78. {
  79. unsigned long ip = (unsigned long)(&ftrace_call);
  80. unsigned char old[MCOUNT_INSN_SIZE], *new;
  81. memcpy(old, (unsigned char *)(ip + MCOUNT_INSN_OFFSET), MCOUNT_INSN_SIZE);
  82. new = ftrace_call_replace(ip, (unsigned long)func);
  83. return ftrace_modify_code(ip + MCOUNT_INSN_OFFSET, old, new);
  84. }
  85. int __init ftrace_dyn_arch_init(void *data)
  86. {
  87. /* The return code is retured via data */
  88. __raw_writel(0, (unsigned long)data);
  89. return 0;
  90. }