kernel
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
23void* 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) {
69 // We dont have a node to put in the deferred list, we must free the stack now to not let the system die on us.
70 MiFreeKernelStack(StackBase, IsLarge);
71 return;
72 }
73
74 node->StackBase = StackBase;
75 node->IsLarge = IsLarge;
76
77 void* old;
78 do {
79 old = (void*)g_StackReaperList;
80 node->Next = (PSTACK_REAPER_ENTRY)old;
81 // cast target to volatile void** to match prototype
82 } while (InterlockedCompareExchangePointer((volatile void* volatile*)&g_StackReaperList, (void*)node, old) != old);
83
84 // Wake the reaper (safe from any context)
85#ifdef DEBUG
87 assert(MT_SUCCEEDED(status));
88#else
90#endif
91}
92
94 // Setup the event.
95 g_StackReaperEvent.lock.locked = 0;
96 g_StackReaperEvent.signaled = false;
98 g_StackReaperEvent.waitingQueue.head = g_StackReaperEvent.waitingQueue.tail = NULL;
99
100 // We just create a system thread for freeing stacks.
101 PETHREAD StackThread = NULL;
102 MTSTATUS status = PsCreateSystemThread((ThreadEntry)PsStackDeleterThread, NULL, LOW_TIMESLICE_TICKS, &StackThread);
103
104 if (MT_FAILURE(status)) {
107 (void*)(uintptr_t)status,
108 NULL,
109 NULL,
110 NULL
111 );
112 }
113
114 // Set it as a worker thread.
115 StackThread->WorkerThread = true;
116}
117
#define FORCEINLINE
Definition annotations.h:23
#define assert(...)
Definition assert.h:57
FORCEINLINE void * InterlockedCompareExchangePointer(volatile void *volatile *target, void *value, void *comparand)
Definition atomic.h:177
FORCEINLINE void * InterlockedExchangePointer(volatile void *volatile *target, void *value)
Definition atomic.h:176
NORETURN void MeBugCheckEx(IN enum _BUGCHECK_CODES BugCheckCode, IN void *BugCheckParameter1, IN void *BugCheckParameter2, IN void *BugCheckParameter3, IN void *BugCheckParameter4)
Definition bugcheck.c:245
ETHREAD * PETHREAD
Definition core.h:44
MTSTATUS MsWaitForEvent(IN PEVENT event)
Definition events.c:117
MTSTATUS MsSetEvent(IN PEVENT event)
Definition events.c:13
void gop_printf(uint32_t color, const char *fmt,...)
Definition gop.c:633
@ 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:30
@ NonPagedPool
Definition mm.h:355
void MiFreeKernelStack(IN void *AllocatedStackTop, IN bool LargeStack)
Definition mmproc.c:149
@ SynchronizationEvent
Definition ms.h:63
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:632
void * MmAllocatePoolWithTag(IN enum _POOL_TYPE PoolType, IN size_t NumberOfBytes, IN uint32_t Tag)
Definition pool.c:443
void(* ThreadEntry)(THREAD_PARAMETER)
Definition ps.h:218
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
void PsDeferKernelStackDeletion(void *StackBase, bool IsLarge)
Definition pswork.c:65
void PsInitializeWorkerThreads(void)
Definition pswork.c:93
void * g_StackReaperList
Definition pswork.c:23
bool WorkerThread
Definition ps.h:201
void * StackBase
Definition ps.h:207
struct _STACK_REAPER_ENTRY * Next
Definition ps.h:206
MTSTATUS PsCreateSystemThread(ThreadEntry entry, THREAD_PARAMETER parameter, TimeSliceTicks TIMESLICE, _Out_Opt PETHREAD *OutThread)
Definition thread.c:196