From patchwork Mon Feb 13 11:59:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Kai" X-Patchwork-Id: 13138309 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 4EBC4C636D4 for ; Mon, 13 Feb 2023 12:01:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D31D86B0080; Mon, 13 Feb 2023 07:01:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C930A6B0081; Mon, 13 Feb 2023 07:01:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A70C26B0082; Mon, 13 Feb 2023 07:01:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 92FCD6B0080 for ; Mon, 13 Feb 2023 07:01:38 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4A295C154E for ; Mon, 13 Feb 2023 12:01:38 +0000 (UTC) X-FDA: 80462129076.26.3261C32 Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by imf11.hostedemail.com (Postfix) with ESMTP id 14E1440026 for ; Mon, 13 Feb 2023 12:01:35 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=mB7i3TK0; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of kai.huang@intel.com designates 192.55.52.88 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=1676289696; 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=Zwhg4Ecrj+mlyKk7x+AI9w0b4sjnLbK8JX2XBMf8FY0=; b=3tvCdZmDXFOGtNrlUilNdS8EHz9borFyJfcsYC7gV7g7sM8FvPe+BG7oG1J7xUKTuJzeum 6L+kvQopXMieI9kfKyfsLonCLIjPQw6evujOop6TuDA43jw1C2UmeFhubmGe/6OnRWipfm KRO4ODM+wP+GhinuosRcONddWJeflBs= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=mB7i3TK0; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of kai.huang@intel.com designates 192.55.52.88 as permitted sender) smtp.mailfrom=kai.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1676289696; a=rsa-sha256; cv=none; b=HXIs+xnrOPVDblCc2BJZhwIo1N1QgfsrZYZM7qkysMX1V/D6twB2DpUlX3agABjbi1vmGb 5pUIF9tOD6eWGH7YRx14goyuGDz4DGYyIzHfmacd1qJTexXPD90YqlcIheHUJ4aqX+Qfqg WON4TX0+nNJfokIUR32NWbaYs9dsd/M= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1676289696; x=1707825696; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bJvVJsnRcKkHlH10MX+mqczhV6X41iy5nbj4zqxw7oo=; b=mB7i3TK0Zsdl2xKJU0V73wsfRg7HpLYik14R/jbHOqjFSTkFEIm+rVZz Q+29WyoZuY8rLHH+qdXeyMR6qbt/tIK3IvrVTVI4ZD1cWMTphSufYqcoO 3ebmN7lnCQaWp+le1G/odemOQskOWc7QZ7p3CtvLpOmvBJZcIkVSx0H8r 2FKBZrTS4wvoIWsk4sw/ObWWJ0V2Qr+2RMBxJ+/Y28jMqBrr/p4OAZgvq 1nswj0AFEpcrjeM7GFDaXpocajYDQufJfFV3o17sDbyq0n15ntpfEQAMX 6eJyUGoLy4U9HpbC5RrJTHsqOBlWKYUHWT1kFXcOHzwq7Uc2HEN0cEzCR w==; X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="358283456" X-IronPort-AV: E=Sophos;i="5.97,293,1669104000"; d="scan'208";a="358283456" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 04:01:27 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10619"; a="701243513" X-IronPort-AV: E=Sophos;i="5.97,293,1669104000"; d="scan'208";a="701243513" Received: from wonger-mobl.amr.corp.intel.com (HELO khuang2-desk.gar.corp.intel.com) ([10.209.188.34]) by orsmga001-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Feb 2023 04:01:21 -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, david@redhat.com, bagasdotme@gmail.com, sagis@google.com, imammedo@redhat.com, kai.huang@intel.com Subject: [PATCH v9 14/18] x86/virt/tdx: Configure TDX module with the TDMRs and global KeyID Date: Tue, 14 Feb 2023 00:59:21 +1300 Message-Id: <4cc5ec58d639a4c10e87abd35bc2002a88a7eb43.1676286526.git.kai.huang@intel.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: References: MIME-Version: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31ZlqJTIwb21GIho7R 0Iy6a4Gt BZf0jg4CkJp1yyUiBdIw8XF/DNw8wNFz52FKaVIEp2GsBGcadwGhBG36WyflNGDM/UE9XVcB+NtOLarcMK3fSd6IzTcmQdSaIEFzKHDLuikqvNNn/4lVlT8Om0y8YvrbcOZG/voQPgR0pycJnWMBcWG/vyNN17YZztTGydiisU2bAufY= 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: The TDX module uses a private KeyID as the "global KeyID" for mapping things like the PAMT and other TDX metadata. This KeyID has already been reserved when detecting TDX during the kernel early boot. After the list of "TD Memory Regions" (TDMRs) has been constructed to cover all TDX-usable memory regions, the next step is to pass them to the TDX module together with the global KeyID. Signed-off-by: Kai Huang Reviewed-by: Isaku Yamahata --- v8 -> v9: - Removed 'i.e.' in changelog and removed the passive voice (Dave). - Improved changelog (Dave). - Changed the way to allocate aligned PA array (Dave). - Moved reserving the TDX global KeyID to the second patch, and also changed 'tdx_keyid_start' and 'nr_tdx_keyid' to guest's KeyIDs in that patch (Dave). 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, 42 insertions(+), 1 deletion(-) diff --git a/arch/x86/virt/vmx/tdx/tdx.c b/arch/x86/virt/vmx/tdx/tdx.c index 34aed2aa9f4b..5a4163d40f58 100644 --- a/arch/x86/virt/vmx/tdx/tdx.c +++ b/arch/x86/virt/vmx/tdx/tdx.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -997,6 +998,39 @@ 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; + 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 also has certain + * alignment requirement. + */ + array_sz = tdmr_list->nr_consumed_tdmrs * sizeof(u64); + array_sz = roundup_pow_of_two(array_sz); + if (array_sz < TDMR_INFO_PA_ARRAY_ALIGNMENT) + array_sz = TDMR_INFO_PA_ARRAY_ALIGNMENT; + + tdmr_pa_array = kzalloc(array_sz, GFP_KERNEL); + if (!tdmr_pa_array) + return -ENOMEM; + + for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++) + tdmr_pa_array[i] = __pa(tdmr_entry(tdmr_list, i)); + + ret = seamcall(TDH_SYS_CONFIG, __pa(tdmr_pa_array), + tdmr_list->nr_consumed_tdmrs, + global_keyid, 0, NULL, NULL); + + /* Free the array as it is not required anymore. */ + kfree(tdmr_pa_array); + + return ret; +} + static int init_tdx_module(void) { static DECLARE_PADDED_STRUCT(tdsysinfo_struct, tdsysinfo, @@ -1061,10 +1095,14 @@ static int init_tdx_module(void) if (ret) goto out_free_tdmrs; + /* Pass the TDMRs and the global KeyID to the TDX module */ + ret = config_tdx_module(&tdx_tdmr_list, tdx_global_keyid); + if (ret) + goto out_free_pamts; + /* * TODO: * - * - Configure the TDMRs and the global KeyID to the TDX module. * - Configure the global KeyID on all packages. * - Initialize all TDMRs. * @@ -1072,6 +1110,7 @@ static int init_tdx_module(void) */ ret = -EINVAL; +out_free_pamts: if (ret) tdmrs_free_pamt_all(&tdx_tdmr_list); else diff --git a/arch/x86/virt/vmx/tdx/tdx.h b/arch/x86/virt/vmx/tdx/tdx.h index 7348ffdfc287..7b34ac257b9a 100644 --- a/arch/x86/virt/vmx/tdx/tdx.h +++ b/arch/x86/virt/vmx/tdx/tdx.h @@ -17,6 +17,7 @@ * TDX module SEAMCALL leaf functions */ #define TDH_SYS_INFO 32 +#define TDH_SYS_CONFIG 45 struct cmr_info { u64 base; @@ -93,6 +94,7 @@ struct tdmr_reserved_area { } __packed; #define TDMR_INFO_ALIGNMENT 512 +#define TDMR_INFO_PA_ARRAY_ALIGNMENT 512 struct tdmr_info { u64 base;