ReactOS 0.4.17-dev-1247-ge46f4bb
thread.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for thread.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

NTSTATUS NTAPI PspThreadOpen (_In_ OB_OPEN_REASON Reason, _In_ KPROCESSOR_MODE AccessMode, _In_opt_ PEPROCESS Process, _In_ PVOID ObjectBody, _Inout_ PACCESS_MASK GrantedAccess, _In_ ULONG HandleCount)
 
VOID NTAPI PspUserThreadStartup (IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
 
LONG PspUnhandledExceptionInSystemThread (PEXCEPTION_POINTERS ExceptionPointers)
 
VOID NTAPI PspSystemThreadStartup (IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
 
NTSTATUS NTAPI PspCreateThread (OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN HANDLE ProcessHandle, IN PEPROCESS TargetProcess, OUT PCLIENT_ID ClientId, IN PCONTEXT ThreadContext, IN PINITIAL_TEB InitialTeb, IN BOOLEAN CreateSuspended, IN PKSTART_ROUTINE StartRoutine OPTIONAL, IN PVOID StartContext OPTIONAL)
 
NTSTATUS NTAPI PsCreateSystemThread (OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE ProcessHandle, IN PCLIENT_ID ClientId, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
 
NTSTATUS NTAPI PsLookupThreadByThreadId (IN HANDLE ThreadId, OUT PETHREAD *Thread)
 
ULONG NTAPI PsGetThreadFreezeCount (IN PETHREAD Thread)
 
BOOLEAN NTAPI PsGetThreadHardErrorsAreDisabled (IN PETHREAD Thread)
 
HANDLE NTAPI PsGetThreadId (IN PETHREAD Thread)
 
HANDLE NTAPI PsGetCurrentThreadId (VOID)
 
PEPROCESS NTAPI PsGetThreadProcess (IN PETHREAD Thread)
 
PEPROCESS NTAPI PsGetCurrentThreadProcess (VOID)
 
HANDLE NTAPI PsGetThreadProcessId (IN PETHREAD Thread)
 
HANDLE NTAPI PsGetCurrentThreadProcessId (VOID)
 
ULONG NTAPI PsGetThreadSessionId (IN PETHREAD Thread)
 
PTEB NTAPI PsGetThreadTeb (IN PETHREAD Thread)
 
PVOID NTAPI PsGetCurrentThreadTeb (VOID)
 
PVOID NTAPI PsGetThreadWin32Thread (IN PETHREAD Thread)
 
PVOID NTAPI PsGetCurrentThreadWin32Thread (VOID)
 
PVOID NTAPI PsGetCurrentThreadWin32ThreadAndEnterCriticalRegion (_Out_ HANDLE *OutProcessId)
 
KPROCESSOR_MODE NTAPI PsGetCurrentThreadPreviousMode (VOID)
 
PVOID NTAPI PsGetCurrentThreadStackBase (VOID)
 
PVOID NTAPI PsGetCurrentThreadStackLimit (VOID)
 
BOOLEAN NTAPI PsIsThreadTerminating (IN PETHREAD Thread)
 
BOOLEAN NTAPI PsIsSystemThread (IN PETHREAD Thread)
 
BOOLEAN NTAPI PsIsThreadImpersonating (IN PETHREAD Thread)
 
VOID NTAPI PsSetThreadHardErrorsAreDisabled (IN PETHREAD Thread, IN BOOLEAN HardErrorsAreDisabled)
 
PVOID NTAPI PsSetThreadWin32Thread (_Inout_ PETHREAD Thread, _In_ PVOID Win32Thread, _In_ PVOID OldWin32Thread)
 
NTSTATUS NTAPI PsWrapApcWow64Thread (IN OUT PVOID *ApcContext, IN OUT PVOID *ApcRoutine)
 
NTSTATUS NTAPI NtCreateThread (OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN HANDLE ProcessHandle, OUT PCLIENT_ID ClientId, IN PCONTEXT ThreadContext, IN PINITIAL_TEB InitialTeb, IN BOOLEAN CreateSuspended)
 
NTSTATUS NTAPI NtOpenThread (OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN PCLIENT_ID ClientId OPTIONAL)
 

Variables

BOOLEAN CcPfEnablePrefetcher
 
ULONG MmReadClusterSize
 
POBJECT_TYPE PsThreadType = NULL
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file thread.c.

Function Documentation

◆ NtCreateThread()

NTSTATUS NTAPI NtCreateThread ( OUT PHANDLE  ThreadHandle,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes  OPTIONAL,
IN HANDLE  ProcessHandle,
OUT PCLIENT_ID  ClientId,
IN PCONTEXT  ThreadContext,
IN PINITIAL_TEB  InitialTeb,
IN BOOLEAN  CreateSuspended 
)

Definition at line 965 of file thread.c.

973{
974 INITIAL_TEB SafeInitialTeb;
975 PAGED_CODE();
977 "ProcessHandle: %p Context: %p\n", ProcessHandle, ThreadContext);
978
979 /* Check if this was from user-mode */
981 {
982 /* Make sure that we got a context */
983 if (!ThreadContext) return STATUS_INVALID_PARAMETER;
984
985 /* Protect checks */
987 {
988 /* Make sure the handle pointer we got is valid */
989 ProbeForWriteHandle(ThreadHandle);
990
991 /* Check if the caller wants a client id */
992 if (ClientId)
993 {
994 /* Make sure we can write to it */
995 ProbeForWrite(ClientId, sizeof(CLIENT_ID), sizeof(ULONG));
996 }
997
998 /* Make sure that the entire context is readable */
999 ProbeForRead(ThreadContext, sizeof(CONTEXT), sizeof(ULONG));
1000
1001 /* Check the Initial TEB */
1002 ProbeForRead(InitialTeb, sizeof(INITIAL_TEB), sizeof(ULONG));
1003 SafeInitialTeb = *InitialTeb;
1004 }
1006 {
1007 /* Return the exception code */
1009 }
1010 _SEH2_END;
1011 }
1012 else
1013 {
1014 /* Use the Initial TEB as is */
1015 SafeInitialTeb = *InitialTeb;
1016 }
1017
1018 /* Call the shared function */
1019 return PspCreateThread(ThreadHandle,
1023 NULL,
1024 ClientId,
1025 ThreadContext,
1026 &SafeInitialTeb,
1027 CreateSuspended,
1028 NULL,
1029 NULL);
1030}
#define PAGED_CODE()
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
#define KeGetPreviousMode()
Definition: ketypes.h:1115
#define KernelMode
Definition: asm.h:38
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:407
NTSTATUS NTAPI PspCreateThread(OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN HANDLE ProcessHandle, IN PEPROCESS TargetProcess, OUT PCLIENT_ID ClientId, IN PCONTEXT ThreadContext, IN PINITIAL_TEB InitialTeb, IN BOOLEAN CreateSuspended, IN PKSTART_ROUTINE StartRoutine OPTIONAL, IN PVOID StartContext OPTIONAL)
Definition: thread.c:195
#define PSTRACE(x, fmt,...)
Definition: ps.h:57
#define PS_THREAD_DEBUG
Definition: ps.h:17
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:204
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:104
#define _SEH2_END
Definition: pseh2_64.h:194
#define _SEH2_TRY
Definition: pseh2_64.h:93
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:207
#define ProbeForWriteHandle(Ptr)
Definition: probe.h:43
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_Must_inspect_result_ _In_ WDFDEVICE _In_ ULONG _In_ ACCESS_MASK DesiredAccess
Definition: wdfdevice.h:2664
_Out_ PCLIENT_ID ClientId
Definition: kefuncs.h:1151

◆ NtOpenThread()

NTSTATUS NTAPI NtOpenThread ( OUT PHANDLE  ThreadHandle,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES  ObjectAttributes,
IN PCLIENT_ID ClientId  OPTIONAL 
)

Definition at line 1037 of file thread.c.

1041{
1043 CLIENT_ID SafeClientId;
1044 ULONG Attributes = 0;
1048 BOOLEAN HasObjectName = FALSE;
1050 AUX_ACCESS_DATA AuxData;
1051 PAGED_CODE();
1053 "ClientId: %p ObjectAttributes: %p\n", ClientId, ObjectAttributes);
1054
1055 /* Check if we were called from user mode */
1056 if (PreviousMode != KernelMode)
1057 {
1058 /* Enter SEH for probing */
1059 _SEH2_TRY
1060 {
1061 /* Probe the thread handle */
1062 ProbeForWriteHandle(ThreadHandle);
1063
1064 /* Check for a CID structure */
1065 if (ClientId)
1066 {
1067 /* Probe and capture it */
1068 ProbeForRead(ClientId, sizeof(CLIENT_ID), sizeof(ULONG));
1069 SafeClientId = *ClientId;
1070 ClientId = &SafeClientId;
1071 }
1072
1073 /*
1074 * Just probe the object attributes structure, don't capture it
1075 * completely. This is done later if necessary
1076 */
1078 sizeof(OBJECT_ATTRIBUTES),
1079 sizeof(ULONG));
1080 HasObjectName = (ObjectAttributes->ObjectName != NULL);
1081
1082 /* Validate user attributes */
1084 }
1086 {
1087 /* Return the exception code */
1089 }
1090 _SEH2_END;
1091 }
1092 else
1093 {
1094 /* Otherwise just get the data directly */
1095 HasObjectName = (ObjectAttributes->ObjectName != NULL);
1096
1097 /* Still have to sanitize attributes */
1099 }
1100
1101 /* Can't pass both, fail */
1102 if ((HasObjectName) && (ClientId)) return STATUS_INVALID_PARAMETER_MIX;
1103
1104 /* Create an access state */
1106 &AuxData,
1109 if (!NT_SUCCESS(Status)) return Status;
1110
1111 /* Check if this is a debugger */
1113 {
1114 /* Did he want full access? */
1115 if (AccessState.RemainingDesiredAccess & MAXIMUM_ALLOWED)
1116 {
1117 /* Give it to him */
1118 AccessState.PreviouslyGrantedAccess |= THREAD_ALL_ACCESS;
1119 }
1120 else
1121 {
1122 /* Otherwise just give every other access he could want */
1123 AccessState.PreviouslyGrantedAccess |=
1124 AccessState.RemainingDesiredAccess;
1125 }
1126
1127 /* The caller desires nothing else now */
1128 AccessState.RemainingDesiredAccess = 0;
1129 }
1130
1131 /* Open by name if one was given */
1132 if (HasObjectName)
1133 {
1134 /* Open it */
1138 &AccessState,
1139 0,
1140 NULL,
1141 &hThread);
1142
1143 /* Get rid of the access state */
1145 }
1146 else if (ClientId)
1147 {
1148 /* Open by Thread ID */
1150 {
1151 /* Get the Process */
1153 }
1154 else
1155 {
1156 /* Get the Process */
1158 }
1159
1160 /* Check if we didn't find anything */
1161 if (!NT_SUCCESS(Status))
1162 {
1163 /* Get rid of the access state and return */
1165 return Status;
1166 }
1167
1168 /* Open the Thread Object */
1170 Attributes,
1171 &AccessState,
1172 0,
1175 &hThread);
1176
1177 /* Delete the access state and dereference the thread */
1180 }
1181 else
1182 {
1183 /* Neither an object name nor a client id was passed */
1185 }
1186
1187 /* Check for success */
1188 if (NT_SUCCESS(Status))
1189 {
1190 /* Protect against bad user-mode pointers */
1191 _SEH2_TRY
1192 {
1193 /* Write back the handle */
1194 *ThreadHandle = hThread;
1195 }
1197 {
1198 /* Get the exception code */
1200 }
1201 _SEH2_END;
1202 }
1203
1204 /* Return status */
1205 return Status;
1206}
_In_ PVOID _In_ ULONG _Out_ PVOID _In_ ULONG _Inout_ PULONG _In_ KPROCESSOR_MODE PreviousMode
unsigned char BOOLEAN
Definition: actypes.h:127
LONG NTSTATUS
Definition: precomp.h:26
_In_ ULONG Attributes
Definition: dispmprt.h:1077
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
Status
Definition: gdiplustypes.h:24
HANDLE hThread
Definition: wizard.c:28
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1342
#define MAXIMUM_ALLOWED
Definition: nt_native.h:83
const LUID SeDebugPrivilege
Definition: priv.c:39
NTSTATUS NTAPI PsLookupProcessThreadByCid(IN PCLIENT_ID Cid, OUT PEPROCESS *Process OPTIONAL, OUT PETHREAD *Thread)
Definition: process.c:1001
POBJECT_TYPE PsThreadType
Definition: thread.c:20
NTSTATUS NTAPI PsLookupThreadByThreadId(IN HANDLE ThreadId, OUT PETHREAD *Thread)
Definition: thread.c:667
VOID NTAPI SeDeleteAccessState(_In_ PACCESS_STATE AccessState)
Deletes an allocated access state from the memory.
Definition: access.c:150
NTSTATUS NTAPI SeCreateAccessState(_Out_ PACCESS_STATE AccessState, _Out_ __drv_aliasesMem PAUX_ACCESS_DATA AuxData, _In_ ACCESS_MASK Access, _In_ PGENERIC_MAPPING GenericMapping)
Creates an access state.
Definition: access.c:121
BOOLEAN NTAPI SeSinglePrivilegeCheck(_In_ LUID PrivilegeValue, _In_ KPROCESSOR_MODE PreviousMode)
Checks if a single privilege is present in the context of the calling thread.
Definition: priv.c:744
#define STATUS_INVALID_PARAMETER_MIX
Definition: ntstatus.h:378
FORCEINLINE ULONG ObpValidateAttributes(IN ULONG Attributes, IN KPROCESSOR_MODE PreviousMode)
Definition: ob_x.h:22
NTSTATUS NTAPI ObOpenObjectByPointer(IN PVOID Object, IN ULONG HandleAttributes, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PHANDLE Handle)
Definition: obhandle.c:2769
NTSTATUS NTAPI ObOpenObjectByName(IN POBJECT_ATTRIBUTES ObjectAttributes, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, IN PACCESS_STATE PassedAccessState, IN ACCESS_MASK DesiredAccess, IN OUT PVOID ParseContext, OUT PHANDLE Handle)
Definition: obhandle.c:2559
HANDLE UniqueThread
Definition: compat.h:826
HANDLE UniqueProcess
Definition: compat.h:825
GENERIC_MAPPING GenericMapping
Definition: obtypes.h:388
OBJECT_TYPE_INITIALIZER TypeInfo
Definition: obtypes.h:420
CCHAR KPROCESSOR_MODE
Definition: ketypes.h:7
#define ObDereferenceObject
Definition: obfuncs.h:203
_In_opt_ PVOID _In_opt_ PUNICODE_STRING _In_ PSECURITY_DESCRIPTOR _In_ PACCESS_STATE AccessState
Definition: sefuncs.h:417

