From c30ca53066dd624831fc47e0bf3a3c783d3de75b Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Wed, 13 Oct 2004 01:42:14 +0000 Subject: [PATCH] Implemented KeAreApcsDisabled, KeFlushEntireTb, KeIsExecutingDpc, KeSetKernelStackSwapEnable, KeQueryPriorityThread, KeRevertToUserAffinityThread, KeSetIdealProcessorThread, KeSetSystemAffinityThread, KeTerminateThread, KeStackAttachProcess, KeUnstackDetachProcess svn path=/trunk/; revision=11284 --- reactos/include/ddk/kefuncs.h | 5 +- reactos/ntoskrnl/Makefile.i386 | 1 + reactos/ntoskrnl/include/internal/ke.h | 4 ++ reactos/ntoskrnl/include/internal/ps.h | 3 +- reactos/ntoskrnl/ke/apc.c | 4 +- reactos/ntoskrnl/ke/device.c | 44 +++++++++++++++-- reactos/ntoskrnl/ke/dpc.c | 8 +-- reactos/ntoskrnl/ke/kthread.c | 67 +++++++++++++++++++------ reactos/ntoskrnl/ke/process.c | 68 ++++++++++++++++++++++++-- 9 files changed, 171 insertions(+), 33 deletions(-) diff --git a/reactos/include/ddk/kefuncs.h b/reactos/include/ddk/kefuncs.h index 883606e7cd0..c4bcd839d7d 100644 --- a/reactos/include/ddk/kefuncs.h +++ b/reactos/include/ddk/kefuncs.h @@ -597,7 +597,7 @@ KeTerminateThread( IN KPRIORITY Increment ); -VOID +BOOLEAN STDCALL KeIsExecutingDpc( VOID @@ -632,7 +632,8 @@ KeFindConfigurationNextEntry( VOID STDCALL KeFlushEntireTb( - IN ULONGLONG Flag + IN BOOLEAN Unknown, + IN BOOLEAN CurrentCpuOnly ); VOID diff --git a/reactos/ntoskrnl/Makefile.i386 b/reactos/ntoskrnl/Makefile.i386 index 76c401c421d..61454fd5c74 100644 --- a/reactos/ntoskrnl/Makefile.i386 +++ b/reactos/ntoskrnl/Makefile.i386 @@ -30,6 +30,7 @@ OBJECTS_KE_I386 := \ ke/i386/brkpoint.o \ ke/i386/kernel.o \ ke/i386/fpu.o \ + ke/i386/tlbflush.o \ ke/i386/tss.o \ ke/i386/usertrap.o \ ke/i386/stkswitch.o diff --git a/reactos/ntoskrnl/include/internal/ke.h b/reactos/ntoskrnl/include/internal/ke.h index 613b32b170d..5221bb559b0 100644 --- a/reactos/ntoskrnl/include/internal/ke.h +++ b/reactos/ntoskrnl/include/internal/ke.h @@ -169,6 +169,10 @@ KeUpdateRunTime( IN PKTRAP_FRAME TrapFrame ); +VOID +STDCALL +KeFlushCurrentTb(VOID); + #endif /* not __ASM__ */ #define MAXIMUM_PROCESSORS 32 diff --git a/reactos/ntoskrnl/include/internal/ps.h b/reactos/ntoskrnl/include/internal/ps.h index 9f19e77c475..62e0c886004 100644 --- a/reactos/ntoskrnl/include/internal/ps.h +++ b/reactos/ntoskrnl/include/internal/ps.h @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: ps.h,v 1.70 2004/10/01 20:26:04 gvg Exp $ +/* $Id: ps.h,v 1.71 2004/10/13 01:42:14 ion Exp $ * * FILE: ntoskrnl/ke/kthread.c * PURPOSE: Process manager definitions @@ -445,6 +445,7 @@ VOID PsInitProcessManagment(VOID); VOID PsInitIdleThread(VOID); VOID PsDispatchThreadNoLock(ULONG NewThreadStatus); VOID PiTerminateProcessThreads(PEPROCESS Process, NTSTATUS ExitStatus); +VOID PsTerminateCurrentThread(NTSTATUS ExitStatus); VOID PsTerminateOtherThread(PETHREAD Thread, NTSTATUS ExitStatus); VOID PsReleaseThread(PETHREAD Thread); VOID PsBeginThread(PKSTART_ROUTINE StartRoutine, PVOID StartContext); diff --git a/reactos/ntoskrnl/ke/apc.c b/reactos/ntoskrnl/ke/apc.c index 202774f0756..284cad75ad4 100644 --- a/reactos/ntoskrnl/ke/apc.c +++ b/reactos/ntoskrnl/ke/apc.c @@ -44,7 +44,7 @@ VOID PsTerminateCurrentThread(NTSTATUS ExitStatus); /* FUNCTIONS *****************************************************************/ /* - * @unimplemented + * @implemented */ BOOLEAN STDCALL @@ -52,7 +52,7 @@ KeAreApcsDisabled( VOID ) { - UNIMPLEMENTED; + return KeGetCurrentThread()->KernelApcDisable ? FALSE : TRUE; } VOID KiRundownThread(VOID) diff --git a/reactos/ntoskrnl/ke/device.c b/reactos/ntoskrnl/ke/device.c index 851554a0050..d355157b95b 100644 --- a/reactos/ntoskrnl/ke/device.c +++ b/reactos/ntoskrnl/ke/device.c @@ -1,4 +1,4 @@ -/* $Id: device.c,v 1.2 2004/08/15 16:39:05 chorns Exp $ +/* $Id: device.c,v 1.3 2004/10/13 01:42:14 ion Exp $ * * FILE: ntoskrnl/ke/profile.c * PURPOSE: Kernel Device/Settings Functions @@ -45,15 +45,51 @@ KeFindConfigurationNextEntry( } /* - * @unimplemented + * @implemented */ STDCALL VOID KeFlushEntireTb( - IN ULONGLONG Flag + IN BOOLEAN Unknown, + IN BOOLEAN CurrentCpuOnly ) { - UNIMPLEMENTED; + KIRQL OldIrql; + PKPROCESS Process; + PKPCR Pcr; + + /* Raise the IRQL for the TB Flush */ + OldIrql = KeRaiseIrqlToSynchLevel(); + + /* All CPUs need to have the TB flushed. */ + if (CurrentCpuOnly == FALSE) { + Pcr = KeGetCurrentKPCR(); + + /* How many CPUs is our caller using? */ + Process = Pcr->PrcbData.CurrentThread->ApcState.Process; + + /* More then one, so send an IPI */ + if (Process->ActiveProcessors > 1) { + /* Send IPI Packet */ + } + } + + /* Flush the TB for the Current CPU */ + KeFlushCurrentTb(); + + /* Clean up */ + if (CurrentCpuOnly == FALSE) { + /* Did we send an IPI? If so, wait for completion */ + if (Process->ActiveProcessors > 1) { + do { + } while (Pcr->PrcbData.TargetSet != 0); + } + } + + /* FIXME: According to MSKB, we should increment a counter? */ + + /* Return to Original IRQL */ + KeLowerIrql(OldIrql); } diff --git a/reactos/ntoskrnl/ke/dpc.c b/reactos/ntoskrnl/ke/dpc.c index bafba4f788e..1a16c0a28b9 100644 --- a/reactos/ntoskrnl/ke/dpc.c +++ b/reactos/ntoskrnl/ke/dpc.c @@ -18,7 +18,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: dpc.c,v 1.36 2004/10/01 20:09:57 hbirr Exp $ +/* $Id: dpc.c,v 1.37 2004/10/13 01:42:14 ion Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -144,15 +144,15 @@ KeFlushQueuedDpcs( } /* - * @unimplemented + * @implemented */ -VOID +BOOLEAN STDCALL KeIsExecutingDpc( VOID ) { - UNIMPLEMENTED; + return KeGetCurrentKPCR()->PrcbData.DpcRoutineActive; } /* diff --git a/reactos/ntoskrnl/ke/kthread.c b/reactos/ntoskrnl/ke/kthread.c index 298d0de666c..c3c4f71e7be 100644 --- a/reactos/ntoskrnl/ke/kthread.c +++ b/reactos/ntoskrnl/ke/kthread.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: kthread.c,v 1.53 2004/09/28 15:02:29 weiden Exp $ +/* $Id: kthread.c,v 1.54 2004/10/13 01:42:14 ion Exp $ * * FILE: ntoskrnl/ke/kthread.c * PURPOSE: Microkernel thread support @@ -63,7 +63,7 @@ KeFreeStackPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address, } /* - * @unimplemented + * @implemented */ KPRIORITY STDCALL @@ -71,8 +71,7 @@ KeQueryPriorityThread ( IN PKTHREAD Thread ) { - UNIMPLEMENTED; - return 0; + return Thread->Priority; } NTSTATUS @@ -106,7 +105,7 @@ KeReleaseThread(PKTHREAD Thread) } /* - * @unimplemented + * @implemented */ BOOLEAN STDCALL @@ -114,8 +113,19 @@ KeSetKernelStackSwapEnable( IN BOOLEAN Enable ) { - UNIMPLEMENTED; - return FALSE; + PKTHREAD Thread; + BOOLEAN PreviousState; + + Thread = KeGetCurrentThread(); + + /* Save Old State */ + PreviousState = Thread->EnableStackSwap; + + /* Set New State */ + Thread->EnableStackSwap = Enable; + + /* Return Old State */ + return PreviousState; } VOID @@ -324,7 +334,7 @@ KeRescheduleThread() } /* - * @unimplemented + * @implemented */ VOID STDCALL @@ -332,11 +342,18 @@ KeRevertToUserAffinityThread( VOID ) { - UNIMPLEMENTED; + PKTHREAD CurrentThread; + CurrentThread = KeGetCurrentThread(); + + /* Return to User Affinity */ + CurrentThread->Affinity = CurrentThread->UserAffinity; + + /* Disable System Affinity */ + CurrentThread->SystemAffinityActive = FALSE; } /* - * @unimplemented + * @implemented */ CCHAR STDCALL @@ -345,12 +362,20 @@ KeSetIdealProcessorThread ( IN CCHAR Processor ) { - UNIMPLEMENTED; - return 0; + CCHAR PreviousIdealProcessor; + + /* Save Old Ideal Processor */ + PreviousIdealProcessor = Thread->IdealProcessor; + + /* Set New Ideal Processor */ + Thread->IdealProcessor = Processor; + + /* Return Old Ideal Processor */ + return PreviousIdealProcessor; } /* - * @unimplemented + * @implemented */ VOID STDCALL @@ -358,11 +383,18 @@ KeSetSystemAffinityThread( IN KAFFINITY Affinity ) { - UNIMPLEMENTED; + PKTHREAD CurrentThread; + CurrentThread = KeGetCurrentThread(); + + /* Set the System Affinity Specified */ + CurrentThread->Affinity = Affinity; + + /* Enable System Affinity */ + CurrentThread->SystemAffinityActive = TRUE; } /* - * @unimplemented + * @implemented */ VOID STDCALL @@ -370,5 +402,8 @@ KeTerminateThread( IN KPRIORITY Increment ) { - UNIMPLEMENTED; + /* The Increment Argument seems to be ignored by NT and always 0 when called */ + + /* Call our own internal routine */ + PsTerminateCurrentThread(0); } diff --git a/reactos/ntoskrnl/ke/process.c b/reactos/ntoskrnl/ke/process.c index 38208f8bc53..b81dff00738 100644 --- a/reactos/ntoskrnl/ke/process.c +++ b/reactos/ntoskrnl/ke/process.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: process.c,v 1.27 2004/08/31 20:17:18 hbirr Exp $ +/* $Id: process.c,v 1.28 2004/10/13 01:42:14 ion Exp $ * * PROJECT: ReactOS kernel * FILE: ntoskrnl/ke/process.c @@ -123,11 +123,37 @@ KeStackAttachProcess ( OUT PRKAPC_STATE ApcState ) { - UNIMPLEMENTED; + KIRQL OldIrql; + PKTHREAD Thread; + + Thread = KeGetCurrentThread(); + OldIrql = KeAcquireDispatcherDatabaseLock(); + + /* Crash system if DPC is being executed! */ + if (KeIsExecutingDpc()) { + DPRINT1("Invalid attach (Thread is executing a DPC!)\n"); + KEBUGCHECK(INVALID_PROCESS_ATTACH_ATTEMPT); + } + + /* Check if the Target Process is already attached */ + if (Thread->ApcState.Process == Process) { + ApcState->Process = (PKPROCESS)1; /* Meaning already attached to the same Process */ + } else { + /* Check if the Current Thread is already attached */ + if (Thread->ApcStateIndex != 0) { + KeAttachProcess((PEPROCESS)Process); /* FIXME: Re-write function to support stackability and fix it not to use EPROCESS */ + } else { + KeAttachProcess((PEPROCESS)Process); + ApcState->Process = NULL; /* FIXME: Re-write function to support stackability and fix it not to use EPROCESS */ + } + } + + /* Return to old IRQL*/ + KeReleaseDispatcherDatabaseLock(OldIrql); } /* - * @unimplemented + * @implemented */ VOID STDCALL @@ -135,7 +161,41 @@ KeUnstackDetachProcess ( IN PRKAPC_STATE ApcState ) { - UNIMPLEMENTED; + KIRQL OldIrql; + PKTHREAD Thread; + ULONG PageDir; + + /* If the special "We tried to attach to the process already being attached to" flag is there, don't do anything */ + if (ApcState->Process == (PKPROCESS)1) return; + + Thread = KeGetCurrentThread(); + OldIrql = KeAcquireDispatcherDatabaseLock(); + + /* Sorry Buddy, can't help you if you've got APCs or just aren't attached */ + if ((Thread->ApcStateIndex == 0) || (Thread->ApcState.KernelApcInProgress)) { + DPRINT1("Invalid detach (Thread not Attached, or Kernel APC in Progress!)\n"); + KEBUGCHECK(INVALID_PROCESS_DETACH_ATTEMPT); + } + + /* Restore the Old APC State if a Process was present */ + if (ApcState->Process) { + RtlMoveMemory(ApcState, &Thread->ApcState, sizeof(KAPC_STATE)); + } else { + /* The ApcState parameter is useless, so use the saved data and reset it */ + RtlMoveMemory(&Thread->SavedApcState, &Thread->ApcState, sizeof(KAPC_STATE)); + Thread->SavedApcState.Process = NULL; + Thread->ApcStateIndex = 0; + Thread->ApcStatePointer[0] = &Thread->ApcState; + Thread->ApcStatePointer[1] = &Thread->SavedApcState; + } + + /* Do the Actual Swap */ + KiSwapApcEnvironment(Thread, Thread->SavedApcState.Process); + PageDir = Thread->ApcState.Process->DirectoryTableBase.u.LowPart; + Ke386SetPageTableDirectory(PageDir); + + /* Return to old IRQL*/ + KeReleaseDispatcherDatabaseLock(OldIrql); } /* EOF */