aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Li <li.davidm96@gmail.com>2016-01-07 11:12:49 -0700
committerDavid Li <li.davidm96@gmail.com>2016-01-07 11:12:49 -0700
commit940a5ae77a1efc8f89fe5b56fe4a4ae34a0e480d (patch)
tree49978de652f60c0bacd4ac5e28dd1fe67e00f56c
parentfbbb16d448a92251779eb6b2e255b05e69bb3868 (diff)
Implement print system call
-rw-r--r--Cargo.lock4
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs59
3 files changed, 46 insertions, 19 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c98941d..12379b0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,7 +4,7 @@ version = "0.1.0"
dependencies = [
"docopt 0.6.78 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
- "rustv 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "rustv 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -65,7 +65,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "rustv"
-version = "0.2.4"
+version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"elfloader32 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index e9c924a..7efe290 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,6 +4,6 @@ version = "0.1.0"
authors = ["David Li <li.davidm96@gmail.com>"]
[dependencies]
-rustv = "0.2.4"
+rustv = "0.3.0"
docopt = "0.6"
rustc-serialize = "0.3"
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<trap::Trap> {
+ 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<T: memory::Mmu>(memory: &mut memory::Memory,
mmu: &T, path: &str) -> isa::Address {
let mut f = File::open(path).unwrap();
@@ -106,23 +143,13 @@ fn load_program<T: memory::Mmu>(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();