From patchwork Sun Jun 16 12:01:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13699532 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 09AA818755D; Sun, 16 Jun 2024 12:01:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539307; cv=none; b=K/NkWEmvLXflpnG3N4BGdYcKU5nD+q1tjyHxTho9t4Y357k3RsrxeuSKUZpBR1JvrxcYDf1lmh5Rpb7WFc2f9pKRsONq6nPJhTY24RqR3zlytngLSLlF+h/gIurVxuuxnAQzX/D/fU7yw5BrPayBYfTG4bJflVFm2cxNbf58R1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539307; c=relaxed/simple; bh=AUrNPcJyqRQvgHQBUEB1pO8rmhlwIqwTYUgZYCPiSsA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mqAUjqSVykCAmRhaq91zb7YFS8wEMadjcFkc4GAZ8CYwfwMVZZCQNBVxP0cEe5qHYYzMmXZL8Mx8y9nTyIvj+hdaTT2SF5spU5OdRmMxOKCg8bZdlpLnuy1hu43ruBsHcWHmtS0HF/3FU1LmzwXxko7bSs2uiD0+tv22vX5tKfc= 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=ibiodJMd; arc=none smtp.client-ip=198.175.65.12 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="ibiodJMd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718539306; x=1750075306; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AUrNPcJyqRQvgHQBUEB1pO8rmhlwIqwTYUgZYCPiSsA=; b=ibiodJMdW9KlrYIqYWH8n+flustbuOUiIZKfsVruQy+MlIjeC5v9lPmq d+ullKopyULK5H88xX/Ck/o4be0x7tXi4m8Ci/B56inxTsX74S45gXl9S u9hlaq0kb7nEn+EG7K0W48jQ1yupNRx2uecdIsk21g/0h8nFxIASuPvMk TgFBlTgULqG/geVRow4if9bmOAxx0nhx25PPwUNld0PyWH0tWCn6DMH/w kRXr4/4F/QBpepill3EsconS6SSVnGCxv6NigRRfex85Te531qZ4PT56f SYRKR0ygqhOUXppeaWPcgK0ACXDs+zRyCVe/nWu5q6HzwZJH7RCoOhhqy Q==; X-CSE-ConnectionGUID: BEpUdVhnRbCH4RiyL6mjlg== X-CSE-MsgGUID: KuTxJhKDRSSATt9bNfm0zQ== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="26799989" X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="26799989" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:01:46 -0700 X-CSE-ConnectionGUID: vDKerc2+SnKAnStCWTV9VA== X-CSE-MsgGUID: Clva5zLgTAeh3xb0tJVV8w== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="46055802" Received: from mgoodin-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.124.223.226]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:01:42 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, dan.j.williams@intel.com, kirill.shutemov@linux.intel.com, rick.p.edgecombe@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, isaku.yamahata@intel.com, binbin.wu@linux.intel.com, kai.huang@intel.com Subject: [PATCH 1/9] x86/virt/tdx: Rename _offset to _member for TD_SYSINFO_MAP() macro Date: Mon, 17 Jun 2024 00:01:11 +1200 Message-ID: <8e48327381647068355d43c672b34961f2b88971.1718538552.git.kai.huang@intel.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 TD_SYSINFO_MAP() macro actually takes the member of the 'struct tdx_tdmr_sysinfo' as the second argument and uses the offsetof() to calculate the offset for that member. Rename the macro argument _offset to _member to reflect this. Signed-off-by: Kai Huang Reviewed-by: Kirill A. Shutemov Reviewed-by: Binbin Wu --- arch/x86/virt/vmx/tdx/tdx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 4e2b2e2ac9f9..fbde24ea3b3e 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -296,9 +296,9 @@ struct field_mapping { int offset; }; -#define TD_SYSINFO_MAP(_field_id, _offset) \ +#define TD_SYSINFO_MAP(_field_id, _member) \ { .field_id = MD_FIELD_ID_##_field_id, \ - .offset = offsetof(struct tdx_tdmr_sysinfo, _offset) } + .offset = offsetof(struct tdx_tdmr_sysinfo, _member) } /* Map TD_SYSINFO fields into 'struct tdx_tdmr_sysinfo': */ static const struct field_mapping fields[] = { From patchwork Sun Jun 16 12:01:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13699533 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 D11E8188CCA; Sun, 16 Jun 2024 12:01:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539311; cv=none; b=pzy87pn66yYMHjekT6f2A3kjMfte2Aln6of5xXXaKwJoqSNgB8wMHwGoZONAtJ+pMc0mzO63OwZqRDRljwMcL/Iy24gsxjBUTxZwJsNzS5Nc3lNZiYPPBiLiMM0K/p3xyIJK7r7pgwDK7bO2vwl2UzoMg3SwIVeo4dv/aqrAEiM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539311; c=relaxed/simple; bh=FYdM0vd/yZti1cw7i+TA3IGlNMrYUsiVBS/rW4QPNeE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d0ee2HIGQu2tJwLLZqjpLju24JZOaeRCzQcMSqvC5fI6YuBcPwMviLMyuFFRwwkQu8ia6G3abRrRBHqaSlzi+ujNqq6YrwBhq0V9XkNkB6eP7wSXwyYKDde8XdV4GsA0cmOADG4eGX/oh7G3z0RY9ysQPUSFBW8A3G6RnRoZdu0= 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=H8IO3eAj; arc=none smtp.client-ip=198.175.65.12 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="H8IO3eAj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718539310; x=1750075310; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=FYdM0vd/yZti1cw7i+TA3IGlNMrYUsiVBS/rW4QPNeE=; b=H8IO3eAjl0OZ12RqfqRKiQf5850LwiL8Q7iBetb/d8EwyXvSFanDfdpX iN+ThogMJkkYecLTGFwf+r9LwC4j57v8tdf8J5b5Vihhln43rIopXPHuq yudSvToMsH/ONKMFGQo6QRGEV9crdcRvDlLlnzG65gqpNPgC6GYFyG1i1 /aid52Z/ga04DV+UL59diujcMfi7Iy2fDYu0lsk0MD8pBSL2f7/1bnI2v tYgEkdvV0axL0CGvDIa3N3MDX6o6Q602j7iLFGZsrUPpOL4OijLOqwOVb sBWvU7LAM1hO4vI7K837QCMb2rOXXc7889ixEyEpNV46g5gznJZWc23uY Q==; X-CSE-ConnectionGUID: vZsXo6QCQY+3G38vj4Ifbw== X-CSE-MsgGUID: qvGrlvrHQqu/P0eJVheoiQ== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="26799997" X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="26799997" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:01:49 -0700 X-CSE-ConnectionGUID: wnDCm0NFSFCF6R1etM+Gtw== X-CSE-MsgGUID: UykEkbVgQ06B/8x2lLREUw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="46055813" Received: from mgoodin-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.124.223.226]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:01:46 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, dan.j.williams@intel.com, kirill.shutemov@linux.intel.com, rick.p.edgecombe@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, isaku.yamahata@intel.com, binbin.wu@linux.intel.com, kai.huang@intel.com Subject: [PATCH 2/9] x86/virt/tdx: Unbind global metadata read with 'struct tdx_tdmr_sysinfo' Date: Mon, 17 Jun 2024 00:01:12 +1200 Message-ID: <43c646d35088a0bada9fbbf8b731a7e4a44b22c0.1718538552.git.kai.huang@intel.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The TDX module provides a set of "global metadata fields". They report things like TDX module version, supported features, and fields related to create/run TDX guests and so on. For now the kernel only reads "TD Memory Region" (TDMR) related global metadata fields to a 'struct tdx_tdmr_sysinfo' for initializing the TDX module, and the metadata reading code can only work with that structure. Future changes will need to read other metadata fields that don't make sense to populate to the "struct tdx_tdmr_sysinfo". It's essential to provide a generic metadata read infrastructure which is not bound to any specific structure. To start providing such infrastructure, unbind the metadata reading code with the 'struct tdx_tdmr_sysinfo'. Note the kernel has a helper macro, TD_SYSINFO_MAP(), for marshaling the metadata into the 'struct tdx_tdmr_sysinfo', and currently the macro hardcodes the structure name. As part of unbinding the metadata reading code with 'struct tdx_tdmr_sysinfo', it is extended to accept different structures. Unfortunately, this will result in the usage of TD_SYSINFO_MAP() for populating 'struct tdx_tdmr_sysinfo' to be changed to use the structure name explicitly for each structure member and make the code longer. Add a wrapper macro which hides the 'struct tdx_tdmr_sysinfo' internally to make the code shorter thus better readability. Signed-off-by: Kai Huang Reviewed-by: Kirill A. Shutemov Reviewed-by: Binbin Wu --- arch/x86/virt/vmx/tdx/tdx.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index fbde24ea3b3e..854312e97eff 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -272,9 +272,9 @@ static int read_sys_metadata_field(u64 field_id, u64 *data) static int read_sys_metadata_field16(u64 field_id, int offset, - struct tdx_tdmr_sysinfo *ts) + void *stbuf) { - u16 *ts_member = ((void *)ts) + offset; + u16 *st_member = stbuf + offset; u64 tmp; int ret; @@ -286,7 +286,7 @@ static int read_sys_metadata_field16(u64 field_id, if (ret) return ret; - *ts_member = tmp; + *st_member = tmp; return 0; } @@ -296,17 +296,20 @@ struct field_mapping { int offset; }; -#define TD_SYSINFO_MAP(_field_id, _member) \ - { .field_id = MD_FIELD_ID_##_field_id, \ - .offset = offsetof(struct tdx_tdmr_sysinfo, _member) } +#define TD_SYSINFO_MAP(_field_id, _struct, _member) \ + { .field_id = MD_FIELD_ID_##_field_id, \ + .offset = offsetof(_struct, _member) } + +#define TD_SYSINFO_MAP_TDMR_INFO(_field_id, _member) \ + TD_SYSINFO_MAP(_field_id, struct tdx_tdmr_sysinfo, _member) /* Map TD_SYSINFO fields into 'struct tdx_tdmr_sysinfo': */ static const struct field_mapping fields[] = { - TD_SYSINFO_MAP(MAX_TDMRS, max_tdmrs), - TD_SYSINFO_MAP(MAX_RESERVED_PER_TDMR, max_reserved_per_tdmr), - TD_SYSINFO_MAP(PAMT_4K_ENTRY_SIZE, pamt_entry_size[TDX_PS_4K]), - TD_SYSINFO_MAP(PAMT_2M_ENTRY_SIZE, pamt_entry_size[TDX_PS_2M]), - TD_SYSINFO_MAP(PAMT_1G_ENTRY_SIZE, pamt_entry_size[TDX_PS_1G]), + 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]), + 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]), }; static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) From patchwork Sun Jun 16 12:01:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13699534 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 A647818C336; Sun, 16 Jun 2024 12:01:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539315; cv=none; b=ZdJvaxBhm/V/WVRGoP0hj+im548XCDkuYL0ujGJ2xv1ODAthfOc3pgka3Fm/VGmbNqen0QofVlICGxB0A2af9eGpex3CMkSaKyhxvsY3/pa0I7+A5AagjFb1lz8B5GrVT+pkAmbeWrVQRsuGmRnuhHQrB8kfxOOn2ndC4a+gDYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539315; c=relaxed/simple; bh=s8I8gURAQHYQFee7FboDTILPxJxSD3iizKPdO6AZT+w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AjnodHKL3ULAbw07JgzrYiFv83BGDIg7mhyfFB+/HwTDGT00y5C987HOzd22gwHfJFd+XwaGXWYy6bmtOSKMS2lX0Dpu+2o+5p9gGg3Gi3WYNOHaLG+hKCMlZ904b7X2zcb8d1F4i6rlTod3GhgR9Dk2Ou3Ssa1/gsxPfyFkpJQ= 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=AsGVBrZA; arc=none smtp.client-ip=198.175.65.12 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="AsGVBrZA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718539314; x=1750075314; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=s8I8gURAQHYQFee7FboDTILPxJxSD3iizKPdO6AZT+w=; b=AsGVBrZA7XJ6V0Cq51daV9ulFhabIrwf5ekKlw2Dz4mRQDIbmfbXpLJl gYiJ4h/AoJEZI24iY66rtBNbnlCes/aI+RPGIAyEnYb0aMfNrZb9ri+Hl tBeKzwyteQBbJ7n4lYwfaSoQ0vSQDba9NCm+0xhaPmrR2x7u0INEVCeyG kHYk/daGRLYETv9n2ELFNvAhRx75xBDti6gDa2uPihnhTPBH6e2r33gZ6 4p+LtQ5W4gPso0hR6/KJseOJe0+siOhiOGEzXqksQo4YgKMJXwjgAxXdp MrIba6M/xkG/9jqm0ulftQ0TtdHgdVNAXXyjovTDu0EG0jAT2jZukONhU Q==; X-CSE-ConnectionGUID: qDomohBWRgCwv6vMwHqYfA== X-CSE-MsgGUID: 616XbCT9QEKv/XOyYWmgVw== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="26800006" X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="26800006" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:01:53 -0700 X-CSE-ConnectionGUID: HlxInbRpQfuTPlsExV5htw== X-CSE-MsgGUID: uDrVl7YPTTOdMsP5302M9A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="46055832" Received: from mgoodin-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.124.223.226]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:01:50 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, dan.j.williams@intel.com, kirill.shutemov@linux.intel.com, rick.p.edgecombe@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, isaku.yamahata@intel.com, binbin.wu@linux.intel.com, kai.huang@intel.com Subject: [PATCH 3/9] x86/virt/tdx: Support global metadata read for all element sizes Date: Mon, 17 Jun 2024 00:01:13 +1200 Message-ID: <210f7747058e01c4d2ed683660a4cb18c5d88440.1718538552.git.kai.huang@intel.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The TDX module provides "global metadata fields" for software to query. Each metadata field is accessible by a unique "metadata field ID". TDX supports 8/16/32/64 bits metadata element sizes. The size of each metadata field is encoded in its associated metadata field ID. For now the kernel only reads "TD Memory Region" (TDMR) related global metadata fields for module initialization. All these metadata fields are 16-bit, and the kernel only supports reading 16-bit fields. Future changes will need to read more metadata fields with other element sizes. To resolve this once for all, extend the existing metadata reading code to support reading all element sizes. Signed-off-by: Kai Huang Reviewed-by: Kirill A. Shutemov --- arch/x86/virt/vmx/tdx/tdx.c | 29 ++++++++++++++++------------- arch/x86/virt/vmx/tdx/tdx.h | 3 ++- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 854312e97eff..4392e82a9bcb 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -270,23 +270,25 @@ 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) +/* + * Read one global metadata field and store the data to a location of a + * given buffer specified by the offset and size (in bytes). + */ +static int stbuf_read_sysmd_field(u64 field_id, void *stbuf, int offset, + int bytes) { - u16 *st_member = stbuf + offset; + 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(MD_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; } @@ -294,11 +296,13 @@ 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) } +#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)) } #define TD_SYSINFO_MAP_TDMR_INFO(_field_id, _member) \ TD_SYSINFO_MAP(_field_id, struct tdx_tdmr_sysinfo, _member) @@ -319,9 +323,8 @@ static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) /* 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); + ret = stbuf_read_sysmd_field(fields[i].field_id, tdmr_sysinfo, + fields[i].offset, fields[i].size); if (ret) return ret; } diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index b701f69485d3..812943516946 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -53,7 +53,8 @@ #define MD_FIELD_ID_ELE_SIZE_CODE(_field_id) \ (((_field_id) & GENMASK_ULL(33, 32)) >> 32) -#define MD_FIELD_ID_ELE_SIZE_16BIT 1 +#define MD_FIELD_BYTES(_field_id) \ + (1 << MD_FIELD_ID_ELE_SIZE_CODE(_field_id)) struct tdmr_reserved_area { u64 offset; From patchwork Sun Jun 16 12:01:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13699535 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 70C5B18F2C8; Sun, 16 Jun 2024 12:01:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539318; cv=none; b=ZbZpZTsi4cs52vYWu7qfOkqVePXeKTyhVn0yI4fkvu3UMTm7ze2FNAOAGHPk6xE8BL4DcF7v/NboeYWTuNQYs+nKaCMufl822jU8XdDxeWSjp4yttrds5uqMhJrAKk5lhhsv0YbjM4T1pUddRFfoxUBAHojpd1kkDleO5YvIWbI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539318; c=relaxed/simple; bh=GkDMHFREUSNasjUMb5V5YXtvJUS3nNxD/1yXJSwUY/A=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Fc4Cir53CiM/JqOPIQu2nWkFkZ4Y4NU/IctESpqRO9mW2gfkjsTtaiEIbXMMGGTlWAsCF03eIgdRBWjrp7E+BlOBhCRnNLCkfO3ZW7El2PP+K7M1wRemGFRTEB3D+C+Pw5NN/ONicueIilqj+GOeN2AQz56AV8lpWoEIPMCbF30= 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=SMZKnXCd; arc=none smtp.client-ip=198.175.65.12 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="SMZKnXCd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718539317; x=1750075317; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=GkDMHFREUSNasjUMb5V5YXtvJUS3nNxD/1yXJSwUY/A=; b=SMZKnXCd6zRlSuuSlLi1SIBsJCNFYPf6dNRtcc94mDJJQCp+AVl1LsbN EsbadkcWH/B2VZpvnoOgA4kDbpibOsoo9nuCY/mQusbSsYtDWBtNZiz9d 58Nt+pFGpU0mrkZz15BhfwkgR5MVM6wcyr0MtCLTzpn0TW64ddw8nGBt4 m5qVuYliiMv0/WP2IGiT5d9yJehxhdXSn+sE+z1qhAvy62TUC0ZrGwEa4 JbNPZqNO63E9zAw5kG3UYR11S7OGm0S9vdQ62O/8aBJy64v+wbXZm1ws7 oUNK8CC6/7d6jmjuCr94FAy1H1gHbNZGE4LcOck+pH4F0KzzOZ2uW+TV3 w==; X-CSE-ConnectionGUID: KEfVdNRKTxGHajUklKRCbg== X-CSE-MsgGUID: HWs2Aat1T3m693nt5Q6Qgw== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="26800015" X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="26800015" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:01:57 -0700 X-CSE-ConnectionGUID: nyGf/4kKSYue1fTMWZCKrQ== X-CSE-MsgGUID: fjieSrYpRc6y5rrBztb9SQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="46055849" Received: from mgoodin-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.124.223.226]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:01:54 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, dan.j.williams@intel.com, kirill.shutemov@linux.intel.com, rick.p.edgecombe@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, isaku.yamahata@intel.com, binbin.wu@linux.intel.com, kai.huang@intel.com Subject: [PATCH 4/9] x86/virt/tdx: Abstract reading multiple global metadata fields as a helper Date: Mon, 17 Jun 2024 00:01:14 +1200 Message-ID: X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For now the kernel only reads "TD Memory Region" (TDMR) related global metadata fields to a 'struct tdx_tdmr_sysinfo' for initializing the TDX module. Future changes will need to read other metadata fields that don't make sense to populate to the "struct tdx_tdmr_sysinfo". Now the code in get_tdx_tdmr_sysinfo() to read multiple global metadata fields is not bound to the 'struct tdx_tdmr_sysinfo', and can support reading all metadata element sizes. Abstract this code as a helper for future use. Signed-off-by: Kai Huang --- arch/x86/virt/vmx/tdx/tdx.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 4392e82a9bcb..c68fbaf4aa15 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -304,6 +304,21 @@ struct field_mapping { .offset = offsetof(_struct, _member), \ .size = sizeof(typeof(((_struct *)0)->_member)) } +static int stbuf_read_sysmd_multi(const struct field_mapping *fields, + int nr_fields, void *stbuf) +{ + int i, ret; + + for (i = 0; i < nr_fields; i++) { + ret = stbuf_read_sysmd_field(fields[i].field_id, stbuf, + fields[i].offset, fields[i].size); + 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) @@ -318,18 +333,8 @@ static const struct field_mapping fields[] = { static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) { - int ret; - int i; - /* Populate 'tdmr_sysinfo' fields using the mapping structure above: */ - for (i = 0; i < ARRAY_SIZE(fields); i++) { - ret = stbuf_read_sysmd_field(fields[i].field_id, tdmr_sysinfo, - fields[i].offset, fields[i].size); - if (ret) - return ret; - } - - return 0; + return stbuf_read_sysmd_multi(fields, ARRAY_SIZE(fields), tdmr_sysinfo); } /* Calculate the actual TDMR size */ From patchwork Sun Jun 16 12:01:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13699536 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 25C4218F2F9; Sun, 16 Jun 2024 12:02:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539322; cv=none; b=NUhzITjfaFxUdWQi4L1CtsG0JZ4cf5zSW31zDHsC9722zufdJsXhgrdGKd9oittMt8QglfhyuuqgsQ2fXYrrizg4cMlWE2za95wi3zkl7Ez45qNxpa6fx35UTCN87YNq2VxVZNB139vWzIFZIUnZZYq6yTqDpDTWRCrOHmidwkM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539322; c=relaxed/simple; bh=gEEav5uPFLohYv3SYSnBxkTTuRP6BswSM90fiXmCWmE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=I3wAYq/2yqVh6VMxW5dlf8U7swNJbyb+um0ryzuc0+peXunIENcD8Ht6YY/GX3dpRz8LHZWUJobh5bGXsoQ+xgFc0QIsKZ+2hh7pGDNWrwQVKvE5l6JpcOQ0j8jWRIGS4Lapgtn7b/bCcMh4EGdsOuM72dGL8dSzODW1DB/Z7wI= 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=l1zBB7Dw; arc=none smtp.client-ip=198.175.65.12 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="l1zBB7Dw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718539321; x=1750075321; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gEEav5uPFLohYv3SYSnBxkTTuRP6BswSM90fiXmCWmE=; b=l1zBB7DwJcpvSUtUVUzY9L0g9UKUwvilqow5sYTJVc2YDvovxRHzOqRv Usb+NP1O1tJGu/kBeflvFj5MNeg2bvUWt5EY1es7ToCcS/FvrjY24UN08 PU8uuOAMO0oua5FzNu9GT9G/Q9cuSM1GGAcNLgfWKjw8utUFZ6oEiE1Sy s5NrJKcs75rl7zHD5y3cMGHl+/q7TKBNCaX10fb+/7eUvpQG37rgfwwK4 73y27NEmLZuJt3HuaRoKTPKB13/RYGzkXfLxNsKN+GnoqRxdbKlrWrs4W xhLFFwfD3ROHgx0v+AJAWyKtm9Qb9beo5xkZbHIUAGgDQ/8TIIi3fdCcw w==; X-CSE-ConnectionGUID: nCm3OGSjSLu1NaSnKFZK+g== X-CSE-MsgGUID: D5BnNj8WROWr5qmF0HjFvg== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="26800023" X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="26800023" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:02:01 -0700 X-CSE-ConnectionGUID: JXC2GztURO+NdtZxINfX6g== X-CSE-MsgGUID: 3yIsSOPNR3KFY/B0A0hOFA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="46055868" Received: from mgoodin-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.124.223.226]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:01:57 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, dan.j.williams@intel.com, kirill.shutemov@linux.intel.com, rick.p.edgecombe@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, isaku.yamahata@intel.com, binbin.wu@linux.intel.com, kai.huang@intel.com Subject: [PATCH 5/9] x86/virt/tdx: Move field mapping table of getting TDMR info to function local Date: Mon, 17 Jun 2024 00:01:15 +1200 Message-ID: X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 For now the kernel only reads "TD Memory Region" (TDMR) related global metadata fields to a 'struct tdx_tdmr_sysinfo' for initializing the TDX module. The kernel populates the relevant metadata fields into the structure using a "field mapping table" of metadata field IDs and the structure members. Currently the scope of this "field mapping table" is the entire C file. Future changes will need to read more global metadata fields that will be organized in other structures and use this kind of field mapping tables for other structures too. Move the field mapping table to the function local to limit its scope so that the same name can also be used by other functions. Signed-off-by: Kai Huang Reviewed-by: Nikolay Borisov --- arch/x86/virt/vmx/tdx/tdx.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index c68fbaf4aa15..fad42014ca37 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -322,17 +322,17 @@ static int stbuf_read_sysmd_multi(const struct field_mapping *fields, #define TD_SYSINFO_MAP_TDMR_INFO(_field_id, _member) \ TD_SYSINFO_MAP(_field_id, struct tdx_tdmr_sysinfo, _member) -/* Map TD_SYSINFO fields into 'struct tdx_tdmr_sysinfo': */ -static const struct 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]), - 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]), -}; - static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) { + /* Map TD_SYSINFO fields into 'struct tdx_tdmr_sysinfo': */ + static const struct 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]), + 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]), + }; + /* Populate 'tdmr_sysinfo' fields using the mapping structure above: */ return stbuf_read_sysmd_multi(fields, ARRAY_SIZE(fields), tdmr_sysinfo); } From patchwork Sun Jun 16 12:01:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13699537 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 0CF1F18FC93; Sun, 16 Jun 2024 12:02:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539326; cv=none; b=Nidj03adjZTPYS0dlW9KMumLTQQPVrv7LEEZw9/2qs0Ru/TxpCOu8pIZL0QMY+BK10fRi9+7k2Sl6MpOZ4KC3+QtuYowPar3scz8h35yWHt31tH6MTl2MCu/l3KiCBZrW/iS3gO3x//iyxuv7BXNxWhT13L4cIzX9dSLe/m7NAs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539326; c=relaxed/simple; bh=DBhTRbapgojfCSzdbMaFFi64591mOutWYsEqjKoRI9w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dV22udbGpINa46XgjPip3i94+IC5S64iZ1onV0vrapydMiDgtARksK4PUKLB80+NqMfohn6oTyE4R+yHSPVs34NWNIoS4p/L5U14DxHJZLks3nZUEHylxagi63du7KwGkzHFkvY8wFQRulKCS4HJZNuTj2m2Lm5dD02/EarCnx4= 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=nAOwGN3P; arc=none smtp.client-ip=198.175.65.12 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="nAOwGN3P" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718539325; x=1750075325; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DBhTRbapgojfCSzdbMaFFi64591mOutWYsEqjKoRI9w=; b=nAOwGN3P19jz6dZgSNsAysB4vN0eqPN/NlveNVd1yKK5mmpU7s2IyRd+ Te4KgarEPiYH0Bllt02Ol1bfi7l81xVfGOQaZvvhvvReeirt+82aenhqz RQEaQ0DhOpeW4JoeqWBVUTXwNxw81x3oWqd1+g5apA8EUu1GYF3wfwMNr 3HihhF4R5NJVAAJajoSDvSUmVcTHN6BPOA7OXT0iu1dJWB1iWmPH84uv2 SoDUU3NmXNl8Gn9V9MTWonwBHFCWJgqqst7ZE/1aGnutaIZRx8Z14EdM6 Hk6RVLspwUbZvqxzqMXBpA58/5MFq5+seWgL8AZPkOD1dqu5T1BuhdfZW w==; X-CSE-ConnectionGUID: KG6KA87yTt2pFlWYWORvUw== X-CSE-MsgGUID: iIZWoODyQzGH8Eb25tS3hQ== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="26800035" X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="26800035" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:02:05 -0700 X-CSE-ConnectionGUID: tT3MKe9HSTqZNzCqT9CX4Q== X-CSE-MsgGUID: atuuzNGMQQio/V51WeGHMw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="46055885" Received: from mgoodin-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.124.223.226]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:02:01 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, dan.j.williams@intel.com, kirill.shutemov@linux.intel.com, rick.p.edgecombe@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, isaku.yamahata@intel.com, binbin.wu@linux.intel.com, kai.huang@intel.com Subject: [PATCH 6/9] x86/virt/tdx: Start to track all global metadata in one structure Date: Mon, 17 Jun 2024 00:01:16 +1200 Message-ID: <9759a946d7861821bf45c6bc73c9f596235087bc.1718538552.git.kai.huang@intel.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The TDX module provides a set of "global metadata fields". They report things like TDX module version, supported features, and fields related to create/run TDX guests and so on. Currently the kernel only reads "TD Memory Region" (TDMR) related fields for module initialization. There are immediate needs which require the TDX module initialization to read more global metadata including module version, supported features and "Convertible Memory Regions" (CMRs). Also, KVM will need to read more metadata fields to support baseline TDX guests. In the longer term, other TDX features like TDX Connect (which supports assigning trusted IO devices to TDX guest) may also require other kernel components such as pci/vt-d to access global metadata. To meet all those requirements, the idea is the TDX host core-kernel to to provide a centralized, canonical, and read-only structure for the global metadata that comes out from the TDX module for all kernel components to use. As the first step, introduce a new 'struct tdx_sysinfo' to track all global metadata fields. TDX categories global metadata fields into different "Class"es. E.g., the current TDMR related fields are under class "TDMR Info". Instead of making 'struct tdx_sysinfo' a plain structure to contain all metadata fields, organize them in smaller structures based on the "Class". This allows those metadata fields to be used in finer granularity thus makes the code more clear. E.g., the current construct_tdmr() can just take the structure which contains "TDMR Info" metadata fields. Start with moving 'struct tdx_tdmr_sysinfo' to 'struct tdx_sysinfo', and rename 'struct tdx_tdmr_sysinfo' to 'struct tdx_sysinfo_tdmr_info' to make it consistent with the "class name". Add a new function get_tdx_sysinfo() as the place to read all metadata fields, and call it at the beginning of init_tdx_module(). Move the existing get_tdx_tdmr_sysinfo() to get_tdx_sysinfo(). Note there is a functional change: get_tdx_tdmr_sysinfo() is moved from after build_tdx_memlist() to before it, but it is fine to do so. Signed-off-by: Kai Huang --- arch/x86/virt/vmx/tdx/tdx.c | 29 +++++++++++++++++------------ arch/x86/virt/vmx/tdx/tdx.h | 32 +++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index fad42014ca37..4683884efcc6 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -320,11 +320,11 @@ static int stbuf_read_sysmd_multi(const struct field_mapping *fields, } #define TD_SYSINFO_MAP_TDMR_INFO(_field_id, _member) \ - TD_SYSINFO_MAP(_field_id, struct tdx_tdmr_sysinfo, _member) + TD_SYSINFO_MAP(_field_id, struct tdx_sysinfo_tdmr_info, _member) -static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) +static int get_tdx_tdmr_sysinfo(struct tdx_sysinfo_tdmr_info *tdmr_sysinfo) { - /* Map TD_SYSINFO fields into 'struct tdx_tdmr_sysinfo': */ + /* Map TD_SYSINFO fields into 'struct tdx_sysinfo_tdmr_info': */ static const struct 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), @@ -337,6 +337,11 @@ static int get_tdx_tdmr_sysinfo(struct tdx_tdmr_sysinfo *tdmr_sysinfo) return stbuf_read_sysmd_multi(fields, ARRAY_SIZE(fields), tdmr_sysinfo); } +static int get_tdx_sysinfo(struct tdx_sysinfo *sysinfo) +{ + return get_tdx_tdmr_sysinfo(&sysinfo->tdmr_info); +} + /* Calculate the actual TDMR size */ static int tdmr_size_single(u16 max_reserved_per_tdmr) { @@ -353,7 +358,7 @@ static int tdmr_size_single(u16 max_reserved_per_tdmr) } static int alloc_tdmr_list(struct tdmr_info_list *tdmr_list, - struct tdx_tdmr_sysinfo *tdmr_sysinfo) + struct tdx_sysinfo_tdmr_info *tdmr_sysinfo) { size_t tdmr_sz, tdmr_array_sz; void *tdmr_array; @@ -936,7 +941,7 @@ static int tdmrs_populate_rsvd_areas_all(struct tdmr_info_list *tdmr_list, */ static int construct_tdmrs(struct list_head *tmb_list, struct tdmr_info_list *tdmr_list, - struct tdx_tdmr_sysinfo *tdmr_sysinfo) + struct tdx_sysinfo_tdmr_info *tdmr_sysinfo) { int ret; @@ -1109,9 +1114,13 @@ static int init_tdmrs(struct tdmr_info_list *tdmr_list) static int init_tdx_module(void) { - struct tdx_tdmr_sysinfo tdmr_sysinfo; + struct tdx_sysinfo sysinfo; int ret; + ret = get_tdx_sysinfo(&sysinfo); + if (ret) + return ret; + /* * To keep things simple, assume that all TDX-protected memory * will come from the page allocator. Make sure all pages in the @@ -1128,17 +1137,13 @@ static int init_tdx_module(void) if (ret) goto out_put_tdxmem; - ret = get_tdx_tdmr_sysinfo(&tdmr_sysinfo); - if (ret) - goto err_free_tdxmem; - /* Allocate enough space for constructing TDMRs */ - ret = alloc_tdmr_list(&tdx_tdmr_list, &tdmr_sysinfo); + ret = alloc_tdmr_list(&tdx_tdmr_list, &sysinfo.tdmr_info); if (ret) goto err_free_tdxmem; /* Cover all TDX-usable memory regions in TDMRs */ - ret = construct_tdmrs(&tdx_memlist, &tdx_tdmr_list, &tdmr_sysinfo); + ret = construct_tdmrs(&tdx_memlist, &tdx_tdmr_list, &sysinfo.tdmr_info); if (ret) goto err_free_tdmrs; diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index 812943516946..6b61dc67b0af 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -100,13 +100,6 @@ struct tdx_memblock { int nid; }; -/* "TDMR info" part of "Global Scope Metadata" for constructing TDMRs */ -struct tdx_tdmr_sysinfo { - u16 max_tdmrs; - u16 max_reserved_per_tdmr; - u16 pamt_entry_size[TDX_PS_NR]; -}; - /* Warn if kernel has less than TDMR_NR_WARN TDMRs after allocation */ #define TDMR_NR_WARN 4 @@ -119,4 +112,29 @@ struct tdmr_info_list { int max_tdmrs; /* How many 'tdmr_info's are allocated */ }; +/* + * Kernel-defined structures to contain "Global Scope Metadata". + * + * TDX global metadata fields are categorized by "Class". See the + * 'global_metadata.json' in the "TDX 1.5 ABI Definitions". + * + * 'struct tdx_sysinfo' is the main structure to contain all metadata + * used by the kernel. It contains sub-structures with each reflecting + * the "Class" in the 'global_metadata.json'. + * + * Note not all metadata fields in each class are defined, only those + * used by the kernel are. + */ + +/* Class "TDMR Info" */ +struct tdx_sysinfo_tdmr_info { + u16 max_tdmrs; + u16 max_reserved_per_tdmr; + u16 pamt_entry_size[TDX_PS_NR]; +}; + +struct tdx_sysinfo { + struct tdx_sysinfo_tdmr_info tdmr_info; +}; + #endif From patchwork Sun Jun 16 12:01:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13699538 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 2125918FDB8; Sun, 16 Jun 2024 12:02:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539330; cv=none; b=CEYq0VG9WF60W4VGTTf1SFDI4TRrBbh7k6kDCMcNmcTQi2yhGQa4C/myQ6Zr13iep8Rds7JBrElyjDDBVgL93P5Td3sBNG+TXw7tNhIA8ipHfWDqE208zVVVUsB0+jDJ3kRIOxLu8H4F8jFdB5Q/bj47TGQ17AlCo4uA8RmDOac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539330; c=relaxed/simple; bh=NPmj8dFgrE18hffcZbQUkVcIJfWHNVXzEZiodSQgNNI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FiNJLgSd3N8mibu25/lx2fBNa9zsZG+jrliLldyHc7qY90KZEW4UO1uZpcr0is6uMZorhYlxfftCcT2BPI31IvvX+KuYk7Obc3hi3+iDgLbC+Y8KkYv/3uUw4rCtvw0f1kNni8o1lPJ+hzoqvPVmpv5TeRv+ALDLJdtW7sE+KoM= 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=I6xWRNnw; arc=none smtp.client-ip=198.175.65.12 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="I6xWRNnw" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718539329; x=1750075329; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=NPmj8dFgrE18hffcZbQUkVcIJfWHNVXzEZiodSQgNNI=; b=I6xWRNnw1r1qUc3oIVjPohXxf6jM8CecxVK7maHjwoi3lTLDeoJ6gl8Q 7nPrfDCW8lMafntY4eQD7wYKEyNx81ge/H4buLahV8ELfC7HkVkZH6mqa NfF5gJhWiJseNQKuz7Ol/3/Hnz6o4ULmzwHJVQdK5qSWA0AyIY/qqQPEF Zlq1O4yHukF/jCwAh4IMd1F/cS+qB8rPO9b4UNsT23cnbFj5fqHnXwkTE GgYUJ5Fv0tWWHZZquxojQxQMEboZ1bDBDICKYaVHaAO27okL/JTbcyctf /5U6YMvPpkDKaf5E+vnKxoPgFpjYEFw/hE8k3ZgPMCGEayyPtGEbS9OMZ w==; X-CSE-ConnectionGUID: ZA4MHHRKSN+qHVBvhEZrEQ== X-CSE-MsgGUID: vCXrHuxSQ/Wy6BidYnKpvA== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="26800052" X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="26800052" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:02:09 -0700 X-CSE-ConnectionGUID: gfJxBBPCTx+aGLps5pVu5w== X-CSE-MsgGUID: jx0452ZCTV2I4mLjBDQG5A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="46055906" Received: from mgoodin-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.124.223.226]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:02:05 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, dan.j.williams@intel.com, kirill.shutemov@linux.intel.com, rick.p.edgecombe@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, isaku.yamahata@intel.com, binbin.wu@linux.intel.com, kai.huang@intel.com Subject: [PATCH 7/9] x86/virt/tdx: Print TDX module basic information Date: Mon, 17 Jun 2024 00:01:17 +1200 Message-ID: X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Currently the kernel doesn't print any information regarding the TDX module itself, e.g. module version. Printing such information is not mandatory for initializing the TDX module, but in practice such information is useful, especially to the developers. For instance, there are couple of use cases for dumping module basic information: 1) When something goes wrong around using TDX, the information like TDX module version, supported features etc could be helpful [1][2]. 2) For Linux, when the user wants to update the TDX module, one needs to replace the old module in a specific location in the EFI partition with the new one so that after reboot the BIOS can load it. However, after kernel boots, currently the user has no way to verify it is indeed the new module that gets loaded and initialized (e.g., error could happen when replacing the old module). With the module version dumped the user can verify this easily. So dump the basic TDX module information: - TDX module type: Debug or Production. - TDX_FEATURES0: Supported TDX features. - TDX module version, and the build date. And dump the information right after reading global metadata, so that this information is printed no matter whether module initialization fails or not. The actual dmesg will look like: virt/tdx: Production module. virt/tdx: TDX_FEATURES0: 0xfbf virt/tdx: Module version: 1.5.00.00.0481, build_date: 20230323 Link: https://lore.kernel.org/lkml/e2d844ad-182a-4fc0-a06a-d609c9cbef74@suse.com/T/#m352829aedf6680d4628c7e40dc40b332eda93355 [1] Link: https://lore.kernel.org/lkml/e2d844ad-182a-4fc0-a06a-d609c9cbef74@suse.com/T/#m351ebcbc006d2e5bc3e7650206a087cb2708d451 [2] Signed-off-by: Kai Huang --- arch/x86/virt/vmx/tdx/tdx.c | 67 +++++++++++++++++++++++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.h | 33 +++++++++++++++++- 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 4683884efcc6..ced40e3b516e 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -319,6 +319,61 @@ static int stbuf_read_sysmd_multi(const struct field_mapping *fields, return 0; } +#define TD_SYSINFO_MAP_MOD_INFO(_field_id, _member) \ + TD_SYSINFO_MAP(_field_id, struct tdx_sysinfo_module_info, _member) + +static int get_tdx_module_info(struct tdx_sysinfo_module_info *modinfo) +{ + static const struct field_mapping fields[] = { + TD_SYSINFO_MAP_MOD_INFO(SYS_ATTRIBUTES, sys_attributes), + TD_SYSINFO_MAP_MOD_INFO(TDX_FEATURES0, tdx_features0), + }; + + return stbuf_read_sysmd_multi(fields, ARRAY_SIZE(fields), modinfo); +} + +#define TD_SYSINFO_MAP_MOD_VERSION(_field_id, _member) \ + TD_SYSINFO_MAP(_field_id, struct tdx_sysinfo_module_version, _member) + +static int get_tdx_module_version(struct tdx_sysinfo_module_version *modver) +{ + static const struct field_mapping fields[] = { + TD_SYSINFO_MAP_MOD_VERSION(MAJOR_VERSION, major), + TD_SYSINFO_MAP_MOD_VERSION(MINOR_VERSION, minor), + TD_SYSINFO_MAP_MOD_VERSION(UPDATE_VERSION, update), + TD_SYSINFO_MAP_MOD_VERSION(INTERNAL_VERSION, internal), + TD_SYSINFO_MAP_MOD_VERSION(BUILD_NUM, build_num), + TD_SYSINFO_MAP_MOD_VERSION(BUILD_DATE, build_date), + }; + + return stbuf_read_sysmd_multi(fields, ARRAY_SIZE(fields), modver); +} + +static void print_basic_sysinfo(struct tdx_sysinfo *sysinfo) +{ + struct tdx_sysinfo_module_version *modver = &sysinfo->module_version; + struct tdx_sysinfo_module_info *modinfo = &sysinfo->module_info; + bool debug = modinfo->sys_attributes & TDX_SYS_ATTR_DEBUG_MODULE; + + pr_info("%s module.\n", debug ? "Debug" : "Production"); + + pr_info("TDX_FEATURES0: 0x%llx\n", modinfo->tdx_features0); + + /* + * TDX module version encoding: + * + * .... + * + * When printed as text, and are 1-digit, + * and are 2-digits and + * is 4-digits. + */ + pr_info("Module version: %u.%u.%02u.%02u.%04u, build_date: %u\n", + modver->major, modver->minor, + modver->update, modver->internal, + modver->build_num, modver->build_date); +} + #define TD_SYSINFO_MAP_TDMR_INFO(_field_id, _member) \ TD_SYSINFO_MAP(_field_id, struct tdx_sysinfo_tdmr_info, _member) @@ -339,6 +394,16 @@ static int get_tdx_tdmr_sysinfo(struct tdx_sysinfo_tdmr_info *tdmr_sysinfo) static int get_tdx_sysinfo(struct tdx_sysinfo *sysinfo) { + int ret; + + ret = get_tdx_module_info(&sysinfo->module_info); + if (ret) + return ret; + + ret = get_tdx_module_version(&sysinfo->module_version); + if (ret) + return ret; + return get_tdx_tdmr_sysinfo(&sysinfo->tdmr_info); } @@ -1121,6 +1186,8 @@ static int init_tdx_module(void) if (ret) return ret; + print_basic_sysinfo(&sysinfo); + /* * To keep things simple, assume that all TDX-protected memory * will come from the page allocator. Make sure all pages in the diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index 6b61dc67b0af..d80ec797fbf1 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -31,6 +31,15 @@ * * See Table "Global Scope Metadata", TDX module 1.5 ABI spec. */ +#define MD_FIELD_ID_SYS_ATTRIBUTES 0x0A00000200000000ULL +#define MD_FIELD_ID_TDX_FEATURES0 0x0A00000300000008ULL +#define MD_FIELD_ID_BUILD_DATE 0x8800000200000001ULL +#define MD_FIELD_ID_BUILD_NUM 0x8800000100000002ULL +#define MD_FIELD_ID_MINOR_VERSION 0x0800000100000003ULL +#define MD_FIELD_ID_MAJOR_VERSION 0x0800000100000004ULL +#define MD_FIELD_ID_UPDATE_VERSION 0x0800000100000005ULL +#define MD_FIELD_ID_INTERNAL_VERSION 0x0800000100000006ULL + #define MD_FIELD_ID_MAX_TDMRS 0x9100000100000008ULL #define MD_FIELD_ID_MAX_RESERVED_PER_TDMR 0x9100000100000009ULL #define MD_FIELD_ID_PAMT_4K_ENTRY_SIZE 0x9100000100000010ULL @@ -124,8 +133,28 @@ struct tdmr_info_list { * * Note not all metadata fields in each class are defined, only those * used by the kernel are. + * + * Also note the "bit definitions" are architectural. */ +/* Class "TDX Module Info" */ +struct tdx_sysinfo_module_info { + u32 sys_attributes; + u64 tdx_features0; +}; + +#define TDX_SYS_ATTR_DEBUG_MODULE 0x1 + +/* Class "TDX Module Version" */ +struct tdx_sysinfo_module_version { + u16 major; + u16 minor; + u16 update; + u16 internal; + u16 build_num; + u32 build_date; +}; + /* Class "TDMR Info" */ struct tdx_sysinfo_tdmr_info { u16 max_tdmrs; @@ -134,7 +163,9 @@ struct tdx_sysinfo_tdmr_info { }; struct tdx_sysinfo { - struct tdx_sysinfo_tdmr_info tdmr_info; + struct tdx_sysinfo_module_info module_info; + struct tdx_sysinfo_module_version module_version; + struct tdx_sysinfo_tdmr_info tdmr_info; }; #endif From patchwork Sun Jun 16 12:01:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13699539 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 E941B18FDC3; Sun, 16 Jun 2024 12:02:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539334; cv=none; b=YmgBIocuKWt1vu+XXncx8VX/xERoKAzniVLN1sRbWdgRqChcanIj+kDKFiBXCu15y+eDZOmphGEqPRxnLtJSzW56M+TLCCZQKurRtFNySSPv4g9qbViahEtj+FemZN3/R8TrxJQvvyWGulLVQdGAKPtApP03KZOYu8jHMtmjgNs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539334; c=relaxed/simple; bh=RQWrTEisf+S1wvmj4mHQzWV4YWIV0ubVfjoG0Zw3l9U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZLe1WXnZMaWupYGtxcfYXcmtygryITVICg0bbiUViFn8+cTn8NiMnfyPqT0MO22F5wG6mPHjUqf9XAjzuXbaQkyufhtdxzshSQ7F06I1PzckJdy5cJCG3fviw9cE+9JutvkqWYEppz8DCc03hKm+BCNoI1p5TZt++r712Jwoj1E= 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=NAHrHhoj; arc=none smtp.client-ip=198.175.65.12 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="NAHrHhoj" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718539333; x=1750075333; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RQWrTEisf+S1wvmj4mHQzWV4YWIV0ubVfjoG0Zw3l9U=; b=NAHrHhojuQKgYkVPpPTvl577U9zV1QFtDLOyTD/4lf6pR8Sgt+lzyEK5 pfja+CNozquze6uG9qhaP1FVyQH8OKQ1XBGEnD1qAoKLoOdLpncEsnWhe v1qCPPi3NiIPS4mQyPQWJ/wf4VlTV2MGE7AremKcrqwJODKJajTHmx4qo ruTq3vNG+D0gIPm3d5qiPZSNsr88r+I9IoBdoHLnklAfXpFDkUdkbjhRm sAJMH2+73kqjJhyTRGtqS1NRqoUrnH6JgouFmRjnDs9dksN3LukLKobyK 0QOcAT1HMcdffonN1Kh8xTDtTDaTbBsAVJluYnv5U6pYFtDhGJuwelA5M Q==; X-CSE-ConnectionGUID: ydzXywC5SW2/28MtSB8GTA== X-CSE-MsgGUID: SgKXS8BaSESx1e19ZG/WEg== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="26800078" X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="26800078" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:02:13 -0700 X-CSE-ConnectionGUID: NBFbaBVbRjCcfsSB9szseQ== X-CSE-MsgGUID: 9WV9WJlVRrWXz4prTfoE/A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="46055955" Received: from mgoodin-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.124.223.226]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:02:09 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, dan.j.williams@intel.com, kirill.shutemov@linux.intel.com, rick.p.edgecombe@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, isaku.yamahata@intel.com, binbin.wu@linux.intel.com, kai.huang@intel.com Subject: [PATCH 8/9] x86/virt/tdx: Exclude memory region hole within CMR as TDMR's reserved area Date: Mon, 17 Jun 2024 00:01:18 +1200 Message-ID: X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 A TDX module initialization failure was reported on a Emerald Rapids platform: virt/tdx: initialization failed: TDMR [0x0, 0x80000000): reserved areas exhausted. virt/tdx: module initialization failed (-28) As a step of initializing the TDX module, the kernel tells the TDX module all the "TDX-usable memory regions" via a set of TDX architecture defined structure "TD Memory Region" (TDMR). Each TDMR must be in 1GB aligned and in 1GB granularity, and all "non-TDX-usable memory holes" in a given TDMR must be marked as a "reserved area". Each TDMR only supports a maximum number of reserved areas reported by the TDX module. As shown above, the root cause of this failure is when the kernel tries to construct a TDMR to cover address range [0x0, 0x80000000), there are too many memory holes within that range and the number of memory holes exceeds the maximum number of reserved areas. The E820 table of that platform (see [1] below) reflects this: the number of memory holes among e820 "usable" entries exceeds 16, which is the maximum number of reserved areas TDX module supports in practice. === Fix === There are two options to fix this: 1) put less memory holes as "reserved area" when constructing a TDMR; 2) reduce the TDMR's size to cover less memory regions, thus less memory holes. Option 1) is possible, and in fact is easier and preferable: TDX actually has a concept of "Convertible Memory Regions" (CMRs). TDX reports a list of CMRs that meet TDX's security requirements on memory. TDX requires all the "TDX-usable memory regions" that the kernel passes to the module via TDMRs, a.k.a, all the "non-reserved regions in TDMRs", must be convertible memory. In other words, if a memory hole is indeed CMR, then it's not mandatory for the kernel to add it to the reserved areas. The number of consumed reserved areas can be reduced if the kernel doesn't add those memory holes as reserved area. Note this doesn't have security impact because the kernel is out of TDX's TCB anyway. This is feasible because in practice the CMRs just reflect the nature of whether the RAM can indeed be used by TDX, thus each CMR tends to be a large range w/o being split into small areas, e.g., in the way the e820 table does to contain a lot "ACPI *" entries. [2] below shows the CMRs reported on the problematic platform (using the off-tree TDX code). So for this particular module initialization failure, the memory holes that are within [0x0, 0x80000000) are mostly indeed CMR. By not adding them to the reserved areas, the number of consumed reserved areas for the TDMR [0x0, 0x80000000) can be dramatically reduced. On the other hand, although option 2) is also theoretically feasible, it requires more complicated logic to handle around splitting TDMR into smaller ones. E.g., today one memory region must be fully in one TDMR, while splitting TDMR will result in each TDMR only covering part of some memory region. And this also increases the total number of TDMRs, which also cannot exceed a maximum value that TDX module supports. So, fix this issue by: 1) reading out the CMRs from the TDX module global metadata, and 2) passing the CMRs to the function construct_tdmrs(), and changing to not add a memory hole to the reserved areas when it is indeed CMR. Also dump the CMRs in dmesg. They are helpful when something goes wrong around "constructing and passing the TDMRs to the TDX module to configure it". [1] BIOS-E820 table of the problematic platform: BIOS-e820: [mem 0x0000000000000000-0x000000000009efff] usable BIOS-e820: [mem 0x000000000009f000-0x00000000000fffff] reserved BIOS-e820: [mem 0x0000000000100000-0x000000005d168fff] usable BIOS-e820: [mem 0x000000005d169000-0x000000005d22afff] ACPI data BIOS-e820: [mem 0x000000005d22b000-0x000000005d3cefff] usable BIOS-e820: [mem 0x000000005d3cf000-0x000000005d469fff] reserved BIOS-e820: [mem 0x000000005d46a000-0x000000005e5b2fff] usable BIOS-e820: [mem 0x000000005e5b3000-0x000000005e5c2fff] reserved BIOS-e820: [mem 0x000000005e5c3000-0x000000005e5d2fff] usable BIOS-e820: [mem 0x000000005e5d3000-0x000000005e5e4fff] reserved BIOS-e820: [mem 0x000000005e5e5000-0x000000005eb57fff] usable BIOS-e820: [mem 0x000000005eb58000-0x0000000061357fff] ACPI NVS BIOS-e820: [mem 0x0000000061358000-0x000000006172afff] usable BIOS-e820: [mem 0x000000006172b000-0x0000000061794fff] ACPI data BIOS-e820: [mem 0x0000000061795000-0x00000000617fefff] usable BIOS-e820: [mem 0x00000000617ff000-0x0000000061912fff] ACPI data BIOS-e820: [mem 0x0000000061913000-0x0000000061998fff] usable BIOS-e820: [mem 0x0000000061999000-0x00000000619dffff] ACPI data BIOS-e820: [mem 0x00000000619e0000-0x00000000619e1fff] usable BIOS-e820: [mem 0x00000000619e2000-0x00000000619e9fff] reserved BIOS-e820: [mem 0x00000000619ea000-0x0000000061a26fff] usable BIOS-e820: [mem 0x0000000061a27000-0x0000000061baefff] ACPI data BIOS-e820: [mem 0x0000000061baf000-0x00000000623c2fff] usable BIOS-e820: [mem 0x00000000623c3000-0x0000000062471fff] reserved BIOS-e820: [mem 0x0000000062472000-0x0000000062823fff] usable BIOS-e820: [mem 0x0000000062824000-0x0000000063a24fff] reserved BIOS-e820: [mem 0x0000000063a25000-0x0000000063d57fff] usable BIOS-e820: [mem 0x0000000063d58000-0x0000000064157fff] reserved BIOS-e820: [mem 0x0000000064158000-0x0000000064158fff] usable BIOS-e820: [mem 0x0000000064159000-0x0000000064194fff] reserved BIOS-e820: [mem 0x0000000064195000-0x000000006e9cefff] usable BIOS-e820: [mem 0x000000006e9cf000-0x000000006eccefff] reserved BIOS-e820: [mem 0x000000006eccf000-0x000000006f6fefff] ACPI NVS BIOS-e820: [mem 0x000000006f6ff000-0x000000006f7fefff] ACPI data BIOS-e820: [mem 0x000000006f7ff000-0x000000006f7fffff] usable BIOS-e820: [mem 0x000000006f800000-0x000000008fffffff] reserved ...... [2] Convertible Memory Regions of the problematic platform: virt/tdx: CMR: [0x100000, 0x6f800000) virt/tdx: CMR: [0x100000000, 0x107a000000) virt/tdx: CMR: [0x1080000000, 0x207c000000) virt/tdx: CMR: [0x2080000000, 0x307c000000) virt/tdx: CMR: [0x3080000000, 0x407c000000) Signed-off-by: Kai Huang --- arch/x86/virt/vmx/tdx/tdx.c | 149 ++++++++++++++++++++++++++++++++---- arch/x86/virt/vmx/tdx/tdx.h | 13 ++++ 2 files changed, 146 insertions(+), 16 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index ced40e3b516e..88a0c8b788b7 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -293,6 +293,10 @@ static int stbuf_read_sysmd_field(u64 field_id, void *stbuf, int offset, return 0; } +/* Wrapper to read one metadata field to u8/u16/u32/u64 */ +#define stbuf_read_sysmd_single(_field_id, _pdata) \ + stbuf_read_sysmd_field(_field_id, _pdata, 0, sizeof(typeof(*(_pdata)))) + struct field_mapping { u64 field_id; int offset; @@ -349,6 +353,76 @@ static int get_tdx_module_version(struct tdx_sysinfo_module_version *modver) return stbuf_read_sysmd_multi(fields, ARRAY_SIZE(fields), modver); } +/* Update the @cmr_info->num_cmrs to trim tail empty CMRs */ +static void trim_empty_tail_cmrs(struct tdx_sysinfo_cmr_info *cmr_info) +{ + int i; + + for (i = 0; i < cmr_info->num_cmrs; i++) { + u64 cmr_base = cmr_info->cmr_base[i]; + u64 cmr_size = cmr_info->cmr_size[i]; + + if (!cmr_size) { + WARN_ON_ONCE(cmr_base); + break; + } + + /* TDX architecture: CMR must be 4KB aligned */ + WARN_ON_ONCE(!PAGE_ALIGNED(cmr_base) || + !PAGE_ALIGNED(cmr_size)); + } + + cmr_info->num_cmrs = i; +} + +#define TD_SYSINFO_MAP_CMR_INFO(_field_id, _member) \ + TD_SYSINFO_MAP(_field_id, struct tdx_sysinfo_cmr_info, _member) + +static int get_tdx_cmr_info(struct tdx_sysinfo_cmr_info *cmr_info) +{ + int i, ret; + + ret = stbuf_read_sysmd_single(MD_FIELD_ID_NUM_CMRS, + &cmr_info->num_cmrs); + if (ret) + return ret; + + for (i = 0; i < cmr_info->num_cmrs; i++) { + const struct field_mapping fields[] = { + TD_SYSINFO_MAP_CMR_INFO(CMR_BASE0 + i, cmr_base[i]), + TD_SYSINFO_MAP_CMR_INFO(CMR_SIZE0 + i, cmr_size[i]), + }; + + ret = stbuf_read_sysmd_multi(fields, ARRAY_SIZE(fields), + cmr_info); + if (ret) + return ret; + } + + /* + * The TDX module may just report the maximum number of CMRs that + * TDX architecturally supports as the actual number of CMRs, + * despite the latter is smaller. In this case all the tail + * CMRs will be empty. Trim them away. + */ + trim_empty_tail_cmrs(cmr_info); + + return 0; +} + +static void print_cmr_info(struct tdx_sysinfo_cmr_info *cmr_info) +{ + int i; + + for (i = 0; i < cmr_info->num_cmrs; i++) { + u64 cmr_base = cmr_info->cmr_base[i]; + u64 cmr_size = cmr_info->cmr_size[i]; + + pr_info("CMR[%d]: [0x%llx, 0x%llx)\n", i, cmr_base, + cmr_base + cmr_size); + } +} + static void print_basic_sysinfo(struct tdx_sysinfo *sysinfo) { struct tdx_sysinfo_module_version *modver = &sysinfo->module_version; @@ -372,6 +446,8 @@ static void print_basic_sysinfo(struct tdx_sysinfo *sysinfo) modver->major, modver->minor, modver->update, modver->internal, modver->build_num, modver->build_date); + + print_cmr_info(&sysinfo->cmr_info); } #define TD_SYSINFO_MAP_TDMR_INFO(_field_id, _member) \ @@ -404,6 +480,10 @@ static int get_tdx_sysinfo(struct tdx_sysinfo *sysinfo) if (ret) return ret; + ret = get_tdx_cmr_info(&sysinfo->cmr_info); + if (ret) + return ret; + return get_tdx_tdmr_sysinfo(&sysinfo->tdmr_info); } @@ -827,6 +907,23 @@ static int tdmr_add_rsvd_area(struct tdmr_info *tdmr, int *p_idx, u64 addr, return 0; } +/* Return whether a given region [start, end) is a sub-region of any CMR */ +static bool is_cmr_subregion(struct tdx_sysinfo_cmr_info *cmr_info, u64 start, + u64 end) +{ + int i; + + for (i = 0; i < cmr_info->num_cmrs; i++) { + u64 cmr_base = cmr_info->cmr_base[i]; + u64 cmr_size = cmr_info->cmr_size[i]; + + if (start >= cmr_base && end <= (cmr_base + cmr_size)) + return true; + } + + return false; +} + /* * Go through @tmb_list to find holes between memory areas. If any of * those holes fall within @tdmr, set up a TDMR reserved area to cover @@ -835,7 +932,8 @@ static int tdmr_add_rsvd_area(struct tdmr_info *tdmr, int *p_idx, u64 addr, static int tdmr_populate_rsvd_holes(struct list_head *tmb_list, struct tdmr_info *tdmr, int *rsvd_idx, - u16 max_reserved_per_tdmr) + u16 max_reserved_per_tdmr, + struct tdx_sysinfo_cmr_info *cmr_info) { struct tdx_memblock *tmb; u64 prev_end; @@ -864,10 +962,16 @@ static int tdmr_populate_rsvd_holes(struct list_head *tmb_list, * Skip over memory areas that * have already been dealt with. */ - if (start <= prev_end) { - prev_end = end; - continue; - } + if (start <= prev_end) + goto next_tmb; + + /* + * Found the hole [prev_end, start) before this region. + * Skip the hole if it is within any CMR to reduce the + * consumption of reserved areas. + */ + if (is_cmr_subregion(cmr_info, prev_end, start)) + goto next_tmb; /* Add the hole before this region */ ret = tdmr_add_rsvd_area(tdmr, rsvd_idx, prev_end, @@ -876,11 +980,16 @@ static int tdmr_populate_rsvd_holes(struct list_head *tmb_list, if (ret) return ret; +next_tmb: prev_end = end; } - /* Add the hole after the last region if it exists. */ - if (prev_end < tdmr_end(tdmr)) { + /* + * Add the hole after the last region if it exists, but skip + * if it is within any CMR. + */ + if (prev_end < tdmr_end(tdmr) && + !is_cmr_subregion(cmr_info, prev_end, tdmr_end(tdmr))) { ret = tdmr_add_rsvd_area(tdmr, rsvd_idx, prev_end, tdmr_end(tdmr) - prev_end, max_reserved_per_tdmr); @@ -956,12 +1065,13 @@ static int rsvd_area_cmp_func(const void *a, const void *b) static int tdmr_populate_rsvd_areas(struct tdmr_info *tdmr, struct list_head *tmb_list, struct tdmr_info_list *tdmr_list, - u16 max_reserved_per_tdmr) + u16 max_reserved_per_tdmr, + struct tdx_sysinfo_cmr_info *cmr_info) { int ret, rsvd_idx = 0; ret = tdmr_populate_rsvd_holes(tmb_list, tdmr, &rsvd_idx, - max_reserved_per_tdmr); + max_reserved_per_tdmr, cmr_info); if (ret) return ret; @@ -979,11 +1089,13 @@ static int tdmr_populate_rsvd_areas(struct tdmr_info *tdmr, /* * Populate reserved areas for all TDMRs in @tdmr_list, including memory - * holes (via @tmb_list) and PAMTs. + * holes (via @tmb_list) and PAMTs. Exclude the memory holes within any + * CMR to reduce number of consumed reserved areas. */ static int tdmrs_populate_rsvd_areas_all(struct tdmr_info_list *tdmr_list, struct list_head *tmb_list, - u16 max_reserved_per_tdmr) + u16 max_reserved_per_tdmr, + struct tdx_sysinfo_cmr_info *cmr_info) { int i; @@ -991,7 +1103,8 @@ static int tdmrs_populate_rsvd_areas_all(struct tdmr_info_list *tdmr_list, int ret; ret = tdmr_populate_rsvd_areas(tdmr_entry(tdmr_list, i), - tmb_list, tdmr_list, max_reserved_per_tdmr); + tmb_list, tdmr_list, max_reserved_per_tdmr, + cmr_info); if (ret) return ret; } @@ -1002,11 +1115,13 @@ static int tdmrs_populate_rsvd_areas_all(struct tdmr_info_list *tdmr_list, /* * Construct a list of TDMRs on the preallocated space in @tdmr_list * to cover all TDX memory regions in @tmb_list based on the TDX module - * TDMR global information in @tdmr_sysinfo. + * TDMR global information in @tdmr_sysinfo and CMR information in + * @cmr_info. */ static int construct_tdmrs(struct list_head *tmb_list, struct tdmr_info_list *tdmr_list, - struct tdx_sysinfo_tdmr_info *tdmr_sysinfo) + struct tdx_sysinfo_tdmr_info *tdmr_sysinfo, + struct tdx_sysinfo_cmr_info *cmr_info) { int ret; @@ -1020,7 +1135,8 @@ static int construct_tdmrs(struct list_head *tmb_list, return ret; ret = tdmrs_populate_rsvd_areas_all(tdmr_list, tmb_list, - tdmr_sysinfo->max_reserved_per_tdmr); + tdmr_sysinfo->max_reserved_per_tdmr, + cmr_info); if (ret) tdmrs_free_pamt_all(tdmr_list); @@ -1210,7 +1326,8 @@ static int init_tdx_module(void) goto err_free_tdxmem; /* Cover all TDX-usable memory regions in TDMRs */ - ret = construct_tdmrs(&tdx_memlist, &tdx_tdmr_list, &sysinfo.tdmr_info); + ret = construct_tdmrs(&tdx_memlist, &tdx_tdmr_list, &sysinfo.tdmr_info, + &sysinfo.cmr_info); if (ret) goto err_free_tdmrs; diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index d80ec797fbf1..be93b6f31e5b 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -40,6 +40,10 @@ #define MD_FIELD_ID_UPDATE_VERSION 0x0800000100000005ULL #define MD_FIELD_ID_INTERNAL_VERSION 0x0800000100000006ULL +#define MD_FIELD_ID_NUM_CMRS 0x9000000100000000ULL +#define MD_FIELD_ID_CMR_BASE0 0x9000000300000080ULL +#define MD_FIELD_ID_CMR_SIZE0 0x9000000300000100ULL + #define MD_FIELD_ID_MAX_TDMRS 0x9100000100000008ULL #define MD_FIELD_ID_MAX_RESERVED_PER_TDMR 0x9100000100000009ULL #define MD_FIELD_ID_PAMT_4K_ENTRY_SIZE 0x9100000100000010ULL @@ -155,6 +159,14 @@ struct tdx_sysinfo_module_version { u32 build_date; }; +/* Class "CMR Info" */ +#define TDX_MAX_CMRS 32 +struct tdx_sysinfo_cmr_info { + u16 num_cmrs; + u64 cmr_base[TDX_MAX_CMRS]; + u64 cmr_size[TDX_MAX_CMRS]; +}; + /* Class "TDMR Info" */ struct tdx_sysinfo_tdmr_info { u16 max_tdmrs; @@ -165,6 +177,7 @@ struct tdx_sysinfo_tdmr_info { struct tdx_sysinfo { struct tdx_sysinfo_module_info module_info; struct tdx_sysinfo_module_version module_version; + struct tdx_sysinfo_cmr_info cmr_info; struct tdx_sysinfo_tdmr_info tdmr_info; }; From patchwork Sun Jun 16 12:01:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kai Huang X-Patchwork-Id: 13699540 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) (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 AFC6F19006B; Sun, 16 Jun 2024 12:02:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539338; cv=none; b=mHW6M7yaMC2mVSx2ajqhzcCMLXtvMeznDzMRA4vUnFmx6qag4kvSDaF798O8N3gDwnPP4VXr/S7hxFUhRsAKYdmNTemTXoxYIO38EHugWNTc7bC100tJXEJWkiT7mCmbDPpmYN9W4wlfYHYWxigbzcl/smGDx+3CDlHehgL8YBE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718539338; c=relaxed/simple; bh=qDfrc/bFBjcwqB+Q39ise3546WOanRxKZPywjkw43Y4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r2c1F0rJaUgGg9REwYF2ZIvNbwH12c5gDlixwdZxZWPJr+dy3GgxBv1k8BunnTkYk4nULGcMLKBTX2V6+qUEp1sBKSHLU3aDUtgdijkFMr+V+1Q2bLsjKNubjxwV5T3+SIz+j8Fq7hnKsLSn9KYH8TgsuX1TeejjVuXbBe7wACI= 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=XSIZFUvf; arc=none smtp.client-ip=198.175.65.12 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="XSIZFUvf" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1718539337; x=1750075337; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qDfrc/bFBjcwqB+Q39ise3546WOanRxKZPywjkw43Y4=; b=XSIZFUvfQbap8we4dI+1VMv8ROx7xcvaa0a86LsENbJkJOFOv1ATLCOM ysySzNfkRfeuIudRUYeFEF+i9adqsK3QQkAVmP03kpuNdjR0MqYd4RUYZ vtNbx8j75W7d3E1bC3S5JvKMJzWwjqaEbEuOK128cBrAYbGtQK0FBLkoU ZYm09OrI5+Bnz/YFeCO2DbPndLeTKlx+4XXDxIOaiq2Wh9jJYGXx8pTRl W/rUlq80mR4kCyrPBvMi49gf5djlRX35eXywipy6/7QRleV0oKu/4bOB6 tJ42rAzXrY+kGIJOquKLCo4hmfc5M7yNcfLRcM2JTNPu/6lq4ARVWZyU5 g==; X-CSE-ConnectionGUID: Jpz/WQenQbOC5mOKZ5cvGA== X-CSE-MsgGUID: 1AUnDGg7S6O7WS5zConFiQ== X-IronPort-AV: E=McAfee;i="6700,10204,11104"; a="26800097" X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="26800097" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:02:16 -0700 X-CSE-ConnectionGUID: bqm0L40nS6+4jgOQUsEOyQ== X-CSE-MsgGUID: 2hbUSlBySfSRU6EBdU1Y+Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,242,1712646000"; d="scan'208";a="46055987" Received: from mgoodin-mobl2.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.124.223.226]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Jun 2024 05:02:13 -0700 From: Kai Huang To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, dave.hansen@intel.com, dan.j.williams@intel.com, kirill.shutemov@linux.intel.com, rick.p.edgecombe@intel.com, peterz@infradead.org, tglx@linutronix.de, bp@alien8.de, mingo@redhat.com, hpa@zytor.com, seanjc@google.com, pbonzini@redhat.com, kvm@vger.kernel.org, isaku.yamahata@intel.com, binbin.wu@linux.intel.com, kai.huang@intel.com Subject: [PATCH 9/9] x86/virt/tdx: Don't initialize module that doesn't support NO_RBP_MOD feature Date: Mon, 17 Jun 2024 00:01:19 +1200 Message-ID: <909d809d0a37e51babfe28f88c7fd1fdefa53e88.1718538552.git.kai.huang@intel.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Old TDX modules can clobber RBP in the TDH.VP.ENTER SEAMCALL. However RBP is used as frame pointer in the x86_64 calling convention, and clobbering RBP could result in bad things like being unable to unwind the stack if any non-maskable exceptions (NMI, #MC etc) happens in that gap. A new "NO_RBP_MOD" feature was introduced to more recent TDX modules to not clobber RBP. This feature is reported in the TDX_FEATURES0 global metadata field via bit 18. Don't initialize the TDX module if this feature is not supported [1]. Link: https://lore.kernel.org/all/c0067319-2653-4cbd-8fee-1ccf21b1e646@suse.com/T/#mef98469c51e2382ead2c537ea189752360bd2bef [1] Signed-off-by: Kai Huang Reviewed-by: Nikolay Borisov --- arch/x86/virt/vmx/tdx/tdx.c | 17 +++++++++++++++++ arch/x86/virt/vmx/tdx/tdx.h | 1 + 2 files changed, 18 insertions(+) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 88a0c8b788b7..c4ff68b565e8 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -487,6 +487,18 @@ static int get_tdx_sysinfo(struct tdx_sysinfo *sysinfo) return get_tdx_tdmr_sysinfo(&sysinfo->tdmr_info); } +static int check_module_compatibility(struct tdx_sysinfo *sysinfo) +{ + u64 tdx_features0 = sysinfo->module_info.tdx_features0; + + if (!(tdx_features0 & TDX_FEATURES0_NO_RBP_MOD)) { + pr_err("NO_RBP_MOD feature is not supported\n"); + return -EINVAL; + } + + return 0; +} + /* Calculate the actual TDMR size */ static int tdmr_size_single(u16 max_reserved_per_tdmr) { @@ -1304,6 +1316,11 @@ static int init_tdx_module(void) print_basic_sysinfo(&sysinfo); + /* Check whether the kernel can support this module */ + ret = check_module_compatibility(&sysinfo); + if (ret) + return ret; + /* * To keep things simple, assume that all TDX-protected memory * will come from the page allocator. Make sure all pages in the diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index be93b6f31e5b..295c3b6d9505 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -148,6 +148,7 @@ struct tdx_sysinfo_module_info { }; #define TDX_SYS_ATTR_DEBUG_MODULE 0x1 +#define TDX_FEATURES0_NO_RBP_MOD _BITULL(18) /* Class "TDX Module Version" */ struct tdx_sysinfo_module_version {