599f9e17524a54f2ce4ecd2f437108ab1a9829ed
[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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20
21 #ifndef __MEMORY_H
22 #define __MEMORY_H
23
24
25 #define MEMTYPE_USABLE 0x01
26 #define MEMTYPE_RESERVED 0x02
27 #define MEMTYPE_ACPI_RECLAIM 0x03
28 #define MEMTYPE_ACPI_NVS 0x04
29
30 typedef struct
31 {
32 ULONGLONG BaseAddress;
33 ULONGLONG Length;
34 ULONG Type;
35 ULONG Reserved;
36 } PACKED BIOS_MEMORY_MAP, *PBIOS_MEMORY_MAP;
37
38 #if defined(__i386__) || defined(_PPC_)
39
40 #define MM_PAGE_SIZE 4096
41
42 #endif // defined __i386__ or _PPC_
43
44 typedef struct
45 {
46 ULONG PageAllocated; // Zero = free, non-zero = allocated
47 ULONG PageAllocationLength; // Number of pages allocated (or zero if this isn't the first page in the chain)
48 } PACKED PAGE_LOOKUP_TABLE_ITEM, *PPAGE_LOOKUP_TABLE_ITEM;
49
50 //
51 // Define this to 1 if you want the entire contents
52 // of the memory allocation bitmap displayed
53 // when a chunk is allocated or freed
54 //
55 #define DUMP_MEM_MAP_ON_VERIFY 0
56
57
58
59 extern PVOID PageLookupTableAddress;
60 extern ULONG TotalPagesInLookupTable;
61 extern ULONG FreePagesInLookupTable;
62 extern ULONG LastFreePageHint;
63
64 #ifdef DEBUG
65 PUCHAR MmGetSystemMemoryMapTypeString(ULONG Type);
66 #endif
67
68 ULONG MmGetPageNumberFromAddress(PVOID Address); // Returns the page number that contains a linear address
69 PVOID MmGetEndAddressOfAnyMemory(PBIOS_MEMORY_MAP BiosMemoryMap, ULONG MapCount); // Returns the last address of memory from the memory map
70 ULONG MmGetAddressablePageCountIncludingHoles(PBIOS_MEMORY_MAP BiosMemoryMap, ULONG MapCount); // Returns the count of addressable pages from address zero including any memory holes and reserved memory regions
71 PVOID MmFindLocationForPageLookupTable(PBIOS_MEMORY_MAP BiosMemoryMap, ULONG MapCount); // Returns the address for a memory chunk big enough to hold the page lookup table (starts search from end of memory)
72 VOID MmSortBiosMemoryMap(PBIOS_MEMORY_MAP BiosMemoryMap, ULONG MapCount); // Sorts the BIOS_MEMORY_MAP array so the first element corresponds to the first address in memory
73 VOID MmInitPageLookupTable(PVOID PageLookupTable, ULONG TotalPageCount, PBIOS_MEMORY_MAP BiosMemoryMap, ULONG MapCount); // Inits the page lookup table according to the memory types in the memory map
74 VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount, ULONG PageAllocated); // Marks the specified pages as allocated or free in the lookup table
75 VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount); // Allocates the specified pages in the lookup table
76 ULONG MmCountFreePagesInLookupTable(PVOID PageLookupTable, ULONG TotalPageCount); // Returns the number of free pages in the lookup table
77 ULONG MmFindAvailablePagesFromEnd(PVOID PageLookupTable, ULONG TotalPageCount, ULONG PagesNeeded); // Returns the page number of the first available page range from the end of memory
78 ULONG MmFindAvailablePagesBeforePage(PVOID PageLookupTable, ULONG TotalPageCount, ULONG PagesNeeded, ULONG LastPage); // Returns the page number of the first available page range before the specified page
79 VOID MmFixupSystemMemoryMap(PBIOS_MEMORY_MAP BiosMemoryMap, ULONG* MapCount); // Removes entries in the memory map that describe memory above 4G
80 VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, ULONG TotalPageCount); // Sets the LastFreePageHint to the last usable page of memory
81 BOOL MmAreMemoryPagesAvailable(PVOID PageLookupTable, ULONG TotalPageCount, PVOID PageAddress, ULONG PageCount); // Returns TRUE if the specified pages of memory are available, otherwise FALSE
82
83 ULONG GetSystemMemorySize(VOID); // Returns the amount of total memory in the system
84
85
86 //BOOL MmInitializeMemoryManager(ULONG LowMemoryStart, ULONG LowMemoryLength);
87 BOOL MmInitializeMemoryManager(VOID);
88 PVOID MmAllocateMemory(ULONG MemorySize);
89 VOID MmFreeMemory(PVOID MemoryPointer);
90 //PVOID MmAllocateLowMemory(ULONG MemorySize);
91 //VOID MmFreeLowMemory(PVOID MemoryPointer);
92 PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress);
93 PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress);
94
95 #endif // defined __MEMORY_H