ReactOS 0.4.15-dev-8102-g108db8f
section.c File Reference
#include <ntoskrnl.h>
#include <cache/newcc.h>
#include <cache/section/newmm.h>
#include <debug.h>
#include <reactos/exeformat.h>
#include "ARM3/miarm.h"
Include dependency graph for section.c:

Go to the source code of this file.

Classes

struct  MM_SECTION_PAGEOUT_CONTEXT
 

Macros

#define NDEBUG
 
#define MmSetPageEntrySectionSegment(S, O, E)
 
#define DIE(ARGS_)   { DPRINT ARGS_; goto l_Return; }
 

Functions

VOID NTAPI _MmLockSectionSegment (PMM_SECTION_SEGMENT Segment, const char *file, int line)
 
VOID NTAPI _MmUnlockSectionSegment (PMM_SECTION_SEGMENT Segment, const char *file, int line)
 
static PMM_SECTION_SEGMENT MiGrabDataSection (PSECTION_OBJECT_POINTERS SectionObjectPointer)
 
PMM_IMAGE_SECTION_OBJECT ImageSectionObjectFromSegment (PMM_SECTION_SEGMENT Segment)
 
NTSTATUS MiMapViewInSystemSpace (IN PVOID Section, IN PVOID Session, OUT PVOID *MappedBase, IN OUT PSIZE_T ViewSize, IN PLARGE_INTEGER SectionOffset)
 
NTSTATUS NTAPI MmCreateArm3Section (OUT PVOID *SectionObject, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN PLARGE_INTEGER InputMaximumSize, IN ULONG SectionPageProtection, IN ULONG AllocationAttributes, IN HANDLE FileHandle OPTIONAL, IN PFILE_OBJECT FileObject OPTIONAL)
 
NTSTATUS NTAPI MmMapViewOfArm3Section (IN PVOID SectionObject, IN PEPROCESS Process, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
 
 C_ASSERT (EXEFMT_LOAD_HEADER_SIZE >=sizeof(IMAGE_DOS_HEADER))
 
 C_ASSERT (sizeof(IMAGE_NT_HEADERS32)<=sizeof(IMAGE_NT_HEADERS64))
 
 C_ASSERT (TYPE_ALIGNMENT(IMAGE_NT_HEADERS32)==TYPE_ALIGNMENT(IMAGE_NT_HEADERS64))
 
 C_ASSERT (RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS32, FileHeader)==RTL_SIZEOF_THROUGH_FIELD(IMAGE_NT_HEADERS64, FileHeader))
 
 C_ASSERT (FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader)==FIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader))
 
 C_ASSERT (PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Magic))
 
 C_ASSERT (PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SectionAlignment))
 
 C_ASSERT (PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, FileAlignment))
 
 C_ASSERT (PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, Subsystem))
 
 C_ASSERT (PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MinorSubsystemVersion))
 
 C_ASSERT (PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MajorSubsystemVersion))
 
 C_ASSERT (PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, AddressOfEntryPoint))
 
 C_ASSERT (PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfCode))
 
 C_ASSERT (PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, SizeOfHeaders))
 
NTSTATUS NTAPI MiWritePage (PMM_SECTION_SEGMENT Segment, LONGLONG SegOffset, PFN_NUMBER Page)
 
NTSTATUS NTAPI PeFmtCreateSection (IN CONST VOID *FileHeader, IN SIZE_T FileHeaderSize, IN PVOID File, OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject, OUT PULONG Flags, IN PEXEFMT_CB_READ_FILE ReadFileCb, IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb)
 
NTSTATUS MmspWaitForFileLock (PFILE_OBJECT File)
 
VOID NTAPI MmpFreePageFileSegment (PMM_SECTION_SEGMENT Segment)
 
static VOID NTAPI FreeSegmentPage (PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset)
 
 _When_ (OldIrql==MM_NOIRQL, _IRQL_requires_max_(DISPATCH_LEVEL))
 
VOID NTAPI MmSharePageEntrySectionSegment (PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset)
 
BOOLEAN NTAPI MmUnsharePageEntrySectionSegment (PMEMORY_AREA MemoryArea, PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset, BOOLEAN Dirty, BOOLEAN PageOut, ULONG_PTR *InEntry)
 
static NTSTATUS MiCopyFromUserPage (PFN_NUMBER DestPage, const VOID *SrcAddress)
 
static NTSTATUS NTAPI MmMakeSegmentResident (_In_ PMM_SECTION_SEGMENT Segment, _In_ LONGLONG Offset, _In_ ULONG Length, _In_opt_ PLARGE_INTEGER ValidDataLength, _In_ BOOLEAN SetDirty)
 
static VOID MmAlterViewAttributes (PMMSUPPORT AddressSpace, PVOID BaseAddress, SIZE_T RegionSize, ULONG OldType, ULONG OldProtect, ULONG NewType, ULONG NewProtect)
 
NTSTATUS NTAPI MmNotPresentFaultSectionView (PMMSUPPORT AddressSpace, MEMORY_AREA *MemoryArea, PVOID Address, BOOLEAN Locked)
 
NTSTATUS NTAPI MmAccessFaultSectionView (PMMSUPPORT AddressSpace, MEMORY_AREA *MemoryArea, PVOID Address, BOOLEAN Locked)
 
NTSTATUS NTAPI MmProtectSectionView (PMMSUPPORT AddressSpace, PMEMORY_AREA MemoryArea, PVOID BaseAddress, SIZE_T Length, ULONG Protect, PULONG OldProtect)
 
NTSTATUS NTAPI MmQuerySectionView (PMEMORY_AREA MemoryArea, PVOID Address, PMEMORY_BASIC_INFORMATION Info, PSIZE_T ResultLength)
 
VOID NTAPI MmpDeleteSection (PVOID ObjectBody)
 
VOID NTAPI MmpCloseSection (IN PEPROCESS Process OPTIONAL, IN PVOID Object, IN ACCESS_MASK GrantedAccess, IN ULONG ProcessHandleCount, IN ULONG SystemHandleCount)
 
NTSTATUS NTAPI MmCreatePhysicalMemorySection (VOID)
 
NTSTATUS NTAPI MmInitSectionImplementation (VOID)
 
static NTSTATUS NTAPI MmCreateDataFileSection (PSECTION *SectionObject, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PLARGE_INTEGER UMaximumSize, ULONG SectionPageProtection, ULONG AllocationAttributes, PFILE_OBJECT FileObject, BOOLEAN GotFileHandle)
 
NTSTATUS NTAPI ElfFmtCreateSection (IN CONST VOID *FileHeader, IN SIZE_T FileHeaderSize, IN PVOID File, OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject, OUT PULONG Flags, IN PEXEFMT_CB_READ_FILE ReadFileCb, IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb)
 
static PMM_SECTION_SEGMENT NTAPI ExeFmtpAllocateSegments (IN ULONG NrSegments)
 
static NTSTATUS NTAPI ExeFmtpReadFile (IN PVOID File, IN PLARGE_INTEGER Offset, IN ULONG Length, OUT PVOID *Data, OUT PVOID *AllocBase, OUT PULONG ReadSize)
 
static VOID NTAPI MmspAssertSegmentsSorted (IN PMM_IMAGE_SECTION_OBJECT ImageSectionObject)
 
static VOID NTAPI MmspAssertSegmentsNoOverlap (IN PMM_IMAGE_SECTION_OBJECT ImageSectionObject)
 
static VOID NTAPI MmspAssertSegmentsPageAligned (IN PMM_IMAGE_SECTION_OBJECT ImageSectionObject)
 
static int __cdecl MmspCompareSegments (const void *x, const void *y)
 
static VOID NTAPI MmspSortSegments (IN OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject, IN ULONG Flags)
 
static BOOLEAN NTAPI MmspCheckSegmentBounds (IN OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject, IN ULONG Flags)
 
static BOOLEAN NTAPI MmspPageAlignSegments (IN OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject, IN ULONG Flags)
 
NTSTATUS ExeFmtpCreateImageSection (PFILE_OBJECT FileObject, PMM_IMAGE_SECTION_OBJECT ImageSectionObject)
 
NTSTATUS MmCreateImageSection (PSECTION *SectionObject, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PLARGE_INTEGER UMaximumSize, ULONG SectionPageProtection, ULONG AllocationAttributes, PFILE_OBJECT FileObject)
 
static NTSTATUS MmMapViewOfSegment (PMMSUPPORT AddressSpace, BOOLEAN AsImage, PMM_SECTION_SEGMENT Segment, PVOID *BaseAddress, SIZE_T ViewSize, ULONG Protect, LONGLONG ViewOffset, ULONG AllocationType)
 
static VOID MmFreeSectionPage (PVOID Context, MEMORY_AREA *MemoryArea, PVOID Address, PFN_NUMBER Page, SWAPENTRY SwapEntry, BOOLEAN Dirty)
 
static NTSTATUS MmUnmapViewOfSegment (PMMSUPPORT AddressSpace, PVOID BaseAddress)
 
NTSTATUS NTAPI MiRosUnmapViewOfSection (IN PEPROCESS Process, IN PVOID BaseAddress, IN BOOLEAN SkipDebuggerNotify)
 
NTSTATUS NTAPI NtQuerySection (_In_ HANDLE SectionHandle, _In_ SECTION_INFORMATION_CLASS SectionInformationClass, _Out_ PVOID SectionInformation, _In_ SIZE_T SectionInformationLength, _Out_opt_ PSIZE_T ResultLength)
 
