Compare commits

..

42 commits

Author SHA1 Message Date
Erin bf78cc751a initial work to adhere spec in handling memory access faults 2023-06-21 01:56:26 +02:00
Erin c95deefcb2 change spec 2023-06-21 00:12:43 +02:00
Erin 2c20c7c859 removed UB 2023-06-20 19:00:23 +02:00
Erin f85e3eb062 updates spec 2023-06-19 19:51:48 +02:00
Erin 7f2676af91 Reimplemented memory instructions, deal with it. 2023-06-19 00:42:57 +02:00
Erin 4dd2052634 fix 2023-06-12 00:40:24 +02:00
Erin 8afb251950 a 2023-06-12 00:39:53 +02:00
Erin e4f84aadc0 assembly 2023-06-12 00:39:50 +02:00
Erin bdb596befb spec 2023-06-12 00:32:42 +02:00
Erin 9ee9d6e266 Start working on spec
(also i wanted to test if my comming signing works)
2023-06-11 23:34:53 +02:00
Erin c7b5512ada fixed label handling 2023-06-11 23:24:16 +02:00
Erin 059e2b4b66 Merged division with remainder 2023-06-11 23:06:31 +02:00
Erin b7ff456808 fix 2023-06-11 22:43:02 +02:00
Erin 1ec4a7a653 gone 2023-06-11 15:54:44 +02:00
Erin ed3bcba42e a 2023-06-11 15:42:53 +02:00
Erin 86df8ddc64 uhh oohh i guess it works 2023-06-11 14:47:49 +02:00
Erin 1263941560 c 2023-06-11 14:05:57 +02:00
Erin efdcd826ee fixed typo 2023-06-11 13:55:05 +02:00
Erin c873945681 stores 2023-06-11 13:47:33 +02:00
Erin 2e6e6b7939 Basic paging 2023-06-11 13:26:16 +02:00
Erin 2144c055d1 added safety notice 2023-06-10 16:46:04 +02:00
Erin f832f6a04a asdasdasdasdasdasdasdasd 2023-06-09 21:58:59 +02:00
Erin 36f4d31fb2 sus 2023-06-09 18:35:34 +02:00
Erin e1499fd5a1 CMP, CMPU, CMPI, CMPUI 2023-06-09 18:25:37 +02:00
Erin d32b9e7fba I guess I improved local labels? 2023-06-09 18:07:08 +02:00
Erin 8ba86db561 daily 2023-06-09 17:38:36 +02:00
Erin 642488cb64 fixed 2023-06-09 15:42:13 +02:00
Erin 82160af7af fixed labels, added comments 2023-06-09 13:33:17 +02:00
Erin 2a08362b52 Very quick and dirty labels.
I wanna sleep now, so... will fix later™
2023-06-09 02:42:00 +02:00
Erin 48353db26f added conditional jumps 2023-06-09 01:55:09 +02:00
Erin 417047806b Program interrupts, immediate binary ops and bitshifts. 2023-06-09 00:10:46 +02:00
Erin da6ad6d2c7 Small improvements 2023-06-08 23:23:23 +02:00
Erin a34f2fc9f8 Added zero register + relative jump 2023-06-08 00:52:24 +02:00
Erin fce7a96e50 VM exceptions 2023-06-08 00:27:53 +02:00
Erin 859e14daa6 Memory access 2023-06-08 00:25:38 +02:00
Erin 0fd3aee6b5 Assembler program 2023-06-07 15:17:45 +02:00
Erin cbb0ac2abe Assembla. 2023-06-07 15:07:37 +02:00
Erin 8675965ef5 Implement copy + docs for instructions 2023-06-07 00:02:27 +02:00
Erin 8d6e7af9d8 Validated VM creator 2023-06-06 23:08:26 +02:00
Erin bb3a472eeb Added valider 2023-06-06 22:56:28 +02:00
Erin e67d512f89 Implemented some portion of VM, missing validation 2023-06-06 22:03:37 +02:00
Erin a4b22e2053 Instruction redefinition 2023-05-31 01:51:04 +02:00
24 changed files with 506 additions and 1181 deletions

80
Cargo.lock generated
View file

@ -13,22 +13,6 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "allocator-api2"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56fc6cf8dc8c4158eed8649f9b8b0ea1518eb62b544fe9490d66fa0b349eafe9"
[[package]]
name = "ariadne"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72fe02fc62033df9ba41cba57ee19acf5e742511a140c7dbc3a873e19a19a1bd"
dependencies = [
"unicode-width",
"yansi",
]
[[package]] [[package]]
name = "beef" name = "beef"
version = "0.5.2" version = "0.5.2"
@ -41,12 +25,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "convert_case"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
[[package]] [[package]]
name = "delegate" name = "delegate"
version = "0.9.0" version = "0.9.0"
@ -58,19 +36,6 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "derive_more"
version = "0.99.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321"
dependencies = [
"convert_case",
"proc-macro2",
"quote",
"rustc_version",
"syn 1.0.109",
]
[[package]] [[package]]
name = "fnv" name = "fnv"
version = "1.0.7" version = "1.0.7"
@ -86,22 +51,10 @@ dependencies = [
"ahash", "ahash",
] ]
[[package]]
name = "hashbrown"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a"
dependencies = [
"ahash",
"allocator-api2",
]
[[package]] [[package]]
name = "hbasm" name = "hbasm"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"ariadne",
"hashbrown 0.14.0",
"hbbytecode", "hbbytecode",
"lasso", "lasso",
"logos", "logos",
@ -117,8 +70,7 @@ name = "hbvm"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"delegate", "delegate",
"derive_more", "hashbrown",
"hashbrown 0.13.2",
"hbbytecode", "hbbytecode",
"log", "log",
"paste", "paste",
@ -131,8 +83,7 @@ version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4644821e1c3d7a560fe13d842d13f587c07348a1a05d3a797152d41c90c56df2" checksum = "4644821e1c3d7a560fe13d842d13f587c07348a1a05d3a797152d41c90c56df2"
dependencies = [ dependencies = [
"ahash", "hashbrown",
"hashbrown 0.13.2",
] ]
[[package]] [[package]]
@ -212,21 +163,6 @@ version = "0.6.29"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]]
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [
"semver",
]
[[package]]
name = "semver"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
[[package]] [[package]]
name = "static_assertions" name = "static_assertions"
version = "1.1.0" version = "1.1.0"
@ -261,20 +197,8 @@ version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0" checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
[[package]]
name = "unicode-width"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "yansi"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec"

4
assets/example.asm Normal file
View file

@ -0,0 +1,4 @@
load 0 a0 ;; 05 00 A0
load 10 a1 ;; 05 10 A1
add a0 1 a0 ;; 01 A0 01 A0
jump_neq a0 a1 0 ;; a1 A0 A1 0

4
assets/example.bytes Normal file
View file

@ -0,0 +1,4 @@
load 10 A1
load 0 A0
add A0 1
jump_less_than A0 A1 0

View file

@ -5,14 +5,8 @@ edition = "2021"
[dependencies] [dependencies]
hbbytecode = { path = "../hbbytecode" } hbbytecode = { path = "../hbbytecode" }
paste = "1.0" lasso = "0.7"
hashbrown = "0.14.0" paste = "1.0"
ariadne = "0.3.0"
[dependencies.lasso]
version = "0.7"
default-features = false
features = ["no-std"]
[dependencies.logos] [dependencies.logos]
version = "0.13" version = "0.13"

View file

@ -1,18 +0,0 @@
jmp r0, start
start:
jmp r0, init_serial_port
-- Uses r20 to set the port
init_serial_port:
add r20, r30, r10
li r20, 00
-- outb(PORT + 1, 0x00); // Disable all interrupts
-- outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
-- outb(PORT + 0, 0x03); // Set divisor to 3 (lo byte) 38400 baud
-- outb(PORT + 1, 0x00); // (hi byte)
-- outb(PORT + 3, 0x03); // 8 bits, no parity, one stop bit
-- outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
-- outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
-- outb(PORT + 4, 0x1E); // Set in loopback mode, test the serial chip
-- outb(PORT + 0, 0xAE); // Test serial chip (send byte 0xAE and check if serial returns same byte)

View file

