public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel] [PATCH] MdePkg/BaseLib: Fix InternalSwitchStack bug for RISCV64.
@ 2023-12-28  4:16 yorange
  0 siblings, 0 replies; 3+ messages in thread
From: yorange @ 2023-12-28  4:16 UTC (permalink / raw)
  To: devel; +Cc: yorange1

From: yorange1 <yangcheng.work@foxmail.com>

REF:https://bugzilla.tianocore.org/show_bug.cgi?id=4611

Fixed the InternalSwitchStack error of RISCV64. LongJump is no
longer used for stack switch. Refer to Arm and AArch64 and
use InternalSwitchStackAsm instead.

Signed-off-by: yorange1 <yangcheng.work@foxmail.com>
---
 MdePkg/Library/BaseLib/BaseLib.inf            |  1 +
 .../BaseLib/RiscV64/InternalSwitchStack.c     | 31 ++++++++++++---
 .../BaseLib/RiscV64/RiscVSwitchStack.S        | 38 +++++++++++++++++++
 3 files changed, 64 insertions(+), 6 deletions(-)
 create mode 100644 MdePkg/Library/BaseLib/RiscV64/RiscVSwitchStack.S

diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/BaseLib.inf
index 5338938944..10382111ef 100644
--- a/MdePkg/Library/BaseLib/BaseLib.inf
+++ b/MdePkg/Library/BaseLib/BaseLib.inf
@@ -405,6 +405,7 @@
   RiscV64/ReadTimer.S               | GCC
   RiscV64/RiscVMmu.S                | GCC
   RiscV64/SpeculationBarrier.S      | GCC
+  RiscV64/RiscVSwitchStack.S        | GCC
 
 [Sources.LOONGARCH64]
   Math64.c
diff --git a/MdePkg/Library/BaseLib/RiscV64/InternalSwitchStack.c b/MdePkg/Library/BaseLib/RiscV64/InternalSwitchStack.c
index b78424c163..c9d92920df 100644
--- a/MdePkg/Library/BaseLib/RiscV64/InternalSwitchStack.c
+++ b/MdePkg/Library/BaseLib/RiscV64/InternalSwitchStack.c
@@ -8,6 +8,30 @@
 
 #include "BaseLibInternals.h"
 
+/**
+  Transfers control to a function starting with a new stack.
+
+  This internal worker function transfers control to the function
+  specified by EntryPoint using the new stack specified by NewStack,
+  and passes in the parameters specified by Context1 and Context2.
+  Context1 and Context2 are optional and may be NULL.
+  The function EntryPoint must never return.
+
+  @param EntryPoint   The pointer to the function to enter.
+  @param Context1     The first parameter to pass in.
+  @param Context2     The second Parameter to pass in
+  @param NewStack     The new Location of the stack
+
+**/
+VOID
+EFIAPI
+InternalSwitchStackAsm (
+  IN      SWITCH_STACK_ENTRY_POINT  EntryPoint,
+  IN      VOID                      *Context1    OPTIONAL,
+  IN      VOID                      *Context2    OPTIONAL,
+  IN      VOID                      *NewStack
+  );
+
 /**
   Transfers control to a function starting with a new stack.
 
@@ -42,12 +66,7 @@ InternalSwitchStack (
   IN      VA_LIST                   Marker
   )
 {
-  BASE_LIBRARY_JUMP_BUFFER  JumpBuffer;
+  InternalSwitchStackAsm(EntryPoint, Context1, Context2, (void *)(UINTN)NewStack - sizeof (VOID *));
 
-  JumpBuffer.RA = (UINTN)EntryPoint;
-  JumpBuffer.SP = (UINTN)NewStack - sizeof (VOID *);
-  JumpBuffer.S0 = (UINT64)(UINTN)Context1;
-  JumpBuffer.S1 = (UINT64)(UINTN)Context2;
-  LongJump (&JumpBuffer, (UINTN)-1);
   ASSERT (FALSE);
 }
diff --git a/MdePkg/Library/BaseLib/RiscV64/RiscVSwitchStack.S b/MdePkg/Library/BaseLib/RiscV64/RiscVSwitchStack.S
new file mode 100644
index 0000000000..7b316dd5e5
--- /dev/null
+++ b/MdePkg/Library/BaseLib/RiscV64/RiscVSwitchStack.S
@@ -0,0 +1,38 @@
+//------------------------------------------------------------------------------
+//
+// Switch Stack for RISC-V
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+//------------------------------------------------------------------------------
+
+/**
+//
+//  This allows the caller to switch the stack and goes to the new entry point
+//
+// @param      EntryPoint   The pointer to the location to enter
+// @param      Context      Parameter to pass in
+// @param      Context2     Parameter2 to pass in
+// @param      NewStack     New Location of the stack
+//
+// @return     Nothing. Goes to the Entry Point passing in the new parameters
+//
+VOID
+EFIAPI
+InternalSwitchStackAsm (
+  SWITCH_STACK_ENTRY_POINT EntryPoint,
+  VOID  *Context,
+  VOID  *Context2,
+  VOID  *NewStack
+  );
+**/
+
+.align 3
+    .globl  InternalSwitchStackAsm
+InternalSwitchStackAsm:
+  mv  ra, a0
+  mv  a0, a1
+  mv  a1, a2
+  mv  sp, a3
+
+  ret
-- 
2.34.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113050): https://edk2.groups.io/g/devel/message/113050
Mute This Topic: https://groups.io/mt/103494172/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] 3+ messages in thread

