4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials are licensed and made
6 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.
17 #include <Ppi/TemporaryRamSupport.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/DebugLib.h>
21 #include <Library/PcdLib.h>
22 #include <Library/DebugAgentLib.h>
25 Switch the stack in the temporary memory to the one in the permanent memory.
27 This function must be invoked after the memory migration immediately. The relative
28 position of the stack in the temporary and permanent memory is same.
30 @param TemporaryMemoryBase Base address of the temporary memory.
31 @param PermenentMemoryBase Base address of the permanent memory.
36 UINT32 TemporaryMemoryBase
,
37 UINT32 PermenentMemoryBase
41 This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
44 @param PeiServices Pointer to the PEI Services Table.
45 @param TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
46 Temporary RAM contents.
47 @param PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
48 Temporary RAM contents.
49 @param CopySize Amount of memory to migrate from temporary to permanent memory.
51 @retval EFI_SUCCESS The data was successfully returned.
52 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
53 TemporaryMemoryBase > PermanentMemoryBase.
58 SecTemporaryRamSupport (
59 IN CONST EFI_PEI_SERVICES
**PeiServices
,
60 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase
,
61 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase
,
65 IA32_DESCRIPTOR IdtDescriptor
;
70 DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext
;
74 PeiStackSize
= (UINTN
)PcdGet32 (PcdPeiTemporaryRamStackSize
);
75 if (PeiStackSize
== 0) {
76 PeiStackSize
= (CopySize
>> 1);
79 ASSERT (PeiStackSize
< CopySize
);
82 // |-------------------|---->
83 // | Stack | PeiStackSize
84 // |-------------------|---->
85 // | Heap | PeiTemporayRamSize
86 // |-------------------|----> TempRamBase
88 // |-------------------|---->
89 // | Heap | PeiTemporayRamSize
90 // |-------------------|---->
91 // | Stack | PeiStackSize
92 // |-------------------|----> PermanentMemoryBase
95 OldHeap
= (VOID
*)(UINTN
)TemporaryMemoryBase
;
96 NewHeap
= (VOID
*)((UINTN
)PermanentMemoryBase
+ PeiStackSize
);
98 OldStack
= (VOID
*)((UINTN
)TemporaryMemoryBase
+ CopySize
- PeiStackSize
);
99 NewStack
= (VOID
*)(UINTN
)PermanentMemoryBase
;
101 DebugAgentContext
.HeapMigrateOffset
= (UINTN
)NewHeap
- (UINTN
)OldHeap
;
102 DebugAgentContext
.StackMigrateOffset
= (UINTN
)NewStack
- (UINTN
)OldStack
;
104 OldStatus
= SaveAndSetDebugTimerInterrupt (FALSE
);
107 // Initialize Debug Agent to support source level debug in PEI phase after memory ready.
108 // It will build HOB and fix up the pointer in IDT table.
110 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC
, (VOID
*) &DebugAgentContext
, NULL
);
115 CopyMem (NewHeap
, OldHeap
, CopySize
- PeiStackSize
);
120 CopyMem (NewStack
, OldStack
, PeiStackSize
);
124 // We need *not* fix the return address because currently,
125 // The PeiCore is executed in flash.
129 // Rebase IDT table in permanent memory
131 AsmReadIdtr (&IdtDescriptor
);
132 IdtDescriptor
.Base
= IdtDescriptor
.Base
- (UINTN
)OldStack
+ (UINTN
)NewStack
;
134 AsmWriteIdtr (&IdtDescriptor
);
142 // SecSwitchStack function must be invoked after the memory migration
143 // immediatly, also we need fixup the stack change caused by new call into
147 (UINT32
) (UINTN
) OldStack
,
148 (UINT32
) (UINTN
) NewStack
151 SaveAndSetDebugTimerInterrupt (OldStatus
);