Skip to contentSkip to conversation

x86/vmx: Use asm goto() in _vmx_cpu_up()

RFC PATCH Superseded Andrew Cooper 2 participants 8 messages Apr 1, 2025 – Apr 2, 2025

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 x86/vmx: Use asm goto() in _vmx_cpu_up()

With the new toolchain baseline, we can make use of asm goto() in certain places, and the VMXON invocation is one example.

This removes the logic to set up rc (including a fixup section where bactraces have no connection to the invoking function), the logic to decode it, and the default case which was dead but in a way the compiler couldn't prove previously.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: [email protected]

RFC. To be rebased over Denis' general cleanup.

In principle, we can split fail into fail_valid and fail_invalid, allowing us to spot the VMfail("VMXON executed in VMX root operation") case from the pseduocode. However, getting that involves a VMREAD of VM_INSTRUCTION_ERROR, and error handling in case there isn't a loaded VMCS, so I think the complexity is unwarranted in this case.

Bloat-o-meter:
add/remove: 0/0 grow/shrink: 1/1 up/down: 13/-32 (-19)
Function old new delta

  _vmx_cpu_up.cold                            2460    2473     +13
  _vmx_cpu_up                                 1803    1771     -32

The if ( 0 ) isn't terribly nice, but it's the least bad option I could come up with. It does allow the structure of the switch() to remain largely intact.

---
 xen/arch/x86/hvm/vmx/vmcs.c            | 21 ++++++++++++---------
 xen/arch/x86/include/asm/hvm/vmx/vmx.h | 19 -------------------
 2 files changed, 12 insertions(+), 28 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +12 -9
index 0ba65becec1e..98f56b636fb3 100644
@@ -749,9 +749,16 @@ static int _vmx_cpu_up(bool bsp)
     if ( bsp && (rc = vmx_cpu_up_prepare(cpu)) != 0 )
         return rc;
 
-    switch ( __vmxon(this_cpu(vmxon_region)) )
+    asm goto ( "1: vmxon %[addr]\n\t"
+               "jbe %l[fail]\n\t"
+               _ASM_EXTABLE(1b, %l[fault])
+               :
+               : [addr] "m" (this_cpu(vmxon_region))
+               :
+               : fail, fault );
+    if ( 0 ) /* asm goto error paths */
     {
-    case -2: /* #UD or #GP */
+    fault:
         if ( bios_locked &&
              test_bit(X86_FEATURE_SMX, &boot_cpu_data.x86_capability) &&
              (!(eax & IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX) ||
@@ -763,17 +770,13 @@ static int _vmx_cpu_up(bool bsp)
                    "bootloader.\n");
             return -EINVAL;
         }
-        /* fall through */
-    case -1: /* CF==1 or ZF==1 */
+    fail:
         printk("CPU%d: unexpected VMXON failure\n", cpu);
         return -EINVAL;
-    case 0: /* success */
-        this_cpu(vmxon) = 1;
-        break;
-    default:
-        BUG();
     }
 
+    this_cpu(vmxon) = 1;
+
     hvm_asid_init(cpu_has_vmx_vpid ? (1u << VMCS_VPID_WIDTH) : 0);
 
     if ( cpu_has_vmx_ept )
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -19
index 843f8591b9cf..7c6ba7340744 100644
@@ -559,25 +559,6 @@ static inline void __vmxoff(void)
         : : : "memory" );
 }
 
-static inline int __vmxon(u64 addr)
-{
-    int rc;
-
-    asm volatile ( 
-        "1: " VMXON_OPCODE MODRM_EAX_06 "\n"
-        "   setna %b0 ; neg %0\n" /* CF==1 or ZF==1 --> rc = -1 */
-        "2:\n"
-        ".section .fixup,\"ax\"\n"
-        "3: sub $2,%0 ; jmp 2b\n"    /* #UD or #GP --> rc = -2 */
-        ".previous\n"
-        _ASM_EXTABLE(1b, 3b)
-        : "=q" (rc)
-        : "0" (0), "a" (&addr)
-        : "memory");
-
-    return rc;
-}
-
 int cf_check vmx_guest_x86_mode(struct vcpu *v);
 unsigned int vmx_get_cpl(void);
 

