From 6f9b1267e19f43e7a51351e9c612492942db5017 Mon Sep 17 00:00:00 2001
From: Xin Jiang <jiangxin@hygon.cn>
Date: Wed, 16 Aug 2023 19:53:27 +0800
Subject: [PATCH 10/11] OvmfPkg: Reserve a CPUID table page for CSV guest

Reserve a page for CPUID table which will be initialized by firmware.
In future, A CSV guest should get CPUID value from a CPUID table
which has been validated by firmware rather than requesting them from
hypervisor via a VMGEXIT.

Signed-off-by: Xin Jiang <jiangxin@hygon.cn>
---
 OvmfPkg/AmdSev/AmdSevX64.fdf                 |  5 ++++-
 OvmfPkg/OvmfPkg.dec                          |  4 ++++
 OvmfPkg/OvmfPkgX64.fdf                       |  5 ++++-
 OvmfPkg/PlatformPei/Csv.c                    |  6 ++++++
 OvmfPkg/PlatformPei/PlatformPei.inf          |  2 ++
 OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm | 15 +++++++++++++++
 OvmfPkg/ResetVector/ResetVector.inf          |  2 ++
 OvmfPkg/ResetVector/ResetVector.nasmb        |  2 ++
 8 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/OvmfPkg/AmdSev/AmdSevX64.fdf b/OvmfPkg/AmdSev/AmdSevX64.fdf
index 5d5612a..714ab00 100644
--- a/OvmfPkg/AmdSev/AmdSevX64.fdf
+++ b/OvmfPkg/AmdSev/AmdSevX64.fdf
@@ -77,7 +77,10 @@ gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCpuidBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCp
 0x010000|0x002000
 gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallBase|gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallSize
 
-0x012000|0x00E000
+0x012000|0x001000
+gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidSize
+
+0x013000|0x00D000
 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize
 
 0x020000|0x0E0000
diff --git a/OvmfPkg/OvmfPkg.dec b/OvmfPkg/OvmfPkg.dec
index 593a536..34bca30 100644
--- a/OvmfPkg/OvmfPkg.dec
+++ b/OvmfPkg/OvmfPkg.dec
@@ -438,6 +438,10 @@
   gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallBase|0|UINT32|0x70
   gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallSize|0|UINT32|0x71
 
+  ## the base address of the cpuid table page used by CSV.
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidBase|0|UINT32|0x72
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidSize|0|UINT32|0x73
+
 [PcdsDynamic, PcdsDynamicEx]
   gUefiOvmfPkgTokenSpaceGuid.PcdEmuVariableEvent|0|UINT64|2
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashVariablesEnable|FALSE|BOOLEAN|0x10
diff --git a/OvmfPkg/OvmfPkgX64.fdf b/OvmfPkg/OvmfPkgX64.fdf
index 597c9de..b1cf0d9 100644
--- a/OvmfPkg/OvmfPkgX64.fdf
+++ b/OvmfPkg/OvmfPkgX64.fdf
@@ -97,7 +97,10 @@ gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCpuidBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCp
 0x00F000|0x002000
 gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallBase|gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallSize
 
-0x011000|0x00F000
+0x011000|0x001000
+gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidSize
+
+0x012000|0x00E000
 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamBase|gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecPeiTempRamSize
 
 0x020000|0x0E0000
diff --git a/OvmfPkg/PlatformPei/Csv.c b/OvmfPkg/PlatformPei/Csv.c
index 5ab8331..a52112d 100644
--- a/OvmfPkg/PlatformPei/Csv.c
+++ b/OvmfPkg/PlatformPei/Csv.c
@@ -57,6 +57,12 @@ CsvInitializeMemInfo (
     (UINT64)(UINTN) FixedPcdGet32 (PcdCsvDefaultSecureCallSize),
     EfiReservedMemoryType
     );
+
+  BuildMemoryAllocationHob (
+    (EFI_PHYSICAL_ADDRESS)(UINTN) FixedPcdGet32 (PcdOvmfCsvCpuidBase),
+    (UINT64)(UINTN) FixedPcdGet32 (PcdOvmfCsvCpuidSize),
+    EfiReservedMemoryType
+    );
 }
 
 VOID
diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf b/OvmfPkg/PlatformPei/PlatformPei.inf
index 45d1688..07de179 100644
--- a/OvmfPkg/PlatformPei/PlatformPei.inf
+++ b/OvmfPkg/PlatformPei/PlatformPei.inf
@@ -135,6 +135,8 @@
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSnpSecretsSize
   gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallBase
   gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallSize
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidBase
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidSize
 
 [FeaturePcd]
   gUefiOvmfPkgTokenSpaceGuid.PcdCsmEnable
diff --git a/OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm b/OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm
index c86b049..dc7348e 100644
--- a/OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm
+++ b/OvmfPkg/ResetVector/Ia16/ResetVectorVtf0.asm
@@ -63,6 +63,21 @@ csvSecureCallBase:
     DB      0xAB, 0x21, 0x6B, 0x27, 0x5D, 0x55, 0x56, 0xA5
 csvSecureCallEnd:
 
+;
+; CSV cpuid table
+;
+; Provide cpuid table page when boot up for CSV guest
+;
+; GUID : 1b4c70e6-07e6-4e4e-8f28-0eaf871a0752
+;
+csvCpuidTableBase:
+    DD      CSV_CPUID_TABLE_SIZE
+    DD      CSV_CPUID_TABLE_BASE
+    DW      csvCpuidTableEnd - csvCpuidTableBase
+    DB      0xE6, 0x70, 0x4C, 0x1B, 0xE6, 0x07, 0x4E, 0x4E
+    DB      0x8F, 0x28, 0x0E, 0xAF, 0x87, 0x1A, 0x07, 0x52
+csvCpuidTableEnd:
+
 ;
 ; TDX Metadata offset block
 ;
diff --git a/OvmfPkg/ResetVector/ResetVector.inf b/OvmfPkg/ResetVector/ResetVector.inf
index e4adedb..5dfba88 100644
--- a/OvmfPkg/ResetVector/ResetVector.inf
+++ b/OvmfPkg/ResetVector/ResetVector.inf
@@ -37,6 +37,8 @@
   gUefiCpuPkgTokenSpaceGuid.PcdSevEsWorkAreaBase
   gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallBase
   gUefiOvmfPkgTokenSpaceGuid.PcdCsvDefaultSecureCallSize
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidBase
+  gUefiOvmfPkgTokenSpaceGuid.PcdOvmfCsvCpuidSize
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbBase
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbSize
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSecGhcbPageTableBase
diff --git a/OvmfPkg/ResetVector/ResetVector.nasmb b/OvmfPkg/ResetVector/ResetVector.nasmb
index d156a51..da4068b 100644
--- a/OvmfPkg/ResetVector/ResetVector.nasmb
+++ b/OvmfPkg/ResetVector/ResetVector.nasmb
@@ -125,6 +125,8 @@
 
   %define CSV_DEFAULT_SECURE_CALL_BASE FixedPcdGet32 (PcdCsvDefaultSecureCallBase)
   %define CSV_DEFAULT_SECURE_CALL_SIZE FixedPcdGet32 (PcdCsvDefaultSecureCallSize)
+  %define CSV_CPUID_TABLE_BASE FixedPcdGet32 (PcdOvmfCsvCpuidBase)
+  %define CSV_CPUID_TABLE_SIZE FixedPcdGet32 (PcdOvmfCsvCpuidSize)
 
   %include "X64/IntelTdxMetadata.asm"
   %include "Ia32/Flat32ToFlat64.asm"
-- 
2.25.1

