[binutils-gdb] x86: extend coverage of -mlfence-after-load
Jan Beulich
jbeulich@sourceware.org
Fri Nov 6 07:18:40 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b81727ba00177913db1ccc6a2c75c25bfd277a11
commit b81727ba00177913db1ccc6a2c75c25bfd277a11
Author: Jan Beulich <jbeulich@suse.com>
Date: Fri Nov 6 08:15:51 2026 +0100
x86: extend coverage of -mlfence-after-load
The generic pattern at the bottom of load_insn_p() covers insns with a
memory operand where that operand isn't the destination one. There are
various exceptions to this pattern, which need custom handling earlier in
the function.
Without even considering ISA extensions which appeared only in newer
hardware, there are several omissions:
- LLDT isn't any different from LGDT, as the new descriptor table may
have an effect on subsequent memory reads (covering LIDT is actually
questionable, as no such effect exists there),
- VERR and VERW are memory reads updating EFLAGS,
- BT{,C,R,S} with register operand aren't any different from ones with
immediate operand,
- SHLD/SHRD aren't any different from SHL/SHR,
- ARPL is read-modify-write and updates EFLAGS,
- CMPXCHG isn't any different from CMPXCHG{8,16}B or XADD,
- VMXON, VMCLEAR, VMPTRST, and VMREAD update EFLAGS (oddly enough VMCLEAR
is covered by the existing testcase, as if it was an exception), much
like ALU read-modify-write insns do,
Additionally
- fold the SPACE_BASE check at the bottom into the bigger SPACE_BASE
block,
- move XADD checking into the SPACE_0F block (to be together with
CMPXCHG),
- simplify the checks for the SPACE_BASE major opcode 0x80..0x83 and
0xA4..0xAF ranges.
Diff:
---
gas/config/tc-i386.c | 61 ++++++++++++++++++++---------
gas/testsuite/gas/i386/lfence-load.d | 21 ++++++++++
gas/testsuite/gas/i386/lfence-load.s | 10 +++++
gas/testsuite/gas/i386/x86-64-lfence-load.d | 19 +++++++++
gas/testsuite/gas/i386/x86-64-lfence-load.s | 9 +++++
5 files changed, 101 insertions(+), 19 deletions(-)
diff --git a/gas/config/tc-i386.c b/gas/config/tc-i386.c
index 19cee094277..b8ffde9f600 100644
--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -6678,7 +6678,7 @@ load_insn_p (void)
return 1;
/* movs, cmps, lods, scas. */
- if ((i.tm.base_opcode | 0xb) == 0xaf)
+ if ((base_opcode | 0xa) == 0xaf)
return 1;
/* outs, xlatb. */
@@ -6700,6 +6700,11 @@ load_insn_p (void)
}
else if (i.tm.opcode_space == SPACE_BASE)
{
+ /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
+ if ((base_opcode | 0x38) == 0x39
+ || (base_opcode | 2) == 0x87)
+ return 1;
+
/* test, not, neg, mul, imul, div, idiv. */
if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
return 1;
@@ -6709,7 +6714,7 @@ load_insn_p (void)
return 1;
/* add, or, adc, sbb, and, sub, xor, cmp. */
- if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
+ if ((base_opcode | 2) == 0x83)
return 1;
/* rol, ror, rcl, rcr, shl/sal, shr, sar. */
@@ -6717,6 +6722,11 @@ load_insn_p (void)
&& i.tm.extension_opcode != 6)
return 1;
+ /* arpl. (Also movxsd, which otherwise would be covered towards
+ the end of the function.) */
+ if (i.tm.base_opcode == 0x63)
+ return 1;
+
/* Check for x87 instructions. */
if ((base_opcode | 6) == 0xdf)
{
@@ -6760,22 +6770,40 @@ load_insn_p (void)
else if (i.tm.opcode_space == SPACE_0F)
{
/* bt, bts, btr, btc. */
- if (i.tm.base_opcode == 0xba
- && (i.tm.extension_opcode | 3) == 7)
+ if ((i.tm.base_opcode == 0xba
+ && (i.tm.extension_opcode | 3) == 7)
+ || (i.tm.base_opcode | 0x18) == 0xbb)
return 1;
- /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
+ /* shld, shrd. */
+ if ((base_opcode | 8) == 0xad)
+ return 1;
+
+ /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld, vmxon, vmclear, vmptrst. */
if (i.tm.base_opcode == 0xc7
- && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
- && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
- || i.tm.extension_opcode == 6))
+ && ((i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
+ && ((i.tm.extension_opcode | 2) == 3))
+ || (i.tm.extension_opcode | 1) == 7))
+ return 1;
+
+ /* vmread. (Also vcvtts{s,d}2usi and vcvttp{s,d}2uqq, which otherwise
+ would be covered towards the end of the function.) */
+ if (i.tm.base_opcode == 0x78)
return 1;
- /* fxrstor, ldmxcsr, xrstor. */
+ /* fxrstor, ldmxcsr, xrstor, xsave, xsaveopt. */
if (i.tm.base_opcode == 0xae
&& (i.tm.extension_opcode == 1
|| i.tm.extension_opcode == 2
- || i.tm.extension_opcode == 5))
+ || i.tm.extension_opcode == 5
+ || (i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
+ && (i.tm.extension_opcode | 2) == 6)))
+ return 1;
+
+ /* lldt, verr, verw. */
+ if (i.tm.base_opcode == 0x00
+ && (i.tm.extension_opcode == 2
+ || (i.tm.extension_opcode | 1) == 5))
return 1;
/* lgdt, lidt, lmsw. */
@@ -6784,6 +6812,10 @@ load_insn_p (void)
|| i.tm.extension_opcode == 3
|| i.tm.extension_opcode == 6))
return 1;
+
+ /* cmpxchg, xadd. */
+ if (base_opcode == 0xb1 || base_opcode == 0xc1)
+ return 1;
}
dest = i.operands - 1;
@@ -6794,15 +6826,6 @@ load_insn_p (void)
&& i.types[dest].bitfield.imm8)
dest--;
- /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
- if (i.tm.opcode_space == SPACE_BASE
- && ((base_opcode | 0x38) == 0x39
- || (base_opcode | 2) == 0x87))
- return 1;
-
- if (i.tm.mnem_off == MN_xadd)
- return 1;
-
/* Check for load instruction. */
return (i.types[dest].bitfield.class != ClassNone
|| i.types[dest].bitfield.instance == Accum);
diff --git a/gas/testsuite/gas/i386/lfence-load.d b/gas/testsuite/gas/i386/lfence-load.d
index 4bed6001644..14f578c661d 100644
--- a/gas/testsuite/gas/i386/lfence-load.d
+++ b/gas/testsuite/gas/i386/lfence-load.d
@@ -15,7 +15,14 @@ Disassembly of section .text:
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 0f c7 75 00 vmptrld 0x0\(%ebp\)
+[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f c7 7d 00 vmptrst 0x0\(%ebp\)
+ +[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 66 0f c7 75 00 vmclear 0x0\(%ebp\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: f3 0f c7 33 vmxon \(%ebx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f 78 0b vmread %ecx,\(%ebx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 66 0f 38 82 55 00 invpcid 0x0\(%ebp\),%edx
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 0f 01 7d 00 invlpg 0x0\(%ebp\)
@@ -42,6 +49,10 @@ Disassembly of section .text:
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: d7 xlat %ds:\(%ebx\)
+[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f 00 12 lldt \(%edx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f 00 22 verr \(%edx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: d9 55 00 fsts 0x0\(%ebp\)
+[a-f0-9]+: d9 45 00 flds 0x0\(%ebp\)
+[a-f0-9]+: 0f ae e8 lfence
@@ -118,6 +129,14 @@ Disassembly of section .text:
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 0f ba 20 01 btl \$0x1,\(%eax\)
+[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f a3 01 bt %eax,\(%ecx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f a4 01 01 shld \$(0x)?1,%eax,\(%ecx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f ad 01 shrd %cl,%eax,\(%ecx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f b1 03 cmpxchg %eax,\(%ebx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 0f c1 03 xadd %eax,\(%ebx\)
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 0f c1 c3 xadd %eax,%ebx
@@ -136,4 +155,6 @@ Disassembly of section .text:
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 85 45 40 test %eax,0x40\(%ebp\)
+[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 63 01 arpl %eax,\(%ecx\)
+ +[a-f0-9]+: 0f ae e8 lfence
#pass
diff --git a/gas/testsuite/gas/i386/lfence-load.s b/gas/testsuite/gas/i386/lfence-load.s
index 157ee28f28b..883b8a7b46d 100644
--- a/gas/testsuite/gas/i386/lfence-load.s
+++ b/gas/testsuite/gas/i386/lfence-load.s
@@ -3,7 +3,10 @@ _start:
vldmxcsr (%ebp)
lgdt (%ebp)
vmptrld (%ebp)
+ vmptrst (%ebp)
vmclear (%ebp)
+ vmxon (%ebx)
+ vmread %ecx, (%ebx)
invpcid (%ebp), %edx
invlpg (%ebp)
clflush (%ebp)
@@ -25,6 +28,8 @@ _start:
popf
popa
xlatb (%ebx)
+ lldt (%edx)
+ verr (%edx)
fsts (%ebp)
flds (%ebp)
fistl (%ebp)
@@ -69,6 +74,10 @@ _start:
rep lodsl
addl $1, (%eax)
btl $1, (%eax)
+ bt %eax, (%ecx)
+ shld $1, %eax, (%ecx)
+ shrd %cl, %eax, (%ecx)
+ cmpxchg %eax, (%ebx)
xadd %eax,(%ebx)
xadd %eax,%ebx
xchg %eax,(%ebx)
@@ -79,3 +88,4 @@ _start:
add (%eax),%eax
test %eax,0x40(%ebp)
test 0x40(%ebp),%eax
+ arpl %eax, (%ecx)
diff --git a/gas/testsuite/gas/i386/x86-64-lfence-load.d b/gas/testsuite/gas/i386/x86-64-lfence-load.d
index 5c8ae326292..a278aa2bce1 100644
--- a/gas/testsuite/gas/i386/x86-64-lfence-load.d
+++ b/gas/testsuite/gas/i386/x86-64-lfence-load.d
@@ -15,7 +15,14 @@ Disassembly of section .text:
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 0f c7 75 00 vmptrld 0x0\(%rbp\)
+[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f c7 7d 00 vmptrst 0x0\(%rbp\)
+ +[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 66 0f c7 75 00 vmclear 0x0\(%rbp\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: f3 0f c7 33 vmxon \(%rbx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f 78 0b vmread %rcx,\(%rbx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 66 0f 38 82 55 00 invpcid 0x0\(%rbp\),%rdx
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 67 0f 01 38 invlpg \(%eax\)
@@ -42,6 +49,10 @@ Disassembly of section .text:
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: d7 xlat \(%rbx\)
+[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f 00 12 lldt \(%rdx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f 00 2a verw \(%rdx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: d9 55 00 fsts 0x0\(%rbp\)
+[a-f0-9]+: d9 45 00 flds 0x0\(%rbp\)
+[a-f0-9]+: 0f ae e8 lfence
@@ -125,6 +136,14 @@ Disassembly of section .text:
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 41 0f ba 23 01 btl \$0x1,\(%r11\)
+[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f bb 01 btc %eax,\(%rcx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f ac 01 01 shrd \$(0x)?1,%eax,\(%rcx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 0f a5 01 shld %cl,%eax,\(%rcx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+ +[a-f0-9]+: 48 0f b1 03 cmpxchg %rax,\(%rbx\)
+ +[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 48 0f c1 03 xadd %rax,\(%rbx\)
+[a-f0-9]+: 0f ae e8 lfence
+[a-f0-9]+: 48 0f c1 c3 xadd %rax,%rbx
diff --git a/gas/testsuite/gas/i386/x86-64-lfence-load.s b/gas/testsuite/gas/i386/x86-64-lfence-load.s
index e30b9a49800..a63f43ec175 100644
--- a/gas/testsuite/gas/i386/x86-64-lfence-load.s
+++ b/gas/testsuite/gas/i386/x86-64-lfence-load.s
@@ -3,7 +3,10 @@ _start:
vldmxcsr (%rbp)
lgdt (%rbp)
vmptrld (%rbp)
+ vmptrst (%rbp)
vmclear (%rbp)
+ vmxon (%rbx)
+ vmread %rcx, (%rbx)
invpcid (%rbp), %rdx
invlpg (%eax)
clflush (%rbp)
@@ -26,6 +29,8 @@ _start:
pop %fs
popf
xlatb (%rbx)
+ lldt (%rdx)
+ verw (%rdx)
fsts (%rbp)
flds (%rbp)
fistl (%rbp)
@@ -76,6 +81,10 @@ _start:
rep lodsl
addl $1, (%r11)
btl $1, (%r11)
+ btc %eax, (%rcx)
+ shrd $1, %eax, (%rcx)
+ shld %cl, %eax, (%rcx)
+ cmpxchg %rax, (%rbx)
xadd %rax,(%rbx)
xadd %rax,%rbx
xchg %rax,(%rbx)
More information about the Binutils-cvs
mailing list