From patchwork Wed Feb 1 13:29:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 51329 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp279628wrn; Wed, 1 Feb 2023 05:33:17 -0800 (PST) X-Google-Smtp-Source: AK7set+VMZlkEF+u6hTYvXS1jaCaAG6pEnq8u97/x7S5aLa7kWAQqeMPl9+zLTFWu1/W5iSEbFPL X-Received: by 2002:a17:90b:4c81:b0:22c:69b6:2be8 with SMTP id my1-20020a17090b4c8100b0022c69b62be8mr2443045pjb.8.1675258397418; Wed, 01 Feb 2023 05:33:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675258397; cv=none; d=google.com; s=arc-20160816; b=VTEbG/JI2Tsourhneg6cJD8LZ5ExJSAs1iwjAWmPhhNl0NTzOZLOZ5n0nEdJJiXdyD tbHp4Cm0DVFbf28pB3aZMBgMyB1icy74Mk9gDpR07XUTtjptbH9hkb0V793sFc/1EZmJ nqBZkgvDIcjdM7yvBIa9H5aF2YvIwX+i3gs1+27WW+kvGHWI/4tOL8SoyVTGbGssW40r nmSpyw50jyJGphiKNWRnGpmMd7HtWm7zl/u5WJQjKJteo/jxANjES7Ab28L3F4VQyad3 n764nFHAnpROpYsHm2L9MpGFHyyRo1bSp5JJ1ZuZZiOCSK9cpgIWtq/m3Gvxbos+ocKs XMTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=n5g5BRaVE7Y5sLa48yF1mcVGIt0PT9vmJ+7t6OX7GDw=; b=EyAxaMSuK5zQbTNakZydexJFpgccnrhchEPwb9qjaMCIpIph7N5mEEjydt1TACxBb+ yKUQyaVqOsGf2BmeN7x4AJlhDA/PjqwkQO6Yapd/JswkxSf6OqaI9hwZdLqL7R7G9cPR 7y1Z4efDB2ATKVRs6VIdR3DRSE3XANa6e6P7r73PjEdd5kz32MwJMEptOiWSfD7deH2Q OpWbP/jgGtydz1lLYIYG6uzhlKbgpeGspsAP+Os2RUzsKwK49GIhtJpzVc2s53LAPD6X t3A4O1V9eZImi99Ve+I+68Z11BmW6dIpSi2BA3r828p/AHz3SRqBrKHVolQJmMjRZza1 piug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=evrQxQLj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s61-20020a17090a2f4300b002271de285easi1541876pjd.139.2023.02.01.05.33.04; Wed, 01 Feb 2023 05:33:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=evrQxQLj; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232340AbjBANae (ORCPT + 99 others); Wed, 1 Feb 2023 08:30:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53238 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232282AbjBANaH (ORCPT ); Wed, 1 Feb 2023 08:30:07 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0047410B2 for ; Wed, 1 Feb 2023 05:29:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675258154; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=n5g5BRaVE7Y5sLa48yF1mcVGIt0PT9vmJ+7t6OX7GDw=; b=evrQxQLjY75uEDbxtMNI/F8NI+1nrTJtLJ9YJ4j+zq5u6ilHHDO32lJ23S4/6l3LwoKIZa MC8JzIfZ6ka1gufwqRqfpKf2iruxJ8xTzm55c3xbRahUoq/bGdvaVoCTRyAAjFFm7E0K/Y gvfc7WwjYAbUdjLB6nHser7IWdbItTo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-315-82CvK5LLMseqK97uy1yOKA-1; Wed, 01 Feb 2023 08:29:11 -0500 X-MC-Unique: 82CvK5LLMseqK97uy1yOKA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A9289802C17; Wed, 1 Feb 2023 13:29:09 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4C33140C2064; Wed, 1 Feb 2023 13:29:09 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Jim Mattson , Ben Serebrin , Peter Shier , Sean Christopherson , Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , Maxim Levitsky , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH 2/3] kvm: svm: Add IA32_FLUSH_CMD guest support Date: Wed, 1 Feb 2023 08:29:04 -0500 Message-Id: <20230201132905.549148-3-eesposit@redhat.com> In-Reply-To: <20230201132905.549148-1-eesposit@redhat.com> References: <20230201132905.549148-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756635749456836703?= X-GMAIL-MSGID: =?utf-8?q?1756635749456836703?= Expose IA32_FLUSH_CMD to the guest if the guest CPUID enumerates support for this MSR. As with IA32_PRED_CMD, permission for unintercepted writes to this MSR will be granted to the guest after the first non-zero write. Signed-off-by: Emanuele Giuseppe Esposito --- arch/x86/kvm/svm/svm.c | 44 +++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index d13cf53e7390..8a4fa8edf6ee 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -2866,6 +2866,28 @@ static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) return 0; } +static int svm_set_msr_ia32_cmd(struct kvm_vcpu *vcpu, struct msr_data *msr, + bool guest_has_feat, u64 cmd, + int x86_feature_bit) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + if (!msr->host_initiated && !guest_has_feat) + return 1; + + if (!(msr->data & ~cmd)) + return 1; + if (!boot_cpu_has(x86_feature_bit)) + return 1; + if (!msr->data) + return 0; + + wrmsrl(msr->index, cmd); + set_msr_interception(vcpu, svm->msrpm, msr->index, 0, 1); + + return 0; +} + static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) { struct vcpu_svm *svm = to_svm(vcpu); @@ -2940,19 +2962,15 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) set_msr_interception(vcpu, svm->msrpm, MSR_IA32_SPEC_CTRL, 1, 1); break; case MSR_IA32_PRED_CMD: - if (!msr->host_initiated && - !guest_has_pred_cmd_msr(vcpu)) - return 1; - - if (data & ~PRED_CMD_IBPB) - return 1; - if (!boot_cpu_has(X86_FEATURE_IBPB)) - return 1; - if (!data) - break; - - wrmsrl(MSR_IA32_PRED_CMD, PRED_CMD_IBPB); - set_msr_interception(vcpu, svm->msrpm, MSR_IA32_PRED_CMD, 0, 1); + r = svm_set_msr_ia32_cmd(vcpu, msr, + guest_has_pred_cmd_msr(vcpu), + PRED_CMD_IBPB, X86_FEATURE_IBPB); + break; + case MSR_IA32_FLUSH_CMD: + bool guest_flush_l1d = guest_cpuid_has(vcpu, + X86_FEATURE_FLUSH_L1D); + r = svm_set_msr_ia32_cmd(vcpu, msr, guest_flush_l1d, + L1D_FLUSH, X86_FEATURE_FLUSH_L1D); break; case MSR_AMD64_VIRT_SPEC_CTRL: if (!msr->host_initiated &&