+#include <ntos.h>
#include <stdio.h>
int main(int argc, char* argv[])
-{
+{
int x;
+ PTEB Teb;
printf("TEB dumpper\n");
__asm__("movl %%fs:0x18, %0\n\t"
: "=a" (x)
: /* no inputs */);
printf("fs[0x18] %x\n", x);
+
+ Teb = (PTEB)x;
+
+ printf("StackBase: 0x%08lX\n", Teb->Tib.StackBase);
+ printf("StackLimit: 0x%08lX\n", Teb->Tib.StackLimit);
+ printf("DeallocationStack: 0x%08lX\n", Teb->DeallocationStack);
+
return(0);
}
-/* $Id: rtl.h,v 1.55 2001/07/04 20:40:18 chorns Exp $
+/* $Id: rtl.h,v 1.56 2001/08/03 17:07:56 ekohl Exp $
*
*/
typedef struct _INITIAL_TEB
{
- PVOID StackBase;
- PVOID StackLimit;
- PVOID StackCommit;
- PVOID StackCommitMax;
- PVOID StackReserve;
+ ULONG StackCommit;
+ ULONG StackReserve;
+ PVOID StackBase;
+ PVOID StackLimit;
+ PVOID StackAllocate;
} INITIAL_TEB, *PINITIAL_TEB;
typedef struct _CONTROLLER_OBJECT
#define RTL_QUERY_REGISTRY_DELETE (0x00000040)
-typedef NTSTATUS (*PRTL_QUERY_REGISTRY_ROUTINE)(PWSTR ValueName,
- ULONG ValueType,
- PVOID ValueData,
- ULONG ValueLength,
- PVOID Context,
- PVOID EntryContext);
+typedef NTSTATUS STDCALL
+(*PRTL_QUERY_REGISTRY_ROUTINE)(PWSTR ValueName,
+ ULONG ValueType,
+ PVOID ValueData,
+ ULONG ValueLength,
+ PVOID Context,
+ PVOID EntryContext);
typedef struct _RTL_QUERY_REGISTRY_TABLE
{
*/
#include <windows.h>
#include <ddk/ntddk.h>
-NT_TEB *GetTeb(VOID);
+PTEB GetTeb(VOID);
typedef struct _CLIENT_ID
{
- HANDLE UniqueProcess;
- HANDLE UniqueThread;
+ HANDLE UniqueProcess;
+ HANDLE UniqueThread;
} CLIENT_ID, *PCLIENT_ID;
typedef struct _CURDIR
ULONG Buffer[0x136];
} GDI_TEB_BATCH, *PGDI_TEB_BATCH;
-typedef struct _NT_TEB
+typedef struct _TEB
{
NT_TIB Tib; // 00h
PVOID EnvironmentPointer; // 1Ch
ULONG Spare4; // F7Ch
PVOID ReservedForOle; // F80h
ULONG WaitingOnLoaderLock; // F84h
+} TEB, *PTEB;
- PVOID StackCommit; // F88h
- PVOID StackCommitMax; // F8Ch
- PVOID StackReserve; // F90h
-} NT_TEB, *PNT_TEB;
-
-#define PEB_STARTUPINFO (0xb0003000)
#define NtCurrentPeb() (NtCurrentTeb()->Peb)
-static inline PNT_TEB NtCurrentTeb(VOID)
+static inline PTEB NtCurrentTeb(VOID)
{
int x;
: /* no inputs */
);
- return((PNT_TEB)x);
+ return((PTEB)x);
}
-/* $Id: create.c,v 1.39 2001/06/18 03:02:43 phreak Exp $
+/* $Id: create.c,v 1.40 2001/08/03 17:20:06 ekohl Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
- * FILE: lib/kernel32/proc/proc.c
+ * FILE: lib/kernel32/process/create.c
* PURPOSE: Process functions
* PROGRAMMER: Ariadne ( ariadne@xs4all.nl)
* UPDATE HISTORY:
HANDLE STDCALL
KlCreateFirstThread(HANDLE ProcessHandle,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
- DWORD dwStackSize,
+ ULONG StackReserve,
+ ULONG StackCommit,
LPTHREAD_START_ROUTINE lpStartAddress,
DWORD dwCreationFlags,
LPDWORD lpThreadId)
{
- NTSTATUS Status;
- HANDLE ThreadHandle;
- OBJECT_ATTRIBUTES ObjectAttributes;
- CLIENT_ID ClientId;
- CONTEXT ThreadContext;
- INITIAL_TEB InitialTeb;
- BOOLEAN CreateSuspended = FALSE;
- PVOID BaseAddress;
-
- ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
- ObjectAttributes.RootDirectory = NULL;
- ObjectAttributes.ObjectName = NULL;
- ObjectAttributes.Attributes = 0;
- if (lpThreadAttributes != NULL)
- {
- if (lpThreadAttributes->bInheritHandle)
- ObjectAttributes.Attributes = OBJ_INHERIT;
- ObjectAttributes.SecurityDescriptor =
- lpThreadAttributes->lpSecurityDescriptor;
- }
- ObjectAttributes.SecurityQualityOfService = NULL;
-
- if ((dwCreationFlags & CREATE_SUSPENDED) == CREATE_SUSPENDED)
- CreateSuspended = TRUE;
- else
- CreateSuspended = FALSE;
-
- /* Allocate thread stack */
- BaseAddress = NULL;
- Status = NtAllocateVirtualMemory(ProcessHandle,
- &BaseAddress,
- 0,
- (PULONG)&dwStackSize,
- MEM_COMMIT,
- PAGE_READWRITE);
- if (!NT_SUCCESS(Status))
- {
- return(NULL);
- }
-
- memset(&ThreadContext,0,sizeof(CONTEXT));
- ThreadContext.Eip = (ULONG)lpStartAddress;
- ThreadContext.SegGs = USER_DS;
- ThreadContext.SegFs = USER_DS;
- ThreadContext.SegEs = USER_DS;
- ThreadContext.SegDs = USER_DS;
- ThreadContext.SegCs = USER_CS;
- ThreadContext.SegSs = USER_DS;
- ThreadContext.Esp = (ULONG)(BaseAddress + dwStackSize - 20);
- ThreadContext.EFlags = (1<<1) + (1<<9);
-
- DPRINT("ThreadContext.Eip %x\n",ThreadContext.Eip);
-
- Status = NtCreateThread(&ThreadHandle,
- THREAD_ALL_ACCESS,
- &ObjectAttributes,
- ProcessHandle,
- &ClientId,
- &ThreadContext,
- &InitialTeb,
- CreateSuspended);
- if (lpThreadId != NULL)
- {
- memcpy(lpThreadId, &ClientId.UniqueThread,sizeof(ULONG));
- }
-
- return(ThreadHandle);
+ NTSTATUS Status;
+ HANDLE ThreadHandle;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ CLIENT_ID ClientId;
+ CONTEXT ThreadContext;
+ INITIAL_TEB InitialTeb;
+ BOOLEAN CreateSuspended = FALSE;
+ ULONG OldPageProtection;
+
+ ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
+ ObjectAttributes.RootDirectory = NULL;
+ ObjectAttributes.ObjectName = NULL;
+ ObjectAttributes.Attributes = 0;
+ if (lpThreadAttributes != NULL)
+ {
+ if (lpThreadAttributes->bInheritHandle)
+ ObjectAttributes.Attributes = OBJ_INHERIT;
+ ObjectAttributes.SecurityDescriptor =
+ lpThreadAttributes->lpSecurityDescriptor;
+ }
+ ObjectAttributes.SecurityQualityOfService = NULL;
+
+ if ((dwCreationFlags & CREATE_SUSPENDED) == CREATE_SUSPENDED)
+ CreateSuspended = TRUE;
+ else
+ CreateSuspended = FALSE;
+
+ InitialTeb.StackCommit = (StackCommit < PAGESIZE) ? PAGESIZE : StackCommit;
+ InitialTeb.StackReserve = (StackReserve < 0x100000) ? 0x100000 : StackReserve;
+
+ /* size of guard page */
+ InitialTeb.StackCommit += PAGESIZE;
+
+ /* Reserve stack */
+ InitialTeb.StackAllocate = NULL;
+ Status = NtAllocateVirtualMemory(ProcessHandle,
+ &InitialTeb.StackAllocate,
+ 0,
+ &InitialTeb.StackReserve,
+ MEM_COMMIT, // MEM_RESERVE,
+ PAGE_READWRITE);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT("Error reserving stack space!\n");
+ SetLastErrorByStatus(Status);
+ return(NULL);
+ }
+
+ DPRINT("StackDeallocation: %p ReserveSize: 0x%lX\n",
+ InitialTeb.StackDeallocation, InitialTeb.StackReserve);
+
+ InitialTeb.StackBase = (PVOID)((ULONG)InitialTeb.StackAllocate + InitialTeb.StackReserve);
+ InitialTeb.StackLimit = (PVOID)((ULONG)InitialTeb.StackBase - InitialTeb.StackCommit);
+
+ DPRINT("StackBase: %p\nStackCommit: %p\n",
+ InitialTeb.StackBase, InitialTeb.StackCommit);
+#if 0
+ /* Commit stack page(s) */
+ Status = NtAllocateVirtualMemory(ProcessHandle,
+ &InitialTeb.StackLimit,
+ 0,
+ &InitialTeb.StackCommit,
+ MEM_COMMIT,
+ PAGE_READWRITE);
+ if (!NT_SUCCESS(Status))
+ {
+ /* release the stack space */
+ NtFreeVirtualMemory(ProcessHandle,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+
+ DPRINT("Error comitting stack page(s)!\n");
+ SetLastErrorByStatus(Status);
+ return(NULL);
+ }
+
+ DPRINT("StackLimit: %p\n",
+ InitialTeb.StackLimit);
+
+ /* Protect guard page */
+ Status = NtProtectVirtualMemory(ProcessHandle,
+ InitialTeb.StackLimit,
+ PAGESIZE,
+ PAGE_GUARD | PAGE_READWRITE,
+ &OldPageProtection);
+ if (!NT_SUCCESS(Status))
+ {
+ /* release the stack space */
+ NtFreeVirtualMemory(ProcessHandle,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+
+ DPRINT("Error comitting guard page!\n");
+ SetLastErrorByStatus(Status);
+ return(NULL);
+ }
+#endif
+
+ memset(&ThreadContext,0,sizeof(CONTEXT));
+ ThreadContext.Eip = (ULONG)lpStartAddress;
+ ThreadContext.SegGs = USER_DS;
+ ThreadContext.SegFs = USER_DS;
+ ThreadContext.SegEs = USER_DS;
+ ThreadContext.SegDs = USER_DS;
+ ThreadContext.SegCs = USER_CS;
+ ThreadContext.SegSs = USER_DS;
+ ThreadContext.Esp = (ULONG)InitialTeb.StackBase - 20;
+ ThreadContext.EFlags = (1<<1) + (1<<9);
+
+ DPRINT("ThreadContext.Eip %x\n",ThreadContext.Eip);
+
+ Status = NtCreateThread(&ThreadHandle,
+ THREAD_ALL_ACCESS,
+ &ObjectAttributes,
+ ProcessHandle,
+ &ClientId,
+ &ThreadContext,
+ &InitialTeb,
+ CreateSuspended);
+ if (!NT_SUCCESS(Status))
+ {
+ NtFreeVirtualMemory(ProcessHandle,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+ SetLastErrorByStatus(Status);
+ return(NULL);
+ }
+
+ if (lpThreadId != NULL)
+ {
+ memcpy(lpThreadId, &ClientId.UniqueThread,sizeof(ULONG));
+ }
+
+ return(ThreadHandle);
}
HANDLE
}
/* create the PPB */
- PpbBase = (PVOID)PEB_STARTUPINFO;
+ PpbBase = NULL;
PpbSize = Ppb->MaximumLength;
Status = NtAllocateVirtualMemory(ProcessHandle,
&PpbBase,
lpThreadAttributes,
//Sii.StackReserve,
0x200000,
+ //Sii.StackCommit,
+ 0x1000,
lpStartAddress,
dwCreationFlags,
&lpProcessInformation->dwThreadId);
-
if (hThread == NULL)
{
return FALSE;
-/* $Id: thread.c,v 1.23 2001/05/07 22:03:26 chorns Exp $
+/* $Id: thread.c,v 1.24 2001/08/03 17:20:46 ekohl Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId)
-{
+{
HANDLE ThreadHandle;
OBJECT_ATTRIBUTES ObjectAttributes;
CLIENT_ID ClientId;
INITIAL_TEB InitialTeb;
BOOLEAN CreateSuspended = FALSE;
PVOID BaseAddress;
- DWORD StackSize;
+ ULONG OldPageProtection;
NTSTATUS Status;
ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
else
CreateSuspended = FALSE;
- StackSize = (dwStackSize == 0) ? 4096 : dwStackSize;
-
- BaseAddress = 0;
-
- Status = NtAllocateVirtualMemory(hProcess,
- &BaseAddress,
- 0,
- (PULONG)&StackSize,
- MEM_COMMIT,
- PAGE_READWRITE);
- if (!NT_SUCCESS(Status))
- {
- DPRINT("Could not allocate stack space!\n");
- return NULL;
- }
-
-
- DPRINT("Stack base address: %p\n", BaseAddress);
-
- memset(&ThreadContext,0,sizeof(CONTEXT));
- ThreadContext.Eip = (LONG)ThreadStartup;
- ThreadContext.SegGs = USER_DS;
- ThreadContext.SegFs = TEB_SELECTOR;
- ThreadContext.SegEs = USER_DS;
- ThreadContext.SegDs = USER_DS;
- ThreadContext.SegCs = USER_CS;
- ThreadContext.SegSs = USER_DS;
- ThreadContext.Esp = (ULONG)(BaseAddress + StackSize - 12);
- ThreadContext.EFlags = (1<<1) + (1<<9);
-
- /* initialize call stack */
- *((PULONG)(BaseAddress + StackSize - 4)) = (ULONG)lpParameter;
- *((PULONG)(BaseAddress + StackSize - 8)) = (ULONG)lpStartAddress;
- *((PULONG)(BaseAddress + StackSize - 12)) = 0xdeadbeef;
-
- DPRINT("Esp: %p\n", ThreadContext.Esp);
- DPRINT("Eip: %p\n", ThreadContext.Eip);
-
- Status = NtCreateThread(&ThreadHandle,
- THREAD_ALL_ACCESS,
- &ObjectAttributes,
- hProcess,
- &ClientId,
- &ThreadContext,
- &InitialTeb,
- CreateSuspended);
-
- if (!NT_SUCCESS(Status))
- {
- DPRINT("NtCreateThread() failed!\n");
- return NULL;
- }
-
- if ( lpThreadId != NULL )
- memcpy(lpThreadId, &ClientId.UniqueThread,sizeof(ULONG));
-
- return ThreadHandle;
+ InitialTeb.StackCommit = (dwStackSize == 0) ? PAGESIZE : dwStackSize;
+ InitialTeb.StackReserve = 0x100000; /* 1MByte */
+
+ /* size of guard page */
+ InitialTeb.StackCommit += PAGESIZE;
+
+ /* Reserve stack */
+ InitialTeb.StackAllocate = NULL;
+ Status = NtAllocateVirtualMemory(hProcess,
+ &InitialTeb.StackAllocate,
+ 0,
+ &InitialTeb.StackReserve,
+ MEM_COMMIT, //MEM_RESERVE,
+ PAGE_READWRITE);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT("Error reserving stack space!\n");
+ SetLastErrorByStatus(Status);
+ return(NULL);
+ }
+
+ DPRINT("StackDeallocation: %p ReserveSize: 0x%lX\n",
+ InitialTeb.StackDeallocation, InitialTeb.StackReserve);
+
+ InitialTeb.StackBase = (PVOID)((ULONG)InitialTeb.StackAllocate + InitialTeb.StackReserve);
+ InitialTeb.StackLimit = (PVOID)((ULONG)InitialTeb.StackBase - InitialTeb.StackCommit);
+
+ DPRINT("StackBase: %p\nStackCommit: 0x%lX\n",
+ InitialTeb.StackBase,
+ InitialTeb.StackCommit);
+#if 0
+ /* Commit stack pages */
+ Status = NtAllocateVirtualMemory(hProcess,
+ &InitialTeb.StackLimit,
+ 0,
+ &InitialTeb.StackCommit,
+ MEM_COMMIT,
+ PAGE_READWRITE);
+ if (!NT_SUCCESS(Status))
+ {
+ /* release the stack space */
+ NtFreeVirtualMemory(hProcess,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+
+ DPRINT("Error comitting stack page(s)!\n");
+ SetLastErrorByStatus(Status);
+ return(NULL);
+ }
+
+ DPRINT("StackLimit: %p\n",
+ InitialTeb.StackLimit);
+
+ /* Protect guard page */
+ Status = NtProtectVirtualMemory(hProcess,
+ InitialTeb.StackLimit,
+ PAGESIZE,
+ PAGE_GUARD | PAGE_READWRITE,
+ &OldPageProtection);
+ if (!NT_SUCCESS(Status))
+ {
+ /* release the stack space */
+ NtFreeVirtualMemory(hProcess,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+
+ DPRINT("Error comitting guard page!\n");
+ SetLastErrorByStatus(Status);
+ return(NULL);
+ }
+#endif
+
+ memset(&ThreadContext,0,sizeof(CONTEXT));
+ ThreadContext.Eip = (LONG)ThreadStartup;
+ ThreadContext.SegGs = USER_DS;
+ ThreadContext.SegFs = TEB_SELECTOR;
+ ThreadContext.SegEs = USER_DS;
+ ThreadContext.SegDs = USER_DS;
+ ThreadContext.SegCs = USER_CS;
+ ThreadContext.SegSs = USER_DS;
+ ThreadContext.Esp = (ULONG)InitialTeb.StackBase - 12;
+ ThreadContext.EFlags = (1<<1) + (1<<9);
+
+ /* initialize call stack */
+ *((PULONG)((ULONG)InitialTeb.StackBase - 4)) = (ULONG)lpParameter;
+ *((PULONG)((ULONG)InitialTeb.StackBase - 8)) = (ULONG)lpStartAddress;
+ *((PULONG)((ULONG)InitialTeb.StackBase - 12)) = 0xdeadbeef;
+
+ DPRINT("Esp: %p\n", ThreadContext.Esp);
+ DPRINT("Eip: %p\n", ThreadContext.Eip);
+
+ Status = NtCreateThread(&ThreadHandle,
+ THREAD_ALL_ACCESS,
+ &ObjectAttributes,
+ hProcess,
+ &ClientId,
+ &ThreadContext,
+ &InitialTeb,
+ CreateSuspended);
+ if (!NT_SUCCESS(Status))
+ {
+ NtFreeVirtualMemory(hProcess,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+
+ DPRINT("NtCreateThread() failed!\n");
+ SetLastErrorByStatus(Status);
+ return(NULL);
+ }
+
+ if (lpThreadId != NULL)
+ memcpy(lpThreadId, &ClientId.UniqueThread,sizeof(ULONG));
+
+ return(ThreadHandle);
}
-NT_TEB *GetTeb(VOID)
+PTEB
+GetTeb(VOID)
{
- return NtCurrentTeb();
+ return(NtCurrentTeb());
}
-WINBOOL STDCALL SwitchToThread(VOID)
+WINBOOL STDCALL
+SwitchToThread(VOID)
{
NTSTATUS errCode;
errCode = NtYieldExecution();
return TRUE;
}
-DWORD STDCALL GetCurrentThreadId()
+DWORD STDCALL
+GetCurrentThreadId()
{
return((DWORD)(NtCurrentTeb()->Cid).UniqueThread);
}
-VOID STDCALL ExitThread(DWORD uExitCode)
+VOID STDCALL
+ExitThread(DWORD uExitCode)
{
NTSTATUS errCode;
BOOLEAN LastThread;
-/* $Id: debug.c,v 1.2 2000/05/25 15:50:44 ekohl Exp $
+/* $Id: debug.c,v 1.3 2001/08/03 17:17:16 ekohl Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
/* FUNCTIONS *****************************************************************/
-VOID
-STDCALL
-DbgSsServerThread (
- PVOID Unused
- )
+VOID STDCALL
+DbgSsServerThread(PVOID Unused)
{
LPC_DBGSS_MESSAGE Message;
NTSTATUS Status;
}
-NTSTATUS
-STDCALL
-DbgSsHandleKmApiMsg (
- ULONG Unknown1,
- HANDLE EventHandle
- )
+NTSTATUS STDCALL
+DbgSsHandleKmApiMsg(ULONG Unknown1,
+ HANDLE EventHandle)
{
-
- return STATUS_NOT_IMPLEMENTED;
+ return STATUS_NOT_IMPLEMENTED;
}
-NTSTATUS
-STDCALL
-DbgSsInitialize (
- HANDLE ReplyPort,
- ULONG Unknown1,
- ULONG Unknown2,
- ULONG Unknown3
- )
+NTSTATUS STDCALL
+DbgSsInitialize(HANDLE ReplyPort,
+ ULONG Unknown1,
+ ULONG Unknown2,
+ ULONG Unknown3)
{
SECURITY_QUALITY_OF_SERVICE Qos;
UNICODE_STRING PortName;
}
-NTSTATUS
-STDCALL
-DbgUiConnectToDbg (
- VOID
- )
+NTSTATUS STDCALL
+DbgUiConnectToDbg(VOID)
{
SECURITY_QUALITY_OF_SERVICE Qos;
UNICODE_STRING PortName;
NTSTATUS Status;
- PNT_TEB Teb;
+ PTEB Teb;
ULONG InfoSize;
Teb = NtCurrentTeb ();
}
-NTSTATUS
-STDCALL
-DbgUiContinue (
- PCLIENT_ID ClientId,
- ULONG ContinueStatus
- )
+NTSTATUS STDCALL
+DbgUiContinue(PCLIENT_ID ClientId,
+ ULONG ContinueStatus)
{
- return STATUS_NOT_IMPLEMENTED;
+ return STATUS_NOT_IMPLEMENTED;
}
-NTSTATUS
-STDCALL
-DbgUiWaitStateChange (
- ULONG Unknown1,
- ULONG Unknown2
- )
+
+NTSTATUS STDCALL
+DbgUiWaitStateChange(ULONG Unknown1,
+ ULONG Unknown2)
{
- return STATUS_NOT_IMPLEMENTED;
+ return STATUS_NOT_IMPLEMENTED;
}
/* EOF */
-/* $Id: error.c,v 1.7 2000/07/04 01:26:35 ekohl Exp $
+/* $Id: error.c,v 1.8 2001/08/03 17:18:50 ekohl Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
* REMARK
* RtlNtStatusToDosErrorNoTeb() does the real work.
*/
-DWORD
-STDCALL
-RtlNtStatusToDosErrorNoTeb (NTSTATUS Status)
+DWORD STDCALL
+RtlNtStatusToDosErrorNoTeb(NTSTATUS Status)
{
PERROR_TABLE Table = (PERROR_TABLE)ErrorTable;
* REMARK
* RtlNtStatusToDosErrorNoTeb() does the real work.
*/
-DWORD
-STDCALL
-RtlNtStatusToDosError (
- NTSTATUS Status
- )
+DWORD STDCALL
+RtlNtStatusToDosError(NTSTATUS Status)
{
- PNT_TEB Teb = NtCurrentTeb ();
+ PTEB Teb = NtCurrentTeb();
- if (NULL != Teb)
- {
- Teb->LastStatusValue = Status;
- }
- return RtlNtStatusToDosErrorNoTeb (Status);
+ if (NULL != Teb)
+ {
+ Teb->LastStatusValue = Status;
+ }
+ return RtlNtStatusToDosErrorNoTeb(Status);
}
* REVISIONS
* 1999-11-30 ea
*/
-INT
-STDCALL
-RtlNtStatusToPsxErrno (
- IN NTSTATUS Status
- )
+INT STDCALL
+RtlNtStatusToPsxErrno(IN NTSTATUS Status)
{
#if 0
- switch (Status)
- {
- }
+ switch (Status)
+ {
+ }
#endif
- return -1; /* generic POSIX error */
+ return -1; /* generic POSIX error */
}
/* EOF */
-/* $Id: process.c,v 1.25 2001/06/22 12:36:22 ekohl Exp $
+/* $Id: process.c,v 1.26 2001/08/03 17:18:50 ekohl Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS system libraries
/* FUNCTIONS ****************************************************************/
-HANDLE STDCALL KlCreateFirstThread(HANDLE ProcessHandle,
- ULONG StackSize,
- LPTHREAD_START_ROUTINE lpStartAddress,
- PCLIENT_ID ClientId)
+static NTSTATUS
+RtlpCreateFirstThread(HANDLE ProcessHandle,
+ ULONG StackReserve,
+ ULONG StackCommit,
+ LPTHREAD_START_ROUTINE lpStartAddress,
+ PCLIENT_ID ClientId,
+ PHANDLE ThreadHandle)
{
- NTSTATUS Status;
- HANDLE ThreadHandle;
- OBJECT_ATTRIBUTES ObjectAttributes;
- CONTEXT ThreadContext;
- INITIAL_TEB InitialTeb;
- PVOID BaseAddress;
- CLIENT_ID Cid;
-
- ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
- ObjectAttributes.RootDirectory = NULL;
- ObjectAttributes.ObjectName = NULL;
- ObjectAttributes.Attributes = 0;
- ObjectAttributes.SecurityQualityOfService = NULL;
-
- BaseAddress = NULL;
- Status = NtAllocateVirtualMemory(ProcessHandle,
- &BaseAddress,
- 0,
- (PULONG)&StackSize,
- MEM_COMMIT,
- PAGE_READWRITE);
- if (!NT_SUCCESS(Status))
- {
- DPRINT("Failed to allocate stack\n");
- return(NULL);
- }
-
- memset(&ThreadContext,0,sizeof(CONTEXT));
- ThreadContext.Eip = (ULONG)lpStartAddress;
- ThreadContext.SegGs = USER_DS;
- ThreadContext.SegFs = TEB_SELECTOR;
- ThreadContext.SegEs = USER_DS;
- ThreadContext.SegDs = USER_DS;
- ThreadContext.SegCs = USER_CS;
- ThreadContext.SegSs = USER_DS;
- ThreadContext.Esp = (ULONG)BaseAddress + StackSize - 20;
- ThreadContext.EFlags = (1<<1) + (1<<9);
-
- DPRINT("ThreadContext.Eip %x\n",ThreadContext.Eip);
-
- Status = NtCreateThread(&ThreadHandle,
- THREAD_ALL_ACCESS,
- &ObjectAttributes,
- ProcessHandle,
- &Cid,
- &ThreadContext,
- &InitialTeb,
- FALSE);
- if (ClientId != NULL)
- {
- memcpy(&ClientId->UniqueThread, &Cid.UniqueThread, sizeof(ULONG));
- }
-
- return(ThreadHandle);
+ NTSTATUS Status;
+ OBJECT_ATTRIBUTES ObjectAttributes;
+ CONTEXT ThreadContext;
+ INITIAL_TEB InitialTeb;
+ ULONG OldPageProtection;
+ CLIENT_ID Cid;
+
+ ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
+ ObjectAttributes.RootDirectory = NULL;
+ ObjectAttributes.ObjectName = NULL;
+ ObjectAttributes.Attributes = 0;
+ ObjectAttributes.SecurityQualityOfService = NULL;
+
+ if (StackCommit > PAGESIZE)
+ InitialTeb.StackCommit = StackCommit;
+ else
+ InitialTeb.StackCommit = PAGESIZE;
+
+ if (StackReserve > 0x100000)
+ InitialTeb.StackReserve = StackReserve;
+ else
+ InitialTeb.StackReserve = 0x100000; /* 1MByte */
+
+ /* Reserve stack */
+ InitialTeb.StackAllocate = NULL;
+ Status = NtAllocateVirtualMemory(ProcessHandle,
+ &InitialTeb.StackAllocate,
+ 0,
+ &InitialTeb.StackReserve,
+ MEM_COMMIT, //MEM_RESERVE,
+ PAGE_READWRITE);
+ if (!NT_SUCCESS(Status))
+ {
+ DPRINT("Error reserving stack space!\n");
+ return(Status);
+ }
+
+ DPRINT("StackAllocate: %p ReserveSize: 0x%lX\n",
+ InitialTeb.StackAllocate, InitialTeb.StackReserve);
+
+ InitialTeb.StackBase = (PVOID)((ULONG)InitialTeb.StackAllocate + InitialTeb.StackReserve);
+ InitialTeb.StackLimit = (PVOID)((ULONG)InitialTeb.StackBase - InitialTeb.StackCommit - PAGESIZE);
+ InitialTeb.StackCommit += PAGESIZE;
+
+ DPRINT("StackBase: %p StackCommit: 0x%lX\n",
+ InitialTeb.StackBase, InitialTeb.StackCommit);
+#if 0
+ /* Commit stack */
+ Status = NtAllocateVirtualMemory(ProcessHandle,
+ &InitialTeb.StackLimit,
+ 0,
+ &InitialTeb.StackCommit,
+ MEM_COMMIT,
+ PAGE_READWRITE);
+ if (!NT_SUCCESS(Status))
+ {
+ /* release the stack space */
+ NtFreeVirtualMemory(ProcessHandle,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+
+ DPRINT("Error comitting stack page(s)!\n");
+ return(Status);
+ }
+
+ DPRINT("StackLimit: %p\n", InitialTeb.StackLimit);
+
+ /* Protect guard page */
+ Status = NtProtectVirtualMemory(ProcessHandle,
+ InitialTeb.StackLimit,
+ PAGESIZE,
+ PAGE_GUARD | PAGE_READWRITE,
+ &OldPageProtection);
+ if (!NT_SUCCESS(Status))
+ {
+ /* release the stack space */
+ NtFreeVirtualMemory(ProcessHandle,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+
+ DPRINT("Error comitting guard page!\n");
+ return(Status);
+ }
+#endif
+ memset(&ThreadContext,0,sizeof(CONTEXT));
+ ThreadContext.Eip = (ULONG)lpStartAddress;
+ ThreadContext.SegGs = USER_DS;
+ ThreadContext.SegFs = TEB_SELECTOR;
+ ThreadContext.SegEs = USER_DS;
+ ThreadContext.SegDs = USER_DS;
+ ThreadContext.SegCs = USER_CS;
+ ThreadContext.SegSs = USER_DS;
+ ThreadContext.Esp = (ULONG)InitialTeb.StackBase - 20;
+ ThreadContext.EFlags = (1<<1) + (1<<9);
+
+ DPRINT("ThreadContext.Eip %x\n",ThreadContext.Eip);
+
+ Status = NtCreateThread(ThreadHandle,
+ THREAD_ALL_ACCESS,
+ &ObjectAttributes,
+ ProcessHandle,
+ &Cid,
+ &ThreadContext,
+ &InitialTeb,
+ FALSE);
+ if (!NT_SUCCESS(Status))
+ {
+ NtFreeVirtualMemory(ProcessHandle,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+ return(Status);
+ }
+
+ if (ClientId != NULL)
+ {
+ memcpy(&ClientId->UniqueThread, &Cid.UniqueThread, sizeof(ULONG));
+ }
+
+ return(STATUS_SUCCESS);
}
static NTSTATUS
PVOID EnvPtr = NULL;
ULONG EnvSize = 0;
-
/* create the Environment */
if (Ppb->Environment != NULL)
{
DPRINT("EnvironmentPointer %p\n", EnvPtr);
/* create the PPB */
- PpbBase = (PVOID)PEB_STARTUPINFO;
+ PpbBase = NULL;
PpbSize = Ppb->MaximumLength;
Status = NtAllocateVirtualMemory(ProcessHandle,
&PpbBase,
PRTL_PROCESS_INFO ProcessInfo)
{
HANDLE hSection;
- HANDLE hThread;
NTSTATUS Status;
LPTHREAD_START_ROUTINE lpStartAddress = NULL;
PROCESS_BASIC_INFORMATION ProcessBasicInfo;
ExceptionPort);
if (!NT_SUCCESS(Status))
{
+ NtClose(hSection);
return(Status);
}
if (!NT_SUCCESS(Status))
{
DbgPrint ("LdrGetProcedureAddress failed (Status %x)\n", Status);
+ NtClose(hSection);
return (Status);
}
DPRINT("lpStartAddress 0x%08lx\n", (ULONG)lpStartAddress);
DPRINT("Creating thread for process\n");
- hThread = KlCreateFirstThread(ProcessInfo->ProcessHandle,
+ Status = RtlpCreateFirstThread(ProcessInfo->ProcessHandle,
// Headers.OptionalHeader.SizeOfStackReserve,
0x200000,
+// Headers.OptionalHeader.SizeOfStackCommit,
+ 0x1000,
lpStartAddress,
- &(ProcessInfo->ClientId));
- if (hThread == NULL)
+ &ProcessInfo->ClientId,
+ &ProcessInfo->ThreadHandle);
+ if (!NT_SUCCESS(Status))
{
DPRINT("Failed to create thread\n");
- return(STATUS_UNSUCCESSFUL);
+ NtClose(hSection);
+ return(Status);
}
return(STATUS_SUCCESS);
}
OBJECT_ATTRIBUTES ObjectAttributes;
INITIAL_TEB InitialTeb;
CONTEXT ThreadContext;
- ULONG ReserveSize;
- ULONG CommitSize;
- ULONG GuardSize;
- ULONG RegionSize;
+ ULONG OldPageProtection;
NTSTATUS Status;
/* initialize initial teb */
if ((StackCommit != NULL) && (*StackCommit > PAGESIZE))
- CommitSize = *StackCommit;
+ InitialTeb.StackCommit = *StackCommit;
else
- CommitSize = PAGESIZE;
+ InitialTeb.StackCommit = PAGESIZE;
if ((StackReserve != NULL) && (*StackReserve > 0x100000))
- ReserveSize = *StackReserve;
+ InitialTeb.StackReserve = *StackReserve;
else
- ReserveSize = 0x100000; /* 1MByte */
+ InitialTeb.StackReserve = 0x100000; /* 1MByte */
- GuardSize = PAGESIZE;
-
- RegionSize = 0;
+ /* add size of guard page */
+ InitialTeb.StackCommit += PAGESIZE;
/* Reserve stack */
- InitialTeb.StackReserve = NULL;
+ InitialTeb.StackAllocate = NULL;
Status = NtAllocateVirtualMemory(ProcessHandle,
- &InitialTeb.StackReserve,
+ &InitialTeb.StackAllocate,
0,
- &ReserveSize,
- MEM_RESERVE,
+ &InitialTeb.StackReserve,
+ MEM_COMMIT, //MEM_RESERVE,
PAGE_READWRITE);
-
if (!NT_SUCCESS(Status))
{
DPRINT("Error reserving stack space!\n");
return Status;
}
- DPRINT("StackReserved: %p ReservedSize: 0x%lx\n",
- InitialTeb.StackReserve, ReserveSize);
-
- InitialTeb.StackBase = (PVOID)(InitialTeb.StackReserve + ReserveSize);
- InitialTeb.StackCommit = (PVOID)(InitialTeb.StackBase - CommitSize);
- InitialTeb.StackLimit = (PVOID)(InitialTeb.StackCommit - PAGESIZE);
- InitialTeb.StackCommitMax = (PVOID)(InitialTeb.StackReserve + PAGESIZE);
+ DPRINT("StackAllocate: %p ReserveSize: 0x%lX\n",
+ InitialTeb.StackAllocate, InitialTeb.StackReserve);
- DPRINT("StackBase: %p\n",
- InitialTeb.StackBase);
+ InitialTeb.StackBase = (PVOID)((ULONG)InitialTeb.StackAllocate + InitialTeb.StackReserve);
+ InitialTeb.StackLimit = (PVOID)((ULONG)InitialTeb.StackBase - InitialTeb.StackCommit);
- /* Commit stack page */
+ DPRINT("StackBase: %p StackCommit: 0x%lX\n",
+ InitialTeb.StackBase, InitialTeb.StackCommit);
+#if 0
+ /* Commit stack */
Status = NtAllocateVirtualMemory(ProcessHandle,
- &InitialTeb.StackCommit,
+ &InitialTeb.StackLimit,
0,
- &CommitSize,
+ &InitialTeb.StackCommit,
MEM_COMMIT,
PAGE_READWRITE);
-
if (!NT_SUCCESS(Status))
{
/* release the stack space */
NtFreeVirtualMemory(ProcessHandle,
- InitialTeb.StackReserve,
- &RegionSize,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
MEM_RELEASE);
DPRINT("Error comitting stack page!\n");
return Status;
}
- DPRINT("StackCommit: %p CommitSize: 0x%lx\n",
- InitialTeb.StackCommit, CommitSize);
-
- /* Commit guard page */
- Status = NtAllocateVirtualMemory(ProcessHandle,
- &InitialTeb.StackLimit,
- 0,
- &GuardSize,
- MEM_COMMIT,
- PAGE_GUARD);
-
- if (!NT_SUCCESS(Status))
+ DPRINT("StackLimit: %p\nStackCommit: 0x%lX\n",
+ InitialTeb.StackLimit,
+ InitialTeb.StackCommit);
+
+ /* Protect guard page */
+ Status = NtProtectVirtualMemory(ProcessHandle,
+ InitialTeb.StackLimit,
+ PAGESIZE,
+ PAGE_GUARD | PAGE_READWRITE,
+ &OldPageProtection);
+ if (!NT_SUCCESS(Status))
{
- /* release the stack space */
- NtFreeVirtualMemory(ProcessHandle,
- InitialTeb.StackReserve,
- &RegionSize,
- MEM_RELEASE);
-
- DPRINT("Error comitting guard page!\n");
- return Status;
+ /* release the stack space */
+ NtFreeVirtualMemory(ProcessHandle,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
+ MEM_RELEASE);
+
+ DPRINT("Error protecting guard page!\n");
+ return(Status);
}
-
- DPRINT("StackLimit: %p GuardSize: 0x%lx\n",
- InitialTeb.StackLimit, GuardSize);
-
+#endif
/* initialize thread context */
RtlInitializeContext (ProcessHandle,
&ThreadContext,
ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES);
ObjectAttributes.RootDirectory = NULL;
ObjectAttributes.ObjectName = NULL;
-// ObjectAttributes.Attributes = 0;
ObjectAttributes.Attributes = OBJ_INHERIT;
ObjectAttributes.SecurityDescriptor = SecurityDescriptor;
ObjectAttributes.SecurityQualityOfService = NULL;
{
/* release the stack space */
NtFreeVirtualMemory(ProcessHandle,
- InitialTeb.StackReserve,
- &RegionSize,
+ InitialTeb.StackAllocate,
+ &InitialTeb.StackReserve,
MEM_RELEASE);
DPRINT("Error creating thread!\n");
/* return committed stack size */
if (StackCommit)
- *StackCommit = CommitSize;
+ *StackCommit = InitialTeb.StackCommit;
/* return reserved stack size */
if (StackReserve)
- *StackReserve = ReserveSize;
+ *StackReserve = InitialTeb.StackReserve;
/* return thread handle */
if (ThreadHandle)
ClientId->UniqueThread = LocalClientId.UniqueThread;
}
- return Status;
+ return(STATUS_SUCCESS);
}
NTSTATUS Status;
memset (Context, 0, sizeof(CONTEXT));
-
Context->Eip = (LONG)StartAddress;
Context->SegGs = USER_DS;
Context->SegFs = TEB_SELECTOR;
Buffer[1] = 0xdeadbeef;
Status = NtWriteVirtualMemory(ProcessHandle,
- (PVOID)(InitialTeb->StackBase - 4),
+ (PVOID)((ULONG)InitialTeb->StackBase - 8),
Buffer,
2 * sizeof(ULONG),
&BytesWritten);
NTSTATUS STDCALL
-RtlFreeUserThreadStack (HANDLE ProcessHandle, HANDLE ThreadHandle)
+RtlFreeUserThreadStack(HANDLE ProcessHandle,
+ HANDLE ThreadHandle)
{
- THREAD_BASIC_INFORMATION ThreadInfo;
- NTSTATUS Status;
- ULONG BytesRead;
- ULONG RegionSize;
- PVOID StackBase;
- PNT_TEB Teb;
-
- Status = NtQueryInformationThread (ThreadHandle,
- ThreadBasicInformation,
- &ThreadInfo,
- sizeof(THREAD_BASIC_INFORMATION),
- NULL);
- if (!NT_SUCCESS(Status))
- return Status;
-
- if (ThreadInfo.TebBaseAddress == NULL)
- return Status;
-
- Teb = (PNT_TEB)ThreadInfo.TebBaseAddress;
- Status = NtReadVirtualMemory (ProcessHandle,
- &Teb->DeallocationStack,
- &StackBase,
- sizeof(PVOID),
- &BytesRead);
- if (!NT_SUCCESS(Status))
- return Status;
-
- if (StackBase == NULL)
- return Status;
-
- RegionSize = 0;
- Status = NtFreeVirtualMemory (ProcessHandle,
- StackBase,
- &RegionSize,
- MEM_RELEASE);
-
- return Status;
+ THREAD_BASIC_INFORMATION ThreadInfo;
+ NTSTATUS Status;
+ ULONG BytesRead;
+ ULONG RegionSize;
+ PVOID StackBase;
+ PTEB Teb;
+
+ Status = NtQueryInformationThread(ThreadHandle,
+ ThreadBasicInformation,
+ &ThreadInfo,
+ sizeof(THREAD_BASIC_INFORMATION),
+ NULL);
+ if (!NT_SUCCESS(Status))
+ return(Status);
+
+ if (ThreadInfo.TebBaseAddress == NULL)
+ return(Status);
+
+ Teb = (PTEB)ThreadInfo.TebBaseAddress;
+ Status = NtReadVirtualMemory(ProcessHandle,
+ &Teb->DeallocationStack,
+ &StackBase,
+ sizeof(PVOID),
+ &BytesRead);
+ if (!NT_SUCCESS(Status))
+ return(Status);
+
+ if (StackBase == NULL)
+ return(Status);
+
+ RegionSize = 0;
+ Status = NtFreeVirtualMemory(ProcessHandle,
+ StackBase,
+ &RegionSize,
+ MEM_RELEASE);
+ return(Status);
}
/* EOF */
ULONG StackLimit; /* 1C */
/* Pointer to the thread's environment block in user memory */
- NT_TEB* Teb; /* 20 */
+ PTEB Teb; /* 20 */
/* Pointer to the thread's TLS array */
PVOID TlsArray; /* 24 */
-/* $Id: create.c,v 1.35 2001/07/12 17:21:05 ekohl Exp $
+/* $Id: create.c,v 1.36 2001/08/03 17:15:00 ekohl Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
}
#endif
-VOID PiDeleteThread(PVOID ObjectBody)
+VOID
+PiDeleteThread(PVOID ObjectBody)
{
KIRQL oldIrql;
DPRINT("PiDeleteThread() finished\n");
}
-VOID PiCloseThread(PVOID ObjectBody, ULONG HandleCount)
+VOID
+PiCloseThread(PVOID ObjectBody,
+ ULONG HandleCount)
{
DPRINT("PiCloseThread(ObjectBody %x)\n", ObjectBody);
DPRINT("ObGetReferenceCount(ObjectBody) %d "
static NTSTATUS
PsCreateTeb(HANDLE ProcessHandle,
- PNT_TEB *TebPtr,
+ PTEB *TebPtr,
PETHREAD Thread,
PINITIAL_TEB InitialTeb)
{
ULONG RegionSize;
ULONG TebSize;
PVOID TebBase;
- NT_TEB Teb;
+ TEB Teb;
ULONG ResultLength;
TebBase = (PVOID)0x7FFDE000;
{
Teb.Tib.StackBase = InitialTeb->StackBase;
Teb.Tib.StackLimit = InitialTeb->StackLimit;
-
- /*
- * I don't know if this is really stored in a WNT-TEB,
- * but it's needed to free the thread stack. (Eric Kohl)
- */
- Teb.StackCommit = InitialTeb->StackCommit;
- Teb.StackCommitMax = InitialTeb->StackCommitMax;
- Teb.StackReserve = InitialTeb->StackReserve;
+ Teb.DeallocationStack = InitialTeb->StackAllocate;
}
-
/* more initialization */
Teb.Cid.UniqueThread = Thread->Cid.UniqueThread;
Teb.Cid.UniqueProcess = Thread->Cid.UniqueProcess;
Teb.CurrentLocale = PsDefaultThreadLocaleId;
- DPRINT("sizeof(NT_TEB) %x\n", sizeof(NT_TEB));
+ DPRINT("sizeof(TEB) %x\n", sizeof(TEB));
/* write TEB data into teb page */
Status = NtWriteVirtualMemory(ProcessHandle,
TebBase,
&Teb,
- sizeof(NT_TEB),
+ sizeof(TEB),
&ByteCount);
if (!NT_SUCCESS(Status))
if (TebPtr != NULL)
{
- *TebPtr = (PNT_TEB)TebBase;
+ *TebPtr = (PTEB)TebBase;
}
DPRINT("TEB allocated at %p\n", TebBase);
BOOLEAN CreateSuspended)
{
PETHREAD Thread;
- PNT_TEB TebBase;
+ PTEB TebBase;
NTSTATUS Status;
DPRINT("NtCreateThread(ThreadHandle %x, PCONTEXT %x)\n",
KeBugCheck(0);
}
ThreadContext->Eip = LdrpGetSystemDllEntryPoint;
-#endif
+#endif
Status = Ke386InitThreadWithContext(&Thread->Tcb, ThreadContext);
if (!NT_SUCCESS(Status))
if (Client != NULL)
{
*Client=Thread->Cid;
- }
+ }
/*
* Maybe send a message to the process's debugger
if (ClientId!=NULL)
{
*ClientId=Thread->Cid;
- }
+ }
PsUnblockThread(Thread, NULL);
-/* $Id: error.c,v 1.2 2001/06/20 13:00:53 ekohl Exp $
+/* $Id: error.c,v 1.3 2001/08/03 17:16:30 ekohl Exp $
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
DWORD STDCALL
RtlNtStatusToDosError(IN NTSTATUS Status)
{
- PNT_TEB Teb = NtCurrentTeb ();
+ PTEB Teb = NtCurrentTeb ();
if (NULL != Teb)
{
VOID
-SetLastNtError(
- NTSTATUS Status)
+SetLastNtError(NTSTATUS Status)
{
SetLastWin32Error(RtlNtStatusToDosError(Status));
}
VOID
-SetLastWin32Error(
- DWORD Status)
+SetLastWin32Error(DWORD Status)
{
- PNT_TEB Teb = NtCurrentTeb();
+ PTEB Teb = NtCurrentTeb();
- if (NULL != Teb)
- {
- Teb->LastErrorValue = Status;
- }
+ if (NULL != Teb)
+ {
+ Teb->LastErrorValue = Status;
+ }
}
/* EOF */