Skip to contentSkip to conversation

x86: Use MOVL when reading segment registers

PATCH Applied Uros Bizjak 4 participants 8 messages Nov 21, 2025 – Feb 18

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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 v2 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 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
#4 lore raw

PATCH v2 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, 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 inserted after the frame pointer is set up, so it is over-constraining here and can be removed.

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]>

---
v2: Expand commit message and include ASM_CALL_CONSTRAINT explanation

---
 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
#5 lore raw

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

Quoted 28 lines · Uros Bizjak

Unlike 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 inserted after the frame pointer is set up, so it is over-constraining here and can be removed.

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]>

---
v2: Expand commit message and include ASM_CALL_CONSTRAINT explanation
---
arch/x86/hyperv/ivm.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/hyperv/ivm.c +0 -0
index 7365d8f43181..be7fad43a88d 100644

arch/x86/hyperv/ivm.c u64 hv_snp_hypercall(u64 control, u64 param1, u64 param2) · line 398

Quoted 8 lines
arch/x86/hyperv/ivm.c +1 -1
@@ -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");
 

I think it would be good to have a comment at the point where ASM_CALL_CONSTRAINT is defined explaining its proper use.

Specifically, instructions like syscall, vmcall, vmfunc, vmmcall, int xx and VM-specific escape instructions are not "calls" because they either don't modify the stack or create an exception frame (kernel) or signal frame (user space) which is completely special.

#6 lore raw

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

Quoted 41 lines · H. Peter Anvin
Quoted 28 lines · Uros Bizjak

Unlike 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 inserted after the frame pointer is set up, so it is over-constraining here and can be removed.

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]>

---
v2: Expand commit message and include ASM_CALL_CONSTRAINT explanation
---
arch/x86/hyperv/ivm.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/hyperv/ivm.c +0 -0
index 7365d8f43181..be7fad43a88d 100644

arch/x86/hyperv/ivm.c u64 hv_snp_hypercall(u64 control, u64 param1, u64 param2) · line 398

Quoted 8 lines
arch/x86/hyperv/ivm.c +1 -1
@@ -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");
 

I think it would be good to have a comment at the point where ASM_CALL_CONSTRAINT is defined explaining its proper use.

Quoted 1 line

Specifically, instructions like syscall, vmcall, vmfunc, vmmcall, int xx and VM-specific escape instructions are not "calls" because they either don't modify the stack or create an exception frame (kernel) or signal frame (user space) which is completely special.

The existing comment already mentions CALL instruction only:

/*
* This output constraint should be used for any inline asm which has a "call"
* instruction. Otherwise the asm may be inserted before the frame pointer
* gets set up by the containing function. If you forget to do this, objtool
* may print a "call without frame pointer save/setup" warning. */

Uros.

#2 lore raw

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

In reply to Uros Bizjak

From: Uros Bizjak <[email protected]> Sent: Friday, November 21, 2025 6:14 AM

Quoted 2 lines

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

Patch 1 of this series was included in the tip tree. But this patch (Patch 2) and Patch 3 have not been picked up anywhere.

Wei Liu -- could you pick these two up in the hyperv tree?

Michael

Quoted 52 lines
Cc "K. Y. Srinivasan" <[email protected]>
Cc Haiyang Zhang <[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);
--
2.51.1

#7 lore raw

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

In reply to Uros Bizjak
Quoted 5 lines · Uros Bizjak
Quoted 4 lines

I think it would be good to have a comment at the point where ASM_CALL_CONSTRAINT is defined explaining its proper use.

Specifically, instructions like syscall, vmcall, vmfunc, vmmcall, int xx and VM-specific escape instructions are not "calls" because they either don't modify the stack or create an exception frame (kernel) or signal frame (user space) which is completely special.

Quoted 9 lines

The existing comment already mentions CALL instruction only:

/*
* This output constraint should be used for any inline asm which has a "call"
* instruction. Otherwise the asm may be inserted before the frame pointer
* gets set up by the containing function. If you forget to do this, objtool
* may print a "call without frame pointer save/setup" warning. */

Yes. Some people seem to have misunderstood it to mean any instruction with "CALL" in the name.

-hpa

maintainer Applied
#3 lore raw

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

In reply to Michael Kelley
Quoted 8 lines · Michael Kelley

From: Uros Bizjak <[email protected]> Sent: Friday, November 21, 2025 6:14 AM

Quoted 2 lines

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

Patch 1 of this series was included in the tip tree. But this patch (Patch 2) and Patch 3 have not been picked up anywhere.

Wei Liu -- could you pick these two up in the hyperv tree?

Applied patch 2 and 3. Thank you for the reminder.

Wei