Page MenuHomeFreeBSD

D61789.1795253224.diff
No OneTemporary

Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None

D61789.1795253224.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
@@ -3302,6 +3302,8 @@
break;
}
launched = 1;
+ /* For IA32_TSC in the VM-exit MSR-store area. */
+ exit->tsc = rdtsc();
reason = vmcs_read(VMCS_EXIT_REASON);
qual = vmcs_exit_qualification();
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
@@ -64,6 +64,7 @@
uint64_t qual;
uint64_t gla;
uint64_t gpa;
+ uint64_t tsc; /* host TSC at the VM exit */
};
#define NESTED_IO_BITMAP_A 0
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
@@ -172,6 +172,23 @@
#define EXIT_QUAL_ENTRY_NONE 0
#define EXIT_QUAL_ENTRY_PDPTE 2
+/* VMX-abort indicators (SDM 28.7). */
+#define VMX_ABORT_MSR_STORE 1
+#define VMX_ABORT_MSR_LOAD 4
+
+/* An entry of an MSR area (SDM table 25-15). */
+struct nested_msr_entry {
+ uint32_t index;
+ uint32_t reserved;
+ uint64_t value;
+};
+
+/*
+ * The most entries an area can have: 512 times one more than bits 27:25 of
+ * MSR_VMX_MISC, which are 0 for L1.
+ */
+#define NESTED_MSR_AREA_MAX 512
+
/* IA32_EFER bits that are not reserved on a CPU that has VMX. */
#define EFER_VMX_VALID (EFER_SCE | EFER_LME | EFER_LMA | EFER_NXE)
@@ -657,6 +674,208 @@
return ((int64_t)(addr << 16) >> 16 == (int64_t)addr);
}
+/*
+ * The MSR areas (SDM 25.7.2, 25.8.2): L1 gives lists of MSRs to load on VM
+ * entry, to store on VM exit and to load on VM exit. An area with entries
+ * must be aligned to 16 bytes and all of it must be at addresses that the
+ * CPU has.
+ */
+static bool
+nested_msr_area_valid(uint64_t addr, uint32_t count)
+{
+ if (count == 0)
+ return (true);
+ return ((addr & 0xf) == 0 && (addr >> cpu_maxphyaddr) == 0 &&
+ ((addr + (uint64_t)count * sizeof(struct nested_msr_entry) - 1) >>
+ cpu_maxphyaddr) == 0);
+}
+
+static int
+nested_msr_idx(u_int num)
+{
+ switch (num) {
+ case MSR_LSTAR:
+ return (IDX_MSR_LSTAR);
+ case MSR_CSTAR:
+ return (IDX_MSR_CSTAR);
+ case MSR_STAR:
+ return (IDX_MSR_STAR);
+ case MSR_SF_MASK:
+ return (IDX_MSR_SF_MASK);
+ case MSR_KGSBASE:
+ return (IDX_MSR_KGSBASE);
+ default:
+ return (-1);
+ }
+}
+
+static uint32_t
+nested_msr_field(u_int num)
+{
+ switch (num) {
+ case MSR_FSBASE:
+ return (VMCS_GUEST_FS_BASE);
+ case MSR_GSBASE:
+ return (VMCS_GUEST_GS_BASE);
+ case MSR_SYSENTER_CS_MSR:
+ return (VMCS_GUEST_IA32_SYSENTER_CS);
+ case MSR_SYSENTER_ESP_MSR:
+ return (VMCS_GUEST_IA32_SYSENTER_ESP);
+ case MSR_SYSENTER_EIP_MSR:
+ return (VMCS_GUEST_IA32_SYSENTER_EIP);
+ default:
+ return (0);
+ }
+}
+
+/*
+ * Write an MSR for an MSR area: for L2 while its VM entry is emulated, or
+ * for L1 after the VM exit. Only the MSRs that the guest writes without a
+ * VM exit, the ones that are in the VMCS and the TSC can be in an area.
+ * Any other MSR, or a value that WRMSR would not take, makes the load fail:
+ * the values are later given to the CPU as they are.
+ */
+static bool
+nested_msr_write(struct vmx_vcpu *vcpu, u_int num, uint64_t val, bool l2)
+{
+ struct vmx_nested *nested = vcpu->nested;
+ struct vmcs *vmcs;
+ uint64_t offset, old;
+ uint32_t field;
+ int error, idx;
+
+ switch (num) {
+ case MSR_SF_MASK:
+ if ((val >> 32) != 0)
+ return (false);
+ break;
+ case MSR_SYSENTER_CS_MSR:
+ val = (uint32_t)val;
+ break;
+ case MSR_STAR:
+ break;
+ case MSR_TSC:
+ /*
+ * There is one TSC: the write is L1's WRMSR. The offset of
+ * vmcs02 is L1's and the one L1 has set for L2.
+ */
+ offset = val - rdtsc();
+ critical_enter();
+ VMPTRLD(vcpu->vmcs);
+ old = (vcpu->cap.proc_ctls & PROCBASED_TSC_OFFSET) != 0 ?
+ vmcs_read(VMCS_TSC_OFFSET) : 0;
+ error = vmx_set_tsc_offset(vcpu, offset);
+ VMCLEAR(vcpu->vmcs);
+ if (error == 0 && l2) {
+ VMPTRLD(nested->vmcs02);
+ vmcs_write(VMCS_PRI_PROC_BASED_CTLS,
+ vmcs_read(VMCS_PRI_PROC_BASED_CTLS) |
+ PROCBASED_TSC_OFFSET);
+ vmcs_write(VMCS_TSC_OFFSET,
+ vmcs_read(VMCS_TSC_OFFSET) + offset - old);
+ VMCLEAR(nested->vmcs02);
+ }
+ critical_exit();
+ return (error == 0);
+ default:
+ if (!nested_canonical(val))
+ return (false);
+ break;
+ }
+ idx = nested_msr_idx(num);
+ if (idx >= 0) {
+ /* L1 and L2 share these; vmx_msr_guest_enter() loads them. */
+ vcpu->guest_msrs[idx] = val;
+ return (true);
+ }
+ field = nested_msr_field(num);
+ if (field == 0)
+ return (false);
+ vmcs = l2 ? nested->vmcs02 : vcpu->vmcs;
+ critical_enter();
+ VMPTRLD(vmcs);
+ vmcs_write(field, val);
+ VMCLEAR(vmcs);
+ critical_exit();
+ return (true);
+}
+
+/* Read an MSR of L2 for the VM-exit MSR-store area; L2's state is in vmcs12. */
+static bool
+nested_msr_read(struct vmx_vcpu *vcpu, u_int num, uint64_t *val)
+{
+ struct vmx_nested *nested = vcpu->nested;
+ uint32_t field;
+ int idx;
+
+ if (num == MSR_TSC) {
+ /*
+ * The MSR at the time of the VM exit, not what RDTSC gives
+ * L2: without the offset that L1 has set for L2.
+ */
+ *val = 0;
+ if ((vcpu->cap.proc_ctls & PROCBASED_TSC_OFFSET) != 0) {
+ critical_enter();
+ VMPTRLD(vcpu->vmcs);
+ *val = vmcs_read(VMCS_TSC_OFFSET);
+ VMCLEAR(vcpu->vmcs);
+ critical_exit();
+ }
+ *val += nested->exit.tsc;
+ return (true);
+ }
+ idx = nested_msr_idx(num);
+ if (idx >= 0) {
+ *val = vcpu->guest_msrs[idx];
+ return (true);
+ }
+ field = nested_msr_field(num);
+ if (field == 0)
+ return (false);
+ *val = V12(nested, field);
+ return (true);
+}
+
+/*
+ * Load the MSRs of an area (SDM 27.4, 28.6). Returns 0, or the number of
+ * the entry that could not be loaded, counted from 1.
+ */
+static u_int
+nested_msr_load(struct vmx_vcpu *vcpu, uint64_t addr, uint32_t count, bool l2)
+{
+ struct nested_msr_entry entry;
+ u_int i;
+
+ for (i = 0; i < count; i++) {
+ if (i == NESTED_MSR_AREA_MAX ||
+ !nested_copy(vcpu, addr + i * sizeof(entry), &entry,
+ sizeof(entry), false) || entry.reserved != 0 ||
+ entry.index == MSR_FSBASE || entry.index == MSR_GSBASE ||
+ !nested_msr_write(vcpu, entry.index, entry.value, l2))
+ return (i + 1);
+ }
+ return (0);
+}
+
+/* Store L2's MSRs in the VM-exit MSR-store area (SDM 28.4). */
+static bool
+nested_msr_store(struct vmx_vcpu *vcpu, uint64_t addr, uint32_t count)
+{
+ struct nested_msr_entry entry;
+ u_int i;
+
+ for (i = 0; i < count; i++) {
+ if (i == NESTED_MSR_AREA_MAX ||
+ !nested_copy(vcpu, addr + i * sizeof(entry), &entry,
+ sizeof(entry), false) || entry.reserved != 0 ||
+ !nested_msr_read(vcpu, entry.index, &entry.value) ||
+ !nested_copy(vcpu, addr + i * sizeof(entry), &entry,
+ sizeof(entry), true))
+ return (false);
+ }
+ return (true);
+}
+
/*
* The checks on the VMX controls (SDM 27.2.1), as far as the controls are
* offered to L1. The fields for the event to inject are checked by the CPU
@@ -690,15 +909,14 @@
if ((proc & PROCBASED_MSR_BITMAPS) != 0 &&
!nested_ptr_valid(V12(nested, VMCS_MSR_BITMAP)))
return (VMXERR_ENTRY_CTLS);
- /*
- * The MSR areas are not implemented: this is where the emulation
- * differs from a CPU, which always has them.
- */
if (V12(nested, VMCS_CR3_TARGET_COUNT) >
VMX_MISC_CR3_COUNT(NESTED_MSR(MSR_VMX_MISC)) ||
- V12(nested, VMCS_EXIT_MSR_STORE_COUNT) != 0 ||
- V12(nested, VMCS_EXIT_MSR_LOAD_COUNT) != 0 ||
- V12(nested, VMCS_ENTRY_MSR_LOAD_COUNT) != 0)
+ !nested_msr_area_valid(V12(nested, VMCS_EXIT_MSR_STORE),
+ V12(nested, VMCS_EXIT_MSR_STORE_COUNT)) ||
+ !nested_msr_area_valid(V12(nested, VMCS_EXIT_MSR_LOAD),
+ V12(nested, VMCS_EXIT_MSR_LOAD_COUNT)) ||
+ !nested_msr_area_valid(V12(nested, VMCS_ENTRY_MSR_LOAD),
+ V12(nested, VMCS_ENTRY_MSR_LOAD_COUNT)))
return (VMXERR_ENTRY_CTLS);
return (0);
}
@@ -911,12 +1129,12 @@
* exit reason, which leaves L2's state in vmcs12 as it is (SDM 27.8).
*/
static void
-nested_entry_failure(struct vmx_vcpu *vcpu, uint64_t qual)
+nested_entry_failure(struct vmx_vcpu *vcpu, uint32_t reason, uint64_t qual)
{
struct vmx_nested_l2exit *exit = &vcpu->nested->exit;
memset(exit, 0, sizeof(*exit));
- exit->reason = EXIT_REASON_INVAL_VMCS | EXIT_REASON_ENTRY_FAIL;
+ exit->reason = reason | EXIT_REASON_ENTRY_FAIL;
exit->qual = qual;
nested_reflect(vcpu);
}
@@ -929,6 +1147,7 @@
nested_enter(struct vmx_vcpu *vcpu, bool launch)
{
struct vmx_nested *nested = vcpu->nested;
+ u_int i;
int error;
if (nested->vmptr == VMPTR_INVALID) {
@@ -965,11 +1184,19 @@
if (V12(nested, VMCS_GUEST_ACTIVITY) > VMCS_ACTIVITY_HLT ||
(V12(nested, VMCS_GUEST_ACTIVITY) == VMCS_ACTIVITY_HLT &&
(NESTED_MSR(MSR_VMX_MISC) & VMX_MISC_ACTIVITY_HLT) == 0)) {
- nested_entry_failure(vcpu, EXIT_QUAL_ENTRY_NONE);
+ nested_entry_failure(vcpu, EXIT_REASON_INVAL_VMCS,
+ EXIT_QUAL_ENTRY_NONE);
return;
}
if (nested_vmcs02_load(vcpu) != 0) {
- nested_entry_failure(vcpu, EXIT_QUAL_ENTRY_PDPTE);
+ nested_entry_failure(vcpu, EXIT_REASON_INVAL_VMCS,
+ EXIT_QUAL_ENTRY_PDPTE);
+ return;
+ }
+ i = nested_msr_load(vcpu, V12(nested, VMCS_ENTRY_MSR_LOAD),
+ V12(nested, VMCS_ENTRY_MSR_LOAD_COUNT), true);
+ if (i != 0) {
+ nested_entry_failure(vcpu, EXIT_REASON_INVAL_MSR, i);
return;
}
nested->launching = launch;
@@ -1296,9 +1523,10 @@
struct vmx_nested *nested = vcpu->nested;
const struct vmx_nested_l2exit *exit = &nested->exit;
uint64_t cr0, cr4, efer;
- uint32_t exit12;
+ uint32_t abort, exit12;
u_int i;
+ abort = 0;
/* A VM-entry failure leaves L2's state in vmcs12 as it is. */
if ((exit->reason & EXIT_REASON_ENTRY_FAIL) == 0) {
exit12 = V12(nested, VMCS_EXIT_CTLS);
@@ -1333,6 +1561,9 @@
V12(nested, VMCS_ENTRY_INTR_INFO) &= ~(uint64_t)VMCS_INTR_VALID;
if (nested->launching)
nested->vmcs12->launched = 1;
+ if (!nested_msr_store(vcpu, V12(nested, VMCS_EXIT_MSR_STORE),
+ V12(nested, VMCS_EXIT_MSR_STORE_COUNT)))
+ abort = VMX_ABORT_MSR_STORE;
}
V12(nested, VMCS_EXIT_REASON) = exit->reason;
V12(nested, VMCS_EXIT_QUALIFICATION) = exit->qual;
@@ -1349,6 +1580,18 @@
nested->l2 = false;
nested_release_bitmaps(nested);
nested_load_host_state(vcpu);
+ if (abort == 0 && nested_msr_load(vcpu, V12(nested, VMCS_EXIT_MSR_LOAD),
+ V12(nested, VMCS_EXIT_MSR_LOAD_COUNT), false) != 0)
+ abort = VMX_ABORT_MSR_LOAD;
+ if (abort != 0) {
+ /*
+ * A VMX abort (SDM 28.7): the CPU would write the reason to
+ * the VMCS and go to the shutdown state.
+ */
+ nested->vmcs12->abort = abort;
+ nested_vmcs12_flush(vcpu);
+ (void)vm_suspend(vcpu->vmx->vm, VM_SUSPEND_TRIPLEFAULT);
+ }
}
/* vmx_run() has left L2. */

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 21, 9:27 AM (14 h, 54 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41886468
Default Alt Text
D61789.1795253224.diff (10 KB)

Event Timeline