Skip to content

Commit 83d9785

Browse files
committed
Clean up snapshot and page table code
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent c018c51 commit 83d9785

7 files changed

Lines changed: 111 additions & 276 deletions

File tree

src/hyperlight_common/src/arch/amd64/vmem.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ limitations under the License.
2828
use crate::vmem::{
2929
BasicMapping, CowMapping, MapRequest, MapResponse, Mapping, MappingKind, TableMovabilityBase,
3030
TableOps, TableReadOps, UpdateParent, UpdateParentNone, UpdateParentRoot, UpdateParentTable,
31-
modify_ptes, read_pte_if_present, require_pte_exist, write_entry_updating,
31+
Void, modify_ptes, read_pte_if_present, require_pte_exist, write_entry_updating,
3232
};
3333

3434
// Paging Flags
@@ -111,7 +111,7 @@ impl<Op: TableOps<TableMovability = crate::vmem::MayMoveTable>> TableMovability<
111111
UpdateParentRoot {}
112112
}
113113
}
114-
impl<Op: TableReadOps> TableMovability<Op, crate::vmem::Void> for crate::vmem::MayNotMoveTable {
114+
impl<Op: TableReadOps> TableMovability<Op, Void> for crate::vmem::MayNotMoveTable {
115115
type RootUpdateParent = UpdateParentNone;
116116
fn root_update_parent() -> Self::RootUpdateParent {
117117
UpdateParentNone {}
@@ -120,8 +120,8 @@ impl<Op: TableReadOps> TableMovability<Op, crate::vmem::Void> for crate::vmem::M
120120

121121
impl<
122122
Op: TableOps<TableMovability = crate::vmem::MayMoveTable>,
123-
P: crate::vmem::UpdateParent<Op, TableMoveInfo = Op::TableAddr>,
124-
> crate::vmem::UpdateParent<Op> for UpdateParentTable<Op, P>
123+
P: UpdateParent<Op, TableMoveInfo = Op::TableAddr>,
124+
> UpdateParent<Op> for UpdateParentTable<Op, P>
125125
{
126126
type TableMoveInfo = Op::TableAddr;
127127
type ChildType = UpdateParentTable<Op, Self>;
@@ -136,7 +136,7 @@ impl<
136136
}
137137
}
138138

139-
impl<Op: TableOps<TableMovability = crate::vmem::MayMoveTable>> crate::vmem::UpdateParent<Op>
139+
impl<Op: TableOps<TableMovability = crate::vmem::MayMoveTable>> UpdateParent<Op>
140140
for UpdateParentRoot
141141
{
142142
type TableMoveInfo = Op::TableAddr;

src/hyperlight_common/src/arch/i686/vmem.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ fn pte_for_table<Op: TableOps>(table_addr: Op::TableAddr) -> u64 {
7070
phys | PAGE_USER | PAGE_RW | PAGE_ACCESSED | PAGE_PRESENT
7171
}
7272

73-
// ---- Page table manipulation ----
74-
7573
/// # Safety
7674
/// Must not be called concurrently with other page table modifications.
7775
unsafe fn alloc_pte_if_needed<

src/hyperlight_common/src/vmem.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,7 @@ mod sealed {
385385
}
386386
use sealed::*;
387387

388-
/// A sealed trait used to collect some information about the marker
389-
/// structures [`MayMoveTable`] and [`MayNotMoveTable`].
390-
/// Implemented in each arch module.
388+
/// A sealed trait used to collect some information about the marker structures [`MayMoveTable`] and [`MayNotMoveTable`]
391389
pub trait TableMovability<Op: TableReadOps + ?Sized>:
392390
TableMovabilityBase<Op>
393391
+ arch::TableMovability<Op, <Self as TableMovabilityBase<Op>>::TableMoveInfo>

src/hyperlight_host/build.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ fn main() -> Result<()> {
105105
crashdump: { all(feature = "crashdump", target_arch = "x86_64") },
106106
// print_debug feature is aliased with debug_assertions to make it only available in debug-builds.
107107
print_debug: { all(feature = "print_debug", debug_assertions) },
108-
// gdb needs writable snapshot memory for debug access.
109-
unshared_snapshot_mem: { any(feature = "gdb", feature = "nanvix-unstable") },
108+
// the nanvix-unstable and gdb features both (only
109+
// temporarily!) need to use writable/un-shared snapshot
110+
// memories, and so can't share
111+
unshared_snapshot_mem: { any(feature = "nanvix-unstable", feature = "gdb") },
110112
}
111113

112114
#[cfg(feature = "build-metadata")]

src/hyperlight_host/src/hypervisor/hyperlight_vm/x86_64.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ impl HyperlightVm {
251251
}
252252

253253
/// Get the current base page table physical address from CR3.
254-
#[allow(dead_code)]
255254
pub(crate) fn get_root_pt(&self) -> Result<u64, AccessPageTableError> {
256255
let sregs = self.vm.sregs()?;
257256
// Mask off the flags bits

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ pub(crate) struct SandboxMemoryManager<S: SharedMemory> {
157157
pub(crate) struct GuestPageTableBuffer {
158158
buffer: std::cell::RefCell<Vec<u8>>,
159159
phys_base: usize,
160-
/// Byte offset from phys_base to the active PD root.
161-
/// Used on i686 to target different per-process PDs.
162-
#[cfg(feature = "i686-guest")]
160+
/// Byte offset from phys_base to the active root page table.
161+
/// For multi-root guests, each root's PD starts at a different
162+
/// offset in the buffer.
163163
root_offset: std::cell::Cell<usize>,
164164
}
165165

@@ -194,14 +194,7 @@ impl vmem::TableReadOps for GuestPageTableBuffer {
194194
}
195195

196196
fn root_table(&self) -> u64 {
197-
#[cfg(feature = "i686-guest")]
198-
{
199-
(self.phys_base + self.root_offset.get()) as u64
200-
}
201-
#[cfg(not(feature = "i686-guest"))]
202-
{
203-
self.phys_base as u64
204-
}
197+
(self.phys_base + self.root_offset.get()) as u64
205198
}
206199
}
207200

@@ -241,76 +234,16 @@ impl GuestPageTableBuffer {
241234
GuestPageTableBuffer {
242235
buffer: std::cell::RefCell::new(vec![0u8; PAGE_TABLE_SIZE]),
243236
phys_base,
244-
#[cfg(feature = "i686-guest")]
245237
root_offset: std::cell::Cell::new(0),
246238
}
247239
}
248240

249-
/// Set the root offset to target a specific PD root.
250-
/// Root i's PD starts at byte offset `i * PAGE_TABLE_SIZE` in the buffer.
251-
#[cfg(feature = "i686-guest")]
241+
/// Set the root offset to target a specific root page table.
242+
/// Root i starts at byte offset `i * PAGE_TABLE_SIZE` in the buffer.
252243
pub(crate) fn set_root_offset(&self, offset: usize) {
253244
self.root_offset.set(offset);
254245
}
255246

256-
/// Copy a range of bytes within the buffer.
257-
#[cfg(feature = "i686-guest")]
258-
pub(crate) fn copy_within(&self, src: std::ops::Range<usize>, dst: usize) {
259-
let mut buf = self.buffer.borrow_mut();
260-
buf.copy_within(src, dst);
261-
}
262-
263-
/// Finalize multi-root i686 page directories after all per-root
264-
/// mappings have been written into root 0 (kernel) and per-root
265-
/// PDs (user).
266-
///
267-
/// This performs three steps:
268-
/// 1. Copy kernel PDEs (0..`user_pde_start`) from root 0 to all
269-
/// other roots so every PD shares the same kernel page tables.
270-
/// 2. Map the scratch region into every additional root.
271-
/// 3. Set `PAGE_USER` on user-space PDEs (`user_pde_start`..scratch)
272-
/// in all roots so user-mode code can access its pages.
273-
///
274-
/// # Safety
275-
/// Caller must ensure the scratch parameters describe a valid region.
276-
#[cfg(feature = "i686-guest")]
277-
pub(crate) unsafe fn finalize_multi_root(
278-
&self,
279-
n_roots: usize,
280-
user_pde_start: usize,
281-
scratch_gpa: u64,
282-
scratch_gva: u64,
283-
scratch_len: u64,
284-
) {
285-
use hyperlight_common::vmem::{BasicMapping, MappingKind};
286-
287-
// Step 1: copy kernel PDEs from root 0 to all other roots.
288-
let kernel_pde_bytes = user_pde_start * 4;
289-
for i in 1..n_roots {
290-
self.copy_within(0..kernel_pde_bytes, i * PAGE_TABLE_SIZE);
291-
}
292-
293-
// Step 2: map scratch into all other roots.
294-
for i in 1..n_roots {
295-
self.set_root_offset(i * PAGE_TABLE_SIZE);
296-
unsafe {
297-
vmem::map(
298-
self,
299-
vmem::Mapping {
300-
phys_base: scratch_gpa,
301-
virt_base: scratch_gva,
302-
len: scratch_len,
303-
kind: MappingKind::Basic(BasicMapping {
304-
readable: true,
305-
writable: true,
306-
executable: false,
307-
}),
308-
user_accessible: false,
309-
},
310-
);
311-
}
312-
}
313-
}
314247
#[cfg(test)]
315248
#[allow(dead_code)]
316249
pub(crate) fn size(&self) -> usize {

0 commit comments

Comments
 (0)