Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp3402852iog; Mon, 27 Jun 2022 15:34:49 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uaZ1to+HyKY76F1BmvPWFK1i0sFY53ABsbX65Xk4O8MtXPk1vmsfluxN7c9zCgQ8UcNEpk X-Received: by 2002:a05:6402:d6b:b0:435:7170:cf15 with SMTP id ec43-20020a0564020d6b00b004357170cf15mr19318270edb.314.1656369289230; Mon, 27 Jun 2022 15:34:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656369289; cv=none; d=google.com; s=arc-20160816; b=fACAFn+Xt9cNh7uY/mXIN/I/9Nt1bk7gGna46oUfeyaDJgd9Rw3lkhYUtCfwCn6z2r c69FrKaz67aH9S3SCT0OIChY8BjyBJBmjjQvYqlAP7mcTSyA8yHs/CAJ+Fik0qboCgjR 9rhegwV1YbfpfHGE6JPJd32FLwXDaOuso142vuocxzXztnzzVPEKyO29S6jkdhJnQa7v zulcakR8nJrLBCSP0bluPbPoiI08a/eNy6XF0NUtxT22Ybuplo/qufj77vuGqqPekUxk evPOpX8cxq8hn9Zu7u+ZfG2wbKYL1djW34jMjRXw5r46QdL2tFiKsr8epRxE3wGF4T4l 557g== 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=PrXpxUeOinGcGkgjPN2areqwCW+7Xwl5BZ4pSV6bVg8=; b=CglF+zSwaFf/A8CKESHG2v/ztnY6ktQEJmDrbDLEBdwv7/9hO468HdfNeayTdk+0gP HiFUqVfI1t3AsqDkrWS0N7Wt8TYvi+jQlaMHpww4pZyrIOzYszjGTsSqrRmSMHvkwdji h/3+a0z1X79wk8HNvifU1TLXu5XWVXImKpEOmV0/njwkqfgnyrzYJAfcpj44bYMrr8qp SWGuuAOvAeoE0ADS4NyONnrf3EyJF1X95IDhV/NaNzxZrdL4gUX8wFmZeIOBhTMneJj6 n68Fzx7ss8Tk+ML/GxFgo6U7nvlNRaYDoKu16qOtx5HuTpbYXknV4WFuAS+HNO05rivt L6fQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=BBCeFTtf; 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 vp13-20020a17090712cd00b00722ed7f8c9csi12347920ejb.814.2022.06.27.15.34.23; Mon, 27 Jun 2022 15:34:49 -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=BBCeFTtf; 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 S242175AbiF0V7F (ORCPT + 99 others); Mon, 27 Jun 2022 17:59:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241502AbiF0Vz3 (ORCPT ); Mon, 27 Jun 2022 17:55:29 -0400 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A81465EF; Mon, 27 Jun 2022 14:55:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1656366900; x=1687902900; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=G7EXlDzr2ljfAEeiNwOeqlNNdVlNm0vOm9CJJETpzpY=; b=BBCeFTtfSWQTpzYl8gF1XVzQGSsqpyYhUMAJXAc5s9kvUocFroGOCcEl 0By+uEEdQstnn0E88SbOVkJfG2aAcE0lR67VsWTtj6A1CMGs/xnrc7rrI sPYrlcpNJcdXNIPQVjm/YVcGuT3LBaema8MBVuYFHJOd2SDqkmkTXTGAb ZUM+R6umdbI6bmeLEQRShrQAHdeFYtasxpxpLlMnXUq9WxFnxvlm8kM8o hpILlraDoct+x9UZjd23VVlIV7iPK1sjz22BXdj8puHWSEHcYIoucMJgC Xc41Kv+GIXpnPEw2itIugohtXxfI6Iggkjgaambc8sCz1zUebtFIxAnb/ g==; X-IronPort-AV: E=McAfee;i="6400,9594,10391"; a="279116106" X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="279116106" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2022 14:54:57 -0700 X-IronPort-AV: E=Sophos;i="5.92,227,1650956400"; d="scan'208";a="657863650" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Jun 2022 14:54:57 -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 Subject: [PATCH v7 066/102] KVM: TDX: restore host xsave state when exit from the guest TD Date: Mon, 27 Jun 2022 14:53:58 -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=-7.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 d9e0dd30c150..277525b6ca51 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" @@ -592,6 +593,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, @@ -615,6 +632,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