* [edk2-devel] [PATCH] MdePkg/BaseLib: Fix InternalSwitchStack bug for RISCV64.
@ 2024-01-03  3:25 yorange
  2024-01-11 16:50 ` yorange
  0 siblings, 1 reply; 3+ messages in thread
From: yorange @ 2024-01-03  3:25 UTC (permalink / raw)
  To: devel; +Cc: gaoliming, yorange

From: yorange <yangcheng.work@foxmail.com>

REF:https://bugzilla.tianocore.org/show_bug.cgi?id=4611

Fixed the InternalSwitchStack error of RISCV64. LongJump is no
longer used for stack switch. Refer to Arm and AArch64 and
use InternalSwitchStackAsm instead.

Signed-off-by: yorange <yangcheng.work@foxmail.com>
---
 MdePkg/Library/BaseLib/BaseLib.inf            |  1 +
 .../BaseLib/RiscV64/InternalSwitchStack.c     | 31 ++++++++++++---
 .../BaseLib/RiscV64/RiscVSwitchStack.S        | 38 +++++++++++++++++++
 3 files changed, 64 insertions(+), 6 deletions(-)
 create mode 100644 MdePkg/Library/BaseLib/RiscV64/RiscVSwitchStack.S

diff --git a/MdePkg/Library/BaseLib/BaseLib.inf b/MdePkg/Library/BaseLib/BaseLib.inf
index 5338938944..10382111ef 100644
--- a/MdePkg/Library/BaseLib/BaseLib.inf
+++ b/MdePkg/Library/BaseLib/BaseLib.inf
@@ -405,6 +405,7 @@
   RiscV64/ReadTimer.S               | GCC
   RiscV64/RiscVMmu.S                | GCC
   RiscV64/SpeculationBarrier.S      | GCC
+  RiscV64/RiscVSwitchStack.S        | GCC
 
 [Sources.LOONGARCH64]
   Math64.c
diff --git a/MdePkg/Library/BaseLib/RiscV64/InternalSwitchStack.c b/MdePkg/Library/BaseLib/RiscV64/InternalSwitchStack.c
index b78424c163..c9d92920df 100644
--- a/MdePkg/Library/BaseLib/RiscV64/InternalSwitchStack.c
+++ b/MdePkg/Library/BaseLib/RiscV64/InternalSwitchStack.c
@@ -8,6 +8,30 @@
 
 #include "BaseLibInternals.h"
 
+/**
+  Transfers control to a function starting with a new stack.
+
+  This internal worker function transfers control to the function
+  specified by EntryPoint using the new stack specified by NewStack,
+  and passes in the parameters specified by Context1 and Context2.
+  Context1 and Context2 are optional and may be NULL.
+  The function EntryPoint must never return.
+
+  @param EntryPoint   The pointer to the function to enter.
+  @param Context1     The first parameter to pass in.
+  @param Context2     The second Parameter to pass in
+  @param NewStack     The new Location of the stack
+
+**/
+VOID
+EFIAPI
+InternalSwitchStackAsm (
+  IN      SWITCH_STACK_ENTRY_POINT  EntryPoint,
+  IN      VOID                      *Context1    OPTIONAL,
+  IN      VOID                      *Context2    OPTIONAL,
+  IN      VOID                      *NewStack
+  );
+
 /**
   Transfers control to a function starting with a new stack.
 
@@ -42,12 +66,7 @@ InternalSwitchStack (
   IN      VA_LIST                   Marker
   )
 {
-  BASE_LIBRARY_JUMP_BUFFER  JumpBuffer;
+  InternalSwitchStackAsm(EntryPoint, Context1, Context2, (void *)(UINTN)NewStack - sizeof (VOID *));
 
-  JumpBuffer.RA = (UINTN)EntryPoint;
-  JumpBuffer.SP = (UINTN)NewStack - sizeof (VOID *);
-  JumpBuffer.S0 = (UINT64)(UINTN)Context1;
-  JumpBuffer.S1 = (UINT64)(UINTN)Context2;
-  LongJump (&JumpBuffer, (UINTN)-1);
   ASSERT (FALSE);
 }
diff --git a/MdePkg/Library/BaseLib/RiscV64/RiscVSwitchStack.S b/MdePkg/Library/BaseLib/RiscV64/RiscVSwitchStack.S
new file mode 100644
index 0000000000..7b316dd5e5
--- /dev/null
+++ b/MdePkg/Library/BaseLib/RiscV64/RiscVSwitchStack.S
@@ -0,0 +1,38 @@
+//------------------------------------------------------------------------------
+//
+// Switch Stack for RISC-V
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+//------------------------------------------------------------------------------
+
+/**
+//
+//  This allows the caller to switch the stack and goes to the new entry point
+//
+// @param      EntryPoint   The pointer to the location to enter
+// @param      Context      Parameter to pass in
+// @param      Context2     Parameter2 to pass in
+// @param      NewStack     New Location of the stack
+//
+// @return     Nothing. Goes to the Entry Point passing in the new parameters
+//
+VOID
+EFIAPI
+InternalSwitchStackAsm (
+  SWITCH_STACK_ENTRY_POINT EntryPoint,
+  VOID  *Context,
+  VOID  *Context2,
+  VOID  *NewStack
+  );
+**/
+
+.align 3
+    .globl  InternalSwitchStackAsm
+InternalSwitchStackAsm:
+  mv  ra, a0
+  mv  a0, a1
+  mv  a1, a2
+  mv  sp, a3
+
+  ret
-- 
2.34.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113057): https://edk2.groups.io/g/devel/message/113057
Mute This Topic: https://groups.io/mt/103494172/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] 3+ messages in thread

* Re: [edk2-devel] [PATCH] MdePkg/BaseLib: Fix InternalSwitchStack bug for RISCV64.
  2024-01-03  3:25 yorange
@ 2024-01-11 16:50 ` yorange
  0 siblings, 0 replies; 3+ messages in thread
From: yorange @ 2024-01-11 16:50 UTC (permalink / raw)
  To: yorange, devel

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

The problem has been fixed in this patch ( Re: [PATCH v2] MdePkg/BaseLib:Fix boot DxeCore hang on riscv platform (groups.io) ( https://edk2.groups.io/g/devel/message/113624 ) ), no reply is needed here, thank you all.


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



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

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

end of thread, other threads:[~2024-01-11 16:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-28  4:16 [edk2-devel] [PATCH] MdePkg/BaseLib: Fix InternalSwitchStack bug for RISCV64 yorange
  -- strict thread matches above, loose matches on Subject: below --
2024-01-03  3:25 yorange
2024-01-11 16:50 ` yorange

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