Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp10508136rwp; Thu, 20 Jul 2023 23:39:24 -0700 (PDT) X-Google-Smtp-Source: APBJJlF000qRDYyHIdTt9BTrWY8rAofm/Y2PJ1Wa5e4Zy53P8cZR+zD4M0x7lHyAqcwUAfnhy87h X-Received: by 2002:a50:ed88:0:b0:51e:253e:1e0c with SMTP id h8-20020a50ed88000000b0051e253e1e0cmr929922edr.25.1689921564562; Thu, 20 Jul 2023 23:39:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689921564; cv=none; d=google.com; s=arc-20160816; b=x3aQGl/6amADardDuML5D5gwdvkgUqzL+Zqh7t8vTzYtJPBPI8P3wBmfBvYxbh24/w gXUcmql1vce4zUghFHktB9DB162h8a5YGqspN+alQOl/0HzTgR35wgPbRehZtOd6RqUB l7H8hin/+NsiiKvvqrxyjBWjjrlR4cGVTgMACsFKyAsUoN9sh1t0d/FSOx5iDkA5OgPF XpvLUrgeM7s9jNhV7Fbbx9L4HqJ1ZsRXLS/gnNY6+D1HVNqU9KBwZjtGDZ6Cm3rq+qwx h7js5rNtXAQHtf1GgMSM8Pal90tQvf7mBkpkvQvSn+J3t5UFLIpAkQ+/ed1PmWiHscUa Qhyw== 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=wL3bpe8TZfyc+w0gAobgedNnNMZtkyMGMr73YyVdauc=; fh=HMcujsZKjNxN/43ffopDJtVTR+LGhIldb/bc7LMNveI=; b=gFALCuFkgTI1cy0IFlIDp3y2MxQ8+4YzdzcHk0B6HvFnZrSajJSRY3pg2SJPKuKUHp gv7bIjUDTNv1MQXY5mgtscgvsSSNI2XqofhcO7dlFZw5Tn4UgdYmXwUK8tlcATc55jl5 QdHuGVR92cps4XaRBh6Hi1EBmmldTnHPu+qHZQFDBSAP5PsHdGwOXXExAXA+CDXoxB+N OD9OgpOTJqo7SbxZ0coI4UT2E5jdJtUkWnTcB7ils6SueZopIIbd108PORRrS9/53eQV uQRfn3JkF5171cQTTCbUWppKiOGND8LxT1/VWkgIdQLAh9vrkWPbw27zsiMnCfAOvXat vrMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EW9Dx781; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b26-20020aa7c6da000000b00521b34387e7si2034332eds.79.2023.07.20.23.39.00; Thu, 20 Jul 2023 23:39:24 -0700 (PDT) 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=@intel.com header.s=Intel header.b=EW9Dx781; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230247AbjGUGJN (ORCPT + 99 others); Fri, 21 Jul 2023 02:09:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229970AbjGUGI4 (ORCPT ); Fri, 21 Jul 2023 02:08:56 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99B1E10F3; Thu, 20 Jul 2023 23:08:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689919735; x=1721455735; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UcCgJA2s4rP276zztg85CBo/7zr+hvds9+mewB5nILs=; b=EW9Dx7815HChxJLjeAJ3wBqdYUcTX0ekdn3SqZXS+uhC/9E0RbB4ZTJY dhfPSatRUUsAQQ8Da8bTS2zLG8bkb2domt8clystvzLFD/6y6VZAkgNij EsnBftfUOo4VOuoRGapSw97sveojG4iaxjKi+h+VIH+WS+fHfAeOJhfWW wZMyyp8/w1EdTBf5Vws/tzw1HboroNCy2LbB03V3SWzxD5iIw5jg26Rap Qc5QCBgWkhHzvCBbYnvRsvujH50XNApb4SCJdWeuecZcBbwCjxawylDWo LXGGDIP9I7J5ZS0tEr9gXsENDOuadRS48Ij2zMOq/S9k1CAw/uKsLqxvW g==; X-IronPort-AV: E=McAfee;i="6600,9927,10777"; a="370547548" X-IronPort-AV: E=Sophos;i="6.01,220,1684825200"; d="scan'208";a="370547548" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2023 23:08:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10777"; a="848721886" X-IronPort-AV: E=Sophos;i="6.01,220,1684825200"; d="scan'208";a="848721886" Received: from embargo.jf.intel.com ([10.165.9.183]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 Jul 2023 23:08:40 -0700 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, peterz@infradead.org, john.allen@amd.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com, chao.gao@intel.com, binbin.wu@linux.intel.com, weijiang.yang@intel.com, Zhang Yi Z Subject: [PATCH v4 04/20] KVM:x86: Refresh CPUID on write to guest MSR_IA32_XSS Date: Thu, 20 Jul 2023 23:03:36 -0400 Message-Id: <20230721030352.72414-5-weijiang.yang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20230721030352.72414-1-weijiang.yang@intel.com> References: <20230721030352.72414-1-weijiang.yang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DATE_IN_PAST_03_06, DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED 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 Update CPUID(EAX=0DH,ECX=1) when the guest's XSS is modified. CPUID(EAX=0DH,ECX=1).EBX reports required storage size of all enabled xstate features in XCR0 | XSS. Guest can allocate sufficient xsave buffer based on the info. Note, KVM does not yet support any XSS based features, i.e. supported_xss is guaranteed to be zero at this time. Co-developed-by: Zhang Yi Z Signed-off-by: Zhang Yi Z Signed-off-by: Yang Weijiang --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/cpuid.c | 20 ++++++++++++++++++-- arch/x86/kvm/x86.c | 8 +++++--- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 28bd38303d70..20bbcd95511f 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -804,6 +804,7 @@ struct kvm_vcpu_arch { u64 xcr0; u64 guest_supported_xcr0; + u64 guest_supported_xss; struct kvm_pio_request pio; void *pio_data; diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 7f4d13383cf2..0338316b827c 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -249,6 +249,17 @@ static u64 cpuid_get_supported_xcr0(struct kvm_cpuid_entry2 *entries, int nent) return (best->eax | ((u64)best->edx << 32)) & kvm_caps.supported_xcr0; } +static u64 cpuid_get_supported_xss(struct kvm_cpuid_entry2 *entries, int nent) +{ + struct kvm_cpuid_entry2 *best; + + best = cpuid_entry2_find(entries, nent, 0xd, 1); + if (!best) + return 0; + + return (best->ecx | ((u64)best->edx << 32)) & kvm_caps.supported_xss; +} + static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *entries, int nent) { @@ -276,8 +287,11 @@ static void __kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu, struct kvm_cpuid_e best = cpuid_entry2_find(entries, nent, 0xD, 1); if (best && (cpuid_entry_has(best, X86_FEATURE_XSAVES) || - cpuid_entry_has(best, X86_FEATURE_XSAVEC))) - best->ebx = xstate_required_size(vcpu->arch.xcr0, true); + cpuid_entry_has(best, X86_FEATURE_XSAVEC))) { + u64 xstate = vcpu->arch.xcr0 | vcpu->arch.ia32_xss; + + best->ebx = xstate_required_size(xstate, true); + } best = __kvm_find_kvm_cpuid_features(vcpu, entries, nent); if (kvm_hlt_in_guest(vcpu->kvm) && best && @@ -325,6 +339,8 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) vcpu->arch.guest_supported_xcr0 = cpuid_get_supported_xcr0(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent); + vcpu->arch.guest_supported_xss = + cpuid_get_supported_xss(vcpu->arch.cpuid_entries, vcpu->arch.cpuid_nent); /* * FP+SSE can always be saved/restored via KVM_{G,S}ET_XSAVE, even if diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4597ab3c811c..26edb8fa857a 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3781,10 +3781,12 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than * XSAVES/XRSTORS to save/restore PT MSRs. */ - if (data & ~kvm_caps.supported_xss) + if (data & ~vcpu->arch.guest_supported_xss) return 1; - vcpu->arch.ia32_xss = data; - kvm_update_cpuid_runtime(vcpu); + if (vcpu->arch.ia32_xss != data) { + vcpu->arch.ia32_xss = data; + kvm_update_cpuid_runtime(vcpu); + } break; case MSR_SMI_COUNT: if (!msr_info->host_initiated) -- 2.27.0