Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp2618093rdg; Mon, 16 Oct 2023 09:31:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHiGOQ+ZtKuGac4EBa2+Fo29ztGHpFXe20CxCpMnEeM7mJd5VNW3DGnQhqe/+OLggukWfMU X-Received: by 2002:a17:90a:191:b0:27d:1b37:8bfd with SMTP id 17-20020a17090a019100b0027d1b378bfdmr10842388pjc.4.1697473899798; Mon, 16 Oct 2023 09:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697473899; cv=none; d=google.com; s=arc-20160816; b=M5j3LxXk3nWc6TeApYWcweY3h5D+T6r04CIPWzXmqbrcfql5UH8GbQJvmmlUklakgR ePf0eENQty9YOaeQ31W/X3KNVO0G9to1ocvv4gAV46y3KIrjJIhqzvBwArWS//iqzSoz qtNjJYyey/OwRU1zi42+4g4610KpemOebG+WMYE8MvoSG9aYdb8go2yVfcCCPjros/wH naLk0UKy1jEXQ7ktVbjrH6MSAkwAYoIfj2BnKzFosF1qV/8A8SAdUP+qc72WzpCkBn5q 9sm47DtB1ZF4AbKjqSzU78ZfTtXPrn5GcnS8+zOcIdlq2KHJa6UiGbKnDc1inDKgOOT9 4Gqg== 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=nwgg9MkCDTQqWjYoiUgH7YEOiDqX1k9oMzYx5QZoAQs=; fh=lRdU2Q/1zx5DcPdZuWBjshA5VT5Oc9cEhB1tCFiV0Nw=; b=NbA6pYMSS6aRHsFOxeiqfq2MMF1cVsM37Cg5mSxWwPzQp07Ye3sMkoiXMTnYnW1Vim vULzRM5aBDA7qD5STx4Lo+duHJMD4e/XfDJgxVIb8ZZj6BdKxhuNwFhmJXdc6fFHNBuq ae2Egofb60MSMeTWb23fmS1c1Xoph7aQNoRWxgQDxuPUF9Vtu0mOivmNrKc42MxVdtVl 5CEqT+vo/VzES2fI8tlCn1HmtASgvOR6Afblw6joowj3k9X+LW2e27q+liyhoUpxnTj6 Ezwpo4D9/lrs0v0CnpvQCPR3tkxRzyFXbNPn2sMFk+QJDr2J3T8J46uTGaJu9V6Cgu2H 5fGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Oi2lgQfZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id ft16-20020a17090b0f9000b00274985b2fcdsi6475829pjb.138.2023.10.16.09.31.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Oct 2023 09:31:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Oi2lgQfZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id A8C9880C03AF; Mon, 16 Oct 2023 09:31:14 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229459AbjJPQa7 (ORCPT + 99 others); Mon, 16 Oct 2023 12:30:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232195AbjJPQaz (ORCPT ); Mon, 16 Oct 2023 12:30:55 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B5D56186; Mon, 16 Oct 2023 09:21:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1697473303; x=1729009303; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WC40/RD5FfvvicfUfo6K6EJDLzlvz4ruiVzHAMVz7EY=; b=Oi2lgQfZ5ulK7lt362+uMRrtl3Y52ptMQ+VI6NZrxfPFrlGpeImg59pi W3xwGq6vqWTmbcqd0/PLkl1u6mX1w5qRJJ+si+FInlDa/NYgfLUBTczXp cGnGefLj82ZqJOixPTXFSvs4Fyy/3q4KrMl7w31mCR9NbtdpSST8gEf4s O0nIum+UEgz1kUatu31FCfoaHOU0X0/G0ye2LD/f+TjH92OPv6+ryLeE7 9mDNANIYZrVJ/b7rtFYSMh4GtVKD905+hxDIh9mupPpzHa6SOIZRpPZ6S 1TjNibc6T3D6+BKi5DrcSA5VYI0s9YIayypBUmU2E8jgcISKoOOQPRW7i Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="365825941" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="365825941" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:29 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10865"; a="1087126016" X-IronPort-AV: E=Sophos;i="6.03,229,1694761200"; d="scan'208";a="1087126016" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2023 09:15:27 -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 v16 016/116] x86/virt/tdx: Add a helper function to return system wide info about TDX module Date: Mon, 16 Oct 2023 09:13:28 -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=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Mon, 16 Oct 2023 09:31:14 -0700 (PDT) From: Isaku Yamahata TDX KVM needs system-wide information about the TDX module, struct tdsysinfo_struct. Add a helper function tdx_get_sysinfo() to return it instead of KVM getting it with various error checks. Make KVM call the function and stash the info. Move out the struct definition about it to common place arch/x86/include/asm/tdx.h. Signed-off-by: Isaku Yamahata --- arch/x86/include/asm/tdx.h | 59 +++++++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 15 +++++++++- arch/x86/virt/vmx/tdx/tdx.c | 20 +++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 51 -------------------------------- 4 files changed, 91 insertions(+), 54 deletions(-) diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index e50129a1f13e..800ed1783b94 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -110,6 +110,62 @@ u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args); #define seamcall_saved_ret(__fn, __args) \ SEAMCALL_NO_ENTROPY_RETRY(__seamcall_saved_ret, (__fn), (__args)) +struct tdx_cpuid_config { + __struct_group(tdx_cpuid_config_leaf, leaf_sub_leaf, __packed, + u32 leaf; + u32 sub_leaf; + ); + __struct_group(tdx_cpuid_config_value, value, __packed, + u32 eax; + u32 ebx; + u32 ecx; + u32 edx; + ); +} __packed; + +#define TDSYSINFO_STRUCT_SIZE 1024 +#define TDSYSINFO_STRUCT_ALIGNMENT 1024 + +/* + * The size of this structure itself is flexible. The actual structure + * passed to TDH.SYS.INFO must be padded to TDSYSINFO_STRUCT_SIZE bytes + * and TDSYSINFO_STRUCT_ALIGNMENT bytes aligned. + */ +struct tdsysinfo_struct { + /* TDX-SEAM Module Info */ + u32 attributes; + u32 vendor_id; + u32 build_date; + u16 build_num; + u16 minor_version; + u16 major_version; + u8 reserved0[14]; + /* Memory Info */ + u16 max_tdmrs; + u16 max_reserved_per_tdmr; + u16 pamt_entry_size; + u8 reserved1[10]; + /* Control Struct Info */ + u16 tdcs_base_size; + u8 reserved2[2]; + u16 tdvps_base_size; + u8 tdvps_xfam_dependent_size; + u8 reserved3[9]; + /* TD Capabilities */ + u64 attributes_fixed0; + u64 attributes_fixed1; + u64 xfam_fixed0; + u64 xfam_fixed1; + u8 reserved4[32]; + u32 num_cpuid_config; + /* + * The actual number of CPUID_CONFIG depends on above + * 'num_cpuid_config'. + */ + DECLARE_FLEX_ARRAY(struct tdx_cpuid_config, cpuid_configs); +} __packed; + +const struct tdsysinfo_struct *tdx_get_sysinfo(void); bool platform_tdx_enabled(void); int tdx_cpu_enable(void); int tdx_enable(void); @@ -138,6 +194,9 @@ static inline u64 __seamcall_saved_ret(u64 fn, struct tdx_module_args *args) { return TDX_SEAMCALL_UD; } + +struct tdsysinfo_struct; +static inline const struct tdsysinfo_struct *tdx_get_sysinfo(void) { return NULL; } static inline bool platform_tdx_enabled(void) { return false; } static inline int tdx_cpu_enable(void) { return -ENODEV; } static inline int tdx_enable(void) { return -ENODEV; } diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 9d3f593eacb8..b0e3409da5a8 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -11,9 +11,18 @@ #undef pr_fmt #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt +#define TDX_MAX_NR_CPUID_CONFIGS \ + ((TDSYSINFO_STRUCT_SIZE - \ + offsetof(struct tdsysinfo_struct, cpuid_configs)) \ + / sizeof(struct tdx_cpuid_config)) + static int __init tdx_module_setup(void) { - int ret; + const struct tdsysinfo_struct *tdsysinfo; + int ret = 0; + + BUILD_BUG_ON(sizeof(*tdsysinfo) > TDSYSINFO_STRUCT_SIZE); + BUILD_BUG_ON(TDX_MAX_NR_CPUID_CONFIGS != 37); ret = tdx_enable(); if (ret) { @@ -21,6 +30,10 @@ static int __init tdx_module_setup(void) return ret; } + /* Sanitary check just in case. */ + tdsysinfo = tdx_get_sysinfo(); + WARN_ON(tdsysinfo->num_cpuid_config > TDX_MAX_NR_CPUID_CONFIGS); + return 0; } diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 5b8f2085d293..ec0eb9edfa21 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -284,6 +284,20 @@ static int get_tdx_sysinfo(struct tdsysinfo_struct *tdsysinfo, return 0; } +static struct tdsysinfo_struct *tdsysinfo; + +const struct tdsysinfo_struct *tdx_get_sysinfo(void) +{ + const struct tdsysinfo_struct *r = NULL; + + mutex_lock(&tdx_module_lock); + if (tdx_module_status == TDX_MODULE_INITIALIZED) + r = tdsysinfo; + mutex_unlock(&tdx_module_lock); + return r; +} +EXPORT_SYMBOL_GPL(tdx_get_sysinfo); + /* * Add a memory region as a TDX memory block. The caller must make sure * all memory regions are added in address ascending order and don't @@ -1109,7 +1123,6 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list) static int init_tdx_module(void) { - struct tdsysinfo_struct *tdsysinfo; struct cmr_info *cmr_array; int tdsysinfo_size; int cmr_array_size; @@ -1208,7 +1221,10 @@ static int init_tdx_module(void) * For now both @sysinfo and @cmr_array are only used during * module initialization, so always free them. */ - kfree(tdsysinfo); + if (ret) { + kfree(tdsysinfo); + tdsysinfo = NULL; + } kfree(cmr_array); return ret; diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index 5bcbfc2fc466..c37a54cff1fa 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -36,57 +36,6 @@ struct cmr_info { #define MAX_CMRS 32 #define CMR_INFO_ARRAY_ALIGNMENT 512 -struct cpuid_config { - u32 leaf; - u32 sub_leaf; - u32 eax; - u32 ebx; - u32 ecx; - u32 edx; -} __packed; - -#define TDSYSINFO_STRUCT_SIZE 1024 -#define TDSYSINFO_STRUCT_ALIGNMENT 1024 - -/* - * The size of this structure itself is flexible. The actual structure - * passed to TDH.SYS.INFO must be padded to TDSYSINFO_STRUCT_SIZE bytes - * and TDSYSINFO_STRUCT_ALIGNMENT bytes aligned. - */ -struct tdsysinfo_struct { - /* TDX-SEAM Module Info */ - u32 attributes; - u32 vendor_id; - u32 build_date; - u16 build_num; - u16 minor_version; - u16 major_version; - u8 reserved0[14]; - /* Memory Info */ - u16 max_tdmrs; - u16 max_reserved_per_tdmr; - u16 pamt_entry_size; - u8 reserved1[10]; - /* Control Struct Info */ - u16 tdcs_base_size; - u8 reserved2[2]; - u16 tdvps_base_size; - u8 tdvps_xfam_dependent_size; - u8 reserved3[9]; - /* TD Capabilities */ - u64 attributes_fixed0; - u64 attributes_fixed1; - u64 xfam_fixed0; - u64 xfam_fixed1; - u8 reserved4[32]; - u32 num_cpuid_config; - /* - * The actual number of CPUID_CONFIG depends on above - * 'num_cpuid_config'. - */ - DECLARE_FLEX_ARRAY(struct cpuid_config, cpuid_configs); -} __packed; - struct tdmr_reserved_area { u64 offset; u64 size; -- 2.25.1