Skip to content

Added PMM and Paging#10

Merged
Retr0Aa merged 1 commit into
mainfrom
pmm
May 28, 2026
Merged

Added PMM and Paging#10
Retr0Aa merged 1 commit into
mainfrom
pmm

Conversation

@gogo1701

Copy link
Copy Markdown
Collaborator

This pull request introduces a physical memory manager (PMM) and an early paging system to the kernel, enabling dynamic page allocation and virtual memory support. It also adds an extensible CPU exception handler mechanism, including robust page fault handling. These features lay the groundwork for more advanced memory management and safer kernel operation.

The most important changes are:

Memory Management (Physical and Virtual):

  • Added src/memory/pmm.c and src/memory/pmm.h implementing a bitmap-based physical memory manager, with initialization from Multiboot2 memory maps, page allocation/freeing, and memory statistics reporting. The PMM reserves critical regions (kernel, boot info, framebuffer, low memory) and exposes an API for page management. ([[1]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-3838634d348124540a02b93da090d0db55ad8668858d2798254a8c267e5cd25fR1-R363), [[2]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-21ba94371f3f6f20ba6dc03979c9841932346d95e7b2f29e52c51ce5f514a7e4R1-R31))
  • Added src/memory/paging.c and src/memory/paging.h implementing early x86 paging with identity mapping for bootstrapping, dynamic page table management, and functions for mapping/unmapping pages and querying physical addresses. Paging is enabled during kernel initialization. ([[1]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-3bf621908b857940f2ec9c1b9ede9a6d2d6f7cb9ec088174f8edd2a0020d5291R1-R166), [[2]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-fe9c547c895abaa43d9c3c2d9d778dc772c377591c822bd57f96ce360e075bf7R1-R26))
  • Integrated PMM and paging initialization into the kernel startup sequence in src/kernel.c, ensuring the memory subsystems are ready before enabling interrupts and launching the shell. ([[1]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-1d7a8ae32f442a56c39d6b501d9c5d0383d531f2a39cd177e6b890a57b175da4R10-R11), [[2]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-1d7a8ae32f442a56c39d6b501d9c5d0383d531f2a39cd177e6b890a57b175da4R153-R155))

CPU Exception Handling:

  • Extended the IRQ subsystem to support registration and dispatch of CPU exception handlers (vectors 0–31), including a default page fault handler that halts the system on faults. ([[1]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-2ac966076011ae0ff50d6698ce04e1e1e7c878dc4693665aed69710bd03d8a4fR9-R23), [[2]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-2ac966076011ae0ff50d6698ce04e1e1e7c878dc4693665aed69710bd03d8a4fR42-R55), [[3]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-2ac966076011ae0ff50d6698ce04e1e1e7c878dc4693665aed69710bd03d8a4fR75-R79), [[4]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-8b94c978c19f4bdd92cc552b56bde06b10a2f27e9daae386a40636440db37b83R26), [[5]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-8b94c978c19f4bdd92cc552b56bde06b10a2f27e9daae386a40636440db37b83R39-R42))
  • The paging subsystem registers a handler for page faults (exception 14) to provide early diagnostics and safe halting on memory access violations. ([src/memory/paging.cR1-R166](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-3bf621908b857940f2ec9c1b9ede9a6d2d6f7cb9ec088174f8edd2a0020d5291R1-R166))

