e6f1c819b9a04962a4a1b44195906bf1cfeeeae6
[reactos.git] / reactos / ntoskrnl / ke / process.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ke/process.c
5 * PURPOSE: Attaching/Detaching and System Call Tables
6 *
7 * PROGRAMMERS: Alex Ionescu (Implemented Attach/Detach and KeRemoveSystemServiceTable)
8 * Gregor Anich (Bugfixes to Attach Functions)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #include <internal/napi.h>
15 #define NDEBUG
16 #include <internal/debug.h>
17
18 /* GLOBALS *****************************************************************/
19
20 KSERVICE_TABLE_DESCRIPTOR
21 __declspec(dllexport)
22 KeServiceDescriptorTable[SSDT_MAX_ENTRIES] = {
23 { MainSSDT, NULL, NUMBER_OF_SYSCALLS, MainSSPT },
24 { NULL, NULL, 0, NULL },
25 { NULL, NULL, 0, NULL },
26 { NULL, NULL, 0, NULL }
27 };
28
29 KSERVICE_TABLE_DESCRIPTOR
30 KeServiceDescriptorTableShadow[SSDT_MAX_ENTRIES] = {
31 { MainSSDT, NULL, NUMBER_OF_SYSCALLS, MainSSPT },
32 { NULL, NULL, 0, NULL },
33 { NULL, NULL, 0, NULL },
34 { NULL, NULL, 0, NULL }
35 };
36
37 /* FUNCTIONS *****************************************************************/
38
39 static __inline void
40 UpdatePageDirs(PKTHREAD Thread, PKPROCESS Process)
41 {
42 /*
43 * The stack and the thread structure of the current process may be
44 * located in a page which is not present in the page directory of
45 * the process we're attaching to. That would lead to a page fault
46 * when this function returns. However, since the processor can't
47 * call the page fault handler 'cause it can't push EIP on the stack,
48 * this will show up as a stack fault which will crash the entire system.
49 * To prevent this, make sure the page directory of the process we're
50 * attaching to is up-to-date.
51 */
52 MmUpdatePageDir((PEPROCESS)Process, (PVOID)Thread->StackLimit, MM_STACK_SIZE);
53 MmUpdatePageDir((PEPROCESS)Process, (PVOID)Thread, sizeof(ETHREAD));
54 }
55
56 /*
57 * FUNCTION: Returns a pointer to the current process
58 */
59 PKPROCESS
60 STDCALL
61 KeGetCurrentProcess(VOID)
62 {
63 return(&(PsGetCurrentProcess()->Pcb));
64 }
65
66 VOID
67 STDCALL
68 KeInitializeProcess(PKPROCESS Process,
69 KPRIORITY Priority,
70 KAFFINITY Affinity,
71 LARGE_INTEGER DirectoryTableBase)
72 {
73 DPRINT("KeInitializeProcess. Process: %x, DirectoryTableBase: %x\n", Process, DirectoryTableBase);
74
75 /* Initialize the Dispatcher Header */
76 KeInitializeDispatcherHeader(&Process->Header,
77 ProcessObject,
78 sizeof(KPROCESS),
79 FALSE);
80
81 /* Initialize Scheduler Data, Disable Alignment Faults and Set the PDE */
82 Process->Affinity = Affinity;
83 Process->BasePriority = Priority;
84 Process->QuantumReset = 6;
85 Process->DirectoryTableBase = DirectoryTableBase;
86 Process->AutoAlignment = TRUE;
87 Process->IopmOffset = 0xFFFF;
88 Process->State = ProcessInMemory;
89
90 /* Initialize the Thread List */
91 InitializeListHead(&Process->ThreadListHead);
92 KeInitializeSpinLock(&Process->ProcessLock);
93 DPRINT("The Process has now been initalized with the Kernel\n");
94 }
95
96 ULONG
97 STDCALL
98 KeSetProcess(PKPROCESS Process,
99 KPRIORITY Increment)
100 {
101 KIRQL OldIrql;
102 ULONG OldState;
103
104 /* Lock Dispatcher */
105 OldIrql = KeAcquireDispatcherDatabaseLock();
106
107 /* Get Old State */
108 OldState = Process->Header.SignalState;
109
110 /* Signal the Process */
111 Process->Header.SignalState = TRUE;
112 if ((OldState == 0) && IsListEmpty(&Process->Header.WaitListHead) != TRUE) {
113
114 /* Satisfy waits */
115 KiWaitTest((PVOID)Process, Increment);
116 }
117
118 /* Release Dispatcher Database */
119 KeReleaseDispatcherDatabaseLock(OldIrql);
120
121 /* Return the previous State */
122 return OldState;
123 }
124
125 /*
126 * @implemented
127 */
128 VOID
129 STDCALL
130 KeAttachProcess(PKPROCESS Process)
131 {
132 KIRQL OldIrql;
133 PKTHREAD Thread = KeGetCurrentThread();
134
135 DPRINT("KeAttachProcess: %x\n", Process);
136
137 /* Make sure that we are in the right page directory */
138 UpdatePageDirs(Thread, Process);
139
140 /* Lock Dispatcher */
141 OldIrql = KeAcquireDispatcherDatabaseLock();
142 KeAcquireSpinLockAtDpcLevel(&Thread->ApcQueueLock);
143
144 /* Crash system if DPC is being executed! */
145 if (KeIsExecutingDpc()) {
146
147 DPRINT1("Invalid attach (Thread is executing a DPC!)\n");
148 KEBUGCHECK(INVALID_PROCESS_ATTACH_ATTEMPT);
149 }
150
151 /* Check if the Target Process is already attached */
152 if (Thread->ApcState.Process == Process || Thread->ApcStateIndex != OriginalApcEnvironment) {
153
154 DPRINT("Process already Attached. Exitting\n");
155 KeReleaseSpinLockFromDpcLevel(&Thread->ApcQueueLock);
156 KeReleaseDispatcherDatabaseLock(OldIrql);
157 } else {
158
159 KiAttachProcess(Thread, Process, OldIrql, &Thread->SavedApcState);
160 }
161 }
162
163 VOID
164 STDCALL
165 KiAttachProcess(PKTHREAD Thread, PKPROCESS Process, KIRQL ApcLock, PRKAPC_STATE SavedApcState)
166 {
167
168 DPRINT("KiAttachProcess(Thread: %x, Process: %x, SavedApcState: %x\n", Thread, Process, SavedApcState);
169
170 /* Increase Stack Count */
171 Process->StackCount++;
172
173 /* Swap the APC Environment */
174 KiMoveApcState(&Thread->ApcState, SavedApcState);
175
176 /* Reinitialize Apc State */
177 InitializeListHead(&Thread->ApcState.ApcListHead[KernelMode]);
178 InitializeListHead(&Thread->ApcState.ApcListHead[UserMode]);
179 Thread->ApcState.Process = Process;
180 Thread->ApcState.KernelApcInProgress = FALSE;
181 Thread->ApcState.KernelApcPending = FALSE;
182 Thread->ApcState.UserApcPending = FALSE;
183
184 /* Update Environment Pointers if needed*/
185 if (SavedApcState == &Thread->SavedApcState) {
186
187 Thread->ApcStatePointer[OriginalApcEnvironment] = &Thread->SavedApcState;
188 Thread->ApcStatePointer[AttachedApcEnvironment] = &Thread->ApcState;
189 Thread->ApcStateIndex = AttachedApcEnvironment;
190 }
191
192 /* Swap the Processes */
193 DPRINT("Swapping\n");
194 KiSwapProcess(Process, SavedApcState->Process);
195
196 /* Return to old IRQL*/
197 KeReleaseSpinLockFromDpcLevel(&Thread->ApcQueueLock);
198 KeReleaseDispatcherDatabaseLock(ApcLock);
199
200 DPRINT("KiAttachProcess Completed Sucesfully\n");
201 }
202
203 VOID
204 STDCALL
205 KiSwapProcess(PKPROCESS NewProcess,
206 PKPROCESS OldProcess)
207 {
208 DPRINT("Switching CR3 to: %x\n", NewProcess->DirectoryTableBase.u.LowPart);
209 Ke386SetPageTableDirectory(NewProcess->DirectoryTableBase.u.LowPart);
210 }
211
212 /*
213 * @implemented
214 */
215 BOOLEAN
216 STDCALL
217 KeIsAttachedProcess(VOID)
218 {
219 /* Return the APC State */
220 return KeGetCurrentThread()->ApcStateIndex;
221 }
222
223 /*
224 * @implemented
225 */
226 VOID
227 STDCALL
228 KeStackAttachProcess(IN PKPROCESS Process,
229 OUT PRKAPC_STATE ApcState)
230 {
231 KIRQL OldIrql;
232 PKTHREAD Thread = KeGetCurrentThread();
233
234 /* Make sure that we are in the right page directory */
235 UpdatePageDirs(Thread, Process);
236
237 OldIrql = KeAcquireDispatcherDatabaseLock();
238 KeAcquireSpinLockAtDpcLevel(&Thread->ApcQueueLock);
239
240 /* Crash system if DPC is being executed! */
241 if (KeIsExecutingDpc()) {
242
243 DPRINT1("Invalid attach (Thread is executing a DPC!)\n");
244 KEBUGCHECK(INVALID_PROCESS_ATTACH_ATTEMPT);
245 }
246
247 /* Check if the Target Process is already attached */
248 if (Thread->ApcState.Process == Process) {
249
250 ApcState->Process = (PKPROCESS)1; /* Meaning already attached to the same Process */
251
252 } else {
253
254 /* Check if the Current Thread is already attached and call the Internal Function*/
255 if (Thread->ApcStateIndex != OriginalApcEnvironment) {
256
257 KiAttachProcess(Thread, Process, OldIrql, ApcState);
258 } else {
259
260 KiAttachProcess(Thread, Process, OldIrql, &Thread->SavedApcState);
261 ApcState->Process = NULL;
262 }
263 }
264 }
265
266 /*
267 * @implemented
268 */
269 VOID STDCALL
270 KeDetachProcess (VOID)
271 {
272 PKTHREAD Thread;
273 KIRQL OldIrql;
274
275 DPRINT("KeDetachProcess()\n");
276
277 /* Get Current Thread and Lock */
278 Thread = KeGetCurrentThread();
279 OldIrql = KeAcquireDispatcherDatabaseLock();
280 KeAcquireSpinLockAtDpcLevel(&Thread->ApcQueueLock);
281
282 /* Check if it's attached */
283 DPRINT("Current ApcStateIndex: %x\n", Thread->ApcStateIndex);
284
285 if (Thread->ApcStateIndex == OriginalApcEnvironment) {
286
287 DPRINT1("Invalid detach (thread was not attached)\n");
288 KEBUGCHECK(INVALID_PROCESS_DETACH_ATTEMPT);
289 }
290
291 /* Decrease Stack Count */
292 Thread->ApcState.Process->StackCount--;
293
294 /* Restore the APC State */
295 KiMoveApcState(&Thread->SavedApcState, &Thread->ApcState);
296 Thread->SavedApcState.Process = NULL;
297 Thread->ApcStatePointer[OriginalApcEnvironment] = &Thread->ApcState;
298 Thread->ApcStatePointer[AttachedApcEnvironment] = &Thread->SavedApcState;
299 Thread->ApcStateIndex = OriginalApcEnvironment;
300
301 /* Swap Processes */
302 KiSwapProcess(Thread->ApcState.Process, Thread->ApcState.Process);
303
304 /* Unlock Dispatcher */
305 KeReleaseSpinLockFromDpcLevel(&Thread->ApcQueueLock);
306 KeReleaseDispatcherDatabaseLock(OldIrql);
307 }
308
309 /*
310 * @implemented
311 */
312 VOID
313 STDCALL
314 KeUnstackDetachProcess (
315 IN PRKAPC_STATE ApcState
316 )
317 {
318 KIRQL OldIrql;
319 PKTHREAD Thread;
320
321 /*
322 * If the special "We tried to attach to the process already being
323 * attached to" flag is there, don't do anything
324 */
325 if (ApcState->Process == (PKPROCESS)1) return;
326
327 Thread = KeGetCurrentThread();
328 OldIrql = KeAcquireDispatcherDatabaseLock();
329 KeAcquireSpinLockAtDpcLevel(&Thread->ApcQueueLock);
330
331 /* Sorry Buddy, can't help you if you've got APCs or just aren't attached */
332 if ((Thread->ApcStateIndex == OriginalApcEnvironment) || (Thread->ApcState.KernelApcInProgress)) {
333
334 DPRINT1("Invalid detach (Thread not Attached, or Kernel APC in Progress!)\n");
335 KEBUGCHECK(INVALID_PROCESS_DETACH_ATTEMPT);
336 }
337
338 /* Restore the Old APC State if a Process was present */
339 if (ApcState->Process) {
340
341 KiMoveApcState(ApcState, &Thread->ApcState);
342
343 } else {
344
345 /* The ApcState parameter is useless, so use the saved data and reset it */
346 KiMoveApcState(&Thread->SavedApcState, &Thread->ApcState);
347 Thread->SavedApcState.Process = NULL;
348 Thread->ApcStateIndex = OriginalApcEnvironment;
349 Thread->ApcStatePointer[OriginalApcEnvironment] = &Thread->ApcState;
350 Thread->ApcStatePointer[AttachedApcEnvironment] = &Thread->SavedApcState;
351 }
352
353 /* Swap Processes */
354 KiSwapProcess(Thread->ApcState.Process, Thread->ApcState.Process);
355
356 /* Return to old IRQL*/
357 KeReleaseSpinLockFromDpcLevel(&Thread->ApcQueueLock);
358 KeReleaseDispatcherDatabaseLock(OldIrql);
359 }
360
361 /*
362 * @implemented
363 */
364 BOOLEAN
365 STDCALL
366 KeAddSystemServiceTable(PULONG_PTR Base,
367 PULONG Count OPTIONAL,
368 ULONG Limit,
369 PUCHAR Number,
370 ULONG Index)
371 {
372 /* Check if descriptor table entry is free */
373 if ((Index > SSDT_MAX_ENTRIES - 1) ||
374 (KeServiceDescriptorTable[Index].Base) ||
375 (KeServiceDescriptorTableShadow[Index].Base))
376 {
377 return FALSE;
378 }
379
380 /* Initialize the shadow service descriptor table */
381 KeServiceDescriptorTableShadow[Index].Base = Base;
382 KeServiceDescriptorTableShadow[Index].Limit = Limit;
383 KeServiceDescriptorTableShadow[Index].Number = Number;
384 KeServiceDescriptorTableShadow[Index].Count = Count;
385
386 return TRUE;
387 }
388
389 /*
390 * @implemented
391 */
392 BOOLEAN
393 STDCALL
394 KeRemoveSystemServiceTable(IN ULONG Index)
395 {
396 /* Make sure the Index is valid */
397 if (Index > SSDT_MAX_ENTRIES - 1) return FALSE;
398
399 /* Is there a Normal Descriptor Table? */
400 if (!KeServiceDescriptorTable[Index].Base)
401 {
402 /* Not with the index, is there a shadow at least? */
403 if (!KeServiceDescriptorTableShadow[Index].Base) return FALSE;
404 }
405
406 /* Now clear from the Shadow Table. */
407 KeServiceDescriptorTableShadow[Index].Base = NULL;
408 KeServiceDescriptorTableShadow[Index].Number = NULL;
409 KeServiceDescriptorTableShadow[Index].Limit = 0;
410 KeServiceDescriptorTableShadow[Index].Count = NULL;
411
412 /* Check if we should clean from the Master one too */
413 if (Index == 1)
414 {
415 KeServiceDescriptorTable[Index].Base = NULL;
416 KeServiceDescriptorTable[Index].Number = NULL;
417 KeServiceDescriptorTable[Index].Limit = 0;
418 KeServiceDescriptorTable[Index].Count = NULL;
419 }
420
421 return TRUE;
422 }
423 /* EOF */