linux-hyperv.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] x86: Use MOVL when reading segment registers
@ 2025-11-20 13:40 Uros Bizjak
  2025-11-20 13:40 ` [PATCH 2/3] x86/hyperv: Use savesegment() instead of inline asm() to save " Uros Bizjak
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Uros Bizjak @ 2025-11-20 13:40 UTC (permalink / raw)
  To: linux-hyperv, x86, linux-kernel
  Cc: Uros Bizjak, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
	Dave Hansen, H. Peter Anvin

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.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
---
 arch/x86/include/asm/segment.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
index f59ae7186940..9f5be2bbd291 100644
--- a/arch/x86/include/asm/segment.h
+++ b/arch/x86/include/asm/segment.h
@@ -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__ */
-- 
2.51.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 2/3] x86/hyperv: Use savesegment() instead of inline asm() to save segment registers
  2025-11-20 13:40 [PATCH 1/3] x86: Use MOVL when reading segment registers Uros Bizjak
@ 2025-11-20 13:40 ` Uros Bizjak
  2025-11-20 16:29   ` Wei Liu
  2025-11-20 13:40 ` [PATCH 3/3] x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn Uros Bizjak
  2025-11-21  1:03 ` [PATCH 1/3] x86: Use MOVL when reading segment registers Michael Kelley
  2 siblings, 1 reply; 7+ messages in thread
From: Uros Bizjak @ 2025-11-20 13:40 UTC (permalink / raw)
  To: linux-hyperv, x86, linux-kernel
  Cc: Uros Bizjak, K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H. Peter Anvin

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

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: "K. Y. Srinivasan" <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Wei Liu <wei.liu@kernel.org>
Cc: Dexuan Cui <decui@microsoft.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
---
 arch/x86/hyperv/ivm.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c
index 651771534cae..7365d8f43181 100644
--- a/arch/x86/hyperv/ivm.c
+++ b/arch/x86/hyperv/ivm.c
@@ -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


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH 3/3] x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn
  2025-11-20 13:40 [PATCH 1/3] x86: Use MOVL when reading segment registers Uros Bizjak
  2025-11-20 13:40 ` [PATCH 2/3] x86/hyperv: Use savesegment() instead of inline asm() to save " Uros Bizjak
@ 2025-11-20 13:40 ` Uros Bizjak
  2025-11-20 16:31   ` Wei Liu
  2025-11-21  1:03 ` [PATCH 1/3] x86: Use MOVL when reading segment registers Michael Kelley
  2 siblings, 1 reply; 7+ messages in thread
From: Uros Bizjak @ 2025-11-20 13:40 UTC (permalink / raw)
  To: linux-hyperv, x86, linux-kernel
  Cc: Uros Bizjak, K. Y. Srinivasan, Haiyang Zhang, Wei Liu, Dexuan Cui,
	Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H. Peter Anvin

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.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
Cc: "K. Y. Srinivasan" <kys@microsoft.com>
Cc: Haiyang Zhang <haiyangz@microsoft.com>
Cc: Wei Liu <wei.liu@kernel.org>
Cc: Dexuan Cui <decui@microsoft.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>
Cc: "H. Peter Anvin" <hpa@zytor.com>
---
 arch/x86/hyperv/ivm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c
index 7365d8f43181..be7fad43a88d 100644
--- a/arch/x86/hyperv/ivm.c
+++ b/arch/x86/hyperv/ivm.c
@@ -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");
 
-- 
2.51.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH 2/3] x86/hyperv: Use savesegment() instead of inline asm() to save segment registers
  2025-11-20 13:40 ` [PATCH 2/3] x86/hyperv: Use savesegment() instead of inline asm() to save " Uros Bizjak
@ 2025-11-20 16:29   ` Wei Liu
  0 siblings, 0 replies; 7+ messages in thread
From: Wei Liu @ 2025-11-20 16:29 UTC (permalink / raw)
  To: Uros Bizjak
  Cc: linux-hyperv, x86, linux-kernel, K. Y. Srinivasan, Haiyang Zhang,
	Wei Liu, Dexuan Cui, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin

On Thu, Nov 20, 2025 at 02:40:23PM +0100, Uros Bizjak wrote:
> Use standard savesegment() utility macro to save segment registers.

Acked-by: Wei Liu <wei.liu@kernel.org>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 3/3] x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn
  2025-11-20 13:40 ` [PATCH 3/3] x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn Uros Bizjak
