fix: improve scheduling, memory, timer implementations

This commit is contained in:
2026-03-24 07:40:37 +01:00
parent 195028d994
commit f902ab48a1
14 changed files with 239 additions and 94 deletions
+16 -7
View File
@@ -12,17 +12,26 @@ namespace Montauk {
return Sched::GetProcessByPid(proc->parentPid);
}
static void RingWrite(uint8_t* buf, uint32_t& head, uint32_t /*tail*/, uint32_t size, uint8_t byte) {
buf[head] = byte;
head = (head + 1) % size;
// SPSC ring buffer helpers. On x86 TSO, atomic-width aligned
// loads/stores are naturally atomic. The compiler barrier ensures
// the data write is visible before the head update.
static void RingWrite(uint8_t* buf, volatile uint32_t& head, uint32_t /*tail*/, uint32_t size, uint8_t byte) {
uint32_t h = head;
buf[h] = byte;
asm volatile("" ::: "memory"); // compiler barrier
head = (h + 1) % size;
}
static int RingRead(uint8_t* buf, uint32_t& head, uint32_t& tail, uint32_t size, uint8_t* out, int maxLen) {
static int RingRead(uint8_t* buf, volatile uint32_t& head, volatile uint32_t& tail, uint32_t size, uint8_t* out, int maxLen) {
int count = 0;
while (tail != head && count < maxLen) {
out[count++] = buf[tail];
tail = (tail + 1) % size;
uint32_t t = tail;
uint32_t h = head;
while (t != h && count < maxLen) {
out[count++] = buf[t];
t = (t + 1) % size;
}
asm volatile("" ::: "memory"); // compiler barrier
tail = t;
return count;
}
}
+1 -38
View File
@@ -97,43 +97,6 @@ namespace Montauk {
}
static int Sys_Kill(int pid) {
// Refuse to kill PID 0 (init)
if (pid == 0) return -1;
// Refuse to kill the caller's own process
if (pid == Sched::GetCurrentPid()) return -1;
auto* proc = Sched::GetProcessByPid(pid);
if (!proc) return -1;
// Clean up any windows owned by this process (unmaps pixel pages from desktop)
WinServer::CleanupProcess(pid);
// Free I/O redirect buffers
if (proc->outBuf) {
Memory::g_pfa->Free(proc->outBuf);
proc->outBuf = nullptr;
}
if (proc->inBuf) {
Memory::g_pfa->Free(proc->inBuf);
proc->inBuf = nullptr;
}
// Free all user-space pages and page table structures
Memory::VMM::Paging::FreeUserHalf(proc->pml4Phys);
// Free kernel stack (safe — killed process isn't running on single-core)
if (proc->stackBase != 0) {
Memory::g_pfa->Free((void*)proc->stackBase, Sched::StackPages);
proc->stackBase = 0;
}
// Free the PML4 page
if (proc->pml4Phys != 0) {
Memory::g_pfa->Free((void*)Memory::HHDM(proc->pml4Phys));
proc->pml4Phys = 0;
}
proc->state = Sched::ProcessState::Terminated;
return 0;
return Sched::KillProcess(pid);
}
};