From patchwork Fri Dec 9 06:52:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Kai" X-Patchwork-Id: 13069306 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48EF0C4332F for ; Fri, 9 Dec 2022 06:54:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E35538E0005; Fri, 9 Dec 2022 01:54:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DE5BA8E0001; Fri, 9 Dec 2022 01:54:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CAE388E0005; Fri, 9 Dec 2022 01:54:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id BBB158E0001 for ; Fri, 9 Dec 2022 01:54:08 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8F50614075B for ; Fri, 9 Dec 2022 06:54:08 +0000 (UTC) X-FDA: 80221853376.26.DDA6532 Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by imf20.hostedemail.com (Postfix) with ESMTP id 8695A1C000B for ; Fri, 9 Dec 2022 06:54:02 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZTPvOaWS; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=kai.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670568842; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=htQLquiHUogdjffK5dk3+nLZVhY5mCUAYHYF05lECrI=; b=NLSE0l28Y5CsSObGg2UmEdvTju9J+HqGUTqhPeTAsr4blg0e2SAD60VRB8cefpQ0taGqXK WbVN2MYdqv1I8SMATCrFshkkOCmINhwEC6X6+oCsSuEsDF/QyZGctK54qCUf5P05CTEBC1 CSuhKjkE6V70QGkbUQKFHi+nQ646mGU= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZTPvOaWS; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf20.hostedemail.com: domain of kai.huang@intel.com designates 134.134.136.24 as permitted sender) smtp.mailfrom=kai.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670568842; a=rsa-sha256; cv=none; b=Fapj47WV1zUs7766X++t7YxoB2VJkHCnBwiqvU3h1aQ8Fim/gDf5Yah4fSo8AZrJ5blfK6 lvhJN7gXr24OHksRMEEUF9tVGLDSk+pykIvtRrywZD2ib0RLbuOJvmfKeyGbvfLdIgCQmD zFansN+O1PeUGum/a1Q6vlWuKmiY23w= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670568842; x=1702104842; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=HQ0ARWB5QqQjACtiLE4nnaxso1L0OZgpdlJycabR6Bk=; b=ZTPvOaWSLQzWavtn1dEQF1MG7YU4JQvyf+XXsfV+7QQyhJl3AeZB4MA9 d/AE1Wg5lCgGXiV78zCyetqtIY0BuvJCPUPv+Hd7wWKcecFFuO2d+GqSe lrXfj985Aa780wIj+uogr1WtGNk9LDXUoDzA+eAUU+lQKrgCCzIdLwPmO XxxlA8GJFLYfCdIvl2K4TGRK7m0KCWP/L3pa3KBHfhyI7oqY/pSKMkEns H2NwmiAqYxr2rlzIu9Ih1hBCoD2Cark2S1vzuKpex9kF3T1HffHBFdo30 Dc50qmRL1uA/5KKWhUu1A5zLgRa/t+WZq4Q+sXwO0bW+TuuxSTbXRD2Eq g==; X-IronPort-AV: E=McAfee;i="6500,9779,10555"; a="318551459" X-IronPort-AV: E=Sophos;i="5.96,230,1665471600"; d="scan'208";a="318551459" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Dec 2022 22:54:02 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10555"; a="679837066" X-IronPort-AV: E=Sophos;i="5.96,230,1665471600"; d="scan'208";a="679837066" Received: from omiramon-mobl1.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.212.28.82]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Dec 2022 22:53:57 -0800 From: Kai Huang To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: linux-mm@kvack.org, dave.hansen@intel.com, peterz@infradead.org, tglx@linutronix.de, seanjc@google.com, pbonzini@redhat.com, dan.j.williams@intel.com, rafael.j.wysocki@intel.com, kirill.shutemov@linux.intel.com, ying.huang@intel.com, reinette.chatre@intel.com, len.brown@intel.com, tony.luck@intel.com, ak@linux.intel.com, isaku.yamahata@intel.com, chao.gao@intel.com, sathyanarayanan.kuppuswamy@linux.intel.com, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com, kai.huang@intel.com Subject: [PATCH v8 12/16] x86/virt/tdx: Designate the global KeyID and configure the TDX module Date: Fri, 9 Dec 2022 19:52:33 +1300 Message-Id: X-Mailer: git-send-email 2.38.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 8695A1C000B X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: hx7c3mixgh759qexpjtpp7r1xun7zcgg X-HE-Tag: 1670568842-306762 X-HE-Meta: U2FsdGVkX1+dPaVC/UAy/64XKX0uj3TGQ93qwbZbHGQ3EcOGZxC+PynnsItNzpGA8t0s+2oAaTmDuIZcUdXoDa2ekjyMqCgiXJp+RXJuzAlKtTlmWAcodoJsa/STt6LlRV+AZjXt9p8h0lIO5H6w7AyWRTMZJLkHuRfTyKF+TsNGuKU8hN24qdjjJqLkXCrILIpLlD/UXLXAL+iyNmcHcB4zh79Zu2lc7bgN9NzyltKKPte238TCIHJdKiMS6RvPCYLSVKlh0c3XYSH/ubEJ95iCJ96E/4GRwV9hiHsewmawSTfBadtHzD98eNU+mUNE7/IdYMRQPelvAkXR89jrR6yBNvav9tJTOz85nQc18VomixKi2YODKXZM7COtpNSvzBWKU0bxrZaQTrPiOntPQkbt+o+o1wOPvhA73Vk1WaGvwiusr6oiQrTc3kQVyc36PyweOsX7sGzeVnQu2szhclS2W7oanhYzIBWCB11RIEQApni0uz/xuuPcF3dzy/zWgNyBqwfrnnT3TfSy1Fjig3lJaLyartRzXvI8BzwlOfutpNdX3AeBYciDdg10kbCM6Uz9q9vkXE7RnG/c8HQzdARGxf8gzV+MpCsfD4eWCe8ajYNEET5dA7++jwKJ/rEq2/QqibBzuKLF1fn6U/RixM5n+UQp45B7QVp3H9kUbigBaBLPMchT3ncd/ZwNPNkw9cfcNYKr4rurkuW7KxWaz0aZ/3hnQLI9BX9k/DHilteE5/fwwZ9R/ELIw4NXKJKYVdLHNO74H9VtARxW4D53W2w4fkAuC0KFxL/bFwpTomAG3+mzi+M+egQLzkXyLLomPLWxLIxpUtqHHWR5yMz2Z7+mLlYowMgnTqIQqK76QMyH5Rn3zZtrmC5iiO0Z8XDILyzG+vskcnO+63ArEpGHYA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: After a list of "TD Memory Regions" (TDMRs) has been constructed to cover all TDX-usable memory regions, the next step is to pick up a TDX private KeyID as the "global KeyID" (which protects, i.e. TDX module's metadata), and configure it to the TDX module along with the TDMRs. To keep things simple, just use the first TDX KeyID as the global KeyID. Reviewed-by: Isaku Yamahata Signed-off-by: Kai Huang --- v7 -> v8: - Merged "Reserve TDX module global KeyID" patch to this patch, and removed 'tdx_global_keyid' but use 'tdx_keyid_start' directly. - Changed changelog accordingly. - Changed how to allocate aligned array (Dave). --- arch/x86/virt/vmx/tdx/tdx.c | 41 +++++++++++++++++++++++++++++++++++-- arch/x86/virt/vmx/tdx/tdx.h | 2 ++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 620b35e2a61b..ab961443fed5 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -916,6 +916,36 @@ static int construct_tdmrs(struct list_head *tmb_list, return ret; } +static int config_tdx_module(struct tdmr_info_list *tdmr_list, u64 global_keyid) +{ + u64 *tdmr_pa_array, *p; + size_t array_sz; + int i, ret; + + /* + * TDMRs are passed to the TDX module via an array of physical + * addresses of each TDMR. The array itself has alignment + * requirement. + */ + array_sz = tdmr_list->nr_tdmrs * sizeof(u64) + + TDMR_INFO_PA_ARRAY_ALIGNMENT - 1; + p = kzalloc(array_sz, GFP_KERNEL); + if (!p) + return -ENOMEM; + + tdmr_pa_array = PTR_ALIGN(p, TDMR_INFO_PA_ARRAY_ALIGNMENT); + for (i = 0; i < tdmr_list->nr_tdmrs; i++) + tdmr_pa_array[i] = __pa(tdmr_entry(tdmr_list, i)); + + ret = seamcall(TDH_SYS_CONFIG, __pa(tdmr_pa_array), tdmr_list->nr_tdmrs, + global_keyid, 0, NULL, NULL); + + /* Free the array as it is not required anymore. */ + kfree(p); + + return ret; +} + static int init_tdx_module(void) { /* @@ -960,17 +990,24 @@ static int init_tdx_module(void) if (ret) goto out_free_tdmrs; + /* + * Use the first private KeyID as the global KeyID, and pass + * it along with the TDMRs to the TDX module. + */ + ret = config_tdx_module(&tdmr_list, tdx_keyid_start); + if (ret) + goto out_free_pamts; + /* * TODO: * - * - Pick up one TDX private KeyID as the global KeyID. - * - Configure the TDMRs and the global KeyID to the TDX module. * - Configure the global KeyID on all packages. * - Initialize all TDMRs. * * Return error before all steps are done. */ ret = -EINVAL; +out_free_pamts: if (ret) tdmrs_free_pamt_all(&tdmr_list); else diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index d0c762f1a94c..4d2edd477480 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -20,6 +20,7 @@ * TDX module SEAMCALL leaf functions */ #define TDH_SYS_INFO 32 +#define TDH_SYS_CONFIG 45 struct cmr_info { u64 base; @@ -96,6 +97,7 @@ struct tdmr_reserved_area { } __packed; #define TDMR_INFO_ALIGNMENT 512 +#define TDMR_INFO_PA_ARRAY_ALIGNMENT 512 struct tdmr_info { u64 base;