Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1180309rbb; Mon, 26 Feb 2024 00:55:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWPvLzmVKzMgfCp5Ruqy90bhrK/19RsMadOBYaLwXj/EYwW9hPz6YIG13eUxh3gtTMO/mqqESx1sa+59IP26CDQDL3toE7ZbMEcioamqg== X-Google-Smtp-Source: AGHT+IFZ2uBnVi/zYA5FERq4fzje3tyH64k49l4yc36waz1Oc0e5yHE0/cKfF1tfHxbUlwf0KhXr X-Received: by 2002:a17:906:a11a:b0:a3e:6862:dc78 with SMTP id t26-20020a170906a11a00b00a3e6862dc78mr3764285ejy.4.1708937733639; Mon, 26 Feb 2024 00:55:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708937733; cv=pass; d=google.com; s=arc-20160816; b=xZnzXdZ4hmCG2p2FJlyctykrzNney98UYodgWTSHG3oZygP95skODbkSxJGzDzbqyd eJ+15veNgu3bEEomdhmDXL0s3hE80+Ws3QuXhVUBrY63aCOC7lvi+sjgiPxJcrQHIVot bELFc/yZn83cH+rnByPs8kU1mQ+s1jIJ/NIcCqFeJL+oVAyGn8e9tLrv+t14q/cKRPbb L7UfFGlFoMM6wkWQGA7VitKFm7EtHV8ZPsu0DMOAehpY03L7sY2qQ2ba5g8KGji2kj3T b83b7DRA+uDxjxMpSEjb4wzxRizysFcTo9FortObDJ2UqNAva6WnJkpdw3vAlqtRU8Cg D15Q== 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=L4DJi+dXLS9IvRR0NzOsQsm7DTfhnCRLBf5eWwNnPNA=; fh=97Li8gbF0p9I/DrVNuPtUH0OjKLOa8S8hCh/PgfDTac=; b=RCL68yLMvhRDQg4Ffu80fT67kYBzg4LcPkWu7dvdm5o2DR3XapTYbUK2FJO2OcQhPr qevYfHwhvHeGgjvA4Q66gJhdcGRvHBIA0vd8KjtFOGhhZSOPkmG2PSfCaZITvf4kPQ4X sj3w0M5DR8u0gNNby+39hEG72UOadj6lCOmTge0btD63T3HO4ugQZFgKSaqvYUKXoWbb 9o4JW55v81TF5AebM94O1d1QYXH87dy+NBNd6VDzcu524X/YTPXmAc6UHDClFoM7/7jL cZI5tKXU47ceGnN1FITbrMhLIEHA43x0iKgmDpdIKwvY5AXs0xyvGCSGsKNxWzogZVw+ mniw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=nu6Bu72i; 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-80847-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80847-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 f1-20020a17090624c100b00a3ea034e5dasi1950285ejb.57.2024.02.26.00.55.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:55:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80847-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=nu6Bu72i; 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-80847-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80847-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 3B1821F21A7A for ; Mon, 26 Feb 2024 08:55:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D097F78692; Mon, 26 Feb 2024 08:28:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nu6Bu72i" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (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 27A2E73F1F; Mon, 26 Feb 2024 08:28:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936123; cv=none; b=Id1x6DYdYajyXn2F/FI/BmCfZpeRKQQrFeH8BwdXQp+kkJjyRf25GSrtl+/5CsOUkDTPHzxcKs1YqETgim5LiHcogtWVV8/P5acgkglxy1XNQY49FJCtUfxDbyAkUQmL64zXsmotgI5TgE5YusX9SrTPoCB4htWyKbDxZgyi5+o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936123; c=relaxed/simple; bh=QAWpp4LjklCnK07uK2feZ4n6OzER6GSYaRkObJrBCqk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=q9PdOcmIH5YL5VofnwfxHT7j/AE8bUyh7Xe9Taujj7ON8TgkDmVw2lu0KvKNOWFr50H4suW8hPhGSWgKQVwzvuNO/tM/D1+v5IawPoeXkI4Zjxa5uNPi6HpQkKLq+IwPukYnA3wRoEAeZtlcbs6L2XR7GBDEPQfn6zDgVX549B4= 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=nu6Bu72i; arc=none smtp.client-ip=198.175.65.19 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=1708936121; x=1740472121; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QAWpp4LjklCnK07uK2feZ4n6OzER6GSYaRkObJrBCqk=; b=nu6Bu72iExAw+Ha37v+ax1J+digzGAae9PZfXnRdBGF4Tcu9dyKUZReS CywflQCdhHZWzva/P4rBGQ+dwDAUqr60LfVsfbR5rbDAdvnwFAgII0U87 v2b8IRSJuKN0lhk46ZZ9BoeUAumb9tPc4JtQvrHIvQsdr8AXffkwwV3WC IbGBkD/bnf+2uUDlKRg0oJ0jYy8ydEC4nCCPotW8GC+qBZx3evBO0140X Kxfhtmg99LWud5+zlTVgZFcmbmERS9/aTJ/T/cuNEAULXNr5j9Y2gPALZ pW/q//GklbTDS4Cx3cefWnPIKOgTMZDhQrrUGAxya07dzq7ODV/5v2/pP Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="3069521" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="3069521" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:41 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="11272563" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:40 -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, Yang Weijiang Subject: [PATCH v19 083/130] KVM: TDX: Add TSX_CTRL msr into uret_msrs list Date: Mon, 26 Feb 2024 00:26:25 -0800 Message-Id: <06135e0897ae90c3dc7fd608948f8bdcd30a17ae.1708933498.git.isaku.yamahata@intel.com> 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: Yang Weijiang TDX module resets the TSX_CTRL MSR to 0 at TD exit if TSX is enabled for TD. Or it preserves the TSX_CTRL MSR if TSX is disabled for TD. VMM can rely on uret_msrs mechanism to defer the reload of host value until exiting to user space. Signed-off-by: Yang Weijiang Signed-off-by: Isaku Yamahata --- v19: - fix the type of tdx_uret_tsx_ctrl_slot. unguent int => int. --- arch/x86/kvm/vmx/tdx.c | 33 +++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 8 ++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 7e2b1e554246..83dcaf5b6fbd 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -547,14 +547,21 @@ static struct tdx_uret_msr tdx_uret_msrs[] = { {.msr = MSR_LSTAR,}, {.msr = MSR_TSC_AUX,}, }; +static int tdx_uret_tsx_ctrl_slot; -static void tdx_user_return_update_cache(void) +static void tdx_user_return_update_cache(struct kvm_vcpu *vcpu) { int i; for (i = 0; i < ARRAY_SIZE(tdx_uret_msrs); i++) kvm_user_return_update_cache(tdx_uret_msrs[i].slot, tdx_uret_msrs[i].defval); + /* + * TSX_CTRL is reset to 0 if guest TSX is supported. Otherwise + * preserved. + */ + if (to_kvm_tdx(vcpu->kvm)->tsx_supported && tdx_uret_tsx_ctrl_slot != -1) + kvm_user_return_update_cache(tdx_uret_tsx_ctrl_slot, 0); } static void tdx_restore_host_xsave_state(struct kvm_vcpu *vcpu) @@ -649,7 +656,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) tdx_vcpu_enter_exit(tdx); - tdx_user_return_update_cache(); + tdx_user_return_update_cache(vcpu); tdx_restore_host_xsave_state(vcpu); tdx->host_state_need_restore = true; @@ -1167,6 +1174,22 @@ static int setup_tdparams_xfam(struct kvm_cpuid2 *cpuid, struct td_params *td_pa return 0; } +static bool tdparams_tsx_supported(struct kvm_cpuid2 *cpuid) +{ + const struct kvm_cpuid_entry2 *entry; + u64 mask; + u32 ebx; + + entry = kvm_find_cpuid_entry2(cpuid->entries, cpuid->nent, 0x7, 0); + if (entry) + ebx = entry->ebx; + else + ebx = 0; + + mask = __feature_bit(X86_FEATURE_HLE) | __feature_bit(X86_FEATURE_RTM); + return ebx & mask; +} + static int setup_tdparams(struct kvm *kvm, struct td_params *td_params, struct kvm_tdx_init_vm *init_vm) { @@ -1209,6 +1232,7 @@ static int setup_tdparams(struct kvm *kvm, struct td_params *td_params, MEMCPY_SAME_SIZE(td_params->mrowner, init_vm->mrowner); MEMCPY_SAME_SIZE(td_params->mrownerconfig, init_vm->mrownerconfig); + to_kvm_tdx(kvm)->tsx_supported = tdparams_tsx_supported(cpuid); return 0; } @@ -2014,6 +2038,11 @@ int __init tdx_hardware_setup(struct kvm_x86_ops *x86_ops) return -EIO; } } + tdx_uret_tsx_ctrl_slot = kvm_find_user_return_msr(MSR_IA32_TSX_CTRL); + if (tdx_uret_tsx_ctrl_slot == -1 && boot_cpu_has(X86_FEATURE_MSR_TSX_CTRL)) { + pr_err("MSR_IA32_TSX_CTRL isn't included by kvm_find_user_return_msr\n"); + return -EIO; + } max_pkgs = topology_max_packages(); tdx_mng_key_config_lock = kcalloc(max_pkgs, sizeof(*tdx_mng_key_config_lock), diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index e96c416e73bf..44eab734e702 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -17,6 +17,14 @@ struct kvm_tdx { u64 xfam; int hkid; + /* + * Used on each TD-exit, see tdx_user_return_update_cache(). + * TSX_CTRL value on TD exit + * - set 0 if guest TSX enabled + * - preserved if guest TSX disabled + */ + bool tsx_supported; + bool finalized; atomic_t tdh_mem_track; -- 2.25.1