NTSTATUS NTAPI MmMapViewOfSection (IN PVOID SectionObject, IN PEPROCESS Process, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
 
BOOLEAN NTAPI MmCanFileBeTruncated (_In_ PSECTION_OBJECT_POINTERS SectionObjectPointer, _In_opt_ PLARGE_INTEGER NewFileSize)
 
static BOOLEAN MiPurgeImageSegment (PMM_SECTION_SEGMENT Segment)
 
BOOLEAN NTAPI MmFlushImageSection (IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN MMFLUSH_TYPE FlushType)
 
NTSTATUS NTAPI MmMapViewInSystemSpace (IN PVOID SectionObject, OUT PVOID *MappedBase, IN OUT PSIZE_T ViewSize)
 
NTSTATUS NTAPI MmMapViewInSystemSpaceEx (_In_ PVOID SectionObject, _Outptr_result_bytebuffer_(*ViewSize) PVOID *MappedBase, _Inout_ PSIZE_T ViewSize, _Inout_ PLARGE_INTEGER SectionOffset, _In_ ULONG_PTR Flags)
 
NTSTATUS NTAPI MiRosUnmapViewInSystemSpace (IN PVOID MappedBase)
 
NTSTATUS NTAPI MmCreateSection (OUT PVOID *Section, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN PLARGE_INTEGER MaximumSize, IN ULONG SectionPageProtection, IN ULONG AllocationAttributes, IN HANDLE FileHandle OPTIONAL, IN PFILE_OBJECT FileObject OPTIONAL)
 
BOOLEAN NTAPI MmArePagesResident (_In_ PEPROCESS Process, _In_ PVOID Address, _In_ ULONG Length)
 
BOOLEAN NTAPI MmPurgeSegment (_In_ PSECTION_OBJECT_POINTERS SectionObjectPointer, _In_opt_ PLARGE_INTEGER Offset, _In_ ULONG Length)
 
NTSTATUS NTAPI MmMakeDataSectionResident (_In_ PSECTION_OBJECT_POINTERS SectionObjectPointer, _In_ LONGLONG Offset, _In_ ULONG Length, _In_ PLARGE_INTEGER ValidDataLength)
 
NTSTATUS NTAPI MmFlushSegment (_In_ PSECTION_OBJECT_POINTERS SectionObjectPointer, _In_opt_ PLARGE_INTEGER Offset, _In_ ULONG Length, _Out_opt_ PIO_STATUS_BLOCK Iosb)
 
 _Requires_exclusive_lock_held_ (Segment->Lock) BOOLEAN NTAPI MmCheckDirtySegment(PMM_SECTION_SEGMENT Segment
 
 ASSERT (Segment->Locked)
 
 ASSERT ((Offset->QuadPart % PAGE_SIZE)==0)
 
 DPRINT ("Checking segment for file %wZ at offset 0x%I64X.\n", &Segment->FileObject->FileName, Offset->QuadPart)
 
 if (Entry==0)
 
 if (!IS_DIRTY_SSE(Entry) &&(SHARE_COUNT_FROM_SSE(Entry)==0) &&PageOut)
 
NTSTATUS NTAPI MmMakePagesDirty (_In_ PEPROCESS Process, _In_ PVOID Address, _In_ ULONG Length)
 
NTSTATUS NTAPI MmExtendSection (_In_ PVOID _Section, _Inout_ PLARGE_INTEGER NewSize)
 

Variables

MMSESSION MmSession
 
static LARGE_INTEGER TinyTime = {{-1L, -1L}}
 
KEVENT MmWaitPageEvent
 
POBJECT_TYPE MmSectionObjectType = NULL
 
ULONG_PTR MmSubsectionBase
 
static ULONG SectionCharacteristicsToProtect [16]
 
ACCESS_MASK MmMakeFileAccess [8]
 
static GENERIC_MAPPING MmpSectionMapping
 
static PEXEFMT_LOADER ExeFmtpLoaders []
 
PLARGE_INTEGER Offset
 
PLARGE_INTEGER BOOLEAN ForceDirty
 
PLARGE_INTEGER BOOLEAN BOOLEAN PageOut
 
NTSTATUS Status
 
PFN_NUMBER Page
 
 Entry = MmGetPageEntrySectionSegment(Segment, Offset)
 
return FALSE
 

Macro Definition Documentation

◆ DIE

#define DIE (   ARGS_)    { DPRINT ARGS_; goto l_Return; }

◆ MmSetPageEntrySectionSegment

#define MmSetPageEntrySectionSegment (   S,
  O,
  E 
)
Value:
do { \
DPRINT("SetPageEntrySectionSegment(old,%p,%x,%x)\n",(S),(O)->LowPart,E); \
_MmSetPageEntrySectionSegment((S),(O),(E),__FILE__,__LINE__); \
} while (0)
static const WCHAR E[]
Definition: oid.c:1253
static const WCHAR O[]
Definition: oid.c:1251
Definition: movable.cpp:9

Definition at line 56 of file section.c.

◆ NDEBUG

#define NDEBUG

Definition at line 49 of file section.c.

Function Documentation

◆ _MmLockSectionSegment()

VOID NTAPI _MmLockSectionSegment ( PMM_SECTION_SEGMENT  Segment,
const char file,
int  line 
)

Definition at line 70 of file section.c.

71{
72 //DPRINT("MmLockSectionSegment(%p,%s:%d)\n", Segment, file, line);
74 Segment->Locked = TRUE;
75}
#define TRUE
Definition: types.h:120
VOID FASTCALL ExAcquireFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:23
_Inout_ PVOID Segment
Definition: exfuncs.h:1101

◆ _MmUnlockSectionSegment()

VOID NTAPI _MmUnlockSectionSegment ( PMM_SECTION_SEGMENT  Segment,
const char file,
int  line 
)

Definition at line 79 of file section.c.

80{
81 ASSERT(Segment->Locked);
82 Segment->Locked = FALSE;
84 //DPRINT("MmUnlockSectionSegment(%p,%s:%d)\n", Segment, file, line);
85}
#define FALSE
Definition: types.h:117
VOID FASTCALL ExReleaseFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:31
#define ASSERT(a)
Definition: mode.c:44

◆ _Requires_exclusive_lock_held_()

_Requires_exclusive_lock_held_ ( Segment->  Lock)

◆ _When_()

Definition at line 988 of file section.c.

998{
999 /* Lock the PFN lock because we mess around with SectionObjectPointers */
1000 if (OldIrql == MM_NOIRQL)
1001 {
1002 OldIrql = MiAcquirePfnLock();
1003 }
1004
1005 if (InterlockedDecrement64(Segment->ReferenceCount) > 0)
1006 {
1007 /* Nothing to do yet */
1008 MiReleasePfnLock(OldIrql);
1009 return;
1010 }
1011
1012 *Segment->Flags |= MM_SEGMENT_INDELETE;
1013
1014 /* Flush the segment */
1015 if (*Segment->Flags & MM_DATAFILE_SEGMENT)
1016 {
1017 MiReleasePfnLock(OldIrql);
1018 /* Free the page table. This will flush any remaining dirty data */
1020
1021 OldIrql = MiAcquirePfnLock();
1022 /* Delete the pointer on the file */
1023 ASSERT(Segment->FileObject->SectionObjectPointer->DataSectionObject == Segment);
1024 Segment->FileObject->SectionObjectPointer->DataSectionObject = NULL;
1025 MiReleasePfnLock(OldIrql);
1026 ObDereferenceObject(Segment->FileObject);
1027
1029 }
1030 else
1031 {
1032 /* Most grotesque thing ever */
1033 PMM_IMAGE_SECTION_OBJECT ImageSectionObject = CONTAINING_RECORD(Segment->ReferenceCount, MM_IMAGE_SECTION_OBJECT, RefCount);
1034 PMM_SECTION_SEGMENT SectionSegments;
1035 ULONG NrSegments;
1036 ULONG i;
1037
1038 /* Delete the pointer on the file */
1039 ASSERT(ImageSectionObject->FileObject->SectionObjectPointer->ImageSectionObject == ImageSectionObject);
1040 ImageSectionObject->FileObject->SectionObjectPointer->ImageSectionObject = NULL;
1041 MiReleasePfnLock(OldIrql);
1042
1043 ObDereferenceObject(ImageSectionObject->FileObject);
1044
1045 NrSegments = ImageSectionObject->NrSegments;
1046 SectionSegments = ImageSectionObject->Segments;
1047 for (i = 0; i < NrSegments; i++)
1048 {
1049 if (SectionSegments[i].Image.Characteristics & IMAGE_SCN_MEM_SHARED)
1050 {
1051 MmpFreePageFileSegment(&SectionSegments[i]);
1052 }
1053
1054 MmFreePageTablesSectionSegment(&SectionSegments[i], NULL);
1055 }
1056
1058 ExFreePoolWithTag(ImageSectionObject, TAG_MM_SECTION_SEGMENT);
1059 }
1060}
#define NULL
Definition: types.h:112
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define InterlockedDecrement64
Definition: interlocked.h:144
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
VOID NTAPI MmFreePageTablesSectionSegment(PMM_SECTION_SEGMENT Segment, FREE_SECTION_PAGE_FUN FreePage)
Definition: sptab.c:321
#define IMAGE_SCN_MEM_SHARED
Definition: ntimage.h:238
#define MM_NOIRQL
Definition: mm.h:70
#define MM_SEGMENT_INDELETE
Definition: mm.h:239
#define MM_DATAFILE_SEGMENT
Definition: mm.h:238
static VOID NTAPI FreeSegmentPage(PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset)
Definition: section.c:960
VOID NTAPI MmpFreePageFileSegment(PMM_SECTION_SEGMENT Segment)
Definition: section.c:917
PMM_SECTION_SEGMENT Segments
Definition: mm.h:234
PFILE_OBJECT FileObject
Definition: mm.h:226
#define TAG_MM_SECTION_SEGMENT
Definition: tag.h:114
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778
#define ObDereferenceObject
Definition: obfuncs.h:203

◆ ASSERT() [1/2]

ASSERT ( (Offset->QuadPart % PAGE_SIZE = =0)

◆ ASSERT() [2/2]

ASSERT ( Segment->  Locked)

◆ C_ASSERT() [1/14]

C_ASSERT ( EXEFMT_LOAD_HEADER_SIZE >=  sizeofIMAGE_DOS_HEADER)

◆ C_ASSERT() [2/14]

C_ASSERT ( FIELD_OFFSET(IMAGE_NT_HEADERS32, OptionalHeader)  = =FIELD_OFFSET(IMAGE_NT_HEADERS64, OptionalHeader))

◆ C_ASSERT() [3/14]

◆ C_ASSERT() [4/14]

◆ C_ASSERT() [5/14]

◆ C_ASSERT() [6/14]

C_ASSERT ( PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MajorSubsystemVersion)  )

◆ C_ASSERT() [7/14]

C_ASSERT ( PEFMT_FIELDS_EQUAL(IMAGE_OPTIONAL_HEADER32, IMAGE_OPTIONAL_HEADER64, MinorSubsystemVersion)  )

◆ C_ASSERT() [8/14]

◆ C_ASSERT() [9/14]

◆ C_ASSERT() [10/14]

◆ C_ASSERT() [11/14]

◆ C_ASSERT() [12/14]

◆ C_ASSERT() [13/14]

◆ C_ASSERT() [14/14]

◆ DPRINT()

DPRINT ( "Checking segment for file %wZ at offset 0x%I64X.\n"  ,
&Segment->FileObject->  FileName,
Offset->  QuadPart 
)

◆ ElfFmtCreateSection()

NTSTATUS NTAPI ElfFmtCreateSection ( IN CONST VOID FileHeader,
IN SIZE_T  FileHeaderSize,
IN PVOID  File,
OUT PMM_IMAGE_SECTION_OBJECT  ImageSectionObject,
OUT PULONG  Flags,
IN PEXEFMT_CB_READ_FILE  ReadFileCb,
IN PEXEFMT_CB_ALLOCATE_SEGMENTS  AllocateSegmentsCb 
)

◆ ExeFmtpAllocateSegments()

static PMM_SECTION_SEGMENT NTAPI ExeFmtpAllocateSegments ( IN ULONG  NrSegments)
static

Definition at line 2590 of file section.c.

2591{
2592 SIZE_T SizeOfSegments;
2593 PMM_SECTION_SEGMENT Segments;
2594
2595 /* TODO: check for integer overflow */
2596 SizeOfSegments = sizeof(MM_SECTION_SEGMENT) * NrSegments;
2597
2599 SizeOfSegments,
2601
2602 if(Segments)
2603 RtlZeroMemory(Segments, SizeOfSegments);
2604
2605 return Segments;
2606}
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define NonPagedPool
Definition: env_spec_w32.h:307
struct _MM_SECTION_SEGMENT MM_SECTION_SEGMENT
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262

Referenced by ExeFmtpCreateImageSection().

◆ ExeFmtpCreateImageSection()

NTSTATUS ExeFmtpCreateImageSection ( PFILE_OBJECT  FileObject,
PMM_IMAGE_SECTION_OBJECT  ImageSectionObject 
)

Definition at line 3024 of file section.c.

3026{
3028 PVOID FileHeader;
3029 PVOID FileHeaderBuffer;
3030 ULONG FileHeaderSize;
3031 ULONG Flags;
3032 ULONG OldNrSegments;
3034 ULONG i;
3035
3036 /*
3037 * Read the beginning of the file (2 pages). Should be enough to contain
3038 * all (or most) of the headers
3039 */
3040 Offset.QuadPart = 0;
3041
3043 &Offset,
3044 PAGE_SIZE * 2,
3045 &FileHeader,
3046 &FileHeaderBuffer,
3047 &FileHeaderSize);
3048
3049 if (!NT_SUCCESS(Status))
3050 return Status;
3051
3052 if (FileHeaderSize == 0)
3053 {
3054 ExFreePool(FileHeaderBuffer);
3055 return STATUS_UNSUCCESSFUL;
3056 }
3057
3058 /*
3059 * Look for a loader that can handle this executable
3060 */
3061 for (i = 0; i < RTL_NUMBER_OF(ExeFmtpLoaders); ++ i)
3062 {
3063 Flags = 0;
3064
3065 Status = ExeFmtpLoaders[i](FileHeader,
3066 FileHeaderSize,
3067 FileObject,
3068 ImageSectionObject,
3069 &Flags,
3072
3073 if (!NT_SUCCESS(Status))
3074 {
3075 if (ImageSectionObject->Segments)
3076 {
3077 ExFreePool(ImageSectionObject->Segments);
3078 ImageSectionObject->Segments = NULL;
3079 }
3080 }
3081
3083 break;
3084 }
3085
3086 ExFreePoolWithTag(FileHeaderBuffer, 'rXmM');
3087
3088 /*
3089 * No loader handled the format
3090 */
3092 {
3095 }
3096
3097 if (!NT_SUCCESS(Status))
3098 return Status;
3099
3100 ASSERT(ImageSectionObject->Segments != NULL);
3101 ASSERT(ImageSectionObject->RefCount > 0);
3102
3103 /*
3104 * Some defaults
3105 */
3106 /* FIXME? are these values platform-dependent? */
3107 if (ImageSectionObject->ImageInformation.MaximumStackSize == 0)
3108 ImageSectionObject->ImageInformation.MaximumStackSize = 0x40000;
3109
3110 if(ImageSectionObject->ImageInformation.CommittedStackSize == 0)
3111 ImageSectionObject->ImageInformation.CommittedStackSize = 0x1000;
3112
3113 if(ImageSectionObject->BasedAddress == NULL)
3114 {
3115 if(ImageSectionObject->ImageInformation.ImageCharacteristics & IMAGE_FILE_DLL)
3116 ImageSectionObject->BasedAddress = (PVOID)0x10000000;
3117 else
3118 ImageSectionObject->BasedAddress = (PVOID)0x00400000;
3119 }
3120
3121 /*
3122 * And now the fun part: fixing the segments
3123 */
3124
3125 /* Sort them by virtual address */
3126 MmspSortSegments(ImageSectionObject, Flags);
3127
3128 /* Ensure they don't overlap in memory */
3129 if (!MmspCheckSegmentBounds(ImageSectionObject, Flags))
3131
3132 /* Ensure they are aligned */
3133 OldNrSegments = ImageSectionObject->NrSegments;
3134
3135 if (!MmspPageAlignSegments(ImageSectionObject, Flags))
3137
3138 /* Trim them if the alignment phase merged some of them */
3139 if (ImageSectionObject->NrSegments < OldNrSegments)
3140 {
3141 PMM_SECTION_SEGMENT Segments;
3142 SIZE_T SizeOfSegments;
3143
3144 SizeOfSegments = sizeof(MM_SECTION_SEGMENT) * ImageSectionObject->NrSegments;
3145
3147 SizeOfSegments,
3149
3150 if (Segments == NULL)
3152
3153 RtlCopyMemory(Segments, ImageSectionObject->Segments, SizeOfSegments);
3154 ExFreePool(ImageSectionObject->Segments);
3155 ImageSectionObject->Segments = Segments;
3156 }
3157
3158 /* And finish their initialization */
3159 for ( i = 0; i < ImageSectionObject->NrSegments; ++ i )
3160 {
3161 ExInitializeFastMutex(&ImageSectionObject->Segments[i].Lock);
3162 ImageSectionObject->Segments[i].ReferenceCount = &ImageSectionObject->RefCount;
3163 ImageSectionObject->Segments[i].Flags = &ImageSectionObject->SegFlags;
3164 MiInitializeSectionPageTable(&ImageSectionObject->Segments[i]);
3165 ImageSectionObject->Segments[i].FileObject = FileObject;
3166 }
3167
3168 ASSERT(ImageSectionObject->RefCount > 0);
3169
3170 ImageSectionObject->FileObject = FileObject;
3171
3173 return Status;
3174}
#define RTL_NUMBER_OF(x)
Definition: RtlRegistry.c:12
LONG NTSTATUS
Definition: precomp.h:26
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define ExFreePool(addr)
Definition: env_spec_w32.h:352
#define PagedPool
Definition: env_spec_w32.h:308
#define STATUS_ROS_EXEFMT_UNKNOWN_FORMAT
Definition: exeformat.h:72
Status
Definition: gdiplustypes.h:25
VOID NTAPI MiInitializeSectionPageTable(PMM_SECTION_SEGMENT Segment)
Definition: sptab.c:165
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
#define STATUS_INVALID_IMAGE_FORMAT
Definition: ntstatus.h:359
#define STATUS_INVALID_IMAGE_NOT_MZ
Definition: ntstatus.h:539
#define IMAGE_FILE_DLL
Definition: pedump.c:169
static PMM_SECTION_SEGMENT NTAPI ExeFmtpAllocateSegments(IN ULONG NrSegments)
Definition: section.c:2590
static BOOLEAN NTAPI MmspPageAlignSegments(IN OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject, IN ULONG Flags)
Definition: section.c:2841
static BOOLEAN NTAPI MmspCheckSegmentBounds(IN OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject, IN ULONG Flags)
Definition: section.c:2790
static VOID NTAPI MmspSortSegments(IN OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject, IN ULONG Flags)
Definition: section.c:2764
static NTSTATUS NTAPI ExeFmtpReadFile(IN PVOID File, IN PLARGE_INTEGER Offset, IN ULONG Length, OUT PVOID *Data, OUT PVOID *AllocBase, OUT PULONG ReadSize)
Definition: section.c:2610
static PEXEFMT_LOADER ExeFmtpLoaders[]
Definition: section.c:2579
PLARGE_INTEGER Offset
Definition: section.c:5028
void * PVOID
Definition: typedefs.h:50
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ WDFREQUEST _In_ WDFFILEOBJECT FileObject
Definition: wdfdevice.h:550
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
FORCEINLINE VOID ExInitializeFastMutex(_Out_ PFAST_MUTEX FastMutex)
Definition: exfuncs.h:274

Referenced by MmCreateImageSection().

◆ ExeFmtpReadFile()

static NTSTATUS NTAPI ExeFmtpReadFile ( IN PVOID  File,
IN PLARGE_INTEGER  Offset,
IN ULONG  Length,
OUT PVOID Data,
OUT PVOID AllocBase,
OUT PULONG  ReadSize 
)
static

Definition at line 2610 of file section.c.

2616{
2619 ULONG AdjustOffset;
2620 ULONG OffsetAdjustment;
2622 ULONG UsedSize;
2623 PVOID Buffer;
2626
2628
2629 if(Length == 0)
2630 {
2631 KeBugCheck(MEMORY_MANAGEMENT);
2632 }
2633
2634 FileOffset = *Offset;
2635
2636 /* Negative/special offset: it cannot be used in this context */
2637 if(FileOffset.u.HighPart < 0)
2638 {
2639 KeBugCheck(MEMORY_MANAGEMENT);
2640 }
2641
2642 AdjustOffset = PAGE_ROUND_DOWN(FileOffset.u.LowPart);
2643 OffsetAdjustment = FileOffset.u.LowPart - AdjustOffset;
2644 FileOffset.u.LowPart = AdjustOffset;
2645
2646 BufferSize = Length + OffsetAdjustment;
2648
2649 /*
2650 * It's ok to use paged pool, because this is a temporary buffer only used in
2651 * the loading of executables. The assumption is that MmCreateSection is
2652 * always called at low IRQLs and that these buffers don't survive a brief
2653 * initialization phase
2654 */
2656 if (!Buffer)
2657 {
2659 }
2660
2662
2663 UsedSize = (ULONG)Iosb.Information;
2664
2665 if(NT_SUCCESS(Status) && UsedSize < OffsetAdjustment)
2666 {
2669 }
2670
2671 if(NT_SUCCESS(Status))
2672 {
2673 *Data = (PVOID)((ULONG_PTR)Buffer + OffsetAdjustment);
2674 *AllocBase = Buffer;
2675 *ReadSize = UsedSize - OffsetAdjustment;
2676 }
2677 else
2678 {
2679 ExFreePoolWithTag(Buffer, 'rXmM');
2680 }
2681
2682 return Status;
2683}
DECLSPEC_NORETURN VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1430
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:160
Definition: bufpool.h:45
Definition: File.h:16
#define BufferSize
Definition: mmc.h:75
return Iosb
Definition: create.c:4402
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
#define PAGE_ROUND_UP(x)
Definition: mmtypes.h:38
#define PAGE_ROUND_DOWN(x)
Definition: mmtypes.h:36
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
NTSTATUS NTAPI MiSimpleRead(PFILE_OBJECT FileObject, PLARGE_INTEGER FileOffset, PVOID Buffer, ULONG Length, BOOLEAN Paging, PIO_STATUS_BLOCK ReadStatus)
Definition: io.c:109
#define STATUS_IN_PAGE_ERROR
Definition: ntstatus.h:243
#define ASSERT_IRQL_LESS(x)
Definition: debug.h:253
uint32_t ULONG_PTR
Definition: typedefs.h:65
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
Definition: wdfmemory.h:254
* PFILE_OBJECT
Definition: iotypes.h:1998

Referenced by ExeFmtpCreateImageSection().

◆ FreeSegmentPage()

static VOID NTAPI FreeSegmentPage ( PMM_SECTION_SEGMENT  Segment,
PLARGE_INTEGER  Offset 
)
static

Definition at line 960 of file section.c.

961{
964
966
968
970
971 /* This must be either a valid entry or nothing */
973
974 /* There should be no reference anymore */
976
978 /* If there is a page, this must be because it's still dirty */
979 ASSERT(Page != 0);
980
981 /* Write the page */
982 if (IS_DIRTY_SSE(Entry))
983 MiWritePage(Segment, Offset->QuadPart, Page);
984
986}
#define MmGetPageEntrySectionSegment(S, O)
Definition: mm.h:1602
#define MmLockSectionSegment(x)
Definition: mm.h:1397
#define SHARE_COUNT_FROM_SSE(E)
Definition: mm.h:1385
#define MC_USER
Definition: mm.h:114
#define MmUnlockSectionSegment(x)
Definition: mm.h:1405
#define IS_DIRTY_SSE(E)
Definition: mm.h:1377
#define PFN_FROM_SSE(E)
Definition: mm.h:1368
#define IS_SWAP_FROM_SSE(E)
Definition: mm.h:1369
NTSTATUS NTAPI MmReleasePageMemoryConsumer(ULONG Consumer, PFN_NUMBER Page)
Definition: balance.c:72
ULONG PFN_NUMBER
Definition: ke.h:9
PFN_NUMBER Page
Definition: section.c:5034
NTSTATUS NTAPI MiWritePage(PMM_SECTION_SEGMENT Segment, LONGLONG SegOffset, PFN_NUMBER Page)
Definition: section.c:238
base of all file and directory entries
Definition: entries.h:83

Referenced by _When_().

◆ if() [1/2]

if ( !IS_DIRTY_SSE(Entry) &&(SHARE_COUNT_FROM_SSE(Entry)==0) &&  PageOut)

Definition at line 5179 of file section.c.

5180 {
5181 ULONG_PTR NewEntry = 0;
5182 /* Restore the swap entry here */
5183 if (!FlagOn(*Segment->Flags, MM_DATAFILE_SEGMENT))
5184 {
5186 if (SwapEntry)
5187 NewEntry = MAKE_SWAP_SSE(SwapEntry);
5188 }
5189
5190 /* Yes. Release it */
5193 /* Tell the caller we released the page */
5194 return TRUE;
5195 }
#define FlagOn(_F, _SF)
Definition: ext2fs.h:179
#define MAKE_SWAP_SSE(S)
Definition: mm.h:1374
ULONG_PTR SWAPENTRY
Definition: mm.h:57
SWAPENTRY NTAPI MmGetSavedSwapEntryPage(PFN_NUMBER Page)
Definition: freelist.c:499
#define MmSetPageEntrySectionSegment(S, O, E)
Definition: section.c:56

◆ if() [2/2]

if ( Entry  = = 0)

Definition at line 5043 of file section.c.

5048 {
5049 BOOLEAN DirtyAgain;
5050
5051 /*
5052 * We got a dirty entry. This path is for the shared data,
5053 * be-it regular file maps or shared sections of DLLs
5054 */
5056 FlagOn(Segment->Image.Characteristics, IMAGE_SCN_MEM_SHARED));
5057
5058 /* Insert the cleaned entry back. Mark it as write in progress, and clear the dirty bit. */
5062
5064
5065 if (FlagOn(*Segment->Flags, MM_DATAFILE_SEGMENT))
5066 {
5068 KIRQL OldIrql;
5069
5070 /* We have to write it back to the file. Tell the FS driver who we are */
5071 if (PageOut)
5072 {
5073 LARGE_INTEGER EndOffset = *Offset;
5074
5076
5077 /* We need to disable all APCs */
5079
5080 EndOffset.QuadPart += PAGE_SIZE;
5082 &EndOffset,
5084 if (NT_SUCCESS(Status))
5085 {
5087 }
5088 else
5089 {
5090 /* Make sure we will not try to release anything */
5092 }
5093 }
5094 else
5095 {
5096 /* We don't have to lock. Say this is success */
5098 }
5099
5100 /* Go ahead and write the page, if previous locking succeeded */
5101 if (NT_SUCCESS(Status))
5102 {
5103 DPRINT("Writing page at offset %I64d for file %wZ, Pageout: %s\n",
5104 Offset->QuadPart, &Segment->FileObject->FileName, PageOut ? "TRUE" : "FALSE");
5105 Status = MiWritePage(Segment, Offset->QuadPart, Page);
5106 }
5107
5108 if (PageOut)
5109 {
5111 if (ResourceToRelease != NULL)
5112 {
5114 }
5116 }
5117 }
5118 else
5119 {
5120 /* This must only be called by the page-out path */
5121 ASSERT(PageOut);
5122
5123 /* And this must be for a shared section in a DLL */
5124 ASSERT(FlagOn(Segment->Image.Characteristics, IMAGE_SCN_MEM_SHARED));
5125
5127 if (!SwapEntry)
5128 {
5129 SwapEntry = MmAllocSwapPage();
5130 }
5131
5132 if (SwapEntry)
5133 {
5134 Status = MmWriteToSwapPage(SwapEntry, Page);
5135 if (NT_SUCCESS(Status))
5136 {
5137 MmSetSavedSwapEntryPage(Page, SwapEntry);
5138 }
5139 else
5140 {
5141 MmFreeSwapPage(SwapEntry);
5142 }
5143 }
5144 else
5145 {
5146 DPRINT1("Failed to allocate a swap page!\n");
5148 }
5149 }
5150
5152
5153 /* Get the entry again */
5156
5157 if (!NT_SUCCESS(Status))
5158 {
5159 /* Damn, this failed. Consider this page as still dirty */
5160 DPRINT1("MiWritePage FAILED: Status 0x%08x!\n", Status);
5161 DirtyAgain = TRUE;
5162 }
5163 else
5164 {
5165 /* Check if someone dirtified this page while we were not looking */
5166 DirtyAgain = IS_DIRTY_SSE(Entry);
5167 }
5168
5169 /* Drop the reference we got, deleting the write altogether. */
5171 if (DirtyAgain)
5172 {
5174 }
5176 }
unsigned char BOOLEAN
#define DPRINT1
Definition: precomp.h:8
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeRaiseIrql(irql, oldIrql)
Definition: env_spec_w32.h:597
#define APC_LEVEL
Definition: env_spec_w32.h:695
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
#define PAGE_SHIFT
Definition: env_spec_w32.h:45
ERESOURCE * PERESOURCE
Definition: env_spec_w32.h:595
#define FSRTL_MOD_WRITE_TOP_LEVEL_IRP
Definition: fsrtltypes.h:61
_Check_return_ NTSTATUS NTAPI FsRtlAcquireFileForModWriteEx(_In_ PFILE_OBJECT FileObject, _In_ PLARGE_INTEGER EndingOffset, _Outptr_result_maybenull_ PERESOURCE *ResourceToRelease)
Lock a file object before flushing pages to disk. To be called by the Modified Page Writer (MPW)
Definition: fastio.c:1852
VOID NTAPI FsRtlReleaseFileForModWrite(_In_ PFILE_OBJECT FileObject, _In_ PERESOURCE ResourceToRelease)
Unlock a file object after flushing pages to disk. To be called by the Modified Page Writer (MPW) aft...
Definition: fastio.c:1959
#define DIRTY_SSE(E)
Definition: mm.h:1375
#define MAKE_SSE(P, C)
Definition: mm.h:1387
SWAPENTRY NTAPI MmAllocSwapPage(VOID)
Definition: pagefile.c:322
NTSTATUS NTAPI MmWriteToSwapPage(SWAPENTRY SwapEntry, PFN_NUMBER Page)
Definition: pagefile.c:147
#define PAGE_FROM_SSE(E)
Definition: mm.h:1383
VOID NTAPI MmFreeSwapPage(SWAPENTRY Entry)
Definition: pagefile.c:291
VOID NTAPI MmSetSavedSwapEntryPage(PFN_NUMBER Page, SWAPENTRY SavedSwapEntry)
Definition: freelist.c:483
#define WRITE_SSE(E)
Definition: mm.h:1378
PIRP NTAPI IoGetTopLevelIrp(VOID)
Definition: irp.c:1843
VOID NTAPI IoSetTopLevelIrp(IN PIRP Irp)
Definition: irp.c:2000
PLARGE_INTEGER BOOLEAN BOOLEAN PageOut
Definition: section.c:5031
#define STATUS_SUCCESS
Definition: shellext.h:65
#define DPRINT
Definition: sndvol32.h:73
LONGLONG QuadPart
Definition: typedefs.h:114
_In_ PLARGE_INTEGER _Out_ struct _ERESOURCE ** ResourceToRelease
Definition: iotypes.h:1598

