Skip to content

refactor(mm): introduce OwnedFrame, ArchPageTable, VMA AddressSpace, and safe user copy - #17

Open
imtsuki wants to merge 1 commit into
masterfrom
refactor/kernel-paging
Open

imtsuki wants to merge 1 commit into
masterfrom
refactor/kernel-paging

Conversation

@imtsuki

@imtsuki imtsuki commented Nov 14, 2026

Copy link
Copy Markdown
Owner

Summary

Rebuilds kernel physical & virtual memory management into a clean three-layer Rust OS architecture, removing OffsetPageTable, x86_64::FrameAllocator, 'static page-table lifetime hacks, memory leaks on address-space teardown, and unchecked user pointers:

1. RAII Physical Frames (src/memory/frame.rs, bitmap.rs, mod.rs)

  • Decouples src/memory completely from x86_64 traits (removing #[cfg(target_arch = "x86_64")] on pub mod memory in main.rs).
  • Introduces linear RAII OwnedFrame (alloc, alloc_zeroed, as_slice, as_mut_slice, into_raw, from_raw) that automatically returns physical pages to FRAME_ALLOCATOR on Drop unless transferred into a hardware PTE.

2. Thin Hardware Page Table (src/arch/x86_64/paging.rs, allocator.rs, start.rs)

  • Replaces OffsetPageTable / Mapper / Translate / MapperFlush with ArchPageTable operating directly over PAGE_OFFSET_BASE without forging &'static mut PageTable references.
  • Pre-populates higher-half kernel PML4[384..480] slots at boot (paging::init()) and marks kernel leaf PTEs GLOBAL, ensuring dynamic kernel heap, stack, and MMIO mappings are permanently synchronized across all processes.
  • Implements full recursive teardown in ArchPageTable::Drop over user PML4[0..256] (freeing all leaf data frames and L1..=L4 page-table frames in one pass).
  • Simplifies arch/x86_64/allocator.rs (init_heap) and KernelStack::alloc to a single paging::map_kernel_range(...) call.

3. VMA-Backed AddressSpace, W^X exec, & Safe User Memory Copy (address_space.rs, process.rs, syscall/)

  • Introduces architecture-independent MapPerm (R | W | X | U), VmArea / VmAreaKind, and BTreeMap-backed AddressSpace with O(log N) overlap checks, unmapped null-page guard (USER_SPACE_MIN = 0x1000), and strong OOM rollback guarantees in map_region.
  • Restores strict W^X permissions in Process::exec (.rodata -> R, .text -> R | X, .data/.bss & stack -> R | W) with atomic self.vm = new_vm swap-and-drop semantics.
  • Replaces unchecked slice::from_raw_parts / from_raw_parts_mut in src/syscall.rs and src/syscall/fs.rs with validate_user_range, copy_from_user, and copy_to_user (enforcing MapPerm::U | R / W and returning EFAULT / ENAMETOOLONG instead of panicking or exposing kernel memory).

Verification

  • make build (dev & release)
  • Verified zero physical frame leaks across AddressSpace creation, multi-VMA mapping, cross-page copy_to_user/copy_from_user, EEXIST overlap checks, W^X read-only EFAULT rejection, null/kernel-pointer EFAULT rejection, and Drop teardown (free_frames before == after)
  • Booted in QEMU (debug & release) and verified Ring 3 /init interactive shell execution

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T00:18:58.791052Z 9944810 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9944810a2f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +143 to +147
let mut rollback = start;
while rollback < mapped_until {
let _ = self.arch.unmap_4k(VirtAddr::new(rollback));
rollback += PAGE_SIZE as u64;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Reclaim newly allocated page tables during OOM rollback

When frame allocation fails while mapping a region, this rollback frees only leaf frames: unmap_4k leaves intermediate tables allocated, and map_4k_in_root also retains tables created before the failing allocation. For example, with three free frames and an unused PML4 slot, the first page consumes a data frame and two table frames before failing to allocate the final table; the data frame is returned, but both table frames remain despite map_region returning ENOMEM without adding a VMA. This violates the advertised strong rollback guarantee and retains scarce physical memory until the entire address space is dropped. Track and reclaim newly created empty tables, including those on the page whose mapping failed.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant