Fix NtGetContextThread() and implement NtSetContextThread()

svn path=/trunk/; revision=4872
This commit is contained in:
Gé van Geldorp 2003-06-07 19:13:43 +00:00
parent 32f2d198df
commit 472ebde7ec
2 changed files with 218 additions and 91 deletions

View file

@ -16,7 +16,7 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
/* $Id: irq.c,v 1.30 2003/06/07 10:14:40 chorns Exp $ /* $Id: irq.c,v 1.31 2003/06/07 19:13:43 gvg Exp $
* *
* PROJECT: ReactOS kernel * PROJECT: ReactOS kernel
* FILE: ntoskrnl/ke/i386/irq.c * FILE: ntoskrnl/ke/i386/irq.c
@ -270,14 +270,12 @@ typedef struct _KIRQ_TRAPFRAME
ULONG Eflags; ULONG Eflags;
} KIRQ_TRAPFRAME, *PKIRQ_TRAPFRAME; } KIRQ_TRAPFRAME, *PKIRQ_TRAPFRAME;
#ifdef DBG
VOID VOID
KeIRQTrapFrameToTrapFrame(PKIRQ_TRAPFRAME IrqTrapFrame, KeIRQTrapFrameToTrapFrame(PKIRQ_TRAPFRAME IrqTrapFrame,
PKTRAP_FRAME TrapFrame) PKTRAP_FRAME TrapFrame)
{ {
TrapFrame->Fs = IrqTrapFrame->Fs; TrapFrame->Fs = IrqTrapFrame->Fs;
TrapFrame->Fs = IrqTrapFrame->Es; TrapFrame->Es = IrqTrapFrame->Es;
TrapFrame->Ds = IrqTrapFrame->Ds; TrapFrame->Ds = IrqTrapFrame->Ds;
TrapFrame->Eax = IrqTrapFrame->Eax; TrapFrame->Eax = IrqTrapFrame->Eax;
TrapFrame->Ecx = IrqTrapFrame->Ecx; TrapFrame->Ecx = IrqTrapFrame->Ecx;
@ -292,7 +290,25 @@ KeIRQTrapFrameToTrapFrame(PKIRQ_TRAPFRAME IrqTrapFrame,
TrapFrame->Eflags = IrqTrapFrame->Eflags; TrapFrame->Eflags = IrqTrapFrame->Eflags;
} }
#endif VOID
KeTrapFrameToIRQTrapFrame(PKTRAP_FRAME TrapFrame,
PKIRQ_TRAPFRAME IrqTrapFrame)
{
IrqTrapFrame->Fs = TrapFrame->Fs;
IrqTrapFrame->Es = TrapFrame->Es;
IrqTrapFrame->Ds = TrapFrame->Ds;
IrqTrapFrame->Eax = TrapFrame->Eax;
IrqTrapFrame->Ecx = TrapFrame->Ecx;
IrqTrapFrame->Edx = TrapFrame->Edx;
IrqTrapFrame->Ebx = TrapFrame->Ebx;
IrqTrapFrame->Esp = TrapFrame->Esp;
IrqTrapFrame->Ebp = TrapFrame->Ebp;
IrqTrapFrame->Esi = TrapFrame->Esi;
IrqTrapFrame->Edi = TrapFrame->Edi;
IrqTrapFrame->Eip = TrapFrame->Eip;
IrqTrapFrame->Cs = TrapFrame->Cs;
IrqTrapFrame->Eflags = TrapFrame->Eflags;
}
#ifdef MP #ifdef MP
@ -449,6 +465,17 @@ KiInterruptDispatch (ULONG irq, PKIRQ_TRAPFRAME Trapframe)
*/ */
{ {
KIRQL old_level; KIRQL old_level;
KTRAP_FRAME KernelTrapFrame;
PKTHREAD CurrentThread;
PKTRAP_FRAME OldTrapFrame;
CurrentThread = KeGetCurrentThread();
if (NULL != CurrentThread && NULL == CurrentThread->TrapFrame)
{
OldTrapFrame = CurrentThread->TrapFrame;
KeIRQTrapFrameToTrapFrame(Trapframe, &KernelTrapFrame);
CurrentThread->TrapFrame = &KernelTrapFrame;
}
/* /*
* At this point we have interrupts disabled, nothing has been done to * At this point we have interrupts disabled, nothing has been done to
@ -498,6 +525,13 @@ KiInterruptDispatch (ULONG irq, PKIRQ_TRAPFRAME Trapframe)
*/ */
__asm__("cli\n\t"); __asm__("cli\n\t");
HalEndSystemInterrupt (old_level, 0); HalEndSystemInterrupt (old_level, 0);
assert(KeGetCurrentThread() == CurrentThread);
if (NULL != CurrentThread && CurrentThread->TrapFrame == &KernelTrapFrame)
{
KeTrapFrameToIRQTrapFrame(&KernelTrapFrame, Trapframe);
CurrentThread->TrapFrame = OldTrapFrame;
}
} }
#endif /* MP */ #endif /* MP */

