-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsi_opt.m
1052 lines (875 loc) · 24.9 KB
/
msi_opt.m
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
-- two-state 4-hop VI protocol
----------------------------------------------------------------------
-- Constants
----------------------------------------------------------------------
const
ProcCount: 3; -- number processors
ValueCount: 2; -- number of data values.
VC0: 0; -- low priority
VC1: 1;
VC2: 2;
QMax: 2;
NumVCs: VC2 - VC0 + 1;
NetMax: 10;
----------------------------------------------------------------------
-- Types
----------------------------------------------------------------------
type
Proc: scalarset(ProcCount); -- unordered range of processors
Value: scalarset(ValueCount); -- arbitrary values for tracking coherence
Home: enum { HomeType }; -- need enumeration for IsMember calls
Node: union { Home , Proc };
ProcCountType: 0..ProcCount;
VCType: VC0..NumVCs-1;
MessageType: enum {
GetS,
GetSAck,
GetEAck,
GetM,
GetMAck,
Upgrade,
UpgradeAck,
PutS,
PutSAck,
PutM,
PutMAck,
ModE,
ModEAck,
PutE,
PutEAck,
WB,
WBAck,
Inv,
InvAck,
NAck
};
Message:
Record
mtype: MessageType;
src: Node;
-- do not need a destination for verification; the destination is indicated by which array entry in the Net the message is placed
vc: VCType;
val: Value;
reqsrc: Node;
End;
HomeState:
Record
state: enum {
H_Modified, -- steady state: M
H_Shared, -- steady state: S
H_Invalid, -- steady state: I
H_Exclusive, -- steady state: E
HT_SS_GetS,
HT_MM_GetM,
HT_MS,
HT_SM_GetM,
HT_SM_Upgrade,
HT_ES,
HT_EM,
HT_ME
}; --transient states during recall
owner: Node;
newOwner: Node;
sharers: multiset [ProcCount] of Node; --No need for sharers in this protocol, but this is a good way to represent them
mshr: multiset [ProcCount] of Node;
val: Value;
End;
ProcState:
Record
state: enum {
P_Modified,
P_Shared,
P_Invalid,
P_Exclusive,
PT_IM,
PT_MI,
PT_IS,
PT_SI,
PT_SM,
PT_EI,
PT_EM,
PT_ME
};
val: Value;
End;
----------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------
var
HomeNode: HomeState;
Procs: array [Proc] of ProcState;
Net: array [Node] of multiset [NetMax] of Message; -- One multiset for each destination - messages are arbitrarily reordered by the multiset
InBox: array [Node] of array [VCType] of Message; -- If a message is not processed, it is placed in InBox, blocking that virtual channel
msg_processed: boolean;
LastWrite: Value; -- Used to confirm that writes are not lost; this variable would not exist in real hardware
----------------------------------------------------------------------
-- Procedures
----------------------------------------------------------------------
Procedure Send(mtype:MessageType;
dst:Node;
src:Node;
vc:VCType;
val:Value;
reqsrc: Node;
);
var msg:Message;
Begin
Assert (MultiSetCount(i:Net[dst], true) < NetMax) "Too many messages";
msg.mtype := mtype;
msg.src := src;
msg.vc := vc;
msg.val := val;
msg.reqsrc := reqsrc;
MultiSetAdd(msg, Net[dst]);
End;
Procedure ErrorUnhandledMsg(msg:Message; n:Node);
Begin
error "Unhandled message type!";
End;
Procedure ErrorUnhandledState();
Begin
error "Unhandled state!";
End;
-- These aren't needed for Valid/Invalid protocol, but this is a good way of writing these functions
Procedure AddToSharersList(n:Node);
Begin
if MultiSetCount(i:HomeNode.sharers, HomeNode.sharers[i] = n) = 0
then
MultiSetAdd(n, HomeNode.sharers);
endif;
End;
Procedure AddToMSHR(n:Node);
Begin
if MultiSetCount(i:HomeNode.mshr, HomeNode.mshr[i] = n) = 0
then
MultiSetAdd(n, HomeNode.mshr);
endif;
End;
Function IsSharer(n:Node) : Boolean;
Begin
return MultiSetCount(i:HomeNode.sharers, HomeNode.sharers[i] = n) > 0
End;
Function CountMSHR() : ProcCountType;
Begin
return MultiSetCount(i:HomeNode.mshr, true);
End;
Function CountSharers() : ProcCountType;
Begin
return MultiSetCount(i:HomeNode.sharers, true);
End;
Function CountModified() : ProcCountType;
var ret: ProcCountType;
Begin
ret := 0;
for n:Proc do
if Procs[n].state = P_Modified
then
ret := ret + 1;
endif;
endfor;
return ret;
End;
Procedure RemoveFromSharersList(n:Node);
Begin
MultiSetRemovePred(i:HomeNode.sharers, HomeNode.sharers[i] = n);
End;
Procedure RemoveFromMSHR(n:Node);
Begin
MultiSetRemovePred(i:HomeNode.mshr, HomeNode.mshr[i] = n);
End;
-- Sends a message to all sharers except rqst
Procedure SendInvReqToSharers(rqst:Node);
Begin
for n:Node do
if (IsMember(n, Proc) &
MultiSetCount(i:HomeNode.sharers, HomeNode.sharers[i] = n) != 0)
then
if n != rqst
then
-- Send invalidation message here
Send(Inv, n, HomeType, VC1, UNDEFINED, rqst);
endif;
endif;
endfor;
End;
Procedure ForwardReqToSharers(rqst:Node; reqType: MessageType);
var flag: Boolean;
Begin
flag := false;
for n:Node do
if (IsMember(n, Proc) &
MultiSetCount(i:HomeNode.sharers, HomeNode.sharers[i] = n) != 0)
then
if n != rqst
then
-- Send invalidation message here
if (flag = false)
then
flag := true;
Send(reqType, n, HomeType, VC1, UNDEFINED, rqst);
AddToMSHR(n);
endif;
endif;
endif;
endfor;
End;
Procedure CopySharersToMSHR();
Begin
undefine HomeNode.mshr;
for n:Node do
if (IsMember(n, Proc) &
MultiSetCount(i:HomeNode.sharers, HomeNode.sharers[i] = n) != 0)
then
MultiSetAdd(n, HomeNode.mshr);
endif;
endfor;
End;
Procedure HomeReceive(msg:Message);
var cnt:ProcCountType; -- for counting sharers
Begin
-- Debug output may be helpful:
-- put "Receiving "; put msg.mtype; put " on VC"; put msg.vc;
-- put " at home -- "; put HomeNode.state;
-- The line below is not needed in Valid/Invalid protocol. However, the
-- compiler barfs if we put this inside a switch, so it is useful to
-- pre-calculate the sharer count here
cnt := MultiSetCount(i:HomeNode.sharers, true);
-- default to 'processing' message. set to false otherwise
msg_processed := true;
switch HomeNode.state
case H_Invalid:
switch msg.mtype
case GetS:
HomeNode.state := H_Exclusive;
HomeNode.owner := msg.src;
Send(GetEAck, msg.src, HomeType, VC2, HomeNode.val, msg.reqsrc);
case GetM:
HomeNode.state := H_Modified;
HomeNode.owner := msg.src;
Send(GetMAck, msg.src, HomeType, VC2, HomeNode.val, msg.reqsrc);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case H_Modified:
Assert (IsUndefined(HomeNode.owner) = false)
"HomeNode has no owner, but line is Modified";
switch msg.mtype
case GetS:
HomeNode.state := HT_MS;
AddToSharersList(HomeNode.owner);
AddToSharersList(msg.src);
Send(GetS, HomeNode.owner, HomeType, VC1, UNDEFINED, msg.src);
case PutM:
assert (msg.src = HomeNode.owner) "Writeback from non-owner";
HomeNode.state := H_Invalid;
HomeNode.val := msg.val;
Send(PutMAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
undefine HomeNode.owner;
case GetM:
assert (msg.src != HomeNode.owner) "GetM involked by owner";
HomeNode.state := HT_MM_GetM;
HomeNode.newOwner := msg.src;
Send(Inv, HomeNode.owner, HomeType, VC1, UNDEFINED, msg.src);
case WB:
assert(HomeNode.owner = msg.src)
"WB called from non-owner";
HomeNode.state := H_Exclusive;
HomeNode.val := msg.val;
Send(WBAck, HomeNode.owner, HomeType, VC2, UNDEFINED, msg.src);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case H_Shared:
switch msg.mtype
case GetM:
assert(!IsSharer(msg.src))
"GetM called from sharer";
HomeNode.state := HT_SM_GetM;
HomeNode.newOwner := msg.src;
Send(GetMAck, msg.src, HomeType, VC2, HomeNode.val, msg.reqsrc);
SendInvReqToSharers(msg.src);
CopySharersToMSHR();
case Upgrade:
assert(IsSharer(msg.src))
"Upgrade called from non-sharer";
HomeNode.state := HT_SM_Upgrade;
HomeNode.newOwner := msg.src;
Send(UpgradeAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
SendInvReqToSharers(msg.src);
CopySharersToMSHR();
RemoveFromMSHR(msg.src);
case PutS:
RemoveFromSharersList(msg.src);
if (cnt = 1)
then
HomeNode.state := H_Invalid;
endif;
Send(PutSAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case GetS:
ForwardReqToSharers(msg.src, GetS);
AddToSharersList(msg.src);
HomeNode.state := HT_SS_GetS;
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case H_Exclusive:
switch msg.mtype
case GetM:
if (msg.src != HomeNode.owner)
then
HomeNode.state := HT_EM;
HomeNode.newOwner := msg.src;
Send(Inv, HomeNode.owner, HomeType, VC1, UNDEFINED, msg.reqsrc);
else
HomeNode.state := H_Modified;
Send(GetMAck, HomeNode.owner, HomeType, VC2, UNDEFINED, msg.reqsrc);
endif;
case GetS:
assert(HomeNode.owner != msg.src)
"GetS called from exclusive owner";
HomeNode.state := HT_ES;
AddToSharersList(HomeNode.owner);
AddToSharersList(msg.src);
Send(GetS, HomeNode.owner, HomeType, VC1, UNDEFINED, msg.src);
case ModE:
assert(HomeNode.owner = msg.src)
"ModE called from non-owner";
HomeNode.state := H_Modified;
Send(ModEAck, msg.src, HomeType, VC2, UNDEFINED, msg.src);
case PutE:
assert(HomeNode.owner = msg.src)
"PutE called from non-owner";
HomeNode.state := H_Invalid;
undefine HomeNode.owner;
Send(PutEAck, msg.src, HomeType, VC2, UNDEFINED, msg.src);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case HT_ES:
switch msg.mtype
case GetSAck:
undefine HomeNode.owner;
HomeNode.state := H_Shared;
case GetS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case GetM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case ModE:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutE:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case Upgrade:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case HT_EM:
switch msg.mtype
case InvAck:
assert(HomeNode.owner = msg.src)
"InvAck to HomeNode from non-owner";
HomeNode.owner := HomeNode.newOwner;
undefine HomeNode.newOwner;
HomeNode.state := H_Modified;
case GetS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case GetM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case ModE:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutE:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case HT_MM_GetM:
switch msg.mtype
case InvAck:
assert(msg.src = HomeNode.owner) "InvAck from non-owner";
HomeNode.owner := HomeNode.newOwner;
undefine HomeNode.newOwner;
HomeNode.state := H_Modified;
case GetM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case GetS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case WB:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case HT_SS_GetS:
switch msg.mtype
case GetSAck:
RemoveFromMSHR(msg.src);
if (CountMSHR() = 0)
then
AddToSharersList(msg.reqsrc);
HomeNode.state := H_Shared;
endif;
case GetS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case GetM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case Upgrade:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case HT_MS:
switch msg.mtype
case GetSAck:
undefine HomeNode.owner;
HomeNode.state := H_Shared;
HomeNode.val := msg.val;
case GetS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case GetM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case Upgrade:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case WB:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case HT_SM_GetM:
switch msg.mtype
case InvAck:
RemoveFromMSHR(msg.src);
if CountMSHR() = 0
then
undefine HomeNode.mshr;
undefine HomeNode.sharers;
HomeNode.owner := HomeNode.newOwner;
undefine HomeNode.newOwner;
HomeNode.state := H_Modified;
endif;
case GetS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case GetM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case Upgrade:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case WB:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
case HT_SM_Upgrade:
switch msg.mtype
case InvAck:
RemoveFromMSHR(msg.src);
if CountMSHR() = 0
then
undefine HomeNode.mshr;
undefine HomeNode.sharers;
HomeNode.owner := HomeNode.newOwner;
undefine HomeNode.newOwner;
HomeNode.state := H_Modified;
endif;
case GetS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case GetM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutS:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case PutM:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case Upgrade:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
case WB:
Send(NAck, msg.src, HomeType, VC2, UNDEFINED, UNDEFINED);
else
ErrorUnhandledMsg(msg, HomeType);
endswitch;
endswitch;
End;
Procedure ProcReceive(msg:Message; p:Proc);
Begin
-- put "Receiving "; put msg.mtype; put " on VC"; put msg.vc;
-- put " at proc "; put p; put "\n";
-- default to 'processing' message. set to false otherwise
msg_processed := true;
alias ps:Procs[p].state do
alias pv:Procs[p].val do
switch ps
case P_Modified:
switch msg.mtype
case GetS:
Send(GetSAck, msg.reqsrc, p, VC2, pv, msg.reqsrc);
Send(GetSAck, HomeType, p, VC2, pv, msg.reqsrc);
ps := P_Shared;
case Inv:
Send(InvAck,HomeType, p, VC2, pv, msg.reqsrc);
ps := P_Invalid;
undefine pv;
case NAck:
ps := ps;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case P_Shared:
switch msg.mtype
case Inv:
Send(InvAck, HomeType, p, VC2, UNDEFINED, msg.reqsrc);
undefine pv;
ps := P_Invalid;
case GetS:
Send(GetSAck, HomeType, p, VC2, pv, msg.reqsrc);
Send(GetSAck, msg.reqsrc, p, VC2, pv, msg.reqsrc);
case NAck:
ps := ps;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case P_Invalid:
switch msg.mtype
case NAck:
ps := ps;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case P_Exclusive:
switch msg.mtype
case GetS:
Send(GetSAck, HomeType, p, VC2, pv, msg.reqsrc);
Send(GetSAck, msg.reqsrc, p, VC2, pv, msg.reqsrc);
ps := P_Shared;
case Inv:
Send(InvAck, HomeType, p, VC2, pv, msg.reqsrc);
undefine pv;
ps := P_Invalid;
case NAck:
ps := ps;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_EI:
switch msg.mtype
case PutEAck:
undefine pv;
ps := P_Invalid;
case NAck:
ps := P_Exclusive;
case Inv:
msg_processed := false;
case GetS:
msg_processed := false;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_EM:
switch msg.mtype
case GetMAck:
ps := P_Modified;
case NAck:
ps := P_Exclusive;
case Inv:
msg_processed := false;
case GetS:
msg_processed := false;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_ME:
switch msg.mtype
case WBAck:
ps := P_Exclusive;
case NAck:
ps := P_Modified;
case Inv:
msg_processed := false;
case GetS:
msg_processed := false;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_IM:
switch msg.mtype
case GetMAck:
assert(msg.reqsrc = p) "Received other's GetMAck";
pv := msg.val;
ps := P_Modified;
case NAck:
ps := P_Invalid;
case Inv:
msg_processed := false;
case GetS:
msg_processed := false;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_IS:
switch msg.mtype
case GetSAck:
assert(msg.reqsrc = p) "Received other's GetSAck";
pv := msg.val;
ps := P_Shared;
case GetEAck:
assert(msg.reqsrc = p) "Received other's GetEAck";
pv := msg.val;
ps := P_Exclusive;
case NAck:
ps := P_Invalid;
case Inv:
msg_processed := false;
case GetS:
msg_processed := false;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_SI:
switch msg.mtype
case PutSAck:
undefine pv;
ps := P_Invalid;
case NAck:
ps := P_Shared;
case Inv:
msg_processed := false;
case GetS:
msg_processed := false;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_SM:
switch msg.mtype
case UpgradeAck:
ps := P_Modified;
case NAck:
ps := P_Shared;
case Inv:
msg_processed := false;
case GetS:
msg_processed := false;
else
ErrorUnhandledMsg(msg, p);
endswitch;
case PT_MI:
switch msg.mtype
case PutMAck:
ps := P_Invalid;
undefine pv;
case NAck:
ps := P_Modified;
case Inv:
msg_processed := false;
case GetS:
msg_processed := false;
else
ErrorUnhandledMsg(msg, p);
endswitch;
----------------------------
-- Error catch
----------------------------
else
ErrorUnhandledState();
endswitch;
endalias;
endalias;
End;
----------------------------------------------------------------------
-- Rules
----------------------------------------------------------------------
-- Processor actions (affecting coherency)
ruleset n:Proc Do
alias p:Procs[n] Do
ruleset v:Value Do
rule "Store Hit"
(p.state = P_Modified)
==>
p.val := v;
LastWrite := v; --We use LastWrite to sanity check that reads receive the value of the last write
endrule;
endruleset;
rule "Load Miss"
p.state = P_Invalid
==>
Send(GetS, HomeType, n, VC0, UNDEFINED, n);
p.state := PT_IS;
endrule;
rule "Store Miss"
p.state = P_Invalid
==>
Send(GetM, HomeType, n, VC0, UNDEFINED, n);
p.state := PT_IM;
endrule;
rule "Store Hit No Permission"
p.state = P_Shared
==>
Send(Upgrade, HomeType, n, VC0, UNDEFINED, n);
p.state := PT_SM;
endrule;
rule "Evict Shared"
p.state = P_Shared
==>
Send(PutS, HomeType, n, VC0, UNDEFINED, n);
p.state := PT_SI;
endrule;
rule "Evict Modified"
p.state = P_Modified
==>
Send(PutM, HomeType, n, VC0, p.val, n);
p.state := PT_MI;
endrule;
rule "Evict Exclusive"
p.state = P_Exclusive
==>
Send(PutE, HomeType, n, VC0, UNDEFINED, n);
p.state := PT_EI;
endrule;
rule "Store to Exclusive"
p.state = P_Exclusive
==>
Send(GetM, HomeType, n, VC0, UNDEFINED, n);
p.state := PT_EM;
endrule;
rule "Self Downgrade"
p.state = P_Modified
==>
Send(WB, HomeType, n, VC0, p.val, n);
p.state := PT_ME;
endrule;
endalias;
endruleset;
-- Message delivery rules
ruleset n:Node do
choose midx:Net[n] do
alias chan:Net[n] do
alias msg:chan[midx] do
alias box:InBox[n] do
-- Pick a random message in the network and delivier it
rule "receive-net"
(isundefined(box[msg.vc].mtype))
==>
if IsMember(n, Home)
then
HomeReceive(msg);
else
ProcReceive(msg, n);
endif;
if ! msg_processed
then
-- The node refused the message, stick it in the InBox to block the VC.
box[msg.vc] := msg;
endif;
MultiSetRemove(midx, chan);
endrule;
endalias
endalias;
endalias;
endchoose;
-- Try to deliver a message from a blocked VC; perhaps the node can handle it now
ruleset vc:VCType do
rule "receive-blocked-vc"
(! isundefined(InBox[n][vc].mtype))
==>
if IsMember(n, Home)
then
HomeReceive(InBox[n][vc]);
else
ProcReceive(InBox[n][vc], n);
endif;
if msg_processed
then
-- Message has been handled, forget it
undefine InBox[n][vc];
endif;
endrule;
endruleset;
endruleset;
----------------------------------------------------------------------
-- Startstate
----------------------------------------------------------------------
startstate
For v:Value do
-- home node initialization
HomeNode.state := H_Invalid;
undefine HomeNode.owner;
undefine HomeNode.newOwner;
undefine HomeNode.sharers;
undefine HomeNode.mshr;
HomeNode.val := v;
endfor;
LastWrite := HomeNode.val;
-- processor initialization
for i:Proc do
Procs[i].state := P_Invalid;
undefine Procs[i].val;
endfor;
-- network initialization
undefine Net;
endstartstate;
----------------------------------------------------------------------
-- Invariants
----------------------------------------------------------------------
invariant "Invalid implies empty owner"
HomeNode.state = H_Invalid
->
IsUndefined(HomeNode.owner);
-- invariant "value in memory matches value of last write, when invalid"
-- HomeNode.state = H_Invalid
-- ->
-- HomeNode.val = LastWrite;
-- invariant "values in valid state match last write"
-- Forall n : Proc Do
-- Procs[n].state = P_Valid
-- ->
-- Procs[n].val = LastWrite --LastWrite is updated whenever a new value is created
-- end;