ftrace.h 2.3 KB

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