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.
37 #include "base/loader/symtab.hh"
38 #include "base/timebuf.hh"
39 #include "cpu/checker/cpu.hh"
40 #include "cpu/exetrace.hh"
41 #include "cpu/o3/commit.hh"
42 #include "cpu/o3/thread_state.hh"
47 DefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
49 : Event(&mainEventQueue, CPU_Tick_Pri), commit(_commit), tid(_tid)
51 this->setFlags(Event::AutoDelete);
56 DefaultCommit<Impl>::TrapEvent::process()
58 // This will get reset by commit if it was switched out at the
59 // time of this event processing.
60 commit->trapSquash[tid] = true;
65 DefaultCommit<Impl>::TrapEvent::description()
71 DefaultCommit<Impl>::DefaultCommit(Params *params)
72 : dcacheInterface(params->dcacheInterface),
74 iewToCommitDelay(params->iewToCommitDelay),
75 commitToIEWDelay(params->commitToIEWDelay),
76 renameToROBDelay(params->renameToROBDelay),
77 fetchToCommitDelay(params->commitToFetchDelay),
78 renameWidth(params->renameWidth),
79 iewWidth(params->executeWidth),
80 commitWidth(params->commitWidth),
81 numThreads(params->numberOfThreads),
83 trapLatency(params->trapLatency),
84 fetchTrapLatency(params->fetchTrapLatency)
87 _nextStatus = Inactive;
88 string policy = params->smtCommitPolicy;
90 //Convert string to lowercase
91 std::transform(policy.begin(), policy.end(), policy.begin(),
92 (int(*)(int)) tolower);
94 //Assign commit policy
95 if (policy == "aggressive"){
96 commitPolicy = Aggressive;
98 DPRINTF(Commit,"Commit Policy set to Aggressive.");
99 } else if (policy == "roundrobin"){
100 commitPolicy = RoundRobin;
102 //Set-Up Priority List
103 for (int tid=0; tid < numThreads; tid++) {
104 priority_list.push_back(tid);
107 DPRINTF(Commit,"Commit Policy set to Round Robin.");
108 } else if (policy == "oldestready"){
109 commitPolicy = OldestReady;
111 DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
113 assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
114 "RoundRobin,OldestReady}");
117 for (int i=0; i < numThreads; i++) {
118 commitStatus[i] = Idle;
119 changedROBNumEntries[i] = false;
120 trapSquash[i] = false;
128 template <class Impl>
130 DefaultCommit<Impl>::name() const
132 return cpu->name() + ".commit";
135 template <class Impl>
137 DefaultCommit<Impl>::regStats()
139 using namespace Stats;
141 .name(name() + ".commitCommittedInsts")
142 .desc("The number of committed instructions")
143 .prereq(commitCommittedInsts);
145 .name(name() + ".commitSquashedInsts")
146 .desc("The number of squashed insts skipped by commit")
147 .prereq(commitSquashedInsts);
149 .name(name() + ".commitSquashEvents")
150 .desc("The number of times commit is told to squash")
151 .prereq(commitSquashEvents);
153 .name(name() + ".commitNonSpecStalls")
154 .desc("The number of times commit has been forced to stall to "
155 "communicate backwards")
156 .prereq(commitNonSpecStalls);
158 .name(name() + ".branchMispredicts")
159 .desc("The number of times a branch was mispredicted")
160 .prereq(branchMispredicts);
162 .init(0,commitWidth,1)
163 .name(name() + ".COM:committed_per_cycle")
164 .desc("Number of insts commited each cycle")
169 .init(cpu->number_of_threads)
170 .name(name() + ".COM:count")
171 .desc("Number of instructions committed")
176 .init(cpu->number_of_threads)
177 .name(name() + ".COM:swp_count")
178 .desc("Number of s/w prefetches committed")
183 .init(cpu->number_of_threads)
184 .name(name() + ".COM:refs")
185 .desc("Number of memory references committed")
190 .init(cpu->number_of_threads)
191 .name(name() + ".COM:loads")
192 .desc("Number of loads committed")
197 .init(cpu->number_of_threads)
198 .name(name() + ".COM:membars")
199 .desc("Number of memory barriers committed")
204 .init(cpu->number_of_threads)
205 .name(name() + ".COM:branches")
206 .desc("Number of branches committed")
211 // Commit-Eligible instructions...
213 // -> The number of instructions eligible to commit in those
214 // cycles where we reached our commit BW limit (less the number
215 // actually committed)
217 // -> The average value is computed over ALL CYCLES... not just
218 // the BW limited cycles
220 // -> The standard deviation is computed only over cycles where
221 // we reached the BW limit
224 .init(cpu->number_of_threads)
225 .name(name() + ".COM:bw_limited")
226 .desc("number of insts not committed due to BW limits")
230 commitEligibleSamples
231 .name(name() + ".COM:bw_lim_events")
232 .desc("number cycles where commit BW limit reached")
236 template <class Impl>
238 DefaultCommit<Impl>::setCPU(FullCPU *cpu_ptr)
240 DPRINTF(Commit, "Commit: Setting CPU pointer.\n");
243 // Commit must broadcast the number of free entries it has at the start of
244 // the simulation, so it starts as active.
245 cpu->activateStage(FullCPU::CommitIdx);
247 trapLatency = cpu->cycles(trapLatency);
248 fetchTrapLatency = cpu->cycles(fetchTrapLatency);
251 template <class Impl>
253 DefaultCommit<Impl>::setThreads(vector<Thread *> &threads)
258 template <class Impl>
260 DefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
262 DPRINTF(Commit, "Commit: Setting time buffer pointer.\n");
265 // Setup wire to send information back to IEW.
266 toIEW = timeBuffer->getWire(0);
268 // Setup wire to read data from IEW (for the ROB).
269 robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
272 template <class Impl>
274 DefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
276 DPRINTF(Commit, "Commit: Setting fetch queue pointer.\n");
279 // Setup wire to get instructions from rename (for the ROB).
280 fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
283 template <class Impl>
285 DefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
287 DPRINTF(Commit, "Commit: Setting rename queue pointer.\n");
288 renameQueue = rq_ptr;
290 // Setup wire to get instructions from rename (for the ROB).
291 fromRename = renameQueue->getWire(-renameToROBDelay);
294 template <class Impl>
296 DefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
298 DPRINTF(Commit, "Commit: Setting IEW queue pointer.\n");
301 // Setup wire to get instructions from IEW.
302 fromIEW = iewQueue->getWire(-iewToCommitDelay);
305 template <class Impl>
307 DefaultCommit<Impl>::setFetchStage(Fetch *fetch_stage)
309 fetchStage = fetch_stage;
312 template <class Impl>
314 DefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
316 iewStage = iew_stage;
321 DefaultCommit<Impl>::setActiveThreads(list<unsigned> *at_ptr)
323 DPRINTF(Commit, "Commit: Setting active threads list pointer.\n");
324 activeThreads = at_ptr;
327 template <class Impl>
329 DefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
331 DPRINTF(Commit, "Setting rename map pointers.\n");
333 for (int i=0; i < numThreads; i++) {
334 renameMap[i] = &rm_ptr[i];
338 template <class Impl>
340 DefaultCommit<Impl>::setROB(ROB *rob_ptr)
342 DPRINTF(Commit, "Commit: Setting ROB pointer.\n");
346 template <class Impl>
348 DefaultCommit<Impl>::initStage()
350 rob->setActiveThreads(activeThreads);
353 // Broadcast the number of free entries.
354 for (int i=0; i < numThreads; i++) {
355 toIEW->commitInfo[i].usedROB = true;
356 toIEW->commitInfo[i].freeROBEntries = rob->numFreeEntries(i);
359 cpu->activityThisCycle();
362 template <class Impl>
364 DefaultCommit<Impl>::switchOut()
366 switchPending = true;
369 template <class Impl>
371 DefaultCommit<Impl>::doSwitchOut()
374 switchPending = false;
378 template <class Impl>
380 DefaultCommit<Impl>::takeOverFrom()
384 _nextStatus = Inactive;
385 for (int i=0; i < numThreads; i++) {
386 commitStatus[i] = Idle;
387 changedROBNumEntries[i] = false;
388 trapSquash[i] = false;
395 template <class Impl>
397 DefaultCommit<Impl>::updateStatus()
399 // reset ROB changed variable
400 list<unsigned>::iterator threads = (*activeThreads).begin();
401 while (threads != (*activeThreads).end()) {
402 unsigned tid = *threads++;
403 changedROBNumEntries[tid] = false;
405 // Also check if any of the threads has a trap pending
406 if (commitStatus[tid] == TrapPending ||
407 commitStatus[tid] == FetchTrapPending) {
408 _nextStatus = Active;
412 if (_nextStatus == Inactive && _status == Active) {
413 DPRINTF(Activity, "Deactivating stage.\n");
414 cpu->deactivateStage(FullCPU::CommitIdx);
415 } else if (_nextStatus == Active && _status == Inactive) {
416 DPRINTF(Activity, "Activating stage.\n");
417 cpu->activateStage(FullCPU::CommitIdx);
420 _status = _nextStatus;
423 template <class Impl>
425 DefaultCommit<Impl>::setNextStatus()
429 list<unsigned>::iterator threads = (*activeThreads).begin();
431 while (threads != (*activeThreads).end()) {
432 unsigned tid = *threads++;
434 if (commitStatus[tid] == ROBSquashing) {
439 assert(squashes == squashCounter);
441 // If commit is currently squashing, then it will have activity for the
442 // next cycle. Set its next status as active.
444 _nextStatus = Active;
448 template <class Impl>
450 DefaultCommit<Impl>::changedROBEntries()
452 list<unsigned>::iterator threads = (*activeThreads).begin();
454 while (threads != (*activeThreads).end()) {
455 unsigned tid = *threads++;
457 if (changedROBNumEntries[tid]) {
465 template <class Impl>
467 DefaultCommit<Impl>::numROBFreeEntries(unsigned tid)
469 return rob->numFreeEntries(tid);
472 template <class Impl>
474 DefaultCommit<Impl>::generateTrapEvent(unsigned tid)
476 DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
478 TrapEvent *trap = new TrapEvent(this, tid);
480 trap->schedule(curTick + trapLatency);
482 thread[tid]->trapPending = true;
485 template <class Impl>
487 DefaultCommit<Impl>::generateXCEvent(unsigned tid)
489 DPRINTF(Commit, "Generating XC squash event for [tid:%i]\n", tid);
491 xcSquash[tid] = true;
494 template <class Impl>
496 DefaultCommit<Impl>::squashAll(unsigned tid)
498 // If we want to include the squashing instruction in the squash,
499 // then use one older sequence number.
500 // Hopefully this doesn't mess things up. Basically I want to squash
501 // all instructions of this thread.
502 InstSeqNum squashed_inst = rob->isEmpty() ?
503 0 : rob->readHeadInst(tid)->seqNum - 1;;
505 // All younger instructions will be squashed. Set the sequence
506 // number as the youngest instruction in the ROB (0 in this case.
507 // Hopefully nothing breaks.)
508 youngestSeqNum[tid] = 0;
510 rob->squash(squashed_inst, tid);
511 changedROBNumEntries[tid] = true;
513 // Send back the sequence number of the squashed instruction.
514 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
516 // Send back the squash signal to tell stages that they should
518 toIEW->commitInfo[tid].squash = true;
520 // Send back the rob squashing signal so other stages know that
521 // the ROB is in the process of squashing.
522 toIEW->commitInfo[tid].robSquashing = true;
524 toIEW->commitInfo[tid].branchMispredict = false;
526 toIEW->commitInfo[tid].nextPC = PC[tid];
529 template <class Impl>
531 DefaultCommit<Impl>::squashFromTrap(unsigned tid)
535 DPRINTF(Commit, "Squashing from trap, restarting at PC %#x\n", PC[tid]);
537 thread[tid]->trapPending = false;
538 thread[tid]->inSyscall = false;
540 trapSquash[tid] = false;
542 commitStatus[tid] = ROBSquashing;
543 cpu->activityThisCycle();
548 template <class Impl>
550 DefaultCommit<Impl>::squashFromXC(unsigned tid)
554 DPRINTF(Commit, "Squashing from XC, restarting at PC %#x\n", PC[tid]);
556 thread[tid]->inSyscall = false;
557 assert(!thread[tid]->trapPending);
559 commitStatus[tid] = ROBSquashing;
560 cpu->activityThisCycle();
562 xcSquash[tid] = false;
567 template <class Impl>
569 DefaultCommit<Impl>::tick()
571 wroteToTimeBuffer = false;
572 _nextStatus = Inactive;
574 if (switchPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
575 cpu->signalSwitched();
579 list<unsigned>::iterator threads = (*activeThreads).begin();
581 // Check if any of the threads are done squashing. Change the
582 // status if they are done.
583 while (threads != (*activeThreads).end()) {
584 unsigned tid = *threads++;
586 if (commitStatus[tid] == ROBSquashing) {
588 if (rob->isDoneSquashing(tid)) {
589 commitStatus[tid] = Running;
592 DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
593 "insts this cycle.\n", tid);
600 markCompletedInsts();
602 threads = (*activeThreads).begin();
604 while (threads != (*activeThreads).end()) {
605 unsigned tid = *threads++;
607 if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
608 // The ROB has more instructions it can commit. Its next status
610 _nextStatus = Active;
612 DynInstPtr inst = rob->readHeadInst(tid);
614 DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %#x is head of"
615 " ROB and ready to commit\n",
616 tid, inst->seqNum, inst->readPC());
618 } else if (!rob->isEmpty(tid)) {
619 DynInstPtr inst = rob->readHeadInst(tid);
621 DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
622 "%#x is head of ROB and not ready\n",
623 tid, inst->seqNum, inst->readPC());
626 DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
627 tid, rob->countInsts(tid), rob->numFreeEntries(tid));
631 if (wroteToTimeBuffer) {
632 DPRINTF(Activity, "Activity This Cycle.\n");
633 cpu->activityThisCycle();
639 template <class Impl>
641 DefaultCommit<Impl>::commit()
644 //////////////////////////////////////
645 // Check for interrupts
646 //////////////////////////////////////
649 // Process interrupts if interrupts are enabled, not in PAL mode,
650 // and no other traps or external squashes are currently pending.
651 // @todo: Allow other threads to handle interrupts.
652 if (cpu->checkInterrupts &&
653 cpu->check_interrupts() &&
654 !cpu->inPalMode(readPC()) &&
657 // Tell fetch that there is an interrupt pending. This will
658 // make fetch wait until it sees a non PAL-mode PC, at which
659 // point it stops fetching instructions.
660 toIEW->commitInfo[0].interruptPending = true;
662 // Wait until the ROB is empty and all stores have drained in
663 // order to enter the interrupt.
664 if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
665 // Not sure which thread should be the one to interrupt. For now
666 // always do thread 0.
667 assert(!thread[0]->inSyscall);
668 thread[0]->inSyscall = true;
670 // CPU will handle implementation of the interrupt.
671 cpu->processInterrupts();
673 // Now squash or record that I need to squash this cycle.
674 commitStatus[0] = TrapPending;
676 // Exit state update mode to avoid accidental updating.
677 thread[0]->inSyscall = false;
679 // Generate trap squash event.
680 generateTrapEvent(0);
682 toIEW->commitInfo[0].clearInterrupt = true;
684 DPRINTF(Commit, "Interrupt detected.\n");
686 DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
689 #endif // FULL_SYSTEM
691 ////////////////////////////////////
692 // Check for any possible squashes, handle them first
693 ////////////////////////////////////
695 list<unsigned>::iterator threads = (*activeThreads).begin();
697 while (threads != (*activeThreads).end()) {
698 unsigned tid = *threads++;
700 if (fromFetch->fetchFault && commitStatus[0] != TrapPending) {
701 // Record the fault. Wait until it's empty in the ROB.
702 // Then handle the trap. Ignore it if there's already a
703 // trap pending as fetch will be redirected.
704 fetchFault = fromFetch->fetchFault;
705 fetchFaultTick = curTick + fetchTrapLatency;
706 commitStatus[0] = FetchTrapPending;
707 DPRINTF(Commit, "Fault from fetch recorded. Will trap if the "
708 "ROB empties without squashing the fault.\n");
712 // Fetch may tell commit to clear the trap if it's been squashed.
713 if (fromFetch->clearFetchFault) {
714 DPRINTF(Commit, "Received clear fetch fault signal\n");
716 if (commitStatus[0] == FetchTrapPending) {
717 DPRINTF(Commit, "Clearing fault from fetch\n");
718 commitStatus[0] = Running;
722 // Not sure which one takes priority. I think if we have
723 // both, that's a bad sign.
724 if (trapSquash[tid] == true) {
725 assert(!xcSquash[tid]);
727 } else if (xcSquash[tid] == true) {
731 // Squashed sequence number must be older than youngest valid
732 // instruction in the ROB. This prevents squashes from younger
733 // instructions overriding squashes from older instructions.
734 if (fromIEW->squash[tid] &&
735 commitStatus[tid] != TrapPending &&
736 fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
738 DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
740 fromIEW->mispredPC[tid],
741 fromIEW->squashedSeqNum[tid]);
743 DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
745 fromIEW->nextPC[tid]);
747 commitStatus[tid] = ROBSquashing;
751 // If we want to include the squashing instruction in the squash,
752 // then use one older sequence number.
753 InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
755 if (fromIEW->includeSquashInst[tid] == true)
758 // All younger instructions will be squashed. Set the sequence
759 // number as the youngest instruction in the ROB.
760 youngestSeqNum[tid] = squashed_inst;
762 rob->squash(squashed_inst, tid);
763 changedROBNumEntries[tid] = true;
765 toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
767 toIEW->commitInfo[tid].squash = true;
769 // Send back the rob squashing signal so other stages know that
770 // the ROB is in the process of squashing.
771 toIEW->commitInfo[tid].robSquashing = true;
773 toIEW->commitInfo[tid].branchMispredict =
774 fromIEW->branchMispredict[tid];
776 toIEW->commitInfo[tid].branchTaken =
777 fromIEW->branchTaken[tid];
779 toIEW->commitInfo[tid].nextPC = fromIEW->nextPC[tid];
781 toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
783 if (toIEW->commitInfo[tid].branchMispredict) {
792 if (squashCounter != numThreads) {
793 // If we're not currently squashing, then get instructions.
796 // Try to commit any instructions.
800 //Check for any activity
801 threads = (*activeThreads).begin();
803 while (threads != (*activeThreads).end()) {
804 unsigned tid = *threads++;
806 if (changedROBNumEntries[tid]) {
807 toIEW->commitInfo[tid].usedROB = true;
808 toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
810 if (rob->isEmpty(tid)) {
811 toIEW->commitInfo[tid].emptyROB = true;
814 wroteToTimeBuffer = true;
815 changedROBNumEntries[tid] = false;
820 template <class Impl>
822 DefaultCommit<Impl>::commitInsts()
824 ////////////////////////////////////
826 // Note that commit will be handled prior to putting new
827 // instructions in the ROB so that the ROB only tries to commit
828 // instructions it has in this current cycle, and not instructions
829 // it is writing in during this cycle. Can't commit and squash
830 // things at the same time...
831 ////////////////////////////////////
833 DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
835 unsigned num_committed = 0;
837 DynInstPtr head_inst;
839 // Not the best way to check if the front end is empty, but it should
841 // @todo: Try to avoid directly accessing fetch.
842 if (commitStatus[0] == FetchTrapPending && rob->isEmpty()) {
843 DPRINTF(Commit, "Fault from fetch is pending.\n");
846 if (fetchTrapWait > 10000000) {
847 panic("Fetch trap has been pending for a long time!");
849 if (fetchFaultTick > curTick) {
850 DPRINTF(Commit, "Not enough cycles since fault, fault will "
853 cpu->activityThisCycle();
855 } else if (iewStage->hasStoresToWB()) {
856 DPRINTF(Commit, "IEW still has stores to WB. Waiting until "
857 "they are completed. fetchTrapWait:%i\n",
859 cpu->activityThisCycle();
861 } else if (cpu->inPalMode(readPC())) {
862 DPRINTF(Commit, "In pal mode right now. fetchTrapWait:%i\n",
865 } else if (fetchStage->getYoungestSN() > youngestSeqNum[0]) {
866 DPRINTF(Commit, "Waiting for front end to drain. fetchTrapWait:%i\n",
871 DPRINTF(Commit, "ROB is empty, handling fetch trap.\n");
873 assert(!thread[0]->inSyscall);
875 thread[0]->inSyscall = true;
877 // Consider holding onto the trap and waiting until the trap event
878 // happens for this to be executed.
879 cpu->trap(fetchFault, 0);
881 // Exit state update mode to avoid accidental updating.
882 thread[0]->inSyscall = false;
884 commitStatus[0] = TrapPending;
885 // Set it up so that we squash next cycle
886 trapSquash[0] = true;
891 // Commit as many instructions as possible until the commit bandwidth
892 // limit is reached, or it becomes impossible to commit any more.
893 while (num_committed < commitWidth) {
894 int commit_thread = getCommittingThread();
896 if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
899 head_inst = rob->readHeadInst(commit_thread);
901 int tid = head_inst->threadNumber;
903 assert(tid == commit_thread);
905 DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
906 head_inst->seqNum, tid);
908 // If the head instruction is squashed, it is ready to retire
909 // (be removed from the ROB) at any time.
910 if (head_inst->isSquashed()) {
912 DPRINTF(Commit, "Retiring squashed instruction from "
915 rob->retireHead(commit_thread);
917 ++commitSquashedInsts;
919 // Record that the number of ROB entries has changed.
920 changedROBNumEntries[tid] = true;
922 PC[tid] = head_inst->readPC();
923 nextPC[tid] = head_inst->readNextPC();
925 // Increment the total number of non-speculative instructions
927 // Hack for now: it really shouldn't happen until after the
928 // commit is deemed to be successful, but this count is needed
930 thread[tid]->funcExeInst++;
932 // Try to commit the head instruction.
933 bool commit_success = commitHead(head_inst, num_committed);
935 if (commit_success) {
938 changedROBNumEntries[tid] = true;
940 // Set the doneSeqNum to the youngest committed instruction.
941 toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
943 ++commitCommittedInsts;
945 // To match the old model, don't count nops and instruction
946 // prefetches towards the total commit count.
947 if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
951 PC[tid] = nextPC[tid];
952 nextPC[tid] = nextPC[tid] + sizeof(TheISA::MachInst);
957 // Debug statement. Checks to make sure we're not
958 // currently updating state while handling PC events.
960 assert(!thread[tid]->inSyscall &&
961 !thread[tid]->trapPending);
963 cpu->system->pcEventQueue.service(
964 thread[tid]->getXCProxy());
966 } while (oldpc != PC[tid]);
968 DPRINTF(Commit, "PC skip function event, stopping commit\n");
973 DPRINTF(Commit, "Unable to commit head instruction PC:%#x "
974 "[tid:%i] [sn:%i].\n",
975 head_inst->readPC(), tid ,head_inst->seqNum);
981 DPRINTF(CommitRate, "%i\n", num_committed);
982 numCommittedDist.sample(num_committed);
984 if (num_committed == commitWidth) {
989 template <class Impl>
991 DefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
995 int tid = head_inst->threadNumber;
997 // If the instruction is not executed yet, then it will need extra
998 // handling. Signal backwards that it should be executed.
999 if (!head_inst->isExecuted()) {
1000 // Keep this number correct. We have not yet actually executed
1001 // and committed this instruction.
1002 thread[tid]->funcExeInst--;
1004 head_inst->reachedCommit = true;
1006 if (head_inst->isNonSpeculative() ||
1007 head_inst->isMemBarrier() ||
1008 head_inst->isWriteBarrier()) {
1010 DPRINTF(Commit, "Encountered a barrier or non-speculative "
1011 "instruction [sn:%lli] at the head of the ROB, PC %#x.\n",
1012 head_inst->seqNum, head_inst->readPC());
1015 // Hack to make sure syscalls/memory barriers/quiesces
1016 // aren't executed until all stores write back their data.
1017 // This direct communication shouldn't be used for
1018 // anything other than this.
1019 if (inst_num > 0 || iewStage->hasStoresToWB())
1021 if ((head_inst->isMemBarrier() || head_inst->isWriteBarrier() ||
1022 head_inst->isQuiesce()) &&
1023 iewStage->hasStoresToWB())
1026 DPRINTF(Commit, "Waiting for all stores to writeback.\n");
1030 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1032 // Change the instruction so it won't try to commit again until
1034 head_inst->clearCanCommit();
1036 ++commitNonSpecStalls;
1039 } else if (head_inst->isLoad()) {
1040 DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %#x.\n",
1041 head_inst->seqNum, head_inst->readPC());
1043 // Send back the non-speculative instruction's sequence
1044 // number. Tell the lsq to re-execute the load.
1045 toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1046 toIEW->commitInfo[tid].uncached = true;
1047 toIEW->commitInfo[tid].uncachedLoad = head_inst;
1049 head_inst->clearCanCommit();
1053 panic("Trying to commit un-executed instruction "
1054 "of unknown type!\n");
1058 if (head_inst->isThreadSync()) {
1059 // Not handled for now.
1060 panic("Thread sync instructions are not handled yet.\n");
1063 // Stores mark themselves as completed.
1064 if (!head_inst->isStore()) {
1065 head_inst->setCompleted();
1068 // Use checker prior to updating anything due to traps or PC
1071 cpu->checker->tick(head_inst);
1074 // Check if the instruction caused a fault. If so, trap.
1075 Fault inst_fault = head_inst->getFault();
1077 if (inst_fault != NoFault) {
1078 head_inst->setCompleted();
1080 DPRINTF(Commit, "Inst [sn:%lli] PC %#x has a fault\n",
1081 head_inst->seqNum, head_inst->readPC());
1083 if (iewStage->hasStoresToWB() || inst_num > 0) {
1084 DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
1088 if (cpu->checker && head_inst->isStore()) {
1089 cpu->checker->tick(head_inst);
1092 assert(!thread[tid]->inSyscall);
1094 // Mark that we're in state update mode so that the trap's
1095 // execution doesn't generate extra squashes.
1096 thread[tid]->inSyscall = true;
1098 // DTB will sometimes need the machine instruction for when
1099 // faults happen. So we will set it here, prior to the DTB
1100 // possibly needing it for its fault.
1101 thread[tid]->setInst(
1102 static_cast<TheISA::MachInst>(head_inst->staticInst->machInst));
1104 // Execute the trap. Although it's slightly unrealistic in
1105 // terms of timing (as it doesn't wait for the full timing of
1106 // the trap event to complete before updating state), it's
1107 // needed to update the state as soon as possible. This
1108 // prevents external agents from changing any specific state
1109 // that the trap need.
1110 cpu->trap(inst_fault, tid);
1112 // Exit state update mode to avoid accidental updating.
1113 thread[tid]->inSyscall = false;
1115 commitStatus[tid] = TrapPending;
1117 // Generate trap squash event.
1118 generateTrapEvent(tid);
1121 #else // !FULL_SYSTEM
1122 panic("fault (%d) detected @ PC %08p", inst_fault,
1124 #endif // FULL_SYSTEM
1127 updateComInstStats(head_inst);
1129 if (head_inst->traceData) {
1130 head_inst->traceData->setFetchSeq(head_inst->seqNum);
1131 head_inst->traceData->setCPSeq(thread[tid]->numInst);
1132 head_inst->traceData->finalize();
1133 head_inst->traceData = NULL;
1136 // Update the commit rename map
1137 for (int i = 0; i < head_inst->numDestRegs(); i++) {
1138 renameMap[tid]->setEntry(head_inst->destRegIdx(i),
1139 head_inst->renamedDestRegIdx(i));
1142 // Finally clear the head ROB entry.
1143 rob->retireHead(tid);
1145 // Return true to indicate that we have committed an instruction.
1149 template <class Impl>
1151 DefaultCommit<Impl>::getInsts()
1153 // Read any renamed instructions and place them into the ROB.
1154 int insts_to_process = min((int)renameWidth, fromRename->size);
1156 for (int inst_num = 0; inst_num < insts_to_process; ++inst_num)
1158 DynInstPtr inst = fromRename->insts[inst_num];
1159 int tid = inst->threadNumber;
1161 if (!inst->isSquashed() &&
1162 commitStatus[tid] != ROBSquashing) {
1163 changedROBNumEntries[tid] = true;
1165 DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ROB.\n",
1166 inst->readPC(), inst->seqNum, tid);
1168 rob->insertInst(inst);
1170 assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
1172 youngestSeqNum[tid] = inst->seqNum;
1174 DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1175 "squashed, skipping.\n",
1176 inst->readPC(), inst->seqNum, tid);
1181 template <class Impl>
1183 DefaultCommit<Impl>::markCompletedInsts()
1185 // Grab completed insts out of the IEW instruction queue, and mark
1186 // instructions completed within the ROB.
1187 for (int inst_num = 0;
1188 inst_num < fromIEW->size && fromIEW->insts[inst_num];
1191 if (!fromIEW->insts[inst_num]->isSquashed()) {
1192 DPRINTF(Commit, "[tid:%i]: Marking PC %#x, [sn:%lli] ready "
1194 fromIEW->insts[inst_num]->threadNumber,
1195 fromIEW->insts[inst_num]->readPC(),
1196 fromIEW->insts[inst_num]->seqNum);
1198 // Mark the instruction as ready to commit.
1199 fromIEW->insts[inst_num]->setCanCommit();
1204 template <class Impl>
1206 DefaultCommit<Impl>::robDoneSquashing()
1208 list<unsigned>::iterator threads = (*activeThreads).begin();
1210 while (threads != (*activeThreads).end()) {
1211 unsigned tid = *threads++;
1213 if (!rob->isDoneSquashing(tid))
1220 template <class Impl>
1222 DefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
1224 unsigned thread = inst->threadNumber;
1227 // Pick off the software prefetches
1230 if (inst->isDataPrefetch()) {
1231 statComSwp[thread]++;
1233 statComInst[thread]++;
1236 statComInst[thread]++;
1240 // Control Instructions
1242 if (inst->isControl())
1243 statComBranches[thread]++;
1246 // Memory references
1248 if (inst->isMemRef()) {
1249 statComRefs[thread]++;
1251 if (inst->isLoad()) {
1252 statComLoads[thread]++;
1256 if (inst->isMemBarrier()) {
1257 statComMembars[thread]++;
1261 ////////////////////////////////////////
1263 // SMT COMMIT POLICY MAINTAINED HERE //
1265 ////////////////////////////////////////
1266 template <class Impl>
1268 DefaultCommit<Impl>::getCommittingThread()
1270 if (numThreads > 1) {
1271 switch (commitPolicy) {
1274 //If Policy is Aggressive, commit will call
1275 //this function multiple times per
1277 return oldestReady();
1280 return roundRobin();
1283 return oldestReady();
1289 int tid = (*activeThreads).front();
1291 if (commitStatus[tid] == Running ||
1292 commitStatus[tid] == Idle ||
1293 commitStatus[tid] == FetchTrapPending) {
1301 template<class Impl>
1303 DefaultCommit<Impl>::roundRobin()
1305 list<unsigned>::iterator pri_iter = priority_list.begin();
1306 list<unsigned>::iterator end = priority_list.end();
1308 while (pri_iter != end) {
1309 unsigned tid = *pri_iter;
1311 if (commitStatus[tid] == Running ||
1312 commitStatus[tid] == Idle) {
1314 if (rob->isHeadReady(tid)) {
1315 priority_list.erase(pri_iter);
1316 priority_list.push_back(tid);
1328 template<class Impl>
1330 DefaultCommit<Impl>::oldestReady()
1332 unsigned oldest = 0;
1335 list<unsigned>::iterator threads = (*activeThreads).begin();
1337 while (threads != (*activeThreads).end()) {
1338 unsigned tid = *threads++;
1340 if (!rob->isEmpty(tid) &&
1341 (commitStatus[tid] == Running ||
1342 commitStatus[tid] == Idle ||
1343 commitStatus[tid] == FetchTrapPending)) {
1345 if (rob->isHeadReady(tid)) {
1347 DynInstPtr head_inst = rob->readHeadInst(tid);
1352 } else if (head_inst->seqNum < oldest) {