3 * Copyright (C) 1998, 1999, 2000, 2001, 2002 ReactOS Team
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /* $Id: anonmem.c,v 1.18 2003/07/12 01:52:10 dwelch Exp $
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/mm/anonmem.c
23 * PURPOSE: Implementing anonymous memory.
24 * PROGRAMMER: David Welch
27 /* INCLUDE *****************************************************************/
29 #include <ddk/ntddk.h>
30 #include <internal/mm.h>
31 #include <internal/ob.h>
32 #include <internal/io.h>
33 #include <internal/ps.h>
34 #include <internal/pool.h>
37 #include <internal/debug.h>
39 /* FUNCTIONS *****************************************************************/
42 MmWritePageVirtualMemory(PMADDRESS_SPACE AddressSpace
,
43 PMEMORY_AREA MemoryArea
,
48 LARGE_INTEGER PhysicalAddress
;
53 * Check for paging out from a deleted virtual memory area.
55 if (MemoryArea
->DeleteInProgress
)
57 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
58 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
59 MmReleasePageOp(PageOp
);
60 return(STATUS_UNSUCCESSFUL
);
64 MmGetPhysicalAddressForProcess(AddressSpace
->Process
, Address
);
67 * Get that the page actually is dirty.
69 if (!MmIsDirtyPage(MemoryArea
->Process
, Address
))
71 PageOp
->Status
= STATUS_SUCCESS
;
72 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
73 MmReleasePageOp(PageOp
);
74 return(STATUS_SUCCESS
);
78 * Speculatively set the mapping to clean.
80 MmSetCleanPage(MemoryArea
->Process
, Address
);
83 * If necessary, allocate an entry in the paging file for this page
85 SwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddress
);
88 SwapEntry
= MmAllocSwapPage();
91 MmSetDirtyPage(MemoryArea
->Process
, Address
);
92 PageOp
->Status
= STATUS_PAGEFILE_QUOTA_EXCEEDED
;
93 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
94 MmReleasePageOp(PageOp
);
95 return(STATUS_PAGEFILE_QUOTA_EXCEEDED
);
100 * Write the page to the pagefile
102 Mdl
= MmCreateMdl(NULL
, NULL
, PAGE_SIZE
);
103 MmBuildMdlFromPages(Mdl
, (PULONG
)&PhysicalAddress
);
104 Status
= MmWriteToSwapPage(SwapEntry
, Mdl
);
105 if (!NT_SUCCESS(Status
))
107 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
109 MmSetDirtyPage(MemoryArea
->Process
, Address
);
110 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
111 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
112 MmReleasePageOp(PageOp
);
113 return(STATUS_UNSUCCESSFUL
);
117 * Otherwise we have succeeded.
119 MmSetSavedSwapEntryPage(PhysicalAddress
, SwapEntry
);
120 PageOp
->Status
= STATUS_SUCCESS
;
121 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
122 MmReleasePageOp(PageOp
);
123 return(STATUS_SUCCESS
);
127 MmPageOutVirtualMemory(PMADDRESS_SPACE AddressSpace
,
128 PMEMORY_AREA MemoryArea
,
132 PHYSICAL_ADDRESS PhysicalAddress
;
138 DPRINT("MmPageOutVirtualMemory(Address 0x%.8X) PID %d\n",
139 Address
, MemoryArea
->Process
->UniqueProcessId
);
142 * Check for paging out from a deleted virtual memory area.
144 if (MemoryArea
->DeleteInProgress
)
146 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
147 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
148 MmReleasePageOp(PageOp
);
149 return(STATUS_UNSUCCESSFUL
);
153 * Disable the virtual mapping.
155 MmDisableVirtualMapping(MemoryArea
->Process
, Address
,
156 &WasDirty
, &PhysicalAddress
);
158 if (PhysicalAddress
.QuadPart
== 0)
164 * Paging out non-dirty data is easy.
168 MmDeleteVirtualMapping(MemoryArea
->Process
, Address
, FALSE
, NULL
, NULL
);
169 MmDeleteAllRmaps(PhysicalAddress
, NULL
, NULL
);
170 if ((SwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddress
)) != 0)
172 MmCreatePageFileMapping(MemoryArea
->Process
, Address
, SwapEntry
);
173 MmSetSavedSwapEntryPage(PhysicalAddress
, 0);
175 MmReleasePageMemoryConsumer(MC_USER
, PhysicalAddress
);
176 PageOp
->Status
= STATUS_SUCCESS
;
177 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
178 MmReleasePageOp(PageOp
);
179 return(STATUS_SUCCESS
);
183 * If necessary, allocate an entry in the paging file for this page
185 SwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddress
);
188 SwapEntry
= MmAllocSwapPage();
191 MmShowOutOfSpaceMessagePagingFile();
192 MmEnableVirtualMapping(MemoryArea
->Process
, Address
);
193 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
194 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
195 MmReleasePageOp(PageOp
);
196 return(STATUS_PAGEFILE_QUOTA
);
201 * Write the page to the pagefile
203 Mdl
= MmCreateMdl(NULL
, NULL
, PAGE_SIZE
);
204 MmBuildMdlFromPages(Mdl
, (ULONG
*)&PhysicalAddress
.u
.LowPart
);
205 Status
= MmWriteToSwapPage(SwapEntry
, Mdl
);
206 if (!NT_SUCCESS(Status
))
208 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
210 MmEnableVirtualMapping(MemoryArea
->Process
, Address
);
211 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
212 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
213 MmReleasePageOp(PageOp
);
214 return(STATUS_UNSUCCESSFUL
);
218 * Otherwise we have succeeded, free the page
220 DPRINT("MM: Swapped out virtual memory page 0x%.8X!\n", PhysicalAddress
);
221 MmDeleteVirtualMapping(MemoryArea
->Process
, Address
, FALSE
, NULL
, NULL
);
222 MmCreatePageFileMapping(MemoryArea
->Process
, Address
, SwapEntry
);
223 MmDeleteAllRmaps(PhysicalAddress
, NULL
, NULL
);
224 MmSetSavedSwapEntryPage(PhysicalAddress
, 0);
225 MmReleasePageMemoryConsumer(MC_USER
, PhysicalAddress
);
226 PageOp
->Status
= STATUS_SUCCESS
;
227 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
228 MmReleasePageOp(PageOp
);
229 return(STATUS_SUCCESS
);
233 MmNotPresentFaultVirtualMemory(PMADDRESS_SPACE AddressSpace
,
234 MEMORY_AREA
* MemoryArea
,
238 * FUNCTION: Move data into memory to satisfy a page not present fault
240 * AddressSpace = Address space within which the fault occurred
241 * MemoryArea = The memory area within which the fault occurred
242 * Address = The absolute address of fault
244 * NOTES: This function is called with the address space lock held.
247 PHYSICAL_ADDRESS Page
;
253 * There is a window between taking the page fault and locking the
254 * address space when another thread could load the page so we check
257 if (MmIsPagePresent(NULL
, Address
))
261 MmLockPage(MmGetPhysicalAddressForProcess(NULL
, Address
));
263 return(STATUS_SUCCESS
);
267 * Check for the virtual memory area being deleted.
269 if (MemoryArea
->DeleteInProgress
)
271 return(STATUS_UNSUCCESSFUL
);
275 * Get the segment corresponding to the virtual address
277 Region
= MmFindRegion(MemoryArea
->BaseAddress
,
278 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
280 if (Region
->Type
== MEM_RESERVE
)
282 return(STATUS_UNSUCCESSFUL
);
286 * Get or create a page operation
288 PageOp
= MmGetPageOp(MemoryArea
, (ULONG
)MemoryArea
->Process
->UniqueProcessId
,
289 (PVOID
)PAGE_ROUND_DOWN(Address
), NULL
, 0,
293 DPRINT1("MmGetPageOp failed");
298 * Check if someone else is already handling this fault, if so wait
301 if (PageOp
->Thread
!= PsGetCurrentThread())
303 MmUnlockAddressSpace(AddressSpace
);
304 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
310 * Check for various strange conditions
312 if (Status
!= STATUS_SUCCESS
)
314 DPRINT1("Failed to wait for page op\n");
317 if (PageOp
->Status
== STATUS_PENDING
)
319 DPRINT1("Woke for page op before completion\n");
323 * If this wasn't a pagein then we need to restart the handling
325 if (PageOp
->OpType
!= MM_PAGEOP_PAGEIN
)
327 MmLockAddressSpace(AddressSpace
);
328 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
329 MmReleasePageOp(PageOp
);
330 return(STATUS_MM_RESTART_OPERATION
);
333 * If the thread handling this fault has failed then we don't retry
335 if (!NT_SUCCESS(PageOp
->Status
))
337 MmLockAddressSpace(AddressSpace
);
338 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
339 Status
= PageOp
->Status
;
340 MmReleasePageOp(PageOp
);
343 MmLockAddressSpace(AddressSpace
);
346 MmLockPage(MmGetPhysicalAddressForProcess(NULL
, Address
));
348 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
349 MmReleasePageOp(PageOp
);
350 return(STATUS_SUCCESS
);
354 * Try to allocate a page
356 Status
= MmRequestPageMemoryConsumer(MC_USER
, FALSE
, &Page
);
357 if (Status
== STATUS_NO_MEMORY
)
359 MmUnlockAddressSpace(AddressSpace
);
360 Status
= MmRequestPageMemoryConsumer(MC_USER
, TRUE
, &Page
);
361 MmLockAddressSpace(AddressSpace
);
363 if (!NT_SUCCESS(Status
))
365 DPRINT1("MmRequestPageMemoryConsumer failed, status = %x\n", Status
);
370 * Handle swapped out pages.
372 if (MmIsPageSwapEntry(NULL
, Address
))
377 MmDeletePageFileMapping(NULL
, Address
, &SwapEntry
);
378 Mdl
= MmCreateMdl(NULL
, NULL
, PAGE_SIZE
);
379 MmBuildMdlFromPages(Mdl
, (PULONG
)&Page
);
380 Status
= MmReadFromSwapPage(SwapEntry
, Mdl
);
381 if (!NT_SUCCESS(Status
))
385 MmSetSavedSwapEntryPage(Page
, SwapEntry
);
389 * Set the page. If we fail because we are out of memory then
392 Status
= MmCreateVirtualMapping(MemoryArea
->Process
,
393 (PVOID
)PAGE_ROUND_DOWN(Address
),
394 MemoryArea
->Attributes
,
397 while (Status
== STATUS_NO_MEMORY
)
399 MmUnlockAddressSpace(AddressSpace
);
400 Status
= MmCreateVirtualMapping(MemoryArea
->Process
,
402 MemoryArea
->Attributes
,
405 MmLockAddressSpace(AddressSpace
);
407 if (!NT_SUCCESS(Status
))
409 DPRINT1("MmCreateVirtualMapping failed, not out of memory\n");
415 * Add the page to the process's working set
417 MmInsertRmap(Page
, MemoryArea
->Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
420 * Finish the operation
424 MmLockPage(MmGetPhysicalAddressForProcess(NULL
, Address
));
426 PageOp
->Status
= STATUS_SUCCESS
;
427 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
428 MmReleasePageOp(PageOp
);
429 return(STATUS_SUCCESS
);
433 MmModifyAttributes(PMADDRESS_SPACE AddressSpace
,
441 * FUNCTION: Modify the attributes of a memory region
445 * If we are switching a previously committed region to reserved then
446 * free any allocated pages within the region
448 if (NewType
== MEM_RESERVE
&& OldType
== MEM_COMMIT
)
452 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
454 LARGE_INTEGER PhysicalAddr
;
456 if (MmIsPageSwapEntry(AddressSpace
->Process
,
457 BaseAddress
+ (i
* PAGE_SIZE
)))
461 MmDeletePageFileMapping(AddressSpace
->Process
,
462 BaseAddress
+ (i
* PAGE_SIZE
),
464 MmFreeSwapPage(SwapEntry
);
468 PhysicalAddr
= MmGetPhysicalAddress(BaseAddress
+ (i
*PAGE_SIZE
));
469 MmDeleteVirtualMapping(AddressSpace
->Process
,
470 BaseAddress
+ (i
*PAGE_SIZE
),
472 if (PhysicalAddr
.QuadPart
!= 0)
474 SWAPENTRY SavedSwapEntry
;
475 SavedSwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddr
);
476 if (SavedSwapEntry
!= 0)
478 MmFreeSwapPage(SavedSwapEntry
);
479 MmSetSavedSwapEntryPage(PhysicalAddr
, 0);
481 MmDeleteRmap(PhysicalAddr
, AddressSpace
->Process
,
482 BaseAddress
+ (i
* PAGE_SIZE
));
483 MmReleasePageMemoryConsumer(MC_USER
, PhysicalAddr
);
490 * If we are changing the protection attributes of a committed region then
491 * alter the attributes for any allocated pages within the region
493 if (NewType
== MEM_COMMIT
&& OldType
== MEM_COMMIT
&&
494 OldProtect
!= NewProtect
)
498 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
500 if (MmIsPagePresent(AddressSpace
->Process
,
501 BaseAddress
+ (i
*PAGE_SIZE
)))
503 MmSetPageProtect(AddressSpace
->Process
,
504 BaseAddress
+ (i
*PAGE_SIZE
),
515 NtAllocateVirtualMemory(IN HANDLE ProcessHandle
,
516 IN OUT PVOID
* UBaseAddress
,
518 IN OUT PULONG URegionSize
,
519 IN ULONG AllocationType
,
522 * FUNCTION: Allocates a block of virtual memory in the process address space
524 * ProcessHandle = The handle of the process which owns the virtual memory
525 * BaseAddress = A pointer to the virtual memory allocated. If you
526 * supply a non zero value the system will try to
527 * allocate the memory at the address supplied. It round
528 * it down to a multiple of the page size.
529 * ZeroBits = (OPTIONAL) You can specify the number of high order bits
530 * that must be zero, ensuring that the memory will be
531 * allocated at a address below a certain value.
532 * RegionSize = The number of bytes to allocate
533 * AllocationType = Indicates the type of virtual memory you like to
534 * allocated, can be a combination of MEM_COMMIT,
535 * MEM_RESERVE, MEM_RESET, MEM_TOP_DOWN.
536 * Protect = Indicates the protection type of the pages allocated, can be
537 * a combination of PAGE_READONLY, PAGE_READWRITE,
538 * PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_GUARD,
544 MEMORY_AREA
* MemoryArea
;
547 PMADDRESS_SPACE AddressSpace
;
553 DPRINT("NtAllocateVirtualMemory(*UBaseAddress %x, "
554 "ZeroBits %d, *URegionSize %x, AllocationType %x, Protect %x)\n",
555 *UBaseAddress
,ZeroBits
,*URegionSize
,AllocationType
,
559 * Check the validity of the parameters
561 if ((Protect
& PAGE_FLAGS_VALID_FROM_USER_MODE
) != Protect
)
563 return(STATUS_INVALID_PAGE_PROTECTION
);
565 if ((AllocationType
& (MEM_COMMIT
| MEM_RESERVE
)) == 0)
567 return(STATUS_INVALID_PARAMETER
);
570 PBaseAddress
= *UBaseAddress
;
571 PRegionSize
= *URegionSize
;
573 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(PBaseAddress
);
574 RegionSize
= PAGE_ROUND_UP(PBaseAddress
+ PRegionSize
) -
575 PAGE_ROUND_DOWN(PBaseAddress
);
577 Status
= ObReferenceObjectByHandle(ProcessHandle
,
578 PROCESS_VM_OPERATION
,
583 if (!NT_SUCCESS(Status
))
585 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
589 Type
= (AllocationType
& MEM_COMMIT
) ? MEM_COMMIT
: MEM_RESERVE
;
590 DPRINT("Type %x\n", Type
);
592 AddressSpace
= &Process
->AddressSpace
;
593 MmLockAddressSpace(AddressSpace
);
595 if (PBaseAddress
!= 0)
597 MemoryArea
= MmOpenMemoryAreaByAddress(AddressSpace
,
600 if (MemoryArea
!= NULL
&&
601 MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
&&
602 MemoryArea
->Length
>= RegionSize
)
605 MmAlterRegion(AddressSpace
,
606 MemoryArea
->BaseAddress
,
607 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
608 BaseAddress
, RegionSize
,
609 Type
, Protect
, MmModifyAttributes
);
610 MmUnlockAddressSpace(AddressSpace
);
611 ObDereferenceObject(Process
);
612 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
615 else if (MemoryArea
!= NULL
)
617 MmUnlockAddressSpace(AddressSpace
);
618 ObDereferenceObject(Process
);
619 return(STATUS_UNSUCCESSFUL
);
623 Status
= MmCreateMemoryArea(Process
,
625 MEMORY_AREA_VIRTUAL_MEMORY
,
631 (AllocationType
& MEM_TOP_DOWN
));
632 if (!NT_SUCCESS(Status
))
634 MmUnlockAddressSpace(AddressSpace
);
635 ObDereferenceObject(Process
);
636 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
639 MmInitialiseRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
640 RegionSize
, Type
, Protect
);
642 if ((AllocationType
& MEM_COMMIT
) &&
643 ((Protect
& PAGE_READWRITE
) ||
644 (Protect
& PAGE_EXECUTE_READWRITE
)))
646 MmReserveSwapPages(RegionSize
);
649 *UBaseAddress
= BaseAddress
;
650 *URegionSize
= RegionSize
;
651 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
653 MmUnlockAddressSpace(AddressSpace
);
654 ObDereferenceObject(Process
);
655 return(STATUS_SUCCESS
);
659 MmFreeVirtualMemoryPage(PVOID Context
,
660 MEMORY_AREA
* MemoryArea
,
662 PHYSICAL_ADDRESS PhysicalAddr
,
666 PEPROCESS Process
= (PEPROCESS
)Context
;
668 if (PhysicalAddr
.QuadPart
!= 0)
670 SWAPENTRY SavedSwapEntry
;
671 SavedSwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddr
);
672 if (SavedSwapEntry
!= 0)
674 MmFreeSwapPage(SavedSwapEntry
);
675 MmSetSavedSwapEntryPage(PhysicalAddr
, 0);
677 MmDeleteRmap(PhysicalAddr
, Process
, Address
);
678 MmReleasePageMemoryConsumer(MC_USER
, PhysicalAddr
);
680 else if (SwapEntry
!= 0)
682 MmFreeSwapPage(SwapEntry
);
687 MmFreeVirtualMemory(PEPROCESS Process
,
688 PMEMORY_AREA MemoryArea
)
690 PLIST_ENTRY current_entry
;
694 DPRINT("MmFreeVirtualMemory(Process %p MemoryArea %p)\n", Process
,
697 /* Mark this memory area as about to be deleted. */
698 MemoryArea
->DeleteInProgress
= TRUE
;
701 * Wait for any ongoing paging operations. Notice that since we have
702 * flagged this memory area as deleted no more page ops will be added.
704 if (MemoryArea
->PageOpCount
> 0)
706 for (i
= 0; i
< PAGE_ROUND_UP(MemoryArea
->Length
) / PAGE_SIZE
; i
++)
710 if (MemoryArea
->PageOpCount
== 0)
715 PageOp
= MmCheckForPageOp(MemoryArea
, Process
->UniqueProcessId
,
716 MemoryArea
->BaseAddress
+ (i
* PAGE_SIZE
),
721 MmUnlockAddressSpace(&Process
->AddressSpace
);
722 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
727 if (Status
!= STATUS_SUCCESS
)
729 DPRINT1("Failed to wait for page op\n");
732 MmLockAddressSpace(&Process
->AddressSpace
);
733 MmReleasePageOp(PageOp
);
738 /* Free all the individual segments. */
739 current_entry
= MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
.Flink
;
740 while (current_entry
!= &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
)
742 current
= CONTAINING_RECORD(current_entry
, MM_REGION
, RegionListEntry
);
743 current_entry
= current_entry
->Flink
;
747 /* Actually free the memory area. */
748 MmFreeMemoryArea(&Process
->AddressSpace
,
749 MemoryArea
->BaseAddress
,
751 MmFreeVirtualMemoryPage
,
759 NtFreeVirtualMemory(IN HANDLE ProcessHandle
,
760 IN PVOID
* PBaseAddress
,
761 IN PULONG PRegionSize
,
764 * FUNCTION: Frees a range of virtual memory
766 * ProcessHandle = Points to the process that allocated the virtual
768 * BaseAddress = Points to the memory address, rounded down to a
769 * multiple of the pagesize
770 * RegionSize = Limits the range to free, rounded up to a multiple of
772 * FreeType = Can be one of the values: MEM_DECOMMIT, or MEM_RELEASE
776 MEMORY_AREA
* MemoryArea
;
779 PMADDRESS_SPACE AddressSpace
;
783 DPRINT("NtFreeVirtualMemory(ProcessHandle %x, *PBaseAddress %x, "
784 "*PRegionSize %x, FreeType %x)\n",ProcessHandle
,*PBaseAddress
,
785 *PRegionSize
,FreeType
);
787 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN((*PBaseAddress
));
788 RegionSize
= PAGE_ROUND_UP((*PBaseAddress
) + (*PRegionSize
)) -
789 PAGE_ROUND_DOWN((*PBaseAddress
));
791 Status
= ObReferenceObjectByHandle(ProcessHandle
,
792 PROCESS_VM_OPERATION
,
797 if (!NT_SUCCESS(Status
))
802 AddressSpace
= &Process
->AddressSpace
;
804 MmLockAddressSpace(AddressSpace
);
805 MemoryArea
= MmOpenMemoryAreaByAddress(AddressSpace
,
807 if (MemoryArea
== NULL
)
809 MmUnlockAddressSpace(AddressSpace
);
810 ObDereferenceObject(Process
);
811 return(STATUS_UNSUCCESSFUL
);
817 /* We can only free a memory area in one step. */
818 if (MemoryArea
->BaseAddress
!= BaseAddress
)
820 MmUnlockAddressSpace(AddressSpace
);
821 ObDereferenceObject(Process
);
822 return(STATUS_UNSUCCESSFUL
);
824 MmFreeVirtualMemory(Process
, MemoryArea
);
825 MmUnlockAddressSpace(AddressSpace
);
826 ObDereferenceObject(Process
);
827 return(STATUS_SUCCESS
);
831 MmAlterRegion(AddressSpace
,
832 MemoryArea
->BaseAddress
,
833 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
839 MmUnlockAddressSpace(AddressSpace
);
840 ObDereferenceObject(Process
);
843 MmUnlockAddressSpace(AddressSpace
);
844 ObDereferenceObject(Process
);
845 return(STATUS_NOT_IMPLEMENTED
);
849 MmProtectAnonMem(PMADDRESS_SPACE AddressSpace
,
850 PMEMORY_AREA MemoryArea
,
859 Region
= MmFindRegion(MemoryArea
->BaseAddress
,
860 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
862 *OldProtect
= Region
->Protect
;
863 Status
= MmAlterRegion(AddressSpace
, MemoryArea
->BaseAddress
,
864 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
865 BaseAddress
, Length
, Region
->Type
, Protect
,
871 MmQueryAnonMem(PMEMORY_AREA MemoryArea
,
873 PMEMORY_BASIC_INFORMATION Info
,
879 Info
->BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(Address
);
881 Region
= MmFindRegion(MemoryArea
->BaseAddress
,
882 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
883 Address
, &RegionBase
);
884 Info
->AllocationBase
= RegionBase
;
885 Info
->AllocationProtect
= Region
->Protect
; /* FIXME */
886 Info
->RegionSize
= RegionBase
+ Region
->Length
- Info
->BaseAddress
;
887 Info
->State
= Region
->Type
;
888 Info
->Protect
= Region
->Protect
;
889 Info
->Type
= MEM_PRIVATE
;
891 *ResultLength
= sizeof(MEMORY_BASIC_INFORMATION
);
892 return(STATUS_SUCCESS
);