Commit | Line | Data |
---|---|---|
ee819aed JT |
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. | |
b735bd3e JP |
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. | |
ee819aed JT |
35 | */ |
36 | #define UNWIND_HINT_TYPE_CALL 0 | |
37 | #define UNWIND_HINT_TYPE_REGS 1 | |
38 | #define UNWIND_HINT_TYPE_REGS_PARTIAL 2 | |
b735bd3e | 39 | #define UNWIND_HINT_TYPE_FUNC 3 |
ee819aed | 40 | |
03f16cd0 | 41 | #ifdef CONFIG_OBJTOOL |
ee819aed | 42 | |
e2ef1158 PZ |
43 | #include <asm/asm.h> |
44 | ||
ee819aed JT |
45 | #ifndef __ASSEMBLY__ |
46 | ||
47 | #define UNWIND_HINT(sp_reg, sp_offset, type, end) \ | |
48 | "987: \n\t" \ | |
49 | ".pushsection .discard.unwind_hints\n\t" \ | |
50 | /* struct unwind_hint */ \ | |
51 | ".long 987b - .\n\t" \ | |
52 | ".short " __stringify(sp_offset) "\n\t" \ | |
53 | ".byte " __stringify(sp_reg) "\n\t" \ | |
54 | ".byte " __stringify(type) "\n\t" \ | |
55 | ".byte " __stringify(end) "\n\t" \ | |
56 | ".balign 4 \n\t" \ | |
57 | ".popsection\n\t" | |
58 | ||
59 | /* | |
60 | * This macro marks the given function's stack frame as "non-standard", which | |
61 | * tells objtool to ignore the function when doing stack metadata validation. | |
62 | * It should only be used in special cases where you're 100% sure it won't | |
63 | * affect the reliability of frame pointers and kernel stack traces. | |
64 | * | |
65 | * For more information, see tools/objtool/Documentation/stack-validation.txt. | |
66 | */ | |
67 | #define STACK_FRAME_NON_STANDARD(func) \ | |
33def849 | 68 | static void __used __section(".discard.func_stack_frame_non_standard") \ |
ee819aed JT |
69 | *__func_stack_frame_non_standard_##func = func |
70 | ||
e028c4f7 JP |
71 | /* |
72 | * STACK_FRAME_NON_STANDARD_FP() is a frame-pointer-specific function ignore | |
73 | * for the case where a function is intentionally missing frame pointer setup, | |
74 | * but otherwise needs objtool/ORC coverage when frame pointers are disabled. | |
75 | */ | |
76 | #ifdef CONFIG_FRAME_POINTER | |
77 | #define STACK_FRAME_NON_STANDARD_FP(func) STACK_FRAME_NON_STANDARD(func) | |
78 | #else | |
79 | #define STACK_FRAME_NON_STANDARD_FP(func) | |
80 | #endif | |
81 | ||
c8c301ab PZ |
82 | #define ANNOTATE_NOENDBR \ |
83 | "986: \n\t" \ | |
84 | ".pushsection .discard.noendbr\n\t" \ | |
85 | _ASM_PTR " 986b\n\t" \ | |
86 | ".popsection\n\t" | |
87 | ||
dca5da2a PZ |
88 | #define ASM_REACHABLE \ |
89 | "998:\n\t" \ | |
90 | ".pushsection .discard.reachable\n\t" \ | |
91 | ".long 998b - .\n\t" \ | |
92 | ".popsection\n\t" | |
93 | ||
ee819aed JT |
94 | #else /* __ASSEMBLY__ */ |
95 | ||
96 | /* | |
97 | * This macro indicates that the following intra-function call is valid. | |
98 | * Any non-annotated intra-function call will cause objtool to issue a warning. | |
99 | */ | |
100 | #define ANNOTATE_INTRA_FUNCTION_CALL \ | |
101 | 999: \ | |
102 | .pushsection .discard.intra_function_calls; \ | |
103 | .long 999b; \ | |
104 | .popsection; | |
105 | ||
106 | /* | |
107 | * In asm, there are two kinds of code: normal C-type callable functions and | |
108 | * the rest. The normal callable functions can be called by other code, and | |
109 | * don't do anything unusual with the stack. Such normal callable functions | |
110 | * are annotated with the ENTRY/ENDPROC macros. Most asm code falls in this | |
111 | * category. In this case, no special debugging annotations are needed because | |
112 | * objtool can automatically generate the ORC data for the ORC unwinder to read | |
113 | * at runtime. | |
114 | * | |
115 | * Anything which doesn't fall into the above category, such as syscall and | |
116 | * interrupt handlers, tends to not be called directly by other functions, and | |
117 | * often does unusual non-C-function-type things with the stack pointer. Such | |
118 | * code needs to be annotated such that objtool can understand it. The | |
119 | * following CFI hint macros are for this type of code. | |
120 | * | |
121 | * These macros provide hints to objtool about the state of the stack at each | |
122 | * instruction. Objtool starts from the hints and follows the code flow, | |
123 | * making automatic CFI adjustments when it sees pushes and pops, filling out | |
124 | * the debuginfo as necessary. It will also warn if it sees any | |
125 | * inconsistencies. | |
126 | */ | |
127 | .macro UNWIND_HINT sp_reg:req sp_offset=0 type:req end=0 | |
128 | .Lunwind_hint_ip_\@: | |
129 | .pushsection .discard.unwind_hints | |
130 | /* struct unwind_hint */ | |
131 | .long .Lunwind_hint_ip_\@ - . | |
132 | .short \sp_offset | |
133 | .byte \sp_reg | |
134 | .byte \type | |
135 | .byte \end | |
136 | .balign 4 | |
137 | .popsection | |
138 | .endm | |
139 | ||
081df943 JP |
140 | .macro STACK_FRAME_NON_STANDARD func:req |
141 | .pushsection .discard.func_stack_frame_non_standard, "aw" | |
e2ef1158 | 142 | _ASM_PTR \func |
081df943 JP |
143 | .popsection |
144 | .endm | |
145 | ||
7b6c7a87 JP |
146 | .macro STACK_FRAME_NON_STANDARD_FP func:req |
147 | #ifdef CONFIG_FRAME_POINTER | |
148 | STACK_FRAME_NON_STANDARD \func | |
149 | #endif | |
150 | .endm | |
151 | ||
c8c301ab PZ |
152 | .macro ANNOTATE_NOENDBR |
153 | .Lhere_\@: | |
154 | .pushsection .discard.noendbr | |
155 | .quad .Lhere_\@ | |
156 | .popsection | |
157 | .endm | |
158 | ||
dca5da2a PZ |
159 | .macro REACHABLE |
160 | .Lhere_\@: | |
161 | .pushsection .discard.reachable | |
162 | .long .Lhere_\@ - . | |
163 | .popsection | |
164 | .endm | |
165 | ||
ee819aed JT |
166 | #endif /* __ASSEMBLY__ */ |
167 | ||
03f16cd0 | 168 | #else /* !CONFIG_OBJTOOL */ |
ee819aed JT |
169 | |
170 | #ifndef __ASSEMBLY__ | |
171 | ||
172 | #define UNWIND_HINT(sp_reg, sp_offset, type, end) \ | |
173 | "\n\t" | |
174 | #define STACK_FRAME_NON_STANDARD(func) | |
e028c4f7 | 175 | #define STACK_FRAME_NON_STANDARD_FP(func) |
c8c301ab | 176 | #define ANNOTATE_NOENDBR |
dca5da2a | 177 | #define ASM_REACHABLE |
ee819aed JT |
178 | #else |
179 | #define ANNOTATE_INTRA_FUNCTION_CALL | |
180 | .macro UNWIND_HINT sp_reg:req sp_offset=0 type:req end=0 | |
181 | .endm | |
081df943 JP |
182 | .macro STACK_FRAME_NON_STANDARD func:req |
183 | .endm | |
c8c301ab PZ |
184 | .macro ANNOTATE_NOENDBR |
185 | .endm | |
dca5da2a PZ |
186 | .macro REACHABLE |
187 | .endm | |
ee819aed JT |
188 | #endif |
189 | ||
03f16cd0 | 190 | #endif /* CONFIG_OBJTOOL */ |
ee819aed JT |
191 | |
192 | #endif /* _LINUX_OBJTOOL_H */ |