]> git.proxmox.com Git - mirror_edk2.git/blob - EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c
55e9249a5766d7df6c5843561b2ece0c9dbc7e4e
[mirror_edk2.git] / EmbeddedPkg / Library / PrePiMemoryAllocationLib / MemoryAllocationLib.c
1 /** @file
2 Implementation of the 6 PEI Ffs (FV) APIs in library form.
3
4 Copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
5
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include <PiPei.h>
17
18 #include <Library/BaseLib.h>
19 #include <Library/BaseMemoryLib.h>
20 #include <Library/PrePiLib.h>
21 #include <Library/DebugLib.h>
22
23
24
25 /**
26 Allocates one or more 4KB pages of type EfiBootServicesData.
27
28 Allocates the number of 4KB pages of MemoryType and returns a pointer to the
29 allocated buffer. The buffer returned is aligned on a 4KB boundary. If Pages is 0, then NULL
30 is returned. If there is not enough memory remaining to satisfy the request, then NULL is
31 returned.
32
33 @param Pages The number of 4 KB pages to allocate.
34
35 @return A pointer to the allocated buffer or NULL if allocation fails.
36
37 **/
38 VOID *
39 EFIAPI
40 AllocatePages (
41 IN UINTN Pages
42 )
43 {
44 EFI_PEI_HOB_POINTERS Hob;
45 EFI_PHYSICAL_ADDRESS Offset;
46
47 Hob.Raw = GetHobList ();
48
49 // Check to see if on 4k boundary
50 Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF;
51 if (Offset != 0) {
52 // If not aligned, make the allocation aligned.
53 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset;
54 }
55
56 //
57 // Verify that there is sufficient memory to satisfy the allocation
58 //
59 if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) {
60 return 0;
61 } else {
62 //
63 // Update the PHIT to reflect the memory usage
64 //
65 Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE;
66
67 // This routine used to create a memory allocation HOB a la PEI, but that's not
68 // necessary for us.
69
70 //
71 // Create a memory allocation HOB.
72 //
73 BuildMemoryAllocationHob (
74 Hob.HandoffInformationTable->EfiFreeMemoryTop,
75 Pages * EFI_PAGE_SIZE,
76 EfiBootServicesData
77 );
78 return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop;
79 }
80 }
81
82
83 /**
84 Allocates one or more 4KB pages of type EfiBootServicesData at a specified alignment.
85
86 Allocates the number of 4KB pages specified by Pages of type EfiBootServicesData with an
87 alignment specified by Alignment. The allocated buffer is returned. If Pages is 0, then NULL is
88 returned. If there is not enough memory at the specified alignment remaining to satisfy the
89 request, then NULL is returned.
90 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
91
92 @param Pages The number of 4 KB pages to allocate.
93 @param Alignment The requested alignment of the allocation. Must be a power of two.
94 If Alignment is zero, then byte alignment is used.
95
96 @return A pointer to the allocated buffer or NULL if allocation fails.
97
98 **/
99 VOID *
100 EFIAPI
101 AllocateAlignedPages (
102 IN UINTN Pages,
103 IN UINTN Alignment
104 )
105 {
106 VOID *Memory;
107 UINTN AlignmentMask;
108
109 //
110 // Alignment must be a power of two or zero.
111 //
112 ASSERT ((Alignment & (Alignment - 1)) == 0);
113
114 if (Pages == 0) {
115 return NULL;
116 }
117 //
118 // Make sure that Pages plus EFI_SIZE_TO_PAGES (Alignment) does not overflow.
119 //
120 ASSERT (Pages <= (MAX_ADDRESS - EFI_SIZE_TO_PAGES (Alignment)));
121 //
122 // We would rather waste some memory to save PEI code size.
123 //
124 Memory = (VOID *)(UINTN)AllocatePages (Pages + EFI_SIZE_TO_PAGES (Alignment));
125 if (Alignment == 0) {
126 AlignmentMask = Alignment;
127 } else {
128 AlignmentMask = Alignment - 1;
129 }
130 return (VOID *) (UINTN) (((UINTN) Memory + AlignmentMask) & ~AlignmentMask);
131 }
132
133
134 /**
135 Frees one or more 4KB pages that were previously allocated with one of the page allocation
136 functions in the Memory Allocation Library.
137
138 Frees the number of 4KB pages specified by Pages from the buffer specified by Buffer. Buffer
139 must have been allocated on a previous call to the page allocation services of the Memory
140 Allocation Library. If it is not possible to free allocated pages, then this function will
141 perform no actions.
142
143 If Buffer was not allocated with a page allocation function in the Memory Allocation Library,
144 then ASSERT().
145 If Pages is zero, then ASSERT().
146
147 @param Buffer Pointer to the buffer of pages to free.
148 @param Pages The number of 4 KB pages to free.
149
150 **/
151 VOID
152 EFIAPI
153 FreePages (
154 IN VOID *Buffer,
155 IN UINTN Pages
156 )
157 {
158 // For now, we do not support the ability to free pages in the PrePei Memory Allocator.
159 // The allocated memory is lost.
160 }
161
162 /**
163 Allocates a buffer of type EfiBootServicesData.
164
165 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData and returns a
166 pointer to the allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is
167 returned. If there is not enough memory remaining to satisfy the request, then NULL is returned.
168
169 @param AllocationSize The number of bytes to allocate.
170
171 @return A pointer to the allocated buffer or NULL if allocation fails.
172
173 **/
174 VOID *
175 EFIAPI
176 AllocatePool (
177 IN UINTN AllocationSize
178 )
179 {
180 EFI_HOB_MEMORY_POOL *Hob;
181
182 Hob = GetHobList ();
183
184
185 //
186 // Verify that there is sufficient memory to satisfy the allocation
187 //
188 if (AllocationSize > 0x10000) {
189 // Please call AllcoatePages for big allocations
190 return 0;
191 } else {
192
193 Hob = (EFI_HOB_MEMORY_POOL *)CreateHob (EFI_HOB_TYPE_MEMORY_POOL, (UINT16)(sizeof (EFI_HOB_TYPE_MEMORY_POOL) + AllocationSize));
194 return (VOID *)(Hob + 1);
195 }
196 }
197
198 /**
199 Allocates and zeros a buffer of type EfiBootServicesData.
200
201 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData, clears the
202 buffer with zeros, and returns a pointer to the allocated buffer. If AllocationSize is 0, then a
203 valid buffer of 0 size is returned. If there is not enough memory remaining to satisfy the
204 request, then NULL is returned.
205
206 @param AllocationSize The number of bytes to allocate and zero.
207
208 @return A pointer to the allocated buffer or NULL if allocation fails.
209
210 **/
211 VOID *
212 EFIAPI
213 AllocateZeroPool (
214 IN UINTN AllocationSize
215 )
216 {
217 VOID *Buffer;
218
219 Buffer = AllocatePool (AllocationSize);
220 if (Buffer == NULL) {
221 return NULL;
222 }
223
224 ZeroMem (Buffer, AllocationSize);
225
226 return Buffer;
227 }
228
229 /**
230 Frees a buffer that was previously allocated with one of the pool allocation functions in the
231 Memory Allocation Library.
232
233 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
234 pool allocation services of the Memory Allocation Library. If it is not possible to free pool
235 resources, then this function will perform no actions.
236
237 If Buffer was not allocated with a pool allocation function in the Memory Allocation Library,
238 then ASSERT().
239
240 @param Buffer Pointer to the buffer to free.
241
242 **/
243 VOID
244 EFIAPI
245 FreePool (
246 IN VOID *Buffer
247 )
248 {
249 // Not implemented yet
250 }