Module Name:    src
Committed By:   maxv
Date:           Thu Jan 10 06:58:37 UTC 2019

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

Log Message:
Optimize:

 * Don't save/restore the host CR2, we don't care because we're not in a
   #PF context (and preemption switches already handle CR2 safely).

 * Don't save/restore the host FS and GS, just reset them to zero after
   VMRUN. Note: DS and ES must be reset _before_ VMRUN, but that doesn't
   apply to FS and GS.

 * Handle FSBASE and KGSBASE outside of the VCPU loop, to avoid the cost
   of saving/restoring them when there's no reason to leave the loop.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sys/dev/nvmm/x86/nvmm_x86_svm.c
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/nvmm/x86/nvmm_x86_svmfunc.S

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.13 src/sys/dev/nvmm/x86/nvmm_x86_svm.c:1.14
--- src/sys/dev/nvmm/x86/nvmm_x86_svm.c:1.13	Tue Jan  8 14:43:18 2019
+++ src/sys/dev/nvmm/x86/nvmm_x86_svm.c	Thu Jan 10 06:58:36 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvmm_x86_svm.c,v 1.13 2019/01/08 14:43:18 maxv Exp $	*/
+/*	$NetBSD: nvmm_x86_svm.c,v 1.14 2019/01/10 06:58:36 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.13 2019/01/08 14:43:18 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: nvmm_x86_svm.c,v 1.14 2019/01/10 06:58:36 maxv Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -521,7 +521,8 @@ struct svm_cpudata {
 	uint64_t lstar;
 	uint64_t cstar;
 	uint64_t sfmask;
-	uint64_t cr2;
+	uint64_t fsbase;
+	uint64_t kernelgsbase;
 	bool ts_set;
 	struct xsave_header hfpu __aligned(16);
 
@@ -1151,14 +1152,12 @@ svm_vcpu_guest_misc_enter(struct nvmm_cp
 {
 	struct svm_cpudata *cpudata = vcpu->cpudata;
 
-	/* Save the fixed Host MSRs. */
 	cpudata->star = rdmsr(MSR_STAR);
 	cpudata->lstar = rdmsr(MSR_LSTAR);
 	cpudata->cstar = rdmsr(MSR_CSTAR);
 	cpudata->sfmask = rdmsr(MSR_SFMASK);
-
-	/* Save the Host CR2. */
-	cpudata->cr2 = rcr2();
+	cpudata->fsbase = rdmsr(MSR_FSBASE);
+	cpudata->kernelgsbase = rdmsr(MSR_KERNELGSBASE);
 }
 
 static void
@@ -1166,14 +1165,12 @@ svm_vcpu_guest_misc_leave(struct nvmm_cp
 {
 	struct svm_cpudata *cpudata = vcpu->cpudata;
 
-	/* Restore the fixed Host MSRs. */
 	wrmsr(MSR_STAR, cpudata->star);
 	wrmsr(MSR_LSTAR, cpudata->lstar);
 	wrmsr(MSR_CSTAR, cpudata->cstar);
 	wrmsr(MSR_SFMASK, cpudata->sfmask);
-
-	/* Restore the Host CR2. */
-	lcr2(cpudata->cr2);
+	wrmsr(MSR_FSBASE, cpudata->fsbase);
+	wrmsr(MSR_KERNELGSBASE, cpudata->kernelgsbase);
 }
 
 static int

Index: src/sys/dev/nvmm/x86/nvmm_x86_svmfunc.S
diff -u src/sys/dev/nvmm/x86/nvmm_x86_svmfunc.S:1.1 src/sys/dev/nvmm/x86/nvmm_x86_svmfunc.S:1.2
--- src/sys/dev/nvmm/x86/nvmm_x86_svmfunc.S:1.1	Wed Nov  7 07:43:08 2018
+++ src/sys/dev/nvmm/x86/nvmm_x86_svmfunc.S	Thu Jan 10 06:58:36 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: nvmm_x86_svmfunc.S,v 1.1 2018/11/07 07:43:08 maxv Exp $	*/
+/*	$NetBSD: nvmm_x86_svmfunc.S,v 1.2 2019/01/10 06:58:36 maxv Exp $	*/
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -73,14 +73,6 @@
 	movq	$msr,%rcx	;\
 	wrmsr
 
-#define HOST_SAVE_SEGREG(sreg)	\
-	movw	sreg,%ax	;\
-	pushw	%ax
-
-#define HOST_RESTORE_SEGREG(sreg)\
-	popw	%ax		;\
-	movw	%ax,sreg
-
 #define HOST_SAVE_TR		\
 	strw	%ax		;\
 	pushw	%ax
@@ -150,22 +142,13 @@ ENTRY(svm_vmrun)
 	/* Save the Host TR. */
 	HOST_SAVE_TR
 
-	/* Save the variable Host MSRs. */
-	HOST_SAVE_MSR(MSR_KERNELGSBASE)
+	/* Save the Host GSBASE. */
 	HOST_SAVE_MSR(MSR_GSBASE)
-	HOST_SAVE_MSR(MSR_FSBASE)
 
-	/* Reset the Host Segregs. */
+	/* Reset DS and ES. */
 	movq	$GSEL(GUDATA_SEL, SEL_UPL),%rax
 	movw	%ax,%ds
 	movw	%ax,%es
-	xorq	%rax,%rax
-	movw	%ax,%fs
-	movw	%ax,%gs
-
-	/* Save some Host Segregs. */
-	HOST_SAVE_SEGREG(%fs)
-	HOST_SAVE_SEGREG(%gs)
 
 	/* Save the Host LDT. */
 	HOST_SAVE_LDT
@@ -195,14 +178,13 @@ ENTRY(svm_vmrun)
 	/* Restore the Host LDT. */
 	HOST_RESTORE_LDT
 
-	/* Restore the Host Segregs. */
-	HOST_RESTORE_SEGREG(%gs)
-	HOST_RESTORE_SEGREG(%fs)
+	/* Reset FS and GS. */
+	xorq	%rax,%rax
+	movw	%ax,%fs
+	movw	%ax,%gs
 
-	/* Restore the variable Host MSRs. */
-	HOST_RESTORE_MSR(MSR_FSBASE)
+	/* Restore the Host GSBASE. */
 	HOST_RESTORE_MSR(MSR_GSBASE)
-	HOST_RESTORE_MSR(MSR_KERNELGSBASE)
 
 	/* Restore the Host TR. */
 	HOST_RESTORE_TR

Reply via email to