Referenced by InitFunctionPtrs(), OpenThread(), PrintThreads(), Test_InfoClass_Access(), test_query_process(), and test_thread_lookup().

◆ PsCreateSystemThread()

NTSTATUS NTAPI PsCreateSystemThread ( OUT PHANDLE  ThreadHandle,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES  ObjectAttributes,
IN HANDLE  ProcessHandle,
IN PCLIENT_ID  ClientId,
IN PKSTART_ROUTINE  StartRoutine,
IN PVOID  StartContext 
)

Definition at line 626 of file thread.c.

633{
634 PEPROCESS TargetProcess = NULL;
636 PAGED_CODE();
638 "ProcessHandle: %p StartRoutine: %p StartContext: %p\n",
639 ProcessHandle, StartRoutine, StartContext);
640
641 /* Check if we have a handle. If not, use the System Process */
642 if (!ProcessHandle)
643 {
644 Handle = NULL;
645 TargetProcess = PsInitialSystemProcess;
646 }
647
648 /* Call the shared function */
649 return PspCreateThread(ThreadHandle,
652 Handle,
653 TargetProcess,
654 ClientId,
655 NULL,
656 NULL,
657 FALSE,
659 StartContext);
660}
ULONG Handle
Definition: gdb_input.c:15
PEPROCESS PsInitialSystemProcess
Definition: psmgr.c:50
_In_ ULONG _In_opt_ POBJECT_ATTRIBUTES _In_opt_ HANDLE _Out_opt_ PCLIENT_ID _In_ PKSTART_ROUTINE StartRoutine
Definition: psfuncs.h:92

