POBJECT_TYPE MmSectionObjectType = NULL;
+BOOLEAN MmAllocationFragment;
+
+ULONG_PTR MmSubsectionBase;
+
static GENERIC_MAPPING MmpSectionMapping = {
STANDARD_RIGHTS_READ | SECTION_MAP_READ | SECTION_QUERY,
STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE,
/* Allocate memory for our structure */
ObjectNameInfo = ExAllocatePoolWithTag(PagedPool,
1024,
- TAG('M', 'm', ' ', ' '));
+ ' mM');
if (!ObjectNameInfo) return STATUS_NO_MEMORY;
/* Query the name */
if (!NT_SUCCESS(Status))
{
/* Failed, free memory */
- ExFreePoolWithTag(ObjectNameInfo, TAG('M', 'm', ' ', ' '));
+ ExFreePoolWithTag(ObjectNameInfo, ' mM');
return Status;
}
{
PROS_SECTION_OBJECT Section;
PMEMORY_AREA MemoryArea;
- PMM_AVL_TABLE AddressSpace;
+ PMMSUPPORT AddressSpace;
POBJECT_NAME_INFORMATION ModuleNameInformation;
NTSTATUS Status = STATUS_ADDRESS_NOT_ASSOCIATED;
}
else
{
- AddressSpace = &PsGetCurrentProcess()->VadRoot;
+ AddressSpace = &PsGetCurrentProcess()->Vm;
}
/* Lock address space */
ModuleNameInformation->Name.Buffer);
/* Free temp taged buffer from MmGetFileNameForSection() */
- ExFreePoolWithTag(ModuleNameInformation, TAG('M', 'm', ' ', ' '));
+ ExFreePoolWithTag(ModuleNameInformation, ' mM');
DPRINT("Found ModuleName %S by address %p\n",
ModuleName->Buffer,Address);
}
PFILE_OBJECT FileObject;
PBCB Bcb;
SWAPENTRY SavedSwapEntry;
- PFN_TYPE Page;
+ PFN_NUMBER Page;
BOOLEAN IsImageSection;
ULONG FileOffset;
return FALSE;
}
+NTSTATUS
+NTAPI
+MiCopyFromUserPage(PFN_NUMBER DestPage, PVOID SourceAddress)
+{
+ PEPROCESS Process;
+ KIRQL Irql;
+ PVOID TempAddress;
+
+ Process = PsGetCurrentProcess();
+ TempAddress = MiMapPageInHyperSpace(Process, DestPage, &Irql);
+ if (TempAddress == NULL)
+ {
+ return(STATUS_NO_MEMORY);
+ }
+ memcpy(TempAddress, SourceAddress, PAGE_SIZE);
+ MiUnmapPageInHyperSpace(Process, TempAddress, Irql);
+ return(STATUS_SUCCESS);
+}
+
NTSTATUS
NTAPI
MiReadPage(PMEMORY_AREA MemoryArea,
ULONG SegOffset,
- PPFN_TYPE Page)
+ PPFN_NUMBER Page)
/*
* FUNCTION: Read a page for a section backed memory area.
* PARAMETERS:
NTSTATUS
NTAPI
-MmNotPresentFaultSectionView(PMM_AVL_TABLE AddressSpace,
+MmNotPresentFaultSectionView(PMMSUPPORT AddressSpace,
MEMORY_AREA* MemoryArea,
PVOID Address,
BOOLEAN Locked)
{
ULONG Offset;
- PFN_TYPE Page;
+ PFN_NUMBER Page;
NTSTATUS Status;
PVOID PAddress;
PROS_SECTION_OBJECT Section;
*/
if (MmIsPagePresent(Process, Address))
{
- if (Locked)
- {
- MmLockPage(MmGetPfnForProcess(Process, Address));
- }
return(STATUS_SUCCESS);
}
}
MmInsertRmap(Page, Process, (PVOID)PAddress);
}
- if (Locked)
- {
- MmLockPage(Page);
- }
MmUnlockSectionSegment(Segment);
PageOp->Status = STATUS_SUCCESS;
MmspCompleteAndReleasePageOp(PageOp);
/*
* Finish the operation
*/
- if (Locked)
- {
- MmLockPage(Page);
- }
PageOp->Status = STATUS_SUCCESS;
MmspCompleteAndReleasePageOp(PageOp);
DPRINT("Address 0x%.8X\n", Address);
KeBugCheck(MEMORY_MANAGEMENT);
return(Status);
}
- /*
- * Don't add an rmap entry since the page mapped could be for
- * anything.
- */
- if (Locked)
- {
- MmLockPageUnsafe(Page);
- }
/*
* Cleanup and release locks
return(Status);
}
MmInsertRmap(Page, Process, (PVOID)PAddress);
- if (Locked)
- {
- MmLockPage(Page);
- }
/*
* Cleanup and release locks
}
MmInsertRmap(Page, Process, (PVOID)PAddress);
- if (Locked)
- {
- MmLockPage(Page);
- }
PageOp->Status = STATUS_SUCCESS;
MmspCompleteAndReleasePageOp(PageOp);
DPRINT("Address 0x%.8X\n", Address);
KeBugCheck(MEMORY_MANAGEMENT);
}
MmInsertRmap(Page, Process, (PVOID)PAddress);
- if (Locked)
- {
- MmLockPage(Page);
- }
PageOp->Status = STATUS_SUCCESS;
MmspCompleteAndReleasePageOp(PageOp);
DPRINT("Address 0x%.8X\n", Address);
KeBugCheck(MEMORY_MANAGEMENT);
}
MmInsertRmap(Page, Process, (PVOID)PAddress);
- if (Locked)
- {
- MmLockPage(Page);
- }
PageOp->Status = STATUS_SUCCESS;
MmspCompleteAndReleasePageOp(PageOp);
DPRINT("Address 0x%.8X\n", Address);
NTSTATUS
NTAPI
-MmAccessFaultSectionView(PMM_AVL_TABLE AddressSpace,
+MmAccessFaultSectionView(PMMSUPPORT AddressSpace,
MEMORY_AREA* MemoryArea,
PVOID Address,
BOOLEAN Locked)
{
PMM_SECTION_SEGMENT Segment;
PROS_SECTION_OBJECT Section;
- PFN_TYPE OldPage;
- PFN_TYPE NewPage;
+ PFN_NUMBER OldPage;
+ PFN_NUMBER NewPage;
NTSTATUS Status;
PVOID PAddress;
ULONG Offset;
DPRINT1("Unable to create virtual mapping\n");
KeBugCheck(MEMORY_MANAGEMENT);
}
- if (Locked)
- {
- MmLockPage(NewPage);
- MmUnlockPage(OldPage);
- }
/*
* Unshare the old page.
{
MM_SECTION_PAGEOUT_CONTEXT* PageOutContext;
BOOLEAN WasDirty;
- PFN_TYPE Page;
+ PFN_NUMBER Page;
PageOutContext = (MM_SECTION_PAGEOUT_CONTEXT*)Context;
if (Process)
{
- MmLockAddressSpace(&Process->VadRoot);
+ MmLockAddressSpace(&Process->Vm);
}
MmDeleteVirtualMapping(Process,
}
if (Process)
{
- MmUnlockAddressSpace(&Process->VadRoot);
+ MmUnlockAddressSpace(&Process->Vm);
}
if (PageOutContext->Private)
NTSTATUS
NTAPI
-MmPageOutSectionView(PMM_AVL_TABLE AddressSpace,
+MmPageOutSectionView(PMMSUPPORT AddressSpace,
MEMORY_AREA* MemoryArea,
PVOID Address,
PMM_PAGEOP PageOp)
{
- PFN_TYPE Page;
+ PFN_NUMBER Page;
MM_SECTION_PAGEOUT_CONTEXT Context;
SWAPENTRY SwapEntry;
ULONG Entry;
BOOLEAN DirectMapped;
BOOLEAN IsImageSection;
PEPROCESS Process = MmGetAddressSpaceOwner(AddressSpace);
+ KIRQL OldIrql;
Address = (PVOID)PAGE_ROUND_DOWN(Address);
}
else
{
+ OldIrql = KeAcquireQueuedSpinLock(LockQueuePfnLock);
MmReferencePage(Page);
+ KeReleaseQueuedSpinLock(LockQueuePfnLock, OldIrql);
}
MmDeleteAllRmaps(Page, (PVOID)&Context, MmPageOutDeleteMapping);
NTSTATUS
NTAPI
-MmWritePageSectionView(PMM_AVL_TABLE AddressSpace,
+MmWritePageSectionView(PMMSUPPORT AddressSpace,
PMEMORY_AREA MemoryArea,
PVOID Address,
PMM_PAGEOP PageOp)
ULONG Offset;
PROS_SECTION_OBJECT Section;
PMM_SECTION_SEGMENT Segment;
- PFN_TYPE Page;
+ PFN_NUMBER Page;
SWAPENTRY SwapEntry;
ULONG Entry;
BOOLEAN Private;
}
static VOID
-MmAlterViewAttributes(PMM_AVL_TABLE AddressSpace,
+MmAlterViewAttributes(PMMSUPPORT AddressSpace,
PVOID BaseAddress,
ULONG RegionSize,
ULONG OldType,
{
ULONG Offset;
ULONG Entry;
- PFN_TYPE Page;
+ PFN_NUMBER Page;
Offset = (ULONG_PTR)Address - (ULONG_PTR)MemoryArea->StartingAddress
+ MemoryArea->Data.SectionData.ViewOffset;
NTSTATUS
NTAPI
-MmProtectSectionView(PMM_AVL_TABLE AddressSpace,
+MmProtectSectionView(PMMSUPPORT AddressSpace,
PMEMORY_AREA MemoryArea,
PVOID BaseAddress,
ULONG Length,
ULONG Offset;
ULONG Entry;
ULONG SavedSwapEntry;
- PFN_TYPE Page;
+ PFN_NUMBER Page;
Page = 0;
ObjectTypeInitializer.CloseProcedure = MmpCloseSection;
ObjectTypeInitializer.ValidAccessMask = SECTION_ALL_ACCESS;
ObCreateObjectType(&Name, &ObjectTypeInitializer, NULL, &MmSectionObjectType);
+
+ MmCreatePhysicalMemorySection();
return(STATUS_SUCCESS);
}
/*
* Initialize it
*/
+ RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
Section->SectionPageProtection = SectionPageProtection;
Section->AllocationAttributes = AllocationAttributes;
- Section->Segment = NULL;
- Section->FileObject = NULL;
Section->MaximumSize = MaximumSize;
Segment = ExAllocatePoolWithTag(NonPagedPool, sizeof(MM_SECTION_SEGMENT),
TAG_MM_SECTION_SEGMENT);
/*
* Initialize it
*/
+ RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
Section->SectionPageProtection = SectionPageProtection;
Section->AllocationAttributes = AllocationAttributes;
- Section->Segment = NULL;
/*
* Check file access required
*/
Buffer = ExAllocatePoolWithTag(PagedPool,
BufferSize,
- TAG('M', 'm', 'X', 'r'));
+ 'rXmM');
+ if (!Buffer)
+ {
+ KeBugCheck(MEMORY_MANAGEMENT);
+ }
UsedSize = 0;
}
else
{
- ExFreePoolWithTag(Buffer, TAG('M', 'm', 'X', 'r'));
+ ExFreePoolWithTag(Buffer, 'rXmM');
}
return Status;
break;
}
- ExFreePoolWithTag(FileHeaderBuffer, TAG('M', 'm', 'X', 'r'));
+ ExFreePoolWithTag(FileHeaderBuffer, 'rXmM');
/*
* No loader handled the format
/*
* Initialize it
*/
+ RtlZeroMemory(Section, sizeof(ROS_SECTION_OBJECT));
Section->SectionPageProtection = SectionPageProtection;
Section->AllocationAttributes = AllocationAttributes;
LARGE_INTEGER SafeMaximumSize;
PVOID SectionObject;
KPROCESSOR_MODE PreviousMode;
- NTSTATUS Status = STATUS_SUCCESS;
+ NTSTATUS Status;
PreviousMode = ExGetPreviousMode();
- if(MaximumSize != NULL && PreviousMode != KernelMode)
+ if(PreviousMode != KernelMode)
{
_SEH2_TRY
{
- /* make a copy on the stack */
- SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
- MaximumSize = &SafeMaximumSize;
+ if (MaximumSize != NULL)
+ {
+ /* make a copy on the stack */
+ SafeMaximumSize = ProbeForReadLargeInteger(MaximumSize);
+ MaximumSize = &SafeMaximumSize;
+ }
+ ProbeForWriteHandle(SectionHandle);
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
- Status = _SEH2_GetExceptionCode();
+ /* Return the exception code */
+ _SEH2_YIELD(return _SEH2_GetExceptionCode());
}
_SEH2_END;
-
- if(!NT_SUCCESS(Status))
- {
- return Status;
- }
}
Status = MmCreateSection(&SectionObject,
{
HANDLE hSection;
KPROCESSOR_MODE PreviousMode;
- NTSTATUS Status = STATUS_SUCCESS;
+ NTSTATUS Status;
PreviousMode = ExGetPreviousMode();
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
- Status = _SEH2_GetExceptionCode();
+ /* Return the exception code */
+ _SEH2_YIELD(return _SEH2_GetExceptionCode());
}
_SEH2_END;
-
- if(!NT_SUCCESS(Status))
- {
- return Status;
- }
}
Status = ObOpenObjectByName(ObjectAttributes,
}
static NTSTATUS
-MmMapViewOfSegment(PMM_AVL_TABLE AddressSpace,
+MmMapViewOfSegment(PMMSUPPORT AddressSpace,
PROS_SECTION_OBJECT Section,
PMM_SECTION_SEGMENT Segment,
PVOID* BaseAddress,
PROS_SECTION_OBJECT Section;
PEPROCESS Process;
KPROCESSOR_MODE PreviousMode;
- PMM_AVL_TABLE AddressSpace;
- NTSTATUS Status = STATUS_SUCCESS;
+ PMMSUPPORT AddressSpace;
+ NTSTATUS Status;
ULONG tmpProtect;
+ ACCESS_MASK DesiredAccess;
/*
* Check the protection
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
- Status = _SEH2_GetExceptionCode();
+ /* Return the exception code */
+ _SEH2_YIELD(return _SEH2_GetExceptionCode());
}
_SEH2_END;
-
- if(!NT_SUCCESS(Status))
- {
- return Status;
- }
}
else
{
return(Status);
}
- AddressSpace = &Process->VadRoot;
+ AddressSpace = &Process->Vm;
+
+ /* Convert NT Protection Attr to Access Mask */
+ if (Protect == PAGE_READONLY)
+ {
+ DesiredAccess = SECTION_MAP_READ;
+ }
+ else if (Protect == PAGE_READWRITE)
+ {
+ DesiredAccess = SECTION_MAP_WRITE;
+ }
+ else if (Protect == PAGE_WRITECOPY)
+ {
+ DesiredAccess = SECTION_QUERY;
+ }
+ /* FIXME: Handle other Protection Attributes. For now keep previous behavior */
+ else
+ {
+ DesiredAccess = SECTION_MAP_READ;
+ }
Status = ObReferenceObjectByHandle(SectionHandle,
- SECTION_MAP_READ,
+ DesiredAccess,
MmSectionObjectType,
PreviousMode,
(PVOID*)(PVOID)&Section,
static VOID
MmFreeSectionPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address,
- PFN_TYPE Page, SWAPENTRY SwapEntry, BOOLEAN Dirty)
+ PFN_NUMBER Page, SWAPENTRY SwapEntry, BOOLEAN Dirty)
{
ULONG Entry;
PFILE_OBJECT FileObject;
NTSTATUS Status;
PROS_SECTION_OBJECT Section;
PMM_SECTION_SEGMENT Segment;
- PMM_AVL_TABLE AddressSpace;
+ PMMSUPPORT AddressSpace;
PEPROCESS Process;
- AddressSpace = (PMM_AVL_TABLE)Context;
+ AddressSpace = (PMMSUPPORT)Context;
Process = MmGetAddressSpaceOwner(AddressSpace);
Address = (PVOID)PAGE_ROUND_DOWN(Address);
}
static NTSTATUS
-MmUnmapViewOfSegment(PMM_AVL_TABLE AddressSpace,
+MmUnmapViewOfSegment(PMMSUPPORT AddressSpace,
PVOID BaseAddress)
{
NTSTATUS Status;
{
NTSTATUS Status;
PMEMORY_AREA MemoryArea;
- PMM_AVL_TABLE AddressSpace;
+ PMMSUPPORT AddressSpace;
PROS_SECTION_OBJECT Section;
PMM_PAGEOP PageOp;
ULONG_PTR Offset;
ASSERT(Process);
- AddressSpace = &Process->VadRoot;
+ AddressSpace = &Process->Vm;
MmLockAddressSpace(AddressSpace);
MemoryArea = MmLocateMemoryAreaByAddress(AddressSpace,
Status = MmUnmapViewOfSegment(AddressSpace, BaseAddress);
}
+ MmUnlockAddressSpace(AddressSpace);
+
/* Notify debugger */
if (ImageBaseAddress) DbgkUnMapViewOfSection(ImageBaseAddress);
- MmUnlockAddressSpace(AddressSpace);
return(STATUS_SUCCESS);
}
{
PROS_SECTION_OBJECT Section;
KPROCESSOR_MODE PreviousMode;
- NTSTATUS Status = STATUS_SUCCESS;
+ NTSTATUS Status;
+ PAGED_CODE();
PreviousMode = ExGetPreviousMode();
LARGE_INTEGER SafeNewMaximumSize;
PROS_SECTION_OBJECT Section;
KPROCESSOR_MODE PreviousMode;
- NTSTATUS Status = STATUS_SUCCESS;
+ NTSTATUS Status;
PreviousMode = ExGetPreviousMode();
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
- Status = _SEH2_GetExceptionCode();
+ /* Return the exception code */
+ _SEH2_YIELD(return _SEH2_GetExceptionCode());
}
_SEH2_END;
-
- if(!NT_SUCCESS(Status))
- {
- return Status;
- }
}
Status = ObReferenceObjectByHandle(SectionHandle,
if (!(Section->AllocationAttributes & SEC_FILE))
{
- ObfDereferenceObject(Section);
+ ObDereferenceObject(Section);
return STATUS_INVALID_PARAMETER;
}
return STATUS_NOT_IMPLEMENTED;
}
-
-/**********************************************************************
- * NAME INTERNAL
- * MmAllocateSection@4
- *
- * DESCRIPTION
- *
- * ARGUMENTS
- * Length
- *
- * RETURN VALUE
- *
- * NOTE
- * Code taken from ntoskrnl/mm/special.c.
- *
- * REVISIONS
- */
-PVOID NTAPI
-MmAllocateSection (IN ULONG Length, PVOID BaseAddress)
-{
- PVOID Result;
- MEMORY_AREA* marea;
- NTSTATUS Status;
- PMM_AVL_TABLE AddressSpace;
- PHYSICAL_ADDRESS BoundaryAddressMultiple;
-
- DPRINT("MmAllocateSection(Length %x)\n",Length);
-
- BoundaryAddressMultiple.QuadPart = 0;
-
- AddressSpace = MmGetKernelAddressSpace();
- Result = BaseAddress;
- MmLockAddressSpace(AddressSpace);
- Status = MmCreateMemoryArea (AddressSpace,
- MEMORY_AREA_SYSTEM,
- &Result,
- Length,
- 0,
- &marea,
- FALSE,
- 0,
- BoundaryAddressMultiple);
- MmUnlockAddressSpace(AddressSpace);
-
- if (!NT_SUCCESS(Status))
- {
- return (NULL);
- }
- DPRINT("Result %p\n",Result);
-
- /* Create a virtual mapping for this memory area */
- MmMapMemoryArea(Result, Length, MC_NPPOOL, PAGE_READWRITE);
-
- return ((PVOID)Result);
-}
-
-
/**********************************************************************
* NAME EXPORTED
* MmMapViewOfSection
IN ULONG Protect)
{
PROS_SECTION_OBJECT Section;
- PMM_AVL_TABLE AddressSpace;
+ PMMSUPPORT AddressSpace;
ULONG ViewOffset;
NTSTATUS Status = STATUS_SUCCESS;
Section = (PROS_SECTION_OBJECT)SectionObject;
- AddressSpace = &Process->VadRoot;
+ AddressSpace = &Process->Vm;
AllocationType |= (Section->AllocationAttributes & SEC_NO_CHANGE);
(*ViewSize) = Section->MaximumSize.u.LowPart - ViewOffset;
}
+ *ViewSize = PAGE_ROUND_UP(*ViewSize);
+
MmLockSectionSegment(Section->Segment);
Status = MmMapViewOfSegment(AddressSpace,
Section,
/* Something must gone wrong
* how can we have a Section but no
* reference? */
- DPRINT1("ERROR: DataSectionObject without reference!\n");
+ DPRINT("ERROR: DataSectionObject without reference!\n");
}
}
- DPRINT1("FIXME: didn't check for outstanding write probes\n");
+ DPRINT("FIXME: didn't check for outstanding write probes\n");
return TRUE;
}
IN OUT PSIZE_T ViewSize)
{
PROS_SECTION_OBJECT Section;
- PMM_AVL_TABLE AddressSpace;
+ PMMSUPPORT AddressSpace;
NTSTATUS Status;
DPRINT("MmMapViewInSystemSpace() called\n");
NTSTATUS NTAPI
MmUnmapViewInSystemSpace (IN PVOID MappedBase)
{
- PMM_AVL_TABLE AddressSpace;
+ PMMSUPPORT AddressSpace;
NTSTATUS Status;
DPRINT("MmUnmapViewInSystemSpace() called\n");
return STATUS_NOT_IMPLEMENTED;
}
-/*
- * @unimplemented
- */
-NTSTATUS NTAPI
-MmSetBankedSection (ULONG Unknown0,
- ULONG Unknown1,
- ULONG Unknown2,
- ULONG Unknown3,
- ULONG Unknown4,
- ULONG Unknown5)
-{
- UNIMPLEMENTED;
- return (STATUS_NOT_IMPLEMENTED);
-}
-
-
/**********************************************************************
* NAME EXPORTED
* MmCreateSection@
* Check the protection
*/
Protection = SectionPageProtection & ~(PAGE_GUARD|PAGE_NOCACHE);
- if (Protection != PAGE_NOACCESS &&
- Protection != PAGE_READONLY &&
+ if (Protection != PAGE_READONLY &&
Protection != PAGE_READWRITE &&
Protection != PAGE_WRITECOPY &&
Protection != PAGE_EXECUTE &&
AllocationAttributes));
}
-NTSTATUS
-NTAPI
-NtAllocateUserPhysicalPages(IN HANDLE ProcessHandle,
- IN OUT PULONG_PTR NumberOfPages,
- IN OUT PULONG_PTR UserPfnArray)
-{
- UNIMPLEMENTED;
- return STATUS_NOT_IMPLEMENTED;
-}
-
-NTSTATUS
-NTAPI
-NtMapUserPhysicalPages(IN PVOID VirtualAddresses,
- IN ULONG_PTR NumberOfPages,
- IN OUT PULONG_PTR UserPfnArray)
-{
- UNIMPLEMENTED;
- return STATUS_NOT_IMPLEMENTED;
-}
-
-NTSTATUS
-NTAPI
-NtMapUserPhysicalPagesScatter(IN PVOID *VirtualAddresses,
- IN ULONG_PTR NumberOfPages,
- IN OUT PULONG_PTR UserPfnArray)
-{
- UNIMPLEMENTED;
- return STATUS_NOT_IMPLEMENTED;
-}
-
-NTSTATUS
-NTAPI
-NtFreeUserPhysicalPages(IN HANDLE ProcessHandle,
- IN OUT PULONG_PTR NumberOfPages,
- IN OUT PULONG_PTR UserPfnArray)
-{
- UNIMPLEMENTED;
- return STATUS_NOT_IMPLEMENTED;
-}
-
NTSTATUS
NTAPI
NtAreMappedFilesTheSame(IN PVOID File1MappedAsAnImage,