forked from chipsalliance/VeeR-ISS
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCsRegs.hpp
2335 lines (1965 loc) · 72.5 KB
/
CsRegs.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
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.
#pragma once
#include <cstdint>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <functional>
#include <cassert>
#include "Triggers.hpp"
#include "PerfRegs.hpp"
#include "CsrFields.hpp"
#include "Imsic.hpp"
#include "util.hpp"
namespace WdRiscv
{
/// Control and status register number.
enum class CsrNumber : uint32_t
{
// Machine mode registers.
// Machine info.
MVENDORID = 0xF11,
MARCHID = 0xF12,
MIMPID = 0xF13,
MHARTID = 0xF14,
MCONFIGPTR = 0xF15,
// Machine trap setup.
MSTATUS = 0x300,
MISA = 0x301,
MEDELEG = 0x302,
MIDELEG = 0x303,
MIE = 0x304,
MTVEC = 0x305,
MCOUNTEREN = 0x306,
MSTATUSH = 0x310,
MCOUNTINHIBIT = 0x320,
// Machine trap handling
MSCRATCH = 0x340,
MEPC = 0x341,
MCAUSE = 0x342,
MTVAL = 0x343,
MIP = 0x344,
MTINST = 0x34A,
MTVAL2 = 0x34B,
// Machine protection and translation.
PMPCFG0 = 0x3a0,
PMPCFG15 = 0x3af,
PMPADDR0 = 0x3b0,
PMPADDR63 = 0x3ef,
MCYCLECFG = 0x321,
MINSTRETCFG = 0x322,
MCYCLECFGH = 0x721,
MINSTRETCFGH = 0x722,
// Machine configuration.
MENVCFG = 0x30a,
MENVCFGH = 0x31a,
MSECCFG = 0x747,
MSECCFGH = 0x757,
// Non maskable interrupts
MNSCRATCH = 0x740,
MNEPC = 0x741,
MNCAUSE = 0x742,
MNSTATUS = 0x744,
// Machine Counter/Timers
MCYCLE = 0xb00,
MINSTRET = 0xb02,
MHPMCOUNTER3 = 0xb03,
MHPMCOUNTER4 = 0xb04,
MHPMCOUNTER5 = 0xb05,
MHPMCOUNTER6 = 0xb06,
MHPMCOUNTER7 = 0xb07,
MHPMCOUNTER8 = 0xb08,
MHPMCOUNTER9 = 0xb09,
MHPMCOUNTER10 = 0xb0a,
MHPMCOUNTER11 = 0xb0b,
MHPMCOUNTER12 = 0xb0c,
MHPMCOUNTER13 = 0xb0d,
MHPMCOUNTER14 = 0xb0e,
MHPMCOUNTER15 = 0xb0f,
MHPMCOUNTER16 = 0xb10,
MHPMCOUNTER17 = 0xb11,
MHPMCOUNTER18 = 0xb12,
MHPMCOUNTER19 = 0xb13,
MHPMCOUNTER20 = 0xb14,
MHPMCOUNTER21 = 0xb15,
MHPMCOUNTER22 = 0xb16,
MHPMCOUNTER23 = 0xb17,
MHPMCOUNTER24 = 0xb18,
MHPMCOUNTER25 = 0xb19,
MHPMCOUNTER26 = 0xb1a,
MHPMCOUNTER27 = 0xb1b,
MHPMCOUNTER28 = 0xb1c,
MHPMCOUNTER29 = 0xb1d,
MHPMCOUNTER30 = 0xb1e,
MHPMCOUNTER31 = 0xb1f,
MCYCLEH = 0xb80,
MINSTRETH = 0xb82,
MHPMCOUNTER3H = 0xb83,
MHPMCOUNTER4H = 0xb84,
MHPMCOUNTER5H = 0xb85,
MHPMCOUNTER6H = 0xb86,
MHPMCOUNTER7H = 0xb87,
MHPMCOUNTER8H = 0xb88,
MHPMCOUNTER9H = 0xb89,
MHPMCOUNTER10H = 0xb8a,
MHPMCOUNTER11H = 0xb8b,
MHPMCOUNTER12H = 0xb8c,
MHPMCOUNTER13H = 0xb8d,
MHPMCOUNTER14H = 0xb8e,
MHPMCOUNTER15H = 0xb8f,
MHPMCOUNTER16H = 0xb90,
MHPMCOUNTER17H = 0xb91,
MHPMCOUNTER18H = 0xb92,
MHPMCOUNTER19H = 0xb93,
MHPMCOUNTER20H = 0xb94,
MHPMCOUNTER21H = 0xb95,
MHPMCOUNTER22H = 0xb96,
MHPMCOUNTER23H = 0xb97,
MHPMCOUNTER24H = 0xb98,
MHPMCOUNTER25H = 0xb99,
MHPMCOUNTER26H = 0xb9a,
MHPMCOUNTER27H = 0xb9b,
MHPMCOUNTER28H = 0xb9c,
MHPMCOUNTER29H = 0xb9d,
MHPMCOUNTER30H = 0xb9e,
MHPMCOUNTER31H = 0xb9f,
// Machine counter setup.
MHPMEVENT3 = 0x323,
MHPMEVENT4 = 0x324,
MHPMEVENT5 = 0x325,
MHPMEVENT6 = 0x326,
MHPMEVENT7 = 0x327,
MHPMEVENT8 = 0x328,
MHPMEVENT9 = 0x329,
MHPMEVENT10 = 0x32a,
MHPMEVENT11 = 0x32b,
MHPMEVENT12 = 0x32c,
MHPMEVENT13 = 0x32d,
MHPMEVENT14 = 0x32e,
MHPMEVENT15 = 0x32f,
MHPMEVENT16 = 0x330,
MHPMEVENT17 = 0x331,
MHPMEVENT18 = 0x332,
MHPMEVENT19 = 0x333,
MHPMEVENT20 = 0x334,
MHPMEVENT21 = 0x335,
MHPMEVENT22 = 0x336,
MHPMEVENT23 = 0x337,
MHPMEVENT24 = 0x338,
MHPMEVENT25 = 0x339,
MHPMEVENT26 = 0x33a,
MHPMEVENT27 = 0x33b,
MHPMEVENT28 = 0x33c,
MHPMEVENT29 = 0x33d,
MHPMEVENT30 = 0x33e,
MHPMEVENT31 = 0x33f,
MHPMEVENTH3 = 0x723,
MHPMEVENTH4 = 0x724,
MHPMEVENTH5 = 0x725,
MHPMEVENTH6 = 0x726,
MHPMEVENTH7 = 0x727,
MHPMEVENTH8 = 0x728,
MHPMEVENTH9 = 0x729,
MHPMEVENTH10 = 0x72a,
MHPMEVENTH11 = 0x72b,
MHPMEVENTH12 = 0x72c,
MHPMEVENTH13 = 0x72d,
MHPMEVENTH14 = 0x72e,
MHPMEVENTH15 = 0x72f,
MHPMEVENTH16 = 0x730,
MHPMEVENTH17 = 0x731,
MHPMEVENTH18 = 0x732,
MHPMEVENTH19 = 0x733,
MHPMEVENTH20 = 0x734,
MHPMEVENTH21 = 0x735,
MHPMEVENTH22 = 0x736,
MHPMEVENTH23 = 0x737,
MHPMEVENTH24 = 0x738,
MHPMEVENTH25 = 0x739,
MHPMEVENTH26 = 0x73a,
MHPMEVENTH27 = 0x73b,
MHPMEVENTH28 = 0x73c,
MHPMEVENTH29 = 0x73d,
MHPMEVENTH30 = 0x73e,
MHPMEVENTH31 = 0x73f,
// Supervisor mode registers.
// Supervisor trap setup.
SSTATUS = 0x100,
SIE = 0x104,
STVEC = 0x105,
SCOUNTEREN = 0x106,
SENVCFG = 0x10a,
SCOUNTOVF = 0xda0,
// Supervisor Trap Handling
SSCRATCH = 0x140,
SEPC = 0x141,
SCAUSE = 0x142,
STVAL = 0x143,
SIP = 0x144,
STIMECMP = 0x14d,
STIMECMPH = 0x15d,
// Supervisor Protection and Translation
SATP = 0x180,
// Quality of service
SRMCFG = 0x181,
// Hypervisor registers
HSTATUS = 0x600,
HEDELEG = 0x602,
HIDELEG = 0x603,
HIE = 0x604,
HCOUNTEREN = 0x606,
HGEIE = 0x607,
HTVAL = 0x643,
HIP = 0x644,
HVIP = 0x645,
HTINST = 0x64A,
HGEIP = 0xE12,
HENVCFG = 0x60A,
HENVCFGH = 0x61A,
HGATP = 0x680,
HCONTEXT = 0x6A8,
HTIMEDELTA = 0x605,
HTIMEDELTAH = 0x615,
// Virtual supervisor
VSSTATUS = 0x200,
VSIE = 0x204,
VSTVEC = 0x205,
VSSCRATCH = 0x240,
VSEPC = 0x241,
VSCAUSE = 0x242,
VSTVAL = 0x243,
VSIP = 0x244,
VSTIMECMP = 0x24d,
VSTIMECMPH = 0x25d,
VSATP = 0x280,
// User mode registers.
// User Floating-Point CSRs
FFLAGS = 0x001,
FRM = 0x002,
FCSR = 0x003,
// User Counter/Timers
CYCLE = 0xc00,
TIME = 0xc01,
INSTRET = 0xc02,
HPMCOUNTER3 = 0xc03,
HPMCOUNTER4 = 0xc04,
HPMCOUNTER5 = 0xc05,
HPMCOUNTER6 = 0xc06,
HPMCOUNTER7 = 0xc07,
HPMCOUNTER8 = 0xc08,
HPMCOUNTER9 = 0xc09,
HPMCOUNTER10 = 0xc0a,
HPMCOUNTER11 = 0xc0b,
HPMCOUNTER12 = 0xc0c,
HPMCOUNTER13 = 0xc0d,
HPMCOUNTER14 = 0xc0e,
HPMCOUNTER15 = 0xc0f,
HPMCOUNTER16 = 0xc10,
HPMCOUNTER17 = 0xc11,
HPMCOUNTER18 = 0xc12,
HPMCOUNTER19 = 0xc13,
HPMCOUNTER20 = 0xc14,
HPMCOUNTER21 = 0xc15,
HPMCOUNTER22 = 0xc16,
HPMCOUNTER23 = 0xc17,
HPMCOUNTER24 = 0xc18,
HPMCOUNTER25 = 0xc19,
HPMCOUNTER26 = 0xc1a,
HPMCOUNTER27 = 0xc1b,
HPMCOUNTER28 = 0xc1c,
HPMCOUNTER29 = 0xc1d,
HPMCOUNTER30 = 0xc1e,
HPMCOUNTER31 = 0xc1f,
CYCLEH = 0xc80,
TIMEH = 0xc81,
INSTRETH = 0xc82,
HPMCOUNTER3H = 0xc83,
HPMCOUNTER4H = 0xc84,
HPMCOUNTER5H = 0xc85,
HPMCOUNTER6H = 0xc86,
HPMCOUNTER7H = 0xc87,
HPMCOUNTER8H = 0xc88,
HPMCOUNTER9H = 0xc89,
HPMCOUNTER10H = 0xc8a,
HPMCOUNTER11H = 0xc8b,
HPMCOUNTER12H = 0xc8c,
HPMCOUNTER13H = 0xc8d,
HPMCOUNTER14H = 0xc8e,
HPMCOUNTER15H = 0xc8f,
HPMCOUNTER16H = 0xc90,
HPMCOUNTER17H = 0xc91,
HPMCOUNTER18H = 0xc92,
HPMCOUNTER19H = 0xc93,
HPMCOUNTER20H = 0xc94,
HPMCOUNTER21H = 0xc95,
HPMCOUNTER22H = 0xc96,
HPMCOUNTER23H = 0xc97,
HPMCOUNTER24H = 0xc98,
HPMCOUNTER25H = 0xc99,
HPMCOUNTER26H = 0xc9a,
HPMCOUNTER27H = 0xc9b,
HPMCOUNTER28H = 0xc9c,
HPMCOUNTER29H = 0xc9d,
HPMCOUNTER30H = 0xc9e,
HPMCOUNTER31H = 0xc9f,
// Debug/Trace registers.
SCONTEXT = 0x5a8,
TSELECT = 0x7a0,
TDATA1 = 0x7a1,
TDATA2 = 0x7a2,
TDATA3 = 0x7a3,
TINFO = 0x7a4,
TCONTROL = 0x7a5,
MCONTEXT = 0x7a8,
MSCONTEXT = 0x7aa,
// Debug mode registers.
_MIN_DBG = 0x7b0,
DCSR = 0x7b0,
DPC = 0x7b1,
DSCRATCH0 = 0x7b2,
DSCRATCH1 = 0x7b3,
_MAX_DBG = 0x7bf,
// Vector extension register.
VSTART = 0x008,
VXSAT = 0x009,
VXRM = 0x00a,
VCSR = 0x00f,
VL = 0xc20,
VTYPE = 0xc21,
VLENB = 0xc22,
// Advanced interrupt architecture (AIA)
MISELECT = 0x350,
MIREG = 0x351,
MIREG2 = 0x352,
MIREG3 = 0x353,
MIREG4 = 0x355,
MIREG5 = 0x356,
MIREG6 = 0x357,
MTOPEI = 0x35c,
MTOPI = 0xFB0,
MVIEN = 0x308,
MVIP = 0x309,
MIDELEGH = 0x313,
MIEH = 0x314,
MVIENH = 0x318,
MVIPH = 0x319,
MIPH = 0x354,
SISELECT = 0x150,
SIREG = 0x151,
SIREG2 = 0x152,
SIREG3 = 0x153,
SIREG4 = 0x155,
SIREG5 = 0x156,
SIREG6 = 0x157,
STOPEI = 0x15c,
STOPI = 0xdb0,
SIEH = 0x114,
SIPH = 0x154,
HVIEN = 0x608,
HVICTL = 0x609,
HVIPRIO1 = 0x646,
HVIPRIO2 = 0x647,
VSISELECT = 0x250,
VSIREG = 0x251,
VSIREG2 = 0x252,
VSIREG3 = 0x253,
VSIREG4 = 0x255,
VSIREG5 = 0x256,
VSIREG6 = 0x257,
VSTOPEI = 0x25c,
VSTOPI = 0xeb0,
HIDELEGH = 0x613,
HVIENH = 0x618,
HVIPH = 0x655,
HVIPRIO1H = 0x656,
HVIPRIO2H = 0x657,
VSIEH = 0x214,
VSIPH = 0x254,
// Stateen CSR
SSTATEEN0 = 0x10c,
SSTATEEN1 = 0x10d,
SSTATEEN2 = 0x10e,
SSTATEEN3 = 0x10f,
MSTATEEN0 = 0x30c,
MSTATEEN1 = 0x30d,
MSTATEEN2 = 0x30e,
MSTATEEN3 = 0x30f,
MSTATEEN0H = 0x31c,
MSTATEEN1H = 0x31d,
MSTATEEN2H = 0x31e,
MSTATEEN3H = 0x31f,
HSTATEEN0 = 0x60c,
HSTATEEN1 = 0x60d,
HSTATEEN2 = 0x60e,
HSTATEEN3 = 0x60f,
HSTATEEN0H = 0x61c,
HSTATEEN1H = 0x61d,
HSTATEEN2H = 0x61e,
HSTATEEN3H = 0x61f,
// Entropy source
SEED = 0x015,
// Tenstorrent Ascalon CSRs
PMACFG0 = 0x7e0, // Physical memory protection
PMACFG15 = 0x7ef,
C_MATP = 0x7c7, // Machine address translation and protection
MAX_CSR_ = 0xfff,
MIN_CSR_ = 0 // csr with smallest number
};
/// Return true if first csr number is smaller than the second.
inline bool operator< (CsrNumber a, CsrNumber b)
{ return static_cast<unsigned>(a) < static_cast<unsigned>(b); }
/// Return true if first csr number is smaller than or equal the second.
inline bool operator<= (CsrNumber a, CsrNumber b)
{ return static_cast<unsigned>(a) <= static_cast<unsigned>(b); }
/// Return true if first csr number is greater than the second.
inline bool operator> (CsrNumber a, CsrNumber b)
{ return static_cast<unsigned>(a) > static_cast<unsigned>(b); }
/// Return true if first csr number is greater than or equal the second.
inline bool operator>= (CsrNumber a, CsrNumber b)
{ return static_cast<unsigned>(a) >= static_cast<unsigned>(b); }
template <typename URV>
class CsRegs;
/// Model a control and status register. The template type URV
/// (unsigned register value) is the type of the register value. It
/// must be uint32_t for 32-bit implementations and uint64_t for
/// 64-bit.
template <typename URV>
class Csr
{
public:
/// Default constructor.
Csr()
{ valuePtr_ = &value_; }
/// Constructor. The mask indicates which bits are writable: A zero bit
/// in the mask corresponds to a non-writable (preserved) bit in the
/// register value. To make the whole register writable, set mask to
/// all ones.
Csr(std::string name, CsrNumber number, bool mandatory,
bool implemented, URV value, URV writeMask = ~URV(0))
: name_(std::move(name)), number_(unsigned(number)), mandatory_(mandatory),
implemented_(implemented), initialValue_(value), value_(value),
writeMask_(writeMask), pokeMask_(writeMask)
{
valuePtr_ = &value_;
privMode_ = PrivilegeMode((number_ & 0x300) >> 8);
}
/// Copy constructor is not available.
Csr(const Csr<URV>& other) = delete;
void operator=(const Csr<URV>& other) = delete;
/// Return lowest privilege mode that can access the register.
/// Bits 9 and 8 of the register number encode the privilege mode.
/// Privilege of user level performance counter is modified by
/// mcounteren.
PrivilegeMode privilegeMode() const
{ return privMode_; }
/// Return true if register is read-only. Bits ten and eleven of
/// the register number denote read-only when both one and read-write
/// otherwise.
bool isReadOnly() const
{ return (number_ & 0xc00) == 0xc00 or number_ == unsigned(CsrNumber::HGEIP); }
/// Return true if register is implemented.
bool isImplemented() const
{ return implemented_ and not userDisabled_; }
/// Return true if register is mandatory (not optional).
bool isMandatory() const
{ return mandatory_; }
/// Return true if this is a hypervisor register.
bool isHypervisor() const
{ return hyper_; }
/// Return true if this is a supervisor register that maps to a
/// virtual supervisor register (e.g. sstatus maps to vsstatus).
bool mapsToVirtual() const
{ return mapsToVirtual_; }
/// Return true if this is a high-half of a CSR (e.g. MSTATUSH is
/// the high half of MSTATUS).
bool isHighHalf() const
{ return high_; }
/// Mark this CSR as a high-half (e.g. MSTATUSH is a high half).
void markAsHighHalf(bool flag)
{ high_ = flag; }
/// Mark this CSR as belonhing to the AIA extension.
void markAia(bool flag)
{ aia_ = flag; }
/// Return true if this an AIA CSR.
bool isAia() const
{ return aia_; }
/// Return true if this register has been marked as a debug-mode
/// register.
bool isDebug() const
{ return debug_; }
/// Return true if this register is shared among harts.
bool isShared() const
{ return shared_; }
/// Return the current value of this register.
URV read() const
{ return *valuePtr_ & readMask_; }
/// Return the write-mask associated with this register. A
/// register value bit is writable by the write method if and only
/// if the corresponding bit in the mask is 1; otherwise, the bit
/// is preserved.
URV getWriteMask() const
{ return writeMask_; }
/// Return the poke mask associated with this register. A register
/// value bit is modifiable if and only if the corresponding bit
/// in the mask is 1; otherwise, the bit is preserved. The write
/// mask is used by the CSR write instructions. The poke mask
/// allows the caller to change bits that are read only for CSR
/// instructions but are modifiable by the hardware.
URV getPokeMask() const
{ return pokeMask_; }
URV getReadMask() const
{ return readMask_; }
/// Return the reset value of this CSR.
URV getResetValue() const
{ return initialValue_; }
/// Return the number of this register.
CsrNumber getNumber() const
{ return CsrNumber(number_); }
/// Return the name of this register.
std::string_view getName() const
{ return name_; }
/// Register a pre-poke call back which will get invoked with CSR and
/// poked value.
void registerPrePoke(std::function<void(Csr<URV>&, URV&)> func)
{ prePoke_.push_back(func); }
/// Register a pre-write call back which will get invoked with
/// CSR and written value.
void registerPreWrite(std::function<void(Csr<URV>&, URV&)> func)
{ preWrite_.push_back(func); }
/// Register a post-poke call back which will get invoked with CSR and
/// poked value.
void registerPostPoke(std::function<void(Csr<URV>&, URV)> func)
{ postPoke_.push_back(func); }
/// Register a post-write call back which will get invoked with
/// CSR and written value.
void registerPostWrite(std::function<void(Csr<URV>&, URV)> func)
{ postWrite_.push_back(func); }
/// Register a post-reset call back.
void registerPostReset(std::function<void(Csr<URV>&)> func)
{ postReset_.push_back(func); }
/// Get field width of CSR
unsigned width(std::string_view field) const
{
for (auto& f : fields_)
{
if (f.field == field)
return f.width;
}
}
struct Field
{
std::string field;
unsigned width;
};
// Returns CSR fields
const std::vector<Field>& fields() const
{ return fields_; }
protected:
friend class CsRegs<URV>;
friend class Hart<URV>;
/// Define the privilege mode of this CSR.
void definePrivilegeMode(PrivilegeMode mode)
{ privMode_ = mode; }
/// Associate given location with the value of this CSR. The
/// previous value of the CSR is lost. If given location is null
/// then the default location defined in this object is restored.
void tie(URV* location)
{ valuePtr_ = location ? location : &value_; }
/// Reset to initial (power-on) value.
void reset()
{
*valuePtr_ = initialValue_;
for (auto func : postReset_)
func(*this);
}
/// Change the privilege required to access the register. This is
/// used to control access to the user-level performance counters.
void setPrivilegeMode(PrivilegeMode mode)
{ privMode_ = mode; }
/// Configure.
void config(std::string name, CsrNumber num, bool mandatory,
bool implemented, URV value, URV writeMask, URV pokeMask)
{
name_ = std::move(name);
number_ = unsigned(num);
mandatory_ = mandatory;
implemented_ = implemented;
initialValue_ = value;
writeMask_ = writeMask;
pokeMask_ = pokeMask;
*valuePtr_ = value;
}
/// Define the mask used by the poke method to write this
/// register. The mask defined the register bits that are
/// modifiable (even though such bits may not be writable using a
/// CSR instruction). For example, the meip bit (of the mip CSR)
/// is not writable using a CSR instruction but is modifiable.
void setPokeMask(URV mask)
{ pokeMask_ = mask; }
/// Mark register as a debug-mode register. Accessing a debug-mode
/// register when the processor is not in debug mode will trigger an
/// illegal instruction exception.
void setIsDebug(bool flag)
{ debug_ = flag; }
/// Mark register as shared among harts.
void setIsShared(bool flag)
{ shared_ = flag; }
/// Mark register as implemented.
void setImplemented(bool flag)
{ implemented_ = flag; }
/// Mark register as disabled by user configuration.
void setUserDisabled(bool flag)
{ userDisabled_ = flag; }
/// Set initial value.
void setInitialValue(URV v)
{ initialValue_ = v; }
/// Mark register as defined.
void setDefined(bool flag)
{ defined_ = flag; }
/// True if this is a hypervisor register. Hypervisor registers
/// are not available in VS/VU (virtual-supervisor) mode.
void setHypervisor(bool flag)
{ hyper_ = flag; }
void setMapsToVirtual(bool flag)
{ mapsToVirtual_ = flag; }
bool isDefined() const
{ return defined_; }
void pokeNoMask(URV v)
{ *valuePtr_ = v; }
void setWriteMask(URV mask)
{ writeMask_ = mask; }
void setReadMask(URV mask)
{ readMask_ = mask; }
/// Set the value of this register to the given value x honoring the write and poke
/// masks (defined at construction): Set the ith bit of this register to the ith bit
/// of the given value x if the ith bit of the write and poke masks is 1; otherwise,
/// leave the ith bit unmodified. This is the interface used by the CSR instructions.
void write(URV x)
{
if (not hasPrev_)
{
prev_ = *valuePtr_;
hasPrev_ = true;
}
for (auto func : preWrite_)
func(*this, x);
URV mask = pokeMask_ & writeMask_;
URV newVal = (x & mask) | (*valuePtr_ & ~mask);
*valuePtr_ = newVal;
for (auto func : postWrite_)
func(*this, newVal);
}
/// Similar to the write method but honoring only the poke mask. This is the interface
/// used by non-csr instructions to change modifiable (but not writable through CSR
/// instructions) bits of this register.
void poke(URV x)
{
for (auto func : prePoke_)
func(*this, x);
URV newVal = (x & pokeMask_) | (*valuePtr_ & ~pokeMask_);
*valuePtr_ = newVal;
for (auto func : postPoke_)
func(*this, newVal);
}
/// Return the value of this register before last sequence of
/// writes. Return current value if no writes since
/// clearLastWritten.
URV prevValue() const
{ return hasPrev_? prev_ : read(); }
/// Clear previous value recorded by first write since
/// clearLastWritten.
void clearLastWritten()
{ hasPrev_ = false; }
void setFields(const std::vector<Field>& fields)
{ fields_ = fields; }
std::vector<Field> getFields() const
{ return fields_; }
bool field(std::string_view field, URV& val) const
{
unsigned start = 0;
for (auto& f : fields_)
{
if (f.field == field)
{
URV mask = ((1 << f.width) - 1) << start;
val = (value_ & mask) >> start;
return true;
}
start += f.width;
}
return false;
}
private:
std::string name_;
unsigned number_ = 0;
bool mandatory_ = false; // True if mandated by architecture.
bool implemented_ = false; // True if register is implemented.
bool userDisabled_ = false; // True if disabled by user in config file.
bool hyper_ = false; // True if hypervisor CSR.
bool mapsToVirtual_ = false; // True if CSR maps to a virtual supervisor CSR.
bool defined_ = false;
bool debug_ = false; // True if this is a debug-mode register.
bool shared_ = false; // True if this is shared among harts.
bool aia_ = false; // True if this an AIA CSR.
URV initialValue_ = 0;
PrivilegeMode privMode_ = PrivilegeMode::Machine;
URV value_ = 0;
URV prev_ = 0;
bool hasPrev_ = false;
bool high_ = false;
// This will point to value_ except when shadowing the value of
// some other register.
URV* valuePtr_ = nullptr;
URV writeMask_ = ~URV(0);
URV pokeMask_ = ~URV(0);
URV readMask_ = ~URV(0); // Used for sstatus.
std::vector<std::function<void(Csr<URV>&, URV)>> postPoke_;
std::vector<std::function<void(Csr<URV>&, URV)>> postWrite_;
std::vector<std::function<void(Csr<URV>&, URV&)>> prePoke_;
std::vector<std::function<void(Csr<URV>&, URV&)>> preWrite_;
std::vector<std::function<void(Csr<URV>&)>> postReset_;
// Optionally define fields within a CSR with name and widths
std::vector<Field> fields_;
};
/// Model the control and status register set.
template <typename URV>
class CsRegs
{
public:
friend class Hart<uint32_t>;
friend class Hart<uint64_t>;
CsRegs();
~CsRegs();
CsRegs(const CsRegs &) = delete;
/// Return pointer to the control-and-status register
/// corresponding to the given name or nullptr if no such
/// register.
Csr<URV>* findCsr(std::string_view name);
/// Return pointer to the control-and-status register
/// corresponding to the given number or nullptr if no such
/// register.
Csr<URV>* findCsr(CsrNumber number);
const Csr<URV>* findCsr(CsrNumber number) const;
/// Read given CSR on behalf of a CSR instruction (e.g. csrrw)
/// into value returning true on success. Return false leaving
/// value unmodified if there is no CSR with the given number or
/// if the CSR is not implemented or if it is not accessible by
/// the given mode.
bool read(CsrNumber number, PrivilegeMode mode, URV& value) const;
/// Write given CSR on behalf of a CSR instruction (e.g. csrrw)
/// returning true on success. Return false writing nothing if
/// there is no CSR with the given number or if the CSR is not
/// implemented or if it is not accessible by the given mode.
bool write(CsrNumber number, PrivilegeMode mode, URV value);
/// Return true if given register is writable by a CSR instruction
/// in the given privilege and virtual mode.
bool isWriteable(CsrNumber number, PrivilegeMode mode, bool virtMode) const;
/// Return true if this is a high-half of a CSR (e.g. MSTATUSH is
/// the high half of MSTATUS).
bool isHighHalf(CsrNumber number) const
{
const Csr<URV>* csr = getImplementedCsr(number, virtMode_);
return csr ? csr->isHighHalf() : false;
}
/// Return true if given register is readable by a CSR instruction
/// in the given privlege and virtual modes.
bool isReadable(CsrNumber number, PrivilegeMode pm, bool virtMode) const;
/// Fill the nums vector with the numbers of the CSRs written by
/// the last instruction.
void getLastWrittenRegs(std::vector<CsrNumber>& csrNums) const
{
csrNums = lastWrittenRegs_;
}
/// Fill the nums vector with the numbers of the CSRs and triggers
/// written by the last instruction.
void getLastWrittenRegs(std::vector<CsrNumber>& csrNums,
std::vector<unsigned>& triggerNums) const
{
csrNums = lastWrittenRegs_;
triggers_.getLastWrittenTriggers(triggerNums);
}
/// Return the previous (prior to last executed instructoin) value of the given csr.
URV lastCsrValue(CsrNumber csrn)
{
auto csr = findCsr(csrn);
return csr->prevValue();
}
/// Associate an IMSIC with this register file.
void attachImsic(std::shared_ptr<TT_IMSIC::Imsic> imsic)
{ imsic_ = imsic; }
bool aiaEnabled() const { return aiaEnabled_; }
protected:
/// Advance a csr number by the given amount (add amount to number).
static CsrNumber advance(CsrNumber csrn, uint32_t amount)
{ return CsrNumber(uint32_t(csrn) + amount); }
/// Advance a csr number by the given amount (add amount to number).
static CsrNumber advance(CsrNumber csrn, int32_t amount)
{ return CsrNumber(uint32_t(csrn) + amount); }
/// Similar to read but returned value is sign extended: sign bit is bit
/// corresponding to most significant set bit in write mask of CSR.
bool readSignExtend(CsrNumber number, PrivilegeMode mode, URV& value) const;
/// Define csr with given name and number. Return pointer to csr
/// on success or nullptr if given name is already in use or if the
/// csr number is out of bounds or if it is associated with an
/// already defined CSR.
Csr<URV>* defineCsr(std::string name, CsrNumber number,
bool mandatory, bool implemented, URV value,
URV writeMask, URV pokeMask, bool quiet = false);
/// Return pointer to CSR with given number. Return nullptr if
/// number is out of bounds or if corresponding CSR is not
/// implemented.
Csr<URV>* getImplementedCsr(CsrNumber num)
{
size_t ix = size_t(num);
if (ix >= regs_.size()) return nullptr;
Csr<URV>* csr = ®s_.at(ix);
return csr->isImplemented() ? csr : nullptr;
}
/// Return pointer to CSR with given number. Return nullptr if
/// number is out of bounds or if corresponding CSR is not
/// implemented.
const Csr<URV>* getImplementedCsr(CsrNumber num) const
{
size_t ix = size_t(num);
if (ix >= regs_.size()) return nullptr;
const Csr<URV>* csr = ®s_.at(ix);
return csr->isImplemented() ? csr : nullptr;
}
/// Similar to getImplementedCsr except that when virtaulMode is true:
/// Supervisor CSRs are remapped to the virtual supervisor counterparts.
Csr<URV>* getImplementedCsr(CsrNumber num, bool virtualMode);
/// Const version.
const Csr<URV>* getImplementedCsr(CsrNumber num, bool virtualMode) const;
/// Enable/disable matching all addresses in a load/store access
/// for debug triggering.
void configAllLdStAddrTrigger(bool flag)
{ triggers_.enableAllLdStAddrMatch(flag); }
/// Enable/disable matching all addresses in a instruction fetch
/// access for debug triggering.
void configAllInstAddrTrigger(bool flag)
{ triggers_.enableAllInstAddrMatch(flag); }
/// Enable triggers.
void enableSdtrig(bool flag);
/// Enable STEE (static trusted execution env)