Skip to content

Commit afea6fd

Browse files
committed
Replace elf reading with xmas-elf
1 parent d1c6a03 commit afea6fd

File tree

9 files changed

+107
-886
lines changed

9 files changed

+107
-886
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ num-traits = "*"
3232
num-derive = "*"
3333
wasm-timer = "0.2.5"
3434
log = "0.4.26"
35+
xmas-elf = "0.10.0"
3536

3637
[dev-dependencies]
3738
libc = "0.2.155"
3839
termios = "0.3.3"
3940
getopts = "0.2"
4041
env_logger = "0.11.7"
42+
xmas-elf = "0.10.0"
43+
anyhow = "1.0.98"
4144

4245
[[example]]
4346
name = "sim"

examples/sim/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod popup_terminal;
44

55
use crate::dummy_terminal::DummyTerminal;
66
use crate::popup_terminal::PopupTerminal;
7+
use anyhow::anyhow;
78
use getopts::Options;
89
use simmerv::Emulator;
910
use simmerv::terminal::Terminal;
@@ -28,7 +29,7 @@ fn get_terminal(terminal_type: &TerminalType) -> Box<dyn Terminal> {
2829
}
2930
}
3031

31-
fn main() -> std::io::Result<()> {
32+
fn main() -> anyhow::Result<()> {
3233
env_logger::init();
3334

3435
let args: Vec<String> = env::args().collect();
@@ -100,8 +101,15 @@ fn main() -> std::io::Result<()> {
100101
TerminalType::PopupTerminal
101102
};
102103

104+
let mut symbols = std::collections::BTreeMap::new();
103105
let mut emulator = Emulator::new(get_terminal(&terminal_type));
104-
emulator.setup_program(elf_contents);
106+
emulator
107+
.setup_program(&elf_contents, 0x80000000, &mut symbols)
108+
.map_err(|e| anyhow!(e))?;
109+
if let Some(addr) = symbols.get("tohost") {
110+
emulator.tohost_addr = *addr;
111+
}
112+
105113
emulator.setup_filesystem(fs_contents);
106114
if has_dtb {
107115
emulator.setup_dtb(&dtb_contents);

src/cpu.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,15 @@ impl Cpu {
282282
}
283283
}
284284

285-
fn handle_exception(&mut self, exc: &Exception) {
286-
// XXX If we pass in the address we don't need
287-
// self.pc, but that requires us to call handle exception from a centrol
288-
// location with access to the pc.
289-
self.handle_trap(exc, self.insn_addr, false);
285+
fn handle_exception(&mut self, exception: &Exception) {
286+
if matches!(exception.trap, Trap::IllegalInstruction) {
287+
log::info!(
288+
"Illegal instruction {:016x} {:x}",
289+
self.insn_addr,
290+
self.insn
291+
);
292+
}
293+
self.handle_trap(exception, self.insn_addr, false);
290294
}
291295

292296
#[allow(clippy::similar_names, clippy::too_many_lines)]
@@ -557,13 +561,11 @@ impl Cpu {
557561
return illegal;
558562
}
559563

560-
let mode = (value >> SATP_MODE_SHIFT) & SATP_MODE_MASK;
561-
if mode != 0
562-
&& mode != SatpMode::Sv39 as u64
563-
&& mode != SatpMode::Sv48 as u64
564-
&& mode != SatpMode::Sv57 as u64
565-
{
566-
log::warn!("Illegal SATP mode {mode:02x}");
564+
if !matches!(
565+
FromPrimitive::from_u64((value >> SATP_MODE_SHIFT) & SATP_MODE_MASK),
566+
Some(SatpMode::Bare | SatpMode::Sv39 | SatpMode::Sv48 | SatpMode::Sv57)
567+
) {
568+
log::warn!("wrote illegal value {value:x} to satp");
567569
return illegal;
568570
}
569571
}
@@ -1822,7 +1824,7 @@ const INSTRUCTIONS: [Instruction; INSTRUCTION_NUM] = [
18221824
if word == 0x0100000f {
18231825
// Nothing to do here, but it would be interesting to see
18241826
// it used.
1825-
log::trace!("pause");
1827+
log::trace!("pause isn't yet implemented");
18261828
}
18271829
// Fence memory ops (we are currently TSO already)
18281830
Ok(())

src/csr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ pub enum Csr {
1818
Sstatus = 0x100,
1919
Sedeleg = 0x102,
2020
Sideleg = 0x103,
21+
Scounteren = 0x106,
2122
Sie = 0x104,
2223
Stvec = 0x105,
23-
Csr106 = 0x106,
2424
Sscratch = 0x140,
2525
Sepc = 0x141,
2626
Scause = 0x142,
2727
Stval = 0x143,
2828
Sip = 0x144,
2929
Satp = 0x180,
30+
3031
Mstatus = 0x300,
3132
Misa = 0x301,
3233
Medeleg = 0x302,
@@ -167,6 +168,7 @@ pub const fn legal(csr: Csr) -> bool {
167168
| Csr::Sedeleg
168169
| Csr::Sepc
169170
| Csr::Sideleg
171+
| Csr::Scounteren
170172
| Csr::Sie
171173
| Csr::Sip
172174
| Csr::Sscratch
@@ -182,7 +184,6 @@ pub const fn legal(csr: Csr) -> bool {
182184
| Csr::Ustatus
183185
| Csr::Utval
184186
| Csr::Utvec
185-
| Csr::Csr106
186187
| Csr::Csr306
187188
)
188189
}

0 commit comments

Comments
 (0)