public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel] [Patch V7 2/2] UefiCpuPkg/MpInitLib: Update the comments of _CPU_MP_DATA.
  2023-11-21  2:34 [edk2-devel] [Patch V7 0/2]UefiCpuPkg/MpInitLib: Enable execute disable bit Yuanhao Xie
@ 2023-11-21  2:34 ` Yuanhao Xie
  0 siblings, 0 replies; 5+ messages in thread
From: Yuanhao Xie @ 2023-11-21  2:34 UTC (permalink / raw)
  To: devel; +Cc: xieyuanh, Eric Dong, Ray Ni, Rahul Kumar, Gerd Hoffmann

No functional changes in this patch.

Updates the comments of _CPU_MP_DATA to delcared that duplications in
CpuMpData are present to avoid to be direct accessed and comprehended
 in assembly code. CpuMpData: Intended for use in C code while
 ExchangeInfo are used in assembly code in this module.

This patch deletes the unnecessary comments in CpuMpData, since
CpuMpData is no longer responsible for passing information from PEI to
DXE.

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
Cc: Laszlo Ersek lersek@redhat.com
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MpInitLib/MpEqu.inc |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.h   | 13 +++++++------
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc b/UefiCpuPkg/Library/MpInitLib/MpEqu.inc
index 72af196513..317e627b58 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc
+++ b/UefiCpuPkg/Library/MpInitLib/MpEqu.inc
@@ -67,6 +67,8 @@ endstruc
 
 ;
 ; Equivalent NASM structure of MP_CPU_EXCHANGE_INFO
+; Assembly routines should refrain from directly interacting with
+; the internal details of CPU_MP_DATA.
 ;
 struc MP_CPU_EXCHANGE_INFO
   .StackStart:                   CTYPE_UINTN 1
diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpInitLib/MpLib.h
index af296f6ac0..a96a6389c1 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.h
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h
@@ -203,6 +203,8 @@ typedef struct _CPU_MP_DATA CPU_MP_DATA;
 // MP CPU exchange information for AP reset code
 // This structure is required to be packed because fixed field offsets
 // into this structure are used in assembly code in this module
+// Assembly routines should refrain from directly interacting with
+// the internal details of CPU_MP_DATA.
 //
 typedef struct {
   UINTN              StackStart;
@@ -239,17 +241,16 @@ typedef struct {
 #pragma pack()
 
 //
-// CPU MP Data save in memory
+// CPU MP Data save in memory, and intended for use in C code.
+// There are some duplicated fields, such as XD status, between
+// CpuMpData and ExchangeInfo. These duplications in CpuMpData
+// are present to avoid to be direct accessed and comprehended
+// in assembly code.
 //
 struct _CPU_MP_DATA {
   UINT64                           CpuInfoInHob;
   UINT32                           CpuCount;
   UINT32                           BspNumber;
-  //
-  // The above fields data will be passed from PEI to DXE
-  // Please make sure the fields offset same in the different
-  // architecture.
-  //
   SPIN_LOCK                        MpLock;
   UINTN                            Buffer;
   UINTN                            CpuApStackSize;
-- 
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#111506): https://edk2.groups.io/g/devel/message/111506
Mute This Topic: https://groups.io/mt/102721665/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



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

* [edk2-devel] [Patch V7 0/2]UefiCpuPkg/MpInitLib: Enable execute disable bit.
@ 2023-11-21  2:35 Yuanhao Xie
  2023-11-21  2:35 ` [edk2-devel] [Patch V7 1/2] UefiCpuPkg/MpInitLib: " Yuanhao Xie
  2023-11-21  2:35 ` [edk2-devel] [Patch V7 2/2] UefiCpuPkg/MpInitLib: Update the comments of _CPU_MP_DATA Yuanhao Xie
  0 siblings, 2 replies; 5+ messages in thread
From: Yuanhao Xie @ 2023-11-21  2:35 UTC (permalink / raw)
  To: devel; +Cc: xieyuanh

This patch series synchronizes the No-Execute bit in the IA32_EFER
register for the APs before the RestoreVolatileRegisters operation.
It also updated the comments of _CPU_MP_DATA to delcared that
duplications in CpuMpData are present to avoid to be direct accessed
and comprehended in assembly code.

It updated the structure comments compared with V6.

Yuanhao Xie (1):
  UefiCpuPkg/MpInitLib: Enable execute disable bit.

xieyuanh (1):
  UefiCpuPkg/MpInitLib: Update the comments of _CPU_MP_DATA.

 UefiCpuPkg/Library/MpInitLib/MpEqu.inc |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.c   | 14 +++++++++++---
 UefiCpuPkg/Library/MpInitLib/MpLib.h   | 14 ++++++++------
 3 files changed, 21 insertions(+), 9 deletions(-)

--
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#111507): https://edk2.groups.io/g/devel/message/111507
Mute This Topic: https://groups.io/mt/102721660/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



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

* [edk2-devel] [Patch V7 1/2] UefiCpuPkg/MpInitLib: Enable execute disable bit.
  2023-11-21  2:35 [edk2-devel] [Patch V7 0/2]UefiCpuPkg/MpInitLib: Enable execute disable bit Yuanhao Xie
@ 2023-11-21  2:35 ` Yuanhao Xie
  2023-11-21  2:35 ` [edk2-devel] [Patch V7 2/2] UefiCpuPkg/MpInitLib: Update the comments of _CPU_MP_DATA Yuanhao Xie
  1 sibling, 0 replies; 5+ messages in thread
From: Yuanhao Xie @ 2023-11-21  2:35 UTC (permalink / raw)
  To: devel
  Cc: Yuanhao Xie, Laszlo Ersek, Ray Ni, Eric Dong, Rahul Kumar,
	Gerd Hoffmann

From: Yuanhao Xie <yuanhao.xie@intel.com>

This patch synchronizes the No-Execute bit in the IA32_EFER
register for the APs before the RestoreVolatileRegisters operation.

The commit 964a4f0, titled "Eliminate the second INIT-SIPI-SIPI
sequence," replaces the second INIT-SIPI-SIPI sequence with the BSP
calling the SwitchApContext function to initiate a specialized start-up
signal, waking up APs in the DXE instead of using INIT-SIPI-SIPI.

Due to this change, the logic for "Enable execute disable bit" in
MpFuncs.nasm is no longer executed. However, to ensure the proper setup
of the page table, it is necessary to synchronize the IA32_EFER.NXE for
APs before executing RestoreVolatileRegisters .

Based on SDM:
If IA32_EFER.NXE is set to 1, it signifies execute-disable, meaning
instruction fetches are not allowed from the 4-KByte page controlled by
this entry. Conversely, if it is set to 0, it is reserved.

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek lersek@redhat.com
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MpInitLib/MpLib.c | 14 +++++++++++---
 UefiCpuPkg/Library/MpInitLib/MpLib.h |  1 +
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.c b/UefiCpuPkg/Library/MpInitLib/MpLib.c
index 9a6ec5db5c..f29e66a14f 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.c
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.c
@@ -910,9 +910,16 @@ DxeApEntryPoint (
   CPU_MP_DATA  *CpuMpData
   )
 {
-  UINTN  ProcessorNumber;
+  UINTN                   ProcessorNumber;
+  MSR_IA32_EFER_REGISTER  EferMsr;
 
   GetProcessorNumber (CpuMpData, &ProcessorNumber);
+  if (CpuMpData->EnableExecuteDisableForSwitchContext) {
+    EferMsr.Uint64   = AsmReadMsr64 (MSR_IA32_EFER);
+    EferMsr.Bits.NXE = 1;
+    AsmWriteMsr64 (MSR_IA32_EFER, EferMsr.Uint64);
+  }
+
   RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);
   InterlockedIncrement ((UINT32 *)&CpuMpData->FinishedCount);
   PlaceAPInMwaitLoopOrRunLoop (
@@ -2188,8 +2195,9 @@ MpInitLibInitialize (
     if (MpHandOff->WaitLoopExecutionMode == sizeof (VOID *)) {
       ASSERT (CpuMpData->ApLoopMode != ApInHltLoop);
 
-      CpuMpData->FinishedCount = 0;
-      CpuMpData->InitFlag      = ApInitDone;
+      CpuMpData->FinishedCount                        = 0;
+      CpuMpData->InitFlag                             = ApInitDone;
+      CpuMpData->EnableExecuteDisableForSwitchContext = IsBspExecuteDisableEnabled ();
       SaveCpuMpData (CpuMpData);
       //
       // In scenarios where both the PEI and DXE phases run in the same
diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpInitLib/MpLib.h
index 763db4963d..af296f6ac0 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.h
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h
@@ -270,6 +270,7 @@ struct _CPU_MP_DATA {
   UINT64                           TotalTime;
   EFI_EVENT                        WaitEvent;
   UINTN                            **FailedCpuList;
+  BOOLEAN                          EnableExecuteDisableForSwitchContext;
 
   AP_INIT_STATE                    InitFlag;
   BOOLEAN                          SwitchBspFlag;
-- 
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#111508): https://edk2.groups.io/g/devel/message/111508
Mute This Topic: https://groups.io/mt/102721680/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



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

* [edk2-devel] [Patch V7 2/2] UefiCpuPkg/MpInitLib: Update the comments of _CPU_MP_DATA.
  2023-11-21  2:35 [edk2-devel] [Patch V7 0/2]UefiCpuPkg/MpInitLib: Enable execute disable bit Yuanhao Xie
  2023-11-21  2:35 ` [edk2-devel] [Patch V7 1/2] UefiCpuPkg/MpInitLib: " Yuanhao Xie
