From 60ca26dcd28136d261cfa4500f7bf8a9f1e15ae7 Mon Sep 17 00:00:00 2001 From: Erin Date: Thu, 22 Jun 2023 11:37:48 +0200 Subject: [PATCH] table --- hbvm/src/main.rs | 12 ++++++++++-- hbvm/src/vm/mod.rs | 16 +++++++++++----- hbvm/src/vm/trap.rs | 3 +++ 3 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 hbvm/src/vm/trap.rs diff --git a/hbvm/src/main.rs b/hbvm/src/main.rs index a136f59b..05c13d33 100644 --- a/hbvm/src/main.rs +++ b/hbvm/src/main.rs @@ -1,3 +1,5 @@ +use hbvm::vm::trap::HandleTrap; + use { hbvm::{validate::validate, vm::Vm}, std::io::{stdin, Read}, @@ -6,13 +8,13 @@ use { fn main() -> Result<(), Box> { let mut prog = vec![]; stdin().read_to_end(&mut prog)?; - + if let Err(e) = validate(&prog) { eprintln!("Program validation error: {e:?}"); return Ok(()); } else { unsafe { - let mut vm = Vm::new_unchecked(&prog); + let mut vm = Vm::new_unchecked(&prog, TestTrapHandler); vm.memory.insert_test_page(); println!("Program interrupt: {:?}", vm.run()); println!("{:?}", vm.registers); @@ -24,3 +26,9 @@ fn main() -> Result<(), Box> { pub fn time() -> u32 { 9 } + +struct TestTrapHandler; +impl HandleTrap for TestTrapHandler { + #[inline] + fn page_fault(&mut self) {} +} diff --git a/hbvm/src/vm/mod.rs b/hbvm/src/vm/mod.rs index ccb60efa..ce21e70f 100644 --- a/hbvm/src/vm/mod.rs +++ b/hbvm/src/vm/mod.rs @@ -11,6 +11,10 @@ // program size. If you are (rightfully) worried about the UB, for now just // append your program with 11 zeroes. +use self::trap::HandleTrap; + +pub mod trap; + mod mem; mod value; @@ -72,28 +76,30 @@ macro_rules! cond_jump { }}; } -pub struct Vm<'a> { +pub struct Vm<'a, T> { pub registers: [Value; 256], pub memory: Memory, + pub traph: T, pc: usize, program: &'a [u8], } -impl<'a> Vm<'a> { +impl<'a, T: HandleTrap> Vm<'a, T> { /// # Safety /// Program code has to be validated - pub unsafe fn new_unchecked(program: &'a [u8]) -> Self { + pub unsafe fn new_unchecked(program: &'a [u8], traph: T) -> Self { Self { registers: [Value::from(0_u64); 256], memory: Default::default(), + traph, pc: 0, program, } } - pub fn new_validated(program: &'a [u8]) -> Result { + pub fn new_validated(program: &'a [u8], traph: T) -> Result { validate::validate(program)?; - Ok(unsafe { Self::new_unchecked(program) }) + Ok(unsafe { Self::new_unchecked(program, traph) }) } pub fn run(&mut self) -> HaltReason { diff --git a/hbvm/src/vm/trap.rs b/hbvm/src/vm/trap.rs new file mode 100644 index 00000000..309180d5 --- /dev/null +++ b/hbvm/src/vm/trap.rs @@ -0,0 +1,3 @@ +pub trait HandleTrap { + fn page_fault(&mut self) {} +}