From patchwork Mon Feb 26 08:25:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Isaku Yamahata X-Patchwork-Id: 206343 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp1938176dyb; Mon, 26 Feb 2024 00:30:49 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUp3Z+IXg6Pd0s8Qmoz4h6H5fU+HBlweLMKtQZ8ftumyfij+yJi6PPZKHoU+sGqM4hgz3NtX7hFqcwBHbuK1f1Yk6mgCw== X-Google-Smtp-Source: AGHT+IGy/0TdkDT1tPWSRzDpQkP231sxJBW80pCrcBMbDRU2femJBBIk5GGS3NvQ7qCO51ZyaA/9 X-Received: by 2002:a05:620a:1038:b0:785:da87:825b with SMTP id a24-20020a05620a103800b00785da87825bmr7655189qkk.38.1708936248875; Mon, 26 Feb 2024 00:30:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708936248; cv=pass; d=google.com; s=arc-20160816; b=QyqpGNl/38F9+Rg1Mqo/0PWubpFjXTY9EgEMfB1hNKyfS3YszhX7qm5bFA39Hx+rGJ eNpF3/uQQJMLlXsOEl4TFy/t+WCrf0BspFmVm6e97qX86dUDzW9yq3GH/lTn83teI3qh cmD2EUlhVCPI19F868oiWbIvymXOgNa/1NI9AaQzFCJQG769a3Lyy2OSr2CePNk2zXbe 2DRnoody2Zp3brvVV78MQWeJr90eFee+ZJgG59YH7FCiv8QgfZEItFF6k0a1ge/mTqY8 8n+NorPEwJtGNdAtgsTTkk7MxwwZfMZvVdkdCOn70w4p0j4QEvANPkuoBDaelfRXa5X3 U7mg== 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=T37w/bpAMAyR0KCx9Yaihb76xUCVoAWz0mtnyLuaOVg=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=EcVD/XtjOcqvIF30RlSQHzB9Y7o2+W67NXa/R+iqZRyoNzHojMZzrxCzXsEq4CqJKV 1iNYx7e1JLpvlIoqkQ0tajGP0acd0KjTX8TQQg+2N9IEXFIBX130MhZX2HYmlPoNJbfy Ac3QgCjljS/+mbdpsLOpDWKABpVw/YyQK+jvLAtHqwwmkBcdHJrnPSz8KsGZ76IeZ3MQ qcvAHrsUf1Z72XZDlsVjhmTCvpaEmP1VF0r7Se9RTzMu4Ah5T00KTUNjqe1EOrlLCEP7 /aKJmKxDk90idf32dYJvQKqs2v4T5ZnfKkbfbOAAZWG8UCTnP0z6lsnIBpaFYl29DkgW 5E0g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=FSWBBbRM; 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-80764-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80764-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h21-20020a05620a10b500b007874ee5955esi4587824qkk.475.2024.02.26.00.30.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:30:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80764-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=FSWBBbRM; 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-80764-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80764-ouuuleilei=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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 4567A1C22D04 for ; Mon, 26 Feb 2024 08:30:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D73E648CCD; Mon, 26 Feb 2024 08:27:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="FSWBBbRM" 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 56FA21EEEA; Mon, 26 Feb 2024 08:27:37 +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=1708936059; cv=none; b=RnY0MUTQ3ByxnOodU7zpqQgf2QiRp7QOEPudp1/oZw6J7nsIzwPRtz8vUOvBxCUT2UO0xQW2ZgAKNaAm1lN5bAwZSPTWDe4TmlrNnVRG7uBeuX+prxGbqz/+dODUOihq7r9LmPhD8wyubrudEn1gvWkEsdcL0lHLdLNlCbn9upo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936059; c=relaxed/simple; bh=CebvYlBsSGTBw3oWiiDipBdbdj7zNc7xmDvoSKzO9SY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JS9FcnuUvGyRZfwsPeXetmgvmiLlZga9L7jB8Cn6XIjuSQR1Kktro874CUiURDTbWSVvJnIe0prWENNiOT4KcUX3b3HiRjCnsUwsNUEblV4KT7xSmStMRpMkk9RrRTcZmT3q7sfjlwF07EPeJGbxqLhyArTaaSeTxATgWpwLRwo= 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=FSWBBbRM; 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=1708936058; x=1740472058; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CebvYlBsSGTBw3oWiiDipBdbdj7zNc7xmDvoSKzO9SY=; b=FSWBBbRMLitF5IqDCT7dvd9kY8U3cOk5mSm+KlP/+IBlD5S5/WA4Ufq2 7IoeQmbplalzOzAGD9DNgaAIlWiEpzpazXmojkSyIOFrHvhPZq8Gb9cdB mf+cC7s3WUb5o5fKvAubsmFOodvkajGblTDp0+ylfnLGKKVj6SWH0z5KI BH0CJyx0FvCA01KdNqxe3a0Z6/4Wa3kUeACww5FiKmP92slxUE1M6cqog vN5sWFWMMjn+QSvytCTWFgWsjBG3RlvSO/Vbs8h2jBWesARNSR4cF0iOF HDpcYQGLKmKAx5rNgsdy+kriIEByvSmYNQfs9n+bbaI5Z052JrGHTPhy6 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="3340707" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="3340707" 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:37 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="7020066" 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:36 -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 Subject: [PATCH v19 004/130] x86/virt/tdx: Support global metadata read for all element sizes Date: Mon, 26 Feb 2024 00:25:06 -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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791949536193691354 X-GMAIL-MSGID: 1791949536193691354 From: Kai Huang For now the kernel only reads TDMR related global metadata fields for module initialization. All these fields are 16-bits, and the kernel only supports reading 16-bits fields. KVM will need to read a bunch of non-TDMR related metadata to create and run TDX guests. It's essential to provide a generic metadata read infrastructure which supports reading all 8/16/32/64 bits element sizes. Extend the metadata read to support reading all these element sizes. Signed-off-by: Kai Huang Signed-off-by: Isaku Yamahata --- arch/x86/virt/vmx/tdx/tdx.c | 59 +++++++++++++++++++++++++------------ arch/x86/virt/vmx/tdx/tdx.h | 2 -- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index eb208da4ff63..a19adc898df6 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -271,23 +271,35 @@ static int read_sys_metadata_field(u64 field_id, u64 *data) return 0; } -static int read_sys_metadata_field16(u64 field_id, - int offset, - void *stbuf) +/* Return the metadata field element size in bytes */ +static int get_metadata_field_bytes(u64 field_id) { - u16 *st_member = stbuf + offset; + /* + * TDX supports 8/16/32/64 bits metadata field element sizes. + * TDX module determines the metadata element size based on the + * "element size code" encoded in the field ID (see the comment + * of MD_FIELD_ID_ELE_SIZE_CODE macro for specific encodings). + */ + return 1 << MD_FIELD_ID_ELE_SIZE_CODE(field_id); +} + +static int stbuf_read_sys_metadata_field(u64 field_id, + int offset, + int bytes, + void *stbuf) +{ + void *st_member = stbuf + offset; u64 tmp; int ret; - if (WARN_ON_ONCE(MD_FIELD_ID_ELE_SIZE_CODE(field_id) != - MD_FIELD_ID_ELE_SIZE_16BIT)) + if (WARN_ON_ONCE(get_metadata_field_bytes(field_id) != bytes)) return -EINVAL; ret = read_sys_metadata_field(field_id, &tmp); if (ret) return ret; - *st_member = tmp; + memcpy(st_member, &tmp, bytes); return 0; } @@ -295,11 +307,30 @@ static int read_sys_metadata_field16(u64 field_id, 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) } + .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 i, ret; + + for (i = 0; i < nr_fields; i++) { + ret = stbuf_read_sys_metadata_field(fields[i].field_id, + fields[i].offset, + fields[i].size, + stbuf); + if (ret) + return ret; + } + + return 0; +} #define TD_SYSINFO_MAP_TDMR_INFO(_field_id, _member) \ TD_SYSINFO_MAP(_field_id, struct tdx_tdmr_sysinfo, _member) @@ -314,19 +345,9 @@ static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) TD_SYSINFO_MAP_TDMR_INFO(PAMT_2M_ENTRY_SIZE, pamt_entry_size[TDX_PS_2M]), TD_SYSINFO_MAP_TDMR_INFO(PAMT_1G_ENTRY_SIZE, pamt_entry_size[TDX_PS_1G]), }; - int ret; - int i; /* Populate 'tdmr_sysinfo' fields using the mapping structure above: */ - for (i = 0; i < ARRAY_SIZE(fields); i++) { - ret = read_sys_metadata_field16(fields[i].field_id, - fields[i].offset, - tdmr_sysinfo); - if (ret) - return ret; - } - - return 0; + return read_sys_metadata(fields, ARRAY_SIZE(fields), tdmr_sysinfo); } /* Calculate the actual TDMR size */ diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index b701f69485d3..4c32c8bf156a 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -53,8 +53,6 @@ #define MD_FIELD_ID_ELE_SIZE_CODE(_field_id) \ (((_field_id) & GENMASK_ULL(33, 32)) >> 32) -#define MD_FIELD_ID_ELE_SIZE_16BIT 1 - struct tdmr_reserved_area { u64 offset; u64 size;