@ -1,65 +1,269 @@
#![no_std] use std::collections::HashMap;
#![feature(error_in_core)] use {
lasso::{Rodeo, Spur},
logos::{Lexer, Logos, Span},
std::fmt::{Display, Formatter},
};
extern crate alloc; macro_rules! tokendef {
($($opcode:literal),* $(,)?) => {
paste::paste! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Logos)]
#[logos(extras = Rodeo)]
#[logos(skip r"[ \t\f]+")]
#[logos(skip r"-- .*")]
pub enum Token {
$(#[token($opcode, |_| hbbytecode::opcode::[<$opcode:upper>])])*
OpCode(u8),
pub mod text; #[regex("[0-9]+", |l| l.slice().parse().ok())]
#[regex(
"-[0-9]+",
|lexer| {
Some(u64::from_ne_bytes(lexer.slice().parse::<i64>().ok()?.to_ne_bytes()))
},
)] Integer(u64),
mod macros; #[regex(
"r[0-9]+",
|lexer| match lexer.slice()[1..].parse() {
Ok(n) => Some(n),
_ => None
},
)] Register(u8),
use {alloc::vec::Vec, hashbrown::HashSet}; #[regex(
r"\p{XID_Start}\p{XID_Continue}*:",
|lexer| lexer.extras.get_or_intern(&lexer.slice()[..lexer.slice().len() - 1]),
)] Label(Spur),
#[derive(Default)] #[regex(
pub struct Assembler { r"\p{XID_Start}\p{XID_Continue}*",
pub buf: Vec<u8>, |lexer| lexer.extras.get_or_intern(lexer.slice()),
sub: HashSet<usize>, )] Symbol(Spur),
}
impl Assembler { #[token("\n")]
macros::impl_asm!( #[token(";")] ISep,
bbbb(p0: u8, p1: u8, p2: u8, p3: u8) #[token(",")] PSep,
=> [DIR, DIRF, FMAF],
bbb(p0: u8, p1: u8, p2: u8)
=> [ADD, SUB, MUL, AND, OR, XOR, SL, SR, SRS, CMP, CMPU, BRC, ADDF, SUBF, MULF],
bbdh(p0: u8, p1: u8, p2: impl Imm, p3: u16)
=> [LD, ST],
bbd(p0: u8, p1: u8, p2: impl Imm)
=> [ADDI, MULI, ANDI, ORI, XORI, SLI, SRI, SRSI, CMPI, CMPUI,
BMC, JEQ, JNE, JLT, JGT, JLTU, JGTU, ADDFI, MULFI],
bb(p0: u8, p1: u8)
=> [NEG, NOT, CP, SWA, NEGF, ITF, FTI],
bd(p0: u8, p1: impl Imm)
=> [LI, JMP],
n()
=> [NOP, ECALL],
);
}
pub trait Imm {
fn insert(&self, asm: &mut Assembler);
}
macro_rules! impl_imm_le_bytes {
($($ty:ty),* $(,)?) => {
$(
impl Imm for $ty {
#[inline(always)]
fn insert(&self, asm: &mut Assembler) {
asm.buf.extend(self.to_le_bytes());
}
} }
)* }
}; };
} }
impl_imm_le_bytes!(u64, i64, f64); #[rustfmt::skip]
tokendef![
"nop", "add", "sub", "mul", "and", "or", "xor", "sl", "sr", "srs", "cmp", "cmpu",
"dir", "neg", "not", "addi", "muli", "andi", "ori", "xori", "sli", "sri", "srsi",
"cmpi", "cmpui", "cp", "swa", "li", "ld", "st", "bmc", "brc", "jmp", "jeq", "jne",
"jlt", "jgt", "jltu", "jgtu", "ecall", "addf", "mulf", "dirf", "addfi", "mulfi",
];
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct Symbol(pub u64); pub enum ErrorKind {
impl Imm for Symbol { UnexpectedToken,
#[inline(always)] InvalidToken,
fn insert(&self, asm: &mut Assembler) { UnexpectedEnd,
asm.sub.insert(asm.buf.len()); InvalidSymbol,
asm.buf.extend(self.0.to_le_bytes()); }
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
pub kind: ErrorKind,
pub span: Span,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Error {:?} at {:?}", self.kind, self.span)
} }
} }
impl std::error::Error for Error {}
macro_rules! expect_matches {
($self:expr, $($pat:pat),* $(,)?) => {$(
let $pat = $self.next()?
else { return Err(ErrorKind::UnexpectedToken) };
)*}
}
pub fn assembly(code: &str, buf: &mut Vec<u8>) -> Result<(), Error> {
struct Assembler<'a> {
lexer: Lexer<'a, Token>,
buf: &'a mut Vec<u8>,
label_map: HashMap<Spur, u64>,
to_sub_label: HashMap<usize, Spur>,
}
impl<'a> Assembler<'a> {
fn next(&mut self) -> Result<Token, ErrorKind> {
match self.lexer.next() {
Some(Ok(t)) => Ok(t),
Some(Err(())) => Err(ErrorKind::InvalidToken),
None => Err(ErrorKind::UnexpectedEnd),
}
}
fn assemble(&mut self) -> Result<(), ErrorKind> {
use hbbytecode::opcode::*;
loop {
match self.lexer.next() {
Some(Ok(Token::OpCode(op))) => {
self.buf.push(op);
match op {
NOP | ECALL => Ok(()),
DIR | DIRF => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
Token::Register(r2),
Token::PSep,
Token::Register(r3),
);
self.buf.extend([r0, r1, r2, r3]);
Ok(())
}
ADD..=CMPU | ADDF..=MULF => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
Token::Register(r2),
);
self.buf.extend([r0, r1, r2]);
Ok(())
}
BRC => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
Token::Integer(count),
);
self.buf.extend([
r0,
r1,
u8::try_from(count).map_err(|_| ErrorKind::UnexpectedToken)?,
]);
Ok(())
}
NEG..=NOT | CP..=SWA => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
);
self.buf.extend([r0, r1]);
Ok(())
}
LI | JMP => {
expect_matches!(self, Token::Register(r0), Token::PSep);
self.buf.push(r0);
self.insert_imm()?;
Ok(())
}
ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
);
self.buf.extend([r0, r1]);
self.insert_imm()?;
Ok(())
}
LD..=ST => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
Token::Integer(offset),
Token::PSep,
Token::Integer(len),
);
self.buf.extend([r0, r1]);
self.buf.extend(offset.to_le_bytes());
self.buf.extend(
u16::try_from(len)
.map_err(|_| ErrorKind::InvalidToken)?
.to_le_bytes(),
);
Ok(())
}
_ => unreachable!(),
}?;
match self.next() {
Ok(Token::ISep) => (),
Ok(_) => return Err(ErrorKind::UnexpectedToken),
Err(ErrorKind::UnexpectedEnd) => return Ok(()),
Err(e) => return Err(e),
}
}
Some(Ok(Token::Label(lbl))) => {
self.label_map.insert(lbl, self.buf.len() as u64);
}
Some(Ok(Token::ISep)) => (),
Some(Ok(_)) => return Err(ErrorKind::UnexpectedToken),
Some(Err(())) => return Err(ErrorKind::InvalidToken),
None => return Ok(()),
}
}
}
fn link_local_syms(&mut self) -> Result<(), ErrorKind> {
for (ix, sym) in &self.to_sub_label {
self.label_map
.get(sym)
.ok_or(ErrorKind::InvalidSymbol)?
.to_le_bytes()
.iter()
.enumerate()
.for_each(|(i, b)| {
self.buf[ix + i] = *b;
});
}
Ok(())
}
fn insert_imm(&mut self) -> Result<(), ErrorKind> {
let imm = match self.next()? {
Token::Integer(i) => i.to_le_bytes(),
Token::Symbol(s) => {
self.to_sub_label.insert(self.buf.len(), s);
[0; 8]
}
_ => return Err(ErrorKind::UnexpectedToken),
};
self.buf.extend(imm);
Ok(())
}
}
let mut asm = Assembler {
lexer: Token::lexer(code),
label_map: Default::default(),
to_sub_label: Default::default(),
buf,
};
asm.assemble().map_err(|kind| Error {
kind,
span: asm.lexer.span(),
})?;
asm.link_local_syms()
.map_err(|kind| Error { kind, span: 0..0 })
}

View file

@ -1,72 +0,0 @@
macro_rules! impl_asm_opcodes {
(
$generic:ident
($($param_i:ident: $param_ty:ty),*)
=> []
) => {};
(
$generic:ident
($($param_i:ident: $param_ty:ty),*)
=> [$opcode:ident, $($rest:tt)*]
) => {
paste::paste! {
#[allow(dead_code)]
#[inline(always)]
pub fn [<i_ $opcode:lower>](&mut self, $($param_i: $param_ty),*) {
self.$generic(hbbytecode::opcode::$opcode, $($param_i),*)
}
}
macros::impl_asm_opcodes!(
$generic($($param_i: $param_ty),*)
=> [$($rest)*]
);
};
}
macro_rules! gen_impl_asm_insert {
($($ty:ident),* $(,)?) => {
macro_rules! impl_asm_insert {
$(($self:expr, $id:ident, $ty) => {
$self.buf.extend($id.to_le_bytes())
};)*
($self:expr, $id:ident, $_:ty) => {
Imm::insert(&$id, $self)
};
}
};
}
gen_impl_asm_insert!(u8, u16, u64);
macro_rules! impl_asm {
(
$(
$ityn:ident
($($param_i:ident: $param_ty:ty),* $(,)?)
=> [$($opcode:ident),* $(,)?],
)*
) => {
paste::paste! {
$(
#[allow(dead_code)]
fn [<i_param_ $ityn>](&mut self, opcode: u8, $($param_i: $param_ty),*) {
self.buf.push(opcode);
$(macros::impl_asm_insert!(self, $param_i, $param_ty);)*
}
macros::impl_asm_opcodes!(
[<i_param_ $ityn>]($($param_i: $param_ty),*)
=> [$($opcode,)*]
);
)*
}
};
}
pub(super) use {impl_asm, impl_asm_opcodes};
#[allow(clippy::single_component_path_imports)]
pub(super) use impl_asm_insert;

View file

@ -1,9 +1,6 @@
use { use std::{
ariadne::{ColorGenerator, Label, Report, ReportKind, Source}, error::Error,
std::{ io::{stdin, stdout, Read, Write},
error::Error,
io::{stdin, Read},
},
}; };
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
@ -11,36 +8,14 @@ fn main() -> Result<(), Box<dyn Error>> {
stdin().read_to_string(&mut code)?; stdin().read_to_string(&mut code)?;
let mut buf = vec![]; let mut buf = vec![];
if let Err(e) = hbasm::assembly(&code, &mut buf) {
if let Err(e) = hbasm::text::assembly(&code, &mut buf) { eprintln!(
let mut colors = ColorGenerator::new(); "Error {:?} at {:?} (`{}`)",
e.kind,
let e_code = match e.kind { e.span.clone(),
hbasm::text::ErrorKind::UnexpectedToken => 1, &code[e.span],
hbasm::text::ErrorKind::InvalidToken => 2, );
hbasm::text::ErrorKind::UnexpectedEnd => 3,
hbasm::text::ErrorKind::InvalidSymbol => 4,
};
let message = match e.kind {
hbasm::text::ErrorKind::UnexpectedToken => "This token is not expected!",
hbasm::text::ErrorKind::InvalidToken => "The token is not valid!",
hbasm::text::ErrorKind::UnexpectedEnd => "The assembler reached the end of input unexpectedly!",
hbasm::text::ErrorKind::InvalidSymbol => "This referenced symbol doesn't have a corresponding label!",
};
let a = colors.next();
Report::build(ReportKind::Error, "engine_internal", e.span.clone().start)
.with_code(e_code)
.with_message(format!("{:?}", e.kind))
.with_label(
Label::new(("engine_internal", e.span.clone()))
.with_message(message)
.with_color(a),
)
.finish()
.eprint(("engine_internal", Source::from(&code)))
.unwrap();
} }
stdout().write_all(&buf)?;
Ok(()) Ok(())
} }

View file

@ -1,273 +0,0 @@
extern crate alloc;
use alloc::vec::Vec;
use {
core::fmt::{Display, Formatter},
hashbrown::HashMap,
lasso::{Rodeo, Spur},
logos::{Lexer, Logos, Span},
};
macro_rules! tokendef {
($($opcode:literal),* $(,)?) => {
paste::paste! {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Logos)]
#[logos(extras = Rodeo)]
#[logos(skip r"[ \t\f]+")]
#[logos(skip r"-- .*")]
pub enum Token {
$(#[token($opcode, |_| hbbytecode::opcode::[<$opcode:upper>])])*
OpCode(u8),
#[regex("[0-9]+", |l| l.slice().parse().ok())]
#[regex(
"-[0-9]+",
|lexer| {
Some(u64::from_ne_bytes(lexer.slice().parse::<i64>().ok()?.to_ne_bytes()))
},
)] Integer(u64),
#[regex(
"r[0-9]+",
|lexer| match lexer.slice()[1..].parse() {
Ok(n) => Some(n),
_ => None
},
)] Register(u8),
#[regex(
r"\p{XID_Start}\p{XID_Continue}*:",
|lexer| lexer.extras.get_or_intern(&lexer.slice()[..lexer.slice().len() - 1]),
)] Label(Spur),
#[regex(
r"\p{XID_Start}\p{XID_Continue}*",
|lexer| lexer.extras.get_or_intern(lexer.slice()),
)] Symbol(Spur),
#[token("\n")]
#[token(";")] ISep,
#[token(",")] PSep,
}
}
};
}
#[rustfmt::skip]
tokendef![
"nop", "add", "sub", "mul", "and", "or", "xor", "sl", "sr", "srs", "cmp", "cmpu",
"dir", "neg", "not", "addi", "muli", "andi", "ori", "xori", "sli", "sri", "srsi",
"cmpi", "cmpui", "cp", "swa", "li", "ld", "st", "bmc", "brc", "jmp", "jeq", "jne",
"jlt", "jgt", "jltu", "jgtu", "ecall", "addf", "subf", "mulf", "dirf", "fmaf", "negf",
"itf", "fti", "addfi", "mulfi",
];
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ErrorKind {
UnexpectedToken,
InvalidToken,
UnexpectedEnd,
InvalidSymbol,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Error {
pub kind: ErrorKind,
pub span: Span,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "Error {:?} at {:?}", self.kind, self.span)
}
}
impl core::error::Error for Error {}
macro_rules! expect_matches {
($self:expr, $($pat:pat),* $(,)?) => {$(
let $pat = $self.next()?
else { return Err(ErrorKind::UnexpectedToken) };
)*}
}
pub fn assembly(code: &str, buf: &mut Vec<u8>) -> Result<(), Error> {
struct Assembler<'a> {
lexer: Lexer<'a, Token>,
buf: &'a mut Vec<u8>,
label_map: HashMap<Spur, u64>,
to_sub_label: HashMap<usize, Spur>,
}
impl<'a> Assembler<'a> {
fn next(&mut self) -> Result<Token, ErrorKind> {
match self.lexer.next() {
Some(Ok(t)) => Ok(t),
Some(Err(())) => Err(ErrorKind::InvalidToken),
None => Err(ErrorKind::UnexpectedEnd),
}
}
fn assemble(&mut self) -> Result<(), ErrorKind> {
use hbbytecode::opcode::*;
loop {
match self.lexer.next() {
Some(Ok(Token::OpCode(op))) => {
self.buf.push(op);
match op {
NOP | ECALL => Ok(()),
DIR | DIRF => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
Token::Register(r2),
Token::PSep,
Token::Register(r3),
);
self.buf.extend([r0, r1, r2, r3]);
Ok(())
}
ADD..=CMPU | ADDF..=MULF => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
Token::Register(r2),
);
self.buf.extend([r0, r1, r2]);
Ok(())
}
BRC => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
Token::Integer(count),
);
self.buf.extend([
r0,
r1,
u8::try_from(count).map_err(|_| ErrorKind::UnexpectedToken)?,
]);
Ok(())
}
NEG..=NOT | CP..=SWA | NEGF..=FTI => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
);
self.buf.extend([r0, r1]);
Ok(())
}
LI | JMP => {
expect_matches!(self, Token::Register(r0), Token::PSep);
self.buf.push(r0);
self.insert_imm()?;
Ok(())
}
ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
);
self.buf.extend([r0, r1]);
self.insert_imm()?;
Ok(())
}
LD..=ST => {
expect_matches!(
self,
Token::Register(r0),
Token::PSep,
Token::Register(r1),
Token::PSep,
Token::Integer(offset),
Token::PSep,
Token::Integer(len),
);
self.buf.extend([r0, r1]);
self.buf.extend(offset.to_le_bytes());
self.buf.extend(
u16::try_from(len)
.map_err(|_| ErrorKind::InvalidToken)?
.to_le_bytes(),
);
Ok(())
}
_ => unreachable!(),
}?;
match self.next() {
Ok(Token::ISep) => (),
Ok(_) => return Err(ErrorKind::UnexpectedToken),
Err(ErrorKind::UnexpectedEnd) => return Ok(()),
Err(e) => return Err(e),
}
}
Some(Ok(Token::Label(lbl))) => {
self.label_map.insert(lbl, self.buf.len() as u64);
}
Some(Ok(Token::ISep)) => (),
Some(Ok(_)) => return Err(ErrorKind::UnexpectedToken),
Some(Err(())) => return Err(ErrorKind::InvalidToken),
None => return Ok(()),
}
}
}
fn link_local_syms(&mut self) -> Result<(), ErrorKind> {
for (ix, sym) in &self.to_sub_label {
self.label_map
.get(sym)
.ok_or(ErrorKind::InvalidSymbol)?
.to_le_bytes()
.iter()
.enumerate()
.for_each(|(i, b)| {
self.buf[ix + i] = *b;
});
}
Ok(())
}
fn insert_imm(&mut self) -> Result<(), ErrorKind> {
let imm = match self.next()? {
Token::Integer(i) => i.to_le_bytes(),
Token::Symbol(s) => {
self.to_sub_label.insert(self.buf.len(), s);
[0; 8]
}
_ => return Err(ErrorKind::UnexpectedToken),
};
self.buf.extend(imm);
Ok(())
}
}
let mut asm = Assembler {
lexer: Token::lexer(code),
label_map: Default::default(),
to_sub_label: Default::default(),
buf,
};
asm.assemble().map_err(|kind| Error {
kind,
span: asm.lexer.span(),
})?;
asm.link_local_syms()
.map_err(|kind| Error { kind, span: 0..0 })
}

View file

@ -4,57 +4,57 @@
#pragma once #pragma once
#include <assert.h> #include <assert.h>
#include <limits.h>
#include <stdint.h> #include <stdint.h>
static_assert(CHAR_BIT == 8, "Cursed architectures are not supported"); typedef enum hbbc_Opcode: uint8_t {
hbbc_Op_NOP, hbbc_Op_ADD, hbbc_Op_MUL, hbbc_Op_AND, hbbc_Op_OR, hbbc_Op_XOR, hbbc_Op_SL,
enum hbbc_Opcode: uint8_t { hbbc_Op_SR, hbbc_Op_SRS, hbbc_Op_CMP, hbbc_Op_CMPU, hbbc_Op_DIR, hbbc_Op_NEG, hbbc_Op_NOT,
hbbc_Op_NOP , hbbc_Op_ADD , hbbc_Op_SUB , hbbc_Op_MUL , hbbc_Op_AND , hbbc_Op_OR , hbbc_Op_ADDI, hbbc_Op_MULI, hbbc_Op_ANDI, hbbc_Op_ORI, hbbc_Op_XORI, hbbc_Op_SLI, hbbc_Op_SRI,
hbbc_Op_XOR , hbbc_Op_SL , hbbc_Op_SR , hbbc_Op_SRS , hbbc_Op_CMP , hbbc_Op_CMPU , hbbc_Op_SRSI, hbbc_Op_CMPI, hbbc_Op_CMPUI, hbbc_Op_CP, hbbc_Op_SWA, hbbc_Op_LI, hbbc_Op_LD,
hbbc_Op_DIR , hbbc_Op_NEG , hbbc_Op_NOT , hbbc_Op_ADDI , hbbc_Op_MULI , hbbc_Op_ANDI , hbbc_Op_ST, hbbc_Op_BMC, hbbc_Op_BRC, hbbc_Op_JMP, hbbc_Op_JEQ, hbbc_Op_JNE, hbbc_Op_JLT,
hbbc_Op_ORI , hbbc_Op_XORI , hbbc_Op_SLI , hbbc_Op_SRI , hbbc_Op_SRSI , hbbc_Op_CMPI , hbbc_Op_JGT, hbbc_Op_JLTU, hbbc_Op_JGTU, hbbc_Op_ECALL, hbbc_Op_ADDF, hbbc_Op_MULF,
hbbc_Op_CMPUI , hbbc_Op_CP , hbbc_Op_SWA , hbbc_Op_LI , hbbc_Op_LD , hbbc_Op_ST , hbbc_Op_DIRF, hbbc_Op_ADDFI, hbbc_Op_MULFI,
hbbc_Op_BMC , hbbc_Op_BRC , hbbc_Op_JMP , hbbc_Op_JEQ , hbbc_Op_JNE , hbbc_Op_JLT , } hbbc_Opcode;
hbbc_Op_JGT , hbbc_Op_JLTU , hbbc_Op_JGTU , hbbc_Op_ECALL , hbbc_Op_ADDF , hbbc_Op_SUBF ,
hbbc_Op_MULF , hbbc_Op_DIRF , hbbc_Op_FMAF , hbbc_Op_NEGF , hbbc_Op_ITF , hbbc_Op_FTI ,
hbbc_Op_ADDFI , hbbc_Op_MULFI ,
} typedef hbbc_Opcode;
static_assert(sizeof(hbbc_Opcode) == 1); static_assert(sizeof(hbbc_Opcode) == 1);
#pragma pack(push, 1) #pragma pack(push, 1)
struct hbbc_ParamBBBB typedef struct hbbc_ParamBBBB
{ uint8_t _0; uint8_t _1; uint8_t _2; uint8_t _3; } { uint8_t _0; uint8_t _1; uint8_t _2; uint8_t _3; }
typedef hbbc_ParamBBBB; hbbc_ParamBBBB;
static_assert(sizeof(hbbc_ParamBBBB) == 32 / 8); static_assert(sizeof(hbbc_ParamBBBB) == 4);
struct hbbc_ParamBBB typedef struct hbbc_ParamBBB
{ uint8_t _0; uint8_t _1; uint8_t _2; } { uint8_t _0; uint8_t _1; uint8_t _2; }
typedef hbbc_ParamBBB; hbbc_ParamBBB;
static_assert(sizeof(hbbc_ParamBBB) == 24 / 8); static_assert(sizeof(hbbc_ParamBBB) == 3);
struct hbbc_ParamBBDH typedef struct hbbc_ParamBBDH
{ uint8_t _0; uint8_t _1; uint64_t _2; uint16_t _3; } { uint8_t _0; uint8_t _1; uint64_t _2; uint16_t _3; }
typedef hbbc_ParamBBDH; hbbc_ParamBBDH;
static_assert(sizeof(hbbc_ParamBBDH) == 96 / 8); static_assert(sizeof(hbbc_ParamBBDH) == 12);
struct hbbc_ParamBBD typedef struct hbbc_ParamBBDB
{ uint8_t _0; uint8_t _1; uint64_t _2; uint8_t _3; }
hbbc_ParamBBDB;
static_assert(sizeof(hbbc_ParamBBDB) == 11);
typedef struct hbbc_ParamBBD
{ uint8_t _0; uint8_t _1; uint64_t _2; } { uint8_t _0; uint8_t _1; uint64_t _2; }
typedef hbbc_ParamBBD; hbbc_ParamBBD;
static_assert(sizeof(hbbc_ParamBBD) == 80 / 8); static_assert(sizeof(hbbc_ParamBBD) == 10);
struct hbbc_ParamBB typedef struct hbbc_ParamBB
{ uint8_t _0; uint8_t _1; } { uint8_t _0; uint8_t _1; }
typedef hbbc_ParamBB; hbbc_ParamBB;
static_assert(sizeof(hbbc_ParamBB) == 16 / 8); static_assert(sizeof(hbbc_ParamBB) == 2);
struct hbbc_ParamBD typedef struct hbbc_ParamBD
{ uint8_t _0; uint64_t _1; } { uint8_t _0; uint64_t _1; }
typedef hbbc_ParamBD; hbbc_ParamBD;
static_assert(sizeof(hbbc_ParamBD) == 72 / 8); static_assert(sizeof(hbbc_ParamBD) == 9);
typedef uint64_t hbbc_ParamD; typedef uint64_t hbbc_ParamD;
static_assert(sizeof(hbbc_ParamD) == 64 / 8); static_assert(sizeof(hbbc_ParamD) == 8);
#pragma pack(pop) #pragma pack(pop)

View file

@ -32,7 +32,7 @@ constmod!(pub opcode(u8) {
CMP = 10, "BBB; #0 ← #1 <=> #2"; CMP = 10, "BBB; #0 ← #1 <=> #2";
CMPU = 11, "BBB; #0 ← #1 <=> #2 (unsigned)"; CMPU = 11, "BBB; #0 ← #1 <=> #2 (unsigned)";
DIR = 12, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3"; DIR = 12, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
NEG = 13, "BB; #0 ← -#1"; NEG = 13, "BB; #0 ← ~#1";
NOT = 14, "BB; #0 ← !#1"; NOT = 14, "BB; #0 ← !#1";
ADDI = 15, "BBD; #0 ← #1 + imm #2"; ADDI = 15, "BBD; #0 ← #1 + imm #2";
@ -63,17 +63,12 @@ constmod!(pub opcode(u8) {
JGTU = 38, "BBD; if #0 > #1 → jump imm #2 (unsigned)"; JGTU = 38, "BBD; if #0 > #1 → jump imm #2 (unsigned)";
ECALL = 39, "N; Issue system call"; ECALL = 39, "N; Issue system call";
ADDF = 40, "BBB; #0 ← #1 +. #2"; ADDF = 40, "BBB; #0 ← #1 +. #2";
SUBF = 41, "BBB; #0 ← #1 -. #2"; MULF = 41, "BBB; #0 ← #1 +. #2";
MULF = 42, "BBB; #0 ← #1 +. #2"; DIRF = 42, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
DIRF = 43, "BBBB; #0 ← #2 / #3, #1 ← #2 % #3";
FMAF = 44, "BBBB; #0 ← (#1 * #2) + #3";
NEGF = 45, "BB; #0 ← -#1";
ITF = 46, "BB; #0 ← #1 as float";
FTI = 47, "BB; #0 ← #1 as int";
ADDFI = 48, "BBD; #0 ← #1 +. imm #2"; ADDFI = 43, "BBD; #0 ← #1 +. imm #2";
MULFI = 49, "BBD; #0 ← #1 *. imm #2"; MULFI = 44, "BBD; #0 ← #1 *. imm #2";
}); });
#[repr(packed)] #[repr(packed)]
@ -85,6 +80,9 @@ pub struct ParamBBB(pub u8, pub u8, pub u8);
#[repr(packed)] #[repr(packed)]
pub struct ParamBBDH(pub u8, pub u8, pub u64, pub u16); pub struct ParamBBDH(pub u8, pub u8, pub u64, pub u16);
#[repr(packed)]
pub struct ParamBBDB(pub u8, pub u8, pub u64, pub u8);
#[repr(packed)] #[repr(packed)]
pub struct ParamBBD(pub u8, pub u8, pub u64); pub struct ParamBBD(pub u8, pub u8, pub u64);
@ -99,6 +97,7 @@ pub struct ParamBD(pub u8, pub u64);
pub unsafe trait OpParam {} pub unsafe trait OpParam {}
unsafe impl OpParam for ParamBBBB {} unsafe impl OpParam for ParamBBBB {}
unsafe impl OpParam for ParamBBB {} unsafe impl OpParam for ParamBBB {}
unsafe impl OpParam for ParamBBDB {}
unsafe impl OpParam for ParamBBDH {} unsafe impl OpParam for ParamBBDH {}
unsafe impl OpParam for ParamBBD {} unsafe impl OpParam for ParamBBD {}
unsafe impl OpParam for ParamBB {} unsafe impl OpParam for ParamBB {}

View file

@ -8,7 +8,6 @@ lto = true
[dependencies] [dependencies]
delegate = "0.9" delegate = "0.9"
derive_more = "0.99"
hashbrown = "0.13" hashbrown = "0.13"
hbbytecode.path = "../hbbytecode" hbbytecode.path = "../hbbytecode"
log = "0.4" log = "0.4"

View file

View file

@ -1,7 +1,22 @@
#![doc = include_str!("../README.md")]
#![no_std] #![no_std]
extern crate alloc; extern crate alloc;
pub mod validate; pub mod validate;
pub mod vm; pub mod vm;
#[derive(Debug, PartialEq)]
pub enum RuntimeErrors {
InvalidOpcodePair(u8, u8),
RegisterTooSmall,
HostError(u64),
PageNotMapped(u64),
InvalidJumpAddress(u64),
InvalidSystemCall(u8),
}
// If you solve the halting problem feel free to remove this
#[derive(PartialEq, Debug)]
pub enum HaltStatus {
Halted,
Running,
}

View file

@ -1,9 +1,3 @@
use hbvm::vm::{
mem::{Memory, MemoryAccessReason, PageSize},
trap::HandleTrap,
value::Value,
};
use { use {
hbvm::{validate::validate, vm::Vm}, hbvm::{validate::validate, vm::Vm},
std::io::{stdin, Read}, std::io::{stdin, Read},
@ -18,7 +12,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
return Ok(()); return Ok(());
} else { } else {
unsafe { unsafe {
let mut vm = Vm::<_, 0>::new_unchecked(&prog, TestTrapHandler); let mut vm = Vm::new_unchecked(&prog);
vm.memory.insert_test_page(); vm.memory.insert_test_page();
println!("Program interrupt: {:?}", vm.run()); println!("Program interrupt: {:?}", vm.run());
println!("{:?}", vm.registers); println!("{:?}", vm.registers);
@ -30,30 +24,3 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
pub fn time() -> u32 { pub fn time() -> u32 {
9 9
} }
struct TestTrapHandler;
impl HandleTrap for TestTrapHandler {
fn page_fault(
&mut self,
_: MemoryAccessReason,
_: &mut Memory,
_: u64,
_: PageSize,
_: *mut u8,
) -> bool {
false
}
fn invalid_op(&mut self, _: &mut [Value; 256], _: &mut usize, _: &mut Memory, _: u8) -> bool
where
Self: Sized,
{
false
}
fn ecall(&mut self, _: &mut [Value; 256], _: &mut usize, _: &mut Memory)
where
Self: Sized,
{
}
}

View file

@ -1,40 +1,28 @@
//! Validate if program is sound to execute
/// Program validation error kind
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ErrorKind { pub enum ErrorKind {
/// Unknown opcode
InvalidInstruction, InvalidInstruction,
/// VM doesn't implement this valid opcode
Unimplemented, Unimplemented,
/// Attempted to copy over register boundary
RegisterArrayOverflow, RegisterArrayOverflow,
} }
/// Error
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Error { pub struct Error {
/// Kind pub kind: ErrorKind,
pub kind: ErrorKind,
/// Location in bytecode
pub index: usize, pub index: usize,
} }
/// Perform bytecode validation. If it passes, the program should be
/// sound to execute.
pub fn validate(mut program: &[u8]) -> Result<(), Error> { pub fn validate(mut program: &[u8]) -> Result<(), Error> {
use hbbytecode::opcode::*; use hbbytecode::opcode::*;
let start = program; let start = program;
loop { loop {
// Match on instruction types and perform necessary checks
program = match program { program = match program {
[] => return Ok(()), [] => return Ok(()),
[LD..=ST, reg, _, _, _, _, _, _, _, _, _, count, ..] [LD..=ST, reg, _, _, _, _, _, _, _, _, _, count, ..]
if usize::from(*reg) * 8 + usize::from(*count) > 2048 => if usize::from(*reg) * 8 + usize::from(*count) > 2048 =>
{ {
return Err(Error { return Err(Error {
kind: ErrorKind::RegisterArrayOverflow, kind: ErrorKind::RegisterArrayOverflow,
index: (program.as_ptr() as usize) - (start.as_ptr() as usize), index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
}) })
} }
@ -42,20 +30,20 @@ pub fn validate(mut program: &[u8]) -> Result<(), Error> {
if src.checked_add(*count).is_none() || dst.checked_add(*count).is_none() => if src.checked_add(*count).is_none() || dst.checked_add(*count).is_none() =>
{ {
return Err(Error { return Err(Error {
kind: ErrorKind::RegisterArrayOverflow, kind: ErrorKind::RegisterArrayOverflow,
index: (program.as_ptr() as usize) - (start.as_ptr() as usize), index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
}) })
} }
[NOP | ECALL, rest @ ..] [NOP | ECALL, rest @ ..]
| [DIR | DIRF, _, _, _, _, rest @ ..] | [DIR | DIRF, _, _, _, _, rest @ ..]
| [ADD..=CMPU | BRC | ADDF..=MULF, _, _, _, rest @ ..] | [ADD..=CMPU | BRC | ADDF..=MULF, _, _, _, rest @ ..]
| [NEG..=NOT | CP..=SWA | NEGF..=FTI, _, _, rest @ ..] | [NEG..=NOT | CP..=SWA, _, _, rest @ ..]
| [LI | JMP, _, _, _, _, _, _, _, _, _, rest @ ..] | [LI | JMP, _, _, _, _, _, _, _, _, _, rest @ ..]
| [ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI, _, _, _, _, _, _, _, _, _, _, rest @ ..] | [ADDI..=CMPUI | BMC | JEQ..=JGTU | ADDFI..=MULFI, _, _, _, _, _, _, _, _, _, _, rest @ ..]
| [LD..=ST, _, _, _, _, _, _, _, _, _, _, _, _, rest @ ..] => rest, | [LD..=ST, _, _, _, _, _, _, _, _, _, _, _, _, rest @ ..] => rest,
_ => { _ => {
return Err(Error { return Err(Error {
kind: ErrorKind::InvalidInstruction, kind: ErrorKind::InvalidInstruction,
index: (program.as_ptr() as usize) - (start.as_ptr() as usize), index: (program.as_ptr() as usize) - (start.as_ptr() as usize),
}) })
} }

View file

@ -1,19 +1,10 @@
//! Program memory implementation mod paging;
pub mod paging; use self::paging::{PageTable, Permission, PtEntry};
use alloc::boxed::Box;
use {
self::paging::{PageTable, Permission, PtEntry},
super::{trap::HandleTrap, VmRunError},
alloc::boxed::Box,
core::mem::MaybeUninit,
derive_more::Display,
};
/// HoleyBytes virtual memory
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Memory { pub struct Memory {
/// Root page table
root_pt: *mut PageTable, root_pt: *mut PageTable,
} }
@ -56,23 +47,13 @@ impl Memory {
entry = PtEntry::new(Box::into_raw(pt) as _, Permission::Node); entry = PtEntry::new(Box::into_raw(pt) as _, Permission::Node);
} }
(*self.root_pt)[0] = entry; self.root_pt_mut()[0] = entry;
} }
} }
/// Load value from an address /// Load value from an address
/// pub unsafe fn load(&self, addr: u64, target: *mut u8, count: usize) -> Result<(), AccessFault> {
/// # Safety
/// Applies same conditions as for [`core::ptr::copy_nonoverlapping`]
pub unsafe fn load(
&mut self,
addr: u64,
target: *mut u8,
count: usize,
traph: &mut impl HandleTrap,
) -> Result<(), LoadError> {
self.memory_access( self.memory_access(
MemoryAccessReason::Load,
addr, addr,
target, target,
count, count,
@ -83,207 +64,105 @@ impl Memory {
) )
}, },
|src, dst, count| core::ptr::copy_nonoverlapping(src, dst, count), |src, dst, count| core::ptr::copy_nonoverlapping(src, dst, count),
traph,
) )
.map_err(LoadError)
} }
/// Store value to an address /// Store value to an address
///
/// # Safety
/// Applies same conditions as for [`core::ptr::copy_nonoverlapping`]
pub unsafe fn store( pub unsafe fn store(
&mut self, &mut self,
addr: u64, addr: u64,
source: *const u8, source: *const u8,
count: usize, count: usize,
traph: &mut impl HandleTrap, ) -> Result<(), AccessFault> {
) -> Result<(), StoreError> {
self.memory_access( self.memory_access(
MemoryAccessReason::Store,
addr, addr,
source.cast_mut(), source.cast_mut(),
count, count,
|perm| perm == Permission::Write, |perm| perm == Permission::Write,
|dst, src, count| core::ptr::copy_nonoverlapping(src, dst, count), |dst, src, count| core::ptr::copy_nonoverlapping(src, dst, count),
traph,
) )
.map_err(StoreError)
} }
/// Copy a block of memory /// Copy a block of memory
/// pub unsafe fn block_copy(&mut self, src: u64, dst: u64, count: u64) -> Result<(), ()> {
/// # Safety /* let count = usize::try_from(count).expect("?conradluget a better CPU");
/// - Same as for [`Self::load`] and [`Self::store`]
/// - Your faith in the gods of UB
/// - Addr-san claims it's fine but who knows is she isn't lying :ferrisSus:
pub unsafe fn block_copy(
&mut self,
src: u64,
dst: u64,
count: usize,
traph: &mut impl HandleTrap,
) -> Result<(), BlkCopyError> {
// Yea, i know it is possible to do this more efficiently, but I am too lazy.
const STACK_BUFFER_SIZE: usize = 512; let mut srcs = PageSplitter::new(src, count, self.root_pt);
let mut dsts = PageSplitter::new(dst, count, self.root_pt);
let mut c_src = srcs.next().ok_or(())?;
let mut c_dst = dsts.next().ok_or(())?;
// Decide if to use stack-allocated buffer or to heap allocate loop {
// Deallocation is again decided on size at the end of the function let min_size = c_src.size.min(c_dst.size);
let mut buf = MaybeUninit::<[u8; STACK_BUFFER_SIZE]>::uninit();
let buf = if count <= STACK_BUFFER_SIZE {
buf.as_mut_ptr().cast()
} else {
unsafe { unsafe {
let layout = core::alloc::Layout::from_size_align_unchecked(count, 1); core::ptr::copy(c_src.ptr, c_dst.ptr, min_size);
let ptr = alloc::alloc::alloc(layout);
if ptr.is_null() {
alloc::alloc::handle_alloc_error(layout);
}
ptr
} }
};
// Perform memory block transfer match (
let status = (|| { match c_src.size.saturating_sub(min_size) {
// Load to buffer 0 => srcs.next(),
self.memory_access( size => Some(PageSplitResult { size, ..c_src }),
MemoryAccessReason::Load,
src,
buf,
count,
|perm| {
matches!(
perm,
Permission::Readonly | Permission::Write | Permission::Exec
)
}, },
|src, dst, count| core::ptr::copy(src, dst, count), match c_dst.size.saturating_sub(min_size) {
traph, 0 => dsts.next(),
) size => Some(PageSplitResult { size, ..c_dst }),
.map_err(|addr| BlkCopyError { },
access_reason: MemoryAccessReason::Load, ) {
addr, (None, None) => return Ok(()),
})?; (Some(src), Some(dst)) => (c_src, c_dst) = (src, dst),
_ => return Err(()),
}
} */
// Store from buffer todo!("Block memory copy")
self.memory_access( }
MemoryAccessReason::Store,
dst, #[inline]
buf, pub fn root_pt(&self) -> &PageTable {
count, unsafe { &*self.root_pt }
|perm| perm == Permission::Write, }
|dst, src, count| core::ptr::copy(src, dst, count),
traph, #[inline]
) pub fn root_pt_mut(&mut self) -> &mut PageTable {
.map_err(|addr| BlkCopyError { unsafe { &mut *self.root_pt }
access_reason: MemoryAccessReason::Store,
addr,
})?;
Ok::<_, BlkCopyError>(())
})();
// Deallocate if used heap-allocated array
if count > STACK_BUFFER_SIZE {
alloc::alloc::dealloc(
buf,
core::alloc::Layout::from_size_align_unchecked(count, 1),
);
}
status
} }
/// Split address to pages, check their permissions and feed pointers with offset
/// to a specified function.
///
/// If page is not found, execute page fault trap handler.
#[allow(clippy::too_many_arguments)] // Silence peasant
fn memory_access( fn memory_access(
&mut self, &self,
reason: MemoryAccessReason,
src: u64, src: u64,
mut dst: *mut u8, mut dst: *mut u8,
len: usize, len: usize,
permission_check: fn(Permission) -> bool, permission_check: impl Fn(Permission) -> bool,
action: fn(*mut u8, *mut u8, usize), action: impl Fn(*mut u8, *mut u8, usize),
traph: &mut impl HandleTrap, ) -> Result<(), AccessFault> {
) -> Result<(), u64> { for item in PageSplitter::new(src, len, self.root_pt) {
let mut pspl = AddrSplitter::new(src, len, self.root_pt); let PageSplitResult { ptr, size, perm } = item?;
loop { if !permission_check(perm) {
match pspl.next() { return Err(AccessFault::Permission);
// Page found
Some(Ok(AddrSplitOk {
vaddr,
ptr,
size,
perm,
})) => {
if !permission_check(perm) {
return Err(vaddr);
}
// Perform memory action and bump dst pointer
action(ptr, dst, size);
dst = unsafe { dst.add(size) };
}
Some(Err(AddrSplitError { addr, size })) => {
// Execute page fault handler
if traph.page_fault(reason, self, addr, size, dst) {
// Shift the splitter address
pspl.bump(size);
// Bump dst pointer
dst = unsafe { dst.add(size as _) };
} else {
return Err(addr); // Unhandleable
}
}
None => return Ok(()),
} }
action(ptr, dst, size);
dst = unsafe { dst.add(size) };
} }
Ok(())
} }
} }
/// Result from address split #[derive(Debug)]
struct AddrSplitOk { struct PageSplitResult {
/// Virtual address
vaddr: u64,
/// Pointer to the start for perform operation
ptr: *mut u8, ptr: *mut u8,
/// Size to the end of page / end of desired size
size: usize, size: usize,
/// Page permission
perm: Permission, perm: Permission,
} }
struct AddrSplitError { struct PageSplitter {
/// Address of failure
addr: u64, addr: u64,
/// Requested page size
size: PageSize,
}
/// Address splitter into pages
struct AddrSplitter {
/// Current address
addr: u64,
/// Size left
size: usize, size: usize,
/// Page table
pagetable: *const PageTable, pagetable: *const PageTable,
} }
impl AddrSplitter { impl PageSplitter {
/// Create a new page splitter
pub const fn new(addr: u64, size: usize, pagetable: *const PageTable) -> Self { pub const fn new(addr: u64, size: usize, pagetable: *const PageTable) -> Self {
Self { Self {
addr, addr,
@ -291,29 +170,19 @@ impl AddrSplitter {
pagetable, pagetable,
} }
} }
/// Bump address by size X
fn bump(&mut self, page_size: PageSize) {
self.addr += page_size as u64;
self.size = self.size.saturating_sub(page_size as _);
}
} }
impl Iterator for AddrSplitter { impl Iterator for PageSplitter {
type Item = Result<AddrSplitOk, AddrSplitError>; type Item = Result<PageSplitResult, AccessFault>;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
// The end, everything is fine
if self.size == 0 { if self.size == 0 {
return None; return None;
} }
let (base, perm, size, offset) = 'a: { let (base, perm, size, offset) = 'a: {
let mut current_pt = self.pagetable; let mut current_pt = self.pagetable;
// Walk the page table
for lvl in (0..5).rev() { for lvl in (0..5).rev() {
// Get an entry
unsafe { unsafe {
let entry = (*current_pt).get_unchecked( let entry = (*current_pt).get_unchecked(
usize::try_from((self.addr >> (lvl * 9 + 12)) & ((1 << 9) - 1)) usize::try_from((self.addr >> (lvl * 9 + 12)) & ((1 << 9) - 1))
@ -322,109 +191,48 @@ impl Iterator for AddrSplitter {
let ptr = entry.ptr(); let ptr = entry.ptr();
match entry.permission() { match entry.permission() {
// No page → page fault
Permission::Empty => { Permission::Empty => {
return Some(Err(AddrSplitError { return Some(Err(AccessFault::NoPage {
addr: self.addr, addr: self.addr,
size: PageSize::from_lvl(lvl)?, remaining: self.size,
})) }))
} }
// Node → proceed waking
Permission::Node => current_pt = ptr as _, Permission::Node => current_pt = ptr as _,
// Leaft → return relevant data
perm => { perm => {
break 'a ( break 'a (
// Pointer in host memory
ptr as *mut u8, ptr as *mut u8,
perm, perm,
PageSize::from_lvl(lvl)?, match lvl {
// In-page offset 0 => 4096,
1 => 1024_usize.pow(2) * 2,
2 => 1024_usize.pow(3),
_ => return Some(Err(AccessFault::TooShallow)),
},
self.addr as usize & ((1 << (lvl * 9 + 12)) - 1), self.addr as usize & ((1 << (lvl * 9 + 12)) - 1),
); )
} }
} }
} }
} }
return None; // Reached the end (should not happen) return Some(Err(AccessFault::TooDeep));
}; };
// Get available byte count in the selected page with offset extern crate std;
let avail = (size as usize - offset).clamp(0, self.size); let avail = (size - offset).clamp(0, self.size);
self.bump(size); self.addr += size as u64;
self.size = self.size.saturating_sub(size);
Some(Ok(AddrSplitOk { std::dbg!(Some(Ok(PageSplitResult {
vaddr: self.addr, ptr: unsafe { base.add(offset) },
ptr: unsafe { base.add(offset) }, // Return pointer to the start of region
size: avail, size: avail,
perm, perm,
})) })))
} }
} }
/// Page size
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PageSize {
/// 4 KiB page (on level 0)
Size4K = 4096,
/// 2 MiB page (on level 1)
Size2M = 1024 * 1024 * 2,
/// 1 GiB page (on level 2)
Size1G = 1024 * 1024 * 1024,
}
impl PageSize {
/// Convert page table level to size of page
fn from_lvl(lvl: u8) -> Option<Self> {
match lvl {
0 => Some(PageSize::Size4K),
1 => Some(PageSize::Size2M),
2 => Some(PageSize::Size1G),
_ => None,
}
}
}
/// Unhandled load access trap
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
pub struct LoadError(u64);
/// Unhandled store access trap
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
pub struct StoreError(u64);
#[derive(Clone, Copy, Display, Debug, PartialEq, Eq)]
pub enum MemoryAccessReason {
Load,
Store,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BlkCopyError { pub enum AccessFault {
access_reason: MemoryAccessReason, NoPage { addr: u64, remaining: usize },
addr: u64, Permission,
} TooShallow,
TooDeep,
impl From<BlkCopyError> for VmRunError {
fn from(value: BlkCopyError) -> Self {
match value.access_reason {
MemoryAccessReason::Load => Self::LoadAccessEx(value.addr),
MemoryAccessReason::Store => Self::StoreAccessEx(value.addr),
}
}
}
impl From<LoadError> for VmRunError {
fn from(value: LoadError) -> Self {
Self::LoadAccessEx(value.0)
}
}
impl From<StoreError> for VmRunError {
fn from(value: StoreError) -> Self {
Self::StoreAccessEx(value.0)
}
} }

View file

@ -1,53 +1,35 @@
//! Page table and associated structures implementation use core::{
fmt::Debug,
use { mem::MaybeUninit,
core::{ ops::{Index, IndexMut},
fmt::Debug, slice::SliceIndex,
mem::MaybeUninit,
ops::{Index, IndexMut},
slice::SliceIndex,
},
delegate::delegate,
}; };
use delegate::delegate;
/// Page entry permission
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
pub enum Permission { pub enum Permission {
/// No page present
#[default] #[default]
Empty, Empty,
/// Points to another pagetable
Node, Node,
/// Page is read only
Readonly, Readonly,
/// Page is readable and writable
Write, Write,
/// Page is readable and executable
Exec, Exec,
} }
/// Page table entry
#[derive(Clone, Copy, Default, PartialEq, Eq)] #[derive(Clone, Copy, Default, PartialEq, Eq)]
pub struct PtEntry(u64); pub struct PtEntry(u64);
impl PtEntry { impl PtEntry {
/// Create new
///
/// # Safety
/// - `ptr` has to point to valid data and shall not be deallocated
/// troughout the entry lifetime
#[inline] #[inline]
pub unsafe fn new(ptr: *mut PtPointedData, permission: Permission) -> Self { pub unsafe fn new(ptr: *mut PtPointedData, permission: Permission) -> Self {
Self(ptr as u64 | permission as u64) Self(ptr as u64 | permission as u64)
} }
/// Get permission
#[inline] #[inline]
pub fn permission(&self) -> Permission { pub fn permission(&self) -> Permission {
unsafe { core::mem::transmute(self.0 as u8 & 0b111) } unsafe { core::mem::transmute(self.0 as u8 & 0b111) }
} }
/// Get pointer to the data (leaf) or next page table (node)
#[inline] #[inline]
pub fn ptr(&self) -> *mut PtPointedData { pub fn ptr(&self) -> *mut PtPointedData {
(self.0 & !((1 << 12) - 1)) as _ (self.0 & !((1 << 12) - 1)) as _
@ -63,50 +45,21 @@ impl Debug for PtEntry {
} }
} }
/// Page table
#[derive(Clone, Copy, Debug, PartialEq, Eq)] #[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(align(4096))] #[repr(align(4096))]
pub struct PageTable([PtEntry; 512]); pub struct PageTable([PtEntry; 512]);
impl PageTable { impl PageTable {
delegate!(to self.0 { delegate!(to self.0 {
/// Returns a reference to an element or subslice depending on the type of pub unsafe fn get<I>(&self, ix: I) -> Option<&I::Output>
/// index.
///
/// - If given a position, returns a reference to the element at that
/// position or `None` if out of bounds.
/// - If given a range, returns the subslice corresponding to that range,
/// or `None` if out of bounds.
///
pub fn get<I>(&self, ix: I) -> Option<&I::Output>
where I: SliceIndex<[PtEntry]>; where I: SliceIndex<[PtEntry]>;
/// Returns a mutable reference to an element or subslice depending on the pub unsafe fn get_mut<I>(&mut self, ix: I) -> Option<&mut I::Output>
/// type of index (see [`get`]) or `None` if the index is out of bounds.
pub fn get_mut<I>(&mut self, ix: I) -> Option<&mut I::Output>
where I: SliceIndex<[PtEntry]>; where I: SliceIndex<[PtEntry]>;
/// Returns a reference to an element or subslice, without doing bounds
/// checking.
///
/// For a safe alternative see [`get`].
///
/// # Safety
///
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
/// even if the resulting reference is not used.
pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output pub unsafe fn get_unchecked<I>(&self, index: I) -> &I::Output
where I: SliceIndex<[PtEntry]>; where I: SliceIndex<[PtEntry]>;
/// Returns a mutable reference to an element or subslice, without doing
/// bounds checking.
///
/// For a safe alternative see [`get_mut`].
///
/// # Safety
///
/// Calling this method with an out-of-bounds index is *[undefined behavior]*
/// even if the resulting reference is not used.
pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output pub unsafe fn get_unchecked_mut<I>(&mut self, index: I) -> &mut I::Output
where I: SliceIndex<[PtEntry]>; where I: SliceIndex<[PtEntry]>;
}); });
@ -136,17 +89,13 @@ where
impl Default for PageTable { impl Default for PageTable {
fn default() -> Self { fn default() -> Self {
// SAFETY: It's fine, zeroed page table entry is valid (= empty)
Self(unsafe { MaybeUninit::zeroed().assume_init() }) Self(unsafe { MaybeUninit::zeroed().assume_init() })
} }
} }
/// Data page table entry can possibly point to
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
#[repr(C, align(4096))] #[repr(C, align(4096))]
pub union PtPointedData { pub union PtPointedData {
/// Node - next page table pub pt: PageTable,
pub pt: PageTable,
/// Leaf
pub page: u8, pub page: u8,
} }

View file

@ -11,11 +11,10 @@
// program size. If you are (rightfully) worried about the UB, for now just // program size. If you are (rightfully) worried about the UB, for now just
// append your program with 11 zeroes. // append your program with 11 zeroes.
use self::trap::HandleTrap; use self::mem::AccessFault;
pub mod mem; mod mem;
pub mod trap; mod value;
pub mod value;
use { use {
crate::validate, crate::validate,
@ -26,7 +25,6 @@ use {
value::Value, value::Value,
}; };
/// Extract a parameter from program
macro_rules! param { macro_rules! param {
($self:expr, $ty:ty) => {{ ($self:expr, $ty:ty) => {{
assert_impl_one!($ty: OpParam); assert_impl_one!($ty: OpParam);
@ -41,7 +39,6 @@ macro_rules! param {
}}; }};
} }
/// Perform binary operation `#0 ← #1 OP #2`
macro_rules! binary_op { macro_rules! binary_op {
($self:expr, $ty:ident, $handler:expr) => {{ ($self:expr, $ty:ident, $handler:expr) => {{
let ParamBBB(tg, a0, a1) = param!($self, ParamBBB); let ParamBBB(tg, a0, a1) = param!($self, ParamBBB);
@ -50,23 +47,22 @@ macro_rules! binary_op {
$handler( $handler(
Value::$ty(&$self.read_reg(a0)), Value::$ty(&$self.read_reg(a0)),
Value::$ty(&$self.read_reg(a1)), Value::$ty(&$self.read_reg(a1)),
), )
.into(),
); );
}}; }};
} }
/// Perform binary operation with immediate `#0 ← #1 OP imm #2`
macro_rules! binary_op_imm { macro_rules! binary_op_imm {
($self:expr, $ty:ident, $handler:expr) => {{ ($self:expr, $ty:ident, $handler:expr) => {{
let ParamBBD(tg, a0, imm) = param!($self, ParamBBD); let ParamBBD(tg, a0, imm) = param!($self, ParamBBD);
$self.write_reg( $self.write_reg(
tg, tg,
$handler(Value::$ty(&$self.read_reg(a0)), Value::$ty(&imm.into())), $handler(Value::$ty(&$self.read_reg(a0)), Value::$ty(&imm.into())).into(),
); );
}}; }};
} }
/// Jump at `#3` if ordering on `#0 <=> #1` is equal to expected
macro_rules! cond_jump { macro_rules! cond_jump {
($self:expr, $ty:ident, $expected:ident) => {{ ($self:expr, $ty:ident, $expected:ident) => {{
let ParamBBD(a0, a1, jt) = param!($self, ParamBBD); let ParamBBD(a0, a1, jt) = param!($self, ParamBBD);
@ -78,63 +74,36 @@ macro_rules! cond_jump {
}}; }};
} }
/// HoleyBytes Virtual Machine pub struct Vm<'a> {
pub struct Vm<'a, T, const TIMER_QUOTIENT: usize> {
/// Holds 256 registers
///
/// Writing to register 0 is considered undefined behaviour
/// in terms of HoleyBytes program execution
pub registers: [Value; 256], pub registers: [Value; 256],
/// Memory implementation
pub memory: Memory, pub memory: Memory,
/// Trap handler
pub traph: T,
// Program counter
pc: usize, pc: usize,
/// Program
program: &'a [u8], program: &'a [u8],
/// Program timer
timer: usize,
} }
impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> { impl<'a> Vm<'a> {
/// Create a new VM with program and trap handler
///
/// # Safety /// # Safety
/// Program code has to be validated /// Program code has to be validated
pub unsafe fn new_unchecked(program: &'a [u8], traph: T) -> Self { pub unsafe fn new_unchecked(program: &'a [u8]) -> Self {
Self { Self {
registers: [Value::from(0_u64); 256], registers: [Value::from(0_u64); 256],
memory: Default::default(), memory: Default::default(),
traph,
pc: 0, pc: 0,
program, program,
timer: 0,
} }
} }
/// Create a new VM with program and trap handler only if it passes validation pub fn new_validated(program: &'a [u8]) -> Result<Self, validate::Error> {
pub fn new_validated(program: &'a [u8], traph: T) -> Result<Self, validate::Error> {
validate::validate(program)?; validate::validate(program)?;
Ok(unsafe { Self::new_unchecked(program, traph) }) Ok(unsafe { Self::new_unchecked(program) })
} }
/// Execute program pub fn run(&mut self) -> HaltReason {
///
/// Program can return [`VmRunError`] if a trap handling failed
pub fn run(&mut self) -> Result<VmRunOk, VmRunError> {
use hbbytecode::opcode::*; use hbbytecode::opcode::*;
loop { loop {
// Fetch instruction
let Some(&opcode) = self.program.get(self.pc) let Some(&opcode) = self.program.get(self.pc)
else { return Ok(VmRunOk::End) }; else { return HaltReason::ProgramEnd };
// Big match
unsafe { unsafe {
match opcode { match opcode {
NOP => param!(self, ()), NOP => param!(self, ()),
@ -151,19 +120,21 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB); let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
self.write_reg( self.write_reg(
tg, tg,
self.read_reg(a0).as_i64().cmp(&self.read_reg(a1).as_i64()) as i64, (self.read_reg(a0).as_i64().cmp(&self.read_reg(a1).as_i64()) as i64)
.into(),
); );
} }
CMPU => { CMPU => {
let ParamBBB(tg, a0, a1) = param!(self, ParamBBB); let ParamBBB(tg, a0, a1) = param!(self, ParamBBB);
self.write_reg( self.write_reg(
tg, tg,
self.read_reg(a0).as_u64().cmp(&self.read_reg(a1).as_u64()) as i64, (self.read_reg(a0).as_u64().cmp(&self.read_reg(a1).as_u64()) as i64)
.into(),
); );
} }
NOT => { NOT => {
let param = param!(self, ParamBB); let param = param!(self, ParamBB);
self.write_reg(param.0, !self.read_reg(param.1).as_u64()); self.write_reg(param.0, (!self.read_reg(param.1).as_u64()).into());
} }
NEG => { NEG => {
let param = param!(self, ParamBB); let param = param!(self, ParamBB);
@ -172,15 +143,16 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
match self.read_reg(param.1).as_u64() { match self.read_reg(param.1).as_u64() {
0 => 1_u64, 0 => 1_u64,
_ => 0, _ => 0,
}, }
.into(),
); );
} }
DIR => { DIR => {
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB); let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
let a0 = self.read_reg(a0).as_u64(); let a0 = self.read_reg(a0).as_u64();
let a1 = self.read_reg(a1).as_u64(); let a1 = self.read_reg(a1).as_u64();
self.write_reg(dt, a0.checked_div(a1).unwrap_or(u64::MAX)); self.write_reg(dt, (a0.checked_div(a1).unwrap_or(u64::MAX)).into());
self.write_reg(rt, a0.checked_rem(a1).unwrap_or(u64::MAX)); self.write_reg(rt, (a0.checked_rem(a1).unwrap_or(u64::MAX)).into());
} }
ADDI => binary_op_imm!(self, as_u64, ops::Add::add), ADDI => binary_op_imm!(self, as_u64, ops::Add::add),
MULI => binary_op_imm!(self, as_u64, ops::Mul::mul), MULI => binary_op_imm!(self, as_u64, ops::Mul::mul),
@ -194,12 +166,13 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
let ParamBBD(tg, a0, imm) = param!(self, ParamBBD); let ParamBBD(tg, a0, imm) = param!(self, ParamBBD);
self.write_reg( self.write_reg(
tg, tg,
self.read_reg(a0).as_i64().cmp(&Value::from(imm).as_i64()) as i64, (self.read_reg(a0).as_i64().cmp(&Value::from(imm).as_i64()) as i64)
.into(),
); );
} }
CMPUI => { CMPUI => {
let ParamBBD(tg, a0, imm) = param!(self, ParamBBD); let ParamBBD(tg, a0, imm) = param!(self, ParamBBD);
self.write_reg(tg, self.read_reg(a0).as_u64().cmp(&imm) as i64); self.write_reg(tg, (self.read_reg(a0).as_u64().cmp(&imm) as i64).into());
} }
CP => { CP => {
let param = param!(self, ParamBB); let param = param!(self, ParamBB);
@ -216,7 +189,7 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
} }
LI => { LI => {
let param = param!(self, ParamBD); let param = param!(self, ParamBD);
self.write_reg(param.0, param.1); self.write_reg(param.0, param.1.into());
} }
LD => { LD => {
let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH); let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH);
@ -225,30 +198,37 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
_ => 0, _ => 0,
}; };
self.memory.load( if let Err(e) = self.memory.load(
self.read_reg(base).as_u64() + off + n as u64, self.read_reg(base).as_u64() + off + n as u64,
self.registers.as_mut_ptr().add(usize::from(dst) + n).cast(), self.registers.as_mut_ptr().add(usize::from(dst) + n).cast(),
usize::from(count).saturating_sub(n), usize::from(count).saturating_sub(n),
&mut self.traph, ) {
)?; return HaltReason::LoadAccessEx(e);
}
} }
ST => { ST => {
let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH); let ParamBBDH(dst, base, off, count) = param!(self, ParamBBDH);
self.memory.store( if let Err(e) = self.memory.store(
self.read_reg(base).as_u64() + off, self.read_reg(base).as_u64() + off,
self.registers.as_ptr().add(usize::from(dst)).cast(), self.registers.as_ptr().add(usize::from(dst)).cast(),
count.into(), count.into(),
&mut self.traph, ) {
)?; return HaltReason::StoreAccessEx(e);
}
} }
BMC => { BMC => {
let ParamBBD(src, dst, count) = param!(self, ParamBBD); let ParamBBD(src, dst, count) = param!(self, ParamBBD);
self.memory.block_copy( if self
self.read_reg(src).as_u64(), .memory
self.read_reg(dst).as_u64(), .block_copy(
count as _, self.read_reg(src).as_u64(),
&mut self.traph, self.read_reg(dst).as_u64(),
)?; count,
)
.is_err()
{
todo!("Block memory copy fault");
}
} }
BRC => { BRC => {
let ParamBBB(src, dst, count) = param!(self, ParamBBB); let ParamBBB(src, dst, count) = param!(self, ParamBBB);
@ -275,99 +255,48 @@ impl<'a, T: HandleTrap, const TIMER_QUOTIENT: usize> Vm<'a, T, TIMER_QUOTIENT> {
JGTU => cond_jump!(self, sint, Greater), JGTU => cond_jump!(self, sint, Greater),
ECALL => { ECALL => {
param!(self, ()); param!(self, ());
self.traph return HaltReason::Ecall;
.ecall(&mut self.registers, &mut self.pc, &mut self.memory);
} }
ADDF => binary_op!(self, as_f64, ops::Add::add), ADDF => binary_op!(self, as_f64, ops::Add::add),
SUBF => binary_op!(self, as_f64, ops::Sub::sub),
MULF => binary_op!(self, as_f64, ops::Mul::mul), MULF => binary_op!(self, as_f64, ops::Mul::mul),
DIRF => { DIRF => {
let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB); let ParamBBBB(dt, rt, a0, a1) = param!(self, ParamBBBB);
let a0 = self.read_reg(a0).as_f64(); let a0 = self.read_reg(a0).as_f64();
let a1 = self.read_reg(a1).as_f64(); let a1 = self.read_reg(a1).as_f64();
self.write_reg(dt, a0 / a1); self.write_reg(dt, (a0 / a1).into());
self.write_reg(rt, a0 % a1); self.write_reg(rt, (a0 % a1).into());
}
FMAF => {
let ParamBBBB(dt, a0, a1, a2) = param!(self, ParamBBBB);
self.write_reg(
dt,
self.read_reg(a0).as_f64() * self.read_reg(a1).as_f64()
+ self.read_reg(a2).as_f64(),
);
}
NEGF => {
let ParamBB(dt, a0) = param!(self, ParamBB);
self.write_reg(dt, -self.read_reg(a0).as_f64());
}
ITF => {
let ParamBB(dt, a0) = param!(self, ParamBB);
self.write_reg(dt, self.read_reg(a0).as_i64() as f64);
}
FTI => {
let ParamBB(dt, a0) = param!(self, ParamBB);
self.write_reg(dt, self.read_reg(a0).as_f64() as i64);
} }
ADDFI => binary_op_imm!(self, as_f64, ops::Add::add), ADDFI => binary_op_imm!(self, as_f64, ops::Add::add),
MULFI => binary_op_imm!(self, as_f64, ops::Mul::mul), MULFI => binary_op_imm!(self, as_f64, ops::Mul::mul),
op => { op => return HaltReason::InvalidOpEx(op),
if !self.traph.invalid_op(
&mut self.registers,
&mut self.pc,
&mut self.memory,
op,
) {
return Err(VmRunError::InvalidOpcodeEx(op));
}
}
}
}
if TIMER_QUOTIENT != 0 {
self.timer = self.timer.wrapping_add(1);
if self.timer % TIMER_QUOTIENT == 0 {
return Ok(VmRunOk::Timer);
} }
} }
} }
} }
/// Read register
#[inline] #[inline]
unsafe fn read_reg(&self, n: u8) -> Value { unsafe fn read_reg(&self, n: u8) -> Value {
*self.registers.get_unchecked(n as usize) if n == 0 {
0_u64.into()
} else {
*self.registers.get_unchecked(n as usize)
}
} }
/// Write a register.
/// Writing to register 0 is no-op.
#[inline] #[inline]
unsafe fn write_reg(&mut self, n: u8, value: impl Into<Value>) { unsafe fn write_reg(&mut self, n: u8, value: Value) {
if n != 0 { if n != 0 {
*self.registers.get_unchecked_mut(n as usize) = value.into(); *self.registers.get_unchecked_mut(n as usize) = value;
} }
} }
} }
/// Virtual machine halt error
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)] #[repr(u8)]
pub enum VmRunError { pub enum HaltReason {
/// Unhandled invalid opcode exceptions ProgramEnd,
InvalidOpcodeEx(u8), Ecall,
InvalidOpEx(u8),
/// Unhandled load access exception LoadAccessEx(AccessFault),
LoadAccessEx(u64), StoreAccessEx(AccessFault),
/// Unhandled store access exception
StoreAccessEx(u64),
}
/// Virtual machine halt ok
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum VmRunOk {
/// Program has eached its end
End,
/// Program was interrupted by a timer
Timer,
} }

View file

@ -1,35 +0,0 @@
//! Program trap handling interfaces
use super::{
mem::{Memory, MemoryAccessReason, PageSize},
value::Value,
};
/// Handle VM traps
pub trait HandleTrap {
/// Handle page fault
fn page_fault(
&mut self,
reason: MemoryAccessReason,
memory: &mut Memory,
vaddr: u64,
size: PageSize,
dataptr: *mut u8,
) -> bool;
/// Handle invalid opcode exception
fn invalid_op(
&mut self,
regs: &mut [Value; 256],
pc: &mut usize,
memory: &mut Memory,
op: u8,
) -> bool
where
Self: Sized;
/// Handle environment calls
fn ecall(&mut self, regs: &mut [Value; 256], pc: &mut usize, memory: &mut Memory)
where
Self: Sized;
}

View file

@ -1,15 +1,7 @@
//! HoleyBytes register value definition
use core::fmt::Debug; use core::fmt::Debug;
/// Define [`Value`] union
///
/// # Safety
/// Union variants have to be sound to byte-reinterpretate
/// between each other. Otherwise the behaviour is undefined.
macro_rules! value_def { macro_rules! value_def {
($($ty:ident),* $(,)?) => { ($($ty:ident),* $(,)?) => {
/// HBVM register value
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
#[repr(packed)] #[repr(packed)]
pub union Value { pub union Value {
@ -18,7 +10,6 @@ macro_rules! value_def {
paste::paste! { paste::paste! {
impl Value {$( impl Value {$(
#[doc = "Byte-reinterpret [`Value`] as [`" $ty "`]"]
#[inline] #[inline]
pub fn [<as_ $ty>](&self) -> $ty { pub fn [<as_ $ty>](&self) -> $ty {
unsafe { self.$ty } unsafe { self.$ty }
@ -38,11 +29,9 @@ macro_rules! value_def {
} }
value_def!(u64, i64, f64); value_def!(u64, i64, f64);
static_assertions::const_assert_eq!(core::mem::size_of::<Value>(), 8);
impl Debug for Value { impl Debug for Value {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// Print formatted as hexadecimal, unsigned integer self.as_u64().fmt(f)
write!(f, "{:x}", self.as_u64())
} }
} }

View file

@ -1 +0,0 @@
nightly

View file

@ -1,4 +1,3 @@
hex_literal_case = "Upper" hex_literal_case = "Upper"
imports_granularity = "One" imports_granularity = "One"
struct_field_align_threshold = 5 struct_field_align_threshold = 5
enum_discrim_align_threshold = 5

37
spec.md
View file

@ -19,6 +19,7 @@
| BBBB | 32 bits | | BBBB | 32 bits |
| BBB | 24 bits | | BBB | 24 bits |
| BBDH | 96 bits | | BBDH | 96 bits |
| BBDB | 88 bits |
| BBD | 80 bits | | BBD | 80 bits |
| BB | 16 bits | | BB | 16 bits |
| BD | 72 bits | | BD | 72 bits |
@ -199,38 +200,14 @@
| Opcode | Name | Action | | Opcode | Name | Action |
|:------:|:----:|:--------------:| |:------:|:----:|:--------------:|
| 40 | ADDF | Addition | | 40 | ADDF | Addition |
| 41 | SUBF | Subtraction | | 41 | MULF | Multiplication |
| 42 | MULF | Multiplication |
### Division-remainder ### Division-remainder
- Type BBBB - Type BBBB
| Opcode | Name | Action | | Opcode | Name | Action |
|:------:|:----:|:-------------------------:| |:------:|:----:|:--------------------------------------:|
| 43 | DIRF | Same as for integer `DIR` | | 42 | DIRF | Same flow applies as for integer `DIR` |
### Fused Multiply-Add
- Type BBBB
| Opcode | Name | Action |
|:------:|:----:|:---------------------:|
| 44 | FMAF | `#0 ← (#1 * #2) + #3` |
### Negation
- Type BB
| Opcode | Name | Action |
|:------:|:----:|:----------:|
| 45 | NEGF | `#0 ← -#1` |
### Conversion
- Type BB
- Signed
- `#0 ← #1 as _`
| Opcode | Name | Action |
|:------:|:----:|:------------:|
| 46 | ITF | Int to Float |
| 47 | FTI | Float to Int |
## Floating point immediate operations ## Floating point immediate operations
- Type BBD - Type BBD
@ -238,8 +215,8 @@
| Opcode | Name | Action | | Opcode | Name | Action |
|:------:|:-----:|:--------------:| |:------:|:-----:|:--------------:|
| 48 | ADDFI | Addition | | 43 | ADDFI | Addition |
| 49 | MULFI | Multiplication | | 44 | MULFI | Multiplication |
# Registers # Registers
- There is 255 registers + one zero register (with index 0) - There is 255 registers + one zero register (with index 0)