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
, MEMORY_AREA
* MemoryArea
, PVOID Address
, ULONG PhysAddr
,
47 SWAPENTRY SwapEntry
, BOOLEAN Dirty
)
49 assert(SwapEntry
== 0);
52 MmDereferencePage((PVOID
)PhysAddr
);
57 HalReleaseTask(PETHREAD Thread
)
59 * FUNCTION: Releases the resource allocated for a thread by
60 * HalInitTaskWithContext or HalInitTask
61 * NOTE: The thread had better not be running when this is called
64 extern unsigned int init_stack
;
66 if (Thread
->Tcb
.StackLimit
!= (ULONG
)&init_stack
)
68 MmFreeMemoryArea(MmGetKernelAddressSpace(),
69 (PVOID
)Thread
->Tcb
.StackLimit
,
74 Thread
->Tcb
.StackLimit
= 0;
75 Thread
->Tcb
.InitialStack
= NULL
;
76 Thread
->Tcb
.StackBase
= NULL
;
77 Thread
->Tcb
.KernelStack
= NULL
;
78 return(STATUS_SUCCESS
);
82 KeInitializeThread(PKPROCESS Process
, PKTHREAD Thread
, BOOLEAN First
)
84 * FUNCTION: Initialize the microkernel state of the thread
89 extern unsigned int init_stack_top
;
90 extern unsigned int init_stack
;
91 PMEMORY_AREA StackArea
;
94 KeInitializeDispatcherHeader(&Thread
->DispatcherHeader
,
98 InitializeListHead(&Thread
->MutantListHead
);
103 MmLockAddressSpace(MmGetKernelAddressSpace());
104 Status
= MmCreateMemoryArea(NULL
,
105 MmGetKernelAddressSpace(),
106 MEMORY_AREA_KERNEL_STACK
,
112 MmUnlockAddressSpace(MmGetKernelAddressSpace());
114 if (!NT_SUCCESS(Status
))
116 DPRINT1("Failed to create thread stack\n");
119 for (i
= 0; i
< (MM_STACK_SIZE
/ PAGESIZE
); i
++)
122 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &Page
);
123 if (!NT_SUCCESS(Status
))
127 Status
= MmCreateVirtualMapping(NULL
,
128 KernelStack
+ (i
* PAGESIZE
),
129 PAGE_EXECUTE_READWRITE
,
132 Thread
->InitialStack
= KernelStack
+ MM_STACK_SIZE
;
133 Thread
->StackBase
= KernelStack
+ MM_STACK_SIZE
;
134 Thread
->StackLimit
= (ULONG
)KernelStack
;
135 Thread
->KernelStack
= KernelStack
+ MM_STACK_SIZE
;
139 Thread
->InitialStack
= (PVOID
)&init_stack_top
;
140 Thread
->StackBase
= (PVOID
)&init_stack_top
;
141 Thread
->StackLimit
= (ULONG
)&init_stack
;
142 Thread
->KernelStack
= (PVOID
)&init_stack_top
;
146 * The Native API function will initialize the TEB field later
149 Thread
->TlsArray
= NULL
;
150 Thread
->DebugActive
= 0;
151 Thread
->State
= THREAD_STATE_BLOCKED
;
152 Thread
->Alerted
[0] = 0;
153 Thread
->Alerted
[1] = 0;
156 * FIXME: Think how this might work
158 Thread
->NpxState
= 0;
160 Thread
->Saturation
= 0;
161 Thread
->Priority
= 0;
162 InitializeListHead(&Thread
->ApcState
.ApcListHead
[0]);
163 InitializeListHead(&Thread
->ApcState
.ApcListHead
[1]);
164 Thread
->ApcState
.Process
= Process
;
165 Thread
->ApcState
.KernelApcInProgress
= 0;
166 Thread
->ApcState
.KernelApcPending
= 0;
167 Thread
->ApcState
.UserApcPending
= 0;
168 Thread
->ContextSwitches
= 0;
169 Thread
->WaitStatus
= STATUS_SUCCESS
;
170 Thread
->WaitIrql
= 0;
171 Thread
->WaitMode
= 0;
172 Thread
->WaitNext
= 0;
173 Thread
->WaitBlockList
= NULL
;
174 Thread
->WaitListEntry
.Flink
= NULL
;
175 Thread
->WaitListEntry
.Blink
= NULL
;
176 Thread
->WaitTime
= 0;
177 Thread
->BasePriority
= 0;
178 Thread
->DecrementCount
= 0;
179 Thread
->PriorityDecrement
= 0;
181 memset(Thread
->WaitBlock
, 0, sizeof(KWAIT_BLOCK
)*4);
182 Thread
->LegoData
= 0;
186 Thread
->KernelApcDisable
= 1;
187 Thread
->UserAffinity
= Process
->Affinity
;
188 Thread
->SystemAffinityActive
= 0;
189 Thread
->Queue
= NULL
;
190 KeInitializeSpinLock(&Thread
->ApcQueueLock
);
191 memset(&Thread
->Timer
, 0, sizeof(KTIMER
));
192 Thread
->QueueListEntry
.Flink
= NULL
;
193 Thread
->QueueListEntry
.Blink
= NULL
;
194 Thread
->Affinity
= Process
->Affinity
;
195 Thread
->Preempted
= 0;
196 Thread
->ProcessReadyQueue
= 0;
197 Thread
->KernelStackResident
= 1;
198 Thread
->NextProcessor
= 0;
199 Thread
->CallbackStack
= NULL
;
200 Thread
->Win32Thread
= 0;
201 Thread
->TrapFrame
= NULL
;
202 Thread
->ApcStatePointer
[0] = NULL
;
203 Thread
->ApcStatePointer
[1] = NULL
;
204 Thread
->EnableStackSwap
= 0;
205 Thread
->LargeStack
= 0;
206 Thread
->ResourceIndex
= 0;
207 Thread
->PreviousMode
= KernelMode
;
208 Thread
->KernelTime
= 0;
209 Thread
->UserTime
= 0;
210 memset(&Thread
->SavedApcState
, 0, sizeof(KAPC_STATE
));
211 Thread
->Alertable
= 1;
212 Thread
->ApcStateIndex
= 0;
213 Thread
->ApcQueueable
= 0;
214 Thread
->AutoAlignment
= 0;
215 KeInitializeApc(&Thread
->SuspendApc
,
218 PiSuspendThreadKernelRoutine
,
219 PiSuspendThreadRundownRoutine
,
220 PiSuspendThreadNormalRoutine
,
223 KeInitializeSemaphore(&Thread
->SuspendSemaphore
, 0, 255);
224 Thread
->ThreadListEntry
.Flink
= NULL
;
225 Thread
->ThreadListEntry
.Blink
= NULL
;
226 Thread
->FreezeCount
= 0;
227 Thread
->SuspendCount
= 0;
230 * Initialize ReactOS specific members
232 Thread
->ProcessThreadListEntry
.Flink
= NULL
;
233 Thread
->ProcessThreadListEntry
.Blink
= NULL
;
234 KeInitializeDpc(&Thread
->TimerDpc
,
235 (PKDEFERRED_ROUTINE
)PiTimeoutThread
,
240 * Do x86 specific part