Skip to content

Commit f776e41

Browse files
committed
x86/alternative: Replace the old macros
Now that the new macros have been gradually put in place, replace the old ones. Leave the new label numbers starting at 7xx as a hint that the new nested alternatives are being used now. Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lore.kernel.org/r/20240607111701.8366-15-bp@kernel.org
1 parent 1a6ade8 commit f776e41

1 file changed

Lines changed: 32 additions & 121 deletions

File tree

arch/x86/include/asm/alternative.h

Lines changed: 32 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -156,131 +156,51 @@ static inline int alternatives_text_reserved(void *start, void *end)
156156

157157
#define ALT_CALL_INSTR "call BUG_func"
158158

159-
#define b_replacement(num) "664"#num
160-
#define e_replacement(num) "665"#num
159+
#define alt_slen "772b-771b"
160+
#define alt_total_slen "773b-771b"
161+
#define alt_rlen "775f-774f"
161162

162-
#define alt_end_marker "663"
163-
#define alt_slen "662b-661b"
164-
#define n_alt_slen "772b-771b"
165-
166-
#define alt_total_slen alt_end_marker"b-661b"
167-
#define n_alt_total_slen "773b-771b"
168-
169-
#define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
170-
#define n_alt_rlen "775f-774f"
171-
172-
#define OLDINSTR(oldinstr, num) \
173-
"# ALT: oldnstr\n" \
174-
"661:\n\t" oldinstr "\n662:\n" \
175-
"# ALT: padding\n" \
176-
".skip -(((" alt_rlen(num) ")-(" alt_slen ")) > 0) * " \
177-
"((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" \
178-
alt_end_marker ":\n"
179-
180-
#define N_OLDINSTR(oldinstr) \
181-
"# N_ALT: oldinstr\n" \
163+
#define OLDINSTR(oldinstr) \
164+
"# ALT: oldinstr\n" \
182165
"771:\n\t" oldinstr "\n772:\n" \
183-
"# N_ALT: padding\n" \
184-
".skip -(((" n_alt_rlen ")-(" n_alt_slen ")) > 0) * " \
185-
"((" n_alt_rlen ")-(" n_alt_slen ")),0x90\n" \
166+
"# ALT: padding\n" \
167+
".skip -(((" alt_rlen ")-(" alt_slen ")) > 0) * " \
168+
"((" alt_rlen ")-(" alt_slen ")),0x90\n" \
186169
"773:\n"
187170

188-
/*
189-
* gas compatible max based on the idea from:
190-
* http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
191-
*
192-
* The additional "-" is needed because gas uses a "true" value of -1.
193-
*/
194-
#define alt_max_short(a, b) "((" a ") ^ (((" a ") ^ (" b ")) & -(-((" a ") < (" b ")))))"
195-
196-
/*
197-
* Pad the second replacement alternative with additional NOPs if it is
198-
* additionally longer than the first replacement alternative.
199-
*/
200-
#define OLDINSTR_2(oldinstr, num1, num2) \
201-
"# ALT: oldinstr2\n" \
202-
"661:\n\t" oldinstr "\n662:\n" \
203-
"# ALT: padding2\n" \
204-
".skip -((" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")) > 0) * " \
205-
"(" alt_max_short(alt_rlen(num1), alt_rlen(num2)) " - (" alt_slen ")), 0x90\n" \
206-
alt_end_marker ":\n"
207-
208-
#define OLDINSTR_3(oldinsn, n1, n2, n3) \
209-
"# ALT: oldinstr3\n" \
210-
"661:\n\t" oldinsn "\n662:\n" \
211-
"# ALT: padding3\n" \
212-
".skip -((" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
213-
" - (" alt_slen ")) > 0) * " \
214-
"(" alt_max_short(alt_max_short(alt_rlen(n1), alt_rlen(n2)), alt_rlen(n3)) \
215-
" - (" alt_slen ")), 0x90\n" \
216-
alt_end_marker ":\n"
217-
218-
#define ALTINSTR_ENTRY(ft_flags, num) \
219-
" .long 661b - .\n" /* label */ \
220-
" .long " b_replacement(num)"f - .\n" /* new instruction */ \
221-
" .4byte " __stringify(ft_flags) "\n" /* feature + flags */ \
222-
" .byte " alt_total_slen "\n" /* source len */ \
223-
" .byte " alt_rlen(num) "\n" /* replacement len */
224-
225-
#define N_ALTINSTR_ENTRY(ft_flags) \
171+
#define ALTINSTR_ENTRY(ft_flags) \
226172
".pushsection .altinstructions,\"a\"\n" \
227173
" .long 771b - .\n" /* label */ \
228174
" .long 774f - .\n" /* new instruction */ \
229175
" .4byte " __stringify(ft_flags) "\n" /* feature + flags */ \
230-
" .byte " n_alt_total_slen "\n" /* source len */ \
231-
" .byte " n_alt_rlen "\n" /* replacement len */ \
176+
" .byte " alt_total_slen "\n" /* source len */ \
177+
" .byte " alt_rlen "\n" /* replacement len */ \
232178
".popsection\n"
233179

