fix: various kernel bug fixes

This commit is contained in:
2026-04-10 23:16:02 +02:00
parent 0433274244
commit fd7fcbbbc8
12 changed files with 282 additions and 163 deletions
+19 -2
View File
@@ -18,6 +18,8 @@
#include <Hal/SmpBoot.hpp>
#include <Timekeeping/ApicTimer.hpp>
#include <Api/WinServer.hpp>
#include <Api/Heap.hpp>
#include <Api/LibSyscall.hpp>
#include <Ipc/Ipc.hpp>
// Assembly: context switch with CR3 and FPU state parameters
@@ -135,6 +137,7 @@ namespace Sched {
processTable[i].cwd[0] = '\0';
processTable[i].runningOnCpu = -1;
processTable[i].killPending = false;
processTable[i].reapReady = false;
processTable[i].waitingForPid = -1;
processTable[i].sleepUntilTick = 0;
processTable[i].waitingOnObject = nullptr;
@@ -184,6 +187,7 @@ namespace Sched {
// dispatch this half-initialized process.
processTable[slot].state = ProcessState::Running;
processTable[slot].runningOnCpu = -1;
processTable[slot].reapReady = false;
schedLock.Release();
// Create per-process PML4 with kernel-half copied
@@ -319,6 +323,7 @@ namespace Sched {
proc.readdirCursor = 0;
proc.runningOnCpu = -1;
proc.killPending = false;
proc.reapReady = false;
proc.waitingForPid = -1;
proc.sleepUntilTick = 0;
proc.waitingOnObject = nullptr;
@@ -417,7 +422,7 @@ namespace Sched {
static void ReclaimTerminated() {
schedLock.Acquire();
for (int i = 0; i < MaxProcesses; i++) {
if (processTable[i].state == ProcessState::Terminated) {
if (processTable[i].state == ProcessState::Terminated && processTable[i].reapReady) {
// Grab the pointers, mark Free, then free memory after releasing lock
void* stackBase = (processTable[i].stackBase != 0)
? (void*)processTable[i].stackBase : nullptr;
@@ -425,6 +430,7 @@ namespace Sched {
? (void*)Memory::HHDM(processTable[i].pml4Phys) : nullptr;
processTable[i].stackBase = 0;
processTable[i].pml4Phys = 0;
processTable[i].reapReady = false;
processTable[i].state = ProcessState::Free;
// Release lock during PFA::Free to minimize hold time
@@ -610,6 +616,8 @@ namespace Sched {
// Release process-scoped IPC handles/mappings before tearing down the address space.
Ipc::CleanupProcessSlot(slot, exitingPid, proc.pml4Phys);
Montauk::CleanupHeapForSlot(slot, proc.pml4Phys);
Montauk::CleanupLibTable(slot);
proc.waitingForPid = -1;
proc.sleepUntilTick = 0;
@@ -640,6 +648,7 @@ namespace Sched {
proc.state = ProcessState::Terminated;
proc.runningOnCpu = -1;
proc.reapReady = true;
// Wake any processes blocked on this PID
for (int i = 0; i < MaxProcesses; i++) {
@@ -729,12 +738,14 @@ namespace Sched {
}
// Process is Ready or Blocked (not running on any CPU).
// Mark it Terminated so the scheduler won't pick it up.
// Mark it non-runnable so the scheduler won't pick it up, but do not
// expose it to the BSP reaper until teardown is complete.
int killedPid = proc.pid;
if (proc.state == ProcessState::Ready)
readyCount--;
proc.state = ProcessState::Terminated;
proc.killPending = false;
proc.reapReady = false;
proc.waitingForPid = -1;
proc.sleepUntilTick = 0;
proc.waitingOnObject = nullptr;
@@ -756,6 +767,8 @@ namespace Sched {
// Safe to clean up resources now -- process is not running anywhere.
WinServer::CleanupProcess(killedPid);
Ipc::CleanupProcessSlot(slot, killedPid, proc.pml4Phys);
Montauk::CleanupHeapForSlot(slot, proc.pml4Phys);
Montauk::CleanupLibTable(slot);
proc.redirected = false;
proc.parentPid = -1;
@@ -778,6 +791,10 @@ namespace Sched {
Memory::VMM::Paging::FreeUserHalf(proc.pml4Phys);
schedLock.Acquire();
proc.reapReady = true;
schedLock.Release();
// Kernel stack and PML4 freed by ReclaimTerminated on BSP tick.
return 0;
}