Skip to content

Commit 4156e07

Browse files
authored
Merge pull request #1853 from arrv-sc/master
feat: add possibility for custom CSRs
2 parents fd0a927 + adafbd3 commit 4156e07

File tree

6 files changed

+106
-3
lines changed

6 files changed

+106
-3
lines changed

ci-tests/create-ci-binary-tarball

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -e
44
rm -rf build
55

66
mkdir -p build/pk && cd "$_"
7-
`git rev-parse --show-toplevel`/../riscv-pk/configure --host=riscv64-unknown-elf
7+
`git rev-parse --show-toplevel`/../riscv-pk/configure --host=riscv64-unknown-elf --with-arch=rv64gc_zifencei
88
make -j4
99
cd -
1010

@@ -16,9 +16,14 @@ mkdir -p build/dummy-slliuw && cd "$_"
1616
riscv64-unknown-elf-gcc -O2 -o dummy-slliuw `git rev-parse --show-toplevel`/ci-tests/dummy-slliuw.c
1717
cd -
1818

19+
mkdir -p build/dummycsr && cd "$_"
20+
riscv64-unknown-elf-gcc -O2 -o customcsr `git rev-parse --show-toplevel`/ci-tests/customcsr.c
21+
cd -
22+
1923
mv build/pk/pk .
2024
mv build/hello/hello .
2125
mv build/dummy-slliuw/dummy-slliuw .
22-
tar -cf spike-ci.tar pk hello dummy-slliuw
26+
mv build/dummycsr/customcsr .
27+
tar -cf spike-ci.tar pk hello dummy-slliuw customcsr
2328

24-
rm pk hello dummy-slliuw
29+
rm pk hello dummy-slliuw customcsr

ci-tests/custom-csr.cc

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <riscv/extension.h>
2+
#include <riscv/sim.h>
3+
4+
5+
class dummycsr_t: public csr_t {
6+
public:
7+
dummycsr_t(processor_t *proc, const reg_t addr): csr_t(proc, addr) {}
8+
9+
reg_t read() const noexcept override {
10+
return 42;
11+
}
12+
13+
void verify_permissions(insn_t insn, bool write) const override {}
14+
15+
protected:
16+
bool unlogged_write(const reg_t val) noexcept override {
17+
return true;
18+
}
19+
};
20+
21+
// dummy extension with dummy CSRs. Nice.
22+
struct xdummycsr_t : public extension_t {
23+
const char *name() { return "dummycsr"; }
24+
25+
xdummycsr_t() {}
26+
27+
std::vector<insn_desc_t> get_instructions() override {
28+
return {};
29+
}
30+
31+
std::vector<disasm_insn_t *> get_disasms() override {
32+
return {};
33+
}
34+
35+
std::vector<csr_t_p> get_csrs(processor_t &proc) const override {
36+
return {std::make_shared<dummycsr_t>(&proc, /*Addr*/ 0xfff)};
37+
}
38+
};
39+
40+
REGISTER_EXTENSION(dummycsr, []() { return new xdummycsr_t; })
41+
42+
// Copied from spike main.
43+
// TODO: This should really be provided in libriscv
44+
static std::vector<std::pair<reg_t, abstract_mem_t *>>
45+
make_mems(const std::vector<mem_cfg_t> &layout) {
46+
std::vector<std::pair<reg_t, abstract_mem_t *>> mems;
47+
mems.reserve(layout.size());
48+
for (const auto &cfg : layout) {
49+
mems.push_back(std::make_pair(cfg.get_base(), new mem_t(cfg.get_size())));
50+
}
51+
return mems;
52+
}
53+
54+
int main(int argc, char **argv) {
55+
cfg_t cfg;
56+
cfg.isa = "RV64IMAFDCV_Zicsr_xdummycsr";
57+
std::vector<device_factory_sargs_t> plugin_devices;
58+
if (argc != 3) {
59+
std::cerr << "Error: invalid arguments\n";
60+
exit(1);
61+
}
62+
std::vector<std::string> htif_args{argv[1] /* pk */, argv[2] /* executable */};
63+
debug_module_config_t dm_config = {.progbufsize = 2,
64+
.max_sba_data_width = 0,
65+
.require_authentication = false,
66+
.abstract_rti = 0,
67+
.support_hasel = true,
68+
.support_abstract_csr_access = true,
69+
.support_abstract_fpr_access = true,
70+
.support_haltgroups = true,
71+
.support_impebreak = true};
72+
std::vector<std::pair<reg_t, abstract_mem_t *>> mems =
73+
make_mems(cfg.mem_layout);
74+
sim_t sim(&cfg, false, mems, plugin_devices, htif_args, dm_config,
75+
nullptr, // log_path
76+
true, // dtb_enabled
77+
nullptr, // dtb_file
78+
false, // socket_enabled
79+
nullptr); // cmd_file
80+
sim.run();
81+
}

ci-tests/customcsr.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
int x = 1;
5+
// dummycsr
6+
asm("csrr %0, 0xfff" : "=r"(x));
7+
if (x == 42)
8+
printf("Executed successfully\n");
9+
else
10+
printf("FAIL. Got value: %d instead of 42\n", x);
11+
return 0;
12+
}

ci-tests/test-spike

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ time ../install/bin/spike --isa=rv64gc pk hello | grep "Hello, world! Pi is app
1515
# check that including sim.h in an external project works
1616
g++ -std=c++2a -I../install/include -L../install/lib $DIR/testlib.cc -lriscv -o test-libriscv
1717
g++ -std=c++2a -I../install/include -L../install/lib $DIR/test-customext.cc -lriscv -o test-customext
18+
g++ -std=c++2a -I../install/include -L../install/lib $DIR/custom-csr.cc -lriscv -o test-custom-csr
1819

1920
# check that all installed headers are functional
2021
g++ -std=c++2a -I../install/include -L../install/lib $DIR/testlib.cc -lriscv -o /dev/null -include ../install-hdrs-list.h
2122

2223
LD_LIBRARY_PATH=../install/lib ./test-libriscv pk hello| grep "Hello, world! Pi is approximately 3.141588."
2324
LD_LIBRARY_PATH=../install/lib ./test-customext pk dummy-slliuw | grep "Executed successfully"
25+
LD_LIBRARY_PATH=../install/lib ./test-custom-csr pk customcsr | grep "Executed successfully"

riscv/extension.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class extension_t
1313
public:
1414
virtual std::vector<insn_desc_t> get_instructions() = 0;
1515
virtual std::vector<disasm_insn_t*> get_disasms() = 0;
16+
virtual std::vector<csr_t_p> get_csrs ([[maybe_unused]] processor_t &proc) const { return {}; };
1617
virtual const char* name() = 0;
1718
virtual void reset() {};
1819
virtual void set_debug(bool UNUSED value) {}

riscv/processor.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,8 @@ void processor_t::register_extension(extension_t *x) {
711711
fprintf(stderr, "extensions must have unique names (got two named \"%s\"!)\n", x->name());
712712
abort();
713713
}
714+
for (auto &csr: x->get_csrs(*this))
715+
state.add_csr(csr->address, csr);
714716
x->set_processor(this);
715717
}
716718

0 commit comments

Comments
 (0)