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.
19 /* $Id: kthread.c,v 1.28 2002/07/10 15:13:33 ekohl Exp $
21 * FILE: ntoskrnl/ke/kthread.c
22 * PURPOSE: Microkernel thread support
23 * PROGRAMMER: David Welch (welch@cwcom.net)
28 /* INCLUDES *****************************************************************/
30 #include <ddk/ntddk.h>
31 #include <internal/ke.h>
32 #include <internal/ps.h>
33 #include <internal/id.h>
34 #include <internal/pool.h>
37 #include <internal/debug.h>
39 /* EXTERN ********************************************************************/
42 //PiTimeoutThread(struct _KDPC Dpc, PVOID Context, PVOID Arg1, PVOID Arg2);
44 /* FUNCTIONS *****************************************************************/
47 KeFreeStackPage(PVOID Context
, MEMORY_AREA
* MemoryArea
, PVOID Address
,
48 PHYSICAL_ADDRESS PhysAddr
, SWAPENTRY SwapEntry
, BOOLEAN Dirty
)
50 assert(SwapEntry
== 0);
51 if (PhysAddr
.QuadPart
!= 0)
53 MmReleasePageMemoryConsumer(MC_NPPOOL
, PhysAddr
);
58 KeReleaseThread(PETHREAD Thread
)
60 * FUNCTION: Releases the resource allocated for a thread by
62 * NOTE: The thread had better not be running when this is called
65 extern unsigned int init_stack
;
67 if (Thread
->Tcb
.StackLimit
!= (ULONG
)&init_stack
)
69 MmLockAddressSpace(MmGetKernelAddressSpace());
70 MmFreeMemoryArea(MmGetKernelAddressSpace(),
71 (PVOID
)Thread
->Tcb
.StackLimit
,
75 MmUnlockAddressSpace(MmGetKernelAddressSpace());
77 Thread
->Tcb
.StackLimit
= 0;
78 Thread
->Tcb
.InitialStack
= NULL
;
79 Thread
->Tcb
.StackBase
= NULL
;
80 Thread
->Tcb
.KernelStack
= NULL
;
81 return(STATUS_SUCCESS
);
85 KeInitializeThread(PKPROCESS Process
, PKTHREAD Thread
, BOOLEAN First
)
87 * FUNCTION: Initialize the microkernel state of the thread
92 extern unsigned int init_stack_top
;
93 extern unsigned int init_stack
;
94 PMEMORY_AREA StackArea
;
97 KeInitializeDispatcherHeader(&Thread
->DispatcherHeader
,
101 InitializeListHead(&Thread
->MutantListHead
);
106 MmLockAddressSpace(MmGetKernelAddressSpace());
107 Status
= MmCreateMemoryArea(NULL
,
108 MmGetKernelAddressSpace(),
109 MEMORY_AREA_KERNEL_STACK
,
115 MmUnlockAddressSpace(MmGetKernelAddressSpace());
117 if (!NT_SUCCESS(Status
))
119 DPRINT1("Failed to create thread stack\n");
122 for (i
= 0; i
< (MM_STACK_SIZE
/ PAGESIZE
); i
++)
124 PHYSICAL_ADDRESS Page
;
125 Status
= MmRequestPageMemoryConsumer(MC_NPPOOL
, TRUE
, &Page
);
126 if (!NT_SUCCESS(Status
))
130 Status
= MmCreateVirtualMapping(NULL
,
131 KernelStack
+ (i
* PAGESIZE
),
132 PAGE_EXECUTE_READWRITE
,
136 Thread
->InitialStack
= KernelStack
+ MM_STACK_SIZE
;
137 Thread
->StackBase
= KernelStack
+ MM_STACK_SIZE
;
138 Thread
->StackLimit
= (ULONG
)KernelStack
;
139 Thread
->KernelStack
= KernelStack
+ MM_STACK_SIZE
;
143 Thread
->InitialStack
= (PVOID
)&init_stack_top
;
144 Thread
->StackBase
= (PVOID
)&init_stack_top
;
145 Thread
->StackLimit
= (ULONG
)&init_stack
;
146 Thread
->KernelStack
= (PVOID
)&init_stack_top
;
150 * The Native API function will initialize the TEB field later
153 Thread
->TlsArray
= NULL
;
154 Thread
->DebugActive
= 0;
155 Thread
->State
= THREAD_STATE_INITIALIZED
;
156 Thread
->Alerted
[0] = 0;
157 Thread
->Alerted
[1] = 0;
160 * FIXME: Think how this might work
162 Thread
->NpxState
= 0;
164 Thread
->Saturation
= 0;
165 Thread
->Priority
= 0;
166 InitializeListHead(&Thread
->ApcState
.ApcListHead
[0]);
167 InitializeListHead(&Thread
->ApcState
.ApcListHead
[1]);
168 Thread
->ApcState
.Process
= Process
;
169 Thread
->ApcState
.KernelApcInProgress
= 0;
170 Thread
->ApcState
.KernelApcPending
= 0;
171 Thread
->ApcState
.UserApcPending
= 0;
172 Thread
->ContextSwitches
= 0;
173 Thread
->WaitStatus
= STATUS_SUCCESS
;
174 Thread
->WaitIrql
= 0;
175 Thread
->WaitMode
= 0;
176 Thread
->WaitNext
= 0;
177 Thread
->WaitBlockList
= NULL
;
178 Thread
->WaitListEntry
.Flink
= NULL
;
179 Thread
->WaitListEntry
.Blink
= NULL
;
180 Thread
->WaitTime
= 0;
181 Thread
->BasePriority
= 0;
182 Thread
->DecrementCount
= 0;
183 Thread
->PriorityDecrement
= 0;
185 memset(Thread
->WaitBlock
, 0, sizeof(KWAIT_BLOCK
)*4);
186 Thread
->LegoData
= 0;
190 Thread
->KernelApcDisable
= 1;
191 Thread
->UserAffinity
= Process
->Affinity
;
192 Thread
->SystemAffinityActive
= 0;
193 Thread
->Queue
= NULL
;
194 KeInitializeSpinLock(&Thread
->ApcQueueLock
);
195 memset(&Thread
->Timer
, 0, sizeof(KTIMER
));
196 Thread
->QueueListEntry
.Flink
= NULL
;
197 Thread
->QueueListEntry
.Blink
= NULL
;
198 Thread
->Affinity
= Process
->Affinity
;
199 Thread
->Preempted
= 0;
200 Thread
->ProcessReadyQueue
= 0;
201 Thread
->KernelStackResident
= 1;
202 Thread
->NextProcessor
= 0;
203 Thread
->CallbackStack
= NULL
;
204 Thread
->Win32Thread
= 0;
205 Thread
->TrapFrame
= NULL
;
206 Thread
->ApcStatePointer
[0] = NULL
;
207 Thread
->ApcStatePointer
[1] = NULL
;
208 Thread
->EnableStackSwap
= 0;
209 Thread
->LargeStack
= 0;
210 Thread
->ResourceIndex
= 0;
211 Thread
->PreviousMode
= KernelMode
;
212 Thread
->KernelTime
= 0;
213 Thread
->UserTime
= 0;
214 memset(&Thread
->SavedApcState
, 0, sizeof(KAPC_STATE
));
215 Thread
->Alertable
= 1;
216 Thread
->ApcStateIndex
= 0;
217 Thread
->ApcQueueable
= 0;
218 Thread
->AutoAlignment
= 0;
219 KeInitializeApc(&Thread
->SuspendApc
,
222 PiSuspendThreadKernelRoutine
,
223 PiSuspendThreadRundownRoutine
,
224 PiSuspendThreadNormalRoutine
,
227 KeInitializeSemaphore(&Thread
->SuspendSemaphore
, 0, 255);
228 Thread
->ThreadListEntry
.Flink
= NULL
;
229 Thread
->ThreadListEntry
.Blink
= NULL
;
230 Thread
->FreezeCount
= 0;
231 Thread
->SuspendCount
= 0;
234 * Initialize ReactOS specific members
236 Thread
->ProcessThreadListEntry
.Flink
= NULL
;
237 Thread
->ProcessThreadListEntry
.Blink
= NULL
;
238 KeInitializeDpc(&Thread
->TimerDpc
,
239 (PKDEFERRED_ROUTINE
)PiTimeoutThread
,
244 * Do x86 specific part