forked from chipsalliance/VeeR-ISS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hart.hpp
3103 lines (2520 loc) · 123 KB
/
Hart.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 <iosfwd>
#include <unordered_set>
#include <type_traits>
#include <functional>
#include <atomic>
#include "InstId.hpp"
#include "InstEntry.hpp"
#include "IntRegs.hpp"
#include "CsRegs.hpp"
#include "FpRegs.hpp"
#include "VecRegs.hpp"
#include "Memory.hpp"
#include "InstProfile.hpp"
#include "DecodedInst.hpp"
#include "Syscall.hpp"
#include "PmpManager.hpp"
#include "VirtMem.hpp"
namespace WdRiscv
{
/// Thrown by the simulator when a stop (store to to-host) is seen
/// or when the target program reaches the exit system call.
class CoreException : public std::exception
{
public:
enum Type { Stop, Exit };
CoreException(Type type, const char* message = "", uint64_t address = 0,
uint64_t value = 0)
: type_(type), msg_(message), addr_(address), val_(value)
{ }
const char* what() const noexcept override
{ return msg_; }
Type type() const
{ return type_; }
uint64_t address() const
{ return addr_; }
uint64_t value() const
{ return val_; }
private:
Type type_ = Stop;
const char* msg_ = "";
uint64_t addr_ = 0;
uint64_t val_ = 0;
};
/// Changes made by the execution of one instruction. Useful for
/// test pattern generation.
struct ChangeRecord
{
void clear()
{ *this = ChangeRecord(); }
uint64_t newPc = 0; // Value of pc after instruction execution.
bool hasException = false; // True if instruction causes an exception.
bool hasIntReg = false; // True if there is an integer register change.
unsigned intRegIx = 0; // Number of changed integer register if any.
uint64_t intRegValue = 0; // Value of changed integer register if any.
bool hasFpReg = false; // True if there is an FP register change.
unsigned fpRegIx = 0; // Number of changed fp register if any.
uint64_t fpRegValue = 0; // Value of changed fp register if any.
unsigned memSize = 0; // Size of changed memory (0 if none).
uint64_t memAddr = 0; // Address of changed memory if any.
uint64_t memValue = 0; // Value of changed memory if any.
// An exception will result in changing multiple CSRs.
std::vector<CsrNumber> csrIx; // Numbers of changed CSRs if any.
std::vector<uint64_t> csrValue; // Values of changed CSRs if any.
};
/// Model a RISCV hart with integer registers of type URV (uint32_t
/// for 32-bit registers and uint64_t for 64-bit registers).
template <typename URV>
class Hart
{
public:
/// Signed register type corresponding to URV. For example, if URV
/// is uint32_t, then SRV will be int32_t.
typedef typename std::make_signed_t<URV> SRV;
/// Constructor: Define a hart with the given index (unique index
/// within a system of cores -- see sysHartIndex method) and
/// associate it with the given memory. The MHARTID is configured as
/// a read-only CSR with a reset value of hartId.
Hart(unsigned hartIx, URV hartId, Memory& memory);
/// Destructor.
~Hart();
/// Return count of integer registers.
size_t intRegCount() const
{ return intRegs_.size(); }
/// Return the name of the given integer register. Return an
/// abi-name (e.g. sp) if abi names are enabled.
std::string intRegName(unsigned regIx) const
{ return intRegs_.regName(regIx, abiNames_); }
/// Return the name of the given floating point register. Return an
/// abi-name (e.g. fa0) if abi names are enabled.
std::string fpRegName(unsigned regIx) const
{ return fpRegs_.regName(regIx, abiNames_); }
/// Return the name (e.g. x1) or the abi-name (e.g. ra) of the
/// given integer register.
std::string intRegName(unsigned regIx, bool abiName) const
{ return intRegs_.regName(regIx, abiName); }
/// Return count of floating point registers. Return zero if
/// extension f is not enabled.
size_t fpRegCount() const
{ return isRvf()? fpRegs_.size() : 0; }
/// Return size of memory in bytes.
size_t memorySize() const
{ return memory_.size(); }
/// Return the value of the program counter.
URV peekPc() const;
/// Set the program counter to the given address.
void pokePc(URV address);
/// Set val to the value of integer register reg returning true on
/// success. Return false leaving val unmodified if reg is out of
/// bounds.
bool peekIntReg(unsigned reg, URV& val) const;
/// Set val to the value of integer register reg returning true on
/// success. Return false leaving val unmodified if reg is out of
/// bounds. If successful, set name to the register name.
bool peekIntReg(unsigned reg, URV& val, std::string& name) const;
/// Return to the value of integer register reg which must not be
/// out of bounds (otherwise we trigger an assert).
URV peekIntReg(unsigned reg) const;
/// Set the given integer register, reg, to the given value
/// returning true on success. Return false if reg is out of
/// bound.
bool pokeIntReg(unsigned reg, URV val);
/// Set val to the bit-pattern of the value of the floating point
/// register returning true on success. Return false leaving val
/// unmodified if reg is out of bounds of if no floating point
/// extension is enabled.
bool peekFpReg(unsigned reg, uint64_t& val) const;
/// Set val to the bit-pattern of the value of the floating point
/// register (after unboxing that value if it is nan-boxed)
/// returning true on success. Return false leaving val unmodified
/// if reg is out of bounds of if no floating point extension is
/// enabled.
bool peekUnboxedFpReg(unsigned reg, uint64_t& val) const;
/// Set the given FP register, reg, to the given value returning
/// true on success. Return false if reg is out of bound.
bool pokeFpReg(unsigned reg, uint64_t val);
/// Set val to the value of the control and status register csr
/// returning true on success. Return false leaving val unmodified
/// if csr is out of bounds.
bool peekCsr(CsrNumber csr, URV& val) const;
/// Set val, reset, writeMask, and pokeMask respectively to the
/// value, reset-value, write-mask and poke-mask of the control
/// and status register csr returning true on success. Return
/// false leaving parameters unmodified if csr is out of bounds.
bool peekCsr(CsrNumber csr, URV& val, URV& reset, URV& writeMask,
URV& pokeMask) const;
/// Set val/name to the value/name of the control and status
/// register csr returning true on success. Return false leaving
/// val/name unmodified if csr is out of bounds.
bool peekCsr(CsrNumber csr, URV& val, std::string& name) const;
/// Set the given control and status register, csr, to the given
/// value returning true on success. Return false if csr is out of
/// bound.
bool pokeCsr(CsrNumber csr, URV val);
/// Find the integer register with the given name (which may
/// represent an integer or a symbolic name). Set num to the
/// number of the corresponding register if found. Return true on
/// success and false if no such register.
bool findIntReg(const std::string& name, unsigned& num) const;
/// Find the floating point with the given name. Set num to the
/// number of the corresponding register if found. Return true on
/// success and false if no such register.
bool findFpReg(const std::string& name, unsigned& num) const;
/// Find the control and status register with the given name
/// (which may represent an integer or a symbolic name). Return
/// pointer to CSR on success and nullptr if no such register.
Csr<URV>* findCsr(const std::string& name);
/// Find the control and status register with the given number.
/// Return pointer to CSR on success and nullptr if no such
/// register.
const Csr<URV>* findCsr(CsrNumber number)
{ return csRegs_.findCsr(number); }
/// Configure given CSR. Return true on success and false if
/// no such CSR.
bool configCsr(const std::string& name, bool implemented,
URV resetValue, URV mask, URV pokeMask,
bool isDebug, bool shared);
/// Define a new CSR (beyond the standard CSRs defined by the
/// RISCV spec). Return true on success and false if name/number
/// already in use.
bool defineCsr(const std::string& name, CsrNumber number,
bool implemented, URV resetValue, URV mask,
URV pokeMask, bool isDebug);
/// Configure given trigger with given reset values, write and
/// poke masks. Return true on success and false on failure.
bool configTrigger(unsigned trigger,
uint64_t rv1, uint64_t rv2, uint64_t rv3,
uint64_t wm1, uint64_t wm2, uint64_t wm3,
uint64_t pm1, uint64_t pm2, uint64_t pm3)
{
return csRegs_.configTrigger(trigger, rv1, rv2, rv3,
wm1, wm2, wm3, pm1, pm2, pm3);
}
/// Enable/disable load-data debug triggerring (disabled by default).
void configLoadDataTrigger(bool flag)
{ csRegs_.configLoadDataTrigger(flag); }
/// Enable/disable exec-opcode triggering (disabled by default).
void configExecOpcodeTrigger(bool flag)
{ csRegs_.configExecOpcodeTrigger(flag); }
/// Restrict chaining only to pairs of consecutive (even-numbered followed
/// by odd) triggers.
void configEvenOddTriggerChaining(bool flag)
{ csRegs_.configEvenOddTriggerChaining(flag); }
/// Configure machine mode performance counters returning true on
/// success and false on failure. N consecutive counters starting
/// at MHPMCOUNTER3/MHPMCOUNTER3H are made read/write. The
/// remaining counters are made write-any read-zero. For each
/// counter that is made read-write the corresponding MHPMEVENT is
/// made read-write otherwise it is make write-any read-zero.
bool configMachineModePerfCounters(unsigned n);
/// Configure user mode performance counters returning true on
/// success and false on failure. N cannot exceed the number of machine
/// mode performance registers. First N performance counters are configured
/// as readable, the remaining ones are made read-zero.
bool configUserModePerfCounters(unsigned n);
/// Set the maximum event id that can be written to the MHPMEVENT
/// registers. Larger values are replaced by this max-value before
/// being written to the MHPMEVENT registers. Return true on
/// success and false on failure.
void configMachineModeMaxPerfEvent(URV maxId)
{ csRegs_.setMaxEventId(maxId); }
/// Configure valid event. If this is used then events outside the
/// given vector are replaced by zero before being assigned to an
/// MHPMEVENT register. Otherwise, events greater that
/// max-event-id are clamped to max-event-id before being assigned
/// to an MHPMEVENT register.
void configPerfEvents(std::vector<unsigned>& eventVec)
{ csRegs_.configPerfEvents(eventVec); }
/// Configure vector unit of this hart.
void configVector(unsigned bytesPerVec, unsigned maxBytesPerElem)
{ vecRegs_.config(bytesPerVec, maxBytesPerElem); }
/// 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 peekTrigger(unsigned trigger, uint64_t& data1, uint64_t& data2,
uint64_t& data3) const
{ return csRegs_.peekTrigger(trigger, data1, data2, data3); }
/// 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 peekTrigger(unsigned trigger,
uint64_t& val1, uint64_t& val2, uint64_t& val3,
uint64_t& wm1, uint64_t& wm2, uint64_t& wm3,
uint64_t& pm1, uint64_t& pm2, uint64_t& pm3) const
{ return csRegs_.peekTrigger(trigger, val1, val2, val3, wm1, wm2, wm3,
pm1, pm2, pm3); }
/// 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 pokeTrigger(URV trigger, URV data1, URV data2, URV data3)
{ return csRegs_.pokeTrigger(trigger, data1, data2, data3); }
/// Fill given vector (cleared on entry) with the numbers of
/// implemented CSRs.
void getImplementedCsrs(std::vector<CsrNumber>& vec) const;
/// Reset this hart. Reset all CSRs to their initial value. Reset all
/// integer registers to zero. Reset PC to the reset-pc as
/// defined by defineResetPc (default is zero).
void reset(bool resetMemoryMappedRegister = false);
/// Run fetch-decode-execute loop. If a stop address (see
/// setStopAddress) is defined, stop when the program counter
/// reaches that address. If a tohost address is defined (see
/// setToHostAdress), stop when a store instruction writes into
/// that address. If given file is non-null, then print to that
/// file a record for each executed instruction.
bool run(FILE* file = nullptr);
/// Run one instruction at the current program counter. Update
/// program counter. If file is non-null then print thereon
/// tracing information related to the executed instruction.
void singleStep(FILE* file = nullptr);
/// Determine the effect of instruction fetching and discarding n
/// bytes (where n is the instruction size of the given
/// instruction) from memory and then executing the given
/// instruction without actually changing the state of the hart or
/// the memory. Return true if the instruction would execute
/// without an exception. Return false otherwise. In either case
/// set the record fields corresponding to the resources that
/// would have been changed by the execution of the instruction.
bool whatIfSingleStep(URV programCounter, uint32_t inst,
ChangeRecord& record);
/// Similar to the preceding method but without fetching anything
/// from from instruction memory (in other words, this variant
/// will never cause an misaligned/instruction-access-fault
/// exception).
bool whatIfSingleStep(uint32_t inst, ChangeRecord& record);
/// Similar to the preceding but without fetching register
/// operands. Register operand values are obtained from the given
/// decoded instruction object.
bool whatIfSingStep(const DecodedInst& inst, ChangeRecord& record);
/// Run until the program counter reaches the given address. Do
/// execute the instruction at that address. If file is non-null
/// then print thereon tracing information after each executed
/// instruction. Similar to method run with respect to tohost.
bool runUntilAddress(size_t address, FILE* file = nullptr);
/// Helper to runUntiAddress: Same as runUntilAddress but does not
/// print run-time and instructions per second.
bool untilAddress(size_t address, FILE* file = nullptr);
/// Define the program counter value at which the run method will
/// stop.
void setStopAddress(URV address)
{ stopAddr_ = address; stopAddrValid_ = true; }
/// Undefine stop address (see setStopAddress).
void clearStopAddress()
{ stopAddrValid_ = false; }
/// Define the memory address corresponding to console io. Reading
/// (lw/lh/lb) or writing (sw/sh/sb) from/to that address
/// reads/writes a byte to/from the console.
void setConsoleIo(URV address)
{ conIo_ = address; conIoValid_ = true; }
/// Do not use console io address for input if flag is false:
/// Loads from that address simply return last value stored there.
void enableConsoleInput(bool flag)
{ enableConIn_ = flag; }
/// Undefine console io address (see setConsoleIo).
void clearConsoleIo()
{ conIoValid_ = false; }
/// Console output gets directed to given file.
void setConsoleOutput(FILE* out)
{ consoleOut_ = out; }
/// If a console io memory mapped location is defined then put its
/// address in address and return true; otherwise, return false
/// leaving address unmodified.
bool getConsoleIo(URV& address) const
{ if (conIoValid_) address = conIo_; return conIoValid_; }
/// Define a memory mapped locations for software interrupts.
void configClint(uint64_t clintStart, uint64_t clintLimit,
std::function<Hart<URV>*(size_t addr)> swFunc,
std::function<Hart<URV>*(size_t addr)> timerFunc)
{
clintStart_ = clintStart;
clintLimit_ = clintLimit;
clintSoftAddrToHart_ = swFunc;
clintTimerAddrToHart_ = timerFunc;
}
/// Disassemble given instruction putting results on the given
/// stream.
void disassembleInst(uint32_t inst, std::ostream&);
/// Disassemble given instruction putting results on the given
/// stream.
void disassembleInst(const DecodedInst& di, std::ostream&);
/// Disassemble given instruction putting results into the given
/// string.
void disassembleInst(uint32_t inst, std::string& str);
/// Disassemble given instruction putting results into the given
/// string.
void disassembleInst(const DecodedInst& di, std::string& str);
/// Decode given instruction returning a pointer to the
/// instruction information and filling op0, op1 and op2 with the
/// corresponding operand specifier values. For example, if inst
/// is the instruction code for "addi r3, r4, 77", then the
/// returned value would correspond to addi and op0, op1 and op2
/// will be set to 3, 4, and 77 respectively. If an instruction
/// has fewer than 3 operands then only a subset of op0, op1 and
/// op2 will be set. If inst is not a valid instruction , then we
/// return a reference to the illegal-instruction info.
const InstEntry& decode(uint32_t inst, uint32_t& op0, uint32_t& op1,
uint32_t& op2, uint32_t& op3);
/// Similar to the precedning decode method but with decoded data
/// placed in the given DecodedInst object.
void decode(URV address, uint32_t inst, DecodedInst& decodedInst);
/// Return the 32-bit instruction corresponding to the given 16-bit
/// compressed instruction. Return an illegal 32-bit opcode if given
/// 16-bit code is not a valid compressed instruction.
uint32_t expandCompressedInst(uint16_t inst) const;
/// Load the given hex file and set memory locations accordingly.
/// Return true on success. Return false if file does not exists,
/// cannot be opened or contains malformed data.
/// File format: A line either contains @address where address
/// is a hexadecimal memory address or one or more space separated
/// tokens each consisting of two hexadecimal digits.
bool loadHexFile(const std::string& file);
/// Load the given ELF file and place its contents in memory.
/// Return true on success. Return false if file does not exists,
/// cannot be opened or contains malformed data. On success, set
/// entryPoint to the program entry-point of the loaded file. If
/// the to-host-address is not set then set it to the value
/// corresponding to the to-host-symbol if such that symbol is
/// found in the ELF file.
bool loadElfFile(const std::string& file, size_t& entryPoint);
/// Locate the given ELF symbol (symbols are collected for every
/// loaded ELF file) returning true if symbol is found and false
/// otherwise. Set value to the corresponding value if symbol is
/// found.
bool findElfSymbol(const std::string& symbol, ElfSymbol& value) const
{ return memory_.findElfSymbol(symbol, value); }
/// Locate the ELF function containing the give address returning true
/// on success and false on failure. If successful set name to the
/// corresponding function name and symbol to the corresponding symbol
/// value.
bool findElfFunction(URV addr, std::string& name, ElfSymbol& value) const
{ return memory_.findElfFunction(addr, name, value); }
/// Print the ELF symbols on the given stream. Output format:
/// <name> <value>
void printElfSymbols(std::ostream& out) const
{ memory_.printElfSymbols(out); }
/// Set val to the value of the byte at the given address
/// returning true on success and false if address is out of
/// bounds. Memory is little-endian. Bypass physical memory
/// attribute checking if usePma is false.
bool peekMemory(size_t addr, uint8_t& val, bool usePma) const;
/// Half-word version of the preceding method.
bool peekMemory(size_t addr, uint16_t& val, bool usePma) const;
/// Word version of the preceding method.
bool peekMemory(size_t addr, uint32_t& val, bool usePma) const;
/// Double-word version of the preceding method.
bool peekMemory(size_t addr, uint64_t& val, bool usePma) const;
/// Set the memory byte at the given address to the given value.
/// Return true on success and false on failure (address out of
/// bounds, location not mapped, location not writable etc...)
/// Bypass physical memory attribute checking if usePma is false.
bool pokeMemory(size_t addr, uint8_t val, bool usePma);
/// Half-word version of the preceding method.
bool pokeMemory(size_t address, uint16_t val, bool usePma);
/// Word version of the preceding method.
bool pokeMemory(size_t addr, uint32_t val, bool usePma);
/// Double-word version of the preceding method.
bool pokeMemory(size_t addr, uint64_t val, bool usePma);
/// Define value of program counter after a reset.
void defineResetPc(URV addr)
{ resetPc_ = addr; }
/// Define value of program counter after a non-maskable interrupt.
void defineNmiPc(URV addr)
{ nmiPc_ = addr; }
/// Clear/set pending non-maskable-interrupt.
void setPendingNmi(NmiCause cause = NmiCause::UNKNOWN);
/// Clear pending non-maskable-interrupt.
void clearPendingNmi();
/// Define address to which a write will stop the simulator. An
/// sb, sh, or sw instruction will stop the simulator if the write
/// address of the instruction is identical to the given address.
void setToHostAddress(size_t address);
/// Special target program symbol writing to which stops the
/// simulated program.
void setTohostSymbol(const std::string& sym)
{ toHostSym_ = sym; }
/// Special target program symbol writing/reading to/from which
/// writes/reads to/from the console.
void setConsoleIoSymbol(const std::string& sym)
{ consoleIoSym_ = sym; }
/// Undefine address to which a write will stop the simulator
void clearToHostAddress();
/// Set address to the special address writing to which stops the
/// simulation. Return true on success and false on failure (no
/// such address defined).
bool getToHostAddress(size_t& address) const
{ if (toHostValid_) address = toHost_; return toHostValid_; }
/// Support for tracing: Return the pc of the last executed
/// instruction.
URV lastPc() const;
/// Support for tracing: Return the index of the integer register
/// written by the last executed instruction. Return -1 it no
/// integer register was written.
int lastIntReg() const;
/// Support for tracing: Return the index of the floating point
/// register written by the last executed instruction. Return -1
/// it no FP register was written.
int lastFpReg() const;
/// Support for tracing: Fill the csrs vector with the
/// register-numbers of the CSRs written by the execution of the
/// last instruction. CSRs modified as a side effect (e.g. mcycle
/// and minstret) are not included. Fill the triggers vector with
/// the number of the debug-trigger registers written by the
/// execution of the last instruction.
void lastCsr(std::vector<CsrNumber>& csrs,
std::vector<unsigned>& triggers) const;
/// Support for tracing: Set address and value to the memory
/// location changed by the last instruction. Return the size
/// of the change or zero if the last instruction did not change
/// memory in which case address and value are not modified.
/// Returned size is one of 0, 1, 2, 4, or 8.
unsigned lastMemory(uint64_t& addr, uint64_t& value) const;
void lastSyscallChanges(std::vector<std::pair<uint64_t, uint64_t>>& v) const
{ syscall_.getMemoryChanges(v); }
/// Return data address of last executed ld/st instruction.
URV lastLdStAddress() const
{ return ldStAddr_; }
/// Read instruction at given address. Return true on success and
/// false if address is out of memory bounds.
bool readInst(size_t address, uint32_t& instr);
/// Set instruction count limit: When running with tracing the
/// run and the runUntil methods will stop if the retired instruction
/// count (true count and not value of minstret) reaches or exceeds
/// the limit.
void setInstructionCountLimit(uint64_t limit)
{ instCountLim_ = limit; }
/// Return current instruction count limit.
uint64_t getInstructionCountLimit() const
{ return instCountLim_; }
/// Reset executed instruction count.
void setInstructionCount(uint64_t count)
{ instCounter_ = count; }
/// Get executed instruction count.
uint64_t getInstructionCount() const
{ return instCounter_; }
/// Define instruction closed coupled memory (in core instruction memory).
bool defineIccm(size_t addr, size_t size);
/// Define data closed coupled memory (in core data memory).
bool defineDccm(size_t addr, size_t size);
/// Define an area of memory mapped registers.
bool defineMemoryMappedRegisterArea(size_t addr, size_t size);
/// Define a memory mapped register. Address must be within an
/// area already defined using defineMemoryMappedRegisterArea.
bool defineMemoryMappedRegisterWriteMask(size_t addr, uint32_t mask);
/// Called after memory is configured to refine memory access to
/// sections of regions containing ICCM, DCCM or PIC-registers.
void finishCcmConfig(bool iccmRw)
{ memory_.finishCcmConfig(iccmRw); }
/// Turn off all fetch access (except in ICCM regions) then turn
/// it on only in the pages overlapping the given address windows.
/// Return true on success and false on failure (invalid window
/// entry). Do nothing returning true if the windows vector is
/// empty.
bool configMemoryFetch(const std::vector< std::pair<URV,URV> >& windows);
/// Turn off all data access (except in DCCM/PIC regions) then
/// turn it on only in the pages overlapping the given address
/// windows. Return true on success and false on failure (invalid
/// window entry). Do nothing returning true if the windows vector
/// is empty.
bool configMemoryDataAccess(const std::vector< std::pair<URV,URV> >& windows);
/// Direct this hart to take an instruction access fault exception
/// within the next singleStep invocation.
void postInstAccessFault(URV offset)
{ forceFetchFail_ = true; forceFetchFailOffset_ = offset; }
/// Direct this hart to take a data access fault exception within
/// the subsequent singleStep invocation executing a load/store
/// instruction or take an NMI (double-bit-ecc) within the
/// subsequent interrupt if fast-interrupt is enabled.
void postDataAccessFault(URV offset, SecondaryCause cause);
/// Enable printing of load/store data address in instruction
/// trace mode.
void setTraceLoadStore(bool flag)
{ traceLdSt_ = flag; }
/// Return count of traps (exceptions or interrupts) seen by this
/// hart.
uint64_t getTrapCount() const
{ return exceptionCount_ + interruptCount_; }
/// Return count of exceptions seen by this hart.
uint64_t getExceptionCount() const
{ return exceptionCount_; }
/// Return count of interrupts seen by this hart.
uint64_t getInterruptCount() const
{ return interruptCount_; }
/// Set pre and post to the count of "before"/"after" triggers
/// that tripped by the last executed instruction.
void countTrippedTriggers(unsigned& pre, unsigned& post) const
{ csRegs_.countTrippedTriggers(pre, post); }
/// Set t1, t2, and t3 to true if corresponding component (tdata1,
/// tdata2, an tdata3) of given trigger was changed by the current
/// instruction.
void getTriggerChange(URV trigger, bool& t1, bool& t2, bool& t3) const
{ csRegs_.getTriggerChange(trigger, t1, t2, t3); }
/// Apply an imprecise store exception at given address. Return
/// true if address is found exactly once in the store
/// queue. Return false otherwise. Save the given address in
/// mdseac. Set matchCount to the number of entries in the store
/// queue that match the given address.
bool applyStoreException(URV address, unsigned& matchCount);
/// Apply an imprecise load exception at given address. Return
/// true if address is found exactly once in the pending load
/// queue. Return false otherwise. Save the given address in
/// mdseac. Set matchCount to the number of entries in the store
/// queue that match the given address.
bool applyLoadException(URV address, unsigned tag, unsigned& matchCount);
/// This supports the test-bench. Mark load-queue entry matching
/// given address as completed and remove it from the queue. Set
/// match count to 1 if matching entry is found and zero
/// otherwise. Return true if matching entry found. The testbench
/// will invoke this only for loads where the destination register
/// is updated.
bool applyLoadFinished(URV address, unsigned tag, unsigned& matchCount);
/// Enable processing of imprecise load exceptions from test-bench.
void enableBenchLoadExceptions(bool flag)
{ loadQueueEnabled_ = flag; }
/// Set load queue size (used when load exceptions are enabled).
void setLoadQueueSize(unsigned size)
{ maxLoadQueueSize_ = size; }
/// Enable collection of instruction frequencies.
void enableInstructionFrequency(bool b);
/// Enable expedited dispatch of external interrupt handler: Instead of
/// setting pc to the external interrupt handler, we set it to the
/// specific entry associated with the external interrupt id.
void enableFastInterrupts(bool b)
{ fastInterrupts_ = b; }
/// Enable/disable the zba (bit manipulation base) extension. When
/// disabled all the instructions in zba extension result in an
/// illegal instruction exception.
void enableRvzba(bool flag)
{ rvzba_ = flag; }
/// Enable/disable the zbb (bit manipulation base) extension. When
/// disabled all the instructions in zbb extension result in an
/// illegal instruction exception.
void enableRvzbb(bool flag)
{ rvzbb_ = flag; }
/// Enable/disable the zbc (bit manipulation carryless multiply)
/// extension. When disabled all the instructions in zbc extension
/// result in an illegal instruction exception.
void enableRvzbc(bool flag)
{ rvzbc_ = flag; }
/// Enable/disable the zbe (bit manipulation) extension. When
/// disabled all the instructions in zbe extension result in an
/// illegal instruction exception.
void enableRvzbe(bool flag)
{ rvzbe_ = flag; }
/// Enable/disable the zbf (bit manipulation) extension. When
/// disabled all the instructions in zbf extension result in an
/// illegal instruction exception.
void enableRvzbf(bool flag)
{ rvzbf_ = flag; }
/// Enable/disable the zbm (bit manipulation matrix)
/// extension. When disabled all the instructions in zbm extension
/// result in an illegal instruction exception.
void enableRvzbm(bool flag)
{ rvzbm_ = flag; }
/// Enable/disable the zbp (bit manipulation permutation)
/// extension. When disabled all the instructions in zbp extension
/// result in an illegal instruction exception.
void enableRvzbp(bool flag)
{ rvzbp_ = flag; }
/// Enable/disable the zbr (bit manipulation crc)
/// extension. When disbaled all the instructions in zbr extension
/// result in an illegal instruction exception.
void enableRvzbr(bool flag)
{ rvzbr_ = flag; }
/// Enable/disable the zbs (bit manipulation single)
/// extension. When disabled all the instructions in zbs extension
/// result in an illegal instruction exception.
void enableRvzbs(bool flag)
{ rvzbs_ = flag; }
/// Enable/disable the zbt (bit manipulation ternary)
/// extension. When disabled all the instructions in zbt extension
/// result in an illegal instruction exception.
void enableRvzbt(bool flag)
{ rvzbt_ = flag; }
/// Put this hart in debug mode setting the DCSR cause field to
/// the given cause.
void enterDebugMode_(DebugModeCause cause, URV pc);
/// Put this hart in debug mode setting the DCSR cause field to
/// either DEBUGGER or SETP depending on the step bit of DCSR.
void enterDebugMode(URV pc, bool force = false);
/// True if in debug mode.
bool inDebugMode() const
{ return debugMode_; }
/// True if in debug-step mode.
bool inDebugStepMode() const
{ return debugStepMode_; }
/// Take this hart out of debug mode.
void exitDebugMode();
/// Enable/disable imprecise store error rollback. This is useful
/// in test-bench server mode.
void enableStoreErrorRollback(bool flag)
{ storeErrorRollback_ = flag; }
/// Enable/disable imprecise load error rollback. This is useful
/// in test-bench server mode.
void enableLoadErrorRollback(bool flag)
{ loadErrorRollback_ = flag; }
/// Print collected instruction frequency to the given file.
void reportInstructionFrequency(FILE* file) const;
/// Print collected trap stats to the given file.
void reportTrapStat(FILE* file) const;
/// Print collected physical memory protection stats on the given file.
void reportPmpStat(FILE* file) const;
/// Print collected load-reserve/store-conditional stats on the given file.
void reportLrScStat(FILE* file) const;
/// Reset trace data (items changed by the execution of an
/// instruction.)
void clearTraceData();
/// Enable debug-triggers. Without this, triggers will not trip
/// and will not cause exceptions.
void enableTriggers(bool flag)
{ enableTriggers_ = flag; }
/// Enable performance counters (count up for some enabled
/// performance counters when their events do occur).
void enablePerformanceCounters(bool flag)
{ enableCounters_ = flag; }
/// Enable gdb-mode.
void enableGdb(bool flag)
{ enableGdb_ = flag; }
/// Open TCP socket for gdb
bool openTcpForGdb();
/// Set TCP port for gdb
void setGdbTcpPort(int port)
{ gdbTcpPort_ = port; }
/// Enable use of ABI register names (e.g. sp instead of x2) in
/// instruction disassembly.
void enableAbiNames(bool flag)
{ abiNames_ = flag; }
/// Return true if ABI register names are enabled.
bool abiNames() const
{ return abiNames_; }
/// Enable emulation of newlib system calls.
void enableNewlib(bool flag)
{ newlib_ = flag; }
/// Enable emulation of Linux system calls.
void enableLinux(bool flag)
{ linux_ = flag; syscall_.enableLinux(flag); }
/// For Linux emulation: Set initial target program break to the
/// RISCV page address larger than or equal to the given address.
void setTargetProgramBreak(URV addr);
/// For Linux emulation: Put the program arguments on the stack
/// suitable for calling the target program main from _start.
/// Return true on success and false on failure (not all stack
/// area required is writable).
bool setTargetProgramArgs(const std::vector<std::string>& args);
/// Return true if given address is in the data closed coupled
/// memory of this hart.
bool isAddrInDccm(size_t addr) const
{ return memory_.pmaMgr_.isAddrInDccm(addr); }
/// Return true if given address is cacheable.
bool isAddrCacheable(size_t addr) const;
/// Return true if given address is in the memory mapped registers
/// area of this hart.
bool isAddrMemMapped(size_t addr) const
{ return memory_.pmaMgr_.isAddrMemMapped(addr); }
/// Return true if given address is in a readable page.
bool isAddrReadable(size_t addr) const
{ Pma pma = memory_.pmaMgr_.getPma(addr); return pma.isRead(); }
/// Return true if page of given address is in instruction closed
/// coupled memory.
bool isAddrInIccm(size_t addr) const
{ Pma pma = memory_.pmaMgr_.getPma(addr); return pma.isIccm(); }
/// Return true if given data (ld/st) address is external to the hart.
bool isDataAddressExternal(size_t addr) const
{ return memory_.isDataAddressExternal(addr); }
/// Return true if rv32f (single precision floating point)
/// extension is enabled in this hart.
bool isRvf() const
{ return rvf_; }
/// Return true if rv64d (double precision floating point)
/// extension is enabled in this hart.
bool isRvd() const
{ return rvd_; }
/// Return true if rv64e (embedded) extension is enabled in this hart.
bool isRve() const
{ return rve_; }
/// Return true if rv64 (64-bit option) extension is enabled in
/// this hart.
bool isRv64() const
{ return rv64_; }
/// Return true if rvm (multiply/divide) extension is enabled in
/// this hart.
bool isRvm() const
{ return rvm_; }
/// Return true if rvc (compression) extension is enabled in this
/// hart.
bool isRvc() const
{ return rvc_; }
/// Return true if rva (atomic) extension is enabled in this hart.
bool isRva() const
{ return rva_; }
/// Return true if rvs (supervisor-mode) extension is enabled in this
/// hart.
bool isRvs() const
{ return rvs_; }
/// Return true if rvu (user-mode) extension is enabled in this
/// hart.
bool isRvu() const
{ return rvu_; }
/// Return true if rvv (vector) extension is enabled in this hart.
bool isRvv() const
{ return rvv_; }
/// Return true if rvn (user-mode-interrupt) extension is enabled
/// in this hart.
bool isRvn() const
{ return rvn_; }
/// Return true if zba extension is enabled in this hart.
bool isRvzba() const
{ return rvzba_; }
/// Return true if zbb extension is enabled in this hart.
bool isRvzbb() const
{ return rvzbb_; }
/// Return true if zbc extension is enabled in this hart.
bool isRvzbc() const
{ return rvzbc_; }
/// Return true if zbe extension is enabled in this hart.
bool isRvzbe() const
{ return rvzbe_; }
/// Return true if zbf extension is enabled in this hart.
bool isRvzbf() const
{ return rvzbf_; }
/// Return true if zbm extension is enabled in this hart.
bool isRvzbm() const
{ return rvzbm_; }
/// Return true if zbp extension is enabled in this hart.
bool isRvzbp() const
{ return rvzbp_; }
/// Return true if zbr extension is enabled in this hart.
bool isRvzbr() const