From 73ac8d3083aab478233a745fd79dffb669c1772a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Autin?= Date: Wed, 19 Apr 2023 18:09:08 +0200 Subject: [PATCH] BurritOS now read configuration file --- src/kernel/exception.rs | 5 +++-- src/kernel/synch.rs | 6 +++--- src/kernel/thread_manager.rs | 10 +++++----- src/main.rs | 3 ++- src/simulator/loader.rs | 6 +++--- src/simulator/machine.rs | 33 +++++++++++++++++---------------- src/simulator/mem_cmp.rs | 4 +++- src/simulator/mmu.rs | 21 ++++++++++++--------- src/utility/cfg.rs | 11 +++++++++-- 9 files changed, 57 insertions(+), 42 deletions(-) diff --git a/src/kernel/exception.rs b/src/kernel/exception.rs index c260d3c..f90f271 100644 --- a/src/kernel/exception.rs +++ b/src/kernel/exception.rs @@ -344,10 +344,11 @@ mod test { use crate::kernel::exception::{SC_SHUTDOWN, SC_WRITE}; use crate::kernel::system::System; use crate::simulator::machine::Machine; + use crate::utility::cfg::get_debug_configuration; #[test] fn test_sc_shutdown() { - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); machine.write_int_register(17, SC_SHUTDOWN as i64); // Set type to shutdown // let ecall = Instruction::new(0b000000000000_00000_000_00000_1110011); @@ -363,7 +364,7 @@ mod test { #[test] #[ignore] fn test_sc_print() { - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); let _address = machine.read_int_register(10); // Write string 'HELLO' in memory diff --git a/src/kernel/synch.rs b/src/kernel/synch.rs index 807793f..a1472e5 100644 --- a/src/kernel/synch.rs +++ b/src/kernel/synch.rs @@ -227,13 +227,13 @@ impl Condition { mod test { use std::{rc::Rc, cell::RefCell}; - use crate::{kernel::{thread::Thread, synch::Lock, thread_manager::ThreadManager}, simulator::machine::Machine}; + use crate::{kernel::{thread::Thread, synch::Lock, thread_manager::ThreadManager}, simulator::machine::Machine, utility::cfg::get_debug_configuration}; #[test] fn test_lock_simple() { - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); let mut tm = ThreadManager::new(true); let thread = Rc::new(RefCell::new(Thread::new("test_lock"))); tm.ready_to_run(Rc::clone(&thread)); @@ -255,7 +255,7 @@ mod test { let thread1 = Rc::new(RefCell::new(Thread::new("test_lock1"))); let thread2 = Rc::new(RefCell::new(Thread::new("test_lock2"))); - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); let mut tm = ThreadManager::new(true); tm.ready_to_run(Rc::clone(&thread1)); diff --git a/src/kernel/thread_manager.rs b/src/kernel/thread_manager.rs index 2150a0b..d24988a 100644 --- a/src/kernel/thread_manager.rs +++ b/src/kernel/thread_manager.rs @@ -371,12 +371,12 @@ impl ThreadManager { mod test { use std::{rc::Rc, cell::RefCell}; - use crate::{simulator::{machine::Machine, loader}, kernel::{system::System, thread::Thread, process::Process, thread_manager::ThreadManager, synch::Semaphore}}; + use crate::{simulator::{machine::Machine, loader}, kernel::{system::System, thread::Thread, process::Process, thread_manager::ThreadManager, synch::Semaphore}, utility::cfg::get_debug_configuration}; use crate::kernel::synch::Lock; #[test] fn test_thread_context() { - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); let (loader, ptr) = loader::Loader::new("./target/guac/halt.guac", &mut machine, 0).expect("IO Error"); let start_pc = loader.elf_header.entrypoint; @@ -404,7 +404,7 @@ mod test { #[test] fn test_lock(){ - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); let mut thread_manager = ThreadManager::new(true); let lock = Lock::new(); let lock_id = thread_manager.get_obj_addrs().add_lock(lock); @@ -430,7 +430,7 @@ mod test { #[test] fn test_semaphore_single() { // Init - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); let mut thread_manager = ThreadManager::new(true); let semaphore = Semaphore::new(1); let sema_id = thread_manager.get_obj_addrs().add_semaphore(semaphore); @@ -457,7 +457,7 @@ mod test { fn test_semaphore_multiple() { // Init let mut tm = ThreadManager::new(true); - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); let semaphore = Semaphore::new(2); let sema_id = tm.get_obj_addrs().add_semaphore(semaphore); let thread1 = Rc::new(RefCell::new(Thread::new("test_semaphore_1"))); diff --git a/src/main.rs b/src/main.rs index 7dcffb6..61529e7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ use simulator::{machine::Machine, loader}; use clap::Parser; +use utility::cfg::{get_debug_configuration, read_settings}; #[derive(Parser, Debug)] #[command(name = "BurritOS", author, version, about = "Burritos (BurritOS Using Rust Really Improves The Operating System) @@ -39,7 +40,7 @@ struct Args { fn main() { let args = Args::parse(); - let mut machine = Machine::new(args.debug); + let mut machine = Machine::new(args.debug, read_settings().unwrap()); let (loader, ptr) = loader::Loader::new(args.executable.as_str(), &mut machine, 0).expect("An error occured while parsing the program"); let mut system = System::new(args.debug); diff --git a/src/simulator/loader.rs b/src/simulator/loader.rs index 0820a4c..18f56fe 100644 --- a/src/simulator/loader.rs +++ b/src/simulator/loader.rs @@ -584,12 +584,12 @@ fn get_address_point(instructions: &[u8], address: usize, is_32bits: bool) -> Op /// 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}; + use crate::{simulator::{loader::{Loader, SectionHeader}, machine::Machine}, utility::cfg::get_debug_configuration}; #[test] fn test_parse_elf() { - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); let loader = Loader::load_and_parse("./target/guac/unsigned_addition.guac").expect("IO Error"); loader.load_into_machine(&mut machine, 0).expect("Parsing error"); assert!(!loader.elf_header.is_32bits); @@ -610,7 +610,7 @@ mod test { #[test] fn test_parse_section() { - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); let loader = Loader::load_and_parse("./target/guac/unsigned_addition.guac").expect("IO Error"); loader.load_into_machine(&mut machine, 0).expect("Parsing error"); assert_eq!(9, loader.sections.len()); diff --git a/src/simulator/machine.rs b/src/simulator/machine.rs index a58eddd..040da76 100644 --- a/src/simulator/machine.rs +++ b/src/simulator/machine.rs @@ -21,7 +21,7 @@ use crate::{simulator::{ interrupt::Interrupt, global::*, register::* -}, kernel::system::System}; +}, kernel::system::System, utility::cfg::{Settings, MachineSettingKey}}; use crate::kernel::{ exception @@ -71,12 +71,6 @@ pub const STACK_REG: usize = 2; pub const NUM_INT_REGS: usize = 32; /// Number of available Floating Point registers pub const NUM_FP_REGS: usize = 32; -/// max number of physical pages -pub const NUM_PHY_PAGE : u64 = 400; -/// Must be 2^x -pub const PAGE_SIZE : u64 = 128; -/// Must be a multiple of PAGE_SIZE -pub const MEM_SIZE : usize = (PAGE_SIZE*NUM_PHY_PAGE*100_000) as usize; /// RISC-V Simulator pub struct Machine { @@ -100,7 +94,8 @@ pub struct Machine { pub interrupt: Interrupt, // futur taille à calculer int memSize = g_cfg->NumPhysPages * g_cfg->PageSize; //creer une struct cfg(configuration) qui s'initialise avec valeur dans un fichier cfg - + num_phy_page: u64, + page_size: u64, /// Current machine status pub status: MachineStatus } @@ -109,27 +104,32 @@ pub struct Machine { impl Machine { /// Machine constructor - pub fn new(debug: bool) -> Self { + pub fn new(debug: bool, settings: Settings) -> Self { let mut shiftmask : [u64 ; 64] = [0 ; 64]; let mut value : u64 = 0xffffffff; - value = (value << 32) + value; for item in &mut shiftmask { *item = value; value >>= 1; } + let num_phy_page = *settings.get(&MachineSettingKey::NumPhysPages).unwrap(); + let page_size = *settings.get(&MachineSettingKey::PageSize).unwrap(); + let mem_size = (page_size*num_phy_page*100_000) as usize; + Machine { debug, pc : 0, sp: 0, int_reg : { let mut r = Register::::init(); r.set_reg(10, -1); r }, fp_reg : Register::::init(), - main_memory : vec![0_u8; MEM_SIZE], + main_memory : vec![0_u8; mem_size], shiftmask, interrupt: Interrupt::new(), registers_trace : String::from(""), - status: MachineStatus::SystemMode + status: MachineStatus::SystemMode, + num_phy_page, + page_size } } @@ -707,6 +707,7 @@ mod test { use std::fs; use crate::simulator::{machine::Machine, mem_cmp}; + use crate::utility::cfg::get_debug_configuration; macro_rules! get_full_path { ($prefix: expr, $test_name:expr) => {{ @@ -720,7 +721,7 @@ mod test { macro_rules! init_test { ($a:expr) => {{ - let mut m = Machine::new(true); + let mut m = Machine::new(true, get_debug_configuration()); let end_file_name = { let mut s = String::from($a); s.push_str("End"); s }; 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(); @@ -735,12 +736,12 @@ mod test { #[test] fn test_init_machine() { - let _ = Machine::new(true); + let _ = Machine::new(true, get_debug_configuration()); } #[test] fn test_read_memory() { - let mut m = Machine::new(true); + let mut m = Machine::new(true, get_debug_configuration()); m.main_memory[4] = 43; m.main_memory[5] = 150; assert_eq!((43 << 8) + 150, m.read_memory(2, 4)); @@ -748,7 +749,7 @@ mod test { #[test] fn test_write_memory() { - let mut m = Machine::new(true); + let mut m = Machine::new(true, get_debug_configuration()); m.write_memory(2, 6, (43 << 8) + 150); assert_eq!(43, m.main_memory[6]); assert_eq!(150, m.main_memory[7]); diff --git a/src/simulator/mem_cmp.rs b/src/simulator/mem_cmp.rs index abe0e46..07f48b6 100644 --- a/src/simulator/mem_cmp.rs +++ b/src/simulator/mem_cmp.rs @@ -206,12 +206,14 @@ impl MemChecker{ #[cfg(test)] mod tests { + use crate::utility::cfg::get_debug_configuration; + use super::*; #[test] fn test_fill_memory(){ let m_c = MemChecker::from("test/machine/memoryAdd.txt").unwrap(); - let mut machine = Machine::new(true); + let mut machine = Machine::new(true, get_debug_configuration()); MemChecker::fill_memory_from_mem_checker(&m_c, &mut machine); MemChecker::compare_print_m_c_machine(&m_c, &mut machine); } diff --git a/src/simulator/mmu.rs b/src/simulator/mmu.rs index f91dcb0..6436794 100644 --- a/src/simulator/mmu.rs +++ b/src/simulator/mmu.rs @@ -6,15 +6,18 @@ pub struct MMU <'a>{ * Cette table est associée au processus courant * Cette référence peut etre mise a jour par exemple lors d'un switchTo */ - translationTable : Option<&'a mut TranslationTable> + translationTable : Option<&'a mut TranslationTable>, + numPhyPages : u64, + pageSize : u64 } impl <'a>MMU <'_>{ - fn create() -> MMU <'a>{ - - MMU{ - translationTable : None + fn create(numPhyPages: u64, pageSize: u64) -> MMU <'a>{ + MMU { + translationTable : None, + numPhyPages, + pageSize } } @@ -88,8 +91,8 @@ impl <'a>MMU <'_>{ pub fn translate(mmu : &mut MMU, virtAddr : u64, physAddr : &mut u64, writing : bool) -> ExceptionType { - let vpn : u64 = virtAddr/PAGE_SIZE; //virtual page index - let offset : u64 = virtAddr%PAGE_SIZE; //adresse intra page + let vpn : u64 = virtAddr/(mmu.pageSize); //virtual page index + let offset : u64 = virtAddr%(mmu.pageSize); //adresse intra page @@ -133,7 +136,7 @@ impl <'a>MMU <'_>{ } //Make sure that the physical adress is correct - if table_ref.get_physical_page(vpn) < 0 || table_ref.get_physical_page(vpn) >= (NUM_PHY_PAGE as i32) { + if table_ref.get_physical_page(vpn) < 0 || table_ref.get_physical_page(vpn) >= (mmu.numPhyPages as i32) { println!("Error from translate :: no valid correspondance"); return ExceptionType::BusErrorException; } @@ -147,7 +150,7 @@ impl <'a>MMU <'_>{ //on se permet ici la conversion du champs physical_page de i32 vers u64 //si cette valeur avait été signée, cela aurait été detecté juste au dessus, renvoyant une BUSERROR_EXCEPTION - *physAddr = (table_ref.get_physical_page(vpn) as u64)*PAGE_SIZE + offset; + *physAddr = (table_ref.get_physical_page(vpn) as u64)*(mmu.pageSize) + offset; } } diff --git a/src/utility/cfg.rs b/src/utility/cfg.rs index f7ff2e0..68b01fc 100644 --- a/src/utility/cfg.rs +++ b/src/utility/cfg.rs @@ -15,7 +15,7 @@ use std::{ /// Aliases the rather long HashMap type /// to a rather simpler to understand Settings. -pub type Settings = HashMap; +pub type Settings = HashMap; /// Keys for the Settings HashMap, represented as enums for /// maintainability. @@ -92,6 +92,13 @@ pub fn read_settings() -> Result { Ok(settings_map) } +pub fn get_debug_configuration() -> Settings { + let mut settings_map = Settings::new(); + settings_map.insert(MachineSettingKey::PageSize, 128); + settings_map.insert(MachineSettingKey::NumPhysPages, 400); + settings_map +} + fn filter_garbage(reader: BufReader) -> Vec { reader.lines() .map(|l| l.unwrap()) @@ -101,7 +108,7 @@ fn filter_garbage(reader: BufReader) -> Vec { fn update_settings_map(mut settings_map: Settings, key: &str, setting: &str) -> Settings { let key = MachineSettingKey::from(key); - let setting = i32::from_str_radix(setting, 10).unwrap_or(0); + let setting = u64::from_str_radix(setting, 10).unwrap_or(0); settings_map.insert(key, setting); settings_map } \ No newline at end of file