3 * Copyright (C) 2000 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * FILE: ntoskrnl/ke/kthread.c
21 * PURPOSE: Microkernel thread support
22 * PROGRAMMER: David Welch (welch@cwcom.net)
27 /* INCLUDES *****************************************************************/
29 #include <ddk/ntddk.h>
30 #include <internal/ke.h>
31 #include <internal/ps.h>
32 #include <internal/id.h>
33 #include <internal/pool.h>
36 #include <internal/debug.h>
38 /* EXTERN ********************************************************************/
41 //PiTimeoutThread(struct _KDPC Dpc, PVOID Context, PVOID Arg1, PVOID Arg2);
43 /* FUNCTIONS *****************************************************************/
46 KeFreeStackPage(PVOID Context
, PVOID Address
, ULONG PhysAddr
)
50 MmDereferencePage((PVOID
)PhysAddr
);
55 HalReleaseTask(PETHREAD Thread
)
57 * FUNCTION: Releases the resource allocated for a thread by
58 * HalInitTaskWithContext or HalInitTask
59 * NOTE: The thread had better not be running when this is called
62 extern unsigned int init_stack
;
64 if (Thread
->Tcb
.StackLimit
!= (ULONG
)&init_stack
)
66 MmFreeMemoryArea(MmGetKernelAddressSpace(),
67 (PVOID
)Thread
->Tcb
.StackLimit
,
72 Thread
->Tcb
.StackLimit
= 0;
73 Thread
->Tcb
.InitialStack
= NULL
;
74 Thread
->Tcb
.StackBase
= NULL
;
75 Thread
->Tcb
.KernelStack
= NULL
;
76 return(STATUS_SUCCESS
);
80 KeInitializeThread(PKPROCESS Process
, PKTHREAD Thread
, BOOLEAN First
)
82 * FUNCTION: Initialize the microkernel state of the thread
87 extern unsigned int init_stack_top
;
88 extern unsigned int init_stack
;
89 PMEMORY_AREA StackArea
;
92 KeInitializeDispatcherHeader(&Thread
->DispatcherHeader
,
96 InitializeListHead(&Thread
->MutantListHead
);
101 MmLockAddressSpace(MmGetKernelAddressSpace());
102 Status
= MmCreateMemoryArea(NULL
,
103 MmGetKernelAddressSpace(),
104 MEMORY_AREA_KERNEL_STACK
,
110 MmUnlockAddressSpace(MmGetKernelAddressSpace());
112 if (!NT_SUCCESS(Status
))
114 DPRINT1("Failed to create thread stack\n");
117 for (i
= 0; i
< (MM_STACK_SIZE
/ PAGESIZE
); i
++)
119 Status
= MmCreateVirtualMapping(NULL
,
120 KernelStack
+ (i
* PAGESIZE
),
121 PAGE_EXECUTE_READWRITE
,
122 (ULONG
)MmAllocPage(0));
124 Thread
->InitialStack
= KernelStack
+ MM_STACK_SIZE
;
125 Thread
->StackBase
= KernelStack
+ MM_STACK_SIZE
;
126 Thread
->StackLimit
= (ULONG
)KernelStack
;
127 Thread
->KernelStack
= KernelStack
+ MM_STACK_SIZE
;
131 Thread
->InitialStack
= (PVOID
)&init_stack_top
;
132 Thread
->StackBase
= (PVOID
)&init_stack_top
;
133 Thread
->StackLimit
= (ULONG
)&init_stack
;
134 Thread
->KernelStack
= (PVOID
)&init_stack_top
;
138 * The Native API function will initialize the TEB field later
141 Thread
->TlsArray
= NULL
;
142 Thread
->DebugActive
= 0;
143 Thread
->State
= THREAD_STATE_BLOCKED
;
144 Thread
->Alerted
[0] = 0;
145 Thread
->Alerted
[1] = 0;
148 * FIXME: Think how this might work
150 Thread
->NpxState
= 0;
152 Thread
->Saturation
= 0;
153 Thread
->Priority
= 0;
154 InitializeListHead(&Thread
->ApcState
.ApcListHead
[0]);
155 InitializeListHead(&Thread
->ApcState
.ApcListHead
[1]);
156 Thread
->ApcState
.Process
= Process
;
157 Thread
->ApcState
.KernelApcInProgress
= 0;
158 Thread
->ApcState
.KernelApcPending
= 0;
159 Thread
->ApcState
.UserApcPending
= 0;
160 Thread
->ContextSwitches
= 0;
161 Thread
->WaitStatus
= STATUS_SUCCESS
;
162 Thread
->WaitIrql
= 0;
163 Thread
->WaitMode
= 0;
164 Thread
->WaitNext
= 0;
165 Thread
->WaitBlockList
= NULL
;
166 Thread
->WaitListEntry
.Flink
= NULL
;
167 Thread
->WaitListEntry
.Blink
= NULL
;
168 Thread
->WaitTime
= 0;
169 Thread
->BasePriority
= 0;
170 Thread
->DecrementCount
= 0;
171 Thread
->PriorityDecrement
= 0;
173 memset(Thread
->WaitBlock
, 0, sizeof(KWAIT_BLOCK
)*4);
174 Thread
->LegoData
= 0;
178 Thread
->KernelApcDisable
= 1;
179 Thread
->UserAffinity
= Process
->Affinity
;
180 Thread
->SystemAffinityActive
= 0;
181 Thread
->Queue
= NULL
;
182 KeInitializeSpinLock(&Thread
->ApcQueueLock
);
183 memset(&Thread
->Timer
, 0, sizeof(KTIMER
));
184 Thread
->QueueListEntry
.Flink
= NULL
;
185 Thread
->QueueListEntry
.Blink
= NULL
;
186 Thread
->Affinity
= Process
->Affinity
;
187 Thread
->Preempted
= 0;
188 Thread
->ProcessReadyQueue
= 0;
189 Thread
->KernelStackResident
= 1;
190 Thread
->NextProcessor
= 0;
191 Thread
->CallbackStack
= NULL
;
192 Thread
->Win32Thread
= 0;
193 Thread
->TrapFrame
= NULL
;
194 Thread
->ApcStatePointer
[0] = NULL
;
195 Thread
->ApcStatePointer
[1] = NULL
;
196 Thread
->EnableStackSwap
= 0;
197 Thread
->LargeStack
= 0;
198 Thread
->ResourceIndex
= 0;
199 Thread
->PreviousMode
= KernelMode
;
200 Thread
->KernelTime
= 0;
201 Thread
->UserTime
= 0;
202 memset(&Thread
->SavedApcState
, 0, sizeof(KAPC_STATE
));
203 Thread
->Alertable
= 1;
204 Thread
->ApcStateIndex
= 0;
205 Thread
->ApcQueueable
= 0;
206 Thread
->AutoAlignment
= 0;
207 KeInitializeApc(&Thread
->SuspendApc
,
210 PiSuspendThreadKernelRoutine
,
211 PiSuspendThreadRundownRoutine
,
212 PiSuspendThreadNormalRoutine
,
215 KeInitializeSemaphore(&Thread
->SuspendSemaphore
, 0, 255);
216 Thread
->ThreadListEntry
.Flink
= NULL
;
217 Thread
->ThreadListEntry
.Blink
= NULL
;
218 Thread
->FreezeCount
= 0;
219 Thread
->SuspendCount
= 0;
222 * Initialize ReactOS specific members
224 Thread
->ProcessThreadListEntry
.Flink
= NULL
;
225 Thread
->ProcessThreadListEntry
.Blink
= NULL
;
226 KeInitializeDpc(&Thread
->TimerDpc
,
227 (PKDEFERRED_ROUTINE
)PiTimeoutThread
,
232 * Do x86 specific part