--- /dev/null
+;------------------------------------------------------------------------------\r
+;\r
+; Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
+; SPDX-License-Identifier: BSD-2-Clause-Patent\r
+;\r
+; Abstract:\r
+;\r
+; Switch the stack from temporary memory to permanent memory.\r
+;\r
+;------------------------------------------------------------------------------\r
+\r
+ SECTION .text\r
+\r
+%include "PushPopRegsNasm.inc"\r
+\r
+extern ASM_PFX(SwapStack)\r
+\r
+;------------------------------------------------------------------------------\r
+; UINT32\r
+; EFIAPI\r
+; Pei2LoaderSwitchStack (\r
+; VOID\r
+; )\r
+;------------------------------------------------------------------------------\r
+global ASM_PFX(Pei2LoaderSwitchStack)\r
+ASM_PFX(Pei2LoaderSwitchStack):\r
+ xor rax, rax\r
+ jmp ASM_PFX(FspSwitchStack)\r
+\r
+;------------------------------------------------------------------------------\r
+; UINT32\r
+; EFIAPI\r
+; Loader2PeiSwitchStack (\r
+; VOID\r
+; )\r
+;------------------------------------------------------------------------------\r
+global ASM_PFX(Loader2PeiSwitchStack)\r
+ASM_PFX(Loader2PeiSwitchStack):\r
+ jmp ASM_PFX(FspSwitchStack)\r
+\r
+;------------------------------------------------------------------------------\r
+; UINT32\r
+; EFIAPI\r
+; FspSwitchStack (\r
+; VOID\r
+; )\r
+;------------------------------------------------------------------------------\r
+global ASM_PFX(FspSwitchStack)\r
+ASM_PFX(FspSwitchStack):\r
+ ; Save current contexts\r
+ push rdx ; ApiParam2\r
+ push rcx ; ApiParam1\r
+ push rax ; FspInfoHeader\r
+ pushfq\r
+ cli\r
+ PUSHA_64\r
+ sub rsp, 16\r
+ sidt [rsp]\r
+\r
+ ; Load new stack\r
+ mov rcx, rsp\r
+ call ASM_PFX(SwapStack)\r
+ mov rsp, rax\r
+\r
+ ; Restore previous contexts\r
+ lidt [rsp]\r
+ add rsp, 16\r
+ POPA_64\r
+ popfq\r
+ add rsp, 24 ; FspInfoHeader + ApiParam[2]\r
+ ret\r
+\r