Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2685246rwd; Sun, 28 May 2023 22:10:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6cIinpVsY18NGUtoa62pu0Y489eBHvLraAH6TfRpwV7J9QggVMd8XmnDDQU+XEaqrbuT0l X-Received: by 2002:a17:902:ec81:b0:1af:a143:b0e8 with SMTP id x1-20020a170902ec8100b001afa143b0e8mr11909171plg.27.1685337048625; Sun, 28 May 2023 22:10:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685337048; cv=none; d=google.com; s=arc-20160816; b=LebxQ9VAXIhEMoCBmm50r6Z4lXh3SVUaKFgAd+JzRzOcfVWwadL1jOQEN0ALkaur3j 3KelaN/dE+00VpXA+dzxeJJ76G3oxFVpqZutgeIcHLLK+eZLDMmYpWcD12jGQaOz1A0n aq8qkC4VxOJlg8UxtXNrRSFVT5jAZzyf2fq6QlQwUJ5C+LNrr+Q1IQrjo4GQXHXN4Ir3 YH1pstmvrpUyH3NBW10xVLmDbh4Dn8jTFENk/eJ1dqhjp44fl12EnKurB1v5lUA7t7oW 4oPtFfZB2UvbXHnN18YiavtM3VaJRuU//qLgcC0HXDVina7TOr/I0K+URYFvrihQhzb9 j4Pg== 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=vLIsVNoCWBYfLo4DSWO/F5M5WUxYbf7F9NeIC2tTp/0=; b=QTuW9Sa6ygaGw0SMzfkHdxoyypdGQGp4Y98uZ5KmxZtll2JdzTRPp12dTY3L8q7Fnn pt36W3B4u4ZNjd+62yeqVfWDtj7/BhGtJ0uzRg4RhRl6g1I56MDQ9Bc8fmjP9ZoRT5q/ CSGNkHEp3GIYkUxxXZMNqge6l5sSplS3Xo+wTiq12tP+glqM+/0lsvCEnzdEHJhdqm8y tvyKwM/1cALwrLXFX4qGwnFf4cHQA5IabIuBQAbcQ+F3sIKwWP5mJiIGpg0KUiwhnuPK OSEjQ3JnkpxF2wMHZPClOhdjCgdAHUBcpU7xgd6+WW68RJc7ENYQQ9lX2aCx7l+DCdcK XQyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JyAz0S5f; 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 z8-20020a1709028f8800b001a6f0eab385si9018550plo.55.2023.05.28.22.10.36; Sun, 28 May 2023 22:10:48 -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=JyAz0S5f; 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 S231873AbjE2E2z (ORCPT + 99 others); Mon, 29 May 2023 00:28:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232010AbjE2E1v (ORCPT ); Mon, 29 May 2023 00:27:51 -0400 Received: from mga17.intel.com (mga17.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 577C71703; Sun, 28 May 2023 21:24:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685334293; x=1716870293; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LjWkX9F7REZVvnqNtpWOCBvIsIKlBV/YinSlFWZjBKQ=; b=JyAz0S5f3YzFw5oGY47Rary9ErUekLsWUZbP6JXX2k1yZXrV73bVjbJO byBKTx+ZYjOabHSmZftNY6jcoWUvtwxVAaBRRco/m6Kok8v5eL9j4IXZK +7loQj4KD1aiGuLMFM32r/A13K2LcXBDUqJao8yEWK283VsTImEc1zX3R zxCsTtgJBS8tOr95AEpmHA2diKh/HoIrSx4LNvyn01/V/u8nmcFSSmKi8 KLjlbKDgb9BXgWj9w1fIfINAJtsXSAqFgjS/NoMNYo4nwftmhuhDM1Mdr CcxXE57fqjIAvR0nNhooi4Uq3QftcRU7VMpRFzOQenA6n3VBzhM7B+Z2s g==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="334966147" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="334966147" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="775784428" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="775784428" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:21:26 -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, Yang Weijiang Subject: [PATCH v14 068/113] KVM: TDX: Add TSX_CTRL msr into uret_msrs list Date: Sun, 28 May 2023 21:19:50 -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.6 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 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 | 35 +++++++++++++++++++++++++++++++++-- arch/x86/kvm/vmx/tdx.h | 8 ++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 5b5c68a1ece4..29aa53d530ca 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) + 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; @@ -1021,6 +1028,24 @@ 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, 0x7, 0); + if (entry) + ebx = entry->ebx; + else + ebx = 0; + + mask = __feature_bit(X86_FEATURE_HLE) | __feature_bit(X86_FEATURE_RTM); + if (ebx & mask) + return true; + return false; +} + static int setup_tdparams(struct kvm *kvm, struct td_params *td_params, struct kvm_tdx_init_vm *init_vm) { @@ -1064,6 +1089,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; } @@ -1693,6 +1719,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 (WARN_ON_ONCE(tdx_uret_tsx_ctrl_slot == -1)) { + 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 cd50d366b7ee..6ce362c70e26 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 + */ + u64 tsx_supported; + hpa_t source_pa; bool finalized; -- 2.25.1