Build System and Linker Support:

  • Updated the Makefile to build and link the new PMM and paging modules into the kernel image. ([MakefileR72-R82](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-76ed074a9305c04054cdebb9e9aad2d818052b07091de1f20cad0bbac34ffb52R72-R82))
  • Modified boot/linker.ld to export __kernel_start and __kernel_end symbols, allowing the memory manager to reserve the kernel image in physical memory. ([[1]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-c8386fa8574eaea80121d2867e817e0e2952fa738dd4256803f6e57977385dc9R5-R6), [[2]](https://github.com/Retr0Aa/PrismOS/pull/10/files#diff-c8386fa8574eaea80121d2867e817e0e2952fa738dd4256803f6e57977385dc9R17-R18))

These changes collectively provide the kernel with foundational memory management capabilities and safer exception handling, preparing it for more advanced features and improved robustness.

@gogo1701 gogo1701 requested a review from Retr0Aa May 28, 2026 11:21
@gogo1701 gogo1701 self-assigned this May 28, 2026
Copilot AI review requested due to automatic review settings May 28, 2026 11:21

Copilot AI 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.

Pull request overview

This PR introduces foundational memory-management infrastructure for the PrismOS kernel by adding a bitmap-based Physical Memory Manager (PMM) and an early identity-mapped paging implementation, plus an extensible CPU exception handler registration path (used for early page-fault handling). It also updates the build and linker script to integrate these new subsystems and export kernel boundary symbols.

Changes:

  • Added pmm (bitmap-based physical page allocator) with Multiboot2 mmap parsing and region reservations.
  • Added early x86 paging with identity mapping, basic map/unmap/query helpers, and page-fault handler registration.
  • Extended IRQ/ISR handling to support per-exception (vectors 0–31) handler registration and dispatch.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/platform/io.h Adds CR0/CR2/CR3 accessors and invlpg for paging control.
src/memory/pmm.h Declares PMM API and stats structure.
src/memory/pmm.c Implements bitmap PMM init/allocation/free and region reservations.
src/memory/paging.h Declares early paging API and flags.
src/memory/paging.c Implements identity-mapped paging, page table provisioning, and page-fault handler hook.
src/kernel.c Wires PMM + paging initialization into kernel startup.
src/interrupts/irq.h Adds exception handler typedef + registration API.
src/interrupts/irq.c Implements exception handler table and dispatch in isr_handler.
Makefile Builds/links PMM and paging objects into the kernel image.
boot/linker.ld Exports __kernel_start/__kernel_end symbols for memory reservations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/kernel.c
Comment on lines +153 to 158
pmm_init(boot_info, &framebuffer);
paging_init(&framebuffer);

// Shell startup marks readiness for user commands.
DEBUG_LOG("starting shell loop");
interrupts_init();
Comment thread src/memory/pmm.c
Comment on lines +82 to +94
uint32_t start = align_down(start_addr, PAGE_SIZE);
uint32_t end = align_up(end_addr, PAGE_SIZE);

if (end <= start) {
return;
}

uint32_t start_page = start / PAGE_SIZE;
uint32_t end_page = end / PAGE_SIZE;

if (end_page > MAX_PAGES) {
end_page = MAX_PAGES;
}
Comment thread src/memory/pmm.c
Comment on lines +121 to +130
uint32_t start = align_up((uint32_t)start_addr, PAGE_SIZE);
uint32_t end = align_down((uint32_t)end_addr, PAGE_SIZE);

if (end <= start) {
return;
}

uint32_t start_page = start / PAGE_SIZE;
uint32_t end_page = end / PAGE_SIZE;

Comment thread src/memory/pmm.c
Comment on lines +229 to +233
if (fb_end64 > ((uint64_t)MAX_PHYS_ADDR + 1ULL)) {
fb_end64 = (uint64_t)MAX_PHYS_ADDR + 1ULL;
}
reserve_range((uint32_t)fb_start64, (uint32_t)fb_end64);
}
Comment thread src/memory/pmm.c
Comment on lines +243 to +249
pmm_stats.total_memory_bytes = (uint32_t)highest_phys;
pmm_stats.total_pages = pmm_stats.total_memory_bytes / PAGE_SIZE;
pmm_stats.used_pages = pmm_stats.total_pages - pmm_stats.free_pages;
pmm_stats.usable_memory_bytes = pmm_stats.free_pages * PAGE_SIZE;
pmm_stats.reserved_memory_bytes = (pmm_stats.total_pages - pmm_stats.free_pages) * PAGE_SIZE;

PRISMOS_TOTAL_MEMORY_KB = pmm_stats.total_memory_bytes / 1024U;
Comment thread src/memory/paging.c
Comment on lines +147 to +152
if (fb_start64 <= 0xFFFFFFFFULL) {
if (fb_end64 > 0x100000000ULL) {
fb_end64 = 0x100000000ULL;
}
map_identity_range((uint32_t)fb_start64, (uint32_t)fb_end64, PAGING_FLAG_READ_WRITE);
}
@gogo1701

Copy link
Copy Markdown
Collaborator Author

@Retr0Aa are you going to review it??

@Retr0Aa

Retr0Aa commented May 28, 2026

Copy link
Copy Markdown
Owner

@gogo1701 sybau

@Retr0Aa Retr0Aa closed this May 28, 2026
@Retr0Aa Retr0Aa reopened this May 28, 2026
@Retr0Aa Retr0Aa merged commit 34a8ce9 into main May 28, 2026
1 check passed
@Retr0Aa Retr0Aa deleted the pmm branch May 28, 2026 11:49
@gogo1701

Copy link
Copy Markdown
Collaborator Author

no @Retr0Aa

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.

3 participants