2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "config/full_system.hh"
33 #include "config/use_checker.hh"
38 #include "arch/utility.hh"
39 #include "base/loader/symtab.hh"
40 #include "base/timebuf.hh"
41 #include "cpu/exetrace.hh"
42 #include "cpu/o3/commit.hh"
43 #include "cpu/o3/thread_state.hh"
46 #include "cpu/checker/cpu.hh"
49 #include "params/DerivO3CPU.hh"
52 DefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
54 : Event(CPU_Tick_Pri), commit(_commit), tid(_tid)
56 this->setFlags(Event::AutoDelete);
61 DefaultCommit<Impl>::TrapEvent::process()
63 // This will get reset by commit if it was switched out at the
64 // time of this event processing.
65 commit->trapSquash[tid] = true;
70 DefaultCommit<Impl>::TrapEvent::description() const
76 DefaultCommit<Impl>::DefaultCommit(O3CPU *_cpu, DerivO3CPUParams *params)
79 iewToCommitDelay(params->iewToCommitDelay),
80 commitToIEWDelay(params->commitToIEWDelay),
81 renameToROBDelay(params->renameToROBDelay),
82 fetchToCommitDelay(params->commitToFetchDelay),
83 renameWidth(params->renameWidth),
84 commitWidth(params->commitWidth),
85 numThreads(params->numThreads),
88 trapLatency(params->trapLatency)
91 _nextStatus = Inactive;
92 std::string policy = params->smtCommitPolicy;
94 //Convert string to lowercase
95 std::transform(policy.begin(), policy.end(), policy.begin(),
96 (int(*)(int)) tolower);
98 //Assign commit policy
99 if (policy == "aggressive"){
100 commitPolicy = Aggressive;
102 DPRINTF(Commit,"Commit Policy set to Aggressive.");
103 } else if (policy == "roundrobin"){
104 commitPolicy = RoundRobin;
106 //Set-Up Priority List
107 for (int tid=0; tid < numThreads; tid++) {
108 priority_list.push_back(tid);
111 DPRINTF(Commit,"Commit Policy set to Round Robin.");
112 } else if (policy == "oldestready"){
113 commitPolicy = OldestReady;
115 DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
117 assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
118 "RoundRobin,OldestReady}");
121 for (int i=0; i < numThreads; i++) {
122 commitStatus[i] = Idle;
123 changedROBNumEntries[i] = false;
124 checkEmptyROB[i] = false;
125 trapInFlight[i] = false;
126 committedStores[i] = false;
127 trapSquash[i] = false;
129 microPC[i] = nextMicroPC[i] = PC[i] = nextPC[i] = nextNPC[i] = 0;
136 template <class Impl>
138 DefaultCommit<Impl>::name() const
140 return cpu->name() + ".commit";
143 template <class Impl>
145 DefaultCommit<Impl>::regStats()
147 using namespace Stats;
149 .name(name() + ".commitCommittedInsts")
150 .desc("The number of committed instructions")
151 .prereq(commitCommittedInsts);
153 .name(name() + ".commitSquashedInsts")
154 .desc("The number of squashed insts skipped by commit")
155 .prereq(commitSquashedInsts);
157 .name(name() + ".commitSquashEvents")
158 .desc("The number of times commit is told to squash")
159 .prereq(commitSquashEvents);
161 .name(name() + ".commitNonSpecStalls")
162 .desc("The number of times commit has been forced to stall to "
163 "communicate backwards")
164 .prereq(commitNonSpecStalls);
166 .name(name() + ".branchMispredicts")
167 .desc("The number of times a branch was mispredicted")
168 .prereq(branchMispredicts);
170 .init(0,commitWidth,1)
171 .name(name() + ".COM:committed_per_cycle")
172 .desc("Number of insts commited each cycle")
177 .init(cpu->number_of_threads)
178 .name(name() + ".COM:count")
179 .desc("Number of instructions committed")
184 .init(cpu->number_of_threads)
185 .name(name() + ".COM:swp_count")
186 .desc("Number of s/w prefetches committed")
191 .init(cpu->number_of_threads)
192 .name(name() + ".COM:refs")
193 .desc("Number of memory references committed")
198 .init(cpu->number_of_threads)
199 .name(name() + ".COM:loads")
200 .desc("Number of loads committed")
205 .init(cpu->number_of_threads)
206 .name(name() + ".COM:membars")
207 .desc("Number of memory barriers committed")
212 .init(cpu->number_of_threads)
213 .name(name() + ".COM:branches")
214 .desc("Number of branches committed")
219 .init(cpu->number_of_threads)
220 .name(name() + ".COM:bw_limited")
221 .desc("number of insts not committed due to BW limits")
225 commitEligibleSamples
226 .name(name() + ".COM:bw_lim_events")
227 .desc("number cycles where commit BW limit reached")
231 template <class Impl>
233 DefaultCommit<Impl>::setThreads(std::vector<Thread *> &threads)
238 template <class Impl>
240 DefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
244 // Setup wire to send information back to IEW.
245 toIEW = timeBuffer->getWire(0);
247 // Setup wire to read data from IEW (for the ROB).
248 robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
251 template <class Impl>
253 DefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
257 // Setup wire to get instructions from rename (for the ROB).
258 fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
261 template <class Impl>
263 DefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
265 renameQueue = rq_ptr;
267 // Setup wire to get instructions from rename (for the ROB).
268 fromRename = renameQueue->getWire(-renameToROBDelay);
271 template <class Impl>
273 DefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
277 // Setup wire to get instructions from IEW.
278 fromIEW = iewQueue->getWire(-iewToCommitDelay);
281 template <class Impl>
283 DefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
285 iewStage = iew_stage;
290 DefaultCommit<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
292 activeThreads = at_ptr;
295 template <class Impl>
297 DefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
299 for (int i=0; i < numThreads; i++) {
300 renameMap[i] = &rm_ptr[i];
304 template <class Impl>
306 DefaultCommit<Impl>::setROB(ROB *rob_ptr)
311 template <class Impl>
313 DefaultCommit<Impl>::initStage()
315 rob->setActiveThreads(activeThreads);
318 // Broadcast the number of free entries.
319 for (int i=0; i < numThreads; i++) {
320 toIEW->commitInfo[i].usedROB = true;
321 toIEW->commitInfo[i].freeROBEntries = rob->numFreeEntries(i);
322 toIEW->commitInfo[i].emptyROB = true;
325 // Commit must broadcast the number of free entries it has at the
326 // start of the simulation, so it starts as active.
327 cpu->activateStage(O3CPU::CommitIdx);
329 cpu->activityThisCycle();
330 trapLatency = cpu->ticks(trapLatency);
333 template <class Impl>
335 DefaultCommit<Impl>::drain()
342 template <class Impl>
344 DefaultCommit<Impl>::switchOut()
347 drainPending = false;
351 template <class Impl>
353 DefaultCommit<Impl>::resume()
355 drainPending = false;
358 template <class Impl>
360 DefaultCommit<Impl>::takeOverFrom()
364 _nextStatus = Inactive;
365 for (int i=0; i < numThreads; i++) {
366 commitStatus[i] = Idle;
367 changedROBNumEntries[i] = false;
368 trapSquash[i] = false;
375 template <class Impl>
377 DefaultCommit<Impl>::updateStatus()
379 // reset ROB changed variable
380 std::list<unsigned>::iterator threads = activeThreads->begin();
381 std::list<unsigned>::iterator end = activeThreads->end();
383 while (threads != end) {
384 unsigned tid = *threads++;
386 changedROBNumEntries[tid] = false;
388 // Also check if any of the threads has a trap pending
389 if (commitStatus[tid] == TrapPending ||
390 commitStatus[tid] == FetchTrapPending) {
391 _nextStatus = Active;
395 if (_nextStatus == Inactive && _status == Active) {
396 DPRINTF(Activity, "Deactivating stage.\n");
397 cpu->deactivateStage(O3CPU::CommitIdx);
398 } else if (_nextStatus == Active && _status == Inactive) {
399 DPRINTF(Activity, "Activating stage.\n");
400 cpu->activateStage(O3CPU::CommitIdx);
403 _status = _nextStatus;
406 template <class Impl>
408 DefaultCommit<Impl>::setNextStatus()
412 std::list<unsigned>::iterator threads = activeThreads->begin();
413 std::list<unsigned>::iterator end = activeThreads->end();
415 while (threads != end) {
416 unsigned tid = *threads++;
418 if (commitStatus[tid] == ROBSquashing) {
423 squashCounter = squashes;
425 // If commit is currently squashing, then it will have activity for the
426 // next cycle. Set its next status as active.
428 _nextStatus = Active;
432 template <class Impl>
434 DefaultCommit<Impl>::changedROBEntries()
436 std::list<unsigned>::iterator threads = activeThreads->begin();
437 std::list<unsigned>::iterator end = activeThreads->end();
439 while (threads != end) {
440 unsigned tid = *threads++;
442 if (changedROBNumEntries[tid]) {
450 template <class Impl>
452 DefaultCommit<Impl>::numROBFreeEntries(unsigned tid)
454 return rob->numFreeEntries(tid);
457 template <class Impl>
459 DefaultCommit<Impl>::generateTrapEvent(unsigned tid)
461 DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
463 TrapEvent *trap = new TrapEvent(this, tid);
465 cpu->schedule(trap, curTick + trapLatency);
466 trapInFlight[tid] = true;
469 template <class Impl>
471 DefaultCommit<Impl>::generateTCEvent(unsigned tid)
473 assert(!trapInFlight[tid]);
474 DPRINTF(Commit, "Generating TC squash event for [tid:%i]\n", tid);
476 tcSquash[tid] = true;
479 template <class Impl>
481 DefaultCommit<Impl>::squashAll(unsigned tid)
483 // If we want to include the squashing instruction in the squash,
484 // then use one older sequence number.
485 // Hopefully this doesn't mess things up. Basically I want to squash
486 // all instructions of this thread.
487 InstSeqNum squashed_inst = rob->isEmpty() ?
488 0 : rob->readHeadInst(tid)->seqNum - 1;
490 // All younger instructions will be squashed. Set the sequence
491 // number as the youngest instruction in the ROB (0 in this case.
492 // Hopefully nothing breaks.)
493 youngestSeqNum[tid] = 0;
495 rob->squash(squashed_inst, tid);
496 changedROBNumEntries[tid] = true;
498 // Send back the sequence number of the squashed instruction.
499 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
501 // Send back the squash signal to tell stages that they should
503 toIEW->commitInfo[tid].squash = true;
505 // Send back the rob squashing signal so other stages know that
506 // the ROB is in the process of squashing.
507 toIEW->commitInfo[tid].robSquashing = true;
509 toIEW->commitInfo[tid].branchMispredict = false;
511 toIEW->commitInfo[tid].nextPC = PC[tid];
512 toIEW->commitInfo[tid].nextNPC = nextPC[tid];
513 toIEW->commitInfo[tid].nextMicroPC = nextMicroPC[tid];
516 template <class Impl>
518 DefaultCommit<Impl>::squashFromTrap(unsigned tid)
522 DPRINTF(Commit, "Squashing from trap, restarting at PC %#x\n", PC[tid]);
524 thread[tid]->trapPending = false;
525 thread[tid]->inSyscall = false;
526 trapInFlight[tid] = false;
528 trapSquash[tid] = false;
530 commitStatus[tid] = ROBSquashing;
531 cpu->activityThisCycle();
534 template <class Impl>
536 DefaultCommit<Impl>::squashFromTC(unsigned tid)
540 DPRINTF(Commit, "Squashing from TC, restarting at PC %#x\n", PC[tid]);
542 thread[tid]->inSyscall = false;
543 assert(!thread[tid]->trapPending);
545 commitStatus[tid] = ROBSquashing;
546 cpu->activityThisCycle();
548 tcSquash[tid] = false;
551 template <class Impl>
553 DefaultCommit<Impl>::tick()
555 wroteToTimeBuffer = false;
556 _nextStatus = Inactive;
558 if (drainPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
559 cpu->signalDrained();
560 drainPending = false;
564 if (activeThreads->empty())
567 std::list<unsigned>::iterator threads = activeThreads->begin();
568 std::list<unsigned>::iterator end = activeThreads->end();
570 // Check if any of the threads are done squashing. Change the
571 // status if they are done.
572 while (threads != end) {
573 unsigned tid = *threads++;
575 // Clear the bit saying if the thread has committed stores
577 committedStores[tid] = false;
579 if (commitStatus[tid] == ROBSquashing) {
581 if (rob->isDoneSquashing(tid)) {
582 commitStatus[tid] = Running;
584 DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
585 " insts this cycle.\n", tid);
587 toIEW->commitInfo[tid].robSquashing = true;
588 wroteToTimeBuffer = true;
595 markCompletedInsts();
597 threads = activeThreads->begin();
599 while (threads != end) {
600 unsigned tid = *threads++;
602 if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
603 // The ROB has more instructions it can commit. Its next status
605 _nextStatus = Active;
607 DynInstPtr inst = rob->readHeadInst(tid);
609 DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %#x is head of"
610 " ROB and ready to commit\n",
611 tid, inst->seqNum, inst->readPC());
613 } else if (!rob->isEmpty(tid)) {
614 DynInstPtr inst = rob->readHeadInst(tid);
616 DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
617 "%#x is head of ROB and not ready\n",
618 tid, inst->seqNum, inst->readPC());
621 DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
622 tid, rob->countInsts(tid), rob->numFreeEntries(tid));
626 if (wroteToTimeBuffer) {
627 DPRINTF(Activity, "Activity This Cycle.\n");
628 cpu->activityThisCycle();
635 template <class Impl>
637 DefaultCommit<Impl>::handleInterrupt()
639 if (interrupt != NoFault) {
640 // Wait until the ROB is empty and all stores have drained in
641 // order to enter the interrupt.
642 if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
643 // Squash or record that I need to squash this cycle if
644 // an interrupt needed to be handled.
645 DPRINTF(Commit, "Interrupt detected.\n");
647 // Clear the interrupt now that it's going to be handled
648 toIEW->commitInfo[0].clearInterrupt = true;
650 assert(!thread[0]->inSyscall);
651 thread[0]->inSyscall = true;
653 // CPU will handle interrupt.
654 cpu->processInterrupts(interrupt);
656 thread[0]->inSyscall = false;
658 commitStatus[0] = TrapPending;
660 // Generate trap squash event.
661 generateTrapEvent(0);
665 DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
667 } else if (commitStatus[0] != TrapPending &&
668 cpu->check_interrupts(cpu->tcBase(0)) &&
671 // Process interrupts if interrupts are enabled, not in PAL
672 // mode, and no other traps or external squashes are currently
674 // @todo: Allow other threads to handle interrupts.
676 // Get any interrupt that happened
677 interrupt = cpu->getInterrupts();
679 if (interrupt != NoFault) {
680 // Tell fetch that there is an interrupt pending. This
681 // will make fetch wait until it sees a non PAL-mode PC,
682 // at which point it stops fetching instructions.
683 toIEW->commitInfo[0].interruptPending = true;
687 #endif // FULL_SYSTEM
689 template <class Impl>
691 DefaultCommit<Impl>::commit()
695 // Check for any interrupt, and start processing it. Or if we
696 // have an outstanding interrupt and are at a point when it is
697 // valid to take an interrupt, process it.
698 if (cpu->check_interrupts(cpu->tcBase(0))) {
701 #endif // FULL_SYSTEM
703 ////////////////////////////////////
704 // Check for any possible squashes, handle them first
705 ////////////////////////////////////
706 std::list<unsigned>::iterator threads = activeThreads->begin();
707 std::list<unsigned>::iterator end = activeThreads->end();
709 while (threads != end) {
710 unsigned tid = *threads++;
712 // Not sure which one takes priority. I think if we have
713 // both, that's a bad sign.
714 if (trapSquash[tid] == true) {
715 assert(!tcSquash[tid]);
717 } else if (tcSquash[tid] == true) {
718 assert(commitStatus[tid] != TrapPending);
722 // Squashed sequence number must be older than youngest valid
723 // instruction in the ROB. This prevents squashes from younger
724 // instructions overriding squashes from older instructions.
725 if (fromIEW->squash[tid] &&
726 commitStatus[tid] != TrapPending &&
727 fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
729 DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
731 fromIEW->mispredPC[tid],
732 fromIEW->squashedSeqNum[tid]);
734 DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
736 fromIEW->nextPC[tid]);
738 commitStatus[tid] = ROBSquashing;
740 // If we want to include the squashing instruction in the squash,
741 // then use one older sequence number.
742 InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
744 if (fromIEW->includeSquashInst[tid] == true) {
748 // All younger instructions will be squashed. Set the sequence
749 // number as the youngest instruction in the ROB.
750 youngestSeqNum[tid] = squashed_inst;
752 rob->squash(squashed_inst, tid);
753 changedROBNumEntries[tid] = true;
755 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
757 toIEW->commitInfo[tid].squash = true;
759 // Send back the rob squashing signal so other stages know that
760 // the ROB is in the process of squashing.
761 toIEW->commitInfo[tid].robSquashing = true;
763 toIEW->commitInfo[tid].branchMispredict =
764 fromIEW->branchMispredict[tid];
766 toIEW->commitInfo[tid].branchTaken =
767 fromIEW->branchTaken[tid];
769 toIEW->commitInfo[tid].nextPC = fromIEW->nextPC[tid];
770 toIEW->commitInfo[tid].nextNPC = fromIEW->nextNPC[tid];
771 toIEW->commitInfo[tid].nextMicroPC = fromIEW->nextMicroPC[tid];
773 toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
775 if (toIEW->commitInfo[tid].branchMispredict) {
784 if (squashCounter != numThreads) {
785 // If we're not currently squashing, then get instructions.
788 // Try to commit any instructions.
792 //Check for any activity
793 threads = activeThreads->begin();
795 while (threads != end) {
796 unsigned tid = *threads++;
798 if (changedROBNumEntries[tid]) {
799 toIEW->commitInfo[tid].usedROB = true;
800 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
802 wroteToTimeBuffer = true;
803 changedROBNumEntries[tid] = false;
804 if (rob->isEmpty(tid))
805 checkEmptyROB[tid] = true;
808 // ROB is only considered "empty" for previous stages if: a)
809 // ROB is empty, b) there are no outstanding stores, c) IEW
810 // stage has received any information regarding stores that
812 // c) is checked by making sure to not consider the ROB empty
813 // on the same cycle as when stores have been committed.
814 // @todo: Make this handle multi-cycle communication between
816 if (checkEmptyROB[tid] && rob->isEmpty(tid) &&
817 !iewStage->hasStoresToWB(tid) && !committedStores[tid]) {
818 checkEmptyROB[tid] = false;
819 toIEW->commitInfo[tid].usedROB = true;
820 toIEW->commitInfo[tid].emptyROB = true;
821 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
822 wroteToTimeBuffer = true;
828 template <class Impl>
830 DefaultCommit<Impl>::commitInsts()
832 ////////////////////////////////////
834 // Note that commit will be handled prior to putting new
835 // instructions in the ROB so that the ROB only tries to commit
836 // instructions it has in this current cycle, and not instructions
837 // it is writing in during this cycle. Can't commit and squash
838 // things at the same time...
839 ////////////////////////////////////
841 DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
843 unsigned num_committed = 0;
845 DynInstPtr head_inst;
847 // Commit as many instructions as possible until the commit bandwidth
848 // limit is reached, or it becomes impossible to commit any more.
849 while (num_committed < commitWidth) {
850 int commit_thread = getCommittingThread();
852 if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
855 head_inst = rob->readHeadInst(commit_thread);
857 int tid = head_inst->threadNumber;
859 assert(tid == commit_thread);
861 DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
862 head_inst->seqNum, tid);
864 // If the head instruction is squashed, it is ready to retire
865 // (be removed from the ROB) at any time.
866 if (head_inst->isSquashed()) {
868 DPRINTF(Commit, "Retiring squashed instruction from "
871 rob->retireHead(commit_thread);
873 ++commitSquashedInsts;
875 // Record that the number of ROB entries has changed.
876 changedROBNumEntries[tid] = true;
878 PC[tid] = head_inst->readPC();
879 nextPC[tid] = head_inst->readNextPC();
880 nextNPC[tid] = head_inst->readNextNPC();
881 nextMicroPC[tid] = head_inst->readNextMicroPC();
883 // Increment the total number of non-speculative instructions
885 // Hack for now: it really shouldn't happen until after the
886 // commit is deemed to be successful, but this count is needed
888 thread[tid]->funcExeInst++;
890 // Try to commit the head instruction.
891 bool commit_success = commitHead(head_inst, num_committed);
893 if (commit_success) {
896 changedROBNumEntries[tid] = true;
898 // Set the doneSeqNum to the youngest committed instruction.
899 toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
901 ++commitCommittedInsts;
903 // To match the old model, don't count nops and instruction
904 // prefetches towards the total commit count.
905 if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
909 PC[tid] = nextPC[tid];
910 nextPC[tid] = nextNPC[tid];
911 nextNPC[tid] = nextNPC[tid] + sizeof(TheISA::MachInst);
912 microPC[tid] = nextMicroPC[tid];
913 nextMicroPC[tid] = microPC[tid] + 1;
917 // Debug statement. Checks to make sure we're not
918 // currently updating state while handling PC events.
919 assert(!thread[tid]->inSyscall && !thread[tid]->trapPending);
922 cpu->system->pcEventQueue.service(thread[tid]->getTC());
924 } while (oldpc != PC[tid]);
927 "PC skip function event, stopping commit\n");
931 DPRINTF(Commit, "Unable to commit head instruction PC:%#x "
932 "[tid:%i] [sn:%i].\n",
933 head_inst->readPC(), tid ,head_inst->seqNum);
939 DPRINTF(CommitRate, "%i\n", num_committed);
940 numCommittedDist.sample(num_committed);
942 if (num_committed == commitWidth) {
943 commitEligibleSamples++;
947 template <class Impl>
949 DefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
953 int tid = head_inst->threadNumber;
955 // If the instruction is not executed yet, then it will need extra
956 // handling. Signal backwards that it should be executed.
957 if (!head_inst->isExecuted()) {
958 // Keep this number correct. We have not yet actually executed
959 // and committed this instruction.
960 thread[tid]->funcExeInst--;
962 if (head_inst->isNonSpeculative() ||
963 head_inst->isStoreConditional() ||
964 head_inst->isMemBarrier() ||
965 head_inst->isWriteBarrier()) {
967 DPRINTF(Commit, "Encountered a barrier or non-speculative "
968 "instruction [sn:%lli] at the head of the ROB, PC %#x.\n",
969 head_inst->seqNum, head_inst->readPC());
971 if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
972 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
976 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
978 // Change the instruction so it won't try to commit again until
980 head_inst->clearCanCommit();
982 ++commitNonSpecStalls;
985 } else if (head_inst->isLoad()) {
986 if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
987 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
991 assert(head_inst->uncacheable());
992 DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %#x.\n",
993 head_inst->seqNum, head_inst->readPC());
995 // Send back the non-speculative instruction's sequence
996 // number. Tell the lsq to re-execute the load.
997 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
998 toIEW->commitInfo[tid].uncached = true;
999 toIEW->commitInfo[tid].uncachedLoad = head_inst;
1001 head_inst->clearCanCommit();
1005 panic("Trying to commit un-executed instruction "
1006 "of unknown type!\n");
1010 if (head_inst->isThreadSync()) {
1011 // Not handled for now.
1012 panic("Thread sync instructions are not handled yet.\n");
1015 // Check if the instruction caused a fault. If so, trap.
1016 Fault inst_fault = head_inst->getFault();
1018 // Stores mark themselves as completed.
1019 if (!head_inst->isStore() && inst_fault == NoFault) {
1020 head_inst->setCompleted();
1024 // Use checker prior to updating anything due to traps or PC
1027 cpu->checker->verify(head_inst);
1031 // DTB will sometimes need the machine instruction for when
1032 // faults happen. So we will set it here, prior to the DTB
1033 // possibly needing it for its fault.
1034 thread[tid]->setInst(
1035 static_cast<TheISA::MachInst>(head_inst->staticInst->machInst));
1037 if (inst_fault != NoFault) {
1038 DPRINTF(Commit, "Inst [sn:%lli] PC %#x has a fault\n",
1039 head_inst->seqNum, head_inst->readPC());
1041 if (iewStage->hasStoresToWB(tid) || inst_num > 0) {
1042 DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
1046 head_inst->setCompleted();
1049 if (cpu->checker && head_inst->isStore()) {
1050 cpu->checker->verify(head_inst);
1054 assert(!thread[tid]->inSyscall);
1056 // Mark that we're in state update mode so that the trap's
1057 // execution doesn't generate extra squashes.
1058 thread[tid]->inSyscall = true;
1060 // Execute the trap. Although it's slightly unrealistic in
1061 // terms of timing (as it doesn't wait for the full timing of
1062 // the trap event to complete before updating state), it's
1063 // needed to update the state as soon as possible. This
1064 // prevents external agents from changing any specific state
1065 // that the trap need.
1066 cpu->trap(inst_fault, tid);
1068 // Exit state update mode to avoid accidental updating.
1069 thread[tid]->inSyscall = false;
1071 commitStatus[tid] = TrapPending;
1073 if (head_inst->traceData) {
1074 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1075 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1076 head_inst->traceData->dump();
1077 delete head_inst->traceData;
1078 head_inst->traceData = NULL;
1081 // Generate trap squash event.
1082 generateTrapEvent(tid);
1083 // warn("%lli fault (%d) handled @ PC %08p", curTick, inst_fault->name(), head_inst->readPC());
1087 updateComInstStats(head_inst);
1090 if (thread[tid]->profile) {
1091 // bool usermode = TheISA::inUserMode(thread[tid]->getTC());
1092 // thread[tid]->profilePC = usermode ? 1 : head_inst->readPC();
1093 thread[tid]->profilePC = head_inst->readPC();
1094 ProfileNode *node = thread[tid]->profile->consume(thread[tid]->getTC(),
1095 head_inst->staticInst);
1098 thread[tid]->profileNode = node;
1102 if (head_inst->traceData) {
1103 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1104 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1105 head_inst->traceData->dump();
1106 delete head_inst->traceData;
1107 head_inst->traceData = NULL;
1110 // Update the commit rename map
1111 for (int i = 0; i < head_inst->numDestRegs(); i++) {
1112 renameMap[tid]->setEntry(head_inst->flattenedDestRegIdx(i),
1113 head_inst->renamedDestRegIdx(i));
1116 if (head_inst->isCopy())
1117 panic("Should not commit any copy instructions!");
1119 // Finally clear the head ROB entry.
1120 rob->retireHead(tid);
1122 // If this was a store, record it for this cycle.
1123 if (head_inst->isStore())
1124 committedStores[tid] = true;
1126 // Return true to indicate that we have committed an instruction.
1130 template <class Impl>
1132 DefaultCommit<Impl>::getInsts()
1134 DPRINTF(Commit, "Getting instructions from Rename stage.\n");
1136 // Read any renamed instructions and place them into the ROB.
1137 int insts_to_process = std::min((int)renameWidth, fromRename->size);
1139 for (int inst_num = 0; inst_num < insts_to_process; ++inst_num) {
1142 inst = fromRename->insts[inst_num];
1143 int tid = inst->threadNumber;
1145 if (!inst->isSquashed() &&
1146 commitStatus[tid] != ROBSquashing &&
1147 commitStatus[tid] != TrapPending) {
1148 changedROBNumEntries[tid] = true;
1150 DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ROB.\n",
1151 inst->readPC(), inst->seqNum, tid);
1153 rob->insertInst(inst);
1155 assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
1157 youngestSeqNum[tid] = inst->seqNum;
1159 DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1160 "squashed, skipping.\n",
1161 inst->readPC(), inst->seqNum, tid);
1166 template <class Impl>
1168 DefaultCommit<Impl>::skidInsert()
1170 DPRINTF(Commit, "Attempting to any instructions from rename into "
1173 for (int inst_num = 0; inst_num < fromRename->size; ++inst_num) {
1174 DynInstPtr inst = fromRename->insts[inst_num];
1176 if (!inst->isSquashed()) {
1177 DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ",
1178 "skidBuffer.\n", inst->readPC(), inst->seqNum,
1179 inst->threadNumber);
1180 skidBuffer.push(inst);
1182 DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1183 "squashed, skipping.\n",
1184 inst->readPC(), inst->seqNum, inst->threadNumber);
1189 template <class Impl>
1191 DefaultCommit<Impl>::markCompletedInsts()
1193 // Grab completed insts out of the IEW instruction queue, and mark
1194 // instructions completed within the ROB.
1195 for (int inst_num = 0;
1196 inst_num < fromIEW->size && fromIEW->insts[inst_num];
1199 if (!fromIEW->insts[inst_num]->isSquashed()) {
1200 DPRINTF(Commit, "[tid:%i]: Marking PC %#x, [sn:%lli] ready "
1202 fromIEW->insts[inst_num]->threadNumber,
1203 fromIEW->insts[inst_num]->readPC(),
1204 fromIEW->insts[inst_num]->seqNum);
1206 // Mark the instruction as ready to commit.
1207 fromIEW->insts[inst_num]->setCanCommit();
1212 template <class Impl>
1214 DefaultCommit<Impl>::robDoneSquashing()
1216 std::list<unsigned>::iterator threads = activeThreads->begin();
1217 std::list<unsigned>::iterator end = activeThreads->end();
1219 while (threads != end) {
1220 unsigned tid = *threads++;
1222 if (!rob->isDoneSquashing(tid))
1229 template <class Impl>
1231 DefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
1233 unsigned thread = inst->threadNumber;
1236 // Pick off the software prefetches
1239 if (inst->isDataPrefetch()) {
1240 statComSwp[thread]++;
1242 statComInst[thread]++;
1245 statComInst[thread]++;
1249 // Control Instructions
1251 if (inst->isControl())
1252 statComBranches[thread]++;
1255 // Memory references
1257 if (inst->isMemRef()) {
1258 statComRefs[thread]++;
1260 if (inst->isLoad()) {
1261 statComLoads[thread]++;
1265 if (inst->isMemBarrier()) {
1266 statComMembars[thread]++;
1270 ////////////////////////////////////////
1272 // SMT COMMIT POLICY MAINTAINED HERE //
1274 ////////////////////////////////////////
1275 template <class Impl>
1277 DefaultCommit<Impl>::getCommittingThread()
1279 if (numThreads > 1) {
1280 switch (commitPolicy) {
1283 //If Policy is Aggressive, commit will call
1284 //this function multiple times per
1286 return oldestReady();
1289 return roundRobin();
1292 return oldestReady();
1298 assert(!activeThreads->empty());
1299 int tid = activeThreads->front();
1301 if (commitStatus[tid] == Running ||
1302 commitStatus[tid] == Idle ||
1303 commitStatus[tid] == FetchTrapPending) {
1311 template<class Impl>
1313 DefaultCommit<Impl>::roundRobin()
1315 std::list<unsigned>::iterator pri_iter = priority_list.begin();
1316 std::list<unsigned>::iterator end = priority_list.end();
1318 while (pri_iter != end) {
1319 unsigned tid = *pri_iter;
1321 if (commitStatus[tid] == Running ||
1322 commitStatus[tid] == Idle ||
1323 commitStatus[tid] == FetchTrapPending) {
1325 if (rob->isHeadReady(tid)) {
1326 priority_list.erase(pri_iter);
1327 priority_list.push_back(tid);
1339 template<class Impl>
1341 DefaultCommit<Impl>::oldestReady()
1343 unsigned oldest = 0;
1346 std::list<unsigned>::iterator threads = activeThreads->begin();
1347 std::list<unsigned>::iterator end = activeThreads->end();
1349 while (threads != end) {
1350 unsigned tid = *threads++;
1352 if (!rob->isEmpty(tid) &&
1353 (commitStatus[tid] == Running ||
1354 commitStatus[tid] == Idle ||
1355 commitStatus[tid] == FetchTrapPending)) {
1357 if (rob->isHeadReady(tid)) {
1359 DynInstPtr head_inst = rob->readHeadInst(tid);
1364 } else if (head_inst->seqNum < oldest) {