public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl
@ 2023-03-31  9:33 duntan
  2023-03-31  9:33 ` [Patch V2 1/8] EmulatorPkg: Add CpuPageTableLib required by DxeIpl in DSC duntan
                   ` (10 more replies)
  0 siblings, 11 replies; 32+ messages in thread
From: duntan @ 2023-03-31  9:33 UTC (permalink / raw)
  To: devel

In this V2 patch set:
1.Remove the unneeded patch for ArmVirtPkg
2.In this patch 'Create page table by CpuPageTableLib', change the input parameter name from Is32BitPageTable to Is64BitPageTable and add a line of "MapAttribute.Bits.Present = 0" before set a range to non-present.
3.In this patch 'Refinement to the code to set PageTable as RO', add a line of "MapAttribute.Bits.ReadWrite = 0" before set a range to ReadOnly.

Dun Tan (8):
  EmulatorPkg: Add CpuPageTableLib required by DxeIpl in DSC
  IntelFsp2Pkg: Add CpuPageTableLib required by DxeIpl in DSC
  MdeModulePkg: Add CpuPageTableLib required by DxeIpl in DSC
  OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file
  MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  MdeModulePkg/DxeIpl: Create page table by CpuPageTableLib
  MdeModulePkg/DxeIpl: Remove duplicated code to enable NX
  MdeModulePkg/DxeIpl: Refinement to the code to set PageTable as RO

 EmulatorPkg/EmulatorPkg.dsc                      |   3 ++-
 IntelFsp2Pkg/Tools/Tests/QemuFspPkg.dsc          |   3 ++-
 MdeModulePkg/Core/DxeIplPeim/DxeIpl.h            |   3 ++-
 MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf          |   4 +++-
 MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c  | 112 ++++------------------------------------------------------------------------------------------------------------
 MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c   |   5 +++--
 MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c | 711 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h | 182 ++++++++++----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 MdeModulePkg/MdeModulePkg.ci.yaml                |   5 +++--
 MdeModulePkg/MdeModulePkg.dsc                    |   3 ++-
 OvmfPkg/AmdSev/AmdSevX64.dsc                     |   2 +-
 OvmfPkg/Bhyve/BhyveX64.dsc                       |   3 ++-
 OvmfPkg/CloudHv/CloudHvX64.dsc                   |   2 +-
 OvmfPkg/Microvm/MicrovmX64.dsc                   |   2 +-
 OvmfPkg/OvmfPkgIa32.dsc                          |   3 ++-
 OvmfPkg/OvmfPkgIa32X64.dsc                       |   2 +-
 OvmfPkg/OvmfPkgX64.dsc                           |   2 +-
 OvmfPkg/OvmfXen.dsc                              |   2 +-
 18 files changed, 200 insertions(+), 849 deletions(-)

-- 
2.31.1.windows.1


^ permalink raw reply	[flat|nested] 32+ messages in thread

