From patchwork Tue Aug 18 15:24:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 11721613 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 33424739 for ; Tue, 18 Aug 2020 15:24:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1BC32207DA for ; Tue, 18 Aug 2020 15:24:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="P8tsIfdz" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728018AbgHRPYq (ORCPT ); Tue, 18 Aug 2020 11:24:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728013AbgHRPYn (ORCPT ); Tue, 18 Aug 2020 11:24:43 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 09B9CC061343 for ; Tue, 18 Aug 2020 08:24:43 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id d26so22573443yba.20 for ; Tue, 18 Aug 2020 08:24:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=kN4Uz1/uoG7NUGItZgFrqtddN6gWXvz9muSNZRarF1w=; b=P8tsIfdzrYJGShydcl26aucWE9kJ4dhFY1/t27T/z+X0H1jhsDq0pzUgCXdIkl1e4z pEYx4MyxRLQlDp2zNa4FiwhKG8v+DqBf0o+3Wg5LJP0GhYfvDwyYCBVzhimpZPNiZiRi 4bjWPB+BPv7mJUAWFUHBFFCYVUN7/ra06iBgZR+CPAPCcSLSqBH71sCc2zIa8K5cP7TP iiKDedjDfkXT0Kh0BOWNKoa/7mXQft9aTLNJX2Os1t2pnHeYt+NHJKl7xO17zmgtVj94 Dp6QuBv546CJaV5XlieTIOeF3St8iyl2FX84AXYwIrdemmC8+w3oMFcj98mrjDMNY+/x Hf7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=kN4Uz1/uoG7NUGItZgFrqtddN6gWXvz9muSNZRarF1w=; b=KO8hfTKdngiT0DCdkhOsnNNwIO5EEUfo6FUduOVwch4Z0TIB5pwj7vqD8vZ32TYCBm gsHKZlYbr/Oa6w+UsVM1Pruy+TqAr+pD92xrEkABrD3yAWFK79MRyfhCffDgOZQJEvp7 bMhq0vkrzu4O4n6r0LAkOZ06jBAyD7Lrilh1ACgA904DokR+D/4A6F1iW3UNfWegmFW5 SH/sOWLD3QJCtxaV6GLvC+mbVmnNwE6zZXeUnLozXPa9Wud/J5IAvPEKULsBYfdjhpEG bwSjrqZap06cs/F6/iHFory5m5Zi8iDn8h9acXYmmF6KolN0qAf4qTqSGDTW0xPecZsk WWjg== X-Gm-Message-State: AOAM530IYESWGY0Q18xoB5yaSAqq5NWWCtSqDXFsOO4oQV2sQFDqWFcE LZO8n/+twr6ge29yo9IezxMLqHZCYqyWUZ02DKUmI5pGOuwIv7CN3IzSb/4PPEyfd41w7HuKq0G 26oL2W1oStsgmAij356+0blTIfiEjAUGzx4cllmCcfgVwkbxl+I5MVb6R1Q== X-Google-Smtp-Source: ABdhPJwzgm1kTDjZWbhmtm+PkBpCKfY55nrE32hW+LLykyNmYe90PofqwEScjYHpC4iEziukZCz1KBun7yI= X-Received: by 2002:a25:aa72:: with SMTP id s105mr25937506ybi.463.1597764282019; Tue, 18 Aug 2020 08:24:42 -0700 (PDT) Date: Tue, 18 Aug 2020 15:24:26 +0000 In-Reply-To: <20200818152429.1923996-1-oupton@google.com> Message-Id: <20200818152429.1923996-2-oupton@google.com> Mime-Version: 1.0 References: <20200818152429.1923996-1-oupton@google.com> X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [PATCH v4 1/4] kvm: x86: encapsulate wrmsr(MSR_KVM_SYSTEM_TIME) emulation in helper fn From: Oliver Upton To: kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Wanpeng Li , Oliver Upton , Jim Mattson , Peter Shier , Wanpeng Li Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org No functional change intended. Reviewed-by: Jim Mattson Reviewed-by: Peter Shier Reviewed-by: Wanpeng Li Signed-off-by: Oliver Upton Change-Id: I7cbe71069db98d1ded612fd2ef088b70e7618426 --- arch/x86/kvm/x86.c | 58 +++++++++++++++++++++++++--------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2db369a64f29..b7ba8eb0c91b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1823,6 +1823,34 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); } +static void kvm_write_system_time(struct kvm_vcpu *vcpu, gpa_t system_time, + bool old_msr, bool host_initiated) +{ + struct kvm_arch *ka = &vcpu->kvm->arch; + + if (vcpu->vcpu_id == 0 && !host_initiated) { + if (ka->boot_vcpu_runs_old_kvmclock && old_msr) + kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); + + ka->boot_vcpu_runs_old_kvmclock = old_msr; + } + + vcpu->arch.time = system_time; + kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); + + /* we verify if the enable bit is set... */ + vcpu->arch.pv_time_enabled = false; + if (!(system_time & 1)) + return; + + if (!kvm_gfn_to_hva_cache_init(vcpu->kvm, + &vcpu->arch.pv_time, system_time & ~1ULL, + sizeof(struct pvclock_vcpu_time_info))) + vcpu->arch.pv_time_enabled = true; + + return; +} + static uint32_t div_frac(uint32_t dividend, uint32_t divisor) { do_shl32_div32(dividend, divisor); @@ -2974,33 +3002,11 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) kvm_write_wall_clock(vcpu->kvm, data); break; case MSR_KVM_SYSTEM_TIME_NEW: - case MSR_KVM_SYSTEM_TIME: { - struct kvm_arch *ka = &vcpu->kvm->arch; - - if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) { - bool tmp = (msr == MSR_KVM_SYSTEM_TIME); - - if (ka->boot_vcpu_runs_old_kvmclock != tmp) - kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); - - ka->boot_vcpu_runs_old_kvmclock = tmp; - } - - vcpu->arch.time = data; - kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); - - /* we verify if the enable bit is set... */ - vcpu->arch.pv_time_enabled = false; - if (!(data & 1)) - break; - - if (!kvm_gfn_to_hva_cache_init(vcpu->kvm, - &vcpu->arch.pv_time, data & ~1ULL, - sizeof(struct pvclock_vcpu_time_info))) - vcpu->arch.pv_time_enabled = true; - + kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); + break; + case MSR_KVM_SYSTEM_TIME: + kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); break; - } case MSR_KVM_ASYNC_PF_EN: if (kvm_pv_enable_async_pf(vcpu, data)) return 1; From patchwork Tue Aug 18 15:24:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 11721615 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7FBB213B1 for ; Tue, 18 Aug 2020 15:24:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 66E8E207DA for ; Tue, 18 Aug 2020 15:24:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="L7An6vzn" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726630AbgHRPYv (ORCPT ); Tue, 18 Aug 2020 11:24:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728025AbgHRPYs (ORCPT ); Tue, 18 Aug 2020 11:24:48 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05763C061389 for ; Tue, 18 Aug 2020 08:24:48 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id j11so7269909plj.6 for ; Tue, 18 Aug 2020 08:24:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=VvND+6fFOfTo9BuTLPzQfieTbn/IyIHYWxUcE7b9iOg=; b=L7An6vznRn/VW+Pk8nEziwqU0U0UH/z1pc2oXqyLk2XaFOzBGsI6LglZ5/4NEAFYDI JpUesSysvMlq2DaWEUTSbY8R/Fabc1UwEeoi1oOPFzAW3qsJIvdIz6tcO2AXt64cl244 n4mkkwPAur/GU0fleEdtqxFGfPG70w7+DQkSXFUJhavjeTSoICM2MJBPobFVkujSjrBY lIN5FEN4n+2deIzbxZWnlRfhC3wLfFwb2Mrzvaaxw3VUSnYKLo9Kmh0Udp02ggIxyUzQ ySoL4Rch1doPdn34qSYP3Yd4ZY1i8QhURiclzFqXp/rsXHYex1sgZOAidS0v5eE7GV/K XPEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=VvND+6fFOfTo9BuTLPzQfieTbn/IyIHYWxUcE7b9iOg=; b=lORpVBzYreeEX4phVB37cOksctCy18MUTL8wSrY2OgusPsyXCNBjXx0c4FypSnnfl2 c0EY2RYRpAQ6fIPVhtfQrcVI2WdY4njlvzevWhjU/UzUTxEQmLJxi+FvJ1PLqKjWH08f thRc9ZdX4FeA13AeHVNXdqrNgi2V1ZzmvXZhdQ8RoM8gPRI8W3vUIM0f75PM0ym8TUpL eSSPi7oDljFOFsPPnk7GM8bSEjWc8E7vGPPLYS8Krk+xok7xAyRA4tc6Z4Z0cIIpZjWK t/JIhTy/wNxQD6JceCsC2PdKSIkGN/MeIWb3vE/KZaoM4pJBG9uxbhwzkd9g46yiC1EE 2YmA== X-Gm-Message-State: AOAM533hNrI2luc6olEzo9BdqN1y1aHyhO0qS/NJXXxyopPLzAGdrg4C 0mtzRL5z+vph06cM52jf62550ykn5gOGv9EXil1P1IN4RXTJ+DRvCJ17TIH1vymTHHsMO4TrKE/ 2gpio5S78DSn2RrB+8wpGAPmfLFx6NHIqG0Jc7qN9BIEoeIlKr8mBJIsYHw== X-Google-Smtp-Source: ABdhPJwxt5E4opAohrps2QKUPmOQFnNR9PiVSomeGgbw4fZmhCZZQ+zVXWHm/00gle1z4SH9QW3k1RShiQk= X-Received: from oupton2.c.googlers.com ([fda3:e722:ac3:10:24:72f4:c0a8:518e]) (user=oupton job=sendgmr) by 2002:a17:90a:24e6:: with SMTP id i93mr352479pje.231.1597764287377; Tue, 18 Aug 2020 08:24:47 -0700 (PDT) Date: Tue, 18 Aug 2020 15:24:27 +0000 In-Reply-To: <20200818152429.1923996-1-oupton@google.com> Message-Id: <20200818152429.1923996-3-oupton@google.com> Mime-Version: 1.0 References: <20200818152429.1923996-1-oupton@google.com> X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [PATCH v4 2/4] kvm: x86: set wall_clock in kvm_write_wall_clock() From: Oliver Upton To: kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Wanpeng Li , Oliver Upton , Jim Mattson , Peter Shier Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Small change to avoid meaningless duplication in the subsequent patch. No functional change intended. Reviewed-by: Jim Mattson Reviewed-by: Peter Shier Signed-off-by: Oliver Upton Change-Id: I77ab9cdad239790766b7a49d5cbae5e57a3005ea Reviewed-by: Wanpeng Li --- arch/x86/kvm/x86.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b7ba8eb0c91b..e16c71fe1b48 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -1791,6 +1791,8 @@ static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) struct pvclock_wall_clock wc; u64 wall_nsec; + kvm->arch.wall_clock = wall_clock; + if (!wall_clock) return; @@ -2998,7 +3000,6 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) break; case MSR_KVM_WALL_CLOCK_NEW: case MSR_KVM_WALL_CLOCK: - vcpu->kvm->arch.wall_clock = data; kvm_write_wall_clock(vcpu->kvm, data); break; case MSR_KVM_SYSTEM_TIME_NEW: From patchwork Tue Aug 18 15:24:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 11721619 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5CD35739 for ; Tue, 18 Aug 2020 15:24:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3CBA9207DA for ; Tue, 18 Aug 2020 15:24:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YXBlqJYm" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728043AbgHRPYz (ORCPT ); Tue, 18 Aug 2020 11:24:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728025AbgHRPYv (ORCPT ); Tue, 18 Aug 2020 11:24:51 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80654C061342 for ; Tue, 18 Aug 2020 08:24:51 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id o6so12431121pll.9 for ; Tue, 18 Aug 2020 08:24:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=3rwunzaIr0pe53eeZZ84cz5cFuKQcHL9hoL/E/rE2eA=; b=YXBlqJYmwKHf+y9vniL778DPvJvnhUSsNPMj+dEDXNuDZcRUdsGW86sRnVTN1UCqvO q0WDNATCKFkNAEUthHK/ybiemCKn9sjzyMhOile8zl+afWPKNlSr32EpKXgMSmE9DPRV od2ggm7zPMuFHDnUvld4htuOo1CpOYkaoam82HHHuUulJVR2agfFjaD0ujyWCytdajy/ p5RNZJQGKeDWmqhQoB0XIkiWGW2U1aLfMJwVsfzAfzlH2aNBVNbPwOHqPzD3uUFUsJfJ Fhos4PxbuCCySPiwcSu8pAfDMJetnmNK7HSfhZZYyCC7dYmXo69/slisK52Alm8cWy04 WS5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=3rwunzaIr0pe53eeZZ84cz5cFuKQcHL9hoL/E/rE2eA=; b=XOoAVduOGx4mo2X1oXgeU6ZUJlJeh23g7TRMKGbl6PIXTWlcHn7DKgtLU2t2sDFp+I /4vl0J8KuphjEv/nUiUOUr5FDE5wdPOJOHl/RnyPtliSSkTwEdkkV3JvbKNey/suolBT E8y0IYhohLm5dLaKA9o8xD8l3T20bL+6EKfh6LxfgEOLDu06+77Qix0o9IbUxx1e1tI2 hiC2tD2wQdno7bMqupvCoRKOONSmTnsADYIACg+WSJnZH+H/0KgrME7qpgUYLwjWsp+7 iCEEjPB0da4e4FchKA8gggSGGTE80jYwLG12EU7a8sfKKMcah2CXDGNM/AmCapW/Zq4/ OaHw== X-Gm-Message-State: AOAM532oqOK/TDnCToH+5FUeGOm5x49VEs4i0Yz3kM+Biqhyp07uDfEA f2gBVHBfaHEjJmblYbc2UDMXq2oCQq8+y44db2+ga2SexpEsvDALrQ0/kHu51HopvQ33CASYbbs DnooQi2xgWICj4gQuXt+lsDgZ7bROeqHEcwFWp9hxhxb9bvrMp1x/fQ4+Dw== X-Google-Smtp-Source: ABdhPJy+y7TG43oH7HCWz+kibDVJj84ncJwDOVGa2yYZtUgIMzqKyee4tbnM+5UUVVN+Xx/SlW6VutW/sRU= X-Received: by 2002:a63:1a49:: with SMTP id a9mr13576701pgm.110.1597764290672; Tue, 18 Aug 2020 08:24:50 -0700 (PDT) Date: Tue, 18 Aug 2020 15:24:28 +0000 In-Reply-To: <20200818152429.1923996-1-oupton@google.com> Message-Id: <20200818152429.1923996-4-oupton@google.com> Mime-Version: 1.0 References: <20200818152429.1923996-1-oupton@google.com> X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [PATCH v4 3/4] kvm: x86: only provide PV features if enabled in guest's CPUID From: Oliver Upton To: kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Wanpeng Li , Oliver Upton , Jim Mattson , Peter Shier Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org KVM unconditionally provides PV features to the guest, regardless of the configured CPUID. An unwitting guest that doesn't check KVM_CPUID_FEATURES before use could access paravirt features that userspace did not intend to provide. Fix this by checking the guest's CPUID before performing any paravirtual operations. Introduce a capability, KVM_CAP_ENFORCE_PV_FEATURE_CPUID, to gate the aforementioned enforcement. Migrating a VM from a host w/o this patch to a host with this patch could silently change the ABI exposed to the guest, warranting that we default to the old behavior and opt-in for the new one. Reviewed-by: Jim Mattson Reviewed-by: Peter Shier Signed-off-by: Oliver Upton Change-Id: I202a0926f65035b872bfe8ad15307c026de59a98 Reviewed-by: Wanpeng Li --- Documentation/virt/kvm/api.rst | 11 ++++++ arch/x86/include/asm/kvm_host.h | 15 ++++++++ arch/x86/kvm/cpuid.c | 7 ++++ arch/x86/kvm/cpuid.h | 10 +++++ arch/x86/kvm/x86.c | 67 ++++++++++++++++++++++++++++++--- include/uapi/linux/kvm.h | 1 + 6 files changed, 106 insertions(+), 5 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index eb3a1316f03e..c9e4f092f743 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6159,3 +6159,14 @@ KVM can therefore start protected VMs. This capability governs the KVM_S390_PV_COMMAND ioctl and the KVM_MP_STATE_LOAD MP_STATE. KVM_SET_MP_STATE can fail for protected guests when the state change is invalid. + + +8.24 KVM_CAP_ENFORCE_PV_CPUID +----------------------------- + +Architectures: x86 + +When enabled, KVM will disable paravirtual features provided to the +guest according to the bits in the KVM_CPUID_FEATURES CPUID leaf +(0x40000001). Otherwise, a guest may use the paravirtual features +regardless of what has actually been exposed through the CPUID leaf. diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 5ab3af7275d8..e0c65e319bdf 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -788,6 +788,21 @@ struct kvm_vcpu_arch { /* AMD MSRC001_0015 Hardware Configuration */ u64 msr_hwcr; + + /* pv related cpuid info */ + struct { + /* + * value of the eax register in the KVM_CPUID_FEATURES CPUID + * leaf. + */ + u32 features; + + /* + * indicates whether pv emulation should be disabled if features + * are not present in the guest's cpuid + */ + bool enforce; + } pv_cpuid; }; struct kvm_lpage_info { diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 3fd6eec202d7..48fb2ea74906 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -107,6 +107,13 @@ void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu) (best->eax & (1 << KVM_FEATURE_PV_UNHALT))) best->eax &= ~(1 << KVM_FEATURE_PV_UNHALT); + /* + * save the feature bitmap to avoid cpuid lookup for every PV + * operation + */ + if (best) + vcpu->arch.pv_cpuid.features = best->eax; + if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT)) { best = kvm_find_cpuid_entry(vcpu, 0x1, 0); if (best) diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h index 3a923ae15f2f..4e2d2e767984 100644 --- a/arch/x86/kvm/cpuid.h +++ b/arch/x86/kvm/cpuid.h @@ -5,6 +5,7 @@ #include "x86.h" #include #include +#include extern u32 kvm_cpu_caps[NCAPINTS] __read_mostly; void kvm_set_cpu_caps(void); @@ -308,4 +309,13 @@ static inline bool page_address_valid(struct kvm_vcpu *vcpu, gpa_t gpa) return PAGE_ALIGNED(gpa) && !(gpa >> cpuid_maxphyaddr(vcpu)); } +static __always_inline bool guest_pv_has(struct kvm_vcpu *vcpu, + unsigned int kvm_feature) +{ + if (!vcpu->arch.pv_cpuid.enforce) + return true; + + return vcpu->arch.pv_cpuid.features & (1u << kvm_feature); +} + #endif diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e16c71fe1b48..941ab75cf572 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2764,6 +2764,14 @@ static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data) if (data & 0x30) return 1; + if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_VMEXIT) && + (data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT)) + return 1; + + if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT) && + (data & KVM_ASYNC_PF_DELIVERY_AS_INT)) + return 1; + if (!lapic_in_kernel(vcpu)) return 1; @@ -2841,10 +2849,12 @@ static void record_steal_time(struct kvm_vcpu *vcpu) * Doing a TLB flush here, on the guest's behalf, can avoid * expensive IPIs. */ - trace_kvm_pv_tlb_flush(vcpu->vcpu_id, - st->preempted & KVM_VCPU_FLUSH_TLB); - if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) - kvm_vcpu_flush_tlb_guest(vcpu); + if (guest_pv_has(vcpu, KVM_FEATURE_PV_TLB_FLUSH)) { + trace_kvm_pv_tlb_flush(vcpu->vcpu_id, + st->preempted & KVM_VCPU_FLUSH_TLB); + if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) + kvm_vcpu_flush_tlb_guest(vcpu); + } vcpu->arch.st.preempted = 0; @@ -2999,30 +3009,54 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.smi_count = data; break; case MSR_KVM_WALL_CLOCK_NEW: + if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) + return 1; + + kvm_write_wall_clock(vcpu->kvm, data); + break; case MSR_KVM_WALL_CLOCK: + if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) + return 1; + kvm_write_wall_clock(vcpu->kvm, data); break; case MSR_KVM_SYSTEM_TIME_NEW: + if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE2)) + return 1; + kvm_write_system_time(vcpu, data, false, msr_info->host_initiated); break; case MSR_KVM_SYSTEM_TIME: - kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); + if (!guest_pv_has(vcpu, KVM_FEATURE_CLOCKSOURCE)) + return 1; + + kvm_write_system_time(vcpu, data, true, msr_info->host_initiated); break; case MSR_KVM_ASYNC_PF_EN: + if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) + return 1; + if (kvm_pv_enable_async_pf(vcpu, data)) return 1; break; case MSR_KVM_ASYNC_PF_INT: + if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF_INT)) + return 1; + if (kvm_pv_enable_async_pf_int(vcpu, data)) return 1; break; case MSR_KVM_ASYNC_PF_ACK: + if (!guest_pv_has(vcpu, KVM_FEATURE_ASYNC_PF)) + return 1; if (data & 0x1) { vcpu->arch.apf.pageready_pending = false; kvm_check_async_pf_completion(vcpu); } break; case MSR_KVM_STEAL_TIME: + if (!guest_pv_has(vcpu, KVM_FEATURE_STEAL_TIME)) + return 1; if (unlikely(!sched_info_on())) return 1; @@ -3039,11 +3073,17 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) break; case MSR_KVM_PV_EOI_EN: + if (!guest_pv_has(vcpu, KVM_FEATURE_PV_EOI)) + return 1; + if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8))) return 1; break; case MSR_KVM_POLL_CONTROL: + if (!guest_pv_has(vcpu, KVM_FEATURE_POLL_CONTROL)) + return 1; + /* only enable bit supported */ if (data & (-1ULL << 1)) return 1; @@ -3523,6 +3563,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_EXCEPTION_PAYLOAD: case KVM_CAP_SET_GUEST_DEBUG: case KVM_CAP_LAST_CPU: + case KVM_CAP_ENFORCE_PV_FEATURE_CPUID: r = 1; break; case KVM_CAP_SYNC_REGS: @@ -4390,6 +4431,11 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, return kvm_x86_ops.enable_direct_tlbflush(vcpu); + case KVM_CAP_ENFORCE_PV_FEATURE_CPUID: + vcpu->arch.pv_cpuid.enforce = cap->args[0]; + + return 0; + default: return -EINVAL; } @@ -7724,11 +7770,16 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) goto out; } + ret = -KVM_ENOSYS; + switch (nr) { case KVM_HC_VAPIC_POLL_IRQ: ret = 0; break; case KVM_HC_KICK_CPU: + if (!guest_pv_has(vcpu, KVM_FEATURE_PV_UNHALT)) + break; + kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1); kvm_sched_yield(vcpu->kvm, a1); ret = 0; @@ -7739,9 +7790,15 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) break; #endif case KVM_HC_SEND_IPI: + if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SEND_IPI)) + break; + ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); break; case KVM_HC_SCHED_YIELD: + if (!guest_pv_has(vcpu, KVM_FEATURE_PV_SCHED_YIELD)) + break; + kvm_sched_yield(vcpu->kvm, a0); ret = 0; break; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index f6d86033c4fa..48c2d5c10b1e 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1035,6 +1035,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_LAST_CPU 184 #define KVM_CAP_SMALLER_MAXPHYADDR 185 #define KVM_CAP_S390_DIAG318 186 +#define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 187 #ifdef KVM_CAP_IRQ_ROUTING From patchwork Tue Aug 18 15:24:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 11721623 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 89EE713B1 for ; Tue, 18 Aug 2020 15:25:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6B40A2076E for ; Tue, 18 Aug 2020 15:25:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bRdidz6c" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728033AbgHRPZC (ORCPT ); Tue, 18 Aug 2020 11:25:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728044AbgHRPYz (ORCPT ); Tue, 18 Aug 2020 11:24:55 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8D302C061344 for ; Tue, 18 Aug 2020 08:24:55 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id w17so22316852ybl.9 for ; Tue, 18 Aug 2020 08:24:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ss9Mm6++2J6/bl4nk0srPijFVv+n5iynw0aopEfHAP8=; b=bRdidz6ciEQKTHEBe8HFfV2yYMnxPuvNxt/bibWg+1By92BYN3dIviveUk8q/W9JwA /nR44Bd49/AvmeViZD/mxbf7OXuCQw1bjdShVitXvoVA42kZbbXMIPDxc3Ki7AoHmXlf kuFv4T+XH/UpeHadRfwxfu1wpyx6V5JJ8pcyqp14vIE/8G76QUBPUWF7h4XEHc6Kv32Z 2iQnKiRguaO3AyoqDdfgRuLTLzfIkABHkQXJliyVKygj6S+Q8JN4tNyJ1C3Y1QVdv2BH 8RWsYtPS6BRnpXkqXgcwKWbBeSRE4j9nXY+lh3YZTWfj4EW7PTVNnuNV5ogDAJIOe9aI xkGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ss9Mm6++2J6/bl4nk0srPijFVv+n5iynw0aopEfHAP8=; b=B6DBzxhWFMe5U//khMr/kJr72IlMUNMWLrAa/nCFWwhXMxtuLI2KYsvCRlliAuxHR8 RjfP1n4FDi9g+gxP21eVxKAxNdzKsRXLd7KWylVYGcV5AY+TKCYRV5m2X40LPj5troTn 6jsqqdSui3lxmGKNnrg0OkXEnCV3hSJa/l4DeyjQntKJbxGMb/3rtYaazxsqkc59vAtN RVKdTMppmeTEu4y/1Sc+SIDptb/Vo4q0Jp3rWZ5Bb4iVvNxsNNsRoJcqeS/89YhEClPL tbRK3cwYc1Rnf+vAkWUwkfvLAtHIe2rECXK+dmqG0otpT/jAnzzww3FCG9i/E4xqPA+L zPQg== X-Gm-Message-State: AOAM532E9irzXzByxiffZ8VvNx1s5pbFADQWWmSlLNCof3NV4L9FkmkY ud2IagLwywaWeuuR/smHiSR4kA3omU3RE0fs4n+P85vljdQiPH+MkCxFpKS3BwTYZNjA0ScV8Eq t/Od7YeNkFTOcZ7nSoYDHb1sI0miJ/F7wSPcU7ZqKb1SGuwZXexOD60wemw== X-Google-Smtp-Source: ABdhPJz0R8oJfiZQxKsTcoDdGNpoOe2G30i1NmbyK8g7YffSHnyouM1ztL43yv8Vw74vEW2ulBn0Z8hnWYY= X-Received: by 2002:a25:4846:: with SMTP id v67mr27778033yba.103.1597764294715; Tue, 18 Aug 2020 08:24:54 -0700 (PDT) Date: Tue, 18 Aug 2020 15:24:29 +0000 In-Reply-To: <20200818152429.1923996-1-oupton@google.com> Message-Id: <20200818152429.1923996-5-oupton@google.com> Mime-Version: 1.0 References: <20200818152429.1923996-1-oupton@google.com> X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [PATCH v4 4/4] Documentation: kvm: fix some typos in cpuid.rst From: Oliver Upton To: kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Wanpeng Li , Oliver Upton , Jim Mattson , Peter Shier Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Reviewed-by: Jim Mattson Reviewed-by: Peter Shier Signed-off-by: Oliver Upton Change-Id: I0c6355b09fedf8f9cc4cc5f51be418e2c1c82b7b --- Documentation/virt/kvm/cpuid.rst | 88 ++++++++++++++++---------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/Documentation/virt/kvm/cpuid.rst b/Documentation/virt/kvm/cpuid.rst index a7dff9186bed..f1583e682cc8 100644 --- a/Documentation/virt/kvm/cpuid.rst +++ b/Documentation/virt/kvm/cpuid.rst @@ -38,64 +38,64 @@ returns:: where ``flag`` is defined as below: -================================= =========== ================================ -flag value meaning -================================= =========== ================================ -KVM_FEATURE_CLOCKSOURCE 0 kvmclock available at msrs - 0x11 and 0x12 +================================== =========== ================================ +flag value meaning +================================== =========== ================================ +KVM_FEATURE_CLOCKSOURCE 0 kvmclock available at msrs + 0x11 and 0x12 -KVM_FEATURE_NOP_IO_DELAY 1 not necessary to perform delays - on PIO operations +KVM_FEATURE_NOP_IO_DELAY 1 not necessary to perform delays + on PIO operations -KVM_FEATURE_MMU_OP 2 deprecated +KVM_FEATURE_MMU_OP 2 deprecated -KVM_FEATURE_CLOCKSOURCE2 3 kvmclock available at msrs - 0x4b564d00 and 0x4b564d01 +KVM_FEATURE_CLOCKSOURCE2 3 kvmclock available at msrs + 0x4b564d00 and 0x4b564d01 -KVM_FEATURE_ASYNC_PF 4 async pf can be enabled by - writing to msr 0x4b564d02 +KVM_FEATURE_ASYNC_PF 4 async pf can be enabled by + writing to msr 0x4b564d02 -KVM_FEATURE_STEAL_TIME 5 steal time can be enabled by - writing to msr 0x4b564d03 +KVM_FEATURE_STEAL_TIME 5 steal time can be enabled by + writing to msr 0x4b564d03 -KVM_FEATURE_PV_EOI 6 paravirtualized end of interrupt - handler can be enabled by - writing to msr 0x4b564d04 +KVM_FEATURE_PV_EOI 6 paravirtualized end of interrupt + handler can be enabled by + writing to msr 0x4b564d04 -KVM_FEATURE_PV_UNHAULT 7 guest checks this feature bit - before enabling paravirtualized - spinlock support +KVM_FEATURE_PV_UNHALT 7 guest checks this feature bit + before enabling paravirtualized + spinlock support -KVM_FEATURE_PV_TLB_FLUSH 9 guest checks this feature bit - before enabling paravirtualized - tlb flush +KVM_FEATURE_PV_TLB_FLUSH 9 guest checks this feature bit + before enabling paravirtualized + tlb flush -KVM_FEATURE_ASYNC_PF_VMEXIT 10 paravirtualized async PF VM EXIT - can be enabled by setting bit 2 - when writing to msr 0x4b564d02 +KVM_FEATURE_ASYNC_PF_VMEXIT 10 paravirtualized async PF VM EXIT + can be enabled by setting bit 2 + when writing to msr 0x4b564d02 -KVM_FEATURE_PV_SEND_IPI 11 guest checks this feature bit - before enabling paravirtualized - sebd IPIs +KVM_FEATURE_PV_SEND_IPI 11 guest checks this feature bit + before enabling paravirtualized + send IPIs -KVM_FEATURE_PV_POLL_CONTROL 12 host-side polling on HLT can - be disabled by writing - to msr 0x4b564d05. +KVM_FEATURE_PV_POLL_CONTROL 12 host-side polling on HLT can + be disabled by writing + to msr 0x4b564d05. -KVM_FEATURE_PV_SCHED_YIELD 13 guest checks this feature bit - before using paravirtualized - sched yield. +KVM_FEATURE_PV_SCHED_YIELD 13 guest checks this feature bit + before using paravirtualized + sched yield. -KVM_FEATURE_ASYNC_PF_INT 14 guest checks this feature bit - before using the second async - pf control msr 0x4b564d06 and - async pf acknowledgment msr - 0x4b564d07. +KVM_FEATURE_ASYNC_PF_INT 14 guest checks this feature bit + before using the second async + pf control msr 0x4b564d06 and + async pf acknowledgment msr + 0x4b564d07. -KVM_FEATURE_CLOCSOURCE_STABLE_BIT 24 host will warn if no guest-side - per-cpu warps are expeced in - kvmclock -================================= =========== ================================ +KVM_FEATURE_CLOCKSOURCE_STABLE_BIT 24 host will warn if no guest-side + per-cpu warps are expected in + kvmclock +================================== =========== ================================ ::