]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Sec/Ia32/SwitchStack.c
Update the copyright notice format
[mirror_edk2.git] / OvmfPkg / Sec / Ia32 / SwitchStack.c
1 /** @file
2 Switch Stack functions.
3
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
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 Transfers control to a function starting with a new stack.
27
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.
32
33 If EntryPoint is NULL, then ASSERT().
34 If NewStack is NULL, then ASSERT().
35
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
38 function.
39 @param Context2 A pointer to the context to pass into the EntryPoint
40 function.
41 @param NewStack A pointer to the new stack to use for the EntryPoint
42 function.
43 @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's
44 Reserved on other architectures.
45
46 **/
47 VOID
48 EFIAPI
49 PeiSwitchStacks (
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,
55 IN VOID *NewStack
56 )
57 {
58 BASE_LIBRARY_JUMP_BUFFER JumpBuffer;
59
60 ASSERT (EntryPoint != NULL);
61 ASSERT (NewStack != NULL);
62
63 //
64 // Stack should be aligned with CPU_STACK_ALIGNMENT
65 //
66 ASSERT (((UINTN)NewStack & (CPU_STACK_ALIGNMENT - 1)) == 0);
67
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;
74
75 LongJump (&JumpBuffer, (UINTN)-1);
76
77 //
78 // InternalSwitchStack () will never return
79 //
80 ASSERT (FALSE);
81 }
82
83 /**
84 Transfers control to a function starting with a new stack.
85
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.
90
91 If EntryPoint is NULL, then ASSERT().
92 If NewStack is NULL, then ASSERT().
93
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
96 function.
97 @param Context2 A pointer to the context to pass into the EntryPoint
98 function.
99 @param NewStack A pointer to the new stack to use for the EntryPoint
100 function.
101 @param NewBsp A pointer to the new BSP for the EntryPoint on IPF. It's
102 Reserved on other architectures.
103
104 **/
105 VOID
106 EFIAPI
107 SecSwitchStack (
108 IN UINTN TemporaryMemoryBase,
109 IN UINTN PermanentMemoryBase,
110 IN UINTN CopySize
111 )
112 {
113 BASE_LIBRARY_JUMP_BUFFER JumpBuffer;
114 UINTN SetJumpFlag;
115
116 ASSERT ((VOID*)TemporaryMemoryBase != NULL);
117 ASSERT ((VOID*)PermanentMemoryBase != NULL);
118
119 SetJumpFlag = SetJump (&JumpBuffer);
120 //
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().
123 //
124 if (SetJumpFlag == 0) {
125 DEBUG ((EFI_D_ERROR, "SecSwitchStack+%d: Esp: 0x%xL\n", __LINE__, JumpBuffer.Esp));
126 JumpBuffer.Esp =
127 (INTN)JumpBuffer.Esp -
128 (INTN)TemporaryMemoryBase +
129 (INTN)PermanentMemoryBase;
130 MemoryFence ();
131 CopyMem((VOID*)PermanentMemoryBase, (VOID*)TemporaryMemoryBase, CopySize);
132 LongJump (&JumpBuffer, (UINTN)-1);
133 }
134
135 }
136