-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoooexec.cpp
2397 lines (2007 loc) · 81.5 KB
/
oooexec.cpp
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
//
// PTLsim: Cycle Accurate x86-64 Simulator
// Out-of-Order Core Simulator
// Execution Pipeline Stages: Scheduling, Execution, Broadcast
//
// Copyright 2003-2008 Matt T. Yourst <yourst@yourst.com>
// Copyright 2006-2008 Hui Zeng <hzeng@cs.binghamton.edu>
//
#include <globals.h>
#include <elf.h>
#include <ptlsim.h>
#include <branchpred.h>
#include <logic.h>
#include <dcache.h>
#define INSIDE_OOOCORE
#include <ooocore.h>
#include <stats.h>
#ifndef ENABLE_CHECKS
#undef assert
#define assert(x) (x)
#endif
#ifndef ENABLE_LOGGING
#undef logable
#define logable(level) (0)
#endif
using namespace OutOfOrderModel;
//
// Issue Queue
//
template <int size, int operandcount>
void IssueQueue<size, operandcount>::reset(int coreid) {
this->coreid = coreid;
OutOfOrderCore& core = getcore();
count = 0;
valid = 0;
issued = 0;
allready = 0;
foreach (i, operandcount) {
tags[i].reset();
}
uopids.reset();
foreach (i, core.threadcount) {
ThreadContext* thread = core.threads[i];
if unlikely (!thread) continue;
thread->issueq_count = 0;
}
}
template <int size, int operandcount>
void IssueQueue<size, operandcount>::reset(int coreid, int threadid) {
OutOfOrderCore& core = getcore();
if unlikely (core.threadcount == 1) {
reset(coreid);
return;
}
#ifndef MULTI_IQ
this->coreid = coreid;
foreach (i, size) {
if likely (valid[i]) {
int slot_threadid = uopids[i] >> MAX_ROB_IDX_BIT;
if likely (slot_threadid == threadid) {
remove(i);
// Now i+1 is moved to position of where i used to be:
i--;
ThreadContext* thread = core.threads[threadid];
if (thread->issueq_count > core.reserved_iq_entries) {
issueq_operation_on_cluster(core, cluster, free_shared_entry());
}
thread->issueq_count--;
}
}
}
#endif
}
template <int size, int operandcount>
void IssueQueue<size, operandcount>::clock() {
allready = (valid & (~issued));
foreach (operand, operandcount) {
allready &= ~tags[operand].valid;
}
}
template <int size, int operandcount>
bool IssueQueue<size, operandcount>::insert(tag_t uopid, const tag_t* operands, const tag_t* preready) {
if unlikely (count == size)
return false;
assert(count < size);
int slot = count++;
assert(!bit(valid, slot));
uopids.insertslot(slot, uopid);
valid[slot] = 1;
issued[slot] = 0;
foreach (operand, operandcount) {
if likely (preready[operand])
tags[operand].invalidateslot(slot);
else tags[operand].insertslot(slot, operands[operand]);
}
return true;
}
template <int size, int operandcount>
void IssueQueue<size, operandcount>::tally_broadcast_matches(IssueQueue<size, operandcount>::tag_t sourceid, const bitvec<size>& mask, int operand) const {
if likely (!config.event_log_enabled) return;
OutOfOrderCore& core = getcore();
int threadid, rob_idx;
decode_tag(sourceid, threadid, rob_idx);
ThreadContext* thread = core.threads[threadid];
const ReorderBufferEntry* source = &thread->ROB[rob_idx];
bitvec<size> temp = mask;
while (*temp) {
int slot = temp.lsb();
int robid = uopof(slot);
int threadid_tmp, rob_idx_tmp;
decode_tag(robid, threadid_tmp, rob_idx_tmp);
assert(threadid_tmp == threadid);
assert(inrange(rob_idx_tmp, 0, ROB_SIZE-1));
const ReorderBufferEntry* target = &thread->ROB[rob_idx_tmp];
temp[slot] = 0;
OutOfOrderCoreEvent* event = core.eventlog.add(EVENT_FORWARD, source);
event->forwarding.operand = operand;
event->forwarding.forward_cycle = source->forward_cycle;
event->forwarding.target_uuid = target->uop.uuid;
event->forwarding.target_rob = target->index();
event->forwarding.target_physreg = target->physreg->index();
event->forwarding.target_rfid = target->physreg->rfid;
event->forwarding.target_cluster = target->cluster;
bool target_st = isstore(target->uop.opcode);
event->forwarding.target_st = target_st;
if (target_st) event->forwarding.target_lsq = target->lsq->index();
event->forwarding.target_operands_ready = 0;
foreach (i, MAX_OPERANDS) event->forwarding.target_operands_ready |= ((target->operands[i]->ready()) << i);
event->forwarding.target_all_operands_ready = target->ready_to_issue();
}
}
template <int size, int operandcount>
bool IssueQueue<size, operandcount>::broadcast(tag_t uopid) {
vec_t tagvec = assoc_t::prep(uopid);
foreach (operand, operandcount) {
bitvec<size> mask = tags[operand].invalidate(tagvec);
if unlikely (config.event_log_enabled) tally_broadcast_matches(uopid, mask, operand);
}
return true;
}
//
// Select one ready slot and move it to the issued state.
// This function returns the slot id. The returned slot
// id becomes invalid after the next call to remove()
// before the next uop can be processed in any way.
//
template <int size, int operandcount>
int IssueQueue<size, operandcount>::issue() {
if (!allready) return -1;
int slot = allready.lsb();
issued[slot] = 1;
return slot;
}
//
// Replay a uop that has already issued once.
// The caller may add or reset dependencies here as needed.
//
template <int size, int operandcount>
bool IssueQueue<size, operandcount>::replay(int slot, const tag_t* operands, const tag_t* preready) {
assert(valid[slot]);
assert(issued[slot]);
issued[slot] = 0;
foreach (operand, operandcount) {
if (preready[operand])
tags[operand].invalidateslot(slot);
else tags[operand].insertslot(slot, operands[operand]);
}
return true;
}
//
// Move a given slot to the end of the issue queue, so it will issue last.
// This is used in SMT to resolve deadlocks where the release part of an
// interlocked load or store cannot dispatch because some other interlocked
// uops are already blocking the issue queue. This guarantees one or the
// other will complete, avoiding the deadlock.
//
template <int size, int operandcount>
bool IssueQueue<size, operandcount>::switch_to_end(int slot, const tag_t* operands, const tag_t* preready) {
tag_t uopid = uopids[slot];
// remove
remove(slot);
// insert at end:
insert(uopid, operands, preready);
return true;
}
// NOTE: This is a fairly expensive operation:
template <int size, int operandcount>
bool IssueQueue<size, operandcount>::remove(int slot) {
uopids.collapse(slot);
foreach (i, operandcount) {
tags[i].collapse(slot);
}
valid = valid.remove(slot, 1);
issued = issued.remove(slot, 1);
allready = allready.remove(slot, 1);
count--;
assert(count >= 0);
return true;
}
template <int size, int operandcount>
ostream& IssueQueue<size, operandcount>::print(ostream& os) const {
os << "IssueQueue: count = ", count, ":", endl;
foreach (i, size) {
os << " uop ";
uopids.printid(os, i);
os << ": ",
((valid[i]) ? 'V' : '-'), ' ',
((issued[i]) ? 'I' : '-'), ' ',
((allready[i]) ? 'R' : '-'), ' ';
foreach (j, operandcount) {
if (j) os << ' ';
tags[j].printid(os, i);
}
os << endl;
}
return os;
}
// Instantiate all methods in the specific IssueQueue sizes we're using:
declare_issueq_templates;
//
// Issue a single ROB.
//
// Returns:
// +1 if issue was successful
// 0 if no functional unit was available
// -1 if there was an exception and we should stop issuing this cycle
//
int ReorderBufferEntry::issue() {
OutOfOrderCore& core = getcore();
ThreadContext& thread = getthread();
OutOfOrderCoreEvent* event = null;
W32 executable_on_fu = fuinfo[uop.opcode].fu & clusters[cluster].fu_mask & core.fu_avail;
// Are any FUs available in this cycle?
if unlikely (!executable_on_fu) {
if unlikely (config.event_log_enabled) {
event = core.eventlog.add(EVENT_ISSUE_NO_FU, this);
event->issue.fu_avail = core.fu_avail;
}
per_context_ooocore_stats_update(threadid, issue.result.no_fu++);
//
// When this (very rarely) happens, stop issuing uops to this cluster
// and try again with the problem uop on the next cycle. In practice
// this scenario rarely happens.
//
issueq_operation_on_cluster(core, cluster, replay(iqslot));
return ISSUE_NEEDS_REPLAY;
}
PhysicalRegister& ra = *operands[RA];
PhysicalRegister& rb = *operands[RB];
PhysicalRegister& rc = *operands[RC];
//
// Check if any other resources are missing that we didn't
// know about earlier, and replay like we did above if
// needed. This is our last chance to do so.
//
stats.summary.uops++;
per_context_ooocore_stats_update(threadid, issue.uops++);
fu = lsbindex(executable_on_fu);
clearbit(core.fu_avail, fu);
core.robs_on_fu[fu] = this;
cycles_left = fuinfo[uop.opcode].latency;
changestate(thread.rob_issued_list[cluster]);
IssueState state;
state.reg.rdflags = 0;
W64 radata = ra.data;
W64 rbdata = (uop.rb == REG_imm) ? uop.rbimm : rb.data;
W64 rcdata = (uop.rc == REG_imm) ? uop.rcimm : rc.data;
bool ld = isload(uop.opcode);
bool st = isstore(uop.opcode);
bool br = isbranch(uop.opcode);
assert(operands[RA]->ready());
assert(rb.ready());
if likely ((!st || (st && load_store_second_phase)) && (uop.rc != REG_imm)) assert(rc.ready());
if likely (!st) assert(operands[RS]->ready());
if likely (ra.nonnull()) {
ra.get_state_list().issue_source_counter++;
ra.all_consumers_sourced_from_bypass &= (ra.state == PHYSREG_BYPASS);
per_physregfile_stats_update(stats.ooocore.issue.source, ra.rfid, [ra.state]++);
}
if likely ((!uop.rbimm) & (rb.nonnull())) {
rb.get_state_list().issue_source_counter++;
rb.all_consumers_sourced_from_bypass &= (rb.state == PHYSREG_BYPASS);
per_physregfile_stats_update(stats.ooocore.issue.source, rb.rfid, [rb.state]++);
}
if unlikely ((!uop.rcimm) & (rc.nonnull())) {
rc.get_state_list().issue_source_counter++;
rc.all_consumers_sourced_from_bypass &= (rc.state == PHYSREG_BYPASS);
per_physregfile_stats_update(stats.ooocore.issue.source, rc.rfid, [rc.state]++);
}
bool propagated_exception = 0;
if unlikely ((ra.flags | rb.flags | rc.flags) & FLAG_INV) {
//
// Invalid data propagated through operands: mark output as
// invalid and don't even execute the uop at all.
//
state.st.invalid = 1;
state.reg.rdflags = FLAG_INV;
state.reg.rddata = EXCEPTION_Propagate;
propagated_exception = 1;
} else {
per_context_ooocore_stats_update(threadid, issue.opclass[opclassof(uop.opcode)]++);
if unlikely (ld|st) {
int completed = 0;
if likely (ld) {
completed = issueload(*lsq, origvirt, radata, rbdata, rcdata, pteupdate);
} else if unlikely (uop.opcode == OP_mf) {
completed = issuefence(*lsq);
} else {
completed = issuestore(*lsq, origvirt, radata, rbdata, rcdata, operands[2]->ready(), pteupdate);
}
if unlikely (completed == ISSUE_MISSPECULATED) {
per_context_ooocore_stats_update(threadid, issue.result.misspeculated++);
return ISSUE_MISSPECULATED;
} else if unlikely (completed == ISSUE_NEEDS_REFETCH) {
per_context_ooocore_stats_update(threadid, issue.result.refetch++);
return ISSUE_NEEDS_REFETCH;
}
state.reg.rddata = lsq->data;
state.reg.rdflags = (lsq->invalid << log2(FLAG_INV)) | ((!lsq->datavalid) << log2(FLAG_WAIT));
if unlikely (completed == ISSUE_NEEDS_REPLAY) {
per_context_ooocore_stats_update(threadid, issue.result.replay++);
return ISSUE_NEEDS_REPLAY;
}
} else if unlikely (uop.opcode == OP_ld_pre) {
issueprefetch(state, radata, rbdata, rcdata, uop.cachelevel);
} else {
if unlikely (br) {
state.brreg.riptaken = uop.riptaken;
state.brreg.ripseq = uop.ripseq;
}
uop.synthop(state, radata, rbdata, rcdata, ra.flags, rb.flags, rc.flags);
}
}
physreg->flags = state.reg.rdflags;
physreg->data = state.reg.rddata;
if unlikely (!physreg->valid()) {
//
// If the uop caused an exception, force it directly to the commit
// state and not through writeback (this keeps dependencies waiting until
// they can be properly annulled by the speculation logic.) The commit
// stage will detect the exception and take appropriate action.
//
// If the exceptional uop was speculatively executed beyond a
// branch, it will never reach commit anyway since the branch would
// have to commit before the exception was ever seen.
//
cycles_left = 0;
changestate(thread.rob_ready_to_commit_queue);
//
// NOTE: The frontend should not necessarily be stalled on exceptions
// when extensive speculation is in use, since re-dispatch can be used
// without refetching to resolve these situations.
//
// stall_frontend = true;
}
//
// Memory fences proceed directly to commit. Once they hit
// the head of the ROB, they get recirculated back to the
// rob_completed_list state so dependent uops can wake up.
//
if unlikely (uop.opcode == OP_mf) {
cycles_left = 0;
changestate(thread.rob_ready_to_commit_queue);
}
bool mispredicted = (physreg->data != uop.riptaken);
if unlikely (config.event_log_enabled && (propagated_exception | (!(ld|st)))) {
event = core.eventlog.add(EVENT_ISSUE_OK, this);
event->issue.state = state;
event->issue.cycles_left = cycles_left;
event->issue.operand_data[0] = radata;
event->issue.operand_data[1] = rbdata;
event->issue.operand_data[2] = rcdata;
event->issue.operand_flags[0] = ra.flags;
event->issue.operand_flags[1] = rb.flags;
event->issue.operand_flags[2] = rc.flags;
event->issue.mispredicted = br & mispredicted;
event->issue.predrip = uop.riptaken;
}
//
// Release the issue queue entry, since we are beyond the point of no return:
// the uop cannot possibly be replayed at this point, but may still be annulled
// or re-dispatched in case of speculation failures.
//
release();
this->issued = 1;
if likely (physreg->valid()) {
if unlikely (br) {
int bptype = uop.predinfo.bptype;
bool cond = bit(bptype, log2(BRANCH_HINT_COND));
bool indir = bit(bptype, log2(BRANCH_HINT_INDIRECT));
bool ret = bit(bptype, log2(BRANCH_HINT_RET));
if unlikely (mispredicted) {
per_context_ooocore_stats_update(threadid, branchpred.cond[MISPRED] += cond);
per_context_ooocore_stats_update(threadid, branchpred.indir[MISPRED] += (indir & !ret));
per_context_ooocore_stats_update(threadid, branchpred.ret[MISPRED] += ret);
per_context_ooocore_stats_update(threadid, branchpred.summary[MISPRED]++);
W64 realrip = physreg->data;
//
// Correct the branch directions and cond code field.
// This is required since the branch may again be
// re-dispatched if we mis-identified a mispredict
// due to very deep speculation.
//
// Basically the riptaken field must always point
// to the correct next instruction in the ROB after
// the branch.
//
if likely (isclass(uop.opcode, OPCLASS_COND_BRANCH)) {
assert(realrip == uop.ripseq);
uop.cond = invert_cond(uop.cond);
//
// We need to be careful here: we already looked up the synthop for this
// uop according to the old condition, so redo that here so we call the
// correct code for the swapped condition.
//
uop.synthop = get_synthcode_for_cond_branch(uop.opcode, uop.cond, uop.size, 0);
swap(uop.riptaken, uop.ripseq);
} else if unlikely (isclass(uop.opcode, OPCLASS_INDIR_BRANCH)) {
uop.riptaken = realrip;
uop.ripseq = realrip;
} else if unlikely (isclass(uop.opcode, OPCLASS_UNCOND_BRANCH)) { // unconditional branches need no special handling
assert(realrip == uop.riptaken);
}
//
// Early misprediction handling. Annul everything after the
// branch and restart fetching in the correct direction
//
thread.annul_fetchq();
annul_after();
//
// The fetch queue is reset and fetching is redirected to the
// correct branch direction.
//
// Note that we do NOT just reissue the branch - this would be
// pointless as we already know the correct direction since
// it has already been issued once. Just let it writeback and
// commit like it was predicted perfectly in the first place.
//
thread.reset_fetch_unit(realrip);
per_context_ooocore_stats_update(threadid, issue.result.branch_mispredict++);
return -1;
} else {
per_context_ooocore_stats_update(threadid, branchpred.cond[CORRECT] += cond);
per_context_ooocore_stats_update(threadid, branchpred.indir[CORRECT] += (indir & !ret));
per_context_ooocore_stats_update(threadid, branchpred.ret[CORRECT] += ret);
per_context_ooocore_stats_update(threadid, branchpred.summary[CORRECT]++);
per_context_ooocore_stats_update(threadid, issue.result.complete++);
}
} else {
per_context_ooocore_stats_update(threadid, issue.result.complete++);
}
} else {
per_context_ooocore_stats_update(threadid, issue.result.exception++);
}
return ISSUE_COMPLETED;
}
//
// Address generation common to both loads and stores
//
Waddr ReorderBufferEntry::addrgen(LoadStoreQueueEntry& state, Waddr& origaddr, Waddr& virtpage, W64 ra, W64 rb, W64 rc, PTEUpdate& pteupdate, Waddr& addr, int& exception, PageFaultErrorCode& pfec, bool& annul) {
Context& ctx = getthread().ctx;
bool st = isstore(uop.opcode);
int sizeshift = uop.size;
int aligntype = uop.cond;
bool internal = uop.internal;
bool signext = (uop.opcode == OP_ldx);
bool a16 = (uop.opcode == OP_ld_a16 || uop.opcode == OP_st_a16);
addr = (st) ? (ra + rb) : ((aligntype == LDST_ALIGN_NORMAL) ? (ra + rb) : ra);
//
// x86-64 requires virtual addresses to be canonical: if bit 47 is set,
// all upper 16 bits must be set. If this is not true, we need to signal
// a general protection fault.
//
if (addr != (W64)signext64(addr, 48) || (a16 && (addr & 0xf))) {
exception = EXCEPTION_InvalidAddr;
return 0;
}
addr = (W64)signext64(addr, 48);
addr &= ctx.virt_addr_mask;
origaddr = addr;
annul = 0;
uop.ld_st_truly_unaligned = (lowbits(origaddr, sizeshift) != 0);
switch (aligntype) {
case LDST_ALIGN_NORMAL:
break;
case LDST_ALIGN_LO:
addr = floor(addr, 8); break;
case LDST_ALIGN_HI:
//
// Is the high load ever even used? If not, don't check for exceptions;
// otherwise we may erroneously flag page boundary conditions as invalid
//
addr = floor(addr, 8);
annul = (floor(origaddr + ((1<<sizeshift)-1), 8) == addr);
addr += 8;
break;
}
state.physaddr = addr >> 3;
state.invalid = 0;
virtpage = addr;
//
// Notice that datavalid is not set until both the rc operand to
// store is ready AND any inherited SFR data is ready to merge.
//
state.addrvalid = 1;
state.datavalid = 0;
//
// Special case: if no part of the actual user load/store falls inside
// of the high 64 bits, do not perform the access and do not signal
// any exceptions if that page was invalid.
//
// However, we must be extremely careful if we're inheriting an SFR
// from an earlier store: the earlier store may have updated some
// bytes in the high 64-bit chunk even though we're not updating
// any bytes. In this case we still must do the write since it
// could very well be the final commit to that address. In any
// case, the SFR mismatch and LSAT must still be checked.
//
// The store commit code checks if the bytemask is zero and does
// not attempt the actual store if so. This will always be correct
// for high stores as described in this scenario.
//
exception = 0;
Waddr physaddr = (annul) ? INVALID_PHYSADDR : ctx.check_and_translate(addr, uop.size, st, uop.internal, exception, pfec, pteupdate);
return physaddr;
}
bool ReorderBufferEntry::handle_common_load_store_exceptions(LoadStoreQueueEntry& state, Waddr& origaddr, Waddr& addr, int& exception, PageFaultErrorCode& pfec) {
OutOfOrderCore& core = getcore();
ThreadContext& thread = getthread();
bool st = isstore(uop.opcode);
int aligntype = uop.cond;
state.invalid = 1;
state.data = exception | ((W64)pfec << 32);
state.datavalid = 1;
if unlikely (config.event_log_enabled) core.eventlog.add_load_store((st) ? EVENT_STORE_EXCEPTION : EVENT_LOAD_EXCEPTION, this, null, addr);
if unlikely (exception == EXCEPTION_UnalignedAccess) {
//
// If we have an unaligned access, locate the excepting uop in the
// basic block cache through the uop.origop pointer. Directly set
// the unaligned bit in the uop, and restart fetching at the start
// of the x86 macro-op. The frontend will then split the uop into
// low and high parts as it is refetched.
//
if unlikely (config.event_log_enabled) core.eventlog.add_load_store(EVENT_ALIGNMENT_FIXUP, this, null, addr);
core.set_unaligned_hint(uop.rip, 1);
thread.annul_fetchq();
W64 recoveryrip = annul_after_and_including();
thread.reset_fetch_unit(recoveryrip);
if unlikely (st) {
per_context_ooocore_stats_update(threadid, dcache.store.issue.unaligned++);
} else {
per_context_ooocore_stats_update(threadid, dcache.load.issue.unaligned++);
}
return false;
}
if unlikely (((exception == EXCEPTION_PageFaultOnRead) | (exception == EXCEPTION_PageFaultOnWrite)) & (aligntype == LDST_ALIGN_HI)) {
//
// If we have a page fault on an unaligned access, and this is the high
// half (ld.hi / st.hi) of that access, the page fault address recorded
// in CR2 must be at the very first byte of the second page the access
// overlapped onto (otherwise the kernel will repeatedly fault in the
// first page, even though that one is already present.
//
origaddr = addr;
}
if unlikely (st) {
per_context_ooocore_stats_update(threadid, dcache.store.issue.exception++);
} else {
per_context_ooocore_stats_update(threadid, dcache.load.issue.exception++);
}
return true;
}
namespace OutOfOrderModel {
// One global interlock buffer for all VCPUs:
MemoryInterlockBuffer interlocks;
};
//
// Release the lock on the cache block touched by this ld.acq uop.
//
// The lock is not actually released until flush_mem_lock_release_list()
// is called, for instance after the entire macro-op has committed.
//
bool ReorderBufferEntry::release_mem_lock(bool forced) {
if likely (!lock_acquired) return false;
W64 physaddr = lsq->physaddr << 3;
MemoryInterlockEntry* lock = interlocks.probe(physaddr);
assert(lock);
OutOfOrderCore& core = getcore();
ThreadContext& thread = getthread();
if unlikely (config.event_log_enabled) {
OutOfOrderCoreEvent* event = core.eventlog.add_load_store((forced) ? EVENT_STORE_LOCK_ANNULLED : EVENT_STORE_LOCK_RELEASED, this, null, physaddr);
event->loadstore.locking_vcpuid = lock->vcpuid;
event->loadstore.locking_uuid = lock->uuid;
event->loadstore.locking_rob = lock->rob;
}
assert(lock->vcpuid == thread.ctx.vcpuid);
assert(lock->uuid == uop.uuid);
assert(lock->rob == index());
//
// Just add to the release list; do not invalidate until the macro-op commits
//
assert(thread.queued_mem_lock_release_count < lengthof(thread.queued_mem_lock_release_list));
thread.queued_mem_lock_release_list[thread.queued_mem_lock_release_count++] = physaddr;
lock_acquired = 0;
return true;
}
//
// Stores have special dependency rules: they may issue as soon as operands ra and rb are ready,
// even if rc (the value to store) or rs (the store buffer to inherit from) is not yet ready or
// even known.
//
// After both ra and rb are ready, the store is moved to [ready_to_issue] as a first phase store.
// When the store issues, it generates its physical address [ra+rb] and establishes an SFR with
// the address marked valid but the data marked invalid.
//
// The sole purpose of doing this is to allow other loads and stores to create an rs dependency
// on the SFR output of the store.
//
// The store is then marked as a second phase store, since the address has been generated.
// When the store is replayed and rescheduled, it must now have all operands ready this time.
//
int ReorderBufferEntry::issuestore(LoadStoreQueueEntry& state, Waddr& origaddr, W64 ra, W64 rb, W64 rc, bool rcready, PTEUpdate& pteupdate) {
ThreadContext& thread = getthread();
Queue<LoadStoreQueueEntry, LSQ_SIZE>& LSQ = thread.LSQ;
Queue<ReorderBufferEntry, ROB_SIZE>& ROB = thread.ROB;
LoadStoreAliasPredictor& lsap = thread.lsap;
OutOfOrderCore& core = getcore();
OutOfOrderCoreEvent* event;
int sizeshift = uop.size;
int aligntype = uop.cond;
Waddr addr;
int exception = 0;
PageFaultErrorCode pfec;
bool annul;
Waddr physaddr = addrgen(state, origaddr, virtpage, ra, rb, rc, pteupdate, addr, exception, pfec, annul);
if unlikely (exception) {
return (handle_common_load_store_exceptions(state, origaddr, addr, exception, pfec)) ? ISSUE_COMPLETED : ISSUE_MISSPECULATED;
}
per_context_ooocore_stats_update(threadid, dcache.store.type.aligned += ((!uop.internal) & (aligntype == LDST_ALIGN_NORMAL)));
per_context_ooocore_stats_update(threadid, dcache.store.type.unaligned += ((!uop.internal) & (aligntype != LDST_ALIGN_NORMAL)));
per_context_ooocore_stats_update(threadid, dcache.store.type.internal += uop.internal);
per_context_ooocore_stats_update(threadid, dcache.store.size[sizeshift]++);
state.physaddr = (annul) ? INVALID_PHYSADDR : (physaddr >> 3);
state.smc_mfn = addr >> 12; /* magic values */
//
// The STQ is then searched for the most recent prior store S to same 64-bit block. If found, U's
// rs dependency is set to S by setting the ROB's rs field to point to the prior store's physreg
// and hence its ROB. If not found, U's rs dependency remains unset (i.e. to PHYS_REG_NULL).
// If some prior stores are ambiguous (addresses not resolved yet), we assume they are a match
// to ensure correctness yet avoid additional checks; the store is replayed and tries again
// when the ambiguous reference resolves.
//
// We also find the first store memory fence (mf.sfence uop) in the LSQ, and
// if one exists, make this store dependent on the fence via its rs operand.
// The mf uop issues immediately but does not complete until it's at the head
// of the ROB and LSQ; only at that point can future loads and stores issue.
//
// All memory fences are considered stores, since in this way both loads and
// stores can depend on them using the rs dependency.
//
LoadStoreQueueEntry* sfra = null;
foreach_backward_before(LSQ, lsq, i) {
LoadStoreQueueEntry& stbuf = LSQ[i];
// Skip over loads (we only care about the store queue subset):
if likely (!stbuf.store) continue;
if likely (stbuf.addrvalid) {
// Only considered a match if it's not a fence (which doesn't match anything)
if unlikely (stbuf.lfence | stbuf.sfence) continue;
if (stbuf.physaddr == state.physaddr) {
per_context_ooocore_stats_update(threadid, dcache.load.dependency.stq_address_match++);
sfra = &stbuf;
break;
}
} else {
//
// Address is unknown: stores to a given word must issue in program order
// to composite data correctly, but we can't do that without the address.
//
// This also catches any unresolved store fences (but not load fences).
//
if unlikely (stbuf.lfence & !stbuf.sfence) {
// Stores can always pass load fences
continue;
}
sfra = &stbuf;
break;
}
}
if (sfra && sfra->addrvalid && sfra->datavalid) {
assert(sfra->physaddr == state.physaddr);
assert(sfra->rob->uop.uuid < uop.uuid);
}
//
// Always update deps in case redispatch is required
// because of a future speculation failure: we must
// know which loads and stores inherited bogus values
//
operands[RS]->unref(*this, thread.threadid);
operands[RS] = (sfra) ? sfra->rob->physreg : &core.physregfiles[0][PHYS_REG_NULL];
operands[RS]->addref(*this, thread.threadid);
bool ready = (!sfra || (sfra && sfra->addrvalid && sfra->datavalid)) && rcready;
//
// If any of the following are true:
// - Prior store S with same address is found but its data is not ready
// - Prior store S with unknown address is found
// - Data to store (rc operand) is not yet ready
//
// Then the store is moved back into [ready_to_dispatch], where this time all operands are checked.
// The replay() function will put the newly selected prior store S's ROB as the rs dependency
// of the current store before replaying it.
//
// When the current store wakes up again, it will rescan the STQ to see if any intervening stores
// slipped in, and may repeatedly go back to sleep on the new store until the entire chain of stores
// to a given location is resolved in the correct order. This does not mean all stores must issue in
// program order - it simply means stores to the same address (8-byte chunk) are serialized in
// program order, but out of order w.r.t. unrelated stores. This is similar to the constraints on
// store buffer merging in Pentium 4 and AMD K8.
//
if unlikely (!ready) {
if unlikely (config.event_log_enabled) {
event = core.eventlog.add_load_store(EVENT_STORE_WAIT, this, sfra, addr);
event->loadstore.rcready = rcready;
}
replay();
load_store_second_phase = 1;
if unlikely (sfra && sfra->sfence) {
per_context_ooocore_stats_update(threadid, dcache.store.issue.replay.fence++);
} else {
per_context_ooocore_stats_update(threadid, dcache.store.issue.replay.sfr_addr_and_data_and_data_to_store_not_ready += ((!rcready) & (sfra && (!sfra->addrvalid) & (!sfra->datavalid))));
per_context_ooocore_stats_update(threadid, dcache.store.issue.replay.sfr_addr_and_data_to_store_not_ready += ((!rcready) & (sfra && (!sfra->addrvalid))));
per_context_ooocore_stats_update(threadid, dcache.store.issue.replay.sfr_data_and_data_to_store_not_ready += ((!rcready) & (sfra && sfra->addrvalid && (!sfra->datavalid))));
per_context_ooocore_stats_update(threadid, dcache.store.issue.replay.sfr_addr_and_data_not_ready += (rcready & (sfra && (!sfra->addrvalid) & (!sfra->datavalid))));
per_context_ooocore_stats_update(threadid, dcache.store.issue.replay.sfr_addr_not_ready += (rcready & (sfra && ((!sfra->addrvalid) & (sfra->datavalid)))));
per_context_ooocore_stats_update(threadid, dcache.store.issue.replay.sfr_data_not_ready += (rcready & (sfra && (sfra->addrvalid & (!sfra->datavalid)))));
}
return ISSUE_NEEDS_REPLAY;
}
//
// Load/Store Aliasing Prevention
//
// We always issue loads as soon as possible even if some entries in the
// store queue have unresolved addresses. If a load gets erroneously
// issued before an earlier store in program order to the same address,
// this is considered load/store aliasing.
//
// Aliasing is detected when stores issue: the load queue is scanned
// for earlier loads in program order which collide with the store's
// address. In this case all uops in program order after and including
// the store (and by extension, the colliding load) must be annulled.
//
// To keep this from happening repeatedly, whenever a collision is
// detected, the store looks up the rip of the colliding load and adds
// it to a small table called the LSAP (load/store alias predictor).
//
// Loads query the LSAP with the rip of the load; if a matching entry
// is found in the LSAP and the store address is unresolved, the load
// is not allowed to proceed.
//
// Check all later loads in LDQ to see if any have already issued
// and have already obtained their data but really should have
// depended on the data generated by this store. If so, mark the
// store as invalid (EXCEPTION_LoadStoreAliasing) so it annuls
// itself and the load after it in program order at commit time.
//
foreach_forward_after (LSQ, lsq, i) {
LoadStoreQueueEntry& ldbuf = LSQ[i];
//
// (see notes on Load Replay Conditions below)
//
if unlikely ((!ldbuf.store) & ldbuf.addrvalid & ldbuf.rob->issued & (ldbuf.physaddr == state.physaddr)) {
//
// Check for the extremely rare case where:
// - load is in the ready_to_load state at the start of the simulated
// cycle, and is processed by load_issue()
// - that load gets its data forwarded from a store (i.e., the store
// being handled here) scheduled for execution in the same cycle
// - the load and the store alias each other
//
// Handle this by checking the list of addresses for loads processed
// in the same cycle, and only signal a load speculation failure if
// the aliased load truly came at least one cycle before the store.
//
int i;
int parallel_forwarding_match = 0;
foreach (i, thread.loads_in_this_cycle) {
bool match = (thread.load_to_store_parallel_forwarding_buffer[i] == state.physaddr);
parallel_forwarding_match |= match;
}
if unlikely (parallel_forwarding_match) {
if unlikely (config.event_log_enabled) event = core.eventlog.add_load_store(EVENT_STORE_PARALLEL_FORWARDING_MATCH, this, &ldbuf, addr);
per_context_ooocore_stats_update(threadid, dcache.store.issue.replay.parallel_aliasing++);
replay();
return ISSUE_NEEDS_REPLAY;
}
state.invalid = 1;
state.data = EXCEPTION_LoadStoreAliasing;
state.datavalid = 1;
if unlikely (config.event_log_enabled) event = core.eventlog.add_load_store(EVENT_STORE_ALIASED_LOAD, this, &ldbuf, addr);
// Add the rip to the load to the load/store alias predictor:
lsap.select(ldbuf.rob->uop.rip);
//
// The load as dependent on this store. Add a new dependency
// on the store to the load so the normal redispatch mechanism
// will find this.
//
ldbuf.rob->operands[RS]->unref(*this, thread.threadid);
ldbuf.rob->operands[RS] = physreg;
ldbuf.rob->operands[RS]->addref(*this, thread.threadid);
redispatch_dependents();
per_context_ooocore_stats_update(threadid, dcache.store.issue.ordering++);
return ISSUE_MISSPECULATED;
}
}
//
// Cache coherent interlocking
//
if unlikely ((contextcount > 1) && (!annul)) {
// assert(0);
W64 physaddr = state.physaddr << 3;
MemoryInterlockEntry* lock = interlocks.probe(physaddr);
//
// All store instructions check if a lock is held by another thread,
// even if the load lacks the LOCK prefix.
//
// This prevents mixing of threads in cases where e.g. thread 0 is
// acquiring a spinlock by a LOCK DEC of the low 4 bytes of the 8-byte
// cache chunk, while thread 1 is releasing a spinlock using an
// *unlocked* ADD [mem],1 on the high 4 bytes of the chunk.
//
if unlikely (lock && (lock->vcpuid != thread.ctx.vcpuid)) {
//
// Non-interlocked store intersected with a previously
// locked block. We must replay the store until the block
// becomes unlocked.
//
if unlikely (config.event_log_enabled) {
event = core.eventlog.add_load_store(EVENT_STORE_LOCK_REPLAY, this, null, addr);
event->loadstore.locking_vcpuid = lock->vcpuid;
event->loadstore.locking_uuid = lock->uuid;
event->loadstore.threadid = lock->threadid;
}
per_context_ooocore_stats_update(threadid, dcache.store.issue.replay.interlocked++);
replay_locked();
return ISSUE_NEEDS_REPLAY;
}
//
// st.rel unlocks the chunk ONLY at commit time. This is required since the