Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610/* * PROJECT: ReactOS Kernel * LICENSE: GPL - See COPYING in the top level directory * FILE: ntoskrnl/ps/state.c * PURPOSE: Process Manager: Process/Thread State Control * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org) * Thomas Weidenmueller (w3seek@reactos.org) */
/* INCLUDES ******************************************************************/
#include <ntoskrnl.h>#define NDEBUG#include <debug.h>
/* PRIVATE FUNCTIONS *********************************************************/
VOIDNTAPIPspQueueApcSpecialApc(IN PKAPC Apc, IN OUT PKNORMAL_ROUTINE* NormalRoutine, IN OUT PVOID* NormalContext, IN OUT PVOID* SystemArgument1, IN OUT PVOID* SystemArgument2){ /* Free the APC and do nothing else */ ExFreePool(Apc);}
NTSTATUSNTAPIPsResumeThread(IN PETHREAD Thread, OUT PULONG PreviousCount OPTIONAL){ ULONG OldCount; PAGED_CODE();
/* Resume the thread */ OldCount = KeResumeThread(&Thread->Tcb);
/* Return the count if asked */ if (PreviousCount) *PreviousCount = OldCount; return STATUS_SUCCESS;}
NTSTATUSNTAPIPsSuspendThread( IN PETHREAD Thread, OUT PULONG PreviousCount OPTIONAL){ NTSTATUS Status; ULONG OldCount = 0; PAGED_CODE();
/* Assume success */ Status = STATUS_SUCCESS;
/* Check if we're suspending ourselves */ if (Thread == PsGetCurrentThread()) { /* Guard with SEH because KeSuspendThread can raise an exception */ _SEH2_TRY { /* Do the suspend */ OldCount = KeSuspendThread(&Thread->Tcb); } _SEH2_EXCEPT(_SEH2_GetExceptionCode() == STATUS_SUSPEND_COUNT_EXCEEDED) { /* Get the exception code */ Status = _SEH2_GetExceptionCode(); } _SEH2_END; } else { /* Acquire rundown protection */ if (ExAcquireRundownProtection(&Thread->RundownProtect)) { /* Make sure the thread isn't terminating */ if (Thread->Terminated) { /* Fail */ Status = STATUS_THREAD_IS_TERMINATING; } else { /* Guard with SEH because KeSuspendThread can raise an exception */ _SEH2_TRY { /* Do the suspend */ OldCount = KeSuspendThread(&Thread->Tcb); } _SEH2_EXCEPT(_SEH2_GetExceptionCode() == STATUS_SUSPEND_COUNT_EXCEEDED) { /* Get the exception code */ Status = _SEH2_GetExceptionCode(); } _SEH2_END;
/* Check if it was terminated during the suspend */ if (Thread->Terminated) { /* Wake it back up and fail */ KeForceResumeThread(&Thread->Tcb); Status = STATUS_THREAD_IS_TERMINATING; OldCount = 0; } }
/* Release rundown protection */ ExReleaseRundownProtection(&Thread->RundownProtect); } else { /* Thread is terminating */ Status = STATUS_THREAD_IS_TERMINATING; } }
/* Write back the previous count */ if (PreviousCount) *PreviousCount = OldCount; return Status;}
NTSTATUSNTAPIPsResumeProcess(IN PEPROCESS Process){ PETHREAD Thread; PAGED_CODE();
/* Lock the Process */ if (!ExAcquireRundownProtection(&Process->RundownProtect)) { /* Process is terminating */ return STATUS_PROCESS_IS_TERMINATING; }
/* Get the first thread */ Thread = PsGetNextProcessThread(Process, NULL); while (Thread) { /* Resume it */ KeResumeThread(&Thread->Tcb);
/* Move to the next thread */ Thread = PsGetNextProcessThread(Process, Thread); }
/* Unlock the process */ ExReleaseRundownProtection(&Process->RundownProtect); return STATUS_SUCCESS;}
NTSTATUSNTAPIPsSuspendProcess(IN PEPROCESS Process){ PETHREAD Thread; PAGED_CODE();
/* Lock the Process */ if (!ExAcquireRundownProtection(&Process->RundownProtect)) { /* Process is terminating */ return STATUS_PROCESS_IS_TERMINATING; }
/* Get the first thread */ Thread = PsGetNextProcessThread(Process, NULL); while (Thread) { /* Resume it */ PsSuspendThread(Thread, NULL);
/* Move to the next thread */ Thread = PsGetNextProcessThread(Process, Thread); }
/* Unlock the process */ ExReleaseRundownProtection(&Process->RundownProtect); return STATUS_SUCCESS;}
/* PUBLIC FUNCTIONS **********************************************************/
/* * @implemented */NTSTATUSNTAPINtAlertThread(IN HANDLE ThreadHandle){ KPROCESSOR_MODE PreviousMode = ExGetPreviousMode(); PETHREAD Thread; NTSTATUS Status;
/* Reference the Object */ Status = ObReferenceObjectByHandle(ThreadHandle, THREAD_SUSPEND_RESUME, PsThreadType, PreviousMode, (PVOID*)&Thread, NULL); if (NT_SUCCESS(Status)) { /* * Do an alert depending on the processor mode. If some kmode code wants to * enforce a umode alert it should call KeAlertThread() directly. If kmode * code wants to do a kmode alert it's sufficient to call it with Zw or just * use KeAlertThread() directly */ KeAlertThread(&Thread->Tcb, PreviousMode);
/* Dereference Object */ ObDereferenceObject(Thread); }
/* Return status */ return Status;}
NTSTATUSNTAPINtAlertResumeThread(IN HANDLE ThreadHandle, OUT PULONG SuspendCount){ KPROCESSOR_MODE PreviousMode = ExGetPreviousMode(); PETHREAD Thread; NTSTATUS Status; ULONG PreviousState;
/* Check if we came from user mode with a suspend count */ if ((SuspendCount) && (PreviousMode != KernelMode)) { /* Enter SEH for probing */ _SEH2_TRY { /* Probe the count */ ProbeForWriteUlong(SuspendCount); } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { /* Return the exception code */ _SEH2_YIELD(return _SEH2_GetExceptionCode()); } _SEH2_END; }
/* Reference the Object */ Status = ObReferenceObjectByHandle(ThreadHandle, THREAD_SUSPEND_RESUME, PsThreadType, PreviousMode, (PVOID*)&Thread, NULL); if (NT_SUCCESS(Status)) { /* Call the Kernel Function */ PreviousState = KeAlertResumeThread(&Thread->Tcb);
/* Dereference Object */ ObDereferenceObject(Thread);
/* Check if the caller gave a suspend count */ if (SuspendCount) { /* Enter SEH for write */ _SEH2_TRY { /* Write state back */ *SuspendCount = PreviousState; } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { /* Get exception code */ Status = _SEH2_GetExceptionCode(); } _SEH2_END; } }
/* Return status */ return Status;}
NTSTATUSNTAPINtResumeThread(IN HANDLE ThreadHandle, OUT PULONG SuspendCount OPTIONAL){ PETHREAD Thread; ULONG Prev; KPROCESSOR_MODE PreviousMode = ExGetPreviousMode(); NTSTATUS Status; PAGED_CODE();
/* Check if caller gave a suspend count from user mode */ if ((SuspendCount) && (PreviousMode != KernelMode)) { /* Enter SEH for probing */ _SEH2_TRY { /* Probe the count */ ProbeForWriteUlong(SuspendCount); } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { /* Return the exception code */ _SEH2_YIELD(return _SEH2_GetExceptionCode()); } _SEH2_END; }
/* Get the Thread Object */ Status = ObReferenceObjectByHandle(ThreadHandle, THREAD_SUSPEND_RESUME, PsThreadType, PreviousMode, (PVOID*)&Thread, NULL); if (!NT_SUCCESS(Status)) return Status;
/* Call the internal function */ Status = PsResumeThread(Thread, &Prev);
/* Check if the caller wanted the count back */ if (SuspendCount) { /* Enter SEH for write back */ _SEH2_TRY { /* Write the count */ *SuspendCount = Prev; } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { /* Get the exception code */ Status = _SEH2_GetExceptionCode(); } _SEH2_END; }
/* Dereference and return */ ObDereferenceObject(Thread); return Status;}
NTSTATUSNTAPINtSuspendThread(IN HANDLE ThreadHandle, OUT PULONG PreviousSuspendCount OPTIONAL){ PETHREAD Thread; ULONG Prev; KPROCESSOR_MODE PreviousMode = ExGetPreviousMode(); NTSTATUS Status; PAGED_CODE();
/* Check if caller gave a suspend count from user mode */ if ((PreviousSuspendCount) && (PreviousMode != KernelMode)) { /* Enter SEH for probing */ _SEH2_TRY { /* Probe the count */ ProbeForWriteUlong(PreviousSuspendCount); } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { /* Return the exception code */ _SEH2_YIELD(return _SEH2_GetExceptionCode()); } _SEH2_END; }
/* Get the Thread Object */ Status = ObReferenceObjectByHandle(ThreadHandle, THREAD_SUSPEND_RESUME, PsThreadType, PreviousMode, (PVOID*)&Thread, NULL); if (!NT_SUCCESS(Status)) return Status;
/* Call the internal function */ Status = PsSuspendThread(Thread, &Prev); ObDereferenceObject(Thread); if (!NT_SUCCESS(Status)) return Status;
/* Protect write with SEH */ _SEH2_TRY { /* Return the Previous Count */ if (PreviousSuspendCount) *PreviousSuspendCount = Prev; } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) { /* Get the exception code */ Status = _SEH2_GetExceptionCode(); } _SEH2_END;
/* Return */ return Status;}
NTSTATUSNTAPINtSuspendProcess(IN HANDLE ProcessHandle){ KPROCESSOR_MODE PreviousMode = ExGetPreviousMode(); PEPROCESS Process; NTSTATUS Status; PAGED_CODE();
/* Reference the process */ Status = ObReferenceObjectByHandle(ProcessHandle, PROCESS_SUSPEND_RESUME, PsProcessType, PreviousMode, (PVOID*)&Process, NULL); if (NT_SUCCESS(Status)) { /* Call the internal function */ Status = PsSuspendProcess(Process); ObDereferenceObject(Process); }
/* Return status */ return Status;}
NTSTATUSNTAPINtResumeProcess(IN HANDLE ProcessHandle){ KPROCESSOR_MODE PreviousMode = ExGetPreviousMode(); PEPROCESS Process; NTSTATUS Status; PAGED_CODE();
/* Reference the process */ Status = ObReferenceObjectByHandle(ProcessHandle, PROCESS_SUSPEND_RESUME, PsProcessType, PreviousMode, (PVOID*)&Process, NULL); if (NT_SUCCESS(Status)) { /* Call the internal function */ Status = PsResumeProcess(Process); ObDereferenceObject(Process); }
/* Return status */ return Status;}
NTSTATUSNTAPINtTestAlert(VOID){ /* Check and Alert Thread if needed */ return KeTestAlertThread(ExGetPreviousMode()) ? STATUS_ALERTED : STATUS_SUCCESS;}
/*++ * @name NtQueueApcThreadEx * NT4 * * This routine is used to queue an APC from user-mode for the specified * thread. * * @param ThreadHandle * Handle to the Thread. * This handle must have THREAD_SET_CONTEXT privileges. * * @param UserApcReserveHandle * Optional handle to reserve object (introduced in Windows 7), providing ability to * reserve memory before performing stability-critical parts of code. * * @param ApcRoutine * Pointer to the APC Routine to call when the APC executes. * * @param NormalContext * Pointer to the context to send to the Normal Routine. * * @param SystemArgument[1-2] * Pointer to a set of two parameters that contain untyped data. * * @return STATUS_SUCCESS or failure cute from associated calls. * * @remarks The thread must enter an alertable wait before the APC will be * delivered. * *--*/NTSTATUSNTAPINtQueueApcThreadEx(IN HANDLE ThreadHandle, IN OPTIONAL HANDLE UserApcReserveHandle, IN PKNORMAL_ROUTINE ApcRoutine, IN PVOID NormalContext, IN OPTIONAL PVOID SystemArgument1, IN OPTIONAL PVOID SystemArgument2){ PKAPC Apc; PETHREAD Thread; NTSTATUS Status = STATUS_SUCCESS; PAGED_CODE();
/* Get ETHREAD from Handle */ Status = ObReferenceObjectByHandle(ThreadHandle, THREAD_SET_CONTEXT, PsThreadType, ExGetPreviousMode(), (PVOID)&Thread, NULL); if (!NT_SUCCESS(Status)) return Status;
/* Check if this is a System Thread */ if (Thread->SystemThread) { /* Fail */ Status = STATUS_INVALID_HANDLE; goto Quit; }
/* Allocate an APC */ Apc = ExAllocatePoolWithQuotaTag(NonPagedPool | POOL_QUOTA_FAIL_INSTEAD_OF_RAISE, sizeof(KAPC), TAG_PS_APC); if (!Apc) { /* Fail */ Status = STATUS_NO_MEMORY; goto Quit; }
/* Initialize the APC */ KeInitializeApc(Apc, &Thread->Tcb, OriginalApcEnvironment, PspQueueApcSpecialApc, NULL, ApcRoutine, UserMode, NormalContext);
/* Queue it */ if (!KeInsertQueueApc(Apc, SystemArgument1, SystemArgument2, IO_NO_INCREMENT)) { /* We failed, free it */ ExFreePool(Apc); Status = STATUS_UNSUCCESSFUL; }
/* Dereference Thread and Return */Quit: ObDereferenceObject(Thread); return Status;}
/*++ * @name NtQueueApcThread * NT4 * * This routine is used to queue an APC from user-mode for the specified * thread. * * @param ThreadHandle * Handle to the Thread. * This handle must have THREAD_SET_CONTEXT privileges. * * @param ApcRoutine * Pointer to the APC Routine to call when the APC executes. * * @param NormalContext * Pointer to the context to send to the Normal Routine. * * @param SystemArgument[1-2] * Pointer to a set of two parameters that contain untyped data. * * @return STATUS_SUCCESS or failure cute from associated calls. * * @remarks The thread must enter an alertable wait before the APC will be * delivered. * *--*/NTSTATUSNTAPINtQueueApcThread(IN HANDLE ThreadHandle, IN PKNORMAL_ROUTINE ApcRoutine, IN PVOID NormalContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2){ return NtQueueApcThreadEx(ThreadHandle, NULL, ApcRoutine, NormalContext, SystemArgument1, SystemArgument2);}
/* EOF */