[FREELDR]
[reactos.git] / reactos / 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 /* .text, .edata and .bss */
25 #define FREELDR_SECTION_COUNT 3
26 #else
27 #ifdef _M_AMD64
28 /* .text and .pdata */
29 #define FREELDR_SECTION_COUNT 2
30 #else
31 /* .text and .edata */
32 #define FREELDR_SECTION_COUNT 2
33 #endif
34 #endif
35
36 typedef struct _FREELDR_MEMORY_DESCRIPTOR
37 {
38 TYPE_OF_MEMORY MemoryType;
39 PFN_NUMBER BasePage;
40 PFN_NUMBER PageCount;
41 } FREELDR_MEMORY_DESCRIPTOR, *PFREELDR_MEMORY_DESCRIPTOR;
42
43
44 #if defined(__i386__) || defined(_PPC_) || defined(_MIPS_) || defined(_ARM_)
45
46 #define MM_PAGE_SIZE 4096
47 #define MM_PAGE_MASK 0xFFF
48 #define MM_PAGE_SHIFT 12
49 #define MM_MAX_PAGE 0xFFFFF
50
51 #define MM_SIZE_TO_PAGES(a) \
52 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
53
54 #endif // defined __i386__ or _PPC_ or _MIPS_
55
56 #if defined (_AMD64_)
57
58 #define MM_PAGE_SIZE 4096
59 #define MM_PAGE_MASK 0xFFF
60 #define MM_PAGE_SHIFT 12
61 // FIXME: freeldr implementation uses ULONG for page numbers
62 #define MM_MAX_PAGE 0xFFFFFFFFFFFFF
63
64 #define MM_SIZE_TO_PAGES(a) \
65 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
66
67 #endif
68
69 // HEAP and STACK size
70 #define HEAP_PAGES 0x400
71 #define STACK_PAGES 0x00
72
73 #include <pshpack1.h>
74 typedef struct
75 {
76 TYPE_OF_MEMORY PageAllocated; // Type of allocated memory (LoaderFree if this memory is free)
77 PFN_NUMBER PageAllocationLength; // Number of pages allocated (or zero if this isn't the first page in the chain)
78 } PAGE_LOOKUP_TABLE_ITEM, *PPAGE_LOOKUP_TABLE_ITEM;
79 #include <poppack.h>
80
81 //
82 // Define this to 1 if you want the entire contents
83 // of the memory allocation bitmap displayed
84 // when a chunk is allocated or freed
85 //
86 #define DUMP_MEM_MAP_ON_VERIFY 0
87
88 extern PVOID PageLookupTableAddress;
89 extern PFN_NUMBER TotalPagesInLookupTable;
90 extern PFN_NUMBER FreePagesInLookupTable;
91 extern PFN_NUMBER LastFreePageHint;
92
93 #if DBG
94 PCSTR MmGetSystemMemoryMapTypeString(TYPE_OF_MEMORY Type);
95 #endif
96
97 PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address); // Returns the page number that contains a linear address
98 PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID); // Returns the count of addressable pages from address zero including any memory holes and reserved memory regions
99 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)
100 VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Inits the page lookup table according to the memory types in the memory map
101 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
102 VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType); // Allocates the specified pages in the lookup table
103 PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Returns the number of free pages in the lookup table
104 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
105 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
106 VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Sets the LastFreePageHint to the last usable page of memory
107 BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount); // Returns TRUE if the specified pages of memory are available, otherwise FALSE
108 VOID MmSetMemoryType(PVOID MemoryAddress, SIZE_T MemorySize, TYPE_OF_MEMORY NewType); // Use with EXTREME caution!
109
110 PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(PFN_NUMBER *NoEntries); // Returns a pointer to the memory mapping table and a number of entries in it
111
112
113 //BOOLEAN MmInitializeMemoryManager(ULONG LowMemoryStart, ULONG LowMemoryLength);
114 BOOLEAN MmInitializeMemoryManager(VOID);
115 VOID MmInitializeHeap(PVOID PageLookupTable);
116 PVOID MmAllocateMemory(SIZE_T MemorySize);
117 PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType);
118 VOID MmFreeMemory(PVOID MemoryPointer);
119 PVOID MmAllocateMemoryAtAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
120 PVOID MmAllocateHighestMemoryBelowAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
121
122 /* Heap */
123 #define DEFAULT_HEAP_SIZE (1024 * 1024)
124 #define TEMP_HEAP_SIZE (32 * 1024 * 1024)
125
126 extern PVOID FrLdrDefaultHeap;
127 extern PVOID FrLdrTempHeap;
128 extern SIZE_T FrLdrImageSize;
129
130 PVOID
131 FrLdrHeapCreate(
132 SIZE_T MaximumSize,
133 TYPE_OF_MEMORY MemoryType);
134
135 VOID
136 FrLdrHeapDestroy(
137 PVOID HeapHandle);
138
139 VOID
140 FrLdrHeapRelease(
141 PVOID HeapHandle);
142
143 VOID
144 FrLdrHeapVerify(
145 PVOID HeapHandle);
146
147 VOID
148 FrLdrHeapCleanupAll(VOID);
149
150 PVOID
151 FrLdrHeapAllocateEx(
152 PVOID HeapHandle,
153 SIZE_T ByteSize,
154 ULONG Tag);
155
156 VOID
157 FrLdrHeapFreeEx(
158 PVOID HeapHandle,
159 PVOID Pointer,
160 ULONG Tag);
161
162 FORCEINLINE
163 PVOID
164 FrLdrHeapAlloc(SIZE_T MemorySize, ULONG Tag)
165 {
166 return FrLdrHeapAllocateEx(FrLdrDefaultHeap, MemorySize, Tag);
167 }
168
169 FORCEINLINE
170 VOID
171 FrLdrHeapFree(PVOID MemoryPointer, ULONG Tag)
172 {
173 FrLdrHeapFreeEx(FrLdrDefaultHeap, MemoryPointer, Tag);
174 }
175
176 FORCEINLINE
177 PVOID
178 FrLdrTempAlloc(
179 ULONG Size, ULONG Tag)
180 {
181 return FrLdrHeapAllocateEx(FrLdrTempHeap, Size, Tag);
182 }
183
184 FORCEINLINE
185 VOID
186 FrLdrTempFree(
187 PVOID Allocation, ULONG Tag)
188 {
189 FrLdrHeapFreeEx(FrLdrTempHeap, Allocation, Tag);
190 }
191