]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdeModulePkg/Core/Pei/Memory/MemoryServices.c
Add PeimDispatcherReenter to fix bug of PeiDispatch can not be quit correctly.
[mirror_edk2.git] / MdeModulePkg / Core / Pei / Memory / MemoryServices.c
... / ...
CommitLineData
1/** @file\r
2 EFI PEI Core memory services\r
3 \r
4Copyright (c) 2006, Intel Corporation \r
5All rights reserved. This program and the accompanying materials \r
6are licensed and made available under the terms and conditions of the BSD License \r
7which accompanies this distribution. The full text of the license may be found at \r
8http://opensource.org/licenses/bsd-license.php \r
9 \r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r
12\r
13**/\r
14\r
15#include <PeiMain.h>\r
16\r
17/**\r
18\r
19 Initialize the memory services.\r
20\r
21 @param PrivateData Points to PeiCore's private instance data.\r
22 @param SecCoreData Points to a data structure containing information about the PEI core's operating\r
23 environment, such as the size and location of temporary RAM, the stack location and\r
24 the BFV location.\r
25 @param OldCoreData Pointer to the PEI Core data.\r
26 NULL if being run in non-permament memory mode.\r
27\r
28**/\r
29VOID\r
30InitializeMemoryServices (\r
31 IN PEI_CORE_INSTANCE *PrivateData,\r
32 IN CONST EFI_SEC_PEI_HAND_OFF *SecCoreData,\r
33 IN PEI_CORE_INSTANCE *OldCoreData\r
34 )\r
35{\r
36 \r
37 PrivateData->SwitchStackSignal = FALSE;\r
38\r
39 //\r
40 // First entering PeiCore, following code will initialized some field\r
41 // in PeiCore's private data according to hand off data from sec core.\r
42 //\r
43 if (OldCoreData == NULL) {\r
44\r
45 PrivateData->PeiMemoryInstalled = FALSE;\r
46\r
47 PrivateData->BottomOfCarHeap = SecCoreData->PeiTemporaryRamBase; \r
48 PrivateData->TopOfCarHeap = (VOID *)((UINTN)(PrivateData->BottomOfCarHeap) + SecCoreData->PeiTemporaryRamSize);\r
49 PrivateData->SizeOfTemporaryMemory = SecCoreData->TemporaryRamSize;\r
50 PrivateData->StackSize = (UINT64) SecCoreData->StackSize;\r
51 \r
52 DEBUG_CODE_BEGIN ();\r
53 PrivateData->SizeOfCacheAsRam = SecCoreData->PeiTemporaryRamSize + SecCoreData->StackSize;\r
54 PrivateData->MaxTopOfCarHeap = (VOID *) ((UINTN) PrivateData->BottomOfCarHeap + (UINTN) PrivateData->SizeOfCacheAsRam);\r
55 PrivateData->StackBase = (EFI_PHYSICAL_ADDRESS) (UINTN) SecCoreData->StackBase;\r
56 PrivateData->StackSize = (UINT64) SecCoreData->StackSize;\r
57 DEBUG_CODE_END ();\r
58\r
59 PrivateData->HobList.Raw = PrivateData->BottomOfCarHeap;\r
60 \r
61 PeiCoreBuildHobHandoffInfoTable (\r
62 BOOT_WITH_FULL_CONFIGURATION,\r
63 (EFI_PHYSICAL_ADDRESS) (UINTN) PrivateData->BottomOfCarHeap,\r
64 (UINTN) SecCoreData->PeiTemporaryRamSize\r
65 );\r
66\r
67 //\r
68 // Set PS to point to ServiceTableShadow in Cache\r
69 //\r
70 PrivateData->PS = &(PrivateData->ServiceTableShadow);\r
71 }\r
72 \r
73 return;\r
74}\r
75\r
76/**\r
77\r
78 This function registers the found memory configuration with the PEI Foundation.\r
79\r
80 The usage model is that the PEIM that discovers the permanent memory shall invoke this service.\r
81 This routine will hold discoveried memory information into PeiCore's private data,\r
82 and set SwitchStackSignal flag. After PEIM who discovery memory is dispatched,\r
83 PeiDispatcher will migrate temporary memory to permenement memory.\r
84 \r
85 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
86 @param MemoryBegin Start of memory address.\r
87 @param MemoryLength Length of memory.\r
88\r
89 @return EFI_SUCCESS Always success.\r
90\r
91**/\r
92EFI_STATUS\r
93EFIAPI\r
94PeiInstallPeiMemory (\r
95 IN CONST EFI_PEI_SERVICES **PeiServices,\r
96 IN EFI_PHYSICAL_ADDRESS MemoryBegin,\r
97 IN UINT64 MemoryLength\r
98 )\r
99{\r
100 PEI_CORE_INSTANCE *PrivateData;\r
101\r
102 DEBUG ((EFI_D_INFO, "PeiInstallPeiMemory MemoryBegin 0x%LX, MemoryLength 0x%LX\n", MemoryBegin, MemoryLength));\r
103 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
104\r
105 PrivateData->PhysicalMemoryBegin = MemoryBegin;\r
106 PrivateData->PhysicalMemoryLength = MemoryLength;\r
107 PrivateData->FreePhysicalMemoryTop = MemoryBegin + MemoryLength;\r
108 \r
109 PrivateData->SwitchStackSignal = TRUE;\r
110\r
111 return EFI_SUCCESS; \r
112}\r
113\r
114/**\r
115\r
116 Memory allocation service on permanent memory,\r
117 not usable prior to the memory installation.\r
118\r
119\r
120 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
121 @param MemoryType Type of memory to allocate.\r
122 @param Pages Number of pages to allocate.\r
123 @param Memory Pointer of memory allocated.\r
124\r
125 @retval EFI_SUCCESS The allocation was successful\r
126 @retval EFI_NOT_AVAILABLE_YET Called with permanent memory not available\r
127 @retval EFI_OUT_OF_RESOURCES There is not enough HOB heap to satisfy the requirement\r
128 to allocate the number of pages.\r
129\r
130**/\r
131EFI_STATUS\r
132EFIAPI\r
133PeiAllocatePages (\r
134 IN CONST EFI_PEI_SERVICES **PeiServices,\r
135 IN EFI_MEMORY_TYPE MemoryType,\r
136 IN UINTN Pages,\r
137 OUT EFI_PHYSICAL_ADDRESS *Memory\r
138 )\r
139{\r
140 PEI_CORE_INSTANCE *PrivateData;\r
141 EFI_PEI_HOB_POINTERS Hob;\r
142 EFI_PHYSICAL_ADDRESS *FreeMemoryTop;\r
143 EFI_PHYSICAL_ADDRESS *FreeMemoryBottom;\r
144\r
145 PrivateData = PEI_CORE_INSTANCE_FROM_PS_THIS (PeiServices);\r
146 Hob.Raw = PrivateData->HobList.Raw;\r
147 \r
148 //\r
149 // Check if Hob already available\r
150 //\r
151 if (!PrivateData->PeiMemoryInstalled) {\r
152 //\r
153 // When PeiInstallMemory is called but CAR has *not* been moved to temporary memory,\r
154 // the AllocatePage will dependent on the field of PEI_CORE_INSTANCE structure.\r
155 //\r
156 if (!PrivateData->SwitchStackSignal) {\r
157 return EFI_NOT_AVAILABLE_YET;\r
158 } else {\r
159 FreeMemoryTop = &(PrivateData->FreePhysicalMemoryTop);\r
160 FreeMemoryBottom = &(PrivateData->PhysicalMemoryBegin);\r
161 }\r
162 } else {\r
163 FreeMemoryTop = &(Hob.HandoffInformationTable->EfiFreeMemoryTop);\r
164 FreeMemoryBottom = &(Hob.HandoffInformationTable->EfiFreeMemoryBottom);\r
165 }\r
166\r
167 //\r
168 // Check to see if on 4k boundary, If not aligned, make the allocation aligned.\r
169 //\r
170 *(FreeMemoryTop) -= *(FreeMemoryTop) & 0xFFF;\r
171 \r
172 //\r
173 // Verify that there is sufficient memory to satisfy the allocation\r
174 //\r
175 if (*(FreeMemoryTop) - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < \r
176 *(FreeMemoryBottom)) {\r
177 DEBUG ((EFI_D_ERROR, "AllocatePages failed: No 0x%x Pages is available.\n", Pages));\r
178 DEBUG ((EFI_D_ERROR, "There is only left 0x%x pages memory resource to be allocated.\n", \\r
179 EFI_SIZE_TO_PAGES ((UINTN) (*(FreeMemoryTop) - *(FreeMemoryBottom)))));\r
180 return EFI_OUT_OF_RESOURCES;\r
181 } else {\r
182 //\r
183 // Update the PHIT to reflect the memory usage\r
184 //\r
185 *(FreeMemoryTop) -= Pages * EFI_PAGE_SIZE;\r
186\r
187 //\r
188 // Update the value for the caller\r
189 //\r
190 *Memory = *(FreeMemoryTop);\r
191\r
192 //\r
193 // Create a memory allocation HOB.\r
194 //\r
195 BuildMemoryAllocationHob (\r
196 *(FreeMemoryTop),\r
197 Pages * EFI_PAGE_SIZE,\r
198 MemoryType\r
199 );\r
200\r
201 return EFI_SUCCESS;\r
202 }\r
203}\r
204\r
205/**\r
206\r
207 Pool allocation service. Before permenent memory is discoveried, the pool will \r
208 be allocated the heap in the CAR. Genenrally, the size of heap in temporary \r
209 memory does not exceed to 64K, so the biggest pool size could be allocated is \r
210 64K.\r
211\r
212 @param PeiServices An indirect pointer to the EFI_PEI_SERVICES table published by the PEI Foundation.\r
213 @param Size Amount of memory required\r
214 @param Buffer Address of pointer to the buffer\r
215\r
216 @retval EFI_SUCCESS The allocation was successful\r
217 @retval EFI_OUT_OF_RESOURCES There is not enough heap to satisfy the requirement\r
218 to allocate the requested size.\r
219\r
220**/\r
221EFI_STATUS\r
222EFIAPI\r
223PeiAllocatePool (\r
224 IN CONST EFI_PEI_SERVICES **PeiServices,\r
225 IN UINTN Size,\r
226 OUT VOID **Buffer\r
227 )\r
228{\r
229 EFI_STATUS Status;\r
230 EFI_HOB_MEMORY_POOL *Hob;\r
231\r
232 //\r
233 // If some "post-memory" PEIM wishes to allocate larger pool,\r
234 // it should use AllocatePages service instead.\r
235 //\r
236 \r
237 //\r
238 // Generally, the size of heap in temporary memory does not exceed to 64K,\r
239 // so the maxmium size of pool is 0x10000 - sizeof (EFI_HOB_MEMORY_POOL)\r
240 //\r
241 ASSERT (Size < 0x10000 - sizeof (EFI_HOB_MEMORY_POOL));\r
242 Status = PeiServicesCreateHob (\r
243 EFI_HOB_TYPE_MEMORY_POOL,\r
244 (UINT16)(sizeof (EFI_HOB_MEMORY_POOL) + Size),\r
245 (VOID **)&Hob\r
246 );\r
247 *Buffer = Hob+1; \r
248\r
249 return Status;\r
250}\r