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