Referenced by _Function_class_(), ACPIDispatchDeviceControl(), AcpiOsExecute(), AtaFdoCreatePortThread(), BehaviorChecks(), CmpInitializeHiveList(), create_calc_threads(), FxSystemThread::CreateThread(), DisplayBootBitmap(), DriverEntry(), ExpCreateWorkerThread(), ExpInitializeWorkerThreads(), Ext2StartReaper(), FsRtlInitializeWorkerThread(), InitializeDeviceData(), IntVideoPortInbvInitialize(), KdbSymInit(), KdpDebugLogInit(), KeyboardInternalDeviceControl(), KmtStartThread(), look_for_balance_item(), MiInitBalancerThread(), MmInitBsmThread(), mount_vol(), PageFileBehaviorChecks(), PopFlushVolumes(), PspInitPhase0(), remove_device(), resize_device(), RxInitializeDispatcher(), RxSpinUpWorkerThread(), ScrInbvInitialize(), send_subvol(), SeRmInitPhase1(), SermouseStartDevice(), start_balance(), start_scrub(), StartMemoryMonitor(), StartSocketDereferenceThread(), StartThread(), sys_thread_new(), SystemProcessTest(), TestEventConcurrent(), USBPORT_CreateWorkerThread(), VfdCreateDevice(), and WskSocket().

◆ PsGetCurrentThreadId()

HANDLE NTAPI PsGetCurrentThreadId ( VOID  )

Definition at line 739 of file thread.c.

740{
741 return PsGetCurrentThread()->Cid.UniqueThread;
742}
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81

◆ PsGetCurrentThreadPreviousMode()

KPROCESSOR_MODE NTAPI PsGetCurrentThreadPreviousMode ( VOID  )

Definition at line 862 of file thread.c.

863{
864 return (KPROCESSOR_MODE)PsGetCurrentThread()->Tcb.PreviousMode;
865}

◆ PsGetCurrentThreadProcess()

PEPROCESS NTAPI PsGetCurrentThreadProcess ( VOID  )

Definition at line 759 of file thread.c.

760{
761 return PsGetCurrentThread()->ThreadsProcess;
762}

◆ PsGetCurrentThreadProcessId()

HANDLE NTAPI PsGetCurrentThreadProcessId ( VOID  )

Definition at line 779 of file thread.c.

780{
781 return PsGetCurrentThread()->Cid.UniqueProcess;
782}

Referenced by intDdCreateDirectDrawLocal().

◆ PsGetCurrentThreadStackBase()

PVOID NTAPI PsGetCurrentThreadStackBase ( VOID  )

Definition at line 872 of file thread.c.

873{
874 return PsGetCurrentThread()->Tcb.StackBase;
875}

◆ PsGetCurrentThreadStackLimit()

PVOID NTAPI PsGetCurrentThreadStackLimit ( VOID  )

Definition at line 882 of file thread.c.

883{
884 return (PVOID)PsGetCurrentThread()->Tcb.StackLimit;
885}

◆ PsGetCurrentThreadTeb()

PVOID NTAPI PsGetCurrentThreadTeb ( VOID  )

Definition at line 809 of file thread.c.

810{
811 return PsGetCurrentThread()->Tcb.Teb;
812}

Referenced by FileOpenAddress().

◆ PsGetCurrentThreadWin32Thread()

PVOID NTAPI PsGetCurrentThreadWin32Thread ( VOID  )

Definition at line 829 of file thread.c.

830{
831 return PsGetCurrentThread()->Tcb.Win32Thread;
832}

Referenced by AllocateUserMessage(), CaretSystemTimerProc(), co_HOOK_CallHooks(), co_IntActivateKeyboardLayout(), co_IntCallHookProc(), co_IntCallSentMessageCallback(), co_IntCallWindowProc(), co_IntClientLoadLibrary(), co_IntClientThreadSetup(), co_IntCreateDefaultImeWindow(), co_IntFixCaret(), co_IntGetPeekMessage(), co_IntLoadDefaultCursors(), co_IntLoadSysMenuTemplate(), co_IntPaintWindows(), co_IntPeekMessage(), co_IntProcessKeyboardMessage(), co_IntProcessMouseMessage(), co_IntSendActivateMessages(), co_IntSendMessageTimeoutSingle(), co_IntSendMessageWithCallBack(), co_IntSetActiveWindow(), co_IntSetCaretPos(), co_IntSetForegroundAndFocusWindow(), co_IntWaitMessage(), co_MsqDispatchOneSentMessage(), co_MsqReplyMessage(), co_MsqSendMessage(), co_MsqSendMessageAsync(), co_UserDestroyWindow(), co_UserHideCaret(), co_UserSetCapture(), co_UserSetFocus(), co_UserShowCaret(), co_WinPosSearchChildren(), co_WinPosSetWindowPos(), co_WinPosShowWindow(), DECREASE_THREAD_LOCK_COUNT(), DefWndDoSizeMove(), DefWndStartSizeMove(), DesktopHeapGetUserDelta(), DesktopThreadMain(), GetW32ThreadInfo(), handle_internal_message(), IdlePing(), INCREASE_THREAD_LOCK_COUNT(), IntAddAtom(), IntCallWndProc(), IntCallWndProcRet(), IntCbAllocateMemory(), IntCbFreeMemory(), IntCreateDesktop(), IntDeactivateWindow(), IntDefWindowProc(), IntDeRegisterShellHookWindow(), IntDesktopOkToClose(), IntDestroyOwnedWindows(), IntDispatchMessage(), IntDrawScrollBar(), IntGetAndReferenceClass(), IntGetAtomName(), IntGetCapture(), IntGetCurrentThreadDesktopWindow(), IntGetImeCompatFlags(), IntGetNextHook(), IntGetQueueStatus(), IntGetThreadDesktopWindow(), IntGetThreadFocusWindow(), IntInitMessagePumpHook(), IntInvalidateWindows(), IntIsClipboardOpenByMe(), IntMsqClearWakeMask(), IntMsqSetWakeMask(), IntNotifyImeShowStatus(), IntNotifyWinEvent(), IntQueryTrackMouseEvent(), IntRegisterShellHookWindow(), IntReleaseCapture(), IntRemoveHook(), IntSendDestroyMsg(), IntSendSyncPaint(), IntSetThreadDesktop(), IntSetTimer(), IntTrackMouseEvent(), IntTrackPopupMenuEx(), IntTranslateKbdMessage(), IntUnhookWindowsHook(), IntUninitMessagePumpHook(), IntUserSetActiveWindow(), MENU_DoNextMenu(), MENU_InitTracking(), MENU_TrackMenu(), MsqGetMessageExtraInfo(), MsqSetMessageExtraInfo(), NtUserBlockInput(), NtUserCallNoParam(), NtUserCallOneParam(), NtUserCallTwoParam(), NtUserCreateAcceleratorTable(), NtUserCreateCaret(), NtUserGetAppImeLevel(), NtUserGetCaretPos(), NtUserGetGUIThreadInfo(), NtUserGetKeyboardLayoutName(), NtUserGetKeyboardState(), NtUserGetKeyNameText(), NtUserGetRawInputBuffer(), NtUserGetRawInputData(), NtUserGetThreadState(), NtUserLockWorkStation(), NtUserMapVirtualKeyEx(), NtUserQueryWindow(), NtUserSendInput(), NtUserSetAppImeLevel(), NtUserSetKeyboardState(), NtUserSetThreadState(), NtUserSetWindowsHookEx(), NtUserSetWinEventHook(), NtUserToUnicodeEx(), NtUserValidateTimerCallback(), NtUserVkKeyScanEx(), NtUserWaitForInputIdle(), PostTimerMessages(), UserCreateInputContext(), UserDbgAssertThreadInfo(), UserDerefObjectCo(), UserDestroyMenu(), UserEnterExclusive(), UserGetActiveWindow(), UserGetCPD(), UserGetKeyboardLayout(), UserGetKeyState(), UserGhostThreadEntry(), UserInitialize(), UserOpenClipboard(), UserOpenInputDesktop(), UserRefObjectCo(), UserRegisterHotKey(), UserRegisterUserApiHook(), UserSendKeyboardInput(), UserSendMouseInput(), UserSetActiveWindow(), UserSetCursor(), UserShowCursor(), and UserUnregisterUserApiHook().

