Skip to content

Commit d2a793d

Browse files
Peter Zijlstrabp3tk0v
authored andcommitted
x86/alternatives: Add nested alternatives macros
Instead of making increasingly complicated ALTERNATIVE_n() implementations, use a nested alternative expression. The only difference between: ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2) and ALTERNATIVE(ALTERNATIVE(oldinst, newinst1, flag1), newinst2, flag2) is that the outer alternative can add additional padding when the inner alternative is the shorter one, which then results in alt_instr::instrlen being inconsistent. However, this is easily remedied since the alt_instr entries will be consecutive and it is trivial to compute the max(alt_instr::instrlen) at runtime while patching. Specifically, after this the ALTERNATIVE_2 macro, after CPP expansion (and manual layout), looks like this: .macro ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2 740: 740: \oldinstr ; 741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ; 742: .pushsection .altinstructions,"a" ; altinstr_entry 740b,743f,\ft_flags1,742b-740b,744f-743f ; .popsection ; .pushsection .altinstr_replacement,"ax" ; 743: \newinstr1 ; 744: .popsection ; ; 741: .skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ; 742: .pushsection .altinstructions,"a" ; altinstr_entry 740b,743f,\ft_flags2,742b-740b,744f-743f ; .popsection ; .pushsection .altinstr_replacement,"ax" ; 743: \newinstr2 ; 744: .popsection ; .endm The only label that is ambiguous is 740, however they all reference the same spot, so that doesn't matter. NOTE: obviously only @oldinstr may be an alternative; making @newinstr an alternative would mean patching .altinstr_replacement which very likely isn't what is intended, also the labels will be confused in that case. [ bp: Debug an issue where it would match the wrong two insns and and consider them nested due to the same signed offsets in the .alternative section and use instr_va() to compare the full virtual addresses instead. - Use new labels to denote that the new, nested alternatives are being used when staring at preprocessed output. - Use the %c constraint everywhere instead of %P and document the difference for future reference. ] Signed-off-by: Peter Zijlstra <peterz@infradead.org> Co-developed-by: Borislav Petkov (AMD) <bp@alien8.de> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://lkml.kernel.org/r/20230628104952.GA2439977@hirez.programming.kicks-ass.net
1 parent 9cbf264 commit d2a793d

4 files changed

Lines changed: 167 additions & 12 deletions

File tree

arch/x86/include/asm/alternative.h

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,13 @@ static inline int alternatives_text_reserved(void *start, void *end)
161161

162162
#define alt_end_marker "663"
163163
#define alt_slen "662b-661b"
164+
#define n_alt_slen "772b-771b"
165+
164166
#define alt_total_slen alt_end_marker"b-661b"
167+
#define n_alt_total_slen "773b-771b"
168+
165169
#define alt_rlen(num) e_replacement(num)"f-"b_replacement(num)"f"
170+
#define n_alt_rlen "775f-774f"
166171

167172
#define OLDINSTR(oldinstr, num) \
168173
"# ALT: oldnstr\n" \
@@ -172,6 +177,14 @@ static inline int alternatives_text_reserved(void *start, void *end)
172177
"((" alt_rlen(num) ")-(" alt_slen ")),0x90\n" \
173178
alt_end_marker ":\n"
174179

180+
#define N_OLDINSTR(oldinstr) \
181+
"# N_ALT: oldinstr\n" \
182+
"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" \
186+
"773:\n"
187+
175188
/*
176189
* gas compatible max based on the idea from:
177190
* http://graphics.stanford.edu/~seander/bithacks.html#IntegerMinOrMax
@@ -209,10 +222,25 @@ static inline int alternatives_text_reserved(void *start, void *end)
209222
" .byte " alt_total_slen "\n" /* source len */ \
210223
" .byte " alt_rlen(num) "\n" /* replacement len */
211224

225+
#define N_ALTINSTR_ENTRY(ft_flags) \
226+
".pushsection .altinstructions,\"a\"\n" \
227+
" .long 771b - .\n" /* label */ \
228+
" .long 774f - .\n" /* new instruction */ \
229+
" .4byte " __stringify(ft_flags) "\n" /* feature + flags */ \
230+
" .byte " n_alt_total_slen "\n" /* source len */ \
231+
" .byte " n_alt_rlen "\n" /* replacement len */ \
232+
".popsection\n"
233+
212234
#define ALTINSTR_REPLACEMENT(newinstr, num) /* replacement */ \
213235
"# ALT: replacement " #num "\n" \
214236
b_replacement(num)":\n\t" newinstr "\n" e_replacement(num) ":\n"
215237

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" \
242+
".popsection\n"
243+
216244
/* alternative assembly primitive: */
217245
#define ALTERNATIVE(oldinstr, newinstr, ft_flags) \
218246
OLDINSTR(oldinstr, 1) \
@@ -223,6 +251,12 @@ static inline int alternatives_text_reserved(void *start, void *end)
223251
ALTINSTR_REPLACEMENT(newinstr, 1) \
224252
".popsection\n"
225253

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)
259+
226260
#define ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
227261
OLDINSTR_2(oldinstr, 1, 2) \
228262
".pushsection .altinstructions,\"a\"\n" \
@@ -234,11 +268,21 @@ static inline int alternatives_text_reserved(void *start, void *end)
234268
ALTINSTR_REPLACEMENT(newinstr2, 2) \
235269
".popsection\n"
236270

