Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp265780rwb; Tue, 25 Jul 2023 15:41:52 -0700 (PDT) X-Google-Smtp-Source: APBJJlFk+1HzSQ8hsz9h62hEpVDoBzBuT0BUpRllIC4aq4K310gcaN1RA1Og5UN4ClI5UOOgRPgG X-Received: by 2002:aa7:88c8:0:b0:682:4c9f:aa1 with SMTP id k8-20020aa788c8000000b006824c9f0aa1mr629987pff.5.1690324911869; Tue, 25 Jul 2023 15:41:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690324911; cv=none; d=google.com; s=arc-20160816; b=sTBW2LOBwKjU0YgRHLXsqRI1PRVxyyEjMrR4cCdSTYU0BmsKLZgi7df516sIkKaZuu D+c4OgnHXcT0OUwCb9yMZb2Nv50PUoJgNWO0KFu3tThFa8I6QYcrrOrOQdb1U5YDvjQ6 oVCWVob0VJ74PQS5RL5PEKWqj5LcMrNvK87HwKv5peqFxwmnvg7wvDIkGemiA7jQWR9L /wcncUbYbdBBrii3OEjzr3D85XpHXwHZ4Sf/UQ9mBxRd+40MLnftE1aAHNhcI35YtwWW Z5jlBMOPHErNRr9VyrV4YwnUIl5+XSYmqUll7HewDQEWR0/YZvPrXr/c0C8FSngoJ8q1 nmRQ== 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=kwpZXKcoF62Ir0k7f00kstKcZ8C26bkIzsEgnSw8z80=; fh=juxwNcA6iKwLISFgUCNwdoIYC0NKLUseq3xZdq25RR4=; b=Ap8OqNnbZuXRaduUYW4jDU1tjm9uJDC1b0RRaJ9No+CGhW3/Ky/hImohbehIlyZD8z Nz1ZWDccjaNKUYHc9RhKttgnM0lrqmpCedu2GhYkcXEdOWf6TxovTu+BseM3izphaDhV wm4LJuvksaSLUklqxD5XaV1fIic39YKNMsvvFjJAYejLKKa/bYKyjfT1+u33WFPhnnqB svHbCCKpjtkO3IkBdqo/2GwGF9354RPsCMRGwDr3jgpDN6rrSoY8uAQGLkBFQU64WCcS CVqGu2wXmrmmZtXXt/h9UhhWkAegHmn3plC58cS+27oQqJttkWE8YvxDYChFECcWNyPJ JZeQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=N9ZGJz+U; 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 a22-20020a056a001d1600b0068219eb27a9si11508966pfx.233.2023.07.25.15.41.37; Tue, 25 Jul 2023 15:41:51 -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=N9ZGJz+U; 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 S232405AbjGYWUT (ORCPT + 99 others); Tue, 25 Jul 2023 18:20:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232347AbjGYWTU (ORCPT ); Tue, 25 Jul 2023 18:19:20 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E2434236; Tue, 25 Jul 2023 15:16:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690323390; x=1721859390; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hAk+DJFvyWblY2NP5P0nsGacBaB4H36NK+A1TuR8L2k=; b=N9ZGJz+UxkdBba341SV7LgC3BjcuEg8kUIzjEp3Wga+y0DJRTNFOit0s aXleuzhKL4bT2/44ZwrxeLqZoI4WLmqdtW7GkFY3J0iz+PsIGgvWCg9K/ y8kFBdakm2mSaK+l/K66DYlKWCDHU0o5gZxbM2zJ6W87NsNJg5jqDbrPx /lejqEhN53tsZ4nf8SKCiO+MryFkMtI4TuCTrr0zxZ89rEf/QI/Uhyl6D gznW8IW47stJYgJHSPWorRw4CAdRMeHBdd40T/xQnseoOt7Um3GlMWHJu 8+IPnSRjAo52PX/i0MfVh4VaQm85BhajpFmeqQrmVxqw5AFxt0bVe56Hx A==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="367882545" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="367882545" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:52 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="840001796" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="840001796" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga002-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Jul 2023 15:15:51 -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, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v15 065/115] KVM: TDX: restore host xsave state when exit from the guest TD Date: Tue, 25 Jul 2023 15:14:16 -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=-4.4 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,URIBL_BLOCKED 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 0a71601c78f9..40b5be05e284 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