^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _ASM_X86_FRAME_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _ASM_X86_FRAME_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <asm/asm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * These are stack frame creation macros. They should be used by every
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * callable non-leaf asm function to make kernel stack traces more reliable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #ifdef CONFIG_FRAME_POINTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #ifdef __ASSEMBLY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) .macro FRAME_BEGIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) push %_ASM_BP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) _ASM_MOV %_ASM_SP, %_ASM_BP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) .macro FRAME_END
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) pop %_ASM_BP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #ifdef CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * is just setting the LSB, which makes it an invalid stack address and is also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * a signal to the unwinder that it's a pt_regs pointer in disguise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * NOTE: This macro must be used *after* PUSH_AND_CLEAR_REGS because it corrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * the original rbp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) .macro ENCODE_FRAME_POINTER ptregs_offset=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) leaq 1+\ptregs_offset(%rsp), %rbp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #else /* !CONFIG_X86_64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * This is a sneaky trick to help the unwinder find pt_regs on the stack. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * frame pointer is replaced with an encoded pointer to pt_regs. The encoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * is just clearing the MSB, which makes it an invalid stack address and is also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * a signal to the unwinder that it's a pt_regs pointer in disguise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * NOTE: This macro must be used *after* SAVE_ALL because it corrupts the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * original ebp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .macro ENCODE_FRAME_POINTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) mov %esp, %ebp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) andl $0x7fffffff, %ebp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #endif /* CONFIG_X86_64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #else /* !__ASSEMBLY__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define FRAME_BEGIN \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) "push %" _ASM_BP "\n" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) _ASM_MOV "%" _ASM_SP ", %" _ASM_BP "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define FRAME_END "pop %" _ASM_BP "\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #ifdef CONFIG_X86_64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define ENCODE_FRAME_POINTER \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) "lea 1(%rsp), %rbp\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static inline unsigned long encode_frame_pointer(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return (unsigned long)regs + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #else /* !CONFIG_X86_64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define ENCODE_FRAME_POINTER \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) "movl %esp, %ebp\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) "andl $0x7fffffff, %ebp\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static inline unsigned long encode_frame_pointer(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return (unsigned long)regs & 0x7fffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #endif /* CONFIG_X86_64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #endif /* __ASSEMBLY__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define FRAME_OFFSET __ASM_SEL(4, 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #else /* !CONFIG_FRAME_POINTER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #ifdef __ASSEMBLY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .macro ENCODE_FRAME_POINTER ptregs_offset=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #else /* !__ASSEMBLY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define ENCODE_FRAME_POINTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static inline unsigned long encode_frame_pointer(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define FRAME_BEGIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define FRAME_END
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define FRAME_OFFSET 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #endif /* CONFIG_FRAME_POINTER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #endif /* _ASM_X86_FRAME_H */