Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp3421438rwb; Fri, 30 Sep 2022 03:26:03 -0700 (PDT) X-Google-Smtp-Source: AMsMyM57sEGGTdB9bKbOf/TQTWaVBeAQ9TsTuF93kVuJ0G55JxALClVRYN+ScqknocVgb+a7FrLo X-Received: by 2002:a17:907:728e:b0:782:8e91:64c8 with SMTP id dt14-20020a170907728e00b007828e9164c8mr6218093ejc.36.1664533563534; Fri, 30 Sep 2022 03:26:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664533563; cv=none; d=google.com; s=arc-20160816; b=Lko2TKbGmipczHKpxXRQqC2H9nBYvsCh0804Al0YHzo1PwRNYnoqnM3F0tY1RR4zpt dhn34XjJvHe41aIgiyiyZ/dI7vw04rxD3hgfrNhPpD6d2qN0NxQOegQgaJrp2Cpbm0ye GFp25uLw6wd1MkTwNouivn8eM4125kNlgzn1+L05IMk2NCObH1QnU2TF3nCz7TI3PWg1 45Nd1DiYruoH3tK8RJalqLU3mrYp8hQiDH47luwpdowV19j2UwFN3JriGSsDzU655bzV uOHVw9OlA/ByNNSpUnBEl5jpOzQja8D36U+s7RMYrzWdFe8MZ+nd/6VXtM0wlKSC9aYt b93g== 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=76mVZOW+idxhswBdn2ae24Rplt/3sLgCE3GWM09QCIA=; b=pZ2keBiRnyFJ1hjA37VjC3ReEiosU/BAxLaarlYQc4G0eVgGwL1ETBSf3jDoa5o+Q3 KIGtZ6UBPHhpo/c9cz297YAbIDScZsBytrQaoEPOXWY+Zc+vyF8GHXj8nPeVagq0bwep mdWpR2+TLzhLmwJKVd/PvhYdqjPHjq1L1Zs28LOWkc4IyekREwsAciljeZOk7Fir3eVH BkVucfo7HO0Q3PWkr0+HaH6Ci+ccLxbzhDGWoXLTCZMb3gyjPPi3amhOZByb95tReYxG UC+CQB/nMAVgjYwheh09UMHY6UhMIZDbvJZgYs20ybPlRHMSUjjBtpS5SwVXaiQOS7Cd cFzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Pxs1WUlg; 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 qb34-20020a1709077ea200b0077cecf89064si1556686ejc.512.2022.09.30.03.25.36; Fri, 30 Sep 2022 03:26:03 -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=Pxs1WUlg; 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 S232382AbiI3KXo (ORCPT + 99 others); Fri, 30 Sep 2022 06:23:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231634AbiI3KTQ (ORCPT ); Fri, 30 Sep 2022 06:19:16 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FDE71B9135; Fri, 30 Sep 2022 03:19:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664533146; x=1696069146; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Lsc7RkBMrBtJ4cW2rT1N+hJ1KRQN4P+cMhYEroo2Rf8=; b=Pxs1WUlg/irsVgFsMx39lRcGfyzcbleb6KC2gPdmSMh+sR1ffIrcWNoP eW01PWxzQqcAzD1g+EOr2rmHGXE5GU59pks6UZAUWHwDnqBmUsmIXV5zr 5e3Znqv4B1uVkO8fbns1io9TNOzPihemTBs/4TtdxGcsJo8nfoJDSeXGx uWbUBfJPkr6nzgIjJOO7aQEvW3kKA5WQw6smGh9AWIt7RMxNDPUA3vr1E qFgbeeoGXNSybM4my3Kj89VjihpIJMhc6MZMQ+Mz2losKRycZTJgq99Ix XLza+QgQYQmusvl+R8jIZX7xHaCwFRF1GxLE2V7V8oNG379Gde/KWTaio A==; X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="285294800" X-IronPort-AV: E=Sophos;i="5.93,358,1654585200"; d="scan'208";a="285294800" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2022 03:19:02 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="726807722" X-IronPort-AV: E=Sophos;i="5.93,358,1654585200"; d="scan'208";a="726807722" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2022 03:19:02 -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 v9 067/105] KVM: TDX: restore host xsave state when exit from the guest TD Date: Fri, 30 Sep 2022 03:18:01 -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=-2.2 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 869cb9952773..9261da8e6236 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" @@ -447,6 +448,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, @@ -470,6 +487,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