ftrace.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Ftrace support for Microblaze.
  3. *
  4. * Copyright (C) 2009 Michal Simek <monstr@monstr.eu>
  5. * Copyright (C) 2009 PetaLogix
  6. *
  7. * Based on MIPS and PowerPC ftrace code
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <asm/cacheflush.h>
  14. #include <linux/ftrace.h>
  15. #ifdef CONFIG_DYNAMIC_FTRACE
  16. /* save value to addr - it is save to do it in asm */
  17. static int ftrace_modify_code(unsigned long addr, unsigned int value)
  18. {
  19. int faulted = 0;
  20. __asm__ __volatile__(" 1: swi %2, %1, 0; \
  21. addik %0, r0, 0; \
  22. 2: \
  23. .section .fixup, \"ax\"; \
  24. 3: brid 2b; \
  25. addik %0, r0, 1; \
  26. .previous; \
  27. .section __ex_table,\"a\"; \
  28. .word 1b,3b; \
  29. .previous;" \
  30. : "=r" (faulted)
  31. : "r" (addr), "r" (value)
  32. );
  33. if (unlikely(faulted))
  34. return -EFAULT;
  35. return 0;
  36. }
  37. #define MICROBLAZE_NOP 0x80000000
  38. #define MICROBLAZE_BRI 0xb800000C
  39. static unsigned int recorded; /* if save was or not */
  40. static unsigned int imm; /* saving whole imm instruction */
  41. /* There are two approaches howto solve ftrace_make nop function - look below */
  42. #undef USE_FTRACE_NOP
  43. #ifdef USE_FTRACE_NOP
  44. static unsigned int bralid; /* saving whole bralid instruction */
  45. #endif
  46. int ftrace_make_nop(struct module *mod,
  47. struct dyn_ftrace *rec, unsigned long addr)
  48. {
  49. /* we have this part of code which we are working with
  50. * b000c000 imm -16384
  51. * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
  52. * 80000000 or r0, r0, r0
  53. *
  54. * The first solution (!USE_FTRACE_NOP-could be called branch solution)
  55. * b000c000 bri 12 (0xC - jump to any other instruction)
  56. * b9fc8e30 bralid r15, -29136 // c0008e30 <_mcount>
  57. * 80000000 or r0, r0, r0
  58. * any other instruction
  59. *
  60. * The second solution (USE_FTRACE_NOP) - no jump just nops
  61. * 80000000 or r0, r0, r0
  62. * 80000000 or r0, r0, r0
  63. * 80000000 or r0, r0, r0
  64. */
  65. int ret = 0;
  66. if (recorded == 0) {
  67. recorded = 1;
  68. imm = *(unsigned int *)rec->ip;
  69. pr_debug("%s: imm:0x%x\n", __func__, imm);
  70. #ifdef USE_FTRACE_NOP
  71. bralid = *(unsigned int *)(rec->ip + 4);
  72. pr_debug("%s: bralid 0x%x\n", __func__, bralid);
  73. #endif /* USE_FTRACE_NOP */
  74. }
  75. #ifdef USE_FTRACE_NOP
  76. ret = ftrace_modify_code(rec->ip, MICROBLAZE_NOP);
  77. ret += ftrace_modify_code(rec->ip + 4, MICROBLAZE_NOP);
  78. #else /* USE_FTRACE_NOP */
  79. ret = ftrace_modify_code(rec->ip, MICROBLAZE_BRI);
  80. #endif /* USE_FTRACE_NOP */
  81. return ret;
  82. }
  83. static int ret_addr; /* initialized as 0 by default */
  84. /* I believe that first is called ftrace_make_nop before this function */
  85. int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
  86. {
  87. int ret;
  88. ret_addr = addr; /* saving where the barrier jump is */
  89. pr_debug("%s: addr:0x%x, rec->ip: 0x%x, imm:0x%x\n",
  90. __func__, (unsigned int)addr, (unsigned int)rec->ip, imm);
  91. ret = ftrace_modify_code(rec->ip, imm);
  92. #ifdef USE_FTRACE_NOP
  93. pr_debug("%s: bralid:0x%x\n", __func__, bralid);
  94. ret += ftrace_modify_code(rec->ip + 4, bralid);
  95. #endif /* USE_FTRACE_NOP */
  96. return ret;
  97. }
  98. int __init ftrace_dyn_arch_init(void *data)
  99. {
  100. /* The return code is retured via data */
  101. *(unsigned long *)data = 0;
  102. return 0;
  103. }
  104. int ftrace_update_ftrace_func(ftrace_func_t func)
  105. {
  106. unsigned long ip = (unsigned long)(&ftrace_call);
  107. unsigned int upper = (unsigned int)func;
  108. unsigned int lower = (unsigned int)func;
  109. int ret = 0;
  110. /* create proper saving to ftrace_call poll */
  111. upper = 0xb0000000 + (upper >> 16); /* imm func_upper */
  112. lower = 0x32800000 + (lower & 0xFFFF); /* addik r20, r0, func_lower */
  113. pr_debug("%s: func=0x%x, ip=0x%x, upper=0x%x, lower=0x%x\n",
  114. __func__, (unsigned int)func, (unsigned int)ip, upper, lower);
  115. /* save upper and lower code */
  116. ret = ftrace_modify_code(ip, upper);
  117. ret += ftrace_modify_code(ip + 4, lower);
  118. /* We just need to remove the rtsd r15, 8 by NOP */
  119. BUG_ON(!ret_addr);
  120. if (ret_addr)
  121. ret += ftrace_modify_code(ret_addr, MICROBLAZE_NOP);
  122. else
  123. ret = 1; /* fault */
  124. /* All changes are done - lets do caches consistent */
  125. flush_icache();
  126. return ret;
  127. }
  128. #endif /* CONFIG_DYNAMIC_FTRACE */