Re: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove MpService2Ppi field in SMM_S3_RESUME_STATE

2024-05-28 Thread duntan
Liming,

Sure, I think we can remove the field. Could you please add your reviewed-by 
for this patch?

Thanks,
Dun

-Original Message-
From: gaoliming  
Sent: Tuesday, May 28, 2024 5:39 PM
To: Tan, Dun ; devel@edk2.groups.io
Cc: Ni, Ray ; Wang, Jian J 
Subject: 回复: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove MpService2Ppi 
field in SMM_S3_RESUME_STATE

Dun:
  If you confirm that there is no impact on the existing module, you can remove 
it. 

Thanks
Liming
> -邮件原件-
> 发件人: Tan, Dun 
> 发送时间: 2024年5月28日 17:34
> 收件人: gaoliming ; devel@edk2.groups.io
> 抄送: Ni, Ray ; Wang, Jian J 
> 主题: RE: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove 
> MpService2Ppi field in SMM_S3_RESUME_STATE
> 
> Hi Liming,
> 
> Sorry I didn't get what the code needs to be compatible with. Could 
> you please explain more?
> The MpService2Ppi field was added in " MdeModulePkg: add MpService2Ppi 
> field in SMM_S3_RESUME_STATE"( b1955cddb2) last year by me. Previously 
> It was added to simplify the code logic to wakeup AP in S3 boot 
> related code of CPU SMM driver when code execution modes for PEI and 
> DXE are the same. In this patch set, all the related code logic was removed.
> 
> Thanks,
> Dun
> 
> -Original Message-
> From: gaoliming 
> Sent: Tuesday, May 28, 2024 5:17 PM
> To: devel@edk2.groups.io; Tan, Dun 
> Cc: Ni, Ray ; Wang, Jian J 
> Subject: 回复: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove 
> MpService2Ppi field in SMM_S3_RESUME_STATE
> 
> Dun:
>   To be compatible, I suggest to keep the same layout of 
> SMM_S3_RESUME_STATE. MpService2Ppi field can be kept, but it is not 
> used any more.
> 
> Thanks
> Liming
> > -邮件原件-
> > 发件人: devel@edk2.groups.io  代表 duntan
> > 发送时间: 2024年5月28日 14:35
> > 收件人: devel@edk2.groups.io; Tan, Dun ; Liming Gao 
> > 
> > 抄送: Ni, Ray ; Wang, Jian J 
> > 主题: Re: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove 
> > MpService2Ppi field in SMM_S3_RESUME_STATE
> >
> > Hi Liming,
> >
> > Could you please help to review this patch?
> > This patch set is to simplify the X86 CPU SMM driver by removing 
> > most of
> S3
> > related logics and we want to merge it as soon as possible.
> >
> > Thanks,
> > Dun
> >
> > -Original Message-
> > From: devel@edk2.groups.io  On Behalf Of 
> > duntan
> > Sent: Friday, May 17, 2024 5:46 PM
> > To: devel@edk2.groups.io
> > Cc: Ni, Ray ; Wang, Jian J 
> > ;
> Liming
> > Gao 
> > Subject: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove 
> > MpService2Ppi field in SMM_S3_RESUME_STATE
> >
> > This MpService2Ppi field in SMM_S3_RESUME_STATE is used to wakeup AP 
> > to do the CPU initialization during smm s3 boot when the execution 
> > mode of PEI
> and
> > DXE are the same.
> > Currently, in CpuS3.c of smm cpu driver, BSP doesn't need to wakeup 
> > AP anymore. The initialization for AP will be done in S3Resume.c 
> > before
> transfer to
> > CpuS3.c of smm cpu driver.
> > So we can remove the MpService2Ppi field in SMM_S3_RESUME_STATE.
> >
> > Signed-off-by: Dun Tan 
> > Reviewed-by: Ray Ni 
> > Cc: Jian J Wang 
> > Cc: Liming Gao 
> > ---
> >  MdeModulePkg/Include/Guid/AcpiS3Context.h | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> >
> > diff --git a/MdeModulePkg/Include/Guid/AcpiS3Context.h
> > b/MdeModulePkg/Include/Guid/AcpiS3Context.h
> > index 72d173c4fd..6c7237727e 100644
> > --- a/MdeModulePkg/Include/Guid/AcpiS3Context.h
> > +++ b/MdeModulePkg/Include/Guid/AcpiS3Context.h
> > @@ -1,7 +1,7 @@
> >  /** @file
> >Definitions for data structures used in S3 resume.
> >
> > -Copyright (c) 2011 - 2023, Intel Corporation. All rights 
> > reserved.
> > +Copyright (c) 2011 - 2024, Intel Corporation. All rights 
> > +reserved.
> >
> >  SPDX-License-Identifier: BSD-2-Clause-Patent
> >
> > @@ -30,7 +30,6 @@ typedef struct {
> >EFI_PHYSICAL_ADDRESSReturnContext1;
> >EFI_PHYSICAL_ADDRESSReturnContext2;
> >EFI_PHYSICAL_ADDRESSReturnStackPointer;
> > -  EFI_PHYSICAL_ADDRESSMpService2Ppi;
> >EFI_PHYSICAL_ADDRESSSmst;
> >  } SMM_S3_RESUME_STATE;
> >
> > --
> > 2.31.1.windows.1
> >
> >
> >
> >
> >
> >
> >
> >
> > 
> >
> 
> 





-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119299): https://edk2.groups.io/g/devel/message/119299
Mute This Topic: https://groups.io/mt/106345604/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove MpService2Ppi field in SMM_S3_RESUME_STATE

2024-05-28 Thread duntan
Hi Liming,

Sorry I didn't get what the code needs to be compatible with. Could you please 
explain more?
The MpService2Ppi field was added in " MdeModulePkg: add MpService2Ppi field in 
SMM_S3_RESUME_STATE"( b1955cddb2) last year by me. Previously It was added to 
simplify the code logic to wakeup AP in S3 boot related code of CPU SMM driver 
when code execution modes for PEI and DXE are the same. In this patch set, all 
the related code logic was removed.

Thanks,
Dun

-Original Message-
From: gaoliming  
Sent: Tuesday, May 28, 2024 5:17 PM
To: devel@edk2.groups.io; Tan, Dun 
Cc: Ni, Ray ; Wang, Jian J 
Subject: 回复: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove MpService2Ppi 
field in SMM_S3_RESUME_STATE

Dun:
  To be compatible, I suggest to keep the same layout of SMM_S3_RESUME_STATE. 
MpService2Ppi field can be kept, but it is not used any more. 

Thanks
Liming
> -邮件原件-
> 发件人: devel@edk2.groups.io  代表 duntan
> 发送时间: 2024年5月28日 14:35
> 收件人: devel@edk2.groups.io; Tan, Dun ; Liming Gao 
> 
> 抄送: Ni, Ray ; Wang, Jian J 
> 主题: Re: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove 
> MpService2Ppi field in SMM_S3_RESUME_STATE
> 
> Hi Liming,
> 
> Could you please help to review this patch?
> This patch set is to simplify the X86 CPU SMM driver by removing most 
> of
S3
> related logics and we want to merge it as soon as possible.
> 
> Thanks,
> Dun
> 
> -Original Message-
> From: devel@edk2.groups.io  On Behalf Of duntan
> Sent: Friday, May 17, 2024 5:46 PM
> To: devel@edk2.groups.io
> Cc: Ni, Ray ; Wang, Jian J ;
Liming
> Gao 
> Subject: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove 
> MpService2Ppi field in SMM_S3_RESUME_STATE
> 
> This MpService2Ppi field in SMM_S3_RESUME_STATE is used to wakeup AP 
> to do the CPU initialization during smm s3 boot when the execution 
> mode of PEI
and
> DXE are the same.
> Currently, in CpuS3.c of smm cpu driver, BSP doesn't need to wakeup AP 
> anymore. The initialization for AP will be done in S3Resume.c before
transfer to
> CpuS3.c of smm cpu driver.
> So we can remove the MpService2Ppi field in SMM_S3_RESUME_STATE.
> 
> Signed-off-by: Dun Tan 
> Reviewed-by: Ray Ni 
> Cc: Jian J Wang 
> Cc: Liming Gao 
> ---
>  MdeModulePkg/Include/Guid/AcpiS3Context.h | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/MdeModulePkg/Include/Guid/AcpiS3Context.h
> b/MdeModulePkg/Include/Guid/AcpiS3Context.h
> index 72d173c4fd..6c7237727e 100644
> --- a/MdeModulePkg/Include/Guid/AcpiS3Context.h
> +++ b/MdeModulePkg/Include/Guid/AcpiS3Context.h
> @@ -1,7 +1,7 @@
>  /** @file
>Definitions for data structures used in S3 resume.
> 
> -Copyright (c) 2011 - 2023, Intel Corporation. All rights 
> reserved.
> +Copyright (c) 2011 - 2024, Intel Corporation. All rights 
> +reserved.
> 
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> @@ -30,7 +30,6 @@ typedef struct {
>EFI_PHYSICAL_ADDRESSReturnContext1;
>EFI_PHYSICAL_ADDRESSReturnContext2;
>EFI_PHYSICAL_ADDRESSReturnStackPointer;
> -  EFI_PHYSICAL_ADDRESSMpService2Ppi;
>EFI_PHYSICAL_ADDRESSSmst;
>  } SMM_S3_RESUME_STATE;
> 
> --
> 2.31.1.windows.1
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 





-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119296): https://edk2.groups.io/g/devel/message/119296
Mute This Topic: https://groups.io/mt/106345552/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V2 01/18] MdeModulePkg: Add gEdkiiS3MtrrSettingGuid

2024-05-28 Thread duntan
Hi Liming,

Could you please help to review this patch?
This patch set is to simplify the X86 CPU SMM driver by removing most of S3 
related logics and we want to merge it as soon as possible.

Thanks,
Dun

-Original Message-
From: devel@edk2.groups.io  On Behalf Of duntan
Sent: Friday, May 17, 2024 5:46 PM
To: devel@edk2.groups.io
Cc: Ni, Ray ; Liming Gao ; Wu, 
Jiaxin 
Subject: [edk2-devel] [Patch V2 01/18] MdeModulePkg: Add gEdkiiS3MtrrSettingGuid

Add gEdkiiS3MtrrSettingGuid a new GUID for s3 MTRR setting. This GUID will be 
used to save MTRR_SETTINGS at EndOfDxe by LockBox and restore at S3 boot PEI 
phase for s3 usage.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/MdeModulePkg.dec | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec 
index f7339f0aec..02f330a453 100644
--- a/MdeModulePkg/MdeModulePkg.dec
+++ b/MdeModulePkg/MdeModulePkg.dec
@@ -4,7 +4,7 @@
 # and libraries instances, which are used for those modules.
 #
 # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
-# Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.
+# Copyright (c) 2007 - 2024, Intel Corporation. All rights 
+reserved.
 # Copyright (c) 2016, Linaro Ltd. All rights reserved.  # (C) Copyright 
2016 - 2019 Hewlett Packard Enterprise Development LP  # Copyright (c) 
2017, AMD Incorporated. All rights reserved. @@ -465,6 +465,9 @@
   gEdk2JedecSfdpSpiDxeDriverGuid  = { 0xBE71701E, 0xB63C, 0x4574, { 0x9C, 
0x5C, 0x36, 0x29, 0xE8, 0xEA, 0xC4, 0x14 }}
   gEdk2JedecSfdpSpiSmmDriverGuid  = { 0x95A1E915, 0x195C, 0x477C, { 0x92, 
0x6F, 0x7E, 0x24, 0x67, 0xC1, 0xB3, 0x1F }}
 
+  ## This GUID will be used to save MTRR_SETTINGS at EndOfDxe by LockBox and 
restore at S3 boot PEI phase for s3 usage.
+  gEdkiiS3MtrrSettingGuid = { 0xd77baa84, 0xb332, 0x4463, { 0x9f, 0x1d, 
+ 0xce, 0x81, 0x00, 0xfe, 0x7f, 0x35 }}
+
 [Ppis]
   ## Include/Ppi/FirmwareVolumeShadowPpi.h
   gEdkiiPeiFirmwareVolumeShadowPpiGuid = { 0x7dfe756c, 0xed8d, 0x4d77, {0x9e, 
0xc4, 0x39, 0x9a, 0x8a, 0x81, 0x51, 0x16 } }
--
2.31.1.windows.1








-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119293): https://edk2.groups.io/g/devel/message/119293
Mute This Topic: https://groups.io/mt/106344405/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove MpService2Ppi field in SMM_S3_RESUME_STATE

2024-05-28 Thread duntan
Hi Liming,

Could you please help to review this patch?
This patch set is to simplify the X86 CPU SMM driver by removing most of S3 
related logics and we want to merge it as soon as possible.

Thanks,
Dun

-Original Message-
From: devel@edk2.groups.io  On Behalf Of duntan
Sent: Friday, May 17, 2024 5:46 PM
To: devel@edk2.groups.io
Cc: Ni, Ray ; Wang, Jian J ; Liming 
Gao 
Subject: [edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove MpService2Ppi field 
in SMM_S3_RESUME_STATE

This MpService2Ppi field in SMM_S3_RESUME_STATE is used to wakeup AP to do the 
CPU initialization during smm s3 boot when the execution mode of PEI and DXE 
are the same.
Currently, in CpuS3.c of smm cpu driver, BSP doesn't need to wakeup AP anymore. 
The initialization for AP will be done in S3Resume.c before transfer to CpuS3.c 
of smm cpu driver.
So we can remove the MpService2Ppi field in SMM_S3_RESUME_STATE.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Jian J Wang 
Cc: Liming Gao 
---
 MdeModulePkg/Include/Guid/AcpiS3Context.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/MdeModulePkg/Include/Guid/AcpiS3Context.h 
b/MdeModulePkg/Include/Guid/AcpiS3Context.h
index 72d173c4fd..6c7237727e 100644
--- a/MdeModulePkg/Include/Guid/AcpiS3Context.h
+++ b/MdeModulePkg/Include/Guid/AcpiS3Context.h
@@ -1,7 +1,7 @@
 /** @file
   Definitions for data structures used in S3 resume.
 
-Copyright (c) 2011 - 2023, Intel Corporation. All rights reserved.
+Copyright (c) 2011 - 2024, Intel Corporation. All rights reserved.
 
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -30,7 +30,6 @@ typedef struct {
   EFI_PHYSICAL_ADDRESSReturnContext1;
   EFI_PHYSICAL_ADDRESSReturnContext2;
   EFI_PHYSICAL_ADDRESSReturnStackPointer;
-  EFI_PHYSICAL_ADDRESSMpService2Ppi;
   EFI_PHYSICAL_ADDRESSSmst;
 } SMM_S3_RESUME_STATE;
 
--
2.31.1.windows.1








-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119292): https://edk2.groups.io/g/devel/message/119292
Mute This Topic: https://groups.io/mt/106344380/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V2 00/18] Remove some S3 related code in CpuS3.c of smm cpu driver

2024-05-23 Thread duntan
Hi Liming,



Could you please help to review the following 2 patches in the patch series?

  [Patch V2 01/18] MdeModulePkg: Add gEdkiiS3MtrrSettingGuid

  [Patch V2 17/18] MdeModulePkg:Remove MpService2Ppi field in 
SMM_S3_RESUME_STATE



Thanks,

Dun

From: Ni, Ray 
Sent: Monday, May 20, 2024 3:05 PM
To: Tan, Dun ; devel@edk2.groups.io
Cc: Liming Gao ; Wu, Jiaxin ; 
Ard Biesheuvel ; Yao, Jiewen ; 
Gerd Hoffmann ; Kumar, Rahul R 
Subject: Re: [Patch V2 00/18] Remove some S3 related code in CpuS3.c of smm cpu 
driver

Following 3 patches still require the Reviewed-by from package maintainers of 
MdeModulePkg and OvmfPkg.
  MdeModulePkg: Add gEdkiiS3MtrrSettingGuid
  OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe
  MdeModulePkg:Remove MpService2Ppi field in SMM_S3_RESUME_STATE

The patch set is a good move to simplify the X86 CPU SMM driver by removing 
most of S3 related logics.
I hope it can be merged next week when the stable tag freeze ends in the end of 
this week.

Thanks,
Ray

From: Tan, Dun mailto:dun@intel.com>>
Sent: Friday, May 17, 2024 17:45
To: devel@edk2.groups.io 
mailto:devel@edk2.groups.io>>
Cc: Liming Gao mailto:gaolim...@byosoft.com.cn>>; Wu, 
Jiaxin mailto:jiaxin...@intel.com>>; Ni, Ray 
mailto:ray...@intel.com>>; Ard Biesheuvel 
mailto:ardb+tianoc...@kernel.org>>; Yao, Jiewen 
mailto:jiewen@intel.com>>; Gerd Hoffmann 
mailto:kra...@redhat.com>>; Kumar, Rahul R 
mailto:rahul.r.ku...@intel.com>>
Subject: [Patch V2 00/18] Remove some S3 related code in CpuS3.c of smm cpu 
driver

Comparing to V1 patchs set, the V2 patch set only adjusts the commits ordering 
and modifies copy right year in some files.
This patch set is to remove some S3 related code in CpuS3.c of smm cpu driver. 
It contain commits to:
1) S3 MTRRs operation:
>   MdeModulePkg: Add gEdkiiS3MtrrSettingGuid
>   OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe
>   UefiCpuPkg: Add locbox lib instance in DSC
>   UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe
>   UefiCpuPkg: LoadMtrrData for all cpu in S3Resume
>   UefiCpuPkg: Remove code to load mtrr setting

2) AP page table unavailiable issue fix:
>   UefiCpuPkg: Disable PG in IA32 ApLoopCode

3) Register table cleanup:
>   UefiCpuPkg:Set PcdCpuFeaturesInitOnS3Resume to TRUE
>   UefiCpuPkg: Remove code to set register table

4)  S3 ApHltLoopCode Operation:
>   UefiCpuPkg:Abstract some DxeMpLib code to function
>   UefiCpuPkg:Move some code in DxeMpLib to common place
>   UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume
>   UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib
>   UefiCpuPkg:Remove code to handle APIC setting and Interrupt
>   UefiCpuPkg:Rremove code to wakeup AP and relocate ap
>   UefiCpuPkg: Remove the duplicated mpservice locate
>   MdeModulePkg: remove MpService2Ppi field in SMM_S3_RESUME_STATE

5)  Remove code to get AcpiCpuData:
>   UefiCpuPkg: Remove GetAcpiCpuData() in CpuS3.c

With this patch set, CpuS3.c in smm CPU driver can be simplified.
The whole patch set has been reviewed-by Ray.
PR for review: https://github.com/tianocore/edk2/pull/5606

Cc: Liming Gao mailto:gaolim...@byosoft.com.cn>>
Cc: Jiaxin Wu mailto:jiaxin...@intel.com>>
Cc: Ray Ni mailto:ray...@intel.com>>
Cc: Ard Biesheuvel mailto:ardb+tianoc...@kernel.org>>
Cc: Jiewen Yao mailto:jiewen@intel.com>>
Cc: Gerd Hoffmann mailto:kra...@redhat.com>>
Cc: Rahul Kumar mailto:rahul1.ku...@intel.com>>
Cc: Gerd Hoffmann mailto:kra...@redhat.com>>

Dun Tan (18):
  MdeModulePkg: Add gEdkiiS3MtrrSettingGuid
  OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe
  UefiCpuPkg: Add locbox lib instance in DSC
  UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe
  UefiCpuPkg: LoadMtrrData for all cpu in S3Resume
  UefiCpuPkg: Remove code to load mtrr setting
  UefiCpuPkg:Set PcdCpuFeaturesInitOnS3Resume to TRUE
  UefiCpuPkg: Remove code to set register table
  UefiCpuPkg: Disable PG in IA32 ApLoopCode
  UefiCpuPkg:Abstract some DxeMpLib code to function
  UefiCpuPkg:Move some code in DxeMpLib to common place
  UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume
  UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib
  UefiCpuPkg:Remove code to handle APIC setting and Interrupt
  UefiCpuPkg:Remove code to wakeup AP and relocate ap
  UefiCpuPkg: Remove unneeded MpService2Ppi assignment
  MdeModulePkg:Remove MpService2Ppi field in SMM_S3_RESUME_STATE
  UefiCpuPkg: Remove GetAcpiCpuData() in CpuS3.c

 MdeModulePkg/Include/Guid/AcpiS3Context.h   |   3 +--
 MdeModulePkg/MdeModulePkg.dec   |   5 -
 OvmfPkg/CpuS3DataDxe/CpuS3Data.c|  13 -
 OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf   |   4 +++-
 UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c |  13 -
 UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf|   4 +++-
 UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 183 

Re: [edk2-devel] [PATCH 0/9] Allocate and unblock variable runtime cache buffer in PEI

2024-05-21 Thread duntan
Hi Sean,

I saw the PR contribution process proposal in community. It looks good.

I agree that the existence of the HOB can indicate if the feature is used or 
not on the consumer side. Will move the reference to 
PcdEnableVariableRuntimeCache on the HOB consumer side. But I think 
PcdEnableVariableRuntimeCache is still needed to decide whether we need to 
build HOB or not.

For other comments, I’ll modify the patch series based on your comments and the 
sample you mentioned. Thanks for you detailed comments.

Thanks,
Dun

From: Sean Brogan 
Sent: Tuesday, May 21, 2024 2:17 AM
To: devel@edk2.groups.io; Tan, Dun 
Cc: Ni, Ray ; Liming Gao ; Wu, 
Jiaxin ; Ard Biesheuvel ; Leif 
Lindholm ; Sami Mujawar ; Gerd 
Hoffmann ; Andrew Fish ; Yao, Jiewen 

Subject: Re: [edk2-devel] [PATCH 0/9] Allocate and unblock variable runtime 
cache buffer in PEI


I can't find patch 1 in the series in my email so putting a few comments here.  
I really hope this patch series can wait for PRs so code comments can more 
easily be correlated with code change.

Looking at your PR with commit: Allocate Varaible cache buffer in PEI by td36 · 
Pull Request #5607 · tianocore/edk2 
(github.com)<https://github.com/tianocore/edk2/pull/5607/commits/36c2cac5fa4196be7fc85d842e8056e376010479>

A few comments for now.

Variable is spelled incorrectly in commit message.

I would really prefer to not mix PCDs and Hobs.  It is really confusing what 
has to be turned on and set to what to get the different behaviors.  For a hob 
producer it is OK to take that info from PCDs but lets not mix in the consumer 
code PCDs and hob data.  The HOB definition should not reference a PCD and a 
HOB definition should not be focused on producer/consumer but on the data.

The existence of a hob is also a good indicator that a feature is used so you 
may not need to have "enable" PCDs anymore.

Please don't include other header files in public header files (especially for 
super common headers like PiPei.h.  i know over the last few years this 
practice has become more common but it just creates pain when debugging build 
errors.  The trade off is not worth it.

Hobs really shouldn't use UINTN sizes.  Since hobs helps transfer config state 
across phases where the size of UINTN may be different this causes problems.

Hobs used for cross phase sharing shouldn't have pointers for the reason.

Better and more complete comments on the different field members would be 
helpful.  I assume the "Buffer" fields are physical addresses and the "Pages" 
are number of 4K pages.   I would suggest EFI_PHYSICAL_ADDRESS for addresses 
and UINT64 for lengths.

More details on what the three cache's are would be helpful or at least 
reference the feature of the cache they are supporting.

This hob definition file isn't perfect and I believe some of the comment in the 
file header belong in different places, it is at least a good template for a 
hob definition.

edk2/MdeModulePkg/Include/Guid/VariableFlashInfo.h at 
284dbac43da752ee34825c8b3f6f9e8281cb5a19 · tianocore/edk2 
(github.com)<https://github.com/tianocore/edk2/blob/284dbac43da752ee34825c8b3f6f9e8281cb5a19/MdeModulePkg/Include/Guid/VariableFlashInfo.h>



Thanks

Sean




On 5/17/2024 2:49 AM, duntan wrote:

This patch set defines a new VARIABLE_RUNTIME_CACHE_INFO HOB. The HOB is used 
to store the address and size of the buffer that will be used for variable 
runtime service when the PcdEnableVariableRuntimeCache is TRUE.

In following patches, when PcdEnableVariableRuntimeCache is TRUE, VariablePei 
will install a callback of gEfiPeiMemoryDiscoveredPpiGuid to allocate the 
needed buffer for different type variable runtime cache and build the HOB.

Then VariableSmmRuntimeDxe driver will consume 
gEdkiiVariableRuntimeCacheInfoHobGuid to initialize the variable runtime cache 
related content. The code to allocate and unblock the runtime cache buffer in 
VariableSmmRuntimeDxe is also removed in this patc set.



PR for review: https://github.com/tianocore/edk2/pull/5607



Cc: Ray Ni <mailto:ray...@intel.com>

Cc: Liming Gao <mailto:gaolim...@byosoft.com.cn>

Cc: Jiaxin Wu <mailto:jiaxin...@intel.com>

Cc: Ard Biesheuvel <mailto:ardb+tianoc...@kernel.org>

Cc: Leif Lindholm <mailto:quic_llind...@quicinc.com>

Cc: Sami Mujawar <mailto:sami.muja...@arm.com>

Cc: Gerd Hoffmann <mailto:kra...@redhat.com>

Cc: Andrew Fish <mailto:af...@apple.com>

Cc: Jiewen Yao <mailto:jiewen@intel.com>



Dun Tan (9):

  MdeModulePkg:Add new gEdkiiVariableRuntimeCacheInfoHobGuid

  ArmVirtPkg: Add MmUnblockMemoryLib in DSC

  EmulatorPkg: Add MmUnblockMemoryLib in DSC

  OvmfPkg: Add MmUnblockMemoryLib in DSC

  MdeModulePkg:Create gEdkiiVariableRuntimeCacheInfoHobGuid

  MdeModulePkg:Remove unnecessary global variable

  MdeModulePkg:Consume gEdkiiVariableRuntimeCacheInfoHobGuid

  MdeModulePkg: Refine InitVariableCa

Re: [edk2-devel] [PATCH 0/9] Allocate and unblock variable runtime cache buffer in PEI

2024-05-20 Thread duntan
Hi Liming,

I haven't created a Bugzilla for this change.
Is a bugzila needed for this patch set? I can create one if needed.

Thanks,
Dun

-Original Message-
From: gaoliming  
Sent: Monday, May 20, 2024 9:41 AM
To: devel@edk2.groups.io; Tan, Dun 
Cc: Ni, Ray ; Wu, Jiaxin ; 'Ard 
Biesheuvel' ; 'Leif Lindholm' 
; 'Sami Mujawar' ; 'Gerd 
Hoffmann' ; 'Andrew Fish' ; Yao, Jiewen 

Subject: 回复: [edk2-devel] [PATCH 0/9] Allocate and unblock variable runtime 
cache buffer in PEI

Dun:
  Is there a Bugzilla for this change?

Thanks
Liming
> -邮件原件-
> 发件人: devel@edk2.groups.io  代表 duntan
> 发送时间: 2024年5月17日 17:49
> 收件人: devel@edk2.groups.io
> 抄送: Ray Ni ; Liming Gao ; 
> Jiaxin Wu ; Ard Biesheuvel
;
> Leif Lindholm ; Sami Mujawar 
> ; Gerd Hoffmann ; Andrew Fish 
> ; Jiewen Yao 
> 主题: [edk2-devel] [PATCH 0/9] Allocate and unblock variable runtime 
> cache buffer in PEI
> 
> This patch set defines a new VARIABLE_RUNTIME_CACHE_INFO HOB. The HOB 
> is used to store the address and size of the buffer that will be used 
> for
variable
> runtime service when the PcdEnableVariableRuntimeCache is TRUE.
> In following patches, when PcdEnableVariableRuntimeCache is TRUE,
VariablePei
> will install a callback of gEfiPeiMemoryDiscoveredPpiGuid to allocate 
> the
needed
> buffer for different type variable runtime cache and build the HOB.
> Then VariableSmmRuntimeDxe driver will consume 
> gEdkiiVariableRuntimeCacheInfoHobGuid to initialize the variable 
> runtime
cache
> related content. The code to allocate and unblock the runtime cache 
> buffer
in
> VariableSmmRuntimeDxe is also removed in this patc set.
> 
> PR for review: https://github.com/tianocore/edk2/pull/5607
> 
> Cc: Ray Ni 
> Cc: Liming Gao 
> Cc: Jiaxin Wu 
> Cc: Ard Biesheuvel 
> Cc: Leif Lindholm 
> Cc: Sami Mujawar 
> Cc: Gerd Hoffmann 
> Cc: Andrew Fish 
> Cc: Jiewen Yao 
> 
> Dun Tan (9):
>   MdeModulePkg:Add new gEdkiiVariableRuntimeCacheInfoHobGuid
>   ArmVirtPkg: Add MmUnblockMemoryLib in DSC
>   EmulatorPkg: Add MmUnblockMemoryLib in DSC
>   OvmfPkg: Add MmUnblockMemoryLib in DSC
>   MdeModulePkg:Create gEdkiiVariableRuntimeCacheInfoHobGuid
>   MdeModulePkg:Remove unnecessary global variable
>   MdeModulePkg:Consume gEdkiiVariableRuntimeCacheInfoHobGuid
>   MdeModulePkg: Refine InitVariableCache()
>   MdeModulePkg:Add global variable mVariableRtCacheInfo
> 
>  ArmVirtPkg/ArmVirtCloudHv.dsc
> |   2 ++
>  EmulatorPkg/EmulatorPkg.dsc
> |   1 +
>  MdeModulePkg/Include/Guid/VariableRuntimeCacheInfo.h
> |  65
> +
> 
>  MdeModulePkg/MdeModulePkg.dec
> |   3 +++
>  MdeModulePkg/Universal/Variable/Pei/Variable.c   |
> 298
> +
> +
> +
> +
> +-
>  MdeModulePkg/Universal/Variable/Pei/Variable.h   |
> 3 +++
>  MdeModulePkg/Universal/Variable/Pei/VariablePei.inf  |
> 8 +++-
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
> | 293
> +
> +
>
++--
++
-
> --
>  MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf 
> |
> 5 +++--
>  OvmfPkg/OvmfPkgIa32X64.dsc
> |   2 +-
>  10 files changed, 506 insertions(+), 174 deletions(-)  create mode 
> 100644 MdeModulePkg/Include/Guid/VariableRuntimeCacheInfo.h
> 
> --
> 2.31.1.windows.1
> 
> 
> 
> 
> 





-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119075): https://edk2.groups.io/g/devel/message/119075
Mute This Topic: https://groups.io/mt/106199240/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 9/9] MdeModulePkg:Add global variable mVariableRtCacheInfo

2024-05-17 Thread duntan
Add global variable mVariableRtCacheInfo to save the
content in gEdkiiVariableRuntimeCacheInfoHobGuid. With
this new global variable, 7 global variables can be
removed.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 97 
+
 1 file changed, 41 insertions(+), 56 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c 
b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
index 6efe5cee10..de39462d68 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
@@ -44,26 +44,20 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include "PrivilegePolymorphic.h"
 #include "VariableParsing.h"
 
-EFI_HANDLE  mHandle  = NULL;
-EFI_SMM_VARIABLE_PROTOCOL   *mSmmVariable= NULL;
-EFI_EVENT   mVirtualAddressChangeEvent   = NULL;
-EFI_MM_COMMUNICATION2_PROTOCOL  *mMmCommunication2   = NULL;
-UINT8   *mVariableBuffer = NULL;
-UINT8   *mVariableBufferPhysical = NULL;
-VARIABLE_INFO_ENTRY *mVariableInfo   = NULL;
-VARIABLE_STORE_HEADER   *mVariableRuntimeHobCacheBuffer  = NULL;
-VARIABLE_STORE_HEADER   *mVariableRuntimeNvCacheBuffer   = NULL;
-VARIABLE_STORE_HEADER   *mVariableRuntimeVolatileCacheBuffer = NULL;
+EFI_HANDLE  mHandle= NULL;
+EFI_SMM_VARIABLE_PROTOCOL   *mSmmVariable  = NULL;
+EFI_EVENT   mVirtualAddressChangeEvent = NULL;
+EFI_MM_COMMUNICATION2_PROTOCOL  *mMmCommunication2 = NULL;
+UINT8   *mVariableBuffer   = NULL;
+UINT8   *mVariableBufferPhysical   = NULL;
+VARIABLE_INFO_ENTRY *mVariableInfo = NULL;
 UINTN   mVariableBufferSize;
-UINTN   mVariableRuntimeHobCacheBufferSize;
 UINTN   mVariableBufferPayloadSize;
-BOOLEAN *mVariableRuntimeCachePendingUpdate;
-BOOLEAN *mVariableRuntimeCacheReadLock;
 BOOLEAN mVariableAuthFormat;
-BOOLEAN *mHobFlushComplete;
 EFI_LOCKmVariableServicesLock;
 EDKII_VARIABLE_LOCK_PROTOCOLmVariableLock;
 EDKII_VAR_CHECK_PROTOCOLmVarCheck;
+VARIABLE_RUNTIME_CACHE_INFO mVariableRtCacheInfo;
 
 /**
   The logic to initialize the VariablePolicy engine is in its own file.
@@ -500,21 +494,21 @@ CheckForRuntimeCacheSync (
   VOID
   )
 {
-  if (*mVariableRuntimeCachePendingUpdate) {
+  if (mVariableRtCacheInfo.CacheInfoFlag->PendingUpdate) {
 SyncRuntimeCache ();
   }
 
-  ASSERT (!(*mVariableRuntimeCachePendingUpdate));
+  ASSERT (!(mVariableRtCacheInfo.CacheInfoFlag->PendingUpdate));
 
   //
   // The HOB variable data may have finished being flushed in the runtime 
cache sync update
   //
-  if ((*mHobFlushComplete) && (mVariableRuntimeHobCacheBuffer != NULL)) {
+  if ((mVariableRtCacheInfo.CacheInfoFlag->HobFlushComplete) && 
(mVariableRtCacheInfo.RuntimeHobCacheBuffer != 0)) {
 if (!EfiAtRuntime ()) {
-  FreePages (mVariableRuntimeHobCacheBuffer, EFI_SIZE_TO_PAGES 
(mVariableRuntimeHobCacheBufferSize));
+  FreePages ((VOID *)(UINTN)mVariableRtCacheInfo.RuntimeHobCacheBuffer, 
mVariableRtCacheInfo.RuntimeHobCachePages);
 }
 
-mVariableRuntimeHobCacheBuffer = NULL;
+mVariableRtCacheInfo.RuntimeHobCacheBuffer = 0;
   }
 }
 
@@ -565,20 +559,20 @@ FindVariableInRuntimeCache (
   // a GetVariable () or GetNextVariable () call from being issued until a 
prior call has returned. The runtime
   // cache read lock should always be free when entering this function.
   //
-  ASSERT (!(*mVariableRuntimeCacheReadLock));
+  ASSERT (!(mVariableRtCacheInfo.CacheInfoFlag->ReadLock));
 
-  *mVariableRuntimeCacheReadLock = TRUE;
+  mVariableRtCacheInfo.CacheInfoFlag->ReadLock = TRUE;
   CheckForRuntimeCacheSync ();
 
-  if (!(*mVariableRuntimeCachePendingUpdate)) {
+  if (!(mVariableRtCacheInfo.CacheInfoFlag->PendingUpdate)) {
 //
 // 0: Volatile, 1: HOB, 2: Non-Volatile.
 // The index and attributes mapping must be kept in this order as 
FindVariable
 // makes use of this mapping to implement search algorithm.
 //
-VariableStoreList[VariableStoreTypeVolatile] = 
mVariableRuntimeVolatileCacheBuffer;
-VariableStoreList[VariableStoreTypeHob]  = 
mVariableRuntimeHobCacheBuffer;
-VariableStoreList[VariableStoreTypeNv]   = 
mVariableRuntimeNvCacheBuffer;
+

[edk2-devel] [PATCH 8/9] MdeModulePkg: Refine InitVariableCache()

2024-05-17 Thread duntan
Refine the code logic in InitVariableCache().
In this commit, three times calling of
InitVariableCache() for different type cache are
merged into one calling. This commit is to make
the code looks cleaner and doesn't change any
code functionality.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 198 
+++---
 1 file changed, 95 insertions(+), 103 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c 
b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
index 68a249c5ac..6efe5cee10 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
@@ -144,54 +144,6 @@ AtRuntime (
   return EfiAtRuntime ();
 }
 
-/**
-  Initialize the variable cache buffer as an empty variable store.
-
-  @param[out] VariableCacheBuffer A pointer to pointer of a cache 
variable store.
-  @param[in,out]  TotalVariableCacheSize  On input, the minimum size needed 
for the UEFI variable store cache
-  buffer that is allocated. On output, 
the actual size of the buffer allocated.
-  If TotalVariableCacheSize is zero, a 
buffer will not be allocated and the
-  function will return with 
EFI_SUCCESS.
-
-  @retval EFI_SUCCESS The variable cache was allocated and 
initialized successfully.
-  @retval EFI_INVALID_PARAMETER   A given pointer is NULL or an invalid 
variable store size was specified.
-  @retval EFI_OUT_OF_RESOURCESInsufficient resources are available to 
allocate the variable store cache buffer.
-
-**/
-EFI_STATUS
-InitVariableCache (
-  OUTVARIABLE_STORE_HEADER  **VariableCacheBuffer,
-  IN OUT UINTN  *TotalVariableCacheSize
-  )
-{
-  VARIABLE_STORE_HEADER  *VariableCacheStorePtr;
-  EFI_STATUS Status;
-
-  if (TotalVariableCacheSize == NULL) {
-return EFI_INVALID_PARAMETER;
-  }
-
-  if (*TotalVariableCacheSize == 0) {
-return EFI_SUCCESS;
-  }
-
-  if ((VariableCacheBuffer == NULL) || (*TotalVariableCacheSize < sizeof 
(VARIABLE_STORE_HEADER))) {
-return EFI_INVALID_PARAMETER;
-  }
-
-  *TotalVariableCacheSize = ALIGN_VALUE (*TotalVariableCacheSize, sizeof 
(UINT32));
-
-  VariableCacheStorePtr = *VariableCacheBuffer;
-  SetMem32 ((VOID *)VariableCacheStorePtr, *TotalVariableCacheSize, 
(UINT32)0x);
-
-  ZeroMem ((VOID *)VariableCacheStorePtr, sizeof (VARIABLE_STORE_HEADER));
-  VariableCacheStorePtr->Size   = (UINT32)*TotalVariableCacheSize;
-  VariableCacheStorePtr->Format = VARIABLE_STORE_FORMATTED;
-  VariableCacheStorePtr->State  = VARIABLE_STORE_HEALTHY;
-
-  return EFI_SUCCESS;
-}
-
 /**
   Initialize the communicate buffer using DataSize and Function.
 
@@ -1554,6 +1506,92 @@ Done:
   return Status;
 }
 
+/**
+  Initialize the variable cache buffer as an empty variable store.
+
+  @param[in]  VariableCacheBuffer A pointer to pointer of a cache variable 
store.
+  @param[in]  TotalVariableCacheSize  The size needed for the UEFI variable 
store cache buffer that is allocated.
+
+**/
+VOID
+InitVariableStoreHeader (
+  IN  VARIABLE_STORE_HEADER  *VariableCacheBuffer,
+  IN  UINTN  TotalVariableCacheSize
+  )
+{
+  if (TotalVariableCacheSize > 0) {
+ASSERT ((VariableCacheBuffer != NULL) && (TotalVariableCacheSize >= sizeof 
(VARIABLE_STORE_HEADER)));
+
+SetMem32 ((VOID *)VariableCacheBuffer, TotalVariableCacheSize, 
(UINT32)0x);
+ZeroMem ((VOID *)VariableCacheBuffer, sizeof (VARIABLE_STORE_HEADER));
+VariableCacheBuffer->Size   = (UINT32)TotalVariableCacheSize;
+VariableCacheBuffer->Format = VARIABLE_STORE_FORMATTED;
+VariableCacheBuffer->State  = VARIABLE_STORE_HEALTHY;
+  }
+}
+
+/**
+  Initialize the runtime variable cache related content.
+
+  @retval EFI_SUCCESSInitialize the runtime variable cache related content 
successfully.
+  @retval Others Could not initialize the runtime variable cache 
related content successfully.
+
+**/
+EFI_STATUS
+InitVariableCache (
+  VOID
+  )
+{
+  EFI_STATUS   Status;
+  UINTNExpectedHobCacheSize;
+  UINTNExpectedNvCacheSize;
+  UINTNExpectedVolatileCacheSize;
+  UINTNAllocatedHobCacheSize;
+  UINTNAllocatedNvCacheSize;
+  UINTNAllocatedVolatileCacheSize;
+  EFI_HOB_GUID_TYPE*GuidHob;
+  VARIABLE_RUNTIME_CACHE_INFO  *VariableRuntimeCacheHob;
+
+  DEBUG ((DEBUG_INFO, "Variable driver runtime cache is enabled.\n"));
+  //
+  // Get 

[edk2-devel] [PATCH 7/9] MdeModulePkg:Consume gEdkiiVariableRuntimeCacheInfoHobGuid

2024-05-17 Thread duntan
Consume gEdkiiVariableRuntimeCacheInfoHobGuid in

VariableSmmRuntimeDxe driver to initialize the following

variable cache related buffer:
  *mVariableRuntimeHobCacheBuffer
  *mVariableRuntimeNvCacheBuffer
  *mVariableRuntimeVolatileCacheBuffer
  *mVariableRuntimeCachePendingUpdate
  *mVariableRuntimeCacheReadLock
  *mHobFlushComplete

The code to to allocate 
and unblock the buffer for
different type cache in VariableSmmRuntimeDxe is also
removed in this commit.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Liming Gao 

Cc: Jiaxin Wu 
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c   | 120 
+---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf |   5 
+++--
 2 files changed, 52 insertions(+), 73 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c 
b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
index 8b42ae7d72..68a249c5ac 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
@@ -35,10 +35,11 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include 
 #include 
 #include 
-#include 
+#include 
 
 #include 
 #include 
+#include 
 
 #include "PrivilegePolymorphic.h"
 #include "VariableParsing.h"
@@ -56,10 +57,10 @@ VARIABLE_STORE_HEADER   
*mVariableRuntimeVolatileCacheBuffer = NULL;
 UINTN   mVariableBufferSize;
 UINTN   mVariableRuntimeHobCacheBufferSize;
 UINTN   mVariableBufferPayloadSize;
-BOOLEAN mVariableRuntimeCachePendingUpdate;
-BOOLEAN mVariableRuntimeCacheReadLock;
+BOOLEAN *mVariableRuntimeCachePendingUpdate;
+BOOLEAN *mVariableRuntimeCacheReadLock;
 BOOLEAN mVariableAuthFormat;
-BOOLEAN mHobFlushComplete;
+BOOLEAN *mHobFlushComplete;
 EFI_LOCKmVariableServicesLock;
 EDKII_VARIABLE_LOCK_PROTOCOLmVariableLock;
 EDKII_VAR_CHECK_PROTOCOLmVarCheck;
@@ -180,27 +181,6 @@ InitVariableCache (
 
   *TotalVariableCacheSize = ALIGN_VALUE (*TotalVariableCacheSize, sizeof 
(UINT32));
 
-  //
-  // Allocate NV Storage Cache and initialize it to all 1's (like an erased FV)
-  //
-  *VariableCacheBuffer =  (VARIABLE_STORE_HEADER *)AllocateRuntimePages (
- EFI_SIZE_TO_PAGES 
(*TotalVariableCacheSize)
- );
-  if (*VariableCacheBuffer == NULL) {
-return EFI_OUT_OF_RESOURCES;
-  }
-
-  //
-  // Request to unblock the newly allocated cache region to be accessible from 
inside MM
-  //
-  Status = MmUnblockMemoryRequest (
- (EFI_PHYSICAL_ADDRESS)(UINTN)*VariableCacheBuffer,
- EFI_SIZE_TO_PAGES (*TotalVariableCacheSize)
- );
-  if ((Status != EFI_UNSUPPORTED) && EFI_ERROR (Status)) {
-return Status;
-  }
-
   VariableCacheStorePtr = *VariableCacheBuffer;
   SetMem32 ((VOID *)VariableCacheStorePtr, *TotalVariableCacheSize, 
(UINT32)0x);
 
@@ -568,16 +548,16 @@ CheckForRuntimeCacheSync (
   VOID
   )
 {
-  if (mVariableRuntimeCachePendingUpdate) {
+  if (*mVariableRuntimeCachePendingUpdate) {
 SyncRuntimeCache ();
   }
 
-  ASSERT (!mVariableRuntimeCachePendingUpdate);
+  ASSERT (!(*mVariableRuntimeCachePendingUpdate));
 
   //
   // The HOB variable data may have finished being flushed in the runtime 
cache sync update
   //
-  if (mHobFlushComplete && (mVariableRuntimeHobCacheBuffer != NULL)) {
+  if ((*mHobFlushComplete) && (mVariableRuntimeHobCacheBuffer != NULL)) {
 if (!EfiAtRuntime ()) {
   FreePages (mVariableRuntimeHobCacheBuffer, EFI_SIZE_TO_PAGES 
(mVariableRuntimeHobCacheBufferSize));
 }
@@ -633,12 +613,12 @@ FindVariableInRuntimeCache (
   // a GetVariable () or GetNextVariable () call from being issued until a 
prior call has returned. The runtime
   // cache read lock should always be free when entering this function.
   //
-  ASSERT (!mVariableRuntimeCacheReadLock);
+  ASSERT (!(*mVariableRuntimeCacheReadLock));
 
-  mVariableRuntimeCacheReadLock = TRUE;
+  *mVariableRuntimeCacheReadLock = TRUE;
   CheckForRuntimeCacheSync ();
 
-  if (!mVariableRuntimeCachePendingUpdate) {
+  if (!(*mVariableRuntimeCachePendingUpdate)) {
 //
 // 0: Volatile, 1: HOB, 2: Non-Volatile.
 // The index and attributes mapping must be kept in this order as 
FindVariable
@@ -698,7 +678,7 @@ Done:
 }
   }
 
-  mVariableRuntimeCacheReadLock = FALSE;
+  *mVariableRuntimeCacheReadLock = FALSE;
 
   return Status;
 }
@@ -921,12 +901,12 @@ GetNextVariableNameInRuntimeCache (
   // a GetVariable () or GetNextVariable () call from being issued until a 
prior 

[edk2-devel] [PATCH 6/9] MdeModulePkg:Remove unnecessary global variable

2024-05-17 Thread duntan
Remove the two unnecessary global variables and
replace them by two local variables:
mVariableRuntimeNvCacheBufferSize
mVariableRuntimeVolatileCacheBufferSize

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c | 14 
+++---
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c 
b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
index 6930875e9f..8b42ae7d72 100644
--- a/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
+++ b/MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c
@@ -13,7 +13,7 @@
 
   InitCommunicateBuffer() is really function to check the variable data size.
 
-Copyright (c) 2010 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.
 Copyright (c) Microsoft Corporation.
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -55,8 +55,6 @@ VARIABLE_STORE_HEADER   
*mVariableRuntimeNvCacheBuffer   = NULL;
 VARIABLE_STORE_HEADER   *mVariableRuntimeVolatileCacheBuffer = NULL;
 UINTN   mVariableBufferSize;
 UINTN   mVariableRuntimeHobCacheBufferSize;
-UINTN   mVariableRuntimeNvCacheBufferSize;
-UINTN   mVariableRuntimeVolatileCacheBufferSize;
 UINTN   mVariableBufferPayloadSize;
 BOOLEAN mVariableRuntimeCachePendingUpdate;
 BOOLEAN mVariableRuntimeCacheReadLock;
@@ -1691,6 +1689,8 @@ SmmVariableReady (
   )
 {
   EFI_STATUS  Status;
+  UINTN   RuntimeNvCacheSize;
+  UINTN   RuntimeVolatileCacheSize;
 
   Status = gBS->LocateProtocol (, NULL, (VOID 
**));
   if (EFI_ERROR (Status)) {
@@ -1721,16 +1721,16 @@ SmmVariableReady (
 //
 Status =  GetRuntimeCacheInfo (
 ,
-,
-,
+,
+,
 
 );
 if (!EFI_ERROR (Status)) {
   Status = InitVariableCache (, 
);
   if (!EFI_ERROR (Status)) {
-Status = InitVariableCache (, 
);
+Status = InitVariableCache (, 
);
 if (!EFI_ERROR (Status)) {
-  Status = InitVariableCache (, 
);
+  Status = InitVariableCache (, 
);
   if (!EFI_ERROR (Status)) {
 Status = SendRuntimeVariableCacheContextToSmm ();
 if (!EFI_ERROR (Status)) {
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119023): https://edk2.groups.io/g/devel/message/119023
Mute This Topic: https://groups.io/mt/106150803/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 5/9] MdeModulePkg:Create gEdkiiVariableRuntimeCacheInfoHobGuid

2024-05-17 Thread duntan
Install the callback of gEfiPeiMemoryDiscoveredPpiGuid
to create gEdkiiVariableRuntimeCacheInfoHobGuid in
VariablePei module. When PcdEnableVariableRuntimeCache
is TRUE, the callback will be installed to allocate
the needed buffer for different type variable runtime
cache, unblock the buffer and build this HOB. Then the
runtime cache buffer address and size will be saved in
the HOB content.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/Universal/Variable/Pei/Variable.c  | 298 
+-
 MdeModulePkg/Universal/Variable/Pei/Variable.h  |   3 +++
 MdeModulePkg/Universal/Variable/Pei/VariablePei.inf |   8 +++-
 3 files changed, 307 insertions(+), 2 deletions(-)

diff --git a/MdeModulePkg/Universal/Variable/Pei/Variable.c 
b/MdeModulePkg/Universal/Variable/Pei/Variable.c
index 26a4c73b45..15419eb437 100644
--- a/MdeModulePkg/Universal/Variable/Pei/Variable.c
+++ b/MdeModulePkg/Universal/Variable/Pei/Variable.c
@@ -2,7 +2,7 @@
   Implement ReadOnly Variable Services required by PEIM and install
   PEI ReadOnly Varaiable2 PPI. These services operates the non volatile 
storage space.
 
-Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2024, Intel Corporation. All rights reserved.
 Copyright (c) Microsoft Corporation.
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -24,6 +24,31 @@ EFI_PEI_PPI_DESCRIPTOR  mPpiListVariable = {
   
 };
 
+/**
+  Build gEdkiiVariableRuntimeCacheInfoHobGuid.
+
+  @param[in] PeiServices  General purpose services available to every 
PEIM.
+  @param[in] NotifyDescriptor The notification structure this PEIM 
registered on install.
+  @param[in] Ppi  The memory discovered PPI.  Not used.
+
+  @retval EFI_SUCCESS The function completed successfully.
+  @retval others  Failed to build VariableRuntimeCacheInfo Hob.
+
+**/
+EFI_STATUS
+EFIAPI
+BuildVariableRuntimeCacheInfoHob (
+  IN EFI_PEI_SERVICES   **PeiServices,
+  IN EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDescriptor,
+  IN VOID   *Ppi
+  );
+
+EFI_PEI_NOTIFY_DESCRIPTOR  mPostMemNotifyList = {
+  (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | 
EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
+  ,
+  BuildVariableRuntimeCacheInfoHob
+};
+
 /**
   Provide the functionality of the variable services.
 
@@ -41,6 +66,10 @@ PeimInitializeVariableServices (
   IN CONST EFI_PEI_SERVICES **PeiServices
   )
 {
+  if (FeaturePcdGet (PcdEnableVariableRuntimeCache)) {
+PeiServicesNotifyPpi ();
+  }
+
   return PeiServicesInstallPpi ();
 }
 
@@ -1250,3 +1279,270 @@ PeiGetNextVariableName (
 }
   }
 }
+
+/**
+  Calculate the auth variable storage size converted from normal variable 
storage.
+
+  @param[in]  StoreInfo Pointer to the store info
+  @param[in]  NormalHobVarStorage  Pointer to the normal variable storage 
header
+
+  @retval the auth variable storage size
+**/
+UINTN
+CalculateAuthVarStorageSize (
+  IN  VARIABLE_STORE_INFO*StoreInfo,
+  IN  VARIABLE_STORE_HEADER  *NormalHobVarStorage
+  )
+{
+  VARIABLE_HEADER  *StartPtr;
+  VARIABLE_HEADER  *EndPtr;
+  UINTNAuthVarStroageSize;
+
+  AuthVarStroageSize = sizeof (VARIABLE_STORE_HEADER);
+
+  //
+  // Calculate Auth Variable Storage Size
+  //
+  StartPtr = GetStartPointer (NormalHobVarStorage);
+  EndPtr   = GetEndPointer (NormalHobVarStorage);
+  while (StartPtr < EndPtr) {
+if (StartPtr->State == VAR_ADDED) {
+  AuthVarStroageSize  = HEADER_ALIGN (AuthVarStroageSize);
+  AuthVarStroageSize += sizeof (AUTHENTICATED_VARIABLE_HEADER);
+  AuthVarStroageSize += StartPtr->NameSize + GET_PAD_SIZE 
(StartPtr->NameSize);
+  AuthVarStroageSize += StartPtr->DataSize + GET_PAD_SIZE 
(StartPtr->DataSize);
+}
+
+StartPtr = GetNextVariablePtr (StoreInfo, StartPtr, StartPtr);
+  }
+
+  return AuthVarStroageSize;
+}
+
+/**
+  Calculate Hob variable cache size.
+
+  @param[in]  NvAuthFlag   If the NV variable store is Auth.
+
+  @retval Maximum of Nv variable cache size.
+
+**/
+UINTN
+CalculateHobVariableCacheSize (
+  IN BOOLEAN  NvAuthFlag
+  )
+{
+  VARIABLE_STORE_INFOStoreInfo;
+  VARIABLE_STORE_HEADER  *VariableStoreHeader;
+
+  VariableStoreHeader = NULL;
+  GetHobVariableStore (, );
+
+  if (VariableStoreHeader == NULL) {
+return 0;
+  }
+
+  if (NvAuthFlag == StoreInfo.AuthFlag) {
+return VariableStoreHeader->Size;
+  } else {
+//
+// Normal NV variable store + Auth HOB variable store is not supported
+//
+ASSERT (NvAuthFlag && (!StoreInfo.AuthFlag));
+
+//
+// Need to calculate auth variable storage size converted from normal 
variable storage
+//
+

[edk2-devel] [PATCH 3/9] EmulatorPkg: Add MmUnblockMemoryLib in DSC

2024-05-17 Thread duntan
Add MmUnblockMemoryLib in EmulatorPkg.dsc.
This lib will be required by VariablePei in
following commits.

Signed-off-by: Dun Tan 
Cc: Andrew Fish 
Cc: Ray Ni 
---
 EmulatorPkg/EmulatorPkg.dsc | 1 +
 1 file changed, 1 insertion(+)

diff --git a/EmulatorPkg/EmulatorPkg.dsc b/EmulatorPkg/EmulatorPkg.dsc
index 5fa1ed345a..0e15dafb5c 100644
--- a/EmulatorPkg/EmulatorPkg.dsc
+++ b/EmulatorPkg/EmulatorPkg.dsc
@@ -127,6 +127,7 @@
   ShellLib|ShellPkg/Library/UefiShellLib/UefiShellLib.inf
   FileHandleLib|MdePkg/Library/UefiFileHandleLib/UefiFileHandleLib.inf
   
ImagePropertiesRecordLib|MdeModulePkg/Library/ImagePropertiesRecordLib/ImagePropertiesRecordLib.inf
+  
MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockMemoryLibNull.inf
 
 !if $(SECURE_BOOT_ENABLE) == TRUE
   RngLib|MdeModulePkg/Library/BaseRngLibTimerLib/BaseRngLibTimerLib.inf
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119020): https://edk2.groups.io/g/devel/message/119020
Mute This Topic: https://groups.io/mt/106150799/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 4/9] OvmfPkg: Add MmUnblockMemoryLib in DSC

2024-05-17 Thread duntan
Add MmUnblockMemoryLib in OvmfPkgIa32X64.dsc.
This lib will be required by VariablePei in
following commits.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Ard Biesheuvel 
Cc: Jiewen Yao 
Cc: Gerd Hoffmann 
---
 OvmfPkg/OvmfPkgIa32X64.dsc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/OvmfPkg/OvmfPkgIa32X64.dsc b/OvmfPkg/OvmfPkgIa32X64.dsc
index d27a4c7278..cc03104aac 100644
--- a/OvmfPkg/OvmfPkgIa32X64.dsc
+++ b/OvmfPkg/OvmfPkgIa32X64.dsc
@@ -233,7 +233,7 @@
   
VariablePolicyLib|MdeModulePkg/Library/VariablePolicyLib/VariablePolicyLib.inf
   
VariablePolicyHelperLib|MdeModulePkg/Library/VariablePolicyHelperLib/VariablePolicyHelperLib.inf
   
VariableFlashInfoLib|MdeModulePkg/Library/BaseVariableFlashInfoLib/BaseVariableFlashInfoLib.inf
-
+  
MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockMemoryLibNull.inf
 
   #
   # Network libraries
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119021): https://edk2.groups.io/g/devel/message/119021
Mute This Topic: https://groups.io/mt/106150800/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 2/9] ArmVirtPkg: Add MmUnblockMemoryLib in DSC

2024-05-17 Thread duntan
Add MmUnblockMemoryLib in ArmVirtCloudHv.dsc.
This lib will be required by VariablePei in
following commits.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Ard Biesheuvel 
Cc: Leif Lindholm 
Cc: Sami Mujawar 
Cc: Gerd Hoffmann 
---
 ArmVirtPkg/ArmVirtCloudHv.dsc | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/ArmVirtPkg/ArmVirtCloudHv.dsc b/ArmVirtPkg/ArmVirtCloudHv.dsc
index 5cb2a609b1..a8ede49ef9 100644
--- a/ArmVirtPkg/ArmVirtCloudHv.dsc
+++ b/ArmVirtPkg/ArmVirtCloudHv.dsc
@@ -57,6 +57,8 @@
   
TpmMeasurementLib|MdeModulePkg/Library/TpmMeasurementLibNull/TpmMeasurementLibNull.inf
   
TpmPlatformHierarchyLib|SecurityPkg/Library/PeiDxeTpmPlatformHierarchyLibNull/PeiDxeTpmPlatformHierarchyLib.inf
 
+  
MmUnblockMemoryLib|MdePkg/Library/MmUnblockMemoryLib/MmUnblockMemoryLibNull.inf
+
 !include MdePkg/MdeLibs.dsc.inc
 
 [LibraryClasses.common.PEIM]
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119019): https://edk2.groups.io/g/devel/message/119019
Mute This Topic: https://groups.io/mt/106150798/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 1/9] MdeModulePkg:Add new gEdkiiVariableRuntimeCacheInfoHobGuid

2024-05-17 Thread duntan
This commit defines VARIABLE_RUNTIME_CACHE_INFO HOB.
The HOB is used to store the address and size of the
buffer that will be used for variable runtime service
when the PcdEnableVariableRuntimeCache is TRUE.

In following patches, when PcdEnableVariableRuntimeCache
is TRUE, VariablePei module will install a callback of
gEfiPeiMemoryDiscoveredPpiGuid to allocate needed buffer
for different type cache, unblock the buffer and build HOB.
Then VariableSmmRuntimeDxe driver will consume the
gEdkiiVariableRuntimeCacheInfoHobGuid to initialize the
variable runtime cache related content.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/Include/Guid/VariableRuntimeCacheInfo.h | 65 
+
 MdeModulePkg/MdeModulePkg.dec|  3 +++
 2 files changed, 68 insertions(+)

diff --git a/MdeModulePkg/Include/Guid/VariableRuntimeCacheInfo.h 
b/MdeModulePkg/Include/Guid/VariableRuntimeCacheInfo.h
new file mode 100644
index 00..c2a8b77945
--- /dev/null
+++ b/MdeModulePkg/Include/Guid/VariableRuntimeCacheInfo.h
@@ -0,0 +1,65 @@
+/** @file
+  This Variable Runtime Cache Info HOB is used to store the address
+  and the size of the buffer that will be used for variable runtime
+  service when the PcdEnableVariableRuntimeCache is TRUE.
+
+  Copyright (c) 2024, Intel Corporation. All rights reserved.
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef VARIABLE_RUNTIME_CACHE_INFO_H_
+#define VARIABLE_RUNTIME_CACHE_INFO_H_
+
+#include 
+
+#define VARIABLE_RUNTIME_CACHE_INFO_HOB_REVISION  1
+
+#define VARIABLE_RUNTIME_CACHE_INFO_GUID \
+  { \
+0x0f472f7d, 0x6713, 0x4915, {0x96, 0x14, 0x5d, 0xda, 0x28, 0x40, 0x10, 
0x56}  \
+  }
+
+#pragma pack(1)
+typedef struct {
+  ///
+  /// TRUE indicates GetVariable () or GetNextVariable () is being called.
+  /// When the value is FALSE, the given update (and any other pending updates)
+  /// can be flushed to the runtime cache.
+  ///
+  BOOLEANReadLock;
+  ///
+  /// TRUE indicates there is pending update for the given variable store 
needed
+  /// to be flushed to the runtime cache.
+  ///
+  BOOLEANPendingUpdate;
+  ///
+  /// TRUE indicates all HOB variables have been flushed in flash.
+  ///
+  BOOLEANHobFlushComplete;
+} CACHE_INFO_FLAG;
+
+typedef struct {
+  CACHE_INFO_FLAG*CacheInfoFlag;
+  ///
+  /// Buffer reserved for runtime Hob cache
+  ///
+  UINT64 RuntimeHobCacheBuffer;
+  UINTN  RuntimeHobCachePages;
+  ///
+  /// Buffer reserved for Non-Volatile runtime cache
+  ///
+  UINT64 RuntimeNvCacheBuffer;
+  UINTN  RuntimeNvCachePages;
+  ///
+  /// Buffer reserved for Volatile runtime cache
+  ///
+  UINT64 RuntimeVolatileCacheBuffer;
+  UINTN  RuntimeVolatileCachePages;
+} VARIABLE_RUNTIME_CACHE_INFO;
+#pragma pack()
+
+extern EFI_GUID  gEdkiiVariableRuntimeCacheInfoHobGuid;
+
+#endif
diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec
index f7339f0aec..1bf5e31b7c 100644
--- a/MdeModulePkg/MdeModulePkg.dec
+++ b/MdeModulePkg/MdeModulePkg.dec
@@ -465,6 +465,9 @@
   gEdk2JedecSfdpSpiDxeDriverGuid  = { 0xBE71701E, 0xB63C, 0x4574, { 0x9C, 
0x5C, 0x36, 0x29, 0xE8, 0xEA, 0xC4, 0x14 }}
   gEdk2JedecSfdpSpiSmmDriverGuid  = { 0x95A1E915, 0x195C, 0x477C, { 0x92, 
0x6F, 0x7E, 0x24, 0x67, 0xC1, 0xB3, 0x1F }}
 
+  ## Include/Guid/VariableRuntimeCacheInfo.h
+  gEdkiiVariableRuntimeCacheInfoHobGuid = { 0x0f472f7d, 0x6713, 0x4915, { 
0x96, 0x14, 0x5d, 0xda, 0x28, 0x40, 0x10, 0x56 }}
+
 [Ppis]
   ## Include/Ppi/FirmwareVolumeShadowPpi.h
   gEdkiiPeiFirmwareVolumeShadowPpiGuid = { 0x7dfe756c, 0xed8d, 0x4d77, {0x9e, 
0xc4, 0x39, 0x9a, 0x8a, 0x81, 0x51, 0x16 } }
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119018): https://edk2.groups.io/g/devel/message/119018
Mute This Topic: https://groups.io/mt/106150797/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 0/9] Allocate and unblock variable runtime cache buffer in PEI

2024-05-17 Thread duntan
This patch set defines a new VARIABLE_RUNTIME_CACHE_INFO HOB. The HOB is used 
to store the address and size of the buffer that will be used for variable 
runtime service when the PcdEnableVariableRuntimeCache is TRUE.
In following patches, when PcdEnableVariableRuntimeCache is TRUE, VariablePei 
will install a callback of gEfiPeiMemoryDiscoveredPpiGuid to allocate the 
needed buffer for different type variable runtime cache and build the HOB.
Then VariableSmmRuntimeDxe driver will consume 
gEdkiiVariableRuntimeCacheInfoHobGuid to initialize the variable runtime cache 
related content. The code to allocate and unblock the runtime cache buffer in 
VariableSmmRuntimeDxe is also removed in this patc set.

PR for review: https://github.com/tianocore/edk2/pull/5607

Cc: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
Cc: Ard Biesheuvel 
Cc: Leif Lindholm 
Cc: Sami Mujawar 
Cc: Gerd Hoffmann 
Cc: Andrew Fish 
Cc: Jiewen Yao 

Dun Tan (9):
  MdeModulePkg:Add new gEdkiiVariableRuntimeCacheInfoHobGuid
  ArmVirtPkg: Add MmUnblockMemoryLib in DSC
  EmulatorPkg: Add MmUnblockMemoryLib in DSC
  OvmfPkg: Add MmUnblockMemoryLib in DSC
  MdeModulePkg:Create gEdkiiVariableRuntimeCacheInfoHobGuid
  MdeModulePkg:Remove unnecessary global variable
  MdeModulePkg:Consume gEdkiiVariableRuntimeCacheInfoHobGuid
  MdeModulePkg: Refine InitVariableCache()
  MdeModulePkg:Add global variable mVariableRtCacheInfo

 ArmVirtPkg/ArmVirtCloudHv.dsc|   2 ++
 EmulatorPkg/EmulatorPkg.dsc  |   1 +
 MdeModulePkg/Include/Guid/VariableRuntimeCacheInfo.h |  65 
+
 MdeModulePkg/MdeModulePkg.dec|   3 +++
 MdeModulePkg/Universal/Variable/Pei/Variable.c   | 298 
+-
 MdeModulePkg/Universal/Variable/Pei/Variable.h   |   3 +++
 MdeModulePkg/Universal/Variable/Pei/VariablePei.inf  |   8 
+++-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.c   | 293 
-
 MdeModulePkg/Universal/Variable/RuntimeDxe/VariableSmmRuntimeDxe.inf |   5 
+++--
 OvmfPkg/OvmfPkgIa32X64.dsc   |   2 +-
 10 files changed, 506 insertions(+), 174 deletions(-)
 create mode 100644 MdeModulePkg/Include/Guid/VariableRuntimeCacheInfo.h

-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119017): https://edk2.groups.io/g/devel/message/119017
Mute This Topic: https://groups.io/mt/106150796/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 18/18] UefiCpuPkg: Remove GetAcpiCpuData() in CpuS3.c

2024-05-17 Thread duntan
Remove GetAcpiCpuData() in CpuS3.c. The mAcpiCpuData
is not needed in S3 boot anymore.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c  | 243 
+--
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c |   8 +++-
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h |  26 +-
 3 files changed, 5 insertions(+), 272 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index 4f69326fdf..c37a2d4d1b 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -9,22 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include "PiSmmCpuDxeSmm.h"
 #include 
 
-//
-// Flags used when program the register.
-//
-typedef struct {
-  volatile UINTN MemoryMappedLock;  // Spinlock used to 
program mmio
-  volatile UINT32*CoreSemaphoreCount;   // Semaphore container 
used to program
-// core level semaphore.
-  volatile UINT32*PackageSemaphoreCount;// Semaphore container 
used to program
-// package level semaphore.
-} PROGRAM_CPU_REGISTER_FLAGS;
-
-#define LEGACY_REGION_SIZE  (2 * 0x1000)
-#define LEGACY_REGION_BASE  (0xA - LEGACY_REGION_SIZE)
-
-ACPI_CPU_DATA  mAcpiCpuData;
-BOOLEANmRestoreSmmConfigurationInS3 = FALSE;
+BOOLEAN  mRestoreSmmConfigurationInS3 = FALSE;
 
 //
 // S3 boot flag
@@ -266,232 +251,6 @@ InitSmmS3ResumeState (
   }
 }
 
-/**
-  Copy register table from non-SMRAM into SMRAM.
-
-  @param[in] DestinationRegisterTableList  Points to destination register 
table.
-  @param[in] SourceRegisterTableList   Points to source register table.
-  @param[in] NumberOfCpus  Number of CPUs.
-
-**/
-VOID
-CopyRegisterTable (
-  IN CPU_REGISTER_TABLE  *DestinationRegisterTableList,
-  IN CPU_REGISTER_TABLE  *SourceRegisterTableList,
-  IN UINT32  NumberOfCpus
-  )
-{
-  UINTN Index;
-  CPU_REGISTER_TABLE_ENTRY  *RegisterTableEntry;
-
-  CopyMem (DestinationRegisterTableList, SourceRegisterTableList, NumberOfCpus 
* sizeof (CPU_REGISTER_TABLE));
-  for (Index = 0; Index < NumberOfCpus; Index++) {
-if (DestinationRegisterTableList[Index].TableLength != 0) {
-  DestinationRegisterTableList[Index].AllocatedSize = 
DestinationRegisterTableList[Index].TableLength * sizeof 
(CPU_REGISTER_TABLE_ENTRY);
-  RegisterTableEntry= AllocateCopyPool (
-
DestinationRegisterTableList[Index].AllocatedSize,
-(VOID 
*)(UINTN)SourceRegisterTableList[Index].RegisterTableEntry
-);
-  ASSERT (RegisterTableEntry != NULL);
-  DestinationRegisterTableList[Index].RegisterTableEntry = 
(EFI_PHYSICAL_ADDRESS)(UINTN)RegisterTableEntry;
-}
-  }
-}
-
-/**
-  Check whether the register table is empty or not.
-
-  @param[in] RegisterTable  Point to the register table.
-  @param[in] NumberOfCpus   Number of CPUs.
-
-  @retval TRUE  The register table is empty.
-  @retval FALSE The register table is not empty.
-**/
-BOOLEAN
-IsRegisterTableEmpty (
-  IN CPU_REGISTER_TABLE  *RegisterTable,
-  IN UINT32  NumberOfCpus
-  )
-{
-  UINTN  Index;
-
-  if (RegisterTable != NULL) {
-for (Index = 0; Index < NumberOfCpus; Index++) {
-  if (RegisterTable[Index].TableLength != 0) {
-return FALSE;
-  }
-}
-  }
-
-  return TRUE;
-}
-
-/**
-  Copy the data used to initialize processor register into SMRAM.
-
-  @param[in,out]  CpuFeatureInitDataDst   Pointer to the destination 
CPU_FEATURE_INIT_DATA structure.
-  @param[in]  CpuFeatureInitDataSrc   Pointer to the source 
CPU_FEATURE_INIT_DATA structure.
-
-**/
-VOID
-CopyCpuFeatureInitDatatoSmram (
-  IN OUT CPU_FEATURE_INIT_DATA  *CpuFeatureInitDataDst,
-  IN CPU_FEATURE_INIT_DATA  *CpuFeatureInitDataSrc
-  )
-{
-  CPU_STATUS_INFORMATION  *CpuStatus;
-
-  if (!IsRegisterTableEmpty ((CPU_REGISTER_TABLE 
*)(UINTN)CpuFeatureInitDataSrc->PreSmmInitRegisterTable, 
mAcpiCpuData.NumberOfCpus)) {
-CpuFeatureInitDataDst->PreSmmInitRegisterTable = 
(EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof 
(CPU_REGISTER_TABLE));
-ASSERT (CpuFeatureInitDataDst->PreSmmInitRegisterTable != 0);
-
-CopyRegisterTable (
-  (CPU_REGISTER_TABLE 
*)(UINTN)CpuFeatureInitDataDst->PreSmmInitRegisterTable,
-  (CPU_REGISTER_TABLE 
*)(UINTN)CpuFeatureInitDataSrc->PreSmmInitRegisterTable,
-  

[edk2-devel] [Patch V2 16/18] UefiCpuPkg: Remove unneeded MpService2Ppi assignment

2024-05-17 Thread duntan
Remove the unneeded assignment of MpService2Ppi field
in SmmS3ResumeState. Previously, when the execution
combination of PEI and DXE are the same, the pointer
of mpservice ppi will be passed to CpuS3.c in smm cpu
driver to wakeup all APs, instead of init-sipi-sipi.
Currently, CpuS3.c doesn't need to wakeup Aps anymore.
So remove the duplicated mpservice locate and assignment
to MpService2Ppi field in SmmS3ResumeState.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c | 14 --
 1 file changed, 14 deletions(-)

diff --git a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c 
b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
index e6dfa09c71..3e64a115bf 100644
--- a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
+++ b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
@@ -1152,7 +1152,6 @@ S3RestoreConfig2 (
 SmmS3ResumeState->ReturnContext1 = 
(EFI_PHYSICAL_ADDRESS)(UINTN)AcpiS3Context;
 SmmS3ResumeState->ReturnContext2 = 
(EFI_PHYSICAL_ADDRESS)(UINTN)EfiBootScriptExecutorVariable;
 SmmS3ResumeState->ReturnStackPointer = 
(EFI_PHYSICAL_ADDRESS)STACK_ALIGN_DOWN ();
-SmmS3ResumeState->MpService2Ppi  = 0;
 
 DEBUG ((DEBUG_INFO, "SMM S3 Signature= %x\n", 
SmmS3ResumeState->Signature));
 DEBUG ((DEBUG_INFO, "SMM S3 Stack Base   = %x\n", 
SmmS3ResumeState->SmmS3StackBase));
@@ -1181,19 +1180,6 @@ S3RestoreConfig2 (
 if (((SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_32) && (sizeof 
(UINTN) == sizeof (UINT32))) ||
 ((SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_64) && (sizeof 
(UINTN) == sizeof (UINT64
 {
-  //
-  // Get MP Services2 Ppi to pass it to Smm S3.
-  //
-  Status = PeiServicesLocatePpi (
- ,
- 0,
- NULL,
- (VOID **)
- );
-  ASSERT_EFI_ERROR (Status);
-  SmmS3ResumeState->MpService2Ppi = 
(EFI_PHYSICAL_ADDRESS)(UINTN)MpService2Ppi;
-  DEBUG ((DEBUG_INFO, "SMM S3 MpService2Ppi Point = %lx\n", 
SmmS3ResumeState->MpService2Ppi));
-
   SwitchStack (
 
(SWITCH_STACK_ENTRY_POINT)(UINTN)SmmS3ResumeState->SmmS3ResumeEntryPoint,
 (VOID *)AcpiS3Context,
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119014): https://edk2.groups.io/g/devel/message/119014
Mute This Topic: https://groups.io/mt/106150779/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 17/18] MdeModulePkg:Remove MpService2Ppi field in SMM_S3_RESUME_STATE

2024-05-17 Thread duntan
This MpService2Ppi field in SMM_S3_RESUME_STATE is used to
wakeup AP to do the CPU initialization during smm s3 boot when
the execution mode of PEI and DXE are the same.
Currently, in CpuS3.c of smm cpu driver, BSP doesn't need to
wakeup AP anymore. The initialization for AP will be done in
S3Resume.c before transfer to CpuS3.c of smm cpu driver.
So we can remove the MpService2Ppi field in SMM_S3_RESUME_STATE.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Jian J Wang 
Cc: Liming Gao 
---
 MdeModulePkg/Include/Guid/AcpiS3Context.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/MdeModulePkg/Include/Guid/AcpiS3Context.h 
b/MdeModulePkg/Include/Guid/AcpiS3Context.h
index 72d173c4fd..6c7237727e 100644
--- a/MdeModulePkg/Include/Guid/AcpiS3Context.h
+++ b/MdeModulePkg/Include/Guid/AcpiS3Context.h
@@ -1,7 +1,7 @@
 /** @file
   Definitions for data structures used in S3 resume.
 
-Copyright (c) 2011 - 2023, Intel Corporation. All rights reserved.
+Copyright (c) 2011 - 2024, Intel Corporation. All rights reserved.
 
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -30,7 +30,6 @@ typedef struct {
   EFI_PHYSICAL_ADDRESSReturnContext1;
   EFI_PHYSICAL_ADDRESSReturnContext2;
   EFI_PHYSICAL_ADDRESSReturnStackPointer;
-  EFI_PHYSICAL_ADDRESSMpService2Ppi;
   EFI_PHYSICAL_ADDRESSSmst;
 } SMM_S3_RESUME_STATE;
 
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119013): https://edk2.groups.io/g/devel/message/119013
Mute This Topic: https://groups.io/mt/106150778/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 15/18] UefiCpuPkg:Remove code to wakeup AP and relocate ap

2024-05-17 Thread duntan
After the code to load mtrr setting, set register table,
handle APIC setting and Interrupt after INIT-SIPI-SIPI
is moved, the InitializeCpuProcedure() only contains
following code logic:
1.Bsp runs ExecuteFirstSmiInit().
2.Bsp transfers AP to safe hlt-loop

During S3 boot, since APs will be relocated to new safe
buffer by the callback of gEdkiiEndOfS3ResumeGuid in
PeiMpLib, Bsp doesn't need to transfer AP to safe hlt-loop
any more. SmmRestoreCpu() in CpuS3 only needs to runs the
ExecuteFirstSmiInit() on BSP. So remove code to wakeup
AP by INIT-SIPI-SIPI and remove code to relocate ap to
safe hlt-loop.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 292 
+---
 UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/MpFuncs.nasm   | 153 
-
 UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmFuncsArch.c |  29 
+
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf  |   5 +
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/MpFuncs.nasm| 189 
-
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmFuncsArch.c  |  30 
+-
 6 files changed, 12 insertions(+), 686 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index 89223e1726..4f69326fdf 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -8,30 +8,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #include "PiSmmCpuDxeSmm.h"
 #include 
-#include 
-
-#pragma pack(1)
-typedef struct {
-  UINTN  Lock;
-  VOID   *StackStart;
-  UINTN  StackSize;
-  VOID   *ApFunction;
-  IA32_DESCRIPTORGdtrProfile;
-  IA32_DESCRIPTORIdtrProfile;
-  UINT32 BufferStart;
-  UINT32 Cr3;
-  UINTN  InitializeFloatingPointUnitsAddress;
-} MP_CPU_EXCHANGE_INFO;
-#pragma pack()
-
-typedef struct {
-  UINT8*RendezvousFunnelAddress;
-  UINTNPModeEntryOffset;
-  UINTNFlatJumpOffset;
-  UINTNSize;
-  UINTNLModeEntryOffset;
-  UINTNLongJumpOffset;
-} MP_ASSEMBLY_ADDRESS_MAP;
 
 //
 // Flags used when program the register.
@@ -44,31 +20,11 @@ typedef struct {
 // package level semaphore.
 } PROGRAM_CPU_REGISTER_FLAGS;
 
-//
-// Signal that SMM BASE relocation is complete.
-//
-volatile BOOLEAN  mInitApsAfterSmmBaseReloc;
-
-/**
-  Get starting address and size of the rendezvous entry for APs.
-  Information for fixing a jump instruction in the code is also returned.
-
-  @param AddressMap  Output buffer for address map information.
-**/
-VOID *
-EFIAPI
-AsmGetAddressMap (
-  MP_ASSEMBLY_ADDRESS_MAP  *AddressMap
-  );
-
 #define LEGACY_REGION_SIZE  (2 * 0x1000)
 #define LEGACY_REGION_BASE  (0xA - LEGACY_REGION_SIZE)
 
-PROGRAM_CPU_REGISTER_FLAGS  mCpuFlags;
-ACPI_CPU_DATA   mAcpiCpuData;
-volatile UINT32 mNumberToFinish;
-MP_CPU_EXCHANGE_INFO*mExchangeInfo;
-BOOLEAN mRestoreSmmConfigurationInS3 = FALSE;
+ACPI_CPU_DATA  mAcpiCpuData;
+BOOLEANmRestoreSmmConfigurationInS3 = FALSE;
 
 //
 // S3 boot flag
@@ -82,191 +38,6 @@ SMM_S3_RESUME_STATE  *mSmmS3ResumeState = NULL;
 
 BOOLEAN  mAcpiS3Enable = TRUE;
 
-UINT8  *mApHltLoopCode  = NULL;
-UINT8  mApHltLoopCodeTemplate[] = {
-  0x8B, 0x44, 0x24, 0x04, // mov  eax, dword ptr [esp+4]
-  0xF0, 0xFF, 0x08,   // lock dec  dword ptr [eax]
-  0xFA,   // cli
-  0xF4,   // hlt
-  0xEB, 0xFC  // jmp $-2
-};
-
-/**
-  The function is invoked before SMBASE relocation in S3 path to restores CPU 
status.
-
-  The function is invoked before SMBASE relocation in S3 path. It does first 
time microcode load
-  and restores MTRRs for both BSP and APs.
-
-  @param   IsBsp   The CPU this function executes on is BSP or not.
-
-**/
-VOID
-InitializeCpuBeforeRebase (
-  IN BOOLEAN  IsBsp
-  )
-{
-  //
-  // Count down the number with lock mechanism.
-  //
-  InterlockedDecrement ();
-
-  if (IsBsp) {
-//
-// Bsp wait here till all AP finish the initialization before rebase
-//
-while (mNumberToFinish > 0) {
-  CpuPause ();
-}
-  }
-}
-
-/**
-  The function is invoked after SMBASE relocation in S3 path to restores CPU 
status.
-
-  The function is invoked after SMBASE relocation in S3 path. 

[edk2-devel] [Patch V2 14/18] UefiCpuPkg:Remove code to handle APIC setting and Interrupt

2024-05-17 Thread duntan
Remove ProgramVirtualWireMode()/DisableLvtInterrupts()
since APs won't be waken by INIT-SIPI-SIPI in CpuS3.c
any more. The two functions has been executed in
MpInitLibInitialize() in PeiMplib.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 5 -
 1 file changed, 5 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index 0bc4a3572d..89223e1726 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -105,11 +105,6 @@ InitializeCpuBeforeRebase (
   IN BOOLEAN  IsBsp
   )
 {
-  ProgramVirtualWireMode ();
-  if (!IsBsp) {
-DisableLvtInterrupts ();
-  }
-
   //
   // Count down the number with lock mechanism.
   //
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119011): https://edk2.groups.io/g/devel/message/119011
Mute This Topic: https://groups.io/mt/106150774/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 13/18] UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib

2024-05-17 Thread duntan
In this commit, change PeiMpLib to install callback
of gEdkiiEndOfS3ResumeGuid to relocate AP to new safe
buffer. The gEdkiiEndOfS3ResumeGuid is installed in
S3Resume.c before jmping to OS waking vector.

Previously, code in CpuS3.c of PiSmmCpuDxe driver will
prepare the new safe buffer for AP and place AP in hlt
loop state. With this code change, we can remove the
Machine Instructions of mApHltLoopCode in PiSmmCpuDxe.
Also we can reuse the related code in DxeMpLib for
PeiMpLib.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Library/MpInitLib/MpLib.h  |   3 +++
 UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf |   6 +-
 UefiCpuPkg/Library/MpInitLib/PeiMpLib.c   | 154 
+-
 3 files changed, 161 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h 
b/UefiCpuPkg/Library/MpInitLib/MpLib.h
index a4a33bf538..cc850c7dd5 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.h
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h
@@ -33,6 +33,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include 
@@ -68,6 +69,8 @@
 //
 #define DEFAULT_MAX_MICROCODE_PATCH_NUM  8
 
+#define PAGING_4K_ADDRESS_MASK_64  0x000FF000ull
+
 //
 // Data structure for microcode patch information
 //
diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf 
b/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf
index e31e34b6f9..e4a7485fef 100644
--- a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf
+++ b/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf
@@ -1,7 +1,7 @@
 ## @file
 #  MP Initialize Library instance for PEI driver.
 #
-#  Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.
+#  Copyright (c) 2016 - 2024, Intel Corporation. All rights reserved.
 #  Copyright (c) 2024, Loongson Technology Corporation Limited. All rights 
reserved.
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -25,10 +25,12 @@
 [Sources.IA32]
   Ia32/AmdSev.c
   Ia32/MpFuncs.nasm
+  Ia32/CreatePageTable.c
 
 [Sources.X64]
   X64/AmdSev.c
   X64/MpFuncs.nasm
+  X64/CreatePageTable.c
 
 [Sources.IA32, Sources.X64]
   AmdSev.c
@@ -64,6 +66,7 @@
   LocalApicLib
   MicrocodeLib
   MtrrLib
+  CpuPageTableLib
 
 [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbBase   ## CONSUMES
@@ -87,6 +90,7 @@
   gEdkiiS3SmmInitDoneGuid
   gEdkiiMicrocodePatchHobGuid
   gGhcbApicIdsGuid   ## SOMETIMES_CONSUMES
+  gEdkiiEndOfS3ResumeGuid
 
 [Guids.LoongArch64]
   gProcessorResourceHobGuid  ## SOMETIMES_CONSUMES  ## HOB
diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c 
b/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
index 4d3acb491f..16a858d542 100644
--- a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
@@ -1,7 +1,7 @@
 /** @file
   MP initialize support functions for PEI phase.
 
-  Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.
+  Copyright (c) 2016 - 2024, Intel Corporation. All rights reserved.
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -9,6 +9,7 @@
 #include "MpLib.h"
 #include 
 #include 
+#include 
 #include 
 
 STATIC UINT64  mSevEsPeiWakeupBuffer = BASE_1MB;
@@ -449,6 +450,47 @@ BuildMicrocodeCacheHob (
   return;
 }
 
+/**
+  S3 SMM Init Done notification function.
+
+  @param  PeiServices  Indirect reference to the PEI Services Table.
+  @param  NotifyDesc   Address of the notification descriptor data 
structure.
+  @param  InvokePpiAddress of the PPI that was invoked.
+
+  @retval EFI_SUCCESS  The function completes successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+NotifyOnEndOfS3Resume (
+  IN  EFI_PEI_SERVICES   **PeiServices,
+  IN  EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDesc,
+  IN  VOID   *InvokePpi
+  )
+{
+  CPU_MP_DATA  *CpuMpData;
+
+  CpuMpData   = GetCpuMpData ();
+  mNumberToFinish = CpuMpData->CpuCount - 1;
+  WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);
+  while (mNumberToFinish > 0) {
+CpuPause ();
+  }
+
+  DEBUG ((DEBUG_INFO, "%a() done!\n", __func__));
+
+  return EFI_SUCCESS;
+}
+
+//
+// Global function
+//
+EFI_PEI_NOTIFY_DESCRIPTOR  mEndOfS3ResumeNotifyDesc = {
+  EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | 
EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
+  ,
+  NotifyOnEndOfS3Resume
+};
+
 /**
   Initialize global data for MP support.
 
@@ -463,12 +505,16 @@ InitMpGlobalData (
 
   BuildMicrocodeCacheHob (CpuMpData);
   SaveCpuMpData (CpuMpData);
+  PrepareApLoopCode (CpuMpData);
 
   ///
   /// Install Notify
   ///
   Status = PeiServicesNotifyPpi ();
   ASSERT_EFI_ERROR (Status);
+
+  Status = PeiServicesNotifyPpi ();
+  ASSERT_EFI_ERROR (Status);
 }
 
 /**
@@ -815,3 +861,109 @@ PlatformShadowMicrocode (
 
   return EFI_SUCCESS;
 }
+
+/**
+  Allocate buffer for ApLoopCode.
+
+  @param[in]  PagesNumber 

[edk2-devel] [Patch V2 12/18] UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume

2024-05-17 Thread duntan
Install gEdkiiEndOfS3ResumeGuid in S3Resume to trigger
callback registered by PeiMpLib. The callback is to
relocate Ap to new safe memory before jump to OS waking
vector in S3 boot flow.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c | 13 +
 1 file changed, 13 insertions(+)

diff --git a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c 
b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
index 078ae2d72d..e6dfa09c71 100644
--- a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
+++ b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
@@ -260,6 +260,12 @@ EFI_PEI_PPI_DESCRIPTOR  mPpiListS3SmmInitDoneTable = {
   0
 };
 
+EFI_PEI_PPI_DESCRIPTOR  mPpiListEndOfS3ResumeTable = {
+  (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
+  ,
+  0
+};
+
 //
 // Global Descriptor Table (GDT)
 //
@@ -490,6 +496,13 @@ S3ResumeBootOs (
   PERF_INMODULE_BEGIN ("EndOfS3Resume");
 
   DEBUG ((DEBUG_INFO, "Signal EndOfS3Resume\n"));
+
+  //
+  // Install EndOfS3Resume.
+  //
+  Status = PeiServicesInstallPpi ();
+  ASSERT_EFI_ERROR (Status);
+
   //
   // Signal EndOfS3Resume to SMM.
   //
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119009): https://edk2.groups.io/g/devel/message/119009
Mute This Topic: https://groups.io/mt/106150771/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 11/18] UefiCpuPkg:Move some code in DxeMpLib to common place

2024-05-17 Thread duntan
Move some code in DxeMpLib.C to common MpLib.c.
The related code is to relocate Ap to new safe buffer
before booting into OS. In next commits, these code
also will be used by PeiMpLib. This commit doesn't
change any code functionality.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 151 
+--
 UefiCpuPkg/Library/MpInitLib/MpLib.c| 144 
+++-
 UefiCpuPkg/Library/MpInitLib/MpLib.h|  53 
+++--
 3 files changed, 199 insertions(+), 149 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c 
b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
index 5f0a87c024..f9c5c92c22 100644
--- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
@@ -20,15 +20,11 @@
 
 #define  AP_SAFE_STACK_SIZE  128
 
-CPU_MP_DATA *mCpuMpData  = NULL;
-EFI_EVENT   mCheckAllApsEvent= NULL;
-EFI_EVENT   mMpInitExitBootServicesEvent = NULL;
-EFI_EVENT   mLegacyBootEvent = NULL;
-volatile BOOLEANmStopCheckAllApsStatus   = TRUE;
-RELOCATE_AP_LOOP_ENTRY  mReservedApLoop;
-UINTN   mReservedTopOfApStack;
-volatile UINT32 mNumberToFinish = 0;
-UINTN   mApPageTable;
+CPU_MP_DATA   *mCpuMpData  = NULL;
+EFI_EVENT mCheckAllApsEvent= NULL;
+EFI_EVENT mMpInitExitBootServicesEvent = NULL;
+EFI_EVENT mLegacyBootEvent = NULL;
+volatile BOOLEAN  mStopCheckAllApsStatus   = TRUE;
 
 //
 // Begin wakeup buffer allocation below 0x88000
@@ -368,60 +364,6 @@ GetProtectedModeCS (
   return Index * 8;
 }
 
-/**
-  Do sync on APs.
-
-  @param[in, out] Buffer  Pointer to private data buffer.
-**/
-VOID
-EFIAPI
-RelocateApLoop (
-  IN OUT VOID  *Buffer
-  )
-{
-  CPU_MP_DATA  *CpuMpData;
-  BOOLEAN  MwaitSupport;
-  UINTNProcessorNumber;
-  UINTNStackStart;
-
-  MpInitLibWhoAmI ();
-  CpuMpData= GetCpuMpData ();
-  MwaitSupport = IsMwaitSupport ();
-  if (CpuMpData->UseSevEsAPMethod) {
-//
-// 64-bit AMD processors with SEV-ES
-//
-StackStart = CpuMpData->SevEsAPResetStackStart;
-mReservedApLoop.AmdSevEntry (
-  MwaitSupport,
-  CpuMpData->ApTargetCState,
-  CpuMpData->PmCodeSegment,
-  StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,
-  (UINTN),
-  CpuMpData->Pm16CodeSegment,
-  CpuMpData->SevEsAPBuffer,
-  CpuMpData->WakeupBuffer
-  );
-  } else {
-//
-// Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit 
AMD processors without SEV-ES
-//
-StackStart = mReservedTopOfApStack;
-mReservedApLoop.GenericEntry (
-  MwaitSupport,
-  CpuMpData->ApTargetCState,
-  StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,
-  (UINTN),
-  mApPageTable
-  );
-  }
-
-  //
-  // It should never reach here
-  //
-  ASSERT (FALSE);
-}
-
 /**
   Allocate buffer for ApLoopCode.
 
@@ -477,89 +419,6 @@ RemoveNxprotection (
   }
 }
 
-/**
-  Prepare ApLoopCode.
-
-  @param[in] CpuMpData  Pointer to CpuMpData.
-**/
-VOID
-PrepareApLoopCode (
-  IN CPU_MP_DATA  *CpuMpData
-  )
-{
-  EFI_PHYSICAL_ADDRESS Address;
-  MP_ASSEMBLY_ADDRESS_MAP  *AddressMap;
-  UINT8*ApLoopFunc;
-  UINTNApLoopFuncSize;
-  UINTNStackPages;
-  UINTNFuncPages;
-  IA32_CR0 Cr0;
-
-  AddressMap = >AddressMap;
-  if (CpuMpData->UseSevEsAPMethod) {
-//
-// 64-bit AMD processors with SEV-ES
-//
-Address= BASE_4GB - 1;
-ApLoopFunc = AddressMap->RelocateApLoopFuncAddressAmdSev;
-ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeAmdSev;
-  } else {
-//
-// Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit 
AMD processors without SEV-ES
-//
-Address= MAX_ADDRESS;
-ApLoopFunc = AddressMap->RelocateApLoopFuncAddressGeneric;
-ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeGeneric;
-  }
-
-  //
-  // Avoid APs access invalid buffer data which allocated by BootServices,
-  // so we will allocate reserved data for AP loop code. We also need to
-  // allocate this buffer below 4GB due to APs may be transferred to 32bit
-  // protected mode on long mode DXE.
-  // Allocating it in advance 

[edk2-devel] [Patch V2 10/18] UefiCpuPkg:Abstract some DxeMpLib code to function

2024-05-17 Thread duntan
Abstract some DxeMpLib code to function in this commit.
Some of these internal functions will be moved to common
MpLib.c in following commits. Then PeiMpLib can reuse
the code.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 222 
--
 1 file changed, 140 insertions(+), 82 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c 
b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
index 57ddb86600..5f0a87c024 100644
--- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
@@ -1,7 +1,7 @@
 /** @file
   MP initialize support functions for DXE phase.
 
-  Copyright (c) 2016 - 2023, Intel Corporation. All rights reserved.
+  Copyright (c) 2016 - 2024, Intel Corporation. All rights reserved.
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -422,6 +422,144 @@ RelocateApLoop (
   ASSERT (FALSE);
 }
 
+/**
+  Allocate buffer for ApLoopCode.
+
+  @param[in]  PagesNumber of pages to allocate.
+  @param[in, out] Address  Pointer to the allocated buffer.
+**/
+VOID
+AllocateApLoopCodeBuffer (
+  IN UINTN Pages,
+  IN OUT EFI_PHYSICAL_ADDRESS  *Address
+  )
+{
+  EFI_STATUS  Status;
+
+  Status = gBS->AllocatePages (
+  AllocateMaxAddress,
+  EfiReservedMemoryType,
+  Pages,
+  Address
+  );
+  ASSERT_EFI_ERROR (Status);
+}
+
+/**
+  Remove Nx protection for the range specific by BaseAddress and Length.
+
+  The PEI implementation uses CpuPageTableLib to change the attribute.
+  The DXE implementation uses gDS to change the attribute.
+
+  @param[in] BaseAddress  BaseAddress of the range.
+  @param[in] Length   Length of the range.
+**/
+VOID
+RemoveNxprotection (
+  IN EFI_PHYSICAL_ADDRESS  BaseAddress,
+  IN UINTN Length
+  )
+{
+  EFI_STATUS   Status;
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR  MemDesc;
+
+  //
+  // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD
+  //   service.
+  //
+  Status = gDS->GetMemorySpaceDescriptor (BaseAddress, );
+  if (!EFI_ERROR (Status)) {
+gDS->SetMemorySpaceAttributes (
+   BaseAddress,
+   Length,
+   MemDesc.Attributes & (~EFI_MEMORY_XP)
+   );
+  }
+}
+
+/**
+  Prepare ApLoopCode.
+
+  @param[in] CpuMpData  Pointer to CpuMpData.
+**/
+VOID
+PrepareApLoopCode (
+  IN CPU_MP_DATA  *CpuMpData
+  )
+{
+  EFI_PHYSICAL_ADDRESS Address;
+  MP_ASSEMBLY_ADDRESS_MAP  *AddressMap;
+  UINT8*ApLoopFunc;
+  UINTNApLoopFuncSize;
+  UINTNStackPages;
+  UINTNFuncPages;
+  IA32_CR0 Cr0;
+
+  AddressMap = >AddressMap;
+  if (CpuMpData->UseSevEsAPMethod) {
+//
+// 64-bit AMD processors with SEV-ES
+//
+Address= BASE_4GB - 1;
+ApLoopFunc = AddressMap->RelocateApLoopFuncAddressAmdSev;
+ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeAmdSev;
+  } else {
+//
+// Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit 
AMD processors without SEV-ES
+//
+Address= MAX_ADDRESS;
+ApLoopFunc = AddressMap->RelocateApLoopFuncAddressGeneric;
+ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeGeneric;
+  }
+
+  //
+  // Avoid APs access invalid buffer data which allocated by BootServices,
+  // so we will allocate reserved data for AP loop code. We also need to
+  // allocate this buffer below 4GB due to APs may be transferred to 32bit
+  // protected mode on long mode DXE.
+  // Allocating it in advance since memory services are not available in
+  // Exit Boot Services callback function.
+  //
+  // ++ (TopOfApStack)
+  // |  Stack * N |
+  // ++ (stack base, 4k aligned)
+  // |  Padding   |
+  // ++
+  // |  Ap Loop   |
+  // ++ ((low address, 4k-aligned)
+  //
+
+  StackPages = EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_STACK_SIZE);
+  FuncPages  = EFI_SIZE_TO_PAGES (ApLoopFuncSize);
+
+  AllocateApLoopCodeBuffer (StackPages + FuncPages, );
+  ASSERT (Address != 0);
+
+  Cr0.UintN = AsmReadCr0 ();
+  if (Cr0.Bits.PG != 0) {
+//
+// Make sure that the buffer memory is executable if NX protection is 
enabled
+// for EfiReservedMemoryType.
+//
+RemoveNxprotection (Address, EFI_PAGES_TO_SIZE (FuncPages));
+  }
+
+  mReservedTopOfApStack = (UINTN)Address + EFI_PAGES_TO_SIZE 
(StackPages+FuncPages);
+  ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);
+  mReservedApLoop.Data = (VOID *)(UINTN)Address;
+  ASSERT (mReservedApLoop.Data != NULL);
+  CopyMem (mReservedApLoop.Data, 

[edk2-devel] [Patch V2 09/18] UefiCpuPkg: Disable PG in IA32 ApLoopCode

2024-05-17 Thread duntan
Disable paging in IA32 RelocateApLoop assembly
code to fix the issue that the AP page table is
unavailiable after boot OS under IA32 execution mode.

This issue exist in IA32 PEI + IA32 DXE normal boot
(also S3 boot with IA32 PEI after previous three commits
are accepted). In current MpLib code, the IA32 execution
mode code did not create page table in reserved memory
like what X64 code did. If PcdCpuStackGuard is TRUE, the
PG is enabled for AP in current RelocateApLoop assembly
code. And the page table for AP is unavailiable after
boot OS. This might cause potential issue. So disable PG
in IA32 RelocateApLoop.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm 
b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm
index d117f09ef5..c0fe631635 100644
--- a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm
+++ b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm
@@ -1,5 +1,5 @@
 
;-- 
;
-; Copyright (c) 2015 - 2023, Intel Corporation. All rights reserved.
+; Copyright (c) 2015 - 2024, Intel Corporation. All rights reserved.
 ; SPDX-License-Identifier: BSD-2-Clause-Patent
 ;
 ; Module Name:
@@ -225,6 +225,10 @@ RendezvousFunnelProcEnd:
 ;  specific to SEV-ES support and are not applicable on IA32.
 
;-
 AsmRelocateApLoopGenericStart:
+moveax, cr0
+btreax, 31 ; Clear CR0.PG
+movcr0, eax; Disable paging since the page table might 
be unavailiable
+
 moveax, esp
 movesp, [eax + 12] ; TopOfApStack
 push   dword [eax] ; push return address for stack trace
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119006): https://edk2.groups.io/g/devel/message/119006
Mute This Topic: https://groups.io/mt/106150767/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 08/18] UefiCpuPkg: Remove code to set register table

2024-05-17 Thread duntan
Remove code to set register table in CpuS3.c.
In previous commit, PcdCpuFeaturesInitOnS3Resume
has been set to TRUE. So that CpuFeaturesPei PEIM
will initialize the CPU registers and perform CPU
features initialization.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 423 
---
 1 file changed, 423 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index 75b8c0d3de..0bc4a3572d 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -91,425 +91,6 @@ UINT8  mApHltLoopCodeTemplate[] = {
   0xEB, 0xFC  // jmp $-2
 };
 
-/**
-  Increment semaphore by 1.
-
-  @param  SemIN:  32-bit unsigned integer
-
-**/
-VOID
-S3ReleaseSemaphore (
-  IN OUT  volatile UINT32  *Sem
-  )
-{
-  InterlockedIncrement (Sem);
-}
-
-/**
-  Decrement the semaphore by 1 if it is not zero.
-
-  Performs an atomic decrement operation for semaphore.
-  The compare exchange operation must be performed using
-  MP safe mechanisms.
-
-  @param  SemIN:  32-bit unsigned integer
-
-**/
-VOID
-S3WaitForSemaphore (
-  IN OUT  volatile UINT32  *Sem
-  )
-{
-  UINT32  Value;
-
-  do {
-Value = *Sem;
-  } while (Value == 0 ||
-   InterlockedCompareExchange32 (
- Sem,
- Value,
- Value - 1
- ) != Value);
-}
-
-/**
-  Read / write CR value.
-
-  @param[in]  CrIndex The CR index which need to read/write.
-  @param[in]  ReadRead or write. TRUE is read.
-  @param[in,out]  CrValue CR value.
-
-  @retvalEFI_SUCCESS means read/write success, else return EFI_UNSUPPORTED.
-**/
-UINTN
-ReadWriteCr (
-  IN UINT32   CrIndex,
-  IN BOOLEAN  Read,
-  IN OUT UINTN*CrValue
-  )
-{
-  switch (CrIndex) {
-case 0:
-  if (Read) {
-*CrValue = AsmReadCr0 ();
-  } else {
-AsmWriteCr0 (*CrValue);
-  }
-
-  break;
-case 2:
-  if (Read) {
-*CrValue = AsmReadCr2 ();
-  } else {
-AsmWriteCr2 (*CrValue);
-  }
-
-  break;
-case 3:
-  if (Read) {
-*CrValue = AsmReadCr3 ();
-  } else {
-AsmWriteCr3 (*CrValue);
-  }
-
-  break;
-case 4:
-  if (Read) {
-*CrValue = AsmReadCr4 ();
-  } else {
-AsmWriteCr4 (*CrValue);
-  }
-
-  break;
-default:
-  return EFI_UNSUPPORTED;
-  }
-
-  return EFI_SUCCESS;
-}
-
-/**
-  Initialize the CPU registers from a register table.
-
-  @param[in]  RegisterTable The register table for this AP.
-  @param[in]  ApLocationAP location info for this ap.
-  @param[in]  CpuStatus CPU status info for this CPU.
-  @param[in]  CpuFlags  Flags data structure used when program the 
register.
-
-  @note This service could be called by BSP/APs.
-**/
-VOID
-ProgramProcessorRegister (
-  IN CPU_REGISTER_TABLE  *RegisterTable,
-  IN EFI_CPU_PHYSICAL_LOCATION   *ApLocation,
-  IN CPU_STATUS_INFORMATION  *CpuStatus,
-  IN PROGRAM_CPU_REGISTER_FLAGS  *CpuFlags
-  )
-{
-  CPU_REGISTER_TABLE_ENTRY  *RegisterTableEntry;
-  UINTN Index;
-  UINTN Value;
-  CPU_REGISTER_TABLE_ENTRY  *RegisterTableEntryHead;
-  volatile UINT32   *SemaphorePtr;
-  UINT32FirstThread;
-  UINT32CurrentThread;
-  UINT32CurrentCore;
-  UINTN ProcessorIndex;
-  UINT32*ThreadCountPerPackage;
-  UINT8 *ThreadCountPerCore;
-  EFI_STATUSStatus;
-  UINT64CurrentValue;
-
-  //
-  // Traverse Register Table of this logical processor
-  //
-  RegisterTableEntryHead = (CPU_REGISTER_TABLE_ENTRY 
*)(UINTN)RegisterTable->RegisterTableEntry;
-
-  for (Index = 0; Index < RegisterTable->TableLength; Index++) {
-RegisterTableEntry = [Index];
-
-//
-// Check the type of specified register
-//
-switch (RegisterTableEntry->RegisterType) {
-  //
-  // The specified register is Control Register
-  //
-  case ControlRegister:
-Status = ReadWriteCr (RegisterTableEntry->Index, TRUE, );
-if (EFI_ERROR (Status)) {
-  break;
-}
-
-if (RegisterTableEntry->TestThenWrite) {
-  CurrentValue = BitFieldRead64 (
-   Value,
-   RegisterTableEntry->ValidBitStart,
-   

[edk2-devel] [Patch V2 07/18] UefiCpuPkg:Set PcdCpuFeaturesInitOnS3Resume to TRUE

2024-05-17 Thread duntan
Set PcdCpuFeaturesInitOnS3Resume to TRUE. So that
CpuFeaturesPei PEIM will initialize the CPU registers
and perform CPU features initialization.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/UefiCpuPkg.dec | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec
index f86a6d2bcb..73bae90ddb 100644
--- a/UefiCpuPkg/UefiCpuPkg.dec
+++ b/UefiCpuPkg/UefiCpuPkg.dec
@@ -1,7 +1,7 @@
 ## @file  UefiCpuPkg.dec
 # This Package provides UEFI compatible CPU modules and libraries.
 #
-# Copyright (c) 2007 - 2023, Intel Corporation. All rights reserved.
+# Copyright (c) 2007 - 2024, Intel Corporation. All rights reserved.
 # Copyright (C) 2023 - 2024, Advanced Micro Devices, Inc. All rights 
reserved.
 # Copyright (c) 2024, Loongson Technology Corporation Limited. All rights 
reserved.
 #
@@ -310,7 +310,7 @@
 
   ## Specifies if CPU features will be initialized during S3 resume.
   # @Prompt If CPU features will be initialized during S3 resume.
-  
gUefiCpuPkgTokenSpaceGuid.PcdCpuFeaturesInitOnS3Resume|FALSE|BOOLEAN|0x001D
+  
gUefiCpuPkgTokenSpaceGuid.PcdCpuFeaturesInitOnS3Resume|TRUE|BOOLEAN|0x001D
 
   ## Specifies CPUID Leaf 0x15 Time Stamp Counter and Nominal Core Crystal 
Clock Frequency.
   # TSC Frequency = ECX (core crystal clock frequency) * EBX/EAX.
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119004): https://edk2.groups.io/g/devel/message/119004
Mute This Topic: https://groups.io/mt/106150765/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 06/18] UefiCpuPkg: Remove code to load mtrr setting

2024-05-17 Thread duntan
Remove code to load mtrr setting in CpuS3.c.
In previous commits, before transferring to
CpuS3.c, MTRR setting has been loaded in
S3RestoreConfig2() for all CPU.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 34 +-
 1 file changed, 1 insertion(+), 33 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index d67fb49890..75b8c0d3de 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -1,7 +1,7 @@
 /** @file
 Code for Processor S3 restoration
 
-Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.
+Copyright (c) 2006 - 2024, Intel Corporation. All rights reserved.
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -91,36 +91,6 @@ UINT8  mApHltLoopCodeTemplate[] = {
   0xEB, 0xFC  // jmp $-2
 };
 
-/**
-  Sync up the MTRR values for all processors.
-
-  @param MtrrTable  Table holding fixed/variable MTRR values to be loaded.
-**/
-VOID
-EFIAPI
-LoadMtrrData (
-  EFI_PHYSICAL_ADDRESS  MtrrTable
-  )
-
-/*++
-
-Routine Description:
-
-  Sync up the MTRR values for all processors.
-
-Arguments:
-
-Returns:
-None
-
---*/
-{
-  MTRR_SETTINGS  *MtrrSettings;
-
-  MtrrSettings = (MTRR_SETTINGS *)(UINTN)MtrrTable;
-  MtrrSetAllMtrrs (MtrrSettings);
-}
-
 /**
   Increment semaphore by 1.
 
@@ -554,8 +524,6 @@ InitializeCpuBeforeRebase (
   IN BOOLEAN  IsBsp
   )
 {
-  LoadMtrrData (mAcpiCpuData.MtrrTable);
-
   SetRegister (TRUE);
 
   ProgramVirtualWireMode ();
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119003): https://edk2.groups.io/g/devel/message/119003
Mute This Topic: https://groups.io/mt/106150764/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 04/18] UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe

2024-05-17 Thread duntan
Save MTRR by lockbox in CpuS3DataDxe. In S3 boot,
The MTRR setting will be restored in S3Resume.c
in following patches. Then S3Resume.c will wakeup
all APs to load the MTRR setting. This can avoid
waking up APs in CpuS3.c.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c  | 13 -
 UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf |  4 +++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c 
b/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
index 86ce5303ca..c87f82bc22 100644
--- a/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
+++ b/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
@@ -9,7 +9,7 @@ number of CPUs reported by the MP Services Protocol, so this 
module does not
 support hot plug CPUs.  This module can be copied into a CPU specific package
 and customized if these additional features are required.
 
-Copyright (c) 2013 - 2021, Intel Corporation. All rights reserved.
+Copyright (c) 2013 - 2024, Intel Corporation. All rights reserved.
 Copyright (c) 2015, Red Hat, Inc.
 
 SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -26,6 +26,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -130,6 +131,16 @@ CpuS3DataOnEndOfDxe (
   DEBUG ((DEBUG_VERBOSE, "%a\n", __func__));
   MtrrGetAllMtrrs (>MtrrTable);
 
+  //
+  // Save MTRR in lockbox
+  //
+  Status = SaveLockBox (
+ ,
+ >MtrrTable,
+ sizeof (MTRR_SETTINGS)
+ );
+  ASSERT_EFI_ERROR (Status);
+
   //
   // Close event, so it will not be invoked again.
   //
diff --git a/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf 
b/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
index 510133a614..c690f7df5b 100644
--- a/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
+++ b/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
@@ -9,7 +9,7 @@
 #  support hot plug CPUs.  This module can be copied into a CPU specific 
package
 #  and customized if these additional features are required.
 #
-#  Copyright (c) 2013-2016, Intel Corporation. All rights reserved.
+#  Copyright (c) 2013-2024, Intel Corporation. All rights reserved.
 #  Copyright (c) 2015, Red Hat, Inc.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -46,9 +46,11 @@
   BaseLib
   MtrrLib
   MemoryAllocationLib
+  LockBoxLib
 
 [Guids]
   gEfiEndOfDxeEventGroupGuid ## CONSUMES   ## Event
+  gEdkiiS3MtrrSettingGuid
 
 [Protocols]
   gEfiMpServiceProtocolGuid  ## CONSUMES
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119001): https://edk2.groups.io/g/devel/message/119001
Mute This Topic: https://groups.io/mt/106150762/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 05/18] UefiCpuPkg: LoadMtrrData for all cpu in S3Resume

2024-05-17 Thread duntan
In this commit, S3Resume.c wakeup all Aps to run
LoadMtrrData for all cpu before transfer to CpuS3.c
in smm cpu driver. The MtrrSetting table can be restored
by gEdkiiS3MtrrSettingGuid which is saved by lockbox in
PEI phase. This can avoid waking up APs in CpuS3.c.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c   | 51 
++-
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf |  4 +++-
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c 
b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
index 4cf676fb3e..078ae2d72d 100644
--- a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
+++ b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
@@ -4,7 +4,7 @@
   This module will execute the boot script saved during last boot and after 
that,
   control is passed to OS waking up handler.
 
-  Copyright (c) 2006 - 2023, Intel Corporation. All rights reserved.
+  Copyright (c) 2006 - 2024, Intel Corporation. All rights reserved.
   Copyright (c) 2017, AMD Incorporated. All rights reserved.
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -39,6 +39,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -938,6 +939,20 @@ S3ResumeExecuteBootScript (
   CpuDeadLoop ();
 }
 
+/**
+  Sync up the MTRR values for all processors.
+
+  @param[in] MtrrTable  Address of MTRR setting.
+**/
+VOID
+EFIAPI
+LoadMtrrData (
+  IN VOID  *MtrrTable
+  )
+{
+  MtrrSetAllMtrrs (MtrrTable);
+}
+
 /**
   Restores the platform to its preboot configuration for an S3 resume and
   jumps to the OS waking vector.
@@ -990,6 +1005,7 @@ S3RestoreConfig2 (
   BOOLEANInterruptStatus;
   IA32_CR0   Cr0;
   EDKII_PEI_MP_SERVICES2_PPI *MpService2Ppi;
+  MTRR_SETTINGS  MtrrTable;
 
   TempAcpiS3Context = 0;
   TempEfiBootScriptExecutorVariable = 0;
@@ -1082,6 +1098,39 @@ S3RestoreConfig2 (
   Status = SmmAccess->Open ((EFI_PEI_SERVICES 
**)GetPeiServicesTablePointer (), SmmAccess, Index);
 }
 
+//
+// Get MP Services2 Ppi to pass it to Smm S3.
+//
+Status = PeiServicesLocatePpi (
+   ,
+   0,
+   NULL,
+   (VOID **)
+   );
+ASSERT_EFI_ERROR (Status);
+
+//
+// Restore MTRR setting
+//
+VarSize = sizeof (MTRR_SETTINGS);
+Status  = RestoreLockBox (
+,
+,
+
+);
+ASSERT_EFI_ERROR (Status);
+
+//
+// Sync up the MTRR values for all processors.
+//
+Status = MpService2Ppi->StartupAllCPUs (
+  MpService2Ppi,
+  (EFI_AP_PROCEDURE)LoadMtrrData,
+  0,
+  (VOID *)
+  );
+ASSERT_EFI_ERROR (Status);
+
 SmramDescriptor  = (EFI_SMRAM_DESCRIPTOR *)GET_GUID_HOB_DATA (GuidHob);
 SmmS3ResumeState = (SMM_S3_RESUME_STATE *)(UINTN)SmramDescriptor->CpuStart;
 
diff --git a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf 
b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf
index 9c9b6f3db3..54de8bc91f 100644
--- a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf
+++ b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf
@@ -5,7 +5,7 @@
 # This module will excute the boot script saved during last boot and after 
that,
 # control is passed to OS waking up handler.
 #
-# Copyright (c) 2010 - 2023, Intel Corporation. All rights reserved.
+# Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.
 # Copyright (c) 2017, AMD Incorporated. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -67,6 +67,7 @@
   LocalApicLib
   ReportStatusCodeLib
   LockBoxLib
+  MtrrLib
 
 [Guids]
   gEfiBootScriptExecutorVariableGuid## SOMETIMES_CONSUMES ## 
UNDEFINED # LockBox
@@ -79,6 +80,7 @@
   ## SOMETIMES_PRODUCES ## UNDEFINED # Install PPI
   ## SOMETIMES_CONSUMES ## UNDEFINED # Used to do smm communication
   gEdkiiS3SmmInitDoneGuid
+  gEdkiiS3MtrrSettingGuid
 
 [Ppis]
   gEfiPeiS3Resume2PpiGuid   ## PRODUCES
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119002): https://edk2.groups.io/g/devel/message/119002
Mute This Topic: https://groups.io/mt/106150763/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 03/18] UefiCpuPkg: Add locbox lib instance in DSC

2024-05-17 Thread duntan
Add locbox lib instance in DSC. The SmmLockBoxDxeLib
will be consumed by CpuS3DataDxe driver

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/UefiCpuPkg.dsc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc
index 3d49f72588..1b52760e1a 100644
--- a/UefiCpuPkg/UefiCpuPkg.dsc
+++ b/UefiCpuPkg/UefiCpuPkg.dsc
@@ -1,7 +1,7 @@
 ## @file
 #  UefiCpuPkg Package
 #
-#  Copyright (c) 2007 - 2023, Intel Corporation. All rights reserved.
+#  Copyright (c) 2007 - 2024, Intel Corporation. All rights reserved.
 #  Copyright (C) 2023 - 2024, Advanced Micro Devices, Inc. All rights 
reserved.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -68,6 +68,7 @@
   UnitTestLib|UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLib.inf
   
UnitTestPersistenceLib|UnitTestFrameworkPkg/Library/UnitTestPersistenceLibNull/UnitTestPersistenceLibNull.inf
   
UnitTestResultReportLib|UnitTestFrameworkPkg/Library/UnitTestResultReportLib/UnitTestResultReportLibDebugLib.inf
+  LockBoxLib|MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf
 
 [LibraryClasses.common.SEC]
   PlatformSecLib|UefiCpuPkg/Library/PlatformSecLibNull/PlatformSecLibNull.inf
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#119000): https://edk2.groups.io/g/devel/message/119000
Mute This Topic: https://groups.io/mt/106150761/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 02/18] OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe

2024-05-17 Thread duntan
Save MTRR by lockbox in CpuS3DataDxe. In S3 boot,
The MTRR setting will be restored in S3Resume.c
in following patches. Then S3Resume.c will wakeup
all APs to load the MTRR setting. This can avoid
waking up APs in CpuS3.c.

Signed-off-by: Dun Tan 
Cc: Ard Biesheuvel 
Cc: Jiewen Yao 
Cc: Gerd Hoffmann 
Reviewed-by: Ray Ni 
Cc: Jiaxin Wu 
---
 OvmfPkg/CpuS3DataDxe/CpuS3Data.c  | 13 -
 OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf |  4 +++-
 2 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/OvmfPkg/CpuS3DataDxe/CpuS3Data.c b/OvmfPkg/CpuS3DataDxe/CpuS3Data.c
index 289048b75d..eacdfa12c3 100644
--- a/OvmfPkg/CpuS3DataDxe/CpuS3Data.c
+++ b/OvmfPkg/CpuS3DataDxe/CpuS3Data.c
@@ -9,7 +9,7 @@ number of CPUs reported by the MP Services Protocol, so this 
module does not
 support hot plug CPUs.  This module can be copied into a CPU specific package
 and customized if these additional features are required.
 
-Copyright (c) 2013 - 2021, Intel Corporation. All rights reserved.
+Copyright (c) 2013 - 2024, Intel Corporation. All rights reserved.
 Copyright (c) 2015 - 2020, Red Hat, Inc.
 
 SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -26,6 +26,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -130,6 +131,16 @@ CpuS3DataOnEndOfDxe (
   DEBUG ((DEBUG_VERBOSE, "%a\n", __func__));
   MtrrGetAllMtrrs (>MtrrTable);
 
+  //
+  // Save MTRR in lockbox
+  //
+  Status = SaveLockBox (
+ ,
+ >MtrrTable,
+ sizeof (MTRR_SETTINGS)
+ );
+  ASSERT_EFI_ERROR (Status);
+
   //
   // Close event, so it will not be invoked again.
   //
diff --git a/OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf 
b/OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf
index 228d5ae1b2..5369613f5d 100644
--- a/OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf
+++ b/OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf
@@ -9,7 +9,7 @@
 #  support hot plug CPUs.  This module can be copied into a CPU specific 
package
 #  and customized if these additional features are required.
 #
-#  Copyright (c) 2013-2016, Intel Corporation. All rights reserved.
+#  Copyright (c) 2013-2024, Intel Corporation. All rights reserved.
 #  Copyright (c) 2015-2020, Red Hat, Inc.
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
@@ -46,9 +46,11 @@
   MtrrLib
   UefiBootServicesTableLib
   UefiDriverEntryPoint
+  LockBoxLib
 
 [Guids]
   gEfiEndOfDxeEventGroupGuid ## CONSUMES   ## Event
+  gEdkiiS3MtrrSettingGuid
 
 [Protocols]
   gEfiMpServiceProtocolGuid  ## CONSUMES
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118999): https://edk2.groups.io/g/devel/message/118999
Mute This Topic: https://groups.io/mt/106150760/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 01/18] MdeModulePkg: Add gEdkiiS3MtrrSettingGuid

2024-05-17 Thread duntan
Add gEdkiiS3MtrrSettingGuid a new GUID for s3
MTRR setting. This GUID will be used to save
MTRR_SETTINGS at EndOfDxe by LockBox and restore
at S3 boot PEI phase for s3 usage.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/MdeModulePkg.dec | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec
index f7339f0aec..02f330a453 100644
--- a/MdeModulePkg/MdeModulePkg.dec
+++ b/MdeModulePkg/MdeModulePkg.dec
@@ -4,7 +4,7 @@
 # and libraries instances, which are used for those modules.
 #
 # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
-# Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.
+# Copyright (c) 2007 - 2024, Intel Corporation. All rights reserved.
 # Copyright (c) 2016, Linaro Ltd. All rights reserved.
 # (C) Copyright 2016 - 2019 Hewlett Packard Enterprise Development LP
 # Copyright (c) 2017, AMD Incorporated. All rights reserved.
@@ -465,6 +465,9 @@
   gEdk2JedecSfdpSpiDxeDriverGuid  = { 0xBE71701E, 0xB63C, 0x4574, { 0x9C, 
0x5C, 0x36, 0x29, 0xE8, 0xEA, 0xC4, 0x14 }}
   gEdk2JedecSfdpSpiSmmDriverGuid  = { 0x95A1E915, 0x195C, 0x477C, { 0x92, 
0x6F, 0x7E, 0x24, 0x67, 0xC1, 0xB3, 0x1F }}
 
+  ## This GUID will be used to save MTRR_SETTINGS at EndOfDxe by LockBox and 
restore at S3 boot PEI phase for s3 usage.
+  gEdkiiS3MtrrSettingGuid = { 0xd77baa84, 0xb332, 0x4463, { 0x9f, 0x1d, 0xce, 
0x81, 0x00, 0xfe, 0x7f, 0x35 }}
+
 [Ppis]
   ## Include/Ppi/FirmwareVolumeShadowPpi.h
   gEdkiiPeiFirmwareVolumeShadowPpiGuid = { 0x7dfe756c, 0xed8d, 0x4d77, {0x9e, 
0xc4, 0x39, 0x9a, 0x8a, 0x81, 0x51, 0x16 } }
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118998): https://edk2.groups.io/g/devel/message/118998
Mute This Topic: https://groups.io/mt/106150759/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 00/18] Remove some S3 related code in CpuS3.c of smm cpu driver

2024-05-17 Thread duntan
Comparing to V1 patchs set, the V2 patch set only adjusts the commits ordering 
and modifies copy right year in some files.
This patch set is to remove some S3 related code in CpuS3.c of smm cpu driver. 
It contain commits to:
1) S3 MTRRs operation:
>   MdeModulePkg: Add gEdkiiS3MtrrSettingGuid
>   OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe
>   UefiCpuPkg: Add locbox lib instance in DSC
>   UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe
>   UefiCpuPkg: LoadMtrrData for all cpu in S3Resume
>   UefiCpuPkg: Remove code to load mtrr setting

2) AP page table unavailiable issue fix:
>   UefiCpuPkg: Disable PG in IA32 ApLoopCode

3) Register table cleanup:
>   UefiCpuPkg:Set PcdCpuFeaturesInitOnS3Resume to TRUE
>   UefiCpuPkg: Remove code to set register table

4)  S3 ApHltLoopCode Operation:
>   UefiCpuPkg:Abstract some DxeMpLib code to function
>   UefiCpuPkg:Move some code in DxeMpLib to common place
>   UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume
>   UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib
>   UefiCpuPkg:Remove code to handle APIC setting and Interrupt
>   UefiCpuPkg:Rremove code to wakeup AP and relocate ap
>   UefiCpuPkg: Remove the duplicated mpservice locate
>   MdeModulePkg: remove MpService2Ppi field in SMM_S3_RESUME_STATE

5)  Remove code to get AcpiCpuData:
>   UefiCpuPkg: Remove GetAcpiCpuData() in CpuS3.c

With this patch set, CpuS3.c in smm CPU driver can be simplified.
The whole patch set has been reviewed-by Ray.
PR for review: https://github.com/tianocore/edk2/pull/5606

Cc: Liming Gao 
Cc: Jiaxin Wu 
Cc: Ray Ni 
Cc: Ard Biesheuvel 
Cc: Jiewen Yao 
Cc: Gerd Hoffmann 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 

Dun Tan (18):
  MdeModulePkg: Add gEdkiiS3MtrrSettingGuid
  OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe
  UefiCpuPkg: Add locbox lib instance in DSC
  UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe
  UefiCpuPkg: LoadMtrrData for all cpu in S3Resume
  UefiCpuPkg: Remove code to load mtrr setting
  UefiCpuPkg:Set PcdCpuFeaturesInitOnS3Resume to TRUE
  UefiCpuPkg: Remove code to set register table
  UefiCpuPkg: Disable PG in IA32 ApLoopCode
  UefiCpuPkg:Abstract some DxeMpLib code to function
  UefiCpuPkg:Move some code in DxeMpLib to common place
  UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume
  UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib
  UefiCpuPkg:Remove code to handle APIC setting and Interrupt
  UefiCpuPkg:Remove code to wakeup AP and relocate ap
  UefiCpuPkg: Remove unneeded MpService2Ppi assignment
  MdeModulePkg:Remove MpService2Ppi field in SMM_S3_RESUME_STATE
  UefiCpuPkg: Remove GetAcpiCpuData() in CpuS3.c

 MdeModulePkg/Include/Guid/AcpiS3Context.h   |   3 +--
 MdeModulePkg/MdeModulePkg.dec   |   5 -
 OvmfPkg/CpuS3DataDxe/CpuS3Data.c|  13 -
 OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf   |   4 +++-
 UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c |  13 -
 UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf|   4 +++-
 UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 183 
-
 UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm  |   6 +-
 UefiCpuPkg/Library/MpInitLib/MpLib.c| 144 
+++-
 UefiCpuPkg/Library/MpInitLib/MpLib.h|  56 
+++--
 UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf   |   6 +-
 UefiCpuPkg/Library/MpInitLib/PeiMpLib.c | 154 
+-
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c   | 993 
+--
 UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/MpFuncs.nasm | 153 
-
 UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmFuncsArch.c   |  29 
+---
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c  |   8 +++-
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h  |  26 
+
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf|   5 +
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/MpFuncs.nasm  | 189 
--
 

[edk2-devel] [PATCH] UefiCpuPkg:fix issue when splitting paging entry

2024-05-17 Thread duntan
This patch is to fix issue when splitting leaf paging
entry in CpuPageTableLib code.

In previous code, before we assign the new child paging
structure address to the content of splitted paging entry,
PageTableLibSetPnle() is called to make sure the bit7 is
set to 0, which indicate the previous leaf entry is
changed to non-leaf entry now. There is a gap between
we change the bit7 and we assign the new child paging
structure address to the content of the splitted paging
entry. If the address of code execution or data access
happens to be in the range covered by the splitted paging
entry, this gap may cause issue.

In this patch, we prepare the new paging entry content
value in a local variable and assign the value to the
splitted paging entry at once. The volatile keyword
is used to ensure that no optimization will occur in
compilation.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
Cc: Zhou Jianfeng 
---
 UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c | 9 +++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c 
b/UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c
index b10a3008e4..bdc411338f 100644
--- a/UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c
+++ b/UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c
@@ -342,6 +342,7 @@ PageTableLibMapInLevel (
   UINT64  PhysicalAddrInAttr;
   IA32_PAGING_ENTRY   OriginalParentPagingEntry;
   IA32_PAGING_ENTRY   OriginalCurrentPagingEntry;
+  IA32_PAGING_ENTRY   TempPagingEntry;
 
   ASSERT (Level != 0);
   ASSERT ((Attribute != NULL) && (Mask != NULL));
@@ -359,6 +360,8 @@ PageTableLibMapInLevel (
 
   OriginalParentPagingEntry.Uint64 = ParentPagingEntry->Uint64;
   OneOfPagingEntry.Uint64  = 0;
+  TempPagingEntry.Uint64   = 0;
+
   //
   // RegionLength: 256T (1 << 48) 512G (1 << 39), 1G (1 << 30), 2M (1 << 21) 
or 4K (1 << 12).
   //
@@ -441,8 +444,10 @@ PageTableLibMapInLevel (
   // Non-leaf entry doesn't have PAT bit. So use 
~IA32_PE_BASE_ADDRESS_MASK_40 is to make sure PAT bit
   // (bit12) in original big-leaf entry is not assigned to 
PageTableBaseAddress field of non-leaf entry.
   //
-  PageTableLibSetPnle (>Pnle, , 
);
-  ParentPagingEntry->Uint64 = ((UINTN)(VOID *)PagingEntry) | 
(ParentPagingEntry->Uint64 & (~IA32_PE_BASE_ADDRESS_MASK_40));
+  TempPagingEntry.Uint64 = ParentPagingEntry->Uint64;
+  PageTableLibSetPnle (, , );
+  TempPagingEntry.Uint64   = ((UINTN)(VOID 
*)PagingEntry) | (TempPagingEntry.Uint64 & (~IA32_PE_BASE_ADDRESS_MASK_40));
+  *(volatile UINT64 *)&(ParentPagingEntry->Uint64) = 
TempPagingEntry.Uint64;
 }
   } else {
 //
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118996): https://edk2.groups.io/g/devel/message/118996
Mute This Topic: https://groups.io/mt/106150750/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH 18/18] MdeModulePkg:Remove MpService2Ppi field in SMM_S3_RESUME_STATE

2024-05-12 Thread duntan
Thanks for the comments.
How about change the commits order to be like:


1) S3 MTRRs operation:

2) AP page table unavailiable issue fix:

3)  S3 ApHltLoopCode Operation:

>   UefiCpuPkg:Abstract some DxeMpLib code to function

>   UefiCpuPkg:Move some code in DxeMpLib to common place

>   UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume

>   UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib

>   UefiCpuPkg:Remove code to handle APIC setting and Interrupt

>   UefiCpuPkg:Rremove code to wakeup AP and relocate ap

>   UefiCpuPkg: Remove the duplicated mpservice locate

>   MdeModulePkg: remove MpService2Ppi field in SMM_S3_RESUME_STATE

4) Register table cleanup:

Then the patch to remove the assignment for MpService2Ppi will be after the 
patch to remove the MpService2Ppi field consumption.
Also I'll change the title of "UefiCpuPkg: Remove the duplicated mpservice 
locate" to "UefiCpuPkg: Remove unneeded MpService2Ppi assignment" to make it 
look clearer.

Thanks,
Dun

From: Ni, Ray 
Sent: Monday, May 13, 2024 10:36 AM
To: Tan, Dun ; devel@edk2.groups.io
Cc: Wang, Jian J ; Liming Gao 
Subject: Re: [PATCH 18/18] MdeModulePkg:Remove MpService2Ppi field in 
SMM_S3_RESUME_STATE

My comment to patch #6 suggested to keep the assignment to 
SmmS3ResumeState->MpService2Ppi.
How about you keep the "SmmS3ResumeState->MpService2Ppi = 0" there and remove 
that assignment and the field together in this patch?

Thanks,
Ray


From: Tan, Dun mailto:dun@intel.com>>
Sent: Friday, May 10, 2024 18:08
To: devel@edk2.groups.io 
mailto:devel@edk2.groups.io>>
Cc: Ni, Ray mailto:ray...@intel.com>>; Wang, Jian J 
mailto:jian.j.w...@intel.com>>; Liming Gao 
mailto:gaolim...@byosoft.com.cn>>
Subject: [PATCH 18/18] MdeModulePkg:Remove MpService2Ppi field in 
SMM_S3_RESUME_STATE

This MpService2Ppi field in SMM_S3_RESUME_STATE is used to
wakeup AP to do the CPU initialization during smm s3 boot when
the execution mode of PEI and DXE are the same.
Currently, in CpuS3.c of smm cpu driver, BSP doesn't need to
wakeup AP anymore. The initialization for AP will be done in
S3Resume.c before transfer to CpuS3.c of smm cpu driver.
So we can remove the MpService2Ppi field in SMM_S3_RESUME_STATE.

Signed-off-by: Dun Tan mailto:dun@intel.com>>
Cc: Ray Ni mailto:ray...@intel.com>>
Cc: Jian J Wang mailto:jian.j.w...@intel.com>>
Cc: Liming Gao mailto:gaolim...@byosoft.com.cn>>
---
 MdeModulePkg/Include/Guid/AcpiS3Context.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/MdeModulePkg/Include/Guid/AcpiS3Context.h 
b/MdeModulePkg/Include/Guid/AcpiS3Context.h
index 72d173c4fd..0cfc81b49b 100644
--- a/MdeModulePkg/Include/Guid/AcpiS3Context.h
+++ b/MdeModulePkg/Include/Guid/AcpiS3Context.h
@@ -30,7 +30,6 @@ typedef struct {
   EFI_PHYSICAL_ADDRESSReturnContext1;
   EFI_PHYSICAL_ADDRESSReturnContext2;
   EFI_PHYSICAL_ADDRESSReturnStackPointer;
-  EFI_PHYSICAL_ADDRESSMpService2Ppi;
   EFI_PHYSICAL_ADDRESSSmst;
 } SMM_S3_RESUME_STATE;

--
2.31.1.windows.1


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118854): https://edk2.groups.io/g/devel/message/118854
Mute This Topic: https://groups.io/mt/106018145/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH 04/18] UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe

2024-05-12 Thread duntan
Yes, let's do the cleanup for PcdCpuS3DataAddress in another new patch set.

Thanks,
Dun

-Original Message-
From: Wu, Jiaxin  
Sent: Monday, May 13, 2024 11:04 AM
To: Tan, Dun ; devel@edk2.groups.io
Cc: Ni, Ray ; Kumar, Rahul R ; Gerd 
Hoffmann 
Subject: RE: [PATCH 04/18] UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe

I think the MtrrTable is not needed in the AcpiCpuDataEx (which is from the 
PcdCpuS3DataAddress).

That mean: we can cleanup unused definition in the PcdCpuS3DataAddress. Maybe 
in the future patches set, not this one.

Thanks,
Jiaxin 

> -Original Message-
> From: Tan, Dun 
> Sent: Friday, May 10, 2024 6:08 PM
> To: devel@edk2.groups.io
> Cc: Ni, Ray ; Kumar, Rahul R 
> ; Gerd Hoffmann ; Wu, 
> Jiaxin 
> Subject: [PATCH 04/18] UefiCpuPkg: Save MTRR by lockbox in 
> CpuS3DataDxe
> 
> Save MTRR by lockbox in CpuS3DataDxe. In S3 boot, The MTRR setting 
> will be restored in S3Resume.c in following patches. Then S3Resume.c 
> will wakeup all APs to load the MTRR setting. This can avoid waking up 
> APs in CpuS3.c.
> 
> Signed-off-by: Dun Tan 
> Cc: Ray Ni 
> Cc: Rahul Kumar 
> Cc: Gerd Hoffmann 
> Cc: Jiaxin Wu 
> ---
>  UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c  | 11 +++
>  UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf |  2 ++
>  2 files changed, 13 insertions(+)
> 
> diff --git a/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
> b/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
> index 86ce5303ca..825bb00b74 100644
> --- a/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
> +++ b/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
> @@ -26,6 +26,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent  
> #include   #include   #include 
> 
> +#include 
> 
>  #include 
>  #include 
> @@ -130,6 +131,16 @@ CpuS3DataOnEndOfDxe (
>DEBUG ((DEBUG_VERBOSE, "%a\n", __func__));
>MtrrGetAllMtrrs (>MtrrTable);
> 
> +  //
> +  // Save MTRR in lockbox
> +  //
> +  Status = SaveLockBox (
> + ,
> + >MtrrTable,
> + sizeof (MTRR_SETTINGS)
> + );
> +  ASSERT_EFI_ERROR (Status);
> +
>//
>// Close event, so it will not be invoked again.
>//
> diff --git a/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
> b/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
> index 510133a614..a09254746b 100644
> --- a/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
> +++ b/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
> @@ -46,9 +46,11 @@
>BaseLib
>MtrrLib
>MemoryAllocationLib
> +  LockBoxLib
> 
>  [Guids]
>gEfiEndOfDxeEventGroupGuid ## CONSUMES   ## Event
> +  gEdkiiS3MtrrSettingGuid
> 
>  [Protocols]
>gEfiMpServiceProtocolGuid  ## CONSUMES
> --
> 2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118853): https://edk2.groups.io/g/devel/message/118853
Mute This Topic: https://groups.io/mt/106018124/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH 00/19] Remove some S3 related code in CpuS3.c of smm cpu driver

2024-05-12 Thread duntan
Thanks for the comments. Agree that the order adjustment can make the patch set 
clearer. Will modify the commits in next version patch set. 

Thanks,
Dun

-Original Message-
From: Wu, Jiaxin  
Sent: Monday, May 13, 2024 10:49 AM
To: devel@edk2.groups.io; Tan, Dun ; Ni, Ray 

Subject: RE: [edk2-devel] [PATCH 00/19] Remove some S3 related code in CpuS3.c 
of smm cpu driver

My comments as below:
1. I stronger recommend re-ordering the patches as below so as to combine all 
related sub-patches together, then we can more easy understand what we are 
target to resolving: 1) S3 MTRRs operation 2) AP page table unavailiable issue 
fix. 3)  S3 ApHltLoopCode Operation 3) Register table cleanup

1) S3 MTRRs operation:
>   MdeModulePkg: Add gEdkiiS3MtrrSettingGuid
>   OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe
>   UefiCpuPkg: Add locbox lib instance in DSC
>   UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe
>   UefiCpuPkg: LoadMtrrData for all cpu in S3Resume
>   UefiCpuPkg: Remove code to load mtrr setting

2) AP page table unavailiable issue fix:
>   UefiCpuPkg: Disable PG in IA32 ApLoopCode

3)  S3 ApHltLoopCode Operation:
>   UefiCpuPkg:Abstract some DxeMpLib code to function
>   UefiCpuPkg:Move some code in DxeMpLib to common place
>   UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume
>   UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib
>   UefiCpuPkg:Remove code to handle APIC setting and Interrupt
>   UefiCpuPkg:Rremove code to wakeup AP and relocate ap
>   UefiCpuPkg: Remove the duplicated mpservice locate
>   MdeModulePkg: remove MpService2Ppi field in SMM_S3_RESUME_STATE

4) Register table cleanup:
>   UefiCpuPkg:Set PcdCpuFeaturesInitOnS3Resume to TRUE
>   UefiCpuPkg: Remove code to set register table
>   UefiCpuPkg: Remove GetAcpiCpuData() in CpuS3.c

2. Update all intel copyright year.
3. Add all reviewer in the 00 patch.
4. Add PR in the 00 patch (if have).

   
Thanks,
Jiaxin


> -Original Message-
> From: devel@edk2.groups.io  On Behalf Of duntan
> Sent: Friday, May 10, 2024 6:08 PM
> To: devel@edk2.groups.io
> Subject: [edk2-devel] [PATCH 00/19] Remove some S3 related code in 
> CpuS3.c of smm cpu driver
> 
> This patch set is to remove some S3 related code in CpuS3.c of smm cpu 
> driver. It contain commits to:
> 1.Save MTRR setting by lockbox in PEI phase 2.Load MTRR in S3Resume.c 
> before transferring to CpuS3.c in smm cpu driver.
> 3.Add callback of gEdkiiEndOfS3ResumeGuid in PeiMpLib to relocate Ap 
> to new safe buffer.
> 4.Install gEdkiiEndOfS3ResumeGuid in S3Resume.c before booting OS to 
> relocate APs 4.Change PcdCpuFeaturesInitOnS3Resume to TRUE to set 
> register table in CpuFeaturesPei 5.Remove code to set register 
> table/load mtrr/wakeup AP and relocate ap in CpuS3.c.
> 
> With this patch set, CpuS3.c in smm CPU driver can be simplified.
> 
> Dun Tan (18):
>   MdeModulePkg: Add gEdkiiS3MtrrSettingGuid
>   OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe
>   UefiCpuPkg: Add locbox lib instance in DSC
>   UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe
>   UefiCpuPkg: LoadMtrrData for all cpu in S3Resume
>   UefiCpuPkg: Remove the duplicated mpservice locate
>   UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume
>   UefiCpuPkg:Abstract some DxeMpLib code to function
>   UefiCpuPkg:Move some code in DxeMpLib to common place
>   UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib
>   UefiCpuPkg: Disable PG in IA32 ApLoopCode
>   UefiCpuPkg: Remove code to load mtrr setting
>   UefiCpuPkg:Set PcdCpuFeaturesInitOnS3Resume to TRUE
>   UefiCpuPkg: Remove code to set register table
>   UefiCpuPkg:Remove code to handle APIC setting and Interrupt
>   UefiCpuPkg:Rremove code to wakeup AP and relocate ap
>   UefiCpuPkg: Remove GetAcpiCpuData() in CpuS3.c
>   MdeModulePkg: remove MpService2Ppi field in SMM_S3_RESUME_STATE
> 
> Ray Ni (1):
>   MdePkg: Add MmUnblockMemoryLib to MdeLibs.dsc
> 
>  MdeModulePkg/Include/Guid/AcpiS3Context.h   |   1 -
>  MdeModulePkg/MdeModulePkg.dec   |   3 +++
>  MdePkg/MdeLibs.dsc.inc  |   3 ++-
>  OvmfPkg/CpuS3DataDxe/CpuS3Data.c|  11 +++
>  OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf   |   2 ++
>  UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c |  11 +++
>  UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf|   2 ++
>  UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 181
> +++---
> --
> 
> ---
>  UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm  |   4 
>  UefiCpuPkg/Library/MpInitLib/MpLib.c

[edk2-devel] [PATCH 18/18] MdeModulePkg:Remove MpService2Ppi field in SMM_S3_RESUME_STATE

2024-05-10 Thread duntan
This MpService2Ppi field in SMM_S3_RESUME_STATE is used to
wakeup AP to do the CPU initialization during smm s3 boot when
the execution mode of PEI and DXE are the same.
Currently, in CpuS3.c of smm cpu driver, BSP doesn't need to
wakeup AP anymore. The initialization for AP will be done in
S3Resume.c before transfer to CpuS3.c of smm cpu driver.
So we can remove the MpService2Ppi field in SMM_S3_RESUME_STATE.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Jian J Wang 
Cc: Liming Gao 
---
 MdeModulePkg/Include/Guid/AcpiS3Context.h | 1 -
 1 file changed, 1 deletion(-)

diff --git a/MdeModulePkg/Include/Guid/AcpiS3Context.h 
b/MdeModulePkg/Include/Guid/AcpiS3Context.h
index 72d173c4fd..0cfc81b49b 100644
--- a/MdeModulePkg/Include/Guid/AcpiS3Context.h
+++ b/MdeModulePkg/Include/Guid/AcpiS3Context.h
@@ -30,7 +30,6 @@ typedef struct {
   EFI_PHYSICAL_ADDRESSReturnContext1;
   EFI_PHYSICAL_ADDRESSReturnContext2;
   EFI_PHYSICAL_ADDRESSReturnStackPointer;
-  EFI_PHYSICAL_ADDRESSMpService2Ppi;
   EFI_PHYSICAL_ADDRESSSmst;
 } SMM_S3_RESUME_STATE;
 
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118810): https://edk2.groups.io/g/devel/message/118810
Mute This Topic: https://groups.io/mt/106018145/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 17/18] UefiCpuPkg: Remove GetAcpiCpuData() in CpuS3.c

2024-05-10 Thread duntan
Remove GetAcpiCpuData() in CpuS3.c. The mAcpiCpuData
is not needed in S3 boot anymore.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c  | 243 
+--
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c |   6 ++
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h |  24 
 3 files changed, 3 insertions(+), 270 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index e84bc14de0..78ecf4efc6 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -9,22 +9,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include "PiSmmCpuDxeSmm.h"
 #include 
 
-//
-// Flags used when program the register.
-//
-typedef struct {
-  volatile UINTN MemoryMappedLock;  // Spinlock used to 
program mmio
-  volatile UINT32*CoreSemaphoreCount;   // Semaphore container 
used to program
-// core level semaphore.
-  volatile UINT32*PackageSemaphoreCount;// Semaphore container 
used to program
-// package level semaphore.
-} PROGRAM_CPU_REGISTER_FLAGS;
-
-#define LEGACY_REGION_SIZE  (2 * 0x1000)
-#define LEGACY_REGION_BASE  (0xA - LEGACY_REGION_SIZE)
-
-ACPI_CPU_DATA  mAcpiCpuData;
-BOOLEANmRestoreSmmConfigurationInS3 = FALSE;
+BOOLEAN  mRestoreSmmConfigurationInS3 = FALSE;
 
 //
 // S3 boot flag
@@ -266,232 +251,6 @@ InitSmmS3ResumeState (
   }
 }
 
-/**
-  Copy register table from non-SMRAM into SMRAM.
-
-  @param[in] DestinationRegisterTableList  Points to destination register 
table.
-  @param[in] SourceRegisterTableList   Points to source register table.
-  @param[in] NumberOfCpus  Number of CPUs.
-
-**/
-VOID
-CopyRegisterTable (
-  IN CPU_REGISTER_TABLE  *DestinationRegisterTableList,
-  IN CPU_REGISTER_TABLE  *SourceRegisterTableList,
-  IN UINT32  NumberOfCpus
-  )
-{
-  UINTN Index;
-  CPU_REGISTER_TABLE_ENTRY  *RegisterTableEntry;
-
-  CopyMem (DestinationRegisterTableList, SourceRegisterTableList, NumberOfCpus 
* sizeof (CPU_REGISTER_TABLE));
-  for (Index = 0; Index < NumberOfCpus; Index++) {
-if (DestinationRegisterTableList[Index].TableLength != 0) {
-  DestinationRegisterTableList[Index].AllocatedSize = 
DestinationRegisterTableList[Index].TableLength * sizeof 
(CPU_REGISTER_TABLE_ENTRY);
-  RegisterTableEntry= AllocateCopyPool (
-
DestinationRegisterTableList[Index].AllocatedSize,
-(VOID 
*)(UINTN)SourceRegisterTableList[Index].RegisterTableEntry
-);
-  ASSERT (RegisterTableEntry != NULL);
-  DestinationRegisterTableList[Index].RegisterTableEntry = 
(EFI_PHYSICAL_ADDRESS)(UINTN)RegisterTableEntry;
-}
-  }
-}
-
-/**
-  Check whether the register table is empty or not.
-
-  @param[in] RegisterTable  Point to the register table.
-  @param[in] NumberOfCpus   Number of CPUs.
-
-  @retval TRUE  The register table is empty.
-  @retval FALSE The register table is not empty.
-**/
-BOOLEAN
-IsRegisterTableEmpty (
-  IN CPU_REGISTER_TABLE  *RegisterTable,
-  IN UINT32  NumberOfCpus
-  )
-{
-  UINTN  Index;
-
-  if (RegisterTable != NULL) {
-for (Index = 0; Index < NumberOfCpus; Index++) {
-  if (RegisterTable[Index].TableLength != 0) {
-return FALSE;
-  }
-}
-  }
-
-  return TRUE;
-}
-
-/**
-  Copy the data used to initialize processor register into SMRAM.
-
-  @param[in,out]  CpuFeatureInitDataDst   Pointer to the destination 
CPU_FEATURE_INIT_DATA structure.
-  @param[in]  CpuFeatureInitDataSrc   Pointer to the source 
CPU_FEATURE_INIT_DATA structure.
-
-**/
-VOID
-CopyCpuFeatureInitDatatoSmram (
-  IN OUT CPU_FEATURE_INIT_DATA  *CpuFeatureInitDataDst,
-  IN CPU_FEATURE_INIT_DATA  *CpuFeatureInitDataSrc
-  )
-{
-  CPU_STATUS_INFORMATION  *CpuStatus;
-
-  if (!IsRegisterTableEmpty ((CPU_REGISTER_TABLE 
*)(UINTN)CpuFeatureInitDataSrc->PreSmmInitRegisterTable, 
mAcpiCpuData.NumberOfCpus)) {
-CpuFeatureInitDataDst->PreSmmInitRegisterTable = 
(EFI_PHYSICAL_ADDRESS)(UINTN)AllocatePool (mAcpiCpuData.NumberOfCpus * sizeof 
(CPU_REGISTER_TABLE));
-ASSERT (CpuFeatureInitDataDst->PreSmmInitRegisterTable != 0);
-
-CopyRegisterTable (
-  (CPU_REGISTER_TABLE 
*)(UINTN)CpuFeatureInitDataDst->PreSmmInitRegisterTable,
-  (CPU_REGISTER_TABLE 
*)(UINTN)CpuFeatureInitDataSrc->PreSmmInitRegisterTable,
- 

[edk2-devel] [PATCH 14/18] UefiCpuPkg: Remove code to set register table

2024-05-10 Thread duntan
Remove code to set register table in CpuS3.c.
In previous commit, PcdCpuFeaturesInitOnS3Resume
has been set to TRUE. So that CpuFeaturesPei PEIM
will initialize the CPU registers and perform CPU
features initialization.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 423 
---
 1 file changed, 423 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index 7ac6b62676..9520451d92 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -91,425 +91,6 @@ UINT8  mApHltLoopCodeTemplate[] = {
   0xEB, 0xFC  // jmp $-2
 };
 
-/**
-  Increment semaphore by 1.
-
-  @param  SemIN:  32-bit unsigned integer
-
-**/
-VOID
-S3ReleaseSemaphore (
-  IN OUT  volatile UINT32  *Sem
-  )
-{
-  InterlockedIncrement (Sem);
-}
-
-/**
-  Decrement the semaphore by 1 if it is not zero.
-
-  Performs an atomic decrement operation for semaphore.
-  The compare exchange operation must be performed using
-  MP safe mechanisms.
-
-  @param  SemIN:  32-bit unsigned integer
-
-**/
-VOID
-S3WaitForSemaphore (
-  IN OUT  volatile UINT32  *Sem
-  )
-{
-  UINT32  Value;
-
-  do {
-Value = *Sem;
-  } while (Value == 0 ||
-   InterlockedCompareExchange32 (
- Sem,
- Value,
- Value - 1
- ) != Value);
-}
-
-/**
-  Read / write CR value.
-
-  @param[in]  CrIndex The CR index which need to read/write.
-  @param[in]  ReadRead or write. TRUE is read.
-  @param[in,out]  CrValue CR value.
-
-  @retvalEFI_SUCCESS means read/write success, else return EFI_UNSUPPORTED.
-**/
-UINTN
-ReadWriteCr (
-  IN UINT32   CrIndex,
-  IN BOOLEAN  Read,
-  IN OUT UINTN*CrValue
-  )
-{
-  switch (CrIndex) {
-case 0:
-  if (Read) {
-*CrValue = AsmReadCr0 ();
-  } else {
-AsmWriteCr0 (*CrValue);
-  }
-
-  break;
-case 2:
-  if (Read) {
-*CrValue = AsmReadCr2 ();
-  } else {
-AsmWriteCr2 (*CrValue);
-  }
-
-  break;
-case 3:
-  if (Read) {
-*CrValue = AsmReadCr3 ();
-  } else {
-AsmWriteCr3 (*CrValue);
-  }
-
-  break;
-case 4:
-  if (Read) {
-*CrValue = AsmReadCr4 ();
-  } else {
-AsmWriteCr4 (*CrValue);
-  }
-
-  break;
-default:
-  return EFI_UNSUPPORTED;
-  }
-
-  return EFI_SUCCESS;
-}
-
-/**
-  Initialize the CPU registers from a register table.
-
-  @param[in]  RegisterTable The register table for this AP.
-  @param[in]  ApLocationAP location info for this ap.
-  @param[in]  CpuStatus CPU status info for this CPU.
-  @param[in]  CpuFlags  Flags data structure used when program the 
register.
-
-  @note This service could be called by BSP/APs.
-**/
-VOID
-ProgramProcessorRegister (
-  IN CPU_REGISTER_TABLE  *RegisterTable,
-  IN EFI_CPU_PHYSICAL_LOCATION   *ApLocation,
-  IN CPU_STATUS_INFORMATION  *CpuStatus,
-  IN PROGRAM_CPU_REGISTER_FLAGS  *CpuFlags
-  )
-{
-  CPU_REGISTER_TABLE_ENTRY  *RegisterTableEntry;
-  UINTN Index;
-  UINTN Value;
-  CPU_REGISTER_TABLE_ENTRY  *RegisterTableEntryHead;
-  volatile UINT32   *SemaphorePtr;
-  UINT32FirstThread;
-  UINT32CurrentThread;
-  UINT32CurrentCore;
-  UINTN ProcessorIndex;
-  UINT32*ThreadCountPerPackage;
-  UINT8 *ThreadCountPerCore;
-  EFI_STATUSStatus;
-  UINT64CurrentValue;
-
-  //
-  // Traverse Register Table of this logical processor
-  //
-  RegisterTableEntryHead = (CPU_REGISTER_TABLE_ENTRY 
*)(UINTN)RegisterTable->RegisterTableEntry;
-
-  for (Index = 0; Index < RegisterTable->TableLength; Index++) {
-RegisterTableEntry = [Index];
-
-//
-// Check the type of specified register
-//
-switch (RegisterTableEntry->RegisterType) {
-  //
-  // The specified register is Control Register
-  //
-  case ControlRegister:
-Status = ReadWriteCr (RegisterTableEntry->Index, TRUE, );
-if (EFI_ERROR (Status)) {
-  break;
-}
-
-if (RegisterTableEntry->TestThenWrite) {
-  CurrentValue = BitFieldRead64 (
-   Value,
-   RegisterTableEntry->ValidBitStart,
-  

[edk2-devel] [PATCH 16/18] UefiCpuPkg:Remove code to wakeup AP and relocate ap

2024-05-10 Thread duntan
After the code to load mtrr setting, set register table,
handle APIC setting and Interrupt after INIT-SIPI-SIPI
is moved, the InitializeCpuProcedure() only contains
following code logic:
1.Bsp runs ExecuteFirstSmiInit().
2.Bsp transfers AP to safe hlt-loop

During S3 boot, since APs will be relocated to new safe
buffer by the callback of gEdkiiEndOfS3ResumeGuid in
PeiMpLib, Bsp doesn't need to transfer AP to safe hlt-loop
any more. SmmRestoreCpu() in CpuS3 only needs to runs the
ExecuteFirstSmiInit() on BSP. So remove code to wakeup
AP by INIT-SIPI-SIPI and remove code to relocate ap to
safe hlt-loop.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 292 
+---
 UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/MpFuncs.nasm   | 153 
-
 UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmFuncsArch.c |  27 ---
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf  |   3 ---
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/MpFuncs.nasm| 189 
-
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmFuncsArch.c  |  28 

 6 files changed, 9 insertions(+), 683 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index 65fe903fd3..e84bc14de0 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -8,30 +8,6 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 
 #include "PiSmmCpuDxeSmm.h"
 #include 
-#include 
-
-#pragma pack(1)
-typedef struct {
-  UINTN  Lock;
-  VOID   *StackStart;
-  UINTN  StackSize;
-  VOID   *ApFunction;
-  IA32_DESCRIPTORGdtrProfile;
-  IA32_DESCRIPTORIdtrProfile;
-  UINT32 BufferStart;
-  UINT32 Cr3;
-  UINTN  InitializeFloatingPointUnitsAddress;
-} MP_CPU_EXCHANGE_INFO;
-#pragma pack()
-
-typedef struct {
-  UINT8*RendezvousFunnelAddress;
-  UINTNPModeEntryOffset;
-  UINTNFlatJumpOffset;
-  UINTNSize;
-  UINTNLModeEntryOffset;
-  UINTNLongJumpOffset;
-} MP_ASSEMBLY_ADDRESS_MAP;
 
 //
 // Flags used when program the register.
@@ -44,31 +20,11 @@ typedef struct {
 // package level semaphore.
 } PROGRAM_CPU_REGISTER_FLAGS;
 
-//
-// Signal that SMM BASE relocation is complete.
-//
-volatile BOOLEAN  mInitApsAfterSmmBaseReloc;
-
-/**
-  Get starting address and size of the rendezvous entry for APs.
-  Information for fixing a jump instruction in the code is also returned.
-
-  @param AddressMap  Output buffer for address map information.
-**/
-VOID *
-EFIAPI
-AsmGetAddressMap (
-  MP_ASSEMBLY_ADDRESS_MAP  *AddressMap
-  );
-
 #define LEGACY_REGION_SIZE  (2 * 0x1000)
 #define LEGACY_REGION_BASE  (0xA - LEGACY_REGION_SIZE)
 
-PROGRAM_CPU_REGISTER_FLAGS  mCpuFlags;
-ACPI_CPU_DATA   mAcpiCpuData;
-volatile UINT32 mNumberToFinish;
-MP_CPU_EXCHANGE_INFO*mExchangeInfo;
-BOOLEAN mRestoreSmmConfigurationInS3 = FALSE;
+ACPI_CPU_DATA  mAcpiCpuData;
+BOOLEANmRestoreSmmConfigurationInS3 = FALSE;
 
 //
 // S3 boot flag
@@ -82,191 +38,6 @@ SMM_S3_RESUME_STATE  *mSmmS3ResumeState = NULL;
 
 BOOLEAN  mAcpiS3Enable = TRUE;
 
-UINT8  *mApHltLoopCode  = NULL;
-UINT8  mApHltLoopCodeTemplate[] = {
-  0x8B, 0x44, 0x24, 0x04, // mov  eax, dword ptr [esp+4]
-  0xF0, 0xFF, 0x08,   // lock dec  dword ptr [eax]
-  0xFA,   // cli
-  0xF4,   // hlt
-  0xEB, 0xFC  // jmp $-2
-};
-
-/**
-  The function is invoked before SMBASE relocation in S3 path to restores CPU 
status.
-
-  The function is invoked before SMBASE relocation in S3 path. It does first 
time microcode load
-  and restores MTRRs for both BSP and APs.
-
-  @param   IsBsp   The CPU this function executes on is BSP or not.
-
-**/
-VOID
-InitializeCpuBeforeRebase (
-  IN BOOLEAN  IsBsp
-  )
-{
-  //
-  // Count down the number with lock mechanism.
-  //
-  InterlockedDecrement ();
-
-  if (IsBsp) {
-//
-// Bsp wait here till all AP finish the initialization before rebase
-//
-while (mNumberToFinish > 0) {
-  CpuPause ();
-}
-  }
-}
-
-/**
-  The function is invoked after SMBASE relocation in S3 path to restores CPU 
status.
-
-  The function is invoked after SMBASE relocation in S3 path. 

[edk2-devel] [PATCH 13/18] UefiCpuPkg:Set PcdCpuFeaturesInitOnS3Resume to TRUE

2024-05-10 Thread duntan
Set PcdCpuFeaturesInitOnS3Resume to TRUE. So that
CpuFeaturesPei PEIM will initialize the CPU registers
and perform CPU features initialization.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/UefiCpuPkg.dec | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/UefiCpuPkg/UefiCpuPkg.dec b/UefiCpuPkg/UefiCpuPkg.dec
index f86a6d2bcb..45384d725c 100644
--- a/UefiCpuPkg/UefiCpuPkg.dec
+++ b/UefiCpuPkg/UefiCpuPkg.dec
@@ -310,7 +310,7 @@
 
   ## Specifies if CPU features will be initialized during S3 resume.
   # @Prompt If CPU features will be initialized during S3 resume.
-  
gUefiCpuPkgTokenSpaceGuid.PcdCpuFeaturesInitOnS3Resume|FALSE|BOOLEAN|0x001D
+  
gUefiCpuPkgTokenSpaceGuid.PcdCpuFeaturesInitOnS3Resume|TRUE|BOOLEAN|0x001D
 
   ## Specifies CPUID Leaf 0x15 Time Stamp Counter and Nominal Core Crystal 
Clock Frequency.
   # TSC Frequency = ECX (core crystal clock frequency) * EBX/EAX.
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118805): https://edk2.groups.io/g/devel/message/118805
Mute This Topic: https://groups.io/mt/106018138/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 15/18] UefiCpuPkg:Remove code to handle APIC setting and Interrupt

2024-05-10 Thread duntan
Remove ProgramVirtualWireMode()/DisableLvtInterrupts()
since APs won't be waken by INIT-SIPI-SIPI in CpuS3.c
any more. The two functions has been executed in
MpInitLibInitialize() in PeiMplib.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 5 -
 1 file changed, 5 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index 9520451d92..65fe903fd3 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -105,11 +105,6 @@ InitializeCpuBeforeRebase (
   IN BOOLEAN  IsBsp
   )
 {
-  ProgramVirtualWireMode ();
-  if (!IsBsp) {
-DisableLvtInterrupts ();
-  }
-
   //
   // Count down the number with lock mechanism.
   //
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118807): https://edk2.groups.io/g/devel/message/118807
Mute This Topic: https://groups.io/mt/106018141/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 12/18] UefiCpuPkg: Remove code to load mtrr setting

2024-05-10 Thread duntan
Remove code to load mtrr setting in CpuS3.c.
In previous commits, before transferring to
CpuS3.c, MTRR setting has been loaded in
S3RestoreConfig2() for all CPU.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c | 32 
 1 file changed, 32 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
index d67fb49890..7ac6b62676 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c
@@ -91,36 +91,6 @@ UINT8  mApHltLoopCodeTemplate[] = {
   0xEB, 0xFC  // jmp $-2
 };
 
-/**
-  Sync up the MTRR values for all processors.
-
-  @param MtrrTable  Table holding fixed/variable MTRR values to be loaded.
-**/
-VOID
-EFIAPI
-LoadMtrrData (
-  EFI_PHYSICAL_ADDRESS  MtrrTable
-  )
-
-/*++
-
-Routine Description:
-
-  Sync up the MTRR values for all processors.
-
-Arguments:
-
-Returns:
-None
-
---*/
-{
-  MTRR_SETTINGS  *MtrrSettings;
-
-  MtrrSettings = (MTRR_SETTINGS *)(UINTN)MtrrTable;
-  MtrrSetAllMtrrs (MtrrSettings);
-}
-
 /**
   Increment semaphore by 1.
 
@@ -554,8 +524,6 @@ InitializeCpuBeforeRebase (
   IN BOOLEAN  IsBsp
   )
 {
-  LoadMtrrData (mAcpiCpuData.MtrrTable);
-
   SetRegister (TRUE);
 
   ProgramVirtualWireMode ();
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118804): https://edk2.groups.io/g/devel/message/118804
Mute This Topic: https://groups.io/mt/106018137/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 11/18] UefiCpuPkg: Disable PG in IA32 ApLoopCode

2024-05-10 Thread duntan
Disable paging in IA32 RelocateApLoop assembly
code to fix the issue that the AP page table is
unavailiable after boot OS under IA32 execution mode.

This issue exist in IA32 PEI + IA32 DXE normal boot
(also S3 boot with IA32 PEI after previous three commits
are accepted). In current MpLib code, the IA32 execution
mode code did not create page table in reserved memory
like what X64 code did. If PcdCpuStackGuard is TRUE, the
PG is enabled for AP in current RelocateApLoop assembly
code. And the page table for AP is unavailiable after
boot OS. This might cause potential issue. So disable PG
in IA32 RelocateApLoop.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm | 4 
 1 file changed, 4 insertions(+)

diff --git a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm 
b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm
index d117f09ef5..75b18ce56f 100644
--- a/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm
+++ b/UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm
@@ -225,6 +225,10 @@ RendezvousFunnelProcEnd:
 ;  specific to SEV-ES support and are not applicable on IA32.
 
;-
 AsmRelocateApLoopGenericStart:
+moveax, cr0
+btreax, 31 ; Clear CR0.PG
+movcr0, eax; Disable paging since the page table might 
be unavailiable
+
 moveax, esp
 movesp, [eax + 12] ; TopOfApStack
 push   dword [eax] ; push return address for stack trace
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118803): https://edk2.groups.io/g/devel/message/118803
Mute This Topic: https://groups.io/mt/106018136/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 10/18] UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib

2024-05-10 Thread duntan
In this commit, change PeiMpLib to install callback
of gEdkiiEndOfS3ResumeGuid to relocate AP to new safe
buffer. The gEdkiiEndOfS3ResumeGuid is installed in
S3Resume.c before jmping to OS waking vector.

Previously, code in CpuS3.c of PiSmmCpuDxe driver will
prepare the new safe buffer for AP and place AP in hlt
loop state. With this code change, we can remove the
Machine Instructions of mApHltLoopCode in PiSmmCpuDxe.
Also we can reuse the related code in DxeMpLib for
PeiMpLib.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Library/MpInitLib/MpLib.h  |   3 +++
 UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf |   4 
 UefiCpuPkg/Library/MpInitLib/PeiMpLib.c   | 152 

 3 files changed, 159 insertions(+)

diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h 
b/UefiCpuPkg/Library/MpInitLib/MpLib.h
index 11e0d2661f..3efd913395 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.h
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h
@@ -33,6 +33,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include 
@@ -68,6 +69,8 @@
 //
 #define DEFAULT_MAX_MICROCODE_PATCH_NUM  8
 
+#define PAGING_4K_ADDRESS_MASK_64  0x000FF000ull
+
 //
 // Data structure for microcode patch information
 //
diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf 
b/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf
index e31e34b6f9..8736690348 100644
--- a/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf
+++ b/UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf
@@ -25,10 +25,12 @@
 [Sources.IA32]
   Ia32/AmdSev.c
   Ia32/MpFuncs.nasm
+  Ia32/CreatePageTable.c
 
 [Sources.X64]
   X64/AmdSev.c
   X64/MpFuncs.nasm
+  X64/CreatePageTable.c
 
 [Sources.IA32, Sources.X64]
   AmdSev.c
@@ -64,6 +66,7 @@
   LocalApicLib
   MicrocodeLib
   MtrrLib
+  CpuPageTableLib
 
 [Pcd]
   gEfiMdeModulePkgTokenSpaceGuid.PcdGhcbBase   ## CONSUMES
@@ -87,6 +90,7 @@
   gEdkiiS3SmmInitDoneGuid
   gEdkiiMicrocodePatchHobGuid
   gGhcbApicIdsGuid   ## SOMETIMES_CONSUMES
+  gEdkiiEndOfS3ResumeGuid
 
 [Guids.LoongArch64]
   gProcessorResourceHobGuid  ## SOMETIMES_CONSUMES  ## HOB
diff --git a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c 
b/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
index 4d3acb491f..deb5fc3aac 100644
--- a/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/PeiMpLib.c
@@ -9,6 +9,7 @@
 #include "MpLib.h"
 #include 
 #include 
+#include 
 #include 
 
 STATIC UINT64  mSevEsPeiWakeupBuffer = BASE_1MB;
@@ -449,6 +450,47 @@ BuildMicrocodeCacheHob (
   return;
 }
 
+/**
+  S3 SMM Init Done notification function.
+
+  @param  PeiServices  Indirect reference to the PEI Services Table.
+  @param  NotifyDesc   Address of the notification descriptor data 
structure.
+  @param  InvokePpiAddress of the PPI that was invoked.
+
+  @retval EFI_SUCCESS  The function completes successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+NotifyOnEndOfS3Resume (
+  IN  EFI_PEI_SERVICES   **PeiServices,
+  IN  EFI_PEI_NOTIFY_DESCRIPTOR  *NotifyDesc,
+  IN  VOID   *InvokePpi
+  )
+{
+  CPU_MP_DATA  *CpuMpData;
+
+  CpuMpData   = GetCpuMpData ();
+  mNumberToFinish = CpuMpData->CpuCount - 1;
+  WakeUpAP (CpuMpData, TRUE, 0, RelocateApLoop, NULL, TRUE);
+  while (mNumberToFinish > 0) {
+CpuPause ();
+  }
+
+  DEBUG ((DEBUG_INFO, "%a() done!\n", __func__));
+
+  return EFI_SUCCESS;
+}
+
+//
+// Global function
+//
+EFI_PEI_NOTIFY_DESCRIPTOR  mEndOfS3ResumeNotifyDesc = {
+  EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | 
EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
+  ,
+  NotifyOnEndOfS3Resume
+};
+
 /**
   Initialize global data for MP support.
 
@@ -463,12 +505,16 @@ InitMpGlobalData (
 
   BuildMicrocodeCacheHob (CpuMpData);
   SaveCpuMpData (CpuMpData);
+  PrepareApLoopCode (CpuMpData);
 
   ///
   /// Install Notify
   ///
   Status = PeiServicesNotifyPpi ();
   ASSERT_EFI_ERROR (Status);
+
+  Status = PeiServicesNotifyPpi ();
+  ASSERT_EFI_ERROR (Status);
 }
 
 /**
@@ -815,3 +861,109 @@ PlatformShadowMicrocode (
 
   return EFI_SUCCESS;
 }
+
+/**
+  Allocate buffer for ApLoopCode.
+
+  @param[in]  PagesNumber of pages to allocate.
+  @param[in, out] Address  Pointer to the allocated buffer.
+**/
+VOID
+AllocateApLoopCodeBuffer (
+  IN UINTN Pages,
+  IN OUT EFI_PHYSICAL_ADDRESS  *Address
+  )
+{
+  EFI_STATUS  Status;
+
+  Status = PeiServicesAllocatePages (EfiACPIMemoryNVS, Pages, Address);
+  if (EFI_ERROR (Status)) {
+*Address = 0;
+  }
+}
+
+/**
+  Remove Nx protection for the range specific by BaseAddress and Length.
+
+  The PEI implementation uses CpuPageTableLib to change the attribute.
+  The DXE implementation uses gDS to change the attribute.
+
+  @param[in] BaseAddress  BaseAddress of the range.
+  @param[in] 

[edk2-devel] [PATCH 09/18] UefiCpuPkg:Move some code in DxeMpLib to common place

2024-05-10 Thread duntan
Move some code in DxeMpLib.C to common MpLib.c.
The related code is to relocate Ap to new safe buffer
before booting into OS. In next commits, these code
also will be used by PeiMpLib. This commit doesn't
change any code functionality.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 151 
+--
 UefiCpuPkg/Library/MpInitLib/MpLib.c| 142 
++
 UefiCpuPkg/Library/MpInitLib/MpLib.h|  51 
++-
 3 files changed, 197 insertions(+), 147 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c 
b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
index d13cebbee6..229e38e593 100644
--- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
@@ -20,15 +20,11 @@
 
 #define  AP_SAFE_STACK_SIZE  128
 
-CPU_MP_DATA *mCpuMpData  = NULL;
-EFI_EVENT   mCheckAllApsEvent= NULL;
-EFI_EVENT   mMpInitExitBootServicesEvent = NULL;
-EFI_EVENT   mLegacyBootEvent = NULL;
-volatile BOOLEANmStopCheckAllApsStatus   = TRUE;
-RELOCATE_AP_LOOP_ENTRY  mReservedApLoop;
-UINTN   mReservedTopOfApStack;
-volatile UINT32 mNumberToFinish = 0;
-UINTN   mApPageTable;
+CPU_MP_DATA   *mCpuMpData  = NULL;
+EFI_EVENT mCheckAllApsEvent= NULL;
+EFI_EVENT mMpInitExitBootServicesEvent = NULL;
+EFI_EVENT mLegacyBootEvent = NULL;
+volatile BOOLEAN  mStopCheckAllApsStatus   = TRUE;
 
 //
 // Begin wakeup buffer allocation below 0x88000
@@ -368,60 +364,6 @@ GetProtectedModeCS (
   return Index * 8;
 }
 
-/**
-  Do sync on APs.
-
-  @param[in, out] Buffer  Pointer to private data buffer.
-**/
-VOID
-EFIAPI
-RelocateApLoop (
-  IN OUT VOID  *Buffer
-  )
-{
-  CPU_MP_DATA  *CpuMpData;
-  BOOLEAN  MwaitSupport;
-  UINTNProcessorNumber;
-  UINTNStackStart;
-
-  MpInitLibWhoAmI ();
-  CpuMpData= GetCpuMpData ();
-  MwaitSupport = IsMwaitSupport ();
-  if (CpuMpData->UseSevEsAPMethod) {
-//
-// 64-bit AMD processors with SEV-ES
-//
-StackStart = CpuMpData->SevEsAPResetStackStart;
-mReservedApLoop.AmdSevEntry (
-  MwaitSupport,
-  CpuMpData->ApTargetCState,
-  CpuMpData->PmCodeSegment,
-  StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,
-  (UINTN),
-  CpuMpData->Pm16CodeSegment,
-  CpuMpData->SevEsAPBuffer,
-  CpuMpData->WakeupBuffer
-  );
-  } else {
-//
-// Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit 
AMD processors without SEV-ES
-//
-StackStart = mReservedTopOfApStack;
-mReservedApLoop.GenericEntry (
-  MwaitSupport,
-  CpuMpData->ApTargetCState,
-  StackStart - ProcessorNumber * AP_SAFE_STACK_SIZE,
-  (UINTN),
-  mApPageTable
-  );
-  }
-
-  //
-  // It should never reach here
-  //
-  ASSERT (FALSE);
-}
-
 /**
   Allocate buffer for ApLoopCode.
 
@@ -477,89 +419,6 @@ RemoveNxprotection (
   }
 }
 
-/**
-  Prepare ApLoopCode.
-
-  @param[in] CpuMpData  Pointer to CpuMpData.
-**/
-VOID
-PrepareApLoopCode (
-  IN CPU_MP_DATA  *CpuMpData
-  )
-{
-  EFI_PHYSICAL_ADDRESS Address;
-  MP_ASSEMBLY_ADDRESS_MAP  *AddressMap;
-  UINT8*ApLoopFunc;
-  UINTNApLoopFuncSize;
-  UINTNStackPages;
-  UINTNFuncPages;
-  IA32_CR0 Cr0;
-
-  AddressMap = >AddressMap;
-  if (CpuMpData->UseSevEsAPMethod) {
-//
-// 64-bit AMD processors with SEV-ES
-//
-Address= BASE_4GB - 1;
-ApLoopFunc = AddressMap->RelocateApLoopFuncAddressAmdSev;
-ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeAmdSev;
-  } else {
-//
-// Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit 
AMD processors without SEV-ES
-//
-Address= MAX_ADDRESS;
-ApLoopFunc = AddressMap->RelocateApLoopFuncAddressGeneric;
-ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeGeneric;
-  }
-
-  //
-  // Avoid APs access invalid buffer data which allocated by BootServices,
-  // so we will allocate reserved data for AP loop code. We also need to
-  // allocate this buffer below 4GB due to APs may be transferred to 32bit
-  // protected mode on long mode DXE.
-  // Allocating it in 

[edk2-devel] [PATCH 08/18] UefiCpuPkg:Abstract some DxeMpLib code to function

2024-05-10 Thread duntan
Abstract some DxeMpLib code to function in this commit.
Some of these internal functions will be moved to common
MpLib.c in following commits. Then PeiMpLib can reuse
the code.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 220 
+++-
 1 file changed, 139 insertions(+), 81 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c 
b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
index 57ddb86600..d13cebbee6 100644
--- a/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/DxeMpLib.c
@@ -422,6 +422,144 @@ RelocateApLoop (
   ASSERT (FALSE);
 }
 
+/**
+  Allocate buffer for ApLoopCode.
+
+  @param[in]  PagesNumber of pages to allocate.
+  @param[in, out] Address  Pointer to the allocated buffer.
+**/
+VOID
+AllocateApLoopCodeBuffer (
+  IN UINTN Pages,
+  IN OUT EFI_PHYSICAL_ADDRESS  *Address
+  )
+{
+  EFI_STATUS  Status;
+
+  Status = gBS->AllocatePages (
+  AllocateMaxAddress,
+  EfiReservedMemoryType,
+  Pages,
+  Address
+  );
+  ASSERT_EFI_ERROR (Status);
+}
+
+/**
+  Remove Nx protection for the range specific by BaseAddress and Length.
+
+  The PEI implementation uses CpuPageTableLib to change the attribute.
+  The DXE implementation uses gDS to change the attribute.
+
+  @param[in] BaseAddress  BaseAddress of the range.
+  @param[in] Length   Length of the range.
+**/
+VOID
+RemoveNxprotection (
+  IN EFI_PHYSICAL_ADDRESS  BaseAddress,
+  IN UINTN Length
+  )
+{
+  EFI_STATUS   Status;
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR  MemDesc;
+
+  //
+  // TODO: Check EFI_MEMORY_XP bit set or not once it's available in DXE GCD
+  //   service.
+  //
+  Status = gDS->GetMemorySpaceDescriptor (BaseAddress, );
+  if (!EFI_ERROR (Status)) {
+gDS->SetMemorySpaceAttributes (
+   BaseAddress,
+   Length,
+   MemDesc.Attributes & (~EFI_MEMORY_XP)
+   );
+  }
+}
+
+/**
+  Prepare ApLoopCode.
+
+  @param[in] CpuMpData  Pointer to CpuMpData.
+**/
+VOID
+PrepareApLoopCode (
+  IN CPU_MP_DATA  *CpuMpData
+  )
+{
+  EFI_PHYSICAL_ADDRESS Address;
+  MP_ASSEMBLY_ADDRESS_MAP  *AddressMap;
+  UINT8*ApLoopFunc;
+  UINTNApLoopFuncSize;
+  UINTNStackPages;
+  UINTNFuncPages;
+  IA32_CR0 Cr0;
+
+  AddressMap = >AddressMap;
+  if (CpuMpData->UseSevEsAPMethod) {
+//
+// 64-bit AMD processors with SEV-ES
+//
+Address= BASE_4GB - 1;
+ApLoopFunc = AddressMap->RelocateApLoopFuncAddressAmdSev;
+ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeAmdSev;
+  } else {
+//
+// Intel processors (32-bit or 64-bit), 32-bit AMD processors, or 64-bit 
AMD processors without SEV-ES
+//
+Address= MAX_ADDRESS;
+ApLoopFunc = AddressMap->RelocateApLoopFuncAddressGeneric;
+ApLoopFuncSize = AddressMap->RelocateApLoopFuncSizeGeneric;
+  }
+
+  //
+  // Avoid APs access invalid buffer data which allocated by BootServices,
+  // so we will allocate reserved data for AP loop code. We also need to
+  // allocate this buffer below 4GB due to APs may be transferred to 32bit
+  // protected mode on long mode DXE.
+  // Allocating it in advance since memory services are not available in
+  // Exit Boot Services callback function.
+  //
+  // ++ (TopOfApStack)
+  // |  Stack * N |
+  // ++ (stack base, 4k aligned)
+  // |  Padding   |
+  // ++
+  // |  Ap Loop   |
+  // ++ ((low address, 4k-aligned)
+  //
+
+  StackPages = EFI_SIZE_TO_PAGES (CpuMpData->CpuCount * AP_SAFE_STACK_SIZE);
+  FuncPages  = EFI_SIZE_TO_PAGES (ApLoopFuncSize);
+
+  AllocateApLoopCodeBuffer (StackPages + FuncPages, );
+  ASSERT (Address != 0);
+
+  Cr0.UintN = AsmReadCr0 ();
+  if (Cr0.Bits.PG != 0) {
+//
+// Make sure that the buffer memory is executable if NX protection is 
enabled
+// for EfiReservedMemoryType.
+//
+RemoveNxprotection (Address, EFI_PAGES_TO_SIZE (FuncPages));
+  }
+
+  mReservedTopOfApStack = (UINTN)Address + EFI_PAGES_TO_SIZE 
(StackPages+FuncPages);
+  ASSERT ((mReservedTopOfApStack & (UINTN)(CPU_STACK_ALIGNMENT - 1)) == 0);
+  mReservedApLoop.Data = (VOID *)(UINTN)Address;
+  ASSERT (mReservedApLoop.Data != NULL);
+  CopyMem (mReservedApLoop.Data, ApLoopFunc, ApLoopFuncSize);
+  if (!CpuMpData->UseSevEsAPMethod) {
+//
+// processors without SEV-ES and paging is enabled
+//
+mApPageTable = CreatePageTable (
+ (UINTN)Address,
+ EFI_PAGES_TO_SIZE 

[edk2-devel] [PATCH 07/18] UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume

2024-05-10 Thread duntan
Install gEdkiiEndOfS3ResumeGuid in S3Resume to trigger
callback registered by PeiMpLib. The callback is to
relocate Ap to new safe memory before jump to OS waking
vector in S3 boot flow.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c | 13 +
 1 file changed, 13 insertions(+)

diff --git a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c 
b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
index a8e9c92a93..98badc74ae 100644
--- a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
+++ b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
@@ -260,6 +260,12 @@ EFI_PEI_PPI_DESCRIPTOR  mPpiListS3SmmInitDoneTable = {
   0
 };
 
+EFI_PEI_PPI_DESCRIPTOR  mPpiListEndOfS3ResumeTable = {
+  (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
+  ,
+  0
+};
+
 //
 // Global Descriptor Table (GDT)
 //
@@ -490,6 +496,13 @@ S3ResumeBootOs (
   PERF_INMODULE_BEGIN ("EndOfS3Resume");
 
   DEBUG ((DEBUG_INFO, "Signal EndOfS3Resume\n"));
+
+  //
+  // Install EndOfS3Resume.
+  //
+  Status = PeiServicesInstallPpi ();
+  ASSERT_EFI_ERROR (Status);
+
   //
   // Signal EndOfS3Resume to SMM.
   //
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118799): https://edk2.groups.io/g/devel/message/118799
Mute This Topic: https://groups.io/mt/106018131/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 06/18] UefiCpuPkg: Remove the duplicated mpservice locate

2024-05-10 Thread duntan
Remove the duplicated mpservice locate in the code
logic. Previously, when the execution combination
of PEI and DXE are the same, the pointer of mpservice
ppi will be passed to CpuS3.c in smm cpu driver to
wakeup all APs, instead of init-sipi-sipi. Currently,
CpuS3.c doesn't need to wakeup Aps anymore. So remove
the duplicated mpservice locate and the assignment to
MpService2Ppi field in SmmS3ResumeState.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c | 14 --
 1 file changed, 14 deletions(-)

diff --git a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c 
b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
index 3e85eab28f..a8e9c92a93 100644
--- a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
+++ b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
@@ -1139,7 +1139,6 @@ S3RestoreConfig2 (
 SmmS3ResumeState->ReturnContext1 = 
(EFI_PHYSICAL_ADDRESS)(UINTN)AcpiS3Context;
 SmmS3ResumeState->ReturnContext2 = 
(EFI_PHYSICAL_ADDRESS)(UINTN)EfiBootScriptExecutorVariable;
 SmmS3ResumeState->ReturnStackPointer = 
(EFI_PHYSICAL_ADDRESS)STACK_ALIGN_DOWN ();
-SmmS3ResumeState->MpService2Ppi  = 0;
 
 DEBUG ((DEBUG_INFO, "SMM S3 Signature= %x\n", 
SmmS3ResumeState->Signature));
 DEBUG ((DEBUG_INFO, "SMM S3 Stack Base   = %x\n", 
SmmS3ResumeState->SmmS3StackBase));
@@ -1168,19 +1167,6 @@ S3RestoreConfig2 (
 if (((SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_32) && (sizeof 
(UINTN) == sizeof (UINT32))) ||
 ((SmmS3ResumeState->Signature == SMM_S3_RESUME_SMM_64) && (sizeof 
(UINTN) == sizeof (UINT64
 {
-  //
-  // Get MP Services2 Ppi to pass it to Smm S3.
-  //
-  Status = PeiServicesLocatePpi (
- ,
- 0,
- NULL,
- (VOID **)
- );
-  ASSERT_EFI_ERROR (Status);
-  SmmS3ResumeState->MpService2Ppi = 
(EFI_PHYSICAL_ADDRESS)(UINTN)MpService2Ppi;
-  DEBUG ((DEBUG_INFO, "SMM S3 MpService2Ppi Point = %lx\n", 
SmmS3ResumeState->MpService2Ppi));
-
   SwitchStack (
 
(SWITCH_STACK_ENTRY_POINT)(UINTN)SmmS3ResumeState->SmmS3ResumeEntryPoint,
 (VOID *)AcpiS3Context,
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118798): https://edk2.groups.io/g/devel/message/118798
Mute This Topic: https://groups.io/mt/106018127/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 05/18] UefiCpuPkg: LoadMtrrData for all cpu in S3Resume

2024-05-10 Thread duntan
In this commit, S3Resume.c wakeup all Aps to run
LoadMtrrData for all cpu before transfer to CpuS3.c
in smm cpu driver. The MtrrSetting table can be restored
by gEdkiiS3MtrrSettingGuid which is saved by lockbox in
PEI phase. This can avoid waking up APs in CpuS3.c.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c   | 49 
+
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf |  2 ++
 2 files changed, 51 insertions(+)

diff --git a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c 
b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
index 4cf676fb3e..3e85eab28f 100644
--- a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
+++ b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c
@@ -39,6 +39,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -938,6 +939,20 @@ S3ResumeExecuteBootScript (
   CpuDeadLoop ();
 }
 
+/**
+  Sync up the MTRR values for all processors.
+
+  @param[in] MtrrTable  Address of MTRR setting.
+**/
+VOID
+EFIAPI
+LoadMtrrData (
+  IN VOID  *MtrrTable
+  )
+{
+  MtrrSetAllMtrrs (MtrrTable);
+}
+
 /**
   Restores the platform to its preboot configuration for an S3 resume and
   jumps to the OS waking vector.
@@ -990,6 +1005,7 @@ S3RestoreConfig2 (
   BOOLEANInterruptStatus;
   IA32_CR0   Cr0;
   EDKII_PEI_MP_SERVICES2_PPI *MpService2Ppi;
+  MTRR_SETTINGS  MtrrTable;
 
   TempAcpiS3Context = 0;
   TempEfiBootScriptExecutorVariable = 0;
@@ -1082,6 +1098,39 @@ S3RestoreConfig2 (
   Status = SmmAccess->Open ((EFI_PEI_SERVICES 
**)GetPeiServicesTablePointer (), SmmAccess, Index);
 }
 
+//
+// Get MP Services2 Ppi to pass it to Smm S3.
+//
+Status = PeiServicesLocatePpi (
+   ,
+   0,
+   NULL,
+   (VOID **)
+   );
+ASSERT_EFI_ERROR (Status);
+
+//
+// Restore MTRR setting
+//
+VarSize = sizeof (MTRR_SETTINGS);
+Status  = RestoreLockBox (
+,
+,
+
+);
+ASSERT_EFI_ERROR (Status);
+
+//
+// Sync up the MTRR values for all processors.
+//
+Status = MpService2Ppi->StartupAllCPUs (
+  MpService2Ppi,
+  (EFI_AP_PROCEDURE)LoadMtrrData,
+  0,
+  (VOID *)
+  );
+ASSERT_EFI_ERROR (Status);
+
 SmramDescriptor  = (EFI_SMRAM_DESCRIPTOR *)GET_GUID_HOB_DATA (GuidHob);
 SmmS3ResumeState = (SMM_S3_RESUME_STATE *)(UINTN)SmramDescriptor->CpuStart;
 
diff --git a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf 
b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf
index 9c9b6f3db3..890c588aa8 100644
--- a/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf
+++ b/UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf
@@ -67,6 +67,7 @@
   LocalApicLib
   ReportStatusCodeLib
   LockBoxLib
+  MtrrLib
 
 [Guids]
   gEfiBootScriptExecutorVariableGuid## SOMETIMES_CONSUMES ## 
UNDEFINED # LockBox
@@ -79,6 +80,7 @@
   ## SOMETIMES_PRODUCES ## UNDEFINED # Install PPI
   ## SOMETIMES_CONSUMES ## UNDEFINED # Used to do smm communication
   gEdkiiS3SmmInitDoneGuid
+  gEdkiiS3MtrrSettingGuid
 
 [Ppis]
   gEfiPeiS3Resume2PpiGuid   ## PRODUCES
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118797): https://edk2.groups.io/g/devel/message/118797
Mute This Topic: https://groups.io/mt/106018126/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 04/18] UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe

2024-05-10 Thread duntan
Save MTRR by lockbox in CpuS3DataDxe. In S3 boot,
The MTRR setting will be restored in S3Resume.c
in following patches. Then S3Resume.c will wakeup
all APs to load the MTRR setting. This can avoid
waking up APs in CpuS3.c.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c  | 11 +++
 UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf |  2 ++
 2 files changed, 13 insertions(+)

diff --git a/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c 
b/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
index 86ce5303ca..825bb00b74 100644
--- a/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
+++ b/UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c
@@ -26,6 +26,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -130,6 +131,16 @@ CpuS3DataOnEndOfDxe (
   DEBUG ((DEBUG_VERBOSE, "%a\n", __func__));
   MtrrGetAllMtrrs (>MtrrTable);
 
+  //
+  // Save MTRR in lockbox
+  //
+  Status = SaveLockBox (
+ ,
+ >MtrrTable,
+ sizeof (MTRR_SETTINGS)
+ );
+  ASSERT_EFI_ERROR (Status);
+
   //
   // Close event, so it will not be invoked again.
   //
diff --git a/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf 
b/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
index 510133a614..a09254746b 100644
--- a/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
+++ b/UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf
@@ -46,9 +46,11 @@
   BaseLib
   MtrrLib
   MemoryAllocationLib
+  LockBoxLib
 
 [Guids]
   gEfiEndOfDxeEventGroupGuid ## CONSUMES   ## Event
+  gEdkiiS3MtrrSettingGuid
 
 [Protocols]
   gEfiMpServiceProtocolGuid  ## CONSUMES
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118796): https://edk2.groups.io/g/devel/message/118796
Mute This Topic: https://groups.io/mt/106018124/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 03/18] UefiCpuPkg: Add locbox lib instance in DSC

2024-05-10 Thread duntan
Add locbox lib instance in DSC. The SmmLockBoxDxeLib
will be consumed by CpuS3DataDxe driver

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Jiaxin Wu 
---
 UefiCpuPkg/UefiCpuPkg.dsc | 1 +
 1 file changed, 1 insertion(+)

diff --git a/UefiCpuPkg/UefiCpuPkg.dsc b/UefiCpuPkg/UefiCpuPkg.dsc
index 3d49f72588..b113ae022f 100644
--- a/UefiCpuPkg/UefiCpuPkg.dsc
+++ b/UefiCpuPkg/UefiCpuPkg.dsc
@@ -68,6 +68,7 @@
   UnitTestLib|UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLib.inf
   
UnitTestPersistenceLib|UnitTestFrameworkPkg/Library/UnitTestPersistenceLibNull/UnitTestPersistenceLibNull.inf
   
UnitTestResultReportLib|UnitTestFrameworkPkg/Library/UnitTestResultReportLib/UnitTestResultReportLibDebugLib.inf
+  LockBoxLib|MdeModulePkg/Library/SmmLockBoxLib/SmmLockBoxDxeLib.inf
 
 [LibraryClasses.common.SEC]
   PlatformSecLib|UefiCpuPkg/Library/PlatformSecLibNull/PlatformSecLibNull.inf
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118795): https://edk2.groups.io/g/devel/message/118795
Mute This Topic: https://groups.io/mt/106018123/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 02/18] OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe

2024-05-10 Thread duntan
Save MTRR by lockbox in CpuS3DataDxe. In S3 boot,
The MTRR setting will be restored in S3Resume.c
in following patches. Then S3Resume.c will wakeup
all APs to load the MTRR setting. This can avoid
waking up APs in CpuS3.c.

Signed-off-by: Dun Tan 
Cc: Ard Biesheuvel 
Cc: Jiewen Yao 
Cc: Gerd Hoffmann 
Cc: Ray Ni 
Cc: Jiaxin Wu 
---
 OvmfPkg/CpuS3DataDxe/CpuS3Data.c  | 11 +++
 OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf |  2 ++
 2 files changed, 13 insertions(+)

diff --git a/OvmfPkg/CpuS3DataDxe/CpuS3Data.c b/OvmfPkg/CpuS3DataDxe/CpuS3Data.c
index 289048b75d..d1aba32842 100644
--- a/OvmfPkg/CpuS3DataDxe/CpuS3Data.c
+++ b/OvmfPkg/CpuS3DataDxe/CpuS3Data.c
@@ -26,6 +26,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include 
 #include 
 #include 
+#include 
 
 #include 
 #include 
@@ -130,6 +131,16 @@ CpuS3DataOnEndOfDxe (
   DEBUG ((DEBUG_VERBOSE, "%a\n", __func__));
   MtrrGetAllMtrrs (>MtrrTable);
 
+  //
+  // Save MTRR in lockbox
+  //
+  Status = SaveLockBox (
+ ,
+ >MtrrTable,
+ sizeof (MTRR_SETTINGS)
+ );
+  ASSERT_EFI_ERROR (Status);
+
   //
   // Close event, so it will not be invoked again.
   //
diff --git a/OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf 
b/OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf
index 228d5ae1b2..f5032a9222 100644
--- a/OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf
+++ b/OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf
@@ -46,9 +46,11 @@
   MtrrLib
   UefiBootServicesTableLib
   UefiDriverEntryPoint
+  LockBoxLib
 
 [Guids]
   gEfiEndOfDxeEventGroupGuid ## CONSUMES   ## Event
+  gEdkiiS3MtrrSettingGuid
 
 [Protocols]
   gEfiMpServiceProtocolGuid  ## CONSUMES
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118794): https://edk2.groups.io/g/devel/message/118794
Mute This Topic: https://groups.io/mt/106018122/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 01/18] MdeModulePkg: Add gEdkiiS3MtrrSettingGuid

2024-05-10 Thread duntan
Add gEdkiiS3MtrrSettingGuid a new GUID for s3
MTRR setting. This GUID will be used to save
MTRR_SETTINGS at EndOfDxe by LockBox and restore
at S3 boot PEI phase for s3 usage.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Liming Gao 
Cc: Jiaxin Wu 
---
 MdeModulePkg/MdeModulePkg.dec | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec
index f7339f0aec..2c50d90e94 100644
--- a/MdeModulePkg/MdeModulePkg.dec
+++ b/MdeModulePkg/MdeModulePkg.dec
@@ -465,6 +465,9 @@
   gEdk2JedecSfdpSpiDxeDriverGuid  = { 0xBE71701E, 0xB63C, 0x4574, { 0x9C, 
0x5C, 0x36, 0x29, 0xE8, 0xEA, 0xC4, 0x14 }}
   gEdk2JedecSfdpSpiSmmDriverGuid  = { 0x95A1E915, 0x195C, 0x477C, { 0x92, 
0x6F, 0x7E, 0x24, 0x67, 0xC1, 0xB3, 0x1F }}
 
+  ## This GUID will be used to save MTRR_SETTINGS at EndOfDxe by LockBox and 
restore at S3 boot PEI phase for s3 usage.
+  gEdkiiS3MtrrSettingGuid = { 0xd77baa84, 0xb332, 0x4463, { 0x9f, 0x1d, 0xce, 
0x81, 0x00, 0xfe, 0x7f, 0x35 }}
+
 [Ppis]
   ## Include/Ppi/FirmwareVolumeShadowPpi.h
   gEdkiiPeiFirmwareVolumeShadowPpiGuid = { 0x7dfe756c, 0xed8d, 0x4d77, {0x9e, 
0xc4, 0x39, 0x9a, 0x8a, 0x81, 0x51, 0x16 } }
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118793): https://edk2.groups.io/g/devel/message/118793
Mute This Topic: https://groups.io/mt/106018120/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 00/19] Remove some S3 related code in CpuS3.c of smm cpu driver

2024-05-10 Thread duntan
This patch set is to remove some S3 related code in CpuS3.c of smm cpu driver. 
It contain commits to:
1.Save MTRR setting by lockbox in PEI phase 
2.Load MTRR in S3Resume.c before transferring to CpuS3.c in smm cpu driver.
3.Add callback of gEdkiiEndOfS3ResumeGuid in PeiMpLib to relocate Ap to new 
safe buffer.
4.Install gEdkiiEndOfS3ResumeGuid in S3Resume.c before booting OS to relocate 
APs
4.Change PcdCpuFeaturesInitOnS3Resume to TRUE to set register table in 
CpuFeaturesPei
5.Remove code to set register table/load mtrr/wakeup AP and relocate ap in 
CpuS3.c.

With this patch set, CpuS3.c in smm CPU driver can be simplified.

Dun Tan (18):
  MdeModulePkg: Add gEdkiiS3MtrrSettingGuid
  OvmfPkg: Save MTRR by lockbox in CpuS3DataDxe
  UefiCpuPkg: Add locbox lib instance in DSC
  UefiCpuPkg: Save MTRR by lockbox in CpuS3DataDxe
  UefiCpuPkg: LoadMtrrData for all cpu in S3Resume
  UefiCpuPkg: Remove the duplicated mpservice locate
  UefiCpuPkg: Install gEdkiiEndOfS3ResumeGuid in S3Resume
  UefiCpuPkg:Abstract some DxeMpLib code to function
  UefiCpuPkg:Move some code in DxeMpLib to common place
  UefiCpuPkg:Relocate AP to new safe buffer in PeiMpLib
  UefiCpuPkg: Disable PG in IA32 ApLoopCode
  UefiCpuPkg: Remove code to load mtrr setting
  UefiCpuPkg:Set PcdCpuFeaturesInitOnS3Resume to TRUE
  UefiCpuPkg: Remove code to set register table
  UefiCpuPkg:Remove code to handle APIC setting and Interrupt
  UefiCpuPkg:Rremove code to wakeup AP and relocate ap
  UefiCpuPkg: Remove GetAcpiCpuData() in CpuS3.c
  MdeModulePkg: remove MpService2Ppi field in SMM_S3_RESUME_STATE

Ray Ni (1):
  MdePkg: Add MmUnblockMemoryLib to MdeLibs.dsc

 MdeModulePkg/Include/Guid/AcpiS3Context.h   |   1 -
 MdeModulePkg/MdeModulePkg.dec   |   3 +++
 MdePkg/MdeLibs.dsc.inc  |   3 ++-
 OvmfPkg/CpuS3DataDxe/CpuS3Data.c|  11 +++
 OvmfPkg/CpuS3DataDxe/CpuS3DataDxe.inf   |   2 ++
 UefiCpuPkg/CpuS3DataDxe/CpuS3Data.c |  11 +++
 UefiCpuPkg/CpuS3DataDxe/CpuS3DataDxe.inf|   2 ++
 UefiCpuPkg/Library/MpInitLib/DxeMpLib.c | 181 
+++
 UefiCpuPkg/Library/MpInitLib/Ia32/MpFuncs.nasm  |   4 
 UefiCpuPkg/Library/MpInitLib/MpLib.c| 142 
++
 UefiCpuPkg/Library/MpInitLib/MpLib.h|  54 
++-
 UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf   |   4 
 UefiCpuPkg/Library/MpInitLib/PeiMpLib.c | 152 

 UefiCpuPkg/PiSmmCpuDxeSmm/CpuS3.c   | 991 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/MpFuncs.nasm | 153 
-
 UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/SmmFuncsArch.c   |  27 
--
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c  |   6 ++
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h  |  24 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf|   3 ---
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/MpFuncs.nasm  | 189 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/SmmFuncsArch.c|  28 
---
 UefiCpuPkg/UefiCpuPkg.dec   |   2 +-
 UefiCpuPkg/UefiCpuPkg.dsc   |   1 +
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume.c   |  76 
++--
 UefiCpuPkg/Universal/Acpi/S3Resume2Pei/S3Resume2Pei.inf |   2 ++
 25 files changed, 511 insertions(+), 1561 deletions(-)
 delete mode 100644 UefiCpuPkg/PiSmmCpuDxeSmm/Ia32/MpFuncs.nasm
 delete mode 100644 UefiCpuPkg/PiSmmCpuDxeSmm/X64/MpFuncs.nasm

-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118792): https://edk2.groups.io/g/devel/message/118792
Mute This Topic: https://groups.io/mt/106018119/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: 

[edk2-devel] [edk2-platforms V2 3/3] MinPlatformPkg: Sort ApicIdOrderTable by special rules

2024-04-01 Thread duntan
Sort ApicIdOrderTable by following special rules:
1. Make sure BSP is the first entry.
2. For APs, big core first, then small core.

With this implementation, BIOS can present cores in order
of relative performance in MADT. Linux OS would schedule
cores by the order that they are presented in the MADT
LocalX2ApicStruct entries.Then Linux OS would think of
this as relative performance order. This implementation
can benefit the linux os usage case.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Jason Lou 
Cc: Chasel Chiu 
Cc: Nate DeSimone 
Cc: Liming Gao 
Cc: Eric Dong 
---
 Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c | 63 
+++
 1 file changed, 63 insertions(+)

diff --git a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c 
b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
index 1fa70e3df9..389df48824 100644
--- a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
+++ b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
@@ -132,6 +132,62 @@ AppendCpuMapTableEntry (
 
 }
 
+/**
+  Sort CpuApicIdOrderTable based on the following rules:
+  1.Make sure BSP is the first entry.
+  2.Big core first, then small core.
+
+  @param[in] CpuApicIdOrderTable  Pointer to EFI_CPU_ID_ORDER_MAP
+  @param[in] CountNumber to EFI_CPU_ID_ORDER_MAP
+  @param[in] BspIndex BSP index
+**/
+VOID
+SortApicIdOrderTable (
+  IN  EFI_CPU_ID_ORDER_MAP  *CpuApicIdOrderTable,
+  IN  UINTN Count,
+  IN  UINTN BspIndex
+  )
+{
+  UINTN Index;
+  UINTN SubIndex;
+  EFI_CPU_ID_ORDER_MAP  SortBuffer;
+
+  //
+  // Put BSP at the first entry.
+  //
+  if (BspIndex != 0) {
+CopyMem (, [BspIndex], sizeof 
(EFI_CPU_ID_ORDER_MAP));
+CopyMem ([1], CpuApicIdOrderTable, (BspIndex) * sizeof 
(EFI_CPU_ID_ORDER_MAP));
+CopyMem (CpuApicIdOrderTable, , sizeof (EFI_CPU_ID_ORDER_MAP));
+  }
+
+  //
+  // If there are more than 2 cores, perform insertion sort for rest cores 
except the bsp in first entry
+  // to move big cores in front of small cores.
+  // Also the original order based on the MpService index inside big cores and 
small cores are retained.
+  //
+  for (Index = 2; Index < Count; Index++) {
+if (CpuApicIdOrderTable[Index].CoreType == CPUID_CORE_TYPE_INTEL_ATOM) {
+  continue;
+}
+
+CopyMem (, [Index], sizeof 
(EFI_CPU_ID_ORDER_MAP));
+
+for (SubIndex = Index - 1; SubIndex >= 1; SubIndex--) {
+  if (CpuApicIdOrderTable[SubIndex].CoreType == 
CPUID_CORE_TYPE_INTEL_ATOM) {
+CopyMem ([SubIndex + 1], 
[SubIndex], sizeof (EFI_CPU_ID_ORDER_MAP));
+  } else {
+//
+// Except the BSP, all cores in front of SubIndex must be big cores.
+//
+break;
+  }
+}
+
+CopyMem ([SubIndex + 1], , sizeof 
(EFI_CPU_ID_ORDER_MAP));
+  }
+}
+
 /**
   Get CPU core type.
 
@@ -174,6 +230,7 @@ CreateCpuLocalApicInTable (
   EFI_CPU_ID_ORDER_MAP  *CpuIdMapPtr;
   UINT32Socket;
   UINT32CpuidMaxInput;
+  UINTN BspIndex;
 
   Status = EFI_SUCCESS;
 
@@ -198,6 +255,10 @@ CreateCpuLocalApicInTable (

);
 
+if ((ProcessorInfoBuffer.StatusFlag & PROCESSOR_AS_BSP_BIT) != 0) {
+  BspIndex = Index;
+}
+
 CpuIdMapPtr = (EFI_CPU_ID_ORDER_MAP *) [Index];
 if ((ProcessorInfoBuffer.StatusFlag & PROCESSOR_ENABLED_BIT) != 0) {
   CpuIdMapPtr->ApicId  = (UINT32)ProcessorInfoBuffer.ProcessorId;
@@ -230,6 +291,8 @@ CreateCpuLocalApicInTable (
 }
   }
 
+  SortApicIdOrderTable (CpuApicIdOrderTable, mNumberOfCpus, BspIndex);
+
   DEBUG ((DEBUG_INFO, "::ACPI::  APIC ID Order Table Init.   mNumOfBitShift = 
%x\n", mNumOfBitShift));
   DebugDisplayReOrderTable (CpuApicIdOrderTable);
 
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117281): https://edk2.groups.io/g/devel/message/117281
Mute This Topic: https://groups.io/mt/105259125/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [edk2-platforms V2 2/3] MinPlatformPkg: Get CoreType for all cores

2024-04-01 Thread duntan
Add a new field CoreType in EFI_CPU_ID_ORDER_MAP
and get CoreType for all cores.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Jason Lou 
Cc: Chasel Chiu 
Cc: Nate DeSimone 
Cc: Liming Gao 
Cc: Eric Dong 
---
 Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c | 50 
++
 1 file changed, 46 insertions(+), 4 deletions(-)

diff --git a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c 
b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
index 62a7da290a..1fa70e3df9 100644
--- a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
+++ b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
@@ -18,6 +18,7 @@ typedef struct {
   UINT32   Flags;
   UINT32   SocketNum;
   UINT32   Thread;
+  UINT8CoreType;
 } EFI_CPU_ID_ORDER_MAP;
 
 //
@@ -71,15 +72,16 @@ DebugDisplayReOrderTable (
 {
   UINT32 Index;
 
-  DEBUG ((DEBUG_INFO, "Index  AcpiProcId  ApicId   Thread  Flags   Skt\n"));
+  DEBUG ((DEBUG_INFO, "Index  AcpiProcId  ApicId   Thread  Flags   Skt  
CoreType\n"));
   for (Index = 0; Index < mNumberOfCpus; Index++) {
-DEBUG ((DEBUG_INFO, " %02d   0x%02X  0x%02X   %d  %d  
%d\n",
+DEBUG ((DEBUG_INFO, " %02d   0x%02X  0x%02X   %d  %d  
%d  0x%x\n",
Index,
CpuApicIdOrderTable[Index].AcpiProcessorUid,
CpuApicIdOrderTable[Index].ApicId,
CpuApicIdOrderTable[Index].Thread,
CpuApicIdOrderTable[Index].Flags,
-   CpuApicIdOrderTable[Index].SocketNum));
+   CpuApicIdOrderTable[Index].SocketNum,
+   CpuApicIdOrderTable[Index].CoreType));
   }
 }
 
@@ -130,6 +132,31 @@ AppendCpuMapTableEntry (
 
 }
 
+/**
+  Get CPU core type.
+
+  @param[in] CpuApicIdOrderTable Point to a buffer which will be 
filled in Core type information.
+**/
+VOID
+EFIAPI
+CollectCpuCoreType (
+  IN EFI_CPU_ID_ORDER_MAP  *CpuApicIdOrderTable
+  )
+{
+  UINTNApNumber;
+  EFI_STATUS   Status;
+  CPUID_NATIVE_MODEL_ID_AND_CORE_TYPE_EAX  NativeModelIdAndCoreTypeEax;
+
+  Status = mMpService->WhoAmI (
+ mMpService,
+ 
+ );
+  ASSERT_EFI_ERROR (Status);
+
+  AsmCpuidEx (CPUID_HYBRID_INFORMATION, CPUID_HYBRID_INFORMATION_MAIN_LEAF, 
, NULL, NULL, NULL);
+  CpuApicIdOrderTable[ApNumber].CoreType = 
(UINT8)NativeModelIdAndCoreTypeEax.Bits.CoreType;
+}
+
 /**
   Collect all processors information and create a Cpu Apic Id table.
 
@@ -146,8 +173,23 @@ CreateCpuLocalApicInTable (
   UINT32CurrProcessor;
   EFI_CPU_ID_ORDER_MAP  *CpuIdMapPtr;
   UINT32Socket;
+  UINT32CpuidMaxInput;
 
-  Status = EFI_SUCCESS;
+  Status = EFI_SUCCESS;
+
+  AsmCpuid (CPUID_SIGNATURE, , NULL, NULL, NULL);
+  if (CpuidMaxInput >= CPUID_HYBRID_INFORMATION) {
+CollectCpuCoreType (CpuApicIdOrderTable);
+mMpService->StartupAllAPs (
+  mMpService,   // This
+  (EFI_AP_PROCEDURE) CollectCpuCoreType,// Procedure
+  TRUE, // SingleThread
+  NULL, // WaitEvent
+  0,// 
TimeoutInMicrosecsond
+  CpuApicIdOrderTable,  // 
ProcedureArgument
+  NULL  // FailedCpuList
+  );
+  }
 
   for (CurrProcessor = 0, Index = 0; CurrProcessor < mNumberOfCpus; 
CurrProcessor++, Index++) {
 Status = mMpService->GetProcessorInfo (
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117280): https://edk2.groups.io/g/devel/message/117280
Mute This Topic: https://groups.io/mt/105259124/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [edk2-platforms V2 1/3] MinPlatformPkg: Remove the global variable mForceX2ApicId

2024-04-01 Thread duntan
This global variable mForceX2ApicId is not assigned
to any value in code and will be initialized to 0 when
the driver is loaded. So remove the global variable
and related code that will not be executed.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Jason Lou 
Cc: Chasel Chiu 
Cc: Nate DeSimone 
Cc: Liming Gao 
Cc: Eric Dong 
---
 Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c | 10 --
 1 file changed, 10 deletions(-)

diff --git a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c 
b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
index 2a833ec99c..62a7da290a 100644
--- a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
+++ b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
@@ -53,7 +53,6 @@ VOID  *mLocalTable[] = {
 EFI_ACPI_TABLE_PROTOCOL *mAcpiTable;
 
 UINT32  mNumOfBitShift = 6;
-BOOLEAN mForceX2ApicId;
 BOOLEAN mX2ApicEnabled;
 
 EFI_MP_SERVICES_PROTOCOL*mMpService;
@@ -163,14 +162,6 @@ CreateCpuLocalApicInTable (
   CpuIdMapPtr->Thread  = ProcessorInfoBuffer.Location.Thread;
   CpuIdMapPtr->Flags   = ((ProcessorInfoBuffer.StatusFlag & 
PROCESSOR_ENABLED_BIT) != 0);
   CpuIdMapPtr->SocketNum = ProcessorInfoBuffer.Location.Package;
-
-  //update processorbitMask
-  if (CpuIdMapPtr->Flags == 1) {
-if (mForceX2ApicId) {
-  CpuIdMapPtr->SocketNum&= 0x7;
-  CpuIdMapPtr->AcpiProcessorUid &= 0xFF; //keep lower 8bit due to use 
Proc obj in dsdt
-}
-  }
 } else {  //not enabled
   CpuIdMapPtr->ApicId = (UINT32)-1;
   CpuIdMapPtr->Thread = (UINT32)-1;
@@ -1537,7 +1528,6 @@ InstallAcpiPlatform (
   }
 
   DEBUG ((DEBUG_INFO, "mX2ApicEnabled - 0x%x\n", mX2ApicEnabled));
-  DEBUG ((DEBUG_INFO, "mForceX2ApicId - 0x%x\n", mForceX2ApicId));
 
   // support up to 64 threads/socket
   AsmCpuidEx (CPUID_EXTENDED_TOPOLOGY, 1, , NULL, NULL, NULL);
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117279): https://edk2.groups.io/g/devel/message/117279
Mute This Topic: https://groups.io/mt/105259123/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [edk2-platforms V2 0/3] MinPlatformPkg: Sort ApicIdOrderTable by special rules

2024-04-01 Thread duntan
This patch set is to sort ApicIdOrderTable by following special rules:
1. Make sure BSP is the first entry.
2. For APs, big core first, then small core.

With this implementation, BIOS can present cores in order of relative 
performance in MADT.
Linux OS would schedule cores by the order that they are presented in the MADT 
LocalX2ApicStruct entries. Then Linux OS would think of this as relative 
performance order.
This implementation can benefit the linux os usage case.

Dun Tan (3):
  MinPlatformPkg: Remove the global variable mForceX2ApicId
  MinPlatformPkg: Get CoreType for all cores
  MinPlatformPkg: Sort ApicIdOrderTable by special rules

 Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c | 123 
+--
 1 file changed, 109 insertions(+), 14 deletions(-)

-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117278): https://edk2.groups.io/g/devel/message/117278
Mute This Topic: https://groups.io/mt/105259122/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [edk2-platforms V2] MinPlatformPkg: Corret AcpiProcessorUid in MADT

2024-03-31 Thread duntan
Change AcpiProcessorUid in CpuApicIdOrderTable to
the index in MpService regardless of disabled core.

Let's take a simple example:
There are 2 enabled cores and 1 disabled core.
  APICID   Index in MpService
0x_0
0x_0010(disbaled)  1
0x_00402

With current implementation, the results of the x2apic
structure entries in MADT are:
MADT Entry  APICID AcpiProcessorUid
0 0x_  0
1 0x_0040  1

The AcpiProcessorUid will be associated with the
processor device defined in DSDT/SSDT. To make sure
the x2apic structure entries in MADT can be mapped to
the right processor devices in DSDT, the AcpiProcessorUid
of the second x2apic structure entry in MADT should
be kept as 2, which is the core index in MpService.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Jason Lou 
Cc: Chasel Chiu 
Cc: Nate DeSimone 
Cc: Liming Gao 
Cc: Eric Dong 
---
 Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c 
b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
index 2a833ec99c..f6ea7454d3 100644
--- a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
+++ b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
@@ -190,7 +190,7 @@ CreateCpuLocalApicInTable (
   //
   for (Socket = 0; Socket < FixedPcdGet32 (PcdMaxCpuSocketCount); Socket++) {
 for (CurrProcessor = 0, Index = 0; CurrProcessor < mNumberOfCpus; 
CurrProcessor++) {
-  if (CpuApicIdOrderTable[CurrProcessor].Flags && 
(CpuApicIdOrderTable[CurrProcessor].SocketNum == Socket)) {
+  if (CpuApicIdOrderTable[CurrProcessor].SocketNum == Socket) {
 CpuApicIdOrderTable[CurrProcessor].AcpiProcessorUid = 
(CpuApicIdOrderTable[CurrProcessor].SocketNum << mNumOfBitShift) + Index;
 Index++;
   }
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117277): https://edk2.groups.io/g/devel/message/117277
Mute This Topic: https://groups.io/mt/105259071/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [edk2-platforms] MinPlatformPkg: Corret AcpiProcessorUid in MADT

2024-03-28 Thread duntan
Change AcpiProcessorUid in CpuApicIdOrderTable to
the Index value in MP service regardless of diabled
fore.

Let take a simple example:
CORE number   APICID   Index in MpService
CORE0   0x_   0
CORE1(Disabled) 0x_0010   1
CORE2   0x_0040   2

With current implementation, the results of the x2apic
structure entries in MADT are:
MADT Entry APICID  AcpiProcessorUid
Entry[0](core0)  0x_  0
Entry[0](core2)  0x_0040  1

The ACPI Processor UID will be associated with the
processor device defined in DSDT/SSDT. To make sure
the x2apic structure entry in MADT can be mapped to
the right processor device in DSDT, the AcpiProcessorUid
value should be the Index in MpService 2.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Jason Lou 
Cc: Chasel Chiu 
Cc: Nate DeSimone 
Cc: Liming Gao 
Cc: Eric Dong 
---
 Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c 
b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
index 2a833ec99c..f6ea7454d3 100644
--- a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
+++ b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
@@ -190,7 +190,7 @@ CreateCpuLocalApicInTable (
   //
   for (Socket = 0; Socket < FixedPcdGet32 (PcdMaxCpuSocketCount); Socket++) {
 for (CurrProcessor = 0, Index = 0; CurrProcessor < mNumberOfCpus; 
CurrProcessor++) {
-  if (CpuApicIdOrderTable[CurrProcessor].Flags && 
(CpuApicIdOrderTable[CurrProcessor].SocketNum == Socket)) {
+  if (CpuApicIdOrderTable[CurrProcessor].SocketNum == Socket) {
 CpuApicIdOrderTable[CurrProcessor].AcpiProcessorUid = 
(CpuApicIdOrderTable[CurrProcessor].SocketNum << mNumOfBitShift) + Index;
 Index++;
   }
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#117202): https://edk2.groups.io/g/devel/message/117202
Mute This Topic: https://groups.io/mt/105193259/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [edk2-platforms] MinPlatformPkg: Sort ApicIdOrderTable by special rules

2024-03-28 Thread duntan
Sort ApicIdOrderTable by following special rules:
1. Make sure BSP is the first entry.
2. For APs, big core first, then small core.
3. For APs, if the core type are the same,
   sort them by AcpiProcessorUid.

With this implementation, BIOS can present cores in order
of relative performance in MADT. Linux OS would schedule
cores by the order that they are presented in the MADT
LocalX2ApicStruct entries.Then Linux OS would think of
this as relative performance order. This implementation
can benefit the linux os usage case.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Jason Lou 
Cc: Chasel Chiu 
Cc: Nate DeSimone 
Cc: Liming Gao 
Cc: Eric Dong 
---
 Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c | 113 
+
 1 file changed, 109 insertions(+), 4 deletions(-)

diff --git a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c 
b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
index 2a833ec99c..1216a42eb4 100644
--- a/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
+++ b/Platform/Intel/MinPlatformPkg/Acpi/AcpiTables/AcpiPlatform.c
@@ -18,6 +18,8 @@ typedef struct {
   UINT32   Flags;
   UINT32   SocketNum;
   UINT32   Thread;
+  UINT8CoreType;
+  BOOLEAN  IsBsp;
 } EFI_CPU_ID_ORDER_MAP;
 
 //
@@ -72,15 +74,16 @@ DebugDisplayReOrderTable (
 {
   UINT32 Index;
 
-  DEBUG ((DEBUG_INFO, "Index  AcpiProcId  ApicId   Thread  Flags   Skt\n"));
+  DEBUG ((DEBUG_INFO, "Index  AcpiProcId  ApicId   Thread  Flags   Skt  
CoreType\n"));
   for (Index = 0; Index < mNumberOfCpus; Index++) {
-DEBUG ((DEBUG_INFO, " %02d   0x%02X  0x%02X   %d  %d  
%d\n",
+DEBUG ((DEBUG_INFO, " %02d   0x%02X  0x%02X   %d  %d  
%d  0X%x\n",
Index,
CpuApicIdOrderTable[Index].AcpiProcessorUid,
CpuApicIdOrderTable[Index].ApicId,
CpuApicIdOrderTable[Index].Thread,
CpuApicIdOrderTable[Index].Flags,
-   CpuApicIdOrderTable[Index].SocketNum));
+   CpuApicIdOrderTable[Index].SocketNum,
+   CpuApicIdOrderTable[Index].CoreType));
   }
 }
 
@@ -131,6 +134,82 @@ AppendCpuMapTableEntry (
 
 }
 
+/**
+  Get CPU core type.
+
+  @param[in] CpuApicIdOrderTable Point to a buffer which will be 
filled in Core type information.
+**/
+VOID
+EFIAPI
+CollectCpuCoreType (
+  IN EFI_CPU_ID_ORDER_MAP  *CpuApicIdOrderTable
+  )
+{
+  UINTNApNumber;
+  EFI_STATUS   Status;
+  CPUID_NATIVE_MODEL_ID_AND_CORE_TYPE_EAX  NativeModelIdAndCoreTypeEax;
+
+  Status = mMpService->WhoAmI (
+ mMpService,
+ 
+ );
+  ASSERT_EFI_ERROR (Status);
+
+  AsmCpuidEx (CPUID_HYBRID_INFORMATION, CPUID_HYBRID_INFORMATION_MAIN_LEAF, 
, NULL, NULL, NULL);
+  CpuApicIdOrderTable[ApNumber].CoreType = 
(UINT8)NativeModelIdAndCoreTypeEax.Bits.CoreType;
+}
+
+/**
+  Function to compare 2 EFI_CPU_ID_ORDER_MAP entry based on special rules.
+
+  @param[in] Buffer1pointer to EFI_CPU_ID_ORDER_MAP poiner to 
compare
+  @param[in] Buffer2pointer to second EFI_CPU_ID_ORDER_MAP pointer 
to compare
+
+  @retval 0 Buffer1 equal to Buffer2
+  @retval <0Buffer1 is less than Buffer2
+  @retval >0Buffer1 is greater than Buffer2
+**/
+INTN
+EFIAPI
+ApicIdOrderTableCompare (
+  IN  CONST VOID  *Buffer1,
+  IN  CONST VOID  *Buffer2
+  )
+{
+  //
+  // Make sure BSP is the first entry.
+  //
+  if (((EFI_CPU_ID_ORDER_MAP *)Buffer1)->IsBsp) {
+return -1;
+  } else if (((EFI_CPU_ID_ORDER_MAP *)Buffer2)->IsBsp) {
+return 1;
+  }
+
+  //
+  // Make sure big core first, then small core
+  //
+  if EFI_CPU_ID_ORDER_MAP *)Buffer1)->CoreType == 
CPUID_CORE_TYPE_INTEL_CORE) &&
+  EFI_CPU_ID_ORDER_MAP *)Buffer2)->CoreType == 
CPUID_CORE_TYPE_INTEL_ATOM)))
+  {
+return -1;
+  } else if EFI_CPU_ID_ORDER_MAP *)Buffer1)->CoreType == 
CPUID_CORE_TYPE_INTEL_ATOM) &&
+ EFI_CPU_ID_ORDER_MAP *)Buffer2)->CoreType == 
CPUID_CORE_TYPE_INTEL_CORE)))
+  {
+return 1;
+  }
+
+  //
+  // If both cores are not BSP and the core type are the same, sort them by 
AcpiProcessorUid.
+  //
+  if (((EFI_CPU_ID_ORDER_MAP *)Buffer1)->AcpiProcessorUid < 
((EFI_CPU_ID_ORDER_MAP *)Buffer2)->AcpiProcessorUid) {
+return -1;
+  } else if (((EFI_CPU_ID_ORDER_MAP *)Buffer1)->AcpiProcessorUid > 
((EFI_CPU_ID_ORDER_MAP *)Buffer2)->AcpiProcessorUid) {
+return 1;
+  }
+
+  return 0;
+}
+
 /**
   Collect all processors information and create a Cpu Apic Id table.
 
@@ -147,8 +226,24 @@ CreateCpuLocalApicInTable (
   UINT32CurrProcessor;
   

Re: [edk2-devel] [edk2-stable202402 PATCH 1/2] UefiCpuPkg/PiSmmCpuDxeSmm: distinguish GetSmBase() failure modes

2024-02-19 Thread duntan
Laszlo,

Sorry for the late reply. Thanks for your code refactoring patch and bugfix 
patch!

Thanks,
Dun

-Original Message-
From: Laszlo Ersek  
Sent: Thursday, February 15, 2024 4:45 PM
To: Kinney, Michael D ; Leif Lindholm 
; devel@edk2.groups.io; Leif Lindholm 
; Andrew Fish (af...@apple.com) ; 
Gao, Liming 
Cc: Tan, Dun ; Gerd Hoffmann ; Kumar, 
Rahul R ; Ni, Ray 
Subject: Re: [edk2-devel] [edk2-stable202402 PATCH 1/2] 
UefiCpuPkg/PiSmmCpuDxeSmm: distinguish GetSmBase() failure modes

On 2/14/24 18:26, Kinney, Michael D wrote:
> Merged: https://github.com/tianocore/edk2/pull/5373

Thanks!
Laszlo

> 
>> -Original Message-
>> From: Leif Lindholm 
>> Sent: Wednesday, February 14, 2024 5:08 AM
>> To: devel@edk2.groups.io; Kinney, Michael D 
>> ; ler...@redhat.com; Leif Lindholm 
>> ; Andrew Fish (af...@apple.com) 
>> ; Gao, Liming 
>> Cc: Tan, Dun ; Gerd Hoffmann ; 
>> Kumar, Rahul R ; Ni, Ray 
>> Subject: Re: [edk2-devel] [edk2-stable202402 PATCH 1/2]
>> UefiCpuPkg/PiSmmCpuDxeSmm: distinguish GetSmBase() failure modes
>>
>> On 2024-02-14 03:43, Michael D Kinney wrote:
>>> Hi Laszlo,
>>>
>>> Thank you for the quick fix.
>>>
>>> I have reviewed the changes.  I agree they fix the issue at hand.
>>>
>>> Reviewed-by: Michael D Kinney 
>>>
>>> I have adjusted the commit message with your suggested changes in 
>>> the PR I have prepared:
>>>
>>> https://github.com/tianocore/edk2/pull/5373
>>>
>>> There may be better ways to organize this code in general to make it 
>>> easier to understand and maintain in the future, but we can let Ray 
>>> review that when he returns.  That will also likely be a much bugger 
>>> change that can be accepted just before a release.
>>>
>>> I also approve this as a critical fix for edk2-stable202402
>>>
>>> I will wait till tomorrow morning my time to see if Gerd and Rahul 
>>> and Leif can also provide their reviews/approvals and to give me 
>>> some time to run some tests.
>>
>> For the series:
>> Reviewed-by: Leif Lindholm  I'm happy for 
>> this to go into the stable tag.
>>
>> /
>>  Leif
>>
>>> I do not expect Ray Ni or Dun Tan to be available this week.
>>>
>>> Best regards,
>>>
>>> Mike
>>>
 -Original Message-
 From: devel@edk2.groups.io  On Behalf Of
>> Laszlo
 Ersek
 Sent: Tuesday, February 13, 2024 1:36 PM
 To: devel@edk2.groups.io
 Cc: Tan, Dun ; Gerd Hoffmann 
 ; Kumar, Rahul R ; Ni, 
 Ray 
 Subject: Re: [edk2-devel] [edk2-stable202402 PATCH 1/2]
 UefiCpuPkg/PiSmmCpuDxeSmm: distinguish GetSmBase() failure modes

 On 2/13/24 22:09, Laszlo Ersek wrote:
> Commit 725acd0b9cc0 ("UefiCpuPkg: Avoid assuming only one
 smmbasehob",
> 2023-12-12) introduced a helper function called GetSmBase(),
 replacing the
> lookup of the first and only "gSmmBaseHobGuid" GUID HOB, with
 iterated
> lookups plus memory allocation.
>
> This introduced a new failure mode for setting
 "mCpuHotPlugData.SmBase".
> Namely, before commit 725acd0b9cc0, "mCpuHotPlugData.SmBase" would
>> be
 set
> to NULL if and only if the GUID HOB was absent. After the commit, 
> a
 NULL
> assignment would be possible if the GUID HOB was absent, *or* one
>> of
 the
> memory allocations inside GetSmBase() failed.

 Sorry, these two paragraphs are not precise. A better version:

 --
 Commit 725acd0b9cc0 ("UefiCpuPkg: Avoid assuming only one
>> smmbasehob",
 2023-12-12) introduced a helper function called GetSmBase(),
>> replacing
 the lookup of the first and only "gSmmBaseHobGuid" GUID HOB and 
 unconditional "mCpuHotPlugData.SmBase" allocation, with iterated 
 lookups plus conditional memory allocation.

 This introduced a new failure mode for setting 
 "mCpuHotPlugData.SmBase".
 Namely, before commit 725acd0b9cc0, "mCpuHotPlugData.SmBase" would
>> be
 allocated regardless of the GUID HOB being absent. After the 
 commit, "mCpuHotPlugData.SmBase" could remain NULL if the GUID HOB 
 was
>> absent,
 *or* one of the memory allocations inside GetSmBase() failed; and 
 in the former case, we'd even proceed to the rest of 
 PiCpuSmmEntry().
 --

 Sorry, it's late.

 If this patch set is accepted otherwise, then Mike or Liming, can
>> you
 please update the first two paragraphs of the commit message upon 
 merge?

 Thanks
 Laszlo

>
> In relation to this conflation of distinct failure modes, commit
> 725acd0b9cc0 actually introduced a NULL pointer dereference.
>> Namely,
 a
> NULL "mCpuHotPlugData.SmBase" is not handled properly at all now.
 We're
> going to fix that NULL pointer dereference in a subsequent patch;
 however,
> as a pre-requisite for that we need to tell apart the failure 
> modes
 of
> GetSmBase().
>
> For memory allocation failures, return EFI_OUT_OF_RESOURCES. Move
>> the

Re: [edk2-devel] [Patch V4] UefiCpuPkg:Limit PhysicalAddressBits in special case

2024-01-12 Thread duntan
Hi Gerd,

I've refined the comments and the commit message. Could you please help to 
review again?

Thanks,
Dun

-Original Message-
From: devel@edk2.groups.io  On Behalf Of duntan
Sent: Friday, January 12, 2024 4:32 PM
To: devel@edk2.groups.io
Cc: Ni, Ray ; Laszlo Ersek ; Kumar, Rahul 
R ; Gerd Hoffmann 
Subject: [edk2-devel] [Patch V4] UefiCpuPkg:Limit PhysicalAddressBits in 
special case

When creating smm page table, limit maximum supported physical addresses bits 
returned by
CalculateMaximumSupportAddress() to 47 if 5-Level Paging is disabled.

This commit is to avoid issue that more than 47-bit physical addresses are 
requested in smm page table when 5-level paging is disabled.
4-level paging supports translating 48-bit linear addresses to 52-bit physical 
addresses.
Since linear addresses are sign-extended, linear-address space of 4-level 
paging is:
[0, 2^47-1] and
[0x8000_, 0x_].
So only [0, 2^47-1] linear-address range maps to the identical physical-address 
range when 5-Level paging is disabled.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 18 --
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
index ddd9be66b5..5964884762 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
@@ -137,11 +137,13 @@ GetSubEntriesNum (
 /**
   Calculate the maximum support address.
 
+  @param[in] Is5LevelPagingNeededIf 5-level paging enabling is needed.
+
   @return the maximum support address.
 **/
 UINT8
 CalculateMaximumSupportAddress (
-  VOID
+  BOOLEAN  Is5LevelPagingNeeded
   )
 {
   UINT32  RegEax;
@@ -164,6 +166,18 @@ CalculateMaximumSupportAddress (
 }
   }
 
+  //
+  // 4-level paging supports translating 48-bit linear addresses to 52-bit 
physical addresses.
+  // Since linear addresses are sign-extended, the linear-address space of 
4-level paging is:
+  // [0, 2^47-1] and [0x8000_, 0x_].
+  // So only [0, 2^47-1] linear-address range maps to the identical 
+ physical-address range when  // 5-Level paging is disabled.
+  //
+  ASSERT (PhysicalAddressBits <= 52);
+  if (!Is5LevelPagingNeeded && (PhysicalAddressBits > 47)) {
+PhysicalAddressBits = 47;
+  }
+
   return PhysicalAddressBits;
 }
 
@@ -197,7 +211,7 @@ SmmInitPageTable (
   mCpuSmmRestrictedMemoryAccess = PcdGetBool (PcdCpuSmmRestrictedMemoryAccess);
   m1GPageTableSupport   = Is1GPageSupport ();
   m5LevelPagingNeeded   = Is5LevelPagingNeeded ();
-  mPhysicalAddressBits  = CalculateMaximumSupportAddress ();
+  mPhysicalAddressBits  = CalculateMaximumSupportAddress 
(m5LevelPagingNeeded);
   PatchInstructionX86 (gPatch5LevelPagingNeeded, m5LevelPagingNeeded, 1);
   if (m5LevelPagingNeeded) {
 mPagingMode = m1GPageTableSupport ? Paging5Level1GB : Paging5Level;
--
2.31.1.windows.1








-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113695): https://edk2.groups.io/g/devel/message/113695
Mute This Topic: https://groups.io/mt/103679520/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V4] UefiCpuPkg:Limit PhysicalAddressBits in special case

2024-01-12 Thread duntan
When creating smm page table, limit maximum
supported physical addresses bits returned by
CalculateMaximumSupportAddress() to 47 if
5-Level Paging is disabled.

This commit is to avoid issue that more than
47-bit physical addresses are requested in smm
page table when 5-level paging is disabled.
4-level paging supports translating 48-bit
linear addresses to 52-bit physical addresses.
Since linear addresses are sign-extended,
linear-address space of 4-level paging is:
[0, 2^47-1] and
[0x8000_, 0x_].
So only [0, 2^47-1] linear-address range maps
to the identical physical-address range when
5-Level paging is disabled.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 18 --
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
index ddd9be66b5..5964884762 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
@@ -137,11 +137,13 @@ GetSubEntriesNum (
 /**
   Calculate the maximum support address.
 
+  @param[in] Is5LevelPagingNeededIf 5-level paging enabling is needed.
+
   @return the maximum support address.
 **/
 UINT8
 CalculateMaximumSupportAddress (
-  VOID
+  BOOLEAN  Is5LevelPagingNeeded
   )
 {
   UINT32  RegEax;
@@ -164,6 +166,18 @@ CalculateMaximumSupportAddress (
 }
   }
 
+  //
+  // 4-level paging supports translating 48-bit linear addresses to 52-bit 
physical addresses.
+  // Since linear addresses are sign-extended, the linear-address space of 
4-level paging is:
+  // [0, 2^47-1] and [0x8000_, 0x_].
+  // So only [0, 2^47-1] linear-address range maps to the identical 
physical-address range when
+  // 5-Level paging is disabled.
+  //
+  ASSERT (PhysicalAddressBits <= 52);
+  if (!Is5LevelPagingNeeded && (PhysicalAddressBits > 47)) {
+PhysicalAddressBits = 47;
+  }
+
   return PhysicalAddressBits;
 }
 
@@ -197,7 +211,7 @@ SmmInitPageTable (
   mCpuSmmRestrictedMemoryAccess = PcdGetBool (PcdCpuSmmRestrictedMemoryAccess);
   m1GPageTableSupport   = Is1GPageSupport ();
   m5LevelPagingNeeded   = Is5LevelPagingNeeded ();
-  mPhysicalAddressBits  = CalculateMaximumSupportAddress ();
+  mPhysicalAddressBits  = CalculateMaximumSupportAddress 
(m5LevelPagingNeeded);
   PatchInstructionX86 (gPatch5LevelPagingNeeded, m5LevelPagingNeeded, 1);
   if (m5LevelPagingNeeded) {
 mPagingMode = m1GPageTableSupport ? Paging5Level1GB : Paging5Level;
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113692): https://edk2.groups.io/g/devel/message/113692
Mute This Topic: https://groups.io/mt/103679520/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V3] UefiCpuPkg:Limit PhysicalAddressBits in special case

2024-01-11 Thread duntan
Hi Gerd,

I explain the reason why 47 here is since virtual addresses are sign-extended 
in the commit message.
About the technical background, I also mentioned in the commit message " When 
5-Level Paging is disabled and the PhysicalAddressBits retrived  from CPU HOB 
or CpuId is bigger than 47". Could you please provide more detailed suggestion 
about the commit message?

Or can we merge the code firstly? Then I'll raise another PR to make the 
comments around the code more detailed as we want.

Thanks,
Dun

-Original Message-
From: Gerd Hoffmann  
Sent: Thursday, January 11, 2024 6:21 PM
To: Tan, Dun 
Cc: devel@edk2.groups.io; Ni, Ray ; Laszlo Ersek 
; Kumar, Rahul R 
Subject: Re: [Patch V3] UefiCpuPkg:Limit PhysicalAddressBits in special case

On Thu, Jan 11, 2024 at 04:59:47PM +0800, Dun Tan wrote:
> When creating smm page table, limit maximum supported physical address 
> bits returned by
> CalculateMaximumSupportAddress() to 47 if 5-Level Paging is disabled.
> When 5-Level Paging is disabled and the PhysicalAddressBits retrived 
> from CPU HOB or CpuId is bigger than 47, and since virtual addresses 
> are sign-extended, only [0, 2^47-1] range in 52-bit physical address 
> is mapped in page table.

> +  //
> +  // Only [0, 2^47 -1] in 52-bit physical addresses is mapped in page 
> + table  // when 5-Level Paging is disabled.
> +  //
> +  ASSERT (PhysicalAddressBits <= 52);  if (!Is5LevelPagingNeeded && 
> + (PhysicalAddressBits > 47)) {
> +PhysicalAddressBits = 47;
> +  }

The code change is fine but the comment should be more verbose and explain the 
why 47 not 48 is used here.  The discussion on the patch clearly showed that 
the technical background is not obvious ...

take care,
  Gerd



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113636): https://edk2.groups.io/g/devel/message/113636
Mute This Topic: https://groups.io/mt/103658816/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V3] UefiCpuPkg:Limit PhysicalAddressBits in special case

2024-01-11 Thread duntan
Hi Gerd,

Could you help to review this V3 patch? The related code and comments has been 
modified based on your previous comments.
Please ignore the V2 patch set. There was a typo error in the V2 patch and was 
corrected in V3 patch.

Thanks,
Dun

-Original Message-
From: devel@edk2.groups.io  On Behalf Of duntan
Sent: Thursday, January 11, 2024 5:00 PM
To: devel@edk2.groups.io
Cc: Ni, Ray ; Laszlo Ersek ; Kumar, Rahul 
R ; Gerd Hoffmann 
Subject: [edk2-devel] [Patch V3] UefiCpuPkg:Limit PhysicalAddressBits in 
special case

When creating smm page table, limit maximum supported physical address bits 
returned by
CalculateMaximumSupportAddress() to 47 if 5-Level Paging is disabled.
When 5-Level Paging is disabled and the
PhysicalAddressBits retrived from CPU HOB or CpuId is bigger than 47, and since 
virtual addresses are sign-extended, only [0, 2^47-1] range in 52-bit physical 
address is mapped in page table.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 15 +--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
index ddd9be66b5..35c282a771 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
@@ -137,11 +137,13 @@ GetSubEntriesNum (
 /**
   Calculate the maximum support address.
 
+  @param[in] Is5LevelPagingNeededIf 5-level paging enabling is needed.
+
   @return the maximum support address.
 **/
 UINT8
 CalculateMaximumSupportAddress (
-  VOID
+  BOOLEAN  Is5LevelPagingNeeded
   )
 {
   UINT32  RegEax;
@@ -164,6 +166,15 @@ CalculateMaximumSupportAddress (
 }
   }
 
+  //
+  // Only [0, 2^47 -1] in 52-bit physical addresses is mapped in page 
+ table  // when 5-Level Paging is disabled.
+  //
+  ASSERT (PhysicalAddressBits <= 52);
+  if (!Is5LevelPagingNeeded && (PhysicalAddressBits > 47)) {
+PhysicalAddressBits = 47;
+  }
+
   return PhysicalAddressBits;
 }
 
@@ -197,7 +208,7 @@ SmmInitPageTable (
   mCpuSmmRestrictedMemoryAccess = PcdGetBool (PcdCpuSmmRestrictedMemoryAccess);
   m1GPageTableSupport   = Is1GPageSupport ();
   m5LevelPagingNeeded   = Is5LevelPagingNeeded ();
-  mPhysicalAddressBits  = CalculateMaximumSupportAddress ();
+  mPhysicalAddressBits  = CalculateMaximumSupportAddress 
(m5LevelPagingNeeded);
   PatchInstructionX86 (gPatch5LevelPagingNeeded, m5LevelPagingNeeded, 1);
   if (m5LevelPagingNeeded) {
 mPagingMode = m1GPageTableSupport ? Paging5Level1GB : Paging5Level;
--
2.31.1.windows.1








-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113605): https://edk2.groups.io/g/devel/message/113605
Mute This Topic: https://groups.io/mt/103658816/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH] UefiCpuPkg: change name of gMpInformationHobGuid2

2024-01-11 Thread duntan
Change name of gMpInformationHobGuid2 to
gMpInformation2HobGuid. It's to align with
the file name MpInformation2.h and the
structure name MP_INFORMATION2_HOB_DATA.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/CpuMpPei/CpuMpPei.c   | 8 
 UefiCpuPkg/CpuMpPei/CpuMpPei.inf | 2 +-
 UefiCpuPkg/Include/Guid/MpInformation2.h | 2 +-
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c   | 6 +++---
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf | 2 +-
 UefiCpuPkg/UefiCpuPkg.dec| 2 +-
 6 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/UefiCpuPkg/CpuMpPei/CpuMpPei.c b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
index 93919be94f..2ce4d6ab50 100644
--- a/UefiCpuPkg/CpuMpPei/CpuMpPei.c
+++ b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
@@ -566,7 +566,7 @@ GetProcessorCoreType (
 }
 
 /**
-  Create gMpInformationHobGuid2.
+  Create gMpInformation2HobGuid.
 **/
 VOID
 BuildMpInformationHob (
@@ -618,13 +618,13 @@ BuildMpInformationHob (
   //
   // Create MP_INFORMATION2_HOB. when the max HobLength 0xFFF8 is not enough, 
there
   // will be a MP_INFORMATION2_HOB series in the HOB list.
-  // In the HOB list, there is a gMpInformationHobGuid2 with 0 value 
NumberOfProcessors
+  // In the HOB list, there is a gMpInformation2HobGuid with 0 value 
NumberOfProcessors
   // fields to indicate it's the last MP_INFORMATION2_HOB.
   //
   while (NumberOfProcessorsInHob != 0) {
 NumberOfProcessorsInHob = MIN (NumberOfProcessors - ProcessorIndex, 
MaxProcessorsPerHob);
 MpInformation2HobData   = BuildGuidHob (
-,
+,
 sizeof (MP_INFORMATION2_HOB_DATA) + sizeof 
(MP_INFORMATION2_ENTRY) * NumberOfProcessorsInHob
 );
 ASSERT (MpInformation2HobData != NULL);
@@ -744,7 +744,7 @@ InitializeCpuMpWorker (
   ASSERT_EFI_ERROR (Status);
 
   //
-  // Create gMpInformationHobGuid2
+  // Create gMpInformation2HobGuid
   //
   BuildMpInformationHob ();
 
diff --git a/UefiCpuPkg/CpuMpPei/CpuMpPei.inf b/UefiCpuPkg/CpuMpPei/CpuMpPei.inf
index 812fa179bd..9ab2623bd0 100644
--- a/UefiCpuPkg/CpuMpPei/CpuMpPei.inf
+++ b/UefiCpuPkg/CpuMpPei/CpuMpPei.inf
@@ -50,7 +50,7 @@
 
 [Guids]
   gEdkiiMigratedFvInfoGuid ## 
SOMETIMES_CONSUMES ## HOB
-  gMpInformationHobGuid2## PRODUCES
+  gMpInformation2HobGuid## PRODUCES
 
 [Ppis]
   gEfiPeiMpServicesPpiGuid  ## PRODUCES
diff --git a/UefiCpuPkg/Include/Guid/MpInformation2.h 
b/UefiCpuPkg/Include/Guid/MpInformation2.h
index 43185a4b01..2d9266f061 100644
--- a/UefiCpuPkg/Include/Guid/MpInformation2.h
+++ b/UefiCpuPkg/Include/Guid/MpInformation2.h
@@ -53,6 +53,6 @@ typedef struct {
 #define GET_MP_INFORMATION_ENTRY(MpInfoHobData, Index) \
 (MP_INFORMATION2_ENTRY *)((UINTN)&((MP_INFORMATION2_HOB_DATA 
*)(MpInfoHobData))->Entry + (MpInfoHobData)->EntrySize * Index)
 
-extern EFI_GUID  gMpInformationHobGuid2;
+extern EFI_GUID  gMpInformation2HobGuid;
 
 #endif
diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
index 9b230772cb..cd394826ff 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
@@ -776,7 +776,7 @@ GetMpInformation (
   HobIndex  = 0;
   HobCount  = 0;
 
-  FirstMpInfo2Hob = GetFirstGuidHob ();
+  FirstMpInfo2Hob = GetFirstGuidHob ();
   ASSERT (FirstMpInfo2Hob != NULL);
   GuidHob = FirstMpInfo2Hob;
   while (GuidHob != NULL) {
@@ -792,7 +792,7 @@ GetMpInformation (
 
 HobCount++;
 *NumberOfCpus += MpInformation2HobData->NumberOfProcessors;
-GuidHob= GetNextGuidHob (, GET_NEXT_HOB 
(GuidHob));
+GuidHob= GetNextGuidHob (, GET_NEXT_HOB 
(GuidHob));
   }
 
   ASSERT (*NumberOfCpus <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));
@@ -820,7 +820,7 @@ GetMpInformation (
   GuidHob = FirstMpInfo2Hob;
   while (HobIndex < HobCount) {
 MpInfo2Hobs[HobIndex++] = GET_GUID_HOB_DATA (GuidHob);
-GuidHob = GetNextGuidHob (, 
GET_NEXT_HOB (GuidHob));
+GuidHob = GetNextGuidHob (, 
GET_NEXT_HOB (GuidHob));
   }
 
   ProcessorInfo = (EFI_PROCESSOR_INFORMATION *)AllocatePool (sizeof 
(EFI_PROCESSOR_INFORMATION) * (*MaxNumberOfCpus));
diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf 
b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf
index 793220aba3..a018954ed7 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf
@@ -120,7 +120,7 @@
   gEdkiiPiSmmMemoryAttributesTableGuid ## CONSUMES ## SystemTable
   gEfiMemoryAttributesTableGuid## CONSUMES ## SystemTable
   gSmmBaseHobGuid  ## CONSUMES
-  gMpInformationHobGuid2   ## CONSUMES # Assume the HOB must 
has been 

[edk2-devel] [Patch V3] UefiCpuPkg:Limit PhysicalAddressBits in special case

2024-01-11 Thread duntan
When creating smm page table, limit maximum
supported physical address bits returned by
CalculateMaximumSupportAddress() to 47 if
5-Level Paging is disabled.
When 5-Level Paging is disabled and the
PhysicalAddressBits retrived from CPU HOB or
CpuId is bigger than 47, and since virtual
addresses are sign-extended, only [0, 2^47-1]
range in 52-bit physical address is mapped
in page table.

Signed-off-by: Dun Tan 
Reviewed-by: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 15 +--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
index ddd9be66b5..35c282a771 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
@@ -137,11 +137,13 @@ GetSubEntriesNum (
 /**
   Calculate the maximum support address.
 
+  @param[in] Is5LevelPagingNeededIf 5-level paging enabling is needed.
+
   @return the maximum support address.
 **/
 UINT8
 CalculateMaximumSupportAddress (
-  VOID
+  BOOLEAN  Is5LevelPagingNeeded
   )
 {
   UINT32  RegEax;
@@ -164,6 +166,15 @@ CalculateMaximumSupportAddress (
 }
   }
 
+  //
+  // Only [0, 2^47 -1] in 52-bit physical addresses is mapped in page table
+  // when 5-Level Paging is disabled.
+  //
+  ASSERT (PhysicalAddressBits <= 52);
+  if (!Is5LevelPagingNeeded && (PhysicalAddressBits > 47)) {
+PhysicalAddressBits = 47;
+  }
+
   return PhysicalAddressBits;
 }
 
@@ -197,7 +208,7 @@ SmmInitPageTable (
   mCpuSmmRestrictedMemoryAccess = PcdGetBool (PcdCpuSmmRestrictedMemoryAccess);
   m1GPageTableSupport   = Is1GPageSupport ();
   m5LevelPagingNeeded   = Is5LevelPagingNeeded ();
-  mPhysicalAddressBits  = CalculateMaximumSupportAddress ();
+  mPhysicalAddressBits  = CalculateMaximumSupportAddress 
(m5LevelPagingNeeded);
   PatchInstructionX86 (gPatch5LevelPagingNeeded, m5LevelPagingNeeded, 1);
   if (m5LevelPagingNeeded) {
 mPagingMode = m1GPageTableSupport ? Paging5Level1GB : Paging5Level;
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113600): https://edk2.groups.io/g/devel/message/113600
Mute This Topic: https://groups.io/mt/103658816/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V2] UefiCpuPkg:Limit PhysicalAddressBits in speicial case

2024-01-11 Thread duntan
Oh, thanks for your comments! Will correct it in next version patch.

Thanks,
Dun

-Original Message-
From: Laszlo Ersek  
Sent: Thursday, January 11, 2024 4:48 PM
To: Tan, Dun ; devel@edk2.groups.io
Cc: Ni, Ray ; Kumar, Rahul R ; Gerd 
Hoffmann 
Subject: Re: [Patch V2] UefiCpuPkg:Limit PhysicalAddressBits in speicial case

On 1/11/24 03:11, Dun Tan wrote:
> When creating smm page table, limit maximum supported physical address 
> bits returned by
> CalculateMaximumSupportAddress() to 47 if 5-Level Paging is disabled.
> When 5-Level Paging is disabled and the PhysicalAddressBits retrived 
> from CPU HOB or CpuId is bigger than 47, and since virtual addresses 
> are sign-extended, only [0, 2^47-1] range in 52-bit physical address 
> is mapped in page table.
> 
> Signed-off-by: Dun Tan 
> Cc: Ray Ni 
> Cc: Laszlo Ersek 
> Cc: Rahul Kumar 
> Cc: Gerd Hoffmann 
> ---
>  UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 15 +--
>  1 file changed, 13 insertions(+), 2 deletions(-)

I'll let Gerd review this (thanks!), I just want to point out a typo in the 
subject: "speicial" should be "special".

Thanks
Laszlo

> 
> diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c 
> b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
> index ddd9be66b5..35c282a771 100644
> --- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
> +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
> @@ -137,11 +137,13 @@ GetSubEntriesNum (
>  /**
>Calculate the maximum support address.
>  
> +  @param[in] Is5LevelPagingNeededIf 5-level paging enabling is needed.
> +
>@return the maximum support address.
>  **/
>  UINT8
>  CalculateMaximumSupportAddress (
> -  VOID
> +  BOOLEAN  Is5LevelPagingNeeded
>)
>  {
>UINT32  RegEax;
> @@ -164,6 +166,15 @@ CalculateMaximumSupportAddress (
>  }
>}
>  
> +  //
> +  // Only [0, 2^47 -1] in 52-bit physical addresses is mapped in page 
> + table  // when 5-Level Paging is disabled.
> +  //
> +  ASSERT (PhysicalAddressBits <= 52);  if (!Is5LevelPagingNeeded && 
> + (PhysicalAddressBits > 47)) {
> +PhysicalAddressBits = 47;
> +  }
> +
>return PhysicalAddressBits;
>  }
>  
> @@ -197,7 +208,7 @@ SmmInitPageTable (
>mCpuSmmRestrictedMemoryAccess = PcdGetBool 
> (PcdCpuSmmRestrictedMemoryAccess);
>m1GPageTableSupport   = Is1GPageSupport ();
>m5LevelPagingNeeded   = Is5LevelPagingNeeded ();
> -  mPhysicalAddressBits  = CalculateMaximumSupportAddress ();
> +  mPhysicalAddressBits  = CalculateMaximumSupportAddress 
> (m5LevelPagingNeeded);
>PatchInstructionX86 (gPatch5LevelPagingNeeded, m5LevelPagingNeeded, 1);
>if (m5LevelPagingNeeded) {
>  mPagingMode = m1GPageTableSupport ? Paging5Level1GB : 
> Paging5Level;



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113599): https://edk2.groups.io/g/devel/message/113599
Mute This Topic: https://groups.io/mt/103655312/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2] UefiCpuPkg:Limit PhysicalAddressBits in speicial case

2024-01-10 Thread duntan
When creating smm page table, limit maximum
supported physical address bits returned by
CalculateMaximumSupportAddress() to 47 if
5-Level Paging is disabled.
When 5-Level Paging is disabled and the
PhysicalAddressBits retrived from CPU HOB or
CpuId is bigger than 47, and since virtual
addresses are sign-extended, only [0, 2^47-1]
range in 52-bit physical address is mapped
in page table.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 15 +--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
index ddd9be66b5..35c282a771 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
@@ -137,11 +137,13 @@ GetSubEntriesNum (
 /**
   Calculate the maximum support address.
 
+  @param[in] Is5LevelPagingNeededIf 5-level paging enabling is needed.
+
   @return the maximum support address.
 **/
 UINT8
 CalculateMaximumSupportAddress (
-  VOID
+  BOOLEAN  Is5LevelPagingNeeded
   )
 {
   UINT32  RegEax;
@@ -164,6 +166,15 @@ CalculateMaximumSupportAddress (
 }
   }
 
+  //
+  // Only [0, 2^47 -1] in 52-bit physical addresses is mapped in page table
+  // when 5-Level Paging is disabled.
+  //
+  ASSERT (PhysicalAddressBits <= 52);
+  if (!Is5LevelPagingNeeded && (PhysicalAddressBits > 47)) {
+PhysicalAddressBits = 47;
+  }
+
   return PhysicalAddressBits;
 }
 
@@ -197,7 +208,7 @@ SmmInitPageTable (
   mCpuSmmRestrictedMemoryAccess = PcdGetBool (PcdCpuSmmRestrictedMemoryAccess);
   m1GPageTableSupport   = Is1GPageSupport ();
   m5LevelPagingNeeded   = Is5LevelPagingNeeded ();
-  mPhysicalAddressBits  = CalculateMaximumSupportAddress ();
+  mPhysicalAddressBits  = CalculateMaximumSupportAddress 
(m5LevelPagingNeeded);
   PatchInstructionX86 (gPatch5LevelPagingNeeded, m5LevelPagingNeeded, 1);
   if (m5LevelPagingNeeded) {
 mPagingMode = m1GPageTableSupport ? Paging5Level1GB : Paging5Level;
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113570): https://edk2.groups.io/g/devel/message/113570
Mute This Topic: https://groups.io/mt/103655312/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH] UefiCpuPkg:Limit PhysicalAddressBits in speicial case

2024-01-10 Thread duntan
Hi Gerd,

Thanks for your comments. I agree with your opinion. Will change the code and 
related comments in V2 patch.

Thanks,
Dun

-Original Message-
From: Gerd Hoffmann  
Sent: Wednesday, January 10, 2024 6:55 PM
To: Tan, Dun 
Cc: devel@edk2.groups.io; Ni, Ray ; Laszlo Ersek 
; Kumar, Rahul R 
Subject: Re: [PATCH] UefiCpuPkg:Limit PhysicalAddressBits in speicial case

On Wed, Jan 10, 2024 at 04:05:44PM +0800, Dun Tan wrote:
> When creating smm page table, limit maximum supported physical address 
> bits returned by
> CalculateMaximumSupportAddress() to 48 if 5-Level Paging is disabled.
> When 5-Level Paging is disabled and the PhysicalAddressBits retrived 
> from CPU HOB or CpuId is bigger than 48, only [0, 2^48 -1] range in 
> 52-bit physical address is mapped in page table.

I think this is wrong.  Virtual addresses are sign-extended, i.e. the virtual 
address space without 5-level paging is:

 0x -> 0x7fff and
 0x8000 -> 0x

Therefore identity-mapping works for [0, 2^47-1] only.

take care,
  Gerd



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113567): https://edk2.groups.io/g/devel/message/113567
Mute This Topic: https://groups.io/mt/103637402/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH] UefiCpuPkg:Limit PhysicalAddressBits in speicial case

2024-01-10 Thread duntan
When creating smm page table, limit maximum
supported physical address bits returned by
CalculateMaximumSupportAddress() to 48 if
5-Level Paging is disabled.
When 5-Level Paging is disabled and the
PhysicalAddressBits retrived from CPU HOB or
CpuId is bigger than 48, only [0, 2^48 -1]
range in 52-bit physical address is mapped
in page table.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c | 15 +--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
index ddd9be66b5..e6f174ca10 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/X64/PageTbl.c
@@ -137,11 +137,13 @@ GetSubEntriesNum (
 /**
   Calculate the maximum support address.
 
+  @param[in] Is5LevelPagingNeededIf 5-level paging enabling is needed.
+
   @return the maximum support address.
 **/
 UINT8
 CalculateMaximumSupportAddress (
-  VOID
+  BOOLEAN  Is5LevelPagingNeeded
   )
 {
   UINT32  RegEax;
@@ -164,6 +166,15 @@ CalculateMaximumSupportAddress (
 }
   }
 
+  //
+  // Only [0, 2^48 -1] in 52-bit physical addresses is mapped in page table
+  // when 5-Level Paging is disabled.
+  //
+  ASSERT (PhysicalAddressBits <= 52);
+  if (!Is5LevelPagingNeeded && (PhysicalAddressBits > 48)) {
+PhysicalAddressBits = 48;
+  }
+
   return PhysicalAddressBits;
 }
 
@@ -197,7 +208,7 @@ SmmInitPageTable (
   mCpuSmmRestrictedMemoryAccess = PcdGetBool (PcdCpuSmmRestrictedMemoryAccess);
   m1GPageTableSupport   = Is1GPageSupport ();
   m5LevelPagingNeeded   = Is5LevelPagingNeeded ();
-  mPhysicalAddressBits  = CalculateMaximumSupportAddress ();
+  mPhysicalAddressBits  = CalculateMaximumSupportAddress 
(m5LevelPagingNeeded);
   PatchInstructionX86 (gPatch5LevelPagingNeeded, m5LevelPagingNeeded, 1);
   if (m5LevelPagingNeeded) {
 mPagingMode = m1GPageTableSupport ? Paging5Level1GB : Paging5Level;
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113518): https://edk2.groups.io/g/devel/message/113518
Mute This Topic: https://groups.io/mt/103637402/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V3 0/2] Change the usage of input parameter ProcessorNumber in MpInitLibGetProcessorInfo() of MpInitLibUp

2024-01-09 Thread duntan
Thank you so much!

Thanks,
Dun

-Original Message-
From: Laszlo Ersek  
Sent: Wednesday, January 10, 2024 12:35 AM
To: devel@edk2.groups.io; Tan, Dun 
Subject: Re: [edk2-devel] [Patch V3 0/2] Change the usage of input parameter 
ProcessorNumber in MpInitLibGetProcessorInfo() of MpInitLibUp

On 1/8/24 06:08, duntan wrote:
> Please ignore the V2 PATCH set. No other change except adding BaseMemoryLib 
> headfile and lib instance in .inf to pass build since ZeroMem() is used. 
> 
> Comparing to the V1 patch set:
> In "set EXTENDED_PROCESSOR_INFORMATION to 0", set 
> EXTENDED_PROCESSOR_INFORMATION to 0 in API MpInitLibGetProcessorInfo() of 
> MpInitLibUp. This commit use ZeroMem() to set all fileds in output 
> EFI_PROCESSOR_INFORMATION to 0 before StatusFlag field is reassigned.
> 
> In "Check lower 24 bits of ProcessorNumber", use BIT24 instead of 
> CPU_V2_EXTENDED_TOPOLOGY to clearly tell that processor number only occupies 
> the lower 24 bits.
> 
> Dun Tan (2):
>   UefiCpuPkg: set EXTENDED_PROCESSOR_INFORMATION to 0
>   UefiCpuPkg: Check lower 24 bits of ProcessorNumber
> 
>  UefiCpuPkg/Include/Library/MpInitLib.h |  2 ++
>  UefiCpuPkg/Library/MpInitLib/MpLib.c   |  2 ++
>  UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c   | 20 
>  UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.inf |  1 +
>  4 files changed, 17 insertions(+), 8 deletions(-)
> 

Merged as commit range f2b074398ca0..08a6528bac38, via 
<https://github.com/tianocore/edk2/pull/5240> (first two commits in the PR).

BR
Laszlo



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113498): https://edk2.groups.io/g/devel/message/113498
Mute This Topic: https://groups.io/mt/103592277/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V2 0/2] Change the usage of input parameter ProcessorNumber in MpInitLibGetProcessorInfo() of MpInitLibUp

2024-01-07 Thread duntan
Please ignore the V2 patch set and help review the following V3 patch set. 
" [Patch V3 2/2] UefiCpuPkg: Check lower 24 bits of ProcessorNumber"
" [Patch V3 1/2] UefiCpuPkg: set EXTENDED_PROCESSOR_INFORMATION to 0"

There was build error in the v2 patch series and  the error was fixed in V3 
patch set.

Thanks,
Dun
-Original Message-
From: devel@edk2.groups.io  On Behalf Of duntan
Sent: Monday, January 8, 2024 11:56 AM
To: devel@edk2.groups.io
Subject: [edk2-devel] [Patch V2 0/2] Change the usage of input parameter 
ProcessorNumber in MpInitLibGetProcessorInfo() of MpInitLibUp

In the V2 patch set:
In "set EXTENDED_PROCESSOR_INFORMATION to 0", set 
EXTENDED_PROCESSOR_INFORMATION to 0 in API MpInitLibGetProcessorInfo() of 
MpInitLibUp. This commit use ZeroMem() to set all fileds in output 
EFI_PROCESSOR_INFORMATION to 0 before StatusFlag field is reassigned.

In "Check lower 24 bits of ProcessorNumber", use BIT24 instead of 
CPU_V2_EXTENDED_TOPOLOGY to clearly tell that processor number only occupies 
the lower 24 bits.

Dun Tan (2):
  UefiCpuPkg: set EXTENDED_PROCESSOR_INFORMATION to 0
  UefiCpuPkg: Check lower 24 bits of ProcessorNumber

 UefiCpuPkg/Include/Library/MpInitLib.h   |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.c |  2 ++
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c | 19 +++
 3 files changed, 15 insertions(+), 8 deletions(-)

-- 
2.31.1.windows.1








-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113357): https://edk2.groups.io/g/devel/message/113357
Mute This Topic: https://groups.io/mt/103591526/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V3 2/2] UefiCpuPkg: Check lower 24 bits of ProcessorNumber

2024-01-07 Thread duntan
Check lower 24 bits of ProcessorNumber instead of
the value of ProcessorNumber in the API
MpInitLibGetProcessorInfo() of MpInitLibUp instance.
Lower 24 bits of ProcessorNumber contains the actual
processor number.
The BIT24 of input ProcessorNumber might be set to
indicate if the EXTENDED_PROCESSOR_INFORMATION will
be retrived.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Min Xu 
---
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c 
b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
index d4f8611af8..35590fb4e7 100644
--- a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
+++ b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
@@ -107,7 +107,10 @@ MpInitLibGetProcessorInfo (
 return EFI_INVALID_PARAMETER;
   }
 
-  if (ProcessorNumber != 0) {
+  //
+  // Lower 24 bits contains the actual processor number.
+  //
+  if ((ProcessorNumber & (BIT24 - 1)) != 0) {
 return EFI_NOT_FOUND;
   }
 
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113356): https://edk2.groups.io/g/devel/message/113356
Mute This Topic: https://groups.io/mt/103592279/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V3 1/2] UefiCpuPkg: set EXTENDED_PROCESSOR_INFORMATION to 0

2024-01-07 Thread duntan
Set EXTENDED_PROCESSOR_INFORMATION to 0 in API
MpInitLibGetProcessorInfo() of MpInitLibUp. This
commit use ZeroMem() to set all fileds in output
EFI_PROCESSOR_INFORMATION to 0 before StatusFlag
field is reassigned.

Previously EXTENDED_PROCESSOR_INFORMATION in the API
MpInitLibGetProcessorInfo() of MpInitLibUp is ignored.
In PEI/DXE MpInitLib, EXTENDED_PROCESSOR_INFORMATION
will be retrived when BIT24 of input ProcessorNumber
is set. This commit can avoid garbage in the output
structure in MpInitLibGetProcessorInfo() of MpInitLibUp.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Min Xu 
---
 UefiCpuPkg/Include/Library/MpInitLib.h |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.c   |  2 ++
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c   | 15 ---
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.inf |  1 +
 4 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/UefiCpuPkg/Include/Library/MpInitLib.h 
b/UefiCpuPkg/Include/Library/MpInitLib.h
index 1853c46415..842c6f7ff9 100644
--- a/UefiCpuPkg/Include/Library/MpInitLib.h
+++ b/UefiCpuPkg/Include/Library/MpInitLib.h
@@ -63,6 +63,8 @@ MpInitLibGetNumberOfProcessors (
   instant this call is made. This service may only be called from the BSP.
 
   @param[in]  ProcessorNumber   The handle number of processor.
+Lower 24 bits contains the actual 
processor number.
+BIT24 indicates if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.
   @param[out] ProcessorInfoBuffer   A pointer to the buffer where information 
for
 the requested processor is deposited.
   @param[out] HealthDataReturn processor health data.
diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c 
b/UefiCpuPkg/Library/MpInitLib/MpLib.c
index a359906923..cdfb570e61 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c
@@ -2333,6 +2333,8 @@ MpInitLibInitialize (
   instant this call is made. This service may only be called from the BSP.
 
   @param[in]  ProcessorNumber   The handle number of processor.
+Lower 24 bits contains the actual 
processor number.
+BIT24 indicates if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.
   @param[out] ProcessorInfoBuffer   A pointer to the buffer where information 
for
 the requested processor is deposited.
   @param[out]  HealthDataReturn processor health data.
diff --git a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c 
b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
index 86f9fbf903..d4f8611af8 100644
--- a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
+++ b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
@@ -12,6 +12,7 @@
 #include 
 #include 
 #include 
+#include 
 
 /**
   MP Initialize Library initialization.
@@ -77,6 +78,8 @@ MpInitLibGetNumberOfProcessors (
   instant this call is made. This service may only be called from the BSP.
 
   @param[in]  ProcessorNumber   The handle number of processor.
+Lower 24 bits contains the actual 
processor number.
+BIT24 indicates if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.
   @param[out] ProcessorInfoBuffer   A pointer to the buffer where information 
for
 the requested processor is deposited.
   @param[out] HealthDataReturn processor health data.
@@ -108,13 +111,11 @@ MpInitLibGetProcessorInfo (
 return EFI_NOT_FOUND;
   }
 
-  ProcessorInfoBuffer->ProcessorId = 0;
-  ProcessorInfoBuffer->StatusFlag  = PROCESSOR_AS_BSP_BIT  |
- PROCESSOR_ENABLED_BIT |
- PROCESSOR_HEALTH_STATUS_BIT;
-  ProcessorInfoBuffer->Location.Package = 0;
-  ProcessorInfoBuffer->Location.Core= 0;
-  ProcessorInfoBuffer->Location.Thread  = 0;
+  ZeroMem (ProcessorInfoBuffer, sizeof (*ProcessorInfoBuffer));
+  ProcessorInfoBuffer->StatusFlag = PROCESSOR_AS_BSP_BIT  |
+PROCESSOR_ENABLED_BIT |
+PROCESSOR_HEALTH_STATUS_BIT;
+
   if (HealthData != NULL) {
 GuidHob = GetFirstGuidHob ();
 if (GuidHob != NULL) {
diff --git a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.inf 
b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.inf
index 24ad29c03c..eb0a168f74 100644
--- a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.inf
+++ b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.inf
@@ -32,6 +32,7 @@
   DebugLib
   LocalApicLib
   HobLib
+  BaseMemoryLib
 
 [Ppis]
   gEfiSecPlatformInformationPpiGuid  ## SOMETIMES_CONSUMES
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113355): https://edk2.groups.io/g/devel/message/113355
Mute This Topic: 

[edk2-devel] [Patch V3 0/2] Change the usage of input parameter ProcessorNumber in MpInitLibGetProcessorInfo() of MpInitLibUp

2024-01-07 Thread duntan
Please ignore the V2 PATCH set. No other change except adding BaseMemoryLib 
headfile and lib instance in .inf to pass build since ZeroMem() is used. 

Comparing to the V1 patch set:
In "set EXTENDED_PROCESSOR_INFORMATION to 0", set 
EXTENDED_PROCESSOR_INFORMATION to 0 in API MpInitLibGetProcessorInfo() of 
MpInitLibUp. This commit use ZeroMem() to set all fileds in output 
EFI_PROCESSOR_INFORMATION to 0 before StatusFlag field is reassigned.

In "Check lower 24 bits of ProcessorNumber", use BIT24 instead of 
CPU_V2_EXTENDED_TOPOLOGY to clearly tell that processor number only occupies 
the lower 24 bits.

Dun Tan (2):
  UefiCpuPkg: set EXTENDED_PROCESSOR_INFORMATION to 0
  UefiCpuPkg: Check lower 24 bits of ProcessorNumber

 UefiCpuPkg/Include/Library/MpInitLib.h |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.c   |  2 ++
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c   | 20 
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.inf |  1 +
 4 files changed, 17 insertions(+), 8 deletions(-)

-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113354): https://edk2.groups.io/g/devel/message/113354
Mute This Topic: https://groups.io/mt/103592277/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH 2/2] UefiCpuPkg: Check lower 24 bits of ProcessorNumber

2024-01-07 Thread duntan
Thanks for the comments. The code has been updated in the V2 patch set.

Thanks,
Dun

-Original Message-
From: Laszlo Ersek  
Sent: Friday, January 5, 2024 9:56 PM
To: Ni, Ray ; devel@edk2.groups.io; Tan, Dun 

Cc: Kumar, Rahul R ; Gerd Hoffmann 
; Xu, Min M 
Subject: Re: [edk2-devel] [PATCH 2/2] UefiCpuPkg: Check lower 24 bits of 
ProcessorNumber

On 1/5/24 13:55, Ni, Ray wrote:
>>> -  if (ProcessorNumber != 0) {
>>> +  //
>>> +  // Lower 24 bits contains the actual processor number.
>>> +  //
>>> +  if ((ProcessorNumber & (CPU_V2_EXTENDED_TOPOLOGY - 1)) != 0) {
> I suggest we explicitly use BIT24 instead of CPU_V2_EXTENDED_TOPOLOGY.
> Using BIT24 clearly tells that processor number only occupies the lower 24 
> bits.

Yes, I've noticed this discrepancy too; I agree BIT24 is clearer here!

> 
> 
>>>  return EFI_NOT_FOUND;
>>>}
>>>
>>
>> Reviewed-by: Laszlo Ersek 
> 



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113352): https://edk2.groups.io/g/devel/message/113352
Mute This Topic: https://groups.io/mt/103518743/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 2/2] UefiCpuPkg: Check lower 24 bits of ProcessorNumber

2024-01-07 Thread duntan
Check lower 24 bits of ProcessorNumber instead of
the value of ProcessorNumber in the API
MpInitLibGetProcessorInfo() of MpInitLibUp instance.
Lower 24 bits of ProcessorNumber contains the actual
processor number.
The BIT24 of input ProcessorNumber might be set to
indicate if the EXTENDED_PROCESSOR_INFORMATION will
be retrived.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Min Xu 
---
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c 
b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
index ea9a45c9c6..30bdd8cfdd 100644
--- a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
+++ b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
@@ -106,7 +106,10 @@ MpInitLibGetProcessorInfo (
 return EFI_INVALID_PARAMETER;
   }
 
-  if (ProcessorNumber != 0) {
+  //
+  // Lower 24 bits contains the actual processor number.
+  //
+  if ((ProcessorNumber & (BIT24 - 1)) != 0) {
 return EFI_NOT_FOUND;
   }
 
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113351): https://edk2.groups.io/g/devel/message/113351
Mute This Topic: https://groups.io/mt/103591528/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 1/2] UefiCpuPkg: set EXTENDED_PROCESSOR_INFORMATION to 0

2024-01-07 Thread duntan
Set EXTENDED_PROCESSOR_INFORMATION to 0 in API
MpInitLibGetProcessorInfo() of MpInitLibUp. This
commit use ZeroMem() to set all fileds in output
EFI_PROCESSOR_INFORMATION to 0 before StatusFlag
field is reassigned.

Previously EXTENDED_PROCESSOR_INFORMATION in the API
MpInitLibGetProcessorInfo() of MpInitLibUp is ignored.
In PEI/DXE MpInitLib, EXTENDED_PROCESSOR_INFORMATION
will be retrived when BIT24 of input ProcessorNumber
is set. This commit can avoid garbage in the output
structure in MpInitLibGetProcessorInfo() of MpInitLibUp.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Min Xu 
---
 UefiCpuPkg/Include/Library/MpInitLib.h   |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.c |  2 ++
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c | 14 +++---
 3 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/UefiCpuPkg/Include/Library/MpInitLib.h 
b/UefiCpuPkg/Include/Library/MpInitLib.h
index 1853c46415..842c6f7ff9 100644
--- a/UefiCpuPkg/Include/Library/MpInitLib.h
+++ b/UefiCpuPkg/Include/Library/MpInitLib.h
@@ -63,6 +63,8 @@ MpInitLibGetNumberOfProcessors (
   instant this call is made. This service may only be called from the BSP.
 
   @param[in]  ProcessorNumber   The handle number of processor.
+Lower 24 bits contains the actual 
processor number.
+BIT24 indicates if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.
   @param[out] ProcessorInfoBuffer   A pointer to the buffer where information 
for
 the requested processor is deposited.
   @param[out] HealthDataReturn processor health data.
diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c 
b/UefiCpuPkg/Library/MpInitLib/MpLib.c
index a359906923..cdfb570e61 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c
@@ -2333,6 +2333,8 @@ MpInitLibInitialize (
   instant this call is made. This service may only be called from the BSP.
 
   @param[in]  ProcessorNumber   The handle number of processor.
+Lower 24 bits contains the actual 
processor number.
+BIT24 indicates if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.
   @param[out] ProcessorInfoBuffer   A pointer to the buffer where information 
for
 the requested processor is deposited.
   @param[out]  HealthDataReturn processor health data.
diff --git a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c 
b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
index 86f9fbf903..ea9a45c9c6 100644
--- a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
+++ b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
@@ -77,6 +77,8 @@ MpInitLibGetNumberOfProcessors (
   instant this call is made. This service may only be called from the BSP.
 
   @param[in]  ProcessorNumber   The handle number of processor.
+Lower 24 bits contains the actual 
processor number.
+BIT24 indicates if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.
   @param[out] ProcessorInfoBuffer   A pointer to the buffer where information 
for
 the requested processor is deposited.
   @param[out] HealthDataReturn processor health data.
@@ -108,13 +110,11 @@ MpInitLibGetProcessorInfo (
 return EFI_NOT_FOUND;
   }
 
-  ProcessorInfoBuffer->ProcessorId = 0;
-  ProcessorInfoBuffer->StatusFlag  = PROCESSOR_AS_BSP_BIT  |
- PROCESSOR_ENABLED_BIT |
- PROCESSOR_HEALTH_STATUS_BIT;
-  ProcessorInfoBuffer->Location.Package = 0;
-  ProcessorInfoBuffer->Location.Core= 0;
-  ProcessorInfoBuffer->Location.Thread  = 0;
+  ZeroMem (ProcessorInfoBuffer, sizeof (*ProcessorInfoBuffer));
+  ProcessorInfoBuffer->StatusFlag = PROCESSOR_AS_BSP_BIT  |
+PROCESSOR_ENABLED_BIT |
+PROCESSOR_HEALTH_STATUS_BIT;
+
   if (HealthData != NULL) {
 GuidHob = GetFirstGuidHob ();
 if (GuidHob != NULL) {
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113350): https://edk2.groups.io/g/devel/message/113350
Mute This Topic: https://groups.io/mt/103591527/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 0/2] Change the usage of input parameter ProcessorNumber in MpInitLibGetProcessorInfo() of MpInitLibUp

2024-01-07 Thread duntan
In the V2 patch set:
In "set EXTENDED_PROCESSOR_INFORMATION to 0", set 
EXTENDED_PROCESSOR_INFORMATION to 0 in API MpInitLibGetProcessorInfo() of 
MpInitLibUp. This commit use ZeroMem() to set all fileds in output 
EFI_PROCESSOR_INFORMATION to 0 before StatusFlag field is reassigned.

In "Check lower 24 bits of ProcessorNumber", use BIT24 instead of 
CPU_V2_EXTENDED_TOPOLOGY to clearly tell that processor number only occupies 
the lower 24 bits.

Dun Tan (2):
  UefiCpuPkg: set EXTENDED_PROCESSOR_INFORMATION to 0
  UefiCpuPkg: Check lower 24 bits of ProcessorNumber

 UefiCpuPkg/Include/Library/MpInitLib.h   |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.c |  2 ++
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c | 19 +++
 3 files changed, 15 insertions(+), 8 deletions(-)

-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113349): https://edk2.groups.io/g/devel/message/113349
Mute This Topic: https://groups.io/mt/103591526/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH 1/2] UefiCpuPkg: Retrive EXTENDED_PROCESSOR_INFORMATION

2024-01-05 Thread duntan
Hi Laszlo,

Thanks for your comments. I agree with your solution. It seems simpler and 
clearer. Will change the code and keep the additional function comments in next 
version patch set.

Thanks, 
Dun

-Original Message-
From: Laszlo Ersek  
Sent: Thursday, January 4, 2024 10:53 PM
To: devel@edk2.groups.io; Tan, Dun 
Cc: Ni, Ray ; Kumar, Rahul R ; Gerd 
Hoffmann ; Xu, Min M 
Subject: Re: [edk2-devel] [PATCH 1/2] UefiCpuPkg: Retrive 
EXTENDED_PROCESSOR_INFORMATION

On 1/4/24 08:32, duntan wrote:
> Retrive EXTENDED_PROCESSOR_INFORMATION in the API
> MpInitLibGetProcessorInfo() of MpInitLibUp instance when the BIT24 of 
> input ProcessorNumber is set.
> It's to align with the behavior in PEI/DXE MpInitLib
> 
> Signed-off-by: Dun Tan 
> Cc: Ray Ni 
> Cc: Laszlo Ersek 
> Cc: Rahul Kumar 
> Cc: Gerd Hoffmann 
> Cc: Min Xu 
> ---
>  UefiCpuPkg/Include/Library/MpInitLib.h   |  2 ++
>  UefiCpuPkg/Library/MpInitLib/MpLib.c |  2 ++
>  UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c | 12 
>  3 files changed, 16 insertions(+)
> 
> diff --git a/UefiCpuPkg/Include/Library/MpInitLib.h 
> b/UefiCpuPkg/Include/Library/MpInitLib.h
> index 1853c46415..842c6f7ff9 100644
> --- a/UefiCpuPkg/Include/Library/MpInitLib.h
> +++ b/UefiCpuPkg/Include/Library/MpInitLib.h
> @@ -63,6 +63,8 @@ MpInitLibGetNumberOfProcessors (
>instant this call is made. This service may only be called from the BSP.
>  
>@param[in]  ProcessorNumber   The handle number of processor.
> +Lower 24 bits contains the actual 
> processor number.
> +BIT24 indicates if the 
> EXTENDED_PROCESSOR_INFORMATION will be retrived.
>@param[out] ProcessorInfoBuffer   A pointer to the buffer where 
> information for
>  the requested processor is deposited.
>@param[out] HealthDataReturn processor health data.
> diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c 
> b/UefiCpuPkg/Library/MpInitLib/MpLib.c
> index a359906923..cdfb570e61 100644
> --- a/UefiCpuPkg/Library/MpInitLib/MpLib.c
> +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c
> @@ -2333,6 +2333,8 @@ MpInitLibInitialize (
>instant this call is made. This service may only be called from the BSP.
>  
>@param[in]  ProcessorNumber   The handle number of processor.
> +Lower 24 bits contains the actual 
> processor number.
> +BIT24 indicates if the 
> EXTENDED_PROCESSOR_INFORMATION will be retrived.
>@param[out] ProcessorInfoBuffer   A pointer to the buffer where 
> information for
>  the requested processor is deposited.
>@param[out]  HealthDataReturn processor health data.
> diff --git a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c 
> b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
> index 86f9fbf903..3af4911d4b 100644
> --- a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
> +++ b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
> @@ -77,6 +77,8 @@ MpInitLibGetNumberOfProcessors (
>instant this call is made. This service may only be called from the BSP.
>  
>@param[in]  ProcessorNumber   The handle number of processor.
> +Lower 24 bits contains the actual 
> processor number.
> +BIT24 indicates if the 
> EXTENDED_PROCESSOR_INFORMATION will be retrived.
>@param[out] ProcessorInfoBuffer   A pointer to the buffer where 
> information for
>  the requested processor is deposited.
>@param[out] HealthDataReturn processor health data.
> @@ -115,6 +117,16 @@ MpInitLibGetProcessorInfo (
>ProcessorInfoBuffer->Location.Package = 0;
>ProcessorInfoBuffer->Location.Core= 0;
>ProcessorInfoBuffer->Location.Thread  = 0;
> +
> +  if ((ProcessorNumber & CPU_V2_EXTENDED_TOPOLOGY) != 0) {
> +ProcessorInfoBuffer->ExtendedInformation.Location2.Package = 0;
> +ProcessorInfoBuffer->ExtendedInformation.Location2.Die = 0;
> +ProcessorInfoBuffer->ExtendedInformation.Location2.Tile= 0;
> +ProcessorInfoBuffer->ExtendedInformation.Location2.Module  = 0;
> +ProcessorInfoBuffer->ExtendedInformation.Location2.Core= 0;
> +ProcessorInfoBuffer->ExtendedInformation.Location2.Thread  = 0;  
> + }
> +
>if (HealthData != NULL) {
>  GuidHob = GetFirstGuidHob ();
>  if (GuidHob != NULL) {

(1) For the UP implementation of MpInitLibGetProcessorInfo():

How about, for a *complete* solution (covering both pre-patch and post-patch 
functionality):

  ZeroMem (ProcessorInfoBuffer, sizeof *

[edk2-devel] [PATCH 1/2] UefiCpuPkg: Retrive EXTENDED_PROCESSOR_INFORMATION

2024-01-03 Thread duntan
Retrive EXTENDED_PROCESSOR_INFORMATION in the API
MpInitLibGetProcessorInfo() of MpInitLibUp instance
when the BIT24 of input ProcessorNumber is set.
It's to align with the behavior in PEI/DXE MpInitLib

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Min Xu 
---
 UefiCpuPkg/Include/Library/MpInitLib.h   |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.c |  2 ++
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c | 12 
 3 files changed, 16 insertions(+)

diff --git a/UefiCpuPkg/Include/Library/MpInitLib.h 
b/UefiCpuPkg/Include/Library/MpInitLib.h
index 1853c46415..842c6f7ff9 100644
--- a/UefiCpuPkg/Include/Library/MpInitLib.h
+++ b/UefiCpuPkg/Include/Library/MpInitLib.h
@@ -63,6 +63,8 @@ MpInitLibGetNumberOfProcessors (
   instant this call is made. This service may only be called from the BSP.
 
   @param[in]  ProcessorNumber   The handle number of processor.
+Lower 24 bits contains the actual 
processor number.
+BIT24 indicates if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.
   @param[out] ProcessorInfoBuffer   A pointer to the buffer where information 
for
 the requested processor is deposited.
   @param[out] HealthDataReturn processor health data.
diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c 
b/UefiCpuPkg/Library/MpInitLib/MpLib.c
index a359906923..cdfb570e61 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c
@@ -2333,6 +2333,8 @@ MpInitLibInitialize (
   instant this call is made. This service may only be called from the BSP.
 
   @param[in]  ProcessorNumber   The handle number of processor.
+Lower 24 bits contains the actual 
processor number.
+BIT24 indicates if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.
   @param[out] ProcessorInfoBuffer   A pointer to the buffer where information 
for
 the requested processor is deposited.
   @param[out]  HealthDataReturn processor health data.
diff --git a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c 
b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
index 86f9fbf903..3af4911d4b 100644
--- a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
+++ b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
@@ -77,6 +77,8 @@ MpInitLibGetNumberOfProcessors (
   instant this call is made. This service may only be called from the BSP.
 
   @param[in]  ProcessorNumber   The handle number of processor.
+Lower 24 bits contains the actual 
processor number.
+BIT24 indicates if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.
   @param[out] ProcessorInfoBuffer   A pointer to the buffer where information 
for
 the requested processor is deposited.
   @param[out] HealthDataReturn processor health data.
@@ -115,6 +117,16 @@ MpInitLibGetProcessorInfo (
   ProcessorInfoBuffer->Location.Package = 0;
   ProcessorInfoBuffer->Location.Core= 0;
   ProcessorInfoBuffer->Location.Thread  = 0;
+
+  if ((ProcessorNumber & CPU_V2_EXTENDED_TOPOLOGY) != 0) {
+ProcessorInfoBuffer->ExtendedInformation.Location2.Package = 0;
+ProcessorInfoBuffer->ExtendedInformation.Location2.Die = 0;
+ProcessorInfoBuffer->ExtendedInformation.Location2.Tile= 0;
+ProcessorInfoBuffer->ExtendedInformation.Location2.Module  = 0;
+ProcessorInfoBuffer->ExtendedInformation.Location2.Core= 0;
+ProcessorInfoBuffer->ExtendedInformation.Location2.Thread  = 0;
+  }
+
   if (HealthData != NULL) {
 GuidHob = GetFirstGuidHob ();
 if (GuidHob != NULL) {
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113138): https://edk2.groups.io/g/devel/message/113138
Mute This Topic: https://groups.io/mt/103518742/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 2/2] UefiCpuPkg: Check lower 24 bits of ProcessorNumber

2024-01-03 Thread duntan
Check lower 24 bits of ProcessorNumber instead of
the value of ProcessorNumber in the API
MpInitLibGetProcessorInfo() of MpInitLibUp instance.
Lower 24 bits of ProcessorNumber contains the actual
processor number.
The BIT24 of input ProcessorNumber might be set to
indicate if the EXTENDED_PROCESSOR_INFORMATION will
be retrived.

Signed-off-by: Dun Tan 
Cc: Ray Ni 
Cc: Laszlo Ersek 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
Cc: Min Xu 
---
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c 
b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
index 3af4911d4b..b804e400e6 100644
--- a/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
+++ b/UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c
@@ -106,7 +106,10 @@ MpInitLibGetProcessorInfo (
 return EFI_INVALID_PARAMETER;
   }
 
-  if (ProcessorNumber != 0) {
+  //
+  // Lower 24 bits contains the actual processor number.
+  //
+  if ((ProcessorNumber & (CPU_V2_EXTENDED_TOPOLOGY - 1)) != 0) {
 return EFI_NOT_FOUND;
   }
 
-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113139): https://edk2.groups.io/g/devel/message/113139
Mute This Topic: https://groups.io/mt/103518743/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [PATCH 0/2] Change the usage of input parameter ProcessorNumber in MpInitLibGetProcessorInfo() of MpInitLibUp

2024-01-03 Thread duntan
In this patch set, the usage of input parameter ProcessorNumber in 
MpInitLibGetProcessorInfo() of MpInitLibUp instance is changed.
1.Retrive EXTENDED_PROCESSOR_INFORMATION in the API MpInitLibGetProcessorInfo() 
of MpInitLibUp instance when the BIT24 of input ProcessorNumber is set.
It's to align with the behavior in PEI/DXE MpInitLib.

2.Also, check lower 24 bits of ProcessorNumber instead of the value of 
ProcessorNumber in the API MpInitLibGetProcessorInfo() of MpInitLibUp instance.
The BIT24 of input ProcessorNumber might be set to indicate if the 
EXTENDED_PROCESSOR_INFORMATION will be retrived.

Dun Tan (2):
  UefiCpuPkg: Retrive EXTENDED_PROCESSOR_INFORMATION
  UefiCpuPkg: Check lower 24 bits of ProcessorNumber

 UefiCpuPkg/Include/Library/MpInitLib.h   |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.c |  2 ++
 UefiCpuPkg/Library/MpInitLibUp/MpInitLibUp.c | 17 -
 3 files changed, 20 insertions(+), 1 deletion(-)

-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113137): https://edk2.groups.io/g/devel/message/113137
Mute This Topic: https://groups.io/mt/103518740/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V2 0/3] Move gMpInformationHobGuid from StandaloneMmPkg to UefiCpuPkg.

2024-01-03 Thread duntan
I think it's a limitation of the existing implementation. Currently the 
HobLength in EFI_HOB_GENERIC_HEADER is UINT16.

Thanks,
Dun

-Original Message-
From: Ard Biesheuvel  
Sent: Wednesday, January 3, 2024 11:11 PM
To: Tan, Dun ; Samer El-Haj-Mahmoud 
; Jose Marinho 
Cc: Ard Biesheuvel ; Ni, Ray ; 
devel@edk2.groups.io
Subject: Re: [edk2-devel] [Patch V2 0/3] Move gMpInformationHobGuid from 
StandaloneMmPkg to UefiCpuPkg.

(cc Samer and Jose)

Hello Dun Tan,

On Fri, 17 Nov 2023 at 10:57, Tan, Dun  wrote:
>
> Hi Ard,
>
> I'm working on moving gMpInformationHobGuid from StandaloneMmPkg to 
> UefiCpuPkg in this patch series. Currently in Edk2, the HOB is only consumed 
> by StandaloneMmCpu.inf.
>
> As we know that this HOB is used to provide a lightweight static information 
> of MP processor. However, there might be a concern that the maximum HOB size 
> 64KB is not large enough when CPU number is 1~2000 or bigger.
>
> May I know if you considered this situation or is there any solution to avoid 
> this issue on Arm?
>

I don't think this has ever come up. Is this maximum HOB size a PI limitation? 
Or simply a limitation of the existing implementation?





>
> -Original Message-
> From: devel@edk2.groups.io  On Behalf Of duntan
> Sent: Friday, November 17, 2023 5:40 PM
> To: devel@edk2.groups.io
> Subject: [edk2-devel] [Patch V2 0/3] Move gMpInformationHobGuid from 
> StandaloneMmPkg to UefiCpuPkg.
>
> In the V2 patch set: Added more comments in the new MpInformation.h to 
> document that some fields in this HOB may be invalidated.
>
> Move gMpInformationHobGuid from StandaloneMmPkg to UefiCpuPkg.
>
> Previously, the HOB is defined, created and consumed only in StandaloneMmPkg. 
> The HOB contains the number of processors and EFI_PROCESSOR_INFORMATION 
> structure. This is the same as the information that PiSmmCpuDxeSmm uses 
> EfiMpServiceProtocolGuid to get.
>
> The incoming plan is to create gMpInformationHobGuid for both StandaloneMm 
> and legacy DXE_SMM in early phase(for example in CpuMpPei). Then 
> PiSmmCpuDxeSmm can consume the hob, which can simplify code logic in 
> PiSmmCpuDxeSmm driver.
>
> So move this HOB definition to UefiCpuPkg in this patch series.
>
> Dun Tan (3):
>   UefiCpuPkg: Create MpInformation.h in UefiCpuPkg
>   StandaloneMmPkg:Add UefiCpuPkg.dec in DependencyCheck
>   StandaloneMmPkg:Remove MpInformation.h
>
>  StandaloneMmPkg/Drivers/StandaloneMmCpu/StandaloneMmCpu.inf  
>  | 1 +
>  
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
>  | 1 +
>  StandaloneMmPkg/StandaloneMmPkg.ci.yaml  
>  | 3 ++-
>  StandaloneMmPkg/StandaloneMmPkg.dec  
>  | 1 -
>  {StandaloneMmPkg => UefiCpuPkg}/Include/Guid/MpInformation.h 
>  | 6 +-
>  UefiCpuPkg/UefiCpuPkg.dec
>  | 3 +++
>  6 files changed, 12 insertions(+), 3 deletions(-)  rename 
> {StandaloneMmPkg => UefiCpuPkg}/Include/Guid/MpInformation.h (71%)
>
> --
> 2.31.1.windows.1
>
>
>
> 
>
>


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113113): https://edk2.groups.io/g/devel/message/113113
Mute This Topic: https://groups.io/mt/102644629/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [PATCH 3/3] StandaloneMmPkg:Remove MpInformation.h

2024-01-03 Thread duntan
Hi Ard,

This patch set has been dropped. Another patch set "Create and consume a new 
gMpInformationHobGuid2 in UefiCpuPkg." is adopted.
When the Maximum length 64KB is not enough, there might be more than 1 HOB 
instance of this gMpInformationHobGuid2.
There is no change in  ARM related code in the new patch set.

Thanks,
Dun

-Original Message-
From: Ard Biesheuvel  
Sent: Wednesday, January 3, 2024 11:12 PM
To: devel@edk2.groups.io; Tan, Dun 
Cc: Ard Biesheuvel ; Sami Mujawar 
; Ni, Ray 
Subject: Re: [edk2-devel] [PATCH 3/3] StandaloneMmPkg:Remove MpInformation.h

On Thu, 9 Nov 2023 at 03:51, duntan  wrote:
>
> Remove MpInformation.h in StandaloneMmPkg since it has been moved to 
> UefiCpuPkg
>
> Signed-off-by: Dun Tan 
> Cc: Ard Biesheuvel 
> Cc: Sami Mujawar 
> Cc: Ray Ni 

Doesn't this break the ARM build?


> ---
>  StandaloneMmPkg/Drivers/StandaloneMmCpu/StandaloneMmCpu.inf  
>  |  1 +
>  StandaloneMmPkg/Include/Guid/MpInformation.h 
>  | 35 ---
>  
> StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreEntryPoint.inf
>  |  1 +
>  StandaloneMmPkg/StandaloneMmPkg.dec  
>  |  1 -
>  4 files changed, 2 insertions(+), 36 deletions(-)
>
> diff --git 
> a/StandaloneMmPkg/Drivers/StandaloneMmCpu/StandaloneMmCpu.inf 
> b/StandaloneMmPkg/Drivers/StandaloneMmCpu/StandaloneMmCpu.inf
> index 1fcb17d89d..4ed0e395c8 100644
> --- a/StandaloneMmPkg/Drivers/StandaloneMmCpu/StandaloneMmCpu.inf
> +++ b/StandaloneMmPkg/Drivers/StandaloneMmCpu/StandaloneMmCpu.inf
> @@ -27,6 +27,7 @@
>MdePkg/MdePkg.dec
>MdeModulePkg/MdeModulePkg.dec
>StandaloneMmPkg/StandaloneMmPkg.dec
> +  UefiCpuPkg/UefiCpuPkg.dec
>
>  [LibraryClasses]
>ArmLib
> diff --git a/StandaloneMmPkg/Include/Guid/MpInformation.h 
> b/StandaloneMmPkg/Include/Guid/MpInformation.h
> deleted file mode 100644
> index dbf88d12de..00
> --- a/StandaloneMmPkg/Include/Guid/MpInformation.h
> +++ /dev/null
> @@ -1,35 +0,0 @@
> -/** @file
> -  EFI MP information protocol provides a lightweight MP_SERVICES_PROTOCOL.
> -
> -  MP information protocol only provides static information of MP processor.
> -
> -  Copyright (c) 2009, Intel Corporation. All rights reserved.
> -  Copyright (c) 2016 - 2018, ARM Limited. All rights reserved.
> -
> -  SPDX-License-Identifier: BSD-2-Clause-Patent
> -
> -**/
> -
> -#ifndef _MP_INFORMATION_H_
> -#define _MP_INFORMATION_H_
> -
> -#include 
> -#include 
> -#include 
> -
> -#define MP_INFORMATION_GUID \
> -  { \
> -0xba33f15d, 0x4000, 0x45c1, {0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 
> 0xe3}  \
> -  }
> -
> -#pragma pack(1)
> -typedef struct {
> -  UINT64   NumberOfProcessors;
> -  UINT64   NumberOfEnabledProcessors;
> -  EFI_PROCESSOR_INFORMATIONProcessorInfoBuffer[];
> -} MP_INFORMATION_HOB_DATA;
> -#pragma pack()
> -
> -extern EFI_GUID  gMpInformationHobGuid;
> -
> -#endif
> diff --git 
> a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreE
> ntryPoint.inf 
> b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreE
> ntryPoint.inf
> index 75cfb98c0e..1fc31360ce 100644
> --- 
> a/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmCoreE
> ntryPoint.inf
> +++ b/StandaloneMmPkg/Library/StandaloneMmCoreEntryPoint/StandaloneMmC
> +++ oreEntryPoint.inf
> @@ -33,6 +33,7 @@
>MdePkg/MdePkg.dec
>MdeModulePkg/MdeModulePkg.dec
>StandaloneMmPkg/StandaloneMmPkg.dec
> +  UefiCpuPkg/UefiCpuPkg.dec
>
>  [Packages.ARM, Packages.AARCH64]
>ArmPkg/ArmPkg.dec
> diff --git a/StandaloneMmPkg/StandaloneMmPkg.dec 
> b/StandaloneMmPkg/StandaloneMmPkg.dec
> index 46784d94e4..01f37deebb 100644
> --- a/StandaloneMmPkg/StandaloneMmPkg.dec
> +++ b/StandaloneMmPkg/StandaloneMmPkg.dec
> @@ -36,7 +36,6 @@
>
>  [Guids]
>gStandaloneMmPkgTokenSpaceGuid   = { 0x18fe7632, 0xf5c8, 0x4e63, { 
> 0x8d, 0xe8, 0x17, 0xa5, 0x5c, 0x59, 0x13, 0xbd }}
> -  gMpInformationHobGuid= { 0xba33f15d, 0x4000, 0x45c1, { 
> 0x8e, 0x88, 0xf9, 0x16, 0x92, 0xd4, 0x57, 0xe3 }}
>gMmFvDispatchGuid= { 0xb65694cc, 0x09e3, 0x4c3b, { 
> 0xb5, 0xcd, 0x05, 0xf4, 0x4d, 0x3c, 0xdb, 0xff }}
>
>## Include/Guid/MmCoreData.h
> --
> 2.31.1.windows.1
>
>
>
> 
>
>


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113112): https://edk2.groups.io/g/devel/message/113112
Mute This Topic: https://groups.io/mt/102479012/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V3 0/6] Create and consume a new gMpInformationHobGuid2 in UefiCpuPkg.

2023-12-11 Thread duntan
Hi Laszlo,

Thanks for your reply. Sorry that I didn't add you in the reviewer list from 
the beginning of this patch series review. About the patch review, please take 
your time. Also take care your body!

The patch set was reviewed-by Ray last week. So I think we can merge the patch 
set first. You can ping me if you have any comments about this patch set later.

Thanks,
Dun

-Original Message-
From: Laszlo Ersek  
Sent: Monday, December 11, 2023 9:50 PM
To: Tan, Dun ; devel@edk2.groups.io
Cc: Ni, Ray ; Ard Biesheuvel ; Kinney, 
Michael D ; Gerd Hoffmann 
Subject: Re: [edk2-devel] [Patch V3 0/6] Create and consume a new 
gMpInformationHobGuid2 in UefiCpuPkg.

Hi Dun,

On 12/11/23 04:16, Tan, Dun wrote:
> Hi Laszlo,
> 
> Previously I sent a patch set " Move gMpInformationHobGuid from 
> StandaloneMmPkg to UefiCpuPkg. " and thanks for your review. To solve the 
> issue that the maximum length of one HOB might not be enough when CPU count 
> is 1-2000 or bigger and extend the HOB, we decide to create a new MpInfo2Hob 
> in UefiCpuPkg in this patch set. Do you have any comments about the patch set?
> 
> Thanks,
> Dun

A few days ago I made an effort to at least identify the newest patch sets I 
should "sometime" review on edk2, including those that apparently superseded 
older versions. Thus, although not with 100% certainty, I did deduce the above 
"change of plan", i.e., that the movement of the existent info HOB between 
packages would be superseded by a brand new HOB. However, all I could do at the 
time was simply tagging the new version for review -- and that's where I stand 
now.

For reference, I have approx. 14+ patch sets tagged for review on edk2-devel -- 
these have accumulated due to my 2 weeks long sick leave.
I'm back to work for 4 days this week, but then I'll disappear again until the 
end of the year. So, I think I had best declare "email bankruptcy".

Apologies for blocking you -- I had made some efforts to inform my 
co-maintainers of my status meanwhile. So, please don't wait for my feedback at 
this time; I might catch up, if I'm lucky, but I probably won't be able to. So 
if Ray is pleased with your patches, please go ahead and merge them.

I might make comments on smaller patches this week; rest assured that that kind 
of "preference" is just practicality, not laziness. It feels hopeless for me to 
make a serious "dent" in reviewing any larger patch set this week, so I'll try 
to spend review effort where it has a fleeting chance at enabling actual 
progress.

Best regards,
Laszlo


> 
> -Original Message-
> From: devel@edk2.groups.io  On Behalf Of duntan
> Sent: Friday, December 8, 2023 5:55 PM
> To: devel@edk2.groups.io
> Subject: [edk2-devel] [Patch V3 0/6] Create and consume a new 
> gMpInformationHobGuid2 in UefiCpuPkg.
> 
> In the V3 patch set,
> In patch "UefiCpuPkg: Build MpInfo2HOB in CpuMpPei", the DEBUG message format 
> is modified In patch "UefiCpuPkg: Consume MpInfo2Hob in PiSmmCpuDxe", remove 
> unneccesary assert check.
> In patch "UefiCpuPkg: Avoid assuming only one smmbasehob", free allocated 
> buffer when error returning case happen.
> 
> Dun Tan (6):
>   UefiCpuPkg: Create gMpInformationHobGuid2 in UefiCpuPkg
>   UefiCpuPkg: Build MpInfo2HOB in CpuMpPei
>   UefiCpuPkg: Consume MpInfo2Hob in PiSmmCpuDxe
>   UefiCpuPkg: Add a new field in MpInfo2 HOB
>   UefiCpuPkg: Cache core type in MpInfo2 HOB
>   UefiCpuPkg: Avoid assuming only one smmbasehob
> 
>  UefiCpuPkg/CpuMpPei/CpuMpPei.c   | 146 
> ++
>  UefiCpuPkg/CpuMpPei/CpuMpPei.h   |   6 +-
>  UefiCpuPkg/CpuMpPei/CpuMpPei.inf |   3 ++-
>  UefiCpuPkg/Include/Guid/MpInformation2.h |  58 
> ++
>  UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c   | 354 
> ++
>  UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h   |   2 +-
>  UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf |   8 
>  UefiCpuPkg/UefiCpuPkg.dec|   3 +++
>  8 files changed, 513 insertions(+), 67 deletions(-)  create mode 
> 100644 UefiCpuPkg/Include/Guid/MpInformation2.h
> 
> --
> 2.31.1.windows.1
> 
> 
> 
> 
> 
> 



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#112353): https://edk2.groups.io/g/devel/message/112353
Mute This Topic: https://groups.io/mt/103052268/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




Re: [edk2-devel] [Patch V3 0/6] Create and consume a new gMpInformationHobGuid2 in UefiCpuPkg.

2023-12-10 Thread duntan
Hi Laszlo,

Previously I sent a patch set " Move gMpInformationHobGuid from StandaloneMmPkg 
to UefiCpuPkg. " and thanks for your review. To solve the issue that the 
maximum length of one HOB might not be enough when CPU count is 1-2000 or 
bigger and extend the HOB, we decide to create a new MpInfo2Hob in UefiCpuPkg 
in this patch set. Do you have any comments about the patch set?

Thanks, 
Dun

-Original Message-
From: devel@edk2.groups.io  On Behalf Of duntan
Sent: Friday, December 8, 2023 5:55 PM
To: devel@edk2.groups.io
Subject: [edk2-devel] [Patch V3 0/6] Create and consume a new 
gMpInformationHobGuid2 in UefiCpuPkg.

In the V3 patch set,
In patch "UefiCpuPkg: Build MpInfo2HOB in CpuMpPei", the DEBUG message format 
is modified In patch "UefiCpuPkg: Consume MpInfo2Hob in PiSmmCpuDxe", remove 
unneccesary assert check.
In patch "UefiCpuPkg: Avoid assuming only one smmbasehob", free allocated 
buffer when error returning case happen.

Dun Tan (6):
  UefiCpuPkg: Create gMpInformationHobGuid2 in UefiCpuPkg
  UefiCpuPkg: Build MpInfo2HOB in CpuMpPei
  UefiCpuPkg: Consume MpInfo2Hob in PiSmmCpuDxe
  UefiCpuPkg: Add a new field in MpInfo2 HOB
  UefiCpuPkg: Cache core type in MpInfo2 HOB
  UefiCpuPkg: Avoid assuming only one smmbasehob

 UefiCpuPkg/CpuMpPei/CpuMpPei.c   | 146 
++
 UefiCpuPkg/CpuMpPei/CpuMpPei.h   |   6 +-
 UefiCpuPkg/CpuMpPei/CpuMpPei.inf |   3 ++-
 UefiCpuPkg/Include/Guid/MpInformation2.h |  58 
++
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c   | 354 
++
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h   |   2 +-
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf |   8 
 UefiCpuPkg/UefiCpuPkg.dec|   3 +++
 8 files changed, 513 insertions(+), 67 deletions(-)  create mode 100644 
UefiCpuPkg/Include/Guid/MpInformation2.h

--
2.31.1.windows.1








-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#112267): https://edk2.groups.io/g/devel/message/112267
Mute This Topic: https://groups.io/mt/103052268/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V3 6/6] UefiCpuPkg: Avoid assuming only one smmbasehob

2023-12-08 Thread duntan
Modify the gSmmBaseHobGuid consumption code to
remove the asuumption that there is only one
gSmmBaseHobGuid. If the CPU number is big enough,
there will be more than one SmmBaseHob in the
HOB list.

Signed-off-by: Dun Tan 
Cc: Eric Dong 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c | 181 
+
 1 file changed, 149 insertions(+), 32 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
index 53f67d544d..209a2e4810 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
@@ -586,6 +586,132 @@ SmmReadyToLockEventNotify (
   return EFI_SUCCESS;
 }
 
+/**
+  Function to compare 2 SMM_BASE_HOB_DATA pointer based on ProcessorIndex.
+
+  @param[in] Buffer1pointer to SMM_BASE_HOB_DATA poiner to compare
+  @param[in] Buffer2pointer to second SMM_BASE_HOB_DATA pointer to 
compare
+
+  @retval 0 Buffer1 equal to Buffer2
+  @retval <0Buffer1 is less than Buffer2
+  @retval >0Buffer1 is greater than Buffer2
+**/
+INTN
+EFIAPI
+SmBaseHobCompare (
+  IN  CONST VOID  *Buffer1,
+  IN  CONST VOID  *Buffer2
+  )
+{
+  if ((*(SMM_BASE_HOB_DATA **)Buffer1)->ProcessorIndex > (*(SMM_BASE_HOB_DATA 
**)Buffer2)->ProcessorIndex) {
+return 1;
+  } else if ((*(SMM_BASE_HOB_DATA **)Buffer1)->ProcessorIndex < 
(*(SMM_BASE_HOB_DATA **)Buffer2)->ProcessorIndex) {
+return -1;
+  }
+
+  return 0;
+}
+
+/**
+  Extract SmBase for all CPU from SmmBase HOB.
+
+  @param[in]  MaxNumberOfCpus   Max NumberOfCpus.
+
+  @retval SmBaseBuffer  Pointer to SmBase Buffer.
+  @retval NULL  gSmmBaseHobGuid was not been created.
+**/
+UINTN *
+GetSmBase (
+  IN  UINTN  MaxNumberOfCpus
+  )
+{
+  UINTN  HobCount;
+  EFI_HOB_GUID_TYPE  *GuidHob;
+  SMM_BASE_HOB_DATA  *SmmBaseHobData;
+  UINTN  NumberOfProcessors;
+  SMM_BASE_HOB_DATA  **SmBaseHobs;
+  UINTN  *SmBaseBuffer;
+  UINTN  HobIndex;
+  UINTN  SortBuffer;
+  UINTN  ProcessorIndex;
+  UINT64 PrevProcessorIndex;
+  EFI_HOB_GUID_TYPE  *FirstSmmBaseGuidHob;
+
+  SmmBaseHobData = NULL;
+  HobIndex   = 0;
+  ProcessorIndex = 0;
+  HobCount   = 0;
+  NumberOfProcessors = 0;
+
+  FirstSmmBaseGuidHob = GetFirstGuidHob ();
+  if (FirstSmmBaseGuidHob == NULL) {
+return NULL;
+  }
+
+  GuidHob = FirstSmmBaseGuidHob;
+  while (GuidHob != NULL) {
+HobCount++;
+SmmBaseHobData  = GET_GUID_HOB_DATA (GuidHob);
+NumberOfProcessors += SmmBaseHobData->NumberOfProcessors;
+
+if (NumberOfProcessors >= MaxNumberOfCpus) {
+  break;
+}
+
+GuidHob = GetNextGuidHob (, GET_NEXT_HOB (GuidHob));
+  }
+
+  ASSERT (NumberOfProcessors == MaxNumberOfCpus);
+  if (NumberOfProcessors != MaxNumberOfCpus) {
+CpuDeadLoop ();
+  }
+
+  SmBaseHobs = AllocatePool (sizeof (SMM_BASE_HOB_DATA *) * HobCount);
+  ASSERT (SmBaseHobs != NULL);
+  if (SmBaseHobs == NULL) {
+return NULL;
+  }
+
+  //
+  // Record each SmmBaseHob pointer in the SmBaseHobs.
+  // The FirstSmmBaseGuidHob is to speed up this while-loop
+  // without needing to look for SmBaseHob from beginning.
+  //
+  GuidHob = FirstSmmBaseGuidHob;
+  while (HobIndex < HobCount) {
+SmBaseHobs[HobIndex++] = GET_GUID_HOB_DATA (GuidHob);
+GuidHob= GetNextGuidHob (, GET_NEXT_HOB 
(GuidHob));
+  }
+
+  SmBaseBuffer = (UINTN *)AllocatePool (sizeof (UINTN) * (MaxNumberOfCpus));
+  ASSERT (SmBaseBuffer != NULL);
+  if (SmBaseBuffer == NULL) {
+FreePool (SmBaseHobs);
+return NULL;
+  }
+
+  QuickSort (SmBaseHobs, HobCount, sizeof (SMM_BASE_HOB_DATA *), 
(BASE_SORT_COMPARE)SmBaseHobCompare, );
+  PrevProcessorIndex = 0;
+  for (HobIndex = 0; HobIndex < HobCount; HobIndex++) {
+//
+// Make sure no overlap and no gap in the CPU range covered by each HOB
+//
+ASSERT (SmBaseHobs[HobIndex]->ProcessorIndex == PrevProcessorIndex);
+
+//
+// Cache each SmBase in order.
+//
+for (ProcessorIndex = 0; ProcessorIndex < 
SmBaseHobs[HobIndex]->NumberOfProcessors; ProcessorIndex++) {
+  SmBaseBuffer[PrevProcessorIndex + ProcessorIndex] = 
(UINTN)SmBaseHobs[HobIndex]->SmBase[ProcessorIndex];
+}
+
+PrevProcessorIndex += SmBaseHobs[HobIndex]->NumberOfProcessors;
+  }
+
+  FreePool (SmBaseHobs);
+  return SmBaseBuffer;
+}
+
 /**
   Function to compare 2 MP_INFORMATION2_HOB_DATA pointer based on 
ProcessorIndex.
 
@@ -743,27 +869,22 @@ PiCpuSmmEntry (
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
 {
-  EFI_STATUS Status;
-  UINTN  Index;
-  VOID   *Buffer;
-  UINTN  BufferPages;
-  UINTN 

[edk2-devel] [Patch V3 3/6] UefiCpuPkg: Consume MpInfo2Hob in PiSmmCpuDxe

2023-12-08 Thread duntan
Consume MpInfo2Hob in PiSmmCpuDxe driver to get
NumberOfProcessors, MaxNumberOfCpus and
EFI_PROCESSOR_INFORMATION for all CPU from the
MpInformation2 HOB.
This can avoid calling MP service.

Signed-off-by: Dun Tan 
Cc: Eric Dong 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c   | 209 
+++--
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h   |   2 +-
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf |   8 
 3 files changed, 168 insertions(+), 51 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
index 1d022a7051..53f67d544d 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
@@ -99,8 +99,8 @@ UINTN  mSmmStackSize;
 UINTNmSmmShadowStackSize;
 BOOLEAN  mCetSupported = TRUE;
 
-UINTN  mMaxNumberOfCpus = 1;
-UINTN  mNumberOfCpus= 1;
+UINTN  mMaxNumberOfCpus = 0;
+UINTN  mNumberOfCpus= 0;
 
 //
 // SMM ready to lock flag
@@ -586,6 +586,146 @@ SmmReadyToLockEventNotify (
   return EFI_SUCCESS;
 }
 
+/**
+  Function to compare 2 MP_INFORMATION2_HOB_DATA pointer based on 
ProcessorIndex.
+
+  @param[in] Buffer1pointer to MP_INFORMATION2_HOB_DATA poiner to 
compare
+  @param[in] Buffer2pointer to second MP_INFORMATION2_HOB_DATA 
pointer to compare
+
+  @retval 0 Buffer1 equal to Buffer2
+  @retval <0Buffer1 is less than Buffer2
+  @retval >0Buffer1 is greater than Buffer2
+**/
+INTN
+EFIAPI
+MpInformation2HobCompare (
+  IN  CONST VOID  *Buffer1,
+  IN  CONST VOID  *Buffer2
+  )
+{
+  if ((*(MP_INFORMATION2_HOB_DATA **)Buffer1)->ProcessorIndex > 
(*(MP_INFORMATION2_HOB_DATA **)Buffer2)->ProcessorIndex) {
+return 1;
+  } else if ((*(MP_INFORMATION2_HOB_DATA **)Buffer1)->ProcessorIndex < 
(*(MP_INFORMATION2_HOB_DATA **)Buffer2)->ProcessorIndex) {
+return -1;
+  }
+
+  return 0;
+}
+
+/**
+  Extract NumberOfCpus, MaxNumberOfCpus and EFI_PROCESSOR_INFORMATION for all 
CPU from MpInformation2 HOB.
+
+  @param[out] NumberOfCpus   Pointer to NumberOfCpus.
+  @param[out] MaxNumberOfCpusPointer to MaxNumberOfCpus.
+
+  @retval ProcessorInfo  Pointer to EFI_PROCESSOR_INFORMATION 
buffer.
+**/
+EFI_PROCESSOR_INFORMATION *
+GetMpInformation (
+  OUT UINTN  *NumberOfCpus,
+  OUT UINTN  *MaxNumberOfCpus
+  )
+{
+  EFI_HOB_GUID_TYPE  *GuidHob;
+  EFI_HOB_GUID_TYPE  *FirstMpInfo2Hob;
+  MP_INFORMATION2_HOB_DATA   *MpInformation2HobData;
+  UINTN  HobCount;
+  UINTN  HobIndex;
+  MP_INFORMATION2_HOB_DATA   **MpInfo2Hobs;
+  UINTN  SortBuffer;
+  UINTN  ProcessorIndex;
+  UINT64 PrevProcessorIndex;
+  MP_INFORMATION2_ENTRY  *MpInformation2Entry;
+  EFI_PROCESSOR_INFORMATION  *ProcessorInfo;
+
+  GuidHob   = NULL;
+  MpInformation2HobData = NULL;
+  FirstMpInfo2Hob   = NULL;
+  MpInfo2Hobs   = NULL;
+  HobIndex  = 0;
+  HobCount  = 0;
+
+  FirstMpInfo2Hob = GetFirstGuidHob ();
+  ASSERT (FirstMpInfo2Hob != NULL);
+  GuidHob = FirstMpInfo2Hob;
+  while (GuidHob != NULL) {
+MpInformation2HobData = GET_GUID_HOB_DATA (GuidHob);
+
+//
+// This is the last MpInformationHob in the HOB list.
+//
+if (MpInformation2HobData->NumberOfProcessors == 0) {
+  ASSERT (HobCount != 0);
+  break;
+}
+
+HobCount++;
+*NumberOfCpus += MpInformation2HobData->NumberOfProcessors;
+GuidHob= GetNextGuidHob (, GET_NEXT_HOB 
(GuidHob));
+  }
+
+  ASSERT (*NumberOfCpus <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));
+
+  //
+  // If support CPU hot plug, we need to allocate resources for possibly 
hot-added processors
+  //
+  if (FeaturePcdGet (PcdCpuHotPlugSupport)) {
+*MaxNumberOfCpus = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
+  } else {
+*MaxNumberOfCpus = *NumberOfCpus;
+  }
+
+  MpInfo2Hobs = AllocatePool (sizeof (MP_INFORMATION2_HOB_DATA *) * HobCount);
+  ASSERT (MpInfo2Hobs != NULL);
+  if (MpInfo2Hobs == NULL) {
+return NULL;
+  }
+
+  //
+  // Record each MpInformation2Hob pointer in the MpInfo2Hobs.
+  // The FirstMpInfo2Hob is to speed up this while-loop without
+  // needing to look for MpInfo2Hob from beginning.
+  //
+  GuidHob = FirstMpInfo2Hob;
+  while (HobIndex < HobCount) {
+MpInfo2Hobs[HobIndex++] = GET_GUID_HOB_DATA (GuidHob);
+GuidHob = GetNextGuidHob (, 
GET_NEXT_HOB (GuidHob));
+  }
+
+  ProcessorInfo = (EFI_PROCESSOR_INFORMATION *)AllocatePool (sizeof 
(EFI_PROCESSOR_INFORMATION) * (*MaxNumberOfCpus));
+  ASSERT (ProcessorInfo != NULL);
+  if (ProcessorInfo == NULL) {
+

[edk2-devel] [Patch V3 2/6] UefiCpuPkg: Build MpInfo2HOB in CpuMpPei

2023-12-08 Thread duntan
Build MpInfo2HOB in CpuMpPei module so that later
PiSmmCpuDxe or other StandaloneMm module can consume
the HOB.
Since there might be more one gMpInformationHobGuid2
in HOB list, CpuMpPei create a gMpInformationHobGuid2
with 0 value NumberOfProcessors field in the end of the
process to indicate it's the last MP_INFORMATION2_HOB.

Signed-off-by: Dun Tan 
Cc: Eric Dong 
Reviewed-by: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/CpuMpPei/CpuMpPei.c   | 91 
+++
 UefiCpuPkg/CpuMpPei/CpuMpPei.h   |  4 +++-
 UefiCpuPkg/CpuMpPei/CpuMpPei.inf |  3 ++-
 3 files changed, 96 insertions(+), 2 deletions(-)

diff --git a/UefiCpuPkg/CpuMpPei/CpuMpPei.c b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
index b504bea3cf..e96a76c9a5 100644
--- a/UefiCpuPkg/CpuMpPei/CpuMpPei.c
+++ b/UefiCpuPkg/CpuMpPei/CpuMpPei.c
@@ -541,6 +541,92 @@ InitializeMpExceptionStackSwitchHandlers (
   FreePages (SwitchStackData, EFI_SIZE_TO_PAGES (NumberOfProcessors * sizeof 
(EXCEPTION_STACK_SWITCH_CONTEXT)));
 }
 
+/**
+  Create gMpInformationHobGuid2.
+**/
+VOID
+BuildMpInformationHob (
+  VOID
+  )
+{
+  EFI_STATUSStatus;
+  UINTN ProcessorIndex;
+  UINTN NumberOfProcessors;
+  UINTN NumberOfEnabledProcessors;
+  UINTN NumberOfProcessorsInHob;
+  UINTN MaxProcessorsPerHob;
+  MP_INFORMATION2_HOB_DATA  *MpInformation2HobData;
+  MP_INFORMATION2_ENTRY *MpInformation2Entry;
+  UINTN Index;
+
+  ProcessorIndex= 0;
+  MpInformation2HobData = NULL;
+  MpInformation2Entry   = NULL;
+
+  Status = MpInitLibGetNumberOfProcessors (, 
);
+  ASSERT_EFI_ERROR (Status);
+  MaxProcessorsPerHob = ((MAX_UINT16 & ~7) - sizeof (EFI_HOB_GUID_TYPE) - 
sizeof (MP_INFORMATION2_HOB_DATA)) / sizeof (MP_INFORMATION2_ENTRY);
+  NumberOfProcessorsInHob = MaxProcessorsPerHob;
+
+  //
+  // Create MP_INFORMATION2_HOB. when the max HobLength 0xFFF8 is not enough, 
there
+  // will be a MP_INFORMATION2_HOB series in the HOB list.
+  // In the HOB list, there is a gMpInformationHobGuid2 with 0 value 
NumberOfProcessors
+  // fields to indicate it's the last MP_INFORMATION2_HOB.
+  //
+  while (NumberOfProcessorsInHob != 0) {
+NumberOfProcessorsInHob = MIN (NumberOfProcessors - ProcessorIndex, 
MaxProcessorsPerHob);
+MpInformation2HobData   = BuildGuidHob (
+,
+sizeof (MP_INFORMATION2_HOB_DATA) + sizeof 
(MP_INFORMATION2_ENTRY) * NumberOfProcessorsInHob
+);
+ASSERT (MpInformation2HobData != NULL);
+
+MpInformation2HobData->Version= MP_INFORMATION2_HOB_REVISION;
+MpInformation2HobData->ProcessorIndex = ProcessorIndex;
+MpInformation2HobData->NumberOfProcessors = 
(UINT16)NumberOfProcessorsInHob;
+MpInformation2HobData->EntrySize  = sizeof (MP_INFORMATION2_ENTRY);
+
+DEBUG ((DEBUG_INFO, "Creating MpInformation2 HOB...\n"));
+
+for (Index = 0; Index < NumberOfProcessorsInHob; Index++) {
+  MpInformation2Entry = >Entry[Index];
+  Status  = MpInitLibGetProcessorInfo (
+  (Index + ProcessorIndex) | 
CPU_V2_EXTENDED_TOPOLOGY,
+  >ProcessorInfo,
+  NULL
+  );
+  ASSERT_EFI_ERROR (Status);
+  DEBUG ((
+DEBUG_INFO,
+"  Processor[%04d]: ProcessorId = 0x%lx, StatusFlag = 0x%x\n",
+Index + ProcessorIndex,
+MpInformation2Entry->ProcessorInfo.ProcessorId,
+MpInformation2Entry->ProcessorInfo.StatusFlag
+));
+  DEBUG ((
+DEBUG_INFO,
+"Location = Package:%d Core:%d Thread:%d\n",
+MpInformation2Entry->ProcessorInfo.Location.Package,
+MpInformation2Entry->ProcessorInfo.Location.Core,
+MpInformation2Entry->ProcessorInfo.Location.Thread
+));
+  DEBUG ((
+DEBUG_INFO,
+"Location2 = Package:%d Die:%d Tile:%d Module:%d Core:%d 
Thread:%d\n",
+
MpInformation2Entry->ProcessorInfo.ExtendedInformation.Location2.Package,
+MpInformation2Entry->ProcessorInfo.ExtendedInformation.Location2.Die,
+MpInformation2Entry->ProcessorInfo.ExtendedInformation.Location2.Tile,
+
MpInformation2Entry->ProcessorInfo.ExtendedInformation.Location2.Module,
+MpInformation2Entry->ProcessorInfo.ExtendedInformation.Location2.Core,
+MpInformation2Entry->ProcessorInfo.ExtendedInformation.Location2.Thread
+));
+}
+
+ProcessorIndex += NumberOfProcessorsInHob;
+  }
+}
+
 /**
   Initializes MP and exceptions handlers.
 
@@ -602,6 +688,11 @@ InitializeCpuMpWorker (
   Status = PeiServicesInstallPpi (mPeiCpuMpPpiList);
   ASSERT_EFI_ERROR (Status);
 
+  //
+  // Create gMpInformationHobGuid2
+  //
+  BuildMpInformationHob ();
+
   

[edk2-devel] [Patch V3 0/6] Create and consume a new gMpInformationHobGuid2 in UefiCpuPkg.

2023-12-08 Thread duntan
In the V3 patch set,
In patch "UefiCpuPkg: Build MpInfo2HOB in CpuMpPei", the DEBUG message format 
is modified
In patch "UefiCpuPkg: Consume MpInfo2Hob in PiSmmCpuDxe", remove unneccesary 
assert check.
In patch "UefiCpuPkg: Avoid assuming only one smmbasehob", free allocated 
buffer when error returning case happen.

Dun Tan (6):
  UefiCpuPkg: Create gMpInformationHobGuid2 in UefiCpuPkg
  UefiCpuPkg: Build MpInfo2HOB in CpuMpPei
  UefiCpuPkg: Consume MpInfo2Hob in PiSmmCpuDxe
  UefiCpuPkg: Add a new field in MpInfo2 HOB
  UefiCpuPkg: Cache core type in MpInfo2 HOB
  UefiCpuPkg: Avoid assuming only one smmbasehob

 UefiCpuPkg/CpuMpPei/CpuMpPei.c   | 146 
++
 UefiCpuPkg/CpuMpPei/CpuMpPei.h   |   6 +-
 UefiCpuPkg/CpuMpPei/CpuMpPei.inf |   3 ++-
 UefiCpuPkg/Include/Guid/MpInformation2.h |  58 
++
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c   | 354 
++
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h   |   2 +-
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf |   8 
 UefiCpuPkg/UefiCpuPkg.dec|   3 +++
 8 files changed, 513 insertions(+), 67 deletions(-)
 create mode 100644 UefiCpuPkg/Include/Guid/MpInformation2.h

-- 
2.31.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#112221): https://edk2.groups.io/g/devel/message/112221
Mute This Topic: https://groups.io/mt/103052268/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-




[edk2-devel] [Patch V2 3/6] UefiCpuPkg: Consume MpInfo2Hob in PiSmmCpuDxe

2023-12-06 Thread duntan
Consume MpInfo2Hob in PiSmmCpuDxe driver to get
NumberOfProcessors, MaxNumberOfCpus and
EFI_PROCESSOR_INFORMATION for all CPU from the
MpInformation2 HOB.
This can avoid calling MP service.

Signed-off-by: Dun Tan 
Cc: Eric Dong 
Cc: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c   | 210 
--
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h   |   2 +-
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf |   8 
 3 files changed, 169 insertions(+), 51 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
index 1d022a7051..b729f8ee63 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
@@ -99,8 +99,8 @@ UINTN  mSmmStackSize;
 UINTNmSmmShadowStackSize;
 BOOLEAN  mCetSupported = TRUE;
 
-UINTN  mMaxNumberOfCpus = 1;
-UINTN  mNumberOfCpus= 1;
+UINTN  mMaxNumberOfCpus = 0;
+UINTN  mNumberOfCpus= 0;
 
 //
 // SMM ready to lock flag
@@ -586,6 +586,147 @@ SmmReadyToLockEventNotify (
   return EFI_SUCCESS;
 }
 
+/**
+  Function to compare 2 MP_INFORMATION2_HOB_DATA pointer based on 
ProcessorIndex.
+
+  @param[in] Buffer1pointer to MP_INFORMATION2_HOB_DATA poiner to 
compare
+  @param[in] Buffer2pointer to second MP_INFORMATION2_HOB_DATA 
pointer to compare
+
+  @retval 0 Buffer1 equal to Buffer2
+  @retval <0Buffer1 is less than Buffer2
+  @retval >0Buffer1 is greater than Buffer2
+**/
+INTN
+EFIAPI
+MpInformation2HobCompare (
+  IN  CONST VOID  *Buffer1,
+  IN  CONST VOID  *Buffer2
+  )
+{
+  if ((*(MP_INFORMATION2_HOB_DATA **)Buffer1)->ProcessorIndex > 
(*(MP_INFORMATION2_HOB_DATA **)Buffer2)->ProcessorIndex) {
+return 1;
+  } else if ((*(MP_INFORMATION2_HOB_DATA **)Buffer1)->ProcessorIndex < 
(*(MP_INFORMATION2_HOB_DATA **)Buffer2)->ProcessorIndex) {
+return -1;
+  }
+
+  return 0;
+}
+
+/**
+  Extract NumberOfCpus, MaxNumberOfCpus and EFI_PROCESSOR_INFORMATION for all 
CPU from MpInformation2 HOB.
+
+  @param[out] NumberOfCpus   Pointer to NumberOfCpus.
+  @param[out] MaxNumberOfCpusPointer to MaxNumberOfCpus.
+
+  @retval ProcessorInfo  Pointer to EFI_PROCESSOR_INFORMATION 
buffer.
+**/
+EFI_PROCESSOR_INFORMATION *
+GetMpInformation (
+  OUT UINTN  *NumberOfCpus,
+  OUT UINTN  *MaxNumberOfCpus
+  )
+{
+  EFI_HOB_GUID_TYPE  *GuidHob;
+  EFI_HOB_GUID_TYPE  *FirstMpInfo2Hob;
+  MP_INFORMATION2_HOB_DATA   *MpInformation2HobData;
+  UINTN  HobCount;
+  UINTN  HobIndex;
+  MP_INFORMATION2_HOB_DATA   **MpInfo2Hobs;
+  UINTN  SortBuffer;
+  UINTN  ProcessorIndex;
+  UINT64 PrevProcessorIndex;
+  MP_INFORMATION2_ENTRY  *MpInformation2Entry;
+  EFI_PROCESSOR_INFORMATION  *ProcessorInfo;
+
+  GuidHob   = NULL;
+  MpInformation2HobData = NULL;
+  FirstMpInfo2Hob   = NULL;
+  MpInfo2Hobs   = NULL;
+  HobIndex  = 0;
+  HobCount  = 0;
+
+  FirstMpInfo2Hob = GetFirstGuidHob ();
+  ASSERT (FirstMpInfo2Hob != NULL);
+  GuidHob = FirstMpInfo2Hob;
+  while (GuidHob != NULL) {
+MpInformation2HobData = GET_GUID_HOB_DATA (GuidHob);
+
+//
+// This is the last MpInformationHob in the HOB list.
+//
+if (MpInformation2HobData->NumberOfProcessors == 0) {
+  ASSERT (HobCount != 0);
+  break;
+}
+
+HobCount++;
+*NumberOfCpus += MpInformation2HobData->NumberOfProcessors;
+GuidHob= GetNextGuidHob (, GET_NEXT_HOB 
(GuidHob));
+  }
+
+  ASSERT (*NumberOfCpus <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));
+
+  //
+  // If support CPU hot plug, we need to allocate resources for possibly 
hot-added processors
+  //
+  if (FeaturePcdGet (PcdCpuHotPlugSupport)) {
+*MaxNumberOfCpus = PcdGet32 (PcdCpuMaxLogicalProcessorNumber);
+  } else {
+*MaxNumberOfCpus = *NumberOfCpus;
+  }
+
+  MpInfo2Hobs = AllocatePool (sizeof (MP_INFORMATION2_HOB_DATA *) * HobCount);
+  ASSERT (MpInfo2Hobs != NULL);
+  if (MpInfo2Hobs == NULL) {
+return NULL;
+  }
+
+  //
+  // Record each MpInformation2Hob pointer in the MpInfo2Hobs.
+  // The FirstMpInfo2Hob is to speed up this while-loop without
+  // needing to look for MpInfo2Hob from beginning.
+  //
+  GuidHob = FirstMpInfo2Hob;
+  ASSERT (GuidHob != NULL);
+  while (HobIndex < HobCount) {
+MpInfo2Hobs[HobIndex++] = GET_GUID_HOB_DATA (GuidHob);
+GuidHob = GetNextGuidHob (, 
GET_NEXT_HOB (GuidHob));
+  }
+
+  ProcessorInfo = (EFI_PROCESSOR_INFORMATION *)AllocatePool (sizeof 
(EFI_PROCESSOR_INFORMATION) * (*MaxNumberOfCpus));
+  ASSERT (ProcessorInfo != NULL);
+  if (ProcessorInfo == 

[edk2-devel] [Patch V2 6/6] UefiCpuPkg: Avoid assuming only one smmbasehob

2023-12-06 Thread duntan
Modify the gSmmBaseHobGuid consumption code to
remove the asuumption that there is only one
gSmmBaseHobGuid. If the CPU number is big enough,
there will be more than one SmmBaseHob in the
HOB list.

Signed-off-by: Dun Tan 
Cc: Eric Dong 
Cc: Ray Ni 
Cc: Rahul Kumar 
Cc: Gerd Hoffmann 
---
 UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c | 179 
+++
 1 file changed, 147 insertions(+), 32 deletions(-)

diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c 
b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
index b729f8ee63..fab2fed286 100644
--- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
+++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.c
@@ -586,6 +586,132 @@ SmmReadyToLockEventNotify (
   return EFI_SUCCESS;
 }
 
+/**
+  Function to compare 2 SMM_BASE_HOB_DATA pointer based on ProcessorIndex.
+
+  @param[in] Buffer1pointer to SMM_BASE_HOB_DATA poiner to compare
+  @param[in] Buffer2pointer to second SMM_BASE_HOB_DATA pointer to 
compare
+
+  @retval 0 Buffer1 equal to Buffer2
+  @retval <0Buffer1 is less than Buffer2
+  @retval >0Buffer1 is greater than Buffer2
+**/
+INTN
+EFIAPI
+SmBaseHobCompare (
+  IN  CONST VOID  *Buffer1,
+  IN  CONST VOID  *Buffer2
+  )
+{
+  if ((*(SMM_BASE_HOB_DATA **)Buffer1)->ProcessorIndex > (*(SMM_BASE_HOB_DATA 
**)Buffer2)->ProcessorIndex) {
+return 1;
+  } else if ((*(SMM_BASE_HOB_DATA **)Buffer1)->ProcessorIndex < 
(*(SMM_BASE_HOB_DATA **)Buffer2)->ProcessorIndex) {
+return -1;
+  }
+
+  return 0;
+}
+
+/**
+  Extract SmBase for all CPU from SmmBase HOB.
+
+  @param[in]  MaxNumberOfCpus   Max NumberOfCpus.
+
+  @retval SmBaseBuffer  Pointer to SmBase Buffer.
+  @retval NULL  gSmmBaseHobGuid was not been created.
+**/
+UINTN *
+GetSmBase (
+  IN  UINTN  MaxNumberOfCpus
+  )
+{
+  UINTN  HobCount;
+  EFI_HOB_GUID_TYPE  *GuidHob;
+  SMM_BASE_HOB_DATA  *SmmBaseHobData;
+  UINTN  NumberOfProcessors;
+  SMM_BASE_HOB_DATA  **SmBaseHobs;
+  UINTN  *SmBaseBuffer;
+  UINTN  HobIndex;
+  UINTN  SortBuffer;
+  UINTN  ProcessorIndex;
+  UINT64 PrevProcessorIndex;
+  EFI_HOB_GUID_TYPE  *FirstSmmBaseGuidHob;
+
+  SmmBaseHobData = NULL;
+  HobIndex   = 0;
+  ProcessorIndex = 0;
+  HobCount   = 0;
+  NumberOfProcessors = 0;
+
+  FirstSmmBaseGuidHob = GetFirstGuidHob ();
+  if (FirstSmmBaseGuidHob == NULL) {
+return NULL;
+  }
+
+  GuidHob = FirstSmmBaseGuidHob;
+  while (GuidHob != NULL) {
+HobCount++;
+SmmBaseHobData = GET_GUID_HOB_DATA (GuidHob);
+
+if (NumberOfProcessors >= MaxNumberOfCpus) {
+  break;
+}
+
+NumberOfProcessors += SmmBaseHobData->NumberOfProcessors;
+GuidHob = GetNextGuidHob (, GET_NEXT_HOB 
(GuidHob));
+  }
+
+  ASSERT (NumberOfProcessors == MaxNumberOfCpus);
+  if (NumberOfProcessors != MaxNumberOfCpus) {
+CpuDeadLoop ();
+  }
+
+  SmBaseHobs = AllocatePool (sizeof (SMM_BASE_HOB_DATA *) * HobCount);
+  ASSERT (SmBaseHobs != NULL);
+  if (SmBaseHobs == NULL) {
+return NULL;
+  }
+
+  //
+  // Record each SmmBaseHob pointer in the SmBaseHobs.
+  // The FirstSmmBaseGuidHob is to speed up this while-loop
+  // without needing to look for SmBaseHob from beginning.
+  //
+  GuidHob = FirstSmmBaseGuidHob;
+  while (HobIndex < HobCount) {
+SmBaseHobs[HobIndex++] = GET_GUID_HOB_DATA (GuidHob);
+GuidHob= GetNextGuidHob (, GET_NEXT_HOB 
(GuidHob));
+  }
+
+  SmBaseBuffer = (UINTN *)AllocatePool (sizeof (UINTN) * (MaxNumberOfCpus));
+  ASSERT (SmBaseBuffer != NULL);
+  if (SmBaseBuffer == NULL) {
+FreePool (SmBaseHobs);
+return NULL;
+  }
+
+  QuickSort (SmBaseHobs, HobCount, sizeof (SMM_BASE_HOB_DATA *), 
(BASE_SORT_COMPARE)SmBaseHobCompare, );
+  PrevProcessorIndex = 0;
+  for (HobIndex = 0; HobIndex < HobCount; HobIndex++) {
+//
+// Make sure no overlap and no gap in the CPU range covered by each HOB
+//
+ASSERT (SmBaseHobs[HobIndex]->ProcessorIndex == PrevProcessorIndex);
+
+//
+// Cache each SmBase in order.
+//
+for (ProcessorIndex = 0; ProcessorIndex < 
SmBaseHobs[HobIndex]->NumberOfProcessors; ProcessorIndex++) {
+  SmBaseBuffer[PrevProcessorIndex + ProcessorIndex] = 
(UINTN)SmBaseHobs[HobIndex]->SmBase[ProcessorIndex];
+}
+
+PrevProcessorIndex += SmBaseHobs[HobIndex]->NumberOfProcessors;
+  }
+
+  FreePool (SmBaseHobs);
+  return SmBaseBuffer;
+}
+
 /**
   Function to compare 2 MP_INFORMATION2_HOB_DATA pointer based on 
ProcessorIndex.
 
@@ -744,27 +870,22 @@ PiCpuSmmEntry (
   IN EFI_SYSTEM_TABLE  *SystemTable
   )
 {
-  EFI_STATUS Status;
-  UINTN  Index;
-  VOID   *Buffer;
-  UINTN  BufferPages;
-  UINTN  

  1   2   3   4   5   6   >