Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp351227img; Mon, 18 Mar 2019 04:45:24 -0700 (PDT) X-Google-Smtp-Source: APXvYqyO7LN5WiDHkpXRXWcf6KXlt66Pv10IpXNgKGom++YE8an2kV1zZowhmq3imaFhyNqa54Uv X-Received: by 2002:a17:902:8d97:: with SMTP id v23mr18864375plo.274.1552909524008; Mon, 18 Mar 2019 04:45:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552909524; cv=none; d=google.com; s=arc-20160816; b=gRubJMzh6J31IbuxIdW9SI7ZSYQUxOw1lDeVqrJwhLd/4WQYPuafgHU00wRwooMD66 7/msi1WvZEkmrUQenju8nQE0gMrwqO2rd8J1fIiau4YgetWmclF+qH8r+WRnEH4yUvtH MQwNevDToZYH/ratGxd8JWN2tOl67LlHqmae95jc0GuSLST7AP/ENpzDqwOtrkdh4FBk rhYqy/BI0m+Q2vUWnnYxcqfI86ZY/A+YE3bnmfl6kVKGFT6iLghkGEVQcyMeNIOt1LWx vyOY5WC4FyuOjEDYrm2pH5iIXHx1OwmXrEhjCLE+5139VU1J9GHRpvM6L7gPzspaK0Pf kgtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=TZByHuUSIk1apPf6rFlUcmHDL25oOvuLVDn4IpQSu7M=; b=g9VJC0k6z5KNUm7YGwmp0sXGrwTWzcQQKYXyxXaKsHXz43bkYSsI1P7SIfkpQsGYDJ BR2c7fHVIQjD4Nm21r+8IYzh/67A5AXnUEoxh3HX81tjaPpx2KjbitPvN8EuiounS8xa 1tzfYFFieE75CM3I6FEnRRT07v8mb2uQhtFjaiXzAvUfFDXmKyPExRfUkODSTYs5zSEf JNqGlTP8evS01+kikW9YTHBMumcSnYMQIg1Y0jMthlvZYO7Ul2EhA8QgIt+pZPrcki95 eWwp76CJ4zcNP9F2zaor7QyuK/DcUMpFmYYTZ/1wwz4aLSLW5lVAko+8BIvRJkCsNLVZ AweA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w1si8876568pgj.113.2019.03.18.04.45.08; Mon, 18 Mar 2019 04:45:23 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727298AbfCRLn4 (ORCPT + 99 others); Mon, 18 Mar 2019 07:43:56 -0400 Received: from mga01.intel.com ([192.55.52.88]:2097 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727357AbfCRLnz (ORCPT ); Mon, 18 Mar 2019 07:43:55 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Mar 2019 04:43:54 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,493,1544515200"; d="scan'208";a="126363924" Received: from lxy-server.sh.intel.com ([10.239.48.11]) by orsmga008.jf.intel.com with ESMTP; 18 Mar 2019 04:43:52 -0700 From: Xiaoyao Li To: kvm@vger.kernel.org, Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= Cc: Xiaoyao Li , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , linux-kernel@vger.kernel.org, chao.gao@intel.com Subject: [PATCH v2 2/2] kvm/vmx: Using hardware cpuid faulting to avoid emulation overhead Date: Mon, 18 Mar 2019 19:43:24 +0800 Message-Id: <20190318114324.14198-3-xiaoyao.li@linux.intel.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190318114324.14198-1-xiaoyao.li@linux.intel.com> References: <20190318114324.14198-1-xiaoyao.li@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Current cpuid faulting of guest is purely emulated in kvm, which exploits CPUID vm exit to inject #GP to guest. However, if host hardware cpu has X86_FEATURE_CPUID_FAULT, we can just use the hardware cpuid faulting for guest to avoid the vm exit overhead. Note: cpuid faulting takes higher priority over CPUID instruction vm exit (Intel SDM vol3.25.1.1). Since cpuid faulting only exists on some Intel's cpu, just apply this optimization to vmx. Signed-off-by: Xiaoyao Li --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/vmx/vmx.c | 19 +++++++++++++++---- arch/x86/kvm/x86.c | 15 ++++++++++++--- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index ce79d7bfe1fd..14cad587b804 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1339,6 +1339,8 @@ void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw); void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l); int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr); +int kvm_supported_msr_misc_features_enables(struct kvm_vcpu *vcpu, u64 data); + int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr); int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr); diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 2c59e0209e36..6b413e471dca 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -1037,7 +1037,7 @@ static void pt_guest_exit(struct vcpu_vmx *vmx) static void vmx_save_host_cpuid_fault(struct vcpu_vmx *vmx) { - u64 host_val; + u64 host_val, guest_val; if (!boot_cpu_has(X86_FEATURE_CPUID_FAULT)) return; @@ -1045,10 +1045,12 @@ static void vmx_save_host_cpuid_fault(struct vcpu_vmx *vmx) rdmsrl(MSR_MISC_FEATURES_ENABLES, host_val); vmx->host_msr_misc_features_enables = host_val; - /* clear cpuid fault bit to avoid it leak to guest */ - if (host_val & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT) { + guest_val = vmx->vcpu.arch.msr_misc_features_enables; + + /* we can use the hardware cpuid faulting to avoid emulation overhead */ + if ((host_val ^ guest_val) & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT) { wrmsrl(MSR_MISC_FEATURES_ENABLES, - host_val & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT); + host_val ^ MSR_MISC_FEATURES_ENABLES_CPUID_FAULT); } } @@ -2057,6 +2059,15 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) else vmx->pt_desc.guest.addr_a[index / 2] = data; break; + case MSR_MISC_FEATURES_ENABLES: + if (!kvm_supported_msr_misc_features_enables(vcpu, data)) + return 1; + if (boot_cpu_has(X86_FEATURE_CPUID_FAULT)) { + if (vmx->loaded_cpu_state) + wrmsrl(MSR_MISC_FEATURES_ENABLES, data); + } + vcpu->arch.msr_misc_features_enables = data; + break; case MSR_TSC_AUX: if (!msr_info->host_initiated && !guest_cpuid_has(vcpu, X86_FEATURE_RDTSCP)) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 434ec113cc79..33a8c95b2f2e 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2449,6 +2449,17 @@ static void record_steal_time(struct kvm_vcpu *vcpu) &vcpu->arch.st.steal, sizeof(struct kvm_steal_time)); } +int kvm_supported_msr_misc_features_enables(struct kvm_vcpu *vcpu, u64 data) +{ + if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || + (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && + !supports_cpuid_fault(vcpu))) + return 0; + else + return 1; +} +EXPORT_SYMBOL_GPL(kvm_supported_msr_misc_features_enables); + int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) { bool pr = false; @@ -2669,9 +2680,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.msr_platform_info = data; break; case MSR_MISC_FEATURES_ENABLES: - if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || - (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && - !supports_cpuid_fault(vcpu))) + if (!kvm_supported_msr_misc_features_enables(vcpu, data)) return 1; vcpu->arch.msr_misc_features_enables = data; break; -- 2.19.1