Skip to content

Commit b320c03

Browse files
yosrym93sean-jc
authored andcommitted
KVM: selftests: Kill eptPageTablePointer
Replace the struct overlay with explicit bitmasks, which is clearer and less error-prone. See commit f18b4ae ("kvm: selftests: do not use bitfields larger than 32-bits for PTEs") for an example of why bitfields are not preferable. Remove the unused PAGE_SHIFT_4K definition while at it. No functional change intended. Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev> Link: https://patch.msgid.link/20251230230150.4150236-5-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 60de423 commit b320c03

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

  • tools/testing/selftests/kvm/lib/x86

tools/testing/selftests/kvm/lib/x86/vmx.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@
1010
#include "processor.h"
1111
#include "vmx.h"
1212

13-
#define PAGE_SHIFT_4K 12
14-
1513
#define KVM_EPT_PAGE_TABLE_MIN_PADDR 0x1c0000
1614

15+
#define EPTP_MT_SHIFT 0 /* EPTP memtype bits 2:0 */
16+
#define EPTP_PWL_SHIFT 3 /* EPTP page walk length bits 5:3 */
17+
#define EPTP_AD_ENABLED_SHIFT 6 /* EPTP AD enabled bit 6 */
18+
19+
#define EPTP_WB (X86_MEMTYPE_WB << EPTP_MT_SHIFT)
20+
#define EPTP_PWL_4 (3ULL << EPTP_PWL_SHIFT) /* PWL is (levels - 1) */
21+
#define EPTP_AD_ENABLED (1ULL << EPTP_AD_ENABLED_SHIFT)
22+
1723
bool enable_evmcs;
1824

1925
struct hv_enlightened_vmcs *current_evmcs;
@@ -34,14 +40,6 @@ struct eptPageTableEntry {
3440
uint64_t suppress_ve:1;
3541
};
3642

37-
struct eptPageTablePointer {
38-
uint64_t memory_type:3;
39-
uint64_t page_walk_length:3;
40-
uint64_t ad_enabled:1;
41-
uint64_t reserved_11_07:5;
42-
uint64_t address:40;
43-
uint64_t reserved_63_52:12;
44-
};
4543
int vcpu_enable_evmcs(struct kvm_vcpu *vcpu)
4644
{
4745
uint16_t evmcs_ver;
@@ -196,16 +194,15 @@ static inline void init_vmcs_control_fields(struct vmx_pages *vmx)
196194
vmwrite(PIN_BASED_VM_EXEC_CONTROL, rdmsr(MSR_IA32_VMX_TRUE_PINBASED_CTLS));
197195

198196
if (vmx->eptp_gpa) {
199-
uint64_t ept_paddr;
200-
struct eptPageTablePointer eptp = {
201-
.memory_type = X86_MEMTYPE_WB,
202-
.page_walk_length = 3, /* + 1 */
203-
.ad_enabled = ept_vpid_cap_supported(VMX_EPT_VPID_CAP_AD_BITS),
204-
.address = vmx->eptp_gpa >> PAGE_SHIFT_4K,
205-
};
206-
207-
memcpy(&ept_paddr, &eptp, sizeof(ept_paddr));
208-
vmwrite(EPT_POINTER, ept_paddr);
197+
uint64_t eptp = vmx->eptp_gpa | EPTP_WB | EPTP_PWL_4;
198+
199+
TEST_ASSERT((vmx->eptp_gpa & ~PHYSICAL_PAGE_MASK) == 0,
200+
"Illegal bits set in vmx->eptp_gpa");
201+
202+
if (ept_vpid_cap_supported(VMX_EPT_VPID_CAP_AD_BITS))
203+
eptp |= EPTP_AD_ENABLED;
204+
205+
vmwrite(EPT_POINTER, eptp);
209206
sec_exec_ctl |= SECONDARY_EXEC_ENABLE_EPT;
210207
}
211208

0 commit comments

Comments
 (0)