◆ ImageSectionObjectFromSegment()

PMM_IMAGE_SECTION_OBJECT ImageSectionObjectFromSegment ( PMM_SECTION_SEGMENT  Segment)

Definition at line 120 of file section.c.

121{
122 ASSERT((Segment->SegFlags & MM_DATAFILE_SEGMENT) == 0);
123
124 return CONTAINING_RECORD(Segment->ReferenceCount, MM_IMAGE_SECTION_OBJECT, RefCount);
125}

Referenced by MiRosUnmapViewOfSection().

◆ MiCopyFromUserPage()

static NTSTATUS MiCopyFromUserPage ( PFN_NUMBER  DestPage,
const VOID SrcAddress 
)
static

Definition at line 1170 of file section.c.

1171{
1173 KIRQL Irql;
1174 PVOID DestAddress;
1175
1177 DestAddress = MiMapPageInHyperSpace(Process, DestPage, &Irql);
1178 if (DestAddress == NULL)
1179 {
1180 return STATUS_NO_MEMORY;
1181 }
1182 ASSERT((ULONG_PTR)DestAddress % PAGE_SIZE == 0);
1183 ASSERT((ULONG_PTR)SrcAddress % PAGE_SIZE == 0);
1184 RtlCopyMemory(DestAddress, SrcAddress, PAGE_SIZE);
1185 MiUnmapPageInHyperSpace(Process, DestAddress, Irql);
1186 return STATUS_SUCCESS;
1187}
_Out_ PKIRQL Irql
Definition: csq.h:179
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
VOID NTAPI MiUnmapPageInHyperSpace(IN PEPROCESS Process, IN PVOID Address, IN KIRQL OldIrql)
Definition: hypermap.c:91
PVOID NTAPI MiMapPageInHyperSpace(IN PEPROCESS Process, IN PFN_NUMBER Page, IN PKIRQL OldIrql)
Definition: hypermap.c:28
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define PsGetCurrentProcess
Definition: psfuncs.h:17

Referenced by MmAccessFaultSectionView().

◆ MiGrabDataSection()

static PMM_SECTION_SEGMENT MiGrabDataSection ( PSECTION_OBJECT_POINTERS  SectionObjectPointer)
static

Definition at line 90 of file section.c.

91{
92 KIRQL OldIrql = MiAcquirePfnLock();
94
95 while (TRUE)
96 {
97 Segment = SectionObjectPointer->DataSectionObject;
98 if (!Segment)
99 break;
100
102 {
103 MiReleasePfnLock(OldIrql);
105 OldIrql = MiAcquirePfnLock();
106 continue;
107 }
108
111 break;
112 }
113
114 MiReleasePfnLock(OldIrql);
115
116 return Segment;
117}
#define InterlockedIncrement64(a)
Definition: btrfs_drv.h:143
#define KeDelayExecutionThread(mode, foo, t)
Definition: env_spec_w32.h:484
_Must_inspect_result_ _In_ PDEVICE_OBJECT _In_ PSECTION_OBJECT_POINTERS SectionObjectPointer
Definition: fsrtlfuncs.h:1369
#define KernelMode
Definition: asm.h:34
#define MM_SEGMENT_INCREATE
Definition: mm.h:240
static LARGE_INTEGER TinyTime
Definition: section.c:63

Referenced by MmCanFileBeTruncated(), MmFlushSegment(), MmMakeDataSectionResident(), and MmPurgeSegment().

◆ MiMapViewInSystemSpace()

NTSTATUS MiMapViewInSystemSpace ( IN PVOID  Section,
IN PVOID  Session,
OUT PVOID MappedBase,
IN OUT PSIZE_T  ViewSize,
IN PLARGE_INTEGER  SectionOffset 
)

◆ MiPurgeImageSegment()

static BOOLEAN MiPurgeImageSegment ( PMM_SECTION_SEGMENT  Segment)
static

Definition at line 4310 of file section.c.