271+
#define N_ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2) \
272+
N_ALTERNATIVE(N_ALTERNATIVE(oldinst, newinst1, flag1), \
273+
newinst2, flag2)
274+
237275
/* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
238276
#define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
239277
ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
240278
newinstr_yes, ft_flags)
241279

280+
/* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
281+
#define N_ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
282+
N_ALTERNATIVE_2(oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \
283+
newinstr_yes, ft_flags)
284+
285+
242286
#define ALTERNATIVE_3(oldinsn, newinsn1, ft_flags1, newinsn2, ft_flags2, \
243287
newinsn3, ft_flags3) \
244288
OLDINSTR_3(oldinsn, 1, 2, 3) \
@@ -253,6 +297,12 @@ static inline int alternatives_text_reserved(void *start, void *end)
253297
ALTINSTR_REPLACEMENT(newinsn3, 3) \
254298
".popsection\n"
255299

300+
301+
#define N_ALTERNATIVE_3(oldinst, newinst1, flag1, newinst2, flag2, \
302+
newinst3, flag3) \
303+
N_ALTERNATIVE(N_ALTERNATIVE_2(oldinst, newinst1, flag1, newinst2, flag2), \
304+
newinst3, flag3)
305+
256306
/*
257307
* Alternative instructions for different CPU types or capabilities.
258308
*
@@ -271,6 +321,12 @@ static inline int alternatives_text_reserved(void *start, void *end)
271321
#define alternative_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
272322
asm_inline volatile(ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) ::: "memory")
273323

324+
#define n_alternative(oldinstr, newinstr, ft_flags) \
325+
asm_inline volatile (N_ALTERNATIVE(oldinstr, newinstr, ft_flags) : : : "memory")
326+
327+
#define n_alternative_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) \
328+
asm_inline volatile(N_ALTERNATIVE_2(oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2) ::: "memory")
329+
274330
/*
275331
* Alternative inline assembly with input.
276332
*
@@ -288,11 +344,32 @@ static inline int alternatives_text_reserved(void *start, void *end)
288344
asm_inline volatile (ALTERNATIVE(oldinstr, newinstr, ft_flags) \
289345
: output : "i" (0), ## input)
290346

291-
/* Like alternative_io, but for replacing a direct call with another one. */
347+
#define n_alternative_io(oldinstr, newinstr, ft_flags, output, input...) \
348+
asm_inline volatile (N_ALTERNATIVE(oldinstr, newinstr, ft_flags) \
349+
: output : "i" (0), ## input)
350+
351+
/*
352+
* Like alternative_io, but for replacing a direct call with another one.
353+
*
354+
* Use the %c operand modifier which is the generic way to print a bare
355+
* constant expression with all syntax-specific punctuation omitted. %P
356+
* is the x86-specific variant which can handle constants too, for
357+
* historical reasons, but it should be used primarily for PIC
358+
* references: i.e., if used for a function, it would add the PLT
359+
* suffix.
360+
*/
292361
#define alternative_call(oldfunc, newfunc, ft_flags, output, input...) \
293362
asm_inline volatile (ALTERNATIVE("call %c[old]", "call %c[new]", ft_flags) \
294363
: output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
295364

365+
#define n_alternative_input(oldinstr, newinstr, ft_flags, input...) \
366+
asm_inline volatile (N_ALTERNATIVE(oldinstr, newinstr, ft_flags) \
367+
: : "i" (0), ## input)
368+
369+
#define n_alternative_call(oldfunc, newfunc, ft_flags, output, input...) \
370+
asm_inline volatile (N_ALTERNATIVE("call %c[old]", "call %c[new]", ft_flags) \
371+
: output : [old] "i" (oldfunc), [new] "i" (newfunc), ## input)
372+
296373
/*
297374
* Like alternative_call, but there are two features and respective functions.
298375
* If CPU has feature2, function2 is used.
@@ -307,6 +384,15 @@ static inline int alternatives_text_reserved(void *start, void *end)
307384
: [old] "i" (oldfunc), [new1] "i" (newfunc1), \
308385
[new2] "i" (newfunc2), ## input)
309386

387+
#define n_alternative_call_2(oldfunc, newfunc1, ft_flags1, newfunc2, ft_flags2, \
388+
output, input...) \
389+
asm_inline volatile (N_ALTERNATIVE_2("call %c[old]", "call %c[new1]", ft_flags1,\
390+
"call %c[new2]", ft_flags2) \
391+
: output, ASM_CALL_CONSTRAINT \
392+
: [old] "i" (oldfunc), [new1] "i" (newfunc1), \
393+
[new2] "i" (newfunc2), ## input)
394+
395+
310396
/*
311397
* use this macro(s) if you need more than one output parameter
312398
* in alternative_io
@@ -403,6 +489,27 @@ void nop_func(void);
403489
.popsection
404490
.endm
405491

492+
#define __N_ALTERNATIVE(oldinst, newinst, flag) \
493+
740: \
494+
oldinst ; \
495+
741: \
496+
.skip -(((744f-743f)-(741b-740b)) > 0) * ((744f-743f)-(741b-740b)),0x90 ;\
497+
742: \
498+
.pushsection .altinstructions,"a" ; \
499+
altinstr_entry 740b,743f,flag,742b-740b,744f-743f ; \
500+
.popsection ; \
501+
.pushsection .altinstr_replacement,"ax" ; \
502+
743: \
503+
newinst ; \
504+
744: \
505+
.popsection ;
506+
507+
508+
.macro N_ALTERNATIVE oldinstr, newinstr, ft_flags
509+
__N_ALTERNATIVE(\oldinstr, \newinstr, \ft_flags)
510+
.endm
511+
512+
406513
#define old_len 141b-140b
407514
#define new_len1 144f-143f
408515
#define new_len2 145f-144f
@@ -417,7 +524,6 @@ void nop_func(void);
417524
#define alt_max_2(a, b) ((a) ^ (((a) ^ (b)) & -(-((a) < (b)))))
418525
#define alt_max_3(a, b, c) (alt_max_2(alt_max_2(a, b), c))
419526

420-
421527
/*
422528
* Same as ALTERNATIVE macro above but for two alternatives. If CPU
423529
* has @feature1, it replaces @oldinstr with @newinstr1. If CPU has
@@ -445,6 +551,11 @@ void nop_func(void);
445551
.popsection
446552
.endm
447553

554+
.macro N_ALTERNATIVE_2 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2
555+
__N_ALTERNATIVE(__N_ALTERNATIVE(\oldinstr, \newinstr1, \ft_flags1),
556+
\newinstr2, \ft_flags2)
557+
.endm
558+
448559
.macro ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3
449560
140:
450561
\oldinstr
@@ -470,6 +581,11 @@ void nop_func(void);
470581
.popsection
471582
.endm
472583

584+
.macro N_ALTERNATIVE_3 oldinstr, newinstr1, ft_flags1, newinstr2, ft_flags2, newinstr3, ft_flags3
585+
__N_ALTERNATIVE(N_ALTERNATIVE_2(\oldinstr, \newinstr1, \ft_flags1, \newinstr2, \ft_flags2),
586+
\newinstr3, \ft_flags3)
587+
.endm
588+
473589
/* If @feature is set, patch in @newinstr_yes, otherwise @newinstr_no. */
474590
#define ALTERNATIVE_TERNARY(oldinstr, ft_flags, newinstr_yes, newinstr_no) \
475591
ALTERNATIVE_2 oldinstr, newinstr_no, X86_FEATURE_ALWAYS, \

arch/x86/kernel/alternative.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ static int alt_replace_call(u8 *instr, u8 *insn_buff, struct alt_instr *a)
432432
return 5;
433433
}
434434

435+
static inline u8 * instr_va(struct alt_instr *i)
436+
{
437+
return (u8 *)&i->instr_offset + i->instr_offset;
438+
}
439+
435440
/*
436441
* Replace instructions with better alternatives for this CPU type. This runs
437442
* before SMP is initialized to avoid SMP problems with self modifying code.
@@ -447,7 +452,7 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
447452
{
448453
u8 insn_buff[MAX_PATCH_LEN];
449454
u8 *instr, *replacement;
450-
struct alt_instr *a;
455+
struct alt_instr *a, *b;
451456

452457
DPRINTK(ALT, "alt table %px, -> %px", start, end);
453458

@@ -473,7 +478,18 @@ void __init_or_module noinline apply_alternatives(struct alt_instr *start,
473478
for (a = start; a < end; a++) {
474479
int insn_buff_sz = 0;
475480

476-
instr = (u8 *)&a->instr_offset + a->instr_offset;
481+
/*
482+
* In case of nested ALTERNATIVE()s the outer alternative might
483+
* add more padding. To ensure consistent patching find the max
484+
* padding for all alt_instr entries for this site (nested
485+
* alternatives result in consecutive entries).
486+
*/
487+
for (b = a+1; b < end && instr_va(b) == instr_va(a); b++) {
488+
u8 len = max(a->instrlen, b->instrlen);
489+
a->instrlen = b->instrlen = len;
490+
}
491+
492+
instr = instr_va(a);
477493
replacement = (u8 *)&a->repl_offset + a->repl_offset;
478494
BUG_ON(a->instrlen > sizeof(insn_buff));
479495
BUG_ON(a->cpuid >= (NCAPINTS + NBUGINTS) * 32);

tools/objtool/arch/x86/special.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,29 @@
99

1010
void arch_handle_alternative(unsigned short feature, struct special_alt *alt)
1111
{
12+
static struct special_alt *group, *prev;
13+
14+
/*
15+
* Recompute orig_len for nested ALTERNATIVE()s.
16+
*/
17+
if (group && group->orig_sec == alt->orig_sec &&
18+
group->orig_off == alt->orig_off) {
19+
20+
struct special_alt *iter = group;
21+
for (;;) {
22+
unsigned int len = max(iter->orig_len, alt->orig_len);
23+
iter->orig_len = alt->orig_len = len;
24+
25+
if (iter == prev)
26+
break;
27+
28+
iter = list_next_entry(iter, list);
29+
}
30+
31+
} else group = alt;
32+
33+
prev = alt;
34+
1235
switch (feature) {
1336
case X86_FEATURE_SMAP:
1437
/*

tools/objtool/special.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ static int get_alt_entry(struct elf *elf, const struct special_entry *entry,
8484
entry->new_len);
8585
}
8686

87+
orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
88+
if (!orig_reloc) {
89+
WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
90+
return -1;
91+
}
92+
93+
reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
94+
8795
if (entry->feature) {
8896
unsigned short feature;
8997

@@ -94,14 +102,6 @@ static int get_alt_entry(struct elf *elf, const struct special_entry *entry,
94102
arch_handle_alternative(feature, alt);
95103
}
96104

97-
orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
98-
if (!orig_reloc) {
99-
WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
100-
return -1;
101-
}
102-
103-
reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
104-
105105
if (!entry->group || alt->new_len) {
106106
new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
107107
if (!new_reloc) {

0 commit comments

Comments
 (0)