3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ke/kthread.c
6 * PURPOSE: Microkernel thread support
8 * PROGRAMMERS: David Welch (welch@cwcom.net)
11 /* INCLUDES *****************************************************************/
15 #include <internal/debug.h>
17 /* FUNCTIONS *****************************************************************/
24 Thread
= PsGetCurrentThread();
26 if (Thread
->Tcb
.ServiceTable
!= KeServiceDescriptorTableShadow
)
28 PsInitWin32Thread (Thread
);
30 Thread
->Tcb
.ServiceTable
= KeServiceDescriptorTableShadow
;
39 KeCapturePersistentThreadState(
40 IN PVOID CurrentThread
,
53 KeFreeStackPage(PVOID Context
, MEMORY_AREA
* MemoryArea
, PVOID Address
,
54 PFN_TYPE Page
, SWAPENTRY SwapEntry
, BOOLEAN Dirty
)
56 ASSERT(SwapEntry
== 0);
59 MmReleasePageMemoryConsumer(MC_NPPOOL
, Page
);
68 KeQueryPriorityThread (
72 return Thread
->Priority
;
85 /* Return the User Time */
86 *UserTime
= Thread
->UserTime
;
88 /* Return the Kernel Time */
89 return Thread
->KernelTime
;
93 KeReleaseThread(PKTHREAD Thread
)
95 * FUNCTION: Releases the resource allocated for a thread by
97 * NOTE: The thread had better not be running when this is called
100 extern unsigned int init_stack
;
102 /* FIXME - lock the process */
103 RemoveEntryList(&Thread
->ThreadListEntry
);
105 if (Thread
->StackLimit
!= (ULONG_PTR
)init_stack
)
107 MmLockAddressSpace(MmGetKernelAddressSpace());
108 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
109 (PVOID
)Thread
->StackLimit
,
112 MmUnlockAddressSpace(MmGetKernelAddressSpace());
114 Thread
->StackLimit
= 0;
115 Thread
->InitialStack
= NULL
;
116 Thread
->StackBase
= NULL
;
117 Thread
->KernelStack
= NULL
;
118 return(STATUS_SUCCESS
);
126 KeSetKernelStackSwapEnable(
131 BOOLEAN PreviousState
;
133 Thread
= KeGetCurrentThread();
136 PreviousState
= Thread
->EnableStackSwap
;
139 Thread
->EnableStackSwap
= Enable
;
141 /* Return Old State */
142 return PreviousState
;
146 KeInitializeThread(PKPROCESS Process
, PKTHREAD Thread
, BOOLEAN First
)
148 * FUNCTION: Initialize the microkernel state of the thread
153 extern unsigned int init_stack_top
;
154 extern unsigned int init_stack
;
155 PMEMORY_AREA StackArea
;
157 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
159 BoundaryAddressMultiple
.QuadPart
= 0;
161 KeInitializeDispatcherHeader(&Thread
->DispatcherHeader
,
165 InitializeListHead(&Thread
->MutantListHead
);
168 PFN_TYPE Page
[MM_STACK_SIZE
/ PAGE_SIZE
];
171 MmLockAddressSpace(MmGetKernelAddressSpace());
172 Status
= MmCreateMemoryArea(NULL
,
173 MmGetKernelAddressSpace(),
174 MEMORY_AREA_KERNEL_STACK
,
181 BoundaryAddressMultiple
);
182 MmUnlockAddressSpace(MmGetKernelAddressSpace());
184 if (!NT_SUCCESS(Status
))
186 DPRINT1("Failed to create thread stack\n");
189 for (i
= 0; i
< (MM_STACK_SIZE
/ PAGE_SIZE
); i
++)
191 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &Page
[i
]);
192 if (!NT_SUCCESS(Status
))
197 Status
= MmCreateVirtualMapping(NULL
,
201 MM_STACK_SIZE
/ PAGE_SIZE
);
202 if (!NT_SUCCESS(Status
))
206 Thread
->InitialStack
= (PCHAR
)KernelStack
+ MM_STACK_SIZE
;
207 Thread
->StackBase
= (PCHAR
)KernelStack
+ MM_STACK_SIZE
;
208 Thread
->StackLimit
= (ULONG_PTR
)KernelStack
;
209 Thread
->KernelStack
= (PCHAR
)KernelStack
+ MM_STACK_SIZE
;
213 Thread
->InitialStack
= (PCHAR
)init_stack_top
;
214 Thread
->StackBase
= (PCHAR
)init_stack_top
;
215 Thread
->StackLimit
= (ULONG_PTR
)init_stack
;
216 Thread
->KernelStack
= (PCHAR
)init_stack_top
;
220 * Establish the pde's for the new stack and the thread structure within the
221 * address space of the new process. They are accessed while taskswitching or
222 * while handling page faults. At this point it isn't possible to call the
223 * page fault handler for the missing pde's.
226 MmUpdatePageDir((PEPROCESS
)Process
, (PVOID
)Thread
->StackLimit
, MM_STACK_SIZE
);
227 MmUpdatePageDir((PEPROCESS
)Process
, (PVOID
)Thread
, sizeof(ETHREAD
));
230 * The Native API function will initialize the TEB field later
233 Thread
->TlsArray
= NULL
;
234 Thread
->DebugActive
= 0;
235 Thread
->State
= THREAD_STATE_INITIALIZED
;
236 Thread
->Alerted
[0] = 0;
237 Thread
->Alerted
[1] = 0;
239 Thread
->NpxState
= NPX_STATE_INVALID
;
241 Thread
->Saturation
= 0;
242 Thread
->Priority
= Process
->BasePriority
;
243 InitializeListHead(&Thread
->ApcState
.ApcListHead
[0]);
244 InitializeListHead(&Thread
->ApcState
.ApcListHead
[1]);
245 Thread
->ApcState
.Process
= Process
;
246 Thread
->ApcState
.KernelApcInProgress
= 0;
247 Thread
->ApcState
.KernelApcPending
= 0;
248 Thread
->ApcState
.UserApcPending
= 0;
249 Thread
->ContextSwitches
= 0;
250 Thread
->WaitStatus
= STATUS_SUCCESS
;
251 Thread
->WaitIrql
= PASSIVE_LEVEL
;
252 Thread
->WaitMode
= 0;
253 Thread
->WaitNext
= FALSE
;
254 Thread
->WaitBlockList
= NULL
;
255 Thread
->WaitListEntry
.Flink
= NULL
;
256 Thread
->WaitListEntry
.Blink
= NULL
;
257 Thread
->WaitTime
= 0;
258 Thread
->BasePriority
= Process
->BasePriority
;
259 Thread
->DecrementCount
= 0;
260 Thread
->PriorityDecrement
= 0;
261 Thread
->Quantum
= Process
->ThreadQuantum
;
262 RtlZeroMemory(Thread
->WaitBlock
, sizeof(KWAIT_BLOCK
)*4);
263 Thread
->LegoData
= 0;
264 Thread
->UserAffinity
= Process
->Affinity
;
265 Thread
->SystemAffinityActive
= 0;
266 Thread
->PowerState
= 0;
268 Thread
->ServiceTable
= KeServiceDescriptorTable
;
269 Thread
->Queue
= NULL
;
270 KeInitializeSpinLock(&Thread
->ApcQueueLock
);
271 RtlZeroMemory(&Thread
->Timer
, sizeof(KTIMER
));
272 KeInitializeTimer(&Thread
->Timer
);
273 Thread
->QueueListEntry
.Flink
= NULL
;
274 Thread
->QueueListEntry
.Blink
= NULL
;
275 Thread
->Affinity
= Process
->Affinity
;
276 Thread
->Preempted
= 0;
277 Thread
->ProcessReadyQueue
= 0;
278 Thread
->KernelStackResident
= 1;
279 Thread
->NextProcessor
= 0;
280 Thread
->CallbackStack
= NULL
;
281 Thread
->Win32Thread
= NULL
;
282 Thread
->TrapFrame
= NULL
;
283 Thread
->ApcStatePointer
[OriginalApcEnvironment
] = &Thread
->ApcState
;
284 Thread
->ApcStatePointer
[AttachedApcEnvironment
] = &Thread
->SavedApcState
;
285 Thread
->EnableStackSwap
= 0;
286 Thread
->LargeStack
= 0;
287 Thread
->ResourceIndex
= 0;
288 Thread
->PreviousMode
= KernelMode
;
289 Thread
->KernelTime
= 0;
290 Thread
->UserTime
= 0;
291 RtlZeroMemory(&Thread
->SavedApcState
, sizeof(KAPC_STATE
));
293 Thread
->ApcStateIndex
= OriginalApcEnvironment
;
294 Thread
->ApcQueueable
= TRUE
;
295 Thread
->AutoAlignment
= Process
->AutoAlignment
;
297 KeInitializeApc(&Thread
->SuspendApc
,
299 OriginalApcEnvironment
,
300 PiSuspendThreadKernelRoutine
,
301 PiSuspendThreadRundownRoutine
,
302 PiSuspendThreadNormalRoutine
,
305 KeInitializeSemaphore(&Thread
->SuspendSemaphore
, 0, 128);
307 InsertTailList(&Process
->ThreadListHead
,
308 &Thread
->ThreadListEntry
);
309 Thread
->FreezeCount
= 0;
310 Thread
->SuspendCount
= 0;
313 * Do x86 specific part
322 KeRevertToUserAffinityThread(VOID
)
325 PKTHREAD CurrentThread
;
328 oldIrql
= KeAcquireDispatcherDatabaseLock();
330 CurrentThread
= KeGetCurrentThread();
332 ASSERT(CurrentThread
->SystemAffinityActive
!= FALSE
);
334 /* Return to User Affinity */
335 CurrentThread
->Affinity
= CurrentThread
->UserAffinity
;
337 /* Disable System Affinity */
338 CurrentThread
->SystemAffinityActive
= FALSE
;
340 if (CurrentThread
->Affinity
& (1 << KeGetCurrentProcessorNumber()))
342 KeReleaseDispatcherDatabaseLock(oldIrql
);
346 CurrentThread
->WaitIrql
= oldIrql
;
347 PsDispatchThreadNoLock(THREAD_STATE_READY
);
348 KeLowerIrql(oldIrql
);
358 KeSetIdealProcessorThread (
362 CCHAR PreviousIdealProcessor
;
364 /* Save Old Ideal Processor */
365 PreviousIdealProcessor
= Thread
->IdealProcessor
;
367 /* Set New Ideal Processor */
368 Thread
->IdealProcessor
= Processor
;
370 /* Return Old Ideal Processor */
371 return PreviousIdealProcessor
;
379 KeSetSystemAffinityThread(IN KAFFINITY Affinity
)
382 PKTHREAD CurrentThread
;
385 oldIrql
= KeAcquireDispatcherDatabaseLock();
387 CurrentThread
= KeGetCurrentThread();
389 ASSERT(Affinity
& ((1 << KeNumberProcessors
) - 1));
391 /* Set the System Affinity Specified */
392 CurrentThread
->Affinity
= Affinity
;
394 /* Enable System Affinity */
395 CurrentThread
->SystemAffinityActive
= TRUE
;
397 if (Affinity
& (1 << KeGetCurrentProcessorNumber()))
399 KeReleaseDispatcherDatabaseLock(oldIrql
);
403 CurrentThread
->WaitIrql
= oldIrql
;
404 PsDispatchThreadNoLock(THREAD_STATE_READY
);
405 KeLowerIrql(oldIrql
);
415 KeTerminateThread(IN KPRIORITY Increment
)
417 /* The Increment Argument seems to be ignored by NT and always 0 when called */
419 /* Call our own internal routine */
420 PsTerminateCurrentThread(0);
426 NtDelayExecution(IN BOOLEAN Alertable
,
427 IN PLARGE_INTEGER DelayInterval
)
429 KPROCESSOR_MODE PreviousMode
;
430 LARGE_INTEGER SafeInterval
;
432 PreviousMode
= ExGetPreviousMode();
434 if(PreviousMode
!= KernelMode
)
436 NTSTATUS Status
= STATUS_SUCCESS
;
440 ProbeForRead(DelayInterval
,
441 sizeof(LARGE_INTEGER
),
443 /* make a copy on the kernel stack and let DelayInterval point to it so
444 we don't need to wrap KeDelayExecutionThread in SEH! */
445 SafeInterval
= *DelayInterval
;
446 DelayInterval
= &SafeInterval
;
450 Status
= _SEH_GetExceptionCode();
454 if(!NT_SUCCESS(Status
))
460 return KeDelayExecutionThread(PreviousMode
,