- Sync up Mm interface with WinLdr branch (introduce the concept of a memory type...
[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 typedef enum
25 {
26 BiosMemoryUsable=1,
27 BiosMemoryReserved,
28 BiosMemoryAcpiReclaim,
29 BiosMemoryAcpiNvs
30 } BIOS_MEMORY_TYPE;
31
32 typedef struct
33 {
34 ULONGLONG BaseAddress;
35 ULONGLONG Length;
36 ULONG Type;
37 ULONG Reserved;
38 } PACKED BIOS_MEMORY_MAP, *PBIOS_MEMORY_MAP;
39
40 #if defined(__i386__) || defined(_PPC_) || defined(_MIPS_)
41
42 #define MM_PAGE_SIZE 4096
43 #define MM_PAGE_MASK 0xFFF
44 #define MM_PAGE_SHIFT 12
45
46 #define MM_SIZE_TO_PAGES(a) \
47 ( ((a) >> MM_PAGE_SHIFT) + ((a) & MM_PAGE_MASK ? 1 : 0) )
48
49 #endif // defined __i386__ or _PPC_ or _MIPS_
50 //
51 // This is the zone which is used by the OS loader
52 //
53 #define LOADER_HIGH_ZONE ((16*1024*1024) >> MM_PAGE_SHIFT) //16Mb page
54
55 // HEAP and STACK size
56 #define HEAP_PAGES 0x100//0x18
57 #define STACK_PAGES 0x00
58
59 typedef struct
60 {
61 TYPE_OF_MEMORY PageAllocated; // Type of allocated memory (LoaderFree if this memory is free)
62 ULONG PageAllocationLength; // Number of pages allocated (or zero if this isn't the first page in the chain)
63 } PACKED PAGE_LOOKUP_TABLE_ITEM, *PPAGE_LOOKUP_TABLE_ITEM;
64
65 //
66 // Define this to 1 if you want the entire contents
67 // of the memory allocation bitmap displayed
68 // when a chunk is allocated or freed
69 //
70 #define DUMP_MEM_MAP_ON_VERIFY 0
71
72
73
74 extern PVOID PageLookupTableAddress;
75 extern ULONG TotalPagesInLookupTable;
76 extern ULONG FreePagesInLookupTable;
77 extern ULONG LastFreePageHint;
78
79 #ifdef DBG
80 PUCHAR MmGetSystemMemoryMapTypeString(ULONG Type);
81 #endif
82
83 ULONG MmGetPageNumberFromAddress(PVOID Address); // Returns the page number that contains a linear address
84 PVOID MmGetEndAddressOfAnyMemory(PBIOS_MEMORY_MAP BiosMemoryMap, ULONG MapCount); // Returns the last address of memory from the memory map
85 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
86 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)
87 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
88 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
89 VOID MmMarkPagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount, TYPE_OF_MEMORY PageAllocated); // Marks the specified pages as allocated or free in the lookup table
90 VOID MmAllocatePagesInLookupTable(PVOID PageLookupTable, ULONG StartPage, ULONG PageCount, TYPE_OF_MEMORY MemoryType); // Allocates the specified pages in the lookup table
91 ULONG MmCountFreePagesInLookupTable(PVOID PageLookupTable, ULONG TotalPageCount); // Returns the number of free pages in the lookup table
92 ULONG MmFindAvailablePages(PVOID PageLookupTable, ULONG TotalPageCount, ULONG PagesNeeded, BOOLEAN FromEnd); // Returns the page number of the first available page range from the beginning or end of memory
93 ULONG MmFindAvailablePagesBeforePage(PVOID PageLookupTable, ULONG TotalPageCount, ULONG PagesNeeded, ULONG LastPage); // Returns the page number of the first available page range before the specified page
94 VOID MmFixupSystemMemoryMap(PBIOS_MEMORY_MAP BiosMemoryMap, ULONG* MapCount); // Removes entries in the memory map that describe memory above 4G
95 VOID MmUpdateLastFreePageHint(PVOID PageLookupTable, ULONG TotalPageCount); // Sets the LastFreePageHint to the last usable page of memory
96 BOOLEAN MmAreMemoryPagesAvailable(PVOID PageLookupTable, ULONG TotalPageCount, PVOID PageAddress, ULONG PageCount); // Returns TRUE if the specified pages of memory are available, otherwise FALSE
97
98 ULONG GetSystemMemorySize(VOID); // Returns the amount of total memory in the system
99 PPAGE_LOOKUP_TABLE_ITEM MmGetMemoryMap(ULONG *NoEntries); // Returns a pointer to the memory mapping table and a number of entries in it
100
101
102 //BOOLEAN MmInitializeMemoryManager(ULONG LowMemoryStart, ULONG LowMemoryLength);
103 BOOLEAN MmInitializeMemoryManager(VOID);
104 VOID MmInitializeHeap(PVOID PageLookupTable);
105 PVOID MmAllocateMemory(ULONG MemorySize);
106 PVOID MmAllocateMemoryWithType(ULONG MemorySize, TYPE_OF_MEMORY MemoryType);
107 VOID MmFreeMemory(PVOID MemoryPointer);
108 VOID MmChangeAllocationPolicy(BOOLEAN PolicyAllocatePagesFromEnd);
109 //PVOID MmAllocateLowMemory(ULONG MemorySize);
110 //VOID MmFreeLowMemory(PVOID MemoryPointer);
111 PVOID MmAllocateMemoryAtAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
112 PVOID MmAllocateHighestMemoryBelowAddress(ULONG MemorySize, PVOID DesiredAddress, TYPE_OF_MEMORY MemoryType);
113
114 PVOID MmHeapAlloc(ULONG MemorySize);
115 VOID MmHeapFree(PVOID MemoryPointer);
116
117 #endif // defined __MEMORY_H