Skip to content

Instantly share code, notes, and snippets.

@kiryl
Created November 21, 2023 08:33
Show Gist options
  • Save kiryl/e1dc1719e0c990b3ceee5d8de8dbf332 to your computer and use it in GitHub Desktop.
Save kiryl/e1dc1719e0c990b3ceee5d8de8dbf332 to your computer and use it in GitHub Desktop.
From 99debcd71161935a3121d65d1b9ea5a51e702bce Mon Sep 17 00:00:00 2001
From: "Kirill A. Shutemov" <kirill.shutemov@linux.intel.com>
Date: Tue, 22 Aug 2023 13:45:19 +0300
Subject: [PATCH] HACK: EDK2: Add support for ACPI MADT CPU reset vector and
TEST command
Very ugly implementation of the draft of ACPI spec change. But it is
enough to test kernel side.
---
MdePkg/Include/IndustryStandard/Acpi64.h | 1 +
OvmfPkg/Include/IndustryStandard/IntelTdx.h | 6 ++-
OvmfPkg/Include/TdxCommondefs.inc | 5 ++-
OvmfPkg/TdxDxe/TdxAcpiTable.c | 13 +++++-
OvmfPkg/TdxDxe/TdxAcpiTable.h | 2 +-
OvmfPkg/TdxDxe/X64/ApRunLoop.nasm | 46 +++++++++++++++++++++
6 files changed, 66 insertions(+), 7 deletions(-)
diff --git a/MdePkg/Include/IndustryStandard/Acpi64.h b/MdePkg/Include/IndustryStandard/Acpi64.h
index 575ca0430c13..d728bb63f735 100644
--- a/MdePkg/Include/IndustryStandard/Acpi64.h
+++ b/MdePkg/Include/IndustryStandard/Acpi64.h
@@ -599,6 +599,7 @@ typedef struct {
UINT16 MailBoxVersion;
UINT32 Reserved;
UINT64 MailBoxAddress;
+ UINT64 ResetVector;
} EFI_ACPI_6_4_MULTIPROCESSOR_WAKEUP_STRUCTURE;
///
diff --git a/OvmfPkg/Include/IndustryStandard/IntelTdx.h b/OvmfPkg/Include/IndustryStandard/IntelTdx.h
index cc849be2fb59..68126b8a3cd4 100644
--- a/OvmfPkg/Include/IndustryStandard/IntelTdx.h
+++ b/OvmfPkg/Include/IndustryStandard/IntelTdx.h
@@ -20,8 +20,9 @@
typedef enum {
MpProtectedModeWakeupCommandNoop = 0,
MpProtectedModeWakeupCommandWakeup = 1,
- MpProtectedModeWakeupCommandSleep = 2,
- MpProtectedModeWakeupCommandAcceptPages = 3,
+ MpProtectedModeWakeupCommandTest = 2,
+ MpProtectedModeWakeupCommandSleep = 3,
+ MpProtectedModeWakeupCommandAcceptPages = 4,
} MP_CPU_PROTECTED_MODE_WAKEUP_CMD;
#pragma pack(1)
@@ -60,6 +61,7 @@ typedef struct {
typedef struct {
UINT8 *RelocateApLoopFuncAddress;
UINTN RelocateApLoopFuncSize;
+ UINT8 *RelocateApResetVector;
} MP_RELOCATION_MAP;
#pragma pack()
diff --git a/OvmfPkg/Include/TdxCommondefs.inc b/OvmfPkg/Include/TdxCommondefs.inc
index a29d2fad4233..200fa405731e 100644
--- a/OvmfPkg/Include/TdxCommondefs.inc
+++ b/OvmfPkg/Include/TdxCommondefs.inc
@@ -41,8 +41,9 @@ ERROR_INVALID_FALLBACK_PAGE_LEVEL equ 3
MpProtectedModeWakeupCommandNoop equ 0
MpProtectedModeWakeupCommandWakeup equ 1
-MpProtectedModeWakeupCommandSleep equ 2
-MpProtectedModeWakeupCommandAcceptPages equ 3
+MpProtectedModeWakeupCommandTest equ 2
+MpProtectedModeWakeupCommandSleep equ 3
+MpProtectedModeWakeupCommandAcceptPages equ 4
MailboxApicIdInvalid equ 0xffffffff
MailboxApicidBroadcast equ 0xfffffffe
diff --git a/OvmfPkg/TdxDxe/TdxAcpiTable.c b/OvmfPkg/TdxDxe/TdxAcpiTable.c
index 5dc27e23ad71..9c6079b64f89 100644
--- a/OvmfPkg/TdxDxe/TdxAcpiTable.c
+++ b/OvmfPkg/TdxDxe/TdxAcpiTable.c
@@ -42,7 +42,7 @@
EFI_PHYSICAL_ADDRESS
EFIAPI
RelocateMailbox (
- VOID
+ EFI_PHYSICAL_ADDRESS *ResetVector
)
{
EFI_PHYSICAL_ADDRESS Address;
@@ -115,6 +115,13 @@ RelocateMailbox (
0
);
+ *ResetVector = (UINT64)ApLoopFunc + (RelocationMap.RelocateApResetVector -
+ RelocationMap.RelocateApLoopFuncAddress);
+ DEBUG ((
+ DEBUG_INFO,
+ "Ap Relocation: reset_vector %llx\n",
+ *ResetVector
+ ));
return Address;
}
@@ -142,6 +149,7 @@ AlterAcpiTable (
UINT8 *NewMadtTable;
UINTN NewMadtTableLength;
EFI_PHYSICAL_ADDRESS RelocateMailboxAddress;
+ EFI_PHYSICAL_ADDRESS RelocateResetVector;
EFI_ACPI_6_4_MULTIPROCESSOR_WAKEUP_STRUCTURE *MadtMpWk;
EFI_ACPI_1_0_MULTIPLE_APIC_DESCRIPTION_TABLE_HEADER *MadtHeader;
@@ -155,7 +163,7 @@ AlterAcpiTable (
return;
}
- RelocateMailboxAddress = RelocateMailbox ();
+ RelocateMailboxAddress = RelocateMailbox (&RelocateResetVector);
if (RelocateMailboxAddress == 0) {
ASSERT (FALSE);
DEBUG ((DEBUG_ERROR, "Failed to relocate Td mailbox\n"));
@@ -189,6 +197,7 @@ AlterAcpiTable (
MadtMpWk->MailBoxVersion = 1;
MadtMpWk->Reserved = 0;
MadtMpWk->MailBoxAddress = RelocateMailboxAddress;
+ MadtMpWk->ResetVector = RelocateResetVector;
Status = AcpiTableProtocol->InstallAcpiTable (AcpiTableProtocol, NewMadtTable, NewMadtTableLength, &NewTableKey);
if (EFI_ERROR (Status)) {
diff --git a/OvmfPkg/TdxDxe/TdxAcpiTable.h b/OvmfPkg/TdxDxe/TdxAcpiTable.h
index 6b7615dc3687..8ffb9157bd21 100644
--- a/OvmfPkg/TdxDxe/TdxAcpiTable.h
+++ b/OvmfPkg/TdxDxe/TdxAcpiTable.h
@@ -41,7 +41,7 @@ AsmGetRelocationMap (
EFI_PHYSICAL_ADDRESS
EFIAPI
RelocateMailbox (
- VOID
+ EFI_PHYSICAL_ADDRESS *ResetVector
);
/**
diff --git a/OvmfPkg/TdxDxe/X64/ApRunLoop.nasm b/OvmfPkg/TdxDxe/X64/ApRunLoop.nasm
index 0bef89c48552..ece6a7973226 100644
--- a/OvmfPkg/TdxDxe/X64/ApRunLoop.nasm
+++ b/OvmfPkg/TdxDxe/X64/ApRunLoop.nasm
@@ -49,6 +49,15 @@ AsmRelocateApMailBoxLoopStart:
test r10, r10
jnz Panic
mov r8, r15
+ mov qword[rel mailbox_address], rbx
+ mov rax, cr0
+ mov qword[rel saved_cr0], rax
+ mov rax, cr3
+ mov qword[rel saved_cr3], rax
+ mov rax, cr4
+ mov qword[rel saved_cr4], rax
+ lea rax, qword[rel saved_gdt]
+ sgdt [rax]
MailBoxLoop:
; Spin until command set
@@ -62,6 +71,8 @@ MailBoxLoop:
MailBoxProcessCommand:
cmp dword [rbx + CommandOffset], MpProtectedModeWakeupCommandWakeup
je MailBoxWakeUp
+ cmp dword [rbx + CommandOffset], MpProtectedModeWakeupCommandTest
+ je MailBoxTest
cmp dword [rbx + CommandOffset], MpProtectedModeWakeupCommandSleep
je MailBoxSleep
; Don't support this command, so ignore
@@ -72,10 +83,43 @@ MailBoxWakeUp:
; the command field back to zero as acknowledgement.
mov qword [rbx + CommandOffset], 0
jmp rax
+MailBoxTest:
+ mov qword [rbx + CommandOffset], 0
+ jmp MailBoxLoop
MailBoxSleep:
jmp $
Panic:
ud2
+AsmRelocateApResetVector:
+ ; FIXME: handle switch to paging mode matching CR3
+ mov rbx, qword[rel mailbox_address]
+ mov rax, qword[rel saved_cr0]
+ mov cr0, rax
+ mov rax, qword[rel saved_cr4]
+ mov cr4, rax
+ mov rax, qword[rel saved_cr3]
+ mov cr3, rax
+ lea rax, qword[rel saved_gdt]
+ lgdt [rax]
+ mov rax, 0x18 ; FIXME: Magic number
+ mov ds, rax
+ mov es, rax
+ mov fs, rax
+ mov gs, rax
+ mov ss, rax
+ ; FIXME: swtich to the right CS descriptor
+ jmp AsmRelocateApMailBoxLoopStart
+align 16
+mailbox_address:
+ dq 0
+saved_cr0:
+ dq 0
+saved_cr3:
+ dq 0
+saved_cr4:
+ dq 0
+saved_gdt:
+ db 10 dup 0
BITS 64
AsmRelocateApMailBoxLoopEnd:
@@ -87,5 +131,7 @@ ASM_PFX(AsmGetRelocationMap):
lea rax, [AsmRelocateApMailBoxLoopStart]
mov qword [rcx], rax
mov qword [rcx + 8h], AsmRelocateApMailBoxLoopEnd - AsmRelocateApMailBoxLoopStart
+ lea rax, [AsmRelocateApResetVector]
+ mov qword [rcx + 10h], rax
ret
--
2.41.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment