2023-06-20 19:07:48 -05:00
|
|
|
use {
|
2023-07-25 16:43:06 -05:00
|
|
|
hbbytecode::valider::validate,
|
2023-08-07 20:03:15 -05:00
|
|
|
hbvm::{
|
2023-08-07 20:10:23 -05:00
|
|
|
softpaging::{paging::PageTable, HandlePageFault, PageSize, SoftPagedMem},
|
2023-08-07 20:03:15 -05:00
|
|
|
MemoryAccessReason, Vm,
|
|
|
|
},
|
2023-06-20 19:07:48 -05:00
|
|
|
std::io::{stdin, Read},
|
2023-04-22 13:00:19 -05:00
|
|
|
};
|
2023-04-18 18:08:30 -05:00
|
|
|
|
2023-06-20 19:07:48 -05:00
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let mut prog = vec![];
|
|
|
|
stdin().read_to_end(&mut prog)?;
|
2023-06-24 17:16:14 -05:00
|
|
|
|
2023-06-20 19:07:48 -05:00
|
|
|
if let Err(e) = validate(&prog) {
|
|
|
|
eprintln!("Program validation error: {e:?}");
|
|
|
|
return Ok(());
|
|
|
|
} else {
|
|
|
|
unsafe {
|
2023-08-07 19:48:47 -05:00
|
|
|
let mut vm =
|
|
|
|
Vm::<_, 0>::new_unchecked(&prog, SoftPagedMem::<TestTrapHandler>::default());
|
2023-07-20 13:47:50 -05:00
|
|
|
let data = {
|
|
|
|
let ptr = std::alloc::alloc_zeroed(std::alloc::Layout::from_size_align_unchecked(
|
|
|
|
4096, 4096,
|
|
|
|
));
|
|
|
|
if ptr.is_null() {
|
|
|
|
panic!("Alloc error tbhl");
|
|
|
|
}
|
|
|
|
ptr
|
|
|
|
};
|
|
|
|
|
|
|
|
vm.memory
|
|
|
|
.map(
|
|
|
|
data,
|
|
|
|
0,
|
2023-08-07 20:03:15 -05:00
|
|
|
hbvm::softpaging::paging::Permission::Write,
|
2023-07-20 13:47:50 -05:00
|
|
|
PageSize::Size4K,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
2023-06-20 19:07:48 -05:00
|
|
|
println!("Program interrupt: {:?}", vm.run());
|
|
|
|
println!("{:?}", vm.registers);
|
2023-07-25 12:20:35 -05:00
|
|
|
|
|
|
|
println!("{:?}", core::slice::from_raw_parts(data, 4096));
|
|
|
|
std::alloc::dealloc(
|
|
|
|
data,
|
|
|
|
std::alloc::Layout::from_size_align_unchecked(4096, 4096),
|
|
|
|
);
|
2023-07-20 13:47:50 -05:00
|
|
|
vm.memory.unmap(0).unwrap();
|
2023-06-20 19:07:48 -05:00
|
|
|
}
|
|
|
|
}
|
2023-04-22 16:06:33 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn time() -> u32 {
|
|
|
|
9
|
|
|
|
}
|
2023-06-24 17:16:14 -05:00
|
|
|
|
2023-08-07 19:48:47 -05:00
|
|
|
#[derive(Default)]
|
2023-06-24 17:16:14 -05:00
|
|
|
struct TestTrapHandler;
|
2023-07-11 10:04:48 -05:00
|
|
|
impl HandlePageFault for TestTrapHandler {
|
2023-06-24 17:16:14 -05:00
|
|
|
fn page_fault(
|
|
|
|
&mut self,
|
|
|
|
_: MemoryAccessReason,
|
2023-08-07 20:10:11 -05:00
|
|
|
_: &mut PageTable,
|
2023-06-24 17:16:14 -05:00
|
|
|
_: u64,
|
|
|
|
_: PageSize,
|
|
|
|
_: *mut u8,
|
|
|
|
) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|