Merge remote-tracking branch 'origin/thread_scheduler' into thread_scheduler
# Conflicts: # src/kernel/exception.rs
This commit is contained in:
commit
7060098809
3
.gitignore
vendored
3
.gitignore
vendored
@ -2,3 +2,6 @@
|
|||||||
/.idea
|
/.idea
|
||||||
*.iml
|
*.iml
|
||||||
/*.txt
|
/*.txt
|
||||||
|
/.vscode
|
||||||
|
*.a
|
||||||
|
*.o
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
default:
|
default:
|
||||||
image: rust:1.68
|
image: rust:1.68-bookworm
|
||||||
|
before_script:
|
||||||
|
- wget -q https://cloud.cuwott.fr/s/9fyrejDxMdNRQNn/download/riscv64-cross-compiler-multilib.tar.gz
|
||||||
|
- mkdir /opt/riscv
|
||||||
|
- tar xzf riscv64-cross-compiler-multilib.tar.gz -C /opt/riscv
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- test
|
- test
|
||||||
@ -7,6 +11,8 @@ stages:
|
|||||||
unit-test-job:
|
unit-test-job:
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- echo "Compiling c files"
|
||||||
|
- cd test && make build && cd ..
|
||||||
- echo "Running unit tests..."
|
- echo "Running unit tests..."
|
||||||
- cargo test
|
- cargo test
|
||||||
|
|
||||||
|
31
Makefile
Normal file
31
Makefile
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
TOPDIR=.
|
||||||
|
include $(TOPDIR)/Makefile.config
|
||||||
|
|
||||||
|
|
||||||
|
all: dumps user_lib instruction_tests
|
||||||
|
#
|
||||||
|
# Main targets
|
||||||
|
#
|
||||||
|
|
||||||
|
instruction_tests:
|
||||||
|
$(MAKE) build -C test/riscv_instructions/
|
||||||
|
|
||||||
|
dumps:
|
||||||
|
$(MAKE) dumps -C test/riscv_instructions/
|
||||||
|
mkdir -p ${TOPDIR}/target/dumps/
|
||||||
|
find . -path ${TOPDIR}/target -prune -o -name '*.dump' -exec mv {} ${TOPDIR}/target/dumps/ \;
|
||||||
|
|
||||||
|
user_lib:
|
||||||
|
$(MAKE) -C userlib/
|
||||||
|
|
||||||
|
syscall: user_lib
|
||||||
|
$(MAKE) build -C test/syscall_tests/
|
||||||
|
$(RM) test/syscall_tests/*.o
|
||||||
|
mkdir -p ${TOPDIR}/target/guac/
|
||||||
|
find . -name '*.guac' -exec mv {} ${TOPDIR}/target/guac/ \;
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(MAKE) clean -C userlib/
|
||||||
|
$(MAKE) clean -C test/riscv_instructions/
|
||||||
|
$(RM) -rf $(TOPDIR)/target
|
||||||
|
|
@ -1,5 +1,11 @@
|
|||||||
include $(TOPDIR)/Makefile.config
|
include $(TOPDIR)/Makefile.config
|
||||||
|
|
||||||
USERLIB = $(TOPDIR)/userlib
|
USERLIB = $(TOPDIR)/userlib
|
||||||
|
|
||||||
|
AS = $(RISCV_AS) -c
|
||||||
|
GCC = $(RISCV_GCC)
|
||||||
|
LD = $(RISCV_LD)
|
||||||
|
|
||||||
INCPATH += -I$(TOPDIR) -I$(USERLIB)
|
INCPATH += -I$(TOPDIR) -I$(USERLIB)
|
||||||
LDFLAGS = $(RISCV_LDFLAGS) -T $(USERLIB)/ldscript.lds
|
LDFLAGS = $(RISCV_LDFLAGS) -T $(USERLIB)/ldscript.lds
|
||||||
ASFLAGS = $(RISCV_ASFLAGS) $(INCPATH)
|
ASFLAGS = $(RISCV_ASFLAGS) $(INCPATH)
|
||||||
@ -7,16 +13,19 @@ CFLAGS = $(RISCV_CFLAGS) $(INCPATH)
|
|||||||
|
|
||||||
# Rules
|
# Rules
|
||||||
%.o: %.s
|
%.o: %.s
|
||||||
$(RISCV_AS) $(ASFLAGS) -c $<
|
$(AS) $(ASFLAGS) -c $<
|
||||||
|
|
||||||
%.o: %.c
|
%.o: %.c
|
||||||
$(RISCV_GCC) $(CFLAGS) -c $<
|
$(GCC) $(CFLAGS) -c $<
|
||||||
|
|
||||||
|
%.a: %.o
|
||||||
|
$(AR) $(ARFLAGS) $@ $<
|
||||||
|
|
||||||
%.dump: %.o
|
%.dump: %.o
|
||||||
$(RISCV_OBJCOPY) -j .text -O $(DUMP_FORMAT) $< $@
|
$(RISCV_OBJCOPY) -j .text -O $(DUMP_FORMAT) $< $@
|
||||||
|
|
||||||
%.guac: %.o
|
%.guac: %.o
|
||||||
$(RISCV_LD) $(LDFLAGS) $+ -o $@
|
$(LD) $(LDFLAGS) $+ -o $@
|
||||||
|
|
||||||
# Dependencies
|
# Dependencies
|
||||||
.%.d: %.s
|
.%.d: %.s
|
||||||
@ -31,6 +40,9 @@ CFLAGS = $(RISCV_CFLAGS) $(INCPATH)
|
|||||||
| sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
|
| sed '\''s/\($*\)\.o[ :]*/\1.o $@ : /g'\'' > $@; \
|
||||||
[ -s $@ ] || rm -f $@'
|
[ -s $@ ] || rm -f $@'
|
||||||
|
|
||||||
|
$(PROGRAMS):
|
||||||
|
$(LD) $(LDFLAGS) $+ -o $@
|
||||||
|
|
||||||
# Targets
|
# Targets
|
||||||
#clean:
|
#clean:
|
||||||
# rm -rf *.o 2> /dev/null
|
# rm -rf *.o 2> /dev/null
|
5
build.rs
5
build.rs
@ -1,5 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
cc::Build::new()
|
|
||||||
.file("test/userlib/sys.s")
|
|
||||||
.compile("my-asm-lib");
|
|
||||||
}
|
|
@ -1,7 +1,9 @@
|
|||||||
use libc::printf;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::simulator::{machine::{ExceptionType, Machine}, error::{MachineOk, MachineError}};
|
use crate::simulator::{machine::{ExceptionType, Machine}, error::{MachineOk, MachineError}};
|
||||||
|
|
||||||
|
use super::system::System;
|
||||||
|
|
||||||
|
|
||||||
pub const SC_SHUTDOWN: u8 = 0;
|
pub const SC_SHUTDOWN: u8 = 0;
|
||||||
pub const SC_EXIT: u8 = 1;
|
pub const SC_EXIT: u8 = 1;
|
||||||
@ -42,11 +44,11 @@ pub const SC_DEBUG: u8 = 34;
|
|||||||
pub const CONSOLE_OUTPUT: u8 = 1;
|
pub const CONSOLE_OUTPUT: u8 = 1;
|
||||||
|
|
||||||
// todo : returns new types, not just machine errors and machine ok
|
// todo : returns new types, not just machine errors and machine ok
|
||||||
pub fn call(exception: ExceptionType, machine: &Machine) -> Result<MachineOk, MachineError> {
|
pub fn call(exception: &ExceptionType, machine: &mut Machine, system: &mut System) -> Result<MachineOk, MachineError> {
|
||||||
|
|
||||||
match exception {
|
match exception {
|
||||||
ExceptionType::NoException => todo!(),
|
ExceptionType::NoException => todo!(),
|
||||||
ExceptionType::SyscallException => syscall(machine),
|
ExceptionType::SyscallException => syscall(machine, system),
|
||||||
ExceptionType::PagefaultException => todo!(),
|
ExceptionType::PagefaultException => todo!(),
|
||||||
ExceptionType::ReadOnlyException => todo!(),
|
ExceptionType::ReadOnlyException => todo!(),
|
||||||
ExceptionType::BusErrorException => todo!(),
|
ExceptionType::BusErrorException => todo!(),
|
||||||
@ -57,12 +59,19 @@ pub fn call(exception: ExceptionType, machine: &Machine) -> Result<MachineOk, Ma
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn syscall(machine: &Machine) -> Result<MachineOk, MachineError> {
|
fn syscall(machine: &mut Machine, system: &mut System) -> Result<MachineOk, MachineError> {
|
||||||
let call_type = machine.read_int_register(17) as u8;
|
let call_type = machine.read_int_register(17) as u8;
|
||||||
|
|
||||||
match call_type {
|
match call_type {
|
||||||
SC_SHUTDOWN => Ok(MachineOk::Shutdown),
|
SC_SHUTDOWN => Ok(MachineOk::Shutdown),
|
||||||
SC_EXIT => todo!(),
|
SC_EXIT => {
|
||||||
|
let th = match &system.get_thread_manager().g_current_thread {
|
||||||
|
Some(th) => th.clone(),
|
||||||
|
None => Err("Current thread is None")?
|
||||||
|
};
|
||||||
|
system.get_thread_manager().thread_finish(machine, th);
|
||||||
|
Ok(MachineOk::Ok)
|
||||||
|
},
|
||||||
SC_EXEC => todo!(),
|
SC_EXEC => todo!(),
|
||||||
SC_JOIN => todo!(),
|
SC_JOIN => todo!(),
|
||||||
SC_CREATE => todo!(),
|
SC_CREATE => todo!(),
|
||||||
@ -96,7 +105,13 @@ fn syscall(machine: &Machine) -> Result<MachineOk, MachineError> {
|
|||||||
SC_PERROR => todo!(),
|
SC_PERROR => todo!(),
|
||||||
SC_P => todo!(),
|
SC_P => todo!(),
|
||||||
SC_V => todo!(),
|
SC_V => todo!(),
|
||||||
SC_SEM_CREATE => todo!(),
|
SC_SEM_CREATE => {
|
||||||
|
let addr_name = machine.read_int_register(10);
|
||||||
|
let initial_count = machine.read_int_register((11));
|
||||||
|
let size = get_length_param(addr_name as usize, machine);
|
||||||
|
Ok(MachineOk::Ok)
|
||||||
|
|
||||||
|
},
|
||||||
SC_SEM_DESTROY => todo!(),
|
SC_SEM_DESTROY => todo!(),
|
||||||
SC_LOCK_CREATE => todo!(),
|
SC_LOCK_CREATE => todo!(),
|
||||||
SC_LOCK_DESTROY => todo!(),
|
SC_LOCK_DESTROY => todo!(),
|
||||||
@ -120,6 +135,17 @@ fn syscall(machine: &Machine) -> Result<MachineOk, MachineError> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_length_param(addr: usize, machine: & Machine) -> usize{
|
||||||
|
let mut i = 0;
|
||||||
|
let mut c = 1;
|
||||||
|
while c!= 0 {
|
||||||
|
c = machine.read_memory(1, addr + i);
|
||||||
|
i+=1;
|
||||||
|
|
||||||
|
}
|
||||||
|
i + 1
|
||||||
|
}
|
||||||
|
|
||||||
fn get_string_param(addr: i64, maxlen: i64, machine: &Machine) -> Vec<char>{
|
fn get_string_param(addr: i64, maxlen: i64, machine: &Machine) -> Vec<char>{
|
||||||
let mut dest = Vec::with_capacity(maxlen as usize);
|
let mut dest = Vec::with_capacity(maxlen as usize);
|
||||||
|
|
||||||
@ -141,6 +167,7 @@ fn get_string_param(addr: i64, maxlen: i64, machine: &Machine) -> Vec<char>{
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::kernel::exception::{SC_SHUTDOWN, SC_WRITE};
|
use crate::kernel::exception::{SC_SHUTDOWN, SC_WRITE};
|
||||||
|
use crate::kernel::system::System;
|
||||||
use crate::simulator::instruction::Instruction;
|
use crate::simulator::instruction::Instruction;
|
||||||
use crate::simulator::machine::Machine;
|
use crate::simulator::machine::Machine;
|
||||||
|
|
||||||
@ -152,8 +179,8 @@ mod test {
|
|||||||
|
|
||||||
machine.write_memory(4, 0, 0b000000000000_00000_000_00000_1110011); // ecall
|
machine.write_memory(4, 0, 0b000000000000_00000_000_00000_1110011); // ecall
|
||||||
machine.write_memory(4, 4, 0b000000001010_00000_000_00001_0010011); // r1 <- 10
|
machine.write_memory(4, 4, 0b000000001010_00000_000_00001_0010011); // r1 <- 10
|
||||||
|
let mut system = System::default();
|
||||||
machine.run();
|
machine.run(&mut system);
|
||||||
// If the machine was stopped with no error, the shutdown worked
|
// If the machine was stopped with no error, the shutdown worked
|
||||||
assert_ne!(machine.read_int_register(1), 10); // Check if the next instruction was executed
|
assert_ne!(machine.read_int_register(1), 10); // Check if the next instruction was executed
|
||||||
}
|
}
|
||||||
@ -183,8 +210,8 @@ mod test {
|
|||||||
machine.write_memory(4, 4, 0b000000000000_00000_000_10001_0010011); // r17 <- SC_SHUTDOWN
|
machine.write_memory(4, 4, 0b000000000000_00000_000_10001_0010011); // r17 <- SC_SHUTDOWN
|
||||||
machine.write_memory(4, 8, 0b000000000000_00000_000_00000_1110011); // ecall
|
machine.write_memory(4, 8, 0b000000000000_00000_000_00000_1110011); // ecall
|
||||||
|
|
||||||
|
let mut system = System::default();
|
||||||
machine.run();
|
machine.run(&mut system);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
mod process;
|
pub mod process;
|
||||||
pub mod thread;
|
pub mod thread;
|
||||||
pub mod mgerror;
|
pub mod mgerror;
|
||||||
pub mod system;
|
pub mod system;
|
||||||
|
@ -16,7 +16,7 @@ macro_rules! get_new_thread {
|
|||||||
pub struct ThreadContext {
|
pub struct ThreadContext {
|
||||||
pub int_registers: [i64; NUM_INT_REGS],
|
pub int_registers: [i64; NUM_INT_REGS],
|
||||||
pub float_registers: [f32; NUM_FP_REGS],
|
pub float_registers: [f32; NUM_FP_REGS],
|
||||||
pub pc: i64,
|
pub pc: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
@ -47,10 +47,10 @@ impl Thread {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_thread_context(&mut self, initial_pc_reg: i64, initial_sp: i64, arg: i64) {
|
pub fn init_thread_context(&mut self, initial_pc_reg: u64, initial_sp: u64, arg: i64) {
|
||||||
self.thread_context.pc = initial_pc_reg;
|
self.thread_context.pc = initial_pc_reg;
|
||||||
self.thread_context.int_registers[10] = arg;
|
self.thread_context.int_registers[10] = arg;
|
||||||
self.thread_context.int_registers[STACK_REG] = initial_sp;
|
self.thread_context.int_registers[STACK_REG] = initial_sp as i64;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
|
pub fn init_simulator_context(&self, base_stack_addr: [i8; SIMULATORSTACKSIZE]) {
|
||||||
|
@ -78,17 +78,19 @@ impl ThreadManager {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
|
self.thread_restore_processor_state(machine, Rc::clone(&next_thread));
|
||||||
|
// next_thread.restore_simulator_state();
|
||||||
|
self.set_g_current_thread(Some(next_thread));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start a thread, attaching it to a process
|
/// Start a thread, attaching it to a process
|
||||||
pub fn start_thread(&mut self, thread: Rc<RefCell<Thread>>, owner: Process, func_pc: i64, argument: i64) {
|
pub fn start_thread(&mut self, thread: Rc<RefCell<Thread>>, owner: Process, func_pc: u64, sp_loc: u64, argument: i64) {
|
||||||
let mut thread_m = thread.borrow_mut();
|
let mut thread_m = thread.borrow_mut();
|
||||||
assert_eq!(thread_m.process, Option::None);
|
assert_eq!(thread_m.process, Option::None);
|
||||||
thread_m.process = Option::Some(owner);
|
thread_m.process = Option::Some(owner);
|
||||||
let ptr = 0; // todo addrspace
|
let ptr = sp_loc; // todo addrspace
|
||||||
thread_m.init_thread_context(func_pc, ptr, argument);
|
thread_m.init_thread_context(func_pc, ptr, argument);
|
||||||
let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray
|
let base_stack_addr: [i8; SIMULATORSTACKSIZE] = [0; SIMULATORSTACKSIZE]; // todo AllocBoundedArray
|
||||||
thread_m.init_simulator_context(base_stack_addr);
|
thread_m.init_simulator_context(base_stack_addr);
|
||||||
@ -121,8 +123,8 @@ impl ThreadManager {
|
|||||||
|
|
||||||
/// Put the thread to sleep and relinquish the processor
|
/// Put the thread to sleep and relinquish the processor
|
||||||
pub fn thread_sleep(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
pub fn thread_sleep(&mut self, machine: &mut Machine, thread: Rc<RefCell<Thread>>) {
|
||||||
assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
|
debug_assert_eq!(Option::Some(Rc::clone(&thread)), self.g_current_thread);
|
||||||
assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
|
debug_assert_eq!(machine.interrupt.get_status(), InterruptStatus::InterruptOff);
|
||||||
|
|
||||||
let mut next_thread = self.find_next_to_run();
|
let mut next_thread = self.find_next_to_run();
|
||||||
while next_thread.is_none() {
|
while next_thread.is_none() {
|
||||||
@ -139,6 +141,8 @@ impl ThreadManager {
|
|||||||
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
let old_status = machine.interrupt.set_status(InterruptStatus::InterruptOff);
|
||||||
self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread));
|
self.g_thread_to_be_destroyed = Option::Some(Rc::clone(&thread));
|
||||||
self.g_alive.remove(Rc::clone(&thread));
|
self.g_alive.remove(Rc::clone(&thread));
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
println!("Sleeping thread {}", thread.borrow().get_name());
|
||||||
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
|
// g_objets_addrs->removeObject(self.thread) // a ajouté plus tard
|
||||||
self.thread_sleep(machine, Rc::clone(&thread));
|
self.thread_sleep(machine, Rc::clone(&thread));
|
||||||
machine.interrupt.set_status(old_status);
|
machine.interrupt.set_status(old_status);
|
||||||
@ -153,6 +157,7 @@ impl ThreadManager {
|
|||||||
for i in 0..NUM_FP_REGS {
|
for i in 0..NUM_FP_REGS {
|
||||||
t.thread_context.float_registers[i] = machine.read_fp_register(i);
|
t.thread_context.float_registers[i] = machine.read_fp_register(i);
|
||||||
}
|
}
|
||||||
|
t.thread_context.pc = machine.pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Restore the CPU state of a user program on a context switch.
|
/// Restore the CPU state of a user program on a context switch.
|
||||||
@ -161,6 +166,7 @@ impl ThreadManager {
|
|||||||
for i in 0..NUM_INT_REGS {
|
for i in 0..NUM_INT_REGS {
|
||||||
machine.write_int_register(i, t.thread_context.int_registers[i]);
|
machine.write_int_register(i, t.thread_context.int_registers[i]);
|
||||||
}
|
}
|
||||||
|
machine.pc = t.thread_context.pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Currently running thread
|
/// Currently running thread
|
||||||
@ -196,28 +202,42 @@ impl ThreadManager {
|
|||||||
mod test {
|
mod test {
|
||||||
use std::{rc::Rc, cell::RefCell};
|
use std::{rc::Rc, cell::RefCell};
|
||||||
|
|
||||||
use crate::{simulator::machine::Machine, kernel::{system::System, thread::Thread, process::Process}};
|
use crate::{simulator::{machine::Machine, loader}, kernel::{system::System, thread::{Thread, self}, process::Process}};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[ignore = "Pas encore terminé, contient des bugs"]
|
||||||
fn test_thread_context() {
|
fn test_thread_context() {
|
||||||
let mut machine = Machine::init_machine();
|
let mut machine = Machine::init_machine();
|
||||||
let mut system = System::default();
|
let (loader, ptr1) = loader::Loader::new("./test/riscv_instructions/simple_arithmetics/unsigned_addition", &mut machine, 0).expect("IO Error");
|
||||||
|
println!("{}", ptr1);
|
||||||
|
let (loader2, ptr2) = loader::Loader::new("./test/riscv_instructions/syscall_tests/halt", &mut machine, ptr1 as usize).expect("IO Error");
|
||||||
|
let start_pc = loader.elf_header.entrypoint;
|
||||||
|
let system = &mut System::default();
|
||||||
|
|
||||||
let thread1 = Thread::new("th1");
|
let thread1 = Thread::new("th1");
|
||||||
let thread1 = Rc::new(RefCell::new(thread1));
|
let thread1 = Rc::new(RefCell::new(thread1));
|
||||||
system.get_thread_manager().get_g_alive().push(Rc::clone(&thread1));
|
system.get_thread_manager().get_g_alive().push(Rc::clone(&thread1));
|
||||||
let owner = Process { num_thread: 0 };
|
|
||||||
system.get_thread_manager().start_thread(Rc::clone(&thread1), owner, thread1_func as i64, 0);
|
|
||||||
assert_eq!(thread1.borrow_mut().thread_context.pc, thread1_func as i64);
|
|
||||||
let to_run = system.get_thread_manager().find_next_to_run().unwrap();
|
|
||||||
assert_eq!(to_run, Rc::clone(&thread1));
|
|
||||||
assert!(system.get_thread_manager().get_g_alive().contains(&Rc::clone(&thread1)));
|
|
||||||
system.get_thread_manager().switch_to(&mut machine, Rc::clone(&to_run));
|
|
||||||
|
|
||||||
println!("{:#?}", thread1.borrow_mut().thread_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn thread1_func() {
|
let thread2 = Thread::new("th2");
|
||||||
println!("Hello");
|
let thread2 = Rc::new(RefCell::new(thread2));
|
||||||
|
system.get_thread_manager().get_g_alive().push(Rc::clone(&thread1));
|
||||||
|
|
||||||
|
let owner1 = Process { num_thread: 0 };
|
||||||
|
system.get_thread_manager().start_thread(Rc::clone(&thread1), owner1, loader.elf_header.entrypoint, ptr1, -1);
|
||||||
|
debug_assert_eq!(thread1.borrow_mut().thread_context.pc, start_pc);
|
||||||
|
debug_assert!(system.get_thread_manager().get_g_alive().contains(&Rc::clone(&thread1)));
|
||||||
|
|
||||||
|
let owner2 = Process { num_thread: 0 };
|
||||||
|
system.get_thread_manager().start_thread(Rc::clone(&thread2), owner2, ptr1 + loader2.elf_header.entrypoint, ptr2 , -1);
|
||||||
|
|
||||||
|
let to_run = system.get_thread_manager().find_next_to_run().unwrap();
|
||||||
|
debug_assert_eq!(to_run, Rc::clone(&thread1));
|
||||||
|
|
||||||
|
system.get_thread_manager().switch_to(&mut machine, Rc::clone(&to_run));
|
||||||
|
debug_assert_eq!(system.get_thread_manager().g_current_thread, Option::Some(Rc::clone(&thread1)));
|
||||||
|
debug_assert_eq!(machine.pc, loader.elf_header.entrypoint);
|
||||||
|
|
||||||
|
machine.run(system);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -18,6 +18,6 @@ use simulator::machine::Machine;
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut machine = Machine::init_machine();
|
let mut machine = Machine::init_machine();
|
||||||
let system = System::default();
|
let mut system = System::default();
|
||||||
machine.run()
|
machine.run(&mut system);
|
||||||
}
|
}
|
||||||
|
@ -1,34 +1,650 @@
|
|||||||
use crate::Machine;
|
use crate::Machine;
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io::Read;
|
||||||
use std::io::BufRead;
|
|
||||||
|
|
||||||
|
/// load a 32-bits binary file into the machine
|
||||||
|
///
|
||||||
/// Load a file into a new machine
|
/// ### Parameters
|
||||||
///
|
///
|
||||||
/// `panic!` when size is not 1, 2, 4 or 8
|
/// - **path** path of the file to load
|
||||||
/// `panic!` when the text does not represents instructions in hexadecimal
|
/// - **machine** the machine where the bin file will be loaded
|
||||||
///
|
/// - **start_index** at which index of machine memory you want to start to load the program
|
||||||
/// ### Parameters
|
///
|
||||||
///
|
/// Returns in a Result any io error
|
||||||
/// - **path** the path of the file to load
|
pub fn load(path: &str, machine: &mut Machine, start_index: usize) -> Result<(), std::io::Error> {
|
||||||
/// - **size** the number of bytes to write (1, 2, 4 or 8)
|
let mut file = fs::File::open(path)?;
|
||||||
pub fn _load(path : &str, instruction_size: i32) -> Machine {
|
let mut instructions: Vec<u32> = Default::default();
|
||||||
let file = fs::File::open(path).expect("Wrong filename");
|
loop {
|
||||||
let reader = io::BufReader::new(file);
|
let mut buf: [u8; 4] = [0; 4];
|
||||||
let mut machine = Machine::init_machine();
|
let res = file.read(&mut buf)?;
|
||||||
|
if res == 0 {
|
||||||
for (i,line) in reader.lines().enumerate() {
|
break; // eof
|
||||||
let res = u64::from_str_radix(&line.unwrap(), 16);
|
} else {
|
||||||
match res {
|
instructions.push(u32::from_le_bytes(buf));
|
||||||
Ok(value) => {
|
|
||||||
Machine::write_memory(&mut machine, instruction_size, i*instruction_size as usize, value);
|
|
||||||
},
|
|
||||||
_ => panic!()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("{:x}", Machine::read_memory(& mut machine, 4, 0));
|
for (i, inst) in instructions.iter().enumerate() {
|
||||||
machine
|
machine.write_memory(4, 4 * i + start_index, inst.to_owned() as u64);
|
||||||
|
}
|
||||||
|
// #[cfg(debug_assertions)]
|
||||||
|
// println!("{:04x?}", instructions); // only print loaded program in debug build
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The elf header defines principes aspects of the binary files, it's place at the start of the file
|
||||||
|
/// see <https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_header> for more informations
|
||||||
|
pub struct ElfHeader {
|
||||||
|
/// Defines whether the file is big or little endian
|
||||||
|
/// true correspond to big endian, false otherwise
|
||||||
|
///
|
||||||
|
/// Offset: 0x05, size: 1 byte
|
||||||
|
pub endianess: bool,
|
||||||
|
/// Defines whether the file is 32 bits or 64 bits
|
||||||
|
///
|
||||||
|
/// Offset: 0x04, size: 1 byte
|
||||||
|
pub is_32bits: bool,
|
||||||
|
/// Version of the elf file, current version is 1
|
||||||
|
///
|
||||||
|
/// Offset: 0x06, size: 1 byte
|
||||||
|
pub version: u8,
|
||||||
|
/// Identifies the target ABI.
|
||||||
|
///
|
||||||
|
/// In this implementation: Defines if the target abi is system V compliant
|
||||||
|
///
|
||||||
|
/// Offset: 0x07, size: 1 byte
|
||||||
|
pub sys_v_abi: bool,
|
||||||
|
/// Identifies target ISA, 0xF3 correspond to RISC-V
|
||||||
|
///
|
||||||
|
/// In this implementatio, true if target isa is RISC-V, false otherwise
|
||||||
|
///
|
||||||
|
/// Offset: 0x12, size: 2 bytes
|
||||||
|
pub is_riscv_target: bool,
|
||||||
|
/// Memory address of the entry point from w<here the process starts its execution.
|
||||||
|
/// If the program doesn't have an entrypoint (i.e. not an executable), the value is 0
|
||||||
|
///
|
||||||
|
/// Offset: 0x18, size: 4 (32 bits) or 8 (64 bits)
|
||||||
|
pub entrypoint: u64,
|
||||||
|
/// Size of the elf header, 64 bytes for 64 bits and 52 for 32 bits
|
||||||
|
///
|
||||||
|
/// Offset: 0x28(32 bits) or 0x34 (64 bits), size: 2 bytes
|
||||||
|
pub elf_header_size: u16,
|
||||||
|
/// Position of the first program header entry
|
||||||
|
///
|
||||||
|
/// Offset: 0x1C (32 bits) or 0x20 (64 bits), size: 4 (32 bits) or 8 (64 bits) bytes
|
||||||
|
pub program_header_location: u64,
|
||||||
|
/// Number of entries in the progream header table
|
||||||
|
///
|
||||||
|
/// Offset: 0x2C (32 bits) or 0x38 (64 bits), size: 2 bytes
|
||||||
|
pub program_header_entries: u16,
|
||||||
|
/// Size of a program header entry
|
||||||
|
///
|
||||||
|
/// Offset: 0x2A (32 bits) or 0x36 (64 bits), size: 2 bytes
|
||||||
|
pub program_header_size: u16,
|
||||||
|
/// Position of the first section header entry
|
||||||
|
///
|
||||||
|
/// Offset: 0x20 (32 bits) or 0x28 (64 bits), size: 4 (32 bits) or 8 (64 bits) bytes
|
||||||
|
pub section_header_location: u64,
|
||||||
|
/// Number of entries in the section header table
|
||||||
|
///
|
||||||
|
/// Offset: 0x30 (32 bits) or 0x3C (64 bits), size: 2 bytes
|
||||||
|
pub section_header_entries: u16,
|
||||||
|
/// Size of a section header entry
|
||||||
|
///
|
||||||
|
/// Offset: 0x2E (32 bits) or 0x36 (64 bits), size: 2 bytes
|
||||||
|
pub section_header_size: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ElfHeader {
|
||||||
|
|
||||||
|
/// return true if the 4 first bytes constitude the elf magic number
|
||||||
|
fn is_elf(instructions: &[u8]) -> bool {
|
||||||
|
instructions.get(0..4) == Option::Some(&[0x7f, 0x45, 0x4c, 0x46])
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return true if big endian, false otherwise
|
||||||
|
fn check_endianess(instructions: &[u8]) -> bool {
|
||||||
|
instructions.get(5) == Option::Some(&2)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return true if file is 32 bits, false if 64 bits
|
||||||
|
fn is_32bits(instructions: &[u8]) -> bool {
|
||||||
|
instructions.get(4) == Option::Some(&1)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return the version of the elf file (should be 1)
|
||||||
|
/// Can be None if the file is smaller than 7 bytes -> the file is invalid
|
||||||
|
fn get_version(instructions: &[u8]) -> Option<u8> {
|
||||||
|
instructions.get(6).copied() // work as primitives implements Copy
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return true if target abi of the binary file is System V, false otherwise
|
||||||
|
fn is_system_v_elf(instructions: &[u8]) -> bool {
|
||||||
|
instructions.get(7) == Option::Some(&0)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return true if specified target instruction set architecture is RISCV
|
||||||
|
fn is_riscv_isa(instructions: &[u8]) -> bool {
|
||||||
|
Self::get_u16_value(instructions, 0x12) == Option::Some(0xf3)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// memory address of the entry point from where the process starts its execution
|
||||||
|
///
|
||||||
|
/// ## Paramters:
|
||||||
|
///
|
||||||
|
/// **instructions** List of bytes of the loaded binary file
|
||||||
|
/// **is_32bits** defines whether the binary file is 32 bits or 64 bits
|
||||||
|
fn get_entrypoint(instructions: &[u8], is_32bits: bool) -> Option<u64> {
|
||||||
|
if is_32bits {
|
||||||
|
get_address_point(instructions, 0x18, true)
|
||||||
|
} else {
|
||||||
|
get_address_point(instructions, 0x18, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Memory address of the start of the program header table
|
||||||
|
///
|
||||||
|
/// ## Paramters:
|
||||||
|
///
|
||||||
|
/// **instructions** List of bytes of the loaded binary file
|
||||||
|
/// **is_32bits** defines whether the binary file is 32 bits or 64 bits
|
||||||
|
fn get_program_header_table_location(instructions: &[u8], is_32bits: bool) -> Option<u64> {
|
||||||
|
if is_32bits {
|
||||||
|
get_address_point(instructions, 0x1c, true)
|
||||||
|
} else {
|
||||||
|
get_address_point(instructions, 0x20, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Memory address of the start of the section header table
|
||||||
|
///
|
||||||
|
/// ## Paramters:
|
||||||
|
///
|
||||||
|
/// **instructions** List of bytes of the loaded binary file
|
||||||
|
/// **is_32bits** defines whether the binary file is 32 bits or 64 bits
|
||||||
|
fn get_section_header_table_location(instructions: &[u8], is_32bits: bool) -> Option<u64> {
|
||||||
|
if is_32bits {
|
||||||
|
get_address_point(instructions, 0x20, true)
|
||||||
|
} else {
|
||||||
|
get_address_point(instructions, 0x28, false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the size of the header, normally, 0x40 for 64 bits bin and 0x34 for 32 bits
|
||||||
|
///
|
||||||
|
/// ## Paramters:
|
||||||
|
///
|
||||||
|
/// **instructions** List of bytes of the loaded binary file
|
||||||
|
/// **is_32bits** defines whether the binary file is 32 bits or 64 bits
|
||||||
|
fn get_elf_header_size(instructions: &[u8], is_32bits: bool) -> Option<u16> {
|
||||||
|
let address = if is_32bits { 0x28 } else { 0x34 };
|
||||||
|
Self::get_u16_value(instructions, address)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return the size of a program header table entry
|
||||||
|
///
|
||||||
|
/// ## Paramters:
|
||||||
|
///
|
||||||
|
/// **instructions** List of bytes of the loaded binary file
|
||||||
|
/// **is_32bits** defines whether the binary file is 32 bits or 64 bits
|
||||||
|
fn get_program_header_size(instructions: &[u8], is_32bits: bool) -> Option<u16> {
|
||||||
|
let address = if is_32bits { 0x2a } else { 0x36 };
|
||||||
|
Self::get_u16_value(instructions, address)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return the number of entries in the program header
|
||||||
|
///
|
||||||
|
/// ## Paramters:
|
||||||
|
///
|
||||||
|
/// **instructions** List of bytes of the loaded binary file
|
||||||
|
/// **is_32bits** defines whether the binary file is 32 bits or 64 bits
|
||||||
|
fn get_number_entries_program_header(instructions: &[u8], is_32bits: bool) -> Option<u16> {
|
||||||
|
let address = if is_32bits { 0x2c } else { 0x38 };
|
||||||
|
Self::get_u16_value(instructions, address)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the size of a section header table entry
|
||||||
|
///
|
||||||
|
/// ## Paramters:
|
||||||
|
///
|
||||||
|
/// **instructions** List of bytes of the loaded binary file
|
||||||
|
/// **is_32bits** defines whether the binary file is 32 bits or 64 bits
|
||||||
|
fn get_section_header_size(instructions: &[u8], is_32bits: bool) -> Option<u16> {
|
||||||
|
let address = if is_32bits { 0x2e } else { 0x3a };
|
||||||
|
Self::get_u16_value(instructions, address)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the number of entries in the section header
|
||||||
|
///
|
||||||
|
/// ## Paramters:
|
||||||
|
///
|
||||||
|
/// **instructions** List of bytes of the loaded binary file
|
||||||
|
/// **is_32bits** defines whether the binary file is 32 bits or 64 bits
|
||||||
|
fn get_section_header_num_entries(instructions: &[u8], is_32bits: bool) -> Option<u16> {
|
||||||
|
let address = if is_32bits { 0x30 } else { 0x3c };
|
||||||
|
Self::get_u16_value(instructions, address)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return a u16 value, usually for the size or the number of entries inside a header
|
||||||
|
///
|
||||||
|
/// This method retrieve 2 bytes and concatenate them assuming the file is little endian
|
||||||
|
///
|
||||||
|
/// ## Paramters:
|
||||||
|
///
|
||||||
|
/// **instructions** List of bytes of the loaded binary file
|
||||||
|
/// **address** Position of the first byte
|
||||||
|
fn get_u16_value(instructions: &[u8], address: usize) -> Option<u16> {
|
||||||
|
let mut bytes: [u8; 2] = [0; 2];
|
||||||
|
bytes[0] = instructions.get(address).copied()?;
|
||||||
|
bytes[1] = instructions.get(address + 1).copied()?;
|
||||||
|
Option::Some(u16::from_le_bytes(bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&Vec<u8>> for ElfHeader {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(instructions: &Vec<u8>) -> Result<Self, Self::Error> {
|
||||||
|
if Self::is_elf(instructions) {
|
||||||
|
let format = Self::is_32bits(instructions);
|
||||||
|
let endianess = Self::check_endianess(instructions);
|
||||||
|
let version = Self::get_version(instructions).ok_or(())?;
|
||||||
|
let is_sys_v_abi = Self::is_system_v_elf(instructions);
|
||||||
|
let is_rv_target = Self::is_riscv_isa(instructions);
|
||||||
|
let entrypoint = Self::get_entrypoint(instructions, format).ok_or(())?;
|
||||||
|
let elf_header_size = Self::get_elf_header_size(instructions, format).ok_or(())?;
|
||||||
|
let program_header_location = Self::get_program_header_table_location(instructions, format).ok_or(())?;
|
||||||
|
let program_header_entries = Self::get_number_entries_program_header(instructions, format).ok_or(())? ;
|
||||||
|
let program_header_size = Self::get_program_header_size(instructions, format).ok_or(())?;
|
||||||
|
let section_header_location = Self::get_section_header_table_location(instructions, format).ok_or(())?;
|
||||||
|
let section_header_entries = Self::get_section_header_num_entries(instructions, format).ok_or(())?;
|
||||||
|
let section_header_size = Self::get_section_header_size(instructions, format).ok_or(())?;
|
||||||
|
Ok(ElfHeader {
|
||||||
|
endianess,
|
||||||
|
is_32bits: format,
|
||||||
|
version,
|
||||||
|
sys_v_abi: is_sys_v_abi,
|
||||||
|
is_riscv_target: is_rv_target,
|
||||||
|
entrypoint,
|
||||||
|
elf_header_size,
|
||||||
|
program_header_location,
|
||||||
|
program_header_entries,
|
||||||
|
program_header_size,
|
||||||
|
section_header_location,
|
||||||
|
section_header_entries,
|
||||||
|
section_header_size
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Flag of a section, a section can have multiples flags by adding the values
|
||||||
|
#[allow(clippy::enum_variant_names)]
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub enum FlagValue {
|
||||||
|
/// The section is writable
|
||||||
|
ShfWrite = 0x1,
|
||||||
|
/// The section need to be allocate/occupe memory during execution
|
||||||
|
ShfAlloc = 0x2,
|
||||||
|
/// The section need to be executable
|
||||||
|
ShfExecinstr = 0x4,
|
||||||
|
/// Section might ber merged
|
||||||
|
ShfMerge = 0x10,
|
||||||
|
/// Contain null-terminated (\0) strings
|
||||||
|
ShfStrings = 0x20,
|
||||||
|
// There is others but are unrelevant (I think)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Section header entry, contains useful informations for each sections of the binary file
|
||||||
|
///
|
||||||
|
/// see <https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#Section_header>
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct SectionHeader {
|
||||||
|
/// Offset to a string in .shstrtab section that represent the name of this section
|
||||||
|
///
|
||||||
|
/// Offset: 0x0, size: 4 bytes
|
||||||
|
pub name_offset: u32,
|
||||||
|
/// Identify the type of this header
|
||||||
|
///
|
||||||
|
/// Offset: 0x4, size: 4 bytes
|
||||||
|
pub header_type: u32,
|
||||||
|
/// Identify the atributes of this section
|
||||||
|
///
|
||||||
|
/// see `Self::does_flag_contains_key(self, FlagValue)`
|
||||||
|
///
|
||||||
|
/// Offset: 0x8, size: 4 (32 bits) or 8 (64 bits) bytes
|
||||||
|
pub flags: u64,
|
||||||
|
/// Virtual address of the section in memory if section is loaded, 0x0 otherwise
|
||||||
|
///
|
||||||
|
/// Offset: 0x0C (32 bits) or 0x10 (64 bits), size: 4 (32 bits) or 8 (64 bits) bytes
|
||||||
|
pub virt_addr: u64,
|
||||||
|
/// Offset of the section in the file image (binary file)
|
||||||
|
///
|
||||||
|
/// Offset: 0x10 (32 bits) or 0x18 (64 bits), size: 4 (32 bits) or 8 (64 bits) bytes
|
||||||
|
pub image_offset: u64,
|
||||||
|
/// Size of the section in the file image, may be 0
|
||||||
|
///
|
||||||
|
/// Offset: 0x14 (32 bits) or 0x20 (64 bits), size: 4 (32 bits) or 8 (64 bits) bytes
|
||||||
|
pub section_size: u64,
|
||||||
|
pub section_link: u32,
|
||||||
|
pub section_info: u32,
|
||||||
|
/// Contain the required alignment of the section, must be a power of 2
|
||||||
|
///
|
||||||
|
/// Offset: 0x20 (32 bits) or 0x30 (64 bits), size: 4 (32 bits) or 8 (64 bits) bytes
|
||||||
|
pub required_align: u64,
|
||||||
|
/// Contain the size of each entry, for sections that contain fixed size entries, otherwise 0
|
||||||
|
///
|
||||||
|
/// Offset: 0x24 (32 bits) or 0x38 (64 bits), size: 4 (32 bits) or 8 (64 bits) bytes
|
||||||
|
pub entry_size: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SectionHeader {
|
||||||
|
|
||||||
|
/// return true if flag of this section contains / have `key`, false otherwise
|
||||||
|
pub fn does_flag_contains_key(&self, key: FlagValue) -> bool {
|
||||||
|
self.flags & key as u64 != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the offset to a string in .shstrtab that represents the name of this section
|
||||||
|
fn get_name_offset(instructions: &[u8], address: usize) -> Option<u32> {
|
||||||
|
get_address_point(instructions, address, true).map(|v| { v as u32 })
|
||||||
|
// set true to return a u32
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the type of header of the section
|
||||||
|
fn get_header_type(instructions: &[u8], address: usize) -> Option<u32> {
|
||||||
|
get_address_point(instructions, address + 0x4, true).map(|v| { v as u32 })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the flags of the section, can hold multiples values, see [`FlagValue`]
|
||||||
|
fn get_flags(instructions: &[u8], address: usize, is_32bits: bool) -> Option<u64> {
|
||||||
|
get_address_point(instructions, address + 0x8, is_32bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Return the virtual address of the section in memory if the sectino is loaded(see section flag), otherwise 0
|
||||||
|
fn get_virtual_address(instructions: &[u8], address: usize, is_32bits: bool) -> Option<u64> {
|
||||||
|
get_address_point(instructions, address + if is_32bits { 0x0C } else { 0x10 }, is_32bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the offset of the section in the file image (binary file)
|
||||||
|
fn get_image_offset(instructions: &[u8], address: usize, is_32bits: bool) -> Option<u64> {
|
||||||
|
get_address_point(instructions, address + if is_32bits { 0x10 } else { 0x18 }, is_32bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the size of the section in the file image (binary file), may be 0
|
||||||
|
fn get_section_size(instructions: &[u8], address: usize, is_32bits: bool) -> Option<u64> {
|
||||||
|
get_address_point(instructions, address + if is_32bits { 0x14 } else { 0x20 }, is_32bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_section_link(instructions: &[u8], address: usize, is_32bits: bool) -> Option<u32> {
|
||||||
|
get_address_point(instructions, address + if is_32bits { 0x18 } else { 0x28 }, false).map(|v| { v as u32 })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_section_info(instructions: &[u8], address: usize, is_32bits: bool) -> Option<u32> {
|
||||||
|
get_address_point(instructions, address + if is_32bits { 0x1C } else { 0x2C }, false).map(|v| { v as u32 })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return the required alignment of the section, must be a power of 2
|
||||||
|
fn get_required_align(instructions: &[u8], address: usize, is_32bits: bool) -> Option<u64> {
|
||||||
|
get_address_point(instructions, address + if is_32bits { 0x20 } else { 0x30 }, is_32bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Contain the size of each entry for sections that contain fixed-size entries, otherwise 0
|
||||||
|
fn get_entry_size(instructions: &[u8], address: usize, is_32bits: bool) -> Option<u64> {
|
||||||
|
get_address_point(instructions, address + if is_32bits { 0x24 } else { 0x38 }, is_32bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TryFrom<(&[u8], u64, bool)> for SectionHeader {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(value: (&[u8], u64, bool)) -> Result<Self, Self::Error> {
|
||||||
|
let instructions = value.0;
|
||||||
|
let address = value.1 as usize;
|
||||||
|
let is_32bits = value.2;
|
||||||
|
|
||||||
|
let name_offset = Self::get_name_offset(instructions, address).ok_or(())?;
|
||||||
|
let header_type = Self::get_header_type(instructions, address).ok_or(())?;
|
||||||
|
let attribute = Self::get_flags(instructions, address, is_32bits).ok_or(())?;
|
||||||
|
let virt_addr = Self::get_virtual_address(instructions, address, is_32bits).ok_or(())?;
|
||||||
|
let image_offset = Self::get_image_offset(instructions, address, is_32bits).ok_or(())?;
|
||||||
|
let section_size = Self::get_section_size(instructions, address, is_32bits).ok_or(())?;
|
||||||
|
let section_link = Self::get_section_link(instructions, address, is_32bits).ok_or(())?;
|
||||||
|
let section_info = Self::get_section_info(instructions, address, is_32bits).ok_or(())?;
|
||||||
|
let required_align = Self::get_required_align(instructions, address, is_32bits).ok_or(())?;
|
||||||
|
let entry_size = Self::get_entry_size(instructions, address, is_32bits).ok_or(())?;
|
||||||
|
Ok(Self { name_offset,
|
||||||
|
header_type,
|
||||||
|
flags: attribute,
|
||||||
|
virt_addr,
|
||||||
|
image_offset,
|
||||||
|
section_size,
|
||||||
|
section_link,
|
||||||
|
section_info,
|
||||||
|
required_align,
|
||||||
|
entry_size
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Error enum for [`Loader`]
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum LoaderError {
|
||||||
|
/// Correspond to std IO error
|
||||||
|
IOError(std::io::Error),
|
||||||
|
/// Others errors
|
||||||
|
ParsingError
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Global structure of the loader, one instance per loaded files
|
||||||
|
pub struct Loader {
|
||||||
|
/// List of bytes inside the binary file
|
||||||
|
bytes: Vec<u8>,
|
||||||
|
/// Elf header, see [`ElfHeader`] for more informations
|
||||||
|
pub elf_header: ElfHeader,
|
||||||
|
/// Section header table entries, see [`SectionHeader`] for more informations
|
||||||
|
pub sections: Vec<SectionHeader>
|
||||||
|
}
|
||||||
|
impl Loader {
|
||||||
|
|
||||||
|
/// # Loader constructor
|
||||||
|
///
|
||||||
|
/// Load the binary file given in parameter, parse it and load inside the machine memory
|
||||||
|
/// return the loader instance and the location of the end of the last a allocated section in memory
|
||||||
|
///
|
||||||
|
/// ## Parameters
|
||||||
|
///
|
||||||
|
/// **path**: location of the binary file on disk
|
||||||
|
/// **machine**: well, the risc-v simulator
|
||||||
|
/// **start_index**: The position at which you want to start to allocate the program
|
||||||
|
pub fn new(path: &str, machine: &mut Machine, start_index: usize) -> Result<(Self, u64), LoaderError> {
|
||||||
|
let loader = Self::load_and_parse(path)?;
|
||||||
|
let end_alloc = loader.load_into_machine(machine, start_index)?;
|
||||||
|
Ok((loader, end_alloc))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try to load the binary file in memory after it been parsed
|
||||||
|
///
|
||||||
|
/// Binary file is loaded according to sections order and rules, see [`SectionHeader`]
|
||||||
|
///
|
||||||
|
/// Return the location of the end of the last a allocated section in memory
|
||||||
|
fn load_into_machine(&self, machine: &mut Machine, start_index: usize) -> Result<u64, LoaderError> {
|
||||||
|
let mut end_index = 0;
|
||||||
|
for i in 0..self.sections.len() {
|
||||||
|
let section = &self.sections[i];
|
||||||
|
if section.does_flag_contains_key(FlagValue::ShfAlloc) {
|
||||||
|
end_index = section.virt_addr + section.section_size;
|
||||||
|
// Can allocate to machine memory
|
||||||
|
for j in (0..section.section_size as usize).step_by(4) {
|
||||||
|
let mut buf: [u8; 4] = [0; 4];
|
||||||
|
#[allow(clippy::needless_range_loop)]
|
||||||
|
for k in 0..buf.len() {
|
||||||
|
buf[k] = self.bytes.get(section.image_offset as usize + j + k).copied().ok_or(LoaderError::ParsingError)?;
|
||||||
|
}
|
||||||
|
machine.write_memory(4, start_index + section.virt_addr as usize + j, u32::from_le_bytes(buf) as u64);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(start_index as u64 + end_index + 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Load the binary file and store it inside an array and try to parse it,
|
||||||
|
/// useful for a lot of thing like to know which sections to allocate memory and where
|
||||||
|
fn load_and_parse(path: &str) -> Result<Self, LoaderError> {
|
||||||
|
let file = fs::File::open(path);
|
||||||
|
match file {
|
||||||
|
Ok(mut file) => {
|
||||||
|
let mut instructions: Vec<u8> = Default::default();
|
||||||
|
loop {
|
||||||
|
let mut buf: [u8; 1] = [0; 1];
|
||||||
|
let res = file.read(&mut buf);
|
||||||
|
match res {
|
||||||
|
Ok(res) => {
|
||||||
|
if res == 0 {
|
||||||
|
break; // eof
|
||||||
|
} else {
|
||||||
|
instructions.push(buf[0]);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
return Err(LoaderError::IOError(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
let elf_header = match ElfHeader::try_from(&instructions) {
|
||||||
|
Ok(header) => {
|
||||||
|
header
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
return Err(LoaderError::ParsingError);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let section_header = match Self::parse_section_header(&instructions, elf_header.is_32bits, elf_header.section_header_location, elf_header.section_header_entries, elf_header.section_header_size) {
|
||||||
|
Ok(header) => {
|
||||||
|
header
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
return Err(LoaderError::ParsingError);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// #[cfg(debug_assertions)]
|
||||||
|
// println!("{:04x?}", instructions); // only print loaded program in debug build
|
||||||
|
Ok(Self { bytes: instructions, elf_header, sections: section_header })
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
Err(LoaderError::IOError(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Try to parse sections header table
|
||||||
|
///
|
||||||
|
/// Create one instance of [`SectionHeader`] for each entry and store it inside an array
|
||||||
|
///
|
||||||
|
/// ## Parameters
|
||||||
|
///
|
||||||
|
/// **instructions**: array of bytes of the binary file
|
||||||
|
/// **is_32bits**: contain whether the binary file is 32 bits or 64 bits
|
||||||
|
/// **header_location**: represent the position of the first entry of the header
|
||||||
|
/// **num_of_entries**: defines the number of section header entries
|
||||||
|
/// **entry_size**: Defines the size of an entry (each entry have the exact same size), value vary depending of if this binary file is 32 or 64 bits
|
||||||
|
fn parse_section_header(instructions: &[u8], is_32bits: bool, header_location: u64, num_of_entries: u16, entry_size: u16) -> Result<Vec<SectionHeader>, ()> {
|
||||||
|
let mut sections: Vec<SectionHeader> = Default::default();
|
||||||
|
for i in 0..num_of_entries as u64 {
|
||||||
|
sections.push(Self::parse_section_entry(instructions, is_32bits, header_location + i * entry_size as u64)?);
|
||||||
|
}
|
||||||
|
Ok(sections)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Parse one entry of the section header
|
||||||
|
///
|
||||||
|
/// ## Parameters:
|
||||||
|
///
|
||||||
|
/// **instructions**: array of bytes of the binary file
|
||||||
|
/// **is_32bits**: contain whether the binary file is 32 bits or 64 bits
|
||||||
|
/// **location**: represent the position of the entry on the file image
|
||||||
|
fn parse_section_entry(instructions: &[u8], is_32bits: bool, location: u64) -> Result<SectionHeader, ()> {
|
||||||
|
SectionHeader::try_from((instructions, location, is_32bits))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// return the memory address of something stored at address
|
||||||
|
/// Can return None if the file is smaller than adress + 3 (or 7 if 64 bits), in this case, the elf header is incorrect
|
||||||
|
fn get_address_point(instructions: &[u8], address: usize, is_32bits: bool) -> Option<u64> {
|
||||||
|
if is_32bits {
|
||||||
|
let mut bytes: [u8; 4] = [0; 4];
|
||||||
|
bytes[0] = instructions.get(address).copied()?;
|
||||||
|
bytes[1] = instructions.get(address + 1).copied()?;
|
||||||
|
bytes[2] = instructions.get(address + 2).copied()?;
|
||||||
|
bytes[3] = instructions.get(address + 3).copied()?;
|
||||||
|
Option::Some(u32::from_le_bytes(bytes) as u64)
|
||||||
|
} else {
|
||||||
|
let mut bytes: [u8; 8] = [0; 8];
|
||||||
|
bytes[0] = instructions.get(address).copied()?;
|
||||||
|
bytes[1] = instructions.get(address + 1).copied()?;
|
||||||
|
bytes[2] = instructions.get(address + 2).copied()?;
|
||||||
|
bytes[3] = instructions.get(address + 3).copied()?;
|
||||||
|
bytes[4] = instructions.get(address + 4).copied()?;
|
||||||
|
bytes[5] = instructions.get(address + 5).copied()?;
|
||||||
|
bytes[6] = instructions.get(address + 6).copied()?;
|
||||||
|
bytes[7] = instructions.get(address + 7).copied()?;
|
||||||
|
Option::Some(u64::from_le_bytes(bytes))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tests has been made for C program compiled with RISC-V GCC 12.2.0, target: riscv64-unknown-elf
|
||||||
|
///
|
||||||
|
/// It may not pass in the future if future gcc version modify order of the binary or something else
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use crate::simulator::{loader::{Loader, SectionHeader}, machine::Machine};
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_elf() {
|
||||||
|
let mut machine = Machine::init_machine();
|
||||||
|
let loader = Loader::load_and_parse("./test/riscv_instructions/simple_arithmetics/unsigned_addition").expect("IO Error");
|
||||||
|
loader.load_into_machine(&mut machine, 0).expect("Parsing error");
|
||||||
|
assert!(!loader.elf_header.is_32bits);
|
||||||
|
assert!(!loader.elf_header.endianess);
|
||||||
|
assert!(loader.elf_header.sys_v_abi);
|
||||||
|
assert!(loader.elf_header.is_riscv_target);
|
||||||
|
assert_eq!(1, loader.elf_header.version);
|
||||||
|
assert_eq!(0x4000, loader.elf_header.entrypoint);
|
||||||
|
assert_eq!(64, loader.elf_header.elf_header_size);
|
||||||
|
assert_eq!(64, loader.elf_header.program_header_location);
|
||||||
|
assert_eq!(18984, loader.elf_header.section_header_location);
|
||||||
|
assert_eq!(56, loader.elf_header.program_header_size);
|
||||||
|
assert_eq!(64, loader.elf_header.section_header_size);
|
||||||
|
assert_eq!(4, loader.elf_header.program_header_entries);
|
||||||
|
assert_eq!(9, loader.elf_header.section_header_entries);
|
||||||
|
println!("{:#x?}", loader.sections);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_section() {
|
||||||
|
let mut machine = Machine::init_machine();
|
||||||
|
let loader = Loader::load_and_parse("./test/riscv_instructions/simple_arithmetics/unsigned_addition").expect("IO Error");
|
||||||
|
loader.load_into_machine(&mut machine, 0).expect("Parsing error");
|
||||||
|
assert_eq!(9, loader.sections.len());
|
||||||
|
let n = loader.sections.iter().filter(|p| { p.does_flag_contains_key(crate::simulator::loader::FlagValue::ShfAlloc)}).collect::<Vec<&SectionHeader>>().len();
|
||||||
|
assert_eq!(3, n);
|
||||||
|
assert_eq!(loader.sections[1].virt_addr, 0x4000);
|
||||||
|
assert_eq!(loader.sections[1].image_offset, 0x1000);
|
||||||
|
assert!(loader.sections[1].does_flag_contains_key(crate::simulator::loader::FlagValue::ShfAlloc));
|
||||||
|
assert_eq!(loader.sections[2].virt_addr, 0x400_000);
|
||||||
|
assert_eq!(loader.sections[2].image_offset, 0x2000);
|
||||||
|
assert!(loader.sections[2].does_flag_contains_key(crate::simulator::loader::FlagValue::ShfAlloc));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -15,13 +15,13 @@ use std::{
|
|||||||
io::Write,
|
io::Write,
|
||||||
fs::File
|
fs::File
|
||||||
};
|
};
|
||||||
use crate::simulator::{
|
use crate::{simulator::{
|
||||||
error::MachineError,
|
error::MachineError,
|
||||||
instruction::{*, self},
|
instruction::{*, self},
|
||||||
interrupt::Interrupt,
|
interrupt::Interrupt,
|
||||||
global::*,
|
global::*,
|
||||||
register::*
|
register::*
|
||||||
};
|
}, kernel::system::System};
|
||||||
|
|
||||||
use crate::kernel::{
|
use crate::kernel::{
|
||||||
exception
|
exception
|
||||||
@ -34,6 +34,7 @@ use super::error::MachineOk;
|
|||||||
/// Textual names of the exceptions that can be generated by user program
|
/// Textual names of the exceptions that can be generated by user program
|
||||||
/// execution, for debugging purpose.
|
/// execution, for debugging purpose.
|
||||||
/// todo: is this really supposed to stand in machine.rs?
|
/// todo: is this really supposed to stand in machine.rs?
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum ExceptionType {
|
pub enum ExceptionType {
|
||||||
/// Everything ok
|
/// Everything ok
|
||||||
NoException,
|
NoException,
|
||||||
@ -75,7 +76,7 @@ pub const NUM_PHY_PAGE : u64 = 400;
|
|||||||
/// Must be 2^x
|
/// Must be 2^x
|
||||||
pub const PAGE_SIZE : u64 = 128;
|
pub const PAGE_SIZE : u64 = 128;
|
||||||
/// Must be a multiple of PAGE_SIZE
|
/// Must be a multiple of PAGE_SIZE
|
||||||
pub const MEM_SIZE : usize = (PAGE_SIZE*NUM_PHY_PAGE*100) as usize;
|
pub const MEM_SIZE : usize = (PAGE_SIZE*NUM_PHY_PAGE*100_000) as usize;
|
||||||
|
|
||||||
/// RISC-V Simulator
|
/// RISC-V Simulator
|
||||||
pub struct Machine {
|
pub struct Machine {
|
||||||
@ -177,7 +178,7 @@ impl Machine {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
///
|
///
|
||||||
/// - **machine** contains the memory
|
/// - **machine** contains the memory
|
||||||
pub fn _extract_memory(&self){
|
pub fn _extract_memory(&mut self){
|
||||||
let file_path = "burritos_memory.txt";
|
let file_path = "burritos_memory.txt";
|
||||||
let write_to_file = |path| -> std::io::Result<File> {
|
let write_to_file = |path| -> std::io::Result<File> {
|
||||||
let mut file = File::create(path)?;
|
let mut file = File::create(path)?;
|
||||||
@ -207,9 +208,9 @@ impl Machine {
|
|||||||
}
|
}
|
||||||
println!("________________SP________________");
|
println!("________________SP________________");
|
||||||
let sp_index = self.int_reg.get_reg(2);
|
let sp_index = self.int_reg.get_reg(2);
|
||||||
for i in 0..5 {
|
/* for i in 0..5 {
|
||||||
println!("SP+{:<2} : {:16x}", i*8, self.read_memory(8, (sp_index + i*8) as usize));
|
println!("SP+{:<2} : {:16x}", i*8, self.read_memory(8, (sp_index + i*8) as usize));
|
||||||
}
|
} */
|
||||||
println!("##################################");
|
println!("##################################");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -226,16 +227,15 @@ impl Machine {
|
|||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn raise_exception(&mut self, exception: ExceptionType, address : u64) -> Result<MachineOk, MachineError>{
|
pub fn raise_exception(&mut self, exception: ExceptionType, address : u64, system: &mut System) -> Result<MachineOk, MachineError>{
|
||||||
|
|
||||||
self.set_status(MachineStatus::SystemMode);
|
self.set_status(MachineStatus::SystemMode);
|
||||||
// Handle the interruption
|
// Handle the interruption
|
||||||
match exception::call(exception, self) {
|
match exception::call(&exception, self, system) {
|
||||||
Ok(MachineOk::Shutdown) => {
|
Ok(MachineOk::Shutdown) => {
|
||||||
self.set_status(MachineStatus::UserMode);
|
self.set_status(MachineStatus::UserMode);
|
||||||
return Ok(MachineOk::Shutdown);
|
return Ok(MachineOk::Shutdown);
|
||||||
}
|
}
|
||||||
_ => ()
|
_ => Err(format!("Syscall {:?} invalid or not implemented", exception))?
|
||||||
} // todo: return error if the syscall code is invalid
|
} // todo: return error if the syscall code is invalid
|
||||||
self.set_status(MachineStatus::UserMode);
|
self.set_status(MachineStatus::UserMode);
|
||||||
Ok(MachineOk::Ok)
|
Ok(MachineOk::Ok)
|
||||||
@ -246,13 +246,14 @@ impl Machine {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
///
|
///
|
||||||
/// - **machine** which contains a table of instructions
|
/// - **machine** which contains a table of instructions
|
||||||
pub fn run(&mut self) {
|
pub fn run(&mut self, system: &mut System) {
|
||||||
loop {
|
loop {
|
||||||
match self.one_instruction() {
|
match self.one_instruction(system) {
|
||||||
Ok(MachineOk::Ok) => println!("hello"),
|
Ok(MachineOk::Ok) => println!("hello"),
|
||||||
Ok(MachineOk::Shutdown) => break,
|
Ok(MachineOk::Shutdown) => break,
|
||||||
Err(e) => { if e.to_string().contains("System") { break; } panic!("FATAL at pc {} -> {}", self.pc, e) }
|
Err(e) => panic!("FATAL at pc {} -> {}", self.pc, e)
|
||||||
}
|
}
|
||||||
|
self.write_int_register(0, 0); // In case an instruction write on register 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +262,7 @@ impl Machine {
|
|||||||
/// ### Parameters
|
/// ### Parameters
|
||||||
///
|
///
|
||||||
/// - **machine** which contains a table of instructions and a pc to the actual instruction
|
/// - **machine** which contains a table of instructions and a pc to the actual instruction
|
||||||
pub fn one_instruction(&mut self) -> Result<MachineOk, MachineError> {
|
pub fn one_instruction(&mut self, system: &mut System) -> Result<MachineOk, MachineError> {
|
||||||
|
|
||||||
if self.main_memory.len() <= self.pc as usize {
|
if self.main_memory.len() <= self.pc as usize {
|
||||||
panic!("ERROR : number max of instructions rushed");
|
panic!("ERROR : number max of instructions rushed");
|
||||||
@ -334,7 +335,7 @@ impl Machine {
|
|||||||
RISCV_FP => self.fp_instruction(inst),
|
RISCV_FP => self.fp_instruction(inst),
|
||||||
|
|
||||||
// Treatment for: SYSTEM CALLS
|
// Treatment for: SYSTEM CALLS
|
||||||
RISCV_SYSTEM => self.raise_exception(ExceptionType::SyscallException, self.pc),
|
RISCV_SYSTEM => self.raise_exception(ExceptionType::SyscallException, self.pc, system),
|
||||||
|
|
||||||
// Default case
|
// Default case
|
||||||
_ => Err(format!("{:x}: Unknown opcode\npc: {:x}", inst.opcode, self.pc))?
|
_ => Err(format!("{:x}: Unknown opcode\npc: {:x}", inst.opcode, self.pc))?
|
||||||
@ -709,7 +710,8 @@ mod test {
|
|||||||
let memory_before = mem_cmp::MemChecker::from(get_full_path!("memory", $a)).unwrap();
|
let memory_before = mem_cmp::MemChecker::from(get_full_path!("memory", $a)).unwrap();
|
||||||
let memory_after = mem_cmp::MemChecker::from(get_full_path!("memory", &end_file_name)).unwrap();
|
let memory_after = mem_cmp::MemChecker::from(get_full_path!("memory", &end_file_name)).unwrap();
|
||||||
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
mem_cmp::MemChecker::fill_memory_from_mem_checker(&memory_before, &mut m);
|
||||||
m.run();
|
let mut system = crate::kernel::system::System::default();
|
||||||
|
m.run(&mut system);
|
||||||
let expected_trace = fs::read_to_string(get_full_path!("reg_trace", $a)).unwrap();
|
let expected_trace = fs::read_to_string(get_full_path!("reg_trace", $a)).unwrap();
|
||||||
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
assert!(mem_cmp::MemChecker::compare_machine_memory(&memory_after, &m));
|
||||||
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
assert!(expected_trace.contains(m.registers_trace.as_str()));
|
||||||
|
@ -113,8 +113,12 @@ impl<T: PartialEq> List<T> {
|
|||||||
let mut current: *mut Node<T> = self.head;
|
let mut current: *mut Node<T> = self.head;
|
||||||
let mut previous: *mut Node<T> = ptr::null_mut();
|
let mut previous: *mut Node<T> = ptr::null_mut();
|
||||||
while !current.is_null() {
|
while !current.is_null() {
|
||||||
if (*current).elem == item {
|
if (&*current).elem == item {
|
||||||
(*previous).next = (*current).next;
|
if !previous.is_null() {
|
||||||
|
(*previous).next = (*current).next;
|
||||||
|
} else {
|
||||||
|
self.head = (*current).next;
|
||||||
|
}
|
||||||
drop(Box::from_raw(current).elem);
|
drop(Box::from_raw(current).elem);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
@ -320,6 +324,22 @@ mod test {
|
|||||||
assert_eq!(list.peek(), Option::None);
|
assert_eq!(list.peek(), Option::None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn remove_test2() {
|
||||||
|
let mut list = List::default();
|
||||||
|
assert_eq!(list.peek(), None);
|
||||||
|
list.push(1);
|
||||||
|
list.push(2);
|
||||||
|
list.push(3);
|
||||||
|
|
||||||
|
assert_eq!(list.contains(&1), true);
|
||||||
|
list.remove(1);
|
||||||
|
assert_eq!(list.contains(&1), false);
|
||||||
|
assert_eq!(list.pop(), Option::Some(2));
|
||||||
|
assert_eq!(list.pop(), Option::Some(3));
|
||||||
|
assert_eq!(list.peek(), Option::None);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn miri_test() {
|
fn miri_test() {
|
||||||
let mut list = List::default();
|
let mut list = List::default();
|
||||||
|
@ -1,21 +0,0 @@
|
|||||||
TOPDIR=.
|
|
||||||
include $(TOPDIR)/Makefile.config
|
|
||||||
|
|
||||||
#
|
|
||||||
# Main targets
|
|
||||||
#
|
|
||||||
dumps:
|
|
||||||
$(MAKE) dumps -C riscv_instructions/
|
|
||||||
mkdir -p ${TOPDIR}/target/dumps/
|
|
||||||
find . -name '*.dump' -exec mv {} ${TOPDIR}/target/dumps/ \;
|
|
||||||
|
|
||||||
user_lib:
|
|
||||||
$(MAKE) -C userlib/
|
|
||||||
|
|
||||||
tests: user_lib
|
|
||||||
$(MAKE) tests -C riscv_instructions/
|
|
||||||
mkdir -p ${TOPDIR}/target/guac/
|
|
||||||
find . -name '*.guac' -exec mv {} ${TOPDIR}/target/guac/ \;
|
|
||||||
|
|
||||||
clean:
|
|
||||||
rm -rf $(TOPDIR)/target
|
|
6
test/riscv_instructions/.gitignore
vendored
Normal file
6
test/riscv_instructions/.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
*
|
||||||
|
!.gitignore
|
||||||
|
!*.c
|
||||||
|
!*/
|
||||||
|
!*.md
|
||||||
|
!**/Makefile
|
@ -1,3 +1,8 @@
|
|||||||
|
build:
|
||||||
|
make build -C boolean_logic/
|
||||||
|
make build -C jump_instructions/
|
||||||
|
make build -C simple_arithmetics/
|
||||||
|
|
||||||
dumps:
|
dumps:
|
||||||
make dumps -C boolean_logic/
|
make dumps -C boolean_logic/
|
||||||
make dumps -C jump_instructions/
|
make dumps -C jump_instructions/
|
||||||
@ -6,4 +11,9 @@ dumps:
|
|||||||
tests:
|
tests:
|
||||||
make tests -C boolean_logic/
|
make tests -C boolean_logic/
|
||||||
make tests -C jump_instructions/
|
make tests -C jump_instructions/
|
||||||
make tests -C simple_arithmetics/
|
make tests -C simple_arithmetics/
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(MAKE) clean -C boolean_logic/
|
||||||
|
$(MAKE) clean -C jump_instructions/
|
||||||
|
$(MAKE) clean -C simple_arithmetics/
|
@ -1,9 +1,17 @@
|
|||||||
TOPDIR = ../..
|
|
||||||
include $(TOPDIR)/Makefile.tests
|
PROGRAMS = comparisons if switch
|
||||||
|
|
||||||
|
build: $(PROGRAMS)
|
||||||
|
|
||||||
dumps: comparisons.dump if.dump switch.dump
|
dumps: comparisons.dump if.dump switch.dump
|
||||||
|
|
||||||
tests: comparisons.guac if.guac switch.guac
|
tests: comparisons.guac if.guac switch.guac
|
||||||
|
|
||||||
|
TOPDIR = ../../..
|
||||||
|
include $(TOPDIR)/Makefile.rules
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) comparisons comparisons.o if if.o
|
||||||
|
|
||||||
# Dependances
|
# Dependances
|
||||||
$(PROGRAMS): % : $(USERLIB)/sys.o $(USERLIB)/libnachos.o %.o
|
$(PROGRAMS): % : $(USERLIB)/sys.o $(USERLIB)/libnachos.o %.o
|
@ -1,6 +1,15 @@
|
|||||||
TOPDIR = ../..
|
PROGRAMS = jump ret
|
||||||
include $(TOPDIR)/Makefile.tests
|
|
||||||
|
build: $(PROGRAMS)
|
||||||
|
|
||||||
dumps: jump.dump ret.dump
|
dumps: jump.dump ret.dump
|
||||||
|
|
||||||
tests: jump.guac ret.guac
|
tests: jump.guac ret.guac
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) jump jump.o ret ret.o
|
||||||
|
|
||||||
|
TOPDIR = ../../..
|
||||||
|
include $(TOPDIR)/Makefile.rules
|
||||||
|
|
||||||
|
$(PROGRAMS): % : $(USERLIB)/sys.o $(USERLIB)/libnachos.o %.o
|
@ -1,6 +1,16 @@
|
|||||||
TOPDIR = ../..
|
|
||||||
include $(TOPDIR)/Makefile.tests
|
PROGRAMS = unsigned_addition unsigned_division unsigned_multiplication unsigned_substraction
|
||||||
|
|
||||||
|
build: $(PROGRAMS)
|
||||||
|
|
||||||
dumps: unsigned_addition.dump unsigned_division.dump unsigned_multiplication.dump unsigned_substraction.dump
|
dumps: unsigned_addition.dump unsigned_division.dump unsigned_multiplication.dump unsigned_substraction.dump
|
||||||
|
|
||||||
tests: unsigned_addition.guac unsigned_division.guac unsigned_multiplication.guac unsigned_substraction.guac
|
tests: unsigned_addition.guac unsigned_division.guac unsigned_multiplication.guac unsigned_substraction.guac
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) unsigned_addition unsigned_addition.o unsigned_division unsigned_division.o unsigned_multiplication unsigned_multiplication.o unsigned_substraction unsigned_substraction.o
|
||||||
|
|
||||||
|
TOPDIR = ../../..
|
||||||
|
include $(TOPDIR)/Makefile.rules
|
||||||
|
|
||||||
|
$(PROGRAMS): % : $(USERLIB)/sys.o $(USERLIB)/libnachos.o %.o
|
16
test/syscall_tests/Makefile
Normal file
16
test/syscall_tests/Makefile
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
PROGRAMS = halt.guac prints.guac
|
||||||
|
|
||||||
|
build: $(PROGRAMS)
|
||||||
|
|
||||||
|
dumps: halt.dump prints.dump
|
||||||
|
|
||||||
|
tests: halt.guac prints.guac
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) halt.o prints.o
|
||||||
|
|
||||||
|
TOPDIR = ../..
|
||||||
|
include $(TOPDIR)/Makefile.rules
|
||||||
|
|
||||||
|
$(PROGRAMS): %.guac : $(USERLIB)/sys.o $(USERLIB)/libnachos.o %.o
|
BIN
test/syscall_tests/halt
Executable file
BIN
test/syscall_tests/halt
Executable file
Binary file not shown.
7
test/syscall_tests/halt.c
Normal file
7
test/syscall_tests/halt.c
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
|
||||||
|
#include "userlib/syscall.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Shutdown();
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
test/syscall_tests/prints
Executable file
BIN
test/syscall_tests/prints
Executable file
Binary file not shown.
10
test/syscall_tests/prints.c
Normal file
10
test/syscall_tests/prints.c
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "userlib/syscall.h"
|
||||||
|
#include "userlib/libnachos.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
n_printf("Hello World 1");
|
||||||
|
n_printf("Hello World 2");
|
||||||
|
n_printf("Hello World 3");
|
||||||
|
n_printf("Hello World 4");
|
||||||
|
return 0;
|
||||||
|
}
|
@ -1,4 +0,0 @@
|
|||||||
TOPDIR = ../
|
|
||||||
include $(TOPDIR)/Makefile.tests
|
|
||||||
|
|
||||||
default: sys.o libnachos.o
|
|
@ -1,21 +0,0 @@
|
|||||||
pub struct Burritos_Time {
|
|
||||||
seconds: i64,
|
|
||||||
nanos: i64
|
|
||||||
}
|
|
||||||
pub struct ThreadId{
|
|
||||||
id: u64
|
|
||||||
}
|
|
||||||
pub struct t_error{
|
|
||||||
t: i32
|
|
||||||
}
|
|
||||||
extern "C"{
|
|
||||||
fn Shutdown() -> ();
|
|
||||||
fn SysTime(t: Burritos_Time) -> ();
|
|
||||||
fn Exit(status: i32) -> ();
|
|
||||||
fn Exec(name: String) -> ThreadId;
|
|
||||||
fn newThread(debug_name: String, func: i32, arg: i32) -> ThreadId;
|
|
||||||
fn Join (id: ThreadId) -> t_error;
|
|
||||||
fn Yield() -> ();
|
|
||||||
fn Perror(mess: String) -> ();
|
|
||||||
|
|
||||||
}
|
|
7
userlib/Makefile
Normal file
7
userlib/Makefile
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
TOPDIR = ../
|
||||||
|
include $(TOPDIR)/Makefile.rules
|
||||||
|
|
||||||
|
default: sys.o libnachos.o
|
||||||
|
|
||||||
|
clean:
|
||||||
|
$(RM) libnachos.o sys.o
|
@ -63,8 +63,8 @@ __start:
|
|||||||
* -------------------------------------------------------------
|
* -------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.globl Halt
|
.globl Shutdown
|
||||||
.type __Halt, @function
|
.type __Shutdown, @function
|
||||||
Shutdown:
|
Shutdown:
|
||||||
addi a7,zero,SC_HALT
|
addi a7,zero,SC_HALT
|
||||||
ecall
|
ecall
|
@ -84,7 +84,7 @@
|
|||||||
typedef int t_error;
|
typedef int t_error;
|
||||||
|
|
||||||
/* Stop Nachos, and print out performance stats */
|
/* Stop Nachos, and print out performance stats */
|
||||||
void Halt();
|
void Shutdown();
|
||||||
|
|
||||||
|
|
||||||
/* Return the time spent running Nachos */
|
/* Return the time spent running Nachos */
|
183
userlib/syscall.rs
Normal file
183
userlib/syscall.rs
Normal file
@ -0,0 +1,183 @@
|
|||||||
|
use std::str::Chars;
|
||||||
|
|
||||||
|
/// Define the BurritOS running time basic unit
|
||||||
|
pub struct BurritosTime {
|
||||||
|
seconds: i64,
|
||||||
|
nanos: i64
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A unique identifier for a thread executed within a user program
|
||||||
|
pub struct ThreadId{
|
||||||
|
id: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The system call interface. These are the operations the BurritOS
|
||||||
|
/// kernel needs to support, to be able to run user programs.
|
||||||
|
pub struct TError {
|
||||||
|
t: i32
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A unique identifier for an open BurritOS file.
|
||||||
|
pub struct OpenFiledId{
|
||||||
|
id: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
/// System calls concerning semaphores management
|
||||||
|
pub struct SemId{
|
||||||
|
id: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
/// System calls concerning locks management
|
||||||
|
pub struct LockId{
|
||||||
|
id: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
/// System calls concerning conditions variables.
|
||||||
|
pub struct CondId{
|
||||||
|
id: u64
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
///Stop BurritOS, and print out performance stats
|
||||||
|
fn Shutdown() -> ();
|
||||||
|
|
||||||
|
/// Return the time spent running BurritOS
|
||||||
|
/// ## Param
|
||||||
|
/// - **t** a struct to define the time unit
|
||||||
|
fn SysTime(t: BurritosTime) -> ();
|
||||||
|
|
||||||
|
/// This user program is done
|
||||||
|
/// ## Param
|
||||||
|
/// - **status** status at the end of execution *(status = 0 means exited normally)*.
|
||||||
|
fn Exit(status: i32) -> ();
|
||||||
|
|
||||||
|
/// Run the executable, stored in the BurritOS file "name", and return the
|
||||||
|
/// master thread identifier
|
||||||
|
fn Exec(name: *const char) -> ThreadId;
|
||||||
|
|
||||||
|
/// Create a new thread in the current process
|
||||||
|
/// Return thread identifier
|
||||||
|
fn newThread(debug_name: *const char, func: i32, arg: i32) -> ThreadId;
|
||||||
|
|
||||||
|
/// Only return once the the thread "id" has finished.
|
||||||
|
fn Join (id: ThreadId) -> TError;
|
||||||
|
|
||||||
|
/// Yield the CPU to another runnable thread, whether in this address space
|
||||||
|
/// or not.
|
||||||
|
fn Yield() -> ();
|
||||||
|
|
||||||
|
/// Print the last error message with the personalized one "mess"
|
||||||
|
fn PError(mess: *const char) -> ();
|
||||||
|
|
||||||
|
/// Create a BurritOS file, with "name"
|
||||||
|
fn Create(name: *const char, size: i32) -> TError;
|
||||||
|
|
||||||
|
/// Open the Nachos file "name", and return an "OpenFileId" that can
|
||||||
|
/// be used to read and write to the file.
|
||||||
|
fn Open(name: *const char) -> OpenFiledId;
|
||||||
|
|
||||||
|
/// Write "size" bytes from "buffer" to the open file.
|
||||||
|
fn Write(buffer: *const char, size: i32, id: OpenFiledId) -> TError;
|
||||||
|
|
||||||
|
/// Read "size" bytes from the open file into "buffer".
|
||||||
|
/// Return the number of bytes actually read -- if the open file isn't
|
||||||
|
/// long enough, or if it is an I/O device, and there aren't enough
|
||||||
|
/// characters to read, return whatever is available (for I/O devices,
|
||||||
|
/// you should always wait until you can return at least one character).
|
||||||
|
fn Read(buffer: *const char, size: i32, id:OpenFiledId) -> TError;
|
||||||
|
|
||||||
|
/// Seek to a specified offset into an opened file
|
||||||
|
fn Seek(offset: i32, id: OpenFiledId) -> TError;
|
||||||
|
|
||||||
|
/// Close the file, we're done reading and writing to it.
|
||||||
|
fn Close(id: OpenFiledId) -> TError;
|
||||||
|
|
||||||
|
/// Remove the file
|
||||||
|
fn Remove(name: *const char) -> TError;
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////
|
||||||
|
/// system calls concerning directory management ///
|
||||||
|
////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
/// Create a new repertory
|
||||||
|
/// Return a negative number if an error ocurred.
|
||||||
|
fn mkdir(name: *const char) -> t_length;
|
||||||
|
|
||||||
|
/// Destroy a repertory, which must be empty.
|
||||||
|
/// Return a negative number if an error ocurred.
|
||||||
|
fn Rmdir(name: *const char) -> TError;
|
||||||
|
|
||||||
|
/// List the content of BurritOS FileSystem
|
||||||
|
fn FSList() -> TError;
|
||||||
|
|
||||||
|
/// Create a semaphore, initialising it at count.
|
||||||
|
/// Return a Semid, which will enable to do operations on this
|
||||||
|
/// semaphore
|
||||||
|
fn SemCreate(debug_name: *const char, count: i32) -> SemId;
|
||||||
|
|
||||||
|
/// Destroy a semaphore identified by sema.
|
||||||
|
/// Return a negative number if an error occured during the destruction
|
||||||
|
fn SemDestroy(sema: SemId) -> TError;
|
||||||
|
|
||||||
|
/// Do the operation P() on the semaphore sema
|
||||||
|
fn P(sema: SemId) -> TError;
|
||||||
|
|
||||||
|
/// Do the operation V() on the semaphore sema
|
||||||
|
fn V(sema: SemId) -> TError;
|
||||||
|
|
||||||
|
/// Create a lock.
|
||||||
|
/// Return an identifier
|
||||||
|
fn LockCreate(debug_name: *const char) -> LockId;
|
||||||
|
|
||||||
|
/// Destroy a lock.
|
||||||
|
/// Return a negative number if an error ocurred
|
||||||
|
/// during the destruction.
|
||||||
|
fn LockDestroy(id: LockId) -> TError;
|
||||||
|
|
||||||
|
/// Do the operation Acquire on the lock id.
|
||||||
|
/// Return a negative number if an error ocurred.
|
||||||
|
fn LockAcquire(id: LockId) -> TError;
|
||||||
|
|
||||||
|
/// Do the operation Release on the lock id.
|
||||||
|
/// Return a negative number if an error ocurred.
|
||||||
|
fn LockRelease(id: LockId) -> TError;
|
||||||
|
|
||||||
|
/// Create a new condition variable
|
||||||
|
fn CondCreate(debug_name: *const char) -> CondId;
|
||||||
|
|
||||||
|
/// Destroy a condition variable.
|
||||||
|
/// Return a negative number if an error ocurred.
|
||||||
|
fn CondDestroy(id: CondId) -> TError;
|
||||||
|
|
||||||
|
/// Do the operation Wait on a condition variable.
|
||||||
|
/// Returns a negative number if an error ocurred.
|
||||||
|
fn CondWait(id: CondId) -> TError;
|
||||||
|
|
||||||
|
/// Do the operation Signal on a condition variable (wake up only one thread).
|
||||||
|
/// Return a negative number if an error ocurred.
|
||||||
|
fn CondSignal(id: CondId) -> TError;
|
||||||
|
|
||||||
|
/// Do the operation Signal on a condition variable (wake up all threads).
|
||||||
|
/// Return a negative number if an error ocurred.
|
||||||
|
fn CondBroadcast(id: CondId) -> TError;
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////
|
||||||
|
/// # System calls concerning serial port and console
|
||||||
|
///////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
///Send the message on the serial communication link.
|
||||||
|
/// Returns the number of bytes successfully sent.
|
||||||
|
fn TtySend(mess: *const char) -> i32;
|
||||||
|
|
||||||
|
/// Wait for a message comming from the serial communication link.
|
||||||
|
/// The length of the buffer where the bytes will be copied is given as a parameter.
|
||||||
|
/// Returns the number of characters actually received.
|
||||||
|
fn TtyReceive(mess: *const char, length: i32) -> i32;
|
||||||
|
|
||||||
|
/// Map an opened file in memory. Size is the size to be mapped in bytes.
|
||||||
|
fn Mmap(id: OpenFiledId, size: i32) -> *mut ();
|
||||||
|
|
||||||
|
/// For debug purpose
|
||||||
|
fn Debug(param: i32)-> ();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user