forked from chipsalliance/VeeR-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amo.cpp
1142 lines (895 loc) · 27.5 KB
/
amo.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 <climits>
#include <mutex>
#include <atomic>
#include <cassert>
#include "instforms.hpp"
#include "DecodedInst.hpp"
#include "Hart.hpp"
#include "System.hpp"
using namespace WdRiscv;
template <typename URV>
ExceptionCause
Hart<URV>::validateAmoAddr(uint32_t rs1, uint64_t& addr, unsigned accessSize,
SecondaryCause& secCause)
{
URV mask = URV(accessSize) - 1;
auto cause = ExceptionCause::NONE;
bool forcedFail = false;
if (accessSize == 4)
{
uint32_t storeVal = 0;
cause = determineStoreException(rs1, addr, addr, storeVal, secCause, forcedFail);
}
else
{
uint64_t storeVal = 0;
cause = determineStoreException(rs1, addr, addr, storeVal, secCause, forcedFail);
}
if (cause == ExceptionCause::STORE_ADDR_MISAL and
misalAtomicCauseAccessFault_)
{
cause = ExceptionCause::STORE_ACC_FAULT;
secCause = SecondaryCause::STORE_ACC_AMO;
}
// Check if invalid unless cacheable.
if (amoInCacheableOnly_ and not isAddrCacheable(addr))
if (cause == ExceptionCause::NONE)
{
cause = ExceptionCause::STORE_ACC_FAULT;
secCause = SecondaryCause::STORE_ACC_AMO_UNCACHED;
}
// Address must be word aligned for word access and double-word
// aligned for double-word access.
bool fail = (addr & mask) != 0;
// Check if invalid outside DCCM.
if (amoInDccmOnly_ and not isAddrInDccm(addr))
fail = true;
if (fail)
{
// AMO secondary cause has priority over ECC.
if (cause == ExceptionCause::NONE or
secCause == SecondaryCause::STORE_ACC_DOUBLE_ECC)
{
// Per spec cause is store-access-fault.
cause = ExceptionCause::STORE_ACC_FAULT;
secCause = SecondaryCause::STORE_ACC_AMO;
}
}
return cause;
}
template <typename URV>
bool
Hart<URV>::amoLoad32(uint32_t rd, uint32_t rs1, uint32_t rs2, URV& value)
{
URV virtAddr = intRegs_.read(rs1);
ldStAddr_ = virtAddr; // For reporting load addr in trace-mode.
ldStAddrValid_ = true; // For reporting load addr in trace-mode.
if (loadQueueEnabled_)
{
removeFromLoadQueue(rs1, false);
removeFromLoadQueue(rs2, false);
}
if (hasActiveTrigger())
{
if (ldStAddrTriggerHit(virtAddr, TriggerTiming::Before, false /*isLoad*/,
privMode_, isInterruptEnabled()))
triggerTripped_ = true;
}
unsigned ldSize = 4;
auto secCause = SecondaryCause::STORE_ACC_AMO;
uint64_t addr = virtAddr;
auto cause = validateAmoAddr(rs1, addr, ldSize, secCause);
if (cause != ExceptionCause::NONE)
{
if (not triggerTripped_)
initiateLoadException(cause, virtAddr, secCause);
return false;
}
uint32_t uval = 0;
if (memory_.read(addr, uval))
{
value = SRV(int32_t(uval)); // Sign extend.
URV prevRdVal = peekIntReg(rd);
putInLoadQueue(4 /*ldSize*/, addr, rd, prevRdVal);
return true; // Success.
}
cause = ExceptionCause::STORE_ACC_FAULT;
initiateLoadException(cause, virtAddr, secCause);
return false;
}
template <typename URV>
bool
Hart<URV>::amoLoad64(uint32_t rd, uint32_t rs1, uint32_t rs2, URV& value)
{
URV virtAddr = intRegs_.read(rs1);
ldStAddr_ = virtAddr; // For reporting load addr in trace-mode.
ldStAddrValid_ = true; // For reporting load addr in trace-mode.
if (loadQueueEnabled_)
{
removeFromLoadQueue(rs1, false);
removeFromLoadQueue(rs2, false);
}
if (hasActiveTrigger())
{
if (ldStAddrTriggerHit(virtAddr, TriggerTiming::Before, false /*isLoad*/,
privMode_, isInterruptEnabled()))
triggerTripped_ = true;
}
unsigned ldSize = 8;
auto secCause = SecondaryCause::STORE_ACC_AMO;
uint64_t addr = virtAddr;
auto cause = validateAmoAddr(rs1, addr, ldSize, secCause);
if (cause != ExceptionCause::NONE)
{
if (not triggerTripped_)
initiateLoadException(cause, virtAddr, secCause);
return false;
}
uint64_t uval = 0;
if (memory_.read(addr, uval))
{
value = SRV(int64_t(uval)); // Sign extend.
URV prevRdVal = peekIntReg(rd);
putInLoadQueue(8 /*ldSize*/, addr, rd, prevRdVal);
return true; // Success.
}
cause = ExceptionCause::STORE_ACC_FAULT;
initiateLoadException(cause, virtAddr, secCause);
return false;
}
template <typename URV>
template <typename LOAD_TYPE>
bool
Hart<URV>::loadReserve(uint32_t rd, uint32_t rs1, uint64_t& physAddr)
{
URV virtAddr = intRegs_.read(rs1);
ldStAddr_ = virtAddr; // For reporting load addr in trace-mode.
ldStAddrValid_ = true; // For reporting load addr in trace-mode.
if (loadQueueEnabled_)
removeFromLoadQueue(rs1, false);
if (hasActiveTrigger())
{
typedef TriggerTiming Timing;
bool isLd = true;
if (ldStAddrTriggerHit(virtAddr, Timing::Before, isLd,
privMode_, isInterruptEnabled()))
triggerTripped_ = true;
if (triggerTripped_)
return false;
}
// Unsigned version of LOAD_TYPE
typedef typename std::make_unsigned<LOAD_TYPE>::type ULT;
auto secCause = SecondaryCause::NONE;
unsigned ldSize = sizeof(LOAD_TYPE);
uint64_t addr = virtAddr;
auto cause = determineLoadException(rs1, virtAddr, addr, ldSize, secCause);
if (cause != ExceptionCause::NONE)
{
if (cause == ExceptionCause::LOAD_ADDR_MISAL and
misalAtomicCauseAccessFault_)
{
cause = ExceptionCause::LOAD_ACC_FAULT;
secCause = SecondaryCause::LOAD_ACC_AMO;
}
}
// Check if invalid unless cacheable.
if (amoInCacheableOnly_ and not isAddrCacheable(addr))
if (cause == ExceptionCause::NONE)
{
cause = ExceptionCause::LOAD_ACC_FAULT;
secCause = SecondaryCause::LOAD_ACC_AMO_UNCACHED;
}
// Address outside DCCM causes an exception (this is swerv specific).
bool fail = amoInDccmOnly_ and not isAddrInDccm(addr);
// Access must be naturally aligned.
if ((addr & (ldSize - 1)) != 0)
fail = true;
if (fail)
{
// AMO secondary cause has priority over ECC.
if (cause == ExceptionCause::NONE or
secCause == SecondaryCause::LOAD_ACC_DOUBLE_ECC)
{
// Per spec cause is store-access-fault.
cause = ExceptionCause::LOAD_ACC_FAULT;
secCause = SecondaryCause::LOAD_ACC_AMO;
}
}
if (cause != ExceptionCause::NONE)
{
initiateLoadException(cause, virtAddr, secCause);
return false;
}
ULT uval = 0;
if (not memory_.read(addr, uval))
{ // Should never happen.
initiateLoadException(cause, virtAddr, secCause);
return false;
}
URV value = uval;
if (not std::is_same<ULT, LOAD_TYPE>::value)
value = SRV(LOAD_TYPE(uval)); // Sign extend.
// Put entry in load queue with value of rd before this load.
if (loadQueueEnabled_)
putInLoadQueue(ldSize, addr, rd, peekIntReg(rd));
intRegs_.write(rd, value);
physAddr = addr;
return true;
}
template <typename URV>
void
Hart<URV>::execLr_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
std::lock_guard<std::mutex> lock(memory_.lrMutex_);
lrCount_++;
uint64_t physAddr = 0;
if (not loadReserve<int32_t>(di->op0(), di->op1(), physAddr))
return;
memory_.makeLr(hartIx_, physAddr, 4 /*size*/);
lrSuccess_++;
}
/// STORE_TYPE is either uint32_t or uint64_t.
template <typename URV>
template <typename STORE_TYPE>
bool
Hart<URV>::storeConditional(uint32_t rs1, URV virtAddr, STORE_TYPE storeVal)
{
ldStAddr_ = virtAddr; // For reporting ld/st addr in trace-mode.
ldStAddrValid_ = true; // For reporting ld/st addr in trace-mode.
// ld/st-address or instruction-address triggers have priority over
// ld/st access or misaligned exceptions.
bool hasTrig = hasActiveTrigger();
TriggerTiming timing = TriggerTiming::Before;
bool isLoad = false;
if (hasTrig)
if (ldStAddrTriggerHit(virtAddr, timing, isLoad, privMode_,
isInterruptEnabled()))
triggerTripped_ = true;
// Misaligned store causes an exception.
constexpr unsigned alignMask = sizeof(STORE_TYPE) - 1;
bool misal = virtAddr & alignMask;
misalignedLdSt_ = misal;
auto secCause = SecondaryCause::NONE;
uint64_t addr = virtAddr;
bool forcedFail = false;
auto cause = determineStoreException(rs1, virtAddr, addr, storeVal, secCause,
forcedFail);
if (cause == ExceptionCause::STORE_ADDR_MISAL and
misalAtomicCauseAccessFault_)
{
cause = ExceptionCause::STORE_ACC_FAULT;
secCause = SecondaryCause::STORE_ACC_AMO;
}
// Check if invalid unless cacheable.
if (amoInCacheableOnly_ and not isAddrCacheable(addr))
if (cause == ExceptionCause::NONE)
{
cause = ExceptionCause::STORE_ACC_FAULT;
secCause = SecondaryCause::STORE_ACC_AMO_UNCACHED;
}
bool fail = misal or (amoInDccmOnly_ and not isAddrInDccm(addr));
if (fail)
{
// AMO secondary cause has priority over ECC.
if (cause == ExceptionCause::NONE or
secCause == SecondaryCause::STORE_ACC_DOUBLE_ECC)
{
// Per spec cause is store-access-fault.
cause = ExceptionCause::STORE_ACC_FAULT;
secCause = SecondaryCause::STORE_ACC_AMO;
}
}
// If no exception: consider store-data trigger
if (cause == ExceptionCause::NONE and hasTrig)
if (ldStDataTriggerHit(storeVal, timing, isLoad, privMode_,
isInterruptEnabled()))
triggerTripped_ = true;
if (triggerTripped_)
return false;
if (cause != ExceptionCause::NONE)
{
initiateStoreException(cause, virtAddr, secCause);
return false;
}
if (not memory_.hasLr(hartIx_, addr, sizeof(storeVal)))
return false;
if (memory_.write(hartIx_, addr, storeVal))
{
invalidateDecodeCache(virtAddr, sizeof(STORE_TYPE));
// If we write to special location, end the simulation.
if (toHostValid_ and addr == toHost_ and storeVal != 0)
throw CoreException(CoreException::Stop, "write to to-host",
toHost_, storeVal);
return true;
}
// Should never happen.
secCause = SecondaryCause::STORE_ACC_AMO;
initiateStoreException(ExceptionCause::STORE_ACC_FAULT, virtAddr, secCause);
return false;
}
template <typename URV>
void
Hart<URV>::execSc_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
std::lock_guard<std::mutex> lock(memory_.lrMutex_);
uint32_t rd = di->op0(), rs1 = di->op1();
URV value = intRegs_.read(di->op2());
URV addr = intRegs_.read(rs1);
scCount_++;
if (loadQueueEnabled_)
removeFromLoadQueue(rs1, false);
uint64_t prevCount = exceptionCount_;
bool ok = storeConditional(rs1, addr, uint32_t(value));
cancelLr(); // Clear LR reservation (if any).
if (ok)
{
if (loadQueueEnabled_)
putInLoadQueue(4 /* size*/, addr, rd, peekIntReg(rd));
memory_.invalidateOtherHartLr(hartIx_, addr, 4);
intRegs_.write(rd, 0); // success
scSuccess_++;
return;
}
// If exception or trigger tripped then rd is not modified.
if (triggerTripped_ or exceptionCount_ != prevCount)
return;
if (loadQueueEnabled_)
putInLoadQueue(4 /* size*/, addr, rd, peekIntReg(rd));
intRegs_.write(di->op0(), 1); // fail
}
template <typename URV>
void
Hart<URV>::execAmoadd_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad32(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
// Sign extend least significant word of register value.
SRV rdVal = SRV(int32_t(loadedValue));
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val + rdVal;
bool storeOk = store<uint32_t>(rs1, addr, addr, uint32_t(result));
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execAmoswap_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad32(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
// Sign extend least significant word of register value.
SRV rdVal = SRV(int32_t(loadedValue));
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val;
bool storeOk = store<uint32_t>(rs1, addr, addr, uint32_t(result));
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execAmoxor_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad32(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
// Sign extend least significant word of register value.
SRV rdVal = SRV(int32_t(loadedValue));
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val ^ rdVal;
bool storeOk = store<uint32_t>(rs1, addr, addr, uint32_t(result));
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execAmoor_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad32(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
// Sign extend least significant word of register value.
SRV rdVal = SRV(int32_t(loadedValue));
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val | rdVal;
bool storeOk = store<uint32_t>(rs1, addr, addr, uint32_t(result));
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execAmoand_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad32(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
// Sign extend least significant word of register value.
SRV rdVal = SRV(int32_t(loadedValue));
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val & rdVal;
bool storeOk = store<uint32_t>(rs1, addr, addr, uint32_t(result));
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execAmomin_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad32(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
URV rs2Val = intRegs_.read(rs2);
int32_t w1 = int32_t(rs2Val);
int32_t w2 = int32_t(loadedValue);
int32_t result = (w1 < w2)? w1 : w2;
bool storeOk = store<uint32_t>(rs1, addr, addr, uint32_t(result));
if (storeOk and not triggerTripped_)
intRegs_.write(rd, loadedValue);
}
}
template <typename URV>
void
Hart<URV>::execAmominu_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad32(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
URV rs2Val = intRegs_.read(rs2);
uint32_t w1 = uint32_t(rs2Val);
uint32_t w2 = uint32_t(loadedValue);
uint32_t result = (w1 < w2)? w1 : w2;
bool storeOk = store<uint32_t>(rs1, addr, addr, result);
if (storeOk and not triggerTripped_)
intRegs_.write(rd, loadedValue);
}
}
template <typename URV>
void
Hart<URV>::execAmomax_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad32(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
URV rs2Val = intRegs_.read(rs2);
int32_t w1 = int32_t(rs2Val);
int32_t w2 = int32_t(loadedValue);
int32_t result = w1 > w2 ? w1 : w2;
bool storeOk = store<uint32_t>(rs1, addr, addr, uint32_t(result));
if (storeOk and not triggerTripped_)
intRegs_.write(rd, loadedValue);
}
}
template <typename URV>
void
Hart<URV>::execAmomaxu_w(const DecodedInst* di)
{
if (not isRva())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad32(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
// Sign extend least significant word of register value.
SRV rdVal = SRV(int32_t(loadedValue));
URV rs2Val = intRegs_.read(rs2);
uint32_t w1 = uint32_t(rs2Val);
uint32_t w2 = uint32_t(rdVal);
URV result = (w1 > w2)? w1 : w2;
bool storeOk = store<uint32_t>(rs1, addr, addr, result);
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execLr_d(const DecodedInst* di)
{
if (not isRva() or not isRv64())
{
illegalInst(di);
return;
}
std::lock_guard<std::mutex> lock(memory_.lrMutex_);
lrCount_++;
uint64_t physAddr = 0;
if (not loadReserve<int64_t>(di->op0(), di->op1(), physAddr))
return;
memory_.makeLr(hartIx_, physAddr, 8 /*size*/);
lrSuccess_++;
}
template <typename URV>
void
Hart<URV>::execSc_d(const DecodedInst* di)
{
if (not isRva() or not isRv64())
{
illegalInst(di);
return;
}
std::lock_guard<std::mutex> lock(memory_.lrMutex_);
uint32_t rd = di->op0(), rs1 = di->op1();
URV value = intRegs_.read(di->op2());
URV addr = intRegs_.read(rs1);
scCount_++;
if (loadQueueEnabled_)
removeFromLoadQueue(rs1, false);
uint64_t prevCount = exceptionCount_;
bool ok = storeConditional(rs1, addr, uint64_t(value));
cancelLr(); // Clear LR reservation (if any).
if (ok)
{
if (loadQueueEnabled_)
putInLoadQueue(8 /* size*/, addr, rd, peekIntReg(rd));
memory_.invalidateOtherHartLr(hartIx_, addr, 8);
intRegs_.write(rd, 0); // success
scSuccess_++;
return;
}
// If exception or trigger tripped then rd is not modified.
if (triggerTripped_ or exceptionCount_ != prevCount)
return;
if (loadQueueEnabled_)
putInLoadQueue(8 /* size*/, addr, rd, peekIntReg(rd));
intRegs_.write(di->op0(), 1); // fail
}
template <typename URV>
void
Hart<URV>::execAmoadd_d(const DecodedInst* di)
{
if (not isRva() or not isRv64())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad64(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
URV rdVal = loadedValue;
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val + rdVal;
bool storeOk = store<uint64_t>(rs1, addr, addr, result);
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execAmoswap_d(const DecodedInst* di)
{
if (not isRva() or not isRv64())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad64(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
URV rdVal = loadedValue;
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val;
bool storeOk = store<uint64_t>(rs1, addr, addr, result);
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execAmoxor_d(const DecodedInst* di)
{
if (not isRva() or not isRv64())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad64(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
URV rdVal = loadedValue;
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val ^ rdVal;
bool storeOk = store<uint64_t>(rs1, addr, addr, result);
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execAmoor_d(const DecodedInst* di)
{
if (not isRva() or not isRv64())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad64(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
URV rdVal = loadedValue;
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val | rdVal;
bool storeOk = store<uint64_t>(rs1, addr, addr, result);
if (storeOk and not triggerTripped_)
intRegs_.write(rd, rdVal);
}
}
template <typename URV>
void
Hart<URV>::execAmoand_d(const DecodedInst* di)
{
if (not isRva() or not isRv64())
{
illegalInst(di);
return;
}
// Lock mutex to serialize AMO instructions. Unlock automatically on
// exit from this scope.
std::lock_guard<std::mutex> lock(memory_.amoMutex_);
URV loadedValue = 0;
uint32_t rd = di->op0(), rs1 = di->op1(), rs2 = di->op2();
bool loadOk = amoLoad64(rd, rs1, rs2, loadedValue);
if (loadOk)
{
URV addr = intRegs_.read(rs1);
URV rdVal = loadedValue;
URV rs2Val = intRegs_.read(rs2);
URV result = rs2Val & rdVal;