3 * Copyright (C) 1998, 1999, 2000, 2001 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.
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/ke/process.c
23 * PURPOSE: Microkernel process management
24 * PROGRAMMER: David Welch (welch@cwcom.net)
30 /* INCLUDES *****************************************************************/
32 #include <ddk/ntddk.h>
33 #include <internal/ke.h>
34 #include <internal/mm.h>
35 #include <internal/ps.h>
38 #include <internal/debug.h>
40 /* FUNCTIONS *****************************************************************/
46 KeAttachProcess (PEPROCESS Process)
49 PETHREAD CurrentThread;
50 PULONG AttachedProcessPageDir;
53 DPRINT("KeAttachProcess(Process %x)\n",Process);
55 CurrentThread = PsGetCurrentThread();
57 if (CurrentThread->OldProcess != NULL)
59 DbgPrint("Invalid attach (thread is already attached)\n");
63 KeRaiseIrql(DISPATCH_LEVEL, &oldlvl);
65 /* The stack of the current process may be located in a page which is
66 not present in the page directory of the process we're attaching to.
67 That would lead to a page fault when this function returns. However,
68 since the processor can't call the page fault handler 'cause it can't
69 push EIP on the stack, this will show up as a stack fault which will
70 crash the entire system.
71 To prevent this, make sure the page directory of the process we're
72 attaching to is up-to-date. */
74 AttachedProcessPageDir = ExAllocatePageWithPhysPage(Process->Pcb.DirectoryTableBase);
75 MmUpdateStackPageDir(AttachedProcessPageDir, &CurrentThread->Tcb);
76 ExUnmapPage(AttachedProcessPageDir);
78 CurrentThread->OldProcess = PsGetCurrentProcess();
79 CurrentThread->ThreadsProcess = Process;
80 PageDir = Process->Pcb.DirectoryTableBase.u.LowPart;
81 DPRINT("Switching process context to %x\n",PageDir)
82 __asm__("movl %0,%%cr3\n\t"
94 KeDetachProcess (VOID)
97 PETHREAD CurrentThread;
100 DPRINT("KeDetachProcess()\n");
102 CurrentThread = PsGetCurrentThread();
104 if (CurrentThread->OldProcess == NULL)
106 DbgPrint("Invalid detach (thread was not attached)\n");
110 KeRaiseIrql(DISPATCH_LEVEL, &oldlvl);
112 CurrentThread->ThreadsProcess = CurrentThread->OldProcess;
113 CurrentThread->OldProcess = NULL;
114 PageDir = CurrentThread->ThreadsProcess->Pcb.DirectoryTableBase.u.LowPart;
115 __asm__("movl %0,%%cr3\n\t"