Skip to content

Commit df25edb

Browse files
Peter Zijlstrabp3tk0v
authored andcommitted
x86/alternatives: Add longer 64-bit NOPs
By adding support for longer NOPs there are a few more alternatives that can turn into a single instruction. Add up to NOP11, the same limit where GNU as .nops also stops generating longer nops. This is because a number of uarchs have severe decode penalties for more than 3 prefixes. [ bp: Sync up with the version in tools/ while at it. ] Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/r/20230515093020.661756940@infradead.org
1 parent d42a2a8 commit df25edb

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

arch/x86/include/asm/nops.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#define BYTES_NOP7 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00
3535
#define BYTES_NOP8 0x3e,BYTES_NOP7
3636

37+
#define ASM_NOP_MAX 8
38+
3739
#else
3840

3941
/*
@@ -47,6 +49,9 @@
4749
* 6: osp nopl 0x00(%eax,%eax,1)
4850
* 7: nopl 0x00000000(%eax)
4951
* 8: nopl 0x00000000(%eax,%eax,1)
52+
* 9: cs nopl 0x00000000(%eax,%eax,1)
53+
* 10: osp cs nopl 0x00000000(%eax,%eax,1)
54+
* 11: osp osp cs nopl 0x00000000(%eax,%eax,1)
5055
*/
5156
#define BYTES_NOP1 0x90
5257
#define BYTES_NOP2 0x66,BYTES_NOP1
@@ -56,6 +61,15 @@
5661
#define BYTES_NOP6 0x66,BYTES_NOP5
5762
#define BYTES_NOP7 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
5863
#define BYTES_NOP8 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
64+
#define BYTES_NOP9 0x2e,BYTES_NOP8
65+
#define BYTES_NOP10 0x66,BYTES_NOP9
66+
#define BYTES_NOP11 0x66,BYTES_NOP10
67+
68+
#define ASM_NOP9 _ASM_BYTES(BYTES_NOP9)
69+
#define ASM_NOP10 _ASM_BYTES(BYTES_NOP10)
70+
#define ASM_NOP11 _ASM_BYTES(BYTES_NOP11)
71+
72+
#define ASM_NOP_MAX 11
5973

6074
#endif /* CONFIG_64BIT */
6175

@@ -68,8 +82,6 @@
6882
#define ASM_NOP7 _ASM_BYTES(BYTES_NOP7)
6983
#define ASM_NOP8 _ASM_BYTES(BYTES_NOP8)
7084

71-
#define ASM_NOP_MAX 8
72-
7385
#ifndef __ASSEMBLY__
7486
extern const unsigned char * const x86_nops[];
7587
#endif

arch/x86/kernel/alternative.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ static const unsigned char x86nops[] =
9898
BYTES_NOP6,
9999
BYTES_NOP7,
100100
BYTES_NOP8,
101+
#ifdef CONFIG_64BIT
102+
BYTES_NOP9,
103+
BYTES_NOP10,
104+
BYTES_NOP11,
105+
#endif
101106
};
102107

103108
const unsigned char * const x86_nops[ASM_NOP_MAX+1] =
@@ -111,6 +116,11 @@ const unsigned char * const x86_nops[ASM_NOP_MAX+1] =
111116
x86nops + 1 + 2 + 3 + 4 + 5,
112117
x86nops + 1 + 2 + 3 + 4 + 5 + 6,
113118
x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7,
119+
#ifdef CONFIG_64BIT
120+
x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8,
121+
x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9,
122+
x86nops + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10,
123+
#endif
114124
};
115125

116126
/*

tools/arch/x86/include/asm/nops.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#define BYTES_NOP7 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00
3535
#define BYTES_NOP8 0x3e,BYTES_NOP7
3636

37+
#define ASM_NOP_MAX 8
38+
3739
#else
3840

3941
/*
@@ -47,6 +49,9 @@
4749
* 6: osp nopl 0x00(%eax,%eax,1)
4850
* 7: nopl 0x00000000(%eax)
4951
* 8: nopl 0x00000000(%eax,%eax,1)
52+
* 9: cs nopl 0x00000000(%eax,%eax,1)
53+
* 10: osp cs nopl 0x00000000(%eax,%eax,1)
54+
* 11: osp osp cs nopl 0x00000000(%eax,%eax,1)
5055
*/
5156
#define BYTES_NOP1 0x90
5257
#define BYTES_NOP2 0x66,BYTES_NOP1
@@ -56,6 +61,15 @@
5661
#define BYTES_NOP6 0x66,BYTES_NOP5
5762
#define BYTES_NOP7 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
5863
#define BYTES_NOP8 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
64+
#define BYTES_NOP9 0x2e,BYTES_NOP8
65+
#define BYTES_NOP10 0x66,BYTES_NOP9
66+
#define BYTES_NOP11 0x66,BYTES_NOP10
67+
68+
#define ASM_NOP9 _ASM_BYTES(BYTES_NOP9)
69+
#define ASM_NOP10 _ASM_BYTES(BYTES_NOP10)
70+
#define ASM_NOP11 _ASM_BYTES(BYTES_NOP11)
71+
72+
#define ASM_NOP_MAX 11
5973

6074
#endif /* CONFIG_64BIT */
6175

@@ -68,8 +82,6 @@
6882
#define ASM_NOP7 _ASM_BYTES(BYTES_NOP7)
6983
#define ASM_NOP8 _ASM_BYTES(BYTES_NOP8)
7084

71-
#define ASM_NOP_MAX 8
72-
7385
#ifndef __ASSEMBLY__
7486
extern const unsigned char * const x86_nops[];
7587
#endif

0 commit comments

Comments
 (0)