Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp405014ybt; Wed, 1 Jul 2020 01:07:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzcTCqzD06ihlsoFukOCQn4Iu8teQizXH5+bSwBfqP6A6BQPeGKoi6UVkcFALrEa+wWsHAx X-Received: by 2002:a50:88e1:: with SMTP id d88mr27649848edd.74.1593590854930; Wed, 01 Jul 2020 01:07:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593590854; cv=none; d=google.com; s=arc-20160816; b=JhK8MWD3gsEnSElbNZbkJaoHL0/nl45phg4SvMdxwfD8c+S/v9ctoSdtE53viqL4f3 N5NLEO/VUTrUi8bLtf1eo6uMY77hPLtj6vsEJiqdjaVrlD0zgkjtZle5YFgy7eTzgLfS HzEFajqoRlOW0NTl4hEUUvk09uloPybgigqGk9CIjHTc45YO6otda6nuSA3LDRkwHJwv LXJKxCNocfpwdccFkM2ghvl6k4gb7yixbXx+XNvsYwYyclvvfM0fqiZIbpGydJpbLLGl UqhDcMfmhTBxV7+KK8xBte5cB10s9XDMimbHgMZ5YwLjXyL5+3LvJ45FC3AwRsrTGQIX OkmA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:ironport-sdr:ironport-sdr; bh=BU8tpJoFLpQVbZT8X4jCEtX7QLUm7aCy46GqXNYtD+E=; b=ifSLp8WhdzIPozTRK8PBKe/+oMmYu0m2pLUmuT8vqJ262suivsvaFqzRIcYONeZzcp B6OhYsgzZBVWv1+G7jL0+ZFtOvpDVnIlNIKpPDkghoJsWo0VEk7I90YNP1a0jRIHFIgz Xn6XHMjM+hXutnsKj8fbOKHJRIw9eaeb6ma7dDIJ25bbt2tlmmmvDkPm3RnSfa8VBpwn rgyt277AD1/dbSjqsgSOuqfQLyzsitJnkzhiefdsjfFW3UjXrBaFQ3GW66ILGlyGpZay 5NG1sn4rQHxvwIUCfetXAPkfNhD0adiHo3hs4Z21FJXBD8Xdm3WSvIlvwR5cfhH2PBeW ob8w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w11si3365545ede.247.2020.07.01.01.07.12; Wed, 01 Jul 2020 01:07:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728575AbgGAIE2 (ORCPT + 99 others); Wed, 1 Jul 2020 04:04:28 -0400 Received: from mga14.intel.com ([192.55.52.115]:2419 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728258AbgGAIE0 (ORCPT ); Wed, 1 Jul 2020 04:04:26 -0400 IronPort-SDR: yp0F7uIO61AbnUpHIyRxZawPdnfA8s2sU0Zl9mmHd/pd+vtEsCy/Ou/och4ufEhQKA6/Q0n1eL UFhMGFfzRvUg== X-IronPort-AV: E=McAfee;i="6000,8403,9668"; a="145581891" X-IronPort-AV: E=Sophos;i="5.75,299,1589266800"; d="scan'208";a="145581891" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Jul 2020 01:04:23 -0700 IronPort-SDR: pj8xvA+ZoUi2T0+JcFPPirZCBI887UqkrWc9L95otJRwGJ2sUUne1syM9zY+r6ZUtR07XGkNq5 dGZIBuwAEdyA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,299,1589266800"; d="scan'208";a="455010276" Received: from unknown (HELO local-michael-cet-test.sh.intel.com) ([10.239.159.128]) by orsmga005.jf.intel.com with ESMTP; 01 Jul 2020 01:04:21 -0700 From: Yang Weijiang To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, pbonzini@redhat.com, sean.j.christopherson@intel.com, jmattson@google.com Cc: yu.c.zhang@linux.intel.com, Yang Weijiang Subject: [PATCH v13 05/11] KVM: x86: Refresh CPUID once guest changes XSS bits Date: Wed, 1 Jul 2020 16:04:05 +0800 Message-Id: <20200701080411.5802-6-weijiang.yang@intel.com> X-Mailer: git-send-email 2.17.2 In-Reply-To: <20200701080411.5802-1-weijiang.yang@intel.com> References: <20200701080411.5802-1-weijiang.yang@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org CPUID(0xd, 1) reports the current required storage size of XCR0 | XSS, when guest updates the XSS, it's necessary to update the CPUID leaf, otherwise guest will fetch old state size, and results to some WARN traces during guest running. supported_xss is initialized to host_xss & KVM_SUPPORTED_XSS to indicate current MSR_IA32_XSS bits supported in KVM, but actual XSS bits seen in guest depends on the setting of CPUID(0xd,1).{ECX, EDX} for guest. 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 | 23 +++++++++++++++++++---- arch/x86/kvm/x86.c | 12 ++++++++---- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 42a2d0d3984a..f68c825e94ad 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -649,6 +649,7 @@ struct kvm_vcpu_arch { u64 xcr0; u64 guest_supported_xcr0; + u64 guest_supported_xss; u32 guest_xstate_size; struct kvm_pio_request pio; diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 901cd1fdecd9..984ab2b395b3 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -89,15 +89,30 @@ int kvm_update_cpuid(struct kvm_vcpu *vcpu) vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET; } else { vcpu->arch.guest_supported_xcr0 = - (best->eax | ((u64)best->edx << 32)) & supported_xcr0; + (((u64)best->edx << 32) | best->eax) & supported_xcr0; vcpu->arch.guest_xstate_size = best->ebx = xstate_required_size(vcpu->arch.xcr0, false); } best = kvm_find_cpuid_entry(vcpu, 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); + if (best) { + if (cpuid_entry_has(best, X86_FEATURE_XSAVES) || + cpuid_entry_has(best, X86_FEATURE_XSAVEC)) { + u64 xstate = vcpu->arch.xcr0 | vcpu->arch.ia32_xss; + + best->ebx = xstate_required_size(xstate, true); + } + + if (!cpuid_entry_has(best, X86_FEATURE_XSAVES)) { + best->ecx = 0; + best->edx = 0; + } + vcpu->arch.guest_supported_xss = + (((u64)best->edx << 32) | best->ecx) & supported_xss; + + } else { + vcpu->arch.guest_supported_xss = 0; + } /* * The existing code assumes virtual address is 48-bit or 57-bit in the diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index b63727318da1..c866087ed0ef 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2843,9 +2843,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 & ~supported_xss) + if (data & ~vcpu->arch.guest_supported_xss) return 1; - vcpu->arch.ia32_xss = data; + if (vcpu->arch.ia32_xss != data) { + vcpu->arch.ia32_xss = data; + kvm_update_cpuid(vcpu); + } break; case MSR_SMI_COUNT: if (!msr_info->host_initiated) @@ -9678,8 +9681,9 @@ int kvm_arch_hardware_setup(void *opaque) memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); - if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES)) - supported_xss = 0; + supported_xss = 0; + if (kvm_cpu_cap_has(X86_FEATURE_XSAVES)) + supported_xss = host_xss & KVM_SUPPORTED_XSS; cr4_reserved_bits = kvm_host_cr4_reserved_bits(&boot_cpu_data); -- 2.17.2