ftrace.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * Dynamic ftrace generates probes to call sites, so we must deal with
  40. * both auipc and jalr at the same time.
  41. */
  42. #define MCOUNT_ADDR ((unsigned long)MCOUNT_NAME)
  43. #define JALR_SIGN_MASK (0x00000800)
  44. #define JALR_OFFSET_MASK (0x00000fff)
  45. #define AUIPC_OFFSET_MASK (0xfffff000)
  46. #define AUIPC_PAD (0x00001000)
  47. #define JALR_SHIFT 20
  48. #define JALR_BASIC (0x000080e7)
  49. #define AUIPC_BASIC (0x00000097)
  50. #define NOP4 (0x00000013)
  51. #define make_call(caller, callee, call) \
  52. do { \
  53. call[0] = to_auipc_insn((unsigned int)((unsigned long)callee - \
  54. (unsigned long)caller)); \
  55. call[1] = to_jalr_insn((unsigned int)((unsigned long)callee - \
  56. (unsigned long)caller)); \
  57. } while (0)
  58. #define to_jalr_insn(offset) \
  59. (((offset & JALR_OFFSET_MASK) << JALR_SHIFT) | JALR_BASIC)
  60. #define to_auipc_insn(offset) \
  61. ((offset & JALR_SIGN_MASK) ? \
  62. (((offset & AUIPC_OFFSET_MASK) + AUIPC_PAD) | AUIPC_BASIC) : \
  63. ((offset & AUIPC_OFFSET_MASK) | AUIPC_BASIC))
  64. /*
  65. * Let auipc+jalr be the basic *mcount unit*, so we make it 8 bytes here.
  66. */
  67. #define MCOUNT_INSN_SIZE 8
  68. #ifndef __ASSEMBLY__
  69. struct dyn_ftrace;
  70. int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec);
  71. #define ftrace_init_nop ftrace_init_nop
  72. #endif
  73. #endif
  74. #endif /* _ASM_RISCV_FTRACE_H */