3 * Copyright (C) 2002-2005 ReactOS Team (and the authors from the programmers section)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * PROJECT: ReactOS kernel
21 * FILE: ntoskrnl/mm/anonmem.c
22 * PURPOSE: Implementing anonymous memory.
24 * PROGRAMMERS: David Welch
33 * Gunnar Andre' Dalsnes
35 * Thomas Weidenmueller
41 /* INCLUDE *****************************************************************/
45 #include <internal/debug.h>
47 /* FUNCTIONS *****************************************************************/
51 MmWritePageVirtualMemory(PMADDRESS_SPACE AddressSpace
,
52 PMEMORY_AREA MemoryArea
,
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(AddressSpace
->Process
, Address
);
74 * Get that the page actually is dirty.
76 if (!MmIsDirtyPage(AddressSpace
->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(AddressSpace
->Process
, Address
);
90 * If necessary, allocate an entry in the paging file for this page
92 SwapEntry
= MmGetSavedSwapEntryPage(Page
);
95 SwapEntry
= MmAllocSwapPage();
98 MmSetDirtyPage(AddressSpace
->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(AddressSpace
->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(PMADDRESS_SPACE AddressSpace
,
134 PMEMORY_AREA MemoryArea
,
143 DPRINT("MmPageOutVirtualMemory(Address 0x%.8X) PID %d\n",
144 Address
, AddressSpace
->Process
->UniqueProcessId
);
147 * Check for paging out from a deleted virtual memory area.
149 if (MemoryArea
->DeleteInProgress
)
151 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
152 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
153 MmReleasePageOp(PageOp
);
154 return(STATUS_UNSUCCESSFUL
);
158 * Disable the virtual mapping.
160 MmDisableVirtualMapping(AddressSpace
->Process
, Address
,
169 * Paging out non-dirty data is easy.
173 MmLockAddressSpace(AddressSpace
);
174 MmDeleteVirtualMapping(AddressSpace
->Process
, Address
, FALSE
, NULL
, NULL
);
175 MmDeleteAllRmaps(Page
, NULL
, NULL
);
176 if ((SwapEntry
= MmGetSavedSwapEntryPage(Page
)) != 0)
178 MmCreatePageFileMapping(AddressSpace
->Process
, Address
, SwapEntry
);
179 MmSetSavedSwapEntryPage(Page
, 0);
181 MmUnlockAddressSpace(AddressSpace
);
182 MmReleasePageMemoryConsumer(MC_USER
, Page
);
183 PageOp
->Status
= STATUS_SUCCESS
;
184 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
185 MmReleasePageOp(PageOp
);
186 return(STATUS_SUCCESS
);
190 * If necessary, allocate an entry in the paging file for this page
192 SwapEntry
= MmGetSavedSwapEntryPage(Page
);
195 SwapEntry
= MmAllocSwapPage();
198 MmShowOutOfSpaceMessagePagingFile();
199 MmEnableVirtualMapping(AddressSpace
->Process
, Address
);
200 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
201 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
202 MmReleasePageOp(PageOp
);
203 return(STATUS_PAGEFILE_QUOTA
);
208 * Write the page to the pagefile
210 Status
= MmWriteToSwapPage(SwapEntry
, Page
);
211 if (!NT_SUCCESS(Status
))
213 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
215 MmEnableVirtualMapping(AddressSpace
->Process
, Address
);
216 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
217 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
218 MmReleasePageOp(PageOp
);
219 return(STATUS_UNSUCCESSFUL
);
223 * Otherwise we have succeeded, free the page
225 DPRINT("MM: Swapped out virtual memory page 0x%.8X!\n", Page
<< PAGE_SHIFT
);
226 MmLockAddressSpace(AddressSpace
);
227 MmDeleteVirtualMapping(AddressSpace
->Process
, Address
, FALSE
, NULL
, NULL
);
228 MmCreatePageFileMapping(AddressSpace
->Process
, Address
, SwapEntry
);
229 MmUnlockAddressSpace(AddressSpace
);
230 MmDeleteAllRmaps(Page
, NULL
, NULL
);
231 MmSetSavedSwapEntryPage(Page
, 0);
232 MmReleasePageMemoryConsumer(MC_USER
, Page
);
233 PageOp
->Status
= STATUS_SUCCESS
;
234 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
235 MmReleasePageOp(PageOp
);
236 return(STATUS_SUCCESS
);
241 MmNotPresentFaultVirtualMemory(PMADDRESS_SPACE AddressSpace
,
242 MEMORY_AREA
* MemoryArea
,
246 * FUNCTION: Move data into memory to satisfy a page not present fault
248 * AddressSpace = Address space within which the fault occurred
249 * MemoryArea = The memory area within which the fault occurred
250 * Address = The absolute address of fault
252 * NOTES: This function is called with the address space lock held.
261 * There is a window between taking the page fault and locking the
262 * address space when another thread could load the page so we check
265 if (MmIsPagePresent(NULL
, Address
))
269 MmLockPage(MmGetPfnForProcess(NULL
, Address
));
271 return(STATUS_SUCCESS
);
275 * Check for the virtual memory area being deleted.
277 if (MemoryArea
->DeleteInProgress
)
279 return(STATUS_UNSUCCESSFUL
);
283 * Get the segment corresponding to the virtual address
285 Region
= MmFindRegion(MemoryArea
->StartingAddress
,
286 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
288 if (Region
->Type
== MEM_RESERVE
|| Region
->Protect
== PAGE_NOACCESS
)
290 return(STATUS_ACCESS_VIOLATION
);
294 * Get or create a page operation
296 PageOp
= MmGetPageOp(MemoryArea
, AddressSpace
->Process
->UniqueProcessId
,
297 (PVOID
)PAGE_ROUND_DOWN(Address
), NULL
, 0,
298 MM_PAGEOP_PAGEIN
, FALSE
);
301 DPRINT1("MmGetPageOp failed");
306 * Check if someone else is already handling this fault, if so wait
309 if (PageOp
->Thread
!= PsGetCurrentThread())
311 MmUnlockAddressSpace(AddressSpace
);
312 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
318 * Check for various strange conditions
320 if (Status
!= STATUS_SUCCESS
)
322 DPRINT1("Failed to wait for page op\n");
325 if (PageOp
->Status
== STATUS_PENDING
)
327 DPRINT1("Woke for page op before completion\n");
331 * If this wasn't a pagein then we need to restart the handling
333 if (PageOp
->OpType
!= MM_PAGEOP_PAGEIN
)
335 MmLockAddressSpace(AddressSpace
);
336 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
337 MmReleasePageOp(PageOp
);
338 return(STATUS_MM_RESTART_OPERATION
);
341 * If the thread handling this fault has failed then we don't retry
343 if (!NT_SUCCESS(PageOp
->Status
))
345 MmLockAddressSpace(AddressSpace
);
346 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
347 Status
= PageOp
->Status
;
348 MmReleasePageOp(PageOp
);
351 MmLockAddressSpace(AddressSpace
);
354 MmLockPage(MmGetPfnForProcess(NULL
, Address
));
356 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
357 MmReleasePageOp(PageOp
);
358 return(STATUS_SUCCESS
);
362 * Try to allocate a page
364 Status
= MmRequestPageMemoryConsumer(MC_USER
, FALSE
, &Page
);
365 if (Status
== STATUS_NO_MEMORY
)
367 MmUnlockAddressSpace(AddressSpace
);
368 Status
= MmRequestPageMemoryConsumer(MC_USER
, TRUE
, &Page
);
369 MmLockAddressSpace(AddressSpace
);
371 if (!NT_SUCCESS(Status
))
373 DPRINT1("MmRequestPageMemoryConsumer failed, status = %x\n", Status
);
378 * Handle swapped out pages.
380 if (MmIsPageSwapEntry(NULL
, Address
))
384 MmDeletePageFileMapping(AddressSpace
->Process
, Address
, &SwapEntry
);
385 Status
= MmReadFromSwapPage(SwapEntry
, Page
);
386 if (!NT_SUCCESS(Status
))
390 MmSetSavedSwapEntryPage(Page
, SwapEntry
);
394 * Set the page. If we fail because we are out of memory then
397 Status
= MmCreateVirtualMapping(AddressSpace
->Process
,
398 (PVOID
)PAGE_ROUND_DOWN(Address
),
402 while (Status
== STATUS_NO_MEMORY
)
404 MmUnlockAddressSpace(AddressSpace
);
405 Status
= MmCreateVirtualMapping(AddressSpace
->Process
,
410 MmLockAddressSpace(AddressSpace
);
412 if (!NT_SUCCESS(Status
))
414 DPRINT1("MmCreateVirtualMapping failed, not out of memory\n");
420 * Add the page to the process's working set
422 MmInsertRmap(Page
, AddressSpace
->Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
425 * Finish the operation
431 PageOp
->Status
= STATUS_SUCCESS
;
432 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
433 MmReleasePageOp(PageOp
);
434 return(STATUS_SUCCESS
);
438 MmModifyAttributes(PMADDRESS_SPACE AddressSpace
,
446 * FUNCTION: Modify the attributes of a memory region
450 * If we are switching a previously committed region to reserved then
451 * free any allocated pages within the region
453 if (NewType
== MEM_RESERVE
&& OldType
== MEM_COMMIT
)
457 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
461 if (MmIsPageSwapEntry(AddressSpace
->Process
,
462 (char*)BaseAddress
+ (i
* PAGE_SIZE
)))
466 MmDeletePageFileMapping(AddressSpace
->Process
,
467 (char*)BaseAddress
+ (i
* PAGE_SIZE
),
469 MmFreeSwapPage(SwapEntry
);
473 MmDeleteVirtualMapping(AddressSpace
->Process
,
474 (char*)BaseAddress
+ (i
*PAGE_SIZE
),
478 SWAPENTRY SavedSwapEntry
;
479 SavedSwapEntry
= MmGetSavedSwapEntryPage(Page
);
480 if (SavedSwapEntry
!= 0)
482 MmFreeSwapPage(SavedSwapEntry
);
483 MmSetSavedSwapEntryPage(Page
, 0);
485 MmDeleteRmap(Page
, AddressSpace
->Process
,
486 (char*)BaseAddress
+ (i
* PAGE_SIZE
));
487 MmReleasePageMemoryConsumer(MC_USER
, Page
);
494 * If we are changing the protection attributes of a committed region then
495 * alter the attributes for any allocated pages within the region
497 if (NewType
== MEM_COMMIT
&& OldType
== MEM_COMMIT
&&
498 OldProtect
!= NewProtect
)
502 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
504 if (MmIsPagePresent(AddressSpace
->Process
,
505 (char*)BaseAddress
+ (i
*PAGE_SIZE
)))
507 MmSetPageProtect(AddressSpace
->Process
,
508 (char*)BaseAddress
+ (i
*PAGE_SIZE
),
519 NtAllocateVirtualMemory(IN HANDLE ProcessHandle
,
520 IN OUT PVOID
* UBaseAddress
,
522 IN OUT PULONG URegionSize
,
523 IN ULONG AllocationType
,
526 * FUNCTION: Allocates a block of virtual memory in the process address space
528 * ProcessHandle = The handle of the process which owns the virtual memory
529 * BaseAddress = A pointer to the virtual memory allocated. If you
530 * supply a non zero value the system will try to
531 * allocate the memory at the address supplied. It round
532 * it down to a multiple of the page size.
533 * ZeroBits = (OPTIONAL) You can specify the number of high order bits
534 * that must be zero, ensuring that the memory will be
535 * allocated at a address below a certain value.
536 * RegionSize = The number of bytes to allocate
537 * AllocationType = Indicates the type of virtual memory you like to
538 * allocated, can be a combination of MEM_COMMIT,
539 * MEM_RESERVE, MEM_RESET, MEM_TOP_DOWN.
540 * Protect = Indicates the protection type of the pages allocated, can be
541 * a combination of PAGE_READONLY, PAGE_READWRITE,
542 * PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_GUARD,
548 MEMORY_AREA
* MemoryArea
;
549 ULONG_PTR MemoryAreaLength
;
552 PMADDRESS_SPACE AddressSpace
;
557 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
559 DPRINT("NtAllocateVirtualMemory(*UBaseAddress %x, "
560 "ZeroBits %d, *URegionSize %x, AllocationType %x, Protect %x)\n",
561 *UBaseAddress
,ZeroBits
,*URegionSize
,AllocationType
,
564 /* Check for valid protection flags */
565 if ((Protect
& PAGE_FLAGS_VALID_FROM_USER_MODE
) != Protect
)
567 DPRINT1("Invalid page protection\n");
568 return STATUS_INVALID_PAGE_PROTECTION
;
571 /* Check for valid Zero bits */
574 DPRINT1("Too many zero bits\n");
575 return STATUS_INVALID_PARAMETER_3
;
578 /* Check for valid Allocation Types */
579 if ((AllocationType
&~ (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
| MEM_PHYSICAL
|
580 MEM_TOP_DOWN
| MEM_WRITE_WATCH
)))
582 DPRINT1("Invalid Allocation Type\n");
583 return STATUS_INVALID_PARAMETER_5
;
586 /* Check for at least one of these Allocation Types to be set */
587 if (!(AllocationType
& (MEM_COMMIT
| MEM_RESERVE
| MEM_RESET
)))
589 DPRINT1("No memory allocation base type\n");
590 return STATUS_INVALID_PARAMETER_5
;
593 /* MEM_RESET is an exclusive flag, make sure that is valid too */
594 if ((AllocationType
& MEM_RESET
) && (AllocationType
!= MEM_RESET
))
596 DPRINT1("MEM_RESET used illegaly\n");
597 return STATUS_INVALID_PARAMETER_5
;
600 /* MEM_WRITE_WATCH can only be used if MEM_RESERVE is also used */
601 if ((AllocationType
& MEM_WRITE_WATCH
) && !(AllocationType
& MEM_RESERVE
))
603 DPRINT1("MEM_WRITE_WATCH used without MEM_RESERVE\n");
604 return STATUS_INVALID_PARAMETER_5
;
607 /* MEM_PHYSICAL can only be used with MEM_RESERVE, and can only be R/W */
608 if (AllocationType
& MEM_PHYSICAL
)
610 /* First check for MEM_RESERVE exclusivity */
611 if (AllocationType
!= (MEM_RESERVE
| MEM_PHYSICAL
))
613 DPRINT1("MEM_PHYSICAL used with other flags then MEM_RESERVE or"
614 "MEM_RESERVE was not present at all\n");
615 return STATUS_INVALID_PARAMETER_5
;
618 /* Then make sure PAGE_READWRITE is used */
619 if (Protect
!= PAGE_READWRITE
)
621 DPRINT1("MEM_PHYSICAL used without PAGE_READWRITE\n");
622 return STATUS_INVALID_PAGE_PROTECTION
;
626 PBaseAddress
= *UBaseAddress
;
627 PRegionSize
= *URegionSize
;
628 BoundaryAddressMultiple
.QuadPart
= 0;
630 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(PBaseAddress
);
631 RegionSize
= PAGE_ROUND_UP(PBaseAddress
+ PRegionSize
) -
632 PAGE_ROUND_DOWN(PBaseAddress
);
635 * We've captured and calculated the data, now do more checks
636 * Yes, MmCreateMemoryArea does similar checks, but they don't return
637 * the right status codes that a caller of this routine would expect.
639 if (BaseAddress
>= MM_HIGHEST_USER_ADDRESS
)
641 DPRINT1("Virtual allocation above User Space\n");
642 return STATUS_INVALID_PARAMETER_2
;
646 DPRINT1("Region size is invalid\n");
647 return STATUS_INVALID_PARAMETER_4
;
649 if (((ULONG_PTR
)MM_HIGHEST_USER_ADDRESS
- (ULONG_PTR
)BaseAddress
) < RegionSize
)
651 DPRINT1("Region size would overflow into kernel-memory\n");
652 return STATUS_INVALID_PARAMETER_4
;
656 * Copy on Write is reserved for system use. This case is a certain failure
657 * but there may be other cases...needs more testing
659 if ((!BaseAddress
|| (AllocationType
& MEM_RESERVE
)) &&
660 ((Protect
& PAGE_WRITECOPY
) || (Protect
& PAGE_EXECUTE_WRITECOPY
)))
662 DPRINT1("Copy on write is not supported by VirtualAlloc\n");
663 return STATUS_INVALID_PAGE_PROTECTION
;
667 Status
= ObReferenceObjectByHandle(ProcessHandle
,
668 PROCESS_VM_OPERATION
,
673 if (!NT_SUCCESS(Status
))
675 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
679 Type
= (AllocationType
& MEM_COMMIT
) ? MEM_COMMIT
: MEM_RESERVE
;
680 DPRINT("Type %x\n", Type
);
682 AddressSpace
= (PMADDRESS_SPACE
)&Process
->VadRoot
;
683 MmLockAddressSpace(AddressSpace
);
685 if (PBaseAddress
!= 0)
687 MemoryArea
= MmLocateMemoryAreaByAddress(AddressSpace
, BaseAddress
);
689 if (MemoryArea
!= NULL
)
691 MemoryAreaLength
= (ULONG_PTR
)MemoryArea
->EndingAddress
-
692 (ULONG_PTR
)MemoryArea
->StartingAddress
;
693 if (MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
&&
694 MemoryAreaLength
>= RegionSize
)
697 MmAlterRegion(AddressSpace
,
698 MemoryArea
->StartingAddress
,
699 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
700 BaseAddress
, RegionSize
,
701 Type
, Protect
, MmModifyAttributes
);
702 MmUnlockAddressSpace(AddressSpace
);
703 ObDereferenceObject(Process
);
704 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
707 else if (MemoryAreaLength
>= RegionSize
)
710 MmAlterRegion(AddressSpace
,
711 MemoryArea
->StartingAddress
,
712 &MemoryArea
->Data
.SectionData
.RegionListHead
,
713 BaseAddress
, RegionSize
,
714 Type
, Protect
, MmModifyAttributes
);
715 MmUnlockAddressSpace(AddressSpace
);
716 ObDereferenceObject(Process
);
717 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
722 MmUnlockAddressSpace(AddressSpace
);
723 ObDereferenceObject(Process
);
724 return(STATUS_UNSUCCESSFUL
);
729 Status
= MmCreateMemoryArea(AddressSpace
,
730 MEMORY_AREA_VIRTUAL_MEMORY
,
736 AllocationType
& MEM_TOP_DOWN
,
737 BoundaryAddressMultiple
);
738 if (!NT_SUCCESS(Status
))
740 MmUnlockAddressSpace(AddressSpace
);
741 ObDereferenceObject(Process
);
742 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
746 MemoryAreaLength
= (ULONG_PTR
)MemoryArea
->EndingAddress
-
747 (ULONG_PTR
)MemoryArea
->StartingAddress
;
749 MmInitializeRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
750 MemoryAreaLength
, Type
, Protect
);
752 if ((AllocationType
& MEM_COMMIT
) &&
753 ((Protect
& PAGE_READWRITE
) ||
754 (Protect
& PAGE_EXECUTE_READWRITE
)))
756 MmReserveSwapPages(MemoryAreaLength
);
759 *UBaseAddress
= BaseAddress
;
760 *URegionSize
= MemoryAreaLength
;
761 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
763 MmUnlockAddressSpace(AddressSpace
);
764 ObDereferenceObject(Process
);
765 return(STATUS_SUCCESS
);
769 MmFreeVirtualMemoryPage(PVOID Context
,
770 MEMORY_AREA
* MemoryArea
,
776 PEPROCESS Process
= (PEPROCESS
)Context
;
780 SWAPENTRY SavedSwapEntry
;
781 SavedSwapEntry
= MmGetSavedSwapEntryPage(Page
);
782 if (SavedSwapEntry
!= 0)
784 MmFreeSwapPage(SavedSwapEntry
);
785 MmSetSavedSwapEntryPage(Page
, 0);
787 MmDeleteRmap(Page
, Process
, Address
);
788 MmReleasePageMemoryConsumer(MC_USER
, Page
);
790 else if (SwapEntry
!= 0)
792 MmFreeSwapPage(SwapEntry
);
798 MmFreeVirtualMemory(PEPROCESS Process
,
799 PMEMORY_AREA MemoryArea
)
801 PLIST_ENTRY current_entry
;
805 DPRINT("MmFreeVirtualMemory(Process %p MemoryArea %p)\n", Process
,
808 /* Mark this memory area as about to be deleted. */
809 MemoryArea
->DeleteInProgress
= TRUE
;
812 * Wait for any ongoing paging operations. Notice that since we have
813 * flagged this memory area as deleted no more page ops will be added.
815 if (MemoryArea
->PageOpCount
> 0)
817 ULONG_PTR MemoryAreaLength
= (ULONG_PTR
)MemoryArea
->EndingAddress
-
818 (ULONG_PTR
)MemoryArea
->StartingAddress
;
820 /* FiN TODO: Optimize loop counter! */
821 for (i
= 0; i
< PAGE_ROUND_UP(MemoryAreaLength
) / PAGE_SIZE
; i
++)
825 if (MemoryArea
->PageOpCount
== 0)
830 PageOp
= MmCheckForPageOp(MemoryArea
, Process
->UniqueProcessId
,
831 (PVOID
)((ULONG_PTR
)MemoryArea
->StartingAddress
+ (i
* PAGE_SIZE
)),
836 MmUnlockAddressSpace((PMADDRESS_SPACE
)&Process
->VadRoot
);
837 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
842 if (Status
!= STATUS_SUCCESS
)
844 DPRINT1("Failed to wait for page op\n");
847 MmLockAddressSpace((PMADDRESS_SPACE
)&Process
->VadRoot
);
848 MmReleasePageOp(PageOp
);
853 /* Free all the individual segments. */
854 current_entry
= MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
.Flink
;
855 while (current_entry
!= &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
)
857 current
= CONTAINING_RECORD(current_entry
, MM_REGION
, RegionListEntry
);
858 current_entry
= current_entry
->Flink
;
862 /* Actually free the memory area. */
863 MmFreeMemoryArea((PMADDRESS_SPACE
)&Process
->VadRoot
,
865 MmFreeVirtualMemoryPage
,
873 NtFreeVirtualMemory(IN HANDLE ProcessHandle
,
874 IN PVOID
* PBaseAddress
,
875 IN PULONG PRegionSize
,
878 * FUNCTION: Frees a range of virtual memory
880 * ProcessHandle = Points to the process that allocated the virtual
882 * BaseAddress = Points to the memory address, rounded down to a
883 * multiple of the pagesize
884 * RegionSize = Limits the range to free, rounded up to a multiple of
886 * FreeType = Can be one of the values: MEM_DECOMMIT, or MEM_RELEASE
890 MEMORY_AREA
* MemoryArea
;
893 PMADDRESS_SPACE AddressSpace
;
897 DPRINT("NtFreeVirtualMemory(ProcessHandle %x, *PBaseAddress %x, "
898 "*PRegionSize %x, FreeType %x)\n",ProcessHandle
,*PBaseAddress
,
899 *PRegionSize
,FreeType
);
901 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN((*PBaseAddress
));
902 RegionSize
= PAGE_ROUND_UP((*PBaseAddress
) + (*PRegionSize
)) -
903 PAGE_ROUND_DOWN((*PBaseAddress
));
905 Status
= ObReferenceObjectByHandle(ProcessHandle
,
906 PROCESS_VM_OPERATION
,
911 if (!NT_SUCCESS(Status
))
916 AddressSpace
= (PMADDRESS_SPACE
)&Process
->VadRoot
;
918 MmLockAddressSpace(AddressSpace
);
919 MemoryArea
= MmLocateMemoryAreaByAddress(AddressSpace
, BaseAddress
);
920 if (MemoryArea
== NULL
)
922 MmUnlockAddressSpace(AddressSpace
);
923 ObDereferenceObject(Process
);
924 return(STATUS_UNSUCCESSFUL
);
930 /* We can only free a memory area in one step. */
931 if (MemoryArea
->StartingAddress
!= BaseAddress
||
932 MemoryArea
->Type
!= MEMORY_AREA_VIRTUAL_MEMORY
)
934 MmUnlockAddressSpace(AddressSpace
);
935 ObDereferenceObject(Process
);
936 return(STATUS_UNSUCCESSFUL
);
938 MmFreeVirtualMemory(Process
, MemoryArea
);
939 MmUnlockAddressSpace(AddressSpace
);
940 ObDereferenceObject(Process
);
941 return(STATUS_SUCCESS
);
945 MmAlterRegion(AddressSpace
,
946 MemoryArea
->StartingAddress
,
947 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
953 MmUnlockAddressSpace(AddressSpace
);
954 ObDereferenceObject(Process
);
957 MmUnlockAddressSpace(AddressSpace
);
958 ObDereferenceObject(Process
);
959 return(STATUS_NOT_IMPLEMENTED
);
964 MmProtectAnonMem(PMADDRESS_SPACE AddressSpace
,
965 PMEMORY_AREA MemoryArea
,
974 Region
= MmFindRegion(MemoryArea
->StartingAddress
,
975 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
977 *OldProtect
= Region
->Protect
;
978 Status
= MmAlterRegion(AddressSpace
, MemoryArea
->StartingAddress
,
979 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
980 BaseAddress
, Length
, Region
->Type
, Protect
,
986 MmQueryAnonMem(PMEMORY_AREA MemoryArea
,
988 PMEMORY_BASIC_INFORMATION Info
,
992 PVOID RegionBase
= NULL
;
994 Info
->BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(Address
);
996 Region
= MmFindRegion(MemoryArea
->StartingAddress
,
997 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
998 Address
, &RegionBase
);
999 Info
->BaseAddress
= RegionBase
;
1000 Info
->AllocationBase
= MemoryArea
->StartingAddress
;
1001 Info
->AllocationProtect
= MemoryArea
->Protect
;
1002 Info
->RegionSize
= Region
->Length
;
1003 Info
->State
= Region
->Type
;
1004 Info
->Protect
= Region
->Protect
;
1005 Info
->Type
= MEM_PRIVATE
;
1007 *ResultLength
= sizeof(MEMORY_BASIC_INFORMATION
);
1008 return(STATUS_SUCCESS
);