Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp169506rdb; Mon, 22 Jan 2024 16:12:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IFAytyNehVb8q6V6gasrTmAaNFs8aHLX8MY2wWLJeGE4XkiY/fp+RBdVm25jIWjRAjRihHl X-Received: by 2002:a17:906:e295:b0:a27:d1c5:906 with SMTP id gg21-20020a170906e29500b00a27d1c50906mr2826146ejb.60.1705968741114; Mon, 22 Jan 2024 16:12:21 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705968741; cv=pass; d=google.com; s=arc-20160816; b=GcSFW5uOfylifgDOv1+dAGWZrT8RIP+c851AVHrRtkaU4xbs2Rn+F1XxmDaRjNnxRx 25/HTIolM6mnjb2sgcT5EB5TgG9shUMVS6wfliX49QEHX/f5G04OAEcvAKk/ahnPd8iN FdgStbTRtBgR0vkZuRvxEhlNdIpNoK2NloQuWUqeHCzleEjGKd/PJe3KS72l59r5aku6 bukyJEg9luGXha1rOa66oM2nfFhFnQetBqmPKL9ebf8hFo/FGz5H0r9Wzg2sgfskuLqA fmBqPr98AdAtHbrf4esy7AL93Cd4n4i6HHAqO2AZiWtr1gz0Ditkg5k4QEPBHcwf9jNb 4dFw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=HdB6o8VSmpt8sIuBPT9qtLDg4b6rQbYgjVByBbir/EA=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=T3KiF3w54IWRxAFpkwjv5UUpAizf1WYUG67j7S7qu35S0jJ69AyqqY/VaINDLqy3T/ gLs11x8vuOWC8h0ZjQQ51qETo7DK5tiKVEqznmJDWvNj2zs6zHKbiJuqHyKf4Vz1sMwl vDVTCthPWc9hy5f2rSVTGvP9m9A+0x2NiZ4cSzpzfSlu8LIyEXLYWpI/MLfsA+KRrzsC MBPpWX+oZCVbekIPBknk5spu3CafeJ70Y6mR9Xp+SZfpzh7YsnRho60ES/RSRiXh/4Yg ahFzBMacIsBBAqIC0Z56WekZKWuBlNN0UHkVSmM4Cl3EQLTlSrPtGAYyYOKD5P0wBlkt xhpQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Kp9EzbBb; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-34398-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34398-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id x21-20020a170906299500b00a2f1bb991d7si4856294eje.716.2024.01.22.16.12.21 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Jan 2024 16:12:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-34398-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Kp9EzbBb; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-34398-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-34398-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id A2EA81F2198A for ; Tue, 23 Jan 2024 00:12:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1A34C6E2A4; Mon, 22 Jan 2024 23:56:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="Kp9EzbBb" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8905D4F5F1; Mon, 22 Jan 2024 23:55:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.11 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967760; cv=none; b=EUn7A+UC4pHmwW+8KFeEvhjDoy2ZTb1bq50MMz64XIKMUIvly+mP/k5QCYjhmLYqKmNJChzGY6R9RFAUdbwfPqXINldE1MbdpycKEaKMqLCHdJbMkiAPw5dLNEHGceLIr3rjVGMrkxc7bRrGwHRNL/Dwfff4AxD35q9Eh7Ui+P4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1705967760; c=relaxed/simple; bh=5Wi3VocsOvptzgAHMe7W6mABPI53Gv4SBRgV6vwuePw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MwEy99LZBWgtWEHIe2o4LmDSJOAxz5X3yDDLx4uBNTjZT7p6LPgkwAuwWTSyr3+43yLyayQCGJByGJX4LV6I77I1Wqg+mvIXjyDeiZnqtmXOgcVVBdHx10tTP31hOgWBJ5u76yg74ZQmzaDsAqYgue4FbcSJpvXwMY76O9c8Yu8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=Kp9EzbBb; arc=none smtp.client-ip=198.175.65.11 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1705967759; x=1737503759; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5Wi3VocsOvptzgAHMe7W6mABPI53Gv4SBRgV6vwuePw=; b=Kp9EzbBbyLLZ6w5ZRELtgz1Fq9fP7HB/KiX/XOAK+k7Ic2UFhqINCB40 helc111VM+1aTDHJIg2mjqO/ojBSJgxXxEd7VjtBHInnZ5/e+BYa2IN7H 3UiyM0p+1TIya3Tm0tmRNUFniHJty6T9O05wMcG7zY0VeGYbga+FtUPHj bu1s9/xC0dQ+FN8Cib1gFMlGZfz7onG8+TnQ0aiU2B56jw7vA7k8JTDuj sznIe4wSNR82OfScDMefs376AEJ/EhExXuTAaaZyi/L85Gsu5bOdyQ2l7 kXtWwU7FCE2nYy5r4SAW1VzIMJMcsbpLCQIBJezlUXZEB+rN9pUvNQ5fV Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10961"; a="8016521" X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="8016521" Received: from fmviesa004.fm.intel.com ([10.60.135.144]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:41 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.05,212,1701158400"; d="scan'208";a="1468246" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmviesa004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 Jan 2024 15:55:39 -0800 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 , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v18 069/121] KVM: TDX: restore host xsave state when exit from the guest TD Date: Mon, 22 Jan 2024 15:53:45 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Isaku Yamahata On exiting from the guest TD, xsave state is clobbered. Restore xsave state on TD exit. Signed-off-by: Isaku Yamahata --- v15 -> v16: - Added CET flag mask --- arch/x86/kvm/vmx/tdx.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 903f4abb3543..fe818cfde9e7 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" @@ -584,6 +585,23 @@ 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 | TDX_TD_XFAM_CET))) + 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); +} + static noinstr void tdx_vcpu_enter_exit(struct vcpu_tdx *tdx) { struct tdx_module_args args; @@ -659,6 +677,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) tdx_vcpu_enter_exit(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