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.12 2003/04/28 10:37:39 gvg 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_UNSUCCESSFUL
);
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
),
512 NtAllocateVirtualMemory(IN HANDLE ProcessHandle
,
513 IN OUT PVOID
* UBaseAddress
,
515 IN OUT PULONG URegionSize
,
516 IN ULONG AllocationType
,
519 * FUNCTION: Allocates a block of virtual memory in the process address space
521 * ProcessHandle = The handle of the process which owns the virtual memory
522 * BaseAddress = A pointer to the virtual memory allocated. If you
523 * supply a non zero value the system will try to
524 * allocate the memory at the address supplied. It round
525 * it down to a multiple of the page size.
526 * ZeroBits = (OPTIONAL) You can specify the number of high order bits
527 * that must be zero, ensuring that the memory will be
528 * allocated at a address below a certain value.
529 * RegionSize = The number of bytes to allocate
530 * AllocationType = Indicates the type of virtual memory you like to
531 * allocated, can be a combination of MEM_COMMIT,
532 * MEM_RESERVE, MEM_RESET, MEM_TOP_DOWN.
533 * Protect = Indicates the protection type of the pages allocated, can be
534 * a combination of PAGE_READONLY, PAGE_READWRITE,
535 * PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_GUARD,
541 MEMORY_AREA
* MemoryArea
;
544 PMADDRESS_SPACE AddressSpace
;
550 DPRINT("NtAllocateVirtualMemory(*UBaseAddress %x, "
551 "ZeroBits %d, *URegionSize %x, AllocationType %x, Protect %x)\n",
552 *UBaseAddress
,ZeroBits
,*URegionSize
,AllocationType
,
556 * Check the validity of the parameters
558 if ((Protect
& PAGE_FLAGS_VALID_FROM_USER_MODE
) != Protect
)
560 return(STATUS_INVALID_PAGE_PROTECTION
);
562 if ((AllocationType
& (MEM_COMMIT
| MEM_RESERVE
)) == 0)
564 return(STATUS_INVALID_PARAMETER
);
567 PBaseAddress
= *UBaseAddress
;
568 PRegionSize
= *URegionSize
;
570 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(PBaseAddress
);
571 RegionSize
= PAGE_ROUND_UP(PBaseAddress
+ PRegionSize
) -
572 PAGE_ROUND_DOWN(PBaseAddress
);
574 Status
= ObReferenceObjectByHandle(ProcessHandle
,
575 PROCESS_VM_OPERATION
,
580 if (!NT_SUCCESS(Status
))
582 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
586 Type
= (AllocationType
& MEM_COMMIT
) ? MEM_COMMIT
: MEM_RESERVE
;
587 DPRINT("Type %x\n", Type
);
589 AddressSpace
= &Process
->AddressSpace
;
590 MmLockAddressSpace(AddressSpace
);
592 if (PBaseAddress
!= 0)
594 MemoryArea
= MmOpenMemoryAreaByAddress(AddressSpace
,
597 if (MemoryArea
!= NULL
&&
598 MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
&&
599 MemoryArea
->Length
>= RegionSize
)
602 MmAlterRegion(AddressSpace
,
603 MemoryArea
->BaseAddress
,
604 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
605 BaseAddress
, RegionSize
,
606 Type
, Protect
, MmModifyAttributes
);
607 MmUnlockAddressSpace(AddressSpace
);
608 ObDereferenceObject(Process
);
609 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
612 else if (MemoryArea
!= NULL
)
614 MmUnlockAddressSpace(AddressSpace
);
615 ObDereferenceObject(Process
);
616 return(STATUS_UNSUCCESSFUL
);
620 Status
= MmCreateMemoryArea(Process
,
622 MEMORY_AREA_VIRTUAL_MEMORY
,
629 if (!NT_SUCCESS(Status
))
631 MmUnlockAddressSpace(AddressSpace
);
632 ObDereferenceObject(Process
);
633 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
636 MmInitialiseRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
637 RegionSize
, Type
, Protect
);
639 if ((AllocationType
& MEM_COMMIT
) &&
640 ((Protect
& PAGE_READWRITE
) ||
641 (Protect
& PAGE_EXECUTE_READWRITE
)))
643 MmReserveSwapPages(RegionSize
);
646 *UBaseAddress
= BaseAddress
;
647 *URegionSize
= RegionSize
;
648 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
650 MmUnlockAddressSpace(AddressSpace
);
651 ObDereferenceObject(Process
);
652 return(STATUS_SUCCESS
);
656 MmFreeVirtualMemoryPage(PVOID Context
,
657 MEMORY_AREA
* MemoryArea
,
659 PHYSICAL_ADDRESS PhysicalAddr
,
663 PEPROCESS Process
= (PEPROCESS
)Context
;
665 if (PhysicalAddr
.QuadPart
!= 0)
667 SWAPENTRY SavedSwapEntry
;
668 SavedSwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddr
);
669 if (SavedSwapEntry
!= 0)
671 MmFreeSwapPage(SavedSwapEntry
);
672 MmSetSavedSwapEntryPage(PhysicalAddr
, 0);
674 MmDeleteRmap(PhysicalAddr
, Process
, Address
);
675 MmReleasePageMemoryConsumer(MC_USER
, PhysicalAddr
);
677 else if (SwapEntry
!= 0)
679 MmFreeSwapPage(SwapEntry
);
684 MmFreeVirtualMemory(PEPROCESS Process
,
685 PMEMORY_AREA MemoryArea
)
687 PLIST_ENTRY current_entry
;
691 DPRINT("MmFreeVirtualMemory(Process %p MemoryArea %p)\n", Process
,
694 /* Mark this memory area as about to be deleted. */
695 MemoryArea
->DeleteInProgress
= TRUE
;
698 * Wait for any ongoing paging operations. Notice that since we have
699 * flagged this memory area as deleted no more page ops will be added.
701 if (MemoryArea
->PageOpCount
> 0)
703 for (i
= 0; i
< PAGE_ROUND_UP(MemoryArea
->Length
) / PAGE_SIZE
; i
++)
707 if (MemoryArea
->PageOpCount
== 0)
712 PageOp
= MmCheckForPageOp(MemoryArea
, Process
->UniqueProcessId
,
713 MemoryArea
->BaseAddress
+ (i
* PAGE_SIZE
),
718 MmUnlockAddressSpace(&Process
->AddressSpace
);
719 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
724 if (Status
!= STATUS_SUCCESS
)
726 DPRINT1("Failed to wait for page op\n");
729 MmLockAddressSpace(&Process
->AddressSpace
);
730 MmReleasePageOp(PageOp
);
735 /* Free all the individual segments. */
736 current_entry
= MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
.Flink
;
737 while (current_entry
!= &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
)
739 current
= CONTAINING_RECORD(current_entry
, MM_REGION
, RegionListEntry
);
740 current_entry
= current_entry
->Flink
;
744 /* Actually free the memory area. */
745 MmFreeMemoryArea(&Process
->AddressSpace
,
746 MemoryArea
->BaseAddress
,
748 MmFreeVirtualMemoryPage
,
753 NtFreeVirtualMemory(IN HANDLE ProcessHandle
,
754 IN PVOID
* PBaseAddress
,
755 IN PULONG PRegionSize
,
758 * FUNCTION: Frees a range of virtual memory
760 * ProcessHandle = Points to the process that allocated the virtual
762 * BaseAddress = Points to the memory address, rounded down to a
763 * multiple of the pagesize
764 * RegionSize = Limits the range to free, rounded up to a multiple of
766 * FreeType = Can be one of the values: MEM_DECOMMIT, or MEM_RELEASE
770 MEMORY_AREA
* MemoryArea
;
773 PMADDRESS_SPACE AddressSpace
;
777 DPRINT("NtFreeVirtualMemory(ProcessHandle %x, *PBaseAddress %x, "
778 "*PRegionSize %x, FreeType %x)\n",ProcessHandle
,*PBaseAddress
,
779 *PRegionSize
,FreeType
);
781 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN((*PBaseAddress
));
782 RegionSize
= PAGE_ROUND_UP((*PBaseAddress
) + (*PRegionSize
)) -
783 PAGE_ROUND_DOWN((*PBaseAddress
));
785 Status
= ObReferenceObjectByHandle(ProcessHandle
,
786 PROCESS_VM_OPERATION
,
791 if (!NT_SUCCESS(Status
))
796 AddressSpace
= &Process
->AddressSpace
;
798 MmLockAddressSpace(AddressSpace
);
799 MemoryArea
= MmOpenMemoryAreaByAddress(AddressSpace
,
801 if (MemoryArea
== NULL
)
803 MmUnlockAddressSpace(AddressSpace
);
804 ObDereferenceObject(Process
);
805 return(STATUS_UNSUCCESSFUL
);
811 /* We can only free a memory area in one step. */
812 if (MemoryArea
->BaseAddress
!= BaseAddress
)
814 MmUnlockAddressSpace(AddressSpace
);
815 ObDereferenceObject(Process
);
816 return(STATUS_UNSUCCESSFUL
);
818 MmFreeVirtualMemory(Process
, MemoryArea
);
819 MmUnlockAddressSpace(AddressSpace
);
820 ObDereferenceObject(Process
);
821 return(STATUS_SUCCESS
);
825 MmAlterRegion(AddressSpace
,
826 MemoryArea
->BaseAddress
,
827 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
833 MmUnlockAddressSpace(AddressSpace
);
834 ObDereferenceObject(Process
);
837 MmUnlockAddressSpace(AddressSpace
);
838 ObDereferenceObject(Process
);
839 return(STATUS_NOT_IMPLEMENTED
);
843 MmProtectAnonMem(PMADDRESS_SPACE AddressSpace
,
844 PMEMORY_AREA MemoryArea
,
853 Region
= MmFindRegion(MemoryArea
->BaseAddress
,
854 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
856 *OldProtect
= Region
->Protect
;
857 Status
= MmAlterRegion(AddressSpace
, MemoryArea
->BaseAddress
,
858 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
859 BaseAddress
, Length
, Region
->Type
, Protect
,
865 MmQueryAnonMem(PMEMORY_AREA MemoryArea
,
867 PMEMORY_BASIC_INFORMATION Info
,
873 Info
->BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(Address
);
875 Region
= MmFindRegion(MemoryArea
->BaseAddress
,
876 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
877 Address
, &RegionBase
);
878 Info
->AllocationBase
= RegionBase
;
879 Info
->AllocationProtect
= Region
->Protect
; /* FIXME */
880 Info
->RegionSize
= RegionBase
+ Region
->Length
- Info
->BaseAddress
;
881 Info
->State
= Region
->Type
;
882 Info
->Protect
= Region
->Protect
;
883 Info
->Type
= MEM_PRIVATE
;
885 *ResultLength
= sizeof(MEMORY_BASIC_INFORMATION
);
886 return(STATUS_SUCCESS
);