4311{
4313
4315
4316 /* Loop over all entries */
4317 for (PageTable = RtlEnumerateGenericTable(&Segment->PageTable, TRUE);
4318 PageTable != NULL;
4320 {
4321 for (ULONG i = 0; i < _countof(PageTable->PageEntries); i++)
4322 {
4323 ULONG_PTR Entry = PageTable->PageEntries[i];
4325
4326 if (!Entry)
4327 continue;
4328
4330 {
4331 /* I/O ongoing or swap entry. Someone mapped this file as we were not looking */
4333 return FALSE;
4334 }
4335
4336 /* Regular entry */
4339
4340 /* Properly remove using the used API */
4341 Offset.QuadPart = PageTable->FileOffset.QuadPart + (i << PAGE_SHIFT);
4344 }
4345 }
4346
4348
4349 return TRUE;
4350}
#define IS_WRITE_SSE(E)
Definition: mm.h:1379
#define _countof(array)
Definition: sndvol32.h:70
static PMEM_HOOK PageTable[TOTAL_PAGES]
Definition: memory.c:43
_Must_inspect_result_ NTSYSAPI PVOID NTAPI RtlEnumerateGenericTable(_In_ PRTL_GENERIC_TABLE Table, _In_ BOOLEAN Restart)

Referenced by MmFlushImageSection().

◆ MiRosUnmapViewInSystemSpace()

NTSTATUS NTAPI MiRosUnmapViewInSystemSpace ( IN PVOID  MappedBase)

Definition at line 4557 of file section.c.

4558{
4559 DPRINT("MmUnmapViewInSystemSpace() called\n");
4560
4562}
FORCEINLINE PMMSUPPORT MmGetKernelAddressSpace(VOID)
Definition: mm.h:1726
static NTSTATUS MmUnmapViewOfSegment(PMMSUPPORT AddressSpace, PVOID BaseAddress)
Definition: section.c:3525
_Must_inspect_result_ _Outptr_result_bytebuffer_ ViewSize PVOID * MappedBase
Definition: mmfuncs.h:492

Referenced by MmUnmapViewInSystemSpace().

◆ MiRosUnmapViewOfSection()

NTSTATUS NTAPI MiRosUnmapViewOfSection ( IN PEPROCESS  Process,
IN PVOID  BaseAddress,
IN BOOLEAN  SkipDebuggerNotify 
)

Definition at line 3589 of file section.c.

3592{
3596 PVOID ImageBaseAddress = 0;
3597
3598 DPRINT("Opening memory area Process %p BaseAddress %p\n",
3600
3601 ASSERT(Process);
3602
3603 AddressSpace = &Process->Vm;
3604
3606 BaseAddress);
3607 if (MemoryArea == NULL ||
3608#ifdef NEWCC
3609 ((MemoryArea->Type != MEMORY_AREA_SECTION_VIEW) && (MemoryArea->Type != MEMORY_AREA_CACHE)) ||
3610#else
3612#endif
3614
3615 {
3617
3618 DPRINT1("Unable to find memory area at address %p.\n", BaseAddress);
3620 }
3621
3623 {
3624 ULONG i;
3625 ULONG NrSegments;
3626 PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
3627 PMM_SECTION_SEGMENT SectionSegments;
3629
3630 Segment = MemoryArea->SectionData.Segment;
3631 ImageSectionObject = ImageSectionObjectFromSegment(Segment);
3632 SectionSegments = ImageSectionObject->Segments;
3633 NrSegments = ImageSectionObject->NrSegments;
3634
3636
3637 /* Search for the current segment within the section segments
3638 * and calculate the image base address */
3639 for (i = 0; i < NrSegments; i++)
3640 {
3641 if (Segment == &SectionSegments[i])
3642 {
3643 ImageBaseAddress = (char*)BaseAddress - (ULONG_PTR)SectionSegments[i].Image.VirtualAddress;
3644 break;
3645 }
3646 }
3647 if (i >= NrSegments)
3648 {
3649 KeBugCheck(MEMORY_MANAGEMENT);
3650 }
3651
3652 for (i = 0; i < NrSegments; i++)
3653 {
3654 PVOID SBaseAddress = (PVOID)
3655 ((char*)ImageBaseAddress + (ULONG_PTR)SectionSegments[i].Image.VirtualAddress);
3656
3657 Status = MmUnmapViewOfSegment(AddressSpace, SBaseAddress);
3658 if (!NT_SUCCESS(Status))
3659 {
3660 DPRINT1("MmUnmapViewOfSegment failed for %p (Process %p) with %lx\n",
3661 SBaseAddress, Process, Status);
3663 }
3664 }
3665 DPRINT("One mapping less for %p\n", ImageSectionObject->FileObject->SectionObjectPointer);
3666 InterlockedDecrement(&ImageSectionObject->MapCount);
3667 }
3668 else
3669 {
3671 PMMVAD Vad = &MemoryArea->VadNode;
3672 PCONTROL_AREA ControlArea = Vad->ControlArea;
3675 LARGE_INTEGER ViewOffset;
3676 ViewOffset.QuadPart = MemoryArea->SectionData.ViewOffset;
3677
3678 InterlockedIncrement64(Segment->ReferenceCount);
3679
3680 ViewSize = PAGE_SIZE + ((Vad->EndingVpn - Vad->StartingVpn) << PAGE_SHIFT);
3681
3683 if (!NT_SUCCESS(Status))
3684 {
3685 DPRINT1("MmUnmapViewOfSegment failed for %p (Process %p) with %lx\n",
3688 }
3689
3690 /* These might be deleted now */
3691 Vad = NULL;
3692 MemoryArea = NULL;
3693
3695 {
3696 /* Don't bother */
3697 MmDereferenceSegment(Segment);
3698 return STATUS_SUCCESS;
3699 }
3701
3702 FileObject = Segment->FileObject;
3704
3705 /* Don't bother for auto-delete closed file. */
3707 {
3709 MmDereferenceSegment(Segment);
3710 return STATUS_SUCCESS;
3711 }
3712
3713 /*
3714 * Flush only when last mapping is deleted.
3715 * FIXME: Why ControlArea == NULL? Or rather: is ControlArea ever not NULL here?
3716 */
3717 if (ControlArea == NULL || ControlArea->NumberOfMappedViews == 1)
3718 {
3719 while (ViewSize > 0)
3720 {
3721 ULONG FlushSize = min(ViewSize, PAGE_ROUND_DOWN(MAXULONG));
3722 MmFlushSegment(FileObject->SectionObjectPointer,
3723 &ViewOffset,
3724 FlushSize,
3725 NULL);
3726 ViewSize -= FlushSize;
3727 ViewOffset.QuadPart += FlushSize;
3728 }
3729 }
3730
3732 MmDereferenceSegment(Segment);
3733 }
3734
3735 /* Notify debugger */
3736 if (ImageBaseAddress && !SkipDebuggerNotify) DbgkUnMapViewOfSection(ImageBaseAddress);
3737
3738 return STATUS_SUCCESS;
3739}
#define InterlockedDecrement
Definition: armddk.h:52
VOID NTAPI DbgkUnMapViewOfSection(IN PVOID BaseAddress)
Definition: dbgkutil.c:436
#define ULONG_PTR
Definition: config.h:101
#define min(a, b)
Definition: monoChain.cc:55
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:408
@ VadImageMap
Definition: mmtypes.h:206
_In_ PMEMORY_AREA MemoryArea
Definition: newmm.h:207
VOID NTAPI FsRtlReleaseFile(IN PFILE_OBJECT FileObject)
Definition: fastio.c:1659
VOID NTAPI FsRtlAcquireFileExclusive(IN PFILE_OBJECT FileObject)
Definition: fastio.c:1634
PMEMORY_AREA NTAPI MmLocateMemoryAreaByAddress(PMMSUPPORT AddressSpace, PVOID Address)
Definition: marea.c:60
#define MEMORY_AREA_OWNED_BY_ARM3
Definition: mm.h:97
#define MEMORY_AREA_SECTION_VIEW
Definition: mm.h:93
#define MM_PHYSICALMEMORY_SEGMENT
Definition: mm.h:237
#define STATUS_NOT_MAPPED_VIEW
Definition: ntstatus.h:262
PMM_IMAGE_SECTION_OBJECT ImageSectionObjectFromSegment(PMM_SECTION_SEGMENT Segment)
Definition: section.c:120
NTSTATUS NTAPI MmFlushSegment(_In_ PSECTION_OBJECT_POINTERS SectionObjectPointer, _In_opt_ PLARGE_INTEGER Offset, _In_ ULONG Length, _Out_opt_ PIO_STATUS_BLOCK Iosb)
Definition: section.c:4940
struct _MEMORY_AREA::@1796 SectionData
BOOLEAN DeleteInProgress
Definition: mm.h:253
ULONG Type
Definition: mm.h:251
MMVAD VadNode
Definition: mm.h:249
ULONG_PTR VadType
Definition: mmtypes.h:694
ULONG_PTR EndingVpn
Definition: mmtypes.h:730
PCONTROL_AREA ControlArea
Definition: mmtypes.h:736
ULONG_PTR StartingVpn
Definition: mmtypes.h:729
MMVAD_FLAGS VadFlags
Definition: mmtypes.h:734
union _MMVAD::@2615 u
struct _MM_SECTION_SEGMENT::@1795 Image
else
Definition: tritemp.h:161
#define MAXULONG
Definition: typedefs.h:251
_In_ ULONG _In_ PHYSICAL_ADDRESS _Inout_ PULONG AddressSpace
Definition: iofuncs.h:2274
#define FO_CLEANUP_COMPLETE
Definition: iotypes.h:1790
#define FO_DELETE_ON_CLOSE
Definition: iotypes.h:1792

Referenced by MiRosCleanupMemoryArea(), and MiUnmapViewOfSection().

◆ MiWritePage()

NTSTATUS NTAPI MiWritePage ( PMM_SECTION_SEGMENT  Segment,
LONGLONG  SegOffset,
PFN_NUMBER  Page 
)

Definition at line 238 of file section.c.

248{
252 UCHAR MdlBase[sizeof(MDL) + sizeof(PFN_NUMBER)];
253 PMDL Mdl = (PMDL)MdlBase;
254 PFILE_OBJECT FileObject = Segment->FileObject;
256
257 FileOffset.QuadPart = Segment->Image.FileOffset + SegOffset;
258
259 RtlZeroMemory(MdlBase, sizeof(MdlBase));
262 Mdl->MdlFlags |= MDL_PAGES_LOCKED;
263
266 if (Status == STATUS_PENDING)
267 {
269 Status = IoStatus.Status;
270 }
271 if (Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA)
272 {
273 MmUnmapLockedPages (Mdl->MappedSystemVa, Mdl);
274 }
275
276 return Status;
277}
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
NTSTATUS NTAPI IoSynchronousPageWrite(IN PFILE_OBJECT FileObject, IN PMDL Mdl, IN PLARGE_INTEGER Offset, IN PKEVENT Event, IN PIO_STATUS_BLOCK StatusBlock)
Definition: iofunc.c:1146
VOID NTAPI MmUnmapLockedPages(IN PVOID BaseAddress, IN PMDL Mdl)
Definition: mdlsup.c:837
__in UCHAR __in POWER_STATE __in_opt PVOID __in PIO_STATUS_BLOCK IoStatus
Definition: mxum.h:159
@ NotificationEvent
#define STATUS_PENDING
Definition: ntstatus.h:82
VOID NTAPI MmBuildMdlFromPages(PMDL Mdl, PPFN_NUMBER Pages)
Definition: pagefile.c:111
PVOID PMDL
Definition: usb.h:39
_In_ WDFDEVICE _In_ PVOID _In_opt_ PMDL Mdl
@ Executive
Definition: ketypes.h:415
#define MmInitializeMdl(_MemoryDescriptorList, _BaseVa, _Length)
MDL
Definition: mmtypes.h:117
#define MDL_PAGES_LOCKED
Definition: mmtypes.h:19
#define MDL_MAPPED_TO_SYSTEM_VA
Definition: mmtypes.h:18
unsigned char UCHAR
Definition: xmlstorage.h:181

Referenced by FreeSegmentPage().

◆ MmAccessFaultSectionView()

NTSTATUS NTAPI MmAccessFaultSectionView ( PMMSUPPORT  AddressSpace,
MEMORY_AREA MemoryArea,
PVOID  Address,
BOOLEAN  Locked 
)

Definition at line 1910 of file section.c.

1914{
1916 PFN_NUMBER OldPage;
1917 PFN_NUMBER NewPage;
1918 PFN_NUMBER UnmappedPage;
1919 PVOID PAddress;
1924 BOOLEAN Cow = FALSE;
1926 BOOLEAN Unmapped;
1928
1929 DPRINT("MmAccessFaultSectionView(%p, %p, %p)\n", AddressSpace, MemoryArea, Address);
1930
1931 /* Get the region for this address */
1933 &MemoryArea->SectionData.RegionListHead,
1934 Address, NULL);
1935 ASSERT(Region != NULL);
1936 if (!(Region->Protect & PAGE_IS_WRITABLE))
1938
1939 /* Make sure we have a page mapping for this address. */
1941 {
1943 if (!NT_SUCCESS(Status))
1944 {
1945 /* This is invalid access ! */
1946 return Status;
1947 }
1948 }
1949
1950 /*
1951 * Check if the page has already been set readwrite
1952 */
1954 {
1955 DPRINT("Address 0x%p\n", Address);
1956 return STATUS_SUCCESS;
1957 }
1958
1959 /* Check if we are doing Copy-On-Write */
1960 Segment = MemoryArea->SectionData.Segment;
1961 Cow = Segment->WriteCopy || (Region->Protect & PAGE_IS_WRITECOPY);
1962
1963 if (!Cow)
1964 {
1965 /* Simply update page protection and we're done */
1967 return STATUS_SUCCESS;
1968 }
1969
1970 /* Calculate the new protection & check if we should update the region */
1971 NewProtect = Region->Protect;
1973 {
1974 NewProtect &= ~PAGE_IS_WRITECOPY;
1975 if (Region->Protect & PAGE_IS_EXECUTABLE)
1977 else
1980 &MemoryArea->SectionData.RegionListHead,
1983 }
1984
1985 /*
1986 * Find the offset of the page
1987 */
1988 PAddress = MM_ROUND_DOWN(Address, PAGE_SIZE);
1989 Offset.QuadPart = (ULONG_PTR)PAddress - MA_GetStartingAddress(MemoryArea)
1990 + MemoryArea->SectionData.ViewOffset;
1991
1992 /* Get the page mapping this section offset. */
1995
1996 /* Get the current page mapping for the process */
1997 ASSERT(MmIsPagePresent(Process, PAddress));
1998 OldPage = MmGetPfnForProcess(Process, PAddress);
1999 ASSERT(OldPage != 0);
2000
2001 if (IS_SWAP_FROM_SSE(Entry) ||
2002 PFN_FROM_SSE(Entry) != OldPage)
2003 {
2005 /* This is a private page. We must only change the page protection. */
2007 return STATUS_SUCCESS;
2008 }
2009
2010 /*
2011 * Allocate a page
2012 */
2014 if (!NT_SUCCESS(Status))
2015 {
2017 return STATUS_NO_MEMORY;
2018 }
2019
2020 /*
2021 * Copy the old page
2022 */
2023 NT_VERIFY(NT_SUCCESS(MiCopyFromUserPage(NewPage, PAddress)));
2024
2025 /*
2026 * Unshare the old page.
2027 */
2028 DPRINT("Swapping page (Old %x New %x)\n", OldPage, NewPage);
2029 Unmapped = MmDeleteVirtualMapping(Process, PAddress, NULL, &UnmappedPage);
2030 if (!Unmapped || (UnmappedPage != OldPage))
2031 {
2032 /* Uh , we had a page just before, but suddenly it changes. Someone corrupted us. */
2033 KeBugCheckEx(MEMORY_MANAGEMENT,
2035 (ULONG_PTR)PAddress,
2036 (ULONG_PTR)__FILE__,
2037 __LINE__);
2038 }
2039
2040 if (Process)
2041 MmDeleteRmap(OldPage, Process, PAddress);
2044
2045 /*
2046 * Set the PTE to point to the new page
2047 */
2048 if (!NT_SUCCESS(MmCreateVirtualMapping(Process, PAddress, NewProtect, NewPage)))
2049 {
2050 DPRINT1("MmCreateVirtualMapping failed, unable to create virtual mapping, not out of memory\n");
2051 KeBugCheck(MEMORY_MANAGEMENT);
2052 }
2053
2054 if (Process)
2055 MmInsertRmap(NewPage, Process, PAddress);
2056
2057 DPRINT("Address 0x%p\n", Address);
2058 return STATUS_SUCCESS;
2059}
_In_ PMEMORY_AREA _In_ PVOID _In_ BOOLEAN Locked
Definition: newmm.h:209
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
VOID NTAPI MmSetPageProtect(struct _EPROCESS *Process, PVOID Address, ULONG flProtect)
ULONG NTAPI MmGetPageProtect(struct _EPROCESS *Process, PVOID Address)
PMM_REGION NTAPI MmFindRegion(PVOID BaseAddress, PLIST_ENTRY RegionListHead, PVOID Address, PVOID *RegionBaseAddress)
Definition: region.c:257
PFN_NUMBER NTAPI MmGetPfnForProcess(struct _EPROCESS *Process, PVOID Address)
#define MA_GetStartingAddress(_MemoryArea)
Definition: mm.h:244
#define PAGE_IS_WRITABLE
Definition: mm.h:153
FORCEINLINE PEPROCESS MmGetAddressSpaceOwner(IN PMMSUPPORT AddressSpace)
Definition: mm.h:1711
BOOLEAN NTAPI MmIsPagePresent(struct _EPROCESS *Process, PVOID Address)
NTSTATUS NTAPI MmAlterRegion(PMMSUPPORT AddressSpace, PVOID BaseAddress, PLIST_ENTRY RegionListHead, PVOID StartAddress, SIZE_T Length, ULONG NewType, ULONG NewProtect, PMM_ALTER_REGION_FUNC AlterFunc)
Definition: region.c:108
#define PAGE_IS_EXECUTABLE
Definition: mm.h:159
NTSTATUS NTAPI MmRequestPageMemoryConsumer(ULONG Consumer, BOOLEAN MyWait, PPFN_NUMBER AllocatedPage)
Definition: balance.c:313
NTSTATUS NTAPI MmCreateVirtualMapping(struct _EPROCESS *Process, PVOID Address, ULONG flProtect, PFN_NUMBER Page)
VOID NTAPI MmDeleteRmap(PFN_NUMBER Page, struct _EPROCESS *Process, PVOID Address)
#define MM_ROUND_DOWN(x, s)
Definition: mm.h:131
VOID NTAPI MmInsertRmap(PFN_NUMBER Page, struct _EPROCESS *Process, PVOID Address)
#define PAGE_IS_WRITECOPY
Definition: mm.h:165
VOID NTAPI MmDeleteVirtualMapping(IN PEPROCESS Process, IN PVOID Address, OUT PBOOLEAN WasDirty, OUT PPFN_NUMBER Page)
Definition: page.c:177
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
static WCHAR Address[46]
Definition: ping.c:68
VOID NTAPI KeBugCheckEx(_In_ ULONG BugCheckCode, _In_ ULONG_PTR BugCheckParameter1, _In_ ULONG_PTR BugCheckParameter2, _In_ ULONG_PTR BugCheckParameter3, _In_ ULONG_PTR BugCheckParameter4)
Definition: rtlcompat.c:108
NTSTATUS NTAPI MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace, MEMORY_AREA *MemoryArea, PVOID Address, BOOLEAN Locked)
Definition: section.c:1532
BOOLEAN NTAPI MmUnsharePageEntrySectionSegment(PMEMORY_AREA MemoryArea, PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset, BOOLEAN Dirty, BOOLEAN PageOut, ULONG_PTR *InEntry)
Definition: section.c:1089
static NTSTATUS MiCopyFromUserPage(PFN_NUMBER DestPage, const VOID *SrcAddress)
Definition: section.c:1170
static VOID MmAlterViewAttributes(PMMSUPPORT AddressSpace, PVOID BaseAddress, SIZE_T RegionSize, ULONG OldType, ULONG OldProtect, ULONG NewType, ULONG NewProtect)
Definition: section.c:1446
Definition: mm.h:463
_Must_inspect_result_ _In_ ULONG NewProtect
Definition: mmfuncs.h:682
#define NT_VERIFY(exp)
Definition: rtlfuncs.h:3287

Referenced by MmpAccessFault().

◆ MmAlterViewAttributes()

static VOID MmAlterViewAttributes ( PMMSUPPORT  AddressSpace,
PVOID  BaseAddress,
SIZE_T  RegionSize,
ULONG  OldType,
ULONG  OldProtect,
ULONG  NewType,
ULONG  NewProtect 
)
static

Definition at line 1446 of file section.c.

1453{
1456 BOOLEAN DoCOW = FALSE;
1457 ULONG i;
1459
1462 Segment = MemoryArea->SectionData.Segment;
1464
1465 if ((Segment->WriteCopy) &&
1467 {
1468 DoCOW = TRUE;
1469 }
1470
1471 if (OldProtect != NewProtect)
1472 {
1473 for (i = 0; i < PAGE_ROUND_UP(RegionSize) / PAGE_SIZE; i++)
1474 {
1475 SWAPENTRY SwapEntry;
1476 PVOID Address = (char*)BaseAddress + (i * PAGE_SIZE);
1478
1479 /* Wait for a wait entry to disappear */
1480 do
1481 {
1482 MmGetPageFileMapping(Process, Address, &SwapEntry);
1483 if (SwapEntry != MM_WAIT_ENTRY)
1484 break;
1490 }
1491 while (TRUE);
1492
1493 /*
1494 * If we doing COW for this segment then check if the page is
1495 * already private.
1496 */
1498 {
1502
1504 + MemoryArea->SectionData.ViewOffset;
1506 /*
1507 * An MM_WAIT_ENTRY is ok in this case... It'll just count as
1508 * IS_SWAP_FROM_SSE and we'll do the right thing.
1509 */
1511
1514 {
1516 }
1517 }
1518
1520 {
1522 Protect);
1523 }
1524 }
1525 }
1526
1528}
#define PAGE_READONLY
Definition: compat.h:138
__kernel_entry _Inout_ _Inout_ PSIZE_T RegionSize
Definition: mmfuncs.h:172
FORCEINLINE VOID MmLockAddressSpace(PMMSUPPORT AddressSpace)
Definition: mm.h:1691
VOID NTAPI MmGetPageFileMapping(PEPROCESS Process, PVOID Address, SWAPENTRY *SwapEntry)
Definition: page.c:299
BOOLEAN NTAPI MmIsDisabledPage(struct _EPROCESS *Process, PVOID Address)
FORCEINLINE VOID MmUnlockAddressSpace(PMMSUPPORT AddressSpace)
Definition: mm.h:1704
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG _In_ ULONG Protect
Definition: zwfuncs.h:221

Referenced by MmAccessFaultSectionView(), MmNotPresentFaultSectionView(), and MmProtectSectionView().

◆ MmArePagesResident()

BOOLEAN NTAPI MmArePagesResident ( _In_ PEPROCESS  Process,
_In_ PVOID  Address,
_In_ ULONG  Length 
)

Definition at line 4775 of file section.c.

4779{
4781 BOOLEAN Ret = TRUE;
4783 LARGE_INTEGER SegmentOffset, RangeEnd;
4785
4787
4789 if (MemoryArea == NULL)
4790 {
4792 return FALSE;
4793 }
4794
4795 /* Only supported in old Mm for now */
4797 /* For file mappings */
4799
4800 Segment = MemoryArea->SectionData.Segment;
4802
4804 + MemoryArea->SectionData.ViewOffset;
4806 + MemoryArea->SectionData.ViewOffset;
4807
4808 while (SegmentOffset.QuadPart < RangeEnd.QuadPart)
4809 {
4811 if ((Entry == 0) || IS_SWAP_FROM_SSE(Entry))
4812 {
4813 Ret = FALSE;
4814 break;
4815 }
4816 SegmentOffset.QuadPart += PAGE_SIZE;
4817 }
4818
4820
4822 return Ret;
4823}

Referenced by CcRosEnsureVacbResident().

◆ MmCanFileBeTruncated()

BOOLEAN NTAPI MmCanFileBeTruncated ( _In_ PSECTION_OBJECT_POINTERS  SectionObjectPointer,
_In_opt_ PLARGE_INTEGER  NewFileSize 
)

Definition at line 4255 of file section.c.

4258{
4259 BOOLEAN Ret;
4261
4262 /* Check whether an ImageSectionObject exists */
4263 if (SectionObjectPointer->ImageSectionObject != NULL)
4264 {
4265 DPRINT1("ERROR: File can't be truncated because it has an image section\n");
4266 return FALSE;
4267 }
4268
4270 if (!Segment)
4271 {
4272 /* There is no data section. It's fine to do anything. */
4273 return TRUE;
4274 }
4275
4277 if ((Segment->SectionCount == 0) ||
4278 ((Segment->SectionCount == 1) && (SectionObjectPointer->SharedCacheMap != NULL)))
4279 {
4280 /* If the cache is the only one holding a reference to the segment, then it's fine to resize */
4281 Ret = TRUE;
4282 }
4283 else if (NewFileSize != NULL)
4284 {
4285 /* We can't shrink, but we can extend */
4286 Ret = NewFileSize->QuadPart >= Segment->RawLength.QuadPart;
4287#if DBG
4288 if (!Ret)
4289 {
4290 DPRINT1("Cannot truncate data: New Size %I64d, Segment Size %I64d\n", NewFileSize->QuadPart, Segment->RawLength.QuadPart);
4291 }
4292#endif
4293 }
4294 else
4295 {
4296 DPRINT1("ERROR: File can't be truncated because it has references held to its data section\n");
4297 Ret = FALSE;
4298 }
4299
4301 MmDereferenceSegment(Segment);
4302
4303 DPRINT("FIXME: didn't check for outstanding write probes\n");
4304
4305 return Ret;
4306}
static PMM_SECTION_SEGMENT MiGrabDataSection(PSECTION_OBJECT_POINTERS SectionObjectPointer)
Definition: section.c:90
_In_opt_ PLARGE_INTEGER NewFileSize
Definition: mmfuncs.h:608

Referenced by Ext2SetFileInformation(), Ext2SupersedeOrOverWriteFile(), NtfsSetEndOfFile(), open_file2(), RxCommonSetInformation(), set_end_of_file_information(), SetAttributeDataLength(), UDFCommonCreate(), UDFSetAllocationInformation(), UDFSetEOF(), VfatSetAllocationSizeInformation(), and VfatSetInformation().

◆ MmCreateArm3Section()

NTSTATUS NTAPI MmCreateArm3Section ( OUT PVOID SectionObject,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes  OPTIONAL,
IN PLARGE_INTEGER  InputMaximumSize,
IN ULONG  SectionPageProtection,
IN ULONG  AllocationAttributes,
IN HANDLE FileHandle  OPTIONAL,
IN PFILE_OBJECT FileObject  OPTIONAL 
)

Definition at line 2468 of file section.c.

2476{
2477 SECTION Section;
2478 PSECTION NewSection;
2479 PSUBSECTION Subsection;
2480 PSEGMENT NewSegment, Segment;
2482 PCONTROL_AREA ControlArea;
2483 ULONG ProtectionMask, ControlAreaSize, Size, NonPagedCharge, PagedCharge;
2485 BOOLEAN FileLock = FALSE, KernelCall = FALSE;
2486 KIRQL OldIrql;
2488 BOOLEAN UserRefIncremented = FALSE;
2489 PVOID PreviousSectionPointer;
2490
2491 /* Make the same sanity checks that the Nt interface should've validated */
2494 SEC_NO_CHANGE)) == 0);
2504
2505 /* Convert section flag to page flag */
2508
2509 /* Check to make sure the protection is correct. Nt* does this already */
2511 if (ProtectionMask == MM_INVALID_PROTECTION)
2512 {
2513 DPRINT1("Invalid protection mask\n");
2515 }
2516
2517 /* Check if this is going to be a data or image backed file section */
2518 if ((FileHandle) || (FileObject))
2519 {
2520 /* These cannot be mapped with large pages */
2522
2523 /* For now, only support the mechanism through a file handle */
2525
2526 /* Reference the file handle to get the object */
2528 MmMakeFileAccess[ProtectionMask],
2531 (PVOID*)&File,
2532 NULL);
2533 if (!NT_SUCCESS(Status)) return Status;
2534
2535 /* Make sure Cc has been doing its job */
2536 if (!File->SectionObjectPointer)
2537 {
2538 /* This is not a valid file system-based file, fail */
2541 }
2542
2543 /* Image-file backed sections are not yet supported */
2545
2546 /* Compute the size of the control area, and allocate it */
2547 ControlAreaSize = sizeof(CONTROL_AREA) + sizeof(MSUBSECTION);
2548 ControlArea = ExAllocatePoolWithTag(NonPagedPool, ControlAreaSize, 'aCmM');
2549 if (!ControlArea)
2550 {
2553 }
2554
2555 /* Zero it out */
2556 RtlZeroMemory(ControlArea, ControlAreaSize);
2557
2558 /* Did we get a handle, or an object? */
2559 if (FileHandle)
2560 {
2561 /* We got a file handle so we have to lock down the file */
2562#if 0
2564 if (!NT_SUCCESS(Status))
2565 {
2566 ExFreePool(ControlArea);
2568 return Status;
2569 }
2570#else
2571 /* ReactOS doesn't support this API yet, so do nothing */
2573#endif
2574 /* Update the top-level IRP so that drivers know what's happening */
2576 FileLock = TRUE;
2577 }
2578
2579 /* Lock the PFN database while we play with the section pointers */
2580 OldIrql = MiAcquirePfnLock();
2581
2582 /* Image-file backed sections are not yet supported */
2584
2585 /* There should not already be a control area for this file */
2586 ASSERT(File->SectionObjectPointer->DataSectionObject == NULL);
2587 NewSegment = NULL;
2588
2589 /* Write down that this CA is being created, and set it */
2590 ControlArea->u.Flags.BeingCreated = TRUE;
2592 PreviousSectionPointer = File->SectionObjectPointer;
2593 File->SectionObjectPointer->DataSectionObject = ControlArea;
2594
2595 /* We can release the PFN lock now */
2596 MiReleasePfnLock(OldIrql);
2597
2598 /* We don't support previously-mapped file */
2599 ASSERT(NewSegment == NULL);
2600
2601 /* Image-file backed sections are not yet supported */
2603
2604 /* So we always create a data file map */
2606 &Segment,
2607 (PSIZE_T)InputMaximumSize,
2610 KernelCall);
2611 if (!NT_SUCCESS(Status))
2612 {
2613 /* Lock the PFN database while we play with the section pointers */
2614 OldIrql = MiAcquirePfnLock();
2615
2616 /* Reset the waiting-for-deletion event */
2617 ASSERT(ControlArea->WaitingForDeletion == NULL);
2618 ControlArea->WaitingForDeletion = NULL;
2619
2620 /* Set the file pointer NULL flag */
2621 ASSERT(ControlArea->u.Flags.FilePointerNull == 0);
2622 ControlArea->u.Flags.FilePointerNull = TRUE;
2623
2624 /* Delete the data section object */
2626 File->SectionObjectPointer->DataSectionObject = NULL;
2627
2628 /* No longer being created */
2629 ControlArea->u.Flags.BeingCreated = FALSE;
2630
2631 /* We can release the PFN lock now */
2632 MiReleasePfnLock(OldIrql);
2633
2634 /* Check if we locked and set the IRP */
2635 if (FileLock)
2636 {
2637 /* Undo */
2639 //FsRtlReleaseFile(File);
2640 }
2641
2642 /* Free the control area and de-ref the file object */
2643 ExFreePool(ControlArea);
2645
2646 /* All done */
2647 return Status;
2648 }
2649
2650 /* On success, we expect this */
2651 ASSERT(PreviousSectionPointer == File->SectionObjectPointer);
2652
2653 /* Check if a maximum size was specified */
2654 if (!InputMaximumSize->QuadPart)
2655 {
2656 /* Nope, use the segment size */
2657 Section.SizeOfSection.QuadPart = (LONGLONG)Segment->SizeOfSegment;
2658 }
2659 else
2660 {
2661 /* Yep, use the entered size */
2662 Section.SizeOfSection.QuadPart = InputMaximumSize->QuadPart;
2663 }
2664 }
2665 else
2666 {
2667 /* A handle must be supplied with SEC_IMAGE, as this is the no-handle path */
2669
2670 /* Not yet supported */
2672
2673 /* So this must be a pagefile-backed section, create the mappings needed */
2674 Status = MiCreatePagingFileMap(&NewSegment,
2675 InputMaximumSize,
2676 ProtectionMask,
2678 if (!NT_SUCCESS(Status)) return Status;
2679
2680 /* Set the size here, and read the control area */
2681 Section.SizeOfSection.QuadPart = NewSegment->SizeOfSegment;
2682 ControlArea = NewSegment->ControlArea;
2683
2684 /* MiCreatePagingFileMap increments user references */
2685 UserRefIncremented = TRUE;
2686 }
2687
2688 /* Did we already have a segment? */
2689 if (!NewSegment)
2690 {
2691 /* This must be the file path and we created a segment */
2692 NewSegment = Segment;
2693 ASSERT(File != NULL);
2694
2695 /* Acquire the PFN lock while we set control area flags */
2696 OldIrql = MiAcquirePfnLock();
2697
2698 /* We don't support this race condition yet, so assume no waiters */
2699 ASSERT(ControlArea->WaitingForDeletion == NULL);
2700 ControlArea->WaitingForDeletion = NULL;
2701
2702 /* Image-file backed sections are not yet supported, nor ROM images */
2704 ASSERT(Segment->ControlArea->u.Flags.Rom == 0);
2705
2706 /* Take off the being created flag, and then release the lock */
2707 ControlArea->u.Flags.BeingCreated = FALSE;
2708 MiReleasePfnLock(OldIrql);
2709 }
2710
2711 /* Check if we locked the file earlier */
2712 if (FileLock)
2713 {
2714 /* Reset the top-level IRP and release the lock */
2716 //FsRtlReleaseFile(File);
2717 FileLock = FALSE;
2718 }
2719
2720 /* Set the initial section object data */
2721 Section.InitialPageProtection = SectionPageProtection;
2722
2723 /* The mapping created a control area and segment, save the flags */
2724 Section.Segment = NewSegment;
2725 Section.u.LongFlags = ControlArea->u.LongFlags;
2726
2727 /* Check if this is a user-mode read-write non-image file mapping */
2728 if (!(FileObject) &&
2730 !(ControlArea->u.Flags.Image) &&
2731 (ControlArea->FilePointer))
2732 {
2733 /* Add a reference and set the flag */
2734 Section.u.Flags.UserWritable = TRUE;
2735 InterlockedIncrement((volatile LONG*)&ControlArea->WritableUserReferences);
2736 }
2737
2738 /* Check for image mappings or page file mappings */
2739 if ((ControlArea->u.Flags.Image) || !(ControlArea->FilePointer))
2740 {
2741 /* Charge the segment size, and allocate a subsection */
2742 PagedCharge = sizeof(SECTION) + NewSegment->TotalNumberOfPtes * sizeof(MMPTE);
2743 Size = sizeof(SUBSECTION);
2744 }
2745 else
2746 {
2747 /* Charge nothing, and allocate a mapped subsection */
2748 PagedCharge = 0;
2749 Size = sizeof(MSUBSECTION);
2750 }
2751
2752 /* Check if this is a normal CA */
2753 ASSERT(ControlArea->u.Flags.GlobalOnlyPerSession == 0);
2754 ASSERT(ControlArea->u.Flags.Rom == 0);
2755
2756 /* Charge only a CA, and the subsection is right after */
2757 NonPagedCharge = sizeof(CONTROL_AREA);
2758 Subsection = (PSUBSECTION)(ControlArea + 1);
2759
2760 /* We only support single-subsection mappings */
2761 NonPagedCharge += Size;
2762 ASSERT(Subsection->NextSubsection == NULL);
2763
2764 /* Create the actual section object, with enough space for the prototype PTEs */
2769 NULL,
2770 sizeof(SECTION),
2771 PagedCharge,
2772 NonPagedCharge,
2773 (PVOID*)&NewSection);
2774 if (!NT_SUCCESS(Status))
2775 {
2776 /* Check if this is a user-mode read-write non-image file mapping */
2777 if (!(FileObject) &&
2779 !(ControlArea->u.Flags.Image) &&
2780 (ControlArea->FilePointer))
2781 {
2782 /* Remove a reference and check the flag */
2783 ASSERT(Section.u.Flags.UserWritable == 1);
2784 InterlockedDecrement((volatile LONG*)&ControlArea->WritableUserReferences);
2785 }
2786
2787 /* Check if a user reference was added */
2788 if (UserRefIncremented)
2789 {
2790 /* Acquire the PFN lock while we change counters */
2791 OldIrql = MiAcquirePfnLock();
2792
2793 /* Decrement the accounting counters */
2794 ControlArea->NumberOfSectionReferences--;
2795 ASSERT((LONG)ControlArea->NumberOfUserReferences > 0);
2796 ControlArea->NumberOfUserReferences--;
2797
2798 /* Check if we should destroy the CA and release the lock */
2799 MiCheckControlArea(ControlArea, OldIrql);
2800 }
2801
2802 /* Return the failure code */
2803 return Status;
2804 }
2805
2806 /* NOTE: Past this point, all failures will be handled by Ob upon ref->0 */
2807
2808 /* Now copy the local section object from the stack into this new object */
2809 RtlCopyMemory(NewSection, &Section, sizeof(SECTION));
2810 NewSection->Address.StartingVpn = 0;
2811
2812 /* For now, only user calls are supported */
2813 ASSERT(KernelCall == FALSE);
2814 NewSection->u.Flags.UserReference = TRUE;
2815
2816 /* Is this a "based" allocation, in which all mappings are identical? */
2818 {
2819 /* Lock the VAD tree during the search */
2821
2822 /* Is it a brand new ControArea ? */
2823 if (ControlArea->u.Flags.BeingCreated == 1)
2824 {
2825 ASSERT(ControlArea->u.Flags.Based == 1);
2826 /* Then we must find a global address, top-down */
2829 _64K,
2831 (ULONG_PTR*)&ControlArea->Segment->BasedAddress);
2832
2833 if (!NT_SUCCESS(Status))
2834 {
2835 /* No way to find a valid range. */
2837 ControlArea->u.Flags.Based = 0;
2838 NewSection->u.Flags.Based = 0;
2839 ObDereferenceObject(NewSection);
2840 return Status;
2841 }
2842
2843 /* Compute the ending address and insert it into the VAD tree */
2844 NewSection->Address.StartingVpn = (ULONG_PTR)ControlArea->Segment->BasedAddress;
2845 NewSection->Address.EndingVpn = NewSection->Address.StartingVpn + NewSection->SizeOfSection.LowPart - 1;
2846 MiInsertBasedSection(NewSection);
2847 }
2848 else
2849 {
2850 /* FIXME : Should we deny section creation if SEC_BASED is not set ? Can we have two different section objects on the same based address ? Investigate !*/
2851 ASSERT(FALSE);
2852 }
2853
2855 }
2856
2857 /* The control area is not being created anymore */
2858 if (ControlArea->u.Flags.BeingCreated == 1)
2859 {
2860 /* Acquire the PFN lock while we set control area flags */
2861 OldIrql = MiAcquirePfnLock();
2862
2863 /* Take off the being created flag, and then release the lock */
2864 ControlArea->u.Flags.BeingCreated = 0;
2865 NewSection->u.Flags.BeingCreated = 0;
2866
2867 MiReleasePfnLock(OldIrql);
2868 }
2869
2870 /* Migrate the attribute into a flag */
2872
2873 /* If R/W access is not requested, this might eventually become a CoW mapping */
2875 {
2876 NewSection->u.Flags.CopyOnWrite = TRUE;
2877 }
2878
2879 /* Write down if this was a kernel call */
2880 ControlArea->u.Flags.WasPurged |= KernelCall;
2881 ASSERT(ControlArea->u.Flags.WasPurged == FALSE);
2882
2883 /* Make sure the segment and the section are the same size, or the section is smaller */
2884 ASSERT((ULONG64)NewSection->SizeOfSection.QuadPart <= NewSection->Segment->SizeOfSegment);
2885
2886 /* Return the object and the creation status */
2887 *SectionObject = (PVOID)NewSection;
2888 return Status;
2889}
KGUARDED_MUTEX MmSectionBasedMutex
Definition: section.c:110
static NTSTATUS NTAPI MiCreatePagingFileMap(OUT PSEGMENT *Segment, IN PLARGE_INTEGER MaximumSize, IN ULONG ProtectionMask, IN ULONG AllocationAttributes)
Definition: section.c:1552
NTSTATUS NTAPI MiCreateDataFileMap(IN PFILE_OBJECT File, OUT PSEGMENT *Segment, IN PSIZE_T MaximumSize, IN ULONG SectionPageProtection, IN ULONG AllocationAttributes, IN ULONG IgnoreFileSizing)
Definition: section.c:1536
PVOID MmHighSectionBase
Definition: section.c:111
VOID NTAPI MiCheckControlArea(IN PCONTROL_AREA ControlArea, IN KIRQL OldIrql)
Definition: section.c:714
ACCESS_MASK MmMakeFileAccess[8]
Definition: section.c:32
MM_AVL_TABLE MmSectionBasedRoot
Definition: section.c:109
ULONG NTAPI MiMakeProtectionMask(IN ULONG Protect)
Definition: section.c:140
#define InterlockedIncrement
Definition: armddk.h:53
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define ExGetPreviousMode
Definition: ex.h:140
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
_Must_inspect_result_ _Outptr_ PVOID * SectionObject
Definition: fsrtlfuncs.h:860
#define FSRTL_FSP_TOP_LEVEL_IRP
Definition: fsrtltypes.h:59
VOID FASTCALL KeReleaseGuardedMutex(IN OUT PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:53
VOID FASTCALL KeAcquireGuardedMutex(IN PKGUARDED_MUTEX GuardedMutex)
Definition: gmutex.c:42
POBJECT_TYPE IoFileObjectType
Definition: iomgr.c:36
VOID NTAPI MiInsertBasedSection(IN PSECTION Section)
Definition: vadnode.c:423
NTSTATUS NTAPI MiFindEmptyAddressRangeDownBasedTree(IN SIZE_T Length, IN ULONG_PTR BoundaryAddress, IN ULONG_PTR Alignment, IN PMM_AVL_TABLE Table, OUT PULONG_PTR Base)
Definition: vadnode.c:799
#define _64K
Definition: miarm.h:23
#define MM_INVALID_PROTECTION
Definition: miarm.h:67
unsigned __int64 ULONG64
Definition: imports.h:198
#define SECTION
Definition: profile.c:31
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER _In_ ULONG SectionPageProtection
Definition: mmfuncs.h:363
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER _In_ ULONG _In_ ULONG AllocationAttributes
Definition: mmfuncs.h:364
struct _SUBSECTION * PSUBSECTION
struct _SUBSECTION SUBSECTION
#define SEC_NOCACHE
Definition: mmtypes.h:101
#define SEC_COMMIT
Definition: mmtypes.h:100
#define SEC_IMAGE
Definition: mmtypes.h:97
struct _MSUBSECTION MSUBSECTION
#define SEC_LARGE_PAGES
Definition: mmtypes.h:103
#define SEC_NO_CHANGE
Definition: mmtypes.h:95
struct _CONTROL_AREA CONTROL_AREA
#define PAGE_NOCACHE
Definition: nt_native.h:1311
#define SEC_RESERVE
Definition: nt_native.h:1323
#define PAGE_NOACCESS
Definition: nt_native.h:1302
#define PAGE_GUARD
Definition: nt_native.h:1310
#define SEC_BASED
NTSTATUS NTAPI FsRtlAcquireToCreateMappedSection(_In_ PFILE_OBJECT FileObject, _In_ ULONG SectionPageProtection)
Definition: fastio.c:1647
#define STATUS_INVALID_FILE_FOR_SECTION
Definition: ntstatus.h:269
#define STATUS_INVALID_PARAMETER_6
Definition: ntstatus.h:480
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:305
NTSTATUS NTAPI ObCreateObject(IN KPROCESSOR_MODE ProbeMode OPTIONAL, IN POBJECT_TYPE Type, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN KPROCESSOR_MODE AccessMode, IN OUT PVOID ParseContext OPTIONAL, IN ULONG ObjectSize, IN ULONG PagedPoolCharge OPTIONAL, IN ULONG NonPagedPoolCharge OPTIONAL, OUT PVOID *Object)
Definition: oblife.c:1039
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:494
long LONG
Definition: pedump.c:60
POBJECT_TYPE MmSectionObjectType
Definition: section.c:194
PSEGMENT Segment
Definition: mmtypes.h:521
ULONG NumberOfSectionReferences
Definition: mmtypes.h:523
PEVENT_COUNTER WaitingForDeletion
Definition: mmtypes.h:534
ULONG NumberOfUserReferences
Definition: mmtypes.h:527
ULONG LongFlags
Definition: mmtypes.h:530
ULONG WritableUserReferences
Definition: mmtypes.h:537
union _CONTROL_AREA::@2608 u
MMSECTION_FLAGS Flags
Definition: mmtypes.h:531
PFILE_OBJECT FilePointer
Definition: mmtypes.h:533
ULONG_PTR StartingVpn
Definition: mmtypes.h:653
ULONG_PTR EndingVpn
Definition: mmtypes.h:654
ULONG NoChange
Definition: mmtypes.h:486
ULONG UserReference
Definition: mmtypes.h:479
ULONG CopyOnWrite
Definition: mmtypes.h:474
ULONG FilePointerNull
Definition: mmtypes.h:482
ULONG BeingCreated
Definition: mmtypes.h:464
ULONG GlobalOnlyPerSession
Definition: mmtypes.h:491
ULONG WasPurged
Definition: mmtypes.h:478
MMSECTION_FLAGS Flags
Definition: mmtypes.h:817
PSEGMENT Segment
Definition: mmtypes.h:812
union _SECTION::@2624 u
MMADDRESS_NODE Address
Definition: mmtypes.h:811
LARGE_INTEGER SizeOfSection
Definition: mmtypes.h:813
struct _CONTROL_AREA * ControlArea
Definition: mmtypes.h:409
ULONG TotalNumberOfPtes
Definition: mmtypes.h:410
PVOID BasedAddress
Definition: mmtypes.h:418
ULONGLONG SizeOfSegment
Definition: mmtypes.h:413
struct _SUBSECTION * NextSubsection
Definition: mmtypes.h:584
ULONG_PTR * PSIZE_T
Definition: typedefs.h:80
int64_t LONGLONG
Definition: typedefs.h:68
ULONG LowPart
Definition: typedefs.h:106
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
#define PAGE_WRITECOMBINE
Definition: mmtypes.h:78
_In_ KPROCESSOR_MODE PreviousMode
Definition: sefuncs.h:103

Referenced by MmCreateSection().

◆ MmCreateDataFileSection()

static NTSTATUS NTAPI MmCreateDataFileSection ( PSECTION SectionObject,
ACCESS_MASK  DesiredAccess,
POBJECT_ATTRIBUTES  ObjectAttributes,
PLARGE_INTEGER  UMaximumSize,
ULONG  SectionPageProtection,
ULONG  AllocationAttributes,
PFILE_OBJECT  FileObject,
BOOLEAN  GotFileHandle 
)
static

Definition at line 2327 of file section.c.

2338{
2339 PSECTION Section;
2343 KIRQL OldIrql;
2344
2345 /*
2346 * Create the section
2347 */
2352 NULL,
2353 sizeof(*Section),
2354 0,
2355 0,
2356 (PVOID*)&Section);
2357 if (!NT_SUCCESS(Status))
2358 {
2359 return Status;
2360 }
2361 /*
2362 * Initialize it
2363 */
2364 RtlZeroMemory(Section, sizeof(*Section));
2365
2366 /* Mark this as a "ROS" section */
2367 Section->u.Flags.filler = 1;
2369 Section->u.Flags.File = 1;
2370
2372 Section->u.Flags.NoChange = 1;
2374 Section->u.Flags.Reserve = 1;
2375
2376 if (!GotFileHandle)
2377 {
2378 ASSERT(UMaximumSize != NULL);
2379 // ASSERT(UMaximumSize->QuadPart != 0);
2380 MaximumSize = *UMaximumSize;
2381 }
2382 else
2383 {
2386 if (!NT_SUCCESS(Status))
2387 {
2388 ObDereferenceObject(Section);
2389 return Status;
2390 }
2391
2392 /*
2393 * FIXME: Revise this once a locking order for file size changes is
2394 * decided
2395 */
2396 if ((UMaximumSize != NULL) && (UMaximumSize->QuadPart != 0))
2397 {
2398 MaximumSize = *UMaximumSize;
2399 }
2400 else
2401 {
2403 /* Mapping zero-sized files isn't allowed. */
2404 if (MaximumSize.QuadPart == 0)
2405 {
2406 ObDereferenceObject(Section);
2408 }
2409 }
2410
2411 if (MaximumSize.QuadPart > FileSize.QuadPart)
2412 {
2415 sizeof(LARGE_INTEGER),
2416 &MaximumSize);
2417 if (!NT_SUCCESS(Status))
2418 {
2419 ObDereferenceObject(Section);
2421 }
2422 }
2423 }
2424
2425 if (FileObject->SectionObjectPointer == NULL)
2426 {
2427 ObDereferenceObject(Section);
2429 }
2430
2431 /*
2432 * Lock the file
2433 */
2435 if (Status != STATUS_SUCCESS)
2436 {
2437 ObDereferenceObject(Section);
2438 return Status;
2439 }
2440
2441 /* Lock the PFN lock while messing with Section Object pointers */
2442grab_segment:
2443 OldIrql = MiAcquirePfnLock();
2444 Segment = FileObject->SectionObjectPointer->DataSectionObject;
2445
2446 while (Segment && (Segment->SegFlags & (MM_SEGMENT_INDELETE | MM_SEGMENT_INCREATE)))
2447 {
2448 MiReleasePfnLock(OldIrql);
2450 OldIrql = MiAcquirePfnLock();
2451 Segment = FileObject->SectionObjectPointer->DataSectionObject;
2452 }
2453
2454 /*
2455 * If this file hasn't been mapped as a data file before then allocate a
2456 * section segment to describe the data file mapping
2457 */
2458 if (Segment == NULL)
2459 {
2460 /* Release the lock. ExAllocatePoolWithTag might acquire it */
2461 MiReleasePfnLock(OldIrql);
2462
2465 if (Segment == NULL)
2466 {
2467 //KeSetEvent((PVOID)&FileObject->Lock, IO_NO_INCREMENT, FALSE);
2468 ObDereferenceObject(Section);
2469 return STATUS_NO_MEMORY;
2470 }
2471
2472 /* We are creating it */
2473 RtlZeroMemory(Segment, sizeof(*Segment));
2475 Segment->RefCount = 1;
2476
2477 /* Acquire lock again */
2478 OldIrql = MiAcquirePfnLock();
2479
2480 if (FileObject->SectionObjectPointer->DataSectionObject != NULL)
2481 {
2482 /* Well that's bad luck. Restart it all over */
2483 MiReleasePfnLock(OldIrql);
2485 goto grab_segment;
2486 }
2487
2488 FileObject->SectionObjectPointer->DataSectionObject = Segment;
2489
2490 /* We're safe to release the lock now */
2491 MiReleasePfnLock(OldIrql);
2492
2493 Section->Segment = (PSEGMENT)Segment;
2494
2495 /* Self-referencing segment */
2496 Segment->Flags = &Segment->SegFlags;
2497 Segment->ReferenceCount = &Segment->RefCount;
2498
2499 Segment->SectionCount = 1;
2500
2502 Segment->FileObject = FileObject;
2504
2505 Segment->Image.FileOffset = 0;
2506 Segment->Protection = SectionPageProtection;
2507
2508 Segment->Image.Characteristics = 0;
2511 {
2512 Segment->Length.QuadPart = Segment->RawLength.QuadPart = 0;
2513 }
2514 else
2515 {
2516 Segment->RawLength.QuadPart = MaximumSize.QuadPart;
2517 Segment->Length.QuadPart = PAGE_ROUND_UP(Segment->RawLength.QuadPart);
2518 }
2519 Segment->Image.VirtualAddress = 0;
2521
2522 /* We're good to use it now */
2523 OldIrql = MiAcquirePfnLock();
2524 Segment->SegFlags &= ~MM_SEGMENT_INCREATE;
2525 MiReleasePfnLock(OldIrql);
2526 }
2527 else
2528 {
2529 Section->Segment = (PSEGMENT)Segment;
2530 InterlockedIncrement64(&Segment->RefCount);
2531 InterlockedIncrementUL(&Segment->SectionCount);
2532
2533 MiReleasePfnLock(OldIrql);
2534
2536
2537 if (MaximumSize.QuadPart > Segment->RawLength.QuadPart &&
2539 {
2540 Segment->RawLength.QuadPart = MaximumSize.QuadPart;
2541 Segment->Length.QuadPart = PAGE_ROUND_UP(Segment->RawLength.QuadPart);
2542 }
2543
2545 }
2546 Section->SizeOfSection = MaximumSize;
2547
2548 //KeSetEvent((PVOID)&FileObject->Lock, IO_NO_INCREMENT, FALSE);
2549 *SectionObject = Section;
2550 return STATUS_SUCCESS;
2551}
#define InterlockedIncrementUL(Addend)
Definition: ex.h:1527
@ FileEndOfFileInformation
Definition: from_kernel.h:81
_Must_inspect_result_ _Out_ PLARGE_INTEGER FileSize
Definition: fsrtlfuncs.h:108
NTSTATUS NTAPI IoSetInformation(IN PFILE_OBJECT FileObject, IN FILE_INFORMATION_CLASS FileInformationClass, IN ULONG Length, IN PVOID FileInformation)
Definition: iofunc.c:1314
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:362
struct _SEGMENT * PSEGMENT
#define PAGE_WRITECOPY
Definition: nt_native.h:1305
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1309
NTSTATUS NTAPI FsRtlGetFileSize(IN PFILE_OBJECT FileObject, IN OUT PLARGE_INTEGER FileSize)
Definition: fastio.c:815
#define STATUS_MAPPED_FILE_SIZE_ZERO
Definition: ntstatus.h:522
#define STATUS_SECTION_NOT_EXTENDED
Definition: ntstatus.h:371
NTSTATUS MmspWaitForFileLock(PFILE_OBJECT File)
Definition: section.c:907
ULONG InitialPageProtection
Definition: mmtypes.h:819
#define ObReferenceObject
Definition: obfuncs.h:204

