Page MenuHomeFreeBSD

D61800.1795272451.diff
No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None

D61800.1795272451.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
@@ -2180,6 +2180,25 @@
vie_init(&vmexit->u.inst_emul.vie, NULL, 0);
}
+/*
+ * Make the exit for the emulation of the instruction that L2 is at, from
+ * vmcs02. The vCPU is not running. Returns L2's %rip.
+ */
+uint64_t
+vmx_nested_inst_emul(struct vmx_vcpu *vcpu, struct vm_exit *vmexit,
+ uint64_t gpa, uint64_t gla)
+{
+ uint64_t rip;
+
+ critical_enter();
+ VMPTRLD(vcpu->nested->vmcs02);
+ rip = vmcs_guest_rip();
+ vmexit_inst_emul(vmexit, gpa, gla);
+ VMCLEAR(vcpu->nested->vmcs02);
+ critical_exit();
+ return (rip);
+}
+
static int
ept_fault_type(uint64_t ept_qual)
{
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
@@ -108,6 +108,8 @@
int vmx_nested_rdmsr(u_int num, uint64_t *val);
bool vmx_nested_vpid_flush(struct vmx_vcpu *vcpu);
bool vmx_nested_ept(struct vmx_vcpu *vcpu);
+uint64_t vmx_nested_inst_emul(struct vmx_vcpu *vcpu, struct vm_exit *vmexit,
+ uint64_t gpa, uint64_t gla);
void vmx_nested_ept_enter(struct vmx_vcpu *vcpu, pmap_t pmap);
bool vmx_nested_intr_exits(struct vmx_vcpu *vcpu);
uint32_t vmx_nested_event(struct vmx_vcpu *vcpu, bool nmi, bool intr,
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
@@ -89,6 +89,7 @@
#include <machine/specialreg.h>
#include <machine/vmparam.h>
#include <machine/vmm.h>
+#include <machine/vmm_instruction_emul.h>
#include <dev/vmm/vmm_mem.h>
#include <dev/vmm/vmm_vm.h>
@@ -1002,6 +1003,155 @@
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;
+ int perm;
+
+ return (nested_ept12_walk(vcpu, vcpu->nested->ept->eptp12, gpa,
+ &gpa1, &perm) == EPT12_OK && (perm & EPT_R) != 0 &&
+ nested_copy(vcpu, gpa1, buf, len, false));
+}
+
+/*
+ * L2's address for its linear address 'gla': a walk of L2's page tables,
+ * which are in L2's memory. It is for the instruction that L2 is at, so
+ * nothing is checked but that the entries are present, and no flags are
+ * set.
+ */
+static bool
+nested_l2_gla2gpa(struct vmx_vcpu *vcpu, const struct vm_guest_paging *paging,
+ uint64_t gla, uint64_t *gpa)
+{
+ uint64_t entry, table;
+ uint32_t entry32;
+ int lvl, shift;
+
+ switch (paging->paging_mode) {
+ case PAGING_MODE_FLAT:
+ *gpa = gla;
+ return (true);
+ case PAGING_MODE_32:
+ table = paging->cr3 & 0xfffff000;
+ for (lvl = 1; lvl >= 0; lvl--) {
+ shift = 12 + 10 * lvl;
+ if (!nested_l2_read(vcpu, table +
+ 4 * ((gla >> shift) & 0x3ff), &entry32,
+ sizeof(entry32)) || (entry32 & PG_V) == 0)
+ return (false);
+ if (lvl == 0 || (entry32 & PG_PS) != 0) {
+ /* A 4 MB page: bits 39:32 are in 20:13. */
+ if (lvl != 0)
+ *gpa = (entry32 & 0xffc00000) |
+ (uint64_t)(entry32 & 0x1fe000) <<
+ 19;
+ else
+ *gpa = entry32 & 0xfffff000;
+ *gpa |= gla & ((1UL << shift) - 1);
+ return (true);
+ }
+ table = entry32 & 0xfffff000;
+ }
+ return (false);
+ case PAGING_MODE_PAE:
+ if (!nested_l2_read(vcpu, (paging->cr3 & 0xffffffe0) +
+ 8 * ((gla >> 30) & 0x3), &entry, sizeof(entry)) ||
+ (entry & PG_V) == 0)
+ return (false);
+ table = EPT_ADDR(entry);
+ lvl = 1;
+ break;
+ case PAGING_MODE_64:
+ table = EPT_ADDR(paging->cr3);
+ lvl = 3;
+ break;
+ default:
+ return (false);
+ }
+ for (; lvl >= 0; lvl--) {
+ shift = 12 + 9 * lvl;
+ if (!nested_l2_read(vcpu, table + 8 * ((gla >> shift) & 0x1ff),
+ &entry, sizeof(entry)) || (entry & PG_V) == 0)
+ return (false);
+ if (lvl == 0 || (lvl < 3 && (entry & PG_PS) != 0)) {
+ *gpa = (EPT_ADDR(entry) & ~((1UL << shift) - 1)) |
+ (gla & ((1UL << shift) - 1));
+ return (true);
+ }
+ table = EPT_ADDR(entry);
+ }
+ return (false);
+}
+
+/*
+ * L2 has accessed, through L1's tables, an address of L1 that is not memory:
+ * a device of L1, which is emulated as it is for L1. The exit becomes the
+ * one for the instruction emulation, with the instruction already fetched,
+ * because the fetch that vm_run() does takes L2's page tables to be in L1's
+ * memory. For the same reason only instructions with no other memory
+ * operand than the device are let through: the emulation would look for a
+ * second one in the wrong place. Returns false if it cannot be done.
+ */
+static bool
+nested_ept_device(struct vmx_vcpu *vcpu, uint64_t gpa1)
+{
+ struct vmx_nested *nested = vcpu->nested;
+ const struct vmx_nested_l2exit *exit = &nested->exit;
+ struct vm_exit *vmexit;
+ const struct vm_guest_paging *paging;
+ uint64_t gla, gpa, rip;
+ uint8_t inst[VIE_INST_SIZE];
+ size_t n, off;
+ u_int i;
+
+ /* Not during a page walk or the delivery of an event. */
+ if ((exit->qual & (1 << 7 | 1 << 8)) != (1 << 7 | 1 << 8) ||
+ (exit->idt_info & VMCS_INTR_VALID) != 0)
+ return (false);
+ vmexit = vm_exitinfo(vcpu->vcpu);
+ rip = vmx_nested_inst_emul(vcpu, vmexit, gpa1, exit->gla);
+ paging = &vmexit->u.inst_emul.paging;
+ gla = rip + vmexit->u.inst_emul.cs_base;
+ if (paging->cpu_mode != CPU_MODE_64BIT)
+ gla &= 0xffffffff;
+ for (off = 0; off < sizeof(inst); off += n) {
+ n = MIN(sizeof(inst) - off, PAGE_SIZE - ((gla + off) &
+ PAGE_MASK));
+ if (!nested_l2_gla2gpa(vcpu, paging, gla + off, &gpa) ||
+ !nested_l2_read(vcpu, gpa, inst + off, n)) {
+ /* What follows the page of the instruction's start. */
+ if (off == 0)
+ return (false);
+ break;
+ }
+ }
+ /* MOVS, POP and PUSH have a second memory operand. */
+ for (i = 0; i < off; i++) {
+ switch (inst[i]) {
+ case 0x26: case 0x2e: case 0x36: case 0x3e:
+ case 0x64: case 0x65: case 0x66: case 0x67:
+ case 0xf0: case 0xf2: case 0xf3:
+ continue;
+ case 0x40 ... 0x4f:
+ if (paging->cpu_mode == CPU_MODE_64BIT)
+ continue;
+ break;
+ }
+ break;
+ }
+ if (i == off || inst[i] == 0xa4 || inst[i] == 0xa5 ||
+ inst[i] == 0x8f || inst[i] == 0xff)
+ return (false);
+ vie_init(&vmexit->u.inst_emul.vie, (const char *)inst, off);
+ /* vm_run() continues with L2 after the instruction. */
+ vmexit->rip = rip;
+ (void)vm_set_register(vcpu->vcpu, VM_REG_GUEST_RIP, rip);
+ nested->l2_rip = true;
+ return (true);
+}
+
/*
* L2 has had an EPT violation on EPT02. It is L1's if its tables do not
* allow the access. If they do, EPT02 gets the page and L2 goes on.
@@ -1056,8 +1206,12 @@
PMAP_UNLOCK(pmap);
if (tries == 2 || !nested_ept01_fault(vcpu, gpa1,
(need & EPT_W) != 0 ? VM_PROT_WRITE : (need & EPT_X) != 0 ?
- VM_PROT_EXECUTE : VM_PROT_READ))
+ VM_PROT_EXECUTE : VM_PROT_READ)) {
+ if (!vm_mem_allocated(vcpu->vcpu, gpa1) &&
+ nested_ept_device(vcpu, gpa1))
+ return;
goto fail;
+ }
}
pte01 = *leaf;
pte = pa | (perm & pte01 & EPT_RWX) |
@@ -1085,8 +1239,8 @@
fail:
/*
* L1's tables are not in its memory, or they lead to something that
- * is not memory, such as a device of L1. Emulating that is not
- * implemented: L1 is told that L2 had a triple fault.
+ * the host cannot give to L2. L1 is told that L2 had a triple
+ * fault.
*/
memset(exit, 0, sizeof(*exit));
exit->reason = EXIT_REASON_TRIPLE_FAULT;
diff --git a/sys/amd64/vmm/vmm.c b/sys/amd64/vmm/vmm.c
--- a/sys/amd64/vmm/vmm.c
+++ b/sys/amd64/vmm/vmm.c
@@ -1262,6 +1262,13 @@
break;
case VM_EXITCODE_VMINSN:
error = vmmops_vminsn(vcpu->cookie);
+ /*
+ * The exit can have become the one for an instruction
+ * of a nested guest that accesses a device.
+ */
+ if (error == 0 &&
+ vme->exitcode == VM_EXITCODE_INST_EMUL)
+ error = vm_handle_inst_emul(vcpu, &retu);
break;
default:
retu = true; /* handled in userland */

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 21, 2:47 PM (20 h, 41 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41890953
Default Alt Text
D61800.1795272451.diff (7 KB)

Event Timeline