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