@ 2025-11-20 16:31   ` Wei Liu
  0 siblings, 0 replies; 7+ messages in thread
From: Wei Liu @ 2025-11-20 16:31 UTC (permalink / raw)
  To: Uros Bizjak
  Cc: linux-hyperv, x86, linux-kernel, K. Y. Srinivasan, Haiyang Zhang,
	Wei Liu, Dexuan Cui, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin

On Thu, Nov 20, 2025 at 02:40:24PM +0100, Uros Bizjak wrote:
> 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.
> 
> Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
> Cc: "K. Y. Srinivasan" <kys@microsoft.com>
> Cc: Haiyang Zhang <haiyangz@microsoft.com>
> Cc: Wei Liu <wei.liu@kernel.org>
> Cc: Dexuan Cui <decui@microsoft.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@redhat.com>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
> Cc: "H. Peter Anvin" <hpa@zytor.com>

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

Wei

> ---
>  arch/x86/hyperv/ivm.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c
> index 7365d8f43181..be7fad43a88d 100644
> --- a/arch/x86/hyperv/ivm.c
> +++ b/arch/x86/hyperv/ivm.c
> @@ -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");
>  
> -- 
> 2.51.1
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: [PATCH 1/3] x86: Use MOVL when reading segment registers
  2025-11-20 13:40 [PATCH 1/3] x86: Use MOVL when reading segment registers Uros Bizjak
  2025-11-20 13:40 ` [PATCH 2/3] x86/hyperv: Use savesegment() instead of inline asm() to save " Uros Bizjak
  2025-11-20 13:40 ` [PATCH 3/3] x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn Uros Bizjak
@ 2025-11-21  1:03 ` Michael Kelley
  2025-11-21  8:09   ` Uros Bizjak
  2 siblings, 1 reply; 7+ messages in thread
From: Michael Kelley @ 2025-11-21  1:03 UTC (permalink / raw)
  To: Uros Bizjak, linux-hyperv@vger.kernel.org, x86@kernel.org,
	linux-kernel@vger.kernel.org
  Cc: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
	H. Peter Anvin

From: Uros Bizjak <ubizjak@gmail.com>
> 
> 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.
> 
> Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
> Cc: Thomas Gleixner <tglx@linutronix.de>
> Cc: Ingo Molnar <mingo@kernel.org>
> Cc: Borislav Petkov <bp@alien8.de>
> Cc: Dave Hansen <dave.hansen@linux.intel.com>
> Cc: "H. Peter Anvin" <hpa@zytor.com>
> ---
>  arch/x86/include/asm/segment.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
> index f59ae7186940..9f5be2bbd291 100644
> --- a/arch/x86/include/asm/segment.h
> +++ b/arch/x86/include/asm/segment.h
> @@ -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__ */
> --
> 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,

Tested-by: Michael Kelley <mhklinux@outlook.com>

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 1/3] x86: Use MOVL when reading segment registers
  2025-11-21  1:03 ` [PATCH 1/3] x86: Use MOVL when reading segment registers Michael Kelley
@ 2025-11-21  8:09   ` Uros Bizjak
  0 siblings, 0 replies; 7+ messages in thread
From: Uros Bizjak @ 2025-11-21  8:09 UTC (permalink / raw)
  To: Michael Kelley
  Cc: linux-hyperv@vger.kernel.org, x86@kernel.org,
	linux-kernel@vger.kernel.org, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, H. Peter Anvin

On Fri, Nov 21, 2025 at 2:03 AM Michael Kelley <mhklinux@outlook.com> wrote:
>
> From: Uros Bizjak <ubizjak@gmail.com>
> >
> > 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.
> >
> > Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
> > Cc: Thomas Gleixner <tglx@linutronix.de>
> > Cc: Ingo Molnar <mingo@kernel.org>
> > Cc: Borislav Petkov <bp@alien8.de>
> > Cc: Dave Hansen <dave.hansen@linux.intel.com>
> > Cc: "H. Peter Anvin" <hpa@zytor.com>
> > ---
> >  arch/x86/include/asm/segment.h | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/arch/x86/include/asm/segment.h b/arch/x86/include/asm/segment.h
> > index f59ae7186940..9f5be2bbd291 100644
> > --- a/arch/x86/include/asm/segment.h
> > +++ b/arch/x86/include/asm/segment.h
> > @@ -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__ */
> > --
> > 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,
>
> Tested-by: Michael Kelley <mhklinux@outlook.com>
>
> 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,

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.

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2025-11-21  8:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-11-20 13:40 [PATCH 1/3] x86: Use MOVL when reading segment registers Uros Bizjak
2025-11-20 13:40 ` [PATCH 2/3] x86/hyperv: Use savesegment() instead of inline asm() to save " Uros Bizjak
2025-11-20 16:29   ` Wei Liu
2025-11-20 13:40 ` [PATCH 3/3] x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn Uros Bizjak
2025-11-20 16:31   ` Wei Liu
2025-11-21  1:03 ` [PATCH 1/3] x86: Use MOVL when reading segment registers Michael Kelley
2025-11-21  8:09   ` Uros Bizjak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).