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.8 2002/11/05 20:31:34 hbirr 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
;
139 DPRINT("MmPageOutVirtualMemory(Address 0x%.8X) PID %d\n",
140 Address
, MemoryArea
->Process
->UniqueProcessId
);
143 * Check for paging out from a deleted virtual memory area.
145 if (MemoryArea
->DeleteInProgress
)
147 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
148 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
149 MmReleasePageOp(PageOp
);
150 return(STATUS_UNSUCCESSFUL
);
154 * Disable the virtual mapping.
157 MmDisableVirtualMapping(MemoryArea
->Process
, Address
,
158 &WasDirty
, &PhysicalAddress
);
160 flProtect
= MmGetPageProtect(MemoryArea
->Process
, Address
);
161 MmDeleteVirtualMapping(MemoryArea
->Process
, Address
, FALSE
,
162 &WasDirty
, &PhysicalAddress
);
165 if (PhysicalAddress
.QuadPart
== 0)
171 * Paging out non-dirty data is easy.
176 MmDeleteVirtualMapping(MemoryArea
->Process
, Address
, FALSE
, NULL
, NULL
);
178 MmDeleteAllRmaps(PhysicalAddress
, NULL
, NULL
);
179 if ((SwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddress
)) != 0)
181 MmCreatePageFileMapping(MemoryArea
->Process
, Address
, SwapEntry
);
182 MmSetSavedSwapEntryPage(PhysicalAddress
, 0);
184 MmReleasePageMemoryConsumer(MC_USER
, PhysicalAddress
);
185 PageOp
->Status
= STATUS_SUCCESS
;
186 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
187 MmReleasePageOp(PageOp
);
188 return(STATUS_SUCCESS
);
192 * If necessary, allocate an entry in the paging file for this page
194 SwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddress
);
197 SwapEntry
= MmAllocSwapPage();
200 MmShowOutOfSpaceMessagePagingFile();
202 MmEnableVirtualMapping(MemoryArea
->Process
, Address
);
204 MmCreateVirtualMapping(MemoryArea
->Process
, Address
,
205 flProtect
, PhysicalAddress
, TRUE
);
207 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
208 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
209 MmReleasePageOp(PageOp
);
210 return(STATUS_UNSUCCESSFUL
);
215 * Write the page to the pagefile
217 Mdl
= MmCreateMdl(NULL
, NULL
, PAGE_SIZE
);
218 MmBuildMdlFromPages(Mdl
, &PhysicalAddress
.u
.LowPart
);
219 Status
= MmWriteToSwapPage(SwapEntry
, Mdl
);
220 if (!NT_SUCCESS(Status
))
222 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
225 MmEnableVirtualMapping(MemoryArea
->Process
, Address
);
227 MmCreateVirtualMapping(MemoryArea
->Process
, Address
,
228 flProtect
, PhysicalAddress
, TRUE
);
230 PageOp
->Status
= STATUS_UNSUCCESSFUL
;
231 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
232 MmReleasePageOp(PageOp
);
233 return(STATUS_UNSUCCESSFUL
);
237 * Otherwise we have succeeded, free the page
239 DPRINT("MM: Swapped out virtual memory page 0x%.8X!\n", PhysicalAddress
);
241 MmDeleteVirtualMapping(MemoryArea
->Process
, Address
, FALSE
, NULL
, NULL
);
243 MmCreatePageFileMapping(MemoryArea
->Process
, Address
, SwapEntry
);
244 MmDeleteAllRmaps(PhysicalAddress
, NULL
, NULL
);
245 MmSetSavedSwapEntryPage(PhysicalAddress
, 0);
246 MmReleasePageMemoryConsumer(MC_USER
, PhysicalAddress
);
247 PageOp
->Status
= STATUS_SUCCESS
;
248 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
249 MmReleasePageOp(PageOp
);
250 return(STATUS_SUCCESS
);
254 MmNotPresentFaultVirtualMemory(PMADDRESS_SPACE AddressSpace
,
255 MEMORY_AREA
* MemoryArea
,
259 * FUNCTION: Move data into memory to satisfy a page not present fault
261 * AddressSpace = Address space within which the fault occurred
262 * MemoryArea = The memory area within which the fault occurred
263 * Address = The absolute address of fault
265 * NOTES: This function is called with the address space lock held.
268 PHYSICAL_ADDRESS Page
;
274 * There is a window between taking the page fault and locking the
275 * address space when another thread could load the page so we check
278 if (MmIsPagePresent(NULL
, Address
))
282 MmLockPage(MmGetPhysicalAddressForProcess(NULL
, Address
));
284 return(STATUS_SUCCESS
);
288 * Check for the virtual memory area being deleted.
290 if (MemoryArea
->DeleteInProgress
)
292 return(STATUS_UNSUCCESSFUL
);
296 * Get the segment corresponding to the virtual address
298 Region
= MmFindRegion(MemoryArea
->BaseAddress
,
299 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
301 if (Region
->Type
== MEM_RESERVE
)
303 return(STATUS_UNSUCCESSFUL
);
307 * Get or create a page operation
309 PageOp
= MmGetPageOp(MemoryArea
, (ULONG
)PsGetCurrentProcessId(),
310 (PVOID
)PAGE_ROUND_DOWN(Address
), NULL
, 0,
314 DPRINT1("MmGetPageOp failed");
319 * Check if someone else is already handling this fault, if so wait
322 if (PageOp
->Thread
!= PsGetCurrentThread())
324 MmUnlockAddressSpace(AddressSpace
);
325 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
331 * Check for various strange conditions
333 if (Status
!= STATUS_SUCCESS
)
335 DPRINT1("Failed to wait for page op\n");
338 if (PageOp
->Status
== STATUS_PENDING
)
340 DPRINT1("Woke for page op before completion\n");
344 * If this wasn't a pagein then we need to restart the handling
346 if (PageOp
->OpType
!= MM_PAGEOP_PAGEIN
)
348 MmLockAddressSpace(AddressSpace
);
349 MmReleasePageOp(PageOp
);
350 return(STATUS_MM_RESTART_OPERATION
);
353 * If the thread handling this fault has failed then we don't retry
355 if (!NT_SUCCESS(PageOp
->Status
))
357 MmLockAddressSpace(AddressSpace
);
358 MmReleasePageOp(PageOp
);
361 MmLockAddressSpace(AddressSpace
);
364 MmLockPage(MmGetPhysicalAddressForProcess(NULL
, Address
));
366 MmReleasePageOp(PageOp
);
367 return(STATUS_SUCCESS
);
371 * Try to allocate a page
373 Status
= MmRequestPageMemoryConsumer(MC_USER
, FALSE
, &Page
);
374 if (Status
== STATUS_NO_MEMORY
)
376 MmUnlockAddressSpace(AddressSpace
);
377 Status
= MmRequestPageMemoryConsumer(MC_USER
, TRUE
, &Page
);
378 MmLockAddressSpace(AddressSpace
);
382 * Handle swapped out pages.
384 if (MmIsPageSwapEntry(NULL
, Address
))
389 MmDeletePageFileMapping(NULL
, Address
, &SwapEntry
);
390 Mdl
= MmCreateMdl(NULL
, NULL
, PAGE_SIZE
);
391 MmBuildMdlFromPages(Mdl
, (PULONG
)&Page
);
392 Status
= MmReadFromSwapPage(SwapEntry
, Mdl
);
393 if (!NT_SUCCESS(Status
))
397 MmSetSavedSwapEntryPage(Page
, SwapEntry
);
401 * Set the page. If we fail because we are out of memory then
404 Status
= MmCreateVirtualMapping(PsGetCurrentProcess(),
406 MemoryArea
->Attributes
,
409 while (Status
== STATUS_NO_MEMORY
)
411 MmUnlockAddressSpace(AddressSpace
);
412 Status
= MmCreateVirtualMapping(PsGetCurrentProcess(),
414 MemoryArea
->Attributes
,
417 MmLockAddressSpace(AddressSpace
);
419 if (!NT_SUCCESS(Status
))
421 DPRINT1("MmCreateVirtualMapping failed, not out of memory\n");
427 * Add the page to the process's working set
429 MmInsertRmap(Page
, PsGetCurrentProcess(), (PVOID
)PAGE_ROUND_DOWN(Address
));
432 * Finish the operation
436 MmLockPage(MmGetPhysicalAddressForProcess(NULL
, Address
));
438 PageOp
->Status
= STATUS_SUCCESS
;
439 KeSetEvent(&PageOp
->CompletionEvent
, IO_NO_INCREMENT
, FALSE
);
440 MmReleasePageOp(PageOp
);
441 return(STATUS_SUCCESS
);
445 MmModifyAttributes(PMADDRESS_SPACE AddressSpace
,
453 * FUNCTION: Modify the attributes of a memory region
457 * If we are switching a previously committed region to reserved then
458 * free any allocated pages within the region
460 if (NewType
== MEM_RESERVE
&& OldType
== MEM_COMMIT
)
464 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
466 LARGE_INTEGER PhysicalAddr
;
468 if (MmIsPageSwapEntry(AddressSpace
->Process
,
469 BaseAddress
+ (i
* PAGE_SIZE
)))
473 MmDeletePageFileMapping(AddressSpace
->Process
,
474 BaseAddress
+ (i
* PAGE_SIZE
),
476 MmFreeSwapPage(SwapEntry
);
480 PhysicalAddr
= MmGetPhysicalAddress(BaseAddress
+ (i
*PAGE_SIZE
));
481 MmDeleteVirtualMapping(AddressSpace
->Process
,
482 BaseAddress
+ (i
*PAGE_SIZE
),
484 if (PhysicalAddr
.QuadPart
!= 0)
486 SWAPENTRY SavedSwapEntry
;
487 SavedSwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddr
);
488 if (SavedSwapEntry
!= 0)
490 MmFreeSwapPage(SavedSwapEntry
);
491 MmSetSavedSwapEntryPage(PhysicalAddr
, 0);
493 MmDeleteRmap(PhysicalAddr
, AddressSpace
->Process
,
494 BaseAddress
+ (i
* PAGE_SIZE
));
495 MmReleasePageMemoryConsumer(MC_USER
, PhysicalAddr
);
502 * If we are changing the protection attributes of a committed region then
503 * alter the attributes for any allocated pages within the region
505 if (NewType
== MEM_COMMIT
&& OldType
== MEM_COMMIT
&&
506 OldProtect
!= NewProtect
)
510 for (i
=0; i
< PAGE_ROUND_UP(RegionSize
)/PAGE_SIZE
; i
++)
512 if (MmIsPagePresent(AddressSpace
->Process
,
513 BaseAddress
+ (i
*PAGE_SIZE
)))
515 MmSetPageProtect(AddressSpace
->Process
,
516 BaseAddress
+ (i
*PAGE_SIZE
),
524 NtAllocateVirtualMemory(IN HANDLE ProcessHandle
,
525 IN OUT PVOID
* UBaseAddress
,
527 IN OUT PULONG URegionSize
,
528 IN ULONG AllocationType
,
531 * FUNCTION: Allocates a block of virtual memory in the process address space
533 * ProcessHandle = The handle of the process which owns the virtual memory
534 * BaseAddress = A pointer to the virtual memory allocated. If you
535 * supply a non zero value the system will try to
536 * allocate the memory at the address supplied. It round
537 * it down to a multiple of the page size.
538 * ZeroBits = (OPTIONAL) You can specify the number of high order bits
539 * that must be zero, ensuring that the memory will be
540 * allocated at a address below a certain value.
541 * RegionSize = The number of bytes to allocate
542 * AllocationType = Indicates the type of virtual memory you like to
543 * allocated, can be a combination of MEM_COMMIT,
544 * MEM_RESERVE, MEM_RESET, MEM_TOP_DOWN.
545 * Protect = Indicates the protection type of the pages allocated, can be
546 * a combination of PAGE_READONLY, PAGE_READWRITE,
547 * PAGE_EXECUTE_READ, PAGE_EXECUTE_READWRITE, PAGE_GUARD,
553 MEMORY_AREA
* MemoryArea
;
556 PMADDRESS_SPACE AddressSpace
;
562 DPRINT("NtAllocateVirtualMemory(*UBaseAddress %x, "
563 "ZeroBits %d, *URegionSize %x, AllocationType %x, Protect %x)\n",
564 *UBaseAddress
,ZeroBits
,*URegionSize
,AllocationType
,
568 * Check the validity of the parameters
570 if ((Protect
& PAGE_FLAGS_VALID_FROM_USER_MODE
) != Protect
)
572 return(STATUS_INVALID_PAGE_PROTECTION
);
574 if ((AllocationType
& (MEM_COMMIT
| MEM_RESERVE
)) == 0)
576 return(STATUS_INVALID_PARAMETER
);
579 PBaseAddress
= *UBaseAddress
;
580 PRegionSize
= *URegionSize
;
582 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(PBaseAddress
);
583 RegionSize
= PAGE_ROUND_UP(PBaseAddress
+ PRegionSize
) -
584 PAGE_ROUND_DOWN(PBaseAddress
);
586 Status
= ObReferenceObjectByHandle(ProcessHandle
,
587 PROCESS_VM_OPERATION
,
592 if (!NT_SUCCESS(Status
))
594 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
598 Type
= (AllocationType
& MEM_COMMIT
) ? MEM_COMMIT
: MEM_RESERVE
;
599 DPRINT("Type %x\n", Type
);
601 AddressSpace
= &Process
->AddressSpace
;
602 MmLockAddressSpace(AddressSpace
);
604 if (PBaseAddress
!= 0)
606 MemoryArea
= MmOpenMemoryAreaByAddress(AddressSpace
,
609 if (MemoryArea
!= NULL
&&
610 MemoryArea
->Type
== MEMORY_AREA_VIRTUAL_MEMORY
&&
611 MemoryArea
->Length
>= RegionSize
)
614 MmAlterRegion(AddressSpace
,
615 MemoryArea
->BaseAddress
,
616 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
617 PBaseAddress
, RegionSize
,
618 Type
, Protect
, MmModifyAttributes
);
619 MmUnlockAddressSpace(AddressSpace
);
620 ObDereferenceObject(Process
);
621 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
624 else if (MemoryArea
!= NULL
)
626 MmUnlockAddressSpace(AddressSpace
);
627 ObDereferenceObject(Process
);
628 return(STATUS_UNSUCCESSFUL
);
632 Status
= MmCreateMemoryArea(Process
,
633 &Process
->AddressSpace
,
634 MEMORY_AREA_VIRTUAL_MEMORY
,
641 if (!NT_SUCCESS(Status
))
643 MmUnlockAddressSpace(AddressSpace
);
644 ObDereferenceObject(Process
);
645 DPRINT("NtAllocateVirtualMemory() = %x\n",Status
);
648 MmInitialiseRegion(&MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
649 RegionSize
, Type
, Protect
);
651 if ((AllocationType
& MEM_COMMIT
) &&
652 ((Protect
& PAGE_READWRITE
) ||
653 (Protect
& PAGE_EXECUTE_READWRITE
)))
655 MmReserveSwapPages(RegionSize
);
658 *UBaseAddress
= BaseAddress
;
659 *URegionSize
= RegionSize
;
660 DPRINT("*UBaseAddress %x *URegionSize %x\n", BaseAddress
, RegionSize
);
662 MmUnlockAddressSpace(AddressSpace
);
663 ObDereferenceObject(Process
);
664 return(STATUS_SUCCESS
);
668 MmFreeVirtualMemoryPage(PVOID Context
,
669 MEMORY_AREA
* MemoryArea
,
671 PHYSICAL_ADDRESS PhysicalAddr
,
675 PEPROCESS Process
= (PEPROCESS
)Context
;
677 if (PhysicalAddr
.QuadPart
!= 0)
679 SWAPENTRY SavedSwapEntry
;
680 SavedSwapEntry
= MmGetSavedSwapEntryPage(PhysicalAddr
);
681 if (SavedSwapEntry
!= 0)
683 MmFreeSwapPage(SavedSwapEntry
);
684 MmSetSavedSwapEntryPage(PhysicalAddr
, 0);
686 MmDeleteRmap(PhysicalAddr
, Process
, Address
);
687 MmReleasePageMemoryConsumer(MC_USER
, PhysicalAddr
);
689 else if (SwapEntry
!= 0)
691 MmFreeSwapPage(SwapEntry
);
696 MmFreeVirtualMemory(PEPROCESS Process
,
697 PMEMORY_AREA MemoryArea
)
699 PLIST_ENTRY current_entry
;
703 DPRINT("MmFreeVirtualMemory(Process %p MemoryArea %p)\n", Process
,
706 /* Mark this memory area as about to be deleted. */
707 MemoryArea
->DeleteInProgress
= TRUE
;
710 * Wait for any ongoing paging operations. Notice that since we have
711 * flagged this memory area as deleted no more page ops will be added.
713 if (MemoryArea
->PageOpCount
> 0)
715 for (i
= 0; i
< PAGE_ROUND_UP(MemoryArea
->Length
) / PAGE_SIZE
; i
++)
719 if (MemoryArea
->PageOpCount
== 0)
724 PageOp
= MmCheckForPageOp(MemoryArea
, Process
->UniqueProcessId
,
725 MemoryArea
->BaseAddress
+ (i
* PAGE_SIZE
),
730 MmUnlockAddressSpace(&Process
->AddressSpace
);
731 Status
= KeWaitForSingleObject(&PageOp
->CompletionEvent
,
736 if (Status
!= STATUS_SUCCESS
)
738 DPRINT1("Failed to wait for page op\n");
741 MmLockAddressSpace(&Process
->AddressSpace
);
742 MmReleasePageOp(PageOp
);
747 /* Free all the individual segments. */
748 current_entry
= MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
.Flink
;
749 while (current_entry
!= &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
)
751 current
= CONTAINING_RECORD(current_entry
, MM_REGION
, RegionListEntry
);
752 current_entry
= current_entry
->Flink
;
756 /* Actually free the memory area. */
757 MmFreeMemoryArea(&Process
->AddressSpace
,
758 MemoryArea
->BaseAddress
,
760 MmFreeVirtualMemoryPage
,
765 NtFreeVirtualMemory(IN HANDLE ProcessHandle
,
766 IN PVOID
* PBaseAddress
,
767 IN PULONG PRegionSize
,
770 * FUNCTION: Frees a range of virtual memory
772 * ProcessHandle = Points to the process that allocated the virtual
774 * BaseAddress = Points to the memory address, rounded down to a
775 * multiple of the pagesize
776 * RegionSize = Limits the range to free, rounded up to a multiple of
778 * FreeType = Can be one of the values: MEM_DECOMMIT, or MEM_RELEASE
782 MEMORY_AREA
* MemoryArea
;
785 PMADDRESS_SPACE AddressSpace
;
789 DPRINT("NtFreeVirtualMemory(ProcessHandle %x, *PBaseAddress %x, "
790 "*PRegionSize %x, FreeType %x)\n",ProcessHandle
,*PBaseAddress
,
791 *PRegionSize
,FreeType
);
793 BaseAddress
= (PVOID
)PAGE_ROUND_DOWN((*PBaseAddress
));
794 RegionSize
= PAGE_ROUND_UP((*PBaseAddress
) + (*PRegionSize
)) -
795 PAGE_ROUND_DOWN((*PBaseAddress
));
797 Status
= ObReferenceObjectByHandle(ProcessHandle
,
798 PROCESS_VM_OPERATION
,
803 if (!NT_SUCCESS(Status
))
808 AddressSpace
= &Process
->AddressSpace
;
810 MmLockAddressSpace(AddressSpace
);
811 MemoryArea
= MmOpenMemoryAreaByAddress(AddressSpace
,
813 if (MemoryArea
== NULL
)
815 MmUnlockAddressSpace(AddressSpace
);
816 ObDereferenceObject(Process
);
817 return(STATUS_UNSUCCESSFUL
);
823 /* We can only free a memory area in one step. */
824 if (MemoryArea
->BaseAddress
!= BaseAddress
)
826 MmUnlockAddressSpace(AddressSpace
);
827 ObDereferenceObject(Process
);
828 return(STATUS_UNSUCCESSFUL
);
830 MmFreeVirtualMemory(Process
, MemoryArea
);
831 MmUnlockAddressSpace(AddressSpace
);
832 ObDereferenceObject(Process
);
833 return(STATUS_SUCCESS
);
837 MmAlterRegion(AddressSpace
,
838 MemoryArea
->BaseAddress
,
839 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
845 MmUnlockAddressSpace(AddressSpace
);
846 ObDereferenceObject(Process
);
849 MmUnlockAddressSpace(AddressSpace
);
850 ObDereferenceObject(Process
);
851 return(STATUS_NOT_IMPLEMENTED
);
855 MmProtectAnonMem(PMADDRESS_SPACE AddressSpace
,
856 PMEMORY_AREA MemoryArea
,
865 Region
= MmFindRegion(MemoryArea
->BaseAddress
,
866 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
868 *OldProtect
= Region
->Protect
;
869 Status
= MmAlterRegion(AddressSpace
, MemoryArea
->BaseAddress
,
870 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
871 BaseAddress
, Length
, Region
->Type
, Protect
,
877 MmQueryAnonMem(PMEMORY_AREA MemoryArea
,
879 PMEMORY_BASIC_INFORMATION Info
,
885 Info
->BaseAddress
= (PVOID
)PAGE_ROUND_DOWN(Address
);
887 Region
= MmFindRegion(MemoryArea
->BaseAddress
,
888 &MemoryArea
->Data
.VirtualMemoryData
.RegionListHead
,
889 Address
, &RegionBase
);
890 Info
->AllocationBase
= RegionBase
;
891 Info
->AllocationProtect
= Region
->Protect
; /* FIXME */
892 Info
->RegionSize
= Region
->Length
;
893 Info
->State
= Region
->Type
;
894 Info
->Protect
= Region
->Protect
;
895 Info
->Type
= MEM_PRIVATE
;
896 return(STATUS_SUCCESS
);