From b3a4c82fd2291d9e7d2702c4720e93cf2b2b86ae Mon Sep 17 00:00:00 2001 From: Jerry Zhao Date: Wed, 12 Apr 2023 01:39:12 -0700 Subject: [PATCH] Add generate-ckpt script --- scripts/generate-ckpt.sh | 107 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100755 scripts/generate-ckpt.sh diff --git a/scripts/generate-ckpt.sh b/scripts/generate-ckpt.sh new file mode 100755 index 00000000..f4987a24 --- /dev/null +++ b/scripts/generate-ckpt.sh @@ -0,0 +1,107 @@ +#!/bin/bash + +set -ex + +usage() { + echo "Usage: $0 [OPTIONS] -- [SPIKEFLAGS]" + echo "" + echo "Options" + echo " --help -h : Display this message" + echo " -n : Mumber of harts" + echo " -b : Binary to run in spike" + echo " -p : PC to take checkpoint at [default 0x80000000]" + echo " -c : Cycles after PC to take checkpoint at [default 0]" + exit "$1" +} + +NHARTS=1 +BINARY="" +PC="0x80000000" +CYCLES=0 +while [ "$1" != "" ]; +do + case $1 in + -h | --help ) + usage 3 ;; + -n ) + shift + NHARTS=$1 ;; + -b ) + shift + BINARY=$1 ;; + -p ) + shift + PC=$1 ;; + -c ) + shift + CYCLES=$1 ;; + * ) + error "Invalid option $1" + usage 1 ;; + esac + shift +done +BASEMEM="$((0x80000000)):$((0x10000000))" +SPIKEFLAGS="-p$NHARTS --pmpregions=0 --isa=rv64gc -m$BASEMEM" + +rm -rf cmds_tmp.txt +touch cmds_tmp.txt + +echo "Generating state capture spike interactive commands in cmds_tmp.txt" +echo "until pc 0 $PC" >> cmds_tmp.txt +echo "rs $CYCLES" >> cmds_tmp.txt +echo "dump" >> cmds_tmp.txt +for (( h=0; h<$NHARTS; h++ )) +do + echo "pc $h" >> cmds_tmp.txt + echo "priv $h" >> cmds_tmp.txt + echo "reg $h fcsr" >> cmds_tmp.txt + + echo "reg $h stvec" >> cmds_tmp.txt + echo "reg $h sscratch" >> cmds_tmp.txt + echo "reg $h sepc" >> cmds_tmp.txt + echo "reg $h scause" >> cmds_tmp.txt + echo "reg $h stval" >> cmds_tmp.txt + echo "reg $h satp" >> cmds_tmp.txt + + echo "reg $h mstatus" >> cmds_tmp.txt + echo "reg $h medeleg" >> cmds_tmp.txt + echo "reg $h mideleg" >> cmds_tmp.txt + echo "reg $h mie" >> cmds_tmp.txt + echo "reg $h mtvec" >> cmds_tmp.txt + echo "reg $h mscratch" >> cmds_tmp.txt + echo "reg $h mepc" >> cmds_tmp.txt + echo "reg $h mcause" >> cmds_tmp.txt + echo "reg $h mtval" >> cmds_tmp.txt + echo "reg $h mip" >> cmds_tmp.txt + + echo "reg $h mcycle" >> cmds_tmp.txt + echo "reg $h minstret" >> cmds_tmp.txt + + echo "mtime" >> cmds_tmp.txt + echo "mtimecmp $h" >> cmds_tmp.txt + + for (( fr=0; fr<32; fr++ )) + do + echo "freg $h $fr" >> cmds_tmp.txt + done + for (( xr=0; xr<32; xr++ )) + do + echo "reg $h $xr" >> cmds_tmp.txt + done +done +echo "quit" >> cmds_tmp.txt + +#cat cmds_tmp.txt +BASENAME=$(basename -- $BINARY) + +echo "Capturing state at checkpoint to spikeout" +spike -d --debug-cmd=cmds_tmp.txt $SPIKEFLAGS $BINARY 2> $BASENAME.loadarch + +echo "Finding tohost/fromhost in elf file" +TOHOST=$(riscv64-unknown-elf-nm $BINARY | grep tohost | head -c 16) +FROMHOST=$(riscv64-unknown-elf-nm $BINARY | grep fromhost | head -c 16) + +echo "Compiling memory to elf" +riscv64-unknown-elf-objcopy -I binary -O elf64-littleriscv mem.0x80000000.bin $BASENAME.mem.elf +riscv64-unknown-elf-ld -Tdata=0x80000000 -nmagic --defsym tohost=0x$TOHOST --defsym fromhost=0x$FROMHOST -o $BASENAME.loadarch.elf $BASENAME.mem.elf