Skip to content

Commit 59e97e4

Browse files
amlutoH. Peter Anvin
authored andcommitted
x86: Make alternative instruction pointers relative
This save a few bytes on x86-64 and means that future patches can apply alternatives to unrelocated code. Signed-off-by: Andy Lutomirski <luto@mit.edu> Link: http://lkml.kernel.org/r/ff64a6b9a1a3860ca4a7b8b6dc7b4754f9491cd7.1310563276.git.luto@mit.edu Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
1 parent c971294 commit 59e97e4

6 files changed

Lines changed: 31 additions & 30 deletions

File tree

‎arch/x86/include/asm/alternative-asm.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
.macro altinstruction_entry orig alt feature orig_len alt_len
1919
.align 8
20-
.quad \orig
21-
.quad \alt
20+
.long \orig - .
21+
.long \alt - .
2222
.word \feature
2323
.byte \orig_len
2424
.byte \alt_len

‎arch/x86/include/asm/alternative.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
#endif
4444

4545
struct alt_instr {
46-
u8 *instr; /* original instruction */
47-
u8 *replacement;
46+
s32 instr_offset; /* original instruction */
47+
s32 repl_offset; /* offset to replacement instruction */
4848
u16 cpuid; /* cpuid bit set for replacement */
4949
u8 instrlen; /* length of original instruction */
5050
u8 replacementlen; /* length of new instruction, <= instrlen */
@@ -84,8 +84,8 @@ static inline int alternatives_text_reserved(void *start, void *end)
8484
"661:\n\t" oldinstr "\n662:\n" \
8585
".section .altinstructions,\"a\"\n" \
8686
_ASM_ALIGN "\n" \
87-
_ASM_PTR "661b\n" /* label */ \
88-
_ASM_PTR "663f\n" /* new instruction */ \
87+
" .long 661b - .\n" /* label */ \
88+
" .long 663f - .\n" /* new instruction */ \
8989
" .word " __stringify(feature) "\n" /* feature bit */ \
9090
" .byte 662b-661b\n" /* sourcelen */ \
9191
" .byte 664f-663f\n" /* replacementlen */ \

‎arch/x86/include/asm/cpufeature.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ static __always_inline __pure bool __static_cpu_has(u16 bit)
331331
"2:\n"
332332
".section .altinstructions,\"a\"\n"
333333
_ASM_ALIGN "\n"
334-
_ASM_PTR "1b\n"
335-
_ASM_PTR "0\n" /* no replacement */
334+
" .long 1b - .\n"
335+
" .long 0\n" /* no replacement */
336336
" .word %P0\n" /* feature bit */
337337
" .byte 2b - 1b\n" /* source len */
338338
" .byte 0\n" /* replacement len */
@@ -349,8 +349,8 @@ static __always_inline __pure bool __static_cpu_has(u16 bit)
349349
"2:\n"
350350
".section .altinstructions,\"a\"\n"
351351
_ASM_ALIGN "\n"
352-
_ASM_PTR "1b\n"
353-
_ASM_PTR "3f\n"
352+
" .long 1b - .\n"
353+
" .long 3f - .\n"
354354
" .word %P1\n" /* feature bit */
355355
" .byte 2b - 1b\n" /* source len */
356356
" .byte 4f - 3f\n" /* replacement len */

‎arch/x86/kernel/alternative.c‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ void __init_or_module apply_alternatives(struct alt_instr *start,
263263
struct alt_instr *end)
264264
{
265265
struct alt_instr *a;
266+
u8 *instr, *replacement;
266267
u8 insnbuf[MAX_PATCH_LEN];
267268

268269
DPRINTK("%s: alt table %p -> %p\n", __func__, start, end);
@@ -276,25 +277,29 @@ void __init_or_module apply_alternatives(struct alt_instr *start,
276277
* order.
277278
*/
278279
for (a = start; a < end; a++) {
279-
u8 *instr = a->instr;
280+
instr = (u8 *)&a->instr_offset + a->instr_offset;
281+
replacement = (u8 *)&a->repl_offset + a->repl_offset;
280282
BUG_ON(a->replacementlen > a->instrlen);
281283
BUG_ON(a->instrlen > sizeof(insnbuf));
282284
BUG_ON(a->cpuid >= NCAPINTS*32);
283285
if (!boot_cpu_has(a->cpuid))
284286
continue;
287+
288+
memcpy(insnbuf, replacement, a->replacementlen);
289+
290+
/* 0xe8 is a relative jump; fix the offset. */
291+
if (*insnbuf == 0xe8 && a->replacementlen == 5)
292+
*(s32 *)(insnbuf + 1) += replacement - instr;
293+
294+
add_nops(insnbuf + a->replacementlen,
295+
a->instrlen - a->replacementlen);
296+
285297
#ifdef CONFIG_X86_64
286298
/* vsyscall code is not mapped yet. resolve it manually. */
287299
if (instr >= (u8 *)VSYSCALL_START && instr < (u8*)VSYSCALL_END) {
288300
instr = __va(instr - (u8*)VSYSCALL_START + (u8*)__pa_symbol(&__vsyscall_0));
289-
DPRINTK("%s: vsyscall fixup: %p => %p\n",
290-
__func__, a->instr, instr);
291301
}
292302
#endif
293-
memcpy(insnbuf, a->replacement, a->replacementlen);
294-
if (*insnbuf == 0xe8 && a->replacementlen == 5)
295-
*(s32 *)(insnbuf + 1) += a->replacement - a->instr;
296-
add_nops(insnbuf + a->replacementlen,
297-
a->instrlen - a->replacementlen);
298303
text_poke_early(instr, insnbuf, a->instrlen);
299304
}
300305
}

‎arch/x86/lib/copy_page_64.S‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <linux/linkage.h>
44
#include <asm/dwarf2.h>
5+
#include <asm/alternative-asm.h>
56

67
ALIGN
78
copy_page_c:
@@ -110,10 +111,6 @@ ENDPROC(copy_page)
110111
2:
111112
.previous
112113
.section .altinstructions,"a"
113-
.align 8
114-
.quad copy_page
115-
.quad 1b
116-
.word X86_FEATURE_REP_GOOD
117-
.byte .Lcopy_page_end - copy_page
118-
.byte 2b - 1b
114+
altinstruction_entry copy_page, 1b, X86_FEATURE_REP_GOOD, \
115+
.Lcopy_page_end-copy_page, 2b-1b
119116
.previous

‎arch/x86/lib/memmove_64.S‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <linux/linkage.h>
1010
#include <asm/dwarf2.h>
1111
#include <asm/cpufeature.h>
12+
#include <asm/alternative-asm.h>
1213

1314
#undef memmove
1415

@@ -214,11 +215,9 @@ ENTRY(memmove)
214215
.previous
215216

216217
.section .altinstructions,"a"
217-
.align 8
218-
.quad .Lmemmove_begin_forward
219-
.quad .Lmemmove_begin_forward_efs
220-
.word X86_FEATURE_ERMS
221-
.byte .Lmemmove_end_forward-.Lmemmove_begin_forward
222-
.byte .Lmemmove_end_forward_efs-.Lmemmove_begin_forward_efs
218+
altinstruction_entry .Lmemmove_begin_forward, \
219+
.Lmemmove_begin_forward_efs,X86_FEATURE_ERMS, \
220+
.Lmemmove_end_forward-.Lmemmove_begin_forward, \
221+
.Lmemmove_end_forward_efs-.Lmemmove_begin_forward_efs
223222
.previous
224223
ENDPROC(memmove)

0 commit comments

Comments
 (0)