Page MenuHomeFreeBSD

D61802.1795272048.diff
No OneTemporary

Authored By
Unknown
Size
5 KB
Referenced Files
None
Subscribers
None

D61802.1795272048.diff

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
@@ -122,12 +122,12 @@
#define VMX_VPID_CAP_INVVPID (1UL << 32 | 0xfUL << 40)
/*
* What L1 is told about EPT (SDM A.10): a page walk of 4 levels, the
- * write-back type for the tables, 2 MB pages, INVEPT with both types.
- * Not offered: execute-only, 1 GB pages, accessed and dirty flags.
+ * write-back type for the tables, 2 MB pages, accessed and dirty flags,
+ * INVEPT with both types. Not offered: execute-only, 1 GB pages.
*/
#define VMX_EPT_CAP_INVEPT (1UL << 20)
#define VMX_EPT_CAP (1UL << 6 | 1UL << 14 | 1UL << 16 | \
- VMX_EPT_CAP_INVEPT | 1UL << 25 | 1UL << 26)
+ VMX_EPT_CAP_INVEPT | 1UL << 21 | 1UL << 25 | 1UL << 26)
/* The bits of an EPT entry (SDM 29.3.2). */
#define EPT_R 0x001
@@ -143,6 +143,7 @@
#define EPT_INDEX(gpa, lvl) (((gpa) >> (12 + 9 * (lvl))) & 0x1ff)
/* An EPTP for 4 levels and the write-back type, without A/D flags. */
#define EPTP_FLAGS (3 << 3 | 6)
+#define EPTP_AD (1 << 6)
#define INVVPID_TYPE_MAX 3
#define VMCS_ACTIVITY_HLT 1
#define VMCS_INTR_RESERVED 0x7ffff000
@@ -790,7 +791,8 @@
static bool
nested_eptp_valid(uint64_t eptp)
{
- return ((eptp & 0xfff) == EPTP_FLAGS && (eptp >> cpu_maxphyaddr) == 0);
+ return ((eptp & (0xfff & ~EPTP_AD)) == EPTP_FLAGS &&
+ (eptp >> cpu_maxphyaddr) == 0);
}
static bool
@@ -959,11 +961,12 @@
* Walk L1's tables for L2's address 'gpa' as the CPU does (SDM 29.3.2,
* 29.3.3): an entry that is not present ends the walk with an EPT
* violation, one that is set up wrongly, for what L1 is offered, with an
- * EPT misconfiguration. '*perm' is what all the entries allow.
+ * EPT misconfiguration. '*perm' is what all the entries allow, '*leafp'
+ * the last entry.
*/
static enum nested_ept_walk
nested_ept12_walk(struct vmx_vcpu *vcpu, uint64_t eptp, uint64_t gpa,
- uint64_t *gpa1, int *perm)
+ uint64_t *gpa1, int *perm, uint64_t *leafp)
{
uint64_t entry, mask, rsvd, table;
int lvl, type;
@@ -994,6 +997,7 @@
(EPT_ADDR(entry) & mask) != 0)
return (EPT12_MISCONFIG);
*gpa1 = EPT_ADDR(entry) | (gpa & mask);
+ *leafp = entry;
return (EPT12_OK);
}
table = EPT_ADDR(entry);
@@ -1001,17 +1005,50 @@
return (EPT12_MISCONFIG);
}
+/*
+ * With the accessed and dirty flags for EPT (SDM 29.3.5), a translation
+ * that is used sets the accessed flag in all its entries, and a write sets
+ * the dirty flag in the last one. L1 may be at the same entries on another
+ * vCPU.
+ */
+static void
+nested_ept12_set_ad(struct vmx_vcpu *vcpu, uint64_t eptp, uint64_t gpa,
+ bool dirty)
+{
+ uint64_t *entry, table, val;
+ void *cookie;
+ int lvl;
+ bool leaf;
+
+ table = EPT_ADDR(eptp);
+ for (lvl = 3; lvl >= 0; lvl--) {
+ entry = vm_gpa_hold(vcpu->vcpu, table + 8 * EPT_INDEX(gpa, lvl),
+ sizeof(*entry), VM_PROT_WRITE, &cookie);
+ if (entry == NULL)
+ return;
+ val = *entry;
+ leaf = lvl == 0 || (lvl == 1 && (val & EPT_PS) != 0);
+ if ((val & EPT_RWX) != 0)
+ atomic_set_long(entry, EPT_A |
+ (leaf && dirty ? EPT_D : 0));
+ vm_gpa_release(cookie);
+ if (leaf || (val & EPT_RWX) == 0)
+ return;
+ table = EPT_ADDR(val);
+ }
+}
+
static void nested_reflect(struct vmx_vcpu *vcpu);
/* Read memory of L2 at its address 'gpa': through L1's tables. */
static bool
nested_l2_read(struct vmx_vcpu *vcpu, uint64_t gpa, void *buf, size_t len)
{
- uint64_t gpa1;
+ uint64_t gpa1, leaf;
int perm;
return (nested_ept12_walk(vcpu, vcpu->nested->ept->eptp12, gpa,
- &gpa1, &perm) == EPT12_OK && (perm & EPT_R) != 0 &&
+ &gpa1, &perm, &leaf) == EPT12_OK && (perm & EPT_R) != 0 &&
nested_copy(vcpu, gpa1, buf, len, false));
}
@@ -1163,14 +1200,22 @@
struct vmx_nested_l2exit *exit = &nested->exit;
struct nested_ept *ept = nested->ept;
pmap_t pmap = vcpu->vmx->pmap;
- uint64_t ad, gpa1, *leaf, old, pte, pte01;
+ uint64_t ad, gpa1, *leaf, leaf12, old, pte, pte01;
vm_paddr_t pa;
long gen;
int need, perm, tries;
+ bool ad12;
+ /*
+ * With the accessed and dirty flags the CPU's own access to one of
+ * L2's page tables counts as a write (SDM 29.3.5).
+ */
+ ad12 = (ept->eptp12 & EPTP_AD) != 0;
need = exit->qual & EPT_RWX;
+ if (ad12 && (exit->qual & (1 << 7 | 1 << 8)) == 1 << 7)
+ need |= EPT_W;
switch (nested_ept12_walk(vcpu, ept->eptp12, exit->gpa, &gpa1,
- &perm)) {
+ &perm, &leaf12)) {
case EPT12_MISCONFIG:
exit->reason = EXIT_REASON_EPT_MISCONFIG;
exit->qual = 0;
@@ -1186,11 +1231,18 @@
* SDM table 28-7: the access, what L1's tables allow, and
* the bits about the linear address and NMI unblocking.
*/
- exit->qual = (exit->qual & (EPT_RWX | 1 << 7 | 1 << 8 |
- EXIT_QUAL_NMIUDTI)) | (uint64_t)perm << 3;
+ exit->qual = (exit->qual & (1 << 7 | 1 << 8 |
+ EXIT_QUAL_NMIUDTI)) | need | (uint64_t)perm << 3;
nested_reflect(vcpu);
return;
}
+ if (ad12) {
+ nested_ept12_set_ad(vcpu, ept->eptp12, exit->gpa,
+ (need & EPT_W) != 0);
+ /* The first write has to come here to set the dirty flag. */
+ if ((need & EPT_W) == 0 && (leaf12 & EPT_D) == 0)
+ perm &= ~EPT_W;
+ }
/* Read the generation before EPT01 itself. */
gen = atomic_load_acq_long(&pmap->pm_eptgen);

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 21, 2:40 PM (20 h, 9 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41890854
Default Alt Text
D61802.1795272048.diff (5 KB)

Event Timeline