Enhanced memory managment

Correct several bugs in the creation/termination of processes

svn path=/trunk/; revision=360
This commit is contained in:
David Welch 1999-04-01 12:39:43 +00:00
parent 240b550d9c
commit 63a3377143
23 changed files with 235 additions and 91 deletions

View file

@ -111,6 +111,7 @@ int ExecuteProcess(char* name, char* cmdline)
{ {
WaitForSingleObject(ProcessInformation.hProcess,INFINITE); WaitForSingleObject(ProcessInformation.hProcess,INFINITE);
} }
CloseHandle(ProcessInformation.hProcess);
return(ret); return(ret);
} }

View file

@ -11,6 +11,21 @@
//#define NDEBUG //#define NDEBUG
#include <internal/debug.h> #include <internal/debug.h>
#define IDMAP_BASE (0xd0000000)
/*
* Return a linear address which can be used to access the physical memory
* starting at x
*/
extern inline unsigned int physical_to_linear(unsigned int x)
{
return(x+IDMAP_BASE);
}
extern inline unsigned int linear_to_physical(unsigned int x)
{
return(x-IDMAP_BASE);
}
#define VIDMEM_BASE 0xb8000 #define VIDMEM_BASE 0xb8000

View file

@ -7,7 +7,7 @@
#ifdef COMPILER_LARGE_INTEGERS #ifdef COMPILER_LARGE_INTEGERS
#define GET_LARGE_INTEGER_HIGH_PART(LI) ( ( (LI) >> 32) ) #define GET_LARGE_INTEGER_HIGH_PART(LI) ( ( (LI) >> 32) )
#define GET_LARGE_INTEGER_LOW_PART(LI) ( ((LI) & 0xFFFFFFFF) ) #define GET_LARGE_INTEGER_LOW_PART(LI) (ULONG)( ((LI) & 0xFFFFFFFF) )
#define SET_LARGE_INTEGER_HIGH_PART(LI, HP) \ #define SET_LARGE_INTEGER_HIGH_PART(LI, HP) \
( (LI) = ((LI) & 0xFFFFFFFFL) | ( ((LARGE_INTEGER)(HP)) << 32 ) ) ( (LI) = ((LI) & 0xFFFFFFFFL) | ( ((LARGE_INTEGER)(HP)) << 32 ) )
#define SET_LARGE_INTEGER_LOW_PART(LI, LP) \ #define SET_LARGE_INTEGER_LOW_PART(LI, LP) \

View file

@ -6,10 +6,10 @@
#include <internal/hal.h> #include <internal/hal.h>
#ifndef TLS_MINIMUM_AVAILABLE #ifndef TLS_MINIMUM_AVAILABLE
#define TLS_MINIMUM_AVAILABLE (64) #define TLS_MINIMUM_AVAILABLE (64)
#endif #endif
#ifndef MAX_PATH #ifndef MAX_PATH
#define MAX_PATH (260) #define MAX_PATH (260)
#endif #endif
typedef NTSTATUS (*PKSTART_ROUTINE)(PVOID StartContext); typedef NTSTATUS (*PKSTART_ROUTINE)(PVOID StartContext);
@ -125,7 +125,6 @@ typedef struct _NT_TIB {
typedef struct _NT_TEB typedef struct _NT_TEB
{ {
NT_TIB Tib; NT_TIB Tib;
CLIENT_ID Cid; CLIENT_ID Cid;
HANDLE RPCHandle; HANDLE RPCHandle;
@ -136,7 +135,6 @@ typedef struct _NT_TEB
NTSTATUS LastStatusValue; NTSTATUS LastStatusValue;
DWORD LockCount; DWORD LockCount;
UCHAR HardErrorMode; UCHAR HardErrorMode;
} NT_TEB; } NT_TEB;
typedef struct _KTHREAD typedef struct _KTHREAD

View file

@ -0,0 +1,2 @@
VOID ExUnmapPage(PVOID Addr);
PVOID ExAllocatePage(VOID);

View file

@ -91,4 +91,6 @@ PVOID MmInitializePageList(PVOID FirstPhysKernelAddress,
PVOID MmAllocPage(VOID); PVOID MmAllocPage(VOID);
VOID MmFreePage(PVOID PhysicalAddress, ULONG Nr); VOID MmFreePage(PVOID PhysicalAddress, ULONG Nr);
VOID MmDeletePageTable(PEPROCESS Process, PVOID Address); VOID MmDeletePageTable(PEPROCESS Process, PVOID Address);
NTSTATUS MmCopyMmInfo(PEPROCESS Src, PEPROCESS Dest);
NTSTATUS MmReleaseMmInfo(PEPROCESS Process);
#endif #endif

View file

@ -111,5 +111,6 @@ NTSTATUS ObFindObject(POBJECT_ATTRIBUTES ObjectAttributes,
PVOID* ReturnedObject, PVOID* ReturnedObject,
PWSTR* RemainingPath); PWSTR* RemainingPath);
ULONG ObGetReferenceCount(PVOID Object);
#endif /* __INCLUDE_INTERNAL_OBJMGR_H */ #endif /* __INCLUDE_INTERNAL_OBJMGR_H */

View file

@ -57,5 +57,6 @@ NTSTATUS HalInitTask(PETHREAD thread, PKSTART_ROUTINE fn, PVOID StartContext);
void HalTaskSwitch(PKTHREAD thread); void HalTaskSwitch(PKTHREAD thread);
NTSTATUS HalInitTaskWithContext(PETHREAD Thread, PCONTEXT Context); NTSTATUS HalInitTaskWithContext(PETHREAD Thread, PCONTEXT Context);
NTSTATUS HalReleaseTask(PETHREAD Thread); NTSTATUS HalReleaseTask(PETHREAD Thread);
VOID PiDeleteProcess(PVOID ObjectBody);
#endif #endif

View file

@ -44,8 +44,7 @@ DeviceIoControl(
bFsIoControlCode = TRUE; bFsIoControlCode = TRUE;
else else
bFsIoControlCode = FALSE; bFsIoControlCode = FALSE;
// CHECKPOINT if(lpOverlapped != NULL) {
if(lpOverlapped != NULL) {
hEvent = lpOverlapped->hEvent; hEvent = lpOverlapped->hEvent;
lpOverlapped->Internal = STATUS_PENDING; lpOverlapped->Internal = STATUS_PENDING;
IoStatusBlock = (PIO_STATUS_BLOCK)lpOverlapped; IoStatusBlock = (PIO_STATUS_BLOCK)lpOverlapped;
@ -54,14 +53,13 @@ DeviceIoControl(
IoStatusBlock = &IIosb; IoStatusBlock = &IIosb;
} }
// CHECKPOINT
if(bFsIoControlCode == TRUE) { if(bFsIoControlCode == TRUE) {
errCode = NtFsControlFile(hDevice,hEvent,NULL,NULL,IoStatusBlock,dwIoControlCode,lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize ); errCode = NtFsControlFile(hDevice,hEvent,NULL,NULL,IoStatusBlock,dwIoControlCode,lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize );
} else { } else {
errCode = NtDeviceIoControlFile(hDevice,hEvent,NULL,NULL,IoStatusBlock,dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize); errCode = NtDeviceIoControlFile(hDevice,hEvent,NULL,NULL,IoStatusBlock,dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize);
} }
// CHECKPOINT
if(errCode == STATUS_PENDING ) { if(errCode == STATUS_PENDING ) {
if(NtWaitForSingleObject(hDevice,FALSE,NULL) < 0) { if(NtWaitForSingleObject(hDevice,FALSE,NULL) < 0) {
*lpBytesReturned = IoStatusBlock->Information; *lpBytesReturned = IoStatusBlock->Information;
@ -73,12 +71,10 @@ DeviceIoControl(
SetLastError(RtlNtStatusToDosError(errCode)); SetLastError(RtlNtStatusToDosError(errCode));
return FALSE; return FALSE;
} }
// CHECKPOINT if (lpOverlapped)
if (lpOverlapped)
*lpBytesReturned = lpOverlapped->InternalHigh; *lpBytesReturned = lpOverlapped->InternalHigh;
else else
*lpBytesReturned = IoStatusBlock->Information; *lpBytesReturned = IoStatusBlock->Information;
// CHECKPOINT
return TRUE; return TRUE;
} }

View file

@ -189,8 +189,8 @@ FindNextFileW@8
;GetCommProperties@8 ;GetCommProperties@8
;GetCommState@8 ;GetCommState@8
;GetCommTimeouts@8 ;GetCommTimeouts@8
;GetCommandLineA@0 GetCommandLineA@0
;GetCommandLineW@0 GetCommandLineW@0
;GetCompressedFileSizeA@8 ;GetCompressedFileSizeA@8
;GetCompressedFileSizeW@8 ;GetCompressedFileSizeW@8
;GetComputerNameA@8 ;GetComputerNameA@8
@ -239,8 +239,8 @@ GetCurrentDirectoryW@8
;GetDriveTypeA@4 ;GetDriveTypeA@4
;GetDriveTypeW@4 ;GetDriveTypeW@4
;GetEnvironmentStrings@0 ;GetEnvironmentStrings@0
;GetEnvironmentStringsA@0 GetEnvironmentStringsA@0
;GetEnvironmentStringsW@0 GetEnvironmentStringsW@0
;GetEnvironmentVariableA@12 ;GetEnvironmentVariableA@12
;GetEnvironmentVariableW@12 ;GetEnvironmentVariableW@12
;GetExitCodeProcess@8 ;GetExitCodeProcess@8
@ -291,7 +291,7 @@ GetCurrentDirectoryW@8
;GetPrivateProfileStructW@20 ;GetPrivateProfileStructW@20
;GetProcAddress@8 ;GetProcAddress@8
;GetProcessAffinityMask@12 ;GetProcessAffinityMask@12
;GetProcessHeap@0 GetProcessHeap@0
;GetProcessHeaps@8 ;GetProcessHeaps@8
;GetProcessShutdownParameters@8 ;GetProcessShutdownParameters@8
;GetProcessTimes@20 ;GetProcessTimes@20
@ -341,7 +341,7 @@ GetStdHandle@4
;GetUserDefaultLCID@0 ;GetUserDefaultLCID@0
;GetUserDefaultLangID@0 ;GetUserDefaultLangID@0
;GetVDMCurrentDirectories@8 ;GetVDMCurrentDirectories@8
;GetVersion@0 GetVersion@0
;GetVersionExA@4 ;GetVersionExA@4
;GetVersionExW@4 ;GetVersionExW@4
;GetVolumeInformationA@32 ;GetVolumeInformationA@32
@ -369,21 +369,21 @@ GetStdHandle@4
;GlobalUnfix@4 ;GlobalUnfix@4
;GlobalUnlock@4 ;GlobalUnlock@4
;GlobalWire@4 ;GlobalWire@4
;HeapAlloc@12 HeapAlloc@12
;HeapCompact@8 HeapCompact@8
;HeapCreate@12 HeapCreate@12
;HeapCreateTagsW@16 ;HeapCreateTagsW@16
;HeapDestroy@4 HeapDestroy@4
;HeapExtend@16 ;HeapExtend@16
;HeapFree@12 HeapFree@12
;HeapLock@4 ;HeapLock@4
;HeapQueryTagW@20 ;HeapQueryTagW@20
;HeapReAlloc@16 HeapReAlloc@16
;HeapSize@12 HeapSize@12
;HeapSummary@12 ;HeapSummary@12
;HeapUnlock@4 HeapUnlock@4
;HeapUsage@20 ;HeapUsage@20
;HeapValidate@12 HeapValidate@12
;HeapWalk@8 ;HeapWalk@8
;InitAtomTable@4 ;InitAtomTable@4
;InitializeCriticalSection@4 ;InitializeCriticalSection@4

View file

@ -214,9 +214,11 @@ asmlinkage void exception_handler(unsigned int edi,
__asm__("movl %%cr2,%0\n\t" __asm__("movl %%cr2,%0\n\t"
: "=d" (cr2)); : "=d" (cr2));
DbgPrint("cr2 %x\n",cr2); DbgPrint("cr2 %x\n",cr2);
for(;;);
DbgPrint("Process: %x\n",PsGetCurrentProcess()); DbgPrint("Process: %x\n",PsGetCurrentProcess());
DbgPrint("Thread: %x\n",PsGetCurrentThread()->Cid.UniqueThread); if (PsGetCurrentThread() != NULL)
{
DbgPrint("Thread: %x\n",PsGetCurrentThread()->Cid.UniqueThread);
}
DbgPrint("DS %x ES %x FS %x GS %x\n",ds&0xffff,es&0xffff,fs&0xffff, DbgPrint("DS %x ES %x FS %x GS %x\n",ds&0xffff,es&0xffff,fs&0xffff,
gs&0xfff); gs&0xfff);
DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n",eax,ebx,ecx); DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n",eax,ebx,ecx);

View file

@ -15,6 +15,7 @@
#include <string.h> #include <string.h>
#include <internal/string.h> #include <internal/string.h>
#include <internal/bitops.h> #include <internal/bitops.h>
#include <internal/ex.h>
#include <ddk/ntddk.h> #include <ddk/ntddk.h>
#define NDEBUG #define NDEBUG
@ -57,6 +58,69 @@ static ULONG ProtectToPTE(ULONG flProtect)
(((ULONG)v / (1024 * 1024))&(~0x3))) (((ULONG)v / (1024 * 1024))&(~0x3)))
#define ADDR_TO_PTE(v) (PULONG)(PAGETABLE_MAP + ((ULONG)v / 1024)) #define ADDR_TO_PTE(v) (PULONG)(PAGETABLE_MAP + ((ULONG)v / 1024))
NTSTATUS MmReleaseMmInfo(PEPROCESS Process)
{
ULONG i,j,addr;
DbgPrint("MmReleaseMmInfo(Process %x)\n",Process);
KeAttachProcess(Process);
for (i=0; i<1024; i++)
{
if (ADDR_TO_PDE(i*4*1024*1024) != 0)
{
for (j=0; j<1024; j++)
{
addr = i*4*1024*1024 + j*4*1024;
if (ADDR_TO_PTE(addr) != 0)
{
MmFreePage((PVOID)PAGE_MASK(*ADDR_TO_PTE(addr)), 1);
}
}
}
}
KeDetachProcess();
MmFreePage(Process->Pcb.PageTableDirectory, 1);
Process->Pcb.PageTableDirectory = NULL;
DbgPrint("Finished MmReleaseMmInfo()\n");
return(STATUS_SUCCESS);
}
NTSTATUS MmCopyMmInfo(PEPROCESS Src, PEPROCESS Dest)
{
PULONG PhysPageDirectory;
PULONG PageDirectory;
PULONG CurrentPageDirectory;
PKPROCESS KProcess = &Dest->Pcb;
ULONG i;
DPRINT("MmCopyMmInfo(Src %x, Dest %x)\n", Src, Dest);
PageDirectory = ExAllocatePage();
if (PageDirectory == NULL)
{
return(STATUS_UNSUCCESSFUL);
}
PhysPageDirectory = (PULONG)
GET_LARGE_INTEGER_LOW_PART(MmGetPhysicalAddress(PageDirectory));
KProcess->PageTableDirectory = PhysPageDirectory;
CurrentPageDirectory = (PULONG)PAGEDIRECTORY_MAP;
memset(PageDirectory,0,PAGESIZE);
for (i=768; i<896; i++)
{
PageDirectory[i] = CurrentPageDirectory[i];
}
DPRINT("Addr %x\n",0xf0000000 / (4*1024*1024));
PageDirectory[0xf0000000 / (4*1024*1024)] = (ULONG)PhysPageDirectory | 0x7;
ExUnmapPage(PageDirectory);
DPRINT("Finished MmCopyMmInfo()\n");
return(STATUS_SUCCESS);
}
VOID MmDeletePageTable(PEPROCESS Process, PVOID Address) VOID MmDeletePageTable(PEPROCESS Process, PVOID Address)
{ {
if (Process != NULL && Process != PsGetCurrentProcess()) if (Process != NULL && Process != PsGetCurrentProcess())
@ -87,6 +151,33 @@ ULONG MmGetPageEntryForProcess(PEPROCESS Process, PVOID Address)
return(Entry); return(Entry);
} }
VOID MmDeletePageEntry(PEPROCESS Process, PVOID Address)
{
PULONG page_tlb;
PULONG page_dir;
if (Process != NULL && Process != PsGetCurrentProcess())
{
KeAttachProcess(Process);
}
page_dir = ADDR_TO_PDE(Address);
if ((*page_dir) == 0)
{
if (Process != NULL && Process != PsGetCurrentProcess())
{
KeDetachProcess();
}
return;
}
page_tlb = ADDR_TO_PTE(Address);
*page_tlb = 0;
if (Process != NULL && Process != PsGetCurrentProcess())
{
KeDetachProcess();
}
}
PULONG MmGetPageEntry(PVOID PAddress) PULONG MmGetPageEntry(PVOID PAddress)
/* /*
* FUNCTION: Get a pointer to the page table entry for a virtual address * FUNCTION: Get a pointer to the page table entry for a virtual address
@ -115,6 +206,7 @@ BOOLEAN MmIsPagePresent(PEPROCESS Process, PVOID Address)
return((MmGetPageEntryForProcess(Process, Address)) & PA_PRESENT); return((MmGetPageEntryForProcess(Process, Address)) & PA_PRESENT);
} }
VOID MmSetPage(PEPROCESS Process, VOID MmSetPage(PEPROCESS Process,
PVOID Address, PVOID Address,
ULONG flProtect, ULONG flProtect,

View file

@ -78,6 +78,7 @@ void HalTaskSwitch(PKTHREAD thread)
&(gdt[thread->Context.nr/8].a), &(gdt[thread->Context.nr/8].a),
gdt[thread->Context.nr/8].a, gdt[thread->Context.nr/8].a,
gdt[thread->Context.nr/8].b); gdt[thread->Context.nr/8].b);
DPRINT("thread->Context.cr3 %x\n",thread->Context.cr3);
__asm__("pushfl\n\t" __asm__("pushfl\n\t"
"cli\n\t" "cli\n\t"
"ljmp %0\n\t" "ljmp %0\n\t"

View file

@ -14,6 +14,7 @@
#include <internal/ke.h> #include <internal/ke.h>
#include <internal/mm.h> #include <internal/mm.h>
//#define NDEBUG
#include <internal/debug.h> #include <internal/debug.h>
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
@ -40,6 +41,7 @@ VOID KeAttachProcess(PEPROCESS Process)
CurrentThread->ThreadsProcess = Process; CurrentThread->ThreadsProcess = Process;
PageDir = (ULONG)CurrentThread->ThreadsProcess->Pcb.PageTableDirectory; PageDir = (ULONG)CurrentThread->ThreadsProcess->Pcb.PageTableDirectory;
CurrentThread->Tcb.Context.cr3 = PageDir; CurrentThread->Tcb.Context.cr3 = PageDir;
DPRINT("Switching process context to %x\n",PageDir)
__asm__("movl %0,%%cr3\n\t" __asm__("movl %0,%%cr3\n\t"
: /* no inputs */ : /* no inputs */
: "r" (PageDir)); : "r" (PageDir));

