My Project
Loading...
Searching...
No Matches
ps.h
Go to the documentation of this file.
1#ifndef X86_MATANEL_PROCESS_H
2#define X86_MATANEL_PROCESS_H
3
4/*++
5
6Module Name:
7
8 mp.h
9
10Purpose:
11
12 This module contains the header files required for process and thread management.
13
14Author:
15
16 slep (Matanel) 2025.
17
18Revision History:
19
20--*/
21
22// Base includes
23#include <stdint.h>
24#include <stddef.h>
25
26// Other file includes
27#include "me.h"
28#include "ht.h"
29#include "ob.h"
30#include "core.h"
31
32// Exception Includes
33#include "exception.h"
34
35// ------------------ ENUMERATORS ------------------
36
45
46typedef enum _PROCESS_STATE {
47 PROCESS_RUNNING = 0, // A thread in the process is currently running
48 PROCESS_READY, // The process is ready to run. (essentially its threads)
49 PROCESS_WAITING, // Waiting on a mutual exclusion or just sleeping.
50 PROCESS_TERMINATING, // The process is ongoing termination in the kernel.
51 PROCESS_TERMINATED, // Process is terminated, but core parts of its structure has been kept.
52 PROCESS_SUSPENDED // The process has been suspended by the kernel, either by choice or forcefully.
54
59
60// ------------------ STRUCTURES ------------------
61
62//
63// Thread Access Rights
64//
65#define MT_THREAD_TERMINATE 0x0001 // Terminate the thread
66#define MT_THREAD_SUSPEND_RESUME 0x0002 // Suspend or resume thread execution
67#define MT_THREAD_SET_CONTEXT 0x0004 // Modify thread CPU context (registers, RIP/RSP)
68#define MT_THREAD_GET_CONTEXT 0x0008 // Read thread CPU context
69#define MT_THREAD_QUERY_INFO 0x0010 // Query thread info (state, priority, etc.)
70#define MT_THREAD_SET_INFO 0x0020 // Modify thread info (priority, name, affinity)
71
72#define MT_THREAD_ALL_ACCESS 0x003F // Request all valid thread access rights
73
74
75//
76// Process Access Rights
77//
78#define MT_PROCESS_TERMINATE 0x0001 // Kill the process
79#define MT_PROCESS_CREATE_THREAD 0x0002 // Create a new thread inside process
80#define MT_PROCESS_VM_OPERATION 0x0004 // Allocate/Protect/Free process memory
81#define MT_PROCESS_VM_READ 0x0008 // Read from process memory
82#define MT_PROCESS_VM_WRITE 0x0010 // Write to process memory
83#define MT_PROCESS_DUP_HANDLE 0x0020 // Duplicate a handle into this process
84#define MT_PROCESS_SET_INFO 0x0040 // Modify process properties/metadata
85#define MT_PROCESS_QUERY_INFO 0x0080 // Query process details (PID, exit code, etc.)
86#define MT_PROCESS_SUSPEND_RESUME 0x0100 // Suspend / Resume process
87#define MT_PROCESS_CREATE_PROCESS 0x0200 // Create a new process.
88
89#define MT_PROCESS_ALL_ACCESS 0x01FF // Everything above
90
91typedef struct _EPROCESS {
92 struct _IPROCESS InternalProcess; // Internal process structure. (KPROCESS Equivalent-ish)
93 char ImageName[24]; // Process image name - e.g "mtoskrnl.mtexe"
94 HANDLE PID; // Process Identifier, unique identifier to the process.
95 HANDLE ParentProcess; // Parent Process Handle
96 uint32_t priority; // TODO
97 uint64_t CreationTime; // Timestamp of creation, seconds from 1970 January 1st. (may change)
98 // SID TODO. - User info as well, when users.
99
100 // TODO PEB
101 void* FileBuffer; // TODO RemoveMe for sections.
102 uint64_t ImageBase; // Base Pointer of loaded process memory.
103
104 // Synchorinzation for internal functions.
105 struct _RUNDOWN_REF ProcessRundown; // A process rundown that is used to safely synchronize the teardown or deletion of a process, ensuring no threads are still accessing it.
106
107 // Thread infos
108 struct _ETHREAD* MainThread; // Pointer to the main thread created for the process.
109 DOUBLY_LINKED_LIST AllThreads; // A linked list of pointers to the current threads of the process. (inserted with each new creation)
110 uint32_t NumThreads; // Unsigned 32 bit integer representing the amount of threads the process has.
111 uint64_t NextStackTop; // A 64 bit value representing the next stack top for a newly created thread
112
113 // Handle Table
115
116 // VAD
117 struct _MMVAD* VadRoot; // The Root of the VAD for the process. (used to find free virtual addresses spaces in the process, and information about them)
118 SPINLOCK VadLock; // The spinlock to ensure VAD atomicity.
120
121typedef struct _ETHREAD {
122 struct _ITHREAD InternalThread; // Internal thread structure. (KTHREAD Equivalent-ish)
123 // TODO TEB
125 HANDLE TID; /* thread id */
126 struct _EVENT* CurrentEvent; /* ptr to current EVENT if any. */
127 struct _EPROCESS* ParentProcess; /* pointer to the parent process of the thread */
128 struct _DOUBLY_LINKED_LIST ThreadListEntry; // Forward and backward links to queue threads in.
129 struct _RUNDOWN_REF ThreadRundown; // A thread rundown that is used to safely synchronize the teardown or deletion of a thread, ensuring no other threads are still accessing it.
130 MTSTATUS ExitStatus; // The status the thread exited in.
131 /* TODO: priority, affinity, wait list, etc. */
133
139
140// ------------------ MACROS ------------------
141#define PROCESS_STACK_SIZE (32*1024) // 32 KiB
142#define PROCESS_STACK_ALIGNMENT 16 // Alignment of 16 Bytes.
143
144// ------------------ TYPE DEFINES ------------------
145
146typedef void* THREAD_PARAMETER;
148
149// ------------------ FUNCTIONS ------------------
150
154
157 IN const char* ExecutablePath,
158 OUT PHANDLE ProcessHandle,
159 IN ACCESS_MASK DesiredAccess,
160 _In_Opt HANDLE ParentProcess
161);
162
165 HANDLE ProcessHandle,
166 PHANDLE ThreadHandle,
167 ThreadEntry EntryPoint,
168 THREAD_PARAMETER ThreadParameter,
169 TimeSliceTicks TimeSlice
170);
171
172extern void MsYieldExecution(PTRAP_FRAME threadRegisters);
174
177 IN enum _PS_PHASE_ROUTINE Phase
178);
179
181
182void
184 IN PEPROCESS Process
185);
186
187void
189 IN PETHREAD Thread,
190 IN MTSTATUS ExitStatus
191);
192
193void
195 IN void* Object
196);
197
200 void
201);
202
204
205void
207 void
208);
209
213 void
214)
215
216// Will return the current process the thread is attached to (could be its parent thread, could be another in an APC)
217
218{
219 if (MeGetCurrentThread()) {
221 }
222 else return NULL;
223}
224
226void
230
232void
236
240 IN PITHREAD IThread
241)
242
243{
244 return CONTAINING_RECORD(IThread, ETHREAD, InternalThread);
245}
246
250 IN PIPROCESS IProcess
251)
252
253{
254 return CONTAINING_RECORD(IProcess, EPROCESS, InternalProcess);
255}
256
258bool
260 IN PETHREAD Thread
261)
262
263{
264 // safety guard, can't believe i had to put it.
265 if (Thread == NULL) return true;
266 return (Thread->ParentProcess == &PsInitialSystemProcess);
267}
268
269HANDLE
271 IN PEPROCESS Process
272);
273
274HANDLE
276 IN PETHREAD Thread
277);
278
281 IN HANDLE ProcessId
282);
283
286 IN HANDLE ThreadId
287);
288
289void
291 IN HANDLE Cid
292);
293
294
295
296
297
298
299
300// End Of Ps API.
301
302// Executive Functions - Are in PS.H
303// Enqueues a thread into the queue with spinlock protection.
305void
307 Queue* queue, PETHREAD thread)
308{
309 IRQL flags;
310 MsAcquireSpinlock(&queue->lock, &flags);
311
312 // Initialize the new node's links
313 thread->ThreadListEntry.Flink = NULL;
314
315 if (queue->tail) {
316 // Link new node to current tail
317 thread->ThreadListEntry.Blink = &queue->tail->ThreadListEntry;
318 // Link current tail to new node
319 queue->tail->ThreadListEntry.Flink = &thread->ThreadListEntry;
320 }
321 else {
322 // List was empty
323 thread->ThreadListEntry.Blink = NULL;
324 queue->head = thread;
325 }
326
327 // Update tail to be the new thread
328 queue->tail = thread;
329
330 MsReleaseSpinlock(&queue->lock, flags);
331}
332
333// Dequeues the head thread from the queue with spinlock protection.
337{
338 IRQL flags;
340
341 if (!q->head) {
343 return NULL;
344 }
345
346 PETHREAD t = q->head;
347
348 // Check if there is a next item
349 if (t->ThreadListEntry.Flink) {
350 // Get the ETHREAD from the generic list entry
351 q->head = CONTAINING_RECORD(t->ThreadListEntry.Flink, ETHREAD, ThreadListEntry);
352 // The new head has no previous item
353 q->head->ThreadListEntry.Blink = NULL;
354 }
355 else {
356 // Queue is now empty
357 q->head = NULL;
358 q->tail = NULL;
359 }
360
361 // Isolate the removed thread
362 t->ThreadListEntry.Flink = NULL;
363 t->ThreadListEntry.Blink = NULL;
364
366 return t;
367}
368
369// Enqueues the thread given to the queue (No Lock).
371void MeEnqueueThread(Queue* queue, PETHREAD thread)
372{
373 // Initialize the new node's links
374 thread->ThreadListEntry.Flink = NULL;
375
376 if (queue->tail) {
377 // Link new node to current tail
378 thread->ThreadListEntry.Blink = &queue->tail->ThreadListEntry;
379 // Link current tail to new node
380 queue->tail->ThreadListEntry.Flink = &thread->ThreadListEntry;
381 }
382 else {
383 // List was empty
384 thread->ThreadListEntry.Blink = NULL;
385 queue->head = thread;
386 }
387
388 // Update tail to be the new thread
389 queue->tail = thread;
390}
391
392// Dequeues the head thread from the queue (No Lock).
395{
396 if (!q->head) {
397 return NULL;
398 }
399
400 PETHREAD t = q->head;
401
402 // Check if there is a next item
403 if (t->ThreadListEntry.Flink) {
404 // Get the ETHREAD from the generic list entry
405 q->head = CONTAINING_RECORD(t->ThreadListEntry.Flink, ETHREAD, ThreadListEntry);
406 // The new head has no previous item
407 q->head->ThreadListEntry.Blink = NULL;
408 }
409 else {
410 // Queue is now empty
411 q->head = NULL;
412 q->tail = NULL;
413 }
414
415 // Isolate the removed thread
416 t->ThreadListEntry.Flink = NULL;
417 t->ThreadListEntry.Blink = NULL;
418
419 return t;
420}
421
422#endif
#define _In_Opt
Definition annotations.h:9
#define FORCEINLINE
Definition annotations.h:22
#define IN
Definition annotations.h:7
#define OUT
Definition annotations.h:8
struct _EPROCESS EPROCESS
Definition core.h:49
IPROCESS * PIPROCESS
Definition core.h:38
enum _IRQL IRQL
EPROCESS * PEPROCESS
Definition core.h:50
ITHREAD * PITHREAD
Definition core.h:34
TRAP_FRAME * PTRAP_FRAME
Definition core.h:54
struct _ETHREAD ETHREAD
Definition core.h:41
ETHREAD * PETHREAD
Definition core.h:42
struct _DOUBLY_LINKED_LIST DOUBLY_LINKED_LIST
uint32_t ACCESS_MASK
Definition ht.h:63
int32_t * PHANDLE
Definition ht.h:59
struct _HANDLE_TABLE * PHANDLE_TABLE
int32_t HANDLE
Definition ht.h:59
#define CONTAINING_RECORD(ptr, type, member)
Definition macros.h:11
FORCEINLINE PITHREAD MeGetCurrentThread(void)
Definition me.h:431
enum _TimeSliceTicks TimeSliceTicks
uint32_t flags
Definition mh.h:2
struct _Queue Queue
struct _SPINLOCK SPINLOCK
#define MT_SUCCESS
Definition mtstatus.h:22
int32_t MTSTATUS
Definition mtstatus.h:12
struct _OBJECT_TYPE * POBJECT_TYPE
MTSTATUS PsCreateSystemThread(ThreadEntry entry, THREAD_PARAMETER parameter, TimeSliceTicks TIMESLICE)
Definition thread.c:122
FORCEINLINE void MeEnqueueThreadWithLock(Queue *queue, PETHREAD thread)
Definition ps.h:306
enum _PS_PHASE_ROUTINE PS_PHASE_ROUTINE
void PsTerminateThread(IN PETHREAD Thread, IN MTSTATUS ExitStatus)
Definition thread.c:196
_PS_PHASE_ROUTINE
Definition ps.h:55
@ PS_PHASE_INITIALIZE_SYSTEM
Definition ps.h:56
@ PS_PHASE_INITIALIZE_WORKER_THREADS
Definition ps.h:57
enum _PROCESS_STATE * PPROCESS_STATE
FORCEINLINE PEPROCESS PsGetCurrentProcess(void)
Definition ps.h:212
FORCEINLINE PETHREAD PsGetEThreadFromIThread(IN PITHREAD IThread)
Definition ps.h:239
void PsInitializeCidTable(void)
Definition cid.c:27
enum _PROCESS_STATE PROCESS_STATE
PETHREAD PsLookupThreadByThreadId(IN HANDLE ThreadId)
Definition cid.c:138
FORCEINLINE void MeEnqueueThread(Queue *queue, PETHREAD thread)
Definition ps.h:371
FORCEINLINE PETHREAD MeDequeueThreadWithLock(Queue *q)
Definition ps.h:336
FORCEINLINE PEPROCESS PsGetEProcessFromIProcess(IN PIPROCESS IProcess)
Definition ps.h:249
HANDLE PsAllocateThreadId(IN PETHREAD Thread)
Definition cid.c:88
void MsYieldExecution(PTRAP_FRAME threadRegisters)
void(* ThreadEntry)(THREAD_PARAMETER)
Definition ps.h:147
enum _THREAD_STATE * PTHREAD_STATE
PETHREAD PsGetCurrentThread(void)
Definition thread.c:191
void PsDeferKernelStackDeletion(void *StackBase, bool IsLarge)
Definition pswork.c:65
enum _THREAD_STATE THREAD_STATE
PEPROCESS PsLookupProcessByProcessId(IN HANDLE ProcessId)
Definition cid.c:113
void PsInitializeWorkerThreads(void)
Definition pswork.c:89
FORCEINLINE void PsTerminateCurrentThread(void)
Definition ps.h:227
_THREAD_STATE
Definition ps.h:37
@ THREAD_TERMINATED
Definition ps.h:42
@ THREAD_ZOMBIE
Definition ps.h:43
@ THREAD_BLOCKED
Definition ps.h:40
@ THREAD_TERMINATING
Definition ps.h:41
@ THREAD_RUNNING
Definition ps.h:38
@ THREAD_READY
Definition ps.h:39
FORCEINLINE bool PsIsKernelThread(IN PETHREAD Thread)
Definition ps.h:259
MTSTATUS PsInitializeSystem(IN enum _PS_PHASE_ROUTINE Phase)
Definition psmgr.c:97
MTSTATUS PsCreateThread(HANDLE ProcessHandle, PHANDLE ThreadHandle, ThreadEntry EntryPoint, THREAD_PARAMETER ThreadParameter, TimeSliceTicks TimeSlice)
Definition thread.c:34
HANDLE PsAllocateProcessId(IN PEPROCESS Process)
Definition cid.c:59
FORCEINLINE void PsTerminateCurrentProcess(void)
Definition ps.h:233
_PROCESS_STATE
Definition ps.h:46
@ PROCESS_WAITING
Definition ps.h:49
@ PROCESS_READY
Definition ps.h:48
@ PROCESS_TERMINATING
Definition ps.h:50
@ PROCESS_RUNNING
Definition ps.h:47
@ PROCESS_SUSPENDED
Definition ps.h:52
@ PROCESS_TERMINATED
Definition ps.h:51
void * THREAD_PARAMETER
Definition ps.h:146
void PsTerminateProcess(IN PEPROCESS Process)
Definition process.c:231
FORCEINLINE PETHREAD MeDequeueThread(Queue *q)
Definition ps.h:394
void PsDeleteThread(IN void *Object)
Definition thread.c:220
struct _STACK_REAPER_ENTRY * PSTACK_REAPER_ENTRY
void PsFreeCid(IN HANDLE Cid)
Definition cid.c:163
struct _STACK_REAPER_ENTRY STACK_REAPER_ENTRY
MTSTATUS PsCreateProcess(IN const char *ExecutablePath, OUT PHANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess, _In_Opt HANDLE ParentProcess)
Definition process.c:50
POBJECT_TYPE PsThreadType
Definition psmgr.c:31
POBJECT_TYPE PsProcessType
Definition psmgr.c:30
EPROCESS PsInitialSystemProcess
Definition kernel.c:162
void MsAcquireSpinlock(IN PSPINLOCK lock, IN PIRQL OldIrql)
Definition spinlock.c:13
void MsReleaseSpinlock(IN PSPINLOCK lock, IN IRQL OldIrql)
Definition spinlock.c:45
PEPROCESS SavedApcProcess
Definition me.h:249
struct _DOUBLY_LINKED_LIST * Blink
Definition core.h:28
struct _DOUBLY_LINKED_LIST * Flink
Definition core.h:29
Definition ps.h:91
uint32_t NumThreads
Definition ps.h:110
struct _RUNDOWN_REF ProcessRundown
Definition ps.h:105
HANDLE PID
Definition ps.h:94
HANDLE ParentProcess
Definition ps.h:95
PHANDLE_TABLE ObjectTable
Definition ps.h:114
void * FileBuffer
Definition ps.h:101
char ImageName[24]
Definition ps.h:93
struct _MMVAD * VadRoot
Definition ps.h:117
uint32_t priority
Definition ps.h:96
struct _ETHREAD * MainThread
Definition ps.h:108
uint64_t NextStackTop
Definition ps.h:111
uint64_t CreationTime
Definition ps.h:97
struct _IPROCESS InternalProcess
Definition ps.h:92
SPINLOCK VadLock
Definition ps.h:118
uint64_t ImageBase
Definition ps.h:102
DOUBLY_LINKED_LIST AllThreads
Definition ps.h:109
Definition ps.h:121
struct _RUNDOWN_REF ThreadRundown
Definition ps.h:129
struct _EPROCESS * ParentProcess
Definition ps.h:127
struct _EXCEPTION_REGISTRATION_RECORD ExceptionRegistration
Definition ps.h:124
struct _ITHREAD InternalThread
Definition ps.h:122
HANDLE TID
Definition ps.h:125
MTSTATUS ExitStatus
Definition ps.h:130
struct _EVENT * CurrentEvent
Definition ps.h:126
struct _DOUBLY_LINKED_LIST ThreadListEntry
Definition ps.h:128
Definition ms.h:69
Definition me.h:261
struct _APC_STATE ApcState
Definition me.h:270
Definition mm.h:473
PETHREAD tail
Definition ms.h:53
PETHREAD head
Definition ms.h:52
struct _SPINLOCK lock
Definition ms.h:54
void * StackBase
Definition ps.h:136
struct _STACK_REAPER_ENTRY * Next
Definition ps.h:135