Referenced by MmCreateSection().

◆ MmCreateImageSection()

NTSTATUS MmCreateImageSection ( PSECTION SectionObject,
ACCESS_MASK  DesiredAccess,
POBJECT_ATTRIBUTES  ObjectAttributes,
PLARGE_INTEGER  UMaximumSize,
ULONG  SectionPageProtection,
ULONG  AllocationAttributes,
PFILE_OBJECT  FileObject 
)

Definition at line 3177 of file section.c.

3184{
3185 PSECTION Section;
3187 PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
3188 KIRQL OldIrql;
3189
3190
3191 if (FileObject == NULL)
3193
3194 if (FileObject->SectionObjectPointer == NULL)
3195 {
3196 DPRINT1("Denying section creation due to missing cache initialization\n");
3198 }
3199
3200 /*
3201 * Create the section
3202 */
3207 NULL,
3208 sizeof(*Section),
3209 0,
3210 0,
3211 (PVOID*)(PVOID)&Section);
3212 if (!NT_SUCCESS(Status))
3213 {
3214 return Status;
3215 }
3216
3217 /*
3218 * Initialize it
3219 */
3220 RtlZeroMemory(Section, sizeof(*Section));
3221
3222 /* Mark this as a "ROS" Section */
3223 Section->u.Flags.filler = 1;
3224
3226 Section->u.Flags.File = 1;
3227 Section->u.Flags.Image = 1;
3229 Section->u.Flags.NoChange = 1;
3230
3231grab_image_section_object:
3232 OldIrql = MiAcquirePfnLock();
3233
3234 /* Wait for it to be properly created or deleted */
3235 ImageSectionObject = FileObject->SectionObjectPointer->ImageSectionObject;
3236 while(ImageSectionObject && (ImageSectionObject->SegFlags & (MM_SEGMENT_INDELETE | MM_SEGMENT_INCREATE)))
3237 {
3238 MiReleasePfnLock(OldIrql);
3239
3241
3242 OldIrql = MiAcquirePfnLock();
3243 ImageSectionObject = FileObject->SectionObjectPointer->ImageSectionObject;
3244 }
3245
3246 if (ImageSectionObject == NULL)
3247 {
3248 NTSTATUS StatusExeFmt;
3249
3250 /* Release the lock because ExAllocatePoolWithTag could need to acquire it */
3251 MiReleasePfnLock(OldIrql);
3252
3253 ImageSectionObject = ExAllocatePoolZero(NonPagedPool, sizeof(MM_IMAGE_SECTION_OBJECT), TAG_MM_SECTION_SEGMENT);
3254 if (ImageSectionObject == NULL)
3255 {
3256 ObDereferenceObject(Section);
3257 return STATUS_NO_MEMORY;
3258 }
3259
3260 ImageSectionObject->SegFlags = MM_SEGMENT_INCREATE;
3261 ImageSectionObject->RefCount = 1;
3262 ImageSectionObject->SectionCount = 1;
3263
3264 OldIrql = MiAcquirePfnLock();
3265 if (FileObject->SectionObjectPointer->ImageSectionObject != NULL)
3266 {
3267 MiReleasePfnLock(OldIrql);
3268 /* Bad luck. Start over */
3269 ExFreePoolWithTag(ImageSectionObject, TAG_MM_SECTION_SEGMENT);
3270 goto grab_image_section_object;
3271 }
3272
3273 FileObject->SectionObjectPointer->ImageSectionObject = ImageSectionObject;
3274
3275 MiReleasePfnLock(OldIrql);
3276
3277 /* Purge the cache */
3278 CcFlushCache(FileObject->SectionObjectPointer, NULL, 0, NULL);
3279
3280 StatusExeFmt = ExeFmtpCreateImageSection(FileObject, ImageSectionObject);
3281
3282 if (!NT_SUCCESS(StatusExeFmt))
3283 {
3284 /* Unset */
3285 OldIrql = MiAcquirePfnLock();
3286 FileObject->SectionObjectPointer->ImageSectionObject = NULL;
3287 MiReleasePfnLock(OldIrql);
3288
3289 if(ImageSectionObject->Segments != NULL)
3290 ExFreePool(ImageSectionObject->Segments);
3291
3292 /*
3293 * If image file is empty, then return that the file is invalid for section
3294 */
3295 Status = StatusExeFmt;
3296 if (StatusExeFmt == STATUS_END_OF_FILE)
3297 {
3299 }
3300
3301 ExFreePoolWithTag(ImageSectionObject, TAG_MM_SECTION_SEGMENT);
3302 ObDereferenceObject(Section);
3303 return Status;
3304 }
3305
3306 Section->Segment = (PSEGMENT)ImageSectionObject;
3307 ASSERT(ImageSectionObject->Segments);
3308 ASSERT(ImageSectionObject->RefCount > 0);
3309
3310 /*
3311 * Lock the file
3312 */
3314 if (!NT_SUCCESS(Status))
3315 {
3316 /* Unset */
3317 OldIrql = MiAcquirePfnLock();
3318 FileObject->SectionObjectPointer->ImageSectionObject = NULL;
3319 MiReleasePfnLock(OldIrql);
3320
3321 ExFreePool(ImageSectionObject->Segments);
3322 ExFreePool(ImageSectionObject);
3323 ObDereferenceObject(Section);
3324 return Status;
3325 }
3326
3327 OldIrql = MiAcquirePfnLock();
3328 ImageSectionObject->SegFlags &= ~MM_SEGMENT_INCREATE;
3329
3330 /* Take a ref on the file on behalf of the newly created structure */
3332
3333 MiReleasePfnLock(OldIrql);
3334
3335 Status = StatusExeFmt;
3336 }
3337 else
3338 {
3339 /* If FS driver called for delete, tell them it's not possible anymore. */
3340 ImageSectionObject->SegFlags &= ~MM_IMAGE_SECTION_FLUSH_DELETE;
3341
3342 /* Take one ref */
3343 InterlockedIncrement64(&ImageSectionObject->RefCount);
3344 ImageSectionObject->SectionCount++;
3345
3346 MiReleasePfnLock(OldIrql);
3347
3348 Section->Segment = (PSEGMENT)ImageSectionObject;
3349
3351 }
3352 //KeSetEvent((PVOID)&FileObject->Lock, IO_NO_INCREMENT, FALSE);
3353 *SectionObject = Section;
3354 ASSERT(ImageSectionObject->RefCount > 0);
3355
3356 return Status;
3357}
VOID NTAPI CcFlushCache(IN PSECTION_OBJECT_POINTERS SectionObjectPointer, IN OPTIONAL PLARGE_INTEGER FileOffset, IN ULONG Length, OUT OPTIONAL PIO_STATUS_BLOCK IoStatus)
Definition: cachesub.c:222
NTSTATUS ExeFmtpCreateImageSection(PFILE_OBJECT FileObject, PMM_IMAGE_SECTION_OBJECT ImageSectionObject)
Definition: section.c:3024
#define STATUS_END_OF_FILE
Definition: shellext.h:67