234-
#define ALTINSTR_REPLACEMENT(newinstr, num) /* replacement */ \
235-
"# ALT: replacement " #num "\n" \
236-
b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
237-
238-
#define N_ALTINSTR_REPLACEMENT(newinstr) /* replacement */ \
239-
".pushsection .altinstr_replacement, \"ax\"\n" \
240-
"# N_ALT: replacement\n" \
241-
"774:\n\t" newinstr "\n775:\n" \
180+
#define ALTINSTR_REPLACEMENT(newinstr) /* replacement */ \
181+
".pushsection .altinstr_replacement, \"ax\"\n" \
182+
"# ALT: replacement\n" \
183+
"774:\n\t" newinstr "\n775:\n" \
242184
".popsection\n"
243185

244186
/* alternative assembly primitive: */
245187
#define ALTERNATIVE(oldinstr, newinstr, ft_flags) \
246-
OLDINSTR(oldinstr, 1) \
247-
".pushsection .altinstructions,\"a\"\n" \
248-
ALTINSTR_ENTRY(ft_flags, 1) \
249-
".popsection\n" \
250-
".pushsection .altinstr_replacement, \"ax\"\n" \
251-
ALTINSTR_REPLACEMENT(newinstr, 1) \
252-
".popsection\n"
253-
254-
/* Nested alternatives macro variant */
255-
#define N_ALTERNATIVE(oldinstr, newinstr, ft_flags) \
256-
N_OLDINSTR(oldinstr) \
257-
N_ALTINSTR_ENTRY(ft_flags) \
258-
N_ALTINSTR_REPLACEMENT(newinstr)
188+
OLDINSTR(oldinstr) \
189+
ALTINSTR_ENTRY(ft_flags) \
190+
ALTINSTR_REPLACEMENT(newinstr)
259191

260192
#define ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
261-
OLDINSTR_2(oldinstr, 1, 2) \
262-
".pushsection .altinstructions,\"a\"\n" \
263-
ALTINSTR_ENTRY(ft_flags1, 1) \
264-
ALTINSTR_ENTRY(ft_flags2, 2) \
265-
".popsection\n" \
266-
".pushsection .altinstr_replacement, \"ax\"\n" \
267-
ALTINSTR_REPLACEMENT(newinstr1, 1) \
268-
ALTINSTR_REPLACEMENT(newinstr2, 2) \
269-
".popsection\n"
270-
271-
#define N_ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2) \
272-
N_ALTERNATIVE(N_ALTERNATIVE(oldinst, newinst1, flag1), \
273-
newinst2, flag2)
193+
ALTERNATIVE(ALTERNATIVE(oldinstr, newinstr1, ft_flags1), newinstr2, ft_flags2)
274194

275195
/* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
276196
#define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
277-
N_ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
278-
newinstr_yes, ft_flags)
197+
ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, newinstr_yes, ft_flags)
279198

280199
#define ALTERNATIVE_3(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, \
281200
newinstr3, ft_flags3) \
282-
N_ALTERNATIVE(N_ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2), \
201+
ALTERNATIVE(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2), \
283202
newinstr3, ft_flags3)
203+
284204
/*
285205
* Alternative instructions for different CPU types or capabilities.
286206
*
@@ -294,10 +214,10 @@ static inline int alternatives_text_reserved(void *start, void *end)
294214
* without volatile and memory clobber.
295215
*/
296216
#define alternative(oldinstr, newinstr, ft_flags) \
297-
asm_inline volatile(N_ALTERNATIVE(oldinstr, newinstr, ft_flags) : : : "memory")
217+
asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) : : : "memory")
298218

299219
#define alternative_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
300-
asm_inline volatile(N_ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) ::: "memory")
220+
asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) ::: "memory")
301221

