From 940a5ae77a1efc8f89fe5b56fe4a4ae34a0e480d Mon Sep 17 00:00:00 2001 From: David Li Date: Thu, 7 Jan 2016 11:12:49 -0700 Subject: Implement print system call --- src/main.rs | 59 +++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index e91db5f..722efdb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,10 @@ use rustv::isa; use rustv::memory; use rustv::memory::MemoryInterface; use rustv::memory::Mmu; +use rustv::register_file; use rustv::simulator; +use rustv::syscall; +use rustv::trap; const USAGE: &'static str = " cacheracer - A F/OSS implementation of the CS 3410 CacheRacer @@ -96,6 +99,40 @@ impl memory::MemoryInterface for MemoryTracker { } } +struct SyscallHandler<'a> { + memory: memory::SharedMemory<'a>, +} + +impl<'a> syscall::SyscallHandler for SyscallHandler<'a> { + fn syscall(&mut self, registers: &mut register_file::RegisterFile) -> Option { + println!("Syscall number {}", registers.read_word(isa::Register::X10)); + let mut base = registers.read_word(isa::Register::X11); + let mut string = vec![]; + + loop { + let c = self.memory.borrow_mut().read_byte(base); + + if let Ok(0x00) = c { + break; + } + else if let Ok(c) = c { + string.push(c); + } + + base += 1; + } + + let result = std::str::from_utf8(&string); + if let Ok(string) = result { + println!("{}", string); + } + else { + println!("Error printing string: {:?}", result); + } + None + } +} + fn load_program(memory: &mut memory::Memory, mmu: &T, path: &str) -> isa::Address { let mut f = File::open(path).unwrap(); @@ -106,23 +143,13 @@ fn load_program(memory: &mut memory::Memory, let elf = elfloader::ElfBinary::new("test", &buffer).unwrap(); let start = elf.file_header().entry as isa::Address; - let mut text = None; - let mut data = None; for p in elf.section_headers() { - if p.name.0 == 0x1b { - text = Some((elf.section_data(p), p.addr)); - } - else if p.name.0 == 0x33 { - data = Some((elf.section_data(p), p.addr)); + let name = elf.section_name(p); + if name == ".text" || name == ".sdata" || name == ".rodata" { + memory.write_segment(mmu, elf.section_data(p), p.addr as usize); } } - let (text, text_offset) = text.unwrap(); - let (data, data_offset) = data.unwrap(); - - memory.write_segment(mmu, text, text_offset as usize); - memory.write_segment(mmu, data, data_offset as usize); - start } @@ -156,9 +183,9 @@ fn main() { start2, 0x1000, cache_ref.clone(), Box::new(mmu2)); let cores = vec![core, core2]; - let mut simulator = simulator::Simulator::new(cores, memory_ref.clone()); - - println!("{:?}", memory_ref.borrow_mut().read_instruction((start1))); + let system = SyscallHandler { memory: memory_ref.clone(), }; + let mut simulator = simulator::Simulator::new( + cores, memory_ref.clone(), system); simulator.run(); -- cgit v1.2.3