1 /* $Id: create.c,v 1.23 2000/10/07 13:41:54 dwelch Exp $
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ps/thread.c
6 * PURPOSE: Thread managment
7 * PROGRAMMER: David Welch (welch@mcmail.com)
10 * 12/10/99: Phillip Susi: Thread priorities, and APC work
16 * All of the routines that manipulate the thread queue synchronize on
21 /* INCLUDES ****************************************************************/
23 #include <ddk/ntddk.h>
24 #include <internal/ke.h>
25 #include <internal/ob.h>
27 #include <internal/string.h>
28 #include <internal/hal.h>
29 #include <internal/ps.h>
30 #include <internal/ob.h>
31 #include <internal/id.h>
34 #include <internal/debug.h>
36 /* GLOBAL *******************************************************************/
38 static ULONG PiNextThreadUniqueId
= 0;
40 extern KSPIN_LOCK PiThreadListLock
;
41 extern ULONG PiNrThreads
;
43 extern LIST_ENTRY PiThreadListHead
;
45 /* FUNCTIONS ***************************************************************/
48 PsAssignImpersonationToken(PETHREAD Thread
,
52 SECURITY_IMPERSONATION_LEVEL ImpersonationLevel
;
55 if (TokenHandle
!= NULL
)
57 Status
= ObReferenceObjectByHandle(TokenHandle
,
63 if (!NT_SUCCESS(Status
))
67 ImpersonationLevel
= Token
->ImpersonationLevel
;
72 ImpersonationLevel
= 0;
75 PsImpersonateClient(Thread
,
82 ObDereferenceObject(Token
);
84 return(STATUS_SUCCESS
);
87 VOID STDCALL
PsRevertToSelf(PETHREAD Thread
)
89 if (Thread
->ActiveImpersonationInfo
!= 0)
91 Thread
->ActiveImpersonationInfo
= 0;
92 ObDereferenceObject(Thread
->ImpersonationInfo
->Token
);
96 VOID STDCALL
PsImpersonateClient(PETHREAD Thread
,
100 SECURITY_IMPERSONATION_LEVEL Level
)
104 if (Thread
->ActiveImpersonationInfo
!= 0)
106 Thread
->ActiveImpersonationInfo
= 0;
107 if (Thread
->ImpersonationInfo
->Token
!= NULL
)
109 ObDereferenceObject(Thread
->ImpersonationInfo
->Token
);
114 if (Thread
->ActiveImpersonationInfo
== 0 ||
115 Thread
->ImpersonationInfo
== NULL
)
117 Thread
->ImpersonationInfo
= ExAllocatePool(NonPagedPool
,
118 sizeof(PS_IMPERSONATION_INFO
));
120 Thread
->ImpersonationInfo
->Level
= Level
;
121 Thread
->ImpersonationInfo
->Unknown2
= c
;
122 Thread
->ImpersonationInfo
->Unknown1
= b
;
123 Thread
->ImpersonationInfo
->Token
= Token
;
124 ObReferenceObjectByPointer(Token
,
128 Thread
->ActiveImpersonationInfo
= 1;
131 PACCESS_TOKEN
PsReferenceEffectiveToken(PETHREAD Thread
,
132 PTOKEN_TYPE TokenType
,
134 PSECURITY_IMPERSONATION_LEVEL Level
)
139 if (Thread
->ActiveImpersonationInfo
== 0)
141 Process
= Thread
->ThreadsProcess
;
142 *TokenType
= TokenPrimary
;
144 Token
= Process
->Token
;
148 Token
= Thread
->ImpersonationInfo
->Token
;
149 *TokenType
= TokenImpersonation
;
150 *b
= Thread
->ImpersonationInfo
->Unknown2
;
151 *Level
= Thread
->ImpersonationInfo
->Level
;
156 NTSTATUS STDCALL
NtImpersonateThread (IN HANDLE ThreadHandle
,
157 IN HANDLE ThreadToImpersonateHandle
,
158 IN PSECURITY_QUALITY_OF_SERVICE
159 SecurityQualityOfService
)
162 PETHREAD ThreadToImpersonate
;
166 Status
= ObReferenceObjectByHandle(ThreadHandle
,
172 if (!NT_SUCCESS(Status
))
177 Status
= ObReferenceObjectByHandle(ThreadToImpersonateHandle
,
181 (PVOID
*)&ThreadToImpersonate
,
183 if (!NT_SUCCESS(Status
))
185 ObDereferenceObject(Thread
);
189 Status
= SeCreateClientSecurity(ThreadToImpersonate
,
190 SecurityQualityOfService
,
193 if (!NT_SUCCESS(Status
))
195 ObDereferenceObject(Thread
);
196 ObDereferenceObject(ThreadToImpersonate
);
200 SeImpersonateClient(&b
, Thread
);
203 ObDereferenceObject(b
.Token
);
205 return(STATUS_SUCCESS
);
208 NTSTATUS STDCALL
NtOpenThreadToken(IN HANDLE ThreadHandle
,
209 IN ACCESS_MASK DesiredAccess
,
210 IN BOOLEAN OpenAsSelf
,
211 OUT PHANDLE TokenHandle
)
218 Status
= ObReferenceObjectByHandle(ThreadHandle
,
224 if (!NT_SUCCESS(Status
))
229 Token
= PsReferencePrimaryToken(Thread
->ThreadsProcess
);
230 SepCreateImpersonationTokenDacl(Token
);
232 return(STATUS_UNSUCCESSFUL
);
235 PACCESS_TOKEN STDCALL
PsReferenceImpersonationToken(PETHREAD Thread
,
238 SECURITY_IMPERSONATION_LEVEL
*
241 if (Thread
->ActiveImpersonationInfo
== 0)
246 *Level
= Thread
->ImpersonationInfo
->Level
;
247 *Unknown1
= Thread
->ImpersonationInfo
->Unknown1
;
248 *Unknown2
= Thread
->ImpersonationInfo
->Unknown2
;
249 ObReferenceObjectByPointer(Thread
->ImpersonationInfo
->Token
,
253 return(Thread
->ImpersonationInfo
->Token
);
256 VOID
PiTimeoutThread(struct _KDPC
*dpc
,
261 // wake up the thread, and tell it it timed out
262 NTSTATUS Status
= STATUS_TIMEOUT
;
264 DPRINT("PiTimeoutThread()\n");
266 KeRemoveAllWaitsThread((PETHREAD
)Context
, Status
);
269 VOID
PiBeforeBeginThread(CONTEXT c
)
271 DPRINT("PiBeforeBeginThread(Eip %x)\n", c
.Eip
);
272 //KeReleaseSpinLock(&PiThreadListLock, PASSIVE_LEVEL);
273 KeLowerIrql(PASSIVE_LEVEL
);
276 VOID
PsBeginThread(PKSTART_ROUTINE StartRoutine
, PVOID StartContext
)
280 // KeReleaseSpinLock(&PiThreadListLock,PASSIVE_LEVEL);
281 KeLowerIrql(PASSIVE_LEVEL
);
282 Ret
= StartRoutine(StartContext
);
283 PsTerminateSystemThread(Ret
);
287 VOID
PiDeleteThread(PVOID ObjectBody
)
291 DPRINT("PiDeleteThread(ObjectBody %x)\n",ObjectBody
);
293 KeAcquireSpinLock(&PiThreadListLock
, &oldIrql
);
294 DPRINT("Process %x(%d)\n", ((PETHREAD
)ObjectBody
)->ThreadsProcess
,
295 ObGetReferenceCount(((PETHREAD
)ObjectBody
)->ThreadsProcess
));
296 ObDereferenceObject(((PETHREAD
)ObjectBody
)->ThreadsProcess
);
297 ((PETHREAD
)ObjectBody
)->ThreadsProcess
= NULL
;
299 RemoveEntryList(&((PETHREAD
)ObjectBody
)->Tcb
.ThreadListEntry
);
300 HalReleaseTask((PETHREAD
)ObjectBody
);
301 KeReleaseSpinLock(&PiThreadListLock
, oldIrql
);
302 DPRINT("PiDeleteThread() finished\n");
305 VOID
PiCloseThread(PVOID ObjectBody
, ULONG HandleCount
)
307 DPRINT("PiCloseThread(ObjectBody %x)\n", ObjectBody
);
308 DPRINT("ObGetReferenceCount(ObjectBody) %d "
309 "ObGetHandleCount(ObjectBody) %d\n",
310 ObGetReferenceCount(ObjectBody
),
311 ObGetHandleCount(ObjectBody
));
314 NTSTATUS
PsInitializeThread(HANDLE ProcessHandle
,
316 PHANDLE ThreadHandle
,
317 ACCESS_MASK DesiredAccess
,
318 POBJECT_ATTRIBUTES ThreadAttributes
)
328 if (ProcessHandle
!= NULL
)
330 Status
= ObReferenceObjectByHandle(ProcessHandle
,
331 PROCESS_CREATE_THREAD
,
336 if (Status
!= STATUS_SUCCESS
)
338 DPRINT("Failed at %s:%d\n",__FILE__
,__LINE__
);
341 DPRINT( "Creating thread in process %x\n", Process
);
345 Process
= PsInitialSystemProcess
;
346 ObReferenceObjectByPointer(Process
,
347 PROCESS_CREATE_THREAD
,
353 * Create and initialize thread
355 Thread
= ObCreateObject(ThreadHandle
,
359 DPRINT("Thread = %x\n",Thread
);
363 KeInitializeThread(&Process
->Pcb
, &Thread
->Tcb
);
364 Thread
->ThreadsProcess
= Process
;
366 * FIXME: What lock protects this?
368 InsertTailList(&Thread
->ThreadsProcess
->ThreadListHead
,
369 &Thread
->Tcb
.ProcessThreadListEntry
);
370 InitializeListHead(&Thread
->TerminationPortList
);
371 KeInitializeSpinLock(&Thread
->ActiveTimerListLock
);
372 InitializeListHead(&Thread
->IrpList
);
373 Thread
->Cid
.UniqueThread
= (HANDLE
)InterlockedIncrement(
374 &PiNextThreadUniqueId
);
375 Thread
->Cid
.UniqueProcess
= (HANDLE
)Thread
->ThreadsProcess
->UniqueProcessId
;
376 Thread
->DeadThread
= 0;
377 Thread
->Win32ThreadData
= 0;
378 DPRINT("Thread->Cid.UniqueThread %d\n",Thread
->Cid
.UniqueThread
);
382 KeAcquireSpinLock(&PiThreadListLock
, &oldIrql
);
383 InsertTailList(&PiThreadListHead
, &Thread
->Tcb
.ThreadListEntry
);
384 KeReleaseSpinLock(&PiThreadListLock
, oldIrql
);
386 Thread
->Tcb
.BasePriority
= Thread
->ThreadsProcess
->Pcb
.BasePriority
;
387 Thread
->Tcb
.Priority
= Thread
->Tcb
.BasePriority
;
389 return(STATUS_SUCCESS
);
393 static NTSTATUS
PsCreateTeb (HANDLE ProcessHandle
,
396 PINITIAL_TEB InitialTeb
)
398 MEMORY_BASIC_INFORMATION Info
;
407 TebBase
= (PVOID
)0x7FFDE000;
412 Status
= NtQueryVirtualMemory(ProcessHandle
,
414 MemoryBasicInformation
,
418 if (!NT_SUCCESS(Status
))
420 DbgPrint("NtQueryVirtualMemory (Status %x)\n", Status
);
423 if (Info
.State
== MEM_FREE
)
425 /* The TEB must reside in user space */
426 Status
= NtAllocateVirtualMemory(ProcessHandle
,
432 if (NT_SUCCESS(Status
))
438 TebBase
= TebBase
- TebSize
;
441 DPRINT ("TebBase %p TebSize %lu\n", TebBase
, TebSize
);
443 /* set all pointers to and from the TEB */
444 Teb
.Tib
.Self
= TebBase
;
445 if (Thread
->ThreadsProcess
)
447 Teb
.Peb
= Thread
->ThreadsProcess
->Peb
; /* No PEB yet!! */
449 // DPRINT1("Teb.Peb %x\n", Teb.Peb);
451 /* store stack information from InitialTeb */
452 if (InitialTeb
!= NULL
)
454 Teb
.Tib
.StackBase
= InitialTeb
->StackBase
;
455 Teb
.Tib
.StackLimit
= InitialTeb
->StackLimit
;
458 * I don't know if this is really stored in a WNT-TEB,
459 * but it's needed to free the thread stack. (Eric Kohl)
461 Teb
.StackCommit
= InitialTeb
->StackCommit
;
462 Teb
.StackCommitMax
= InitialTeb
->StackCommitMax
;
463 Teb
.StackReserved
= InitialTeb
->StackReserved
;
467 /* more initialization */
468 Teb
.Cid
.UniqueThread
= Thread
->Cid
.UniqueThread
;
469 Teb
.Cid
.UniqueProcess
= Thread
->Cid
.UniqueProcess
;
471 DPRINT("sizeof(NT_TEB) %x\n", sizeof(NT_TEB
));
473 /* write TEB data into teb page */
474 Status
= NtWriteVirtualMemory(ProcessHandle
,
480 if (!NT_SUCCESS(Status
))
483 DPRINT1 ("Writing TEB failed!\n");
486 NtFreeVirtualMemory(ProcessHandle
,
496 *TebPtr
= (PNT_TEB
)TebBase
;
499 // DPRINT1 ("TEB allocated at %p\n", TebBase);
505 NTSTATUS STDCALL
NtCreateThread (PHANDLE ThreadHandle
,
506 ACCESS_MASK DesiredAccess
,
507 POBJECT_ATTRIBUTES ObjectAttributes
,
508 HANDLE ProcessHandle
,
510 PCONTEXT ThreadContext
,
511 PINITIAL_TEB InitialTeb
,
512 BOOLEAN CreateSuspended
)
518 DPRINT("NtCreateThread(ThreadHandle %x, PCONTEXT %x)\n",
519 ThreadHandle
,ThreadContext
);
521 Status
= PsInitializeThread(ProcessHandle
,&Thread
,ThreadHandle
,
522 DesiredAccess
,ObjectAttributes
);
523 if (!NT_SUCCESS(Status
))
529 Status
= NtWriteVirtualMemory(ProcessHandle
,
530 (PVOID
)(((ULONG
)ThreadContext
->Esp
) - 8),
534 if (!NT_SUCCESS(Status
))
536 DPRINT1("NtWriteVirtualMemory failed\n");
539 ThreadContext
->Eip
= LdrpGetSystemDllEntryPoint
;
542 Status
= HalInitTaskWithContext(Thread
,ThreadContext
);
543 if (!NT_SUCCESS(Status
))
548 Status
= PsCreateTeb (ProcessHandle
,
552 if (!NT_SUCCESS(Status
))
557 /* Attention: TebBase is in user memory space */
558 Thread
->Tcb
.Teb
= TebBase
;
560 Thread
->StartAddress
=NULL
;
568 * Maybe send a message to the process's debugger
571 if (ParentProcess
->DebugPort
!= NULL
)
573 LPC_DBG_MESSAGE Message
;
577 Message
.Header
.MessageSize
= sizeof(LPC_DBG_MESSAGE
);
578 Message
.Header
.DataSize
= sizeof(LPC_DBG_MESSAGE
) -
579 sizeof(LPC_MESSAGE_HEADER
);
580 Message
.EventCode
= DBG_EVENT_CREATE_THREAD
;
581 Message
.Data
.CreateThread
.StartAddress
=
583 Message
.Data
.CreateProcess
.Base
= ImageBase
;
584 Message
.Data
.CreateProcess
.EntryPoint
= NULL
; //
586 Status
= LpcSendDebugMessagePort(ParentProcess
->DebugPort
,
591 if (!CreateSuspended
)
593 DPRINT("Not creating suspended\n");
594 PsUnfreezeThread(Thread
, NULL
);
596 DPRINT("Thread %x\n", Thread
);
597 DPRINT("ObGetReferenceCount(Thread) %d ObGetHandleCount(Thread) %x\n",
598 ObGetReferenceCount(Thread
), ObGetHandleCount(Thread
));
599 DPRINT("Finished PsCreateThread()\n");
600 return(STATUS_SUCCESS
);
604 NTSTATUS STDCALL
PsCreateSystemThread(PHANDLE ThreadHandle
,
605 ACCESS_MASK DesiredAccess
,
606 POBJECT_ATTRIBUTES ObjectAttributes
,
607 HANDLE ProcessHandle
,
609 PKSTART_ROUTINE StartRoutine
,
612 * FUNCTION: Creates a thread which executes in kernel mode
614 * ThreadHandle (OUT) = Caller supplied storage for the returned thread
616 * DesiredAccess = Requested access to the thread
617 * ObjectAttributes = Object attributes (optional)
618 * ProcessHandle = Handle of process thread will run in
619 * NULL to use system process
620 * ClientId (OUT) = Caller supplied storage for the returned client id
621 * of the thread (optional)
622 * StartRoutine = Entry point for the thread
623 * StartContext = Argument supplied to the thread when it begins
625 * RETURNS: Success or failure status
631 DPRINT("PsCreateSystemThread(ThreadHandle %x, ProcessHandle %x)\n",
632 ThreadHandle
,ProcessHandle
);
634 Status
= PsInitializeThread(ProcessHandle
,&Thread
,ThreadHandle
,
635 DesiredAccess
,ObjectAttributes
);
636 if (!NT_SUCCESS(Status
))
641 Thread
->StartAddress
=StartRoutine
;
642 Status
= HalInitTask(Thread
,StartRoutine
,StartContext
);
643 if (!NT_SUCCESS(Status
))
650 *ClientId
=Thread
->Cid
;
653 PsUnfreezeThread(Thread
, NULL
);
655 return(STATUS_SUCCESS
);