-
Notifications
You must be signed in to change notification settings - Fork 0
/
PMMU.h
executable file
·1106 lines (995 loc) · 44.9 KB
/
PMMU.h
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
//
// PMMU.h
// MIPS_Emulator
//
// Created by Matt on 1/6/16.
// Copyright © 2016 Matt. All rights reserved.
//
#ifndef PMMU_h
#define PMMU_h
#include <cstdint> // uint32_t
#include <iostream> // std::cerr
#include "SharedDefs.h" // Shared defines
#include "MMIO_Device.h" // MMIO Device interface
#include "Coprocessor0.h" // Control Coprocessor Interface
#include "MIPSException.h" // MIPS Exception Types
// Paged Memory Management Unit
// Singleton class because there really is only ever one of these
class PMMU {
private:
// Physical Memory
// The table of physical memory frames
static size_t frameTableSize;
static const size_t pageSize = 4096;
static const size_t frameTableMax = 0x100000;
static size_t frameTableRamLimit;
static uint8_t* frameTable[frameTableMax];
static bool frameBoolTable[frameTableMax];
// Memory-Mapped IO
// Quick linear table to check for mapped addresses
// Only 32 addresses right now until I can find a faster solution...
#define MMIOADDRESSTABLEMAX 32
static uint32_t mmioAddressTable[MMIOADDRESSTABLEMAX];
static MMIO_Device* mmioDeviceTable[MMIOADDRESSTABLEMAX];
static uint32_t mmioAddressTableSize;
// TLB and Virtual Memory
// TLB entry definition
#ifdef TEST_PROJECT
public:
#endif
typedef struct TLBEntry_t {
uint32_t Mask;
uint32_t VPN2;
uint8_t ASID;
bool G;
uint32_t PFN0;
uint8_t C0;
bool D0;
bool V0;
uint32_t PFN1;
uint8_t C1;
bool D1;
bool V1;
} TLBEntry_t;
#ifdef TEST_PROJECT
private:
#endif
// Per CPU TLB entry table
static TLBEntry_t TLBTable[MAXCPUS][TLBMAXENTRIES];
/* The following translateVaddr functions differ only by the types of exceptions thrown. */
// Virtual to Physical Address translation for data access
// Also checks address space permissions (i.e. kernel address space accessed from usermode)
inline static void translateVaddrData(uint32_t* vaddr, uint8_t cpunum, Coprocessor0* cop0, bool store) {
// KSEG3
if (((*vaddr) & 0xE0000000) == 0xE0000000) {
// Kernel Mapped
// Check ring level
if (!cop0->inKernelMode()) {
throw AddressErrorDataException();
}
// Do TLB Translation
translateVaddr(vaddr, cpunum, cop0, true, store);
return;
}
// KSSEG
else if (((*vaddr) & 0xC0000000) == 0xC0000000) {
// Supervisor Mapped
// TODO: Supervisor mode is optional and unimplemented
// so this segment is (correctly) treated as kernel space
// Check ring level
if (!cop0->inKernelMode()) {
throw AddressErrorDataException();
}
// Do TLB Translation
translateVaddr(vaddr, cpunum, cop0, true, store);
return;
}
// KSEG1
else if (((*vaddr) & 0xA0000000) == 0xA0000000) {
// Kernel Unmapped Uncached
// Check ring level
if (!cop0->inKernelMode()) {
throw AddressErrorDataException();
}
// Remap to 0
*vaddr -= 0xA0000000;
return;
}
// KSEG0
else if (((*vaddr) & 0x80000000) == 0x80000000) {
// Kernel Unmapped
// Check ring level
if (!cop0->inKernelMode()) {
throw AddressErrorDataException();
}
// Remap to 0
*vaddr -= 0x80000000;
return;
}
// USEG
else {
// Do TLB Translation
translateVaddr(vaddr, cpunum, cop0, true, store);
return;
}
}
// Virtual to Physical Address translation for instruction fetch
// Also checks address space permissions (i.e. kernel address space accessed from usermode)
inline static void translateVaddrIF(uint32_t* vaddr, uint8_t cpunum, Coprocessor0* cop0) {
// KSEG3
if (((*vaddr) & 0xE0000000) == 0xE0000000) {
// Kernel Mapped
// Check ring level
if (!cop0->inKernelMode()) {
throw AddressErrorIFException();
}
// Do TLB Translation
translateVaddr(vaddr, cpunum, cop0, false, false);
return;
}
// KSSEG
else if (((*vaddr) & 0xC0000000) == 0xC0000000) {
// Supervisor Mapped
// TODO: Supervisor mode is optional and unimplemented
// so this segment is (correctly) treated as kernel space
// Check ring level
if (!cop0->inKernelMode()) {
throw AddressErrorIFException();
}
// Do TLB Translation
translateVaddr(vaddr, cpunum, cop0, false, false);
return;
}
// KSEG1
else if (((*vaddr) & 0xA0000000) == 0xA0000000) {
// Kernel Unmapped Uncached
// Check ring level
if (!cop0->inKernelMode()) {
throw AddressErrorIFException();
}
// Remap to 0
*vaddr -= 0xA0000000;
return;
}
// KSEG0
else if (((*vaddr) & 0x80000000) == 0x80000000) {
// Kernel Unmapped
// Check ring level
if (!cop0->inKernelMode()) {
throw AddressErrorIFException();
}
// Remap to 0
*vaddr -= 0x80000000;
return;
}
// USEG
else {
// Do TLB Translation
translateVaddr(vaddr, cpunum, cop0, false, false);
return;
}
}
// Does segment remapping for phys accessors, specifically A0 and 80 address
// spaces which are mapped to the low 512mb region starting at 0.
inline static void translateVaddrPhys(uint32_t* vaddr) {
// KSEG3
if (((*vaddr) & 0xE0000000) == 0xE0000000) {
// Kernel Mapped
return;
}
// KSSEG
else if (((*vaddr) & 0xC0000000) == 0xC0000000) {
// Supervisor Mapped
// TODO: Supervisor mode is optional and unimplemented
// so this segment is (correctly) treated as kernel space
return;
}
// KSEG1
else if (((*vaddr) & 0xA0000000) == 0xA0000000) {
// Kernel Unmapped Uncached
// Remap to 0
*vaddr -= 0xA0000000;
return;
}
// KSEG0
else if (((*vaddr) & 0x80000000) == 0x80000000) {
// Kernel Unmapped
// Remap to 0
*vaddr -= 0x80000000;
return;
}
// USEG
else {
return;
}
}
// Does TLB search and translation for 4k pages (pg 39, vol3)
// data bool indicates whether this is instruction fetch or data access
// store bool indicates whether this was a store or load
inline static void translateVaddr(uint32_t* vaddr, uint8_t cpuNum, Coprocessor0* cop0, bool data, bool store) {
bool found = false;
for (uint8_t i=0; i<TLBMAXENTRIES; i++) {
// Check if VPN2 == Bits 31 to 13 of virtual address and that EntryHi_ASID == TLB entry's ASID
if (((TLBTable[cpuNum][i].VPN2 & ~TLBTable[cpuNum][i].Mask) == (((*vaddr) & 0xFFFFE000) & ~TLBTable[cpuNum][i].Mask))
&& (TLBTable[cpuNum][i].G || (TLBTable[cpuNum][i].ASID == (cop0->getRegister(CO0_ENTRYHI) & ENTRYHI_ASID)))) {
uint8_t EvenOddBit;
uint32_t PAMaskHigh; // These masks are engineered based on a 32bit physical address space
uint32_t PAMaskLow; // They are used to merge the physical frame and virtual address.
switch (TLBTable[cpuNum][i].Mask) {
// This probably needs more work, not to mention
// Pagemask probably needs to be locked at 4KB anyway
case 0x0000: EvenOddBit = 12; PAMaskHigh = 0xFFFFF000; PAMaskLow = 0x00000FFF; break; // 4KB page
case 0x0003: EvenOddBit = 14; PAMaskHigh = 0xFFFFC000; PAMaskLow = 0x00003FFF; break; // 16KB page
case 0x000C: EvenOddBit = 16; PAMaskHigh = 0xFFFF0000; PAMaskLow = 0x0000FFFF; break; // 64KB page
case 0x0030: EvenOddBit = 18; PAMaskHigh = 0xFFFC0000; PAMaskLow = 0x0003FFFF; break; // 256KB page
case 0x00C0: EvenOddBit = 20; PAMaskHigh = 0xFFF00000; PAMaskLow = 0x000FFFFF; break; // 1MB page
case 0x0300: EvenOddBit = 22; PAMaskHigh = 0xFFC00000; PAMaskLow = 0x003FFFFF; break; // 4MB page
case 0x0C00: EvenOddBit = 24; PAMaskHigh = 0xFF000000; PAMaskLow = 0x00FFFFFF; break; // 16MB page
case 0x3000: EvenOddBit = 26; PAMaskHigh = 0xFC000000; PAMaskLow = 0x03FFFFFF; break; // 64MB page
case 0xC000: EvenOddBit = 28; PAMaskHigh = 0xF0000000; PAMaskLow = 0x0FFFFFFF; break; // 256MB page
default: throw std::runtime_error("Invalid PageMask during TLB lookup. This is a bug.");
}
uint32_t pfn;
bool v;
uint8_t c;
bool d;
if (((*vaddr) & (0x1u << EvenOddBit)) == 0) {
pfn = TLBTable[cpuNum][i].PFN0;
v = TLBTable[cpuNum][i].V0;
c = TLBTable[cpuNum][i].C0;
d = TLBTable[cpuNum][i].D0;
// TODO: RI1 XI1 entries
}
else {
pfn = TLBTable[cpuNum][i].PFN1;
v = TLBTable[cpuNum][i].V1;
c = TLBTable[cpuNum][i].C1;
d = TLBTable[cpuNum][i].D1;
// TODO: RI1 XI1 entries
}
if (v == false) {
if (data) {
throw TLBRefillDataException(store);
}
else {
throw TLBRefillIFException();
}
}
// TODO: Config3_RXI and Config3_SM stuff
if ((d == false) && (store == true)) {
throw TLBModifiedException();
}
// Merge final address (finally whew..)
*vaddr = (pfn & PAMaskHigh) | ((*vaddr) & PAMaskLow);
found = true;
break;
}
}
if (found == false) {
if (data == true) {
throw TLBRefillDataException(store);
}
else {
throw TLBRefillIFException();
}
}
}
// Retrieves the pointer to the frame block from a physical address
// otherwise creates a new physical frame entry
// Store will invalidate an atomic boolean (for LL/SC) if it is set
inline static uint8_t* getFramePointer(uint32_t paddr, bool store) {
// Get the frame pointer
paddr >>= 12;
uint8_t* frame = frameTable[paddr];
// Update frame's LLbit
frameBoolTable[paddr] |= store;
if (frame == nullptr) {
// Check if out of memory
if (frameTableSize == frameTableRamLimit) {
// Probably fatal for the VM
std::cerr << "MMU has run out of memory!" << std::endl;
exit(-1);
}
else {
// Allocate a new physical frame
frameTable[paddr] = new uint8_t[pageSize];
#ifdef TEST_PROJECT
// Zero memory for testing
// Disabled for performance reasons during
// normal use as it is not guaranteed
// to be all zeroes
for (uint32_t i=0; i<pageSize; i++) {
frameTable[paddr][i] = 0;
}
#endif
frameTableSize++;
frame = frameTable[paddr];
}
}
return frame;
}
/* Alignment Tests */
// Checks if address is word aligned otherwise throws
// address alignment error for instruction fetch
inline static void checkWordAlignmentIF(uint32_t vaddr) {
if ((vaddr & 0x3) > 0) {
throw AddressErrorIFException();
}
}
// Checks if address is word aligned otherwise throws
// address alignment error for data
inline static void checkWordAlignmentData(uint32_t vaddr) {
if ((vaddr & 0x3) > 0) {
throw AddressErrorDataException();
}
}
// Checks if address is halfword aligned otherwise throws
// address alignment error for data
inline static void checkHalfAlignmentData(uint32_t vaddr) {
if ((vaddr & 0x1) > 0) {
throw AddressErrorDataException();
}
}
public:
PMMU(size_t ramSize);
~PMMU();
// Attaches a device to its given range of addresses
// and calls its initializer
// The device addresses vector is a pair of start/end addresses
static bool attachDevice(MMIO_Device* device) {
if (device->getAddresses().size() % 2 > 0) {
throw std::runtime_error("Error: MMIO Device addresses must be a pair of ranges!");
}
// Check if address range was already mapped
auto addrtbl = device->getAddresses();
for (size_t i=0; i<device->getAddresses().size(); i+=2) {
for (size_t j=0; j<mmioAddressTableSize; j+=2) {
if ((addrtbl[i] >= mmioAddressTable[j]) && (addrtbl[i] <= mmioAddressTable[j+1])) {
return false;
}
if ((addrtbl[i+1] >= mmioAddressTable[j]) && (addrtbl[i+1] <= mmioAddressTable[j+1])) {
return false;
}
}
}
// Map the address ranges
for (size_t i=0; i<device->getAddresses().size(); i+=2) {
mmioAddressTable[mmioAddressTableSize] = addrtbl[i];
mmioAddressTable[mmioAddressTableSize+1] = addrtbl[i+1];
mmioDeviceTable[mmioAddressTableSize] = device;
mmioDeviceTable[mmioAddressTableSize+1] = device;
mmioAddressTableSize+=2;
}
device->initDevice();
return true;
}
/*
* Software/ISA Memory Accessors
*
*/
// Experimental fast instruction fetch
inline static void readWordIF(uint32_t vaddr, uint32_t* word, uint8_t cpunum, Coprocessor0* cop0) {
checkWordAlignmentIF(vaddr);
translateVaddrIF(&vaddr, cpunum, cop0);
uint32_t* frame = (uint32_t*)getFramePointer(vaddr, false);
vaddr &= 0x00000FFF;
frame += vaddr >> 2;
*word = *frame;
*word = __builtin_bswap32(*word);
}
// Memory reading
// Read a byte
inline static void readByte(uint32_t vaddr, uint8_t* byte, uint8_t cpunum, Coprocessor0* cop0) {
translateVaddrData(&vaddr, cpunum, cop0, false);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr >= mmioAddressTable[i]) && (vaddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
*byte = device->readByte(vaddr);
return;
}
}
// No MMIO Device found so retrieve from memory
uint8_t* frame = getFramePointer(vaddr, false);
vaddr &= 0x00000FFF;
*byte = frame[vaddr];
}
// Read a halfword
inline static void readHalf(uint32_t vaddr, uint16_t* half, uint8_t cpunum, Coprocessor0* cop0) {
checkHalfAlignmentData(vaddr);
translateVaddrData(&vaddr, cpunum, cop0, false);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr >= mmioAddressTable[i]) && (vaddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
*half = device->readByte(vaddr);
*half <<= 8;
*half |= device->readByte(vaddr+1);
return;
}
}
// No MMIO Device found so retrieve from memory
uint8_t* frame = getFramePointer(vaddr, false);
vaddr &= 0x00000FFF;
*half = frame[vaddr];
*half <<= 8;
*half |= frame[vaddr+1];
}
// Read an unaligned halfword (lwl, lwr)
inline static void readHalfUnaligned(uint32_t vaddr1, uint32_t vaddr2, uint16_t* half, uint8_t cpunum, Coprocessor0* cop0) {
translateVaddrData(&vaddr1, cpunum, cop0, false);
translateVaddrData(&vaddr2, cpunum, cop0, false);
// Check if MMIO device holds address
bool found1 = false;
bool found2 = false;
MMIO_Device* device1 = nullptr;
MMIO_Device* device2 = nullptr;
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr1 >= mmioAddressTable[i]) && (vaddr1 <= mmioAddressTable[i+1])) {
device1 = mmioDeviceTable[i];
found1 = true;
}
if ((vaddr2 >= mmioAddressTable[i]) && (vaddr2 <= mmioAddressTable[i+1])) {
device2 = mmioDeviceTable[i];
found2 = true;
}
if (found1 && found2) {
break;
}
}
if (found1) {
*half = device1->readByte(vaddr1);
}
else {
uint8_t* frame1 = getFramePointer(vaddr1, false);
vaddr1 &= 0x00000FFF;
*half = frame1[vaddr1];
}
if (found2) {
*half <<= 8;
*half |= device2->readByte(vaddr2);
}
else {
uint8_t* frame2 = getFramePointer(vaddr2, false);
vaddr2 &= 0x00000FFF;
*half <<= 8;
*half |= frame2[vaddr2];
}
}
// Read a word
inline static void readWord(uint32_t vaddr, uint32_t* word, uint8_t cpunum, Coprocessor0* cop0) {
checkWordAlignmentData(vaddr);
translateVaddrData(&vaddr, cpunum, cop0, false);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr >= mmioAddressTable[i]) && (vaddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
*word = device->readByte(vaddr);
*word <<= 8;
*word |= device->readByte(vaddr+1);
*word <<= 8;
*word |= device->readByte(vaddr+2);
*word <<= 8;
*word |= device->readByte(vaddr+3);
return;
}
}
// No MMIO Device found so retrieve from memory
uint8_t* frame = getFramePointer(vaddr, false);
vaddr &= 0x00000FFF;
*word = frame[vaddr];
*word <<= 8;
*word |= frame[vaddr+1];
*word <<= 8;
*word |= frame[vaddr+2];
*word <<= 8;
*word |= frame[vaddr+3];
}
// Read a word and start RWM sequence (LL)
inline static void readWordLL(uint32_t vaddr, uint32_t* word, uint8_t cpunum, Coprocessor0* cop0) {
checkWordAlignmentData(vaddr);
translateVaddrData(&vaddr, cpunum, cop0, false);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr >= mmioAddressTable[i]) && (vaddr <= mmioAddressTable[i+1])) {
// Found device, the RWM sequence will fail (see storeWordSC).
MMIO_Device* device = mmioDeviceTable[i];
*word = device->readByte(vaddr);
*word <<= 8;
*word |= device->readByte(vaddr+1);
*word <<= 8;
*word |= device->readByte(vaddr+2);
*word <<= 8;
*word |= device->readByte(vaddr+3);
return;
}
}
// No MMIO Device found so retrieve from memory
// Get Frame Pointer
uint8_t* frame = getFramePointer(vaddr, false);
// Begin RWM sequence on this frame
frameBoolTable[vaddr >> 12] = false;
vaddr &= 0x00000FFF;
*word = frame[vaddr];
*word <<= 8;
*word |= frame[vaddr+1];
*word <<= 8;
*word |= frame[vaddr+2];
*word <<= 8;
*word |= frame[vaddr+3];
}
// Memory writing
// Store a byte
inline static void storeByte(uint32_t vaddr, uint8_t value, uint8_t cpunum, Coprocessor0* cop0) {
translateVaddrData(&vaddr, cpunum, cop0, true);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr >= mmioAddressTable[i]) && (vaddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
device->storeByte(vaddr, value);
return;
}
}
// No MMIO Device found so save to memory
uint8_t* frame = getFramePointer(vaddr, true);
vaddr &= 0x00000FFF;
frame[vaddr] = value;
}
// Store a halfword
inline static void storeHalf(uint32_t vaddr, uint16_t value, uint8_t cpunum, Coprocessor0* cop0) {
checkHalfAlignmentData(vaddr);
translateVaddrData(&vaddr, cpunum, cop0, true);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr >= mmioAddressTable[i]) && (vaddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
device->storeByte(vaddr, value >> 8);
device->storeByte(vaddr+1, value);
return;
}
}
// No MMIO Device found so save to memory
uint8_t* frame = getFramePointer(vaddr, true);
vaddr &= 0x00000FFF;
frame[vaddr] = value >> 8;
frame[vaddr+1] = value;
}
// Store an unaligned halfword (swl, swr)
inline static void storeHalfUnaligned(uint32_t vaddr1, uint32_t vaddr2, uint16_t value, uint8_t cpunum, Coprocessor0* cop0) {
translateVaddrData(&vaddr1, cpunum, cop0, true);
translateVaddrData(&vaddr2, cpunum, cop0, true);
// Check if MMIO device holds address
bool found1 = false;
bool found2 = false;
MMIO_Device* device1 = nullptr;
MMIO_Device* device2 = nullptr;
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr1 >= mmioAddressTable[i]) && (vaddr1 <= mmioAddressTable[i+1])) {
device1 = mmioDeviceTable[i];
found1 = true;
}
if ((vaddr2 >= mmioAddressTable[i]) && (vaddr2 <= mmioAddressTable[i+1])) {
device2 = mmioDeviceTable[i];
found2 = true;
}
if (found1 && found2) {
break;
}
}
if (found1) {
device1->storeByte(vaddr1, value >> 8);
}
else {
uint8_t* frame1 = getFramePointer(vaddr1, true);
vaddr1 &= 0x00000FFF;
frame1[vaddr1] = value >> 8;
}
if (found2) {
device2->storeByte(vaddr2, value);
}
else {
uint8_t* frame2 = getFramePointer(vaddr2, true);
vaddr2 &= 0x00000FFF;
frame2[vaddr2] = value;
}
}
// Store a word
inline static void storeWord(uint32_t vaddr, uint32_t value, uint8_t cpunum, Coprocessor0* cop0) {
checkWordAlignmentData(vaddr);
translateVaddrData(&vaddr, cpunum, cop0, true);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr >= mmioAddressTable[i]) && (vaddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
device->storeByte(vaddr, value >> 24);
device->storeByte(vaddr+1, value >> 16);
device->storeByte(vaddr+2, value >> 8);
device->storeByte(vaddr+3, value);
return;
}
}
// No MMIO Device found so save to memory
uint8_t* frame = getFramePointer(vaddr, true);
vaddr &= 0x00000FFF;
frame[vaddr] = value >> 24;
frame[vaddr+1] = value >> 16;
frame[vaddr+2] = value >> 8;
frame[vaddr+3] = value;
}
// Conditionally stores a word and returns success or failure (SC)
inline static bool storeWordSC(uint32_t vaddr, uint32_t value, uint8_t cpunum, Coprocessor0* cop0) {
checkWordAlignmentData(vaddr);
translateVaddrData(&vaddr, cpunum, cop0, true);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((vaddr >= mmioAddressTable[i]) && (vaddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
device->storeByte(vaddr, value >> 24);
device->storeByte(vaddr+1, value >> 16);
device->storeByte(vaddr+2, value >> 8);
device->storeByte(vaddr+3, value);
// FIXME: Implementation dependent on whether such devices are sychronizable in such fashion
// so for now, no.
return false;
}
}
// Check if frame was modified
// Note we're passing in a false for the store reference here!
// This guarantees that the frame isn't NULL.
uint8_t* frame = getFramePointer(vaddr, false);
// Get bool value
// If dirty bit is set then fail
if (frameBoolTable[vaddr >> 12]) {
return false;
}
// Otherwise modify and report success
else {
frameBoolTable[(vaddr&0xFFFFF000) >> 12] = true;
vaddr &= 0x00000FFF;
frame[vaddr] = value >> 24;
frame[vaddr+1] = value >> 16;
frame[vaddr+2] = value >> 8;
frame[vaddr+3] = value;
return true;
}
}
/*
* TLB Instructions
*
*/
// TLBP
// Probes the TLB for a matching entry and updates CO0_Index Register
// with the result of the search.
inline static void probeTLB(uint8_t cpuNum, Coprocessor0* cop0) {
// Set Index bit 31 to 1
cop0->setRegisterHW(CO0_INDEX, 0x1u << 31);
// Get current EntryHi
uint32_t entryhi = cop0->getRegister(CO0_ENTRYHI);
uint8_t asid = entryhi & ENTRYHI_ASID;
entryhi &= ENTRYHI_FULLVPN;
for (uint8_t i=0; i<TLBMAXENTRIES; i++) {
// There has to be a way to simplify this...
// Pg 268 vol 3
if (((TLBTable[cpuNum][i].VPN2 & ~TLBTable[cpuNum][i].Mask) == (entryhi & ~TLBTable[cpuNum][i].Mask))
&& (TLBTable[cpuNum][i].G || (TLBTable[cpuNum][i].ASID == asid))) {
// Update CO0_Index
cop0->setRegisterHW(CO0_INDEX, i);
break;
}
}
}
// TLBR
// Reads the TLB entry specified by CO0_Index Register
inline static void readTLB(uint8_t cpuNum, Coprocessor0* cop0) {
// Check ring level
if (!cop0->inKernelMode()) {
throw CoprocessorUnusableException(CoprocessorUnusableException::FaultingCoprocessor::CO0);
}
uint8_t i = cop0->getRegister(CO0_INDEX);
if (i >= TLBMAXENTRIES) {
// Undefined
return;
}
// Update PageMask
cop0->andRegisterHW(CO0_PAGEMASK, ~PAGEMASK_MASK);
cop0->orRegisterHW(CO0_PAGEMASK, TLBTable[cpuNum][i].Mask);
// Update EntryHi
cop0->setRegisterHW(CO0_ENTRYHI, (TLBTable[cpuNum][i].VPN2 & ~TLBTable[cpuNum][i].Mask) | TLBTable[cpuNum][i].ASID);
// Update EntryLo1
cop0->setRegisterHW(CO0_ENTRYLO1, ((TLBTable[cpuNum][i].PFN1 & ~TLBTable[cpuNum][i].Mask) >> 6u)
| (TLBTable[cpuNum][i].C1 << 3) | (TLBTable[cpuNum][i].D1 << 2) | (TLBTable[cpuNum][i].V1 << 1)
| (TLBTable[cpuNum][i].G));
// Update EntryLo0
cop0->setRegisterHW(CO0_ENTRYLO0, ((TLBTable[cpuNum][i].PFN0 & ~TLBTable[cpuNum][i].Mask) >> 6u)
| (TLBTable[cpuNum][i].C0 << 3) | (TLBTable[cpuNum][i].D0 << 2) | (TLBTable[cpuNum][i].V0 << 1)
| (TLBTable[cpuNum][i].G));
}
// TLBWI
// Writes a TLB entry to the value of CO0_Index Register
inline static void writeIndexedTLB(uint8_t cpuNum, Coprocessor0* cop0) {
// Check ring level
if (!cop0->inKernelMode()) {
throw CoprocessorUnusableException(CoprocessorUnusableException::FaultingCoprocessor::CO0);
}
uint8_t i = cop0->getRegister(CO0_INDEX);
if (i >= TLBMAXENTRIES) {
// Undefined
return;
}
// Insert TLB Entries
TLBTable[cpuNum][i].Mask = cop0->getRegister(CO0_PAGEMASK) & PAGEMASK_MASK;
TLBTable[cpuNum][i].VPN2 = (cop0->getRegister(CO0_ENTRYHI) & ENTRYHI_FULLVPN) & ~TLBTable[cpuNum][i].Mask;
TLBTable[cpuNum][i].ASID = cop0->getRegister(CO0_ENTRYHI) & ENTRYHI_ASID;
TLBTable[cpuNum][i].G = ((cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_G) > 0) && ((cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_G) > 0);
TLBTable[cpuNum][i].PFN1 = ((cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_PFN) & ~TLBTable[cpuNum][i].Mask) << 6u;
TLBTable[cpuNum][i].C1 = (cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_C) >> 3u;
TLBTable[cpuNum][i].D1 = (cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_D) > 0;
TLBTable[cpuNum][i].V1 = (cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_V) > 0;
TLBTable[cpuNum][i].PFN0 = ((cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_PFN) & ~TLBTable[cpuNum][i].Mask) << 6u;
TLBTable[cpuNum][i].C0 = (cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_C) >> 3u;
TLBTable[cpuNum][i].D0 = (cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_D) > 0;
TLBTable[cpuNum][i].V0 = (cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_V) > 0;
}
// TLBWR
// Writes a TLB entry to the value of CO0_Random Register
inline static void writeRandomTLB(uint8_t cpuNum, Coprocessor0* cop0) {
// Check ring level
if (!cop0->inKernelMode()) {
throw CoprocessorUnusableException(CoprocessorUnusableException::FaultingCoprocessor::CO0);
}
// Get random index
uint8_t i = cop0->getRegister(CO0_RANDOM);
// Insert TLB Entries
TLBTable[cpuNum][i].Mask = cop0->getRegister(CO0_PAGEMASK) & PAGEMASK_MASK;
TLBTable[cpuNum][i].VPN2 = (cop0->getRegister(CO0_ENTRYHI) & ENTRYHI_FULLVPN) & ~TLBTable[cpuNum][i].Mask;
TLBTable[cpuNum][i].ASID = cop0->getRegister(CO0_ENTRYHI) & ENTRYHI_ASID;
TLBTable[cpuNum][i].G = ((cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_G) > 0) && ((cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_G) > 0);
TLBTable[cpuNum][i].PFN1 = ((cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_PFN) & ~TLBTable[cpuNum][i].Mask) << 6u;
TLBTable[cpuNum][i].C1 = (cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_C) >> 3u;
TLBTable[cpuNum][i].D1 = (cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_D) > 0;
TLBTable[cpuNum][i].V1 = (cop0->getRegister(CO0_ENTRYLO1) & ENTRYLO1_V) > 0;
TLBTable[cpuNum][i].PFN0 = ((cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_PFN) & ~TLBTable[cpuNum][i].Mask) << 6u;
TLBTable[cpuNum][i].C0 = (cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_C) >> 3u;
TLBTable[cpuNum][i].D0 = (cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_D) > 0;
TLBTable[cpuNum][i].V0 = (cop0->getRegister(CO0_ENTRYLO0) & ENTRYLO0_V) > 0;
}
/*
* Physical Memory Accessors
*
* For use by non ISA code, e.g. ELF loaders
* unit tests etc. Do not throw errors for alignment
* or address translation.
*/
// Memory reading
// Read a byte
inline static void readBytePhys(uint32_t paddr, uint8_t* byte) {
translateVaddrPhys(&paddr);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((paddr >= mmioAddressTable[i]) && (paddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
*byte = device->readByte(paddr);
return;
}
}
// No MMIO Device found so retrieve from memory
uint8_t* frame = getFramePointer(paddr, false);
paddr &= 0x00000FFF;
*byte = frame[paddr];
}
// Read a halfword
inline static void readHalfPhys(uint32_t paddr, uint16_t* half) {
translateVaddrPhys(&paddr);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((paddr >= mmioAddressTable[i]) && (paddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
*half = device->readByte(paddr);
*half <<= 8;
*half |= device->readByte(paddr+1);
return;
}
}
// No MMIO Device found so retrieve from memory
uint8_t* frame = getFramePointer(paddr, false);
paddr &= 0x00000FFF;
*half = frame[paddr];
*half <<= 8;
*half |= frame[paddr+1];
}
// Read an unaligned halfword (lwl, lwr)
inline static void readHalfUnalignedPhys(uint32_t paddr1, uint32_t paddr2, uint16_t* half) {
translateVaddrPhys(&paddr1);
translateVaddrPhys(&paddr2);
// Check if MMIO device holds address
bool found1 = false;
bool found2 = false;
MMIO_Device* device1 = nullptr;
MMIO_Device* device2 = nullptr;
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((paddr1 >= mmioAddressTable[i]) && (paddr1 <= mmioAddressTable[i+1])) {
device1 = mmioDeviceTable[i];
found1 = true;
}
if ((paddr2 >= mmioAddressTable[i]) && (paddr2 <= mmioAddressTable[i+1])) {
device2 = mmioDeviceTable[i];
found2 = true;
}
if (found1 && found2) {
break;
}
}
if (found1) {
*half = device1->readByte(paddr1);
}
else {
uint8_t* frame1 = getFramePointer(paddr1, false);
paddr1 &= 0x00000FFF;
*half = frame1[paddr1];
}
if (found2) {
*half <<= 8;
*half |= device2->readByte(paddr2);
}
else {
uint8_t* frame2 = getFramePointer(paddr2, false);
paddr2 &= 0x00000FFF;
*half <<= 8;
*half |= frame2[paddr2];
}
}
// Read a word
inline static void readWordPhys(uint32_t paddr, uint32_t* word) {
translateVaddrPhys(&paddr);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((paddr >= mmioAddressTable[i]) && (paddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
*word = device->readByte(paddr);
*word <<= 8;
*word |= device->readByte(paddr+1);
*word <<= 8;
*word |= device->readByte(paddr+2);
*word <<= 8;
*word |= device->readByte(paddr+3);
return;
}
}
// No MMIO Device found so retrieve from memory
uint8_t* frame = getFramePointer(paddr, false);
paddr &= 0x00000FFF;
*word = frame[paddr];
*word <<= 8;
*word |= frame[paddr+1];
*word <<= 8;
*word |= frame[paddr+2];
*word <<= 8;
*word |= frame[paddr+3];
}
// Memory writing
// Store a byte
inline static void storeBytePhys(uint32_t paddr, uint8_t value) {
translateVaddrPhys(&paddr);
// Check if MMIO device holds address
for (uint32_t i=0; i+1<mmioAddressTableSize; i+=2) {
if ((paddr >= mmioAddressTable[i]) && (paddr <= mmioAddressTable[i+1])) {
// Found device
MMIO_Device* device = mmioDeviceTable[i];
device->storeByte(paddr, value);
return;
}
}
// No MMIO Device found so save to memory
uint8_t* frame = getFramePointer(paddr, true);
paddr &= 0x00000FFF;
frame[paddr] = value;
}
// Store a halfword
inline static void storeHalfPhys(uint32_t paddr, uint16_t value) {
translateVaddrPhys(&paddr);
// Check if MMIO device holds address