BurritOS now read configuration file

This commit is contained in:
François Autin 2023-04-19 18:09:08 +02:00
parent 1c4c51b0ba
commit 73ac8d3083
9 changed files with 57 additions and 42 deletions

View File

@ -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

View File

@ -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));

View File

@ -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")));

View File

@ -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);

View File

@ -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());

View File

@ -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::<i64>::init(); r.set_reg(10, -1); r },
fp_reg : Register::<f32>::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]);

View File

@ -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);
}

View File

@ -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>{
fn create(numPhyPages: u64, pageSize: u64) -> MMU <'a>{
MMU {
translationTable : None
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;
}
}

View File

@ -15,7 +15,7 @@ use std::{
/// Aliases the rather long HashMap<MachineSettingKey, i32> type
/// to a rather simpler to understand Settings.
pub type Settings = HashMap<MachineSettingKey, i32>;
pub type Settings = HashMap<MachineSettingKey, u64>;
/// Keys for the Settings HashMap, represented as enums for
/// maintainability.
@ -92,6 +92,13 @@ pub fn read_settings() -> Result<Settings, Error> {
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<R: std::io::Read>(reader: BufReader<R>) -> Vec<String> {
reader.lines()
.map(|l| l.unwrap())
@ -101,7 +108,7 @@ fn filter_garbage<R: std::io::Read>(reader: BufReader<R>) -> Vec<String> {
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
}