Referenced by MmCreateSection().

◆ MmCreatePhysicalMemorySection()

NTSTATUS NTAPI MmCreatePhysicalMemorySection ( VOID  )

Definition at line 2203 of file section.c.

2204{
2205 PSECTION PhysSection;
2208 UNICODE_STRING Name = RTL_CONSTANT_STRING(L"\\Device\\PhysicalMemory");
2209 LARGE_INTEGER SectionSize;
2210 HANDLE Handle;
2212
2213 /*
2214 * Create the section mapping physical memory
2215 */
2218 &Name,
2220 NULL,
2221 NULL);
2222 /*
2223 * Create the Object
2224 */
2227 &Obj,
2229 NULL,
2230 sizeof(*PhysSection),
2231 0,
2232 0,
2233 (PVOID*)&PhysSection);
2234 if (!NT_SUCCESS(Status))
2235 {
2236 DPRINT1("MmCreatePhysicalMemorySection: failed to create object (0x%lx)\n", Status);
2237 return Status;
2238 }
2239
2240 /*
2241 * Initialize it
2242 */
2243 RtlZeroMemory(PhysSection, sizeof(*PhysSection));
2244
2245 /* Mark this as a "ROS Section" */
2246 PhysSection->u.Flags.filler = 1;
2248 PhysSection->u.Flags.PhysicalMemory = 1;
2249 PhysSection->SizeOfSection = SectionSize;
2252 if (Segment == NULL)
2253 {
2254 ObDereferenceObject(PhysSection);
2255 return STATUS_NO_MEMORY;
2256 }
2258 PhysSection->Segment = (PSEGMENT)Segment;
2259 Segment->RefCount = 1;
2260
2261 Segment->ReferenceCount = &Segment->RefCount;
2262 Segment->Flags = &Segment->SegFlags;
2263
2265 Segment->Image.FileOffset = 0;
2266 Segment->Protection = PAGE_EXECUTE_READWRITE;
2267 Segment->RawLength = SectionSize;
2268 Segment->Length = SectionSize;
2270 Segment->WriteCopy = FALSE;
2271 Segment->Image.VirtualAddress = 0;
2272 Segment->Image.Characteristics = 0;
2274
2275 Status = ObInsertObject(PhysSection,
2276 NULL,
2278 0,
2279 NULL,
2280 &Handle);
2281 if (!NT_SUCCESS(Status))
2282 {
2283 ObDereferenceObject(PhysSection);
2284 return Status;
2285 }
2287
2288 return STATUS_SUCCESS;
2289}
ULONG Handle
Definition: gdb_input.c:15
#define OBJ_PERMANENT
Definition: winternl.h:226
PFN_NUMBER MmHighestPhysicalPage
Definition: meminit.c:31
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define OBJ_KERNEL_EXCLUSIVE
Definition: obtypes.h:91
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define L(x)
Definition: ntvdm.h:50
NTSTATUS NTAPI ObCloseHandle(IN HANDLE Handle, IN KPROCESSOR_MODE AccessMode)
Definition: obhandle.c:3379
NTSTATUS NTAPI ObInsertObject(IN PVOID Object, IN PACCESS_STATE AccessState OPTIONAL, IN ACCESS_MASK DesiredAccess, IN ULONG ObjectPointerBias, OUT PVOID *NewObject OPTIONAL, OUT PHANDLE Handle)
Definition: obhandle.c:2935
ULONG PhysicalMemory
Definition: mmtypes.h:473
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14

Referenced by MmInitSectionImplementation().

◆ MmCreateSection()

NTSTATUS NTAPI MmCreateSection ( OUT PVOID Section,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes  OPTIONAL,
IN PLARGE_INTEGER  MaximumSize,
IN ULONG  SectionPageProtection,
IN ULONG  AllocationAttributes,
IN HANDLE FileHandle  OPTIONAL,
IN PFILE_OBJECT FileObject  OPTIONAL 
)

Definition at line 4620 of file section.c.

4628{
4630 ULONG Protection;
4631 PSECTION *SectionObject = (PSECTION *)Section;
4632 BOOLEAN FileLock = FALSE;
4633
4634 /* Check if an ARM3 section is being created instead */
4636 {
4637 if (!(FileObject) && !(FileHandle))
4638 {
4639 return MmCreateArm3Section(Section,
4645 FileHandle,
4646 FileObject);
4647 }
4648 }
4649
4650 /* Convert section flag to page flag */
4652
4653 /* Check to make sure the protection is correct. Nt* does this already */
4655 if (Protection == MM_INVALID_PROTECTION)
4656 {
4657 DPRINT1("Page protection is invalid\n");
4659 }
4660
4661 /* Check if this is going to be a data or image backed file section */
4662 if ((FileHandle) || (FileObject))
4663 {
4664 /* These cannot be mapped with large pages */
4666 {
4667 DPRINT1("Large pages cannot be used with an image mapping\n");
4669 }
4670
4671 /* Did the caller pass a file object ? */
4672 if (FileObject)
4673 {
4674 /* Reference the object directly */
4676
4677 /* We don't create image mappings with file objects */
4678 AllocationAttributes &= ~SEC_IMAGE;
4679 }
4680 else
4681 {
4682 /* Reference the file handle to get the object */
4684 MmMakeFileAccess[Protection],
4687 (PVOID*)&FileObject,
4688 NULL);
4689 if (!NT_SUCCESS(Status))
4690 {
4691 DPRINT1("Failed to get a handle to the FO: %lx\n", Status);
4692 return Status;
4693 }
4694
4695 /* Lock the file */
4697 if (!NT_SUCCESS(Status))
4698 {
4700 return Status;
4701 }
4702
4703 FileLock = TRUE;
4704
4705 /* Deny access if there are writes on the file */
4706#if 0
4708 {
4709 DPRINT1("Cannot create image maps with writers open on the file!\n");
4711 goto Quit;
4712 }
4713#else
4715 DPRINT1("Creating image map with writers open on the file!\n");
4716#endif
4717 }
4718 }
4719 else
4720 {
4721 /* A handle must be supplied with SEC_IMAGE, as this is the no-handle path */
4723 }
4724
4726 {
4733 FileObject);
4734 }
4735#ifndef NEWCC
4736 else if (FileObject != NULL)
4737 {
4744 FileObject,
4745 FileHandle != NULL);
4746 }
4747#else
4748 else if (FileHandle != NULL || FileObject != NULL)
4749 {
4756 FileObject);
4757 }
4758#endif
4759 else
4760 {
4761 /* All cases should be handled above */
4763 }
4764
4765 if (FileLock)
4767 if (FileObject)
4769
4770 return Status;
4771}
NTSTATUS NTAPI MmCreateCacheSection(PSECTION *SectionObject, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PLARGE_INTEGER UMaximumSize, ULONG SectionPageProtection, ULONG AllocationAttributes, PFILE_OBJECT FileObject)
#define SEC_PHYSICALMEMORY
Definition: mm.h:112
#define STATUS_FILE_LOCKED_WITH_WRITERS
Definition: ntstatus.h:112
static NTSTATUS NTAPI MmCreateDataFileSection(PSECTION *SectionObject, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PLARGE_INTEGER UMaximumSize, ULONG SectionPageProtection, ULONG AllocationAttributes, PFILE_OBJECT FileObject, BOOLEAN GotFileHandle)
Definition: section.c:2327
NTSTATUS NTAPI MmCreateArm3Section(OUT PVOID *SectionObject, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN PLARGE_INTEGER InputMaximumSize, IN ULONG SectionPageProtection, IN ULONG AllocationAttributes, IN HANDLE FileHandle OPTIONAL, IN PFILE_OBJECT FileObject OPTIONAL)
Definition: section.c:2468
NTSTATUS MmCreateImageSection(PSECTION *SectionObject, ACCESS_MASK DesiredAccess, POBJECT_ATTRIBUTES ObjectAttributes, PLARGE_INTEGER UMaximumSize, ULONG SectionPageProtection, ULONG AllocationAttributes, PFILE_OBJECT FileObject)
Definition: section.c:3177
#define STATUS_ACCESS_DENIED
Definition: udferr_usr.h:145
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2658

