x86: Use MOVL when reading segment registers
Subsystems (3)
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.
--- arch/x86/include/asm/segment.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/segment.h +1 -1
@@ -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
PATCH 2/3 x86/hyperv: Use savesegment() instead of inline asm() to save segment registers
Use standard savesegment() utility macro to save segment registers.
--- arch/x86/hyperv/ivm.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
arch/x86/hyperv/ivm.c +5 -4
@@ -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; - asmvolatile ("movl %%es, %%eax;" :"=a" (vmsa->es.selector)); +savesegment (es, vmsa->es.selector);hv_populate_vmcb_seg (vmsa->es, vmsa->gdtr.base); - asmvolatile ("movl %%cs, %%eax;" :"=a" (vmsa->cs.selector)); +savesegment (cs, vmsa->cs.selector);hv_populate_vmcb_seg (vmsa->cs, vmsa->gdtr.base); - asmvolatile ("movl %%ss, %%eax;" :"=a" (vmsa->ss.selector)); +savesegment (ss, vmsa->ss.selector);hv_populate_vmcb_seg (vmsa->ss, vmsa->gdtr.base); - asmvolatile ("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
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.
PATCH 3/3 x86/hyperv: Remove ASM_CALL_CONSTRAINT with VMMCALL insn
In reply to Uros BizjakUnlike CALL instruction, VMMCALL does not push to the stack, and may be inserted before the frame pointer gets set up by the containing function.
--- arch/x86/hyperv/ivm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
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; asmvolatile ("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
Wei Liu
maintainerRe: [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.
Signed-off-by Uros Bizjak <[email protected]>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@@ -392,7 +392,7 @@ u64 hv_snp_hypercall(u64 control, u64 param1, u64 param2) register u64 __r8 asm("r8") = param2; asmvolatile ("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
From: Uros Bizjak <[email protected]>
Quoted 29 lines
Use MOVL when reading segment registers to avoid
0x66operand-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 <[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/include/asm/segment.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/include/asm/segment.h +1
-2@@ -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
Quoted 55 lines · Michael Kelley
From: Uros Bizjak <[email protected]>
Quoted 29 lines
Use MOVL when reading segment registers to avoid
0x66operand-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 <[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/include/asm/segment.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/include/asm/segment.h +1
-2@@ -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,
Tested-by Michael Kelley <[email protected]>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_CONSTRAINTis 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_CONSTRAINTdoes not change the generated code forhv_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.