2 Sample to provide SecTemporaryRamSupport function.
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 #include <Ppi/TemporaryRamSupport.h>
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/DebugAgentLib.h>
19 Switch the stack in the temporary memory to the one in the permanent memory.
21 This function must be invoked after the memory migration immediately. The relative
22 position of the stack in the temporary and permanent memory is same.
24 @param[in] TemporaryMemoryBase Base address of the temporary memory.
25 @param[in] PermenentMemoryBase Base address of the permanent memory.
30 IN UINT32 TemporaryMemoryBase
,
31 IN UINT32 PermenentMemoryBase
35 This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
38 @param[in] PeiServices Pointer to the PEI Services Table.
39 @param[in] TemporaryMemoryBase Source Address in temporary memory from which the SEC or PEIM will copy the
40 Temporary RAM contents.
41 @param[in] PermanentMemoryBase Destination Address in permanent memory into which the SEC or PEIM will copy the
42 Temporary RAM contents.
43 @param[in] CopySize Amount of memory to migrate from temporary to permanent memory.
45 @retval EFI_SUCCESS The data was successfully returned.
46 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
47 TemporaryMemoryBase > PermanentMemoryBase.
52 SecTemporaryRamSupport (
53 IN CONST EFI_PEI_SERVICES
**PeiServices
,
54 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase
,
55 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase
,
59 IA32_DESCRIPTOR IdtDescriptor
;
64 DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext
;
68 PeiStackSize
= (UINTN
)PcdGet32 (PcdPeiTemporaryRamStackSize
);
69 if (PeiStackSize
== 0) {
70 PeiStackSize
= (CopySize
>> 1);
73 ASSERT (PeiStackSize
< CopySize
);
76 // |-------------------|---->
77 // | Stack | PeiStackSize
78 // |-------------------|---->
79 // | Heap | PeiTemporayRamSize
80 // |-------------------|----> TempRamBase
82 // |-------------------|---->
83 // | Heap | PeiTemporayRamSize
84 // |-------------------|---->
85 // | Stack | PeiStackSize
86 // |-------------------|----> PermanentMemoryBase
89 OldHeap
= (VOID
*)(UINTN
)TemporaryMemoryBase
;
90 NewHeap
= (VOID
*)((UINTN
)PermanentMemoryBase
+ PeiStackSize
);
92 OldStack
= (VOID
*)((UINTN
)TemporaryMemoryBase
+ CopySize
- PeiStackSize
);
93 NewStack
= (VOID
*)(UINTN
)PermanentMemoryBase
;
95 DebugAgentContext
.HeapMigrateOffset
= (UINTN
)NewHeap
- (UINTN
)OldHeap
;
96 DebugAgentContext
.StackMigrateOffset
= (UINTN
)NewStack
- (UINTN
)OldStack
;
98 OldStatus
= SaveAndSetDebugTimerInterrupt (FALSE
);
100 // Initialize Debug Agent to support source level debug in PEI phase after memory ready.
101 // It will build HOB and fix up the pointer in IDT table.
103 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC
, (VOID
*) &DebugAgentContext
, NULL
);
108 CopyMem (NewHeap
, OldHeap
, CopySize
- PeiStackSize
);
113 CopyMem (NewStack
, OldStack
, PeiStackSize
);
117 // We need *not* fix the return address because currently,
118 // The PeiCore is executed in flash.
122 // Rebase IDT table in permanent memory
124 AsmReadIdtr (&IdtDescriptor
);
125 IdtDescriptor
.Base
= IdtDescriptor
.Base
- (UINTN
)OldStack
+ (UINTN
)NewStack
;
127 AsmWriteIdtr (&IdtDescriptor
);
135 // SecSwitchStack function must be invoked after the memory migration
136 // immediately, also we need fixup the stack change caused by new call into
140 (UINT32
) (UINTN
) OldStack
,
141 (UINT32
) (UINTN
) NewStack
144 SaveAndSetDebugTimerInterrupt (OldStatus
);