From f71937d4bb74ad0ba8937da94643ddfe403ea346 Mon Sep 17 00:00:00 2001 From: David Li Date: Mon, 4 Jan 2016 13:43:41 -0700 Subject: Load ELF directly --- src/lib.rs | 55 ++++++++++++++++++------------------------- src/rust-elfloader/src/lib.rs | 4 ++++ src/simulator.rs | 8 ++----- 3 files changed, 29 insertions(+), 38 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1a176fd..c3fa51b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,30 +20,12 @@ pub mod binary; pub mod memory; pub mod simulator; -#[test] -fn it_works() { - use std::rc::Rc; - use std::cell::RefCell; - - use std::path::Path; - match binary::Binary::new_from_hex_file(Path::new("../riscv/kernel.hex")) { - Ok(b) => { - let mmu = memory::IdentityMmu::new(); - let memory = memory::Memory::new_from_binary(0x2000, b); - let memory_ref = Rc::new(RefCell::new(Box::new(memory) as Box)); - let cache = Rc::new( RefCell::new( Box::new( memory::DirectMappedCache::new(4, 4, memory_ref.clone())) as Box) ); - let core = simulator::Core::new(cache.clone(), Box::new(mmu)); - let mut simulator = simulator::Simulator::new(vec![core], memory_ref.clone()); - simulator.run(); - }, - Err(err) => println!("Error: {:?}", err), - } -} - #[test] fn test_elfloader() { use std::io::prelude::*; use std::fs::File; + use std::rc::Rc; + use std::cell::RefCell; extern crate elfloader; let mut f = File::open("../riscv/kernel").unwrap(); @@ -52,23 +34,32 @@ fn test_elfloader() { f.read_to_end(&mut buffer).unwrap(); let elf = elfloader::ElfBinary::new("test", &buffer).unwrap(); - println!("HEADERS"); - for p in elf.program_headers() { - println!("{}", p); - } + let start = elf.file_header().entry as isa::Address; + + let mut text = None; + let mut data = None; for p in elf.section_headers() { - println!("{}", p); if p.name.0 == 0x1b { - let data = elf.section_data(p); - print!("\t"); - for x in data[0..8].iter() { - print!("{:02x}", x); - } - println!(""); + text = Some((elf.section_data(p), p.addr)); + } + else if p.name.0 == 0x33 { + data = Some((elf.section_data(p), p.addr)); } } - println!("{:?}", elf); + let (text, text_offset) = text.unwrap(); + let (data, data_offset) = data.unwrap(); + + let mmu = memory::IdentityMmu::new(); + let memory = memory::Memory::new_from_text_and_data( + 0x8000, + text, text_offset as usize, + data, data_offset as usize); + let memory_ref = Rc::new(RefCell::new(Box::new(memory) as Box)); + let cache = Rc::new( RefCell::new( Box::new( memory::DirectMappedCache::new(4, 4, memory_ref.clone())) as Box) ); + let core = simulator::Core::new(start, cache.clone(), Box::new(mmu)); + let mut simulator = simulator::Simulator::new(vec![core], memory_ref.clone()); + simulator.run(); } #[cfg(test)] diff --git a/src/rust-elfloader/src/lib.rs b/src/rust-elfloader/src/lib.rs index e344bcb..92cae24 100644 --- a/src/rust-elfloader/src/lib.rs +++ b/src/rust-elfloader/src/lib.rs @@ -58,6 +58,10 @@ impl<'s> ElfBinary<'s> { None } + pub fn file_header(&self) -> &'s elf::FileHeader { + self.header + } + /// Print the program headers. pub fn print_program_headers(&self) { for p in self.program_headers() { diff --git a/src/simulator.rs b/src/simulator.rs index f92d3d2..571fafe 100644 --- a/src/simulator.rs +++ b/src/simulator.rs @@ -75,9 +75,9 @@ impl RegisterFile { impl<'a> Core<'a> { // TODO: take Rc> to Memory as well? - pub fn new(cache: SharedMemory<'a>, mmu: Box) -> Core<'a> { + pub fn new(entry: isa::Address, cache: SharedMemory<'a>, mmu: Box) -> Core<'a> { Core { - pc: 0x1002c, // TODO: hardcoded: fix later + pc: entry, registers: RegisterFile::new(), stall: 0, running: true, @@ -361,10 +361,6 @@ impl<'a> Simulator<'a> { } pub fn run(&mut self) { - // hardcode _start - self.cores[0].pc = 0x1002C; - // hardcode GP - self.cores[0].registers.write_word(isa::Register::X3, 0x108D0); // hardcode SP self.cores[0].registers.write_word(isa::Register::X2, 0x7FFC); let mut total_cycles = 0; -- cgit v1.2.3