[v2,2/3] x86: limit coverage of -mlfence-after-load

Message ID f981e7b1-a333-4e84-89c0-b2d8107dcd41@suse.com
State New
Headers
Series x86: coverage of -mlfence-after-load |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

Jan Beulich Oct. 30, 2026, 10:40 a.m. UTC
  The original change went a little too far in certain cases:
- POP with a memory operand doesn't alter any register (other than the
  stack pointer itself),
- NOT with a memory operand doesn't alter any register, in particular
  not EFLAGS,
- shift/rotate insns with a memory operand and a zero immediate one don't
  alter any register, in particular not EFLAGS,
- LIDT, unlike LGDT and LLDT, but like LTR, has no effect on subsequent
  memory accesses (unless they incur a fault),
- INVPCID doesn't alter any register, in particular not EFLAGS (oddly it
  is covered by the existing testcase, as if it was an exception).

While doing these adjustments, drop the special casing of extension opcode
1/6 for, respectively, major opcodes F6/F7 and C0/C1/D0/D1/D2/D3. These
are well known alias encodings which the assembler won't use, but which
also cannot be re-used by future new ISA extensions. Also drop the
meaningless any_vex_p check around the remaining ANY_SIZE check.
---
v2: New.
  

Patch

--- a/gas/config/tc-i386.c
+++ b/gas/config/tc-i386.c
@@ -6655,20 +6655,12 @@  static int
 load_insn_p (void)
 {
   unsigned int dest;
-  int any_vex_p = is_any_vex_encoding (&i.tm);
   unsigned int base_opcode = i.tm.base_opcode | 1;
 
-  if (!any_vex_p)
-    {
-      /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
-	 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote.  */
-      if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
-	return 0;
-
-      /* pop.   */
-      if (i.tm.mnem_off == MN_pop)
-	return 1;
-    }
+  /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
+     bndcn, bndstx, bndldx, clflushopt, clwb, cldemote.  */
+  if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
+    return 0;
 
   if (i.tm.opcode_space == SPACE_BASE)
     {
@@ -6691,9 +6683,15 @@  load_insn_p (void)
 
   /* No memory operand.  */
   if (!i.mem_operands)
+    {
+      /* pop.   */
+      if (i.tm.mnem_off == MN_pop)
+	return 1;
+
     return 0;
+    }
 
-  if (any_vex_p)
+  if (is_any_vex_encoding (&i.tm))
     {
       if (i.tm.mnem_off == MN_vldmxcsr)
 	return 1;
@@ -6705,8 +6703,8 @@  load_insn_p (void)
 	  || (base_opcode | 2) == 0x87)
 	return 1;
 
-      /* test, not, neg, mul, imul, div, idiv.  */
-      if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
+      /* test, neg, mul, imul, div, idiv.  */
+      if (base_opcode == 0xf7 && i.tm.extension_opcode != 2)
 	return 1;
 
       /* inc, dec.  */
@@ -6719,7 +6717,9 @@  load_insn_p (void)
 
       /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
       if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
-	  && i.tm.extension_opcode != 6)
+	  && (!operand_type_check (i.types[0], imm)
+	      || i.op[0].imms->X_op != O_constant
+	      || i.op[0].imms->X_add_number != 0))
 	return 1;
 
       /* arpl.  (Also movxsd, which otherwise would be covered towards
@@ -6776,7 +6776,10 @@  load_insn_p (void)
 	return 1;
 
       /* shld, shrd.  */
-      if ((base_opcode | 8) == 0xad)
+      if ((base_opcode | 8) == 0xad
+	  && (!operand_type_check (i.types[0], imm)
+	      || i.op[0].imms->X_op != O_constant
+	      || i.op[0].imms->X_add_number != 0))
 	return 1;
 
       /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld, vmxon, vmclear, vmptrst.  */
@@ -6806,17 +6809,22 @@  load_insn_p (void)
 	      || (i.tm.extension_opcode | 1) == 5))
 	return 1;
 
-      /* lgdt, lidt, lmsw.  */
+      /* lgdt, lmsw.  */
       if (i.tm.base_opcode == 0x01
-	  && (i.tm.extension_opcode == 2
-	      || i.tm.extension_opcode == 3
-	      || i.tm.extension_opcode == 6))
+	  && (i.tm.extension_opcode | 4) == 6)
 	return 1;
 
       /* cmpxchg, xadd.  */
       if (base_opcode == 0xb1 || base_opcode == 0xc1)
 	return 1;
     }
