ftrace.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (C) 2017 Andes Technology Corporation */
  3. #ifndef _ASM_RISCV_FTRACE_H
  4. #define _ASM_RISCV_FTRACE_H
  5. /*
  6. * The graph frame test is not possible if CONFIG_FRAME_POINTER is not enabled.
  7. * Check arch/riscv/kernel/mcount.S for detail.
  8. */
  9. #if defined(CONFIG_FUNCTION_GRAPH_TRACER) && defined(CONFIG_FRAME_POINTER)
  10. #define HAVE_FUNCTION_GRAPH_FP_TEST
  11. #endif
  12. #define HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
  13. /*
  14. * Clang prior to 13 had "mcount" instead of "_mcount":
  15. * https://reviews.llvm.org/D98881
  16. */
  17. #if defined(CONFIG_CC_IS_GCC) || CONFIG_CLANG_VERSION >= 130000
  18. #define MCOUNT_NAME _mcount
  19. #else
  20. #define MCOUNT_NAME mcount
  21. #endif
  22. #define ARCH_SUPPORTS_FTRACE_OPS 1
  23. #ifndef __ASSEMBLY__
  24. void MCOUNT_NAME(void);
  25. static inline unsigned long ftrace_call_adjust(unsigned long addr)
  26. {
  27. return addr;
  28. }
  29. struct dyn_arch_ftrace {
  30. };
  31. #endif
  32. #ifdef CONFIG_DYNAMIC_FTRACE
  33. /*
  34. * A general call in RISC-V is a pair of insts:
  35. * 1) auipc: setting high-20 pc-related bits to ra register
  36. * 2) jalr: setting low-12 offset to ra, jump to ra, and set ra to
  37. * return address (original pc + 4)
  38. *
  39. *<ftrace enable>:
  40. * 0: auipc t0/ra, 0x?
  41. * 4: jalr t0/ra, ?(t0/ra)
  42. *
  43. *<ftrace disable>:
  44. * 0: nop
  45. * 4: nop
  46. *
  47. * Dynamic ftrace generates probes to call sites, so we must deal with
  48. * both auipc and jalr at the same time.
  49. */
  50. #define MCOUNT_ADDR ((unsigned long)MCOUNT_NAME)
  51. #define JALR_SIGN_MASK (0x00000800)
  52. #define JALR_OFFSET_MASK (0x00000fff)
  53. #define AUIPC_OFFSET_MASK (0xfffff000)
  54. #define AUIPC_PAD (0x00001000)
  55. #define JALR_SHIFT 20
  56. #define JALR_RA (0x000080e7)
  57. #define AUIPC_RA (0x00000097)
  58. #define JALR_T0 (0x000282e7)
  59. #define AUIPC_T0 (0x00000297)
  60. #define NOP4 (0x00000013)
  61. #define to_jalr_t0(offset) \
  62. (((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_T0)
  63. #define to_auipc_t0(offset) \
  64. ((offset & JALR_SIGN_MASK) ? \
  65. (((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_T0) : \
  66. ((offset & AUIPC_OFFSET_MASK) | AUIPC_T0))
  67. #define make_call_t0(caller, callee, call) \
  68. do { \
  69. unsigned int offset = \
  70. (unsigned long) callee - (unsigned long) caller; \
  71. call[0] = to_auipc_t0(offset); \
  72. call[1] = to_jalr_t0(offset); \
  73. } while (0)
  74. #define to_jalr_ra(offset) \
  75. (((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_RA)
  76. #define to_auipc_ra(offset) \
  77. ((offset & JALR_SIGN_MASK) ? \
  78. (((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_RA) : \
  79. ((offset & AUIPC_OFFSET_MASK) | AUIPC_RA))
  80. #define make_call_ra(caller, callee, call) \
  81. do { \
  82. unsigned int offset = \
  83. (unsigned long) callee - (unsigned long) caller; \
  84. call[0] = to_auipc_ra(offset); \
  85. call[1] = to_jalr_ra(offset); \
  86. } while (0)
  87. /*
  88. * Let auipc+jalr be the basic *mcount unit*, so we make it 8 bytes here.
  89. */
  90. #define MCOUNT_INSN_SIZE 8
  91. #ifndef __ASSEMBLY__
  92. struct dyn_ftrace;
  93. int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec);
  94. #define ftrace_init_nop ftrace_init_nop
  95. #endif
  96. #endif /* CONFIG_DYNAMIC_FTRACE */
  97. #endif /* _ASM_RISCV_FTRACE_H */