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
|
#include "riscv_model_impl.h"
#include <algorithm>
#include <cassert>
#include <random>
#include <unistd.h>
#include "riscv_callbacks_if.h"
#include "symbol_table.h"
int term_fd = 1; // set during startup
void plat_term_write_impl(char c) {
if (write(term_fd, &c, sizeof(c)) < 0) {
fprintf(stderr, "Unable to write to terminal!\n");
}
}
void ModelImpl::register_callback(callbacks_if *cb) {
if (std::find(m_callbacks.begin(), m_callbacks.end(), cb) == m_callbacks.end()) {
m_callbacks.push_back(cb);
}
}
void ModelImpl::remove_callback(callbacks_if *cb) {
m_callbacks.erase(std::remove(m_callbacks.begin(), m_callbacks.end(), cb), m_callbacks.end());
}
void ModelImpl::call_pre_step_callbacks(bool is_waiting) {
for (auto c : m_callbacks) {
c->pre_step_callback(*this, is_waiting);
}
}
void ModelImpl::call_post_step_callbacks(bool is_waiting) {
for (auto c : m_callbacks) {
c->post_step_callback(*this, is_waiting);
}
}
void ModelImpl::set_enable_experimental_extensions(bool en) {
m_enable_experimental_extensions = en;
}
void ModelImpl::set_reservation_set_size_exp(uint64_t exponent) {
m_reservation_set_addr_mask = ~((1 << exponent) - 1);
}
void ModelImpl::set_reservation_require_exact_addr_match(bool require_exact_addr) {
m_reservation_require_exact_addr = require_exact_addr;
}
void ModelImpl::print_current_exception() {
if (current_exception != nullptr) {
zprint_exception(*current_exception);
}
}
unit ModelImpl::fetch_callback(sbits opcode) {
for (auto c : m_callbacks) {
c->fetch_callback(*this, opcode);
}
return UNIT;
}
unit ModelImpl::mem_write_callback(const char *type, sbits paddr, uint64_t width, lbits value) {
for (auto c : m_callbacks) {
c->mem_write_callback(*this, type, paddr, width, value);
}
return UNIT;
}
unit ModelImpl::mem_read_callback(const char *type, sbits paddr, uint64_t width, lbits value) {
for (auto c : m_callbacks) {
c->mem_read_callback(*this, type, paddr, width, value);
}
return UNIT;
}
unit ModelImpl::mem_exception_callback(sbits paddr, uint64_t num_of_exception) {
for (auto c : m_callbacks) {
c->mem_exception_callback(*this, paddr, num_of_exception);
}
return UNIT;
}
unit ModelImpl::xreg_full_write_callback(const_sail_string abi_name, sbits reg, sbits value) {
for (auto c : m_callbacks) {
c->xreg_full_write_callback(*this, abi_name, reg, value);
}
return UNIT;
}
unit ModelImpl::freg_write_callback(unsigned reg, sbits value) {
for (auto c : m_callbacks) {
c->freg_write_callback(*this, reg, value);
}
return UNIT;
}
unit ModelImpl::csr_full_write_callback(const_sail_string csr_name, unsigned reg, sbits value) {
for (auto c : m_callbacks) {
c->csr_full_write_callback(*this, csr_name, reg, value);
}
return UNIT;
}
unit ModelImpl::csr_full_read_callback(const_sail_string csr_name, unsigned reg, sbits value) {
for (auto c : m_callbacks) {
c->csr_full_read_callback(*this, csr_name, reg, value);
}
return UNIT;
}
unit ModelImpl::vreg_write_callback(unsigned reg, lbits value) {
for (auto c : m_callbacks) {
c->vreg_write_callback(*this, reg, value);
}
return UNIT;
}
unit ModelImpl::pc_write_callback(sbits new_pc) {
for (auto c : m_callbacks) {
c->pc_write_callback(*this, new_pc);
}
return UNIT;
}
unit ModelImpl::redirect_callback(sbits new_pc) {
for (auto c : m_callbacks) {
c->redirect_callback(*this, new_pc);
}
return UNIT;
}
unit ModelImpl::trap_callback(bool is_interrupt, fbits cause) {
for (auto c : m_callbacks) {
c->trap_callback(*this, is_interrupt, cause);
}
return UNIT;
}
unit ModelImpl::xret_callback(bool is_mret) {
for (auto c : m_callbacks) {
c->xret_callback(*this, is_mret);
}
return UNIT;
}
unit ModelImpl::instret_callback(unit) {
for (auto c : m_callbacks) {
c->instret_callback(*this);
}
return UNIT;
}
unit ModelImpl::ptw_start_callback(
uint64_t vpn,
hart::zMemoryAccessTypezIEmem_payloadz5zK access_type,
hart::ztuple_z8z5enumz0zzPrivilegezCz0z5unitz9 privilege
) {
for (auto c : m_callbacks) {
c->ptw_start_callback(*this, vpn, access_type, privilege);
}
return UNIT;
}
unit ModelImpl::ptw_step_callback(int64_t level, sbits pte_addr, uint64_t pte) {
for (auto c : m_callbacks) {
c->ptw_step_callback(*this, level, pte_addr, pte);
}
return UNIT;
}
unit ModelImpl::ptw_success_callback(uint64_t final_ppn, int64_t level) {
for (auto c : m_callbacks) {
c->ptw_success_callback(*this, final_ppn, level);
}
return UNIT;
}
unit ModelImpl::ptw_fail_callback(hart::zPTW_Error error_type, int64_t level, sbits pte_addr) {
for (auto c : m_callbacks) {
c->ptw_fail_callback(*this, error_type, level, pte_addr);
}
return UNIT;
}
unit ModelImpl::tlb_add_callback(hart::zz5vecz8z5unionz0zzoptionzzIRTLB_EntryzzKz9 tlb, uint64_t index) {
for (auto c : m_callbacks) {
c->tlb_add_callback(*this, tlb, index);
}
return UNIT;
}
unit ModelImpl::tlb_flush_begin_callback(unit) {
for (auto c : m_callbacks) {
c->tlb_flush_begin_callback(*this);
}
return UNIT;
}
unit ModelImpl::tlb_flush_callback(uint64_t index) {
for (auto c : m_callbacks) {
c->tlb_flush_callback(*this, index);
}
return UNIT;
}
unit ModelImpl::tlb_flush_end_callback(hart::zz5vecz8z5unionz0zzoptionzzIRTLB_EntryzzKz9 tlb) {
for (auto c : m_callbacks) {
c->tlb_flush_end_callback(*this, tlb);
}
return UNIT;
}
// Provides entropy for the scalar cryptography extension.
mach_bits ModelImpl::plat_get_16_random_bits(unit) {
// This function can be changed to support deterministic sequences of
// pseudo-random bytes. This is useful for testing.
return m_gen64();
}
// Note: Store-Conditionals are allowed to spuriously fail. If you want
// that to happen you can spuriously set `reservation_valid = false`
// either directly in `load_reservation()` or by calling
// `cancel_reservation()`.
unit ModelImpl::load_reservation(sbits addr, uint64_t width) {
m_reservation_addr = addr.bits;
m_reservation = addr.bits & m_reservation_set_addr_mask;
m_reservation_valid = true;
// Ensure the reservation set subsumes the reserved bytes.
assert((width > 0) && (((addr.bits + width - 1) & m_reservation_set_addr_mask) == m_reservation));
return UNIT;
}
bool ModelImpl::match_reservation(sbits addr) {
return m_reservation_valid && (m_reservation_require_exact_addr ? (addr.bits == m_reservation_addr)
: (m_reservation & m_reservation_set_addr_mask) ==
(addr.bits & m_reservation_set_addr_mask));
}
unit ModelImpl::cancel_reservation(unit) {
m_reservation_valid = false;
return UNIT;
}
bool ModelImpl::valid_reservation(unit) {
return m_reservation_valid;
}
unit ModelImpl::plat_term_write(mach_bits s) {
plat_term_write_impl(static_cast<char>(s));
return UNIT;
}
bool ModelImpl::sys_enable_experimental_extensions(unit) {
return m_enable_experimental_extensions;
}
unit ModelImpl::print_string(const_sail_string prefix, const_sail_string msg) {
printf("%s%s\n", prefix, msg);
return UNIT;
}
unit ModelImpl::print_log(const_sail_string s) {
fprintf(trace_log, "%s\n", s);
return UNIT;
}
unit ModelImpl::print_log_instr(const_sail_string s, uint64_t pc) {
auto maybe_symbol = symbolize_address(g_symbols, pc);
if (maybe_symbol.has_value()) {
fprintf(trace_log, "%-80s %s+%" PRIu64 "\n", s, maybe_symbol->second.c_str(), pc - maybe_symbol->first);
} else {
fprintf(trace_log, "%s\n", s);
}
return UNIT;
}
unit ModelImpl::print_step(unit) {
if (m_config_print_step) {
fprintf(trace_log, "\n");
}
return UNIT;
}
bool ModelImpl::get_config_print_instr(unit) {
return m_config_print_instr;
}
bool ModelImpl::get_config_print_clint(unit) {
return m_config_print_clint;
}
bool ModelImpl::get_config_print_exception(unit) {
return m_config_print_exception;
}
bool ModelImpl::get_config_print_interrupt(unit) {
return m_config_print_interrupt;
}
bool ModelImpl::get_config_print_htif(unit) {
return m_config_print_htif;
}
bool ModelImpl::get_config_print_pma(unit) {
return m_config_print_pma;
}
bool ModelImpl::get_config_rvfi(unit) {
return m_config_rvfi;
}
bool ModelImpl::get_config_use_abi_names(unit) {
return m_config_use_abi_names;
}
void ModelImpl::set_config_print_instr(bool on) {
m_config_print_instr = on;
}
void ModelImpl::set_config_print_clint(bool on) {
m_config_print_clint = on;
}
void ModelImpl::set_config_print_exception(bool on) {
m_config_print_exception = on;
}
void ModelImpl::set_config_print_interrupt(bool on) {
m_config_print_interrupt = on;
}
void ModelImpl::set_config_print_htif(bool on) {
m_config_print_htif = on;
}
void ModelImpl::set_config_print_pma(bool on) {
m_config_print_pma = on;
}
void ModelImpl::set_config_rvfi(bool on) {
m_config_rvfi = on;
}
void ModelImpl::set_config_use_abi_names(bool on) {
m_config_use_abi_names = on;
}
void ModelImpl::set_config_print_step(bool on) {
m_config_print_step = on;
}
|