Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp553342rwb; Thu, 12 Jan 2023 09:14:45 -0800 (PST) X-Google-Smtp-Source: AMrXdXvwPTWtzyROuJ7L8fTvY15IIGanQmo/N0OmYpShc8AqUHQUo05CkbL7Yx56zga5vE2AnT6K X-Received: by 2002:a17:906:fa96:b0:7c1:5098:9074 with SMTP id lt22-20020a170906fa9600b007c150989074mr268376ejb.0.1673543685231; Thu, 12 Jan 2023 09:14:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673543685; cv=none; d=google.com; s=arc-20160816; b=IIR9LndEmezVi2XgjPz2FFsDxd2cGiHhEMdfioXMkW/Ds7ndisnD6vTV9dWtzP0KpB MPixUuwGHIgs7+p23x1KMaCFehTf/2W2fdND5X/lgS9oVqTNH/tmSnE7yWCdWFcujh9f G+v/zvYXMEpaBH2lJnIcdTJ+sJU56pTiHaftSUoXFeF8s7DQFPMn8hCXFSH/gf4JgHxG jgd5qqTzCUQf2pVE1zu5OXqxBAJtsWnv/Fww52Ie/7PYcbwCEoLyAS0f8mI0otX9RnbF vgtg/Z+FUNPBHzn1dYrtG1uQ0F9D6nbkHWsBnAE8LpTprCSUTXy+x9OIvj/mytwhdgTA wtQg== 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=by5xQ9X0NSdMLGzgArd8Jw48WRthvuTLfwdoJuZ5u00=; b=YnPzhIC8yJdQMzxQ/i+5Ym/SjsitgryCI4NwOQCT6FuP9MDgcSxYhVk5veQmp2fqqm K4pJ2+lX1EKs9yYBTfqbOvFEf8vw41msIeTQiiKkoPgsm+/SaCPl6xuc2k/ogdsvI4Wz PRHmJcrs1uO1vRULbJdGITtnpNLsoYy0JFIwCPahgMJZDFZsplhlhYffPI8ZcETA+e/e rE5WKjyUYjJACLxzU58xCrmZksc6GwFUwjuZEaiPJlaC2RsEz1fy8OERpoym8NGhCb22 Uj4ynf/WmXqHhXg4cOl4JodXR/X7mrON7KJJq3qmmGxtpkZ7x0d6hRCBz61pUbv7q82T z9wA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VAAh6OUH; 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 ji21-20020a170907981500b007c14a639de3si19200311ejc.480.2023.01.12.09.14.32; Thu, 12 Jan 2023 09:14:45 -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=@intel.com header.s=Intel header.b=VAAh6OUH; 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 S241267AbjALQtf (ORCPT + 50 others); Thu, 12 Jan 2023 11:49:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240338AbjALQis (ORCPT ); Thu, 12 Jan 2023 11:38:48 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A42D1E3E8; Thu, 12 Jan 2023 08:34:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673541249; x=1705077249; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PJ5FSwAax6ajntqTh3c6asKm7Anmhm9GqKHRSGtihr4=; b=VAAh6OUHngLRWd+iHKGVeG7g3I6/WCSmn/nXQhrwOTwPusE0alLk0BVp SBOJjv5LTvQ65s4wEkEPzFtsQ54lofBr9/ORCo7YNhAcOwIAENqwByqL9 0VNtYoUwYwFsqLKH7mMkNpRO25qU/KuSFV/V0n9t16IHlcBbcdiM0bGmK Fp/65v7Zxeg83hH6Vf0vXDD1WJOJFP3JQrIP/CO74dIaw2RKXvSv8CVz5 Okwyd7Tt6T81BN7QoGk7nYDRw/xeuEuFoebPQPaqDE5gCkZrPeDxBT3FE hn0oMQXaXcYfH4NK3USIfh6cTyI33/uiIJfSr5XtzLc3YET5BTf6dk9+q A==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="323811986" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="323811986" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:31 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="721151870" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="721151870" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:31 -0800 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack Subject: [PATCH v11 067/113] KVM: TDX: restore host xsave state when exit from the guest TD Date: Thu, 12 Jan 2023 08:32:15 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE 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 From: Isaku Yamahata On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 854aa4af4937..7bd47a76c96c 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -2,6 +2,7 @@ #include #include +#include #include #include "capabilities.h" @@ -460,6 +461,22 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) vcpu->kvm->vm_bugged = true; } +static void tdx_restore_host_xsave_state(struct kvm_vcpu *vcpu) +{ + struct kvm_tdx *kvm_tdx = to_kvm_tdx(vcpu->kvm); + + if (static_cpu_has(X86_FEATURE_XSAVE) && + host_xcr0 != (kvm_tdx->xfam & kvm_caps.supported_xcr0)) + xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0); + if (static_cpu_has(X86_FEATURE_XSAVES) && + /* PT can be exposed to TD guest regardless of KVM's XSS support */ + host_xss != (kvm_tdx->xfam & (kvm_caps.supported_xss | XFEATURE_MASK_PT))) + wrmsrl(MSR_IA32_XSS, host_xss); + if (static_cpu_has(X86_FEATURE_PKU) && + (kvm_tdx->xfam & XFEATURE_MASK_PKRU)) + write_pkru(vcpu->arch.host_pkru); +} + u64 __tdx_vcpu_run(hpa_t tdvpr, void *regs, u32 regs_mask); static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu, @@ -483,6 +500,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) tdx_vcpu_enter_exit(vcpu, tdx); + tdx_restore_host_xsave_state(vcpu); tdx->host_state_need_restore = true; vcpu->arch.regs_avail &= ~VMX_REGS_LAZY_LOAD_SET; -- 2.25.1