^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 _LINUX_OBJTOOL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _LINUX_OBJTOOL_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #ifndef __ASSEMBLY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * This struct is used by asm and inline asm code to manually annotate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * location of registers on the stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct unwind_hint {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) u32 ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) s16 sp_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) u8 sp_reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) u8 end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * UNWIND_HINT_TYPE_CALL: Indicates that sp_reg+sp_offset resolves to PREV_SP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * (the caller's SP right before it made the call). Used for all callable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * functions, i.e. all C code and all callable asm functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * UNWIND_HINT_TYPE_REGS: Used in entry code to indicate that sp_reg+sp_offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * points to a fully populated pt_regs from a syscall, interrupt, or exception.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * UNWIND_HINT_TYPE_REGS_PARTIAL: Used in entry code to indicate that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * sp_reg+sp_offset points to the iret return frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define UNWIND_HINT_TYPE_CALL 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define UNWIND_HINT_TYPE_REGS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define UNWIND_HINT_TYPE_REGS_PARTIAL 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define UNWIND_HINT_TYPE_RET_OFFSET 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #ifdef CONFIG_STACK_VALIDATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #ifndef __ASSEMBLY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define UNWIND_HINT(sp_reg, sp_offset, type, end) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) "987: \n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ".pushsection .discard.unwind_hints\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* struct unwind_hint */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) ".long 987b - .\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) ".short " __stringify(sp_offset) "\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ".byte " __stringify(sp_reg) "\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) ".byte " __stringify(type) "\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ".byte " __stringify(end) "\n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ".balign 4 \n\t" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) ".popsection\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * This macro marks the given function's stack frame as "non-standard", which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * tells objtool to ignore the function when doing stack metadata validation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * It should only be used in special cases where you're 100% sure it won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * affect the reliability of frame pointers and kernel stack traces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * For more information, see tools/objtool/Documentation/stack-validation.txt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define STACK_FRAME_NON_STANDARD(func) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void __used __section(".discard.func_stack_frame_non_standard") \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) *__func_stack_frame_non_standard_##func = func
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #else /* __ASSEMBLY__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * This macro indicates that the following intra-function call is valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * Any non-annotated intra-function call will cause objtool to issue a warning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define ANNOTATE_INTRA_FUNCTION_CALL \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) 999: \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .pushsection .discard.intra_function_calls; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .long 999b; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .popsection;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * In asm, there are two kinds of code: normal C-type callable functions and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * the rest. The normal callable functions can be called by other code, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * don't do anything unusual with the stack. Such normal callable functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * are annotated with the ENTRY/ENDPROC macros. Most asm code falls in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * category. In this case, no special debugging annotations are needed because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * objtool can automatically generate the ORC data for the ORC unwinder to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * at runtime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * Anything which doesn't fall into the above category, such as syscall and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * interrupt handlers, tends to not be called directly by other functions, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * often does unusual non-C-function-type things with the stack pointer. Such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * code needs to be annotated such that objtool can understand it. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * following CFI hint macros are for this type of code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * These macros provide hints to objtool about the state of the stack at each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * instruction. Objtool starts from the hints and follows the code flow,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * making automatic CFI adjustments when it sees pushes and pops, filling out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * the debuginfo as necessary. It will also warn if it sees any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * inconsistencies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) .macro UNWIND_HINT sp_reg:req sp_offset=0 type:req end=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) .Lunwind_hint_ip_\@:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .pushsection .discard.unwind_hints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* struct unwind_hint */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .long .Lunwind_hint_ip_\@ - .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .short \sp_offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .byte \sp_reg
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .byte \type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) .byte \end
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .balign 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) .popsection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #endif /* __ASSEMBLY__ */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #else /* !CONFIG_STACK_VALIDATION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #ifndef __ASSEMBLY__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define UNWIND_HINT(sp_reg, sp_offset, type, end) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) "\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #define STACK_FRAME_NON_STANDARD(func)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define ANNOTATE_INTRA_FUNCTION_CALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .macro UNWIND_HINT sp_reg:req sp_offset=0 type:req end=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .endm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #endif /* CONFIG_STACK_VALIDATION */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #endif /* _LINUX_OBJTOOL_H */