@ 2023-11-21  2:35 ` Yuanhao Xie
  2023-11-22 16:41   ` Laszlo Ersek
  1 sibling, 1 reply; 5+ messages in thread
From: Yuanhao Xie @ 2023-11-21  2:35 UTC (permalink / raw)
  To: devel; +Cc: xieyuanh, Eric Dong, Ray Ni, Rahul Kumar, Gerd Hoffmann

No functional changes in this patch.

Updates the comments of _CPU_MP_DATA to delcared that duplications in
CpuMpData are present to avoid to be direct accessed and comprehended
 in assembly code. CpuMpData: Intended for use in C code while
 ExchangeInfo are used in assembly code in this module.

This patch deletes the unnecessary comments in CpuMpData, since
CpuMpData is no longer responsible for passing information from PEI to
DXE.

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
Cc: Laszlo Ersek lersek@redhat.com
Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Rahul Kumar <rahul1.kumar@intel.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>
---
 UefiCpuPkg/Library/MpInitLib/MpEqu.inc |  2 ++
 UefiCpuPkg/Library/MpInitLib/MpLib.h   | 13 +++++++------
 2 files changed, 9 insertions(+), 6 deletions(-)

diff --git a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc b/UefiCpuPkg/Library/MpInitLib/MpEqu.inc
index 72af196513..317e627b58 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc
+++ b/UefiCpuPkg/Library/MpInitLib/MpEqu.inc
@@ -67,6 +67,8 @@ endstruc
 
 ;
 ; Equivalent NASM structure of MP_CPU_EXCHANGE_INFO
