From patchwork Thu Mar 18 09:49:55 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xu, Dongxiao" X-Patchwork-Id: 86658 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o2I9olSR004342 for ; Thu, 18 Mar 2010 09:50:47 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752203Ab0CRJuo (ORCPT ); Thu, 18 Mar 2010 05:50:44 -0400 Received: from mga02.intel.com ([134.134.136.20]:61205 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752171Ab0CRJun convert rfc822-to-8bit (ORCPT ); Thu, 18 Mar 2010 05:50:43 -0400 Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga101.jf.intel.com with ESMTP; 18 Mar 2010 02:46:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.51,265,1267430400"; d="scan'208";a="501618054" Received: from pgsmsx602.gar.corp.intel.com ([10.221.43.81]) by orsmga002.jf.intel.com with ESMTP; 18 Mar 2010 02:49:52 -0700 Received: from shzsmsx501.ccr.corp.intel.com (10.239.4.96) by pgsmsx602.gar.corp.intel.com (10.221.43.81) with Microsoft SMTP Server (TLS) id 8.2.176.0; Thu, 18 Mar 2010 17:49:48 +0800 Received: from shsmsx501.ccr.corp.intel.com ([10.239.4.141]) by shzsmsx501.ccr.corp.intel.com ([10.239.4.96]) with mapi; Thu, 18 Mar 2010 17:49:47 +0800 From: "Xu, Dongxiao" To: "kvm@vger.kernel.org" CC: Avi Kivity , Marcelo Tosatti Date: Thu, 18 Mar 2010 17:49:55 +0800 Subject: [PATCH 3/3] KVM: VMX: VMXON/VMXOFF usage changes. Thread-Topic: [PATCH 3/3] KVM: VMX: VMXON/VMXOFF usage changes. Thread-Index: AcrGgF0wHvUiCn3MSKuo2s/VA13h/A== Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: acceptlanguage: en-US MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Thu, 18 Mar 2010 09:50:47 +0000 (UTC) diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c index aaf5b52..dd3a7fa 100644 --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -161,8 +161,11 @@ static inline struct vcpu_vmx *to_vmx(struct kvm_vcpu *vcpu) static int init_rmode(struct kvm *kvm); static u64 construct_eptp(unsigned long root_hpa); +static void kvm_cpu_vmxon(u64 addr); +static void kvm_cpu_vmxoff(void); static DEFINE_PER_CPU(struct vmcs *, vmxarea); +static DEFINE_PER_CPU(bool, vmx_mode); static unsigned long *vmx_io_bitmap_a; static unsigned long *vmx_io_bitmap_b; @@ -755,7 +758,9 @@ static void vmx_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { struct vcpu_vmx *vmx = to_vmx(vcpu); u64 tsc_this, delta, new_offset; + u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); + kvm_cpu_vmxon(phys_addr); vmcs_load(vmx->vmcs); if (vcpu->cpu != cpu) { @@ -796,6 +801,7 @@ static void vmx_vcpu_put(struct kvm_vcpu *vcpu) vmcs_clear(vmx->vmcs); rdtscll(vmx->vcpu.arch.host_tsc); vmx->launched = 0; + kvm_cpu_vmxoff(); } static void vmx_fpu_activate(struct kvm_vcpu *vcpu) @@ -1187,9 +1193,11 @@ static __init int vmx_disabled_by_bios(void) static void kvm_cpu_vmxon(u64 addr) { + int cpu = raw_smp_processor_id(); asm volatile (ASM_VMX_VMXON_RAX : : "a"(&addr), "m"(addr) : "memory", "cc"); + per_cpu(vmx_mode, cpu) = 1; } static int hardware_enable(void *garbage) @@ -1223,12 +1231,16 @@ static int hardware_enable(void *garbage) */ static void kvm_cpu_vmxoff(void) { + int cpu = raw_smp_processor_id(); asm volatile (__ex(ASM_VMX_VMXOFF) : : : "cc"); + per_cpu(vmx_mode, cpu) = 0; } static void hardware_disable(void *garbage) { - kvm_cpu_vmxoff(); + int cpu = raw_smp_processor_id(); + if (per_cpu(vmx_mode, cpu)) + kvm_cpu_vmxoff(); write_cr4(read_cr4() & ~X86_CR4_VMXE); } @@ -3930,6 +3942,7 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) { int err; struct vcpu_vmx *vmx = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); + u64 phys_addr = __pa(per_cpu(vmxarea, smp_processor_id())); if (!vmx) return ERR_PTR(-ENOMEM); @@ -3951,9 +3964,11 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) goto free_msrs; preempt_disable(); + kvm_cpu_vmxon(phys_addr); vmcs_load(vmx->vmcs); err = vmx_vcpu_setup(vmx); vmcs_clear(vmx->vmcs); + kvm_cpu_vmxoff(); preempt_enable(); if (err) @@ -4091,10 +4106,13 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu) { struct kvm_cpuid_entry2 *best; struct vcpu_vmx *vmx = to_vmx(vcpu); + int cpu = raw_smp_processor_id(); + u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); u32 exec_control; vmx->rdtscp_enabled = false; if (vmx_rdtscp_supported()) { + kvm_cpu_vmxon(phys_addr); vmcs_load(vmx->vmcs); exec_control = vmcs_read32(SECONDARY_VM_EXEC_CONTROL); if (exec_control & SECONDARY_EXEC_RDTSCP) { @@ -4108,6 +4126,7 @@ static void vmx_cpuid_update(struct kvm_vcpu *vcpu) } } vmcs_clear(vmx->vmcs); + kvm_cpu_vmxoff(); } }