Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp863756rdb; Tue, 23 Jan 2024 18:48:37 -0800 (PST) X-Google-Smtp-Source: AGHT+IE5j430WnKaNZ75OJHA4HVVDa/TOByuQY3OuCHjm4dUnIgkkycXVNy/C2SvQHkGmmT58zl4 X-Received: by 2002:a05:6870:1648:b0:210:f0c1:3789 with SMTP id c8-20020a056870164800b00210f0c13789mr2623612oae.6.1706064517656; Tue, 23 Jan 2024 18:48:37 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706064517; cv=pass; d=google.com; s=arc-20160816; b=FjKpRykPs+ppKK96vtAfOczNs8q3+F1PMsvjRk8SLwaVQi+Q4TCBaulGIdr6ZXCr5D 2GIP96N+H2Vlp/DqxrPdCfYrRdl/pKfYfK3sXS0JxR5OAom1lzV9Yslu6MLJP8xrydS4 Sjv1mK5ycBmjzZGGjv8r3mT+5ySCiNT9PESsvtaMxGR807+WXVbiCfKkimLfbV/ugBNV oJx1sHPe/N3qhl4dpL6AtveVSJiLN+1uSQkU8mb9TIODphB3NsVDx0yavxcKeO/GsDzG QUOxrKmCKNse+4+fSN9O8ktdZH1nBiXAjGjmbY75J/MDO8BNybwZZ2n/tO1NdJkVQq9D cCmw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=IUd891AT8iP1S6A7F/3xY4ZO2KRfYqDtuEquwIRa9u8=; fh=P3RdGGFvb5N2uSrXSkzTbks6QAbSjiL8sphx8dC5Dno=; b=q0YjnLTnIn1nOeqp1rccvMcUwGRLwf0wNcyUanVRk2zVPzzHFl7TAC4+AVudOfXkHy kkwoqSI0GWrt0hVWYvMC2orlygFKKNGw7S1VbNme4GYA764lNlGofE+6CmZbv/+YbMY8 165aSgesPgrUPQz2A3tmJE0MNr3ahfQ7GptKgm3HfytgImp6Zc3rhudbzZnANStYwoP0 8Sal/QzvXRIUVI3wvKEgOiNCengJ0M1Sb0Eiq6gKBrnf3PCjIwNGmxUuzduniRVsEFSx x2Id9C9yRGQDRK4ReatR9697IxM+6DEwhlx3uDCqthhyQFATsqam54DYLYBxaeCj44zq Zcfg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AqnQCJNY; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-36334-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36334-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id r13-20020a63d90d000000b005cf8e57c831si10848922pgg.187.2024.01.23.18.48.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 18:48:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-36334-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=AqnQCJNY; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-36334-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-36334-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7DAD9284C6C for ; Wed, 24 Jan 2024 02:47:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 59AAE17C7B; Wed, 24 Jan 2024 02:42:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="AqnQCJNY" Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 89AE517596; Wed, 24 Jan 2024 02:42:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.55.52.120 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706064163; cv=none; b=R7ym0eeJz+q/sVsknf6sW8klrvIrz6KI5/PFrUcYPvST7tC9OsllLdDZQitkjJAuRmAuPI5XL1fJjN+mV4QSfErNRh8dStF7qZAc1tg4prDZyIdwjNNHoDJBvVfGAncXfdY44l/153AVzPTYbgIzCEUTMZ16GMVdiqIOnMBZTrA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706064163; c=relaxed/simple; bh=Lh/HRtmyTmg9PjjoKlPhg0GrQura9c97fLsX2mTq5DU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EZgdM9AIEwsUL/QOYkc62kdWvSWOGoe7PFT52CzXcA7OhPoXPLGs3JU/Gx03MaR+tadBSfHWqagTe7G8lZASNxprSxOKsH6NDr+6HisRR3TlEAQtt8yElJUWsbHqTwfL+Xz4Ul8mhnygCae2AIJyMYBrfz0yCDVANU0ro5qrNug= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=AqnQCJNY; arc=none smtp.client-ip=192.55.52.120 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1706064161; x=1737600161; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Lh/HRtmyTmg9PjjoKlPhg0GrQura9c97fLsX2mTq5DU=; b=AqnQCJNYg6C/IVPfeJxmbeu7jttdbR2LtRpu+PoQ23DVqw1Rsc5MrEQO +Uwlf7SPM2BsgSNrKDsJPnfCSrYQwqraNFCr6x0QGmTwlfnSKaWgNlOm7 Xhl1bD4Bu69fl+zPet0Bon89n3lxuAEuHcri2ErSxKBpJSZD9utt0jNPU vhmU0tu198rLwG5VvdisE1+A1EcL7XDB3yHleV594GoWqsLUcoizIhJkL k2Lj23qGah4mboYLllMdh1PlVcd1bejWeyekJ5Mz17GYBduMsoHBAr7PZ v85hyDJr1ddnqw38FxypTVi7hxdfzZYuPdknEBh8t7s+acpGGTiGN2UD+ g==; X-IronPort-AV: E=McAfee;i="6600,9927,10962"; a="400586504" X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="400586504" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 18:42:37 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,215,1701158400"; d="scan'208";a="1825869" Received: from 984fee00a5ca.jf.intel.com ([10.165.9.183]) by fmviesa003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jan 2024 18:42:37 -0800 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, x86@kernel.org, yuan.yao@linux.intel.com Cc: peterz@infradead.org, chao.gao@intel.com, rick.p.edgecombe@intel.com, mlevitsk@redhat.com, john.allen@amd.com, weijiang.yang@intel.com, Zhang Yi Z Subject: [PATCH v9 13/27] KVM: x86: Refresh CPUID on write to guest MSR_IA32_XSS Date: Tue, 23 Jan 2024 18:41:46 -0800 Message-Id: <20240124024200.102792-14-weijiang.yang@intel.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240124024200.102792-1-weijiang.yang@intel.com> References: <20240124024200.102792-1-weijiang.yang@intel.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Update CPUID.(EAX=0DH,ECX=1).EBX to reflect current required xstate size due to XSS MSR modification. CPUID(EAX=0DH,ECX=1).EBX reports the required storage size of all enabled xstate features in (XCR0 | IA32_XSS). The CPUID value can be used by guest before allocate sufficient xsave buffer. Note, KVM does not yet support any XSS based features, i.e. supported_xss is guaranteed to be zero at this time. Opportunistically modify XSS write access logic as: If XSAVES is not enabled in the guest CPUID, forbid setting IA32_XSS msr to anything but 0, even if the write is host initiated. Suggested-by: Sean Christopherson Co-developed-by: Zhang Yi Z Signed-off-by: Zhang Yi Z Signed-off-by: Yang Weijiang Reviewed-by: Maxim Levitsky --- arch/x86/include/asm/kvm_host.h | 3 ++- arch/x86/kvm/cpuid.c | 15 ++++++++++++++- arch/x86/kvm/x86.c | 16 ++++++++++++---- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 40dd796ea085..6efaaaa15945 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -772,7 +772,6 @@ struct kvm_vcpu_arch { bool at_instruction_boundary; bool tpr_access_reporting; bool xfd_no_write_intercept; - u64 ia32_xss; u64 microcode_version; u64 arch_capabilities; u64 perf_capabilities; @@ -828,6 +827,8 @@ struct kvm_vcpu_arch { u64 xcr0; u64 guest_supported_xcr0; + u64 guest_supported_xss; + u64 ia32_xss; struct kvm_pio_request pio; void *pio_data; diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index acc360c76318..3ab133530573 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -275,7 +275,8 @@ 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); + best->ebx = xstate_required_size(vcpu->arch.xcr0 | + vcpu->arch.ia32_xss, true); best = __kvm_find_kvm_cpuid_features(vcpu, entries, nent); if (kvm_hlt_in_guest(vcpu->kvm) && best && @@ -312,6 +313,17 @@ static u64 vcpu_get_supported_xcr0(struct kvm_vcpu *vcpu) return (best->eax | ((u64)best->edx << 32)) & kvm_caps.supported_xcr0; } +static u64 vcpu_get_supported_xss(struct kvm_vcpu *vcpu) +{ + struct kvm_cpuid_entry2 *best; + + best = kvm_find_cpuid_entry_index(vcpu, 0xd, 1); + if (!best) + return 0; + + return (best->ecx | ((u64)best->edx << 32)) & kvm_caps.supported_xss; +} + static bool kvm_cpuid_has_hyperv(struct kvm_cpuid_entry2 *entries, int nent) { #ifdef CONFIG_KVM_HYPERV @@ -362,6 +374,7 @@ static void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) } vcpu->arch.guest_supported_xcr0 = vcpu_get_supported_xcr0(vcpu); + vcpu->arch.guest_supported_xss = vcpu_get_supported_xss(vcpu); kvm_update_pv_runtime(vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b3a39886e418..7b7a15aab3aa 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -3924,20 +3924,28 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) vcpu->arch.ia32_tsc_adjust_msr += adj; } break; - case MSR_IA32_XSS: - if (!msr_info->host_initiated && - !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) + case MSR_IA32_XSS: { + /* + * If KVM reported support of XSS MSR, even guest CPUID doesn't + * support XSAVES, still allow userspace to set default value(0) + * to this MSR. + */ + if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVES) && + !(msr_info->host_initiated && data == 0)) return 1; /* * KVM supports exposing PT to the guest, but does not support * 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; + if (vcpu->arch.ia32_xss == data) + break; vcpu->arch.ia32_xss = data; kvm_update_cpuid_runtime(vcpu); break; + } case MSR_SMI_COUNT: if (!msr_info->host_initiated) return 1; -- 2.39.3