Referenced by CcpAllocateSection(), CcRosInitializeFileCache(), EngCreateSection(), EngCreateSectionHack(), EngLoadModuleEx(), InitGdiHandleTable(), IntGdiAddFontResourceEx(), NtCreateSection(), TestCreateSection(), TestPhysicalMemorySection(), and UserCreateHeap().

◆ MmExtendSection()

NTSTATUS NTAPI MmExtendSection ( _In_ PVOID  _Section,
_Inout_ PLARGE_INTEGER  NewSize 
)

Definition at line 5273 of file section.c.

5276{
5277 PSECTION Section = _Section;
5278
5279 /* It makes no sense to extend an image mapping */
5280 if (Section->u.Flags.Image)
5282
5283 /* Nor is it possible to extend a page file mapping */
5284 if (!Section->u.Flags.File)
5286
5287 if (!MiIsRosSectionObject(Section))
5289
5290 /* We just extend the sizes. Shrinking is a no-op ? */
5291 if (NewSize->QuadPart > Section->SizeOfSection.QuadPart)
5292 {
5294 Section->SizeOfSection = *NewSize;
5295
5296 if (!Section->u.Flags.Reserve)
5297 {
5299 if (Segment->RawLength.QuadPart < NewSize->QuadPart)
5300 {
5301 Segment->RawLength = *NewSize;
5302 Segment->Length.QuadPart = (NewSize->QuadPart + PAGE_SIZE - 1) & ~((LONGLONG)PAGE_SIZE);
5303 }
5305 }
5306 }
5307
5308 return STATUS_SUCCESS;
5309}
_Must_inspect_result_ _In_ USHORT NewSize
Definition: fltkernel.h:975
if(dx< 0)
Definition: linetemp.h:194
FORCEINLINE BOOLEAN MiIsRosSectionObject(IN PSECTION Section)
Definition: miarm.h:1098
struct _MM_SECTION_SEGMENT * PMM_SECTION_SEGMENT
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:239

Referenced by CcSetFileSizes(), and NtExtendSection().

◆ MmFlushImageSection()

BOOLEAN NTAPI MmFlushImageSection ( IN PSECTION_OBJECT_POINTERS  SectionObjectPointer,
IN MMFLUSH_TYPE  FlushType 
)

Definition at line 4356 of file section.c.

4358{
4359 switch(FlushType)
4360 {
4361 case MmFlushForDelete:
4362 {
4363 /*
4364 * FIXME: Check for outstanding write probes on Data section.
4365 * How do we do that ?
4366 */
4367 }
4368 /* Fall-through */
4369 case MmFlushForWrite:
4370 {
4371 KIRQL OldIrql = MiAcquirePfnLock();
4372 PMM_IMAGE_SECTION_OBJECT ImageSectionObject = SectionObjectPointer->ImageSectionObject;
4373
4374 DPRINT("Deleting or modifying %p\n", SectionObjectPointer);
4375
4376 /* Wait for concurrent creation or deletion of image to be done */
4377 ImageSectionObject = SectionObjectPointer->ImageSectionObject;
4378 while (ImageSectionObject && (ImageSectionObject->SegFlags & (MM_SEGMENT_INCREATE | MM_SEGMENT_INDELETE)))
4379 {
4380 MiReleasePfnLock(OldIrql);
4382 OldIrql = MiAcquirePfnLock();
4383 ImageSectionObject = SectionObjectPointer->ImageSectionObject;
4384 }
4385
4386 if (!ImageSectionObject)
4387 {
4388 DPRINT("No image section object. Accepting\n");
4389 /* Nothing to do */
4390 MiReleasePfnLock(OldIrql);
4391 return TRUE;
4392 }
4393
4394 /* Do we have open sections or mappings on it ? */
4395 if ((ImageSectionObject->SectionCount) || (ImageSectionObject->MapCount))
4396 {
4397 /* We do. No way to delete it */
4398 MiReleasePfnLock(OldIrql);
4399 DPRINT("Denying. There are mappings open\n");
4400 return FALSE;
4401 }
4402
4403 /* There are no sections open on it, but we must still have pages around. Discard everything */
4404 ImageSectionObject->SegFlags |= MM_IMAGE_SECTION_FLUSH_DELETE;
4405 InterlockedIncrement64(&ImageSectionObject->RefCount);
4406 MiReleasePfnLock(OldIrql);
4407
4408 DPRINT("Purging\n");
4409
4410 for (ULONG i = 0; i < ImageSectionObject->NrSegments; i++)
4411 {
4412 if (!MiPurgeImageSegment(&ImageSectionObject->Segments[i]))
4413 break;
4414 }
4415
4416 /* Grab lock again */
4417 OldIrql = MiAcquirePfnLock();
4418
4419 if (!(ImageSectionObject->SegFlags & MM_IMAGE_SECTION_FLUSH_DELETE))
4420 {
4421 /*
4422 * Someone actually created a section while we were not looking.
4423 * Drop our ref and deny.
4424 * MmDereferenceSegmentWithLock releases Pfn lock
4425 */
4426 MmDereferenceSegmentWithLock(&ImageSectionObject->Segments[0], OldIrql);
4427 return FALSE;
4428 }
4429
4430 /* We should be the last one holding a ref here. */
4431 ASSERT(ImageSectionObject->RefCount == 1);
4432 ASSERT(ImageSectionObject->SectionCount == 0);
4433
4434 /* Dereference the first segment, this will free everything & release the lock */
4435 MmDereferenceSegmentWithLock(&ImageSectionObject->Segments[0], OldIrql);
4436 return TRUE;
4437 }
4438 }
4439 return FALSE;
4440}
IN PFCB IN FAT_FLUSH_TYPE FlushType
Definition: fatprocs.h:1082
#define MM_IMAGE_SECTION_FLUSH_DELETE
Definition: mm.h:241
static BOOLEAN MiPurgeImageSegment(PMM_SECTION_SEGMENT Segment)
Definition: section.c:4310

Referenced by _Requires_lock_held_(), Ext2CreateFile(), Ext2IsFileRemovable(), Ext2PurgeFile(), Ext2PurgeVolume(), FatSetRenameInfo(), open_file3(), RxCommonSetInformation(), RxPurgeFcbInSystemCache(), RxPurgeFobx(), RxPurgeNetFcb(), set_disposition_information(), UDFCloseAllXXXDelayedInDir(), UDFCommonCreate(), UDFMarkStreamsForDeletion(), UDFSetDispositionInformation(), VfatCreateFile(), vfatPrepareTargetForRename(), and VfatSetDispositionInformation().

◆ MmFlushSegment()

NTSTATUS NTAPI MmFlushSegment ( _In_ PSECTION_OBJECT_POINTERS  SectionObjectPointer,
_In_opt_ PLARGE_INTEGER  Offset,
_In_ ULONG  Length,
_Out_opt_ PIO_STATUS_BLOCK  Iosb 
)

Definition at line 4940 of file section.c.

4945{
4946 LARGE_INTEGER FlushStart, FlushEnd;
4948
4949 if (Offset)
4950 {
4951 FlushStart = *Offset;
4952 Status = RtlLongLongAdd(FlushStart.QuadPart, Length, &FlushEnd.QuadPart);
4953 if (!NT_SUCCESS(Status))
4954 return Status;
4955 }
4956
4957 if (Iosb)
4958 Iosb->Information = 0;
4959
4961 if (!Segment)
4962 {
4963 /* Nothing to flush */
4964 if (Iosb)
4965 Iosb->Status = STATUS_SUCCESS;
4966 return STATUS_SUCCESS;
4967 }
4968
4970
4972
4973 if (!Offset)
4974 {
4975 FlushStart.QuadPart = 0;
4976
4977 /* FIXME: All of this is suboptimal */
4978 ULONG ElemCount = RtlNumberGenericTableElements(&Segment->PageTable);
4979 /* No page. Nothing to flush */
4980 if (!ElemCount)
4981 {
4983 MmDereferenceSegment(Segment);
4984 if (Iosb)
4985 {
4986 Iosb->Status = STATUS_SUCCESS;
4987 Iosb->Information = 0;
4988 }
4989 return STATUS_SUCCESS;
4990 }
4991
4993 FlushEnd.QuadPart = PageTable->FileOffset.QuadPart + _countof(PageTable->PageEntries) * PAGE_SIZE;
4994 }
4995
4996 FlushStart.QuadPart >>= PAGE_SHIFT;
4997 FlushStart.QuadPart <<= PAGE_SHIFT;
4998
4999 while (FlushStart.QuadPart < FlushEnd.QuadPart)
5000 {
5002
5003 if (IS_DIRTY_SSE(Entry))
5004 {
5005 MmCheckDirtySegment(Segment, &FlushStart, FALSE, FALSE);
5006
5007 if (Iosb)
5008 Iosb->Information += PAGE_SIZE;
5009 }
5010
5011 FlushStart.QuadPart += PAGE_SIZE;
5012 }
5013
5015 MmDereferenceSegment(Segment);
5016
5017 if (Iosb)
5018 Iosb->Status = STATUS_SUCCESS;
5019
5020 return STATUS_SUCCESS;
5021}
BOOLEAN NTAPI MmCheckDirtySegment(PMM_SECTION_SEGMENT Segment, PLARGE_INTEGER Offset, BOOLEAN ForceDirty, BOOLEAN PageOut)
_Must_inspect_result_ NTSYSAPI PVOID NTAPI RtlGetElementGenericTable(_In_ PRTL_GENERIC_TABLE Table, _In_ ULONG I)
NTSYSAPI ULONG NTAPI RtlNumberGenericTableElements(_In_ PRTL_GENERIC_TABLE Table)

Referenced by CcFlushCache(), CcRosDeleteFileCache(), CcRosFlushVacb(), and MiRosUnmapViewOfSection().

◆ MmFreeSectionPage()

static VOID MmFreeSectionPage ( PVOID  Context,
MEMORY_AREA MemoryArea,
PVOID  Address,
PFN_NUMBER  Page,
SWAPENTRY  SwapEntry,
BOOLEAN  Dirty 
)
static

Definition at line 3441 of file section.c.

3443{
3446 SWAPENTRY SavedSwapEntry;
3450
3453
3455
3457 MemoryArea->SectionData.ViewOffset;
3458
3459 Segment = MemoryArea->SectionData.Segment;
3460
3462 while (Entry && MM_IS_WAIT_PTE(Entry))
3463 {
3466
3468
3472 }
3473
3474 /*
3475 * For a dirty, datafile, non-private page, there shoulkd be no swap entry
3476 */
3477 if (*Segment->Flags & MM_DATAFILE_SEGMENT)
3478 {
3479 if (Page == PFN_FROM_SSE(Entry) && Dirty)
3480 {
3481 ASSERT(SwapEntry == 0);
3482 }
3483 }
3484
3485 if (SwapEntry != 0)
3486 {
3487 /*
3488 * Sanity check
3489 */
3490 MmFreeSwapPage(SwapEntry);
3491 }
3492 else if (Page != 0)
3493 {
3494 if (IS_SWAP_FROM_SSE(Entry) ||
3496 {
3497 ASSERT(Process != NULL);
3498
3499 /*
3500 * Just dereference private pages
3501 */
3502 SavedSwapEntry = MmGetSavedSwapEntryPage(Page);
3503 if (SavedSwapEntry != 0)
3504 {
3505 MmFreeSwapPage(SavedSwapEntry);
3507 }
3510 }
3511 else
3512 {
3513 if (Process)
3514 {
3516 }
3517
3518 /* We don't dirtify for System Space Maps. We let Cc manage that */
3520 }
3521 }
3522}
struct _MMSUPPORT * PMMSUPPORT
#define MM_IS_WAIT_PTE(E)
Definition: mm.h:1370

Referenced by MmUnmapViewOfSegment().

◆ MmInitSectionImplementation()

NTSTATUS NTAPI MmInitSectionImplementation ( VOID  )

Definition at line 2294 of file section.c.

2295{
2296 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer;
2298
2299 DPRINT("Creating Section Object Type\n");
2300
2301 /* Initialize the section based root */
2304
2305 /* Initialize the Section object type */
2306 RtlZeroMemory(&ObjectTypeInitializer, sizeof(ObjectTypeInitializer));
2307 RtlInitUnicodeString(&Name, L"Section");
2308 ObjectTypeInitializer.Length = sizeof(ObjectTypeInitializer);
2309 ObjectTypeInitializer.DefaultPagedPoolCharge = sizeof(SECTION);
2310 ObjectTypeInitializer.PoolType = PagedPool;
2311 ObjectTypeInitializer.UseDefaultObject = TRUE;
2312 ObjectTypeInitializer.GenericMapping = MmpSectionMapping;
2313 ObjectTypeInitializer.DeleteProcedure = MmpDeleteSection;
2314 ObjectTypeInitializer.CloseProcedure = MmpCloseSection;
2315 ObjectTypeInitializer.ValidAccessMask = SECTION_ALL_ACCESS;
2316 ObjectTypeInitializer.InvalidAttributes = OBJ_OPENLINK;
2317 ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &MmSectionObjectType);
2318
2320
2321 return STATUS_SUCCESS;
2322}
struct NameRec_ * Name
Definition: cdprocs.h:460
#define OBJ_OPENLINK
Definition: winternl.h:230
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSTATUS NTAPI ObCreateObjectType(IN PUNICODE_STRING TypeName, IN POBJECT_TYPE_INITIALIZER ObjectTypeInitializer, IN PVOID Reserved, OUT POBJECT_TYPE *ObjectType)
Definition: oblife.c:1136
static GENERIC_MAPPING MmpSectionMapping
Definition: section.c:223
VOID NTAPI MmpDeleteSection(PVOID ObjectBody)
Definition: section.c:2138
VOID NTAPI MmpCloseSection(IN PEPROCESS Process OPTIONAL, IN PVOID Object, IN ACCESS_MASK GrantedAccess, IN ULONG ProcessHandleCount, IN ULONG SystemHandleCount)
Definition: section.c:2191
NTSTATUS NTAPI MmCreatePhysicalMemorySection(VOID)
Definition: section.c:2203
union _MMADDRESS_NODE::@2613 u1
struct _MMADDRESS_NODE * Parent
Definition: mmtypes.h:649
ULONG_PTR NumberGenericTableElements
Definition: mmtypes.h:668
MMADDRESS_NODE BalancedRoot
Definition: mmtypes.h:662
OB_CLOSE_METHOD CloseProcedure
Definition: obtypes.h:368
GENERIC_MAPPING GenericMapping
Definition: obtypes.h:358
OB_DELETE_METHOD DeleteProcedure
Definition: obtypes.h:369

Referenced by MmInitSystem().

◆ MmMakeDataSectionResident()

NTSTATUS NTAPI MmMakeDataSectionResident ( _In_ PSECTION_OBJECT_POINTERS  SectionObjectPointer,
_In_ LONGLONG  Offset,
_In_ ULONG  Length,
_In_ PLARGE_INTEGER  ValidDataLength 
)

Definition at line 4920 of file section.c.

4925{
4927
4928 /* There must be a segment for this call */
4929 ASSERT(Segment);
4930
4932
4933 MmDereferenceSegment(Segment);
4934
4935 return Status;
4936}
static NTSTATUS NTAPI MmMakeSegmentResident(_In_ PMM_SECTION_SEGMENT Segment, _In_ LONGLONG Offset, _In_ ULONG Length, _In_opt_ PLARGE_INTEGER ValidDataLength, _In_ BOOLEAN SetDirty)
Definition: section.c:1192

Referenced by CcRosEnsureVacbResident().

◆ MmMakePagesDirty()

NTSTATUS NTAPI MmMakePagesDirty ( _In_ PEPROCESS  Process,
_In_ PVOID  Address,
_In_ ULONG  Length 
)

Definition at line 5202 of file section.c.

5206{
5209 LARGE_INTEGER SegmentOffset, RangeEnd;
5211
5213
5215 if (MemoryArea == NULL)
5216 {
5217 DPRINT1("Unable to find memory area at address %p.\n", Address);
5220 }
5221
5222 /* Only supported in old Mm for now */
5224 /* For file mappings */
5226
5227 Segment = MemoryArea->SectionData.Segment;
5229
5231 + MemoryArea->SectionData.ViewOffset;
5233 + MemoryArea->SectionData.ViewOffset;
5234
5235 DPRINT("MmMakePagesResident: Segment %p, 0x%I64x -> 0x%I64x\n", Segment, SegmentOffset.QuadPart, RangeEnd.QuadPart);
5236
5237 while (SegmentOffset.QuadPart < RangeEnd.QuadPart)
5238 {
5240
5241 /* Let any pending read proceed */
5242 while (MM_IS_WAIT_PTE(Entry))
5243 {
5249 Entry = MmGetPageEntrySectionSegment(Segment, &SegmentOffset);
5250 }
5251
5252 /* We are called from Cc, this can't be backed by the page files */
5254
5255 /* If there is no page there, there is nothing to make dirty */
5256 if (Entry != 0)
5257 {
5258 /* Dirtify the entry */
5260 }
5261
5262 SegmentOffset.QuadPart += PAGE_SIZE;
5263 }
5264
5266
5268 return STATUS_SUCCESS;
5269}

Referenced by CcCopyWrite(), CcSetDirtyPinnedData(), and CcZeroData().

◆ MmMakeSegmentResident()

static NTSTATUS NTAPI MmMakeSegmentResident ( _In_ PMM_SECTION_SEGMENT  Segment,
_In_ LONGLONG  Offset,
_In_ ULONG  Length,
_In_opt_ PLARGE_INTEGER  ValidDataLength,
_In_ BOOLEAN  SetDirty 
)
static

Definition at line 1192 of file section.c.

