-
Notifications
You must be signed in to change notification settings - Fork 11
/
ooopipe.cpp
2093 lines (1782 loc) · 73.4 KB
/
ooopipe.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
// Core Pipeline Stages: Frontend, Writeback, Commit
//
// Copyright 2003-2008 Matt T. Yourst <[email protected]>
// Copyright 2006-2008 Hui Zeng <[email protected]>
//
#include <globals.h>
#include <elf.h>
#include <ptlsim.h>
#include <branchpred.h>
#include <datastore.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;
void OutOfOrderCoreCacheCallbacks::icache_wakeup(LoadStoreInfo lsi, W64 physaddr) {
foreach (i, core.threadcount) {
ThreadContext* thread = core.threads[i];
if unlikely (thread && thread->waiting_for_icache_fill && (floor(thread->waiting_for_icache_fill_physaddr, CacheSubsystem::L1_LINE_SIZE) == physaddr)) {
if (logable(6)) logfile << "[vcpu ", thread->ctx.vcpuid, "] i-cache wakeup of physaddr ", (void*)(Waddr)physaddr, endl;
thread->waiting_for_icache_fill = 0;
thread->waiting_for_icache_fill_physaddr = 0;
}
}
}
//
// Determine which physical register files can be written
// by a given type of uop.
//
// This must be customized if the physical register files
// are altered in smthwdef.h.
//
static W32 phys_reg_files_writable_by_uop(const TransOp& uop) {
W32 c = opinfo[uop.opcode].opclass;
#ifdef UNIFIED_INT_FP_PHYS_REG_FILE
return
(c & OPCLASS_STORE) ? OutOfOrderCore::PHYS_REG_FILE_MASK_ST :
(c & OPCLASS_BRANCH) ? OutOfOrderCore::PHYS_REG_FILE_MASK_BR :
OutOfOrderCore::PHYS_REG_FILE_MASK_INT;
#else
return
(c & OPCLASS_STORE) ? OutOfOrderCore::PHYS_REG_FILE_MASK_ST :
(c & OPCLASS_BRANCH) ? OutOfOrderCore::PHYS_REG_FILE_MASK_BR :
(c & (OPCLASS_LOAD | OPCLASS_PREFETCH)) ? ((uop.datatype == DATATYPE_INT) ? OutOfOrderCore::PHYS_REG_FILE_MASK_INT : OutOfOrderCore::PHYS_REG_FILE_MASK_FP) :
((c & OPCLASS_FP) | inrange((int)uop.rd, REG_xmml0, REG_xmmh15) | inrange((int)uop.rd, REG_fptos, REG_ctx)) ? OutOfOrderCore::PHYS_REG_FILE_MASK_FP :
OutOfOrderCore::PHYS_REG_FILE_MASK_INT;
#endif
}
void ThreadContext::annul_fetchq() {
//
// There may be return address stack (RAS) updates from calls and returns
// in the fetch queue that never made it to renaming, so they have no ROB
// that the core can annul normally. Therefore, we must go backwards in
// the fetch queue to annul these updates, in addition to checking the ROB.
//
foreach_backward (fetchq, i) {
FetchBufferEntry& fetchbuf = fetchq[i];
if unlikely (isbranch(fetchbuf.opcode) && (fetchbuf.predinfo.bptype & (BRANCH_HINT_CALL|BRANCH_HINT_RET))) {
if unlikely (config.event_log_enabled) core.eventlog.add(EVENT_ANNUL_FETCHQ_RAS, fetchbuf);
branchpred.annulras(fetchbuf.predinfo);
}
}
}
//
// Flush entire pipeline immediately, reset all processor
// structures to their initial state, and resume from the
// state saved in ctx.commitarf.
//
void OutOfOrderCore::flush_pipeline_all() {
// Clear per-thread state:
foreach (i, threadcount) {
ThreadContext* thread = threads[i];
thread->flush_pipeline();
}
// Clear out everything global:
setzero(robs_on_fu);
}
void ThreadContext::flush_pipeline() {
// SD: I wonder if flush_pipeline should really be able to flush halfway
// through a partially committed x86 instruction. This is dangerous,
// especially if the instruction has already partially updated
// architectural state.
if unlikely (logable(1) && rob_ready_to_commit_queue.count &&
!ROB.peekhead()->uop.som) {
logfile << "[vcpu ", ctx.vcpuid, "] thread ", threadid, ": Flushing through a "
"partially committed x86 instruction, this is likely BAD:",endl;
foreach_forward(ROB, i) {
ReorderBufferEntry& rob = ROB[i];
logfile <<" ", rob, endl;
if (rob.uop.eom) break;
}
}
core.caches.complete(threadid);
annul_fetchq();
foreach_forward(ROB, i) {
ReorderBufferEntry& rob = ROB[i];
rob.release_mem_lock(true);
//
// Note that we might actually flush halfway through a locked RMW
// instruction, but this is not as bad as in the annul case, as the
// store (the W-part of the RMW) will be wiped too.
//
flush_mem_lock_release_list();
rob.physreg->reset(threadid); // free all register allocated by rob
if unlikely (config.event_log_enabled)
core.eventlog.add(EVENT_ANNUL_FLUSH, &rob);
}
// free all register in arch state:
foreach (i, PHYS_REG_FILE_COUNT){
StateList& list = core.physregfiles[i].states[PHYSREG_ARCH];
PhysicalRegister* obj;
int n = 0;
foreach_list_mutable( list, obj, entry, nextentry) {
n++;
obj->reset(threadid);
}
}
// free all register in arch state:
foreach (i, PHYS_REG_FILE_COUNT){
StateList& list = core.physregfiles[i].states[PHYSREG_PENDINGFREE];
PhysicalRegister* obj;
int n = 0;
foreach_list_mutable( list, obj, entry, nextentry) {
n++;
obj->reset(threadid);
}
}
reset_fetch_unit(ctx.commitarf[REG_rip]);
rob_states.reset();
ROB.reset();
foreach (i, ROB_SIZE) {
ROB[i].coreid = core.coreid;
ROB[i].threadid = threadid;
ROB[i].changestate(rob_free_list);
}
LSQ.reset();
foreach (i, LSQ_SIZE) {
LSQ[i].coreid = core.coreid;
}
loads_in_flight = 0;
stores_in_flight = 0;
foreach_issueq(reset(core.coreid, threadid));
dispatch_deadlock_countdown = DISPATCH_DEADLOCK_COUNTDOWN_CYCLES;
last_commit_at_cycle = sim_cycle;
external_to_core_state();
}
//
// Respond to a branch mispredict or other redirection:
//
void ThreadContext::reset_fetch_unit(W64 realrip) {
if (current_basic_block) {
// Release our lock on the cached basic block we're currently fetching
current_basic_block->release();
current_basic_block = null;
}
fetchrip = realrip;
fetchrip.update(ctx);
stall_frontend = 0;
waiting_for_icache_fill = 0;
fetchq.reset();
current_basic_block_transop_index = 0;
unaligned_ldst_buf.reset();
}
//
// Process any pending self-modifying code invalidate requests.
// This must be called on all cores *after* flushing all pipelines,
// to ensure no stale BBs are referenced, thus preventing them
// from being freed.
//
void ThreadContext::invalidate_smc() {
if unlikely (smc_invalidate_pending) {
if (logable(5)) logfile << "SMC invalidate pending on ", smc_invalidate_rvp, endl;
bbcache.invalidate_page(smc_invalidate_rvp.mfnlo, INVALIDATE_REASON_SMC);
if unlikely (smc_invalidate_rvp.mfnlo != smc_invalidate_rvp.mfnhi) bbcache.invalidate_page(smc_invalidate_rvp.mfnhi, INVALIDATE_REASON_SMC);
smc_invalidate_pending = 0;
}
}
//
// Copy external archregs to physregs and reset all rename tables
//
void ThreadContext::external_to_core_state() {
foreach (i, PHYS_REG_FILE_COUNT) {
PhysicalRegisterFile& rf = core.physregfiles[i];
PhysicalRegister* zeroreg = rf.alloc(threadid, PHYS_REG_NULL);
zeroreg->addspecref(0, threadid);
zeroreg->commit();
zeroreg->data = 0;
zeroreg->flags = 0;
zeroreg->archreg = REG_zero;
}
// Always start out on cluster 0:
PhysicalRegister* zeroreg = &core.physregfiles[0][PHYS_REG_NULL];
//
// Allocate and commit each architectural register
//
foreach (i, ARCHREG_COUNT) {
//
// IMPORTANT! If using some register file configuration other
// than (integer, fp), this needs to be changed!
//
#ifdef UNIFIED_INT_FP_PHYS_REG_FILE
int rfid = (i == REG_rip) ? PHYS_REG_FILE_BR : PHYS_REG_FILE_INT;
#else
bool fp = inrange((int)i, REG_xmml0, REG_xmmh15) | (inrange((int)i, REG_fptos, REG_ctx));
int rfid = (fp) ? core.PHYS_REG_FILE_FP : (i == REG_rip) ? core.PHYS_REG_FILE_BR : core.PHYS_REG_FILE_INT;
#endif
PhysicalRegisterFile& rf = core.physregfiles[rfid];
PhysicalRegister* physreg = (i == REG_zero) ? zeroreg : rf.alloc(threadid);
assert(physreg); /// need increase rf size if failed.
physreg->archreg = i;
physreg->data = ctx.commitarf[i];
physreg->flags = 0;
commitrrt[i] = physreg;
}
commitrrt[REG_flags]->flags = (W16)commitrrt[REG_flags]->data;
//
// Internal translation registers are never used before
// they are written for the first time:
//
for (int i = ARCHREG_COUNT; i < TRANSREG_COUNT; i++) {
commitrrt[i] = zeroreg;
}
//
// Set renamable flags
//
commitrrt[REG_zf] = commitrrt[REG_flags];
commitrrt[REG_cf] = commitrrt[REG_flags];
commitrrt[REG_of] = commitrrt[REG_flags];
//
// Copy commitrrt to specrrt and update refcounts
//
foreach (i, TRANSREG_COUNT) {
commitrrt[i]->commit();
specrrt[i] = commitrrt[i];
specrrt[i]->addspecref(i, threadid);
commitrrt[i]->addcommitref(i, threadid);
}
#ifdef ENABLE_TRANSIENT_VALUE_TRACKING
specrrt.renamed_in_this_basic_block.reset();
commitrrt.renamed_in_this_basic_block.reset();
#endif
}
//
// Re-dispatch all uops in the ROB that have not yet generated
// a result or are otherwise stalled.
//
void ThreadContext::redispatch_deadlock_recovery() {
if (logable(6)) core.dump_smt_state(logfile);
per_context_ooocore_stats_update(threadid, dispatch.redispatch.deadlock_flushes++);
// don't want to reset the counter for no commit in this case
W64 previous_last_commit_at_cycle = last_commit_at_cycle;
flush_pipeline();
last_commit_at_cycle = previous_last_commit_at_cycle; /// so we can exit after no commit after deadlock recovery a few times in a roll
logfile << "[vcpu ", ctx.vcpuid, "] thread ", threadid, ": reset thread.last_commit_at_cycle to be before redispatch_deadlock_recovery() ", previous_last_commit_at_cycle, endl;
/*
//
// This is a more selective scheme than the full pipeline flush.
// Presently it does not work correctly with some combinations
// of user-modifiable parameters, so it's disabled to ensure
// deadlock-free operation in every configuration.
//
ReorderBufferEntry* prevrob = null;
bitvec<MAX_OPERANDS> noops = 0;
foreach_forward(ROB, robidx) {
ReorderBufferEntry& rob = ROB[robidx];
//
// Only re-dispatch those uops that have not yet generated a value
// or are guaranteed to produce a value soon without tying up resources.
// This must occur in program order to avoid deadlock!
//
// bool recovery_required = (rob.current_state_list->flags & ROB_STATE_IN_ISSUE_QUEUE) || (rob.current_state_list == &rob_ready_to_dispatch_list);
bool recovery_required = 1; // for now, just to be safe
if (recovery_required) {
rob.redispatch(noops, prevrob);
prevrob = &rob;
per_context_ooocore_stats_update(threadid, dispatch.redispatch.deadlock_uops_flushed++);
}
}
if (logable(6)) dump_smt_state();
*/
}
//
// Fetch Stage
//
// Fetch a stream of x86 instructions from the L1 i-cache along predicted
// branch paths.
//
// Internally, up to N uops per clock corresponding to instructions in
// the current basic block are fetched per cycle and placed in the uopq
// as TransOps. When we run out of uops in one basic block, we proceed
// to lookup or translate the next basic block.
//
//
// Used to debug crashes when cycle to start logging can't be determined:
//
static RIPVirtPhys fetch_bb_address_ringbuf[256];
static W64 fetch_bb_address_ringbuf_head = 0;
static void print_fetch_bb_address_ringbuf(ostream& os) {
os << "Head: ", fetch_bb_address_ringbuf_head, endl;
foreach (i, lengthof(fetch_bb_address_ringbuf)) {
int j = (fetch_bb_address_ringbuf_head + i) % lengthof(fetch_bb_address_ringbuf);
const RIPVirtPhys& addr = fetch_bb_address_ringbuf[j];
os << " ", intstring(i, 16), ": ", addr, endl;
}
}
int OutOfOrderCore::hash_unaligned_predictor_slot(const RIPVirtPhysBase& rvp) {
W32 h = rvp.rip ^ rvp.mfnlo;
return lowbits(h, log2(UNALIGNED_PREDICTOR_SIZE));
}
bool OutOfOrderCore::get_unaligned_hint(const RIPVirtPhysBase& rvp) const {
int slot = hash_unaligned_predictor_slot(rvp);
return unaligned_predictor[slot];
}
void OutOfOrderCore::set_unaligned_hint(const RIPVirtPhysBase& rvp, bool value) {
int slot = hash_unaligned_predictor_slot(rvp);
assert(inrange(slot, 0, UNALIGNED_PREDICTOR_SIZE));
unaligned_predictor[slot] = value;
}
bool ThreadContext::fetch() {
OutOfOrderCore& core = getcore();
EventLog& eventlog = core.eventlog;
time_this_scope(ctfetch);
int fetchcount = 0;
int taken_branch_count = 0;
OutOfOrderCoreEvent* event;
if unlikely (stall_frontend) {
if unlikely (config.event_log_enabled) {
event = eventlog.add(EVENT_FETCH_STALLED);
event->threadid = threadid;
}
per_context_ooocore_stats_update(threadid, fetch.stop.stalled++);
return true;
}
if unlikely (waiting_for_icache_fill) {
if unlikely (config.event_log_enabled){
event = eventlog.add(EVENT_FETCH_ICACHE_WAIT);
event->threadid = threadid;
event->rip = fetchrip;
event->uuid = fetch_uuid;
}
per_context_ooocore_stats_update(threadid, fetch.stop.icache_miss++);
return true;
}
while ((fetchcount < FETCH_WIDTH) && (taken_branch_count == 0)) {
if unlikely (!fetchq.remaining()) {
if unlikely (config.event_log_enabled) {
if (!fetchcount) {
event = eventlog.add(EVENT_FETCH_FETCHQ_FULL);
event->threadid = threadid;
event->uuid = fetch_uuid;
}
}
per_context_ooocore_stats_update(threadid, fetch.stop.fetchq_full++);
break;
}
#ifndef MULTI_IQ
if unlikely (core.threadcount > 1) {
if ((issueq_count + fetchcount) >= core.reserved_iq_entries) {
bool empty = false;
issueq_operation_on_cluster_with_result(core, cluster, empty, shared_empty());
//
//++MTY FIXME starvation occurs when only one thread is in running state:
// we should re-balance reserved pool depending on number of running threads,
// not total number of threads!
//
if (empty) {
// no shared entries left, stop fetching
break;
} else {
// found a shared entry: continue fetching
}
} else {
// still have reserved entries left, continue fetching
}
}
#endif
if unlikely ((fetchrip.rip == config.start_log_at_rip) && (fetchrip.rip != 0xffffffffffffffffULL)) {
config.start_log_at_iteration = 0;
logenable = 1;
}
if unlikely ((!current_basic_block) || (current_basic_block_transop_index >= current_basic_block->count)) {
fetch_bb_address_ringbuf[fetch_bb_address_ringbuf_head] = fetchrip;
fetch_bb_address_ringbuf_head = add_index_modulo(fetch_bb_address_ringbuf_head, +1, lengthof(fetch_bb_address_ringbuf));
fetch_or_translate_basic_block(fetchrip);
}
if unlikely (current_basic_block->invalidblock) {
if unlikely (config.event_log_enabled) {
event = eventlog.add(EVENT_FETCH_BOGUS_RIP, fetchrip);
event->threadid = threadid;
}
per_context_ooocore_stats_update(threadid, fetch.stop.bogus_rip++);
//
// Keep fetching - the decoder has injected assist microcode that
// branches to the invalid opcode or exec page fault handler.
//
}
#ifdef PTLSIM_HYPERVISOR
Waddr physaddr = (fetchrip.mfnlo << 12) + lowbits(fetchrip, 12);
#else
Waddr physaddr = fetchrip;
#endif
W64 req_icache_block = floor(physaddr, ICACHE_FETCH_GRANULARITY);
if ((!current_basic_block->invalidblock) && (req_icache_block != current_icache_block)) {
bool hit = core.caches.probe_icache(fetchrip, physaddr);
hit |= config.perfect_cache;
if unlikely (!hit) {
int missbuf = core.caches.initiate_icache_miss(physaddr, fetch_uuid,threadid);
if unlikely (config.event_log_enabled) {
event = eventlog.add(EVENT_FETCH_ICACHE_MISS, fetchrip);
event->fetch.missbuf = missbuf;
event->threadid = threadid;
event->uuid = fetch_uuid;
}
if unlikely (missbuf < 0) {
// Try to re-allocate a miss buffer on the next cycle
break;
}
waiting_for_icache_fill = 1;
waiting_for_icache_fill_physaddr = req_icache_block;
per_context_ooocore_stats_update(threadid, fetch.stop.icache_miss++);
break;
}
per_context_ooocore_stats_update(threadid, fetch.blocks++);
current_icache_block = req_icache_block;
per_context_dcache_stats_update(threadid, fetch.hit.L1++);
}
FetchBufferEntry& transop = *fetchq.alloc();
uopimpl_func_t synthop = null;
assert(current_basic_block->synthops);
if likely (!unaligned_ldst_buf.get(transop, synthop)) {
transop = current_basic_block->transops[current_basic_block_transop_index];
synthop = current_basic_block->synthops[current_basic_block_transop_index];
}
transop.unaligned = core.get_unaligned_hint(fetchrip) &&
((transop.opcode == OP_ld) | (transop.opcode == OP_ldx) | (transop.opcode == OP_st)) &&
(transop.cond == LDST_ALIGN_NORMAL);
transop.ld_st_truly_unaligned = 0;
transop.rip = fetchrip;
transop.uuid = fetch_uuid;
transop.threadid = threadid;
//
// Handle loads and stores marked as unaligned in the unaligned
// predictor predecode information. These uops are split into two
// parts (ld.lo, ld.hi or st.lo, st.hi) and the parts are put into
// a 4-entry buffer (unaligned_ldst_pair). Fetching continues
// from this buffer instead of the basic block until both uops
// are forced into the pipeline.
//
if unlikely (transop.unaligned) {
if unlikely (config.event_log_enabled) eventlog.add(EVENT_FETCH_SPLIT, transop);
split_unaligned(transop, unaligned_ldst_buf);
assert(unaligned_ldst_buf.get(transop, synthop));
}
assert(transop.bbindex == current_basic_block_transop_index);
transop.synthop = synthop;
current_basic_block_transop_index += (unaligned_ldst_buf.empty());
per_context_ooocore_stats_update(threadid, fetch.user_insns += transop.som);
if unlikely (isclass(transop.opcode, OPCLASS_BARRIER)) {
// We've hit an assist: stall the frontend until we resume or redirect
if unlikely (config.event_log_enabled) eventlog.add(EVENT_FETCH_ASSIST, transop);
per_context_ooocore_stats_update(threadid, fetch.stop.microcode_assist++);
stall_frontend = 1;
}
per_context_ooocore_stats_update(threadid, fetch.uops++);
Waddr predrip = 0;
bool redirectrip = false;
transop.rip = fetchrip;
transop.uuid = fetch_uuid++;
if (isbranch(transop.opcode)) {
transop.predinfo.uuid = transop.uuid;
transop.predinfo.bptype =
(isclass(transop.opcode, OPCLASS_COND_BRANCH) << log2(BRANCH_HINT_COND)) |
(isclass(transop.opcode, OPCLASS_INDIR_BRANCH) << log2(BRANCH_HINT_INDIRECT)) |
(bit(transop.extshift, log2(BRANCH_HINT_PUSH_RAS)) << log2(BRANCH_HINT_CALL)) |
(bit(transop.extshift, log2(BRANCH_HINT_POP_RAS)) << log2(BRANCH_HINT_RET));
// SMP/SMT: Fill in with target thread ID (if the predictor supports this):
transop.predinfo.ctxid = 0;
transop.predinfo.ripafter = fetchrip + transop.bytes;
predrip = branchpred.predict(transop.predinfo, transop.predinfo.bptype, transop.predinfo.ripafter, transop.riptaken);
redirectrip = 1;
per_context_ooocore_stats_update(threadid, branchpred.predictions++);
}
// Set up branches so mispredicts can be calculated correctly:
if unlikely (isclass(transop.opcode, OPCLASS_COND_BRANCH)) {
if unlikely (predrip != transop.riptaken) {
assert(predrip == transop.ripseq);
transop.cond = invert_cond(transop.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.
//
transop.synthop = get_synthcode_for_cond_branch(transop.opcode, transop.cond, transop.size, 0);
swap(transop.riptaken, transop.ripseq);
}
} else if unlikely (isclass(transop.opcode, OPCLASS_INDIR_BRANCH)) {
transop.riptaken = predrip;
transop.ripseq = predrip;
}
per_context_ooocore_stats_update(threadid, fetch.opclass[opclassof(transop.opcode)]++);
if unlikely (config.event_log_enabled) {
event = eventlog.add(EVENT_FETCH_OK, transop);
event->fetch.predrip = predrip;
}
if likely (transop.eom) {
fetchrip.rip += transop.bytes;
fetchrip.update(ctx);
if unlikely (isbranch(transop.opcode) && (transop.predinfo.bptype & (BRANCH_HINT_CALL|BRANCH_HINT_RET)))
branchpred.updateras(transop.predinfo, transop.predinfo.ripafter);
if unlikely (redirectrip) {
// follow to target, then end fetching for this cycle if predicted taken
bool taken = (predrip != fetchrip);
taken_branch_count += taken;
fetchrip = predrip;
fetchrip.update(ctx);
if (taken) {
fetchcount++;
per_context_ooocore_stats_update(threadid, fetch.stop.branch_taken++);
break;
}
}
}
fetchcount++;
}
per_context_ooocore_stats_update(threadid, fetch.stop.full_width += (fetchcount == FETCH_WIDTH));
per_context_ooocore_stats_update(threadid, fetch.width[fetchcount]++);
return true;
}
BasicBlock* ThreadContext::fetch_or_translate_basic_block(const RIPVirtPhys& rvp) {
time_this_scope(ctdecode);
if likely (current_basic_block) {
// Release our ref to the old basic block being fetched
current_basic_block->release();
current_basic_block = null;
}
BasicBlock* bb = bbcache(rvp);
if likely (bb) {
current_basic_block = bb;
} else {
current_basic_block = bbcache.translate(ctx, rvp);
assert(current_basic_block);
if unlikely (config.event_log_enabled) {
OutOfOrderCoreEvent* event = core.eventlog.add(EVENT_FETCH_TRANSLATE, rvp);
event->fetch.bb_uop_count = current_basic_block->count;
event->threadid = threadid;
}
}
//
// Acquire a reference to the new basic block being fetched.
// This must be done right away so future allocations do not
// reclaim the BB while we still have a reference to it.
//
current_basic_block->acquire();
current_basic_block->use(sim_cycle);
if unlikely (!current_basic_block->synthops) synth_uops_for_bb(*current_basic_block);
assert(current_basic_block->synthops);
current_basic_block_transop_index = 0;
assert(current_basic_block->rip == rvp);
return current_basic_block;
}
//
// Allocate and Rename Stages
//
void ThreadContext::rename() {
OutOfOrderCoreEvent* event;
time_this_scope(ctrename);
int prepcount = 0;
while (prepcount < FRONTEND_WIDTH) {
if unlikely (fetchq.empty()) {
if unlikely (config.event_log_enabled) {
if likely (!prepcount) {
event = core.eventlog.add(EVENT_RENAME_FETCHQ_EMPTY);
event->threadid = threadid;
}
}
per_context_ooocore_stats_update(threadid, frontend.status.fetchq_empty++);
break;
}
if unlikely (!ROB.remaining()) {
if unlikely (config.event_log_enabled) {
if likely (!prepcount) {
event = core.eventlog.add(EVENT_RENAME_ROB_FULL);
event->threadid = threadid;
}
}
per_context_ooocore_stats_update(threadid, frontend.status.rob_full++);
break;
}
FetchBufferEntry& fetchbuf = *fetchq.peek();
int phys_reg_file = -1;
W32 acceptable_phys_reg_files = phys_reg_files_writable_by_uop(fetchbuf);
foreach (i, PHYS_REG_FILE_COUNT) {
int reg_file_to_check = add_index_modulo(core.round_robin_reg_file_offset, i, PHYS_REG_FILE_COUNT);
if likely (bit(acceptable_phys_reg_files, reg_file_to_check) && core.physregfiles[reg_file_to_check].remaining()) {
phys_reg_file = reg_file_to_check; break;
}
}
if (phys_reg_file < 0) {
if unlikely (config.event_log_enabled) {
if likely (!prepcount) {
event = core.eventlog.add()->fill(EVENT_RENAME_PHYSREGS_FULL);
event->threadid = threadid;
}
}
per_context_ooocore_stats_update(threadid, frontend.status.physregs_full++);
break;
}
bool ld = isload(fetchbuf.opcode);
bool st = isstore(fetchbuf.opcode);
bool br = isbranch(fetchbuf.opcode);
if unlikely (ld && (loads_in_flight >= LDQ_SIZE)) {
if unlikely (config.event_log_enabled) { if likely (!prepcount) core.eventlog.add(EVENT_RENAME_LDQ_FULL)->threadid = threadid; }
per_context_ooocore_stats_update(threadid, frontend.status.ldq_full++);
break;
}
if unlikely (st && (stores_in_flight >= STQ_SIZE)) {
if unlikely (config.event_log_enabled) { if likely (!prepcount) core.eventlog.add(EVENT_RENAME_STQ_FULL)->threadid = threadid; }
per_context_ooocore_stats_update(threadid, frontend.status.stq_full++);
break;
}
if unlikely ((ld|st) && (!LSQ.remaining())) {
if unlikely (config.event_log_enabled) { if likely (!prepcount) core.eventlog.add(EVENT_RENAME_MEMQ_FULL)->threadid = threadid; }
break;
}
per_context_ooocore_stats_update(threadid, frontend.status.complete++);
FetchBufferEntry& transop = *fetchq.dequeue();
ReorderBufferEntry& rob = *ROB.alloc();
PhysicalRegister* physreg = null;
LoadStoreQueueEntry* lsqp = (ld|st) ? LSQ.alloc() : null;
LoadStoreQueueEntry& lsq = *lsqp;
rob.reset();
rob.uop = transop;
rob.entry_valid = 1;
rob.cycles_left = FRONTEND_STAGES;
rob.lsq = null;
if unlikely (ld|st) {
rob.lsq = &lsq;
lsq.rob = &rob;
lsq.store = st;
lsq.lfence = (transop.opcode == OP_mf) & ((transop.extshift & MF_TYPE_LFENCE) != 0);
lsq.sfence = (transop.opcode == OP_mf) & ((transop.extshift & MF_TYPE_SFENCE) != 0);
lsq.datavalid = 0;
lsq.addrvalid = 0;
lsq.invalid = 0;
loads_in_flight += (st == 0);
stores_in_flight += (st == 1);
}
per_context_ooocore_stats_update(threadid, frontend.alloc.reg += (!(ld|st|br)));
per_context_ooocore_stats_update(threadid, frontend.alloc.ldreg += ld);
per_context_ooocore_stats_update(threadid, frontend.alloc.sfr += st);
per_context_ooocore_stats_update(threadid, frontend.alloc.br += br);
//
// Rename operands:
//
rob.operands[RA] = specrrt[transop.ra];
rob.operands[RB] = specrrt[transop.rb];
rob.operands[RC] = specrrt[transop.rc];
rob.operands[RS] = &core.physregfiles[0][PHYS_REG_NULL]; // used for loads and stores only
// See notes above on Physical Register Recycling Complications
foreach (i, MAX_OPERANDS) {
rob.operands[i]->addref(rob, threadid);
assert(rob.operands[i]->state != PHYSREG_FREE);
if likely ((rob.operands[i]->state == PHYSREG_WAITING) |
(rob.operands[i]->state == PHYSREG_BYPASS) |
(rob.operands[i]->state == PHYSREG_WRITTEN)) {
rob.operands[i]->rob->consumer_count = min(rob.operands[i]->rob->consumer_count + 1, 255);
}
}
//
// Select a physical register file based on desired
// heuristics. We only consider a given register
// file N if bit N in the acceptable_phys_reg_files
// bitmap is set (otherwise it is off limits for
// the type of functional unit or cluster the uop
// must execute on).
//
// The phys_reg_file variable should be set to the
// register file ID selected by the heuristics.
//
//
// Default heuristics from above: phys_reg_file is already
// set to the first acceptable physical register file ID
// which has free registers.
//
rob.executable_on_cluster_mask = uop_executable_on_cluster[transop.opcode];
// This is used if there is exactly one physical register file per cluster:
// rob.executable_on_cluster_mask = (1 << phys_reg_file);
// For assignment only:
assert(bit(acceptable_phys_reg_files, phys_reg_file));
//
// Allocate the physical register
//
physreg = core.physregfiles[phys_reg_file].alloc(threadid);
assert(physreg);
physreg->flags = FLAG_WAIT;
physreg->data = 0xdeadbeefdeadbeefULL;
physreg->rob = &rob;
physreg->archreg = rob.uop.rd;
rob.physreg = physreg;
//
// Logging
//
if unlikely (config.event_log_enabled) {
OutOfOrderCoreEvent* event = core.eventlog.add(EVENT_RENAME_OK, &rob);
foreach (i, MAX_OPERANDS) rob.operands[i]->fill_operand_info(event->rename.opinfo[i]);
if likely (archdest_can_commit[transop.rd]) {
event->rename.oldphys = specrrt[transop.rd]->index();
event->rename.oldzf = specrrt[REG_zf]->index();
event->rename.oldcf = specrrt[REG_cf]->index();
event->rename.oldof = specrrt[REG_of]->index();
}
}
bool renamed_reg = 0;
bool renamed_flags = 0;
if likely (archdest_can_commit[transop.rd]) {
#ifdef ENABLE_TRANSIENT_VALUE_TRACKING
PhysicalRegister* oldmapping = specrrt[transop.rd];
if ((oldmapping->current_state_list == &physreg_waiting_list) |
(oldmapping->current_state_list == &physreg_ready_list)) {
oldmapping->rob->dest_renamed_before_writeback = 1;
}
if ((oldmapping->current_state_list == &physreg_waiting_list) |
(oldmapping->current_state_list == &physreg_ready_list) |
(oldmapping->current_state_list == &physreg_written_list)) {
oldmapping->rob->no_branches_between_renamings = specrrt.renamed_in_this_basic_block[transop.rd];
}
specrrt.renamed_in_this_basic_block[transop.rd] = 1;
#endif
specrrt[transop.rd]->unspecref(transop.rd, threadid);
specrrt[transop.rd] = rob.physreg;
rob.physreg->addspecref(transop.rd, threadid);
renamed_reg = archdest_is_visible[transop.rd];
}
if unlikely (!transop.nouserflags) {
if (transop.setflags & SETFLAG_ZF) {
specrrt[REG_zf]->unspecref(REG_zf, threadid);
specrrt[REG_zf] = rob.physreg;
rob.physreg->addspecref(REG_zf, threadid);
}
if (transop.setflags & SETFLAG_CF) {
specrrt[REG_cf]->unspecref(REG_cf, threadid);
specrrt[REG_cf] = rob.physreg;
rob.physreg->addspecref(REG_cf, threadid);
}
if (transop.setflags & SETFLAG_OF) {
specrrt[REG_of]->unspecref(REG_of, threadid);
specrrt[REG_of] = rob.physreg;
rob.physreg->addspecref(REG_of, threadid);
}
renamed_flags = (transop.setflags != 0);
}
foreach (i, MAX_OPERANDS) {
assert(rob.operands[i]->allocated());
}
#ifdef ENABLE_TRANSIENT_VALUE_TRACKING
if unlikely (br) specrrt.renamed_in_this_basic_block.reset();
#endif
per_context_ooocore_stats_update(threadid, frontend.renamed.none += ((!renamed_reg) && (!renamed_flags)));
per_context_ooocore_stats_update(threadid, frontend.renamed.reg += ((renamed_reg) && (!renamed_flags)));
per_context_ooocore_stats_update(threadid, frontend.renamed.flags += ((!renamed_reg) && (renamed_flags)));
per_context_ooocore_stats_update(threadid, frontend.renamed.reg_and_flags += ((renamed_reg) && (renamed_flags)));
rob.changestate(rob_frontend_list);
prepcount++;
}
per_context_ooocore_stats_update(threadid, frontend.width[prepcount]++);
}
void ThreadContext::frontend() {
time_this_scope(ctfrontend);
ReorderBufferEntry* rob;
foreach_list_mutable(rob_frontend_list, rob, entry, nextentry) {
if unlikely (rob->cycles_left <= 0) {
rob->cycles_left = -1;
rob->changestate(rob_ready_to_dispatch_list);
} else {
if unlikely (config.event_log_enabled) {
OutOfOrderCoreEvent* event = core.eventlog.add(EVENT_FRONTEND, rob);
event->frontend.cycles_left = rob->cycles_left;
}
}
rob->cycles_left--;
}
}
//
// Dispatch and Cluster Selection
//
static byte bit_indices_set_8bits[1<<8][8] = {
{0, 0, 0, 0, 0, 0, 0,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