Module Name:    src
Committed By:   maxv
Date:           Thu Feb 21 11:58:04 UTC 2019

Modified Files:
        src/sys/dev/nvmm/x86: nvmm_x86_svm.c nvmm_x86_vmx.c

Log Message:
Clarify the gTLB code a little.


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/sys/dev/nvmm/x86/nvmm_x86_svm.c
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/nvmm/x86/nvmm_x86_vmx.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/nvmm/x86/nvmm_x86_svm.c
diff -u src/sys/dev/nvmm/x86/nvmm_x86_svm.c:1.27 src/sys/dev/nvmm/x86/nvmm_x86_svm.c:1.28
--- src/sys/dev/nvmm/x86/nvmm_x86_svm.c:1.27	Mon Feb 18 12:17:45 2019
+++ src/sys/dev/nvmm/x86/nvmm_x86_svm.c	Thu Feb 21 11:58:04 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvmm_x86_svm.c,v 1.27 2019/02/18 12:17:45 maxv Exp $	*/
+/*	$NetBSD: nvmm_x86_svm.c,v 1.28 2019/02/21 11:58:04 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.27 2019/02/18 12:17:45 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.28 2019/02/21 11:58:04 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -502,7 +502,7 @@ static const size_t svm_conf_sizes[NVMM_
 struct svm_cpudata {
 	/* General */
 	bool shared_asid;
-	bool tlb_want_flush;
+	bool gtlb_want_flush;
 
 	/* VMCB */
 	struct vmcb *vmcb;
@@ -977,7 +977,7 @@ svm_inkernel_handle_msr(struct nvmm_mach
 			}
 			if ((vmcb->state.efer ^ exit->u.msr.val) &
 			     EFER_TLB_FLUSH) {
-				cpudata->tlb_want_flush = true;
+				cpudata->gtlb_want_flush = true;
 			}
 			vmcb->state.efer = exit->u.msr.val | EFER_SVME;
 			svm_vmcb_cache_flush(vmcb, VMCB_CTRL_VMCB_CLEAN_CR);
@@ -1185,21 +1185,30 @@ svm_vcpu_guest_misc_leave(struct nvmm_cp
 	wrmsr(MSR_KERNELGSBASE, cpudata->kernelgsbase);
 }
 
