Page MenuHomeFreeBSD

D61797.1795272278.diff
No OneTemporary

Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None

D61797.1795272278.diff

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
@@ -70,6 +70,7 @@
#define NESTED_IO_BITMAP_A 0
#define NESTED_IO_BITMAP_B 1
#define NESTED_MSR_BITMAP 2
+#define NESTED_VAPIC_PAGE 3
struct vmx_nested {
bool vmxon; /* L1 is in VMX operation */
@@ -80,9 +81,10 @@
uint64_t vmptr; /* L1's current-VMCS pointer */
uint64_t rip; /* where L1 continues after L2 */
struct vmcs12 *vmcs12; /* copy of L1's current VMCS */
- uint8_t *bitmap[3]; /* L1's bitmaps while L2 runs */
- void *bitmap_cookie[3];
+ uint8_t *bitmap[4]; /* L1's pages while L2 runs */
+ void *bitmap_cookie[4];
uint8_t *msr_bitmap; /* L1's and the host's, for vmcs02 */
+ uint8_t *vapic; /* stands in for a page L1 has not */
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 */
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
@@ -114,6 +114,7 @@
#define INVVPID_TYPE_MAX 3
#define VMCS_ACTIVITY_HLT 1
#define VMCS_INTR_RESERVED 0x7ffff000
+#define VAPIC_TPR 0x80 /* in the virtual-APIC page */
/*
* The default1 controls, which the MSRs that are not the TRUE ones report as
@@ -149,7 +150,8 @@
PROCBASED_INVLPG_EXITING | PROCBASED_RDPMC_EXITING | \
PROCBASED_RDTSC_EXITING | PROCBASED_CR3_LOAD_EXITING | \
PROCBASED_CR3_STORE_EXITING | PROCBASED_NMI_WINDOW_EXITING | \
- PROCBASED_MOV_DR_EXITING | PROCBASED_PAUSE_EXITING)
+ PROCBASED_MOV_DR_EXITING | PROCBASED_PAUSE_EXITING | \
+ PROCBASED_USE_TPR_SHADOW)
#define VM_EXIT_L1_MUST VM_EXIT_HOST_LMA
#define VM_EXIT_L1_MAY \
(VM_EXIT_SAVE_DEBUG_CONTROLS | VM_EXIT_ACKNOWLEDGE_INTERRUPT | \
@@ -411,6 +413,8 @@
nested->vmcs12 = malloc(PAGE_SIZE, M_VMXNESTED, M_WAITOK | M_ZERO);
nested->msr_bitmap = malloc_aligned(PAGE_SIZE, PAGE_SIZE, M_VMXNESTED,
M_WAITOK | M_ZERO);
+ nested->vapic = malloc_aligned(PAGE_SIZE, PAGE_SIZE, M_VMXNESTED,
+ M_WAITOK | M_ZERO);
nested->vmptr = VMPTR_INVALID;
return (nested);
}
@@ -435,6 +439,7 @@
return;
nested_release_bitmaps(nested);
free(nested->msr_bitmap, M_VMXNESTED);
+ free(nested->vapic, M_VMXNESTED);
free(nested->vmcs12, M_VMXNESTED);
free(nested, M_VMXNESTED);
}
@@ -1011,6 +1016,11 @@
MSR_VMX_PROCBASED_CTLS2) || (nested_vpid(nested) &&
V12(nested, VMCS_VPID) == 0)))
return (VMXERR_ENTRY_CTLS);
+ /* The TPR shadow: the virtual-APIC page and the TPR threshold. */
+ if ((proc & PROCBASED_USE_TPR_SHADOW) != 0 &&
+ (!nested_ptr_valid(V12(nested, VMCS_VIRTUAL_APIC)) ||
+ (V12(nested, VMCS_TPR_THRESHOLD) & ~0xfUL) != 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)) ||
@@ -1033,6 +1043,29 @@
return (0);
}
+/*
+ * With the TPR shadow the TPR threshold must not be above the TPR that is
+ * in the virtual-APIC page (SDM 27.2.1.1; L1 has neither "virtualize APIC
+ * accesses" nor "virtual-interrupt delivery"). A page that is not memory
+ * reads as zero.
+ */
+static int
+nested_check_tpr(struct vmx_vcpu *vcpu)
+{
+ struct vmx_nested *nested = vcpu->nested;
+ uint32_t vtpr;
+
+ if ((V12(nested, VMCS_PRI_PROC_BASED_CTLS) &
+ PROCBASED_USE_TPR_SHADOW) == 0)
+ return (0);
+ if (!nested_copy(vcpu, V12(nested, VMCS_VIRTUAL_APIC) + VAPIC_TPR,
+ &vtpr, sizeof(vtpr), false))
+ vtpr = 0;
+ if ((V12(nested, VMCS_TPR_THRESHOLD) & 0xf) > ((vtpr >> 4) & 0xf))
+ return (VMXERR_ENTRY_CTLS);
+ return (0);
+}
+
/*
* The checks on the host-state area (SDM 27.2.2 to 27.2.4), which is what L1
* continues with after a VM exit of L2. L1 is in IA-32e mode, since "host
@@ -1159,6 +1192,12 @@
V12(nested, VMCS_IO_BITMAP_B), PAGE_SIZE, VM_PROT_READ,
&nested->bitmap_cookie[NESTED_IO_BITMAP_B]);
}
+ /* L2's CR8 is the TPR in L1's virtual-APIC page: the CPU does that. */
+ if ((proc & PROCBASED_USE_TPR_SHADOW) != 0) {
+ nested->bitmap[NESTED_VAPIC_PAGE] = vm_gpa_hold(vcpu->vcpu,
+ V12(nested, VMCS_VIRTUAL_APIC), PAGE_SIZE, VM_PROT_WRITE,
+ &nested->bitmap_cookie[NESTED_VAPIC_PAGE]);
+ }
if ((proc & PROCBASED_MSR_BITMAPS) != 0) {
bitmap = vm_gpa_hold(vcpu->vcpu, V12(nested, VMCS_MSR_BITMAP),
PAGE_SIZE, VM_PROT_READ,
@@ -1189,6 +1228,24 @@
proc &= ~PROCBASED_IO_BITMAPS;
proc |= PROCBASED_SECONDARY_CONTROLS | PROCBASED_L1_HOST |
(proc01 & PROCBASED_TSC_OFFSET);
+ if ((proc & PROCBASED_USE_TPR_SHADOW) != 0) {
+ /* CR8 is not L1's TPR then: only L1 can want these exits. */
+ proc &= ~(PROCBASED_CR8_LOAD_EXITING |
+ PROCBASED_CR8_STORE_EXITING);
+ proc |= V12(nested, VMCS_PRI_PROC_BASED_CTLS) &
+ (PROCBASED_CR8_LOAD_EXITING | PROCBASED_CR8_STORE_EXITING);
+ /*
+ * A CPU takes any address for the page. One that is not
+ * memory of the VM gets a page that nothing else uses.
+ */
+ if (nested->bitmap[NESTED_VAPIC_PAGE] != NULL)
+ vmcs_write(VMCS_VIRTUAL_APIC,
+ vtophys(nested->bitmap[NESTED_VAPIC_PAGE]));
+ else
+ vmcs_write(VMCS_VIRTUAL_APIC, vtophys(nested->vapic));
+ vmcs_write(VMCS_TPR_THRESHOLD,
+ V12(nested, VMCS_TPR_THRESHOLD));
+ }
/*
* The host needs the VM exits for interrupts and NMIs. Without "NMI
* exiting" L1 cannot use virtual NMIs: the host does, to find out
@@ -1337,6 +1394,8 @@
return;
}
error = nested_check_ctls(nested);
+ if (error == 0)
+ error = nested_check_tpr(vcpu);
if (error == 0)
error = nested_check_host_state(nested);
if (error != 0) {
@@ -1961,7 +2020,12 @@
case EXIT_REASON_HLT:
return ((proc & PROCBASED_HLT_EXITING) == 0);
case EXIT_REASON_CR_ACCESS:
- /* MOV to or from CR8: L1's TPR, which the host keeps. */
+ /*
+ * MOV to or from CR8: L1's TPR, which the host keeps, unless
+ * L2 has a TPR of its own in the virtual-APIC page.
+ */
+ if ((proc & PROCBASED_USE_TPR_SHADOW) != 0)
+ return (false);
return ((proc & ((qual & 0x10) != 0 ?
PROCBASED_CR8_STORE_EXITING :
PROCBASED_CR8_LOAD_EXITING)) == 0);

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 21, 2:44 PM (20 h, 12 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41890759
Default Alt Text
D61797.1795272278.diff (6 KB)

Event Timeline