Page MenuHomeFreeBSD

D61791.1795277365.diff
No OneTemporary

Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None

D61791.1795277365.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
@@ -3156,10 +3156,10 @@
struct region_descriptor gdtr, idtr;
uint64_t entryinfo, gpa, qual;
register_t l2_rip;
- uint32_t idtvec_info, intr_info, reason;
+ uint32_t event, idtvec_info, intr_info, reason;
uint16_t ldt_sel;
- int launched;
- bool host, nmi, set_rip;
+ int launched, vector;
+ bool host, intr, nmi, set_rip;
vmx = vcpu->vmx;
vmxctx = &vcpu->ctx;
@@ -3253,25 +3253,26 @@
}
/*
- * An NMI or an interrupt for L1 is a VM exit of L2. An event
- * that is to be injected into L2 is delivered first: L2 is
- * entered with an interrupt for the host pending, so that it
- * has a VM exit right after the delivery.
+ * An NMI or an interrupt for L1 is a VM exit of L2, if L1
+ * has asked for it; if not, L2 gets it. An event that is
+ * to be injected into L2 is delivered before the VM exit:
+ * L2 is entered with an interrupt for the host pending, so
+ * that it has a VM exit right after the delivery.
*/
nmi = vm_nmi_pending(vcpu->vcpu);
- if (nmi || vlapic_pending_intr(vlapic, NULL)) {
+ intr = vlapic_pending_intr(vlapic, &vector);
+ event = nmi || intr ?
+ vmx_nested_event(vcpu, nmi, intr, vector) : 0;
+ if (event != 0) {
if ((vmcs_read(VMCS_ENTRY_INTR_INFO) &
VMCS_INTR_VALID) != 0) {
ipi_cpu(curcpu, IPI_AST);
} else {
enable_intr();
- if (nmi) {
- exit->reason = EXIT_REASON_EXCEPTION;
+ exit->reason = event;
+ if (event == EXIT_REASON_EXCEPTION)
exit->intr_info = VMCS_INTR_VALID |
VMCS_INTR_T_NMI | IDT_NMI;
- } else {
- exit->reason = EXIT_REASON_EXT_INTR;
- }
vmexit->exitcode = VM_EXITCODE_VMINSN;
break;
}
@@ -3335,8 +3336,8 @@
case EXIT_REASON_WRMSR:
/*
* Without L1 asking for the VM exit this is emulated
- * as it is for L1. An interrupt for L1 is a VM exit
- * of L2 whatever its RFLAGS.IF: it ends the HLT.
+ * as it is for L1. An interrupt for L1 that is a VM
+ * exit of L2 ends the HLT whatever L2's RFLAGS.IF.
*/
if (!vmx_nested_host_exit(vcpu, reason, qual))
break;
@@ -3344,7 +3345,8 @@
vmexit->inst_length = vmexit_instruction_length();
vmexit->u.vmx.exit_qualification = qual;
if (vmx_exit_process(vmx, vcpu, vmexit) == 0) {
- if (reason == EXIT_REASON_HLT)
+ if (reason == EXIT_REASON_HLT &&
+ vmx_nested_intr_exits(vcpu))
vmexit->u.hlt.rflags |= PSL_I;
nested->l2_rip = true;
} else {
@@ -3353,6 +3355,10 @@
}
host = true;
break;
+ case EXIT_REASON_INTR_WINDOW:
+ case EXIT_REASON_NMI_WINDOW:
+ host = vmx_nested_window_exit(vcpu, reason);
+ break;
case EXIT_REASON_CR_ACCESS:
host = vmx_nested_cr_exit(vcpu, qual);
break;
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
@@ -94,6 +94,10 @@
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_intr_exits(struct vmx_vcpu *vcpu);
+uint32_t vmx_nested_event(struct vmx_vcpu *vcpu, bool nmi, bool intr,
+ int vector);
+bool vmx_nested_window_exit(struct vmx_vcpu *vcpu, uint32_t reason);
bool vmx_nested_host_exit(struct vmx_vcpu *vcpu, uint32_t reason,
uint64_t qual);
bool vmx_nested_cr_exit(struct vmx_vcpu *vcpu, uint64_t qual);
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
@@ -125,10 +125,13 @@
* needs L2 to have in any case. The exceptions are PROCBASED_L1_HOST: L2
* has these exits whether L1 wants them or not, and the host emulates the
* instruction as it does for L1 when L1 does not want the exit. The same
- * goes for the I/O and MSR bitmaps: the host looks at L1's.
+ * goes for the I/O and MSR bitmaps: the host looks at L1's. L2 also has
+ * a VM exit for every interrupt and NMI: one that is for L1 is injected into
+ * L2 when L1 has not set "external-interrupt exiting" or "NMI exiting".
*/
-#define PINBASED_L1_MUST (PINBASED_EXTINT_EXITING | PINBASED_NMI_EXITING)
-#define PINBASED_L1_MAY PINBASED_VIRTUAL_NMI
+#define PINBASED_L1_MUST 0
+#define PINBASED_L1_MAY \
+ (PINBASED_EXTINT_EXITING | PINBASED_NMI_EXITING | PINBASED_VIRTUAL_NMI)
#define PROCBASED_L1_MUST \
(PROCBASED_MWAIT_EXITING | PROCBASED_MONITOR_EXITING | \
PROCBASED_CR8_LOAD_EXITING | PROCBASED_CR8_STORE_EXITING)
@@ -895,7 +898,13 @@
!nested_ctls_valid(V12(nested, VMCS_ENTRY_CTLS),
MSR_VMX_TRUE_ENTRY_CTLS))
return (VMXERR_ENTRY_CTLS);
- /* "NMI-window exiting" needs "virtual NMIs" (SDM 27.2.1.1). */
+ /*
+ * "Virtual NMIs" needs "NMI exiting" and "NMI-window exiting" needs
+ * "virtual NMIs" (SDM 27.2.1.1).
+ */
+ if ((V12(nested, VMCS_PIN_BASED_CTLS) & PINBASED_VIRTUAL_NMI) != 0 &&
+ (V12(nested, VMCS_PIN_BASED_CTLS) & PINBASED_NMI_EXITING) == 0)
+ return (VMXERR_ENTRY_CTLS);
if ((V12(nested, VMCS_PRI_PROC_BASED_CTLS) &
PROCBASED_NMI_WINDOW_EXITING) != 0 &&
(V12(nested, VMCS_PIN_BASED_CTLS) & PINBASED_VIRTUAL_NMI) == 0)
@@ -983,7 +992,7 @@
struct vmx_nested *nested = vcpu->nested;
uint64_t cr, debugctl, dr7, efer, mask, mask12, pdpte[4], tsc_offset;
uint8_t *bitmap;
- uint32_t entry12, proc, proc01;
+ uint32_t entry12, pin, proc, proc01;
u_int i;
entry12 = V12(nested, VMCS_ENTRY_CTLS);
@@ -1047,7 +1056,16 @@
proc &= ~PROCBASED_IO_BITMAPS;
proc |= PROCBASED_SECONDARY_CONTROLS | PROCBASED_L1_HOST |
(proc01 & PROCBASED_TSC_OFFSET);
- vmcs_write(VMCS_PIN_BASED_CTLS, V12(nested, VMCS_PIN_BASED_CTLS));
+ /*
+ * The host needs the VM exits for interrupts and NMIs. Without "NMI
+ * exiting" L1 cannot use virtual NMIs: the host does, to find out
+ * when L2 can take an NMI. For L2 the blocking is the same.
+ */
+ pin = V12(nested, VMCS_PIN_BASED_CTLS);
+ if ((pin & PINBASED_NMI_EXITING) == 0)
+ pin |= PINBASED_VIRTUAL_NMI;
+ pin |= PINBASED_EXTINT_EXITING | PINBASED_NMI_EXITING;
+ vmcs_write(VMCS_PIN_BASED_CTLS, pin);
vmcs_write(VMCS_PRI_PROC_BASED_CTLS, proc);
if ((proc & PROCBASED_MSR_BITMAPS) != 0)
vmcs_write(VMCS_MSR_BITMAP, vtophys(nested->msr_bitmap));
@@ -1651,6 +1669,84 @@
}
}
+/* Is an interrupt for L1 a VM exit of L2? */
+bool
+vmx_nested_intr_exits(struct vmx_vcpu *vcpu)
+{
+ return ((V12(vcpu->nested, VMCS_PIN_BASED_CTLS) &
+ PINBASED_EXTINT_EXITING) != 0);
+}
+
+/*
+ * An NMI or an interrupt is pending for L1 and L2 is about to run; vmcs02
+ * is the current VMCS. Returns the reason of the VM exit that L2 has to
+ * have, or 0: without "NMI exiting" or "external-interrupt exiting" L2 gets
+ * the event, as it would on a CPU (SDM 26.2, 27.6). If L2 cannot take it
+ * now, it has a VM exit that only the host sees when it can.
+ */
+uint32_t
+vmx_nested_event(struct vmx_vcpu *vcpu, bool nmi, bool intr, int vector)
+{
+ struct vmx_nested *nested = vcpu->nested;
+ uint32_t gi, info, pin, proc, window;
+
+ pin = V12(nested, VMCS_PIN_BASED_CTLS);
+ if (nmi && (pin & PINBASED_NMI_EXITING) != 0)
+ return (EXIT_REASON_EXCEPTION);
+ if (intr && (pin & PINBASED_EXTINT_EXITING) != 0)
+ return (EXIT_REASON_EXT_INTR);
+
+ info = vmcs_read(VMCS_ENTRY_INTR_INFO);
+ gi = vmcs_read(VMCS_GUEST_INTERRUPTIBILITY);
+ window = 0;
+ if (nmi) {
+ if ((info & VMCS_INTR_VALID) == 0 &&
+ (gi & (VMCS_INTERRUPTIBILITY_NMI_BLOCKING |
+ VMCS_INTERRUPTIBILITY_STI_BLOCKING |
+ VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)) == 0) {
+ info = VMCS_INTR_VALID | VMCS_INTR_T_NMI | IDT_NMI;
+ vmcs_write(VMCS_ENTRY_INTR_INFO, info);
+ vm_nmi_clear(vcpu->vcpu);
+ } else
+ window |= PROCBASED_NMI_WINDOW_EXITING;
+ }
+ if (intr) {
+ if ((info & VMCS_INTR_VALID) == 0 &&
+ (vmcs_read(VMCS_GUEST_RFLAGS) & PSL_I) != 0 &&
+ (gi & (VMCS_INTERRUPTIBILITY_STI_BLOCKING |
+ VMCS_INTERRUPTIBILITY_MOVSS_BLOCKING)) == 0) {
+ vlapic_intr_accepted(vm_lapic(vcpu->vcpu), vector);
+ vmcs_write(VMCS_ENTRY_INTR_INFO, VMCS_INTR_VALID |
+ VMCS_INTR_T_HWINTR | vector);
+ } else
+ window |= PROCBASED_INT_WINDOW_EXITING;
+ }
+ if (window != 0) {
+ proc = vmcs_read(VMCS_PRI_PROC_BASED_CTLS);
+ if ((proc & window) != window)
+ vmcs_write(VMCS_PRI_PROC_BASED_CTLS, proc | window);
+ }
+ return (0);
+}
+
+/*
+ * L2 can take an interrupt or an NMI. Without L1 having asked for this VM
+ * exit it is the one that vmx_nested_event() has set up: the host goes on.
+ */
+bool
+vmx_nested_window_exit(struct vmx_vcpu *vcpu, uint32_t reason)
+{
+ uint32_t bit;
+
+ bit = reason == EXIT_REASON_NMI_WINDOW ?
+ PROCBASED_NMI_WINDOW_EXITING : PROCBASED_INT_WINDOW_EXITING;
+ if ((V12(vcpu->nested, VMCS_PRI_PROC_BASED_CTLS) & bit) != 0)
+ return (false);
+ vmcs_write(VMCS_PRI_PROC_BASED_CTLS,
+ vmcs_read(VMCS_PRI_PROC_BASED_CTLS) & ~bit);
+ return (true);
+}
+
/*
* L2 has had a VM exit that the host forces: is it one that L1 did not ask
* for, so that the host has to emulate the instruction (SDM 26.1.3)?

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 21, 4:09 PM (21 h, 37 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41886822
Default Alt Text
D61791.1795277365.diff (8 KB)

Event Timeline