3 * Copyright (C) 1998, 1999, 2000, 2001 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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * PROJECT: ReactOS kernel
21 * FILE: ntoskrnl/mm/pagefile.c
22 * PURPOSE: Paging file functions
23 * PROGRAMMER: David Welch (welch@mcmail.com)
29 /* INCLUDES *****************************************************************/
35 #if defined (ALLOC_PRAGMA)
36 #pragma alloc_text(INIT, MmInitPagingFile)
39 /* GLOBALS *******************************************************************/
41 #define PAIRS_PER_RUN (1024)
43 /* List of paging files, both used and free */
44 PMMPAGING_FILE MmPagingFile
[MAX_PAGING_FILES
];
46 /* Lock for examining the list of paging files */
47 static KSPIN_LOCK PagingFileListLock
;
49 /* Number of paging files */
50 ULONG MmNumberOfPagingFiles
;
52 /* Number of pages that are available for swapping */
53 PFN_COUNT MiFreeSwapPages
;
55 /* Number of pages that have been allocated for swapping */
56 PFN_COUNT MiUsedSwapPages
;
58 BOOLEAN MmZeroPageFile
;
61 * Number of pages that have been reserved for swapping but not yet allocated
63 static PFN_COUNT MiReservedSwapPages
;
66 * Ratio between reserved and available swap pages, e.g. setting this to five
67 * forces one swap page to be available for every five swap pages that are
68 * reserved. Setting this to zero turns off commit checking altogether.
70 #define MM_PAGEFILE_COMMIT_RATIO (1)
73 * Number of pages that can be used for potentially swapable memory without
74 * pagefile space being reserved. The intention is that this allows smss
75 * to start up and create page files while ordinarily having a commit
78 #define MM_PAGEFILE_COMMIT_GRACE (256)
81 * Translate between a swap entry and a file and offset pair.
83 #define FILE_FROM_ENTRY(i) ((i) & 0x0f)
84 #define OFFSET_FROM_ENTRY(i) ((i) >> 11)
85 #define ENTRY_FROM_FILE_OFFSET(i, j) ((i) | ((j) << 11) | 0x400)
87 /* Make sure there can be only 16 paging files */
88 C_ASSERT(FILE_FROM_ENTRY(0xffffffff) < MAX_PAGING_FILES
);
90 static BOOLEAN MmSwapSpaceMessage
= FALSE
;
92 /* FUNCTIONS *****************************************************************/
96 MmBuildMdlFromPages(PMDL Mdl
, PPFN_NUMBER Pages
)
98 memcpy(Mdl
+ 1, Pages
, sizeof(PFN_NUMBER
) * (PAGE_ROUND_UP(Mdl
->ByteOffset
+Mdl
->ByteCount
)/PAGE_SIZE
));
100 /* FIXME: this flag should be set by the caller perhaps? */
101 Mdl
->MdlFlags
|= MDL_IO_PAGE_READ
;
107 MmIsFileObjectAPagingFile(PFILE_OBJECT FileObject
)
111 /* Loop through all the paging files */
112 for (i
= 0; i
< MmNumberOfPagingFiles
; i
++)
114 /* Check if this is one of them */
115 if (MmPagingFile
[i
]->FileObject
== FileObject
) return TRUE
;
124 MmShowOutOfSpaceMessagePagingFile(VOID
)
126 if (!MmSwapSpaceMessage
)
128 DPRINT1("MM: Out of swap space.\n");
129 MmSwapSpaceMessage
= TRUE
;
135 MmWriteToSwapPage(SWAPENTRY SwapEntry
, PFN_NUMBER Page
)
139 LARGE_INTEGER file_offset
;
140 IO_STATUS_BLOCK Iosb
;
143 UCHAR MdlBase
[sizeof(MDL
) + sizeof(ULONG
)];
144 PMDL Mdl
= (PMDL
)MdlBase
;
146 DPRINT("MmWriteToSwapPage\n");
150 KeBugCheck(MEMORY_MANAGEMENT
);
151 return(STATUS_UNSUCCESSFUL
);
154 i
= FILE_FROM_ENTRY(SwapEntry
);
155 offset
= OFFSET_FROM_ENTRY(SwapEntry
) - 1;
157 if (MmPagingFile
[i
]->FileObject
== NULL
||
158 MmPagingFile
[i
]->FileObject
->DeviceObject
== NULL
)
160 DPRINT1("Bad paging file 0x%.8X\n", SwapEntry
);
161 KeBugCheck(MEMORY_MANAGEMENT
);
164 MmInitializeMdl(Mdl
, NULL
, PAGE_SIZE
);
165 MmBuildMdlFromPages(Mdl
, &Page
);
166 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
168 file_offset
.QuadPart
= offset
* PAGE_SIZE
;
170 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
171 Status
= IoSynchronousPageWrite(MmPagingFile
[i
]->FileObject
,
176 if (Status
== STATUS_PENDING
)
178 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
179 Status
= Iosb
.Status
;
182 if (Mdl
->MdlFlags
& MDL_MAPPED_TO_SYSTEM_VA
)
184 MmUnmapLockedPages (Mdl
->MappedSystemVa
, Mdl
);
192 MmReadFromSwapPage(SWAPENTRY SwapEntry
, PFN_NUMBER Page
)
194 return MiReadPageFile(Page
, FILE_FROM_ENTRY(SwapEntry
), OFFSET_FROM_ENTRY(SwapEntry
) - 1);
200 _In_ PFN_NUMBER Page
,
201 _In_ ULONG PageFileIndex
,
202 _In_ ULONG_PTR PageFileOffset
)
204 LARGE_INTEGER file_offset
;
205 IO_STATUS_BLOCK Iosb
;
208 UCHAR MdlBase
[sizeof(MDL
) + sizeof(ULONG
)];
209 PMDL Mdl
= (PMDL
)MdlBase
;
210 PMMPAGING_FILE PagingFile
;
212 DPRINT("MiReadSwapFile\n");
214 if (PageFileOffset
== 0)
216 KeBugCheck(MEMORY_MANAGEMENT
);
217 return(STATUS_UNSUCCESSFUL
);
220 ASSERT(PageFileIndex
< MAX_PAGING_FILES
);
222 PagingFile
= MmPagingFile
[PageFileIndex
];
224 if (PagingFile
->FileObject
== NULL
|| PagingFile
->FileObject
->DeviceObject
== NULL
)
226 DPRINT1("Bad paging file %u\n", PageFileIndex
);
227 KeBugCheck(MEMORY_MANAGEMENT
);
230 MmInitializeMdl(Mdl
, NULL
, PAGE_SIZE
);
231 MmBuildMdlFromPages(Mdl
, &Page
);
232 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
234 file_offset
.QuadPart
= PageFileOffset
* PAGE_SIZE
;
236 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
237 Status
= IoPageRead(PagingFile
->FileObject
,
242 if (Status
== STATUS_PENDING
)
244 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
245 Status
= Iosb
.Status
;
247 if (Mdl
->MdlFlags
& MDL_MAPPED_TO_SYSTEM_VA
)
249 MmUnmapLockedPages (Mdl
->MappedSystemVa
, Mdl
);
257 MmInitPagingFile(VOID
)
261 KeInitializeSpinLock(&PagingFileListLock
);
265 MiReservedSwapPages
= 0;
267 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
269 MmPagingFile
[i
] = NULL
;
271 MmNumberOfPagingFiles
= 0;
276 MmFreeSwapPage(SWAPENTRY Entry
)
281 PMMPAGING_FILE PagingFile
;
283 i
= FILE_FROM_ENTRY(Entry
);
284 off
= OFFSET_FROM_ENTRY(Entry
) - 1;
286 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
288 PagingFile
= MmPagingFile
[i
];
289 if (PagingFile
== NULL
)
291 KeBugCheck(MEMORY_MANAGEMENT
);
294 RtlClearBit(PagingFile
->AllocMap
, off
>> 5);
296 PagingFile
->FreePages
++;
297 PagingFile
->UsedPages
--;
302 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
307 MmAllocSwapPage(VOID
)
314 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
316 if (MiFreeSwapPages
== 0)
318 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
322 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
324 if (MmPagingFile
[i
] != NULL
&&
325 MmPagingFile
[i
]->FreePages
>= 1)
327 off
= RtlFindClearBitsAndSet(MmPagingFile
[i
]->AllocMap
, 1, 0);
328 if (off
== 0xFFFFFFFF)
330 KeBugCheck(MEMORY_MANAGEMENT
);
331 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
332 return(STATUS_UNSUCCESSFUL
);
336 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
338 entry
= ENTRY_FROM_FILE_OFFSET(i
, off
+ 1);
343 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
344 KeBugCheck(MEMORY_MANAGEMENT
);
349 NtCreatePagingFile(IN PUNICODE_STRING FileName
,
350 IN PLARGE_INTEGER InitialSize
,
351 IN PLARGE_INTEGER MaximumSize
,
355 OBJECT_ATTRIBUTES ObjectAttributes
;
357 IO_STATUS_BLOCK IoStatus
;
358 PFILE_OBJECT FileObject
;
359 PMMPAGING_FILE PagingFile
;
363 KPROCESSOR_MODE PreviousMode
;
364 UNICODE_STRING PageFileName
;
365 LARGE_INTEGER SafeInitialSize
, SafeMaximumSize
, AllocationSize
;
366 FILE_FS_DEVICE_INFORMATION FsDeviceInfo
;
367 SECURITY_DESCRIPTOR SecurityDescriptor
;
371 DPRINT("NtCreatePagingFile(FileName %wZ, InitialSize %I64d)\n",
372 FileName
, InitialSize
->QuadPart
);
374 if (MmNumberOfPagingFiles
>= MAX_PAGING_FILES
)
376 return STATUS_TOO_MANY_PAGING_FILES
;
379 PreviousMode
= ExGetPreviousMode();
381 if (PreviousMode
!= KernelMode
)
383 if (SeSinglePrivilegeCheck(SeCreatePagefilePrivilege
, PreviousMode
) != TRUE
)
385 return STATUS_PRIVILEGE_NOT_HELD
;
390 SafeInitialSize
= ProbeForReadLargeInteger(InitialSize
);
391 SafeMaximumSize
= ProbeForReadLargeInteger(MaximumSize
);
393 PageFileName
.Length
= FileName
->Length
;
394 PageFileName
.MaximumLength
= FileName
->MaximumLength
;
395 PageFileName
.Buffer
= FileName
->Buffer
;
397 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
399 /* Return the exception code */
400 _SEH2_YIELD(return _SEH2_GetExceptionCode());
406 SafeInitialSize
= *InitialSize
;
407 SafeMaximumSize
= *MaximumSize
;
409 PageFileName
.Length
= FileName
->Length
;
410 PageFileName
.MaximumLength
= FileName
->MaximumLength
;
411 PageFileName
.Buffer
= FileName
->Buffer
;
414 /* Pagefiles can't be larger than 4GB and ofcourse the minimum should be
415 smaller than the maximum */
416 if (0 != SafeInitialSize
.u
.HighPart
)
418 return STATUS_INVALID_PARAMETER_2
;
420 if (0 != SafeMaximumSize
.u
.HighPart
)
422 return STATUS_INVALID_PARAMETER_3
;
424 if (SafeMaximumSize
.u
.LowPart
< SafeInitialSize
.u
.LowPart
)
426 return STATUS_INVALID_PARAMETER_MIX
;
429 /* Validate name length */
430 if (PageFileName
.Length
> 128 * sizeof(WCHAR
))
432 return STATUS_OBJECT_NAME_INVALID
;
435 /* We won't care about any potential UNICODE_NULL */
436 PageFileName
.MaximumLength
= PageFileName
.Length
;
437 /* Allocate a buffer to keep name copy */
438 Buffer
= ExAllocatePoolWithTag(PagedPool
, PageFileName
.Length
, TAG_MM
);
441 return STATUS_INSUFFICIENT_RESOURCES
;
445 if (PreviousMode
!= KernelMode
)
449 if (PageFileName
.Length
!= 0)
451 ProbeForRead(PageFileName
.Buffer
, PageFileName
.Length
, sizeof(WCHAR
));
454 RtlCopyMemory(Buffer
, PageFileName
.Buffer
, PageFileName
.Length
);
456 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
458 ExFreePoolWithTag(Buffer
, TAG_MM
);
460 /* Return the exception code */
461 _SEH2_YIELD(return _SEH2_GetExceptionCode());
467 RtlCopyMemory(Buffer
, PageFileName
.Buffer
, PageFileName
.Length
);
470 /* Erase caller's buffer with ours */
471 PageFileName
.Buffer
= Buffer
;
473 /* Create the security descriptor for the page file */
474 Status
= RtlCreateSecurityDescriptor(&SecurityDescriptor
, SECURITY_DESCRIPTOR_REVISION
);
475 if (!NT_SUCCESS(Status
))
477 ExFreePoolWithTag(Buffer
, TAG_MM
);
481 /* Create the DACL: we will only allow two SIDs */
482 Count
= sizeof(ACL
) + (sizeof(ACE
) + RtlLengthSid(SeLocalSystemSid
)) +
483 (sizeof(ACE
) + RtlLengthSid(SeAliasAdminsSid
));
484 Dacl
= ExAllocatePoolWithTag(PagedPool
, Count
, 'lcaD');
487 ExFreePoolWithTag(Buffer
, TAG_MM
);
488 return STATUS_INSUFFICIENT_RESOURCES
;
491 /* Initialize the DACL */
492 Status
= RtlCreateAcl(Dacl
, Count
, ACL_REVISION
);
493 if (!NT_SUCCESS(Status
))
495 ExFreePoolWithTag(Dacl
, 'lcaD');
496 ExFreePoolWithTag(Buffer
, TAG_MM
);
500 /* Grant full access to admins */
501 Status
= RtlAddAccessAllowedAce(Dacl
, ACL_REVISION
, FILE_ALL_ACCESS
, SeAliasAdminsSid
);
502 if (!NT_SUCCESS(Status
))
504 ExFreePoolWithTag(Dacl
, 'lcaD');
505 ExFreePoolWithTag(Buffer
, TAG_MM
);
509 /* Grant full access to SYSTEM */
510 Status
= RtlAddAccessAllowedAce(Dacl
, ACL_REVISION
, FILE_ALL_ACCESS
, SeLocalSystemSid
);
511 if (!NT_SUCCESS(Status
))
513 ExFreePoolWithTag(Dacl
, 'lcaD');
514 ExFreePoolWithTag(Buffer
, TAG_MM
);
518 /* Attach the DACL to the security descriptor */
519 Status
= RtlSetDaclSecurityDescriptor(&SecurityDescriptor
, TRUE
, Dacl
, FALSE
);
520 if (!NT_SUCCESS(Status
))
522 ExFreePoolWithTag(Dacl
, 'lcaD');
523 ExFreePoolWithTag(Buffer
, TAG_MM
);
527 InitializeObjectAttributes(&ObjectAttributes
,
531 &SecurityDescriptor
);
533 /* Make sure we can at least store a complete page:
534 * If we have 2048 BytesPerAllocationUnit (FAT16 < 128MB) there is
535 * a problem if the paging file is fragmented. Suppose the first cluster
536 * of the paging file is cluster 3042 but cluster 3043 is NOT part of the
537 * paging file but of another file. We can't write a complete page (4096
538 * bytes) to the physical location of cluster 3042 then. */
539 AllocationSize
.QuadPart
= SafeInitialSize
.QuadPart
+ PAGE_SIZE
;
541 /* First, attempt to replace the page file, if existing */
542 Status
= IoCreateFile(&FileHandle
,
543 SYNCHRONIZE
| WRITE_DAC
| FILE_READ_DATA
| FILE_WRITE_DATA
,
547 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
550 FILE_DELETE_ON_CLOSE
| FILE_NO_COMPRESSION
| FILE_NO_INTERMEDIATE_BUFFERING
,
555 SL_OPEN_PAGING_FILE
| IO_NO_PARAMETER_CHECKING
);
556 /* If we failed, relax a bit constraints, someone may be already holding the
557 * the file, so share write, don't attempt to replace and don't delete on close
558 * (basically, don't do anything conflicting)
559 * This can happen if the caller attempts to extend a page file.
561 if (!NT_SUCCESS(Status
))
565 Status
= IoCreateFile(&FileHandle
,
566 SYNCHRONIZE
| FILE_WRITE_DATA
,
570 FILE_ATTRIBUTE_SYSTEM
| FILE_ATTRIBUTE_HIDDEN
,
571 FILE_SHARE_WRITE
| FILE_SHARE_READ
,
573 FILE_NO_COMPRESSION
| FILE_NO_INTERMEDIATE_BUFFERING
,
578 SL_OPEN_PAGING_FILE
| IO_NO_PARAMETER_CHECKING
);
579 if (!NT_SUCCESS(Status
))
581 ExFreePoolWithTag(Dacl
, 'lcaD');
582 ExFreePoolWithTag(Buffer
, TAG_MM
);
586 /* We opened it! Check we are that "someone" ;-)
587 * First, get the opened file object.
589 Status
= ObReferenceObjectByHandle(FileHandle
,
590 FILE_READ_DATA
| FILE_WRITE_DATA
,
595 if (!NT_SUCCESS(Status
))
598 ExFreePoolWithTag(Dacl
, 'lcaD');
599 ExFreePoolWithTag(Buffer
, TAG_MM
);
603 /* Find if it matches a previous page file */
605 if (MmNumberOfPagingFiles
> 0)
609 while (MmPagingFile
[i
]->FileObject
->SectionObjectPointer
!= FileObject
->SectionObjectPointer
)
612 if (i
>= MmNumberOfPagingFiles
)
618 /* This is the matching page file */
619 PagingFile
= MmPagingFile
[i
];
622 /* If we didn't find the page file, fail */
623 if (PagingFile
== NULL
)
625 ObDereferenceObject(FileObject
);
627 ExFreePoolWithTag(Dacl
, 'lcaD');
628 ExFreePoolWithTag(Buffer
, TAG_MM
);
629 return STATUS_NOT_FOUND
;
632 /* FIXME: implement parameters checking and page file extension */
635 ObDereferenceObject(FileObject
);
637 ExFreePoolWithTag(Dacl
, 'lcaD');
638 ExFreePoolWithTag(Buffer
, TAG_MM
);
639 return STATUS_NOT_IMPLEMENTED
;
642 if (!NT_SUCCESS(Status
))
644 DPRINT1("Failed creating page file: %lx\n", Status
);
645 ExFreePoolWithTag(Dacl
, 'lcaD');
646 ExFreePoolWithTag(Buffer
, TAG_MM
);
650 /* Set the security descriptor */
651 if (NT_SUCCESS(IoStatus
.Status
))
653 Status
= ZwSetSecurityObject(FileHandle
, DACL_SECURITY_INFORMATION
, &SecurityDescriptor
);
654 if (!NT_SUCCESS(Status
))
656 ExFreePoolWithTag(Dacl
, 'lcaD');
658 ExFreePoolWithTag(Buffer
, TAG_MM
);
663 /* DACL is no longer needed, free it */
664 ExFreePoolWithTag(Dacl
, 'lcaD');
666 /* Set its end of file to initial size */
667 Status
= ZwSetInformationFile(FileHandle
,
670 sizeof(LARGE_INTEGER
),
671 FileEndOfFileInformation
);
672 if (!NT_SUCCESS(Status
) || !NT_SUCCESS(IoStatus
.Status
))
675 ExFreePoolWithTag(Buffer
, TAG_MM
);
679 Status
= ObReferenceObjectByHandle(FileHandle
,
685 if (!NT_SUCCESS(Status
))
688 ExFreePoolWithTag(Buffer
, TAG_MM
);
692 /* Deny page file creation on a floppy disk */
693 FsDeviceInfo
.Characteristics
= 0;
694 IoQueryVolumeInformation(FileObject
, FileFsDeviceInformation
, sizeof(FsDeviceInfo
), &FsDeviceInfo
, &Count
);
695 if (BooleanFlagOn(FsDeviceInfo
.Characteristics
, FILE_FLOPPY_DISKETTE
))
697 ObDereferenceObject(FileObject
);
699 ExFreePoolWithTag(Buffer
, TAG_MM
);
700 return STATUS_FLOPPY_VOLUME
;
703 PagingFile
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(*PagingFile
), TAG_MM
);
704 if (PagingFile
== NULL
)
706 ObDereferenceObject(FileObject
);
708 ExFreePoolWithTag(Buffer
, TAG_MM
);
709 return STATUS_INSUFFICIENT_RESOURCES
;
712 RtlZeroMemory(PagingFile
, sizeof(*PagingFile
));
714 PagingFile
->FileHandle
= FileHandle
;
715 PagingFile
->FileObject
= FileObject
;
716 PagingFile
->MaximumSize
.QuadPart
= SafeMaximumSize
.QuadPart
;
717 PagingFile
->CurrentSize
.QuadPart
= SafeInitialSize
.QuadPart
;
718 PagingFile
->FreePages
= (ULONG
)(SafeInitialSize
.QuadPart
/ PAGE_SIZE
);
719 PagingFile
->UsedPages
= 0;
720 PagingFile
->PageFileName
= PageFileName
;
722 AllocMapSize
= sizeof(RTL_BITMAP
) + (((PagingFile
->FreePages
+ 31) / 32) * sizeof(ULONG
));
723 PagingFile
->AllocMap
= ExAllocatePoolWithTag(NonPagedPool
,
726 if (PagingFile
->AllocMap
== NULL
)
728 ExFreePoolWithTag(PagingFile
, TAG_MM
);
729 ObDereferenceObject(FileObject
);
731 ExFreePoolWithTag(Buffer
, TAG_MM
);
732 return STATUS_INSUFFICIENT_RESOURCES
;
735 RtlInitializeBitMap(PagingFile
->AllocMap
,
736 (PULONG
)(PagingFile
->AllocMap
+ 1),
737 (ULONG
)(PagingFile
->FreePages
));
738 RtlClearAllBits(PagingFile
->AllocMap
);
740 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
741 ASSERT(MmPagingFile
[MmNumberOfPagingFiles
] == NULL
);
742 MmPagingFile
[MmNumberOfPagingFiles
] = PagingFile
;
743 MmNumberOfPagingFiles
++;
744 MiFreeSwapPages
= MiFreeSwapPages
+ PagingFile
->FreePages
;
745 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
747 MmSwapSpaceMessage
= FALSE
;
749 return STATUS_SUCCESS
;