+/* -------------------------------------------------------------------------- */
+
+static inline void
+svm_gtlb_catchup(struct nvmm_cpu *vcpu, int hcpu)
+{
+	struct svm_cpudata *cpudata = vcpu->cpudata;
+
+	if (vcpu->hcpu_last != hcpu || cpudata->shared_asid) {
+		cpudata->gtlb_want_flush = true;
+	}
+}
+
 static int
 svm_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
     struct nvmm_exit *exit)
 {
 	struct svm_cpudata *cpudata = vcpu->cpudata;
 	struct vmcb *vmcb = cpudata->vmcb;
-	bool tlb_need_flush = false;
 	int hcpu, s;
 
 	kpreempt_disable();
 	hcpu = cpu_number();
 
-	if (vcpu->hcpu_last != hcpu || cpudata->shared_asid) {
-		tlb_need_flush = true;
-	}
+	svm_gtlb_catchup(vcpu, hcpu);
 
 	if (vcpu->hcpu_last != hcpu) {
 		vmcb->ctrl.tsc_offset = cpudata->tsc_offset +
@@ -1211,7 +1220,7 @@ svm_vcpu_run(struct nvmm_machine *mach, 
 	svm_vcpu_guest_misc_enter(vcpu);
 
 	while (1) {
-		if (cpudata->tlb_want_flush || tlb_need_flush) {
+		if (cpudata->gtlb_want_flush) {
 			vmcb->ctrl.tlb_ctrl = svm_ctrl_tlb_flush;
 		} else {
 			vmcb->ctrl.tlb_ctrl = 0;
@@ -1226,8 +1235,7 @@ svm_vcpu_run(struct nvmm_machine *mach, 
 		svm_vmcb_cache_default(vmcb);
 
 		if (vmcb->ctrl.exitcode != VMCB_EXITCODE_INVALID) {
-			cpudata->tlb_want_flush = false;
-			tlb_need_flush = false;
+			cpudata->gtlb_want_flush = false;
 			vcpu->hcpu_last = hcpu;
 		}
 
@@ -1751,7 +1759,7 @@ svm_vcpu_setstate(struct nvmm_cpu *vcpu,
 	struct fxsave *fpustate;
 
 	if (svm_state_tlb_flush(vmcb, state, flags)) {
-		cpudata->tlb_want_flush = true;
+		cpudata->gtlb_want_flush = true;
 	}
 
 	if (flags & NVMM_X64_STATE_SEGS) {
@@ -1985,7 +1993,7 @@ svm_tlb_flush(struct pmap *pm)
 		if (error)
 			continue;
 		cpudata = vcpu->cpudata;
-		cpudata->tlb_want_flush = true;
+		cpudata->gtlb_want_flush = true;
 		nvmm_vcpu_put(vcpu);
 	}
 }

Index: src/sys/dev/nvmm/x86/nvmm_x86_vmx.c
diff -u src/sys/dev/nvmm/x86/nvmm_x86_vmx.c:1.7 src/sys/dev/nvmm/x86/nvmm_x86_vmx.c:1.8
--- src/sys/dev/nvmm/x86/nvmm_x86_vmx.c:1.7	Mon Feb 18 12:17:45 2019
+++ src/sys/dev/nvmm/x86/nvmm_x86_vmx.c	Thu Feb 21 11:58:04 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvmm_x86_vmx.c,v 1.7 2019/02/18 12:17:45 maxv Exp $	*/
+/*	$NetBSD: nvmm_x86_vmx.c,v 1.8 2019/02/21 11:58:04 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_vmx.c,v 1.7 2019/02/18 12:17:45 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_vmx.c,v 1.8 2019/02/21 11:58:04 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -637,7 +637,7 @@ static const size_t vmx_conf_sizes[NVMM_
 struct vmx_cpudata {
 	/* General */
 	uint64_t asid;
-	bool tlb_want_flush;
+	bool gtlb_want_flush;
 
 	/* VMCS */
 	struct vmcs *vmcs;
@@ -1601,6 +1601,8 @@ vmx_vcpu_guest_misc_leave(struct nvmm_cp
 	wrmsr(MSR_KERNELGSBASE, cpudata->kernelgsbase);
 }
 
+/* --------------------------------------------------------------------- */
+
 #define VMX_INVVPID_ADDRESS		0
 #define VMX_INVVPID_CONTEXT		1
 #define VMX_INVVPID_ALL			2
@@ -1609,13 +1611,22 @@ vmx_vcpu_guest_misc_leave(struct nvmm_cp
 #define VMX_INVEPT_CONTEXT		1
 #define VMX_INVEPT_ALL			2
 
+static inline void
+vmx_gtlb_catchup(struct nvmm_cpu *vcpu, int hcpu)
+{
+	struct vmx_cpudata *cpudata = vcpu->cpudata;
+
+	if (vcpu->hcpu_last != hcpu) {
+		cpudata->gtlb_want_flush = true;
+	}
+}
+
 static int
 vmx_vcpu_run(struct nvmm_machine *mach, struct nvmm_cpu *vcpu,
     struct nvmm_exit *exit)
 {
 	struct vmx_machdata *machdata = mach->machdata;
 	struct vmx_cpudata *cpudata = vcpu->cpudata;
-	bool tlb_need_flush = false;
 	struct vpid_desc vpid_desc;
 	struct ept_desc ept_desc;
 	struct cpu_info *ci;
@@ -1628,6 +1639,8 @@ vmx_vcpu_run(struct nvmm_machine *mach, 
 	ci = curcpu();
 	hcpu = cpu_number();
 
+	vmx_gtlb_catchup(vcpu, hcpu);
+
 	if (__predict_false(kcpuset_isset(machdata->ept_want_flush, hcpu))) {
 		vmx_vmread(VMCS_EPTP, &ept_desc.eptp);
 		ept_desc.mbz = 0;
@@ -1636,10 +1649,6 @@ vmx_vcpu_run(struct nvmm_machine *mach, 
 	}
 
 	if (vcpu->hcpu_last != hcpu) {
-		tlb_need_flush = true;
-	}
-
-	if (vcpu->hcpu_last != hcpu) {
 		vmx_vmwrite(VMCS_HOST_TR_SELECTOR, ci->ci_tss_sel);
 		vmx_vmwrite(VMCS_HOST_TR_BASE, (uint64_t)ci->ci_tss);
 		vmx_vmwrite(VMCS_HOST_GDTR_BASE, (uint64_t)ci->ci_gdt);
@@ -1653,12 +1662,11 @@ vmx_vcpu_run(struct nvmm_machine *mach, 
 	vmx_vcpu_guest_misc_enter(vcpu);
 
 	while (1) {
-		if (cpudata->tlb_want_flush || tlb_need_flush) {
+		if (cpudata->gtlb_want_flush) {
 			vpid_desc.vpid = cpudata->asid;
 			vpid_desc.addr = 0;
 			vmx_invvpid(vmx_tlb_flush_op, &vpid_desc);
-			cpudata->tlb_want_flush = false;
-			tlb_need_flush = false;
+			cpudata->gtlb_want_flush = false;
 		}
 
 		s = splhigh();
@@ -2217,7 +2225,7 @@ vmx_vcpu_setstate(struct nvmm_cpu *vcpu,
 	vmx_vmcs_enter(vcpu);
 
 	if (vmx_state_tlb_flush(state, flags)) {
-		cpudata->tlb_want_flush = true;
+		cpudata->gtlb_want_flush = true;
 	}
 
 	if (flags & NVMM_X64_STATE_SEGS) {

Reply via email to