ReactOS  0.4.15-dev-2528-g5506091
sptab.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2005 ReactOS Team (and the authors from the programmers section)
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  *
19  * PROJECT: ReactOS kernel
20  * FILE: ntoskrnl/cache/section/sptab.c
21  * PURPOSE: Section object page tables
22  *
23  * PROGRAMMERS: arty
24  */
25 
26 /*
27 
28 This file implements the section page table. It relies on rtl generic table
29 functionality to provide access to 256-page chunks. Calls to
30 MiSetPageEntrySectionSegment and MiGetPageEntrySectionSegment must be
31 synchronized by holding the segment lock.
32 
33 Each page table entry is a ULONG as in x86.
34 
35 Bit 1 is used as a swap entry indication as in the main page table.
36 Bit 2 is used as a dirty indication. A dirty page will eventually be written
37 back to the file.
38 Bits 3-11 are used as a map count in the legacy mm code, Note that zero is
39 illegal, as the legacy code does not take advantage of segment rmaps.
40 Therefore, every segment page is mapped in at least one address space, and
41 MmUnsharePageEntry is quite complicated. In addition, the page may also be
42 owned by the legacy cache manager, giving an implied additional reference.
43 Upper bits are a PFN_NUMBER.
44 
45 These functions, in addition to maintaining the segment page table also
46 automatically maintain the segment rmap by calling MmSetSectionAssociation
47 and MmDeleteSectionAssociation. Segment rmaps are discussed in rmap.c. The
48 upshot is that it is impossible to have a page properly registered in a segment
49 page table and not also found in a segment rmap that can be found from the
50 paging machinery.
51 
52 */
53 
54 /* INCLUDES *****************************************************************/
55 
56 #include <ntoskrnl.h>
57 #include "newmm.h"
58 #define NDEBUG
59 #include <debug.h>
60 
61 #define DPRINTC DPRINT
62 
63 /* TYPES *********************************************************************/
64 
66 
67 _Function_class_(RTL_GENERIC_ALLOCATE_ROUTINE)
68 static
69 PVOID
70 NTAPI
71 MiSectionPageTableAllocate(PRTL_GENERIC_TABLE Table, CLONG Bytes)
72 {
73  PVOID Result;
75  //DPRINT("MiSectionPageTableAllocate(%d) => %p\n", Bytes, Result);
76  return Result;
77 }
78 
79 _Function_class_(RTL_GENERIC_FREE_ROUTINE)
80 static
81 VOID
82 NTAPI
83 MiSectionPageTableFree(PRTL_GENERIC_TABLE Table, PVOID Data)
84 {
85  //DPRINT("MiSectionPageTableFree(%p)\n", Data);
86  ExFreePoolWithTag(Data, 'tPmM');
87 }
88 
89 _Function_class_(RTL_GENERIC_COMPARE_ROUTINE)
90 static
92 NTAPI
93 MiSectionPageTableCompare(PRTL_GENERIC_TABLE Table,
94  PVOID PtrA,
95  PVOID PtrB)
96 {
97  PLARGE_INTEGER A = PtrA, B = PtrB;
98  BOOLEAN Result = (A->QuadPart < B->QuadPart) ? GenericLessThan :
99  (A->QuadPart == B->QuadPart) ? GenericEqual : GenericGreaterThan;
100 
101 #if 0
102  DPRINT
103  ("Compare: %08x%08x vs %08x%08x => %s\n",
104  A->u.HighPart, A->u.LowPart,
105  B->u.HighPart, B->u.LowPart,
106  Result == GenericLessThan ? "GenericLessThan" :
107  Result == GenericGreaterThan ? "GenericGreaterThan" :
108  "GenericEqual");
109 #endif
110 
111  return Result;
112 }
113 
114 static
116 NTAPI
119 {
120  LARGE_INTEGER SearchFileOffset;
122  SearchFileOffset.QuadPart = ROUND_DOWN(FileOffset->QuadPart,
124  PageTable = RtlLookupElementGenericTable(Table, &SearchFileOffset);
125 
126  DPRINT("MiSectionPageTableGet(%p,%I64x)\n",
127  Table,
128  FileOffset->QuadPart);
129 
130  return PageTable;
131 }
132 
133 static
135 NTAPI
138 {
139  LARGE_INTEGER SearchFileOffset;
140  CACHE_SECTION_PAGE_TABLE SectionZeroPageTable;
142  FileOffset);
143  /* Please zero memory when taking away zero initialization. */
144  RtlZeroMemory(&SectionZeroPageTable, sizeof(CACHE_SECTION_PAGE_TABLE));
145  if (!PageTableSlice)
146  {
147  SearchFileOffset.QuadPart = ROUND_DOWN(FileOffset->QuadPart,
149  SectionZeroPageTable.FileOffset = SearchFileOffset;
150  SectionZeroPageTable.Refcount = 1;
151  PageTableSlice = RtlInsertElementGenericTable(Table,
152  &SectionZeroPageTable,
153  sizeof(SectionZeroPageTable),
154  NULL);
155  if (!PageTableSlice) return NULL;
156  DPRINT("Allocate page table %p (%I64x)\n",
157  PageTableSlice,
158  PageTableSlice->FileOffset.QuadPart);
159  }
160  return PageTableSlice;
161 }
162 
163 VOID
164 NTAPI
166 {
168  MiSectionPageTableCompare,
169  MiSectionPageTableAllocate,
170  MiSectionPageTableFree,
171  NULL);
172 
173  DPRINT("MiInitializeSectionPageTable(%p)\n", &Segment->PageTable);
174 }
175 
176 NTSTATUS
177 NTAPI
181  const char *file,
182  int line)
183 {
184  ULONG_PTR PageIndex, OldEntry;
186 
187  ASSERT(Segment->Locked);
189 
191 
192  if (!PageTable) return STATUS_NO_MEMORY;
193 
195 
196  PageTable->Segment = Segment;
197  PageIndex = (ULONG_PTR)((Offset->QuadPart - PageTable->FileOffset.QuadPart) / PAGE_SIZE);
198  OldEntry = PageTable->PageEntries[PageIndex];
199 
200  DPRINT("MiSetPageEntrySectionSegment(%p,%08x%08x,%x=>%x)\n",
201  Segment,
202  Offset->u.HighPart,
203  Offset->u.LowPart,
204  OldEntry,
205  Entry);
206 
207  if (Entry && !IS_SWAP_FROM_SSE(Entry))
208  {
209  /* We have a valid entry. See if we must do something */
210  if (OldEntry && !IS_SWAP_FROM_SSE(OldEntry))
211  {
212  /* The previous entry was valid. Shall we swap the Rmaps ? */
213  if (PFN_FROM_SSE(Entry) != PFN_FROM_SSE(OldEntry))
214  {
217  }
218  }
219  else
220  {
221  /*
222  * We're switching to a valid entry from an invalid one.
223  * Add the Rmap and take a ref on the segment.
224  */
226  InterlockedIncrement64(Segment->ReferenceCount);
227 
228  if (Offset->QuadPart >= (Segment->LastPage << PAGE_SHIFT))
229  Segment->LastPage = (Offset->QuadPart >> PAGE_SHIFT) + 1;
230  }
231  }
232  else if (OldEntry && !IS_SWAP_FROM_SSE(OldEntry))
233  {
234  /* We're switching to an invalid entry from a valid one */
237 
238  if (Offset->QuadPart == ((Segment->LastPage - 1ULL) << PAGE_SHIFT))
239  {
240  /* We are unsetting the last page */
241  while (--Segment->LastPage)
242  {
243  LARGE_INTEGER CheckOffset;
244  CheckOffset.QuadPart = (Segment->LastPage - 1) << PAGE_SHIFT;
246  if ((Entry != 0) && !IS_SWAP_FROM_SSE(Entry))
247  break;
248  }
249  }
250  }
251 
252  PageTable->PageEntries[PageIndex] = Entry;
253  return STATUS_SUCCESS;
254 }
255 
256 ULONG_PTR
257 NTAPI
260  const char *file,
261  int line)
262 {
264  ULONG_PTR PageIndex, Result;
266 
267  ASSERT(Segment->Locked);
268  FileOffset.QuadPart = ROUND_DOWN(Offset->QuadPart,
271  if (!PageTable) return 0;
272  PageIndex = (ULONG_PTR)((Offset->QuadPart - PageTable->FileOffset.QuadPart) / PAGE_SIZE);
273  Result = PageTable->PageEntries[PageIndex];
274 #if 0
275  DPRINTC
276  ("MiGetPageEntrySectionSegment(%p,%08x%08x) => %x %s:%d\n",
277  Segment,
278  FileOffset.u.HighPart,
279  FileOffset.u.LowPart + PageIndex * PAGE_SIZE,
280  Result,
281  file, line);
282 #endif
283  return Result;
284 }
285 
286 /*
287 
288 Destroy the rtl generic table that serves as the section's page table. Call
289 the FreePage function for each non-zero entry in the section page table as
290 we go. Note that the page table is still techinally valid until after all
291 pages are destroyed, as we don't finally destroy the table until we've free
292 each slice. There is no order guarantee for deletion of individual elements
293 although it's in-order as written now.
294 
295 */
296 
297 VOID
298 NTAPI
301 {
303  DPRINT("MiFreePageTablesSectionSegment(%p)\n", &Segment->PageTable);
304  while ((Element = RtlGetElementGenericTable(&Segment->PageTable, 0))) {
305  DPRINT("Delete table for <%wZ> %p -> %I64x\n",
306  Segment->FileObject ? &Segment->FileObject->FileName : NULL,
307  Segment,
308  Element->FileOffset.QuadPart);
309  if (FreePage)
310  {
311  ULONG i;
312  for (i = 0; i < ENTRIES_PER_ELEMENT; i++)
313  {
316  Offset.QuadPart = Element->FileOffset.QuadPart + i * PAGE_SIZE;
317  Entry = Element->PageEntries[i];
318  if (Entry && !IS_SWAP_FROM_SSE(Entry))
319  {
320  DPRINT("Freeing page %p:%Ix @ %I64x\n",
321  Segment,
322  Entry,
323  Offset.QuadPart);
324 
326  }
327  }
328  }
329  DPRINT("Remove memory\n");
330  RtlDeleteElementGenericTable(&Segment->PageTable, Element);
331  }
332  DPRINT("Done\n");
333 }
334 
335 /*
336 
337 Retrieves the MM_SECTION_SEGMENT and fills in the LARGE_INTEGER Offset given
338 by the caller that corresponds to the page specified. This uses
339 MmGetSegmentRmap to find the rmap belonging to the segment itself, and uses
340 the result as a pointer to a 256-entry page table structure. The rmap also
341 includes 8 bits of offset information indication one of 256 page entries that
342 the rmap corresponds to. This information together gives us an exact offset
343 into the file, as well as the MM_SECTION_SEGMENT pointer stored in the page
344 table slice.
345 
346 NULL is returned is there is no segment rmap for the page.
347 
348 */
349 
351 NTAPI
354 {
355  ULONG RawOffset;
358 
359  PageTable = MmGetSegmentRmap(Page, &RawOffset);
360  if (PageTable)
361  {
362  Segment = PageTable->Segment;
363  Offset->QuadPart = PageTable->FileOffset.QuadPart +
364  ((ULONG64)RawOffset << PAGE_SHIFT);
365  ASSERT(PFN_FROM_SSE(PageTable->PageEntries[RawOffset]) == Page);
366  }
367 
368  return Segment;
369 }
370 
371 NTSTATUS
372 NTAPI
376 {
378  ULONG ActualOffset;
379 
381  ASSERT(PageTable);
382 
383  ActualOffset = (ULONG)(Offset->QuadPart - PageTable->FileOffset.QuadPart);
386  (PVOID)(RMAP_SEGMENT_MASK | (ActualOffset >> PAGE_SHIFT)));
387 
388  return STATUS_SUCCESS;
389 }
VOID(NTAPI * FREE_SECTION_PAGE_FUN)(PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset)
Definition: newmm.h:98
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
ASMGENDATA Table[]
Definition: genincdata.c:61
#define MmGetPageEntrySectionSegment(S, O)
Definition: mm.h:1467
Definition: ehthrow.cxx:53
VOID NTAPI MmInsertRmap(PFN_NUMBER Page, struct _EPROCESS *Process, PVOID Address)
PVOID NTAPI MmGetSegmentRmap(PFN_NUMBER Page, PULONG RawOffset)
Definition: rmap.c:459
struct _Entry Entry
Definition: kefuncs.h:627
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:40
ULONG_PTR NTAPI _MmGetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset, const char *file, int line)
Definition: sptab.c:258
#define PFN_FROM_SSE(E)
Definition: mm.h:1263
LONG NTSTATUS
Definition: precomp.h:26
static PCACHE_SECTION_PAGE_TABLE NTAPI MiSectionPageTableGet(PRTL_GENERIC_TABLE Table, PLARGE_INTEGER FileOffset)
Definition: sptab.c:117
_In_ UINT Bytes
Definition: mmcopy.h:9
VOID NTAPI MmFreePageTablesSectionSegment(PMM_SECTION_SEGMENT Segment, FREE_SECTION_PAGE_FUN FreePage)
Definition: sptab.c:299
static PCACHE_SECTION_PAGE_TABLE NTAPI MiSectionPageTableGetOrAllocate(PRTL_GENERIC_TABLE Table, PLARGE_INTEGER FileOffset)
Definition: sptab.c:136
Definition: ehthrow.cxx:92
VOID NTAPI RtlInitializeGenericTable(IN PRTL_GENERIC_TABLE Table, IN PRTL_GENERIC_COMPARE_ROUTINE CompareRoutine, IN PRTL_GENERIC_ALLOCATE_ROUTINE AllocateRoutine, IN PRTL_GENERIC_FREE_ROUTINE FreeRoutine, IN PVOID TableContext)
Definition: generictable.c:100
uint32_t ULONG_PTR
Definition: typedefs.h:65
ULONG CLONG
Definition: umtypes.h:126
ULONG PFN_NUMBER
Definition: ke.h:9
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define IS_DIRTY_SSE(E)
Definition: mm.h:1272
ULONG_PTR PageEntries[ENTRIES_PER_ELEMENT]
Definition: newmm.h:30
_Function_class_(RTL_GENERIC_ALLOCATE_ROUTINE)
Definition: sptab.c:67
KSPIN_LOCK MiSectionPageTableLock
VOID NTAPI MmDeleteSectionAssociation(PFN_NUMBER Page)
Definition: rmap.c:498
unsigned char BOOLEAN
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
Definition: parser.c:48
PFN_NUMBER Page
Definition: section.c:4756
#define ULL(a, b)
Definition: format_msg.c:27
_Inout_ PVOID Segment
Definition: exfuncs.h:1101
_Must_inspect_result_ NTSYSAPI PVOID NTAPI RtlLookupElementGenericTable(_In_ PRTL_GENERIC_TABLE Table, _In_ PVOID Buffer)
#define InterlockedIncrement64
Definition: interlocked.h:211
#define ASSERT(a)
Definition: mode.c:45
PMM_SECTION_SEGMENT NTAPI MmGetSectionAssociation(PFN_NUMBER Page, PLARGE_INTEGER Offset)
Definition: sptab.c:352
NTSTATUS NTAPI _MmSetPageEntrySectionSegment(PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset, ULONG_PTR Entry, const char *file, int line)
Definition: sptab.c:178
_Must_inspect_result_ NTSYSAPI PVOID NTAPI RtlGetElementGenericTable(_In_ PRTL_GENERIC_TABLE Table, _In_ ULONG I)
#define RMAP_SEGMENT_MASK
Definition: mm.h:868
LARGE_INTEGER FileOffset
Definition: newmm.h:27
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
NTSTATUS NTAPI MmSetSectionAssociation(PFN_NUMBER Page, PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset)
Definition: sptab.c:373
unsigned __int64 ULONG64
Definition: imports.h:198
#define PAGE_SIZE
Definition: env_spec_w32.h:49
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
#define ROUND_DOWN(n, align)
Definition: eventvwr.h:30
NTSYSAPI BOOLEAN NTAPI RtlDeleteElementGenericTable(_In_ PRTL_GENERIC_TABLE Table, _In_ PVOID Buffer)
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
VOID NTAPI MiInitializeSectionPageTable(PMM_SECTION_SEGMENT Segment)
Definition: sptab.c:165
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:159
#define IS_SWAP_FROM_SSE(E)
Definition: mm.h:1264
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
ULONG KSPIN_LOCK
Definition: env_spec_w32.h:72
#define NULL
Definition: types.h:112
enum _RTL_GENERIC_COMPARE_RESULTS RTL_GENERIC_COMPARE_RESULTS
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define ULONG_PTR
Definition: config.h:101
VOID NTAPI MmDereferenceSegment(PMM_SECTION_SEGMENT Segment)
Definition: section.c:991
#define STATUS_SUCCESS
Definition: shellext.h:65
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define DPRINT
Definition: sndvol32.h:71
#define DPRINTC
Definition: sptab.c:61
NTSYSAPI PVOID NTAPI RtlInsertElementGenericTable(_In_ PRTL_GENERIC_TABLE Table, _In_reads_bytes_(BufferSize) PVOID Buffer, _In_ CLONG BufferSize, _Out_opt_ PBOOLEAN NewElement)
base of all file and directory entries
Definition: entries.h:82
LONGLONG QuadPart
Definition: typedefs.h:114
#define ENTRIES_PER_ELEMENT
Definition: newmm.h:21
Definition: fci.c:126