View file

@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ */
/* $Id: debug.c,v 1.8 2003/06/05 23:36:35 gdalsnes Exp $ /* $Id: debug.c,v 1.9 2003/06/07 19:13:43 gvg Exp $
* *
* PROJECT: ReactOS kernel * PROJECT: ReactOS kernel
* FILE: ntoskrnl/ps/debug.c * FILE: ntoskrnl/ps/debug.c
@ -44,6 +44,7 @@
#include <string.h> #include <string.h>
#include <internal/ps.h> #include <internal/ps.h>
#include <internal/ob.h> #include <internal/ob.h>
#include <internal/safe.h>
#define NDEBUG #define NDEBUG
#include <internal/debug.h> #include <internal/debug.h>
@ -157,7 +158,7 @@ KeTrapFrameToContext(PKTRAP_FRAME TrapFrame,
} }
VOID STDCALL VOID STDCALL
KeGetContextRundownRoutine(PKAPC Apc) KeGetSetContextRundownRoutine(PKAPC Apc)
{ {
PKEVENT Event; PKEVENT Event;
PNTSTATUS Status; PNTSTATUS Status;
@ -195,20 +196,29 @@ KeGetContextKernelRoutine(PKAPC Apc,
NTSTATUS STDCALL NTSTATUS STDCALL
NtGetContextThread(IN HANDLE ThreadHandle, NtGetContextThread(IN HANDLE ThreadHandle,
OUT PCONTEXT Context) OUT PCONTEXT UnsafeContext)
{ {
PETHREAD Thread; PETHREAD Thread;
NTSTATUS Status; NTSTATUS Status;
CONTEXT Context;
KAPC Apc;
KEVENT Event;
NTSTATUS AStatus;
Status = MmCopyFromCaller(&Context, UnsafeContext, sizeof(CONTEXT));
if (! NT_SUCCESS(Status))
{
return Status;
}
Status = ObReferenceObjectByHandle(ThreadHandle, Status = ObReferenceObjectByHandle(ThreadHandle,
THREAD_GET_CONTEXT, THREAD_GET_CONTEXT,
PsThreadType, PsThreadType,
UserMode, UserMode,
(PVOID*)&Thread, (PVOID*)&Thread,
NULL); NULL);
if (!NT_SUCCESS(Status)) if (! NT_SUCCESS(Status))
{ {
return(Status); return Status;
} }
if (Thread == PsGetCurrentThread()) if (Thread == PsGetCurrentThread())
{ {
@ -217,18 +227,11 @@ NtGetContextThread(IN HANDLE ThreadHandle,
* sense but we can handle it more efficently. * sense but we can handle it more efficently.
*/ */
KeTrapFrameToContext(Thread->Tcb.TrapFrame, Context); KeTrapFrameToContext(Thread->Tcb.TrapFrame, &Context);
ObDereferenceObject(Thread); Status = STATUS_SUCCESS;
return(STATUS_SUCCESS);
} }
else else
{ {
KAPC Apc;
KEVENT Event;
NTSTATUS AStatus;
CONTEXT KContext;
KContext.ContextFlags = Context->ContextFlags;
KeInitializeEvent(&Event, KeInitializeEvent(&Event,
NotificationEvent, NotificationEvent,
FALSE); FALSE);
@ -238,10 +241,10 @@ NtGetContextThread(IN HANDLE ThreadHandle,
&Thread->Tcb, &Thread->Tcb,
OriginalApcEnvironment, OriginalApcEnvironment,
KeGetContextKernelRoutine, KeGetContextKernelRoutine,
KeGetContextRundownRoutine, KeGetSetContextRundownRoutine,
NULL, NULL,
KernelMode, KernelMode,
(PVOID)&KContext); (PVOID)&Context);
KeInsertQueueApc(&Apc, KeInsertQueueApc(&Apc,
(PVOID)&Event, (PVOID)&Event,
(PVOID)&AStatus, (PVOID)&AStatus,
@ -251,25 +254,115 @@ NtGetContextThread(IN HANDLE ThreadHandle,
UserMode, UserMode,
FALSE, FALSE,
NULL); NULL);
if (!NT_SUCCESS(Status)) if (NT_SUCCESS(Status) && ! NT_SUCCESS(AStatus))
{ {
return(Status); Status = AStatus;
} }
if (!NT_SUCCESS(AStatus)) }
if (NT_SUCCESS(Status))
{ {
return(AStatus); Status = MmCopyToCaller(UnsafeContext, &Context, sizeof(Context));
} }
memcpy(Context, &KContext, sizeof(CONTEXT));
ObDereferenceObject(Thread); ObDereferenceObject(Thread);
return(STATUS_SUCCESS); return Status;
} }
VOID STDCALL
KeSetContextKernelRoutine(PKAPC Apc,
PKNORMAL_ROUTINE* NormalRoutine,
PVOID* NormalContext,
PVOID* SystemArgument1,
PVOID* SystemArgument2)
/*
* FUNCTION: This routine is called by an APC sent by NtSetContextThread to
* set the context of a thread from a buffer.
*/
{
PKEVENT Event;
PCONTEXT Context;
PNTSTATUS Status;
Context = (PCONTEXT)(*NormalContext);
Event = (PKEVENT)(*SystemArgument1);
Status = (PNTSTATUS)(*SystemArgument2);
KeContextToTrapFrame(Context, KeGetCurrentThread()->TrapFrame);
*Status = STATUS_SUCCESS;
KeSetEvent(Event, IO_NO_INCREMENT, FALSE);
} }
NTSTATUS STDCALL NTSTATUS STDCALL
NtSetContextThread(IN HANDLE ThreadHandle, NtSetContextThread(IN HANDLE ThreadHandle,
IN PCONTEXT Context) IN PCONTEXT UnsafeContext)
{ {
UNIMPLEMENTED; PETHREAD Thread;
NTSTATUS Status;
KAPC Apc;
KEVENT Event;
NTSTATUS AStatus;
CONTEXT Context;
Status = MmCopyFromCaller(&Context, UnsafeContext, sizeof(CONTEXT));
if (! NT_SUCCESS(Status))
{
return Status;
}
Status = ObReferenceObjectByHandle(ThreadHandle,
THREAD_SET_CONTEXT,
PsThreadType,
UserMode,
(PVOID*)&Thread,
NULL);
if (!NT_SUCCESS(Status))
{
return Status;
}
if (Thread == PsGetCurrentThread())
{
/*
* I don't know if trying to set your own context makes much
* sense but we can handle it more efficently.
*/
KeContextToTrapFrame(&Context, Thread->Tcb.TrapFrame);
Status = STATUS_SUCCESS;
}
else
{
KeInitializeEvent(&Event,
NotificationEvent,
FALSE);
AStatus = STATUS_SUCCESS;
KeInitializeApc(&Apc,
&Thread->Tcb,
OriginalApcEnvironment,
KeSetContextKernelRoutine,
KeGetSetContextRundownRoutine,
NULL,
KernelMode,
(PVOID)&Context);
KeInsertQueueApc(&Apc,
(PVOID)&Event,
(PVOID)&AStatus,
IO_NO_INCREMENT);
Status = KeWaitForSingleObject(&Event,
0,
UserMode,
FALSE,
NULL);
if (NT_SUCCESS(Status) && ! NT_SUCCESS(AStatus))
{
Status = AStatus;
}
}
ObDereferenceObject(Thread);
return Status;
} }
/* EOF */ /* EOF */