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.34 2004/12/19 16:16:57 navaraf Exp $
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/mm/anonmem.c
23 * PURPOSE: Implementing anonymous memory.
24 * PROGRAMMER: David Welch
27 /* INCLUDE *****************************************************************/
31 #include <internal/debug.h>
33 /* FUNCTIONS *****************************************************************/
36 MmWritePageVirtualMemory(PMADDRESS_SPACE AddressSpace
,
37 PMEMORY_AREA MemoryArea
,
46 * Check for paging out from a deleted virtual memory area.
48 if (MemoryArea
->DeleteInProgress
)
50 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
51 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
52 MmReleasePageOp(PageOp
);
53 return(STATUS_UNSUCCESSFUL
);
56 Page
= MmGetPfnForProcess(AddressSpace
->Process
, Address
);
59 * Get that the page actually is dirty.
61 if (!MmIsDirtyPage(MemoryArea
->Process
, Address
))
63 PageOp
->Status
= STATUS_SUCCESS
;
64 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
65 MmReleasePageOp(PageOp
);
66 return(STATUS_SUCCESS
);
70 * Speculatively set the mapping to clean.
72 MmSetCleanPage(MemoryArea
->Process
, Address
);
75 * If necessary, allocate an entry in the paging file for this page
77 SwapEntry
= MmGetSavedSwapEntryPage(Page
);
80 SwapEntry
= MmAllocSwapPage();
83 MmSetDirtyPage(MemoryArea
->Process
, Address
);
84 PageOp
->Status
= STATUS_PAGEFILE_QUOTA_EXCEEDED
;
85 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
86 MmReleasePageOp(PageOp
);
87 return(STATUS_PAGEFILE_QUOTA_EXCEEDED
);
92 * Write the page to the pagefile
94 Status
= MmWriteToSwapPage(SwapEntry
, Page
);
95 if (!NT_SUCCESS(Status
))
97 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
99 MmSetDirtyPage(MemoryArea
->Process
, Address
);
100 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
101 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
102 MmReleasePageOp(PageOp
);
103 return(STATUS_UNSUCCESSFUL
);
107 * Otherwise we have succeeded.
109 MmSetSavedSwapEntryPage(Page
, SwapEntry
);
110 PageOp
->Status
= STATUS_SUCCESS
;
111 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
112 MmReleasePageOp(PageOp
);
113 return(STATUS_SUCCESS
);
117 MmPageOutVirtualMemory(PMADDRESS_SPACE AddressSpace
,
118 PMEMORY_AREA MemoryArea
,
127 DPRINT("MmPageOutVirtualMemory(Address 0x%.8X) PID %d\n",
128 Address
, MemoryArea
->Process
->UniqueProcessId
);
131 * Check for paging out from a deleted virtual memory area.
133 if (MemoryArea
->DeleteInProgress
)
135 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
136 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
137 MmReleasePageOp(PageOp
);
138 return(STATUS_UNSUCCESSFUL
);
142 * Disable the virtual mapping.
144 MmDisableVirtualMapping(MemoryArea
->Process
, Address
,
153 * Paging out non-dirty data is easy.
157 MmDeleteVirtualMapping(MemoryArea
->Process
, Address
, FALSE
, NULL
, NULL
);
158 MmDeleteAllRmaps(Page
, NULL
, NULL
);
159 if ((SwapEntry
= MmGetSavedSwapEntryPage(Page
)) != 0)
161 MmCreatePageFileMapping(MemoryArea
->Process
, Address
, SwapEntry
);
162 MmSetSavedSwapEntryPage(Page
, 0);
164 MmReleasePageMemoryConsumer(MC_USER
, Page
);
165 PageOp
->Status
= STATUS_SUCCESS
;
166 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
167 MmReleasePageOp(PageOp
);
168 return(STATUS_SUCCESS
);
172 * If necessary, allocate an entry in the paging file for this page
174 SwapEntry
= MmGetSavedSwapEntryPage(Page
);
177 SwapEntry
= MmAllocSwapPage();
180 MmShowOutOfSpaceMessagePagingFile();
181 MmEnableVirtualMapping(MemoryArea
->Process
, Address
);
182 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
183 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
184 MmReleasePageOp(PageOp
);
185 return(STATUS_PAGEFILE_QUOTA
);
190 * Write the page to the pagefile
192 Status
= MmWriteToSwapPage(SwapEntry
, Page
);
193 if (!NT_SUCCESS(Status
))
195 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
197 MmEnableVirtualMapping(MemoryArea
->Process
, Address
);
198 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
199 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
200 MmReleasePageOp(PageOp
);
201 return(STATUS_UNSUCCESSFUL
);
205 * Otherwise we have succeeded, free the page
207 DPRINT("MM: Swapped out virtual memory page 0x%.8X!\n", Page
<< PAGE_SHIFT
);
208 MmDeleteVirtualMapping(MemoryArea
->Process
, Address
, FALSE
, NULL
, NULL
);
209 MmCreatePageFileMapping(MemoryArea
->Process
, Address
, SwapEntry
);
210 MmDeleteAllRmaps(Page
, NULL
, NULL
);
211 MmSetSavedSwapEntryPage(Page
, 0);
212 MmReleasePageMemoryConsumer(MC_USER
, Page
);
213 PageOp
->Status
= STATUS_SUCCESS
;
214 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
215 MmReleasePageOp(PageOp
);
216 return(STATUS_SUCCESS
);
220 MmNotPresentFaultVirtualMemory(PMADDRESS_SPACE AddressSpace
,
221 MEMORY_AREA
* MemoryArea
,
225 * FUNCTION: Move data into memory to satisfy a page not present fault
227 * AddressSpace = Address space within which the fault occurred
228 * MemoryArea = The memory area within which the fault occurred
229 * Address = The absolute address of fault
231 * NOTES: This function is called with the address space lock held.
240 * There is a window between taking the page fault and locking the
241 * address space when another thread could load the page so we check
244 if (MmIsPagePresent(NULL
, Address
))
248 MmLockPage(MmGetPfnForProcess(NULL
, Address
));
250 return(STATUS_SUCCESS
);
254 * Check for the virtual memory area being deleted.
256 if (MemoryArea
->DeleteInProgress
)
258 return(STATUS_UNSUCCESSFUL
);
262 * Get the segment corresponding to the virtual address
264 Region
= MmFindRegion(MemoryArea
->BaseAddress
,
265 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
267 if (Region
->Type
== MEM_RESERVE
|| Region
->Protect
== PAGE_NOACCESS
)
269 return(STATUS_ACCESS_VIOLATION
);
273 * Get or create a page operation
275 PageOp
= MmGetPageOp(MemoryArea
, (ULONG
)MemoryArea
->Process
->UniqueProcessId
,
276 (PVOID
)PAGE_ROUND_DOWN(Address
), NULL
, 0,
277 MM_PAGEOP_PAGEIN
, FALSE
);
280 DPRINT1("MmGetPageOp failed");
285 * Check if someone else is already handling this fault, if so wait
288 if (PageOp
->Thread
!= PsGetCurrentThread())
290 MmUnlockAddressSpace(AddressSpace
);
291 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
297 * Check for various strange conditions
299 if (Status
!= STATUS_SUCCESS
)
301 DPRINT1("Failed to wait for page op\n");
304 if (PageOp
->Status
== STATUS_PENDING
)
306 DPRINT1("Woke for page op before completion\n");
310 * If this wasn't a pagein then we need to restart the handling
312 if (PageOp
->OpType
!= MM_PAGEOP_PAGEIN
)
314 MmLockAddressSpace(AddressSpace
);
315 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
316 MmReleasePageOp(PageOp
);
317 return(STATUS_MM_RESTART_OPERATION
);
320 * If the thread handling this fault has failed then we don't retry
322 if (!NT_SUCCESS(PageOp
->Status
))
324 MmLockAddressSpace(AddressSpace
);
325 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
326 Status
= PageOp
->Status
;
327 MmReleasePageOp(PageOp
);
330 MmLockAddressSpace(AddressSpace
);
333 MmLockPage(MmGetPfnForProcess(NULL
, Address
));
335 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
336 MmReleasePageOp(PageOp
);
337 return(STATUS_SUCCESS
);
341 * Try to allocate a page
343 Status
= MmRequestPageMemoryConsumer(MC_USER
, FALSE
, &Page
);
344 if (Status
== STATUS_NO_MEMORY
)
346 MmUnlockAddressSpace(AddressSpace
);
347 Status
= MmRequestPageMemoryConsumer(MC_USER
, TRUE
, &Page
);
348 MmLockAddressSpace(AddressSpace
);
350 if (!NT_SUCCESS(Status
))
352 DPRINT1("MmRequestPageMemoryConsumer failed, status = %x\n", Status
);
357 * Handle swapped out pages.
359 if (MmIsPageSwapEntry(NULL
, Address
))
363 MmDeletePageFileMapping(MemoryArea
->Process
, Address
, &SwapEntry
);
364 Status
= MmReadFromSwapPage(SwapEntry
, Page
);
365 if (!NT_SUCCESS(Status
))
369 MmSetSavedSwapEntryPage(Page
, SwapEntry
);
373 * Set the page. If we fail because we are out of memory then
376 Status
= MmCreateVirtualMapping(MemoryArea
->Process
,
377 (PVOID
)PAGE_ROUND_DOWN(Address
),
381 while (Status
== STATUS_NO_MEMORY
)
383 MmUnlockAddressSpace(AddressSpace
);
384 Status
= MmCreateVirtualMapping(MemoryArea
->Process
,
389 MmLockAddressSpace(AddressSpace
);
391 if (!NT_SUCCESS(Status
))
393 DPRINT1("MmCreateVirtualMapping failed, not out of memory\n");
399 * Add the page to the process's working set
401 MmInsertRmap(Page
, MemoryArea
->Process
, (PVOID
)PAGE_ROUND_DOWN(Address
));
404 * Finish the operation
410 PageOp
->Status
= STATUS_SUCCESS
;
411 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
412 MmReleasePageOp(PageOp
);
413 return(STATUS_SUCCESS
);
417 MmModifyAttributes(PMADDRESS_SPACE AddressSpace
,
425 * FUNCTION: Modify the attributes of a memory region
429 * If we are switching a previously committed region to reserved then
430 * free any allocated pages within the region
432 if (NewType
== MEM_RESERVE
&& OldType
== MEM_COMMIT
)
436 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
440 if (MmIsPageSwapEntry(AddressSpace
->Process
,
441 (char*)BaseAddress
+ (i
* PAGE_SIZE
)))
445 MmDeletePageFileMapping(AddressSpace
->Process
,
446 (char*)BaseAddress
+ (i
* PAGE_SIZE
),
448 MmFreeSwapPage(SwapEntry
);
452 MmDeleteVirtualMapping(AddressSpace
->Process
,
453 (char*)BaseAddress
+ (i
*PAGE_SIZE
),
457 SWAPENTRY SavedSwapEntry
;
458 SavedSwapEntry
= MmGetSavedSwapEntryPage(Page
);
459 if (SavedSwapEntry
!= 0)
461 MmFreeSwapPage(SavedSwapEntry
);
462 MmSetSavedSwapEntryPage(Page
, 0);
464 MmDeleteRmap(Page
, AddressSpace
->Process
,
465 (char*)BaseAddress
+ (i
* PAGE_SIZE
));
466 MmReleasePageMemoryConsumer(MC_USER
, Page
);
473 * If we are changing the protection attributes of a committed region then
474 * alter the attributes for any allocated pages within the region
476 if (NewType
== MEM_COMMIT
&& OldType
== MEM_COMMIT
&&
477 OldProtect
!= NewProtect
)
481 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
483 if (MmIsPagePresent(AddressSpace
->Process
,
484 (char*)BaseAddress
+ (i
*PAGE_SIZE
)))
486 MmSetPageProtect(AddressSpace
->Process
,
487 (char*)BaseAddress
+ (i
*PAGE_SIZE
),
498 NtAllocateVirtualMemory(IN HANDLE ProcessHandle
,
499 IN OUT PVOID
* UBaseAddress
,
501 IN OUT PULONG URegionSize
,
502 IN ULONG AllocationType
,
505 * FUNCTION: Allocates a block of virtual memory in the process address space
507 * ProcessHandle = The handle of the process which owns the virtual memory
508 * BaseAddress = A pointer to the virtual memory allocated. If you
509 * supply a non zero value the system will try to
510 * allocate the memory at the address supplied. It round
511 * it down to a multiple of the page size.
512 * ZeroBits = (OPTIONAL) You can specify the number of high order bits
513 * that must be zero, ensuring that the memory will be
514 * allocated at a address below a certain value.
515 * RegionSize = The number of bytes to allocate
516 * AllocationType = Indicates the type of virtual memory you like to
517 * allocated, can be a combination of MEM_COMMIT,
518 * MEM_RESERVE, MEM_RESET, MEM_TOP_DOWN.
519 * Protect = Indicates the protection type of the pages allocated, can be
520 * a combination of PAGE_READONLY, PAGE_READWRITE,
521 * PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_GUARD,
527 MEMORY_AREA
* MemoryArea
;
530 PMADDRESS_SPACE AddressSpace
;
535 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
537 DPRINT("NtAllocateVirtualMemory(*UBaseAddress %x, "
538 "ZeroBits %d, *URegionSize %x, AllocationType %x, Protect %x)\n",
539 *UBaseAddress
,ZeroBits
,*URegionSize
,AllocationType
,
543 * Check the validity of the parameters
545 if ((Protect
& PAGE_FLAGS_VALID_FROM_USER_MODE
) != Protect
)
547 return(STATUS_INVALID_PAGE_PROTECTION
);
549 if ((AllocationType
& (MEM_COMMIT
| MEM_RESERVE
)) == 0)
551 return(STATUS_INVALID_PARAMETER
);
554 PBaseAddress
= *UBaseAddress
;
555 PRegionSize
= *URegionSize
;
556 BoundaryAddressMultiple
.QuadPart
= 0;
558 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(PBaseAddress
);
559 RegionSize
= PAGE_ROUND_UP(PBaseAddress
+ PRegionSize
) -
560 PAGE_ROUND_DOWN(PBaseAddress
);
562 Status
= ObReferenceObjectByHandle(ProcessHandle
,
563 PROCESS_VM_OPERATION
,
568 if (!NT_SUCCESS(Status
))
570 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
574 Type
= (AllocationType
& MEM_COMMIT
) ? MEM_COMMIT
: MEM_RESERVE
;
575 DPRINT("Type %x\n", Type
);
577 AddressSpace
= &Process
->AddressSpace
;
578 MmLockAddressSpace(AddressSpace
);
580 if (PBaseAddress
!= 0)
582 MemoryArea
= MmOpenMemoryAreaByAddress(AddressSpace
,
585 if (MemoryArea
!= NULL
&&
586 MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
&&
587 MemoryArea
->Length
>= RegionSize
)
590 MmAlterRegion(AddressSpace
,
591 MemoryArea
->BaseAddress
,
592 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
593 BaseAddress
, RegionSize
,
594 Type
, Protect
, MmModifyAttributes
);
595 MmUnlockAddressSpace(AddressSpace
);
596 ObDereferenceObject(Process
);
597 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
600 else if (MemoryArea
!= NULL
&& MemoryArea
->Length
>= RegionSize
)
603 MmAlterRegion(AddressSpace
,
604 MemoryArea
->BaseAddress
,
605 &MemoryArea
->Data
.SectionData
.RegionListHead
,
606 BaseAddress
, RegionSize
,
607 Type
, Protect
, MmModifyAttributes
);
608 MmUnlockAddressSpace(AddressSpace
);
609 ObDereferenceObject(Process
);
610 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
613 else if (MemoryArea
!= NULL
)
615 MmUnlockAddressSpace(AddressSpace
);
616 ObDereferenceObject(Process
);
617 return(STATUS_UNSUCCESSFUL
);
621 Status
= MmCreateMemoryArea(Process
,
623 MEMORY_AREA_VIRTUAL_MEMORY
,
629 (AllocationType
& MEM_TOP_DOWN
),
630 BoundaryAddressMultiple
);
631 if (!NT_SUCCESS(Status
))
633 MmUnlockAddressSpace(AddressSpace
);
634 ObDereferenceObject(Process
);
635 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
638 MmInitialiseRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
639 MemoryArea
->Length
, Type
, Protect
);
641 if ((AllocationType
& MEM_COMMIT
) &&
642 ((Protect
& PAGE_READWRITE
) ||
643 (Protect
& PAGE_EXECUTE_READWRITE
)))
645 MmReserveSwapPages(MemoryArea
->Length
);
648 *UBaseAddress
= BaseAddress
;
649 *URegionSize
= MemoryArea
->Length
;
650 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
652 MmUnlockAddressSpace(AddressSpace
);
653 ObDereferenceObject(Process
);
654 return(STATUS_SUCCESS
);
658 MmFreeVirtualMemoryPage(PVOID Context
,
659 MEMORY_AREA
* MemoryArea
,
665 PEPROCESS Process
= (PEPROCESS
)Context
;
669 SWAPENTRY SavedSwapEntry
;
670 SavedSwapEntry
= MmGetSavedSwapEntryPage(Page
);
671 if (SavedSwapEntry
!= 0)
673 MmFreeSwapPage(SavedSwapEntry
);
674 MmSetSavedSwapEntryPage(Page
, 0);
676 MmDeleteRmap(Page
, Process
, Address
);
677 MmReleasePageMemoryConsumer(MC_USER
, Page
);
679 else if (SwapEntry
!= 0)
681 MmFreeSwapPage(SwapEntry
);
686 MmFreeVirtualMemory(PEPROCESS Process
,
687 PMEMORY_AREA MemoryArea
)
689 PLIST_ENTRY current_entry
;
693 DPRINT("MmFreeVirtualMemory(Process %p MemoryArea %p)\n", Process
,
696 /* Mark this memory area as about to be deleted. */
697 MemoryArea
->DeleteInProgress
= TRUE
;
700 * Wait for any ongoing paging operations. Notice that since we have
701 * flagged this memory area as deleted no more page ops will be added.
703 if (MemoryArea
->PageOpCount
> 0)
705 for (i
= 0; i
< PAGE_ROUND_UP(MemoryArea
->Length
) / PAGE_SIZE
; i
++)
709 if (MemoryArea
->PageOpCount
== 0)
714 PageOp
= MmCheckForPageOp(MemoryArea
, Process
->UniqueProcessId
,
715 (char*)MemoryArea
->BaseAddress
+ (i
* PAGE_SIZE
),
720 MmUnlockAddressSpace(&Process
->AddressSpace
);
721 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
726 if (Status
!= STATUS_SUCCESS
)
728 DPRINT1("Failed to wait for page op\n");
731 MmLockAddressSpace(&Process
->AddressSpace
);
732 MmReleasePageOp(PageOp
);
737 /* Free all the individual segments. */
738 current_entry
= MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
.Flink
;
739 while (current_entry
!= &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
)
741 current
= CONTAINING_RECORD(current_entry
, MM_REGION
, RegionListEntry
);
742 current_entry
= current_entry
->Flink
;
746 /* Actually free the memory area. */
747 MmFreeMemoryArea(&Process
->AddressSpace
,
748 MemoryArea
->BaseAddress
,
750 MmFreeVirtualMemoryPage
,
758 NtFreeVirtualMemory(IN HANDLE ProcessHandle
,
759 IN PVOID
* PBaseAddress
,
760 IN PULONG PRegionSize
,
763 * FUNCTION: Frees a range of virtual memory
765 * ProcessHandle = Points to the process that allocated the virtual
767 * BaseAddress = Points to the memory address, rounded down to a
768 * multiple of the pagesize
769 * RegionSize = Limits the range to free, rounded up to a multiple of
771 * FreeType = Can be one of the values: MEM_DECOMMIT, or MEM_RELEASE
775 MEMORY_AREA
* MemoryArea
;
778 PMADDRESS_SPACE AddressSpace
;
782 DPRINT("NtFreeVirtualMemory(ProcessHandle %x, *PBaseAddress %x, "
783 "*PRegionSize %x, FreeType %x)\n",ProcessHandle
,*PBaseAddress
,
784 *PRegionSize
,FreeType
);
786 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN((*PBaseAddress
));
787 RegionSize
= PAGE_ROUND_UP((*PBaseAddress
) + (*PRegionSize
)) -
788 PAGE_ROUND_DOWN((*PBaseAddress
));
790 Status
= ObReferenceObjectByHandle(ProcessHandle
,
791 PROCESS_VM_OPERATION
,
796 if (!NT_SUCCESS(Status
))
801 AddressSpace
= &Process
->AddressSpace
;
803 MmLockAddressSpace(AddressSpace
);
804 MemoryArea
= MmOpenMemoryAreaByAddress(AddressSpace
,
806 if (MemoryArea
== NULL
)
808 MmUnlockAddressSpace(AddressSpace
);
809 ObDereferenceObject(Process
);
810 return(STATUS_UNSUCCESSFUL
);
816 /* We can only free a memory area in one step. */
817 if (MemoryArea
->BaseAddress
!= BaseAddress
||
818 MemoryArea
->Type
!= MEMORY_AREA_VIRTUAL_MEMORY
)
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
->BaseAddress
= RegionBase
;
885 Info
->AllocationBase
= MemoryArea
->BaseAddress
;
886 Info
->AllocationProtect
= MemoryArea
->Attributes
;
887 Info
->RegionSize
= (char*)RegionBase
+ Region
->Length
- (char*)Info
->BaseAddress
;
888 Info
->State
= Region
->Type
;
889 Info
->Protect
= Region
->Protect
;
890 Info
->Type
= MEM_PRIVATE
;
892 *ResultLength
= sizeof(MEMORY_BASIC_INFORMATION
);
893 return(STATUS_SUCCESS
);