2 * Copyright (C) 2002-2005 ReactOS Team (and the authors from the programmers section)
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * PROJECT: ReactOS kernel
20 * FILE: ntoskrnl/mm/anonmem.c
21 * PURPOSE: Implementing anonymous memory.
23 * PROGRAMMERS: David Welch
32 * Gunnar Andre' Dalsnes
34 * Thomas Weidenmueller
40 /* INCLUDE *****************************************************************/
46 /* FUNCTIONS *****************************************************************/
50 MmWritePageVirtualMemory(PMMSUPPORT AddressSpace
,
51 PMEMORY_AREA MemoryArea
,
58 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
61 * Check for paging out from a deleted virtual memory area.
63 if (MemoryArea
->DeleteInProgress
)
65 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
66 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
67 MmReleasePageOp(PageOp
);
68 return(STATUS_UNSUCCESSFUL
);
71 Page
= MmGetPfnForProcess(Process
, Address
);
74 * Get that the page actually is dirty.
76 if (!MmIsDirtyPage(Process
, Address
))
78 PageOp
->Status
= STATUS_SUCCESS
;
79 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
80 MmReleasePageOp(PageOp
);
81 return(STATUS_SUCCESS
);
85 * Speculatively set the mapping to clean.
87 MmSetCleanPage(Process
, Address
);
90 * If necessary, allocate an entry in the paging file for this page
92 SwapEntry
= MmGetSavedSwapEntryPage(Page
);
95 SwapEntry
= MmAllocSwapPage();
98 MmSetDirtyPage(Process
, Address
);
99 PageOp
->Status
= STATUS_PAGEFILE_QUOTA_EXCEEDED
;
100 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
101 MmReleasePageOp(PageOp
);
102 return(STATUS_PAGEFILE_QUOTA_EXCEEDED
);
107 * Write the page to the pagefile
109 Status
= MmWriteToSwapPage(SwapEntry
, Page
);
110 if (!NT_SUCCESS(Status
))
112 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
114 MmSetDirtyPage(Process
, Address
);
115 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
116 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
117 MmReleasePageOp(PageOp
);
118 return(STATUS_UNSUCCESSFUL
);
122 * Otherwise we have succeeded.
124 MmSetSavedSwapEntryPage(Page
, SwapEntry
);
125 PageOp
->Status
= STATUS_SUCCESS
;
126 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
127 MmReleasePageOp(PageOp
);
128 return(STATUS_SUCCESS
);
133 MmPageOutVirtualMemory(PMMSUPPORT AddressSpace
,
134 PMEMORY_AREA MemoryArea
,
142 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
144 DPRINT("MmPageOutVirtualMemory(Address 0x%.8X) PID %d\n",
145 Address
, Process
->UniqueProcessId
);
148 * Check for paging out from a deleted virtual memory area.
150 if (MemoryArea
->DeleteInProgress
)
152 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
153 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
154 MmReleasePageOp(PageOp
);
155 return(STATUS_UNSUCCESSFUL
);
159 * Disable the virtual mapping.
161 MmDisableVirtualMapping(Process
, Address
,
166 KeBugCheck(MEMORY_MANAGEMENT
);
170 * Paging out non-dirty data is easy.
174 MmLockAddressSpace(AddressSpace
);
175 MmDeleteVirtualMapping(Process
, Address
, FALSE
, NULL
, NULL
);
176 MmDeleteAllRmaps(Page
, NULL
, NULL
);
177 if ((SwapEntry
= MmGetSavedSwapEntryPage(Page
)) != 0)
179 MmCreatePageFileMapping(Process
, Address
, SwapEntry
);
180 MmSetSavedSwapEntryPage(Page
, 0);
182 MmUnlockAddressSpace(AddressSpace
);
183 MmReleasePageMemoryConsumer(MC_USER
, Page
);
184 PageOp
->Status
= STATUS_SUCCESS
;
185 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
186 MmReleasePageOp(PageOp
);
187 return(STATUS_SUCCESS
);
191 * If necessary, allocate an entry in the paging file for this page
193 SwapEntry
= MmGetSavedSwapEntryPage(Page
);
196 SwapEntry
= MmAllocSwapPage();
199 MmShowOutOfSpaceMessagePagingFile();
200 MmEnableVirtualMapping(Process
, Address
);
201 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
202 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
203 MmReleasePageOp(PageOp
);
204 return(STATUS_PAGEFILE_QUOTA
);
209 * Write the page to the pagefile
211 Status
= MmWriteToSwapPage(SwapEntry
, Page
);
212 if (!NT_SUCCESS(Status
))
214 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
216 MmEnableVirtualMapping(Process
, Address
);
217 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
218 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
219 MmReleasePageOp(PageOp
);
220 return(STATUS_UNSUCCESSFUL
);
224 * Otherwise we have succeeded, free the page
226 DPRINT("MM: Swapped out virtual memory page 0x%.8X!\n", Page
<< PAGE_SHIFT
);
227 MmLockAddressSpace(AddressSpace
);
228 MmDeleteVirtualMapping(Process
, Address
, FALSE
, NULL
, NULL
);
229 MmCreatePageFileMapping(Process
, Address
, SwapEntry
);
230 MmUnlockAddressSpace(AddressSpace
);
231 MmDeleteAllRmaps(Page
, NULL
, NULL
);
232 MmSetSavedSwapEntryPage(Page
, 0);
233 MmReleasePageMemoryConsumer(MC_USER
, Page
);
234 PageOp
->Status
= STATUS_SUCCESS
;
235 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
236 MmReleasePageOp(PageOp
);
237 return(STATUS_SUCCESS
);
242 MmNotPresentFaultVirtualMemory(PMMSUPPORT AddressSpace
,
243 MEMORY_AREA
* MemoryArea
,
247 * FUNCTION: Move data into memory to satisfy a page not present fault
249 * AddressSpace = Address space within which the fault occurred
250 * MemoryArea = The memory area within which the fault occurred
251 * Address = The absolute address of fault
253 * NOTES: This function is called with the address space lock held.
260 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
263 * There is a window between taking the page fault and locking the
264 * address space when another thread could load the page so we check
267 if (MmIsPagePresent(NULL
, Address
))
269 return(STATUS_SUCCESS
);
273 * Check for the virtual memory area being deleted.
275 if (MemoryArea
->DeleteInProgress
)
277 return(STATUS_UNSUCCESSFUL
);
281 * Get the segment corresponding to the virtual address
283 Region
= MmFindRegion(MemoryArea
->StartingAddress
,
284 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
287 if (Region
->Type
== MEM_RESERVE
|| Region
->Protect
== PAGE_NOACCESS
)
289 return(STATUS_ACCESS_VIOLATION
);
295 if (Region
->Protect
& PAGE_GUARD
)
297 return(STATUS_GUARD_PAGE_VIOLATION
);
301 * Get or create a page operation
303 PageOp
= MmGetPageOp(MemoryArea
, Process
->UniqueProcessId
,
304 (PVOID
)PAGE_ROUND_DOWN(Address
), NULL
, 0,
305 MM_PAGEOP_PAGEIN
, FALSE
);
308 DPRINT1("MmGetPageOp failed");
309 KeBugCheck(MEMORY_MANAGEMENT
);
313 * Check if someone else is already handling this fault, if so wait
316 if (PageOp
->Thread
!= PsGetCurrentThread())
318 MmUnlockAddressSpace(AddressSpace
);
319 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
325 * Check for various strange conditions
327 if (Status
!= STATUS_SUCCESS
)
329 DPRINT1("Failed to wait for page op\n");
330 KeBugCheck(MEMORY_MANAGEMENT
);
332 if (PageOp
->Status
== STATUS_PENDING
)
334 DPRINT1("Woke for page op before completion\n");
335 KeBugCheck(MEMORY_MANAGEMENT
);
338 * If this wasn't a pagein then we need to restart the handling
340 if (PageOp
->OpType
!= MM_PAGEOP_PAGEIN
)
342 MmLockAddressSpace(AddressSpace
);
343 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
344 MmReleasePageOp(PageOp
);
345 return(STATUS_MM_RESTART_OPERATION
);
348 * If the thread handling this fault has failed then we don't retry
350 if (!NT_SUCCESS(PageOp
->Status
))
352 MmLockAddressSpace(AddressSpace
);
353 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
354 Status
= PageOp
->Status
;
355 MmReleasePageOp(PageOp
);
358 MmLockAddressSpace(AddressSpace
);
359 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
360 MmReleasePageOp(PageOp
);
361 return(STATUS_SUCCESS
);
365 * Try to allocate a page
367 Status
= MmRequestPageMemoryConsumer(MC_USER
, FALSE
, &Page
);
368 if (Status
== STATUS_NO_MEMORY
)
370 MmUnlockAddressSpace(AddressSpace
);
371 Status
= MmRequestPageMemoryConsumer(MC_USER
, TRUE
, &Page
);
372 MmLockAddressSpace(AddressSpace
);
374 if (!NT_SUCCESS(Status
))
376 DPRINT1("MmRequestPageMemoryConsumer failed, status = %x\n", Status
);
377 KeBugCheck(MEMORY_MANAGEMENT
);
381 * Handle swapped out pages.
383 if (MmIsPageSwapEntry(NULL
, Address
))
387 MmDeletePageFileMapping(Process
, Address
, &SwapEntry
);
388 Status
= MmReadFromSwapPage(SwapEntry
, Page
);
389 if (!NT_SUCCESS(Status
))
391 KeBugCheck(MEMORY_MANAGEMENT
);
393 MmSetSavedSwapEntryPage(Page
, SwapEntry
);
397 * Set the page. If we fail because we are out of memory then
400 Status
= MmCreateVirtualMapping(Process
,
401 (PVOID
)PAGE_ROUND_DOWN(Address
),
405 while (Status
== STATUS_NO_MEMORY
)
407 MmUnlockAddressSpace(AddressSpace
);
408 Status
= MmCreateVirtualMapping(Process
,
413 MmLockAddressSpace(AddressSpace
);
415 if (!NT_SUCCESS(Status
))
417 DPRINT1("MmCreateVirtualMapping failed, not out of memory\n");
418 KeBugCheck(MEMORY_MANAGEMENT
);
423 * Add the page to the process's working set
425 MmInsertRmap(Page
, Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
428 * Finish the operation
430 PageOp
->Status
= STATUS_SUCCESS
;
431 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
432 MmReleasePageOp(PageOp
);
433 return(STATUS_SUCCESS
);
437 MmModifyAttributes(PMMSUPPORT AddressSpace
,
445 * FUNCTION: Modify the attributes of a memory region
448 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
451 * If we are switching a previously committed region to reserved then
452 * free any allocated pages within the region
454 if (NewType
== MEM_RESERVE
&& OldType
== MEM_COMMIT
)
458 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
462 if (MmIsPageSwapEntry(Process
,
463 (char*)BaseAddress
+ (i
* PAGE_SIZE
)))
467 MmDeletePageFileMapping(Process
,
468 (char*)BaseAddress
+ (i
* PAGE_SIZE
),
470 MmFreeSwapPage(SwapEntry
);
474 MmDeleteVirtualMapping(Process
,
475 (char*)BaseAddress
+ (i
*PAGE_SIZE
),
479 SWAPENTRY SavedSwapEntry
;
480 SavedSwapEntry
= MmGetSavedSwapEntryPage(Page
);
481 if (SavedSwapEntry
!= 0)
483 MmFreeSwapPage(SavedSwapEntry
);
484 MmSetSavedSwapEntryPage(Page
, 0);
486 MmDeleteRmap(Page
, Process
,
487 (char*)BaseAddress
+ (i
* PAGE_SIZE
));
488 MmReleasePageMemoryConsumer(MC_USER
, Page
);
495 * If we are changing the protection attributes of a committed region then
496 * alter the attributes for any allocated pages within the region
498 if (NewType
== MEM_COMMIT
&& OldType
== MEM_COMMIT
&&
499 OldProtect
!= NewProtect
)
503 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
505 if (MmIsPagePresent(Process
,
506 (char*)BaseAddress
+ (i
*PAGE_SIZE
)))
508 MmSetPageProtect(Process
,
509 (char*)BaseAddress
+ (i
*PAGE_SIZE
),
520 NtAllocateVirtualMemory(IN HANDLE ProcessHandle
,
521 IN OUT PVOID
* UBaseAddress
,
522 IN ULONG_PTR ZeroBits
,
523 IN OUT PSIZE_T URegionSize
,
524 IN ULONG AllocationType
,
527 * FUNCTION: Allocates a block of virtual memory in the process address space
529 * ProcessHandle = The handle of the process which owns the virtual memory
530 * BaseAddress = A pointer to the virtual memory allocated. If you
531 * supply a non zero value the system will try to
532 * allocate the memory at the address supplied. It round
533 * it down to a multiple of the page size.
534 * ZeroBits = (OPTIONAL) You can specify the number of high order bits
535 * that must be zero, ensuring that the memory will be
536 * allocated at a address below a certain value.
537 * RegionSize = The number of bytes to allocate
538 * AllocationType = Indicates the type of virtual memory you like to
539 * allocated, can be a combination of MEM_COMMIT,
540 * MEM_RESERVE, MEM_RESET, MEM_TOP_DOWN.
541 * Protect = Indicates the protection type of the pages allocated.
546 MEMORY_AREA
* MemoryArea
;
547 ULONG_PTR MemoryAreaLength
;
550 PMMSUPPORT AddressSpace
;
556 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
557 KPROCESSOR_MODE PreviousMode
;
561 DPRINT("NtAllocateVirtualMemory(*UBaseAddress %x, "
562 "ZeroBits %d, *URegionSize %x, AllocationType %x, Protect %x)\n",
563 *UBaseAddress
,ZeroBits
,*URegionSize
,AllocationType
,
566 /* Check for valid protection flags */
567 MemProtection
= Protect
& ~(PAGE_GUARD
|PAGE_NOCACHE
);
568 if (MemProtection
!= PAGE_NOACCESS
&&
569 MemProtection
!= PAGE_READONLY
&&
570 MemProtection
!= PAGE_READWRITE
&&
571 MemProtection
!= PAGE_WRITECOPY
&&
572 MemProtection
!= PAGE_EXECUTE
&&
573 MemProtection
!= PAGE_EXECUTE_READ
&&
574 MemProtection
!= PAGE_EXECUTE_READWRITE
&&
575 MemProtection
!= PAGE_EXECUTE_WRITECOPY
)
577 DPRINT1("Invalid page protection\n");
578 return STATUS_INVALID_PAGE_PROTECTION
;
581 /* Check for valid Zero bits */
584 DPRINT1("Too many zero bits\n");
585 return STATUS_INVALID_PARAMETER_3
;
588 /* Check for valid Allocation Types */
589 if ((AllocationType
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
| MEM_PHYSICAL
|
590 MEM_TOP_DOWN
| MEM_WRITE_WATCH
)))
592 DPRINT1("Invalid Allocation Type\n");
593 return STATUS_INVALID_PARAMETER_5
;
596 /* Check for at least one of these Allocation Types to be set */
597 if (!(AllocationType
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)))
599 DPRINT1("No memory allocation base type\n");
600 return STATUS_INVALID_PARAMETER_5
;
603 /* MEM_RESET is an exclusive flag, make sure that is valid too */
604 if ((AllocationType
& MEM_RESET
) && (AllocationType
!= MEM_RESET
))
606 DPRINT1("Invalid use of MEM_RESET\n");
607 return STATUS_INVALID_PARAMETER_5
;
610 /* MEM_WRITE_WATCH can only be used if MEM_RESERVE is also used */
611 if ((AllocationType
& MEM_WRITE_WATCH
) && !(AllocationType
& MEM_RESERVE
))
613 DPRINT1("MEM_WRITE_WATCH used without MEM_RESERVE\n");
614 return STATUS_INVALID_PARAMETER_5
;
617 /* MEM_PHYSICAL can only be used with MEM_RESERVE, and can only be R/W */
618 if (AllocationType
& MEM_PHYSICAL
)
620 /* First check for MEM_RESERVE exclusivity */
621 if (AllocationType
!= (MEM_RESERVE
| MEM_PHYSICAL
))
623 DPRINT1("MEM_PHYSICAL used with other flags then MEM_RESERVE or"
624 "MEM_RESERVE was not present at all\n");
625 return STATUS_INVALID_PARAMETER_5
;
628 /* Then make sure PAGE_READWRITE is used */
629 if (Protect
!= PAGE_READWRITE
)
631 DPRINT1("MEM_PHYSICAL used without PAGE_READWRITE\n");
632 return STATUS_INVALID_PAGE_PROTECTION
;
636 PreviousMode
= KeGetPreviousMode();
640 if (PreviousMode
!= KernelMode
)
642 ProbeForWritePointer(UBaseAddress
);
643 ProbeForWriteUlong(URegionSize
);
645 PBaseAddress
= *UBaseAddress
;
646 PRegionSize
= *URegionSize
;
648 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
650 /* Return the exception code */
651 _SEH2_YIELD(return _SEH2_GetExceptionCode());
655 BoundaryAddressMultiple
.QuadPart
= 0;
657 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(PBaseAddress
);
658 RegionSize
= PAGE_ROUND_UP((ULONG_PTR
)PBaseAddress
+ PRegionSize
) -
659 PAGE_ROUND_DOWN(PBaseAddress
);
662 * We've captured and calculated the data, now do more checks
663 * Yes, MmCreateMemoryArea does similar checks, but they don't return
664 * the right status codes that a caller of this routine would expect.
666 if ((ULONG_PTR
)BaseAddress
>= USER_SHARED_DATA
)
668 DPRINT1("Virtual allocation base above User Space\n");
669 return STATUS_INVALID_PARAMETER_2
;
673 DPRINT1("Region size is invalid (zero)\n");
674 return STATUS_INVALID_PARAMETER_4
;
676 if ((USER_SHARED_DATA
- (ULONG_PTR
)BaseAddress
) < RegionSize
)
678 DPRINT1("Region size would overflow into kernel-memory\n");
679 return STATUS_INVALID_PARAMETER_4
;
683 * Copy on Write is reserved for system use. This case is a certain failure
684 * but there may be other cases...needs more testing
686 if ((!BaseAddress
|| (AllocationType
& MEM_RESERVE
)) &&
687 (Protect
& (PAGE_WRITECOPY
| PAGE_EXECUTE_WRITECOPY
)))
689 DPRINT1("Copy on write is not supported by VirtualAlloc\n");
690 return STATUS_INVALID_PAGE_PROTECTION
;
694 Status
= ObReferenceObjectByHandle(ProcessHandle
,
695 PROCESS_VM_OPERATION
,
700 if (!NT_SUCCESS(Status
))
702 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
706 Type
= (AllocationType
& MEM_COMMIT
) ? MEM_COMMIT
: MEM_RESERVE
;
707 DPRINT("Type %x\n", Type
);
709 AddressSpace
= &Process
->Vm
;
710 MmLockAddressSpace(AddressSpace
);
712 if (PBaseAddress
!= 0)
714 MemoryArea
= MmLocateMemoryAreaByAddress(AddressSpace
, BaseAddress
);
716 if (MemoryArea
!= NULL
)
718 MemoryAreaLength
= (ULONG_PTR
)MemoryArea
->EndingAddress
-
719 (ULONG_PTR
)MemoryArea
->StartingAddress
;
721 if (((ULONG_PTR
)BaseAddress
+ RegionSize
) > (ULONG_PTR
)MemoryArea
->EndingAddress
)
723 DPRINT("BaseAddress + RegionSize %x is larger than MemoryArea's EndingAddress %x\n",
724 (ULONG_PTR
)BaseAddress
+ RegionSize
, MemoryArea
->EndingAddress
);
726 MmUnlockAddressSpace(AddressSpace
);
727 ObDereferenceObject(Process
);
729 return STATUS_MEMORY_NOT_ALLOCATED
;
732 if (AllocationType
== MEM_RESET
)
734 if (MmIsPagePresent(Process
, BaseAddress
))
736 /* FIXME: mark pages as not modified */
740 /* FIXME: if pages are in paging file discard them and bring in pages of zeros */
743 MmUnlockAddressSpace(AddressSpace
);
744 ObDereferenceObject(Process
);
746 /* MEM_RESET does not modify any attributes of region */
747 return STATUS_SUCCESS
;
750 if (MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
&&
751 MemoryAreaLength
>= RegionSize
)
754 MmAlterRegion(AddressSpace
,
755 MemoryArea
->StartingAddress
,
756 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
757 BaseAddress
, RegionSize
,
758 Type
, Protect
, MmModifyAttributes
);
759 MmUnlockAddressSpace(AddressSpace
);
760 ObDereferenceObject(Process
);
761 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
763 /* Give the caller rounded BaseAddress and area length */
764 if (NT_SUCCESS(Status
))
766 *UBaseAddress
= BaseAddress
;
767 *URegionSize
= RegionSize
;
768 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
773 else if (MemoryAreaLength
>= RegionSize
)
775 /* Region list initialized? */
776 if (MemoryArea
->Data
.SectionData
.RegionListHead
.Flink
)
779 MmAlterRegion(AddressSpace
,
780 MemoryArea
->StartingAddress
,
781 &MemoryArea
->Data
.SectionData
.RegionListHead
,
782 BaseAddress
, RegionSize
,
783 Type
, Protect
, MmModifyAttributes
);
787 Status
= STATUS_ACCESS_VIOLATION
;
790 MmUnlockAddressSpace(AddressSpace
);
791 ObDereferenceObject(Process
);
792 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
794 /* Give the caller rounded BaseAddress and area length */
795 if (NT_SUCCESS(Status
))
797 *UBaseAddress
= BaseAddress
;
798 *URegionSize
= RegionSize
;
799 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
806 MmUnlockAddressSpace(AddressSpace
);
807 ObDereferenceObject(Process
);
808 return(STATUS_UNSUCCESSFUL
);
813 Status
= MmCreateMemoryArea(AddressSpace
,
814 MEMORY_AREA_VIRTUAL_MEMORY
,
820 AllocationType
& MEM_TOP_DOWN
,
821 BoundaryAddressMultiple
);
822 if (!NT_SUCCESS(Status
))
824 MmUnlockAddressSpace(AddressSpace
);
825 ObDereferenceObject(Process
);
826 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
830 MemoryAreaLength
= (ULONG_PTR
)MemoryArea
->EndingAddress
-
831 (ULONG_PTR
)MemoryArea
->StartingAddress
;
833 MmInitializeRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
834 MemoryAreaLength
, Type
, Protect
);
836 if ((AllocationType
& MEM_COMMIT
) &&
837 (Protect
& (PAGE_READWRITE
| PAGE_EXECUTE_READWRITE
)))
839 const ULONG nPages
= PAGE_ROUND_UP(MemoryAreaLength
) >> PAGE_SHIFT
;
840 MmReserveSwapPages(nPages
);
843 *UBaseAddress
= BaseAddress
;
844 *URegionSize
= MemoryAreaLength
;
845 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
847 MmUnlockAddressSpace(AddressSpace
);
848 ObDereferenceObject(Process
);
849 return(STATUS_SUCCESS
);
853 MmFreeVirtualMemoryPage(PVOID Context
,
854 MEMORY_AREA
* MemoryArea
,
860 PEPROCESS Process
= (PEPROCESS
)Context
;
864 SWAPENTRY SavedSwapEntry
;
865 SavedSwapEntry
= MmGetSavedSwapEntryPage(Page
);
866 if (SavedSwapEntry
!= 0)
868 MmFreeSwapPage(SavedSwapEntry
);
869 MmSetSavedSwapEntryPage(Page
, 0);
871 MmDeleteRmap(Page
, Process
, Address
);
872 MmReleasePageMemoryConsumer(MC_USER
, Page
);
874 else if (SwapEntry
!= 0)
876 MmFreeSwapPage(SwapEntry
);
882 MmFreeVirtualMemory(PEPROCESS Process
,
883 PMEMORY_AREA MemoryArea
)
885 PLIST_ENTRY current_entry
;
889 DPRINT("MmFreeVirtualMemory(Process %p MemoryArea %p)\n", Process
,
892 /* Mark this memory area as about to be deleted. */
893 MemoryArea
->DeleteInProgress
= TRUE
;
896 * Wait for any ongoing paging operations. Notice that since we have
897 * flagged this memory area as deleted no more page ops will be added.
899 if (MemoryArea
->PageOpCount
> 0)
901 ULONG_PTR MemoryAreaLength
= (ULONG_PTR
)MemoryArea
->EndingAddress
-
902 (ULONG_PTR
)MemoryArea
->StartingAddress
;
903 const ULONG nPages
= PAGE_ROUND_UP(MemoryAreaLength
) >> PAGE_SHIFT
;
905 for (i
= 0; i
< nPages
&& MemoryArea
->PageOpCount
!= 0; ++i
)
908 PageOp
= MmCheckForPageOp(MemoryArea
, Process
->UniqueProcessId
,
909 (PVOID
)((ULONG_PTR
)MemoryArea
->StartingAddress
+ (i
* PAGE_SIZE
)),
914 MmUnlockAddressSpace(&Process
->Vm
);
915 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
920 if (Status
!= STATUS_SUCCESS
)
922 DPRINT1("Failed to wait for page op\n");
923 KeBugCheck(MEMORY_MANAGEMENT
);
925 MmLockAddressSpace(&Process
->Vm
);
926 MmReleasePageOp(PageOp
);
931 /* Free all the individual segments. */
932 current_entry
= MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
.Flink
;
933 while (current_entry
!= &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
)
935 current
= CONTAINING_RECORD(current_entry
, MM_REGION
, RegionListEntry
);
936 current_entry
= current_entry
->Flink
;
940 /* Actually free the memory area. */
941 MmFreeMemoryArea(&Process
->Vm
,
943 MmFreeVirtualMemoryPage
,
951 NtFreeVirtualMemory(IN HANDLE ProcessHandle
,
952 IN PVOID
* PBaseAddress
,
953 IN PSIZE_T PRegionSize
,
956 * FUNCTION: Frees a range of virtual memory
958 * ProcessHandle = Points to the process that allocated the virtual
960 * BaseAddress = Points to the memory address, rounded down to a
961 * multiple of the pagesize
962 * RegionSize = Limits the range to free, rounded up to a multiple of
964 * FreeType = Can be one of the values: MEM_DECOMMIT, or MEM_RELEASE
968 MEMORY_AREA
* MemoryArea
;
971 PMMSUPPORT AddressSpace
;
977 DPRINT("NtFreeVirtualMemory(ProcessHandle %x, *PBaseAddress %x, "
978 "*PRegionSize %x, FreeType %x)\n",ProcessHandle
,*PBaseAddress
,
979 *PRegionSize
,FreeType
);
981 if (!(FreeType
& (MEM_RELEASE
| MEM_DECOMMIT
)))
983 DPRINT1("Invalid FreeType\n");
984 return STATUS_INVALID_PARAMETER_4
;
987 if (ExGetPreviousMode() != KernelMode
)
991 /* Probe user pointers */
992 ProbeForWriteSize_t(PRegionSize
);
993 ProbeForWritePointer(PBaseAddress
);
995 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
997 /* Return the exception code */
998 _SEH2_YIELD(return _SEH2_GetExceptionCode());
1003 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN((*PBaseAddress
));
1004 RegionSize
= PAGE_ROUND_UP((ULONG_PTR
)(*PBaseAddress
) + (*PRegionSize
)) -
1005 PAGE_ROUND_DOWN((*PBaseAddress
));
1007 Status
= ObReferenceObjectByHandle(ProcessHandle
,
1008 PROCESS_VM_OPERATION
,
1013 if (!NT_SUCCESS(Status
))
1018 AddressSpace
= &Process
->Vm
;
1020 MmLockAddressSpace(AddressSpace
);
1021 MemoryArea
= MmLocateMemoryAreaByAddress(AddressSpace
, BaseAddress
);
1022 if (MemoryArea
== NULL
)
1024 Status
= STATUS_UNSUCCESSFUL
;
1025 goto unlock_deref_and_return
;
1031 /* We can only free a memory area in one step. */
1032 if (MemoryArea
->StartingAddress
!= BaseAddress
||
1033 MemoryArea
->Type
!= MEMORY_AREA_VIRTUAL_MEMORY
)
1035 Status
= STATUS_UNSUCCESSFUL
;
1036 goto unlock_deref_and_return
;
1039 MmFreeVirtualMemory(Process
, MemoryArea
);
1040 Status
= STATUS_SUCCESS
;
1041 goto unlock_deref_and_return
;
1045 MmAlterRegion(AddressSpace
,
1046 MemoryArea
->StartingAddress
,
1047 (MemoryArea
->Type
== MEMORY_AREA_SECTION_VIEW
) ?
1048 &MemoryArea
->Data
.SectionData
.RegionListHead
:
1049 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
1054 MmModifyAttributes
);
1055 goto unlock_deref_and_return
;
1058 Status
= STATUS_NOT_IMPLEMENTED
;
1060 unlock_deref_and_return
:
1062 MmUnlockAddressSpace(AddressSpace
);
1063 ObDereferenceObject(Process
);
1070 MmProtectAnonMem(PMMSUPPORT AddressSpace
,
1071 PMEMORY_AREA MemoryArea
,
1078 NTSTATUS Status
= STATUS_SUCCESS
;
1079 ULONG LengthCount
= 0;
1081 /* Search all Regions in MemoryArea up to Length */
1082 /* Every Region up to Length must be committed for success */
1085 Region
= MmFindRegion(MemoryArea
->StartingAddress
,
1086 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
1087 (PVOID
)((ULONG_PTR
)BaseAddress
+ (ULONG_PTR
)LengthCount
), NULL
);
1089 /* If a Region was found and it is committed */
1090 if ((Region
) && (Region
->Type
== MEM_COMMIT
))
1092 LengthCount
+= Region
->Length
;
1093 if (Length
<= LengthCount
) break;
1096 /* If Region was found and it is not commited */
1099 Status
= STATUS_NOT_COMMITTED
;
1102 /* If no Region was found at all */
1103 else if (LengthCount
== 0)
1105 Status
= STATUS_INVALID_ADDRESS
;
1110 if (NT_SUCCESS(Status
))
1112 *OldProtect
= Region
->Protect
;
1113 Status
= MmAlterRegion(AddressSpace
, MemoryArea
->StartingAddress
,
1114 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
1115 BaseAddress
, Length
, Region
->Type
, Protect
,
1116 MmModifyAttributes
);
1123 MmQueryAnonMem(PMEMORY_AREA MemoryArea
,
1125 PMEMORY_BASIC_INFORMATION Info
,
1126 PSIZE_T ResultLength
)
1129 PVOID RegionBase
= NULL
;
1131 Info
->BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(Address
);
1133 Region
= MmFindRegion(MemoryArea
->StartingAddress
,
1134 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
1135 Address
, &RegionBase
);
1136 Info
->BaseAddress
= RegionBase
;
1137 Info
->AllocationBase
= MemoryArea
->StartingAddress
;
1138 Info
->AllocationProtect
= MemoryArea
->Protect
;
1139 Info
->RegionSize
= Region
->Length
;
1140 Info
->State
= Region
->Type
;
1141 Info
->Protect
= Region
->Protect
;
1142 Info
->Type
= MEM_PRIVATE
;
1144 *ResultLength
= sizeof(MEMORY_BASIC_INFORMATION
);
1145 return(STATUS_SUCCESS
);