3 * Copyright (C) 2000, 1999, 1998 David Welch <welch@cwcom.net>,
4 * Philip Susi <phreak@iag.net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * PROJECT: ReactOS kernel
23 * FILE: ntoskrnl/ps/debug.c
24 * PURPOSE: Thread managment
25 * PROGRAMMER: David Welch (welch@mcmail.com)
28 * 12/10/99: Phillip Susi: Thread priorities, and APC work
34 * All of the routines that manipulate the thread queue synchronize on
39 /* INCLUDES ****************************************************************/
41 #include <ddk/ntddk.h>
42 #include <internal/ke.h>
43 #include <internal/ob.h>
45 #include <internal/ps.h>
46 #include <internal/ob.h>
49 #include <internal/debug.h>
51 /* FUNCTIONS ***************************************************************/
54 KeContextToTrapFrame(PCONTEXT Context,
55 PKTRAP_FRAME TrapFrame)
57 if (Context->ContextFlags & CONTEXT_CONTROL)
59 TrapFrame->Esp = Context->Esp;
60 TrapFrame->Ss = Context->SegSs;
61 TrapFrame->Cs = Context->SegCs;
62 TrapFrame->Eip = Context->Eip;
63 TrapFrame->Eflags = Context->EFlags;
64 TrapFrame->Ebp = Context->Ebp;
66 if (Context->ContextFlags & CONTEXT_INTEGER)
68 TrapFrame->Eax = Context->Eax;
69 TrapFrame->Ebx = Context->Ebx;
70 TrapFrame->Ecx = Context->Ecx;
72 * Edx is used in the TrapFrame to hold the old trap frame pointer
73 * so we don't want to overwrite it here
75 /* TrapFrame->Edx = Context->Edx; */
76 TrapFrame->Esi = Context->Esi;
77 TrapFrame->Edi = Context->Edi;
79 if (Context->ContextFlags & CONTEXT_SEGMENTS)
81 TrapFrame->Ds = Context->SegDs;
82 TrapFrame->Es = Context->SegEs;
83 TrapFrame->Fs = Context->SegFs;
84 TrapFrame->Gs = Context->SegGs;
86 if (Context->ContextFlags & CONTEXT_FLOATING_POINT)
92 if (Context->ContextFlags & CONTEXT_DEBUG_REGISTERS)
101 KeTrapFrameToContext(PKTRAP_FRAME TrapFrame,
104 if (Context->ContextFlags & CONTEXT_CONTROL)
106 Context->SegSs = TrapFrame->Ss;
107 Context->Esp = TrapFrame->Esp;
108 Context->SegCs = TrapFrame->Cs;
109 Context->Eip = TrapFrame->Eip;
110 Context->EFlags = TrapFrame->Eflags;
111 Context->Ebp = TrapFrame->Ebp;
113 if (Context->ContextFlags & CONTEXT_INTEGER)
115 Context->Eax = TrapFrame->Eax;
116 Context->Ebx = TrapFrame->Ebx;
117 Context->Ecx = TrapFrame->Ecx;
119 * NOTE: In the trap frame which is built on entry to a system
120 * call TrapFrame->Edx will actually hold the address of the
121 * previous TrapFrame. I don't believe leaking this information
122 * has security implications. Also EDX holds the address of the
123 * arguments to the system call in progress so it isn't of much
124 * interest to the debugger.
126 Context->Edx = TrapFrame->Edx;
127 Context->Esi = TrapFrame->Esi;
128 Context->Edi = TrapFrame->Edi;
130 if (Context->ContextFlags & CONTEXT_SEGMENTS)
132 Context->SegDs = TrapFrame->Ds;
133 Context->SegEs = TrapFrame->Es;
134 Context->SegFs = TrapFrame->Fs;
135 Context->SegGs = TrapFrame->Gs;
137 if (Context->ContextFlags & CONTEXT_DEBUG_REGISTERS)
140 * FIXME: Implement this case
143 if (Context->ContextFlags & CONTEXT_FLOATING_POINT)
146 * FIXME: Implement this case
150 if (Context->ContextFlags & CONTEXT_EXTENDED_REGISTERS)
153 * FIXME: Investigate this
160 KeGetContextRundownRoutine(PKAPC Apc)
165 Event = (PKEVENT)Apc->SystemArgument1;
166 Status = (PNTSTATUS)Apc->SystemArgument2;
167 (*Status) = STATUS_THREAD_IS_TERMINATING;
168 KeSetEvent(Event, IO_NO_INCREMENT, FALSE);
172 KeGetContextKernelRoutine(PKAPC Apc,
173 PKNORMAL_ROUTINE* NormalRoutine,
174 PVOID* NormalContext,
175 PVOID* SystemArgument1,
176 PVOID* SystemArgument2)
178 * FUNCTION: This routine is called by an APC sent by NtGetContextThread to
179 * copy the context of a thread into a buffer.
186 Context = (PCONTEXT)(*NormalContext);
187 Event = (PKEVENT)(*SystemArgument1);
188 Status = (PNTSTATUS)(*SystemArgument2);
190 KeTrapFrameToContext(KeGetCurrentThread()->TrapFrame, Context);
192 *Status = STATUS_SUCCESS;
193 KeSetEvent(Event, IO_NO_INCREMENT, FALSE);
197 NtGetContextThread(IN HANDLE ThreadHandle,
198 OUT PCONTEXT Context)
203 Status = ObReferenceObjectByHandle(ThreadHandle,
209 if (!NT_SUCCESS(Status))
213 if (Thread == PsGetCurrentThread())
216 * I don't know if trying to get your own context makes much
217 * sense but we can handle it more efficently.
220 KeTrapFrameToContext(Thread->Tcb.TrapFrame, Context);
221 ObDereferenceObject(Thread);
222 return(STATUS_SUCCESS);
231 KContext.ContextFlags = Context->ContextFlags;
232 KeInitializeEvent(&Event,
235 AStatus = STATUS_SUCCESS;
237 KeInitializeApc(&Apc,
240 KeGetContextKernelRoutine,
241 KeGetContextRundownRoutine,
245 KeInsertQueueApc(&Apc,
249 Status = KeWaitForSingleObject(&Event,
254 if (!NT_SUCCESS(Status))
258 if (!NT_SUCCESS(AStatus))
262 memcpy(Context, &KContext, sizeof(CONTEXT));
263 ObDereferenceObject(Thread);
264 return(STATUS_SUCCESS);
269 NtSetContextThread(IN HANDLE ThreadHandle,