use std::process::ExitCode;
use et_abi::{CacheTestArgs, DeviceArgs, MINIONS_PER_SHIRE};
use et_soc1::{Device, LaunchOptions};
const EXC_BUF_LEN: usize = 512;
const SENTINEL: u8 = 0xFF;
const SENTINEL_WORD: u32 = u32::from_ne_bytes([SENTINEL; 4]);
fn main() -> ExitCode {
match run() {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("error: {e}");
ExitCode::FAILURE
}
}
}
fn run() -> et_soc1::Result<()> {
let argv: Vec<String> = std::env::args().collect();
let kernel_path = argv.get(1).cloned().unwrap_or_else(|| {
eprintln!("usage: cache_test <cache-test-rs.elf> [shires] [dest:1=L2,2=L3,3=Mem]");
std::process::exit(2);
});
let elf = std::fs::read(&kernel_path).map_err(|e| et_soc1::Error::io("read kernel ELF", e))?;
let device = Device::open(0)?;
let topo = device.topology()?;
let max_shires = topo.num_shires() as usize;
let n_shires = argv
.get(2)
.and_then(|s| s.parse::<usize>().ok())
.map(|k| k.clamp(1, max_shires))
.unwrap_or(max_shires);
let dest: u64 = argv.get(3).and_then(|s| s.parse().ok()).unwrap_or(3);
let mut shire_mask = 0u64;
for shire in (0..u64::BITS)
.filter(|&s| topo.shire_mask & (1 << s) != 0)
.take(n_shires)
{
shire_mask |= 1 << shire;
}
let shire_extent = (u64::BITS - shire_mask.leading_zeros()) as usize;
let n_cells = shire_extent * MINIONS_PER_SHIRE as usize;
let n_minions = n_shires * MINIONS_PER_SHIRE as usize;
let launched = |cell: usize| shire_mask & (1 << (cell / MINIONS_PER_SHIRE as usize)) != 0;
println!(
"Device: {} shires present (mask {:#x}); running {} shire(s) (mask {:#x}), {} Minions, dest = {}",
max_shires,
topo.shire_mask,
n_shires,
shire_mask,
n_minions,
dest_name(dest),
);
let kernel = device.load_kernel(&elf)?;
let output = device.alloc_padded::<u32>(n_cells)?;
device.fill(output.region(), SENTINEL)?;
let args = CacheTestArgs {
output: output.addr(),
n_shires: shire_extent as u64,
dest,
};
let exc = if std::env::var_os("ET_EXC_BUFFER").is_some() {
let region = device.alloc(EXC_BUF_LEN as u64)?;
device.memcpy_h2d(&[0u8; EXC_BUF_LEN], region.addr)?;
println!(
"ET_EXC_BUFFER set: capturing U-mode context @ {:#x}",
region.addr
);
Some(region)
} else {
None
};
println!("Launching cache_writeback test ...");
let mut opts = LaunchOptions::new(shire_mask).with_args(args.as_bytes().to_vec());
if std::env::var_os("ET_NO_BARRIER").is_some() {
opts = opts.without_barrier();
}
if let Some(ref region) = exc {
opts.exception_buffer = region.addr;
}
if let Err(e) = device.launch(&kernel, &opts) {
if let Some(ref region) = exc {
let mut raw = vec![0u8; EXC_BUF_LEN];
device.memcpy_d2h(region.addr, &mut raw)?;
print_exception(&raw);
}
return Err(e);
}
println!("Kernel returned.");
let results = device.download_padded(&output)?;
let mut failures = 0_usize;
for (i, &val) in results.iter().enumerate() {
let expected = if launched(i) { i as u32 } else { SENTINEL_WORD };
if val != expected {
eprintln!(" FAIL output[{i}] = {val:#010x} (expected {expected:#010x})");
failures += 1;
}
}
if failures == 0 {
println!("cache_writeback PASSED: all {} cells correct", n_cells);
Ok(())
} else {
Err(et_soc1::Error::Protocol(format!(
"cache_writeback FAILED: {failures}/{n_cells} cells incorrect"
)))
}
}
fn dest_name(dest: u64) -> &'static str {
match dest {
1 => "L2",
2 => "L3",
_ => "Mem",
}
}
fn print_exception(raw: &[u8]) {
if raw.len() < 56 {
eprintln!(" exception buffer too short to decode");
return;
}
let rd = |o: usize| u64::from_le_bytes(raw[o..o + 8].try_into().unwrap());
let ty = rd(0); let hart_id = rd(16);
let mepc = rd(24); let mstatus = rd(32);
let mtval = rd(40); let mcause = rd(48);
if ty == 0 && mepc == 0 && mcause == 0 {
eprintln!(" exception buffer empty: firmware saved no U-mode context here");
return;
}
eprintln!(" U-mode exception context (first faulting hart):");
eprintln!(" hart_id = {hart_id}");
eprintln!(" mcause = {mcause:#x} ({})", mcause_name(mcause));
eprintln!(" mepc = {mepc:#x} (faulting instruction PC)");
eprintln!(" mtval = {mtval:#x} (faulting address / instruction bits)");
eprintln!(" mstatus = {mstatus:#x}");
}
fn mcause_name(cause: u64) -> &'static str {
if cause >> 63 != 0 {
return "interrupt";
}
match cause & 0xff {
0 => "instruction address misaligned",
1 => "instruction access fault",
2 => "illegal instruction",
3 => "breakpoint",
4 => "load address misaligned",
5 => "load access fault",
6 => "store/AMO address misaligned",
7 => "store/AMO access fault",
8 => "environment call from U-mode",
12 => "instruction page fault",
13 => "load page fault",
15 => "store/AMO page fault",
_ => "other/unknown",
}
}