2005-04-22 12:52:25 +00:00
|
|
|
/*
|
2005-01-26 13:58:37 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS kernel
|
2015-10-04 11:54:25 +00:00
|
|
|
* FILE: ntoskrnl/ke/i386/thrdini.c
|
2005-04-22 12:52:25 +00:00
|
|
|
* PURPOSE: i386 Thread Context Creation
|
2006-01-15 09:23:55 +00:00
|
|
|
* PROGRAMMER: Alex Ionescu (alex@relsoft.net)
|
1999-11-12 12:01:17 +00:00
|
|
|
*/
|
|
|
|
|
2007-01-17 20:44:37 +00:00
|
|
|
/* INCLUDES ******************************************************************/
|
1999-11-12 12:01:17 +00:00
|
|
|
|
2004-08-15 16:39:12 +00:00
|
|
|
#include <ntoskrnl.h>
|
1999-11-12 12:01:17 +00:00
|
|
|
#define NDEBUG
|
2007-01-17 20:44:37 +00:00
|
|
|
#include <debug.h>
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2007-01-17 20:44:37 +00:00
|
|
|
typedef struct _KSWITCHFRAME
|
2006-01-15 09:23:55 +00:00
|
|
|
{
|
2005-04-22 12:52:25 +00:00
|
|
|
PVOID ExceptionList;
|
2007-01-17 20:44:37 +00:00
|
|
|
BOOLEAN ApcBypassDisable;
|
|
|
|
PVOID RetAddr;
|
|
|
|
} KSWITCHFRAME, *PKSWITCHFRAME;
|
2005-04-22 12:52:25 +00:00
|
|
|
|
2006-01-15 09:23:55 +00:00
|
|
|
typedef struct _KSTART_FRAME
|
|
|
|
{
|
2005-04-22 12:52:25 +00:00
|
|
|
PKSYSTEM_ROUTINE SystemRoutine;
|
|
|
|
PKSTART_ROUTINE StartRoutine;
|
|
|
|
PVOID StartContext;
|
2005-05-09 01:38:29 +00:00
|
|
|
BOOLEAN UserThread;
|
2005-04-22 12:52:25 +00:00
|
|
|
} KSTART_FRAME, *PKSTART_FRAME;
|
1999-11-12 12:01:17 +00:00
|
|
|
|
2007-01-17 20:44:37 +00:00
|
|
|
typedef struct _KUINIT_FRAME
|
|
|
|
{
|
|
|
|
KSWITCHFRAME CtxSwitchFrame;
|
|
|
|
KSTART_FRAME StartFrame;
|
|
|
|
KTRAP_FRAME TrapFrame;
|
|
|
|
FX_SAVE_AREA FxSaveArea;
|
2005-04-22 12:52:25 +00:00
|
|
|
} KUINIT_FRAME, *PKUINIT_FRAME;
|
|
|
|
|
2007-01-17 20:44:37 +00:00
|
|
|
typedef struct _KKINIT_FRAME
|
|
|
|
{
|
|
|
|
KSWITCHFRAME CtxSwitchFrame;
|
|
|
|
KSTART_FRAME StartFrame;
|
|
|
|
FX_SAVE_AREA FxSaveArea;
|
2005-04-22 12:52:25 +00:00
|
|
|
} KKINIT_FRAME, *PKKINIT_FRAME;
|
2002-08-08 17:54:16 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
KiSwitchThreads(
|
|
|
|
IN PKTHREAD OldThread,
|
|
|
|
IN PKTHREAD NewThread
|
|
|
|
);
|
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
KiRetireDpcListInDpcStack(
|
|
|
|
IN PKPRCB Prcb,
|
|
|
|
IN PVOID DpcStack
|
|
|
|
);
|
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
2000-05-13 13:51:08 +00:00
|
|
|
|
2010-01-19 06:34:15 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KiThreadStartup(VOID)
|
|
|
|
{
|
|
|
|
PKTRAP_FRAME TrapFrame;
|
|
|
|
PKSTART_FRAME StartFrame;
|
|
|
|
PKUINIT_FRAME InitFrame;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-01-19 06:34:15 +00:00
|
|
|
/* Get the start and trap frames */
|
|
|
|
InitFrame = KeGetCurrentThread()->KernelStack;
|
|
|
|
StartFrame = &InitFrame->StartFrame;
|
|
|
|
TrapFrame = &InitFrame->TrapFrame;
|
|
|
|
|
|
|
|
/* Lower to APC level */
|
|
|
|
KfLowerIrql(APC_LEVEL);
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-01-19 06:34:15 +00:00
|
|
|
/* Call the system routine */
|
|
|
|
StartFrame->SystemRoutine(StartFrame->StartRoutine, StartFrame->StartContext);
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-01-19 06:34:15 +00:00
|
|
|
/* If we returned, we better be a user thread */
|
2018-04-24 22:07:02 +00:00
|
|
|
if (!StartFrame->UserThread)
|
|
|
|
{
|
|
|
|
KeBugCheck(NO_USER_MODE_CONTEXT);
|
|
|
|
}
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-01-19 06:34:15 +00:00
|
|
|
/* Exit to user-mode */
|
|
|
|
KiServiceExit2(TrapFrame);
|
|
|
|
}
|
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
VOID
|
2007-01-17 20:44:37 +00:00
|
|
|
NTAPI
|
2009-09-27 10:09:38 +00:00
|
|
|
KiInitializeContextThread(IN PKTHREAD Thread,
|
|
|
|
IN PKSYSTEM_ROUTINE SystemRoutine,
|
|
|
|
IN PKSTART_ROUTINE StartRoutine,
|
|
|
|
IN PVOID StartContext,
|
|
|
|
IN PCONTEXT ContextPointer)
|
1999-11-12 12:01:17 +00:00
|
|
|
{
|
2005-04-22 12:52:25 +00:00
|
|
|
PFX_SAVE_AREA FxSaveArea;
|
2006-01-15 09:23:55 +00:00
|
|
|
PFXSAVE_FORMAT FxSaveFormat;
|
2005-04-22 12:52:25 +00:00
|
|
|
PKSTART_FRAME StartFrame;
|
2007-01-17 20:44:37 +00:00
|
|
|
PKSWITCHFRAME CtxSwitchFrame;
|
2006-01-15 09:23:55 +00:00
|
|
|
PKTRAP_FRAME TrapFrame;
|
|
|
|
CONTEXT LocalContext;
|
|
|
|
PCONTEXT Context = NULL;
|
|
|
|
ULONG ContextFlags;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Check if this is a With-Context Thread */
|
2006-01-15 09:23:55 +00:00
|
|
|
if (ContextPointer)
|
2005-04-23 04:12:26 +00:00
|
|
|
{
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Set up the Initial Frame */
|
|
|
|
PKUINIT_FRAME InitFrame;
|
2006-01-15 09:23:55 +00:00
|
|
|
InitFrame = (PKUINIT_FRAME)((ULONG_PTR)Thread->InitialStack -
|
|
|
|
sizeof(KUINIT_FRAME));
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2006-01-15 09:23:55 +00:00
|
|
|
/* Copy over the context we got */
|
2006-10-22 09:06:58 +00:00
|
|
|
RtlCopyMemory(&LocalContext, ContextPointer, sizeof(CONTEXT));
|
2006-01-15 09:23:55 +00:00
|
|
|
Context = &LocalContext;
|
|
|
|
ContextFlags = CONTEXT_CONTROL;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2007-03-19 19:43:50 +00:00
|
|
|
/* Zero out the trap frame and save area */
|
|
|
|
RtlZeroMemory(&InitFrame->TrapFrame,
|
|
|
|
KTRAP_FRAME_LENGTH + sizeof(FX_SAVE_AREA));
|
|
|
|
|
2006-01-15 09:23:55 +00:00
|
|
|
/* Setup the Fx Area */
|
|
|
|
FxSaveArea = &InitFrame->FxSaveArea;
|
|
|
|
|
|
|
|
/* Check if we support FXsr */
|
|
|
|
if (KeI386FxsrPresent)
|
|
|
|
{
|
|
|
|
/* Get the FX Save Format Area */
|
|
|
|
FxSaveFormat = (PFXSAVE_FORMAT)Context->ExtendedRegisters;
|
|
|
|
|
|
|
|
/* Set an initial state */
|
|
|
|
FxSaveFormat->ControlWord = 0x27F;
|
|
|
|
FxSaveFormat->StatusWord = 0;
|
|
|
|
FxSaveFormat->TagWord = 0;
|
|
|
|
FxSaveFormat->ErrorOffset = 0;
|
|
|
|
FxSaveFormat->ErrorSelector = 0;
|
2006-08-20 19:08:57 +00:00
|
|
|
FxSaveFormat->DataOffset = 0;
|
2006-01-15 09:23:55 +00:00
|
|
|
FxSaveFormat->DataSelector = 0;
|
|
|
|
FxSaveFormat->MXCsr = 0x1F80;
|
|
|
|
}
|
|
|
|
else
|
2005-04-23 04:12:26 +00:00
|
|
|
{
|
2006-01-15 09:23:55 +00:00
|
|
|
/* Setup the regular save area */
|
|
|
|
Context->FloatSave.ControlWord = 0x27F;
|
|
|
|
Context->FloatSave.StatusWord = 0;
|
|
|
|
Context->FloatSave.TagWord = -1;
|
|
|
|
Context->FloatSave.ErrorOffset = 0;
|
|
|
|
Context->FloatSave.ErrorSelector = 0;
|
|
|
|
Context->FloatSave.DataOffset =0;
|
|
|
|
Context->FloatSave.DataSelector = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the CPU has NPX */
|
|
|
|
if (KeI386NpxPresent)
|
|
|
|
{
|
|
|
|
/* Set an intial NPX State */
|
|
|
|
Context->FloatSave.Cr0NpxState = 0;
|
|
|
|
FxSaveArea->Cr0NpxState = 0;
|
|
|
|
FxSaveArea->NpxSavedCpu = 0;
|
|
|
|
|
|
|
|
/* Now set the context flags depending on XMM support */
|
2007-03-19 19:43:50 +00:00
|
|
|
ContextFlags |= (KeI386FxsrPresent) ? CONTEXT_EXTENDED_REGISTERS :
|
2006-01-15 09:23:55 +00:00
|
|
|
CONTEXT_FLOATING_POINT;
|
|
|
|
|
|
|
|
/* Set the Thread's NPX State */
|
2006-08-20 19:08:57 +00:00
|
|
|
Thread->NpxState = NPX_STATE_NOT_LOADED;
|
2011-10-07 16:18:52 +00:00
|
|
|
Thread->Header.NpxIrql = PASSIVE_LEVEL;
|
2005-05-09 01:38:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-01-15 09:23:55 +00:00
|
|
|
/* We'll use emulation */
|
|
|
|
FxSaveArea->Cr0NpxState = CR0_EM;
|
|
|
|
Thread->NpxState = NPX_STATE_NOT_LOADED &~ CR0_MP;
|
2005-04-23 04:12:26 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2008-08-22 14:29:01 +00:00
|
|
|
/* Disable any debug registers */
|
2006-01-15 20:13:08 +00:00
|
|
|
Context->ContextFlags &= ~CONTEXT_DEBUG_REGISTERS;
|
2006-01-15 09:23:55 +00:00
|
|
|
|
|
|
|
/* Setup the Trap Frame */
|
|
|
|
TrapFrame = &InitFrame->TrapFrame;
|
|
|
|
|
|
|
|
/* Set up a trap frame from the context. */
|
|
|
|
KeContextToTrapFrame(Context,
|
|
|
|
NULL,
|
|
|
|
TrapFrame,
|
|
|
|
Context->ContextFlags | ContextFlags,
|
|
|
|
UserMode);
|
|
|
|
|
|
|
|
/* Set SS, DS, ES's RPL Mask properly */
|
|
|
|
TrapFrame->HardwareSegSs |= RPL_MASK;
|
|
|
|
TrapFrame->SegDs |= RPL_MASK;
|
|
|
|
TrapFrame->SegEs |= RPL_MASK;
|
2006-01-15 20:26:53 +00:00
|
|
|
TrapFrame->Dr7 = 0;
|
2006-01-15 09:23:55 +00:00
|
|
|
|
|
|
|
/* Set the debug mark */
|
|
|
|
TrapFrame->DbgArgMark = 0xBADB0D00;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-23 04:12:26 +00:00
|
|
|
/* Set the previous mode as user */
|
2005-11-27 19:21:19 +00:00
|
|
|
TrapFrame->PreviousPreviousMode = UserMode;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-23 04:12:26 +00:00
|
|
|
/* Terminate the Exception Handler List */
|
2007-03-19 19:43:50 +00:00
|
|
|
TrapFrame->ExceptionList = EXCEPTION_CHAIN_END;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Setup the Stack for KiThreadStartup and Context Switching */
|
|
|
|
StartFrame = &InitFrame->StartFrame;
|
|
|
|
CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Tell the thread it will run in User Mode */
|
|
|
|
Thread->PreviousMode = UserMode;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Tell KiThreadStartup of that too */
|
|
|
|
StartFrame->UserThread = TRUE;
|
2005-05-09 01:38:29 +00:00
|
|
|
}
|
|
|
|
else
|
2005-04-23 04:12:26 +00:00
|
|
|
{
|
2006-01-15 09:23:55 +00:00
|
|
|
/* Set up the Initial Frame for the system thread */
|
2005-04-22 12:52:25 +00:00
|
|
|
PKKINIT_FRAME InitFrame;
|
2006-01-15 09:23:55 +00:00
|
|
|
InitFrame = (PKKINIT_FRAME)((ULONG_PTR)Thread->InitialStack -
|
|
|
|
sizeof(KKINIT_FRAME));
|
2006-01-14 20:36:49 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Setup the Fx Area */
|
|
|
|
FxSaveArea = &InitFrame->FxSaveArea;
|
|
|
|
RtlZeroMemory(FxSaveArea, sizeof(FX_SAVE_AREA));
|
2006-01-14 20:36:49 +00:00
|
|
|
|
2006-01-15 09:23:55 +00:00
|
|
|
/* Check if we have Fxsr support */
|
|
|
|
if (KeI386FxsrPresent)
|
|
|
|
{
|
|
|
|
/* Set the stub FX area */
|
|
|
|
FxSaveArea->U.FxArea.ControlWord = 0x27F;
|
|
|
|
FxSaveArea->U.FxArea.MXCsr = 0x1F80;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Set the stub FN area */
|
|
|
|
FxSaveArea->U.FnArea.ControlWord = 0x27F;
|
|
|
|
FxSaveArea->U.FnArea.TagWord = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No NPX State */
|
|
|
|
Thread->NpxState = NPX_STATE_NOT_LOADED;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Setup the Stack for KiThreadStartup and Context Switching */
|
|
|
|
StartFrame = &InitFrame->StartFrame;
|
|
|
|
CtxSwitchFrame = &InitFrame->CtxSwitchFrame;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Tell the thread it will run in Kernel Mode */
|
|
|
|
Thread->PreviousMode = KernelMode;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Tell KiThreadStartup of that too */
|
|
|
|
StartFrame->UserThread = FALSE;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Now setup the remaining data for KiThreadStartup */
|
|
|
|
StartFrame->StartContext = StartContext;
|
|
|
|
StartFrame->StartRoutine = StartRoutine;
|
|
|
|
StartFrame->SystemRoutine = SystemRoutine;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* And set up the Context Switch Frame */
|
2007-01-17 20:44:37 +00:00
|
|
|
CtxSwitchFrame->RetAddr = KiThreadStartup;
|
|
|
|
CtxSwitchFrame->ApcBypassDisable = TRUE;
|
2008-09-12 11:13:15 +00:00
|
|
|
CtxSwitchFrame->ExceptionList = EXCEPTION_CHAIN_END;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-22 12:52:25 +00:00
|
|
|
/* Save back the new value of the kernel stack. */
|
2005-04-23 04:12:26 +00:00
|
|
|
Thread->KernelStack = (PVOID)CtxSwitchFrame;
|
1999-11-12 12:01:17 +00:00
|
|
|
}
|
|
|
|
|
2020-09-09 01:46:56 +00:00
|
|
|
DECLSPEC_NORETURN
|
2010-11-05 15:58:34 +00:00
|
|
|
VOID
|
|
|
|
KiIdleLoop(VOID)
|
|
|
|
{
|
|
|
|
PKPRCB Prcb = KeGetCurrentPrcb();
|
|
|
|
PKTHREAD OldThread, NewThread;
|
2004-11-20 23:46:37 +00:00
|
|
|
|
2010-11-05 15:58:34 +00:00
|
|
|
/* Now loop forever */
|
|
|
|
while (TRUE)
|
|
|
|
{
|
2017-03-27 20:16:59 +00:00
|
|
|
/* Start of the idle loop: disable interrupts */
|
|
|
|
_enable();
|
|
|
|
YieldProcessor();
|
|
|
|
YieldProcessor();
|
|
|
|
_disable();
|
|
|
|
|
2010-11-05 15:58:34 +00:00
|
|
|
/* Check for pending timers, pending DPCs, or pending ready threads */
|
|
|
|
if ((Prcb->DpcData[0].DpcQueueDepth) ||
|
|
|
|
(Prcb->TimerRequest) ||
|
|
|
|
(Prcb->DeferredReadyListHead.Next))
|
|
|
|
{
|
|
|
|
/* Quiesce the DPC software interrupt */
|
|
|
|
HalClearSoftwareInterrupt(DISPATCH_LEVEL);
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-05 15:58:34 +00:00
|
|
|
/* Handle it */
|
|
|
|
KiRetireDpcList(Prcb);
|
|
|
|
}
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-05 15:58:34 +00:00
|
|
|
/* Check if a new thread is scheduled for execution */
|
|
|
|
if (Prcb->NextThread)
|
|
|
|
{
|
2016-02-04 20:27:44 +00:00
|
|
|
/* Enable interrupts */
|
2010-11-05 15:58:34 +00:00
|
|
|
_enable();
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-05 15:58:34 +00:00
|
|
|
/* Capture current thread data */
|
|
|
|
OldThread = Prcb->CurrentThread;
|
|
|
|
NewThread = Prcb->NextThread;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-05 15:58:34 +00:00
|
|
|
/* Set new thread data */
|
|
|
|
Prcb->NextThread = NULL;
|
|
|
|
Prcb->CurrentThread = NewThread;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-05 15:58:34 +00:00
|
|
|
/* The thread is now running */
|
|
|
|
NewThread->State = Running;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-05 15:58:34 +00:00
|
|
|
/* Switch away from the idle thread */
|
2010-11-08 11:56:22 +00:00
|
|
|
KiSwapContext(APC_LEVEL, OldThread);
|
2010-11-05 15:58:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Continue staying idle. Note the HAL returns with interrupts on */
|
|
|
|
Prcb->PowerState.IdleFunction(&Prcb->PowerState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-01-17 20:44:37 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
BOOLEAN
|
|
|
|
FASTCALL
|
|
|
|
KiSwapContextExit(IN PKTHREAD OldThread,
|
|
|
|
IN PKSWITCHFRAME SwitchFrame)
|
|
|
|
{
|
|
|
|
PKIPCR Pcr = (PKIPCR)KeGetPcr();
|
|
|
|
PKPROCESS OldProcess, NewProcess;
|
|
|
|
PKTHREAD NewThread;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
/* We are on the new thread stack now */
|
|
|
|
NewThread = Pcr->PrcbData.CurrentThread;
|
|
|
|
|
|
|
|
/* Now we are the new thread. Check if it's in a new process */
|
|
|
|
OldProcess = OldThread->ApcState.Process;
|
|
|
|
NewProcess = NewThread->ApcState.Process;
|
|
|
|
if (OldProcess != NewProcess)
|
|
|
|
{
|
|
|
|
/* Check if there is a different LDT */
|
|
|
|
if (*(PULONGLONG)&OldProcess->LdtDescriptor != *(PULONGLONG)&NewProcess->LdtDescriptor)
|
|
|
|
{
|
2016-07-19 12:51:49 +00:00
|
|
|
if (NewProcess->LdtDescriptor.LimitLow)
|
|
|
|
{
|
|
|
|
KeSetGdtSelector(KGDT_LDT,
|
|
|
|
((PULONG)&NewProcess->LdtDescriptor)[0],
|
|
|
|
((PULONG)&NewProcess->LdtDescriptor)[1]);
|
|
|
|
Ke386SetLocalDescriptorTable(KGDT_LDT);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Ke386SetLocalDescriptorTable(0);
|
|
|
|
}
|
2010-11-08 02:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Switch address space and flush TLB */
|
|
|
|
__writecr3(NewProcess->DirectoryTableBase[0]);
|
|
|
|
}
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
/* Clear GS */
|
|
|
|
Ke386SetGs(0);
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
/* Set the TEB */
|
2021-03-11 15:26:28 +00:00
|
|
|
KiSetTebBase((PKPCR)Pcr, &NewThread->Teb->NtTib);
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
/* Set new TSS fields */
|
2010-11-19 06:46:59 +00:00
|
|
|
Pcr->TSS->Esp0 = (ULONG_PTR)NewThread->InitialStack;
|
|
|
|
if (!((KeGetTrapFrame(NewThread))->EFlags & EFLAGS_V86_MASK))
|
2010-11-08 02:15:53 +00:00
|
|
|
{
|
2019-12-01 19:36:13 +00:00
|
|
|
Pcr->TSS->Esp0 -= sizeof(KTRAP_FRAME) - FIELD_OFFSET(KTRAP_FRAME, V86Es);
|
2010-11-08 02:15:53 +00:00
|
|
|
}
|
2010-11-19 06:46:59 +00:00
|
|
|
Pcr->TSS->Esp0 -= NPX_FRAME_LENGTH;
|
2010-11-08 02:15:53 +00:00
|
|
|
Pcr->TSS->IoMapBase = NewProcess->IopmOffset;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
/* Increase thread context switches */
|
|
|
|
NewThread->ContextSwitches++;
|
|
|
|
|
|
|
|
/* Load data from switch frame */
|
|
|
|
Pcr->NtTib.ExceptionList = SwitchFrame->ExceptionList;
|
|
|
|
|
|
|
|
/* DPCs shouldn't be active */
|
|
|
|
if (Pcr->PrcbData.DpcRoutineActive)
|
|
|
|
{
|
|
|
|
/* Crash the machine */
|
|
|
|
KeBugCheckEx(ATTEMPTED_SWITCH_FROM_DPC,
|
|
|
|
(ULONG_PTR)OldThread,
|
|
|
|
(ULONG_PTR)NewThread,
|
|
|
|
(ULONG_PTR)OldThread->InitialStack,
|
|
|
|
0);
|
|
|
|
}
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
/* Kernel APCs may be pending */
|
|
|
|
if (NewThread->ApcState.KernelApcPending)
|
|
|
|
{
|
|
|
|
/* Are APCs enabled? */
|
|
|
|
if (!NewThread->SpecialApcDisable)
|
|
|
|
{
|
|
|
|
/* Request APC delivery */
|
2011-04-24 11:24:37 +00:00
|
|
|
if (SwitchFrame->ApcBypassDisable)
|
|
|
|
HalRequestSoftwareInterrupt(APC_LEVEL);
|
|
|
|
else
|
|
|
|
return TRUE;
|
2010-11-08 02:15:53 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2011-04-24 11:24:37 +00:00
|
|
|
/* Return stating that no kernel APCs are pending*/
|
2010-11-08 02:15:53 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID
|
|
|
|
FASTCALL
|
|
|
|
KiSwapContextEntry(IN PKSWITCHFRAME SwitchFrame,
|
|
|
|
IN ULONG_PTR OldThreadAndApcFlag)
|
|
|
|
{
|
|
|
|
PKIPCR Pcr = (PKIPCR)KeGetPcr();
|
|
|
|
PKTHREAD OldThread, NewThread;
|
|
|
|
ULONG Cr0, NewCr0;
|
|
|
|
|
|
|
|
/* Save APC bypass disable */
|
|
|
|
SwitchFrame->ApcBypassDisable = OldThreadAndApcFlag & 3;
|
|
|
|
SwitchFrame->ExceptionList = Pcr->NtTib.ExceptionList;
|
|
|
|
|
|
|
|
/* Increase context switch count and check if tracing is enabled */
|
|
|
|
Pcr->ContextSwitches++;
|
|
|
|
if (Pcr->PerfGlobalGroupMask)
|
|
|
|
{
|
|
|
|
/* We don't support this yet on x86 either */
|
|
|
|
DPRINT1("WMI Tracing not supported\n");
|
|
|
|
ASSERT(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get thread pointers */
|
|
|
|
OldThread = (PKTHREAD)(OldThreadAndApcFlag & ~3);
|
|
|
|
NewThread = Pcr->PrcbData.CurrentThread;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
/* Get the old thread and set its kernel stack */
|
|
|
|
OldThread->KernelStack = SwitchFrame;
|
|
|
|
|
|
|
|
/* ISRs can change FPU state, so disable interrupts while checking */
|
|
|
|
_disable();
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:15:53 +00:00
|
|
|
/* Get current and new CR0 and check if they've changed */
|
|
|
|
Cr0 = __readcr0();
|
|
|
|
NewCr0 = NewThread->NpxState |
|
|
|
|
(Cr0 & ~(CR0_MP | CR0_EM | CR0_TS)) |
|
2010-11-19 06:46:59 +00:00
|
|
|
KiGetThreadNpxArea(NewThread)->Cr0NpxState;
|
2010-11-08 02:15:53 +00:00
|
|
|
if (Cr0 != NewCr0) __writecr0(NewCr0);
|
|
|
|
|
|
|
|
/* Now enable interrupts and do the switch */
|
|
|
|
_enable();
|
2010-11-08 11:56:22 +00:00
|
|
|
KiSwitchThreads(OldThread, NewThread->KernelStack);
|
2010-11-08 02:15:53 +00:00
|
|
|
}
|
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
VOID
|
|
|
|
NTAPI
|
|
|
|
KiDispatchInterrupt(VOID)
|
|
|
|
{
|
|
|
|
PKIPCR Pcr = (PKIPCR)KeGetPcr();
|
|
|
|
PKPRCB Prcb = &Pcr->PrcbData;
|
|
|
|
PVOID OldHandler;
|
|
|
|
PKTHREAD NewThread, OldThread;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
/* Disable interrupts */
|
|
|
|
_disable();
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
/* Check for pending timers, pending DPCs, or pending ready threads */
|
|
|
|
if ((Prcb->DpcData[0].DpcQueueDepth) ||
|
|
|
|
(Prcb->TimerRequest) ||
|
|
|
|
(Prcb->DeferredReadyListHead.Next))
|
|
|
|
{
|
|
|
|
/* Switch to safe execution context */
|
|
|
|
OldHandler = Pcr->NtTib.ExceptionList;
|
|
|
|
Pcr->NtTib.ExceptionList = EXCEPTION_CHAIN_END;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
/* Retire DPCs while under the DPC stack */
|
|
|
|
KiRetireDpcListInDpcStack(Prcb, Prcb->DpcStack);
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
/* Restore context */
|
|
|
|
Pcr->NtTib.ExceptionList = OldHandler;
|
|
|
|
}
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
/* Re-enable interrupts */
|
|
|
|
_enable();
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
/* Check for quantum end */
|
|
|
|
if (Prcb->QuantumEnd)
|
|
|
|
{
|
|
|
|
/* Handle quantum end */
|
|
|
|
Prcb->QuantumEnd = FALSE;
|
|
|
|
KiQuantumEnd();
|
|
|
|
}
|
|
|
|
else if (Prcb->NextThread)
|
2013-03-10 11:44:04 +00:00
|
|
|
{
|
2010-11-08 02:37:17 +00:00
|
|
|
/* Capture current thread data */
|
|
|
|
OldThread = Prcb->CurrentThread;
|
|
|
|
NewThread = Prcb->NextThread;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
/* Set new thread data */
|
|
|
|
Prcb->NextThread = NULL;
|
|
|
|
Prcb->CurrentThread = NewThread;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
/* The thread is now running */
|
|
|
|
NewThread->State = Running;
|
|
|
|
OldThread->WaitReason = WrDispatchInt;
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 02:37:17 +00:00
|
|
|
/* Make the old thread ready */
|
|
|
|
KxQueueReadyThread(OldThread, Prcb);
|
2010-11-19 06:28:20 +00:00
|
|
|
|
2010-11-08 11:56:22 +00:00
|
|
|
/* Swap to the new thread */
|
|
|
|
KiSwapContext(APC_LEVEL, OldThread);
|
2010-11-08 02:37:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-05 15:58:34 +00:00
|
|
|
/* EOF */
|