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 if (interrupt != NoFault) {
678 // Wait until the ROB is empty and all stores have drained in
679 // order to enter the interrupt.
680 if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
681 // Squash or record that I need to squash this cycle if
682 // an interrupt needed to be handled.
683 DPRINTF(Commit, "Interrupt detected.\n");
685 // Clear the interrupt now that it's going to be handled
686 toIEW->commitInfo[0].clearInterrupt = true;
688 assert(!thread[0]->inSyscall);
689 thread[0]->inSyscall = true;
691 // CPU will handle interrupt.
692 cpu->processInterrupts(interrupt);
694 thread[0]->inSyscall = false;
696 commitStatus[0] = TrapPending;
698 // Generate trap squash event.
699 generateTrapEvent(0);
703 DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
705 } else if (commitStatus[0] != TrapPending &&
706 cpu->checkInterrupts(cpu->tcBase(0)) &&
709 // Process interrupts if interrupts are enabled, not in PAL
710 // mode, and no other traps or external squashes are currently
712 // @todo: Allow other threads to handle interrupts.
714 // Get any interrupt that happened
715 interrupt = cpu->getInterrupts();
717 if (interrupt != NoFault) {
718 // Tell fetch that there is an interrupt pending. This
719 // will make fetch wait until it sees a non PAL-mode PC,
720 // at which point it stops fetching instructions.
721 toIEW->commitInfo[0].interruptPending = true;
725 #endif // FULL_SYSTEM
727 template <class Impl>
729 DefaultCommit<Impl>::commit()
733 // Check for any interrupt, and start processing it. Or if we
734 // have an outstanding interrupt and are at a point when it is
735 // valid to take an interrupt, process it.
736 if (cpu->checkInterrupts(cpu->tcBase(0))) {
739 #endif // FULL_SYSTEM
741 ////////////////////////////////////
742 // Check for any possible squashes, handle them first
743 ////////////////////////////////////
744 list<ThreadID>::iterator threads = activeThreads->begin();
745 list<ThreadID>::iterator end = activeThreads->end();
747 while (threads != end) {
748 ThreadID tid = *threads++;
750 // Not sure which one takes priority. I think if we have
751 // both, that's a bad sign.
752 if (trapSquash[tid] == true) {
753 assert(!tcSquash[tid]);
755 } else if (tcSquash[tid] == true) {
756 assert(commitStatus[tid] != TrapPending);
760 // Squashed sequence number must be older than youngest valid
761 // instruction in the ROB. This prevents squashes from younger
762 // instructions overriding squashes from older instructions.
763 if (fromIEW->squash[tid] &&
764 commitStatus[tid] != TrapPending &&
765 fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
767 DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
769 fromIEW->mispredPC[tid],
770 fromIEW->squashedSeqNum[tid]);
772 DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
774 fromIEW->pc[tid].nextInstAddr());
776 commitStatus[tid] = ROBSquashing;
778 // If we want to include the squashing instruction in the squash,
779 // then use one older sequence number.
780 InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
782 if (fromIEW->includeSquashInst[tid] == true) {
786 // All younger instructions will be squashed. Set the sequence
787 // number as the youngest instruction in the ROB.
788 youngestSeqNum[tid] = squashed_inst;
790 rob->squash(squashed_inst, tid);
791 changedROBNumEntries[tid] = true;
793 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
795 toIEW->commitInfo[tid].squash = true;
797 // Send back the rob squashing signal so other stages know that
798 // the ROB is in the process of squashing.
799 toIEW->commitInfo[tid].robSquashing = true;
801 toIEW->commitInfo[tid].branchMispredict =
802 fromIEW->branchMispredict[tid];
804 toIEW->commitInfo[tid].branchTaken =
805 fromIEW->branchTaken[tid];
807 toIEW->commitInfo[tid].pc = fromIEW->pc[tid];
809 toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
811 if (toIEW->commitInfo[tid].branchMispredict) {
820 if (squashCounter != numThreads) {
821 // If we're not currently squashing, then get instructions.
824 // Try to commit any instructions.
828 //Check for any activity
829 threads = activeThreads->begin();
831 while (threads != end) {
832 ThreadID tid = *threads++;
834 if (changedROBNumEntries[tid]) {
835 toIEW->commitInfo[tid].usedROB = true;
836 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
838 wroteToTimeBuffer = true;
839 changedROBNumEntries[tid] = false;
840 if (rob->isEmpty(tid))
841 checkEmptyROB[tid] = true;
844 // ROB is only considered "empty" for previous stages if: a)
845 // ROB is empty, b) there are no outstanding stores, c) IEW
846 // stage has received any information regarding stores that
848 // c) is checked by making sure to not consider the ROB empty
849 // on the same cycle as when stores have been committed.
850 // @todo: Make this handle multi-cycle communication between
852 if (checkEmptyROB[tid] && rob->isEmpty(tid) &&
853 !iewStage->hasStoresToWB(tid) && !committedStores[tid]) {
854 checkEmptyROB[tid] = false;
855 toIEW->commitInfo[tid].usedROB = true;
856 toIEW->commitInfo[tid].emptyROB = true;
857 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
858 wroteToTimeBuffer = true;
864 template <class Impl>
866 DefaultCommit<Impl>::commitInsts()
868 ////////////////////////////////////
870 // Note that commit will be handled prior to putting new
871 // instructions in the ROB so that the ROB only tries to commit
872 // instructions it has in this current cycle, and not instructions
873 // it is writing in during this cycle. Can't commit and squash
874 // things at the same time...
875 ////////////////////////////////////
877 DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
879 unsigned num_committed = 0;
881 DynInstPtr head_inst;
883 // Commit as many instructions as possible until the commit bandwidth
884 // limit is reached, or it becomes impossible to commit any more.
885 while (num_committed < commitWidth) {
886 int commit_thread = getCommittingThread();
888 if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
891 head_inst = rob->readHeadInst(commit_thread);
893 ThreadID tid = head_inst->threadNumber;
895 assert(tid == commit_thread);
897 DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
898 head_inst->seqNum, tid);
900 // If the head instruction is squashed, it is ready to retire
901 // (be removed from the ROB) at any time.
902 if (head_inst->isSquashed()) {
904 DPRINTF(Commit, "Retiring squashed instruction from "
907 rob->retireHead(commit_thread);
909 ++commitSquashedInsts;
911 // Record that the number of ROB entries has changed.
912 changedROBNumEntries[tid] = true;
914 pc[tid] = head_inst->pcState();
916 // Increment the total number of non-speculative instructions
918 // Hack for now: it really shouldn't happen until after the
919 // commit is deemed to be successful, but this count is needed
921 thread[tid]->funcExeInst++;
923 // Try to commit the head instruction.
924 bool commit_success = commitHead(head_inst, num_committed);
926 if (commit_success) {
929 changedROBNumEntries[tid] = true;
931 // Set the doneSeqNum to the youngest committed instruction.
932 toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
934 ++commitCommittedInsts;
936 // To match the old model, don't count nops and instruction
937 // prefetches towards the total commit count.
938 if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
942 // Updates misc. registers.
943 head_inst->updateMiscRegs();
945 TheISA::advancePC(pc[tid], head_inst->staticInst);
947 // If this is an instruction that doesn't play nicely with
948 // others squash everything and restart fetch
949 if (head_inst->isSquashAfter())
950 squashAfter(tid, head_inst->seqNum);
954 // Debug statement. Checks to make sure we're not
955 // currently updating state while handling PC events.
956 assert(!thread[tid]->inSyscall && !thread[tid]->trapPending);
958 oldpc = pc[tid].instAddr();
959 cpu->system->pcEventQueue.service(thread[tid]->getTC());
961 } while (oldpc != pc[tid].instAddr());
964 "PC skip function event, stopping commit\n");
968 DPRINTF(Commit, "Unable to commit head instruction PC:%s "
969 "[tid:%i] [sn:%i].\n",
970 head_inst->pcState(), tid ,head_inst->seqNum);
976 DPRINTF(CommitRate, "%i\n", num_committed);
977 numCommittedDist.sample(num_committed);
979 if (num_committed == commitWidth) {
980 commitEligibleSamples++;
984 template <class Impl>
986 DefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
990 ThreadID tid = head_inst->threadNumber;
992 // If the instruction is not executed yet, then it will need extra
993 // handling. Signal backwards that it should be executed.
994 if (!head_inst->isExecuted()) {
995 // Keep this number correct. We have not yet actually executed
996 // and committed this instruction.
997 thread[tid]->funcExeInst--;
999 if (head_inst->isNonSpeculative() ||
1000 head_inst->isStoreConditional() ||
1001 head_inst->isMemBarrier() ||
1002 head_inst->isWriteBarrier()) {
1004 DPRINTF(Commit, "Encountered a barrier or non-speculative "
1005 "instruction [sn:%lli] at the head of the ROB, PC %s.\n",
1006 head_inst->seqNum, head_inst->pcState());
1008 if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
1009 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
1013 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1015 // Change the instruction so it won't try to commit again until
1017 head_inst->clearCanCommit();
1019 ++commitNonSpecStalls;
1022 } else if (head_inst->isLoad()) {
1023 if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
1024 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
1028 assert(head_inst->uncacheable());
1029 DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %s.\n",
1030 head_inst->seqNum, head_inst->pcState());
1032 // Send back the non-speculative instruction's sequence
1033 // number. Tell the lsq to re-execute the load.
1034 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1035 toIEW->commitInfo[tid].uncached = true;
1036 toIEW->commitInfo[tid].uncachedLoad = head_inst;
1038 head_inst->clearCanCommit();
1042 panic("Trying to commit un-executed instruction "
1043 "of unknown type!\n");
1047 if (head_inst->isThreadSync()) {
1048 // Not handled for now.
1049 panic("Thread sync instructions are not handled yet.\n");
1052 // Check if the instruction caused a fault. If so, trap.
1053 Fault inst_fault = head_inst->getFault();
1055 // Stores mark themselves as completed.
1056 if (!head_inst->isStore() && inst_fault == NoFault) {
1057 head_inst->setCompleted();
1061 // Use checker prior to updating anything due to traps or PC
1064 cpu->checker->verify(head_inst);
1068 if (inst_fault != NoFault) {
1069 DPRINTF(Commit, "Inst [sn:%lli] PC %s has a fault\n",
1070 head_inst->seqNum, head_inst->pcState());
1072 if (iewStage->hasStoresToWB(tid) || inst_num > 0) {
1073 DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
1077 head_inst->setCompleted();
1080 if (cpu->checker && head_inst->isStore()) {
1081 cpu->checker->verify(head_inst);
1085 assert(!thread[tid]->inSyscall);
1087 // Mark that we're in state update mode so that the trap's
1088 // execution doesn't generate extra squashes.
1089 thread[tid]->inSyscall = true;
1091 // Execute the trap. Although it's slightly unrealistic in
1092 // terms of timing (as it doesn't wait for the full timing of
1093 // the trap event to complete before updating state), it's
1094 // needed to update the state as soon as possible. This
1095 // prevents external agents from changing any specific state
1096 // that the trap need.
1097 cpu->trap(inst_fault, tid, head_inst->staticInst);
1099 // Exit state update mode to avoid accidental updating.
1100 thread[tid]->inSyscall = false;
1102 commitStatus[tid] = TrapPending;
1104 if (head_inst->traceData) {
1105 if (DTRACE(ExecFaulting)) {
1106 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1107 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1108 head_inst->traceData->dump();
1110 delete head_inst->traceData;
1111 head_inst->traceData = NULL;
1114 // Generate trap squash event.
1115 generateTrapEvent(tid);
1119 updateComInstStats(head_inst);
1122 if (thread[tid]->profile) {
1123 thread[tid]->profilePC = head_inst->instAddr();
1124 ProfileNode *node = thread[tid]->profile->consume(thread[tid]->getTC(),
1125 head_inst->staticInst);
1128 thread[tid]->profileNode = node;
1130 if (CPA::available()) {
1131 if (head_inst->isControl()) {
1132 ThreadContext *tc = thread[tid]->getTC();
1133 CPA::cpa()->swAutoBegin(tc, head_inst->nextInstAddr());
1138 if (head_inst->traceData) {
1139 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1140 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1141 head_inst->traceData->dump();
1142 delete head_inst->traceData;
1143 head_inst->traceData = NULL;
1146 // Update the commit rename map
1147 for (int i = 0; i < head_inst->numDestRegs(); i++) {
1148 renameMap[tid]->setEntry(head_inst->flattenedDestRegIdx(i),
1149 head_inst->renamedDestRegIdx(i));
1152 if (head_inst->isCopy())
1153 panic("Should not commit any copy instructions!");
1155 // Finally clear the head ROB entry.
1156 rob->retireHead(tid);
1158 // If this was a store, record it for this cycle.
1159 if (head_inst->isStore())
1160 committedStores[tid] = true;
1162 // Return true to indicate that we have committed an instruction.
1166 template <class Impl>
1168 DefaultCommit<Impl>::getInsts()
1170 DPRINTF(Commit, "Getting instructions from Rename stage.\n");
1172 // Read any renamed instructions and place them into the ROB.
1173 int insts_to_process = std::min((int)renameWidth, fromRename->size);
1175 for (int inst_num = 0; inst_num < insts_to_process; ++inst_num) {
1178 inst = fromRename->insts[inst_num];
1179 ThreadID tid = inst->threadNumber;
1181 if (!inst->isSquashed() &&
1182 commitStatus[tid] != ROBSquashing &&
1183 commitStatus[tid] != TrapPending) {
1184 changedROBNumEntries[tid] = true;
1186 DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ROB.\n",
1187 inst->pcState(), inst->seqNum, tid);
1189 rob->insertInst(inst);
1191 assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
1193 youngestSeqNum[tid] = inst->seqNum;
1195 DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
1196 "squashed, skipping.\n",
1197 inst->pcState(), inst->seqNum, tid);
1202 template <class Impl>
1204 DefaultCommit<Impl>::skidInsert()
1206 DPRINTF(Commit, "Attempting to any instructions from rename into "
1209 for (int inst_num = 0; inst_num < fromRename->size; ++inst_num) {
1210 DynInstPtr inst = fromRename->insts[inst_num];
1212 if (!inst->isSquashed()) {
1213 DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ",
1214 "skidBuffer.\n", inst->pcState(), inst->seqNum,
1215 inst->threadNumber);
1216 skidBuffer.push(inst);
1218 DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
1219 "squashed, skipping.\n",
1220 inst->pcState(), inst->seqNum, inst->threadNumber);
1225 template <class Impl>
1227 DefaultCommit<Impl>::markCompletedInsts()
1229 // Grab completed insts out of the IEW instruction queue, and mark
1230 // instructions completed within the ROB.
1231 for (int inst_num = 0;
1232 inst_num < fromIEW->size && fromIEW->insts[inst_num];
1235 if (!fromIEW->insts[inst_num]->isSquashed()) {
1236 DPRINTF(Commit, "[tid:%i]: Marking PC %s, [sn:%lli] ready "
1238 fromIEW->insts[inst_num]->threadNumber,
1239 fromIEW->insts[inst_num]->pcState(),
1240 fromIEW->insts[inst_num]->seqNum);
1242 // Mark the instruction as ready to commit.
1243 fromIEW->insts[inst_num]->setCanCommit();
1248 template <class Impl>
1250 DefaultCommit<Impl>::robDoneSquashing()
1252 list<ThreadID>::iterator threads = activeThreads->begin();
1253 list<ThreadID>::iterator end = activeThreads->end();
1255 while (threads != end) {
1256 ThreadID tid = *threads++;
1258 if (!rob->isDoneSquashing(tid))
1265 template <class Impl>
1267 DefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
1269 ThreadID tid = inst->threadNumber;
1272 // Pick off the software prefetches
1275 if (inst->isDataPrefetch()) {
1285 // Control Instructions
1287 if (inst->isControl())
1288 statComBranches[tid]++;
1291 // Memory references
1293 if (inst->isMemRef()) {
1296 if (inst->isLoad()) {
1297 statComLoads[tid]++;
1301 if (inst->isMemBarrier()) {
1302 statComMembars[tid]++;
1306 ////////////////////////////////////////
1308 // SMT COMMIT POLICY MAINTAINED HERE //
1310 ////////////////////////////////////////
1311 template <class Impl>
1313 DefaultCommit<Impl>::getCommittingThread()
1315 if (numThreads > 1) {
1316 switch (commitPolicy) {
1319 //If Policy is Aggressive, commit will call
1320 //this function multiple times per
1322 return oldestReady();
1325 return roundRobin();
1328 return oldestReady();
1331 return InvalidThreadID;
1334 assert(!activeThreads->empty());
1335 ThreadID tid = activeThreads->front();
1337 if (commitStatus[tid] == Running ||
1338 commitStatus[tid] == Idle ||
1339 commitStatus[tid] == FetchTrapPending) {
1342 return InvalidThreadID;
1347 template<class Impl>
1349 DefaultCommit<Impl>::roundRobin()
1351 list<ThreadID>::iterator pri_iter = priority_list.begin();
1352 list<ThreadID>::iterator end = priority_list.end();
1354 while (pri_iter != end) {
1355 ThreadID tid = *pri_iter;
1357 if (commitStatus[tid] == Running ||
1358 commitStatus[tid] == Idle ||
1359 commitStatus[tid] == FetchTrapPending) {
1361 if (rob->isHeadReady(tid)) {
1362 priority_list.erase(pri_iter);
1363 priority_list.push_back(tid);
1372 return InvalidThreadID;
1375 template<class Impl>
1377 DefaultCommit<Impl>::oldestReady()
1379 unsigned oldest = 0;
1382 list<ThreadID>::iterator threads = activeThreads->begin();
1383 list<ThreadID>::iterator end = activeThreads->end();
1385 while (threads != end) {
1386 ThreadID tid = *threads++;
1388 if (!rob->isEmpty(tid) &&
1389 (commitStatus[tid] == Running ||
1390 commitStatus[tid] == Idle ||
1391 commitStatus[tid] == FetchTrapPending)) {
1393 if (rob->isHeadReady(tid)) {
1395 DynInstPtr head_inst = rob->readHeadInst(tid);
1400 } else if (head_inst->seqNum < oldest) {
1410 return InvalidThreadID;