forked from chipsalliance/VeeR-ISS
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTrace.hpp
420 lines (336 loc) · 13.8 KB
/
Trace.hpp
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
// Copyright 2022 Tenstorrent.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <unordered_set>
#include "DecodedInst.hpp"
#include "Hart.hpp"
namespace WdRiscv
{
template <typename URV>
struct TraceRecord
{
public:
TraceRecord(Hart<URV>* hart, const DecodedInst& di)
: hart_(hart), di_(di)
{ }
/// Hart index of trace record. Every hart in the system is
/// assigned an unqiue index from a set of consecutive integers
/// starting with zero.
unsigned hartIndex() const
{ return hart_->sysHartIndex(); }
/// Virtual PC of last executed instruction.
uint64_t virtPc() const
{ return di_.address(); }
/// Physical PC of last executed instruction.
uint64_t physPc() const
{ return di_.physAddress(); }
/// Opcode of last executed instruction.
uint32_t instruction() const
{ return di_.inst(); }
/// PC of the next instruction.
uint64_t nextVirtPc() const
{ return hart_->pc(); }
/// Return the operand type of the ith operand. Returns None if
/// i is out of bounds.
OperandType ithOperandType(unsigned i) const
{ return di_.ithOperandType(i); }
/// Return the mode of the ith operand or None if i is out of
/// bounds. Object must be valid.
OperandMode ithOperandMode(unsigned i) const
{ return di_.ithOperandMode(i); }
/// Return the ith operands or zero if i is out of bounds. For exmaple, if
/// decode insruction is "addi x3, x4, 10" then the 0th operand would be 3
/// and the second operands would be 10.
uint32_t ithOperand(unsigned i) const
{ return di_.ithOperand(i); }
/// Return the size with which the immediate data is left shifted
unsigned immediateShiftSize() const
{ return di_.immediateShiftSize(); }
/// Return the rounding mode associated with a floating point instruction
uint32_t roundingMode() const
{ return di_.roundingMode(); }
/// Privilege mode before last executed instruction.
PrivilegeMode privMode() const
{ return hart_->lastPrivMode(); }
/// Privilege mode after last executed instruction.
PrivilegeMode nextPrivMode() const
{ return hart_->privilegeMode(); }
/// Trap vector mode after last executed instruction
TrapVectorMode nextTvecMode() const
{
URV tvec = peekCsr(CsrNumber::MTVEC);
auto tvm = TrapVectorMode{tvec&3};
return tvm;
}
/// True if last executed instruction encountered a trap.
bool hasTrap() const
{ return hart_->lastInstructionTrapped(); }
/// True if last executed instruction encountered a trap and
/// set cause to interrupt/exception cause.
bool hasTrap(URV& cause) const
{
bool trapped = hasTrap();
if (trapped)
{
if (nextPrivMode() == PrivilegeMode::Machine)
cause = hart_->peekCsr(CsrNumber::MCAUSE);
else if (nextPrivMode() == PrivilegeMode::Supervisor)
cause = hart_->peekCsr(CsrNumber::SCAUSE);
}
return trapped;
}
/// True if traget program finished.
bool hasStop() const
{ return hart_->hasTargetProgramFinished(); }
/// Return size of data of last load/store/amo instruction or zero
/// if last instrcution was not load/store/amo. Set virtAddr and
/// physAddr to the corresponding addresses if load/store/amo.
unsigned loadSoreAddr(uint64_t& virtAddr, uint64_t& physAddr)
{ return hart_->lastLdStAddress(virtAddr, physAddr); }
/// Return true if record is for a load instruction.
bool isLoad() const
{ return di_.isLoad(); }
/// Return true if record is for a store instruction.
bool isStore() const
{ return di_.isStore(); }
/// Return true if record is for an AMO instruction.
bool isAmo() const
{ return di_.isAmo(); }
/// Return true if record is for a branch instruction.
bool isBranch() const
{ return di_.isBranch(); }
/// Return true if record is for a conditional branch instruction.
bool isConditionalBranch() const
{ return di_.isConditionalBranch(); }
/// Return true if this is a branch instruction where the target
/// address is in a register.
bool isBranchToRegister() const
{ return di_.isBranchToRegister(); }
/// Return true if last instruction was a branch and (if it was)
/// whether it was taken.
std::pair<bool, bool> lastBranchTaken() const
{ return std::make_pair(isBranch(), hart_->lastBranchTaken()); }
/// Return true if record is for a floating point instruction.
bool isFp() const
{ return di_.isFp(); }
/// Return true if record is for a vector instruction.
bool isVector() const
{ return di_.isVector(); }
/// Return true if record is for a multiply instruction.
bool isMultiply() const
{ return di_.isVector(); }
/// Return true if record is for a divide/remainder instruction.
bool isDivide() const
{ return di_.isDivide(); }
/// Return the RISCV ISA extension of the instruction of this record.
RvExtension extension() const
{ return di_.extension(); }
/// Return the RISCV instruction format of the instruction of this record.
RvFormat format() const
{ return di_.format(); }
/// Return the instruction id of the instruction of this record.
InstId instId() const
{ return di_.instId(); }
/// Return the instruction name of the instruction of this record.
std::string name() const
{ return di_.name(); }
/// Return currently configured element width.
ElementWidth elemWidth() const
{ return hart_->elemWidth(); }
/// Return currently configured group multiplier.
GroupMultiplier groupMultiplier() const
{ return hart_->groupMultiplier(); }
/// Return the paging mode of the last executed instruction.
VirtMem::Mode pageMode() const
{ return hart_->lastPageMode(); }
/// Return the paging mode after last executed instruction.
VirtMem::Mode nextPageMode() const
{ return hart_->pageMode(); }
/// Return the VS paging mode of the last executed instruction.
VirtMem::Mode vsMode() const
{ return hart_->lastVsPageMode(); }
/// Return the 2nd stage paging mode after last executed instruction.
VirtMem::Mode pageModeStage2() const
{ return hart_->lastPageModeStage2(); }
/// Return CSR value after last executed instruction.
bool peekCsr(CsrNumber csr, URV& val) const
{ return hart_->peekCsr(csr, val); }
/// Return CSR field value after last executed instruction.
bool peekCsr(CsrNumber csr, std::string_view field, URV& val) const
{ return hart_->peekCsr(csr, field, val); }
/// Return the number of page table walks of the last
/// executed instruction
unsigned getNumPageTableWalks(bool instr) const
{ return hart_->getNumPageTableWalks(instr); }
/// Return the page table walk addresses for load/store/fetch of last executed instruction.
/// Will be empty if there was no walk.
void getPageTableWalkAddresses(bool instr, unsigned ix,
std::vector<uint64_t>& addrs) const
{ hart_->getPageTableWalkAddresses(instr, ix, addrs); }
/// Return the page table walk entries for load/store/fetch of last executed instruction.
/// Will be empty if there was no walk.
void getPageTableWalkEntries(bool instr, unsigned ix,
std::vector<uint64_t>& ptes) const
{ hart_->getPageTableWalkEntries(instr, ix, ptes); }
/// Return the entire page table walk for load/store/fetch of last executed instruction.
/// Will be empty if there was no walk.
void getPageTableWalkEntries(bool instr, std::vector<std::vector<VirtMem::WalkEntry>>& walks) const
{ hart_->getPageTableWalkEntries(instr, walks); }
bool peekIntReg(unsigned i, URV& value) const
{ return hart_->peekIntReg(i, value); }
bool peekFpReg(unsigned i, uint64_t& value) const
{ return hart_->peekFpReg(i, value); }
bool peekCsReg(CsrNumber number, URV& value) const
{ return hart_->peekCsr(number, value); }
void peekMemory(uint64_t addr, uint64_t &val, bool usePma) {
hart_->peekMemory(addr, val, usePma);
};
/// TODO: modified regs
/// Return the list of CSR address-value pairs modified after last executed instruction.
using CVP = std::pair<URV, URV>; // CSR-value pair
void getModifiedCsrs(std::vector<CVP>& cvps) const
{
URV value;
cvps.clear();
std::vector<CsrNumber> csrs;
std::vector<unsigned> triggers;
hart_->csRegs().getLastWrittenRegs(csrs, triggers);
cvps.reserve(csrs.size() + triggers.size());
/// Collect non-trigger CSRs and their values.
for (CsrNumber csr : csrs)
{
if (not hart_->peekCsr(csr, value, false))
continue;
if (csr >= CsrNumber::TDATA1 and csr <= CsrNumber::TINFO)
continue; // Debug trigger values collected below.
cvps.push_back(CVP(URV(csr), value));
}
/// Collect trigger CSRs and their values. A synthetic CSR number is used encoding
/// the trigger number and the trigger component.
for (unsigned trigger : triggers)
{
// Components of trigger that changed.
std::vector<std::pair<CsrNumber, uint64_t>> trigChanges;
hart_->getTriggerChange(trigger, trigChanges);
for (auto& pair : trigChanges)
{
auto csrn = pair.first;
auto val = pair.second;
URV ecsr = (trigger << 16) | URV(csrn);
cvps.push_back(CVP(ecsr, URV(val)));
}
}
}
using SVP = std::pair<URV, uint64_t>; // select-value pair
void getImsicChanges(std::vector<SVP>& mcvps,
std::vector<SVP>& scvps,
std::vector<std::vector<SVP>>& gcvps,
std::vector<unsigned>& minterrupts,
std::vector<unsigned>& sinterrupts,
std::vector<std::vector<unsigned>>& ginterrupts) const
{
if (not hart_->imsic())
return;
const auto& imsic = (*hart_->imsic());
std::vector<std::pair<unsigned, unsigned>> mselects, sselects;
std::vector<std::vector<std::pair<unsigned, unsigned>>> gselects;
imsic.fileTraces(mselects, sselects, gselects, minterrupts, sinterrupts, ginterrupts);
bool ok; unsigned int value;
for (auto [select, size] : mselects)
{
if (size == 4)
ok = imsic.template readMireg<uint32_t>(select, value);
else
ok = imsic.readMireg(select, value);
if (ok)
mcvps.emplace_back(select, value);
}
for (auto [select, size] : sselects)
{
if (size == 4)
ok = imsic.template readSireg<uint32_t>(false, 0 /* guest */, select, value);
else
ok = imsic.readSireg(false, 0 /* guest */, select, value);
if (ok)
scvps.emplace_back(select, value);
}
for (unsigned i = 0; i < gselects.size(); ++i)
{
std::vector<SVP> tmp;
for (auto [select, size] : gselects.at(i))
{
if (size == 4)
ok = imsic.template readSireg<uint32_t>(true, i, select, value);
else
ok = imsic.readSireg(true, i, select, value);
if (ok)
tmp.emplace_back(select, value);
}
gcvps.push_back(std::move(tmp));
}
}
/// TODO: add support for vector ld/st
unsigned lastLdStAddress(uint64_t& virtAddr, uint64_t& physAddr) const
{ return hart_->lastLdStAddress(virtAddr, physAddr); /* this returns the size of the last ld/st*/ }
unsigned lastStVal(uint64_t& value) const
{
uint64_t addr;
return hart_->lastStore(addr, value); /* this returns the size of the last ld/st */
}
bool misalignedLdSt(bool& misal) const
{ return hart_->misalignedLdSt(misal); }
/// Return true if associated hart is holding a reservation
/// (obtained thru an LR instruction).
bool hasLr() const
{ return hart_->hasLr(); }
/// Return the reason for loss of reservation of the associated hart.
CancelLrCause cancelLrCause() const
{ return hart_->cancelLrCause(); }
void getPmpsAccessed(std::vector<PmpManager::PmpTrace>& pmps) const
{ hart_->getPmpsAccessed(pmps); }
void getPmasAccessed(std::vector<PmaManager::PmaTrace>& pmas) const
{ hart_->getPmasAccessed(pmas); }
bool matchMultiplePmp(uint64_t addr) const
{ return hart_->pmpManager().matchMultiplePmp(addr); }
bool matchMultiplePma(uint64_t addr) const
{ return hart_->pmaManager().matchMultiplePma(addr); }
/// Return the number of pages accessed by a vector ld/st instruction.
unsigned numVecPagesAccessed() const
{
if (not isVector())
return 0;
std::unordered_set<uint64_t> pages;
auto& info = hart_->getLastVectorMemory();
unsigned elemSize = info.elemSize_;
if (elemSize != 0)
for (auto& einfo : info.elems_)
{
const auto& addr = einfo.va_;
const auto& skipped = einfo.skip_;
if (not skipped)
{
unsigned page = hart_->virtMem().pageNumber(addr);
pages.emplace(page);
}
}
return pages.size();
}
bool virtualMode() const
{ return hart_->lastVirtMode(); }
bool nextVirtualMode() const
{ return hart_->virtMode(); }
const Hart<URV>* hart_ = nullptr;
const DecodedInst& di_;
};
}