Pagefile swap file
Author: jiurl home: http://jiurl.yeah.net date: 2005-06-02
1 PageFile Swap File IntroductionWindows 2000 uses page-based virtual memory management, contents of some physical pages can be out swapped into PageFile swap file, so these physical pages can be used for new purpose.In Windows 2000, file "pagefile.sys" is The Pagefile Swap file.
The physical pages that can be out swapped to PageFile swap file include:. Thread's kernel mode stack Pages in process's WorkingSet except for file-mapping, such as, process's heap, thread's user mode stack, process environment block, thread environment block, process's page table, etc. Pages in PagedPool in system address space. Under some conditions, even process's page directory, process's WorkingSet may be also out swapped to PageFile swap file. There's another special case, a file-mapping created by calling CreateFileMapping and parameter 'hFile 'is INVALID_HANDLE_VALUE, basing on file-mapping mechanism, the file-mapping is also backed by PageFile swap file.2 PageFile Swap File DetailsThe entire PageFile is, from beginning to end, divided into one by one PAGE_SIZE blocks. For 32-bit CPU Page_size is 4kb. When Contents of Physical Pages Are Out Swapped To Pagefile, Find The Unused Blocks in Pagefile, THEN WRITE The Contents of Physical Pages Into The Blocks onding flag bits to indicate that the corresponding page's content of the PTE is in PageFile, and save the corresponding block number in the PTE. When content of page are in swapped from PageFile, according to the information in the PTE, find the corresponding block in PageFile, then read the content of the block into a physical page.In memory, system maintains a MMPAGING_FILE structure for every PageFile. Depending on the information in the structure, system can know PageFile is which file, system can know which blocks in PageFile have BEEN Used, Which Blocks Haven '
t been used, to find the unused block to use.MMPAGING_FILE structure defined as following: typedef struct _MMPAGING_FILE {PFN_NUMBER Size; PFN_NUMBER MaximumSize; PFN_NUMBER MinimumSize; PFN_NUMBER FreeSpace; PFN_NUMBER CurrentUsage; PFN_NUMBER PeakUsage; PFN_NUMBER Hint; PFN_NUMBER HighestPage; PMMMOD_WRITER_MDL_ENTRY Entry [2 ]; PRTL_BITMAP Bitmap; PFILE_OBJECT File; UNICODE_STRING PageFileName; ULONG PageFileNumber; BOOLEAN Extended; BOOLEAN HintSetToZero;} MMPAGING_FILE, * PMMPAGING_FILE; RTL_BITMAP structure defined as following: typedef struct _RTL_BITMAP {ULONG SizeOfBitMap; // Number of bits in bit mapPULONG Buffer; / / Pointer to the bit map itself} RTL_BITMAP; Depending on "PFILE_OBJECT File", corresponding PageFile file can be found.Depending on "PRTL_BITMAP Bitmap", which blocks in PageFile have not been used can be konwn.Each bit in Bitmap-> Buffer Corresponds to A Block in Pagefile, Value of Bit IS 0 Means That Corresponding Block Haven't Been Used, Value Of Bit IS 1 Means That Corresponding Blo ck have been used. Use RtlFindClearBitsAndSet to find unused blocks in Bitmap. SizeOfBitMap depends on the PageFile's MaximumSize.A PTE's bit0, bit10, bit11 are all 0, means that the content of the page is in PageFile. Bit0 is Valid flag bit, the bit is 0 means that this is an invalid page, the other bits of an invalid page's PTE are all defined and used by system. For an invalid PTE, bit10 is Prototype flag bit. For an invalid PTE and Prototype flag bit is 0, bit11 is Transition flag bit, Transition flag bit is 0 means that the content of the page is in a PageFile.PTE structure which point to a block in a PageFile defined as following: {ULONG Valid: 1; // equal to 0ULONG PageFileNumber: 4 ;
ULONG Protection: 5; ULONG Prototype: 1; // equal to 0ULONG Transition: 1; // equal to 0ULONG PageFileBlockNumber: 20;} PageFileNumber: 4 bits, used to find the corresponding PageFile in PMMPAGING_FILE MmPagingFile [] array.There's a PMMPAGING_FILE . MmPagingFile [] array in system, which keep pointer of every PageFile's MMPAGING_FILE structure PageFileNumber is 4 bits so that there may be 16 PageFiles at most in system.PageFileBlockNumber: 20 bits, the value as a index is used to find the corresponding block in PageFile.PageFileBlockNumber is 20 bits so that it can index 1M blocks at most. For 32-bit CPU, the size of block is 4KB so that the possible maximum size of a PageFile is 4GB. Every block needs a bit in Bitmap-> Buffer to indicate whether it has been used, so that 1M blocks * 1 bit / block = 1M bits = 256KB, so that the Bitmap-> Buffer of maximum size PageFile needs only 256KB memory.3 Out SwapOut swap of a page is divided into two Steps. First Step, Page Turns to Transition State Fr om Valid state. Second step, the physical page in Transition state is out swapped to PageFile, only modified physical pages can be out swapped to PageFile, and only modified physical pages need be out swapped to PageFile. Only modified physical pages can be out swapped to PageFile, so that we only introduce the modified physical page case.Page turns to Transition state from Valid state, PTE truns to invliad from valid. The higher-order 20 bits of invalid PTE is still a physical frame number, the physical page is Just The Physical Page That Is Valid, And The Content of The PTE IS VALID. The Physical Page '
s PFN (PageFrameNumber) structure also turns to Modified state from Valid state, and be linked into MmModifiedPageListHead list.Every physical page has a PFN structure, to keep the physical page's relative information, such as whether the physical page has been used, whether the physical page has been modified. The size of PFN structure is 24 Bytes. There's a structure called PFN database in system, it is a PFN array, orderly correspond every physical pages. There are MmZeroedPageListHead list, MmFreePageListHead list, MmStandbyPageListHead list, MmModifiedPageListHead list in system. All modified Transition state physical pages' PFN structure are linked in MmModifiedPageListHead list. All non-modified Transition state physical pages' PFN structure are linked in MmStandbyPageListHead list. All free and zeroed physical pages' PFN structure are linked in MmZeroedPageListHead list. All Free and non-zeroed Physical Pages' PFN Structure Are LINKED in mmfreepagelisthead list.pfn in diffreent state has different structure definition, PFN structure in Modified state defined as following: {ULONG Flink; PULONG PteAddress; ULONG Blink; USHORT Flags; USHORT ReferenceCount; ULONG OriginalPte; ULONG PteFrame;} Flink, next PFN structure's physical frame number in list.Blink , previous PFN structure's physical frame number in list.There are mainly two cases that page turns to Transition state from Valid state, one is the out swap of thread's kernel mode stack, one is triming process's WorkingSet.Every a moment, for example 4 seconds System Thread Keswapprocessorstack Gets Executed. Find The Wait State Threads Which Have Been Wait State Longer Than The Stack Protect Time, Trun Those THREADS '
kernel mode stack pages, from Valid state to Transition state.When available memory is very tight, system thread KeBalanceSetManager will call MmWorkingSetManager, MmWorkingSetManager will call MiTrimWorkingSet to trim WorkingSet. Process's WorkingSet structure keeps the process's all valid pages' virtual address, include all valid pages in user address space, process's page tables, etc. in WorkingSet, modified valid pages include, process's heap, thread's user mode stack, process environment block, thread environment block, process's page table, etc. triming WorkingSet will turn some valid pages from Valid state to Transition state, and these pages will be removed from WorkingSet. The pages removed from WorkingSet and turned from Valid state to Transition State, can be modified pages or non-modified pages, but modified pages will all be linked in MmModifiedPageListHead list. When MmavailablePages 0x29002 nt! MiModifiedPageWriterWorker 0x22403 nt! MiModifiedPageWriter 0x14c04 nt! PspSystemThreadStartup 0x6905 nt! KiThreadStartup 0x16When physical page write to PageFile complete, MiWriteComplete will be executed. In MiWriteComplete, link the physical page to MmStandbyPageList list. This moment, the content of the physical page is still the content when the PTE is valid, corresponding PTE is still in Transition state and points to this physcial page.When system needs physical pages, system will call, such as, MiRemoveAnyPage, MiRemoveZeroPage to obtain physcial pages. MiRemoveAnyPage, MiRemoveZeroPage first remove physical pages from ZeroedPageList and FreePageList, when there is no physical pages in ZeroedPageList, FreePageList, will remove physical pages from StandbyPageList. in the progress of removing physical pages from StandbyPageList, will set corresponding PTE according to the physical page's PFN structure's OriginalPte . Thee Saved The Block's Block Number Which Keeped The Co NTENT OF THESICAL PAGE AND OTHER Information in Pfn Structure ' s OriginalPte, so that the corresponding PTE points to the corresponding block in PageFile. So physical page removed from StandbyPageList can be used for other purpose.4 In SwapProgram codes access the page which has been out swapped in PageFile, will cause an exception, exception handler procedure will in swap the content of the page. The PTE corresponding the out swapped page in PageFile is invalid, and points to a block in PageFile. Codes access the content of the page which has been out swapped in PageFile, because the corresponding PTE is invalid, will cause an Page-Fault exception, the interrupt vector of Page-Fault exception is 0xe, the entry address of the interrupt handler procedure is KiTrap0E. In Page-Fault exception processing, according to the faulting address, find the corresponding PTE , PTE's Bit0, Bit10, Bit11 Are All 0, Means That The Content of the PageFileNumber in Ptel, Find The Corresponding Pagefile In MmpagingFile [], THEN ACCORDIN g to PageFileBlockNumber in PTE, can find the needed block in the PageFile. Read the corresponding block into a physical page, then set the PTE to point to the physical page, and set the PTE valid, the in swapping is complete.In Page- Fault exception processing, finding that the PTE's bit10, bit11 are all 0, MiDispatchFault will call MiResolvePageFileFault. MiResolvePageFileFault find the corresponding block according to PageFileNumber, PageFileBlockNumber in PTE, make ready to read, then return. MiDispatchFault will call IoPageRead to read the corresponding block In PageFile Into Physical PageFileFault: # 00 nt! mireesolvepagefilefault01 nt! midispatchfault 0x13c02 nt! mmaccessfault