commit_impl.hh revision 7851
11689SN/A/*
27783SGiacomo.Gabrielli@arm.com * Copyright (c) 2010 ARM Limited
37783SGiacomo.Gabrielli@arm.com * All rights reserved
47783SGiacomo.Gabrielli@arm.com *
57783SGiacomo.Gabrielli@arm.com * The license below extends only to copyright in the software and shall
67783SGiacomo.Gabrielli@arm.com * not be construed as granting a license to any other intellectual
77783SGiacomo.Gabrielli@arm.com * property including but not limited to intellectual property relating
87783SGiacomo.Gabrielli@arm.com * to a hardware implementation of the functionality of the software
97783SGiacomo.Gabrielli@arm.com * licensed hereunder.  You may use the software subject to the license
107783SGiacomo.Gabrielli@arm.com * terms below provided that you ensure that this notice is replicated
117783SGiacomo.Gabrielli@arm.com * unmodified and in its entirety in all distributions of the software,
127783SGiacomo.Gabrielli@arm.com * modified or unmodified, in source code or in binary form.
137783SGiacomo.Gabrielli@arm.com *
142316SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
151689SN/A * All rights reserved.
161689SN/A *
171689SN/A * Redistribution and use in source and binary forms, with or without
181689SN/A * modification, are permitted provided that the following conditions are
191689SN/A * met: redistributions of source code must retain the above copyright
201689SN/A * notice, this list of conditions and the following disclaimer;
211689SN/A * redistributions in binary form must reproduce the above copyright
221689SN/A * notice, this list of conditions and the following disclaimer in the
231689SN/A * documentation and/or other materials provided with the distribution;
241689SN/A * neither the name of the copyright holders nor the names of its
251689SN/A * contributors may be used to endorse or promote products derived from
261689SN/A * this software without specific prior written permission.
271689SN/A *
281689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
291689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
301689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
311689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
321689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
331689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
341689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
351689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
361689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
371689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
381689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
392665Ssaidi@eecs.umich.edu *
402665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
412965Sksewell@umich.edu *          Korey Sewell
421689SN/A */
431689SN/A
442292SN/A#include <algorithm>
452329SN/A#include <string>
462292SN/A
473577Sgblack@eecs.umich.edu#include "arch/utility.hh"
485953Ssaidi@eecs.umich.edu#include "base/cp_annotate.hh"
492292SN/A#include "base/loader/symtab.hh"
507813Ssteve.reinhardt@amd.com#include "cpu/timebuf.hh"
516221Snate@binkert.org#include "config/full_system.hh"
526658Snate@binkert.org#include "config/the_isa.hh"
536221Snate@binkert.org#include "config/use_checker.hh"
542292SN/A#include "cpu/exetrace.hh"
551717SN/A#include "cpu/o3/commit.hh"
562292SN/A#include "cpu/o3/thread_state.hh"
576221Snate@binkert.org#include "params/DerivO3CPU.hh"
582292SN/A
592790Sktlim@umich.edu#if USE_CHECKER
602790Sktlim@umich.edu#include "cpu/checker/cpu.hh"
612790Sktlim@umich.edu#endif
622790Sktlim@umich.edu
636221Snate@binkert.orgusing namespace std;
645529Snate@binkert.org
651061SN/Atemplate <class Impl>
662292SN/ADefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
676221Snate@binkert.org                                          ThreadID _tid)
685606Snate@binkert.org    : Event(CPU_Tick_Pri), commit(_commit), tid(_tid)
691060SN/A{
705769Snate@binkert.org    this->setFlags(AutoDelete);
711060SN/A}
721060SN/A
731061SN/Atemplate <class Impl>
741060SN/Avoid
752292SN/ADefaultCommit<Impl>::TrapEvent::process()
761062SN/A{
772316SN/A    // This will get reset by commit if it was switched out at the
782316SN/A    // time of this event processing.
792292SN/A    commit->trapSquash[tid] = true;
802292SN/A}
812292SN/A
822292SN/Atemplate <class Impl>
832292SN/Aconst char *
845336Shines@cs.fsu.eduDefaultCommit<Impl>::TrapEvent::description() const
852292SN/A{
864873Sstever@eecs.umich.edu    return "Trap";
872292SN/A}
882292SN/A
892292SN/Atemplate <class Impl>
905529Snate@binkert.orgDefaultCommit<Impl>::DefaultCommit(O3CPU *_cpu, DerivO3CPUParams *params)
914329Sktlim@umich.edu    : cpu(_cpu),
924329Sktlim@umich.edu      squashCounter(0),
932292SN/A      iewToCommitDelay(params->iewToCommitDelay),
942292SN/A      commitToIEWDelay(params->commitToIEWDelay),
952292SN/A      renameToROBDelay(params->renameToROBDelay),
962292SN/A      fetchToCommitDelay(params->commitToFetchDelay),
972292SN/A      renameWidth(params->renameWidth),
982292SN/A      commitWidth(params->commitWidth),
995529Snate@binkert.org      numThreads(params->numThreads),
1002843Sktlim@umich.edu      drainPending(false),
1012316SN/A      switchedOut(false),
1022874Sktlim@umich.edu      trapLatency(params->trapLatency)
1032292SN/A{
1042292SN/A    _status = Active;
1052292SN/A    _nextStatus = Inactive;
1062980Sgblack@eecs.umich.edu    std::string policy = params->smtCommitPolicy;
1072292SN/A
1082292SN/A    //Convert string to lowercase
1092292SN/A    std::transform(policy.begin(), policy.end(), policy.begin(),
1102292SN/A                   (int(*)(int)) tolower);
1112292SN/A
1122292SN/A    //Assign commit policy
1132292SN/A    if (policy == "aggressive"){
1142292SN/A        commitPolicy = Aggressive;
1152292SN/A
1164329Sktlim@umich.edu        DPRINTF(Commit,"Commit Policy set to Aggressive.");
1172292SN/A    } else if (policy == "roundrobin"){
1182292SN/A        commitPolicy = RoundRobin;
1192292SN/A
1202292SN/A        //Set-Up Priority List
1216221Snate@binkert.org        for (ThreadID tid = 0; tid < numThreads; tid++) {
1222292SN/A            priority_list.push_back(tid);
1232292SN/A        }
1242292SN/A
1254329Sktlim@umich.edu        DPRINTF(Commit,"Commit Policy set to Round Robin.");
1262292SN/A    } else if (policy == "oldestready"){
1272292SN/A        commitPolicy = OldestReady;
1282292SN/A
1294329Sktlim@umich.edu        DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
1302292SN/A    } else {
1312292SN/A        assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
1322292SN/A               "RoundRobin,OldestReady}");
1332292SN/A    }
1342292SN/A
1356221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++) {
1366221Snate@binkert.org        commitStatus[tid] = Idle;
1376221Snate@binkert.org        changedROBNumEntries[tid] = false;
1386221Snate@binkert.org        checkEmptyROB[tid] = false;
1396221Snate@binkert.org        trapInFlight[tid] = false;
1406221Snate@binkert.org        committedStores[tid] = false;
1416221Snate@binkert.org        trapSquash[tid] = false;
1426221Snate@binkert.org        tcSquash[tid] = false;
1437720Sgblack@eecs.umich.edu        pc[tid].set(0);
1442292SN/A    }
1453640Sktlim@umich.edu#if FULL_SYSTEM
1463640Sktlim@umich.edu    interrupt = NoFault;
1473640Sktlim@umich.edu#endif
1482292SN/A}
1492292SN/A
1502292SN/Atemplate <class Impl>
1512292SN/Astd::string
1522292SN/ADefaultCommit<Impl>::name() const
1532292SN/A{
1542292SN/A    return cpu->name() + ".commit";
1552292SN/A}
1562292SN/A
1572292SN/Atemplate <class Impl>
1582292SN/Avoid
1592292SN/ADefaultCommit<Impl>::regStats()
1602132SN/A{
1612301SN/A    using namespace Stats;
1621062SN/A    commitCommittedInsts
1631062SN/A        .name(name() + ".commitCommittedInsts")
1641062SN/A        .desc("The number of committed instructions")
1651062SN/A        .prereq(commitCommittedInsts);
1661062SN/A    commitSquashedInsts
1671062SN/A        .name(name() + ".commitSquashedInsts")
1681062SN/A        .desc("The number of squashed insts skipped by commit")
1691062SN/A        .prereq(commitSquashedInsts);
1701062SN/A    commitSquashEvents
1711062SN/A        .name(name() + ".commitSquashEvents")
1721062SN/A        .desc("The number of times commit is told to squash")
1731062SN/A        .prereq(commitSquashEvents);
1741062SN/A    commitNonSpecStalls
1751062SN/A        .name(name() + ".commitNonSpecStalls")
1761062SN/A        .desc("The number of times commit has been forced to stall to "
1771062SN/A              "communicate backwards")
1781062SN/A        .prereq(commitNonSpecStalls);
1791062SN/A    branchMispredicts
1801062SN/A        .name(name() + ".branchMispredicts")
1811062SN/A        .desc("The number of times a branch was mispredicted")
1821062SN/A        .prereq(branchMispredicts);
1832292SN/A    numCommittedDist
1841062SN/A        .init(0,commitWidth,1)
1851062SN/A        .name(name() + ".COM:committed_per_cycle")
1861062SN/A        .desc("Number of insts commited each cycle")
1871062SN/A        .flags(Stats::pdf)
1881062SN/A        ;
1892301SN/A
1902316SN/A    statComInst
1916221Snate@binkert.org        .init(cpu->numThreads)
1922301SN/A        .name(name() + ".COM:count")
1932301SN/A        .desc("Number of instructions committed")
1942301SN/A        .flags(total)
1952301SN/A        ;
1962301SN/A
1972316SN/A    statComSwp
1986221Snate@binkert.org        .init(cpu->numThreads)
1992301SN/A        .name(name() + ".COM:swp_count")
2002301SN/A        .desc("Number of s/w prefetches committed")
2012301SN/A        .flags(total)
2022301SN/A        ;
2032301SN/A
2042316SN/A    statComRefs
2056221Snate@binkert.org        .init(cpu->numThreads)
2062301SN/A        .name(name() +  ".COM:refs")
2072301SN/A        .desc("Number of memory references committed")
2082301SN/A        .flags(total)
2092301SN/A        ;
2102301SN/A
2112316SN/A    statComLoads
2126221Snate@binkert.org        .init(cpu->numThreads)
2132301SN/A        .name(name() +  ".COM:loads")
2142301SN/A        .desc("Number of loads committed")
2152301SN/A        .flags(total)
2162301SN/A        ;
2172301SN/A
2182316SN/A    statComMembars
2196221Snate@binkert.org        .init(cpu->numThreads)
2202301SN/A        .name(name() +  ".COM:membars")
2212301SN/A        .desc("Number of memory barriers committed")
2222301SN/A        .flags(total)
2232301SN/A        ;
2242301SN/A
2252316SN/A    statComBranches
2266221Snate@binkert.org        .init(cpu->numThreads)
2272301SN/A        .name(name() + ".COM:branches")
2282301SN/A        .desc("Number of branches committed")
2292301SN/A        .flags(total)
2302301SN/A        ;
2312301SN/A
2322316SN/A    commitEligible
2336221Snate@binkert.org        .init(cpu->numThreads)
2342301SN/A        .name(name() + ".COM:bw_limited")
2352301SN/A        .desc("number of insts not committed due to BW limits")
2362301SN/A        .flags(total)
2372301SN/A        ;
2382301SN/A
2392316SN/A    commitEligibleSamples
2402301SN/A        .name(name() + ".COM:bw_lim_events")
2412301SN/A        .desc("number cycles where commit BW limit reached")
2422301SN/A        ;
2431062SN/A}
2441062SN/A
2451062SN/Atemplate <class Impl>
2461062SN/Avoid
2472980Sgblack@eecs.umich.eduDefaultCommit<Impl>::setThreads(std::vector<Thread *> &threads)
2482292SN/A{
2492292SN/A    thread = threads;
2502292SN/A}
2512292SN/A
2522292SN/Atemplate <class Impl>
2532292SN/Avoid
2542292SN/ADefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
2551060SN/A{
2561060SN/A    timeBuffer = tb_ptr;
2571060SN/A
2581060SN/A    // Setup wire to send information back to IEW.
2591060SN/A    toIEW = timeBuffer->getWire(0);
2601060SN/A
2611060SN/A    // Setup wire to read data from IEW (for the ROB).
2621060SN/A    robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
2631060SN/A}
2641060SN/A
2651061SN/Atemplate <class Impl>
2661060SN/Avoid
2672292SN/ADefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
2682292SN/A{
2692292SN/A    fetchQueue = fq_ptr;
2702292SN/A
2712292SN/A    // Setup wire to get instructions from rename (for the ROB).
2722292SN/A    fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
2732292SN/A}
2742292SN/A
2752292SN/Atemplate <class Impl>
2762292SN/Avoid
2772292SN/ADefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
2781060SN/A{
2791060SN/A    renameQueue = rq_ptr;
2801060SN/A
2811060SN/A    // Setup wire to get instructions from rename (for the ROB).
2821060SN/A    fromRename = renameQueue->getWire(-renameToROBDelay);
2831060SN/A}
2841060SN/A
2851061SN/Atemplate <class Impl>
2861060SN/Avoid
2872292SN/ADefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
2881060SN/A{
2891060SN/A    iewQueue = iq_ptr;
2901060SN/A
2911060SN/A    // Setup wire to get instructions from IEW.
2921060SN/A    fromIEW = iewQueue->getWire(-iewToCommitDelay);
2931060SN/A}
2941060SN/A
2951061SN/Atemplate <class Impl>
2961060SN/Avoid
2972292SN/ADefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
2982292SN/A{
2992292SN/A    iewStage = iew_stage;
3002292SN/A}
3012292SN/A
3022292SN/Atemplate<class Impl>
3032292SN/Avoid
3046221Snate@binkert.orgDefaultCommit<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
3052292SN/A{
3062292SN/A    activeThreads = at_ptr;
3072292SN/A}
3082292SN/A
3092292SN/Atemplate <class Impl>
3102292SN/Avoid
3112292SN/ADefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
3122292SN/A{
3136221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++)
3146221Snate@binkert.org        renameMap[tid] = &rm_ptr[tid];
3152292SN/A}
3162292SN/A
3172292SN/Atemplate <class Impl>
3182292SN/Avoid
3192292SN/ADefaultCommit<Impl>::setROB(ROB *rob_ptr)
3201060SN/A{
3211060SN/A    rob = rob_ptr;
3221060SN/A}
3231060SN/A
3241061SN/Atemplate <class Impl>
3251060SN/Avoid
3262292SN/ADefaultCommit<Impl>::initStage()
3271060SN/A{
3282292SN/A    rob->setActiveThreads(activeThreads);
3292292SN/A    rob->resetEntries();
3301060SN/A
3312292SN/A    // Broadcast the number of free entries.
3326221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++) {
3336221Snate@binkert.org        toIEW->commitInfo[tid].usedROB = true;
3346221Snate@binkert.org        toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
3356221Snate@binkert.org        toIEW->commitInfo[tid].emptyROB = true;
3361060SN/A    }
3371060SN/A
3384329Sktlim@umich.edu    // Commit must broadcast the number of free entries it has at the
3394329Sktlim@umich.edu    // start of the simulation, so it starts as active.
3404329Sktlim@umich.edu    cpu->activateStage(O3CPU::CommitIdx);
3414329Sktlim@umich.edu
3422292SN/A    cpu->activityThisCycle();
3435100Ssaidi@eecs.umich.edu    trapLatency = cpu->ticks(trapLatency);
3441060SN/A}
3451060SN/A
3461061SN/Atemplate <class Impl>
3472863Sktlim@umich.edubool
3482843Sktlim@umich.eduDefaultCommit<Impl>::drain()
3491060SN/A{
3502843Sktlim@umich.edu    drainPending = true;
3512863Sktlim@umich.edu
3522863Sktlim@umich.edu    return false;
3532316SN/A}
3542316SN/A
3552316SN/Atemplate <class Impl>
3562316SN/Avoid
3572843Sktlim@umich.eduDefaultCommit<Impl>::switchOut()
3582316SN/A{
3592316SN/A    switchedOut = true;
3602843Sktlim@umich.edu    drainPending = false;
3612307SN/A    rob->switchOut();
3622307SN/A}
3632307SN/A
3642307SN/Atemplate <class Impl>
3652307SN/Avoid
3662843Sktlim@umich.eduDefaultCommit<Impl>::resume()
3672843Sktlim@umich.edu{
3682864Sktlim@umich.edu    drainPending = false;
3692843Sktlim@umich.edu}
3702843Sktlim@umich.edu
3712843Sktlim@umich.edutemplate <class Impl>
3722843Sktlim@umich.eduvoid
3732307SN/ADefaultCommit<Impl>::takeOverFrom()
3742307SN/A{
3752316SN/A    switchedOut = false;
3762307SN/A    _status = Active;
3772307SN/A    _nextStatus = Inactive;
3786221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++) {
3796221Snate@binkert.org        commitStatus[tid] = Idle;
3806221Snate@binkert.org        changedROBNumEntries[tid] = false;
3816221Snate@binkert.org        trapSquash[tid] = false;
3826221Snate@binkert.org        tcSquash[tid] = false;
3832307SN/A    }
3842307SN/A    squashCounter = 0;
3852307SN/A    rob->takeOverFrom();
3862307SN/A}
3872307SN/A
3882307SN/Atemplate <class Impl>
3892307SN/Avoid
3902292SN/ADefaultCommit<Impl>::updateStatus()
3912132SN/A{
3922316SN/A    // reset ROB changed variable
3936221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
3946221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
3953867Sbinkertn@umich.edu
3963867Sbinkertn@umich.edu    while (threads != end) {
3976221Snate@binkert.org        ThreadID tid = *threads++;
3983867Sbinkertn@umich.edu
3992316SN/A        changedROBNumEntries[tid] = false;
4002316SN/A
4012316SN/A        // Also check if any of the threads has a trap pending
4022316SN/A        if (commitStatus[tid] == TrapPending ||
4032316SN/A            commitStatus[tid] == FetchTrapPending) {
4042316SN/A            _nextStatus = Active;
4052316SN/A        }
4062292SN/A    }
4072292SN/A
4082292SN/A    if (_nextStatus == Inactive && _status == Active) {
4092292SN/A        DPRINTF(Activity, "Deactivating stage.\n");
4102733Sktlim@umich.edu        cpu->deactivateStage(O3CPU::CommitIdx);
4112292SN/A    } else if (_nextStatus == Active && _status == Inactive) {
4122292SN/A        DPRINTF(Activity, "Activating stage.\n");
4132733Sktlim@umich.edu        cpu->activateStage(O3CPU::CommitIdx);
4142292SN/A    }
4152292SN/A
4162292SN/A    _status = _nextStatus;
4172292SN/A}
4182292SN/A
4192292SN/Atemplate <class Impl>
4202292SN/Avoid
4212292SN/ADefaultCommit<Impl>::setNextStatus()
4222292SN/A{
4232292SN/A    int squashes = 0;
4242292SN/A
4256221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4266221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4272292SN/A
4283867Sbinkertn@umich.edu    while (threads != end) {
4296221Snate@binkert.org        ThreadID tid = *threads++;
4302292SN/A
4312292SN/A        if (commitStatus[tid] == ROBSquashing) {
4322292SN/A            squashes++;
4332292SN/A        }
4342292SN/A    }
4352292SN/A
4362702Sktlim@umich.edu    squashCounter = squashes;
4372292SN/A
4382292SN/A    // If commit is currently squashing, then it will have activity for the
4392292SN/A    // next cycle. Set its next status as active.
4402292SN/A    if (squashCounter) {
4412292SN/A        _nextStatus = Active;
4422292SN/A    }
4432292SN/A}
4442292SN/A
4452292SN/Atemplate <class Impl>
4462292SN/Abool
4472292SN/ADefaultCommit<Impl>::changedROBEntries()
4482292SN/A{
4496221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4506221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4512292SN/A
4523867Sbinkertn@umich.edu    while (threads != end) {
4536221Snate@binkert.org        ThreadID tid = *threads++;
4542292SN/A
4552292SN/A        if (changedROBNumEntries[tid]) {
4562292SN/A            return true;
4572292SN/A        }
4582292SN/A    }
4592292SN/A
4602292SN/A    return false;
4612292SN/A}
4622292SN/A
4632292SN/Atemplate <class Impl>
4646221Snate@binkert.orgsize_t
4656221Snate@binkert.orgDefaultCommit<Impl>::numROBFreeEntries(ThreadID tid)
4662292SN/A{
4672292SN/A    return rob->numFreeEntries(tid);
4682292SN/A}
4692292SN/A
4702292SN/Atemplate <class Impl>
4712292SN/Avoid
4726221Snate@binkert.orgDefaultCommit<Impl>::generateTrapEvent(ThreadID tid)
4732292SN/A{
4742292SN/A    DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
4752292SN/A
4762292SN/A    TrapEvent *trap = new TrapEvent(this, tid);
4772292SN/A
4787823Ssteve.reinhardt@amd.com    cpu->schedule(trap, curTick() + trapLatency);
4794035Sktlim@umich.edu    trapInFlight[tid] = true;
4802292SN/A}
4812292SN/A
4822292SN/Atemplate <class Impl>
4832292SN/Avoid
4846221Snate@binkert.orgDefaultCommit<Impl>::generateTCEvent(ThreadID tid)
4852292SN/A{
4864035Sktlim@umich.edu    assert(!trapInFlight[tid]);
4872680Sktlim@umich.edu    DPRINTF(Commit, "Generating TC squash event for [tid:%i]\n", tid);
4882292SN/A
4892680Sktlim@umich.edu    tcSquash[tid] = true;
4902292SN/A}
4912292SN/A
4922292SN/Atemplate <class Impl>
4932292SN/Avoid
4946221Snate@binkert.orgDefaultCommit<Impl>::squashAll(ThreadID tid)
4952292SN/A{
4962292SN/A    // If we want to include the squashing instruction in the squash,
4972292SN/A    // then use one older sequence number.
4982292SN/A    // Hopefully this doesn't mess things up.  Basically I want to squash
4992292SN/A    // all instructions of this thread.
5002292SN/A    InstSeqNum squashed_inst = rob->isEmpty() ?
5014035Sktlim@umich.edu        0 : rob->readHeadInst(tid)->seqNum - 1;
5022292SN/A
5032292SN/A    // All younger instructions will be squashed. Set the sequence
5042292SN/A    // number as the youngest instruction in the ROB (0 in this case.
5052292SN/A    // Hopefully nothing breaks.)
5062292SN/A    youngestSeqNum[tid] = 0;
5072292SN/A
5082292SN/A    rob->squash(squashed_inst, tid);
5092292SN/A    changedROBNumEntries[tid] = true;
5102292SN/A
5112292SN/A    // Send back the sequence number of the squashed instruction.
5122292SN/A    toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
5132292SN/A
5142292SN/A    // Send back the squash signal to tell stages that they should
5152292SN/A    // squash.
5162292SN/A    toIEW->commitInfo[tid].squash = true;
5172292SN/A
5182292SN/A    // Send back the rob squashing signal so other stages know that
5192292SN/A    // the ROB is in the process of squashing.
5202292SN/A    toIEW->commitInfo[tid].robSquashing = true;
5212292SN/A
5222292SN/A    toIEW->commitInfo[tid].branchMispredict = false;
5237851SMatt.Horsnell@arm.com    toIEW->commitInfo[tid].mispredictInst = NULL;
5242292SN/A
5257720Sgblack@eecs.umich.edu    toIEW->commitInfo[tid].pc = pc[tid];
5262316SN/A}
5272292SN/A
5282316SN/Atemplate <class Impl>
5292316SN/Avoid
5306221Snate@binkert.orgDefaultCommit<Impl>::squashFromTrap(ThreadID tid)
5312316SN/A{
5322316SN/A    squashAll(tid);
5332316SN/A
5347720Sgblack@eecs.umich.edu    DPRINTF(Commit, "Squashing from trap, restarting at PC %s\n", pc[tid]);
5352316SN/A
5362316SN/A    thread[tid]->trapPending = false;
5372316SN/A    thread[tid]->inSyscall = false;
5384035Sktlim@umich.edu    trapInFlight[tid] = false;
5392316SN/A
5402316SN/A    trapSquash[tid] = false;
5412316SN/A
5422316SN/A    commitStatus[tid] = ROBSquashing;
5432316SN/A    cpu->activityThisCycle();
5442316SN/A}
5452316SN/A
5462316SN/Atemplate <class Impl>
5472316SN/Avoid
5486221Snate@binkert.orgDefaultCommit<Impl>::squashFromTC(ThreadID tid)
5492316SN/A{
5502316SN/A    squashAll(tid);
5512292SN/A
5527720Sgblack@eecs.umich.edu    DPRINTF(Commit, "Squashing from TC, restarting at PC %s\n", pc[tid]);
5532292SN/A
5542292SN/A    thread[tid]->inSyscall = false;
5552292SN/A    assert(!thread[tid]->trapPending);
5562316SN/A
5572292SN/A    commitStatus[tid] = ROBSquashing;
5582292SN/A    cpu->activityThisCycle();
5592292SN/A
5602680Sktlim@umich.edu    tcSquash[tid] = false;
5612292SN/A}
5622292SN/A
5632292SN/Atemplate <class Impl>
5642292SN/Avoid
5657784SAli.Saidi@ARM.comDefaultCommit<Impl>::squashAfter(ThreadID tid, uint64_t squash_after_seq_num)
5667784SAli.Saidi@ARM.com{
5677784SAli.Saidi@ARM.com    youngestSeqNum[tid] = squash_after_seq_num;
5687784SAli.Saidi@ARM.com
5697784SAli.Saidi@ARM.com    rob->squash(squash_after_seq_num, tid);
5707784SAli.Saidi@ARM.com    changedROBNumEntries[tid] = true;
5717784SAli.Saidi@ARM.com
5727784SAli.Saidi@ARM.com    // Send back the sequence number of the squashed instruction.
5737784SAli.Saidi@ARM.com    toIEW->commitInfo[tid].doneSeqNum = squash_after_seq_num;
5747784SAli.Saidi@ARM.com
5757784SAli.Saidi@ARM.com    // Send back the squash signal to tell stages that they should squash.
5767784SAli.Saidi@ARM.com    toIEW->commitInfo[tid].squash = true;
5777784SAli.Saidi@ARM.com
5787784SAli.Saidi@ARM.com    // Send back the rob squashing signal so other stages know that
5797784SAli.Saidi@ARM.com    // the ROB is in the process of squashing.
5807784SAli.Saidi@ARM.com    toIEW->commitInfo[tid].robSquashing = true;
5817784SAli.Saidi@ARM.com
5827784SAli.Saidi@ARM.com    toIEW->commitInfo[tid].branchMispredict = false;
5837784SAli.Saidi@ARM.com
5847784SAli.Saidi@ARM.com    toIEW->commitInfo[tid].pc = pc[tid];
5857784SAli.Saidi@ARM.com    DPRINTF(Commit, "Executing squash after for [tid:%i] inst [sn:%lli]\n",
5867784SAli.Saidi@ARM.com            tid, squash_after_seq_num);
5877784SAli.Saidi@ARM.com    commitStatus[tid] = ROBSquashing;
5887784SAli.Saidi@ARM.com}
5897784SAli.Saidi@ARM.com
5907784SAli.Saidi@ARM.comtemplate <class Impl>
5917784SAli.Saidi@ARM.comvoid
5922292SN/ADefaultCommit<Impl>::tick()
5932292SN/A{
5942292SN/A    wroteToTimeBuffer = false;
5952292SN/A    _nextStatus = Inactive;
5962292SN/A
5972843Sktlim@umich.edu    if (drainPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
5982843Sktlim@umich.edu        cpu->signalDrained();
5992843Sktlim@umich.edu        drainPending = false;
6002316SN/A        return;
6012316SN/A    }
6022316SN/A
6033867Sbinkertn@umich.edu    if (activeThreads->empty())
6042875Sksewell@umich.edu        return;
6052875Sksewell@umich.edu
6066221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
6076221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
6082292SN/A
6092316SN/A    // Check if any of the threads are done squashing.  Change the
6102316SN/A    // status if they are done.
6113867Sbinkertn@umich.edu    while (threads != end) {
6126221Snate@binkert.org        ThreadID tid = *threads++;
6132292SN/A
6144035Sktlim@umich.edu        // Clear the bit saying if the thread has committed stores
6154035Sktlim@umich.edu        // this cycle.
6164035Sktlim@umich.edu        committedStores[tid] = false;
6174035Sktlim@umich.edu
6182292SN/A        if (commitStatus[tid] == ROBSquashing) {
6192292SN/A
6202292SN/A            if (rob->isDoneSquashing(tid)) {
6212292SN/A                commitStatus[tid] = Running;
6222292SN/A            } else {
6232292SN/A                DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
6242877Sksewell@umich.edu                        " insts this cycle.\n", tid);
6252702Sktlim@umich.edu                rob->doSquash(tid);
6262702Sktlim@umich.edu                toIEW->commitInfo[tid].robSquashing = true;
6272702Sktlim@umich.edu                wroteToTimeBuffer = true;
6282292SN/A            }
6292292SN/A        }
6302292SN/A    }
6312292SN/A
6322292SN/A    commit();
6332292SN/A
6342292SN/A    markCompletedInsts();
6352292SN/A
6363867Sbinkertn@umich.edu    threads = activeThreads->begin();
6372292SN/A
6383867Sbinkertn@umich.edu    while (threads != end) {
6396221Snate@binkert.org        ThreadID tid = *threads++;
6402292SN/A
6412292SN/A        if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
6422292SN/A            // The ROB has more instructions it can commit. Its next status
6432292SN/A            // will be active.
6442292SN/A            _nextStatus = Active;
6452292SN/A
6462292SN/A            DynInstPtr inst = rob->readHeadInst(tid);
6472292SN/A
6487720Sgblack@eecs.umich.edu            DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %s is head of"
6492292SN/A                    " ROB and ready to commit\n",
6507720Sgblack@eecs.umich.edu                    tid, inst->seqNum, inst->pcState());
6512292SN/A
6522292SN/A        } else if (!rob->isEmpty(tid)) {
6532292SN/A            DynInstPtr inst = rob->readHeadInst(tid);
6542292SN/A
6552292SN/A            DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
6567720Sgblack@eecs.umich.edu                    "%s is head of ROB and not ready\n",
6577720Sgblack@eecs.umich.edu                    tid, inst->seqNum, inst->pcState());
6582292SN/A        }
6592292SN/A
6602292SN/A        DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
6612292SN/A                tid, rob->countInsts(tid), rob->numFreeEntries(tid));
6622292SN/A    }
6632292SN/A
6642292SN/A
6652292SN/A    if (wroteToTimeBuffer) {
6662316SN/A        DPRINTF(Activity, "Activity This Cycle.\n");
6672292SN/A        cpu->activityThisCycle();
6682292SN/A    }
6692292SN/A
6702292SN/A    updateStatus();
6712292SN/A}
6722292SN/A
6734035Sktlim@umich.edu#if FULL_SYSTEM
6742292SN/Atemplate <class Impl>
6752292SN/Avoid
6764035Sktlim@umich.eduDefaultCommit<Impl>::handleInterrupt()
6772292SN/A{
6787847Sminkyu.jeong@arm.com    // Verify that we still have an interrupt to handle
6797847Sminkyu.jeong@arm.com    if (!cpu->checkInterrupts(cpu->tcBase(0))) {
6807847Sminkyu.jeong@arm.com        DPRINTF(Commit, "Pending interrupt is cleared by master before "
6817847Sminkyu.jeong@arm.com                "it got handled. Restart fetching from the orig path.\n");
6827847Sminkyu.jeong@arm.com        toIEW->commitInfo[0].clearInterrupt = true;
6837847Sminkyu.jeong@arm.com        interrupt = NoFault;
6847847Sminkyu.jeong@arm.com        return;
6857847Sminkyu.jeong@arm.com    }
6863633Sktlim@umich.edu
6877847Sminkyu.jeong@arm.com    // Wait until the ROB is empty and all stores have drained in
6887847Sminkyu.jeong@arm.com    // order to enter the interrupt.
6897847Sminkyu.jeong@arm.com    if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
6907847Sminkyu.jeong@arm.com        // Squash or record that I need to squash this cycle if
6917847Sminkyu.jeong@arm.com        // an interrupt needed to be handled.
6927847Sminkyu.jeong@arm.com        DPRINTF(Commit, "Interrupt detected.\n");
6934035Sktlim@umich.edu
6947847Sminkyu.jeong@arm.com        // Clear the interrupt now that it's going to be handled
6957847Sminkyu.jeong@arm.com        toIEW->commitInfo[0].clearInterrupt = true;
6962292SN/A
6977847Sminkyu.jeong@arm.com        assert(!thread[0]->inSyscall);
6987847Sminkyu.jeong@arm.com        thread[0]->inSyscall = true;
6992292SN/A
7007847Sminkyu.jeong@arm.com        // CPU will handle interrupt.
7017847Sminkyu.jeong@arm.com        cpu->processInterrupts(interrupt);
7023633Sktlim@umich.edu
7037847Sminkyu.jeong@arm.com        thread[0]->inSyscall = false;
7042292SN/A
7057847Sminkyu.jeong@arm.com        commitStatus[0] = TrapPending;
7062292SN/A
7077847Sminkyu.jeong@arm.com        // Generate trap squash event.
7087847Sminkyu.jeong@arm.com        generateTrapEvent(0);
7093640Sktlim@umich.edu
7107847Sminkyu.jeong@arm.com        interrupt = NoFault;
7117847Sminkyu.jeong@arm.com    } else {
7127847Sminkyu.jeong@arm.com        DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
7131060SN/A    }
7144035Sktlim@umich.edu}
7157847Sminkyu.jeong@arm.com
7167847Sminkyu.jeong@arm.comtemplate <class Impl>
7177847Sminkyu.jeong@arm.comvoid
7187847Sminkyu.jeong@arm.comDefaultCommit<Impl>::propagateInterrupt()
7197847Sminkyu.jeong@arm.com{
7207847Sminkyu.jeong@arm.com    if (commitStatus[0] == TrapPending || interrupt || trapSquash[0] ||
7217847Sminkyu.jeong@arm.com            tcSquash[0])
7227847Sminkyu.jeong@arm.com        return;
7237847Sminkyu.jeong@arm.com
7247847Sminkyu.jeong@arm.com    // Process interrupts if interrupts are enabled, not in PAL
7257847Sminkyu.jeong@arm.com    // mode, and no other traps or external squashes are currently
7267847Sminkyu.jeong@arm.com    // pending.
7277847Sminkyu.jeong@arm.com    // @todo: Allow other threads to handle interrupts.
7287847Sminkyu.jeong@arm.com
7297847Sminkyu.jeong@arm.com    // Get any interrupt that happened
7307847Sminkyu.jeong@arm.com    interrupt = cpu->getInterrupts();
7317847Sminkyu.jeong@arm.com
7327847Sminkyu.jeong@arm.com    // Tell fetch that there is an interrupt pending.  This
7337847Sminkyu.jeong@arm.com    // will make fetch wait until it sees a non PAL-mode PC,
7347847Sminkyu.jeong@arm.com    // at which point it stops fetching instructions.
7357847Sminkyu.jeong@arm.com    if (interrupt != NoFault)
7367847Sminkyu.jeong@arm.com        toIEW->commitInfo[0].interruptPending = true;
7377847Sminkyu.jeong@arm.com}
7387847Sminkyu.jeong@arm.com
7394035Sktlim@umich.edu#endif // FULL_SYSTEM
7403634Sktlim@umich.edu
7414035Sktlim@umich.edutemplate <class Impl>
7424035Sktlim@umich.eduvoid
7434035Sktlim@umich.eduDefaultCommit<Impl>::commit()
7444035Sktlim@umich.edu{
7454035Sktlim@umich.edu
7464035Sktlim@umich.edu#if FULL_SYSTEM
7477847Sminkyu.jeong@arm.com    // Check for any interrupt that we've already squashed for and start processing it.
7487847Sminkyu.jeong@arm.com    if (interrupt != NoFault)
7494035Sktlim@umich.edu        handleInterrupt();
7507847Sminkyu.jeong@arm.com
7517847Sminkyu.jeong@arm.com    // Check if we have a interrupt and get read to handle it
7527847Sminkyu.jeong@arm.com    if (cpu->checkInterrupts(cpu->tcBase(0)))
7537847Sminkyu.jeong@arm.com        propagateInterrupt();
7541060SN/A#endif // FULL_SYSTEM
7551060SN/A
7561060SN/A    ////////////////////////////////////
7572316SN/A    // Check for any possible squashes, handle them first
7581060SN/A    ////////////////////////////////////
7596221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
7606221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
7611060SN/A
7623867Sbinkertn@umich.edu    while (threads != end) {
7636221Snate@binkert.org        ThreadID tid = *threads++;
7641060SN/A
7652292SN/A        // Not sure which one takes priority.  I think if we have
7662292SN/A        // both, that's a bad sign.
7672292SN/A        if (trapSquash[tid] == true) {
7682680Sktlim@umich.edu            assert(!tcSquash[tid]);
7692292SN/A            squashFromTrap(tid);
7702680Sktlim@umich.edu        } else if (tcSquash[tid] == true) {
7714035Sktlim@umich.edu            assert(commitStatus[tid] != TrapPending);
7722680Sktlim@umich.edu            squashFromTC(tid);
7732292SN/A        }
7741061SN/A
7752292SN/A        // Squashed sequence number must be older than youngest valid
7762292SN/A        // instruction in the ROB. This prevents squashes from younger
7772292SN/A        // instructions overriding squashes from older instructions.
7782292SN/A        if (fromIEW->squash[tid] &&
7792292SN/A            commitStatus[tid] != TrapPending &&
7802292SN/A            fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
7811061SN/A
7822292SN/A            DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
7832292SN/A                    tid,
7842292SN/A                    fromIEW->mispredPC[tid],
7852292SN/A                    fromIEW->squashedSeqNum[tid]);
7861061SN/A
7872292SN/A            DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
7882292SN/A                    tid,
7897720Sgblack@eecs.umich.edu                    fromIEW->pc[tid].nextInstAddr());
7901061SN/A
7912292SN/A            commitStatus[tid] = ROBSquashing;
7921061SN/A
7932292SN/A            // If we want to include the squashing instruction in the squash,
7942292SN/A            // then use one older sequence number.
7952292SN/A            InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
7961062SN/A
7972935Sksewell@umich.edu            if (fromIEW->includeSquashInst[tid] == true) {
7982292SN/A                squashed_inst--;
7992935Sksewell@umich.edu            }
8004035Sktlim@umich.edu
8012292SN/A            // All younger instructions will be squashed. Set the sequence
8022292SN/A            // number as the youngest instruction in the ROB.
8032292SN/A            youngestSeqNum[tid] = squashed_inst;
8042292SN/A
8053093Sksewell@umich.edu            rob->squash(squashed_inst, tid);
8062292SN/A            changedROBNumEntries[tid] = true;
8072292SN/A
8082292SN/A            toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
8092292SN/A
8102292SN/A            toIEW->commitInfo[tid].squash = true;
8112292SN/A
8122292SN/A            // Send back the rob squashing signal so other stages know that
8132292SN/A            // the ROB is in the process of squashing.
8142292SN/A            toIEW->commitInfo[tid].robSquashing = true;
8152292SN/A
8162292SN/A            toIEW->commitInfo[tid].branchMispredict =
8172292SN/A                fromIEW->branchMispredict[tid];
8187851SMatt.Horsnell@arm.com            toIEW->commitInfo[tid].mispredictInst =
8197851SMatt.Horsnell@arm.com                fromIEW->mispredictInst[tid];
8202292SN/A            toIEW->commitInfo[tid].branchTaken =
8212292SN/A                fromIEW->branchTaken[tid];
8222292SN/A
8237720Sgblack@eecs.umich.edu            toIEW->commitInfo[tid].pc = fromIEW->pc[tid];
8242292SN/A
8252316SN/A            toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
8262292SN/A
8272292SN/A            if (toIEW->commitInfo[tid].branchMispredict) {
8282292SN/A                ++branchMispredicts;
8292292SN/A            }
8301062SN/A        }
8312292SN/A
8321060SN/A    }
8331060SN/A
8342292SN/A    setNextStatus();
8352292SN/A
8362292SN/A    if (squashCounter != numThreads) {
8371061SN/A        // If we're not currently squashing, then get instructions.
8381060SN/A        getInsts();
8391060SN/A
8401061SN/A        // Try to commit any instructions.
8411060SN/A        commitInsts();
8421060SN/A    }
8431060SN/A
8442292SN/A    //Check for any activity
8453867Sbinkertn@umich.edu    threads = activeThreads->begin();
8462292SN/A
8473867Sbinkertn@umich.edu    while (threads != end) {
8486221Snate@binkert.org        ThreadID tid = *threads++;
8492292SN/A
8502292SN/A        if (changedROBNumEntries[tid]) {
8512292SN/A            toIEW->commitInfo[tid].usedROB = true;
8522292SN/A            toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
8532292SN/A
8542292SN/A            wroteToTimeBuffer = true;
8552292SN/A            changedROBNumEntries[tid] = false;
8564035Sktlim@umich.edu            if (rob->isEmpty(tid))
8574035Sktlim@umich.edu                checkEmptyROB[tid] = true;
8582292SN/A        }
8594035Sktlim@umich.edu
8604035Sktlim@umich.edu        // ROB is only considered "empty" for previous stages if: a)
8614035Sktlim@umich.edu        // ROB is empty, b) there are no outstanding stores, c) IEW
8624035Sktlim@umich.edu        // stage has received any information regarding stores that
8634035Sktlim@umich.edu        // committed.
8644035Sktlim@umich.edu        // c) is checked by making sure to not consider the ROB empty
8654035Sktlim@umich.edu        // on the same cycle as when stores have been committed.
8664035Sktlim@umich.edu        // @todo: Make this handle multi-cycle communication between
8674035Sktlim@umich.edu        // commit and IEW.
8684035Sktlim@umich.edu        if (checkEmptyROB[tid] && rob->isEmpty(tid) &&
8695557Sktlim@umich.edu            !iewStage->hasStoresToWB(tid) && !committedStores[tid]) {
8704035Sktlim@umich.edu            checkEmptyROB[tid] = false;
8714035Sktlim@umich.edu            toIEW->commitInfo[tid].usedROB = true;
8724035Sktlim@umich.edu            toIEW->commitInfo[tid].emptyROB = true;
8734035Sktlim@umich.edu            toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
8744035Sktlim@umich.edu            wroteToTimeBuffer = true;
8754035Sktlim@umich.edu        }
8764035Sktlim@umich.edu
8771060SN/A    }
8781060SN/A}
8791060SN/A
8801061SN/Atemplate <class Impl>
8811060SN/Avoid
8822292SN/ADefaultCommit<Impl>::commitInsts()
8831060SN/A{
8841060SN/A    ////////////////////////////////////
8851060SN/A    // Handle commit
8862316SN/A    // Note that commit will be handled prior to putting new
8872316SN/A    // instructions in the ROB so that the ROB only tries to commit
8882316SN/A    // instructions it has in this current cycle, and not instructions
8892316SN/A    // it is writing in during this cycle.  Can't commit and squash
8902316SN/A    // things at the same time...
8911060SN/A    ////////////////////////////////////
8921060SN/A
8932292SN/A    DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
8941060SN/A
8951060SN/A    unsigned num_committed = 0;
8961060SN/A
8972292SN/A    DynInstPtr head_inst;
8982316SN/A
8991060SN/A    // Commit as many instructions as possible until the commit bandwidth
9001060SN/A    // limit is reached, or it becomes impossible to commit any more.
9012292SN/A    while (num_committed < commitWidth) {
9022292SN/A        int commit_thread = getCommittingThread();
9031060SN/A
9042292SN/A        if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
9052292SN/A            break;
9062292SN/A
9072292SN/A        head_inst = rob->readHeadInst(commit_thread);
9082292SN/A
9096221Snate@binkert.org        ThreadID tid = head_inst->threadNumber;
9102292SN/A
9112292SN/A        assert(tid == commit_thread);
9122292SN/A
9132292SN/A        DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
9142292SN/A                head_inst->seqNum, tid);
9152132SN/A
9162316SN/A        // If the head instruction is squashed, it is ready to retire
9172316SN/A        // (be removed from the ROB) at any time.
9181060SN/A        if (head_inst->isSquashed()) {
9191060SN/A
9202292SN/A            DPRINTF(Commit, "Retiring squashed instruction from "
9211060SN/A                    "ROB.\n");
9221060SN/A
9232292SN/A            rob->retireHead(commit_thread);
9241060SN/A
9251062SN/A            ++commitSquashedInsts;
9261062SN/A
9272292SN/A            // Record that the number of ROB entries has changed.
9282292SN/A            changedROBNumEntries[tid] = true;
9291060SN/A        } else {
9307720Sgblack@eecs.umich.edu            pc[tid] = head_inst->pcState();
9312292SN/A
9321060SN/A            // Increment the total number of non-speculative instructions
9331060SN/A            // executed.
9341060SN/A            // Hack for now: it really shouldn't happen until after the
9351061SN/A            // commit is deemed to be successful, but this count is needed
9361061SN/A            // for syscalls.
9372292SN/A            thread[tid]->funcExeInst++;
9381060SN/A
9391060SN/A            // Try to commit the head instruction.
9401060SN/A            bool commit_success = commitHead(head_inst, num_committed);
9411060SN/A
9421062SN/A            if (commit_success) {
9431060SN/A                ++num_committed;
9441060SN/A
9452292SN/A                changedROBNumEntries[tid] = true;
9462292SN/A
9472292SN/A                // Set the doneSeqNum to the youngest committed instruction.
9482292SN/A                toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
9491060SN/A
9501062SN/A                ++commitCommittedInsts;
9511062SN/A
9522292SN/A                // To match the old model, don't count nops and instruction
9532292SN/A                // prefetches towards the total commit count.
9542292SN/A                if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
9552292SN/A                    cpu->instDone(tid);
9561062SN/A                }
9572292SN/A
9587783SGiacomo.Gabrielli@arm.com                // Updates misc. registers.
9597783SGiacomo.Gabrielli@arm.com                head_inst->updateMiscRegs();
9607783SGiacomo.Gabrielli@arm.com
9617720Sgblack@eecs.umich.edu                TheISA::advancePC(pc[tid], head_inst->staticInst);
9622935Sksewell@umich.edu
9637784SAli.Saidi@ARM.com                // If this is an instruction that doesn't play nicely with
9647784SAli.Saidi@ARM.com                // others squash everything and restart fetch
9657784SAli.Saidi@ARM.com                if (head_inst->isSquashAfter())
9667784SAli.Saidi@ARM.com                    squashAfter(tid, head_inst->seqNum);
9677784SAli.Saidi@ARM.com
9682292SN/A                int count = 0;
9692292SN/A                Addr oldpc;
9705108Sgblack@eecs.umich.edu                // Debug statement.  Checks to make sure we're not
9715108Sgblack@eecs.umich.edu                // currently updating state while handling PC events.
9725108Sgblack@eecs.umich.edu                assert(!thread[tid]->inSyscall && !thread[tid]->trapPending);
9732292SN/A                do {
9747720Sgblack@eecs.umich.edu                    oldpc = pc[tid].instAddr();
9755108Sgblack@eecs.umich.edu                    cpu->system->pcEventQueue.service(thread[tid]->getTC());
9762292SN/A                    count++;
9777720Sgblack@eecs.umich.edu                } while (oldpc != pc[tid].instAddr());
9782292SN/A                if (count > 1) {
9795108Sgblack@eecs.umich.edu                    DPRINTF(Commit,
9805108Sgblack@eecs.umich.edu                            "PC skip function event, stopping commit\n");
9812292SN/A                    break;
9822292SN/A                }
9831060SN/A            } else {
9847720Sgblack@eecs.umich.edu                DPRINTF(Commit, "Unable to commit head instruction PC:%s "
9852292SN/A                        "[tid:%i] [sn:%i].\n",
9867720Sgblack@eecs.umich.edu                        head_inst->pcState(), tid ,head_inst->seqNum);
9871060SN/A                break;
9881060SN/A            }
9891060SN/A        }
9901060SN/A    }
9911062SN/A
9921063SN/A    DPRINTF(CommitRate, "%i\n", num_committed);
9932292SN/A    numCommittedDist.sample(num_committed);
9942307SN/A
9952307SN/A    if (num_committed == commitWidth) {
9962349SN/A        commitEligibleSamples++;
9972307SN/A    }
9981060SN/A}
9991060SN/A
10001061SN/Atemplate <class Impl>
10011060SN/Abool
10022292SN/ADefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
10031060SN/A{
10041060SN/A    assert(head_inst);
10051060SN/A
10066221Snate@binkert.org    ThreadID tid = head_inst->threadNumber;
10072292SN/A
10082316SN/A    // If the instruction is not executed yet, then it will need extra
10092316SN/A    // handling.  Signal backwards that it should be executed.
10101061SN/A    if (!head_inst->isExecuted()) {
10111061SN/A        // Keep this number correct.  We have not yet actually executed
10121061SN/A        // and committed this instruction.
10132292SN/A        thread[tid]->funcExeInst--;
10141062SN/A
10152292SN/A        if (head_inst->isNonSpeculative() ||
10162348SN/A            head_inst->isStoreConditional() ||
10172292SN/A            head_inst->isMemBarrier() ||
10182292SN/A            head_inst->isWriteBarrier()) {
10192316SN/A
10202316SN/A            DPRINTF(Commit, "Encountered a barrier or non-speculative "
10217720Sgblack@eecs.umich.edu                    "instruction [sn:%lli] at the head of the ROB, PC %s.\n",
10227720Sgblack@eecs.umich.edu                    head_inst->seqNum, head_inst->pcState());
10232316SN/A
10245557Sktlim@umich.edu            if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
10252292SN/A                DPRINTF(Commit, "Waiting for all stores to writeback.\n");
10262292SN/A                return false;
10272292SN/A            }
10282292SN/A
10292292SN/A            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
10301061SN/A
10311061SN/A            // Change the instruction so it won't try to commit again until
10321061SN/A            // it is executed.
10331061SN/A            head_inst->clearCanCommit();
10341061SN/A
10351062SN/A            ++commitNonSpecStalls;
10361062SN/A
10371061SN/A            return false;
10382292SN/A        } else if (head_inst->isLoad()) {
10395557Sktlim@umich.edu            if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
10404035Sktlim@umich.edu                DPRINTF(Commit, "Waiting for all stores to writeback.\n");
10414035Sktlim@umich.edu                return false;
10424035Sktlim@umich.edu            }
10434035Sktlim@umich.edu
10444035Sktlim@umich.edu            assert(head_inst->uncacheable());
10457720Sgblack@eecs.umich.edu            DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %s.\n",
10467720Sgblack@eecs.umich.edu                    head_inst->seqNum, head_inst->pcState());
10472292SN/A
10482292SN/A            // Send back the non-speculative instruction's sequence
10492316SN/A            // number.  Tell the lsq to re-execute the load.
10502292SN/A            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
10512292SN/A            toIEW->commitInfo[tid].uncached = true;
10522292SN/A            toIEW->commitInfo[tid].uncachedLoad = head_inst;
10532292SN/A
10542292SN/A            head_inst->clearCanCommit();
10552292SN/A
10562292SN/A            return false;
10571061SN/A        } else {
10582292SN/A            panic("Trying to commit un-executed instruction "
10591061SN/A                  "of unknown type!\n");
10601061SN/A        }
10611060SN/A    }
10621060SN/A
10632316SN/A    if (head_inst->isThreadSync()) {
10642292SN/A        // Not handled for now.
10652316SN/A        panic("Thread sync instructions are not handled yet.\n");
10662132SN/A    }
10672132SN/A
10684035Sktlim@umich.edu    // Check if the instruction caused a fault.  If so, trap.
10694035Sktlim@umich.edu    Fault inst_fault = head_inst->getFault();
10704035Sktlim@umich.edu
10712316SN/A    // Stores mark themselves as completed.
10724035Sktlim@umich.edu    if (!head_inst->isStore() && inst_fault == NoFault) {
10732310SN/A        head_inst->setCompleted();
10742310SN/A    }
10752310SN/A
10762733Sktlim@umich.edu#if USE_CHECKER
10772316SN/A    // Use checker prior to updating anything due to traps or PC
10782316SN/A    // based events.
10792316SN/A    if (cpu->checker) {
10802732Sktlim@umich.edu        cpu->checker->verify(head_inst);
10811060SN/A    }
10822733Sktlim@umich.edu#endif
10831060SN/A
10842112SN/A    if (inst_fault != NoFault) {
10857720Sgblack@eecs.umich.edu        DPRINTF(Commit, "Inst [sn:%lli] PC %s has a fault\n",
10867720Sgblack@eecs.umich.edu                head_inst->seqNum, head_inst->pcState());
10872292SN/A
10885557Sktlim@umich.edu        if (iewStage->hasStoresToWB(tid) || inst_num > 0) {
10892316SN/A            DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
10902316SN/A            return false;
10912316SN/A        }
10922310SN/A
10934035Sktlim@umich.edu        head_inst->setCompleted();
10944035Sktlim@umich.edu
10952733Sktlim@umich.edu#if USE_CHECKER
10962316SN/A        if (cpu->checker && head_inst->isStore()) {
10972732Sktlim@umich.edu            cpu->checker->verify(head_inst);
10982316SN/A        }
10992733Sktlim@umich.edu#endif
11002292SN/A
11012316SN/A        assert(!thread[tid]->inSyscall);
11022292SN/A
11032316SN/A        // Mark that we're in state update mode so that the trap's
11042316SN/A        // execution doesn't generate extra squashes.
11052316SN/A        thread[tid]->inSyscall = true;
11062292SN/A
11072316SN/A        // Execute the trap.  Although it's slightly unrealistic in
11082316SN/A        // terms of timing (as it doesn't wait for the full timing of
11092316SN/A        // the trap event to complete before updating state), it's
11102316SN/A        // needed to update the state as soon as possible.  This
11112316SN/A        // prevents external agents from changing any specific state
11122316SN/A        // that the trap need.
11137684Sgblack@eecs.umich.edu        cpu->trap(inst_fault, tid, head_inst->staticInst);
11142292SN/A
11152316SN/A        // Exit state update mode to avoid accidental updating.
11162316SN/A        thread[tid]->inSyscall = false;
11172292SN/A
11182316SN/A        commitStatus[tid] = TrapPending;
11192292SN/A
11204035Sktlim@umich.edu        if (head_inst->traceData) {
11216667Ssteve.reinhardt@amd.com            if (DTRACE(ExecFaulting)) {
11226667Ssteve.reinhardt@amd.com                head_inst->traceData->setFetchSeq(head_inst->seqNum);
11236667Ssteve.reinhardt@amd.com                head_inst->traceData->setCPSeq(thread[tid]->numInst);
11246667Ssteve.reinhardt@amd.com                head_inst->traceData->dump();
11256667Ssteve.reinhardt@amd.com            }
11264288Sktlim@umich.edu            delete head_inst->traceData;
11274035Sktlim@umich.edu            head_inst->traceData = NULL;
11284035Sktlim@umich.edu        }
11294035Sktlim@umich.edu
11302316SN/A        // Generate trap squash event.
11312316SN/A        generateTrapEvent(tid);
11322316SN/A        return false;
11331060SN/A    }
11341060SN/A
11352301SN/A    updateComInstStats(head_inst);
11362132SN/A
11372362SN/A#if FULL_SYSTEM
11382362SN/A    if (thread[tid]->profile) {
11397720Sgblack@eecs.umich.edu        thread[tid]->profilePC = head_inst->instAddr();
11403126Sktlim@umich.edu        ProfileNode *node = thread[tid]->profile->consume(thread[tid]->getTC(),
11412362SN/A                                                          head_inst->staticInst);
11422362SN/A
11432362SN/A        if (node)
11442362SN/A            thread[tid]->profileNode = node;
11452362SN/A    }
11465953Ssaidi@eecs.umich.edu    if (CPA::available()) {
11475953Ssaidi@eecs.umich.edu        if (head_inst->isControl()) {
11485953Ssaidi@eecs.umich.edu            ThreadContext *tc = thread[tid]->getTC();
11497720Sgblack@eecs.umich.edu            CPA::cpa()->swAutoBegin(tc, head_inst->nextInstAddr());
11505953Ssaidi@eecs.umich.edu        }
11515953Ssaidi@eecs.umich.edu    }
11522362SN/A#endif
11532362SN/A
11542132SN/A    if (head_inst->traceData) {
11552292SN/A        head_inst->traceData->setFetchSeq(head_inst->seqNum);
11562292SN/A        head_inst->traceData->setCPSeq(thread[tid]->numInst);
11574046Sbinkertn@umich.edu        head_inst->traceData->dump();
11584046Sbinkertn@umich.edu        delete head_inst->traceData;
11592292SN/A        head_inst->traceData = NULL;
11601060SN/A    }
11611060SN/A
11622292SN/A    // Update the commit rename map
11632292SN/A    for (int i = 0; i < head_inst->numDestRegs(); i++) {
11643771Sgblack@eecs.umich.edu        renameMap[tid]->setEntry(head_inst->flattenedDestRegIdx(i),
11652292SN/A                                 head_inst->renamedDestRegIdx(i));
11661060SN/A    }
11671062SN/A
11682353SN/A    if (head_inst->isCopy())
11692353SN/A        panic("Should not commit any copy instructions!");
11702353SN/A
11712292SN/A    // Finally clear the head ROB entry.
11722292SN/A    rob->retireHead(tid);
11731060SN/A
11744035Sktlim@umich.edu    // If this was a store, record it for this cycle.
11754035Sktlim@umich.edu    if (head_inst->isStore())
11764035Sktlim@umich.edu        committedStores[tid] = true;
11774035Sktlim@umich.edu
11781060SN/A    // Return true to indicate that we have committed an instruction.
11791060SN/A    return true;
11801060SN/A}
11811060SN/A
11821061SN/Atemplate <class Impl>
11831060SN/Avoid
11842292SN/ADefaultCommit<Impl>::getInsts()
11851060SN/A{
11862935Sksewell@umich.edu    DPRINTF(Commit, "Getting instructions from Rename stage.\n");
11872935Sksewell@umich.edu
11883093Sksewell@umich.edu    // Read any renamed instructions and place them into the ROB.
11893093Sksewell@umich.edu    int insts_to_process = std::min((int)renameWidth, fromRename->size);
11902965Sksewell@umich.edu
11912965Sksewell@umich.edu    for (int inst_num = 0; inst_num < insts_to_process; ++inst_num) {
11922965Sksewell@umich.edu        DynInstPtr inst;
11932965Sksewell@umich.edu
11943093Sksewell@umich.edu        inst = fromRename->insts[inst_num];
11956221Snate@binkert.org        ThreadID tid = inst->threadNumber;
11962292SN/A
11972292SN/A        if (!inst->isSquashed() &&
11984035Sktlim@umich.edu            commitStatus[tid] != ROBSquashing &&
11994035Sktlim@umich.edu            commitStatus[tid] != TrapPending) {
12002292SN/A            changedROBNumEntries[tid] = true;
12012292SN/A
12027720Sgblack@eecs.umich.edu            DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ROB.\n",
12037720Sgblack@eecs.umich.edu                    inst->pcState(), inst->seqNum, tid);
12042292SN/A
12052292SN/A            rob->insertInst(inst);
12062292SN/A
12072292SN/A            assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
12082292SN/A
12092292SN/A            youngestSeqNum[tid] = inst->seqNum;
12101061SN/A        } else {
12117720Sgblack@eecs.umich.edu            DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
12121061SN/A                    "squashed, skipping.\n",
12137720Sgblack@eecs.umich.edu                    inst->pcState(), inst->seqNum, tid);
12141061SN/A        }
12151060SN/A    }
12162965Sksewell@umich.edu}
12172965Sksewell@umich.edu
12182965Sksewell@umich.edutemplate <class Impl>
12192965Sksewell@umich.eduvoid
12202965Sksewell@umich.eduDefaultCommit<Impl>::skidInsert()
12212965Sksewell@umich.edu{
12222965Sksewell@umich.edu    DPRINTF(Commit, "Attempting to any instructions from rename into "
12232965Sksewell@umich.edu            "skidBuffer.\n");
12242965Sksewell@umich.edu
12252965Sksewell@umich.edu    for (int inst_num = 0; inst_num < fromRename->size; ++inst_num) {
12262965Sksewell@umich.edu        DynInstPtr inst = fromRename->insts[inst_num];
12272965Sksewell@umich.edu
12282965Sksewell@umich.edu        if (!inst->isSquashed()) {
12297720Sgblack@eecs.umich.edu            DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ",
12307720Sgblack@eecs.umich.edu                    "skidBuffer.\n", inst->pcState(), inst->seqNum,
12313221Sktlim@umich.edu                    inst->threadNumber);
12322965Sksewell@umich.edu            skidBuffer.push(inst);
12332965Sksewell@umich.edu        } else {
12347720Sgblack@eecs.umich.edu            DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
12352965Sksewell@umich.edu                    "squashed, skipping.\n",
12367720Sgblack@eecs.umich.edu                    inst->pcState(), inst->seqNum, inst->threadNumber);
12372965Sksewell@umich.edu        }
12382965Sksewell@umich.edu    }
12391060SN/A}
12401060SN/A
12411061SN/Atemplate <class Impl>
12421060SN/Avoid
12432292SN/ADefaultCommit<Impl>::markCompletedInsts()
12441060SN/A{
12451060SN/A    // Grab completed insts out of the IEW instruction queue, and mark
12461060SN/A    // instructions completed within the ROB.
12471060SN/A    for (int inst_num = 0;
12481681SN/A         inst_num < fromIEW->size && fromIEW->insts[inst_num];
12491060SN/A         ++inst_num)
12501060SN/A    {
12512292SN/A        if (!fromIEW->insts[inst_num]->isSquashed()) {
12527720Sgblack@eecs.umich.edu            DPRINTF(Commit, "[tid:%i]: Marking PC %s, [sn:%lli] ready "
12532316SN/A                    "within ROB.\n",
12542292SN/A                    fromIEW->insts[inst_num]->threadNumber,
12557720Sgblack@eecs.umich.edu                    fromIEW->insts[inst_num]->pcState(),
12562292SN/A                    fromIEW->insts[inst_num]->seqNum);
12571060SN/A
12582292SN/A            // Mark the instruction as ready to commit.
12592292SN/A            fromIEW->insts[inst_num]->setCanCommit();
12602292SN/A        }
12611060SN/A    }
12621060SN/A}
12631060SN/A
12641061SN/Atemplate <class Impl>
12652292SN/Abool
12662292SN/ADefaultCommit<Impl>::robDoneSquashing()
12671060SN/A{
12686221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
12696221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
12702292SN/A
12713867Sbinkertn@umich.edu    while (threads != end) {
12726221Snate@binkert.org        ThreadID tid = *threads++;
12732292SN/A
12742292SN/A        if (!rob->isDoneSquashing(tid))
12752292SN/A            return false;
12762292SN/A    }
12772292SN/A
12782292SN/A    return true;
12791060SN/A}
12802292SN/A
12812301SN/Atemplate <class Impl>
12822301SN/Avoid
12832301SN/ADefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
12842301SN/A{
12856221Snate@binkert.org    ThreadID tid = inst->threadNumber;
12862301SN/A
12872301SN/A    //
12882301SN/A    //  Pick off the software prefetches
12892301SN/A    //
12902301SN/A#ifdef TARGET_ALPHA
12912301SN/A    if (inst->isDataPrefetch()) {
12926221Snate@binkert.org        statComSwp[tid]++;
12932301SN/A    } else {
12946221Snate@binkert.org        statComInst[tid]++;
12952301SN/A    }
12962301SN/A#else
12976221Snate@binkert.org    statComInst[tid]++;
12982301SN/A#endif
12992301SN/A
13002301SN/A    //
13012301SN/A    //  Control Instructions
13022301SN/A    //
13032301SN/A    if (inst->isControl())
13046221Snate@binkert.org        statComBranches[tid]++;
13052301SN/A
13062301SN/A    //
13072301SN/A    //  Memory references
13082301SN/A    //
13092301SN/A    if (inst->isMemRef()) {
13106221Snate@binkert.org        statComRefs[tid]++;
13112301SN/A
13122301SN/A        if (inst->isLoad()) {
13136221Snate@binkert.org            statComLoads[tid]++;
13142301SN/A        }
13152301SN/A    }
13162301SN/A
13172301SN/A    if (inst->isMemBarrier()) {
13186221Snate@binkert.org        statComMembars[tid]++;
13192301SN/A    }
13202301SN/A}
13212301SN/A
13222292SN/A////////////////////////////////////////
13232292SN/A//                                    //
13242316SN/A//  SMT COMMIT POLICY MAINTAINED HERE //
13252292SN/A//                                    //
13262292SN/A////////////////////////////////////////
13272292SN/Atemplate <class Impl>
13286221Snate@binkert.orgThreadID
13292292SN/ADefaultCommit<Impl>::getCommittingThread()
13302292SN/A{
13312292SN/A    if (numThreads > 1) {
13322292SN/A        switch (commitPolicy) {
13332292SN/A
13342292SN/A          case Aggressive:
13352292SN/A            //If Policy is Aggressive, commit will call
13362292SN/A            //this function multiple times per
13372292SN/A            //cycle
13382292SN/A            return oldestReady();
13392292SN/A
13402292SN/A          case RoundRobin:
13412292SN/A            return roundRobin();
13422292SN/A
13432292SN/A          case OldestReady:
13442292SN/A            return oldestReady();
13452292SN/A
13462292SN/A          default:
13476221Snate@binkert.org            return InvalidThreadID;
13482292SN/A        }
13492292SN/A    } else {
13503867Sbinkertn@umich.edu        assert(!activeThreads->empty());
13516221Snate@binkert.org        ThreadID tid = activeThreads->front();
13522292SN/A
13532292SN/A        if (commitStatus[tid] == Running ||
13542292SN/A            commitStatus[tid] == Idle ||
13552292SN/A            commitStatus[tid] == FetchTrapPending) {
13562292SN/A            return tid;
13572292SN/A        } else {
13586221Snate@binkert.org            return InvalidThreadID;
13592292SN/A        }
13602292SN/A    }
13612292SN/A}
13622292SN/A
13632292SN/Atemplate<class Impl>
13646221Snate@binkert.orgThreadID
13652292SN/ADefaultCommit<Impl>::roundRobin()
13662292SN/A{
13676221Snate@binkert.org    list<ThreadID>::iterator pri_iter = priority_list.begin();
13686221Snate@binkert.org    list<ThreadID>::iterator end      = priority_list.end();
13692292SN/A
13702292SN/A    while (pri_iter != end) {
13716221Snate@binkert.org        ThreadID tid = *pri_iter;
13722292SN/A
13732292SN/A        if (commitStatus[tid] == Running ||
13742831Sksewell@umich.edu            commitStatus[tid] == Idle ||
13752831Sksewell@umich.edu            commitStatus[tid] == FetchTrapPending) {
13762292SN/A
13772292SN/A            if (rob->isHeadReady(tid)) {
13782292SN/A                priority_list.erase(pri_iter);
13792292SN/A                priority_list.push_back(tid);
13802292SN/A
13812292SN/A                return tid;
13822292SN/A            }
13832292SN/A        }
13842292SN/A
13852292SN/A        pri_iter++;
13862292SN/A    }
13872292SN/A
13886221Snate@binkert.org    return InvalidThreadID;
13892292SN/A}
13902292SN/A
13912292SN/Atemplate<class Impl>
13926221Snate@binkert.orgThreadID
13932292SN/ADefaultCommit<Impl>::oldestReady()
13942292SN/A{
13952292SN/A    unsigned oldest = 0;
13962292SN/A    bool first = true;
13972292SN/A
13986221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
13996221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
14002292SN/A
14013867Sbinkertn@umich.edu    while (threads != end) {
14026221Snate@binkert.org        ThreadID tid = *threads++;
14032292SN/A
14042292SN/A        if (!rob->isEmpty(tid) &&
14052292SN/A            (commitStatus[tid] == Running ||
14062292SN/A             commitStatus[tid] == Idle ||
14072292SN/A             commitStatus[tid] == FetchTrapPending)) {
14082292SN/A
14092292SN/A            if (rob->isHeadReady(tid)) {
14102292SN/A
14112292SN/A                DynInstPtr head_inst = rob->readHeadInst(tid);
14122292SN/A
14132292SN/A                if (first) {
14142292SN/A                    oldest = tid;
14152292SN/A                    first = false;
14162292SN/A                } else if (head_inst->seqNum < oldest) {
14172292SN/A                    oldest = tid;
14182292SN/A                }
14192292SN/A            }
14202292SN/A        }
14212292SN/A    }
14222292SN/A
14232292SN/A    if (!first) {
14242292SN/A        return oldest;
14252292SN/A    } else {
14266221Snate@binkert.org        return InvalidThreadID;
14272292SN/A    }
14282292SN/A}
1429