Sync with trunk r63935.
[reactos.git] / boot / freeldr / freeldr / include / mm.h
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 extern char __ImageBase;
23 #ifdef __GNUC__
24 #define FREELDR_SECTION_COUNT 3
25 #else
26 #ifdef _M_AMD64
27 /* .text and .pdata */
28 #define FREELDR_SECTION_COUNT 2
29 #else
30 #define FREELDR_SECTION_COUNT 1
31 #endif
32 #endif
33
34 typedef struct _FREELDR_MEMORY_DESCRIPTOR
35 {
36 TYPE_OF_MEMORY MemoryType;
37 PFN_NUMBER BasePage;
38 PFN_NUMBER PageCount;
39 } FREELDR_MEMORY_DESCRIPTOR, *PFREELDR_MEMORY_DESCRIPTOR;
40
41
42 #if defined(__i386__) || defined(_PPC_) || defined(_MIPS_) || defined(_ARM_)
43
44 #define MM_PAGE_SIZE 4096
45 #define MM_PAGE_MASK 0xFFF
46 #define MM_PAGE_SHIFT 12
47 #define MM_MAX_PAGE 0xFFFFF
48
49 #define MM_SIZE_TO_PAGES(a) \
50 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
51
52 #endif // defined __i386__ or _PPC_ or _MIPS_
53
54 #if defined (_AMD64_)
55
56 #define MM_PAGE_SIZE 4096
57 #define MM_PAGE_MASK 0xFFF
58 #define MM_PAGE_SHIFT 12
59 // FIXME: freeldr implementation uses ULONG for page numbers
60 #define MM_MAX_PAGE 0xFFFFFFFFFFFFF
61
62 #define MM_SIZE_TO_PAGES(a) \
63 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
64
65 #endif
66
67 // HEAP and STACK size
68 #define HEAP_PAGES 0x400
69 #define STACK_PAGES 0x00
70
71 #include <pshpack1.h>
72 typedef struct
73 {
74 TYPE_OF_MEMORY PageAllocated; // Type of allocated memory (LoaderFree if this memory is free)
75 PFN_NUMBER PageAllocationLength; // Number of pages allocated (or zero if this isn't the first page in the chain)
76 } PAGE_LOOKUP_TABLE_ITEM, *PPAGE_LOOKUP_TABLE_ITEM;
77 #include <poppack.h>
78
79 //
80 // Define this to 1 if you want the entire contents
81 // of the memory allocation bitmap displayed
82 // when a chunk is allocated or freed
83 //
84 #define DUMP_MEM_MAP_ON_VERIFY 0
85
86 extern PVOID PageLookupTableAddress;
87 extern PFN_NUMBER TotalPagesInLookupTable;
88 extern PFN_NUMBER FreePagesInLookupTable;
89 extern PFN_NUMBER LastFreePageHint;
90
91 #if DBG
92 PCSTR MmGetSystemMemoryMapTypeString(TYPE_OF_MEMORY Type);
93 #endif
94
95 PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address); // Returns the page number that contains a linear address
96 PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID); // Returns the count of addressable pages from address zero including any memory holes and reserved memory regions
97 PVOID MmFindLocationForPageLookupTable(PFN_NUMBER TotalPageCount); // Returns the address for a memory chunk big enough to hold the page lookup table (starts search from end of memory)
98 VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Inits the page lookup table according to the memory types in the memory map
99 VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated); // Marks the specified pages as allocated or free in the lookup table
100 VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType); // Allocates the specified pages in the lookup table
101 PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Returns the number of free pages in the lookup table
102 PFN_NUMBER MmFindAvailablePages(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd); // Returns the page number of the first available page range from the beginning or end of memory
103 PFN_NUMBER MmFindAvailablePagesBeforePage(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, PFN_NUMBER LastPage); // Returns the page number of the first available page range before the specified page
104 VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Sets the LastFreePageHint to the last usable page of memory
105 BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount); // Returns TRUE if the specified pages of memory are available, otherwise FALSE
106 VOID MmSetMemoryType(PVOID MemoryAddress, SIZE_T MemorySize, TYPE_OF_MEMORY NewType); // Use with EXTREME caution!
107
108 PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(PFN_NUMBER *NoEntries); // Returns a pointer to the memory mapping table and a number of entries in it
109
110
111 //BOOLEAN MmInitializeMemoryManager(ULONG LowMemoryStart, ULONG LowMemoryLength);
112 BOOLEAN MmInitializeMemoryManager(VOID);
113 VOID MmInitializeHeap(PVOID PageLookupTable);
114 PVOID MmAllocateMemory(SIZE_T MemorySize);
115 PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType);
116 VOID MmFreeMemory(PVOID MemoryPointer);
117 PVOID MmAllocateMemoryAtAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
118 PVOID MmAllocateHighestMemoryBelowAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
119
120 /* Heap */
121 #define DEFAULT_HEAP_SIZE (1024 * 1024)
122 #define TEMP_HEAP_SIZE (32 * 1024 * 1024)
123
124 extern PVOID FrLdrDefaultHeap;
125 extern PVOID FrLdrTempHeap;
126
127 PVOID
128 FrLdrHeapCreate(
129 SIZE_T MaximumSize,
130 TYPE_OF_MEMORY MemoryType);
131
132 VOID
133 FrLdrHeapDestroy(
134 PVOID HeapHandle);
135
136 VOID
137 FrLdrHeapRelease(
138 PVOID HeapHandle);
139
140 VOID
141 FrLdrHeapVerify(
142 PVOID HeapHandle);
143
144 VOID
145 FrLdrHeapCleanupAll(VOID);
146
147 PVOID
148 FrLdrHeapAllocateEx(
149 PVOID HeapHandle,
150 SIZE_T ByteSize,
151 ULONG Tag);
152
153 VOID
154 FrLdrHeapFreeEx(
155 PVOID HeapHandle,
156 PVOID Pointer,
157 ULONG Tag);
158
159 FORCEINLINE
160 PVOID
161 FrLdrHeapAlloc(SIZE_T MemorySize, ULONG Tag)
162 {
163 return FrLdrHeapAllocateEx(FrLdrDefaultHeap, MemorySize, Tag);
164 }
165
166 FORCEINLINE
167 VOID
168 FrLdrHeapFree(PVOID MemoryPointer, ULONG Tag)
169 {
170 FrLdrHeapFreeEx(FrLdrDefaultHeap, MemoryPointer, Tag);
171 }
172
173 FORCEINLINE
174 PVOID
175 FrLdrTempAlloc(
176 ULONG Size, ULONG Tag)
177 {
178 return FrLdrHeapAllocateEx(FrLdrTempHeap, Size, Tag);
179 }
180
181 FORCEINLINE
182 VOID
183 FrLdrTempFree(
184 PVOID Allocation, ULONG Tag)
185 {
186 FrLdrHeapFreeEx(FrLdrTempHeap, Allocation, Tag);
187 }
188