forked from chipsalliance/VeeR-ISS
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCsRegs.cpp
5514 lines (4685 loc) · 155 KB
/
CsRegs.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 <iostream>
#include <algorithm>
#include <cassert>
#include <cfenv>
#include <array>
#include <bit>
#include <tuple>
#include "CsRegs.hpp"
#include "FpRegs.hpp"
#include "VecRegs.hpp"
#include "float-util.hpp"
using namespace WdRiscv;
template <typename URV>
CsRegs<URV>::CsRegs()
: regs_(size_t(CsrNumber::MAX_CSR_) + 1)
{
// Define CSR entries.
defineMachineRegs();
defineSupervisorRegs();
defineUserRegs();
defineHypervisorRegs();
defineDebugRegs();
defineVectorRegs();
defineFpRegs();
defineAiaRegs();
defineStateEnableRegs();
defineEntropyReg();
definePmaRegs();
defineSteeRegs();
}
template <typename URV>
CsRegs<URV>::~CsRegs()
{
regs_.clear();
nameToNumber_.clear();
}
template <typename URV>
Csr<URV>*
CsRegs<URV>::defineCsr(std::string name, CsrNumber csrn, bool mandatory,
bool implemented, URV resetValue, URV writeMask,
URV pokeMask, bool quiet)
{
size_t ix = size_t(csrn);
if (ix >= regs_.size())
return nullptr;
if (nameToNumber_.contains(name))
{
if (not quiet)
std::cerr << "Error: CSR " << name << " already defined\n";
return nullptr;
}
auto& csr = regs_.at(ix);
if (csr.isDefined())
{
if (not quiet)
std::cerr << "Error: CSR 0x" << std::hex << size_t(csrn) << std::dec
<< " is already defined as " << csr.getName() << '\n';
return nullptr;
}
PrivilegeMode priv = PrivilegeMode((ix & 0x300) >> 8);
if (priv == PrivilegeMode::Reserved) {
priv = PrivilegeMode::Supervisor;
}
csr.definePrivilegeMode(priv);
csr.setIsDebug(csrn >= CsrNumber::_MIN_DBG and csrn <= CsrNumber::_MAX_DBG);
csr.setDefined(true);
csr.config(name, csrn, mandatory, implemented, resetValue, writeMask, pokeMask);
nameToNumber_.insert_or_assign(std::move(name), csrn);
return &csr;
}
template <typename URV>
Csr<URV>*
CsRegs<URV>::findCsr(std::string_view name)
{
const auto iter = nameToNumber_.find(name);
if (iter == nameToNumber_.end())
return nullptr;
size_t num = size_t(iter->second);
if (num >= regs_.size())
return nullptr;
return ®s_.at(num);
}
template <typename URV>
Csr<URV>*
CsRegs<URV>::findCsr(CsrNumber number)
{
size_t ix = size_t(number);
if (ix >= regs_.size())
return nullptr;
return ®s_.at(ix);
}
template <typename URV>
const Csr<URV>*
CsRegs<URV>::findCsr(CsrNumber number) const
{
size_t ix = size_t(number);
if (ix >= regs_.size())
return nullptr;
return ®s_.at(ix);
}
template <typename URV>
Csr<URV>*
CsRegs<URV>::getImplementedCsr(CsrNumber num, bool virtualMode)
{
auto csr = getImplementedCsr(num);
if (not csr)
return csr;
if (not virtualMode)
return csr;
if (not csr->mapsToVirtual())
return csr;
num = advance(num, 0x100); // Get VCSR corresponding to CSR.
return getImplementedCsr(num);
}
template <typename URV>
const Csr<URV>*
CsRegs<URV>::getImplementedCsr(CsrNumber num, bool virtualMode) const
{
auto csr = getImplementedCsr(num);
if (not csr)
return csr;
if (not virtualMode)
return csr;
if (not csr->mapsToVirtual())
return csr;
num = advance(num, 0x100); // Get VCSR corresponding to CSR.
return getImplementedCsr(num);
}
template <typename URV>
bool
CsRegs<URV>::readSip(URV& value) const
{
value = 0;
auto sip = getImplementedCsr(CsrNumber::SIP);
if (not sip)
return false;
value = sip->read();
// Read value of MIP/SIP is masked by MIDELG.
auto deleg = getImplementedCsr(CsrNumber::MIDELEG);
if (deleg)
value &= deleg->read();
// Where mideleg is 0 and mvien is 1, sip becomes an alias to mvip.
auto mvien = getImplementedCsr(CsrNumber::MVIEN);
auto mvip = getImplementedCsr(CsrNumber::MVIP);
if (deleg and mvien and mvip)
{
URV mask = mvien->read() & ~deleg->read();
value = (value & ~mask) | (mvip->read() & mask);
}
// Bits SGEIP, VSEIP, VSTIP, VSSIP are read-only zero in SIE/SIP.
value &= ~ URV(0x1444);
return true;
}
template <typename URV>
bool
CsRegs<URV>::readSie(URV& value) const
{
value = 0;
auto sie = getImplementedCsr(CsrNumber::SIE);
if (not sie)
return false;
auto sieVal = sie->read();
value = sieVal;
auto deleg = getImplementedCsr(CsrNumber::MIDELEG);
auto mvien = getImplementedCsr(CsrNumber::MVIEN);
auto mvip = getImplementedCsr(CsrNumber::MVIP);
if (deleg and mvien and mvip)
{
// Where MIDELEG is 0 and MVIEN is 1, SIE becomes writable.
URV mask = mvien->read() & ~deleg->read();
value = shadowSie_ & mask;
// Everywhere else it is masked by MIDELEG
mask = ~mask & deleg->read();
value |= (sieVal & mask);
}
else if (deleg)
value = sieVal & deleg->read();
// Bits SGEIP, VSEIP, VSTIP, VSSIP are read-only zero in SIE/SIP.
value &= ~ URV(0x1444);
return true;
}
template <typename URV>
bool
CsRegs<URV>::readMvip(URV& value) const
{
value = 0;
auto mvip = getImplementedCsr(CsrNumber::MVIP);
if (not mvip)
return false;
value = mvip->read();
auto mip = getImplementedCsr(CsrNumber::MIP);
auto mvien = getImplementedCsr(CsrNumber::MVIEN);
if (mip and mvien)
{
// Bit 1 is an alias of mip when MVIEN is not set.
URV b1 = URV(0x2);
URV mask = mvien->read() ^ b1;
mask &= b1;
value = (value & ~mask) | (mip->read() & mask);
// Bit STIE (5) of MVIP is an alias to bit 5 of MIP if bit 5 of MIP is writable.
// Othrwise, it is zero.
mask = URV(0x20); // Bit 5
value &= ~mask; // Clear bit 5.
if ((mip->getWriteMask() & mask) != 0) // Bit 5 writable in mip
value |= (mip->read() & mask); // Set bit 5 to that of mip
}
return true;
}
template <typename URV>
bool
CsRegs<URV>::writeMvip(URV value)
{
auto mvip = getImplementedCsr(CsrNumber::MVIP);
if (not mvip)
return false;
auto mvien = getImplementedCsr(CsrNumber::MVIEN);
auto mip = getImplementedCsr(CsrNumber::MIP);
if (mvien and mip)
{
// Bit 1/9 of MVIP is an alias to bit 1/9 in MIP if bit 1/9 is not set in MVIEN.
URV mvienVal = mvien->read();
URV mask = mvienVal;
URV b19 = URV(0x202);
mask ^= b19;
// Bit STIE (5) of MVIP is an alias to bit 5 of MIP if bit 5 of MIP is writable.
// Othrwise, it is zero.
URV b5 = URV(0x20); // Bit 5 mask
if ((mip->getWriteMask() & b5) != 0) // Bit 5 writable in mip
mask |= b5;
mask &= b19 | b5;
mip->write((mip->read() & ~mask) | (value & mask));
recordWrite(CsrNumber::MIP);
}
mvip->write(value);
recordWrite(CsrNumber::MVIP);
return true;
}
template <typename URV>
bool
CsRegs<URV>::writeMvien(URV value)
{
auto mvien = getImplementedCsr(CsrNumber::MVIEN);
mvien->write(value);
recordWrite(CsrNumber::MVIEN);
auto mip = getImplementedCsr(CsrNumber::MIP);
if (mip)
{
URV b9 = URV(0x200);
URV mask = (mvien->read() ^ b9) & b9;
// Bit 9 is read-only when MVIEN is set.
mip->setWriteMask((mip->getWriteMask() & ~b9) | mask);
}
// Bits 13-63 are read-only zero in hideleg if both
// mideleg/mvien are not set.
auto mideleg = getImplementedCsr(CsrNumber::MIDELEG);
auto hideleg = getImplementedCsr(CsrNumber::HIDELEG);
if (mideleg and hideleg)
{
URV mask = URV(0x1fff);
hideleg->setReadMask((hideleg->getReadMask() & mask) |
(mideleg->read() & value & ~mask));
hideleg->write(hideleg->read());
}
return true;
}
template <typename URV>
URV
CsRegs<URV>::adjustTimeValue(CsrNumber num, URV value, bool virtMode) const
{
if (not virtMode)
return value;
auto delta = getImplementedCsr(CsrNumber::HTIMEDELTA);
if (num == CsrNumber::TIME)
{
if (delta)
value += delta->read();
}
else if (num == CsrNumber::TIMEH)
{
auto time = getImplementedCsr(CsrNumber::TIME);
auto deltah = getImplementedCsr(CsrNumber::HTIMEDELTAH);
if (time and delta and deltah)
{
uint64_t t64 = (uint64_t(value) << 32) | uint32_t(time->read());
uint64_t d64 = (uint64_t(deltah->read()) << 32) | uint32_t(delta->read());
t64 += d64;
value = t64 >> 32;
}
}
return value;
}
template <typename URV>
URV
CsRegs<URV>::adjustSstateenValue(CsrNumber num, URV value, bool virtMode) const
{
using CN = CsrNumber;
if (num >= CN::SSTATEEN0 and num <= CN::SSTATEEN3)
{
CN base = CN::SSTATEEN0;
unsigned ix = unsigned(num) - unsigned(base);
// If a bit is zero in MSTATEEN, it becomes zero in SSTATEEN
CsrNumber mnum = advance(CN::MSTATEEN0, ix);
auto mcsr = getImplementedCsr(mnum);
if (mcsr)
value &= mcsr->read();
// If a bit is zero in HSTATEEN, it becomes zero in SSTATEEN
if (virtMode)
{
CsrNumber hnum = advance(CN::HSTATEEN0, ix);
auto hcsr = getImplementedCsr(hnum);
if (hcsr)
value &= hcsr->read();
}
}
return value;
}
template <typename URV>
URV
CsRegs<URV>::adjustHstateenValue(CsrNumber num, URV value) const
{
using CN = CsrNumber;
if ((num >= CN::HSTATEEN0 and num <= CN::HSTATEEN3) or
(num >= CN::HSTATEEN0H and num <= CN::HSTATEEN3H))
{
CN base = CN::HSTATEEN0;
if (num >= CN::HSTATEEN0H and num <= CN::HSTATEEN3H)
base = CN::HSTATEEN0H;
unsigned ix = unsigned(num) - unsigned(base);
// If a bit is zero in MSTATEEN, it becomes zero in HSTATEEN
CN mnum = CN::MSTATEEN0;
if (num >= CN::HSTATEEN0H and num <= CN::HSTATEEN3H)
mnum = CN::MSTATEEN0H;
mnum = advance(mnum, ix);
auto mcsr = getImplementedCsr(mnum);
if (mcsr)
value &= mcsr->read();
}
return value;
}
template <typename URV>
URV
CsRegs<URV>::adjustScountovfValue(URV value, bool virtMode) const
{
auto csr = getImplementedCsr(CsrNumber::MCOUNTEREN);
assert(csr and "MCOUNTEREN not implemented");
URV mask = csr->read();
if (virtMode)
{
csr = getImplementedCsr(CsrNumber::HCOUNTEREN);
assert(csr and "HCOUNTERN not implemented");
mask &= csr->read();
}
return value & mask;
}
template <typename URV>
bool
CsRegs<URV>::readMireg(CsrNumber num, URV& value, bool virtMode) const
{
URV sel = 0;
peek(CsrNumber::MISELECT, sel);
if (imsic_)
{
auto csr = getImplementedCsr(num, virtMode);
if (not csr)
return false;
return imsic_->readMireg(sel, value);
}
return false;
}
template <typename URV>
bool
CsRegs<URV>::readSireg(CsrNumber num, URV& value, bool virtMode) const
{
URV sel = 0;
peek(CsrNumber::SISELECT, sel);
if (imsic_)
{
auto csr = getImplementedCsr(num, virtMode);
if (not csr)
return false;
unsigned guest = 0;
if (virtMode)
{
URV hs = 0;
peek(CsrNumber::HSTATUS, hs);
HstatusFields<URV> hsf(hs);
guest = hsf.bits_.VGEIN;
}
return imsic_->readSireg(virtMode, guest, sel, value);
}
return false;
}
template <typename URV>
bool
CsRegs<URV>::readVsireg(CsrNumber num, URV& value, bool virtMode) const
{
URV sel = 0;
peek(CsrNumber::VSISELECT, sel);
if (imsic_)
{
auto csr = getImplementedCsr(num, virtMode);
if (not csr)
return false;
URV hs = 0;
peek(CsrNumber::HSTATUS, hs);
HstatusFields<URV> hsf(hs);
unsigned guest = hsf.bits_.VGEIN;
return imsic_->readSireg(true, guest, sel, value);
}
return false;
}
template <typename URV>
bool
CsRegs<URV>::read(CsrNumber num, PrivilegeMode mode, URV& value) const
{
using CN = CsrNumber;
auto csr = getImplementedCsr(num, virtMode_);
if (not csr or mode < csr->privilegeMode() or not isStateEnabled(num, mode, virtMode_))
return false;
num = csr->getNumber(); // CSR may have been remapped from S to VS
if (csr->isDebug() and not inDebugMode())
return false; // Debug-mode register.
if (num >= CN::TDATA1 and num <= CN::TINFO)
return readTrigger(num, mode, value);
if (num == CN::FFLAGS or num == CN::FRM)
{
auto fcsr = getImplementedCsr(CN::FCSR);
if (not fcsr)
return false;
value = fcsr->read();
if (num == CN::FFLAGS)
value = FcsrFields{value}.bits_.FFLAGS;
else
value = FcsrFields{value}.bits_.FRM;
return true;
}
else if (num == CN::MIREG)
return readMireg(num, value, virtMode_);
else if (num == CN::SIREG)
return readSireg(num, value, virtMode_);
else if (num == CN::VSIREG)
return readVsireg(num, value, virtMode_);
else if (num == CN::SIP)
return readSip(value);
else if (num == CN::SIE)
return readSie(value);
if (num == CN::MTOPEI)
{
if (not imsic_)
return false;
value = imsic_->machineTopId();
value |= value << 16; // Bits 26:16 same as bits 10;0 as required by spec.
return true;
}
else if (num == CN::STOPEI)
{
if (not imsic_)
return false;
value = imsic_->supervisorTopId();
value |= value << 16; // Bits 26:16 same as bits 10;0 as required by spec.
return true;
}
else if (num == CN::VSTOPEI)
{
if (not imsic_)
return false;
const auto& hs = regs_.at(size_t(CsrNumber::HSTATUS));
URV hsVal = hs.read();
HstatusFields<URV> hsf(hsVal);
unsigned vgein = hsf.bits_.VGEIN;
if (not vgein or vgein >= imsic_->guestCount())
return false;
value = imsic_->guestTopId(vgein);
value |= value << 16; // Bits 26:16 same as bits 10;0 as required by spec.
return true;
}
if (num == CN::MTOPI or num == CN::STOPI or num == CN::VSTOPI)
return readTopi(num, value, virtMode_);
else if (num == CN::SIE)
return readSie(value);
else if (num == CN::SIP)
return readSip(value);
else if (num == CN::MVIP)
return readMvip(value);
value = csr->read();
if (virtMode_ and (num == CN::TIME or num == CN::TIMEH))
value = adjustTimeValue(num, value, virtMode_); // In virt mode, time is time + htimedelta.
else if (num >= CN::PMPADDR0 and num <= CN::PMPADDR63)
value = adjustPmpValue(num, value);
else if (num >= CN::SSTATEEN0 and num <= CN::SSTATEEN3)
value = adjustSstateenValue(num, value, virtMode_);
else if ((num >= CN::HSTATEEN0 and num <= CN::HSTATEEN3) or
(num >= CN::HSTATEEN0H and num <= CN::HSTATEEN3H))
value = adjustHstateenValue(num, value);
else if (num == CN::SCOUNTOVF and mode != PrivilegeMode::Machine)
value = adjustScountovfValue(value, virtMode_);
return true;
}
template <typename URV>
bool
CsRegs<URV>::readSignExtend(CsrNumber number, PrivilegeMode mode, URV& value) const
{
if (not read(number, mode, value))
return false;
if (value == 0)
return true;
auto csr = getImplementedCsr(number, virtMode_);
URV mask = csr->getWriteMask();
unsigned lz = std::countl_zero(mask);
using SRV = typename std::make_signed_t<URV>;
SRV svalue = value;
svalue = (svalue << lz) >> lz;
value = svalue;
return true;
}
template <typename URV>
void
CsRegs<URV>::enableSupervisorMode(bool flag)
{
superEnabled_ = flag;
using CN = CsrNumber;
auto enableCsr = [this] (CN csrn, bool flag) {
auto csr = findCsr(csrn);
if (csr)
csr->setImplemented(flag);
};
for (auto csrn : { CN::SSTATUS, CN::SIE, CN::STVEC, CN::SCOUNTEREN,
CN::SSCRATCH, CN::SEPC, CN::SCAUSE, CN::STVAL, CN::SIP,
CN::SENVCFG, CN::SATP, CN::MEDELEG, CN::MIDELEG,
CN::SCONTEXT } )
enableCsr(csrn, flag);
if (hyperEnabled_)
for (auto csrn : { CN::VSSTATUS, CN::VSIE, CN::VSTVEC, CN::VSSCRATCH,
CN::VSEPC, CN::VSCAUSE, CN::VSTVAL, CN::VSIP, CN::VSATP } )
enableCsr(csrn, flag);
using IC = InterruptCause;
// In MIP/MIE, make writable/pokable bits corresponding to
// SEIP/STIP/SSIP (supervisor external/timer/software interrupt
// pending) when sstc is enabled and read-only-zero when supervisor
// is disabled.
URV sbits = ( URV(1) << unsigned(IC::S_EXTERNAL) |
URV(1) << unsigned(IC::S_TIMER) |
URV(1) << unsigned(IC::S_SOFTWARE) );
for (auto csrn : { CN::MIP, CN::MIE } )
{
auto csr = findCsr(csrn);
if (csr)
{
URV mask = csr->getWriteMask();
mask = flag? mask | sbits : mask & ~sbits;
csr->setWriteMask(mask);
mask = csr->getPokeMask();
mask = flag? mask | sbits : mask & ~sbits;
csr->setPokeMask(mask);
}
}
// Make IR/TM/CY bits read only zero in MCOUNTERNE/SCOUNTEREN/HCOUNTEREN if the
// RETIRED/TIME/CYCLE CSRs is not implemented. This is not explicitly stated in the spec
// but it is in the lore.
URV mask = 0; // Least sig 3 bits of MCOUNTEREN.
if (regs_.at(unsigned(CN::CYCLE)).isImplemented())
mask |= 1;
if (regs_.at(unsigned(CN::TIME)).isImplemented())
mask |= 2;
if (regs_.at(unsigned(CN::INSTRET)).isImplemented())
mask |= 4;
auto& mce = regs_.at(unsigned(CN::MCOUNTEREN));
auto& sce = regs_.at(unsigned(CN::SCOUNTEREN));
auto& hce = regs_.at(unsigned(CN::SCOUNTEREN));
mce.setReadMask((mce.getReadMask() & ~URV(7)) | mask);
sce.setReadMask((sce.getReadMask() & ~URV(7)) | mask);
hce.setReadMask((hce.getReadMask() & ~URV(7)) | mask);
updateSstc(); // To activate/deactivate STIMECMP.
enableSscofpmf(cofEnabled_); // To activate/deactivate SCOUNTOVF.
enableSmstateen(stateenOn_); // To activate/deactivate STATEEN CSRs.
enableSdtrig(sdtrigOn_); // To activate/deactivate SCONTEXT.
enableSsqosid(ssqosidOn_); // To activate/deactivate SRMCFG.
}
template <typename URV>
void
CsRegs<URV>::updateSstc()
{
bool stce = menvcfgStce();
URV mMask = 0;
if (not peek(CsrNumber::MCOUNTEREN, mMask))
return;
bool mTm = (mMask & 2) >> 1;
PrivilegeMode mode = (stce & mTm)? PrivilegeMode::Supervisor : PrivilegeMode::Machine;
auto stimecmp = findCsr(CsrNumber::STIMECMP);
if (sstcEnabled_ and not stimecmp->isImplemented())
stimecmp->setImplemented(true);
stimecmp->setPrivilegeMode(mode);
stimecmp->setHypervisor(stce);
if (rv32_)
{
auto stimecmph = findCsr(CsrNumber::STIMECMPH);
if (sstcEnabled_ and not stimecmph->isImplemented())
stimecmph->setImplemented(true);
stimecmph->setPrivilegeMode(mode);
stimecmp->setHypervisor(stce);
}
if (superEnabled_)
{
// S_TIMER bit in MIP is read-only if stimecmp is implemented and
// writeable if it is not.
auto mip = findCsr(CsrNumber::MIP);
if (mip)
{
URV mask = mip->getWriteMask();
URV stBit = URV(1) << unsigned(InterruptCause::S_TIMER);
bool readOnly = stce;
mask = readOnly? mask & ~stBit : mask | stBit;
mip->setWriteMask(mask);
}
}
URV hMask = 0;
peek(CsrNumber::HCOUNTEREN, hMask);
bool hstce = henvcfgStce();
bool hTm = (hMask & 2) >> 1;
auto vstimecmp = findCsr(CsrNumber::VSTIMECMP);
vstimecmp->setImplemented(sstcEnabled_ and hyperEnabled_);
vstimecmp->setPrivilegeMode(mode);
if (rv32_)
{
auto vstimecmph = findCsr(CsrNumber::VSTIMECMPH);
vstimecmph->setImplemented(sstcEnabled_ and hyperEnabled_);
vstimecmph->setPrivilegeMode(mode);
}
if (stce)
{
bool noVs = not (hstce and hTm);
stimecmp->setHypervisor(noVs);
if (rv32_)
findCsr(CsrNumber::STIMECMPH)->setHypervisor(noVs);
}
// If henvcfg.VSTCE is cleared, we also clear the VSTIP bit. This is
// unspecified behavior and we do this to match RTL.
auto mip = findCsr(CsrNumber::MIP);
if (mip and not hstce)
{
URV mask = URV(1) << URV(InterruptCause::VS_TIMER);
auto hvip = findCsr(CsrNumber::HVIP);
URV vstip = hvip? hvip->read() : 0;
mip->poke((mip->read() & ~mask) | vstip);
hyperWrite(mip);
}
auto hip = findCsr(CsrNumber::HIP);
if (hip)
{
// Update VSTIP bit in HIP. See chapter 3 of SSTC spc.
URV mask = hip->getReadMask();
URV vstBit = URV(1) << unsigned(InterruptCause::VS_TIMER);
if (stce and not hstce)
mask = mask & ~vstBit; // Make read-only zero
else
mask = mask | vstBit; // Make readable
hip->setReadMask(mask);
}
}
template <typename URV>
void
CsRegs<URV>::enableHypervisorMode(bool flag)
{
hyperEnabled_ = flag;
using CN = CsrNumber;
auto enableCsr = [this] (CN csrn, bool flag) {
auto csr = findCsr(csrn);
if (csr)
csr->setImplemented(flag);
};
for (auto csrn : { CN::HSTATUS, CN::HEDELEG, CN::HIDELEG, CN::HIE, CN::HCOUNTEREN,
CN::HGEIE, CN::HTVAL, CN::HIP, CN::HVIP, CN::HTINST, CN::HGEIP, CN::HENVCFG,
CN::HGATP, CN::HCONTEXT, CN::HTIMEDELTA, CN::MTVAL2, CN::MTINST, CN::HCONTEXT } )
enableCsr(csrn, flag);
if (rv32_)
for (auto csrn : { CN::HENVCFGH, CN::HTIMEDELTAH } )
enableCsr(csrn, flag);
if (superEnabled_)
for (auto csrn : { CN::VSSTATUS, CN::VSIE, CN::VSTVEC, CN::VSSCRATCH,
CN::VSEPC, CN::VSCAUSE, CN::VSTVAL, CN::VSIP, CN::VSATP })
enableCsr(csrn, flag);
// Enable/disable MPV and GVA bits
{
uint64_t hyperBits;
Csr<URV>* mstatus;
if (not rv32_)
{
hyperBits = uint64_t(0x3) << 38;
mstatus = findCsr(CN::MSTATUS);
}
else
{
hyperBits = 0x3 << 6;
mstatus = findCsr(CN::MSTATUSH);
}
if (not flag)
mstatus->write(mstatus->read() & ~hyperBits); // Clear MPV and GVA.
URV mask = mstatus->getWriteMask();
mask = flag? (mask | hyperBits) : (mask & ~hyperBits);
mstatus->setWriteMask(mask);
mask = mstatus->getPokeMask();
mask = flag? (mask | hyperBits) : (mask & ~hyperBits);
mstatus->setPokeMask(mask);
mask = mstatus->getReadMask();
mask = flag? (mask | hyperBits) : (mask & ~hyperBits);
mstatus->setReadMask(mask);
}
// Bits correspondig to VSEIP, VSTIP, VSSIP, and SGEIP.
URV vsBits = 0x444;
URV sgBit = geilen_ ? 0x1000 : 0; // Bit SGEIP
auto csr = findCsr(CN::MIDELEG);
if (flag)
{
// Make VSEIP, VSTIP, and VSSIP and posibly SGEIP read-only one.
URV rooMask = vsBits | sgBit; // Bits to be made read-only-one.
csr->setWriteMask(csr->getWriteMask() | rooMask); // Make bits writeable.
csr->setReadMask(csr->getReadMask() | rooMask); // Open for reading.
csr->write(csr->read() | rooMask); // Set bits to one.
csr->setWriteMask(csr->getWriteMask() & ~rooMask); // Make bits read-only.
}
else
{
// Make VSEIP, VSTIP, VSSIP, and SGEIP read only zero.
auto mask = csr->getReadMask();
mask &= ~URV(0x1444);
csr->setReadMask(mask);
}
if (flag)
{
auto hideleg = getImplementedCsr(CsrNumber::HIDELEG);
assert(hideleg);
auto vsip = getImplementedCsr(CsrNumber::VSIP);
auto vsie = getImplementedCsr(CsrNumber::VSIE);
URV mask = 0x222; // Bits VSEIP, VSTIP, and VSSIP of VSIP
mask &= (hideleg->read() >> 1);
if (vsip)
vsip->setReadMask(mask);
if (vsie)
vsie->setReadMask(mask);
}
// If hypervisor is off, related bits in MEDELEG are read-only-zero (bits 23:20 and 10).
csr = findCsr(CN::MEDELEG);
if (csr)
{
URV bits = URV(0xf) << 20; // Bits 23:20
bits |= URV(1) << 10; // Bit 10
auto mask = csr->getReadMask();
csr->setReadMask(flag ? (mask | bits) : (mask & ~bits));
}
// Bit MIP.VSSIP is writeable if hypervisor is enabled, otherwise it is not
csr = findCsr(CN::MIP);
if (csr)
{
URV bit = 4;
auto mask = csr->getWriteMask();
csr->setWriteMask(flag ? (mask | bit) : (mask & ~bit));
}
// In MIE, bits VSEIE, VSTIE, VSSIE, and SGEIE become read-only-zero if no hypervisor.
csr = findCsr(CN::MIE);
if (csr)
{
URV bits = 0x1444;
auto mask = csr->getReadMask();
csr->setReadMask(flag ? (mask | bits) : (mask & ~bits));
if (not flag)
csr->write(csr->read()); // Clear bits in CSR that are now read-only-zero.
mask = csr->getWriteMask();
csr->setWriteMask(flag ? (mask | bits) : (mask & ~bits));
}
updateSstc(); // To activate/deactivate VSTIMECMP.
enableSmstateen(stateenOn_); // To activate/deactivate STATEEN CSRs.
enableAia(aiaEnabled_); // To activate/deactivate AIA hypervisor CSRs.
enableSdtrig(sdtrigOn_); // To activate/deactivate HCONTEXT.
enableSsqosid(ssqosidOn_); // To activate/deactivate SRMCFG.
triggers_.enableHypervisor(flag);
}
template <typename URV>
void
CsRegs<URV>::enableRvf(bool flag)
{
for (auto csrn : { CsrNumber::FCSR, CsrNumber::FFLAGS, CsrNumber::FRM } )
{
auto csr = findCsr(csrn);
if (not csr)
{
std::cerr << "Error: enableRvf: CSR number 0x"
<< std::hex << URV(csrn) << std::dec << " undefined\n";
assert(0);
}
else if (not csr->isImplemented())
csr->setImplemented(flag);
}
// If neither F or S extension is enabled then FS bits in MSTATUS are
// read only zero; otherwise, they are readable.
auto mstatus = findCsr(CsrNumber::MSTATUS);
if (mstatus)
{
MstatusFields<URV> fields(mstatus->getReadMask());
fields.bits_.FS = 0;
if (flag or superEnabled_)
fields.bits_.FS = ~ fields.bits_.FS;
mstatus->setReadMask(fields.value_);
}
}
template <typename URV>
void
CsRegs<URV>::enableSscofpmf(bool flag)
{
cofEnabled_ = flag;
auto csrn = CsrNumber::SCOUNTOVF;
auto csr = findCsr(csrn);
if (not csr)
{
std::cerr << "Error: enableSscofpmf: CSR number 0x"
<< std::hex << URV(csrn) << std::dec << " is not defined\n";
assert(0);
}
else
csr->setImplemented(flag & superEnabled_);
// Add CSR fields.
std::vector<typename Csr<URV>::Field> hpm = {{"zero", 3}};
for (unsigned i = 3; i <= 31; ++i)
hpm.push_back({"HPM" + std::to_string(i), 1});
setCsrFields(csrn, hpm);
// Mask/unmask LCOF bits
for (auto csrn : {CsrNumber::MIE, CsrNumber::MIP, CsrNumber::SIE, CsrNumber::SIP})
{
auto csr = findCsr(csrn);
if (csr)
{
auto lcof = URV(1) << URV(InterruptCause::LCOF);
if (flag)
{
csr->setWriteMask(csr->getWriteMask() | lcof);
csr->setReadMask(csr->getReadMask() | lcof);
}
else
{