diff --git a/kernel/src/Api/Heap.hpp b/kernel/src/Api/Heap.hpp index b462663..6078c3f 100644 --- a/kernel/src/Api/Heap.hpp +++ b/kernel/src/Api/Heap.hpp @@ -61,6 +61,25 @@ namespace Zenith { return userVa; } + // Free all heap allocations for a process slot (called on process exit) + static void CleanupHeapForSlot(int slot, uint64_t pml4Phys) { + if (slot < 0 || slot >= Sched::MaxProcesses) return; + + for (int i = 0; i < g_heapAllocCount[slot]; i++) { + uint64_t va = g_heapAllocs[slot][i].va; + uint64_t numPages = g_heapAllocs[slot][i].numPages; + for (uint64_t p = 0; p < numPages; p++) { + uint64_t pageVa = va + p * 0x1000; + uint64_t physAddr = Memory::VMM::Paging::GetPhysAddr(pml4Phys, pageVa); + if (physAddr != 0) { + Memory::g_pfa->Free((void*)Memory::HHDM(physAddr)); + } + Memory::VMM::Paging::UnmapUserIn(pml4Phys, pageVa); + } + } + g_heapAllocCount[slot] = 0; + } + static void Sys_Free(uint64_t addr) { auto* proc = Sched::GetCurrentProcessPtr(); if (proc == nullptr) return; diff --git a/kernel/src/Api/Syscall.cpp b/kernel/src/Api/Syscall.cpp index ec32daa..bd60ab3 100644 --- a/kernel/src/Api/Syscall.cpp +++ b/kernel/src/Api/Syscall.cpp @@ -39,9 +39,14 @@ namespace Zenith { extern "C" int64_t SyscallDispatch(SyscallFrame* frame) { switch (frame->syscall_nr) { - case SYS_EXIT: + case SYS_EXIT: { + int slot = GetCurrentSlot(); + auto* proc = Sched::GetCurrentProcessPtr(); + if (slot >= 0 && proc) + CleanupHeapForSlot(slot, proc->pml4Phys); Sys_Exit((int)frame->arg1); return 0; + } case SYS_YIELD: Sys_Yield(); return 0; @@ -192,8 +197,16 @@ namespace Zenith { return (int64_t)Sys_WinResize((int)frame->arg1, (int)frame->arg2, (int)frame->arg3); case SYS_PROCLIST: return (int64_t)Sys_ProcList((ProcInfo*)frame->arg1, (int)frame->arg2); - case SYS_KILL: + case SYS_KILL: { + // Free heap allocations for the target process before killing it + auto* target = Sched::GetProcessByPid((int)frame->arg1); + if (target) { + auto* slot0 = Sched::GetProcessSlot(0); + int targetSlot = (int)(target - slot0); + CleanupHeapForSlot(targetSlot, target->pml4Phys); + } return (int64_t)Sys_Kill((int)frame->arg1); + } case SYS_DEVLIST: return (int64_t)Sys_DevList((DevInfo*)frame->arg1, (int)frame->arg2); case SYS_WINSETSCALE: diff --git a/ramdisk.tar b/ramdisk.tar index cc622a3..824c7e6 100644 Binary files a/ramdisk.tar and b/ramdisk.tar differ