]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Sec/X64/SwitchStack.c
3ff98ba5c42a2ed2670637a50b28ab8d62767585
[mirror_edk2.git] / OvmfPkg / Sec / X64 / SwitchStack.c
1 /** @file
2 Switch Stack functions.
3
4 Copyright (c) 2006 - 2007, Intel Corporation<BR>
5 All rights reserved. This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 //
16 // Include common header file for this module.
17 //
18
19
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/PeiServicesLib.h>
24
25 //
26 // Type define for PEI Core Entry Point function
27 //
28 typedef
29 VOID
30 (EFIAPI *PEI_CORE_ENTRY_POINT)(
31 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,
32 IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList,
33 IN VOID *Data
34 )
35 ;
36
37 /**
38 Transfers control to a function starting with a new stack.
39
40 Transfers control to the function specified by EntryPoint using the new stack
41 specified by NewStack and passing in the parameters specified by Context1 and
42 Context2. Context1 and Context2 are optional and may be NULL. The function
43 EntryPoint must never return.
44
45 If EntryPoint is NULL, then ASSERT().
46 If NewStack is NULL, then ASSERT().
47
48 @param EntryPoint A pointer to function to call with the new stack.
49 @param Context1 A pointer to the context to pass into the EntryPoint
50 function.
51 @param Context2 A pointer to the context to pass into the EntryPoint
52 function.
53 @param NewStack A pointer to the new stack to use for the EntryPoint
54 function.
55 @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's
56 Reserved on other architectures.
57
58 **/
59 VOID
60 EFIAPI
61 PeiSwitchStacks (
62 IN SWITCH_STACK_ENTRY_POINT EntryPoint,
63 IN VOID *Context1, OPTIONAL
64 IN VOID *Context2, OPTIONAL
65 IN VOID *Context3, OPTIONAL
66 IN VOID *OldTopOfStack,
67 IN VOID *NewStack
68 )
69 {
70 BASE_LIBRARY_JUMP_BUFFER JumpBuffer;
71 UINTN SizeOfStackUsed;
72 UINTN SetJumpFlag;
73
74 ASSERT (EntryPoint != NULL);
75 ASSERT (NewStack != NULL);
76
77 SetJumpFlag = SetJump (&JumpBuffer);
78 //
79 // The initial call to SetJump() must always return 0.
80 // Subsequent calls to LongJump() may cause a non-zero value to be returned by SetJump().
81 //
82 if (SetJumpFlag == 0) {
83 //
84 // Stack should be aligned with CPU_STACK_ALIGNMENT
85 //
86 ASSERT (((UINTN)NewStack & (CPU_STACK_ALIGNMENT - 1)) == 0);
87
88 //JumpBuffer.Rip = (UINTN)EntryPoint;
89 SizeOfStackUsed = (UINTN)OldTopOfStack - JumpBuffer.Rsp;
90 JumpBuffer.Rsp = (UINTN)NewStack - SizeOfStackUsed;
91 MemoryFence ();
92 CopyMem (
93 (VOID*) ((UINTN)NewStack - SizeOfStackUsed),
94 (VOID*) ((UINTN)OldTopOfStack - SizeOfStackUsed),
95 SizeOfStackUsed
96 );
97 LongJump (&JumpBuffer, (UINTN)-1);
98 } else {
99 (*(PEI_CORE_ENTRY_POINT)(EntryPoint)) (
100 (EFI_SEC_PEI_HAND_OFF *) Context1,
101 (EFI_PEI_PPI_DESCRIPTOR *) Context2,
102 Context3
103 );
104 }
105
106 //
107 // InternalSwitchStack () will never return
108 //
109 ASSERT (FALSE);
110 }
111
112 /**
113 Transfers control to a function starting with a new stack.
114
115 Transfers control to the function specified by EntryPoint using the new stack
116 specified by NewStack and passing in the parameters specified by Context1 and
117 Context2. Context1 and Context2 are optional and may be NULL. The function
118 EntryPoint must never return.
119
120 If EntryPoint is NULL, then ASSERT().
121 If NewStack is NULL, then ASSERT().
122
123 @param EntryPoint A pointer to function to call with the new stack.
124 @param Context1 A pointer to the context to pass into the EntryPoint
125 function.
126 @param Context2 A pointer to the context to pass into the EntryPoint
127 function.
128 @param NewStack A pointer to the new stack to use for the EntryPoint
129 function.
130 @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's
131 Reserved on other architectures.
132
133 **/
134 VOID
135 EFIAPI
136 SecSwitchStack (
137 IN UINTN TemporaryMemoryBase,
138 IN UINTN PermanentMemoryBase,
139 IN UINTN CopySize
140 )
141 {
142 BASE_LIBRARY_JUMP_BUFFER JumpBuffer;
143 UINTN SetJumpFlag;
144
145 ASSERT ((VOID*)TemporaryMemoryBase != NULL);
146 ASSERT ((VOID*)PermanentMemoryBase != NULL);
147
148 SetJumpFlag = SetJump (&JumpBuffer);
149 //
150 // The initial call to SetJump() must always return 0.
151 // Subsequent calls to LongJump() may cause a non-zero value to be returned by SetJump().
152 //
153 if (SetJumpFlag == 0) {
154 DEBUG ((EFI_D_ERROR, "SecSwitchStack+%d: Rsp: 0x%xL\n", __LINE__, JumpBuffer.Rsp));
155 JumpBuffer.Rsp =
156 (INTN)JumpBuffer.Rsp -
157 (INTN)TemporaryMemoryBase +
158 (INTN)PermanentMemoryBase;
159 MemoryFence ();
160 CopyMem((VOID*)PermanentMemoryBase, (VOID*)TemporaryMemoryBase, CopySize);
161 LongJump (&JumpBuffer, (UINTN)-1);
162 }
163
164 }
165