Remove legacy HTIF; implement HTIF directly
[riscv-isa-sim.git] / riscv / sim.cc
1 // See LICENSE for license details.
2
3 #include "sim.h"
4 #include "mmu.h"
5 #include "gdbserver.h"
6 #include <map>
7 #include <iostream>
8 #include <sstream>
9 #include <climits>
10 #include <cstdlib>
11 #include <cassert>
12 #include <signal.h>
13
14 volatile bool ctrlc_pressed = false;
15 static void handle_signal(int sig)
16 {
17 if (ctrlc_pressed)
18 exit(-1);
19 ctrlc_pressed = true;
20 signal(sig, &handle_signal);
21 }
22
23 sim_t::sim_t(const char* isa, size_t nprocs, size_t mem_mb, bool halted,
24 const std::vector<std::string>& args)
25 : htif_t(args), procs(std::max(nprocs, size_t(1))),
26 current_step(0), current_proc(0), debug(false), gdbserver(NULL)
27 {
28 signal(SIGINT, &handle_signal);
29 // allocate target machine's memory, shrinking it as necessary
30 // until the allocation succeeds
31 size_t memsz0 = (size_t)mem_mb << 20;
32 size_t quantum = 1L << 20;
33 if (memsz0 == 0)
34 memsz0 = (size_t)((sizeof(size_t) == 8 ? 4096 : 2048) - 256) << 20;
35
36 memsz = memsz0;
37 while ((mem = (char*)calloc(1, memsz)) == NULL)
38 memsz = (size_t)(memsz*0.9)/quantum*quantum;
39
40 if (memsz != memsz0)
41 fprintf(stderr, "warning: only got %lu bytes of target mem (wanted %lu)\n",
42 (unsigned long)memsz, (unsigned long)memsz0);
43
44 bus.add_device(DEBUG_START, &debug_module);
45
46 debug_mmu = new mmu_t(this, NULL);
47
48 for (size_t i = 0; i < procs.size(); i++) {
49 procs[i] = new processor_t(isa, this, i, halted);
50 }
51
52 rtc.reset(new rtc_t(procs));
53 make_config_string();
54 }
55
56 sim_t::~sim_t()
57 {
58 for (size_t i = 0; i < procs.size(); i++)
59 delete procs[i];
60 delete debug_mmu;
61 free(mem);
62 }
63
64 void sim_thread_main(void* arg)
65 {
66 ((sim_t*)arg)->main();
67 }
68
69 void sim_t::main()
70 {
71 if (!debug && log)
72 set_procs_debug(true);
73
74 while (!done())
75 {
76 if (debug || ctrlc_pressed)
77 interactive();
78 else
79 step(INTERLEAVE);
80 if (gdbserver) {
81 gdbserver->handle();
82 }
83 }
84 }
85
86 int sim_t::run()
87 {
88 host = context_t::current();
89 target.init(sim_thread_main, this);
90 return htif_t::run();
91 }
92
93 void sim_t::step(size_t n)
94 {
95 for (size_t i = 0, steps = 0; i < n; i += steps)
96 {
97 steps = std::min(n - i, INTERLEAVE - current_step);
98 procs[current_proc]->step(steps);
99
100 current_step += steps;
101 if (current_step == INTERLEAVE)
102 {
103 current_step = 0;
104 procs[current_proc]->yield_load_reservation();
105 if (++current_proc == procs.size()) {
106 current_proc = 0;
107 rtc->increment(INTERLEAVE / INSNS_PER_RTC_TICK);
108 }
109
110 host->switch_to();
111 }
112 }
113 }
114
115 void sim_t::set_debug(bool value)
116 {
117 debug = value;
118 }
119
120 void sim_t::set_log(bool value)
121 {
122 log = value;
123 }
124
125 void sim_t::set_histogram(bool value)
126 {
127 histogram_enabled = value;
128 for (size_t i = 0; i < procs.size(); i++) {
129 procs[i]->set_histogram(histogram_enabled);
130 }
131 }
132
133 void sim_t::set_procs_debug(bool value)
134 {
135 for (size_t i=0; i< procs.size(); i++)
136 procs[i]->set_debug(value);
137 }
138
139 bool sim_t::mmio_load(reg_t addr, size_t len, uint8_t* bytes)
140 {
141 if (addr + len < addr)
142 return false;
143 return bus.load(addr, len, bytes);
144 }
145
146 bool sim_t::mmio_store(reg_t addr, size_t len, const uint8_t* bytes)
147 {
148 if (addr + len < addr)
149 return false;
150 return bus.store(addr, len, bytes);
151 }
152
153 void sim_t::make_config_string()
154 {
155 reg_t rtc_addr = EXT_IO_BASE;
156 bus.add_device(rtc_addr, rtc.get());
157
158 const int align = 0x1000;
159 reg_t cpu_addr = rtc_addr + ((rtc->size() - 1) / align + 1) * align;
160 reg_t cpu_size = align;
161
162 uint32_t reset_vec[8] = {
163 0x297 + DRAM_BASE - DEFAULT_RSTVEC, // reset vector
164 0x00028067, // jump straight to DRAM_BASE
165 0x00000000, // reserved
166 0, // config string pointer
167 0, 0, 0, 0 // trap vector
168 };
169 reset_vec[3] = DEFAULT_RSTVEC + sizeof(reset_vec); // config string pointer
170
171 std::vector<char> rom((char*)reset_vec, (char*)reset_vec + sizeof(reset_vec));
172
173 std::stringstream s;
174 s << std::hex <<
175 "platform {\n"
176 " vendor ucb;\n"
177 " arch spike;\n"
178 "};\n"
179 "rtc {\n"
180 " addr 0x" << rtc_addr << ";\n"
181 "};\n"
182 "ram {\n"
183 " 0 {\n"
184 " addr 0x" << DRAM_BASE << ";\n"
185 " size 0x" << memsz << ";\n"
186 " };\n"
187 "};\n"
188 "core {\n";
189 for (size_t i = 0; i < procs.size(); i++) {
190 s <<
191 " " << i << " {\n"
192 " " << "0 {\n" << // hart 0 on core i
193 " isa " << procs[i]->isa_string << ";\n"
194 " timecmp 0x" << (rtc_addr + 8*(1+i)) << ";\n"
195 " ipi 0x" << cpu_addr << ";\n"
196 " };\n"
197 " };\n";
198 bus.add_device(cpu_addr, procs[i]);
199 cpu_addr += cpu_size;
200 }
201 s << "};\n";
202
203 config_string = s.str();
204 rom.insert(rom.end(), config_string.begin(), config_string.end());
205 rom.resize((rom.size() / align + 1) * align);
206
207 boot_rom.reset(new rom_device_t(rom));
208 bus.add_device(DEFAULT_RSTVEC, boot_rom.get());
209 }
210
211 // htif
212
213 void sim_t::idle()
214 {
215 target.switch_to();
216 }
217
218 void sim_t::read_chunk(addr_t taddr, size_t len, void* dst)
219 {
220 assert(len == 8);
221 auto data = debug_mmu->load_uint64(taddr);
222 memcpy(dst, &data, sizeof data);
223 }
224
225 void sim_t::write_chunk(addr_t taddr, size_t len, const void* src)
226 {
227 assert(len == 8);
228 uint64_t data;
229 memcpy(&data, src, sizeof data);
230 debug_mmu->store_uint64(taddr, data);
231 }