OvmfPkg: Update Sec to support Tdx

RFC: https://bugzilla.tianocore.org/show_bug.cgi?id=3429

There are below major changes in this commit.

1. SecEntry.nasm
In TDX BSP and APs goes to the same entry point in SecEntry.nasm.

BSP initialize the temporary stack and then jumps to SecMain, just as
legacy Ovmf does.

APs spin in a modified mailbox loop using initial mailbox structure.
Its structure defition is in OvmfPkg/Include/IndustryStandard/IntelTdx.h.
APs wait for command to see if the command is for me. If so execute the
command.

2. Sec/SecMain.c
When host VMM create the Td guest, the system memory informations are
stored in TdHob, which is a memory region described in Tdx metadata.
The system memory region in TdHob should be accepted before it can be
accessed. So the major task of this patch is to process the TdHobList
to accept the memory. After that TDVF follow the standard OVMF flow
and jump to PEI phase.

PcdUse1GPageTable is set to FALSE by default in OvmfPkgX64.dsc. It gives
no chance for Intel TDX to support 1G page table. To support 1G page
table this PCD is set to TRUE in OvmfPkgX64.dsc.

TDX_GUEST_SUPPORTED is defined in OvmfPkgX64.dsc. This macro wraps the
Tdx specific code.

TDX only works on X64, so the code is only valid in X64 arch.

Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: Erdem Aktas <erdemaktas@google.com>
Cc: James Bottomley <jejb@linux.ibm.com>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Tom Lendacky <thomas.lendacky@amd.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
Reviewed-by: Jiewen Yao <jiewen.yao@intel.com>
Signed-off-by: Min Xu <min.m.xu@intel.com>
This commit is contained in:
Min Xu 2021-07-16 16:04:11 +08:00 committed by mergify[bot]
parent ccca1c2d5d
commit 2b80269d98
11 changed files with 184 additions and 2 deletions

View File

@ -208,6 +208,7 @@
[LibraryClasses.common]
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
VmgExitLib|OvmfPkg/Library/VmgExitLib/VmgExitLib.inf
TdxLib|MdePkg/Library/TdxLib/TdxLib.inf
[LibraryClasses.common.SEC]
TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

View File

@ -228,6 +228,7 @@
[LibraryClasses.common]
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf
TdxLib|MdePkg/Library/TdxLib/TdxLib.inf
[LibraryClasses.common.SEC]
!ifdef $(DEBUG_ON_SERIAL_PORT)

View File

@ -237,6 +237,7 @@
[LibraryClasses.common]
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
VmgExitLib|OvmfPkg/Library/VmgExitLib/VmgExitLib.inf
TdxLib|MdePkg/Library/TdxLib/TdxLib.inf
[LibraryClasses.common.SEC]
TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

View File

@ -0,0 +1,51 @@
;------------------------------------------------------------------------------
; @file
; TDX Common defitions used by the APs in mailbox
;
; Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
; SPDX-License-Identifier: BSD-2-Clause-Patent
;
;------------------------------------------------------------------------------
CommandOffset equ 00h
ApicidOffset equ 04h
WakeupVectorOffset equ 08h
OSArgsOffset equ 10h
FirmwareArgsOffset equ 800h
WakeupArgsRelocatedMailBox equ 800h
AcceptPageArgsPhysicalStart equ 800h
AcceptPageArgsPhysicalEnd equ 808h
AcceptPageArgsChunkSize equ 810h
AcceptPageArgsPageSize equ 818h
CpuArrivalOffset equ 900h
CpusExitingOffset equ 0a00h
TalliesOffset equ 0a08h
ErrorsOffset equ 0e08h
SIZE_4KB equ 1000h
SIZE_2MB equ 200000h
SIZE_1GB equ 40000000h
PAGE_ACCEPT_LEVEL_4K equ 0
PAGE_ACCEPT_LEVEL_2M equ 1
PAGE_ACCEPT_LEVEL_1G equ 2
TDX_PAGE_ALREADY_ACCEPTED equ 0x00000b0a
TDX_PAGE_SIZE_MISMATCH equ 0xc0000b0b
; Errors of APs in Mailbox
ERROR_NON equ 0
ERROR_INVALID_ACCEPT_PAGE_SIZE equ 1
ERROR_ACCEPT_PAGE_ERROR equ 2
ERROR_INVALID_FALLBACK_PAGE_LEVEL equ 3
MpProtectedModeWakeupCommandNoop equ 0
MpProtectedModeWakeupCommandWakeup equ 1
MpProtectedModeWakeupCommandSleep equ 2
MpProtectedModeWakeupCommandAcceptPages equ 3
MailboxApicIdInvalid equ 0xffffffff
MailboxApicidBroadcast equ 0xfffffffe
%define TDCALL_TDINFO 0x1
%define TDCALL_TDACCEPTPAGE 0x6

