Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1171625rbb; Mon, 26 Feb 2024 00:29:56 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWK+TmP7DCKo2zO+vmkxSRgFeaiB/LZiEPzSeVWA/wI+FCYdesqsW8GgsIonTU/ZxfYIzYBd5DG36O6P6NKtHGxgppegdULRGAN8fLpjg== X-Google-Smtp-Source: AGHT+IH1OvW/rStFVuRuIRuvs0D7Cco8Rz0PjKFYxKfWZe0nZYvjU8K9yStozJT05+o9nHZu7M+e X-Received: by 2002:a17:90a:bf13:b0:299:3482:d162 with SMTP id c19-20020a17090abf1300b002993482d162mr7856807pjs.12.1708936196595; Mon, 26 Feb 2024 00:29:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708936196; cv=pass; d=google.com; s=arc-20160816; b=szb++TjQIaBA82bxQ6oCWDQmS8GnAH4LFnYYlKrTjTwIgcUkQnGefFPf3OAmBloPXM +E3g6s8hw8TBq9C/5HWvi2weoxBCy+kv2PMIDR9K2Wf34+p/qpD4FtPGnzqW85szmw6I QXPberYbaGCz/NTno9YfAL/gYmxHLLvGRe4AEWwJFkzBkNUwsToTokWghPswVMjbdgTH O041jSqpGA1pG2EhTgFsEGH5wrHq2bCVmKWF5XJ3zzu8IH296IojOrnCjhHP4tFc2t3j l9JyIxTIMobkjlSFz9T0IbpqS4tnOkBDOYtpr4F1EQi7SwxFZzoo96IEBTu1wH4XA8Db CBng== 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=pgrwvNl9Pe/QOSC7ad7RmPZE3C/esr0LoE4E4s7F0Tk=; fh=wfzf/8eWbe9eyxhcv3ArurOzI8Y6mCaD4zS1Rilq9io=; b=c/kxUND/GB/X/qiBykZwImZQ6cdhiD7Icm0BsEFa+5ymAZwN9sHyiURpUYiO5RT5aD 0WUL7OKuPbJbE7pVustIaSZdMYQyM7tNFz/uJh5qjwfNO524ihprNNMiLHOoUwjOT1uA agn/HoQRj1mSr7k2O4Xitc7X/kuoAsjax4ZXjVubFH47CdKJ/gvegqKxl+826IQKovq/ +9ZimZA3TXK3bZ0Lhbcw3k1Y+7nVzzqHBWf+5k1H2ikHIoAEMFvt2IWV5uBYGf1LaC2+ QTeFhEeLOlUaBbnH/RA56FYzbjLxJ8aN3yET5XK75isunp/5TK6VNj5Zz+aOCnFSPqw9 lHFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CKXMFwPT; 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-80766-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80766-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id bg3-20020a17090b0d8300b00290349120dfsi5214835pjb.129.2024.02.26.00.29.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:29:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80766-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=CKXMFwPT; 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-80766-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80766-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 4AE1F284269 for ; Mon, 26 Feb 2024 08:29:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4C2E3381A1; Mon, 26 Feb 2024 08:27:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="CKXMFwPT" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (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 EE17E200D5; Mon, 26 Feb 2024 08:27:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936061; cv=none; b=bI4h32bjIgFic4tDx1KVV61iBhjj02hS9gziswhSyUm3wwwjFTQ+Qaxcqmvn5Bhlsyj5O2LN2hsffaH6mEiqLJw2lE2fSXDIwJpVLWUXOIj42PwI5oHPx/hzFVwyq8GEiLN25nSy602fUJUF58X3JuDT1ESbASxfL/Pi89rj/ic= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936061; c=relaxed/simple; bh=WwPAQVyS6ydkrI/ZnLxTBH2KaaNIW1B/7NwF7C9kOnA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ejQDQOvzpC71nXy5fgQ4sykAI+OJz71tt6WJBrguC3AfD6hEhIYwXw936ZBf/EIBLVrC/3ODdjxE30/ANoFfHE306L6XDKaEWxBM3ZqnscFKBIcT9hEUSAEU8vIrnr6oKsj7B3Vb72X/D+zKudUnvmY7PAPID4tU7S09XSWq0U0= 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=CKXMFwPT; arc=none smtp.client-ip=198.175.65.16 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=1708936059; x=1740472059; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=WwPAQVyS6ydkrI/ZnLxTBH2KaaNIW1B/7NwF7C9kOnA=; b=CKXMFwPTaQE5LrCyCQ+FoTc1WCsogcaL0w592cJI5tB8J3YHi2BOgBdl sDlsZW3gdXvh0uOPAgPhjqpPXcNoLau6iFSUWgkn2G1CfuKdC8Byy+/Q0 08tk/kTRx34+R46XAyoRb4DREEs3KRW3j+gK3cVwvqoHF+1MB2LHZ5OMS iRavtE2IIEAM2Cj/53NU0SKXXV0fNYgERdi94UXVnzqtEKP3lczsDkqmE WCunFC5uQFweQQ2BAHvegdEM3d8eHhygY1TcKofwbYUkRCDVjhjM/5lLo v2COmKXTsPf/nnAxEwecCTnpMtpt21T8+wYg3FTYzQn3/2AUhj+coy6wt A==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="3340712" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="3340712" Received: from orviesa006.jf.intel.com ([10.64.159.146]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:27:38 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="7020075" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:27:37 -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, "Kirill A . Shutemov" Subject: [PATCH v19 005/130] x86/virt/tdx: Export global metadata read infrastructure Date: Mon, 26 Feb 2024 00:25:07 -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: Kai Huang KVM will need to read a bunch of non-TDMR related metadata to create and run TDX guests. Export the metadata read infrastructure for KVM to use. Specifically, export two helpers: 1) The helper which reads multiple metadata fields to a buffer of a structure based on the "field ID -> structure member" mapping table. 2) The low level helper which just reads a given field ID. The two helpers cover cases when the user wants to cache a bunch of metadata fields to a certain structure and when the user just wants to query a specific metadata field on demand. They are enough for KVM to use (and also should be enough for other potential users). Signed-off-by: Kai Huang Reviewed-by: Kirill A. Shutemov Signed-off-by: Isaku Yamahata --- arch/x86/include/asm/tdx.h | 22 ++++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.c | 25 ++++++++----------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/arch/x86/include/asm/tdx.h b/arch/x86/include/asm/tdx.h index eba178996d84..709b9483f9e4 100644 --- a/arch/x86/include/asm/tdx.h +++ b/arch/x86/include/asm/tdx.h @@ -116,6 +116,28 @@ static inline u64 sc_retry(sc_func_t func, u64 fn, int tdx_cpu_enable(void); int tdx_enable(void); const char *tdx_dump_mce_info(struct mce *m); + +struct tdx_metadata_field_mapping { + u64 field_id; + int offset; + int size; +}; + +#define TD_SYSINFO_MAP(_field_id, _struct, _member) \ + { .field_id = MD_FIELD_ID_##_field_id, \ + .offset = offsetof(_struct, _member), \ + .size = sizeof(typeof(((_struct *)0)->_member)) } + +/* + * Read multiple global metadata fields to a buffer of a structure + * based on the "field ID -> structure member" mapping table. + */ +int tdx_sys_metadata_read(const struct tdx_metadata_field_mapping *fields, + int nr_fields, void *stbuf); + +/* Read a single global metadata field */ +int tdx_sys_metadata_field_read(u64 field_id, u64 *data); + #else static inline void tdx_init(void) { } static inline int tdx_cpu_enable(void) { return -ENODEV; } diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index a19adc898df6..dc21310776ab 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -251,7 +251,7 @@ static int build_tdx_memlist(struct list_head *tmb_list) return ret; } -static int read_sys_metadata_field(u64 field_id, u64 *data) +int tdx_sys_metadata_field_read(u64 field_id, u64 *data) { struct tdx_module_args args = {}; int ret; @@ -270,6 +270,7 @@ static int read_sys_metadata_field(u64 field_id, u64 *data) return 0; } +EXPORT_SYMBOL_GPL(tdx_sys_metadata_field_read); /* Return the metadata field element size in bytes */ static int get_metadata_field_bytes(u64 field_id) @@ -295,7 +296,7 @@ static int stbuf_read_sys_metadata_field(u64 field_id, if (WARN_ON_ONCE(get_metadata_field_bytes(field_id) != bytes)) return -EINVAL; - ret = read_sys_metadata_field(field_id, &tmp); + ret = tdx_sys_metadata_field_read(field_id, &tmp); if (ret) return ret; @@ -304,19 +305,8 @@ static int stbuf_read_sys_metadata_field(u64 field_id, return 0; } -struct field_mapping { - u64 field_id; - int offset; - int size; -}; - -#define TD_SYSINFO_MAP(_field_id, _struct, _member) \ - { .field_id = MD_FIELD_ID_##_field_id, \ - .offset = offsetof(_struct, _member), \ - .size = sizeof(typeof(((_struct *)0)->_member)) } - -static int read_sys_metadata(struct field_mapping *fields, int nr_fields, - void *stbuf) +int tdx_sys_metadata_read(const struct tdx_metadata_field_mapping *fields, + int nr_fields, void *stbuf) { int i, ret; @@ -331,6 +321,7 @@ static int read_sys_metadata(struct field_mapping *fields, int nr_fields, return 0; } +EXPORT_SYMBOL_GPL(tdx_sys_metadata_read); #define TD_SYSINFO_MAP_TDMR_INFO(_field_id, _member) \ TD_SYSINFO_MAP(_field_id, struct tdx_tdmr_sysinfo, _member) @@ -338,7 +329,7 @@ static int read_sys_metadata(struct field_mapping *fields, int nr_fields, static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) { /* Map TD_SYSINFO fields into 'struct tdx_tdmr_sysinfo': */ - const struct field_mapping fields[] = { + const struct tdx_metadata_field_mapping fields[] = { TD_SYSINFO_MAP_TDMR_INFO(MAX_TDMRS, max_tdmrs), TD_SYSINFO_MAP_TDMR_INFO(MAX_RESERVED_PER_TDMR, max_reserved_per_tdmr), TD_SYSINFO_MAP_TDMR_INFO(PAMT_4K_ENTRY_SIZE, pamt_entry_size[TDX_PS_4K]), @@ -347,7 +338,7 @@ static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) }; /* Populate 'tdmr_sysinfo' fields using the mapping structure above: */ - return read_sys_metadata(fields, ARRAY_SIZE(fields), tdmr_sysinfo); + return tdx_sys_metadata_read(fields, ARRAY_SIZE(fields), tdmr_sysinfo); } /* Calculate the actual TDMR size */ -- 2.25.1