View file

@ -86,22 +86,43 @@ unsigned int EiUsedNonPagedPool = 0;
/* FUNCTIONS ***************************************************************/ /* FUNCTIONS ***************************************************************/
VOID ExUnmapPage(PVOID Addr)
{
KIRQL oldIrql;
ULONG i = ((ULONG)Addr - kernel_pool_base) / PAGESIZE;
DbgPrint("ExUnmapPage(Addr %x)\n",Addr);
DbgPrint("i %x\n",i);
KeAcquireSpinLock(&AllocMapLock, &oldIrql);
MmSetPage(NULL, (PVOID)Addr, 0, 0);
clear_bit(i%32, &alloc_map[i/32]);
KeReleaseSpinLock(&AllocMapLock, oldIrql);
}
PVOID ExAllocatePage(VOID) PVOID ExAllocatePage(VOID)
{ {
KIRQL oldlvl; KIRQL oldlvl;
ULONG addr; ULONG addr;
ULONG i; ULONG i;
ULONG PhysPage;
PhysPage = (ULONG)MmAllocPage();
DbgPrint("Allocated page %x\n",PhysPage);
if (PhysPage == 0)
{
return(NULL);
}
KeAcquireSpinLock(&AllocMapLock, &oldlvl); KeAcquireSpinLock(&AllocMapLock, &oldlvl);
for (i=1; i<ALLOC_MAP_SIZE;i++) for (i=1; i<ALLOC_MAP_SIZE;i++)
{ {
if (!test_bit(i%32,&alloc_map[i/32])) if (!test_bit(i%32,&alloc_map[i/32]))
{ {
DbgPrint("i %x\n",i);
set_bit(i%32,&alloc_map[i/32]);
addr = kernel_pool_base + (i*PAGESIZE); addr = kernel_pool_base + (i*PAGESIZE);
MmSetPage(NULL, MmSetPage(NULL, (PVOID)addr, PAGE_READWRITE, PhysPage);
(PVOID)addr,
PAGE_READWRITE,
(ULONG)MmAllocPage());
KeReleaseSpinLock(&AllocMapLock, oldlvl); KeReleaseSpinLock(&AllocMapLock, oldlvl);
return((PVOID)addr); return((PVOID)addr);
} }

View file

@ -345,6 +345,7 @@ NTSTATUS STDCALL ZwMapViewOfSection(HANDLE SectionHandle,
if (!NT_SUCCESS(Status)) if (!NT_SUCCESS(Status))
{ {
DPRINT("ZwMapViewOfSection() = %x\n",Status); DPRINT("ZwMapViewOfSection() = %x\n",Status);
ObDereferenceObject(Process);
return(Status); return(Status);
} }
Result->Data.SectionData.Section = Section; Result->Data.SectionData.Section = Section;
@ -362,7 +363,7 @@ NTSTATUS STDCALL ZwMapViewOfSection(HANDLE SectionHandle,
} }
DPRINT("*BaseAddress %x\n",*BaseAddress); DPRINT("*BaseAddress %x\n",*BaseAddress);
ObDereferenceObject(Process);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }

View file

@ -218,6 +218,7 @@ asmlinkage int page_fault_handler(unsigned int cs,
stat = 0; stat = 0;
break; break;
} }
DPRINT("Completed page fault handling\n");
if (stat) if (stat)
{ {
KeLowerIrql(oldlvl); KeLowerIrql(oldlvl);
@ -355,6 +356,7 @@ ZwAllocateVirtualMemory(
MemoryArea->Type = Type; MemoryArea->Type = Type;
MemoryArea->Attributes =Protect; MemoryArea->Attributes =Protect;
DPRINT("*BaseAddress %x\n",*BaseAddress); DPRINT("*BaseAddress %x\n",*BaseAddress);
ObDereferenceObject(Process);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }
@ -365,6 +367,7 @@ ZwAllocateVirtualMemory(
Type, Type,
Protect); Protect);
DPRINT("*BaseAddress %x\n",*BaseAddress); DPRINT("*BaseAddress %x\n",*BaseAddress);
ObDereferenceObject(Process);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }
} }
@ -383,11 +386,12 @@ ZwAllocateVirtualMemory(
if (Status != STATUS_SUCCESS) if (Status != STATUS_SUCCESS)
{ {
DPRINT("ZwAllocateVirtualMemory() = %x\n",Status); DPRINT("ZwAllocateVirtualMemory() = %x\n",Status);
ObDereferenceObject(Process);
return(Status); return(Status);
} }
DPRINT("*BaseAddress %x\n",*BaseAddress); DPRINT("*BaseAddress %x\n",*BaseAddress);
ObDereferenceObject(Process);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }
@ -476,12 +480,14 @@ NTSTATUS STDCALL ZwFreeVirtualMemory(IN HANDLE ProcessHandle,
case MEM_RELEASE: case MEM_RELEASE:
if (MemoryArea->BaseAddress != (*BaseAddress)) if (MemoryArea->BaseAddress != (*BaseAddress))
{ {
ObDereferenceObject(Process);
return(STATUS_UNSUCCESSFUL); return(STATUS_UNSUCCESSFUL);
} }
MmFreeMemoryArea(PsGetCurrentProcess(), MmFreeMemoryArea(PsGetCurrentProcess(),
BaseAddress, BaseAddress,
0, 0,
TRUE); TRUE);
ObDereferenceObject(Process);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
case MEM_DECOMMIT: case MEM_DECOMMIT:
@ -491,9 +497,10 @@ NTSTATUS STDCALL ZwFreeVirtualMemory(IN HANDLE ProcessHandle,
*RegionSize, *RegionSize,
MEMORY_AREA_RESERVE, MEMORY_AREA_RESERVE,
MemoryArea->Attributes); MemoryArea->Attributes);
ObDereferenceObject(Process);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }
ObDereferenceObject(Process);
return(STATUS_NOT_IMPLEMENTED); return(STATUS_NOT_IMPLEMENTED);
} }
@ -592,6 +599,7 @@ NTSTATUS STDCALL ZwProtectVirtualMemory(IN HANDLE ProcessHandle,
} }
MmChangeAreaProtection(Process,BaseAddress,NumberOfBytesToProtect, MmChangeAreaProtection(Process,BaseAddress,NumberOfBytesToProtect,
NewAccessProtection); NewAccessProtection);
ObDereferenceObject(Process);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }
@ -765,6 +773,8 @@ NTSTATUS STDCALL ZwWriteVirtualMemory(IN HANDLE ProcessHandle,
KeDetachProcess(); KeDetachProcess();
ObDereferenceObject(Process);
*NumberOfBytesWritten = NumberOfBytesToWrite; *NumberOfBytesWritten = NumberOfBytesToWrite;
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }

