]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFspWrapperPkg/Library/SecPeiFspPlatformSecLibSample/SecTempRamSupport.c
b526bc0f53b2db8c7dc289d682ff332140628ac2
[mirror_edk2.git] / IntelFspWrapperPkg / Library / SecPeiFspPlatformSecLibSample / SecTempRamSupport.c
1 /** @file
2 Sample to provide SecTemporaryRamSupport function.
3
4 Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6
7 **/
8
9 #include <PiPei.h>
10
11 #include <Ppi/TemporaryRamSupport.h>
12
13 #include <Library/BaseMemoryLib.h>
14 #include <Library/DebugLib.h>
15 #include <Library/PcdLib.h>
16 #include <Library/DebugAgentLib.h>
17
18 /**
19 Switch the stack in the temporary memory to the one in the permanent memory.
20
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.
23
24 @param[in] TemporaryMemoryBase Base address of the temporary memory.
25 @param[in] PermenentMemoryBase Base address of the permanent memory.
26 **/
27 VOID
28 EFIAPI
29 SecSwitchStack (
30 IN UINT32 TemporaryMemoryBase,
31 IN UINT32 PermenentMemoryBase
32 );
33
34 /**
35 This service of the TEMPORARY_RAM_SUPPORT_PPI that migrates temporary RAM into
36 permanent memory.
37
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.
44
45 @retval EFI_SUCCESS The data was successfully returned.
46 @retval EFI_INVALID_PARAMETER PermanentMemoryBase + CopySize > TemporaryMemoryBase when
47 TemporaryMemoryBase > PermanentMemoryBase.
48
49 **/
50 EFI_STATUS
51 EFIAPI
52 SecTemporaryRamSupport (
53 IN CONST EFI_PEI_SERVICES **PeiServices,
54 IN EFI_PHYSICAL_ADDRESS TemporaryMemoryBase,
55 IN EFI_PHYSICAL_ADDRESS PermanentMemoryBase,
56 IN UINTN CopySize
57 )
58 {
59 IA32_DESCRIPTOR IdtDescriptor;
60 VOID* OldHeap;
61 VOID* NewHeap;
62 VOID* OldStack;
63 VOID* NewStack;
64 DEBUG_AGENT_CONTEXT_POSTMEM_SEC DebugAgentContext;
65 BOOLEAN OldStatus;
66 UINTN PeiStackSize;
67
68 PeiStackSize = (UINTN)PcdGet32 (PcdPeiTemporaryRamStackSize);
69 if (PeiStackSize == 0) {
70 PeiStackSize = (CopySize >> 1);
71 }
72
73 ASSERT (PeiStackSize < CopySize);
74
75 //
76 // |-------------------|---->
77 // | Stack | PeiStackSize
78 // |-------------------|---->
79 // | Heap | PeiTemporayRamSize
80 // |-------------------|----> TempRamBase
81 //
82 // |-------------------|---->
83 // | Heap | PeiTemporayRamSize
84 // |-------------------|---->
85 // | Stack | PeiStackSize
86 // |-------------------|----> PermanentMemoryBase
87 //
88
89 OldHeap = (VOID*)(UINTN)TemporaryMemoryBase;
90 NewHeap = (VOID*)((UINTN)PermanentMemoryBase + PeiStackSize);
91
92 OldStack = (VOID*)((UINTN)TemporaryMemoryBase + CopySize - PeiStackSize);
93 NewStack = (VOID*)(UINTN)PermanentMemoryBase;
94
95 DebugAgentContext.HeapMigrateOffset = (UINTN)NewHeap - (UINTN)OldHeap;
96 DebugAgentContext.StackMigrateOffset = (UINTN)NewStack - (UINTN)OldStack;
97
98 OldStatus = SaveAndSetDebugTimerInterrupt (FALSE);
99 //
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.
102 //
103 InitializeDebugAgent (DEBUG_AGENT_INIT_POSTMEM_SEC, (VOID *) &DebugAgentContext, NULL);
104
105 //
106 // Migrate Heap
107 //
108 CopyMem (NewHeap, OldHeap, CopySize - PeiStackSize);
109
110 //
111 // Migrate Stack
112 //
113 CopyMem (NewStack, OldStack, PeiStackSize);
114
115
116 //
117 // We need *not* fix the return address because currently,
118 // The PeiCore is executed in flash.
119 //
120
121 //
122 // Rebase IDT table in permanent memory
123 //
124 AsmReadIdtr (&IdtDescriptor);
125 IdtDescriptor.Base = IdtDescriptor.Base - (UINTN)OldStack + (UINTN)NewStack;
126
127 AsmWriteIdtr (&IdtDescriptor);
128
129
130 //
131 // Program MTRR
132 //
133
134 //
135 // SecSwitchStack function must be invoked after the memory migration
136 // immediately, also we need fixup the stack change caused by new call into
137 // permanent memory.
138 //
139 SecSwitchStack (
140 (UINT32) (UINTN) OldStack,
141 (UINT32) (UINTN) NewStack
142 );
143
144 SaveAndSetDebugTimerInterrupt (OldStatus);
145
146 return EFI_SUCCESS;
147 }
148