Mention branches and keyring.
[releases.git] / x86 / include / asm / alternative.h
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 <asm/asm.h>
8
9 #define ALTINSTR_FLAG_INV       (1 << 15)
10 #define ALT_NOT(feat)           ((feat) | ALTINSTR_FLAG_INV)
11
12 #ifndef __ASSEMBLY__
13
14 #include <linux/stddef.h>
15
16 /*
17  * Alternative inline assembly for SMP.
18  *
19  * The LOCK_PREFIX macro defined here replaces the LOCK and
20  * LOCK_PREFIX macros used everywhere in the source tree.
21  *
22  * SMP alternatives use the same data structures as the other
23  * alternatives and the X86_FEATURE_UP flag to indicate the case of a
24  * UP system running a SMP kernel.  The existing apply_alternatives()
25  * works fine for patching a SMP kernel for UP.
26  *
27  * The SMP alternative tables can be kept after boot and contain both
28  * UP and SMP versions of the instructions to allow switching back to
29  * SMP at runtime, when hotplugging in a new CPU, which is especially
30  * useful in virtualized environments.
31  *
32  * The very common lock prefix is handled as special case in a
33  * separate table which is a pure address list without replacement ptr
34  * and size information.  That keeps the table sizes small.
35  */
36
37 #ifdef CONFIG_SMP
38 #define LOCK_PREFIX_HERE \
39                 ".pushsection .smp_locks,\"a\"\n"       \
40                 ".balign 4\n"                           \
41                 ".long 671f - .\n" /* offset */         \
42                 ".popsection\n"                         \
43                 "671:"
44
45 #define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
46
47 #else /* ! CONFIG_SMP */
48 #define LOCK_PREFIX_HERE ""
49 #define LOCK_PREFIX ""
50 #endif
51
52 /*
53  * objtool annotation to ignore the alternatives and only consider the original
54  * instruction(s).
55  */
56 #define ANNOTATE_IGNORE_ALTERNATIVE                             \
57         "999:\n\t"                                              \
58         ".pushsection .discard.ignore_alts\n\t"                 \
59         ".long 999b - .\n\t"                                    \
60         ".popsection\n\t"
61
62 struct alt_instr {
63         s32 instr_offset;       /* original instruction */
64         s32 repl_offset;        /* offset to replacement instruction */
65         u16 cpuid;              /* cpuid bit set for replacement */
66         u8  instrlen;           /* length of original instruction */
67         u8  replacementlen;     /* length of new instruction */
68 } __packed;
69
70 /*
71  * Debug flag that can be tested to see whether alternative
72  * instructions were patched in already:
73  */
74 extern int alternatives_patched;
75
76 extern void alternative_instructions(void);
77 extern void apply_alternatives(struct alt_instr *start, struct alt_instr *end);
78 extern void apply_retpolines(s32 *start, s32 *end);
79 extern void apply_returns(s32 *start, s32 *end);
80 extern void apply_ibt_endbr(s32 *start, s32 *end);
81
82 struct module;
83
84 #ifdef CONFIG_SMP
85 extern void alternatives_smp_module_add(struct module *mod, char *name,
86                                         void *locks, void *locks_end,
87                                         void *text, void *text_end);
88 extern void alternatives_smp_module_del(struct module *mod);
89 extern void alternatives_enable_smp(void);
90 extern int alternatives_text_reserved(void *start, void *end);
91 extern bool skip_smp_alternatives;
92 #else
93 static inline void alternatives_smp_module_add(struct module *mod, char *name,
94                                                void *locks, void *locks_end,
95                                                void *text, void *text_end) {}
96 static inline void alternatives_smp_module_del(struct module *mod) {}
97 static inline void alternatives_enable_smp(void) {}
98 static inline int alternatives_text_reserved(void *start, void *end)
99 {
100         return 0;
101 }
102 #endif  /* CONFIG_SMP */
103
104 #define b_replacement(num)      "664"#num
105 #define e_replacement(num)      "665"#num
106
107 #define alt_end_marker          "663"
108 #define alt_slen                "662b-661b"
109 #define alt_total_slen          alt_end_marker"b-661b"
110 #define alt_rlen(num)           e_replacement(num)"f-"b_replacement(num)"f"
111
112 #define OLDINSTR(oldinstr, num)                                         \
113         "# ALT: oldnstr\n"                                              \
114         "661:\n\t" oldinstr "\n662:\n"                                  \
115         "# ALT: padding\n"                                              \
116         ".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * "          \
117                 "((" alt_rlen(num) ")-(" alt_slen ")),0x90\n"           \
118         alt_end_marker ":\n"
119
120 /*
121  * gas compatible max based on the idea from:
122  * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
123  *
124  * The additional "-" is needed because gas uses a "true" value of -1.
125  */
126 #define alt_max_short(a, b)     "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
127
128 /*
129  * Pad the second replacement alternative with additional NOPs if it is
130  * additionally longer than the first replacement alternative.
131  */
132 #define OLDINSTR_2(oldinstr, num1, num2) \
133         "# ALT: oldinstr2\n"                                                                    \
134         "661:\n\t" oldinstr "\n662:\n"                                                          \
135         "# ALT: padding2\n"                                                                     \
136         ".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * "  \
137                 "(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n"  \
138         alt_end_marker ":\n"
139
140 #define OLDINSTR_3(oldinsn, n1, n2, n3)                                                         \
141         "# ALT: oldinstr3\n"                                                                    \
142         "661:\n\t" oldinsn "\n662:\n"                                                           \
143         "# ALT: padding3\n"                                                                     \
144         ".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3))      \
145                 " - (" alt_slen ")) > 0) * "                                                    \
146                 "(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3))      \
147                 " - (" alt_slen ")), 0x90\n"                                                    \
148         alt_end_marker ":\n"
149
150 #define ALTINSTR_ENTRY(feature, num)                                          \
151         " .long 661b - .\n"                             /* label           */ \
152         " .long " b_replacement(num)"f - .\n"           /* new instruction */ \
153         " .word " __stringify(feature) "\n"             /* feature bit     */ \
154         " .byte " alt_total_slen "\n"                   /* source len      */ \
155         " .byte " alt_rlen(num) "\n"                    /* replacement len */
156
157 #define ALTINSTR_REPLACEMENT(newinstr, num)             /* replacement */       \
158         "# ALT: replacement " #num "\n"                                         \
159         b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
160
161 /* alternative assembly primitive: */
162 #define ALTERNATIVE(oldinstr, newinstr, feature)                        \
163         OLDINSTR(oldinstr, 1)                                           \
164         ".pushsection .altinstructions,\"a\"\n"                         \
165         ALTINSTR_ENTRY(feature, 1)                                      \
166         ".popsection\n"                                                 \
167         ".pushsection .altinstr_replacement, \"ax\"\n"                  \
168         ALTINSTR_REPLACEMENT(newinstr, 1)                               \
169         ".popsection\n"
170
171 #define ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2)\
172         OLDINSTR_2(oldinstr, 1, 2)                                      \
173         ".pushsection .altinstructions,\"a\"\n"                         \
174         ALTINSTR_ENTRY(feature1, 1)                                     \
175         ALTINSTR_ENTRY(feature2, 2)                                     \
176         ".popsection\n"                                                 \
177         ".pushsection .altinstr_replacement, \"ax\"\n"                  \
178         ALTINSTR_REPLACEMENT(newinstr1, 1)                              \
179         ALTINSTR_REPLACEMENT(newinstr2, 2)                              \
180         ".popsection\n"
181
182 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
183 #define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
184         ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS,        \
185                       newinstr_yes, feature)
186
187 #define ALTERNATIVE_3(oldinsn, newinsn1, feat1, newinsn2, feat2, newinsn3, feat3) \
188         OLDINSTR_3(oldinsn, 1, 2, 3)                                            \
189         ".pushsection .altinstructions,\"a\"\n"                                 \
190         ALTINSTR_ENTRY(feat1, 1)                                                \
191         ALTINSTR_ENTRY(feat2, 2)                                                \
192         ALTINSTR_ENTRY(feat3, 3)                                                \
193         ".popsection\n"                                                         \
194         ".pushsection .altinstr_replacement, \"ax\"\n"                          \
195         ALTINSTR_REPLACEMENT(newinsn1, 1)                                       \
196         ALTINSTR_REPLACEMENT(newinsn2, 2)                                       \
197         ALTINSTR_REPLACEMENT(newinsn3, 3)                                       \
198         ".popsection\n"
199
200 /*
201  * Alternative instructions for different CPU types or capabilities.
202  *
203  * This allows to use optimized instructions even on generic binary
204  * kernels.
205  *
206  * length of oldinstr must be longer or equal the length of newinstr
207  * It can be padded with nops as needed.
208  *
209  * For non barrier like inlines please define new variants
210  * without volatile and memory clobber.
211  */
212 #define alternative(oldinstr, newinstr, feature)                        \
213         asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature) : : : "memory")
214
215 #define alternative_2(oldinstr, newinstr1, feature1, newinstr2, feature2) \
216         asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1, newinstr2, feature2) ::: "memory")
217
218 #define alternative_ternary(oldinstr, feature, newinstr_yes, newinstr_no) \
219         asm_inline volatile(ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) ::: "memory")
220
221 /*
222  * Alternative inline assembly with input.
223  *
224  * Peculiarities:
225  * No memory clobber here.
226  * Argument numbers start with 1.
227  * Leaving an unused argument 0 to keep API compatibility.
228  */
229 #define alternative_input(oldinstr, newinstr, feature, input...)        \
230         asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)   \
231                 : : "i" (0), ## input)
232
233 /*
234  * This is similar to alternative_input. But it has two features and
235  * respective instructions.
236  *
237  * If CPU has feature2, newinstr2 is used.
238  * Otherwise, if CPU has feature1, newinstr1 is used.
239  * Otherwise, oldinstr is used.
240  */
241 #define alternative_input_2(oldinstr, newinstr1, feature1, newinstr2,        \
242                            feature2, input...)                               \
243         asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, feature1,     \
244                 newinstr2, feature2)                                         \
245                 : : "i" (0), ## input)
246
247 /* Like alternative_input, but with a single output argument */
248 #define alternative_io(oldinstr, newinstr, feature, output, input...)   \
249         asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, feature)   \
250                 : output : "i" (0), ## input)
251
252 /* Like alternative_io, but for replacing a direct call with another one. */
253 #define alternative_call(oldfunc, newfunc, feature, output, input...)   \
254         asm_inline volatile (ALTERNATIVE("call %P[old]", "call %P[new]", feature) \
255                 : output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
256
257 /*
258  * Like alternative_call, but there are two features and respective functions.
259  * If CPU has feature2, function2 is used.
260  * Otherwise, if CPU has feature1, function1 is used.
261  * Otherwise, old function is used.
262  */
263 #define alternative_call_2(oldfunc, newfunc1, feature1, newfunc2, feature2,   \
264                            output, input...)                                  \
265         asm_inline volatile (ALTERNATIVE_2("call %P[old]", "call %P[new1]", feature1,\
266                 "call %P[new2]", feature2)                                    \
267                 : output, ASM_CALL_CONSTRAINT                                 \
268                 : [old] "i" (oldfunc), [new1] "i" (newfunc1),                 \
269                   [new2] "i" (newfunc2), ## input)
270
271 /*
272  * use this macro(s) if you need more than one output parameter
273  * in alternative_io
274  */
275 #define ASM_OUTPUT2(a...) a
276
277 /*
278  * use this macro if you need clobbers but no inputs in
279  * alternative_{input,io,call}()
280  */
281 #define ASM_NO_INPUT_CLOBBER(clbr...) "i" (0) : clbr
282
283 #else /* __ASSEMBLY__ */
284
285 #ifdef CONFIG_SMP
286         .macro LOCK_PREFIX
287 672:    lock
288         .pushsection .smp_locks,"a"
289         .balign 4
290         .long 672b - .
291         .popsection
292         .endm
293 #else
294         .macro LOCK_PREFIX
295         .endm
296 #endif
297
298 /*
299  * objtool annotation to ignore the alternatives and only consider the original
300  * instruction(s).
301  */
302 .macro ANNOTATE_IGNORE_ALTERNATIVE
303         .Lannotate_\@:
304         .pushsection .discard.ignore_alts
305         .long .Lannotate_\@ - .
306         .popsection
307 .endm
308
309 /*
310  * Issue one struct alt_instr descriptor entry (need to put it into
311  * the section .altinstructions, see below). This entry contains
312  * enough information for the alternatives patching code to patch an
313  * instruction. See apply_alternatives().
314  */
315 .macro altinstruction_entry orig alt feature orig_len alt_len
316         .long \orig - .
317         .long \alt - .
318         .word \feature
319         .byte \orig_len
320         .byte \alt_len
321 .endm
322
323 /*
324  * Define an alternative between two instructions. If @feature is
325  * present, early code in apply_alternatives() replaces @oldinstr with
326  * @newinstr. ".skip" directive takes care of proper instruction padding
327  * in case @newinstr is longer than @oldinstr.
328  */
329 .macro ALTERNATIVE oldinstr, newinstr, feature
330 140:
331         \oldinstr
332 141:
333         .skip -(((144f-143f)-(141b-140b)) > 0) * ((144f-143f)-(141b-140b)),0x90
334 142:
335
336         .pushsection .altinstructions,"a"
337         altinstruction_entry 140b,143f,\feature,142b-140b,144f-143f
338         .popsection
339
340         .pushsection .altinstr_replacement,"ax"
341 143:
342         \newinstr
343 144:
344         .popsection
345 .endm
346
347 #define old_len                 141b-140b
348 #define new_len1                144f-143f
349 #define new_len2                145f-144f
350
351 /*
352  * gas compatible max based on the idea from:
353  * http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
354  *
355  * The additional "-" is needed because gas uses a "true" value of -1.
356  */
357 #define alt_max_short(a, b)     ((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
358
359
360 /*
361  * Same as ALTERNATIVE macro above but for two alternatives. If CPU
362  * has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
363  * @feature2, it replaces @oldinstr with @feature2.
364  */
365 .macro ALTERNATIVE_2 oldinstr, newinstr1, feature1, newinstr2, feature2
366 140:
367         \oldinstr
368 141:
369         .skip -((alt_max_short(new_len1, new_len2) - (old_len)) > 0) * \
370                 (alt_max_short(new_len1, new_len2) - (old_len)),0x90
371 142:
372
373         .pushsection .altinstructions,"a"
374         altinstruction_entry 140b,143f,\feature1,142b-140b,144f-143f
375         altinstruction_entry 140b,144f,\feature2,142b-140b,145f-144f
376         .popsection
377
378         .pushsection .altinstr_replacement,"ax"
379 143:
380         \newinstr1
381 144:
382         \newinstr2
383 145:
384         .popsection
385 .endm
386
387 /* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
388 #define ALTERNATIVE_TERNARY(oldinstr, feature, newinstr_yes, newinstr_no) \
389         ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS,        \
390         newinstr_yes, feature
391
392 #endif /* __ASSEMBLY__ */
393
394 #endif /* _ASM_X86_ALTERNATIVE_H */