1 // See LICENSE for license details.
5 #include "remote_bitbang.h"
15 #include <sys/types.h>
17 volatile bool ctrlc_pressed
= false;
18 static void handle_signal(int sig
)
23 signal(sig
, &handle_signal
);
26 sim_t::sim_t(const char* isa
, size_t nprocs
, bool halted
, reg_t start_pc
,
27 std::vector
<std::pair
<reg_t
, mem_t
*>> mems
,
28 const std::vector
<std::string
>& args
,
29 std::vector
<int> const hartids
, unsigned progsize
)
30 : htif_t(args
), mems(mems
), procs(std::max(nprocs
, size_t(1))),
31 start_pc(start_pc
), current_step(0), current_proc(0), debug(false),
32 remote_bitbang(NULL
), debug_module(this, progsize
)
34 signal(SIGINT
, &handle_signal
);
37 bus
.add_device(x
.first
, x
.second
);
39 debug_module
.add_device(&bus
);
41 debug_mmu
= new mmu_t(this, NULL
);
43 if (hartids
.size() == 0) {
44 for (size_t i
= 0; i
< procs
.size(); i
++) {
45 procs
[i
] = new processor_t(isa
, this, i
, halted
);
49 if (hartids
.size() != procs
.size()) {
50 std::cerr
<< "Number of specified hartids doesn't match number of processors" << strerror(errno
) << std::endl
;
53 for (size_t i
= 0; i
< procs
.size(); i
++) {
54 procs
[i
] = new processor_t(isa
, this, hartids
[i
], halted
);
58 clint
.reset(new clint_t(procs
));
59 bus
.add_device(CLINT_BASE
, clint
.get());
64 for (size_t i
= 0; i
< procs
.size(); i
++)
69 void sim_thread_main(void* arg
)
71 ((sim_t
*)arg
)->main();
77 set_procs_debug(true);
81 if (debug
|| ctrlc_pressed
)
86 remote_bitbang
->tick();
93 host
= context_t::current();
94 target
.init(sim_thread_main
, this);
98 void sim_t::step(size_t n
)
100 for (size_t i
= 0, steps
= 0; i
< n
; i
+= steps
)
102 steps
= std::min(n
- i
, INTERLEAVE
- current_step
);
103 procs
[current_proc
]->step(steps
);
105 current_step
+= steps
;
106 if (current_step
== INTERLEAVE
)
109 procs
[current_proc
]->yield_load_reservation();
110 if (++current_proc
== procs
.size()) {
112 clint
->increment(INTERLEAVE
/ INSNS_PER_RTC_TICK
);
120 void sim_t::set_debug(bool value
)
125 void sim_t::set_log(bool value
)
130 void sim_t::set_histogram(bool value
)
132 histogram_enabled
= value
;
133 for (size_t i
= 0; i
< procs
.size(); i
++) {
134 procs
[i
]->set_histogram(histogram_enabled
);
138 void sim_t::set_procs_debug(bool value
)
140 for (size_t i
=0; i
< procs
.size(); i
++)
141 procs
[i
]->set_debug(value
);
144 bool sim_t::mmio_load(reg_t addr
, size_t len
, uint8_t* bytes
)
146 if (addr
+ len
< addr
)
148 return bus
.load(addr
, len
, bytes
);
151 bool sim_t::mmio_store(reg_t addr
, size_t len
, const uint8_t* bytes
)
153 if (addr
+ len
< addr
)
155 return bus
.store(addr
, len
, bytes
);
158 static std::string
dts_compile(const std::string
& dts
)
160 // Convert the DTS to DTB
164 if (pipe(dts_pipe
) != 0 || (dts_pid
= fork()) < 0) {
165 std::cerr
<< "Failed to fork dts child: " << strerror(errno
) << std::endl
;
169 // Child process to output dts
172 int step
, len
= dts
.length();
173 const char *buf
= dts
.c_str();
174 for (int done
= 0; done
< len
; done
+= step
) {
175 step
= write(dts_pipe
[1], buf
+done
, len
-done
);
177 std::cerr
<< "Failed to write dts: " << strerror(errno
) << std::endl
;
187 if (pipe(dtb_pipe
) != 0 || (dtb_pid
= fork()) < 0) {
188 std::cerr
<< "Failed to fork dtb child: " << strerror(errno
) << std::endl
;
192 // Child process to output dtb
194 dup2(dts_pipe
[0], 0);
195 dup2(dtb_pipe
[1], 1);
200 execl(DTC
, DTC
, "-O", "dtb", 0);
201 std::cerr
<< "Failed to run " DTC
": " << strerror(errno
) << std::endl
;
210 std::stringstream dtb
;
214 while ((got
= read(dtb_pipe
[0], buf
, sizeof(buf
))) > 0) {
218 std::cerr
<< "Failed to read dtb: " << strerror(errno
) << std::endl
;
225 waitpid(dts_pid
, &status
, 0);
226 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
227 std::cerr
<< "Child dts process failed" << std::endl
;
230 waitpid(dtb_pid
, &status
, 0);
231 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
232 std::cerr
<< "Child dtb process failed" << std::endl
;
239 void sim_t::make_dtb()
241 const int reset_vec_size
= 8;
243 start_pc
= start_pc
== reg_t(-1) ? get_entry_point() : start_pc
;
245 uint32_t reset_vec
[reset_vec_size
] = {
246 0x297, // auipc t0,0x0
247 0x28593 + (reset_vec_size
* 4 << 20), // addi a1, t0, &dtb
248 0xf1402573, // csrr a0, mhartid
249 get_core(0)->xlen
== 32 ?
250 0x0182a283u
: // lw t0,24(t0)
251 0x0182b283u
, // ld t0,24(t0)
254 (uint32_t) (start_pc
& 0xffffffff),
255 (uint32_t) (start_pc
>> 32)
258 std::vector
<char> rom((char*)reset_vec
, (char*)reset_vec
+ sizeof(reset_vec
));
265 " #address-cells = <2>;\n"
266 " #size-cells = <2>;\n"
267 " compatible = \"ucbbar,spike-bare-dev\";\n"
268 " model = \"ucbbar,spike-bare\";\n"
270 " #address-cells = <1>;\n"
271 " #size-cells = <0>;\n"
272 " timebase-frequency = <" << (CPU_HZ
/INSNS_PER_RTC_TICK
) << ">;\n";
273 for (size_t i
= 0; i
< procs
.size(); i
++) {
274 s
<< " CPU" << i
<< ": cpu@" << i
<< " {\n"
275 " device_type = \"cpu\";\n"
276 " reg = <" << i
<< ">;\n"
277 " status = \"okay\";\n"
278 " compatible = \"riscv\";\n"
279 " riscv,isa = \"" << procs
[i
]->isa_string
<< "\";\n"
280 " mmu-type = \"riscv," << (procs
[i
]->max_xlen
<= 32 ? "sv32" : "sv48") << "\";\n"
281 " clock-frequency = <" << CPU_HZ
<< ">;\n"
282 " CPU" << i
<< "_intc: interrupt-controller {\n"
283 " #interrupt-cells = <1>;\n"
284 " interrupt-controller;\n"
285 " compatible = \"riscv,cpu-intc\";\n"
290 for (auto& m
: mems
) {
292 " memory@" << m
.first
<< " {\n"
293 " device_type = \"memory\";\n"
294 " reg = <0x" << (m
.first
>> 32) << " 0x" << (m
.first
& (uint32_t)-1) <<
295 " 0x" << (m
.second
->size() >> 32) << " 0x" << (m
.second
->size() & (uint32_t)-1) << ">;\n"
299 " #address-cells = <2>;\n"
300 " #size-cells = <2>;\n"
301 " compatible = \"ucbbar,spike-bare-soc\", \"simple-bus\";\n"
303 " clint@" << CLINT_BASE
<< " {\n"
304 " compatible = \"riscv,clint0\";\n"
305 " interrupts-extended = <" << std::dec
;
306 for (size_t i
= 0; i
< procs
.size(); i
++)
307 s
<< "&CPU" << i
<< "_intc 3 &CPU" << i
<< "_intc 7 ";
308 reg_t clintbs
= CLINT_BASE
;
309 reg_t clintsz
= CLINT_SIZE
;
310 s
<< std::hex
<< ">;\n"
311 " reg = <0x" << (clintbs
>> 32) << " 0x" << (clintbs
& (uint32_t)-1) <<
312 " 0x" << (clintsz
>> 32) << " 0x" << (clintsz
& (uint32_t)-1) << ">;\n"
316 " compatible = \"ucb,htif0\";\n"
321 std::string dtb
= dts_compile(dts
);
323 rom
.insert(rom
.end(), dtb
.begin(), dtb
.end());
324 const int align
= 0x1000;
325 rom
.resize((rom
.size() + align
- 1) / align
* align
);
327 boot_rom
.reset(new rom_device_t(rom
));
328 bus
.add_device(DEFAULT_RSTVEC
, boot_rom
.get());
331 char* sim_t::addr_to_mem(reg_t addr
) {
332 auto desc
= bus
.find_device(addr
);
333 if (auto mem
= dynamic_cast<mem_t
*>(desc
.second
))
334 if (addr
- desc
.first
< mem
->size())
335 return mem
->contents() + (addr
- desc
.first
);
351 void sim_t::read_chunk(addr_t taddr
, size_t len
, void* dst
)
354 auto data
= debug_mmu
->load_uint64(taddr
);
355 memcpy(dst
, &data
, sizeof data
);
358 void sim_t::write_chunk(addr_t taddr
, size_t len
, const void* src
)
362 memcpy(&data
, src
, sizeof data
);
363 debug_mmu
->store_uint64(taddr
, data
);