forked from aengelke/raspsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ooocore.cpp
1910 lines (1686 loc) · 67.7 KB
/
ooocore.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 Structures
//
// 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 <logic.h>
#include <dcache.h>
#define INSIDE_OOOCORE
#define DECLARE_STRUCTURES
#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;
namespace OutOfOrderModel {
byte uop_executable_on_cluster[OP_MAX_OPCODE];
W32 forward_at_cycle_lut[MAX_CLUSTERS][MAX_FORWARDING_LATENCY+1];
};
void StateList::init(const char* name, ListOfStateLists& lol, W32 flags) {
this->name = strdup(name);
this->flags = flags;
listid = lol.add(this);
reset();
}
void StateList::reset() {
selfqueuelink::reset();
count = 0;
dispatch_source_counter = 0;
issue_source_counter = 0;
}
int ListOfStateLists::add(StateList* list) {
assert(count < lengthof(data));
data[count] = list;
return count++;
}
void ListOfStateLists::reset() {
foreach (i, count) {
data[i]->reset();
}
}
//
// Initialize lookup tables used by the simulation
//
static void init_luts() {
// Initialize opcode maps
foreach (i, OP_MAX_OPCODE) {
W32 allowedfu = fuinfo[i].fu;
W32 allowedcl = 0;
foreach (cl, MAX_CLUSTERS) {
if (clusters[cl].fu_mask & allowedfu) setbit(allowedcl, cl);
}
uop_executable_on_cluster[i] = allowedcl;
}
// Initialize forward-at-cycle LUTs
foreach (srcc, MAX_CLUSTERS) {
foreach (destc, MAX_CLUSTERS) {
foreach (lat, MAX_FORWARDING_LATENCY+1) {
if (lat == intercluster_latency_map[srcc][destc]) {
setbit(forward_at_cycle_lut[srcc][lat], destc);
}
}
}
}
}
void ThreadContext::reset() {
setzero(specrrt);
setzero(commitrrt);
setzero(fetchrip);
current_basic_block = null;
current_basic_block_transop_index = -1;
stall_frontend = false;
waiting_for_icache_fill = false;
waiting_for_icache_fill_physaddr = 0;
fetch_uuid = 0;
current_icache_block = 0;
loads_in_flight = 0;
stores_in_flight = 0;
prev_interrupts_pending = false;
handle_interrupt_at_next_eom = false;
stop_at_next_eom = false;
last_commit_at_cycle = 0;
smc_invalidate_pending = 0;
setzero(smc_invalidate_rvp);
chk_recovery_rip = 0;
unaligned_ldst_buf.reset();
consecutive_commits_inside_spinlock = 0;
total_uops_committed = 0;
total_insns_committed = 0;
dispatch_deadlock_countdown = 0;
issueq_count = 0;
queued_mem_lock_release_count = 0;
branchpred.init();
}
void ThreadContext::init() {
rob_states.reset();
//
// ROB states
//
rob_free_list("free", rob_states, 0);
rob_frontend_list("frontend", rob_states, ROB_STATE_PRE_READY_TO_DISPATCH);
rob_ready_to_dispatch_list("ready-to-dispatch", rob_states, 0);
InitClusteredROBList(rob_dispatched_list, "dispatched", ROB_STATE_IN_ISSUE_QUEUE);
InitClusteredROBList(rob_ready_to_issue_list, "ready-to-issue", ROB_STATE_IN_ISSUE_QUEUE);
InitClusteredROBList(rob_ready_to_store_list, "ready-to-store", ROB_STATE_IN_ISSUE_QUEUE);
InitClusteredROBList(rob_ready_to_load_list, "ready-to-load", ROB_STATE_IN_ISSUE_QUEUE);
InitClusteredROBList(rob_issued_list, "issued", 0);
InitClusteredROBList(rob_completed_list, "completed", ROB_STATE_READY);
InitClusteredROBList(rob_ready_to_writeback_list, "ready-to-write", ROB_STATE_READY);
rob_cache_miss_list("cache-miss", rob_states, 0);
rob_tlb_miss_list("tlb-miss", rob_states, 0);
rob_memory_fence_list("memory-fence", rob_states, 0);
rob_ready_to_commit_queue("ready-to-commit", rob_states, ROB_STATE_READY);
reset();
}
void OutOfOrderCore::reset() {
round_robin_tid = 0;
round_robin_reg_file_offset = 0;
caches.reset();
caches.callback = &cache_callbacks;
setzero(robs_on_fu);
foreach_issueq(reset(coreid));
reserved_iq_entries = (int)std::sqrt((double) ISSUE_QUEUE_SIZE / MAX_THREADS_PER_CORE);
assert(reserved_iq_entries && reserved_iq_entries < ISSUE_QUEUE_SIZE);
foreach_issueq(set_reserved_entries(reserved_iq_entries * MAX_THREADS_PER_CORE));
foreach_issueq(reset_shared_entries());
unaligned_predictor.reset();
foreach (i, threadcount) threads[i]->reset();
}
void OutOfOrderCore::init_generic() {
reset();
}
template <typename T>
static void OutOfOrderModel::print_list_of_state_lists(ostream& os, const ListOfStateLists& lol, const char* title) {
os << title, ":", endl;
foreach (i, lol.count) {
StateList& list = *lol[i];
os << list.name, " (", list.count, " entries):", endl;
int n = 0;
T* obj;
foreach_list_mutable(list, obj, entry, nextentry) {
if ((n % 16) == 0) os << " ";
os << " ", intstring(obj->index(), -3);
if (((n % 16) == 15) || (n == list.count-1)) os << endl;
n++;
}
assert(n == list.count);
os << endl;
// list.validate();
}
}
void StateList::checkvalid() {
#if 0
int realcount = 0;
selfqueuelink* obj;
foreach_list_mutable(*this, obj, entry, nextentry) {
realcount++;
}
assert(count == realcount);
#endif
}
void PhysicalRegisterFile::init(const char* name, int coreid, int rfid, int size) {
assert(rfid < PHYS_REG_FILE_COUNT);
assert(size <= MAX_PHYS_REG_FILE_SIZE);
this->size = size;
this->coreid = coreid;
this->rfid = rfid;
this->name = name;
this->allocations = 0;
this->frees = 0;
foreach (i, MAX_PHYSREG_STATE) {
stringbuf sb;
sb << name, "-", physreg_state_names[i];
states[i].init(sb, getcore().physreg_states);
}
foreach (i, size) {
(*this)[i].init(coreid, rfid, i);
}
}
PhysicalRegister* PhysicalRegisterFile::alloc(W8 threadid, int r) {
PhysicalRegister* physreg = (PhysicalRegister*)((r == 0) ? &(*this)[r] : states[PHYSREG_FREE].peek());
if unlikely (!physreg) return null;
physreg->changestate(PHYSREG_WAITING);
physreg->flags = FLAG_WAIT;
physreg->threadid = threadid;
allocations++;
assert(states[PHYSREG_FREE].count >= 0);
return physreg;
}
ostream& PhysicalRegisterFile::print(ostream& os) const {
os << "PhysicalRegisterFile<", name, ", rfid ", rfid, ", size ", size, ">:", endl;
foreach (i, size) {
os << (*this)[i], endl;
}
return os;
}
void PhysicalRegisterFile::reset(W8 threadid) {
foreach (i, size) {
if ((*this)[i].threadid == threadid) {
(*this)[i].reset(threadid);
}
}
}
void PhysicalRegisterFile::reset() {
foreach (i, MAX_PHYSREG_STATE) {
states[i].reset();
}
foreach (i, size) {
(*this)[i].reset(0, false);
}
}
StateList& PhysicalRegister::get_state_list(int s) const {
return getcore().physregfiles[rfid].states[s];
}
namespace OutOfOrderModel {
ostream& operator <<(ostream& os, const PhysicalRegister& physreg) {
stringbuf sb;
print_value_and_flags(sb, physreg.data, physreg.flags);
os << "TH ", physreg.threadid, " rfid ", physreg.rfid;
os << " r", intstring(physreg.index(), -3), " state ", padstring(physreg.get_state_list().name, -12), " ", sb;
if (physreg.rob) os << " rob ", physreg.rob->index(), " (uuid ", physreg.rob->uop.uuid, ")";
os << " refcount ", physreg.refcount;
return os;
}
};
ostream& RegisterRenameTable::print(ostream& os) const {
foreach (i, TRANSREG_COUNT) {
if ((i % 8) == 0) os << " ";
os << " ", padstring(arch_reg_names[i], -6), " r", intstring((*this)[i]->index(), -3), " | ";
if (((i % 8) == 7) || (i == TRANSREG_COUNT-1)) os << endl;
}
return os;
}
//
// Get the thread priority, with lower numbers receiving higher priority.
// This is used to regulate the order in which fetch, rename, frontend
// and dispatch slots are filled in each cycle.
//
// The well known ICOUNT algorithm adds up the number of uops in
// the frontend pipeline stages and gives highest priority to
// the thread with the lowest number, since this thread is moving
// uops through very quickly and can make more progress.
//
int ThreadContext::get_priority() const {
int priority =
fetchq.count +
rob_frontend_list.count +
rob_ready_to_dispatch_list.count;
for_each_cluster (cluster) {
priority +=
rob_dispatched_list[cluster].count +
rob_ready_to_issue_list[cluster].count +
rob_ready_to_store_list[cluster].count +
rob_ready_to_load_list[cluster].count;
}
return priority;
}
//
// Execute one cycle of the entire core state machine
//
bool OutOfOrderCore::runcycle() {
bool exiting = 0;
//
// Detect edge triggered transition from 0->1 for
// pending interrupt events, then wait for current
// x86 insn EOM uop to commit before redirecting
// to the interrupt handler.
//
#ifdef PTLSIM_HYPERVISOR
foreach (i, threadcount) {
ThreadContext* thread = threads[i];
bool current_interrupts_pending = thread->ctx.check_events();
bool edge_triggered = ((!thread->prev_interrupts_pending) & current_interrupts_pending);
thread->handle_interrupt_at_next_eom |= edge_triggered;
thread->prev_interrupts_pending = current_interrupts_pending;
}
#endif
//
// Compute reserved issue queue entries to avoid starvation:
//
#ifdef ENABLE_CHECKS
int total_issueq_count = 0;
int total_issueq_reserved_free = 0;
foreach (i, MAX_THREADS_PER_CORE) {
ThreadContext* thread = threads[i];
if unlikely (!thread) {
total_issueq_reserved_free += reserved_iq_entries;
} else {
total_issueq_count += thread->issueq_count;
if(thread->issueq_count < reserved_iq_entries){
total_issueq_reserved_free += reserved_iq_entries - thread->issueq_count;
}
}
}
// assert (total_issueq_count == issueq_all.count);
// assert((ISSUE_QUEUE_SIZE - issueq_all.count) == (issueq_all.shared_entries + total_issueq_reserved_free));
#endif
foreach (i, threadcount) threads[i]->loads_in_this_cycle = 0;
fu_avail = bitmask(FU_COUNT);
caches.clock();
//
// Backend and issue pipe stages run with round robin priority
//
int commitrc[MAX_THREADS_PER_CORE];
commitcount = 0;
writecount = 0;
foreach (permute, threadcount) {
int tid = add_index_modulo(round_robin_tid, +permute, threadcount);
ThreadContext* thread = threads[tid];
if unlikely (!thread->ctx.running) continue;
commitrc[tid] = thread->commit();
for_each_cluster(j) thread->writeback(j);
for_each_cluster(j) thread->transfer(j);
}
//
// Clock the TLB miss page table walk state machine
// This may use up load ports, so do it before other
// loads can issue
//
#ifdef PTLSIM_HYPERVISOR
foreach (i, threadcount) {
threads[i]->tlbwalk();
}
#endif
//
// Issue whatever is ready
//
for_each_cluster(i) { issue(i); }
//
// Most of the frontend (except fetch!) also works with round robin priority
//
int dispatchrc[MAX_THREADS_PER_CORE];
dispatchcount = 0;
foreach (permute, threadcount) {
int tid = add_index_modulo(round_robin_tid, +permute, threadcount);
ThreadContext* thread = threads[tid];
if unlikely (!thread->ctx.running) continue;
for_each_cluster(j) { thread->complete(j); }
dispatchrc[tid] = thread->dispatch();
if likely (dispatchrc[tid] >= 0) {
thread->frontend();
thread->rename();
}
}
//
// Compute fetch priorities (default is ICOUNT algorithm)
//
// This means we sort in ascending order, with any unused threads
// (if any) given the lowest priority.
//
int priority_value[MAX_THREADS_PER_CORE];
int priority_index[MAX_THREADS_PER_CORE];
if likely (threadcount == 1) {
priority_value[0] = 0;
priority_index[0] = 0;
} else {
foreach (i, threadcount) {
priority_index[i] = i;
ThreadContext* thread = threads[i];
priority_value[i] = thread->get_priority();
if unlikely (!thread->ctx.running) priority_value[i] = limits<int>::max;
}
sort(priority_index, threadcount, SortPrecomputedIndexListComparator<int, false>(priority_value));
}
//
// Fetch in thread priority order
//
// NOTE: True ICOUNT only fetches the highest priority
// thread per cycle, since there is usually only one
// instruction cache port. In a banked i-cache, we can
// fetch from multiple threads every cycle.
//
foreach (j, threadcount) {
int i = priority_index[j];
ThreadContext* thread = threads[i];
assert(thread);
if unlikely (!thread->ctx.running) {
continue;
}
if likely (dispatchrc[i] >= 0) {
thread->fetch();
}
}
//
// Always clock the issue queues: they're independent of all threads
//
foreach_issueq(clock());
//
// Advance the round robin priority index
//
round_robin_tid = add_index_modulo(round_robin_tid, +1, threadcount);
//
// Flush event log ring buffer
//
if unlikely (config.event_log_enabled) {
// logfile << "[cycle ", sim_cycle, "] Miss buffer contents:", endl;
// logfile << caches.missbuf;
if unlikely (config.flush_event_log_every_cycle) {
eventlog.flush(true);
}
}
#ifdef ENABLE_CHECKS
// This significantly slows down simulation; only enable it if absolutely needed:
// check_refcounts();
#endif
foreach (i, threadcount) {
ThreadContext* thread = threads[i];
if unlikely (!thread->ctx.running) continue;
int rc = commitrc[i];
if likely ((rc == COMMIT_RESULT_OK) | (rc == COMMIT_RESULT_NONE)) continue;
switch (rc) {
case COMMIT_RESULT_SMC: {
if (logable(3)) logfile << "Potentially cross-modifying SMC detected: global flush required (cycle ", sim_cycle, ", ", total_user_insns_committed, " commits)", endl, flush;
//
// DO NOT GLOBALLY FLUSH! It will cut off the other thread(s) in the
// middle of their currently committing x86 instruction, causing massive
// internal corruption on any VCPUs that happen to be straddling the
// instruction boundary.
//
// BAD: machine.flush_all_pipelines();
//
// This is a temporary fix: in the *extremely* rare case where both
// threads have the same basic block in their pipelines and that
// BB is being invalidated, the BB cache will forbid us from
// freeing it (and will print a warning to that effect).
//
// I'm working on a solution to this, to put some BBs on an
// "invisible" list, where they cannot be looked up anymore,
// but their memory is not freed until the lock is released.
//
foreach (i, threadcount) {
ThreadContext* t = threads[i];
if unlikely (!t) continue;
if (logable(3)) {
logfile << " [vcpu ", i, "] current_basic_block = ", t->current_basic_block; ": ";
if (t->current_basic_block) logfile << t->current_basic_block->rip;
logfile << endl;
}
}
thread->flush_pipeline();
thread->invalidate_smc();
break;
}
case COMMIT_RESULT_EXCEPTION: {
exiting = !thread->handle_exception();
break;
}
case COMMIT_RESULT_BARRIER: {
exiting = !thread->handle_barrier();
break;
}
case COMMIT_RESULT_INTERRUPT: {
thread->handle_interrupt();
break;
}
case COMMIT_RESULT_STOP: {
thread->flush_pipeline();
thread->stall_frontend = 1;
machine.stopped[thread->ctx.vcpuid] = 1;
// Wait for other cores to sync up, so don't exit right away
break;
}
}
}
#ifdef PTLSIM_HYPERVISOR
if unlikely (vcpu_online_map_changed) {
vcpu_online_map_changed = 0;
foreach (i, contextcount) {
Context& vctx = contextof(i);
if likely (!vctx.dirty) continue;
//
// The VCPU is coming up for the first time after booting or being
// taken offline by the user.
//
// Force the active core model to flush any cached (uninitialized)
// internal state (like register file copies) it might have, since
// it did not know anything about this VCPU prior to now: if it
// suddenly gets marked as running without this, the core model
// will try to execute from bogus state data.
//
logfile << "VCPU ", vctx.vcpuid, " context was dirty: update core model internal state", endl;
ThreadContext* tc = threads[vctx.vcpuid];
assert(tc);
assert(&tc->ctx == &vctx);
tc->flush_pipeline();
vctx.dirty = 0;
}
}
#endif
foreach (i, threadcount) {
ThreadContext* thread = threads[i];
if unlikely (!thread->ctx.running) break;
if unlikely ((sim_cycle - thread->last_commit_at_cycle) > 4096) {
stringbuf sb;
sb << "[vcpu ", thread->ctx.vcpuid, "] thread ", thread->threadid, ": WARNING: At cycle ",
sim_cycle, ", ", total_user_insns_committed, " user commits: no instructions have committed for ",
(sim_cycle - thread->last_commit_at_cycle), " cycles; the pipeline could be deadlocked", endl;
logfile << sb, flush;
cerr << sb, flush;
exiting = 1;
}
}
return exiting;
}
//
// ReorderBufferEntry
//
void ReorderBufferEntry::init(int idx) {
this->idx = idx;
entry_valid = 0;
selfqueuelink::reset();
current_state_list = null;
reset();
}
//
// Clean out various fields from the ROB entry that are
// expected to be zero when allocating a new ROB entry.
//
void ReorderBufferEntry::reset() {
int latency, operand;
// Deallocate ROB entry
entry_valid = false;
cycles_left = 0;
physreg = (PhysicalRegister*)null;
lfrqslot = -1;
lsq = 0;
load_store_second_phase = 0;
lock_acquired = 0;
consumer_count = 0;
executable_on_cluster_mask = 0;
pteupdate = 0;
cluster = -1;
#ifdef ENABLE_TRANSIENT_VALUE_TRACKING
dest_renamed_before_writeback = 0;
no_branches_between_renamings = 0;
#endif
issued = 0;
}
bool ReorderBufferEntry::ready_to_issue() const {
bool raready = operands[0]->ready();
bool rbready = operands[1]->ready();
bool rcready = operands[2]->ready();
bool rsready = operands[3]->ready();
if (isstore(uop.opcode)) {
return (load_store_second_phase) ? (raready & rbready & rcready & rsready) : (raready & rbready);
} else if (isload(uop.opcode)) {
return (load_store_second_phase) ? (raready & rbready & rcready & rsready) : (raready & rbready & rcready);
} else {
return (raready & rbready & rcready & rsready);
}
}
bool ReorderBufferEntry::ready_to_commit() const {
return (current_state_list == &getthread().rob_ready_to_commit_queue);
}
StateList& ReorderBufferEntry::get_ready_to_issue_list() const {
OutOfOrderCore& core = getcore();
ThreadContext& thread = getthread();
return
isload(uop.opcode) ? thread.rob_ready_to_load_list[cluster] :
isstore(uop.opcode) ? thread.rob_ready_to_store_list[cluster] :
thread.rob_ready_to_issue_list[cluster];
}
//
// Reorder Buffer
//
stringbuf& ReorderBufferEntry::get_operand_info(stringbuf& sb, int operand) const {
PhysicalRegister& physreg = *operands[operand];
ReorderBufferEntry& sourcerob = *physreg.rob;
sb << "r", physreg.index();
if (PHYS_REG_FILE_COUNT > 1) sb << "@", getcore().physregfiles[physreg.rfid].name;
switch (physreg.state) {
case PHYSREG_WRITTEN:
sb << " (written)"; break;
case PHYSREG_BYPASS:
sb << " (ready)"; break;
case PHYSREG_WAITING:
sb << " (wait rob ", sourcerob.index(), " uuid ", sourcerob.uop.uuid, ")"; break;
case PHYSREG_ARCH: break;
if (physreg.index() == PHYS_REG_NULL) sb << " (zero)"; else sb << " (arch ", arch_reg_names[physreg.archreg], ")"; break;
case PHYSREG_PENDINGFREE:
sb << " (pending free for ", arch_reg_names[physreg.archreg], ")"; break;
default:
// Cannot be in free state!
sb << " (FREE)"; break;
}
return sb;
}
ThreadContext& ReorderBufferEntry::getthread() const { return *getcore().threads[threadid]; }
issueq_tag_t ReorderBufferEntry::get_tag() {
int mask = ((1 << MAX_THREADS_BIT) - 1) << MAX_ROB_IDX_BIT;
if (logable(100)) logfile << " get_tag() thread ", (void*) threadid, " rob idx ", (void*)idx, " mask ", (void*)mask, endl;
assert(!(idx & mask));
assert(!(threadid >> MAX_THREADS_BIT));
// int threadid = 1;
issueq_tag_t rc = (idx | (threadid << MAX_ROB_IDX_BIT));
if (logable(100)) logfile << " tag ", (void*) rc, endl;
return rc;
}
ostream& ReorderBufferEntry::print_operand_info(ostream& os, int operand) const {
stringbuf sb;
get_operand_info(sb, operand);
os << sb;
return os;
}
ostream& ReorderBufferEntry::print(ostream& os) const {
stringbuf name, rainfo, rbinfo, rcinfo;
nameof(name, uop);
get_operand_info(rainfo, 0);
get_operand_info(rbinfo, 1);
get_operand_info(rcinfo, 2);
os << "rob ", intstring(index(), -3), " uuid ", intstring(uop.uuid, 16), " rip 0x", hexstring(uop.rip, 48), " ",
padstring(current_state_list->name, -24), " ", (uop.som ? "SOM" : " "), " ", (uop.eom ? "EOM" : " "),
" @ ", padstring((cluster >= 0) ? clusters[cluster].name : "???", -4), " ",
padstring(name, -12), " r", intstring(physreg->index(), -3), " ", padstring(arch_reg_names[uop.rd], -6);
if (isload(uop.opcode))
os << " ld", intstring(lsq->index(), -3);
else if (isstore(uop.opcode))
os << " st", intstring(lsq->index(), -3);
else os << " ";
os << " = ";
os << padstring(rainfo, -30);
os << padstring(rbinfo, -30);
os << padstring(rcinfo, -30);
return os;
}
void ThreadContext::print_rob(ostream& os) {
os << "ROB head ", ROB.head, " to tail ", ROB.tail, " (", ROB.count, " entries):", endl;
foreach_forward(ROB, i) {
ReorderBufferEntry& rob = ROB[i];
os << " ", rob, endl;
}
}
void ThreadContext::print_lsq(ostream& os) {
os << "LSQ head ", LSQ.head, " to tail ", LSQ.tail, " (", LSQ.count, " entries):", endl;
foreach_forward(LSQ, i) {
LoadStoreQueueEntry& lsq = LSQ[i];
os << " ", lsq, endl;
}
}
void ThreadContext::print_rename_tables(ostream& os) {
os << "SpecRRT:", endl;
os << specrrt;
os << "CommitRRT:", endl;
os << commitrrt;
}
void OutOfOrderCore::print_smt_state(ostream& os) {
os << "Print SMT statistics:", endl;
foreach (i, threadcount) {
ThreadContext* thread = threads[i];
os << "Thread ", i, ":", endl,
" total_uops_committed ", thread->total_uops_committed, endl,
" uipc ", double(thread->total_uops_committed) / double(iterations), endl,
" total_insns_committed ", thread->total_insns_committed,
" ipc ", double(thread->total_insns_committed) / double(iterations), endl;
}
}
void ThreadContext::dump_smt_state(ostream& os) {
os << "SMT per-thread state for t", threadid, ":", endl;
print_rename_tables(os);
print_rob(os);
print_lsq(os);
os << flush;
}
void OutOfOrderCore::dump_smt_state(ostream& os) {
os << "SMT common structures:", endl;
print_list_of_state_lists<PhysicalRegister>(os, physreg_states, "Physical register states");
foreach (i, PHYS_REG_FILE_COUNT) {
os << physregfiles[i];
}
print_list_of_state_lists<ReorderBufferEntry>(os, rob_states, "ROB entry states");
os << "Issue Queues:", endl;
foreach_issueq(print(os));
caches.print(os);
os << "Unaligned predictor:", endl;
os << " ", unaligned_predictor.popcount(), " unaligned bits out of ", UNALIGNED_PREDICTOR_SIZE, " bits", endl;
os << " Raw data: ", unaligned_predictor, endl;
foreach (i, threadcount) {
ThreadContext* thread = threads[i];
thread->dump_smt_state(os);
}
}
//
// Validate the physical register reference counters against what
// is really accessible from the various tables and operand fields.
//
// This is for debugging only.
//
void OutOfOrderCore::check_refcounts() {
// this should be for each thread instead of whole core:
// for now, we just work on thread[0];
ThreadContext& thread = *threads[0];
Queue<ReorderBufferEntry, ROB_SIZE>& ROB = thread.ROB;
RegisterRenameTable& specrrt = thread.specrrt;
RegisterRenameTable& commitrrt = thread.commitrrt;
int refcounts[PHYS_REG_FILE_COUNT][MAX_PHYS_REG_FILE_SIZE];
memset(refcounts, 0, sizeof(refcounts));
foreach (rfid, PHYS_REG_FILE_COUNT) {
// Null physreg in each register file is special and can never be freed:
refcounts[rfid][PHYS_REG_NULL]++;
}
foreach_forward(ROB, i) {
ReorderBufferEntry& rob = ROB[i];
foreach (j, MAX_OPERANDS) {
refcounts[rob.operands[j]->rfid][rob.operands[j]->index()]++;
}
}
foreach (i, TRANSREG_COUNT) {
refcounts[commitrrt[i]->rfid][commitrrt[i]->index()]++;
refcounts[specrrt[i]->rfid][specrrt[i]->index()]++;
}
bool errors = 0;
foreach (rfid, PHYS_REG_FILE_COUNT) {
PhysicalRegisterFile& physregs = physregfiles[rfid];
foreach (i, physregs.size) {
if unlikely (physregs[i].refcount != refcounts[rfid][i]) {
logfile << "ERROR: r", i, " refcount is ", physregs[i].refcount, " but should be ", refcounts[rfid][i], endl;
foreach_forward(ROB, r) {
ReorderBufferEntry& rob = ROB[r];
foreach (j, MAX_OPERANDS) {
if ((rob.operands[j]->index() == i) & (rob.operands[j]->rfid == rfid)) logfile << " ROB ", r, " operand ", j, endl;
}
}
foreach (j, TRANSREG_COUNT) {
if ((commitrrt[j]->index() == i) & (commitrrt[j]->rfid == rfid)) logfile << " CommitRRT ", arch_reg_names[j], endl;
if ((specrrt[j]->index() == i) & (specrrt[j]->rfid == rfid)) logfile << " SpecRRT ", arch_reg_names[j], endl;
}
errors = 1;
}
}
}
if (errors) assert(false);
}
void OutOfOrderCore::check_rob() {
// this should be for each thread instead of whole core:
// for now, we just work on thread[0];
ThreadContext& thread = *threads[0];
Queue<ReorderBufferEntry, ROB_SIZE>& ROB = thread.ROB;
foreach (i, ROB_SIZE) {
ReorderBufferEntry& rob = ROB[i];
if (!rob.entry_valid) continue;
assert(inrange((int)rob.forward_cycle, 0, (MAX_FORWARDING_LATENCY+1)-1));
}
foreach (i, threadcount) {
ThreadContext* thread = threads[i];
foreach (i, rob_states.count) {
StateList& list = *(thread->rob_states[i]);
ReorderBufferEntry* rob;
foreach_list_mutable(list, rob, entry, nextentry) {
assert(inrange(rob->index(), 0, ROB_SIZE-1));
assert(rob->current_state_list == &list);
if (!((rob->current_state_list != &thread->rob_free_list) ? rob->entry_valid : (!rob->entry_valid))) {
logfile << "ROB ", rob->index(), " list = ", rob->current_state_list->name, " entry_valid ", rob->entry_valid, endl, flush;
dump_smt_state(logfile);
assert(false);
}
}
}
}
}
ostream& LoadStoreQueueEntry::print(ostream& os) const {
os << (store ? "st" : "ld"), intstring(index(), -3), " ";
os << "uuid ", intstring(rob->uop.uuid, 10), " ";
os << "rob ", intstring(rob->index(), -3), " ";
os << "r", intstring(rob->physreg->index(), -3);
if (PHYS_REG_FILE_COUNT > 1) os << "@", getcore().physregfiles[rob->physreg->rfid].name;
os << " ";
if (invalid) {
os << "< Invalid: fault 0x", hexstring(data, 8), " > ";
} else {
if (datavalid)
os << bytemaskstring((const byte*)&data, bytemask, 8);
else os << "< Data Invalid >";
os << " @ ";
if (addrvalid)
os << "0x", hexstring(physaddr << 3, 48);
else os << "< Addr Inval >";
}
return os;
}
//
// Barriers must flush the fetchq and stall the frontend until
// after the barrier is consumed. Execution resumes at the address
// in internal register nextrip (rip after the instruction) after
// handling the barrier in microcode.
//
bool ThreadContext::handle_barrier() {
// Release resources of everything in the pipeline:
core_to_external_state();
flush_pipeline();
int assistid = ctx.commitarf[REG_rip];
assist_func_t assist = (assist_func_t)(Waddr)assistid_to_func[assistid];
if (logable(4)) {
logfile << "[vcpu ", ctx.vcpuid, "] Barrier (#", assistid, " -> ", (void*)assist, " ", assist_name(assist), " called from ",
(RIPVirtPhys(ctx.commitarf[REG_selfrip]).update(ctx)), "; return to ", (void*)(Waddr)ctx.commitarf[REG_nextrip],
") at ", sim_cycle, " cycles, ", total_user_insns_committed, " commits", endl, flush;
}
if (logable(6)) logfile << "Calling assist function at ", (void*)assist, "...", endl, flush;
update_assist_stats(assist);
if (logable(6)) {
logfile << "Before assist:", endl, ctx, endl;
#ifdef PTLSIM_HYPERVISOR
logfile << sshinfo, endl;
#endif
}
assist(ctx);
if (logable(6)) {
logfile << "Done with assist", endl;
logfile << "New state:", endl;
logfile << ctx;
#ifdef PTLSIM_HYPERVISOR
logfile << sshinfo;
#endif
}
// Flush again, but restart at possibly modified rip
flush_pipeline();
#ifndef PTLSIM_HYPERVISOR
if (requested_switch_to_native) {
logfile << "PTL call requested switch to native mode at rip ", (void*)(Waddr)ctx.commitarf[REG_rip], endl;
return false;
}
#endif
return true;
}
bool ThreadContext::handle_exception() {
// Release resources of everything in the pipeline:
core_to_external_state();
flush_pipeline();
if (logable(4)) {
logfile << "[vcpu ", ctx.vcpuid, "] Exception ", exception_name(ctx.exception), " called from rip ", (void*)(Waddr)ctx.commitarf[REG_rip],
" at ", sim_cycle, " cycles, ", total_user_insns_committed, " commits", endl, flush;
}
//
// CheckFailed and SkipBlock exceptions are raised by the chk uop.
// This uop is used at the start of microcoded instructions to assert
// that certain conditions are true so complex corrective actions can
// be taken if the check fails.
//
// SkipBlock is a special case used for checks at the top of REP loops.
// Specifically, if the %rcx register is zero on entry to the REP, no
// action at all is to be taken; the rip should simply advance to
// whatever is in chk_recovery_rip and execution should resume.
//
// CheckFailed exceptions usually indicate the processor needs to take
// evasive action to avoid a user visible exception. For instance,
// CheckFailed is raised when an inlined floating point operand is
// denormal or otherwise cannot be handled by inlined fastpath uops,
// or when some unexpected segmentation or page table conditions
// arise.
//
if (ctx.exception == EXCEPTION_SkipBlock) {
ctx.commitarf[REG_rip] = chk_recovery_rip;