1198{
1199 /* Let's use a 64K granularity. */
1200 LONGLONG RangeStart, RangeEnd;
1202 PFILE_OBJECT FileObject = Segment->FileObject;
1203
1204 /* Calculate our range, aligned on 64K if possible. */
1205 Status = RtlLongLongAdd(Offset, Length, &RangeEnd);
1207 if (!NT_SUCCESS(Status))
1208 return Status;
1209
1210 /* If the file is not random access and we are not the page out thread
1211 * read a 64K Chunk. */
1213 && !FlagOn(FileObject->Flags, FO_RANDOM_ACCESS))
1214 {
1215 RangeStart = Offset - (Offset % _64K);
1216 if (RangeEnd % _64K)
1217 RangeEnd += _64K - (RangeEnd % _64K);
1218 }
1219 else
1220 {
1221 RangeStart = Offset - (Offset % PAGE_SIZE);
1222 if (RangeEnd % PAGE_SIZE)
1223 RangeEnd += PAGE_SIZE - (RangeEnd % PAGE_SIZE);
1224 }
1225
1226 /* Clamp if needed */
1227 if (!FlagOn(*Segment->Flags, MM_DATAFILE_SEGMENT))
1228 {
1229 if (RangeEnd > Segment->RawLength.QuadPart)
1230 RangeEnd = Segment->RawLength.QuadPart;
1231 }
1232
1233 /* Let's gooooooooo */
1234 for ( ; RangeStart < RangeEnd; RangeStart += _64K)
1235 {
1236 /* First take a look at where we miss pages */
1237 ULONG ToReadPageBits = 0;
1238 LONGLONG ChunkEnd = RangeStart + _64K;
1239
1240 if (ChunkEnd > RangeEnd)
1241 ChunkEnd = RangeEnd;
1242
1244 for (LONGLONG ChunkOffset = RangeStart; ChunkOffset < ChunkEnd; ChunkOffset += PAGE_SIZE)
1245 {
1246 LARGE_INTEGER CurrentOffset;
1247
1248 CurrentOffset.QuadPart = ChunkOffset;
1250
1251 /* Let any pending read proceed */
1252 while (MM_IS_WAIT_PTE(Entry))
1253 {
1255
1257
1259 Entry = MmGetPageEntrySectionSegment(Segment, &CurrentOffset);
1260 }
1261
1262 if (Entry != 0)
1263 {
1264 /* Dirtify it if it's a resident page and we're asked to */
1265 if (SetDirty && !IS_SWAP_FROM_SSE(Entry))
1267 continue;
1268 }
1269
1270 ToReadPageBits |= 1UL << ((ChunkOffset - RangeStart) >> PAGE_SHIFT);
1271
1272 /* Put a wait entry here */
1273 MmSetPageEntrySectionSegment(Segment, &CurrentOffset, MAKE_SWAP_SSE(MM_WAIT_ENTRY));
1274 }
1276
1277 if (ToReadPageBits == 0)
1278 {
1279 /* Nothing to do for this chunk */
1280 continue;
1281 }
1282
1283 /* Now perform the actual read */
1284 LONGLONG ChunkOffset = RangeStart;
1285 while (ChunkOffset < ChunkEnd)
1286 {
1287 /* Move forward if there is a hole */
1288 ULONG BitSet;
1289 if (!_BitScanForward(&BitSet, ToReadPageBits))
1290 {
1291 /* Nothing more to read */
1292 break;
1293 }
1294 ToReadPageBits >>= BitSet;
1295 ChunkOffset += BitSet * PAGE_SIZE;
1296 ASSERT(ChunkOffset < ChunkEnd);
1297
1298 /* Get the range we have to read */
1299 _BitScanForward(&BitSet, ~ToReadPageBits);
1300 ULONG ReadLength = BitSet * PAGE_SIZE;
1301
1303
1304 /* Clamp (This is for image mappings */
1305 if ((ChunkOffset + ReadLength) > ChunkEnd)
1306 ReadLength = ChunkEnd - ChunkOffset;
1307
1308 ASSERT(ReadLength != 0);
1309
1310 /* Allocate a MDL */
1312 if (!Mdl)
1313 {
1314 /* Damn. Roll-back. */
1316 while (ChunkOffset < ChunkEnd)
1317 {
1318 if (ToReadPageBits & 1)
1319 {
1320 LARGE_INTEGER CurrentOffset;
1321 CurrentOffset.QuadPart = ChunkOffset;
1323 MmSetPageEntrySectionSegment(Segment, &CurrentOffset, 0);
1324 }
1325 ToReadPageBits >>= 1;
1326 ChunkOffset += PAGE_SIZE;
1327 }
1330 }
1331
1332 /* Get our pages */
1335 for (UINT i = 0; i < BYTES_TO_PAGES(ReadLength); i++)
1336 {
1338 if (!NT_SUCCESS(Status))
1339 {
1340 /* Damn. Roll-back. */
1341 for (UINT j = 0; j < i; j++)
1343 goto Failed;
1344 }
1345 }
1346
1347 Mdl->MdlFlags |= MDL_PAGES_LOCKED | MDL_IO_PAGE_READ;
1348
1350 FileOffset.QuadPart = Segment->Image.FileOffset + ChunkOffset;
1351
1352 /* Clamp to VDL */
1353 if (ValidDataLength && ((FileOffset.QuadPart + ReadLength) > ValidDataLength->QuadPart))
1354 {
1355 if (FileOffset.QuadPart > ValidDataLength->QuadPart)
1356 {
1357 /* Great, nothing to read. */
1358 goto AssignPagesToSegment;
1359 }
1360
1361 Mdl->Size = (FileOffset.QuadPart + ReadLength) - ValidDataLength->QuadPart;
1362 }
1363
1364 KEVENT Event;
1366
1367 /* Disable APCs */
1368 KIRQL OldIrql;
1370
1373 if (Status == STATUS_PENDING)
1374 {
1376 Status = Iosb.Status;
1377 }
1378
1379 if (Mdl->MdlFlags & MDL_MAPPED_TO_SYSTEM_VA)
1380 {
1381 MmUnmapLockedPages(Mdl->MappedSystemVa, Mdl);
1382 }
1383
1385
1387 {
1388 DPRINT1("Got STATUS_END_OF_FILE at offset %I64d for file %wZ.\n", FileOffset.QuadPart, &FileObject->FileName);
1390 }
1391
1392 if (!NT_SUCCESS(Status))
1393 {
1394 /* Damn. Roll back. */
1395 for (UINT i = 0; i < BYTES_TO_PAGES(ReadLength); i++)
1397
1398Failed:
1400 while (ChunkOffset < ChunkEnd)
1401 {
1402 if (ToReadPageBits & 1)
1403 {
1404 LARGE_INTEGER CurrentOffset;
1405 CurrentOffset.QuadPart = ChunkOffset;
1407 MmSetPageEntrySectionSegment(Segment, &CurrentOffset, 0);
1408 }
1409 ToReadPageBits >>= 1;
1410 ChunkOffset += PAGE_SIZE;
1411 }
1413 IoFreeMdl(Mdl);;
1414 return Status;
1415 }
1416
1417AssignPagesToSegment:
1419
1420 for (UINT i = 0; i < BYTES_TO_PAGES(ReadLength); i++)
1421 {
1422 ULONG_PTR Entry = MAKE_SSE(Pages[i] << PAGE_SHIFT, 0);
1423 LARGE_INTEGER CurrentOffset;
1424 CurrentOffset.QuadPart = ChunkOffset + (i * PAGE_SIZE);
1425
1427
1428 if (SetDirty)
1430
1431 MmSetPageEntrySectionSegment(Segment, &CurrentOffset, Entry);
1432 }
1433
1435
1436 IoFreeMdl(Mdl);
1437 ToReadPageBits >>= BitSet;
1438 ChunkOffset += BitSet * PAGE_SIZE;
1439 }
1440 }
1441
1442 return STATUS_SUCCESS;
1443}
ULONG ReadLength
#define IoFreeMdl
Definition: fxmdl.h:89
#define IoAllocateMdl
Definition: fxmdl.h:88
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask)
Definition: intrin_arm.h:57
NTSTATUS NTAPI IoPageRead(IN PFILE_OBJECT FileObject, IN PMDL Mdl, IN PLARGE_INTEGER Offset, IN PKEVENT Event, IN PIO_STATUS_BLOCK StatusBlock)
Definition: iofunc.c:1201
unsigned int UINT
Definition: ndis.h:50
@ Failed
Definition: arc.h:79
ULONG * PPFN_NUMBER
Definition: ke.h:9
#define FO_RANDOM_ACCESS
Definition: iotypes.h:1796
@ WrPageIn
Definition: ketypes.h:424
#define BYTES_TO_PAGES(Size)
#define MmGetMdlPfnArray(_Mdl)
#define MDL_IO_PAGE_READ
Definition: mmtypes.h:24

Referenced by MmMakeDataSectionResident(), and MmNotPresentFaultSectionView().

◆ MmMapViewInSystemSpace()

NTSTATUS NTAPI MmMapViewInSystemSpace ( IN PVOID  SectionObject,
OUT PVOID MappedBase,
IN OUT PSIZE_T  ViewSize 
)

Definition at line 4447 of file section.c.

4450{
4452
4453 SectionOffset.QuadPart = 0;
4454
4456}
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:407
NTSTATUS NTAPI MmMapViewInSystemSpaceEx(_In_ PVOID SectionObject, _Outptr_result_bytebuffer_(*ViewSize) PVOID *MappedBase, _Inout_ PSIZE_T ViewSize, _Inout_ PLARGE_INTEGER SectionOffset, _In_ ULONG_PTR Flags)
Definition: section.c:4460

Referenced by ExpInitNls(), IntGdiAddFontResourceEx(), and MmMapViewInSessionSpace().

◆ MmMapViewInSystemSpaceEx()

NTSTATUS NTAPI MmMapViewInSystemSpaceEx ( _In_ PVOID  SectionObject,
_Outptr_result_bytebuffer_ *ViewSize PVOID MappedBase,
_Inout_ PSIZE_T  ViewSize,
_Inout_ PLARGE_INTEGER  SectionOffset,
_In_ ULONG_PTR  Flags 
)

Definition at line 4460 of file section.c.

4467{
4468 PSECTION Section = SectionObject;
4472
4474
4475 PAGED_CODE();
4476
4478 {
4480 &MmSession,
4481 MappedBase,
4482 ViewSize,
4484 }
4485
4486 DPRINT("MmMapViewInSystemSpaceEx() called\n");
4487
4488 /* unsupported for now */
4489 ASSERT(Section->u.Flags.Image == 0);
4490
4491 Section = SectionObject;
4493
4494 if (*ViewSize == 0)
4495 {
4496 LONGLONG MapSizeLL;
4497
4498 /* Page-align the mapping */
4499 SectionOffset->LowPart = PAGE_ROUND_DOWN(SectionOffset->LowPart);
4500
4501 if (!NT_SUCCESS(RtlLongLongSub(Section->SizeOfSection.QuadPart, SectionOffset->QuadPart, &MapSizeLL)))
4503
4504 if (!NT_SUCCESS(RtlLongLongToSIZET(MapSizeLL, ViewSize)))
4506 }
4507 else
4508 {
4509 LONGLONG HelperLL;
4510
4511 /* Get the map end */
4512 if (!NT_SUCCESS(RtlLongLongAdd(SectionOffset->QuadPart, *ViewSize, &HelperLL)))
4514
4515 /* Round it up, if needed */
4516 if (HelperLL % PAGE_SIZE)
4517 {
4518 if (!NT_SUCCESS(RtlLongLongAdd(HelperLL, PAGE_SIZE - (HelperLL % PAGE_SIZE), &HelperLL)))
4520 }
4521
4522 /* Now that we have the mapping end, we can align down its start */
4523 SectionOffset->LowPart = PAGE_ROUND_DOWN(SectionOffset->LowPart);
4524
4525 /* Get the new size */
4526 if (!NT_SUCCESS(RtlLongLongSub(HelperLL, SectionOffset->QuadPart, &HelperLL)))
4528
4529 if (!NT_SUCCESS(RtlLongLongToSIZET(HelperLL, ViewSize)))
4531 }
4532
4534
4536
4538
4540 Section->u.Flags.Image,
4541 Segment,
4542 MappedBase,
4543 *ViewSize,
4545 SectionOffset->QuadPart,
4546 SEC_RESERVE);
4547
4550
4551 return Status;
4552}
NTSTATUS MiMapViewInSystemSpace(_In_ PVOID Section, _In_ PMMSESSION Session, _Outptr_result_bytebuffer_(*ViewSize) PVOID *MappedBase, _Inout_ PSIZE_T ViewSize, _Inout_ PLARGE_INTEGER SectionOffset)
Definition: section.c:1040
MMSESSION MmSession
Definition: section.c:107
#define PAGED_CODE()
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
#define STATUS_INVALID_VIEW_SIZE
Definition: ntstatus.h:268
static NTSTATUS MmMapViewOfSegment(PMMSUPPORT AddressSpace, BOOLEAN AsImage, PMM_SECTION_SEGMENT Segment, PVOID *BaseAddress, SIZE_T ViewSize, ULONG Protect, LONGLONG ViewOffset, ULONG AllocationType)
Definition: section.c:3362

Referenced by CcRosCreateVacb(), and MmMapViewInSystemSpace().

◆ MmMapViewOfArm3Section()

NTSTATUS NTAPI MmMapViewOfArm3Section ( IN PVOID  SectionObject,
IN PEPROCESS  Process,
IN OUT PVOID BaseAddress,
IN ULONG_PTR  ZeroBits,
IN SIZE_T  CommitSize,
IN OUT PLARGE_INTEGER SectionOffset  OPTIONAL,
IN OUT PSIZE_T  ViewSize,
IN SECTION_INHERIT  InheritDisposition,
IN ULONG  AllocationType,
IN ULONG  Protect 
)

Definition at line 2896 of file section.c.

2906{
2909 PSECTION Section;
2910 PCONTROL_AREA ControlArea;
2911 ULONG ProtectionMask;
2913 ULONG64 CalculatedViewSize;
2914 PAGED_CODE();
2915
2916 /* Get the segment and control area */
2917 Section = (PSECTION)SectionObject;
2918 ControlArea = Section->Segment->ControlArea;
2919
2920 /* These flags/states are not yet supported by ARM3 */
2921 ASSERT(Section->u.Flags.Image == 0);
2922 ASSERT(Section->u.Flags.NoCache == 0);
2923 ASSERT(Section->u.Flags.WriteCombined == 0);
2924 ASSERT(ControlArea->u.Flags.PhysicalMemory == 0);
2925
2926 /* FIXME */
2927 if ((AllocationType & MEM_RESERVE) != 0)
2928 {
2929 DPRINT1("MmMapViewOfArm3Section called with MEM_RESERVE, this is not implemented yet!!!\n");
2931 }
2932
2933 /* Check if the mapping protection is compatible with the create */
2935 {
2936 DPRINT1("Mapping protection is incompatible\n");
2938 }
2939
2940 /* Check if the offset and size would cause an overflow */
2941 if (((ULONG64)SectionOffset->QuadPart + *ViewSize) <
2942 (ULONG64)SectionOffset->QuadPart)
2943 {
2944 DPRINT1("Section offset overflows\n");
2946 }
2947
2948 /* Check if the offset and size are bigger than the section itself */
2949 if (((ULONG64)SectionOffset->QuadPart + *ViewSize) >
2950 (ULONG64)Section->SizeOfSection.QuadPart)
2951 {
2952 DPRINT1("Section offset is larger than section\n");
2954 }
2955
2956 /* Check if the caller did not specify a view size */
2957 if (!(*ViewSize))
2958 {
2959 /* Compute it for the caller */
2960 CalculatedViewSize = Section->SizeOfSection.QuadPart -
2961 SectionOffset->QuadPart;
2962
2963 /* Check if it's larger than 4GB or overflows into kernel-mode */
2964 if (!NT_SUCCESS(RtlULongLongToSIZET(CalculatedViewSize, ViewSize)) ||
2965 (((ULONG_PTR)MM_HIGHEST_VAD_ADDRESS - (ULONG_PTR)*BaseAddress) < CalculatedViewSize))
2966 {
2967 DPRINT1("Section view won't fit\n");
2969 }
2970 }
2971
2972 /* Check if the commit size is larger than the view size */
2973 if (CommitSize > *ViewSize)
2974 {
2975 DPRINT1("Attempting to commit more than the view itself\n");
2977 }
2978
2979 /* Check if the view size is larger than the section */
2980 if (*ViewSize > (ULONG64)Section->SizeOfSection.QuadPart)
2981 {
2982 DPRINT1("The view is larger than the section\n");
2984 }
2985
2986 /* Compute and validate the protection mask */
2987 ProtectionMask = MiMakeProtectionMask(Protect);
2988 if (ProtectionMask == MM_INVALID_PROTECTION)
2989 {
2990 DPRINT1("The protection is invalid\n");
2992 }
2993
2994 /* We only handle pagefile-backed sections, which cannot be writecombined */
2996 {
2997 DPRINT1("Cannot write combine a pagefile-backed section\n");
2999 }
3000
3001 /* Start by attaching to the current process if needed */
3003 {
3005 Attached = TRUE;
3006 }
3007
3008 /* Do the actual mapping */
3009 Status = MiMapViewOfDataSection(ControlArea,
3010 Process,
3013 ViewSize,
3014 Section,
3016 ProtectionMask,
3017 CommitSize,
3018 ZeroBits,
3020
3021 /* Detach if needed, then return status */
3023 return Status;
3024}
BOOLEAN NTAPI MiIsProtectionCompatible(IN ULONG SectionPageProtection, IN ULONG NewSectionPageProtection)
Definition: section.c:117
NTSTATUS NTAPI MiMapViewOfDataSection(IN PCONTROL_AREA ControlArea, IN PEPROCESS Process, IN PVOID *BaseAddress, IN PLARGE_INTEGER SectionOffset, IN PSIZE_T ViewSize, IN PSECTION Section, IN SECTION_INHERIT InheritDisposition, IN ULONG ProtectionMask, IN SIZE_T CommitSize, IN ULONG_PTR ZeroBits, IN ULONG AllocationType)
Definition: section.c:1264
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT InheritDisposition
Definition: mmfuncs.h:409
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR ZeroBits
Definition: mmfuncs.h:405
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG AllocationType
Definition: mmfuncs.h:410
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T CommitSize
Definition: mmfuncs.h:406
struct _SECTION * PSECTION
#define MEM_RESERVE
Definition: nt_native.h:1314
#define MM_HIGHEST_VAD_ADDRESS
Definition: mm.h:46
_Out_ PKAPC_STATE ApcState
Definition: mm.h:1765
#define STATUS_SECTION_PROTECTION
Definition: ntstatus.h:314
#define STATUS_INVALID_PARAMETER_10
Definition: ntstatus.h:484
#define STATUS_INVALID_PARAMETER_5
Definition: ntstatus.h:479
VOID NTAPI KeStackAttachProcess(IN PKPROCESS Process, OUT PRKAPC_STATE ApcState)
Definition: procobj.c:704
VOID NTAPI KeUnstackDetachProcess(IN PRKAPC_STATE ApcState)
Definition: procobj.c:756
ULONG WriteCombined
Definition: mmtypes.h:493
static BOOL Attached
Definition: vidbios.c:3905
KAPC_STATE
Definition: ketypes.h:1409

Referenced by MmMapViewOfSection().

◆ MmMapViewOfSection()

NTSTATUS NTAPI MmMapViewOfSection ( IN PVOID  SectionObject,
IN PEPROCESS  Process,
IN OUT PVOID BaseAddress,
IN ULONG_PTR  ZeroBits,
IN SIZE_T  CommitSize,
IN OUT PLARGE_INTEGER SectionOffset  OPTIONAL,
IN OUT PSIZE_T  ViewSize,
IN SECTION_INHERIT  InheritDisposition,
IN ULONG  AllocationType,
IN ULONG  Protect 
)

Definition at line 3996 of file section.c.

4006{
4007 PSECTION Section;
4010 BOOLEAN NotAtBase = FALSE;
4011 BOOLEAN IsAttached = FALSE;
4013
4015 {
4016 DPRINT("Mapping ARM3 section into %s\n", Process->ImageFileName);
4018 Process,
4020 ZeroBits,
4021 CommitSize,
4023 ViewSize,
4026 Protect);
4027 }
4028
4029 ASSERT(Process);
4030