Revert 15473 and 15474 as Alex doesn't like them
[reactos.git] / reactos / ntoskrnl / mm / cont.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/mm/cont.c
6 * PURPOSE: Manages continuous memory
7 *
8 * PROGRAMMERS: David Welch (welch@cwcom.net)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <internal/debug.h>
16
17 /* FUNCTIONS *****************************************************************/
18
19 VOID STATIC
20 MmFreeContinuousPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
21 PFN_TYPE Page, SWAPENTRY SwapEntry,
22 BOOLEAN Dirty)
23 {
24 ASSERT(SwapEntry == 0);
25 if (Page != 0)
26 {
27 MmReleasePageMemoryConsumer(MC_NPPOOL, Page);
28 }
29 }
30
31 /*
32 * @implemented
33 */
34 PVOID STDCALL
35 MmAllocateContiguousAlignedMemory(IN ULONG NumberOfBytes,
36 IN PHYSICAL_ADDRESS LowestAcceptableAddress OPTIONAL,
37 IN PHYSICAL_ADDRESS HighestAcceptableAddress,
38 IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
39 IN MEMORY_CACHING_TYPE CacheType OPTIONAL,
40 IN ULONG Alignment)
41 {
42 PMEMORY_AREA MArea;
43 NTSTATUS Status;
44 PVOID BaseAddress = NULL;
45 PFN_TYPE PBase;
46 ULONG Attributes;
47 ULONG i;
48
49 Attributes = PAGE_EXECUTE_READWRITE | PAGE_SYSTEM;
50 if (CacheType == MmNonCached || CacheType == MmWriteCombined)
51 {
52 Attributes |= PAGE_NOCACHE;
53 }
54 if (CacheType == MmWriteCombined)
55 {
56 Attributes |= PAGE_WRITECOMBINE;
57 }
58
59 MmLockAddressSpace(MmGetKernelAddressSpace());
60 Status = MmCreateMemoryArea(NULL,
61 MmGetKernelAddressSpace(),
62 MEMORY_AREA_CONTINUOUS_MEMORY,
63 &BaseAddress,
64 NumberOfBytes,
65 0,
66 &MArea,
67 FALSE,
68 FALSE,
69 BoundaryAddressMultiple);
70 MmUnlockAddressSpace(MmGetKernelAddressSpace());
71
72 if (!NT_SUCCESS(Status))
73 {
74 return(NULL);
75 }
76 DPRINT( "Base = %x\n", BaseAddress );
77 PBase = MmGetContinuousPages(NumberOfBytes,
78 LowestAcceptableAddress,
79 HighestAcceptableAddress,
80 Alignment);
81 if (PBase == 0)
82 {
83 MmLockAddressSpace(MmGetKernelAddressSpace());
84 MmFreeMemoryArea(MmGetKernelAddressSpace(),
85 MArea,
86 NULL,
87 NULL);
88 MmUnlockAddressSpace(MmGetKernelAddressSpace());
89 return(NULL);
90 }
91 for (i = 0; i < (PAGE_ROUND_UP(NumberOfBytes) / PAGE_SIZE); i++, PBase++)
92 {
93 MmCreateVirtualMapping(NULL,
94 (char*)BaseAddress + (i * 4096),
95 Attributes,
96 &PBase,
97 1);
98 }
99 return(BaseAddress);
100 }
101
102 /**********************************************************************
103 * NAME EXPORTED
104 * MmAllocateContiguousMemory@12
105 *
106 * DESCRIPTION
107 * Allocates a range of physically contiguous cache aligned
108 * memory from the non-paged pool.
109 *
110 * ARGUMENTS
111 * NumberOfBytes
112 * Size of the memory block to allocate;
113 *
114 * HighestAcceptableAddress
115 * Highest address valid for the caller.
116 *
117 * RETURN VALUE
118 * The virtual address of the memory block on success;
119 * NULL on error.
120 *
121 * NOTE
122 * Description taken from include/ddk/mmfuncs.h.
123 * Code taken from ntoskrnl/mm/special.c.
124 *
125 * REVISIONS
126 *
127 * @implemented
128 */
129 PVOID STDCALL
130 MmAllocateContiguousMemory (IN ULONG NumberOfBytes,
131 IN PHYSICAL_ADDRESS HighestAcceptableAddress)
132 {
133 PHYSICAL_ADDRESS LowestAcceptableAddress;
134 PHYSICAL_ADDRESS BoundaryAddressMultiple;
135
136 LowestAcceptableAddress.QuadPart = 0;
137 BoundaryAddressMultiple.QuadPart = 0;
138
139 return(MmAllocateContiguousAlignedMemory(NumberOfBytes,
140 LowestAcceptableAddress,
141 HighestAcceptableAddress,
142 BoundaryAddressMultiple,
143 MmCached,
144 PAGE_SIZE));
145 }
146
147
148 /**********************************************************************
149 * NAME EXPORTED
150 * MmFreeContiguousMemory@4
151 *
152 * DESCRIPTION
153 * Releases a range of physically contiguous memory allocated
154 * with MmAllocateContiguousMemory.
155 *
156 * ARGUMENTS
157 * BaseAddress
158 * Virtual address of the memory to be freed.
159 *
160 * RETURN VALUE
161 * None.
162 *
163 * NOTE
164 * Description taken from include/ddk/mmfuncs.h.
165 * Code taken from ntoskrnl/mm/special.c.
166 *
167 * REVISIONS
168 *
169 * @implemented
170 */
171 VOID STDCALL
172 MmFreeContiguousMemory(IN PVOID BaseAddress)
173 {
174 MmLockAddressSpace(MmGetKernelAddressSpace());
175 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
176 BaseAddress,
177 MmFreeContinuousPage,
178 NULL);
179 MmUnlockAddressSpace(MmGetKernelAddressSpace());
180 }
181
182 /**********************************************************************
183 * NAME EXPORTED
184 * MmAllocateContiguousMemorySpecifyCache@32
185 *
186 * DESCRIPTION
187 * Allocates a range of physically contiguous memory
188 * with a cache parameter.
189 *
190 * ARGUMENTS
191 * NumberOfBytes
192 * Size of the memory block to allocate;
193 *
194 * LowestAcceptableAddress
195 * Lowest address valid for the caller.
196 *
197 * HighestAcceptableAddress
198 * Highest address valid for the caller.
199 *
200 * BoundaryAddressMultiple
201 * Address multiple not to be crossed by allocated buffer (optional).
202 *
203 * CacheType
204 * Type of caching to use.
205 *
206 * RETURN VALUE
207 * The virtual address of the memory block on success;
208 * NULL on error.
209 *
210 * REVISIONS
211 *
212 * @implemented
213 */
214 PVOID STDCALL
215 MmAllocateContiguousMemorySpecifyCache (IN ULONG NumberOfBytes,
216 IN PHYSICAL_ADDRESS LowestAcceptableAddress,
217 IN PHYSICAL_ADDRESS HighestAcceptableAddress,
218 IN PHYSICAL_ADDRESS BoundaryAddressMultiple OPTIONAL,
219 IN MEMORY_CACHING_TYPE CacheType)
220 {
221 return(MmAllocateContiguousAlignedMemory(NumberOfBytes,
222 LowestAcceptableAddress,
223 HighestAcceptableAddress,
224 BoundaryAddressMultiple,
225 CacheType,
226 PAGE_SIZE));
227 }
228
229 /**********************************************************************
230 * NAME EXPORTED
231 * MmFreeContiguousMemorySpecifyCache@12
232 *
233 * DESCRIPTION
234 * Releases a range of physically contiguous memory allocated
235 * with MmAllocateContiguousMemorySpecifyCache.
236 *
237 * ARGUMENTS
238 * BaseAddress
239 * Virtual address of the memory to be freed.
240 *
241 * NumberOfBytes
242 * Size of the memory block to free.
243 *
244 * CacheType
245 * Type of caching used.
246 *
247 * RETURN VALUE
248 * None.
249 *
250 * REVISIONS
251 *
252 * @implemented
253 */
254 VOID STDCALL
255 MmFreeContiguousMemorySpecifyCache(IN PVOID BaseAddress,
256 IN ULONG NumberOfBytes,
257 IN MEMORY_CACHING_TYPE CacheType)
258 {
259 MmLockAddressSpace(MmGetKernelAddressSpace());
260 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
261 BaseAddress,
262 MmFreeContinuousPage,
263 NULL);
264 MmUnlockAddressSpace(MmGetKernelAddressSpace());
265 }
266
267
268 /* EOF */