Skip to content

Commit 94b53cf

Browse files
committed
no_std page table builder
1 parent 0c211f1 commit 94b53cf

File tree

10 files changed

+216
-89
lines changed

10 files changed

+216
-89
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5293,7 +5293,6 @@ name = "page_table"
52935293
version = "0.0.0"
52945294
dependencies = [
52955295
"bitfield-struct 0.11.0",
5296-
"tracing",
52975296
"zerocopy 0.8.25",
52985297
]
52995298

vm/loader/manifests/openhcl-x64-cvm-dev.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@
5757
}
5858
}
5959
]
60-
}
60+
}

vm/loader/page_table/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ rust-version.workspace = true
88

99
[dependencies]
1010
bitfield-struct.workspace = true
11-
tracing.workspace = true
1211
zerocopy.workspace = true
1312
[lints]
1413
workspace = true

vm/loader/page_table/src/aarch64.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
//! Methods to construct page tables on Aarch64.
55
6+
use crate::PageTableBuffer;
67
use bitfield_struct::bitfield;
78

9+
//TODO(babayet2) nonsensical
10+
pub const MAX_PAGE_TABLE_REGION_SIZE: usize = 1024 * 1024;
11+
812
/// Some memory attributes. Refer to the ARM VMSA
913
/// manual for further details and other types.
1014
#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
@@ -437,10 +441,6 @@ impl<'a> Arm64PageTableSpace<'a> {
437441
debug_assert!(aligned(phys_table_start, Arm64PageSize::Small));
438442
debug_assert!(index < PAGE_SIZE_4K as usize / size_of::<Arm64PageTableEntry>());
439443

440-
tracing::debug!(
441-
"Writing page table entry {entry:#016x}, index {index:#x}, table {phys_table_start:#x}"
442-
);
443-
444444
let pos = phys_table_start as usize - self.phys_page_table_root
445445
+ index * size_of::<Arm64PageTableEntry>();
446446
self.space[pos..pos + 8].copy_from_slice(&entry.to_le_bytes());
@@ -677,13 +677,16 @@ impl<'a> Arm64PageTableSpace<'a> {
677677
}
678678

679679
/// Build a set of Aarch64 page tables identity mapping the given region.
680-
pub fn build_identity_page_tables_aarch64(
680+
pub fn build_identity_page_tables_aarch64<P>(
681681
page_table_gpa: u64,
682682
start_gpa: u64,
683683
size: u64,
684684
memory_attribute_indirection: MemoryAttributeIndirectionEl1,
685685
page_table_region_size: usize,
686-
) -> Vec<u8> {
686+
page_table_space: &mut P,
687+
) where
688+
P: PageTableBuffer<Element = u8>,
689+
{
687690
// start_gpa and size must be 2MB aligned.
688691
if !aligned(start_gpa, Arm64PageSize::Large) {
689692
panic!("start_gpa not 2mb aligned");
@@ -693,13 +696,12 @@ pub fn build_identity_page_tables_aarch64(
693696
panic!("size not 2mb aligned");
694697
}
695698

696-
tracing::debug!(
697-
"Creating Aarch64 page tables at {page_table_gpa:#x} mapping starting at {start_gpa:#x} of size {size} bytes"
698-
);
699+
for _ in 0..page_table_region_size {
700+
page_table_space.push(0);
701+
}
699702

700-
let mut page_table_space = vec![0; page_table_region_size];
701703
let mut page_tables =
702-
Arm64PageTableSpace::new(page_table_gpa as usize, &mut page_table_space).unwrap();
704+
Arm64PageTableSpace::new(page_table_gpa as usize, page_table_space.as_mut_slice()).unwrap();
703705
page_tables
704706
.map_range(
705707
start_gpa,
@@ -713,12 +715,8 @@ pub fn build_identity_page_tables_aarch64(
713715
.unwrap();
714716

715717
let used_space = page_tables.used_space();
716-
tracing::debug!("Page tables use {used_space} bytes");
717-
tracing::debug!("Page tables stats by level: {:?}", page_tables.lvl_stats());
718718

719719
page_table_space.truncate(used_space);
720-
721-
page_table_space
722720
}
723721

724722
#[cfg(test)]

vm/loader/page_table/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
//! Methods to construct page tables.
55
6+
#![no_std]
67
#![expect(missing_docs)]
78
#![forbid(unsafe_code)]
89

@@ -17,3 +18,27 @@ pub enum IdentityMapSize {
1718
/// Identity-map the bottom 8GB
1819
Size8Gb,
1920
}
21+
22+
/// A trait for an indexable, mutable, and extendable working memory buffer for page table building
23+
pub trait PageTableBuffer:
24+
core::ops::Index<usize, Output = Self::Element> + core::ops::IndexMut<usize, Output = Self::Element>
25+
{
26+
/// Associated Type defining the element type stored in the buffer
27+
type Element;
28+
29+
fn new() -> Self;
30+
31+
fn push(&mut self, item: Self::Element);
32+
33+
fn extend(&mut self, items: &[Self::Element]);
34+
35+
fn len(&self) -> usize;
36+
37+
fn as_slice(&self) -> &[Self::Element];
38+
39+
fn as_mut_slice(&mut self) -> &mut [Self::Element];
40+
41+
fn truncate(&mut self, new_len: usize);
42+
43+
fn iter_mut(&mut self) -> core::slice::IterMut<'_, Self::Element>;
44+
}

0 commit comments

Comments
 (0)