cpu.cc revision 3221
11689SN/A/*
22325SN/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
292756Sksewell@umich.edu *          Korey Sewell
301689SN/A */
311689SN/A
321858SN/A#include "config/full_system.hh"
332733Sktlim@umich.edu#include "config/use_checker.hh"
341858SN/A
351858SN/A#if FULL_SYSTEM
362356SN/A#include "cpu/quiesce_event.hh"
371060SN/A#include "sim/system.hh"
381060SN/A#else
391060SN/A#include "sim/process.hh"
401060SN/A#endif
411060SN/A
422325SN/A#include "cpu/activity.hh"
432683Sktlim@umich.edu#include "cpu/simple_thread.hh"
442680Sktlim@umich.edu#include "cpu/thread_context.hh"
452817Sksewell@umich.edu#include "cpu/o3/isa_specific.hh"
461717SN/A#include "cpu/o3/cpu.hh"
471060SN/A
482325SN/A#include "sim/root.hh"
492292SN/A#include "sim/stat_control.hh"
502292SN/A
512794Sktlim@umich.edu#if USE_CHECKER
522794Sktlim@umich.edu#include "cpu/checker/cpu.hh"
532794Sktlim@umich.edu#endif
542794Sktlim@umich.edu
551060SN/Ausing namespace std;
562669Sktlim@umich.eduusing namespace TheISA;
571060SN/A
582733Sktlim@umich.eduBaseO3CPU::BaseO3CPU(Params *params)
592292SN/A    : BaseCPU(params), cpu_id(0)
601060SN/A{
611060SN/A}
621060SN/A
632292SN/Avoid
642733Sktlim@umich.eduBaseO3CPU::regStats()
652292SN/A{
662292SN/A    BaseCPU::regStats();
672292SN/A}
682292SN/A
691060SN/Atemplate <class Impl>
701755SN/AFullO3CPU<Impl>::TickEvent::TickEvent(FullO3CPU<Impl> *c)
711060SN/A    : Event(&mainEventQueue, CPU_Tick_Pri), cpu(c)
721060SN/A{
731060SN/A}
741060SN/A
751060SN/Atemplate <class Impl>
761060SN/Avoid
771755SN/AFullO3CPU<Impl>::TickEvent::process()
781060SN/A{
791060SN/A    cpu->tick();
801060SN/A}
811060SN/A
821060SN/Atemplate <class Impl>
831060SN/Aconst char *
841755SN/AFullO3CPU<Impl>::TickEvent::description()
851060SN/A{
861755SN/A    return "FullO3CPU tick event";
871060SN/A}
881060SN/A
891060SN/Atemplate <class Impl>
902829Sksewell@umich.eduFullO3CPU<Impl>::ActivateThreadEvent::ActivateThreadEvent()
913221Sktlim@umich.edu    : Event(&mainEventQueue, CPU_Switch_Pri)
922829Sksewell@umich.edu{
932829Sksewell@umich.edu}
942829Sksewell@umich.edu
952829Sksewell@umich.edutemplate <class Impl>
962829Sksewell@umich.eduvoid
972829Sksewell@umich.eduFullO3CPU<Impl>::ActivateThreadEvent::init(int thread_num,
982829Sksewell@umich.edu                                           FullO3CPU<Impl> *thread_cpu)
992829Sksewell@umich.edu{
1002829Sksewell@umich.edu    tid = thread_num;
1012829Sksewell@umich.edu    cpu = thread_cpu;
1022829Sksewell@umich.edu}
1032829Sksewell@umich.edu
1042829Sksewell@umich.edutemplate <class Impl>
1052829Sksewell@umich.eduvoid
1062829Sksewell@umich.eduFullO3CPU<Impl>::ActivateThreadEvent::process()
1072829Sksewell@umich.edu{
1082829Sksewell@umich.edu    cpu->activateThread(tid);
1092829Sksewell@umich.edu}
1102829Sksewell@umich.edu
1112829Sksewell@umich.edutemplate <class Impl>
1122829Sksewell@umich.educonst char *
1132829Sksewell@umich.eduFullO3CPU<Impl>::ActivateThreadEvent::description()
1142829Sksewell@umich.edu{
1152829Sksewell@umich.edu    return "FullO3CPU \"Activate Thread\" event";
1162829Sksewell@umich.edu}
1172829Sksewell@umich.edu
1182829Sksewell@umich.edutemplate <class Impl>
1192875Sksewell@umich.eduFullO3CPU<Impl>::DeallocateContextEvent::DeallocateContextEvent()
1202875Sksewell@umich.edu    : Event(&mainEventQueue, CPU_Tick_Pri)
1212875Sksewell@umich.edu{
1222875Sksewell@umich.edu}
1232875Sksewell@umich.edu
1242875Sksewell@umich.edutemplate <class Impl>
1252875Sksewell@umich.eduvoid
1262875Sksewell@umich.eduFullO3CPU<Impl>::DeallocateContextEvent::init(int thread_num,
1272875Sksewell@umich.edu                                           FullO3CPU<Impl> *thread_cpu)
1282875Sksewell@umich.edu{
1292875Sksewell@umich.edu    tid = thread_num;
1302875Sksewell@umich.edu    cpu = thread_cpu;
1312875Sksewell@umich.edu}
1322875Sksewell@umich.edu
1332875Sksewell@umich.edutemplate <class Impl>
1342875Sksewell@umich.eduvoid
1352875Sksewell@umich.eduFullO3CPU<Impl>::DeallocateContextEvent::process()
1362875Sksewell@umich.edu{
1372875Sksewell@umich.edu    cpu->deactivateThread(tid);
1383221Sktlim@umich.edu    if (remove)
1393221Sktlim@umich.edu        cpu->removeThread(tid);
1402875Sksewell@umich.edu}
1412875Sksewell@umich.edu
1422875Sksewell@umich.edutemplate <class Impl>
1432875Sksewell@umich.educonst char *
1442875Sksewell@umich.eduFullO3CPU<Impl>::DeallocateContextEvent::description()
1452875Sksewell@umich.edu{
1462875Sksewell@umich.edu    return "FullO3CPU \"Deallocate Context\" event";
1472875Sksewell@umich.edu}
1482875Sksewell@umich.edu
1492875Sksewell@umich.edutemplate <class Impl>
1502292SN/AFullO3CPU<Impl>::FullO3CPU(Params *params)
1512733Sktlim@umich.edu    : BaseO3CPU(params),
1521060SN/A      tickEvent(this),
1532292SN/A      removeInstsThisCycle(false),
1541060SN/A      fetch(params),
1551060SN/A      decode(params),
1561060SN/A      rename(params),
1571060SN/A      iew(params),
1581060SN/A      commit(params),
1591060SN/A
1602292SN/A      regFile(params->numPhysIntRegs, params->numPhysFloatRegs),
1611060SN/A
1622831Sksewell@umich.edu      freeList(params->numberOfThreads,
1632292SN/A               TheISA::NumIntRegs, params->numPhysIntRegs,
1642292SN/A               TheISA::NumFloatRegs, params->numPhysFloatRegs),
1651060SN/A
1662292SN/A      rob(params->numROBEntries, params->squashWidth,
1672292SN/A          params->smtROBPolicy, params->smtROBThreshold,
1682292SN/A          params->numberOfThreads),
1691060SN/A
1702831Sksewell@umich.edu      scoreboard(params->numberOfThreads,
1712292SN/A                 TheISA::NumIntRegs, params->numPhysIntRegs,
1722292SN/A                 TheISA::NumFloatRegs, params->numPhysFloatRegs,
1732292SN/A                 TheISA::NumMiscRegs * number_of_threads,
1742292SN/A                 TheISA::ZeroReg),
1751060SN/A
1762873Sktlim@umich.edu      timeBuffer(params->backComSize, params->forwardComSize),
1772873Sktlim@umich.edu      fetchQueue(params->backComSize, params->forwardComSize),
1782873Sktlim@umich.edu      decodeQueue(params->backComSize, params->forwardComSize),
1792873Sktlim@umich.edu      renameQueue(params->backComSize, params->forwardComSize),
1802873Sktlim@umich.edu      iewQueue(params->backComSize, params->forwardComSize),
1812873Sktlim@umich.edu      activityRec(NumStages,
1822873Sktlim@umich.edu                  params->backComSize + params->forwardComSize,
1832873Sktlim@umich.edu                  params->activity),
1841060SN/A
1851060SN/A      globalSeqNum(1),
1861858SN/A#if FULL_SYSTEM
1872292SN/A      system(params->system),
1881060SN/A      physmem(system->physmem),
1891060SN/A#endif // FULL_SYSTEM
1902292SN/A      mem(params->mem),
1912843Sktlim@umich.edu      drainCount(0),
1922316SN/A      deferRegistration(params->deferRegistration),
1932316SN/A      numThreads(number_of_threads)
1941060SN/A{
1953221Sktlim@umich.edu    if (!deferRegistration) {
1963221Sktlim@umich.edu        _status = Running;
1973221Sktlim@umich.edu    } else {
1983221Sktlim@umich.edu        _status = Idle;
1993221Sktlim@umich.edu    }
2001681SN/A
2012733Sktlim@umich.edu    checker = NULL;
2022733Sktlim@umich.edu
2032794Sktlim@umich.edu    if (params->checker) {
2042733Sktlim@umich.edu#if USE_CHECKER
2052316SN/A        BaseCPU *temp_checker = params->checker;
2062316SN/A        checker = dynamic_cast<Checker<DynInstPtr> *>(temp_checker);
2072316SN/A        checker->setMemory(mem);
2082316SN/A#if FULL_SYSTEM
2092316SN/A        checker->setSystem(params->system);
2102316SN/A#endif
2112794Sktlim@umich.edu#else
2122794Sktlim@umich.edu        panic("Checker enabled but not compiled in!");
2132794Sktlim@umich.edu#endif // USE_CHECKER
2142316SN/A    }
2152316SN/A
2161858SN/A#if !FULL_SYSTEM
2172292SN/A    thread.resize(number_of_threads);
2182292SN/A    tids.resize(number_of_threads);
2191681SN/A#endif
2201681SN/A
2212325SN/A    // The stages also need their CPU pointer setup.  However this
2222325SN/A    // must be done at the upper level CPU because they have pointers
2232325SN/A    // to the upper level CPU, and not this FullO3CPU.
2241060SN/A
2252292SN/A    // Set up Pointers to the activeThreads list for each stage
2262292SN/A    fetch.setActiveThreads(&activeThreads);
2272292SN/A    decode.setActiveThreads(&activeThreads);
2282292SN/A    rename.setActiveThreads(&activeThreads);
2292292SN/A    iew.setActiveThreads(&activeThreads);
2302292SN/A    commit.setActiveThreads(&activeThreads);
2311060SN/A
2321060SN/A    // Give each of the stages the time buffer they will use.
2331060SN/A    fetch.setTimeBuffer(&timeBuffer);
2341060SN/A    decode.setTimeBuffer(&timeBuffer);
2351060SN/A    rename.setTimeBuffer(&timeBuffer);
2361060SN/A    iew.setTimeBuffer(&timeBuffer);
2371060SN/A    commit.setTimeBuffer(&timeBuffer);
2381060SN/A
2391060SN/A    // Also setup each of the stages' queues.
2401060SN/A    fetch.setFetchQueue(&fetchQueue);
2411060SN/A    decode.setFetchQueue(&fetchQueue);
2422292SN/A    commit.setFetchQueue(&fetchQueue);
2431060SN/A    decode.setDecodeQueue(&decodeQueue);
2441060SN/A    rename.setDecodeQueue(&decodeQueue);
2451060SN/A    rename.setRenameQueue(&renameQueue);
2461060SN/A    iew.setRenameQueue(&renameQueue);
2471060SN/A    iew.setIEWQueue(&iewQueue);
2481060SN/A    commit.setIEWQueue(&iewQueue);
2491060SN/A    commit.setRenameQueue(&renameQueue);
2501060SN/A
2512292SN/A    commit.setIEWStage(&iew);
2522292SN/A    rename.setIEWStage(&iew);
2532292SN/A    rename.setCommitStage(&commit);
2542292SN/A
2552292SN/A#if !FULL_SYSTEM
2562307SN/A    int active_threads = params->workload.size();
2572831Sksewell@umich.edu
2582831Sksewell@umich.edu    if (active_threads > Impl::MaxThreads) {
2592831Sksewell@umich.edu        panic("Workload Size too large. Increase the 'MaxThreads'"
2602831Sksewell@umich.edu              "constant in your O3CPU impl. file (e.g. o3/alpha/impl.hh) or "
2612831Sksewell@umich.edu              "edit your workload size.");
2622831Sksewell@umich.edu    }
2632292SN/A#else
2642307SN/A    int active_threads = 1;
2652292SN/A#endif
2662292SN/A
2672316SN/A    //Make Sure That this a Valid Architeture
2682292SN/A    assert(params->numPhysIntRegs   >= numThreads * TheISA::NumIntRegs);
2692292SN/A    assert(params->numPhysFloatRegs >= numThreads * TheISA::NumFloatRegs);
2702292SN/A
2712292SN/A    rename.setScoreboard(&scoreboard);
2722292SN/A    iew.setScoreboard(&scoreboard);
2732292SN/A
2741060SN/A    // Setup the rename map for whichever stages need it.
2752292SN/A    PhysRegIndex lreg_idx = 0;
2762292SN/A    PhysRegIndex freg_idx = params->numPhysIntRegs; //Index to 1 after int regs
2771060SN/A
2782292SN/A    for (int tid=0; tid < numThreads; tid++) {
2792307SN/A        bool bindRegs = (tid <= active_threads - 1);
2802292SN/A
2812292SN/A        commitRenameMap[tid].init(TheISA::NumIntRegs,
2822292SN/A                                  params->numPhysIntRegs,
2832325SN/A                                  lreg_idx,            //Index for Logical. Regs
2842292SN/A
2852292SN/A                                  TheISA::NumFloatRegs,
2862292SN/A                                  params->numPhysFloatRegs,
2872325SN/A                                  freg_idx,            //Index for Float Regs
2882292SN/A
2892292SN/A                                  TheISA::NumMiscRegs,
2902292SN/A
2912292SN/A                                  TheISA::ZeroReg,
2922292SN/A                                  TheISA::ZeroReg,
2932292SN/A
2942292SN/A                                  tid,
2952292SN/A                                  false);
2962292SN/A
2972292SN/A        renameMap[tid].init(TheISA::NumIntRegs,
2982292SN/A                            params->numPhysIntRegs,
2992325SN/A                            lreg_idx,                  //Index for Logical. Regs
3002292SN/A
3012292SN/A                            TheISA::NumFloatRegs,
3022292SN/A                            params->numPhysFloatRegs,
3032325SN/A                            freg_idx,                  //Index for Float Regs
3042292SN/A
3052292SN/A                            TheISA::NumMiscRegs,
3062292SN/A
3072292SN/A                            TheISA::ZeroReg,
3082292SN/A                            TheISA::ZeroReg,
3092292SN/A
3102292SN/A                            tid,
3112292SN/A                            bindRegs);
3123221Sktlim@umich.edu
3133221Sktlim@umich.edu        activateThreadEvent[tid].init(tid, this);
3143221Sktlim@umich.edu        deallocateContextEvent[tid].init(tid, this);
3152292SN/A    }
3162292SN/A
3172292SN/A    rename.setRenameMap(renameMap);
3182292SN/A    commit.setRenameMap(commitRenameMap);
3192292SN/A
3202292SN/A    // Give renameMap & rename stage access to the freeList;
3212292SN/A    for (int i=0; i < numThreads; i++) {
3222292SN/A        renameMap[i].setFreeList(&freeList);
3232292SN/A    }
3241060SN/A    rename.setFreeList(&freeList);
3252292SN/A
3261060SN/A    // Setup the ROB for whichever stages need it.
3271060SN/A    commit.setROB(&rob);
3282292SN/A
3292292SN/A    lastRunningCycle = curTick;
3302292SN/A
3312829Sksewell@umich.edu    lastActivatedCycle = -1;
3322829Sksewell@umich.edu
3333093Sksewell@umich.edu    // Give renameMap & rename stage access to the freeList;
3343093Sksewell@umich.edu    //for (int i=0; i < numThreads; i++) {
3353093Sksewell@umich.edu        //globalSeqNum[i] = 1;
3363093Sksewell@umich.edu        //}
3373093Sksewell@umich.edu
3382292SN/A    contextSwitch = false;
3391060SN/A}
3401060SN/A
3411060SN/Atemplate <class Impl>
3421755SN/AFullO3CPU<Impl>::~FullO3CPU()
3431060SN/A{
3441060SN/A}
3451060SN/A
3461060SN/Atemplate <class Impl>
3471060SN/Avoid
3481755SN/AFullO3CPU<Impl>::fullCPURegStats()
3491062SN/A{
3502733Sktlim@umich.edu    BaseO3CPU::regStats();
3512292SN/A
3522733Sktlim@umich.edu    // Register any of the O3CPU's stats here.
3532292SN/A    timesIdled
3542292SN/A        .name(name() + ".timesIdled")
3552292SN/A        .desc("Number of times that the entire CPU went into an idle state and"
3562292SN/A              " unscheduled itself")
3572292SN/A        .prereq(timesIdled);
3582292SN/A
3592292SN/A    idleCycles
3602292SN/A        .name(name() + ".idleCycles")
3612292SN/A        .desc("Total number of cycles that the CPU has spent unscheduled due "
3622292SN/A              "to idling")
3632292SN/A        .prereq(idleCycles);
3642292SN/A
3652292SN/A    // Number of Instructions simulated
3662292SN/A    // --------------------------------
3672292SN/A    // Should probably be in Base CPU but need templated
3682292SN/A    // MaxThreads so put in here instead
3692292SN/A    committedInsts
3702292SN/A        .init(numThreads)
3712292SN/A        .name(name() + ".committedInsts")
3722292SN/A        .desc("Number of Instructions Simulated");
3732292SN/A
3742292SN/A    totalCommittedInsts
3752292SN/A        .name(name() + ".committedInsts_total")
3762292SN/A        .desc("Number of Instructions Simulated");
3772292SN/A
3782292SN/A    cpi
3792292SN/A        .name(name() + ".cpi")
3802292SN/A        .desc("CPI: Cycles Per Instruction")
3812292SN/A        .precision(6);
3822292SN/A    cpi = simTicks / committedInsts;
3832292SN/A
3842292SN/A    totalCpi
3852292SN/A        .name(name() + ".cpi_total")
3862292SN/A        .desc("CPI: Total CPI of All Threads")
3872292SN/A        .precision(6);
3882292SN/A    totalCpi = simTicks / totalCommittedInsts;
3892292SN/A
3902292SN/A    ipc
3912292SN/A        .name(name() + ".ipc")
3922292SN/A        .desc("IPC: Instructions Per Cycle")
3932292SN/A        .precision(6);
3942292SN/A    ipc =  committedInsts / simTicks;
3952292SN/A
3962292SN/A    totalIpc
3972292SN/A        .name(name() + ".ipc_total")
3982292SN/A        .desc("IPC: Total IPC of All Threads")
3992292SN/A        .precision(6);
4002292SN/A    totalIpc =  totalCommittedInsts / simTicks;
4012292SN/A
4021062SN/A}
4031062SN/A
4041062SN/Atemplate <class Impl>
4052871Sktlim@umich.eduPort *
4062871Sktlim@umich.eduFullO3CPU<Impl>::getPort(const std::string &if_name, int idx)
4072871Sktlim@umich.edu{
4082871Sktlim@umich.edu    if (if_name == "dcache_port")
4092871Sktlim@umich.edu        return iew.getDcachePort();
4102871Sktlim@umich.edu    else if (if_name == "icache_port")
4112871Sktlim@umich.edu        return fetch.getIcachePort();
4122871Sktlim@umich.edu    else
4132871Sktlim@umich.edu        panic("No Such Port\n");
4142871Sktlim@umich.edu}
4152871Sktlim@umich.edu
4162871Sktlim@umich.edutemplate <class Impl>
4171062SN/Avoid
4181755SN/AFullO3CPU<Impl>::tick()
4191060SN/A{
4202733Sktlim@umich.edu    DPRINTF(O3CPU, "\n\nFullO3CPU: Ticking main, FullO3CPU.\n");
4211060SN/A
4222292SN/A    ++numCycles;
4232292SN/A
4242325SN/A//    activity = false;
4252292SN/A
4262292SN/A    //Tick each of the stages
4271060SN/A    fetch.tick();
4281060SN/A
4291060SN/A    decode.tick();
4301060SN/A
4311060SN/A    rename.tick();
4321060SN/A
4331060SN/A    iew.tick();
4341060SN/A
4351060SN/A    commit.tick();
4361060SN/A
4372292SN/A#if !FULL_SYSTEM
4382292SN/A    doContextSwitch();
4392292SN/A#endif
4402292SN/A
4412292SN/A    // Now advance the time buffers
4421060SN/A    timeBuffer.advance();
4431060SN/A
4441060SN/A    fetchQueue.advance();
4451060SN/A    decodeQueue.advance();
4461060SN/A    renameQueue.advance();
4471060SN/A    iewQueue.advance();
4481060SN/A
4492325SN/A    activityRec.advance();
4502292SN/A
4512292SN/A    if (removeInstsThisCycle) {
4522292SN/A        cleanUpRemovedInsts();
4532292SN/A    }
4542292SN/A
4552325SN/A    if (!tickEvent.scheduled()) {
4562867Sktlim@umich.edu        if (_status == SwitchedOut ||
4572905Sktlim@umich.edu            getState() == SimObject::Drained) {
4582325SN/A            // increment stat
4592325SN/A            lastRunningCycle = curTick;
4603221Sktlim@umich.edu        } else if (!activityRec.active() || _status == Idle) {
4612325SN/A            lastRunningCycle = curTick;
4622325SN/A            timesIdled++;
4632325SN/A        } else {
4642325SN/A            tickEvent.schedule(curTick + cycles(1));
4652325SN/A        }
4662292SN/A    }
4672292SN/A
4682292SN/A#if !FULL_SYSTEM
4692292SN/A    updateThreadPriority();
4702292SN/A#endif
4712292SN/A
4721060SN/A}
4731060SN/A
4741060SN/Atemplate <class Impl>
4751060SN/Avoid
4761755SN/AFullO3CPU<Impl>::init()
4771060SN/A{
4782307SN/A    if (!deferRegistration) {
4792680Sktlim@umich.edu        registerThreadContexts();
4802292SN/A    }
4811060SN/A
4822292SN/A    // Set inSyscall so that the CPU doesn't squash when initially
4832292SN/A    // setting up registers.
4842292SN/A    for (int i = 0; i < number_of_threads; ++i)
4852292SN/A        thread[i]->inSyscall = true;
4862292SN/A
4872292SN/A    for (int tid=0; tid < number_of_threads; tid++) {
4881858SN/A#if FULL_SYSTEM
4892680Sktlim@umich.edu        ThreadContext *src_tc = threadContexts[tid];
4901681SN/A#else
4912680Sktlim@umich.edu        ThreadContext *src_tc = thread[tid]->getTC();
4921681SN/A#endif
4932292SN/A        // Threads start in the Suspended State
4942680Sktlim@umich.edu        if (src_tc->status() != ThreadContext::Suspended) {
4952292SN/A            continue;
4961060SN/A        }
4971060SN/A
4982292SN/A#if FULL_SYSTEM
4992680Sktlim@umich.edu        TheISA::initCPU(src_tc, src_tc->readCpuId());
5002292SN/A#endif
5012292SN/A    }
5022292SN/A
5032292SN/A    // Clear inSyscall.
5042292SN/A    for (int i = 0; i < number_of_threads; ++i)
5052292SN/A        thread[i]->inSyscall = false;
5062292SN/A
5072316SN/A    // Initialize stages.
5082292SN/A    fetch.initStage();
5092292SN/A    iew.initStage();
5102292SN/A    rename.initStage();
5112292SN/A    commit.initStage();
5122292SN/A
5132292SN/A    commit.setThreads(thread);
5142292SN/A}
5152292SN/A
5162292SN/Atemplate <class Impl>
5172292SN/Avoid
5182875Sksewell@umich.eduFullO3CPU<Impl>::activateThread(unsigned tid)
5192875Sksewell@umich.edu{
5202875Sksewell@umich.edu    list<unsigned>::iterator isActive = find(
5212875Sksewell@umich.edu        activeThreads.begin(), activeThreads.end(), tid);
5222875Sksewell@umich.edu
5232875Sksewell@umich.edu    if (isActive == activeThreads.end()) {
5242875Sksewell@umich.edu        DPRINTF(O3CPU, "[tid:%i]: Adding to active threads list\n",
5252875Sksewell@umich.edu                tid);
5262875Sksewell@umich.edu
5272875Sksewell@umich.edu        activeThreads.push_back(tid);
5282875Sksewell@umich.edu    }
5292875Sksewell@umich.edu}
5302875Sksewell@umich.edu
5312875Sksewell@umich.edutemplate <class Impl>
5322875Sksewell@umich.eduvoid
5332875Sksewell@umich.eduFullO3CPU<Impl>::deactivateThread(unsigned tid)
5342875Sksewell@umich.edu{
5352875Sksewell@umich.edu    //Remove From Active List, if Active
5362875Sksewell@umich.edu    list<unsigned>::iterator thread_it =
5372875Sksewell@umich.edu        find(activeThreads.begin(), activeThreads.end(), tid);
5382875Sksewell@umich.edu
5392875Sksewell@umich.edu    if (thread_it != activeThreads.end()) {
5402875Sksewell@umich.edu        DPRINTF(O3CPU,"[tid:%i]: Removing from active threads list\n",
5412875Sksewell@umich.edu                tid);
5422875Sksewell@umich.edu        activeThreads.erase(thread_it);
5432875Sksewell@umich.edu    }
5442875Sksewell@umich.edu}
5452875Sksewell@umich.edu
5462875Sksewell@umich.edutemplate <class Impl>
5472875Sksewell@umich.eduvoid
5482875Sksewell@umich.eduFullO3CPU<Impl>::activateContext(int tid, int delay)
5492875Sksewell@umich.edu{
5502875Sksewell@umich.edu    // Needs to set each stage to running as well.
5512875Sksewell@umich.edu    if (delay){
5522875Sksewell@umich.edu        DPRINTF(O3CPU, "[tid:%i]: Scheduling thread context to activate "
5532875Sksewell@umich.edu                "on cycle %d\n", tid, curTick + cycles(delay));
5542875Sksewell@umich.edu        scheduleActivateThreadEvent(tid, delay);
5552875Sksewell@umich.edu    } else {
5562875Sksewell@umich.edu        activateThread(tid);
5572875Sksewell@umich.edu    }
5582875Sksewell@umich.edu
5593221Sktlim@umich.edu    if (lastActivatedCycle < curTick) {
5602875Sksewell@umich.edu        scheduleTickEvent(delay);
5612875Sksewell@umich.edu
5622875Sksewell@umich.edu        // Be sure to signal that there's some activity so the CPU doesn't
5632875Sksewell@umich.edu        // deschedule itself.
5642875Sksewell@umich.edu        activityRec.activity();
5652875Sksewell@umich.edu        fetch.wakeFromQuiesce();
5662875Sksewell@umich.edu
5672875Sksewell@umich.edu        lastActivatedCycle = curTick;
5682875Sksewell@umich.edu
5692875Sksewell@umich.edu        _status = Running;
5702875Sksewell@umich.edu    }
5712875Sksewell@umich.edu}
5722875Sksewell@umich.edu
5732875Sksewell@umich.edutemplate <class Impl>
5743221Sktlim@umich.edubool
5753221Sktlim@umich.eduFullO3CPU<Impl>::deallocateContext(int tid, bool remove, int delay)
5762875Sksewell@umich.edu{
5772875Sksewell@umich.edu    // Schedule removal of thread data from CPU
5782875Sksewell@umich.edu    if (delay){
5792875Sksewell@umich.edu        DPRINTF(O3CPU, "[tid:%i]: Scheduling thread context to deallocate "
5802875Sksewell@umich.edu                "on cycle %d\n", tid, curTick + cycles(delay));
5813221Sktlim@umich.edu        scheduleDeallocateContextEvent(tid, remove, delay);
5823221Sktlim@umich.edu        return false;
5832875Sksewell@umich.edu    } else {
5842875Sksewell@umich.edu        deactivateThread(tid);
5853221Sktlim@umich.edu        if (remove)
5863221Sktlim@umich.edu            removeThread(tid);
5873221Sktlim@umich.edu        return true;
5882875Sksewell@umich.edu    }
5892875Sksewell@umich.edu}
5902875Sksewell@umich.edu
5912875Sksewell@umich.edutemplate <class Impl>
5922875Sksewell@umich.eduvoid
5932875Sksewell@umich.eduFullO3CPU<Impl>::suspendContext(int tid)
5942875Sksewell@umich.edu{
5952875Sksewell@umich.edu    DPRINTF(O3CPU,"[tid: %i]: Suspending Thread Context.\n", tid);
5963221Sktlim@umich.edu    bool deallocated = deallocateContext(tid, false, 1);
5973221Sktlim@umich.edu    // If this was the last thread then unschedule the tick event.
5983221Sktlim@umich.edu    if ((activeThreads.size() == 1 && !deallocated) || activeThreads.size() == 0)
5992910Sksewell@umich.edu        unscheduleTickEvent();
6002875Sksewell@umich.edu    _status = Idle;
6012875Sksewell@umich.edu}
6022875Sksewell@umich.edu
6032875Sksewell@umich.edutemplate <class Impl>
6042875Sksewell@umich.eduvoid
6052875Sksewell@umich.eduFullO3CPU<Impl>::haltContext(int tid)
6062875Sksewell@umich.edu{
6072910Sksewell@umich.edu    //For now, this is the same as deallocate
6082910Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i]: Halt Context called. Deallocating", tid);
6093221Sktlim@umich.edu    deallocateContext(tid, true, 1);
6102875Sksewell@umich.edu}
6112875Sksewell@umich.edu
6122875Sksewell@umich.edutemplate <class Impl>
6132875Sksewell@umich.eduvoid
6142292SN/AFullO3CPU<Impl>::insertThread(unsigned tid)
6152292SN/A{
6162847Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i] Initializing thread into CPU");
6172292SN/A    // Will change now that the PC and thread state is internal to the CPU
6182683Sktlim@umich.edu    // and not in the ThreadContext.
6192292SN/A#if FULL_SYSTEM
6202680Sktlim@umich.edu    ThreadContext *src_tc = system->threadContexts[tid];
6212292SN/A#else
6222847Sksewell@umich.edu    ThreadContext *src_tc = tcBase(tid);
6232292SN/A#endif
6242292SN/A
6252292SN/A    //Bind Int Regs to Rename Map
6262292SN/A    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
6272292SN/A        PhysRegIndex phys_reg = freeList.getIntReg();
6282292SN/A
6292292SN/A        renameMap[tid].setEntry(ireg,phys_reg);
6302292SN/A        scoreboard.setReg(phys_reg);
6312292SN/A    }
6322292SN/A
6332292SN/A    //Bind Float Regs to Rename Map
6342292SN/A    for (int freg = 0; freg < TheISA::NumFloatRegs; freg++) {
6352292SN/A        PhysRegIndex phys_reg = freeList.getFloatReg();
6362292SN/A
6372292SN/A        renameMap[tid].setEntry(freg,phys_reg);
6382292SN/A        scoreboard.setReg(phys_reg);
6392292SN/A    }
6402292SN/A
6412292SN/A    //Copy Thread Data Into RegFile
6422847Sksewell@umich.edu    //this->copyFromTC(tid);
6432292SN/A
6442847Sksewell@umich.edu    //Set PC/NPC/NNPC
6452847Sksewell@umich.edu    setPC(src_tc->readPC(), tid);
6462847Sksewell@umich.edu    setNextPC(src_tc->readNextPC(), tid);
6473093Sksewell@umich.edu#if ISA_HAS_DELAY_SLOT
6482847Sksewell@umich.edu    setNextNPC(src_tc->readNextNPC(), tid);
6492847Sksewell@umich.edu#endif
6502292SN/A
6512680Sktlim@umich.edu    src_tc->setStatus(ThreadContext::Active);
6522292SN/A
6532292SN/A    activateContext(tid,1);
6542292SN/A
6552292SN/A    //Reset ROB/IQ/LSQ Entries
6562292SN/A    commit.rob->resetEntries();
6572292SN/A    iew.resetEntries();
6582292SN/A}
6592292SN/A
6602292SN/Atemplate <class Impl>
6612292SN/Avoid
6622292SN/AFullO3CPU<Impl>::removeThread(unsigned tid)
6632292SN/A{
6642877Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i] Removing thread context from CPU.\n", tid);
6652847Sksewell@umich.edu
6662847Sksewell@umich.edu    // Copy Thread Data From RegFile
6672847Sksewell@umich.edu    // If thread is suspended, it might be re-allocated
6682847Sksewell@umich.edu    //this->copyToTC(tid);
6692847Sksewell@umich.edu
6702847Sksewell@umich.edu    // Unbind Int Regs from Rename Map
6712292SN/A    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
6722292SN/A        PhysRegIndex phys_reg = renameMap[tid].lookup(ireg);
6732292SN/A
6742292SN/A        scoreboard.unsetReg(phys_reg);
6752292SN/A        freeList.addReg(phys_reg);
6762292SN/A    }
6772292SN/A
6782847Sksewell@umich.edu    // Unbind Float Regs from Rename Map
6792292SN/A    for (int freg = 0; freg < TheISA::NumFloatRegs; freg++) {
6802292SN/A        PhysRegIndex phys_reg = renameMap[tid].lookup(freg);
6812292SN/A
6822292SN/A        scoreboard.unsetReg(phys_reg);
6832292SN/A        freeList.addReg(phys_reg);
6842292SN/A    }
6852292SN/A
6862847Sksewell@umich.edu    // Squash Throughout Pipeline
6872935Sksewell@umich.edu    InstSeqNum squash_seq_num = commit.rob->readHeadInst(tid)->seqNum;
6882935Sksewell@umich.edu    fetch.squash(0, squash_seq_num, true, tid);
6892292SN/A    decode.squash(tid);
6902935Sksewell@umich.edu    rename.squash(squash_seq_num, tid);
6912875Sksewell@umich.edu    iew.squash(tid);
6922935Sksewell@umich.edu    commit.rob->squash(squash_seq_num, tid);
6932292SN/A
6942292SN/A    assert(iew.ldstQueue.getCount(tid) == 0);
6952292SN/A
6962847Sksewell@umich.edu    // Reset ROB/IQ/LSQ Entries
6972292SN/A    if (activeThreads.size() >= 1) {
6982292SN/A        commit.rob->resetEntries();
6992292SN/A        iew.resetEntries();
7002292SN/A    }
7012292SN/A}
7022292SN/A
7032292SN/A
7042292SN/Atemplate <class Impl>
7052292SN/Avoid
7062292SN/AFullO3CPU<Impl>::activateWhenReady(int tid)
7072292SN/A{
7082733Sktlim@umich.edu    DPRINTF(O3CPU,"[tid:%i]: Checking if resources are available for incoming"
7092292SN/A            "(e.g. PhysRegs/ROB/IQ/LSQ) \n",
7102292SN/A            tid);
7112292SN/A
7122292SN/A    bool ready = true;
7132292SN/A
7142292SN/A    if (freeList.numFreeIntRegs() >= TheISA::NumIntRegs) {
7152733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
7162292SN/A                "Phys. Int. Regs.\n",
7172292SN/A                tid);
7182292SN/A        ready = false;
7192292SN/A    } else if (freeList.numFreeFloatRegs() >= TheISA::NumFloatRegs) {
7202733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
7212292SN/A                "Phys. Float. Regs.\n",
7222292SN/A                tid);
7232292SN/A        ready = false;
7242292SN/A    } else if (commit.rob->numFreeEntries() >=
7252292SN/A               commit.rob->entryAmount(activeThreads.size() + 1)) {
7262733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
7272292SN/A                "ROB entries.\n",
7282292SN/A                tid);
7292292SN/A        ready = false;
7302292SN/A    } else if (iew.instQueue.numFreeEntries() >=
7312292SN/A               iew.instQueue.entryAmount(activeThreads.size() + 1)) {
7322733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
7332292SN/A                "IQ entries.\n",
7342292SN/A                tid);
7352292SN/A        ready = false;
7362292SN/A    } else if (iew.ldstQueue.numFreeEntries() >=
7372292SN/A               iew.ldstQueue.entryAmount(activeThreads.size() + 1)) {
7382733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
7392292SN/A                "LSQ entries.\n",
7402292SN/A                tid);
7412292SN/A        ready = false;
7422292SN/A    }
7432292SN/A
7442292SN/A    if (ready) {
7452292SN/A        insertThread(tid);
7462292SN/A
7472292SN/A        contextSwitch = false;
7482292SN/A
7492292SN/A        cpuWaitList.remove(tid);
7502292SN/A    } else {
7512292SN/A        suspendContext(tid);
7522292SN/A
7532292SN/A        //blocks fetch
7542292SN/A        contextSwitch = true;
7552292SN/A
7562875Sksewell@umich.edu        //@todo: dont always add to waitlist
7572292SN/A        //do waitlist
7582292SN/A        cpuWaitList.push_back(tid);
7591060SN/A    }
7601060SN/A}
7611060SN/A
7621060SN/Atemplate <class Impl>
7632852Sktlim@umich.eduvoid
7642864Sktlim@umich.eduFullO3CPU<Impl>::serialize(std::ostream &os)
7652864Sktlim@umich.edu{
7662918Sktlim@umich.edu    SimObject::State so_state = SimObject::getState();
7672918Sktlim@umich.edu    SERIALIZE_ENUM(so_state);
7682864Sktlim@umich.edu    BaseCPU::serialize(os);
7692864Sktlim@umich.edu    nameOut(os, csprintf("%s.tickEvent", name()));
7702864Sktlim@umich.edu    tickEvent.serialize(os);
7712864Sktlim@umich.edu
7722864Sktlim@umich.edu    // Use SimpleThread's ability to checkpoint to make it easier to
7732864Sktlim@umich.edu    // write out the registers.  Also make this static so it doesn't
7742864Sktlim@umich.edu    // get instantiated multiple times (causes a panic in statistics).
7752864Sktlim@umich.edu    static SimpleThread temp;
7762864Sktlim@umich.edu
7772864Sktlim@umich.edu    for (int i = 0; i < thread.size(); i++) {
7782864Sktlim@umich.edu        nameOut(os, csprintf("%s.xc.%i", name(), i));
7792864Sktlim@umich.edu        temp.copyTC(thread[i]->getTC());
7802864Sktlim@umich.edu        temp.serialize(os);
7812864Sktlim@umich.edu    }
7822864Sktlim@umich.edu}
7832864Sktlim@umich.edu
7842864Sktlim@umich.edutemplate <class Impl>
7852864Sktlim@umich.eduvoid
7862864Sktlim@umich.eduFullO3CPU<Impl>::unserialize(Checkpoint *cp, const std::string &section)
7872864Sktlim@umich.edu{
7882918Sktlim@umich.edu    SimObject::State so_state;
7892918Sktlim@umich.edu    UNSERIALIZE_ENUM(so_state);
7902864Sktlim@umich.edu    BaseCPU::unserialize(cp, section);
7912864Sktlim@umich.edu    tickEvent.unserialize(cp, csprintf("%s.tickEvent", section));
7922864Sktlim@umich.edu
7932864Sktlim@umich.edu    // Use SimpleThread's ability to checkpoint to make it easier to
7942864Sktlim@umich.edu    // read in the registers.  Also make this static so it doesn't
7952864Sktlim@umich.edu    // get instantiated multiple times (causes a panic in statistics).
7962864Sktlim@umich.edu    static SimpleThread temp;
7972864Sktlim@umich.edu
7982864Sktlim@umich.edu    for (int i = 0; i < thread.size(); i++) {
7992864Sktlim@umich.edu        temp.copyTC(thread[i]->getTC());
8002864Sktlim@umich.edu        temp.unserialize(cp, csprintf("%s.xc.%i", section, i));
8012864Sktlim@umich.edu        thread[i]->getTC()->copyArchRegs(temp.getTC());
8022864Sktlim@umich.edu    }
8032864Sktlim@umich.edu}
8042864Sktlim@umich.edu
8052864Sktlim@umich.edutemplate <class Impl>
8062905Sktlim@umich.eduunsigned int
8072843Sktlim@umich.eduFullO3CPU<Impl>::drain(Event *drain_event)
8081060SN/A{
8093125Sktlim@umich.edu    DPRINTF(O3CPU, "Switching out\n");
8102843Sktlim@umich.edu    drainCount = 0;
8112843Sktlim@umich.edu    fetch.drain();
8122843Sktlim@umich.edu    decode.drain();
8132843Sktlim@umich.edu    rename.drain();
8142843Sktlim@umich.edu    iew.drain();
8152843Sktlim@umich.edu    commit.drain();
8162325SN/A
8172325SN/A    // Wake the CPU and record activity so everything can drain out if
8182863Sktlim@umich.edu    // the CPU was not able to immediately drain.
8192905Sktlim@umich.edu    if (getState() != SimObject::Drained) {
8202864Sktlim@umich.edu        // A bit of a hack...set the drainEvent after all the drain()
8212864Sktlim@umich.edu        // calls have been made, that way if all of the stages drain
8222864Sktlim@umich.edu        // immediately, the signalDrained() function knows not to call
8232864Sktlim@umich.edu        // process on the drain event.
8242864Sktlim@umich.edu        drainEvent = drain_event;
8252843Sktlim@umich.edu
8262863Sktlim@umich.edu        wakeCPU();
8272863Sktlim@umich.edu        activityRec.activity();
8282852Sktlim@umich.edu
8292905Sktlim@umich.edu        return 1;
8302863Sktlim@umich.edu    } else {
8312905Sktlim@umich.edu        return 0;
8322863Sktlim@umich.edu    }
8332316SN/A}
8342310SN/A
8352316SN/Atemplate <class Impl>
8362316SN/Avoid
8372843Sktlim@umich.eduFullO3CPU<Impl>::resume()
8382316SN/A{
8392905Sktlim@umich.edu    assert(system->getMemoryMode() == System::Timing);
8402843Sktlim@umich.edu    fetch.resume();
8412843Sktlim@umich.edu    decode.resume();
8422843Sktlim@umich.edu    rename.resume();
8432843Sktlim@umich.edu    iew.resume();
8442843Sktlim@umich.edu    commit.resume();
8452316SN/A
8462905Sktlim@umich.edu    changeState(SimObject::Running);
8472905Sktlim@umich.edu
8482864Sktlim@umich.edu    if (_status == SwitchedOut || _status == Idle)
8492864Sktlim@umich.edu        return;
8502864Sktlim@umich.edu
8512843Sktlim@umich.edu    if (!tickEvent.scheduled())
8522843Sktlim@umich.edu        tickEvent.schedule(curTick);
8532843Sktlim@umich.edu    _status = Running;
8542843Sktlim@umich.edu}
8552316SN/A
8562843Sktlim@umich.edutemplate <class Impl>
8572843Sktlim@umich.eduvoid
8582843Sktlim@umich.eduFullO3CPU<Impl>::signalDrained()
8592843Sktlim@umich.edu{
8602843Sktlim@umich.edu    if (++drainCount == NumStages) {
8612316SN/A        if (tickEvent.scheduled())
8622316SN/A            tickEvent.squash();
8632863Sktlim@umich.edu
8642905Sktlim@umich.edu        changeState(SimObject::Drained);
8652863Sktlim@umich.edu
8663126Sktlim@umich.edu        BaseCPU::switchOut();
8673126Sktlim@umich.edu
8682863Sktlim@umich.edu        if (drainEvent) {
8692863Sktlim@umich.edu            drainEvent->process();
8702863Sktlim@umich.edu            drainEvent = NULL;
8712863Sktlim@umich.edu        }
8722310SN/A    }
8732843Sktlim@umich.edu    assert(drainCount <= 5);
8742843Sktlim@umich.edu}
8752843Sktlim@umich.edu
8762843Sktlim@umich.edutemplate <class Impl>
8772843Sktlim@umich.eduvoid
8782843Sktlim@umich.eduFullO3CPU<Impl>::switchOut()
8792843Sktlim@umich.edu{
8802843Sktlim@umich.edu    fetch.switchOut();
8812843Sktlim@umich.edu    rename.switchOut();
8822325SN/A    iew.switchOut();
8832843Sktlim@umich.edu    commit.switchOut();
8842843Sktlim@umich.edu    instList.clear();
8852843Sktlim@umich.edu    while (!removeList.empty()) {
8862843Sktlim@umich.edu        removeList.pop();
8872843Sktlim@umich.edu    }
8882843Sktlim@umich.edu
8892843Sktlim@umich.edu    _status = SwitchedOut;
8902843Sktlim@umich.edu#if USE_CHECKER
8912843Sktlim@umich.edu    if (checker)
8922843Sktlim@umich.edu        checker->switchOut();
8932843Sktlim@umich.edu#endif
8943126Sktlim@umich.edu    if (tickEvent.scheduled())
8953126Sktlim@umich.edu        tickEvent.squash();
8961060SN/A}
8971060SN/A
8981060SN/Atemplate <class Impl>
8991060SN/Avoid
9001755SN/AFullO3CPU<Impl>::takeOverFrom(BaseCPU *oldCPU)
9011060SN/A{
9022325SN/A    // Flush out any old data from the time buffers.
9032873Sktlim@umich.edu    for (int i = 0; i < timeBuffer.getSize(); ++i) {
9042307SN/A        timeBuffer.advance();
9052307SN/A        fetchQueue.advance();
9062307SN/A        decodeQueue.advance();
9072307SN/A        renameQueue.advance();
9082307SN/A        iewQueue.advance();
9092307SN/A    }
9102307SN/A
9112325SN/A    activityRec.reset();
9122307SN/A
9131060SN/A    BaseCPU::takeOverFrom(oldCPU);
9141060SN/A
9152307SN/A    fetch.takeOverFrom();
9162307SN/A    decode.takeOverFrom();
9172307SN/A    rename.takeOverFrom();
9182307SN/A    iew.takeOverFrom();
9192307SN/A    commit.takeOverFrom();
9202307SN/A
9211060SN/A    assert(!tickEvent.scheduled());
9221060SN/A
9232325SN/A    // @todo: Figure out how to properly select the tid to put onto
9242325SN/A    // the active threads list.
9252307SN/A    int tid = 0;
9262307SN/A
9272307SN/A    list<unsigned>::iterator isActive = find(
9282307SN/A        activeThreads.begin(), activeThreads.end(), tid);
9292307SN/A
9302307SN/A    if (isActive == activeThreads.end()) {
9312325SN/A        //May Need to Re-code this if the delay variable is the delay
9322325SN/A        //needed for thread to activate
9332733Sktlim@umich.edu        DPRINTF(O3CPU, "Adding Thread %i to active threads list\n",
9342307SN/A                tid);
9352307SN/A
9362307SN/A        activeThreads.push_back(tid);
9372307SN/A    }
9382307SN/A
9392325SN/A    // Set all statuses to active, schedule the CPU's tick event.
9402307SN/A    // @todo: Fix up statuses so this is handled properly
9412680Sktlim@umich.edu    for (int i = 0; i < threadContexts.size(); ++i) {
9422680Sktlim@umich.edu        ThreadContext *tc = threadContexts[i];
9432680Sktlim@umich.edu        if (tc->status() == ThreadContext::Active && _status != Running) {
9441681SN/A            _status = Running;
9451681SN/A            tickEvent.schedule(curTick);
9461681SN/A        }
9471060SN/A    }
9482307SN/A    if (!tickEvent.scheduled())
9492307SN/A        tickEvent.schedule(curTick);
9503221Sktlim@umich.edu
9513221Sktlim@umich.edu    Port *peer;
9523221Sktlim@umich.edu    Port *icachePort = fetch.getIcachePort();
9533221Sktlim@umich.edu    if (icachePort->getPeer() == NULL) {
9543221Sktlim@umich.edu        peer = oldCPU->getPort("icachePort")->getPeer();
9553221Sktlim@umich.edu        icachePort->setPeer(peer);
9563221Sktlim@umich.edu    } else {
9573221Sktlim@umich.edu        peer = icachePort->getPeer();
9583221Sktlim@umich.edu    }
9593221Sktlim@umich.edu    peer->setPeer(icachePort);
9603221Sktlim@umich.edu
9613221Sktlim@umich.edu    Port *dcachePort = iew.getDcachePort();
9623221Sktlim@umich.edu    if (dcachePort->getPeer() == NULL) {
9633221Sktlim@umich.edu        Port *peer = oldCPU->getPort("dcachePort")->getPeer();
9643221Sktlim@umich.edu        dcachePort->setPeer(peer);
9653221Sktlim@umich.edu    } else {
9663221Sktlim@umich.edu        peer = dcachePort->getPeer();
9673221Sktlim@umich.edu    }
9683221Sktlim@umich.edu    peer->setPeer(dcachePort);
9691060SN/A}
9701060SN/A
9711060SN/Atemplate <class Impl>
9721060SN/Auint64_t
9731755SN/AFullO3CPU<Impl>::readIntReg(int reg_idx)
9741060SN/A{
9751060SN/A    return regFile.readIntReg(reg_idx);
9761060SN/A}
9771060SN/A
9781060SN/Atemplate <class Impl>
9792455SN/AFloatReg
9802455SN/AFullO3CPU<Impl>::readFloatReg(int reg_idx, int width)
9811060SN/A{
9822455SN/A    return regFile.readFloatReg(reg_idx, width);
9831060SN/A}
9841060SN/A
9851060SN/Atemplate <class Impl>
9862455SN/AFloatReg
9872455SN/AFullO3CPU<Impl>::readFloatReg(int reg_idx)
9881060SN/A{
9892455SN/A    return regFile.readFloatReg(reg_idx);
9901060SN/A}
9911060SN/A
9921060SN/Atemplate <class Impl>
9932455SN/AFloatRegBits
9942455SN/AFullO3CPU<Impl>::readFloatRegBits(int reg_idx, int width)
9951060SN/A{
9962455SN/A    return regFile.readFloatRegBits(reg_idx, width);
9972455SN/A}
9982455SN/A
9992455SN/Atemplate <class Impl>
10002455SN/AFloatRegBits
10012455SN/AFullO3CPU<Impl>::readFloatRegBits(int reg_idx)
10022455SN/A{
10032455SN/A    return regFile.readFloatRegBits(reg_idx);
10041060SN/A}
10051060SN/A
10061060SN/Atemplate <class Impl>
10071060SN/Avoid
10081755SN/AFullO3CPU<Impl>::setIntReg(int reg_idx, uint64_t val)
10091060SN/A{
10101060SN/A    regFile.setIntReg(reg_idx, val);
10111060SN/A}
10121060SN/A
10131060SN/Atemplate <class Impl>
10141060SN/Avoid
10152455SN/AFullO3CPU<Impl>::setFloatReg(int reg_idx, FloatReg val, int width)
10161060SN/A{
10172455SN/A    regFile.setFloatReg(reg_idx, val, width);
10181060SN/A}
10191060SN/A
10201060SN/Atemplate <class Impl>
10211060SN/Avoid
10222455SN/AFullO3CPU<Impl>::setFloatReg(int reg_idx, FloatReg val)
10231060SN/A{
10242455SN/A    regFile.setFloatReg(reg_idx, val);
10251060SN/A}
10261060SN/A
10271060SN/Atemplate <class Impl>
10281060SN/Avoid
10292455SN/AFullO3CPU<Impl>::setFloatRegBits(int reg_idx, FloatRegBits val, int width)
10301060SN/A{
10312455SN/A    regFile.setFloatRegBits(reg_idx, val, width);
10322455SN/A}
10332455SN/A
10342455SN/Atemplate <class Impl>
10352455SN/Avoid
10362455SN/AFullO3CPU<Impl>::setFloatRegBits(int reg_idx, FloatRegBits val)
10372455SN/A{
10382455SN/A    regFile.setFloatRegBits(reg_idx, val);
10391060SN/A}
10401060SN/A
10411060SN/Atemplate <class Impl>
10421060SN/Auint64_t
10432292SN/AFullO3CPU<Impl>::readArchIntReg(int reg_idx, unsigned tid)
10441060SN/A{
10452292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10462292SN/A
10472292SN/A    return regFile.readIntReg(phys_reg);
10482292SN/A}
10492292SN/A
10502292SN/Atemplate <class Impl>
10512292SN/Afloat
10522292SN/AFullO3CPU<Impl>::readArchFloatRegSingle(int reg_idx, unsigned tid)
10532292SN/A{
10542307SN/A    int idx = reg_idx + TheISA::FP_Base_DepTag;
10552307SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
10562292SN/A
10572669Sktlim@umich.edu    return regFile.readFloatReg(phys_reg);
10582292SN/A}
10592292SN/A
10602292SN/Atemplate <class Impl>
10612292SN/Adouble
10622292SN/AFullO3CPU<Impl>::readArchFloatRegDouble(int reg_idx, unsigned tid)
10632292SN/A{
10642307SN/A    int idx = reg_idx + TheISA::FP_Base_DepTag;
10652307SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
10662292SN/A
10672669Sktlim@umich.edu    return regFile.readFloatReg(phys_reg, 64);
10682292SN/A}
10692292SN/A
10702292SN/Atemplate <class Impl>
10712292SN/Auint64_t
10722292SN/AFullO3CPU<Impl>::readArchFloatRegInt(int reg_idx, unsigned tid)
10732292SN/A{
10742307SN/A    int idx = reg_idx + TheISA::FP_Base_DepTag;
10752307SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
10762292SN/A
10772669Sktlim@umich.edu    return regFile.readFloatRegBits(phys_reg);
10781060SN/A}
10791060SN/A
10801060SN/Atemplate <class Impl>
10811060SN/Avoid
10822292SN/AFullO3CPU<Impl>::setArchIntReg(int reg_idx, uint64_t val, unsigned tid)
10831060SN/A{
10842292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10852292SN/A
10862292SN/A    regFile.setIntReg(phys_reg, val);
10871060SN/A}
10881060SN/A
10891060SN/Atemplate <class Impl>
10901060SN/Avoid
10912292SN/AFullO3CPU<Impl>::setArchFloatRegSingle(int reg_idx, float val, unsigned tid)
10921060SN/A{
10932918Sktlim@umich.edu    int idx = reg_idx + TheISA::FP_Base_DepTag;
10942918Sktlim@umich.edu    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
10952292SN/A
10962669Sktlim@umich.edu    regFile.setFloatReg(phys_reg, val);
10971060SN/A}
10981060SN/A
10991060SN/Atemplate <class Impl>
11001060SN/Avoid
11012292SN/AFullO3CPU<Impl>::setArchFloatRegDouble(int reg_idx, double val, unsigned tid)
11021060SN/A{
11032918Sktlim@umich.edu    int idx = reg_idx + TheISA::FP_Base_DepTag;
11042918Sktlim@umich.edu    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
11052292SN/A
11062669Sktlim@umich.edu    regFile.setFloatReg(phys_reg, val, 64);
11071060SN/A}
11081060SN/A
11091060SN/Atemplate <class Impl>
11101060SN/Avoid
11112292SN/AFullO3CPU<Impl>::setArchFloatRegInt(int reg_idx, uint64_t val, unsigned tid)
11121060SN/A{
11132918Sktlim@umich.edu    int idx = reg_idx + TheISA::FP_Base_DepTag;
11142918Sktlim@umich.edu    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
11151060SN/A
11162669Sktlim@umich.edu    regFile.setFloatRegBits(phys_reg, val);
11172292SN/A}
11182292SN/A
11192292SN/Atemplate <class Impl>
11202292SN/Auint64_t
11212292SN/AFullO3CPU<Impl>::readPC(unsigned tid)
11222292SN/A{
11232292SN/A    return commit.readPC(tid);
11241060SN/A}
11251060SN/A
11261060SN/Atemplate <class Impl>
11271060SN/Avoid
11282292SN/AFullO3CPU<Impl>::setPC(Addr new_PC,unsigned tid)
11291060SN/A{
11302292SN/A    commit.setPC(new_PC, tid);
11312292SN/A}
11321060SN/A
11332292SN/Atemplate <class Impl>
11342292SN/Auint64_t
11352292SN/AFullO3CPU<Impl>::readNextPC(unsigned tid)
11362292SN/A{
11372292SN/A    return commit.readNextPC(tid);
11382292SN/A}
11391060SN/A
11402292SN/Atemplate <class Impl>
11412292SN/Avoid
11422292SN/AFullO3CPU<Impl>::setNextPC(uint64_t val,unsigned tid)
11432292SN/A{
11442292SN/A    commit.setNextPC(val, tid);
11452292SN/A}
11461060SN/A
11472756Sksewell@umich.edutemplate <class Impl>
11482756Sksewell@umich.eduuint64_t
11492756Sksewell@umich.eduFullO3CPU<Impl>::readNextNPC(unsigned tid)
11502756Sksewell@umich.edu{
11512756Sksewell@umich.edu    return commit.readNextNPC(tid);
11522756Sksewell@umich.edu}
11532756Sksewell@umich.edu
11542756Sksewell@umich.edutemplate <class Impl>
11552756Sksewell@umich.eduvoid
11562935Sksewell@umich.eduFullO3CPU<Impl>::setNextNPC(uint64_t val,unsigned tid)
11572756Sksewell@umich.edu{
11582756Sksewell@umich.edu    commit.setNextNPC(val, tid);
11592756Sksewell@umich.edu}
11602756Sksewell@umich.edu
11612292SN/Atemplate <class Impl>
11622292SN/Atypename FullO3CPU<Impl>::ListIt
11632292SN/AFullO3CPU<Impl>::addInst(DynInstPtr &inst)
11642292SN/A{
11652292SN/A    instList.push_back(inst);
11661060SN/A
11672292SN/A    return --(instList.end());
11682292SN/A}
11691060SN/A
11702292SN/Atemplate <class Impl>
11712292SN/Avoid
11722292SN/AFullO3CPU<Impl>::instDone(unsigned tid)
11732292SN/A{
11742292SN/A    // Keep an instruction count.
11752292SN/A    thread[tid]->numInst++;
11762292SN/A    thread[tid]->numInsts++;
11772292SN/A    committedInsts[tid]++;
11782292SN/A    totalCommittedInsts++;
11792292SN/A
11802292SN/A    // Check for instruction-count-based events.
11812292SN/A    comInstEventQueue[tid]->serviceEvents(thread[tid]->numInst);
11822292SN/A}
11832292SN/A
11842292SN/Atemplate <class Impl>
11852292SN/Avoid
11862292SN/AFullO3CPU<Impl>::addToRemoveList(DynInstPtr &inst)
11872292SN/A{
11882292SN/A    removeInstsThisCycle = true;
11892292SN/A
11902292SN/A    removeList.push(inst->getInstListIt());
11911060SN/A}
11921060SN/A
11931060SN/Atemplate <class Impl>
11941060SN/Avoid
11951755SN/AFullO3CPU<Impl>::removeFrontInst(DynInstPtr &inst)
11961060SN/A{
11972733Sktlim@umich.edu    DPRINTF(O3CPU, "Removing committed instruction [tid:%i] PC %#x "
11982292SN/A            "[sn:%lli]\n",
11992303SN/A            inst->threadNumber, inst->readPC(), inst->seqNum);
12001060SN/A
12012292SN/A    removeInstsThisCycle = true;
12021060SN/A
12031060SN/A    // Remove the front instruction.
12042292SN/A    removeList.push(inst->getInstListIt());
12051060SN/A}
12061060SN/A
12071060SN/Atemplate <class Impl>
12081060SN/Avoid
12092935Sksewell@umich.eduFullO3CPU<Impl>::removeInstsNotInROB(unsigned tid,
12102935Sksewell@umich.edu                                     bool squash_delay_slot,
12112935Sksewell@umich.edu                                     const InstSeqNum &delay_slot_seq_num)
12121060SN/A{
12132733Sktlim@umich.edu    DPRINTF(O3CPU, "Thread %i: Deleting instructions from instruction"
12142292SN/A            " list.\n", tid);
12151060SN/A
12162292SN/A    ListIt end_it;
12171060SN/A
12182292SN/A    bool rob_empty = false;
12192292SN/A
12202292SN/A    if (instList.empty()) {
12212292SN/A        return;
12222292SN/A    } else if (rob.isEmpty(/*tid*/)) {
12232733Sktlim@umich.edu        DPRINTF(O3CPU, "ROB is empty, squashing all insts.\n");
12242292SN/A        end_it = instList.begin();
12252292SN/A        rob_empty = true;
12262292SN/A    } else {
12272292SN/A        end_it = (rob.readTailInst(tid))->getInstListIt();
12282733Sktlim@umich.edu        DPRINTF(O3CPU, "ROB is not empty, squashing insts not in ROB.\n");
12292292SN/A    }
12302292SN/A
12312292SN/A    removeInstsThisCycle = true;
12322292SN/A
12332292SN/A    ListIt inst_it = instList.end();
12342292SN/A
12352292SN/A    inst_it--;
12362292SN/A
12372292SN/A    // Walk through the instruction list, removing any instructions
12382292SN/A    // that were inserted after the given instruction iterator, end_it.
12392292SN/A    while (inst_it != end_it) {
12402292SN/A        assert(!instList.empty());
12412292SN/A
12423093Sksewell@umich.edu#if ISA_HAS_DELAY_SLOT
12432935Sksewell@umich.edu        if(!squash_delay_slot &&
12442935Sksewell@umich.edu           delay_slot_seq_num >= (*inst_it)->seqNum) {
12452935Sksewell@umich.edu            break;
12462935Sksewell@umich.edu        }
12472935Sksewell@umich.edu#endif
12482292SN/A        squashInstIt(inst_it, tid);
12492292SN/A
12502292SN/A        inst_it--;
12512292SN/A    }
12522292SN/A
12532292SN/A    // If the ROB was empty, then we actually need to remove the first
12542292SN/A    // instruction as well.
12552292SN/A    if (rob_empty) {
12562292SN/A        squashInstIt(inst_it, tid);
12572292SN/A    }
12581060SN/A}
12591060SN/A
12601060SN/Atemplate <class Impl>
12611060SN/Avoid
12622292SN/AFullO3CPU<Impl>::removeInstsUntil(const InstSeqNum &seq_num,
12632292SN/A                                  unsigned tid)
12641062SN/A{
12652292SN/A    assert(!instList.empty());
12662292SN/A
12672292SN/A    removeInstsThisCycle = true;
12682292SN/A
12692292SN/A    ListIt inst_iter = instList.end();
12702292SN/A
12712292SN/A    inst_iter--;
12722292SN/A
12732733Sktlim@umich.edu    DPRINTF(O3CPU, "Deleting instructions from instruction "
12742292SN/A            "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
12752292SN/A            tid, seq_num, (*inst_iter)->seqNum);
12761062SN/A
12772292SN/A    while ((*inst_iter)->seqNum > seq_num) {
12781062SN/A
12792292SN/A        bool break_loop = (inst_iter == instList.begin());
12801062SN/A
12812292SN/A        squashInstIt(inst_iter, tid);
12821062SN/A
12832292SN/A        inst_iter--;
12841062SN/A
12852292SN/A        if (break_loop)
12862292SN/A            break;
12872292SN/A    }
12882292SN/A}
12892292SN/A
12902292SN/Atemplate <class Impl>
12912292SN/Ainline void
12922292SN/AFullO3CPU<Impl>::squashInstIt(const ListIt &instIt, const unsigned &tid)
12932292SN/A{
12942292SN/A    if ((*instIt)->threadNumber == tid) {
12952733Sktlim@umich.edu        DPRINTF(O3CPU, "Squashing instruction, "
12962292SN/A                "[tid:%i] [sn:%lli] PC %#x\n",
12972292SN/A                (*instIt)->threadNumber,
12982292SN/A                (*instIt)->seqNum,
12992292SN/A                (*instIt)->readPC());
13001062SN/A
13011062SN/A        // Mark it as squashed.
13022292SN/A        (*instIt)->setSquashed();
13032292SN/A
13042325SN/A        // @todo: Formulate a consistent method for deleting
13052325SN/A        // instructions from the instruction list
13062292SN/A        // Remove the instruction from the list.
13072292SN/A        removeList.push(instIt);
13082292SN/A    }
13092292SN/A}
13102292SN/A
13112292SN/Atemplate <class Impl>
13122292SN/Avoid
13132292SN/AFullO3CPU<Impl>::cleanUpRemovedInsts()
13142292SN/A{
13152292SN/A    while (!removeList.empty()) {
13162733Sktlim@umich.edu        DPRINTF(O3CPU, "Removing instruction, "
13172292SN/A                "[tid:%i] [sn:%lli] PC %#x\n",
13182292SN/A                (*removeList.front())->threadNumber,
13192292SN/A                (*removeList.front())->seqNum,
13202292SN/A                (*removeList.front())->readPC());
13212292SN/A
13222292SN/A        instList.erase(removeList.front());
13232292SN/A
13242292SN/A        removeList.pop();
13251062SN/A    }
13261062SN/A
13272292SN/A    removeInstsThisCycle = false;
13281062SN/A}
13292325SN/A/*
13301062SN/Atemplate <class Impl>
13311062SN/Avoid
13321755SN/AFullO3CPU<Impl>::removeAllInsts()
13331060SN/A{
13341060SN/A    instList.clear();
13351060SN/A}
13362325SN/A*/
13371060SN/Atemplate <class Impl>
13381060SN/Avoid
13391755SN/AFullO3CPU<Impl>::dumpInsts()
13401060SN/A{
13411060SN/A    int num = 0;
13421060SN/A
13432292SN/A    ListIt inst_list_it = instList.begin();
13442292SN/A
13452292SN/A    cprintf("Dumping Instruction List\n");
13462292SN/A
13472292SN/A    while (inst_list_it != instList.end()) {
13482292SN/A        cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
13492292SN/A                "Squashed:%i\n\n",
13502292SN/A                num, (*inst_list_it)->readPC(), (*inst_list_it)->threadNumber,
13512292SN/A                (*inst_list_it)->seqNum, (*inst_list_it)->isIssued(),
13522292SN/A                (*inst_list_it)->isSquashed());
13531060SN/A        inst_list_it++;
13541060SN/A        ++num;
13551060SN/A    }
13561060SN/A}
13572325SN/A/*
13581060SN/Atemplate <class Impl>
13591060SN/Avoid
13601755SN/AFullO3CPU<Impl>::wakeDependents(DynInstPtr &inst)
13611060SN/A{
13621060SN/A    iew.wakeDependents(inst);
13631060SN/A}
13642325SN/A*/
13652292SN/Atemplate <class Impl>
13662292SN/Avoid
13672292SN/AFullO3CPU<Impl>::wakeCPU()
13682292SN/A{
13692325SN/A    if (activityRec.active() || tickEvent.scheduled()) {
13702325SN/A        DPRINTF(Activity, "CPU already running.\n");
13712292SN/A        return;
13722292SN/A    }
13732292SN/A
13742325SN/A    DPRINTF(Activity, "Waking up CPU\n");
13752325SN/A
13762325SN/A    idleCycles += (curTick - 1) - lastRunningCycle;
13772292SN/A
13782292SN/A    tickEvent.schedule(curTick);
13792292SN/A}
13802292SN/A
13812292SN/Atemplate <class Impl>
13822292SN/Aint
13832292SN/AFullO3CPU<Impl>::getFreeTid()
13842292SN/A{
13852292SN/A    for (int i=0; i < numThreads; i++) {
13862292SN/A        if (!tids[i]) {
13872292SN/A            tids[i] = true;
13882292SN/A            return i;
13892292SN/A        }
13902292SN/A    }
13912292SN/A
13922292SN/A    return -1;
13932292SN/A}
13942292SN/A
13952292SN/Atemplate <class Impl>
13962292SN/Avoid
13972292SN/AFullO3CPU<Impl>::doContextSwitch()
13982292SN/A{
13992292SN/A    if (contextSwitch) {
14002292SN/A
14012292SN/A        //ADD CODE TO DEACTIVE THREAD HERE (???)
14022292SN/A
14032292SN/A        for (int tid=0; tid < cpuWaitList.size(); tid++) {
14042292SN/A            activateWhenReady(tid);
14052292SN/A        }
14062292SN/A
14072292SN/A        if (cpuWaitList.size() == 0)
14082292SN/A            contextSwitch = true;
14092292SN/A    }
14102292SN/A}
14112292SN/A
14122292SN/Atemplate <class Impl>
14132292SN/Avoid
14142292SN/AFullO3CPU<Impl>::updateThreadPriority()
14152292SN/A{
14162292SN/A    if (activeThreads.size() > 1)
14172292SN/A    {
14182292SN/A        //DEFAULT TO ROUND ROBIN SCHEME
14192292SN/A        //e.g. Move highest priority to end of thread list
14202292SN/A        list<unsigned>::iterator list_begin = activeThreads.begin();
14212292SN/A        list<unsigned>::iterator list_end   = activeThreads.end();
14222292SN/A
14232292SN/A        unsigned high_thread = *list_begin;
14242292SN/A
14252292SN/A        activeThreads.erase(list_begin);
14262292SN/A
14272292SN/A        activeThreads.push_back(high_thread);
14282292SN/A    }
14292292SN/A}
14301060SN/A
14311755SN/A// Forward declaration of FullO3CPU.
14322818Sksewell@umich.edutemplate class FullO3CPU<O3CPUImpl>;
1433