302222
/*
303223
* Alternative inline assembly with input.
@@ -308,12 +228,12 @@ static inline int alternatives_text_reserved(void *start, void *end)
308228
* Leaving an unused argument 0 to keep API compatibility.
309229
*/
310230
#define alternative_input(oldinstr, newinstr, ft_flags, input...) \
311-
asm_inline volatile(N_ALTERNATIVE(oldinstr, newinstr, ft_flags) \
231+
asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) \
312232
: : "i" (0), ## input)
313233

314234
/* Like alternative_input, but with a single output argument */
315235
#define alternative_io(oldinstr, newinstr, ft_flags, output, input...) \
316-
asm_inline volatile(N_ALTERNATIVE(oldinstr, newinstr, ft_flags) \
236+
asm_inline volatile(ALTERNATIVE(oldinstr, newinstr, ft_flags) \
317237
: output : "i" (0), ## input)
318238

319239
/*
@@ -327,7 +247,7 @@ static inline int alternatives_text_reserved(void *start, void *end)
327247
* suffix.
328248
*/
329249
#define alternative_call(oldfunc, newfunc, ft_flags, output, input...) \
330-
asm_inline volatile(N_ALTERNATIVE("call %c[old]", "call %c[new]", ft_flags) \
250+
asm_inline volatile(ALTERNATIVE("call %c[old]", "call %c[new]", ft_flags) \
331251
: output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
332252

333253
/*
@@ -338,7 +258,7 @@ static inline int alternatives_text_reserved(void *start, void *end)
338258
*/
339259
#define alternative_call_2(oldfunc, newfunc1, ft_flags1, newfunc2, ft_flags2, \
340260
output, input...) \
341-
asm_inline volatile(N_ALTERNATIVE_2("call %c[old]", "call %c[new1]", ft_flags1, \
261+
asm_inline volatile(ALTERNATIVE_2("call %c[old]", "call %c[new1]", ft_flags1, \
342262
"call %c[new2]", ft_flags2) \
343263
: output, ASM_CALL_CONSTRAINT \
344264
: [old] "i" (oldfunc), [new1] "i" (newfunc1), \
@@ -422,7 +342,7 @@ void nop_func(void);
422342
* @newinstr. ".skip" directive takes care of proper instruction padding
423343
* in case @newinstr is longer than @oldinstr.
424344
*/
425-
#define __N_ALTERNATIVE(oldinst, newinst, flag) \
345+
#define __ALTERNATIVE(oldinst, newinst, flag) \
426346
740: \
427347
oldinst ; \
428348
741: \
@@ -438,35 +358,26 @@ void nop_func(void);
438358
.popsection ;
439359

440360
.macro ALTERNATIVE oldinstr, newinstr, ft_flags
441-
__N_ALTERNATIVE(\oldinstr, \newinstr, \ft_flags)
361+
__ALTERNATIVE(\oldinstr, \newinstr, \ft_flags)
442362
.endm
443363

444364
#define old_len 141b-140b
445365
#define new_len1 144f-143f
446366
#define new_len2 145f-144f
447367
#define new_len3 146f-145f
448368

449-
/*
450-
* gas compatible max based on the idea from:
451-
* http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
452-
*
453-
* The additional "-" is needed because gas uses a "true" value of -1.
454-
*/
455-
#define alt_max_2(a, b) ((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
456-
#define alt_max_3(a, b, c) (alt_max_2(alt_max_2(a, b), c))
457-
458369
/*
459370
* Same as ALTERNATIVE macro above but for two alternatives. If CPU
460371
* has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
461372
* @feature2, it replaces @oldinstr with @feature2.
462373
*/
463374
.macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
464-
__N_ALTERNATIVE(__N_ALTERNATIVE(\oldinstr, \newinstr1, \ft_flags1),
375+
__ALTERNATIVE(__ALTERNATIVE(\oldinstr, \newinstr1, \ft_flags1),
465376
\newinstr2, \ft_flags2)
466377
.endm
467378

468379
.macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3
469-
__N_ALTERNATIVE(N_ALTERNATIVE_2(\oldinstr, \newinstr1, \ft_flags1, \newinstr2, \ft_flags2),
380+
__ALTERNATIVE(ALTERNATIVE_2(\oldinstr, \newinstr1, \ft_flags1, \newinstr2, \ft_flags2),
470381
\newinstr3, \ft_flags3)
471382
.endm
472383

0 commit comments

Comments
 (0)