View File

@ -232,6 +232,7 @@
PlatformHookLib|MdeModulePkg/Library/BasePlatformHookLibNull/BasePlatformHookLibNull.inf
FdtLib|EmbeddedPkg/Library/FdtLib/FdtLib.inf
VirtioMmioDeviceLib|OvmfPkg/Library/VirtioMmioDeviceLib/VirtioMmioDeviceLib.inf
TdxLib|MdePkg/Library/TdxLib/TdxLib.inf
[LibraryClasses.common.SEC]
QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgSecLib.inf

View File

@ -238,6 +238,8 @@
[LibraryClasses.common]
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf
TdxLib|MdePkg/Library/TdxLib/TdxLib.inf
TdxMailboxLib|OvmfPkg/Library/TdxMailboxLib/TdxMailboxLib.inf
[LibraryClasses.common.SEC]
TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf

View File

@ -93,6 +93,13 @@
INTEL:*_*_*_CC_FLAGS = /D DISABLE_NEW_DEPRECATED_INTERFACES
GCC:*_*_*_CC_FLAGS = -D DISABLE_NEW_DEPRECATED_INTERFACES
#
# Add TDX_GUEST_SUPPORTED
#
MSFT:*_*_*_CC_FLAGS = /D TDX_GUEST_SUPPORTED
INTEL:*_*_*_CC_FLAGS = /D TDX_GUEST_SUPPORTED
GCC:*_*_*_CC_FLAGS = -D TDX_GUEST_SUPPORTED
!include NetworkPkg/NetworkBuildOptions.dsc.inc
[BuildOptions.common.EDKII.DXE_RUNTIME_DRIVER]
@ -238,6 +245,8 @@
[LibraryClasses.common]
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
VmgExitLib|OvmfPkg/Library/VmgExitLib/VmgExitLib.inf
TdxLib|MdePkg/Library/TdxLib/TdxLib.inf
TdxMailboxLib|OvmfPkg/Library/TdxMailboxLib/TdxMailboxLib.inf
[LibraryClasses.common.SEC]
TimerLib|OvmfPkg/Library/AcpiTimerLib/BaseRomAcpiTimerLib.inf
@ -558,6 +567,10 @@
gEmbeddedTokenSpaceGuid.PcdMemoryTypeEfiRuntimeServicesCode|0x100
gEmbeddedTokenSpaceGuid.PcdMemoryTypeEfiRuntimeServicesData|0x100
#
# TDX need 1G PageTable support
gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable|TRUE
#
# Network Pcds
#
@ -669,6 +682,7 @@
OvmfPkg/Sec/SecMain.inf {
<LibraryClasses>
NULL|MdeModulePkg/Library/LzmaCustomDecompressLib/LzmaCustomDecompressLib.inf
NULL|OvmfPkg/Library/PlatformInitLib/PlatformInitLib.inf
}
#

View File

@ -227,6 +227,7 @@
[LibraryClasses.common]
BaseCryptLib|CryptoPkg/Library/BaseCryptLib/BaseCryptLib.inf
VmgExitLib|UefiCpuPkg/Library/VmgExitLibNull/VmgExitLibNull.inf
TdxLib|MdePkg/Library/TdxLib/TdxLib.inf
[LibraryClasses.common.SEC]
QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgSecLib.inf

View File

