* Sync up to trunk head (r65095).
[reactos.git] / ntoskrnl / mm / section.c
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/mm/section.c
21 * PURPOSE: Implements section objects
22 *
23 * PROGRAMMERS: Rex Jolliff
24 * David Welch
25 * Eric Kohl
26 * Emanuele Aliberti
27 * Eugene Ingerman
28 * Casper Hornstrup
29 * KJK::Hyperion
30 * Guido de Jong
31 * Ge van Geldorp
32 * Royce Mitchell III
33 * Filip Navara
34 * Aleksey Bragin
35 * Jason Filby
36 * Thomas Weidenmueller
37 * Gunnar Andre' Dalsnes
38 * Mike Nordell
39 * Alex Ionescu
40 * Gregor Anich
41 * Steven Edwards
42 * Herve Poussineau
43 */
44
45 /* INCLUDES *****************************************************************/
46
47 #include <ntoskrnl.h>
48 #include "../cache/newcc.h"
49 #include "../cache/section/newmm.h"
50 #define NDEBUG
51 #include <debug.h>
52 #include <reactos/exeformat.h>
53
54 #if defined (ALLOC_PRAGMA)
55 #pragma alloc_text(INIT, MmCreatePhysicalMemorySection)
56 #pragma alloc_text(INIT, MmInitSectionImplementation)
57 #endif
58
59 #include "ARM3/miarm.h"
60
61 #undef MmSetPageEntrySectionSegment
62 #define MmSetPageEntrySectionSegment(S,O,E) do { \
63 DPRINT("SetPageEntrySectionSegment(old,%p,%x,%x)\n",(S),(O)->LowPart,E); \
64 _MmSetPageEntrySectionSegment((S),(O),(E),__FILE__,__LINE__); \
65 } while (0)
66
67 extern MMSESSION MmSession;
68
69 NTSTATUS
70 NTAPI
71 MiMapViewInSystemSpace(IN PVOID Section,
72 IN PVOID Session,
73 OUT PVOID *MappedBase,
74 IN OUT PSIZE_T ViewSize);
75
76 NTSTATUS
77 NTAPI
78 MmCreateArm3Section(OUT PVOID *SectionObject,
79 IN ACCESS_MASK DesiredAccess,
80 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
81 IN PLARGE_INTEGER InputMaximumSize,
82 IN ULONG SectionPageProtection,
83 IN ULONG AllocationAttributes,
84 IN HANDLE FileHandle OPTIONAL,
85 IN PFILE_OBJECT FileObject OPTIONAL);
86
87 NTSTATUS
88 NTAPI
89 MmMapViewOfArm3Section(IN PVOID SectionObject,
90 IN PEPROCESS Process,
91 IN OUT PVOID *BaseAddress,
92 IN ULONG_PTR ZeroBits,
93 IN SIZE_T CommitSize,
94 IN OUT PLARGE_INTEGER SectionOffset OPTIONAL,
95 IN OUT PSIZE_T ViewSize,
96 IN SECTION_INHERIT InheritDisposition,
97 IN ULONG AllocationType,
98 IN ULONG Protect);
99
100 //
101 // PeFmtCreateSection depends on the following:
102 //
103 C_ASSERT(EXEFMT_LOAD_HEADER_SIZE >= sizeof(IMAGE_DOS_HEADER));
104 C_ASSERT(sizeof(IMAGE_NT_HEADERS32) <= sizeof(IMAGE_NT_HEADERS64));
105
106 C_ASSERT(TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) == TYPE_ALIGNMENT(IMAGE_NT_HEADERS64));
107 C_ASSERT(RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS32, FileHeader) == RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS64, FileHeader));
108 C_ASSERT(FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader) == FIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader));
109
110 C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Magic));
111 C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SectionAlignment));
112 C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, FileAlignment));
113 C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Subsystem));
114 C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MinorSubsystemVersion));
115 C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MajorSubsystemVersion));
116 C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, AddressOfEntryPoint));
117 C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfCode));
118 C_ASSERT(PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfHeaders));
119
120 /* TYPES *********************************************************************/
121
122 typedef struct
123 {
124 PROS_SECTION_OBJECT Section;
125 PMM_SECTION_SEGMENT Segment;
126 LARGE_INTEGER Offset;
127 BOOLEAN WasDirty;
128 BOOLEAN Private;
129 PEPROCESS CallingProcess;
130 ULONG_PTR SectionEntry;
131 }
132 MM_SECTION_PAGEOUT_CONTEXT;
133
134 /* GLOBALS *******************************************************************/
135
136 POBJECT_TYPE MmSectionObjectType = NULL;
137
138 ULONG_PTR MmSubsectionBase;
139
140 static ULONG SectionCharacteristicsToProtect[16] =
141 {
142 PAGE_NOACCESS, /* 0 = NONE */
143 PAGE_NOACCESS, /* 1 = SHARED */
144 PAGE_EXECUTE, /* 2 = EXECUTABLE */
145 PAGE_EXECUTE, /* 3 = EXECUTABLE, SHARED */
146 PAGE_READONLY, /* 4 = READABLE */
147 PAGE_READONLY, /* 5 = READABLE, SHARED */
148 PAGE_EXECUTE_READ, /* 6 = READABLE, EXECUTABLE */
149 PAGE_EXECUTE_READ, /* 7 = READABLE, EXECUTABLE, SHARED */
150 /*
151 * FIXME? do we really need the WriteCopy field in segments? can't we use
152 * PAGE_WRITECOPY here?
153 */
154 PAGE_READWRITE, /* 8 = WRITABLE */
155 PAGE_READWRITE, /* 9 = WRITABLE, SHARED */
156 PAGE_EXECUTE_READWRITE, /* 10 = WRITABLE, EXECUTABLE */
157 PAGE_EXECUTE_READWRITE, /* 11 = WRITABLE, EXECUTABLE, SHARED */
158 PAGE_READWRITE, /* 12 = WRITABLE, READABLE */
159 PAGE_READWRITE, /* 13 = WRITABLE, READABLE, SHARED */
160 PAGE_EXECUTE_READWRITE, /* 14 = WRITABLE, READABLE, EXECUTABLE */
161 PAGE_EXECUTE_READWRITE, /* 15 = WRITABLE, READABLE, EXECUTABLE, SHARED */
162 };
163
164 extern ULONG MmMakeFileAccess [];
165 ACCESS_MASK NTAPI MiArm3GetCorrectFileAccessMask(IN ACCESS_MASK SectionPageProtection);
166 static GENERIC_MAPPING MmpSectionMapping =
167 {
168 STANDARD_RIGHTS_READ | SECTION_MAP_READ | SECTION_QUERY,
169 STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE,
170 STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE,
171 SECTION_ALL_ACCESS
172 };
173
174 static const INFORMATION_CLASS_INFO ExSectionInfoClass[] =
175 {
176 ICI_SQ_SAME( sizeof(SECTION_BASIC_INFORMATION), sizeof(ULONG), ICIF_QUERY ), /* SectionBasicInformation */
177 ICI_SQ_SAME( sizeof(SECTION_IMAGE_INFORMATION), sizeof(ULONG), ICIF_QUERY ), /* SectionImageInformation */
178 };
179
180 /* FUNCTIONS *****************************************************************/
181
182
183 /*
184 References:
185 [1] Microsoft Corporation, "Microsoft Portable Executable and Common Object
186 File Format Specification", revision 6.0 (February 1999)
187 */
188 NTSTATUS NTAPI PeFmtCreateSection(IN CONST VOID * FileHeader,
189 IN SIZE_T FileHeaderSize,
190 IN PVOID File,
191 OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
192 OUT PULONG Flags,
193 IN PEXEFMT_CB_READ_FILE ReadFileCb,
194 IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb)
195 {
196 NTSTATUS nStatus;
197 ULONG cbFileHeaderOffsetSize = 0;
198 ULONG cbSectionHeadersOffset = 0;
199 ULONG cbSectionHeadersSize;
200 ULONG cbSectionHeadersOffsetSize = 0;
201 ULONG cbOptHeaderSize;
202 ULONG cbHeadersSize = 0;
203 ULONG nSectionAlignment;
204 ULONG nFileAlignment;
205 ULONG_PTR ImageBase;
206 const IMAGE_DOS_HEADER * pidhDosHeader;
207 const IMAGE_NT_HEADERS32 * pinhNtHeader;
208 const IMAGE_OPTIONAL_HEADER32 * piohOptHeader;
209 const IMAGE_SECTION_HEADER * pishSectionHeaders;
210 PMM_SECTION_SEGMENT pssSegments;
211 LARGE_INTEGER lnOffset;
212 PVOID pBuffer;
213 SIZE_T nPrevVirtualEndOfSegment = 0;
214 ULONG nFileSizeOfHeaders = 0;
215 ULONG i;
216
217 ASSERT(FileHeader);
218 ASSERT(FileHeaderSize > 0);
219 ASSERT(File);
220 ASSERT(ImageSectionObject);
221 ASSERT(ReadFileCb);
222 ASSERT(AllocateSegmentsCb);
223
224 ASSERT(Intsafe_CanOffsetPointer(FileHeader, FileHeaderSize));
225
226 ASSERT(((UINT_PTR)FileHeader % TYPE_ALIGNMENT(IMAGE_DOS_HEADER)) == 0);
227
228 #define DIE(ARGS_) { DPRINT ARGS_; goto l_Return; }
229
230 pBuffer = NULL;
231 pidhDosHeader = FileHeader;
232
233 /* DOS HEADER */
234 nStatus = STATUS_ROS_EXEFMT_UNKNOWN_FORMAT;
235
236 /* image too small to be an MZ executable */
237 if(FileHeaderSize < sizeof(IMAGE_DOS_HEADER))
238 DIE(("Too small to be an MZ executable, size is %lu\n", FileHeaderSize));
239
240 /* no MZ signature */
241 if(pidhDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
242 DIE(("No MZ signature found, e_magic is %hX\n", pidhDosHeader->e_magic));
243
244 /* not a Windows executable */
245 if(pidhDosHeader->e_lfanew <= 0)
246 DIE(("Not a Windows executable, e_lfanew is %d\n", pidhDosHeader->e_lfanew));
247
248 /* NT HEADER */
249 nStatus = STATUS_INVALID_IMAGE_FORMAT;
250
251 if(!Intsafe_AddULong32(&cbFileHeaderOffsetSize, pidhDosHeader->e_lfanew, RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS32, FileHeader)))
252 DIE(("The DOS stub is too large, e_lfanew is %X\n", pidhDosHeader->e_lfanew));
253
254 if(FileHeaderSize < cbFileHeaderOffsetSize)
255 pinhNtHeader = NULL;
256 else
257 {
258 /*
259 * we already know that Intsafe_CanOffsetPointer(FileHeader, FileHeaderSize),
260 * and FileHeaderSize >= cbFileHeaderOffsetSize, so this holds true too
261 */
262 ASSERT(Intsafe_CanOffsetPointer(FileHeader, pidhDosHeader->e_lfanew));
263 pinhNtHeader = (PVOID)((UINT_PTR)FileHeader + pidhDosHeader->e_lfanew);
264 }
265
266 /*
267 * the buffer doesn't contain the NT file header, or the alignment is wrong: we
268 * need to read the header from the file
269 */
270 if(FileHeaderSize < cbFileHeaderOffsetSize ||
271 (UINT_PTR)pinhNtHeader % TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) != 0)
272 {
273 ULONG cbNtHeaderSize;
274 ULONG cbReadSize;
275 PVOID pData;
276
277 l_ReadHeaderFromFile:
278 cbNtHeaderSize = 0;
279 lnOffset.QuadPart = pidhDosHeader->e_lfanew;
280
281 /* read the header from the file */
282 nStatus = ReadFileCb(File, &lnOffset, sizeof(IMAGE_NT_HEADERS64), &pData, &pBuffer, &cbReadSize);
283
284 if(!NT_SUCCESS(nStatus))
285 {
286 NTSTATUS ReturnedStatus = nStatus;
287
288 /* If it attempted to read past the end of the file, it means e_lfanew is invalid */
289 if (ReturnedStatus == STATUS_END_OF_FILE) nStatus = STATUS_ROS_EXEFMT_UNKNOWN_FORMAT;
290
291 DIE(("ReadFile failed, status %08X\n", ReturnedStatus));
292 }
293
294 ASSERT(pData);
295 ASSERT(pBuffer);
296 ASSERT(cbReadSize > 0);
297
298 nStatus = STATUS_INVALID_IMAGE_FORMAT;
299
300 /* the buffer doesn't contain the file header */
301 if(cbReadSize < RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS32, FileHeader))
302 DIE(("The file doesn't contain the PE file header\n"));
303
304 pinhNtHeader = pData;
305
306 /* object still not aligned: copy it to the beginning of the buffer */
307 if((UINT_PTR)pinhNtHeader % TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) != 0)
308 {
309 ASSERT((UINT_PTR)pBuffer % TYPE_ALIGNMENT(IMAGE_NT_HEADERS32) == 0);
310 RtlMoveMemory(pBuffer, pData, cbReadSize);
311 pinhNtHeader = pBuffer;
312 }
313
314 /* invalid NT header */
315 nStatus = STATUS_INVALID_IMAGE_PROTECT;
316
317 if(pinhNtHeader->Signature != IMAGE_NT_SIGNATURE)
318 DIE(("The file isn't a PE executable, Signature is %X\n", pinhNtHeader->Signature));
319
320 nStatus = STATUS_INVALID_IMAGE_FORMAT;
321
322 if(!Intsafe_AddULong32(&cbNtHeaderSize, pinhNtHeader->FileHeader.SizeOfOptionalHeader, FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader)))
323 DIE(("The full NT header is too large\n"));
324
325 /* the buffer doesn't contain the whole NT header */
326 if(cbReadSize < cbNtHeaderSize)
327 DIE(("The file doesn't contain the full NT header\n"));
328 }
329 else
330 {
331 ULONG cbOptHeaderOffsetSize = 0;
332
333 nStatus = STATUS_INVALID_IMAGE_FORMAT;
334
335 /* don't trust an invalid NT header */
336 if(pinhNtHeader->Signature != IMAGE_NT_SIGNATURE)
337 DIE(("The file isn't a PE executable, Signature is %X\n", pinhNtHeader->Signature));
338
339 if(!Intsafe_AddULong32(&cbOptHeaderOffsetSize, pidhDosHeader->e_lfanew, FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader)))
340 DIE(("The DOS stub is too large, e_lfanew is %X\n", pidhDosHeader->e_lfanew));
341
342 if(!Intsafe_AddULong32(&cbOptHeaderOffsetSize, cbOptHeaderOffsetSize, pinhNtHeader->FileHeader.SizeOfOptionalHeader))
343 DIE(("The NT header is too large, SizeOfOptionalHeader is %X\n", pinhNtHeader->FileHeader.SizeOfOptionalHeader));
344
345 /* the buffer doesn't contain the whole NT header: read it from the file */
346 if(cbOptHeaderOffsetSize > FileHeaderSize)
347 goto l_ReadHeaderFromFile;
348 }
349
350 /* read information from the NT header */
351 piohOptHeader = &pinhNtHeader->OptionalHeader;
352 cbOptHeaderSize = pinhNtHeader->FileHeader.SizeOfOptionalHeader;
353
354 nStatus = STATUS_INVALID_IMAGE_FORMAT;
355
356 if(!RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Magic))
357 DIE(("The optional header doesn't contain the Magic field, SizeOfOptionalHeader is %X\n", cbOptHeaderSize));
358
359 /* ASSUME: RtlZeroMemory(ImageSectionObject, sizeof(*ImageSectionObject)); */
360
361 switch(piohOptHeader->Magic)
362 {
363 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
364 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
365 break;
366
367 default:
368 DIE(("Unrecognized optional header, Magic is %X\n", piohOptHeader->Magic));
369 }
370
371 if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SectionAlignment) &&
372 RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, FileAlignment))
373 {
374 /* See [1], section 3.4.2 */
375 if(piohOptHeader->SectionAlignment < PAGE_SIZE)
376 {
377 if(piohOptHeader->FileAlignment != piohOptHeader->SectionAlignment)
378 DIE(("Sections aren't page-aligned and the file alignment isn't the same\n"));
379 }
380 else if(piohOptHeader->SectionAlignment < piohOptHeader->FileAlignment)
381 DIE(("The section alignment is smaller than the file alignment\n"));
382
383 nSectionAlignment = piohOptHeader->SectionAlignment;
384 nFileAlignment = piohOptHeader->FileAlignment;
385
386 if(!IsPowerOf2(nSectionAlignment) || !IsPowerOf2(nFileAlignment))
387 DIE(("The section alignment (%u) and file alignment (%u) aren't both powers of 2\n", nSectionAlignment, nFileAlignment));
388 }
389 else
390 {
391 nSectionAlignment = PAGE_SIZE;
392 nFileAlignment = PAGE_SIZE;
393 }
394
395 ASSERT(IsPowerOf2(nSectionAlignment));
396 ASSERT(IsPowerOf2(nFileAlignment));
397
398 switch(piohOptHeader->Magic)
399 {
400 /* PE32 */
401 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
402 {
403 if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, ImageBase))
404 ImageBase = piohOptHeader->ImageBase;
405
406 if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfImage))
407 ImageSectionObject->ImageInformation.ImageFileSize = piohOptHeader->SizeOfImage;
408
409 if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackReserve))
410 ImageSectionObject->ImageInformation.MaximumStackSize = piohOptHeader->SizeOfStackReserve;
411
412 if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfStackCommit))
413 ImageSectionObject->ImageInformation.CommittedStackSize = piohOptHeader->SizeOfStackCommit;
414
415 if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, Subsystem))
416 {
417 ImageSectionObject->ImageInformation.SubSystemType = piohOptHeader->Subsystem;
418
419 if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
420 RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, MajorSubsystemVersion))
421 {
422 ImageSectionObject->ImageInformation.SubSystemMinorVersion = piohOptHeader->MinorSubsystemVersion;
423 ImageSectionObject->ImageInformation.SubSystemMajorVersion = piohOptHeader->MajorSubsystemVersion;
424 }
425 }
426
427 if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
428 {
429 ImageSectionObject->ImageInformation.TransferAddress = (PVOID) (ImageBase +
430 piohOptHeader->AddressOfEntryPoint);
431 }
432
433 if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfCode))
434 ImageSectionObject->ImageInformation.ImageContainsCode = piohOptHeader->SizeOfCode != 0;
435 else
436 ImageSectionObject->ImageInformation.ImageContainsCode = TRUE;
437
438 if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, AddressOfEntryPoint))
439 {
440 if (piohOptHeader->AddressOfEntryPoint == 0)
441 {
442 ImageSectionObject->ImageInformation.ImageContainsCode = FALSE;
443 }
444 }
445
446 if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, LoaderFlags))
447 ImageSectionObject->ImageInformation.LoaderFlags = piohOptHeader->LoaderFlags;
448
449 if (RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, DllCharacteristics))
450 {
451 ImageSectionObject->ImageInformation.DllCharacteristics = piohOptHeader->DllCharacteristics;
452
453 /*
454 * Since we don't really implement SxS yet and LD doesn't supoprt /ALLOWISOLATION:NO, hard-code
455 * this flag here, which will prevent the loader and other code from doing any .manifest or SxS
456 * magic to any binary.
457 *
458 * This will break applications that depend on SxS when running with real Windows Kernel32/SxS/etc
459 * but honestly that's not tested. It will also break them when running no ReactOS once we implement
460 * the SxS support -- at which point, duh, this should be removed.
461 *
462 * But right now, any app depending on SxS is already broken anyway, so this flag only helps.
463 */
464 ImageSectionObject->ImageInformation.DllCharacteristics |= IMAGE_DLLCHARACTERISTICS_NO_ISOLATION;
465 }
466
467 break;
468 }
469 #ifdef _WIN64
470 /* PE64 */
471 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
472 {
473 const IMAGE_OPTIONAL_HEADER64 * pioh64OptHeader;
474
475 pioh64OptHeader = (const IMAGE_OPTIONAL_HEADER64 *)piohOptHeader;
476
477 if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, ImageBase))
478 {
479 ImageBase = pioh64OptHeader->ImageBase;
480 if(pioh64OptHeader->ImageBase > MAXULONG_PTR)
481 DIE(("ImageBase exceeds the address space\n"));
482 }
483
484 if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfImage))
485 {
486 if(pioh64OptHeader->SizeOfImage > MAXULONG_PTR)
487 DIE(("SizeOfImage exceeds the address space\n"));
488
489 ImageSectionObject->ImageInformation.ImageFileSize = pioh64OptHeader->SizeOfImage;
490 }
491
492 if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackReserve))
493 {
494 if(pioh64OptHeader->SizeOfStackReserve > MAXULONG_PTR)
495 DIE(("SizeOfStackReserve exceeds the address space\n"));
496
497 ImageSectionObject->ImageInformation.MaximumStackSize = (ULONG_PTR) pioh64OptHeader->SizeOfStackReserve;
498 }
499
500 if(RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfStackCommit))
501 {
502 if(pioh64OptHeader->SizeOfStackCommit > MAXULONG_PTR)
503 DIE(("SizeOfStackCommit exceeds the address space\n"));
504
505 ImageSectionObject->ImageInformation.CommittedStackSize = (ULONG_PTR) pioh64OptHeader->SizeOfStackCommit;
506 }
507
508 if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, Subsystem))
509 {
510 ImageSectionObject->ImageInformation.SubSystemType = pioh64OptHeader->Subsystem;
511
512 if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, MinorSubsystemVersion) &&
513 RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, MajorSubsystemVersion))
514 {
515 ImageSectionObject->ImageInformation.SubSystemMinorVersion = pioh64OptHeader->MinorSubsystemVersion;
516 ImageSectionObject->ImageInformation.SubSystemMajorVersion = pioh64OptHeader->MajorSubsystemVersion;
517 }
518 }
519
520 if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, AddressOfEntryPoint))
521 {
522 ImageSectionObject->ImageInformation.TransferAddress = (PVOID) (ImageBase +
523 pioh64OptHeader->AddressOfEntryPoint);
524 }
525
526 if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, SizeOfCode))
527 ImageSectionObject->ImageInformation.ImageContainsCode = pioh64OptHeader->SizeOfCode != 0;
528 else
529 ImageSectionObject->ImageInformation.ImageContainsCode = TRUE;
530
531 if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, AddressOfEntryPoint))
532 {
533 if (pioh64OptHeader->AddressOfEntryPoint == 0)
534 {
535 ImageSectionObject->ImageInformation.ImageContainsCode = FALSE;
536 }
537 }
538
539 if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, LoaderFlags))
540 ImageSectionObject->ImageInformation.LoaderFlags = pioh64OptHeader->LoaderFlags;
541
542 if (RTL_CONTAINS_FIELD(pioh64OptHeader, cbOptHeaderSize, DllCharacteristics))
543 ImageSectionObject->ImageInformation.DllCharacteristics = pioh64OptHeader->DllCharacteristics;
544
545 break;
546 }
547 #endif // _WIN64
548 }
549
550 /* [1], section 3.4.2 */
551 if((ULONG_PTR)ImageBase % 0x10000)
552 DIE(("ImageBase is not aligned on a 64KB boundary"));
553
554 ImageSectionObject->ImageInformation.ImageCharacteristics = pinhNtHeader->FileHeader.Characteristics;
555 ImageSectionObject->ImageInformation.Machine = pinhNtHeader->FileHeader.Machine;
556 ImageSectionObject->ImageInformation.GpValue = 0;
557 ImageSectionObject->ImageInformation.ZeroBits = 0;
558 ImageSectionObject->BasedAddress = (PVOID)ImageBase;
559
560 /* SECTION HEADERS */
561 nStatus = STATUS_INVALID_IMAGE_FORMAT;
562
563 /* see [1], section 3.3 */
564 if(pinhNtHeader->FileHeader.NumberOfSections > 96)
565 DIE(("Too many sections, NumberOfSections is %u\n", pinhNtHeader->FileHeader.NumberOfSections));
566
567 /*
568 * the additional segment is for the file's headers. They need to be present for
569 * the benefit of the dynamic loader (to locate exports, defaults for thread
570 * parameters, resources, etc.)
571 */
572 ImageSectionObject->NrSegments = pinhNtHeader->FileHeader.NumberOfSections + 1;
573
574 /* file offset for the section headers */
575 if(!Intsafe_AddULong32(&cbSectionHeadersOffset, pidhDosHeader->e_lfanew, FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader)))
576 DIE(("Offset overflow\n"));
577
578 if(!Intsafe_AddULong32(&cbSectionHeadersOffset, cbSectionHeadersOffset, pinhNtHeader->FileHeader.SizeOfOptionalHeader))
579 DIE(("Offset overflow\n"));
580
581 /* size of the section headers */
582 ASSERT(Intsafe_CanMulULong32(pinhNtHeader->FileHeader.NumberOfSections, sizeof(IMAGE_SECTION_HEADER)));
583 cbSectionHeadersSize = pinhNtHeader->FileHeader.NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
584
585 if(!Intsafe_AddULong32(&cbSectionHeadersOffsetSize, cbSectionHeadersOffset, cbSectionHeadersSize))
586 DIE(("Section headers too large\n"));
587
588 /* size of the executable's headers */
589 if(RTL_CONTAINS_FIELD(piohOptHeader, cbOptHeaderSize, SizeOfHeaders))
590 {
591 // if(!IsAligned(piohOptHeader->SizeOfHeaders, nFileAlignment))
592 // DIE(("SizeOfHeaders is not aligned\n"));
593
594 if(cbSectionHeadersSize > piohOptHeader->SizeOfHeaders)
595 DIE(("The section headers overflow SizeOfHeaders\n"));
596
597 cbHeadersSize = piohOptHeader->SizeOfHeaders;
598 }
599 else if(!AlignUp(&cbHeadersSize, cbSectionHeadersOffsetSize, nFileAlignment))
600 DIE(("Overflow aligning the size of headers\n"));
601
602 if(pBuffer)
603 {
604 ExFreePool(pBuffer);
605 pBuffer = NULL;
606 }
607 /* WARNING: pinhNtHeader IS NO LONGER USABLE */
608 /* WARNING: piohOptHeader IS NO LONGER USABLE */
609 /* WARNING: pioh64OptHeader IS NO LONGER USABLE */
610
611 if(FileHeaderSize < cbSectionHeadersOffsetSize)
612 pishSectionHeaders = NULL;
613 else
614 {
615 /*
616 * we already know that Intsafe_CanOffsetPointer(FileHeader, FileHeaderSize),
617 * and FileHeaderSize >= cbSectionHeadersOffsetSize, so this holds true too
618 */
619 ASSERT(Intsafe_CanOffsetPointer(FileHeader, cbSectionHeadersOffset));
620 pishSectionHeaders = (PVOID)((UINT_PTR)FileHeader + cbSectionHeadersOffset);
621 }
622
623 /*
624 * the buffer doesn't contain the section headers, or the alignment is wrong:
625 * read the headers from the file
626 */
627 if(FileHeaderSize < cbSectionHeadersOffsetSize ||
628 (UINT_PTR)pishSectionHeaders % TYPE_ALIGNMENT(IMAGE_SECTION_HEADER) != 0)
629 {
630 PVOID pData;
631 ULONG cbReadSize;
632
633 lnOffset.QuadPart = cbSectionHeadersOffset;
634
635 /* read the header from the file */
636 nStatus = ReadFileCb(File, &lnOffset, cbSectionHeadersSize, &pData, &pBuffer, &cbReadSize);
637
638 if(!NT_SUCCESS(nStatus))
639 DIE(("ReadFile failed with status %08X\n", nStatus));
640
641 ASSERT(pData);
642 ASSERT(pBuffer);
643 ASSERT(cbReadSize > 0);
644
645 nStatus = STATUS_INVALID_IMAGE_FORMAT;
646
647 /* the buffer doesn't contain all the section headers */
648 if(cbReadSize < cbSectionHeadersSize)
649 DIE(("The file doesn't contain all of the section headers\n"));
650
651 pishSectionHeaders = pData;
652
653 /* object still not aligned: copy it to the beginning of the buffer */
654 if((UINT_PTR)pishSectionHeaders % TYPE_ALIGNMENT(IMAGE_SECTION_HEADER) != 0)
655 {
656 ASSERT((UINT_PTR)pBuffer % TYPE_ALIGNMENT(IMAGE_SECTION_HEADER) == 0);
657 RtlMoveMemory(pBuffer, pData, cbReadSize);
658 pishSectionHeaders = pBuffer;
659 }
660 }
661
662 /* SEGMENTS */
663 /* allocate the segments */
664 nStatus = STATUS_INSUFFICIENT_RESOURCES;
665 ImageSectionObject->Segments = AllocateSegmentsCb(ImageSectionObject->NrSegments);
666
667 if(ImageSectionObject->Segments == NULL)
668 DIE(("AllocateSegments failed\n"));
669
670 /* initialize the headers segment */
671 pssSegments = ImageSectionObject->Segments;
672
673 // ASSERT(IsAligned(cbHeadersSize, nFileAlignment));
674
675 if(!AlignUp(&nFileSizeOfHeaders, cbHeadersSize, nFileAlignment))
676 DIE(("Cannot align the size of the section headers\n"));
677
678 nPrevVirtualEndOfSegment = ALIGN_UP_BY(cbHeadersSize, nSectionAlignment);
679 if (nPrevVirtualEndOfSegment < cbHeadersSize)
680 DIE(("Cannot align the size of the section headers\n"));
681
682 pssSegments[0].Image.FileOffset = 0;
683 pssSegments[0].Protection = PAGE_READONLY;
684 pssSegments[0].Length.QuadPart = nPrevVirtualEndOfSegment;
685 pssSegments[0].RawLength.QuadPart = nFileSizeOfHeaders;
686 pssSegments[0].Image.VirtualAddress = 0;
687 pssSegments[0].Image.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA;
688 pssSegments[0].WriteCopy = TRUE;
689
690 /* skip the headers segment */
691 ++ pssSegments;
692
693 nStatus = STATUS_INVALID_IMAGE_FORMAT;
694
695 /* convert the executable sections into segments. See also [1], section 4 */
696 for(i = 0; i < ImageSectionObject->NrSegments - 1; ++ i)
697 {
698 ULONG nCharacteristics;
699
700 /* validate the alignment */
701 if(!IsAligned(pishSectionHeaders[i].VirtualAddress, nSectionAlignment))
702 DIE(("Image.VirtualAddress[%u] is not aligned\n", i));
703
704 /* sections must be contiguous, ordered by base address and non-overlapping */
705 if(pishSectionHeaders[i].VirtualAddress != nPrevVirtualEndOfSegment)
706 DIE(("Memory gap between section %u and the previous\n", i));
707
708 /* ignore explicit BSS sections */
709 if(pishSectionHeaders[i].SizeOfRawData != 0)
710 {
711 /* validate the alignment */
712 #if 0
713 /* Yes, this should be a multiple of FileAlignment, but there's
714 * stuff out there that isn't. We can cope with that
715 */
716 if(!IsAligned(pishSectionHeaders[i].SizeOfRawData, nFileAlignment))
717 DIE(("SizeOfRawData[%u] is not aligned\n", i));
718 #endif
719
720 // if(!IsAligned(pishSectionHeaders[i].PointerToRawData, nFileAlignment))
721 // DIE(("PointerToRawData[%u] is not aligned\n", i));
722
723 /* conversion */
724 pssSegments[i].Image.FileOffset = pishSectionHeaders[i].PointerToRawData;
725 pssSegments[i].RawLength.QuadPart = pishSectionHeaders[i].SizeOfRawData;
726 }
727 else
728 {
729 ASSERT(pssSegments[i].Image.FileOffset == 0);
730 ASSERT(pssSegments[i].RawLength.QuadPart == 0);
731 }
732
733 ASSERT(Intsafe_CanAddLong64(pssSegments[i].Image.FileOffset, pssSegments[i].RawLength.QuadPart));
734
735 nCharacteristics = pishSectionHeaders[i].Characteristics;
736
737 /* no explicit protection */
738 if((nCharacteristics & (IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE)) == 0)
739 {
740 if(nCharacteristics & IMAGE_SCN_CNT_CODE)
741 nCharacteristics |= IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ;
742
743 if(nCharacteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
744 nCharacteristics |= IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
745
746 if(nCharacteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
747 nCharacteristics |= IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
748 }
749
750 /* see table above */
751 pssSegments[i].Protection = SectionCharacteristicsToProtect[nCharacteristics >> 28];
752 pssSegments[i].WriteCopy = !(nCharacteristics & IMAGE_SCN_MEM_SHARED);
753
754 if(pishSectionHeaders[i].Misc.VirtualSize == 0 || pishSectionHeaders[i].Misc.VirtualSize < pishSectionHeaders[i].SizeOfRawData)
755 pssSegments[i].Length.QuadPart = pishSectionHeaders[i].SizeOfRawData;
756 else
757 pssSegments[i].Length.QuadPart = pishSectionHeaders[i].Misc.VirtualSize;
758
759 pssSegments[i].Length.LowPart = ALIGN_UP_BY(pssSegments[i].Length.LowPart, nSectionAlignment);
760 /* FIXME: always false */
761 if (pssSegments[i].Length.QuadPart < pssSegments[i].Length.QuadPart)
762 DIE(("Cannot align the virtual size of section %u\n", i));
763
764 if(pssSegments[i].Length.QuadPart == 0)
765 DIE(("Virtual size of section %u is null\n", i));
766
767 pssSegments[i].Image.VirtualAddress = pishSectionHeaders[i].VirtualAddress;
768 pssSegments[i].Image.Characteristics = pishSectionHeaders[i].Characteristics;
769
770 /* ensure the memory image is no larger than 4GB */
771 nPrevVirtualEndOfSegment = (ULONG_PTR)(pssSegments[i].Image.VirtualAddress + pssSegments[i].Length.QuadPart);
772 if (nPrevVirtualEndOfSegment < pssSegments[i].Image.VirtualAddress)
773 DIE(("The image is too large\n"));
774 }
775
776 if(nSectionAlignment >= PAGE_SIZE)
777 *Flags |= EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED;
778
779 /* Success */
780 nStatus = STATUS_SUCCESS;// STATUS_ROS_EXEFMT_LOADED_FORMAT | EXEFMT_LOADED_PE32;
781
782 l_Return:
783 if(pBuffer)
784 ExFreePool(pBuffer);
785
786 return nStatus;
787 }
788
789 /*
790 * FUNCTION: Waits in kernel mode indefinitely for a file object lock.
791 * ARGUMENTS: PFILE_OBJECT to wait for.
792 * RETURNS: Status of the wait.
793 */
794 NTSTATUS
795 MmspWaitForFileLock(PFILE_OBJECT File)
796 {
797 return STATUS_SUCCESS;
798 //return KeWaitForSingleObject(&File->Lock, 0, KernelMode, FALSE, NULL);
799 }
800
801 VOID
802 NTAPI
803 MmFreeSectionSegments(PFILE_OBJECT FileObject)
804 {
805 if (FileObject->SectionObjectPointer->ImageSectionObject != NULL)
806 {
807 PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
808 PMM_SECTION_SEGMENT SectionSegments;
809 ULONG NrSegments;
810 ULONG i;
811
812 ImageSectionObject = (PMM_IMAGE_SECTION_OBJECT)FileObject->SectionObjectPointer->ImageSectionObject;
813 NrSegments = ImageSectionObject->NrSegments;
814 SectionSegments = ImageSectionObject->Segments;
815 for (i = 0; i < NrSegments; i++)
816 {
817 if (SectionSegments[i].ReferenceCount != 0)
818 {
819 DPRINT1("Image segment %lu still referenced (was %lu)\n", i,
820 SectionSegments[i].ReferenceCount);
821 KeBugCheck(MEMORY_MANAGEMENT);
822 }
823 MmFreePageTablesSectionSegment(&SectionSegments[i], NULL);
824 }
825 ExFreePool(ImageSectionObject->Segments);
826 ExFreePool(ImageSectionObject);
827 FileObject->SectionObjectPointer->ImageSectionObject = NULL;
828 }
829 if (FileObject->SectionObjectPointer->DataSectionObject != NULL)
830 {
831 PMM_SECTION_SEGMENT Segment;
832
833 Segment = (PMM_SECTION_SEGMENT)FileObject->SectionObjectPointer->
834 DataSectionObject;
835
836 if (Segment->ReferenceCount != 0)
837 {
838 DPRINT1("Data segment still referenced\n");
839 KeBugCheck(MEMORY_MANAGEMENT);
840 }
841 MmFreePageTablesSectionSegment(Segment, NULL);
842 ExFreePool(Segment);
843 FileObject->SectionObjectPointer->DataSectionObject = NULL;
844 }
845 }
846
847 VOID
848 NTAPI
849 MmSharePageEntrySectionSegment(PMM_SECTION_SEGMENT Segment,
850 PLARGE_INTEGER Offset)
851 {
852 ULONG_PTR Entry;
853
854 Entry = MmGetPageEntrySectionSegment(Segment, Offset);
855 if (Entry == 0)
856 {
857 DPRINT1("Entry == 0 for MmSharePageEntrySectionSegment\n");
858 KeBugCheck(MEMORY_MANAGEMENT);
859 }
860 if (SHARE_COUNT_FROM_SSE(Entry) == MAX_SHARE_COUNT)
861 {
862 DPRINT1("Maximum share count reached\n");
863 KeBugCheck(MEMORY_MANAGEMENT);
864 }
865 if (IS_SWAP_FROM_SSE(Entry))
866 {
867 KeBugCheck(MEMORY_MANAGEMENT);
868 }
869 Entry = MAKE_SSE(PAGE_FROM_SSE(Entry), SHARE_COUNT_FROM_SSE(Entry) + 1);
870 MmSetPageEntrySectionSegment(Segment, Offset, Entry);
871 }
872
873 BOOLEAN
874 NTAPI
875 MmUnsharePageEntrySectionSegment(PROS_SECTION_OBJECT Section,
876 PMM_SECTION_SEGMENT Segment,
877 PLARGE_INTEGER Offset,
878 BOOLEAN Dirty,
879 BOOLEAN PageOut,
880 ULONG_PTR *InEntry)
881 {
882 ULONG_PTR Entry = InEntry ? *InEntry : MmGetPageEntrySectionSegment(Segment, Offset);
883 BOOLEAN IsDirectMapped = FALSE;
884
885 if (Entry == 0)
886 {
887 DPRINT1("Entry == 0 for MmUnsharePageEntrySectionSegment\n");
888 KeBugCheck(MEMORY_MANAGEMENT);
889 }
890 if (SHARE_COUNT_FROM_SSE(Entry) == 0)
891 {
892 DPRINT1("Zero share count for unshare (Seg %p Offset %x Page %x)\n", Segment, Offset->LowPart, PFN_FROM_SSE(Entry));
893 KeBugCheck(MEMORY_MANAGEMENT);
894 }
895 if (IS_SWAP_FROM_SSE(Entry))
896 {
897 KeBugCheck(MEMORY_MANAGEMENT);
898 }
899 Entry = MAKE_SSE(PAGE_FROM_SSE(Entry), SHARE_COUNT_FROM_SSE(Entry) - 1);
900 /*
901 * If we reducing the share count of this entry to zero then set the entry
902 * to zero and tell the cache the page is no longer mapped.
903 */
904 if (SHARE_COUNT_FROM_SSE(Entry) == 0)
905 {
906 PFILE_OBJECT FileObject;
907 #ifndef NEWCC
908 PROS_SHARED_CACHE_MAP SharedCacheMap;
909 #endif
910 SWAPENTRY SavedSwapEntry;
911 PFN_NUMBER Page;
912 BOOLEAN IsImageSection;
913 LARGE_INTEGER FileOffset;
914
915 FileOffset.QuadPart = Offset->QuadPart + Segment->Image.FileOffset;
916
917 IsImageSection = Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
918
919 Page = PFN_FROM_SSE(Entry);
920 FileObject = Section->FileObject;
921 if (FileObject != NULL &&
922 !(Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
923 {
924
925 #ifndef NEWCC
926 if ((FileOffset.QuadPart % PAGE_SIZE) == 0 &&
927 (Offset->QuadPart + PAGE_SIZE <= Segment->RawLength.QuadPart || !IsImageSection))
928 {
929 NTSTATUS Status;
930 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
931 IsDirectMapped = TRUE;
932 #ifndef NEWCC
933 Status = CcRosUnmapVacb(SharedCacheMap, FileOffset.QuadPart, Dirty);
934 #else
935 Status = STATUS_SUCCESS;
936 #endif
937 if (!NT_SUCCESS(Status))
938 {
939 DPRINT1("CcRosUnmapVacb failed, status = %x\n", Status);
940 KeBugCheck(MEMORY_MANAGEMENT);
941 }
942 }
943 #endif
944 }
945
946 SavedSwapEntry = MmGetSavedSwapEntryPage(Page);
947 if (SavedSwapEntry == 0)
948 {
949 if (!PageOut &&
950 ((Segment->Flags & MM_PAGEFILE_SEGMENT) ||
951 (Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED)))
952 {
953 /*
954 * FIXME:
955 * Try to page out this page and set the swap entry
956 * within the section segment. There exist no rmap entry
957 * for this page. The pager thread can't page out a
958 * page without a rmap entry.
959 */
960 MmSetPageEntrySectionSegment(Segment, Offset, Entry);
961 if (InEntry) *InEntry = Entry;
962 MiSetPageEvent(NULL, NULL);
963 }
964 else
965 {
966 MmSetPageEntrySectionSegment(Segment, Offset, 0);
967 if (InEntry) *InEntry = 0;
968 MiSetPageEvent(NULL, NULL);
969 if (!IsDirectMapped)
970 {
971 MmReleasePageMemoryConsumer(MC_USER, Page);
972 }
973 }
974 }
975 else
976 {
977 if ((Segment->Flags & MM_PAGEFILE_SEGMENT) ||
978 (Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
979 {
980 if (!PageOut)
981 {
982 if (Dirty)
983 {
984 /*
985 * FIXME:
986 * We hold all locks. Nobody can do something with the current
987 * process and the current segment (also not within an other process).
988 */
989 NTSTATUS Status;
990 Status = MmWriteToSwapPage(SavedSwapEntry, Page);
991 if (!NT_SUCCESS(Status))
992 {
993 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n", Status);
994 KeBugCheck(MEMORY_MANAGEMENT);
995 }
996 }
997 MmSetPageEntrySectionSegment(Segment, Offset, MAKE_SWAP_SSE(SavedSwapEntry));
998 if (InEntry) *InEntry = MAKE_SWAP_SSE(SavedSwapEntry);
999 MmSetSavedSwapEntryPage(Page, 0);
1000 MiSetPageEvent(NULL, NULL);
1001 }
1002 MmReleasePageMemoryConsumer(MC_USER, Page);
1003 }
1004 else
1005 {
1006 DPRINT1("Found a swapentry for a non private page in an image or data file sgment\n");
1007 KeBugCheck(MEMORY_MANAGEMENT);
1008 }
1009 }
1010 }
1011 else
1012 {
1013 if (InEntry)
1014 *InEntry = Entry;
1015 else
1016 MmSetPageEntrySectionSegment(Segment, Offset, Entry);
1017 }
1018 return(SHARE_COUNT_FROM_SSE(Entry) > 0);
1019 }
1020
1021 BOOLEAN MiIsPageFromCache(PMEMORY_AREA MemoryArea,
1022 LONGLONG SegOffset)
1023 {
1024 #ifndef NEWCC
1025 if (!(MemoryArea->Data.SectionData.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
1026 {
1027 PROS_SHARED_CACHE_MAP SharedCacheMap;
1028 PROS_VACB Vacb;
1029 SharedCacheMap = MemoryArea->Data.SectionData.Section->FileObject->SectionObjectPointer->SharedCacheMap;
1030 Vacb = CcRosLookupVacb(SharedCacheMap, SegOffset + MemoryArea->Data.SectionData.Segment->Image.FileOffset);
1031 if (Vacb)
1032 {
1033 CcRosReleaseVacb(SharedCacheMap, Vacb, Vacb->Valid, FALSE, TRUE);
1034 return TRUE;
1035 }
1036 }
1037 #endif
1038 return FALSE;
1039 }
1040
1041 NTSTATUS
1042 NTAPI
1043 MiCopyFromUserPage(PFN_NUMBER DestPage, PFN_NUMBER SrcPage)
1044 {
1045 PEPROCESS Process;
1046 KIRQL Irql, Irql2;
1047 PVOID DestAddress, SrcAddress;
1048
1049 Process = PsGetCurrentProcess();
1050 DestAddress = MiMapPageInHyperSpace(Process, DestPage, &Irql);
1051 SrcAddress = MiMapPageInHyperSpace(Process, SrcPage, &Irql2);
1052 if (DestAddress == NULL || SrcAddress == NULL)
1053 {
1054 return(STATUS_NO_MEMORY);
1055 }
1056 ASSERT((ULONG_PTR)DestAddress % PAGE_SIZE == 0);
1057 ASSERT((ULONG_PTR)SrcAddress % PAGE_SIZE == 0);
1058 RtlCopyMemory(DestAddress, SrcAddress, PAGE_SIZE);
1059 MiUnmapPageInHyperSpace(Process, SrcAddress, Irql2);
1060 MiUnmapPageInHyperSpace(Process, DestAddress, Irql);
1061 return(STATUS_SUCCESS);
1062 }
1063
1064 #ifndef NEWCC
1065 NTSTATUS
1066 NTAPI
1067 MiReadPage(PMEMORY_AREA MemoryArea,
1068 LONGLONG SegOffset,
1069 PPFN_NUMBER Page)
1070 /*
1071 * FUNCTION: Read a page for a section backed memory area.
1072 * PARAMETERS:
1073 * MemoryArea - Memory area to read the page for.
1074 * Offset - Offset of the page to read.
1075 * Page - Variable that receives a page contains the read data.
1076 */
1077 {
1078 LONGLONG BaseOffset;
1079 LONGLONG FileOffset;
1080 PVOID BaseAddress;
1081 BOOLEAN UptoDate;
1082 PROS_VACB Vacb;
1083 PFILE_OBJECT FileObject;
1084 NTSTATUS Status;
1085 LONGLONG RawLength;
1086 PROS_SHARED_CACHE_MAP SharedCacheMap;
1087 BOOLEAN IsImageSection;
1088 LONGLONG Length;
1089
1090 FileObject = MemoryArea->Data.SectionData.Section->FileObject;
1091 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1092 RawLength = MemoryArea->Data.SectionData.Segment->RawLength.QuadPart;
1093 FileOffset = SegOffset + MemoryArea->Data.SectionData.Segment->Image.FileOffset;
1094 IsImageSection = MemoryArea->Data.SectionData.Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
1095
1096 ASSERT(SharedCacheMap);
1097
1098 DPRINT("%S %I64x\n", FileObject->FileName.Buffer, FileOffset);
1099
1100 /*
1101 * If the file system is letting us go directly to the cache and the
1102 * memory area was mapped at an offset in the file which is page aligned
1103 * then get the related VACB.
1104 */
1105 if (((FileOffset % PAGE_SIZE) == 0) &&
1106 ((SegOffset + PAGE_SIZE <= RawLength) || !IsImageSection) &&
1107 !(MemoryArea->Data.SectionData.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
1108 {
1109
1110 /*
1111 * Get the related VACB; we use a lower level interface than
1112 * filesystems do because it is safe for us to use an offset with an
1113 * alignment less than the file system block size.
1114 */
1115 Status = CcRosGetVacb(SharedCacheMap,
1116 FileOffset,
1117 &BaseOffset,
1118 &BaseAddress,
1119 &UptoDate,
1120 &Vacb);
1121 if (!NT_SUCCESS(Status))
1122 {
1123 return(Status);
1124 }
1125 if (!UptoDate)
1126 {
1127 /*
1128 * If the VACB isn't up to date then call the file
1129 * system to read in the data.
1130 */
1131 Status = CcReadVirtualAddress(Vacb);
1132 if (!NT_SUCCESS(Status))
1133 {
1134 CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
1135 return Status;
1136 }
1137 }
1138
1139 /* Probe the page, since it's PDE might not be synced */
1140 (void)*((volatile char*)BaseAddress + FileOffset - BaseOffset);
1141
1142 /*
1143 * Retrieve the page from the view that we actually want.
1144 */
1145 (*Page) = MmGetPhysicalAddress((char*)BaseAddress +
1146 FileOffset - BaseOffset).LowPart >> PAGE_SHIFT;
1147
1148 CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, TRUE);
1149 }
1150 else
1151 {
1152 PEPROCESS Process;
1153 KIRQL Irql;
1154 PVOID PageAddr;
1155 LONGLONG VacbOffset;
1156
1157 /*
1158 * Allocate a page, this is rather complicated by the possibility
1159 * we might have to move other things out of memory
1160 */
1161 MI_SET_USAGE(MI_USAGE_SECTION);
1162 MI_SET_PROCESS2(PsGetCurrentProcess()->ImageFileName);
1163 Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, Page);
1164 if (!NT_SUCCESS(Status))
1165 {
1166 return(Status);
1167 }
1168 Status = CcRosGetVacb(SharedCacheMap,
1169 FileOffset,
1170 &BaseOffset,
1171 &BaseAddress,
1172 &UptoDate,
1173 &Vacb);
1174 if (!NT_SUCCESS(Status))
1175 {
1176 return(Status);
1177 }
1178 if (!UptoDate)
1179 {
1180 /*
1181 * If the VACB isn't up to date then call the file
1182 * system to read in the data.
1183 */
1184 Status = CcReadVirtualAddress(Vacb);
1185 if (!NT_SUCCESS(Status))
1186 {
1187 CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
1188 return Status;
1189 }
1190 }
1191
1192 Process = PsGetCurrentProcess();
1193 PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
1194 VacbOffset = BaseOffset + VACB_MAPPING_GRANULARITY - FileOffset;
1195 Length = RawLength - SegOffset;
1196 if (Length <= VacbOffset && Length <= PAGE_SIZE)
1197 {
1198 memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, Length);
1199 }
1200 else if (VacbOffset >= PAGE_SIZE)
1201 {
1202 memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, PAGE_SIZE);
1203 }
1204 else
1205 {
1206 memcpy(PageAddr, (char*)BaseAddress + FileOffset - BaseOffset, VacbOffset);
1207 MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
1208 CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, FALSE);
1209 Status = CcRosGetVacb(SharedCacheMap,
1210 FileOffset + VacbOffset,
1211 &BaseOffset,
1212 &BaseAddress,
1213 &UptoDate,
1214 &Vacb);
1215 if (!NT_SUCCESS(Status))
1216 {
1217 return(Status);
1218 }
1219 if (!UptoDate)
1220 {
1221 /*
1222 * If the VACB isn't up to date then call the file
1223 * system to read in the data.
1224 */
1225 Status = CcReadVirtualAddress(Vacb);
1226 if (!NT_SUCCESS(Status))
1227 {
1228 CcRosReleaseVacb(SharedCacheMap, Vacb, FALSE, FALSE, FALSE);
1229 return Status;
1230 }
1231 }
1232 PageAddr = MiMapPageInHyperSpace(Process, *Page, &Irql);
1233 if (Length < PAGE_SIZE)
1234 {
1235 memcpy((char*)PageAddr + VacbOffset, BaseAddress, Length - VacbOffset);
1236 }
1237 else
1238 {
1239 memcpy((char*)PageAddr + VacbOffset, BaseAddress, PAGE_SIZE - VacbOffset);
1240 }
1241 }
1242 MiUnmapPageInHyperSpace(Process, PageAddr, Irql);
1243 CcRosReleaseVacb(SharedCacheMap, Vacb, TRUE, FALSE, FALSE);
1244 }
1245 return(STATUS_SUCCESS);
1246 }
1247 #else
1248 NTSTATUS
1249 NTAPI
1250 MiReadPage(PMEMORY_AREA MemoryArea,
1251 LONGLONG SegOffset,
1252 PPFN_NUMBER Page)
1253 /*
1254 * FUNCTION: Read a page for a section backed memory area.
1255 * PARAMETERS:
1256 * MemoryArea - Memory area to read the page for.
1257 * Offset - Offset of the page to read.
1258 * Page - Variable that receives a page contains the read data.
1259 */
1260 {
1261 MM_REQUIRED_RESOURCES Resources;
1262 NTSTATUS Status;
1263
1264 RtlZeroMemory(&Resources, sizeof(MM_REQUIRED_RESOURCES));
1265
1266 Resources.Context = MemoryArea->Data.SectionData.Section->FileObject;
1267 Resources.FileOffset.QuadPart = SegOffset +
1268 MemoryArea->Data.SectionData.Segment->Image.FileOffset;
1269 Resources.Consumer = MC_USER;
1270 Resources.Amount = PAGE_SIZE;
1271
1272 DPRINT("%S, offset 0x%x, len 0x%x, page 0x%x\n", ((PFILE_OBJECT)Resources.Context)->FileName.Buffer, Resources.FileOffset.LowPart, Resources.Amount, Resources.Page[0]);
1273
1274 Status = MiReadFilePage(MmGetKernelAddressSpace(), MemoryArea, &Resources);
1275 *Page = Resources.Page[0];
1276 return Status;
1277 }
1278 #endif
1279
1280 NTSTATUS
1281 NTAPI
1282 MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
1283 MEMORY_AREA* MemoryArea,
1284 PVOID Address,
1285 BOOLEAN Locked)
1286 {
1287 LARGE_INTEGER Offset;
1288 PFN_NUMBER Page;
1289 NTSTATUS Status;
1290 PROS_SECTION_OBJECT Section;
1291 PMM_SECTION_SEGMENT Segment;
1292 ULONG_PTR Entry;
1293 ULONG_PTR Entry1;
1294 ULONG Attributes;
1295 PMM_REGION Region;
1296 BOOLEAN HasSwapEntry;
1297 PVOID PAddress;
1298 PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
1299 SWAPENTRY SwapEntry;
1300
1301 /*
1302 * There is a window between taking the page fault and locking the
1303 * address space when another thread could load the page so we check
1304 * that.
1305 */
1306 if (MmIsPagePresent(Process, Address))
1307 {
1308 return(STATUS_SUCCESS);
1309 }
1310
1311 if (MmIsDisabledPage(Process, Address))
1312 {
1313 return(STATUS_ACCESS_VIOLATION);
1314 }
1315
1316 /*
1317 * Check for the virtual memory area being deleted.
1318 */
1319 if (MemoryArea->DeleteInProgress)
1320 {
1321 return(STATUS_UNSUCCESSFUL);
1322 }
1323
1324 PAddress = MM_ROUND_DOWN(Address, PAGE_SIZE);
1325 Offset.QuadPart = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress
1326 + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
1327
1328 Segment = MemoryArea->Data.SectionData.Segment;
1329 Section = MemoryArea->Data.SectionData.Section;
1330 Region = MmFindRegion(MemoryArea->StartingAddress,
1331 &MemoryArea->Data.SectionData.RegionListHead,
1332 Address, NULL);
1333 ASSERT(Region != NULL);
1334 /*
1335 * Lock the segment
1336 */
1337 MmLockSectionSegment(Segment);
1338 Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
1339 /*
1340 * Check if this page needs to be mapped COW
1341 */
1342 if ((Segment->WriteCopy) &&
1343 (Region->Protect == PAGE_READWRITE ||
1344 Region->Protect == PAGE_EXECUTE_READWRITE))
1345 {
1346 Attributes = Region->Protect == PAGE_READWRITE ? PAGE_READONLY : PAGE_EXECUTE_READ;
1347 }
1348 else
1349 {
1350 Attributes = Region->Protect;
1351 }
1352
1353 /*
1354 * Check if someone else is already handling this fault, if so wait
1355 * for them
1356 */
1357 if (Entry && IS_SWAP_FROM_SSE(Entry) && SWAPENTRY_FROM_SSE(Entry) == MM_WAIT_ENTRY)
1358 {
1359 MmUnlockSectionSegment(Segment);
1360 MmUnlockAddressSpace(AddressSpace);
1361 MiWaitForPageEvent(NULL, NULL);
1362 MmLockAddressSpace(AddressSpace);
1363 DPRINT("Address 0x%p\n", Address);
1364 return(STATUS_MM_RESTART_OPERATION);
1365 }
1366
1367 HasSwapEntry = MmIsPageSwapEntry(Process, Address);
1368
1369 if (HasSwapEntry)
1370 {
1371 SWAPENTRY DummyEntry;
1372
1373 /*
1374 * Is it a wait entry?
1375 */
1376 MmGetPageFileMapping(Process, Address, &SwapEntry);
1377
1378 if (SwapEntry == MM_WAIT_ENTRY)
1379 {
1380 MmUnlockSectionSegment(Segment);
1381 MmUnlockAddressSpace(AddressSpace);
1382 MiWaitForPageEvent(NULL, NULL);
1383 MmLockAddressSpace(AddressSpace);
1384 return STATUS_MM_RESTART_OPERATION;
1385 }
1386
1387 /*
1388 * Must be private page we have swapped out.
1389 */
1390
1391 /*
1392 * Sanity check
1393 */
1394 if (Segment->Flags & MM_PAGEFILE_SEGMENT)
1395 {
1396 DPRINT1("Found a swaped out private page in a pagefile section.\n");
1397 KeBugCheck(MEMORY_MANAGEMENT);
1398 }
1399
1400 MmUnlockSectionSegment(Segment);
1401 MmDeletePageFileMapping(Process, Address, &SwapEntry);
1402 MmCreatePageFileMapping(Process, Address, MM_WAIT_ENTRY);
1403
1404 MmUnlockAddressSpace(AddressSpace);
1405 MI_SET_USAGE(MI_USAGE_SECTION);
1406 if (Process) MI_SET_PROCESS2(Process->ImageFileName);
1407 if (!Process) MI_SET_PROCESS2("Kernel Section");
1408 Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
1409 if (!NT_SUCCESS(Status))
1410 {
1411 KeBugCheck(MEMORY_MANAGEMENT);
1412 }
1413
1414 Status = MmReadFromSwapPage(SwapEntry, Page);
1415 if (!NT_SUCCESS(Status))
1416 {
1417 DPRINT1("MmReadFromSwapPage failed, status = %x\n", Status);
1418 KeBugCheck(MEMORY_MANAGEMENT);
1419 }
1420 MmLockAddressSpace(AddressSpace);
1421 MmDeletePageFileMapping(Process, PAddress, &DummyEntry);
1422 Status = MmCreateVirtualMapping(Process,
1423 PAddress,
1424 Region->Protect,
1425 &Page,
1426 1);
1427 if (!NT_SUCCESS(Status))
1428 {
1429 DPRINT("MmCreateVirtualMapping failed, not out of memory\n");
1430 KeBugCheck(MEMORY_MANAGEMENT);
1431 return(Status);
1432 }
1433
1434 /*
1435 * Store the swap entry for later use.
1436 */
1437 MmSetSavedSwapEntryPage(Page, SwapEntry);
1438
1439 /*
1440 * Add the page to the process's working set
1441 */
1442 MmInsertRmap(Page, Process, Address);
1443 /*
1444 * Finish the operation
1445 */
1446 MiSetPageEvent(Process, Address);
1447 DPRINT("Address 0x%p\n", Address);
1448 return(STATUS_SUCCESS);
1449 }
1450
1451 /*
1452 * Satisfying a page fault on a map of /Device/PhysicalMemory is easy
1453 */
1454 if (Section->AllocationAttributes & SEC_PHYSICALMEMORY)
1455 {
1456 MmUnlockSectionSegment(Segment);
1457 /*
1458 * Just map the desired physical page
1459 */
1460 Page = (PFN_NUMBER)(Offset.QuadPart >> PAGE_SHIFT);
1461 Status = MmCreateVirtualMappingUnsafe(Process,
1462 PAddress,
1463 Region->Protect,
1464 &Page,
1465 1);
1466 if (!NT_SUCCESS(Status))
1467 {
1468 DPRINT("MmCreateVirtualMappingUnsafe failed, not out of memory\n");
1469 KeBugCheck(MEMORY_MANAGEMENT);
1470 return(Status);
1471 }
1472
1473 /*
1474 * Cleanup and release locks
1475 */
1476 MiSetPageEvent(Process, Address);
1477 DPRINT("Address 0x%p\n", Address);
1478 return(STATUS_SUCCESS);
1479 }
1480
1481 /*
1482 * Get the entry corresponding to the offset within the section
1483 */
1484 Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
1485
1486 if (Entry == 0)
1487 {
1488 SWAPENTRY FakeSwapEntry;
1489
1490 /*
1491 * If the entry is zero (and it can't change because we have
1492 * locked the segment) then we need to load the page.
1493 */
1494
1495 /*
1496 * Release all our locks and read in the page from disk
1497 */
1498 MmSetPageEntrySectionSegment(Segment, &Offset, MAKE_SWAP_SSE(MM_WAIT_ENTRY));
1499 MmUnlockSectionSegment(Segment);
1500 MmCreatePageFileMapping(Process, PAddress, MM_WAIT_ENTRY);
1501 MmUnlockAddressSpace(AddressSpace);
1502
1503 if ((Segment->Flags & MM_PAGEFILE_SEGMENT) ||
1504 ((Offset.QuadPart >= (LONGLONG)PAGE_ROUND_UP(Segment->RawLength.QuadPart) &&
1505 (Section->AllocationAttributes & SEC_IMAGE))))
1506 {
1507 MI_SET_USAGE(MI_USAGE_SECTION);
1508 if (Process) MI_SET_PROCESS2(Process->ImageFileName);
1509 if (!Process) MI_SET_PROCESS2("Kernel Section");
1510 Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
1511 if (!NT_SUCCESS(Status))
1512 {
1513 DPRINT1("MmRequestPageMemoryConsumer failed (Status %x)\n", Status);
1514 }
1515
1516 }
1517 else
1518 {
1519 Status = MiReadPage(MemoryArea, Offset.QuadPart, &Page);
1520 if (!NT_SUCCESS(Status))
1521 {
1522 DPRINT1("MiReadPage failed (Status %x)\n", Status);
1523 }
1524 }
1525 if (!NT_SUCCESS(Status))
1526 {
1527 /*
1528 * FIXME: What do we know in this case?
1529 */
1530 /*
1531 * Cleanup and release locks
1532 */
1533 MmLockAddressSpace(AddressSpace);
1534 MiSetPageEvent(Process, Address);
1535 DPRINT("Address 0x%p\n", Address);
1536 return(Status);
1537 }
1538
1539 /*
1540 * Mark the offset within the section as having valid, in-memory
1541 * data
1542 */
1543 MmLockAddressSpace(AddressSpace);
1544 MmLockSectionSegment(Segment);
1545 Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
1546 MmSetPageEntrySectionSegment(Segment, &Offset, Entry);
1547 MmUnlockSectionSegment(Segment);
1548
1549 MmDeletePageFileMapping(Process, PAddress, &FakeSwapEntry);
1550 DPRINT("CreateVirtualMapping Page %x Process %p PAddress %p Attributes %x\n",
1551 Page, Process, PAddress, Attributes);
1552 Status = MmCreateVirtualMapping(Process,
1553 PAddress,
1554 Attributes,
1555 &Page,
1556 1);
1557 if (!NT_SUCCESS(Status))
1558 {
1559 DPRINT1("Unable to create virtual mapping\n");
1560 KeBugCheck(MEMORY_MANAGEMENT);
1561 }
1562 ASSERT(MmIsPagePresent(Process, PAddress));
1563 MmInsertRmap(Page, Process, Address);
1564
1565 MiSetPageEvent(Process, Address);
1566 DPRINT("Address 0x%p\n", Address);
1567 return(STATUS_SUCCESS);
1568 }
1569 else if (IS_SWAP_FROM_SSE(Entry))
1570 {
1571 SWAPENTRY SwapEntry;
1572
1573 SwapEntry = SWAPENTRY_FROM_SSE(Entry);
1574
1575 /*
1576 * Release all our locks and read in the page from disk
1577 */
1578 MmUnlockSectionSegment(Segment);
1579
1580 MmUnlockAddressSpace(AddressSpace);
1581 MI_SET_USAGE(MI_USAGE_SECTION);
1582 if (Process) MI_SET_PROCESS2(Process->ImageFileName);
1583 if (!Process) MI_SET_PROCESS2("Kernel Section");
1584 Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &Page);
1585 if (!NT_SUCCESS(Status))
1586 {
1587 KeBugCheck(MEMORY_MANAGEMENT);
1588 }
1589
1590 Status = MmReadFromSwapPage(SwapEntry, Page);
1591 if (!NT_SUCCESS(Status))
1592 {
1593 KeBugCheck(MEMORY_MANAGEMENT);
1594 }
1595
1596 /*
1597 * Relock the address space and segment
1598 */
1599 MmLockAddressSpace(AddressSpace);
1600 MmLockSectionSegment(Segment);
1601
1602 /*
1603 * Check the entry. No one should change the status of a page
1604 * that has a pending page-in.
1605 */
1606 Entry1 = MmGetPageEntrySectionSegment(Segment, &Offset);
1607 if (Entry != Entry1)
1608 {
1609 DPRINT1("Someone changed ppte entry while we slept (%x vs %x)\n", Entry, Entry1);
1610 KeBugCheck(MEMORY_MANAGEMENT);
1611 }
1612
1613 /*
1614 * Mark the offset within the section as having valid, in-memory
1615 * data
1616 */
1617 Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
1618 MmSetPageEntrySectionSegment(Segment, &Offset, Entry);
1619 MmUnlockSectionSegment(Segment);
1620
1621 /*
1622 * Save the swap entry.
1623 */
1624 MmSetSavedSwapEntryPage(Page, SwapEntry);
1625 Status = MmCreateVirtualMapping(Process,
1626 PAddress,
1627 Region->Protect,
1628 &Page,
1629 1);
1630 if (!NT_SUCCESS(Status))
1631 {
1632 DPRINT1("Unable to create virtual mapping\n");
1633 KeBugCheck(MEMORY_MANAGEMENT);
1634 }
1635 MmInsertRmap(Page, Process, Address);
1636 MiSetPageEvent(Process, Address);
1637 DPRINT("Address 0x%p\n", Address);
1638 return(STATUS_SUCCESS);
1639 }
1640 else
1641 {
1642 /*
1643 * If the section offset is already in-memory and valid then just
1644 * take another reference to the page
1645 */
1646
1647 Page = PFN_FROM_SSE(Entry);
1648
1649 MmSharePageEntrySectionSegment(Segment, &Offset);
1650 MmUnlockSectionSegment(Segment);
1651
1652 Status = MmCreateVirtualMapping(Process,
1653 PAddress,
1654 Attributes,
1655 &Page,
1656 1);
1657 if (!NT_SUCCESS(Status))
1658 {
1659 DPRINT1("Unable to create virtual mapping\n");
1660 KeBugCheck(MEMORY_MANAGEMENT);
1661 }
1662 MmInsertRmap(Page, Process, Address);
1663 MiSetPageEvent(Process, Address);
1664 DPRINT("Address 0x%p\n", Address);
1665 return(STATUS_SUCCESS);
1666 }
1667 }
1668
1669 NTSTATUS
1670 NTAPI
1671 MmAccessFaultSectionView(PMMSUPPORT AddressSpace,
1672 MEMORY_AREA* MemoryArea,
1673 PVOID Address)
1674 {
1675 PMM_SECTION_SEGMENT Segment;
1676 PROS_SECTION_OBJECT Section;
1677 PFN_NUMBER OldPage;
1678 PFN_NUMBER NewPage;
1679 NTSTATUS Status;
1680 PVOID PAddress;
1681 LARGE_INTEGER Offset;
1682 PMM_REGION Region;
1683 ULONG_PTR Entry;
1684 PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
1685 SWAPENTRY SwapEntry;
1686
1687 DPRINT("MmAccessFaultSectionView(%p, %p, %p)\n", AddressSpace, MemoryArea, Address);
1688
1689 /*
1690 * Check if the page has already been set readwrite
1691 */
1692 if (MmGetPageProtect(Process, Address) & PAGE_READWRITE)
1693 {
1694 DPRINT("Address 0x%p\n", Address);
1695 return(STATUS_SUCCESS);
1696 }
1697
1698 /*
1699 * Find the offset of the page
1700 */
1701 PAddress = MM_ROUND_DOWN(Address, PAGE_SIZE);
1702 Offset.QuadPart = (ULONG_PTR)PAddress - (ULONG_PTR)MemoryArea->StartingAddress
1703 + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
1704
1705 Segment = MemoryArea->Data.SectionData.Segment;
1706 Section = MemoryArea->Data.SectionData.Section;
1707 Region = MmFindRegion(MemoryArea->StartingAddress,
1708 &MemoryArea->Data.SectionData.RegionListHead,
1709 Address, NULL);
1710 ASSERT(Region != NULL);
1711 /*
1712 * Lock the segment
1713 */
1714 MmLockSectionSegment(Segment);
1715
1716 OldPage = MmGetPfnForProcess(Process, Address);
1717 Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
1718
1719 MmUnlockSectionSegment(Segment);
1720
1721 /*
1722 * Check if we are doing COW
1723 */
1724 if (!((Segment->WriteCopy) &&
1725 (Region->Protect == PAGE_READWRITE ||
1726 Region->Protect == PAGE_EXECUTE_READWRITE)))
1727 {
1728 DPRINT("Address 0x%p\n", Address);
1729 return(STATUS_ACCESS_VIOLATION);
1730 }
1731
1732 if (IS_SWAP_FROM_SSE(Entry) ||
1733 PFN_FROM_SSE(Entry) != OldPage)
1734 {
1735 /* This is a private page. We must only change the page protection. */
1736 MmSetPageProtect(Process, Address, Region->Protect);
1737 return(STATUS_SUCCESS);
1738 }
1739
1740 if(OldPage == 0)
1741 DPRINT("OldPage == 0!\n");
1742
1743 /*
1744 * Get or create a pageop
1745 */
1746 MmLockSectionSegment(Segment);
1747 Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
1748
1749 /*
1750 * Wait for any other operations to complete
1751 */
1752 if (Entry == SWAPENTRY_FROM_SSE(MM_WAIT_ENTRY))
1753 {
1754 MmUnlockSectionSegment(Segment);
1755 MmUnlockAddressSpace(AddressSpace);
1756 MiWaitForPageEvent(NULL, NULL);
1757 /*
1758 * Restart the operation
1759 */
1760 MmLockAddressSpace(AddressSpace);
1761 DPRINT("Address 0x%p\n", Address);
1762 return(STATUS_MM_RESTART_OPERATION);
1763 }
1764
1765 MmDeleteRmap(OldPage, Process, PAddress);
1766 MmDeleteVirtualMapping(Process, PAddress, NULL, NULL);
1767 MmCreatePageFileMapping(Process, PAddress, MM_WAIT_ENTRY);
1768
1769 /*
1770 * Release locks now we have the pageop
1771 */
1772 MmUnlockSectionSegment(Segment);
1773 MmUnlockAddressSpace(AddressSpace);
1774
1775 /*
1776 * Allocate a page
1777 */
1778 MI_SET_USAGE(MI_USAGE_SECTION);
1779 if (Process) MI_SET_PROCESS2(Process->ImageFileName);
1780 if (!Process) MI_SET_PROCESS2("Kernel Section");
1781 Status = MmRequestPageMemoryConsumer(MC_USER, TRUE, &NewPage);
1782 if (!NT_SUCCESS(Status))
1783 {
1784 KeBugCheck(MEMORY_MANAGEMENT);
1785 }
1786
1787 /*
1788 * Copy the old page
1789 */
1790 MiCopyFromUserPage(NewPage, OldPage);
1791
1792 MmLockAddressSpace(AddressSpace);
1793
1794 /*
1795 * Set the PTE to point to the new page
1796 */
1797 MmDeletePageFileMapping(Process, PAddress, &SwapEntry);
1798 Status = MmCreateVirtualMapping(Process,
1799 PAddress,
1800 Region->Protect,
1801 &NewPage,
1802 1);
1803 if (!NT_SUCCESS(Status))
1804 {
1805 DPRINT1("MmCreateVirtualMapping failed, unable to create virtual mapping, not out of memory\n");
1806 KeBugCheck(MEMORY_MANAGEMENT);
1807 return(Status);
1808 }
1809
1810 /*
1811 * Unshare the old page.
1812 */
1813 DPRINT("Swapping page (Old %x New %x)\n", OldPage, NewPage);
1814 MmInsertRmap(NewPage, Process, PAddress);
1815 MmLockSectionSegment(Segment);
1816 MmUnsharePageEntrySectionSegment(Section, Segment, &Offset, FALSE, FALSE, NULL);
1817 MmUnlockSectionSegment(Segment);
1818
1819 MiSetPageEvent(Process, Address);
1820 DPRINT("Address 0x%p\n", Address);
1821 return(STATUS_SUCCESS);
1822 }
1823
1824 VOID
1825 MmPageOutDeleteMapping(PVOID Context, PEPROCESS Process, PVOID Address)
1826 {
1827 MM_SECTION_PAGEOUT_CONTEXT* PageOutContext;
1828 BOOLEAN WasDirty;
1829 PFN_NUMBER Page = 0;
1830
1831 PageOutContext = (MM_SECTION_PAGEOUT_CONTEXT*)Context;
1832 if (Process)
1833 {
1834 MmLockAddressSpace(&Process->Vm);
1835 }
1836
1837 MmDeleteVirtualMapping(Process,
1838 Address,
1839 &WasDirty,
1840 &Page);
1841 if (WasDirty)
1842 {
1843 PageOutContext->WasDirty = TRUE;
1844 }
1845 if (!PageOutContext->Private)
1846 {
1847 MmLockSectionSegment(PageOutContext->Segment);
1848 MmUnsharePageEntrySectionSegment((PROS_SECTION_OBJECT)PageOutContext->Section,
1849 PageOutContext->Segment,
1850 &PageOutContext->Offset,
1851 PageOutContext->WasDirty,
1852 TRUE,
1853 &PageOutContext->SectionEntry);
1854 MmUnlockSectionSegment(PageOutContext->Segment);
1855 }
1856 if (Process)
1857 {
1858 MmUnlockAddressSpace(&Process->Vm);
1859 }
1860
1861 if (PageOutContext->Private)
1862 {
1863 MmReleasePageMemoryConsumer(MC_USER, Page);
1864 }
1865 }
1866
1867 NTSTATUS
1868 NTAPI
1869 MmPageOutSectionView(PMMSUPPORT AddressSpace,
1870 MEMORY_AREA* MemoryArea,
1871 PVOID Address, ULONG_PTR Entry)
1872 {
1873 PFN_NUMBER Page;
1874 MM_SECTION_PAGEOUT_CONTEXT Context;
1875 SWAPENTRY SwapEntry;
1876 ULONGLONG FileOffset;
1877 NTSTATUS Status;
1878 PFILE_OBJECT FileObject;
1879 #ifndef NEWCC
1880 PROS_SHARED_CACHE_MAP SharedCacheMap = NULL;
1881 #endif
1882 BOOLEAN DirectMapped;
1883 BOOLEAN IsImageSection;
1884 PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
1885 KIRQL OldIrql;
1886
1887 Address = (PVOID)PAGE_ROUND_DOWN(Address);
1888
1889 /*
1890 * Get the segment and section.
1891 */
1892 Context.Segment = MemoryArea->Data.SectionData.Segment;
1893 Context.Section = MemoryArea->Data.SectionData.Section;
1894 Context.SectionEntry = Entry;
1895 Context.CallingProcess = Process;
1896
1897 Context.Offset.QuadPart = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
1898 + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
1899 FileOffset = Context.Offset.QuadPart + Context.Segment->Image.FileOffset;
1900
1901 IsImageSection = Context.Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
1902
1903 FileObject = Context.Section->FileObject;
1904 DirectMapped = FALSE;
1905
1906 MmLockSectionSegment(Context.Segment);
1907
1908 #ifndef NEWCC
1909 if (FileObject != NULL &&
1910 !(Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
1911 {
1912 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
1913
1914 /*
1915 * If the file system is letting us go directly to the cache and the
1916 * memory area was mapped at an offset in the file which is page aligned
1917 * then note this is a direct mapped page.
1918 */
1919 if ((FileOffset % PAGE_SIZE) == 0 &&
1920 (Context.Offset.QuadPart + PAGE_SIZE <= Context.Segment->RawLength.QuadPart || !IsImageSection))
1921 {
1922 DirectMapped = TRUE;
1923 }
1924 }
1925 #endif
1926
1927
1928 /*
1929 * This should never happen since mappings of physical memory are never
1930 * placed in the rmap lists.
1931 */
1932 if (Context.Section->AllocationAttributes & SEC_PHYSICALMEMORY)
1933 {
1934 DPRINT1("Trying to page out from physical memory section address 0x%p "
1935 "process %p\n", Address,
1936 Process ? Process->UniqueProcessId : 0);
1937 KeBugCheck(MEMORY_MANAGEMENT);
1938 }
1939
1940 /*
1941 * Get the section segment entry and the physical address.
1942 */
1943 if (!MmIsPagePresent(Process, Address))
1944 {
1945 DPRINT1("Trying to page out not-present page at (%p,0x%p).\n",
1946 Process ? Process->UniqueProcessId : 0, Address);
1947 KeBugCheck(MEMORY_MANAGEMENT);
1948 }
1949 Page = MmGetPfnForProcess(Process, Address);
1950 SwapEntry = MmGetSavedSwapEntryPage(Page);
1951
1952 /*
1953 * Check the reference count to ensure this page can be paged out
1954 */
1955 if (MmGetReferenceCountPage(Page) != 1)
1956 {
1957 DPRINT("Cannot page out locked section page: 0x%lu (RefCount: %lu)\n",
1958 Page, MmGetReferenceCountPage(Page));
1959 MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
1960 MmUnlockSectionSegment(Context.Segment);
1961 return STATUS_UNSUCCESSFUL;
1962 }
1963
1964 /*
1965 * Prepare the context structure for the rmap delete call.
1966 */
1967 MmUnlockSectionSegment(Context.Segment);
1968 Context.WasDirty = FALSE;
1969 if (Context.Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
1970 IS_SWAP_FROM_SSE(Entry) ||
1971 PFN_FROM_SSE(Entry) != Page)
1972 {
1973 Context.Private = TRUE;
1974 }
1975 else
1976 {
1977 Context.Private = FALSE;
1978 }
1979
1980 /*
1981 * Take an additional reference to the page or the VACB.
1982 */
1983 if (DirectMapped && !Context.Private)
1984 {
1985 if(!MiIsPageFromCache(MemoryArea, Context.Offset.QuadPart))
1986 {
1987 DPRINT1("Direct mapped non private page is not associated with the cache.\n");
1988 KeBugCheck(MEMORY_MANAGEMENT);
1989 }
1990 }
1991 else
1992 {
1993 OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
1994 MmReferencePage(Page);
1995 KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
1996 }
1997
1998 MmDeleteAllRmaps(Page, (PVOID)&Context, MmPageOutDeleteMapping);
1999
2000 /* Since we passed in a surrogate, we'll get back the page entry
2001 * state in our context. This is intended to make intermediate
2002 * decrements of share count not release the wait entry.
2003 */
2004 Entry = Context.SectionEntry;
2005
2006 /*
2007 * If this wasn't a private page then we should have reduced the entry to
2008 * zero by deleting all the rmaps.
2009 */
2010 if (!Context.Private && Entry != 0)
2011 {
2012 if (!(Context.Segment->Flags & MM_PAGEFILE_SEGMENT) &&
2013 !(Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
2014 {
2015 KeBugCheckEx(MEMORY_MANAGEMENT, Entry, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
2016 }
2017 }
2018
2019 /*
2020 * If the page wasn't dirty then we can just free it as for a readonly page.
2021 * Since we unmapped all the mappings above we know it will not suddenly
2022 * become dirty.
2023 * If the page is from a pagefile section and has no swap entry,
2024 * we can't free the page at this point.
2025 */
2026 SwapEntry = MmGetSavedSwapEntryPage(Page);
2027 if (Context.Segment->Flags & MM_PAGEFILE_SEGMENT)
2028 {
2029 if (Context.Private)
2030 {
2031 DPRINT1("Found a %s private page (address %p) in a pagefile segment.\n",
2032 Context.WasDirty ? "dirty" : "clean", Address);
2033 KeBugCheckEx(MEMORY_MANAGEMENT, SwapEntry, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
2034 }
2035 if (!Context.WasDirty && SwapEntry != 0)
2036 {
2037 MmSetSavedSwapEntryPage(Page, 0);
2038 MmLockSectionSegment(Context.Segment);
2039 MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, MAKE_SWAP_SSE(SwapEntry));
2040 MmUnlockSectionSegment(Context.Segment);
2041 MmReleasePageMemoryConsumer(MC_USER, Page);
2042 MiSetPageEvent(NULL, NULL);
2043 return(STATUS_SUCCESS);
2044 }
2045 }
2046 else if (Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED)
2047 {
2048 if (Context.Private)
2049 {
2050 DPRINT1("Found a %s private page (address %p) in a shared section segment.\n",
2051 Context.WasDirty ? "dirty" : "clean", Address);
2052 KeBugCheckEx(MEMORY_MANAGEMENT, Page, (ULONG_PTR)Process, (ULONG_PTR)Address, 0);
2053 }
2054 if (!Context.WasDirty || SwapEntry != 0)
2055 {
2056 MmSetSavedSwapEntryPage(Page, 0);
2057 if (SwapEntry != 0)
2058 {
2059 MmLockSectionSegment(Context.Segment);
2060 MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, MAKE_SWAP_SSE(SwapEntry));
2061 MmUnlockSectionSegment(Context.Segment);
2062 }
2063 MmReleasePageMemoryConsumer(MC_USER, Page);
2064 MiSetPageEvent(NULL, NULL);
2065 return(STATUS_SUCCESS);
2066 }
2067 }
2068 else if (!Context.Private && DirectMapped)
2069 {
2070 if (SwapEntry != 0)
2071 {
2072 DPRINT1("Found a swapentry for a non private and direct mapped page (address %p)\n",
2073 Address);
2074 KeBugCheckEx(MEMORY_MANAGEMENT, STATUS_UNSUCCESSFUL, SwapEntry, (ULONG_PTR)Process, (ULONG_PTR)Address);
2075 }
2076 #ifndef NEWCC
2077 Status = CcRosUnmapVacb(SharedCacheMap, FileOffset, FALSE);
2078 #else
2079 Status = STATUS_SUCCESS;
2080 #endif
2081 #ifndef NEWCC
2082 if (!NT_SUCCESS(Status))
2083 {
2084 DPRINT1("CcRosUnmapVacb failed, status = %x\n", Status);
2085 KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)SharedCacheMap, (ULONG_PTR)FileOffset, (ULONG_PTR)Address);
2086 }
2087 #endif
2088 MiSetPageEvent(NULL, NULL);
2089 return(STATUS_SUCCESS);
2090 }
2091 else if (!Context.WasDirty && !DirectMapped && !Context.Private)
2092 {
2093 if (SwapEntry != 0)
2094 {
2095 DPRINT1("Found a swap entry for a non dirty, non private and not direct mapped page (address %p)\n",
2096 Address);
2097 KeBugCheckEx(MEMORY_MANAGEMENT, SwapEntry, Page, (ULONG_PTR)Process, (ULONG_PTR)Address);
2098 }
2099 MmReleasePageMemoryConsumer(MC_USER, Page);
2100 MiSetPageEvent(NULL, NULL);
2101 return(STATUS_SUCCESS);
2102 }
2103 else if (!Context.WasDirty && Context.Private && SwapEntry != 0)
2104 {
2105 DPRINT("Not dirty and private and not swapped (%p:%p)\n", Process, Address);
2106 MmSetSavedSwapEntryPage(Page, 0);
2107 MmLockAddressSpace(AddressSpace);
2108 Status = MmCreatePageFileMapping(Process,
2109 Address,
2110 SwapEntry);
2111 MmUnlockAddressSpace(AddressSpace);
2112 if (!NT_SUCCESS(Status))
2113 {
2114 DPRINT1("Status %x Swapping out %p:%p\n", Status, Process, Address);
2115 KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)Process, (ULONG_PTR)Address, SwapEntry);
2116 }
2117 MmReleasePageMemoryConsumer(MC_USER, Page);
2118 MiSetPageEvent(NULL, NULL);
2119 return(STATUS_SUCCESS);
2120 }
2121
2122 /*
2123 * If necessary, allocate an entry in the paging file for this page
2124 */
2125 if (SwapEntry == 0)
2126 {
2127 SwapEntry = MmAllocSwapPage();
2128 if (SwapEntry == 0)
2129 {
2130 MmShowOutOfSpaceMessagePagingFile();
2131 MmLockAddressSpace(AddressSpace);
2132 /*
2133 * For private pages restore the old mappings.
2134 */
2135 if (Context.Private)
2136 {
2137 Status = MmCreateVirtualMapping(Process,
2138 Address,
2139 MemoryArea->Protect,
2140 &Page,
2141 1);
2142 MmSetDirtyPage(Process, Address);
2143 MmInsertRmap(Page,
2144 Process,
2145 Address);
2146 }
2147 else
2148 {
2149 ULONG_PTR OldEntry;
2150 /*
2151 * For non-private pages if the page wasn't direct mapped then
2152 * set it back into the section segment entry so we don't loose
2153 * our copy. Otherwise it will be handled by the cache manager.
2154 */
2155 Status = MmCreateVirtualMapping(Process,
2156 Address,
2157 MemoryArea->Protect,
2158 &Page,
2159 1);
2160 MmSetDirtyPage(Process, Address);
2161 MmInsertRmap(Page,
2162 Process,
2163 Address);
2164 // If we got here, the previous entry should have been a wait
2165 Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
2166 MmLockSectionSegment(Context.Segment);
2167 OldEntry = MmGetPageEntrySectionSegment(Context.Segment, &Context.Offset);
2168 ASSERT(OldEntry == 0 || OldEntry == MAKE_SWAP_SSE(MM_WAIT_ENTRY));
2169 MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
2170 MmUnlockSectionSegment(Context.Segment);
2171 }
2172 MmUnlockAddressSpace(AddressSpace);
2173 MiSetPageEvent(NULL, NULL);
2174 return(STATUS_PAGEFILE_QUOTA);
2175 }
2176 }
2177
2178 /*
2179 * Write the page to the pagefile
2180 */
2181 Status = MmWriteToSwapPage(SwapEntry, Page);
2182 if (!NT_SUCCESS(Status))
2183 {
2184 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
2185 Status);
2186 /*
2187 * As above: undo our actions.
2188 * FIXME: Also free the swap page.
2189 */
2190 MmLockAddressSpace(AddressSpace);
2191 if (Context.Private)
2192 {
2193 Status = MmCreateVirtualMapping(Process,
2194 Address,
2195 MemoryArea->Protect,
2196 &Page,
2197 1);
2198 MmSetDirtyPage(Process, Address);
2199 MmInsertRmap(Page,
2200 Process,
2201 Address);
2202 }
2203 else
2204 {
2205 Status = MmCreateVirtualMapping(Process,
2206 Address,
2207 MemoryArea->Protect,
2208 &Page,
2209 1);
2210 MmSetDirtyPage(Process, Address);
2211 MmInsertRmap(Page,
2212 Process,
2213 Address);
2214 Entry = MAKE_SSE(Page << PAGE_SHIFT, 1);
2215 MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
2216 }
2217 MmUnlockAddressSpace(AddressSpace);
2218 MiSetPageEvent(NULL, NULL);
2219 return(STATUS_UNSUCCESSFUL);
2220 }
2221
2222 /*
2223 * Otherwise we have succeeded.
2224 */
2225 DPRINT("MM: Wrote section page 0x%.8X to swap!\n", Page << PAGE_SHIFT);
2226 MmSetSavedSwapEntryPage(Page, 0);
2227 if (Context.Segment->Flags & MM_PAGEFILE_SEGMENT ||
2228 Context.Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED)
2229 {
2230 MmLockSectionSegment(Context.Segment);
2231 MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, MAKE_SWAP_SSE(SwapEntry));
2232 MmUnlockSectionSegment(Context.Segment);
2233 }
2234 else
2235 {
2236 MmReleasePageMemoryConsumer(MC_USER, Page);
2237 }
2238
2239 if (Context.Private)
2240 {
2241 MmLockAddressSpace(AddressSpace);
2242 MmLockSectionSegment(Context.Segment);
2243 Status = MmCreatePageFileMapping(Process,
2244 Address,
2245 SwapEntry);
2246 /* We had placed a wait entry upon entry ... replace it before leaving */
2247 MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
2248 MmUnlockSectionSegment(Context.Segment);
2249 MmUnlockAddressSpace(AddressSpace);
2250 if (!NT_SUCCESS(Status))
2251 {
2252 DPRINT1("Status %x Creating page file mapping for %p:%p\n", Status, Process, Address);
2253 KeBugCheckEx(MEMORY_MANAGEMENT, Status, (ULONG_PTR)Process, (ULONG_PTR)Address, SwapEntry);
2254 }
2255 }
2256 else
2257 {
2258 MmLockAddressSpace(AddressSpace);
2259 MmLockSectionSegment(Context.Segment);
2260 Entry = MAKE_SWAP_SSE(SwapEntry);
2261 /* We had placed a wait entry upon entry ... replace it before leaving */
2262 MmSetPageEntrySectionSegment(Context.Segment, &Context.Offset, Entry);
2263 MmUnlockSectionSegment(Context.Segment);
2264 MmUnlockAddressSpace(AddressSpace);
2265 }
2266
2267 MiSetPageEvent(NULL, NULL);
2268 return(STATUS_SUCCESS);
2269 }
2270
2271 NTSTATUS
2272 NTAPI
2273 MmWritePageSectionView(PMMSUPPORT AddressSpace,
2274 PMEMORY_AREA MemoryArea,
2275 PVOID Address,
2276 ULONG PageEntry)
2277 {
2278 LARGE_INTEGER Offset;
2279 PROS_SECTION_OBJECT Section;
2280 PMM_SECTION_SEGMENT Segment;
2281 PFN_NUMBER Page;
2282 SWAPENTRY SwapEntry;
2283 ULONG_PTR Entry;
2284 BOOLEAN Private;
2285 NTSTATUS Status;
2286 PFILE_OBJECT FileObject;
2287 PROS_SHARED_CACHE_MAP SharedCacheMap = NULL;
2288 BOOLEAN DirectMapped;
2289 BOOLEAN IsImageSection;
2290 PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
2291
2292 Address = (PVOID)PAGE_ROUND_DOWN(Address);
2293
2294 Offset.QuadPart = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
2295 + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
2296
2297 /*
2298 * Get the segment and section.
2299 */
2300 Segment = MemoryArea->Data.SectionData.Segment;
2301 Section = MemoryArea->Data.SectionData.Section;
2302 IsImageSection = Section->AllocationAttributes & SEC_IMAGE ? TRUE : FALSE;
2303
2304 FileObject = Section->FileObject;
2305 DirectMapped = FALSE;
2306 if (FileObject != NULL &&
2307 !(Segment->Image.Characteristics & IMAGE_SCN_MEM_SHARED))
2308 {
2309 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
2310
2311 /*
2312 * If the file system is letting us go directly to the cache and the
2313 * memory area was mapped at an offset in the file which is page aligned
2314 * then note this is a direct mapped page.
2315 */
2316 if (((Offset.QuadPart + Segment->Image.FileOffset) % PAGE_SIZE) == 0 &&
2317 (Offset.QuadPart + PAGE_SIZE <= Segment->RawLength.QuadPart || !IsImageSection))
2318 {
2319 DirectMapped = TRUE;
2320 }
2321 }
2322
2323 /*
2324 * This should never happen since mappings of physical memory are never
2325 * placed in the rmap lists.
2326 */
2327 if (Section->AllocationAttributes & SEC_PHYSICALMEMORY)
2328 {
2329 DPRINT1("Trying to write back page from physical memory mapped at %p "
2330 "process %p\n", Address,
2331 Process ? Process->UniqueProcessId : 0);
2332 KeBugCheck(MEMORY_MANAGEMENT);
2333 }
2334
2335 /*
2336 * Get the section segment entry and the physical address.
2337 */
2338 Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
2339 if (!MmIsPagePresent(Process, Address))
2340 {
2341 DPRINT1("Trying to page out not-present page at (%p,0x%p).\n",
2342 Process ? Process->UniqueProcessId : 0, Address);
2343 KeBugCheck(MEMORY_MANAGEMENT);
2344 }
2345 Page = MmGetPfnForProcess(Process, Address);
2346 SwapEntry = MmGetSavedSwapEntryPage(Page);
2347
2348 /*
2349 * Check for a private (COWed) page.
2350 */
2351 if (Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
2352 IS_SWAP_FROM_SSE(Entry) ||
2353 PFN_FROM_SSE(Entry) != Page)
2354 {
2355 Private = TRUE;
2356 }
2357 else
2358 {
2359 Private = FALSE;
2360 }
2361
2362 /*
2363 * Speculatively set all mappings of the page to clean.
2364 */
2365 MmSetCleanAllRmaps(Page);
2366
2367 /*
2368 * If this page was direct mapped from the cache then the cache manager
2369 * will take care of writing it back to disk.
2370 */
2371 if (DirectMapped && !Private)
2372 {
2373 //LARGE_INTEGER SOffset;
2374 ASSERT(SwapEntry == 0);
2375 //SOffset.QuadPart = Offset.QuadPart + Segment->Image.FileOffset;
2376 #ifndef NEWCC
2377 CcRosMarkDirtyVacb(SharedCacheMap, Offset.QuadPart);
2378 #endif
2379 MmLockSectionSegment(Segment);
2380 MmSetPageEntrySectionSegment(Segment, &Offset, PageEntry);
2381 MmUnlockSectionSegment(Segment);
2382 MiSetPageEvent(NULL, NULL);
2383 return(STATUS_SUCCESS);
2384 }
2385
2386 /*
2387 * If necessary, allocate an entry in the paging file for this page
2388 */
2389 if (SwapEntry == 0)
2390 {
2391 SwapEntry = MmAllocSwapPage();
2392 if (SwapEntry == 0)
2393 {
2394 MmSetDirtyAllRmaps(Page);
2395 MiSetPageEvent(NULL, NULL);
2396 return(STATUS_PAGEFILE_QUOTA);
2397 }
2398 MmSetSavedSwapEntryPage(Page, SwapEntry);
2399 }
2400
2401 /*
2402 * Write the page to the pagefile
2403 */
2404 Status = MmWriteToSwapPage(SwapEntry, Page);
2405 if (!NT_SUCCESS(Status))
2406 {
2407 DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
2408 Status);
2409 MmSetDirtyAllRmaps(Page);
2410 MiSetPageEvent(NULL, NULL);
2411 return(STATUS_UNSUCCESSFUL);
2412 }
2413
2414 /*
2415 * Otherwise we have succeeded.
2416 */
2417 DPRINT("MM: Wrote section page 0x%.8X to swap!\n", Page << PAGE_SHIFT);
2418 MiSetPageEvent(NULL, NULL);
2419 return(STATUS_SUCCESS);
2420 }
2421
2422 static VOID
2423 MmAlterViewAttributes(PMMSUPPORT AddressSpace,
2424 PVOID BaseAddress,
2425 SIZE_T RegionSize,
2426 ULONG OldType,
2427 ULONG OldProtect,
2428 ULONG NewType,
2429 ULONG NewProtect)
2430 {
2431 PMEMORY_AREA MemoryArea;
2432 PMM_SECTION_SEGMENT Segment;
2433 BOOLEAN DoCOW = FALSE;
2434 ULONG i;
2435 PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
2436
2437 MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace, BaseAddress);
2438 ASSERT(MemoryArea != NULL);
2439 Segment = MemoryArea->Data.SectionData.Segment;
2440 MmLockSectionSegment(Segment);
2441
2442 if ((Segment->WriteCopy) &&
2443 (NewProtect == PAGE_READWRITE || NewProtect == PAGE_EXECUTE_READWRITE))
2444 {
2445 DoCOW = TRUE;
2446 }
2447
2448 if (OldProtect != NewProtect)
2449 {
2450 for (i = 0; i < PAGE_ROUND_UP(RegionSize) / PAGE_SIZE; i++)
2451 {
2452 SWAPENTRY SwapEntry;
2453 PVOID Address = (char*)BaseAddress + (i * PAGE_SIZE);
2454 ULONG Protect = NewProtect;
2455
2456 /* Wait for a wait entry to disappear */
2457 do
2458 {
2459 MmGetPageFileMapping(Process, Address, &SwapEntry);
2460 if (SwapEntry != MM_WAIT_ENTRY)
2461 break;
2462 MiWaitForPageEvent(Process, Address);
2463 }
2464 while (TRUE);
2465
2466 /*
2467 * If we doing COW for this segment then check if the page is
2468 * already private.
2469 */
2470 if (DoCOW && MmIsPagePresent(Process, Address))
2471 {
2472 LARGE_INTEGER Offset;
2473 ULONG_PTR Entry;
2474 PFN_NUMBER Page;
2475
2476 Offset.QuadPart = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
2477 + MemoryArea->Data.SectionData.ViewOffset.QuadPart;
2478 Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
2479 /*
2480 * An MM_WAIT_ENTRY is ok in this case... It'll just count as
2481 * IS_SWAP_FROM_SSE and we'll do the right thing.
2482 */
2483 Page = MmGetPfnForProcess(Process, Address);
2484
2485 Protect = PAGE_READONLY;
2486 if (Segment->Image.Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA ||
2487 IS_SWAP_FROM_SSE(Entry) ||
2488 PFN_FROM_SSE(Entry) != Page)
2489 {
2490 Protect = NewProtect;
2491 }
2492 }
2493
2494 if (MmIsPagePresent(Process, Address) || MmIsDisabledPage(Process, Address))
2495 {
2496 MmSetPageProtect(Process, Address,
2497 Protect);
2498 }
2499 }
2500 }
2501
2502 MmUnlockSectionSegment(Segment);
2503 }
2504
2505 NTSTATUS
2506 NTAPI
2507 MmProtectSectionView(PMMSUPPORT AddressSpace,
2508 PMEMORY_AREA MemoryArea,
2509 PVOID BaseAddress,
2510 SIZE_T Length,
2511 ULONG Protect,
2512 PULONG OldProtect)
2513 {
2514 PMM_REGION Region;
2515 NTSTATUS Status;
2516 ULONG_PTR MaxLength;
2517
2518 MaxLength = (ULONG_PTR)MemoryArea->EndingAddress - (ULONG_PTR)BaseAddress;
2519 if (Length > MaxLength)
2520 Length = (ULONG)MaxLength;
2521
2522 Region = MmFindRegion(MemoryArea->StartingAddress,
2523 &MemoryArea->Data.SectionData.RegionListHead,
2524 BaseAddress, NULL);
2525 ASSERT(Region != NULL);
2526
2527 if ((MemoryArea->Flags & SEC_NO_CHANGE) &&
2528 Region->Protect != Protect)
2529 {
2530 return STATUS_INVALID_PAGE_PROTECTION;
2531 }
2532
2533 *OldProtect = Region->Protect;
2534 Status = MmAlterRegion(AddressSpace, MemoryArea->StartingAddress,
2535 &MemoryArea->Data.SectionData.RegionListHead,
2536 BaseAddress, Length, Region->Type, Protect,
2537 MmAlterViewAttributes);
2538
2539 return(Status);
2540 }
2541
2542 NTSTATUS NTAPI
2543 MmQuerySectionView(PMEMORY_AREA MemoryArea,
2544 PVOID Address,
2545 PMEMORY_BASIC_INFORMATION Info,
2546 PSIZE_T ResultLength)
2547 {
2548 PMM_REGION Region;
2549 PVOID RegionBaseAddress;
2550 PROS_SECTION_OBJECT Section;
2551 PMM_SECTION_SEGMENT Segment;
2552
2553 Region = MmFindRegion((PVOID)MemoryArea->StartingAddress,
2554 &MemoryArea->Data.SectionData.RegionListHead,
2555 Address, &RegionBaseAddress);
2556 if (Region == NULL)
2557 {
2558 return STATUS_UNSUCCESSFUL;
2559 }
2560
2561 Section = MemoryArea->Data.SectionData.Section;
2562 if (Section->AllocationAttributes & SEC_IMAGE)
2563 {
2564 Segment = MemoryArea->Data.SectionData.Segment;
2565 Info->AllocationBase = (PUCHAR)MemoryArea->StartingAddress - Segment->Image.VirtualAddress;
2566 Info->Type = MEM_IMAGE;
2567 }
2568 else
2569 {
2570 Info->AllocationBase = MemoryArea->StartingAddress;
2571 Info->Type = MEM_MAPPED;
2572 }
2573 Info->BaseAddress = RegionBaseAddress;
2574 Info->AllocationProtect = MemoryArea->Protect;
2575 Info->RegionSize = Region->Length;
2576 Info->State = MEM_COMMIT;
2577 Info->Protect = Region->Protect;
2578
2579 *ResultLength = sizeof(MEMORY_BASIC_INFORMATION);
2580 return(STATUS_SUCCESS);
2581 }
2582
2583 VOID
2584 NTAPI
2585 MmpFreePageFileSegment(PMM_SECTION_SEGMENT Segment)
2586 {
2587 ULONG Length;
2588 LARGE_INTEGER Offset;
2589 ULONG_PTR Entry;
2590 SWAPENTRY SavedSwapEntry;
2591 PFN_NUMBER Page;
2592
2593 Page = 0;
2594
2595 MmLockSectionSegment(Segment);
2596
2597 Length = PAGE_ROUND_UP(Segment->Length.QuadPart);
2598 for (Offset.QuadPart = 0; Offset.QuadPart < Length; Offset.QuadPart += PAGE_SIZE)
2599 {
2600 Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
2601 if (Entry)
2602 {
2603 MmSetPageEntrySectionSegment(Segment, &Offset, 0);
2604 if (IS_SWAP_FROM_SSE(Entry))
2605 {
2606 MmFreeSwapPage(SWAPENTRY_FROM_SSE(Entry));
2607 }
2608 else
2609 {
2610 Page = PFN_FROM_SSE(Entry);
2611 SavedSwapEntry = MmGetSavedSwapEntryPage(Page);
2612 if (SavedSwapEntry != 0)
2613 {
2614 MmSetSavedSwapEntryPage(Page, 0);
2615 MmFreeSwapPage(SavedSwapEntry);
2616 }
2617 MmReleasePageMemoryConsumer(MC_USER, Page);
2618 }
2619 }
2620 }
2621
2622 MmUnlockSectionSegment(Segment);
2623 }
2624
2625 VOID NTAPI
2626 MmpDeleteSection(PVOID ObjectBody)
2627 {
2628 PROS_SECTION_OBJECT Section = (PROS_SECTION_OBJECT)ObjectBody;
2629
2630 /* Check if it's an ARM3, or ReactOS section */
2631 if (!MiIsRosSectionObject(Section))
2632 {
2633 MiDeleteARM3Section(ObjectBody);
2634 return;
2635 }
2636
2637 DPRINT("MmpDeleteSection(ObjectBody %p)\n", ObjectBody);
2638 if (Section->AllocationAttributes & SEC_IMAGE)
2639 {
2640 ULONG i;
2641 ULONG NrSegments;
2642 ULONG RefCount;
2643 PMM_SECTION_SEGMENT SectionSegments;
2644
2645 /*
2646 * NOTE: Section->ImageSection can be NULL for short time
2647 * during the section creating. If we fail for some reason
2648 * until the image section is properly initialized we shouldn't
2649 * process further here.
2650 */
2651 if (Section->ImageSection == NULL)
2652 return;
2653
2654 SectionSegments = Section->ImageSection->Segments;
2655 NrSegments = Section->ImageSection->NrSegments;
2656
2657 for (i = 0; i < NrSegments; i++)
2658 {
2659 if (SectionSegments[i].Image.Characteristics & IMAGE_SCN_MEM_SHARED)
2660 {
2661 MmLockSectionSegment(&SectionSegments[i]);
2662 }
2663 RefCount = InterlockedDecrementUL(&SectionSegments[i].ReferenceCount);
2664 if (SectionSegments[i].Image.Characteristics & IMAGE_SCN_MEM_SHARED)
2665 {
2666 MmUnlockSectionSegment(&SectionSegments[i]);
2667 if (RefCount == 0)
2668 {
2669 MmpFreePageFileSegment(&SectionSegments[i]);
2670 }
2671 }
2672 }
2673 }
2674 #ifdef NEWCC
2675 else if (Section->Segment && Section->Segment->Flags & MM_DATAFILE_SEGMENT)
2676 {
2677 ULONG RefCount = 0;
2678 PMM_SECTION_SEGMENT Segment = Section->Segment;
2679
2680 if (Segment &&
2681 (RefCount = InterlockedDecrementUL(&Segment->ReferenceCount)) == 0)
2682 {
2683 DPRINT("Freeing section segment\n");
2684 Section->Segment = NULL;
2685 MmFinalizeSegment(Segment);
2686 }
2687 else
2688 {
2689 DPRINT("RefCount %d\n", RefCount);
2690 }
2691 }
2692 #endif
2693 else
2694 {
2695 /*
2696 * NOTE: Section->Segment can be NULL for short time
2697 * during the section creating.
2698 */
2699 if (Section->Segment == NULL)
2700 return;
2701
2702 if (Section->Segment->Flags & MM_PAGEFILE_SEGMENT)
2703 {
2704 MmpFreePageFileSegment(Section->Segment);
2705 MmFreePageTablesSectionSegment(Section->Segment, NULL);
2706 ExFreePool(Section->Segment);
2707 Section->Segment = NULL;
2708 }
2709 else
2710 {
2711 (void)InterlockedDecrementUL(&Section->Segment->ReferenceCount);
2712 }
2713 }
2714 if (Section->FileObject != NULL)
2715 {
2716 #ifndef NEWCC
2717 CcRosDereferenceCache(Section->FileObject);
2718 #endif
2719 ObDereferenceObject(Section->FileObject);
2720 Section->FileObject = NULL;
2721 }
2722 }
2723
2724 VOID NTAPI
2725 MmpCloseSection(IN PEPROCESS Process OPTIONAL,
2726 IN PVOID Object,
2727 IN ACCESS_MASK GrantedAccess,
2728 IN ULONG ProcessHandleCount,
2729 IN ULONG SystemHandleCount)
2730 {
2731 DPRINT("MmpCloseSection(OB %p, HC %lu)\n", Object, ProcessHandleCount);
2732 }
2733
2734 NTSTATUS
2735 INIT_FUNCTION
2736 NTAPI
2737 MmCreatePhysicalMemorySection(VOID)
2738 {
2739 PROS_SECTION_OBJECT PhysSection;
2740 NTSTATUS Status;
2741 OBJECT_ATTRIBUTES Obj;
2742 UNICODE_STRING Name = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
2743 LARGE_INTEGER SectionSize;
2744 HANDLE Handle;
2745
2746 /*
2747 * Create the section mapping physical memory
2748 */
2749 SectionSize.QuadPart = 0xFFFFFFFF;
2750 InitializeObjectAttributes(&Obj,
2751 &Name,
2752 OBJ_PERMANENT,
2753 NULL,
2754 NULL);
2755 Status = MmCreateSection((PVOID)&PhysSection,
2756 SECTION_ALL_ACCESS,
2757 &Obj,
2758 &SectionSize,
2759 PAGE_EXECUTE_READWRITE,
2760 SEC_PHYSICALMEMORY,
2761 NULL,
2762 NULL);
2763 if (!NT_SUCCESS(Status))
2764 {
2765 DPRINT1("Failed to create PhysicalMemory section\n");
2766 KeBugCheck(MEMORY_MANAGEMENT);
2767 }
2768 Status = ObInsertObject(PhysSection,
2769 NULL,
2770 SECTION_ALL_ACCESS,
2771 0,
2772 NULL,
2773 &Handle);
2774 if (!NT_SUCCESS(Status))
2775 {
2776 ObDereferenceObject(PhysSection);
2777 }
2778 ObCloseHandle(Handle, KernelMode);
2779 PhysSection->AllocationAttributes |= SEC_PHYSICALMEMORY;
2780 PhysSection->Segment->Flags &= ~MM_PAGEFILE_SEGMENT;
2781
2782 return(STATUS_SUCCESS);
2783 }
2784
2785 NTSTATUS
2786 INIT_FUNCTION
2787 NTAPI
2788 MmInitSectionImplementation(VOID)
2789 {
2790 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
2791 UNICODE_STRING Name;
2792
2793 DPRINT("Creating Section Object Type\n");
2794
2795 /* Initialize the section based root */
2796 ASSERT(MmSectionBasedRoot.NumberGenericTableElements == 0);
2797 MmSectionBasedRoot.BalancedRoot.u1.Parent = &MmSectionBasedRoot.BalancedRoot;
2798
2799 /* Initialize the Section object type */
2800 RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
2801 RtlInitUnicodeString(&Name, L"Section");
2802 ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
2803 ObjectTypeInitializer.DefaultPagedPoolCharge = sizeof(ROS_SECTION_OBJECT);
2804 ObjectTypeInitializer.PoolType = PagedPool;
2805 ObjectTypeInitializer.UseDefaultObject = TRUE;
2806 ObjectTypeInitializer.GenericMapping = MmpSectionMapping;
2807 ObjectTypeInitializer.DeleteProcedure = MmpDeleteSection;
2808 ObjectTypeInitializer.CloseProcedure = MmpCloseSection;
2809 ObjectTypeInitializer.ValidAccessMask = SECTION_ALL_ACCESS;
2810 ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK;
2811 ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &MmSectionObjectType);
2812
2813 MmCreatePhysicalMemorySection();
2814
2815 return(STATUS_SUCCESS);
2816 }
2817
2818 NTSTATUS
2819 NTAPI
2820 MmCreatePageFileSection(PROS_SECTION_OBJECT *SectionObject,
2821 ACCESS_MASK DesiredAccess,
2822 POBJECT_ATTRIBUTES ObjectAttributes,
2823 PLARGE_INTEGER UMaximumSize,
2824 ULONG SectionPageProtection,
2825 ULONG AllocationAttributes)
2826 /*
2827 * Create a section which is backed by the pagefile
2828 */
2829 {
2830 LARGE_INTEGER MaximumSize;
2831 PROS_SECTION_OBJECT Section;
2832 PMM_SECTION_SEGMENT Segment;
2833 NTSTATUS Status;
2834
2835 if (UMaximumSize == NULL)
2836 {
2837 DPRINT1("MmCreatePageFileSection: (UMaximumSize == NULL)\n");
2838 return(STATUS_INVALID_PARAMETER);
2839 }
2840 MaximumSize = *UMaximumSize;
2841
2842 /*
2843 * Create the section
2844 */
2845 Status = ObCreateObject(ExGetPreviousMode(),
2846 MmSectionObjectType,
2847 ObjectAttributes,
2848 ExGetPreviousMode(),
2849 NULL,
2850 sizeof(ROS_SECTION_OBJECT),
2851 0,
2852 0,
2853 (PVOID*)(PVOID)&Section);
2854 if (!NT_SUCCESS(Status))
2855 {
2856 DPRINT1("MmCreatePageFileSection: failed to create object (0x%lx)\n", Status);
2857 return(Status);
2858 }
2859
2860 /*
2861 * Initialize it
2862 */
2863 RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
2864 Section->Type = 'SC';
2865 Section->Size = 'TN';
2866 Section->SectionPageProtection = SectionPageProtection;
2867 Section->AllocationAttributes = AllocationAttributes;
2868 Section->MaximumSize = MaximumSize;
2869 Segment = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_SECTION_SEGMENT),
2870 TAG_MM_SECTION_SEGMENT);
2871 if (Segment == NULL)
2872 {
2873 ObDereferenceObject(Section);
2874 return(STATUS_NO_MEMORY);
2875 }
2876 RtlZeroMemory(Segment, sizeof(MM_SECTION_SEGMENT));
2877 Section->Segment = Segment;
2878 Segment->ReferenceCount = 1;
2879 ExInitializeFastMutex(&Segment->Lock);
2880 Segment->Image.FileOffset = 0;
2881 Segment->Protection = SectionPageProtection;
2882 Segment->RawLength.QuadPart = MaximumSize.u.LowPart;
2883 Segment->Length.QuadPart = PAGE_ROUND_UP(MaximumSize.u.LowPart);
2884 Segment->Flags = MM_PAGEFILE_SEGMENT;
2885 Segment->WriteCopy = FALSE;
2886 Segment->Image.VirtualAddress = 0;
2887 Segment->Image.Characteristics = 0;
2888 *SectionObject = Section;
2889 MiInitializeSectionPageTable(Segment);
2890 return(STATUS_SUCCESS);
2891 }
2892
2893 NTSTATUS
2894 NTAPI
2895 MmCreateDataFileSection(PROS_SECTION_OBJECT *SectionObject,
2896 ACCESS_MASK DesiredAccess,
2897 POBJECT_ATTRIBUTES ObjectAttributes,
2898 PLARGE_INTEGER UMaximumSize,
2899 ULONG SectionPageProtection,
2900 ULONG AllocationAttributes,
2901 HANDLE FileHandle)
2902 /*
2903 * Create a section backed by a data file
2904 */
2905 {
2906 PROS_SECTION_OBJECT Section;
2907 NTSTATUS Status;
2908 LARGE_INTEGER MaximumSize;
2909 PFILE_OBJECT FileObject;
2910 PMM_SECTION_SEGMENT Segment;
2911 ULONG FileAccess;
2912 IO_STATUS_BLOCK Iosb;
2913 LARGE_INTEGER Offset;
2914 CHAR Buffer;
2915 FILE_STANDARD_INFORMATION FileInfo;
2916 ULONG Length;
2917
2918 /*
2919 * Create the section
2920 */
2921 Status = ObCreateObject(ExGetPreviousMode(),
2922 MmSectionObjectType,
2923 ObjectAttributes,
2924 ExGetPreviousMode(),
2925 NULL,
2926 sizeof(ROS_SECTION_OBJECT),
2927 0,
2928 0,
2929 (PVOID*)&Section);
2930 if (!NT_SUCCESS(Status))
2931 {
2932 return(Status);
2933 }
2934 /*
2935 * Initialize it
2936 */
2937 RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
2938 Section->Type = 'SC';
2939 Section->Size = 'TN';
2940 Section->SectionPageProtection = SectionPageProtection;
2941 Section->AllocationAttributes = AllocationAttributes;
2942
2943 /*
2944 * Reference the file handle
2945 */
2946 FileAccess = MiArm3GetCorrectFileAccessMask(SectionPageProtection);
2947 Status = ObReferenceObjectByHandle(FileHandle,
2948 FileAccess,
2949 IoFileObjectType,
2950 ExGetPreviousMode(),
2951 (PVOID*)(PVOID)&FileObject,
2952 NULL);
2953 if (!NT_SUCCESS(Status))
2954 {
2955 ObDereferenceObject(Section);
2956 return(Status);
2957 }
2958
2959 /*
2960 * FIXME: This is propably not entirely correct. We can't look into
2961 * the standard FCB header because it might not be initialized yet
2962 * (as in case of the EXT2FS driver by Manoj Paul Joseph where the
2963 * standard file information is filled on first request).
2964 */
2965 Status = IoQueryFileInformation(FileObject,
2966 FileStandardInformation,
2967 sizeof(FILE_STANDARD_INFORMATION),
2968 &FileInfo,
2969 &Length);
2970 Iosb.Information = Length;
2971 if (!NT_SUCCESS(Status))
2972 {
2973 ObDereferenceObject(Section);
2974 ObDereferenceObject(FileObject);
2975 return Status;
2976 }
2977
2978 /*
2979 * FIXME: Revise this once a locking order for file size changes is
2980 * decided
2981 */
2982 if ((UMaximumSize != NULL) && (UMaximumSize->QuadPart != 0))
2983 {
2984 MaximumSize = *UMaximumSize;
2985 }
2986 else
2987 {
2988 MaximumSize = FileInfo.EndOfFile;
2989 /* Mapping zero-sized files isn't allowed. */
2990 if (MaximumSize.QuadPart == 0)
2991 {
2992 ObDereferenceObject(Section);
2993 ObDereferenceObject(FileObject);
2994 return STATUS_FILE_INVALID;
2995 }
2996 }
2997
2998 if (MaximumSize.QuadPart > FileInfo.EndOfFile.QuadPart)
2999 {
3000 Status = IoSetInformation(FileObject,
3001 FileAllocationInformation,
3002 sizeof(LARGE_INTEGER),
3003 &MaximumSize);
3004 if (!NT_SUCCESS(Status))
3005 {
3006 ObDereferenceObject(Section);
3007 ObDereferenceObject(FileObject);
3008 return(STATUS_SECTION_NOT_EXTENDED);
3009 }
3010 }
3011
3012 if (FileObject->SectionObjectPointer == NULL ||
3013 FileObject->SectionObjectPointer->SharedCacheMap == NULL)
3014 {
3015 /*
3016 * Read a bit so caching is initiated for the file object.
3017 * This is only needed because MiReadPage currently cannot
3018 * handle non-cached streams.
3019 */
3020 Offset.QuadPart = 0;
3021 Status = ZwReadFile(FileHandle,
3022 NULL,
3023 NULL,
3024 NULL,
3025 &Iosb,
3026 &Buffer,
3027 sizeof (Buffer),
3028 &Offset,
3029 0);
3030 if (!NT_SUCCESS(Status) && (Status != STATUS_END_OF_FILE))
3031 {
3032 ObDereferenceObject(Section);
3033 ObDereferenceObject(FileObject);
3034 return(Status);
3035 }
3036 if (FileObject->SectionObjectPointer == NULL ||
3037 FileObject->SectionObjectPointer->SharedCacheMap == NULL)
3038 {
3039 /* FIXME: handle this situation */
3040 ObDereferenceObject(Section);
3041 ObDereferenceObject(FileObject);
3042 return STATUS_INVALID_PARAMETER;
3043 }
3044 }
3045
3046 /*
3047 * Lock the file
3048 */
3049 Status = MmspWaitForFileLock(FileObject);
3050 if (Status != STATUS_SUCCESS)
3051 {
3052 ObDereferenceObject(Section);
3053 ObDereferenceObject(FileObject);
3054 return(Status);
3055 }
3056
3057 /*
3058 * If this file hasn't been mapped as a data file before then allocate a
3059 * section segment to describe the data file mapping
3060 */
3061 if (FileObject->SectionObjectPointer->DataSectionObject == NULL)
3062 {
3063 Segment = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_SECTION_SEGMENT),
3064 TAG_MM_SECTION_SEGMENT);
3065 if (Segment == NULL)
3066 {
3067 //KeSetEvent((PVOID)&FileObject->Lock, IO_NO_INCREMENT, FALSE);
3068 ObDereferenceObject(Section);
3069 ObDereferenceObject(FileObject);
3070 return(STATUS_NO_MEMORY);
3071 }
3072 Section->Segment = Segment;
3073 Segment->ReferenceCount = 1;
3074 ExInitializeFastMutex(&Segment->Lock);
3075 /*
3076 * Set the lock before assigning the segment to the file object
3077 */
3078 ExAcquireFastMutex(&Segment->Lock);
3079 FileObject->SectionObjectPointer->DataSectionObject = (PVOID)Segment;
3080
3081 Segment->Image.FileOffset = 0;
3082 Segment->Protection = SectionPageProtection;
3083 Segment->Flags = MM_DATAFILE_SEGMENT;
3084 Segment->Image.Characteristics = 0;
3085 Segment->WriteCopy = (SectionPageProtection & (PAGE_WRITECOPY | PAGE_EXECUTE_WRITECOPY));
3086 if (AllocationAttributes & SEC_RESERVE)
3087 {
3088 Segment->Length.QuadPart = Segment->RawLength.QuadPart = 0;
3089 }
3090 else
3091 {
3092 Segment->RawLength.QuadPart = MaximumSize.QuadPart;
3093 Segment->Length.QuadPart = PAGE_ROUND_UP(Segment->RawLength.QuadPart);
3094 }
3095 Segment->Image.VirtualAddress = 0;
3096 Segment->Locked = TRUE;
3097 MiInitializeSectionPageTable(Segment);
3098 }
3099 else
3100 {
3101 /*
3102 * If the file is already mapped as a data file then we may need
3103 * to extend it
3104 */
3105 Segment =
3106 (PMM_SECTION_SEGMENT)FileObject->SectionObjectPointer->
3107 DataSectionObject;
3108 Section->Segment = Segment;
3109 (void)InterlockedIncrementUL(&Segment->ReferenceCount);
3110 MmLockSectionSegment(Segment);
3111
3112 if (MaximumSize.QuadPart > Segment->RawLength.QuadPart &&
3113 !(AllocationAttributes & SEC_RESERVE))
3114 {
3115 Segment->RawLength.QuadPart = MaximumSize.QuadPart;
3116 Segment->Length.QuadPart = PAGE_ROUND_UP(Segment->RawLength.QuadPart);
3117 }
3118 }
3119 MmUnlockSectionSegment(Segment);
3120 Section->FileObject = FileObject;
3121 Section->MaximumSize = MaximumSize;
3122 #ifndef NEWCC
3123 CcRosReferenceCache(FileObject);
3124 #endif
3125 //KeSetEvent((PVOID)&FileObject->Lock, IO_NO_INCREMENT, FALSE);
3126 *SectionObject = Section;
3127 return(STATUS_SUCCESS);
3128 }
3129
3130 /*
3131 TODO: not that great (declaring loaders statically, having to declare all of
3132 them, having to keep them extern, etc.), will fix in the future
3133 */
3134 extern NTSTATUS NTAPI PeFmtCreateSection
3135 (
3136 IN CONST VOID * FileHeader,
3137 IN SIZE_T FileHeaderSize,
3138 IN PVOID File,
3139 OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
3140 OUT PULONG Flags,
3141 IN PEXEFMT_CB_READ_FILE ReadFileCb,
3142 IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
3143 );
3144
3145 extern NTSTATUS NTAPI ElfFmtCreateSection
3146 (
3147 IN CONST VOID * FileHeader,
3148 IN SIZE_T FileHeaderSize,
3149 IN PVOID File,
3150 OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
3151 OUT PULONG Flags,
3152 IN PEXEFMT_CB_READ_FILE ReadFileCb,
3153 IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
3154 );
3155
3156 /* TODO: this is a standard DDK/PSDK macro */
3157 #ifndef RTL_NUMBER_OF
3158 #define RTL_NUMBER_OF(ARR_) (sizeof(ARR_) / sizeof((ARR_)[0]))
3159 #endif
3160
3161 static PEXEFMT_LOADER ExeFmtpLoaders[] =
3162 {
3163 PeFmtCreateSection,
3164 #ifdef __ELF
3165 ElfFmtCreateSection
3166 #endif
3167 };
3168
3169 static
3170 PMM_SECTION_SEGMENT
3171 NTAPI
3172 ExeFmtpAllocateSegments(IN ULONG NrSegments)
3173 {
3174 SIZE_T SizeOfSegments;
3175 PMM_SECTION_SEGMENT Segments;
3176
3177 /* TODO: check for integer overflow */
3178 SizeOfSegments = sizeof(MM_SECTION_SEGMENT) * NrSegments;
3179
3180 Segments = ExAllocatePoolWithTag(NonPagedPool,
3181 SizeOfSegments,
3182 TAG_MM_SECTION_SEGMENT);
3183
3184 if(Segments)
3185 RtlZeroMemory(Segments, SizeOfSegments);
3186
3187 return Segments;
3188 }
3189
3190 static
3191 NTSTATUS
3192 NTAPI
3193 ExeFmtpReadFile(IN PVOID File,
3194 IN PLARGE_INTEGER Offset,
3195 IN ULONG Length,
3196 OUT PVOID * Data,
3197 OUT PVOID * AllocBase,
3198 OUT PULONG ReadSize)
3199 {
3200 NTSTATUS Status;
3201 LARGE_INTEGER FileOffset;
3202 ULONG AdjustOffset;
3203 ULONG OffsetAdjustment;
3204 ULONG BufferSize;
3205 ULONG UsedSize;
3206 PVOID Buffer;
3207 PFILE_OBJECT FileObject = File;
3208 IO_STATUS_BLOCK Iosb;
3209
3210 ASSERT_IRQL_LESS(DISPATCH_LEVEL);
3211
3212 if(Length == 0)
3213 {
3214 KeBugCheck(MEMORY_MANAGEMENT);
3215 }
3216
3217 FileOffset = *Offset;
3218
3219 /* Negative/special offset: it cannot be used in this context */
3220 if(FileOffset.u.HighPart < 0)
3221 {
3222 KeBugCheck(MEMORY_MANAGEMENT);
3223 }
3224
3225 AdjustOffset = PAGE_ROUND_DOWN(FileOffset.u.LowPart);
3226 OffsetAdjustment = FileOffset.u.LowPart - AdjustOffset;
3227 FileOffset.u.LowPart = AdjustOffset;
3228
3229 BufferSize = Length + OffsetAdjustment;
3230 BufferSize = PAGE_ROUND_UP(BufferSize);
3231
3232 /*
3233 * It's ok to use paged pool, because this is a temporary buffer only used in
3234 * the loading of executables. The assumption is that MmCreateSection is
3235 * always called at low IRQLs and that these buffers don't survive a brief
3236 * initialization phase
3237 */
3238 Buffer = ExAllocatePoolWithTag(PagedPool,
3239 BufferSize,
3240 'rXmM');
3241 if (!Buffer)
3242 {
3243 KeBugCheck(MEMORY_MANAGEMENT);
3244 }
3245
3246 UsedSize = 0;
3247
3248 Status = MiSimpleRead(FileObject, &FileOffset, Buffer, BufferSize, TRUE, &Iosb);
3249
3250 UsedSize = (ULONG)Iosb.Information;
3251
3252 if(NT_SUCCESS(Status) && UsedSize < OffsetAdjustment)
3253 {
3254 Status = STATUS_IN_PAGE_ERROR;
3255 ASSERT(!NT_SUCCESS(Status));
3256 }
3257
3258 if(NT_SUCCESS(Status))
3259 {
3260 *Data = (PVOID)((ULONG_PTR)Buffer + OffsetAdjustment);
3261 *AllocBase = Buffer;
3262 *ReadSize = UsedSize - OffsetAdjustment;
3263 }
3264 else
3265 {
3266 ExFreePoolWithTag(Buffer, 'rXmM');
3267 }
3268
3269 return Status;
3270 }
3271
3272 #ifdef NASSERT
3273 # define MmspAssertSegmentsSorted(OBJ_) ((void)0)
3274 # define MmspAssertSegmentsNoOverlap(OBJ_) ((void)0)
3275 # define MmspAssertSegmentsPageAligned(OBJ_) ((void)0)
3276 #else
3277 static
3278 VOID
3279 NTAPI
3280 MmspAssertSegmentsSorted(IN PMM_IMAGE_SECTION_OBJECT ImageSectionObject)
3281 {
3282 ULONG i;
3283
3284 for( i = 1; i < ImageSectionObject->NrSegments; ++ i )
3285 {
3286 ASSERT(ImageSectionObject->Segments[i].Image.VirtualAddress >=
3287 ImageSectionObject->Segments[i - 1].Image.VirtualAddress);
3288 }
3289 }
3290
3291 static
3292 VOID
3293 NTAPI
3294 MmspAssertSegmentsNoOverlap(IN PMM_IMAGE_SECTION_OBJECT ImageSectionObject)
3295 {
3296 ULONG i;
3297
3298 MmspAssertSegmentsSorted(ImageSectionObject);
3299
3300 for( i = 0; i < ImageSectionObject->NrSegments; ++ i )
3301 {
3302 ASSERT(ImageSectionObject->Segments[i].Length.QuadPart > 0);
3303
3304 if(i > 0)
3305 {
3306 ASSERT(ImageSectionObject->Segments[i].Image.VirtualAddress >=
3307 (ImageSectionObject->Segments[i - 1].Image.VirtualAddress +
3308 ImageSectionObject->Segments[i - 1].Length.QuadPart));
3309 }
3310 }
3311 }
3312
3313 static
3314 VOID
3315 NTAPI
3316 MmspAssertSegmentsPageAligned(IN PMM_IMAGE_SECTION_OBJECT ImageSectionObject)
3317 {
3318 ULONG i;
3319
3320 for( i = 0; i < ImageSectionObject->NrSegments; ++ i )
3321 {
3322 ASSERT((ImageSectionObject->Segments[i].Image.VirtualAddress % PAGE_SIZE) == 0);
3323 ASSERT((ImageSectionObject->Segments[i].Length.QuadPart % PAGE_SIZE) == 0);
3324 }
3325 }
3326 #endif
3327
3328 static
3329 int
3330 __cdecl
3331 MmspCompareSegments(const void * x,
3332 const void * y)
3333 {
3334 const MM_SECTION_SEGMENT *Segment1 = (const MM_SECTION_SEGMENT *)x;
3335 const MM_SECTION_SEGMENT *Segment2 = (const MM_SECTION_SEGMENT *)y;
3336
3337 return
3338 (Segment1->Image.VirtualAddress - Segment2->Image.VirtualAddress) >>
3339 ((sizeof(ULONG_PTR) - sizeof(int)) * 8);
3340 }
3341
3342 /*
3343 * Ensures an image section's segments are sorted in memory
3344 */
3345 static
3346 VOID
3347 NTAPI
3348 MmspSortSegments(IN OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
3349 IN ULONG Flags)
3350 {
3351 if (Flags & EXEFMT_LOAD_ASSUME_SEGMENTS_SORTED)
3352 {
3353 MmspAssertSegmentsSorted(ImageSectionObject);
3354 }
3355 else
3356 {
3357 qsort(ImageSectionObject->Segments,
3358 ImageSectionObject->NrSegments,
3359 sizeof(ImageSectionObject->Segments[0]),
3360 MmspCompareSegments);
3361 }
3362 }
3363
3364
3365 /*
3366 * Ensures an image section's segments don't overlap in memory and don't have
3367 * gaps and don't have a null size. We let them map to overlapping file regions,
3368 * though - that's not necessarily an error
3369 */
3370 static
3371 BOOLEAN
3372 NTAPI
3373 MmspCheckSegmentBounds
3374 (
3375 IN OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
3376 IN ULONG Flags
3377 )
3378 {
3379 ULONG i;
3380
3381 if (Flags & EXEFMT_LOAD_ASSUME_SEGMENTS_NO_OVERLAP)
3382 {
3383 MmspAssertSegmentsNoOverlap(ImageSectionObject);
3384 return TRUE;
3385 }
3386
3387 ASSERT(ImageSectionObject->NrSegments >= 1);
3388
3389 for ( i = 0; i < ImageSectionObject->NrSegments; ++ i )
3390 {
3391 if(ImageSectionObject->Segments[i].Length.QuadPart == 0)
3392 {
3393 return FALSE;
3394 }
3395
3396 if(i > 0)
3397 {
3398 /*
3399 * TODO: relax the limitation on gaps. For example, gaps smaller than a
3400 * page could be OK (Windows seems to be OK with them), and larger gaps
3401 * could lead to image sections spanning several discontiguous regions
3402 * (NtMapViewOfSection could then refuse to map them, and they could
3403 * e.g. only be allowed as parameters to NtCreateProcess, like on UNIX)
3404 */
3405 if ((ImageSectionObject->Segments[i - 1].Image.VirtualAddress +
3406 ImageSectionObject->Segments[i - 1].Length.QuadPart) !=
3407 ImageSectionObject->Segments[i].Image.VirtualAddress)
3408 {
3409 return FALSE;
3410 }
3411 }
3412 }
3413
3414 return TRUE;
3415 }
3416
3417 /*
3418 * Merges and pads an image section's segments until they all are page-aligned
3419 * and have a size that is a multiple of the page size
3420 */
3421 static
3422 BOOLEAN
3423 NTAPI
3424 MmspPageAlignSegments
3425 (
3426 IN OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
3427 IN ULONG Flags
3428 )
3429 {
3430 ULONG i;
3431 ULONG LastSegment;
3432 PMM_SECTION_SEGMENT EffectiveSegment;
3433
3434 if (Flags & EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED)
3435 {
3436 MmspAssertSegmentsPageAligned(ImageSectionObject);
3437 return TRUE;
3438 }
3439
3440 LastSegment = 0;
3441 EffectiveSegment = &ImageSectionObject->Segments[LastSegment];
3442
3443 for ( i = 0; i < ImageSectionObject->NrSegments; ++ i )
3444 {
3445 /*
3446 * The first segment requires special handling
3447 */
3448 if (i == 0)
3449 {
3450 ULONG_PTR VirtualAddress;
3451 ULONG_PTR VirtualOffset;
3452
3453 VirtualAddress = EffectiveSegment->Image.VirtualAddress;
3454
3455 /* Round down the virtual address to the nearest page */
3456 EffectiveSegment->Image.VirtualAddress = PAGE_ROUND_DOWN(VirtualAddress);
3457
3458 /* Round up the virtual size to the nearest page */
3459 EffectiveSegment->Length.QuadPart = PAGE_ROUND_UP(VirtualAddress + EffectiveSegment->Length.QuadPart) -
3460 EffectiveSegment->Image.VirtualAddress;
3461
3462 /* Adjust the raw address and size */
3463 VirtualOffset = VirtualAddress - EffectiveSegment->Image.VirtualAddress;
3464
3465 if (EffectiveSegment->Image.FileOffset < VirtualOffset)
3466 {
3467 return FALSE;
3468 }
3469
3470 /*
3471 * Garbage in, garbage out: unaligned base addresses make the file
3472 * offset point in curious and odd places, but that's what we were
3473 * asked for
3474 */
3475 EffectiveSegment->Image.FileOffset -= VirtualOffset;
3476 EffectiveSegment->RawLength.QuadPart += VirtualOffset;
3477 }
3478 else
3479 {
3480 PMM_SECTION_SEGMENT Segment = &ImageSectionObject->Segments[i];
3481 ULONG_PTR EndOfEffectiveSegment;
3482
3483 EndOfEffectiveSegment = (ULONG_PTR)(EffectiveSegment->Image.VirtualAddress + EffectiveSegment->Length.QuadPart);
3484 ASSERT((EndOfEffectiveSegment % PAGE_SIZE) == 0);
3485
3486 /*
3487 * The current segment begins exactly where the current effective
3488 * segment ended, therefore beginning a new effective segment
3489 */
3490 if (EndOfEffectiveSegment == Segment->Image.VirtualAddress)
3491 {
3492 LastSegment ++;
3493 ASSERT(LastSegment <= i);
3494 ASSERT(LastSegment < ImageSectionObject->NrSegments);
3495
3496 EffectiveSegment = &ImageSectionObject->Segments[LastSegment];
3497
3498 if (LastSegment != i)
3499 {
3500 /*
3501 * Copy the current segment. If necessary, the effective segment
3502 * will be expanded later
3503 */
3504 *EffectiveSegment = *Segment;
3505 }
3506
3507 /*
3508 * Page-align the virtual size. We know for sure the virtual address
3509 * already is
3510 */
3511 ASSERT((EffectiveSegment->Image.VirtualAddress % PAGE_SIZE) == 0);
3512 EffectiveSegment->Length.QuadPart = PAGE_ROUND_UP(EffectiveSegment->Length.QuadPart);
3513 }
3514 /*
3515 * The current segment is still part of the current effective segment:
3516 * extend the effective segment to reflect this
3517 */
3518 else if (EndOfEffectiveSegment > Segment->Image.VirtualAddress)
3519 {
3520 static const ULONG FlagsToProtection[16] =
3521 {
3522 PAGE_NOACCESS,
3523 PAGE_READONLY,
3524 PAGE_READWRITE,
3525 PAGE_READWRITE,
3526 PAGE_EXECUTE_READ,
3527 PAGE_EXECUTE_READ,
3528 PAGE_EXECUTE_READWRITE,
3529 PAGE_EXECUTE_READWRITE,
3530 PAGE_WRITECOPY,
3531 PAGE_WRITECOPY,
3532 PAGE_WRITECOPY,
3533 PAGE_WRITECOPY,
3534 PAGE_EXECUTE_WRITECOPY,
3535 PAGE_EXECUTE_WRITECOPY,
3536 PAGE_EXECUTE_WRITECOPY,
3537 PAGE_EXECUTE_WRITECOPY
3538 };
3539
3540 unsigned ProtectionFlags;
3541
3542 /*
3543 * Extend the file size
3544 */
3545
3546 /* Unaligned segments must be contiguous within the file */
3547 if (Segment->Image.FileOffset != (EffectiveSegment->Image.FileOffset +
3548 EffectiveSegment->RawLength.QuadPart))
3549 {
3550 return FALSE;
3551 }
3552
3553 EffectiveSegment->RawLength.QuadPart += Segment->RawLength.QuadPart;
3554
3555 /*
3556 * Extend the virtual size
3557 */
3558 ASSERT(PAGE_ROUND_UP(Segment->Image.VirtualAddress + Segment->Length.QuadPart) >= EndOfEffectiveSegment);
3559
3560 EffectiveSegment->Length.QuadPart = PAGE_ROUND_UP(Segment->Image.VirtualAddress + Segment->Length.QuadPart) -
3561 EffectiveSegment->Image.VirtualAddress;
3562
3563 /*
3564 * Merge the protection
3565 */
3566 EffectiveSegment->Protection |= Segment->Protection;
3567
3568 /* Clean up redundance */
3569 ProtectionFlags = 0;
3570
3571 if(EffectiveSegment->Protection & PAGE_IS_READABLE)
3572 ProtectionFlags |= 1 << 0;
3573
3574 if(EffectiveSegment->Protection & PAGE_IS_WRITABLE)
3575 ProtectionFlags |= 1 << 1;
3576
3577 if(EffectiveSegment->Protection & PAGE_IS_EXECUTABLE)
3578 ProtectionFlags |= 1 << 2;
3579
3580 if(EffectiveSegment->Protection & PAGE_IS_WRITECOPY)
3581 ProtectionFlags |= 1 << 3;
3582
3583 ASSERT(ProtectionFlags < 16);
3584 EffectiveSegment->Protection = FlagsToProtection[ProtectionFlags];
3585
3586 /* If a segment was required to be shared and cannot, fail */
3587 if(!(Segment->Protection & PAGE_IS_WRITECOPY) &&
3588 EffectiveSegment->Protection & PAGE_IS_WRITECOPY)
3589 {
3590 return FALSE;
3591 }
3592 }
3593 /*
3594 * We assume no holes between segments at this point
3595 */
3596 else
3597 {
3598 KeBugCheck(MEMORY_MANAGEMENT);
3599 }
3600 }
3601 }
3602 ImageSectionObject->NrSegments = LastSegment + 1;
3603
3604 return TRUE;
3605 }
3606
3607 NTSTATUS
3608 ExeFmtpCreateImageSection(HANDLE FileHandle,
3609 PMM_IMAGE_SECTION_OBJECT ImageSectionObject)
3610 {
3611 LARGE_INTEGER Offset;
3612 PVOID FileHeader;
3613 PVOID FileHeaderBuffer;
3614 ULONG FileHeaderSize;
3615 ULONG Flags;
3616 ULONG OldNrSegments;
3617 NTSTATUS Status;
3618 ULONG i;
3619
3620 /*
3621 * Read the beginning of the file (2 pages). Should be enough to contain
3622 * all (or most) of the headers
3623 */
3624 Offset.QuadPart = 0;
3625
3626 /* FIXME: use FileObject instead of FileHandle */
3627 Status = ExeFmtpReadFile (FileHandle,
3628 &Offset,
3629 PAGE_SIZE * 2,
3630 &FileHeader,
3631 &FileHeaderBuffer,
3632 &FileHeaderSize);
3633
3634 if (!NT_SUCCESS(Status))
3635 return Status;
3636
3637 if (FileHeaderSize == 0)
3638 {
3639 ExFreePool(FileHeaderBuffer);
3640 return STATUS_UNSUCCESSFUL;
3641 }
3642
3643 /*
3644 * Look for a loader that can handle this executable
3645 */
3646 for (i = 0; i < RTL_NUMBER_OF(ExeFmtpLoaders); ++ i)
3647 {
3648 RtlZeroMemory(ImageSectionObject, sizeof(*ImageSectionObject));
3649 Flags = 0;
3650
3651 /* FIXME: use FileObject instead of FileHandle */
3652 Status = ExeFmtpLoaders[i](FileHeader,
3653 FileHeaderSize,
3654 FileHandle,
3655 ImageSectionObject,
3656 &Flags,
3657 ExeFmtpReadFile,
3658 ExeFmtpAllocateSegments);
3659
3660 if (!NT_SUCCESS(Status))
3661 {
3662 if (ImageSectionObject->Segments)
3663 {
3664 ExFreePool(ImageSectionObject->Segments);
3665 ImageSectionObject->Segments = NULL;
3666 }
3667 }
3668
3669 if (Status != STATUS_ROS_EXEFMT_UNKNOWN_FORMAT)
3670 break;
3671 }
3672
3673 ExFreePoolWithTag(FileHeaderBuffer, 'rXmM');
3674
3675 /*
3676 * No loader handled the format
3677 */
3678 if (Status == STATUS_ROS_EXEFMT_UNKNOWN_FORMAT)
3679 {
3680 Status = STATUS_INVALID_IMAGE_NOT_MZ;
3681 ASSERT(!NT_SUCCESS(Status));
3682 }
3683
3684 if (!NT_SUCCESS(Status))
3685 return Status;
3686
3687 ASSERT(ImageSectionObject->Segments != NULL);
3688
3689 /*
3690 * Some defaults
3691 */
3692 /* FIXME? are these values platform-dependent? */
3693 if (ImageSectionObject->ImageInformation.MaximumStackSize == 0)
3694 ImageSectionObject->ImageInformation.MaximumStackSize = 0x40000;
3695
3696 if(ImageSectionObject->ImageInformation.CommittedStackSize == 0)
3697 ImageSectionObject->ImageInformation.CommittedStackSize = 0x1000;
3698
3699 if(ImageSectionObject->BasedAddress == NULL)
3700 {
3701 if(ImageSectionObject->ImageInformation.ImageCharacteristics & IMAGE_FILE_DLL)
3702 ImageSectionObject->BasedAddress = (PVOID)0x10000000;
3703 else
3704 ImageSectionObject->BasedAddress = (PVOID)0x00400000;
3705 }
3706
3707 /*
3708 * And now the fun part: fixing the segments
3709 */
3710
3711 /* Sort them by virtual address */
3712 MmspSortSegments(ImageSectionObject, Flags);
3713
3714 /* Ensure they don't overlap in memory */
3715 if (!MmspCheckSegmentBounds(ImageSectionObject, Flags))
3716 return STATUS_INVALID_IMAGE_FORMAT;
3717
3718 /* Ensure they are aligned */
3719 OldNrSegments = ImageSectionObject->NrSegments;
3720
3721 if (!MmspPageAlignSegments(ImageSectionObject, Flags))
3722 return STATUS_INVALID_IMAGE_FORMAT;
3723
3724 /* Trim them if the alignment phase merged some of them */
3725 if (ImageSectionObject->NrSegments < OldNrSegments)
3726 {
3727 PMM_SECTION_SEGMENT Segments;
3728 SIZE_T SizeOfSegments;
3729
3730 SizeOfSegments = sizeof(MM_SECTION_SEGMENT) * ImageSectionObject->NrSegments;
3731
3732 Segments = ExAllocatePoolWithTag(PagedPool,
3733 SizeOfSegments,
3734 TAG_MM_SECTION_SEGMENT);
3735
3736 if (Segments == NULL)
3737 return STATUS_INSUFFICIENT_RESOURCES;
3738
3739 RtlCopyMemory(Segments, ImageSectionObject->Segments, SizeOfSegments);
3740 ExFreePool(ImageSectionObject->Segments);
3741 ImageSectionObject->Segments = Segments;
3742 }
3743
3744 /* And finish their initialization */
3745 for ( i = 0; i < ImageSectionObject->NrSegments; ++ i )
3746 {
3747 ExInitializeFastMutex(&ImageSectionObject->Segments[i].Lock);
3748 ImageSectionObject->Segments[i].ReferenceCount = 1;
3749 MiInitializeSectionPageTable(&ImageSectionObject->Segments[i]);
3750 }
3751
3752 ASSERT(NT_SUCCESS(Status));
3753 return Status;
3754 }
3755
3756 NTSTATUS
3757 MmCreateImageSection(PROS_SECTION_OBJECT *SectionObject,
3758 ACCESS_MASK DesiredAccess,
3759 POBJECT_ATTRIBUTES ObjectAttributes,
3760 PLARGE_INTEGER UMaximumSize,
3761 ULONG SectionPageProtection,
3762 ULONG AllocationAttributes,
3763 PFILE_OBJECT FileObject)
3764 {
3765 PROS_SECTION_OBJECT Section;
3766 NTSTATUS Status;
3767 PMM_SECTION_SEGMENT SectionSegments;
3768 PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
3769 ULONG i;
3770
3771 if (FileObject == NULL)
3772 return STATUS_INVALID_FILE_FOR_SECTION;
3773
3774 /*
3775 * Create the section
3776 */
3777 Status = ObCreateObject (ExGetPreviousMode(),
3778 MmSectionObjectType,
3779 ObjectAttributes,
3780 ExGetPreviousMode(),
3781 NULL,
3782 sizeof(ROS_SECTION_OBJECT),
3783 0,
3784 0,
3785 (PVOID*)(PVOID)&Section);
3786 if (!NT_SUCCESS(Status))
3787 {
3788 ObDereferenceObject(FileObject);
3789 return(Status);
3790 }
3791
3792 /*
3793 * Initialize it
3794 */
3795 RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
3796 Section->Type = 'SC';
3797 Section->Size = 'TN';
3798 Section->SectionPageProtection = SectionPageProtection;
3799 Section->AllocationAttributes = AllocationAttributes;
3800
3801 #ifndef NEWCC
3802 /*
3803 * Initialized caching for this file object if previously caching
3804 * was initialized for the same on disk file
3805 */
3806 Status = CcTryToInitializeFileCache(FileObject);
3807 #else
3808 Status = STATUS_SUCCESS;
3809 #endif
3810
3811 if (!NT_SUCCESS(Status) || FileObject->SectionObjectPointer->ImageSectionObject == NULL)
3812 {
3813 NTSTATUS StatusExeFmt;
3814
3815 ImageSectionObject = ExAllocatePoolWithTag(PagedPool, sizeof(MM_IMAGE_SECTION_OBJECT), TAG_MM_SECTION_SEGMENT);
3816 if (ImageSectionObject == NULL)
3817 {
3818 ObDereferenceObject(FileObject);
3819 ObDereferenceObject(Section);
3820 return(STATUS_NO_MEMORY);
3821 }
3822
3823 RtlZeroMemory(ImageSectionObject, sizeof(MM_IMAGE_SECTION_OBJECT));
3824
3825 StatusExeFmt = ExeFmtpCreateImageSection(FileObject, ImageSectionObject);
3826
3827 if (!NT_SUCCESS(StatusExeFmt))
3828 {
3829 if(ImageSectionObject->Segments != NULL)
3830 ExFreePool(ImageSectionObject->Segments);
3831
3832 ExFreePoolWithTag(ImageSectionObject, TAG_MM_SECTION_SEGMENT);
3833 ObDereferenceObject(Section);
3834 ObDereferenceObject(FileObject);
3835 return(StatusExeFmt);
3836 }
3837
3838 Section->ImageSection = ImageSectionObject;
3839 ASSERT(ImageSectionObject->Segments);
3840
3841 /*
3842 * Lock the file
3843 */
3844 Status = MmspWaitForFileLock(FileObject);
3845 if (!NT_SUCCESS(Status))
3846 {
3847 ExFreePool(ImageSectionObject->Segments);
3848 ExFreePool(ImageSectionObject);
3849 ObDereferenceObject(Section);
3850 ObDereferenceObject(FileObject);
3851 return(Status);
3852 }
3853
3854 if (NULL != InterlockedCompareExchangePointer(&FileObject->SectionObjectPointer->ImageSectionObject,
3855 ImageSectionObject, NULL))
3856 {
3857 /*
3858 * An other thread has initialized the same image in the background
3859 */
3860 ExFreePool(ImageSectionObject->Segments);
3861 ExFreePool(ImageSectionObject);
3862 ImageSectionObject = FileObject->SectionObjectPointer->ImageSectionObject;
3863 Section->ImageSection = ImageSectionObject;
3864 SectionSegments = ImageSectionObject->Segments;
3865
3866 for (i = 0; i < ImageSectionObject->NrSegments; i++)
3867 {
3868 (void)InterlockedIncrementUL(&SectionSegments[i].ReferenceCount);
3869 }
3870 }
3871
3872 Status = StatusExeFmt;
3873 }
3874 else
3875 {
3876 /*
3877 * Lock the file
3878 */
3879 Status = MmspWaitForFileLock(FileObject);
3880 if (Status != STATUS_SUCCESS)
3881 {
3882 ObDereferenceObject(Section);
3883 ObDereferenceObject(FileObject);
3884 return(Status);
3885 }
3886
3887 ImageSectionObject = FileObject->SectionObjectPointer->ImageSectionObject;
3888 Section->ImageSection = ImageSectionObject;
3889 SectionSegments = ImageSectionObject->Segments;
3890
3891 /*
3892 * Otherwise just reference all the section segments
3893 */
3894 for (i = 0; i < ImageSectionObject->NrSegments; i++)
3895 {
3896 (void)InterlockedIncrementUL(&SectionSegments[i].ReferenceCount);
3897 }
3898
3899 Status = STATUS_SUCCESS;
3900 }
3901 Section->FileObject = FileObject;
3902 #ifndef NEWCC
3903 CcRosReferenceCache(FileObject);
3904 #endif
3905 //KeSetEvent((PVOID)&FileObject->Lock, IO_NO_INCREMENT, FALSE);
3906 *SectionObject = Section;
3907 return(Status);
3908 }
3909
3910
3911
3912 static NTSTATUS
3913 MmMapViewOfSegment(PMMSUPPORT AddressSpace,
3914 PROS_SECTION_OBJECT Section,
3915 PMM_SECTION_SEGMENT Segment,
3916 PVOID* BaseAddress,
3917 SIZE_T ViewSize,
3918 ULONG Protect,
3919 ULONG ViewOffset,
3920 ULONG AllocationType)
3921 {
3922 PMEMORY_AREA MArea;
3923 NTSTATUS Status;
3924 ULONG Granularity;
3925
3926 if (Segment->WriteCopy)
3927 {
3928 /* We have to do this because the not present fault
3929 * and access fault handlers depend on the protection
3930 * that should be granted AFTER the COW fault takes
3931 * place to be in Region->Protect. The not present fault
3932 * handler changes this to the correct protection for COW when
3933 * mapping the pages into the process's address space. If a COW
3934 * fault takes place, the access fault handler sets the page protection
3935 * to these values for the newly copied pages
3936 */
3937 if (Protect == PAGE_WRITECOPY)
3938 Protect = PAGE_READWRITE;
3939 else if (Protect == PAGE_EXECUTE_WRITECOPY)
3940 Protect = PAGE_EXECUTE_READWRITE;
3941 }
3942
3943 if (*BaseAddress == NULL)
3944 Granularity = MM_ALLOCATION_GRANULARITY;
3945 else
3946 Granularity = PAGE_SIZE;
3947
3948 #ifdef NEWCC
3949 if (Segment->Flags & MM_DATAFILE_SEGMENT)
3950 {
3951 LARGE_INTEGER FileOffset;
3952 FileOffset.QuadPart = ViewOffset;
3953 ObReferenceObject(Section);
3954 return _MiMapViewOfSegment(AddressSpace, Segment, BaseAddress, ViewSize, Protect, &FileOffset, AllocationType, __FILE__, __LINE__);
3955 }
3956 #endif
3957 Status = MmCreateMemoryArea(AddressSpace,
3958 MEMORY_AREA_SECTION_VIEW,
3959 BaseAddress,
3960 ViewSize,
3961 Protect,
3962 &MArea,
3963 FALSE,
3964 AllocationType,
3965 Granularity);
3966 if (!NT_SUCCESS(Status))
3967 {
3968 DPRINT1("Mapping between 0x%p and 0x%p failed (%X).\n",
3969 (*BaseAddress), (char*)(*BaseAddress) + ViewSize, Status);
3970 return(Status);
3971 }
3972
3973 ObReferenceObject((PVOID)Section);
3974
3975 MArea->Data.SectionData.Segment = Segment;
3976 MArea->Data.SectionData.Section = Section;
3977 MArea->Data.SectionData.ViewOffset.QuadPart = ViewOffset;
3978 MmInitializeRegion(&MArea->Data.SectionData.RegionListHead,
3979 ViewSize, 0, Protect);
3980
3981 return(STATUS_SUCCESS);
3982 }
3983
3984
3985 static VOID
3986 MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
3987 PFN_NUMBER Page, SWAPENTRY SwapEntry, BOOLEAN Dirty)
3988 {
3989 ULONG_PTR Entry;
3990 PFILE_OBJECT FileObject;
3991 PROS_SHARED_CACHE_MAP SharedCacheMap;
3992 LARGE_INTEGER Offset;
3993 SWAPENTRY SavedSwapEntry;
3994 PROS_SECTION_OBJECT Section;
3995 PMM_SECTION_SEGMENT Segment;
3996 PMMSUPPORT AddressSpace;
3997 PEPROCESS Process;
3998
3999 AddressSpace = (PMMSUPPORT)Context;
4000 Process = MmGetAddressSpaceOwner(AddressSpace);
4001
4002 Address = (PVOID)PAGE_ROUND_DOWN(Address);
4003
4004 Offset.QuadPart = ((ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress) +
4005 MemoryArea->Data.SectionData.ViewOffset.QuadPart;
4006
4007 Section = MemoryArea->Data.SectionData.Section;
4008 Segment = MemoryArea->Data.SectionData.Segment;
4009
4010 Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
4011 while (Entry && IS_SWAP_FROM_SSE(Entry) && SWAPENTRY_FROM_SSE(Entry) == MM_WAIT_ENTRY)
4012 {
4013 MmUnlockSectionSegment(Segment);
4014 MmUnlockAddressSpace(AddressSpace);
4015
4016 MiWaitForPageEvent(NULL, NULL);
4017
4018 MmLockAddressSpace(AddressSpace);
4019 MmLockSectionSegment(Segment);
4020 Entry = MmGetPageEntrySectionSegment(Segment, &Offset);
4021 }
4022
4023 /*
4024 * For a dirty, datafile, non-private page mark it as dirty in the
4025 * cache manager.
4026 */
4027 if (Segment->Flags & MM_DATAFILE_SEGMENT)
4028 {
4029 if (Page == PFN_FROM_SSE(Entry) && Dirty)
4030 {
4031 FileObject = MemoryArea->Data.SectionData.Section->FileObject;
4032 SharedCacheMap = FileObject->SectionObjectPointer->SharedCacheMap;
4033 #ifndef NEWCC
4034 CcRosMarkDirtyVacb(SharedCacheMap, Offset.QuadPart + Segment->Image.FileOffset);
4035 #endif
4036 ASSERT(SwapEntry == 0);
4037 }
4038 }
4039
4040 if (SwapEntry != 0)
4041 {
4042 /*
4043 * Sanity check
4044 */
4045 if (Segment->Flags & MM_PAGEFILE_SEGMENT)
4046 {
4047 DPRINT1("Found a swap entry for a page in a pagefile section.\n");
4048 KeBugCheck(MEMORY_MANAGEMENT);
4049 }
4050 MmFreeSwapPage(SwapEntry);
4051 }
4052 else if (Page != 0)
4053 {
4054 if (IS_SWAP_FROM_SSE(Entry) ||
4055 Page != PFN_FROM_SSE(Entry))
4056 {
4057 /*
4058 * Sanity check
4059 */
4060 if (Segment->Flags & MM_PAGEFILE_SEGMENT)
4061 {
4062 DPRINT1("Found a private page in a pagefile section.\n");
4063 KeBugCheck(MEMORY_MANAGEMENT);
4064 }
4065 /*
4066 * Just dereference private pages
4067 */
4068 SavedSwapEntry = MmGetSavedSwapEntryPage(Page);
4069 if (SavedSwapEntry != 0)
4070 {
4071 MmFreeSwapPage(SavedSwapEntry);
4072 MmSetSavedSwapEntryPage(Page, 0);
4073 }
4074 MmDeleteRmap(Page, Process, Address);
4075 MmReleasePageMemoryConsumer(MC_USER, Page);
4076 }
4077 else
4078 {
4079 MmDeleteRmap(Page, Process, Address);
4080 MmUnsharePageEntrySectionSegment(Section, Segment, &Offset, Dirty, FALSE, NULL);
4081 }
4082 }
4083 }
4084
4085 static NTSTATUS
4086 MmUnmapViewOfSegment(PMMSUPPORT AddressSpace,
4087 PVOID BaseAddress)
4088 {
4089 NTSTATUS Status;
4090 PMEMORY_AREA MemoryArea;
4091 PROS_SECTION_OBJECT Section;
4092 PMM_SECTION_SEGMENT Segment;
4093 PLIST_ENTRY CurrentEntry;
4094 PMM_REGION CurrentRegion;
4095 PLIST_ENTRY RegionListHead;
4096
4097 MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace,
4098 BaseAddress);
4099 if (MemoryArea == NULL)
4100 {
4101 return(STATUS_UNSUCCESSFUL);
4102 }
4103
4104 Section = MemoryArea->Data.SectionData.Section;
4105 Segment = MemoryArea->Data.SectionData.Segment;
4106
4107 #ifdef NEWCC
4108 if (Segment->Flags & MM_DATAFILE_SEGMENT)
4109 {
4110 MmUnlockAddressSpace(AddressSpace);
4111 Status = MmUnmapViewOfCacheSegment(AddressSpace, BaseAddress);
4112 MmLockAddressSpace(AddressSpace);
4113
4114 return Status;
4115 }
4116 #endif
4117
4118 MemoryArea->DeleteInProgress = TRUE;
4119
4120 MmLockSectionSegment(Segment);
4121
4122 RegionListHead = &MemoryArea->Data.SectionData.RegionListHead;
4123 while (!IsListEmpty(RegionListHead))
4124 {
4125 CurrentEntry = RemoveHeadList(RegionListHead);
4126 CurrentRegion = CONTAINING_RECORD(CurrentEntry, MM_REGION, RegionListEntry);
4127 ExFreePoolWithTag(CurrentRegion, TAG_MM_REGION);
4128 }
4129
4130 if (Section->AllocationAttributes & SEC_PHYSICALMEMORY)
4131 {
4132 Status = MmFreeMemoryArea(AddressSpace,
4133 MemoryArea,
4134 NULL,
4135 NULL);
4136 }
4137 else
4138 {
4139 Status = MmFreeMemoryArea(AddressSpace,
4140 MemoryArea,
4141 MmFreeSectionPage,
4142 AddressSpace);
4143 }
4144 MmUnlockSectionSegment(Segment);
4145 ObDereferenceObject(Section);
4146 return(Status);
4147 }
4148
4149 NTSTATUS
4150 NTAPI
4151 MiRosUnmapViewOfSection(IN PEPROCESS Process,
4152 IN PVOID BaseAddress,
4153 IN ULONG Flags)
4154 {
4155 NTSTATUS Status;
4156 PMEMORY_AREA MemoryArea;
4157 PMMSUPPORT AddressSpace;
4158 PROS_SECTION_OBJECT Section;
4159 PVOID ImageBaseAddress = 0;
4160
4161 DPRINT("Opening memory area Process %p BaseAddress %p\n",
4162 Process, BaseAddress);
4163
4164 ASSERT(Process);
4165
4166 AddressSpace = Process ? &Process->Vm : MmGetKernelAddressSpace();
4167
4168 MmLockAddressSpace(AddressSpace);
4169 MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace,
4170 BaseAddress);
4171 if (MemoryArea == NULL ||
4172 ((MemoryArea->Type != MEMORY_AREA_SECTION_VIEW) &&
4173 (MemoryArea->Type != MEMORY_AREA_CACHE)) ||
4174 MemoryArea->DeleteInProgress)
4175 {
4176 if (MemoryArea) NT_ASSERT(MemoryArea->Type != MEMORY_AREA_OWNED_BY_ARM3);
4177 MmUnlockAddressSpace(AddressSpace);
4178 return STATUS_NOT_MAPPED_VIEW;
4179 }
4180
4181 Section = MemoryArea->Data.SectionData.Section;
4182
4183 if ((Section != NULL) && (Section->AllocationAttributes & SEC_IMAGE))
4184 {
4185 ULONG i;
4186 ULONG NrSegments;
4187 PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
4188 PMM_SECTION_SEGMENT SectionSegments;
4189 PMM_SECTION_SEGMENT Segment;
4190
4191 Segment = MemoryArea->Data.SectionData.Segment;
4192 ImageSectionObject = Section->ImageSection;
4193 SectionSegments = ImageSectionObject->Segments;
4194 NrSegments = ImageSectionObject->NrSegments;
4195
4196 MemoryArea->DeleteInProgress = TRUE;
4197
4198 /* Search for the current segment within the section segments
4199 * and calculate the image base address */
4200 for (i = 0; i < NrSegments; i++)
4201 {
4202 if (!(SectionSegments[i].Image.Characteristics & IMAGE_SCN_TYPE_NOLOAD))
4203 {
4204 if (Segment == &SectionSegments[i])
4205 {
4206 ImageBaseAddress = (char*)BaseAddress - (ULONG_PTR)SectionSegments[i].Image.VirtualAddress;
4207 break;
4208 }
4209 }
4210 }
4211 if (i >= NrSegments)
4212 {
4213 KeBugCheck(MEMORY_MANAGEMENT);
4214 }
4215
4216 for (i = 0; i < NrSegments; i++)
4217 {
4218 if (!(SectionSegments[i].Image.Characteristics & IMAGE_SCN_TYPE_NOLOAD))
4219 {
4220 PVOID SBaseAddress = (PVOID)
4221 ((char*)ImageBaseAddress + (ULONG_PTR)SectionSegments[i].Image.VirtualAddress);
4222
4223 Status = MmUnmapViewOfSegment(AddressSpace, SBaseAddress);
4224 NT_ASSERT(NT_SUCCESS(Status));
4225 }
4226 }
4227 }
4228 else
4229 {
4230 Status = MmUnmapViewOfSegment(AddressSpace, BaseAddress);
4231 NT_ASSERT(NT_SUCCESS(Status));
4232 }
4233
4234 MmUnlockAddressSpace(AddressSpace);
4235
4236 /* Notify debugger */
4237 if (ImageBaseAddress) DbgkUnMapViewOfSection(ImageBaseAddress);
4238
4239 return(STATUS_SUCCESS);
4240 }
4241
4242
4243
4244
4245 /**
4246 * Queries the information of a section object.
4247 *
4248 * @param SectionHandle
4249 * Handle to the section object. It must be opened with SECTION_QUERY
4250 * access.
4251 * @param SectionInformationClass
4252 * Index to a certain information structure. Can be either
4253 * SectionBasicInformation or SectionImageInformation. The latter
4254 * is valid only for sections that were created with the SEC_IMAGE
4255 * flag.
4256 * @param SectionInformation
4257 * Caller supplies storage for resulting information.
4258 * @param Length
4259 * Size of the supplied storage.
4260 * @param ResultLength
4261 * Data written.
4262 *
4263 * @return Status.
4264 *
4265 * @implemented
4266 */
4267 NTSTATUS NTAPI
4268 NtQuerySection(IN HANDLE SectionHandle,
4269 IN SECTION_INFORMATION_CLASS SectionInformationClass,
4270 OUT PVOID SectionInformation,
4271 IN SIZE_T SectionInformationLength,
4272 OUT PSIZE_T ResultLength OPTIONAL)
4273 {
4274 PROS_SECTION_OBJECT Section;
4275 KPROCESSOR_MODE PreviousMode;
4276 NTSTATUS Status;
4277 PAGED_CODE();
4278
4279 PreviousMode = ExGetPreviousMode();
4280
4281 Status = DefaultQueryInfoBufferCheck(SectionInformationClass,
4282 ExSectionInfoClass,
4283 sizeof(ExSectionInfoClass) / sizeof(ExSectionInfoClass[0]),
4284 SectionInformation,
4285 (ULONG)SectionInformationLength,
4286 NULL,
4287 ResultLength,
4288 PreviousMode);
4289
4290 if(!NT_SUCCESS(Status))
4291 {
4292 DPRINT1("NtQuerySection() failed, Status: 0x%x\n", Status);
4293 return Status;
4294 }
4295
4296 Status = ObReferenceObjectByHandle(SectionHandle,
4297 SECTION_QUERY,
4298 MmSectionObjectType,
4299 PreviousMode,
4300 (PVOID*)(PVOID)&Section,
4301 NULL);
4302 if (NT_SUCCESS(Status))
4303 {
4304 switch (SectionInformationClass)
4305 {
4306 case SectionBasicInformation:
4307 {
4308 PSECTION_BASIC_INFORMATION Sbi = (PSECTION_BASIC_INFORMATION)SectionInformation;
4309
4310 _SEH2_TRY
4311 {
4312 Sbi->Attributes = Section->AllocationAttributes;
4313 if (Section->AllocationAttributes & SEC_IMAGE)
4314 {
4315 Sbi->BaseAddress = 0;
4316 Sbi->Size.QuadPart = 0;
4317 }
4318 else
4319 {
4320 Sbi->BaseAddress = (PVOID)Section->Segment->Image.VirtualAddress;
4321 Sbi->Size.QuadPart = Section->Segment->Length.QuadPart;
4322 }
4323
4324 if (ResultLength != NULL)
4325 {
4326 *ResultLength = sizeof(SECTION_BASIC_INFORMATION);
4327 }
4328 Status = STATUS_SUCCESS;
4329 }
4330 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
4331 {
4332 Status = _SEH2_GetExceptionCode();
4333 }
4334 _SEH2_END;
4335
4336 break;
4337 }
4338
4339 case SectionImageInformation:
4340 {
4341 PSECTION_IMAGE_INFORMATION Sii = (PSECTION_IMAGE_INFORMATION)SectionInformation;
4342
4343 _SEH2_TRY
4344 {
4345 if (Section->AllocationAttributes & SEC_IMAGE)
4346 {
4347 PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
4348 ImageSectionObject = Section->ImageSection;
4349
4350 *Sii = ImageSectionObject->ImageInformation;
4351 }
4352
4353 if (ResultLength != NULL)
4354 {
4355 *ResultLength = sizeof(SECTION_IMAGE_INFORMATION);
4356 }
4357 Status = STATUS_SUCCESS;
4358 }
4359 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
4360 {
4361 Status = _SEH2_GetExceptionCode();
4362 }
4363 _SEH2_END;
4364
4365 break;
4366 }
4367 }
4368
4369 ObDereferenceObject(Section);
4370 }
4371
4372 return(Status);
4373 }
4374
4375 /**********************************************************************
4376 * NAME EXPORTED
4377 * MmMapViewOfSection
4378 *
4379 * DESCRIPTION
4380 * Maps a view of a section into the virtual address space of a
4381 * process.
4382 *
4383 * ARGUMENTS
4384 * Section
4385 * Pointer to the section object.
4386 *
4387 * ProcessHandle
4388 * Pointer to the process.
4389 *
4390 * BaseAddress
4391 * Desired base address (or NULL) on entry;
4392 * Actual base address of the view on exit.
4393 *
4394 * ZeroBits
4395 * Number of high order address bits that must be zero.
4396 *
4397 * CommitSize
4398 * Size in bytes of the initially committed section of
4399 * the view.
4400 *
4401 * SectionOffset
4402 * Offset in bytes from the beginning of the section
4403 * to the beginning of the view.
4404 *
4405 * ViewSize
4406 * Desired length of map (or zero to map all) on entry
4407 * Actual length mapped on exit.
4408 *
4409 * InheritDisposition
4410 * Specified how the view is to be shared with
4411 * child processes.
4412 *
4413 * AllocationType
4414 * Type of allocation for the pages.
4415 *
4416 * Protect
4417 * Protection for the committed region of the view.
4418 *
4419 * RETURN VALUE
4420 * Status.
4421 *
4422 * @implemented
4423 */
4424 NTSTATUS NTAPI
4425 MmMapViewOfSection(IN PVOID SectionObject,
4426 IN PEPROCESS Process,
4427 IN OUT PVOID *BaseAddress,
4428 IN ULONG_PTR ZeroBits,
4429 IN SIZE_T CommitSize,
4430 IN OUT PLARGE_INTEGER SectionOffset OPTIONAL,
4431 IN OUT PSIZE_T ViewSize,
4432 IN SECTION_INHERIT InheritDisposition,
4433 IN ULONG AllocationType,
4434 IN ULONG Protect)
4435 {
4436 PROS_SECTION_OBJECT Section;
4437 PMMSUPPORT AddressSpace;
4438 ULONG ViewOffset;
4439 NTSTATUS Status = STATUS_SUCCESS;
4440 BOOLEAN NotAtBase = FALSE;
4441
4442 if (MiIsRosSectionObject(SectionObject) == FALSE)
4443 {
4444 DPRINT("Mapping ARM3 section into %s\n", Process->ImageFileName);
4445 return MmMapViewOfArm3Section(SectionObject,
4446 Process,
4447 BaseAddress,
4448 ZeroBits,
4449 CommitSize,
4450 SectionOffset,
4451 ViewSize,
4452 InheritDisposition,
4453 AllocationType,
4454 Protect);
4455 }
4456
4457 ASSERT(Process);
4458
4459 if (!Protect || Protect & ~PAGE_FLAGS_VALID_FOR_SECTION)
4460 {
4461 return STATUS_INVALID_PAGE_PROTECTION;
4462 }
4463
4464
4465 Section = (PROS_SECTION_OBJECT)SectionObject;
4466 AddressSpace = &Process->Vm;
4467
4468 AllocationType |= (Section->AllocationAttributes & SEC_NO_CHANGE);
4469
4470 MmLockAddressSpace(AddressSpace);
4471
4472 if (Section->AllocationAttributes & SEC_IMAGE)
4473 {
4474 ULONG i;
4475 ULONG NrSegments;
4476 ULONG_PTR ImageBase;
4477 SIZE_T ImageSize;
4478 PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
4479 PMM_SECTION_SEGMENT SectionSegments;
4480
4481 ImageSectionObject = Section->ImageSection;
4482 SectionSegments = ImageSectionObject->Segments;
4483 NrSegments = ImageSectionObject->NrSegments;
4484
4485 ImageBase = (ULONG_PTR)*BaseAddress;
4486 if (ImageBase == 0)
4487 {
4488 ImageBase = (ULONG_PTR)ImageSectionObject->BasedAddress;
4489 }
4490
4491 ImageSize = 0;
4492 for (i = 0; i < NrSegments; i++)
4493 {
4494 if (!(SectionSegments[i].Image.Characteristics & IMAGE_SCN_TYPE_NOLOAD))
4495 {
4496 ULONG_PTR MaxExtent;
4497 MaxExtent = (ULONG_PTR)(SectionSegments[i].Image.VirtualAddress +
4498 SectionSegments[i].Length.QuadPart);
4499 ImageSize = max(ImageSize, MaxExtent);
4500 }
4501 }
4502
4503 ImageSectionObject->ImageInformation.ImageFileSize = (ULONG)ImageSize;
4504
4505 /* Check for an illegal base address */
4506 if (((ImageBase + ImageSize) > (ULONG_PTR)MmHighestUserAddress) ||
4507 ((ImageBase + ImageSize) < ImageSize))
4508 {
4509 NT_ASSERT(*BaseAddress == NULL);
4510 ImageBase = ALIGN_DOWN_BY((ULONG_PTR)MmHighestUserAddress - ImageSize,
4511 MM_VIRTMEM_GRANULARITY);
4512 NotAtBase = TRUE;
4513 }
4514 else if (ImageBase != ALIGN_DOWN_BY(ImageBase, MM_VIRTMEM_GRANULARITY))
4515 {
4516 NT_ASSERT(*BaseAddress == NULL);
4517 ImageBase = ALIGN_DOWN_BY(ImageBase, MM_VIRTMEM_GRANULARITY);
4518 NotAtBase = TRUE;
4519 }
4520
4521 /* Check there is enough space to map the section at that point. */
4522 if (MmLocateMemoryAreaByRegion(AddressSpace, (PVOID)ImageBase,
4523 PAGE_ROUND_UP(ImageSize)) != NULL)
4524 {
4525 /* Fail if the user requested a fixed base address. */
4526 if ((*BaseAddress) != NULL)
4527 {
4528 MmUnlockAddressSpace(AddressSpace);
4529 return(STATUS_CONFLICTING_ADDRESSES);
4530 }
4531 /* Otherwise find a gap to map the image. */
4532 ImageBase = (ULONG_PTR)MmFindGap(AddressSpace, PAGE_ROUND_UP(ImageSize), MM_VIRTMEM_GRANULARITY, FALSE);
4533 if (ImageBase == 0)
4534 {
4535 MmUnlockAddressSpace(AddressSpace);
4536 return(STATUS_CONFLICTING_ADDRESSES);
4537 }
4538 /* Remember that we loaded image at a different base address */
4539 NotAtBase = TRUE;
4540 }
4541
4542 for (i = 0; i < NrSegments; i++)
4543 {
4544 if (!(SectionSegments[i].Image.Characteristics & IMAGE_SCN_TYPE_NOLOAD))
4545 {
4546 PVOID SBaseAddress = (PVOID)
4547 ((char*)ImageBase + (ULONG_PTR)SectionSegments[i].Image.VirtualAddress);
4548 MmLockSectionSegment(&SectionSegments[i]);
4549 Status = MmMapViewOfSegment(AddressSpace,
4550 Section,
4551 &SectionSegments[i],
4552 &SBaseAddress,
4553 SectionSegments[i].Length.LowPart,
4554 SectionSegments[i].Protection,
4555 0,
4556 0);
4557 MmUnlockSectionSegment(&SectionSegments[i]);
4558 if (!NT_SUCCESS(Status))
4559 {
4560 MmUnlockAddressSpace(AddressSpace);
4561 return(Status);
4562 }
4563 }
4564 }
4565
4566 *BaseAddress = (PVOID)ImageBase;
4567 *ViewSize = ImageSize;
4568 }
4569 else
4570 {
4571 /* check for write access */
4572 if ((Protect & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE)) &&
4573 !(Section->SectionPageProtection & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE)))
4574 {
4575 MmUnlockAddressSpace(AddressSpace);
4576 return STATUS_SECTION_PROTECTION;
4577 }
4578 /* check for read access */
4579 if ((Protect & (PAGE_READONLY|PAGE_WRITECOPY|PAGE_EXECUTE_READ|PAGE_EXECUTE_WRITECOPY)) &&
4580 !(Section->SectionPageProtection & (PAGE_READONLY|PAGE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY)))
4581 {
4582 MmUnlockAddressSpace(AddressSpace);
4583 return STATUS_SECTION_PROTECTION;
4584 }
4585 /* check for execute access */
4586 if ((Protect & (PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY)) &&
4587 !(Section->SectionPageProtection & (PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY)))
4588 {
4589 MmUnlockAddressSpace(AddressSpace);
4590 return STATUS_SECTION_PROTECTION;
4591 }
4592
4593 if (ViewSize == NULL)
4594 {
4595 /* Following this pointer would lead to us to the dark side */
4596 /* What to do? Bugcheck? Return status? Do the mambo? */
4597 KeBugCheck(MEMORY_MANAGEMENT);
4598 }
4599
4600 if (SectionOffset == NULL)
4601 {
4602 ViewOffset = 0;
4603 }
4604 else
4605 {
4606 ViewOffset = SectionOffset->u.LowPart;
4607 }
4608
4609 if ((ViewOffset % PAGE_SIZE) != 0)
4610 {
4611 MmUnlockAddressSpace(AddressSpace);
4612 return(STATUS_MAPPED_ALIGNMENT);
4613 }
4614
4615 if ((*ViewSize) == 0)
4616 {
4617 (*ViewSize) = Section->MaximumSize.u.LowPart - ViewOffset;
4618 }
4619 else if (((*ViewSize)+ViewOffset) > Section->MaximumSize.u.LowPart)
4620 {
4621 (*ViewSize) = Section->MaximumSize.u.LowPart - ViewOffset;
4622 }
4623
4624 *ViewSize = PAGE_ROUND_UP(*ViewSize);
4625
4626 MmLockSectionSegment(Section->Segment);
4627 Status = MmMapViewOfSegment(AddressSpace,
4628 Section,
4629 Section->Segment,
4630 BaseAddress,
4631 *ViewSize,
4632 Protect,
4633 ViewOffset,
4634 AllocationType & (MEM_TOP_DOWN|SEC_NO_CHANGE));
4635 MmUnlockSectionSegment(Section->Segment);
4636 if (!NT_SUCCESS(Status))
4637 {
4638 MmUnlockAddressSpace(AddressSpace);
4639 return(Status);
4640 }
4641 }
4642
4643 MmUnlockAddressSpace(AddressSpace);
4644 NT_ASSERT(*BaseAddress == ALIGN_DOWN_POINTER_BY(*BaseAddress, MM_VIRTMEM_GRANULARITY));
4645
4646 if (NotAtBase)
4647 Status = STATUS_IMAGE_NOT_AT_BASE;
4648 else
4649 Status = STATUS_SUCCESS;
4650
4651 return Status;
4652 }
4653
4654 /*
4655 * @unimplemented
4656 */
4657 BOOLEAN NTAPI
4658 MmCanFileBeTruncated (IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
4659 IN PLARGE_INTEGER NewFileSize)
4660 {
4661 /* Check whether an ImageSectionObject exists */
4662 if (SectionObjectPointer->ImageSectionObject != NULL)
4663 {
4664 DPRINT1("ERROR: File can't be truncated because it has an image section\n");
4665 return FALSE;
4666 }
4667
4668 if (SectionObjectPointer->DataSectionObject != NULL)
4669 {
4670 PMM_SECTION_SEGMENT Segment;
4671
4672 Segment = (PMM_SECTION_SEGMENT)SectionObjectPointer->
4673 DataSectionObject;
4674
4675 if (Segment->ReferenceCount != 0)
4676 {
4677 #ifdef NEWCC
4678 CC_FILE_SIZES FileSizes;
4679 CcpLock();
4680 if (SectionObjectPointer->SharedCacheMap && (Segment->ReferenceCount > CcpCountCacheSections((PNOCC_CACHE_MAP)SectionObjectPointer->SharedCacheMap)))
4681 {
4682 CcpUnlock();
4683 /* Check size of file */
4684 if (SectionObjectPointer->SharedCacheMap)
4685 {
4686 if (!CcGetFileSizes(Segment->FileObject, &FileSizes))
4687 {
4688 return FALSE;
4689 }
4690
4691 if (NewFileSize->QuadPart <= FileSizes.FileSize.QuadPart)
4692 {
4693 return FALSE;
4694 }
4695 }
4696 }
4697 else
4698 CcpUnlock();
4699 #else
4700 /* Check size of file */
4701 if (SectionObjectPointer->SharedCacheMap)
4702 {
4703 PROS_SHARED_CACHE_MAP SharedCacheMap = SectionObjectPointer->SharedCacheMap;
4704 if (NewFileSize->QuadPart <= SharedCacheMap->FileSize.QuadPart)
4705 {
4706 return FALSE;
4707 }
4708 }
4709 #endif
4710 }
4711 else
4712 {
4713 /* Something must gone wrong
4714 * how can we have a Section but no
4715 * reference? */
4716 DPRINT("ERROR: DataSectionObject without reference!\n");
4717 }
4718 }
4719
4720 DPRINT("FIXME: didn't check for outstanding write probes\n");
4721
4722 return TRUE;
4723 }
4724
4725
4726
4727
4728 /*
4729 * @implemented
4730 */
4731 BOOLEAN NTAPI
4732 MmFlushImageSection (IN PSECTION_OBJECT_POINTERS SectionObjectPointer,
4733 IN MMFLUSH_TYPE FlushType)
4734 {
4735 BOOLEAN Result = TRUE;
4736 #ifdef NEWCC
4737 PMM_SECTION_SEGMENT Segment;
4738 #endif
4739
4740 switch(FlushType)
4741 {
4742 case MmFlushForDelete:
4743 if (SectionObjectPointer->ImageSectionObject ||
4744 SectionObjectPointer->DataSectionObject)
4745 {
4746 return FALSE;
4747 }
4748 #ifndef NEWCC
4749 CcRosRemoveIfClosed(SectionObjectPointer);
4750 #endif
4751 return TRUE;
4752 case MmFlushForWrite:
4753 {
4754 DPRINT("MmFlushImageSection(%d)\n", FlushType);
4755 #ifdef NEWCC
4756 Segment = (PMM_SECTION_SEGMENT)SectionObjectPointer->DataSectionObject;
4757 #endif
4758
4759 if (SectionObjectPointer->ImageSectionObject)
4760 {
4761 DPRINT1("SectionObject has ImageSection\n");
4762 return FALSE;
4763 }
4764
4765 #ifdef NEWCC
4766 CcpLock();
4767 Result = !SectionObjectPointer->SharedCacheMap || (Segment->ReferenceCount == CcpCountCacheSections((PNOCC_CACHE_MAP)SectionObjectPointer->SharedCacheMap));
4768 CcpUnlock();
4769 DPRINT("Result %d\n", Result);
4770 #endif
4771 return Result;
4772 }
4773 }
4774 return FALSE;
4775 }
4776
4777 /*
4778 * @implemented
4779 */
4780 NTSTATUS NTAPI
4781 MmMapViewInSystemSpace (IN PVOID SectionObject,
4782 OUT PVOID * MappedBase,
4783 IN OUT PSIZE_T ViewSize)
4784 {
4785 PROS_SECTION_OBJECT Section;
4786 PMMSUPPORT AddressSpace;
4787 NTSTATUS Status;
4788 PAGED_CODE();
4789
4790 if (MiIsRosSectionObject(SectionObject) == FALSE)
4791 {
4792 return MiMapViewInSystemSpace(SectionObject,
4793 &MmSession,
4794 MappedBase,
4795 ViewSize);
4796 }
4797
4798 DPRINT("MmMapViewInSystemSpace() called\n");
4799
4800 Section = (PROS_SECTION_OBJECT)SectionObject;
4801 AddressSpace = MmGetKernelAddressSpace();
4802
4803 MmLockAddressSpace(AddressSpace);
4804
4805
4806 if ((*ViewSize) == 0)
4807 {
4808 (*ViewSize) = Section->MaximumSize.u.LowPart;
4809 }
4810 else if ((*ViewSize) > Section->MaximumSize.u.LowPart)
4811 {
4812 (*ViewSize) = Section->MaximumSize.u.LowPart;
4813 }
4814
4815 MmLockSectionSegment(Section->Segment);
4816
4817
4818 Status = MmMapViewOfSegment(AddressSpace,
4819 Section,
4820 Section->Segment,
4821 MappedBase,
4822 *ViewSize,
4823 PAGE_READWRITE,
4824 0,
4825 0);
4826
4827 MmUnlockSectionSegment(Section->Segment);
4828 MmUnlockAddressSpace(AddressSpace);
4829
4830 return Status;
4831 }
4832
4833 NTSTATUS
4834 NTAPI
4835 MiRosUnmapViewInSystemSpace(IN PVOID MappedBase)
4836 {
4837 PMMSUPPORT AddressSpace;
4838 NTSTATUS Status;
4839
4840 DPRINT("MmUnmapViewInSystemSpace() called\n");
4841
4842 AddressSpace = MmGetKernelAddressSpace();
4843
4844 MmLockAddressSpace(AddressSpace);
4845
4846 Status = MmUnmapViewOfSegment(AddressSpace, MappedBase);
4847
4848 MmUnlockAddressSpace(AddressSpace);
4849
4850 return Status;
4851 }
4852
4853 /**********************************************************************
4854 * NAME EXPORTED
4855 * MmCreateSection@
4856 *
4857 * DESCRIPTION
4858 * Creates a section object.
4859 *
4860 * ARGUMENTS
4861 * SectionObject (OUT)
4862 * Caller supplied storage for the resulting pointer
4863 * to a SECTION_OBJECT instance;
4864 *
4865 * DesiredAccess
4866 * Specifies the desired access to the section can be a
4867 * combination of:
4868 * STANDARD_RIGHTS_REQUIRED |
4869 * SECTION_QUERY |
4870 * SECTION_MAP_WRITE |
4871 * SECTION_MAP_READ |
4872 * SECTION_MAP_EXECUTE
4873 *
4874 * ObjectAttributes [OPTIONAL]
4875 * Initialized attributes for the object can be used
4876 * to create a named section;
4877 *
4878 * MaximumSize
4879 * Maximizes the size of the memory section. Must be
4880 * non-NULL for a page-file backed section.
4881 * If value specified for a mapped file and the file is
4882 * not large enough, file will be extended.
4883 *
4884 * SectionPageProtection
4885 * Can be a combination of:
4886 * PAGE_READONLY |
4887 * PAGE_READWRITE |
4888 * PAGE_WRITEONLY |
4889 * PAGE_WRITECOPY
4890 *
4891 * AllocationAttributes
4892 * Can be a combination of:
4893 * SEC_IMAGE |
4894 * SEC_RESERVE
4895 *
4896 * FileHandle
4897 * Handle to a file to create a section mapped to a file
4898 * instead of a memory backed section;
4899 *
4900 * File
4901 * Unknown.
4902 *
4903 * RETURN VALUE
4904 * Status.
4905 *
4906 * @implemented
4907 */
4908 NTSTATUS NTAPI
4909 MmCreateSection (OUT PVOID * Section,
4910 IN ACCESS_MASK DesiredAccess,
4911 IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL,
4912 IN PLARGE_INTEGER MaximumSize,
4913 IN ULONG SectionPageProtection,
4914 IN ULONG AllocationAttributes,
4915 IN HANDLE FileHandle OPTIONAL,
4916 IN PFILE_OBJECT FileObject OPTIONAL)
4917 {
4918 NTSTATUS Status;
4919 ULONG Protection;
4920 PROS_SECTION_OBJECT *SectionObject = (PROS_SECTION_OBJECT *)Section;
4921
4922 /* Check if an ARM3 section is being created instead */
4923 if (!(AllocationAttributes & (SEC_IMAGE | SEC_PHYSICALMEMORY)))
4924 {
4925 if (!(FileObject) && !(FileHandle))
4926 {
4927 return MmCreateArm3Section(Section,
4928 DesiredAccess,
4929 ObjectAttributes,
4930 MaximumSize,
4931 SectionPageProtection,
4932 AllocationAttributes &~ 1,
4933 FileHandle,
4934 FileObject);
4935 }
4936 }
4937
4938 /* Convert section flag to page flag */
4939 if (AllocationAttributes & SEC_NOCACHE) SectionPageProtection |= PAGE_NOCACHE;
4940
4941 /* Check to make sure the protection is correct. Nt* does this already */
4942 Protection = MiMakeProtectionMask(SectionPageProtection);
4943 if (Protection == MM_INVALID_PROTECTION)
4944 {
4945 DPRINT1("Page protection is invalid\n");
4946 return STATUS_INVALID_PAGE_PROTECTION;
4947 }
4948
4949 /* Check if this is going to be a data or image backed file section */
4950 if ((FileHandle) || (FileObject))
4951 {
4952 /* These cannot be mapped with large pages */
4953 if (AllocationAttributes & SEC_LARGE_PAGES)
4954 {
4955 DPRINT1("Large pages cannot be used with an image mapping\n");
4956 return STATUS_INVALID_PARAMETER_6;
4957 }
4958
4959 /* Did the caller pass an object? */
4960 if (FileObject)
4961 {
4962 /* Reference the object directly */
4963 ObReferenceObject(FileObject);
4964 }
4965 else
4966 {
4967 /* Reference the file handle to get the object */
4968 Status = ObReferenceObjectByHandle(FileHandle,
4969 MmMakeFileAccess[Protection],
4970 IoFileObjectType,
4971 ExGetPreviousMode(),
4972 (PVOID*)&FileObject,
4973 NULL);
4974 if (!NT_SUCCESS(Status))
4975 {
4976 DPRINT1("Failed to get a handle to the FO: %lx\n", Status);
4977 return Status;
4978 }
4979 }
4980 }
4981 else
4982 {
4983 /* A handle must be supplied with SEC_IMAGE, as this is the no-handle path */
4984 if (AllocationAttributes & SEC_IMAGE) return STATUS_INVALID_FILE_FOR_SECTION;
4985 }
4986
4987 #ifndef NEWCC // A hack for initializing caching.
4988 // This is needed only in the old case.
4989 if (FileHandle)
4990 {
4991 IO_STATUS_BLOCK Iosb;
4992 NTSTATUS Status;
4993 CHAR Buffer;
4994 LARGE_INTEGER ByteOffset;
4995 ByteOffset.QuadPart = 0;
4996 Status = ZwReadFile(FileHandle,
4997 NULL,
4998 NULL,
4999 NULL,
5000 &Iosb,
5001 &Buffer,
5002 sizeof(Buffer),
5003 &ByteOffset,
5004 NULL);
5005 if (!NT_SUCCESS(Status) && Status != STATUS_END_OF_FILE)
5006 {
5007 DPRINT1("CC failure: %lx\n", Status);
5008 return Status;
5009 }
5010 // Caching is initialized...
5011 }
5012 #endif
5013
5014 if (AllocationAttributes & SEC_IMAGE)
5015 {
5016 Status = MmCreateImageSection(SectionObject,
5017 DesiredAccess,
5018 ObjectAttributes,
5019 MaximumSize,
5020 SectionPageProtection,
5021 AllocationAttributes,
5022 FileObject);
5023 }
5024 #ifndef NEWCC
5025 else if (FileHandle != NULL)
5026 {
5027 Status = MmCreateDataFileSection(SectionObject,
5028 DesiredAccess,
5029 ObjectAttributes,
5030 MaximumSize,
5031 SectionPageProtection,
5032 AllocationAttributes,
5033 FileHandle);
5034 if (FileObject)
5035 ObDereferenceObject(FileObject);
5036 }
5037 #else
5038 else if (FileHandle != NULL || FileObject != NULL)
5039 {
5040 Status = MmCreateCacheSection(SectionObject,
5041 DesiredAccess,
5042 ObjectAttributes,
5043 MaximumSize,
5044 SectionPageProtection,
5045 AllocationAttributes,
5046 FileObject);
5047 }
5048 #endif
5049 else
5050 {
5051 if ((AllocationAttributes & SEC_PHYSICALMEMORY) == 0)
5052 {
5053 DPRINT1("Invalid path: %lx %p %p\n", AllocationAttributes, FileObject, FileHandle);
5054 }
5055 // ASSERT(AllocationAttributes & SEC_PHYSICALMEMORY);
5056 Status = MmCreatePageFileSection(SectionObject,
5057 DesiredAccess,
5058 ObjectAttributes,
5059 MaximumSize,
5060 SectionPageProtection,
5061 AllocationAttributes);
5062 }
5063
5064 return Status;
5065 }
5066
5067 /* EOF */