View file

@ -328,11 +328,13 @@ NTSTATUS ObReferenceObjectByHandle(HANDLE Handle,
if (Handle == NtCurrentProcess()) if (Handle == NtCurrentProcess())
{ {
BODY_TO_HEADER(PsGetCurrentProcess())->RefCount++;
*Object = PsGetCurrentProcess(); *Object = PsGetCurrentProcess();
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }
if (Handle == NtCurrentThread()) if (Handle == NtCurrentThread())
{ {
BODY_TO_HEADER(PsGetCurrentThread())->RefCount++;
*Object = PsGetCurrentThread(); *Object = PsGetCurrentThread();
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }

View file

@ -205,6 +205,8 @@ NTSTATUS ObPerformRetentionChecks(POBJECT_HEADER Header)
if (Header->RefCount < 0 || Header->HandleCount < 0) if (Header->RefCount < 0 || Header->HandleCount < 0)
{ {
DbgPrint("Object %x/%x has invalid reference or handle count\n",
Header,HEADER_TO_BODY(Header));
KeBugCheck(0); KeBugCheck(0);
} }
@ -226,6 +228,13 @@ NTSTATUS ObPerformRetentionChecks(POBJECT_HEADER Header)
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }
ULONG ObGetReferenceCount(PVOID ObjectBody)
{
POBJECT_HEADER Header = BODY_TO_HEADER(ObjectBody);
return(Header->RefCount);
}
VOID ObDereferenceObject(PVOID ObjectBody) VOID ObDereferenceObject(PVOID ObjectBody)
/* /*
* FUNCTION: Decrements a given object's reference count and performs * FUNCTION: Decrements a given object's reference count and performs

View file

@ -13,8 +13,10 @@
#include <ddk/ntddk.h> #include <ddk/ntddk.h>
#include <internal/ps.h> #include <internal/ps.h>
#include <internal/ke.h> #include <internal/ke.h>
#include <internal/mm.h>
#include <internal/ob.h>
#define NDEBUG //#define NDEBUG
#include <internal/debug.h> #include <internal/debug.h>
/* GLBOALS *******************************************************************/ /* GLBOALS *******************************************************************/
@ -23,6 +25,12 @@ extern ULONG PiNrThreads;
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
VOID PiDeleteProcess(PVOID ObjectBody)
{
DPRINT("PiDeleteProcess(ObjectBody %x)\n",ObjectBody);
(VOID)MmReleaseMmInfo((PEPROCESS)ObjectBody);
}
VOID PsTerminateCurrentThread(NTSTATUS ExitStatus) VOID PsTerminateCurrentThread(NTSTATUS ExitStatus)
/* /*
* FUNCTION: Terminates the current thread * FUNCTION: Terminates the current thread
@ -39,6 +47,7 @@ VOID PsTerminateCurrentThread(NTSTATUS ExitStatus)
DPRINT("terminating %x\n",CurrentThread); DPRINT("terminating %x\n",CurrentThread);
ObDereferenceObject(CurrentThread->ThreadsProcess); ObDereferenceObject(CurrentThread->ThreadsProcess);
CurrentThread->ThreadsProcess = NULL;
KeRaiseIrql(DISPATCH_LEVEL,&oldlvl); KeRaiseIrql(DISPATCH_LEVEL,&oldlvl);
CurrentThread->Tcb.ThreadState = THREAD_STATE_TERMINATED; CurrentThread->Tcb.ThreadState = THREAD_STATE_TERMINATED;
ZwYieldExecution(); ZwYieldExecution();
@ -67,6 +76,9 @@ NTSTATUS STDCALL ZwTerminateProcess(IN HANDLE ProcessHandle,
PEPROCESS Process; PEPROCESS Process;
KIRQL oldlvl; KIRQL oldlvl;
DPRINT("ZwTerminateProcess(ProcessHandle %x, ExitStatus %x)\n",
ProcessHandle, ExitStatus);
Status = ObReferenceObjectByHandle(ProcessHandle, Status = ObReferenceObjectByHandle(ProcessHandle,
PROCESS_TERMINATE, PROCESS_TERMINATE,
PsProcessType, PsProcessType,
@ -78,16 +90,21 @@ NTSTATUS STDCALL ZwTerminateProcess(IN HANDLE ProcessHandle,
return(Status); return(Status);
} }
DPRINT("Process %x ReferenceCount %d\n",Process,
ObGetReferenceCount(Process));
PiTerminateProcessThreads(Process, ExitStatus); PiTerminateProcessThreads(Process, ExitStatus);
KeRaiseIrql(DISPATCH_LEVEL, &oldlvl); KeRaiseIrql(DISPATCH_LEVEL, &oldlvl);
KeDispatcherObjectWake(&Process->Pcb.DispatcherHeader);
Process->Pcb.ProcessState = PROCESS_STATE_TERMINATED; Process->Pcb.ProcessState = PROCESS_STATE_TERMINATED;
KeDispatcherObjectWake(&Process->Pcb.DispatcherHeader);
if (PsGetCurrentThread()->ThreadsProcess == Process) if (PsGetCurrentThread()->ThreadsProcess == Process)
{ {
KeLowerIrql(oldlvl); KeLowerIrql(oldlvl);
ObDereferenceObject(Process);
PsTerminateCurrentThread(ExitStatus); PsTerminateCurrentThread(ExitStatus);
} }
KeLowerIrql(oldlvl); KeLowerIrql(oldlvl);
ObDereferenceObject(Process);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }

View file

@ -14,6 +14,7 @@
#include <internal/ob.h> #include <internal/ob.h>
#include <internal/mm.h> #include <internal/mm.h>
#include <internal/ke.h> #include <internal/ke.h>
#include <internal/ps.h>
#include <string.h> #include <string.h>
#include <internal/string.h> #include <internal/string.h>
@ -29,27 +30,6 @@ POBJECT_TYPE PsProcessType = NULL;
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
#define IDMAP_BASE (0xd0000000)
/*
* Return a linear address which can be used to access the physical memory
* starting at x
*/
extern inline unsigned int physical_to_linear(unsigned int x)
{
return(x+IDMAP_BASE);
}
extern inline unsigned int linear_to_physical(unsigned int x)
{
return(x-IDMAP_BASE);
}
PEPROCESS PsGetSystemProcess(VOID)
{
return(SystemProcess);
}
VOID PsInitProcessManagment(VOID) VOID PsInitProcessManagment(VOID)
{ {
ANSI_STRING AnsiString; ANSI_STRING AnsiString;
@ -70,7 +50,7 @@ VOID PsInitProcessManagment(VOID)
PsProcessType->Dump = NULL; PsProcessType->Dump = NULL;
PsProcessType->Open = NULL; PsProcessType->Open = NULL;
PsProcessType->Close = NULL; PsProcessType->Close = NULL;
PsProcessType->Delete = NULL; PsProcessType->Delete = PiDeleteProcess;
PsProcessType->Parse = NULL; PsProcessType->Parse = NULL;
PsProcessType->Security = NULL; PsProcessType->Security = NULL;
PsProcessType->QueryName = NULL; PsProcessType->QueryName = NULL;
@ -82,8 +62,10 @@ VOID PsInitProcessManagment(VOID)
/* /*
* Initialize the system process * Initialize the system process
*/ */
SystemProcess = ObCreateObject(NULL,PROCESS_ALL_ACCESS,NULL, SystemProcess = ObCreateObject(NULL,
PsProcessType); PROCESS_ALL_ACCESS,
NULL,
PsProcessType);
KProcess = &SystemProcess->Pcb; KProcess = &SystemProcess->Pcb;
InitializeListHead(&(KProcess->MemoryAreaList)); InitializeListHead(&(KProcess->MemoryAreaList));
@ -174,12 +156,8 @@ NTSTATUS STDCALL ZwCreateProcess(
{ {
PEPROCESS Process; PEPROCESS Process;
PEPROCESS ParentProcess; PEPROCESS ParentProcess;
PULONG PageDirectory;
PULONG CurrentPageDirectory;
ULONG i;
PKPROCESS KProcess; PKPROCESS KProcess;
NTSTATUS Status; NTSTATUS Status;
PULONG PhysicalPageDirectory;
DPRINT("ZwCreateProcess(ObjectAttributes %x)\n",ObjectAttributes); DPRINT("ZwCreateProcess(ObjectAttributes %x)\n",ObjectAttributes);
@ -210,21 +188,7 @@ NTSTATUS STDCALL ZwCreateProcess(
ObCreateHandleTable(ParentProcess, ObCreateHandleTable(ParentProcess,
InheritObjectTable, InheritObjectTable,
Process); Process);
MmCopyMmInfo(ParentProcess, Process);
PhysicalPageDirectory = (PULONG)MmAllocPage();
PageDirectory = (PULONG)physical_to_linear((ULONG)PhysicalPageDirectory);
KProcess->PageTableDirectory = PhysicalPageDirectory;
CurrentPageDirectory = (PULONG)physical_to_linear(
(ULONG)get_page_directory());
memset(PageDirectory,0,PAGESIZE);
for (i=768; i<896; i++)
{
PageDirectory[i] = CurrentPageDirectory[i];
}
PageDirectory[0xf0000000 / (4*1024*1024)]
= (ULONG)PhysicalPageDirectory | 0x7;
/* /*
* FIXME: I don't what I'm supposed to know with a section handle * FIXME: I don't what I'm supposed to know with a section handle
@ -236,7 +200,8 @@ NTSTATUS STDCALL ZwCreateProcess(
} }
Process->Pcb.ProcessState = PROCESS_STATE_ACTIVE; Process->Pcb.ProcessState = PROCESS_STATE_ACTIVE;
ObDereferenceObject(Process);
ObDereferenceObject(ParentProcess);
return(STATUS_SUCCESS); return(STATUS_SUCCESS);
} }

View file

@ -243,7 +243,7 @@ NTSTATUS PsInitializeThread(HANDLE ProcessHandle,
} }
else else
{ {
Thread->ThreadsProcess=SystemProcess; Thread->ThreadsProcess = SystemProcess;
ObReferenceObjectByPointer(Thread->ThreadsProcess, ObReferenceObjectByPointer(Thread->ThreadsProcess,
PROCESS_CREATE_THREAD, PROCESS_CREATE_THREAD,
PsProcessType, PsProcessType,
@ -297,7 +297,12 @@ VOID PsSuspendThread(PETHREAD Thread)
} }
} }
void PsInitThreadManagment(void) VOID PiDeleteThread(PVOID ObjectBody)
{
DbgPrint("PiDeleteThread(ObjectBody %x)\n",ObjectBody);
}
VOID PsInitThreadManagment(VOID)
/* /*
* FUNCTION: Initialize thread managment * FUNCTION: Initialize thread managment
*/ */
@ -327,7 +332,7 @@ void PsInitThreadManagment(void)
PsThreadType->Dump = NULL; PsThreadType->Dump = NULL;
PsThreadType->Open = NULL; PsThreadType->Open = NULL;
PsThreadType->Close = NULL; PsThreadType->Close = NULL;
PsThreadType->Delete = NULL; PsThreadType->Delete = PiDeleteThread;
PsThreadType->Parse = NULL; PsThreadType->Parse = NULL;
PsThreadType->Security = NULL; PsThreadType->Security = NULL;
PsThreadType->QueryName = NULL; PsThreadType->QueryName = NULL;

View file

@ -93,6 +93,7 @@ NTSTATUS ZwSetInformationThread(HANDLE ThreadHandle,
default: default:
Status = STATUS_UNSUCCESSFUL; Status = STATUS_UNSUCCESSFUL;
} }
ObDereferenceObject(Thread);
return(Status); return(Status);
} }