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