forked from byungwoo733/riscv-ovpsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
riscvExceptions.c
1853 lines (1467 loc) · 50.2 KB
/
riscvExceptions.c
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 (c) 2005-2020 Imperas Software Ltd., www.imperas.com
*
* 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.
*
*/
// Standard header files
#include "string.h"
// Imperas header files
#include "hostapi/impAlloc.h"
// VMI header files
#include "vmi/vmiMessage.h"
#include "vmi/vmiRt.h"
// model header files
#include "riscvCSR.h"
#include "riscvDecode.h"
#include "riscvExceptions.h"
#include "riscvExceptionDefinitions.h"
#include "riscvFunctions.h"
#include "riscvMessage.h"
#include "riscvStructure.h"
#include "riscvUtils.h"
#include "riscvVM.h"
#include "riscvVMConstants.h"
////////////////////////////////////////////////////////////////////////////////
// EXCEPTION DEFINITIONS
////////////////////////////////////////////////////////////////////////////////
//
// Fill one member of exceptions
//
#define RISCV_EXCEPTION(_NAME, _ARCH, _DESC) { \
vmiInfo : {name:#_NAME, code:riscv_E_##_NAME, description:_DESC}, \
arch : _ARCH \
}
//
// Fill one member of exceptions with number
//
#define RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, _NUM) \
RISCV_EXCEPTION(_NAME##_NUM, _ARCH, _DESC#_NUM)
//
// Fill eight members of exceptions
//
#define RISCV_EXCEPTIONx8(_NAME, _ARCH, _DESC) \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 0), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 1), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 2), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 3), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 4), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 5), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 6), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 7)
//
// Fill ten members of exceptions
//
#define RISCV_EXCEPTIONx10(_NAME, _ARCH, _DESC) \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 0), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 1), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 2), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 3), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 4), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 5), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 6), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 7), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 8), \
RISCV_EXCEPTION_N(_NAME, _ARCH, _DESC, 9)
//
// Table of exception descriptors
//
static const riscvExceptionDesc exceptions[] = {
////////////////////////////////////////////////////////////////////
// EXCEPTIONS
////////////////////////////////////////////////////////////////////
RISCV_EXCEPTION (InstructionAddressMisaligned, 0, "Fetch from unaligned address"),
RISCV_EXCEPTION (InstructionAccessFault, 0, "No access permission for fetch"),
RISCV_EXCEPTION (IllegalInstruction, 0, "Undecoded, unimplemented or disabled instruction"),
RISCV_EXCEPTION (Breakpoint, 0, "EBREAK instruction executed"),
RISCV_EXCEPTION (LoadAddressMisaligned, 0, "Load from unaligned address"),
RISCV_EXCEPTION (LoadAccessFault, 0, "No access permission for load"),
RISCV_EXCEPTION (StoreAMOAddressMisaligned, 0, "Store/atomic memory operation at unaligned address"),
RISCV_EXCEPTION (StoreAMOAccessFault, 0, "No access permission for store/atomic memory operation"),
RISCV_EXCEPTION (EnvironmentCallFromUMode, ISA_U, "ECALL instruction executed in User mode"),
RISCV_EXCEPTION (EnvironmentCallFromSMode, ISA_S, "ECALL instruction executed in Supervisor mode"),
RISCV_EXCEPTION (EnvironmentCallFromMMode, 0, "ECALL instruction executed in Machine mode"),
RISCV_EXCEPTION (InstructionPageFault, 0, "Page fault at fetch address"),
RISCV_EXCEPTION (LoadPageFault, 0, "Page fault at load address"),
RISCV_EXCEPTION (StoreAMOPageFault, 0, "Page fault at store/atomic memory operation address"),
////////////////////////////////////////////////////////////////////
// INTERRUPTS
////////////////////////////////////////////////////////////////////
RISCV_EXCEPTION (USWInterrupt, ISA_N, "User software interrupt"),
RISCV_EXCEPTION (SSWInterrupt, ISA_S, "Supervisor software interrupt"),
RISCV_EXCEPTION (MSWInterrupt, 0, "Machine software interrupt"),
RISCV_EXCEPTION (UTimerInterrupt, ISA_N, "User timer interrupt"),
RISCV_EXCEPTION (STimerInterrupt, ISA_S, "Supervisor timer interrupt"),
RISCV_EXCEPTION (MTimerInterrupt, 0, "Machine timer interrupt"),
RISCV_EXCEPTION (UExternalInterrupt, ISA_N, "User external interrupt"),
RISCV_EXCEPTION (SExternalInterrupt, ISA_S, "Supervisor external interrupt"),
RISCV_EXCEPTION (MExternalInterrupt, 0, "Machine external interrupt"),
RISCV_EXCEPTIONx10(LocalInterrupt, 0, "Local interrupt "),
RISCV_EXCEPTIONx10(LocalInterrupt1, 0, "Local interrupt 1"),
RISCV_EXCEPTIONx10(LocalInterrupt2, 0, "Local interrupt 2"),
RISCV_EXCEPTIONx10(LocalInterrupt3, 0, "Local interrupt 3"),
RISCV_EXCEPTIONx8 (LocalInterrupt4, 0, "Local interrupt 4"),
////////////////////////////////////////////////////////////////////
// TERMINATOR
////////////////////////////////////////////////////////////////////
{{0}}
};
////////////////////////////////////////////////////////////////////////////////
// UTILITIES
////////////////////////////////////////////////////////////////////////////////
//
// Return current PC
//
inline static Uns64 getPC(riscvP riscv) {
return vmirtGetPC((vmiProcessorP)riscv);
}
//
// Set current PC
//
inline static void setPCxRET(riscvP riscv, Uns64 newPC) {
// mask exception return address to 32 bits if compressed instructions
// are not currently enabled
if(!(riscv->currentArch & ISA_C)) {
newPC &= -4;
}
vmirtSetPC((vmiProcessorP)riscv, newPC);
}
//
// Clear any active exclusive access
//
inline static void clearEA(riscvP riscv) {
riscv->exclusiveTag = RISCV_NO_TAG;
}
//
// Clear any active exclusive access on an xRET, if required
//
inline static void clearEAxRET(riscvP riscv) {
if(!riscv->configInfo.xret_preserves_lr) {
clearEA(riscv);
}
}
//
// Return a Boolean indicating whether an active first-only-fault exception has
// been encountered, in which case no exception should be taken
//
static Bool handleFF(riscvP riscv) {
Bool suppress = False;
// is first-only-fault mode active?
if(riscv->vFirstFault) {
// deactivate first-only-fault mode (whether or not exception is to be
// taken)
riscv->vFirstFault = False;
// special action required only if not the first element
if(RD_CSR(riscv, vstart)) {
// suppress the exception
suppress = True;
// clamp vl to current vstart
riscvSetVL(riscv, RD_CSR(riscv, vstart));
// set matching polymorphic key and clamped vl
riscvRefreshVectorPMKey(riscv);
}
}
return suppress;
}
//
// Halt the passed processor
//
static void haltProcessor(riscvP riscv, riscvDisableReason reason) {
if(!riscv->disable) {
vmirtHalt((vmiProcessorP)riscv);
}
riscv->disable |= reason;
}
//
// Restart the passed processor
//
static void restartProcessor(riscvP riscv, riscvDisableReason reason) {
riscv->disable &= ~reason;
// restart if no longer disabled (maybe from blocked state not visible in
// disable code)
if(!riscv->disable) {
vmirtRestartNext((vmiProcessorP)riscv);
}
}
////////////////////////////////////////////////////////////////////////////////
// TAKING EXCEPTIONS
////////////////////////////////////////////////////////////////////////////////
//
// Forward reference
//
static void enterDM(riscvP riscv, dmCause cause);
//
// Return PC to which to return after taking an exception. For processors with
// instruction table extensions, the address should be the original instruction,
// not the table instruction.
//
static Uns64 getEPC(riscvP riscv) {
Uns8 dsOffset;
Uns64 eretPC = vmirtGetPCDS((vmiProcessorP)riscv, &dsOffset);
return dsOffset ? riscv->jumpBase : eretPC;
}
//
// Return the mode to which to take the given exception or interrupt (mode X)
//
static riscvMode getModeX(
riscvP riscv,
Uns32 mMask,
Uns32 sMask,
riscvException ecode
) {
riscvMode modeY = getCurrentMode(riscv);
riscvMode modeX;
// get mode X implied by delegation registers
if(!(mMask & (1<<ecode))) {
modeX = RISCV_MODE_MACHINE;
} else if(!(sMask & (1<<ecode))) {
modeX = RISCV_MODE_SUPERVISOR;
} else {
modeX = RISCV_MODE_USER;
}
// exception cannot be taken to lower-privilege mode
return (modeX>modeY) ? modeX : modeY;
}
//
// Return the mode to which to take the given interrupt (mode X)
//
static riscvMode getInterruptModeX(riscvP riscv, riscvException ecode) {
return getModeX(riscv, RD_CSR(riscv, mideleg), RD_CSR(riscv, sideleg), ecode);
}
//
// Return the mode to which to take the given exception (mode X)
//
static riscvMode getExceptionModeX(riscvP riscv, riscvException ecode) {
return getModeX(riscv, RD_CSR(riscv, medeleg), RD_CSR(riscv, sedeleg), ecode);
}
//
// Is exception an interrupt?
//
#define IS_INTERRUPT(_EXCEPTION) ((_EXCEPTION) & riscv_E_Interrupt)
//
// Get code from exception
//
#define GET_ECODE(_EXCEPTION) ((_EXCEPTION) & ~riscv_E_Interrupt)
//
// Return interrupt mode (0:direct, 1:vectored) - from privileged ISA version
// 1.10 this is encoded in the [msu]tvec register, but previous versions did
// not support vectored mode except in some custom manner (for example, Andes
// N25 and NX25 processors)
//
inline static Uns8 getIMode(Uns8 customMode, Uns8 tvecMode) {
return tvecMode ? tvecMode : customMode;
}
//
// Update exception state when taking exception to mode X from mode Y
//
#define TARGET_MODE_X(_P, _X, _x, _IS_INT, _ECODE, _EPC, _BASE, _MODE, _TVAL) { \
\
/* get interrupt enable bit for mode X */ \
Uns8 _IE = RD_CSR_FIELD(riscv, mstatus, _X##IE); \
\
/* update interrupt enable and interrupt enable stack */ \
WR_CSR_FIELD(riscv, mstatus, _X##PIE, _IE); \
WR_CSR_FIELD(riscv, mstatus, _X##IE, 0); \
\
/* update cause register */ \
WR_CSR_FIELD(riscv, _x##cause, ExceptionCode, _ECODE); \
WR_CSR_FIELD(riscv, _x##cause, Interrupt, _IS_INT); \
\
/* update writable bits in epc register */ \
Uns64 epcMask = RD_CSR_MASK(riscv, _x##epc); \
WR_CSR_FIELD(riscv, _x##epc, value, (_EPC) & epcMask); \
\
/* update tval register */ \
WR_CSR_FIELD(riscv, _x##tval, value, _TVAL); \
\
/* get exception base address and mode */ \
_BASE = (Addr)RD_CSR_FIELD(riscv, _x##tvec, BASE) << 2; \
_MODE = getIMode(riscv->_X##IMode, RD_CSR_FIELD(riscv, _x##tvec, MODE)); \
}
//
// Does this exception code correspond to a retired instruction?
//
static Bool retiredCode(riscvException exception) {
switch(exception) {
case riscv_E_Breakpoint:
case riscv_E_EnvironmentCallFromUMode:
case riscv_E_EnvironmentCallFromSMode:
case riscv_E_EnvironmentCallFromHMode:
case riscv_E_EnvironmentCallFromMMode:
return True;
default:
return False;
}
}
//
// Does this exception code correspond to an Access Fault?
//
static Bool accessFaultCode(riscvException exception) {
switch(exception) {
case riscv_E_InstructionAccessFault:
case riscv_E_LoadAccessFault:
case riscv_E_StoreAMOAccessFault:
return True;
default:
return False;
}
}
//
// Notify a derived model of trap entry or exception return if required
//
inline static void notifyTrapDerived(
riscvP riscv,
riscvMode mode,
riscvTrapNotifierFn notifier,
void *clientData
) {
if(notifier) {
notifier(riscv, mode, clientData);
}
}
//
// Notify a derived model of exception return if required
//
inline static void notifyERETDerived(riscvP riscv, riscvMode mode) {
riscvExtCBP extCB;
// call derived model preMorph functions if required
for(extCB=riscv->extCBs; extCB; extCB=extCB->next) {
notifyTrapDerived(riscv, mode, extCB->ERETNotifier, extCB->clientData);
}
}
//
// Take processor exception
//
void riscvTakeException(
riscvP riscv,
riscvException exception,
Uns64 tval
) {
if(inDebugMode(riscv)) {
// terminate execution of program buffer
vmirtAbortRepeat((vmiProcessorP)riscv);
enterDM(riscv, DMC_NONE);
} else {
Bool isInt = IS_INTERRUPT(exception);
Uns32 ecode = GET_ECODE(exception);
Uns64 EPC = getEPC(riscv);
Uns64 handlerPC = 0;
riscvMode modeY = getCurrentMode(riscv);
riscvMode modeX;
riscvExtCBP extCB;
Uns64 base;
Uns8 mode;
// adjust baseInstructions based on the exception code to take into
// account whether the previous instruction has retired, unless
// inhibited by mcountinhibit.IR
if(!retiredCode(exception) && !riscvInhibitInstret(riscv)) {
riscv->baseInstructions++;
}
// latch or clear Access Fault detail depending on exception type
if(accessFaultCode(exception)) {
riscv->AFErrorOut = riscv->AFErrorIn;
} else {
riscv->AFErrorOut = riscv_AFault_None;
}
// clear any active exclusive access
clearEA(riscv);
// get exception target mode (X)
if(isInt) {
modeX = getInterruptModeX(riscv, ecode);
} else {
modeX = getExceptionModeX(riscv, ecode);
}
// update state dependent on target exception level
if(modeX==RISCV_MODE_USER) {
// target user mode
TARGET_MODE_X(riscv, U, u, isInt, ecode, EPC, base, mode, tval);
} else if(modeX==RISCV_MODE_SUPERVISOR) {
// target supervisor mode
TARGET_MODE_X(riscv, S, s, isInt, ecode, EPC, base, mode, tval);
WR_CSR_FIELD(riscv, mstatus, SPP, modeY);
} else {
// target machine mode
TARGET_MODE_X(riscv, M, m, isInt, ecode, EPC, base, mode, tval);
WR_CSR_FIELD(riscv, mstatus, MPP, modeY);
}
// handle direct or vectored exception
if((mode == 0) || !isInt) {
handlerPC = base;
} else {
handlerPC = base + (4 * ecode);
}
// switch to target mode
riscvSetMode(riscv, modeX);
// indicate the taken exception
riscv->exception = exception;
// set address at which to execute
vmirtSetPCException((vmiProcessorP)riscv, handlerPC);
// notify derived model of exception entry if required
for(extCB=riscv->extCBs; extCB; extCB=extCB->next) {
notifyTrapDerived(riscv, modeX, extCB->trapNotifier, extCB->clientData);
}
}
}
//
// Return description of the given exception
//
static const char *getExceptionDesc(riscvException exception) {
const char *result = 0;
riscvExceptionDescCP desc;
for(desc=&exceptions[0]; desc->vmiInfo.description && !result; desc++) {
if(desc->vmiInfo.code==exception) {
result = desc->vmiInfo.description;
}
}
return result;
}
//
// Report memory exception in verbose mode
//
static void reportMemoryException(
riscvP riscv,
riscvException exception,
Uns64 tval
) {
if(riscv->verbose) {
vmiMessage("W", CPU_PREFIX "_IMA",
SRCREF_FMT "%s (0x"FMT_Ax")",
SRCREF_ARGS(riscv, getPC(riscv)),
getExceptionDesc(exception), tval
);
}
}
//
// Take processor exception because of memory access error which could be
// suppressed for a fault-only-first instruction
//
void riscvTakeMemoryException(
riscvP riscv,
riscvException exception,
Uns64 tval
) {
// force vstart to zero if required
if(!RD_CSR_MASK(riscv, vstart)) {
WR_CSR(riscv, vstart, 0);
}
// take exception unless fault-only-first mode overrides it
if(!handleFF(riscv)) {
reportMemoryException(riscv, exception, tval);
riscvTakeException(riscv, exception, tval);
}
}
//
// Take Illegal Instruction exception
//
void riscvIllegalInstruction(riscvP riscv) {
Uns64 tval = 0;
// tval is either 0 or the instruction pattern
if(riscv->configInfo.tval_ii_code) {
tval = riscvGetInstruction(riscv, getPC(riscv));
}
riscvTakeException(riscv, riscv_E_IllegalInstruction, tval);
}
//
// Take Instruction Address Misaligned exception
//
void riscvInstructionAddressMisaligned(riscvP riscv, Uns64 tval) {
riscvException exception = riscv_E_InstructionAddressMisaligned;
reportMemoryException(riscv, exception, tval);
riscvTakeException(riscv, exception, tval & -2);
}
//
// Take ECALL exception
//
void riscvECALL(riscvP riscv) {
riscvMode mode = getCurrentMode(riscv);
riscvException exception = riscv_E_EnvironmentCallFromUMode + mode;
riscvTakeException(riscv, exception, 0);
}
////////////////////////////////////////////////////////////////////////////////
// EXCEPTION RETURN
////////////////////////////////////////////////////////////////////////////////
//
// Given a mode to which the processor is attempting to return, check that the
// mode is implemented on this processor and return the minimum implemented
// mode if not
//
static riscvMode getERETMode(riscvP riscv, riscvMode newMode, riscvMode minMode) {
return riscvHasMode(riscv, newMode) ? newMode : minMode;
}
//
// From version 1.12, MRET and SRET clear MPRV when leaving M-mode if new mode
// is less privileged than M-mode
//
static void clearMPRV(riscvP riscv, riscvMode newMode) {
if(
(RISCV_PRIV_VERSION(riscv)>RVPV_20190405) &&
(newMode!=RISCV_MODE_MACHINE)
) {
WR_CSR_FIELD(riscv, mstatus, MPRV, 0);
}
}
//
// Do common actions when returning from an exception
//
static void doERETCommon(
riscvP riscv,
riscvMode retMode,
riscvMode newMode,
Uns64 epc
) {
// switch to target mode
riscvSetMode(riscv, newMode);
// jump to return address
setPCxRET(riscv, epc);
// notify derived model of exception return if required
notifyERETDerived(riscv, retMode);
// check for pending interrupts
riscvTestInterrupt(riscv);
}
//
// Return from M-mode exception
//
void riscvMRET(riscvP riscv) {
// undefined behavior in Debug mode - NOP in this model
if(!inDebugMode(riscv)) {
Uns32 MPP = RD_CSR_FIELD(riscv, mstatus, MPP);
riscvMode minMode = riscvGetMinMode(riscv);
riscvMode newMode = getERETMode(riscv, MPP, minMode);
riscvMode retMode = RISCV_MODE_MACHINE;
// clear any active exclusive access
clearEAxRET(riscv);
// restore previous MIE
WR_CSR_FIELD(riscv, mstatus, MIE, RD_CSR_FIELD(riscv, mstatus, MPIE))
// MPIE=1
WR_CSR_FIELD(riscv, mstatus, MPIE, 1);
// MPP=<minimum_supported_mode>
WR_CSR_FIELD(riscv, mstatus, MPP, minMode);
// clear mstatus.MPRV if required
clearMPRV(riscv, newMode);
// do common return actions
doERETCommon(riscv, retMode, newMode, RD_CSR_FIELD(riscv, mepc, value));
}
}
//
// Return from S-mode exception
//
void riscvSRET(riscvP riscv) {
// undefined behavior in Debug mode - NOP in this model
if(!inDebugMode(riscv)) {
Uns32 SPP = RD_CSR_FIELD(riscv, mstatus, SPP);
riscvMode minMode = riscvGetMinMode(riscv);
riscvMode newMode = getERETMode(riscv, SPP, minMode);
riscvMode retMode = RISCV_MODE_SUPERVISOR;
// clear any active exclusive access
clearEAxRET(riscv);
// restore previous SIE
WR_CSR_FIELD(riscv, mstatus, SIE, RD_CSR_FIELD(riscv, mstatus, SPIE))
// SPIE=1
WR_CSR_FIELD(riscv, mstatus, SPIE, 1);
// SPP=<minimum_supported_mode>
WR_CSR_FIELD(riscv, mstatus, SPP, minMode);
// clear mstatus.MPRV if required
clearMPRV(riscv, newMode);
// do common return actions
doERETCommon(riscv, retMode, newMode, RD_CSR_FIELD(riscv, sepc, value));
}
}
//
// Return from U-mode exception
//
void riscvURET(riscvP riscv) {
// undefined behavior in Debug mode - NOP in this model
if(!inDebugMode(riscv)) {
riscvMode newMode = RISCV_MODE_USER;
riscvMode retMode = RISCV_MODE_USER;
// clear any active exclusive access
clearEAxRET(riscv);
// restore previous UIE
WR_CSR_FIELD(riscv, mstatus, UIE, RD_CSR_FIELD(riscv, mstatus, UPIE))
// UPIE=1
WR_CSR_FIELD(riscv, mstatus, UPIE, 1);
// do common return actions
doERETCommon(riscv, retMode, newMode, RD_CSR_FIELD(riscv, uepc, value));
}
}
////////////////////////////////////////////////////////////////////////////////
// DEBUG MODE
////////////////////////////////////////////////////////////////////////////////
//
// Enter Debug mode
//
static void enterDM(riscvP riscv, dmCause cause) {
if(!inDebugMode(riscv)) {
riscvCountState state;
// get state before possible inhibit update
riscvPreInhibit(riscv, &state);
// update current state
riscv->DM = True;
// save current mode
WR_CSR_FIELD(riscv, dcsr, prv, getCurrentMode(riscv));
// save cause
WR_CSR_FIELD(riscv, dcsr, cause, cause);
// save current instruction address
WR_CSR(riscv, dpc, getEPC(riscv));
// switch to Machine mode
riscvSetMode(riscv, RISCV_MODE_MACHINE);
// refresh state after possible inhibit update
riscvPostInhibit(riscv, &state, False);
}
// interrupt the processor
vmirtInterrupt((vmiProcessorP)riscv);
}
//
// Leave Debug mode
//
static void leaveDM(riscvP riscv) {
riscvMode newMode = RD_CSR_FIELD(riscv, dcsr, prv);
riscvMode retMode = RISCV_MODE_MACHINE;
riscvCountState state;
// get state before possible inhibit update
riscvPreInhibit(riscv, &state);
// update current state
riscv->DM = False;
// clear mstatus.MPRV if required
clearMPRV(riscv, newMode);
// do common return actions
doERETCommon(riscv, retMode, newMode, RD_CSR_FIELD(riscv, dpc, value));
// refresh state after possible inhibit update
riscvPostInhibit(riscv, &state, False);
}
//
// Enter or leave Debug mode
//
void riscvSetDM(riscvP riscv, Bool DM) {
Bool oldDM = riscv->DM;
if((oldDM==DM) || riscv->inSaveRestore) {
// no change in state or state restore
} else if(DM) {
enterDM(riscv, DMC_HALTREQ);
} else {
leaveDM(riscv);
}
}
//
// Instruction step breakpoint callback
//
VMI_ICOUNT_FN(riscvStepExcept) {
riscvP riscv = (riscvP)processor;
if(!inDebugMode(riscv) && RD_CSR_FIELD(riscv, dcsr, step)) {
enterDM(riscv, DMC_STEP);
}
}
//
// Set step breakpoint if required
//
void riscvSetStepBreakpoint(riscvP riscv) {
if(!inDebugMode(riscv) && RD_CSR_FIELD(riscv, dcsr, step)) {
vmirtSetICountInterrupt((vmiProcessorP)riscv, 1);
}
}
//
// Return from Debug mode
//
void riscvDRET(riscvP riscv) {
if(!inDebugMode(riscv)) {
// report FS state
if(riscv->verbose) {
vmiMessage("W", CPU_PREFIX "_NDM",
SRCREF_FMT "Illegal instruction - not debug mode",
SRCREF_ARGS(riscv, getPC(riscv))
);
}
// take Illegal Instruction exception
riscvIllegalInstruction(riscv);
} else {
// leave Debug mode
leaveDM(riscv);
}
}
//
// Take EBREAK exception
//
void riscvEBREAK(riscvP riscv) {
riscvMode mode = getCurrentMode(riscv);
Bool useDM = False;
// determine whether ebreak should cause debug module entry
if(inDebugMode(riscv)) {
useDM = True;
} else if(mode==RISCV_MODE_USER) {
useDM = RD_CSR_FIELD(riscv, dcsr, ebreaku);
} else if(mode==RISCV_MODE_SUPERVISOR) {
useDM = RD_CSR_FIELD(riscv, dcsr, ebreaks);
} else if(mode==RISCV_MODE_MACHINE) {
useDM = RD_CSR_FIELD(riscv, dcsr, ebreakm);
}
if(useDM) {
// don't count the ebreak instruction if dcsr.stopcount is set
if(RD_CSR_FIELD(riscv, dcsr, stopcount)) {
if(!riscvInhibitCycle(riscv)) {
riscv->baseCycles++;
}
if(!riscvInhibitInstret(riscv)) {
riscv->baseInstructions++;
}
}
// handle EBREAK as Debug module action
enterDM(riscv, DMC_EBREAK);
} else {
// handle EBREAK as normal exception
riscvTakeException(riscv, riscv_E_Breakpoint, getPC(riscv));
}
}
////////////////////////////////////////////////////////////////////////////////
// VMI INTERFACE ROUTINES
////////////////////////////////////////////////////////////////////////////////
//
// Read privilege exception handler
//
VMI_RD_PRIV_EXCEPT_FN(riscvRdPrivExcept) {
riscvP riscv = (riscvP)processor;
if(!riscvVMMiss(riscv, domain, MEM_PRIV_R, address, bytes, attrs)) {
*action = VMI_LOAD_STORE_CONTINUE;
}
}
//
// Write privilege exception handler
//
VMI_WR_PRIV_EXCEPT_FN(riscvWrPrivExcept) {
riscvP riscv = (riscvP)processor;
if(!riscvVMMiss(riscv, domain, MEM_PRIV_W, address, bytes, attrs)) {
*action = VMI_LOAD_STORE_CONTINUE;
}
}
//
// Read alignment exception handler
//
VMI_RD_ALIGN_EXCEPT_FN(riscvRdAlignExcept) {
riscvP riscv = (riscvP)processor;
riscvTakeMemoryException(riscv, riscv_E_LoadAddressMisaligned, address);
return 0;
}
//
// Write alignment exception handler
//
VMI_WR_ALIGN_EXCEPT_FN(riscvWrAlignExcept) {
riscvP riscv = (riscvP)processor;
riscvTakeMemoryException(riscv, riscv_E_StoreAMOAddressMisaligned, address);
return 0;
}
//
// Read abort exception handler
//
VMI_RD_ABORT_EXCEPT_FN(riscvRdAbortExcept) {
riscvP riscv = (riscvP)processor;
if(riscv->PTWActive) {
riscv->PTWBadAddr = True;
} else {
riscvTakeMemoryException(riscv, riscv_E_LoadAccessFault, address);
}
}
//
// Write abort exception handler
//
VMI_WR_ABORT_EXCEPT_FN(riscvWrAbortExcept) {
riscvP riscv = (riscvP)processor;
if(riscv->PTWActive) {
riscv->PTWBadAddr = True;
} else {
riscvTakeMemoryException(riscv, riscv_E_StoreAMOAccessFault, address);
}
}
//
// Fetch addresses are always snapped to a 2-byte boundary, irrespective of
// whether compressed instructions are implemented (see comments associated
// with the JALR instruction in the RISC-V User-level ISA)
//
VMI_FETCH_SNAP_FN(riscvFetchSnap) {
return thisPC & -2;
}
//
// Validate instruction fetch from the passed address
//
static Bool validateFetchAddressInt(
riscvP riscv,