Skip to content

Add stage-2 pagetables#16

Open
m4tx wants to merge 7 commits intoel1-supportfrom
stage-2
Open

Add stage-2 pagetables#16
m4tx wants to merge 7 commits intoel1-supportfrom
stage-2

Conversation

@m4tx
Copy link
Collaborator

@m4tx m4tx commented Jan 20, 2026

This makes the RITM memory invisible from the guest.

@m4tx m4tx force-pushed the stage-2 branch 22 times, most recently from d91e3dc to 32d3e8a Compare January 21, 2026 15:00
@m4tx m4tx requested review from qperret2 and qwandor January 21, 2026 15:02

fn setup_stage2() {
debug!("Setting up stage 2 page table");
let idmap = Box::new(PlatformImpl::make_stage2_pagetable());
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could avoid the heap allocation by putting it in a static spin::Once instead.

Copy link
Collaborator Author

@m4tx m4tx Feb 6, 2026

Choose a reason for hiding this comment

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

aarch64-paging still does heap allocation internally (https://github.com/google/aarch64-paging/blob/e36465b1b557303aed5474ba6f840fda65275598/src/paging.rs#L869), but sure - it's good to remove as many heap allocations as possible.

EDIT: the current code actually causes a memory leak - as this function is invoked on every CPU_ON or return from CPU_SUSPEND. Even better reason to use Once.

src/main.rs Outdated
const HEAP_SIZE: usize = 40 * PAGE_SIZE;
static HEAP: SpinMutex<[u8; HEAP_SIZE]> = SpinMutex::new([0; HEAP_SIZE]);

const SHARED_HEAP_SIZE: usize = 16 * PAGE_SIZE;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Will different platforms need different heap sizes?

Copy link
Collaborator Author

@m4tx m4tx Feb 6, 2026

Choose a reason for hiding this comment

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

Right, for real devices 16 * 4kB might be insufficient even to store the modified DTB. It doesn't hurt to move this constant to the Platform trait.

src/main.rs Outdated
/// Panics if the requested size is invalid or if the allocation fails.
pub fn shared_alloc(size: usize) -> &'static mut [u8] {
use core::alloc::Layout;
let layout = Layout::from_size_align(size, PAGE_SIZE).expect("invalid layout");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is the alignment fixed at PAGE_SIZE?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, a dirty leftover from the early versions of the code. Updated the function so that Layout can be passed instead. For the FDT itself, using 8 byte alignment, as per the docs: https://docs.kernel.org/arch/arm64/booting.html#setup-the-device-tree

m4tx added 2 commits February 6, 2026 10:44
This makes the RITM memory invisible from the guest.
@m4tx m4tx requested a review from qwandor February 6, 2026 16:30

// Linux requires the device tree to be "placed on an 8-byte boundary":
// https://docs.kernel.org/arch/arm64/booting.html#setup-the-device-tree
const FDT_ALIGNMENT: usize = 8;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this constant should be in a common module, not platform-specific.

.union(Stage2Attributes::MEMATTR_NORMAL)
.union(Stage2Attributes::S2AP_ACCESS_RW)
.union(Stage2Attributes::ACCESS_FLAG)
.union(Stage2Attributes::SH_INNER);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do these memory attributes need to be platform-specific?

// High MMIO
// We split this into two ranges to avoid mapping a full L0 entry (512 GiB) as a single
// block, which is not supported by the architecture (L0 blocks are not supported with 4 KiB
// pages without enabling FEAT_LPA2)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should probably add some logic in aarch64-paging to avoid this, perhaps with a flag.

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.

2 participants

Comments