base-commit: cff389bca78885447c8cfa381e058c6fb983df9c

Signature · 1 line
2.39.5
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Andrew Cooper

With the new toolchain baseline, we can make use of asm goto() in certain places, and the VMXON invocation is one example.

This removes the logic to set up rc (including a fixup section where bactraces have no connection to the invoking function), the logic to decode it, and the default case which was dead but in a way the compiler couldn't prove previously.

No functional change.

Quoted 7 lines

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: [email protected]

RFC. To be rebased over Denis' general cleanup.

LGTM. Can't this actually replace some of his cleanup? Judging from
base-commit: at the bottom this isn't based on his work. In which case:

Quoted 5 lines

In principle, we can split fail into fail_valid and fail_invalid, allowing us to spot the VMfail("VMXON executed in VMX root operation") case from the pseduocode. However, getting that involves a VMREAD of VM_INSTRUCTION_ERROR, and error handling in case there isn't a loaded VMCS, so I think the complexity is unwarranted in this case.

+1

Quoted 9 lines

Bloat-o-meter:
add/remove: 0/0 grow/shrink: 1/1 up/down: 13/-32 (-19)
Function old new delta

  _vmx_cpu_up.cold                            2460    2473     +13
  _vmx_cpu_up                                 1803    1771     -32

The if ( 0 ) isn't terribly nice, but it's the least bad option I could come up with. It does allow the structure of the switch() to remain largely intact.

For the purpose of the diff here I agree. In a subsequent change we could then still move the whole blob to the end of the function. Especially if some of the static analysis tools didn't like the "if ( 0 )".

Jan

#2 lore raw
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper

With the new toolchain baseline, we can make use of asm goto() in certain places, and the VMXON invocation is one example.

This removes the logic to set up rc (including a fixup section where bactraces have no connection to the invoking function), the logic to decode it, and the default case which was dead but in a way the compiler couldn't prove previously.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: [email protected]

Quoted 4 lines
Quoted 1 line

RFC. To be rebased over Denis' general cleanup.

LGTM. Can't this actually replace some of his cleanup? Judging from
base-commit: at the bottom this isn't based on his work. In which case:

Oh.  I was expecting there to be far more debate on this.

In which case I expect it will be easiest for this patch to go in first, and supersede Denis' cleanup to __vmxon(), so as not to rework it twice in quick succession.  (Sorry.)

Quoted 14 lines
Quoted 6 lines

In principle, we can split fail into fail_valid and fail_invalid, allowing us to spot the VMfail("VMXON executed in VMX root operation") case from the pseduocode. However, getting that involves a VMREAD of VM_INSTRUCTION_ERROR, and error handling in case there isn't a loaded VMCS, so I think the complexity is unwarranted in this case.

+1

Quoted 6 lines

Bloat-o-meter:
add/remove: 0/0 grow/shrink: 1/1 up/down: 13/-32 (-19)
Function old new delta

  _vmx_cpu_up.cold                            2460    2473     +13
  _vmx_cpu_up                                 1803    1771     -32
Quoted 6 lines
Quoted 3 lines

The if ( 0 ) isn't terribly nice, but it's the least bad option I could come up with. It does allow the structure of the switch() to remain largely intact.

For the purpose of the diff here I agree. In a subsequent change we could then still move the whole blob to the end of the function. Especially if some of the static analysis tools didn't like the "if ( 0 )".

Actually, doing that is still a nice diff to read.  I think I'll take this approach.  I'll send out a v2 in just a moment.

~Andrew

#3 lore raw
Quoted 18 lines · Andrew Cooper
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper

