From patchwork Wed Feb 1 13:29:03 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: 51330 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp280053wrn; Wed, 1 Feb 2023 05:34:05 -0800 (PST) X-Google-Smtp-Source: AK7set+Z/xLeaHz+tU2QEWcAUEDkJyzXTxLlE5f+ZV+Wj5TIEROA8Z3egbKmi0emzlCqOXbtmL/O X-Received: by 2002:a17:902:c403:b0:196:8a80:4d91 with SMTP id k3-20020a170902c40300b001968a804d91mr3332535plk.35.1675258445306; Wed, 01 Feb 2023 05:34:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675258445; cv=none; d=google.com; s=arc-20160816; b=x297hVFi8Mo6T93eyujWDzzQWnMUxoQ3AzVZJoF+US+LMQyCl8u1NwVuISP74gyWc1 IQQ3KX78UXIG1AMJvYK70RUupRALmyIyoCapA3hmgp4r1u0Q4St07qB0imxiBe5sPbEF p21SawM82ze1gF7jJC/isxBueuED4v3fLbZOxXI9Z4R+K6a3qGuiKYdldORKRdni2eaO YM7JJ203sjAhi4db4SvI+InOowS1duOKPg4jha9KD2eGrESe34d/8rcdgClpZ2eRp+WX toSmTEVDf/5F4hmd0Okjkv53hauq69ZR3u/4JSpwdWIosJr1AC3A/WV/04Oxt5PzOfOz +nCw== 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=XCDj+BnS4gsfltBgpuq4/PEw1EkFceCEZmDi9yc7JXM=; b=MeCOAeaRBJbpfjQZJpF08j05HyDIWULmZNYAbQpXUF3z1Wrid/f7BnFj6sVsOFeG7z 0xuo9ivjOKqHFge1fyZtk0zPSWYYS0Z2LAE1OaNUJtlIfEC1fj0+gfe2b3wxxVVqslRH S+auflvrR4bw1s4KyJBFdfO94puTqCXAvCkG4oRzpqYSJ07cWIUhDOCGgilAF8rM6oQ+ eo+oOe9yfBMNpVijlBWULFwB/HDnyFJqa9GBP0kScRDkuYjA+pQI5hcpejBysGIC6QVZ 3uSB7aqn17bX7W1Td5mqbCD+pzCGGzRbf/wodglb6akF5PjhArSgGdw2zSwL9LdZF1ZK RWcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=fpCYAD5Y; 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 w5-20020a170902a70500b001944e99d38fsi18018602plq.557.2023.02.01.05.33.52; Wed, 01 Feb 2023 05:34:05 -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=fpCYAD5Y; 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 S232463AbjBANa6 (ORCPT + 99 others); Wed, 1 Feb 2023 08:30:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231582AbjBANaj (ORCPT ); Wed, 1 Feb 2023 08:30:39 -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 999BB646B3 for ; Wed, 1 Feb 2023 05:29:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1675258155; 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=XCDj+BnS4gsfltBgpuq4/PEw1EkFceCEZmDi9yc7JXM=; b=fpCYAD5Ygt96wMEbXVWDzJw1hjvUN7EDv41DaRBodvEAflwcWZ3+aNprjXyTu9OgcSkoxS MoktPYyH0zY09l5I8jqSAyndiAgjJTjGsJwrbI8Qdo9euRWwTzkI5+3RZ1KfK/Cm5mwH2+ tlP3DJbrFd6qRNgWAYBq5nZjYZgz9PM= 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-583-Pt1cGpLqNAeI-X_Ovxjr6g-1; Wed, 01 Feb 2023 08:29:10 -0500 X-MC-Unique: Pt1cGpLqNAeI-X_Ovxjr6g-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 4292718A6460; 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 DA10B40C2064; Wed, 1 Feb 2023 13:29:08 +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 1/3] kvm: vmx: Add IA32_FLUSH_CMD guest support Date: Wed, 1 Feb 2023 08:29:03 -0500 Message-Id: <20230201132905.549148-2-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=unavailable 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?1756635799262319935?= X-GMAIL-MSGID: =?utf-8?q?1756635799262319935?= 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: Jim Mattson Signed-off-by: Emanuele Giuseppe Esposito Tested-by: Nathan Chancellor --- arch/x86/kvm/vmx/nested.c | 3 ++ arch/x86/kvm/vmx/vmx.c | 70 +++++++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 557b9c468734..075b5ade7c80 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -654,6 +654,9 @@ static inline bool nested_vmx_prepare_msr_bitmap(struct kvm_vcpu *vcpu, nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, MSR_IA32_PRED_CMD, MSR_TYPE_W); + nested_vmx_set_intercept_for_msr(vmx, msr_bitmap_l1, msr_bitmap_l0, + MSR_IA32_FLUSH_CMD, MSR_TYPE_W); + kvm_vcpu_unmap(vcpu, &vmx->nested.msr_bitmap_map, false); vmx->nested.force_msr_bitmap_recalc = false; diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index c788aa382611..9a78ea96a6d7 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2133,6 +2133,39 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated return debugctl; } +static int vmx_set_msr_ia32_cmd(struct kvm_vcpu *vcpu, + struct msr_data *msr_info, + bool guest_has_feat, u64 cmd, + int x86_feature_bit) +{ + if (!msr_info->host_initiated && !guest_has_feat) + return 1; + + if (!(msr_info->data & ~cmd)) + return 1; + if (!boot_cpu_has(x86_feature_bit)) + return 1; + if (!msr_info->data) + return 0; + + wrmsrl(msr_info->index, cmd); + + /* + * For non-nested: + * When it's written (to non-zero) for the first time, pass + * it through. + * + * For nested: + * The handling of the MSR bitmap for L2 guests is done in + * nested_vmx_prepare_msr_bitmap. We should not touch the + * vmcs02.msr_bitmap here since it gets completely overwritten + * in the merging. + */ + vmx_disable_intercept_for_msr(vcpu, msr_info->index, MSR_TYPE_W); + + return 0; +} + /* * Writes msr value into the appropriate "register". * Returns 0 on success, non-0 otherwise. @@ -2288,31 +2321,18 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) return 1; goto find_uret_msr; case MSR_IA32_PRED_CMD: - if (!msr_info->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); - - /* - * For non-nested: - * When it's written (to non-zero) for the first time, pass - * it through. - * - * For nested: - * The handling of the MSR bitmap for L2 guests is done in - * nested_vmx_prepare_msr_bitmap. We should not touch the - * vmcs02.msr_bitmap here since it gets completely overwritten - * in the merging. - */ - vmx_disable_intercept_for_msr(vcpu, MSR_IA32_PRED_CMD, MSR_TYPE_W); + ret = vmx_set_msr_ia32_cmd(vcpu, msr_info, + 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); + ret = vmx_set_msr_ia32_cmd(vcpu, msr_info, + guest_flush_l1d, + L1D_FLUSH, + X86_FEATURE_FLUSH_L1D); break; case MSR_IA32_CR_PAT: if (!kvm_pat_valid(data))