Skip to content

Conversation

@HeatCrab
Copy link
Collaborator

@HeatCrab HeatCrab commented Oct 29, 2025

This PR implements PMP (Physical Memory Protection) support for RISC-V to enable hardware-enforced memory isolation in Linmo, addressing #30.

Currently Phase 1 (infrastructure) is complete. This branch will continue development through the remaining phases. Phase 1 adds the foundational structures and declarations: PMP hardware layer in arch/riscv with CSR definitions and region management structures, architecture-independent memory abstractions (flex pages, address spaces, memory pools), kernel memory pool declarations from linker symbols, and TCB extension for address space linkage.

The actual PMP operations including region configuration, CSR manipulation, and context switching integration are not yet implemented.

TOR mode is used for its flexibility with arbitrary address ranges without alignment constraints, simplifying region management for task stacks of varying sizes. Priority-based eviction allows the system to manage competing demands when the 16 hardware regions are exhausted, ensuring critical kernel and stack regions remain protected while allowing temporary mappings to be reclaimed as needed.


Summary by cubic

Enables RISC-V PMP for hardware memory isolation (#30). Uses TOR mode with boot-time kernel protection, trap-time flexpage loading, per-task context switching, and U-mode kernel stack isolation via mscratch; unrecoverable access faults terminate the task instead of panicking.

  • New Features
    • PMP CSR and numeric accessors; TOR-mode region set/disable/lock/read and access checks with shadow state.
    • Kernel memory pools from linker symbols: text RX; data/bss RW (no execute).
    • Flexpages and memory spaces with on-demand load/evict and victim selection; TCB linked to a memory space.
    • U-mode kernel stack isolation via mscratch (ISR frame includes SP); trap handler is nested-trap safe and performs load/evict on access faults with task termination on unrecoverable faults; context switch swaps task regions.

Written for commit e34fac0. Summary will update on new commits.

Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

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

Use unified "flexpage" notation.

@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch from e264a35 to 4a62d5b Compare October 31, 2025 13:25
@HeatCrab
Copy link
Collaborator Author

Use unified "flexpage" notation.

Got it! Thanks for the correction and the L4 X.2 reference.
I've fixed all occurrences to use "flexpage" notation.

@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch 5 times, most recently from 109259d to f6c3912 Compare November 6, 2025 09:16
@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch 2 times, most recently from 2644558 to 1bb5fcf Compare November 16, 2025 13:18
jserv

This comment was marked as outdated.

@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch 6 times, most recently from 904e972 to ed800fc Compare November 21, 2025 12:38
@HeatCrab

This comment was marked as outdated.

jserv

This comment was marked as resolved.

@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch 6 times, most recently from 0d55f21 to 865a5d6 Compare November 22, 2025 08:36
@HeatCrab
Copy link
Collaborator Author

Rebase the latest 'main' branch to resolve rtsched issues.

Finished. And I removed the M-mode fault-handling commits, as they are not aligned with the upcoming work.
Next, I plan to start U-mode support (#19) on a new branch, and then circle back to complete the PMP development and apply any adjustments that may be needed after the U-mode integration.

@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch from 865a5d6 to 7e3992e Compare December 11, 2025 08:51
@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch 5 times, most recently from f1422f7 to 5fa0716 Compare January 30, 2026 06:45
jserv

This comment was marked as resolved.

@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch from 5fa0716 to 337fdb6 Compare January 30, 2026 07:38
@HeatCrab

This comment was marked as resolved.

jserv

This comment was marked as resolved.

@jserv jserv requested a review from visitorckw January 30, 2026 12:33
Establish the complete PMP infrastructure to support hardware-enforced
memory isolation. This implementation spans the driver layer, hardware
abstraction, and region lifecycle management.

Introduce `flexpages` to represent contiguous physical regions with
protection attributes, and `memory_spaces` to group them into per-task
protection domains. These abstractions decouple high-level memory
policies from low-level hardware constraints.

Address the RISC-V immediate value limitation for CSR instructions by
implementing a runtime-indexed access mechanism. A switch-case dispatch
system maps dynamic indices to static `pmpcfg` and `pmpaddr` instructions,
enabling iterative configuration of PMP entries. All regions use TOR
(Top-of-Range) mode to support arbitrary address alignment.

Implement a driver stack that maintains a centralized shadow configuration
to mirror hardware state. This serves as the single source of truth for
PMP operations, supporting atomic updates and dynamic region allocation.
The region management API handles the validation, locking, and eviction
of entries, while kernel memory pools are automatically secured at boot
using linker symbols (Text: RX, Data: RW).
@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch from 337fdb6 to 18841ee Compare January 31, 2026 09:38
@sysprog21 sysprog21 deleted a comment from cubic-dev-ai bot Jan 31, 2026
@sysprog21 sysprog21 deleted a comment from cubic-dev-ai bot Jan 31, 2026
cubic-dev-ai[bot]

This comment was marked as resolved.

cubic-dev-ai[bot]

This comment was marked as resolved.

@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch from 18841ee to 7b02e82 Compare January 31, 2026 14:27
Memory protection requires dynamic reconfiguration when switching
between tasks. Each task receives a dedicated memory space with its
stack registered as a protected flexpage. During context switches, the
scheduler evicts the outgoing task's regions from hardware slots and
loads the incoming task's regions, while kernel regions remain locked
across all transitions.

Kernel text, data, and BSS regions are configured at boot and protected
from eviction. User mode tasks operate in isolated memory domains where
they cannot access kernel memory or other tasks' stacks.

Nested trap handling is required for correct U-mode operation. When a
user mode syscall triggers a yield, the resulting nested trap must not
corrupt the outer trap's context. Trap nesting depth tracking ensures
only the outermost trap performs context switch restoration, and yield
from trap context invokes the scheduler directly without additional
trap nesting.

A test validates mixed-privilege context switching by spawning M-mode
and U-mode tasks that continuously yield, verifying correct operation
across privilege boundaries.
@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch from 7b02e82 to 18485e7 Compare January 31, 2026 14:38
Hardware PMP entries are limited resources, restricting the number of
simultaneous memory mappings. Additionally, unrecoverable access faults
currently panic the kernel, which compromises system availability during
multitasking.

Address the hardware implementation limit by loading memory regions
dynamically. When a task accesses a valid region not present in hardware,
the fault handler evicts a lower-priority entry to map the required
flexpage on demand. This decouples the number of task memory regions
from the physical PMP slot count.

Improve system stability by terminating tasks that trigger unrecoverable
faults. Instead of halting the entire system, the fault handler marks the
task as a zombie and signals the scheduler to maximize resource
reclamation. This ensures that isolation violations affect only the
faulting process.
Provides comprehensive documentation covering memory abstraction,
context switching, and fault handling for the PMP implementation.

The test application validates three core isolation guarantees.
Tasks verify stack integrity across context switches using guard
values. Destructive tests validate kernel memory protection and
inter-task isolation by attempting unauthorized accesses that
trigger fault handling and task termination.

CI scripts are modified to recognize expected output results.
Synchronization timing in mutex tests is adjusted to accommodate
context switch overhead from PMP configuration.
@HeatCrab HeatCrab force-pushed the pmp/memory-isolation branch from 18485e7 to e34fac0 Compare January 31, 2026 16:52
@jserv jserv merged commit d21ba4d into sysprog21:main Jan 31, 2026
6 checks passed
@jserv
Copy link
Contributor

jserv commented Jan 31, 2026

Thank @HeatCrab for contributing!

@HeatCrab HeatCrab deleted the pmp/memory-isolation branch February 1, 2026 04:46
@HeatCrab HeatCrab mentioned this pull request Feb 1, 2026
25 tasks
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