With the new toolchain baseline, we can make use of asm goto() in certain places, and the VMXON invocation is one example.

This removes the logic to set up rc (including a fixup section where bactraces have no connection to the invoking function), the logic to decode it, and the default case which was dead but in a way the compiler couldn't prove previously.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: [email protected]

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

RFC. To be rebased over Denis' general cleanup.

LGTM. Can't this actually replace some of his cleanup? Judging from
base-commit: at the bottom this isn't based on his work. In which case:

Oh.  I was expecting there to be far more debate on this.

You had indicated many times that you want to make use of asm goto(), and for situation like the one here I'm actually in trouble seeing any objections you might have been expecting. Were you maybe expecting me to object just for the purpose of objecting ;-) ?

Jan

#4 lore raw

PATCH v2 1/1 x86/vmx: Use asm goto() in _vmx_cpu_up()

In reply to Andrew Cooper

With the new toolchain baseline, we can make use of asm goto() in certain places, and the VMXON invocation is one example.

This removes the logic to set up rc (including a fixup section where bactraces have no connection to the invoking function), the logic to decode it, and the default case which was dead but in a way the compiler couldn't prove previously.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: [email protected]

v2:
* Move error block to the end of the function.

Bloat-o-meter:
add/remove: 0/0 grow/shrink: 1/1 up/down: 13/-32 (-19)
Function old new delta

  _vmx_cpu_up.cold                            2460    2473     +13
  _vmx_cpu_up                                 1803    1771     -32
---
 xen/arch/x86/hvm/vmx/vmcs.c            | 50 +++++++++++++-------------
 xen/arch/x86/include/asm/hvm/vmx/vmx.h | 19 ----------
 2 files changed, 26 insertions(+), 43 deletions(-)
xen/arch/x86/hvm/vmx/vmcs.c +26 -24
index 0ba65becec1e..8e99e6f73062 100644
@@ -749,30 +749,15 @@ static int _vmx_cpu_up(bool bsp)
     if ( bsp && (rc = vmx_cpu_up_prepare(cpu)) != 0 )
         return rc;
 
-    switch ( __vmxon(this_cpu(vmxon_region)) )
-    {
-    case -2: /* #UD or #GP */
-        if ( bios_locked &&
-             test_bit(X86_FEATURE_SMX, &boot_cpu_data.x86_capability) &&
-             (!(eax & IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX) ||
-              !(eax & IA32_FEATURE_CONTROL_ENABLE_VMXON_INSIDE_SMX)) )
-        {
-            printk("CPU%d: VMXON failed: perhaps because of TXT settings "
-                   "in your BIOS configuration?\n", cpu);
-            printk(" --> Disable TXT in your BIOS unless using a secure "
-                   "bootloader.\n");
-            return -EINVAL;
-        }
-        /* fall through */
-    case -1: /* CF==1 or ZF==1 */
-        printk("CPU%d: unexpected VMXON failure\n", cpu);
-        return -EINVAL;
-    case 0: /* success */
-        this_cpu(vmxon) = 1;
-        break;
-    default:
-        BUG();
-    }
+    asm goto ( "1: vmxon %[addr]\n\t"
+               "jbe %l[vmxon_fail]\n\t"
+               _ASM_EXTABLE(1b, %l[vmxon_fault])
+               :
+               : [addr] "m" (this_cpu(vmxon_region))
+               :
+               : vmxon_fail, vmxon_fault );
+
+    this_cpu(vmxon) = 1;
 
     hvm_asid_init(cpu_has_vmx_vpid ? (1u << VMCS_VPID_WIDTH) : 0);
 
@@ -785,6 +770,23 @@ static int _vmx_cpu_up(bool bsp)
     vmx_pi_per_cpu_init(cpu);
 
     return 0;