+; Assembly routines should refrain from directly interacting with
+; the internal details of CPU_MP_DATA.
 ;
 struc MP_CPU_EXCHANGE_INFO
   .StackStart:                   CTYPE_UINTN 1
diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpInitLib/MpLib.h
index af296f6ac0..a96a6389c1 100644
--- a/UefiCpuPkg/Library/MpInitLib/MpLib.h
+++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h
@@ -203,6 +203,8 @@ typedef struct _CPU_MP_DATA CPU_MP_DATA;
 // MP CPU exchange information for AP reset code
 // This structure is required to be packed because fixed field offsets
 // into this structure are used in assembly code in this module
+// Assembly routines should refrain from directly interacting with
+// the internal details of CPU_MP_DATA.
 //
 typedef struct {
   UINTN              StackStart;
@@ -239,17 +241,16 @@ typedef struct {
 #pragma pack()
 
 //
-// CPU MP Data save in memory
+// CPU MP Data save in memory, and intended for use in C code.
+// There are some duplicated fields, such as XD status, between
+// CpuMpData and ExchangeInfo. These duplications in CpuMpData
+// are present to avoid to be direct accessed and comprehended
+// in assembly code.
 //
 struct _CPU_MP_DATA {
   UINT64                           CpuInfoInHob;
   UINT32                           CpuCount;
   UINT32                           BspNumber;
-  //
-  // The above fields data will be passed from PEI to DXE
-  // Please make sure the fields offset same in the different
-  // architecture.
-  //
   SPIN_LOCK                        MpLock;
   UINTN                            Buffer;
   UINTN                            CpuApStackSize;
-- 
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#111509): https://edk2.groups.io/g/devel/message/111509
Mute This Topic: https://groups.io/mt/102721681/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



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

* Re: [edk2-devel] [Patch V7 2/2] UefiCpuPkg/MpInitLib: Update the comments of _CPU_MP_DATA.
  2023-11-21  2:35 ` [edk2-devel] [Patch V7 2/2] UefiCpuPkg/MpInitLib: Update the comments of _CPU_MP_DATA Yuanhao Xie
@ 2023-11-22 16:41   ` Laszlo Ersek
  0 siblings, 0 replies; 5+ messages in thread
From: Laszlo Ersek @ 2023-11-22 16:41 UTC (permalink / raw)
  To: devel, yuanhao.xie; +Cc: Eric Dong, Ray Ni, Rahul Kumar, Gerd Hoffmann

On 11/21/23 03:35, Yuanhao Xie wrote:
> No functional changes in this patch.
> 
> Updates the comments of _CPU_MP_DATA to delcared that duplications in
> CpuMpData are present to avoid to be direct accessed and comprehended
>  in assembly code. CpuMpData: Intended for use in C code while
>  ExchangeInfo are used in assembly code in this module.

(1) The space characters at the front of these last two lines seem
superfluous.

> 
> This patch deletes the unnecessary comments in CpuMpData, since
> CpuMpData is no longer responsible for passing information from PEI to
> DXE.
> 
> Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
> Cc: Laszlo Ersek lersek@redhat.com
> Cc: Eric Dong <eric.dong@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Cc: Rahul Kumar <rahul1.kumar@intel.com>
> Cc: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  UefiCpuPkg/Library/MpInitLib/MpEqu.inc |  2 ++
>  UefiCpuPkg/Library/MpInitLib/MpLib.h   | 13 +++++++------
>  2 files changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc b/UefiCpuPkg/Library/MpInitLib/MpEqu.inc
> index 72af196513..317e627b58 100644
> --- a/UefiCpuPkg/Library/MpInitLib/MpEqu.inc
> +++ b/UefiCpuPkg/Library/MpInitLib/MpEqu.inc
> @@ -67,6 +67,8 @@ endstruc
>  
>  ;
>  ; Equivalent NASM structure of MP_CPU_EXCHANGE_INFO
> +; Assembly routines should refrain from directly interacting with
> +; the internal details of CPU_MP_DATA.
>  ;
>  struc MP_CPU_EXCHANGE_INFO
>    .StackStart:                   CTYPE_UINTN 1

OK!

> diff --git a/UefiCpuPkg/Library/MpInitLib/MpLib.h b/UefiCpuPkg/Library/MpInitLib/MpLib.h
> index af296f6ac0..a96a6389c1 100644
> --- a/UefiCpuPkg/Library/MpInitLib/MpLib.h
> +++ b/UefiCpuPkg/Library/MpInitLib/MpLib.h
> @@ -203,6 +203,8 @@ typedef struct _CPU_MP_DATA CPU_MP_DATA;
>  // MP CPU exchange information for AP reset code
>  // This structure is required to be packed because fixed field offsets
>  // into this structure are used in assembly code in this module
> +// Assembly routines should refrain from directly interacting with
> +// the internal details of CPU_MP_DATA.
>  //
>  typedef struct {
>    UINTN              StackStart;

OK!

> @@ -239,17 +241,16 @@ typedef struct {
>  #pragma pack()
>  
>  //
> -// CPU MP Data save in memory
> +// CPU MP Data save in memory, and intended for use in C code.
> +// There are some duplicated fields, such as XD status, between
> +// CpuMpData and ExchangeInfo. These duplications in CpuMpData
> +// are present to avoid to be direct accessed and comprehended
> +// in assembly code.
>  //

(2) So, my following comment applies to both the commit message and the
comment here.

I think we should not attempt to explain the *direction* of the
duplications. I think there have been examples for either direction --
at some point, some information was initially only used by the assembly
code, and then needed duplication for the C code's sake; at another
time, duplication in the the opposite direction was necessary (because
stuff used by the C code was needed by the assembly code too).

In particular, the statement "duplicate a field from ExchangeInfo to
CpuMpData so that assembly code need not understand it" seems wrong. For
easing the job of the assembly code, fields would be duplicated in the
*opposite* direction (from CpuMpData to ExchangeInfo).

Note that in patch#1 in this series, we indeed duplicate a field from
ExchangeInfo to CpuMpData -- but there the reason is different: we want
to make the C code's job easier! The "run loop" wait mode does not
involve *rebooting* APs, so there is no assembly code for AP startup,
and no ExchangeInfo either. Therefore, we're making the C code's job
easier, so that it can function with just CpuMpData.

Thus, I would rather avoid any language on the *direction* of
duplication. I'd just say:

  MP_CPU_EXCHANGE_INFO is to be consumed by assembly code only, and
  CPU_MP_DATA is to be consumed by C code only. If both kinds of code
  need to consume a piece of information, then that information must be
  duplicated between both structures.

... I'm sorry that I'm splitting hairs for so long on this issue, but I
find this principle behind the duplications *really* non-obvious. So I
would like us to get the documentation right.

(Ray's original explanation was here:
<https://edk2.groups.io/g/devel/message/111336>.)

>  struct _CPU_MP_DATA {
>    UINT64                           CpuInfoInHob;
>    UINT32                           CpuCount;
>    UINT32                           BspNumber;
> -  //
> -  // The above fields data will be passed from PEI to DXE
> -  // Please make sure the fields offset same in the different
> -  // architecture.
> -  //
>    SPIN_LOCK                        MpLock;
>    UINTN                            Buffer;
>    UINTN                            CpuApStackSize;

This is OK.

Thanks!
Laszlo



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#111615): https://edk2.groups.io/g/devel/message/111615
Mute This Topic: https://groups.io/mt/102721681/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/1913456212/xyzzy [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



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

end of thread, other threads:[~2023-11-22 16:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-21  2:35 [edk2-devel] [Patch V7 0/2]UefiCpuPkg/MpInitLib: Enable execute disable bit Yuanhao Xie
2023-11-21  2:35 ` [edk2-devel] [Patch V7 1/2] UefiCpuPkg/MpInitLib: " Yuanhao Xie
2023-11-21  2:35 ` [edk2-devel] [Patch V7 2/2] UefiCpuPkg/MpInitLib: Update the comments of _CPU_MP_DATA Yuanhao Xie
2023-11-22 16:41   ` Laszlo Ersek
  -- strict thread matches above, loose matches on Subject: below --
2023-11-21  2:34 [edk2-devel] [Patch V7 0/2]UefiCpuPkg/MpInitLib: Enable execute disable bit Yuanhao Xie
2023-11-21  2:34 ` [edk2-devel] [Patch V7 2/2] UefiCpuPkg/MpInitLib: Update the comments of _CPU_MP_DATA Yuanhao Xie

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