commit_impl.hh revision 2864
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
291689SN/A */
301689SN/A
312733Sktlim@umich.edu#include "config/full_system.hh"
322733Sktlim@umich.edu#include "config/use_checker.hh"
332733Sktlim@umich.edu
342292SN/A#include <algorithm>
352329SN/A#include <string>
362292SN/A
372292SN/A#include "base/loader/symtab.hh"
381060SN/A#include "base/timebuf.hh"
392292SN/A#include "cpu/exetrace.hh"
401717SN/A#include "cpu/o3/commit.hh"
412292SN/A#include "cpu/o3/thread_state.hh"
422292SN/A
432790Sktlim@umich.edu#if USE_CHECKER
442790Sktlim@umich.edu#include "cpu/checker/cpu.hh"
452790Sktlim@umich.edu#endif
462790Sktlim@umich.edu
472292SN/Ausing namespace std;
481060SN/A
491061SN/Atemplate <class Impl>
502292SN/ADefaultCommit<Impl>::TrapEvent::TrapEvent(DefaultCommit<Impl> *_commit,
512292SN/A                                          unsigned _tid)
522292SN/A    : Event(&mainEventQueue, CPU_Tick_Pri), commit(_commit), tid(_tid)
531060SN/A{
542292SN/A    this->setFlags(Event::AutoDelete);
551060SN/A}
561060SN/A
571061SN/Atemplate <class Impl>
581060SN/Avoid
592292SN/ADefaultCommit<Impl>::TrapEvent::process()
601062SN/A{
612316SN/A    // This will get reset by commit if it was switched out at the
622316SN/A    // time of this event processing.
632292SN/A    commit->trapSquash[tid] = true;
642292SN/A}
652292SN/A
662292SN/Atemplate <class Impl>
672292SN/Aconst char *
682292SN/ADefaultCommit<Impl>::TrapEvent::description()
692292SN/A{
702292SN/A    return "Trap event";
712292SN/A}
722292SN/A
732292SN/Atemplate <class Impl>
742292SN/ADefaultCommit<Impl>::DefaultCommit(Params *params)
752669Sktlim@umich.edu    : squashCounter(0),
762292SN/A      iewToCommitDelay(params->iewToCommitDelay),
772292SN/A      commitToIEWDelay(params->commitToIEWDelay),
782292SN/A      renameToROBDelay(params->renameToROBDelay),
792292SN/A      fetchToCommitDelay(params->commitToFetchDelay),
802292SN/A      renameWidth(params->renameWidth),
812292SN/A      commitWidth(params->commitWidth),
822307SN/A      numThreads(params->numberOfThreads),
832843Sktlim@umich.edu      drainPending(false),
842316SN/A      switchedOut(false),
852316SN/A      trapLatency(params->trapLatency),
862316SN/A      fetchTrapLatency(params->fetchTrapLatency)
872292SN/A{
882292SN/A    _status = Active;
892292SN/A    _nextStatus = Inactive;
902292SN/A    string policy = params->smtCommitPolicy;
912292SN/A
922292SN/A    //Convert string to lowercase
932292SN/A    std::transform(policy.begin(), policy.end(), policy.begin(),
942292SN/A                   (int(*)(int)) tolower);
952292SN/A
962292SN/A    //Assign commit policy
972292SN/A    if (policy == "aggressive"){
982292SN/A        commitPolicy = Aggressive;
992292SN/A
1002292SN/A        DPRINTF(Commit,"Commit Policy set to Aggressive.");
1012292SN/A    } else if (policy == "roundrobin"){
1022292SN/A        commitPolicy = RoundRobin;
1032292SN/A
1042292SN/A        //Set-Up Priority List
1052292SN/A        for (int tid=0; tid < numThreads; tid++) {
1062292SN/A            priority_list.push_back(tid);
1072292SN/A        }
1082292SN/A
1092292SN/A        DPRINTF(Commit,"Commit Policy set to Round Robin.");
1102292SN/A    } else if (policy == "oldestready"){
1112292SN/A        commitPolicy = OldestReady;
1122292SN/A
1132292SN/A        DPRINTF(Commit,"Commit Policy set to Oldest Ready.");
1142292SN/A    } else {
1152292SN/A        assert(0 && "Invalid SMT Commit Policy. Options Are: {Aggressive,"
1162292SN/A               "RoundRobin,OldestReady}");
1172292SN/A    }
1182292SN/A
1192292SN/A    for (int i=0; i < numThreads; i++) {
1202292SN/A        commitStatus[i] = Idle;
1212292SN/A        changedROBNumEntries[i] = false;
1222292SN/A        trapSquash[i] = false;
1232680Sktlim@umich.edu        tcSquash[i] = false;
1242678Sktlim@umich.edu        PC[i] = nextPC[i] = 0;
1252292SN/A    }
1262292SN/A
1272292SN/A    fetchFaultTick = 0;
1282292SN/A    fetchTrapWait = 0;
1292292SN/A}
1302292SN/A
1312292SN/Atemplate <class Impl>
1322292SN/Astd::string
1332292SN/ADefaultCommit<Impl>::name() const
1342292SN/A{
1352292SN/A    return cpu->name() + ".commit";
1362292SN/A}
1372292SN/A
1382292SN/Atemplate <class Impl>
1392292SN/Avoid
1402292SN/ADefaultCommit<Impl>::regStats()
1412132SN/A{
1422301SN/A    using namespace Stats;
1431062SN/A    commitCommittedInsts
1441062SN/A        .name(name() + ".commitCommittedInsts")
1451062SN/A        .desc("The number of committed instructions")
1461062SN/A        .prereq(commitCommittedInsts);
1471062SN/A    commitSquashedInsts
1481062SN/A        .name(name() + ".commitSquashedInsts")
1491062SN/A        .desc("The number of squashed insts skipped by commit")
1501062SN/A        .prereq(commitSquashedInsts);
1511062SN/A    commitSquashEvents
1521062SN/A        .name(name() + ".commitSquashEvents")
1531062SN/A        .desc("The number of times commit is told to squash")
1541062SN/A        .prereq(commitSquashEvents);
1551062SN/A    commitNonSpecStalls
1561062SN/A        .name(name() + ".commitNonSpecStalls")
1571062SN/A        .desc("The number of times commit has been forced to stall to "
1581062SN/A              "communicate backwards")
1591062SN/A        .prereq(commitNonSpecStalls);
1601062SN/A    branchMispredicts
1611062SN/A        .name(name() + ".branchMispredicts")
1621062SN/A        .desc("The number of times a branch was mispredicted")
1631062SN/A        .prereq(branchMispredicts);
1642292SN/A    numCommittedDist
1651062SN/A        .init(0,commitWidth,1)
1661062SN/A        .name(name() + ".COM:committed_per_cycle")
1671062SN/A        .desc("Number of insts commited each cycle")
1681062SN/A        .flags(Stats::pdf)
1691062SN/A        ;
1702301SN/A
1712316SN/A    statComInst
1722301SN/A        .init(cpu->number_of_threads)
1732301SN/A        .name(name() + ".COM:count")
1742301SN/A        .desc("Number of instructions committed")
1752301SN/A        .flags(total)
1762301SN/A        ;
1772301SN/A
1782316SN/A    statComSwp
1792301SN/A        .init(cpu->number_of_threads)
1802301SN/A        .name(name() + ".COM:swp_count")
1812301SN/A        .desc("Number of s/w prefetches committed")
1822301SN/A        .flags(total)
1832301SN/A        ;
1842301SN/A
1852316SN/A    statComRefs
1862301SN/A        .init(cpu->number_of_threads)
1872301SN/A        .name(name() +  ".COM:refs")
1882301SN/A        .desc("Number of memory references committed")
1892301SN/A        .flags(total)
1902301SN/A        ;
1912301SN/A
1922316SN/A    statComLoads
1932301SN/A        .init(cpu->number_of_threads)
1942301SN/A        .name(name() +  ".COM:loads")
1952301SN/A        .desc("Number of loads committed")
1962301SN/A        .flags(total)
1972301SN/A        ;
1982301SN/A
1992316SN/A    statComMembars
2002301SN/A        .init(cpu->number_of_threads)
2012301SN/A        .name(name() +  ".COM:membars")
2022301SN/A        .desc("Number of memory barriers committed")
2032301SN/A        .flags(total)
2042301SN/A        ;
2052301SN/A
2062316SN/A    statComBranches
2072301SN/A        .init(cpu->number_of_threads)
2082301SN/A        .name(name() + ".COM:branches")
2092301SN/A        .desc("Number of branches committed")
2102301SN/A        .flags(total)
2112301SN/A        ;
2122301SN/A
2132316SN/A    commitEligible
2142301SN/A        .init(cpu->number_of_threads)
2152301SN/A        .name(name() + ".COM:bw_limited")
2162301SN/A        .desc("number of insts not committed due to BW limits")
2172301SN/A        .flags(total)
2182301SN/A        ;
2192301SN/A
2202316SN/A    commitEligibleSamples
2212301SN/A        .name(name() + ".COM:bw_lim_events")
2222301SN/A        .desc("number cycles where commit BW limit reached")
2232301SN/A        ;
2241062SN/A}
2251062SN/A
2261062SN/Atemplate <class Impl>
2271062SN/Avoid
2282733Sktlim@umich.eduDefaultCommit<Impl>::setCPU(O3CPU *cpu_ptr)
2291060SN/A{
2301060SN/A    DPRINTF(Commit, "Commit: Setting CPU pointer.\n");
2311060SN/A    cpu = cpu_ptr;
2322292SN/A
2332292SN/A    // Commit must broadcast the number of free entries it has at the start of
2342292SN/A    // the simulation, so it starts as active.
2352733Sktlim@umich.edu    cpu->activateStage(O3CPU::CommitIdx);
2362307SN/A
2372316SN/A    trapLatency = cpu->cycles(trapLatency);
2382316SN/A    fetchTrapLatency = cpu->cycles(fetchTrapLatency);
2391060SN/A}
2401060SN/A
2411061SN/Atemplate <class Impl>
2421060SN/Avoid
2432292SN/ADefaultCommit<Impl>::setThreads(vector<Thread *> &threads)
2442292SN/A{
2452292SN/A    thread = threads;
2462292SN/A}
2472292SN/A
2482292SN/Atemplate <class Impl>
2492292SN/Avoid
2502292SN/ADefaultCommit<Impl>::setTimeBuffer(TimeBuffer<TimeStruct> *tb_ptr)
2511060SN/A{
2521060SN/A    DPRINTF(Commit, "Commit: Setting time buffer pointer.\n");
2531060SN/A    timeBuffer = tb_ptr;
2541060SN/A
2551060SN/A    // Setup wire to send information back to IEW.
2561060SN/A    toIEW = timeBuffer->getWire(0);
2571060SN/A
2581060SN/A    // Setup wire to read data from IEW (for the ROB).
2591060SN/A    robInfoFromIEW = timeBuffer->getWire(-iewToCommitDelay);
2601060SN/A}
2611060SN/A
2621061SN/Atemplate <class Impl>
2631060SN/Avoid
2642292SN/ADefaultCommit<Impl>::setFetchQueue(TimeBuffer<FetchStruct> *fq_ptr)
2652292SN/A{
2662292SN/A    DPRINTF(Commit, "Commit: Setting fetch queue pointer.\n");
2672292SN/A    fetchQueue = fq_ptr;
2682292SN/A
2692292SN/A    // Setup wire to get instructions from rename (for the ROB).
2702292SN/A    fromFetch = fetchQueue->getWire(-fetchToCommitDelay);
2712292SN/A}
2722292SN/A
2732292SN/Atemplate <class Impl>
2742292SN/Avoid
2752292SN/ADefaultCommit<Impl>::setRenameQueue(TimeBuffer<RenameStruct> *rq_ptr)
2761060SN/A{
2771060SN/A    DPRINTF(Commit, "Commit: Setting rename queue pointer.\n");
2781060SN/A    renameQueue = rq_ptr;
2791060SN/A
2801060SN/A    // Setup wire to get instructions from rename (for the ROB).
2811060SN/A    fromRename = renameQueue->getWire(-renameToROBDelay);
2821060SN/A}
2831060SN/A
2841061SN/Atemplate <class Impl>
2851060SN/Avoid
2862292SN/ADefaultCommit<Impl>::setIEWQueue(TimeBuffer<IEWStruct> *iq_ptr)
2871060SN/A{
2881060SN/A    DPRINTF(Commit, "Commit: Setting IEW queue pointer.\n");
2891060SN/A    iewQueue = iq_ptr;
2901060SN/A
2911060SN/A    // Setup wire to get instructions from IEW.
2921060SN/A    fromIEW = iewQueue->getWire(-iewToCommitDelay);
2931060SN/A}
2941060SN/A
2951061SN/Atemplate <class Impl>
2961060SN/Avoid
2972316SN/ADefaultCommit<Impl>::setFetchStage(Fetch *fetch_stage)
2982316SN/A{
2992316SN/A    fetchStage = fetch_stage;
3002316SN/A}
3012316SN/A
3022316SN/Atemplate <class Impl>
3032316SN/Avoid
3042292SN/ADefaultCommit<Impl>::setIEWStage(IEW *iew_stage)
3052292SN/A{
3062292SN/A    iewStage = iew_stage;
3072292SN/A}
3082292SN/A
3092292SN/Atemplate<class Impl>
3102292SN/Avoid
3112292SN/ADefaultCommit<Impl>::setActiveThreads(list<unsigned> *at_ptr)
3122292SN/A{
3132292SN/A    DPRINTF(Commit, "Commit: Setting active threads list pointer.\n");
3142292SN/A    activeThreads = at_ptr;
3152292SN/A}
3162292SN/A
3172292SN/Atemplate <class Impl>
3182292SN/Avoid
3192292SN/ADefaultCommit<Impl>::setRenameMap(RenameMap rm_ptr[])
3202292SN/A{
3212292SN/A    DPRINTF(Commit, "Setting rename map pointers.\n");
3222292SN/A
3232292SN/A    for (int i=0; i < numThreads; i++) {
3242292SN/A        renameMap[i] = &rm_ptr[i];
3252292SN/A    }
3262292SN/A}
3272292SN/A
3282292SN/Atemplate <class Impl>
3292292SN/Avoid
3302292SN/ADefaultCommit<Impl>::setROB(ROB *rob_ptr)
3311060SN/A{
3321060SN/A    DPRINTF(Commit, "Commit: Setting ROB pointer.\n");
3331060SN/A    rob = rob_ptr;
3341060SN/A}
3351060SN/A
3361061SN/Atemplate <class Impl>
3371060SN/Avoid
3382292SN/ADefaultCommit<Impl>::initStage()
3391060SN/A{
3402292SN/A    rob->setActiveThreads(activeThreads);
3412292SN/A    rob->resetEntries();
3421060SN/A
3432292SN/A    // Broadcast the number of free entries.
3442292SN/A    for (int i=0; i < numThreads; i++) {
3452292SN/A        toIEW->commitInfo[i].usedROB = true;
3462292SN/A        toIEW->commitInfo[i].freeROBEntries = rob->numFreeEntries(i);
3471060SN/A    }
3481060SN/A
3492292SN/A    cpu->activityThisCycle();
3501060SN/A}
3511060SN/A
3521061SN/Atemplate <class Impl>
3532863Sktlim@umich.edubool
3542843Sktlim@umich.eduDefaultCommit<Impl>::drain()
3551060SN/A{
3562843Sktlim@umich.edu    drainPending = true;
3572863Sktlim@umich.edu
3582863Sktlim@umich.edu    // If it's already drained, return true.
3592863Sktlim@umich.edu    if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
3602863Sktlim@umich.edu        cpu->signalDrained();
3612863Sktlim@umich.edu        return true;
3622863Sktlim@umich.edu    }
3632863Sktlim@umich.edu
3642863Sktlim@umich.edu    return false;
3652316SN/A}
3662316SN/A
3672316SN/Atemplate <class Impl>
3682316SN/Avoid
3692843Sktlim@umich.eduDefaultCommit<Impl>::switchOut()
3702316SN/A{
3712316SN/A    switchedOut = true;
3722843Sktlim@umich.edu    drainPending = false;
3732307SN/A    rob->switchOut();
3742307SN/A}
3752307SN/A
3762307SN/Atemplate <class Impl>
3772307SN/Avoid
3782843Sktlim@umich.eduDefaultCommit<Impl>::resume()
3792843Sktlim@umich.edu{
3802864Sktlim@umich.edu    drainPending = false;
3812843Sktlim@umich.edu}
3822843Sktlim@umich.edu
3832843Sktlim@umich.edutemplate <class Impl>
3842843Sktlim@umich.eduvoid
3852307SN/ADefaultCommit<Impl>::takeOverFrom()
3862307SN/A{
3872316SN/A    switchedOut = false;
3882307SN/A    _status = Active;
3892307SN/A    _nextStatus = Inactive;
3902307SN/A    for (int i=0; i < numThreads; i++) {
3912307SN/A        commitStatus[i] = Idle;
3922307SN/A        changedROBNumEntries[i] = false;
3932307SN/A        trapSquash[i] = false;
3942680Sktlim@umich.edu        tcSquash[i] = false;
3952307SN/A    }
3962307SN/A    squashCounter = 0;
3972307SN/A    rob->takeOverFrom();
3982307SN/A}
3992307SN/A
4002307SN/Atemplate <class Impl>
4012307SN/Avoid
4022292SN/ADefaultCommit<Impl>::updateStatus()
4032132SN/A{
4042316SN/A    // reset ROB changed variable
4052316SN/A    list<unsigned>::iterator threads = (*activeThreads).begin();
4062316SN/A    while (threads != (*activeThreads).end()) {
4072316SN/A        unsigned tid = *threads++;
4082316SN/A        changedROBNumEntries[tid] = false;
4092316SN/A
4102316SN/A        // Also check if any of the threads has a trap pending
4112316SN/A        if (commitStatus[tid] == TrapPending ||
4122316SN/A            commitStatus[tid] == FetchTrapPending) {
4132316SN/A            _nextStatus = Active;
4142316SN/A        }
4152292SN/A    }
4162292SN/A
4172292SN/A    if (_nextStatus == Inactive && _status == Active) {
4182292SN/A        DPRINTF(Activity, "Deactivating stage.\n");
4192733Sktlim@umich.edu        cpu->deactivateStage(O3CPU::CommitIdx);
4202292SN/A    } else if (_nextStatus == Active && _status == Inactive) {
4212292SN/A        DPRINTF(Activity, "Activating stage.\n");
4222733Sktlim@umich.edu        cpu->activateStage(O3CPU::CommitIdx);
4232292SN/A    }
4242292SN/A
4252292SN/A    _status = _nextStatus;
4262292SN/A}
4272292SN/A
4282292SN/Atemplate <class Impl>
4292292SN/Avoid
4302292SN/ADefaultCommit<Impl>::setNextStatus()
4312292SN/A{
4322292SN/A    int squashes = 0;
4332292SN/A
4342292SN/A    list<unsigned>::iterator threads = (*activeThreads).begin();
4352292SN/A
4362292SN/A    while (threads != (*activeThreads).end()) {
4372292SN/A        unsigned tid = *threads++;
4382292SN/A
4392292SN/A        if (commitStatus[tid] == ROBSquashing) {
4402292SN/A            squashes++;
4412292SN/A        }
4422292SN/A    }
4432292SN/A
4442702Sktlim@umich.edu    squashCounter = squashes;
4452292SN/A
4462292SN/A    // If commit is currently squashing, then it will have activity for the
4472292SN/A    // next cycle. Set its next status as active.
4482292SN/A    if (squashCounter) {
4492292SN/A        _nextStatus = Active;
4502292SN/A    }
4512292SN/A}
4522292SN/A
4532292SN/Atemplate <class Impl>
4542292SN/Abool
4552292SN/ADefaultCommit<Impl>::changedROBEntries()
4562292SN/A{
4572292SN/A    list<unsigned>::iterator threads = (*activeThreads).begin();
4582292SN/A
4592292SN/A    while (threads != (*activeThreads).end()) {
4602292SN/A        unsigned tid = *threads++;
4612292SN/A
4622292SN/A        if (changedROBNumEntries[tid]) {
4632292SN/A            return true;
4642292SN/A        }
4652292SN/A    }
4662292SN/A
4672292SN/A    return false;
4682292SN/A}
4692292SN/A
4702292SN/Atemplate <class Impl>
4712292SN/Aunsigned
4722292SN/ADefaultCommit<Impl>::numROBFreeEntries(unsigned tid)
4732292SN/A{
4742292SN/A    return rob->numFreeEntries(tid);
4752292SN/A}
4762292SN/A
4772292SN/Atemplate <class Impl>
4782292SN/Avoid
4792292SN/ADefaultCommit<Impl>::generateTrapEvent(unsigned tid)
4802292SN/A{
4812292SN/A    DPRINTF(Commit, "Generating trap event for [tid:%i]\n", tid);
4822292SN/A
4832292SN/A    TrapEvent *trap = new TrapEvent(this, tid);
4842292SN/A
4852292SN/A    trap->schedule(curTick + trapLatency);
4862292SN/A
4872292SN/A    thread[tid]->trapPending = true;
4882292SN/A}
4892292SN/A
4902292SN/Atemplate <class Impl>
4912292SN/Avoid
4922680Sktlim@umich.eduDefaultCommit<Impl>::generateTCEvent(unsigned tid)
4932292SN/A{
4942680Sktlim@umich.edu    DPRINTF(Commit, "Generating TC squash event for [tid:%i]\n", tid);
4952292SN/A
4962680Sktlim@umich.edu    tcSquash[tid] = true;
4972292SN/A}
4982292SN/A
4992292SN/Atemplate <class Impl>
5002292SN/Avoid
5012316SN/ADefaultCommit<Impl>::squashAll(unsigned tid)
5022292SN/A{
5032292SN/A    // If we want to include the squashing instruction in the squash,
5042292SN/A    // then use one older sequence number.
5052292SN/A    // Hopefully this doesn't mess things up.  Basically I want to squash
5062292SN/A    // all instructions of this thread.
5072292SN/A    InstSeqNum squashed_inst = rob->isEmpty() ?
5082292SN/A        0 : rob->readHeadInst(tid)->seqNum - 1;;
5092292SN/A
5102292SN/A    // All younger instructions will be squashed. Set the sequence
5112292SN/A    // number as the youngest instruction in the ROB (0 in this case.
5122292SN/A    // Hopefully nothing breaks.)
5132292SN/A    youngestSeqNum[tid] = 0;
5142292SN/A
5152292SN/A    rob->squash(squashed_inst, tid);
5162292SN/A    changedROBNumEntries[tid] = true;
5172292SN/A
5182292SN/A    // Send back the sequence number of the squashed instruction.
5192292SN/A    toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
5202292SN/A
5212292SN/A    // Send back the squash signal to tell stages that they should
5222292SN/A    // squash.
5232292SN/A    toIEW->commitInfo[tid].squash = true;
5242292SN/A
5252292SN/A    // Send back the rob squashing signal so other stages know that
5262292SN/A    // the ROB is in the process of squashing.
5272292SN/A    toIEW->commitInfo[tid].robSquashing = true;
5282292SN/A
5292292SN/A    toIEW->commitInfo[tid].branchMispredict = false;
5302292SN/A
5312316SN/A    toIEW->commitInfo[tid].nextPC = PC[tid];
5322316SN/A}
5332292SN/A
5342316SN/Atemplate <class Impl>
5352316SN/Avoid
5362316SN/ADefaultCommit<Impl>::squashFromTrap(unsigned tid)
5372316SN/A{
5382316SN/A    squashAll(tid);
5392316SN/A
5402316SN/A    DPRINTF(Commit, "Squashing from trap, restarting at PC %#x\n", PC[tid]);
5412316SN/A
5422316SN/A    thread[tid]->trapPending = false;
5432316SN/A    thread[tid]->inSyscall = false;
5442316SN/A
5452316SN/A    trapSquash[tid] = false;
5462316SN/A
5472316SN/A    commitStatus[tid] = ROBSquashing;
5482316SN/A    cpu->activityThisCycle();
5492316SN/A}
5502316SN/A
5512316SN/Atemplate <class Impl>
5522316SN/Avoid
5532680Sktlim@umich.eduDefaultCommit<Impl>::squashFromTC(unsigned tid)
5542316SN/A{
5552316SN/A    squashAll(tid);
5562292SN/A
5572680Sktlim@umich.edu    DPRINTF(Commit, "Squashing from TC, restarting at PC %#x\n", PC[tid]);
5582292SN/A
5592292SN/A    thread[tid]->inSyscall = false;
5602292SN/A    assert(!thread[tid]->trapPending);
5612316SN/A
5622292SN/A    commitStatus[tid] = ROBSquashing;
5632292SN/A    cpu->activityThisCycle();
5642292SN/A
5652680Sktlim@umich.edu    tcSquash[tid] = false;
5662292SN/A}
5672292SN/A
5682292SN/Atemplate <class Impl>
5692292SN/Avoid
5702292SN/ADefaultCommit<Impl>::tick()
5712292SN/A{
5722292SN/A    wroteToTimeBuffer = false;
5732292SN/A    _nextStatus = Inactive;
5742292SN/A
5752843Sktlim@umich.edu    if (drainPending && rob->isEmpty() && !iewStage->hasStoresToWB()) {
5762843Sktlim@umich.edu        cpu->signalDrained();
5772843Sktlim@umich.edu        drainPending = false;
5782316SN/A        return;
5792316SN/A    }
5802316SN/A
5812292SN/A    list<unsigned>::iterator threads = (*activeThreads).begin();
5822292SN/A
5832316SN/A    // Check if any of the threads are done squashing.  Change the
5842316SN/A    // status if they are done.
5852292SN/A    while (threads != (*activeThreads).end()) {
5862292SN/A        unsigned tid = *threads++;
5872292SN/A
5882292SN/A        if (commitStatus[tid] == ROBSquashing) {
5892292SN/A
5902292SN/A            if (rob->isDoneSquashing(tid)) {
5912292SN/A                commitStatus[tid] = Running;
5922292SN/A            } else {
5932292SN/A                DPRINTF(Commit,"[tid:%u]: Still Squashing, cannot commit any"
5942292SN/A                        "insts this cycle.\n", tid);
5952702Sktlim@umich.edu                rob->doSquash(tid);
5962702Sktlim@umich.edu                toIEW->commitInfo[tid].robSquashing = true;
5972702Sktlim@umich.edu                wroteToTimeBuffer = true;
5982292SN/A            }
5992292SN/A        }
6002292SN/A    }
6012292SN/A
6022292SN/A    commit();
6032292SN/A
6042292SN/A    markCompletedInsts();
6052292SN/A
6062292SN/A    threads = (*activeThreads).begin();
6072292SN/A
6082292SN/A    while (threads != (*activeThreads).end()) {
6092292SN/A        unsigned tid = *threads++;
6102292SN/A
6112292SN/A        if (!rob->isEmpty(tid) && rob->readHeadInst(tid)->readyToCommit()) {
6122292SN/A            // The ROB has more instructions it can commit. Its next status
6132292SN/A            // will be active.
6142292SN/A            _nextStatus = Active;
6152292SN/A
6162292SN/A            DynInstPtr inst = rob->readHeadInst(tid);
6172292SN/A
6182292SN/A            DPRINTF(Commit,"[tid:%i]: Instruction [sn:%lli] PC %#x is head of"
6192292SN/A                    " ROB and ready to commit\n",
6202292SN/A                    tid, inst->seqNum, inst->readPC());
6212292SN/A
6222292SN/A        } else if (!rob->isEmpty(tid)) {
6232292SN/A            DynInstPtr inst = rob->readHeadInst(tid);
6242292SN/A
6252292SN/A            DPRINTF(Commit,"[tid:%i]: Can't commit, Instruction [sn:%lli] PC "
6262292SN/A                    "%#x is head of ROB and not ready\n",
6272292SN/A                    tid, inst->seqNum, inst->readPC());
6282292SN/A        }
6292292SN/A
6302292SN/A        DPRINTF(Commit, "[tid:%i]: ROB has %d insts & %d free entries.\n",
6312292SN/A                tid, rob->countInsts(tid), rob->numFreeEntries(tid));
6322292SN/A    }
6332292SN/A
6342292SN/A
6352292SN/A    if (wroteToTimeBuffer) {
6362316SN/A        DPRINTF(Activity, "Activity This Cycle.\n");
6372292SN/A        cpu->activityThisCycle();
6382292SN/A    }
6392292SN/A
6402292SN/A    updateStatus();
6412292SN/A}
6422292SN/A
6432292SN/Atemplate <class Impl>
6442292SN/Avoid
6452292SN/ADefaultCommit<Impl>::commit()
6462292SN/A{
6472292SN/A
6481060SN/A    //////////////////////////////////////
6491060SN/A    // Check for interrupts
6501060SN/A    //////////////////////////////////////
6511060SN/A
6521858SN/A#if FULL_SYSTEM
6532316SN/A    // Process interrupts if interrupts are enabled, not in PAL mode,
6542316SN/A    // and no other traps or external squashes are currently pending.
6552316SN/A    // @todo: Allow other threads to handle interrupts.
6562292SN/A    if (cpu->checkInterrupts &&
6571060SN/A        cpu->check_interrupts() &&
6582292SN/A        !cpu->inPalMode(readPC()) &&
6592292SN/A        !trapSquash[0] &&
6602680Sktlim@umich.edu        !tcSquash[0]) {
6612316SN/A        // Tell fetch that there is an interrupt pending.  This will
6622316SN/A        // make fetch wait until it sees a non PAL-mode PC, at which
6632316SN/A        // point it stops fetching instructions.
6642292SN/A        toIEW->commitInfo[0].interruptPending = true;
6651060SN/A
6662316SN/A        // Wait until the ROB is empty and all stores have drained in
6672316SN/A        // order to enter the interrupt.
6682292SN/A        if (rob->isEmpty() && !iewStage->hasStoresToWB()) {
6692292SN/A            // Not sure which thread should be the one to interrupt.  For now
6702292SN/A            // always do thread 0.
6712292SN/A            assert(!thread[0]->inSyscall);
6722292SN/A            thread[0]->inSyscall = true;
6732292SN/A
6742292SN/A            // CPU will handle implementation of the interrupt.
6752292SN/A            cpu->processInterrupts();
6762292SN/A
6772292SN/A            // Now squash or record that I need to squash this cycle.
6782292SN/A            commitStatus[0] = TrapPending;
6792292SN/A
6802292SN/A            // Exit state update mode to avoid accidental updating.
6812292SN/A            thread[0]->inSyscall = false;
6822292SN/A
6832292SN/A            // Generate trap squash event.
6842292SN/A            generateTrapEvent(0);
6852292SN/A
6862292SN/A            toIEW->commitInfo[0].clearInterrupt = true;
6872292SN/A
6882292SN/A            DPRINTF(Commit, "Interrupt detected.\n");
6892292SN/A        } else {
6902292SN/A            DPRINTF(Commit, "Interrupt pending, waiting for ROB to empty.\n");
6912292SN/A        }
6921060SN/A    }
6931060SN/A#endif // FULL_SYSTEM
6941060SN/A
6951060SN/A    ////////////////////////////////////
6962316SN/A    // Check for any possible squashes, handle them first
6971060SN/A    ////////////////////////////////////
6981060SN/A
6992292SN/A    list<unsigned>::iterator threads = (*activeThreads).begin();
7001060SN/A
7012292SN/A    while (threads != (*activeThreads).end()) {
7022292SN/A        unsigned tid = *threads++;
7031060SN/A
7042292SN/A        // Not sure which one takes priority.  I think if we have
7052292SN/A        // both, that's a bad sign.
7062292SN/A        if (trapSquash[tid] == true) {
7072680Sktlim@umich.edu            assert(!tcSquash[tid]);
7082292SN/A            squashFromTrap(tid);
7092680Sktlim@umich.edu        } else if (tcSquash[tid] == true) {
7102680Sktlim@umich.edu            squashFromTC(tid);
7112292SN/A        }
7121061SN/A
7132292SN/A        // Squashed sequence number must be older than youngest valid
7142292SN/A        // instruction in the ROB. This prevents squashes from younger
7152292SN/A        // instructions overriding squashes from older instructions.
7162292SN/A        if (fromIEW->squash[tid] &&
7172292SN/A            commitStatus[tid] != TrapPending &&
7182292SN/A            fromIEW->squashedSeqNum[tid] <= youngestSeqNum[tid]) {
7191061SN/A
7202292SN/A            DPRINTF(Commit, "[tid:%i]: Squashing due to PC %#x [sn:%i]\n",
7212292SN/A                    tid,
7222292SN/A                    fromIEW->mispredPC[tid],
7232292SN/A                    fromIEW->squashedSeqNum[tid]);
7241061SN/A
7252292SN/A            DPRINTF(Commit, "[tid:%i]: Redirecting to PC %#x\n",
7262292SN/A                    tid,
7272292SN/A                    fromIEW->nextPC[tid]);
7281061SN/A
7292292SN/A            commitStatus[tid] = ROBSquashing;
7301061SN/A
7312292SN/A            // If we want to include the squashing instruction in the squash,
7322292SN/A            // then use one older sequence number.
7332292SN/A            InstSeqNum squashed_inst = fromIEW->squashedSeqNum[tid];
7341062SN/A
7352292SN/A            if (fromIEW->includeSquashInst[tid] == true)
7362292SN/A                squashed_inst--;
7372292SN/A
7382292SN/A            // All younger instructions will be squashed. Set the sequence
7392292SN/A            // number as the youngest instruction in the ROB.
7402292SN/A            youngestSeqNum[tid] = squashed_inst;
7412292SN/A
7422292SN/A            rob->squash(squashed_inst, tid);
7432292SN/A            changedROBNumEntries[tid] = true;
7442292SN/A
7452292SN/A            toIEW->commitInfo[tid].doneSeqNum = squashed_inst;
7462292SN/A
7472292SN/A            toIEW->commitInfo[tid].squash = true;
7482292SN/A
7492292SN/A            // Send back the rob squashing signal so other stages know that
7502292SN/A            // the ROB is in the process of squashing.
7512292SN/A            toIEW->commitInfo[tid].robSquashing = true;
7522292SN/A
7532292SN/A            toIEW->commitInfo[tid].branchMispredict =
7542292SN/A                fromIEW->branchMispredict[tid];
7552292SN/A
7562292SN/A            toIEW->commitInfo[tid].branchTaken =
7572292SN/A                fromIEW->branchTaken[tid];
7582292SN/A
7592292SN/A            toIEW->commitInfo[tid].nextPC = fromIEW->nextPC[tid];
7602292SN/A
7612316SN/A            toIEW->commitInfo[tid].mispredPC = fromIEW->mispredPC[tid];
7622292SN/A
7632292SN/A            if (toIEW->commitInfo[tid].branchMispredict) {
7642292SN/A                ++branchMispredicts;
7652292SN/A            }
7661062SN/A        }
7672292SN/A
7681060SN/A    }
7691060SN/A
7702292SN/A    setNextStatus();
7712292SN/A
7722292SN/A    if (squashCounter != numThreads) {
7731061SN/A        // If we're not currently squashing, then get instructions.
7741060SN/A        getInsts();
7751060SN/A
7761061SN/A        // Try to commit any instructions.
7771060SN/A        commitInsts();
7781060SN/A    }
7791060SN/A
7802292SN/A    //Check for any activity
7812292SN/A    threads = (*activeThreads).begin();
7822292SN/A
7832292SN/A    while (threads != (*activeThreads).end()) {
7842292SN/A        unsigned tid = *threads++;
7852292SN/A
7862292SN/A        if (changedROBNumEntries[tid]) {
7872292SN/A            toIEW->commitInfo[tid].usedROB = true;
7882292SN/A            toIEW->commitInfo[tid].freeROBEntries = rob->numFreeEntries(tid);
7892292SN/A
7902292SN/A            if (rob->isEmpty(tid)) {
7912292SN/A                toIEW->commitInfo[tid].emptyROB = true;
7922292SN/A            }
7932292SN/A
7942292SN/A            wroteToTimeBuffer = true;
7952292SN/A            changedROBNumEntries[tid] = false;
7962292SN/A        }
7971060SN/A    }
7981060SN/A}
7991060SN/A
8001061SN/Atemplate <class Impl>
8011060SN/Avoid
8022292SN/ADefaultCommit<Impl>::commitInsts()
8031060SN/A{
8041060SN/A    ////////////////////////////////////
8051060SN/A    // Handle commit
8062316SN/A    // Note that commit will be handled prior to putting new
8072316SN/A    // instructions in the ROB so that the ROB only tries to commit
8082316SN/A    // instructions it has in this current cycle, and not instructions
8092316SN/A    // it is writing in during this cycle.  Can't commit and squash
8102316SN/A    // things at the same time...
8111060SN/A    ////////////////////////////////////
8121060SN/A
8132292SN/A    DPRINTF(Commit, "Trying to commit instructions in the ROB.\n");
8141060SN/A
8151060SN/A    unsigned num_committed = 0;
8161060SN/A
8172292SN/A    DynInstPtr head_inst;
8182316SN/A
8191060SN/A    // Commit as many instructions as possible until the commit bandwidth
8201060SN/A    // limit is reached, or it becomes impossible to commit any more.
8212292SN/A    while (num_committed < commitWidth) {
8222292SN/A        int commit_thread = getCommittingThread();
8231060SN/A
8242292SN/A        if (commit_thread == -1 || !rob->isHeadReady(commit_thread))
8252292SN/A            break;
8262292SN/A
8272292SN/A        head_inst = rob->readHeadInst(commit_thread);
8282292SN/A
8292292SN/A        int tid = head_inst->threadNumber;
8302292SN/A
8312292SN/A        assert(tid == commit_thread);
8322292SN/A
8332292SN/A        DPRINTF(Commit, "Trying to commit head instruction, [sn:%i] [tid:%i]\n",
8342292SN/A                head_inst->seqNum, tid);
8352132SN/A
8362316SN/A        // If the head instruction is squashed, it is ready to retire
8372316SN/A        // (be removed from the ROB) at any time.
8381060SN/A        if (head_inst->isSquashed()) {
8391060SN/A
8402292SN/A            DPRINTF(Commit, "Retiring squashed instruction from "
8411060SN/A                    "ROB.\n");
8421060SN/A
8432292SN/A            rob->retireHead(commit_thread);
8441060SN/A
8451062SN/A            ++commitSquashedInsts;
8461062SN/A
8472292SN/A            // Record that the number of ROB entries has changed.
8482292SN/A            changedROBNumEntries[tid] = true;
8491060SN/A        } else {
8502292SN/A            PC[tid] = head_inst->readPC();
8512292SN/A            nextPC[tid] = head_inst->readNextPC();
8522292SN/A
8531060SN/A            // Increment the total number of non-speculative instructions
8541060SN/A            // executed.
8551060SN/A            // Hack for now: it really shouldn't happen until after the
8561061SN/A            // commit is deemed to be successful, but this count is needed
8571061SN/A            // for syscalls.
8582292SN/A            thread[tid]->funcExeInst++;
8591060SN/A
8601060SN/A            // Try to commit the head instruction.
8611060SN/A            bool commit_success = commitHead(head_inst, num_committed);
8621060SN/A
8631062SN/A            if (commit_success) {
8641060SN/A                ++num_committed;
8651060SN/A
8662292SN/A                changedROBNumEntries[tid] = true;
8672292SN/A
8682292SN/A                // Set the doneSeqNum to the youngest committed instruction.
8692292SN/A                toIEW->commitInfo[tid].doneSeqNum = head_inst->seqNum;
8701060SN/A
8711062SN/A                ++commitCommittedInsts;
8721062SN/A
8732292SN/A                // To match the old model, don't count nops and instruction
8742292SN/A                // prefetches towards the total commit count.
8752292SN/A                if (!head_inst->isNop() && !head_inst->isInstPrefetch()) {
8762292SN/A                    cpu->instDone(tid);
8771062SN/A                }
8782292SN/A
8792292SN/A                PC[tid] = nextPC[tid];
8802307SN/A                nextPC[tid] = nextPC[tid] + sizeof(TheISA::MachInst);
8812292SN/A#if FULL_SYSTEM
8822292SN/A                int count = 0;
8832292SN/A                Addr oldpc;
8842292SN/A                do {
8852316SN/A                    // Debug statement.  Checks to make sure we're not
8862316SN/A                    // currently updating state while handling PC events.
8872292SN/A                    if (count == 0)
8882316SN/A                        assert(!thread[tid]->inSyscall &&
8892316SN/A                               !thread[tid]->trapPending);
8902292SN/A                    oldpc = PC[tid];
8912292SN/A                    cpu->system->pcEventQueue.service(
8922690Sktlim@umich.edu                        thread[tid]->getTC());
8932292SN/A                    count++;
8942292SN/A                } while (oldpc != PC[tid]);
8952292SN/A                if (count > 1) {
8962292SN/A                    DPRINTF(Commit, "PC skip function event, stopping commit\n");
8972292SN/A                    break;
8982292SN/A                }
8992292SN/A#endif
9001060SN/A            } else {
9012292SN/A                DPRINTF(Commit, "Unable to commit head instruction PC:%#x "
9022292SN/A                        "[tid:%i] [sn:%i].\n",
9032292SN/A                        head_inst->readPC(), tid ,head_inst->seqNum);
9041060SN/A                break;
9051060SN/A            }
9061060SN/A        }
9071060SN/A    }
9081062SN/A
9091063SN/A    DPRINTF(CommitRate, "%i\n", num_committed);
9102292SN/A    numCommittedDist.sample(num_committed);
9112307SN/A
9122307SN/A    if (num_committed == commitWidth) {
9132349SN/A        commitEligibleSamples++;
9142307SN/A    }
9151060SN/A}
9161060SN/A
9171061SN/Atemplate <class Impl>
9181060SN/Abool
9192292SN/ADefaultCommit<Impl>::commitHead(DynInstPtr &head_inst, unsigned inst_num)
9201060SN/A{
9211060SN/A    assert(head_inst);
9221060SN/A
9232292SN/A    int tid = head_inst->threadNumber;
9242292SN/A
9252316SN/A    // If the instruction is not executed yet, then it will need extra
9262316SN/A    // handling.  Signal backwards that it should be executed.
9271061SN/A    if (!head_inst->isExecuted()) {
9281061SN/A        // Keep this number correct.  We have not yet actually executed
9291061SN/A        // and committed this instruction.
9302292SN/A        thread[tid]->funcExeInst--;
9311062SN/A
9322731Sktlim@umich.edu        head_inst->setAtCommit();
9331060SN/A
9342292SN/A        if (head_inst->isNonSpeculative() ||
9352348SN/A            head_inst->isStoreConditional() ||
9362292SN/A            head_inst->isMemBarrier() ||
9372292SN/A            head_inst->isWriteBarrier()) {
9382316SN/A
9392316SN/A            DPRINTF(Commit, "Encountered a barrier or non-speculative "
9402316SN/A                    "instruction [sn:%lli] at the head of the ROB, PC %#x.\n",
9412316SN/A                    head_inst->seqNum, head_inst->readPC());
9422316SN/A
9432292SN/A#if !FULL_SYSTEM
9442316SN/A            // Hack to make sure syscalls/memory barriers/quiesces
9452316SN/A            // aren't executed until all stores write back their data.
9462316SN/A            // This direct communication shouldn't be used for
9472316SN/A            // anything other than this.
9482292SN/A            if (inst_num > 0 || iewStage->hasStoresToWB())
9492292SN/A#else
9502292SN/A            if ((head_inst->isMemBarrier() || head_inst->isWriteBarrier() ||
9512292SN/A                    head_inst->isQuiesce()) &&
9522292SN/A                iewStage->hasStoresToWB())
9532292SN/A#endif
9542292SN/A            {
9552292SN/A                DPRINTF(Commit, "Waiting for all stores to writeback.\n");
9562292SN/A                return false;
9572292SN/A            }
9582292SN/A
9592292SN/A            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
9601061SN/A
9611061SN/A            // Change the instruction so it won't try to commit again until
9621061SN/A            // it is executed.
9631061SN/A            head_inst->clearCanCommit();
9641061SN/A
9651062SN/A            ++commitNonSpecStalls;
9661062SN/A
9671061SN/A            return false;
9682292SN/A        } else if (head_inst->isLoad()) {
9692292SN/A            DPRINTF(Commit, "[sn:%lli]: Uncached load, PC %#x.\n",
9702292SN/A                    head_inst->seqNum, head_inst->readPC());
9712292SN/A
9722292SN/A            // Send back the non-speculative instruction's sequence
9732316SN/A            // number.  Tell the lsq to re-execute the load.
9742292SN/A            toIEW->commitInfo[tid].nonSpecSeqNum = head_inst->seqNum;
9752292SN/A            toIEW->commitInfo[tid].uncached = true;
9762292SN/A            toIEW->commitInfo[tid].uncachedLoad = head_inst;
9772292SN/A
9782292SN/A            head_inst->clearCanCommit();
9792292SN/A
9802292SN/A            return false;
9811061SN/A        } else {
9822292SN/A            panic("Trying to commit un-executed instruction "
9831061SN/A                  "of unknown type!\n");
9841061SN/A        }
9851060SN/A    }
9861060SN/A
9872316SN/A    if (head_inst->isThreadSync()) {
9882292SN/A        // Not handled for now.
9892316SN/A        panic("Thread sync instructions are not handled yet.\n");
9902132SN/A    }
9912132SN/A
9922316SN/A    // Stores mark themselves as completed.
9932310SN/A    if (!head_inst->isStore()) {
9942310SN/A        head_inst->setCompleted();
9952310SN/A    }
9962310SN/A
9972733Sktlim@umich.edu#if USE_CHECKER
9982316SN/A    // Use checker prior to updating anything due to traps or PC
9992316SN/A    // based events.
10002316SN/A    if (cpu->checker) {
10012732Sktlim@umich.edu        cpu->checker->verify(head_inst);
10021060SN/A    }
10032733Sktlim@umich.edu#endif
10041060SN/A
10051060SN/A    // Check if the instruction caused a fault.  If so, trap.
10062132SN/A    Fault inst_fault = head_inst->getFault();
10071681SN/A
10082112SN/A    if (inst_fault != NoFault) {
10092316SN/A        head_inst->setCompleted();
10102316SN/A        DPRINTF(Commit, "Inst [sn:%lli] PC %#x has a fault\n",
10112316SN/A                head_inst->seqNum, head_inst->readPC());
10122292SN/A
10132316SN/A        if (iewStage->hasStoresToWB() || inst_num > 0) {
10142316SN/A            DPRINTF(Commit, "Stores outstanding, fault must wait.\n");
10152316SN/A            return false;
10162316SN/A        }
10172310SN/A
10182733Sktlim@umich.edu#if USE_CHECKER
10192316SN/A        if (cpu->checker && head_inst->isStore()) {
10202732Sktlim@umich.edu            cpu->checker->verify(head_inst);
10212316SN/A        }
10222733Sktlim@umich.edu#endif
10232292SN/A
10242316SN/A        assert(!thread[tid]->inSyscall);
10252292SN/A
10262316SN/A        // Mark that we're in state update mode so that the trap's
10272316SN/A        // execution doesn't generate extra squashes.
10282316SN/A        thread[tid]->inSyscall = true;
10292292SN/A
10302316SN/A        // DTB will sometimes need the machine instruction for when
10312316SN/A        // faults happen.  So we will set it here, prior to the DTB
10322316SN/A        // possibly needing it for its fault.
10332316SN/A        thread[tid]->setInst(
10342316SN/A            static_cast<TheISA::MachInst>(head_inst->staticInst->machInst));
10352292SN/A
10362316SN/A        // Execute the trap.  Although it's slightly unrealistic in
10372316SN/A        // terms of timing (as it doesn't wait for the full timing of
10382316SN/A        // the trap event to complete before updating state), it's
10392316SN/A        // needed to update the state as soon as possible.  This
10402316SN/A        // prevents external agents from changing any specific state
10412316SN/A        // that the trap need.
10422316SN/A        cpu->trap(inst_fault, tid);
10432292SN/A
10442316SN/A        // Exit state update mode to avoid accidental updating.
10452316SN/A        thread[tid]->inSyscall = false;
10462292SN/A
10472316SN/A        commitStatus[tid] = TrapPending;
10482292SN/A
10492316SN/A        // Generate trap squash event.
10502316SN/A        generateTrapEvent(tid);
10512316SN/A
10522316SN/A        return false;
10531060SN/A    }
10541060SN/A
10552301SN/A    updateComInstStats(head_inst);
10562132SN/A
10572132SN/A    if (head_inst->traceData) {
10582292SN/A        head_inst->traceData->setFetchSeq(head_inst->seqNum);
10592292SN/A        head_inst->traceData->setCPSeq(thread[tid]->numInst);
10602132SN/A        head_inst->traceData->finalize();
10612292SN/A        head_inst->traceData = NULL;
10621060SN/A    }
10631060SN/A
10642292SN/A    // Update the commit rename map
10652292SN/A    for (int i = 0; i < head_inst->numDestRegs(); i++) {
10662292SN/A        renameMap[tid]->setEntry(head_inst->destRegIdx(i),
10672292SN/A                                 head_inst->renamedDestRegIdx(i));
10681060SN/A    }
10691062SN/A
10702292SN/A    // Finally clear the head ROB entry.
10712292SN/A    rob->retireHead(tid);
10721060SN/A
10731060SN/A    // Return true to indicate that we have committed an instruction.
10741060SN/A    return true;
10751060SN/A}
10761060SN/A
10771061SN/Atemplate <class Impl>
10781060SN/Avoid
10792292SN/ADefaultCommit<Impl>::getInsts()
10801060SN/A{
10812316SN/A    // Read any renamed instructions and place them into the ROB.
10821061SN/A    int insts_to_process = min((int)renameWidth, fromRename->size);
10831061SN/A
10842292SN/A    for (int inst_num = 0; inst_num < insts_to_process; ++inst_num)
10851060SN/A    {
10862292SN/A        DynInstPtr inst = fromRename->insts[inst_num];
10872292SN/A        int tid = inst->threadNumber;
10882292SN/A
10892292SN/A        if (!inst->isSquashed() &&
10902292SN/A            commitStatus[tid] != ROBSquashing) {
10912292SN/A            changedROBNumEntries[tid] = true;
10922292SN/A
10932292SN/A            DPRINTF(Commit, "Inserting PC %#x [sn:%i] [tid:%i] into ROB.\n",
10942292SN/A                    inst->readPC(), inst->seqNum, tid);
10952292SN/A
10962292SN/A            rob->insertInst(inst);
10972292SN/A
10982292SN/A            assert(rob->getThreadEntries(tid) <= rob->getMaxEntries(tid));
10992292SN/A
11002292SN/A            youngestSeqNum[tid] = inst->seqNum;
11011061SN/A        } else {
11022292SN/A            DPRINTF(Commit, "Instruction PC %#x [sn:%i] [tid:%i] was "
11031061SN/A                    "squashed, skipping.\n",
11042292SN/A                    inst->readPC(), inst->seqNum, tid);
11051061SN/A        }
11061060SN/A    }
11071060SN/A}
11081060SN/A
11091061SN/Atemplate <class Impl>
11101060SN/Avoid
11112292SN/ADefaultCommit<Impl>::markCompletedInsts()
11121060SN/A{
11131060SN/A    // Grab completed insts out of the IEW instruction queue, and mark
11141060SN/A    // instructions completed within the ROB.
11151060SN/A    for (int inst_num = 0;
11161681SN/A         inst_num < fromIEW->size && fromIEW->insts[inst_num];
11171060SN/A         ++inst_num)
11181060SN/A    {
11192292SN/A        if (!fromIEW->insts[inst_num]->isSquashed()) {
11202316SN/A            DPRINTF(Commit, "[tid:%i]: Marking PC %#x, [sn:%lli] ready "
11212316SN/A                    "within ROB.\n",
11222292SN/A                    fromIEW->insts[inst_num]->threadNumber,
11232292SN/A                    fromIEW->insts[inst_num]->readPC(),
11242292SN/A                    fromIEW->insts[inst_num]->seqNum);
11251060SN/A
11262292SN/A            // Mark the instruction as ready to commit.
11272292SN/A            fromIEW->insts[inst_num]->setCanCommit();
11282292SN/A        }
11291060SN/A    }
11301060SN/A}
11311060SN/A
11321061SN/Atemplate <class Impl>
11332292SN/Abool
11342292SN/ADefaultCommit<Impl>::robDoneSquashing()
11351060SN/A{
11362292SN/A    list<unsigned>::iterator threads = (*activeThreads).begin();
11372292SN/A
11382292SN/A    while (threads != (*activeThreads).end()) {
11392292SN/A        unsigned tid = *threads++;
11402292SN/A
11412292SN/A        if (!rob->isDoneSquashing(tid))
11422292SN/A            return false;
11432292SN/A    }
11442292SN/A
11452292SN/A    return true;
11461060SN/A}
11472292SN/A
11482301SN/Atemplate <class Impl>
11492301SN/Avoid
11502301SN/ADefaultCommit<Impl>::updateComInstStats(DynInstPtr &inst)
11512301SN/A{
11522301SN/A    unsigned thread = inst->threadNumber;
11532301SN/A
11542301SN/A    //
11552301SN/A    //  Pick off the software prefetches
11562301SN/A    //
11572301SN/A#ifdef TARGET_ALPHA
11582301SN/A    if (inst->isDataPrefetch()) {
11592316SN/A        statComSwp[thread]++;
11602301SN/A    } else {
11612316SN/A        statComInst[thread]++;
11622301SN/A    }
11632301SN/A#else
11642316SN/A    statComInst[thread]++;
11652301SN/A#endif
11662301SN/A
11672301SN/A    //
11682301SN/A    //  Control Instructions
11692301SN/A    //
11702301SN/A    if (inst->isControl())
11712316SN/A        statComBranches[thread]++;
11722301SN/A
11732301SN/A    //
11742301SN/A    //  Memory references
11752301SN/A    //
11762301SN/A    if (inst->isMemRef()) {
11772316SN/A        statComRefs[thread]++;
11782301SN/A
11792301SN/A        if (inst->isLoad()) {
11802316SN/A            statComLoads[thread]++;
11812301SN/A        }
11822301SN/A    }
11832301SN/A
11842301SN/A    if (inst->isMemBarrier()) {
11852316SN/A        statComMembars[thread]++;
11862301SN/A    }
11872301SN/A}
11882301SN/A
11892292SN/A////////////////////////////////////////
11902292SN/A//                                    //
11912316SN/A//  SMT COMMIT POLICY MAINTAINED HERE //
11922292SN/A//                                    //
11932292SN/A////////////////////////////////////////
11942292SN/Atemplate <class Impl>
11952292SN/Aint
11962292SN/ADefaultCommit<Impl>::getCommittingThread()
11972292SN/A{
11982292SN/A    if (numThreads > 1) {
11992292SN/A        switch (commitPolicy) {
12002292SN/A
12012292SN/A          case Aggressive:
12022292SN/A            //If Policy is Aggressive, commit will call
12032292SN/A            //this function multiple times per
12042292SN/A            //cycle
12052292SN/A            return oldestReady();
12062292SN/A
12072292SN/A          case RoundRobin:
12082292SN/A            return roundRobin();
12092292SN/A
12102292SN/A          case OldestReady:
12112292SN/A            return oldestReady();
12122292SN/A
12132292SN/A          default:
12142292SN/A            return -1;
12152292SN/A        }
12162292SN/A    } else {
12172292SN/A        int tid = (*activeThreads).front();
12182292SN/A
12192292SN/A        if (commitStatus[tid] == Running ||
12202292SN/A            commitStatus[tid] == Idle ||
12212292SN/A            commitStatus[tid] == FetchTrapPending) {
12222292SN/A            return tid;
12232292SN/A        } else {
12242292SN/A            return -1;
12252292SN/A        }
12262292SN/A    }
12272292SN/A}
12282292SN/A
12292292SN/Atemplate<class Impl>
12302292SN/Aint
12312292SN/ADefaultCommit<Impl>::roundRobin()
12322292SN/A{
12332292SN/A    list<unsigned>::iterator pri_iter = priority_list.begin();
12342292SN/A    list<unsigned>::iterator end      = priority_list.end();
12352292SN/A
12362292SN/A    while (pri_iter != end) {
12372292SN/A        unsigned tid = *pri_iter;
12382292SN/A
12392292SN/A        if (commitStatus[tid] == Running ||
12402831Sksewell@umich.edu            commitStatus[tid] == Idle ||
12412831Sksewell@umich.edu            commitStatus[tid] == FetchTrapPending) {
12422292SN/A
12432292SN/A            if (rob->isHeadReady(tid)) {
12442292SN/A                priority_list.erase(pri_iter);
12452292SN/A                priority_list.push_back(tid);
12462292SN/A
12472292SN/A                return tid;
12482292SN/A            }
12492292SN/A        }
12502292SN/A
12512292SN/A        pri_iter++;
12522292SN/A    }
12532292SN/A
12542292SN/A    return -1;
12552292SN/A}
12562292SN/A
12572292SN/Atemplate<class Impl>
12582292SN/Aint
12592292SN/ADefaultCommit<Impl>::oldestReady()
12602292SN/A{
12612292SN/A    unsigned oldest = 0;
12622292SN/A    bool first = true;
12632292SN/A
12642292SN/A    list<unsigned>::iterator threads = (*activeThreads).begin();
12652292SN/A
12662292SN/A    while (threads != (*activeThreads).end()) {
12672292SN/A        unsigned tid = *threads++;
12682292SN/A
12692292SN/A        if (!rob->isEmpty(tid) &&
12702292SN/A            (commitStatus[tid] == Running ||
12712292SN/A             commitStatus[tid] == Idle ||
12722292SN/A             commitStatus[tid] == FetchTrapPending)) {
12732292SN/A
12742292SN/A            if (rob->isHeadReady(tid)) {
12752292SN/A
12762292SN/A                DynInstPtr head_inst = rob->readHeadInst(tid);
12772292SN/A
12782292SN/A                if (first) {
12792292SN/A                    oldest = tid;
12802292SN/A                    first = false;
12812292SN/A                } else if (head_inst->seqNum < oldest) {
12822292SN/A                    oldest = tid;
12832292SN/A                }
12842292SN/A            }
12852292SN/A        }
12862292SN/A    }
12872292SN/A
12882292SN/A    if (!first) {
12892292SN/A        return oldest;
12902292SN/A    } else {
12912292SN/A        return -1;
12922292SN/A    }
12932292SN/A}
1294