Skip to contentSkip to conversation

x86: Use MOVL when reading segment registers

PATCH Superseded Uros Bizjak 3 participants 7 messages Nov 20, 2025 – Nov 21, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 x86: Use MOVL when reading segment registers

Use MOVL when reading segment registers to avoid 0x66 operand-size override insn prefix. The segment value is always 16-bit and gets zero-extended to the full 32-bit size.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/segment.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/segment.h +1 -1
index f59ae7186940..9f5be2bbd291 100644
@@ -348,7 +348,7 @@ static inline void __loadsegment_fs(unsigned short value)
  * Save a segment register away:
  */
 #define savesegment(seg, value)				\
-	asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
+	asm("movl %%" #seg ",%k0" : "=r" (value) : : "memory")
 
 #endif /* !__ASSEMBLER__ */
 #endif /* __KERNEL__ */
Signature · 1 line
2.51.1
#1 lore raw

PATCH 2/3 x86/hyperv: Use savesegment() instead of inline asm() to save segment registers

Use standard savesegment() utility macro to save segment registers.

Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Wei Liu <[email protected]>
Cc Dexuan Cui <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/hyperv/ivm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/hyperv/ivm.c +5 -4
index 651771534cae..7365d8f43181 100644
@@ -25,6 +25,7 @@
 #include <asm/e820/api.h>
 #include <asm/desc.h>
 #include <asm/msr.h>
+#include <asm/segment.h>
 #include <uapi/asm/vmx.h>
 
 #ifdef CONFIG_AMD_MEM_ENCRYPT
@@ -315,16 +316,16 @@ int hv_snp_boot_ap(u32 apic_id, unsigned long start_ip, unsigned int cpu)
 	vmsa->gdtr.base = gdtr.address;
 	vmsa->gdtr.limit = gdtr.size;
 
-	asm volatile("movl %%es, %%eax;" : "=a" (vmsa->es.selector));
+	savesegment(es, vmsa->es.selector);
 	hv_populate_vmcb_seg(vmsa->es, vmsa->gdtr.base);
 
-	asm volatile("movl %%cs, %%eax;" : "=a" (vmsa->cs.selector));
+	savesegment(cs, vmsa->cs.selector);
 	hv_populate_vmcb_seg(vmsa->cs, vmsa->gdtr.base);
 
-	asm volatile("movl %%ss, %%eax;" : "=a" (vmsa->ss.selector));
+	savesegment(ss, vmsa->ss.selector);
 	hv_populate_vmcb_seg(vmsa->ss, vmsa->gdtr.base);
 
-	asm volatile("movl %%ds, %%eax;" : "=a" (vmsa->ds.selector));
+	savesegment(ds, vmsa->ds.selector);
 	hv_populate_vmcb_seg(vmsa->ds, vmsa->gdtr.base);
 
 	vmsa->efer = native_read_msr(MSR_EFER);
Signature · 1 line
2.51.1
maintainer Acked-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] x86/hyperv: Use savesegment() instead of inline asm() to save segment registers

Quoted 1 line · Uros Bizjak

Use standard savesegment() utility macro to save segment registers.

#3 lore raw

PATCH 3/3 x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn

In reply to Uros Bizjak

Unlike CALL instruction, VMMCALL does not push to the stack, and may be inserted before the frame pointer gets set up by the containing function.

Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Wei Liu <[email protected]>
Cc Dexuan Cui <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/hyperv/ivm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/hyperv/ivm.c +1 -1
index 7365d8f43181..be7fad43a88d 100644
@@ -392,7 +392,7 @@ u64 hv_snp_hypercall(u64 control, u64 param1, u64 param2)
 
 	register u64 __r8 asm("r8") = param2;
 	asm volatile("vmmcall"
-		     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
+		     : "=a" (hv_status),
 		       "+c" (control), "+d" (param1), "+r" (__r8)
 		     : : "cc", "memory", "r9", "r10", "r11");
 
Signature · 1 line
2.51.1
maintainer
#4 lore raw

Re: [PATCH 3/3] x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn

Quoted 13 lines · Uros Bizjak

Unlike CALL instruction, VMMCALL does not push to the stack, and may be inserted before the frame pointer gets set up by the containing function.

Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[email protected]>
Cc Wei Liu <[email protected]>
Cc Dexuan Cui <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>

Dexuan and Tianyu are more qualified than I do to review CVM changes. I will defer this to them.

Wei

Quoted 20 lines
---
 arch/x86/hyperv/ivm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/hyperv/ivm.c +1 -1
index 7365d8f43181..be7fad43a88d 100644
@@ -392,7 +392,7 @@ u64 hv_snp_hypercall(u64 control, u64 param1, u64 param2)
 
 	register u64 __r8 asm("r8") = param2;
 	asm volatile("vmmcall"
-		     : "=a" (hv_status), ASM_CALL_CONSTRAINT,
+		     : "=a" (hv_status),
 		       "+c" (control), "+d" (param1), "+r" (__r8)
 		     : : "cc", "memory", "r9", "r10", "r11");
 
