1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
// See LICENSE for license details.
#include "config.h"
#include "htif.h"
#include "rfb.h"
#include "elfloader.h"
#include "platform.h"
#include "byteorder.h"
#include "trap.h"
#include "../riscv/common.h"
#include <algorithm>
#include <assert.h>
#include <vector>
#include <queue>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <getopt.h>
/* Attempt to determine the execution prefix automatically. autoconf
* sets PREFIX, and pconfigure sets __PCONFIGURE__PREFIX. */
#if !defined(PREFIX) && defined(__PCONFIGURE__PREFIX)
# define PREFIX __PCONFIGURE__PREFIX
#endif
#ifndef TARGET_ARCH
# define TARGET_ARCH "riscv64-unknown-elf"
#endif
#ifndef TARGET_DIR
# define TARGET_DIR "/" TARGET_ARCH "/bin/"
#endif
static volatile bool signal_exit = false;
static void handle_signal(int sig)
{
if (sig == SIGABRT || signal_exit) // someone set up us the bomb!
exit(-1);
signal_exit = true;
signal(sig, &handle_signal);
}
htif_t::htif_t()
: mem(this), entry(DRAM_BASE), sig_addr(0), sig_len(0),
tohost_addr(0), fromhost_addr(0), exitcode(0), stopped(false),
syscall_proxy(this)
{
signal(SIGINT, &handle_signal);
signal(SIGTERM, &handle_signal);
signal(SIGABRT, &handle_signal); // we still want to call static destructors
}
htif_t::htif_t(int argc, char** argv) : htif_t()
{
//Set line size as 16 by default.
line_size = 16;
parse_arguments(argc, argv);
register_devices();
}
htif_t::htif_t(const std::vector<std::string>& args) : htif_t()
{
int argc = args.size() + 1;
char * argv[argc];
argv[0] = (char *) "htif";
for (unsigned int i = 0; i < args.size(); i++) {
argv[i+1] = (char *) args[i].c_str();
}
//Set line size as 16 by default.
line_size = 16;
parse_arguments(argc, argv);
register_devices();
}
htif_t::~htif_t()
{
for (auto d : dynamic_devices)
delete d;
}
void htif_t::start()
{
if (!targs.empty() && targs[0] != "none") {
try {
load_program();
} catch (const incompat_xlen & err) {
fprintf(stderr, "Error: cannot execute %d-bit program on RV%d hart\n", err.actual_xlen, err.expected_xlen);
exit(1);
}
}
reset();
}
static void bad_address(const std::string& situation, reg_t addr)
{
std::cerr << "Access exception occurred while " << situation << ":\n";
std::cerr << "Memory address 0x" << std::hex << addr << " is invalid\n";
exit(-1);
}
std::map<std::string, uint64_t> htif_t::load_payload(const std::string& payload, reg_t* entry, reg_t load_offset)
{
std::string path;
if (access(payload.c_str(), F_OK) == 0)
path = payload;
else if (payload.find('/') == std::string::npos)
{
std::string test_path = PREFIX TARGET_DIR + payload;
if (access(test_path.c_str(), F_OK) == 0)
path = test_path;
else
throw std::runtime_error(
"could not open " + payload + "; searched paths:\n" +
"\t. (current directory)\n" +
"\t" + PREFIX TARGET_DIR + " (based on configured --prefix and --with-target)"
);
}
if (path.empty())
throw std::runtime_error(
"could not open " + payload +
" (did you misspell it? If VCS, did you forget +permissive/+permissive-off?)");
// temporarily construct a memory interface that skips writing bytes
// that have already been preloaded through a sideband
class preload_aware_memif_t : public memif_t {
public:
preload_aware_memif_t(htif_t* htif) : memif_t(htif), htif(htif) {}
void write(addr_t taddr, size_t len, const void* src) override
{
if (!htif->is_address_preloaded(taddr, len))
memif_t::write(taddr, len, src);
}
private:
htif_t* htif;
} preload_aware_memif(this);
try {
return load_elf(path.c_str(), &preload_aware_memif, entry, load_offset, expected_xlen);
} catch (mem_trap_t& t) {
bad_address("loading payload " + payload, t.get_tval());
abort();
}
}
void htif_t::load_program()
{
std::map<std::string, uint64_t> symbols = load_payload(targs[0], &entry, load_offset);
if (symbols.count("tohost") && symbols.count("fromhost")) {
tohost_addr = symbols["tohost"];
fromhost_addr = symbols["fromhost"];
} else {
fprintf(stderr, "warning: tohost and fromhost symbols not in ELF; can't communicate with target\n");
}
// detect torture tests so we can print the memory signature at the end
if (symbols.count("begin_signature") && symbols.count("end_signature")) {
sig_addr = symbols["begin_signature"];
sig_len = symbols["end_signature"] - sig_addr;
}
for (auto payload : payloads) {
reg_t dummy_entry;
load_payload(payload, &dummy_entry, 0);
}
class nop_memif_t : public memif_t {
public:
nop_memif_t(htif_t* htif) : memif_t(htif), htif(htif) {}
void read(addr_t UNUSED addr, size_t UNUSED len, void UNUSED *bytes) override {}
void write(addr_t UNUSED taddr, size_t UNUSED len, const void UNUSED *src) override {}
private:
htif_t* htif;
} nop_memif(this);
reg_t nop_entry;
for (auto &s : symbol_elfs) {
std::map<std::string, uint64_t> other_symbols = load_elf(s.c_str(), &nop_memif, &nop_entry,
expected_xlen);
symbols.merge(other_symbols);
}
for (auto i : symbols) {
auto it = addr2symbol.find(i.second);
if ( it == addr2symbol.end())
addr2symbol[i.second] = i.first;
}
return;
}
const char* htif_t::get_symbol(uint64_t addr)
{
auto it = addr2symbol.find(addr);
if(it == addr2symbol.end())
return nullptr;
return it->second.c_str();
}
void htif_t::stop()
{
if (!sig_file.empty() && sig_len) // print final torture test signature
{
std::vector<uint8_t> buf(sig_len);
mem.read(sig_addr, sig_len, buf.data());
std::ofstream sigs(sig_file);
assert(sigs && "can't open signature file!");
sigs << std::setfill('0') << std::hex;
for (addr_t i = 0; i < sig_len; i += line_size)
{
for (addr_t j = line_size; j > 0; j--)
if (i+j <= sig_len)
sigs << std::setw(2) << (uint16_t)buf[i+j-1];
else
sigs << std::setw(2) << (uint16_t)0;
sigs << '\n';
}
sigs.close();
}
stopped = true;
}
void htif_t::clear_chunk(addr_t taddr, size_t len)
{
char zeros[chunk_max_size()];
memset(zeros, 0, chunk_max_size());
for (size_t pos = 0; pos < len; pos += chunk_max_size())
write_chunk(taddr + pos, std::min(len - pos, chunk_max_size()), zeros);
}
int htif_t::run()
{
start();
auto enq_func = [](std::queue<reg_t>* q, uint64_t x) { q->push(x); };
std::queue<reg_t> fromhost_queue;
std::function<void(reg_t)> fromhost_callback =
std::bind(enq_func, &fromhost_queue, std::placeholders::_1);
if (tohost_addr == 0) {
while (!signal_exit)
idle();
}
while (!signal_exit && exitcode == 0)
{
uint64_t tohost;
try {
if ((tohost = from_target(mem.read_uint64(tohost_addr))) != 0)
mem.write_uint64(tohost_addr, target_endian<uint64_t>::zero);
} catch (mem_trap_t& t) {
bad_address("accessing tohost", t.get_tval());
}
try {
if (tohost != 0) {
command_t cmd(mem, tohost, fromhost_callback);
device_list.handle_command(cmd);
} else {
idle();
}
device_list.tick();
} catch (mem_trap_t& t) {
std::stringstream tohost_hex;
tohost_hex << std::hex << tohost;
bad_address("host was accessing memory on behalf of target (tohost = 0x" + tohost_hex.str() + ")", t.get_tval());
}
try {
if (!fromhost_queue.empty() && !mem.read_uint64(fromhost_addr)) {
mem.write_uint64(fromhost_addr, to_target(fromhost_queue.front()));
fromhost_queue.pop();
}
} catch (mem_trap_t& t) {
bad_address("accessing fromhost", t.get_tval());
}
}
stop();
return exit_code();
}
bool htif_t::done()
{
return stopped;
}
int htif_t::exit_code()
{
return exitcode >> 1;
}
void htif_t::parse_arguments(int argc, char ** argv)
{
optind = 0; // reset optind as HTIF may run getopt _after_ others
while (1) {
static struct option long_options[] = { HTIF_LONG_OPTIONS };
int option_index = 0;
int c = getopt_long(argc, argv, "-h", long_options, &option_index);
if (c == -1) break;
retry:
switch (c) {
case 'h': usage(argv[0]);
throw std::invalid_argument("User queried htif_t help text");
case HTIF_LONG_OPTIONS_OPTIND:
if (optarg) dynamic_devices.push_back(new rfb_t(atoi(optarg)));
else dynamic_devices.push_back(new rfb_t);
break;
case HTIF_LONG_OPTIONS_OPTIND + 1:
// [TODO] Remove once disks are supported again
throw std::invalid_argument("--disk/+disk unsupported (use a ramdisk)");
dynamic_devices.push_back(new disk_t(optarg));
break;
case HTIF_LONG_OPTIONS_OPTIND + 2:
sig_file = optarg;
break;
case HTIF_LONG_OPTIONS_OPTIND + 3:
syscall_proxy.set_chroot(optarg);
break;
case HTIF_LONG_OPTIONS_OPTIND + 4:
payloads.push_back(optarg);
break;
case HTIF_LONG_OPTIONS_OPTIND + 5:
line_size = atoi(optarg);
break;
case HTIF_LONG_OPTIONS_OPTIND + 6:
targs.push_back(optarg);
break;
case HTIF_LONG_OPTIONS_OPTIND + 7:
symbol_elfs.push_back(optarg);
break;
case '?':
if (!opterr)
break;
throw std::invalid_argument("Unknown argument (did you mean to enable +permissive parsing?)");
case 1: {
std::string arg = optarg;
if (arg == "+h" || arg == "+help") {
c = 'h';
optarg = nullptr;
}
else if (arg == "+rfb") {
c = HTIF_LONG_OPTIONS_OPTIND;
optarg = nullptr;
}
else if (arg.find("+rfb=") == 0) {
c = HTIF_LONG_OPTIONS_OPTIND;
optarg = optarg + 5;
}
else if (arg.find("+disk=") == 0) {
c = HTIF_LONG_OPTIONS_OPTIND + 1;
optarg = optarg + 6;
}
else if (arg.find("+signature=") == 0) {
c = HTIF_LONG_OPTIONS_OPTIND + 2;
optarg = optarg + 11;
}
else if (arg.find("+chroot=") == 0) {
c = HTIF_LONG_OPTIONS_OPTIND + 3;
optarg = optarg + 8;
}
else if (arg.find("+payload=") == 0) {
c = HTIF_LONG_OPTIONS_OPTIND + 4;
optarg = optarg + 9;
}
else if (arg.find("+signature-granularity=") == 0) {
c = HTIF_LONG_OPTIONS_OPTIND + 5;
optarg = optarg + 23;
}
else if (arg.find("+target-argument=") == 0) {
c = HTIF_LONG_OPTIONS_OPTIND + 6;
optarg = optarg + 17;
}
else if (arg.find("+symbol-elf=") == 0) {
c = HTIF_LONG_OPTIONS_OPTIND + 7;
optarg = optarg + 12;
}
else if (arg.find("+permissive-off") == 0) {
if (opterr)
throw std::invalid_argument("Found +permissive-off when not parsing permissively");
opterr = 1;
break;
}
else if (arg.find("+permissive") == 0) {
if (!opterr)
throw std::invalid_argument("Found +permissive when already parsing permissively");
opterr = 0;
break;
}
else {
if (!opterr)
break;
else {
optind--;
goto done_processing;
}
}
goto retry;
}
}
}
done_processing:
while (optind < argc)
targs.push_back(argv[optind++]);
if (!targs.size()) {
usage(argv[0]);
throw std::invalid_argument("No binary specified (Did you forget it? Did you forget '+permissive-off' if running with +permissive?)");
}
}
void htif_t::register_devices()
{
device_list.register_device(&syscall_proxy);
device_list.register_device(&bcd);
for (auto d : dynamic_devices)
device_list.register_device(d);
}
void htif_t::usage(const char * program_name)
{
printf("Usage: %s [EMULATOR OPTION]... [VERILOG PLUSARG]... [HOST OPTION]... BINARY [TARGET OPTION]...\n ",
program_name);
fputs("\
Run a BINARY on the Rocket Chip emulator.\n\
\n\
Mandatory arguments to long options are mandatory for short options too.\n\
\n\
EMULATOR OPTIONS\n\
Consult emulator.cc if using Verilator or VCS documentation if using VCS\n\
for available options.\n\
EMUALTOR VERILOG PLUSARGS\n\
Consult generated-src*/*.plusArgs for available options\n\
", stdout);
fputs("\n" HTIF_USAGE_OPTIONS, stdout);
}
|