KVM: VMX: Introduce Intel Mode-Based Execute Control (MBEC)
Subsystems (5)
cover KVM: VMX: Introduce Intel Mode-Based Execute Control (MBEC)
## Summary
This series introduces support for Intel Mode-Based Execute Control (MBEC) to KVM and nested VMX virtualization. By exposing MBEC to L2 guests, it enables a dramatic reduction in VMexits (up to 24x) for Windows guests running with Hypervisor-Protected Code Integrity (HVCI), significantly improving virtualization performance.
## What?
Intel MBEC is a hardware feature, introduced in the Kabylake generation, that allows for more granular control over execution permissions. MBEC enables the separation and tracking of execution permissions for supervisor (kernel) and user-mode code. It is used as an accelerator for Microsoft's Memory Integrity [1] (also known as hypervisor-protected code integrity or HVCI).
## Why?
The primary reason for this feature is performance.
Without hardware-level MBEC, enabling Windows HVCI runs a 'software MBEC' known as Restricted User Mode, which imposes a runtime overhead due to increased state transitions between the guest's L2 root partition and the L2 secure partition for running kernel mode code integrity operations.
In practice, this results in a significant number of exits. For example, playing a YouTube video within the Edge Browser produces roughly 1.2 million VMexits/second across an 8 vCPU Windows 11 guest.
Most of these exits are VMREAD/VMWRITE operations, which can be emulated with Enlightened VMCS (eVMCS). However, even with eVMCS, this configuration still produces around 200,000 VMexits/second.
With MBEC exposed to the L1 Windows Hypervisor, the same scenario results in approximately 50,000 VMexits/second, a *24x* reduction from the baseline.
Not a typo, 24x reduction in VMexits.
## How?
This series implements core KVM support for exposing the MBEC bit in secondary execution controls (bit 22) to L2 nested guests, based on configuration from user space. The inspiration for this series started with Mickaël's series for Heki [3], where we've extracted, refactored, and completely reworked the MBEC-specific use case to be general-purpose.
MBEC splits the EPT execute permission into two independent bits. When secondary execution control bit 22 ("mode-based execute control for EPT") is set for the L2 guest, EPT PTE bit 2 controls execute permission for supervisor-mode linear addresses, while bit 10 controls execute permission for user-mode linear addresses.
The semantics for EPT violation qualifications also change when MBEC is enabled, with bit 5 reflecting supervisor/kernel mode execute permissions and bit 6 reflecting user mode execute permissions. This ultimately serves to expose this feature to the L1 hypervisor, which consumes MBEC and informs the L2 partitions not to use the software MBEC by removing bit 13 in 0x40000004 EAX [4].
## Where?
The implementation spans multiple components:
- KVM MMU code: Teach the shadow MMU about MBEC execution modes
- KVM VMX code: Handle EPT violations and VMX controls for MBEC
- User space VMM: Pass secondary execution control bit 22 to enable MBEC for L2 guests
A trivial enablement patch for QEMU enablement is available [5].
A GitHub mirror of this series is also available [6].
## Performance Impact
Testing shows dramatic performance improvements for Windows HVCI workloads:
- 24x reduction in VMexits for typical browser usage
- From ~1.2M VMexits/second to ~50K VMexits/second
- Enables hardware acceleration of Windows Memory Integrity
The implementation adds minimal overhead when MBEC is not used, especially when combined with EVMCS to elide nested VMREAD/VMWRITE vmexits.
## Testing
Initial testing has been on done on 6.18-based code with:
Guests
- Windows 11 24H2 26100.2894
- Windows Server 2025 24H2 26100.2894
- Windows Server 2022 W1H2 20348.825
Processors:
- Intel Skylake 6154
- Intel Sapphire Rapids 6444Y
Unit Tests
- KVM Unit Tests [7]
## Changelog
RFC -> V1:
- Fix incorrect bit reference in cover letter (Adrian-Ken)
- Remove module parameters (Sean, Amit)
- Remove redundant arch-level tracking boolean (Sean)
- Update is_present_gpte to account for MBEC bit 10 (Chao)
- Move MBEC enablement tracking to MMU role (Sean)
- Restrict MBEC advertisement to nested virtualization only (Sean)
- Consolidate preparatory patches into main implementation (Sean)
- Add permission mask refactoring preparation (Sean)
- Implement TDP-aware executable permission checking (Sean)
[1] https://learn.microsoft.com/en-us/windows/security/hardware-security/enable-virtualization-based-protection-of-code-integrity
[2] https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/tlfs/nested-virtualization#enlightened-vmcs-intel
[3] https://patchwork.kernel.org/project/kvm/patch/[email protected]/
[4] https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/tlfs/feature-discovery#implementation-recommendations---0x40000004
[5] https://github.com/JonKohler/qemu/tree/mbec-v1
[6] https://github.com/JonKohler/linux/tree/mbec-v1-6.18
[7] https://github.com/JonKohler/kvm-unit-tests/tree/mbec-v1
Jon Kohler (8):
KVM: TDX/VMX: rework EPT_VIOLATION_EXEC_FOR_RING3_LIN into PROT_MASK
KVM: x86/mmu: remove SPTE_PERM_MASK
KVM: x86/mmu: adjust MMIO generation bit allocation and allowed mask
KVM: x86/mmu: update access permissions from ACC_ALL to ACC_RWX
KVM: x86/mmu: bootstrap support for Intel MBEC
KVM: VMX: enhance EPT violation handler for MBEC
KVM: VMX: allow MBEC with EVMCS
KVM: nVMX: advertise MBEC and setup mmu has_mbec
Documentation/virt/kvm/x86/mmu.rst | 9 +++- arch/x86/include/asm/kvm_host.h | 19 +++++--- arch/x86/include/asm/vmx.h | 9 +++- arch/x86/kvm/mmu.h | 15 +++++- arch/x86/kvm/mmu/mmu.c | 74 ++++++++++++++++++++++++++-- arch/x86/kvm/mmu/mmutrace.h | 23 ++++++--- arch/x86/kvm/mmu/paging_tmpl.h | 24 ++++++--- arch/x86/kvm/mmu/spte.c | 65 +++++++++++++++++++------ arch/x86/kvm/mmu/spte.h | 78 ++++++++++++++++++++++++------ arch/x86/kvm/mmu/tdp_mmu.c | 12 +++-- arch/x86/kvm/vmx/capabilities.h | 6 +++ arch/x86/kvm/vmx/common.h | 15 ++++-- arch/x86/kvm/vmx/hyperv_evmcs.h | 1 + arch/x86/kvm/vmx/nested.c | 6 +++ arch/x86/kvm/vmx/tdx.c | 2 +- arch/x86/kvm/vmx/vmx.c | 10 +++- arch/x86/kvm/vmx/vmx.h | 1 + 17 files changed, 301 insertions(+), 68 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/8 KVM: TDX/VMX: rework EPT_VIOLATION_EXEC_FOR_RING3_LIN into PROT_MASK
EPT exit qualification bit 6 is used when mode-based execute control is enabled, and reflects user executable addresses. Rework name to reflect the intention and add to EPT_VIOLATION_PROT_MASK, which allows simplifying the return evaluation in tdx_is_sept_violation_unexpected_pending a pinch.
Rework handling in __vmx_handle_ept_violation to unconditionally clear EPT_VIOLATION_PROT_USER_EXEC until MBEC is implemented, as suggested by Sean [1].
Note: Intel SDM Table 29-7 defines bit 6 as:
If the “mode-based execute control” VM-execution control is 0, the value of this bit is undefined. If that control is 1, this bit is the logical-AND of bit 10 in the EPT paging-structure entries used to translate the guest-physical address of the access causing the EPT violation. In this case, it indicates whether the guest-physical address was executable for user-mode linear addresses.
[1] https://lore.kernel.org/all/[email protected]/
--- arch/x86/include/asm/vmx.h | 5 +++-- arch/x86/kvm/vmx/common.h | 9 +++++++-- arch/x86/kvm/vmx/tdx.c | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-)
arch/x86/include/asm/vmx.h +3 -2
@@ -596,10 +596,11 @@ enum vm_entry_failure_code {#define EPT_VIOLATION_PROT_READ BIT(3)#define EPT_VIOLATION_PROT_WRITE BIT(4)#define EPT_VIOLATION_PROT_EXEC BIT(5) -#define EPT_VIOLATION_EXEC_FOR_RING3_LIN BIT(6) +#define EPT_VIOLATION_PROT_USER_EXEC BIT(6)#define EPT_VIOLATION_PROT_MASK (EPT_VIOLATION_PROT_READ | \ EPT_VIOLATION_PROT_WRITE | \ - EPT_VIOLATION_PROT_EXEC) + EPT_VIOLATION_PROT_EXEC | \ + EPT_VIOLATION_PROT_USER_EXEC)#define EPT_VIOLATION_GVA_IS_VALID BIT(7)#define EPT_VIOLATION_GVA_TRANSLATED BIT(8)
arch/x86/kvm/vmx/common.h +7 -2
@@ -94,8 +94,13 @@ static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa,/* Is it a fetch fault? */ error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) ?PFERR_FETCH_MASK :0 ; -/* ept page table entry is present? */ - error_code |= (exit_qualification &EPT_VIOLATION_PROT_MASK ) +/* +* ept page table entry is present? +* note: unconditionally clear USER_EXEC until mode-based +* execute control is implemented +*/ + error_code |= (exit_qualification & + (EPT_VIOLATION_PROT_MASK & ~EPT_VIOLATION_PROT_USER_EXEC )) ?PFERR_PRESENT_MASK :0 ;if (exit_qualification &EPT_VIOLATION_GVA_IS_VALID )
arch/x86/kvm/vmx/tdx.c +1 -1
@@ -1922,7 +1922,7 @@ static inline bool tdx_is_sept_violation_unexpected_pending(struct kvm_vcpu *vcpif (eeq_type !=TDX_EXT_EXIT_QUAL_TYPE_PENDING_EPT_VIOLATION )return false; -return !(eq &EPT_VIOLATION_PROT_MASK ) && !(eq &EPT_VIOLATION_EXEC_FOR_RING3_LIN ); +return !(eq &EPT_VIOLATION_PROT_MASK ); }static int tdx_handle_ept_violation (struct kvm_vcpu *vcpu)
Signature · 1 line
2.43.0
PATCH 2/8 KVM: x86/mmu: remove SPTE_PERM_MASK
In reply to Jon KohlerSPTE_PERM_MASK is no longer referenced by anything in the kernel.
--- arch/x86/kvm/mmu/spte.h | 3 --- 1 file changed, 3 deletions(-)
arch/x86/kvm/mmu/spte.h +0 -3
@@ -42,9 +42,6 @@ static_assert(SPTE_TDP_AD_ENABLED == 0);#define SPTE_BASE_ADDR_MASK (((1ULL << 52) - 1) & ~(u64)(PAGE_SIZE-1))#endif -#define SPTE_PERM_MASK (PT_PRESENT_MASK | PT_WRITABLE_MASK | shadow_user_mask \ - | shadow_x_mask | shadow_nx_mask | shadow_me_mask) -#define ACC_EXEC_MASK 1#define ACC_WRITE_MASK PT_WRITABLE_MASK#define ACC_USER_MASK PT_USER_MASK
Signature · 1 line
2.43.0
PATCH 3/8 KVM: x86/mmu: adjust MMIO generation bit allocation and allowed mask
In reply to Jon KohlerUpdate SPTE_MMIO_ALLOWED_MASK to allow EPT user executable (bit 10) to be treated like EPT RWX bit2:0, as when mode-based execute control is enabled, bit 10 can act like a "present" bit.
No functional changes intended.
--- arch/x86/kvm/mmu/spte.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu/spte.h +7 -6
@@ -96,11 +96,11 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));#undef SHADOW_ACC_TRACK_SAVED_MASK/* -* Due to limited space in PTEs, the MMIO generation is a 19 bit subset of +* Due to limited space in PTEs, the MMIO generation is an 18 bit subset of * the memslots generation and is derived as follows: * -* Bits 0-7 of the MMIO generation are propagated to spte bits 3-10 -* Bits 8-18 of the MMIO generation are propagated to spte bits 52-62 +* Bits 0-6 of the MMIO generation are propagated to spte bits 3-9 +* Bits 7-17 of the MMIO generation are propagated to spte bits 52-62 * * The KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS flag is intentionally not included in * the MMIO generation number, as doing so would require stealing a bit from
@@ -111,7 +111,7 @@ static_assert(!(EPT_SPTE_MMU_WRITABLE & SHADOW_ACC_TRACK_SAVED_MASK));*/ #define MMIO_SPTE_GEN_LOW_START 3 -#define MMIO_SPTE_GEN_LOW_END 10 +#define MMIO_SPTE_GEN_LOW_END 9#define MMIO_SPTE_GEN_HIGH_START 52#define MMIO_SPTE_GEN_HIGH_END 62
@@ -133,7 +133,8 @@ static_assert(!(SPTE_MMU_PRESENT_MASK &* and so they're off-limits for generation; additional checks ensure the mask * doesn't overlap legal PA bits), and bit 63 (carved out for future usage). */ -#define SPTE_MMIO_ALLOWED_MASK (BIT_ULL(63) | GENMASK_ULL(51, 12) | GENMASK_ULL(2, 0)) +#define SPTE_MMIO_ALLOWED_MASK (BIT_ULL(63) | GENMASK_ULL(51, 12) | \ + BIT_ULL(10) | GENMASK_ULL(2, 0))static_assert (!(SPTE_MMIO_ALLOWED_MASK & (SPTE_MMU_PRESENT_MASK |MMIO_SPTE_GEN_LOW_MASK |MMIO_SPTE_GEN_HIGH_MASK )));
@@ -141,7 +142,7 @@ static_assert(!(SPTE_MMIO_ALLOWED_MASK &#define MMIO_SPTE_GEN_HIGH_BITS (MMIO_SPTE_GEN_HIGH_END - MMIO_SPTE_GEN_HIGH_START + 1)/* remember to adjust the comment above as well if you change these */ -static_assert (MMIO_SPTE_GEN_LOW_BITS ==8 &&MMIO_SPTE_GEN_HIGH_BITS ==11 ); +static_assert (MMIO_SPTE_GEN_LOW_BITS ==7 &&MMIO_SPTE_GEN_HIGH_BITS ==11 );#define MMIO_SPTE_GEN_LOW_SHIFT (MMIO_SPTE_GEN_LOW_START - 0)#define MMIO_SPTE_GEN_HIGH_SHIFT (MMIO_SPTE_GEN_HIGH_START - MMIO_SPTE_GEN_LOW_BITS)
Signature · 1 line
2.43.0
PATCH 4/8 KVM: x86/mmu: update access permissions from ACC_ALL to ACC_RWX
In reply to Jon KohlerIntroduce ACC_RWX to capture traditional RWX access bits and modify the various consumers of ACC_ALL to use ACC_RWX instead, to prepare for Intel MBEC enablement, as suggested by Sean [1].
The only areas that really need ACC_ALL are kvm_mmu_page_get_access() and trace_mark_mmio_spte().
No functional change intended.
[1] https://lore.kernel.org/all/[email protected]/
--- arch/x86/kvm/mmu/mmu.c | 16 ++++++++-------- arch/x86/kvm/mmu/spte.h | 3 ++- arch/x86/kvm/mmu/tdp_mmu.c | 4 ++-- 3 files changed, 12 insertions(+), 11 deletions(-)
arch/x86/kvm/mmu/mmu.c +8 -8
@@ -3452,7 +3452,7 @@ static int direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (it.level == fault->goal_level)break ; - sp =kvm_mmu_get_child_sp (vcpu, it.sptep, base_gfn, true,ACC_ALL ); + sp =kvm_mmu_get_child_sp (vcpu, it.sptep, base_gfn, true,ACC_RWX );if (sp ==ERR_PTR (-EEXIST ))continue ;
@@ -3465,7 +3465,7 @@ static int direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (WARN_ON_ONCE (it.level != fault->goal_level))return -EFAULT ; - ret =mmu_set_spte (vcpu, fault->slot, it.sptep,ACC_ALL , + ret =mmu_set_spte (vcpu, fault->slot, it.sptep,ACC_RWX , base_gfn, fault->pfn, fault);if (ret ==RET_PF_SPURIOUS )return ret;
@@ -3698,7 +3698,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* current CPU took the fault. * * Need not check the access of upper level table entries since -* they are always ACC_ALL. +* they are always ACC_RWX. */ if (is_access_allowed (fault, spte)) { ret =RET_PF_SPURIOUS ;
@@ -4804,7 +4804,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultif (r)return r; - r =kvm_mmu_faultin_pfn (vcpu, fault,ACC_ALL ); + r =kvm_mmu_faultin_pfn (vcpu, fault,ACC_RWX );if (r !=RET_PF_CONTINUE )return r;
@@ -4895,7 +4895,7 @@ static int kvm_tdp_mmu_page_fault(struct kvm_vcpu *vcpu,if (r)return r; - r =kvm_mmu_faultin_pfn (vcpu, fault,ACC_ALL ); + r =kvm_mmu_faultin_pfn (vcpu, fault,ACC_RWX );if (r !=RET_PF_CONTINUE )return r;
@@ -5614,7 +5614,7 @@ static union kvm_cpu_role kvm_calc_cpu_role(struct kvm_vcpu *vcpu, {union kvm_cpu_role role = {0 }; - role.base.access =ACC_ALL ; + role.base.access =ACC_RWX ; role.base.smm =is_smm (vcpu); role.base.guest_mode =is_guest_mode (vcpu); role.ext.valid =1 ;
@@ -5695,7 +5695,7 @@ kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu, {union kvm_mmu_page_role role = {0 }; - role.access =ACC_ALL ; + role.access =ACC_RWX ; role.cr0_wp = true; role.efer_nx = true; role.smm = cpu_role.base.smm;
@@ -5826,7 +5826,7 @@ kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty, role.base.direct = false; role.base.ad_disabled = !accessed_dirty; role.base.guest_mode = true; - role.base.access =ACC_ALL ; + role.base.access =ACC_RWX ; role.ext.word =0 ; role.ext.execonly = execonly;
arch/x86/kvm/mmu/spte.h +2 -1
@@ -45,7 +45,8 @@ static_assert(SPTE_TDP_AD_ENABLED == 0);#define ACC_EXEC_MASK 1#define ACC_WRITE_MASK PT_WRITABLE_MASK#define ACC_USER_MASK PT_USER_MASK -#define ACC_ALL (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK) +#define ACC_RWX (ACC_EXEC_MASK | ACC_WRITE_MASK | ACC_USER_MASK) +#define ACC_ALL ACC_RWX/* The mask for the R/X bits in EPT PTEs */ #define SPTE_EPT_READABLE_MASK 0x1ull
arch/x86/kvm/mmu/tdp_mmu.c +2 -2
@@ -1190,9 +1190,9 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, }if (unlikely (!fault->slot)) - new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_ALL ); + new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_RWX );else - wrprot =make_spte (vcpu, sp, fault->slot,ACC_ALL , iter->gfn, + wrprot =make_spte (vcpu, sp, fault->slot,ACC_RWX , iter->gfn, fault->pfn, iter->old_spte, fault->prefetch, false, fault->map_writable, &new_spte);
Signature · 1 line
2.43.0
PATCH 5/8 KVM: x86/mmu: bootstrap support for Intel MBEC
In reply to Jon KohlerExtend kvm_mmu_page_role access bitfield from 3 to 4, where the 4th bit will be used to track user executable pages with Intel mode-based execute control (MBEC).
Extend SPTE generation and introduce shadow_ux value to account for user and kernel executable distinctions under MBEC.
Extend kvm_mmu_page_role to include a new has_mbec field, such that a given MMU's can be flagged as one with MBEC-awareness, which is to say with an access bitfield that can evaluate to 4 different options.
Modified various functions to utilize the new access masks, ensuring compatibility with MBEC.
Update mmu documentation to clarify the role of execute permissions when MBEC is enabled.
Add capability helper for cpu_has_vmx_mode_based_ept_exec and plumb into kvm_mmu_set_ept_masks.
NOTE: Also roll back change to ACC_ALL vs ACC_RWX in mmu.c, as VM's do not boot properly, which likely need to be reworked, open to ideas!
Past that, no functional change intended, as nothing sets has_mbec yet.
--- Documentation/virt/kvm/x86/mmu.rst | 9 ++++- arch/x86/include/asm/kvm_host.h | 11 ++--- arch/x86/include/asm/vmx.h | 4 ++ arch/x86/kvm/mmu.h | 8 +++- arch/x86/kvm/mmu/mmu.c | 24 +++++------ arch/x86/kvm/mmu/mmutrace.h | 23 +++++++---- arch/x86/kvm/mmu/paging_tmpl.h | 24 +++++++---- arch/x86/kvm/mmu/spte.c | 65 +++++++++++++++++++++++------- arch/x86/kvm/mmu/spte.h | 57 +++++++++++++++++++++++--- arch/x86/kvm/mmu/tdp_mmu.c | 12 ++++-- arch/x86/kvm/vmx/capabilities.h | 6 +++ arch/x86/kvm/vmx/vmx.c | 3 +- 12 files changed, 188 insertions(+), 58 deletions(-)
Documentation/virt/kvm/x86/mmu.rst +8 -1
@@ -172,7 +172,14 @@ Shadow pages contain the following information: quadrant maps 1GB virtual address space. role.access: Inherited guest access permissions from the parent ptes in the form uwx. - Note execute permission is positive, not negative. + Note execute permission is positive, not negative. When Intel MBEC is + not enabled, permissions follow the uwx form. When Intel MBEC is enabled, + execute is split into two permissions, kernel executable and user + executable, with the split controlled by role.has_mbec. + role.has_mbec: + When role.has_mbec=1, Intel mode-based execute control is active, which + gives the guest the ability to split execute pages into two permissions, + kernel executable and user executable. role.invalid: The page is invalid and should not be used. It is a root page that is currently pinned (by a cpu hardware register pointing to it); once it is
arch/x86/include/asm/kvm_host.h +6 -5
@@ -319,11 +319,11 @@ struct kvm_kernel_irq_routing_entry;* the number of unique SPs that can theoretically be created is 2^n, where n * is the number of bits that are used to compute the role. * -* But, even though there are 20 bits in the mask below, not all combinations +* But, even though there are 22 bits in the mask below, not all combinations * of modes and flags are possible: * * - invalid shadow pages are not accounted, mirror pages are not shadowed, -* so the bits are effectively 18. +* so the bits are effectively 20. * * - quadrant will only be used if has_4_byte_gpte=1 (non-PAE paging); * execonly and ad_disabled are only used for nested EPT which has
@@ -338,7 +338,7 @@ struct kvm_kernel_irq_routing_entry;* cr0_wp=0, therefore these three bits only give rise to 5 possibilities. * * Therefore, the maximum number of possible upper-level shadow pages for a -* single gfn is a bit less than 2^13. +* single gfn is a bit less than 2^15. */ union kvm_mmu_page_role {u32 word;
@@ -347,7 +347,7 @@ union kvm_mmu_page_role { unsigned has_4_byte_gpte:1; unsigned quadrant:2; unsigned direct:1; - unsigned access:3; + unsigned access:4; unsigned invalid:1; unsigned efer_nx:1; unsigned cr0_wp:1;
@@ -357,7 +357,8 @@ union kvm_mmu_page_role { unsigned guest_mode:1; unsigned passthrough:1; unsigned is_mirror:1; - unsigned :4; + unsigned has_mbec:1; + unsigned:2;/* * This is left at the top of the word so that
arch/x86/include/asm/vmx.h +4 -0
@@ -547,6 +547,7 @@ enum vmcs_field {#define VMX_EPT_IPAT_BIT (1ull << 6)#define VMX_EPT_ACCESS_BIT (1ull << 8)#define VMX_EPT_DIRTY_BIT (1ull << 9) +#define VMX_EPT_USER_EXECUTABLE_MASK (1ull << 10)#define VMX_EPT_SUPPRESS_VE_BIT (1ull << 63)#define VMX_EPT_RWX_MASK (VMX_EPT_READABLE_MASK | \ VMX_EPT_WRITABLE_MASK | \
@@ -605,9 +606,12 @@ enum vm_entry_failure_code {#define EPT_VIOLATION_GVA_TRANSLATED BIT(8)#define EPT_VIOLATION_RWX_TO_PROT (__epte) (((__epte) & VMX_EPT_RWX_MASK) << 3) +#define EPT_VIOLATION_USER_EXEC_TO_PROT (__epte) (((__epte) & VMX_EPT_USER_EXECUTABLE_MASK) >> 4)static_assert (EPT_VIOLATION_RWX_TO_PROT (VMX_EPT_RWX_MASK ) == (EPT_VIOLATION_PROT_READ |EPT_VIOLATION_PROT_WRITE |EPT_VIOLATION_PROT_EXEC )); +static_assert (EPT_VIOLATION_USER_EXEC_TO_PROT (VMX_EPT_USER_EXECUTABLE_MASK ) == + (EPT_VIOLATION_PROT_USER_EXEC ));/* * Exit Qualifications for NOTIFY VM EXIT
arch/x86/kvm/mmu.h +7 -1
@@ -81,7 +81,8 @@ u8 kvm_mmu_get_max_tdp_level(void);void kvm_mmu_set_mmio_spte_mask (u64 mmio_value,u64 mmio_mask,u64 access_mask);void kvm_mmu_set_mmio_spte_value (struct kvm *kvm,u64 mmio_value);void kvm_mmu_set_me_spte_mask (u64 me_value,u64 me_mask); -void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only); +void kvm_mmu_set_ept_masks (bool has_ad_bits,bool has_exec_only, +bool has_mbec);void kvm_init_mmu (struct kvm_vcpu *vcpu); void kvm_init_shadow_npt_mmu(struct kvm_vcpu *vcpu, unsigned long cr0,
@@ -174,6 +175,11 @@ static inline void kvm_mmu_refresh_passthrough_bits(struct kvm_vcpu *vcpu,__kvm_mmu_refresh_passthrough_bits (vcpu, mmu); } +static inline bool mmu_has_mbec (struct kvm_vcpu *vcpu) +{ +return vcpu->arch.mmu->root_role.has_mbec; +} +/* * Check if a given access (described through the I/D, W/R and U/S bits of a * page fault error code pfec) causes a permission fault with the given PTE
arch/x86/kvm/mmu/mmu.c +12 -12
@@ -2033,7 +2033,7 @@ static bool kvm_sync_page_check(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)*/ const union kvm_mmu_page_role sync_role_ign = { .level =0xf , - .access =0x7 , + .access =0xf , .quadrant =0x3 , .passthrough =0x1 , };
@@ -3080,7 +3080,7 @@ static int mmu_set_spte(struct kvm_vcpu *vcpu, struct kvm_memory_slot *slot, ret =RET_PF_SPURIOUS ; }else { flush |=mmu_spte_update (sptep, spte); -trace_kvm_mmu_set_spte (level, gfn, sptep); +trace_kvm_mmu_set_spte (vcpu, level, gfn, sptep); }if (wrprot && write_fault)
@@ -3452,7 +3452,7 @@ static int direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (it.level == fault->goal_level)break ; - sp =kvm_mmu_get_child_sp (vcpu, it.sptep, base_gfn, true,ACC_RWX ); + sp =kvm_mmu_get_child_sp (vcpu, it.sptep, base_gfn, true,ACC_ALL );if (sp ==ERR_PTR (-EEXIST ))continue ;
@@ -3465,7 +3465,7 @@ static int direct_map(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)if (WARN_ON_ONCE (it.level != fault->goal_level))return -EFAULT ; - ret =mmu_set_spte (vcpu, fault->slot, it.sptep,ACC_RWX , + ret =mmu_set_spte (vcpu, fault->slot, it.sptep,ACC_ALL , base_gfn, fault->pfn, fault);if (ret ==RET_PF_SPURIOUS )return ret;
@@ -3698,9 +3698,9 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)* current CPU took the fault. * * Need not check the access of upper level table entries since -* they are always ACC_RWX. +* they are always ACC_ALL. */ -if (is_access_allowed (fault, spte)) { +if (is_access_allowed (fault, spte, vcpu)) { ret =RET_PF_SPURIOUS ;break ; }
@@ -3748,7 +3748,7 @@ static int fast_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *fault)/* Verify that the fault can be handled in the fast path */ if (new_spte == spte || - !is_access_allowed (fault, new_spte)) + !is_access_allowed (fault, new_spte, vcpu))break ;/*
@@ -4804,7 +4804,7 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, struct kvm_page_fault *faultif (r)return r; - r =kvm_mmu_faultin_pfn (vcpu, fault,ACC_RWX ); + r =kvm_mmu_faultin_pfn (vcpu, fault,ACC_ALL );if (r !=RET_PF_CONTINUE )return r;
@@ -4895,7 +4895,7 @@ static int kvm_tdp_mmu_page_fault(struct kvm_vcpu *vcpu,if (r)return r; - r =kvm_mmu_faultin_pfn (vcpu, fault,ACC_RWX ); + r =kvm_mmu_faultin_pfn (vcpu, fault,ACC_ALL );if (r !=RET_PF_CONTINUE )return r;
@@ -5614,7 +5614,7 @@ static union kvm_cpu_role kvm_calc_cpu_role(struct kvm_vcpu *vcpu, {union kvm_cpu_role role = {0 }; - role.base.access =ACC_RWX ; + role.base.access =ACC_ALL ; role.base.smm =is_smm (vcpu); role.base.guest_mode =is_guest_mode (vcpu); role.ext.valid =1 ;
@@ -5695,7 +5695,7 @@ kvm_calc_tdp_mmu_root_page_role(struct kvm_vcpu *vcpu, {union kvm_mmu_page_role role = {0 }; - role.access =ACC_RWX ; + role.access =ACC_ALL ; role.cr0_wp = true; role.efer_nx = true; role.smm = cpu_role.base.smm;
@@ -5826,7 +5826,7 @@ kvm_calc_shadow_ept_root_page_role(struct kvm_vcpu *vcpu, bool accessed_dirty, role.base.direct = false; role.base.ad_disabled = !accessed_dirty; role.base.guest_mode = true; - role.base.access =ACC_RWX ; + role.base.access =ACC_ALL ; role.ext.word =0 ; role.ext.execonly = execonly;
arch/x86/kvm/mmu/mmutrace.h +16 -7
@@ -22,10 +22,16 @@ __entry->root_count = sp->root_count; \ __entry->unsync = sp->unsync; +/* +* X == ACC_EXEC_MASK: executable without guest_exec_control and only +* supervisor execute with guest exec control +* x == ACC_USER_EXEC_MASK: user execute with guest exec control +*/ #define KVM_MMU_PAGE_PRINTK () ({ \ const char *saved_ptr = trace_seq_buffer_ptr(p); \ static const char *access_str[] = { \ - "---", "--x", "w--", "w-x", "-u-", "-ux", "wu-", "wux" \ + "----", "---X", "-w--", "-w-X", "--u-", "--uX", "-wu-", "-wuX", \ + "x---", "x--X", "xw--", "xw-X", "x-u-", "x-uX", "xwu-", "xwuX" \ }; \ union kvm_mmu_page_role role; \ \
@@ -336,8 +342,8 @@ TRACE_EVENT( TRACE_EVENT( kvm_mmu_set_spte, - TP_PROTO(int level, gfn_t gfn, u64 *sptep), - TP_ARGS(level, gfn, sptep), + TP_PROTO(struct kvm_vcpu *vcpu, int level, gfn_t gfn, u64 *sptep), + TP_ARGS(vcpu, level, gfn, sptep), TP_STRUCT__entry( __field(u64, gfn)
@@ -346,7 +352,8 @@ TRACE_EVENT(__field (u8, level) /* These depend on page entry type, so compute them now. */ __field(bool, r) - __field(bool, x) + __field(bool, kx) + __field(bool, ux) __field(signed char, u) ),
@@ -356,15 +363,17 @@ TRACE_EVENT( __entry->sptep =virt_to_phys (sptep); __entry->level = level; __entry->r = shadow_present_mask || (__entry->spte &PT_PRESENT_MASK ); - __entry->x =is_executable_pte (__entry->spte); + __entry->kx =is_executable_pte (__entry->spte, false, vcpu); + __entry->ux =is_executable_pte (__entry->spte, true, vcpu); __entry->u = shadow_user_mask ? !!(__entry->spte & shadow_user_mask) :-1 ; ), -TP_printk ("gfn %llx spte %llx (%s%s%s%s) level %d at %llx" , +TP_printk ("gfn %llx spte %llx (%s%s%s%s%s) level %d at %llx" , __entry->gfn, __entry->spte, __entry->r ?"r" :"-" , __entry->spte &PT_WRITABLE_MASK ?"w" :"-" , - __entry->x ?"x" :"-" , + __entry->kx ?"X" :"-" , + __entry->ux ?"x" :"-" , __entry->u ==-1 ?"" : (__entry->u ?"u" :"-" ), __entry->level, __entry->sptep )
arch/x86/kvm/mmu/paging_tmpl.h +17 -7
@@ -124,12 +124,17 @@ static inline void FNAME(protect_clean_gpte)(struct kvm_mmu *mmu, unsigned *acce *access &= mask; } -static inline int FNAME (is_present_gpte )(unsigned long pte) +static inline int FNAME (is_present_gpte )(struct kvm_vcpu *vcpu, +unsigned long pte) {#if PTTYPE !=PTTYPE_EPT return pte &PT_PRESENT_MASK ;#else -return pte &7 ; +/* +* For EPT, an entry is present if any of bits 2:0 are set. +* With mode-based execute control, bit 10 also indicates presence. +*/ +return (pte &7 ) || (mmu_has_mbec (vcpu) && (pte & (1ULL <<10 )));#endif }
@@ -152,7 +157,7 @@ static bool FNAME(prefetch_invalid_gpte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, u64 *spte, u64 gpte) { - if (!FNAME(is_present_gpte)(gpte)) + if (!FNAME(is_present_gpte)(vcpu, gpte)) goto no_present;/* Prefetch only accessed entries (unless A/D bits are disabled). */
@@ -181,8 +186,9 @@ static inline unsigned FNAME(gpte_access)(u64 gpte)unsigned access;#if PTTYPE ==PTTYPE_EPT access = ((gpte &VMX_EPT_WRITABLE_MASK ) ?ACC_WRITE_MASK :0 ) | - ((gpte &VMX_EPT_EXECUTABLE_MASK ) ?ACC_EXEC_MASK :0 ) | - ((gpte &VMX_EPT_READABLE_MASK ) ?ACC_USER_MASK :0 ); + ((gpte &VMX_EPT_EXECUTABLE_MASK ) ?ACC_EXEC_MASK :0 ) | + ((gpte &VMX_EPT_USER_EXECUTABLE_MASK ) ?ACC_USER_EXEC_MASK :0 ) | + ((gpte &VMX_EPT_READABLE_MASK ) ?ACC_USER_MASK :0 );#else BUILD_BUG_ON (ACC_EXEC_MASK !=PT_PRESENT_MASK );BUILD_BUG_ON (ACC_EXEC_MASK !=1 );
@@ -332,7 +338,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,if (walker->level ==PT32E_ROOT_LEVEL ) { pte = mmu->get_pdptr (vcpu, (addr >>30 ) &3 );trace_kvm_mmu_paging_element (pte, walker->level); -if (!FNAME (is_present_gpte)(pte)) +if (!FNAME (is_present_gpte)(vcpu, pte)) goto error; --walker->level; }
@@ -414,7 +420,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,*/ pte_access = pt_access & (pte ^ walk_nx_mask); -if (unlikely (!FNAME (is_present_gpte)(pte))) +if (unlikely (!FNAME (is_present_gpte)(vcpu, pte))) goto error;if (unlikely (FNAME (is_rsvd_bits_set)(mmu, pte, walker->level))) {
@@ -493,6 +499,7 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,* out of date if it is serving an EPT misconfiguration. * [5:3] - Calculated by the page walk of the guest EPT page tables * [7:8] - Derived from [7:8] of real exit_qualification +* [10] - Derived from real exit_qualification, useful only with MBEC. * * The other bits are set to 0. */
@@ -511,6 +518,9 @@ static int FNAME(walk_addr_generic)(struct guest_walker *walker,* ACC_*_MASK flags! */ walker->fault.exit_qualification |=EPT_VIOLATION_RWX_TO_PROT (pte_access); +if (mmu_has_mbec (vcpu)) + walker->fault.exit_qualification |= + EPT_VIOLATION_USER_EXEC_TO_PROT(pte_access); }#endif walker->fault.address = addr;
arch/x86/kvm/mmu/spte.c +50 -15
136 lines not on this page · raw message
arch/x86/kvm/mmu/spte.h +52 -5
85 lines not on this page · raw message
arch/x86/kvm/mmu/tdp_mmu.c +8 -4
@@ -1183,16 +1183,20 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu,return RET_PF_RETRY ;if (is_shadow_present_pte (iter->old_spte) && - (fault->prefetch ||is_access_allowed (fault, iter->old_spte)) && + (fault->prefetch || +is_access_allowed (fault, iter->old_spte, vcpu)) &&is_last_spte (iter->old_spte, iter->level)) {WARN_ON_ONCE (fault->pfn !=spte_to_pfn (iter->old_spte));return RET_PF_SPURIOUS ; }if (unlikely (!fault->slot)) - new_spte =make_mmio_spte (vcpu, iter->gfn,ACC_RWX ); + new_spte =make_mmio_spte (vcpu, iter->gfn, +ACC_ALL );else - wrprot =make_spte (vcpu, sp, fault->slot,ACC_RWX , iter->gfn, + wrprot =make_spte (vcpu, sp, fault->slot, +ACC_ALL , + iter->gfn, fault->pfn, iter->old_spte, fault->prefetch, false, fault->map_writable, &new_spte);
@@ -1220,7 +1224,7 @@ static int tdp_mmu_map_handle_target_level(struct kvm_vcpu *vcpu, new_spte); ret =RET_PF_EMULATE ; }else { -trace_kvm_mmu_set_spte (iter->level, iter->gfn, +trace_kvm_mmu_set_spte (vcpu, iter->level, iter->gfn,rcu_dereference (iter->sptep)); }
arch/x86/kvm/vmx/capabilities.h +6 -0
@@ -270,6 +270,12 @@ static inline bool cpu_has_vmx_tsc_scaling(void)SECONDARY_EXEC_TSC_SCALING ; } +static inline bool cpu_has_vmx_mode_based_ept_exec (void ) +{ +return vmcs_config.cpu_based_2nd_exec_ctrl & +SECONDARY_EXEC_MODE_BASED_EPT_EXEC ; +} +static inline bool cpu_has_vmx_bus_lock_detection (void ) { return vmcs_config.cpu_based_2nd_exec_ctrl &
arch/x86/kvm/vmx/vmx.c +2 -1
@@ -8607,7 +8607,8 @@ __init int vmx_hardware_setup(void)if (enable_ept)kvm_mmu_set_ept_masks (enable_ept_ad_bits, -cpu_has_vmx_ept_execute_only ()); +cpu_has_vmx_ept_execute_only (), +cpu_has_vmx_mode_based_ept_exec ());else vt_x86_ops.get_mt_mask =NULL ;
Signature · 1 line
2.43.0
Paolo Bonzini
maintainerRe: [PATCH 5/8] KVM: x86/mmu: bootstrap support for Intel MBEC
Quoted 6 lines · Jon Kohler
Extend
kvm_mmu_page_roleaccess bitfield from 3 to 4, where the 4th bit will be used to track user executable pages with Intel mode-based execute control (MBEC).Extend SPTE generation and introduce
shadow_uxvalue to account for user and kernel executable distinctions under MBEC.
While MBEC has a different definition of the bits, GMET is essentially SMEP (except that AMD couldn't retrofit it into hCR4.SMEP due to how NPT handles the U bit). I wonder if it's possible to handle MBEC as SMEP as well, with some additional handling of the SPTEs (with shadox_x_mask and shadow_ux_mask taking the functionality of shadow_nx_mask and shadow_u_mask) but no large changes to the MMU.
This should be a much simpler patch set if it can be made to work. I'll take a look.
Paolo
Re: [PATCH 5/8] KVM: x86/mmu: bootstrap support for Intel MBEC
Quoted 11 lines · Paolo Bonzini
Quoted 5 lines · Jon Kohler
Extend
kvm_mmu_page_roleaccess bitfield from 3 to 4, where the 4th bit will be used to track user executable pages with Intel mode-based execute control (MBEC).
Extend SPTE generation and introduceshadow_uxvalue to account for user and kernel executable distinctions under MBEC.
Quoted 5 lines
While MBEC has a different definition of the bits, GMET is essentially SMEP (except that AMD couldn't retrofit it into hCR4.SMEP due to how NPT handles the U bit). I wonder if it's possible to handle MBEC as SMEP as well, with some additional handling of the SPTEs (with
shadox_x_maskandshadow_ux_masktaking the functionality ofshadow_nx_maskandshadow_u_mask) but no large changes to the MMU.This should be a much simpler patch set if it can be made to work. I'll take a look.
Paolo
Thanks, Paolo. Certainly if we can make this simpler, I’m all for it. I appreciate any help we can get on this one.
Happy to test out additional approaches, as we’ve got the Windows performance regression test for this completely automated on our side to study the impact one way or the other.
Thanks again,
Jon
PATCH 6/8 KVM: VMX: enhance EPT violation handler for MBEC
In reply to Jon KohlerExtend __vmx_handle_ept_violation to understand mmu_has_mbec and the differences between user mode and kernel mode fetches.
Add synthetic PF bit PFERR_USER_FETCH_MASK in EPT violation handler, used in error_code as a signal that the EPT violation is a user mode instruction fetch into permission_fault.
Extend permissions_fault and route mmu_has_mbec to a special handler, mbec_permission_fault, since permission_fault can no longer trivially shift to figure out if there was a permission fault or not.
--- arch/x86/include/asm/kvm_host.h | 8 +++- arch/x86/kvm/mmu.h | 7 +++- arch/x86/kvm/mmu/mmu.c | 66 +++++++++++++++++++++++++++++++++ arch/x86/kvm/mmu/spte.h | 14 ++++--- arch/x86/kvm/vmx/common.h | 22 ++++++----- 5 files changed, 100 insertions(+), 17 deletions(-)
arch/x86/include/asm/kvm_host.h +7 -1
@@ -286,7 +286,13 @@ enum x86_intercept_stage;* when the guest was accessing private memory. */ #define PFERR_PRIVATE_ACCESS BIT_ULL(49) -#define PFERR_SYNTHETIC_MASK (PFERR_IMPLICIT_ACCESS | PFERR_PRIVATE_ACCESS) +/* +* USER_FETCH_MASK is a KVM-defined flag used to indicate user fetches when +* translating EPT violations for Intel MBEC. +*/ +#define PFERR_USER_FETCH_MASK BIT_ULL(50) +#define PFERR_SYNTHETIC_MASK (PFERR_IMPLICIT_ACCESS | PFERR_PRIVATE_ACCESS | \ + PFERR_USER_FETCH_MASK)/* apic attention bits */ #define KVM_APIC_CHECK_VAPIC 0
arch/x86/kvm/mmu.h +6 -1
@@ -95,6 +95,8 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code, u64 fault_address, char *insn, int insn_len);void __kvm_mmu_refresh_passthrough_bits (struct kvm_vcpu *vcpu,struct kvm_mmu *mmu); +bool mbec_permission_fault (struct kvm_vcpu *vcpu,unsigned int pte_access, +unsigned int pfec);int kvm_mmu_load (struct kvm_vcpu *vcpu);void kvm_mmu_unload (struct kvm_vcpu *vcpu);
@@ -216,7 +218,10 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,kvm_mmu_refresh_passthrough_bits (vcpu, mmu); - fault = (mmu->permissions[index] >> pte_access) &1 ; +if (mmu_has_mbec (vcpu)) + fault =mbec_permission_fault (vcpu, pte_access, pfec); +else + fault = (mmu->permissions[index] >> pte_access) &1 ;WARN_ON_ONCE (pfec & (PFERR_PK_MASK |PFERR_SS_MASK |PFERR_RSVD_MASK ));if (unlikely (mmu->pkru_mask)) {
arch/x86/kvm/mmu/mmu.c +66 -0
72 lines not on this page · raw message
arch/x86/kvm/mmu/spte.h +9 -5
@@ -383,14 +383,18 @@ static inline bool is_executable_pte_fault(u64 spte,*/ if (WARN_ON_ONCE (!shadow_x_mask))return fault->user || !(spte & shadow_user_mask); -/* -* For TDP MMU, the fault->user bit indicates a read access, -* not the guest's CPL. For execute faults, check both execute -* permissions since we don't know the actual CPL. +* For TDP MMU, fault->user indicates a read access, not CPL. +* For execute faults, we don't know the CPL here, so we can't +* definitively check permissions. Being optimistic and checking +* for any execute permission can lead to infinite fault loops +* if the wrong type of execute permission is present (e.g. UX +* only for a kernel fetch). The safe approach is to be +* pessimistic and return false, forcing the fault to the slow +* path which can do a full permission check. */ if (fault->is_tdp) -return spte & (shadow_x_mask | shadow_ux_mask); +return false;return spte & (fault->user ? shadow_ux_mask : shadow_x_mask); }
arch/x86/kvm/vmx/common.h +12 -10
@@ -83,6 +83,7 @@ static inline bool vt_is_tdx_private_gpa(struct kvm *kvm, gpa_t gpa)static inline int __vmx_handle_ept_violation (struct kvm_vcpu *vcpu,gpa_t gpa,unsigned long exit_qualification) { +unsigned long rwx_mask;u64 error_code;/* Is it a read fault? */
@@ -92,16 +93,17 @@ static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa, error_code |= (exit_qualification &EPT_VIOLATION_ACC_WRITE ) ?PFERR_WRITE_MASK :0 ;/* Is it a fetch fault? */ - error_code |= (exit_qualification &EPT_VIOLATION_ACC_INSTR ) - ?PFERR_FETCH_MASK :0 ; -/* -* ept page table entry is present? -* note: unconditionally clear USER_EXEC until mode-based -* execute control is implemented -*/ - error_code |= (exit_qualification & - (EPT_VIOLATION_PROT_MASK & ~EPT_VIOLATION_PROT_USER_EXEC )) - ?PFERR_PRESENT_MASK :0 ; +if (exit_qualification &EPT_VIOLATION_ACC_INSTR ) { + error_code |=PFERR_FETCH_MASK ; +if (mmu_has_mbec (vcpu) && + exit_qualification &EPT_VIOLATION_PROT_USER_EXEC ) + error_code |=PFERR_USER_FETCH_MASK ; + } +/* ept page table entry is present? */ + rwx_mask =EPT_VIOLATION_PROT_MASK ; +if (mmu_has_mbec (vcpu)) + rwx_mask |=EPT_VIOLATION_PROT_USER_EXEC ; + error_code |= (exit_qualification & rwx_mask) ?PFERR_PRESENT_MASK :0 ;if (exit_qualification &EPT_VIOLATION_GVA_IS_VALID ) error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) ?
Signature · 1 line
2.43.0
PATCH 7/8 KVM: VMX: allow MBEC with EVMCS
In reply to Jon KohlerExtend EVMCS1_SUPPORTED_2NDEXEC to allow MBEC and EVMCS to coexist. Presenting both EVMCS and MBEC simultaneously causes KVM to filter out MBEC and not present it as a supported control to the guest, preventing performance gains from MBEC when Windows HVCI is enabled.
The guest may choose not to use MBEC (e.g., if the admin does not enable Windows HVCI / Memory Integrity), but if they use traditional nested virt (Hyper-V, WSL2, etc.), having EVMCS exposed is important for improving nested guest performance. IOW allowing MBEC and EVMCS to coexist provides maximum optionality to Windows users without overcomplicating VM administration.
--- arch/x86/kvm/vmx/hyperv_evmcs.h | 1 + 1 file changed, 1 insertion(+)
arch/x86/kvm/vmx/hyperv_evmcs.h +1 -0
@@ -87,6 +87,7 @@SECONDARY_EXEC_PT_CONCEAL_VMX | \SECONDARY_EXEC_BUS_LOCK_DETECTION | \SECONDARY_EXEC_NOTIFY_VM_EXITING | \ +SECONDARY_EXEC_MODE_BASED_EPT_EXEC | \ SECONDARY_EXEC_ENCLS_EXITING) #define EVMCS1_SUPPORTED_3RDEXEC (0ULL)
Signature · 1 line
2.43.0
PATCH 8/8 KVM: nVMX: advertise MBEC and setup mmu has_mbec
In reply to Jon KohlerAdd SECONDARY_EXEC_MODE_BASED_EPT_EXEC as optional secondary execution control bit; however, this is not used by L1 VM's, so filter out this similar to how VMFUNC is treated.
Advertise SECONDARY_EXEC_MODE_BASED_EPT_EXEC (MBEC) to userspace, which allows userspace to expose and advertise the feature to the guest.
When MBEC is enabled by userspace, configure mmu root_role has_mbec.
--- arch/x86/kvm/vmx/nested.c | 6 ++++++ arch/x86/kvm/vmx/vmx.c | 7 +++++++ arch/x86/kvm/vmx/vmx.h | 1 + 3 files changed, 14 insertions(+)
arch/x86/kvm/vmx/nested.c +6 -0
@@ -474,6 +474,7 @@ static void nested_ept_new_eptp(struct kvm_vcpu *vcpu)static void nested_ept_init_mmu_context (struct kvm_vcpu *vcpu) { +struct vmcs12 *vmcs12 =get_vmcs12 (vcpu);WARN_ON (mmu_is_nested (vcpu)); vcpu->arch.mmu = &vcpu->arch.guest_mmu;
@@ -483,6 +484,8 @@ static void nested_ept_init_mmu_context(struct kvm_vcpu *vcpu) vcpu->arch.mmu->get_pdptr = kvm_pdptr_read; vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; +if (nested_cpu_has2 (vmcs12,SECONDARY_EXEC_MODE_BASED_EPT_EXEC )) + vcpu->arch.mmu->root_role.has_mbec = true; }static void nested_ept_uninit_mmu_context (struct kvm_vcpu *vcpu)
@@ -7313,6 +7316,9 @@ static void nested_vmx_setup_secondary_ctls(u32 ept_caps, msrs->ept_caps |=VMX_EPT_AD_BIT ; } +if (cpu_has_vmx_mode_based_ept_exec ()) + msrs->secondary_ctls_high |= +SECONDARY_EXEC_MODE_BASED_EPT_EXEC ;/* * Advertise EPTP switching irrespective of hardware support, * KVM emulates it in software so long as VMFUNC is supported.
arch/x86/kvm/vmx/vmx.c +7 -0
@@ -2682,6 +2682,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf,return -EIO ; vmx_cap->ept =0 ; + _cpu_based_2nd_exec_control &= ~SECONDARY_EXEC_MODE_BASED_EPT_EXEC ; _cpu_based_2nd_exec_control &= ~SECONDARY_EXEC_EPT_VIOLATION_VE ; } if (!(_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_VPID) &&
@@ -4610,6 +4611,12 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx *vmx)*/ exec_control &= ~SECONDARY_EXEC_ENABLE_VMFUNC ; +/* +* KVM doesn't support mode-based EPT execute control for L1, but the +* capability is advertised to L1 guests so they can use it for L2. +*/ + exec_control &= ~SECONDARY_EXEC_MODE_BASED_EPT_EXEC ; +/* SECONDARY_EXEC_DESC is enabled/disabled on writes to CR4.UMIP, * in vmx_set_cr4. */ exec_control &= ~SECONDARY_EXEC_DESC ;
arch/x86/kvm/vmx/vmx.h +1 -0
@@ -584,6 +584,7 @@ static inline u8 vmx_get_rvi(void)SECONDARY_EXEC_ENABLE_VMFUNC | \SECONDARY_EXEC_BUS_LOCK_DETECTION | \SECONDARY_EXEC_NOTIFY_VM_EXITING | \ +SECONDARY_EXEC_MODE_BASED_EPT_EXEC | \SECONDARY_EXEC_ENCLS_EXITING | \ SECONDARY_EXEC_EPT_VIOLATION_VE)
Signature · 1 line
2.43.0