ReactOS  0.4.15-dev-2344-g8ddbfde
mm.h
Go to the documentation of this file.
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  #ifdef _M_AMD64
25  /* .text/.data/.rdata, and .bss */
26  #define FREELDR_SECTION_COUNT 2
27  #else
28  /* .text/.data/.rdata, .edata and .bss */
29  #define FREELDR_SECTION_COUNT 3
30  #endif
31 #else
32 #ifdef _M_AMD64
33 /* .text, .rdata/.edata, .pdata and .data/.bss */
34 #define FREELDR_SECTION_COUNT 4
35 #else
36 /* .text, .rdata/.edata and .data/.bss */
37 #define FREELDR_SECTION_COUNT 3
38 #endif
39 #endif
40 
42 {
47 
48 
49 #if defined(__i386__) || defined(_PPC_) || defined(_MIPS_) || defined(_ARM_)
50 
51 #define MM_PAGE_SIZE 4096
52 #define MM_PAGE_MASK 0xFFF
53 #define MM_PAGE_SHIFT 12
54 #define MM_MAX_PAGE 0xFFFFF
55 
56 #define MM_SIZE_TO_PAGES(a) \
57  ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
58 
59 #endif // defined __i386__ or _PPC_ or _MIPS_
60 
61 #if defined (_AMD64_)
62 
63 #define MM_PAGE_SIZE 4096
64 #define MM_PAGE_MASK 0xFFF
65 #define MM_PAGE_SHIFT 12
66 #define MM_MAX_PAGE 0x3FFFF /* freeldr only maps 1 GB */
67 
68 #define MM_SIZE_TO_PAGES(a) \
69  ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
70 
71 #endif
72 
73 // HEAP and STACK size
74 #define HEAP_PAGES 0x400
75 #define STACK_PAGES 0x00
76 
77 #include <pshpack1.h>
78 typedef struct
79 {
80  TYPE_OF_MEMORY PageAllocated; // Type of allocated memory (LoaderFree if this memory is free)
81  PFN_NUMBER PageAllocationLength; // Number of pages allocated (or zero if this isn't the first page in the chain)
83 #include <poppack.h>
84 
85 //
86 // Define this to 1 if you want the entire contents
87 // of the memory allocation bitmap displayed
88 // when a chunk is allocated or freed
89 //
90 #define DUMP_MEM_MAP_ON_VERIFY 0
91 
96 
97 #if DBG
98 PCSTR MmGetSystemMemoryMapTypeString(TYPE_OF_MEMORY Type);
99 #endif
100 
101 PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address); // Returns the page number that contains a linear address
102 PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID); // Returns the count of addressable pages from address zero including any memory holes and reserved memory regions
103 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)
104 VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Inits the page lookup table according to the memory types in the memory map
105 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
106 VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType); // Allocates the specified pages in the lookup table
107 PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Returns the number of free pages in the lookup table
108 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
109 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
110 VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount); // Sets the LastFreePageHint to the last usable page of memory
111 BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount); // Returns TRUE if the specified pages of memory are available, otherwise FALSE
112 VOID MmSetMemoryType(PVOID MemoryAddress, SIZE_T MemorySize, TYPE_OF_MEMORY NewType); // Use with EXTREME caution!
113 
114 PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(PFN_NUMBER *NoEntries); // Returns a pointer to the memory mapping table and a number of entries in it
115 
116 
117 //BOOLEAN MmInitializeMemoryManager(ULONG LowMemoryStart, ULONG LowMemoryLength);
119 VOID MmInitializeHeap(PVOID PageLookupTable);
122 VOID MmFreeMemory(PVOID MemoryPointer);
125 
126 /* Heap */
127 #define DEFAULT_HEAP_SIZE (1024 * 1024)
128 #define TEMP_HEAP_SIZE (32 * 1024 * 1024)
129 
130 extern PVOID FrLdrDefaultHeap;
131 extern PVOID FrLdrTempHeap;
132 extern SIZE_T FrLdrImageSize;
133 
134 PVOID
137  TYPE_OF_MEMORY MemoryType);
138 
139 VOID
141  PVOID HeapHandle);
142 
143 VOID
145  PVOID HeapHandle);
146 
147 VOID
149  PVOID HeapHandle);
150 
151 VOID
153 
154 PVOID
156  PVOID HeapHandle,
158  ULONG Tag);
159 
160 VOID
162  PVOID HeapHandle,
163  PVOID Pointer,
164  ULONG Tag);
165 
167 PVOID
169 {
171 }
172 
174 VOID
175 FrLdrHeapFree(PVOID MemoryPointer, ULONG Tag)
176 {
177  FrLdrHeapFreeEx(FrLdrDefaultHeap, MemoryPointer, Tag);
178 }
179 
181 PVOID
183  _In_ SIZE_T Size,
184  _In_ ULONG Tag)
185 {
187 }
188 
190 VOID
193 {
195 }
196 
FORCEINLINE VOID FrLdrHeapFree(PVOID MemoryPointer, ULONG Tag)
Definition: mm.h:175
BOOLEAN MmInitializeMemoryManager(VOID)
Definition: meminit.c:313
VOID FrLdrHeapVerify(PVOID HeapHandle)
Definition: heap.c:156
PVOID FrLdrDefaultHeap
Definition: heap.c:34
TYPE_OF_MEMORY MemoryType
Definition: mm.h:43
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:361
PVOID FrLdrHeapAllocateEx(PVOID HeapHandle, SIZE_T ByteSize, ULONG Tag)
Definition: heap.c:320
PFN_NUMBER MmFindAvailablePagesBeforePage(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, PFN_NUMBER LastPage)
Definition: meminit.c:624
VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY MemoryType)
Definition: meminit.c:534
Allocation
Definition: exfuncs.h:598
PVOID MmFindLocationForPageLookupTable(PFN_NUMBER TotalPageCount)
Definition: meminit.c:414
VOID MmSetMemoryType(PVOID MemoryAddress, SIZE_T MemorySize, TYPE_OF_MEMORY NewType)
Definition: mm.c:144
_Must_inspect_result_ _In_ SIZE_T _In_ PVOID PageAddress
Definition: mmfuncs.h:471
enum _TYPE_OF_MEMORY TYPE_OF_MEMORY
int WINAPI StartPage(_In_ HDC)
PFN_NUMBER LastFreePageHint
Definition: meminit.c:29
VOID MmInitializeHeap(PVOID PageLookupTable)
Definition: heap.c:534
ULONG PFN_NUMBER
Definition: ke.h:9
FORCEINLINE PVOID FrLdrTempAlloc(_In_ SIZE_T Size, _In_ ULONG Tag)
Definition: mm.h:182
unsigned char BOOLEAN
static WCHAR Address[46]
Definition: ping.c:68
PFN_NUMBER FreePagesInLookupTable
Definition: meminit.c:28
PFN_NUMBER PageAllocationLength
Definition: mm.h:81
PFN_NUMBER MmGetAddressablePageCountIncludingHoles(VOID)
Definition: meminit.c:374
TYPE_OF_MEMORY PageAllocated
Definition: mm.h:80
PFN_NUMBER TotalPagesInLookupTable
Definition: meminit.c:27
PVOID MmAllocateHighestMemoryBelowAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:160
PFN_NUMBER BasePage
Definition: mm.h:44
VOID FrLdrHeapFreeEx(PVOID HeapHandle, PVOID Pointer, ULONG Tag)
Definition: heap.c:438
PVOID MmAllocateMemory(SIZE_T MemorySize)
FORCEINLINE PVOID FrLdrHeapAlloc(SIZE_T MemorySize, ULONG Tag)
Definition: mm.h:168
PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(PFN_NUMBER *NoEntries)
Definition: mm.c:297
PVOID FrLdrTempHeap
Definition: heap.c:35
Type
Definition: Type.h:6
VOID MmInitPageLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:461
VOID FrLdrHeapCleanupAll(VOID)
Definition: heap.c:248
static CONST DWORD MemorySize[]
Definition: svga.c:32
struct _FREELDR_MEMORY_DESCRIPTOR FREELDR_MEMORY_DESCRIPTOR
PVOID MmAllocateMemoryWithType(SIZE_T MemorySize, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:31
PVOID MmAllocateMemoryAtAddress(SIZE_T MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType)
Definition: mm.c:85
_Must_inspect_result_ _In_ WDFDEVICE _In_ BOOLEAN _In_opt_ PVOID Tag
Definition: wdfdevice.h:4061
PFN_NUMBER MmCountFreePagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:547
PFN_NUMBER MmGetPageNumberFromAddress(PVOID Address)
Definition: meminit.c:369
PVOID PageLookupTableAddress
Definition: meminit.c:26
VOID MmFreeMemory(PVOID MemoryPointer)
Definition: mm.c:215
#define _In_
Definition: no_sal2.h:158
ULONG_PTR SIZE_T
Definition: typedefs.h:80
BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PVOID PageAddress, PFN_NUMBER PageCount)
Definition: meminit.c:672
PFN_NUMBER MmFindAvailablePages(PVOID PageLookupTable, PFN_NUMBER TotalPageCount, PFN_NUMBER PagesNeeded, BOOLEAN FromEnd)
Definition: meminit.c:565
#define FORCEINLINE
Definition: wdftypes.h:67
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:360
unsigned int ULONG
Definition: retypes.h:1
VOID FrLdrHeapRelease(PVOID HeapHandle)
Definition: heap.c:181
SIZE_T FrLdrImageSize
Definition: meminit.c:35
const char * PCSTR
Definition: typedefs.h:52
char __ImageBase
Definition: mstscax.cpp:17
VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, PFN_NUMBER StartPage, PFN_NUMBER PageCount, TYPE_OF_MEMORY PageAllocated)
Definition: meminit.c:504
_IRQL_requires_same_ _In_ CLONG ByteSize
Definition: rtltypes.h:393
struct _FREELDR_MEMORY_DESCRIPTOR * PFREELDR_MEMORY_DESCRIPTOR
PVOID FrLdrHeapCreate(SIZE_T MaximumSize, TYPE_OF_MEMORY MemoryType)
Definition: heap.c:66
VOID FrLdrHeapDestroy(PVOID HeapHandle)
Definition: heap.c:137
struct PAGE_LOOKUP_TABLE_ITEM * PPAGE_LOOKUP_TABLE_ITEM
PFN_NUMBER PageCount
Definition: mm.h:45
VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, PFN_NUMBER TotalPageCount)
Definition: meminit.c:657
FORCEINLINE VOID FrLdrTempFree(PVOID Allocation, ULONG Tag)
Definition: mm.h:191