+;------------------------------------------------------------------------------\r
+;\r
+; Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>\r
+; This program and the accompanying materials\r
+; are licensed and made available under the terms and conditions of the BSD License\r
+; which accompanies this distribution. The full text of the license may be found at\r
+; http://opensource.org/licenses/bsd-license.php\r
+;\r
+; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+;\r
+; Module Name:\r
+;\r
+; Stack.asm\r
+;\r
+; Abstract:\r
+;\r
+; Switch the stack from temporary memory to permenent memory.\r
+;\r
+;------------------------------------------------------------------------------\r
+\r
+ .code\r
+ \r
+;------------------------------------------------------------------------------\r
+; VOID\r
+; EFIAPI\r
+; SecSwitchStack (\r
+; UINT32 TemporaryMemoryBase,\r
+; UINT32 PermenentMemoryBase\r
+; );\r
+;------------------------------------------------------------------------------ \r
+SecSwitchStack PROC\r
+ mov [rsp + 08h], rcx\r
+ mov [rsp + 10h], rdx\r
+\r
+ ;\r
+ ; Save three register: eax, ebx, ecx\r
+ ;\r
+ push rax\r
+ push rbx\r
+ push rcx\r
+ push rdx\r
+ \r
+ ;\r
+ ; !!CAUTION!! this function address's is pushed into stack after\r
+ ; migration of whole temporary memory, so need save it to permenent\r
+ ; memory at first!\r
+ ;\r
+ \r
+ mov rbx, [rsp + 28h] ; Save the first parameter\r
+ mov rcx, [rsp + 30h] ; Save the second parameter\r
+ \r
+ ;\r
+ ; Save this function's return address into permenent memory at first.\r
+ ; Then, Fixup the esp point to permenent memory\r
+ ;\r
+ mov rax, rsp\r
+ sub rax, rbx\r
+ add rax, rcx\r
+ mov rdx, qword ptr [rsp] ; copy pushed register's value to permenent memory\r
+ mov qword ptr [rax], rdx \r
+ mov rdx, qword ptr [rsp + 8]\r
+ mov qword ptr [rax + 8], rdx \r
+ mov rdx, qword ptr [rsp + 10h]\r
+ mov qword ptr [rax + 10h], rdx \r
+ mov rdx, qword ptr [rsp + 18h]\r
+ mov qword ptr [rax + 18h], rdx \r
+ mov rdx, qword ptr [rsp + 20h] ; Update this function's return address into permenent memory\r
+ mov qword ptr [rax + 20h], rdx \r
+ mov rsp, rax ; From now, esp is pointed to permenent memory\r
+ \r
+ ;\r
+ ; Fixup the ebp point to permenent memory\r
+ ;\r
+ mov rax, rbp\r
+ sub rax, rbx\r
+ add rax, rcx\r
+ mov rbp, rax ; From now, ebp is pointed to permenent memory\r
+ \r
+ pop rdx\r
+ pop rcx\r
+ pop rbx\r
+ pop rax\r
+ ret\r
+SecSwitchStack ENDP\r
+\r
+;------------------------------------------------------------------------------\r
+; VOID\r
+; EFIAPI\r
+; PeiSwitchStacks (\r
+; IN SWITCH_STACK_ENTRY_POINT EntryPoint,\r
+; IN VOID *Context1, OPTIONAL\r
+; IN VOID *Context2, OPTIONAL\r
+; IN VOID *Context3, OPTIONAL\r
+; IN VOID *NewStack\r
+; )\r
+;------------------------------------------------------------------------------\r
+PeiSwitchStacks PROC\r
+ mov rax, rcx\r
+ mov rcx, rdx\r
+ mov rdx, r8\r
+ mov r8, r9\r
+ mov rsp, [rsp + 28h]\r
+ sub rsp, 20h\r
+ call rax\r
+ jmp $\r
+ ret\r
+PeiSwitchStacks ENDP\r
+\r
+ END\r