KVM: nVMX: Adjust VMPTRLD/VMPTRST behavior with active eVMCS
cover KVM: nVMX: Adjust VMPTRLD/VMPTRST behavior with active eVMCS
It was reported (https://bugzilla.kernel.org/show_bug.cgi?id=221841) that KVM's emulation of VMPTRLD/VMPTRST instructions when Enlightened VMCS is active is sub-optimal: returning '1' does not make the L1 guest skip the instruction so a hang is likely to happen. While TLFS does not really specify the expected behavior, genuine Hyper-V seems to return #UD for VMPTRLD and eVMCS pointer for VMPTRST. Alter the behavior in KVM and check it in selftests.
Note: this is not a security issue. Misbehaving L1 can only do self-harm by doing VMPTRLD/VMPTRST when it also uses eVMCS.
Vitaly Kuznetsov (4):
KVM: nVMX: Make VMPTRLD result in #UD when eVMCS is used
KVM: nVMX: Make VMPTRST return eVMCS GPA when it is used
KVM: selftests: Adapt to the updated VMPTRST behavior when eVMCS is
used
KVM: selftests: Check VMPTRLD with active eVMCS
arch/x86/kvm/vmx/nested.c | 22 +++++++++++++------ tools/testing/selftests/kvm/include/x86/vmx.h | 6 ----- .../testing/selftests/kvm/x86/hyperv_evmcs.c | 18 ++++++++++----- 3 files changed, 28 insertions(+), 18 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/4 KVM: nVMX: Make VMPTRLD result in #UD when eVMCS is used
VMPTRLD with active eVMCS is already forbidden, however, returning 1 without skipping the instruction or queuing an exception will likely result in L1 getting stuck. Genuine Hyper-V seems to inject #UD under similar circumstances, mimic the behavior in KVM.
--- arch/x86/kvm/vmx/nested.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/nested.c +6 -4
@@ -5879,6 +5879,12 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)if (!nested_vmx_check_permission (vcpu))return 1 ; +/* Forbid normal VMPTRLD if Enlightened version was used */ +if (nested_vmx_is_evmptr12_valid (vmx)) { +kvm_queue_exception (vcpu,UD_VECTOR ); +return 1 ; + } +if (nested_vmx_get_vmptr (vcpu, &vmptr, &r))return r;
@@ -5888,10 +5894,6 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)if (vmptr == vmx->nested.vmxon_ptr)return nested_vmx_fail (vcpu,VMXERR_VMPTRLD_VMXON_POINTER ); -/* Forbid normal VMPTRLD if Enlightened version was used */ -if (nested_vmx_is_evmptr12_valid (vmx)) -return 1 ; -if (vmx->nested.current_vmptr != vmptr) {struct gfn_to_hva_cache *ghc = &vmx->nested.vmcs12_cache;struct vmcs_hdr hdr;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 1/4] KVM: nVMX: Make VMPTRLD result in #UD when eVMCS is used
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch fixes an infinite VM-exit loop in handle_vmptrld by injecting a #UD exception when eVMCS is active, but misses the identical pre-existing bug in its sibling function handle_vmptrst. This problem wasn't introduced by this patch, but represents an incomplete fix for the class of bugs described in the commit message. --
--- Patch [1]: [PATCH 1/4] KVM: nVMX: Make VMPTRLD result in #UD when eVMCS is used --- commit 34f5c31113003a4163d028b0ce4bfea669ca97f5
Author: Vitaly Kuznetsov <[email protected]>
KVM: nVMX: Make VMPTRLD result in #UD when eVMCS is used
This commit updates the VMPTRLD emulation to inject a #UD exception when eVMCS is active, rather than returning 1 without skipping the instruction. This resolves an issue where returning 1 without advancing the instruction pointer would cause the L1 guest to get stuck in an infinite VM-exit loop.
Quoted 5 lines
arch/x86/kvm/vmx/nested.c +0
-0
arch/x86/kvm/vmx/nested.c static int handle_vmptrld(struct kvm_vcpu *vcpu) · line 5891
Quoted 9 lines
arch/x86/kvm/vmx/nested.c +6
-0@@ -5883,6 +5883,12 @@ static int handle_vmptrld(struct kvm_vcpu *vcpu)if (!nested_vmx_check_permission (vcpu))return 1 ; +/* Forbid normal VMPTRLD if Enlightened version was used */ +if (nested_vmx_is_evmptr12_valid (vmx)) { +kvm_queue_exception (vcpu,UD_VECTOR ); +return 1 ; + } +
[Severity: High]
This isn't a bug introduced by this patch, but does handle_vmptrst() need the same fix?
Looking at handle_vmptrst() further down in arch/x86/kvm/vmx/nested.c, it appears to have the identical pattern that this patch fixes for VMPTRLD:
if (unlikely(nested_vmx_is_evmptr12_valid(to_vmx(vcpu))))
return 1;Could this unhandled return cause the same infinite VM-exit loop if a guest executes VMPTRST while eVMCS is active?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Sean Christopherson
maintainerRe: [PATCH 1/4] KVM: nVMX: Make VMPTRLD result in #UD when eVMCS is used
Quoted 3 lines · On Fri, Aug 14, 2026, [email protected] wrote:
[Severity: High]
This isn't a bug introduced by this patch, but doeshandle_vmptrst()need the same fix?
LOL, one of these days Sashiko will look at the next patch before responding.
PATCH 2/4 KVM: nVMX: Make VMPTRST return eVMCS GPA when it is used
In reply to Vitaly KuznetsovVMPTRST with active eVMCS is currently forbidden, however, returning 1 without skipping the instruction will likely result in L1 getting stuck. While TLFS does not specify the expected behavior, genuine Hyper-V seems to be returning eVMCS GPA. Implement the same behavior in KVM.
--- arch/x86/kvm/vmx/nested.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/nested.c +9 -3
@@ -5946,7 +5946,7 @@ static int handle_vmptrst(struct kvm_vcpu *vcpu) {unsigned long exit_qual =vmx_get_exit_qual (vcpu);u32 instr_info =vmcs_read32 (VMX_INSTRUCTION_INFO ); -gpa_t current_vmptr =to_vmx (vcpu)->nested.current_vmptr; +gpa_t current_vmptr;struct x86_exception e;gva_t gva;int r;
@@ -5954,8 +5954,14 @@ static int handle_vmptrst(struct kvm_vcpu *vcpu)if (!nested_vmx_check_permission (vcpu))return 1 ; -if (unlikely (nested_vmx_is_evmptr12_valid (to_vmx (vcpu)))) -return 1 ; +/* +* Hyper-V TLFS does not specify the behavior of VMPTRST when eVMCS is used +* but genuine Hyper-V seems to be returning eVMCS GPA. +*/ +if (nested_vmx_is_evmptr12_valid (to_vmx (vcpu))) + current_vmptr =to_vmx (vcpu)->nested.hv_evmcs_vmptr; +else + current_vmptr =to_vmx (vcpu)->nested.current_vmptr;if (get_vmx_mem_address (vcpu, exit_qual, instr_info, true,sizeof (gpa_t), &gva))
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 2/4] KVM: nVMX: Make VMPTRST return eVMCS GPA when it is used
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Compilation failure when CONFIG_KVM_HYPERV is disabled. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Compilation failure when CONFIG_KVM_HYPERV is disabled.
--
--- Patch [2]: [PATCH 2/4] KVM: nVMX: Make VMPTRST return eVMCS GPA when it is used --- commit e9f713cf2125ef734863ce5fc9c30ea6f602ede7
Author: Vitaly Kuznetsov <[email protected]>
KVM: nVMX: Make VMPTRST return eVMCS GPA when it is used
This commit changes the behavior of VMPTRST to return the eVMCS GPA when eVMCS is active. This aligns KVM's behavior with genuine Hyper-V instead of returning 1 without skipping the instruction.
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +0
-0
[ ... ]
arch/x86/kvm/vmx/nested.c static int handle_vmptrst(struct kvm_vcpu *vcpu) · line 5966
Quoted 12 lines
arch/x86/kvm/vmx/nested.c +6
-2@@ -5958,8 +5958,14 @@ static int handle_vmptrst(struct kvm_vcpu *vcpu)if (!nested_vmx_check_permission (vcpu))return 1 ; -if (unlikely (nested_vmx_is_evmptr12_valid (to_vmx (vcpu)))) -return 1 ; +/* +* Hyper-V TLFS does not specify the behavior of VMPTRST when eVMCS is used +* but genuine Hyper-V seems to be returning eVMCS GPA. +*/ +if (nested_vmx_is_evmptr12_valid (to_vmx (vcpu))) + current_vmptr =to_vmx (vcpu)->nested.hv_evmcs_vmptr;
[Severity: Low]
Will this cause a build failure when CONFIG_KVM_HYPERV is disabled?
Because nested_vmx_is_evmptr12_valid() is a static inline function rather than a preprocessor macro, the compiler still evaluates and type-checks this branch even when the helper is hardcoded to return false. If the hv_evmcs_vmptr member in struct nested_vmx is conditionally compiled based on CONFIG_KVM_HYPERV, the compiler will fail to find it here.
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +2
-0+else + current_vmptr =to_vmx (vcpu)->nested.current_vmptr; if (get_vmx_mem_address(vcpu, exit_qual, instr_info,
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Sean Christopherson
maintainerRe: [PATCH 2/4] KVM: nVMX: Make VMPTRST return eVMCS GPA when it is used
Quoted 21 lines · On Fri, Aug 14, 2026, [email protected] wrote:
Quoted 4 lines
arch/x86/kvm/vmx/nested.c +0
-0[ ... ]
arch/x86/kvm/vmx/nested.c static int handle_vmptrst(struct kvm_vcpu *vcpu) · line 5966
Quoted 12 lines
arch/x86/kvm/vmx/nested.c +6
-2@@ -5958,8 +5958,14 @@ static int handle_vmptrst(struct kvm_vcpu *vcpu)if (!nested_vmx_check_permission (vcpu))return 1 ; -if (unlikely (nested_vmx_is_evmptr12_valid (to_vmx (vcpu)))) -return 1 ; +/* +* Hyper-V TLFS does not specify the behavior of VMPTRST when eVMCS is used +* but genuine Hyper-V seems to be returning eVMCS GPA. +*/ +if (nested_vmx_is_evmptr12_valid (to_vmx (vcpu))) + current_vmptr =to_vmx (vcpu)->nested.hv_evmcs_vmptr;[Severity: Low]
Will this cause a build failure whenCONFIG_KVM_HYPERVis disabled?
Quoted 5 lines
Because
nested_vmx_is_evmptr12_valid()is a static inline function rather than a preprocessor macro, the compiler still evaluates and type-checks this branch even when the helper is hardcoded to return false. If thehv_evmcs_vmptrmember in structnested_vmxis conditionally compiled based onCONFIG_KVM_HYPERV, the compiler will fail to find it here.
Yep.
arch/x86/kvm/vmx/nested.c: In function ‘handle_vmptrst’:arch/x86/kvm/vmx/nested.c:5971:53: error: ‘struct nested_vmx’ has no member named ‘hv_evmcs_vmptr’
5971 | current_vmptr = to_vmx(vcpu)->nested.hv_evmcs_vmptr;
| ^
PATCH 3/4 KVM: selftests: Adapt to the updated VMPTRST behavior when eVMCS is used
In reply to Vitaly KuznetsovPreviously, VMPTRST was forbidden with eVMCS and selftests were mocking the correct behavior in vmptrst() by returning enlightened vmptr directly. Since KVM's behavior has changed to match genuine Hyper-V, adjust evmcs test accordingly.
--- tools/testing/selftests/kvm/include/x86/vmx.h | 3 --- tools/testing/selftests/kvm/x86/hyperv_evmcs.c | 5 ++--- 2 files changed, 2 insertions(+), 6 deletions(-)
tools/testing/selftests/kvm/include/x86/vmx.h +0 -3
@@ -341,9 +341,6 @@ static inline int vmptrst(u64 *value)u64 tmp;u8 ret; -if (enable_evmcs) -return evmcs_vmptrst (value); - __asm__ __volatile__("vmptrst %[value]; setna %[ret]" : [value]"=m" (tmp), [ret]"=rm" (ret) : :"cc" ,"memory" );
tools/testing/selftests/kvm/x86/hyperv_evmcs.c +2 -3
@@ -95,16 +95,15 @@ void guest_code(struct vmx_pages *vmx_pages, struct hyperv_test_pages *hv_pages,GUEST_ASSERT (prepare_for_vmx_operation (vmx_pages));GUEST_SYNC (3 );GUEST_ASSERT (load_evmcs (hv_pages)); -GUEST_ASSERT (vmptrstz () == hv_pages->enlightened_vmcs_gpa); +/* VMPTRST returns -1 until VMLAUNCH with eVMCS ptr set */ +GUEST_ASSERT (vmptrstz () ==-1 );GUEST_SYNC (4 ); -GUEST_ASSERT (vmptrstz () == hv_pages->enlightened_vmcs_gpa);prepare_vmcs (vmx_pages, l2_guest_code, &l2_guest_stack[L2_GUEST_STACK_SIZE ]);GUEST_SYNC (5 ); -GUEST_ASSERT (vmptrstz () == hv_pages->enlightened_vmcs_gpa); current_evmcs->revision_id =-1u ;GUEST_ASSERT (vmlaunch ()); current_evmcs->revision_id =EVMCS_VERSION ;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 3/4] KVM: selftests: Adapt to the updated VMPTRST behavior when eVMCS is used
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] The helper function `evmcs_vmptrst` became completely unused after this patch removed its only…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The helper function evmcs_vmptrst became completely unused after this patch removed its only caller in vmptrst(), but its definition was left behind. --
--- Patch [3]: [PATCH 3/4] KVM: selftests: Adapt to the updated VMPTRST behavior when eVMCS is used --- commit 18309c20d40234d7991916df5f035584459347c3
Author: Vitaly Kuznetsov <[email protected]>
KVM: selftests: Adapt to the updated VMPTRST behavior when eVMCS is used
This commit updates selftests to stop mocking VMPTRST behavior with eVMCS, adjusting them to match genuine Hyper-V behavior. It removes the eVMCS-specific branch from vmptrst() and modifies the eVMCS test expectations.
tools/testing/selftests/kvm/include/x86/vmx.h static inline int vmptrst(u64 *value) · line 346
Quoted 10 lines
tools/testing/selftests/kvm/include/x86/vmx.h +0
-3@@ -341,9 +341,6 @@ static inline int vmptrst(u64 *value)u64 tmp;u8 ret; -if (enable_evmcs) -return evmcs_vmptrst (value); -
[Severity: Low]
Does this leave evmcs_vmptrst() as unused dead code?
Since this was the only caller, the definition in tools/testing/selftests/kvm/include/x86/evmcs.h is left behind:
static inline int evmcs_vmptrst(u64 *value)
{
*value = current_vp_assist->current_nested_vmcs &
~HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
return 0;
}Could this definition be removed as well to keep the header clean?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH 4/4 KVM: selftests: Check VMPTRLD with active eVMCS
In reply to Vitaly KuznetsovCheck that VMPTRLD when eVMCS is active results in #UD. This matches genuine Hyper-V's behavior.
--- tools/testing/selftests/kvm/include/x86/vmx.h | 3 --- tools/testing/selftests/kvm/x86/hyperv_evmcs.c | 13 +++++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/include/x86/vmx.h +0 -3
@@ -325,9 +325,6 @@ static inline int vmptrld(u64 vmcs_pa) {u8 ret; -if (enable_evmcs) -return -1 ; - __asm__ __volatile__ ("vmptrld %[pa]; setna %[ret]" : [ret]"=rm" (ret) : [pa]"m"(vmcs_pa)
tools/testing/selftests/kvm/x86/hyperv_evmcs.c +11 -2
@@ -18,12 +18,16 @@#include "hyperv.h" #include "vmx.h" +#define VMLAUNCH_INSN_SIZE 3 +#define VMPTRLD_INSN_SIZE 5 +static int ud_count; +static int ud_insn_skip_size;static void guest_ud_handler (struct ex_regs *regs) { ud_count++; - regs->rip +=3 ;/* VMLAUNCH */ + regs->rip += ud_insn_skip_size; }static void guest_nmi_handler (struct ex_regs *regs)
@@ -182,10 +186,15 @@ void guest_code(struct vmx_pages *vmx_pages, struct hyperv_test_pages *hv_pages,GUEST_ASSERT (vmreadz (VM_EXIT_REASON ) ==EXIT_REASON_VMCALL );GUEST_SYNC (11 ); + ud_insn_skip_size =VMPTRLD_INSN_SIZE ; +vmptrld (hv_pages->enlightened_vmcs_gpa); +GUEST_ASSERT (ud_count ==1 ); +/* Try enlightened vmptrld with an incorrect GPA */ + ud_insn_skip_size =VMLAUNCH_INSN_SIZE ;evmcs_vmptrld (0xdeadbeef , hv_pages->enlightened_vmcs);GUEST_ASSERT (vmlaunch ()); -GUEST_ASSERT (ud_count ==1 ); +GUEST_ASSERT (ud_count ==2 );GUEST_DONE (); }
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 4/4] KVM: selftests: Check VMPTRLD with active eVMCS
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The hardcoded `VMPTRLD_INSN_SIZE` of 5 bytes is brittle because `vmptrld` is executed using an…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The hardcoded VMPTRLD_INSN_SIZE of 5 bytes is brittle because vmptrld is executed using an inline assembly memory operand constraint ("m"), which results in a variable-sized instruction depending on compiler optimizations and register allocation. --
--- Patch [4]: [PATCH 4/4] KVM: selftests: Check VMPTRLD with active eVMCS --- commit 80c84d1f71bebfd26712329b4fe8a487216ec3fb
Author: Vitaly Kuznetsov <[email protected]>
KVM: selftests: Check VMPTRLD with active eVMCS
Check that VMPTRLD when eVMCS is active results in #UD. This matches genuine Hyper-V's behavior.
Quoted 20 lines
tools/testing/selftests/kvm/x86/hyperv_evmcs.c +5
-1@@ -18,12 +18,16 @@#include "hyperv.h" #include "vmx.h" +#define VMLAUNCH_INSN_SIZE 3 +#define VMPTRLD_INSN_SIZE 5 +static int ud_count; +static int ud_insn_skip_size;static void guest_ud_handler (struct ex_regs *regs) { ud_count++; - regs->rip +=3 ;/* VMLAUNCH */ + regs->rip += ud_insn_skip_size; }
[ ... ]
tools/testing/selftests/kvm/x86/hyperv_evmcs.c void guest_code(struct vmx_pages *vmx_pages, struct hyperv_test_pages *hv_pages, · line 190
Quoted 6 lines
tools/testing/selftests/kvm/x86/hyperv_evmcs.c +2
-0@@ -182,10 +186,15 @@ void guest_code(struct vmx_pages *vmx_pages, struct hyperv_test_pages *hv_pages,GUEST_ASSERT (vmreadz (VM_EXIT_REASON ) ==EXIT_REASON_VMCALL );GUEST_SYNC (11 ); + ud_insn_skip_size =VMPTRLD_INSN_SIZE ; +vmptrld (hv_pages->enlightened_vmcs_gpa);
[Severity: Medium]
Could this hardcoded instruction size lead to guest instruction stream corruption?
The vmptrld() inline assembly in include/x86/vmx.h uses a memory operand constraint ("m") for the physical address:
static inline int vmptrld(u64 vmcs_pa)
{
u8 ret;
__asm__ __volatile__ ("vmptrld %[pa]; setna %[ret]"
: [ret]"=rm"(ret)
: [pa]"m"(vmcs_pa)
: "cc", "memory");
...
}Depending on compiler optimizations and register allocation, the resulting instruction might not be exactly 5 bytes in size (for example, it could be 4 or 7 bytes depending on the chosen addressing mode).
Since guest_ud_handler() unconditionally advances regs->rip by ud_insn_skip_size, if the generated instruction length does not match VMPTRLD_INSN_SIZE, resuming the guest could land in the middle of an instruction and cause #GP faults or infinite #UD loops.
Quoted 10 lines
tools/testing/selftests/kvm/x86/hyperv_evmcs.c +4
-1+GUEST_ASSERT (ud_count ==1 ); +/* Try enlightened vmptrld with an incorrect GPA */ + ud_insn_skip_size =VMLAUNCH_INSN_SIZE ;evmcs_vmptrld (0xdeadbeef , hv_pages->enlightened_vmcs);GUEST_ASSERT (vmlaunch ()); -GUEST_ASSERT (ud_count ==1 ); +GUEST_ASSERT (ud_count ==2 );GUEST_DONE (); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH 4/4] KVM: selftests: Check VMPTRLD with active eVMCS
Quoted 5 lines · [email protected] writes:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The hardcodedVMPTRLD_INSN_SIZEof 5 bytes is brittle
becausevmptrldis executed using an inline assembly memory operand constraint ("m"), which results in a variable-sized instruction
depending on compiler optimizations and register allocation.
True that, but afair we don't currently have an instruction decoder in selftests. I see three ways to move forward:
- Calculate the size of the instruction with two labels, something like (untested):
static inline int __vmptrld(u64 vmcs_pa, int *insn_size)
{
u8 ret;
__asm__ __volatile__ ("movl $(2f-1f), %[size];"
"1: vmptrld %[pa]; 2:"
"setna %[ret]"
: [ret]"=rm"(ret), [size]"=m"(*insn_size)
: [pa]"m"(vmcs_pa)
: "cc", "memory");
return ret;
} and use insn_size in the tests.
- Stuf __vmptrld() with NOPs after vmptrld and keep the constant jump, just make it loner (e.g. 10 bytes should realistically be enough).
- Make peace with the fragility (it's just a selftest after all) and leave a comment in vmptrld() that changing the asm there make break the assumption.
Any preferences? Personally, I'm feeling adventurous and can go with the first one - calculating the exact size with labels.
Signature · 1 line
Vitaly
Sean Christopherson
maintainerRe: [PATCH 4/4] KVM: selftests: Check VMPTRLD with active eVMCS
Quoted 28 lines · Vitaly Kuznetsov
Quoted 5 lines · [email protected] writes:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The hardcodedVMPTRLD_INSN_SIZEof 5 bytes is brittle
becausevmptrldis executed using an inline assembly memory operand constraint ("m"), which results in a variable-sized instruction
depending on compiler optimizations and register allocation.True that, but afair we don't currently have an instruction decoder in selftests. I see three ways to move forward:
- Calculate the size of the instruction with two labels, something like (untested):
static inline int __vmptrld(u64 vmcs_pa, int *insn_size) { u8 ret; __asm__ __volatile__ ("movl $(2f-1f), %[size];" "1: vmptrld %[pa]; 2:" "setna %[ret]" : [ret]"=rm"(ret), [size]"=m"(*insn_size) : [pa]"m"(vmcs_pa) : "cc", "memory"); return ret; }
Quoted 8 lines
and use
insn_sizein the tests.- Stuf
__vmptrld()with NOPs after vmptrld and keep the constant jump, just make it loner (e.g. 10 bytes should realistically be enough).- Make peace with the fragility (it's just a selftest after all) and leave a comment in
vmptrld()that changing the asm there make break the assumption.
Option D: use KVM_ASM_SAFE() to do the heavy lifting. Completely untested, but something like so:
static inline int vmptrst_safe(u64 *value)
{
u64 error_code;
u8 vector;
int ret;
asm volatile(KVM_ASM_SAFE("vmptrst %[value]")
"\n\tsetna %[ret]"
: KVM_ASM_SAFE_OUTPUTS(vector, error_code),
[value]"=m"(*value), [ret]"=rm"(ret)
:
: "cc", "memory");
return vector ? vector : ret ? -EINVAL : 0;
}Quoted 2 lines
Any preferences? Personally, I'm feeling adventurous and can go with the first one - calculating the exact size with labels.