@ -26,9 +26,8 @@
#include <Library/ExtractGuidedSectionLib.h>
#include <Library/LocalApicLib.h>
#include <Library/CpuExceptionHandlerLib.h>
#include <Ppi/TemporaryRamSupport.h>
#include <Library/PlatformInitLib.h>
#include "AmdSev.h"
#define SEC_IDT_ENTRY_COUNT 34
@ -738,6 +737,20 @@ SecCoreStartupWithStack (
UINT32 Index;
volatile UINT8 *Table;
#if defined (TDX_GUEST_SUPPORTED)
if (TdIsEnabled ()) {
//
// For Td guests, the memory map info is in TdHobLib. It should be processed
// first so that the memory is accepted. Otherwise access to the unaccepted
// memory will trigger tripple fault.
//
if (ProcessTdxHobList () != EFI_SUCCESS) {
CpuDeadLoop ();
}
}
#endif
//
// To ensure SMM can't be compromised on S3 resume, we must force re-init of
// the BaseExtractGuidedSectionLib. Since this is before library contructors
@ -756,6 +769,7 @@ SecCoreStartupWithStack (
// we use a loop rather than CopyMem.
//
IdtTableInStack.PeiService = NULL;
for (Index = 0; Index < SEC_IDT_ENTRY_COUNT; Index++) {
//
// Declare the local variables that actually move the data elements as
@ -813,6 +827,17 @@ SecCoreStartupWithStack (
AsmEnableCache ();
}
#if defined (TDX_GUEST_SUPPORTED)
if (TdIsEnabled ()) {
//
// InitializeCpuExceptionHandlers () should be called in Td guests so that
// #VE exceptions can be handled correctly.
//
InitializeCpuExceptionHandlers (NULL);
}
#endif
DEBUG ((
DEBUG_INFO,
"SecCoreStartupWithStack(0x%x, 0x%x)\n",

View File

@ -77,6 +77,9 @@
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfWorkAreaBase
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecValidatedStart
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecValidatedEnd
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBackupBase
gUefiOvmfPkgTokenSpaceGuid.PcdTdxAcceptPageSize
gUefiOvmfPkgTokenSpaceGuid.PcdOvmfWorkAreaBase
[FeaturePcd]
gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire

View File

@ -10,12 +10,17 @@
;------------------------------------------------------------------------------
#include <Base.h>
%include "TdxCommondefs.inc"
DEFAULT REL
SECTION .text
extern ASM_PFX(SecCoreStartupWithStack)
%macro tdcall 0
db 0x66, 0x0f, 0x01, 0xcc
%endmacro
;
; SecCore Entry Point
;
@ -35,6 +40,32 @@ extern ASM_PFX(SecCoreStartupWithStack)
global ASM_PFX(_ModuleEntryPoint)
ASM_PFX(_ModuleEntryPoint):
;
; Guest type is stored in OVMF_WORK_AREA
;
%define OVMF_WORK_AREA FixedPcdGet32 (PcdOvmfWorkAreaBase)
%define VM_GUEST_TYPE_TDX 2
mov eax, OVMF_WORK_AREA
cmp byte[eax], VM_GUEST_TYPE_TDX
jne InitStack
mov rax, TDCALL_TDINFO
tdcall
;
; R8 [31:0] NUM_VCPUS
; [63:32] MAX_VCPUS
; R9 [31:0] VCPU_INDEX
; Td Guest set the VCPU0 as the BSP, others are the APs
; APs jump to spinloop and get released by DXE's MpInitLib
;
mov rax, r9
and rax, 0xffff
test rax, rax
jne ParkAp
InitStack:
;
; Fill the temporary RAM with the initial stack value.
; The loop below will seed the heap as well, but that's harmless.
@ -67,3 +98,54 @@ ASM_PFX(_ModuleEntryPoint):
sub rsp, 0x20
call ASM_PFX(SecCoreStartupWithStack)
;
; Note: BSP never gets here. APs will be unblocked by DXE
;
; R8 [31:0] NUM_VCPUS
; [63:32] MAX_VCPUS
; R9 [31:0] VCPU_INDEX
;
ParkAp:
mov rbp, r9
.do_wait_loop:
mov rsp, FixedPcdGet32 (PcdOvmfSecGhcbBackupBase)
;
; register itself in [rsp + CpuArrivalOffset]
;
mov rax, 1
lock xadd dword [rsp + CpuArrivalOffset], eax
inc eax
.check_arrival_cnt:
cmp eax, r8d
je .check_command
mov eax, dword[rsp + CpuArrivalOffset]
jmp .check_arrival_cnt
.check_command:
mov eax, dword[rsp + CommandOffset]
cmp eax, MpProtectedModeWakeupCommandNoop
je .check_command
cmp eax, MpProtectedModeWakeupCommandWakeup
je .do_wakeup
; Don't support this command, so ignore
jmp .check_command
.do_wakeup:
;
; BSP sets these variables before unblocking APs
; RAX: WakeupVectorOffset
; RBX: Relocated mailbox address
; RBP: vCpuId
;
mov rax, 0
mov eax, dword[rsp + WakeupVectorOffset]
mov rbx, [rsp + WakeupArgsRelocatedMailBox]
nop
jmp rax
jmp $