summaryrefslogtreecommitdiff
path: root/website-emu/src/lib.rs
diff options
context:
space:
mode:
authorNathan Lee <me@nwlee.tech>2026-10-03 21:37:57 -0500
committerNathan Lee <me@nwlee.tech>2026-10-03 21:37:57 -0500
commit0189cc9d771e5911b07fbf91ca18da3a338f8d30 (patch)
treedbf2c8a366547dda3ec99f752417a4085a5a5599 /website-emu/src/lib.rs
parent182ad75161c77b4a75d48275979ce44cde213e3f (diff)
add first userspace invocation
Diffstat (limited to 'website-emu/src/lib.rs')
-rw-r--r--website-emu/src/lib.rs95
1 files changed, 73 insertions, 22 deletions
diff --git a/website-emu/src/lib.rs b/website-emu/src/lib.rs
index 9abffa0..b4d32e6 100644
--- a/website-emu/src/lib.rs
+++ b/website-emu/src/lib.rs
@@ -5,7 +5,8 @@ mod state;
use wasm_bindgen::prelude::wasm_bindgen;
use crate::core::*;
use crate::mem::*;
-use crate::state::CpuState;
+use crate::state::{CpuState, Mode};
+use crate::state::Mode::USER;
const REGISTER_NAME_WIDTH: u32 = 0b11111;
@@ -19,6 +20,7 @@ const INITRD_LOAD: u32 = 0;
#[wasm_bindgen(module = "/src/dev.js")]
extern "C" {
fn uart_push_line(line: &str);
+ fn log(line: &str);
}
#[repr(C)]
@@ -142,10 +144,23 @@ pub fn boot(image: &[u8], initrd: &[u8], memory: &[u8]) {
device_type: 1, // UART
start: CONSOLE_LOAD,
size: 1,
+ next: DRAM_LOAD + args_size + device_size * 2
+ };
+ let initramfs = Device {
+ device_type: 10, // DRAM, initrd
+ start: INITRD_LOAD,
+ size: initrd_size as u32,
+ next: DRAM_LOAD + args_size + device_size * 3
+ };
+ let kernel = Device {
+ device_type: 11, // DRAM, reserved
+ start: OS_LOAD,
+ size: os_size as u32,
next: 0
};
+
let args = BootArgs {
- cmd_line: DRAM_LOAD + args_size + device_size * 2,
+ cmd_line: DRAM_LOAD + args_size + device_size * 4,
devices: DRAM_LOAD + args_size
};
@@ -167,6 +182,18 @@ pub fn boot(image: &[u8], initrd: &[u8], memory: &[u8]) {
size_of::<Device>(),
)
};
+ let initramfs_bytes: &[u8] = unsafe {
+ std::slice::from_raw_parts(
+ &initramfs as *const Device as *const u8,
+ size_of::<Device>(),
+ )
+ };
+ let kernel_bytes: &[u8] = unsafe {
+ std::slice::from_raw_parts(
+ &kernel as *const Device as *const u8,
+ size_of::<Device>(),
+ )
+ };
let cmd_line_bytes = cmd_line.as_bytes();
for i in 0..args_size {
state.memory.write(DRAM_LOAD + i, args_bytes[i as usize]).unwrap();
@@ -174,9 +201,12 @@ pub fn boot(image: &[u8], initrd: &[u8], memory: &[u8]) {
for i in 0..device_size {
state.memory.write(DRAM_LOAD + args_size + i, dram_bytes[i as usize]).unwrap();
state.memory.write(DRAM_LOAD + args_size + device_size + i, uart_bytes[i as usize]).unwrap();
+ state.memory.write(DRAM_LOAD + args_size + device_size * 2 + i, initramfs_bytes[i as usize]).unwrap();
+ state.memory.write(DRAM_LOAD + args_size + device_size * 3 + i, kernel_bytes[i as usize]).unwrap();
+
}
for i in 0..cmd_line.len() as u32 {
- state.memory.write(DRAM_LOAD + args_size + device_size * 2 + i, cmd_line_bytes[i as usize]).unwrap();
+ state.memory.write(DRAM_LOAD + args_size + device_size * 4 + i, cmd_line_bytes[i as usize]).unwrap();
}
state.pc = 0x80000000;
@@ -223,7 +253,7 @@ fn handle_instruction(state: &mut CpuState) {
13 => divu(state, rd, rs1, rs2),
14 => rem(state, rd, rs1, rs2),
15 => remu(state, rd, rs1, rs2),
- _ => panic!("Illegal instruction: {:x}", word),
+ _ => trap_ill(state),
}
}
0b0010011 => {
@@ -240,13 +270,15 @@ fn handle_instruction(state: &mut CpuState) {
7 => andi(state, rd, rs1, imm),
1 => {
if (imm >> 5) != 0 {
- panic!("Illegal instruction: {:x}", word);
+ trap_ill(state);
+ return;
}
slli(state, rd, rs1, imm);
}
5 => {
if (imm >> 5) != 0 {
- panic!("Illegal instruction: {:x}", word);
+ trap_ill(state);
+ return;
}
if (imm >> 5) & 0b1111111 == 0 {
srli(state, rd, rs1, imm);
@@ -256,7 +288,7 @@ fn handle_instruction(state: &mut CpuState) {
}
2 => slti(state, rd, rs1, imm),
3 => sltiu(state, rd, rs1, imm),
- _ => panic!("Illegal instruction: {:x}", word),
+ _ => trap_ill(state),
}
}
0b0000011 => {
@@ -272,7 +304,7 @@ fn handle_instruction(state: &mut CpuState) {
2 => lw(state, rd, rs1, imm),
4 => lbu(state, rd, rs1, imm),
5 => lhu(state, rd, rs1, imm),
- _ => panic!("Illegal instruction: {:x}", word),
+ _ => trap_ill(state)
}
}
0b0100011 => {
@@ -288,7 +320,7 @@ fn handle_instruction(state: &mut CpuState) {
0 => sb(state, rs1, rs2, imm),
1 => sh(state, rs1, rs2, imm),
2 => sw(state, rs1, rs2, imm),
- _ => panic!("Illegal instruction: {:x}", word),
+ _ => trap_ill(state)
};
}
0b1100011 => {
@@ -310,7 +342,7 @@ fn handle_instruction(state: &mut CpuState) {
5 => bge(state, rs1, rs2, imm),
6 => bltu(state, rs1, rs2, imm),
7 => bgeu(state, rs1, rs2, imm),
- _ => panic!("Illegal instruction: {:x}", word),
+ _ => trap_ill(state)
}
return;
}
@@ -331,7 +363,8 @@ fn handle_instruction(state: &mut CpuState) {
let sentinel = (word >> 12) & 0b111;
if sentinel != 0 {
- panic!("Illegal instruction: {:x}", word);
+ trap_ill(state);
+ return;
}
jalr(state, rd as usize, rs1 as usize, imm);
@@ -356,13 +389,29 @@ fn handle_instruction(state: &mut CpuState) {
match f3 {
0 => {
match imm {
- 0 => trap_to_kernel(state), // ecall
- 258 => return_to_userspace(state), // sret
+ 0 => {
+ match state.mode {
+ Mode::MACHINE => {trap_ill(state); return;}
+ USER => {
+ trap_syscall(state); return;}
+ }
+ }, // ecall
+ 0x302 => {
+ let mpp = (state.csr[0x300 /* mstatus */] >> 11) & 0x3;
+ if mpp == 0 {
+ state.mode = USER;
+ } else {
+ state.mode = Mode::MACHINE;
+ }
+
+ state.pc = state.csr[0x341 /* mepc */];
+ return;
+ }, // mret
261 => {
state.on = false;
return;
- }
- _ => panic!("Illegal instruction: {:x}", word),
+ } // wfi
+ _ => trap_ill(state),
}
} // ecall
1 => csrrw(state, rd, rs1, imm as usize),
@@ -372,20 +421,22 @@ fn handle_instruction(state: &mut CpuState) {
6 => csrrsi(state, rd, rs1 as u32, imm as usize),
7 => csrrci(state, rd, rs1 as u32, imm as usize),
- _ => panic!("Illegal instruction: {:x}", word),
+ _ => trap_ill(state),
}
} // ecall (ebreak not implemented)
- _ => panic!("Illegal instruction or todo: {:032b}", word),
+ _ => trap_ill(state),
}
state.pc += 4;
}
-fn trap_to_kernel(state: &mut CpuState) {
- state.csr[0x341 /* mepc */] = state.pc;
+fn trap_ill(state: &mut CpuState) {
+ log(format!("illegal intsruction: {:x}", state.memory.read_instruction(state.pc).expect(format!("pc {:x} does not map to valid memory", state.pc).as_str())).as_str());
state.pc = state.csr[0x305 /* mtvec */];
+ state.csr[0x342 /* mcause */] = 2 /* synchronous, illegal instruction */;
}
-fn return_to_userspace(state: &mut CpuState) {
- state.pc = state.csr[0x341 /* mepc */] + 4;
-}
+fn trap_syscall(state: &mut CpuState) {
+ state.pc = state.csr[0x305 /* mtvec */];
+ state.csr[0x342 /* mcause */] = 8;
+} \ No newline at end of file