forked from chipsalliance/VeeR-ISS
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSystem.cpp
1526 lines (1274 loc) · 38.1 KB
/
System.cpp
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 Western Digital Corporation or its affiliates.
//
// 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.
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>
#include <boost/algorithm/string.hpp>
#include "Filesystem.hpp"
#include "Hart.hpp"
#include "Core.hpp"
#include "SparseMem.hpp"
#include "System.hpp"
#include "Mcm.hpp"
#include "PerfApi.hpp"
#include "Uart8250.hpp"
#include "Uartsf.hpp"
#include "pci/virtio/Blk.hpp"
using namespace WdRiscv;
inline bool
isPowerOf2(uint64_t x)
{
return x != 0 and (x & (x-1)) == 0;
}
template <typename URV>
System<URV>::System(unsigned coreCount, unsigned hartsPerCore,
unsigned hartIdOffset, size_t memSize,
size_t pageSize)
: hartCount_(coreCount * hartsPerCore), hartsPerCore_(hartsPerCore),
imsicMgr_((coreCount * hartsPerCore), pageSize), time_(0)
{
cores_.resize(coreCount);
memory_ = std::make_unique<Memory>(memSize, pageSize);
syscall_ = std::make_unique<Syscall<URV>>(sysHarts_, memSize);
sparseMem_ = nullptr;
Memory& mem = *memory_;
mem.setHartCount(hartCount_);
for (unsigned ix = 0; ix < coreCount; ++ix)
{
URV coreHartId = ix * hartIdOffset;
cores_.at(ix) = std::make_shared<CoreClass>(coreHartId, ix, hartsPerCore, mem, *syscall_, time_);
// Maintain a vector of all the harts in the system. Map hart-id to index
// of hart in system.
auto core = cores_.at(ix);
for (unsigned i = 0; i < hartsPerCore; ++i)
{
auto hart = core->ithHart(i);
sysHarts_.push_back(hart);
URV hartId = coreHartId + i;
unsigned hartIx = ix*hartsPerCore + i;
hartIdToIndex_[hartId] = hartIx;
}
}
#ifdef MEM_CALLBACKS
sparseMem_ = std::make_unique<SparseMem>();
auto readf = [this](uint64_t addr, unsigned size, uint64_t& value) -> bool {
return sparseMem_->read(addr, size, value); };
auto writef = [this](uint64_t addr, unsigned size, uint64_t value) -> bool {
return sparseMem_->write(addr, size, value); };
auto initf = [this](uint64_t addr, const std::span<uint8_t> buffer) -> bool {
return sparseMem_->initializePage(addr, buffer); };
mem.defineReadMemoryCallback(readf);
mem.defineWriteMemoryCallback(writef);
mem.defineInitPageCallback(initf);
#endif
}
template <typename URV>
bool
System<URV>::defineUart(const std::string& type, uint64_t addr, uint64_t size)
{
std::shared_ptr<IoDevice> dev;
if (type == "uartsf")
dev = std::make_shared<Uartsf>(addr, size);
else if (type == "uart8250")
dev = std::make_shared<Uart8250>(addr, size);
else
{
std::cerr << "System::defineUart: Invalid uadrt type: " << type << "\n";
return false;
}
memory_->registerIoDevice(dev);
ioDevs_.push_back(std::move(dev));
return true;
}
template <typename URV>
System<URV>::~System()
{
// Final MCM checks
if (mcm_)
for (const auto& hartPtr : sysHarts_)
mcm_->finalChecks(*hartPtr);
// Write back binary files were marked for update.
for (auto bf : binaryFiles_)
{
auto path = std::get<0>(bf);
uint64_t addr = std::get<1>(bf);
uint64_t size = std::get<2>(bf);
std::cerr << "Updating " << path << " from addr: 0x" << std::hex << addr
<< std::dec << " size: " << size << '\n';
FILE* file = fopen(path.c_str(), "w");
if (not file)
{
std::cerr << "Failed to open " << path << " for update\n";
continue;
}
for (uint64_t i = 0; i < size; ++i)
{
uint8_t byte = 0;
memory_->peek(addr + i, byte, false);
fputc(byte, file);
}
fclose(file);
}
}
template <typename URV>
void
System<URV>::checkUnmappedElf(bool flag)
{
if (memory_)
memory_->checkUnmappedElf(flag);
}
template <typename URV>
bool
System<URV>::writeAccessedMemory(const std::string& path) const
{
if (not sparseMem_)
return false;
return sparseMem_->writeHexFile(path);
}
template <typename URV>
bool
System<URV>::loadElfFiles(const std::vector<std::string>& files, bool raw, bool verbose)
{
unsigned registerWidth = sizeof(URV)*8;
uint64_t end = 0, entry = 0;
uint64_t gp = 0, tp = 0; // gp: global-pointer, tp: thread-pointer
unsigned errors = 0;
ElfSymbol sym;
for (const auto& file : files)
{
if (verbose)
std::cerr << "Loading ELF file " << file << '\n';
uint64_t end0 = 0, entry0 = 0;
if (not memory_->loadElfFile(file, registerWidth, entry0, end0))
errors++;
else
{
if (not entry)
entry = entry0;
if (memory_->findElfSymbol("_end", sym)) // For newlib/linux emulation.
end = std::max(end, sym.addr_);
else
end = std::max(end, end0);
if (not gp and memory_->findElfSymbol("__global_pointer$", sym))
gp = sym.addr_;
if (not tp and memory_->findElfSection(".tdata", sym))
tp = sym.addr_;
}
}
for (const auto& hart : sysHarts_)
{
if (not toHostSym_.empty() and memory_->findElfSymbol(toHostSym_, sym))
hart->setToHostAddress(sym.addr_);
if (not fromHostSym_.empty() and memory_->findElfSymbol(fromHostSym_, sym))
hart->setFromHostAddress(sym.addr_, true);
if (not consoleIoSym_.empty() and memory_->findElfSymbol(consoleIoSym_, sym))
hart->setConsoleIo(URV(sym.addr_));
if (verbose)
std::cerr << "Setting program break to 0x" << std::hex << end << std::dec << '\n';
hart->setTargetProgramBreak(end);
if (not raw)
{
if (not hart->peekIntReg(RegGp) and gp)
{
if (verbose)
std::cerr << "Setting register gp to 0x" << std::hex << gp << std::dec << '\n';
hart->pokeIntReg(RegGp, URV(gp));
}
if (not hart->peekIntReg(RegTp) and tp)
{
if (verbose)
std::cerr << "Setting register tp to 0x" << std::hex << tp << std::dec << '\n';
hart->pokeIntReg(RegTp, URV(tp));
}
if (entry)
{
if (verbose)
std::cerr << "Setting PC to 0x" << std::hex << entry << std::dec << '\n';
hart->pokePc(URV(entry));
}
}
}
return errors == 0;
}
template <typename URV>
bool
System<URV>::loadHexFiles(const std::vector<std::string>& files, bool verbose)
{
unsigned errors = 0;
for (const auto& file : files)
{
if (verbose)
std::cerr << "Loading HEX file " << file << '\n';
if (not memory_->loadHexFile(file))
errors++;
}
return errors == 0;
}
static bool
binaryFileParams(std::string spec, uint64_t defOffset, std::string& filename, uint64_t &offset, bool &update)
{
using std::cerr;
// Split filespec around colons. Spec format: <file>,
// <file>:<offset>, or <file>:<offset>:u
std::vector<std::string> parts;
boost::split(parts, spec, boost::is_any_of(":"));
filename = parts.at(0);
offset = defOffset;
update = false;
if (parts.empty())
{
std::cerr << "Error: Empty binary file name\n";
return false;
}
filename = parts.at(0);
if (parts.size() > 1)
{
std::string offsStr = parts.at(1);
if (offsStr.empty())
cerr << "Warning: Empty binary file offset: " << spec << '\n';
else
{
char* tail = nullptr;
offset = strtoull(offsStr.c_str(), &tail, 0);
if (tail and *tail)
{
cerr << "Error: Invalid binary file offset: " << spec << '\n';
return false;
}
}
}
else
cerr << "Binary file " << filename << " does not have an address, will use address 0x"
<< std::hex << offset << std::dec << '\n';
if (parts.size() > 2)
{
if (parts.at(2) != "u")
{
cerr << "Error: Invalid binary file attribute: " << spec << '\n';
return false;
}
update = true;
}
return true;
}
template <typename URV>
bool
System<URV>::loadBinaryFiles(const std::vector<std::string>& fileSpecs,
uint64_t defOffset, bool verbose)
{
using std::cerr;
unsigned errors = 0;
for (const auto& spec : fileSpecs)
{
std::string filename;
uint64_t offset;
bool update;
if (!binaryFileParams(spec, defOffset, filename, offset, update))
{
errors++;
continue;
}
if (verbose)
cerr << "Loading binary " << filename << " at address 0x" << std::hex
<< offset << std::dec << '\n';
if (not memory_->loadBinaryFile(filename, offset))
{
errors++;
continue;
}
if (update)
{
uint64_t size = Filesystem::file_size(filename);
BinaryFile bf = { filename, offset, size };
binaryFiles_.push_back(bf);
}
}
return errors == 0;
}
#ifdef LZ4_COMPRESS
template <typename URV>
bool
System<URV>::loadLz4Files(const std::vector<std::string>& fileSpecs,
uint64_t defOffset, bool verbose)
{
using std::cerr;
unsigned errors = 0;
for (const auto& spec : fileSpecs)
{
std::string filename;
uint64_t offset;
bool update;
if (!binaryFileParams(spec, defOffset, filename, offset, update))
{
errors++;
continue;
}
if (update)
{
cerr << "Updating not supported on lz4 files, ignoring " << filename << '\n';
errors++;
continue;
}
if (verbose)
cerr << "Loading lz4 compressed file " << filename << " at address 0x" << std::hex
<< offset << std::dec << '\n';
if (not memory_->loadLz4File(filename, offset))
{
errors++;
continue;
}
if (update)
{
uint64_t size = Filesystem::file_size(filename);
BinaryFile bf = { filename, offset, size };
binaryFiles_.push_back(bf);
}
}
return errors == 0;
}
#endif
static
bool
saveUsedMemBlocks(const std::string& filename,
std::vector<std::pair<uint64_t, uint64_t>>& blocks)
{
std::ofstream ofs(filename, std::ios::trunc);
if (not ofs)
{
std::cerr << "saveUsedMemBlocks failed - cannot open "
<< filename << " for write\n";
return false;
}
for (auto& it: blocks)
ofs << it.first << " " << it.second << "\n";
return true;
}
static
bool
saveTime(const std::string& filename, uint64_t time)
{
std::ofstream ofs(filename, std::ios::trunc);
if (not ofs)
{
std::cerr << "saveTime failed - cannot open "
<< filename << " for write\n";
return false;
}
ofs << std::dec << time << "\n";
return true;
}
template <typename URV>
bool
System<URV>::saveSnapshot(const std::string& dir)
{
Filesystem::path dirPath = dir;
if (not Filesystem::is_directory(dirPath))
if (not Filesystem::create_directories(dirPath))
{
std::cerr << "Error: Failed to create snapshot directory " << dir << '\n';
return false;
}
uint64_t minSp = ~uint64_t(0);
for (auto hartPtr : sysHarts_)
{
std::string name = "registers";
if (hartCount_ > 1)
name += std::to_string(hartPtr->sysHartIndex());
Filesystem::path regPath = dirPath / name;
if (not hartPtr->saveSnapshotRegs(regPath.string()))
return false;
URV sp = 0;
if (not hartPtr->peekIntReg(IntRegNumber::RegSp, sp))
assert(0);
minSp = std::min(minSp, uint64_t(sp));
}
auto& hart0 = *ithHart(0);
auto& syscall = hart0.getSyscall();
Filesystem::path usedBlocksPath = dirPath / "usedblocks";
std::vector<std::pair<uint64_t,uint64_t>> usedBlocks;
if (sparseMem_)
sparseMem_->getUsedBlocks(usedBlocks);
else
syscall.getUsedMemBlocks(minSp, usedBlocks);
if (not saveUsedMemBlocks(usedBlocksPath.string(), usedBlocks))
return false;
Filesystem::path timePath = dirPath / "time";
if (not saveTime(timePath.string(), time_))
return false;
Filesystem::path memPath = dirPath / "memory";
if (not memory_->saveSnapshot(memPath.string(), usedBlocks))
return false;
Filesystem::path fdPath = dirPath / "fd";
if (not syscall.saveFileDescriptors(fdPath.string()))
return false;
Filesystem::path mmapPath = dirPath / "mmap";
if (not syscall.saveMmap(mmapPath.string()))
return false;
Filesystem::path dtracePath = dirPath / "data-lines";
if (not memory_->saveDataAddressTrace(dtracePath))
return false;
Filesystem::path itracePath = dirPath / "instr-lines";
if (not memory_->saveInstructionAddressTrace(itracePath))
return false;
Filesystem::path branchPath = dirPath / "branch-trace";
if (not hart0.saveBranchTrace(branchPath))
return false;
Filesystem::path imsicPath = dirPath / "imsic";
if (not imsicMgr_.saveSnapshot(imsicPath))
return false;
return true;
}
static
bool
loadUsedMemBlocks(const std::string& filename,
std::vector<std::pair<uint64_t, uint64_t>>& blocks)
{
blocks.clear();
std::ifstream ifs(filename);
if (not ifs)
{
std::cerr << "loadUsedMemBlocks failed - cannot open "
<< filename << " for read\n";
return false;
}
std::string line;
while (std::getline(ifs, line))
{
std::istringstream iss(line);
uint64_t addr, length;
iss >> addr;
iss >> length;
blocks.emplace_back(addr, length);
}
return true;
}
static
bool
loadTime(const std::string& filename, uint64_t& time)
{
std::ifstream ifs(filename);
if (not ifs)
{
std::cerr << "loadTime failed - cannot open "
<< filename << " for read\n";
return false;
}
std::string line;
std::getline(ifs, line);
uint64_t val = strtoull(line.c_str(), nullptr, 0);
time = val;
return true;
}
template <typename URV>
bool
System<URV>::configImsic(uint64_t mbase, uint64_t mstride,
uint64_t sbase, uint64_t sstride,
unsigned guests, const std::vector<unsigned>& idsVec,
const std::vector<unsigned>& tmVec, // Threshold masks
bool trace)
{
using std::cerr;
size_t ps = pageSize();
if ((mbase % ps) != 0)
{
cerr << "Error: IMISC mbase (0x" << std::hex << mbase << ") is not"
<< " a multiple of page size (0x" << ps << ")\n" << std::dec;
return false;
}
if (mstride == 0)
{
cerr << "Error: IMSIC mstride must not be zero.\n";
return false;
}
if ((mstride % ps) != 0)
{
cerr << "Error: IMISC mstride (0x" << std::hex << mstride << ") is not"
<< " a multiple of page size (0x" << ps << ")\n" << std::dec;
return false;
}
if (sstride)
{
if ((sbase % ps) != 0)
{
cerr << "Error: IMISC sbase (0x" << std::hex << sbase << ") is not"
<< " a multiple of page size (0x" << ps << ")\n" << std::dec;
return false;
}
if ((sstride % ps) != 0)
{
cerr << "Error: IMISC sstride (0x" << std::hex << sstride << ") is not"
<< " a multiple of page size (0x" << ps << ")\n" << std::dec;
return false;
}
}
if (guests and sstride < (guests + 1)*ps)
{
cerr << "Error: IMISC supervisor stride (0x" << std::hex << sstride << ") is"
<< " too small for configured guests (" << std::dec << guests << ").\n";
return false;
}
if (mstride and sstride)
{
unsigned hc = hartCount();
uint64_t mend = mbase + hc*mstride, send = sbase + hc*sstride;
if ((sbase > mbase and sbase < mend) or
(send > mbase and send < mend))
{
cerr << "Error: IMSIC machine file address range overlaps that of supervisor.\n";
return false;
}
}
if (idsVec.size() != 3)
{
cerr << "Error: IMSIC interrupt-ids array size (" << idsVec.size() << ") is "
<< "invalid -- Expecting 3.\n";
return false;
}
for (auto ids : idsVec)
{
if ((ids % 64) != 0)
{
cerr << "Error: IMSIC interrupt id limit (" << ids << ") is not a multiple of 64.\n";
return false;
}
if (ids > 2048)
{
cerr << "Error: IMSIC interrupt id limit (" << ids << ") is larger than 2048.\n";
return false;
}
}
if (idsVec.size() != tmVec.size())
{
cerr << "Error: IMSIC interrupt ids count (" << idsVec.size() << ") is different "
<< " thant the threshold-mask count (" << tmVec.size() << ")\n";
return false;
}
for (size_t i = 0; i < idsVec.size(); ++i)
{
if (tmVec.at(i) < idsVec.at(i) - 1)
{
cerr << "Error: Threshold mask (" << tmVec.at(0) << ") cannot be less than the "
<< "max interrupt id (" << (idsVec.at(i) - 1) << ").\n";
return false;
}
}
bool ok = imsicMgr_.configureMachine(mbase, mstride, idsVec.at(0), tmVec.at(0));
ok = imsicMgr_.configureSupervisor(sbase, sstride, idsVec.at(1), tmVec.at(1)) and ok;
ok = imsicMgr_.configureGuests(guests, idsVec.at(2), tmVec.at(2)) and ok;
if (not ok)
{
cerr << "Error: Failed to configure IMSIC.\n";
return false;
}
uint64_t mend = mbase + mstride * hartCount();
uint64_t send = sbase + sstride * hartCount();
auto readFunc = [this](uint64_t addr, unsigned size, uint64_t& data) -> bool {
return this->imsicMgr_.read(addr, size, data);
};
auto writeFunc = [this](uint64_t addr, unsigned size, uint64_t data) -> bool {
return this->imsicMgr_.write(addr, size, data);
};
for (unsigned i = 0; i < hartCount(); ++i)
{
auto hart = ithHart(i);
auto imsic = imsicMgr_.ithImsic(i);
hart->attachImsic(imsic, mbase, mend, sbase, send, readFunc, writeFunc, trace);
}
return true;
}
template <typename URV>
bool
System<URV>::configPci(uint64_t configBase, uint64_t mmioBase, uint64_t mmioSize, unsigned buses, unsigned slots)
{
if (mmioBase - configBase < (1ULL << 28))
{
std::cerr << "PCI config space typically needs 28bits to fully cover entire region" << std::endl;
return false;
}
pci_ = std::make_shared<Pci>(configBase, (1ULL << 28), mmioBase, mmioSize, buses, slots);
auto readf = [this](uint64_t addr, size_t size, uint64_t& data) -> bool {
bool ok = false;
if (size == 1)
{
uint8_t tmp = 0;
ok = this->memory_->peek(addr, tmp, false);
data = tmp;
}
if (size == 2)
{
uint16_t tmp = 0;
ok = this->memory_->peek(addr, tmp, false);
data = tmp;
}
if (size == 4)
{
uint32_t tmp = 0;
ok = this->memory_->peek(addr, tmp, false);
data = tmp;
}
if (size == 8)
return this->memory_->peek(addr, data, false);
return ok;
};
auto writef = [this](uint64_t addr, size_t size, uint64_t data) -> bool {
bool ok = false;
if (size == 1)
ok = this->memory_->poke(addr, uint8_t(data), false);
if (size == 2)
ok = this->memory_->poke(addr, uint16_t(data), false);
if (size == 4)
ok = this->memory_->poke(addr, uint32_t(data), false);
if (size == 8)
ok = this->memory_->poke(addr, data, false);
return ok;
};
auto msif = [this](uint64_t addr, unsigned size, uint64_t data) -> bool {
return this->imsicMgr_.write(addr, size, data);
};
pci_->define_read_mem(readf);
pci_->define_write_mem(writef);
pci_->define_msi(msif);
for (auto& hart : sysHarts_)
hart->attachPci(pci_);
return true;
}
template <typename URV>
bool
System<URV>::addPciDevices(const std::vector<std::string>& devs)
{
if (not pci_)
{
std::cerr << "Please specify a PCI region in the json" << std::endl;
return false;
}
for (const auto& devStr : devs)
{
std::vector<std::string> tokens;
boost::split(tokens, devStr, boost::is_any_of(":"), boost::token_compress_on);
if (tokens.size() < 3)
{
std::cerr << "PCI device string should have at least 3 fields" << std::endl;
return false;
}
std::string name = tokens.at(0);
unsigned bus = std::stoi(tokens.at(1));
unsigned slot = std::stoi(tokens.at(2));
if (name == "virtio-blk")
{
if (not (tokens.size() == 4))
{
std::cerr << "virtio-blk requires backing input file" << std::endl;
return false;
}
std::shared_ptr<Blk> dev = std::make_shared<Blk>(false);
if (not dev->open_file(tokens.at(3)))
return false;
if (not pci_->register_device(dev, bus, slot))
return false;
}
else
return false;
}
return true;
}
template <typename URV>
bool
System<URV>::enableMcm(unsigned mbLineSize, bool mbLineCheckAll,
const std::vector<unsigned>& enabledPpos)
{
if (mbLineSize != 0)
if (not isPowerOf2(mbLineSize) or mbLineSize > 512)
{
std::cerr << "Error: Invalid merge buffer line size: "
<< mbLineSize << '\n';
return false;
}
mcm_ = std::make_shared<Mcm<URV>>(this->hartCount(), pageSize(), mbLineSize);
mbSize_ = mbLineSize;
mcm_->setCheckWholeMbLine(mbLineCheckAll);
mcm_->enablePpo(false);
for (auto ppoIx : enabledPpos)
if (ppoIx < Mcm<URV>::PpoRule::Limit)
{
typedef typename Mcm<URV>::PpoRule Rule;
Rule rule = Rule(ppoIx);
mcm_->enablePpo(rule, true);
}
for (auto& hart : sysHarts_)
hart->setMcm(mcm_);
return true;
}
template <typename URV>
bool
System<URV>::enableMcm(unsigned mbLineSize, bool mbLineCheckAll, bool enablePpos)
{
if (mbLineSize != 0)
if (not isPowerOf2(mbLineSize) or mbLineSize > 512)
{
std::cerr << "Error: Invalid merge buffer line size: "
<< mbLineSize << '\n';
return false;
}
mcm_ = std::make_shared<Mcm<URV>>(this->hartCount(), pageSize(), mbLineSize);
mbSize_ = mbLineSize;
mcm_->setCheckWholeMbLine(mbLineCheckAll);
typedef typename Mcm<URV>::PpoRule Rule;
for (unsigned ix = 0; ix < Rule::Io; ++ix) // Temporary: Disable IO rule.
{
Rule rule = Rule(ix);
mcm_->enablePpo(rule, enablePpos);
}
for (auto& hart : sysHarts_)
hart->setMcm(mcm_);
return true;
}
template <typename URV>
void
System<URV>::endMcm()
{
if (mcm_)
{
auto& path = memory_->dataLineTracePath();
if (not path.empty())
{
memory_->saveDataAddressTrace(path, true /* writeValues */);
std::string emptyPath;
memory_->enableDataLineTrace(emptyPath); // Disable
}
}
for (auto& hart : sysHarts_)
hart->setMcm(nullptr);
mcm_ = nullptr;
}
template <typename URV>
bool
System<URV>::enablePerfApi(std::vector<FILE*>& traceFiles)
{
if constexpr (sizeof(URV) == 4)
{
std::cerr << "Performance model API is not supported for RV32\n";
return false;
}
else
{
perfApi_ = std::make_shared<TT_PERF::PerfApi>(*this);
for (auto& hart : sysHarts_)
hart->setPerfApi(perfApi_);
perfApi_->enableTraceLog(traceFiles);
}
return true;
}
template <typename URV>
void
System<URV>::enableTso(bool flag)
{
if (mcm_)
mcm_->enableTso(flag);
}
template <typename URV>
bool
System<URV>::mcmRead(Hart<URV>& hart, uint64_t time, uint64_t tag, uint64_t addr,
unsigned size, uint64_t data, unsigned elemIx, unsigned field)
{
if (not mcm_)
return false;
return mcm_->readOp(hart, time, tag, addr, size, data, elemIx, field);
}
template <typename URV>
bool
System<URV>::mcmMbWrite(Hart<URV>& hart, uint64_t time, uint64_t addr,
const std::vector<uint8_t>& data,
const std::vector<bool>& mask)
{
if (not mcm_)
return false;
return mcm_->mergeBufferWrite(hart, time, addr, data, mask);
}
template <typename URV>
bool
System<URV>::mcmMbInsert(Hart<URV>& hart, uint64_t time, uint64_t tag, uint64_t addr,
unsigned size, uint64_t data, unsigned elem, unsigned field)
{
if (not mcm_)
return false;
return mcm_->mergeBufferInsert(hart, time, tag, addr, size, data, elem, field);
}
template <typename URV>
bool
System<URV>::mcmBypass(Hart<URV>& hart, uint64_t time, uint64_t tag, uint64_t addr,
unsigned size, uint64_t data, unsigned elem, unsigned field)
{
if (not mcm_)
return false;
return mcm_->bypassOp(hart, time, tag, addr, size, data, elem, field);
}
template <typename URV>
bool
System<URV>::mcmIFetch(Hart<URV>& hart, uint64_t /*time*/, uint64_t addr)
{
if (not mcm_)
return false;
return hart.mcmIFetch(addr);
}
template <typename URV>
bool
System<URV>::mcmIEvict(Hart<URV>& hart, uint64_t /*time*/, uint64_t addr)
{
if (not mcm_)
return false;
return hart.mcmIEvict(addr);
}
template <typename URV>