Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2686590rwd; Sun, 28 May 2023 22:12:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6M83ZsNvxEOciQAWQI036fNXEZMzJVO5XRwaalzhfPq5/V1EK0PpfY8OCLJ49ILicH728K X-Received: by 2002:a17:902:ec90:b0:1af:c599:6a88 with SMTP id x16-20020a170902ec9000b001afc5996a88mr11754821plg.49.1685337155391; Sun, 28 May 2023 22:12:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685337155; cv=none; d=google.com; s=arc-20160816; b=RL3T8JicoeuWM9DjHFGM4QiWZ9nTy/Cn+aXHVE7d4J6kKPi14ogVDAQTzxGF+i+kfy n5l/vlMzJglHlh/zBZ/f7uk2YUh6iNT3TEjrYALYQBvRbl+c/hZzlTG0wlLLzTqHbyZn 3MNdAW4epBgCgJFgmSOU3L4m118jMZJpfEKF5DwK/M+pPVAjZf57qY98Amr1VCFizpwz JrNWA7rWwGQB/MsC12Hxy3XACFlIHbQ6pdADRDcJ2NA7v02VBz8AaSOB3A2lDmPwNidA I1TcAaReUe5lpQZlTenZeoO9QA0coa9iFJCNt8oly5hoHS+K+IyUxiyTdZQw4K78nQpj TxgA== 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=9QrL/VmLDXe68VRzeH5Fm2IOlXEYXbQPrWlBM3DZTlQ=; b=nTKrhFae7cShidU32OxGAXx2GVHjnj/uyvwtjBCTwNYGfbHDNoimXaUhhHNYBqIQGM 0nRKjcPJxlk6pqFKjt3RPVmww61SHzCLctJpOxv1CvL3NnwDv8ln3o/1Z0Pjc8X29nB1 86pfqP00Ro0kOsgdHaMoCFS8cBdJ+Hei5PvK4BN5s9tGejidU2fwAJt1hMR5PEtUvfqu LtehNMKq1NAF7GMoMFBeOXVPZ0EI7Ru0aAjcEUJGyrswaxjYbFQge3iHSgSEL1E3pspI Zl7sDYnCZTB8+OuzBszO2ScabcxML5tTUt73YXoKjzHnHu13AXp+Ruq4OX8MsWx3jsTp g10Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=dTkD9WZa; 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 b5-20020a170902bd4500b001a691f376e8si3648493plx.415.2023.05.28.22.12.23; Sun, 28 May 2023 22:12:35 -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=dTkD9WZa; 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 S232109AbjE2E2W (ORCPT + 99 others); Mon, 29 May 2023 00:28:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232037AbjE2E1c (ORCPT ); Mon, 29 May 2023 00:27:32 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F31CE10E2; Sun, 28 May 2023 21:24:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685334276; x=1716870276; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7OOy3qSWslSN7/j/GWK3xqmpXSxqB+wdm6CYBi/6m6M=; b=dTkD9WZabMb4b5PlAEH7gqJZMHpzCLOP7pau0lNTZleVixDiF72q5O9w pXVtIRDiwZM4uJeJvU7SJNRZ6k/9oOL7TknIUvNngjDWe7bgx1gelRVx6 KeBTVYq914t0xCd+sYTf9P8EVVcySpAp6I8/paxEj/0WApDxoC1YtRYo+ 3andPzcx0H+pSBvJx67vFQ2J2NrIWCVet73d3T78jKwmrx+Zqymh9ClW4 Wy4xd/baWput24lSHwd3P3OOR1eDJ80sfsmpcwFF0TURCzzw95eFA/CBR sE4TGmT/86PW5I6AxHH0RC2TpHIuIL8G/pQ/9m/GJYduKxHuwRrmVOL2f w==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="334966125" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="334966125" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:24 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="775784404" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="775784404" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:24 -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 , David Matlack , Kai Huang , Zhi Wang , chen.bo@intel.com Subject: [PATCH v14 065/113] KVM: TDX: restore host xsave state when exit from the guest TD Date: Sun, 28 May 2023 21:19:47 -0700 Message-Id: <930e9acebf51ccd7119a4cf0668ee9923d0e3807.1685333728.git.isaku.yamahata@intel.com> 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.6 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 c3ca5726cd83..2e76671dabcc 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" @@ -483,6 +484,22 @@ void tdx_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) */ } +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, @@ -508,6 +525,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