+  else if (i.tm.opcode_space == SPACE_0F38)
+    {
+      /* invpcid.  */
+      if (i.tm.base_opcode == 0x82
+	  && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
+	return 0;
+    }
 
   dest = i.operands - 1;
 
--- a/gas/testsuite/gas/i386/lfence-load.d
+++ b/gas/testsuite/gas/i386/lfence-load.d
@@ -13,6 +13,7 @@  Disassembly of section .text:
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	0f 01 55 00          	lgdtl  0x0\(%ebp\)
  +[a-f0-9]+:	0f ae e8             	lfence
+ +[a-f0-9]+:	0f 01 5d 00          	lidtl  0x0\(%ebp\)
  +[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\)
@@ -24,7 +25,6 @@  Disassembly of section .text:
  +[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\)
  +[a-f0-9]+:	0f ae 7d 00          	clflush 0x0\(%ebp\)
  +[a-f0-9]+:	66 0f ae 7d 00       	clflushopt 0x0\(%ebp\)
@@ -92,17 +92,16 @@  Disassembly of section .text:
  +[a-f0-9]+:	0f 0f 66 02 b0       	pfcmpeq 0x2\(%esi\),%mm4
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	8f 00                	pop    \(%eax\)
- +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	58                   	pop    %eax
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	66 d1 11             	rclw   \$1,\(%ecx\)
  +[a-f0-9]+:	0f ae e8             	lfence
+ +[a-f0-9]+:	c0 19 00             	rcrb   \$(0x)?0,\(%ecx\)
  +[a-f0-9]+:	f7 01 01 00 00 00    	testl  \$0x1,\(%ecx\)
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	ff 01                	incl   \(%ecx\)
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	f7 11                	notl   \(%ecx\)
- +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	f7 31                	divl   \(%ecx\)
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	f7 21                	mull   \(%ecx\)
@@ -131,6 +130,7 @@  Disassembly of section .text:
  +[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 00          	shld   \$(0x)?0,%eax,\(%ecx\)
  +[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/gas/testsuite/gas/i386/lfence-load.s
+++ b/gas/testsuite/gas/i386/lfence-load.s
@@ -2,6 +2,7 @@ 
 _start:
 	vldmxcsr (%ebp)
 	lgdt (%ebp)
+	lidt (%ebp)
 	vmptrld (%ebp)
 	vmptrst (%ebp)
 	vmclear (%ebp)
@@ -57,6 +58,7 @@  _start:
 	popl (%eax)
 	popl %eax
 	rclw	(%ecx)
+	rcrb	$0,(%ecx)
 	testl	$1,(%ecx)
 	incl	(%ecx)
 	notl	(%ecx)
@@ -75,6 +77,7 @@  _start:
 	addl $1, (%eax)
 	btl $1, (%eax)
 	bt %eax, (%ecx)
+	shld $0, %eax, (%ecx)
 	shld $1, %eax, (%ecx)
 	shrd %cl, %eax, (%ecx)
 	cmpxchg %eax, (%ebx)
--- a/gas/testsuite/gas/i386/x86-64-lfence-load.d
+++ b/gas/testsuite/gas/i386/x86-64-lfence-load.d
@@ -13,6 +13,7 @@  Disassembly of section .text:
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	0f 01 55 00          	lgdt   0x0\(%rbp\)
  +[a-f0-9]+:	0f ae e8             	lfence
+ +[a-f0-9]+:	0f 01 5d 00          	lidt   0x0\(%rbp\)
  +[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\)
@@ -24,7 +25,6 @@  Disassembly of section .text:
  +[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\)
  +[a-f0-9]+:	0f ae 7d 00          	clflush 0x0\(%rbp\)
  +[a-f0-9]+:	66 0f ae 7d 00       	clflushopt 0x0\(%rbp\)
@@ -99,17 +99,16 @@  Disassembly of section .text:
  +[a-f0-9]+:	0f 0f 66 02 b0       	pfcmpeq 0x2\(%rsi\),%mm4
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	8f 00                	pop    \(%rax\)
- +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	58                   	pop    %rax
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	66 d1 11             	rclw   \$1,\(%rcx\)
  +[a-f0-9]+:	0f ae e8             	lfence
+ +[a-f0-9]+:	48 c1 19 00          	rcrq   \$(0x)?0,\(%rcx\)
  +[a-f0-9]+:	f7 01 01 00 00 00    	testl  \$0x1,\(%rcx\)
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	ff 01                	incl   \(%rcx\)
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	f7 11                	notl   \(%rcx\)
- +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	f7 31                	divl   \(%rcx\)
  +[a-f0-9]+:	0f ae e8             	lfence
  +[a-f0-9]+:	f7 21                	mull   \(%rcx\)
@@ -138,6 +137,7 @@  Disassembly of section .text:
  +[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 00          	shrd   \$(0x)?0,%eax,\(%rcx\)
  +[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/gas/testsuite/gas/i386/x86-64-lfence-load.s
+++ b/gas/testsuite/gas/i386/x86-64-lfence-load.s
@@ -2,6 +2,7 @@ 
 _start:
 	vldmxcsr (%rbp)
 	lgdt (%rbp)
+	lidt (%rbp)
 	vmptrld (%rbp)
 	vmptrst (%rbp)
 	vmclear (%rbp)
@@ -64,6 +65,7 @@  _start:
 	popq (%rax)
 	popq %rax
 	rclw	(%rcx)
+	rcrq	$0,(%rcx)
 	testl	$1,(%rcx)
 	incl	(%rcx)
 	notl	(%rcx)
@@ -82,6 +84,7 @@  _start:
 	addl $1, (%r11)
 	btl $1, (%r11)
 	btc %eax, (%rcx)
+	shrd $0, %eax, (%rcx)
 	shrd $1, %eax, (%rcx)
 	shld %cl, %eax, (%rcx)
 	cmpxchg %rax, (%rbx)