◆ PsGetCurrentThreadWin32ThreadAndEnterCriticalRegion()

PVOID NTAPI PsGetCurrentThreadWin32ThreadAndEnterCriticalRegion ( _Out_ HANDLE *  OutProcessId)

Definition at line 839 of file thread.c.

841{
842 PETHREAD CurrentThread;
843
844 /* Get the current thread */
845 CurrentThread = PsGetCurrentThread();
846
847 /* Return the process id */
848 *OutProcessId = CurrentThread->Cid.UniqueProcess;
849
850 /* Enter critical region */
852
853 /* Return the win32 thread */
854 return CurrentThread->Tcb.Win32Thread;
855}
#define KeEnterCriticalRegion()
Definition: ke_x.h:88
KTHREAD Tcb
Definition: pstypes.h:1296
CLIENT_ID Cid
Definition: pstypes.h:1321
PVOID Win32Thread
Definition: ketypes.h:2013

◆ PsGetThreadFreezeCount()

ULONG NTAPI PsGetThreadFreezeCount ( IN PETHREAD  Thread)

Definition at line 709 of file thread.c.

710{
711 return Thread->Tcb.FreezeCount;
712}
ULONG FreezeCount
Definition: ketypes.h:1884

Referenced by IsThreadSuspended(), and MsqIsHung().

◆ PsGetThreadHardErrorsAreDisabled()

BOOLEAN NTAPI PsGetThreadHardErrorsAreDisabled ( IN PETHREAD  Thread)

Definition at line 719 of file thread.c.

720{
722}
#define TRUE
Definition: types.h:120
ULONG HardErrorsAreDisabled
Definition: pstypes.h:1376

Referenced by MountMgrMountedDeviceNotification(), and MountMgrVolumeArrivalNotification().

◆ PsGetThreadId()

◆ PsGetThreadProcess()

PEPROCESS NTAPI PsGetThreadProcess ( IN PETHREAD  Thread)

Definition at line 749 of file thread.c.

750{
751 return Thread->ThreadsProcess;
752}

◆ PsGetThreadProcessId()

◆ PsGetThreadSessionId()

ULONG NTAPI PsGetThreadSessionId ( IN PETHREAD  Thread)

Definition at line 789 of file thread.c.

790{
791 return MmGetSessionId(Thread->ThreadsProcess);
792}
ULONG NTAPI MmGetSessionId(IN PEPROCESS Process)
Definition: session.c:179

◆ PsGetThreadTeb()

PTEB NTAPI PsGetThreadTeb ( IN PETHREAD  Thread)

Definition at line 799 of file thread.c.

800{
801 return Thread->Tcb.Teb;
802}
PVOID Teb
Definition: ketypes.h:1954

◆ PsGetThreadWin32Thread()

PVOID NTAPI PsGetThreadWin32Thread ( IN PETHREAD  Thread)

◆ PsIsSystemThread()

BOOLEAN NTAPI PsIsSystemThread ( IN PETHREAD  Thread)

Definition at line 902 of file thread.c.

903{
904 return Thread->SystemThread ? TRUE: FALSE;
905}
ULONG SystemThread
Definition: pstypes.h:1375

Referenced by IntEnlistPowerCallout(), and IoIsSystemThread().

◆ PsIsThreadImpersonating()

BOOLEAN NTAPI PsIsThreadImpersonating ( IN PETHREAD  Thread)

Definition at line 912 of file thread.c.

913{
915}
ULONG ActiveImpersonationInfo
Definition: pstypes.h:1374

Referenced by NtCloseObjectAuditAlarm().

◆ PsIsThreadTerminating()

BOOLEAN NTAPI PsIsThreadTerminating ( IN PETHREAD  Thread)

Definition at line 892 of file thread.c.

893{
894 return Thread->Terminated ? TRUE : FALSE;
895}
ULONG Terminated
Definition: pstypes.h:1367

Referenced by IntTID2PTI(), NtQueryInformationThread(), ObpCloseHandle(), and RequestSynchronizeProcessWithSerialQueue().

◆ PsLookupThreadByThreadId()

NTSTATUS NTAPI PsLookupThreadByThreadId ( IN HANDLE  ThreadId,
OUT PETHREAD *  Thread 
)

Definition at line 667 of file thread.c.

669{
670 PHANDLE_TABLE_ENTRY CidEntry;
671 PETHREAD FoundThread;
673 PAGED_CODE();
674 PSTRACE(PS_THREAD_DEBUG, "ThreadId: %p\n", ThreadId);
676
677 /* Get the CID Handle Entry */
678 CidEntry = ExMapHandleToPointer(PspCidTable, ThreadId);
679 if (CidEntry)
680 {
681 /* Get the Thread */
682 FoundThread = CidEntry->Object;
683
684 /* Make sure it's really a thread */
685 if (FoundThread->Tcb.Header.Type == ThreadObject)
686 {
687 /* Safe Reference and return it */
688 if (ObReferenceObjectSafe(FoundThread))
689 {
690 *Thread = FoundThread;
692 }
693 }
694
695 /* Unlock the Entry */
697 }
698
699 /* Return to caller */
701 return Status;
702}
#define KeLeaveCriticalRegion()
Definition: ke_x.h:119
@ ThreadObject
Definition: ketypes.h:431
PHANDLE_TABLE_ENTRY NTAPI ExMapHandleToPointer(IN PHANDLE_TABLE HandleTable, IN HANDLE Handle)
Definition: handle.c:1046
VOID NTAPI ExUnlockHandleTableEntry(IN PHANDLE_TABLE HandleTable, IN PHANDLE_TABLE_ENTRY HandleTableEntry)
Definition: handle.c:923
BOOLEAN FASTCALL ObReferenceObjectSafe(IN PVOID Object)
Definition: obref.c:22
PHANDLE_TABLE PspCidTable
Definition: psmgr.c:48
#define STATUS_SUCCESS
Definition: shellext.h:65
Definition: extypes.h:772
PVOID Object
Definition: extypes.h:775
DISPATCHER_HEADER Header
Definition: ketypes.h:1808

