Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

diff memcpy COW optimization #5

Open
wants to merge 73 commits into
base: difftest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
70663dc
Ignore vscode project files
poemonsense Apr 19, 2023
ca2b07c
Add fuzz harness
poemonsense Apr 19, 2023
9f2036b
Add test interface to main
poemonsense Apr 19, 2023
58e9299
Update Makefile to support libafl
poemonsense Apr 20, 2023
2f570c6
Makefile: fix dependency on spike generated files
poemonsense Apr 20, 2023
ceab357
Disable default signal handlers.
poemonsense Apr 20, 2023
a15ba93
Close file descriptors in destructor of syscall_t
poemonsense Apr 20, 2023
bf483df
fuzz: remove catiss dependencies
poemonsense Apr 21, 2023
1f1c62f
Merge remote-tracking branch 'origin/master' into fuzz
poemonsense Jun 20, 2023
7713507
fuzz: use macros to switch code blocks
poemonsense Jun 20, 2023
b95048e
Rewrite difftest support for Spike as a REF
poemonsense Jun 20, 2023
c6f503a
csrs: set mstatus.fs to 0 or 3 for CPU_ROCKET_CHIP
poemonsense Jun 20, 2023
ee71b0d
trap: make the tval values on insn_trap configurable
poemonsense Jun 20, 2023
e4e7314
Add support for an optional flash region
poemonsense Jun 21, 2023
8eee588
difftest: fix the memcpy for n > PGSIZE
poemonsense Jun 30, 2023
3d0823e
mmu: update the physical address for DUTs
poemonsense Jun 30, 2023
ba31db1
Update ASID configs for different DUTs.
poemonsense Jun 30, 2023
695a596
Update misa write mask for rocket-chip
poemonsense Jun 30, 2023
6520266
Update medeleg write mask for rocket-chip
poemonsense Jun 30, 2023
652cfa2
Change the default mmu capability to Sv39 for DUTs
poemonsense Jun 30, 2023
5f771d3
Add support for difftest logs and traces
poemonsense Jun 30, 2023
b605d97
Add zicntr extension for rocket-chip
poemonsense Jul 3, 2023
90679be
Move macro definitions to a separated header file
poemonsense Jul 3, 2023
b73fe6f
Add support for disabling AMO store diff
poemonsense Jul 3, 2023
a7ff4a2
Set tval to zero on instruction misaligned exceptions
poemonsense Jul 3, 2023
9e88039
Fix the missing AMO store condition for SC
poemonsense Jul 3, 2023
45243be
Fix the display for FPR
poemonsense Jul 3, 2023
a490da9
mmu: set is_amo before store operations
poemonsense Jul 3, 2023
14886a2
difftest: use rom_device_t for the read-only flash
poemonsense Jul 4, 2023
5be2b5f
Add Sdtrig isa string with permission checkers for CSRs
poemonsense Jul 5, 2023
e2aedc0
difftrace: use Enum to wrap memory access types
poemonsense Jul 7, 2023
72bfbef
Add support for instruction traces
poemonsense Jul 7, 2023
e26ab3f
Add support for forced SC failures
poemonsense Jul 7, 2023
5cd8876
Add support for REF-provided disambiguation state
poemonsense Jul 7, 2023
f7a2914
Add support for PTW's ambiguation states
poemonsense Jul 7, 2023
5373861
Add support for ambiguation after satp is updated
poemonsense Jul 7, 2023
3e89f41
store_tracker: check the next position if needed
poemonsense Jul 9, 2023
bf196f5
csrs: align the behaviors of epc with rocket-chip
poemonsense Jul 10, 2023
d3c0087
difftest: fix the memory leak on plugin_devices
poemonsense Jul 13, 2023
afef448
difftest: fix the flash base address
poemonsense Jul 13, 2023
bea82f1
csr,tvec: mode is non-writable for CPU_NUTSHELL
poemonsense Jul 13, 2023
6dda269
proc: fix the counteren_mask for rocket-chip
poemonsense Jul 13, 2023
8586b05
mmu: clear sc_failed after check_load_reservation
poemonsense Jul 14, 2023
06458b0
difftest: add on_demand option to regcpy
poemonsense Jul 14, 2023
3d431b6
proc: fix the IALIGN for RVC
poemonsense Jul 14, 2023
ebfdc67
Update marchid for rocket-chip
poemonsense Jul 14, 2023
17ca299
Disable time CSR in difftest mode
poemonsense Jul 14, 2023
8a882b4
csrs: fix the permissions of mcontext
poemonsense Jul 15, 2023
2e64f66
processor: fix the mimpid for rocket-chip
poemonsense Jul 15, 2023
32516bd
sim: disable routine timer update for DiffTest
poemonsense Jul 15, 2023
552126d
mmu: fix the order of bytes on instruction fetch
poemonsense Jul 18, 2023
619bc48
Update default CSR values and masks for NutShell
poemonsense Jul 18, 2023
7d4aac7
csrs: mstatus.fs is always zero in NutShell
poemonsense Jul 18, 2023
2017630
Add support for sanitizer coverage instrumentation
poemonsense Jul 20, 2023
3c2b4bb
Add the difftest_display interface
poemonsense Jul 20, 2023
437777c
Allow the memory size to be overrided at run-time
poemonsense Jul 21, 2023
bbd5213
Allow the mhartid to be overrided at run-time
poemonsense Jul 21, 2023
7e7d660
mmu: change the size of reservation sets for DIFFTEST
poemonsense Jul 21, 2023
6abfa14
sfence_vma: fix the missing flush_tlb_on_sfence_vma
poemonsense Jul 22, 2023
08ec9c4
difftrace: set satp_written only if vm enabled previously
poemonsense Jul 22, 2023
31aa3be
decode: disable 48- and 64-bit instructions if needed
poemonsense Jul 23, 2023
63d1b1e
Add support for the ref_skip_one interface
poemonsense Jul 23, 2023
57a8870
Update PMP configs for XIANGSHAN
poemonsense Sep 4, 2023
dc253dd
debug,dummy: return false on out-of-range accesses
poemonsense Sep 5, 2023
f857851
Create README.md
poemonsense Sep 11, 2023
fe255a0
Update README.md
poemonsense Sep 11, 2023
549e4e9
proc: avoid initializing PMP entries at reset with DIFFTEST
poemonsense Sep 12, 2023
c8036d7
Show PMP registers on reg_display
poemonsense Sep 12, 2023
80a3c81
Update writable mask for `misa` of XiangShan
poemonsense Sep 17, 2023
9297021
Update `mimpid` and `marchid` for XiangShan
poemonsense Sep 17, 2023
71de241
Add support for RISC-V vector extension difftest (#3)
weidingliu Sep 21, 2023
b333b1d
difftest: fix init of mstatus (#4)
weidingliu Oct 24, 2023
145445f
Update diff memcpy with COW optimization
tastynoob Dec 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ autom4te.cache/
.gdb_history
.#*
*~
.vscode

1 change: 1 addition & 0 deletions difftest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
84 changes: 84 additions & 0 deletions difftest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
CC = clang
CXX = clang++

GUEST_ISA = riscv64
BUILD_DIR = $(abspath ./build)
TARGET = $(BUILD_DIR)/$(GUEST_ISA)-spike-so

# Dependencies for building Spike
SPIKE_PATH = $(abspath ..)
SPIKE_TARGET = $(BUILD_DIR)/spike
SPIKE_MAKEFILE = $(BUILD_DIR)/Makefile

SPIKE_CFLAGS = -O3 -DDIFFTEST
SPIKE_CXXFLAGS = -O3 -DDIFFTEST -Wno-c99-designator
SPIKE_LDFLAGS =

ifneq ($(CPU),)
SPIKE_CFLAGS += -DCPU_$(CPU)
SPIKE_CXXFLAGS += -DCPU_$(CPU)
endif

ifeq ($(DEBUG),)
SPIKE_CFLAGS += -g0
SPIKE_CXXFLAGS += -g0
endif

ifeq ($(SANCOV),1)
SPIKE_CFLAGS += -fsanitize-coverage=trace-pc-guard -fsanitize-coverage=pc-table -g
SPIKE_CXXFLAGS += -fsanitize-coverage=trace-pc-guard -fsanitize-coverage=pc-table -g
SPIKE_LDFLAGS += -fsanitize-coverage=trace-pc-guard -fsanitize-coverage=pc-table -g
endif

CONFIGURE_FLAGS = -q CC=$(CC) CXX=$(CXX) \
--with-boost=no --with-boost-asio=no --with-boost-regex=no \
CFLAGS="$(SPIKE_CFLAGS)" CXXFLAGS="$(SPIKE_CXXFLAGS)" \
LDFLAGS="$(SPIKE_LDFLAGS)"

SPIKE_INCS = fesvr riscv disasm customext fdt softfloat spike_main spike_dasm
INC_PATH = -I$(SPIKE_PATH) -I$(BUILD_DIR) $(addprefix -I$(SPIKE_PATH)/, $(SPIKE_INCS))

SPIKE_SRCS = $(shell find $(SPIKE_PATH) -maxdepth 2 -name '*.cc') \
$(shell find $(SPIKE_PATH) -maxdepth 2 -name '*.h')
SPIKE_LIBS = libriscv.a libdisasm.a libsoftfloat.a libfesvr.a libfdt.a
INC_LIBS = $(addprefix $(BUILD_DIR)/, $(SPIKE_LIBS))

# We need some utilities from spike
CXX_UTILS = $(BUILD_DIR)/difftest_utils.cc

$(CXX_UTILS): $(SPIKE_PATH)/spike_main/spike.cc
cp $< $(CXX_UTILS)
@sed -i 's/static //g' $@
@sed -i 's/int main/static int spike_main/g' $@

# Dependencies for building the difftest dynamic library
DIFFTEST_SOURCES = $(shell find . -maxdepth 1 -name '*.cc') $(CXX_UTILS)
DIFFTEST_HEADERS = $(shell find . -maxdepth 1 -name '*.h')
CXXFLAGS = $(SPIKE_CXXFLAGS) --std=c++17 -shared -fPIC
ifneq ($(SANCOV),1)
CXXFLAGS += -flto
endif

ifneq ($(DEBUG),)
CONFIGURE_FLAGS += --enable-commitlog
CXXFLAGS += -DDIFFTEST_LOG_FILE=\"$(NOOP_HOME)/build/spike.log\"
endif

$(BUILD_DIR):
mkdir -p $(BUILD_DIR)

$(SPIKE_MAKEFILE): $(SPIKE_PATH)/configure | $(BUILD_DIR)
cd $(@D) && $(abspath $(SPIKE_PATH))/configure $(CONFIGURE_FLAGS)

$(SPIKE_TARGET): $(SPIKE_MAKEFILE) $(SPIKE_SRCS)
$(MAKE) -s -C $(BUILD_DIR)

$(TARGET): $(SPIKE_TARGET) $(DIFFTEST_SOURCES) $(DIFFTEST_HEADERS) | $(BUILD_DIR)
$(CXX) $(CXXFLAGS) $(INC_PATH) $(DIFFTEST_SOURCES) $(INC_LIBS) -o $@

clean:
rm -rf $(BUILD_DIR)

.DEFAULT_GOAL = $(TARGET)

.PHONY: clean
31 changes: 31 additions & 0 deletions difftest/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Spike as a Reference Model

The Spike RISC-V ISA simulator can be a golden reference model for CPU co-simulation as well.
This directory contains the wrapper files for using Spike as the REF used in DiffTest.

## Supported CPUs

RISC-V allows diverse implementation-specific behaviors as long as they don't violate the ISA.
Therefore, to co-simulate Spike, one of the RISC-V implementations, with another implementation
without errors, we have to align their behaviors on the undefined or design-specific regions.

Currently we are supporting CPUs including:
- [XiangShan](https://github.com/OpenXiangShan/XiangShan)
- [NutShell](https://github.com/OSCPU/NutShell)
- [Rocket Chip](https://github.com/chipsalliance/rocket-chip)

## How to Compile

To compile the Spike into a dynamic library for co-simulation via DiffTest, run:

```
cd riscv-isa-sim/difftest
make CPU=XIANGSHAN -jN
```

Replace `XIANGSHAN` with `ROCKET` or `NUTSHELL` for co-simulation with Rocket or NutShell.

## Coverage Instrumentation with LLVM

Add `SANCOV=1` to the `make` command to instrument the source code with LLVM coverage.
This enables fuzzers to be guided with branch coverage feedback from a golden RISC-V model.
46 changes: 46 additions & 0 deletions difftest/difftest-def.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef __DIFFTEST_DEF_H
#define __DIFFTEST_DEF_H

#if defined(CPU_NUTSHELL)
#elif defined(CPU_XIANGSHAN)
#elif defined(CPU_ROCKET_CHIP)
#else
// This is the default CPU
#define CPU_NUTSHELL
#endif

#if defined(CPU_XIANGSHAN) || defined(CPU_ROCKET_CHIP)
#define CONFIG_DIFF_FPU
#endif

#if defined(CPU_XIANGSHAN)
#define CONFIG_DIFF_DEBUG_MODE
// #define CONFIG_DIFF_RVV // Default off
#endif

#if defined(CPU_NUTSHELL)
#define CONFIG_DIFF_ISA_STRING "rv64imaczicsr_zifencei"
#define CONFIG_MEMORY_SIZE (2 * 1024 * 1024 * 1024UL)
#define CONFIG_FLASH_BASE 0x40000000UL
#define CONFIG_FLASH_SIZE 0x1000UL
#define CONFIG_PMP_NUM 0
#elif defined(CPU_XIANGSHAN)
#if defined(CONFIG_DIFF_RVV)
#define CONFIG_DIFF_ISA_STRING "RV64IMAFDCV_zba_zbb_zbc_zbs_zbkb_zbkc_zbkx_zknd_zkne_zknh_zksed_zksh_svinval"
#else
#define CONFIG_DIFF_ISA_STRING "RV64IMAFDC_zba_zbb_zbc_zbs_zbkb_zbkc_zbkx_zknd_zkne_zknh_zksed_zksh_svinval"
#endif // CONFIG_DIFF_RVV
#define CONFIG_MEMORY_SIZE (16 * 1024 * 1024 * 1024UL)
#define CONFIG_FLASH_BASE 0x10000000UL
#define CONFIG_FLASH_SIZE 0x100000UL
#define CONFIG_PMP_NUM 16
#define CONFIG_PMP_GRAN 12
#elif defined(CPU_ROCKET_CHIP)
#define CONFIG_DIFF_ISA_STRING "rv64imafdczicsr_zifencei_zihpm_zicntr"
#define CONFIG_MEMORY_SIZE (2 * 1024 * 1024 * 1024UL)
#define CONFIG_FLASH_BASE 0x10000000UL
#define CONFIG_FLASH_SIZE 0x10000UL
#define CONFIG_PMP_NUM 0
#endif

#endif
Loading