Signature · 1 line
2.51.1
Tested-by on v1 1/3
#5 lore raw
In reply to Uros Bizjak

From: Uros Bizjak <[email protected]>

Quoted 29 lines

Use MOVL when reading segment registers to avoid 0x66 operand-size override insn prefix. The segment value is always 16-bit and gets zero-extended to the full 32-bit size.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/segment.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/segment.h +1 -2
index f59ae7186940..9f5be2bbd291 100644
@@ -348,7 +348,7 @@ static inline void __loadsegment_fs(unsigned short value)
  * Save a segment register away:
  */
 #define savesegment(seg, value)				\
-	asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
+	asm("movl %%" #seg ",%k0" : "=r" (value) : : "memory")
 
 #endif /* !__ASSEMBLER__ */
 #endif /* __KERNEL__ */
--
Quoted 2 lines

2.51.1

I've built a linux-next20251119 kernel plus the three patches in this series, and tested it in an SEV-SNP VM in the Azure public cloud. The VM is a Standard DC16ads v5 (16 vcpus, 64 GiB memory) running Ubuntu 20.04. It boots and does basic smoke tests with no issues. So, for all three patches,

I do have a comment on the commit message for Patch 3 (I would have replied there, but for unknown reasons the third patch didn't show up in my LKML feed). The commit message says "VMMCALL ... may be inserted before the frame pointer". This text was somewhat ambiguous to me. I initially read it as "the compiler might insert VMCALL before the frame pointer is set up". But I think you meant "it's OK to allow the compiler to insert the VMMCALL before the frame point is set up". Maybe the intended meaning is obvious to experts in this area, but I'm new enough that it was confusing to me. ;-)

To avoid any future confusion, I'd suggest this wording, which is explicit about why this patch is appropriate:

Unlike the CALL instruction, VMMCALL does not push to the stack, so it's OK to allow the compiler to insert it before the frame pointer gets set up by the containing function. ASM_CALL_CONSTRAINT is for CALLs that must be after the frame pointer is set up, so it is over-constraining here and can be removed.

FWIW, removing the ASM_CALL_CONSTRAINT does not change the generated code for hv_snp_hypercall().

Michael

#6 lore raw
Quoted 55 lines · Michael Kelley

From: Uros Bizjak <[email protected]>

Quoted 29 lines

Use MOVL when reading segment registers to avoid 0x66 operand-size override insn prefix. The segment value is always 16-bit and gets zero-extended to the full 32-bit size.

Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
---
 arch/x86/include/asm/segment.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/segment.h +1 -2
index f59ae7186940..9f5be2bbd291 100644
@@ -348,7 +348,7 @@ static inline void __loadsegment_fs(unsigned short value)
  * Save a segment register away:
  */
 #define savesegment(seg, value)                              \
-     asm("mov %%" #seg ",%0":"=r" (value) : : "memory")
+     asm("movl %%" #seg ",%k0" : "=r" (value) : : "memory")
 
 #endif /* !__ASSEMBLER__ */
 #endif /* __KERNEL__ */
--
Quoted 2 lines

2.51.1

I've built a linux-next20251119 kernel plus the three patches in this series, and tested it in an SEV-SNP VM in the Azure public cloud. The VM is a Standard DC16ads v5 (16 vcpus, 64 GiB memory) running Ubuntu 20.04. It boots and does basic smoke tests with no issues. So, for all three patches,

I do have a comment on the commit message for Patch 3 (I would have replied there, but for unknown reasons the third patch didn't show up in my LKML feed). The commit message says "VMMCALL ... may be inserted before the frame pointer". This text was somewhat ambiguous to me. I initially read it as "the compiler might insert VMCALL before the frame pointer is set up". But I think you meant "it's OK to allow the compiler to insert the VMMCALL before the frame point is set up". Maybe the intended meaning is obvious to experts in this area, but I'm new enough that it was confusing to me. ;-)

To avoid any future confusion, I'd suggest this wording, which is explicit about why this patch is appropriate:

Quoted 8 lines

Unlike the CALL instruction, VMMCALL does not push to the stack, so it's OK to allow the compiler to insert it before the frame pointer gets set up by the containing function. ASM_CALL_CONSTRAINT is for CALLs that must be after the frame pointer is set up, so it is over-constraining here and can be removed.

FWIW, removing the ASM_CALL_CONSTRAINT does not change the generated code for hv_snp_hypercall().

Michael,

thanks for your testing and rewording suggestion! After reading it again a couple of times, I agree that the original text is a bit too terse, and adding your words indeed clear it up considerably! I have changed the patch description as you proposed in v2.

BR,
Uros.