My Project
Loading...
Searching...
No Matches
pswork.c
Go to the documentation of this file.
1/*++
2
3Module Name:
4
5 pswork.c
6
7Purpose:
8
9 This translation unit contains the implementation of worker threads in the kernel.
10
11Author:
12
13 slep (Matanel) 2025.
14
15Revision History:
16
17--*/
18
19#include "../../includes/ps.h"
20#include "../../includes/mg.h"
21#include "../../assert.h"
22// globals
23volatile void* g_StackReaperList = NULL; // head of LIFO list (casts to PSTACK_REAPER_ENTRY)
25
26// atomically pop all entries (returns head or NULL)
31
32
33static void PsStackDeleterThread(void) {
34#ifdef DEBUG
35 gop_printf(COLOR_RED, "I have arrived, the reaper of souls n shit (and stacks)\n");
36#endif
37 for (;;) {
38 // Wait until there is work (or force wake).
40
41 // Atomically steal the whole list
43
44 // If nothing (possible if race condition), continue waiting again
45 if (!head) {
46 continue;
47 }
48
49 // Walk and free each stack entry (safe at PASSIVE_LEVEL)
50 while (head) {
51 PSTACK_REAPER_ENTRY cur = head;
52 head = cur->Next;
53
54 // free the kernel stack safely from this thread's stack
56
57 // free the node
58 MmFreePool(cur);
59 }
60
61 // Loop back to wait for more work, if there is work, i work, on fridays, i work, saturdays - work too.
62 }
63}
64
65void PsDeferKernelStackDeletion(void* StackBase, bool IsLarge)
66{
68 if (!node) return;
69
70 node->StackBase = StackBase;
71 node->IsLarge = IsLarge;
72
73 void* old;
74 do {
75 old = (void*)g_StackReaperList;
76 node->Next = (PSTACK_REAPER_ENTRY)old;
77 // cast target to volatile void** to match prototype
78 } while (InterlockedCompareExchangePointer((volatile void* volatile*)&g_StackReaperList, (void*)node, old) != old);
79
80 // Wake the reaper (safe from any context)
81#ifdef DEBUG
83 assert(MT_SUCCEEDED(status));
84#else
86#endif
87}
88
90 // Setup the event.
91 g_StackReaperEvent.lock.locked = 0;
92 g_StackReaperEvent.signaled = false;
94 g_StackReaperEvent.waitingQueue.head = g_StackReaperEvent.waitingQueue.tail = NULL;
95
96 // We just create a system thread for freeing stacks.
97 MTSTATUS status = PsCreateSystemThread((ThreadEntry)PsStackDeleterThread, NULL, LOW_TIMESLICE_TICKS);
98
99 if (MT_FAILURE(status)) {
102 (void*)(uintptr_t)status,
103 NULL,
104 NULL,
105 NULL
106 );
107 }
108}
109
#define FORCEINLINE
Definition annotations.h:22
#define assert(...)
Definition assert.h:57
FORCEINLINE void * InterlockedCompareExchangePointer(volatile void *volatile *target, void *value, void *comparand)
Definition atomic.h:171
FORCEINLINE void * InterlockedExchangePointer(volatile void *volatile *target, void *value)
Definition atomic.h:170
NORETURN void MeBugCheckEx(IN enum _BUGCHECK_CODES BugCheckCode, IN void *BugCheckParameter1, IN void *BugCheckParameter2, IN void *BugCheckParameter3, IN void *BugCheckParameter4)
Definition bugcheck.c:305
MTSTATUS MsWaitForEvent(IN PEVENT event)
Definition events.c:113
MTSTATUS MsSetEvent(IN PEVENT event)
Definition events.c:13
void gop_printf(uint32_t color, const char *fmt,...)
Definition gop.c:694
@ PSWORKER_INIT_FAILED
Definition me.h:131
@ LOW_TIMESLICE_TICKS
Definition me.h:47
#define COLOR_RED
Colors definitions for easier access.
Definition mg.h:29
@ NonPagedPool
Definition mm.h:316
void MiFreeKernelStack(IN void *AllocatedStackTop, IN bool LargeStack)
Definition mmproc.c:148
@ SynchronizationEvent
Definition ms.h:62
struct _EVENT EVENT
#define MT_FAILURE(Status)
Definition mtstatus.h:16
int32_t MTSTATUS
Definition mtstatus.h:12
#define MT_SUCCEEDED(Status)
Macros to test status.
Definition mtstatus.h:15
void MmFreePool(IN void *buf)
Definition pool.c:586
void * MmAllocatePoolWithTag(IN enum _POOL_TYPE PoolType, IN size_t NumberOfBytes, IN uint32_t Tag)
Definition pool.c:427
void(* ThreadEntry)(THREAD_PARAMETER)
Definition ps.h:147
struct _STACK_REAPER_ENTRY * PSTACK_REAPER_ENTRY
struct _STACK_REAPER_ENTRY STACK_REAPER_ENTRY
FORCEINLINE PSTACK_REAPER_ENTRY PopAllStacks(void)
Definition pswork.c:27
EVENT g_StackReaperEvent
Definition pswork.c:24
volatile void * g_StackReaperList
Definition pswork.c:23
void PsDeferKernelStackDeletion(void *StackBase, bool IsLarge)
Definition pswork.c:65
void PsInitializeWorkerThreads(void)
Definition pswork.c:89
void * StackBase
Definition ps.h:136
struct _STACK_REAPER_ENTRY * Next
Definition ps.h:135
MTSTATUS PsCreateSystemThread(ThreadEntry entry, THREAD_PARAMETER parameter, TimeSliceTicks TIMESLICE)
Definition thread.c:122