+
+ vmxon_fault:
+    if ( bios_locked &&
+         test_bit(X86_FEATURE_SMX, &boot_cpu_data.x86_capability) &&
+         (!(eax & IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX) ||
+          !(eax & IA32_FEATURE_CONTROL_ENABLE_VMXON_INSIDE_SMX)) )
+    {
+        printk("CPU%d: VMXON failed: perhaps because of TXT settings "
+               "in your BIOS configuration?\n", cpu);
+        printk(" --> Disable TXT in your BIOS unless using a secure "
+               "bootloader.\n");
+        return -EINVAL;
+    }
+
+ vmxon_fail:
+    printk("CPU%d: unexpected VMXON failure\n", cpu);
+    return -EINVAL;
 }
 
 int cf_check vmx_cpu_up(void)
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -19
index 843f8591b9cf..7c6ba7340744 100644
@@ -559,25 +559,6 @@ static inline void __vmxoff(void)
         : : : "memory" );
 }
 
-static inline int __vmxon(u64 addr)
-{
-    int rc;
-
-    asm volatile ( 
-        "1: " VMXON_OPCODE MODRM_EAX_06 "\n"
-        "   setna %b0 ; neg %0\n" /* CF==1 or ZF==1 --> rc = -1 */
-        "2:\n"
-        ".section .fixup,\"ax\"\n"
-        "3: sub $2,%0 ; jmp 2b\n"    /* #UD or #GP --> rc = -2 */
-        ".previous\n"
-        _ASM_EXTABLE(1b, 3b)
-        : "=q" (rc)
-        : "0" (0), "a" (&addr)
-        : "memory");
-
-    return rc;
-}
-
 int cf_check vmx_guest_x86_mode(struct vcpu *v);
 unsigned int vmx_get_cpl(void);
 
Signature · 1 line
2.39.5
Reviewed-by on v2 1/1
#5 lore raw
Quoted 27 lines · Andrew Cooper
xen/arch/x86/hvm/vmx/vmcs.c +0 -21
@@ -749,30 +749,15 @@ static int _vmx_cpu_up(bool bsp)
     if ( bsp && (rc = vmx_cpu_up_prepare(cpu)) != 0 )
         return rc;
 
-    switch ( __vmxon(this_cpu(vmxon_region)) )
-    {
-    case -2: /* #UD or #GP */
-        if ( bios_locked &&
-             test_bit(X86_FEATURE_SMX, &boot_cpu_data.x86_capability) &&
-             (!(eax & IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX) ||
-              !(eax & IA32_FEATURE_CONTROL_ENABLE_VMXON_INSIDE_SMX)) )
-        {
-            printk("CPU%d: VMXON failed: perhaps because of TXT settings "
-                   "in your BIOS configuration?\n", cpu);
-            printk(" --> Disable TXT in your BIOS unless using a secure "
-                   "bootloader.\n");
-            return -EINVAL;
-        }
-        /* fall through */
-    case -1: /* CF==1 or ZF==1 */
-        printk("CPU%d: unexpected VMXON failure\n", cpu);
-        return -EINVAL;
-    case 0: /* success */
-        this_cpu(vmxon) = 1;
-        break;

xen/arch/x86/hvm/vmx/vmcs.c static int _vmx_cpu_up(bool bsp) · line 758

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +7 -3
-    default:
-        BUG();
-    }
+    asm goto ( "1: vmxon %[addr]\n\t"
+               "jbe %l[vmxon_fail]\n\t"
+               _ASM_EXTABLE(1b, %l[vmxon_fault])
+               :
+               : [addr] "m" (this_cpu(vmxon_region))
+               :
+               : vmxon_fail, vmxon_fault );

I must have overlooked this in the RFC - you're losing ...

Quoted 11 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -5
@@ -559,25 +559,6 @@ static inline void __vmxoff(void)
         : : : "memory" );
 }
 
