forked from chipsalliance/VeeR-ISS
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTriggers.hpp
1104 lines (906 loc) · 38.5 KB
/
Triggers.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 <array>
#include <string>
#include <bit>
#include "trapEnums.hpp"
namespace WdRiscv
{
/// Trigger timing control: Before instruction or after.
enum class TriggerTiming { Before, After };
/// Trigger type.
enum class TriggerType : uint32_t {
None = 0, Legacy = 1, AddrData = 2, Mcontrol = AddrData, Icount = 3, Itrigger = 4,
Etrigger = 5, Mcontrol6 = 6, Tmext = 7, Reserved0 = 8, Reserved1 = 9, Reserved2 = 10,
Reserved3 = 11, Custom0 = 12, Custom1 = 13, Custom2 = 14, Disabled = 15
};
enum class TriggerOffset { Tdata1 = 0, Tdata2 = 1, Tdata3 = 2, Tinfo = 3 };
template <typename URV>
struct Mcontrol;
template <typename URV>
struct Mcontrol6;
/// Bit fields of tinfo trigger register component.
struct Tinfo
{
unsigned info_ : 16; // Bits 15-0 : Supported types in type field of TDATA1.
unsigned : 8; // Bits 23-16: Reserved -- hardwired to zero.
unsigned version_ : 8; // Bits 24-31: Version
};
/// Bit fields of mcontrol trigger register component. 32-bit version.
template <>
struct Mcontrol<uint32_t>
{
unsigned load_ : 1; // Bit 0 : trigger on load
unsigned store_ : 1; // Bit 1 : trigger on store
unsigned execute_ : 1; // Bit 2 : trigger on instruction
unsigned u_ : 1; // Bit 3 : enable in user mode
unsigned s_ : 1; // Bit 4 : enable in supervisor mode
unsigned : 1; // Bit 5 : reserved: hardwired to zero
unsigned m_ : 1; // Bit 6 : enable in machine mode
unsigned match_ : 4; // Bits 10-7 : controls what is considered to be a match
unsigned chain_ : 1; // Bit 11 :
unsigned action_ : 4; // Bits 15-12:
unsigned sizelo_ : 2; // Bits 17-16:
unsigned timing_ : 1; // Bit 18 :
unsigned select_ : 1; // Bit 19 :
unsigned hit_ : 1; // Bit 20 :
unsigned maskMax_ : 6; // Bit 26-21:
unsigned dmode_ : 1; // Bit 27 : trigger writable only in debug mode.
unsigned type_ : 4; // Bits 31-28:
} __attribute__((packed));
/// Bit fields of mcontrol trigger register component. 64-bit version.
template <>
struct Mcontrol<uint64_t>
{
unsigned load_ : 1; // Bit 0 : trigger on load
unsigned store_ : 1; // Bit 1 : trigger on store
unsigned execute_ : 1; // Bit 2 : trigger on instruction
unsigned u_ : 1; // Bit 3 : enable in user mode
unsigned s_ : 1; // Bit 4 : enable in supervisor mode
unsigned : 1; // Bit 5 : reserved: hardwired to zero
unsigned m_ : 1; // Bit 6 : enable in machine mode
unsigned match_ : 4; // Bits 10-7 : controls what is considered to be a match
unsigned chain_ : 1; // Bit 11 :
unsigned action_ : 4; // Bits 15-12:
unsigned sizelo_ : 2; // Bits 17-16:
unsigned timing_ : 1; // Bit 18 :
unsigned select_ : 1; // Bit 19 :
unsigned hit_ : 1; // Bit 20 :
unsigned sizehi_ : 2; // Bits 22-21:
unsigned : 30; // Bits 52-23:
unsigned maskMax_ : 6; // Bits 58-53:
unsigned dmode_ : 1; // Bit 59 : trigger writable only in debug mode.
unsigned type_ : 4; // Bits 63-60:
} __attribute__((packed));
/// Bit fields of mcontrol6 trigger register component. 32-bit version.
template <>
struct Mcontrol6<uint32_t>
{
unsigned load_ : 1; // Bit 0 : trigger on load
unsigned store_ : 1; // Bit 1 : trigger on store
unsigned execute_ : 1; // Bit 2 : trigger on instruction
unsigned u_ : 1; // Bit 3 : enable in user mode
unsigned s_ : 1; // Bit 4 : enable in supervisor mode
unsigned unceren_ : 1; // Bit 5 : uncertain
unsigned m_ : 1; // Bit 6 : enable in machine mode
unsigned match_ : 4; // Bits 10-7 : controls what is considered to be a match
unsigned chain_ : 1; // Bit 11 :
unsigned action_ : 4; // Bits 15-12:
unsigned size_ : 3; // Bits 18-16:
unsigned : 2; // Bits 20-19: reserved
unsigned select_ : 1; // Bit 21 :
unsigned hit0_ : 1; // Bit 22 :
unsigned vu_ : 1; // Bit 23 :
unsigned vs_ : 1; // Bit 24 :
unsigned hit1_ : 1; // Bit 25 :
unsigned uncer_ : 2; // Bits 26 :
unsigned dmode_ : 1; // Bit 27 : trigger writable only in debug mode.
unsigned type_ : 4; // Bits 31-28:
} __attribute__((packed));
/// Bit fields of mcontrol6 trigger register component. 64-bit version.
template <>
struct Mcontrol6<uint64_t>
{
unsigned load_ : 1; // Bit 0 : trigger on load
unsigned store_ : 1; // Bit 1 : trigger on store
unsigned execute_ : 1; // Bit 2 : trigger on instruction
unsigned u_ : 1; // Bit 3 : enable in user mode
unsigned s_ : 1; // Bit 4 : enable in supervisor mode
unsigned unceren_ : 1; // Bit 5 : uncertain
unsigned m_ : 1; // Bit 6 : enable in machine mode
unsigned match_ : 4; // Bits 10-7 : controls what is considered to be a match
unsigned chain_ : 1; // Bit 11 :
unsigned action_ : 4; // Bits 15-12:
unsigned size_ : 3; // Bits 18-16:
unsigned : 2; // Bits 20-19: reserved
unsigned select_ : 1; // Bit 21 :
unsigned hit0_ : 1; // Bit 22 :
unsigned vu_ : 1; // Bit 23 :
unsigned vs_ : 1; // Bit 24 :
unsigned hit1_ : 1; // Bit 25 :
unsigned uncer_ : 2; // Bits 26 :
unsigned : 32; // Bits 58-27:
unsigned dmode_ : 1; // Bit 59 : trigger writable only in debug mode.
unsigned type_ : 4; // Bits 63-60:
} __attribute__((packed));
/// Bit fields for Icount trigger register component.
template <typename URV>
struct Icount
{
unsigned action_ : 6;
unsigned u_ : 1;
unsigned s_ : 1;
unsigned pending_ : 1;
unsigned m_ : 1;
unsigned count_ : 14;
unsigned hit_ : 1;
unsigned vu_ : 1;
unsigned vs_ : 1;
URV : 8*sizeof(URV) - 32; // Zero bits
unsigned dmode_ : 1; // Trigger writable only in debug mode.
unsigned type_ : 4;
} __attribute__((packed));
/// Bif tields for Etrigger trigger register component.
template <typename URV>
struct Etrigger
{
unsigned action_ : 6;
unsigned u_ : 1;
unsigned s_ : 1;
unsigned : 1; // Reserved -- zero.
unsigned m_ : 1;
unsigned : 1; // Reserved -- zero.
unsigned vu_ : 1;
unsigned vs_ : 1;
uint64_t : 8*sizeof(URV) - 19; // Reserved -- zero.
unsigned hit : 1;
unsigned dmode : 1;
unsigned type : 4;
} __attribute((packed));
/// Bif tields for Itrigger trigger register component.
template <typename URV>
struct Itrigger
{
unsigned action_ : 6;
unsigned u_ : 1;
unsigned s_ : 1;
unsigned : 1; // Reserved -- zero.
unsigned m_ : 1;
unsigned nmi_ : 1;
unsigned vu_ : 1;
unsigned vs_ : 1;
uint64_t : sizeof(URV)*8 - 19; // Reserved -- zero.
unsigned hit : 1;
unsigned dmode : 1;
unsigned type : 4;
} __attribute((packed));
/// Union to pack/unpack TINFO trigger register value
union TinfoBits
{
TinfoBits(uint64_t value) :
value_(value)
{ }
unsigned info() const { return tinfo_.info_; }
unsigned version() const { return tinfo_.version_; }
uint64_t value_ = 0;
Tinfo tinfo_;
};
/// Union to pack/unpack TDATA1 trigger register value
template <typename URV>
union Data1Bits
{
Data1Bits(URV value) :
value_(value)
{ }
/// Return trigger type field of Tdata1.
TriggerType type() const { return TriggerType(mcontrol_.type_); }
/// Return true if type is None or Disabled.
bool isDisabled() const
{ return type() == TriggerType::None or type() == TriggerType::Disabled; }
bool isMcontrol() const { return type() == TriggerType::Mcontrol; }
bool isMcontrol6() const { return type() == TriggerType::Mcontrol6; }
bool isAddrData() const { return isMcontrol() or isMcontrol6(); }
bool isInstCount() const { return type() == TriggerType::Icount; }
bool isEtrigger() const { return type() == TriggerType::Etrigger; }
bool isItrigger() const { return type() == TriggerType::Itrigger; }
/// Return true if trigger is writable only in debug mode.
bool dmodeOnly() const { return mcontrol_.dmode_; }
/// Set the type field of tdata1.
void setType(TriggerType type)
{ mcontrol_.type_ = unsigned(type); }
template <typename T>
const T& mcontrol() const
{
if constexpr (std::is_same_v<T, decltype(mcontrol_)>)
return mcontrol_;
else
return mcontrol6_;
}
URV value_ = 0;
Mcontrol<URV> mcontrol_;
Mcontrol6<URV> mcontrol6_;
Icount<URV> icount_;
Etrigger<URV> etrigger_;
Itrigger<URV> itrigger_;
};
template <typename URV>
class Triggers;
/// Model a RISCV trigger.
template <typename URV>
class Trigger
{
public:
friend class Triggers<URV>;
enum class Select { MatchAddress, MatchData };
enum class Action { RaiseBreak, EnterDebug, StartTrace, StopTrace,
EmitTrace, Reserved, External0, External1 };
enum class Chain { No, Yes };
enum class Match { Equal = 0, Masked = 1, GE = 2, LT = 3, MaskHighEqualLow = 4,
MaskLowEqualHigh = 5, NotEqual = 8, NotMasked = 9,
NotMaskHighEqualLow = 12, NotMaskLowEqualHigh = 13 };
/// Constructor.
Trigger(URV data1 = 0, URV data2 = 0, URV /*data3*/ = 0,
URV mask1 = ~URV(0), URV mask2 = ~URV(0), URV mask3 = 0);
/// Return the type of this trigger.
TriggerType type() const
{ return data1_.type(); }
/// Read the tdata1 register of the trigger. This is typically the control register of
/// the trigger.
URV readData1() const
{ return modifiedT1_? prevData1_ : data1_.value_; }
/// Read the tdata2 register of the trigger. This is typically the
/// target value of the trigger.
URV readData2() const
{ return data2_; }
/// Read the tdata3 register of the trigger (currently unused).
URV readData3() const
{ return data3_; }
/// Read the tinfo register ot the trigger.
URV readInfo() const
{ return info_.value_; }
/// Write the tdata1 register of the trigger. This is the interface for CSR
/// instructions.
bool writeData1(bool debugMode, URV val)
{
if (isDebugModeOnly() and not debugMode)
return false;
URV mask = data1WriteMask_;
if (not debugMode) // dmode bit writable only in debug mode
mask &= ~(URV(1) << (8*sizeof(URV) - 5));
if (not modifiedT1_)
prevData1_ = data1_.value_;
// Writing 0 (None) into type is changed to 15 (Disabled). Section 5.7.2 of spec.
Data1Bits<URV> valBits{val};
if (valBits.type() == TriggerType::None)
valBits.setType(TriggerType::Disabled);
val = valBits.value_;
data1_.value_ = (val & mask) | (data1_.value_ & ~mask);
modifiedT1_ = true;
if (data1_.isAddrData())
{
if (not data1_.dmodeOnly())
data1_.mcontrol_.action_ = 0;
if (data1_.isMcontrol())
data1_.mcontrol_.maskMax_ = std::countr_zero(napotMask_) + 1;
}
else if (data1_.isInstCount())
{
if (not data1_.dmodeOnly())
data1_.icount_.action_ = 0;
}
return true;
}
/// Write the tdata2 register of the trigger. This is the interface for CSR
/// instructions.
bool writeData2(bool debugMode, URV value)
{
if (isDebugModeOnly() and not debugMode)
return false;
data2_ = (value & data2WriteMask_) | (data2_ & ~data2WriteMask_);
modifiedT2_ = true;
if (data1_.isMcontrol6() and data2_ == ~URV(0))
data2_ = napotMask_;
updateCompareMask();
return true;
}
/// Write the tdata3 register of the trigger. This is the interface for CSR
/// instructions.
bool writeData3(bool debugMode, URV value)
{
if (isDebugModeOnly() and not debugMode)
return false;
data3_ = (value & data3WriteMask_) | (data3_ & ~data3WriteMask_);
modifiedT3_ = true;
return true;
}
/// Write the tinfo register of the trigger. This is the interface for CSR
/// instructions.
bool writeInfo(bool debugMode, URV value)
{
if (isDebugModeOnly() and not debugMode)
return false;
info_.value_ = (value & infoWriteMask_) | (info_.value_ & ~infoWriteMask_);
modifiedInfo_ = true;
return true;
}
/// Poke tdata1. This allows writing of modifiable bits that are read-only to the CSR
/// instructions.
void pokeData1(URV x)
{
URV val = (x & data1PokeMask_) | (data1_.value_ & ~data1PokeMask_);
data1_.value_ = val;
// Configuration dmode==0 and action==1 is not allowed.
if (data1_.mcontrol_.dmode_ == 0 and data1_.mcontrol_.action_ == 1)
data1_.mcontrol_.action_ = 0;
}
/// Poke tdata2. This allows writing of modifiable bits that are read-only to the CSR
/// instructions.
void pokeData2(URV x)
{
data2_ = (x & data2PokeMask_) | (data2_ & ~data2PokeMask_);
if (data1_.isMcontrol6() and data2_ == ~URV(0))
data2_ = napotMask_;
updateCompareMask();
}
/// Poke tdata3. This allows writing of modifiable bits that are read-only to the CSR
/// instructions.
void pokeData3(URV x)
{ data3_ = (x & data3PokeMask_) | (data3_ & ~data3PokeMask_); }
/// Poke tinfo. This allows writing of modifiable bits that are read-only to the CSR
/// instructions.
void pokeInfo(URV x)
{ info_.value_ = (x & infoPokeMask_) | (info_.value_ & ~infoPokeMask_); }
void configData1(URV reset, URV mask, URV pokeMask)
{ data1Reset_ = reset; data1_.value_ = reset; data1WriteMask_ = mask; data1PokeMask_ = pokeMask;}
void configData2(URV reset, URV mask, URV pokeMask)
{ data2Reset_ = reset; data2_ = reset; data2WriteMask_ = mask; data2PokeMask_ = pokeMask;}
void configData3(URV reset, URV mask, URV pokeMask)
{ data3Reset_ = reset; data3_ = reset; data3WriteMask_ = mask; data3PokeMask_ = pokeMask;}
void configInfo(URV reset, URV mask, URV pokeMask)
{ infoReset_ = reset; info_ = reset; infoWriteMask_ = mask; infoPokeMask_ = pokeMask;}
/// Reset trigger.
void reset()
{
data1_.value_ = data1Reset_; data2_ = data2Reset_; data3_ = data3Reset_;
writeData2(true, data2Reset_); // Define compare mask
}
/// Return true if this trigger is enabled.
bool isEnabled() const
{
if (data1_.isAddrData())
{
if (data1_.isMcontrol())
return data1_.mcontrol_.m_ or data1_.mcontrol_.s_ or data1_.mcontrol_.u_;
else
return data1_.mcontrol6_.m_ or data1_.mcontrol6_.s_ or data1_.mcontrol6_.u_ or
data1_.mcontrol6_.vs_ or data1_.mcontrol6_.vu_;
}
if (data1_.isInstCount())
return data1_.icount_.m_ or data1_.icount_.s_ or data1_.icount_.u_;
return false;
}
/// Return true if trigger is writable only in debug mode.
bool isDebugModeOnly() const
{ return data1_.dmodeOnly(); }
/// Return true if this is an instruction (execute) trigger.
bool isInst() const
{ return data1_.isAddrData() and data1_.mcontrol_.execute_; }
/// Return true if this trigger will cause the processor to enter debug mode on a hit.
bool isEnterDebugOnHit() const
{
if (data1_.isAddrData())
return Action(data1_.mcontrol_.action_) == Action::EnterDebug;
if (data1_.isInstCount())
return Action(data1_.icount_.action_) == Action::EnterDebug;
return false;
}
/// Return true if this trigger is enabled for loads (or stores if
/// isLoad is false), for addresses, for the given timing and if
/// it matches the given data address. Return false otherwise.
bool matchLdStAddr(URV address, unsigned size, TriggerTiming timing, bool isLoad,
PrivilegeMode mode, bool virtMode) const;
/// Return true if this trigger is enabled for loads (or stores if
/// isLoad is false), for data, for the given timing and if it
/// matches the given value address. Return false otherwise.
bool matchLdStData(URV value, TriggerTiming timing, bool isLoad,
PrivilegeMode mode, bool virtMode) const;
/// Return true if this trigger is enabled for instruction
/// addresses (execution), for the given timing and if it matches
/// the given address. Return false otherwise.
bool matchInstAddr(URV address, unsigned size, TriggerTiming timing,
PrivilegeMode mode, bool virtMode) const;
/// Return true if this trigger is enabled for instruction opcodes
/// (execution), for the given timing and if it matches the given
/// opcode. Return false otherwise.
bool matchInstOpcode(URV opcode, TriggerTiming timing,
PrivilegeMode mode, bool virtMode) const;
/// Return true if this trigger is enabled for given mode.
/// Return false otherwise. This is called for both
/// instruction retire and trap scenarios.
bool matchInstCount(PrivilegeMode mode, bool virtMode)
{
if (not data1_.isInstCount())
return false; // Not an icount trigger.
Icount<URV>& icount = data1_.icount_;
if (mode == PrivilegeMode::Machine and not icount.m_)
return false; // Trigger is not enabled.
if (mode == PrivilegeMode::Supervisor and not virtMode and not icount.s_)
return false; // Trigger is not enabled.
if (mode == PrivilegeMode::User and not virtMode and not icount.u_)
return false; // Trigger is not enabled.
if (mode == PrivilegeMode::Supervisor and virtMode and not icount.vs_)
return false; // Trigger is not enabled.
if (mode == PrivilegeMode::User and virtMode and not icount.vu_)
return false; // Trigger is not enabled.
if (mode == PrivilegeMode::Reserved)
return false;
return true;
}
/// If this trigger is enabled and is of type icount, then make it
/// count down returning true if its value becomes zero. Return
/// false otherwise.
bool instCountdown(PrivilegeMode mode, bool virtMode)
{
if (not matchInstCount(mode, virtMode))
return false;
Icount<URV>& icount = data1_.icount_;
icount.count_ = icount.count_? icount.count_ - 1 : 0;
icount.pending_ = not icount.count_;
return icount.pending_;
}
/// Perform a match on the given item (maybe an address or a value) and the data2
/// component of this trigger according to the match variable.
bool doMatch(URV item, Match match) const;
/// Set the hit bit of this trigger. For a chained trigger, this is to be called only
/// if all the triggers in the chain have tripped.
void setHit(bool flag)
{
if (data1_.isAddrData())
{
if (not modifiedT1_)
prevData1_ = data1_.value_;
if (data1_.isMcontrol())
data1_.mcontrol_.hit_ = flag;
else
data1_.mcontrol6_.hit0_ = flag; // only implement before
modifiedT1_ = true;
}
if (data1_.isInstCount())
{
if (not modifiedT1_)
prevData1_ = data1_.value_;
data1_.icount_.hit_ = flag;
modifiedT1_ = true;
}
}
/// Return the hit bit of this trigger.
bool getHit() const
{
if (data1_.isAddrData())
return data1_.isMcontrol()? data1_.mcontrol_.hit_ : data1_.mcontrol6_.hit0_;
if (data1_.isInstCount())
return data1_.icount_.hit_;
return false;
}
/// Return the chain bit of this trigger or false if this trigger has
/// no chain bit.
bool getChain() const
{
if (data1_.isAddrData())
return data1_.mcontrol_.chain_;
return false;
}
/// Return the timing of this trigger.
TriggerTiming getTiming() const
{
if (data1_.isAddrData())
return data1_.isMcontrol()? TriggerTiming(data1_.mcontrol_.timing_) : TriggerTiming::Before;
return TriggerTiming::After; // icount has "after" timing.
}
/// Return true if the chain of this trigger has tripped.
bool hasTripped() const
{ return chainHit_; }
/// Mark this trigger as tripped.
void setTripped(bool flag)
{ chainHit_ = flag; }
/// Return the action fields of the trigger.
Action getAction() const
{
if (data1_.isAddrData())
return Action(data1_.mcontrol_.action_);
if (data1_.isInstCount())
return Action(data1_.icount_.action_);
return Action::RaiseBreak;
}
/// Enable all ld/st address matching [address, address+size-1].
void enableAllLdStAddrMatch(bool flag)
{ matchAllLdStAddr_ = flag; }
/// Enable all inst address matching [address, address+size-1].
void enableAllInstAddrMatch(bool flag)
{ matchAllInstAddr_ = flag; }
/// Config the maximum NAPOT mask.
void configNapotMask(uint64_t mask)
{ napotMask_ = mask; }
protected:
static bool isNegatedMatch(Match m)
{ return m >= Match::NotEqual and m <= Match::NotMaskLowEqualHigh; }
static Match negateNegatedMatch(Match m)
{ assert(isNegatedMatch); return Match(unsigned(m) - unsigned(Match::NotEqual)); }
void updateCompareMask()
{
// Pre-compute mask for a masked compare (match == 1 in mcontrol).
data2CompareMask_ = ~URV(0);
unsigned leastSigZeroBit = std::countr_one(data2_); // Index of least sig zero bit
if (leastSigZeroBit >= 8*sizeof(URV) - 1)
data2CompareMask_ = 0;
else
data2CompareMask_ = data2CompareMask_ << (leastSigZeroBit + 1);
}
bool isModified() const
{ return modifiedT1_ or modifiedT2_ or modifiedT3_ or modifiedInfo_ or modifiedControl_; }
void clearModified()
{ modifiedT1_ = modifiedT2_ = modifiedT3_ = modifiedInfo_ = modifiedControl_ = false; }
bool getLocalHit() const
{ return localHit_; }
void setLocalHit(bool flag)
{ localHit_ = flag; }
void setChainHit(bool flag)
{ chainHit_ = flag; }
void setChainBounds(size_t begin, size_t end)
{
chainBegin_ = begin;
chainEnd_ = end;
}
void getChainBounds(size_t& begin, size_t& end) const
{
begin = chainBegin_;
end = chainEnd_;
}
bool peek(uint64_t& data1, uint64_t& data2, uint64_t& data3) const
{
data1 = data1_.value_; data2 = data2_; data3 = data3_;
return true;
}
bool peek(uint64_t& data1, uint64_t& data2, uint64_t& data3,
uint64_t& wm1, uint64_t& wm2, uint64_t& wm3,
uint64_t& pm1, uint64_t& pm2, uint64_t& pm3) const
{
bool ok = peek(data1, data2, data3);
wm1 = data1WriteMask_; wm2 = data2WriteMask_; wm3 = data3WriteMask_;
pm1 = data1PokeMask_; pm2 = data2PokeMask_; pm3 = data3PokeMask_;
return ok;
}
// Helper to public matchLdStAddr.
template <typename M>
bool matchLdStAddr(URV address, unsigned size, TriggerTiming timing, bool isLoad,
PrivilegeMode mode, bool virtMode) const;
template <typename M>
bool matchLdStData(URV value, TriggerTiming timing, bool isLoad,
PrivilegeMode mode, bool virtMode) const;
template <typename M>
bool matchInstAddr(URV address, unsigned size, TriggerTiming timing,
PrivilegeMode mode, bool virtMode) const;
template <typename M>
bool matchInstOpcode(URV opcode, TriggerTiming timing,
PrivilegeMode mode, bool virtMode) const;
private:
Data1Bits<URV> data1_ = Data1Bits<URV>(0);
URV data2_ = 0;
URV data3_ = 0;
TinfoBits info_ = TinfoBits(0);
URV data1Reset_ = 0;
URV data2Reset_ = 0;
URV data3Reset_ = 0;
URV infoReset_ = 0;
URV data1WriteMask_ = ~URV(0);
URV data2WriteMask_ = ~URV(0);
URV data3WriteMask_ = 0; // Place holder.
URV infoWriteMask_ = ~URV(0);
URV data1PokeMask_ = ~URV(0);
URV data2PokeMask_ = ~URV(0);
URV data3PokeMask_ = 0; // Place holder.
URV infoPokeMask_ = ~URV(0);
URV data2CompareMask_ = ~URV(0);
URV napotMask_ = ~(URV(1) << (8*sizeof(URV) - 2));
URV prevData1_ = 0;
bool localHit_ = false; // Trigger tripped in isolation.
bool chainHit_ = false; // All entries in chain tripped.
bool modifiedT1_ = false;
bool modifiedT2_ = false;
bool modifiedT3_ = false;
bool modifiedInfo_ = false;
bool modifiedControl_ = false;
size_t chainBegin_ = 0, chainEnd_ = 0;
bool matchAllLdStAddr_ = true; // If enabled, attempt to match address against
// any [address, address+size-1].
bool matchAllInstAddr_ = true; // Same as above but for instruction fetch.
};
template <typename URV>
class Triggers
{
public:
Triggers(unsigned count = 0);
/// Return the number of defined triggers.
size_t size() const
{ return triggers_.size(); }
/// Set value to the tdata1 register of the given trigger. Return true on success and
/// false (leaving value unmodified) if trigger is out of bounds.
bool readData1(URV trigger, URV& value) const;
/// Set value to the tdata2 register of the given trigger. Return true on success and
/// false (leaving value unmodified) if trigger is out of bounds or if data2 is not
/// implemented.
bool readData2(URV trigger, URV& value) const;
/// Set value to the tdata3 register of the given trigger. Return true on success and
/// false (leaving value unmodified) if trigger is out of bounds of if data3 is not
/// implemented.
bool readData3(URV trigger, URV& value) const;
/// Set value to the tinfo register of the given trigger. Return true on success and
/// false (leaving value unmodified) if trigger is out of bounds of if tinfo is not
/// implemented.
bool readInfo(URV trigger, URV& value) const;
/// Set value to the tcontroinfo of the given trigger. Return true on success and
/// false (leaving value unmodified) if trigger is out of bounds of if tcontrol is not
/// implemented.
bool readControl(URV trigger, URV& value) const;
/// Set the tdata1 register of the given trigger to the given value. Return true on
/// success and false (leaving value unmodified) if trigger is out of bounds.
bool writeData1(URV trigger, bool debugMode, URV value);
/// Set the tdata2 register of the given trigger to the given value. Return true on
/// success and false (leaving value unmodified) if trigger is out of bounds or if
/// tdata2 is not implemented.
bool writeData2(URV trigger, bool debugMode, URV value);
/// Set the tdata3 register of the given trigger to the given value. Return true on
/// success and false (leaving value unmodified) if trigger is out of bounds or if
/// tdata3 is not implemented.
bool writeData3(URV trigger, bool debugMode, URV value);
/// Set the tinfo register of the given trigger to the given value. Return true on
/// success and false (leaving value unmodified) if trigger is out of bounds or if
/// tinfo is not implemented.
bool writeInfo(URV trigger, bool debugMode, URV value);
/// Set the tcontrol register of the given trigger to the given value. Return true on
/// success and false (leaving value unmodified) if trigger is out of bounds or if
/// tcontrol is not implemented.
bool writeControl(URV trigger, bool debugMode, URV value);
/// Return true if given trigger is enabled. Return false if trigger is not enabled or
/// if it is out of bounds.
bool isEnabled(URV trigger) const
{
if (trigger >= triggers_.size())
return false;
return triggers_.at(trigger).isEnabled();
}
/// Return true if one or more triggers are enabled.
bool hasActiveTrigger() const
{
for (const auto& trigger : triggers_)
if (trigger.isEnabled())
return true;
return false;
}
/// Return true if one or more instruction (execute) triggers are
/// enabled.
bool hasActiveInstTrigger() const
{
for (const auto& trigger : triggers_)
if (trigger.isEnabled() and trigger.isInst())
return true;
return false;
}
/// Return true if any of the load (store if isLoad is true) triggers trips. A
/// load/store trigger trips if it matches the given address and timing and if all the
/// remaining triggers in its chain have tripped. Set the local-hit bit of any
/// load/store trigger that matches. If the trigger action is contingent on interrupts
/// being enabled (ie == true), then the trigger will not trip even if its condition
/// is satisfied.
bool ldStAddrTriggerHit(URV address, unsigned size, TriggerTiming, bool isLoad,
PrivilegeMode mode, bool virtMode, bool ie);
/// Similar to ldStAddrTriggerHit but for data match.
bool ldStDataTriggerHit(URV value, TriggerTiming, bool isLoad,
PrivilegeMode mode, bool virtMode, bool ie);
/// Similar to ldStAddrTriggerHit but for instruction address.
bool instAddrTriggerHit(URV address, unsigned size, TriggerTiming timing,
PrivilegeMode mode, bool virtMode, bool ie);
/// Similar to instAddrTriggerHit but for instruction opcode.
bool instOpcodeTriggerHit(URV opcode, TriggerTiming timing,
PrivilegeMode mode, bool virtMode, bool ie);
/// Make every active icount trigger count down unless it was written by the current
/// instruction. If a count-down register becomes zero as a result of the count-down
/// and the associated actions is not suppressed (e.g. action is ebreak exception and
/// interrupts are disabled), then consider the trigger as having tripped and set its
/// hit bit to 1. Return true if any icount trigger trips; otherwise, return false.
bool icountTriggerHit(PrivilegeMode prevPrivMode, bool prevVirtMode,
PrivilegeMode mode, bool virtMode, bool ie);
/// Return true if any of the exception-triggers (etrigger) trips.
bool expTriggerHit(URV cause, PrivilegeMode mode, bool virtMode, bool interruptEnabled);
/// Return true if any of the interrupt-triggers (itrigger) trips.
bool intTriggerHit(URV cause, PrivilegeMode mode, bool virtMode, bool interruptEnabled);
/// Return the tigger at the given index. Reurn None if index is out of bounds.
TriggerType triggerType(URV trigger) const
{
if (trigger >= triggers_.size())
return TriggerType::None;
return triggers_.at(trigger).type();
}
/// Reset the given trigger with the given data1, data2, and data3
/// values and corresponding write and poke masks. Values are applied
/// without masking. Subsequent writes will be masked.
bool reset(URV trigger, URV data1, URV data2, URV data3,
URV writeMask1, URV writeMask2, URV writeMask3,
URV pokeMask1, URV pokeMask2, URV pokeMask3);
/// Configure given trigger with given reset values, write masks and
/// and poke masks.
bool config(unsigned trigger,
const std::vector<uint64_t>& resets,
const std::vector<uint64_t>& masks,
const std::vector<uint64_t>& pokeMasks);
/// Get the values of the three components of the given debug
/// trigger. Return true on success and false if trigger is out of
/// bounds.
bool peek(unsigned trigger, uint64_t& data1, uint64_t& data2,
uint64_t& data3) const;
/// Get the values of the three components of the given debug
/// trigger as well as the components write and poke masks. Return
/// true on success and false if trigger is out of bounds.
bool peek(unsigned trigger,
uint64_t& data1, uint64_t& data2, uint64_t& data3,
uint64_t& wm1, uint64_t& wm2, uint64_t& wm3,
uint64_t& pm1, uint64_t& pm2, uint64_t& pm3) const;
/// Set the values of the three components of the given debug
/// trigger. Return true on success and false if trigger is out of
/// bounds.
bool poke(URV trigger, URV v1, URV v2, URV v3);
bool pokeData1(URV trigger, URV val);
bool pokeData2(URV trigger, URV val);
bool pokeData3(URV trigger, URV val);
bool pokeInfo(URV trigger, URV val);
/// Clear the remembered indices of the triggers written by the
/// last instruction.
void clearLastWrittenTriggers()
{
for (auto& trig : triggers_)
{
trig.setLocalHit(false);
trig.setChainHit(false);
trig.clearModified();
}
}
/// Fill the trigs vector with the indices of the triggers written
/// by the last instruction.
void getLastWrittenTriggers(std::vector<unsigned>& trigs) const
{
trigs.clear();
for (unsigned i = 0; i < triggers_.size(); ++i)
if (triggers_.at(i).isModified())
trigs.push_back(i);
}
/// Set before/after to the count of tripped triggers with
/// before/after timing.
void countTrippedTriggers(unsigned& before, unsigned& after) const
{
before = after = 0;
for (const auto& trig : triggers_)
if (trig.hasTripped())
(trig.getTiming() == TriggerTiming::Before)? before++ : after++;
}
/// Return true if there is one or more tripped trigger action set
/// to "enter debug mode".
bool hasEnterDebugModeTripped() const
{
for (const auto& trig : triggers_)
if (trig.hasTripped())
{
// If chained, use action of last trigger in chain.
size_t start = 0, end = 0;
trig.getChainBounds(start, end);
auto& last = triggers_.at(end - 1);
if (last.getAction() == Trigger<URV>::Action::EnterDebug)
return true;
}
return false;
}
/// Enable all ld/st address matching [address, address+size-1].
void enableAllLdStAddrMatch(bool flag)
{ for ( auto& trig : triggers_) trig.enableAllLdStAddrMatch(flag); }
/// Enable all inst address matching [address, address+size-1].
void enableAllInstAddrMatch(bool flag)
{ for ( auto& trig : triggers_) trig.enableAllInstAddrMatch(flag); }
/// Set the maximum NAPOT range with maskmax.
void configNapotMaskMax(unsigned bits)
{
if (bits > (8*sizeof(URV) - 1))
return;
bits -= 1;