Referenced by IntTID2PTI(), KdbpAttachToThread(), KdbpCmdThread(), NtOpenThread(), NtUserBuildHwndList(), NtUserPostThreadMessage(), and NtUserSetWinEventHook().

◆ PspCreateThread()

NTSTATUS NTAPI PspCreateThread ( OUT PHANDLE  ThreadHandle,
IN ACCESS_MASK  DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes  OPTIONAL,
IN HANDLE  ProcessHandle,
IN PEPROCESS  TargetProcess,
OUT PCLIENT_ID  ClientId,
IN PCONTEXT  ThreadContext,
IN PINITIAL_TEB  InitialTeb,
IN BOOLEAN  CreateSuspended,
IN PKSTART_ROUTINE StartRoutine  OPTIONAL,
IN PVOID StartContext  OPTIONAL 
)

Definition at line 195 of file thread.c.

206{
210 PTEB TebBase = NULL;
213 HANDLE_TABLE_ENTRY CidEntry;
214 ACCESS_STATE LocalAccessState;
215 PACCESS_STATE AccessState = &LocalAccessState;
216 AUX_ACCESS_DATA AuxData;
217 BOOLEAN Result, SdAllocated;
220 PAGED_CODE();
222 "ThreadContext: %p TargetProcess: %p ProcessHandle: %p\n",
223 ThreadContext, TargetProcess, ProcessHandle);
224
225 /* If we were called from PsCreateSystemThread, then we're kernel mode */
227
228 /* Reference the Process by handle or pointer, depending on what we got */
229 if (ProcessHandle)
230 {
231 /* Normal thread or System Thread */
236 (PVOID*)&Process,
237 NULL);
239 }
240 else
241 {
242 /* System thread inside System Process, or Normal Thread with a bug */
243 if (StartRoutine)
244 {
245 /* Reference the Process by Pointer */
246 ObReferenceObject(TargetProcess);
247 Process = TargetProcess;
249 }
250 else
251 {
252 /* Fake ObReference returning this */
254 }
255 }
256
257 /* Check for success */
258 if (!NT_SUCCESS(Status)) return Status;
259
260 /* Also make sure that User-Mode isn't trying to create a system thread */
262 {
263 /* Fail */
266 }
267
268 /* Create Thread Object */
273 NULL,
274 sizeof(ETHREAD),
275 0,
276 0,
277 (PVOID*)&Thread);
278 if (!NT_SUCCESS(Status))
279 {
280 /* We failed; dereference the process and exit */
282 return Status;
283 }
284
285 /* Zero the Object entirely */
286 RtlZeroMemory(Thread, sizeof(ETHREAD));
287
288 /* Initialize rundown protection */
290
291 /* Initialize exit code */
293
294 /* Set the Process CID */
295 Thread->ThreadsProcess = Process;
296 Thread->Cid.UniqueProcess = Process->UniqueProcessId;
297
298 /* Create Cid Handle */
299 CidEntry.Object = Thread;
300 CidEntry.GrantedAccess = 0;
302 if (!Thread->Cid.UniqueThread)
303 {
304 /* We couldn't create the CID, dereference the thread and fail */
307 }
308
309 /* Save the read cluster size */
311
312 /* Initialize the LPC Reply Semaphore */
313 KeInitializeSemaphore(&Thread->LpcReplySemaphore, 0, 1);
314
315 /* Initialize the list heads and locks */
321
322 /* Acquire rundown protection */
323 if (!ExAcquireRundownProtection (&Process->RundownProtect))
324 {
325 /* Fail */
328 }
329
330 /* Now let the kernel initialize the context */
331 if (ThreadContext)
332 {
333 /* User-mode Thread, create Teb */
334 Status = MmCreateTeb(Process, &Thread->Cid, InitialTeb, &TebBase);
335 if (!NT_SUCCESS(Status))
336 {
337 /* Failed to create the TEB. Release rundown and dereference */
338 ExReleaseRundownProtection(&Process->RundownProtect);
340 return Status;
341 }
342
343 /* Set the Start Addresses from the untrusted ThreadContext */
345 {
346 Thread->StartAddress = (PVOID)KeGetContextPc(ThreadContext);
348 }
350 {
352 }
353 _SEH2_END;
354
355 /* Let the kernel intialize the Thread */
356 if (NT_SUCCESS(Status))
357 {
359 NULL,
361 NULL,
363 ThreadContext,
364 TebBase,
365 &Process->Pcb);
366 }
367 }
368 else
369 {
370 /* System Thread */
373
374 /* Let the kernel intialize the Thread */
376 NULL,
379 StartContext,
380 NULL,
381 NULL,
382 &Process->Pcb);
383 }
384
385 /* Check if we failed */
386 if (!NT_SUCCESS(Status))
387 {
388 /* Delete the TEB if we had done */
389 if (TebBase) MmDeleteTeb(Process, TebBase);
390
391 /* Release rundown and dereference */
392 ExReleaseRundownProtection(&Process->RundownProtect);
394 return Status;
395 }
396
397 /* Lock the process */
399 ExAcquirePushLockExclusive(&Process->ProcessLock);
400
401 /* Make sure the process didn't just die on us */
402 if (Process->ProcessDelete) goto Quickie;
403
404 /* Check if the thread was ours, terminated and it was user mode */
405 if ((Thread->Terminated) &&
406 (ThreadContext) &&
407 (Thread->ThreadsProcess == Process))
408 {
409 /* Cleanup, we don't want to start it up and context switch */
410 goto Quickie;
411 }
412
413 /*
414 * Insert the Thread into the Process's Thread List
415 * Note, this is the ETHREAD Thread List. It is removed in
416 * ps/kill.c!PspExitThread.
417 */
418 InsertTailList(&Process->ThreadListHead, &Thread->ThreadListEntry);
419 Process->ActiveThreads++;
420
421 /* Start the thread */
423
424 /* Release the process lock */
425 ExReleasePushLockExclusive(&Process->ProcessLock);
427
428 /* Release rundown */
429 ExReleaseRundownProtection(&Process->RundownProtect);
430
431 /* Notify WMI */
432 //WmiTraceProcess(Process, TRUE);
433 //WmiTraceThread(Thread, InitialTeb, TRUE);
434
435 /* Notify Thread Creation */
437
438 /* Reference ourselves as a keep-alive */
440
441 /* Suspend the Thread if we have to */
442 if (CreateSuspended) KeSuspendThread(&Thread->Tcb);
443
444 /* Check if we were already terminated */
446
447 /* Create an access state */
449 ThreadContext ?
451 &LocalAccessState,
452 &AuxData,
455 if (!NT_SUCCESS(Status))
456 {
457 /* Access state failed, thread is dead */
459
460 /* If we were suspended, wake it up */
461 if (CreateSuspended) KeResumeThread(&Thread->Tcb);
462
463 /* Dispatch thread */
465
466 /* Dereference completely to kill it */
468 return Status;
469 }
470
471 /* Insert the Thread into the Object Manager */
475 0,
476 NULL,
477 &hThread);
478
479 /* Delete the access state if we had one */
481
482 /* Check for success */
483 if (NT_SUCCESS(Status))
484 {
485 /* Wrap in SEH to protect against bad user-mode pointers */
487 {
488 /* Return Cid and Handle */
489 if (ClientId) *ClientId = Thread->Cid;
490 *ThreadHandle = hThread;
491 }
493 {
494 /* Thread insertion failed, thread is dead */
496
497 /* If we were suspended, wake it up */
498 if (CreateSuspended) KeResumeThread(&Thread->Tcb);
499
500 /* Dispatch thread */
502
503 /* Dereference it, leaving only the keep-alive */
505
506 /* Close its handle, killing it */
508
509 /* Return the exception code */
511 }
512 _SEH2_END;
513 }
514 else
515 {
516 /* Thread insertion failed, thread is dead */
518
519 /* If we were suspended, wake it up */
520 if (CreateSuspended) KeResumeThread(&Thread->Tcb);
521 }
522
523 /* Get the create time */
525 ASSERT(!(Thread->CreateTime.HighPart & 0xF0000000));
526
527 /* Make sure the thread isn't dead */
528 if (!Thread->DeadThread)
529 {
530 /* Get the thread's SD */
533 &SdAllocated);
534 if (!NT_SUCCESS(Status))
535 {
536 /* Thread insertion failed, thread is dead */
538
539 /* If we were suspended, wake it up */
540 if (CreateSuspended) KeResumeThread(&Thread->Tcb);
541
542 /* Dispatch thread */
544
545 /* Dereference it, leaving only the keep-alive */
547
548 /* Close its handle, killing it */
550 return Status;
551 }
552
553 /* Create the subject context */
554 SubjectContext.ProcessAuditId = Process;
556 SubjectContext.ClientToken = NULL;
557
558 /* Do the access check */
561 FALSE,
563 0,
564 NULL,
567 &Thread->GrantedAccess,
568 &AccessStatus);
569
570 /* Dereference the token and let go the SD */
572 SubjectContext.PrimaryToken);
574
575 /* Remove access if it failed */
576 if (!Result) Process->GrantedAccess = 0;
577
578 /* Set at least some minimum access */
579 Thread->GrantedAccess |= (THREAD_TERMINATE |
583 }
584 else
585 {
586 /* Set the thread access mask to maximum */
587 Thread->GrantedAccess = THREAD_ALL_ACCESS;
588 }
589
590 /* Dispatch thread */
592
593 /* Dereference it, leaving only the keep-alive */
595
596 /* Return */
597 return Status;
598
599 /* Most annoying failure case ever, where we undo almost all manually */
600Quickie:
601 /* When we get here, the process is locked, unlock it */
602 ExReleasePushLockExclusive(&Process->ProcessLock);
604
605 /* Uninitialize it */
607
608 /* If we had a TEB, delete it */
609 if (TebBase) MmDeleteTeb(Process, TebBase);
610
611 /* Release rundown protection, which we also hold */
612 ExReleaseRundownProtection(&Process->RundownProtect);
613
614 /* Dereference the thread and return failure */
617}
BOOLEAN NTAPI SeAccessCheck(_In_ PSECURITY_DESCRIPTOR SecurityDescriptor, _In_ PSECURITY_SUBJECT_CONTEXT SubjectSecurityContext, _In_ BOOLEAN SubjectContextLocked, _In_ ACCESS_MASK DesiredAccess, _In_ ACCESS_MASK PreviouslyGrantedAccess, _Out_ PPRIVILEGE_SET *Privileges, _In_ PGENERIC_MAPPING GenericMapping, _In_ KPROCESSOR_MODE AccessMode, _Out_ PACCESS_MASK GrantedAccess, _Out_ PNTSTATUS AccessStatus)
Determines whether security access rights can be given to an object depending on the security descrip...
Definition: accesschk.c:1994
#define STATUS_INVALID_HANDLE
Definition: d3dkmdt.h:40
#define InsertTailList(ListHead, Entry)
#define KeQuerySystemTime(t)
Definition: env_spec_w32.h:570
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
#define KeInitializeSpinLock(sl)
Definition: env_spec_w32.h:604
#define ExReleaseRundownProtection
Definition: ex.h:139
#define ExGetPreviousMode
Definition: ex.h:143
#define ExInitializeRundownProtection
Definition: ex.h:140
FORCEINLINE VOID ExAcquirePushLockExclusive(PEX_PUSH_LOCK PushLock)
Definition: ex.h:1039
FORCEINLINE VOID ExReleasePushLockExclusive(PEX_PUSH_LOCK PushLock)
Definition: ex.h:1255
#define ExAcquireRundownProtection
Definition: ex.h:138
_Inout_ PLIST_ENTRY _In_ PVOID _In_ PSTRING _In_ BOOLEAN _In_ BOOLEAN _In_ ULONG _In_ PFLT_CALLBACK_DATA _In_opt_ PCHECK_FOR_TRAVERSE_ACCESS _In_opt_ PSECURITY_SUBJECT_CONTEXT SubjectContext
Definition: fltkernel.h:2246
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
#define PROCESS_CREATE_THREAD
Definition: pstypes.h:154
#define THREAD_QUERY_INFORMATION
Definition: pstypes.h:145
#define CT_DEAD_THREAD_BIT
Definition: pstypes.h:265
#define CT_SYSTEM_THREAD_BIT
Definition: pstypes.h:268
#define ASSERT(a)
Definition: mode.c:44
#define THREAD_QUERY_LIMITED_INFORMATION
Definition: security.c:76
#define THREAD_TERMINATE
Definition: nt_native.h:1339
#define THREAD_SET_INFORMATION
Definition: nt_native.h:1340
HANDLE NTAPI ExCreateHandle(IN PHANDLE_TABLE HandleTable, IN PHANDLE_TABLE_ENTRY HandleTableEntry)
Definition: handle.c:827
#define KeGetContextPc(Context)
Definition: ke.h:31
#define KeGetContextReturnRegister(Context)
Definition: ke.h:40
NTSTATUS NTAPI KeInitThread(IN OUT PKTHREAD Thread, IN PVOID KernelStack, IN PKSYSTEM_ROUTINE SystemRoutine, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext, IN PCONTEXT Context, IN PVOID Teb, IN PKPROCESS Process)
Definition: thrdobj.c:756
ULONG NTAPI KeForceResumeThread(IN PKTHREAD Thread)
Definition: thrdobj.c:267
VOID NTAPI KeUninitThread(IN PKTHREAD Thread)
Definition: thrdobj.c:917
ULONG NTAPI KeSuspendThread(PKTHREAD Thread)
Definition: thrdobj.c:601
ULONG NTAPI KeResumeThread(IN PKTHREAD Thread)
Definition: thrdobj.c:388
VOID NTAPI KeStartThread(IN OUT PKTHREAD Thread)
Definition: thrdobj.c:489
VOID NTAPI KeReadyThread(IN PKTHREAD Thread)
Definition: thrdobj.c:106
NTSTATUS NTAPI MmCreateTeb(IN PEPROCESS Process, IN PCLIENT_ID ClientId, IN PINITIAL_TEB InitialTeb, OUT PTEB *BaseTeb)
Definition: procsup.c:809
VOID NTAPI MmDeleteTeb(struct _EPROCESS *Process, PTEB Teb)
NTSTATUS NTAPI SeCreateAccessStateEx(_In_ PETHREAD Thread, _In_ PEPROCESS Process, _In_ OUT PACCESS_STATE AccessState, _In_ PAUX_ACCESS_DATA AuxData, _In_ ACCESS_MASK Access, _In_ PGENERIC_MAPPING GenericMapping)
POBJECT_TYPE PsProcessType
Definition: process.c:20
PACCESS_TOKEN NTAPI PsReferencePrimaryToken(PEPROCESS Process)
Definition: security.c:440
VOID NTAPI PspUserThreadStartup(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
Definition: thread.c:49
ULONG MmReadClusterSize
Definition: mminit.c:21
VOID NTAPI PspSystemThreadStartup(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
Definition: thread.c:162
#define STATUS_PROCESS_IS_TERMINATING
Definition: ntstatus.h:596
LONG FASTCALL ObDereferenceObjectEx(IN PVOID Object, IN LONG Count)
Definition: obref.c:88
LONG FASTCALL ObReferenceObjectEx(IN PVOID Object, IN LONG Count)
Definition: obref.c:77
VOID FASTCALL ObFastDereferenceObject(IN PEX_FAST_REF FastRef, IN PVOID Object)
Definition: obref.c:167
NTSTATUS NTAPI ObCloseHandle(IN HANDLE Handle, IN KPROCESSOR_MODE AccessMode)
Definition: obhandle.c:3411
NTSTATUS NTAPI ObInsertObject(IN PVOID Object, IN PACCESS_STATE AccessState OPTIONAL, IN ACCESS_MASK DesiredAccess, IN ULONG ObjectPointerBias, OUT PVOID *NewObject OPTIONAL, OUT PHANDLE Handle)
Definition: obhandle.c:2962
NTSTATUS NTAPI ObCreateObject(IN KPROCESSOR_MODE ProbeMode OPTIONAL, IN POBJECT_TYPE Type, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN KPROCESSOR_MODE AccessMode, IN OUT PVOID ParseContext OPTIONAL, IN ULONG ObjectSize, IN ULONG PagedPoolCharge OPTIONAL, IN ULONG NonPagedPoolCharge OPTIONAL, OUT PVOID *Object)
Definition: oblife.c:1049
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:493
NTSTATUS NTAPI ObGetObjectSecurity(IN PVOID Object, OUT PSECURITY_DESCRIPTOR *SecurityDescriptor, OUT PBOOLEAN MemoryAllocated)
Definition: obsecure.c:612
VOID NTAPI ObReleaseObjectSecurity(IN PSECURITY_DESCRIPTOR SecurityDescriptor, IN BOOLEAN MemoryAllocated)
Definition: obsecure.c:712
#define PSREFTRACE(x)
Definition: ps.h:58
#define PspSetCrossThreadFlag(Thread, Flag)
Definition: ps_x.h:25
FORCEINLINE VOID PspRunCreateThreadNotifyRoutines(IN PETHREAD CurrentThread, IN BOOLEAN Create)
Definition: ps_x.h:40
VOID NTAPI KeInitializeSemaphore(IN PKSEMAPHORE Semaphore, IN LONG Count, IN LONG Limit)
Definition: semphobj.c:22
PKSTART_ROUTINE StartAddress
Definition: pstypes.h:1348
NTSTATUS ExitStatus
Definition: pstypes.h:1306
EX_RUNDOWN_REF RundownProtect
Definition: pstypes.h:1352
ULONG ReadClusterSize
Definition: pstypes.h:1357
PVOID Win32StartAddress
Definition: pstypes.h:1345
LIST_ENTRY IrpList
Definition: pstypes.h:1337
LIST_ENTRY ActiveTimerListHead
Definition: pstypes.h:1320
KSPIN_LOCK ActiveTimerListLock
Definition: pstypes.h:1319
LARGE_INTEGER CreateTime
Definition: pstypes.h:1297
LIST_ENTRY ThreadListEntry
Definition: pstypes.h:1351
LIST_ENTRY LpcReplyChain
Definition: pstypes.h:1301
LIST_ENTRY PostBlockList
Definition: pstypes.h:1309
ULONG GrantedAccess
Definition: extypes.h:782
Definition: compat.h:836
#define STATUS_PENDING
Definition: telnetd.h:14
void * PVOID
Definition: typedefs.h:50
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_In_ USHORT _In_ ULONG _In_ PSOCKADDR _In_ PSOCKADDR _Reserved_ ULONG _In_opt_ PVOID _In_opt_ const WSK_CLIENT_CONNECTION_DISPATCH _In_opt_ PEPROCESS _In_opt_ PETHREAD _In_opt_ PSECURITY_DESCRIPTOR SecurityDescriptor
Definition: wsk.h:191
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:409
#define ObReferenceObject
Definition: obfuncs.h:204
#define PsGetCurrentProcess
Definition: psfuncs.h:17
_In_ PSECURITY_SUBJECT_CONTEXT _In_ BOOLEAN _In_ ACCESS_MASK _In_ ACCESS_MASK _Outptr_opt_ PPRIVILEGE_SET _In_ PGENERIC_MAPPING _In_ KPROCESSOR_MODE _Out_ PACCESS_MASK _Out_ PNTSTATUS AccessStatus
Definition: sefuncs.h:21

Referenced by NtCreateThread(), and PsCreateSystemThread().

◆ PspSystemThreadStartup()

VOID NTAPI PspSystemThreadStartup ( IN PKSTART_ROUTINE  StartRoutine,
IN PVOID  StartContext 
)

Definition at line 162 of file thread.c.

164{
167 "StartRoutine: %p StartContext: %p\n", StartRoutine, StartContext);
168
169 /* Unlock the dispatcher Database */
172
173 /* Make sure the thread isn't gone */
175 {
176 if (!(Thread->Terminated) && !(Thread->DeadThread))
177 {
178 /* Call the Start Routine */
179 StartRoutine(StartContext);
180 }
181 }
183 {
184 /* Bugcheck if we got here */
185 KeBugCheck(KMODE_EXCEPTION_NOT_HANDLED);
186 }
187 _SEH2_END;
188
189 /* Exit the thread */
191}
DECLSPEC_NORETURN VOID NTAPI KeBugCheck(ULONG BugCheckCode)
Definition: bug.c:1434
#define PASSIVE_LEVEL
Definition: env_spec_w32.h:693
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
LONG PspUnhandledExceptionInSystemThread(PEXCEPTION_POINTERS ExceptionPointers)
Definition: thread.c:138
NTSTATUS NTAPI PspTerminateThreadByPointer(IN PETHREAD Thread, IN NTSTATUS ExitStatus, IN BOOLEAN bSelf)
Definition: kill.c:1012
#define _SEH2_GetExceptionInformation()
Definition: pseh2_64.h:203

Referenced by PspCreateThread().

◆ PspThreadOpen()

NTSTATUS NTAPI PspThreadOpen ( _In_ OB_OPEN_REASON  Reason,
_In_ KPROCESSOR_MODE  AccessMode,
_In_opt_ PEPROCESS  Process,
_In_ PVOID  ObjectBody,
_Inout_ PACCESS_MASK  GrantedAccess,
_In_ ULONG  HandleCount 
)

Definition at line 26 of file thread.c.

33{
35 {
37 }
38
40 {
42 }
43
44 return STATUS_SUCCESS;
45}
#define THREAD_SET_LIMITED_INFORMATION
Definition: security.c:75
_In_ PSECURITY_SUBJECT_CONTEXT _In_ BOOLEAN _In_ ACCESS_MASK _In_ ACCESS_MASK _Outptr_opt_ PPRIVILEGE_SET _In_ PGENERIC_MAPPING _In_ KPROCESSOR_MODE _Out_ PACCESS_MASK GrantedAccess
Definition: sefuncs.h:20

Referenced by PspInitPhase0().

◆ PspUnhandledExceptionInSystemThread()

LONG PspUnhandledExceptionInSystemThread ( PEXCEPTION_POINTERS  ExceptionPointers)

Definition at line 138 of file thread.c.

139{
140 /* Print debugging information */
141 DPRINT1("PS: Unhandled Kernel Mode Exception Pointers = 0x%p\n",
142 ExceptionPointers);
143 DPRINT1("Code %x Addr %p Info0 %p Info1 %p Info2 %p Info3 %p\n",
144 ExceptionPointers->ExceptionRecord->ExceptionCode,
145 ExceptionPointers->ExceptionRecord->ExceptionAddress,
146 ExceptionPointers->ExceptionRecord->ExceptionInformation[0],
147 ExceptionPointers->ExceptionRecord->ExceptionInformation[1],
148 ExceptionPointers->ExceptionRecord->ExceptionInformation[2],
149 ExceptionPointers->ExceptionRecord->ExceptionInformation[3]);
150
151 /* Bugcheck the system */
152 KeBugCheckEx(SYSTEM_THREAD_EXCEPTION_NOT_HANDLED,
153 ExceptionPointers->ExceptionRecord->ExceptionCode,
154 (ULONG_PTR)ExceptionPointers->ExceptionRecord->ExceptionAddress,
155 (ULONG_PTR)ExceptionPointers->ExceptionRecord,
156 (ULONG_PTR)ExceptionPointers->ContextRecord);
157 return 0;
158}
#define DPRINT1
Definition: precomp.h:8
DECLSPEC_NORETURN VOID NTAPI KeBugCheckEx(IN ULONG BugCheckCode, IN ULONG_PTR BugCheckParameter1, IN ULONG_PTR BugCheckParameter2, IN ULONG_PTR BugCheckParameter3, IN ULONG_PTR BugCheckParameter4)
Definition: debug.c:485
PEXCEPTION_RECORD ExceptionRecord
Definition: rtltypes.h:200
PCONTEXT ContextRecord
Definition: rtltypes.h:201
DWORD ExceptionCode
Definition: compat.h:208
ULONG_PTR ExceptionInformation[EXCEPTION_MAXIMUM_PARAMETERS]
Definition: compat.h:213
PVOID ExceptionAddress
Definition: compat.h:211
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by PspSystemThreadStartup().

◆ PspUserThreadStartup()

VOID NTAPI PspUserThreadStartup ( IN PKSTART_ROUTINE  StartRoutine,
IN PVOID  StartContext 
)

Definition at line 49 of file thread.c.

51{
53 PTEB Teb;
54 BOOLEAN DeadThread = FALSE;
56 PAGED_CODE();
58 "StartRoutine: %p StartContext: %p\n", StartRoutine, StartContext);
59
60 /* Go to Passive Level */
63
64 /* Check if the thread is dead */
65 if (Thread->DeadThread)
66 {
67 /* Remember that we're dead */
68 DeadThread = TRUE;
69 }
70 else
71 {
72 /* Get the Locale ID and save Preferred Proc */
73 Teb = NtCurrentTeb();
75 Teb->IdealProcessor = Thread->Tcb.IdealProcessor;
76 }
77
78 /* Check if this is a dead thread, or if we're hiding */
79 if (!(Thread->DeadThread) && !(Thread->HideFromDebugger))
80 {
81 /* We're not, so notify the debugger */
82 DbgkCreateThread(Thread, StartContext);
83 }
84
85 /* Make sure we're not already dead */
86 if (!DeadThread)
87 {
88 /* Check if the Prefetcher is enabled */
90 {
91 /* FIXME: Prepare to prefetch this process */
92 }
93
94 /* Raise to APC */
96
97 /* Queue the User APC */
101 NULL,
103 NULL);
104
105 /* Lower it back to passive */
107 }
108 else
109 {
110 /* We're dead, kill us now */
113 TRUE);
114 }
115
116 /* Do we have a cookie set yet? */
117 while (!SharedUserData->Cookie)
118 {
119 LARGE_INTEGER SystemTime;
120 ULONG NewCookie;
121 PKPRCB Prcb;
122
123 /* Generate a new cookie */
124 KeQuerySystemTime(&SystemTime);
125 Prcb = KeGetCurrentPrcb();
126 NewCookie = (Prcb->MmPageFaultCount ^ Prcb->InterruptTime ^
127 SystemTime.u.LowPart ^ SystemTime.u.HighPart ^
128 (ULONG)(ULONG_PTR)&SystemTime);
129
130 /* Set the new cookie*/
132 NewCookie,
133 0);
134 }
135}
VOID NTAPI DbgkCreateThread(IN PETHREAD Thread, IN PVOID StartAddress)
Definition: dbgkutil.c:87
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define KeRaiseIrql(irql, oldIrql)
Definition: env_spec_w32.h:597
#define APC_LEVEL
Definition: env_spec_w32.h:695
#define InterlockedCompareExchange
Definition: interlocked.h:119
#define NtCurrentTeb
static ULONG
Definition: thread.c:81
FORCEINLINE struct _KPRCB * KeGetCurrentPrcb(VOID)
Definition: ketypes.h:1197
#define KeGetExceptionFrame(Thread)
Definition: ke.h:235
#define KeGetTrapFrame(Thread)
Definition: ke.h:218
VOID NTAPI KiInitializeUserApc(IN PKEXCEPTION_FRAME Reserved, IN PKTRAP_FRAME TrapFrame, IN PKNORMAL_ROUTINE NormalRoutine, IN PVOID NormalContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
Definition: usercall.c:266
ULONG NTAPI MmGetSessionLocaleId(VOID)
Definition: session.c:56
BOOLEAN CcPfEnablePrefetcher
Definition: cacheman.c:17
#define STATUS_THREAD_IS_TERMINATING
Definition: ntstatus.h:405
long LONG
Definition: pedump.c:60
PVOID PspSystemDllEntryPoint
Definition: psmgr.c:43
PVOID PspSystemDllBase
Definition: psmgr.c:41
#define SharedUserData
ULONG HideFromDebugger
Definition: pstypes.h:1373
ULONG InterruptTime
Definition: ketypes.h:840
LONG MmPageFaultCount
Definition: ketypes.h:733
ULONG IdealProcessor
Definition: ketypes.h:2091
ULONG CurrentLocale
Definition: compat.h:849
struct _LARGE_INTEGER::@2753 u
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778

Referenced by PspCreateThread().

◆ PsSetThreadHardErrorsAreDisabled()

VOID NTAPI PsSetThreadHardErrorsAreDisabled ( IN PETHREAD  Thread,
IN BOOLEAN  HardErrorsAreDisabled 
)

Definition at line 922 of file thread.c.

924{
925 Thread->HardErrorsAreDisabled = HardErrorsAreDisabled;
926}

Referenced by MountMgrMountedDeviceNotification(), and MountMgrVolumeArrivalNotification().

◆ PsSetThreadWin32Thread()

PVOID NTAPI PsSetThreadWin32Thread ( _Inout_ PETHREAD  Thread,
_In_ PVOID  Win32Thread,
_In_ PVOID  OldWin32Thread 
)

Definition at line 933 of file thread.c.

937{
938 /* Are we setting the win32 process? */
939 if (Win32Thread != NULL)
940 {
941 /* Just exchange it */
943 Win32Thread);
944 }
945 else
946 {
947 /* We are resetting, only exchange when the old win32 thread matches */
949 Win32Thread,
950 OldWin32Thread);
951 }
952}
#define InterlockedExchangePointer(Target, Value)
Definition: dshow.h:45
#define InterlockedCompareExchangePointer
Definition: interlocked.h:144

Referenced by AllocW32Thread(), and ExitThreadCallback().

◆ PsWrapApcWow64Thread()

NTSTATUS NTAPI PsWrapApcWow64Thread ( IN OUT PVOID *  ApcContext,
IN OUT PVOID *  ApcRoutine 
)

Definition at line 956 of file thread.c.

958{
961}
#define UNIMPLEMENTED
Definition: ntoskrnl.c:15
#define STATUS_NOT_IMPLEMENTED
Definition: d3dkmdt.h:42

Variable Documentation

◆ CcPfEnablePrefetcher

BOOLEAN CcPfEnablePrefetcher
extern

Definition at line 17 of file cacheman.c.

Referenced by PspUserThreadStartup().

◆ MmReadClusterSize

ULONG MmReadClusterSize
extern

Definition at line 21 of file mminit.c.

Referenced by PspCreateThread().

◆ PsThreadType