Skip to content

Commit e979aa3

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

File tree

10 files changed

+304
-195
lines changed

10 files changed

+304
-195
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5293,7 +5293,7 @@ name = "page_table"
52935293
version = "0.0.0"
52945294
dependencies = [
52955295
"bitfield-struct 0.11.0",
5296-
"tracing",
5296+
"thiserror 2.0.16",
52975297
"zerocopy 0.8.25",
52985298
]
52995299

tmk/tmk_vmm/src/load.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use vm_topology::processor::ProcessorTopology;
2222
use vm_topology::processor::aarch64::Aarch64Topology;
2323
use vm_topology::processor::x86::X86Topology;
2424
use zerocopy::FromBytes as _;
25+
use zerocopy::FromZeros;
2526
use zerocopy::IntoBytes;
2627

2728
/// Loads a TMK, returning the initial registers for the BSP.
@@ -38,19 +39,24 @@ pub fn load_x86(
3839
let load_info = load_common(&mut loader, tmk, test)?;
3940

4041
let page_table_base = load_info.next_available_address;
41-
let page_tables = page_table::x64::build_page_tables_64(
42+
let mut page_table_work_buffer: Vec<page_table::x64::PageTable> =
43+
vec![page_table::x64::PageTable::new_zeroed(); page_table::x64::PAGE_TABLE_MAX_COUNT];
44+
let mut page_tables: Vec<u8> = vec![0; page_table::x64::PAGE_TABLE_MAX_BYTES];
45+
let page_table_builder = page_table::x64::IdentityMapBuilder::new(
4246
page_table_base,
43-
0,
4447
page_table::IdentityMapSize::Size4Gb,
45-
None,
4648
);
49+
let page_tables = page_table_builder.build(
50+
page_table_work_buffer.as_mut_slice(),
51+
page_tables.as_mut_slice(),
52+
)?;
4753
loader
4854
.import_pages(
4955
page_table_base >> 12,
5056
page_tables.len() as u64 >> 12,
5157
"page_tables",
5258
loader::importer::BootPageAcceptance::Exclusive,
53-
&page_tables,
59+
page_tables,
5460
)
5561
.context("failed to import page tables")?;
5662

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ rust-version.workspace = true
88

99
[dependencies]
1010
bitfield-struct.workspace = true
11-
tracing.workspace = true
11+
thiserror.workspace = true
1212
zerocopy.workspace = true
1313
[lints]
1414
workspace = true

vm/loader/page_table/src/aarch64.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,6 @@ impl<'a> Arm64PageTableSpace<'a> {
437437
debug_assert!(aligned(phys_table_start, Arm64PageSize::Small));
438438
debug_assert!(index < PAGE_SIZE_4K as usize / size_of::<Arm64PageTableEntry>());
439439

440-
tracing::debug!(
441-
"Writing page table entry {entry:#016x}, index {index:#x}, table {phys_table_start:#x}"
442-
);
443-
444440
let pos = phys_table_start as usize - self.phys_page_table_root
445441
+ index * size_of::<Arm64PageTableEntry>();
446442
self.space[pos..pos + 8].copy_from_slice(&entry.to_le_bytes());
@@ -682,8 +678,8 @@ pub fn build_identity_page_tables_aarch64(
682678
start_gpa: u64,
683679
size: u64,
684680
memory_attribute_indirection: MemoryAttributeIndirectionEl1,
685-
page_table_region_size: usize,
686-
) -> Vec<u8> {
681+
page_table_space: &mut [u8],
682+
) -> &[u8] {
687683
// start_gpa and size must be 2MB aligned.
688684
if !aligned(start_gpa, Arm64PageSize::Large) {
689685
panic!("start_gpa not 2mb aligned");
@@ -693,13 +689,8 @@ pub fn build_identity_page_tables_aarch64(
693689
panic!("size not 2mb aligned");
694690
}
695691

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-
700-
let mut page_table_space = vec![0; page_table_region_size];
701692
let mut page_tables =
702-
Arm64PageTableSpace::new(page_table_gpa as usize, &mut page_table_space).unwrap();
693+
Arm64PageTableSpace::new(page_table_gpa as usize, page_table_space).unwrap();
703694
page_tables
704695
.map_range(
705696
start_gpa,
@@ -713,17 +704,16 @@ pub fn build_identity_page_tables_aarch64(
713704
.unwrap();
714705

715706
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());
718707

719-
page_table_space.truncate(used_space);
720-
721-
page_table_space
708+
&page_table_space[0..used_space]
722709
}
723710

724711
#[cfg(test)]
725712
mod tests {
713+
extern crate std;
714+
726715
use super::*;
716+
use std::vec;
727717

728718
const DUMP_PAGE_TABLES: bool = false;
729719

vm/loader/page_table/src/lib.rs

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

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

910
pub mod aarch64;
1011
pub mod x64;
1112

13+
use thiserror::Error;
14+
15+
/// Errors returned by the Page Table Builder
16+
#[derive(Debug, PartialEq, Eq, Error)]
17+
pub enum Error {
18+
/// The PageTableBuilder bytes buffer does not match the size of the struct buffer
19+
#[error(
20+
"PageTableBuilder bytes buffer size [{bytes_buf}] does not match the struct buffer size [{struct_buf}]"
21+
)]
22+
BadBufferSize { bytes_buf: usize, struct_buf: usize },
23+
}
24+
1225
/// Size of the initial identity map
1326
#[derive(Debug, Copy, Clone)]
1427
pub enum IdentityMapSize {

0 commit comments

Comments
 (0)