xref: /linux/arch/x86/include/asm/alternative.h (revision 570f7e331f5febb30f1384817463c7e42b65ca7d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_ALTERNATIVE_H
3 #define _ASM_X86_ALTERNATIVE_H
4 
5 #include <linux/types.h>
6 #include <linux/stringify.h>
7 #include <linux/objtool.h>
8 #include <asm/asm.h>
9 #include <asm/bug.h>
10 
11 #define ALT_FLAGS_SHIFT		16
12 
13 #define ALT_FLAG_NOT		(1 << 0)
14 #define ALT_NOT(feature)	((ALT_FLAG_NOT << ALT_FLAGS_SHIFT) | (feature))
15 #define ALT_FLAG_DIRECT_CALL	(1 << 1)
16 #define ALT_DIRECT_CALL(feature) ((ALT_FLAG_DIRECT_CALL << ALT_FLAGS_SHIFT) | (feature))
17 #define ALT_CALL_ALWAYS		ALT_DIRECT_CALL(X86_FEATURE_ALWAYS)
18 
19 #ifndef __ASSEMBLER__
20 
21 #include <linux/stddef.h>
22 
23 #ifdef CONFIG_SMP
24 #define LOCK_PREFIX "lock "
25 #else
26 #define LOCK_PREFIX ""
27 #endif
28 
29 /*
30  * The patching flags are part of the upper bits of the @ft_flags parameter when
31  * specifying them. The split is currently like this:
32  *
33  * [31... flags ...16][15... CPUID feature bit ...0]
34  *
35  * but since this is all hidden in the macros argument being split, those fields can be
36  * extended in the future to fit in a u64 or however the need arises.
37  */
38 struct alt_instr {
39 	s32 instr_offset;	/* original instruction */
40 	s32 repl_offset;	/* offset to replacement instruction */
41 
42 	union {
43 		struct {
44 			u32 cpuid: 16;	/* CPUID bit set for replacement */
45 			u32 flags: 16;	/* patching control flags */
46 		};
47 		u32 ft_flags;
48 	};
49 
50 	u8  instrlen;		/* length of original instruction */
51 	u8  replacementlen;	/* length of new instruction */
52 } __packed;
53 
54 extern struct alt_instr __alt_instructions[], __alt_instructions_end[];
55 
56 extern s32 __retpoline_sites[], __retpoline_sites_end[];
57 extern s32 __return_sites[],	__return_sites_end[];
58 extern s32 __cfi_sites[],	__cfi_sites_end[];
59 extern s32 __ibt_endbr_seal[],	__ibt_endbr_seal_end[];
60 
61 /*
62  * Debug flag that can be tested to see whether alternative
63  * instructions were patched in already:
64  */
65 extern int alternatives_patched;
66 
67 extern void alternative_instructions(void);
68 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
69 extern void apply_retpolines(s32 *start, s32 *end);
70 extern void apply_returns(s32 *start, s32 *end);
71 extern void apply_seal_endbr(s32 *start, s32 *end);
72 extern void apply_fineibt(s32 *start_retpoline, s32 *end_retpoine,
73 			  s32 *start_cfi, s32 *end_cfi);
74 
75 struct module;
76 
77 struct callthunk_sites {
78 	s32				*call_start, *call_end;
79 };
80 
81 #ifdef CONFIG_CALL_THUNKS
82 extern void callthunks_patch_builtin_calls(void);
83 extern void callthunks_patch_module_calls(struct callthunk_sites *sites,
84 					  struct module *mod);
85 extern void *callthunks_translate_call_dest(void *dest);
86 extern int x86_call_depth_emit_accounting(u8 **pprog, void *func, void *ip);
87 #else
88 static __always_inline void callthunks_patch_builtin_calls(void) {}
89 static __always_inline void
90 callthunks_patch_module_calls(struct callthunk_sites *sites,
91 			      struct module *mod) {}
92 static __always_inline void *callthunks_translate_call_dest(void *dest)
93 {
94 	return dest;
95 }
96 static __always_inline int x86_call_depth_emit_accounting(u8 **pprog,
97 							  void *func, void *ip)
98 {
99 	return 0;
100 }
101 #endif
102 
103 #ifdef CONFIG_MITIGATION_ITS
104 extern void its_init_mod(struct module *mod);
105 extern void its_fini_mod(struct module *mod);
106 extern void its_free_mod(struct module *mod);
107 extern u8 *its_static_thunk(int reg);
108 #else /* CONFIG_MITIGATION_ITS */
109 static inline void its_init_mod(struct module *mod) { }
110 static inline void its_fini_mod(struct module *mod) { }
111 static inline void its_free_mod(struct module *mod) { }
112 static inline u8 *its_static_thunk(int reg)
113 {
114 	WARN_ONCE(1, "ITS not compiled in");
115 
116 	return NULL;
117 }
118 #endif
119 
120 #if defined(CONFIG_MITIGATION_RETHUNK) && defined(CONFIG_OBJTOOL)
121 extern bool cpu_wants_rethunk(void);
122 extern bool cpu_wants_rethunk_at(void *addr);
123 #else
124 static __always_inline bool cpu_wants_rethunk(void)
125 {
126 	return false;
127 }
128 static __always_inline bool cpu_wants_rethunk_at(void *addr)
129 {
130 	return false;
131 }
132 #endif
133 
134 #define ALT_CALL_INSTR		"call BUG_func"
135 
136 #define alt_slen		"772b-771b"
137 #define alt_total_slen		"773b-771b"
138 #define alt_rlen		"775f-774f"
139 
140 #define OLDINSTR(oldinstr)						\
141 	"# ALT: oldinstr\n"						\
142 	"771:\n\t" oldinstr "\n772:\n"					\
143 	"# ALT: padding\n"						\
144 	".skip -(((" alt_rlen ")-(" alt_slen ")) > 0) * "		\
145 		"((" alt_rlen ")-(" alt_slen ")),0x90\n"		\
146 	"773:\n"
147 
148 #define ALTINSTR_ENTRY(ft_flags)					      \
149 	".pushsection .altinstructions, \"aM\", @progbits, "		      \
150 		      __stringify(ALT_INSTR_SIZE) "\n"			      \
151 	" .long 771b - .\n"				/* label           */ \
152 	" .long 774f - .\n"				/* new instruction */ \
153 	" .4byte " __stringify(ft_flags) "\n"		/* feature + flags */ \
154 	" .byte " alt_total_slen "\n"			/* source len      */ \
155 	" .byte " alt_rlen "\n"				/* replacement len */ \
156 	".popsection\n"
157 
158 #define ALTINSTR_REPLACEMENT(newinstr)		/* replacement */	\
159 	".pushsection .altinstr_replacement, \"ax\"\n"			\
160 	ANNOTATE_DATA_SPECIAL "\n"					\
161 	"# ALT: replacement\n"						\
162 	"774:\n\t" newinstr "\n775:\n"					\
163 	".popsection\n"
164 
165 /* alternative assembly primitive: */
166 #define ALTERNATIVE(oldinstr, newinstr, ft_flags)			\
167 	OLDINSTR(oldinstr)						\
168 	ALTINSTR_ENTRY(ft_flags)					\
169 	ALTINSTR_REPLACEMENT(newinstr)
170 
171 #define ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
172 	ALTERNATIVE(ALTERNATIVE(oldinstr, newinstr1, ft_flags1), newinstr2, ft_flags2)
173 
174 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
175 #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
176 	ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, newinstr_yes, ft_flags)
177 
178 #define ALTERNATIVE_3(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, \
179 			newinstr3, ft_flags3)				\
180 	ALTERNATIVE(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2), \
181 		      newinstr3, ft_flags3)
182 
183 /*
184  * Alternative instructions for different CPU types or capabilities.
185  *
186  * This allows to use optimized instructions even on generic binary
187  * kernels.
188  *
189  * length of oldinstr must be longer or equal the length of newinstr
190  * It can be padded with nops as needed.
191  *
192  * For non barrier like inlines please define new variants
193  * without volatile and memory clobber.
194  */
195 #define alternative(oldinstr, newinstr, ft_flags)			\
196 	asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) : : : "memory")
197 
198 #define alternative_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
199 	asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) ::: "memory")
200 
201 /*
202  * Alternative inline assembly with input.
203  *
204  * Peculiarities:
205  * No memory clobber here.
206  * Argument numbers start with 1.
207  * Leaving an unused argument 0 to keep API compatibility.
208  */
209 #define alternative_input(oldinstr, newinstr, ft_flags, input...)	\
210 	asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) \
211 		: : "i" (0), ## input)
212 
213 /* Like alternative_input, but with a single output argument */
214 #define alternative_io(oldinstr, newinstr, ft_flags, output, input...)	\
215 	asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags)	\
216 		: output : "i" (0), ## input)
217 
218 /*
219  * Like alternative_io, but for replacing a direct call with another one.
220  *
221  * Use the %c operand modifier which is the generic way to print a bare
222  * constant expression with all syntax-specific punctuation omitted. %P
223  * is the x86-specific variant which can handle constants too, for
224  * historical reasons, but it should be used primarily for PIC
225  * references: i.e., if used for a function, it would add the PLT
226  * suffix.
227  */
228 #define alternative_call(oldfunc, newfunc, ft_flags, output, input, clobbers...)	\
229 	asm_inline volatile(ALTERNATIVE("call %c[old]", "call %c[new]", ft_flags)	\
230 		: ALT_OUTPUT_SP(output)							\
231 		: [old] "i" (oldfunc), [new] "i" (newfunc)				\
232 		  COMMA(input)								\
233 		: clobbers)
234 
235 /*
236  * Like alternative_call, but there are two features and respective functions.
237  * If CPU has feature2, function2 is used.
238  * Otherwise, if CPU has feature1, function1 is used.
239  * Otherwise, old function is used.
240  */
241 #define alternative_call_2(oldfunc, newfunc1, ft_flags1, newfunc2, ft_flags2,		\
242 			   output, input, clobbers...)					\
243 	asm_inline volatile(ALTERNATIVE_2("call %c[old]", "call %c[new1]", ft_flags1,	\
244 		"call %c[new2]", ft_flags2)						\
245 		: ALT_OUTPUT_SP(output)							\
246 		: [old] "i" (oldfunc), [new1] "i" (newfunc1),				\
247 		  [new2] "i" (newfunc2)							\
248 		  COMMA(input)								\
249 		: clobbers)
250 
251 #define ALT_OUTPUT_SP(...) ASM_CALL_CONSTRAINT, ## __VA_ARGS__
252 
253 /* Macro for creating assembler functions avoiding any C magic. */
254 #define DEFINE_ASM_FUNC(func, instr, sec)		\
255 	asm (".pushsection " #sec ", \"ax\"\n"		\
256 	     ".global " #func "\n\t"			\
257 	     ".type " #func ", @function\n\t"		\
258 	     ASM_FUNC_ALIGN "\n"			\
259 	     #func ":\n\t"				\
260 	     ASM_ENDBR					\
261 	     instr "\n\t"				\
262 	     ASM_RET					\
263 	     ".size " #func ", . - " #func "\n\t"	\
264 	     ".popsection")
265 
266 void BUG_func(void);
267 void nop_func(void);
268 
269 #else /* __ASSEMBLER__ */
270 
271 .macro LOCK_PREFIX
272 #ifdef CONFIG_SMP
273 	lock
274 #endif
275 	.endm
276 
277 /*
278  * Issue one struct alt_instr descriptor entry (need to put it into
279  * the section .altinstructions, see below). This entry contains
280  * enough information for the alternatives patching code to patch an
281  * instruction. See apply_alternatives().
282  */
283 .macro altinstr_entry orig alt ft_flags orig_len alt_len
284 	.long \orig - .
285 	.long \alt - .
286 	.4byte \ft_flags
287 	.byte \orig_len
288 	.byte \alt_len
289 .endm
290 
291 .macro ALT_CALL_INSTR
292 	call BUG_func
293 .endm
294 
295 /*
296  * Define an alternative between two instructions. If @feature is
297  * present, early code in apply_alternatives() replaces @oldinstr with
298  * @newinstr. ".skip" directive takes care of proper instruction padding
299  * in case @newinstr is longer than @oldinstr.
300  */
301 #define __ALTERNATIVE(oldinst, newinst, flag)				\
302 740:									\
303 	oldinst	;							\
304 741:									\
305 	.skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90	;\
306 742:									\
307 	.pushsection .altinstructions, "aM", @progbits, ALT_INSTR_SIZE ;\
308 	altinstr_entry 740b,743f,flag,742b-740b,744f-743f ;		\
309 	.popsection ;							\
310 	.pushsection .altinstr_replacement,"ax"	;			\
311 743:									\
312 	ANNOTATE_DATA_SPECIAL ;						\
313 	newinst	;							\
314 744:									\
315 	.popsection ;
316 
317 .macro ALTERNATIVE oldinstr, newinstr, ft_flags
318 	__ALTERNATIVE(\oldinstr, \newinstr, \ft_flags)
319 .endm
320 
321 /*
322  * Same as ALTERNATIVE macro above but for two alternatives. If CPU
323  * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
324  * @feature2, it replaces @oldinstr with @feature2.
325  */
326 .macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
327 	__ALTERNATIVE(__ALTERNATIVE(\oldinstr, \newinstr1, \ft_flags1),
328 		      \newinstr2, \ft_flags2)
329 .endm
330 
331 .macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3
332 	__ALTERNATIVE(ALTERNATIVE_2(\oldinstr, \newinstr1, \ft_flags1, \newinstr2, \ft_flags2),
333 		      \newinstr3, \ft_flags3)
334 .endm
335 
336 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
337 #define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
338 	ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS,	\
339 	newinstr_yes, ft_flags
340 
341 #endif /* __ASSEMBLER__ */
342 
343 #endif /* _ASM_X86_ALTERNATIVE_H */
344