1 /** @file
2   Data structure and functions to allocate and free memory space.
3 
4 Copyright (c) 2006 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #ifndef _IMEM_H_
10 #define _IMEM_H_
11 
12 //
13 // +---------------------------------------------------+
14 // | 0..(EfiMaxMemoryType - 1)    - Normal memory type |
15 // +---------------------------------------------------+
16 // | EfiMaxMemoryType..0x6FFFFFFF - Invalid            |
17 // +---------------------------------------------------+
18 // | 0x70000000..0x7FFFFFFF       - OEM reserved       |
19 // +---------------------------------------------------+
20 // | 0x80000000..0xFFFFFFFF       - OS reserved        |
21 // +---------------------------------------------------+
22 //
23 #define MEMORY_TYPE_OS_RESERVED_MIN                 0x80000000
24 #define MEMORY_TYPE_OS_RESERVED_MAX                 0xFFFFFFFF
25 #define MEMORY_TYPE_OEM_RESERVED_MIN                0x70000000
26 #define MEMORY_TYPE_OEM_RESERVED_MAX                0x7FFFFFFF
27 
28 //
29 // MEMORY_MAP_ENTRY
30 //
31 
32 #define MEMORY_MAP_SIGNATURE   SIGNATURE_32('m','m','a','p')
33 typedef struct {
34   UINTN           Signature;
35   LIST_ENTRY      Link;
36   BOOLEAN         FromPages;
37 
38   EFI_MEMORY_TYPE Type;
39   UINT64          Start;
40   UINT64          End;
41 
42   UINT64          VirtualStart;
43   UINT64          Attribute;
44 } MEMORY_MAP;
45 
46 //
47 // Internal prototypes
48 //
49 
50 
51 /**
52   Internal function.  Used by the pool functions to allocate pages
53   to back pool allocation requests.
54 
55   @param  PoolType               The type of memory for the new pool pages
56   @param  NumberOfPages          No of pages to allocate
57   @param  Alignment              Bits to align.
58   @param  NeedGuard              Flag to indicate Guard page is needed or not
59 
60   @return The allocated memory, or NULL
61 
62 **/
63 VOID *
64 CoreAllocatePoolPages (
65   IN EFI_MEMORY_TYPE    PoolType,
66   IN UINTN              NumberOfPages,
67   IN UINTN              Alignment,
68   IN BOOLEAN            NeedGuard
69   );
70 
71 
72 
73 /**
74   Internal function.  Frees pool pages allocated via AllocatePoolPages ()
75 
76   @param  Memory                 The base address to free
77   @param  NumberOfPages          The number of pages to free
78 
79 **/
80 VOID
81 CoreFreePoolPages (
82   IN EFI_PHYSICAL_ADDRESS   Memory,
83   IN UINTN                  NumberOfPages
84   );
85 
86 
87 
88 /**
89   Internal function to allocate pool of a particular type.
90   Caller must have the memory lock held
91 
92   @param  PoolType               Type of pool to allocate
93   @param  Size                   The amount of pool to allocate
94   @param  NeedGuard              Flag to indicate Guard page is needed or not
95 
96   @return The allocate pool, or NULL
97 
98 **/
99 VOID *
100 CoreAllocatePoolI (
101   IN EFI_MEMORY_TYPE  PoolType,
102   IN UINTN            Size,
103   IN BOOLEAN          NeedGuard
104   );
105 
106 
107 
108 /**
109   Internal function to free a pool entry.
110   Caller must have the memory lock held
111 
112   @param  Buffer                 The allocated pool entry to free
113   @param  PoolType               Pointer to pool type
114 
115   @retval EFI_INVALID_PARAMETER  Buffer not valid
116   @retval EFI_SUCCESS            Buffer successfully freed.
117 
118 **/
119 EFI_STATUS
120 CoreFreePoolI (
121   IN VOID               *Buffer,
122   OUT EFI_MEMORY_TYPE   *PoolType OPTIONAL
123   );
124 
125 
126 
127 /**
128   Enter critical section by gaining lock on gMemoryLock.
129 
130 **/
131 VOID
132 CoreAcquireMemoryLock (
133   VOID
134   );
135 
136 
137 /**
138   Exit critical section by releasing lock on gMemoryLock.
139 
140 **/
141 VOID
142 CoreReleaseMemoryLock (
143   VOID
144   );
145 
146 /**
147   Allocates pages from the memory map.
148 
149   @param  Type                   The type of allocation to perform
150   @param  MemoryType             The type of memory to turn the allocated pages
151                                  into
152   @param  NumberOfPages          The number of pages to allocate
153   @param  Memory                 A pointer to receive the base allocated memory
154                                  address
155   @param  NeedGuard              Flag to indicate Guard page is needed or not
156 
157   @return Status. On success, Memory is filled in with the base address allocated
158   @retval EFI_INVALID_PARAMETER  Parameters violate checking rules defined in
159                                  spec.
160   @retval EFI_NOT_FOUND          Could not allocate pages match the requirement.
161   @retval EFI_OUT_OF_RESOURCES   No enough pages to allocate.
162   @retval EFI_SUCCESS            Pages successfully allocated.
163 
164 **/
165 EFI_STATUS
166 EFIAPI
167 CoreInternalAllocatePages (
168   IN EFI_ALLOCATE_TYPE      Type,
169   IN EFI_MEMORY_TYPE        MemoryType,
170   IN UINTN                  NumberOfPages,
171   IN OUT EFI_PHYSICAL_ADDRESS  *Memory,
172   IN BOOLEAN                NeedGuard
173   );
174 
175 //
176 // Internal Global data
177 //
178 
179 extern EFI_LOCK           gMemoryLock;
180 extern LIST_ENTRY         gMemoryMap;
181 extern LIST_ENTRY         mGcdMemorySpaceMap;
182 #endif
183