diff --git a/hbvm/src/vm/mem/mod.rs b/hbvm/src/vm/mem/mod.rs index 476f835f..26dc8a36 100644 --- a/hbvm/src/vm/mem/mod.rs +++ b/hbvm/src/vm/mem/mod.rs @@ -3,6 +3,8 @@ mod paging; use self::paging::{PageTable, Permission, PtEntry}; use alloc::boxed::Box; +use super::trap::HandleTrap; + #[derive(Clone, Debug)] pub struct Memory { root_pt: *mut PageTable, @@ -52,7 +54,13 @@ impl Memory { } /// Load value from an address - pub unsafe fn load(&self, addr: u64, target: *mut u8, count: usize) -> Result<(), ()> { + pub unsafe fn load( + &self, + addr: u64, + target: *mut u8, + count: usize, + traph: &mut impl HandleTrap, + ) -> Result<(), ()> { self.memory_access( addr, target, @@ -64,17 +72,25 @@ impl Memory { ) }, |src, dst, count| core::ptr::copy_nonoverlapping(src, dst, count), + traph, ) } /// Store value to an address - pub unsafe fn store(&mut self, addr: u64, source: *const u8, count: usize) -> Result<(), ()> { + pub unsafe fn store( + &mut self, + addr: u64, + source: *const u8, + count: usize, + traph: &mut impl HandleTrap, + ) -> Result<(), ()> { self.memory_access( addr, source.cast_mut(), count, |perm| perm == Permission::Write, |dst, src, count| core::ptr::copy_nonoverlapping(src, dst, count), + traph, ) } @@ -125,8 +141,9 @@ impl Memory { src: u64, mut dst: *mut u8, len: usize, - permission_check: impl Fn(Permission) -> bool, - action: impl Fn(*mut u8, *mut u8, usize), + permission_check: fn(Permission) -> bool, + action: fn(*mut u8, *mut u8, usize), + traph: &mut impl HandleTrap, ) -> Result<(), ()> { for PageSplitResult { ptr, size, perm } in PageSplitter::new(src, len, self.root_pt) { if !permission_check(perm) { diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index ce21e70f..1ae52a17 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -208,6 +208,7 @@ impl<'a, T: HandleTrap> Vm<'a, T> { self.read_reg(base).as_u64() + off + n as u64, self.registers.as_mut_ptr().add(usize::from(dst) + n).cast(), usize::from(count).saturating_sub(n), + &mut self.traph, ) .is_err() { @@ -222,6 +223,7 @@ impl<'a, T: HandleTrap> Vm<'a, T> { self.read_reg(base).as_u64() + off, self.registers.as_ptr().add(usize::from(dst)).cast(), count.into(), + &mut self.traph, ) .is_err() {