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.
19 /* $Id: process.c,v 1.18 2003/12/30 18:52:04 fireball Exp $
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 KiSwapApcEnvironment(&CurrentThread
->Tcb
, &Process
->Pcb
);
67 /* The stack of the current process may be located in a page which is
68 not present in the page directory of the process we're attaching to.
69 That would lead to a page fault when this function returns. However,
70 since the processor can't call the page fault handler 'cause it can't
71 push EIP on the stack, this will show up as a stack fault which will
72 crash the entire system.
73 To prevent this, make sure the page directory of the process we're
74 attaching to is up-to-date. */
76 AttachedProcessPageDir
= ExAllocatePageWithPhysPage(Process
->Pcb
.DirectoryTableBase
);
77 MmUpdateStackPageDir(AttachedProcessPageDir
, &CurrentThread
->Tcb
);
78 ExUnmapPage(AttachedProcessPageDir
);
80 CurrentThread
->OldProcess
= PsGetCurrentProcess();
81 CurrentThread
->ThreadsProcess
= Process
;
82 PageDir
= Process
->Pcb
.DirectoryTableBase
.u
.LowPart
;
83 DPRINT("Switching process context to %x\n",PageDir
);
85 __asm__("movl %0,%%cr3\n\t"
88 #elif defined(_MSC_VER)
89 __asm mov eax
, PageDir
;
92 #error Unknown compiler for inline assembler
101 KeDetachProcess (VOID
)
104 PETHREAD CurrentThread
;
107 DPRINT("KeDetachProcess()\n");
109 CurrentThread
= PsGetCurrentThread();
111 if (CurrentThread
->OldProcess
== NULL
)
113 DbgPrint("Invalid detach (thread was not attached)\n");
117 KeRaiseIrql(DISPATCH_LEVEL
, &oldlvl
);
119 KiSwapApcEnvironment(&CurrentThread
->Tcb
, &CurrentThread
->OldProcess
->Pcb
);
121 CurrentThread
->ThreadsProcess
= CurrentThread
->OldProcess
;
122 CurrentThread
->OldProcess
= NULL
;
123 PageDir
= CurrentThread
->ThreadsProcess
->Pcb
.DirectoryTableBase
.u
.LowPart
;
124 #if defined(__GNUC__)
125 __asm__("movl %0,%%cr3\n\t"
128 #elif defined(_MSC_VER)
129 __asm mov eax
, PageDir
;
132 #error Unknown compiler for inline assembler