Received: by 2002:a05:6358:795:b0:dc:4c66:fc3e with SMTP id n21csp1441256rwj; Sun, 30 Oct 2022 00:23:37 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4xSvlHmbTL5nlGVKlrZyiBA9GxhTMZiHMFkvCnFHQXbZ11VlWM//sYIAdL9ka+oE6qUrwp X-Received: by 2002:a17:902:d0d3:b0:186:9869:adb5 with SMTP id n19-20020a170902d0d300b001869869adb5mr8325276pln.105.1667114617442; Sun, 30 Oct 2022 00:23:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667114617; cv=none; d=google.com; s=arc-20160816; b=xUG28LtHi2GF0OigMyrG/Q2eQUB7hmymuvYubFBq0rR7HYS472IUGHySCrylhPH2gK XO/KuplfruEzVpSBM2kjpR8ziEWFsKjLBO1zblxi02XGS2KPkw85KzL5OukbFUO6Ah8b VEcziXvuuko4EJPiTB92pEFZDp6pmJQ5/+AQtysJq6E5QLyZCCzAe8hklFObrawcXxb7 SHl0a1a7DqOnOty39yGj0PCtPQHlmNPC2wUYewx6UpDoUjwS/eya09LWZ3rr1DPSQ9mh 4IAaYF7h+PFskLwJbOuPihmsLzRn8OhDypxp2KOfQLr95c5yTisVSmQdVZpYbOsM9NnO Vw2Q== 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=JWBUh5oXNLLh7qVEAd1BGHbo9ryiEdqrFJQPRUBbazw=; b=UeE4q2221twTSD6Lwil9YFg4aVNy3X8Y5qdJ8Yn1/F5ZFWe1xYVIQAtXvLk8LW70hu ZYxL4gZ2PQctma5Wk0SB2xb8Z4ISKUM0NUy7z3MdEnlmBdTi+TVGYsS8dGtopNi56q3u xX17cO8vNMXb2rsCSXj1IUnQlKC4XZ/wEchLk0JjfjM6DpETGXPr1t0MbnBp7+ZFG7Z0 2KuyHzhAueywRvaj75n/IL42eKU9VmeLKY2UfZWG/JQ+uKzZiAJv5kX1Vlo1AgxwvGGQ wife7Ggb/ofAkZ5UWHv5jKK975HDDGjqqDmWQ31/J4cd+zmAs2mM4lhslGJEUDaGJGog /tRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=I2RVeNVb; 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 m18-20020a170902f21200b00186aace22ccsi3683759plc.288.2022.10.30.00.23.26; Sun, 30 Oct 2022 00:23:37 -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=I2RVeNVb; 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 S231270AbiJ3G34 (ORCPT + 99 others); Sun, 30 Oct 2022 02:29:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230267AbiJ3G1t (ORCPT ); Sun, 30 Oct 2022 02:27:49 -0400 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 02D222B9; Sat, 29 Oct 2022 23:24:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1667111074; x=1698647074; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=64+6ECToZEcCn4qfUeaWI0Qwqi2QNI8ybbnZnGdMBJA=; b=I2RVeNVbVktTNv31kx2GzhY5JdQw3VMnJclmDV/OzIUf6PZn4xf+M2Bu zI2T1+gG4YwbHss65EZQKdivKmrlWb4CBlqOBEjy3k9czFw/efT5VLmtR oaSa+l2VdwENoavmjZplerT8oty3u8HslHJMMJnkthcKruQEkMP9z9g/m jPmqh5caI2do34qqytBC1BNRU0M5VSCDfn1r+njlI4Rmut6cX720c5DXb /bz2BPREP53gIOOvMs9LwYYHOSTh5iNQMTHRiuA+G0r9VrljyNB8h+zWA lG26QAAkbGjsdmaePYRNnf7ckzWip3otsdiZ/h511bSssy+fyxkui8Vtp Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10515"; a="395037183" X-IronPort-AV: E=Sophos;i="5.95,225,1661842800"; d="scan'208";a="395037183" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Oct 2022 23:24:09 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10515"; a="878393070" X-IronPort-AV: E=Sophos;i="5.95,225,1661842800"; d="scan'208";a="878393070" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga006-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Oct 2022 23:24:09 -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 Subject: [PATCH v10 070/108] KVM: TDX: restore host xsave state when exit from the guest TD Date: Sat, 29 Oct 2022 23:23:11 -0700 Message-Id: <1be74c1754ef1401d5413270112651faefa8b41b.1667110240.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.9 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 021040fdd630..3ec465cbaeef 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" @@ -455,6 +456,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, @@ -478,6 +495,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