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
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * PROJECT: ReactOS kernel
21 * FILE: ntoskrnl/mm/pagefile.c
22 * PURPOSE: Paging file functions
23 * PROGRAMMER: David Welch (welch@mcmail.com)
28 /* INCLUDES *****************************************************************/
34 #if defined (ALLOC_PRAGMA)
35 #pragma alloc_text(INIT, MmInitPagingFile)
40 MiFindExportedRoutineByName(IN PVOID DllBase
,
41 IN PANSI_STRING ExportName
);
43 /* TYPES *********************************************************************/
45 typedef struct _PAGINGFILE
47 LIST_ENTRY PagingFileListEntry
;
48 PFILE_OBJECT FileObject
;
49 LARGE_INTEGER MaximumSize
;
50 LARGE_INTEGER CurrentSize
;
54 KSPIN_LOCK AllocMapLock
;
56 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
58 PAGINGFILE
, *PPAGINGFILE
;
60 typedef struct _RETRIEVEL_DESCRIPTOR_LIST
62 struct _RETRIEVEL_DESCRIPTOR_LIST
* Next
;
63 RETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
65 RETRIEVEL_DESCRIPTOR_LIST
, *PRETRIEVEL_DESCRIPTOR_LIST
;
67 /* GLOBALS *******************************************************************/
69 #define PAIRS_PER_RUN (1024)
71 #define MAX_PAGING_FILES (32)
73 /* List of paging files, both used and free */
74 static PPAGINGFILE PagingFileList
[MAX_PAGING_FILES
];
76 /* Lock for examining the list of paging files */
77 static KSPIN_LOCK PagingFileListLock
;
79 /* Number of paging files */
80 static ULONG MiPagingFileCount
;
82 /* Number of pages that are available for swapping */
83 ULONG MiFreeSwapPages
;
85 /* Number of pages that have been allocated for swapping */
86 ULONG MiUsedSwapPages
;
89 * Number of pages that have been reserved for swapping but not yet allocated
91 static ULONG MiReservedSwapPages
;
94 * Ratio between reserved and available swap pages, e.g. setting this to five
95 * forces one swap page to be available for every five swap pages that are
96 * reserved. Setting this to zero turns off commit checking altogether.
98 #define MM_PAGEFILE_COMMIT_RATIO (1)
101 * Number of pages that can be used for potentially swapable memory without
102 * pagefile space being reserved. The intention is that this allows smss
103 * to start up and create page files while ordinarily having a commit
106 #define MM_PAGEFILE_COMMIT_GRACE (256)
108 static PVOID MmCoreDumpPageFrame
= NULL
;
109 static ULONG MmCoreDumpSize
;
110 static DUMP_POINTERS MmCoreDumpPointers
;
111 static PMM_CORE_DUMP_FUNCTIONS MmCoreDumpFunctions
;
112 static ULONG MmCoreDumpPageFile
= 0xFFFFFFFF;
113 static ROS_QUERY_LCN_MAPPING MmCoreDumpLcnMapping
;
115 ULONG MmCoreDumpType
= MM_CORE_DUMP_TYPE_NONE
;
118 * Translate between a swap entry and a file and offset pair.
120 #define FILE_FROM_ENTRY(i) ((i) >> 24)
121 #define OFFSET_FROM_ENTRY(i) (((i) & 0xffffff) - 1)
122 #define ENTRY_FROM_FILE_OFFSET(i, j) (((i) << 24) | ((j) + 1))
124 static BOOLEAN MmSwapSpaceMessage
= FALSE
;
126 /* FUNCTIONS *****************************************************************/
130 MmBuildMdlFromPages(PMDL Mdl
, PPFN_TYPE Pages
)
132 memcpy(Mdl
+ 1, Pages
, sizeof(PFN_TYPE
) * (PAGE_ROUND_UP(Mdl
->ByteOffset
+Mdl
->ByteCount
)/PAGE_SIZE
));
134 /* FIXME: this flag should be set by the caller perhaps? */
135 Mdl
->MdlFlags
|= MDL_IO_PAGE_READ
;
141 MmIsFileAPagingFile(PFILE_OBJECT FileObject
)
145 /* Loop through all the paging files */
146 for (i
= 0; i
< MiPagingFileCount
; i
++)
148 /* Check if this is one of them */
149 if (PagingFileList
[i
]->FileObject
== FileObject
) return TRUE
;
158 MmShowOutOfSpaceMessagePagingFile(VOID
)
160 if (!MmSwapSpaceMessage
)
162 DPRINT1("MM: Out of swap space.\n");
163 MmSwapSpaceMessage
= TRUE
;
168 MmGetOffsetPageFile(PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
, LARGE_INTEGER Offset
)
170 /* Simple binary search */
171 ULONG first
, last
, mid
;
173 last
= RetrievalPointers
->ExtentCount
- 1;
174 while (first
<= last
)
176 mid
= (last
- first
) / 2 + first
;
177 if (Offset
.QuadPart
< RetrievalPointers
->Extents
[mid
].NextVcn
.QuadPart
)
181 Offset
.QuadPart
+= RetrievalPointers
->Extents
[0].Lcn
.QuadPart
- RetrievalPointers
->StartingVcn
.QuadPart
;
186 if (Offset
.QuadPart
>= RetrievalPointers
->Extents
[mid
-1].NextVcn
.QuadPart
)
188 Offset
.QuadPart
+= RetrievalPointers
->Extents
[mid
].Lcn
.QuadPart
- RetrievalPointers
->Extents
[mid
-1].NextVcn
.QuadPart
;
196 if (mid
== RetrievalPointers
->ExtentCount
- 1)
200 if (Offset
.QuadPart
< RetrievalPointers
->Extents
[mid
+1].NextVcn
.QuadPart
)
202 Offset
.QuadPart
+= RetrievalPointers
->Extents
[mid
+1].Lcn
.QuadPart
- RetrievalPointers
->Extents
[mid
].NextVcn
.QuadPart
;
209 #if defined(__GNUC__)
211 return (LARGE_INTEGER
)0LL;
215 const LARGE_INTEGER dummy
=
226 MmWriteToSwapPage(SWAPENTRY SwapEntry
, PFN_TYPE Page
)
229 LARGE_INTEGER file_offset
;
230 IO_STATUS_BLOCK Iosb
;
233 UCHAR MdlBase
[sizeof(MDL
) + sizeof(ULONG
)];
234 PMDL Mdl
= (PMDL
)MdlBase
;
236 DPRINT("MmWriteToSwapPage\n");
241 return(STATUS_UNSUCCESSFUL
);
244 i
= FILE_FROM_ENTRY(SwapEntry
);
245 offset
= OFFSET_FROM_ENTRY(SwapEntry
);
247 if (i
>= MAX_PAGING_FILES
)
249 DPRINT1("Bad swap entry 0x%.8X\n", SwapEntry
);
252 if (PagingFileList
[i
]->FileObject
== NULL
||
253 PagingFileList
[i
]->FileObject
->DeviceObject
== NULL
)
255 DPRINT1("Bad paging file 0x%.8X\n", SwapEntry
);
259 MmInitializeMdl(Mdl
, NULL
, PAGE_SIZE
);
260 MmBuildMdlFromPages(Mdl
, &Page
);
261 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
263 file_offset
.QuadPart
= offset
* PAGE_SIZE
;
264 file_offset
= MmGetOffsetPageFile(PagingFileList
[i
]->RetrievalPointers
, file_offset
);
266 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
267 Status
= IoSynchronousPageWrite(PagingFileList
[i
]->FileObject
,
272 if (Status
== STATUS_PENDING
)
274 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
275 Status
= Iosb
.Status
;
278 if (Mdl
->MdlFlags
& MDL_MAPPED_TO_SYSTEM_VA
)
280 MmUnmapLockedPages (Mdl
->MappedSystemVa
, Mdl
);
287 MmReadFromSwapPage(SWAPENTRY SwapEntry
, PFN_TYPE Page
)
290 LARGE_INTEGER file_offset
;
291 IO_STATUS_BLOCK Iosb
;
294 UCHAR MdlBase
[sizeof(MDL
) + sizeof(ULONG
)];
295 PMDL Mdl
= (PMDL
)MdlBase
;
297 DPRINT("MmReadFromSwapPage\n");
302 return(STATUS_UNSUCCESSFUL
);
305 i
= FILE_FROM_ENTRY(SwapEntry
);
306 offset
= OFFSET_FROM_ENTRY(SwapEntry
);
308 if (i
>= MAX_PAGING_FILES
)
310 DPRINT1("Bad swap entry 0x%.8X\n", SwapEntry
);
313 if (PagingFileList
[i
]->FileObject
== NULL
||
314 PagingFileList
[i
]->FileObject
->DeviceObject
== NULL
)
316 DPRINT1("Bad paging file 0x%.8X\n", SwapEntry
);
320 MmInitializeMdl(Mdl
, NULL
, PAGE_SIZE
);
321 MmBuildMdlFromPages(Mdl
, &Page
);
322 Mdl
->MdlFlags
|= MDL_PAGES_LOCKED
;
324 file_offset
.QuadPart
= offset
* PAGE_SIZE
;
325 file_offset
= MmGetOffsetPageFile(PagingFileList
[i
]->RetrievalPointers
, file_offset
);
327 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
328 Status
= IoPageRead(PagingFileList
[i
]->FileObject
,
333 if (Status
== STATUS_PENDING
)
335 KeWaitForSingleObject(&Event
, Executive
, KernelMode
, FALSE
, NULL
);
336 Status
= Iosb
.Status
;
338 if (Mdl
->MdlFlags
& MDL_MAPPED_TO_SYSTEM_VA
)
340 MmUnmapLockedPages (Mdl
->MappedSystemVa
, Mdl
);
348 MmInitPagingFile(VOID
)
352 KeInitializeSpinLock(&PagingFileListLock
);
356 MiReservedSwapPages
= 0;
358 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
360 PagingFileList
[i
] = NULL
;
362 MiPagingFileCount
= 0;
365 * Initialize the crash dump support.
367 if (MmCoreDumpType
!= MM_CORE_DUMP_TYPE_NONE
)
369 MmCoreDumpPageFrame
= MmAllocateSection(PAGE_SIZE
, NULL
);
370 if (MmCoreDumpType
== MM_CORE_DUMP_TYPE_FULL
)
372 MmCoreDumpSize
= MmStats
.NrTotalPages
* 4096 + 1024 * 1024;
376 MmCoreDumpSize
= 1024 * 1024;
383 MmReserveSwapPages(ULONG Nr
)
386 ULONG MiAvailSwapPages
;
388 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
390 (MiFreeSwapPages
* MM_PAGEFILE_COMMIT_RATIO
) + MM_PAGEFILE_COMMIT_GRACE
;
391 MiReservedSwapPages
= MiReservedSwapPages
+ Nr
;
392 if ((MM_PAGEFILE_COMMIT_RATIO
!= 0) && (MiAvailSwapPages
< MiReservedSwapPages
))
394 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
397 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
403 MmDereserveSwapPages(ULONG Nr
)
407 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
408 MiReservedSwapPages
= MiReservedSwapPages
- Nr
;
409 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
413 MiAllocPageFromPagingFile(PPAGINGFILE PagingFile
)
418 KeAcquireSpinLock(&PagingFile
->AllocMapLock
, &oldIrql
);
420 for (i
= 0; i
< PagingFile
->AllocMapSize
; i
++)
422 for (j
= 0; j
< 32; j
++)
424 if (!(PagingFile
->AllocMap
[i
] & (1 << j
)))
426 PagingFile
->AllocMap
[i
] |= (1 << j
);
427 PagingFile
->UsedPages
++;
428 PagingFile
->FreePages
--;
429 KeReleaseSpinLock(&PagingFile
->AllocMapLock
, oldIrql
);
430 return((i
* 32) + j
);
435 KeReleaseSpinLock(&PagingFile
->AllocMapLock
, oldIrql
);
441 MmFreeSwapPage(SWAPENTRY Entry
)
447 i
= FILE_FROM_ENTRY(Entry
);
448 off
= OFFSET_FROM_ENTRY(Entry
);
450 if (i
>= MAX_PAGING_FILES
)
452 DPRINT1("Bad swap entry 0x%.8X\n", Entry
);
456 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
457 if (PagingFileList
[i
] == NULL
)
461 KeAcquireSpinLockAtDpcLevel(&PagingFileList
[i
]->AllocMapLock
);
463 PagingFileList
[i
]->AllocMap
[off
>> 5] &= (~(1 << (off
% 32)));
465 PagingFileList
[i
]->FreePages
++;
466 PagingFileList
[i
]->UsedPages
--;
471 KeReleaseSpinLockFromDpcLevel(&PagingFileList
[i
]->AllocMapLock
);
472 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
477 MmIsAvailableSwapPage(VOID
)
479 return(MiFreeSwapPages
> 0);
484 MmAllocSwapPage(VOID
)
491 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
493 if (MiFreeSwapPages
== 0)
495 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
499 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
501 if (PagingFileList
[i
] != NULL
&&
502 PagingFileList
[i
]->FreePages
>= 1)
504 off
= MiAllocPageFromPagingFile(PagingFileList
[i
]);
505 if (off
== 0xFFFFFFFF)
508 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
509 return(STATUS_UNSUCCESSFUL
);
513 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
515 entry
= ENTRY_FROM_FILE_OFFSET(i
, off
);
520 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
525 static PRETRIEVEL_DESCRIPTOR_LIST FASTCALL
526 MmAllocRetrievelDescriptorList(ULONG Pairs
)
529 PRETRIEVEL_DESCRIPTOR_LIST RetDescList
;
531 Size
= sizeof(RETRIEVEL_DESCRIPTOR_LIST
) + Pairs
* 2 * sizeof(LARGE_INTEGER
);
532 RetDescList
= ExAllocatePool(NonPagedPool
, Size
);
535 RtlZeroMemory(RetDescList
, Size
);
542 MmDumpToPagingFile(ULONG BugCode
,
543 ULONG BugCodeParameter1
,
544 ULONG BugCodeParameter2
,
545 ULONG BugCodeParameter3
,
546 ULONG BugCodeParameter4
,
547 PKTRAP_FRAME TrapFrame
)
549 PMM_CORE_DUMP_HEADER Headers
;
551 UCHAR MdlBase
[sizeof(MDL
) + sizeof(ULONG
)];
552 PMDL Mdl
= (PMDL
)MdlBase
;
553 PETHREAD Thread
= PsGetCurrentThread();
556 LONGLONG NextOffset
= 0;
558 PRETRIEVAL_POINTERS_BUFFER RetrievalPointers
;
559 LARGE_INTEGER DiskOffset
;
561 if (MmCoreDumpPageFile
== 0xFFFFFFFF)
563 return(STATUS_UNSUCCESSFUL
);
566 DbgPrint("\nMM: Dumping core: ");
568 /* Prepare the dump headers. */
569 Headers
= (PMM_CORE_DUMP_HEADER
)MmCoreDumpPageFrame
;
570 Headers
->Magic
= MM_CORE_DUMP_HEADER_MAGIC
;
571 Headers
->Version
= MM_CORE_DUMP_HEADER_VERSION
;
572 Headers
->Type
= MmCoreDumpType
;
573 if (TrapFrame
!= NULL
)
576 if (!(TrapFrame
->EFlags
& (1 << 17)))
578 memcpy(&Headers
->TrapFrame
, TrapFrame
,
579 sizeof(KTRAP_FRAME
) - (4 * sizeof(ULONG
)));
584 memcpy(&Headers
->TrapFrame
, TrapFrame
, sizeof(KTRAP_FRAME
));
587 Headers
->BugCheckCode
= BugCode
;
588 Headers
->BugCheckParameters
[0] = BugCodeParameter1
;
589 Headers
->BugCheckParameters
[1] = BugCodeParameter2
;
590 Headers
->BugCheckParameters
[2] = BugCodeParameter3
;
591 Headers
->BugCheckParameters
[3] = BugCodeParameter4
;
592 Headers
->FaultingStackBase
= (PVOID
)Thread
->Tcb
.StackLimit
;
593 Headers
->FaultingStackSize
=
594 StackSize
= (ULONG_PTR
)Thread
->Tcb
.StackBase
- (ULONG_PTR
)Thread
->Tcb
.StackLimit
;
595 Headers
->PhysicalMemorySize
= MmStats
.NrTotalPages
* PAGE_SIZE
;
597 /* Initialize the dump device. */
598 Status
= MmCoreDumpFunctions
->DumpInit();
599 if (!NT_SUCCESS(Status
))
601 DPRINT1("MM: Failed to initialize core dump device.\n");
605 /* Initialize the MDL. */
606 MmInitializeMdl(Mdl
, MmCoreDumpPageFrame
, PAGE_SIZE
);
607 Mdl
->MdlFlags
= MDL_PAGES_LOCKED
|MDL_IO_PAGE_READ
|MDL_SOURCE_IS_NONPAGED_POOL
;
608 MdlMap
= (PULONG
)(Mdl
+ 1);
611 /* Initialize the retrieval offsets. */
612 RetrievalPointers
= PagingFileList
[MmCoreDumpPageFile
]->RetrievalPointers
;
614 /* Dump the header. */
615 MdlMap
[0] = (ULONG
)(MmGetPhysicalAddress(MmCoreDumpPageFrame
).QuadPart
>> PAGE_SHIFT
);
616 #if defined(__GNUC__)
618 DiskOffset
= MmGetOffsetPageFile(RetrievalPointers
, (LARGE_INTEGER
)0LL);
622 const LARGE_INTEGER dummy
=
626 DiskOffset
= MmGetOffsetPageFile(RetrievalPointers
, dummy
);
629 DiskOffset
.QuadPart
+= MmCoreDumpLcnMapping
.LcnDiskOffset
.QuadPart
;
630 Status
= MmCoreDumpFunctions
->DumpWrite(DiskOffset
, Mdl
);
631 if (!NT_SUCCESS(Status
))
633 DPRINT1("MM: Failed to write core dump header\n.");
636 NextOffset
+= PAGE_SIZE
;
641 /* Write out the contents of physical memory. */
642 if (MmCoreDumpType
== MM_CORE_DUMP_TYPE_FULL
)
644 for (i
= 0; i
< MmStats
.NrTotalPages
; i
++)
647 MmCreateVirtualMappingForKernel(MmCoreDumpPageFrame
,
651 #if defined(__GNUC__)
653 DiskOffset
= MmGetOffsetPageFile(RetrievalPointers
,
654 (LARGE_INTEGER
)NextOffset
);
659 dummy
.QuadPart
= NextOffset
;
660 DiskOffset
= MmGetOffsetPageFile(RetrievalPointers
, dummy
);
663 DiskOffset
.QuadPart
+= MmCoreDumpLcnMapping
.LcnDiskOffset
.QuadPart
;
664 Status
= MmCoreDumpFunctions
->DumpWrite(DiskOffset
, Mdl
);
665 MmRawDeleteVirtualMapping(MmCoreDumpPageFrame
);
666 if (!NT_SUCCESS(Status
))
668 DPRINT1("MM: Failed to write page to core dump.\n");
671 if ((i
% ((1024*1024) / PAGE_SIZE
)) == 0)
673 DbgPrint("\b\b%.2d", i
/ ((1024*1024)/PAGE_SIZE
));
675 NextOffset
+= PAGE_SIZE
;
680 MmCoreDumpFunctions
->DumpFinish();
681 return(STATUS_SUCCESS
);
685 MmInitializeCrashDump(HANDLE PageFileHandle
, ULONG PageFileNum
)
687 PFILE_OBJECT PageFile
;
688 PDEVICE_OBJECT PageFileDevice
;
692 IO_STATUS_BLOCK Iosb
;
693 UNICODE_STRING DiskDumpName
= RTL_CONSTANT_STRING(L
"DiskDump");
694 ANSI_STRING ProcName
;
695 PIO_STACK_LOCATION StackPtr
;
696 PLDR_DATA_TABLE_ENTRY ModuleObject
= NULL
;
699 Status
= ZwFsControlFile(PageFileHandle
,
704 FSCTL_ROS_QUERY_LCN_MAPPING
,
707 &MmCoreDumpLcnMapping
,
708 sizeof(ROS_QUERY_LCN_MAPPING
));
709 if (!NT_SUCCESS(Status
) ||
710 Iosb
.Information
!= sizeof(ROS_QUERY_LCN_MAPPING
))
715 /* Get the underlying storage device. */
717 ObReferenceObjectByHandle(PageFileHandle
,
723 if (!NT_SUCCESS(Status
))
728 PageFileDevice
= PageFile
->Vpb
->RealDevice
;
730 /* Get the dump pointers. */
731 KeInitializeEvent(&Event
, NotificationEvent
, FALSE
);
732 Irp
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_GET_DUMP_POINTERS
,
737 sizeof(MmCoreDumpPointers
),
743 ObDereferenceObject(PageFile
);
744 return(STATUS_NO_MEMORY
);// tMk - is this correct return code ???
747 StackPtr
= IoGetNextIrpStackLocation(Irp
);
748 StackPtr
->FileObject
= PageFile
;
749 StackPtr
->DeviceObject
= PageFileDevice
;
750 StackPtr
->Parameters
.DeviceIoControl
.InputBufferLength
= 0;
751 StackPtr
->Parameters
.DeviceIoControl
.OutputBufferLength
= sizeof(MmCoreDumpPointers
);
753 Status
= IoCallDriver(PageFileDevice
,Irp
);
754 if (Status
== STATUS_PENDING
)
756 Status
= KeWaitForSingleObject(&Event
,
762 if (Status
!= STATUS_SUCCESS
||
763 Iosb
.Information
!= sizeof(MmCoreDumpPointers
))
765 ObDereferenceObject(PageFile
);
769 /* Load the diskdump driver. */
770 Status
= MmLoadSystemImage(&DiskDumpName
, NULL
, NULL
, 0, (PVOID
)&ModuleObject
, &BaseAddress
);
771 if (ModuleObject
== NULL
)
773 return(STATUS_OBJECT_NAME_NOT_FOUND
);
775 RtlInitAnsiString(&ProcName
, "DiskDumpFunctions");
776 MmCoreDumpFunctions
= MiFindExportedRoutineByName(BaseAddress
,
778 if (!NT_SUCCESS(Status
))
780 ObDereferenceObject(PageFile
);
784 /* Prepare for disk dumping. */
785 Status
= MmCoreDumpFunctions
->DumpPrepare(PageFileDevice
,
786 &MmCoreDumpPointers
);
787 if (!NT_SUCCESS(Status
))
789 ObDereferenceObject(PageFile
);
793 MmCoreDumpPageFile
= PageFileNum
;
794 ObDereferenceObject(PageFile
);
795 return(STATUS_SUCCESS
);
799 NtCreatePagingFile(IN PUNICODE_STRING FileName
,
800 IN PLARGE_INTEGER InitialSize
,
801 IN PLARGE_INTEGER MaximumSize
,
804 NTSTATUS Status
= STATUS_SUCCESS
;
805 OBJECT_ATTRIBUTES ObjectAttributes
;
807 IO_STATUS_BLOCK IoStatus
;
808 PFILE_OBJECT FileObject
;
809 PPAGINGFILE PagingFile
;
812 FILE_FS_SIZE_INFORMATION FsSizeInformation
;
813 PRETRIEVEL_DESCRIPTOR_LIST RetDescList
;
814 PRETRIEVEL_DESCRIPTOR_LIST CurrentRetDescList
;
816 ULONG BytesPerAllocationUnit
;
819 LARGE_INTEGER MaxVcn
;
822 KPROCESSOR_MODE PreviousMode
;
823 UNICODE_STRING CapturedFileName
;
824 LARGE_INTEGER SafeInitialSize
, SafeMaximumSize
;
826 DPRINT("NtCreatePagingFile(FileName %wZ, InitialSize %I64d)\n",
827 FileName
, InitialSize
->QuadPart
);
829 if (MiPagingFileCount
>= MAX_PAGING_FILES
)
831 return(STATUS_TOO_MANY_PAGING_FILES
);
834 PreviousMode
= ExGetPreviousMode();
836 if (PreviousMode
!= KernelMode
)
840 SafeInitialSize
= ProbeForReadLargeInteger(InitialSize
);
841 SafeMaximumSize
= ProbeForReadLargeInteger(MaximumSize
);
845 Status
= _SEH_GetExceptionCode();
849 if (!NT_SUCCESS(Status
))
856 SafeInitialSize
= *InitialSize
;
857 SafeMaximumSize
= *MaximumSize
;
860 /* Pagefiles can't be larger than 4GB and ofcourse the minimum should be
861 smaller than the maximum */
862 if (0 != SafeInitialSize
.u
.HighPart
)
864 return STATUS_INVALID_PARAMETER_2
;
866 if (0 != SafeMaximumSize
.u
.HighPart
)
868 return STATUS_INVALID_PARAMETER_3
;
870 if (SafeMaximumSize
.u
.LowPart
< SafeInitialSize
.u
.LowPart
)
872 return STATUS_INVALID_PARAMETER_MIX
;
875 Status
= ProbeAndCaptureUnicodeString(&CapturedFileName
,
878 if (!NT_SUCCESS(Status
))
883 InitializeObjectAttributes(&ObjectAttributes
,
889 Status
= IoCreateFile(&FileHandle
,
897 FILE_SYNCHRONOUS_IO_NONALERT
,
902 SL_OPEN_PAGING_FILE
| IO_NO_PARAMETER_CHECKING
);
904 ReleaseCapturedUnicodeString(&CapturedFileName
,
906 if (!NT_SUCCESS(Status
))
911 Status
= ZwQueryVolumeInformationFile(FileHandle
,
914 sizeof(FILE_FS_SIZE_INFORMATION
),
915 FileFsSizeInformation
);
916 if (!NT_SUCCESS(Status
))
922 BytesPerAllocationUnit
= FsSizeInformation
.SectorsPerAllocationUnit
*
923 FsSizeInformation
.BytesPerSector
;
924 /* FIXME: If we have 2048 BytesPerAllocationUnit (FAT16 < 128MB) there is
925 * a problem if the paging file is fragmented. Suppose the first cluster
926 * of the paging file is cluster 3042 but cluster 3043 is NOT part of the
927 * paging file but of another file. We can't write a complete page (4096
928 * bytes) to the physical location of cluster 3042 then. */
929 if (BytesPerAllocationUnit
% PAGE_SIZE
)
931 DPRINT1("BytesPerAllocationUnit %d is not a multiple of PAGE_SIZE %d\n",
932 BytesPerAllocationUnit
, PAGE_SIZE
);
934 return STATUS_UNSUCCESSFUL
;
937 Status
= ZwSetInformationFile(FileHandle
,
940 sizeof(LARGE_INTEGER
),
941 FileAllocationInformation
);
942 if (!NT_SUCCESS(Status
))
948 Status
= ObReferenceObjectByHandle(FileHandle
,
954 if (!NT_SUCCESS(Status
))
960 CurrentRetDescList
= RetDescList
= MmAllocRetrievelDescriptorList(PAIRS_PER_RUN
);
962 if (CurrentRetDescList
== NULL
)
964 ObDereferenceObject(FileObject
);
966 return(STATUS_NO_MEMORY
);
969 #if defined(__GNUC__)
977 MaxVcn
.QuadPart
= (SafeInitialSize
.QuadPart
+ BytesPerAllocationUnit
- 1) / BytesPerAllocationUnit
;
980 Status
= ZwFsControlFile(FileHandle
,
985 FSCTL_GET_RETRIEVAL_POINTERS
,
987 sizeof(LARGE_INTEGER
),
988 &CurrentRetDescList
->RetrievalPointers
,
989 sizeof(RETRIEVAL_POINTERS_BUFFER
) + PAIRS_PER_RUN
* 2 * sizeof(LARGE_INTEGER
));
990 if (!NT_SUCCESS(Status
))
994 CurrentRetDescList
= RetDescList
;
995 RetDescList
= RetDescList
->Next
;
996 ExFreePool(CurrentRetDescList
);
998 ObDereferenceObject(FileObject
);
1002 ExtentCount
+= CurrentRetDescList
->RetrievalPointers
.ExtentCount
;
1003 if (CurrentRetDescList
->RetrievalPointers
.Extents
[CurrentRetDescList
->RetrievalPointers
.ExtentCount
-1].NextVcn
.QuadPart
< MaxVcn
.QuadPart
)
1005 CurrentRetDescList
->Next
= MmAllocRetrievelDescriptorList(PAIRS_PER_RUN
);
1006 if (CurrentRetDescList
->Next
== NULL
)
1010 CurrentRetDescList
= RetDescList
;
1011 RetDescList
= RetDescList
->Next
;
1012 ExFreePool(CurrentRetDescList
);
1014 ObDereferenceObject(FileObject
);
1015 ZwClose(FileHandle
);
1016 return(STATUS_NO_MEMORY
);
1018 Vcn
= CurrentRetDescList
->RetrievalPointers
.Extents
[CurrentRetDescList
->RetrievalPointers
.ExtentCount
-1].NextVcn
;
1019 CurrentRetDescList
= CurrentRetDescList
->Next
;
1027 PagingFile
= ExAllocatePool(NonPagedPool
, sizeof(*PagingFile
));
1028 if (PagingFile
== NULL
)
1032 CurrentRetDescList
= RetDescList
;
1033 RetDescList
= RetDescList
->Next
;
1034 ExFreePool(CurrentRetDescList
);
1036 ObDereferenceObject(FileObject
);
1037 ZwClose(FileHandle
);
1038 return(STATUS_NO_MEMORY
);
1041 RtlZeroMemory(PagingFile
, sizeof(*PagingFile
));
1043 PagingFile
->FileObject
= FileObject
;
1044 PagingFile
->MaximumSize
.QuadPart
= SafeMaximumSize
.QuadPart
;
1045 PagingFile
->CurrentSize
.QuadPart
= SafeInitialSize
.QuadPart
;
1046 PagingFile
->FreePages
= (ULONG
)(SafeInitialSize
.QuadPart
/ PAGE_SIZE
);
1047 PagingFile
->UsedPages
= 0;
1048 KeInitializeSpinLock(&PagingFile
->AllocMapLock
);
1050 AllocMapSize
= (PagingFile
->FreePages
/ 32) + 1;
1051 PagingFile
->AllocMap
= ExAllocatePool(NonPagedPool
,
1052 AllocMapSize
* sizeof(ULONG
));
1053 PagingFile
->AllocMapSize
= AllocMapSize
;
1055 if (PagingFile
->AllocMap
== NULL
)
1059 CurrentRetDescList
= RetDescList
;
1060 RetDescList
= RetDescList
->Next
;
1061 ExFreePool(CurrentRetDescList
);
1063 ExFreePool(PagingFile
);
1064 ObDereferenceObject(FileObject
);
1065 ZwClose(FileHandle
);
1066 return(STATUS_NO_MEMORY
);
1068 DPRINT("ExtentCount: %d\n", ExtentCount
);
1069 Size
= sizeof(RETRIEVAL_POINTERS_BUFFER
) + ExtentCount
* 2 * sizeof(LARGE_INTEGER
);
1070 PagingFile
->RetrievalPointers
= ExAllocatePool(NonPagedPool
, Size
);
1071 if (PagingFile
->RetrievalPointers
== NULL
)
1075 CurrentRetDescList
= RetDescList
;
1076 RetDescList
= RetDescList
->Next
;
1077 ExFreePool(CurrentRetDescList
);
1079 ExFreePool(PagingFile
->AllocMap
);
1080 ExFreePool(PagingFile
);
1081 ObDereferenceObject(FileObject
);
1082 ZwClose(FileHandle
);
1083 return(STATUS_NO_MEMORY
);
1086 RtlZeroMemory(PagingFile
->AllocMap
, AllocMapSize
* sizeof(ULONG
));
1087 RtlZeroMemory(PagingFile
->RetrievalPointers
, Size
);
1090 PagingFile
->RetrievalPointers
->ExtentCount
= ExtentCount
;
1091 PagingFile
->RetrievalPointers
->StartingVcn
= RetDescList
->RetrievalPointers
.StartingVcn
;
1092 CurrentRetDescList
= RetDescList
;
1093 while (CurrentRetDescList
)
1095 memcpy(&PagingFile
->RetrievalPointers
->Extents
[Count
],
1096 CurrentRetDescList
->RetrievalPointers
.Extents
,
1097 CurrentRetDescList
->RetrievalPointers
.ExtentCount
* 2 * sizeof(LARGE_INTEGER
));
1098 Count
+= CurrentRetDescList
->RetrievalPointers
.ExtentCount
;
1099 RetDescList
= CurrentRetDescList
;
1100 CurrentRetDescList
= CurrentRetDescList
->Next
;
1101 ExFreePool(RetDescList
);
1104 if (PagingFile
->RetrievalPointers
->ExtentCount
!= ExtentCount
||
1105 PagingFile
->RetrievalPointers
->Extents
[ExtentCount
- 1].NextVcn
.QuadPart
!= MaxVcn
.QuadPart
)
1107 ExFreePool(PagingFile
->RetrievalPointers
);
1108 ExFreePool(PagingFile
->AllocMap
);
1109 ExFreePool(PagingFile
);
1110 ObDereferenceObject(FileObject
);
1111 ZwClose(FileHandle
);
1112 return(STATUS_UNSUCCESSFUL
);
1116 * Change the entries from lcn's to volume offset's.
1118 PagingFile
->RetrievalPointers
->StartingVcn
.QuadPart
*= BytesPerAllocationUnit
;
1119 for (i
= 0; i
< ExtentCount
; i
++)
1121 PagingFile
->RetrievalPointers
->Extents
[i
].Lcn
.QuadPart
*= BytesPerAllocationUnit
;
1122 PagingFile
->RetrievalPointers
->Extents
[i
].NextVcn
.QuadPart
*= BytesPerAllocationUnit
;
1125 KeAcquireSpinLock(&PagingFileListLock
, &oldIrql
);
1126 for (i
= 0; i
< MAX_PAGING_FILES
; i
++)
1128 if (PagingFileList
[i
] == NULL
)
1130 PagingFileList
[i
] = PagingFile
;
1134 MiFreeSwapPages
= MiFreeSwapPages
+ PagingFile
->FreePages
;
1135 MiPagingFileCount
++;
1136 KeReleaseSpinLock(&PagingFileListLock
, oldIrql
);
1138 /* Check whether this pagefile can be a crash dump target. */
1139 if (MmCoreDumpType
!= MM_CORE_DUMP_TYPE_NONE
&&
1140 PagingFile
->CurrentSize
.QuadPart
>= MmCoreDumpSize
&&
1141 MmCoreDumpPageFile
== 0xFFFFFFFF)
1143 MmInitializeCrashDump(FileHandle
, i
);
1145 ZwClose(FileHandle
);
1147 MmSwapSpaceMessage
= FALSE
;
1149 return(STATUS_SUCCESS
);