Received: by 2002:a05:6358:e9c4:b0:b2:91dc:71ab with SMTP id hc4csp4119602rwb; Sun, 7 Aug 2022 15:36:30 -0700 (PDT) X-Google-Smtp-Source: AA6agR56K0vvMOWfKSk70dGWsZPxihPqJCPdkClJaB5RGO3jOLu96o0WEgpWeQByT3dl7zK5N3k5 X-Received: by 2002:a17:902:7ec2:b0:16f:9432:b120 with SMTP id p2-20020a1709027ec200b0016f9432b120mr11365845plb.5.1659911789824; Sun, 07 Aug 2022 15:36:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659911789; cv=none; d=google.com; s=arc-20160816; b=DrB6tD9VHHak6JKVB6bVcG3QFtXxhMJfyZBHeBu26JBgAK8qjN49hKCx03jrR3K9QC PbU44hbXr6wpScsEscWW/ASjXYRGst619LsymWNV8Ay0Lhga/z6/MqGFenIIdbnWnLzB BNuqGctr3L43q3pwHQhu6Ce3xO1KKY5YwdnxKJ5H2XT5Vn1cB6UlFTUY36UMG86ncQA9 doVBoWECNMChc/+zHLskhfJDePTdDtvAYTjOorwb+2E3VUvMo2BSdcwmP3QgQtEZtH2+ wcVCC9HKZ9VL/EwBF1rY+aicApm86hgO8+HA013sy1XfWxBxYyxJi6JHr5z2J9i5m3Yz CpIg== 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=DH63v1mEe6QPbBBYE5YN3NVVax0yL++jAmN5r6zjsZo=; b=ZLDodoww9iFuRDzmee1Lqo3f2ufrLVuWYQ0SLX8yhMVyscqHn+vsYZGa+OWllZNvjo pKdfGbjiYbUrPZxDKV0pztz2UcpbXGGIUf5P/KGqOudx2a7bhg/oT4xvG3OaM/tK0YXP t0Xa+6/Cc62Nl8oNEFzuMSEE2yD66gEGvsJV4wg9dBKJYSaWSfQPJrbJaSqHwfuvzY/E uS00sWiH+2D5VgQ8oqtOn03SCQraz+zZwLBM/SxToG4+VHRvKfFDbfT7uoqTq5vdHKdj hnH+rnmMYmCcX6GUfwYVndPtFac8q8ke71e63wlZZ1WbVbyXzYaq7u7cGcIDydQn+8Lu CjZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=aL7xK+nO; 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 k63-20020a638442000000b0041d7ab4e620si1627616pgd.866.2022.08.07.15.36.16; Sun, 07 Aug 2022 15:36:29 -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=aL7xK+nO; 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 S242215AbiHGWKQ (ORCPT + 99 others); Sun, 7 Aug 2022 18:10:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237096AbiHGWFN (ORCPT ); Sun, 7 Aug 2022 18:05:13 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75BD2B7E5; Sun, 7 Aug 2022 15:03:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1659909783; x=1691445783; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=6A6aJOCU7GH1tjBE8WcWgFQ4nrzXq4R0KwyJQAB37bs=; b=aL7xK+nOZpsiLnb/3FxmbwFqo4z/xYYPguiNPvORRctkAHA7aONaI9DC m9W1vTJviCQze8UMDiaoSg6NGtmXr+t64Nc3PSknkJ2juXVPOdz3NH+u/ VHV/ylbqxxQqy1eE9x3dr8bXEZwQJ2Jt5gBlVTPaynEt4xbyfz2j8JQGY 3pPkd5Lm4aUa2u/Yt5V6DwYJITBlKkIWqs9DkJWrDXiuMMBXTQ7rIV/qK VWgiNmMUq+Hwkf12zOp69hZd8sMj0/wqoujpECWmpw/15tMlELsyOKlWH OgQRFLrLrUCJV4R+w/jKVAo/y1/wQPO7ii+ZH/CptI+7ETSMzQIlTww4w Q==; X-IronPort-AV: E=McAfee;i="6400,9594,10432"; a="291695708" X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="291695708" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Aug 2022 15:02:39 -0700 X-IronPort-AV: E=Sophos;i="5.93,220,1654585200"; d="scan'208";a="663682663" 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; 07 Aug 2022 15:02:39 -0700 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 Subject: [PATCH v8 066/103] KVM: TDX: restore host xsave state when exit from the guest TD Date: Sun, 7 Aug 2022 15:01:51 -0700 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=-5.0 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,T_SCC_BODY_TEXT_LINE 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 482e85337997..0cf9ef4a1d2a 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" @@ -579,6 +580,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, @@ -602,6 +619,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