Skip to content

Commit 554ce1c

Browse files
kaihuanghansendc
authored andcommitted
x86/virt/tdx: Configure TDX module with the TDMRs and global KeyID
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. Now that the "TD Memory Regions" (TDMRs) are fully built, pass them to the TDX module together with the global KeyID. Signed-off-by: Kai Huang <kai.huang@intel.com> Signed-off-by: Dave Hansen <dave.hansen@linux.intel.com> Reviewed-by: Isaku Yamahata <isaku.yamahata@intel.com> Reviewed-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com> Reviewed-by: Yuan Yao <yuan.yao@intel.com> Reviewed-by: Dave Hansen <dave.hansen@linux.intel.com> Link: https://lore.kernel.org/all/20231208170740.53979-13-dave.hansen%40intel.com
1 parent dde3b60 commit 554ce1c

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

arch/x86/virt/vmx/tdx/tdx.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <linux/pfn.h>
2525
#include <linux/align.h>
2626
#include <linux/sort.h>
27+
#include <linux/log2.h>
28+
#include <asm/page.h>
2729
#include <asm/msr-index.h>
2830
#include <asm/msr.h>
2931
#include <asm/cpufeature.h>
@@ -892,6 +894,41 @@ static int construct_tdmrs(struct list_head *tmb_list,
892894
return ret;
893895
}
894896

897+
static int config_tdx_module(struct tdmr_info_list *tdmr_list, u64 global_keyid)
898+
{
899+
struct tdx_module_args args = {};
900+
u64 *tdmr_pa_array;
901+
size_t array_sz;
902+
int i, ret;
903+
904+
/*
905+
* TDMRs are passed to the TDX module via an array of physical
906+
* addresses of each TDMR. The array itself also has certain
907+
* alignment requirement.
908+
*/
909+
array_sz = tdmr_list->nr_consumed_tdmrs * sizeof(u64);
910+
array_sz = roundup_pow_of_two(array_sz);
911+
if (array_sz < TDMR_INFO_PA_ARRAY_ALIGNMENT)
912+
array_sz = TDMR_INFO_PA_ARRAY_ALIGNMENT;
913+
914+
tdmr_pa_array = kzalloc(array_sz, GFP_KERNEL);
915+
if (!tdmr_pa_array)
916+
return -ENOMEM;
917+
918+
for (i = 0; i < tdmr_list->nr_consumed_tdmrs; i++)
919+
tdmr_pa_array[i] = __pa(tdmr_entry(tdmr_list, i));
920+
921+
args.rcx = __pa(tdmr_pa_array);
922+
args.rdx = tdmr_list->nr_consumed_tdmrs;
923+
args.r8 = global_keyid;
924+
ret = seamcall_prerr(TDH_SYS_CONFIG, &args);
925+
926+
/* Free the array as it is not required anymore. */
927+
kfree(tdmr_pa_array);
928+
929+
return ret;
930+
}
931+
895932
static int init_tdx_module(void)
896933
{
897934
struct tdx_tdmr_sysinfo tdmr_sysinfo;
@@ -927,10 +964,14 @@ static int init_tdx_module(void)
927964
if (ret)
928965
goto err_free_tdmrs;
929966

967+
/* Pass the TDMRs and the global KeyID to the TDX module */
968+
ret = config_tdx_module(&tdx_tdmr_list, tdx_global_keyid);
969+
if (ret)
970+
goto err_free_pamts;
971+
930972
/*
931973
* TODO:
932974
*
933-
* - Configure the TDMRs and the global KeyID to the TDX module.
934975
* - Configure the global KeyID on all packages.
935976
* - Initialize all TDMRs.
936977
*

arch/x86/virt/vmx/tdx/tdx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define TDH_SYS_INIT 33
1818
#define TDH_SYS_RD 34
1919
#define TDH_SYS_LP_INIT 35
20+
#define TDH_SYS_CONFIG 45
2021

2122
/*
2223
* Global scope metadata field ID.
@@ -53,6 +54,7 @@ struct tdmr_reserved_area {
5354
} __packed;
5455

5556
#define TDMR_INFO_ALIGNMENT 512
57+
#define TDMR_INFO_PA_ARRAY_ALIGNMENT 512
5658

5759
struct tdmr_info {
5860
u64 base;

0 commit comments

Comments
 (0)