common.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __X86_KERNEL_KPROBES_COMMON_H
  3. #define __X86_KERNEL_KPROBES_COMMON_H
  4. /* Kprobes and Optprobes common header */
  5. #include <asm/asm.h>
  6. #ifdef CONFIG_FRAME_POINTER
  7. # define SAVE_RBP_STRING " push %" _ASM_BP "\n" \
  8. " mov %" _ASM_SP ", %" _ASM_BP "\n"
  9. #else
  10. # define SAVE_RBP_STRING " push %" _ASM_BP "\n"
  11. #endif
  12. #ifdef CONFIG_X86_64
  13. #define SAVE_REGS_STRING \
  14. /* Skip cs, ip, orig_ax. */ \
  15. " subq $24, %rsp\n" \
  16. " pushq %rdi\n" \
  17. " pushq %rsi\n" \
  18. " pushq %rdx\n" \
  19. " pushq %rcx\n" \
  20. " pushq %rax\n" \
  21. " pushq %r8\n" \
  22. " pushq %r9\n" \
  23. " pushq %r10\n" \
  24. " pushq %r11\n" \
  25. " pushq %rbx\n" \
  26. SAVE_RBP_STRING \
  27. " pushq %r12\n" \
  28. " pushq %r13\n" \
  29. " pushq %r14\n" \
  30. " pushq %r15\n"
  31. #define RESTORE_REGS_STRING \
  32. " popq %r15\n" \
  33. " popq %r14\n" \
  34. " popq %r13\n" \
  35. " popq %r12\n" \
  36. " popq %rbp\n" \
  37. " popq %rbx\n" \
  38. " popq %r11\n" \
  39. " popq %r10\n" \
  40. " popq %r9\n" \
  41. " popq %r8\n" \
  42. " popq %rax\n" \
  43. " popq %rcx\n" \
  44. " popq %rdx\n" \
  45. " popq %rsi\n" \
  46. " popq %rdi\n" \
  47. /* Skip orig_ax, ip, cs */ \
  48. " addq $24, %rsp\n"
  49. #else
  50. #define SAVE_REGS_STRING \
  51. /* Skip cs, ip, orig_ax and gs. */ \
  52. " subl $16, %esp\n" \
  53. " pushl %fs\n" \
  54. " pushl %es\n" \
  55. " pushl %ds\n" \
  56. " pushl %eax\n" \
  57. SAVE_RBP_STRING \
  58. " pushl %edi\n" \
  59. " pushl %esi\n" \
  60. " pushl %edx\n" \
  61. " pushl %ecx\n" \
  62. " pushl %ebx\n"
  63. #define RESTORE_REGS_STRING \
  64. " popl %ebx\n" \
  65. " popl %ecx\n" \
  66. " popl %edx\n" \
  67. " popl %esi\n" \
  68. " popl %edi\n" \
  69. " popl %ebp\n" \
  70. " popl %eax\n" \
  71. /* Skip ds, es, fs, gs, orig_ax, and ip. Note: don't pop cs here*/\
  72. " addl $24, %esp\n"
  73. #endif
  74. /* Ensure if the instruction can be boostable */
  75. extern int can_boost(struct insn *insn, void *orig_addr);
  76. /* Recover instruction if given address is probed */
  77. extern unsigned long recover_probed_instruction(kprobe_opcode_t *buf,
  78. unsigned long addr);
  79. /*
  80. * Copy an instruction and adjust the displacement if the instruction
  81. * uses the %rip-relative addressing mode.
  82. */
  83. extern int __copy_instruction(u8 *dest, u8 *src, u8 *real, struct insn *insn);
  84. /* Generate a relative-jump/call instruction */
  85. extern void synthesize_reljump(void *dest, void *from, void *to);
  86. extern void synthesize_relcall(void *dest, void *from, void *to);
  87. #ifdef CONFIG_OPTPROBES
  88. extern int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter);
  89. extern unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsigned long addr);
  90. #else /* !CONFIG_OPTPROBES */
  91. static inline int setup_detour_execution(struct kprobe *p, struct pt_regs *regs, int reenter)
  92. {
  93. return 0;
  94. }
  95. static inline unsigned long __recover_optprobed_insn(kprobe_opcode_t *buf, unsigned long addr)
  96. {
  97. return addr;
  98. }
  99. #endif
  100. #endif