-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathriscvVM.c
2885 lines (2358 loc) · 72 KB
/
riscvVM.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-2019 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 <stdio.h> // for sprintf
// Imperas header files
#include "hostapi/impAlloc.h"
#include "hostapi/typeMacros.h"
// VMI header files
#include "vmi/vmiAttrs.h"
#include "vmi/vmiMessage.h"
#include "vmi/vmiRt.h"
#include "vmi/vmiTypes.h"
// Model header files
#include "riscvExceptions.h"
#include "riscvFunctions.h"
#include "riscvMessage.h"
#include "riscvStructure.h"
#include "riscvUtils.h"
#include "riscvVM.h"
#include "riscvVMConstants.h"
//
// This is the highest possible address
//
#define RISCV_MAX_ADDR (-1)
//
// tlbEntry opaque type pointer
//
DEFINE_S(tlbEntry);
//
// Union representing simulated ASID (including bits depending on MSTATUS
// register settings)
//
typedef union riscvSimASIDU {
// field view
struct {
Uns16 ASID : 16; // ASID
Bool MXR : 1; // MSTATUS make-executable-readable
Bool SUM : 1; // MSTATUS supervisor-user-access
Uns32 _u1 : 14; // padding bits
} f;
// full simulated ASID view
Uns32 u32;
} riscvSimASID;
//
// Structure representing a single TLB entry
//
typedef struct tlbEntryS {
// entry virtual address range
Uns64 lowVA; // entry low virtual address
Uns64 highVA; // entry high virtual address
// entry low physical address
Uns64 PA;
// simulated ASID when mapped (including MSTATUS bits that affect it)
riscvSimASID simASID;
// entry attributes
Uns8 isMapped : 4; // TLB entry mapped (per mode)
Uns32 priv : 3; // access privilege
Uns32 U : 1; // user accessible?
Uns32 G : 1; // global bit
Uns32 A : 1; // accessed bit (read or written)
Uns32 D : 1; // dirty bit (written)
Bool artifact : 1; // entry created by artifact lookup (do not match)
Uns32 _u1 : 20; // spare bits
// range LUT entry (for fast lookup by address)
union {
struct tlbEntryS *nextFree; // when in free list
vmiRangeEntryP lutEntry; // equivalent range entry when mapped
Uns64 _size; // for 32/64-bit host compatibility
};
} tlbEntry;
//
// Structure representing a TLB
//
typedef struct riscvTLBS {
vmiRangeTableP lut; // range LUT entry (for fast lookup by address)
tlbEntryP free; // list of free TLB entries available for reuse
} riscvTLB;
//
// Structure describing mapping constraints for TLB entry
//
typedef struct tlbMapInfoS {
Uns64 lowVA; // low virtual address mapped
Uns64 highVA; // high virtual address mapped
memPriv priv; // effective privilege
} tlbMapInfo, *tlbMapInfoP;
//
// Enumeration of supported translation modes
//
typedef enum VAModeE {
VAM_Sv32 = 1, // Sv32 translation (32-bit VA)
VAM_Sv39 = 8, // Sv39 translation (39-bit VA)
VAM_Sv48 = 9, // Sv48 translation (48-bit VA)
} VAMode;
////////////////////////////////////////////////////////////////////////////////
// UTILITIES
////////////////////////////////////////////////////////////////////////////////
//
// This macro implements an iterator to traverse all TLB entries in a range
//
#define ITER_TLB_ENTRY_RANGE(_RISCV, _TLB, _LOWVA, _HIGHVA, _ENTRY, _B) { \
\
tlbEntryP _ENTRY; \
Uns64 _lowVA = _LOWVA; \
Uns64 _highVA = _HIGHVA; \
\
for( \
_ENTRY = firstTLBEntryRange(_RISCV, _TLB, _lowVA, _highVA); \
_ENTRY; \
_ENTRY = nextTLBEntryRange(_RISCV, _TLB, _lowVA, _highVA) \
) { \
_B; \
} \
}
//
// Return current program counter
//
static Uns64 getPC(riscvP riscv) {
return vmirtGetPC((vmiProcessorP)riscv);
}
//
// Return table entry implied global state
//
inline static Bool getG(riscvP riscv, Bool G) {
return G || !getASIDMask(riscv);
}
//
// Is hardware update of PTE A bit supported?
//
inline static Bool updatePTEA(riscvP riscv) {
return riscv->configInfo.updatePTEA;
}
//
// Is hardware update of PTE D bit supported?
//
inline static Bool updatePTED(riscvP riscv) {
return riscv->configInfo.updatePTED;
}
//
// Return TLB entry ASID
//
inline static Uns32 getEntryASID(tlbEntryP entry) {
return entry->simASID.f.ASID;
}
//
// Given a TLB entry, return the corresponding simulated ASID including MSTATUS
// bits that affect whether entries are used that wee in force when the entry
// was created
//
inline static Uns32 getEntrySimASID(tlbEntryP entry) {
return entry->simASID.u32;
}
//
// Return TLB entry low VA
//
inline static Uns64 getEntryLowVA(tlbEntryP entry) {
return entry->lowVA;
}
//
// Return TLB entry high VA
//
inline static Uns64 getEntryHighVA(tlbEntryP entry) {
return entry->highVA;
}
//
// Return TLB entry low PA
//
inline static Uns64 getEntryLowPA(tlbEntryP entry) {
return entry->PA;
}
//
// Return TLB entry high PA
//
inline static Uns64 getEntryHighPA(tlbEntryP entry) {
return entry->PA + entry->highVA - entry->lowVA;
}
//
// Return TLB entry ASID mask
//
inline static Uns32 getEntryASIDMask(tlbEntryP entry, riscvMode mode) {
riscvSimASID ASIDMask = {f:{MXR:1}};
// include ASID field only if this entry is not global
if(!entry->G) {
ASIDMask.f.ASID = -1;
}
// include U field only if this entry is user-accessible and in Supervisor
// mode
if(entry->U && (mode==RISCV_MODE_SUPERVISOR)) {
ASIDMask.f.SUM = 1;
}
return ASIDMask.u32;
}
//
// Get effective value of MSTATUS.MXR
//
inline static Bool getMXR(riscvP riscv) {
return RD_CSR_FIELD(riscv, mstatus, MXR);
}
//
// Get effective value of MSTATUS.SUM
//
inline static Bool getSUM(riscvP riscv) {
return RD_CSR_FIELD(riscv, mstatus, SUM);
}
//
// Get effective value of MSTATUS.MPRV
//
inline static Bool getMPRV(riscvP riscv) {
return RD_CSR_FIELD(riscv, mstatus, MPRV);
}
//
// Get effective value of MSTATUS.MPP
//
inline static riscvMode getMPP(riscvP riscv) {
return RD_CSR_FIELD(riscv, mstatus, MPP);
}
//
// Get effective value of MSTATUS.SUM
//
static Uns32 getActiveASID(riscvP riscv) {
return RD_CSR_FIELD(riscv, satp, ASID);
}
//
// Return address mask for the given number of bits
//
inline static Uns64 getAddressMask(Uns32 bits) {
return (bits==64) ? -1 : ((1ULL<<bits)-1);
}
//
// Is code demain required for the passed privilege?
//
inline static Bool isFetch(memPriv priv) {
return (priv & MEM_PRIV_X) && True;
}
//
// Does the entry ASID match the given TLB ASID? (always true for global
// entries)
//
inline static Bool matchASID(Uns32 ASID, tlbEntryP entry) {
return entry->G || (ASID==getEntryASID(entry));
}
//
// Return the current simulated ASID, taking into account MSTATUS bits that
// affect whether entries are used
//
static riscvSimASID getSimASID(riscvP riscv) {
return (riscvSimASID){
f: {
ASID : getActiveASID(riscv),
MXR : getMXR(riscv),
SUM : getSUM(riscv)
}
};
}
//
// Get root page table address
//
inline static Uns64 getPTETableAddress(Uns64 PPN) {
return PPN << RISCV_PAGE_SHIFT;
}
//
// Get root page table address
//
static Uns64 getRootTableAddress(riscvP riscv) {
return getPTETableAddress(RD_CSR_FIELD(riscv, satp, PPN));
}
//
// Validate entry access permissions
//
static memPriv checkEntryPermission(
riscvP riscv,
riscvMode mode,
tlbEntryP entry,
memPriv requiredPriv
) {
memPriv priv = entry->priv;
// add read permission if executable and MSTATUS.MXR=1 (must be done
// *before* mode-specific check below to correctly handle version-specific
// SUM behavior)
if((priv&MEM_PRIV_X) && getMXR(riscv)) {
priv |= MEM_PRIV_R;
}
if(mode==RISCV_MODE_USER) {
// no access in user mode unless U=1
if(!entry->U) {
priv = MEM_PRIV_NONE;
}
} else if(entry->U) {
// no access in supervisor mode if U=1 unless MSTATUS.SUM=1
if(!getSUM(riscv)) {
priv = MEM_PRIV_NONE;
} else if(RISCV_PRIV_VERSION(riscv) >= RVPV_1_11) {
// behavior from privileged architecture 1.11 only - never
// executable in Supervisor mode if U=1
priv &= ~MEM_PRIV_X;
}
}
// indicate whether permission is allowed
return ((priv & requiredPriv) == requiredPriv) ? priv : MEM_PRIV_NONE;
}
//
// Is the VA valid? (VPNextend must extend VPN)
//
static Bool validVA(Int64 VPN, Int32 VPNextend) {
return (VPN>=0) ? (VPNextend==0) : (VPNextend==-1);
}
//
// Return the PMP memory domain to use for the passed memory access type
//
static memDomainP getPMPDomainPriv(riscvP riscv, riscvMode mode, memPriv priv) {
return riscv->pmpDomains[mode][isFetch(priv)];
}
//
// Return the memory domain to use for page table walk accesses
//
inline static memDomainP getPTWDomain(riscvP riscv) {
return getPMPDomainPriv(riscv, RISCV_MODE_SUPERVISOR, MEM_PRIV_RW);
}
//
// Read an entry from a page table (returning invalid all-zero entry if the
// lookup fails)
//
static Uns64 readPageTableEntry(
riscvP riscv,
memDomainP domain,
Uns64 PTEAddr,
Uns32 entryBytes,
memAccessAttrs attrs
) {
memEndian endian = riscv->dendian;
Uns64 result;
// enter PTW context
riscv->PTWActive = True;
riscv->PTWBadAddr = False;
// read 4-byte or 8-byte entry
if(entryBytes==4) {
result = vmirtRead4ByteDomain(domain, PTEAddr, endian, attrs);
} else {
result = vmirtRead8ByteDomain(domain, PTEAddr, endian, attrs);
}
// exit PTW context
riscv->PTWActive = False;
return result;
}
//
// Write an entry in a page table
//
static void writePageTableEntry(
riscvP riscv,
memDomainP domain,
Uns64 PTEAddr,
Uns32 entryBytes,
memAccessAttrs attrs,
Uns64 value
) {
memEndian endian = riscv->dendian;
// enter PTW context
riscv->PTWActive = True;
riscv->PTWBadAddr = False;
// write 4-byte or 8-byte entry
if(riscv->artifactAccess) {
// no action if an artifact access (e.g. page table walk initiated by
// pseudo-register write)
} else if(entryBytes==4) {
vmirtWrite4ByteDomain(domain, PTEAddr, endian, value, attrs);
} else {
vmirtWrite8ByteDomain(domain, PTEAddr, endian, value, attrs);
}
// exit PTW context
riscv->PTWActive = False;
}
////////////////////////////////////////////////////////////////////////////////
// PAGE TABLE WALK ERROR HANDLING AND REPORTING
////////////////////////////////////////////////////////////////////////////////
//
// This enumerates page table walk errors
//
typedef enum pteErrorE {
PTEE_VAEXTEND, // page table entry VA has invalid extension
PTEE_READ, // page table entry load failed
PTEE_WRITE, // page table entry store failed
PTEE_V0, // page table entry V=0
PTEE_R0W1, // page table entry R=0 W=1
PTEE_LEAF, // page table entry must be leaf level
PTEE_ALIGN, // page table entry is a misaligned superpage
PTEE_PRIV, // page table entry does not allow access
PTEE_A0, // page table entry A=0
PTEE_D0, // page table entry D=0
} pteError;
//
// Return character corresponding to privilege
//
static char getAccessChar(memPriv requiredPriv) {
char result = 0;
switch(requiredPriv) {
case MEM_PRIV_R:
result = 'R' ;
break;
case MEM_PRIV_W:
result = 'W' ;
break;
case MEM_PRIV_X:
result = 'X' ;
break;
default:
VMI_ABORT("Invalid privilege %u", requiredPriv); // LCOV_EXCL_LINE
break;
}
return result;
}
//
// Handle a specific error arising during a page table walk
//
static riscvException handlePTWException(
riscvP riscv,
riscvMode mode,
tlbEntryP entry,
memPriv requiredPriv,
Uns64 PTEAddr,
pteError error
) {
// this enumerates generated exceptions
typedef enum pteExceptionE {
PTX_LOAD_ACCESS, // load access fault
PTX_STORE_ACCESS, // store access fault
PTX_PAGE, // page fault
} pteException;
// structure holding information about a specific error
typedef struct pteErrorDescS {
Bool warn; // whether to generate warning (otherwise, info)
pteException exception; // exception description
const char *desc; // description
} pteErrorDesc;
// exception table
static const pteErrorDesc map[] = {
[PTEE_VAEXTEND] = {1, PTX_PAGE, "VA has invalid extension" },
[PTEE_READ] = {1, PTX_LOAD_ACCESS, "load failed" },
[PTEE_WRITE] = {1, PTX_STORE_ACCESS, "store failed" },
[PTEE_V0] = {0, PTX_PAGE, "V=0" },
[PTEE_R0W1] = {1, PTX_PAGE, "R=0 and W=1" },
[PTEE_LEAF] = {1, PTX_PAGE, "must be leaf level" },
[PTEE_ALIGN] = {1, PTX_PAGE, "is a misaligned superpage"},
[PTEE_PRIV] = {0, PTX_PAGE, "does not allow access" },
[PTEE_A0] = {0, PTX_PAGE, "A=0" },
[PTEE_D0] = {0, PTX_PAGE, "D=0" },
};
// get description for this error
const pteErrorDesc *desc = &map[error];
const char *severity = 0;
// determine whether PTW exception should be reported, and with what
// severity
if(desc->warn) {
severity = "W";
} else if(RISCV_DEBUG_MMU(riscv)) {
severity = "I";
}
// generate message for error if required
if(severity) {
vmiMessage(severity, CPU_PREFIX "_PTWE",
NO_SRCREF_FMT "Page table entry %s "
"[VA=0x"FMT_Ax" PTEAddress=0x"FMT_Ax" access=%c]",
NO_SRCREF_ARGS(riscv),
desc->desc,
entry->lowVA,
PTEAddr,
getAccessChar(requiredPriv)
);
}
// return appropriate exception
if(desc->exception==PTX_LOAD_ACCESS) {
return riscv_E_LoadAccessFault;
} else if(desc->exception==PTX_STORE_ACCESS) {
return riscv_E_StoreAMOAccessFault;
} else if(requiredPriv==MEM_PRIV_R) {
return riscv_E_LoadPageFault;
} else if(requiredPriv==MEM_PRIV_W) {
return riscv_E_StoreAMOPageFault;
} else {
return riscv_E_InstructionPageFault;
}
}
//
// Macro encapsulating PTW error generation
//
#define PTE_ERROR(_CODE) return handlePTWException( \
riscv, mode, entry, requiredPriv, PTEAddr, PTEE_##_CODE \
)
////////////////////////////////////////////////////////////////////////////////
// Sv32 PAGE TABLE WALK
////////////////////////////////////////////////////////////////////////////////
//
// This is the VPN page size in bits
//
#define SV32_VPN_SHIFT 10
//
// This is the VPN page mask
//
#define SV32_VPN_MASK ((1<<SV32_VPN_SHIFT)-1)
//
// Sv32 VA
//
typedef union Sv32VAU {
Uns32 raw;
struct {
Uns32 pageOffset : 12;
Uns32 VPN : 20;
} fields;
} Sv32VA;
//
// Sv32 PA
//
typedef union Sv32PAU {
Uns64 raw;
struct {
Uns64 pageOffset : 12;
Uns64 PPN : 52;
} fields;
} Sv32PA;
//
// Sv32 entry
//
typedef union Sv32EntryU {
Uns32 raw;
struct {
Uns32 V : 1;
Uns32 priv : 3;
Uns32 U : 1;
Uns32 G : 1;
Uns32 A : 1;
Uns32 D : 1;
Uns32 RSW : 2;
Uns32 PPN : 22;
} fields;
} Sv32Entry;
//
// Return Sv32 VPN[level]
//
static Uns32 getSv32VPNi(Sv32VA VA, Uns32 level) {
return (VA.fields.VPN >> (level*SV32_VPN_SHIFT)) & SV32_VPN_MASK;
}
//
// Look up any TLB entry for the passed address using Sv32 mode and fill byref
// argument 'entry' with the details.
//
static riscvException tlbLookupSv32(
riscvP riscv,
riscvMode mode,
tlbEntryP entry,
memPriv requiredPriv,
memAccessAttrs attrs
) {
memDomainP domain = getPTWDomain(riscv);
Sv32VA VA = {raw : entry->lowVA};
Sv32PA PA;
Sv32Entry PTE;
Addr PTEAddr;
Addr a;
Int32 i;
// clear page offset bits (not relevant for entry creation)
VA.fields.pageOffset = 0;
// do table walk to find ultimate PTE
for(
i=1, a=getRootTableAddress(riscv);
i>=0;
i--, a=getPTETableAddress(PTE.fields.PPN)
) {
// get next page table entry address
PTEAddr = a + getSv32VPNi(VA, i)*4;
// read entry from memory
PTE.raw = readPageTableEntry(riscv, domain, PTEAddr, 4, attrs);
// return with page-fault exception if an invalid entry or entry with
// permission combination that is reserved, or break from the loop if
// a leaf entry is found
if(riscv->PTWBadAddr) {
PTE_ERROR(READ);
} else if(!PTE.fields.V) {
PTE_ERROR(V0);
} else if((PTE.fields.priv&MEM_PRIV_RW) == MEM_PRIV_W) {
PTE_ERROR(R0W1);
} else if(PTE.fields.priv) {
break;
}
}
// return with page-fault exception if leaf entry was not found
if(i<0) {
PTE_ERROR(LEAF);
}
// construct entry low PA
PA.fields.PPN = PTE.fields.PPN;
PA.fields.pageOffset = 0;
// calculate entry size
Uns32 size = 1 << ((i*SV32_VPN_SHIFT) + RISCV_PAGE_SHIFT);
// return with page-fault exception if invalid page alignment
if(PA.raw & (size-1)) {
PTE_ERROR(ALIGN);
}
// fill TLB entry virtual address range
entry->lowVA = VA.raw & -size;
entry->highVA = entry->lowVA + size - 1;
// fill TLB entry low physical address
entry->PA = PA.raw;
// fill TLB entry attributes
entry->priv = PTE.fields.priv;
entry->U = PTE.fields.U;
entry->G = getG(riscv, PTE.fields.G);
entry->A = PTE.fields.A;
entry->D = PTE.fields.D;
// return with page-fault exception if permissions are invalid
if(!checkEntryPermission(riscv, mode, entry, requiredPriv)) {
PTE_ERROR(PRIV);
}
// update entry A/D bits if required
Bool doWrite = False;
if(entry->A) {
// A bit is already set
} else if(!updatePTEA(riscv)) {
// A bit not yet set, no hardware support
PTE_ERROR(A0);
} else {
// A bit is set on any access
entry->A = PTE.fields.A = 1;
doWrite = True;
}
// D bit is set on any write
if(entry->D || !(requiredPriv & MEM_PRIV_W)) {
// D bit is already set or not required
} else if(!updatePTED(riscv)) {
// D bit not yet set, no hardware support
PTE_ERROR(D0);
} else {
entry->D = PTE.fields.D = 1;
doWrite = True;
}
// write PTE if it has changed
if(doWrite) {
writePageTableEntry(riscv, domain, PTEAddr, 4, attrs, PTE.raw);
// error if entry is not writable
if(riscv->PTWBadAddr) {
PTE_ERROR(WRITE);
}
}
// entry is valid
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Sv39 PAGE TABLE WALK
////////////////////////////////////////////////////////////////////////////////
//
// This is the VPN page size in bits
//
#define SV39_VPN_SHIFT 9
//
// This is the VPN page mask
//
#define SV39_VPN_MASK ((1<<SV39_VPN_SHIFT)-1)
//
// Sv39 VA
//
typedef union Sv39VAU {
Uns64 raw;
struct {
Uns32 pageOffset : 12;
Int64 VPN : 27;
Int32 VPNextend : 25;
} fields;
} Sv39VA;
//
// Sv39 PA
//
typedef union Sv39PAU {
Uns64 raw;
struct {
Uns64 pageOffset : 12;
Uns64 PPN : 52;
} fields;
} Sv39PA;
//
// Sv39 entry
//
typedef union Sv39EntryU {
Uns64 raw;
struct {
Uns32 V : 1;
Uns32 priv : 3;
Uns32 U : 1;
Uns32 G : 1;
Uns32 A : 1;
Uns32 D : 1;
Uns32 RSW : 2;
Uns64 PPN : 44;
Uns32 _u1 : 10;
} fields;
} Sv39Entry;
//
// Return Sv39 VPN[level]
//
static Uns32 getSv39VPNi(Sv39VA VA, Uns32 level) {
return (VA.fields.VPN >> (level*SV39_VPN_SHIFT)) & SV39_VPN_MASK;
}
//
// Look up any TLB entry for the passed address using Sv39 mode and fill byref
// argument 'entry' with the details.
//
static riscvException tlbLookupSv39(
riscvP riscv,
riscvMode mode,
tlbEntryP entry,
memPriv requiredPriv,
memAccessAttrs attrs
) {
memDomainP domain = getPTWDomain(riscv);
Sv39VA VA = {raw : entry->lowVA};
Addr PTEAddr = 0;
Sv39PA PA;
Sv39Entry PTE;
Addr a;
Int32 i;
// validate VPNextend correctly extends VPN
if(!validVA(VA.fields.VPN, VA.fields.VPNextend)) {
PTE_ERROR(VAEXTEND);
}
// clear page offset bits (not relevant for entry creation)
VA.fields.pageOffset = 0;
// do table walk to find ultimate PTE
for(
i=2, a=getRootTableAddress(riscv);
i>=0;
i--, a=getPTETableAddress(PTE.fields.PPN)
) {
// get next page table entry address
PTEAddr = a + getSv39VPNi(VA, i)*8;
// read entry from memory
PTE.raw = readPageTableEntry(riscv, domain, PTEAddr, 8, attrs);
// return with page-fault exception if an invalid entry or entry with
// permission combination that is reserved, or break from the loop if
// a leaf entry is found
if(riscv->PTWBadAddr) {
PTE_ERROR(READ);
} else if(!PTE.fields.V) {
PTE_ERROR(V0);
} else if((PTE.fields.priv&MEM_PRIV_RW) == MEM_PRIV_W) {
PTE_ERROR(R0W1);
} else if(PTE.fields.priv) {
break;
}
}
// return with page-fault exception if leaf entry was not found
if(i<0) {
PTE_ERROR(LEAF);
}
// construct entry low PA
PA.fields.PPN = PTE.fields.PPN;
PA.fields.pageOffset = 0;
// calculate entry size
Uns64 size = 1ULL << ((i*SV39_VPN_SHIFT) + RISCV_PAGE_SHIFT);
// return with page-fault exception if invalid page alignment
if(PA.raw & (size-1)) {
PTE_ERROR(ALIGN);
}
// fill TLB entry virtual address range
entry->lowVA = VA.raw & -size;
entry->highVA = entry->lowVA + size - 1;
// fill TLB entry low physical address
entry->PA = PA.raw;
// fill TLB entry attributes
entry->priv = PTE.fields.priv;
entry->U = PTE.fields.U;
entry->G = getG(riscv, PTE.fields.G);
entry->A = PTE.fields.A;
entry->D = PTE.fields.D;
// return with page-fault exception if permissions are invalid
if(!checkEntryPermission(riscv, mode, entry, requiredPriv)) {
PTE_ERROR(PRIV);
}
// update entry A/D bits if required
Bool doWrite = False;
if(entry->A) {
// A bit is already set
} else if(!updatePTEA(riscv)) {
// A bit not yet set, no hardware support
PTE_ERROR(A0);
} else {
// A bit is set on any access
entry->A = PTE.fields.A = 1;
doWrite = True;
}
// D bit is set on any write
if(entry->D || !(requiredPriv & MEM_PRIV_W)) {
// D bit is already set or not required
} else if(!updatePTED(riscv)) {
// D bit not yet set, no hardware support
PTE_ERROR(D0);
} else {
entry->D = PTE.fields.D = 1;
doWrite = True;
}
// write PTE if it has changed
if(doWrite) {
writePageTableEntry(riscv, domain, PTEAddr, 8, attrs, PTE.raw);
// error if entry is not writable
if(riscv->PTWBadAddr) {
PTE_ERROR(WRITE);
}
}
// entry is valid
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// Sv48 PAGE TABLE WALK
////////////////////////////////////////////////////////////////////////////////
//
// This is the VPN page size in bits
//
#define SV48_VPN_SHIFT 9
//
// This is the VPN page mask
//
#define SV48_VPN_MASK ((1<<SV48_VPN_SHIFT)-1)
//
// Sv48 VA
//
typedef union Sv48VAU {
Uns64 raw;
struct {
Uns32 pageOffset : 12;
Int64 VPN : 36;
Int32 VPNextend : 16;
} fields;
} Sv48VA;
//
// Sv48 PA
//
typedef union Sv48PAU {
Uns64 raw;
struct {
Uns64 pageOffset : 12;
Uns64 PPN : 52;
} fields;
} Sv48PA;