Page MenuHomeFreeBSD

D61796.1795272581.diff
No OneTemporary

Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None

D61796.1795272581.diff

diff --git a/sys/amd64/vmm/intel/vmx.c b/sys/amd64/vmm/intel/vmx.c
--- a/sys/amd64/vmm/intel/vmx.c
+++ b/sys/amd64/vmm/intel/vmx.c
@@ -1151,7 +1151,7 @@
vmx_nested_vcpu_init(struct vmx_vcpu *vcpu)
{
struct vmcs *vmcs;
- int error;
+ int error, vpid;
vmcs = malloc_aligned(sizeof(*vmcs), PAGE_SIZE, M_VMX,
M_WAITOK | M_ZERO);
@@ -1182,6 +1182,13 @@
vcpu->nested = vmx_nested_alloc();
vcpu->nested->vmcs02 = vmcs;
+ /* A VPID for L2. Without one L2 runs without VPID. */
+ vcpu->nested->vpid_cpu = NOCPU;
+ if ((procbased_ctls2 & PROCBASED2_ENABLE_VPID) != 0) {
+ vpid = alloc_unr(vpid_unr);
+ if (vpid != -1)
+ vcpu->nested->vpid02 = vpid;
+ }
}
static void *
@@ -2928,6 +2935,8 @@
case EXIT_REASON_VMWRITE:
case EXIT_REASON_VMXOFF:
case EXIT_REASON_VMXON:
+ case EXIT_REASON_INVEPT:
+ case EXIT_REASON_INVVPID:
SDT_PROBE3(vmm, vmx, exit, vminsn, vmx, vcpuid, vmexit);
if (vcpu->nested != NULL) {
vmx_nested_insn_exit(vcpu, reason, qual);
@@ -3154,6 +3163,7 @@
struct vm_exit *vmexit;
struct vlapic *vlapic;
struct region_descriptor gdtr, idtr;
+ struct invvpid_desc invvpid_desc = { 0 };
uint64_t entryinfo, gpa, qual;
register_t l2_rip;
uint32_t event, idtvec_info, intr_info, reason;
@@ -3196,6 +3206,10 @@
vmcs_write(VMCS_HOST_TR_BASE, vmm_get_host_trbase());
vmcs_write(VMCS_HOST_GDTR_BASE, vmm_get_host_gdtrbase());
vmcs_write(VMCS_HOST_GS_BASE, vmm_get_host_gsbase());
+ if (vmx_nested_vpid_flush(vcpu)) {
+ invvpid_desc.vpid = nested->vpid02;
+ invvpid(INVVPID_TYPE_SINGLE_CONTEXT, invvpid_desc);
+ }
for (;;) {
/* See vmx_run() for why interrupts are disabled from here. */
@@ -3680,6 +3694,8 @@
vpid_free(vcpu->state.vpid);
if (vcpu->nested != NULL) {
+ if (vcpu->nested->vpid02 != 0)
+ vpid_free(vcpu->nested->vpid02);
free(vcpu->nested->vmcs02, M_VMX);
vmx_nested_free(vcpu->nested);
}
diff --git a/sys/amd64/vmm/intel/vmx_nested.h b/sys/amd64/vmm/intel/vmx_nested.h
--- a/sys/amd64/vmm/intel/vmx_nested.h
+++ b/sys/amd64/vmm/intel/vmx_nested.h
@@ -84,6 +84,11 @@
void *bitmap_cookie[3];
uint8_t *msr_bitmap; /* L1's and the host's, for vmcs02 */
struct vmcs *vmcs02; /* the VMCS that L2 runs on */
+ uint16_t vpid02; /* the VPID of vmcs02, or 0 */
+ uint16_t vpid12; /* L1's VPID of the mappings it tags */
+ bool vpid_active; /* L2 runs with vpid02 */
+ bool vpid_flush; /* flush vpid02 before L2 runs */
+ int vpid_cpu; /* where L2 ran with vpid02 last */
uint64_t cr0_host; /* CR0/CR4 bits that vmcs02 owns but */
uint64_t cr4_host; /* L1's guest/host masks do not */
struct vmx_nested_insn insn;
@@ -94,6 +99,7 @@
struct vmx_nested *vmx_nested_alloc(void);
void vmx_nested_free(struct vmx_nested *nested);
int vmx_nested_rdmsr(u_int num, uint64_t *val);
+bool vmx_nested_vpid_flush(struct vmx_vcpu *vcpu);
bool vmx_nested_intr_exits(struct vmx_vcpu *vcpu);
uint32_t vmx_nested_event(struct vmx_vcpu *vcpu, bool nmi, bool intr,
int vector);
diff --git a/sys/amd64/vmm/intel/vmx_nested.c b/sys/amd64/vmm/intel/vmx_nested.c
--- a/sys/amd64/vmm/intel/vmx_nested.c
+++ b/sys/amd64/vmm/intel/vmx_nested.c
@@ -109,6 +109,9 @@
#define VMX_MISC_CR3_COUNT(m) (((m) >> 16) & 0x1ff)
#define VMX_MISC_INJECT_LEN0 (1U << 30)
+/* INVVPID and its four types (SDM A.10). */
+#define VMX_VPID_CAP_INVVPID (1UL << 32 | 0xfUL << 40)
+#define INVVPID_TYPE_MAX 3
#define VMCS_ACTIVITY_HLT 1
#define VMCS_INTR_RESERVED 0x7ffff000
@@ -170,6 +173,7 @@
#define VMXERR_READONLY_FIELD 13
#define VMXERR_VMXON_IN_ROOT 15
#define VMXERR_ENTRY_MOVSS 26
+#define VMXERR_INVEPT_INVVPID 28
#define EXIT_REASON_ENTRY_FAIL (1U << 31)
@@ -328,6 +332,7 @@
void
vmx_nested_init(void)
{
+ uint32_t proc_may;
u_int i, maxidx;
/*
@@ -341,9 +346,21 @@
VMX_BASIC_TRUE_CTLS);
nested_msr_set_ctls(MSR_VMX_PINBASED_CTLS, MSR_VMX_TRUE_PINBASED_CTLS,
PINBASED_DEFAULT1, PINBASED_L1_MUST, PINBASED_L1_MAY);
+ /*
+ * The secondary controls: only "enable VPID", if the CPU has it.
+ * All types of INVVPID are offered, as they all do the same here.
+ */
+ proc_may = PROCBASED_L1_MAY;
+ if (((rdmsr(MSR_VMX_PROCBASED_CTLS2) >> 32) &
+ PROCBASED2_ENABLE_VPID) != 0) {
+ proc_may |= PROCBASED_SECONDARY_CONTROLS;
+ nested_msr_set(MSR_VMX_PROCBASED_CTLS2,
+ (uint64_t)PROCBASED2_ENABLE_VPID << 32);
+ nested_msr_set(MSR_VMX_EPT_VPID_CAP, VMX_VPID_CAP_INVVPID);
+ }
nested_msr_set_ctls(MSR_VMX_PROCBASED_CTLS,
MSR_VMX_TRUE_PROCBASED_CTLS, PROCBASED_DEFAULT1,
- PROCBASED_L1_MUST, PROCBASED_L1_MAY);
+ PROCBASED_L1_MUST, proc_may);
nested_msr_set_ctls(MSR_VMX_EXIT_CTLS, MSR_VMX_TRUE_EXIT_CTLS,
VM_EXIT_DEFAULT1, VM_EXIT_L1_MUST, VM_EXIT_L1_MAY);
nested_msr_set_ctls(MSR_VMX_ENTRY_CTLS, MSR_VMX_TRUE_ENTRY_CTLS,
@@ -896,6 +913,16 @@
return (true);
}
+/* Has L1 set "enable VPID" for L2? */
+static bool
+nested_vpid(struct vmx_nested *nested)
+{
+ return ((V12(nested, VMCS_PRI_PROC_BASED_CTLS) &
+ PROCBASED_SECONDARY_CONTROLS) != 0 &&
+ (V12(nested, VMCS_SEC_PROC_BASED_CTLS) &
+ PROCBASED2_ENABLE_VPID) != 0);
+}
+
/*
* The fields for the event to inject (SDM 27.2.1.3). The CPU checks them
* too when it enters vmcs02, but that is after the checks on L2's state
@@ -977,8 +1004,14 @@
PROCBASED_NMI_WINDOW_EXITING) != 0 &&
(V12(nested, VMCS_PIN_BASED_CTLS) & PINBASED_VIRTUAL_NMI) == 0)
return (VMXERR_ENTRY_CTLS);
- /* The addresses of the bitmaps that are in use (SDM 27.2.1.1). */
+ /* The secondary controls; a VPID that is in use is not 0. */
proc = V12(nested, VMCS_PRI_PROC_BASED_CTLS);
+ if ((proc & PROCBASED_SECONDARY_CONTROLS) != 0 &&
+ (!nested_ctls_valid(V12(nested, VMCS_SEC_PROC_BASED_CTLS),
+ MSR_VMX_PROCBASED_CTLS2) || (nested_vpid(nested) &&
+ V12(nested, VMCS_VPID) == 0)))
+ return (VMXERR_ENTRY_CTLS);
+ /* The addresses of the bitmaps that are in use (SDM 27.2.1.1). */
if ((proc & PROCBASED_IO_BITMAPS) != 0 &&
(!nested_ptr_valid(V12(nested, VMCS_IO_BITMAP_A)) ||
!nested_ptr_valid(V12(nested, VMCS_IO_BITMAP_B))))
@@ -1167,6 +1200,23 @@
pin |= PINBASED_EXTINT_EXITING | PINBASED_NMI_EXITING;
vmcs_write(VMCS_PIN_BASED_CTLS, pin);
vmcs_write(VMCS_PRI_PROC_BASED_CTLS, proc);
+ /*
+ * Without "enable VPID" every VM entry and VM exit of L2 flushes
+ * the TLB. With it L2 runs with a VPID of its own, vpid02, which
+ * stands for all the VPIDs that L1 uses: what is cached for one of
+ * them must go when L1 comes with another one.
+ */
+ nested->vpid_active = nested_vpid(nested) && nested->vpid02 != 0;
+ if (nested->vpid_active) {
+ if (nested->vpid12 != V12(nested, VMCS_VPID)) {
+ nested->vpid12 = V12(nested, VMCS_VPID);
+ nested->vpid_flush = true;
+ }
+ vmcs_write(VMCS_VPID, nested->vpid02);
+ vmcs_write(VMCS_SEC_PROC_BASED_CTLS, PROCBASED2_ENABLE_EPT |
+ PROCBASED2_ENABLE_VPID);
+ } else
+ vmcs_write(VMCS_SEC_PROC_BASED_CTLS, PROCBASED2_ENABLE_EPT);
if ((proc & PROCBASED_MSR_BITMAPS) != 0)
vmcs_write(VMCS_MSR_BITMAP, vtophys(nested->msr_bitmap));
vmcs_write(VMCS_CR3_TARGET_COUNT, V12(nested, VMCS_CR3_TARGET_COUNT));
@@ -1336,7 +1386,7 @@
{
struct vmx_nested *nested = vcpu->nested;
const struct vmx_nested_insn *insn = &nested->insn;
- uint64_t enc, ptr, val;
+ uint64_t desc[2], enc, ptr, val;
uint32_t header[2], info, revision;
int size, slot;
@@ -1509,13 +1559,55 @@
case EXIT_REASON_VMRESUME:
nested_enter(vcpu, insn->reason == EXIT_REASON_VMLAUNCH);
break;
+ case EXIT_REASON_INVVPID:
+ /*
+ * SDM 31.3. The mappings of all of L1's VPIDs have the
+ * same tag, vpid02, so every type flushes all of them.
+ */
+ if (NESTED_MSR(MSR_VMX_EPT_VPID_CAP) == 0) {
+ vm_inject_ud(vcpu->vcpu);
+ break;
+ }
+ if (!nested_insn_mem(vcpu, desc, sizeof(desc), false))
+ break;
+ val = nested_get_gpr(vcpu, (info >> 28) & 0xf);
+ if (size == 4)
+ val &= 0xffffffff;
+ if (val > INVVPID_TYPE_MAX || (desc[0] >> 16) != 0 ||
+ (val != INVVPID_TYPE_ALL_CONTEXTS && desc[0] == 0) ||
+ (val == INVVPID_TYPE_ADDRESS &&
+ !nested_canonical(desc[1]))) {
+ nested_fail(vcpu, VMXERR_INVEPT_INVVPID);
+ break;
+ }
+ nested->vpid_flush = true;
+ nested_succeed(vcpu);
+ break;
default:
- /* VMCALL, INVEPT, INVVPID and VMFUNC. */
+ /* VMCALL, INVEPT and VMFUNC. */
vm_inject_ud(vcpu->vcpu);
break;
}
}
+/*
+ * L2 is about to run, on this CPU. Returns true if the mappings that are
+ * cached for its VPID must be flushed first: L1 has asked for it, or L2 ran
+ * on another CPU last and what this one has cached is old.
+ */
+bool
+vmx_nested_vpid_flush(struct vmx_vcpu *vcpu)
+{
+ struct vmx_nested *nested = vcpu->nested;
+
+ if (!nested->vpid_active ||
+ (!nested->vpid_flush && nested->vpid_cpu == curcpu))
+ return (false);
+ nested->vpid_flush = false;
+ nested->vpid_cpu = curcpu;
+ return (true);
+}
+
/* The segment registers in the order of their VMCS fields. */
enum {
SEG_ES,

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 21, 2:49 PM (20 h, 17 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41890789
Default Alt Text
D61796.1795272581.diff (8 KB)

Event Timeline