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
;
76 KeReleaseThread(PKTHREAD Thread
)
78 * FUNCTION: Releases the resource allocated for a thread by
80 * NOTE: The thread had better not be running when this is called
83 extern unsigned int init_stack
;
85 /* FIXME - lock the process */
86 RemoveEntryList(&Thread
->ThreadListEntry
);
88 if (Thread
->StackLimit
!= (ULONG_PTR
)init_stack
)
90 MmLockAddressSpace(MmGetKernelAddressSpace());
91 MmFreeMemoryAreaByPtr(MmGetKernelAddressSpace(),
92 (PVOID
)Thread
->StackLimit
,
95 MmUnlockAddressSpace(MmGetKernelAddressSpace());
97 Thread
->StackLimit
= 0;
98 Thread
->InitialStack
= NULL
;
99 Thread
->StackBase
= NULL
;
100 Thread
->KernelStack
= NULL
;
101 return(STATUS_SUCCESS
);
109 KeSetKernelStackSwapEnable(
114 BOOLEAN PreviousState
;
116 Thread
= KeGetCurrentThread();
119 PreviousState
= Thread
->EnableStackSwap
;
122 Thread
->EnableStackSwap
= Enable
;
124 /* Return Old State */
125 return PreviousState
;
129 KeInitializeThread(PKPROCESS Process
, PKTHREAD Thread
, BOOLEAN First
)
131 * FUNCTION: Initialize the microkernel state of the thread
136 extern unsigned int init_stack_top
;
137 extern unsigned int init_stack
;
138 PMEMORY_AREA StackArea
;
140 PHYSICAL_ADDRESS BoundaryAddressMultiple
;
142 BoundaryAddressMultiple
.QuadPart
= 0;
144 KeInitializeDispatcherHeader(&Thread
->DispatcherHeader
,
148 InitializeListHead(&Thread
->MutantListHead
);
151 PFN_TYPE Page
[MM_STACK_SIZE
/ PAGE_SIZE
];
154 MmLockAddressSpace(MmGetKernelAddressSpace());
155 Status
= MmCreateMemoryArea(NULL
,
156 MmGetKernelAddressSpace(),
157 MEMORY_AREA_KERNEL_STACK
,
164 BoundaryAddressMultiple
);
165 MmUnlockAddressSpace(MmGetKernelAddressSpace());
167 if (!NT_SUCCESS(Status
))
169 DPRINT1("Failed to create thread stack\n");
172 for (i
= 0; i
< (MM_STACK_SIZE
/ PAGE_SIZE
); i
++)
174 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &Page
[i
]);
175 if (!NT_SUCCESS(Status
))
180 Status
= MmCreateVirtualMapping(NULL
,
184 MM_STACK_SIZE
/ PAGE_SIZE
);
185 if (!NT_SUCCESS(Status
))
189 Thread
->InitialStack
= (PCHAR
)KernelStack
+ MM_STACK_SIZE
;
190 Thread
->StackBase
= (PCHAR
)KernelStack
+ MM_STACK_SIZE
;
191 Thread
->StackLimit
= (ULONG_PTR
)KernelStack
;
192 Thread
->KernelStack
= (PCHAR
)KernelStack
+ MM_STACK_SIZE
;
196 Thread
->InitialStack
= (PCHAR
)init_stack_top
;
197 Thread
->StackBase
= (PCHAR
)init_stack_top
;
198 Thread
->StackLimit
= (ULONG_PTR
)init_stack
;
199 Thread
->KernelStack
= (PCHAR
)init_stack_top
;
203 * Establish the pde's for the new stack and the thread structure within the
204 * address space of the new process. They are accessed while taskswitching or
205 * while handling page faults. At this point it isn't possible to call the
206 * page fault handler for the missing pde's.
209 MmUpdatePageDir((PEPROCESS
)Process
, (PVOID
)Thread
->StackLimit
, MM_STACK_SIZE
);
210 MmUpdatePageDir((PEPROCESS
)Process
, (PVOID
)Thread
, sizeof(ETHREAD
));
213 * The Native API function will initialize the TEB field later
216 Thread
->TlsArray
= NULL
;
217 Thread
->DebugActive
= 0;
218 Thread
->State
= THREAD_STATE_INITIALIZED
;
219 Thread
->Alerted
[0] = 0;
220 Thread
->Alerted
[1] = 0;
223 * FIXME: Think how this might work
225 Thread
->NpxState
= 0;
227 Thread
->Saturation
= 0;
228 Thread
->Priority
= Process
->BasePriority
;
229 InitializeListHead(&Thread
->ApcState
.ApcListHead
[0]);
230 InitializeListHead(&Thread
->ApcState
.ApcListHead
[1]);
231 Thread
->ApcState
.Process
= Process
;
232 Thread
->ApcState
.KernelApcInProgress
= 0;
233 Thread
->ApcState
.KernelApcPending
= 0;
234 Thread
->ApcState
.UserApcPending
= 0;
235 Thread
->ContextSwitches
= 0;
236 Thread
->WaitStatus
= STATUS_SUCCESS
;
237 Thread
->WaitIrql
= PASSIVE_LEVEL
;
238 Thread
->WaitMode
= 0;
239 Thread
->WaitNext
= FALSE
;
240 Thread
->WaitBlockList
= NULL
;
241 Thread
->WaitListEntry
.Flink
= NULL
;
242 Thread
->WaitListEntry
.Blink
= NULL
;
243 Thread
->WaitTime
= 0;
244 Thread
->BasePriority
= Process
->BasePriority
;
245 Thread
->DecrementCount
= 0;
246 Thread
->PriorityDecrement
= 0;
247 Thread
->Quantum
= Process
->ThreadQuantum
;
248 memset(Thread
->WaitBlock
, 0, sizeof(KWAIT_BLOCK
)*4);
249 Thread
->LegoData
= 0;
250 Thread
->UserAffinity
= Process
->Affinity
;
251 Thread
->SystemAffinityActive
= 0;
252 Thread
->PowerState
= 0;
254 Thread
->ServiceTable
= KeServiceDescriptorTable
;
255 Thread
->Queue
= NULL
;
256 KeInitializeSpinLock(&Thread
->ApcQueueLock
);
257 memset(&Thread
->Timer
, 0, sizeof(KTIMER
));
258 KeInitializeTimer(&Thread
->Timer
);
259 Thread
->QueueListEntry
.Flink
= NULL
;
260 Thread
->QueueListEntry
.Blink
= NULL
;
261 Thread
->Affinity
= Process
->Affinity
;
262 Thread
->Preempted
= 0;
263 Thread
->ProcessReadyQueue
= 0;
264 Thread
->KernelStackResident
= 1;
265 Thread
->NextProcessor
= 0;
266 Thread
->CallbackStack
= NULL
;
267 Thread
->Win32Thread
= NULL
;
268 Thread
->TrapFrame
= NULL
;
269 Thread
->ApcStatePointer
[OriginalApcEnvironment
] = &Thread
->ApcState
;
270 Thread
->ApcStatePointer
[AttachedApcEnvironment
] = &Thread
->SavedApcState
;
271 Thread
->EnableStackSwap
= 0;
272 Thread
->LargeStack
= 0;
273 Thread
->ResourceIndex
= 0;
274 Thread
->PreviousMode
= KernelMode
;
275 Thread
->KernelTime
= 0;
276 Thread
->UserTime
= 0;
277 memset(&Thread
->SavedApcState
, 0, sizeof(KAPC_STATE
));
279 Thread
->ApcStateIndex
= OriginalApcEnvironment
;
280 Thread
->ApcQueueable
= TRUE
;
281 Thread
->AutoAlignment
= Process
->AutoAlignment
;
283 KeInitializeApc(&Thread
->SuspendApc
,
285 OriginalApcEnvironment
,
286 PiSuspendThreadKernelRoutine
,
287 PiSuspendThreadRundownRoutine
,
288 PiSuspendThreadNormalRoutine
,
291 KeInitializeSemaphore(&Thread
->SuspendSemaphore
, 0, 128);
293 InsertTailList(&Process
->ThreadListHead
,
294 &Thread
->ThreadListEntry
);
295 Thread
->FreezeCount
= 0;
296 Thread
->SuspendCount
= 0;
299 * Do x86 specific part
308 KeRevertToUserAffinityThread(VOID
)
311 PKTHREAD CurrentThread
;
314 oldIrql
= KeAcquireDispatcherDatabaseLock();
316 CurrentThread
= KeGetCurrentThread();
318 ASSERT(CurrentThread
->SystemAffinityActive
!= FALSE
);
320 /* Return to User Affinity */
321 CurrentThread
->Affinity
= CurrentThread
->UserAffinity
;
323 /* Disable System Affinity */
324 CurrentThread
->SystemAffinityActive
= FALSE
;
326 if (CurrentThread
->Affinity
& (1 << KeGetCurrentProcessorNumber()))
328 KeReleaseDispatcherDatabaseLock(oldIrql
);
332 CurrentThread
->WaitIrql
= oldIrql
;
333 PsDispatchThreadNoLock(THREAD_STATE_READY
);
334 KeLowerIrql(oldIrql
);
344 KeSetIdealProcessorThread (
348 CCHAR PreviousIdealProcessor
;
350 /* Save Old Ideal Processor */
351 PreviousIdealProcessor
= Thread
->IdealProcessor
;
353 /* Set New Ideal Processor */
354 Thread
->IdealProcessor
= Processor
;
356 /* Return Old Ideal Processor */
357 return PreviousIdealProcessor
;
365 KeSetSystemAffinityThread(IN KAFFINITY Affinity
)
368 PKTHREAD CurrentThread
;
371 oldIrql
= KeAcquireDispatcherDatabaseLock();
373 CurrentThread
= KeGetCurrentThread();
375 ASSERT(Affinity
& ((1 << KeNumberProcessors
) - 1));
377 /* Set the System Affinity Specified */
378 CurrentThread
->Affinity
= Affinity
;
380 /* Enable System Affinity */
381 CurrentThread
->SystemAffinityActive
= TRUE
;
383 if (Affinity
& (1 << KeGetCurrentProcessorNumber()))
385 KeReleaseDispatcherDatabaseLock(oldIrql
);
389 CurrentThread
->WaitIrql
= oldIrql
;
390 PsDispatchThreadNoLock(THREAD_STATE_READY
);
391 KeLowerIrql(oldIrql
);
401 KeTerminateThread(IN KPRIORITY Increment
)
403 /* The Increment Argument seems to be ignored by NT and always 0 when called */
405 /* Call our own internal routine */
406 PsTerminateCurrentThread(0);