1;------------------------------------------------------------------------------
2;
3; Copyright (c) 2013, Intel Corporation. All rights reserved.<BR>
4; This program and the accompanying materials
5; are licensed and made available under the terms and conditions of the BSD License
6; which accompanies this distribution.  The full text of the license may be found at
7; http://opensource.org/licenses/bsd-license.php
8;
9; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11;
12; Module Name:
13;
14;   Stack.asm
15;
16; Abstract:
17;
18;   Switch the stack from temporary memory to permenent memory.
19;
20;------------------------------------------------------------------------------
21
22    .code
23
24;------------------------------------------------------------------------------
25; VOID
26; EFIAPI
27; SecSwitchStack (
28;   UINT32   TemporaryMemoryBase,
29;   UINT32   PermenentMemoryBase
30;   );
31;------------------------------------------------------------------------------
32SecSwitchStack   PROC
33    mov [rsp + 08h], rcx
34    mov [rsp + 10h], rdx
35
36    ;
37    ; Save three register: eax, ebx, ecx
38    ;
39    push  rax
40    push  rbx
41    push  rcx
42    push  rdx
43
44    ;
45    ; !!CAUTION!! this function address's is pushed into stack after
46    ; migration of whole temporary memory, so need save it to permenent
47    ; memory at first!
48    ;
49
50    mov   rbx, [rsp + 28h]          ; Save the first parameter
51    mov   rcx, [rsp + 30h]          ; Save the second parameter
52
53    ;
54    ; Save this function's return address into permenent memory at first.
55    ; Then, Fixup the esp point to permenent memory
56    ;
57    mov   rax, rsp
58    sub   rax, rbx
59    add   rax, rcx
60    mov   rdx, qword ptr [rsp]         ; copy pushed register's value to permenent memory
61    mov   qword ptr [rax], rdx
62    mov   rdx, qword ptr [rsp + 8]
63    mov   qword ptr [rax + 8], rdx
64    mov   rdx, qword ptr [rsp + 10h]
65    mov   qword ptr [rax + 10h], rdx
66    mov   rdx, qword ptr [rsp + 18h]
67    mov   qword ptr [rax + 18h], rdx
68    mov   rdx, qword ptr [rsp + 20h]    ; Update this function's return address into permenent memory
69    mov   qword ptr [rax + 20h], rdx
70    mov   rsp, rax                     ; From now, esp is pointed to permenent memory
71
72    ;
73    ; Fixup the ebp point to permenent memory
74    ;
75    mov   rax, rbp
76    sub   rax, rbx
77    add   rax, rcx
78    mov   rbp, rax                ; From now, ebp is pointed to permenent memory
79
80    pop   rdx
81    pop   rcx
82    pop   rbx
83    pop   rax
84    ret
85SecSwitchStack   ENDP
86
87;------------------------------------------------------------------------------
88; VOID
89; EFIAPI
90; PeiSwitchStacks (
91;   IN      SWITCH_STACK_ENTRY_POINT  EntryPoint,
92;   IN      VOID                      *Context1,  OPTIONAL
93;   IN      VOID                      *Context2,  OPTIONAL
94;   IN      VOID                      *Context3,  OPTIONAL
95;   IN      VOID                      *NewStack
96;   )
97;------------------------------------------------------------------------------
98PeiSwitchStacks   PROC
99    mov  rax, rcx
100    mov  rcx, rdx
101    mov  rdx, r8
102    mov  r8, r9
103    mov  rsp, [rsp + 28h]
104    sub  rsp, 20h
105    call rax
106    jmp $
107    ret
108PeiSwitchStacks   ENDP
109
110    END
111