From patchwork Tue May 21 06:06:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 10952917 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D4C41112C for ; Tue, 21 May 2019 06:07:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C299B288E2 for ; Tue, 21 May 2019 06:07:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B6A9D28924; Tue, 21 May 2019 06:07:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6365F288E2 for ; Tue, 21 May 2019 06:07:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727864AbfEUGHB (ORCPT ); Tue, 21 May 2019 02:07:01 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:40177 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726719AbfEUGHB (ORCPT ); Tue, 21 May 2019 02:07:01 -0400 Received: by mail-pf1-f195.google.com with SMTP id u17so8482193pfn.7; Mon, 20 May 2019 23:07:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=sbuzaaWsGooYKRF5hbHXqNhTGEcasdrIczbLRR2hHHM=; b=saXJWYw2jHbhxim/0lwRfNiV2/djBlPQ0xI0+lFcXra2FD1zMCQXuXa4MrdDYpvZip H9iA2q4ty2ZLuw66wIlkLY1712YH5n5vbZsQrf85uOEiqikX9AQCALDmF7+8qj5S5vpr aa8f2k4Bp4kOuntmJEzQikmD4oeFiqE3FNOS8LBKX1HU/YXThyZVeTJeqPelBhX45QTz L2rBuX301sv42jVomroF9Z4fX1QorWaMM3yoUWNCdp0chKPxxNhC3DLqK8A1A6iFhRhQ TaqMjedI2GmsxD/v2vDALrg6S3hL45fy9rGhFQIx6clAPsxeIGoJD4hL5FKPjWOso5Rz BccQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=sbuzaaWsGooYKRF5hbHXqNhTGEcasdrIczbLRR2hHHM=; b=dE6HesCMrACar16chqqOVyRxycyXuHFAiTnEFXH4RblK0xJjAxd5o8/vvroDKAoYL/ 7AX4B3DCG1QaSRi+YuSfHR7AqIZqeDUvWpgqb/inMqBZqOOdJaYsg3RV8FzYqxZSYhzm kdD+RlSIf65gOVl6DPQ3nN8geJkxBY166ZXji8kSldcrTxTtV70SIExBE9jFkRDqXrzv Cz7eHIUQsYoiT6qlca0710hju+REYqA+3WWQUYZ8BsRvtqR243lFg88vnxQ9Oqh4QAXN DKK5WSPCebK3dRGx3kxSdk2g4JkwbMJ9PEMfJwFIwLbEvbi/V6693TJbjwQfg0pcKabi 4j1A== X-Gm-Message-State: APjAAAWQqFuH1iQ7J9SBCe1J76bGTVYkaThFBtM5+e2nFnIJtqIIErsJ UMeHeS+mbcd+J380udnkcM8KXljg X-Google-Smtp-Source: APXvYqyYJfAhOFbsgbheeA9FzL945ilIFAg5nm75jDFRMsMJCNztDnsUsAARni2j8OwUhJQctAfIVQ== X-Received: by 2002:a62:2cc2:: with SMTP id s185mr46057385pfs.106.1558418820429; Mon, 20 May 2019 23:07:00 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id a15sm2351484pgv.4.2019.05.20.23.06.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 May 2019 23:07:00 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= , Sean Christopherson , Liran Alon Subject: [PATCH 1/3] KVM: Documentation: Add disable pause exits to KVM_CAP_X86_DISABLE_EXITS Date: Tue, 21 May 2019 14:06:52 +0800 Message-Id: <1558418814-6822-1-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wanpeng Li Commit b31c114b (KVM: X86: Provide a capability to disable PAUSE intercepts) forgot to add the KVM_X86_DISABLE_EXITS_PAUSE into api doc. This patch adds it. Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Sean Christopherson Cc: Liran Alon Signed-off-by: Wanpeng Li --- Documentation/virtual/kvm/api.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt index ba6c42c..33cd92d 100644 --- a/Documentation/virtual/kvm/api.txt +++ b/Documentation/virtual/kvm/api.txt @@ -4893,6 +4893,7 @@ Valid bits in args[0] are #define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0) #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) +#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) Enabling this capability on a VM provides userspace with a way to no longer intercept some instructions for improved latency in some From patchwork Tue May 21 06:06:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 10952915 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9754517D2 for ; Tue, 21 May 2019 06:07:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 86CD3288E2 for ; Tue, 21 May 2019 06:07:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7AE882891A; Tue, 21 May 2019 06:07:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E18C928924 for ; Tue, 21 May 2019 06:07:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727898AbfEUGHE (ORCPT ); Tue, 21 May 2019 02:07:04 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46550 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726719AbfEUGHD (ORCPT ); Tue, 21 May 2019 02:07:03 -0400 Received: by mail-pg1-f196.google.com with SMTP id t187so7987048pgb.13; Mon, 20 May 2019 23:07:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WMmDh5ublSWiaoVJ7xAuxbCA1uJ5y/sAAbLy4QWe0yE=; b=cttOljSYk+vMwjaiHb8qs6lDzVkinsv9NdsfmzJCnvtV4G1hq2AWTZbnjNtY/9VsET tmt4twqqjni2qgRmV9cgah1IclAmaW8SOJz5pzmsLSRCbQSL+M3lcp5CSfvmRDpf5Ykc 1KgHReDOrt3p8/KoQ75pnQn8fG6Uqjyz9texu+3t8H253KjxHVUhIFeyuI1ok3f8zQmh ik6i8b9WT0h9m7TNjLDc7Gzl017CP3XHQdSDp8WHflcjTulBIM4GMWia4AtFD3Iqrk/Q fjU54rMj3IrmnVkWI1iO/9GL1xu1qjdpIrKmNBKeLmEj5esMcJyqDHl/ddCh52GYcDbS o6jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WMmDh5ublSWiaoVJ7xAuxbCA1uJ5y/sAAbLy4QWe0yE=; b=ZWMa4+5kfj1DBT1GOMqw+V7q/f9C72OaG07usDJ0IfDltEYqJYChZZn6HkNa3PFBF0 bfArsUCLH26bzNikj7kO7Lb40+Z+V2kq1SZ2TsusUwH9qJHSfXNk2XMN5DJUQ7EoOr5k KsiH+xP3gJ5BgXiayRxI0AkikPj45ElsruTWQ6pvRlf2EwUwDSTClYSzM3j+6HUslWBU p18a4omdskVgmMPu1wbp9ezSM/6X32INKyUGPOAllcTB2jKLOyMJPUznFsd7Yp4HkpcM htr4tLicrdNQzj4llCmbyaZQgQECVNo1DFEDdjPhE4h6iB0rEYHT2lGtApCwC+Wgx8Ge NEqw== X-Gm-Message-State: APjAAAXajx7QXhioIzwNRfwrdFYAGwz9lZPehVGXPuTcbxzRFtJxhESw oJFizZniI5Ta3DnJzHvofnYWQUst X-Google-Smtp-Source: APXvYqzpfv0DvbWjagjfN8oSkahd2AdQkFRkh8YKneIumuxFFobNOVb9Wln528UFGPNSiXTJun2Pnw== X-Received: by 2002:a62:6585:: with SMTP id z127mr45612843pfb.179.1558418822658; Mon, 20 May 2019 23:07:02 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id a15sm2351484pgv.4.2019.05.20.23.07.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 May 2019 23:07:02 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= , Sean Christopherson , Liran Alon Subject: [PATCH v2 2/3] KVM: X86: Provide a capability to disable cstate msr read intercepts Date: Tue, 21 May 2019 14:06:53 +0800 Message-Id: <1558418814-6822-2-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1558418814-6822-1-git-send-email-wanpengli@tencent.com> References: <1558418814-6822-1-git-send-email-wanpengli@tencent.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wanpeng Li Allow guest reads CORE cstate when exposing host CPU power management capabilities to the guest. PKG cstate is restricted to avoid a guest to get the whole package information in multi-tenant scenario. Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Sean Christopherson Cc: Liran Alon Signed-off-by: Wanpeng Li --- v1 -> v2: * use a separate bit for KVM_CAP_X86_DISABLE_EXITS Documentation/virtual/kvm/api.txt | 1 + arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/vmx.c | 6 ++++++ arch/x86/kvm/x86.c | 5 ++++- arch/x86/kvm/x86.h | 5 +++++ include/uapi/linux/kvm.h | 4 +++- tools/include/uapi/linux/kvm.h | 4 +++- 7 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt index 33cd92d..91fd86f 100644 --- a/Documentation/virtual/kvm/api.txt +++ b/Documentation/virtual/kvm/api.txt @@ -4894,6 +4894,7 @@ Valid bits in args[0] are #define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0) #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) +#define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) Enabling this capability on a VM provides userspace with a way to no longer intercept some instructions for improved latency in some diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index d5457c7..1ce8289 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -882,6 +882,7 @@ struct kvm_arch { bool mwait_in_guest; bool hlt_in_guest; bool pause_in_guest; + bool cstate_in_guest; unsigned long irq_sources_bitmap; s64 kvmclock_offset; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 0861c71..da24f18 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -6637,6 +6637,12 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id) vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_CS, MSR_TYPE_RW); vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_ESP, MSR_TYPE_RW); vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, MSR_TYPE_RW); + if (kvm_cstate_in_guest(kvm)) { + vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C1_RES, MSR_TYPE_R); + vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C3_RESIDENCY, MSR_TYPE_R); + vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C6_RESIDENCY, MSR_TYPE_R); + vmx_disable_intercept_for_msr(msr_bitmap, MSR_CORE_C7_RESIDENCY, MSR_TYPE_R); + } vmx->msr_bitmap_mode = 0; vmx->loaded_vmcs = &vmx->vmcs01; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 202048e..765fe59 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3098,7 +3098,8 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r = KVM_CLOCK_TSC_STABLE; break; case KVM_CAP_X86_DISABLE_EXITS: - r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE; + r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE | + KVM_X86_DISABLE_EXITS_CSTATE; if(kvm_can_mwait_in_guest()) r |= KVM_X86_DISABLE_EXITS_MWAIT; break; @@ -4612,6 +4613,8 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, kvm->arch.hlt_in_guest = true; if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) kvm->arch.pause_in_guest = true; + if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) + kvm->arch.cstate_in_guest = true; r = 0; break; case KVM_CAP_MSR_PLATFORM_INFO: diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index a470ff0..275b3b6 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -333,6 +333,11 @@ static inline bool kvm_pause_in_guest(struct kvm *kvm) return kvm->arch.pause_in_guest; } +static inline bool kvm_cstate_in_guest(struct kvm *kvm) +{ + return kvm->arch.cstate_in_guest; +} + DECLARE_PER_CPU(struct kvm_vcpu *, current_vcpu); static inline void kvm_before_interrupt(struct kvm_vcpu *vcpu) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 2fe12b4..c2152f3 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -696,9 +696,11 @@ struct kvm_ioeventfd { #define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0) #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) +#define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) #define KVM_X86_DISABLE_VALID_EXITS (KVM_X86_DISABLE_EXITS_MWAIT | \ KVM_X86_DISABLE_EXITS_HLT | \ - KVM_X86_DISABLE_EXITS_PAUSE) + KVM_X86_DISABLE_EXITS_PAUSE | \ + KVM_X86_DISABLE_EXITS_CSTATE) /* for KVM_ENABLE_CAP */ struct kvm_enable_cap { diff --git a/tools/include/uapi/linux/kvm.h b/tools/include/uapi/linux/kvm.h index 6d4ea4b..ef3303f 100644 --- a/tools/include/uapi/linux/kvm.h +++ b/tools/include/uapi/linux/kvm.h @@ -696,9 +696,11 @@ struct kvm_ioeventfd { #define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0) #define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2) +#define KVM_X86_DISABLE_EXITS_CSTATE (1 << 3) #define KVM_X86_DISABLE_VALID_EXITS (KVM_X86_DISABLE_EXITS_MWAIT | \ KVM_X86_DISABLE_EXITS_HLT | \ - KVM_X86_DISABLE_EXITS_PAUSE) + KVM_X86_DISABLE_EXITS_PAUSE | \ + KVM_X86_DISABLE_EXITS_CSTATE) /* for KVM_ENABLE_CAP */ struct kvm_enable_cap { From patchwork Tue May 21 06:06:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 10952913 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 242CD1390 for ; Tue, 21 May 2019 06:07:13 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0870B288E2 for ; Tue, 21 May 2019 06:07:13 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ED15E28925; Tue, 21 May 2019 06:07:12 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD69D288E2 for ; Tue, 21 May 2019 06:07:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727926AbfEUGHG (ORCPT ); Tue, 21 May 2019 02:07:06 -0400 Received: from mail-pf1-f196.google.com ([209.85.210.196]:39078 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726719AbfEUGHG (ORCPT ); Tue, 21 May 2019 02:07:06 -0400 Received: by mail-pf1-f196.google.com with SMTP id z26so8478948pfg.6; Mon, 20 May 2019 23:07:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=T2f0j+HcR+Hm2808+uYZ0INzAMJinwKKeoQwZ5KAAMI=; b=AfTgyoeXL7FvsFCYPAMw2WB6WEYULcybTi8Y4feHwmKhP32/yEDF7uQVjZCOD1BOyH MJkOKTFQTBy+9FR9fykBAuRzjhuYIMFY6LOJUotbnevwBeiCealuEvvzdv3t2799rSeh feljqw21jIYZLBfOMhKN3jDfh0PVDpcDsPpfqP5aVrv9DhEllO5x4ASFWNKqThoN0Trj Pazimk3nOviXHAB3wyl0VbQ4Dvwd0M1zmMDIGhszXhuKzZBxsh3LgR5NKRWv1Mn59Jvv PsuPJ+NDlziuakmxo10qlB5hxKS4p10i44atoayaiRyldpDnMGCuZU3fIIlhqQMllRV2 s12Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=T2f0j+HcR+Hm2808+uYZ0INzAMJinwKKeoQwZ5KAAMI=; b=H6n6IGbTdFraDRn3h0z+dcRso0p4U3dY16rK6MTppkkRZZ80a5TUAS09RzCtJA0ITz C3adAMALZp3Tq+s5qCwUiLzQBO+9gGYbbv++srOxgWGQ49F92k9iDE9cXpHfw+u0R9kf ZH2BfXnvF6xPTymtf7a3nYF3IdHbpdXoCTylz+qyJCujuLxSk6HkVyHJn9eiw8ycibno MX+f9socNtkqWQlh5/4vCzJN+dkCLqttbe1vNl46Y6BzTW9ls3J0WzYgDf1F8biIEfiC TnP5BM4nggxXvoMj9TURH32ltJhLDU7sJ5iYmRaH/PrZIeG3OH2vBuRmC3suimQi5sKs XB3g== X-Gm-Message-State: APjAAAXz+Px93naRAUKj5iV4RAteatoy0mFyCdnpGzyzvFAy0oJ387Yw /k1rQX+arNP+IO1SXIf39CuCOLb1 X-Google-Smtp-Source: APXvYqwK/Tl2RHm3mvIPhxToSh/2lEv1avoEWuM3BPxoT0DKr2jUctRvx8kGLha9eTQMMOzFVdc1+g== X-Received: by 2002:a63:495e:: with SMTP id y30mr37000742pgk.185.1558418825234; Mon, 20 May 2019 23:07:05 -0700 (PDT) Received: from localhost.localdomain ([203.205.141.123]) by smtp.googlemail.com with ESMTPSA id a15sm2351484pgv.4.2019.05.20.23.07.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 May 2019 23:07:04 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?utf-8?b?UmFkaW0gS3LEjW3DocWZ?= , Sean Christopherson , Liran Alon , Konrad Rzeszutek Wilk Subject: [PATCH v2 3/3] KVM: X86: Emulate MSR_IA32_MISC_ENABLE MWAIT bit Date: Tue, 21 May 2019 14:06:54 +0800 Message-Id: <1558418814-6822-3-git-send-email-wanpengli@tencent.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1558418814-6822-1-git-send-email-wanpengli@tencent.com> References: <1558418814-6822-1-git-send-email-wanpengli@tencent.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wanpeng Li MSR IA32_MISC_ENABLE bit 18, according to SDM: | When this bit is set to 0, the MONITOR feature flag is not set (CPUID.01H:ECX[bit 3] = 0). | This indicates that MONITOR/MWAIT are not supported. | | Software attempts to execute MONITOR/MWAIT will cause #UD when this bit is 0. | | When this bit is set to 1 (default), MONITOR/MWAIT are supported (CPUID.01H:ECX[bit 3] = 1). The CPUID.01H:ECX[bit 3] ought to mirror the value of the MSR bit, CPUID.01H:ECX[bit 3] is a better guard than kvm_mwait_in_guest(). kvm_mwait_in_guest() affects the behavior of MONITOR/MWAIT, not its guest visibility. This patch implements toggling of the CPUID bit based on guest writes to the MSR. Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Sean Christopherson Cc: Liran Alon Cc: Konrad Rzeszutek Wilk Signed-off-by: Wanpeng Li --- v1 -> v2: * hide behind KVM_CAP_DISABLE_QUIRKS arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/cpuid.c | 10 ++++++++++ arch/x86/kvm/x86.c | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 7a0e64c..e3ae96b5 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -382,6 +382,7 @@ struct kvm_sync_regs { #define KVM_X86_QUIRK_CD_NW_CLEARED (1 << 1) #define KVM_X86_QUIRK_LAPIC_MMIO_HOLE (1 << 2) #define KVM_X86_QUIRK_OUT_7E_INC_RIP (1 << 3) +#define KVM_X86_QUIRK_MISC_ENABLE_MWAIT (1 << 4) #define KVM_STATE_NESTED_GUEST_MODE 0x00000001 #define KVM_STATE_NESTED_RUN_PENDING 0x00000002 diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index e18a9f9..f54d266 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -137,6 +137,16 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu) (best->eax & (1 << KVM_FEATURE_PV_UNHALT))) best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT); + if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_MWAIT)) { + best = kvm_find_cpuid_entry(vcpu, 0x1, 0); + if (best) { + if (vcpu->arch.ia32_misc_enable_msr & MSR_IA32_MISC_ENABLE_MWAIT) + best->ecx |= F(MWAIT); + else + best->ecx &= ~F(MWAIT); + } + } + /* Update physical-address width */ vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); kvm_mmu_reset_context(vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 765fe59..a4eb711 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2547,6 +2547,16 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) } break; case MSR_IA32_MISC_ENABLE: + if (kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_MWAIT) && + ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) { + if ((vcpu->arch.ia32_misc_enable_msr & MSR_IA32_MISC_ENABLE_MWAIT) && + !(data & MSR_IA32_MISC_ENABLE_MWAIT)) { + if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3)) + return 1; + } + vcpu->arch.ia32_misc_enable_msr = data; + kvm_update_cpuid(vcpu); + } vcpu->arch.ia32_misc_enable_msr = data; break; case MSR_IA32_SMBASE: