aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-02-23 02:06:37 +0530
committerBobby <[email protected]>2026-02-23 02:06:37 +0530
commit5f0244915a2005f0fa60083f9e405d11d55a6280 (patch)
tree994a55ab57d3d727a071147c848fbd90bd1e6c15
parent6f7101ed9b5d9d29a8d3c0441c91f022d89b65df (diff)
downloadakiba-5f0244915a2005f0fa60083f9e405d11d55a6280.tar.xz
akiba-5f0244915a2005f0fa60083f9e405d11d55a6280.zip
refactor: Simplify memory cleanup logic in Kata management
-rw-r--r--mirai/kata/memory.zig6
-rw-r--r--mirai/kata/pool.zig2
-rw-r--r--mirai/memory/paging.zig23
3 files changed, 1 insertions, 30 deletions
diff --git a/mirai/kata/memory.zig b/mirai/kata/memory.zig
index 6e835af..a479c25 100644
--- a/mirai/kata/memory.zig
+++ b/mirai/kata/memory.zig
@@ -218,17 +218,11 @@ pub fn load_segment(
}
}
-/// Clean up all memory associated with a Kata
-/// Called when a Kata is dissolved/exits
pub fn cleanup(kata: *types.Kata) void {
if (kata.page_table != 0) {
- // Destroy the page table and free all associated pages
- // This frees: user stack, program pages, page table structures
- // Does NOT free: shared kernel pages, framebuffer
paging.destroy_page_table(kata.page_table);
kata.page_table = 0;
}
-
kata.stack_top = 0;
kata.user_stack_top = 0;
}
diff --git a/mirai/kata/pool.zig b/mirai/kata/pool.zig
index b4b90cd..8c057f9 100644
--- a/mirai/kata/pool.zig
+++ b/mirai/kata/pool.zig
@@ -71,9 +71,7 @@ pub fn dissolve(kata_id: u32) void {
for (&pool, 0..) |*kata, i| {
if (used[i] and kata.id == kata_id) {
- // Clean up all memory associated with this Kata
memory.cleanup(kata);
-
kata.state = .Dissolved;
used[i] = false;
waker.wake_waiting(kata_id);
diff --git a/mirai/memory/paging.zig b/mirai/memory/paging.zig
index c8584a2..61c69b7 100644
--- a/mirai/memory/paging.zig
+++ b/mirai/memory/paging.zig
@@ -246,32 +246,19 @@ pub fn virt_to_phys(cr3: u64, virt: u64) ?u64 {
return phys_base + offset;
}
-/// Check if a page should be freed based on virtual and physical address
-/// Returns true if the page should be freed, false if it's shared
fn should_free_page(virt: u64, phys: u64) bool {
- // Identity-mapped kernel region (kernel code, data, PMM bitmap)
- // These physical pages are shared - don't free
- if (virt < pmm_const.KERNEL_MAP_END) {
- return false;
- }
- // Framebuffer region - hardware memory, not PMM managed
+ if (virt < pmm_const.KERNEL_MAP_END) return false;
if (phys >= pmm_const.MMIO_FRAMEBUFFER_BASE and
phys < pmm_const.MMIO_FRAMEBUFFER_BASE + pmm_const.MMIO_FRAMEBUFFER_SIZE)
{
return false;
}
- // Everything else (user stack, program segments, etc.) should be freed
return true;
}
-/// Destroy a page table and free all associated memory
-/// Frees: user pages, page table structures
-/// Does NOT free: shared kernel pages, framebuffer
pub fn destroy_page_table(page_table_phys: u64) void {
const pml4: [*]volatile u64 = @ptrFromInt(page_table_phys + HIGHER_HALF_START);
- // Only walk user-space PML4 entries (0-255)
- // Entries 256-511 are shared kernel mappings, don't touch
for (0..paging_const.KERNEL_PML4_START) |pml4_idx| {
const pml4_entry = pml4[pml4_idx];
if ((pml4_entry & PAGE_PRESENT) == 0) continue;
@@ -293,37 +280,29 @@ pub fn destroy_page_table(page_table_phys: u64) void {
const pt_phys = pd_entry & paging_const.PTE_MASK;
const pt: [*]volatile u64 = @ptrFromInt(pt_phys + HIGHER_HALF_START);
- // Free all physical pages in this page table
for (0..512) |pt_idx| {
const pt_entry = pt[pt_idx];
if ((pt_entry & PAGE_PRESENT) == 0) continue;
const page_phys = pt_entry & paging_const.PTE_MASK;
-
- // Compute virtual address from indices
const virt: u64 = (@as(u64, pml4_idx) << 39) |
(@as(u64, pdpt_idx) << 30) |
(@as(u64, pd_idx) << 21) |
(@as(u64, pt_idx) << 12);
- // Only free if not a shared page
if (should_free_page(virt, page_phys)) {
pmm.free_page(page_phys);
}
}
- // Free the PT page itself
pmm.free_page(pt_phys);
}
- // Free the PD page itself
pmm.free_page(pd_phys);
}
- // Free the PDPT page itself
pmm.free_page(pdpt_phys);
}
- // Free the PML4 page itself
pmm.free_page(page_table_phys);
}