GNU Linux-libre 5.10.217-gnu1
[releases.git] / include / linux / objtool.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_OBJTOOL_H
3 #define _LINUX_OBJTOOL_H
4
5 #ifndef __ASSEMBLY__
6
7 #include <linux/types.h>
8
9 /*
10  * This struct is used by asm and inline asm code to manually annotate the
11  * location of registers on the stack.
12  */
13 struct unwind_hint {
14         u32             ip;
15         s16             sp_offset;
16         u8              sp_reg;
17         u8              type;
18         u8              end;
19 };
20 #endif
21
22 /*
23  * UNWIND_HINT_TYPE_CALL: Indicates that sp_reg+sp_offset resolves to PREV_SP
24  * (the caller's SP right before it made the call).  Used for all callable
25  * functions, i.e. all C code and all callable asm functions.
26  *
27  * UNWIND_HINT_TYPE_REGS: Used in entry code to indicate that sp_reg+sp_offset
28  * points to a fully populated pt_regs from a syscall, interrupt, or exception.
29  *
30  * UNWIND_HINT_TYPE_REGS_PARTIAL: Used in entry code to indicate that
31  * sp_reg+sp_offset points to the iret return frame.
32  *
33  * UNWIND_HINT_FUNC: Generate the unwind metadata of a callable function.
34  * Useful for code which doesn't have an ELF function annotation.
35  *
36  * UNWIND_HINT_ENTRY: machine entry without stack, SYSCALL/SYSENTER etc.
37  */
38 #define UNWIND_HINT_TYPE_CALL           0
39 #define UNWIND_HINT_TYPE_REGS           1
40 #define UNWIND_HINT_TYPE_REGS_PARTIAL   2
41 #define UNWIND_HINT_TYPE_FUNC           3
42 #define UNWIND_HINT_TYPE_ENTRY          4
43 #define UNWIND_HINT_TYPE_SAVE           5
44 #define UNWIND_HINT_TYPE_RESTORE        6
45
46 #ifdef CONFIG_STACK_VALIDATION
47
48 #ifndef __ASSEMBLY__
49
50 #define UNWIND_HINT(sp_reg, sp_offset, type, end)               \
51         "987: \n\t"                                             \
52         ".pushsection .discard.unwind_hints\n\t"                \
53         /* struct unwind_hint */                                \
54         ".long 987b - .\n\t"                                    \
55         ".short " __stringify(sp_offset) "\n\t"                 \
56         ".byte " __stringify(sp_reg) "\n\t"                     \
57         ".byte " __stringify(type) "\n\t"                       \
58         ".byte " __stringify(end) "\n\t"                        \
59         ".balign 4 \n\t"                                        \
60         ".popsection\n\t"
61
62 /*
63  * This macro marks the given function's stack frame as "non-standard", which
64  * tells objtool to ignore the function when doing stack metadata validation.
65  * It should only be used in special cases where you're 100% sure it won't
66  * affect the reliability of frame pointers and kernel stack traces.
67  *
68  * For more information, see tools/objtool/Documentation/stack-validation.txt.
69  */
70 #define STACK_FRAME_NON_STANDARD(func) \
71         static void __used __section(".discard.func_stack_frame_non_standard") \
72                 *__func_stack_frame_non_standard_##func = func
73
74 /*
75  * STACK_FRAME_NON_STANDARD_FP() is a frame-pointer-specific function ignore
76  * for the case where a function is intentionally missing frame pointer setup,
77  * but otherwise needs objtool/ORC coverage when frame pointers are disabled.
78  */
79 #ifdef CONFIG_FRAME_POINTER
80 #define STACK_FRAME_NON_STANDARD_FP(func) STACK_FRAME_NON_STANDARD(func)
81 #else
82 #define STACK_FRAME_NON_STANDARD_FP(func)
83 #endif
84
85 #define ANNOTATE_NOENDBR                                        \
86         "986: \n\t"                                             \
87         ".pushsection .discard.noendbr\n\t"                     \
88         _ASM_PTR " 986b\n\t"                                    \
89         ".popsection\n\t"
90
91 #else /* __ASSEMBLY__ */
92
93 /*
94  * This macro indicates that the following intra-function call is valid.
95  * Any non-annotated intra-function call will cause objtool to issue a warning.
96  */
97 #define ANNOTATE_INTRA_FUNCTION_CALL                            \
98         999:                                                    \
99         .pushsection .discard.intra_function_calls;             \
100         .long 999b;                                             \
101         .popsection;
102
103 /*
104  * In asm, there are two kinds of code: normal C-type callable functions and
105  * the rest.  The normal callable functions can be called by other code, and
106  * don't do anything unusual with the stack.  Such normal callable functions
107  * are annotated with the ENTRY/ENDPROC macros.  Most asm code falls in this
108  * category.  In this case, no special debugging annotations are needed because
109  * objtool can automatically generate the ORC data for the ORC unwinder to read
110  * at runtime.
111  *
112  * Anything which doesn't fall into the above category, such as syscall and
113  * interrupt handlers, tends to not be called directly by other functions, and
114  * often does unusual non-C-function-type things with the stack pointer.  Such
115  * code needs to be annotated such that objtool can understand it.  The
116  * following CFI hint macros are for this type of code.
117  *
118  * These macros provide hints to objtool about the state of the stack at each
119  * instruction.  Objtool starts from the hints and follows the code flow,
120  * making automatic CFI adjustments when it sees pushes and pops, filling out
121  * the debuginfo as necessary.  It will also warn if it sees any
122  * inconsistencies.
123  */
124 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 end=0
125 .Lunwind_hint_ip_\@:
126         .pushsection .discard.unwind_hints
127                 /* struct unwind_hint */
128                 .long .Lunwind_hint_ip_\@ - .
129                 .short \sp_offset
130                 .byte \sp_reg
131                 .byte \type
132                 .byte \end
133                 .balign 4
134         .popsection
135 .endm
136
137 .macro ANNOTATE_NOENDBR
138 .Lhere_\@:
139         .pushsection .discard.noendbr
140         .quad   .Lhere_\@
141         .popsection
142 .endm
143
144 .macro STACK_FRAME_NON_STANDARD func:req
145         .pushsection .discard.func_stack_frame_non_standard, "aw"
146                 .long \func - .
147         .popsection
148 .endm
149
150 #endif /* __ASSEMBLY__ */
151
152 #else /* !CONFIG_STACK_VALIDATION */
153
154 #ifndef __ASSEMBLY__
155
156 #define UNWIND_HINT(sp_reg, sp_offset, type, end)       \
157         "\n\t"
158 #define STACK_FRAME_NON_STANDARD(func)
159 #define STACK_FRAME_NON_STANDARD_FP(func)
160 #define ANNOTATE_NOENDBR
161 #else
162 #define ANNOTATE_INTRA_FUNCTION_CALL
163 .macro UNWIND_HINT type:req sp_reg=0 sp_offset=0 end=0
164 .endm
165 .macro ANNOTATE_NOENDBR
166 .endm
167 .macro STACK_FRAME_NON_STANDARD func:req
168 .endm
169 #endif
170
171 #endif /* CONFIG_STACK_VALIDATION */
172
173 #endif /* _LINUX_OBJTOOL_H */