2 * Copyright (c) 2010 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Copyright (c) 2004-2006 The Regents of The University of Michigan
15 * All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 #include "arch/utility.hh"
48 #include "base/cp_annotate.hh"
49 #include "base/loader/symtab.hh"
50 #include "cpu/timebuf.hh"
51 #include "config/full_system.hh"
52 #include "config/the_isa.hh"
53 #include "config/use_checker.hh"
54 #include "cpu/exetrace.hh"
55 #include "cpu/o3/commit.hh"
56 #include "cpu/o3/thread_state.hh"
57 #include "params/DerivO3CPU.hh"
60 #include "cpu/checker/cpu.hh"
66 DefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
68 : Event(CPU_Tick_Pri), commit(_commit), tid(_tid)
70 this->setFlags(AutoDelete);
75 DefaultCommit<Impl>::TrapEvent::process()
77 // This will get reset by commit if it was switched out at the
78 // time of this event processing.
79 commit->trapSquash[tid] = true;
84 DefaultCommit<Impl>::TrapEvent::description() const
90 DefaultCommit<Impl>::DefaultCommit(O3CPU *_cpu, DerivO3CPUParams *params)
93 iewToCommitDelay(params->iewToCommitDelay),
94 commitToIEWDelay(params->commitToIEWDelay),
95 renameToROBDelay(params->renameToROBDelay),
96 fetchToCommitDelay(params->commitToFetchDelay),
97 renameWidth(params->renameWidth),
98 commitWidth(params->commitWidth),
99 numThreads(params->numThreads),
102 trapLatency(params->trapLatency)
105 _nextStatus = Inactive;
106 std::string policy = params->smtCommitPolicy;
108 //Convert string to lowercase
109 std::transform(policy.begin(), policy.end(), policy.begin(),
110 (int(*)(int)) tolower);
112 //Assign commit policy
113 if (policy == "aggressive"){
114 commitPolicy = Aggressive;
116 DPRINTF(Commit,"Commit Policy set to Aggressive.");
117 } else if (policy == "roundrobin"){
118 commitPolicy = RoundRobin;
120 //Set-Up Priority List
121 for (ThreadID tid = 0; tid < numThreads; tid++) {
122 priority_list.push_back(tid);
125 DPRINTF(Commit,"Commit Policy set to Round Robin.");
126 } else if (policy == "oldestready"){
127 commitPolicy = OldestReady;
129 DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
131 assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
132 "RoundRobin,OldestReady}");
135 for (ThreadID tid = 0; tid < numThreads; tid++) {
136 commitStatus[tid] = Idle;
137 changedROBNumEntries[tid] = false;
138 checkEmptyROB[tid] = false;
139 trapInFlight[tid] = false;
140 committedStores[tid] = false;
141 trapSquash[tid] = false;
142 tcSquash[tid] = false;
150 template <class Impl>
152 DefaultCommit<Impl>::name() const
154 return cpu->name() + ".commit";
157 template <class Impl>
159 DefaultCommit<Impl>::regStats()
161 using namespace Stats;
163 .name(name() + ".commitCommittedInsts")
164 .desc("The number of committed instructions")
165 .prereq(commitCommittedInsts);
167 .name(name() + ".commitSquashedInsts")
168 .desc("The number of squashed insts skipped by commit")
169 .prereq(commitSquashedInsts);
171 .name(name() + ".commitSquashEvents")
172 .desc("The number of times commit is told to squash")
173 .prereq(commitSquashEvents);
175 .name(name() + ".commitNonSpecStalls")
176 .desc("The number of times commit has been forced to stall to "
177 "communicate backwards")
178 .prereq(commitNonSpecStalls);
180 .name(name() + ".branchMispredicts")
181 .desc("The number of times a branch was mispredicted")
182 .prereq(branchMispredicts);
184 .init(0,commitWidth,1)
185 .name(name() + ".COM:committed_per_cycle")
186 .desc("Number of insts commited each cycle")
191 .init(cpu->numThreads)
192 .name(name() + ".COM:count")
193 .desc("Number of instructions committed")
198 .init(cpu->numThreads)
199 .name(name() + ".COM:swp_count")
200 .desc("Number of s/w prefetches committed")
205 .init(cpu->numThreads)
206 .name(name() + ".COM:refs")
207 .desc("Number of memory references committed")
212 .init(cpu->numThreads)
213 .name(name() + ".COM:loads")
214 .desc("Number of loads committed")
219 .init(cpu->numThreads)
220 .name(name() + ".COM:membars")
221 .desc("Number of memory barriers committed")
226 .init(cpu->numThreads)
227 .name(name() + ".COM:branches")
228 .desc("Number of branches committed")
233 .init(cpu->numThreads)
234 .name(name() + ".COM:bw_limited")
235 .desc("number of insts not committed due to BW limits")
239 commitEligibleSamples
240 .name(name() + ".COM:bw_lim_events")
241 .desc("number cycles where commit BW limit reached")
245 template <class Impl>
247 DefaultCommit<Impl>::setThreads(std::vector<Thread *> &threads)
252 template <class Impl>
254 DefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
258 // Setup wire to send information back to IEW.
259 toIEW = timeBuffer->getWire(0);
261 // Setup wire to read data from IEW (for the ROB).
262 robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
265 template <class Impl>
267 DefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
271 // Setup wire to get instructions from rename (for the ROB).
272 fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
275 template <class Impl>
277 DefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
279 renameQueue = rq_ptr;
281 // Setup wire to get instructions from rename (for the ROB).
282 fromRename = renameQueue->getWire(-renameToROBDelay);
285 template <class Impl>
287 DefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
291 // Setup wire to get instructions from IEW.
292 fromIEW = iewQueue->getWire(-iewToCommitDelay);
295 template <class Impl>
297 DefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
299 iewStage = iew_stage;
304 DefaultCommit<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
306 activeThreads = at_ptr;
309 template <class Impl>
311 DefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
313 for (ThreadID tid = 0; tid < numThreads; tid++)
314 renameMap[tid] = &rm_ptr[tid];
317 template <class Impl>
319 DefaultCommit<Impl>::setROB(ROB *rob_ptr)
324 template <class Impl>
326 DefaultCommit<Impl>::initStage()
328 rob->setActiveThreads(activeThreads);
331 // Broadcast the number of free entries.
332 for (ThreadID tid = 0; tid < numThreads; tid++) {
333 toIEW->commitInfo[tid].usedROB = true;
334 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
335 toIEW->commitInfo[tid].emptyROB = true;
338 // Commit must broadcast the number of free entries it has at the
339 // start of the simulation, so it starts as active.
340 cpu->activateStage(O3CPU::CommitIdx);
342 cpu->activityThisCycle();
343 trapLatency = cpu->ticks(trapLatency);
346 template <class Impl>
348 DefaultCommit<Impl>::drain()
355 template <class Impl>
357 DefaultCommit<Impl>::switchOut()
360 drainPending = false;
364 template <class Impl>
366 DefaultCommit<Impl>::resume()
368 drainPending = false;
371 template <class Impl>
373 DefaultCommit<Impl>::takeOverFrom()
377 _nextStatus = Inactive;
378 for (ThreadID tid = 0; tid < numThreads; tid++) {
379 commitStatus[tid] = Idle;
380 changedROBNumEntries[tid] = false;
381 trapSquash[tid] = false;
382 tcSquash[tid] = false;
388 template <class Impl>
390 DefaultCommit<Impl>::updateStatus()
392 // reset ROB changed variable
393 list<ThreadID>::iterator threads = activeThreads->begin();
394 list<ThreadID>::iterator end = activeThreads->end();
396 while (threads != end) {
397 ThreadID tid = *threads++;
399 changedROBNumEntries[tid] = false;
401 // Also check if any of the threads has a trap pending
402 if (commitStatus[tid] == TrapPending ||
403 commitStatus[tid] == FetchTrapPending) {
404 _nextStatus = Active;
408 if (_nextStatus == Inactive && _status == Active) {
409 DPRINTF(Activity, "Deactivating stage.\n");
410 cpu->deactivateStage(O3CPU::CommitIdx);
411 } else if (_nextStatus == Active && _status == Inactive) {
412 DPRINTF(Activity, "Activating stage.\n");
413 cpu->activateStage(O3CPU::CommitIdx);
416 _status = _nextStatus;
419 template <class Impl>
421 DefaultCommit<Impl>::setNextStatus()
425 list<ThreadID>::iterator threads = activeThreads->begin();
426 list<ThreadID>::iterator end = activeThreads->end();
428 while (threads != end) {
429 ThreadID tid = *threads++;
431 if (commitStatus[tid] == ROBSquashing) {
436 squashCounter = squashes;
438 // If commit is currently squashing, then it will have activity for the
439 // next cycle. Set its next status as active.
441 _nextStatus = Active;
445 template <class Impl>
447 DefaultCommit<Impl>::changedROBEntries()
449 list<ThreadID>::iterator threads = activeThreads->begin();
450 list<ThreadID>::iterator end = activeThreads->end();
452 while (threads != end) {
453 ThreadID tid = *threads++;
455 if (changedROBNumEntries[tid]) {
463 template <class Impl>
465 DefaultCommit<Impl>::numROBFreeEntries(ThreadID tid)
467 return rob->numFreeEntries(tid);
470 template <class Impl>
472 DefaultCommit<Impl>::generateTrapEvent(ThreadID tid)
474 DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
476 TrapEvent *trap = new TrapEvent(this, tid);
478 cpu->schedule(trap, curTick() + trapLatency);
479 trapInFlight[tid] = true;
482 template <class Impl>
484 DefaultCommit<Impl>::generateTCEvent(ThreadID tid)
486 assert(!trapInFlight[tid]);
487 DPRINTF(Commit, "Generating TC squash event for [tid:%i]\n", tid);
489 tcSquash[tid] = true;
492 template <class Impl>
494 DefaultCommit<Impl>::squashAll(ThreadID tid)
496 // If we want to include the squashing instruction in the squash,
497 // then use one older sequence number.
498 // Hopefully this doesn't mess things up. Basically I want to squash
499 // all instructions of this thread.
500 InstSeqNum squashed_inst = rob->isEmpty() ?
501 0 : rob->readHeadInst(tid)->seqNum - 1;
503 // All younger instructions will be squashed. Set the sequence
504 // number as the youngest instruction in the ROB (0 in this case.
505 // Hopefully nothing breaks.)
506 youngestSeqNum[tid] = 0;
508 rob->squash(squashed_inst, tid);
509 changedROBNumEntries[tid] = true;
511 // Send back the sequence number of the squashed instruction.
512 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
514 // Send back the squash signal to tell stages that they should
516 toIEW->commitInfo[tid].squash = true;
518 // Send back the rob squashing signal so other stages know that
519 // the ROB is in the process of squashing.
520 toIEW->commitInfo[tid].robSquashing = true;
522 toIEW->commitInfo[tid].branchMispredict = false;
524 toIEW->commitInfo[tid].pc = pc[tid];
527 template <class Impl>
529 DefaultCommit<Impl>::squashFromTrap(ThreadID tid)
533 DPRINTF(Commit, "Squashing from trap, restarting at PC %s\n", pc[tid]);
535 thread[tid]->trapPending = false;
536 thread[tid]->inSyscall = false;
537 trapInFlight[tid] = false;
539 trapSquash[tid] = false;
541 commitStatus[tid] = ROBSquashing;
542 cpu->activityThisCycle();
545 template <class Impl>
547 DefaultCommit<Impl>::squashFromTC(ThreadID tid)
551 DPRINTF(Commit, "Squashing from TC, restarting at PC %s\n", pc[tid]);
553 thread[tid]->inSyscall = false;
554 assert(!thread[tid]->trapPending);
556 commitStatus[tid] = ROBSquashing;
557 cpu->activityThisCycle();
559 tcSquash[tid] = false;
562 template <class Impl>
564 DefaultCommit<Impl>::squashAfter(ThreadID tid, uint64_t squash_after_seq_num)
566 youngestSeqNum[tid] = squash_after_seq_num;
568 rob->squash(squash_after_seq_num, tid);
569 changedROBNumEntries[tid] = true;
571 // Send back the sequence number of the squashed instruction.
572 toIEW->commitInfo[tid].doneSeqNum = squash_after_seq_num;
574 // Send back the squash signal to tell stages that they should squash.
575 toIEW->commitInfo[tid].squash = true;
577 // Send back the rob squashing signal so other stages know that
578 // the ROB is in the process of squashing.
579 toIEW->commitInfo[tid].robSquashing = true;
581 toIEW->commitInfo[tid].branchMispredict = false;
583 toIEW->commitInfo[tid].pc = pc[tid];
584 DPRINTF(Commit, "Executing squash after for [tid:%i] inst [sn:%lli]\n",
585 tid, squash_after_seq_num);
586 commitStatus[tid] = ROBSquashing;
589 template <class Impl>
591 DefaultCommit<Impl>::tick()
593 wroteToTimeBuffer = false;
594 _nextStatus = Inactive;
596 if (drainPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
597 cpu->signalDrained();
598 drainPending = false;
602 if (activeThreads->empty())
605 list<ThreadID>::iterator threads = activeThreads->begin();
606 list<ThreadID>::iterator end = activeThreads->end();
608 // Check if any of the threads are done squashing. Change the
609 // status if they are done.
610 while (threads != end) {
611 ThreadID tid = *threads++;
613 // Clear the bit saying if the thread has committed stores
615 committedStores[tid] = false;
617 if (commitStatus[tid] == ROBSquashing) {
619 if (rob->isDoneSquashing(tid)) {
620 commitStatus[tid] = Running;
622 DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
623 " insts this cycle.\n", tid);
625 toIEW->commitInfo[tid].robSquashing = true;
626 wroteToTimeBuffer = true;
633 markCompletedInsts();
635 threads = activeThreads->begin();
637 while (threads != end) {
638 ThreadID tid = *threads++;
640 if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
641 // The ROB has more instructions it can commit. Its next status
643 _nextStatus = Active;
645 DynInstPtr inst = rob->readHeadInst(tid);
647 DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %s is head of"
648 " ROB and ready to commit\n",
649 tid, inst->seqNum, inst->pcState());
651 } else if (!rob->isEmpty(tid)) {
652 DynInstPtr inst = rob->readHeadInst(tid);
654 DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
655 "%s is head of ROB and not ready\n",
656 tid, inst->seqNum, inst->pcState());
659 DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
660 tid, rob->countInsts(tid), rob->numFreeEntries(tid));
664 if (wroteToTimeBuffer) {
665 DPRINTF(Activity, "Activity This Cycle.\n");
666 cpu->activityThisCycle();
673 template <class Impl>
675 DefaultCommit<Impl>::handleInterrupt()
677 // Verify that we still have an interrupt to handle
678 if (!cpu->checkInterrupts(cpu->tcBase(0))) {
679 DPRINTF(Commit, "Pending interrupt is cleared by master before "
680 "it got handled. Restart fetching from the orig path.\n");
681 toIEW->commitInfo[0].clearInterrupt = true;
686 // Wait until the ROB is empty and all stores have drained in
687 // order to enter the interrupt.
688 if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
689 // Squash or record that I need to squash this cycle if
690 // an interrupt needed to be handled.
691 DPRINTF(Commit, "Interrupt detected.\n");
693 // Clear the interrupt now that it's going to be handled
694 toIEW->commitInfo[0].clearInterrupt = true;
696 assert(!thread[0]->inSyscall);
697 thread[0]->inSyscall = true;
699 // CPU will handle interrupt.
700 cpu->processInterrupts(interrupt);
702 thread[0]->inSyscall = false;
704 commitStatus[0] = TrapPending;
706 // Generate trap squash event.
707 generateTrapEvent(0);
711 DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
715 template <class Impl>
717 DefaultCommit<Impl>::propagateInterrupt()
719 if (commitStatus[0] == TrapPending || interrupt || trapSquash[0] ||
723 // Process interrupts if interrupts are enabled, not in PAL
724 // mode, and no other traps or external squashes are currently
726 // @todo: Allow other threads to handle interrupts.
728 // Get any interrupt that happened
729 interrupt = cpu->getInterrupts();
731 // Tell fetch that there is an interrupt pending. This
732 // will make fetch wait until it sees a non PAL-mode PC,
733 // at which point it stops fetching instructions.
734 if (interrupt != NoFault)
735 toIEW->commitInfo[0].interruptPending = true;
738 #endif // FULL_SYSTEM
740 template <class Impl>
742 DefaultCommit<Impl>::commit()
746 // Check for any interrupt that we've already squashed for and start processing it.
747 if (interrupt != NoFault)
750 // Check if we have a interrupt and get read to handle it
751 if (cpu->checkInterrupts(cpu->tcBase(0)))
752 propagateInterrupt();
753 #endif // FULL_SYSTEM
755 ////////////////////////////////////
756 // Check for any possible squashes, handle them first
757 ////////////////////////////////////
758 list<ThreadID>::iterator threads = activeThreads->begin();
759 list<ThreadID>::iterator end = activeThreads->end();
761 while (threads != end) {
762 ThreadID tid = *threads++;
764 // Not sure which one takes priority. I think if we have
765 // both, that's a bad sign.
766 if (trapSquash[tid] == true) {
767 assert(!tcSquash[tid]);
769 } else if (tcSquash[tid] == true) {
770 assert(commitStatus[tid] != TrapPending);
774 // Squashed sequence number must be older than youngest valid
775 // instruction in the ROB. This prevents squashes from younger
776 // instructions overriding squashes from older instructions.
777 if (fromIEW->squash[tid] &&
778 commitStatus[tid] != TrapPending &&
779 fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
781 DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
783 fromIEW->mispredPC[tid],
784 fromIEW->squashedSeqNum[tid]);
786 DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
788 fromIEW->pc[tid].nextInstAddr());
790 commitStatus[tid] = ROBSquashing;
792 // If we want to include the squashing instruction in the squash,
793 // then use one older sequence number.
794 InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
796 if (fromIEW->includeSquashInst[tid] == true) {
800 // All younger instructions will be squashed. Set the sequence
801 // number as the youngest instruction in the ROB.
802 youngestSeqNum[tid] = squashed_inst;
804 rob->squash(squashed_inst, tid);
805 changedROBNumEntries[tid] = true;
807 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
809 toIEW->commitInfo[tid].squash = true;
811 // Send back the rob squashing signal so other stages know that
812 // the ROB is in the process of squashing.
813 toIEW->commitInfo[tid].robSquashing = true;
815 toIEW->commitInfo[tid].branchMispredict =
816 fromIEW->branchMispredict[tid];
818 toIEW->commitInfo[tid].branchTaken =
819 fromIEW->branchTaken[tid];
821 toIEW->commitInfo[tid].pc = fromIEW->pc[tid];
823 toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
825 if (toIEW->commitInfo[tid].branchMispredict) {
834 if (squashCounter != numThreads) {
835 // If we're not currently squashing, then get instructions.
838 // Try to commit any instructions.
842 //Check for any activity
843 threads = activeThreads->begin();
845 while (threads != end) {
846 ThreadID tid = *threads++;
848 if (changedROBNumEntries[tid]) {
849 toIEW->commitInfo[tid].usedROB = true;
850 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
852 wroteToTimeBuffer = true;
853 changedROBNumEntries[tid] = false;
854 if (rob->isEmpty(tid))
855 checkEmptyROB[tid] = true;
858 // ROB is only considered "empty" for previous stages if: a)
859 // ROB is empty, b) there are no outstanding stores, c) IEW
860 // stage has received any information regarding stores that
862 // c) is checked by making sure to not consider the ROB empty
863 // on the same cycle as when stores have been committed.
864 // @todo: Make this handle multi-cycle communication between
866 if (checkEmptyROB[tid] && rob->isEmpty(tid) &&
867 !iewStage->hasStoresToWB(tid) && !committedStores[tid]) {
868 checkEmptyROB[tid] = false;
869 toIEW->commitInfo[tid].usedROB = true;
870 toIEW->commitInfo[tid].emptyROB = true;
871 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
872 wroteToTimeBuffer = true;
878 template <class Impl>
880 DefaultCommit<Impl>::commitInsts()
882 ////////////////////////////////////
884 // Note that commit will be handled prior to putting new
885 // instructions in the ROB so that the ROB only tries to commit
886 // instructions it has in this current cycle, and not instructions
887 // it is writing in during this cycle. Can't commit and squash
888 // things at the same time...
889 ////////////////////////////////////
891 DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
893 unsigned num_committed = 0;
895 DynInstPtr head_inst;
897 // Commit as many instructions as possible until the commit bandwidth
898 // limit is reached, or it becomes impossible to commit any more.
899 while (num_committed < commitWidth) {
900 int commit_thread = getCommittingThread();
902 if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
905 head_inst = rob->readHeadInst(commit_thread);
907 ThreadID tid = head_inst->threadNumber;
909 assert(tid == commit_thread);
911 DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
912 head_inst->seqNum, tid);
914 // If the head instruction is squashed, it is ready to retire
915 // (be removed from the ROB) at any time.
916 if (head_inst->isSquashed()) {
918 DPRINTF(Commit, "Retiring squashed instruction from "
921 rob->retireHead(commit_thread);
923 ++commitSquashedInsts;
925 // Record that the number of ROB entries has changed.
926 changedROBNumEntries[tid] = true;
928 pc[tid] = head_inst->pcState();
930 // Increment the total number of non-speculative instructions
932 // Hack for now: it really shouldn't happen until after the
933 // commit is deemed to be successful, but this count is needed
935 thread[tid]->funcExeInst++;
937 // Try to commit the head instruction.
938 bool commit_success = commitHead(head_inst, num_committed);
940 if (commit_success) {
943 changedROBNumEntries[tid] = true;
945 // Set the doneSeqNum to the youngest committed instruction.
946 toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
948 ++commitCommittedInsts;
950 // To match the old model, don't count nops and instruction
951 // prefetches towards the total commit count.
952 if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
956 // Updates misc. registers.
957 head_inst->updateMiscRegs();
959 TheISA::advancePC(pc[tid], head_inst->staticInst);
961 // If this is an instruction that doesn't play nicely with
962 // others squash everything and restart fetch
963 if (head_inst->isSquashAfter())
964 squashAfter(tid, head_inst->seqNum);
968 // Debug statement. Checks to make sure we're not
969 // currently updating state while handling PC events.
970 assert(!thread[tid]->inSyscall && !thread[tid]->trapPending);
972 oldpc = pc[tid].instAddr();
973 cpu->system->pcEventQueue.service(thread[tid]->getTC());
975 } while (oldpc != pc[tid].instAddr());
978 "PC skip function event, stopping commit\n");
982 DPRINTF(Commit, "Unable to commit head instruction PC:%s "
983 "[tid:%i] [sn:%i].\n",
984 head_inst->pcState(), tid ,head_inst->seqNum);
990 DPRINTF(CommitRate, "%i\n", num_committed);
991 numCommittedDist.sample(num_committed);
993 if (num_committed == commitWidth) {
994 commitEligibleSamples++;
998 template <class Impl>
1000 DefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
1004 ThreadID tid = head_inst->threadNumber;
1006 // If the instruction is not executed yet, then it will need extra
1007 // handling. Signal backwards that it should be executed.
1008 if (!head_inst->isExecuted()) {
1009 // Keep this number correct. We have not yet actually executed
1010 // and committed this instruction.
1011 thread[tid]->funcExeInst--;
1013 if (head_inst->isNonSpeculative() ||
1014 head_inst->isStoreConditional() ||
1015 head_inst->isMemBarrier() ||
1016 head_inst->isWriteBarrier()) {
1018 DPRINTF(Commit, "Encountered a barrier or non-speculative "
1019 "instruction [sn:%lli] at the head of the ROB, PC %s.\n",
1020 head_inst->seqNum, head_inst->pcState());
1022 if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
1023 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
1027 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1029 // Change the instruction so it won't try to commit again until
1031 head_inst->clearCanCommit();
1033 ++commitNonSpecStalls;
1036 } else if (head_inst->isLoad()) {
1037 if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
1038 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
1042 assert(head_inst->uncacheable());
1043 DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %s.\n",
1044 head_inst->seqNum, head_inst->pcState());
1046 // Send back the non-speculative instruction's sequence
1047 // number. Tell the lsq to re-execute the load.
1048 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1049 toIEW->commitInfo[tid].uncached = true;
1050 toIEW->commitInfo[tid].uncachedLoad = head_inst;
1052 head_inst->clearCanCommit();
1056 panic("Trying to commit un-executed instruction "
1057 "of unknown type!\n");
1061 if (head_inst->isThreadSync()) {
1062 // Not handled for now.
1063 panic("Thread sync instructions are not handled yet.\n");
1066 // Check if the instruction caused a fault. If so, trap.
1067 Fault inst_fault = head_inst->getFault();
1069 // Stores mark themselves as completed.
1070 if (!head_inst->isStore() && inst_fault == NoFault) {
1071 head_inst->setCompleted();
1075 // Use checker prior to updating anything due to traps or PC
1078 cpu->checker->verify(head_inst);
1082 if (inst_fault != NoFault) {
1083 DPRINTF(Commit, "Inst [sn:%lli] PC %s has a fault\n",
1084 head_inst->seqNum, head_inst->pcState());
1086 if (iewStage->hasStoresToWB(tid) || inst_num > 0) {
1087 DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
1091 head_inst->setCompleted();
1094 if (cpu->checker && head_inst->isStore()) {
1095 cpu->checker->verify(head_inst);
1099 assert(!thread[tid]->inSyscall);
1101 // Mark that we're in state update mode so that the trap's
1102 // execution doesn't generate extra squashes.
1103 thread[tid]->inSyscall = true;
1105 // Execute the trap. Although it's slightly unrealistic in
1106 // terms of timing (as it doesn't wait for the full timing of
1107 // the trap event to complete before updating state), it's
1108 // needed to update the state as soon as possible. This
1109 // prevents external agents from changing any specific state
1110 // that the trap need.
1111 cpu->trap(inst_fault, tid, head_inst->staticInst);
1113 // Exit state update mode to avoid accidental updating.
1114 thread[tid]->inSyscall = false;
1116 commitStatus[tid] = TrapPending;
1118 if (head_inst->traceData) {
1119 if (DTRACE(ExecFaulting)) {
1120 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1121 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1122 head_inst->traceData->dump();
1124 delete head_inst->traceData;
1125 head_inst->traceData = NULL;
1128 // Generate trap squash event.
1129 generateTrapEvent(tid);
1133 updateComInstStats(head_inst);
1136 if (thread[tid]->profile) {
1137 thread[tid]->profilePC = head_inst->instAddr();
1138 ProfileNode *node = thread[tid]->profile->consume(thread[tid]->getTC(),
1139 head_inst->staticInst);
1142 thread[tid]->profileNode = node;
1144 if (CPA::available()) {
1145 if (head_inst->isControl()) {
1146 ThreadContext *tc = thread[tid]->getTC();
1147 CPA::cpa()->swAutoBegin(tc, head_inst->nextInstAddr());
1152 if (head_inst->traceData) {
1153 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1154 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1155 head_inst->traceData->dump();
1156 delete head_inst->traceData;
1157 head_inst->traceData = NULL;
1160 // Update the commit rename map
1161 for (int i = 0; i < head_inst->numDestRegs(); i++) {
1162 renameMap[tid]->setEntry(head_inst->flattenedDestRegIdx(i),
1163 head_inst->renamedDestRegIdx(i));
1166 if (head_inst->isCopy())
1167 panic("Should not commit any copy instructions!");
1169 // Finally clear the head ROB entry.
1170 rob->retireHead(tid);
1172 // If this was a store, record it for this cycle.
1173 if (head_inst->isStore())
1174 committedStores[tid] = true;
1176 // Return true to indicate that we have committed an instruction.
1180 template <class Impl>
1182 DefaultCommit<Impl>::getInsts()
1184 DPRINTF(Commit, "Getting instructions from Rename stage.\n");
1186 // Read any renamed instructions and place them into the ROB.
1187 int insts_to_process = std::min((int)renameWidth, fromRename->size);
1189 for (int inst_num = 0; inst_num < insts_to_process; ++inst_num) {
1192 inst = fromRename->insts[inst_num];
1193 ThreadID tid = inst->threadNumber;
1195 if (!inst->isSquashed() &&
1196 commitStatus[tid] != ROBSquashing &&
1197 commitStatus[tid] != TrapPending) {
1198 changedROBNumEntries[tid] = true;
1200 DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ROB.\n",
1201 inst->pcState(), inst->seqNum, tid);
1203 rob->insertInst(inst);
1205 assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
1207 youngestSeqNum[tid] = inst->seqNum;
1209 DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
1210 "squashed, skipping.\n",
1211 inst->pcState(), inst->seqNum, tid);
1216 template <class Impl>
1218 DefaultCommit<Impl>::skidInsert()
1220 DPRINTF(Commit, "Attempting to any instructions from rename into "
1223 for (int inst_num = 0; inst_num < fromRename->size; ++inst_num) {
1224 DynInstPtr inst = fromRename->insts[inst_num];
1226 if (!inst->isSquashed()) {
1227 DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ",
1228 "skidBuffer.\n", inst->pcState(), inst->seqNum,
1229 inst->threadNumber);
1230 skidBuffer.push(inst);
1232 DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
1233 "squashed, skipping.\n",
1234 inst->pcState(), inst->seqNum, inst->threadNumber);
1239 template <class Impl>
1241 DefaultCommit<Impl>::markCompletedInsts()
1243 // Grab completed insts out of the IEW instruction queue, and mark
1244 // instructions completed within the ROB.
1245 for (int inst_num = 0;
1246 inst_num < fromIEW->size && fromIEW->insts[inst_num];
1249 if (!fromIEW->insts[inst_num]->isSquashed()) {
1250 DPRINTF(Commit, "[tid:%i]: Marking PC %s, [sn:%lli] ready "
1252 fromIEW->insts[inst_num]->threadNumber,
1253 fromIEW->insts[inst_num]->pcState(),
1254 fromIEW->insts[inst_num]->seqNum);
1256 // Mark the instruction as ready to commit.
1257 fromIEW->insts[inst_num]->setCanCommit();
1262 template <class Impl>
1264 DefaultCommit<Impl>::robDoneSquashing()
1266 list<ThreadID>::iterator threads = activeThreads->begin();
1267 list<ThreadID>::iterator end = activeThreads->end();
1269 while (threads != end) {
1270 ThreadID tid = *threads++;
1272 if (!rob->isDoneSquashing(tid))
1279 template <class Impl>
1281 DefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
1283 ThreadID tid = inst->threadNumber;
1286 // Pick off the software prefetches
1289 if (inst->isDataPrefetch()) {
1299 // Control Instructions
1301 if (inst->isControl())
1302 statComBranches[tid]++;
1305 // Memory references
1307 if (inst->isMemRef()) {
1310 if (inst->isLoad()) {
1311 statComLoads[tid]++;
1315 if (inst->isMemBarrier()) {
1316 statComMembars[tid]++;
1320 ////////////////////////////////////////
1322 // SMT COMMIT POLICY MAINTAINED HERE //
1324 ////////////////////////////////////////
1325 template <class Impl>
1327 DefaultCommit<Impl>::getCommittingThread()
1329 if (numThreads > 1) {
1330 switch (commitPolicy) {
1333 //If Policy is Aggressive, commit will call
1334 //this function multiple times per
1336 return oldestReady();
1339 return roundRobin();
1342 return oldestReady();
1345 return InvalidThreadID;
1348 assert(!activeThreads->empty());
1349 ThreadID tid = activeThreads->front();
1351 if (commitStatus[tid] == Running ||
1352 commitStatus[tid] == Idle ||
1353 commitStatus[tid] == FetchTrapPending) {
1356 return InvalidThreadID;
1361 template<class Impl>
1363 DefaultCommit<Impl>::roundRobin()
1365 list<ThreadID>::iterator pri_iter = priority_list.begin();
1366 list<ThreadID>::iterator end = priority_list.end();
1368 while (pri_iter != end) {
1369 ThreadID tid = *pri_iter;
1371 if (commitStatus[tid] == Running ||
1372 commitStatus[tid] == Idle ||
1373 commitStatus[tid] == FetchTrapPending) {
1375 if (rob->isHeadReady(tid)) {
1376 priority_list.erase(pri_iter);
1377 priority_list.push_back(tid);
1386 return InvalidThreadID;
1389 template<class Impl>
1391 DefaultCommit<Impl>::oldestReady()
1393 unsigned oldest = 0;
1396 list<ThreadID>::iterator threads = activeThreads->begin();
1397 list<ThreadID>::iterator end = activeThreads->end();
1399 while (threads != end) {
1400 ThreadID tid = *threads++;
1402 if (!rob->isEmpty(tid) &&
1403 (commitStatus[tid] == Running ||
1404 commitStatus[tid] == Idle ||
1405 commitStatus[tid] == FetchTrapPending)) {
1407 if (rob->isHeadReady(tid)) {
1409 DynInstPtr head_inst = rob->readHeadInst(tid);
1414 } else if (head_inst->seqNum < oldest) {
1424 return InvalidThreadID;