Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp18690rdb; Thu, 21 Dec 2023 01:14:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IHzjctUbeGsvO4AwIFaY1m7l4ror1CXskSybzDaYo9RsbMaYu0kOUulO4BCh8ioUlhFWCOj X-Received: by 2002:a17:902:e888:b0:1d3:dc48:838 with SMTP id w8-20020a170902e88800b001d3dc480838mr3042615plg.20.1703150069724; Thu, 21 Dec 2023 01:14:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703150069; cv=none; d=google.com; s=arc-20160816; b=mSSAnKF1pqLKRcbNGZfW4J7uV1wUn+4p2RKcKeInD4HakHuFpI/D1Uv8Po00ZA6n7O 6QTj0GgzvY4nXnsj32sTd2ZHVvxqHl8Ii0Dsrq9/aUip3Qx/YwDNJEwOU68thYMeU9Dn djIg6PSdmpTmqHywhsNMBmN9p80rMivmVHLjRvMRvH01/MJRl8bEphJz90zbsSq6uG7t jnE4+mgoueL879zd9BLUtTDVGxYh6q7vNIZnpNFR7BBMnBi7APyPyN3U5RYOZki21ARq 1o+MAUFbzpozCG1A3ppSTD1+W2rLW+oCayeU9RVqNJPzOxjXmvb904nOThsIKSWdFkQa S4Nw== ARC-Message-Signature: i=1; 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=GH5waOuli6h9Set3ggYc/VU0NXbCWcgfIqnPtXKiONQ=; b=TVB5cnfAPI1LOtqes2S2ZL9fb270GxvUvcecVv/nhWFCVzRgFex9kFgExJSKTC4IFt LV+XusWSdC1Tuzm5qeHBEJ6KiATGUJclvsqycShOYsWaWToxHkEtydWChjB+q8k/6/6g oqvHnYImS54feONGqMPEVbYkbw05H9CnffOJxbQGti1BRtdZcD/JkEVxFXYp+XFzASyx /2QZawiyFmI0V38ITFtMRnGClvwJQaNpaNCR7uMMJepWw3OpjatSZ75uToU4AuzD8KBA LgZSO4V/DL2mavfYUI5nbQSELJXVE7AynegXpTR/bXbZn83zZxx4zmbCA6xhuJS2uTKu KOcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nVoBPIdy; spf=pass (google.com: domain of linux-kernel+bounces-8068-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8068-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id u1-20020a170902b28100b001d34cb55b86si1140661plr.549.2023.12.21.01.14.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Dec 2023 01:14:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-8068-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nVoBPIdy; spf=pass (google.com: domain of linux-kernel+bounces-8068-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-8068-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 5E11E281D76 for ; Thu, 21 Dec 2023 09:14:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7049554F9F; Thu, 21 Dec 2023 09:03:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nVoBPIdy" X-Original-To: linux-kernel@vger.kernel.org Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.24]) (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 03F204D5B6; Thu, 21 Dec 2023 09:03:49 +0000 (UTC) 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=1703149430; x=1734685430; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Lh/HRtmyTmg9PjjoKlPhg0GrQura9c97fLsX2mTq5DU=; b=nVoBPIdy7oFzTQEUjtPrOPugDUe/AxzDYGphjsLftP2YKQljss/57AC2 Ub0VxFANnObppsqfGvyTj7rXoPP3tR1Z/jAJgCTBQTc1OmRnImhb+z0xJ kGPqL9H+3zK07rxC0iu8aibX/NHrN7X5I5UdYjmNLcYX6o2G54xifWba2 Ll5SHYnxQpbisQYrQEIFlT6YJJJSdHESjPei0LVl2ire5vNnibSnyrIkL fFst536pTYybhQr6qJo8PigG3vKwA3OpDTRfIegvHaExKQcumiV/pa9Jw t2qSm4LjYj0rStaHbkkNtnzPmXTunodQ78QJxsJdfsVYSIi4msBVgIsPF Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10930"; a="398729643" X-IronPort-AV: E=Sophos;i="6.04,293,1695711600"; d="scan'208";a="398729643" Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Dec 2023 01:03:44 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10930"; a="900028600" X-IronPort-AV: E=Sophos;i="6.04,293,1695711600"; d="scan'208";a="900028600" Received: from 984fee00a5ca.jf.intel.com (HELO embargo.jf.intel.com) ([10.165.9.183]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Dec 2023 01:03:11 -0800 From: Yang Weijiang To: seanjc@google.com, pbonzini@redhat.com, dave.hansen@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org 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 v8 13/26] KVM: x86: Refresh CPUID on write to guest MSR_IA32_XSS Date: Thu, 21 Dec 2023 09:02:26 -0500 Message-Id: <20231221140239.4349-14-weijiang.yang@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20231221140239.4349-1-weijiang.yang@intel.com> References: <20231221140239.4349-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