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(PMM_AVL_TABLE 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(PMM_AVL_TABLE 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(PMM_AVL_TABLE 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
))
271 MmLockPage(MmGetPfnForProcess(NULL
, Address
));
273 return(STATUS_SUCCESS
);
277 * Check for the virtual memory area being deleted.
279 if (MemoryArea
->DeleteInProgress
)
281 return(STATUS_UNSUCCESSFUL
);
285 * Get the segment corresponding to the virtual address
287 Region
= MmFindRegion(MemoryArea
->StartingAddress
,
288 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
290 if (Region
->Type
== MEM_RESERVE
|| Region
->Protect
== PAGE_NOACCESS
)
292 return(STATUS_ACCESS_VIOLATION
);
296 * Get or create a page operation
298 PageOp
= MmGetPageOp(MemoryArea
, Process
->UniqueProcessId
,
299 (PVOID
)PAGE_ROUND_DOWN(Address
), NULL
, 0,
300 MM_PAGEOP_PAGEIN
, FALSE
);
303 DPRINT1("MmGetPageOp failed");
304 KeBugCheck(MEMORY_MANAGEMENT
);
308 * Check if someone else is already handling this fault, if so wait
311 if (PageOp
->Thread
!= PsGetCurrentThread())
313 MmUnlockAddressSpace(AddressSpace
);
314 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
320 * Check for various strange conditions
322 if (Status
!= STATUS_SUCCESS
)
324 DPRINT1("Failed to wait for page op\n");
325 KeBugCheck(MEMORY_MANAGEMENT
);
327 if (PageOp
->Status
== STATUS_PENDING
)
329 DPRINT1("Woke for page op before completion\n");
330 KeBugCheck(MEMORY_MANAGEMENT
);
333 * If this wasn't a pagein then we need to restart the handling
335 if (PageOp
->OpType
!= MM_PAGEOP_PAGEIN
)
337 MmLockAddressSpace(AddressSpace
);
338 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
339 MmReleasePageOp(PageOp
);
340 return(STATUS_MM_RESTART_OPERATION
);
343 * If the thread handling this fault has failed then we don't retry
345 if (!NT_SUCCESS(PageOp
->Status
))
347 MmLockAddressSpace(AddressSpace
);
348 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
349 Status
= PageOp
->Status
;
350 MmReleasePageOp(PageOp
);
353 MmLockAddressSpace(AddressSpace
);
356 MmLockPage(MmGetPfnForProcess(NULL
, Address
));
358 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
359 MmReleasePageOp(PageOp
);
360 return(STATUS_SUCCESS
);
364 * Try to allocate a page
366 Status
= MmRequestPageMemoryConsumer(MC_USER
, FALSE
, &Page
);
367 if (Status
== STATUS_NO_MEMORY
)
369 MmUnlockAddressSpace(AddressSpace
);
370 Status
= MmRequestPageMemoryConsumer(MC_USER
, TRUE
, &Page
);
371 MmLockAddressSpace(AddressSpace
);
373 if (!NT_SUCCESS(Status
))
375 DPRINT1("MmRequestPageMemoryConsumer failed, status = %x\n", Status
);
376 KeBugCheck(MEMORY_MANAGEMENT
);
380 * Handle swapped out pages.
382 if (MmIsPageSwapEntry(NULL
, Address
))
386 MmDeletePageFileMapping(Process
, Address
, &SwapEntry
);
387 Status
= MmReadFromSwapPage(SwapEntry
, Page
);
388 if (!NT_SUCCESS(Status
))
390 KeBugCheck(MEMORY_MANAGEMENT
);
392 MmSetSavedSwapEntryPage(Page
, SwapEntry
);
396 * Set the page. If we fail because we are out of memory then
399 Status
= MmCreateVirtualMapping(Process
,
400 (PVOID
)PAGE_ROUND_DOWN(Address
),
404 while (Status
== STATUS_NO_MEMORY
)
406 MmUnlockAddressSpace(AddressSpace
);
407 Status
= MmCreateVirtualMapping(Process
,
412 MmLockAddressSpace(AddressSpace
);
414 if (!NT_SUCCESS(Status
))
416 DPRINT1("MmCreateVirtualMapping failed, not out of memory\n");
417 KeBugCheck(MEMORY_MANAGEMENT
);
422 * Add the page to the process's working set
424 MmInsertRmap(Page
, Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
427 * Finish the operation
433 PageOp
->Status
= STATUS_SUCCESS
;
434 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
435 MmReleasePageOp(PageOp
);
436 return(STATUS_SUCCESS
);
440 MmModifyAttributes(PMM_AVL_TABLE AddressSpace
,
448 * FUNCTION: Modify the attributes of a memory region
451 PEPROCESS Process
= MmGetAddressSpaceOwner(AddressSpace
);
454 * If we are switching a previously committed region to reserved then
455 * free any allocated pages within the region
457 if (NewType
== MEM_RESERVE
&& OldType
== MEM_COMMIT
)
461 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
465 if (MmIsPageSwapEntry(Process
,
466 (char*)BaseAddress
+ (i
* PAGE_SIZE
)))
470 MmDeletePageFileMapping(Process
,
471 (char*)BaseAddress
+ (i
* PAGE_SIZE
),
473 MmFreeSwapPage(SwapEntry
);
477 MmDeleteVirtualMapping(Process
,
478 (char*)BaseAddress
+ (i
*PAGE_SIZE
),
482 SWAPENTRY SavedSwapEntry
;
483 SavedSwapEntry
= MmGetSavedSwapEntryPage(Page
);
484 if (SavedSwapEntry
!= 0)
486 MmFreeSwapPage(SavedSwapEntry
);
487 MmSetSavedSwapEntryPage(Page
, 0);
489 MmDeleteRmap(Page
, Process
,
490 (char*)BaseAddress
+ (i
* PAGE_SIZE
));
491 MmReleasePageMemoryConsumer(MC_USER
, Page
);
498 * If we are changing the protection attributes of a committed region then
499 * alter the attributes for any allocated pages within the region
501 if (NewType
== MEM_COMMIT
&& OldType
== MEM_COMMIT
&&
502 OldProtect
!= NewProtect
)
506 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
508 if (MmIsPagePresent(Process
,
509 (char*)BaseAddress
+ (i
*PAGE_SIZE
)))
511 MmSetPageProtect(Process
,
512 (char*)BaseAddress
+ (i
*PAGE_SIZE
),
523 NtAllocateVirtualMemory(IN HANDLE ProcessHandle
,
524 IN OUT PVOID
* UBaseAddress
,
525 IN ULONG_PTR ZeroBits
,
526 IN OUT PSIZE_T URegionSize
,
527 IN ULONG AllocationType
,
530 * FUNCTION: Allocates a block of virtual memory in the process address space
532 * ProcessHandle = The handle of the process which owns the virtual memory
533 * BaseAddress = A pointer to the virtual memory allocated. If you
534 * supply a non zero value the system will try to
535 * allocate the memory at the address supplied. It round
536 * it down to a multiple of the page size.
537 * ZeroBits = (OPTIONAL) You can specify the number of high order bits
538 * that must be zero, ensuring that the memory will be
539 * allocated at a address below a certain value.
540 * RegionSize = The number of bytes to allocate
541 * AllocationType = Indicates the type of virtual memory you like to
542 * allocated, can be a combination of MEM_COMMIT,
543 * MEM_RESERVE, MEM_RESET, MEM_TOP_DOWN.
544 * Protect = Indicates the protection type of the pages allocated, can be
545 * a combination of PAGE_READONLY, PAGE_READWRITE,
546 * PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_GUARD,
552 MEMORY_AREA
* MemoryArea
;
553 ULONG_PTR MemoryAreaLength
;
556 PMM_AVL_TABLE AddressSpace
;
561 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
562 KPROCESSOR_MODE PreviousMode
;
566 DPRINT("NtAllocateVirtualMemory(*UBaseAddress %x, "
567 "ZeroBits %d, *URegionSize %x, AllocationType %x, Protect %x)\n",
568 *UBaseAddress
,ZeroBits
,*URegionSize
,AllocationType
,
571 /* Check for valid protection flags */
572 if ((Protect
& PAGE_FLAGS_VALID_FROM_USER_MODE
) != Protect
)
574 DPRINT1("Invalid page protection\n");
575 return STATUS_INVALID_PAGE_PROTECTION
;
578 /* Check for valid Zero bits */
581 DPRINT1("Too many zero bits\n");
582 return STATUS_INVALID_PARAMETER_3
;
585 /* Check for valid Allocation Types */
586 if ((AllocationType
& ~(MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
| MEM_PHYSICAL
|
587 MEM_TOP_DOWN
| MEM_WRITE_WATCH
)))
589 DPRINT1("Invalid Allocation Type\n");
590 return STATUS_INVALID_PARAMETER_5
;
593 /* Check for at least one of these Allocation Types to be set */
594 if (!(AllocationType
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)))
596 DPRINT1("No memory allocation base type\n");
597 return STATUS_INVALID_PARAMETER_5
;
600 /* MEM_RESET is an exclusive flag, make sure that is valid too */
601 if ((AllocationType
& MEM_RESET
) && (AllocationType
!= MEM_RESET
))
603 DPRINT1("Invalid use of MEM_RESET\n");
604 return STATUS_INVALID_PARAMETER_5
;
607 /* MEM_WRITE_WATCH can only be used if MEM_RESERVE is also used */
608 if ((AllocationType
& MEM_WRITE_WATCH
) && !(AllocationType
& MEM_RESERVE
))
610 DPRINT1("MEM_WRITE_WATCH used without MEM_RESERVE\n");
611 return STATUS_INVALID_PARAMETER_5
;
614 /* MEM_PHYSICAL can only be used with MEM_RESERVE, and can only be R/W */
615 if (AllocationType
& MEM_PHYSICAL
)
617 /* First check for MEM_RESERVE exclusivity */
618 if (AllocationType
!= (MEM_RESERVE
| MEM_PHYSICAL
))
620 DPRINT1("MEM_PHYSICAL used with other flags then MEM_RESERVE or"
621 "MEM_RESERVE was not present at all\n");
622 return STATUS_INVALID_PARAMETER_5
;
625 /* Then make sure PAGE_READWRITE is used */
626 if (Protect
!= PAGE_READWRITE
)
628 DPRINT1("MEM_PHYSICAL used without PAGE_READWRITE\n");
629 return STATUS_INVALID_PAGE_PROTECTION
;
633 PreviousMode
= KeGetPreviousMode();
637 if (PreviousMode
!= KernelMode
)
639 ProbeForWritePointer(UBaseAddress
);
640 ProbeForWriteUlong(URegionSize
);
642 PBaseAddress
= *UBaseAddress
;
643 PRegionSize
= *URegionSize
;
645 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
647 /* Get the exception code */
648 Status
= _SEH2_GetExceptionCode();
649 _SEH2_YIELD(return Status
);
653 BoundaryAddressMultiple
.QuadPart
= 0;
655 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(PBaseAddress
);
656 RegionSize
= PAGE_ROUND_UP((ULONG_PTR
)PBaseAddress
+ PRegionSize
) -
657 PAGE_ROUND_DOWN(PBaseAddress
);
660 * We've captured and calculated the data, now do more checks
661 * Yes, MmCreateMemoryArea does similar checks, but they don't return
662 * the right status codes that a caller of this routine would expect.
664 if ((ULONG_PTR
)BaseAddress
>= USER_SHARED_DATA
)
666 DPRINT1("Virtual allocation base above User Space\n");
667 return STATUS_INVALID_PARAMETER_2
;
671 DPRINT1("Region size is invalid (zero)\n");
672 return STATUS_INVALID_PARAMETER_4
;
674 if ((USER_SHARED_DATA
- (ULONG_PTR
)BaseAddress
) < RegionSize
)
676 DPRINT1("Region size would overflow into kernel-memory\n");
677 return STATUS_INVALID_PARAMETER_4
;
681 * Copy on Write is reserved for system use. This case is a certain failure
682 * but there may be other cases...needs more testing
684 if ((!BaseAddress
|| (AllocationType
& MEM_RESERVE
)) &&
685 (Protect
& (PAGE_WRITECOPY
| PAGE_EXECUTE_WRITECOPY
)))
687 DPRINT1("Copy on write is not supported by VirtualAlloc\n");
688 return STATUS_INVALID_PAGE_PROTECTION
;
692 Status
= ObReferenceObjectByHandle(ProcessHandle
,
693 PROCESS_VM_OPERATION
,
698 if (!NT_SUCCESS(Status
))
700 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
704 Type
= (AllocationType
& MEM_COMMIT
) ? MEM_COMMIT
: MEM_RESERVE
;
705 DPRINT("Type %x\n", Type
);
707 AddressSpace
= &Process
->VadRoot
;
708 MmLockAddressSpace(AddressSpace
);
710 if (PBaseAddress
!= 0)
712 MemoryArea
= MmLocateMemoryAreaByAddress(AddressSpace
, BaseAddress
);
714 if (MemoryArea
!= NULL
)
716 MemoryAreaLength
= (ULONG_PTR
)MemoryArea
->EndingAddress
-
717 (ULONG_PTR
)MemoryArea
->StartingAddress
;
718 if (MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
&&
719 MemoryAreaLength
>= RegionSize
)
722 MmAlterRegion(AddressSpace
,
723 MemoryArea
->StartingAddress
,
724 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
725 BaseAddress
, RegionSize
,
726 Type
, Protect
, MmModifyAttributes
);
727 MmUnlockAddressSpace(AddressSpace
);
728 ObDereferenceObject(Process
);
729 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
731 /* Give the caller rounded BaseAddress and area length */
732 if (NT_SUCCESS(Status
))
734 *UBaseAddress
= BaseAddress
;
735 *URegionSize
= RegionSize
;
736 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
741 else if (MemoryAreaLength
>= RegionSize
)
743 /* Region list initialized? */
744 if (MemoryArea
->Data
.SectionData
.RegionListHead
.Flink
)
747 MmAlterRegion(AddressSpace
,
748 MemoryArea
->StartingAddress
,
749 &MemoryArea
->Data
.SectionData
.RegionListHead
,
750 BaseAddress
, RegionSize
,
751 Type
, Protect
, MmModifyAttributes
);
755 Status
= STATUS_ACCESS_VIOLATION
;
758 MmUnlockAddressSpace(AddressSpace
);
759 ObDereferenceObject(Process
);
760 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
762 /* Give the caller rounded BaseAddress and area length */
763 if (NT_SUCCESS(Status
))
765 *UBaseAddress
= BaseAddress
;
766 *URegionSize
= RegionSize
;
767 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
774 MmUnlockAddressSpace(AddressSpace
);
775 ObDereferenceObject(Process
);
776 return(STATUS_UNSUCCESSFUL
);
781 Status
= MmCreateMemoryArea(AddressSpace
,
782 MEMORY_AREA_VIRTUAL_MEMORY
,
788 AllocationType
& MEM_TOP_DOWN
,
789 BoundaryAddressMultiple
);
790 if (!NT_SUCCESS(Status
))
792 MmUnlockAddressSpace(AddressSpace
);
793 ObDereferenceObject(Process
);
794 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
798 MemoryAreaLength
= (ULONG_PTR
)MemoryArea
->EndingAddress
-
799 (ULONG_PTR
)MemoryArea
->StartingAddress
;
801 MmInitializeRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
802 MemoryAreaLength
, Type
, Protect
);
804 if ((AllocationType
& MEM_COMMIT
) &&
805 (Protect
& (PAGE_READWRITE
| PAGE_EXECUTE_READWRITE
)))
807 const ULONG nPages
= PAGE_ROUND_UP(MemoryAreaLength
) >> PAGE_SHIFT
;
808 MmReserveSwapPages(nPages
);
811 *UBaseAddress
= BaseAddress
;
812 *URegionSize
= MemoryAreaLength
;
813 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
815 MmUnlockAddressSpace(AddressSpace
);
816 ObDereferenceObject(Process
);
817 return(STATUS_SUCCESS
);
821 MmFreeVirtualMemoryPage(PVOID Context
,
822 MEMORY_AREA
* MemoryArea
,
828 PEPROCESS Process
= (PEPROCESS
)Context
;
832 SWAPENTRY SavedSwapEntry
;
833 SavedSwapEntry
= MmGetSavedSwapEntryPage(Page
);
834 if (SavedSwapEntry
!= 0)
836 MmFreeSwapPage(SavedSwapEntry
);
837 MmSetSavedSwapEntryPage(Page
, 0);
839 MmDeleteRmap(Page
, Process
, Address
);
840 MmReleasePageMemoryConsumer(MC_USER
, Page
);
842 else if (SwapEntry
!= 0)
844 MmFreeSwapPage(SwapEntry
);
850 MmFreeVirtualMemory(PEPROCESS Process
,
851 PMEMORY_AREA MemoryArea
)
853 PLIST_ENTRY current_entry
;
857 DPRINT("MmFreeVirtualMemory(Process %p MemoryArea %p)\n", Process
,
860 /* Mark this memory area as about to be deleted. */
861 MemoryArea
->DeleteInProgress
= TRUE
;
864 * Wait for any ongoing paging operations. Notice that since we have
865 * flagged this memory area as deleted no more page ops will be added.
867 if (MemoryArea
->PageOpCount
> 0)
869 ULONG_PTR MemoryAreaLength
= (ULONG_PTR
)MemoryArea
->EndingAddress
-
870 (ULONG_PTR
)MemoryArea
->StartingAddress
;
871 const ULONG nPages
= PAGE_ROUND_UP(MemoryAreaLength
) >> PAGE_SHIFT
;
873 for (i
= 0; i
< nPages
&& MemoryArea
->PageOpCount
!= 0; ++i
)
876 PageOp
= MmCheckForPageOp(MemoryArea
, Process
->UniqueProcessId
,
877 (PVOID
)((ULONG_PTR
)MemoryArea
->StartingAddress
+ (i
* PAGE_SIZE
)),
882 MmUnlockAddressSpace(&Process
->VadRoot
);
883 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
888 if (Status
!= STATUS_SUCCESS
)
890 DPRINT1("Failed to wait for page op\n");
891 KeBugCheck(MEMORY_MANAGEMENT
);
893 MmLockAddressSpace(&Process
->VadRoot
);
894 MmReleasePageOp(PageOp
);
899 /* Free all the individual segments. */
900 current_entry
= MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
.Flink
;
901 while (current_entry
!= &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
)
903 current
= CONTAINING_RECORD(current_entry
, MM_REGION
, RegionListEntry
);
904 current_entry
= current_entry
->Flink
;
908 /* Actually free the memory area. */
909 MmFreeMemoryArea(&Process
->VadRoot
,
911 MmFreeVirtualMemoryPage
,
919 NtFreeVirtualMemory(IN HANDLE ProcessHandle
,
920 IN PVOID
* PBaseAddress
,
921 IN PSIZE_T PRegionSize
,
924 * FUNCTION: Frees a range of virtual memory
926 * ProcessHandle = Points to the process that allocated the virtual
928 * BaseAddress = Points to the memory address, rounded down to a
929 * multiple of the pagesize
930 * RegionSize = Limits the range to free, rounded up to a multiple of
932 * FreeType = Can be one of the values: MEM_DECOMMIT, or MEM_RELEASE
936 MEMORY_AREA
* MemoryArea
;
939 PMM_AVL_TABLE AddressSpace
;
943 DPRINT("NtFreeVirtualMemory(ProcessHandle %x, *PBaseAddress %x, "
944 "*PRegionSize %x, FreeType %x)\n",ProcessHandle
,*PBaseAddress
,
945 *PRegionSize
,FreeType
);
947 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN((*PBaseAddress
));
948 RegionSize
= PAGE_ROUND_UP((ULONG_PTR
)(*PBaseAddress
) + (*PRegionSize
)) -
949 PAGE_ROUND_DOWN((*PBaseAddress
));
951 Status
= ObReferenceObjectByHandle(ProcessHandle
,
952 PROCESS_VM_OPERATION
,
957 if (!NT_SUCCESS(Status
))
962 AddressSpace
= &Process
->VadRoot
;
964 MmLockAddressSpace(AddressSpace
);
965 MemoryArea
= MmLocateMemoryAreaByAddress(AddressSpace
, BaseAddress
);
966 if (MemoryArea
== NULL
)
968 Status
= STATUS_UNSUCCESSFUL
;
969 goto unlock_deref_and_return
;
975 /* We can only free a memory area in one step. */
976 if (MemoryArea
->StartingAddress
!= BaseAddress
||
977 MemoryArea
->Type
!= MEMORY_AREA_VIRTUAL_MEMORY
)
979 Status
= STATUS_UNSUCCESSFUL
;
980 goto unlock_deref_and_return
;
983 MmFreeVirtualMemory(Process
, MemoryArea
);
984 Status
= STATUS_SUCCESS
;
985 goto unlock_deref_and_return
;
989 MmAlterRegion(AddressSpace
,
990 MemoryArea
->StartingAddress
,
991 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
997 goto unlock_deref_and_return
;
1000 Status
= STATUS_NOT_IMPLEMENTED
;
1002 unlock_deref_and_return
:
1004 MmUnlockAddressSpace(AddressSpace
);
1005 ObDereferenceObject(Process
);
1012 MmProtectAnonMem(PMM_AVL_TABLE AddressSpace
,
1013 PMEMORY_AREA MemoryArea
,
1022 Region
= MmFindRegion(MemoryArea
->StartingAddress
,
1023 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
1025 *OldProtect
= Region
->Protect
;
1026 Status
= MmAlterRegion(AddressSpace
, MemoryArea
->StartingAddress
,
1027 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
1028 BaseAddress
, Length
, Region
->Type
, Protect
,
1029 MmModifyAttributes
);
1034 MmQueryAnonMem(PMEMORY_AREA MemoryArea
,
1036 PMEMORY_BASIC_INFORMATION Info
,
1037 PSIZE_T ResultLength
)
1040 PVOID RegionBase
= NULL
;
1042 Info
->BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(Address
);
1044 Region
= MmFindRegion(MemoryArea
->StartingAddress
,
1045 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
1046 Address
, &RegionBase
);
1047 Info
->BaseAddress
= RegionBase
;
1048 Info
->AllocationBase
= MemoryArea
->StartingAddress
;
1049 Info
->AllocationProtect
= MemoryArea
->Protect
;
1050 Info
->RegionSize
= Region
->Length
;
1051 Info
->State
= Region
->Type
;
1052 Info
->Protect
= Region
->Protect
;
1053 Info
->Type
= MEM_PRIVATE
;
1055 *ResultLength
= sizeof(MEMORY_BASIC_INFORMATION
);
1056 return(STATUS_SUCCESS
);