commit_impl.hh revision 5953
11689SN/A/*
22316SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
31689SN/A * All rights reserved.
41689SN/A *
51689SN/A * Redistribution and use in source and binary forms, with or without
61689SN/A * modification, are permitted provided that the following conditions are
71689SN/A * met: redistributions of source code must retain the above copyright
81689SN/A * notice, this list of conditions and the following disclaimer;
91689SN/A * redistributions in binary form must reproduce the above copyright
101689SN/A * notice, this list of conditions and the following disclaimer in the
111689SN/A * documentation and/or other materials provided with the distribution;
121689SN/A * neither the name of the copyright holders nor the names of its
131689SN/A * contributors may be used to endorse or promote products derived from
141689SN/A * this software without specific prior written permission.
151689SN/A *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
292965Sksewell@umich.edu *          Korey Sewell
301689SN/A */
311689SN/A
322733Sktlim@umich.edu#include "config/full_system.hh"
332733Sktlim@umich.edu#include "config/use_checker.hh"
342733Sktlim@umich.edu
352292SN/A#include <algorithm>
362329SN/A#include <string>
372292SN/A
383577Sgblack@eecs.umich.edu#include "arch/utility.hh"
395953Ssaidi@eecs.umich.edu#include "base/cp_annotate.hh"
402292SN/A#include "base/loader/symtab.hh"
411060SN/A#include "base/timebuf.hh"
422292SN/A#include "cpu/exetrace.hh"
431717SN/A#include "cpu/o3/commit.hh"
442292SN/A#include "cpu/o3/thread_state.hh"
452292SN/A
462790Sktlim@umich.edu#if USE_CHECKER
472790Sktlim@umich.edu#include "cpu/checker/cpu.hh"
482790Sktlim@umich.edu#endif
492790Sktlim@umich.edu
505529Snate@binkert.org#include "params/DerivO3CPU.hh"
515529Snate@binkert.org
521061SN/Atemplate <class Impl>
532292SN/ADefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
542292SN/A                                          unsigned _tid)
555606Snate@binkert.org    : Event(CPU_Tick_Pri), commit(_commit), tid(_tid)
561060SN/A{
575769Snate@binkert.org    this->setFlags(AutoDelete);
581060SN/A}
591060SN/A
601061SN/Atemplate <class Impl>
611060SN/Avoid
622292SN/ADefaultCommit<Impl>::TrapEvent::process()
631062SN/A{
642316SN/A    // This will get reset by commit if it was switched out at the
652316SN/A    // time of this event processing.
662292SN/A    commit->trapSquash[tid] = true;
672292SN/A}
682292SN/A
692292SN/Atemplate <class Impl>
702292SN/Aconst char *
715336Shines@cs.fsu.eduDefaultCommit<Impl>::TrapEvent::description() const
722292SN/A{
734873Sstever@eecs.umich.edu    return "Trap";
742292SN/A}
752292SN/A
762292SN/Atemplate <class Impl>
775529Snate@binkert.orgDefaultCommit<Impl>::DefaultCommit(O3CPU *_cpu, DerivO3CPUParams *params)
784329Sktlim@umich.edu    : cpu(_cpu),
794329Sktlim@umich.edu      squashCounter(0),
802292SN/A      iewToCommitDelay(params->iewToCommitDelay),
812292SN/A      commitToIEWDelay(params->commitToIEWDelay),
822292SN/A      renameToROBDelay(params->renameToROBDelay),
832292SN/A      fetchToCommitDelay(params->commitToFetchDelay),
842292SN/A      renameWidth(params->renameWidth),
852292SN/A      commitWidth(params->commitWidth),
865529Snate@binkert.org      numThreads(params->numThreads),
872843Sktlim@umich.edu      drainPending(false),
882316SN/A      switchedOut(false),
892874Sktlim@umich.edu      trapLatency(params->trapLatency)
902292SN/A{
912292SN/A    _status = Active;
922292SN/A    _nextStatus = Inactive;
932980Sgblack@eecs.umich.edu    std::string policy = params->smtCommitPolicy;
942292SN/A
952292SN/A    //Convert string to lowercase
962292SN/A    std::transform(policy.begin(), policy.end(), policy.begin(),
972292SN/A                   (int(*)(int)) tolower);
982292SN/A
992292SN/A    //Assign commit policy
1002292SN/A    if (policy == "aggressive"){
1012292SN/A        commitPolicy = Aggressive;
1022292SN/A
1034329Sktlim@umich.edu        DPRINTF(Commit,"Commit Policy set to Aggressive.");
1042292SN/A    } else if (policy == "roundrobin"){
1052292SN/A        commitPolicy = RoundRobin;
1062292SN/A
1072292SN/A        //Set-Up Priority List
1082292SN/A        for (int tid=0; tid < numThreads; tid++) {
1092292SN/A            priority_list.push_back(tid);
1102292SN/A        }
1112292SN/A
1124329Sktlim@umich.edu        DPRINTF(Commit,"Commit Policy set to Round Robin.");
1132292SN/A    } else if (policy == "oldestready"){
1142292SN/A        commitPolicy = OldestReady;
1152292SN/A
1164329Sktlim@umich.edu        DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
1172292SN/A    } else {
1182292SN/A        assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
1192292SN/A               "RoundRobin,OldestReady}");
1202292SN/A    }
1212292SN/A
1222292SN/A    for (int i=0; i < numThreads; i++) {
1232292SN/A        commitStatus[i] = Idle;
1242292SN/A        changedROBNumEntries[i] = false;
1254035Sktlim@umich.edu        checkEmptyROB[i] = false;
1264035Sktlim@umich.edu        trapInFlight[i] = false;
1274035Sktlim@umich.edu        committedStores[i] = false;
1282292SN/A        trapSquash[i] = false;
1292680Sktlim@umich.edu        tcSquash[i] = false;
1304636Sgblack@eecs.umich.edu        microPC[i] = nextMicroPC[i] = PC[i] = nextPC[i] = nextNPC[i] = 0;
1312292SN/A    }
1323640Sktlim@umich.edu#if FULL_SYSTEM
1333640Sktlim@umich.edu    interrupt = NoFault;
1343640Sktlim@umich.edu#endif
1352292SN/A}
1362292SN/A
1372292SN/Atemplate <class Impl>
1382292SN/Astd::string
1392292SN/ADefaultCommit<Impl>::name() const
1402292SN/A{
1412292SN/A    return cpu->name() + ".commit";
1422292SN/A}
1432292SN/A
1442292SN/Atemplate <class Impl>
1452292SN/Avoid
1462292SN/ADefaultCommit<Impl>::regStats()
1472132SN/A{
1482301SN/A    using namespace Stats;
1491062SN/A    commitCommittedInsts
1501062SN/A        .name(name() + ".commitCommittedInsts")
1511062SN/A        .desc("The number of committed instructions")
1521062SN/A        .prereq(commitCommittedInsts);
1531062SN/A    commitSquashedInsts
1541062SN/A        .name(name() + ".commitSquashedInsts")
1551062SN/A        .desc("The number of squashed insts skipped by commit")
1561062SN/A        .prereq(commitSquashedInsts);
1571062SN/A    commitSquashEvents
1581062SN/A        .name(name() + ".commitSquashEvents")
1591062SN/A        .desc("The number of times commit is told to squash")
1601062SN/A        .prereq(commitSquashEvents);
1611062SN/A    commitNonSpecStalls
1621062SN/A        .name(name() + ".commitNonSpecStalls")
1631062SN/A        .desc("The number of times commit has been forced to stall to "
1641062SN/A              "communicate backwards")
1651062SN/A        .prereq(commitNonSpecStalls);
1661062SN/A    branchMispredicts
1671062SN/A        .name(name() + ".branchMispredicts")
1681062SN/A        .desc("The number of times a branch was mispredicted")
1691062SN/A        .prereq(branchMispredicts);
1702292SN/A    numCommittedDist
1711062SN/A        .init(0,commitWidth,1)
1721062SN/A        .name(name() + ".COM:committed_per_cycle")
1731062SN/A        .desc("Number of insts commited each cycle")
1741062SN/A        .flags(Stats::pdf)
1751062SN/A        ;
1762301SN/A
1772316SN/A    statComInst
1782301SN/A        .init(cpu->number_of_threads)
1792301SN/A        .name(name() + ".COM:count")
1802301SN/A        .desc("Number of instructions committed")
1812301SN/A        .flags(total)
1822301SN/A        ;
1832301SN/A
1842316SN/A    statComSwp
1852301SN/A        .init(cpu->number_of_threads)
1862301SN/A        .name(name() + ".COM:swp_count")
1872301SN/A        .desc("Number of s/w prefetches committed")
1882301SN/A        .flags(total)
1892301SN/A        ;
1902301SN/A
1912316SN/A    statComRefs
1922301SN/A        .init(cpu->number_of_threads)
1932301SN/A        .name(name() +  ".COM:refs")
1942301SN/A        .desc("Number of memory references committed")
1952301SN/A        .flags(total)
1962301SN/A        ;
1972301SN/A
1982316SN/A    statComLoads
1992301SN/A        .init(cpu->number_of_threads)
2002301SN/A        .name(name() +  ".COM:loads")
2012301SN/A        .desc("Number of loads committed")
2022301SN/A        .flags(total)
2032301SN/A        ;
2042301SN/A
2052316SN/A    statComMembars
2062301SN/A        .init(cpu->number_of_threads)
2072301SN/A        .name(name() +  ".COM:membars")
2082301SN/A        .desc("Number of memory barriers committed")
2092301SN/A        .flags(total)
2102301SN/A        ;
2112301SN/A
2122316SN/A    statComBranches
2132301SN/A        .init(cpu->number_of_threads)
2142301SN/A        .name(name() + ".COM:branches")
2152301SN/A        .desc("Number of branches committed")
2162301SN/A        .flags(total)
2172301SN/A        ;
2182301SN/A
2192316SN/A    commitEligible
2202301SN/A        .init(cpu->number_of_threads)
2212301SN/A        .name(name() + ".COM:bw_limited")
2222301SN/A        .desc("number of insts not committed due to BW limits")
2232301SN/A        .flags(total)
2242301SN/A        ;
2252301SN/A
2262316SN/A    commitEligibleSamples
2272301SN/A        .name(name() + ".COM:bw_lim_events")
2282301SN/A        .desc("number cycles where commit BW limit reached")
2292301SN/A        ;
2301062SN/A}
2311062SN/A
2321062SN/Atemplate <class Impl>
2331062SN/Avoid
2342980Sgblack@eecs.umich.eduDefaultCommit<Impl>::setThreads(std::vector<Thread *> &threads)
2352292SN/A{
2362292SN/A    thread = threads;
2372292SN/A}
2382292SN/A
2392292SN/Atemplate <class Impl>
2402292SN/Avoid
2412292SN/ADefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
2421060SN/A{
2431060SN/A    timeBuffer = tb_ptr;
2441060SN/A
2451060SN/A    // Setup wire to send information back to IEW.
2461060SN/A    toIEW = timeBuffer->getWire(0);
2471060SN/A
2481060SN/A    // Setup wire to read data from IEW (for the ROB).
2491060SN/A    robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
2501060SN/A}
2511060SN/A
2521061SN/Atemplate <class Impl>
2531060SN/Avoid
2542292SN/ADefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
2552292SN/A{
2562292SN/A    fetchQueue = fq_ptr;
2572292SN/A
2582292SN/A    // Setup wire to get instructions from rename (for the ROB).
2592292SN/A    fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
2602292SN/A}
2612292SN/A
2622292SN/Atemplate <class Impl>
2632292SN/Avoid
2642292SN/ADefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
2651060SN/A{
2661060SN/A    renameQueue = rq_ptr;
2671060SN/A
2681060SN/A    // Setup wire to get instructions from rename (for the ROB).
2691060SN/A    fromRename = renameQueue->getWire(-renameToROBDelay);
2701060SN/A}
2711060SN/A
2721061SN/Atemplate <class Impl>
2731060SN/Avoid
2742292SN/ADefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
2751060SN/A{
2761060SN/A    iewQueue = iq_ptr;
2771060SN/A
2781060SN/A    // Setup wire to get instructions from IEW.
2791060SN/A    fromIEW = iewQueue->getWire(-iewToCommitDelay);
2801060SN/A}
2811060SN/A
2821061SN/Atemplate <class Impl>
2831060SN/Avoid
2842292SN/ADefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
2852292SN/A{
2862292SN/A    iewStage = iew_stage;
2872292SN/A}
2882292SN/A
2892292SN/Atemplate<class Impl>
2902292SN/Avoid
2912980Sgblack@eecs.umich.eduDefaultCommit<Impl>::setActiveThreads(std::list<unsigned> *at_ptr)
2922292SN/A{
2932292SN/A    activeThreads = at_ptr;
2942292SN/A}
2952292SN/A
2962292SN/Atemplate <class Impl>
2972292SN/Avoid
2982292SN/ADefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
2992292SN/A{
3002292SN/A    for (int i=0; i < numThreads; i++) {
3012292SN/A        renameMap[i] = &rm_ptr[i];
3022292SN/A    }
3032292SN/A}
3042292SN/A
3052292SN/Atemplate <class Impl>
3062292SN/Avoid
3072292SN/ADefaultCommit<Impl>::setROB(ROB *rob_ptr)
3081060SN/A{
3091060SN/A    rob = rob_ptr;
3101060SN/A}
3111060SN/A
3121061SN/Atemplate <class Impl>
3131060SN/Avoid
3142292SN/ADefaultCommit<Impl>::initStage()
3151060SN/A{
3162292SN/A    rob->setActiveThreads(activeThreads);
3172292SN/A    rob->resetEntries();
3181060SN/A
3192292SN/A    // Broadcast the number of free entries.
3202292SN/A    for (int i=0; i < numThreads; i++) {
3212292SN/A        toIEW->commitInfo[i].usedROB = true;
3222292SN/A        toIEW->commitInfo[i].freeROBEntries = rob->numFreeEntries(i);
3234035Sktlim@umich.edu        toIEW->commitInfo[i].emptyROB = true;
3241060SN/A    }
3251060SN/A
3264329Sktlim@umich.edu    // Commit must broadcast the number of free entries it has at the
3274329Sktlim@umich.edu    // start of the simulation, so it starts as active.
3284329Sktlim@umich.edu    cpu->activateStage(O3CPU::CommitIdx);
3294329Sktlim@umich.edu
3302292SN/A    cpu->activityThisCycle();
3315100Ssaidi@eecs.umich.edu    trapLatency = cpu->ticks(trapLatency);
3321060SN/A}
3331060SN/A
3341061SN/Atemplate <class Impl>
3352863Sktlim@umich.edubool
3362843Sktlim@umich.eduDefaultCommit<Impl>::drain()
3371060SN/A{
3382843Sktlim@umich.edu    drainPending = true;
3392863Sktlim@umich.edu
3402863Sktlim@umich.edu    return false;
3412316SN/A}
3422316SN/A
3432316SN/Atemplate <class Impl>
3442316SN/Avoid
3452843Sktlim@umich.eduDefaultCommit<Impl>::switchOut()
3462316SN/A{
3472316SN/A    switchedOut = true;
3482843Sktlim@umich.edu    drainPending = false;
3492307SN/A    rob->switchOut();
3502307SN/A}
3512307SN/A
3522307SN/Atemplate <class Impl>
3532307SN/Avoid
3542843Sktlim@umich.eduDefaultCommit<Impl>::resume()
3552843Sktlim@umich.edu{
3562864Sktlim@umich.edu    drainPending = false;
3572843Sktlim@umich.edu}
3582843Sktlim@umich.edu
3592843Sktlim@umich.edutemplate <class Impl>
3602843Sktlim@umich.eduvoid
3612307SN/ADefaultCommit<Impl>::takeOverFrom()
3622307SN/A{
3632316SN/A    switchedOut = false;
3642307SN/A    _status = Active;
3652307SN/A    _nextStatus = Inactive;
3662307SN/A    for (int i=0; i < numThreads; i++) {
3672307SN/A        commitStatus[i] = Idle;
3682307SN/A        changedROBNumEntries[i] = false;
3692307SN/A        trapSquash[i] = false;
3702680Sktlim@umich.edu        tcSquash[i] = false;
3712307SN/A    }
3722307SN/A    squashCounter = 0;
3732307SN/A    rob->takeOverFrom();
3742307SN/A}
3752307SN/A
3762307SN/Atemplate <class Impl>
3772307SN/Avoid
3782292SN/ADefaultCommit<Impl>::updateStatus()
3792132SN/A{
3802316SN/A    // reset ROB changed variable
3813867Sbinkertn@umich.edu    std::list<unsigned>::iterator threads = activeThreads->begin();
3823867Sbinkertn@umich.edu    std::list<unsigned>::iterator end = activeThreads->end();
3833867Sbinkertn@umich.edu
3843867Sbinkertn@umich.edu    while (threads != end) {
3852316SN/A        unsigned tid = *threads++;
3863867Sbinkertn@umich.edu
3872316SN/A        changedROBNumEntries[tid] = false;
3882316SN/A
3892316SN/A        // Also check if any of the threads has a trap pending
3902316SN/A        if (commitStatus[tid] == TrapPending ||
3912316SN/A            commitStatus[tid] == FetchTrapPending) {
3922316SN/A            _nextStatus = Active;
3932316SN/A        }
3942292SN/A    }
3952292SN/A
3962292SN/A    if (_nextStatus == Inactive && _status == Active) {
3972292SN/A        DPRINTF(Activity, "Deactivating stage.\n");
3982733Sktlim@umich.edu        cpu->deactivateStage(O3CPU::CommitIdx);
3992292SN/A    } else if (_nextStatus == Active && _status == Inactive) {
4002292SN/A        DPRINTF(Activity, "Activating stage.\n");
4012733Sktlim@umich.edu        cpu->activateStage(O3CPU::CommitIdx);
4022292SN/A    }
4032292SN/A
4042292SN/A    _status = _nextStatus;
4052292SN/A}
4062292SN/A
4072292SN/Atemplate <class Impl>
4082292SN/Avoid
4092292SN/ADefaultCommit<Impl>::setNextStatus()
4102292SN/A{
4112292SN/A    int squashes = 0;
4122292SN/A
4133867Sbinkertn@umich.edu    std::list<unsigned>::iterator threads = activeThreads->begin();
4143867Sbinkertn@umich.edu    std::list<unsigned>::iterator end = activeThreads->end();
4152292SN/A
4163867Sbinkertn@umich.edu    while (threads != end) {
4172292SN/A        unsigned tid = *threads++;
4182292SN/A
4192292SN/A        if (commitStatus[tid] == ROBSquashing) {
4202292SN/A            squashes++;
4212292SN/A        }
4222292SN/A    }
4232292SN/A
4242702Sktlim@umich.edu    squashCounter = squashes;
4252292SN/A
4262292SN/A    // If commit is currently squashing, then it will have activity for the
4272292SN/A    // next cycle. Set its next status as active.
4282292SN/A    if (squashCounter) {
4292292SN/A        _nextStatus = Active;
4302292SN/A    }
4312292SN/A}
4322292SN/A
4332292SN/Atemplate <class Impl>
4342292SN/Abool
4352292SN/ADefaultCommit<Impl>::changedROBEntries()
4362292SN/A{
4373867Sbinkertn@umich.edu    std::list<unsigned>::iterator threads = activeThreads->begin();
4383867Sbinkertn@umich.edu    std::list<unsigned>::iterator end = activeThreads->end();
4392292SN/A
4403867Sbinkertn@umich.edu    while (threads != end) {
4412292SN/A        unsigned tid = *threads++;
4422292SN/A
4432292SN/A        if (changedROBNumEntries[tid]) {
4442292SN/A            return true;
4452292SN/A        }
4462292SN/A    }
4472292SN/A
4482292SN/A    return false;
4492292SN/A}
4502292SN/A
4512292SN/Atemplate <class Impl>
4522292SN/Aunsigned
4532292SN/ADefaultCommit<Impl>::numROBFreeEntries(unsigned tid)
4542292SN/A{
4552292SN/A    return rob->numFreeEntries(tid);
4562292SN/A}
4572292SN/A
4582292SN/Atemplate <class Impl>
4592292SN/Avoid
4602292SN/ADefaultCommit<Impl>::generateTrapEvent(unsigned tid)
4612292SN/A{
4622292SN/A    DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
4632292SN/A
4642292SN/A    TrapEvent *trap = new TrapEvent(this, tid);
4652292SN/A
4665606Snate@binkert.org    cpu->schedule(trap, curTick + trapLatency);
4674035Sktlim@umich.edu    trapInFlight[tid] = true;
4682292SN/A}
4692292SN/A
4702292SN/Atemplate <class Impl>
4712292SN/Avoid
4722680Sktlim@umich.eduDefaultCommit<Impl>::generateTCEvent(unsigned tid)
4732292SN/A{
4744035Sktlim@umich.edu    assert(!trapInFlight[tid]);
4752680Sktlim@umich.edu    DPRINTF(Commit, "Generating TC squash event for [tid:%i]\n", tid);
4762292SN/A
4772680Sktlim@umich.edu    tcSquash[tid] = true;
4782292SN/A}
4792292SN/A
4802292SN/Atemplate <class Impl>
4812292SN/Avoid
4822316SN/ADefaultCommit<Impl>::squashAll(unsigned tid)
4832292SN/A{
4842292SN/A    // If we want to include the squashing instruction in the squash,
4852292SN/A    // then use one older sequence number.
4862292SN/A    // Hopefully this doesn't mess things up.  Basically I want to squash
4872292SN/A    // all instructions of this thread.
4882292SN/A    InstSeqNum squashed_inst = rob->isEmpty() ?
4894035Sktlim@umich.edu        0 : rob->readHeadInst(tid)->seqNum - 1;
4902292SN/A
4912292SN/A    // All younger instructions will be squashed. Set the sequence
4922292SN/A    // number as the youngest instruction in the ROB (0 in this case.
4932292SN/A    // Hopefully nothing breaks.)
4942292SN/A    youngestSeqNum[tid] = 0;
4952292SN/A
4962292SN/A    rob->squash(squashed_inst, tid);
4972292SN/A    changedROBNumEntries[tid] = true;
4982292SN/A
4992292SN/A    // Send back the sequence number of the squashed instruction.
5002292SN/A    toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
5012292SN/A
5022292SN/A    // Send back the squash signal to tell stages that they should
5032292SN/A    // squash.
5042292SN/A    toIEW->commitInfo[tid].squash = true;
5052292SN/A
5062292SN/A    // Send back the rob squashing signal so other stages know that
5072292SN/A    // the ROB is in the process of squashing.
5082292SN/A    toIEW->commitInfo[tid].robSquashing = true;
5092292SN/A
5102292SN/A    toIEW->commitInfo[tid].branchMispredict = false;
5112292SN/A
5122316SN/A    toIEW->commitInfo[tid].nextPC = PC[tid];
5133795Sgblack@eecs.umich.edu    toIEW->commitInfo[tid].nextNPC = nextPC[tid];
5144636Sgblack@eecs.umich.edu    toIEW->commitInfo[tid].nextMicroPC = nextMicroPC[tid];
5152316SN/A}
5162292SN/A
5172316SN/Atemplate <class Impl>
5182316SN/Avoid
5192316SN/ADefaultCommit<Impl>::squashFromTrap(unsigned tid)
5202316SN/A{
5212316SN/A    squashAll(tid);
5222316SN/A
5232316SN/A    DPRINTF(Commit, "Squashing from trap, restarting at PC %#x\n", PC[tid]);
5242316SN/A
5252316SN/A    thread[tid]->trapPending = false;
5262316SN/A    thread[tid]->inSyscall = false;
5274035Sktlim@umich.edu    trapInFlight[tid] = false;
5282316SN/A
5292316SN/A    trapSquash[tid] = false;
5302316SN/A
5312316SN/A    commitStatus[tid] = ROBSquashing;
5322316SN/A    cpu->activityThisCycle();
5332316SN/A}
5342316SN/A
5352316SN/Atemplate <class Impl>
5362316SN/Avoid
5372680Sktlim@umich.eduDefaultCommit<Impl>::squashFromTC(unsigned tid)
5382316SN/A{
5392316SN/A    squashAll(tid);
5402292SN/A
5412680Sktlim@umich.edu    DPRINTF(Commit, "Squashing from TC, restarting at PC %#x\n", PC[tid]);
5422292SN/A
5432292SN/A    thread[tid]->inSyscall = false;
5442292SN/A    assert(!thread[tid]->trapPending);
5452316SN/A
5462292SN/A    commitStatus[tid] = ROBSquashing;
5472292SN/A    cpu->activityThisCycle();
5482292SN/A
5492680Sktlim@umich.edu    tcSquash[tid] = false;
5502292SN/A}
5512292SN/A
5522292SN/Atemplate <class Impl>
5532292SN/Avoid
5542292SN/ADefaultCommit<Impl>::tick()
5552292SN/A{
5562292SN/A    wroteToTimeBuffer = false;
5572292SN/A    _nextStatus = Inactive;
5582292SN/A
5592843Sktlim@umich.edu    if (drainPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
5602843Sktlim@umich.edu        cpu->signalDrained();
5612843Sktlim@umich.edu        drainPending = false;
5622316SN/A        return;
5632316SN/A    }
5642316SN/A
5653867Sbinkertn@umich.edu    if (activeThreads->empty())
5662875Sksewell@umich.edu        return;
5672875Sksewell@umich.edu
5683867Sbinkertn@umich.edu    std::list<unsigned>::iterator threads = activeThreads->begin();
5693867Sbinkertn@umich.edu    std::list<unsigned>::iterator end = activeThreads->end();
5702292SN/A
5712316SN/A    // Check if any of the threads are done squashing.  Change the
5722316SN/A    // status if they are done.
5733867Sbinkertn@umich.edu    while (threads != end) {
5742292SN/A        unsigned tid = *threads++;
5752292SN/A
5764035Sktlim@umich.edu        // Clear the bit saying if the thread has committed stores
5774035Sktlim@umich.edu        // this cycle.
5784035Sktlim@umich.edu        committedStores[tid] = false;
5794035Sktlim@umich.edu
5802292SN/A        if (commitStatus[tid] == ROBSquashing) {
5812292SN/A
5822292SN/A            if (rob->isDoneSquashing(tid)) {
5832292SN/A                commitStatus[tid] = Running;
5842292SN/A            } else {
5852292SN/A                DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
5862877Sksewell@umich.edu                        " insts this cycle.\n", tid);
5872702Sktlim@umich.edu                rob->doSquash(tid);
5882702Sktlim@umich.edu                toIEW->commitInfo[tid].robSquashing = true;
5892702Sktlim@umich.edu                wroteToTimeBuffer = true;
5902292SN/A            }
5912292SN/A        }
5922292SN/A    }
5932292SN/A
5942292SN/A    commit();
5952292SN/A
5962292SN/A    markCompletedInsts();
5972292SN/A
5983867Sbinkertn@umich.edu    threads = activeThreads->begin();
5992292SN/A
6003867Sbinkertn@umich.edu    while (threads != end) {
6012292SN/A        unsigned tid = *threads++;
6022292SN/A
6032292SN/A        if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
6042292SN/A            // The ROB has more instructions it can commit. Its next status
6052292SN/A            // will be active.
6062292SN/A            _nextStatus = Active;
6072292SN/A
6082292SN/A            DynInstPtr inst = rob->readHeadInst(tid);
6092292SN/A
6102292SN/A            DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %#x is head of"
6112292SN/A                    " ROB and ready to commit\n",
6122292SN/A                    tid, inst->seqNum, inst->readPC());
6132292SN/A
6142292SN/A        } else if (!rob->isEmpty(tid)) {
6152292SN/A            DynInstPtr inst = rob->readHeadInst(tid);
6162292SN/A
6172292SN/A            DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
6182292SN/A                    "%#x is head of ROB and not ready\n",
6192292SN/A                    tid, inst->seqNum, inst->readPC());
6202292SN/A        }
6212292SN/A
6222292SN/A        DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
6232292SN/A                tid, rob->countInsts(tid), rob->numFreeEntries(tid));
6242292SN/A    }
6252292SN/A
6262292SN/A
6272292SN/A    if (wroteToTimeBuffer) {
6282316SN/A        DPRINTF(Activity, "Activity This Cycle.\n");
6292292SN/A        cpu->activityThisCycle();
6302292SN/A    }
6312292SN/A
6322292SN/A    updateStatus();
6332292SN/A}
6342292SN/A
6354035Sktlim@umich.edu#if FULL_SYSTEM
6362292SN/Atemplate <class Impl>
6372292SN/Avoid
6384035Sktlim@umich.eduDefaultCommit<Impl>::handleInterrupt()
6392292SN/A{
6403640Sktlim@umich.edu    if (interrupt != NoFault) {
6412316SN/A        // Wait until the ROB is empty and all stores have drained in
6422316SN/A        // order to enter the interrupt.
6432292SN/A        if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
6443633Sktlim@umich.edu            // Squash or record that I need to squash this cycle if
6453633Sktlim@umich.edu            // an interrupt needed to be handled.
6463633Sktlim@umich.edu            DPRINTF(Commit, "Interrupt detected.\n");
6473633Sktlim@umich.edu
6484035Sktlim@umich.edu            // Clear the interrupt now that it's going to be handled
6494035Sktlim@umich.edu            toIEW->commitInfo[0].clearInterrupt = true;
6504035Sktlim@umich.edu
6512292SN/A            assert(!thread[0]->inSyscall);
6522292SN/A            thread[0]->inSyscall = true;
6532292SN/A
6543633Sktlim@umich.edu            // CPU will handle interrupt.
6553640Sktlim@umich.edu            cpu->processInterrupts(interrupt);
6562292SN/A
6573633Sktlim@umich.edu            thread[0]->inSyscall = false;
6583633Sktlim@umich.edu
6592292SN/A            commitStatus[0] = TrapPending;
6602292SN/A
6612292SN/A            // Generate trap squash event.
6622292SN/A            generateTrapEvent(0);
6632292SN/A
6643640Sktlim@umich.edu            interrupt = NoFault;
6652292SN/A        } else {
6662292SN/A            DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
6672292SN/A        }
6684035Sktlim@umich.edu    } else if (commitStatus[0] != TrapPending &&
6695704Snate@binkert.org               cpu->checkInterrupts(cpu->tcBase(0)) &&
6704035Sktlim@umich.edu               !trapSquash[0] &&
6714035Sktlim@umich.edu               !tcSquash[0]) {
6723640Sktlim@umich.edu        // Process interrupts if interrupts are enabled, not in PAL
6733640Sktlim@umich.edu        // mode, and no other traps or external squashes are currently
6743640Sktlim@umich.edu        // pending.
6753640Sktlim@umich.edu        // @todo: Allow other threads to handle interrupts.
6763640Sktlim@umich.edu
6773640Sktlim@umich.edu        // Get any interrupt that happened
6783640Sktlim@umich.edu        interrupt = cpu->getInterrupts();
6793640Sktlim@umich.edu
6803640Sktlim@umich.edu        if (interrupt != NoFault) {
6813640Sktlim@umich.edu            // Tell fetch that there is an interrupt pending.  This
6823640Sktlim@umich.edu            // will make fetch wait until it sees a non PAL-mode PC,
6833640Sktlim@umich.edu            // at which point it stops fetching instructions.
6843640Sktlim@umich.edu            toIEW->commitInfo[0].interruptPending = true;
6853640Sktlim@umich.edu        }
6861060SN/A    }
6874035Sktlim@umich.edu}
6884035Sktlim@umich.edu#endif // FULL_SYSTEM
6893634Sktlim@umich.edu
6904035Sktlim@umich.edutemplate <class Impl>
6914035Sktlim@umich.eduvoid
6924035Sktlim@umich.eduDefaultCommit<Impl>::commit()
6934035Sktlim@umich.edu{
6944035Sktlim@umich.edu
6954035Sktlim@umich.edu#if FULL_SYSTEM
6964035Sktlim@umich.edu    // Check for any interrupt, and start processing it.  Or if we
6974035Sktlim@umich.edu    // have an outstanding interrupt and are at a point when it is
6984035Sktlim@umich.edu    // valid to take an interrupt, process it.
6995704Snate@binkert.org    if (cpu->checkInterrupts(cpu->tcBase(0))) {
7004035Sktlim@umich.edu        handleInterrupt();
7014035Sktlim@umich.edu    }
7021060SN/A#endif // FULL_SYSTEM
7031060SN/A
7041060SN/A    ////////////////////////////////////
7052316SN/A    // Check for any possible squashes, handle them first
7061060SN/A    ////////////////////////////////////
7073867Sbinkertn@umich.edu    std::list<unsigned>::iterator threads = activeThreads->begin();
7083867Sbinkertn@umich.edu    std::list<unsigned>::iterator end = activeThreads->end();
7091060SN/A
7103867Sbinkertn@umich.edu    while (threads != end) {
7112292SN/A        unsigned tid = *threads++;
7121060SN/A
7132292SN/A        // Not sure which one takes priority.  I think if we have
7142292SN/A        // both, that's a bad sign.
7152292SN/A        if (trapSquash[tid] == true) {
7162680Sktlim@umich.edu            assert(!tcSquash[tid]);
7172292SN/A            squashFromTrap(tid);
7182680Sktlim@umich.edu        } else if (tcSquash[tid] == true) {
7194035Sktlim@umich.edu            assert(commitStatus[tid] != TrapPending);
7202680Sktlim@umich.edu            squashFromTC(tid);
7212292SN/A        }
7221061SN/A
7232292SN/A        // Squashed sequence number must be older than youngest valid
7242292SN/A        // instruction in the ROB. This prevents squashes from younger
7252292SN/A        // instructions overriding squashes from older instructions.
7262292SN/A        if (fromIEW->squash[tid] &&
7272292SN/A            commitStatus[tid] != TrapPending &&
7282292SN/A            fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
7291061SN/A
7302292SN/A            DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
7312292SN/A                    tid,
7322292SN/A                    fromIEW->mispredPC[tid],
7332292SN/A                    fromIEW->squashedSeqNum[tid]);
7341061SN/A
7352292SN/A            DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
7362292SN/A                    tid,
7372292SN/A                    fromIEW->nextPC[tid]);
7381061SN/A
7392292SN/A            commitStatus[tid] = ROBSquashing;
7401061SN/A
7412292SN/A            // If we want to include the squashing instruction in the squash,
7422292SN/A            // then use one older sequence number.
7432292SN/A            InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
7441062SN/A
7452935Sksewell@umich.edu            if (fromIEW->includeSquashInst[tid] == true) {
7462292SN/A                squashed_inst--;
7472935Sksewell@umich.edu            }
7484035Sktlim@umich.edu
7492292SN/A            // All younger instructions will be squashed. Set the sequence
7502292SN/A            // number as the youngest instruction in the ROB.
7512292SN/A            youngestSeqNum[tid] = squashed_inst;
7522292SN/A
7533093Sksewell@umich.edu            rob->squash(squashed_inst, tid);
7542292SN/A            changedROBNumEntries[tid] = true;
7552292SN/A
7562292SN/A            toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
7572292SN/A
7582292SN/A            toIEW->commitInfo[tid].squash = true;
7592292SN/A
7602292SN/A            // Send back the rob squashing signal so other stages know that
7612292SN/A            // the ROB is in the process of squashing.
7622292SN/A            toIEW->commitInfo[tid].robSquashing = true;
7632292SN/A
7642292SN/A            toIEW->commitInfo[tid].branchMispredict =
7652292SN/A                fromIEW->branchMispredict[tid];
7662292SN/A
7672292SN/A            toIEW->commitInfo[tid].branchTaken =
7682292SN/A                fromIEW->branchTaken[tid];
7692292SN/A
7702292SN/A            toIEW->commitInfo[tid].nextPC = fromIEW->nextPC[tid];
7713795Sgblack@eecs.umich.edu            toIEW->commitInfo[tid].nextNPC = fromIEW->nextNPC[tid];
7724636Sgblack@eecs.umich.edu            toIEW->commitInfo[tid].nextMicroPC = fromIEW->nextMicroPC[tid];
7732292SN/A
7742316SN/A            toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
7752292SN/A
7762292SN/A            if (toIEW->commitInfo[tid].branchMispredict) {
7772292SN/A                ++branchMispredicts;
7782292SN/A            }
7791062SN/A        }
7802292SN/A
7811060SN/A    }
7821060SN/A
7832292SN/A    setNextStatus();
7842292SN/A
7852292SN/A    if (squashCounter != numThreads) {
7861061SN/A        // If we're not currently squashing, then get instructions.
7871060SN/A        getInsts();
7881060SN/A
7891061SN/A        // Try to commit any instructions.
7901060SN/A        commitInsts();
7911060SN/A    }
7921060SN/A
7932292SN/A    //Check for any activity
7943867Sbinkertn@umich.edu    threads = activeThreads->begin();
7952292SN/A
7963867Sbinkertn@umich.edu    while (threads != end) {
7972292SN/A        unsigned tid = *threads++;
7982292SN/A
7992292SN/A        if (changedROBNumEntries[tid]) {
8002292SN/A            toIEW->commitInfo[tid].usedROB = true;
8012292SN/A            toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
8022292SN/A
8032292SN/A            wroteToTimeBuffer = true;
8042292SN/A            changedROBNumEntries[tid] = false;
8054035Sktlim@umich.edu            if (rob->isEmpty(tid))
8064035Sktlim@umich.edu                checkEmptyROB[tid] = true;
8072292SN/A        }
8084035Sktlim@umich.edu
8094035Sktlim@umich.edu        // ROB is only considered "empty" for previous stages if: a)
8104035Sktlim@umich.edu        // ROB is empty, b) there are no outstanding stores, c) IEW
8114035Sktlim@umich.edu        // stage has received any information regarding stores that
8124035Sktlim@umich.edu        // committed.
8134035Sktlim@umich.edu        // c) is checked by making sure to not consider the ROB empty
8144035Sktlim@umich.edu        // on the same cycle as when stores have been committed.
8154035Sktlim@umich.edu        // @todo: Make this handle multi-cycle communication between
8164035Sktlim@umich.edu        // commit and IEW.
8174035Sktlim@umich.edu        if (checkEmptyROB[tid] && rob->isEmpty(tid) &&
8185557Sktlim@umich.edu            !iewStage->hasStoresToWB(tid) && !committedStores[tid]) {
8194035Sktlim@umich.edu            checkEmptyROB[tid] = false;
8204035Sktlim@umich.edu            toIEW->commitInfo[tid].usedROB = true;
8214035Sktlim@umich.edu            toIEW->commitInfo[tid].emptyROB = true;
8224035Sktlim@umich.edu            toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
8234035Sktlim@umich.edu            wroteToTimeBuffer = true;
8244035Sktlim@umich.edu        }
8254035Sktlim@umich.edu
8261060SN/A    }
8271060SN/A}
8281060SN/A
8291061SN/Atemplate <class Impl>
8301060SN/Avoid
8312292SN/ADefaultCommit<Impl>::commitInsts()
8321060SN/A{
8331060SN/A    ////////////////////////////////////
8341060SN/A    // Handle commit
8352316SN/A    // Note that commit will be handled prior to putting new
8362316SN/A    // instructions in the ROB so that the ROB only tries to commit
8372316SN/A    // instructions it has in this current cycle, and not instructions
8382316SN/A    // it is writing in during this cycle.  Can't commit and squash
8392316SN/A    // things at the same time...
8401060SN/A    ////////////////////////////////////
8411060SN/A
8422292SN/A    DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
8431060SN/A
8441060SN/A    unsigned num_committed = 0;
8451060SN/A
8462292SN/A    DynInstPtr head_inst;
8472316SN/A
8481060SN/A    // Commit as many instructions as possible until the commit bandwidth
8491060SN/A    // limit is reached, or it becomes impossible to commit any more.
8502292SN/A    while (num_committed < commitWidth) {
8512292SN/A        int commit_thread = getCommittingThread();
8521060SN/A
8532292SN/A        if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
8542292SN/A            break;
8552292SN/A
8562292SN/A        head_inst = rob->readHeadInst(commit_thread);
8572292SN/A
8582292SN/A        int tid = head_inst->threadNumber;
8592292SN/A
8602292SN/A        assert(tid == commit_thread);
8612292SN/A
8622292SN/A        DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
8632292SN/A                head_inst->seqNum, tid);
8642132SN/A
8652316SN/A        // If the head instruction is squashed, it is ready to retire
8662316SN/A        // (be removed from the ROB) at any time.
8671060SN/A        if (head_inst->isSquashed()) {
8681060SN/A
8692292SN/A            DPRINTF(Commit, "Retiring squashed instruction from "
8701060SN/A                    "ROB.\n");
8711060SN/A
8722292SN/A            rob->retireHead(commit_thread);
8731060SN/A
8741062SN/A            ++commitSquashedInsts;
8751062SN/A
8762292SN/A            // Record that the number of ROB entries has changed.
8772292SN/A            changedROBNumEntries[tid] = true;
8781060SN/A        } else {
8792292SN/A            PC[tid] = head_inst->readPC();
8802292SN/A            nextPC[tid] = head_inst->readNextPC();
8812935Sksewell@umich.edu            nextNPC[tid] = head_inst->readNextNPC();
8824636Sgblack@eecs.umich.edu            nextMicroPC[tid] = head_inst->readNextMicroPC();
8832292SN/A
8841060SN/A            // Increment the total number of non-speculative instructions
8851060SN/A            // executed.
8861060SN/A            // Hack for now: it really shouldn't happen until after the
8871061SN/A            // commit is deemed to be successful, but this count is needed
8881061SN/A            // for syscalls.
8892292SN/A            thread[tid]->funcExeInst++;
8901060SN/A
8911060SN/A            // Try to commit the head instruction.
8921060SN/A            bool commit_success = commitHead(head_inst, num_committed);
8931060SN/A
8941062SN/A            if (commit_success) {
8951060SN/A                ++num_committed;
8961060SN/A
8972292SN/A                changedROBNumEntries[tid] = true;
8982292SN/A
8992292SN/A                // Set the doneSeqNum to the youngest committed instruction.
9002292SN/A                toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
9011060SN/A
9021062SN/A                ++commitCommittedInsts;
9031062SN/A
9042292SN/A                // To match the old model, don't count nops and instruction
9052292SN/A                // prefetches towards the total commit count.
9062292SN/A                if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
9072292SN/A                    cpu->instDone(tid);
9081062SN/A                }
9092292SN/A
9102292SN/A                PC[tid] = nextPC[tid];
9112935Sksewell@umich.edu                nextPC[tid] = nextNPC[tid];
9122935Sksewell@umich.edu                nextNPC[tid] = nextNPC[tid] + sizeof(TheISA::MachInst);
9134636Sgblack@eecs.umich.edu                microPC[tid] = nextMicroPC[tid];
9144636Sgblack@eecs.umich.edu                nextMicroPC[tid] = microPC[tid] + 1;
9152935Sksewell@umich.edu
9162292SN/A                int count = 0;
9172292SN/A                Addr oldpc;
9185108Sgblack@eecs.umich.edu                // Debug statement.  Checks to make sure we're not
9195108Sgblack@eecs.umich.edu                // currently updating state while handling PC events.
9205108Sgblack@eecs.umich.edu                assert(!thread[tid]->inSyscall && !thread[tid]->trapPending);
9212292SN/A                do {
9222292SN/A                    oldpc = PC[tid];
9235108Sgblack@eecs.umich.edu                    cpu->system->pcEventQueue.service(thread[tid]->getTC());
9242292SN/A                    count++;
9252292SN/A                } while (oldpc != PC[tid]);
9262292SN/A                if (count > 1) {
9275108Sgblack@eecs.umich.edu                    DPRINTF(Commit,
9285108Sgblack@eecs.umich.edu                            "PC skip function event, stopping commit\n");
9292292SN/A                    break;
9302292SN/A                }
9311060SN/A            } else {
9322292SN/A                DPRINTF(Commit, "Unable to commit head instruction PC:%#x "
9332292SN/A                        "[tid:%i] [sn:%i].\n",
9342292SN/A                        head_inst->readPC(), tid ,head_inst->seqNum);
9351060SN/A                break;
9361060SN/A            }
9371060SN/A        }
9381060SN/A    }
9391062SN/A
9401063SN/A    DPRINTF(CommitRate, "%i\n", num_committed);
9412292SN/A    numCommittedDist.sample(num_committed);
9422307SN/A
9432307SN/A    if (num_committed == commitWidth) {
9442349SN/A        commitEligibleSamples++;
9452307SN/A    }
9461060SN/A}
9471060SN/A
9481061SN/Atemplate <class Impl>
9491060SN/Abool
9502292SN/ADefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
9511060SN/A{
9521060SN/A    assert(head_inst);
9531060SN/A
9542292SN/A    int tid = head_inst->threadNumber;
9552292SN/A
9562316SN/A    // If the instruction is not executed yet, then it will need extra
9572316SN/A    // handling.  Signal backwards that it should be executed.
9581061SN/A    if (!head_inst->isExecuted()) {
9591061SN/A        // Keep this number correct.  We have not yet actually executed
9601061SN/A        // and committed this instruction.
9612292SN/A        thread[tid]->funcExeInst--;
9621062SN/A
9632292SN/A        if (head_inst->isNonSpeculative() ||
9642348SN/A            head_inst->isStoreConditional() ||
9652292SN/A            head_inst->isMemBarrier() ||
9662292SN/A            head_inst->isWriteBarrier()) {
9672316SN/A
9682316SN/A            DPRINTF(Commit, "Encountered a barrier or non-speculative "
9692316SN/A                    "instruction [sn:%lli] at the head of the ROB, PC %#x.\n",
9702316SN/A                    head_inst->seqNum, head_inst->readPC());
9712316SN/A
9725557Sktlim@umich.edu            if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
9732292SN/A                DPRINTF(Commit, "Waiting for all stores to writeback.\n");
9742292SN/A                return false;
9752292SN/A            }
9762292SN/A
9772292SN/A            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
9781061SN/A
9791061SN/A            // Change the instruction so it won't try to commit again until
9801061SN/A            // it is executed.
9811061SN/A            head_inst->clearCanCommit();
9821061SN/A
9831062SN/A            ++commitNonSpecStalls;
9841062SN/A
9851061SN/A            return false;
9862292SN/A        } else if (head_inst->isLoad()) {
9875557Sktlim@umich.edu            if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
9884035Sktlim@umich.edu                DPRINTF(Commit, "Waiting for all stores to writeback.\n");
9894035Sktlim@umich.edu                return false;
9904035Sktlim@umich.edu            }
9914035Sktlim@umich.edu
9924035Sktlim@umich.edu            assert(head_inst->uncacheable());
9932292SN/A            DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %#x.\n",
9942292SN/A                    head_inst->seqNum, head_inst->readPC());
9952292SN/A
9962292SN/A            // Send back the non-speculative instruction's sequence
9972316SN/A            // number.  Tell the lsq to re-execute the load.
9982292SN/A            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
9992292SN/A            toIEW->commitInfo[tid].uncached = true;
10002292SN/A            toIEW->commitInfo[tid].uncachedLoad = head_inst;
10012292SN/A
10022292SN/A            head_inst->clearCanCommit();
10032292SN/A
10042292SN/A            return false;
10051061SN/A        } else {
10062292SN/A            panic("Trying to commit un-executed instruction "
10071061SN/A                  "of unknown type!\n");
10081061SN/A        }
10091060SN/A    }
10101060SN/A
10112316SN/A    if (head_inst->isThreadSync()) {
10122292SN/A        // Not handled for now.
10132316SN/A        panic("Thread sync instructions are not handled yet.\n");
10142132SN/A    }
10152132SN/A
10164035Sktlim@umich.edu    // Check if the instruction caused a fault.  If so, trap.
10174035Sktlim@umich.edu    Fault inst_fault = head_inst->getFault();
10184035Sktlim@umich.edu
10192316SN/A    // Stores mark themselves as completed.
10204035Sktlim@umich.edu    if (!head_inst->isStore() && inst_fault == NoFault) {
10212310SN/A        head_inst->setCompleted();
10222310SN/A    }
10232310SN/A
10242733Sktlim@umich.edu#if USE_CHECKER
10252316SN/A    // Use checker prior to updating anything due to traps or PC
10262316SN/A    // based events.
10272316SN/A    if (cpu->checker) {
10282732Sktlim@umich.edu        cpu->checker->verify(head_inst);
10291060SN/A    }
10302733Sktlim@umich.edu#endif
10311060SN/A
10322918Sktlim@umich.edu    // DTB will sometimes need the machine instruction for when
10332918Sktlim@umich.edu    // faults happen.  So we will set it here, prior to the DTB
10342918Sktlim@umich.edu    // possibly needing it for its fault.
10352918Sktlim@umich.edu    thread[tid]->setInst(
10362918Sktlim@umich.edu        static_cast<TheISA::MachInst>(head_inst->staticInst->machInst));
10372918Sktlim@umich.edu
10382112SN/A    if (inst_fault != NoFault) {
10392316SN/A        DPRINTF(Commit, "Inst [sn:%lli] PC %#x has a fault\n",
10402316SN/A                head_inst->seqNum, head_inst->readPC());
10412292SN/A
10425557Sktlim@umich.edu        if (iewStage->hasStoresToWB(tid) || inst_num > 0) {
10432316SN/A            DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
10442316SN/A            return false;
10452316SN/A        }
10462310SN/A
10474035Sktlim@umich.edu        head_inst->setCompleted();
10484035Sktlim@umich.edu
10492733Sktlim@umich.edu#if USE_CHECKER
10502316SN/A        if (cpu->checker && head_inst->isStore()) {
10512732Sktlim@umich.edu            cpu->checker->verify(head_inst);
10522316SN/A        }
10532733Sktlim@umich.edu#endif
10542292SN/A
10552316SN/A        assert(!thread[tid]->inSyscall);
10562292SN/A
10572316SN/A        // Mark that we're in state update mode so that the trap's
10582316SN/A        // execution doesn't generate extra squashes.
10592316SN/A        thread[tid]->inSyscall = true;
10602292SN/A
10612316SN/A        // Execute the trap.  Although it's slightly unrealistic in
10622316SN/A        // terms of timing (as it doesn't wait for the full timing of
10632316SN/A        // the trap event to complete before updating state), it's
10642316SN/A        // needed to update the state as soon as possible.  This
10652316SN/A        // prevents external agents from changing any specific state
10662316SN/A        // that the trap need.
10672316SN/A        cpu->trap(inst_fault, tid);
10682292SN/A
10692316SN/A        // Exit state update mode to avoid accidental updating.
10702316SN/A        thread[tid]->inSyscall = false;
10712292SN/A
10722316SN/A        commitStatus[tid] = TrapPending;
10732292SN/A
10744035Sktlim@umich.edu        if (head_inst->traceData) {
10754035Sktlim@umich.edu            head_inst->traceData->setFetchSeq(head_inst->seqNum);
10764035Sktlim@umich.edu            head_inst->traceData->setCPSeq(thread[tid]->numInst);
10774288Sktlim@umich.edu            head_inst->traceData->dump();
10784288Sktlim@umich.edu            delete head_inst->traceData;
10794035Sktlim@umich.edu            head_inst->traceData = NULL;
10804035Sktlim@umich.edu        }
10814035Sktlim@umich.edu
10822316SN/A        // Generate trap squash event.
10832316SN/A        generateTrapEvent(tid);
10842353SN/A//        warn("%lli fault (%d) handled @ PC %08p", curTick, inst_fault->name(), head_inst->readPC());
10852316SN/A        return false;
10861060SN/A    }
10871060SN/A
10882301SN/A    updateComInstStats(head_inst);
10892132SN/A
10902362SN/A#if FULL_SYSTEM
10912362SN/A    if (thread[tid]->profile) {
10923577Sgblack@eecs.umich.edu//        bool usermode = TheISA::inUserMode(thread[tid]->getTC());
10932362SN/A//        thread[tid]->profilePC = usermode ? 1 : head_inst->readPC();
10942362SN/A        thread[tid]->profilePC = head_inst->readPC();
10953126Sktlim@umich.edu        ProfileNode *node = thread[tid]->profile->consume(thread[tid]->getTC(),
10962362SN/A                                                          head_inst->staticInst);
10972362SN/A
10982362SN/A        if (node)
10992362SN/A            thread[tid]->profileNode = node;
11002362SN/A    }
11015953Ssaidi@eecs.umich.edu    if (CPA::available()) {
11025953Ssaidi@eecs.umich.edu        if (head_inst->isControl()) {
11035953Ssaidi@eecs.umich.edu            ThreadContext *tc = thread[tid]->getTC();
11045953Ssaidi@eecs.umich.edu            CPA::cpa()->swAutoBegin(tc, head_inst->readNextPC());
11055953Ssaidi@eecs.umich.edu        }
11065953Ssaidi@eecs.umich.edu    }
11072362SN/A#endif
11082362SN/A
11092132SN/A    if (head_inst->traceData) {
11102292SN/A        head_inst->traceData->setFetchSeq(head_inst->seqNum);
11112292SN/A        head_inst->traceData->setCPSeq(thread[tid]->numInst);
11124046Sbinkertn@umich.edu        head_inst->traceData->dump();
11134046Sbinkertn@umich.edu        delete head_inst->traceData;
11142292SN/A        head_inst->traceData = NULL;
11151060SN/A    }
11161060SN/A
11172292SN/A    // Update the commit rename map
11182292SN/A    for (int i = 0; i < head_inst->numDestRegs(); i++) {
11193771Sgblack@eecs.umich.edu        renameMap[tid]->setEntry(head_inst->flattenedDestRegIdx(i),
11202292SN/A                                 head_inst->renamedDestRegIdx(i));
11211060SN/A    }
11221062SN/A
11232353SN/A    if (head_inst->isCopy())
11242353SN/A        panic("Should not commit any copy instructions!");
11252353SN/A
11262292SN/A    // Finally clear the head ROB entry.
11272292SN/A    rob->retireHead(tid);
11281060SN/A
11294035Sktlim@umich.edu    // If this was a store, record it for this cycle.
11304035Sktlim@umich.edu    if (head_inst->isStore())
11314035Sktlim@umich.edu        committedStores[tid] = true;
11324035Sktlim@umich.edu
11331060SN/A    // Return true to indicate that we have committed an instruction.
11341060SN/A    return true;
11351060SN/A}
11361060SN/A
11371061SN/Atemplate <class Impl>
11381060SN/Avoid
11392292SN/ADefaultCommit<Impl>::getInsts()
11401060SN/A{
11412935Sksewell@umich.edu    DPRINTF(Commit, "Getting instructions from Rename stage.\n");
11422935Sksewell@umich.edu
11433093Sksewell@umich.edu    // Read any renamed instructions and place them into the ROB.
11443093Sksewell@umich.edu    int insts_to_process = std::min((int)renameWidth, fromRename->size);
11452965Sksewell@umich.edu
11462965Sksewell@umich.edu    for (int inst_num = 0; inst_num < insts_to_process; ++inst_num) {
11472965Sksewell@umich.edu        DynInstPtr inst;
11482965Sksewell@umich.edu
11493093Sksewell@umich.edu        inst = fromRename->insts[inst_num];
11502292SN/A        int tid = inst->threadNumber;
11512292SN/A
11522292SN/A        if (!inst->isSquashed() &&
11534035Sktlim@umich.edu            commitStatus[tid] != ROBSquashing &&
11544035Sktlim@umich.edu            commitStatus[tid] != TrapPending) {
11552292SN/A            changedROBNumEntries[tid] = true;
11562292SN/A
11572292SN/A            DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ROB.\n",
11582292SN/A                    inst->readPC(), inst->seqNum, tid);
11592292SN/A
11602292SN/A            rob->insertInst(inst);
11612292SN/A
11622292SN/A            assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
11632292SN/A
11642292SN/A            youngestSeqNum[tid] = inst->seqNum;
11651061SN/A        } else {
11662292SN/A            DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
11671061SN/A                    "squashed, skipping.\n",
11682292SN/A                    inst->readPC(), inst->seqNum, tid);
11691061SN/A        }
11701060SN/A    }
11712965Sksewell@umich.edu}
11722965Sksewell@umich.edu
11732965Sksewell@umich.edutemplate <class Impl>
11742965Sksewell@umich.eduvoid
11752965Sksewell@umich.eduDefaultCommit<Impl>::skidInsert()
11762965Sksewell@umich.edu{
11772965Sksewell@umich.edu    DPRINTF(Commit, "Attempting to any instructions from rename into "
11782965Sksewell@umich.edu            "skidBuffer.\n");
11792965Sksewell@umich.edu
11802965Sksewell@umich.edu    for (int inst_num = 0; inst_num < fromRename->size; ++inst_num) {
11812965Sksewell@umich.edu        DynInstPtr inst = fromRename->insts[inst_num];
11822965Sksewell@umich.edu
11832965Sksewell@umich.edu        if (!inst->isSquashed()) {
11842965Sksewell@umich.edu            DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ",
11853221Sktlim@umich.edu                    "skidBuffer.\n", inst->readPC(), inst->seqNum,
11863221Sktlim@umich.edu                    inst->threadNumber);
11872965Sksewell@umich.edu            skidBuffer.push(inst);
11882965Sksewell@umich.edu        } else {
11892965Sksewell@umich.edu            DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
11902965Sksewell@umich.edu                    "squashed, skipping.\n",
11913221Sktlim@umich.edu                    inst->readPC(), inst->seqNum, inst->threadNumber);
11922965Sksewell@umich.edu        }
11932965Sksewell@umich.edu    }
11941060SN/A}
11951060SN/A
11961061SN/Atemplate <class Impl>
11971060SN/Avoid
11982292SN/ADefaultCommit<Impl>::markCompletedInsts()
11991060SN/A{
12001060SN/A    // Grab completed insts out of the IEW instruction queue, and mark
12011060SN/A    // instructions completed within the ROB.
12021060SN/A    for (int inst_num = 0;
12031681SN/A         inst_num < fromIEW->size && fromIEW->insts[inst_num];
12041060SN/A         ++inst_num)
12051060SN/A    {
12062292SN/A        if (!fromIEW->insts[inst_num]->isSquashed()) {
12072316SN/A            DPRINTF(Commit, "[tid:%i]: Marking PC %#x, [sn:%lli] ready "
12082316SN/A                    "within ROB.\n",
12092292SN/A                    fromIEW->insts[inst_num]->threadNumber,
12102292SN/A                    fromIEW->insts[inst_num]->readPC(),
12112292SN/A                    fromIEW->insts[inst_num]->seqNum);
12121060SN/A
12132292SN/A            // Mark the instruction as ready to commit.
12142292SN/A            fromIEW->insts[inst_num]->setCanCommit();
12152292SN/A        }
12161060SN/A    }
12171060SN/A}
12181060SN/A
12191061SN/Atemplate <class Impl>
12202292SN/Abool
12212292SN/ADefaultCommit<Impl>::robDoneSquashing()
12221060SN/A{
12233867Sbinkertn@umich.edu    std::list<unsigned>::iterator threads = activeThreads->begin();
12243867Sbinkertn@umich.edu    std::list<unsigned>::iterator end = activeThreads->end();
12252292SN/A
12263867Sbinkertn@umich.edu    while (threads != end) {
12272292SN/A        unsigned tid = *threads++;
12282292SN/A
12292292SN/A        if (!rob->isDoneSquashing(tid))
12302292SN/A            return false;
12312292SN/A    }
12322292SN/A
12332292SN/A    return true;
12341060SN/A}
12352292SN/A
12362301SN/Atemplate <class Impl>
12372301SN/Avoid
12382301SN/ADefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
12392301SN/A{
12402301SN/A    unsigned thread = inst->threadNumber;
12412301SN/A
12422301SN/A    //
12432301SN/A    //  Pick off the software prefetches
12442301SN/A    //
12452301SN/A#ifdef TARGET_ALPHA
12462301SN/A    if (inst->isDataPrefetch()) {
12472316SN/A        statComSwp[thread]++;
12482301SN/A    } else {
12492316SN/A        statComInst[thread]++;
12502301SN/A    }
12512301SN/A#else
12522316SN/A    statComInst[thread]++;
12532301SN/A#endif
12542301SN/A
12552301SN/A    //
12562301SN/A    //  Control Instructions
12572301SN/A    //
12582301SN/A    if (inst->isControl())
12592316SN/A        statComBranches[thread]++;
12602301SN/A
12612301SN/A    //
12622301SN/A    //  Memory references
12632301SN/A    //
12642301SN/A    if (inst->isMemRef()) {
12652316SN/A        statComRefs[thread]++;
12662301SN/A
12672301SN/A        if (inst->isLoad()) {
12682316SN/A            statComLoads[thread]++;
12692301SN/A        }
12702301SN/A    }
12712301SN/A
12722301SN/A    if (inst->isMemBarrier()) {
12732316SN/A        statComMembars[thread]++;
12742301SN/A    }
12752301SN/A}
12762301SN/A
12772292SN/A////////////////////////////////////////
12782292SN/A//                                    //
12792316SN/A//  SMT COMMIT POLICY MAINTAINED HERE //
12802292SN/A//                                    //
12812292SN/A////////////////////////////////////////
12822292SN/Atemplate <class Impl>
12832292SN/Aint
12842292SN/ADefaultCommit<Impl>::getCommittingThread()
12852292SN/A{
12862292SN/A    if (numThreads > 1) {
12872292SN/A        switch (commitPolicy) {
12882292SN/A
12892292SN/A          case Aggressive:
12902292SN/A            //If Policy is Aggressive, commit will call
12912292SN/A            //this function multiple times per
12922292SN/A            //cycle
12932292SN/A            return oldestReady();
12942292SN/A
12952292SN/A          case RoundRobin:
12962292SN/A            return roundRobin();
12972292SN/A
12982292SN/A          case OldestReady:
12992292SN/A            return oldestReady();
13002292SN/A
13012292SN/A          default:
13022292SN/A            return -1;
13032292SN/A        }
13042292SN/A    } else {
13053867Sbinkertn@umich.edu        assert(!activeThreads->empty());
13063867Sbinkertn@umich.edu        int tid = activeThreads->front();
13072292SN/A
13082292SN/A        if (commitStatus[tid] == Running ||
13092292SN/A            commitStatus[tid] == Idle ||
13102292SN/A            commitStatus[tid] == FetchTrapPending) {
13112292SN/A            return tid;
13122292SN/A        } else {
13132292SN/A            return -1;
13142292SN/A        }
13152292SN/A    }
13162292SN/A}
13172292SN/A
13182292SN/Atemplate<class Impl>
13192292SN/Aint
13202292SN/ADefaultCommit<Impl>::roundRobin()
13212292SN/A{
13222980Sgblack@eecs.umich.edu    std::list<unsigned>::iterator pri_iter = priority_list.begin();
13232980Sgblack@eecs.umich.edu    std::list<unsigned>::iterator end      = priority_list.end();
13242292SN/A
13252292SN/A    while (pri_iter != end) {
13262292SN/A        unsigned tid = *pri_iter;
13272292SN/A
13282292SN/A        if (commitStatus[tid] == Running ||
13292831Sksewell@umich.edu            commitStatus[tid] == Idle ||
13302831Sksewell@umich.edu            commitStatus[tid] == FetchTrapPending) {
13312292SN/A
13322292SN/A            if (rob->isHeadReady(tid)) {
13332292SN/A                priority_list.erase(pri_iter);
13342292SN/A                priority_list.push_back(tid);
13352292SN/A
13362292SN/A                return tid;
13372292SN/A            }
13382292SN/A        }
13392292SN/A
13402292SN/A        pri_iter++;
13412292SN/A    }
13422292SN/A
13432292SN/A    return -1;
13442292SN/A}
13452292SN/A
13462292SN/Atemplate<class Impl>
13472292SN/Aint
13482292SN/ADefaultCommit<Impl>::oldestReady()
13492292SN/A{
13502292SN/A    unsigned oldest = 0;
13512292SN/A    bool first = true;
13522292SN/A
13533867Sbinkertn@umich.edu    std::list<unsigned>::iterator threads = activeThreads->begin();
13543867Sbinkertn@umich.edu    std::list<unsigned>::iterator end = activeThreads->end();
13552292SN/A
13563867Sbinkertn@umich.edu    while (threads != end) {
13572292SN/A        unsigned tid = *threads++;
13582292SN/A
13592292SN/A        if (!rob->isEmpty(tid) &&
13602292SN/A            (commitStatus[tid] == Running ||
13612292SN/A             commitStatus[tid] == Idle ||
13622292SN/A             commitStatus[tid] == FetchTrapPending)) {
13632292SN/A
13642292SN/A            if (rob->isHeadReady(tid)) {
13652292SN/A
13662292SN/A                DynInstPtr head_inst = rob->readHeadInst(tid);
13672292SN/A
13682292SN/A                if (first) {
13692292SN/A                    oldest = tid;
13702292SN/A                    first = false;
13712292SN/A                } else if (head_inst->seqNum < oldest) {
13722292SN/A                    oldest = tid;
13732292SN/A                }
13742292SN/A            }
13752292SN/A        }
13762292SN/A    }
13772292SN/A
13782292SN/A    if (!first) {
13792292SN/A        return oldest;
13802292SN/A    } else {
13812292SN/A        return -1;
13822292SN/A    }
13832292SN/A}
1384