-static inline int __vmxon(u64 addr)
-{
-    int rc;
-
-    asm volatile ( 

xen/arch/x86/include/asm/hvm/vmx/vmx.h static inline void __vmxoff(void) · line 576

Quoted 10 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -10
-        "1: " VMXON_OPCODE MODRM_EAX_06 "\n"
-        "   setna %b0 ; neg %0\n" /* CF==1 or ZF==1 --> rc = -1 */
-        "2:\n"
-        ".section .fixup,\"ax\"\n"
-        "3: sub $2,%0 ; jmp 2b\n"    /* #UD or #GP --> rc = -2 */
-        ".previous\n"
-        _ASM_EXTABLE(1b, 3b)
-        : "=q" (rc)
-        : "0" (0), "a" (&addr)
-        : "memory");

... the memory barrier here. I will admit I'm not sure I see why it's there, but if the removal was deliberate, then a sentence wants saying about this in the description. With that or with it re-added:

Now that you move the printk()s, I also wouldn't mind if you pruned them
some: Un-wrap the format strings and perhaps purge the full stop.

Jan

#6 lore raw
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Andrew Cooper
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -10
@@ -559,25 +559,6 @@ static inline void __vmxoff(void)
         : : : "memory" );
 }
 
-static inline int __vmxon(u64 addr)
-{
-    int rc;
-
-    asm volatile ( 
-        "1: " VMXON_OPCODE MODRM_EAX_06 "\n"
-        "   setna %b0 ; neg %0\n" /* CF==1 or ZF==1 --> rc = -1 */
-        "2:\n"
-        ".section .fixup,\"ax\"\n"
-        "3: sub $2,%0 ; jmp 2b\n"    /* #UD or #GP --> rc = -2 */
Quoted 8 lines

xen/arch/x86/include/asm/hvm/vmx/vmx.h static inline void __vmxoff(void) · line 576

Quoted 5 lines
xen/arch/x86/include/asm/hvm/vmx/vmx.h +0 -5
-        ".previous\n"
-        _ASM_EXTABLE(1b, 3b)
-        : "=q" (rc)
-        : "0" (0), "a" (&addr)
-        : "memory");

... the memory barrier here. I will admit I'm not sure I see why it's there, but if the removal was deliberate, then a sentence wants saying about this in the description.

Hmm.  (Honestly, I wrote the asm goto from scratch, rather than copying it).

The VMXON instruction itself will modify the 4k vmxon_region.  It's an opaque region, and it might (implementation specific) become non-coherent with main memory.

However, we don't have a 4k object we could pass as a faux memory operand, nor can we use asm goto with outputs (That requires a newer toolchain, and some compiler errata workarounds, but we can do it conditionally I think).

Lets put the memory clobber back in.

Quoted 2 lines

With that or with it re-added:

Thanks.

~Andrew

#7 lore raw
In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper
xen/arch/x86/hvm/vmx/vmcs.c +1 -0
index 0ba65becec1e..8e99e6f73062 100644
@@ -785,6 +770,23 @@ static int _vmx_cpu_up(bool bsp)
     vmx_pi_per_cpu_init(cpu);
 
     return 0;
+

xen/arch/x86/hvm/vmx/vmcs.c static int _vmx_cpu_up(bool bsp) · line 783

Quoted 10 lines
xen/arch/x86/hvm/vmx/vmcs.c +10 -0
+ vmxon_fault:
+    if ( bios_locked &&
+         test_bit(X86_FEATURE_SMX, &boot_cpu_data.x86_capability) &&
+         (!(eax & IA32_FEATURE_CONTROL_ENABLE_VMXON_OUTSIDE_SMX) ||
+          !(eax & IA32_FEATURE_CONTROL_ENABLE_VMXON_INSIDE_SMX)) )
+    {
+        printk("CPU%d: VMXON failed: perhaps because of TXT settings "
+               "in your BIOS configuration?\n", cpu);
+        printk(" --> Disable TXT in your BIOS unless using a secure "
+               "bootloader.\n");

Oh, I should remove the line splits, and put in some XENLOG_*.

~Andrew