[ros-kernel] mdl patch
Vizzini
vizzini at reactos.com
Thu Jun 17 01:56:27 CEST 2004
I have a patch in my tree that is really old (~2 months) that somehow
got lost in the shuffle during my fdd work. I have cleaned it up and
applied it to my tree, but i thought i'd post it here for review. It
was originally sent to me by a guy named Tom (tmkpl at poczta.onet.pl).
-Vizzini
-------------- next part --------------
? patch
Index: cc/copy.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/cc/copy.c,v
retrieving revision 1.23
diff -u -r1.23 copy.c
--- cc/copy.c 6 Jun 2004 08:36:30 -0000 1.23
+++ cc/copy.c 17 Jun 2004 05:39:30 -0000
@@ -135,7 +135,12 @@
/*
* Create an MDL which contains all their pages.
*/
- MmInitializeMdl(Mdl, NULL, current_size);
+ Mdl = MmCreateMdl(NULL, NULL, current_size);
+ if(Mdl == NULL) {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
+
Mdl->MdlFlags |= (MDL_PAGES_LOCKED | MDL_IO_PAGE_READ);
current2 = current;
offset = 0;
@@ -218,8 +223,14 @@
{
Size = CacheSeg->Bcb->CacheSegmentSize;
}
- Mdl = alloca(MmSizeOfMdl(CacheSeg->BaseAddress, Size));
- MmInitializeMdl(Mdl, CacheSeg->BaseAddress, Size);
+
+ Mdl = MmCreateMdl(NULL, CacheSeg->BaseAddress, Size);
+ if(Mdl == NULL)
+ {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
+
MmBuildMdlForNonPagedPool(Mdl);
Mdl->MdlFlags |= MDL_IO_PAGE_READ;
KeInitializeEvent(&Event, NotificationEvent, FALSE);
@@ -260,8 +271,14 @@
{
Size = CacheSeg->Bcb->CacheSegmentSize;
}
- Mdl = alloca(MmSizeOfMdl(CacheSeg->BaseAddress, Size));
- MmInitializeMdl(Mdl, CacheSeg->BaseAddress, Size);
+
+ Mdl = MmCreateMdl(NULL, CacheSeg->BaseAddress, Size);
+ if(Mdl == NULL)
+ {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
+
MmBuildMdlForNonPagedPool(Mdl);
Mdl->MdlFlags |= MDL_IO_PAGE_READ;
KeInitializeEvent(&Event, NotificationEvent, FALSE);
Index: io/buildirp.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/io/buildirp.c,v
retrieving revision 1.39
diff -u -r1.39 buildirp.c
--- io/buildirp.c 4 Mar 2004 00:07:00 -0000 1.39
+++ io/buildirp.c 17 Jun 2004 05:39:30 -0000
@@ -56,6 +56,10 @@
DPRINT("Doing direct i/o\n");
Irp->MdlAddress = MmCreateMdl(NULL,Buffer,Length);
+ if(Irp->MdlAddress == NULL) {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
if (MajorFunction == IRP_MJ_READ)
{
MmProbeAndLockPages(Irp->MdlAddress,UserMode,IoWriteAccess);
@@ -298,6 +302,10 @@
FALSE,
FALSE,
Irp);
+ if(Irp->MdlAddress == NULL) {
+ IoFreeIrp(Irp);
+ return(NULL);
+ }
MmProbeAndLockPages (Irp->MdlAddress,UserMode,IoReadAccess);
}
break;
Index: mm/anonmem.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/mm/anonmem.c,v
retrieving revision 1.28
diff -u -r1.28 anonmem.c
--- mm/anonmem.c 6 Jun 2004 08:36:31 -0000 1.28
+++ mm/anonmem.c 17 Jun 2004 05:39:30 -0000
@@ -98,7 +98,18 @@
/*
* Write the page to the pagefile
*/
- Status = MmWriteToSwapPage(SwapEntry, &PhysicalAddress);
+ Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE);
+ if(Mdl == NULL) {
+ DPRINT("MmCreateMdl: Out of memory!");
+ MmSetDirtyPage(MemoryArea->Process, Address);
+ PageOp->Status = STATUS_UNSUCCESSFUL;
+ KeSetEvent(&PageOp->CompletionEvent, IO_NO_INCREMENT, FALSE);
+ MmReleasePageOp(PageOp);
+ return(STATUS_NO_MEMORY);
+ }
+
+ MmBuildMdlFromPages(Mdl, (PULONG)&PhysicalAddress);
+ Status = MmWriteToSwapPage(SwapEntry, Mdl);
if (!NT_SUCCESS(Status))
{
DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
@@ -196,7 +207,17 @@
/*
* Write the page to the pagefile
*/
- Status = MmWriteToSwapPage(SwapEntry, &PhysicalAddress);
+ Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE);
+ if(Mdl == NULL) {
+ DPRINT("MmCreateMdl: Out of memory!");
+ MmEnableVirtualMapping(MemoryArea->Process, Address);
+ PageOp->Status = STATUS_UNSUCCESSFUL;
+ KeSetEvent(&PageOp->CompletionEvent, IO_NO_INCREMENT, FALSE);
+ MmReleasePageOp(PageOp);
+ return(STATUS_NO_MEMORY);
+ }
+ MmBuildMdlFromPages(Mdl, (ULONG *)&PhysicalAddress.u.LowPart);
+ Status = MmWriteToSwapPage(SwapEntry, Mdl);
if (!NT_SUCCESS(Status))
{
DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
@@ -366,9 +387,16 @@
if (MmIsPageSwapEntry(NULL, Address))
{
SWAPENTRY SwapEntry;
+ PMDL Mdl;
MmDeletePageFileMapping(MemoryArea->Process, Address, &SwapEntry);
- Status = MmReadFromSwapPage(SwapEntry, &Page);
+ Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE);
+ if(Mdl == NULL) {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
+ MmBuildMdlFromPages(Mdl, (PULONG)&Page);
+ Status = MmReadFromSwapPage(SwapEntry, Mdl);
if (!NT_SUCCESS(Status))
{
KEBUGCHECK(0);
Index: mm/mpw.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/mm/mpw.c,v
retrieving revision 1.18
diff -u -r1.18 mpw.c
--- mm/mpw.c 10 Apr 2004 22:35:25 -0000 1.18
+++ mm/mpw.c 17 Jun 2004 05:39:30 -0000
@@ -46,6 +46,9 @@
/* FUNCTIONS *****************************************************************/
+/** Search through all LRU Pages and free max 'Target' pages
+ *
+ */
NTSTATUS STDCALL
MmWriteDirtyPages(ULONG Target, PULONG Actual)
{
@@ -74,6 +77,7 @@
if (NT_SUCCESS(Status))
{
Target--;
+ NextPage=MmGetLRUFirstUserPage(); // tMk - is this FIX correct?
}
}
Page = NextPage;
@@ -82,6 +86,10 @@
return(STATUS_SUCCESS);
}
+
+/** Infinite loop that writes changed 'paged memory' and 'cache memory' to the disk after waiting 'Timeout'
+ *
+ */
NTSTATUS STDCALL
MmMpwThreadMain(PVOID Ignored)
{
@@ -122,6 +130,10 @@
}
}
+
+/** Initialization of flushing 'dirty-pages' thread
+ *
+ */
NTSTATUS MmInitMpwThread(VOID)
{
KPRIORITY Priority;
Index: mm/pagefile.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/mm/pagefile.c,v
retrieving revision 1.47
diff -u -r1.47 pagefile.c
--- mm/pagefile.c 6 Jun 2004 09:13:21 -0000 1.47
+++ mm/pagefile.c 17 Jun 2004 05:39:30 -0000
@@ -376,18 +376,13 @@
{
if (!(PagingFile->AllocMap[i] & (1 << j)))
{
- break;
+ PagingFile->AllocMap[i] |= (1 << j);
+ PagingFile->UsedPages++;
+ PagingFile->FreePages--;
+ KeReleaseSpinLock(&PagingFile->AllocMapLock, oldIrql);
+ return((i * 32) + j);
}
}
- if (j == 32)
- {
- continue;
- }
- PagingFile->AllocMap[i] |= (1 << j);
- PagingFile->UsedPages++;
- PagingFile->FreePages--;
- KeReleaseSpinLock(&PagingFile->AllocMapLock, oldIrql);
- return((i * 32) + j);
}
KeReleaseSpinLock(&PagingFile->AllocMapLock, oldIrql);
@@ -403,6 +398,12 @@
i = FILE_FROM_ENTRY(Entry);
off = OFFSET_FROM_ENTRY(Entry);
+
+ if (i >= MAX_PAGING_FILES)
+ {
+ DPRINT1("Bad swap entry 0x%.8X\n", Entry);
+ KEBUGCHECK(0);
+ }
KeAcquireSpinLock(&PagingFileListLock, &oldIrql);
if (PagingFileList[i] == NULL)
@@ -410,9 +411,9 @@
KEBUGCHECK(0);
}
KeAcquireSpinLockAtDpcLevel(&PagingFileList[i]->AllocMapLock);
-
- PagingFileList[i]->AllocMap[off / 32] &= (~(1 << (off % 32)));
-
+
+ PagingFileList[i]->AllocMap[off >> 5] &= (~(1 << (off % 32)));
+
PagingFileList[i]->FreePages++;
PagingFileList[i]->UsedPages--;
@@ -685,6 +686,12 @@
FALSE,
&Event,
&Iosb);
+ if(Irp == NULL)
+ {
+ ObDereferenceObject(PageFile);
+ return(STATUS_NO_MEMORY);// tMk - is this correct return code ???
+ }
+
StackPtr = IoGetNextIrpStackLocation(Irp);
StackPtr->FileObject = PageFile;
StackPtr->DeviceObject = PageFileDevice;
Index: mm/section.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/mm/section.c,v
retrieving revision 1.151
diff -u -r1.151 section.c
--- mm/section.c 6 Jun 2004 08:36:31 -0000 1.151
+++ mm/section.c 17 Jun 2004 05:39:31 -0000
@@ -396,7 +396,16 @@
* process and the current segment (also not within an other process).
*/
NTSTATUS Status;
- Status = MmWriteToSwapPage(SavedSwapEntry, &Page);
+ PMDL Mdl;
+ Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE);
+ if(Mdl == NULL)
+ {
+ DPRINT("MmCreateMdl: Out of memory!");
+ KEBUGCHECK(0);
+ }
+
+ MmBuildMdlFromPages(Mdl, (PULONG)&Page);
+ Status = MmWriteToSwapPage(SavedSwapEntry, Mdl);
if (!NT_SUCCESS(Status))
{
DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n", Status);
@@ -789,6 +798,7 @@
* Must be private page we have swapped out.
*/
SWAPENTRY SwapEntry;
+ PMDL Mdl;
/*
* Sanity check
@@ -809,7 +819,15 @@
KEBUGCHECK(0);
}
- Status = MmReadFromSwapPage(SwapEntry, &Page);
+ Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE);
+ if(Mdl == NULL)
+ {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
+
+ MmBuildMdlFromPages(Mdl, (PULONG)&Page);
+ Status = MmReadFromSwapPage(SwapEntry, Mdl);
if (!NT_SUCCESS(Status))
{
DPRINT1("MmReadFromSwapPage failed, status = %x\n", Status);
@@ -1072,6 +1090,7 @@
else if (IS_SWAP_FROM_SSE(Entry))
{
SWAPENTRY SwapEntry;
+ PMDL Mdl;
SwapEntry = SWAPENTRY_FROM_SSE(Entry);
@@ -1088,7 +1107,14 @@
KEBUGCHECK(0);
}
- Status = MmReadFromSwapPage(SwapEntry, &Page);
+ Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE);
+ if(Mdl == NULL)
+ {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
+ MmBuildMdlFromPages(Mdl, (PULONG)&Page);
+ Status = MmReadFromSwapPage(SwapEntry, Mdl);
if (!NT_SUCCESS(Status))
{
KEBUGCHECK(0);
@@ -1685,7 +1711,14 @@
/*
* Write the page to the pagefile
*/
- Status = MmWriteToSwapPage(SwapEntry, &PhysicalAddress);
+ Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE);
+ if(Mdl == NULL)
+ {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
+ MmBuildMdlFromPages(Mdl, (PULONG)&PhysicalAddress);
+ Status = MmWriteToSwapPage(SwapEntry, Mdl);
if (!NT_SUCCESS(Status))
{
DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
@@ -1887,7 +1920,14 @@
/*
* Write the page to the pagefile
*/
- Status = MmWriteToSwapPage(SwapEntry, &PhysicalAddress);
+ Mdl = MmCreateMdl(NULL, NULL, PAGE_SIZE);
+ if(Mdl == NULL)
+ {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
+ MmBuildMdlFromPages(Mdl, (PULONG)&PhysicalAddress);
+ Status = MmWriteToSwapPage(SwapEntry, Mdl);
if (!NT_SUCCESS(Status))
{
DPRINT1("MM: Failed to write to swap page (Status was 0x%.8X)\n",
Index: mm/virtual.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/mm/virtual.c,v
retrieving revision 1.75
diff -u -r1.75 virtual.c
--- mm/virtual.c 13 Jun 2004 10:35:52 -0000 1.75
+++ mm/virtual.c 17 Jun 2004 05:39:31 -0000
@@ -59,6 +59,12 @@
return(STATUS_NOT_IMPLEMENTED);
}
+/* (tMk 2004.II.4)
+ * FUNCTION: Locks range of process virtual memory.
+ * Called from VirtualLock (lib\kernel32\mem\virtual.c)
+ *
+ * NOTE: This function will be correct if MmProbeAndLockPages() would be fully IMPLEMENTED.
+ */
NTSTATUS STDCALL
NtLockVirtualMemory(HANDLE ProcessHandle,
PVOID BaseAddress,
@@ -82,7 +88,7 @@
UserMode,
(PVOID*)(&Process),
NULL);
- if (Status != STATUS_SUCCESS)
+ if (!NT_SUCCESS(Status))
{
return(Status);
}
@@ -90,6 +96,11 @@
Mdl = MmCreateMdl(NULL,
BaseAddress,
NumberOfBytesToLock);
+ if(Mdl == NULL)
+ {
+ ObDereferenceObject(Process);
+ return(STATUS_NO_MEMORY);
+ }
MmProbeAndLockPages(Mdl,
UserMode,
IoWriteAccess);
@@ -102,7 +113,13 @@
return(STATUS_SUCCESS);
}
-NTSTATUS STDCALL
+
+/* (tMk 2004.II.4)
+ * FUNCTION:
+ * Called from VirtualQueryEx (lib\kernel32\mem\virtual.c)
+ *
+ */
+NTSTATUS STDCALL
NtQueryVirtualMemory (IN HANDLE ProcessHandle,
IN PVOID Address,
IN CINT VirtualMemoryInformationClass,
@@ -204,6 +221,12 @@
return(Status);
}
+
+/* (tMk 2004.II.5)
+ * FUNCTION:
+ * Called from VirtualProtectEx (lib\kernel32\mem\virtual.c)
+ *
+ */
NTSTATUS STDCALL
NtProtectVirtualMemory(IN HANDLE ProcessHandle,
IN PVOID *UnsafeBaseAddress,
@@ -226,6 +249,13 @@
if (!NT_SUCCESS(Status))
return Status;
+ // (tMk 2004.II.5) in Microsoft SDK I read:
+ // 'if this parameter is NULL or does not point to a valid variable, the function fails'
+ if(UnsafeOldAccessProtection == NULL)
+ {
+ return(STATUS_INVALID_PARAMETER);
+ }
+
NumberOfBytesToProtect =
PAGE_ROUND_UP(BaseAddress + NumberOfBytesToProtect) -
PAGE_ROUND_DOWN(BaseAddress);
@@ -279,6 +309,13 @@
return(Status);
}
+
+/* (tMk 2004.II.05)
+ * FUNCTION:
+ * Called from ReadProcessMemory (lib\kernel32\mem\procmem.c) and KlInitPeb(lib\kernel32\process\create.c)
+ *
+ * NOTE: This function will be correct if MmProbeAndLockPages() would be fully IMPLEMENTED.
+ */
NTSTATUS STDCALL
NtReadVirtualMemory(IN HANDLE ProcessHandle,
IN PVOID BaseAddress,
@@ -301,7 +338,7 @@
UserMode,
(PVOID*)(&Process),
NULL);
- if (Status != STATUS_SUCCESS)
+ if (!NT_SUCCESS(Status))
{
return(Status);
}
@@ -309,6 +346,11 @@
Mdl = MmCreateMdl(NULL,
Buffer,
NumberOfBytesToRead);
+ if(Mdl == NULL)
+ {
+ ObDereferenceObject(Process);
+ return(STATUS_NO_MEMORY);
+ }
MmProbeAndLockPages(Mdl,
UserMode,
IoWriteAccess);
@@ -331,10 +373,13 @@
if (NumberOfBytesRead)
*NumberOfBytesRead = NumberOfBytesToRead;
-
return(STATUS_SUCCESS);
}
+/* (tMk 2004.II.05)
+ * FUNCTION: THIS function doesn't make a sense...
+ * Called from VirtualUnlock (lib\kernel32\mem\virtual.c)
+ */
NTSTATUS STDCALL
NtUnlockVirtualMemory(HANDLE ProcessHandle,
PVOID BaseAddress,
@@ -358,7 +403,7 @@
UserMode,
(PVOID*)(&Process),
NULL);
- if (Status != STATUS_SUCCESS)
+ if (!NT_SUCCESS(Status))
{
return(Status);
}
@@ -366,6 +411,11 @@
Mdl = MmCreateMdl(NULL,
BaseAddress,
NumberOfBytesToUnlock);
+ if(Mdl == NULL)
+ {
+ ObDereferenceObject(Process);
+ return(STATUS_NO_MEMORY);
+ }
ObDereferenceObject(Process);
@@ -382,6 +432,12 @@
}
+/* (tMk 2004.II.05)
+ * FUNCTION:
+ * Called from WriteProcessMemory (lib\kernel32\mem\procmem.c) and KlInitPeb(lib\kernel32\process\create.c)
+ *
+ * NOTE: This function will be correct if MmProbeAndLockPages() would be fully IMPLEMENTED.
+ */
NTSTATUS STDCALL
NtWriteVirtualMemory(IN HANDLE ProcessHandle,
IN PVOID BaseAddress,
@@ -404,7 +460,7 @@
UserMode,
(PVOID*)(&Process),
NULL);
- if (Status != STATUS_SUCCESS)
+ if (!NT_SUCCESS(Status))
{
return(Status);
}
@@ -415,7 +471,11 @@
MmProbeAndLockPages(Mdl,
UserMode,
IoReadAccess);
-
+ if(Mdl == NULL)
+ {
+ ObDereferenceObject(Process);
+ return(STATUS_NO_MEMORY);
+ }
KeAttachProcess(Process);
SystemAddress = MmGetSystemAddressForMdl(Mdl);
@@ -437,7 +497,8 @@
return(STATUS_SUCCESS);
}
-/*
+/* FUNCTION:
+ * Called from EngSecureMem (subsys\win32k\eng\mem.c)
* @unimplemented
*/
PVOID STDCALL
@@ -457,7 +518,8 @@
}
-/*
+/* FUNCTION:
+ * Called from EngUnsecureMem (subsys\win32k\eng\mem.c)
* @unimplemented
*/
VOID STDCALL
Index: nt/profile.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/nt/profile.c,v
retrieving revision 1.15
diff -u -r1.15 profile.c
--- nt/profile.c 30 Dec 2003 18:52:05 -0000 1.15
+++ nt/profile.c 17 Jun 2004 05:39:31 -0000
@@ -482,6 +482,10 @@
Profile->Size = ImageSize;
Profile->BucketShift = Granularity;
Profile->BufferMdl = MmCreateMdl(NULL, Buffer, BufferSize);
+ if(Profile->BufferMdl == NULL) {
+ DPRINT("MmCreateMdl: Out of memory!");
+ return(STATUS_NO_MEMORY);
+ }
MmProbeAndLockPages(Profile->BufferMdl, UserMode, IoWriteAccess);
Profile->Buffer = MmGetSystemAddressForMdl(Profile->BufferMdl);
Profile->BufferSize = BufferSize;
Index: ps/idle.c
===================================================================
RCS file: /CVS/ReactOS/reactos/ntoskrnl/ps/idle.c,v
retrieving revision 1.24
diff -u -r1.24 idle.c
--- ps/idle.c 18 Apr 2004 00:50:53 -0000 1.24
+++ ps/idle.c 17 Jun 2004 05:39:31 -0000
@@ -26,6 +26,9 @@
/* FUNCTIONS *****************************************************************/
+/** System idle thread procedure
+ *
+ */
VOID STDCALL
PsIdleThreadMain(PVOID Context)
{
@@ -50,28 +53,46 @@
}
}
+
+/** Initialization of system idle thread
+ *
+ */
VOID INIT_FUNCTION
PsInitIdleThread(VOID)
{
KPRIORITY Priority;
ULONG Affinity;
-
- PsCreateSystemThread(&PsIdleThreadHandle,
+ NTSTATUS Status;
+
+ Status = PsCreateSystemThread(&PsIdleThreadHandle,
THREAD_ALL_ACCESS,
NULL,
NULL,
NULL,
PsIdleThreadMain,
NULL);
-
+ if(!NT_SUCCESS(Status)) {
+ DPRINT("Couldn't create Idle System Thread!");
+ KEBUGCHECK(0);
+ return;
+ }
+
Priority = LOW_PRIORITY;
- NtSetInformationThread(PsIdleThreadHandle,
+ Status = NtSetInformationThread(PsIdleThreadHandle,
ThreadPriority,
&Priority,
sizeof(Priority));
+ if(!NT_SUCCESS(Status)) {
+ DPRINT("Couldn't set Priority to Idle System Thread!");
+ return;
+ }
+
Affinity = 1 << 0;
- NtSetInformationThread(PsIdleThreadHandle,
+ Status = NtSetInformationThread(PsIdleThreadHandle,
ThreadAffinityMask,
&Affinity,
sizeof(Affinity));
+ if(!NT_SUCCESS(Status)) {
+ DPRINT("Couldn't set Affinity Mask to Idle System Thread!");
+ }
}
More information about the Ros-kernel
mailing list