Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp267937rwb; Tue, 25 Jul 2023 15:44:44 -0700 (PDT) X-Google-Smtp-Source: APBJJlHEL4psdHviqCITFrVNXJdcqM0mIMsflwd7k/wFRiZJhdEdDV3FZVkMYizK2OfqF6442xEw X-Received: by 2002:a05:6a20:158d:b0:13a:8082:531b with SMTP id h13-20020a056a20158d00b0013a8082531bmr628049pzj.4.1690325084428; Tue, 25 Jul 2023 15:44:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690325084; cv=none; d=google.com; s=arc-20160816; b=pjjz4zeqTV5qpiMZ14haaxCnP8rmvcB2QMQ6gihPbJGv25a/xZdqFqD0DiaFURo6RB CT1eIClU5arRB3FALijMCX4h6d1TpOAZW8BTLW183VgxR1QNu6+YHZlPAM0wog/Kt5zE NU32RqueqraCk8Liwi5OmgMsWy+0YBLoCv/u2dEBktimA5ojm6kmTENozrIHIvgGItbD xMpPzeYw36D0VkWQZ7mJlfzxsISLwEl6SovFtL1jgTGFHMwaInYUUyRh06wMUO/iS9qL 5YyNwUHWOmz5MmxHDa6SQeX17pDrY4WIbx3Xw4Ud+3O6FiYwKxrEuL2Bnha6gzhvR0Lf IVlA== 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=ohu3FUTukGfaUw8DF//N7rvexQK2IxgMmNaFnQK65FE=; fh=vRiFISoCTUYFP6ULNLn26mIVCV7dUEjPHhAKWRflFhc=; b=WFI+vpHiHrTGgv8xV1dJkFv+/70b1Ke8QQSvxYudnZdQW5Vuyr6tlJDi2fiLoKGT4W rSvxFhwquXp4zxWhhl76Dsrx8RX9vy1Ig9NshD5oOgXqjjnuYnN6j7ioiOM29FCK8w0f N1q9kpOoby79lJ6DRaX8RphH2vq/DBMuuQA+2p1sqG1sVfmeZOJt38i8nJnaPfwNBMs4 RsjYLcmaw5c3d9xLX3ckEsMMp4fGkDA0wdTIur3JybYWh3SM6YTAp0lJvzQWHrQecLOM E5Msx8xUGTw+VxllKQSfRb44g/njfg2Wz9/hCzefPtWtIfzQLSiRpEiuEBi840Hr/7tL EQgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=fNtQM2Cj; 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 w8-20020a63f508000000b0056379ae7b03si12349863pgh.71.2023.07.25.15.44.32; Tue, 25 Jul 2023 15:44:44 -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=fNtQM2Cj; 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 S232448AbjGYWUv (ORCPT + 99 others); Tue, 25 Jul 2023 18:20:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232452AbjGYWTe (ORCPT ); Tue, 25 Jul 2023 18:19:34 -0400 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B9BE44BE; Tue, 25 Jul 2023 15:16:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690323406; x=1721859406; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HFH89bGGYO1CEevnA/rPrJnk0mYCrAPQ2IaH6NklX3c=; b=fNtQM2CjUYCHTb9HvQvTDmyz5P1R7y1bDEE0JcBnpVSI6lxyrM2FVWcO nhrVHjUQpKLZGxHSEYJYHsc8Ne47xWHKZxoNUlzvCoPwD7C7VAaFkzmKO 6or47927g/S9xVzimOLLbesRsIDqIb8rTjXmzRB06jOXUCT1BX/SKmLiK JyPu7aNRqasOMaTAnVRKLajszg6MbXHA0nTt3r0JvjRjBH7FXI8DsZnfQ wiFWakhbvq4o23wXuNzZ892fBSVDsqd3LLCuEgqNOHKaVsyTKq/1z598x EzwDGgK1g8X1xsqbEQa4xLTCEy5XZKgFFDOrxIXoUCLr95tPLHwdyFObv Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="367882562" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="367882562" 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:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10782"; a="840001805" X-IronPort-AV: E=Sophos;i="6.01,231,1684825200"; d="scan'208";a="840001805" 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:52 -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, Yang Weijiang Subject: [PATCH v15 068/115] KVM: TDX: Add TSX_CTRL msr into uret_msrs list Date: Tue, 25 Jul 2023 15:14:19 -0700 Message-Id: <6c2d9c76d58cd511e2a271014d6e04eecea45cca.1690322424.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.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: 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 --- 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 05657e51406a..1ae72c84d40e 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -496,14 +496,21 @@ static struct tdx_uret_msr tdx_uret_msrs[] = { {.msr = MSR_LSTAR,}, {.msr = MSR_TSC_AUX,}, }; +static unsigned 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) @@ -547,7 +554,7 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu) tdx_vcpu_enter_exit(vcpu, tdx); - tdx_user_return_update_cache(); + tdx_user_return_update_cache(vcpu); tdx_restore_host_xsave_state(vcpu); tdx->host_state_need_restore = true; @@ -1050,6 +1057,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) { @@ -1095,6 +1118,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; } @@ -1760,6 +1784,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 a3c64a2ec9e0..98c8d07723a1 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; + hpa_t source_pa; bool finalized; -- 2.25.1