commit_impl.hh revision 7720
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
322292SN/A#include <algorithm>
332329SN/A#include <string>
342292SN/A
353577Sgblack@eecs.umich.edu#include "arch/utility.hh"
365953Ssaidi@eecs.umich.edu#include "base/cp_annotate.hh"
372292SN/A#include "base/loader/symtab.hh"
381060SN/A#include "base/timebuf.hh"
396221Snate@binkert.org#include "config/full_system.hh"
406658Snate@binkert.org#include "config/the_isa.hh"
416221Snate@binkert.org#include "config/use_checker.hh"
422292SN/A#include "cpu/exetrace.hh"
431717SN/A#include "cpu/o3/commit.hh"
442292SN/A#include "cpu/o3/thread_state.hh"
456221Snate@binkert.org#include "params/DerivO3CPU.hh"
462292SN/A
472790Sktlim@umich.edu#if USE_CHECKER
482790Sktlim@umich.edu#include "cpu/checker/cpu.hh"
492790Sktlim@umich.edu#endif
502790Sktlim@umich.edu
516221Snate@binkert.orgusing namespace std;
525529Snate@binkert.org
531061SN/Atemplate <class Impl>
542292SN/ADefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
556221Snate@binkert.org                                          ThreadID _tid)
565606Snate@binkert.org    : Event(CPU_Tick_Pri), commit(_commit), tid(_tid)
571060SN/A{
585769Snate@binkert.org    this->setFlags(AutoDelete);
591060SN/A}
601060SN/A
611061SN/Atemplate <class Impl>
621060SN/Avoid
632292SN/ADefaultCommit<Impl>::TrapEvent::process()
641062SN/A{
652316SN/A    // This will get reset by commit if it was switched out at the
662316SN/A    // time of this event processing.
672292SN/A    commit->trapSquash[tid] = true;
682292SN/A}
692292SN/A
702292SN/Atemplate <class Impl>
712292SN/Aconst char *
725336Shines@cs.fsu.eduDefaultCommit<Impl>::TrapEvent::description() const
732292SN/A{
744873Sstever@eecs.umich.edu    return "Trap";
752292SN/A}
762292SN/A
772292SN/Atemplate <class Impl>
785529Snate@binkert.orgDefaultCommit<Impl>::DefaultCommit(O3CPU *_cpu, DerivO3CPUParams *params)
794329Sktlim@umich.edu    : cpu(_cpu),
804329Sktlim@umich.edu      squashCounter(0),
812292SN/A      iewToCommitDelay(params->iewToCommitDelay),
822292SN/A      commitToIEWDelay(params->commitToIEWDelay),
832292SN/A      renameToROBDelay(params->renameToROBDelay),
842292SN/A      fetchToCommitDelay(params->commitToFetchDelay),
852292SN/A      renameWidth(params->renameWidth),
862292SN/A      commitWidth(params->commitWidth),
875529Snate@binkert.org      numThreads(params->numThreads),
882843Sktlim@umich.edu      drainPending(false),
892316SN/A      switchedOut(false),
902874Sktlim@umich.edu      trapLatency(params->trapLatency)
912292SN/A{
922292SN/A    _status = Active;
932292SN/A    _nextStatus = Inactive;
942980Sgblack@eecs.umich.edu    std::string policy = params->smtCommitPolicy;
952292SN/A
962292SN/A    //Convert string to lowercase
972292SN/A    std::transform(policy.begin(), policy.end(), policy.begin(),
982292SN/A                   (int(*)(int)) tolower);
992292SN/A
1002292SN/A    //Assign commit policy
1012292SN/A    if (policy == "aggressive"){
1022292SN/A        commitPolicy = Aggressive;
1032292SN/A
1044329Sktlim@umich.edu        DPRINTF(Commit,"Commit Policy set to Aggressive.");
1052292SN/A    } else if (policy == "roundrobin"){
1062292SN/A        commitPolicy = RoundRobin;
1072292SN/A
1082292SN/A        //Set-Up Priority List
1096221Snate@binkert.org        for (ThreadID tid = 0; tid < numThreads; tid++) {
1102292SN/A            priority_list.push_back(tid);
1112292SN/A        }
1122292SN/A
1134329Sktlim@umich.edu        DPRINTF(Commit,"Commit Policy set to Round Robin.");
1142292SN/A    } else if (policy == "oldestready"){
1152292SN/A        commitPolicy = OldestReady;
1162292SN/A
1174329Sktlim@umich.edu        DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
1182292SN/A    } else {
1192292SN/A        assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
1202292SN/A               "RoundRobin,OldestReady}");
1212292SN/A    }
1222292SN/A
1236221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++) {
1246221Snate@binkert.org        commitStatus[tid] = Idle;
1256221Snate@binkert.org        changedROBNumEntries[tid] = false;
1266221Snate@binkert.org        checkEmptyROB[tid] = false;
1276221Snate@binkert.org        trapInFlight[tid] = false;
1286221Snate@binkert.org        committedStores[tid] = false;
1296221Snate@binkert.org        trapSquash[tid] = false;
1306221Snate@binkert.org        tcSquash[tid] = false;
1317720Sgblack@eecs.umich.edu        pc[tid].set(0);
1322292SN/A    }
1333640Sktlim@umich.edu#if FULL_SYSTEM
1343640Sktlim@umich.edu    interrupt = NoFault;
1353640Sktlim@umich.edu#endif
1362292SN/A}
1372292SN/A
1382292SN/Atemplate <class Impl>
1392292SN/Astd::string
1402292SN/ADefaultCommit<Impl>::name() const
1412292SN/A{
1422292SN/A    return cpu->name() + ".commit";
1432292SN/A}
1442292SN/A
1452292SN/Atemplate <class Impl>
1462292SN/Avoid
1472292SN/ADefaultCommit<Impl>::regStats()
1482132SN/A{
1492301SN/A    using namespace Stats;
1501062SN/A    commitCommittedInsts
1511062SN/A        .name(name() + ".commitCommittedInsts")
1521062SN/A        .desc("The number of committed instructions")
1531062SN/A        .prereq(commitCommittedInsts);
1541062SN/A    commitSquashedInsts
1551062SN/A        .name(name() + ".commitSquashedInsts")
1561062SN/A        .desc("The number of squashed insts skipped by commit")
1571062SN/A        .prereq(commitSquashedInsts);
1581062SN/A    commitSquashEvents
1591062SN/A        .name(name() + ".commitSquashEvents")
1601062SN/A        .desc("The number of times commit is told to squash")
1611062SN/A        .prereq(commitSquashEvents);
1621062SN/A    commitNonSpecStalls
1631062SN/A        .name(name() + ".commitNonSpecStalls")
1641062SN/A        .desc("The number of times commit has been forced to stall to "
1651062SN/A              "communicate backwards")
1661062SN/A        .prereq(commitNonSpecStalls);
1671062SN/A    branchMispredicts
1681062SN/A        .name(name() + ".branchMispredicts")
1691062SN/A        .desc("The number of times a branch was mispredicted")
1701062SN/A        .prereq(branchMispredicts);
1712292SN/A    numCommittedDist
1721062SN/A        .init(0,commitWidth,1)
1731062SN/A        .name(name() + ".COM:committed_per_cycle")
1741062SN/A        .desc("Number of insts commited each cycle")
1751062SN/A        .flags(Stats::pdf)
1761062SN/A        ;
1772301SN/A
1782316SN/A    statComInst
1796221Snate@binkert.org        .init(cpu->numThreads)
1802301SN/A        .name(name() + ".COM:count")
1812301SN/A        .desc("Number of instructions committed")
1822301SN/A        .flags(total)
1832301SN/A        ;
1842301SN/A
1852316SN/A    statComSwp
1866221Snate@binkert.org        .init(cpu->numThreads)
1872301SN/A        .name(name() + ".COM:swp_count")
1882301SN/A        .desc("Number of s/w prefetches committed")
1892301SN/A        .flags(total)
1902301SN/A        ;
1912301SN/A
1922316SN/A    statComRefs
1936221Snate@binkert.org        .init(cpu->numThreads)
1942301SN/A        .name(name() +  ".COM:refs")
1952301SN/A        .desc("Number of memory references committed")
1962301SN/A        .flags(total)
1972301SN/A        ;
1982301SN/A
1992316SN/A    statComLoads
2006221Snate@binkert.org        .init(cpu->numThreads)
2012301SN/A        .name(name() +  ".COM:loads")
2022301SN/A        .desc("Number of loads committed")
2032301SN/A        .flags(total)
2042301SN/A        ;
2052301SN/A
2062316SN/A    statComMembars
2076221Snate@binkert.org        .init(cpu->numThreads)
2082301SN/A        .name(name() +  ".COM:membars")
2092301SN/A        .desc("Number of memory barriers committed")
2102301SN/A        .flags(total)
2112301SN/A        ;
2122301SN/A
2132316SN/A    statComBranches
2146221Snate@binkert.org        .init(cpu->numThreads)
2152301SN/A        .name(name() + ".COM:branches")
2162301SN/A        .desc("Number of branches committed")
2172301SN/A        .flags(total)
2182301SN/A        ;
2192301SN/A
2202316SN/A    commitEligible
2216221Snate@binkert.org        .init(cpu->numThreads)
2222301SN/A        .name(name() + ".COM:bw_limited")
2232301SN/A        .desc("number of insts not committed due to BW limits")
2242301SN/A        .flags(total)
2252301SN/A        ;
2262301SN/A
2272316SN/A    commitEligibleSamples
2282301SN/A        .name(name() + ".COM:bw_lim_events")
2292301SN/A        .desc("number cycles where commit BW limit reached")
2302301SN/A        ;
2311062SN/A}
2321062SN/A
2331062SN/Atemplate <class Impl>
2341062SN/Avoid
2352980Sgblack@eecs.umich.eduDefaultCommit<Impl>::setThreads(std::vector<Thread *> &threads)
2362292SN/A{
2372292SN/A    thread = threads;
2382292SN/A}
2392292SN/A
2402292SN/Atemplate <class Impl>
2412292SN/Avoid
2422292SN/ADefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
2431060SN/A{
2441060SN/A    timeBuffer = tb_ptr;
2451060SN/A
2461060SN/A    // Setup wire to send information back to IEW.
2471060SN/A    toIEW = timeBuffer->getWire(0);
2481060SN/A
2491060SN/A    // Setup wire to read data from IEW (for the ROB).
2501060SN/A    robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
2511060SN/A}
2521060SN/A
2531061SN/Atemplate <class Impl>
2541060SN/Avoid
2552292SN/ADefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
2562292SN/A{
2572292SN/A    fetchQueue = fq_ptr;
2582292SN/A
2592292SN/A    // Setup wire to get instructions from rename (for the ROB).
2602292SN/A    fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
2612292SN/A}
2622292SN/A
2632292SN/Atemplate <class Impl>
2642292SN/Avoid
2652292SN/ADefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
2661060SN/A{
2671060SN/A    renameQueue = rq_ptr;
2681060SN/A
2691060SN/A    // Setup wire to get instructions from rename (for the ROB).
2701060SN/A    fromRename = renameQueue->getWire(-renameToROBDelay);
2711060SN/A}
2721060SN/A
2731061SN/Atemplate <class Impl>
2741060SN/Avoid
2752292SN/ADefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
2761060SN/A{
2771060SN/A    iewQueue = iq_ptr;
2781060SN/A
2791060SN/A    // Setup wire to get instructions from IEW.
2801060SN/A    fromIEW = iewQueue->getWire(-iewToCommitDelay);
2811060SN/A}
2821060SN/A
2831061SN/Atemplate <class Impl>
2841060SN/Avoid
2852292SN/ADefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
2862292SN/A{
2872292SN/A    iewStage = iew_stage;
2882292SN/A}
2892292SN/A
2902292SN/Atemplate<class Impl>
2912292SN/Avoid
2926221Snate@binkert.orgDefaultCommit<Impl>::setActiveThreads(list<ThreadID> *at_ptr)
2932292SN/A{
2942292SN/A    activeThreads = at_ptr;
2952292SN/A}
2962292SN/A
2972292SN/Atemplate <class Impl>
2982292SN/Avoid
2992292SN/ADefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
3002292SN/A{
3016221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++)
3026221Snate@binkert.org        renameMap[tid] = &rm_ptr[tid];
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.
3206221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++) {
3216221Snate@binkert.org        toIEW->commitInfo[tid].usedROB = true;
3226221Snate@binkert.org        toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
3236221Snate@binkert.org        toIEW->commitInfo[tid].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;
3666221Snate@binkert.org    for (ThreadID tid = 0; tid < numThreads; tid++) {
3676221Snate@binkert.org        commitStatus[tid] = Idle;
3686221Snate@binkert.org        changedROBNumEntries[tid] = false;
3696221Snate@binkert.org        trapSquash[tid] = false;
3706221Snate@binkert.org        tcSquash[tid] = 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
3816221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
3826221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
3833867Sbinkertn@umich.edu
3843867Sbinkertn@umich.edu    while (threads != end) {
3856221Snate@binkert.org        ThreadID 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
4136221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4146221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4152292SN/A
4163867Sbinkertn@umich.edu    while (threads != end) {
4176221Snate@binkert.org        ThreadID 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{
4376221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
4386221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
4392292SN/A
4403867Sbinkertn@umich.edu    while (threads != end) {
4416221Snate@binkert.org        ThreadID 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>
4526221Snate@binkert.orgsize_t
4536221Snate@binkert.orgDefaultCommit<Impl>::numROBFreeEntries(ThreadID tid)
4542292SN/A{
4552292SN/A    return rob->numFreeEntries(tid);
4562292SN/A}
4572292SN/A
4582292SN/Atemplate <class Impl>
4592292SN/Avoid
4606221Snate@binkert.orgDefaultCommit<Impl>::generateTrapEvent(ThreadID 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
4726221Snate@binkert.orgDefaultCommit<Impl>::generateTCEvent(ThreadID 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
4826221Snate@binkert.orgDefaultCommit<Impl>::squashAll(ThreadID 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
5127720Sgblack@eecs.umich.edu    toIEW->commitInfo[tid].pc = pc[tid];
5132316SN/A}
5142292SN/A
5152316SN/Atemplate <class Impl>
5162316SN/Avoid
5176221Snate@binkert.orgDefaultCommit<Impl>::squashFromTrap(ThreadID tid)
5182316SN/A{
5192316SN/A    squashAll(tid);
5202316SN/A
5217720Sgblack@eecs.umich.edu    DPRINTF(Commit, "Squashing from trap, restarting at PC %s\n", pc[tid]);
5222316SN/A
5232316SN/A    thread[tid]->trapPending = false;
5242316SN/A    thread[tid]->inSyscall = false;
5254035Sktlim@umich.edu    trapInFlight[tid] = false;
5262316SN/A
5272316SN/A    trapSquash[tid] = false;
5282316SN/A
5292316SN/A    commitStatus[tid] = ROBSquashing;
5302316SN/A    cpu->activityThisCycle();
5312316SN/A}
5322316SN/A
5332316SN/Atemplate <class Impl>
5342316SN/Avoid
5356221Snate@binkert.orgDefaultCommit<Impl>::squashFromTC(ThreadID tid)
5362316SN/A{
5372316SN/A    squashAll(tid);
5382292SN/A
5397720Sgblack@eecs.umich.edu    DPRINTF(Commit, "Squashing from TC, restarting at PC %s\n", pc[tid]);
5402292SN/A
5412292SN/A    thread[tid]->inSyscall = false;
5422292SN/A    assert(!thread[tid]->trapPending);
5432316SN/A
5442292SN/A    commitStatus[tid] = ROBSquashing;
5452292SN/A    cpu->activityThisCycle();
5462292SN/A
5472680Sktlim@umich.edu    tcSquash[tid] = false;
5482292SN/A}
5492292SN/A
5502292SN/Atemplate <class Impl>
5512292SN/Avoid
5522292SN/ADefaultCommit<Impl>::tick()
5532292SN/A{
5542292SN/A    wroteToTimeBuffer = false;
5552292SN/A    _nextStatus = Inactive;
5562292SN/A
5572843Sktlim@umich.edu    if (drainPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
5582843Sktlim@umich.edu        cpu->signalDrained();
5592843Sktlim@umich.edu        drainPending = false;
5602316SN/A        return;
5612316SN/A    }
5622316SN/A
5633867Sbinkertn@umich.edu    if (activeThreads->empty())
5642875Sksewell@umich.edu        return;
5652875Sksewell@umich.edu
5666221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
5676221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
5682292SN/A
5692316SN/A    // Check if any of the threads are done squashing.  Change the
5702316SN/A    // status if they are done.
5713867Sbinkertn@umich.edu    while (threads != end) {
5726221Snate@binkert.org        ThreadID tid = *threads++;
5732292SN/A
5744035Sktlim@umich.edu        // Clear the bit saying if the thread has committed stores
5754035Sktlim@umich.edu        // this cycle.
5764035Sktlim@umich.edu        committedStores[tid] = false;
5774035Sktlim@umich.edu
5782292SN/A        if (commitStatus[tid] == ROBSquashing) {
5792292SN/A
5802292SN/A            if (rob->isDoneSquashing(tid)) {
5812292SN/A                commitStatus[tid] = Running;
5822292SN/A            } else {
5832292SN/A                DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
5842877Sksewell@umich.edu                        " insts this cycle.\n", tid);
5852702Sktlim@umich.edu                rob->doSquash(tid);
5862702Sktlim@umich.edu                toIEW->commitInfo[tid].robSquashing = true;
5872702Sktlim@umich.edu                wroteToTimeBuffer = true;
5882292SN/A            }
5892292SN/A        }
5902292SN/A    }
5912292SN/A
5922292SN/A    commit();
5932292SN/A
5942292SN/A    markCompletedInsts();
5952292SN/A
5963867Sbinkertn@umich.edu    threads = activeThreads->begin();
5972292SN/A
5983867Sbinkertn@umich.edu    while (threads != end) {
5996221Snate@binkert.org        ThreadID tid = *threads++;
6002292SN/A
6012292SN/A        if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
6022292SN/A            // The ROB has more instructions it can commit. Its next status
6032292SN/A            // will be active.
6042292SN/A            _nextStatus = Active;
6052292SN/A
6062292SN/A            DynInstPtr inst = rob->readHeadInst(tid);
6072292SN/A
6087720Sgblack@eecs.umich.edu            DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %s is head of"
6092292SN/A                    " ROB and ready to commit\n",
6107720Sgblack@eecs.umich.edu                    tid, inst->seqNum, inst->pcState());
6112292SN/A
6122292SN/A        } else if (!rob->isEmpty(tid)) {
6132292SN/A            DynInstPtr inst = rob->readHeadInst(tid);
6142292SN/A
6152292SN/A            DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
6167720Sgblack@eecs.umich.edu                    "%s is head of ROB and not ready\n",
6177720Sgblack@eecs.umich.edu                    tid, inst->seqNum, inst->pcState());
6182292SN/A        }
6192292SN/A
6202292SN/A        DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
6212292SN/A                tid, rob->countInsts(tid), rob->numFreeEntries(tid));
6222292SN/A    }
6232292SN/A
6242292SN/A
6252292SN/A    if (wroteToTimeBuffer) {
6262316SN/A        DPRINTF(Activity, "Activity This Cycle.\n");
6272292SN/A        cpu->activityThisCycle();
6282292SN/A    }
6292292SN/A
6302292SN/A    updateStatus();
6312292SN/A}
6322292SN/A
6334035Sktlim@umich.edu#if FULL_SYSTEM
6342292SN/Atemplate <class Impl>
6352292SN/Avoid
6364035Sktlim@umich.eduDefaultCommit<Impl>::handleInterrupt()
6372292SN/A{
6383640Sktlim@umich.edu    if (interrupt != NoFault) {
6392316SN/A        // Wait until the ROB is empty and all stores have drained in
6402316SN/A        // order to enter the interrupt.
6412292SN/A        if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
6423633Sktlim@umich.edu            // Squash or record that I need to squash this cycle if
6433633Sktlim@umich.edu            // an interrupt needed to be handled.
6443633Sktlim@umich.edu            DPRINTF(Commit, "Interrupt detected.\n");
6453633Sktlim@umich.edu
6464035Sktlim@umich.edu            // Clear the interrupt now that it's going to be handled
6474035Sktlim@umich.edu            toIEW->commitInfo[0].clearInterrupt = true;
6484035Sktlim@umich.edu
6492292SN/A            assert(!thread[0]->inSyscall);
6502292SN/A            thread[0]->inSyscall = true;
6512292SN/A
6523633Sktlim@umich.edu            // CPU will handle interrupt.
6533640Sktlim@umich.edu            cpu->processInterrupts(interrupt);
6542292SN/A
6553633Sktlim@umich.edu            thread[0]->inSyscall = false;
6563633Sktlim@umich.edu
6572292SN/A            commitStatus[0] = TrapPending;
6582292SN/A
6592292SN/A            // Generate trap squash event.
6602292SN/A            generateTrapEvent(0);
6612292SN/A
6623640Sktlim@umich.edu            interrupt = NoFault;
6632292SN/A        } else {
6642292SN/A            DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
6652292SN/A        }
6664035Sktlim@umich.edu    } else if (commitStatus[0] != TrapPending &&
6675704Snate@binkert.org               cpu->checkInterrupts(cpu->tcBase(0)) &&
6684035Sktlim@umich.edu               !trapSquash[0] &&
6694035Sktlim@umich.edu               !tcSquash[0]) {
6703640Sktlim@umich.edu        // Process interrupts if interrupts are enabled, not in PAL
6713640Sktlim@umich.edu        // mode, and no other traps or external squashes are currently
6723640Sktlim@umich.edu        // pending.
6733640Sktlim@umich.edu        // @todo: Allow other threads to handle interrupts.
6743640Sktlim@umich.edu
6753640Sktlim@umich.edu        // Get any interrupt that happened
6763640Sktlim@umich.edu        interrupt = cpu->getInterrupts();
6773640Sktlim@umich.edu
6783640Sktlim@umich.edu        if (interrupt != NoFault) {
6793640Sktlim@umich.edu            // Tell fetch that there is an interrupt pending.  This
6803640Sktlim@umich.edu            // will make fetch wait until it sees a non PAL-mode PC,
6813640Sktlim@umich.edu            // at which point it stops fetching instructions.
6823640Sktlim@umich.edu            toIEW->commitInfo[0].interruptPending = true;
6833640Sktlim@umich.edu        }
6841060SN/A    }
6854035Sktlim@umich.edu}
6864035Sktlim@umich.edu#endif // FULL_SYSTEM
6873634Sktlim@umich.edu
6884035Sktlim@umich.edutemplate <class Impl>
6894035Sktlim@umich.eduvoid
6904035Sktlim@umich.eduDefaultCommit<Impl>::commit()
6914035Sktlim@umich.edu{
6924035Sktlim@umich.edu
6934035Sktlim@umich.edu#if FULL_SYSTEM
6944035Sktlim@umich.edu    // Check for any interrupt, and start processing it.  Or if we
6954035Sktlim@umich.edu    // have an outstanding interrupt and are at a point when it is
6964035Sktlim@umich.edu    // valid to take an interrupt, process it.
6975704Snate@binkert.org    if (cpu->checkInterrupts(cpu->tcBase(0))) {
6984035Sktlim@umich.edu        handleInterrupt();
6994035Sktlim@umich.edu    }
7001060SN/A#endif // FULL_SYSTEM
7011060SN/A
7021060SN/A    ////////////////////////////////////
7032316SN/A    // Check for any possible squashes, handle them first
7041060SN/A    ////////////////////////////////////
7056221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
7066221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
7071060SN/A
7083867Sbinkertn@umich.edu    while (threads != end) {
7096221Snate@binkert.org        ThreadID tid = *threads++;
7101060SN/A
7112292SN/A        // Not sure which one takes priority.  I think if we have
7122292SN/A        // both, that's a bad sign.
7132292SN/A        if (trapSquash[tid] == true) {
7142680Sktlim@umich.edu            assert(!tcSquash[tid]);
7152292SN/A            squashFromTrap(tid);
7162680Sktlim@umich.edu        } else if (tcSquash[tid] == true) {
7174035Sktlim@umich.edu            assert(commitStatus[tid] != TrapPending);
7182680Sktlim@umich.edu            squashFromTC(tid);
7192292SN/A        }
7201061SN/A
7212292SN/A        // Squashed sequence number must be older than youngest valid
7222292SN/A        // instruction in the ROB. This prevents squashes from younger
7232292SN/A        // instructions overriding squashes from older instructions.
7242292SN/A        if (fromIEW->squash[tid] &&
7252292SN/A            commitStatus[tid] != TrapPending &&
7262292SN/A            fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
7271061SN/A
7282292SN/A            DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
7292292SN/A                    tid,
7302292SN/A                    fromIEW->mispredPC[tid],
7312292SN/A                    fromIEW->squashedSeqNum[tid]);
7321061SN/A
7332292SN/A            DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
7342292SN/A                    tid,
7357720Sgblack@eecs.umich.edu                    fromIEW->pc[tid].nextInstAddr());
7361061SN/A
7372292SN/A            commitStatus[tid] = ROBSquashing;
7381061SN/A
7392292SN/A            // If we want to include the squashing instruction in the squash,
7402292SN/A            // then use one older sequence number.
7412292SN/A            InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
7421062SN/A
7432935Sksewell@umich.edu            if (fromIEW->includeSquashInst[tid] == true) {
7442292SN/A                squashed_inst--;
7452935Sksewell@umich.edu            }
7464035Sktlim@umich.edu
7472292SN/A            // All younger instructions will be squashed. Set the sequence
7482292SN/A            // number as the youngest instruction in the ROB.
7492292SN/A            youngestSeqNum[tid] = squashed_inst;
7502292SN/A
7513093Sksewell@umich.edu            rob->squash(squashed_inst, tid);
7522292SN/A            changedROBNumEntries[tid] = true;
7532292SN/A
7542292SN/A            toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
7552292SN/A
7562292SN/A            toIEW->commitInfo[tid].squash = true;
7572292SN/A
7582292SN/A            // Send back the rob squashing signal so other stages know that
7592292SN/A            // the ROB is in the process of squashing.
7602292SN/A            toIEW->commitInfo[tid].robSquashing = true;
7612292SN/A
7622292SN/A            toIEW->commitInfo[tid].branchMispredict =
7632292SN/A                fromIEW->branchMispredict[tid];
7642292SN/A
7652292SN/A            toIEW->commitInfo[tid].branchTaken =
7662292SN/A                fromIEW->branchTaken[tid];
7672292SN/A
7687720Sgblack@eecs.umich.edu            toIEW->commitInfo[tid].pc = fromIEW->pc[tid];
7692292SN/A
7702316SN/A            toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
7712292SN/A
7722292SN/A            if (toIEW->commitInfo[tid].branchMispredict) {
7732292SN/A                ++branchMispredicts;
7742292SN/A            }
7751062SN/A        }
7762292SN/A
7771060SN/A    }
7781060SN/A
7792292SN/A    setNextStatus();
7802292SN/A
7812292SN/A    if (squashCounter != numThreads) {
7821061SN/A        // If we're not currently squashing, then get instructions.
7831060SN/A        getInsts();
7841060SN/A
7851061SN/A        // Try to commit any instructions.
7861060SN/A        commitInsts();
7871060SN/A    }
7881060SN/A
7892292SN/A    //Check for any activity
7903867Sbinkertn@umich.edu    threads = activeThreads->begin();
7912292SN/A
7923867Sbinkertn@umich.edu    while (threads != end) {
7936221Snate@binkert.org        ThreadID tid = *threads++;
7942292SN/A
7952292SN/A        if (changedROBNumEntries[tid]) {
7962292SN/A            toIEW->commitInfo[tid].usedROB = true;
7972292SN/A            toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
7982292SN/A
7992292SN/A            wroteToTimeBuffer = true;
8002292SN/A            changedROBNumEntries[tid] = false;
8014035Sktlim@umich.edu            if (rob->isEmpty(tid))
8024035Sktlim@umich.edu                checkEmptyROB[tid] = true;
8032292SN/A        }
8044035Sktlim@umich.edu
8054035Sktlim@umich.edu        // ROB is only considered "empty" for previous stages if: a)
8064035Sktlim@umich.edu        // ROB is empty, b) there are no outstanding stores, c) IEW
8074035Sktlim@umich.edu        // stage has received any information regarding stores that
8084035Sktlim@umich.edu        // committed.
8094035Sktlim@umich.edu        // c) is checked by making sure to not consider the ROB empty
8104035Sktlim@umich.edu        // on the same cycle as when stores have been committed.
8114035Sktlim@umich.edu        // @todo: Make this handle multi-cycle communication between
8124035Sktlim@umich.edu        // commit and IEW.
8134035Sktlim@umich.edu        if (checkEmptyROB[tid] && rob->isEmpty(tid) &&
8145557Sktlim@umich.edu            !iewStage->hasStoresToWB(tid) && !committedStores[tid]) {
8154035Sktlim@umich.edu            checkEmptyROB[tid] = false;
8164035Sktlim@umich.edu            toIEW->commitInfo[tid].usedROB = true;
8174035Sktlim@umich.edu            toIEW->commitInfo[tid].emptyROB = true;
8184035Sktlim@umich.edu            toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
8194035Sktlim@umich.edu            wroteToTimeBuffer = true;
8204035Sktlim@umich.edu        }
8214035Sktlim@umich.edu
8221060SN/A    }
8231060SN/A}
8241060SN/A
8251061SN/Atemplate <class Impl>
8261060SN/Avoid
8272292SN/ADefaultCommit<Impl>::commitInsts()
8281060SN/A{
8291060SN/A    ////////////////////////////////////
8301060SN/A    // Handle commit
8312316SN/A    // Note that commit will be handled prior to putting new
8322316SN/A    // instructions in the ROB so that the ROB only tries to commit
8332316SN/A    // instructions it has in this current cycle, and not instructions
8342316SN/A    // it is writing in during this cycle.  Can't commit and squash
8352316SN/A    // things at the same time...
8361060SN/A    ////////////////////////////////////
8371060SN/A
8382292SN/A    DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
8391060SN/A
8401060SN/A    unsigned num_committed = 0;
8411060SN/A
8422292SN/A    DynInstPtr head_inst;
8432316SN/A
8441060SN/A    // Commit as many instructions as possible until the commit bandwidth
8451060SN/A    // limit is reached, or it becomes impossible to commit any more.
8462292SN/A    while (num_committed < commitWidth) {
8472292SN/A        int commit_thread = getCommittingThread();
8481060SN/A
8492292SN/A        if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
8502292SN/A            break;
8512292SN/A
8522292SN/A        head_inst = rob->readHeadInst(commit_thread);
8532292SN/A
8546221Snate@binkert.org        ThreadID tid = head_inst->threadNumber;
8552292SN/A
8562292SN/A        assert(tid == commit_thread);
8572292SN/A
8582292SN/A        DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
8592292SN/A                head_inst->seqNum, tid);
8602132SN/A
8612316SN/A        // If the head instruction is squashed, it is ready to retire
8622316SN/A        // (be removed from the ROB) at any time.
8631060SN/A        if (head_inst->isSquashed()) {
8641060SN/A
8652292SN/A            DPRINTF(Commit, "Retiring squashed instruction from "
8661060SN/A                    "ROB.\n");
8671060SN/A
8682292SN/A            rob->retireHead(commit_thread);
8691060SN/A
8701062SN/A            ++commitSquashedInsts;
8711062SN/A
8722292SN/A            // Record that the number of ROB entries has changed.
8732292SN/A            changedROBNumEntries[tid] = true;
8741060SN/A        } else {
8757720Sgblack@eecs.umich.edu            pc[tid] = head_inst->pcState();
8762292SN/A
8771060SN/A            // Increment the total number of non-speculative instructions
8781060SN/A            // executed.
8791060SN/A            // Hack for now: it really shouldn't happen until after the
8801061SN/A            // commit is deemed to be successful, but this count is needed
8811061SN/A            // for syscalls.
8822292SN/A            thread[tid]->funcExeInst++;
8831060SN/A
8841060SN/A            // Try to commit the head instruction.
8851060SN/A            bool commit_success = commitHead(head_inst, num_committed);
8861060SN/A
8871062SN/A            if (commit_success) {
8881060SN/A                ++num_committed;
8891060SN/A
8902292SN/A                changedROBNumEntries[tid] = true;
8912292SN/A
8922292SN/A                // Set the doneSeqNum to the youngest committed instruction.
8932292SN/A                toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
8941060SN/A
8951062SN/A                ++commitCommittedInsts;
8961062SN/A
8972292SN/A                // To match the old model, don't count nops and instruction
8982292SN/A                // prefetches towards the total commit count.
8992292SN/A                if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
9002292SN/A                    cpu->instDone(tid);
9011062SN/A                }
9022292SN/A
9037720Sgblack@eecs.umich.edu                TheISA::advancePC(pc[tid], head_inst->staticInst);
9042935Sksewell@umich.edu
9052292SN/A                int count = 0;
9062292SN/A                Addr oldpc;
9075108Sgblack@eecs.umich.edu                // Debug statement.  Checks to make sure we're not
9085108Sgblack@eecs.umich.edu                // currently updating state while handling PC events.
9095108Sgblack@eecs.umich.edu                assert(!thread[tid]->inSyscall && !thread[tid]->trapPending);
9102292SN/A                do {
9117720Sgblack@eecs.umich.edu                    oldpc = pc[tid].instAddr();
9125108Sgblack@eecs.umich.edu                    cpu->system->pcEventQueue.service(thread[tid]->getTC());
9132292SN/A                    count++;
9147720Sgblack@eecs.umich.edu                } while (oldpc != pc[tid].instAddr());
9152292SN/A                if (count > 1) {
9165108Sgblack@eecs.umich.edu                    DPRINTF(Commit,
9175108Sgblack@eecs.umich.edu                            "PC skip function event, stopping commit\n");
9182292SN/A                    break;
9192292SN/A                }
9201060SN/A            } else {
9217720Sgblack@eecs.umich.edu                DPRINTF(Commit, "Unable to commit head instruction PC:%s "
9222292SN/A                        "[tid:%i] [sn:%i].\n",
9237720Sgblack@eecs.umich.edu                        head_inst->pcState(), tid ,head_inst->seqNum);
9241060SN/A                break;
9251060SN/A            }
9261060SN/A        }
9271060SN/A    }
9281062SN/A
9291063SN/A    DPRINTF(CommitRate, "%i\n", num_committed);
9302292SN/A    numCommittedDist.sample(num_committed);
9312307SN/A
9322307SN/A    if (num_committed == commitWidth) {
9332349SN/A        commitEligibleSamples++;
9342307SN/A    }
9351060SN/A}
9361060SN/A
9371061SN/Atemplate <class Impl>
9381060SN/Abool
9392292SN/ADefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
9401060SN/A{
9411060SN/A    assert(head_inst);
9421060SN/A
9436221Snate@binkert.org    ThreadID tid = head_inst->threadNumber;
9442292SN/A
9452316SN/A    // If the instruction is not executed yet, then it will need extra
9462316SN/A    // handling.  Signal backwards that it should be executed.
9471061SN/A    if (!head_inst->isExecuted()) {
9481061SN/A        // Keep this number correct.  We have not yet actually executed
9491061SN/A        // and committed this instruction.
9502292SN/A        thread[tid]->funcExeInst--;
9511062SN/A
9522292SN/A        if (head_inst->isNonSpeculative() ||
9532348SN/A            head_inst->isStoreConditional() ||
9542292SN/A            head_inst->isMemBarrier() ||
9552292SN/A            head_inst->isWriteBarrier()) {
9562316SN/A
9572316SN/A            DPRINTF(Commit, "Encountered a barrier or non-speculative "
9587720Sgblack@eecs.umich.edu                    "instruction [sn:%lli] at the head of the ROB, PC %s.\n",
9597720Sgblack@eecs.umich.edu                    head_inst->seqNum, head_inst->pcState());
9602316SN/A
9615557Sktlim@umich.edu            if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
9622292SN/A                DPRINTF(Commit, "Waiting for all stores to writeback.\n");
9632292SN/A                return false;
9642292SN/A            }
9652292SN/A
9662292SN/A            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
9671061SN/A
9681061SN/A            // Change the instruction so it won't try to commit again until
9691061SN/A            // it is executed.
9701061SN/A            head_inst->clearCanCommit();
9711061SN/A
9721062SN/A            ++commitNonSpecStalls;
9731062SN/A
9741061SN/A            return false;
9752292SN/A        } else if (head_inst->isLoad()) {
9765557Sktlim@umich.edu            if (inst_num > 0 || iewStage->hasStoresToWB(tid)) {
9774035Sktlim@umich.edu                DPRINTF(Commit, "Waiting for all stores to writeback.\n");
9784035Sktlim@umich.edu                return false;
9794035Sktlim@umich.edu            }
9804035Sktlim@umich.edu
9814035Sktlim@umich.edu            assert(head_inst->uncacheable());
9827720Sgblack@eecs.umich.edu            DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %s.\n",
9837720Sgblack@eecs.umich.edu                    head_inst->seqNum, head_inst->pcState());
9842292SN/A
9852292SN/A            // Send back the non-speculative instruction's sequence
9862316SN/A            // number.  Tell the lsq to re-execute the load.
9872292SN/A            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
9882292SN/A            toIEW->commitInfo[tid].uncached = true;
9892292SN/A            toIEW->commitInfo[tid].uncachedLoad = head_inst;
9902292SN/A
9912292SN/A            head_inst->clearCanCommit();
9922292SN/A
9932292SN/A            return false;
9941061SN/A        } else {
9952292SN/A            panic("Trying to commit un-executed instruction "
9961061SN/A                  "of unknown type!\n");
9971061SN/A        }
9981060SN/A    }
9991060SN/A
10002316SN/A    if (head_inst->isThreadSync()) {
10012292SN/A        // Not handled for now.
10022316SN/A        panic("Thread sync instructions are not handled yet.\n");
10032132SN/A    }
10042132SN/A
10054035Sktlim@umich.edu    // Check if the instruction caused a fault.  If so, trap.
10064035Sktlim@umich.edu    Fault inst_fault = head_inst->getFault();
10074035Sktlim@umich.edu
10082316SN/A    // Stores mark themselves as completed.
10094035Sktlim@umich.edu    if (!head_inst->isStore() && inst_fault == NoFault) {
10102310SN/A        head_inst->setCompleted();
10112310SN/A    }
10122310SN/A
10132733Sktlim@umich.edu#if USE_CHECKER
10142316SN/A    // Use checker prior to updating anything due to traps or PC
10152316SN/A    // based events.
10162316SN/A    if (cpu->checker) {
10172732Sktlim@umich.edu        cpu->checker->verify(head_inst);
10181060SN/A    }
10192733Sktlim@umich.edu#endif
10201060SN/A
10212112SN/A    if (inst_fault != NoFault) {
10227720Sgblack@eecs.umich.edu        DPRINTF(Commit, "Inst [sn:%lli] PC %s has a fault\n",
10237720Sgblack@eecs.umich.edu                head_inst->seqNum, head_inst->pcState());
10242292SN/A
10255557Sktlim@umich.edu        if (iewStage->hasStoresToWB(tid) || inst_num > 0) {
10262316SN/A            DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
10272316SN/A            return false;
10282316SN/A        }
10292310SN/A
10304035Sktlim@umich.edu        head_inst->setCompleted();
10314035Sktlim@umich.edu
10322733Sktlim@umich.edu#if USE_CHECKER
10332316SN/A        if (cpu->checker && head_inst->isStore()) {
10342732Sktlim@umich.edu            cpu->checker->verify(head_inst);
10352316SN/A        }
10362733Sktlim@umich.edu#endif
10372292SN/A
10382316SN/A        assert(!thread[tid]->inSyscall);
10392292SN/A
10402316SN/A        // Mark that we're in state update mode so that the trap's
10412316SN/A        // execution doesn't generate extra squashes.
10422316SN/A        thread[tid]->inSyscall = true;
10432292SN/A
10442316SN/A        // Execute the trap.  Although it's slightly unrealistic in
10452316SN/A        // terms of timing (as it doesn't wait for the full timing of
10462316SN/A        // the trap event to complete before updating state), it's
10472316SN/A        // needed to update the state as soon as possible.  This
10482316SN/A        // prevents external agents from changing any specific state
10492316SN/A        // that the trap need.
10507684Sgblack@eecs.umich.edu        cpu->trap(inst_fault, tid, head_inst->staticInst);
10512292SN/A
10522316SN/A        // Exit state update mode to avoid accidental updating.
10532316SN/A        thread[tid]->inSyscall = false;
10542292SN/A
10552316SN/A        commitStatus[tid] = TrapPending;
10562292SN/A
10574035Sktlim@umich.edu        if (head_inst->traceData) {
10586667Ssteve.reinhardt@amd.com            if (DTRACE(ExecFaulting)) {
10596667Ssteve.reinhardt@amd.com                head_inst->traceData->setFetchSeq(head_inst->seqNum);
10606667Ssteve.reinhardt@amd.com                head_inst->traceData->setCPSeq(thread[tid]->numInst);
10616667Ssteve.reinhardt@amd.com                head_inst->traceData->dump();
10626667Ssteve.reinhardt@amd.com            }
10634288Sktlim@umich.edu            delete head_inst->traceData;
10644035Sktlim@umich.edu            head_inst->traceData = NULL;
10654035Sktlim@umich.edu        }
10664035Sktlim@umich.edu
10672316SN/A        // Generate trap squash event.
10682316SN/A        generateTrapEvent(tid);
10692316SN/A        return false;
10701060SN/A    }
10711060SN/A
10722301SN/A    updateComInstStats(head_inst);
10732132SN/A
10742362SN/A#if FULL_SYSTEM
10752362SN/A    if (thread[tid]->profile) {
10767720Sgblack@eecs.umich.edu        thread[tid]->profilePC = head_inst->instAddr();
10773126Sktlim@umich.edu        ProfileNode *node = thread[tid]->profile->consume(thread[tid]->getTC(),
10782362SN/A                                                          head_inst->staticInst);
10792362SN/A
10802362SN/A        if (node)
10812362SN/A            thread[tid]->profileNode = node;
10822362SN/A    }
10835953Ssaidi@eecs.umich.edu    if (CPA::available()) {
10845953Ssaidi@eecs.umich.edu        if (head_inst->isControl()) {
10855953Ssaidi@eecs.umich.edu            ThreadContext *tc = thread[tid]->getTC();
10867720Sgblack@eecs.umich.edu            CPA::cpa()->swAutoBegin(tc, head_inst->nextInstAddr());
10875953Ssaidi@eecs.umich.edu        }
10885953Ssaidi@eecs.umich.edu    }
10892362SN/A#endif
10902362SN/A
10912132SN/A    if (head_inst->traceData) {
10922292SN/A        head_inst->traceData->setFetchSeq(head_inst->seqNum);
10932292SN/A        head_inst->traceData->setCPSeq(thread[tid]->numInst);
10944046Sbinkertn@umich.edu        head_inst->traceData->dump();
10954046Sbinkertn@umich.edu        delete head_inst->traceData;
10962292SN/A        head_inst->traceData = NULL;
10971060SN/A    }
10981060SN/A
10992292SN/A    // Update the commit rename map
11002292SN/A    for (int i = 0; i < head_inst->numDestRegs(); i++) {
11013771Sgblack@eecs.umich.edu        renameMap[tid]->setEntry(head_inst->flattenedDestRegIdx(i),
11022292SN/A                                 head_inst->renamedDestRegIdx(i));
11031060SN/A    }
11041062SN/A
11052353SN/A    if (head_inst->isCopy())
11062353SN/A        panic("Should not commit any copy instructions!");
11072353SN/A
11082292SN/A    // Finally clear the head ROB entry.
11092292SN/A    rob->retireHead(tid);
11101060SN/A
11114035Sktlim@umich.edu    // If this was a store, record it for this cycle.
11124035Sktlim@umich.edu    if (head_inst->isStore())
11134035Sktlim@umich.edu        committedStores[tid] = true;
11144035Sktlim@umich.edu
11151060SN/A    // Return true to indicate that we have committed an instruction.
11161060SN/A    return true;
11171060SN/A}
11181060SN/A
11191061SN/Atemplate <class Impl>
11201060SN/Avoid
11212292SN/ADefaultCommit<Impl>::getInsts()
11221060SN/A{
11232935Sksewell@umich.edu    DPRINTF(Commit, "Getting instructions from Rename stage.\n");
11242935Sksewell@umich.edu
11253093Sksewell@umich.edu    // Read any renamed instructions and place them into the ROB.
11263093Sksewell@umich.edu    int insts_to_process = std::min((int)renameWidth, fromRename->size);
11272965Sksewell@umich.edu
11282965Sksewell@umich.edu    for (int inst_num = 0; inst_num < insts_to_process; ++inst_num) {
11292965Sksewell@umich.edu        DynInstPtr inst;
11302965Sksewell@umich.edu
11313093Sksewell@umich.edu        inst = fromRename->insts[inst_num];
11326221Snate@binkert.org        ThreadID tid = inst->threadNumber;
11332292SN/A
11342292SN/A        if (!inst->isSquashed() &&
11354035Sktlim@umich.edu            commitStatus[tid] != ROBSquashing &&
11364035Sktlim@umich.edu            commitStatus[tid] != TrapPending) {
11372292SN/A            changedROBNumEntries[tid] = true;
11382292SN/A
11397720Sgblack@eecs.umich.edu            DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ROB.\n",
11407720Sgblack@eecs.umich.edu                    inst->pcState(), inst->seqNum, tid);
11412292SN/A
11422292SN/A            rob->insertInst(inst);
11432292SN/A
11442292SN/A            assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
11452292SN/A
11462292SN/A            youngestSeqNum[tid] = inst->seqNum;
11471061SN/A        } else {
11487720Sgblack@eecs.umich.edu            DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
11491061SN/A                    "squashed, skipping.\n",
11507720Sgblack@eecs.umich.edu                    inst->pcState(), inst->seqNum, tid);
11511061SN/A        }
11521060SN/A    }
11532965Sksewell@umich.edu}
11542965Sksewell@umich.edu
11552965Sksewell@umich.edutemplate <class Impl>
11562965Sksewell@umich.eduvoid
11572965Sksewell@umich.eduDefaultCommit<Impl>::skidInsert()
11582965Sksewell@umich.edu{
11592965Sksewell@umich.edu    DPRINTF(Commit, "Attempting to any instructions from rename into "
11602965Sksewell@umich.edu            "skidBuffer.\n");
11612965Sksewell@umich.edu
11622965Sksewell@umich.edu    for (int inst_num = 0; inst_num < fromRename->size; ++inst_num) {
11632965Sksewell@umich.edu        DynInstPtr inst = fromRename->insts[inst_num];
11642965Sksewell@umich.edu
11652965Sksewell@umich.edu        if (!inst->isSquashed()) {
11667720Sgblack@eecs.umich.edu            DPRINTF(Commit, "Inserting PC %s [sn:%i] [tid:%i] into ",
11677720Sgblack@eecs.umich.edu                    "skidBuffer.\n", inst->pcState(), inst->seqNum,
11683221Sktlim@umich.edu                    inst->threadNumber);
11692965Sksewell@umich.edu            skidBuffer.push(inst);
11702965Sksewell@umich.edu        } else {
11717720Sgblack@eecs.umich.edu            DPRINTF(Commit, "Instruction PC %s [sn:%i] [tid:%i] was "
11722965Sksewell@umich.edu                    "squashed, skipping.\n",
11737720Sgblack@eecs.umich.edu                    inst->pcState(), inst->seqNum, inst->threadNumber);
11742965Sksewell@umich.edu        }
11752965Sksewell@umich.edu    }
11761060SN/A}
11771060SN/A
11781061SN/Atemplate <class Impl>
11791060SN/Avoid
11802292SN/ADefaultCommit<Impl>::markCompletedInsts()
11811060SN/A{
11821060SN/A    // Grab completed insts out of the IEW instruction queue, and mark
11831060SN/A    // instructions completed within the ROB.
11841060SN/A    for (int inst_num = 0;
11851681SN/A         inst_num < fromIEW->size && fromIEW->insts[inst_num];
11861060SN/A         ++inst_num)
11871060SN/A    {
11882292SN/A        if (!fromIEW->insts[inst_num]->isSquashed()) {
11897720Sgblack@eecs.umich.edu            DPRINTF(Commit, "[tid:%i]: Marking PC %s, [sn:%lli] ready "
11902316SN/A                    "within ROB.\n",
11912292SN/A                    fromIEW->insts[inst_num]->threadNumber,
11927720Sgblack@eecs.umich.edu                    fromIEW->insts[inst_num]->pcState(),
11932292SN/A                    fromIEW->insts[inst_num]->seqNum);
11941060SN/A
11952292SN/A            // Mark the instruction as ready to commit.
11962292SN/A            fromIEW->insts[inst_num]->setCanCommit();
11972292SN/A        }
11981060SN/A    }
11991060SN/A}
12001060SN/A
12011061SN/Atemplate <class Impl>
12022292SN/Abool
12032292SN/ADefaultCommit<Impl>::robDoneSquashing()
12041060SN/A{
12056221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
12066221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
12072292SN/A
12083867Sbinkertn@umich.edu    while (threads != end) {
12096221Snate@binkert.org        ThreadID tid = *threads++;
12102292SN/A
12112292SN/A        if (!rob->isDoneSquashing(tid))
12122292SN/A            return false;
12132292SN/A    }
12142292SN/A
12152292SN/A    return true;
12161060SN/A}
12172292SN/A
12182301SN/Atemplate <class Impl>
12192301SN/Avoid
12202301SN/ADefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
12212301SN/A{
12226221Snate@binkert.org    ThreadID tid = inst->threadNumber;
12232301SN/A
12242301SN/A    //
12252301SN/A    //  Pick off the software prefetches
12262301SN/A    //
12272301SN/A#ifdef TARGET_ALPHA
12282301SN/A    if (inst->isDataPrefetch()) {
12296221Snate@binkert.org        statComSwp[tid]++;
12302301SN/A    } else {
12316221Snate@binkert.org        statComInst[tid]++;
12322301SN/A    }
12332301SN/A#else
12346221Snate@binkert.org    statComInst[tid]++;
12352301SN/A#endif
12362301SN/A
12372301SN/A    //
12382301SN/A    //  Control Instructions
12392301SN/A    //
12402301SN/A    if (inst->isControl())
12416221Snate@binkert.org        statComBranches[tid]++;
12422301SN/A
12432301SN/A    //
12442301SN/A    //  Memory references
12452301SN/A    //
12462301SN/A    if (inst->isMemRef()) {
12476221Snate@binkert.org        statComRefs[tid]++;
12482301SN/A
12492301SN/A        if (inst->isLoad()) {
12506221Snate@binkert.org            statComLoads[tid]++;
12512301SN/A        }
12522301SN/A    }
12532301SN/A
12542301SN/A    if (inst->isMemBarrier()) {
12556221Snate@binkert.org        statComMembars[tid]++;
12562301SN/A    }
12572301SN/A}
12582301SN/A
12592292SN/A////////////////////////////////////////
12602292SN/A//                                    //
12612316SN/A//  SMT COMMIT POLICY MAINTAINED HERE //
12622292SN/A//                                    //
12632292SN/A////////////////////////////////////////
12642292SN/Atemplate <class Impl>
12656221Snate@binkert.orgThreadID
12662292SN/ADefaultCommit<Impl>::getCommittingThread()
12672292SN/A{
12682292SN/A    if (numThreads > 1) {
12692292SN/A        switch (commitPolicy) {
12702292SN/A
12712292SN/A          case Aggressive:
12722292SN/A            //If Policy is Aggressive, commit will call
12732292SN/A            //this function multiple times per
12742292SN/A            //cycle
12752292SN/A            return oldestReady();
12762292SN/A
12772292SN/A          case RoundRobin:
12782292SN/A            return roundRobin();
12792292SN/A
12802292SN/A          case OldestReady:
12812292SN/A            return oldestReady();
12822292SN/A
12832292SN/A          default:
12846221Snate@binkert.org            return InvalidThreadID;
12852292SN/A        }
12862292SN/A    } else {
12873867Sbinkertn@umich.edu        assert(!activeThreads->empty());
12886221Snate@binkert.org        ThreadID tid = activeThreads->front();
12892292SN/A
12902292SN/A        if (commitStatus[tid] == Running ||
12912292SN/A            commitStatus[tid] == Idle ||
12922292SN/A            commitStatus[tid] == FetchTrapPending) {
12932292SN/A            return tid;
12942292SN/A        } else {
12956221Snate@binkert.org            return InvalidThreadID;
12962292SN/A        }
12972292SN/A    }
12982292SN/A}
12992292SN/A
13002292SN/Atemplate<class Impl>
13016221Snate@binkert.orgThreadID
13022292SN/ADefaultCommit<Impl>::roundRobin()
13032292SN/A{
13046221Snate@binkert.org    list<ThreadID>::iterator pri_iter = priority_list.begin();
13056221Snate@binkert.org    list<ThreadID>::iterator end      = priority_list.end();
13062292SN/A
13072292SN/A    while (pri_iter != end) {
13086221Snate@binkert.org        ThreadID tid = *pri_iter;
13092292SN/A
13102292SN/A        if (commitStatus[tid] == Running ||
13112831Sksewell@umich.edu            commitStatus[tid] == Idle ||
13122831Sksewell@umich.edu            commitStatus[tid] == FetchTrapPending) {
13132292SN/A
13142292SN/A            if (rob->isHeadReady(tid)) {
13152292SN/A                priority_list.erase(pri_iter);
13162292SN/A                priority_list.push_back(tid);
13172292SN/A
13182292SN/A                return tid;
13192292SN/A            }
13202292SN/A        }
13212292SN/A
13222292SN/A        pri_iter++;
13232292SN/A    }
13242292SN/A
13256221Snate@binkert.org    return InvalidThreadID;
13262292SN/A}
13272292SN/A
13282292SN/Atemplate<class Impl>
13296221Snate@binkert.orgThreadID
13302292SN/ADefaultCommit<Impl>::oldestReady()
13312292SN/A{
13322292SN/A    unsigned oldest = 0;
13332292SN/A    bool first = true;
13342292SN/A
13356221Snate@binkert.org    list<ThreadID>::iterator threads = activeThreads->begin();
13366221Snate@binkert.org    list<ThreadID>::iterator end = activeThreads->end();
13372292SN/A
13383867Sbinkertn@umich.edu    while (threads != end) {
13396221Snate@binkert.org        ThreadID tid = *threads++;
13402292SN/A
13412292SN/A        if (!rob->isEmpty(tid) &&
13422292SN/A            (commitStatus[tid] == Running ||
13432292SN/A             commitStatus[tid] == Idle ||
13442292SN/A             commitStatus[tid] == FetchTrapPending)) {
13452292SN/A
13462292SN/A            if (rob->isHeadReady(tid)) {
13472292SN/A
13482292SN/A                DynInstPtr head_inst = rob->readHeadInst(tid);
13492292SN/A
13502292SN/A                if (first) {
13512292SN/A                    oldest = tid;
13522292SN/A                    first = false;
13532292SN/A                } else if (head_inst->seqNum < oldest) {
13542292SN/A                    oldest = tid;
13552292SN/A                }
13562292SN/A            }
13572292SN/A        }
13582292SN/A    }
13592292SN/A
13602292SN/A    if (!first) {
13612292SN/A        return oldest;
13622292SN/A    } else {
13636221Snate@binkert.org        return InvalidThreadID;
13642292SN/A    }
13652292SN/A}
1366