2 Switch Stack functions.
4 Copyright (c) 2006 - 2007, Intel Corporation. All rights reserved.<BR>
5 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
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.
16 // Include common header file for this module.
20 #include <Library/BaseLib.h>
21 #include <Library/BaseMemoryLib.h>
22 #include <Library/DebugLib.h>
23 #include <Library/PeiServicesLib.h>
26 Transfers control to a function starting with a new stack.
28 Transfers control to the function specified by EntryPoint using the new stack
29 specified by NewStack and passing in the parameters specified by Context1 and
30 Context2. Context1 and Context2 are optional and may be NULL. The function
31 EntryPoint must never return.
33 If EntryPoint is NULL, then ASSERT().
34 If NewStack is NULL, then ASSERT().
36 @param EntryPoint A pointer to function to call with the new stack.
37 @param Context1 A pointer to the context to pass into the EntryPoint
39 @param Context2 A pointer to the context to pass into the EntryPoint
41 @param NewStack A pointer to the new stack to use for the EntryPoint
43 @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's
44 Reserved on other architectures.
50 IN SWITCH_STACK_ENTRY_POINT EntryPoint
,
51 IN VOID
*Context1
, OPTIONAL
52 IN VOID
*Context2
, OPTIONAL
53 IN VOID
*Context3
, OPTIONAL
54 IN VOID
*OldTopOfStack
,
58 BASE_LIBRARY_JUMP_BUFFER JumpBuffer
;
60 ASSERT (EntryPoint
!= NULL
);
61 ASSERT (NewStack
!= NULL
);
64 // Stack should be aligned with CPU_STACK_ALIGNMENT
66 ASSERT (((UINTN
)NewStack
& (CPU_STACK_ALIGNMENT
- 1)) == 0);
68 JumpBuffer
.Eip
= (UINTN
)EntryPoint
;
69 JumpBuffer
.Esp
= (UINTN
)NewStack
- sizeof (VOID
*);
70 JumpBuffer
.Esp
-= sizeof (Context1
) + sizeof (Context2
) + sizeof(Context3
);
71 ((VOID
**)JumpBuffer
.Esp
)[1] = Context1
;
72 ((VOID
**)JumpBuffer
.Esp
)[2] = Context2
;
73 ((VOID
**)JumpBuffer
.Esp
)[3] = Context3
;
75 LongJump (&JumpBuffer
, (UINTN
)-1);
78 // InternalSwitchStack () will never return
84 Transfers control to a function starting with a new stack.
86 Transfers control to the function specified by EntryPoint using the new stack
87 specified by NewStack and passing in the parameters specified by Context1 and
88 Context2. Context1 and Context2 are optional and may be NULL. The function
89 EntryPoint must never return.
91 If EntryPoint is NULL, then ASSERT().
92 If NewStack is NULL, then ASSERT().
94 @param EntryPoint A pointer to function to call with the new stack.
95 @param Context1 A pointer to the context to pass into the EntryPoint
97 @param Context2 A pointer to the context to pass into the EntryPoint
99 @param NewStack A pointer to the new stack to use for the EntryPoint
101 @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's
102 Reserved on other architectures.
108 IN UINTN TemporaryMemoryBase
,
109 IN UINTN PermanentMemoryBase
,
113 BASE_LIBRARY_JUMP_BUFFER JumpBuffer
;
116 ASSERT ((VOID
*)TemporaryMemoryBase
!= NULL
);
117 ASSERT ((VOID
*)PermanentMemoryBase
!= NULL
);
119 SetJumpFlag
= SetJump (&JumpBuffer
);
121 // The initial call to SetJump() must always return 0.
122 // Subsequent calls to LongJump() may cause a non-zero value to be returned by SetJump().
124 if (SetJumpFlag
== 0) {
125 DEBUG ((EFI_D_ERROR
, "SecSwitchStack+%d: Esp: 0x%xL\n", __LINE__
, JumpBuffer
.Esp
));
127 (INTN
)JumpBuffer
.Esp
-
128 (INTN
)TemporaryMemoryBase
+
129 (INTN
)PermanentMemoryBase
;
131 CopyMem((VOID
*)PermanentMemoryBase
, (VOID
*)TemporaryMemoryBase
, CopySize
);
132 LongJump (&JumpBuffer
, (UINTN
)-1);