commit_impl.hh revision 3126:756092c6383c
17087Snate@binkert.org/*
27087Snate@binkert.org * Copyright (c) 2004-2006 The Regents of The University of Michigan
37087Snate@binkert.org * All rights reserved.
47087Snate@binkert.org *
57087Snate@binkert.org * Redistribution and use in source and binary forms, with or without
67087Snate@binkert.org * modification, are permitted provided that the following conditions are
77087Snate@binkert.org * met: redistributions of source code must retain the above copyright
87087Snate@binkert.org * notice, this list of conditions and the following disclaimer;
97087Snate@binkert.org * redistributions in binary form must reproduce the above copyright
107087Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
117087Snate@binkert.org * documentation and/or other materials provided with the distribution;
127087Snate@binkert.org * neither the name of the copyright holders nor the names of its
135326Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
145326Sgblack@eecs.umich.edu * this software without specific prior written permission.
155326Sgblack@eecs.umich.edu *
165326Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175326Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185326Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195326Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205326Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215326Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225326Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235326Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245326Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255326Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265326Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275326Sgblack@eecs.umich.edu *
285326Sgblack@eecs.umich.edu * Authors: Kevin Lim
295326Sgblack@eecs.umich.edu *          Korey Sewell
305326Sgblack@eecs.umich.edu */
315326Sgblack@eecs.umich.edu
325326Sgblack@eecs.umich.edu#include "config/full_system.hh"
335326Sgblack@eecs.umich.edu#include "config/use_checker.hh"
345326Sgblack@eecs.umich.edu
355326Sgblack@eecs.umich.edu#include <algorithm>
365326Sgblack@eecs.umich.edu#include <string>
375326Sgblack@eecs.umich.edu
385326Sgblack@eecs.umich.edu#include "base/loader/symtab.hh"
395326Sgblack@eecs.umich.edu#include "base/timebuf.hh"
405326Sgblack@eecs.umich.edu#include "cpu/exetrace.hh"
415240Sgblack@eecs.umich.edu#include "cpu/o3/commit.hh"
425240Sgblack@eecs.umich.edu#include "cpu/o3/thread_state.hh"
435240Sgblack@eecs.umich.edu
445240Sgblack@eecs.umich.edu#if USE_CHECKER
455240Sgblack@eecs.umich.edu#include "cpu/checker/cpu.hh"
465240Sgblack@eecs.umich.edu#endif
475306Sgblack@eecs.umich.edu
485240Sgblack@eecs.umich.edutemplate <class Impl>
495240Sgblack@eecs.umich.eduDefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
505240Sgblack@eecs.umich.edu                                          unsigned _tid)
515326Sgblack@eecs.umich.edu    : Event(&mainEventQueue, CPU_Tick_Pri), commit(_commit), tid(_tid)
525240Sgblack@eecs.umich.edu{
535240Sgblack@eecs.umich.edu    this->setFlags(Event::AutoDelete);
545240Sgblack@eecs.umich.edu}
555240Sgblack@eecs.umich.edu
565240Sgblack@eecs.umich.edutemplate <class Impl>
575306Sgblack@eecs.umich.eduvoid
585326Sgblack@eecs.umich.eduDefaultCommit<Impl>::TrapEvent::process()
595240Sgblack@eecs.umich.edu{
605240Sgblack@eecs.umich.edu    // This will get reset by commit if it was switched out at the
615240Sgblack@eecs.umich.edu    // time of this event processing.
625240Sgblack@eecs.umich.edu    commit->trapSquash[tid] = true;
635240Sgblack@eecs.umich.edu}
645240Sgblack@eecs.umich.edu
655240Sgblack@eecs.umich.edutemplate <class Impl>
665240Sgblack@eecs.umich.educonst char *
675326Sgblack@eecs.umich.eduDefaultCommit<Impl>::TrapEvent::description()
685326Sgblack@eecs.umich.edu{
695326Sgblack@eecs.umich.edu    return "Trap event";
705326Sgblack@eecs.umich.edu}
715240Sgblack@eecs.umich.edu
725240Sgblack@eecs.umich.edutemplate <class Impl>
735240Sgblack@eecs.umich.eduDefaultCommit<Impl>::DefaultCommit(Params *params)
745240Sgblack@eecs.umich.edu    : squashCounter(0),
755240Sgblack@eecs.umich.edu      iewToCommitDelay(params->iewToCommitDelay),
765326Sgblack@eecs.umich.edu      commitToIEWDelay(params->commitToIEWDelay),
775326Sgblack@eecs.umich.edu      renameToROBDelay(params->renameToROBDelay),
785326Sgblack@eecs.umich.edu      fetchToCommitDelay(params->commitToFetchDelay),
795326Sgblack@eecs.umich.edu      renameWidth(params->renameWidth),
805240Sgblack@eecs.umich.edu      commitWidth(params->commitWidth),
815240Sgblack@eecs.umich.edu      numThreads(params->numberOfThreads),
825240Sgblack@eecs.umich.edu      drainPending(false),
835240Sgblack@eecs.umich.edu      switchedOut(false),
845240Sgblack@eecs.umich.edu      trapLatency(params->trapLatency)
855240Sgblack@eecs.umich.edu{
865240Sgblack@eecs.umich.edu    _status = Active;
875240Sgblack@eecs.umich.edu    _nextStatus = Inactive;
885240Sgblack@eecs.umich.edu    std::string policy = params->smtCommitPolicy;
895240Sgblack@eecs.umich.edu
905240Sgblack@eecs.umich.edu    //Convert string to lowercase
915306Sgblack@eecs.umich.edu    std::transform(policy.begin(), policy.end(), policy.begin(),
925240Sgblack@eecs.umich.edu                   (int(*)(int)) tolower);
935240Sgblack@eecs.umich.edu
945240Sgblack@eecs.umich.edu    //Assign commit policy
955326Sgblack@eecs.umich.edu    if (policy == "aggressive"){
965326Sgblack@eecs.umich.edu        commitPolicy = Aggressive;
975326Sgblack@eecs.umich.edu
985240Sgblack@eecs.umich.edu        DPRINTF(Commit,"Commit Policy set to Aggressive.");
995326Sgblack@eecs.umich.edu    } else if (policy == "roundrobin"){
1005326Sgblack@eecs.umich.edu        commitPolicy = RoundRobin;
1015240Sgblack@eecs.umich.edu
1025240Sgblack@eecs.umich.edu        //Set-Up Priority List
1035240Sgblack@eecs.umich.edu        for (int tid=0; tid < numThreads; tid++) {
1045306Sgblack@eecs.umich.edu            priority_list.push_back(tid);
1055306Sgblack@eecs.umich.edu        }
1065326Sgblack@eecs.umich.edu
1075326Sgblack@eecs.umich.edu        DPRINTF(Commit,"Commit Policy set to Round Robin.");
1085326Sgblack@eecs.umich.edu    } else if (policy == "oldestready"){
1095240Sgblack@eecs.umich.edu        commitPolicy = OldestReady;
1105326Sgblack@eecs.umich.edu
1115326Sgblack@eecs.umich.edu        DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
1125240Sgblack@eecs.umich.edu    } else {
1135240Sgblack@eecs.umich.edu        assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
1146096Sgblack@eecs.umich.edu               "RoundRobin,OldestReady}");
1156096Sgblack@eecs.umich.edu    }
1166096Sgblack@eecs.umich.edu
1176096Sgblack@eecs.umich.edu    for (int i=0; i < numThreads; i++) {
1186096Sgblack@eecs.umich.edu        commitStatus[i] = Idle;
1196096Sgblack@eecs.umich.edu        changedROBNumEntries[i] = false;
1206096Sgblack@eecs.umich.edu        trapSquash[i] = false;
1216096Sgblack@eecs.umich.edu        tcSquash[i] = false;
1226096Sgblack@eecs.umich.edu        PC[i] = nextPC[i] = nextNPC[i] = 0;
1236096Sgblack@eecs.umich.edu    }
1246096Sgblack@eecs.umich.edu}
1256096Sgblack@eecs.umich.edu
1266096Sgblack@eecs.umich.edutemplate <class Impl>
1276096Sgblack@eecs.umich.edustd::string
1286096Sgblack@eecs.umich.eduDefaultCommit<Impl>::name() const
1296096Sgblack@eecs.umich.edu{
1306096Sgblack@eecs.umich.edu    return cpu->name() + ".commit";
1316096Sgblack@eecs.umich.edu}
1326096Sgblack@eecs.umich.edu
1336096Sgblack@eecs.umich.edutemplate <class Impl>
1346096Sgblack@eecs.umich.eduvoid
1355240Sgblack@eecs.umich.eduDefaultCommit<Impl>::regStats()
1365240Sgblack@eecs.umich.edu{
1375240Sgblack@eecs.umich.edu    using namespace Stats;
1385240Sgblack@eecs.umich.edu    commitCommittedInsts
1395240Sgblack@eecs.umich.edu        .name(name() + ".commitCommittedInsts")
1405240Sgblack@eecs.umich.edu        .desc("The number of committed instructions")
1415240Sgblack@eecs.umich.edu        .prereq(commitCommittedInsts);
1425240Sgblack@eecs.umich.edu    commitSquashedInsts
1435326Sgblack@eecs.umich.edu        .name(name() + ".commitSquashedInsts")
1445326Sgblack@eecs.umich.edu        .desc("The number of squashed insts skipped by commit")
1455326Sgblack@eecs.umich.edu        .prereq(commitSquashedInsts);
1465326Sgblack@eecs.umich.edu    commitSquashEvents
1475326Sgblack@eecs.umich.edu        .name(name() + ".commitSquashEvents")
1485326Sgblack@eecs.umich.edu        .desc("The number of times commit is told to squash")
1495240Sgblack@eecs.umich.edu        .prereq(commitSquashEvents);
1505326Sgblack@eecs.umich.edu    commitNonSpecStalls
1515326Sgblack@eecs.umich.edu        .name(name() + ".commitNonSpecStalls")
1525240Sgblack@eecs.umich.edu        .desc("The number of times commit has been forced to stall to "
1535240Sgblack@eecs.umich.edu              "communicate backwards")
1545240Sgblack@eecs.umich.edu        .prereq(commitNonSpecStalls);
1555306Sgblack@eecs.umich.edu    branchMispredicts
1565326Sgblack@eecs.umich.edu        .name(name() + ".branchMispredicts")
1575326Sgblack@eecs.umich.edu        .desc("The number of times a branch was mispredicted")
1585326Sgblack@eecs.umich.edu        .prereq(branchMispredicts);
1595326Sgblack@eecs.umich.edu    numCommittedDist
1605326Sgblack@eecs.umich.edu        .init(0,commitWidth,1)
1615326Sgblack@eecs.umich.edu        .name(name() + ".COM:committed_per_cycle")
1625240Sgblack@eecs.umich.edu        .desc("Number of insts commited each cycle")
1635326Sgblack@eecs.umich.edu        .flags(Stats::pdf)
1645297Sgblack@eecs.umich.edu        ;
1655240Sgblack@eecs.umich.edu
1665240Sgblack@eecs.umich.edu    statComInst
1676096Sgblack@eecs.umich.edu        .init(cpu->number_of_threads)
1686096Sgblack@eecs.umich.edu        .name(name() + ".COM:count")
1696096Sgblack@eecs.umich.edu        .desc("Number of instructions committed")
1706096Sgblack@eecs.umich.edu        .flags(total)
1716096Sgblack@eecs.umich.edu        ;
1726096Sgblack@eecs.umich.edu
1736096Sgblack@eecs.umich.edu    statComSwp
1746096Sgblack@eecs.umich.edu        .init(cpu->number_of_threads)
1756096Sgblack@eecs.umich.edu        .name(name() + ".COM:swp_count")
1766096Sgblack@eecs.umich.edu        .desc("Number of s/w prefetches committed")
1776096Sgblack@eecs.umich.edu        .flags(total)
1786096Sgblack@eecs.umich.edu        ;
1796096Sgblack@eecs.umich.edu
1806096Sgblack@eecs.umich.edu    statComRefs
1816096Sgblack@eecs.umich.edu        .init(cpu->number_of_threads)
1826096Sgblack@eecs.umich.edu        .name(name() +  ".COM:refs")
1836096Sgblack@eecs.umich.edu        .desc("Number of memory references committed")
1846096Sgblack@eecs.umich.edu        .flags(total)
1856096Sgblack@eecs.umich.edu        ;
1866096Sgblack@eecs.umich.edu
1876096Sgblack@eecs.umich.edu    statComLoads
1886096Sgblack@eecs.umich.edu        .init(cpu->number_of_threads)
1896096Sgblack@eecs.umich.edu        .name(name() +  ".COM:loads")
1906096Sgblack@eecs.umich.edu        .desc("Number of loads committed")
1916096Sgblack@eecs.umich.edu        .flags(total)
1925240Sgblack@eecs.umich.edu        ;
1935240Sgblack@eecs.umich.edu
1945240Sgblack@eecs.umich.edu    statComMembars
1955240Sgblack@eecs.umich.edu        .init(cpu->number_of_threads)
1965240Sgblack@eecs.umich.edu        .name(name() +  ".COM:membars")
1975240Sgblack@eecs.umich.edu        .desc("Number of memory barriers committed")
1985240Sgblack@eecs.umich.edu        .flags(total)
1995240Sgblack@eecs.umich.edu        ;
2005306Sgblack@eecs.umich.edu
2015326Sgblack@eecs.umich.edu    statComBranches
2025326Sgblack@eecs.umich.edu        .init(cpu->number_of_threads)
2035326Sgblack@eecs.umich.edu        .name(name() + ".COM:branches")
2045240Sgblack@eecs.umich.edu        .desc("Number of branches committed")
2055326Sgblack@eecs.umich.edu        .flags(total)
2065326Sgblack@eecs.umich.edu        ;
2075240Sgblack@eecs.umich.edu
2085240Sgblack@eecs.umich.edu    commitEligible
2095240Sgblack@eecs.umich.edu        .init(cpu->number_of_threads)
2105306Sgblack@eecs.umich.edu        .name(name() + ".COM:bw_limited")
2115306Sgblack@eecs.umich.edu        .desc("number of insts not committed due to BW limits")
2125326Sgblack@eecs.umich.edu        .flags(total)
2135326Sgblack@eecs.umich.edu        ;
2145326Sgblack@eecs.umich.edu
2155240Sgblack@eecs.umich.edu    commitEligibleSamples
2165326Sgblack@eecs.umich.edu        .name(name() + ".COM:bw_lim_events")
2175326Sgblack@eecs.umich.edu        .desc("number cycles where commit BW limit reached")
2185240Sgblack@eecs.umich.edu        ;
2195240Sgblack@eecs.umich.edu}
2206095Sgblack@eecs.umich.edu
2216095Sgblack@eecs.umich.edutemplate <class Impl>
2226095Sgblack@eecs.umich.eduvoid
2236095Sgblack@eecs.umich.eduDefaultCommit<Impl>::setCPU(O3CPU *cpu_ptr)
2246095Sgblack@eecs.umich.edu{
2256095Sgblack@eecs.umich.edu    DPRINTF(Commit, "Commit: Setting CPU pointer.\n");
2266095Sgblack@eecs.umich.edu    cpu = cpu_ptr;
2276095Sgblack@eecs.umich.edu
2286095Sgblack@eecs.umich.edu    // Commit must broadcast the number of free entries it has at the start of
2296095Sgblack@eecs.umich.edu    // the simulation, so it starts as active.
2306095Sgblack@eecs.umich.edu    cpu->activateStage(O3CPU::CommitIdx);
2316095Sgblack@eecs.umich.edu
2326095Sgblack@eecs.umich.edu    trapLatency = cpu->cycles(trapLatency);
2336095Sgblack@eecs.umich.edu}
2346095Sgblack@eecs.umich.edu
2356095Sgblack@eecs.umich.edutemplate <class Impl>
2366095Sgblack@eecs.umich.eduvoid
2376095Sgblack@eecs.umich.eduDefaultCommit<Impl>::setThreads(std::vector<Thread *> &threads)
2386095Sgblack@eecs.umich.edu{
2396095Sgblack@eecs.umich.edu    thread = threads;
2406095Sgblack@eecs.umich.edu}
2415240Sgblack@eecs.umich.edu
2425240Sgblack@eecs.umich.edutemplate <class Impl>
2435240Sgblack@eecs.umich.eduvoid
2445240Sgblack@eecs.umich.eduDefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
2455240Sgblack@eecs.umich.edu{
2465240Sgblack@eecs.umich.edu    DPRINTF(Commit, "Commit: Setting time buffer pointer.\n");
2475240Sgblack@eecs.umich.edu    timeBuffer = tb_ptr;
2485240Sgblack@eecs.umich.edu
2495326Sgblack@eecs.umich.edu    // Setup wire to send information back to IEW.
2505326Sgblack@eecs.umich.edu    toIEW = timeBuffer->getWire(0);
2515326Sgblack@eecs.umich.edu
2525326Sgblack@eecs.umich.edu    // Setup wire to read data from IEW (for the ROB).
2535326Sgblack@eecs.umich.edu    robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
2545326Sgblack@eecs.umich.edu}
2555240Sgblack@eecs.umich.edu
2565326Sgblack@eecs.umich.edutemplate <class Impl>
2575326Sgblack@eecs.umich.eduvoid
2585240Sgblack@eecs.umich.eduDefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
2595240Sgblack@eecs.umich.edu{
2605240Sgblack@eecs.umich.edu    DPRINTF(Commit, "Commit: Setting fetch queue pointer.\n");
2615306Sgblack@eecs.umich.edu    fetchQueue = fq_ptr;
2625326Sgblack@eecs.umich.edu
2635326Sgblack@eecs.umich.edu    // Setup wire to get instructions from rename (for the ROB).
2645326Sgblack@eecs.umich.edu    fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
2655326Sgblack@eecs.umich.edu}
2665326Sgblack@eecs.umich.edu
2675326Sgblack@eecs.umich.edutemplate <class Impl>
2685240Sgblack@eecs.umich.eduvoid
2695326Sgblack@eecs.umich.eduDefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
2705326Sgblack@eecs.umich.edu{
2715240Sgblack@eecs.umich.edu    DPRINTF(Commit, "Commit: Setting rename queue pointer.\n");
2725240Sgblack@eecs.umich.edu    renameQueue = rq_ptr;
2736095Sgblack@eecs.umich.edu
2746095Sgblack@eecs.umich.edu    // Setup wire to get instructions from rename (for the ROB).
2756095Sgblack@eecs.umich.edu    fromRename = renameQueue->getWire(-renameToROBDelay);
2766095Sgblack@eecs.umich.edu}
2776095Sgblack@eecs.umich.edu
2786095Sgblack@eecs.umich.edutemplate <class Impl>
2796095Sgblack@eecs.umich.eduvoid
2806095Sgblack@eecs.umich.eduDefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
2816095Sgblack@eecs.umich.edu{
2826095Sgblack@eecs.umich.edu    DPRINTF(Commit, "Commit: Setting IEW queue pointer.\n");
2836095Sgblack@eecs.umich.edu    iewQueue = iq_ptr;
2846095Sgblack@eecs.umich.edu
2856095Sgblack@eecs.umich.edu    // Setup wire to get instructions from IEW.
2866095Sgblack@eecs.umich.edu    fromIEW = iewQueue->getWire(-iewToCommitDelay);
2876095Sgblack@eecs.umich.edu}
2886095Sgblack@eecs.umich.edu
2896095Sgblack@eecs.umich.edutemplate <class Impl>
2906095Sgblack@eecs.umich.eduvoid
2916095Sgblack@eecs.umich.eduDefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
2926095Sgblack@eecs.umich.edu{
2936095Sgblack@eecs.umich.edu    iewStage = iew_stage;
2946095Sgblack@eecs.umich.edu}
2956095Sgblack@eecs.umich.edu
2966095Sgblack@eecs.umich.edutemplate<class Impl>
2976095Sgblack@eecs.umich.eduvoid
2985240Sgblack@eecs.umich.eduDefaultCommit<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
2995240Sgblack@eecs.umich.edu{
3005240Sgblack@eecs.umich.edu    DPRINTF(Commit, "Commit: Setting active threads list pointer.\n");
3015240Sgblack@eecs.umich.edu    activeThreads = at_ptr;
3025240Sgblack@eecs.umich.edu}
3035240Sgblack@eecs.umich.edu
3045240Sgblack@eecs.umich.edutemplate <class Impl>
3055240Sgblack@eecs.umich.eduvoid
3065306Sgblack@eecs.umich.eduDefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
3075326Sgblack@eecs.umich.edu{
3085326Sgblack@eecs.umich.edu    DPRINTF(Commit, "Setting rename map pointers.\n");
3095326Sgblack@eecs.umich.edu
3105240Sgblack@eecs.umich.edu    for (int i=0; i < numThreads; i++) {
3115326Sgblack@eecs.umich.edu        renameMap[i] = &rm_ptr[i];
3125326Sgblack@eecs.umich.edu    }
3135240Sgblack@eecs.umich.edu}
3145240Sgblack@eecs.umich.edu
3155240Sgblack@eecs.umich.edutemplate <class Impl>
3165306Sgblack@eecs.umich.eduvoid
3175306Sgblack@eecs.umich.eduDefaultCommit<Impl>::setROB(ROB *rob_ptr)
3185326Sgblack@eecs.umich.edu{
3195326Sgblack@eecs.umich.edu    DPRINTF(Commit, "Commit: Setting ROB pointer.\n");
3205326Sgblack@eecs.umich.edu    rob = rob_ptr;
3215240Sgblack@eecs.umich.edu}
3225326Sgblack@eecs.umich.edu
3235326Sgblack@eecs.umich.edutemplate <class Impl>
3245240Sgblack@eecs.umich.eduvoid
3255240Sgblack@eecs.umich.eduDefaultCommit<Impl>::initStage()
3266093Sgblack@eecs.umich.edu{
3276093Sgblack@eecs.umich.edu    rob->setActiveThreads(activeThreads);
3286093Sgblack@eecs.umich.edu    rob->resetEntries();
3296093Sgblack@eecs.umich.edu
3306093Sgblack@eecs.umich.edu    // Broadcast the number of free entries.
3316093Sgblack@eecs.umich.edu    for (int i=0; i < numThreads; i++) {
3326093Sgblack@eecs.umich.edu        toIEW->commitInfo[i].usedROB = true;
3336093Sgblack@eecs.umich.edu        toIEW->commitInfo[i].freeROBEntries = rob->numFreeEntries(i);
3346093Sgblack@eecs.umich.edu    }
3356093Sgblack@eecs.umich.edu
3366093Sgblack@eecs.umich.edu    cpu->activityThisCycle();
3376093Sgblack@eecs.umich.edu}
3386093Sgblack@eecs.umich.edu
3396093Sgblack@eecs.umich.edutemplate <class Impl>
3406093Sgblack@eecs.umich.edubool
3416093Sgblack@eecs.umich.eduDefaultCommit<Impl>::drain()
3426093Sgblack@eecs.umich.edu{
3436093Sgblack@eecs.umich.edu    drainPending = true;
3446093Sgblack@eecs.umich.edu
3456093Sgblack@eecs.umich.edu    // If it's already drained, return true.
3466093Sgblack@eecs.umich.edu    if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
3475240Sgblack@eecs.umich.edu        cpu->signalDrained();
3485240Sgblack@eecs.umich.edu        return true;
3495240Sgblack@eecs.umich.edu    }
3505240Sgblack@eecs.umich.edu
3515240Sgblack@eecs.umich.edu    return false;
3525240Sgblack@eecs.umich.edu}
3535240Sgblack@eecs.umich.edu
3545240Sgblack@eecs.umich.edutemplate <class Impl>
3555326Sgblack@eecs.umich.eduvoid
3565326Sgblack@eecs.umich.eduDefaultCommit<Impl>::switchOut()
3575326Sgblack@eecs.umich.edu{
3585326Sgblack@eecs.umich.edu    switchedOut = true;
3595326Sgblack@eecs.umich.edu    drainPending = false;
3605326Sgblack@eecs.umich.edu    rob->switchOut();
3615240Sgblack@eecs.umich.edu}
3625326Sgblack@eecs.umich.edu
3635326Sgblack@eecs.umich.edutemplate <class Impl>
3645240Sgblack@eecs.umich.eduvoid
3655240Sgblack@eecs.umich.eduDefaultCommit<Impl>::resume()
3665240Sgblack@eecs.umich.edu{
3675306Sgblack@eecs.umich.edu    drainPending = false;
3685326Sgblack@eecs.umich.edu}
3695326Sgblack@eecs.umich.edu
3705326Sgblack@eecs.umich.edutemplate <class Impl>
3715326Sgblack@eecs.umich.eduvoid
3725326Sgblack@eecs.umich.eduDefaultCommit<Impl>::takeOverFrom()
3735326Sgblack@eecs.umich.edu{
3745240Sgblack@eecs.umich.edu    switchedOut = false;
3755326Sgblack@eecs.umich.edu    _status = Active;
3765326Sgblack@eecs.umich.edu    _nextStatus = Inactive;
3775240Sgblack@eecs.umich.edu    for (int i=0; i < numThreads; i++) {
3786093Sgblack@eecs.umich.edu        commitStatus[i] = Idle;
3796093Sgblack@eecs.umich.edu        changedROBNumEntries[i] = false;
3806093Sgblack@eecs.umich.edu        trapSquash[i] = false;
3816093Sgblack@eecs.umich.edu        tcSquash[i] = false;
3826093Sgblack@eecs.umich.edu    }
3836093Sgblack@eecs.umich.edu    squashCounter = 0;
3846093Sgblack@eecs.umich.edu    rob->takeOverFrom();
3856093Sgblack@eecs.umich.edu}
3866093Sgblack@eecs.umich.edu
3876093Sgblack@eecs.umich.edutemplate <class Impl>
3886093Sgblack@eecs.umich.eduvoid
3896093Sgblack@eecs.umich.eduDefaultCommit<Impl>::updateStatus()
3906093Sgblack@eecs.umich.edu{
3916093Sgblack@eecs.umich.edu    // reset ROB changed variable
3926093Sgblack@eecs.umich.edu    std::list<unsigned>::iterator threads = (*activeThreads).begin();
3936093Sgblack@eecs.umich.edu    while (threads != (*activeThreads).end()) {
3946093Sgblack@eecs.umich.edu        unsigned tid = *threads++;
3956093Sgblack@eecs.umich.edu        changedROBNumEntries[tid] = false;
3966093Sgblack@eecs.umich.edu
3976093Sgblack@eecs.umich.edu        // Also check if any of the threads has a trap pending
3986093Sgblack@eecs.umich.edu        if (commitStatus[tid] == TrapPending ||
3996093Sgblack@eecs.umich.edu            commitStatus[tid] == FetchTrapPending) {
4006093Sgblack@eecs.umich.edu            _nextStatus = Active;
4016093Sgblack@eecs.umich.edu        }
4026093Sgblack@eecs.umich.edu    }
4035240Sgblack@eecs.umich.edu
404    if (_nextStatus == Inactive && _status == Active) {
405        DPRINTF(Activity, "Deactivating stage.\n");
406        cpu->deactivateStage(O3CPU::CommitIdx);
407    } else if (_nextStatus == Active && _status == Inactive) {
408        DPRINTF(Activity, "Activating stage.\n");
409        cpu->activateStage(O3CPU::CommitIdx);
410    }
411
412    _status = _nextStatus;
413}
414
415template <class Impl>
416void
417DefaultCommit<Impl>::setNextStatus()
418{
419    int squashes = 0;
420
421    std::list<unsigned>::iterator threads = (*activeThreads).begin();
422
423    while (threads != (*activeThreads).end()) {
424        unsigned tid = *threads++;
425
426        if (commitStatus[tid] == ROBSquashing) {
427            squashes++;
428        }
429    }
430
431    squashCounter = squashes;
432
433    // If commit is currently squashing, then it will have activity for the
434    // next cycle. Set its next status as active.
435    if (squashCounter) {
436        _nextStatus = Active;
437    }
438}
439
440template <class Impl>
441bool
442DefaultCommit<Impl>::changedROBEntries()
443{
444    std::list<unsigned>::iterator threads = (*activeThreads).begin();
445
446    while (threads != (*activeThreads).end()) {
447        unsigned tid = *threads++;
448
449        if (changedROBNumEntries[tid]) {
450            return true;
451        }
452    }
453
454    return false;
455}
456
457template <class Impl>
458unsigned
459DefaultCommit<Impl>::numROBFreeEntries(unsigned tid)
460{
461    return rob->numFreeEntries(tid);
462}
463
464template <class Impl>
465void
466DefaultCommit<Impl>::generateTrapEvent(unsigned tid)
467{
468    DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
469
470    TrapEvent *trap = new TrapEvent(this, tid);
471
472    trap->schedule(curTick + trapLatency);
473
474    thread[tid]->trapPending = true;
475}
476
477template <class Impl>
478void
479DefaultCommit<Impl>::generateTCEvent(unsigned tid)
480{
481    DPRINTF(Commit, "Generating TC squash event for [tid:%i]\n", tid);
482
483    tcSquash[tid] = true;
484}
485
486template <class Impl>
487void
488DefaultCommit<Impl>::squashAll(unsigned tid)
489{
490    // If we want to include the squashing instruction in the squash,
491    // then use one older sequence number.
492    // Hopefully this doesn't mess things up.  Basically I want to squash
493    // all instructions of this thread.
494    InstSeqNum squashed_inst = rob->isEmpty() ?
495        0 : rob->readHeadInst(tid)->seqNum - 1;;
496
497    // All younger instructions will be squashed. Set the sequence
498    // number as the youngest instruction in the ROB (0 in this case.
499    // Hopefully nothing breaks.)
500    youngestSeqNum[tid] = 0;
501
502    rob->squash(squashed_inst, tid);
503    changedROBNumEntries[tid] = true;
504
505    // Send back the sequence number of the squashed instruction.
506    toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
507
508    // Send back the squash signal to tell stages that they should
509    // squash.
510    toIEW->commitInfo[tid].squash = true;
511
512    // Send back the rob squashing signal so other stages know that
513    // the ROB is in the process of squashing.
514    toIEW->commitInfo[tid].robSquashing = true;
515
516    toIEW->commitInfo[tid].branchMispredict = false;
517
518    toIEW->commitInfo[tid].nextPC = PC[tid];
519}
520
521template <class Impl>
522void
523DefaultCommit<Impl>::squashFromTrap(unsigned tid)
524{
525    squashAll(tid);
526
527    DPRINTF(Commit, "Squashing from trap, restarting at PC %#x\n", PC[tid]);
528
529    thread[tid]->trapPending = false;
530    thread[tid]->inSyscall = false;
531
532    trapSquash[tid] = false;
533
534    commitStatus[tid] = ROBSquashing;
535    cpu->activityThisCycle();
536}
537
538template <class Impl>
539void
540DefaultCommit<Impl>::squashFromTC(unsigned tid)
541{
542    squashAll(tid);
543
544    DPRINTF(Commit, "Squashing from TC, restarting at PC %#x\n", PC[tid]);
545
546    thread[tid]->inSyscall = false;
547    assert(!thread[tid]->trapPending);
548
549    commitStatus[tid] = ROBSquashing;
550    cpu->activityThisCycle();
551
552    tcSquash[tid] = false;
553}
554
555template <class Impl>
556void
557DefaultCommit<Impl>::tick()
558{
559    wroteToTimeBuffer = false;
560    _nextStatus = Inactive;
561
562    if (drainPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
563        cpu->signalDrained();
564        drainPending = false;
565        return;
566    }
567
568    if ((*activeThreads).size() <= 0)
569        return;
570
571    std::list<unsigned>::iterator threads = (*activeThreads).begin();
572
573    // Check if any of the threads are done squashing.  Change the
574    // status if they are done.
575    while (threads != (*activeThreads).end()) {
576        unsigned tid = *threads++;
577
578        if (commitStatus[tid] == ROBSquashing) {
579
580            if (rob->isDoneSquashing(tid)) {
581                commitStatus[tid] = Running;
582            } else {
583                DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
584                        " insts this cycle.\n", tid);
585                rob->doSquash(tid);
586                toIEW->commitInfo[tid].robSquashing = true;
587                wroteToTimeBuffer = true;
588            }
589        }
590    }
591
592    commit();
593
594    markCompletedInsts();
595
596    threads = (*activeThreads).begin();
597
598    while (threads != (*activeThreads).end()) {
599        unsigned tid = *threads++;
600
601        if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
602            // The ROB has more instructions it can commit. Its next status
603            // will be active.
604            _nextStatus = Active;
605
606            DynInstPtr inst = rob->readHeadInst(tid);
607
608            DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %#x is head of"
609                    " ROB and ready to commit\n",
610                    tid, inst->seqNum, inst->readPC());
611
612        } else if (!rob->isEmpty(tid)) {
613            DynInstPtr inst = rob->readHeadInst(tid);
614
615            DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
616                    "%#x is head of ROB and not ready\n",
617                    tid, inst->seqNum, inst->readPC());
618        }
619
620        DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
621                tid, rob->countInsts(tid), rob->numFreeEntries(tid));
622    }
623
624
625    if (wroteToTimeBuffer) {
626        DPRINTF(Activity, "Activity This Cycle.\n");
627        cpu->activityThisCycle();
628    }
629
630    updateStatus();
631}
632
633template <class Impl>
634void
635DefaultCommit<Impl>::commit()
636{
637
638    //////////////////////////////////////
639    // Check for interrupts
640    //////////////////////////////////////
641
642#if FULL_SYSTEM
643    // Process interrupts if interrupts are enabled, not in PAL mode,
644    // and no other traps or external squashes are currently pending.
645    // @todo: Allow other threads to handle interrupts.
646    if (cpu->checkInterrupts &&
647        cpu->check_interrupts() &&
648        !cpu->inPalMode(readPC()) &&
649        !trapSquash[0] &&
650        !tcSquash[0]) {
651        // Tell fetch that there is an interrupt pending.  This will
652        // make fetch wait until it sees a non PAL-mode PC, at which
653        // point it stops fetching instructions.
654        toIEW->commitInfo[0].interruptPending = true;
655
656        // Wait until the ROB is empty and all stores have drained in
657        // order to enter the interrupt.
658        if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
659            // Not sure which thread should be the one to interrupt.  For now
660            // always do thread 0.
661            assert(!thread[0]->inSyscall);
662            thread[0]->inSyscall = true;
663
664            // CPU will handle implementation of the interrupt.
665            cpu->processInterrupts();
666
667            // Now squash or record that I need to squash this cycle.
668            commitStatus[0] = TrapPending;
669
670            // Exit state update mode to avoid accidental updating.
671            thread[0]->inSyscall = false;
672
673            // Generate trap squash event.
674            generateTrapEvent(0);
675
676            toIEW->commitInfo[0].clearInterrupt = true;
677
678            DPRINTF(Commit, "Interrupt detected.\n");
679        } else {
680            DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
681        }
682    }
683#endif // FULL_SYSTEM
684
685    ////////////////////////////////////
686    // Check for any possible squashes, handle them first
687    ////////////////////////////////////
688
689    std::list<unsigned>::iterator threads = (*activeThreads).begin();
690
691    while (threads != (*activeThreads).end()) {
692        unsigned tid = *threads++;
693
694        // Not sure which one takes priority.  I think if we have
695        // both, that's a bad sign.
696        if (trapSquash[tid] == true) {
697            assert(!tcSquash[tid]);
698            squashFromTrap(tid);
699        } else if (tcSquash[tid] == true) {
700            squashFromTC(tid);
701        }
702
703        // Squashed sequence number must be older than youngest valid
704        // instruction in the ROB. This prevents squashes from younger
705        // instructions overriding squashes from older instructions.
706        if (fromIEW->squash[tid] &&
707            commitStatus[tid] != TrapPending &&
708            fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
709
710            DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
711                    tid,
712                    fromIEW->mispredPC[tid],
713                    fromIEW->squashedSeqNum[tid]);
714
715            DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
716                    tid,
717                    fromIEW->nextPC[tid]);
718
719            commitStatus[tid] = ROBSquashing;
720
721            // If we want to include the squashing instruction in the squash,
722            // then use one older sequence number.
723            InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
724
725#if ISA_HAS_DELAY_SLOT
726            InstSeqNum bdelay_done_seq_num;
727            bool squash_bdelay_slot;
728
729            if (fromIEW->branchMispredict[tid]) {
730                if (fromIEW->branchTaken[tid] &&
731                    fromIEW->condDelaySlotBranch[tid]) {
732                    DPRINTF(Commit, "[tid:%i]: Cond. delay slot branch"
733                            "mispredicted as taken. Squashing after previous "
734                            "inst, [sn:%i]\n",
735                            tid, squashed_inst);
736                     bdelay_done_seq_num = squashed_inst;
737                     squash_bdelay_slot = true;
738                } else {
739                    DPRINTF(Commit, "[tid:%i]: Branch Mispredict. Squashing "
740                            "after delay slot [sn:%i]\n", tid, squashed_inst+1);
741                    bdelay_done_seq_num = squashed_inst + 1;
742                    squash_bdelay_slot = false;
743                }
744            } else {
745                bdelay_done_seq_num = squashed_inst;
746            }
747#endif
748
749            if (fromIEW->includeSquashInst[tid] == true) {
750                squashed_inst--;
751#if ISA_HAS_DELAY_SLOT
752                bdelay_done_seq_num--;
753#endif
754            }
755            // All younger instructions will be squashed. Set the sequence
756            // number as the youngest instruction in the ROB.
757            youngestSeqNum[tid] = squashed_inst;
758
759#if ISA_HAS_DELAY_SLOT
760            rob->squash(bdelay_done_seq_num, tid);
761            toIEW->commitInfo[tid].squashDelaySlot = squash_bdelay_slot;
762            toIEW->commitInfo[tid].bdelayDoneSeqNum = bdelay_done_seq_num;
763#else
764            rob->squash(squashed_inst, tid);
765            toIEW->commitInfo[tid].squashDelaySlot = true;
766#endif
767            changedROBNumEntries[tid] = true;
768
769            toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
770
771            toIEW->commitInfo[tid].squash = true;
772
773            // Send back the rob squashing signal so other stages know that
774            // the ROB is in the process of squashing.
775            toIEW->commitInfo[tid].robSquashing = true;
776
777            toIEW->commitInfo[tid].branchMispredict =
778                fromIEW->branchMispredict[tid];
779
780            toIEW->commitInfo[tid].branchTaken =
781                fromIEW->branchTaken[tid];
782
783            toIEW->commitInfo[tid].nextPC = fromIEW->nextPC[tid];
784
785            toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
786
787            if (toIEW->commitInfo[tid].branchMispredict) {
788                ++branchMispredicts;
789            }
790        }
791
792    }
793
794    setNextStatus();
795
796    if (squashCounter != numThreads) {
797        // If we're not currently squashing, then get instructions.
798        getInsts();
799
800        // Try to commit any instructions.
801        commitInsts();
802    } else {
803#if ISA_HAS_DELAY_SLOT
804        skidInsert();
805#endif
806    }
807
808    //Check for any activity
809    threads = (*activeThreads).begin();
810
811    while (threads != (*activeThreads).end()) {
812        unsigned tid = *threads++;
813
814        if (changedROBNumEntries[tid]) {
815            toIEW->commitInfo[tid].usedROB = true;
816            toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
817
818            if (rob->isEmpty(tid)) {
819                toIEW->commitInfo[tid].emptyROB = true;
820            }
821
822            wroteToTimeBuffer = true;
823            changedROBNumEntries[tid] = false;
824        }
825    }
826}
827
828template <class Impl>
829void
830DefaultCommit<Impl>::commitInsts()
831{
832    ////////////////////////////////////
833    // Handle commit
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    ////////////////////////////////////
840
841    DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
842
843    unsigned num_committed = 0;
844
845    DynInstPtr head_inst;
846
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();
851
852        if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
853            break;
854
855        head_inst = rob->readHeadInst(commit_thread);
856
857        int tid = head_inst->threadNumber;
858
859        assert(tid == commit_thread);
860
861        DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
862                head_inst->seqNum, tid);
863
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()) {
867
868            DPRINTF(Commit, "Retiring squashed instruction from "
869                    "ROB.\n");
870
871            rob->retireHead(commit_thread);
872
873            ++commitSquashedInsts;
874
875            // Record that the number of ROB entries has changed.
876            changedROBNumEntries[tid] = true;
877        } else {
878            PC[tid] = head_inst->readPC();
879            nextPC[tid] = head_inst->readNextPC();
880            nextNPC[tid] = head_inst->readNextNPC();
881
882            // Increment the total number of non-speculative instructions
883            // executed.
884            // Hack for now: it really shouldn't happen until after the
885            // commit is deemed to be successful, but this count is needed
886            // for syscalls.
887            thread[tid]->funcExeInst++;
888
889            // Try to commit the head instruction.
890            bool commit_success = commitHead(head_inst, num_committed);
891
892            if (commit_success) {
893                ++num_committed;
894
895                changedROBNumEntries[tid] = true;
896
897                // Set the doneSeqNum to the youngest committed instruction.
898                toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
899
900                ++commitCommittedInsts;
901
902                // To match the old model, don't count nops and instruction
903                // prefetches towards the total commit count.
904                if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
905                    cpu->instDone(tid);
906                }
907
908                PC[tid] = nextPC[tid];
909#if ISA_HAS_DELAY_SLOT
910                nextPC[tid] = nextNPC[tid];
911                nextNPC[tid] = nextNPC[tid] + sizeof(TheISA::MachInst);
912#else
913                nextPC[tid] = nextPC[tid] + sizeof(TheISA::MachInst);
914#endif
915
916#if FULL_SYSTEM
917                int count = 0;
918                Addr oldpc;
919                do {
920                    // Debug statement.  Checks to make sure we're not
921                    // currently updating state while handling PC events.
922                    if (count == 0)
923                        assert(!thread[tid]->inSyscall &&
924                               !thread[tid]->trapPending);
925                    oldpc = PC[tid];
926                    cpu->system->pcEventQueue.service(
927                        thread[tid]->getTC());
928                    count++;
929                } while (oldpc != PC[tid]);
930                if (count > 1) {
931                    DPRINTF(Commit, "PC skip function event, stopping commit\n");
932                    break;
933                }
934#endif
935            } else {
936                DPRINTF(Commit, "Unable to commit head instruction PC:%#x "
937                        "[tid:%i] [sn:%i].\n",
938                        head_inst->readPC(), tid ,head_inst->seqNum);
939                break;
940            }
941        }
942    }
943
944    DPRINTF(CommitRate, "%i\n", num_committed);
945    numCommittedDist.sample(num_committed);
946
947    if (num_committed == commitWidth) {
948        commitEligibleSamples++;
949    }
950}
951
952template <class Impl>
953bool
954DefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
955{
956    assert(head_inst);
957
958    int tid = head_inst->threadNumber;
959
960    // If the instruction is not executed yet, then it will need extra
961    // handling.  Signal backwards that it should be executed.
962    if (!head_inst->isExecuted()) {
963        // Keep this number correct.  We have not yet actually executed
964        // and committed this instruction.
965        thread[tid]->funcExeInst--;
966
967        head_inst->setAtCommit();
968
969        if (head_inst->isNonSpeculative() ||
970            head_inst->isStoreConditional() ||
971            head_inst->isMemBarrier() ||
972            head_inst->isWriteBarrier()) {
973
974            DPRINTF(Commit, "Encountered a barrier or non-speculative "
975                    "instruction [sn:%lli] at the head of the ROB, PC %#x.\n",
976                    head_inst->seqNum, head_inst->readPC());
977
978#if !FULL_SYSTEM
979            // Hack to make sure syscalls/memory barriers/quiesces
980            // aren't executed until all stores write back their data.
981            // This direct communication shouldn't be used for
982            // anything other than this.
983            if (inst_num > 0 || iewStage->hasStoresToWB())
984#else
985            if ((head_inst->isMemBarrier() || head_inst->isWriteBarrier() ||
986                    head_inst->isQuiesce()) &&
987                iewStage->hasStoresToWB())
988#endif
989            {
990                DPRINTF(Commit, "Waiting for all stores to writeback.\n");
991                return false;
992            }
993
994            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
995
996            // Change the instruction so it won't try to commit again until
997            // it is executed.
998            head_inst->clearCanCommit();
999
1000            ++commitNonSpecStalls;
1001
1002            return false;
1003        } else if (head_inst->isLoad()) {
1004            DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %#x.\n",
1005                    head_inst->seqNum, head_inst->readPC());
1006
1007            // Send back the non-speculative instruction's sequence
1008            // number.  Tell the lsq to re-execute the load.
1009            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
1010            toIEW->commitInfo[tid].uncached = true;
1011            toIEW->commitInfo[tid].uncachedLoad = head_inst;
1012
1013            head_inst->clearCanCommit();
1014
1015            return false;
1016        } else {
1017            panic("Trying to commit un-executed instruction "
1018                  "of unknown type!\n");
1019        }
1020    }
1021
1022    if (head_inst->isThreadSync()) {
1023        // Not handled for now.
1024        panic("Thread sync instructions are not handled yet.\n");
1025    }
1026
1027    // Stores mark themselves as completed.
1028    if (!head_inst->isStore()) {
1029        head_inst->setCompleted();
1030    }
1031
1032#if USE_CHECKER
1033    // Use checker prior to updating anything due to traps or PC
1034    // based events.
1035    if (cpu->checker) {
1036        cpu->checker->verify(head_inst);
1037    }
1038#endif
1039
1040    // Check if the instruction caused a fault.  If so, trap.
1041    Fault inst_fault = head_inst->getFault();
1042
1043    // DTB will sometimes need the machine instruction for when
1044    // faults happen.  So we will set it here, prior to the DTB
1045    // possibly needing it for its fault.
1046    thread[tid]->setInst(
1047        static_cast<TheISA::MachInst>(head_inst->staticInst->machInst));
1048
1049    if (inst_fault != NoFault) {
1050        head_inst->setCompleted();
1051        DPRINTF(Commit, "Inst [sn:%lli] PC %#x has a fault\n",
1052                head_inst->seqNum, head_inst->readPC());
1053
1054        if (iewStage->hasStoresToWB() || inst_num > 0) {
1055            DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
1056            return false;
1057        }
1058
1059#if USE_CHECKER
1060        if (cpu->checker && head_inst->isStore()) {
1061            cpu->checker->verify(head_inst);
1062        }
1063#endif
1064
1065        assert(!thread[tid]->inSyscall);
1066
1067        // Mark that we're in state update mode so that the trap's
1068        // execution doesn't generate extra squashes.
1069        thread[tid]->inSyscall = true;
1070
1071        // Execute the trap.  Although it's slightly unrealistic in
1072        // terms of timing (as it doesn't wait for the full timing of
1073        // the trap event to complete before updating state), it's
1074        // needed to update the state as soon as possible.  This
1075        // prevents external agents from changing any specific state
1076        // that the trap need.
1077        cpu->trap(inst_fault, tid);
1078
1079        // Exit state update mode to avoid accidental updating.
1080        thread[tid]->inSyscall = false;
1081
1082        commitStatus[tid] = TrapPending;
1083
1084        // Generate trap squash event.
1085        generateTrapEvent(tid);
1086//        warn("%lli fault (%d) handled @ PC %08p", curTick, inst_fault->name(), head_inst->readPC());
1087        return false;
1088    }
1089
1090    updateComInstStats(head_inst);
1091
1092#if FULL_SYSTEM
1093    if (thread[tid]->profile) {
1094//        bool usermode =
1095//            (cpu->readMiscReg(AlphaISA::IPR_DTB_CM, tid) & 0x18) != 0;
1096//        thread[tid]->profilePC = usermode ? 1 : head_inst->readPC();
1097        thread[tid]->profilePC = head_inst->readPC();
1098        ProfileNode *node = thread[tid]->profile->consume(thread[tid]->getTC(),
1099                                                          head_inst->staticInst);
1100
1101        if (node)
1102            thread[tid]->profileNode = node;
1103    }
1104#endif
1105
1106    if (head_inst->traceData) {
1107        head_inst->traceData->setFetchSeq(head_inst->seqNum);
1108        head_inst->traceData->setCPSeq(thread[tid]->numInst);
1109        head_inst->traceData->finalize();
1110        head_inst->traceData = NULL;
1111    }
1112
1113    // Update the commit rename map
1114    for (int i = 0; i < head_inst->numDestRegs(); i++) {
1115        renameMap[tid]->setEntry(head_inst->destRegIdx(i),
1116                                 head_inst->renamedDestRegIdx(i));
1117    }
1118
1119    if (head_inst->isCopy())
1120        panic("Should not commit any copy instructions!");
1121
1122    // Finally clear the head ROB entry.
1123    rob->retireHead(tid);
1124
1125    // Return true to indicate that we have committed an instruction.
1126    return true;
1127}
1128
1129template <class Impl>
1130void
1131DefaultCommit<Impl>::getInsts()
1132{
1133    DPRINTF(Commit, "Getting instructions from Rename stage.\n");
1134
1135#if ISA_HAS_DELAY_SLOT
1136    // Read any renamed instructions and place them into the ROB.
1137    int insts_to_process = std::min((int)renameWidth,
1138                               (int)(fromRename->size + skidBuffer.size()));
1139    int rename_idx = 0;
1140
1141    DPRINTF(Commit, "%i insts available to process. Rename Insts:%i "
1142            "SkidBuffer Insts:%i\n", insts_to_process, fromRename->size,
1143            skidBuffer.size());
1144#else
1145    // Read any renamed instructions and place them into the ROB.
1146    int insts_to_process = std::min((int)renameWidth, fromRename->size);
1147#endif
1148
1149
1150    for (int inst_num = 0; inst_num < insts_to_process; ++inst_num) {
1151        DynInstPtr inst;
1152
1153#if ISA_HAS_DELAY_SLOT
1154        // Get insts from skidBuffer or from Rename
1155        if (skidBuffer.size() > 0) {
1156            DPRINTF(Commit, "Grabbing skidbuffer inst.\n");
1157            inst = skidBuffer.front();
1158            skidBuffer.pop();
1159        } else {
1160            DPRINTF(Commit, "Grabbing rename inst.\n");
1161            inst = fromRename->insts[rename_idx++];
1162        }
1163#else
1164        inst = fromRename->insts[inst_num];
1165#endif
1166        int tid = inst->threadNumber;
1167
1168        if (!inst->isSquashed() &&
1169            commitStatus[tid] != ROBSquashing) {
1170            changedROBNumEntries[tid] = true;
1171
1172            DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ROB.\n",
1173                    inst->readPC(), inst->seqNum, tid);
1174
1175            rob->insertInst(inst);
1176
1177            assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
1178
1179            youngestSeqNum[tid] = inst->seqNum;
1180        } else {
1181            DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1182                    "squashed, skipping.\n",
1183                    inst->readPC(), inst->seqNum, tid);
1184        }
1185    }
1186
1187#if ISA_HAS_DELAY_SLOT
1188    if (rename_idx < fromRename->size) {
1189        DPRINTF(Commit,"Placing Rename Insts into skidBuffer.\n");
1190
1191        for (;
1192             rename_idx < fromRename->size;
1193             rename_idx++) {
1194            DynInstPtr inst = fromRename->insts[rename_idx];
1195            int tid = inst->threadNumber;
1196
1197            if (!inst->isSquashed()) {
1198                DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ",
1199                        "skidBuffer.\n", inst->readPC(), inst->seqNum, tid);
1200                skidBuffer.push(inst);
1201            } else {
1202                DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1203                        "squashed, skipping.\n",
1204                        inst->readPC(), inst->seqNum, tid);
1205            }
1206        }
1207    }
1208#endif
1209
1210}
1211
1212template <class Impl>
1213void
1214DefaultCommit<Impl>::skidInsert()
1215{
1216    DPRINTF(Commit, "Attempting to any instructions from rename into "
1217            "skidBuffer.\n");
1218
1219    for (int inst_num = 0; inst_num < fromRename->size; ++inst_num) {
1220        DynInstPtr inst = fromRename->insts[inst_num];
1221        int tid = inst->threadNumber;
1222
1223        if (!inst->isSquashed()) {
1224            DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ",
1225                    "skidBuffer.\n", inst->readPC(), inst->seqNum, tid);
1226            skidBuffer.push(inst);
1227        } else {
1228            DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
1229                    "squashed, skipping.\n",
1230                    inst->readPC(), inst->seqNum, tid);
1231        }
1232    }
1233}
1234
1235template <class Impl>
1236void
1237DefaultCommit<Impl>::markCompletedInsts()
1238{
1239    // Grab completed insts out of the IEW instruction queue, and mark
1240    // instructions completed within the ROB.
1241    for (int inst_num = 0;
1242         inst_num < fromIEW->size && fromIEW->insts[inst_num];
1243         ++inst_num)
1244    {
1245        if (!fromIEW->insts[inst_num]->isSquashed()) {
1246            DPRINTF(Commit, "[tid:%i]: Marking PC %#x, [sn:%lli] ready "
1247                    "within ROB.\n",
1248                    fromIEW->insts[inst_num]->threadNumber,
1249                    fromIEW->insts[inst_num]->readPC(),
1250                    fromIEW->insts[inst_num]->seqNum);
1251
1252            // Mark the instruction as ready to commit.
1253            fromIEW->insts[inst_num]->setCanCommit();
1254        }
1255    }
1256}
1257
1258template <class Impl>
1259bool
1260DefaultCommit<Impl>::robDoneSquashing()
1261{
1262    std::list<unsigned>::iterator threads = (*activeThreads).begin();
1263
1264    while (threads != (*activeThreads).end()) {
1265        unsigned tid = *threads++;
1266
1267        if (!rob->isDoneSquashing(tid))
1268            return false;
1269    }
1270
1271    return true;
1272}
1273
1274template <class Impl>
1275void
1276DefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
1277{
1278    unsigned thread = inst->threadNumber;
1279
1280    //
1281    //  Pick off the software prefetches
1282    //
1283#ifdef TARGET_ALPHA
1284    if (inst->isDataPrefetch()) {
1285        statComSwp[thread]++;
1286    } else {
1287        statComInst[thread]++;
1288    }
1289#else
1290    statComInst[thread]++;
1291#endif
1292
1293    //
1294    //  Control Instructions
1295    //
1296    if (inst->isControl())
1297        statComBranches[thread]++;
1298
1299    //
1300    //  Memory references
1301    //
1302    if (inst->isMemRef()) {
1303        statComRefs[thread]++;
1304
1305        if (inst->isLoad()) {
1306            statComLoads[thread]++;
1307        }
1308    }
1309
1310    if (inst->isMemBarrier()) {
1311        statComMembars[thread]++;
1312    }
1313}
1314
1315////////////////////////////////////////
1316//                                    //
1317//  SMT COMMIT POLICY MAINTAINED HERE //
1318//                                    //
1319////////////////////////////////////////
1320template <class Impl>
1321int
1322DefaultCommit<Impl>::getCommittingThread()
1323{
1324    if (numThreads > 1) {
1325        switch (commitPolicy) {
1326
1327          case Aggressive:
1328            //If Policy is Aggressive, commit will call
1329            //this function multiple times per
1330            //cycle
1331            return oldestReady();
1332
1333          case RoundRobin:
1334            return roundRobin();
1335
1336          case OldestReady:
1337            return oldestReady();
1338
1339          default:
1340            return -1;
1341        }
1342    } else {
1343        int tid = (*activeThreads).front();
1344
1345        if (commitStatus[tid] == Running ||
1346            commitStatus[tid] == Idle ||
1347            commitStatus[tid] == FetchTrapPending) {
1348            return tid;
1349        } else {
1350            return -1;
1351        }
1352    }
1353}
1354
1355template<class Impl>
1356int
1357DefaultCommit<Impl>::roundRobin()
1358{
1359    std::list<unsigned>::iterator pri_iter = priority_list.begin();
1360    std::list<unsigned>::iterator end      = priority_list.end();
1361
1362    while (pri_iter != end) {
1363        unsigned tid = *pri_iter;
1364
1365        if (commitStatus[tid] == Running ||
1366            commitStatus[tid] == Idle ||
1367            commitStatus[tid] == FetchTrapPending) {
1368
1369            if (rob->isHeadReady(tid)) {
1370                priority_list.erase(pri_iter);
1371                priority_list.push_back(tid);
1372
1373                return tid;
1374            }
1375        }
1376
1377        pri_iter++;
1378    }
1379
1380    return -1;
1381}
1382
1383template<class Impl>
1384int
1385DefaultCommit<Impl>::oldestReady()
1386{
1387    unsigned oldest = 0;
1388    bool first = true;
1389
1390    std::list<unsigned>::iterator threads = (*activeThreads).begin();
1391
1392    while (threads != (*activeThreads).end()) {
1393        unsigned tid = *threads++;
1394
1395        if (!rob->isEmpty(tid) &&
1396            (commitStatus[tid] == Running ||
1397             commitStatus[tid] == Idle ||
1398             commitStatus[tid] == FetchTrapPending)) {
1399
1400            if (rob->isHeadReady(tid)) {
1401
1402                DynInstPtr head_inst = rob->readHeadInst(tid);
1403
1404                if (first) {
1405                    oldest = tid;
1406                    first = false;
1407                } else if (head_inst->seqNum < oldest) {
1408                    oldest = tid;
1409                }
1410            }
1411        }
1412    }
1413
1414    if (!first) {
1415        return oldest;
1416    } else {
1417        return -1;
1418    }
1419}
1420