cpu.cc revision 8707
112841Sgabeblack@google.com/*
212841Sgabeblack@google.com * Copyright (c) 2011 ARM Limited
312841Sgabeblack@google.com * All rights reserved
412841Sgabeblack@google.com *
512841Sgabeblack@google.com * The license below extends only to copyright in the software and shall
612841Sgabeblack@google.com * not be construed as granting a license to any other intellectual
712841Sgabeblack@google.com * property including but not limited to intellectual property relating
812841Sgabeblack@google.com * to a hardware implementation of the functionality of the software
912841Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1012841Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1112841Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1212841Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1312841Sgabeblack@google.com *
1412841Sgabeblack@google.com * Copyright (c) 2004-2006 The Regents of The University of Michigan
1512841Sgabeblack@google.com * Copyright (c) 2011 Regents of the University of California
1612841Sgabeblack@google.com * All rights reserved.
1712841Sgabeblack@google.com *
1812841Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1912841Sgabeblack@google.com * modification, are permitted provided that the following conditions are
2012841Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
2112841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
2212841Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
2312841Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2412841Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2512841Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2612841Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2712841Sgabeblack@google.com * this software without specific prior written permission.
2812841Sgabeblack@google.com *
2912841Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
3012841Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3112841Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
3212841Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
3312841Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3412841Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3512841Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3612841Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3713198Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3812841Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3913198Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4012841Sgabeblack@google.com *
4112841Sgabeblack@google.com * Authors: Kevin Lim
4213198Sgabeblack@google.com *          Korey Sewell
4313198Sgabeblack@google.com *          Rick Strong
4412841Sgabeblack@google.com */
4512841Sgabeblack@google.com
4612841Sgabeblack@google.com#include "config/full_system.hh"
4712841Sgabeblack@google.com#include "config/the_isa.hh"
4812841Sgabeblack@google.com#include "config/use_checker.hh"
4913198Sgabeblack@google.com#include "cpu/o3/cpu.hh"
5013198Sgabeblack@google.com#include "cpu/o3/isa_specific.hh"
5112841Sgabeblack@google.com#include "cpu/o3/thread_context.hh"
5212841Sgabeblack@google.com#include "cpu/activity.hh"
5312841Sgabeblack@google.com#include "cpu/simple_thread.hh"
5412841Sgabeblack@google.com#include "cpu/thread_context.hh"
5512841Sgabeblack@google.com#include "debug/Activity.hh"
5612841Sgabeblack@google.com#include "debug/O3CPU.hh"
5713198Sgabeblack@google.com#include "debug/Quiesce.hh"
5813198Sgabeblack@google.com#include "enums/MemoryMode.hh"
5913198Sgabeblack@google.com#include "sim/core.hh"
6013198Sgabeblack@google.com#include "sim/stat_control.hh"
6112841Sgabeblack@google.com#include "sim/system.hh"
6212841Sgabeblack@google.com
6312841Sgabeblack@google.com#if FULL_SYSTEM
6412841Sgabeblack@google.com#include "cpu/quiesce_event.hh"
6512841Sgabeblack@google.com#else
6612841Sgabeblack@google.com#include "sim/process.hh"
6713198Sgabeblack@google.com#endif
6813198Sgabeblack@google.com
6912841Sgabeblack@google.com#if USE_CHECKER
7012841Sgabeblack@google.com#include "cpu/checker/cpu.hh"
7112841Sgabeblack@google.com#endif
7213198Sgabeblack@google.com
7312841Sgabeblack@google.com#if THE_ISA == ALPHA_ISA
7412841Sgabeblack@google.com#include "arch/alpha/osfpal.hh"
75#include "debug/Activity.hh"
76#endif
77
78class BaseCPUParams;
79
80using namespace TheISA;
81using namespace std;
82
83BaseO3CPU::BaseO3CPU(BaseCPUParams *params)
84    : BaseCPU(params)
85{
86}
87
88void
89BaseO3CPU::regStats()
90{
91    BaseCPU::regStats();
92}
93
94template<class Impl>
95bool
96FullO3CPU<Impl>::IcachePort::recvTiming(PacketPtr pkt)
97{
98    DPRINTF(O3CPU, "Fetch unit received timing\n");
99    if (pkt->isResponse()) {
100        // We shouldn't ever get a block in ownership state
101        assert(!(pkt->memInhibitAsserted() && !pkt->sharedAsserted()));
102
103        fetch->processCacheCompletion(pkt);
104    }
105    //else Snooped a coherence request, just return
106    return true;
107}
108
109template<class Impl>
110void
111FullO3CPU<Impl>::IcachePort::recvRetry()
112{
113    fetch->recvRetry();
114}
115
116template <class Impl>
117bool
118FullO3CPU<Impl>::DcachePort::recvTiming(PacketPtr pkt)
119{
120    return lsq->recvTiming(pkt);
121}
122
123template <class Impl>
124void
125FullO3CPU<Impl>::DcachePort::recvRetry()
126{
127    lsq->recvRetry();
128}
129
130template <class Impl>
131FullO3CPU<Impl>::TickEvent::TickEvent(FullO3CPU<Impl> *c)
132    : Event(CPU_Tick_Pri), cpu(c)
133{
134}
135
136template <class Impl>
137void
138FullO3CPU<Impl>::TickEvent::process()
139{
140    cpu->tick();
141}
142
143template <class Impl>
144const char *
145FullO3CPU<Impl>::TickEvent::description() const
146{
147    return "FullO3CPU tick";
148}
149
150template <class Impl>
151FullO3CPU<Impl>::ActivateThreadEvent::ActivateThreadEvent()
152    : Event(CPU_Switch_Pri)
153{
154}
155
156template <class Impl>
157void
158FullO3CPU<Impl>::ActivateThreadEvent::init(int thread_num,
159                                           FullO3CPU<Impl> *thread_cpu)
160{
161    tid = thread_num;
162    cpu = thread_cpu;
163}
164
165template <class Impl>
166void
167FullO3CPU<Impl>::ActivateThreadEvent::process()
168{
169    cpu->activateThread(tid);
170}
171
172template <class Impl>
173const char *
174FullO3CPU<Impl>::ActivateThreadEvent::description() const
175{
176    return "FullO3CPU \"Activate Thread\"";
177}
178
179template <class Impl>
180FullO3CPU<Impl>::DeallocateContextEvent::DeallocateContextEvent()
181    : Event(CPU_Tick_Pri), tid(0), remove(false), cpu(NULL)
182{
183}
184
185template <class Impl>
186void
187FullO3CPU<Impl>::DeallocateContextEvent::init(int thread_num,
188                                              FullO3CPU<Impl> *thread_cpu)
189{
190    tid = thread_num;
191    cpu = thread_cpu;
192    remove = false;
193}
194
195template <class Impl>
196void
197FullO3CPU<Impl>::DeallocateContextEvent::process()
198{
199    cpu->deactivateThread(tid);
200    if (remove)
201        cpu->removeThread(tid);
202}
203
204template <class Impl>
205const char *
206FullO3CPU<Impl>::DeallocateContextEvent::description() const
207{
208    return "FullO3CPU \"Deallocate Context\"";
209}
210
211template <class Impl>
212FullO3CPU<Impl>::FullO3CPU(DerivO3CPUParams *params)
213    : BaseO3CPU(params),
214      itb(params->itb),
215      dtb(params->dtb),
216      tickEvent(this),
217#ifndef NDEBUG
218      instcount(0),
219#endif
220      removeInstsThisCycle(false),
221      fetch(this, params),
222      decode(this, params),
223      rename(this, params),
224      iew(this, params),
225      commit(this, params),
226
227      regFile(this, params->numPhysIntRegs,
228              params->numPhysFloatRegs),
229
230      freeList(params->numThreads,
231               TheISA::NumIntRegs, params->numPhysIntRegs,
232               TheISA::NumFloatRegs, params->numPhysFloatRegs),
233
234      rob(this,
235          params->numROBEntries, params->squashWidth,
236          params->smtROBPolicy, params->smtROBThreshold,
237          params->numThreads),
238
239      scoreboard(params->numThreads,
240                 TheISA::NumIntRegs, params->numPhysIntRegs,
241                 TheISA::NumFloatRegs, params->numPhysFloatRegs,
242                 TheISA::NumMiscRegs * numThreads,
243                 TheISA::ZeroReg),
244
245      icachePort(&fetch, this),
246      dcachePort(&iew.ldstQueue, this),
247
248      timeBuffer(params->backComSize, params->forwardComSize),
249      fetchQueue(params->backComSize, params->forwardComSize),
250      decodeQueue(params->backComSize, params->forwardComSize),
251      renameQueue(params->backComSize, params->forwardComSize),
252      iewQueue(params->backComSize, params->forwardComSize),
253      activityRec(name(), NumStages,
254                  params->backComSize + params->forwardComSize,
255                  params->activity),
256
257      globalSeqNum(1),
258      system(params->system),
259      drainCount(0),
260      deferRegistration(params->defer_registration)
261{
262    if (!deferRegistration) {
263        _status = Running;
264    } else {
265        _status = Idle;
266    }
267
268#if USE_CHECKER
269    if (params->checker) {
270        BaseCPU *temp_checker = params->checker;
271        checker = dynamic_cast<Checker<DynInstPtr> *>(temp_checker);
272        checker->setIcachePort(&icachePort);
273#if FULL_SYSTEM
274        checker->setSystem(params->system);
275#endif
276    } else {
277        checker = NULL;
278    }
279#endif // USE_CHECKER
280
281#if !FULL_SYSTEM
282    thread.resize(numThreads);
283    tids.resize(numThreads);
284#endif
285
286    // The stages also need their CPU pointer setup.  However this
287    // must be done at the upper level CPU because they have pointers
288    // to the upper level CPU, and not this FullO3CPU.
289
290    // Set up Pointers to the activeThreads list for each stage
291    fetch.setActiveThreads(&activeThreads);
292    decode.setActiveThreads(&activeThreads);
293    rename.setActiveThreads(&activeThreads);
294    iew.setActiveThreads(&activeThreads);
295    commit.setActiveThreads(&activeThreads);
296
297    // Give each of the stages the time buffer they will use.
298    fetch.setTimeBuffer(&timeBuffer);
299    decode.setTimeBuffer(&timeBuffer);
300    rename.setTimeBuffer(&timeBuffer);
301    iew.setTimeBuffer(&timeBuffer);
302    commit.setTimeBuffer(&timeBuffer);
303
304    // Also setup each of the stages' queues.
305    fetch.setFetchQueue(&fetchQueue);
306    decode.setFetchQueue(&fetchQueue);
307    commit.setFetchQueue(&fetchQueue);
308    decode.setDecodeQueue(&decodeQueue);
309    rename.setDecodeQueue(&decodeQueue);
310    rename.setRenameQueue(&renameQueue);
311    iew.setRenameQueue(&renameQueue);
312    iew.setIEWQueue(&iewQueue);
313    commit.setIEWQueue(&iewQueue);
314    commit.setRenameQueue(&renameQueue);
315
316    commit.setIEWStage(&iew);
317    rename.setIEWStage(&iew);
318    rename.setCommitStage(&commit);
319
320#if !FULL_SYSTEM
321    ThreadID active_threads = params->workload.size();
322
323    if (active_threads > Impl::MaxThreads) {
324        panic("Workload Size too large. Increase the 'MaxThreads'"
325              "constant in your O3CPU impl. file (e.g. o3/alpha/impl.hh) or "
326              "edit your workload size.");
327    }
328#else
329    ThreadID active_threads = 1;
330#endif
331
332    //Make Sure That this a Valid Architeture
333    assert(params->numPhysIntRegs   >= numThreads * TheISA::NumIntRegs);
334    assert(params->numPhysFloatRegs >= numThreads * TheISA::NumFloatRegs);
335
336    rename.setScoreboard(&scoreboard);
337    iew.setScoreboard(&scoreboard);
338
339    // Setup the rename map for whichever stages need it.
340    PhysRegIndex lreg_idx = 0;
341    PhysRegIndex freg_idx = params->numPhysIntRegs; //Index to 1 after int regs
342
343    for (ThreadID tid = 0; tid < numThreads; tid++) {
344        bool bindRegs = (tid <= active_threads - 1);
345
346        commitRenameMap[tid].init(TheISA::NumIntRegs,
347                                  params->numPhysIntRegs,
348                                  lreg_idx,            //Index for Logical. Regs
349
350                                  TheISA::NumFloatRegs,
351                                  params->numPhysFloatRegs,
352                                  freg_idx,            //Index for Float Regs
353
354                                  TheISA::NumMiscRegs,
355
356                                  TheISA::ZeroReg,
357                                  TheISA::ZeroReg,
358
359                                  tid,
360                                  false);
361
362        renameMap[tid].init(TheISA::NumIntRegs,
363                            params->numPhysIntRegs,
364                            lreg_idx,                  //Index for Logical. Regs
365
366                            TheISA::NumFloatRegs,
367                            params->numPhysFloatRegs,
368                            freg_idx,                  //Index for Float Regs
369
370                            TheISA::NumMiscRegs,
371
372                            TheISA::ZeroReg,
373                            TheISA::ZeroReg,
374
375                            tid,
376                            bindRegs);
377
378        activateThreadEvent[tid].init(tid, this);
379        deallocateContextEvent[tid].init(tid, this);
380    }
381
382    rename.setRenameMap(renameMap);
383    commit.setRenameMap(commitRenameMap);
384
385    // Give renameMap & rename stage access to the freeList;
386    for (ThreadID tid = 0; tid < numThreads; tid++)
387        renameMap[tid].setFreeList(&freeList);
388    rename.setFreeList(&freeList);
389
390    // Setup the ROB for whichever stages need it.
391    commit.setROB(&rob);
392
393    lastRunningCycle = curTick();
394
395    lastActivatedCycle = -1;
396#if 0
397    // Give renameMap & rename stage access to the freeList;
398    for (ThreadID tid = 0; tid < numThreads; tid++)
399        globalSeqNum[tid] = 1;
400#endif
401
402    contextSwitch = false;
403    DPRINTF(O3CPU, "Creating O3CPU object.\n");
404
405    // Setup any thread state.
406    this->thread.resize(this->numThreads);
407
408    for (ThreadID tid = 0; tid < this->numThreads; ++tid) {
409#if FULL_SYSTEM
410        // SMT is not supported in FS mode yet.
411        assert(this->numThreads == 1);
412        this->thread[tid] = new Thread(this, 0);
413#else
414        if (tid < params->workload.size()) {
415            DPRINTF(O3CPU, "Workload[%i] process is %#x",
416                    tid, this->thread[tid]);
417            this->thread[tid] = new typename FullO3CPU<Impl>::Thread(
418                    (typename Impl::O3CPU *)(this),
419                    tid, params->workload[tid]);
420
421            //usedTids[tid] = true;
422            //threadMap[tid] = tid;
423        } else {
424            //Allocate Empty thread so M5 can use later
425            //when scheduling threads to CPU
426            Process* dummy_proc = NULL;
427
428            this->thread[tid] = new typename FullO3CPU<Impl>::Thread(
429                    (typename Impl::O3CPU *)(this),
430                    tid, dummy_proc);
431            //usedTids[tid] = false;
432        }
433#endif // !FULL_SYSTEM
434
435        ThreadContext *tc;
436
437        // Setup the TC that will serve as the interface to the threads/CPU.
438        O3ThreadContext<Impl> *o3_tc = new O3ThreadContext<Impl>;
439
440        tc = o3_tc;
441
442        // If we're using a checker, then the TC should be the
443        // CheckerThreadContext.
444#if USE_CHECKER
445        if (params->checker) {
446            tc = new CheckerThreadContext<O3ThreadContext<Impl> >(
447                o3_tc, this->checker);
448        }
449#endif
450
451        o3_tc->cpu = (typename Impl::O3CPU *)(this);
452        assert(o3_tc->cpu);
453        o3_tc->thread = this->thread[tid];
454
455#if FULL_SYSTEM
456        // Setup quiesce event.
457        this->thread[tid]->quiesceEvent = new EndQuiesceEvent(tc);
458#endif
459        // Give the thread the TC.
460        this->thread[tid]->tc = tc;
461
462        // Add the TC to the CPU's list of TC's.
463        this->threadContexts.push_back(tc);
464    }
465
466    for (ThreadID tid = 0; tid < this->numThreads; tid++)
467        this->thread[tid]->setFuncExeInst(0);
468
469    lockAddr = 0;
470    lockFlag = false;
471}
472
473template <class Impl>
474FullO3CPU<Impl>::~FullO3CPU()
475{
476}
477
478template <class Impl>
479void
480FullO3CPU<Impl>::regStats()
481{
482    BaseO3CPU::regStats();
483
484    // Register any of the O3CPU's stats here.
485    timesIdled
486        .name(name() + ".timesIdled")
487        .desc("Number of times that the entire CPU went into an idle state and"
488              " unscheduled itself")
489        .prereq(timesIdled);
490
491    idleCycles
492        .name(name() + ".idleCycles")
493        .desc("Total number of cycles that the CPU has spent unscheduled due "
494              "to idling")
495        .prereq(idleCycles);
496
497    quiesceCycles
498        .name(name() + ".quiesceCycles")
499        .desc("Total number of cycles that CPU has spent quiesced or waiting "
500              "for an interrupt")
501        .prereq(quiesceCycles);
502
503    // Number of Instructions simulated
504    // --------------------------------
505    // Should probably be in Base CPU but need templated
506    // MaxThreads so put in here instead
507    committedInsts
508        .init(numThreads)
509        .name(name() + ".committedInsts")
510        .desc("Number of Instructions Simulated");
511
512    totalCommittedInsts
513        .name(name() + ".committedInsts_total")
514        .desc("Number of Instructions Simulated");
515
516    cpi
517        .name(name() + ".cpi")
518        .desc("CPI: Cycles Per Instruction")
519        .precision(6);
520    cpi = numCycles / committedInsts;
521
522    totalCpi
523        .name(name() + ".cpi_total")
524        .desc("CPI: Total CPI of All Threads")
525        .precision(6);
526    totalCpi = numCycles / totalCommittedInsts;
527
528    ipc
529        .name(name() + ".ipc")
530        .desc("IPC: Instructions Per Cycle")
531        .precision(6);
532    ipc =  committedInsts / numCycles;
533
534    totalIpc
535        .name(name() + ".ipc_total")
536        .desc("IPC: Total IPC of All Threads")
537        .precision(6);
538    totalIpc =  totalCommittedInsts / numCycles;
539
540    this->fetch.regStats();
541    this->decode.regStats();
542    this->rename.regStats();
543    this->iew.regStats();
544    this->commit.regStats();
545    this->rob.regStats();
546
547    intRegfileReads
548        .name(name() + ".int_regfile_reads")
549        .desc("number of integer regfile reads")
550        .prereq(intRegfileReads);
551
552    intRegfileWrites
553        .name(name() + ".int_regfile_writes")
554        .desc("number of integer regfile writes")
555        .prereq(intRegfileWrites);
556
557    fpRegfileReads
558        .name(name() + ".fp_regfile_reads")
559        .desc("number of floating regfile reads")
560        .prereq(fpRegfileReads);
561
562    fpRegfileWrites
563        .name(name() + ".fp_regfile_writes")
564        .desc("number of floating regfile writes")
565        .prereq(fpRegfileWrites);
566
567    miscRegfileReads
568        .name(name() + ".misc_regfile_reads")
569        .desc("number of misc regfile reads")
570        .prereq(miscRegfileReads);
571
572    miscRegfileWrites
573        .name(name() + ".misc_regfile_writes")
574        .desc("number of misc regfile writes")
575        .prereq(miscRegfileWrites);
576}
577
578template <class Impl>
579Port *
580FullO3CPU<Impl>::getPort(const std::string &if_name, int idx)
581{
582    if (if_name == "dcache_port")
583        return &dcachePort;
584    else if (if_name == "icache_port")
585        return &icachePort;
586    else
587        panic("No Such Port\n");
588}
589
590template <class Impl>
591void
592FullO3CPU<Impl>::tick()
593{
594    DPRINTF(O3CPU, "\n\nFullO3CPU: Ticking main, FullO3CPU.\n");
595
596    ++numCycles;
597
598//    activity = false;
599
600    //Tick each of the stages
601    fetch.tick();
602
603    decode.tick();
604
605    rename.tick();
606
607    iew.tick();
608
609    commit.tick();
610
611#if !FULL_SYSTEM
612    doContextSwitch();
613#endif
614
615    // Now advance the time buffers
616    timeBuffer.advance();
617
618    fetchQueue.advance();
619    decodeQueue.advance();
620    renameQueue.advance();
621    iewQueue.advance();
622
623    activityRec.advance();
624
625    if (removeInstsThisCycle) {
626        cleanUpRemovedInsts();
627    }
628
629    if (!tickEvent.scheduled()) {
630        if (_status == SwitchedOut ||
631            getState() == SimObject::Drained) {
632            DPRINTF(O3CPU, "Switched out!\n");
633            // increment stat
634            lastRunningCycle = curTick();
635        } else if (!activityRec.active() || _status == Idle) {
636            DPRINTF(O3CPU, "Idle!\n");
637            lastRunningCycle = curTick();
638            timesIdled++;
639        } else {
640            schedule(tickEvent, nextCycle(curTick() + ticks(1)));
641            DPRINTF(O3CPU, "Scheduling next tick!\n");
642        }
643    }
644
645#if !FULL_SYSTEM
646    updateThreadPriority();
647#endif
648}
649
650template <class Impl>
651void
652FullO3CPU<Impl>::init()
653{
654    BaseCPU::init();
655
656    // Set inSyscall so that the CPU doesn't squash when initially
657    // setting up registers.
658    for (ThreadID tid = 0; tid < numThreads; ++tid)
659        thread[tid]->inSyscall = true;
660
661    // this CPU could still be unconnected if we are restoring from a
662    // checkpoint and this CPU is to be switched in, thus we can only
663    // do this here if the instruction port is actually connected, if
664    // not we have to do it as part of takeOverFrom
665    if (icachePort.isConnected())
666        fetch.setIcache();
667
668#if FULL_SYSTEM
669    for (ThreadID tid = 0; tid < numThreads; tid++) {
670        ThreadContext *src_tc = threadContexts[tid];
671        TheISA::initCPU(src_tc, src_tc->contextId());
672        // Initialise the ThreadContext's memory proxies
673        thread[tid]->initMemProxies(thread[tid]->getTC());
674    }
675#endif
676
677    // Clear inSyscall.
678    for (int tid = 0; tid < numThreads; ++tid)
679        thread[tid]->inSyscall = false;
680
681    // Initialize stages.
682    fetch.initStage();
683    iew.initStage();
684    rename.initStage();
685    commit.initStage();
686
687    commit.setThreads(thread);
688}
689
690template <class Impl>
691void
692FullO3CPU<Impl>::activateThread(ThreadID tid)
693{
694    list<ThreadID>::iterator isActive =
695        std::find(activeThreads.begin(), activeThreads.end(), tid);
696
697    DPRINTF(O3CPU, "[tid:%i]: Calling activate thread.\n", tid);
698
699    if (isActive == activeThreads.end()) {
700        DPRINTF(O3CPU, "[tid:%i]: Adding to active threads list\n",
701                tid);
702
703        activeThreads.push_back(tid);
704    }
705}
706
707template <class Impl>
708void
709FullO3CPU<Impl>::deactivateThread(ThreadID tid)
710{
711    //Remove From Active List, if Active
712    list<ThreadID>::iterator thread_it =
713        std::find(activeThreads.begin(), activeThreads.end(), tid);
714
715    DPRINTF(O3CPU, "[tid:%i]: Calling deactivate thread.\n", tid);
716
717    if (thread_it != activeThreads.end()) {
718        DPRINTF(O3CPU,"[tid:%i]: Removing from active threads list\n",
719                tid);
720        activeThreads.erase(thread_it);
721    }
722}
723
724template <class Impl>
725Counter
726FullO3CPU<Impl>::totalInstructions() const
727{
728    Counter total(0);
729
730    ThreadID size = thread.size();
731    for (ThreadID i = 0; i < size; i++)
732        total += thread[i]->numInst;
733
734    return total;
735}
736
737template <class Impl>
738void
739FullO3CPU<Impl>::activateContext(ThreadID tid, int delay)
740{
741    // Needs to set each stage to running as well.
742    if (delay){
743        DPRINTF(O3CPU, "[tid:%i]: Scheduling thread context to activate "
744                "on cycle %d\n", tid, curTick() + ticks(delay));
745        scheduleActivateThreadEvent(tid, delay);
746    } else {
747        activateThread(tid);
748    }
749
750    if (lastActivatedCycle < curTick()) {
751        scheduleTickEvent(delay);
752
753        // Be sure to signal that there's some activity so the CPU doesn't
754        // deschedule itself.
755        activityRec.activity();
756        fetch.wakeFromQuiesce();
757
758        quiesceCycles += tickToCycles((curTick() - 1) - lastRunningCycle);
759
760        lastActivatedCycle = curTick();
761
762        _status = Running;
763    }
764}
765
766template <class Impl>
767bool
768FullO3CPU<Impl>::deallocateContext(ThreadID tid, bool remove, int delay)
769{
770    // Schedule removal of thread data from CPU
771    if (delay){
772        DPRINTF(O3CPU, "[tid:%i]: Scheduling thread context to deallocate "
773                "on cycle %d\n", tid, curTick() + ticks(delay));
774        scheduleDeallocateContextEvent(tid, remove, delay);
775        return false;
776    } else {
777        deactivateThread(tid);
778        if (remove)
779            removeThread(tid);
780        return true;
781    }
782}
783
784template <class Impl>
785void
786FullO3CPU<Impl>::suspendContext(ThreadID tid)
787{
788    DPRINTF(O3CPU,"[tid: %i]: Suspending Thread Context.\n", tid);
789    bool deallocated = deallocateContext(tid, false, 1);
790    // If this was the last thread then unschedule the tick event.
791    if ((activeThreads.size() == 1 && !deallocated) ||
792        activeThreads.size() == 0)
793        unscheduleTickEvent();
794
795    DPRINTF(Quiesce, "Suspending Context\n");
796    lastRunningCycle = curTick();
797    _status = Idle;
798}
799
800template <class Impl>
801void
802FullO3CPU<Impl>::haltContext(ThreadID tid)
803{
804    //For now, this is the same as deallocate
805    DPRINTF(O3CPU,"[tid:%i]: Halt Context called. Deallocating", tid);
806    deallocateContext(tid, true, 1);
807}
808
809template <class Impl>
810void
811FullO3CPU<Impl>::insertThread(ThreadID tid)
812{
813    DPRINTF(O3CPU,"[tid:%i] Initializing thread into CPU");
814    // Will change now that the PC and thread state is internal to the CPU
815    // and not in the ThreadContext.
816#if FULL_SYSTEM
817    ThreadContext *src_tc = system->threadContexts[tid];
818#else
819    ThreadContext *src_tc = tcBase(tid);
820#endif
821
822    //Bind Int Regs to Rename Map
823    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
824        PhysRegIndex phys_reg = freeList.getIntReg();
825
826        renameMap[tid].setEntry(ireg,phys_reg);
827        scoreboard.setReg(phys_reg);
828    }
829
830    //Bind Float Regs to Rename Map
831    for (int freg = 0; freg < TheISA::NumFloatRegs; freg++) {
832        PhysRegIndex phys_reg = freeList.getFloatReg();
833
834        renameMap[tid].setEntry(freg,phys_reg);
835        scoreboard.setReg(phys_reg);
836    }
837
838    //Copy Thread Data Into RegFile
839    //this->copyFromTC(tid);
840
841    //Set PC/NPC/NNPC
842    pcState(src_tc->pcState(), tid);
843
844    src_tc->setStatus(ThreadContext::Active);
845
846    activateContext(tid,1);
847
848    //Reset ROB/IQ/LSQ Entries
849    commit.rob->resetEntries();
850    iew.resetEntries();
851}
852
853template <class Impl>
854void
855FullO3CPU<Impl>::removeThread(ThreadID tid)
856{
857    DPRINTF(O3CPU,"[tid:%i] Removing thread context from CPU.\n", tid);
858
859    // Copy Thread Data From RegFile
860    // If thread is suspended, it might be re-allocated
861    // this->copyToTC(tid);
862
863
864    // @todo: 2-27-2008: Fix how we free up rename mappings
865    // here to alleviate the case for double-freeing registers
866    // in SMT workloads.
867
868    // Unbind Int Regs from Rename Map
869    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
870        PhysRegIndex phys_reg = renameMap[tid].lookup(ireg);
871
872        scoreboard.unsetReg(phys_reg);
873        freeList.addReg(phys_reg);
874    }
875
876    // Unbind Float Regs from Rename Map
877    for (int freg = TheISA::NumIntRegs; freg < TheISA::NumFloatRegs; freg++) {
878        PhysRegIndex phys_reg = renameMap[tid].lookup(freg);
879
880        scoreboard.unsetReg(phys_reg);
881        freeList.addReg(phys_reg);
882    }
883
884    // Squash Throughout Pipeline
885    DynInstPtr inst = commit.rob->readHeadInst(tid);
886    InstSeqNum squash_seq_num = inst->seqNum;
887    fetch.squash(0, squash_seq_num, inst, tid);
888    decode.squash(tid);
889    rename.squash(squash_seq_num, tid);
890    iew.squash(tid);
891    iew.ldstQueue.squash(squash_seq_num, tid);
892    commit.rob->squash(squash_seq_num, tid);
893
894
895    assert(iew.instQueue.getCount(tid) == 0);
896    assert(iew.ldstQueue.getCount(tid) == 0);
897
898    // Reset ROB/IQ/LSQ Entries
899
900    // Commented out for now.  This should be possible to do by
901    // telling all the pipeline stages to drain first, and then
902    // checking until the drain completes.  Once the pipeline is
903    // drained, call resetEntries(). - 10-09-06 ktlim
904/*
905    if (activeThreads.size() >= 1) {
906        commit.rob->resetEntries();
907        iew.resetEntries();
908    }
909*/
910}
911
912
913template <class Impl>
914void
915FullO3CPU<Impl>::activateWhenReady(ThreadID tid)
916{
917    DPRINTF(O3CPU,"[tid:%i]: Checking if resources are available for incoming"
918            "(e.g. PhysRegs/ROB/IQ/LSQ) \n",
919            tid);
920
921    bool ready = true;
922
923    if (freeList.numFreeIntRegs() >= TheISA::NumIntRegs) {
924        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
925                "Phys. Int. Regs.\n",
926                tid);
927        ready = false;
928    } else if (freeList.numFreeFloatRegs() >= TheISA::NumFloatRegs) {
929        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
930                "Phys. Float. Regs.\n",
931                tid);
932        ready = false;
933    } else if (commit.rob->numFreeEntries() >=
934               commit.rob->entryAmount(activeThreads.size() + 1)) {
935        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
936                "ROB entries.\n",
937                tid);
938        ready = false;
939    } else if (iew.instQueue.numFreeEntries() >=
940               iew.instQueue.entryAmount(activeThreads.size() + 1)) {
941        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
942                "IQ entries.\n",
943                tid);
944        ready = false;
945    } else if (iew.ldstQueue.numFreeEntries() >=
946               iew.ldstQueue.entryAmount(activeThreads.size() + 1)) {
947        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
948                "LSQ entries.\n",
949                tid);
950        ready = false;
951    }
952
953    if (ready) {
954        insertThread(tid);
955
956        contextSwitch = false;
957
958        cpuWaitList.remove(tid);
959    } else {
960        suspendContext(tid);
961
962        //blocks fetch
963        contextSwitch = true;
964
965        //@todo: dont always add to waitlist
966        //do waitlist
967        cpuWaitList.push_back(tid);
968    }
969}
970
971#if FULL_SYSTEM
972template <class Impl>
973Fault
974FullO3CPU<Impl>::hwrei(ThreadID tid)
975{
976#if THE_ISA == ALPHA_ISA
977    // Need to clear the lock flag upon returning from an interrupt.
978    this->setMiscRegNoEffect(AlphaISA::MISCREG_LOCKFLAG, false, tid);
979
980    this->thread[tid]->kernelStats->hwrei();
981
982    // FIXME: XXX check for interrupts? XXX
983#endif
984    return NoFault;
985}
986
987template <class Impl>
988bool
989FullO3CPU<Impl>::simPalCheck(int palFunc, ThreadID tid)
990{
991#if THE_ISA == ALPHA_ISA
992    if (this->thread[tid]->kernelStats)
993        this->thread[tid]->kernelStats->callpal(palFunc,
994                                                this->threadContexts[tid]);
995
996    switch (palFunc) {
997      case PAL::halt:
998        halt();
999        if (--System::numSystemsRunning == 0)
1000            exitSimLoop("all cpus halted");
1001        break;
1002
1003      case PAL::bpt:
1004      case PAL::bugchk:
1005        if (this->system->breakpoint())
1006            return false;
1007        break;
1008    }
1009#endif
1010    return true;
1011}
1012
1013template <class Impl>
1014Fault
1015FullO3CPU<Impl>::getInterrupts()
1016{
1017    // Check if there are any outstanding interrupts
1018    return this->interrupts->getInterrupt(this->threadContexts[0]);
1019}
1020
1021template <class Impl>
1022void
1023FullO3CPU<Impl>::processInterrupts(Fault interrupt)
1024{
1025    // Check for interrupts here.  For now can copy the code that
1026    // exists within isa_fullsys_traits.hh.  Also assume that thread 0
1027    // is the one that handles the interrupts.
1028    // @todo: Possibly consolidate the interrupt checking code.
1029    // @todo: Allow other threads to handle interrupts.
1030
1031    assert(interrupt != NoFault);
1032    this->interrupts->updateIntrInfo(this->threadContexts[0]);
1033
1034    DPRINTF(O3CPU, "Interrupt %s being handled\n", interrupt->name());
1035    this->trap(interrupt, 0, NULL);
1036}
1037
1038#endif
1039
1040template <class Impl>
1041void
1042FullO3CPU<Impl>::trap(Fault fault, ThreadID tid, StaticInstPtr inst)
1043{
1044    // Pass the thread's TC into the invoke method.
1045    fault->invoke(this->threadContexts[tid], inst);
1046}
1047
1048#if !FULL_SYSTEM
1049
1050template <class Impl>
1051void
1052FullO3CPU<Impl>::syscall(int64_t callnum, ThreadID tid)
1053{
1054    DPRINTF(O3CPU, "[tid:%i] Executing syscall().\n\n", tid);
1055
1056    DPRINTF(Activity,"Activity: syscall() called.\n");
1057
1058    // Temporarily increase this by one to account for the syscall
1059    // instruction.
1060    ++(this->thread[tid]->funcExeInst);
1061
1062    // Execute the actual syscall.
1063    this->thread[tid]->syscall(callnum);
1064
1065    // Decrease funcExeInst by one as the normal commit will handle
1066    // incrementing it.
1067    --(this->thread[tid]->funcExeInst);
1068}
1069
1070#endif
1071
1072template <class Impl>
1073void
1074FullO3CPU<Impl>::serialize(std::ostream &os)
1075{
1076    SimObject::State so_state = SimObject::getState();
1077    SERIALIZE_ENUM(so_state);
1078    BaseCPU::serialize(os);
1079    nameOut(os, csprintf("%s.tickEvent", name()));
1080    tickEvent.serialize(os);
1081
1082    // Use SimpleThread's ability to checkpoint to make it easier to
1083    // write out the registers.  Also make this static so it doesn't
1084    // get instantiated multiple times (causes a panic in statistics).
1085    static SimpleThread temp;
1086
1087    ThreadID size = thread.size();
1088    for (ThreadID i = 0; i < size; i++) {
1089        nameOut(os, csprintf("%s.xc.%i", name(), i));
1090        temp.copyTC(thread[i]->getTC());
1091        temp.serialize(os);
1092    }
1093}
1094
1095template <class Impl>
1096void
1097FullO3CPU<Impl>::unserialize(Checkpoint *cp, const std::string &section)
1098{
1099    SimObject::State so_state;
1100    UNSERIALIZE_ENUM(so_state);
1101    BaseCPU::unserialize(cp, section);
1102    tickEvent.unserialize(cp, csprintf("%s.tickEvent", section));
1103
1104    // Use SimpleThread's ability to checkpoint to make it easier to
1105    // read in the registers.  Also make this static so it doesn't
1106    // get instantiated multiple times (causes a panic in statistics).
1107    static SimpleThread temp;
1108
1109    ThreadID size = thread.size();
1110    for (ThreadID i = 0; i < size; i++) {
1111        temp.copyTC(thread[i]->getTC());
1112        temp.unserialize(cp, csprintf("%s.xc.%i", section, i));
1113        thread[i]->getTC()->copyArchRegs(temp.getTC());
1114    }
1115}
1116
1117template <class Impl>
1118unsigned int
1119FullO3CPU<Impl>::drain(Event *drain_event)
1120{
1121    DPRINTF(O3CPU, "Switching out\n");
1122
1123    // If the CPU isn't doing anything, then return immediately.
1124    if (_status == Idle || _status == SwitchedOut) {
1125        return 0;
1126    }
1127
1128    drainCount = 0;
1129    fetch.drain();
1130    decode.drain();
1131    rename.drain();
1132    iew.drain();
1133    commit.drain();
1134
1135    // Wake the CPU and record activity so everything can drain out if
1136    // the CPU was not able to immediately drain.
1137    if (getState() != SimObject::Drained) {
1138        // A bit of a hack...set the drainEvent after all the drain()
1139        // calls have been made, that way if all of the stages drain
1140        // immediately, the signalDrained() function knows not to call
1141        // process on the drain event.
1142        drainEvent = drain_event;
1143
1144        wakeCPU();
1145        activityRec.activity();
1146
1147        return 1;
1148    } else {
1149        return 0;
1150    }
1151}
1152
1153template <class Impl>
1154void
1155FullO3CPU<Impl>::resume()
1156{
1157    fetch.resume();
1158    decode.resume();
1159    rename.resume();
1160    iew.resume();
1161    commit.resume();
1162
1163    changeState(SimObject::Running);
1164
1165    if (_status == SwitchedOut || _status == Idle)
1166        return;
1167
1168    assert(system->getMemoryMode() == Enums::timing);
1169
1170    if (!tickEvent.scheduled())
1171        schedule(tickEvent, nextCycle());
1172    _status = Running;
1173}
1174
1175template <class Impl>
1176void
1177FullO3CPU<Impl>::signalDrained()
1178{
1179    if (++drainCount == NumStages) {
1180        if (tickEvent.scheduled())
1181            tickEvent.squash();
1182
1183        changeState(SimObject::Drained);
1184
1185        BaseCPU::switchOut();
1186
1187        if (drainEvent) {
1188            drainEvent->process();
1189            drainEvent = NULL;
1190        }
1191    }
1192    assert(drainCount <= 5);
1193}
1194
1195template <class Impl>
1196void
1197FullO3CPU<Impl>::switchOut()
1198{
1199    fetch.switchOut();
1200    rename.switchOut();
1201    iew.switchOut();
1202    commit.switchOut();
1203    instList.clear();
1204    while (!removeList.empty()) {
1205        removeList.pop();
1206    }
1207
1208    _status = SwitchedOut;
1209#if USE_CHECKER
1210    if (checker)
1211        checker->switchOut();
1212#endif
1213    if (tickEvent.scheduled())
1214        tickEvent.squash();
1215}
1216
1217template <class Impl>
1218void
1219FullO3CPU<Impl>::takeOverFrom(BaseCPU *oldCPU)
1220{
1221    // Flush out any old data from the time buffers.
1222    for (int i = 0; i < timeBuffer.getSize(); ++i) {
1223        timeBuffer.advance();
1224        fetchQueue.advance();
1225        decodeQueue.advance();
1226        renameQueue.advance();
1227        iewQueue.advance();
1228    }
1229
1230    activityRec.reset();
1231
1232    BaseCPU::takeOverFrom(oldCPU, &icachePort, &dcachePort);
1233
1234    fetch.takeOverFrom();
1235    decode.takeOverFrom();
1236    rename.takeOverFrom();
1237    iew.takeOverFrom();
1238    commit.takeOverFrom();
1239
1240    assert(!tickEvent.scheduled() || tickEvent.squashed());
1241
1242    // @todo: Figure out how to properly select the tid to put onto
1243    // the active threads list.
1244    ThreadID tid = 0;
1245
1246    list<ThreadID>::iterator isActive =
1247        std::find(activeThreads.begin(), activeThreads.end(), tid);
1248
1249    if (isActive == activeThreads.end()) {
1250        //May Need to Re-code this if the delay variable is the delay
1251        //needed for thread to activate
1252        DPRINTF(O3CPU, "Adding Thread %i to active threads list\n",
1253                tid);
1254
1255        activeThreads.push_back(tid);
1256    }
1257
1258    // Set all statuses to active, schedule the CPU's tick event.
1259    // @todo: Fix up statuses so this is handled properly
1260    ThreadID size = threadContexts.size();
1261    for (ThreadID i = 0; i < size; ++i) {
1262        ThreadContext *tc = threadContexts[i];
1263        if (tc->status() == ThreadContext::Active && _status != Running) {
1264            _status = Running;
1265            reschedule(tickEvent, nextCycle(), true);
1266        }
1267    }
1268    if (!tickEvent.scheduled())
1269        schedule(tickEvent, nextCycle());
1270
1271    lastRunningCycle = curTick();
1272}
1273
1274template <class Impl>
1275TheISA::MiscReg
1276FullO3CPU<Impl>::readMiscRegNoEffect(int misc_reg, ThreadID tid)
1277{
1278    return this->isa[tid].readMiscRegNoEffect(misc_reg);
1279}
1280
1281template <class Impl>
1282TheISA::MiscReg
1283FullO3CPU<Impl>::readMiscReg(int misc_reg, ThreadID tid)
1284{
1285    miscRegfileReads++;
1286    return this->isa[tid].readMiscReg(misc_reg, tcBase(tid));
1287}
1288
1289template <class Impl>
1290void
1291FullO3CPU<Impl>::setMiscRegNoEffect(int misc_reg,
1292        const TheISA::MiscReg &val, ThreadID tid)
1293{
1294    this->isa[tid].setMiscRegNoEffect(misc_reg, val);
1295}
1296
1297template <class Impl>
1298void
1299FullO3CPU<Impl>::setMiscReg(int misc_reg,
1300        const TheISA::MiscReg &val, ThreadID tid)
1301{
1302    miscRegfileWrites++;
1303    this->isa[tid].setMiscReg(misc_reg, val, tcBase(tid));
1304}
1305
1306template <class Impl>
1307uint64_t
1308FullO3CPU<Impl>::readIntReg(int reg_idx)
1309{
1310    intRegfileReads++;
1311    return regFile.readIntReg(reg_idx);
1312}
1313
1314template <class Impl>
1315FloatReg
1316FullO3CPU<Impl>::readFloatReg(int reg_idx)
1317{
1318    fpRegfileReads++;
1319    return regFile.readFloatReg(reg_idx);
1320}
1321
1322template <class Impl>
1323FloatRegBits
1324FullO3CPU<Impl>::readFloatRegBits(int reg_idx)
1325{
1326    fpRegfileReads++;
1327    return regFile.readFloatRegBits(reg_idx);
1328}
1329
1330template <class Impl>
1331void
1332FullO3CPU<Impl>::setIntReg(int reg_idx, uint64_t val)
1333{
1334    intRegfileWrites++;
1335    regFile.setIntReg(reg_idx, val);
1336}
1337
1338template <class Impl>
1339void
1340FullO3CPU<Impl>::setFloatReg(int reg_idx, FloatReg val)
1341{
1342    fpRegfileWrites++;
1343    regFile.setFloatReg(reg_idx, val);
1344}
1345
1346template <class Impl>
1347void
1348FullO3CPU<Impl>::setFloatRegBits(int reg_idx, FloatRegBits val)
1349{
1350    fpRegfileWrites++;
1351    regFile.setFloatRegBits(reg_idx, val);
1352}
1353
1354template <class Impl>
1355uint64_t
1356FullO3CPU<Impl>::readArchIntReg(int reg_idx, ThreadID tid)
1357{
1358    intRegfileReads++;
1359    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
1360
1361    return regFile.readIntReg(phys_reg);
1362}
1363
1364template <class Impl>
1365float
1366FullO3CPU<Impl>::readArchFloatReg(int reg_idx, ThreadID tid)
1367{
1368    fpRegfileReads++;
1369    int idx = reg_idx + TheISA::NumIntRegs;
1370    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
1371
1372    return regFile.readFloatReg(phys_reg);
1373}
1374
1375template <class Impl>
1376uint64_t
1377FullO3CPU<Impl>::readArchFloatRegInt(int reg_idx, ThreadID tid)
1378{
1379    fpRegfileReads++;
1380    int idx = reg_idx + TheISA::NumIntRegs;
1381    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
1382
1383    return regFile.readFloatRegBits(phys_reg);
1384}
1385
1386template <class Impl>
1387void
1388FullO3CPU<Impl>::setArchIntReg(int reg_idx, uint64_t val, ThreadID tid)
1389{
1390    intRegfileWrites++;
1391    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
1392
1393    regFile.setIntReg(phys_reg, val);
1394}
1395
1396template <class Impl>
1397void
1398FullO3CPU<Impl>::setArchFloatReg(int reg_idx, float val, ThreadID tid)
1399{
1400    fpRegfileWrites++;
1401    int idx = reg_idx + TheISA::NumIntRegs;
1402    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
1403
1404    regFile.setFloatReg(phys_reg, val);
1405}
1406
1407template <class Impl>
1408void
1409FullO3CPU<Impl>::setArchFloatRegInt(int reg_idx, uint64_t val, ThreadID tid)
1410{
1411    fpRegfileWrites++;
1412    int idx = reg_idx + TheISA::NumIntRegs;
1413    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
1414
1415    regFile.setFloatRegBits(phys_reg, val);
1416}
1417
1418template <class Impl>
1419TheISA::PCState
1420FullO3CPU<Impl>::pcState(ThreadID tid)
1421{
1422    return commit.pcState(tid);
1423}
1424
1425template <class Impl>
1426void
1427FullO3CPU<Impl>::pcState(const TheISA::PCState &val, ThreadID tid)
1428{
1429    commit.pcState(val, tid);
1430}
1431
1432template <class Impl>
1433Addr
1434FullO3CPU<Impl>::instAddr(ThreadID tid)
1435{
1436    return commit.instAddr(tid);
1437}
1438
1439template <class Impl>
1440Addr
1441FullO3CPU<Impl>::nextInstAddr(ThreadID tid)
1442{
1443    return commit.nextInstAddr(tid);
1444}
1445
1446template <class Impl>
1447MicroPC
1448FullO3CPU<Impl>::microPC(ThreadID tid)
1449{
1450    return commit.microPC(tid);
1451}
1452
1453template <class Impl>
1454void
1455FullO3CPU<Impl>::squashFromTC(ThreadID tid)
1456{
1457    this->thread[tid]->inSyscall = true;
1458    this->commit.generateTCEvent(tid);
1459}
1460
1461template <class Impl>
1462typename FullO3CPU<Impl>::ListIt
1463FullO3CPU<Impl>::addInst(DynInstPtr &inst)
1464{
1465    instList.push_back(inst);
1466
1467    return --(instList.end());
1468}
1469
1470template <class Impl>
1471void
1472FullO3CPU<Impl>::instDone(ThreadID tid)
1473{
1474    // Keep an instruction count.
1475    thread[tid]->numInst++;
1476    thread[tid]->numInsts++;
1477    committedInsts[tid]++;
1478    totalCommittedInsts++;
1479    system->totalNumInsts++;
1480    // Check for instruction-count-based events.
1481    comInstEventQueue[tid]->serviceEvents(thread[tid]->numInst);
1482    system->instEventQueue.serviceEvents(system->totalNumInsts);
1483}
1484
1485template <class Impl>
1486void
1487FullO3CPU<Impl>::removeFrontInst(DynInstPtr &inst)
1488{
1489    DPRINTF(O3CPU, "Removing committed instruction [tid:%i] PC %s "
1490            "[sn:%lli]\n",
1491            inst->threadNumber, inst->pcState(), inst->seqNum);
1492
1493    removeInstsThisCycle = true;
1494
1495    // Remove the front instruction.
1496    removeList.push(inst->getInstListIt());
1497}
1498
1499template <class Impl>
1500void
1501FullO3CPU<Impl>::removeInstsNotInROB(ThreadID tid)
1502{
1503    DPRINTF(O3CPU, "Thread %i: Deleting instructions from instruction"
1504            " list.\n", tid);
1505
1506    ListIt end_it;
1507
1508    bool rob_empty = false;
1509
1510    if (instList.empty()) {
1511        return;
1512    } else if (rob.isEmpty(/*tid*/)) {
1513        DPRINTF(O3CPU, "ROB is empty, squashing all insts.\n");
1514        end_it = instList.begin();
1515        rob_empty = true;
1516    } else {
1517        end_it = (rob.readTailInst(tid))->getInstListIt();
1518        DPRINTF(O3CPU, "ROB is not empty, squashing insts not in ROB.\n");
1519    }
1520
1521    removeInstsThisCycle = true;
1522
1523    ListIt inst_it = instList.end();
1524
1525    inst_it--;
1526
1527    // Walk through the instruction list, removing any instructions
1528    // that were inserted after the given instruction iterator, end_it.
1529    while (inst_it != end_it) {
1530        assert(!instList.empty());
1531
1532        squashInstIt(inst_it, tid);
1533
1534        inst_it--;
1535    }
1536
1537    // If the ROB was empty, then we actually need to remove the first
1538    // instruction as well.
1539    if (rob_empty) {
1540        squashInstIt(inst_it, tid);
1541    }
1542}
1543
1544template <class Impl>
1545void
1546FullO3CPU<Impl>::removeInstsUntil(const InstSeqNum &seq_num, ThreadID tid)
1547{
1548    assert(!instList.empty());
1549
1550    removeInstsThisCycle = true;
1551
1552    ListIt inst_iter = instList.end();
1553
1554    inst_iter--;
1555
1556    DPRINTF(O3CPU, "Deleting instructions from instruction "
1557            "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1558            tid, seq_num, (*inst_iter)->seqNum);
1559
1560    while ((*inst_iter)->seqNum > seq_num) {
1561
1562        bool break_loop = (inst_iter == instList.begin());
1563
1564        squashInstIt(inst_iter, tid);
1565
1566        inst_iter--;
1567
1568        if (break_loop)
1569            break;
1570    }
1571}
1572
1573template <class Impl>
1574inline void
1575FullO3CPU<Impl>::squashInstIt(const ListIt &instIt, ThreadID tid)
1576{
1577    if ((*instIt)->threadNumber == tid) {
1578        DPRINTF(O3CPU, "Squashing instruction, "
1579                "[tid:%i] [sn:%lli] PC %s\n",
1580                (*instIt)->threadNumber,
1581                (*instIt)->seqNum,
1582                (*instIt)->pcState());
1583
1584        // Mark it as squashed.
1585        (*instIt)->setSquashed();
1586
1587        // @todo: Formulate a consistent method for deleting
1588        // instructions from the instruction list
1589        // Remove the instruction from the list.
1590        removeList.push(instIt);
1591    }
1592}
1593
1594template <class Impl>
1595void
1596FullO3CPU<Impl>::cleanUpRemovedInsts()
1597{
1598    while (!removeList.empty()) {
1599        DPRINTF(O3CPU, "Removing instruction, "
1600                "[tid:%i] [sn:%lli] PC %s\n",
1601                (*removeList.front())->threadNumber,
1602                (*removeList.front())->seqNum,
1603                (*removeList.front())->pcState());
1604
1605        instList.erase(removeList.front());
1606
1607        removeList.pop();
1608    }
1609
1610    removeInstsThisCycle = false;
1611}
1612/*
1613template <class Impl>
1614void
1615FullO3CPU<Impl>::removeAllInsts()
1616{
1617    instList.clear();
1618}
1619*/
1620template <class Impl>
1621void
1622FullO3CPU<Impl>::dumpInsts()
1623{
1624    int num = 0;
1625
1626    ListIt inst_list_it = instList.begin();
1627
1628    cprintf("Dumping Instruction List\n");
1629
1630    while (inst_list_it != instList.end()) {
1631        cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1632                "Squashed:%i\n\n",
1633                num, (*inst_list_it)->instAddr(), (*inst_list_it)->threadNumber,
1634                (*inst_list_it)->seqNum, (*inst_list_it)->isIssued(),
1635                (*inst_list_it)->isSquashed());
1636        inst_list_it++;
1637        ++num;
1638    }
1639}
1640/*
1641template <class Impl>
1642void
1643FullO3CPU<Impl>::wakeDependents(DynInstPtr &inst)
1644{
1645    iew.wakeDependents(inst);
1646}
1647*/
1648template <class Impl>
1649void
1650FullO3CPU<Impl>::wakeCPU()
1651{
1652    if (activityRec.active() || tickEvent.scheduled()) {
1653        DPRINTF(Activity, "CPU already running.\n");
1654        return;
1655    }
1656
1657    DPRINTF(Activity, "Waking up CPU\n");
1658
1659    idleCycles += tickToCycles((curTick() - 1) - lastRunningCycle);
1660    numCycles += tickToCycles((curTick() - 1) - lastRunningCycle);
1661
1662    schedule(tickEvent, nextCycle());
1663}
1664
1665#if FULL_SYSTEM
1666template <class Impl>
1667void
1668FullO3CPU<Impl>::wakeup()
1669{
1670    if (this->thread[0]->status() != ThreadContext::Suspended)
1671        return;
1672
1673    this->wakeCPU();
1674
1675    DPRINTF(Quiesce, "Suspended Processor woken\n");
1676    this->threadContexts[0]->activate();
1677}
1678#endif
1679
1680template <class Impl>
1681ThreadID
1682FullO3CPU<Impl>::getFreeTid()
1683{
1684    for (ThreadID tid = 0; tid < numThreads; tid++) {
1685        if (!tids[tid]) {
1686            tids[tid] = true;
1687            return tid;
1688        }
1689    }
1690
1691    return InvalidThreadID;
1692}
1693
1694template <class Impl>
1695void
1696FullO3CPU<Impl>::doContextSwitch()
1697{
1698    if (contextSwitch) {
1699
1700        //ADD CODE TO DEACTIVE THREAD HERE (???)
1701
1702        ThreadID size = cpuWaitList.size();
1703        for (ThreadID tid = 0; tid < size; tid++) {
1704            activateWhenReady(tid);
1705        }
1706
1707        if (cpuWaitList.size() == 0)
1708            contextSwitch = true;
1709    }
1710}
1711
1712template <class Impl>
1713void
1714FullO3CPU<Impl>::updateThreadPriority()
1715{
1716    if (activeThreads.size() > 1) {
1717        //DEFAULT TO ROUND ROBIN SCHEME
1718        //e.g. Move highest priority to end of thread list
1719        list<ThreadID>::iterator list_begin = activeThreads.begin();
1720
1721        unsigned high_thread = *list_begin;
1722
1723        activeThreads.erase(list_begin);
1724
1725        activeThreads.push_back(high_thread);
1726    }
1727}
1728
1729// Forward declaration of FullO3CPU.
1730template class FullO3CPU<O3CPUImpl>;
1731