* [Patch V2 1/8] EmulatorPkg: Add CpuPageTableLib required by DxeIpl in DSC
  2023-03-31  9:33 [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl duntan
@ 2023-03-31  9:33 ` duntan
  2023-03-31 10:21   ` Ni, Ray
  2023-03-31  9:33 ` [Patch V2 2/8] IntelFsp2Pkg: " duntan
                   ` (9 subsequent siblings)
  10 siblings, 1 reply; 32+ messages in thread
From: duntan @ 2023-03-31  9:33 UTC (permalink / raw)
  To: devel; +Cc: Andrew Fish, Ray Ni

Add CpuPageTableLib instance required by DxeIpl in EmulatorPkg.dsc.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Andrew Fish <afish@apple.com>
Cc: Ray Ni <ray.ni@intel.com>
---
 EmulatorPkg/EmulatorPkg.dsc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/EmulatorPkg/EmulatorPkg.dsc b/EmulatorPkg/EmulatorPkg.dsc
index b44435d7e6..d1fb9d9256 100644
--- a/EmulatorPkg/EmulatorPkg.dsc
+++ b/EmulatorPkg/EmulatorPkg.dsc
@@ -4,7 +4,7 @@
 # The Emulation Platform can be used to debug individual modules, prior to creating
 # a real platform. This also provides an example for how an DSC is created.
 #
-# Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
 # Portions copyright (c) 2010 - 2011, Apple Inc. All rights reserved.<BR>
 # Copyright (c) Microsoft Corporation.
 #
@@ -66,6 +66,7 @@
   PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/BasePeCoffGetEntryPointLib.inf
   BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
   FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
   #
   # UEFI & PI
-- 
2.31.1.windows.1


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [Patch V2 2/8] IntelFsp2Pkg: Add CpuPageTableLib required by DxeIpl in DSC
  2023-03-31  9:33 [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl duntan
  2023-03-31  9:33 ` [Patch V2 1/8] EmulatorPkg: Add CpuPageTableLib required by DxeIpl in DSC duntan
@ 2023-03-31  9:33 ` duntan
  2023-03-31  9:33 ` [Patch V2 3/8] MdeModulePkg: " duntan
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: duntan @ 2023-03-31  9:33 UTC (permalink / raw)
  To: devel; +Cc: Chasel Chiu, Nate DeSimone, Star Zeng, Ray Ni

Add CpuPageTableLib instance required by DxeIpl in QemuFspPkg.dsc
of IntelFsp2Pkg.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Reviewed-by: Chasel Chiu <chasel.chiu@intel.com>
Cc: Nate DeSimone <nathaniel.l.desimone@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
---
 IntelFsp2Pkg/Tools/Tests/QemuFspPkg.dsc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/IntelFsp2Pkg/Tools/Tests/QemuFspPkg.dsc b/IntelFsp2Pkg/Tools/Tests/QemuFspPkg.dsc
index 3155812118..52052692dd 100644
--- a/IntelFsp2Pkg/Tools/Tests/QemuFspPkg.dsc
+++ b/IntelFsp2Pkg/Tools/Tests/QemuFspPkg.dsc
@@ -1,7 +1,7 @@
 ## @file
 # FSP DSC build file for QEMU platform
 #
-# Copyright (c) 2017 - 2021, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2017 - 2023, Intel Corporation. All rights reserved.<BR>
 #
 #    This program and the accompanying materials
 #    are licensed and made available under the terms and conditions of the BSD License
@@ -114,6 +114,7 @@
   DebugLib|MdePkg/Library/BaseDebugLibNull/BaseDebugLibNull.inf
   SerialPortLib|MdePkg/Library/BaseSerialPortLibNull/BaseSerialPortLibNull.inf
 !endif
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 
 ################################################################################
-- 
2.31.1.windows.1


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [Patch V2 3/8] MdeModulePkg: Add CpuPageTableLib required by DxeIpl in DSC
  2023-03-31  9:33 [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl duntan
  2023-03-31  9:33 ` [Patch V2 1/8] EmulatorPkg: Add CpuPageTableLib required by DxeIpl in DSC duntan
  2023-03-31  9:33 ` [Patch V2 2/8] IntelFsp2Pkg: " duntan
@ 2023-03-31  9:33 ` duntan
  2023-04-14  9:09   ` Wang, Jian J
  2023-03-31  9:33 ` [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file duntan
                   ` (7 subsequent siblings)
  10 siblings, 1 reply; 32+ messages in thread
From: duntan @ 2023-03-31  9:33 UTC (permalink / raw)
  To: devel; +Cc: Jian J Wang, Liming Gao, Ray Ni

Add CpuPageTableLib instance required by DxeIpl in
MdeModulePkg.dsc.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Ray Ni <ray.ni@intel.com>
---
 MdeModulePkg/MdeModulePkg.dsc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 1014598f31..d95acabe83 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -2,7 +2,7 @@
 # EFI/PI Reference Module Package for All Architectures
 #
 # (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
-# Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2007 - 2023, Intel Corporation. All rights reserved.<BR>
 # Copyright (c) Microsoft Corporation.
 # Copyright (C) 2023 Advanced Micro Devices, Inc. All rights reserved.<BR>
 #
@@ -106,6 +106,7 @@
   MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockMemoryLibNull.inf
   VariableFlashInfoLib|MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
   IpmiCommandLib|MdeModulePkg/Library/BaseIpmiCommandLibNull/BaseIpmiCommandLibNull.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 [LibraryClasses.EBC.PEIM]
   IoLib|MdePkg/Library/PeiIoLibCpuIo/PeiIoLibCpuIo.inf
-- 
2.31.1.windows.1


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file
  2023-03-31  9:33 [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl duntan
                   ` (2 preceding siblings ...)
  2023-03-31  9:33 ` [Patch V2 3/8] MdeModulePkg: " duntan
@ 2023-03-31  9:33 ` duntan
  2023-03-31 11:50   ` [edk2-devel] " Gerd Hoffmann
  2023-03-31  9:33 ` [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck duntan
                   ` (6 subsequent siblings)
  10 siblings, 1 reply; 32+ messages in thread
From: duntan @ 2023-03-31  9:33 UTC (permalink / raw)
  To: devel; +Cc: Ard Biesheuvel, Jiewen Yao, Jordan Justen, Gerd Hoffmann, Ray Ni

Add CpuPageTableLib instance required by DxeIpl in corresponding
DSC files of OvmfPkg.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
---
 OvmfPkg/AmdSev/AmdSevX64.dsc   | 2 +-
 OvmfPkg/Bhyve/BhyveX64.dsc     | 3 ++-
 OvmfPkg/CloudHv/CloudHvX64.dsc | 2 +-
 OvmfPkg/Microvm/MicrovmX64.dsc | 2 +-
 OvmfPkg/OvmfPkgIa32.dsc        | 3 ++-
 OvmfPkg/OvmfPkgIa32X64.dsc     | 2 +-
 OvmfPkg/OvmfPkgX64.dsc         | 2 +-
 OvmfPkg/OvmfXen.dsc            | 2 +-
 8 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/OvmfPkg/AmdSev/AmdSevX64.dsc b/OvmfPkg/AmdSev/AmdSevX64.dsc
index c005e474dd..3a23e38263 100644
--- a/OvmfPkg/AmdSev/AmdSevX64.dsc
+++ b/OvmfPkg/AmdSev/AmdSevX64.dsc
@@ -169,6 +169,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 !if $(SOURCE_DEBUG_ENABLE) == TRUE
   PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLibDebug.inf
@@ -352,7 +353,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/Bhyve/BhyveX64.dsc b/OvmfPkg/Bhyve/BhyveX64.dsc
index d0d2712c56..67f8a77c3a 100644
--- a/OvmfPkg/Bhyve/BhyveX64.dsc
+++ b/OvmfPkg/Bhyve/BhyveX64.dsc
@@ -1,6 +1,6 @@
 #
 #  Copyright (c) 2020, Rebecca Cran <rebecca@bsdio.com>
-#  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
 #  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
 #  Copyright (c) 2014, Pluribus Networks, Inc.
 #
@@ -171,6 +171,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
   CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf
   FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf
diff --git a/OvmfPkg/CloudHv/CloudHvX64.dsc b/OvmfPkg/CloudHv/CloudHvX64.dsc
index b9820cc14b..ffc65b0e15 100644
--- a/OvmfPkg/CloudHv/CloudHvX64.dsc
+++ b/OvmfPkg/CloudHv/CloudHvX64.dsc
@@ -190,6 +190,7 @@
   MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 !if $(SMM_REQUIRE) == FALSE
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
 !endif
@@ -403,7 +404,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/Microvm/MicrovmX64.dsc b/OvmfPkg/Microvm/MicrovmX64.dsc
index 384b0b7afc..aa74a9d5ad 100644
--- a/OvmfPkg/Microvm/MicrovmX64.dsc
+++ b/OvmfPkg/Microvm/MicrovmX64.dsc
@@ -193,6 +193,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 !if $(SOURCE_DEBUG_ENABLE) == TRUE
   PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLibDebug.inf
@@ -402,7 +403,6 @@
   PciLib|MdePkg/Library/BasePciLibPciExpress/BasePciLibPciExpress.inf
   PciPcdProducerLib|OvmfPkg/Fdt/FdtPciPcdProducerLib/FdtPciPcdProducerLib.inf
   PciExpressLib|OvmfPkg/Library/BaseCachingPciExpressLib/BaseCachingPciExpressLib.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc
index a6db902f54..6352c84759 100644
--- a/OvmfPkg/OvmfPkgIa32.dsc
+++ b/OvmfPkg/OvmfPkgIa32.dsc
@@ -1,7 +1,7 @@
 ## @file
 #  EFI/Framework Open Virtual Machine Firmware (OVMF) platform
 #
-#  Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
 #  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
 #  Copyright (c) Microsoft Corporation.
 #
@@ -193,6 +193,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLibNull.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 !if $(SMM_REQUIRE) == FALSE
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
 !endif
diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc
index 076fc0353d..9bec68c733 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -197,6 +197,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLibNull.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 !if $(SMM_REQUIRE) == FALSE
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
 !endif
@@ -413,7 +414,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index b2f3d14cd9..9e8aaede09 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -210,6 +210,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 !if $(SMM_REQUIRE) == FALSE
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
@@ -434,7 +435,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc
index 990225d2dd..806e6e064e 100644
--- a/OvmfPkg/OvmfXen.dsc
+++ b/OvmfPkg/OvmfXen.dsc
@@ -173,6 +173,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 !if $(SOURCE_DEBUG_ENABLE) == TRUE
   PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLibDebug.inf
@@ -338,7 +339,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
-- 
2.31.1.windows.1


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-03-31  9:33 [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl duntan
                   ` (3 preceding siblings ...)
  2023-03-31  9:33 ` [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file duntan
@ 2023-03-31  9:33 ` duntan
  2023-04-14  9:03   ` Wang, Jian J
  2023-03-31  9:33 ` [Patch V2 6/8] MdeModulePkg/DxeIpl: Create page table by CpuPageTableLib duntan
                   ` (5 subsequent siblings)
  10 siblings, 1 reply; 32+ messages in thread
From: duntan @ 2023-03-31  9:33 UTC (permalink / raw)
  To: devel; +Cc: Liming Gao, Ray Ni, Jian J Wang

Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
DependencyCheck since DxeIpl in MdeModulePkg needs to consume
CpuPageTableLib in UefiCpuPkg.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
---
 MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml b/MdeModulePkg/MdeModulePkg.ci.yaml
index f69989087b..d2616f4cdc 100644
--- a/MdeModulePkg/MdeModulePkg.ci.yaml
+++ b/MdeModulePkg/MdeModulePkg.ci.yaml
@@ -2,7 +2,7 @@
 # CI configuration for MdeModulePkg
 #
 # Copyright (c) Microsoft Corporation
-# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
 # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
 # SPDX-License-Identifier: BSD-2-Clause-Patent
 ##
@@ -51,7 +51,8 @@
             "MdePkg/MdePkg.dec",
             "MdeModulePkg/MdeModulePkg.dec",
             "StandaloneMmPkg/StandaloneMmPkg.dec",
-            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an abstraction
+            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an abstraction
+            "UefiCpuPkg/UefiCpuPkg.dec"
         ],
         # For host based unit tests
         "AcceptableDependencies-HOST_APPLICATION":[
-- 
2.31.1.windows.1


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [Patch V2 6/8] MdeModulePkg/DxeIpl: Create page table by CpuPageTableLib
  2023-03-31  9:33 [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl duntan
                   ` (4 preceding siblings ...)
  2023-03-31  9:33 ` [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck duntan
@ 2023-03-31  9:33 ` duntan
  2023-03-31 10:24   ` Ni, Ray
  2023-03-31  9:33 ` [Patch V2 7/8] MdeModulePkg/DxeIpl: Remove duplicated code to enable NX duntan
                   ` (4 subsequent siblings)
  10 siblings, 1 reply; 32+ messages in thread
From: duntan @ 2023-03-31  9:33 UTC (permalink / raw)
  To: devel; +Cc: Dandan Bi, Liming Gao, Ray Ni, Jian J Wang

Modify CreateIdentityMappingPageTables() to create page table
based on CpuPageTableLib in DxeIpl module. This function can
be used to create both IA32 PAE paging and long mode 4-level,
5-level paging structure. With the PageTableMap() API in the
CpuPageTableLib, we can remove the complicated page table
manipulating code. This commit doesn't change any functionality.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
---
 MdeModulePkg/Core/DxeIplPeim/DxeIpl.h            |   3 ++-
 MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf          |   4 +++-
 MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c  | 109 ++++---------------------------------------------------------------------------------------------------------
 MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c   |   5 +++--
 MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c | 558 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h | 167 ++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------------------
 6 files changed, 167 insertions(+), 679 deletions(-)

diff --git a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.h b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.h
index 2f015befce..03e6f8cff7 100644
--- a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.h
+++ b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.h
@@ -2,7 +2,7 @@
   Master header file for DxeIpl PEIM. All source files in this module should
   include this file for common definitions.
 
-Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -42,6 +42,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Library/DebugAgentLib.h>
 #include <Library/PeiServicesTablePointerLib.h>
 #include <Library/PerformanceLib.h>
+#include <Library/CpuPageTableLib.h>
 
 #define STACK_SIZE      0x20000
 #define BSP_STORE_SIZE  0x4000
diff --git a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf
index 052ea0ec1a..60623b4f66 100644
--- a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf
+++ b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf
@@ -5,7 +5,7 @@
 #  PPI to discover and dispatch the DXE Foundation and components that are
 #  needed to run the DXE Foundation.
 #
-#  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
 #  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
 #  Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
 #  Copyright (c) 2022, Loongson Technology Corporation Limited. All rights reserved.<BR>
@@ -60,6 +60,7 @@
 [Packages]
   MdePkg/MdePkg.dec
   MdeModulePkg/MdeModulePkg.dec
+  UefiCpuPkg/UefiCpuPkg.dec
 
 [Packages.ARM, Packages.AARCH64]
   ArmPkg/ArmPkg.dec
@@ -79,6 +80,7 @@
   DebugAgentLib
   PeiServicesTablePointerLib
   PerformanceLib
+  CpuPageTableLib
 
 [LibraryClasses.ARM, LibraryClasses.AARCH64]
   ArmMmuLib
diff --git a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c b/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
index fdeaaa39d8..af1e1e3d02 100644
--- a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
+++ b/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
@@ -1,7 +1,7 @@
 /** @file
   Ia32-specific functionality for DxeLoad.
 
-Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
 
 SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -70,107 +70,6 @@ GLOBAL_REMOVE_IF_UNREFERENCED  IA32_DESCRIPTOR  gLidtDescriptor = {
   0
 };
 
-/**
-  Allocates and fills in the Page Directory and Page Table Entries to
-  establish a 4G page table.
-
-  @param[in] StackBase  Stack base address.
-  @param[in] StackSize  Stack size.
-
-  @return The address of page table.
-
-**/
-UINTN
-Create4GPageTablesIa32Pae (
-  IN EFI_PHYSICAL_ADDRESS  StackBase,
-  IN UINTN                 StackSize
-  )
-{
-  UINT8                           PhysicalAddressBits;
-  EFI_PHYSICAL_ADDRESS            PhysicalAddress;
-  UINTN                           IndexOfPdpEntries;
-  UINTN                           IndexOfPageDirectoryEntries;
-  UINT32                          NumberOfPdpEntriesNeeded;
-  PAGE_MAP_AND_DIRECTORY_POINTER  *PageMap;
-  PAGE_MAP_AND_DIRECTORY_POINTER  *PageDirectoryPointerEntry;
-  PAGE_TABLE_ENTRY                *PageDirectoryEntry;
-  UINTN                           TotalPagesNum;
-  UINTN                           PageAddress;
-  UINT64                          AddressEncMask;
-
-  //
-  // Make sure AddressEncMask is contained to smallest supported address field
-  //
-  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
-
-  PhysicalAddressBits = 32;
-
-  //
-  // Calculate the table entries needed.
-  //
-  NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits - 30));
-
-  TotalPagesNum = NumberOfPdpEntriesNeeded + 1;
-  PageAddress   = (UINTN)AllocatePageTableMemory (TotalPagesNum);
-  ASSERT (PageAddress != 0);
-
-  PageMap      = (VOID *)PageAddress;
-  PageAddress += SIZE_4KB;
-
-  PageDirectoryPointerEntry = PageMap;
-  PhysicalAddress           = 0;
-
-  for (IndexOfPdpEntries = 0; IndexOfPdpEntries < NumberOfPdpEntriesNeeded; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
-    //
-    // Each Directory Pointer entries points to a page of Page Directory entires.
-    // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.
-    //
-    PageDirectoryEntry = (VOID *)PageAddress;
-    PageAddress       += SIZE_4KB;
-
-    //
-    // Fill in a Page Directory Pointer Entries
-    //
-    PageDirectoryPointerEntry->Uint64       = (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask;
-    PageDirectoryPointerEntry->Bits.Present = 1;
-
-    for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress += SIZE_2MB) {
-      if (  (IsNullDetectionEnabled () && (PhysicalAddress == 0))
-         || (  (PhysicalAddress < StackBase + StackSize)
-            && ((PhysicalAddress + SIZE_2MB) > StackBase)))
-      {
-        //
-        // Need to split this 2M page that covers stack range.
-        //
-        Split2MPageTo4K (PhysicalAddress, (UINT64 *)PageDirectoryEntry, StackBase, StackSize, 0, 0);
-      } else {
-        //
-        // Fill in the Page Directory entries
-        //
-        PageDirectoryEntry->Uint64         = (UINT64)PhysicalAddress | AddressEncMask;
-        PageDirectoryEntry->Bits.ReadWrite = 1;
-        PageDirectoryEntry->Bits.Present   = 1;
-        PageDirectoryEntry->Bits.MustBe1   = 1;
-      }
-    }
-  }
-
-  for ( ; IndexOfPdpEntries < 512; IndexOfPdpEntries++, PageDirectoryPointerEntry++) {
-    ZeroMem (
-      PageDirectoryPointerEntry,
-      sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)
-      );
-  }
-
-  //
-  // Protect the page table by marking the memory used for page table to be
-  // read-only.
-  //
-  EnablePageTableProtection ((UINTN)PageMap, FALSE);
-
-  return (UINTN)PageMap;
-}
-
 /**
   The function will check if IA32 PAE is supported.
 
@@ -299,9 +198,9 @@ HandOffToDxeCore (
     //
     AsmWriteGdtr (&gGdt);
     //
-    // Create page table and save PageMapLevel4 to CR3
+    // Create page table and save PageMapLevel4 or PageMapLevel5 to CR3
     //
-    PageTables = CreateIdentityMappingPageTables (BaseOfStack, STACK_SIZE, 0, 0);
+    PageTables = CreateIdentityMappingPageTables (TRUE, BaseOfStack, STACK_SIZE, 0, 0);
 
     //
     // End of PEI phase signal
@@ -422,7 +321,7 @@ HandOffToDxeCore (
     PageTables             = 0;
     BuildPageTablesIa32Pae = ToBuildPageTable ();
     if (BuildPageTablesIa32Pae) {
-      PageTables = Create4GPageTablesIa32Pae (BaseOfStack, STACK_SIZE);
+      PageTables = CreateIdentityMappingPageTables (FALSE, BaseOfStack, STACK_SIZE, 0, 0);
       if (IsEnableNonExecNeeded ()) {
         EnableExecuteDisableBit ();
       }
diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c b/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c
index fa2050cf02..2642092ee5 100644
--- a/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c
+++ b/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c
@@ -1,7 +1,7 @@
 /** @file
   x64-specifc functionality for DxeLoad.
 
-Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -91,9 +91,10 @@ HandOffToDxeCore (
   PageTables = 0;
   if (FeaturePcdGet (PcdDxeIplBuildPageTables)) {
     //
-    // Create page table and save PageMapLevel4 to CR3
+    // Create page table and save PageMapLevel4 or PageMapLevel5 to CR3
     //
     PageTables = CreateIdentityMappingPageTables (
+                   TRUE,
                    (EFI_PHYSICAL_ADDRESS)(UINTN)BaseOfStack,
                    STACK_SIZE,
                    (EFI_PHYSICAL_ADDRESS)(UINTN)GhcbBase,
diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
index 18b121d768..ecdbd2ca24 100644
--- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
+++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
@@ -15,7 +15,7 @@
     2) IA-32 Intel(R) Architecture Software Developer's Manual Volume 2:Instruction Set Reference, Intel
     3) IA-32 Intel(R) Architecture Software Developer's Manual Volume 3:System Programmer's Guide, Intel
 
-Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
 
 SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -186,55 +186,6 @@ EnableExecuteDisableBit (
   }
 }
 
-/**
-  The function will check if page table entry should be splitted to smaller
-  granularity.
-
-  @param Address      Physical memory address.
-  @param Size         Size of the given physical memory.
-  @param StackBase    Base address of stack.
-  @param StackSize    Size of stack.
-  @param GhcbBase     Base address of GHCB pages.
-  @param GhcbSize     Size of GHCB area.
-
-  @retval TRUE      Page table should be split.
-  @retval FALSE     Page table should not be split.
-**/
-BOOLEAN
-ToSplitPageTable (
-  IN EFI_PHYSICAL_ADDRESS  Address,
-  IN UINTN                 Size,
-  IN EFI_PHYSICAL_ADDRESS  StackBase,
-  IN UINTN                 StackSize,
-  IN EFI_PHYSICAL_ADDRESS  GhcbBase,
-  IN UINTN                 GhcbSize
-  )
-{
-  if (IsNullDetectionEnabled () && (Address == 0)) {
-    return TRUE;
-  }
-
-  if (PcdGetBool (PcdCpuStackGuard)) {
-    if ((StackBase >= Address) && (StackBase < (Address + Size))) {
-      return TRUE;
-    }
-  }
-
-  if (PcdGetBool (PcdSetNxForStack)) {
-    if ((Address < StackBase + StackSize) && ((Address + Size) > StackBase)) {
-      return TRUE;
-    }
-  }
-
-  if (GhcbBase != 0) {
-    if ((Address < GhcbBase + GhcbSize) && ((Address + Size) > GhcbBase)) {
-      return TRUE;
-    }
-  }
-
-  return FALSE;
-}
-
 /**
   Initialize a buffer pool for page table use only.
 
@@ -341,143 +292,42 @@ AllocatePageTableMemory (
 }
 
 /**
-  Split 2M page to 4K.
-
-  @param[in]      PhysicalAddress       Start physical address the 2M page covered.
-  @param[in, out] PageEntry2M           Pointer to 2M page entry.
-  @param[in]      StackBase             Stack base address.
-  @param[in]      StackSize             Stack size.
-  @param[in]      GhcbBase              GHCB page area base address.
-  @param[in]      GhcbSize              GHCB page area size.
-
+  This function create new page table or modifies the page MapAttribute for the memory region
+  specified by BaseAddress and Length from their current attributes to the attributes specified
+  by MapAttribute and Mask.
+
+  @param[in] PageTable        Pointer to Page table address.
+  @param[in] PagingMode       The paging mode.
+  @param[in] BaseAddress      The start of the linear address range.
+  @param[in] Length           The length of the linear address range.
+  @param[in] MapAttribute     The attribute of the linear address range.
+  @param[in] MapMask          The mask used for attribute.
 **/
 VOID
-Split2MPageTo4K (
-  IN EFI_PHYSICAL_ADDRESS  PhysicalAddress,
-  IN OUT UINT64            *PageEntry2M,
-  IN EFI_PHYSICAL_ADDRESS  StackBase,
-  IN UINTN                 StackSize,
-  IN EFI_PHYSICAL_ADDRESS  GhcbBase,
-  IN UINTN                 GhcbSize
+CreateOrUpdatePageTable (
+  IN  UINTN               *PageTable,
+  IN  PAGING_MODE         PagingMode,
+  IN  PHYSICAL_ADDRESS    BaseAddress,
+  IN  UINT64              Length,
+  IN  IA32_MAP_ATTRIBUTE  *MapAttribute,
+  IN  IA32_MAP_ATTRIBUTE  *MapMask
   )
 {
-  EFI_PHYSICAL_ADDRESS  PhysicalAddress4K;
-  UINTN                 IndexOfPageTableEntries;
-  PAGE_TABLE_4K_ENTRY   *PageTableEntry;
-  UINT64                AddressEncMask;
-
-  //
-  // Make sure AddressEncMask is contained to smallest supported address field
-  //
-  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
-
-  PageTableEntry = AllocatePageTableMemory (1);
-  ASSERT (PageTableEntry != NULL);
-
-  //
-  // Fill in 2M page entry.
-  //
-  *PageEntry2M = (UINT64)(UINTN)PageTableEntry | AddressEncMask | IA32_PG_P | IA32_PG_RW;
-
-  PhysicalAddress4K = PhysicalAddress;
-  for (IndexOfPageTableEntries = 0; IndexOfPageTableEntries < 512; IndexOfPageTableEntries++, PageTableEntry++, PhysicalAddress4K += SIZE_4KB) {
-    //
-    // Fill in the Page Table entries
-    //
-    PageTableEntry->Uint64 = (UINT64)PhysicalAddress4K;
-
-    //
-    // The GHCB range consists of two pages per CPU, the GHCB and a
-    // per-CPU variable page. The GHCB page needs to be mapped as an
-    // unencrypted page while the per-CPU variable page needs to be
-    // mapped encrypted. These pages alternate in assignment.
-    //
-    if (  (GhcbBase == 0)
-       || (PhysicalAddress4K < GhcbBase)
-       || (PhysicalAddress4K >= GhcbBase + GhcbSize)
-       || (((PhysicalAddress4K - GhcbBase) & SIZE_4KB) != 0))
-    {
-      PageTableEntry->Uint64 |= AddressEncMask;
-    }
-
-    PageTableEntry->Bits.ReadWrite = 1;
-
-    if ((IsNullDetectionEnabled () && (PhysicalAddress4K == 0)) ||
-        (PcdGetBool (PcdCpuStackGuard) && (PhysicalAddress4K == StackBase)))
-    {
-      PageTableEntry->Bits.Present = 0;
-    } else {
-      PageTableEntry->Bits.Present = 1;
-    }
-
-    if (  PcdGetBool (PcdSetNxForStack)
-       && (PhysicalAddress4K >= StackBase)
-       && (PhysicalAddress4K < StackBase + StackSize))
-    {
-      //
-      // Set Nx bit for stack.
-      //
-      PageTableEntry->Bits.Nx = 1;
-    }
+  RETURN_STATUS  Status;
+  UINTN          PageTableBufferSize;
+  VOID           *PageTableBuffer;
+
+  PageTableBufferSize = 0;
+  Status              = PageTableMap (PageTable, PagingMode, NULL, &PageTableBufferSize, BaseAddress, Length, MapAttribute, MapMask, NULL);
+  if (Status == RETURN_BUFFER_TOO_SMALL) {
+    PageTableBuffer = AllocatePageTableMemory (EFI_SIZE_TO_PAGES (PageTableBufferSize));
+    DEBUG ((DEBUG_INFO, "DxeIpl: 0x%x bytes needed for page table\n", PageTableBufferSize));
+    ASSERT (PageTableBuffer != NULL);
+    Status = PageTableMap (PageTable, PagingMode, PageTableBuffer, &PageTableBufferSize, BaseAddress, Length, MapAttribute, MapMask, NULL);
   }
-}
-
-/**
-  Split 1G page to 2M.
 
-  @param[in]      PhysicalAddress       Start physical address the 1G page covered.
-  @param[in, out] PageEntry1G           Pointer to 1G page entry.
-  @param[in]      StackBase             Stack base address.
-  @param[in]      StackSize             Stack size.
-  @param[in]      GhcbBase              GHCB page area base address.
-  @param[in]      GhcbSize              GHCB page area size.
-
-**/
-VOID
-Split1GPageTo2M (
-  IN EFI_PHYSICAL_ADDRESS  PhysicalAddress,
-  IN OUT UINT64            *PageEntry1G,
-  IN EFI_PHYSICAL_ADDRESS  StackBase,
-  IN UINTN                 StackSize,
-  IN EFI_PHYSICAL_ADDRESS  GhcbBase,
-  IN UINTN                 GhcbSize
-  )
-{
-  EFI_PHYSICAL_ADDRESS  PhysicalAddress2M;
-  UINTN                 IndexOfPageDirectoryEntries;
-  PAGE_TABLE_ENTRY      *PageDirectoryEntry;
-  UINT64                AddressEncMask;
-
-  //
-  // Make sure AddressEncMask is contained to smallest supported address field
-  //
-  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
-
-  PageDirectoryEntry = AllocatePageTableMemory (1);
-  ASSERT (PageDirectoryEntry != NULL);
-
-  //
-  // Fill in 1G page entry.
-  //
-  *PageEntry1G = (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask | IA32_PG_P | IA32_PG_RW;
-
-  PhysicalAddress2M = PhysicalAddress;
-  for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress2M += SIZE_2MB) {
-    if (ToSplitPageTable (PhysicalAddress2M, SIZE_2MB, StackBase, StackSize, GhcbBase, GhcbSize)) {
-      //
-      // Need to split this 2M page that covers NULL or stack range.
-      //
-      Split2MPageTo4K (PhysicalAddress2M, (UINT64 *)PageDirectoryEntry, StackBase, StackSize, GhcbBase, GhcbSize);
-    } else {
-      //
-      // Fill in the Page Directory entries
-      //
-      PageDirectoryEntry->Uint64         = (UINT64)PhysicalAddress2M | AddressEncMask;
-      PageDirectoryEntry->Bits.ReadWrite = 1;
-      PageDirectoryEntry->Bits.Present   = 1;
-      PageDirectoryEntry->Bits.MustBe1   = 1;
-    }
-  }
+  ASSERT_RETURN_ERROR (Status);
+  ASSERT (PageTableBufferSize == 0);
 }
 
 /**
@@ -657,19 +507,20 @@ EnablePageTableProtection (
 }
 
 /**
-  Allocates and fills in the Page Directory and Page Table Entries to
+  Create IA32 PAE paging or 4-level/5-level paging for long mode to
   establish a 1:1 Virtual to Physical mapping.
 
-  @param[in] StackBase  Stack base address.
-  @param[in] StackSize  Stack size.
-  @param[in] GhcbBase   GHCB base address.
-  @param[in] GhcbSize   GHCB size.
-
-  @return The address of 4 level page map.
+  @param[in] Is64BitPageTable     Whether to create 64-bit page table.
+  @param[in] StackBase            Stack base address.
+  @param[in] StackSize            Stack size.
+  @param[in] GhcbBase             GHCB base address.
+  @param[in] GhcbSize             GHCB size.
 
+  @return  PageTable Address
 **/
 UINTN
 CreateIdentityMappingPageTables (
+  IN BOOLEAN               Is64BitPageTable,
   IN EFI_PHYSICAL_ADDRESS  StackBase,
   IN UINTN                 StackSize,
   IN EFI_PHYSICAL_ADDRESS  GhcbBase,
@@ -680,274 +531,155 @@ CreateIdentityMappingPageTables (
   CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_ECX  EcxFlags;
   UINT32                                       RegEdx;
   UINT8                                        PhysicalAddressBits;
-  EFI_PHYSICAL_ADDRESS                         PageAddress;
-  UINTN                                        IndexOfPml5Entries;
-  UINTN                                        IndexOfPml4Entries;
-  UINTN                                        IndexOfPdpEntries;
-  UINTN                                        IndexOfPageDirectoryEntries;
-  UINT32                                       NumberOfPml5EntriesNeeded;
-  UINT32                                       NumberOfPml4EntriesNeeded;
-  UINT32                                       NumberOfPdpEntriesNeeded;
-  PAGE_MAP_AND_DIRECTORY_POINTER               *PageMapLevel5Entry;
-  PAGE_MAP_AND_DIRECTORY_POINTER               *PageMapLevel4Entry;
-  PAGE_MAP_AND_DIRECTORY_POINTER               *PageMap;
-  PAGE_MAP_AND_DIRECTORY_POINTER               *PageDirectoryPointerEntry;
-  PAGE_TABLE_ENTRY                             *PageDirectoryEntry;
-  UINTN                                        TotalPagesNum;
-  UINTN                                        BigPageAddress;
   VOID                                         *Hob;
   BOOLEAN                                      Page5LevelSupport;
   BOOLEAN                                      Page1GSupport;
-  PAGE_TABLE_1G_ENTRY                          *PageDirectory1GEntry;
   UINT64                                       AddressEncMask;
   IA32_CR4                                     Cr4;
-
-  //
-  // Set PageMapLevel5Entry to suppress incorrect compiler/analyzer warnings
-  //
-  PageMapLevel5Entry = NULL;
+  PAGING_MODE                                  PagingMode;
+  UINTN                                        PageTable;
+  IA32_MAP_ATTRIBUTE                           MapAttribute;
+  IA32_MAP_ATTRIBUTE                           MapMask;
+  EFI_PHYSICAL_ADDRESS                         GhcbBase4K;
 
   //
   // Make sure AddressEncMask is contained to smallest supported address field
   //
-  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
-
-  Page1GSupport = FALSE;
-  if (PcdGetBool (PcdUse1GPageTable)) {
-    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
-    if (RegEax >= 0x80000001) {
-      AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);
-      if ((RegEdx & BIT26) != 0) {
-        Page1GSupport = TRUE;
+  AddressEncMask    = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) & PAGING_1G_ADDRESS_MASK_64;
+  Page5LevelSupport = FALSE;
+  Page1GSupport     = FALSE;
+
+  if (!Is64BitPageTable) {
+    PagingMode          = PagingPae;
+    PhysicalAddressBits = 32;
+  } else {
+    if (PcdGetBool (PcdUse1GPageTable)) {
+      AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
+      if (RegEax >= 0x80000001) {
+        AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);
+        if ((RegEdx & BIT26) != 0) {
+          Page1GSupport = TRUE;
+        }
       }
     }
-  }
 
-  //
-  // Get physical address bits supported.
-  //
-  Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
-  if (Hob != NULL) {
-    PhysicalAddressBits = ((EFI_HOB_CPU *)Hob)->SizeOfMemorySpace;
-  } else {
-    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
-    if (RegEax >= 0x80000008) {
-      AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
-      PhysicalAddressBits = (UINT8)RegEax;
+    //
+    // Get physical address bits supported.
+    //
+    Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
+    if (Hob != NULL) {
+      PhysicalAddressBits = ((EFI_HOB_CPU *)Hob)->SizeOfMemorySpace;
     } else {
-      PhysicalAddressBits = 36;
+      AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
+      if (RegEax >= 0x80000008) {
+        AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
+        PhysicalAddressBits = (UINT8)RegEax;
+      } else {
+        PhysicalAddressBits = 36;
+      }
     }
-  }
 
-  Page5LevelSupport = FALSE;
-  if (PcdGetBool (PcdUse5LevelPageTable)) {
-    AsmCpuidEx (
-      CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS,
-      CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_SUB_LEAF_INFO,
-      NULL,
-      NULL,
-      &EcxFlags.Uint32,
-      NULL
-      );
-    if (EcxFlags.Bits.FiveLevelPage != 0) {
-      Page5LevelSupport = TRUE;
+    if (PcdGetBool (PcdUse5LevelPageTable)) {
+      AsmCpuidEx (
+        CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS,
+        CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_SUB_LEAF_INFO,
+        NULL,
+        NULL,
+        &EcxFlags.Uint32,
+        NULL
+        );
+      if (EcxFlags.Bits.FiveLevelPage != 0) {
+        Page5LevelSupport = TRUE;
+      }
     }
-  }
-
-  DEBUG ((DEBUG_INFO, "AddressBits=%u 5LevelPaging=%u 1GPage=%u\n", PhysicalAddressBits, Page5LevelSupport, Page1GSupport));
 
-  //
-  // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses
-  //  when 5-Level Paging is disabled,
-  //  due to either unsupported by HW, or disabled by PCD.
-  //
-  ASSERT (PhysicalAddressBits <= 52);
-  if (!Page5LevelSupport && (PhysicalAddressBits > 48)) {
-    PhysicalAddressBits = 48;
-  }
-
-  //
-  // Calculate the table entries needed.
-  //
-  NumberOfPml5EntriesNeeded = 1;
-  if (PhysicalAddressBits > 48) {
-    NumberOfPml5EntriesNeeded = (UINT32)LShiftU64 (1, PhysicalAddressBits - 48);
-    PhysicalAddressBits       = 48;
-  }
+    if (Page5LevelSupport) {
+      if (Page1GSupport) {
+        PagingMode = Paging5Level1GB;
+      } else {
+        PagingMode = Paging5Level;
+      }
+    } else {
+      if (Page1GSupport) {
+        PagingMode = Paging4Level1GB;
+      } else {
+        PagingMode = Paging4Level;
+      }
+    }
 
-  NumberOfPml4EntriesNeeded = 1;
-  if (PhysicalAddressBits > 39) {
-    NumberOfPml4EntriesNeeded = (UINT32)LShiftU64 (1, PhysicalAddressBits - 39);
-    PhysicalAddressBits       = 39;
+    DEBUG ((DEBUG_INFO, "AddressBits=%u 5LevelPaging=%u 1GPage=%u\n", PhysicalAddressBits, Page5LevelSupport, Page1GSupport));
+    //
+    // IA-32e paging translates 48-bit linear addresses to 52-bit physical addresses
+    // when 5-Level Paging is disabled, due to either unsupported by HW, or disabled by PCD.
+    //
+    ASSERT (PhysicalAddressBits <= 52);
+    if (!Page5LevelSupport && (PhysicalAddressBits > 48)) {
+      PhysicalAddressBits = 48;
+    }
   }
 
-  NumberOfPdpEntriesNeeded = 1;
-  ASSERT (PhysicalAddressBits > 30);
-  NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, PhysicalAddressBits - 30);
+  PageTable                   = 0;
+  MapAttribute.Uint64         = AddressEncMask;
+  MapAttribute.Bits.Present   = 1;
+  MapAttribute.Bits.ReadWrite = 1;
+  MapMask.Uint64              = MAX_UINT64;
+  CreateOrUpdatePageTable (&PageTable, PagingMode, 0, LShiftU64 (1, PhysicalAddressBits), &MapAttribute, &MapMask);
 
-  //
-  // Pre-allocate big pages to avoid later allocations.
-  //
-  if (!Page1GSupport) {
-    TotalPagesNum = ((NumberOfPdpEntriesNeeded + 1) * NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1;
-  } else {
-    TotalPagesNum = (NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1;
-  }
-
-  //
-  // Substract the one page occupied by PML5 entries if 5-Level Paging is disabled.
-  //
-  if (!Page5LevelSupport) {
-    TotalPagesNum--;
+  if ((GhcbBase > 0) && (GhcbSize > 0) && (AddressEncMask != 0)) {
+    //
+    // The GHCB range consists of two pages per CPU, the GHCB and a
+    // per-CPU variable page. The GHCB page needs to be mapped as an
+    // unencrypted page while the per-CPU variable page needs to be
+    // mapped encrypted. These pages alternate in assignment.
+    //
+    ASSERT (Is64BitPageTable == TRUE);
+    GhcbBase4K                           = ALIGN_VALUE (GhcbBase, SIZE_4KB);
+    MapAttribute.Uint64                  = GhcbBase4K;
+    MapMask.Uint64                       = 0;
+    MapMask.Bits.PageTableBaseAddressLow = 1;
+    CreateOrUpdatePageTable (&PageTable, PagingMode, GhcbBase4K, SIZE_4KB, &MapAttribute, &MapMask);
   }
 
-  DEBUG ((
-    DEBUG_INFO,
-    "Pml5=%u Pml4=%u Pdp=%u TotalPage=%Lu\n",
-    NumberOfPml5EntriesNeeded,
-    NumberOfPml4EntriesNeeded,
-    NumberOfPdpEntriesNeeded,
-    (UINT64)TotalPagesNum
-    ));
-
-  BigPageAddress = (UINTN)AllocatePageTableMemory (TotalPagesNum);
-  ASSERT (BigPageAddress != 0);
-
-  //
-  // By architecture only one PageMapLevel4 exists - so lets allocate storage for it.
-  //
-  PageMap = (VOID *)BigPageAddress;
-  if (Page5LevelSupport) {
+  if (PcdGetBool (PcdSetNxForStack)) {
     //
-    // By architecture only one PageMapLevel5 exists - so lets allocate storage for it.
+    // Set the stack as Nx in page table.
     //
-    PageMapLevel5Entry = PageMap;
-    BigPageAddress    += SIZE_4KB;
+    MapAttribute.Uint64  = 0;
+    MapAttribute.Bits.Nx = 1;
+    MapMask.Uint64       = 0;
+    MapMask.Bits.Nx      = 1;
+    CreateOrUpdatePageTable (&PageTable, PagingMode, StackBase, StackSize, &MapAttribute, &MapMask);
   }
 
-  PageAddress = 0;
-
-  for ( IndexOfPml5Entries = 0
-        ; IndexOfPml5Entries < NumberOfPml5EntriesNeeded
-        ; IndexOfPml5Entries++)
-  {
+  MapAttribute.Uint64       = 0;
+  MapAttribute.Bits.Present = 0;
+  MapMask.Uint64            = 0;
+  MapMask.Bits.Present      = 1;
+  if (IsNullDetectionEnabled ()) {
     //
-    // Each PML5 entry points to a page of PML4 entires.
-    // So lets allocate space for them and fill them in in the IndexOfPml4Entries loop.
-    // When 5-Level Paging is disabled, below allocation happens only once.
+    // Set [0, 4KB] as not-present in page table.
     //
-    PageMapLevel4Entry = (VOID *)BigPageAddress;
-    BigPageAddress    += SIZE_4KB;
-
-    if (Page5LevelSupport) {
-      //
-      // Make a PML5 Entry
-      //
-      PageMapLevel5Entry->Uint64         = (UINT64)(UINTN)PageMapLevel4Entry | AddressEncMask;
-      PageMapLevel5Entry->Bits.ReadWrite = 1;
-      PageMapLevel5Entry->Bits.Present   = 1;
-      PageMapLevel5Entry++;
-    }
-
-    for ( IndexOfPml4Entries = 0
-          ; IndexOfPml4Entries < (NumberOfPml5EntriesNeeded == 1 ? NumberOfPml4EntriesNeeded : 512)
-          ; IndexOfPml4Entries++, PageMapLevel4Entry++)
-    {
-      //
-      // Each PML4 entry points to a page of Page Directory Pointer entires.
-      // So lets allocate space for them and fill them in in the IndexOfPdpEntries loop.
-      //
-      PageDirectoryPointerEntry = (VOID *)BigPageAddress;
-      BigPageAddress           += SIZE_4KB;
-
-      //
-      // Make a PML4 Entry
-      //
-      PageMapLevel4Entry->Uint64         = (UINT64)(UINTN)PageDirectoryPointerEntry | AddressEncMask;
-      PageMapLevel4Entry->Bits.ReadWrite = 1;
-      PageMapLevel4Entry->Bits.Present   = 1;
-
-      if (Page1GSupport) {
-        PageDirectory1GEntry = (VOID *)PageDirectoryPointerEntry;
-
-        for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress += SIZE_1GB) {
-          if (ToSplitPageTable (PageAddress, SIZE_1GB, StackBase, StackSize, GhcbBase, GhcbSize)) {
-            Split1GPageTo2M (PageAddress, (UINT64 *)PageDirectory1GEntry, StackBase, StackSize, GhcbBase, GhcbSize);
-          } else {
-            //
-            // Fill in the Page Directory entries
-            //
-            PageDirectory1GEntry->Uint64         = (UINT64)PageAddress | AddressEncMask;
-            PageDirectory1GEntry->Bits.ReadWrite = 1;
-            PageDirectory1GEntry->Bits.Present   = 1;
-            PageDirectory1GEntry->Bits.MustBe1   = 1;
-          }
-        }
-      } else {
-        for ( IndexOfPdpEntries = 0
-              ; IndexOfPdpEntries < (NumberOfPml4EntriesNeeded == 1 ? NumberOfPdpEntriesNeeded : 512)
-              ; IndexOfPdpEntries++, PageDirectoryPointerEntry++)
-        {
-          //
-          // Each Directory Pointer entries points to a page of Page Directory entires.
-          // So allocate space for them and fill them in in the IndexOfPageDirectoryEntries loop.
-          //
-          PageDirectoryEntry = (VOID *)BigPageAddress;
-          BigPageAddress    += SIZE_4KB;
-
-          //
-          // Fill in a Page Directory Pointer Entries
-          //
-          PageDirectoryPointerEntry->Uint64         = (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask;
-          PageDirectoryPointerEntry->Bits.ReadWrite = 1;
-          PageDirectoryPointerEntry->Bits.Present   = 1;
-
-          for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress += SIZE_2MB) {
-            if (ToSplitPageTable (PageAddress, SIZE_2MB, StackBase, StackSize, GhcbBase, GhcbSize)) {
-              //
-              // Need to split this 2M page that covers NULL or stack range.
-              //
-              Split2MPageTo4K (PageAddress, (UINT64 *)PageDirectoryEntry, StackBase, StackSize, GhcbBase, GhcbSize);
-            } else {
-              //
-              // Fill in the Page Directory entries
-              //
-              PageDirectoryEntry->Uint64         = (UINT64)PageAddress | AddressEncMask;
-              PageDirectoryEntry->Bits.ReadWrite = 1;
-              PageDirectoryEntry->Bits.Present   = 1;
-              PageDirectoryEntry->Bits.MustBe1   = 1;
-            }
-          }
-        }
-
-        //
-        // Fill with null entry for unused PDPTE
-        //
-        ZeroMem (PageDirectoryPointerEntry, (512 - IndexOfPdpEntries) * sizeof (PAGE_MAP_AND_DIRECTORY_POINTER));
-      }
-    }
+    CreateOrUpdatePageTable (&PageTable, PagingMode, 0, SIZE_4KB, &MapAttribute, &MapMask);
+  }
 
+  if (PcdGetBool (PcdCpuStackGuard)) {
     //
-    // For the PML4 entries we are not using fill in a null entry.
+    // Set the the last 4KB of stack as not-present in page table.
     //
-    ZeroMem (PageMapLevel4Entry, (512 - IndexOfPml4Entries) * sizeof (PAGE_MAP_AND_DIRECTORY_POINTER));
+    CreateOrUpdatePageTable (&PageTable, PagingMode, StackBase, SIZE_4KB, &MapAttribute, &MapMask);
   }
 
   if (Page5LevelSupport) {
     Cr4.UintN     = AsmReadCr4 ();
     Cr4.Bits.LA57 = 1;
     AsmWriteCr4 (Cr4.UintN);
-    //
-    // For the PML5 entries we are not using fill in a null entry.
-    //
-    ZeroMem (PageMapLevel5Entry, (512 - IndexOfPml5Entries) * sizeof (PAGE_MAP_AND_DIRECTORY_POINTER));
   }
 
   //
   // Protect the page table by marking the memory used for page table to be
   // read-only.
   //
-  EnablePageTableProtection ((UINTN)PageMap, TRUE);
+  EnablePageTableProtection ((UINTN)PageTable, TRUE);
 
   //
   // Set IA32_EFER.NXE if necessary.
@@ -956,5 +688,5 @@ CreateIdentityMappingPageTables (
     EnableExecuteDisableBit ();
   }
 
-  return (UINTN)PageMap;
+  return PageTable;
 }
diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
index 616ebe42b0..a6cf31811d 100644
--- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
+++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
@@ -7,7 +7,7 @@
     3) IA-32 Intel(R) Architecture Software Developer's Manual Volume 3:System Programmer's Guide, Intel
     4) AMD64 Architecture Programmer's Manual Volume 2: System Programming
 
-Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
 
 SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -46,99 +46,6 @@ typedef struct {
   UINT32                      Reserved;
 } X64_IDT_GATE_DESCRIPTOR;
 
-//
-// Page-Map Level-4 Offset (PML4) and
-// Page-Directory-Pointer Offset (PDPE) entries 4K & 2MB
-//
-
-typedef union {
-  struct {
-    UINT64    Present              : 1;  // 0 = Not present in memory, 1 = Present in memory
-    UINT64    ReadWrite            : 1;  // 0 = Read-Only, 1= Read/Write
-    UINT64    UserSupervisor       : 1;  // 0 = Supervisor, 1=User
-    UINT64    WriteThrough         : 1;  // 0 = Write-Back caching, 1=Write-Through caching
-    UINT64    CacheDisabled        : 1;  // 0 = Cached, 1=Non-Cached
-    UINT64    Accessed             : 1;  // 0 = Not accessed, 1 = Accessed (set by CPU)
-    UINT64    Reserved             : 1;  // Reserved
-    UINT64    MustBeZero           : 2;  // Must Be Zero
-    UINT64    Available            : 3;  // Available for use by system software
-    UINT64    PageTableBaseAddress : 40; // Page Table Base Address
-    UINT64    AvabilableHigh       : 11; // Available for use by system software
-    UINT64    Nx                   : 1;  // No Execute bit
-  } Bits;
-  UINT64    Uint64;
-} PAGE_MAP_AND_DIRECTORY_POINTER;
-
-//
-// Page Table Entry 4KB
-//
-typedef union {
-  struct {
-    UINT64    Present              : 1;  // 0 = Not present in memory, 1 = Present in memory
-    UINT64    ReadWrite            : 1;  // 0 = Read-Only, 1= Read/Write
-    UINT64    UserSupervisor       : 1;  // 0 = Supervisor, 1=User
-    UINT64    WriteThrough         : 1;  // 0 = Write-Back caching, 1=Write-Through caching
-    UINT64    CacheDisabled        : 1;  // 0 = Cached, 1=Non-Cached
-    UINT64    Accessed             : 1;  // 0 = Not accessed, 1 = Accessed (set by CPU)
-    UINT64    Dirty                : 1;  // 0 = Not Dirty, 1 = written by processor on access to page
-    UINT64    PAT                  : 1;  //
-    UINT64    Global               : 1;  // 0 = Not global page, 1 = global page TLB not cleared on CR3 write
-    UINT64    Available            : 3;  // Available for use by system software
-    UINT64    PageTableBaseAddress : 40; // Page Table Base Address
-    UINT64    AvabilableHigh       : 11; // Available for use by system software
-    UINT64    Nx                   : 1;  // 0 = Execute Code, 1 = No Code Execution
-  } Bits;
-  UINT64    Uint64;
-} PAGE_TABLE_4K_ENTRY;
-
-//
-// Page Table Entry 2MB
-//
-typedef union {
-  struct {
-    UINT64    Present              : 1;  // 0 = Not present in memory, 1 = Present in memory
-    UINT64    ReadWrite            : 1;  // 0 = Read-Only, 1= Read/Write
-    UINT64    UserSupervisor       : 1;  // 0 = Supervisor, 1=User
-    UINT64    WriteThrough         : 1;  // 0 = Write-Back caching, 1=Write-Through caching
-    UINT64    CacheDisabled        : 1;  // 0 = Cached, 1=Non-Cached
-    UINT64    Accessed             : 1;  // 0 = Not accessed, 1 = Accessed (set by CPU)
-    UINT64    Dirty                : 1;  // 0 = Not Dirty, 1 = written by processor on access to page
-    UINT64    MustBe1              : 1;  // Must be 1
-    UINT64    Global               : 1;  // 0 = Not global page, 1 = global page TLB not cleared on CR3 write
-    UINT64    Available            : 3;  // Available for use by system software
-    UINT64    PAT                  : 1;  //
-    UINT64    MustBeZero           : 8;  // Must be zero;
-    UINT64    PageTableBaseAddress : 31; // Page Table Base Address
-    UINT64    AvabilableHigh       : 11; // Available for use by system software
-    UINT64    Nx                   : 1;  // 0 = Execute Code, 1 = No Code Execution
-  } Bits;
-  UINT64    Uint64;
-} PAGE_TABLE_ENTRY;
-
-//
-// Page Table Entry 1GB
-//
-typedef union {
-  struct {
-    UINT64    Present              : 1;  // 0 = Not present in memory, 1 = Present in memory
-    UINT64    ReadWrite            : 1;  // 0 = Read-Only, 1= Read/Write
-    UINT64    UserSupervisor       : 1;  // 0 = Supervisor, 1=User
-    UINT64    WriteThrough         : 1;  // 0 = Write-Back caching, 1=Write-Through caching
-    UINT64    CacheDisabled        : 1;  // 0 = Cached, 1=Non-Cached
-    UINT64    Accessed             : 1;  // 0 = Not accessed, 1 = Accessed (set by CPU)
-    UINT64    Dirty                : 1;  // 0 = Not Dirty, 1 = written by processor on access to page
-    UINT64    MustBe1              : 1;  // Must be 1
-    UINT64    Global               : 1;  // 0 = Not global page, 1 = global page TLB not cleared on CR3 write
-    UINT64    Available            : 3;  // Available for use by system software
-    UINT64    PAT                  : 1;  //
-    UINT64    MustBeZero           : 17; // Must be zero;
-    UINT64    PageTableBaseAddress : 22; // Page Table Base Address
-    UINT64    AvabilableHigh       : 11; // Available for use by system software
-    UINT64    Nx                   : 1;  // 0 = Execute Code, 1 = No Code Execution
-  } Bits;
-  UINT64    Uint64;
-} PAGE_TABLE_1G_ENTRY;
-
 #pragma pack()
 
 #define CR0_WP  BIT16
@@ -194,44 +101,25 @@ EnableExecuteDisableBit (
   );
 
 /**
-  Split 2M page to 4K.
-
-  @param[in]      PhysicalAddress       Start physical address the 2M page covered.
-  @param[in, out] PageEntry2M           Pointer to 2M page entry.
-  @param[in]      StackBase             Stack base address.
-  @param[in]      StackSize             Stack size.
-  @param[in]      GhcbBase              GHCB page area base address.
-  @param[in]      GhcbSize              GHCB page area size.
-
-**/
-VOID
-Split2MPageTo4K (
-  IN EFI_PHYSICAL_ADDRESS  PhysicalAddress,
-  IN OUT UINT64            *PageEntry2M,
-  IN EFI_PHYSICAL_ADDRESS  StackBase,
-  IN UINTN                 StackSize,
-  IN EFI_PHYSICAL_ADDRESS  GhcbBase,
-  IN UINTN                 GhcbSize
-  );
-
-/**
-  Allocates and fills in the Page Directory and Page Table Entries to
+  Create IA32 PAE paging or 4-level/5-level paging for long mode to
   establish a 1:1 Virtual to Physical mapping.
 
-  @param[in] StackBase  Stack base address.
-  @param[in] StackSize  Stack size.
-  @param[in] GhcbBase   GHCB page area base address.
-  @param[in] GhcbSize   GHCB page area size.
+  @param[in] Is64BitPageTable   Whether to create 64-bit page table.
+  @param[in] StackBase          Stack base address.
+  @param[in] StackSize          Stack size.
+  @param[in] GhcbBase           GHCB page area base address.
+  @param[in] GhcbSize           GHCB page area size.
 
-  @return The address of 4 level page map.
+  @return The address of page table.
 
 **/
 UINTN
 CreateIdentityMappingPageTables (
+  IN BOOLEAN               Is64BitPageTable,
   IN EFI_PHYSICAL_ADDRESS  StackBase,
   IN UINTN                 StackSize,
   IN EFI_PHYSICAL_ADDRESS  GhcbBase,
-  IN UINTN                 GhcbkSize
+  IN UINTN                 GhcbSize
   );
 
 /**
@@ -289,39 +177,4 @@ IsNullDetectionEnabled (
   VOID
   );
 
-/**
-  Prevent the memory pages used for page table from been overwritten.
-
-  @param[in] PageTableBase    Base address of page table (CR3).
-  @param[in] Level4Paging     Level 4 paging flag.
-
-**/
-VOID
-EnablePageTableProtection (
-  IN  UINTN    PageTableBase,
-  IN  BOOLEAN  Level4Paging
-  );
-
-/**
-  This API provides a way to allocate memory for page table.
-
-  This API can be called more than once to allocate memory for page tables.
-
-  Allocates the number of 4KB pages and returns a pointer to the allocated
-  buffer. The buffer returned is aligned on a 4KB boundary.
-
-  If Pages is 0, then NULL is returned.
-  If there is not enough memory remaining to satisfy the request, then NULL is
-  returned.
-
-  @param  Pages                 The number of 4 KB pages to allocate.
-
-  @return A pointer to the allocated buffer or NULL if allocation fails.
-
-**/
-VOID *
-AllocatePageTableMemory (
-  IN UINTN  Pages
-  );
-
 #endif
-- 
2.31.1.windows.1


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [Patch V2 7/8] MdeModulePkg/DxeIpl: Remove duplicated code to enable NX
  2023-03-31  9:33 [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl duntan
                   ` (5 preceding siblings ...)
  2023-03-31  9:33 ` [Patch V2 6/8] MdeModulePkg/DxeIpl: Create page table by CpuPageTableLib duntan
@ 2023-03-31  9:33 ` duntan
  2023-03-31  9:33 ` [Patch V2 8/8] MdeModulePkg/DxeIpl: Refinement to the code to set PageTable as RO duntan
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 32+ messages in thread
From: duntan @ 2023-03-31  9:33 UTC (permalink / raw)
  To: devel; +Cc: Dandan Bi, Liming Gao, Ray Ni, Jian J Wang

In IA32 code, remove the duplicated code to enable NX.
In the previous patch, IA32 code also uses the new
CreateIdentityMappingPageTables() to create PAE page table.
This function calls EnableExecuteDisableBit if needed.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
---
 MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c b/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
index af1e1e3d02..83c5478895 100644
--- a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
+++ b/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
@@ -322,9 +322,6 @@ HandOffToDxeCore (
     BuildPageTablesIa32Pae = ToBuildPageTable ();
     if (BuildPageTablesIa32Pae) {
       PageTables = CreateIdentityMappingPageTables (FALSE, BaseOfStack, STACK_SIZE, 0, 0);
-      if (IsEnableNonExecNeeded ()) {
-        EnableExecuteDisableBit ();
-      }
     }
 
     //
-- 
2.31.1.windows.1


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* [Patch V2 8/8] MdeModulePkg/DxeIpl: Refinement to the code to set PageTable as RO
  2023-03-31  9:33 [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl duntan
                   ` (6 preceding siblings ...)
  2023-03-31  9:33 ` [Patch V2 7/8] MdeModulePkg/DxeIpl: Remove duplicated code to enable NX duntan
@ 2023-03-31  9:33 ` duntan
  2023-03-31 10:25   ` Ni, Ray
       [not found] ` <1751776493F12DAB.27612@groups.io>
                   ` (2 subsequent siblings)
  10 siblings, 1 reply; 32+ messages in thread
From: duntan @ 2023-03-31  9:33 UTC (permalink / raw)
  To: devel; +Cc: Dandan Bi, Liming Gao, Ray Ni, Jian J Wang

Code refinement to the code to set page table as RO in DxeIpl module.
Set all page table pools as ReadOnly by calling PageTableMap() in
CpuPageTableLib multiple times instead of searching each page table
pool address in page table layer by layer. Also, this commit solve
the issue that original SetPageTablePoolReadOnly() code in DxeIpl
doesn't handle the Level5Paging case.

Bugzila: https://bugzilla.tianocore.org/show_bug.cgi?id=4176
Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Dandan Bi <dandan.bi@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
---
 MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c | 155 +++++++++++++++--------------------------------------------------------------------------------------------------------------------------------------------
 MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h |  15 ---------------
 2 files changed, 15 insertions(+), 155 deletions(-)

diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
index ecdbd2ca24..a9edf4de32 100644
--- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
+++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
@@ -330,154 +330,37 @@ CreateOrUpdatePageTable (
   ASSERT (PageTableBufferSize == 0);
 }
 
-/**
-  Set one page of page table pool memory to be read-only.
-
-  @param[in] PageTableBase    Base address of page table (CR3).
-  @param[in] Address          Start address of a page to be set as read-only.
-  @param[in] Level4Paging     Level 4 paging flag.
-
-**/
-VOID
-SetPageTablePoolReadOnly (
-  IN  UINTN                 PageTableBase,
-  IN  EFI_PHYSICAL_ADDRESS  Address,
-  IN  BOOLEAN               Level4Paging
-  )
-{
-  UINTN                 Index;
-  UINTN                 EntryIndex;
-  UINT64                AddressEncMask;
-  EFI_PHYSICAL_ADDRESS  PhysicalAddress;
-  UINT64                *PageTable;
-  UINT64                *NewPageTable;
-  UINT64                PageAttr;
-  UINT64                LevelSize[5];
-  UINT64                LevelMask[5];
-  UINTN                 LevelShift[5];
-  UINTN                 Level;
-  UINT64                PoolUnitSize;
-
-  ASSERT (PageTableBase != 0);
-
-  //
-  // Since the page table is always from page table pool, which is always
-  // located at the boundary of PcdPageTablePoolAlignment, we just need to
-  // set the whole pool unit to be read-only.
-  //
-  Address = Address & PAGE_TABLE_POOL_ALIGN_MASK;
-
-  LevelShift[1] = PAGING_L1_ADDRESS_SHIFT;
-  LevelShift[2] = PAGING_L2_ADDRESS_SHIFT;
-  LevelShift[3] = PAGING_L3_ADDRESS_SHIFT;
-  LevelShift[4] = PAGING_L4_ADDRESS_SHIFT;
-
-  LevelMask[1] = PAGING_4K_ADDRESS_MASK_64;
-  LevelMask[2] = PAGING_2M_ADDRESS_MASK_64;
-  LevelMask[3] = PAGING_1G_ADDRESS_MASK_64;
-  LevelMask[4] = PAGING_1G_ADDRESS_MASK_64;
-
-  LevelSize[1] = SIZE_4KB;
-  LevelSize[2] = SIZE_2MB;
-  LevelSize[3] = SIZE_1GB;
-  LevelSize[4] = SIZE_512GB;
-
-  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask) &
-                   PAGING_1G_ADDRESS_MASK_64;
-  PageTable    = (UINT64 *)(UINTN)PageTableBase;
-  PoolUnitSize = PAGE_TABLE_POOL_UNIT_SIZE;
-
-  for (Level = (Level4Paging) ? 4 : 3; Level > 0; --Level) {
-    Index  = ((UINTN)RShiftU64 (Address, LevelShift[Level]));
-    Index &= PAGING_PAE_INDEX_MASK;
-
-    PageAttr = PageTable[Index];
-    if ((PageAttr & IA32_PG_PS) == 0) {
-      //
-      // Go to next level of table.
-      //
-      PageTable = (UINT64 *)(UINTN)(PageAttr & ~AddressEncMask &
-                                    PAGING_4K_ADDRESS_MASK_64);
-      continue;
-    }
-
-    if (PoolUnitSize >= LevelSize[Level]) {
-      //
-      // Clear R/W bit if current page granularity is not larger than pool unit
-      // size.
-      //
-      if ((PageAttr & IA32_PG_RW) != 0) {
-        while (PoolUnitSize > 0) {
-          //
-          // PAGE_TABLE_POOL_UNIT_SIZE and PAGE_TABLE_POOL_ALIGNMENT are fit in
-          // one page (2MB). Then we don't need to update attributes for pages
-          // crossing page directory. ASSERT below is for that purpose.
-          //
-          ASSERT (Index < EFI_PAGE_SIZE/sizeof (UINT64));
-
-          PageTable[Index] &= ~(UINT64)IA32_PG_RW;
-          PoolUnitSize     -= LevelSize[Level];
-
-          ++Index;
-        }
-      }
-
-      break;
-    } else {
-      //
-      // The smaller granularity of page must be needed.
-      //
-      ASSERT (Level > 1);
-
-      NewPageTable = AllocatePageTableMemory (1);
-      ASSERT (NewPageTable != NULL);
-
-      PhysicalAddress = PageAttr & LevelMask[Level];
-      for (EntryIndex = 0;
-           EntryIndex < EFI_PAGE_SIZE/sizeof (UINT64);
-           ++EntryIndex)
-      {
-        NewPageTable[EntryIndex] = PhysicalAddress  | AddressEncMask |
-                                   IA32_PG_P | IA32_PG_RW;
-        if (Level > 2) {
-          NewPageTable[EntryIndex] |= IA32_PG_PS;
-        }
-
-        PhysicalAddress += LevelSize[Level - 1];
-      }
-
-      PageTable[Index] = (UINT64)(UINTN)NewPageTable | AddressEncMask |
-                         IA32_PG_P | IA32_PG_RW;
-      PageTable = NewPageTable;
-    }
-  }
-}
-
 /**
   Prevent the memory pages used for page table from been overwritten.
 
-  @param[in] PageTableBase    Base address of page table (CR3).
-  @param[in] Level4Paging     Level 4 paging flag.
+  @param[in] PageTableBase   Base address of page table (CR3).
+  @param[in] PagingMode      The paging mode.
 
 **/
 VOID
 EnablePageTableProtection (
-  IN  UINTN    PageTableBase,
-  IN  BOOLEAN  Level4Paging
+  IN  UINTN        PageTableBase,
+  IN  PAGING_MODE  PagingMode
   )
 {
   PAGE_TABLE_POOL       *HeadPool;
   PAGE_TABLE_POOL       *Pool;
   UINT64                PoolSize;
   EFI_PHYSICAL_ADDRESS  Address;
+  IA32_MAP_ATTRIBUTE    MapAttribute;
+  IA32_MAP_ATTRIBUTE    MapMask;
 
   if (mPageTablePool == NULL) {
     return;
   }
 
+  MapAttribute.Uint64         = 0;
+  MapAttribute.Bits.ReadWrite = 0;
+  MapMask.Uint64              = 0;
+  MapMask.Bits.ReadWrite      = 1;
+
   //
-  // No need to clear CR0.WP since PageTableBase has't been written to CR3 yet.
-  // SetPageTablePoolReadOnly might update mPageTablePool. It's safer to
+  // CreateOrUpdatePageTable might update mPageTablePool. It's safer to
   // remember original one in advance.
   //
   HeadPool = mPageTablePool;
@@ -485,18 +368,10 @@ EnablePageTableProtection (
   do {
     Address  = (EFI_PHYSICAL_ADDRESS)(UINTN)Pool;
     PoolSize = Pool->Offset + EFI_PAGES_TO_SIZE (Pool->FreePages);
-
     //
-    // The size of one pool must be multiple of PAGE_TABLE_POOL_UNIT_SIZE, which
-    // is one of page size of the processor (2MB by default). Let's apply the
-    // protection to them one by one.
+    // Set entire pool including header, used-memory and left free-memory as ReadOnly.
     //
-    while (PoolSize > 0) {
-      SetPageTablePoolReadOnly (PageTableBase, Address, Level4Paging);
-      Address  += PAGE_TABLE_POOL_UNIT_SIZE;
-      PoolSize -= PAGE_TABLE_POOL_UNIT_SIZE;
-    }
-
+    CreateOrUpdatePageTable (&PageTableBase, PagingMode, Address, PoolSize, &MapAttribute, &MapMask);
     Pool = Pool->NextPool;
   } while (Pool != HeadPool);
 
@@ -679,7 +554,7 @@ CreateIdentityMappingPageTables (
   // Protect the page table by marking the memory used for page table to be
   // read-only.
   //
-  EnablePageTableProtection ((UINTN)PageTable, TRUE);
+  EnablePageTableProtection (PageTable, PagingMode);
 
   //
   // Set IA32_EFER.NXE if necessary.
diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
index a6cf31811d..034c4249d4 100644
--- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
+++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
@@ -50,23 +50,8 @@ typedef struct {
 
 #define CR0_WP  BIT16
 
-#define IA32_PG_P   BIT0
-#define IA32_PG_RW  BIT1
-#define IA32_PG_PS  BIT7
-
-#define PAGING_PAE_INDEX_MASK  0x1FF
-
-#define PAGING_4K_ADDRESS_MASK_64  0x000FFFFFFFFFF000ull
-#define PAGING_2M_ADDRESS_MASK_64  0x000FFFFFFFE00000ull
 #define PAGING_1G_ADDRESS_MASK_64  0x000FFFFFC0000000ull
 
-#define PAGING_L1_ADDRESS_SHIFT  12
-#define PAGING_L2_ADDRESS_SHIFT  21
-#define PAGING_L3_ADDRESS_SHIFT  30
-#define PAGING_L4_ADDRESS_SHIFT  39
-
-#define PAGING_PML4E_NUMBER  4
-
 #define PAGE_TABLE_POOL_ALIGNMENT   BASE_2MB
 #define PAGE_TABLE_POOL_UNIT_SIZE   SIZE_2MB
 #define PAGE_TABLE_POOL_UNIT_PAGES  EFI_SIZE_TO_PAGES (PAGE_TABLE_POOL_UNIT_SIZE)
-- 
2.31.1.windows.1


^ permalink raw reply related	[flat|nested] 32+ messages in thread

* Re: [Patch V2 1/8] EmulatorPkg: Add CpuPageTableLib required by DxeIpl in DSC
  2023-03-31  9:33 ` [Patch V2 1/8] EmulatorPkg: Add CpuPageTableLib required by DxeIpl in DSC duntan
@ 2023-03-31 10:21   ` Ni, Ray
  0 siblings, 0 replies; 32+ messages in thread
From: Ni, Ray @ 2023-03-31 10:21 UTC (permalink / raw)
  To: Tan, Dun, devel@edk2.groups.io; +Cc: Andrew Fish

Reviewed-by: Ray Ni <ray.ni@intel.com>

> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Friday, March 31, 2023 5:34 PM
> To: devel@edk2.groups.io
> Cc: Andrew Fish <afish@apple.com>; Ni, Ray <ray.ni@intel.com>
> Subject: [Patch V2 1/8] EmulatorPkg: Add CpuPageTableLib required by
> DxeIpl in DSC
> 
> Add CpuPageTableLib instance required by DxeIpl in EmulatorPkg.dsc.
> 
> Signed-off-by: Dun Tan <dun.tan@intel.com>
> Cc: Andrew Fish <afish@apple.com>
> Cc: Ray Ni <ray.ni@intel.com>
> ---
>  EmulatorPkg/EmulatorPkg.dsc | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/EmulatorPkg/EmulatorPkg.dsc b/EmulatorPkg/EmulatorPkg.dsc
> index b44435d7e6..d1fb9d9256 100644
> --- a/EmulatorPkg/EmulatorPkg.dsc
> +++ b/EmulatorPkg/EmulatorPkg.dsc
> @@ -4,7 +4,7 @@
>  # The Emulation Platform can be used to debug individual modules, prior to
> creating
>  # a real platform. This also provides an example for how an DSC is created.
>  #
> -# Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
>  # Portions copyright (c) 2010 - 2011, Apple Inc. All rights reserved.<BR>
>  # Copyright (c) Microsoft Corporation.
>  #
> @@ -66,6 +66,7 @@
> 
> PeCoffGetEntryPointLib|MdePkg/Library/BasePeCoffGetEntryPointLib/Base
> PeCoffGetEntryPointLib.inf
>    BaseMemoryLib|MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
> 
> FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBuffer
> BltLib.inf
> +
> CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
> 
>    #
>    # UEFI & PI
> --
> 2.31.1.windows.1


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [Patch V2 6/8] MdeModulePkg/DxeIpl: Create page table by CpuPageTableLib
  2023-03-31  9:33 ` [Patch V2 6/8] MdeModulePkg/DxeIpl: Create page table by CpuPageTableLib duntan
@ 2023-03-31 10:24   ` Ni, Ray
  0 siblings, 0 replies; 32+ messages in thread
From: Ni, Ray @ 2023-03-31 10:24 UTC (permalink / raw)
  To: Tan, Dun, devel@edk2.groups.io; +Cc: Bi, Dandan, Gao, Liming, Wang, Jian J

Reviewed-by: Ray Ni <ray.ni@intel.com>

> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Friday, March 31, 2023 5:34 PM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Gao, Liming
> <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>
> Subject: [Patch V2 6/8] MdeModulePkg/DxeIpl: Create page table by
> CpuPageTableLib
> 
> Modify CreateIdentityMappingPageTables() to create page table
> based on CpuPageTableLib in DxeIpl module. This function can
> be used to create both IA32 PAE paging and long mode 4-level,
> 5-level paging structure. With the PageTableMap() API in the
> CpuPageTableLib, we can remove the complicated page table
> manipulating code. This commit doesn't change any functionality.
> 
> Signed-off-by: Dun Tan <dun.tan@intel.com>
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> ---
>  MdeModulePkg/Core/DxeIplPeim/DxeIpl.h            |   3 ++-
>  MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf          |   4 +++-
>  MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c  | 109 ++++-----------
> ----------------------------------------------------------------------------------------------
>  MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c   |   5 +++--
>  MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c | 558
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> +++++++++++++++++++++++++++++-----------------------------------------------
> ----------------------------------------------------------------------------------------------
> ----------------------------------------------------------------------------------------------
> ----------------------------------------------------------------------------------------------
> ------------------------------------------------------------------------------------
>  MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h | 167
> ++++++++++------------------------------------------------------------------------------
> -------------------------------------------------------------------------------
>  6 files changed, 167 insertions(+), 679 deletions(-)
> 
> diff --git a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.h
> b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.h
> index 2f015befce..03e6f8cff7 100644
> --- a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.h
> +++ b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.h
> @@ -2,7 +2,7 @@
>    Master header file for DxeIpl PEIM. All source files in this module should
>    include this file for common definitions.
> 
> -Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> @@ -42,6 +42,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #include <Library/DebugAgentLib.h>
>  #include <Library/PeiServicesTablePointerLib.h>
>  #include <Library/PerformanceLib.h>
> +#include <Library/CpuPageTableLib.h>
> 
>  #define STACK_SIZE      0x20000
>  #define BSP_STORE_SIZE  0x4000
> diff --git a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf
> b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf
> index 052ea0ec1a..60623b4f66 100644
> --- a/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf
> +++ b/MdeModulePkg/Core/DxeIplPeim/DxeIpl.inf
> @@ -5,7 +5,7 @@
>  #  PPI to discover and dispatch the DXE Foundation and components that are
>  #  needed to run the DXE Foundation.
>  #
> -#  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
>  #  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
>  #  Copyright (c) 2020, Hewlett Packard Enterprise Development LP. All rights
> reserved.<BR>
>  #  Copyright (c) 2022, Loongson Technology Corporation Limited. All rights
> reserved.<BR>
> @@ -60,6 +60,7 @@
>  [Packages]
>    MdePkg/MdePkg.dec
>    MdeModulePkg/MdeModulePkg.dec
> +  UefiCpuPkg/UefiCpuPkg.dec
> 
>  [Packages.ARM, Packages.AARCH64]
>    ArmPkg/ArmPkg.dec
> @@ -79,6 +80,7 @@
>    DebugAgentLib
>    PeiServicesTablePointerLib
>    PerformanceLib
> +  CpuPageTableLib
> 
>  [LibraryClasses.ARM, LibraryClasses.AARCH64]
>    ArmMmuLib
> diff --git a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
> b/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
> index fdeaaa39d8..af1e1e3d02 100644
> --- a/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
> +++ b/MdeModulePkg/Core/DxeIplPeim/Ia32/DxeLoadFunc.c
> @@ -1,7 +1,7 @@
>  /** @file
>    Ia32-specific functionality for DxeLoad.
> 
> -Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
>  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
> 
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> @@ -70,107 +70,6 @@ GLOBAL_REMOVE_IF_UNREFERENCED
> IA32_DESCRIPTOR  gLidtDescriptor = {
>    0
>  };
> 
> -/**
> -  Allocates and fills in the Page Directory and Page Table Entries to
> -  establish a 4G page table.
> -
> -  @param[in] StackBase  Stack base address.
> -  @param[in] StackSize  Stack size.
> -
> -  @return The address of page table.
> -
> -**/
> -UINTN
> -Create4GPageTablesIa32Pae (
> -  IN EFI_PHYSICAL_ADDRESS  StackBase,
> -  IN UINTN                 StackSize
> -  )
> -{
> -  UINT8                           PhysicalAddressBits;
> -  EFI_PHYSICAL_ADDRESS            PhysicalAddress;
> -  UINTN                           IndexOfPdpEntries;
> -  UINTN                           IndexOfPageDirectoryEntries;
> -  UINT32                          NumberOfPdpEntriesNeeded;
> -  PAGE_MAP_AND_DIRECTORY_POINTER  *PageMap;
> -  PAGE_MAP_AND_DIRECTORY_POINTER  *PageDirectoryPointerEntry;
> -  PAGE_TABLE_ENTRY                *PageDirectoryEntry;
> -  UINTN                           TotalPagesNum;
> -  UINTN                           PageAddress;
> -  UINT64                          AddressEncMask;
> -
> -  //
> -  // Make sure AddressEncMask is contained to smallest supported address
> field
> -  //
> -  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask)
> & PAGING_1G_ADDRESS_MASK_64;
> -
> -  PhysicalAddressBits = 32;
> -
> -  //
> -  // Calculate the table entries needed.
> -  //
> -  NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, (PhysicalAddressBits
> - 30));
> -
> -  TotalPagesNum = NumberOfPdpEntriesNeeded + 1;
> -  PageAddress   = (UINTN)AllocatePageTableMemory (TotalPagesNum);
> -  ASSERT (PageAddress != 0);
> -
> -  PageMap      = (VOID *)PageAddress;
> -  PageAddress += SIZE_4KB;
> -
> -  PageDirectoryPointerEntry = PageMap;
> -  PhysicalAddress           = 0;
> -
> -  for (IndexOfPdpEntries = 0; IndexOfPdpEntries <
> NumberOfPdpEntriesNeeded; IndexOfPdpEntries++,
> PageDirectoryPointerEntry++) {
> -    //
> -    // Each Directory Pointer entries points to a page of Page Directory entires.
> -    // So allocate space for them and fill them in in the
> IndexOfPageDirectoryEntries loop.
> -    //
> -    PageDirectoryEntry = (VOID *)PageAddress;
> -    PageAddress       += SIZE_4KB;
> -
> -    //
> -    // Fill in a Page Directory Pointer Entries
> -    //
> -    PageDirectoryPointerEntry->Uint64       =
> (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask;
> -    PageDirectoryPointerEntry->Bits.Present = 1;
> -
> -    for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512;
> IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress +=
> SIZE_2MB) {
> -      if (  (IsNullDetectionEnabled () && (PhysicalAddress == 0))
> -         || (  (PhysicalAddress < StackBase + StackSize)
> -            && ((PhysicalAddress + SIZE_2MB) > StackBase)))
> -      {
> -        //
> -        // Need to split this 2M page that covers stack range.
> -        //
> -        Split2MPageTo4K (PhysicalAddress, (UINT64 *)PageDirectoryEntry,
> StackBase, StackSize, 0, 0);
> -      } else {
> -        //
> -        // Fill in the Page Directory entries
> -        //
> -        PageDirectoryEntry->Uint64         = (UINT64)PhysicalAddress |
> AddressEncMask;
> -        PageDirectoryEntry->Bits.ReadWrite = 1;
> -        PageDirectoryEntry->Bits.Present   = 1;
> -        PageDirectoryEntry->Bits.MustBe1   = 1;
> -      }
> -    }
> -  }
> -
> -  for ( ; IndexOfPdpEntries < 512; IndexOfPdpEntries++,
> PageDirectoryPointerEntry++) {
> -    ZeroMem (
> -      PageDirectoryPointerEntry,
> -      sizeof (PAGE_MAP_AND_DIRECTORY_POINTER)
> -      );
> -  }
> -
> -  //
> -  // Protect the page table by marking the memory used for page table to be
> -  // read-only.
> -  //
> -  EnablePageTableProtection ((UINTN)PageMap, FALSE);
> -
> -  return (UINTN)PageMap;
> -}
> -
>  /**
>    The function will check if IA32 PAE is supported.
> 
> @@ -299,9 +198,9 @@ HandOffToDxeCore (
>      //
>      AsmWriteGdtr (&gGdt);
>      //
> -    // Create page table and save PageMapLevel4 to CR3
> +    // Create page table and save PageMapLevel4 or PageMapLevel5 to CR3
>      //
> -    PageTables = CreateIdentityMappingPageTables (BaseOfStack,
> STACK_SIZE, 0, 0);
> +    PageTables = CreateIdentityMappingPageTables (TRUE, BaseOfStack,
> STACK_SIZE, 0, 0);
> 
>      //
>      // End of PEI phase signal
> @@ -422,7 +321,7 @@ HandOffToDxeCore (
>      PageTables             = 0;
>      BuildPageTablesIa32Pae = ToBuildPageTable ();
>      if (BuildPageTablesIa32Pae) {
> -      PageTables = Create4GPageTablesIa32Pae (BaseOfStack, STACK_SIZE);
> +      PageTables = CreateIdentityMappingPageTables (FALSE, BaseOfStack,
> STACK_SIZE, 0, 0);
>        if (IsEnableNonExecNeeded ()) {
>          EnableExecuteDisableBit ();
>        }
> diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c
> b/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c
> index fa2050cf02..2642092ee5 100644
> --- a/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c
> +++ b/MdeModulePkg/Core/DxeIplPeim/X64/DxeLoadFunc.c
> @@ -1,7 +1,7 @@
>  /** @file
>    x64-specifc functionality for DxeLoad.
> 
> -Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> @@ -91,9 +91,10 @@ HandOffToDxeCore (
>    PageTables = 0;
>    if (FeaturePcdGet (PcdDxeIplBuildPageTables)) {
>      //
> -    // Create page table and save PageMapLevel4 to CR3
> +    // Create page table and save PageMapLevel4 or PageMapLevel5 to CR3
>      //
>      PageTables = CreateIdentityMappingPageTables (
> +                   TRUE,
>                     (EFI_PHYSICAL_ADDRESS)(UINTN)BaseOfStack,
>                     STACK_SIZE,
>                     (EFI_PHYSICAL_ADDRESS)(UINTN)GhcbBase,
> diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
> b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
> index 18b121d768..ecdbd2ca24 100644
> --- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
> +++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
> @@ -15,7 +15,7 @@
>      2) IA-32 Intel(R) Architecture Software Developer's Manual Volume
> 2:Instruction Set Reference, Intel
>      3) IA-32 Intel(R) Architecture Software Developer's Manual Volume
> 3:System Programmer's Guide, Intel
> 
> -Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
>  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
> 
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> @@ -186,55 +186,6 @@ EnableExecuteDisableBit (
>    }
>  }
> 
> -/**
> -  The function will check if page table entry should be splitted to smaller
> -  granularity.
> -
> -  @param Address      Physical memory address.
> -  @param Size         Size of the given physical memory.
> -  @param StackBase    Base address of stack.
> -  @param StackSize    Size of stack.
> -  @param GhcbBase     Base address of GHCB pages.
> -  @param GhcbSize     Size of GHCB area.
> -
> -  @retval TRUE      Page table should be split.
> -  @retval FALSE     Page table should not be split.
> -**/
> -BOOLEAN
> -ToSplitPageTable (
> -  IN EFI_PHYSICAL_ADDRESS  Address,
> -  IN UINTN                 Size,
> -  IN EFI_PHYSICAL_ADDRESS  StackBase,
> -  IN UINTN                 StackSize,
> -  IN EFI_PHYSICAL_ADDRESS  GhcbBase,
> -  IN UINTN                 GhcbSize
> -  )
> -{
> -  if (IsNullDetectionEnabled () && (Address == 0)) {
> -    return TRUE;
> -  }
> -
> -  if (PcdGetBool (PcdCpuStackGuard)) {
> -    if ((StackBase >= Address) && (StackBase < (Address + Size))) {
> -      return TRUE;
> -    }
> -  }
> -
> -  if (PcdGetBool (PcdSetNxForStack)) {
> -    if ((Address < StackBase + StackSize) && ((Address + Size) > StackBase)) {
> -      return TRUE;
> -    }
> -  }
> -
> -  if (GhcbBase != 0) {
> -    if ((Address < GhcbBase + GhcbSize) && ((Address + Size) > GhcbBase)) {
> -      return TRUE;
> -    }
> -  }
> -
> -  return FALSE;
> -}
> -
>  /**
>    Initialize a buffer pool for page table use only.
> 
> @@ -341,143 +292,42 @@ AllocatePageTableMemory (
>  }
> 
>  /**
> -  Split 2M page to 4K.
> -
> -  @param[in]      PhysicalAddress       Start physical address the 2M page
> covered.
> -  @param[in, out] PageEntry2M           Pointer to 2M page entry.
> -  @param[in]      StackBase             Stack base address.
> -  @param[in]      StackSize             Stack size.
> -  @param[in]      GhcbBase              GHCB page area base address.
> -  @param[in]      GhcbSize              GHCB page area size.
> -
> +  This function create new page table or modifies the page MapAttribute for
> the memory region
> +  specified by BaseAddress and Length from their current attributes to the
> attributes specified
> +  by MapAttribute and Mask.
> +
> +  @param[in] PageTable        Pointer to Page table address.
> +  @param[in] PagingMode       The paging mode.
> +  @param[in] BaseAddress      The start of the linear address range.
> +  @param[in] Length           The length of the linear address range.
> +  @param[in] MapAttribute     The attribute of the linear address range.
> +  @param[in] MapMask          The mask used for attribute.
>  **/
>  VOID
> -Split2MPageTo4K (
> -  IN EFI_PHYSICAL_ADDRESS  PhysicalAddress,
> -  IN OUT UINT64            *PageEntry2M,
> -  IN EFI_PHYSICAL_ADDRESS  StackBase,
> -  IN UINTN                 StackSize,
> -  IN EFI_PHYSICAL_ADDRESS  GhcbBase,
> -  IN UINTN                 GhcbSize
> +CreateOrUpdatePageTable (
> +  IN  UINTN               *PageTable,
> +  IN  PAGING_MODE         PagingMode,
> +  IN  PHYSICAL_ADDRESS    BaseAddress,
> +  IN  UINT64              Length,
> +  IN  IA32_MAP_ATTRIBUTE  *MapAttribute,
> +  IN  IA32_MAP_ATTRIBUTE  *MapMask
>    )
>  {
> -  EFI_PHYSICAL_ADDRESS  PhysicalAddress4K;
> -  UINTN                 IndexOfPageTableEntries;
> -  PAGE_TABLE_4K_ENTRY   *PageTableEntry;
> -  UINT64                AddressEncMask;
> -
> -  //
> -  // Make sure AddressEncMask is contained to smallest supported address
> field
> -  //
> -  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask)
> & PAGING_1G_ADDRESS_MASK_64;
> -
> -  PageTableEntry = AllocatePageTableMemory (1);
> -  ASSERT (PageTableEntry != NULL);
> -
> -  //
> -  // Fill in 2M page entry.
> -  //
> -  *PageEntry2M = (UINT64)(UINTN)PageTableEntry | AddressEncMask |
> IA32_PG_P | IA32_PG_RW;
> -
> -  PhysicalAddress4K = PhysicalAddress;
> -  for (IndexOfPageTableEntries = 0; IndexOfPageTableEntries < 512;
> IndexOfPageTableEntries++, PageTableEntry++, PhysicalAddress4K +=
> SIZE_4KB) {
> -    //
> -    // Fill in the Page Table entries
> -    //
> -    PageTableEntry->Uint64 = (UINT64)PhysicalAddress4K;
> -
> -    //
> -    // The GHCB range consists of two pages per CPU, the GHCB and a
> -    // per-CPU variable page. The GHCB page needs to be mapped as an
> -    // unencrypted page while the per-CPU variable page needs to be
> -    // mapped encrypted. These pages alternate in assignment.
> -    //
> -    if (  (GhcbBase == 0)
> -       || (PhysicalAddress4K < GhcbBase)
> -       || (PhysicalAddress4K >= GhcbBase + GhcbSize)
> -       || (((PhysicalAddress4K - GhcbBase) & SIZE_4KB) != 0))
> -    {
> -      PageTableEntry->Uint64 |= AddressEncMask;
> -    }
> -
> -    PageTableEntry->Bits.ReadWrite = 1;
> -
> -    if ((IsNullDetectionEnabled () && (PhysicalAddress4K == 0)) ||
> -        (PcdGetBool (PcdCpuStackGuard) && (PhysicalAddress4K ==
> StackBase)))
> -    {
> -      PageTableEntry->Bits.Present = 0;
> -    } else {
> -      PageTableEntry->Bits.Present = 1;
> -    }
> -
> -    if (  PcdGetBool (PcdSetNxForStack)
> -       && (PhysicalAddress4K >= StackBase)
> -       && (PhysicalAddress4K < StackBase + StackSize))
> -    {
> -      //
> -      // Set Nx bit for stack.
> -      //
> -      PageTableEntry->Bits.Nx = 1;
> -    }
> +  RETURN_STATUS  Status;
> +  UINTN          PageTableBufferSize;
> +  VOID           *PageTableBuffer;
> +
> +  PageTableBufferSize = 0;
> +  Status              = PageTableMap (PageTable, PagingMode, NULL,
> &PageTableBufferSize, BaseAddress, Length, MapAttribute, MapMask,
> NULL);
> +  if (Status == RETURN_BUFFER_TOO_SMALL) {
> +    PageTableBuffer = AllocatePageTableMemory (EFI_SIZE_TO_PAGES
> (PageTableBufferSize));
> +    DEBUG ((DEBUG_INFO, "DxeIpl: 0x%x bytes needed for page table\n",
> PageTableBufferSize));
> +    ASSERT (PageTableBuffer != NULL);
> +    Status = PageTableMap (PageTable, PagingMode, PageTableBuffer,
> &PageTableBufferSize, BaseAddress, Length, MapAttribute, MapMask,
> NULL);
>    }
> -}
> -
> -/**
> -  Split 1G page to 2M.
> 
> -  @param[in]      PhysicalAddress       Start physical address the 1G page
> covered.
> -  @param[in, out] PageEntry1G           Pointer to 1G page entry.
> -  @param[in]      StackBase             Stack base address.
> -  @param[in]      StackSize             Stack size.
> -  @param[in]      GhcbBase              GHCB page area base address.
> -  @param[in]      GhcbSize              GHCB page area size.
> -
> -**/
> -VOID
> -Split1GPageTo2M (
> -  IN EFI_PHYSICAL_ADDRESS  PhysicalAddress,
> -  IN OUT UINT64            *PageEntry1G,
> -  IN EFI_PHYSICAL_ADDRESS  StackBase,
> -  IN UINTN                 StackSize,
> -  IN EFI_PHYSICAL_ADDRESS  GhcbBase,
> -  IN UINTN                 GhcbSize
> -  )
> -{
> -  EFI_PHYSICAL_ADDRESS  PhysicalAddress2M;
> -  UINTN                 IndexOfPageDirectoryEntries;
> -  PAGE_TABLE_ENTRY      *PageDirectoryEntry;
> -  UINT64                AddressEncMask;
> -
> -  //
> -  // Make sure AddressEncMask is contained to smallest supported address
> field
> -  //
> -  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask)
> & PAGING_1G_ADDRESS_MASK_64;
> -
> -  PageDirectoryEntry = AllocatePageTableMemory (1);
> -  ASSERT (PageDirectoryEntry != NULL);
> -
> -  //
> -  // Fill in 1G page entry.
> -  //
> -  *PageEntry1G = (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask |
> IA32_PG_P | IA32_PG_RW;
> -
> -  PhysicalAddress2M = PhysicalAddress;
> -  for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries < 512;
> IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PhysicalAddress2M
> += SIZE_2MB) {
> -    if (ToSplitPageTable (PhysicalAddress2M, SIZE_2MB, StackBase, StackSize,
> GhcbBase, GhcbSize)) {
> -      //
> -      // Need to split this 2M page that covers NULL or stack range.
> -      //
> -      Split2MPageTo4K (PhysicalAddress2M, (UINT64 *)PageDirectoryEntry,
> StackBase, StackSize, GhcbBase, GhcbSize);
> -    } else {
> -      //
> -      // Fill in the Page Directory entries
> -      //
> -      PageDirectoryEntry->Uint64         = (UINT64)PhysicalAddress2M |
> AddressEncMask;
> -      PageDirectoryEntry->Bits.ReadWrite = 1;
> -      PageDirectoryEntry->Bits.Present   = 1;
> -      PageDirectoryEntry->Bits.MustBe1   = 1;
> -    }
> -  }
> +  ASSERT_RETURN_ERROR (Status);
> +  ASSERT (PageTableBufferSize == 0);
>  }
> 
>  /**
> @@ -657,19 +507,20 @@ EnablePageTableProtection (
>  }
> 
>  /**
> -  Allocates and fills in the Page Directory and Page Table Entries to
> +  Create IA32 PAE paging or 4-level/5-level paging for long mode to
>    establish a 1:1 Virtual to Physical mapping.
> 
> -  @param[in] StackBase  Stack base address.
> -  @param[in] StackSize  Stack size.
> -  @param[in] GhcbBase   GHCB base address.
> -  @param[in] GhcbSize   GHCB size.
> -
> -  @return The address of 4 level page map.
> +  @param[in] Is64BitPageTable     Whether to create 64-bit page table.
> +  @param[in] StackBase            Stack base address.
> +  @param[in] StackSize            Stack size.
> +  @param[in] GhcbBase             GHCB base address.
> +  @param[in] GhcbSize             GHCB size.
> 
> +  @return  PageTable Address
>  **/
>  UINTN
>  CreateIdentityMappingPageTables (
> +  IN BOOLEAN               Is64BitPageTable,
>    IN EFI_PHYSICAL_ADDRESS  StackBase,
>    IN UINTN                 StackSize,
>    IN EFI_PHYSICAL_ADDRESS  GhcbBase,
> @@ -680,274 +531,155 @@ CreateIdentityMappingPageTables (
>    CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_ECX  EcxFlags;
>    UINT32                                       RegEdx;
>    UINT8                                        PhysicalAddressBits;
> -  EFI_PHYSICAL_ADDRESS                         PageAddress;
> -  UINTN                                        IndexOfPml5Entries;
> -  UINTN                                        IndexOfPml4Entries;
> -  UINTN                                        IndexOfPdpEntries;
> -  UINTN                                        IndexOfPageDirectoryEntries;
> -  UINT32                                       NumberOfPml5EntriesNeeded;
> -  UINT32                                       NumberOfPml4EntriesNeeded;
> -  UINT32                                       NumberOfPdpEntriesNeeded;
> -  PAGE_MAP_AND_DIRECTORY_POINTER               *PageMapLevel5Entry;
> -  PAGE_MAP_AND_DIRECTORY_POINTER               *PageMapLevel4Entry;
> -  PAGE_MAP_AND_DIRECTORY_POINTER               *PageMap;
> -  PAGE_MAP_AND_DIRECTORY_POINTER
> *PageDirectoryPointerEntry;
> -  PAGE_TABLE_ENTRY                             *PageDirectoryEntry;
> -  UINTN                                        TotalPagesNum;
> -  UINTN                                        BigPageAddress;
>    VOID                                         *Hob;
>    BOOLEAN                                      Page5LevelSupport;
>    BOOLEAN                                      Page1GSupport;
> -  PAGE_TABLE_1G_ENTRY                          *PageDirectory1GEntry;
>    UINT64                                       AddressEncMask;
>    IA32_CR4                                     Cr4;
> -
> -  //
> -  // Set PageMapLevel5Entry to suppress incorrect compiler/analyzer
> warnings
> -  //
> -  PageMapLevel5Entry = NULL;
> +  PAGING_MODE                                  PagingMode;
> +  UINTN                                        PageTable;
> +  IA32_MAP_ATTRIBUTE                           MapAttribute;
> +  IA32_MAP_ATTRIBUTE                           MapMask;
> +  EFI_PHYSICAL_ADDRESS                         GhcbBase4K;
> 
>    //
>    // Make sure AddressEncMask is contained to smallest supported address
> field
>    //
> -  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask)
> & PAGING_1G_ADDRESS_MASK_64;
> -
> -  Page1GSupport = FALSE;
> -  if (PcdGetBool (PcdUse1GPageTable)) {
> -    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
> -    if (RegEax >= 0x80000001) {
> -      AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);
> -      if ((RegEdx & BIT26) != 0) {
> -        Page1GSupport = TRUE;
> +  AddressEncMask    = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask)
> & PAGING_1G_ADDRESS_MASK_64;
> +  Page5LevelSupport = FALSE;
> +  Page1GSupport     = FALSE;
> +
> +  if (!Is64BitPageTable) {
> +    PagingMode          = PagingPae;
> +    PhysicalAddressBits = 32;
> +  } else {
> +    if (PcdGetBool (PcdUse1GPageTable)) {
> +      AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
> +      if (RegEax >= 0x80000001) {
> +        AsmCpuid (0x80000001, NULL, NULL, NULL, &RegEdx);
> +        if ((RegEdx & BIT26) != 0) {
> +          Page1GSupport = TRUE;
> +        }
>        }
>      }
> -  }
> 
> -  //
> -  // Get physical address bits supported.
> -  //
> -  Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
> -  if (Hob != NULL) {
> -    PhysicalAddressBits = ((EFI_HOB_CPU *)Hob)->SizeOfMemorySpace;
> -  } else {
> -    AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
> -    if (RegEax >= 0x80000008) {
> -      AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
> -      PhysicalAddressBits = (UINT8)RegEax;
> +    //
> +    // Get physical address bits supported.
> +    //
> +    Hob = GetFirstHob (EFI_HOB_TYPE_CPU);
> +    if (Hob != NULL) {
> +      PhysicalAddressBits = ((EFI_HOB_CPU *)Hob)->SizeOfMemorySpace;
>      } else {
> -      PhysicalAddressBits = 36;
> +      AsmCpuid (0x80000000, &RegEax, NULL, NULL, NULL);
> +      if (RegEax >= 0x80000008) {
> +        AsmCpuid (0x80000008, &RegEax, NULL, NULL, NULL);
> +        PhysicalAddressBits = (UINT8)RegEax;
> +      } else {
> +        PhysicalAddressBits = 36;
> +      }
>      }
> -  }
> 
> -  Page5LevelSupport = FALSE;
> -  if (PcdGetBool (PcdUse5LevelPageTable)) {
> -    AsmCpuidEx (
> -      CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS,
> -      CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_SUB_LEAF_INFO,
> -      NULL,
> -      NULL,
> -      &EcxFlags.Uint32,
> -      NULL
> -      );
> -    if (EcxFlags.Bits.FiveLevelPage != 0) {
> -      Page5LevelSupport = TRUE;
> +    if (PcdGetBool (PcdUse5LevelPageTable)) {
> +      AsmCpuidEx (
> +        CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS,
> +        CPUID_STRUCTURED_EXTENDED_FEATURE_FLAGS_SUB_LEAF_INFO,
> +        NULL,
> +        NULL,
> +        &EcxFlags.Uint32,
> +        NULL
> +        );
> +      if (EcxFlags.Bits.FiveLevelPage != 0) {
> +        Page5LevelSupport = TRUE;
> +      }
>      }
> -  }
> -
> -  DEBUG ((DEBUG_INFO, "AddressBits=%u 5LevelPaging=%u 1GPage=%u\n",
> PhysicalAddressBits, Page5LevelSupport, Page1GSupport));
> 
> -  //
> -  // IA-32e paging translates 48-bit linear addresses to 52-bit physical
> addresses
> -  //  when 5-Level Paging is disabled,
> -  //  due to either unsupported by HW, or disabled by PCD.
> -  //
> -  ASSERT (PhysicalAddressBits <= 52);
> -  if (!Page5LevelSupport && (PhysicalAddressBits > 48)) {
> -    PhysicalAddressBits = 48;
> -  }
> -
> -  //
> -  // Calculate the table entries needed.
> -  //
> -  NumberOfPml5EntriesNeeded = 1;
> -  if (PhysicalAddressBits > 48) {
> -    NumberOfPml5EntriesNeeded = (UINT32)LShiftU64 (1,
> PhysicalAddressBits - 48);
> -    PhysicalAddressBits       = 48;
> -  }
> +    if (Page5LevelSupport) {
> +      if (Page1GSupport) {
> +        PagingMode = Paging5Level1GB;
> +      } else {
> +        PagingMode = Paging5Level;
> +      }
> +    } else {
> +      if (Page1GSupport) {
> +        PagingMode = Paging4Level1GB;
> +      } else {
> +        PagingMode = Paging4Level;
> +      }
> +    }
> 
> -  NumberOfPml4EntriesNeeded = 1;
> -  if (PhysicalAddressBits > 39) {
> -    NumberOfPml4EntriesNeeded = (UINT32)LShiftU64 (1,
> PhysicalAddressBits - 39);
> -    PhysicalAddressBits       = 39;
> +    DEBUG ((DEBUG_INFO, "AddressBits=%u 5LevelPaging=%u
> 1GPage=%u\n", PhysicalAddressBits, Page5LevelSupport, Page1GSupport));
> +    //
> +    // IA-32e paging translates 48-bit linear addresses to 52-bit physical
> addresses
> +    // when 5-Level Paging is disabled, due to either unsupported by HW, or
> disabled by PCD.
> +    //
> +    ASSERT (PhysicalAddressBits <= 52);
> +    if (!Page5LevelSupport && (PhysicalAddressBits > 48)) {
> +      PhysicalAddressBits = 48;
> +    }
>    }
> 
> -  NumberOfPdpEntriesNeeded = 1;
> -  ASSERT (PhysicalAddressBits > 30);
> -  NumberOfPdpEntriesNeeded = (UINT32)LShiftU64 (1, PhysicalAddressBits -
> 30);
> +  PageTable                   = 0;
> +  MapAttribute.Uint64         = AddressEncMask;
> +  MapAttribute.Bits.Present   = 1;
> +  MapAttribute.Bits.ReadWrite = 1;
> +  MapMask.Uint64              = MAX_UINT64;
> +  CreateOrUpdatePageTable (&PageTable, PagingMode, 0, LShiftU64 (1,
> PhysicalAddressBits), &MapAttribute, &MapMask);
> 
> -  //
> -  // Pre-allocate big pages to avoid later allocations.
> -  //
> -  if (!Page1GSupport) {
> -    TotalPagesNum = ((NumberOfPdpEntriesNeeded + 1) *
> NumberOfPml4EntriesNeeded + 1) * NumberOfPml5EntriesNeeded + 1;
> -  } else {
> -    TotalPagesNum = (NumberOfPml4EntriesNeeded + 1) *
> NumberOfPml5EntriesNeeded + 1;
> -  }
> -
> -  //
> -  // Substract the one page occupied by PML5 entries if 5-Level Paging is
> disabled.
> -  //
> -  if (!Page5LevelSupport) {
> -    TotalPagesNum--;
> +  if ((GhcbBase > 0) && (GhcbSize > 0) && (AddressEncMask != 0)) {
> +    //
> +    // The GHCB range consists of two pages per CPU, the GHCB and a
> +    // per-CPU variable page. The GHCB page needs to be mapped as an
> +    // unencrypted page while the per-CPU variable page needs to be
> +    // mapped encrypted. These pages alternate in assignment.
> +    //
> +    ASSERT (Is64BitPageTable == TRUE);
> +    GhcbBase4K                           = ALIGN_VALUE (GhcbBase, SIZE_4KB);
> +    MapAttribute.Uint64                  = GhcbBase4K;
> +    MapMask.Uint64                       = 0;
> +    MapMask.Bits.PageTableBaseAddressLow = 1;
> +    CreateOrUpdatePageTable (&PageTable, PagingMode, GhcbBase4K,
> SIZE_4KB, &MapAttribute, &MapMask);
>    }
> 
> -  DEBUG ((
> -    DEBUG_INFO,
> -    "Pml5=%u Pml4=%u Pdp=%u TotalPage=%Lu\n",
> -    NumberOfPml5EntriesNeeded,
> -    NumberOfPml4EntriesNeeded,
> -    NumberOfPdpEntriesNeeded,
> -    (UINT64)TotalPagesNum
> -    ));
> -
> -  BigPageAddress = (UINTN)AllocatePageTableMemory (TotalPagesNum);
> -  ASSERT (BigPageAddress != 0);
> -
> -  //
> -  // By architecture only one PageMapLevel4 exists - so lets allocate storage
> for it.
> -  //
> -  PageMap = (VOID *)BigPageAddress;
> -  if (Page5LevelSupport) {
> +  if (PcdGetBool (PcdSetNxForStack)) {
>      //
> -    // By architecture only one PageMapLevel5 exists - so lets allocate storage
> for it.
> +    // Set the stack as Nx in page table.
>      //
> -    PageMapLevel5Entry = PageMap;
> -    BigPageAddress    += SIZE_4KB;
> +    MapAttribute.Uint64  = 0;
> +    MapAttribute.Bits.Nx = 1;
> +    MapMask.Uint64       = 0;
> +    MapMask.Bits.Nx      = 1;
> +    CreateOrUpdatePageTable (&PageTable, PagingMode, StackBase,
> StackSize, &MapAttribute, &MapMask);
>    }
> 
> -  PageAddress = 0;
> -
> -  for ( IndexOfPml5Entries = 0
> -        ; IndexOfPml5Entries < NumberOfPml5EntriesNeeded
> -        ; IndexOfPml5Entries++)
> -  {
> +  MapAttribute.Uint64       = 0;
> +  MapAttribute.Bits.Present = 0;
> +  MapMask.Uint64            = 0;
> +  MapMask.Bits.Present      = 1;
> +  if (IsNullDetectionEnabled ()) {
>      //
> -    // Each PML5 entry points to a page of PML4 entires.
> -    // So lets allocate space for them and fill them in in the
> IndexOfPml4Entries loop.
> -    // When 5-Level Paging is disabled, below allocation happens only once.
> +    // Set [0, 4KB] as not-present in page table.
>      //
> -    PageMapLevel4Entry = (VOID *)BigPageAddress;
> -    BigPageAddress    += SIZE_4KB;
> -
> -    if (Page5LevelSupport) {
> -      //
> -      // Make a PML5 Entry
> -      //
> -      PageMapLevel5Entry->Uint64         =
> (UINT64)(UINTN)PageMapLevel4Entry | AddressEncMask;
> -      PageMapLevel5Entry->Bits.ReadWrite = 1;
> -      PageMapLevel5Entry->Bits.Present   = 1;
> -      PageMapLevel5Entry++;
> -    }
> -
> -    for ( IndexOfPml4Entries = 0
> -          ; IndexOfPml4Entries < (NumberOfPml5EntriesNeeded == 1 ?
> NumberOfPml4EntriesNeeded : 512)
> -          ; IndexOfPml4Entries++, PageMapLevel4Entry++)
> -    {
> -      //
> -      // Each PML4 entry points to a page of Page Directory Pointer entires.
> -      // So lets allocate space for them and fill them in in the
> IndexOfPdpEntries loop.
> -      //
> -      PageDirectoryPointerEntry = (VOID *)BigPageAddress;
> -      BigPageAddress           += SIZE_4KB;
> -
> -      //
> -      // Make a PML4 Entry
> -      //
> -      PageMapLevel4Entry->Uint64         =
> (UINT64)(UINTN)PageDirectoryPointerEntry | AddressEncMask;
> -      PageMapLevel4Entry->Bits.ReadWrite = 1;
> -      PageMapLevel4Entry->Bits.Present   = 1;
> -
> -      if (Page1GSupport) {
> -        PageDirectory1GEntry = (VOID *)PageDirectoryPointerEntry;
> -
> -        for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries <
> 512; IndexOfPageDirectoryEntries++, PageDirectory1GEntry++, PageAddress
> += SIZE_1GB) {
> -          if (ToSplitPageTable (PageAddress, SIZE_1GB, StackBase, StackSize,
> GhcbBase, GhcbSize)) {
> -            Split1GPageTo2M (PageAddress, (UINT64 *)PageDirectory1GEntry,
> StackBase, StackSize, GhcbBase, GhcbSize);
> -          } else {
> -            //
> -            // Fill in the Page Directory entries
> -            //
> -            PageDirectory1GEntry->Uint64         = (UINT64)PageAddress |
> AddressEncMask;
> -            PageDirectory1GEntry->Bits.ReadWrite = 1;
> -            PageDirectory1GEntry->Bits.Present   = 1;
> -            PageDirectory1GEntry->Bits.MustBe1   = 1;
> -          }
> -        }
> -      } else {
> -        for ( IndexOfPdpEntries = 0
> -              ; IndexOfPdpEntries < (NumberOfPml4EntriesNeeded == 1 ?
> NumberOfPdpEntriesNeeded : 512)
> -              ; IndexOfPdpEntries++, PageDirectoryPointerEntry++)
> -        {
> -          //
> -          // Each Directory Pointer entries points to a page of Page Directory
> entires.
> -          // So allocate space for them and fill them in in the
> IndexOfPageDirectoryEntries loop.
> -          //
> -          PageDirectoryEntry = (VOID *)BigPageAddress;
> -          BigPageAddress    += SIZE_4KB;
> -
> -          //
> -          // Fill in a Page Directory Pointer Entries
> -          //
> -          PageDirectoryPointerEntry->Uint64         =
> (UINT64)(UINTN)PageDirectoryEntry | AddressEncMask;
> -          PageDirectoryPointerEntry->Bits.ReadWrite = 1;
> -          PageDirectoryPointerEntry->Bits.Present   = 1;
> -
> -          for (IndexOfPageDirectoryEntries = 0; IndexOfPageDirectoryEntries <
> 512; IndexOfPageDirectoryEntries++, PageDirectoryEntry++, PageAddress +=
> SIZE_2MB) {
> -            if (ToSplitPageTable (PageAddress, SIZE_2MB, StackBase, StackSize,
> GhcbBase, GhcbSize)) {
> -              //
> -              // Need to split this 2M page that covers NULL or stack range.
> -              //
> -              Split2MPageTo4K (PageAddress, (UINT64 *)PageDirectoryEntry,
> StackBase, StackSize, GhcbBase, GhcbSize);
> -            } else {
> -              //
> -              // Fill in the Page Directory entries
> -              //
> -              PageDirectoryEntry->Uint64         = (UINT64)PageAddress |
> AddressEncMask;
> -              PageDirectoryEntry->Bits.ReadWrite = 1;
> -              PageDirectoryEntry->Bits.Present   = 1;
> -              PageDirectoryEntry->Bits.MustBe1   = 1;
> -            }
> -          }
> -        }
> -
> -        //
> -        // Fill with null entry for unused PDPTE
> -        //
> -        ZeroMem (PageDirectoryPointerEntry, (512 - IndexOfPdpEntries) *
> sizeof (PAGE_MAP_AND_DIRECTORY_POINTER));
> -      }
> -    }
> +    CreateOrUpdatePageTable (&PageTable, PagingMode, 0, SIZE_4KB,
> &MapAttribute, &MapMask);
> +  }
> 
> +  if (PcdGetBool (PcdCpuStackGuard)) {
>      //
> -    // For the PML4 entries we are not using fill in a null entry.
> +    // Set the the last 4KB of stack as not-present in page table.
>      //
> -    ZeroMem (PageMapLevel4Entry, (512 - IndexOfPml4Entries) * sizeof
> (PAGE_MAP_AND_DIRECTORY_POINTER));
> +    CreateOrUpdatePageTable (&PageTable, PagingMode, StackBase,
> SIZE_4KB, &MapAttribute, &MapMask);
>    }
> 
>    if (Page5LevelSupport) {
>      Cr4.UintN     = AsmReadCr4 ();
>      Cr4.Bits.LA57 = 1;
>      AsmWriteCr4 (Cr4.UintN);
> -    //
> -    // For the PML5 entries we are not using fill in a null entry.
> -    //
> -    ZeroMem (PageMapLevel5Entry, (512 - IndexOfPml5Entries) * sizeof
> (PAGE_MAP_AND_DIRECTORY_POINTER));
>    }
> 
>    //
>    // Protect the page table by marking the memory used for page table to be
>    // read-only.
>    //
> -  EnablePageTableProtection ((UINTN)PageMap, TRUE);
> +  EnablePageTableProtection ((UINTN)PageTable, TRUE);
> 
>    //
>    // Set IA32_EFER.NXE if necessary.
> @@ -956,5 +688,5 @@ CreateIdentityMappingPageTables (
>      EnableExecuteDisableBit ();
>    }
> 
> -  return (UINTN)PageMap;
> +  return PageTable;
>  }
> diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
> b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
> index 616ebe42b0..a6cf31811d 100644
> --- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
> +++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
> @@ -7,7 +7,7 @@
>      3) IA-32 Intel(R) Architecture Software Developer's Manual Volume
> 3:System Programmer's Guide, Intel
>      4) AMD64 Architecture Programmer's Manual Volume 2: System
> Programming
> 
> -Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.<BR>
>  Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
> 
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> @@ -46,99 +46,6 @@ typedef struct {
>    UINT32                      Reserved;
>  } X64_IDT_GATE_DESCRIPTOR;
> 
> -//
> -// Page-Map Level-4 Offset (PML4) and
> -// Page-Directory-Pointer Offset (PDPE) entries 4K & 2MB
> -//
> -
> -typedef union {
> -  struct {
> -    UINT64    Present              : 1;  // 0 = Not present in memory, 1 = Present in
> memory
> -    UINT64    ReadWrite            : 1;  // 0 = Read-Only, 1= Read/Write
> -    UINT64    UserSupervisor       : 1;  // 0 = Supervisor, 1=User
> -    UINT64    WriteThrough         : 1;  // 0 = Write-Back caching, 1=Write-
> Through caching
> -    UINT64    CacheDisabled        : 1;  // 0 = Cached, 1=Non-Cached
> -    UINT64    Accessed             : 1;  // 0 = Not accessed, 1 = Accessed (set by CPU)
> -    UINT64    Reserved             : 1;  // Reserved
> -    UINT64    MustBeZero           : 2;  // Must Be Zero
> -    UINT64    Available            : 3;  // Available for use by system software
> -    UINT64    PageTableBaseAddress : 40; // Page Table Base Address
> -    UINT64    AvabilableHigh       : 11; // Available for use by system software
> -    UINT64    Nx                   : 1;  // No Execute bit
> -  } Bits;
> -  UINT64    Uint64;
> -} PAGE_MAP_AND_DIRECTORY_POINTER;
> -
> -//
> -// Page Table Entry 4KB
> -//
> -typedef union {
> -  struct {
> -    UINT64    Present              : 1;  // 0 = Not present in memory, 1 = Present in
> memory
> -    UINT64    ReadWrite            : 1;  // 0 = Read-Only, 1= Read/Write
> -    UINT64    UserSupervisor       : 1;  // 0 = Supervisor, 1=User
> -    UINT64    WriteThrough         : 1;  // 0 = Write-Back caching, 1=Write-
> Through caching
> -    UINT64    CacheDisabled        : 1;  // 0 = Cached, 1=Non-Cached
> -    UINT64    Accessed             : 1;  // 0 = Not accessed, 1 = Accessed (set by CPU)
> -    UINT64    Dirty                : 1;  // 0 = Not Dirty, 1 = written by processor on
> access to page
> -    UINT64    PAT                  : 1;  //
> -    UINT64    Global               : 1;  // 0 = Not global page, 1 = global page TLB not
> cleared on CR3 write
> -    UINT64    Available            : 3;  // Available for use by system software
> -    UINT64    PageTableBaseAddress : 40; // Page Table Base Address
> -    UINT64    AvabilableHigh       : 11; // Available for use by system software
> -    UINT64    Nx                   : 1;  // 0 = Execute Code, 1 = No Code Execution
> -  } Bits;
> -  UINT64    Uint64;
> -} PAGE_TABLE_4K_ENTRY;
> -
> -//
> -// Page Table Entry 2MB
> -//
> -typedef union {
> -  struct {
> -    UINT64    Present              : 1;  // 0 = Not present in memory, 1 = Present in
> memory
> -    UINT64    ReadWrite            : 1;  // 0 = Read-Only, 1= Read/Write
> -    UINT64    UserSupervisor       : 1;  // 0 = Supervisor, 1=User
> -    UINT64    WriteThrough         : 1;  // 0 = Write-Back caching, 1=Write-
> Through caching
> -    UINT64    CacheDisabled        : 1;  // 0 = Cached, 1=Non-Cached
> -    UINT64    Accessed             : 1;  // 0 = Not accessed, 1 = Accessed (set by CPU)
> -    UINT64    Dirty                : 1;  // 0 = Not Dirty, 1 = written by processor on
> access to page
> -    UINT64    MustBe1              : 1;  // Must be 1
> -    UINT64    Global               : 1;  // 0 = Not global page, 1 = global page TLB not
> cleared on CR3 write
> -    UINT64    Available            : 3;  // Available for use by system software
> -    UINT64    PAT                  : 1;  //
> -    UINT64    MustBeZero           : 8;  // Must be zero;
> -    UINT64    PageTableBaseAddress : 31; // Page Table Base Address
> -    UINT64    AvabilableHigh       : 11; // Available for use by system software
> -    UINT64    Nx                   : 1;  // 0 = Execute Code, 1 = No Code Execution
> -  } Bits;
> -  UINT64    Uint64;
> -} PAGE_TABLE_ENTRY;
> -
> -//
> -// Page Table Entry 1GB
> -//
> -typedef union {
> -  struct {
> -    UINT64    Present              : 1;  // 0 = Not present in memory, 1 = Present in
> memory
> -    UINT64    ReadWrite            : 1;  // 0 = Read-Only, 1= Read/Write
> -    UINT64    UserSupervisor       : 1;  // 0 = Supervisor, 1=User
> -    UINT64    WriteThrough         : 1;  // 0 = Write-Back caching, 1=Write-
> Through caching
> -    UINT64    CacheDisabled        : 1;  // 0 = Cached, 1=Non-Cached
> -    UINT64    Accessed             : 1;  // 0 = Not accessed, 1 = Accessed (set by CPU)
> -    UINT64    Dirty                : 1;  // 0 = Not Dirty, 1 = written by processor on
> access to page
> -    UINT64    MustBe1              : 1;  // Must be 1
> -    UINT64    Global               : 1;  // 0 = Not global page, 1 = global page TLB not
> cleared on CR3 write
> -    UINT64    Available            : 3;  // Available for use by system software
> -    UINT64    PAT                  : 1;  //
> -    UINT64    MustBeZero           : 17; // Must be zero;
> -    UINT64    PageTableBaseAddress : 22; // Page Table Base Address
> -    UINT64    AvabilableHigh       : 11; // Available for use by system software
> -    UINT64    Nx                   : 1;  // 0 = Execute Code, 1 = No Code Execution
> -  } Bits;
> -  UINT64    Uint64;
> -} PAGE_TABLE_1G_ENTRY;
> -
>  #pragma pack()
> 
>  #define CR0_WP  BIT16
> @@ -194,44 +101,25 @@ EnableExecuteDisableBit (
>    );
> 
>  /**
> -  Split 2M page to 4K.
> -
> -  @param[in]      PhysicalAddress       Start physical address the 2M page
> covered.
> -  @param[in, out] PageEntry2M           Pointer to 2M page entry.
> -  @param[in]      StackBase             Stack base address.
> -  @param[in]      StackSize             Stack size.
> -  @param[in]      GhcbBase              GHCB page area base address.
> -  @param[in]      GhcbSize              GHCB page area size.
> -
> -**/
> -VOID
> -Split2MPageTo4K (
> -  IN EFI_PHYSICAL_ADDRESS  PhysicalAddress,
> -  IN OUT UINT64            *PageEntry2M,
> -  IN EFI_PHYSICAL_ADDRESS  StackBase,
> -  IN UINTN                 StackSize,
> -  IN EFI_PHYSICAL_ADDRESS  GhcbBase,
> -  IN UINTN                 GhcbSize
> -  );
> -
> -/**
> -  Allocates and fills in the Page Directory and Page Table Entries to
> +  Create IA32 PAE paging or 4-level/5-level paging for long mode to
>    establish a 1:1 Virtual to Physical mapping.
> 
> -  @param[in] StackBase  Stack base address.
> -  @param[in] StackSize  Stack size.
> -  @param[in] GhcbBase   GHCB page area base address.
> -  @param[in] GhcbSize   GHCB page area size.
> +  @param[in] Is64BitPageTable   Whether to create 64-bit page table.
> +  @param[in] StackBase          Stack base address.
> +  @param[in] StackSize          Stack size.
> +  @param[in] GhcbBase           GHCB page area base address.
> +  @param[in] GhcbSize           GHCB page area size.
> 
> -  @return The address of 4 level page map.
> +  @return The address of page table.
> 
>  **/
>  UINTN
>  CreateIdentityMappingPageTables (
> +  IN BOOLEAN               Is64BitPageTable,
>    IN EFI_PHYSICAL_ADDRESS  StackBase,
>    IN UINTN                 StackSize,
>    IN EFI_PHYSICAL_ADDRESS  GhcbBase,
> -  IN UINTN                 GhcbkSize
> +  IN UINTN                 GhcbSize
>    );
> 
>  /**
> @@ -289,39 +177,4 @@ IsNullDetectionEnabled (
>    VOID
>    );
> 
> -/**
> -  Prevent the memory pages used for page table from been overwritten.
> -
> -  @param[in] PageTableBase    Base address of page table (CR3).
> -  @param[in] Level4Paging     Level 4 paging flag.
> -
> -**/
> -VOID
> -EnablePageTableProtection (
> -  IN  UINTN    PageTableBase,
> -  IN  BOOLEAN  Level4Paging
> -  );
> -
> -/**
> -  This API provides a way to allocate memory for page table.
> -
> -  This API can be called more than once to allocate memory for page tables.
> -
> -  Allocates the number of 4KB pages and returns a pointer to the allocated
> -  buffer. The buffer returned is aligned on a 4KB boundary.
> -
> -  If Pages is 0, then NULL is returned.
> -  If there is not enough memory remaining to satisfy the request, then NULL
> is
> -  returned.
> -
> -  @param  Pages                 The number of 4 KB pages to allocate.
> -
> -  @return A pointer to the allocated buffer or NULL if allocation fails.
> -
> -**/
> -VOID *
> -AllocatePageTableMemory (
> -  IN UINTN  Pages
> -  );
> -
>  #endif
> --
> 2.31.1.windows.1


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [Patch V2 8/8] MdeModulePkg/DxeIpl: Refinement to the code to set PageTable as RO
  2023-03-31  9:33 ` [Patch V2 8/8] MdeModulePkg/DxeIpl: Refinement to the code to set PageTable as RO duntan
@ 2023-03-31 10:25   ` Ni, Ray
  0 siblings, 0 replies; 32+ messages in thread
From: Ni, Ray @ 2023-03-31 10:25 UTC (permalink / raw)
  To: Tan, Dun, devel@edk2.groups.io; +Cc: Bi, Dandan, Gao, Liming, Wang, Jian J

Reviewed-by: Ray Ni <ray.ni@intel.com>

> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Friday, March 31, 2023 5:34 PM
> To: devel@edk2.groups.io
> Cc: Bi, Dandan <dandan.bi@intel.com>; Gao, Liming
> <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J
> <jian.j.wang@intel.com>
> Subject: [Patch V2 8/8] MdeModulePkg/DxeIpl: Refinement to the code to
> set PageTable as RO
> 
> Code refinement to the code to set page table as RO in DxeIpl module.
> Set all page table pools as ReadOnly by calling PageTableMap() in
> CpuPageTableLib multiple times instead of searching each page table
> pool address in page table layer by layer. Also, this commit solve
> the issue that original SetPageTablePoolReadOnly() code in DxeIpl
> doesn't handle the Level5Paging case.
> 
> Bugzila: https://bugzilla.tianocore.org/show_bug.cgi?id=4176
> Signed-off-by: Dun Tan <dun.tan@intel.com>
> Cc: Dandan Bi <dandan.bi@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> ---
>  MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c | 155
> +++++++++++++++----------------------------------------------------------------------
> ----------------------------------------------------------------------
>  MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h |  15 ---------------
>  2 files changed, 15 insertions(+), 155 deletions(-)
> 
> diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
> b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
> index ecdbd2ca24..a9edf4de32 100644
> --- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
> +++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.c
> @@ -330,154 +330,37 @@ CreateOrUpdatePageTable (
>    ASSERT (PageTableBufferSize == 0);
>  }
> 
> -/**
> -  Set one page of page table pool memory to be read-only.
> -
> -  @param[in] PageTableBase    Base address of page table (CR3).
> -  @param[in] Address          Start address of a page to be set as read-only.
> -  @param[in] Level4Paging     Level 4 paging flag.
> -
> -**/
> -VOID
> -SetPageTablePoolReadOnly (
> -  IN  UINTN                 PageTableBase,
> -  IN  EFI_PHYSICAL_ADDRESS  Address,
> -  IN  BOOLEAN               Level4Paging
> -  )
> -{
> -  UINTN                 Index;
> -  UINTN                 EntryIndex;
> -  UINT64                AddressEncMask;
> -  EFI_PHYSICAL_ADDRESS  PhysicalAddress;
> -  UINT64                *PageTable;
> -  UINT64                *NewPageTable;
> -  UINT64                PageAttr;
> -  UINT64                LevelSize[5];
> -  UINT64                LevelMask[5];
> -  UINTN                 LevelShift[5];
> -  UINTN                 Level;
> -  UINT64                PoolUnitSize;
> -
> -  ASSERT (PageTableBase != 0);
> -
> -  //
> -  // Since the page table is always from page table pool, which is always
> -  // located at the boundary of PcdPageTablePoolAlignment, we just need to
> -  // set the whole pool unit to be read-only.
> -  //
> -  Address = Address & PAGE_TABLE_POOL_ALIGN_MASK;
> -
> -  LevelShift[1] = PAGING_L1_ADDRESS_SHIFT;
> -  LevelShift[2] = PAGING_L2_ADDRESS_SHIFT;
> -  LevelShift[3] = PAGING_L3_ADDRESS_SHIFT;
> -  LevelShift[4] = PAGING_L4_ADDRESS_SHIFT;
> -
> -  LevelMask[1] = PAGING_4K_ADDRESS_MASK_64;
> -  LevelMask[2] = PAGING_2M_ADDRESS_MASK_64;
> -  LevelMask[3] = PAGING_1G_ADDRESS_MASK_64;
> -  LevelMask[4] = PAGING_1G_ADDRESS_MASK_64;
> -
> -  LevelSize[1] = SIZE_4KB;
> -  LevelSize[2] = SIZE_2MB;
> -  LevelSize[3] = SIZE_1GB;
> -  LevelSize[4] = SIZE_512GB;
> -
> -  AddressEncMask = PcdGet64 (PcdPteMemoryEncryptionAddressOrMask)
> &
> -                   PAGING_1G_ADDRESS_MASK_64;
> -  PageTable    = (UINT64 *)(UINTN)PageTableBase;
> -  PoolUnitSize = PAGE_TABLE_POOL_UNIT_SIZE;
> -
> -  for (Level = (Level4Paging) ? 4 : 3; Level > 0; --Level) {
> -    Index  = ((UINTN)RShiftU64 (Address, LevelShift[Level]));
> -    Index &= PAGING_PAE_INDEX_MASK;
> -
> -    PageAttr = PageTable[Index];
> -    if ((PageAttr & IA32_PG_PS) == 0) {
> -      //
> -      // Go to next level of table.
> -      //
> -      PageTable = (UINT64 *)(UINTN)(PageAttr & ~AddressEncMask &
> -                                    PAGING_4K_ADDRESS_MASK_64);
> -      continue;
> -    }
> -
> -    if (PoolUnitSize >= LevelSize[Level]) {
> -      //
> -      // Clear R/W bit if current page granularity is not larger than pool unit
> -      // size.
> -      //
> -      if ((PageAttr & IA32_PG_RW) != 0) {
> -        while (PoolUnitSize > 0) {
> -          //
> -          // PAGE_TABLE_POOL_UNIT_SIZE and
> PAGE_TABLE_POOL_ALIGNMENT are fit in
> -          // one page (2MB). Then we don't need to update attributes for pages
> -          // crossing page directory. ASSERT below is for that purpose.
> -          //
> -          ASSERT (Index < EFI_PAGE_SIZE/sizeof (UINT64));
> -
> -          PageTable[Index] &= ~(UINT64)IA32_PG_RW;
> -          PoolUnitSize     -= LevelSize[Level];
> -
> -          ++Index;
> -        }
> -      }
> -
> -      break;
> -    } else {
> -      //
> -      // The smaller granularity of page must be needed.
> -      //
> -      ASSERT (Level > 1);
> -
> -      NewPageTable = AllocatePageTableMemory (1);
> -      ASSERT (NewPageTable != NULL);
> -
> -      PhysicalAddress = PageAttr & LevelMask[Level];
> -      for (EntryIndex = 0;
> -           EntryIndex < EFI_PAGE_SIZE/sizeof (UINT64);
> -           ++EntryIndex)
> -      {
> -        NewPageTable[EntryIndex] = PhysicalAddress  | AddressEncMask |
> -                                   IA32_PG_P | IA32_PG_RW;
> -        if (Level > 2) {
> -          NewPageTable[EntryIndex] |= IA32_PG_PS;
> -        }
> -
> -        PhysicalAddress += LevelSize[Level - 1];
> -      }
> -
> -      PageTable[Index] = (UINT64)(UINTN)NewPageTable | AddressEncMask |
> -                         IA32_PG_P | IA32_PG_RW;
> -      PageTable = NewPageTable;
> -    }
> -  }
> -}
> -
>  /**
>    Prevent the memory pages used for page table from been overwritten.
> 
> -  @param[in] PageTableBase    Base address of page table (CR3).
> -  @param[in] Level4Paging     Level 4 paging flag.
> +  @param[in] PageTableBase   Base address of page table (CR3).
> +  @param[in] PagingMode      The paging mode.
> 
>  **/
>  VOID
>  EnablePageTableProtection (
> -  IN  UINTN    PageTableBase,
> -  IN  BOOLEAN  Level4Paging
> +  IN  UINTN        PageTableBase,
> +  IN  PAGING_MODE  PagingMode
>    )
>  {
>    PAGE_TABLE_POOL       *HeadPool;
>    PAGE_TABLE_POOL       *Pool;
>    UINT64                PoolSize;
>    EFI_PHYSICAL_ADDRESS  Address;
> +  IA32_MAP_ATTRIBUTE    MapAttribute;
> +  IA32_MAP_ATTRIBUTE    MapMask;
> 
>    if (mPageTablePool == NULL) {
>      return;
>    }
> 
> +  MapAttribute.Uint64         = 0;
> +  MapAttribute.Bits.ReadWrite = 0;
> +  MapMask.Uint64              = 0;
> +  MapMask.Bits.ReadWrite      = 1;
> +
>    //
> -  // No need to clear CR0.WP since PageTableBase has't been written to CR3
> yet.
> -  // SetPageTablePoolReadOnly might update mPageTablePool. It's safer to
> +  // CreateOrUpdatePageTable might update mPageTablePool. It's safer to
>    // remember original one in advance.
>    //
>    HeadPool = mPageTablePool;
> @@ -485,18 +368,10 @@ EnablePageTableProtection (
>    do {
>      Address  = (EFI_PHYSICAL_ADDRESS)(UINTN)Pool;
>      PoolSize = Pool->Offset + EFI_PAGES_TO_SIZE (Pool->FreePages);
> -
>      //
> -    // The size of one pool must be multiple of
> PAGE_TABLE_POOL_UNIT_SIZE, which
> -    // is one of page size of the processor (2MB by default). Let's apply the
> -    // protection to them one by one.
> +    // Set entire pool including header, used-memory and left free-memory
> as ReadOnly.
>      //
> -    while (PoolSize > 0) {
> -      SetPageTablePoolReadOnly (PageTableBase, Address, Level4Paging);
> -      Address  += PAGE_TABLE_POOL_UNIT_SIZE;
> -      PoolSize -= PAGE_TABLE_POOL_UNIT_SIZE;
> -    }
> -
> +    CreateOrUpdatePageTable (&PageTableBase, PagingMode, Address,
> PoolSize, &MapAttribute, &MapMask);
>      Pool = Pool->NextPool;
>    } while (Pool != HeadPool);
> 
> @@ -679,7 +554,7 @@ CreateIdentityMappingPageTables (
>    // Protect the page table by marking the memory used for page table to be
>    // read-only.
>    //
> -  EnablePageTableProtection ((UINTN)PageTable, TRUE);
> +  EnablePageTableProtection (PageTable, PagingMode);
> 
>    //
>    // Set IA32_EFER.NXE if necessary.
> diff --git a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
> b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
> index a6cf31811d..034c4249d4 100644
> --- a/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
> +++ b/MdeModulePkg/Core/DxeIplPeim/X64/VirtualMemory.h
> @@ -50,23 +50,8 @@ typedef struct {
> 
>  #define CR0_WP  BIT16
> 
> -#define IA32_PG_P   BIT0
> -#define IA32_PG_RW  BIT1
> -#define IA32_PG_PS  BIT7
> -
> -#define PAGING_PAE_INDEX_MASK  0x1FF
> -
> -#define PAGING_4K_ADDRESS_MASK_64  0x000FFFFFFFFFF000ull
> -#define PAGING_2M_ADDRESS_MASK_64  0x000FFFFFFFE00000ull
>  #define PAGING_1G_ADDRESS_MASK_64  0x000FFFFFC0000000ull
> 
> -#define PAGING_L1_ADDRESS_SHIFT  12
> -#define PAGING_L2_ADDRESS_SHIFT  21
> -#define PAGING_L3_ADDRESS_SHIFT  30
> -#define PAGING_L4_ADDRESS_SHIFT  39
> -
> -#define PAGING_PML4E_NUMBER  4
> -
>  #define PAGE_TABLE_POOL_ALIGNMENT   BASE_2MB
>  #define PAGE_TABLE_POOL_UNIT_SIZE   SIZE_2MB
>  #define PAGE_TABLE_POOL_UNIT_PAGES  EFI_SIZE_TO_PAGES
> (PAGE_TABLE_POOL_UNIT_SIZE)
> --
> 2.31.1.windows.1


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file
  2023-03-31  9:33 ` [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file duntan
@ 2023-03-31 11:50   ` Gerd Hoffmann
  0 siblings, 0 replies; 32+ messages in thread
From: Gerd Hoffmann @ 2023-03-31 11:50 UTC (permalink / raw)
  To: devel, dun.tan; +Cc: Ard Biesheuvel, Jiewen Yao, Jordan Justen, Ray Ni

On Fri, Mar 31, 2023 at 05:33:40PM +0800, duntan wrote:
> Add CpuPageTableLib instance required by DxeIpl in corresponding
> DSC files of OvmfPkg.
> 
> Signed-off-by: Dun Tan <dun.tan@intel.com>
> Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
> Cc: Jiewen Yao <jiewen.yao@intel.com>
> Cc: Jordan Justen <jordan.l.justen@intel.com>
> Cc: Gerd Hoffmann <kraxel@redhat.com>
> Cc: Ray Ni <ray.ni@intel.com>

Acked-by: Gerd Hoffmann <kraxel@redhat.com>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file
       [not found] ` <1751776493F12DAB.27612@groups.io>
@ 2023-04-11  6:51   ` duntan
  2023-04-11  8:56     ` Gerd Hoffmann
  0 siblings, 1 reply; 32+ messages in thread
From: duntan @ 2023-04-11  6:51 UTC (permalink / raw)
  To: devel@edk2.groups.io, Tan, Dun
  Cc: Ard Biesheuvel, Yao, Jiewen, Justen, Jordan L, Gerd Hoffmann,
	Ni, Ray

Hi all, 
Could you please help review this patch?

Thanks,
Dun

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of duntan
Sent: Friday, March 31, 2023 5:34 PM
To: devel@edk2.groups.io
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>; Yao, Jiewen <jiewen.yao@intel.com>; Justen, Jordan L <jordan.l.justen@intel.com>; Gerd Hoffmann <kraxel@redhat.com>; Ni, Ray <ray.ni@intel.com>
Subject: [edk2-devel] [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file

Add CpuPageTableLib instance required by DxeIpl in corresponding DSC files of OvmfPkg.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
Cc: Jiewen Yao <jiewen.yao@intel.com>
Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
Cc: Ray Ni <ray.ni@intel.com>
---
 OvmfPkg/AmdSev/AmdSevX64.dsc   | 2 +-
 OvmfPkg/Bhyve/BhyveX64.dsc     | 3 ++-
 OvmfPkg/CloudHv/CloudHvX64.dsc | 2 +-
 OvmfPkg/Microvm/MicrovmX64.dsc | 2 +-
 OvmfPkg/OvmfPkgIa32.dsc        | 3 ++-
 OvmfPkg/OvmfPkgIa32X64.dsc     | 2 +-
 OvmfPkg/OvmfPkgX64.dsc         | 2 +-
 OvmfPkg/OvmfXen.dsc            | 2 +-
 8 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/OvmfPkg/AmdSev/AmdSevX64.dsc b/OvmfPkg/AmdSev/AmdSevX64.dsc index c005e474dd..3a23e38263 100644
--- a/OvmfPkg/AmdSev/AmdSevX64.dsc
+++ b/OvmfPkg/AmdSev/AmdSevX64.dsc
@@ -169,6 +169,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  
+ CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 !if $(SOURCE_DEBUG_ENABLE) == TRUE
   PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLibDebug.inf
@@ -352,7 +353,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/Bhyve/BhyveX64.dsc b/OvmfPkg/Bhyve/BhyveX64.dsc index d0d2712c56..67f8a77c3a 100644
--- a/OvmfPkg/Bhyve/BhyveX64.dsc
+++ b/OvmfPkg/Bhyve/BhyveX64.dsc
@@ -1,6 +1,6 @@
 #
 #  Copyright (c) 2020, Rebecca Cran <rebecca@bsdio.com> -#  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2006 - 2023, Intel Corporation. All rights 
+reserved.<BR>
 #  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>  #  Copyright (c) 2014, Pluribus Networks, Inc.
 #
@@ -171,6 +171,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  
+ CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
   CustomizedDisplayLib|MdeModulePkg/Library/CustomizedDisplayLib/CustomizedDisplayLib.inf
   FrameBufferBltLib|MdeModulePkg/Library/FrameBufferBltLib/FrameBufferBltLib.inf
diff --git a/OvmfPkg/CloudHv/CloudHvX64.dsc b/OvmfPkg/CloudHv/CloudHvX64.dsc index b9820cc14b..ffc65b0e15 100644
--- a/OvmfPkg/CloudHv/CloudHvX64.dsc
+++ b/OvmfPkg/CloudHv/CloudHvX64.dsc
@@ -190,6 +190,7 @@
   MemEncryptSevLib|OvmfPkg/Library/BaseMemEncryptSevLib/DxeMemEncryptSevLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  
+ CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 !if $(SMM_REQUIRE) == FALSE
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
 !endif
@@ -403,7 +404,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/Microvm/MicrovmX64.dsc b/OvmfPkg/Microvm/MicrovmX64.dsc index 384b0b7afc..aa74a9d5ad 100644
--- a/OvmfPkg/Microvm/MicrovmX64.dsc
+++ b/OvmfPkg/Microvm/MicrovmX64.dsc
@@ -193,6 +193,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  
+ CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 !if $(SOURCE_DEBUG_ENABLE) == TRUE
   PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLibDebug.inf
@@ -402,7 +403,6 @@
   PciLib|MdePkg/Library/BasePciLibPciExpress/BasePciLibPciExpress.inf
   PciPcdProducerLib|OvmfPkg/Fdt/FdtPciPcdProducerLib/FdtPciPcdProducerLib.inf
   PciExpressLib|OvmfPkg/Library/BaseCachingPciExpressLib/BaseCachingPciExpressLib.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/OvmfPkgIa32.dsc b/OvmfPkg/OvmfPkgIa32.dsc index a6db902f54..6352c84759 100644
--- a/OvmfPkg/OvmfPkgIa32.dsc
+++ b/OvmfPkg/OvmfPkgIa32.dsc
@@ -1,7 +1,7 @@
 ## @file
 #  EFI/Framework Open Virtual Machine Firmware (OVMF) platform  # -#  Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2006 - 2023, Intel Corporation. All rights 
+reserved.<BR>
 #  (C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>  #  Copyright (c) Microsoft Corporation.
 #
@@ -193,6 +193,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLibNull.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  
+ CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 !if $(SMM_REQUIRE) == FALSE
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
 !endif
diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc index 076fc0353d..9bec68c733 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -197,6 +197,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLibNull.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  
+ CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 !if $(SMM_REQUIRE) == FALSE
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
 !endif
@@ -413,7 +414,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc index b2f3d14cd9..9e8aaede09 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -210,6 +210,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  
+ CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 !if $(SMM_REQUIRE) == FALSE
   LockBoxLib|OvmfPkg/Library/LockBoxLib/LockBoxBaseLib.inf
@@ -434,7 +435,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
diff --git a/OvmfPkg/OvmfXen.dsc b/OvmfPkg/OvmfXen.dsc index 990225d2dd..806e6e064e 100644
--- a/OvmfPkg/OvmfXen.dsc
+++ b/OvmfPkg/OvmfXen.dsc
@@ -173,6 +173,7 @@
   MemEncryptTdxLib|OvmfPkg/Library/BaseMemEncryptTdxLib/BaseMemEncryptTdxLib.inf
   PeiHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/PeiHardwareInfoLib.inf
   DxeHardwareInfoLib|OvmfPkg/Library/HardwareInfoLib/DxeHardwareInfoLib.inf
+  
+ CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 !if $(SOURCE_DEBUG_ENABLE) == TRUE
   PeCoffExtraActionLib|SourceLevelDebugPkg/Library/PeCoffExtraActionLibDebug/PeCoffExtraActionLibDebug.inf
@@ -338,7 +339,6 @@
   DebugAgentLib|SourceLevelDebugPkg/Library/DebugAgent/DxeDebugAgentLib.inf
 !endif
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
-  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
   MpInitLib|UefiCpuPkg/Library/MpInitLib/DxeMpInitLib.inf
   NestedInterruptTplLib|OvmfPkg/Library/NestedInterruptTplLib/NestedInterruptTplLib.inf
   QemuFwCfgS3Lib|OvmfPkg/Library/QemuFwCfgS3Lib/DxeQemuFwCfgS3LibFwCfg.inf
--
2.31.1.windows.1







^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
       [not found] ` <1751776BEEE9C9E8.27612@groups.io>
@ 2023-04-11  6:51   ` duntan
  0 siblings, 0 replies; 32+ messages in thread
From: duntan @ 2023-04-11  6:51 UTC (permalink / raw)
  To: devel@edk2.groups.io, Tan, Dun
  Cc: Gao, Liming, Ni, Ray, Wang, Jian J, Kinney, Michael D

Hi all, 

Could you please help review this patch?

Thanks,
Dun

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of duntan
Sent: Friday, March 31, 2023 5:34 PM
To: devel@edk2.groups.io
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>; Wang, Jian J <jian.j.wang@intel.com>
Subject: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass DependencyCheck since DxeIpl in MdeModulePkg needs to consume CpuPageTableLib in UefiCpuPkg.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
---
 MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml b/MdeModulePkg/MdeModulePkg.ci.yaml
index f69989087b..d2616f4cdc 100644
--- a/MdeModulePkg/MdeModulePkg.ci.yaml
+++ b/MdeModulePkg/MdeModulePkg.ci.yaml
@@ -2,7 +2,7 @@
 # CI configuration for MdeModulePkg
 #
 # Copyright (c) Microsoft Corporation
-# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2020 - 2023, Intel Corporation. All rights 
+reserved.<BR>
 # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>  # SPDX-License-Identifier: BSD-2-Clause-Patent  ## @@ -51,7 +51,8 @@
             "MdePkg/MdePkg.dec",
             "MdeModulePkg/MdeModulePkg.dec",
             "StandaloneMmPkg/StandaloneMmPkg.dec",
-            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an abstraction
+            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an abstraction
+            "UefiCpuPkg/UefiCpuPkg.dec"
         ],
         # For host based unit tests
         "AcceptableDependencies-HOST_APPLICATION":[
--
2.31.1.windows.1







^ permalink raw reply related	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 3/8] MdeModulePkg: Add CpuPageTableLib required by DxeIpl in DSC
       [not found] ` <1751776458C20361.12651@groups.io>
@ 2023-04-11  6:53   ` duntan
  0 siblings, 0 replies; 32+ messages in thread
From: duntan @ 2023-04-11  6:53 UTC (permalink / raw)
  To: devel@edk2.groups.io; +Cc: Ni, Ray, Wang, Jian J, Gao, Liming

Hi all,
Could you please help review this patch?

Thanks,
Dun

-----Original Message-----
From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of duntan
Sent: Friday, March 31, 2023 5:34 PM
To: devel@edk2.groups.io
Cc: Wang, Jian J <jian.j.wang@intel.com>; Gao, Liming <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>
Subject: [edk2-devel] [Patch V2 3/8] MdeModulePkg: Add CpuPageTableLib required by DxeIpl in DSC

Add CpuPageTableLib instance required by DxeIpl in MdeModulePkg.dsc.

Signed-off-by: Dun Tan <dun.tan@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Ray Ni <ray.ni@intel.com>
---
 MdeModulePkg/MdeModulePkg.dsc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc index 1014598f31..d95acabe83 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -2,7 +2,7 @@
 # EFI/PI Reference Module Package for All Architectures  #  # (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR> -# Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2007 - 2023, Intel Corporation. All rights 
+reserved.<BR>
 # Copyright (c) Microsoft Corporation.
 # Copyright (C) 2023 Advanced Micro Devices, Inc. All rights reserved.<BR>  # @@ -106,6 +106,7 @@
   MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockMemoryLibNull.inf
   VariableFlashInfoLib|MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
   IpmiCommandLib|MdeModulePkg/Library/BaseIpmiCommandLibNull/BaseIpmiCommandLibNull.inf
+  
+ CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
 
 [LibraryClasses.EBC.PEIM]
   IoLib|MdePkg/Library/PeiIoLibCpuIo/PeiIoLibCpuIo.inf
--
2.31.1.windows.1







^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file
  2023-04-11  6:51   ` [edk2-devel] [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file duntan
@ 2023-04-11  8:56     ` Gerd Hoffmann
  0 siblings, 0 replies; 32+ messages in thread
From: Gerd Hoffmann @ 2023-04-11  8:56 UTC (permalink / raw)
  To: Tan, Dun
  Cc: devel@edk2.groups.io, Ard Biesheuvel, Yao, Jiewen,
	Justen, Jordan L, Ni, Ray

On Tue, Apr 11, 2023 at 06:51:25AM +0000, Tan, Dun wrote:
> Hi all, 
> Could you please help review this patch?

Acked-by: Gerd Hoffmann <kraxel@redhat.com>


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-03-31  9:33 ` [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck duntan
@ 2023-04-14  9:03   ` Wang, Jian J
  2023-04-14 15:16     ` [edk2-devel] " Michael D Kinney
  0 siblings, 1 reply; 32+ messages in thread
From: Wang, Jian J @ 2023-04-14  9:03 UTC (permalink / raw)
  To: Tan, Dun, devel@edk2.groups.io; +Cc: Gao, Liming, Ni, Ray

MdeModulePkg has never depended on UefiCpuPkg before. Please double
check if there's any side effect introduced by this mutual dependency.

Acked-by: Jian J Wang <jian.j.wang@intel.com>


> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Friday, March 31, 2023 5:34 PM
> To: devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>;
> Wang, Jian J <jian.j.wang@intel.com>
> Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> DependencyCheck
> 
> Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> CpuPageTableLib in UefiCpuPkg.
> 
> Signed-off-by: Dun Tan <dun.tan@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> ---
>  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> b/MdeModulePkg/MdeModulePkg.ci.yaml
> index f69989087b..d2616f4cdc 100644
> --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> @@ -2,7 +2,7 @@
>  # CI configuration for MdeModulePkg
>  #
>  # Copyright (c) Microsoft Corporation
> -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
>  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
>  # SPDX-License-Identifier: BSD-2-Clause-Patent
>  ##
> @@ -51,7 +51,8 @@
>              "MdePkg/MdePkg.dec",
>              "MdeModulePkg/MdeModulePkg.dec",
>              "StandaloneMmPkg/StandaloneMmPkg.dec",
> -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> abstraction
> +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> abstraction
> +            "UefiCpuPkg/UefiCpuPkg.dec"
>          ],
>          # For host based unit tests
>          "AcceptableDependencies-HOST_APPLICATION":[
> --
> 2.31.1.windows.1


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [Patch V2 3/8] MdeModulePkg: Add CpuPageTableLib required by DxeIpl in DSC
  2023-03-31  9:33 ` [Patch V2 3/8] MdeModulePkg: " duntan
@ 2023-04-14  9:09   ` Wang, Jian J
  2023-04-24 10:11     ` duntan
  0 siblings, 1 reply; 32+ messages in thread
From: Wang, Jian J @ 2023-04-14  9:09 UTC (permalink / raw)
  To: Tan, Dun, devel@edk2.groups.io; +Cc: Gao, Liming, Ni, Ray

This will cause that MdeModulePkg depends on UefiCpuPkg. Please double
check if there's any side effect introduced by breaking unidirectional dependency.

Acked-by: Jian J Wang <jian.j.wang@intel.com>

> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Friday, March 31, 2023 5:34 PM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Gao, Liming
> <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>
> Subject: [Patch V2 3/8] MdeModulePkg: Add CpuPageTableLib required by DxeIpl
> in DSC
> 
> Add CpuPageTableLib instance required by DxeIpl in
> MdeModulePkg.dsc.
> 
> Signed-off-by: Dun Tan <dun.tan@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Ray Ni <ray.ni@intel.com>
> ---
>  MdeModulePkg/MdeModulePkg.dsc | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/MdeModulePkg/MdeModulePkg.dsc
> b/MdeModulePkg/MdeModulePkg.dsc
> index 1014598f31..d95acabe83 100644
> --- a/MdeModulePkg/MdeModulePkg.dsc
> +++ b/MdeModulePkg/MdeModulePkg.dsc
> @@ -2,7 +2,7 @@
>  # EFI/PI Reference Module Package for All Architectures
>  #
>  # (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>
> -# Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2007 - 2023, Intel Corporation. All rights reserved.<BR>
>  # Copyright (c) Microsoft Corporation.
>  # Copyright (C) 2023 Advanced Micro Devices, Inc. All rights reserved.<BR>
>  #
> @@ -106,6 +106,7 @@
> 
> MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockM
> emoryLibNull.inf
> 
> VariableFlashInfoLib|MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVar
> iableFlashInfoLib.inf
> 
> IpmiCommandLib|MdeModulePkg/Library/BaseIpmiCommandLibNull/BaseIpmi
> CommandLibNull.inf
> +  CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.inf
> 
>  [LibraryClasses.EBC.PEIM]
>    IoLib|MdePkg/Library/PeiIoLibCpuIo/PeiIoLibCpuIo.inf
> --
> 2.31.1.windows.1


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-14  9:03   ` Wang, Jian J
@ 2023-04-14 15:16     ` Michael D Kinney
  2023-04-14 16:07       ` Ni, Ray
  0 siblings, 1 reply; 32+ messages in thread
From: Michael D Kinney @ 2023-04-14 15:16 UTC (permalink / raw)
  To: devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming, Ni, Ray, Kinney, Michael D

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com>; devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
> 
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
> 
> Acked-by: Jian J Wang <jian.j.wang@intel.com>
> 
> 
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>;
> > Wang, Jian J <jian.j.wang@intel.com>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
> 
> 
> 
> 
> 


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-14 15:16     ` [edk2-devel] " Michael D Kinney
@ 2023-04-14 16:07       ` Ni, Ray
  2023-04-15 15:50         ` Michael D Kinney
  0 siblings, 1 reply; 32+ messages in thread
From: Ni, Ray @ 2023-04-14 16:07 UTC (permalink / raw)
  To: Kinney, Michael D, devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming, Kinney, Michael D

[-- Attachment #1: Type: text/plain, Size: 3503 bytes --]

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io <devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io <devel@edk2.groups.io> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com>; devel@edk2.groups.io
> Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>;
> > Wang, Jian J <jian.j.wang@intel.com>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn>
> > Cc: Ray Ni <ray.ni@intel.com>
> > Cc: Jian J Wang <jian.j.wang@intel.com>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>


[-- Attachment #2: Type: text/html, Size: 5617 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-14 16:07       ` Ni, Ray
@ 2023-04-15 15:50         ` Michael D Kinney
  2023-04-15 15:57           ` Michael D Kinney
  0 siblings, 1 reply; 32+ messages in thread
From: Michael D Kinney @ 2023-04-15 15:50 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming, Kinney, Michael D

[-- Attachment #1: Type: text/plain, Size: 5366 bytes --]

MdePkg: Include files for industry standard and public specs and lib classes and lib implementations that support those specs

If all the IA32/X64 CPU header files to support the CpuPageTableLib class are in the MdePkg, then we could consider
moving he class to MdePkg and avoid this patch.

The current CpuPageTableLib looks IA32/X64 specific.  Should it follow the naming conventions in the EDK II C Coding Style Spec
updated by Abner?  It does not look like the current CpuPageTableLib APIs would apply to other CPU archs.

MdePkg does not have any modules.  UefiCpuPkg contains CPU specific modules.  UefiCpuPkg can also contain libs
that are required by modules in the UefiCpuPkg or modules in other Si/Platform packages.

Thanks,

Mike

From: Ni, Ray <ray.ni@intel.com>
Sent: Friday, April 14, 2023 9:08 AM
To: Kinney, Michael D <michael.d.kinney@intel.com>; devel@edk2.groups.io; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>;
> > Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
> > Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> > Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>

[-- Attachment #2: Type: text/html, Size: 53389 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-15 15:50         ` Michael D Kinney
@ 2023-04-15 15:57           ` Michael D Kinney
  2023-04-16  5:21             ` Ni, Ray
  0 siblings, 1 reply; 32+ messages in thread
From: Michael D Kinney @ 2023-04-15 15:57 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming, Kinney, Michael D

[-- Attachment #1: Type: text/plain, Size: 6441 bytes --]

Back in 2019, I had proposed some more generic rules for package dependencies.

    https://edk2.groups.io/g/devel/message/52211
    https://github.com/mdkinney/edk2/wiki/EDKII-Packages#edk-ii-package-dependency-rules

The EDK II DEC files do not have enough meta-data to apply these rules.  Would require some extra
Define values or well-known tags in comments.  The current package dependency checker uses a
set of named packages.

Mike


From: Kinney, Michael D <michael.d.kinney@intel.com>
Sent: Saturday, April 15, 2023 8:50 AM
To: Ni, Ray <ray.ni@intel.com>; devel@edk2.groups.io; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

MdePkg: Include files for industry standard and public specs and lib classes and lib implementations that support those specs

If all the IA32/X64 CPU header files to support the CpuPageTableLib class are in the MdePkg, then we could consider
moving he class to MdePkg and avoid this patch.

The current CpuPageTableLib looks IA32/X64 specific.  Should it follow the naming conventions in the EDK II C Coding Style Spec
updated by Abner?  It does not look like the current CpuPageTableLib APIs would apply to other CPU archs.

MdePkg does not have any modules.  UefiCpuPkg contains CPU specific modules.  UefiCpuPkg can also contain libs
that are required by modules in the UefiCpuPkg or modules in other Si/Platform packages.

Thanks,

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Friday, April 14, 2023 9:08 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>;
> > Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
> > Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> > Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>

[-- Attachment #2: Type: text/html, Size: 57140 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-15 15:57           ` Michael D Kinney
@ 2023-04-16  5:21             ` Ni, Ray
  2023-04-18 19:06               ` Michael D Kinney
  2023-04-24 15:59               ` Ard Biesheuvel
  0 siblings, 2 replies; 32+ messages in thread
From: Ni, Ray @ 2023-04-16  5:21 UTC (permalink / raw)
  To: Kinney, Michael D, devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming

[-- Attachment #1: Type: text/plain, Size: 8378 bytes --]

Mike,
MdeModule belongs to the common-package category.
I agree that the common-package should not depend on a specific arch.
MdeModule depending on UefiCpu because DxeIpl needs to prepare an arch specific environment for DXE phase.

So, I am thinking if the arch-specific-env-preparation can be abstracted through an arch-agnostic API.
Then each arch can implement a concrete instance for that API. The API itself can be in MdeModule pkg.

In fact, the API is almost already there: “HandOffToDxeCore”.

So, we could:

  1.  Create a new API HandOffToDxeCore() in a new TBD lib class
  2.  Implement different instances for different arch.
  3.  Default instance does nothing arch specific and can be used by EmulatorPkg platform
     *   Today EmulatorPkg uses X64 version of HandOffToDxeCore and skips page table building by setting PcdDxeIplBuildPageTables to FALSE.
  4.  UefiCpuPkg implements the HandOffToDxe() for IA32 and X64.

So, the dependency is reversed: only UefiCpu depends on MdeModule.

This also removes the arch-specific contents from MdeModulePkg.

One side effect is: every platform needs to include the new TBD lib class.

I agree that moving to MdePkg also works. But we might end up with a bigger and bigger MdePkg by including more and more.

Thanks,
Ray

From: Kinney, Michael D <michael.d.kinney@intel.com>
Sent: Saturday, April 15, 2023 11:57 PM
To: Ni, Ray <ray.ni@intel.com>; devel@edk2.groups.io; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Back in 2019, I had proposed some more generic rules for package dependencies.

    https://edk2.groups.io/g/devel/message/52211
    https://github.com/mdkinney/edk2/wiki/EDKII-Packages#edk-ii-package-dependency-rules

The EDK II DEC files do not have enough meta-data to apply these rules.  Would require some extra
Define values or well-known tags in comments.  The current package dependency checker uses a
set of named packages.

Mike


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 8:50 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

MdePkg: Include files for industry standard and public specs and lib classes and lib implementations that support those specs

If all the IA32/X64 CPU header files to support the CpuPageTableLib class are in the MdePkg, then we could consider
moving he class to MdePkg and avoid this patch.

The current CpuPageTableLib looks IA32/X64 specific.  Should it follow the naming conventions in the EDK II C Coding Style Spec
updated by Abner?  It does not look like the current CpuPageTableLib APIs would apply to other CPU archs.

MdePkg does not have any modules.  UefiCpuPkg contains CPU specific modules.  UefiCpuPkg can also contain libs
that are required by modules in the UefiCpuPkg or modules in other Si/Platform packages.

Thanks,

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Friday, April 14, 2023 9:08 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>;
> > Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
> > Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> > Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>

[-- Attachment #2: Type: text/html, Size: 18511 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-16  5:21             ` Ni, Ray
@ 2023-04-18 19:06               ` Michael D Kinney
  2023-04-19  6:00                 ` Ni, Ray
  2023-04-24 15:59               ` Ard Biesheuvel
  1 sibling, 1 reply; 32+ messages in thread
From: Michael D Kinney @ 2023-04-18 19:06 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming, Kinney, Michael D

[-- Attachment #1: Type: text/plain, Size: 9664 bytes --]

Hi Ray,

There are other discussions to add more arch specific content to MdeModulePkg

https://edk2.groups.io/g/devel/message/101104

The tradeoff here is moving a lib class from UefiCpuPkg to MdePkg vs defining a new lib class/instance and requiring all downstream DSC files to be updated for the new lib instance.

Moving the lib class is simpler and has less impact and we have done this a few times before (e.g. CpuLib)

I agree we need to be careful about ho much content we move into MdePkg.  However, for this specific topic, if we want to maximize the use of the Page Table Library and remove redundant code that manages page tables, moving to MdePkg may be the best option.

Mike


From: Ni, Ray <ray.ni@intel.com>
Sent: Saturday, April 15, 2023 10:21 PM
To: Kinney, Michael D <michael.d.kinney@intel.com>; devel@edk2.groups.io; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
MdeModule belongs to the common-package category.
I agree that the common-package should not depend on a specific arch.
MdeModule depending on UefiCpu because DxeIpl needs to prepare an arch specific environment for DXE phase.

So, I am thinking if the arch-specific-env-preparation can be abstracted through an arch-agnostic API.
Then each arch can implement a concrete instance for that API. The API itself can be in MdeModule pkg.

In fact, the API is almost already there: “HandOffToDxeCore”.

So, we could:

  1.  Create a new API HandOffToDxeCore() in a new TBD lib class
  2.  Implement different instances for different arch.
  3.  Default instance does nothing arch specific and can be used by EmulatorPkg platform

     *   Today EmulatorPkg uses X64 version of HandOffToDxeCore and skips page table building by setting PcdDxeIplBuildPageTables to FALSE.

  1.  UefiCpuPkg implements the HandOffToDxe() for IA32 and X64.

So, the dependency is reversed: only UefiCpu depends on MdeModule.

This also removes the arch-specific contents from MdeModulePkg.

One side effect is: every platform needs to include the new TBD lib class.

I agree that moving to MdePkg also works. But we might end up with a bigger and bigger MdePkg by including more and more.

Thanks,
Ray

From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 11:57 PM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Back in 2019, I had proposed some more generic rules for package dependencies.

    https://edk2.groups.io/g/devel/message/52211
    https://github.com/mdkinney/edk2/wiki/EDKII-Packages#edk-ii-package-dependency-rules

The EDK II DEC files do not have enough meta-data to apply these rules.  Would require some extra
Define values or well-known tags in comments.  The current package dependency checker uses a
set of named packages.

Mike


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 8:50 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

MdePkg: Include files for industry standard and public specs and lib classes and lib implementations that support those specs

If all the IA32/X64 CPU header files to support the CpuPageTableLib class are in the MdePkg, then we could consider
moving he class to MdePkg and avoid this patch.

The current CpuPageTableLib looks IA32/X64 specific.  Should it follow the naming conventions in the EDK II C Coding Style Spec
updated by Abner?  It does not look like the current CpuPageTableLib APIs would apply to other CPU archs.

MdePkg does not have any modules.  UefiCpuPkg contains CPU specific modules.  UefiCpuPkg can also contain libs
that are required by modules in the UefiCpuPkg or modules in other Si/Platform packages.

Thanks,

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Friday, April 14, 2023 9:08 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>;
> > Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
> > Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> > Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>

[-- Attachment #2: Type: text/html, Size: 59853 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-18 19:06               ` Michael D Kinney
@ 2023-04-19  6:00                 ` Ni, Ray
  2023-04-19 15:02                   ` Michael D Kinney
  0 siblings, 1 reply; 32+ messages in thread
From: Ni, Ray @ 2023-04-19  6:00 UTC (permalink / raw)
  To: Kinney, Michael D, devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming, Ard Biesheuvel

[-- Attachment #1: Type: text/plain, Size: 11123 bytes --]

Mike,
Moving the PageTableLib to MdePkg today also requires all downstream DSC files to be updated to use the instance in MdePkg.
Because MpInitLib today depends on PageTableLib already due to the change to put AP in 64bit before handling to OS.

But you remind me to search for other modules manipulating PageTableLib in MdeModulePkg, or any other packages that should not depend on UefiCpuPkg.
I found:

  1.  MdeModulePkg/Universal/CapsulePei
  2.  OvmfPkg/…: I don’t care which module because OvmfPkg can depend on UefiCpuPkg.
  3.  UefiPayloadPkg/…: I don’t care which module because UefiPayloadPkg can depend on UefiCpuPkg.

Because CapsulePei doesn’t need to use PageTableLib in 64bit PEI mode, I am fine with leaving CapsulePei duplicating the page table manipulating code.


+ Ard to see some feedback from ARM side.

Thanks,
Ray


From: Kinney, Michael D <michael.d.kinney@intel.com>
Sent: Wednesday, April 19, 2023 3:07 AM
To: Ni, Ray <ray.ni@intel.com>; devel@edk2.groups.io; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Hi Ray,

There are other discussions to add more arch specific content to MdeModulePkg

https://edk2.groups.io/g/devel/message/101104

The tradeoff here is moving a lib class from UefiCpuPkg to MdePkg vs defining a new lib class/instance and requiring all downstream DSC files to be updated for the new lib instance.

Moving the lib class is simpler and has less impact and we have done this a few times before (e.g. CpuLib)

I agree we need to be careful about ho much content we move into MdePkg.  However, for this specific topic, if we want to maximize the use of the Page Table Library and remove redundant code that manages page tables, moving to MdePkg may be the best option.

Mike


From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Saturday, April 15, 2023 10:21 PM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
MdeModule belongs to the common-package category.
I agree that the common-package should not depend on a specific arch.
MdeModule depending on UefiCpu because DxeIpl needs to prepare an arch specific environment for DXE phase.

So, I am thinking if the arch-specific-env-preparation can be abstracted through an arch-agnostic API.
Then each arch can implement a concrete instance for that API. The API itself can be in MdeModule pkg.

In fact, the API is almost already there: “HandOffToDxeCore”.

So, we could:

  1.  Create a new API HandOffToDxeCore() in a new TBD lib class
  2.  Implement different instances for different arch.
  3.  Default instance does nothing arch specific and can be used by EmulatorPkg platform

     *   Today EmulatorPkg uses X64 version of HandOffToDxeCore and skips page table building by setting PcdDxeIplBuildPageTables to FALSE.

  1.  UefiCpuPkg implements the HandOffToDxe() for IA32 and X64.

So, the dependency is reversed: only UefiCpu depends on MdeModule.

This also removes the arch-specific contents from MdeModulePkg.

One side effect is: every platform needs to include the new TBD lib class.

I agree that moving to MdePkg also works. But we might end up with a bigger and bigger MdePkg by including more and more.

Thanks,
Ray

From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 11:57 PM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Back in 2019, I had proposed some more generic rules for package dependencies.

    https://edk2.groups.io/g/devel/message/52211
    https://github.com/mdkinney/edk2/wiki/EDKII-Packages#edk-ii-package-dependency-rules

The EDK II DEC files do not have enough meta-data to apply these rules.  Would require some extra
Define values or well-known tags in comments.  The current package dependency checker uses a
set of named packages.

Mike


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 8:50 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

MdePkg: Include files for industry standard and public specs and lib classes and lib implementations that support those specs

If all the IA32/X64 CPU header files to support the CpuPageTableLib class are in the MdePkg, then we could consider
moving he class to MdePkg and avoid this patch.

The current CpuPageTableLib looks IA32/X64 specific.  Should it follow the naming conventions in the EDK II C Coding Style Spec
updated by Abner?  It does not look like the current CpuPageTableLib APIs would apply to other CPU archs.

MdePkg does not have any modules.  UefiCpuPkg contains CPU specific modules.  UefiCpuPkg can also contain libs
that are required by modules in the UefiCpuPkg or modules in other Si/Platform packages.

Thanks,

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Friday, April 14, 2023 9:08 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>;
> > Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
> > Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> > Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>

[-- Attachment #2: Type: text/html, Size: 25807 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-19  6:00                 ` Ni, Ray
@ 2023-04-19 15:02                   ` Michael D Kinney
  2023-04-21  8:10                     ` Ni, Ray
  0 siblings, 1 reply; 32+ messages in thread
From: Michael D Kinney @ 2023-04-19 15:02 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming, Ard Biesheuvel, Kinney, Michael D

[-- Attachment #1: Type: text/plain, Size: 11852 bytes --]

Ray,

I am suggesting that only the lib class be defined in MdePkg.  Lib instance can remain in UefiCpuPkg.

Mike

From: Ni, Ray <ray.ni@intel.com>
Sent: Tuesday, April 18, 2023 11:01 PM
To: Kinney, Michael D <michael.d.kinney@intel.com>; devel@edk2.groups.io; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ard Biesheuvel <ardb@kernel.org>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
Moving the PageTableLib to MdePkg today also requires all downstream DSC files to be updated to use the instance in MdePkg.
Because MpInitLib today depends on PageTableLib already due to the change to put AP in 64bit before handling to OS.

But you remind me to search for other modules manipulating PageTableLib in MdeModulePkg, or any other packages that should not depend on UefiCpuPkg.
I found:

  1.  MdeModulePkg/Universal/CapsulePei
  2.  OvmfPkg/…: I don’t care which module because OvmfPkg can depend on UefiCpuPkg.
  3.  UefiPayloadPkg/…: I don’t care which module because UefiPayloadPkg can depend on UefiCpuPkg.

Because CapsulePei doesn’t need to use PageTableLib in 64bit PEI mode, I am fine with leaving CapsulePei duplicating the page table manipulating code.


+ Ard to see some feedback from ARM side.

Thanks,
Ray


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Wednesday, April 19, 2023 3:07 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Hi Ray,

There are other discussions to add more arch specific content to MdeModulePkg

https://edk2.groups.io/g/devel/message/101104

The tradeoff here is moving a lib class from UefiCpuPkg to MdePkg vs defining a new lib class/instance and requiring all downstream DSC files to be updated for the new lib instance.

Moving the lib class is simpler and has less impact and we have done this a few times before (e.g. CpuLib)

I agree we need to be careful about ho much content we move into MdePkg.  However, for this specific topic, if we want to maximize the use of the Page Table Library and remove redundant code that manages page tables, moving to MdePkg may be the best option.

Mike


From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Saturday, April 15, 2023 10:21 PM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
MdeModule belongs to the common-package category.
I agree that the common-package should not depend on a specific arch.
MdeModule depending on UefiCpu because DxeIpl needs to prepare an arch specific environment for DXE phase.

So, I am thinking if the arch-specific-env-preparation can be abstracted through an arch-agnostic API.
Then each arch can implement a concrete instance for that API. The API itself can be in MdeModule pkg.

In fact, the API is almost already there: “HandOffToDxeCore”.

So, we could:

  1.  Create a new API HandOffToDxeCore() in a new TBD lib class
  2.  Implement different instances for different arch.
  3.  Default instance does nothing arch specific and can be used by EmulatorPkg platform

     *   Today EmulatorPkg uses X64 version of HandOffToDxeCore and skips page table building by setting PcdDxeIplBuildPageTables to FALSE.

  1.  UefiCpuPkg implements the HandOffToDxe() for IA32 and X64.

So, the dependency is reversed: only UefiCpu depends on MdeModule.

This also removes the arch-specific contents from MdeModulePkg.

One side effect is: every platform needs to include the new TBD lib class.

I agree that moving to MdePkg also works. But we might end up with a bigger and bigger MdePkg by including more and more.

Thanks,
Ray

From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 11:57 PM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Back in 2019, I had proposed some more generic rules for package dependencies.

    https://edk2.groups.io/g/devel/message/52211
    https://github.com/mdkinney/edk2/wiki/EDKII-Packages#edk-ii-package-dependency-rules

The EDK II DEC files do not have enough meta-data to apply these rules.  Would require some extra
Define values or well-known tags in comments.  The current package dependency checker uses a
set of named packages.

Mike


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 8:50 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

MdePkg: Include files for industry standard and public specs and lib classes and lib implementations that support those specs

If all the IA32/X64 CPU header files to support the CpuPageTableLib class are in the MdePkg, then we could consider
moving he class to MdePkg and avoid this patch.

The current CpuPageTableLib looks IA32/X64 specific.  Should it follow the naming conventions in the EDK II C Coding Style Spec
updated by Abner?  It does not look like the current CpuPageTableLib APIs would apply to other CPU archs.

MdePkg does not have any modules.  UefiCpuPkg contains CPU specific modules.  UefiCpuPkg can also contain libs
that are required by modules in the UefiCpuPkg or modules in other Si/Platform packages.

Thanks,

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Friday, April 14, 2023 9:08 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>;
> > Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
> > Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> > Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>

[-- Attachment #2: Type: text/html, Size: 64955 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-19 15:02                   ` Michael D Kinney
@ 2023-04-21  8:10                     ` Ni, Ray
  2023-04-21 15:42                       ` Michael D Kinney
  0 siblings, 1 reply; 32+ messages in thread
From: Ni, Ray @ 2023-04-21  8:10 UTC (permalink / raw)
  To: Kinney, Michael D, devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming, Ard Biesheuvel

[-- Attachment #1: Type: text/plain, Size: 12548 bytes --]

Mike, shall MdePkg contain the NULL instance?

From: Kinney, Michael D <michael.d.kinney@intel.com>
Sent: Wednesday, April 19, 2023 11:03 PM
To: Ni, Ray <ray.ni@intel.com>; devel@edk2.groups.io; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ard Biesheuvel <ardb@kernel.org>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Ray,

I am suggesting that only the lib class be defined in MdePkg.  Lib instance can remain in UefiCpuPkg.

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Tuesday, April 18, 2023 11:01 PM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ard Biesheuvel <ardb@kernel.org<mailto:ardb@kernel.org>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
Moving the PageTableLib to MdePkg today also requires all downstream DSC files to be updated to use the instance in MdePkg.
Because MpInitLib today depends on PageTableLib already due to the change to put AP in 64bit before handling to OS.

But you remind me to search for other modules manipulating PageTableLib in MdeModulePkg, or any other packages that should not depend on UefiCpuPkg.
I found:

  1.  MdeModulePkg/Universal/CapsulePei
  2.  OvmfPkg/…: I don’t care which module because OvmfPkg can depend on UefiCpuPkg.
  3.  UefiPayloadPkg/…: I don’t care which module because UefiPayloadPkg can depend on UefiCpuPkg.

Because CapsulePei doesn’t need to use PageTableLib in 64bit PEI mode, I am fine with leaving CapsulePei duplicating the page table manipulating code.


+ Ard to see some feedback from ARM side.

Thanks,
Ray


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Wednesday, April 19, 2023 3:07 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Hi Ray,

There are other discussions to add more arch specific content to MdeModulePkg

https://edk2.groups.io/g/devel/message/101104

The tradeoff here is moving a lib class from UefiCpuPkg to MdePkg vs defining a new lib class/instance and requiring all downstream DSC files to be updated for the new lib instance.

Moving the lib class is simpler and has less impact and we have done this a few times before (e.g. CpuLib)

I agree we need to be careful about ho much content we move into MdePkg.  However, for this specific topic, if we want to maximize the use of the Page Table Library and remove redundant code that manages page tables, moving to MdePkg may be the best option.

Mike


From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Saturday, April 15, 2023 10:21 PM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
MdeModule belongs to the common-package category.
I agree that the common-package should not depend on a specific arch.
MdeModule depending on UefiCpu because DxeIpl needs to prepare an arch specific environment for DXE phase.

So, I am thinking if the arch-specific-env-preparation can be abstracted through an arch-agnostic API.
Then each arch can implement a concrete instance for that API. The API itself can be in MdeModule pkg.

In fact, the API is almost already there: “HandOffToDxeCore”.

So, we could:

  1.  Create a new API HandOffToDxeCore() in a new TBD lib class
  2.  Implement different instances for different arch.
  3.  Default instance does nothing arch specific and can be used by EmulatorPkg platform

     *   Today EmulatorPkg uses X64 version of HandOffToDxeCore and skips page table building by setting PcdDxeIplBuildPageTables to FALSE.

  1.  UefiCpuPkg implements the HandOffToDxe() for IA32 and X64.

So, the dependency is reversed: only UefiCpu depends on MdeModule.

This also removes the arch-specific contents from MdeModulePkg.

One side effect is: every platform needs to include the new TBD lib class.

I agree that moving to MdePkg also works. But we might end up with a bigger and bigger MdePkg by including more and more.

Thanks,
Ray

From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 11:57 PM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Back in 2019, I had proposed some more generic rules for package dependencies.

    https://edk2.groups.io/g/devel/message/52211
    https://github.com/mdkinney/edk2/wiki/EDKII-Packages#edk-ii-package-dependency-rules

The EDK II DEC files do not have enough meta-data to apply these rules.  Would require some extra
Define values or well-known tags in comments.  The current package dependency checker uses a
set of named packages.

Mike


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 8:50 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

MdePkg: Include files for industry standard and public specs and lib classes and lib implementations that support those specs

If all the IA32/X64 CPU header files to support the CpuPageTableLib class are in the MdePkg, then we could consider
moving he class to MdePkg and avoid this patch.

The current CpuPageTableLib looks IA32/X64 specific.  Should it follow the naming conventions in the EDK II C Coding Style Spec
updated by Abner?  It does not look like the current CpuPageTableLib APIs would apply to other CPU archs.

MdePkg does not have any modules.  UefiCpuPkg contains CPU specific modules.  UefiCpuPkg can also contain libs
that are required by modules in the UefiCpuPkg or modules in other Si/Platform packages.

Thanks,

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Friday, April 14, 2023 9:08 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>;
> > Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
> > Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> > Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>

[-- Attachment #2: Type: text/html, Size: 28540 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-21  8:10                     ` Ni, Ray
@ 2023-04-21 15:42                       ` Michael D Kinney
  2023-04-24 10:27                         ` duntan
  0 siblings, 1 reply; 32+ messages in thread
From: Michael D Kinney @ 2023-04-21 15:42 UTC (permalink / raw)
  To: Ni, Ray, devel@edk2.groups.io, Wang, Jian J, Tan, Dun
  Cc: Gao, Liming, Ard Biesheuvel, Kinney, Michael D

[-- Attachment #1: Type: text/plain, Size: 13380 bytes --]

Not required.

Is there any use case of a null instances that would actually be needed (e.g. EmulatorPkg)?
If there is a real use case, then a null instance in the MdePkg would be useful.

Mike

From: Ni, Ray <ray.ni@intel.com>
Sent: Friday, April 21, 2023 1:10 AM
To: Kinney, Michael D <michael.d.kinney@intel.com>; devel@edk2.groups.io; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ard Biesheuvel <ardb@kernel.org>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike, shall MdePkg contain the NULL instance?

From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Wednesday, April 19, 2023 11:03 PM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ard Biesheuvel <ardb@kernel.org<mailto:ardb@kernel.org>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Ray,

I am suggesting that only the lib class be defined in MdePkg.  Lib instance can remain in UefiCpuPkg.

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Tuesday, April 18, 2023 11:01 PM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ard Biesheuvel <ardb@kernel.org<mailto:ardb@kernel.org>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
Moving the PageTableLib to MdePkg today also requires all downstream DSC files to be updated to use the instance in MdePkg.
Because MpInitLib today depends on PageTableLib already due to the change to put AP in 64bit before handling to OS.

But you remind me to search for other modules manipulating PageTableLib in MdeModulePkg, or any other packages that should not depend on UefiCpuPkg.
I found:

  1.  MdeModulePkg/Universal/CapsulePei
  2.  OvmfPkg/…: I don’t care which module because OvmfPkg can depend on UefiCpuPkg.
  3.  UefiPayloadPkg/…: I don’t care which module because UefiPayloadPkg can depend on UefiCpuPkg.

Because CapsulePei doesn’t need to use PageTableLib in 64bit PEI mode, I am fine with leaving CapsulePei duplicating the page table manipulating code.


+ Ard to see some feedback from ARM side.

Thanks,
Ray


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Wednesday, April 19, 2023 3:07 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Hi Ray,

There are other discussions to add more arch specific content to MdeModulePkg

https://edk2.groups.io/g/devel/message/101104

The tradeoff here is moving a lib class from UefiCpuPkg to MdePkg vs defining a new lib class/instance and requiring all downstream DSC files to be updated for the new lib instance.

Moving the lib class is simpler and has less impact and we have done this a few times before (e.g. CpuLib)

I agree we need to be careful about ho much content we move into MdePkg.  However, for this specific topic, if we want to maximize the use of the Page Table Library and remove redundant code that manages page tables, moving to MdePkg may be the best option.

Mike


From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Saturday, April 15, 2023 10:21 PM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
MdeModule belongs to the common-package category.
I agree that the common-package should not depend on a specific arch.
MdeModule depending on UefiCpu because DxeIpl needs to prepare an arch specific environment for DXE phase.

So, I am thinking if the arch-specific-env-preparation can be abstracted through an arch-agnostic API.
Then each arch can implement a concrete instance for that API. The API itself can be in MdeModule pkg.

In fact, the API is almost already there: “HandOffToDxeCore”.

So, we could:

  1.  Create a new API HandOffToDxeCore() in a new TBD lib class
  2.  Implement different instances for different arch.
  3.  Default instance does nothing arch specific and can be used by EmulatorPkg platform

     *   Today EmulatorPkg uses X64 version of HandOffToDxeCore and skips page table building by setting PcdDxeIplBuildPageTables to FALSE.

  1.  UefiCpuPkg implements the HandOffToDxe() for IA32 and X64.

So, the dependency is reversed: only UefiCpu depends on MdeModule.

This also removes the arch-specific contents from MdeModulePkg.

One side effect is: every platform needs to include the new TBD lib class.

I agree that moving to MdePkg also works. But we might end up with a bigger and bigger MdePkg by including more and more.

Thanks,
Ray

From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 11:57 PM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Back in 2019, I had proposed some more generic rules for package dependencies.

    https://edk2.groups.io/g/devel/message/52211
    https://github.com/mdkinney/edk2/wiki/EDKII-Packages#edk-ii-package-dependency-rules

The EDK II DEC files do not have enough meta-data to apply these rules.  Would require some extra
Define values or well-known tags in comments.  The current package dependency checker uses a
set of named packages.

Mike


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 8:50 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

MdePkg: Include files for industry standard and public specs and lib classes and lib implementations that support those specs

If all the IA32/X64 CPU header files to support the CpuPageTableLib class are in the MdePkg, then we could consider
moving he class to MdePkg and avoid this patch.

The current CpuPageTableLib looks IA32/X64 specific.  Should it follow the naming conventions in the EDK II C Coding Style Spec
updated by Abner?  It does not look like the current CpuPageTableLib APIs would apply to other CPU archs.

MdePkg does not have any modules.  UefiCpuPkg contains CPU specific modules.  UefiCpuPkg can also contain libs
that are required by modules in the UefiCpuPkg or modules in other Si/Platform packages.

Thanks,

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Friday, April 14, 2023 9:08 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>;
> > Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
> > Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> > Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>

[-- Attachment #2: Type: text/html, Size: 68213 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [Patch V2 3/8] MdeModulePkg: Add CpuPageTableLib required by DxeIpl in DSC
  2023-04-14  9:09   ` Wang, Jian J
@ 2023-04-24 10:11     ` duntan
  0 siblings, 0 replies; 32+ messages in thread
From: duntan @ 2023-04-24 10:11 UTC (permalink / raw)
  To: Wang, Jian J, devel@edk2.groups.io; +Cc: Gao, Liming, Ni, Ray

Thanks for the comments. In the latest V3 patch set, I have moved CpuPageTableLib definition from UefiCpuPkg to MdePkg. So that MdeModulePkg won't depend on UefiCpuPkg.

Thanks,
Dun

-----Original Message-----
From: Wang, Jian J <jian.j.wang@intel.com> 
Sent: Friday, April 14, 2023 5:10 PM
To: Tan, Dun <dun.tan@intel.com>; devel@edk2.groups.io
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>
Subject: RE: [Patch V2 3/8] MdeModulePkg: Add CpuPageTableLib required by DxeIpl in DSC

This will cause that MdeModulePkg depends on UefiCpuPkg. Please double check if there's any side effect introduced by breaking unidirectional dependency.

Acked-by: Jian J Wang <jian.j.wang@intel.com>

> -----Original Message-----
> From: Tan, Dun <dun.tan@intel.com>
> Sent: Friday, March 31, 2023 5:34 PM
> To: devel@edk2.groups.io
> Cc: Wang, Jian J <jian.j.wang@intel.com>; Gao, Liming 
> <gaoliming@byosoft.com.cn>; Ni, Ray <ray.ni@intel.com>
> Subject: [Patch V2 3/8] MdeModulePkg: Add CpuPageTableLib required by 
> DxeIpl in DSC
> 
> Add CpuPageTableLib instance required by DxeIpl in MdeModulePkg.dsc.
> 
> Signed-off-by: Dun Tan <dun.tan@intel.com>
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Ray Ni <ray.ni@intel.com>
> ---
>  MdeModulePkg/MdeModulePkg.dsc | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/MdeModulePkg/MdeModulePkg.dsc 
> b/MdeModulePkg/MdeModulePkg.dsc index 1014598f31..d95acabe83 100644
> --- a/MdeModulePkg/MdeModulePkg.dsc
> +++ b/MdeModulePkg/MdeModulePkg.dsc
> @@ -2,7 +2,7 @@
>  # EFI/PI Reference Module Package for All Architectures  #  # (C) 
> Copyright 2014 Hewlett-Packard Development Company, L.P.<BR> -# 
> Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
> +# Copyright (c) 2007 - 2023, Intel Corporation. All rights 
> +reserved.<BR>
>  # Copyright (c) Microsoft Corporation.
>  # Copyright (C) 2023 Advanced Micro Devices, Inc. All rights 
> reserved.<BR>  # @@ -106,6 +106,7 @@
> 
> MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockM
> emoryLibNull.inf
> 
> VariableFlashInfoLib|MdeModulePkg/Library/BaseVariableFlashInfoLib/Bas
> VariableFlashInfoLib|eVar
> iableFlashInfoLib.inf
> 
> IpmiCommandLib|MdeModulePkg/Library/BaseIpmiCommandLibNull/BaseIpmi
> CommandLibNull.inf
> +  
> + CpuPageTableLib|UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableLib.i
> + nf
> 
>  [LibraryClasses.EBC.PEIM]
>    IoLib|MdePkg/Library/PeiIoLibCpuIo/PeiIoLibCpuIo.inf
> --
> 2.31.1.windows.1


^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-21 15:42                       ` Michael D Kinney
@ 2023-04-24 10:27                         ` duntan
  0 siblings, 0 replies; 32+ messages in thread
From: duntan @ 2023-04-24 10:27 UTC (permalink / raw)
  To: Kinney, Michael D, Ni, Ray, devel@edk2.groups.io, Wang, Jian J
  Cc: Gao, Liming, Ard Biesheuvel

[-- Attachment #1: Type: text/plain, Size: 14324 bytes --]

Hi Mike,

Thanks for the comments. This patch has been dropped in the latest V4 patch set. I have added a new patch ‘[Patch V4 1/8] MdePkg: Move CpuPageTableLib definition to MdePkg’ to move CpuPageTableLib definition from UefiCpuPkg to MdePkg. Could you please help review?

Thanks,
Dun

From: Kinney, Michael D <michael.d.kinney@intel.com>
Sent: Friday, April 21, 2023 11:43 PM
To: Ni, Ray <ray.ni@intel.com>; devel@edk2.groups.io; Wang, Jian J <jian.j.wang@intel.com>; Tan, Dun <dun.tan@intel.com>
Cc: Gao, Liming <gaoliming@byosoft.com.cn>; Ard Biesheuvel <ardb@kernel.org>; Kinney, Michael D <michael.d.kinney@intel.com>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Not required.

Is there any use case of a null instances that would actually be needed (e.g. EmulatorPkg)?
If there is a real use case, then a null instance in the MdePkg would be useful.

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Friday, April 21, 2023 1:10 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ard Biesheuvel <ardb@kernel.org<mailto:ardb@kernel.org>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike, shall MdePkg contain the NULL instance?

From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Wednesday, April 19, 2023 11:03 PM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ard Biesheuvel <ardb@kernel.org<mailto:ardb@kernel.org>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Ray,

I am suggesting that only the lib class be defined in MdePkg.  Lib instance can remain in UefiCpuPkg.

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Tuesday, April 18, 2023 11:01 PM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ard Biesheuvel <ardb@kernel.org<mailto:ardb@kernel.org>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
Moving the PageTableLib to MdePkg today also requires all downstream DSC files to be updated to use the instance in MdePkg.
Because MpInitLib today depends on PageTableLib already due to the change to put AP in 64bit before handling to OS.

But you remind me to search for other modules manipulating PageTableLib in MdeModulePkg, or any other packages that should not depend on UefiCpuPkg.
I found:

  1.  MdeModulePkg/Universal/CapsulePei
  2.  OvmfPkg/…: I don’t care which module because OvmfPkg can depend on UefiCpuPkg.
  3.  UefiPayloadPkg/…: I don’t care which module because UefiPayloadPkg can depend on UefiCpuPkg.

Because CapsulePei doesn’t need to use PageTableLib in 64bit PEI mode, I am fine with leaving CapsulePei duplicating the page table manipulating code.


+ Ard to see some feedback from ARM side.

Thanks,
Ray


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Wednesday, April 19, 2023 3:07 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Hi Ray,

There are other discussions to add more arch specific content to MdeModulePkg

https://edk2.groups.io/g/devel/message/101104

The tradeoff here is moving a lib class from UefiCpuPkg to MdePkg vs defining a new lib class/instance and requiring all downstream DSC files to be updated for the new lib instance.

Moving the lib class is simpler and has less impact and we have done this a few times before (e.g. CpuLib)

I agree we need to be careful about ho much content we move into MdePkg.  However, for this specific topic, if we want to maximize the use of the Page Table Library and remove redundant code that manages page tables, moving to MdePkg may be the best option.

Mike


From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Saturday, April 15, 2023 10:21 PM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
MdeModule belongs to the common-package category.
I agree that the common-package should not depend on a specific arch.
MdeModule depending on UefiCpu because DxeIpl needs to prepare an arch specific environment for DXE phase.

So, I am thinking if the arch-specific-env-preparation can be abstracted through an arch-agnostic API.
Then each arch can implement a concrete instance for that API. The API itself can be in MdeModule pkg.

In fact, the API is almost already there: “HandOffToDxeCore”.

So, we could:

  1.  Create a new API HandOffToDxeCore() in a new TBD lib class
  2.  Implement different instances for different arch.
  3.  Default instance does nothing arch specific and can be used by EmulatorPkg platform

     *   Today EmulatorPkg uses X64 version of HandOffToDxeCore and skips page table building by setting PcdDxeIplBuildPageTables to FALSE.

  1.  UefiCpuPkg implements the HandOffToDxe() for IA32 and X64.

So, the dependency is reversed: only UefiCpu depends on MdeModule.

This also removes the arch-specific contents from MdeModulePkg.

One side effect is: every platform needs to include the new TBD lib class.

I agree that moving to MdePkg also works. But we might end up with a bigger and bigger MdePkg by including more and more.

Thanks,
Ray

From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 11:57 PM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Back in 2019, I had proposed some more generic rules for package dependencies.

    https://edk2.groups.io/g/devel/message/52211
    https://github.com/mdkinney/edk2/wiki/EDKII-Packages#edk-ii-package-dependency-rules

The EDK II DEC files do not have enough meta-data to apply these rules.  Would require some extra
Define values or well-known tags in comments.  The current package dependency checker uses a
set of named packages.

Mike


From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Saturday, April 15, 2023 8:50 AM
To: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

MdePkg: Include files for industry standard and public specs and lib classes and lib implementations that support those specs

If all the IA32/X64 CPU header files to support the CpuPageTableLib class are in the MdePkg, then we could consider
moving he class to MdePkg and avoid this patch.

The current CpuPageTableLib looks IA32/X64 specific.  Should it follow the naming conventions in the EDK II C Coding Style Spec
updated by Abner?  It does not look like the current CpuPageTableLib APIs would apply to other CPU archs.

MdePkg does not have any modules.  UefiCpuPkg contains CPU specific modules.  UefiCpuPkg can also contain libs
that are required by modules in the UefiCpuPkg or modules in other Si/Platform packages.

Thanks,

Mike

From: Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
Sent: Friday, April 14, 2023 9:08 AM
To: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

Mike,
What's the rule regarding content in mdepkg and cpupkg?

thanks,
ray
________________________________
From: Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Sent: Friday, April 14, 2023 11:16:45 PM
To: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>>; Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>; Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>; Kinney, Michael D <michael.d.kinney@intel.com<mailto:michael.d.kinney@intel.com>>
Subject: RE: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck

If components outside the UefiCpuPkg need access to the CpuPageTableLib, should we
consider moving CpuPageTableLib to MdePkg or MdeModulePkg?  There are many different
boot phases that need to crate/manage page tables, so we need to find the right
common location.  Perhaps the only part that needs to be moved is the lib class?

Mike

> -----Original Message-----
> From: devel@edk2.groups.io<mailto:devel@edk2.groups.io> <devel@edk2.groups.io<mailto:devel@edk2.groups.io>> On Behalf Of Wang, Jian J
> Sent: Friday, April 14, 2023 2:03 AM
> To: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>; devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> Subject: Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
>
> MdeModulePkg has never depended on UefiCpuPkg before. Please double
> check if there's any side effect introduced by this mutual dependency.
>
> Acked-by: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
>
>
> > -----Original Message-----
> > From: Tan, Dun <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Sent: Friday, March 31, 2023 5:34 PM
> > To: devel@edk2.groups.io<mailto:devel@edk2.groups.io>
> > Cc: Gao, Liming <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>; Ni, Ray <ray.ni@intel.com<mailto:ray.ni@intel.com>>;
> > Wang, Jian J <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > Subject: [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass
> > DependencyCheck
> >
> > Add UefiCpuPkg/UefiCpuPkg.dec in MdeModulePkg.ci.yaml to pass
> > DependencyCheck since DxeIpl in MdeModulePkg needs to consume
> > CpuPageTableLib in UefiCpuPkg.
> >
> > Signed-off-by: Dun Tan <dun.tan@intel.com<mailto:dun.tan@intel.com>>
> > Cc: Liming Gao <gaoliming@byosoft.com.cn<mailto:gaoliming@byosoft.com.cn>>
> > Cc: Ray Ni <ray.ni@intel.com<mailto:ray.ni@intel.com>>
> > Cc: Jian J Wang <jian.j.wang@intel.com<mailto:jian.j.wang@intel.com>>
> > ---
> >  MdeModulePkg/MdeModulePkg.ci.yaml | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/MdeModulePkg.ci.yaml
> > b/MdeModulePkg/MdeModulePkg.ci.yaml
> > index f69989087b..d2616f4cdc 100644
> > --- a/MdeModulePkg/MdeModulePkg.ci.yaml
> > +++ b/MdeModulePkg/MdeModulePkg.ci.yaml
> > @@ -2,7 +2,7 @@
> >  # CI configuration for MdeModulePkg
> >  #
> >  # Copyright (c) Microsoft Corporation
> > -# Copyright (c) 2020, Intel Corporation. All rights reserved.<BR>
> > +# Copyright (c) 2020 - 2023, Intel Corporation. All rights reserved.<BR>
> >  # (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
> >  # SPDX-License-Identifier: BSD-2-Clause-Patent
> >  ##
> > @@ -51,7 +51,8 @@
> >              "MdePkg/MdePkg.dec",
> >              "MdeModulePkg/MdeModulePkg.dec",
> >              "StandaloneMmPkg/StandaloneMmPkg.dec",
> > -            "ArmPkg/ArmPkg.dec"  # this should be fixed by promoting an
> > abstraction
> > +            "ArmPkg/ArmPkg.dec",  # this should be fixed by promoting an
> > abstraction
> > +            "UefiCpuPkg/UefiCpuPkg.dec"
> >          ],
> >          # For host based unit tests
> >          "AcceptableDependencies-HOST_APPLICATION":[
> > --
> > 2.31.1.windows.1
>
>
>
> 
>

[-- Attachment #2: Type: text/html, Size: 31710 bytes --]

^ permalink raw reply	[flat|nested] 32+ messages in thread

* Re: [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck
  2023-04-16  5:21             ` Ni, Ray
  2023-04-18 19:06               ` Michael D Kinney
@ 2023-04-24 15:59               ` Ard Biesheuvel
  1 sibling, 0 replies; 32+ messages in thread
From: Ard Biesheuvel @ 2023-04-24 15:59 UTC (permalink / raw)
  To: devel, ray.ni

On Sun, 16 Apr 2023 at 07:21, Ni, Ray <ray.ni@intel.com> wrote:
>
> Mike,
>
> MdeModule belongs to the common-package category.
>
> I agree that the common-package should not depend on a specific arch.
>
> MdeModule depending on UefiCpu because DxeIpl needs to prepare an arch specific environment for DXE phase.
>
>
>
> So, I am thinking if the arch-specific-env-preparation can be abstracted through an arch-agnostic API.
>
> Then each arch can implement a concrete instance for that API. The API itself can be in MdeModule pkg.
>
>
>
> In fact, the API is almost already there: “HandOffToDxeCore”.
>
>
>
> So, we could:
>
> Create a new API HandOffToDxeCore() in a new TBD lib class
> Implement different instances for different arch.
> Default instance does nothing arch specific and can be used by EmulatorPkg platform
>
> Today EmulatorPkg uses X64 version of HandOffToDxeCore and skips page table building by setting PcdDxeIplBuildPageTables to FALSE.
>
> UefiCpuPkg implements the HandOffToDxe() for IA32 and X64.
>
>
>
> So, the dependency is reversed: only UefiCpu depends on MdeModule.
>
>
>
> This also removes the arch-specific contents from MdeModulePkg.
>
>
>
> One side effect is: every platform needs to include the new TBD lib class.
>
>
>
> I agree that moving to MdePkg also works. But we might end up with a bigger and bigger MdePkg by including more and more.
>
>
>

I would prefer this approach. CpuPageTableLib is very x86-specific and
does not comply with the requirements for MdePkg.

Loading DXE core and mapping it with restricted permissions (to avoid
W+X memory) will require some kind of abstraction here in any case, so
it would be better to let the x86 specific version of that live in
UefiCpuPkg, and keep this out of MdePkg entirely.

^ permalink raw reply	[flat|nested] 32+ messages in thread

end of thread, other threads:[~2023-04-24 15:59 UTC | newest]

Thread overview: 32+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-03-31  9:33 [Patch V2 0/8] Create page table by CpuPageTableLib in DxeIpl duntan
2023-03-31  9:33 ` [Patch V2 1/8] EmulatorPkg: Add CpuPageTableLib required by DxeIpl in DSC duntan
2023-03-31 10:21   ` Ni, Ray
2023-03-31  9:33 ` [Patch V2 2/8] IntelFsp2Pkg: " duntan
2023-03-31  9:33 ` [Patch V2 3/8] MdeModulePkg: " duntan
2023-04-14  9:09   ` Wang, Jian J
2023-04-24 10:11     ` duntan
2023-03-31  9:33 ` [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file duntan
2023-03-31 11:50   ` [edk2-devel] " Gerd Hoffmann
2023-03-31  9:33 ` [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck duntan
2023-04-14  9:03   ` Wang, Jian J
2023-04-14 15:16     ` [edk2-devel] " Michael D Kinney
2023-04-14 16:07       ` Ni, Ray
2023-04-15 15:50         ` Michael D Kinney
2023-04-15 15:57           ` Michael D Kinney
2023-04-16  5:21             ` Ni, Ray
2023-04-18 19:06               ` Michael D Kinney
2023-04-19  6:00                 ` Ni, Ray
2023-04-19 15:02                   ` Michael D Kinney
2023-04-21  8:10                     ` Ni, Ray
2023-04-21 15:42                       ` Michael D Kinney
2023-04-24 10:27                         ` duntan
2023-04-24 15:59               ` Ard Biesheuvel
2023-03-31  9:33 ` [Patch V2 6/8] MdeModulePkg/DxeIpl: Create page table by CpuPageTableLib duntan
2023-03-31 10:24   ` Ni, Ray
2023-03-31  9:33 ` [Patch V2 7/8] MdeModulePkg/DxeIpl: Remove duplicated code to enable NX duntan
2023-03-31  9:33 ` [Patch V2 8/8] MdeModulePkg/DxeIpl: Refinement to the code to set PageTable as RO duntan
2023-03-31 10:25   ` Ni, Ray
     [not found] ` <1751776493F12DAB.27612@groups.io>
2023-04-11  6:51   ` [edk2-devel] [Patch V2 4/8] OvmfPkg: Add CpuPageTableLib required by DxeIpl in DSC file duntan
2023-04-11  8:56     ` Gerd Hoffmann
     [not found] ` <1751776BEEE9C9E8.27612@groups.io>
2023-04-11  6:51   ` [edk2-devel] [Patch V2 5/8] MdeModulePkg: Add UefiCpuPkg.dec to pass DependencyCheck duntan
     [not found] ` <1751776458C20361.12651@groups.io>
2023-04-11  6:53   ` [edk2-devel] [Patch V2 3/8] MdeModulePkg: Add CpuPageTableLib required by DxeIpl in DSC duntan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox