From owner-svn-src-projects@FreeBSD.ORG Fri Oct 26 03:12:41 2012 Return-Path: Delivered-To: svn-src-projects@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id D44093BD; Fri, 26 Oct 2012 03:12:41 +0000 (UTC) (envelope-from neel@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id A338A8FC12; Fri, 26 Oct 2012 03:12:41 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q9Q3CfEX009025; Fri, 26 Oct 2012 03:12:41 GMT (envelope-from neel@svn.freebsd.org) Received: (from neel@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q9Q3Cfwh009022; Fri, 26 Oct 2012 03:12:41 GMT (envelope-from neel@svn.freebsd.org) Message-Id: <201210260312.q9Q3Cfwh009022@svn.freebsd.org> From: Neel Natu Date: Fri, 26 Oct 2012 03:12:41 +0000 (UTC) To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r242122 - in projects/bhyve/sys/amd64/vmm: . intel X-SVN-Group: projects MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Oct 2012 03:12:41 -0000 Author: neel Date: Fri Oct 26 03:12:40 2012 New Revision: 242122 URL: http://svn.freebsd.org/changeset/base/242122 Log: Unconditionally enable fpu emulation by setting CR0.TS in the host after the guest does a vm exit. This allows us to trap any fpu access in the host context while the fpu still has "dirty" state belonging to the guest. Reported by: "s vas" on freebsd-virtualization@ Obtained from: NetApp Modified: projects/bhyve/sys/amd64/vmm/intel/vmcs.c projects/bhyve/sys/amd64/vmm/vmm.c Modified: projects/bhyve/sys/amd64/vmm/intel/vmcs.c ============================================================================== --- projects/bhyve/sys/amd64/vmm/intel/vmcs.c Fri Oct 26 03:02:39 2012 (r242121) +++ projects/bhyve/sys/amd64/vmm/intel/vmcs.c Fri Oct 26 03:12:40 2012 (r242122) @@ -367,7 +367,15 @@ vmcs_set_defaults(struct vmcs *vmcs, goto done; /* Load the control registers */ - cr0 = rcr0(); + + /* + * We always want CR0.TS to be set when the processor does a VM exit. + * + * With emulation turned on unconditionally after a VM exit, we are + * able to trap inadvertent use of the FPU until the guest FPU state + * has been safely squirreled away. + */ + cr0 = rcr0() | CR0_TS; if ((error = vmwrite(VMCS_HOST_CR0, cr0)) != 0) goto done; Modified: projects/bhyve/sys/amd64/vmm/vmm.c ============================================================================== --- projects/bhyve/sys/amd64/vmm/vmm.c Fri Oct 26 03:02:39 2012 (r242121) +++ projects/bhyve/sys/amd64/vmm/vmm.c Fri Oct 26 03:12:40 2012 (r242122) @@ -640,14 +640,27 @@ restore_guest_fpustate(struct vcpu *vcpu /* flush host state to the pcb */ fpuexit(curthread); + + /* restore guest FPU state */ fpu_stop_emulating(); fpurestore(vcpu->guestfpu); + + /* + * The FPU is now "dirty" with the guest's state so turn on emulation + * to trap any access to the FPU by the host. + */ + fpu_start_emulating(); } static void save_guest_fpustate(struct vcpu *vcpu) { + if ((rcr0() & CR0_TS) == 0) + panic("fpu emulation not enabled in host!"); + + /* save guest FPU state */ + fpu_stop_emulating(); fpusave(vcpu->guestfpu); fpu_start_emulating(); }