From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by mx.groups.io with SMTP id smtpd.web08.61151.1629321976882850922 for ; Wed, 18 Aug 2021 14:26:16 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@ibm.com header.s=pp1 header.b=i7bW5G7b; spf=pass (domain: linux.ibm.com, ip: 148.163.156.1, mailfrom: tobin@linux.ibm.com) Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 17IL2wqM092534; Wed, 18 Aug 2021 17:26:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=l4d/daKhxC3N5anOTSjegRbajAxRwolNsp+z5C+c89Y=; b=i7bW5G7bQ2GZTxGWNGkvsHIIfGBBjIqXCqwSvaq89AHxO9j+RBPChhE/k7ubN6NBxOQF SumNIdV0FHKd7BrVXYhRtu0lMvecKxu4hTeavSzfp0BBywj1b/iWJzugPofqLRIiTvum oDRzOJNjXPk9/l5PppYOnKBHutpGzege09dWIS5m0UM+mnP1cLBx/K6nnWU4itnVYjs2 5n79hBtDAYqG17BvWUnbfsaQQtWmxh4t1Sh5vOxqsm/MTSSvFsDGNbo53Cxg6JWeA6mN eQ94ZYZO1yIGPyBO+Z6OHtDOipFOJ4B5wORDzuD1T9mQjXmTJZziOHjf79eB1MlmlQ3m 5A== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3agg0ajhm6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 18 Aug 2021 17:26:08 -0400 Received: from m0098396.ppops.net (m0098396.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 17ILQ7X6164628; Wed, 18 Aug 2021 17:26:07 -0400 Received: from ppma04wdc.us.ibm.com (1a.90.2fa9.ip4.static.sl-reverse.com [169.47.144.26]) by mx0a-001b2d01.pphosted.com with ESMTP id 3agg0ajgw7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 18 Aug 2021 17:26:07 -0400 Received: from pps.filterd (ppma04wdc.us.ibm.com [127.0.0.1]) by ppma04wdc.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 17ILHcl0022423; Wed, 18 Aug 2021 21:21:17 GMT Received: from b03cxnp08026.gho.boulder.ibm.com (b03cxnp08026.gho.boulder.ibm.com [9.17.130.18]) by ppma04wdc.us.ibm.com with ESMTP id 3ae5fdw6ap-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 18 Aug 2021 21:21:17 +0000 Received: from b03ledav005.gho.boulder.ibm.com (b03ledav005.gho.boulder.ibm.com [9.17.130.236]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 17ILLFLQ31261176 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 18 Aug 2021 21:21:15 GMT Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 81E23BE04F; Wed, 18 Aug 2021 21:21:15 +0000 (GMT) Received: from b03ledav005.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6D283BE051; Wed, 18 Aug 2021 21:21:14 +0000 (GMT) Received: from amdrome1.watson.ibm.com (unknown [9.2.130.16]) by b03ledav005.gho.boulder.ibm.com (Postfix) with ESMTP; Wed, 18 Aug 2021 21:21:14 +0000 (GMT) From: "Tobin Feldman-Fitzthum" To: tobin@ibm.com, dovmurik@linux.vnet.ibm.com, jejb@linux.ibm.com, frankeh@us.ibm.com, pbonzini@redhat.com, ashish.kalra@amd.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, dgilbert@redhat.com, srutherford@google.com, devel@edk2.groups.io, ard.biesheuvel@arm.com, jiewen.yao@intel.com Subject: [RFC PATCH 8/9] OvmfPkg/AmdSev: Add Migration Handler entry point Date: Wed, 18 Aug 2021 17:20:47 -0400 Message-Id: <20210818212048.162626-9-tobin@linux.ibm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210818212048.162626-1-tobin@linux.ibm.com> References: <20210818212048.162626-1-tobin@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: rWdZaIFeT6sLr3UevnZlJcD6sIL9KiAF X-Proofpoint-GUID: X3wUYLEyrSytI0FA5wO8QDqietySz4iK X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.790 definitions=2021-08-18_07:2021-08-17,2021-08-18 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 bulkscore=0 adultscore=0 mlxlogscore=999 phishscore=0 impostorscore=0 malwarescore=0 priorityscore=1501 clxscore=1015 spamscore=0 lowpriorityscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2108180130 Content-Transfer-Encoding: 8bit The Migration Handler runs in the mirror VM. The MH is started directly by the hypervisor. SetupMigrationHandler runs in the main VM and sets up the migration entry point. The HV starts execution of the mirror vCPU at the entry point, which trampolines to MigrationHandlerMain Signed-off-by: Tobin Feldman-Fitzthum --- OvmfPkg/OvmfPkg.dec | 3 ++ OvmfPkg/AmdSev/AmdSevX64.fdf | 3 ++ .../ConfidentialMigrationDxe.inf | 2 + .../ConfidentialMigrationPei.inf | 2 + .../ConfidentialMigrationDxe.c | 48 +++++++++++++++++ .../ConfidentialMigrationPei.c | 6 +++ .../MigrationEntryPoint.nasm | 51 +++++++++++++++++++ 7 files changed, 115 insertions(+) create mode 100644 OvmfPkg/AmdSev/ConfidentialMigration/MigrationEntryPoint.nasm diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec index 1252582c99..c6e07accf6 100644 --- a/OvmfPkg/OvmfPkg.dec +++ b/OvmfPkg/OvmfPkg.dec @@ -336,6 +336,9 @@ gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationMailboxBase|0x0|UINT32|0x4b gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationMailboxSize|0x0|UINT32|0x4c + gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationEntryBase|0x0|UINT32|0x4d + gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationEntrySize|0x0|UINT32|0x4e + [PcdsDynamic, PcdsDynamicEx] gUefiOvmfPkgTokenSpaceGuid.PcdEmuVariableEvent|0|UINT64|2 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashVariablesEnable|FALSE|BOOLEAN|0x10 diff --git a/OvmfPkg/AmdSev/AmdSevX64.fdf b/OvmfPkg/AmdSev/AmdSevX64.fdf index a8e296e641..8687fadfcc 100644 --- a/OvmfPkg/AmdSev/AmdSevX64.fdf +++ b/OvmfPkg/AmdSev/AmdSevX64.fdf @@ -74,6 +74,9 @@ gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.P 0x020000|0x003000 gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationMailboxBase|gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationMailboxSize +0x023000|0x001000 +gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationEntryBase|gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationEntrySize + 0x120000|0x0E0000 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvSize FV = PEIFV diff --git a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.inf b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.inf index 42875095fc..b879037586 100644 --- a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.inf +++ b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.inf @@ -16,6 +16,7 @@ [Sources] ConfidentialMigrationDxe.c VirtualMemory.h + MigrationEntryPoint.nasm [Packages] MdePkg/MdePkg.dec @@ -31,6 +32,7 @@ gUefiOvmfPkgTokenSpaceGuid.PcdIsConfidentialMigrationTarget gUefiOvmfPkgTokenSpaceGuid.PcdStartConfidentialMigrationHandler gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationMailboxBase + gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationEntryBase [Depex] TRUE diff --git a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationPei.inf b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationPei.inf index 918cf22abd..6233b82cc2 100644 --- a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationPei.inf +++ b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationPei.inf @@ -30,6 +30,8 @@ [FixedPcd] gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationMailboxBase gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationMailboxSize + gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationEntryBase + gUefiOvmfPkgTokenSpaceGuid.PcdConfidentialMigrationEntrySize [Depex] TRUE diff --git a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.c b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.c index 2de35a7bb1..5e96206d17 100644 --- a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.c +++ b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.c @@ -38,11 +38,23 @@ typedef volatile struct { UINT32 Done; } MH_COMMAND_PARAMETERS; +// +// Addresses to be used in the entry point +// +typedef struct { + UINT32 Cr3; + UINT64 StackBase; + UINT64 MhBase; +} ENTRY_ADDRS; + // // Offset for non-cbit mapping. // #define UNENC_VIRT_ADDR_BASE 0xffffff8000000000ULL +void MigrationHandlerEntryPoint(void); +void MigrationHandlerEntryPoint64(void); + STATIC PAGE_TABLE_POOL *mPageTablePool = NULL; PHYSICAL_ADDRESS mMigrationHandlerPageTables = 0; PHYSICAL_ADDRESS mMigrationHandlerStackBase = 0; @@ -193,6 +205,16 @@ SetupMigrationHandler ( IN EFI_SYSTEM_TABLE *SystemTable ) { + UINT32 LongModeOffset; + UINT32 EntryAddrsOffset; + UINT32 GdtOffset; + IA32_DESCRIPTOR GdtPtr; + UINT64 EntryPoint; + ENTRY_ADDRS *EntryData; + + LongModeOffset = 0x200; + EntryAddrsOffset = 0x400; + GdtOffset = 0x600; if (!PcdGetBool(PcdStartConfidentialMigrationHandler)) { return 0; @@ -205,6 +227,32 @@ SetupMigrationHandler ( PrepareMigrationHandlerPageTables (); + // + // Copy Migration Handler entry point to a known location. + // + EntryPoint = PcdGet32 (PcdConfidentialMigrationEntryBase); + CopyMem ((void *)EntryPoint, MigrationHandlerEntryPoint, 0x50); + + CopyMem ((void *)(EntryPoint + LongModeOffset), + MigrationHandlerEntryPoint64, 0x50); + + // + // Copy Migration Handler GDT to a known location. + // + AsmReadGdtr (&GdtPtr); + CopyMem ((void *)(EntryPoint + GdtOffset), (void *)GdtPtr.Base, + GdtPtr.Limit); + + // + // Populate entry point with address of page tables, stack, + // and MigrationHandlerMain + // + EntryData = (void *)(EntryPoint + EntryAddrsOffset); + + EntryData->Cr3 = mMigrationHandlerPageTables; + EntryData->StackBase = mMigrationHandlerStackBase; + EntryData->MhBase = (UINT64)MigrationHandlerMain; + // // If VM is migration target, wait until hypervisor modifies CPU state // and restarts execution. diff --git a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationPei.c b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationPei.c index ce304bc07b..5371ef23a9 100644 --- a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationPei.c +++ b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationPei.c @@ -21,5 +21,11 @@ InitializeConfidentialMigrationPei ( EfiRuntimeServicesData ); + BuildMemoryAllocationHob ( + PcdGet32 (PcdConfidentialMigrationEntryBase), + PcdGet32 (PcdConfidentialMigrationEntrySize), + EfiRuntimeServicesData + ); + return EFI_SUCCESS; } diff --git a/OvmfPkg/AmdSev/ConfidentialMigration/MigrationEntryPoint.nasm b/OvmfPkg/AmdSev/ConfidentialMigration/MigrationEntryPoint.nasm new file mode 100644 index 0000000000..9375771b88 --- /dev/null +++ b/OvmfPkg/AmdSev/ConfidentialMigration/MigrationEntryPoint.nasm @@ -0,0 +1,51 @@ +; Entrypoint for Migration Handler + + DEFAULT REL + SECTION .text + +%define ENABLE_DEBUG 1 +%define X86_CR0_PG BIT31 +%define X86_EFER_LME BIT8 +%define X86_CR4_PAE BIT5 + +%define ENTRY_BASE FixedPcdGet32 (PcdConfidentialMigrationEntryBase) + +%define LONG_MODE_OFFSET 0x200; +%define ENTRY_ADDRS_OFFSET 0x400 +%define GDT_OFFSET 0x600 + +%define LONG_MODE_ADDR ENTRY_BASE + LONG_MODE_OFFSET +%define LINEAR_CODE64_SEL 0x38 + +BITS 32 + +global ASM_PFX(MigrationHandlerEntryPoint) +ASM_PFX(MigrationHandlerEntryPoint): + + ; CR3 + mov edi, [ENTRY_BASE + ENTRY_ADDRS_OFFSET] + mov cr3, edi + + ; EFER.LME + mov ecx, 0xc0000080 + rdmsr + bts eax, 8 + wrmsr + + ; CR0.PG + mov eax, cr0 + bts eax, 31 + mov cr0, eax + + ; Far jump to enter long mode + jmp LINEAR_CODE64_SEL:LONG_MODE_ADDR + +BITS 64 +global ASM_PFX(MigrationHandlerEntryPoint64) +ASM_PFX(MigrationHandlerEntryPoint64): + + ; RSP + mov rsp, [ENTRY_BASE + ENTRY_ADDRS_OFFSET + 0x8] + + ; Jump to MH + jmp [ENTRY_BASE + ENTRY_ADDRS_OFFSET + 0x10] -- 2.20.1