cpu.cc revision 10164:2d2c60bda8b2
1/*
2 * Copyright (c) 2011-2012 ARM Limited
3 * Copyright (c) 2013 Advanced Micro Devices, Inc.
4 * All rights reserved
5 *
6 * The license below extends only to copyright in the software and shall
7 * not be construed as granting a license to any other intellectual
8 * property including but not limited to intellectual property relating
9 * to a hardware implementation of the functionality of the software
10 * licensed hereunder.  You may use the software subject to the license
11 * terms below provided that you ensure that this notice is replicated
12 * unmodified and in its entirety in all distributions of the software,
13 * modified or unmodified, in source code or in binary form.
14 *
15 * Copyright (c) 2004-2006 The Regents of The University of Michigan
16 * Copyright (c) 2011 Regents of the University of California
17 * All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are
21 * met: redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer;
23 * redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution;
26 * neither the name of the copyright holders nor the names of its
27 * contributors may be used to endorse or promote products derived from
28 * this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Authors: Kevin Lim
43 *          Korey Sewell
44 *          Rick Strong
45 */
46
47#include "arch/kernel_stats.hh"
48#include "config/the_isa.hh"
49#include "cpu/checker/cpu.hh"
50#include "cpu/checker/thread_context.hh"
51#include "cpu/o3/cpu.hh"
52#include "cpu/o3/isa_specific.hh"
53#include "cpu/o3/thread_context.hh"
54#include "cpu/activity.hh"
55#include "cpu/quiesce_event.hh"
56#include "cpu/simple_thread.hh"
57#include "cpu/thread_context.hh"
58#include "debug/Activity.hh"
59#include "debug/Drain.hh"
60#include "debug/O3CPU.hh"
61#include "debug/Quiesce.hh"
62#include "enums/MemoryMode.hh"
63#include "sim/core.hh"
64#include "sim/full_system.hh"
65#include "sim/process.hh"
66#include "sim/stat_control.hh"
67#include "sim/system.hh"
68
69#if THE_ISA == ALPHA_ISA
70#include "arch/alpha/osfpal.hh"
71#include "debug/Activity.hh"
72#endif
73
74struct BaseCPUParams;
75
76using namespace TheISA;
77using namespace std;
78
79BaseO3CPU::BaseO3CPU(BaseCPUParams *params)
80    : BaseCPU(params)
81{
82}
83
84void
85BaseO3CPU::regStats()
86{
87    BaseCPU::regStats();
88}
89
90template<class Impl>
91bool
92FullO3CPU<Impl>::IcachePort::recvTimingResp(PacketPtr pkt)
93{
94    DPRINTF(O3CPU, "Fetch unit received timing\n");
95    // We shouldn't ever get a block in ownership state
96    assert(!(pkt->memInhibitAsserted() && !pkt->sharedAsserted()));
97    fetch->processCacheCompletion(pkt);
98
99    return true;
100}
101
102template<class Impl>
103void
104FullO3CPU<Impl>::IcachePort::recvRetry()
105{
106    fetch->recvRetry();
107}
108
109template <class Impl>
110bool
111FullO3CPU<Impl>::DcachePort::recvTimingResp(PacketPtr pkt)
112{
113    return lsq->recvTimingResp(pkt);
114}
115
116template <class Impl>
117void
118FullO3CPU<Impl>::DcachePort::recvTimingSnoopReq(PacketPtr pkt)
119{
120    lsq->recvTimingSnoopReq(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(params->numPhysIntRegs,
228              params->numPhysFloatRegs,
229              params->numPhysCCRegs),
230
231      freeList(name() + ".freelist", &regFile),
232
233      rob(this, params),
234
235      scoreboard(name() + ".scoreboard",
236                 regFile.totalNumPhysRegs(), TheISA::NumMiscRegs,
237                 TheISA::ZeroReg, TheISA::ZeroReg),
238
239      isa(numThreads, NULL),
240
241      icachePort(&fetch, this),
242      dcachePort(&iew.ldstQueue, this),
243
244      timeBuffer(params->backComSize, params->forwardComSize),
245      fetchQueue(params->backComSize, params->forwardComSize),
246      decodeQueue(params->backComSize, params->forwardComSize),
247      renameQueue(params->backComSize, params->forwardComSize),
248      iewQueue(params->backComSize, params->forwardComSize),
249      activityRec(name(), NumStages,
250                  params->backComSize + params->forwardComSize,
251                  params->activity),
252
253      globalSeqNum(1),
254      system(params->system),
255      drainManager(NULL),
256      lastRunningCycle(curCycle())
257{
258    if (!params->switched_out) {
259        _status = Running;
260    } else {
261        _status = SwitchedOut;
262    }
263
264    if (params->checker) {
265        BaseCPU *temp_checker = params->checker;
266        checker = dynamic_cast<Checker<Impl> *>(temp_checker);
267        checker->setIcachePort(&icachePort);
268        checker->setSystem(params->system);
269    } else {
270        checker = NULL;
271    }
272
273    if (!FullSystem) {
274        thread.resize(numThreads);
275        tids.resize(numThreads);
276    }
277
278    // The stages also need their CPU pointer setup.  However this
279    // must be done at the upper level CPU because they have pointers
280    // to the upper level CPU, and not this FullO3CPU.
281
282    // Set up Pointers to the activeThreads list for each stage
283    fetch.setActiveThreads(&activeThreads);
284    decode.setActiveThreads(&activeThreads);
285    rename.setActiveThreads(&activeThreads);
286    iew.setActiveThreads(&activeThreads);
287    commit.setActiveThreads(&activeThreads);
288
289    // Give each of the stages the time buffer they will use.
290    fetch.setTimeBuffer(&timeBuffer);
291    decode.setTimeBuffer(&timeBuffer);
292    rename.setTimeBuffer(&timeBuffer);
293    iew.setTimeBuffer(&timeBuffer);
294    commit.setTimeBuffer(&timeBuffer);
295
296    // Also setup each of the stages' queues.
297    fetch.setFetchQueue(&fetchQueue);
298    decode.setFetchQueue(&fetchQueue);
299    commit.setFetchQueue(&fetchQueue);
300    decode.setDecodeQueue(&decodeQueue);
301    rename.setDecodeQueue(&decodeQueue);
302    rename.setRenameQueue(&renameQueue);
303    iew.setRenameQueue(&renameQueue);
304    iew.setIEWQueue(&iewQueue);
305    commit.setIEWQueue(&iewQueue);
306    commit.setRenameQueue(&renameQueue);
307
308    commit.setIEWStage(&iew);
309    rename.setIEWStage(&iew);
310    rename.setCommitStage(&commit);
311
312    ThreadID active_threads;
313    if (FullSystem) {
314        active_threads = 1;
315    } else {
316        active_threads = params->workload.size();
317
318        if (active_threads > Impl::MaxThreads) {
319            panic("Workload Size too large. Increase the 'MaxThreads' "
320                  "constant in your O3CPU impl. file (e.g. o3/alpha/impl.hh) "
321                  "or edit your workload size.");
322        }
323    }
324
325    //Make Sure That this a Valid Architeture
326    assert(params->numPhysIntRegs   >= numThreads * TheISA::NumIntRegs);
327    assert(params->numPhysFloatRegs >= numThreads * TheISA::NumFloatRegs);
328    assert(params->numPhysCCRegs >= numThreads * TheISA::NumCCRegs);
329
330    rename.setScoreboard(&scoreboard);
331    iew.setScoreboard(&scoreboard);
332
333    // Setup the rename map for whichever stages need it.
334    for (ThreadID tid = 0; tid < numThreads; tid++) {
335        isa[tid] = params->isa[tid];
336
337        // Only Alpha has an FP zero register, so for other ISAs we
338        // use an invalid FP register index to avoid special treatment
339        // of any valid FP reg.
340        RegIndex invalidFPReg = TheISA::NumFloatRegs + 1;
341        RegIndex fpZeroReg =
342            (THE_ISA == ALPHA_ISA) ? TheISA::ZeroReg : invalidFPReg;
343
344        commitRenameMap[tid].init(&regFile, TheISA::ZeroReg, fpZeroReg,
345                                  &freeList);
346
347        renameMap[tid].init(&regFile, TheISA::ZeroReg, fpZeroReg,
348                            &freeList);
349
350        activateThreadEvent[tid].init(tid, this);
351        deallocateContextEvent[tid].init(tid, this);
352    }
353
354    // Initialize rename map to assign physical registers to the
355    // architectural registers for active threads only.
356    for (ThreadID tid = 0; tid < active_threads; tid++) {
357        for (RegIndex ridx = 0; ridx < TheISA::NumIntRegs; ++ridx) {
358            // Note that we can't use the rename() method because we don't
359            // want special treatment for the zero register at this point
360            PhysRegIndex phys_reg = freeList.getIntReg();
361            renameMap[tid].setIntEntry(ridx, phys_reg);
362            commitRenameMap[tid].setIntEntry(ridx, phys_reg);
363        }
364
365        for (RegIndex ridx = 0; ridx < TheISA::NumFloatRegs; ++ridx) {
366            PhysRegIndex phys_reg = freeList.getFloatReg();
367            renameMap[tid].setFloatEntry(ridx, phys_reg);
368            commitRenameMap[tid].setFloatEntry(ridx, phys_reg);
369        }
370
371        for (RegIndex ridx = 0; ridx < TheISA::NumCCRegs; ++ridx) {
372            PhysRegIndex phys_reg = freeList.getCCReg();
373            renameMap[tid].setCCEntry(ridx, phys_reg);
374            commitRenameMap[tid].setCCEntry(ridx, phys_reg);
375        }
376    }
377
378    rename.setRenameMap(renameMap);
379    commit.setRenameMap(commitRenameMap);
380    rename.setFreeList(&freeList);
381
382    // Setup the ROB for whichever stages need it.
383    commit.setROB(&rob);
384
385    lastActivatedCycle = 0;
386#if 0
387    // Give renameMap & rename stage access to the freeList;
388    for (ThreadID tid = 0; tid < numThreads; tid++)
389        globalSeqNum[tid] = 1;
390#endif
391
392    contextSwitch = false;
393    DPRINTF(O3CPU, "Creating O3CPU object.\n");
394
395    // Setup any thread state.
396    this->thread.resize(this->numThreads);
397
398    for (ThreadID tid = 0; tid < this->numThreads; ++tid) {
399        if (FullSystem) {
400            // SMT is not supported in FS mode yet.
401            assert(this->numThreads == 1);
402            this->thread[tid] = new Thread(this, 0, NULL);
403        } else {
404            if (tid < params->workload.size()) {
405                DPRINTF(O3CPU, "Workload[%i] process is %#x",
406                        tid, this->thread[tid]);
407                this->thread[tid] = new typename FullO3CPU<Impl>::Thread(
408                        (typename Impl::O3CPU *)(this),
409                        tid, params->workload[tid]);
410
411                //usedTids[tid] = true;
412                //threadMap[tid] = tid;
413            } else {
414                //Allocate Empty thread so M5 can use later
415                //when scheduling threads to CPU
416                Process* dummy_proc = NULL;
417
418                this->thread[tid] = new typename FullO3CPU<Impl>::Thread(
419                        (typename Impl::O3CPU *)(this),
420                        tid, dummy_proc);
421                //usedTids[tid] = false;
422            }
423        }
424
425        ThreadContext *tc;
426
427        // Setup the TC that will serve as the interface to the threads/CPU.
428        O3ThreadContext<Impl> *o3_tc = new O3ThreadContext<Impl>;
429
430        tc = o3_tc;
431
432        // If we're using a checker, then the TC should be the
433        // CheckerThreadContext.
434        if (params->checker) {
435            tc = new CheckerThreadContext<O3ThreadContext<Impl> >(
436                o3_tc, this->checker);
437        }
438
439        o3_tc->cpu = (typename Impl::O3CPU *)(this);
440        assert(o3_tc->cpu);
441        o3_tc->thread = this->thread[tid];
442
443        if (FullSystem) {
444            // Setup quiesce event.
445            this->thread[tid]->quiesceEvent = new EndQuiesceEvent(tc);
446        }
447        // Give the thread the TC.
448        this->thread[tid]->tc = tc;
449
450        // Add the TC to the CPU's list of TC's.
451        this->threadContexts.push_back(tc);
452    }
453
454    // FullO3CPU always requires an interrupt controller.
455    if (!params->switched_out && !interrupts) {
456        fatal("FullO3CPU %s has no interrupt controller.\n"
457              "Ensure createInterruptController() is called.\n", name());
458    }
459
460    for (ThreadID tid = 0; tid < this->numThreads; tid++)
461        this->thread[tid]->setFuncExeInst(0);
462}
463
464template <class Impl>
465FullO3CPU<Impl>::~FullO3CPU()
466{
467}
468
469template <class Impl>
470void
471FullO3CPU<Impl>::regProbePoints()
472{
473    ppInstAccessComplete = new ProbePointArg<PacketPtr>(getProbeManager(), "InstAccessComplete");
474    ppDataAccessComplete = new ProbePointArg<std::pair<DynInstPtr, PacketPtr> >(getProbeManager(), "DataAccessComplete");
475    fetch.regProbePoints();
476    iew.regProbePoints();
477    commit.regProbePoints();
478}
479
480template <class Impl>
481void
482FullO3CPU<Impl>::regStats()
483{
484    BaseO3CPU::regStats();
485
486    // Register any of the O3CPU's stats here.
487    timesIdled
488        .name(name() + ".timesIdled")
489        .desc("Number of times that the entire CPU went into an idle state and"
490              " unscheduled itself")
491        .prereq(timesIdled);
492
493    idleCycles
494        .name(name() + ".idleCycles")
495        .desc("Total number of cycles that the CPU has spent unscheduled due "
496              "to idling")
497        .prereq(idleCycles);
498
499    quiesceCycles
500        .name(name() + ".quiesceCycles")
501        .desc("Total number of cycles that CPU has spent quiesced or waiting "
502              "for an interrupt")
503        .prereq(quiesceCycles);
504
505    // Number of Instructions simulated
506    // --------------------------------
507    // Should probably be in Base CPU but need templated
508    // MaxThreads so put in here instead
509    committedInsts
510        .init(numThreads)
511        .name(name() + ".committedInsts")
512        .desc("Number of Instructions Simulated");
513
514    committedOps
515        .init(numThreads)
516        .name(name() + ".committedOps")
517        .desc("Number of Ops (including micro ops) Simulated");
518
519    totalCommittedInsts
520        .name(name() + ".committedInsts_total")
521        .desc("Number of Instructions Simulated");
522
523    cpi
524        .name(name() + ".cpi")
525        .desc("CPI: Cycles Per Instruction")
526        .precision(6);
527    cpi = numCycles / committedInsts;
528
529    totalCpi
530        .name(name() + ".cpi_total")
531        .desc("CPI: Total CPI of All Threads")
532        .precision(6);
533    totalCpi = numCycles / totalCommittedInsts;
534
535    ipc
536        .name(name() + ".ipc")
537        .desc("IPC: Instructions Per Cycle")
538        .precision(6);
539    ipc =  committedInsts / numCycles;
540
541    totalIpc
542        .name(name() + ".ipc_total")
543        .desc("IPC: Total IPC of All Threads")
544        .precision(6);
545    totalIpc =  totalCommittedInsts / numCycles;
546
547    this->fetch.regStats();
548    this->decode.regStats();
549    this->rename.regStats();
550    this->iew.regStats();
551    this->commit.regStats();
552    this->rob.regStats();
553
554    intRegfileReads
555        .name(name() + ".int_regfile_reads")
556        .desc("number of integer regfile reads")
557        .prereq(intRegfileReads);
558
559    intRegfileWrites
560        .name(name() + ".int_regfile_writes")
561        .desc("number of integer regfile writes")
562        .prereq(intRegfileWrites);
563
564    fpRegfileReads
565        .name(name() + ".fp_regfile_reads")
566        .desc("number of floating regfile reads")
567        .prereq(fpRegfileReads);
568
569    fpRegfileWrites
570        .name(name() + ".fp_regfile_writes")
571        .desc("number of floating regfile writes")
572        .prereq(fpRegfileWrites);
573
574    ccRegfileReads
575        .name(name() + ".cc_regfile_reads")
576        .desc("number of cc regfile reads")
577        .prereq(ccRegfileReads);
578
579    ccRegfileWrites
580        .name(name() + ".cc_regfile_writes")
581        .desc("number of cc regfile writes")
582        .prereq(ccRegfileWrites);
583
584    miscRegfileReads
585        .name(name() + ".misc_regfile_reads")
586        .desc("number of misc regfile reads")
587        .prereq(miscRegfileReads);
588
589    miscRegfileWrites
590        .name(name() + ".misc_regfile_writes")
591        .desc("number of misc regfile writes")
592        .prereq(miscRegfileWrites);
593}
594
595template <class Impl>
596void
597FullO3CPU<Impl>::tick()
598{
599    DPRINTF(O3CPU, "\n\nFullO3CPU: Ticking main, FullO3CPU.\n");
600    assert(!switchedOut());
601    assert(getDrainState() != Drainable::Drained);
602
603    ++numCycles;
604
605//    activity = false;
606
607    //Tick each of the stages
608    fetch.tick();
609
610    decode.tick();
611
612    rename.tick();
613
614    iew.tick();
615
616    commit.tick();
617
618    if (!FullSystem)
619        doContextSwitch();
620
621    // Now advance the time buffers
622    timeBuffer.advance();
623
624    fetchQueue.advance();
625    decodeQueue.advance();
626    renameQueue.advance();
627    iewQueue.advance();
628
629    activityRec.advance();
630
631    if (removeInstsThisCycle) {
632        cleanUpRemovedInsts();
633    }
634
635    if (!tickEvent.scheduled()) {
636        if (_status == SwitchedOut) {
637            DPRINTF(O3CPU, "Switched out!\n");
638            // increment stat
639            lastRunningCycle = curCycle();
640        } else if (!activityRec.active() || _status == Idle) {
641            DPRINTF(O3CPU, "Idle!\n");
642            lastRunningCycle = curCycle();
643            timesIdled++;
644        } else {
645            schedule(tickEvent, clockEdge(Cycles(1)));
646            DPRINTF(O3CPU, "Scheduling next tick!\n");
647        }
648    }
649
650    if (!FullSystem)
651        updateThreadPriority();
652
653    tryDrain();
654}
655
656template <class Impl>
657void
658FullO3CPU<Impl>::init()
659{
660    BaseCPU::init();
661
662    for (ThreadID tid = 0; tid < numThreads; ++tid) {
663        // Set noSquashFromTC so that the CPU doesn't squash when initially
664        // setting up registers.
665        thread[tid]->noSquashFromTC = true;
666        // Initialise the ThreadContext's memory proxies
667        thread[tid]->initMemProxies(thread[tid]->getTC());
668    }
669
670    if (FullSystem && !params()->switched_out) {
671        for (ThreadID tid = 0; tid < numThreads; tid++) {
672            ThreadContext *src_tc = threadContexts[tid];
673            TheISA::initCPU(src_tc, src_tc->contextId());
674        }
675    }
676
677    // Clear noSquashFromTC.
678    for (int tid = 0; tid < numThreads; ++tid)
679        thread[tid]->noSquashFromTC = false;
680
681    commit.setThreads(thread);
682}
683
684template <class Impl>
685void
686FullO3CPU<Impl>::startup()
687{
688    BaseCPU::startup();
689    for (int tid = 0; tid < numThreads; ++tid)
690        isa[tid]->startup(threadContexts[tid]);
691
692    fetch.startupStage();
693    decode.startupStage();
694    iew.startupStage();
695    rename.startupStage();
696    commit.startupStage();
697}
698
699template <class Impl>
700void
701FullO3CPU<Impl>::activateThread(ThreadID tid)
702{
703    list<ThreadID>::iterator isActive =
704        std::find(activeThreads.begin(), activeThreads.end(), tid);
705
706    DPRINTF(O3CPU, "[tid:%i]: Calling activate thread.\n", tid);
707    assert(!switchedOut());
708
709    if (isActive == activeThreads.end()) {
710        DPRINTF(O3CPU, "[tid:%i]: Adding to active threads list\n",
711                tid);
712
713        activeThreads.push_back(tid);
714    }
715}
716
717template <class Impl>
718void
719FullO3CPU<Impl>::deactivateThread(ThreadID tid)
720{
721    //Remove From Active List, if Active
722    list<ThreadID>::iterator thread_it =
723        std::find(activeThreads.begin(), activeThreads.end(), tid);
724
725    DPRINTF(O3CPU, "[tid:%i]: Calling deactivate thread.\n", tid);
726    assert(!switchedOut());
727
728    if (thread_it != activeThreads.end()) {
729        DPRINTF(O3CPU,"[tid:%i]: Removing from active threads list\n",
730                tid);
731        activeThreads.erase(thread_it);
732    }
733}
734
735template <class Impl>
736Counter
737FullO3CPU<Impl>::totalInsts() const
738{
739    Counter total(0);
740
741    ThreadID size = thread.size();
742    for (ThreadID i = 0; i < size; i++)
743        total += thread[i]->numInst;
744
745    return total;
746}
747
748template <class Impl>
749Counter
750FullO3CPU<Impl>::totalOps() const
751{
752    Counter total(0);
753
754    ThreadID size = thread.size();
755    for (ThreadID i = 0; i < size; i++)
756        total += thread[i]->numOp;
757
758    return total;
759}
760
761template <class Impl>
762void
763FullO3CPU<Impl>::activateContext(ThreadID tid, Cycles delay)
764{
765    assert(!switchedOut());
766
767    // Needs to set each stage to running as well.
768    if (delay){
769        DPRINTF(O3CPU, "[tid:%i]: Scheduling thread context to activate "
770                "on cycle %d\n", tid, clockEdge(delay));
771        scheduleActivateThreadEvent(tid, delay);
772    } else {
773        activateThread(tid);
774    }
775
776    // We don't want to wake the CPU if it is drained. In that case,
777    // we just want to flag the thread as active and schedule the tick
778    // event from drainResume() instead.
779    if (getDrainState() == Drainable::Drained)
780        return;
781
782    // If we are time 0 or if the last activation time is in the past,
783    // schedule the next tick and wake up the fetch unit
784    if (lastActivatedCycle == 0 || lastActivatedCycle < curTick()) {
785        scheduleTickEvent(delay);
786
787        // Be sure to signal that there's some activity so the CPU doesn't
788        // deschedule itself.
789        activityRec.activity();
790        fetch.wakeFromQuiesce();
791
792        Cycles cycles(curCycle() - lastRunningCycle);
793        // @todo: This is an oddity that is only here to match the stats
794        if (cycles != 0)
795            --cycles;
796        quiesceCycles += cycles;
797
798        lastActivatedCycle = curTick();
799
800        _status = Running;
801    }
802}
803
804template <class Impl>
805bool
806FullO3CPU<Impl>::scheduleDeallocateContext(ThreadID tid, bool remove,
807                                           Cycles delay)
808{
809    // Schedule removal of thread data from CPU
810    if (delay){
811        DPRINTF(O3CPU, "[tid:%i]: Scheduling thread context to deallocate "
812                "on tick %d\n", tid, clockEdge(delay));
813        scheduleDeallocateContextEvent(tid, remove, delay);
814        return false;
815    } else {
816        deactivateThread(tid);
817        if (remove)
818            removeThread(tid);
819        return true;
820    }
821}
822
823template <class Impl>
824void
825FullO3CPU<Impl>::suspendContext(ThreadID tid)
826{
827    DPRINTF(O3CPU,"[tid: %i]: Suspending Thread Context.\n", tid);
828    assert(!switchedOut());
829    bool deallocated = scheduleDeallocateContext(tid, false, Cycles(1));
830    // If this was the last thread then unschedule the tick event.
831    if ((activeThreads.size() == 1 && !deallocated) ||
832        activeThreads.size() == 0)
833        unscheduleTickEvent();
834
835    DPRINTF(Quiesce, "Suspending Context\n");
836    lastRunningCycle = curCycle();
837    _status = Idle;
838}
839
840template <class Impl>
841void
842FullO3CPU<Impl>::haltContext(ThreadID tid)
843{
844    //For now, this is the same as deallocate
845    DPRINTF(O3CPU,"[tid:%i]: Halt Context called. Deallocating", tid);
846    assert(!switchedOut());
847    scheduleDeallocateContext(tid, true, Cycles(1));
848}
849
850template <class Impl>
851void
852FullO3CPU<Impl>::insertThread(ThreadID tid)
853{
854    DPRINTF(O3CPU,"[tid:%i] Initializing thread into CPU");
855    // Will change now that the PC and thread state is internal to the CPU
856    // and not in the ThreadContext.
857    ThreadContext *src_tc;
858    if (FullSystem)
859        src_tc = system->threadContexts[tid];
860    else
861        src_tc = tcBase(tid);
862
863    //Bind Int Regs to Rename Map
864    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
865        PhysRegIndex phys_reg = freeList.getIntReg();
866
867        renameMap[tid].setEntry(ireg,phys_reg);
868        scoreboard.setReg(phys_reg);
869    }
870
871    //Bind Float Regs to Rename Map
872    int max_reg = TheISA::NumIntRegs + TheISA::NumFloatRegs;
873    for (int freg = TheISA::NumIntRegs; freg < max_reg; freg++) {
874        PhysRegIndex phys_reg = freeList.getFloatReg();
875
876        renameMap[tid].setEntry(freg,phys_reg);
877        scoreboard.setReg(phys_reg);
878    }
879
880    //Bind condition-code Regs to Rename Map
881    max_reg = TheISA::NumIntRegs + TheISA::NumFloatRegs + TheISA::NumCCRegs;
882    for (int creg = TheISA::NumIntRegs + TheISA::NumFloatRegs;
883         creg < max_reg; creg++) {
884        PhysRegIndex phys_reg = freeList.getCCReg();
885
886        renameMap[tid].setEntry(creg,phys_reg);
887        scoreboard.setReg(phys_reg);
888    }
889
890    //Copy Thread Data Into RegFile
891    //this->copyFromTC(tid);
892
893    //Set PC/NPC/NNPC
894    pcState(src_tc->pcState(), tid);
895
896    src_tc->setStatus(ThreadContext::Active);
897
898    activateContext(tid, Cycles(1));
899
900    //Reset ROB/IQ/LSQ Entries
901    commit.rob->resetEntries();
902    iew.resetEntries();
903}
904
905template <class Impl>
906void
907FullO3CPU<Impl>::removeThread(ThreadID tid)
908{
909    DPRINTF(O3CPU,"[tid:%i] Removing thread context from CPU.\n", tid);
910
911    // Copy Thread Data From RegFile
912    // If thread is suspended, it might be re-allocated
913    // this->copyToTC(tid);
914
915
916    // @todo: 2-27-2008: Fix how we free up rename mappings
917    // here to alleviate the case for double-freeing registers
918    // in SMT workloads.
919
920    // Unbind Int Regs from Rename Map
921    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
922        PhysRegIndex phys_reg = renameMap[tid].lookup(ireg);
923
924        scoreboard.unsetReg(phys_reg);
925        freeList.addReg(phys_reg);
926    }
927
928    // Unbind Float Regs from Rename Map
929    int max_reg = TheISA::NumIntRegs + TheISA::NumFloatRegs;
930    for (int freg = TheISA::NumIntRegs; freg < max_reg; freg++) {
931        PhysRegIndex phys_reg = renameMap[tid].lookup(freg);
932
933        scoreboard.unsetReg(phys_reg);
934        freeList.addReg(phys_reg);
935    }
936
937    // Unbind condition-code Regs from Rename Map
938    max_reg = TheISA::NumIntRegs + TheISA::NumFloatRegs + TheISA::NumCCRegs;
939    for (int creg = TheISA::NumIntRegs + TheISA::NumFloatRegs;
940         creg < max_reg; creg++) {
941        PhysRegIndex phys_reg = renameMap[tid].lookup(creg);
942
943        scoreboard.unsetReg(phys_reg);
944        freeList.addReg(phys_reg);
945    }
946
947    // Squash Throughout Pipeline
948    DynInstPtr inst = commit.rob->readHeadInst(tid);
949    InstSeqNum squash_seq_num = inst->seqNum;
950    fetch.squash(0, squash_seq_num, inst, tid);
951    decode.squash(tid);
952    rename.squash(squash_seq_num, tid);
953    iew.squash(tid);
954    iew.ldstQueue.squash(squash_seq_num, tid);
955    commit.rob->squash(squash_seq_num, tid);
956
957
958    assert(iew.instQueue.getCount(tid) == 0);
959    assert(iew.ldstQueue.getCount(tid) == 0);
960
961    // Reset ROB/IQ/LSQ Entries
962
963    // Commented out for now.  This should be possible to do by
964    // telling all the pipeline stages to drain first, and then
965    // checking until the drain completes.  Once the pipeline is
966    // drained, call resetEntries(). - 10-09-06 ktlim
967/*
968    if (activeThreads.size() >= 1) {
969        commit.rob->resetEntries();
970        iew.resetEntries();
971    }
972*/
973}
974
975
976template <class Impl>
977void
978FullO3CPU<Impl>::activateWhenReady(ThreadID tid)
979{
980    DPRINTF(O3CPU,"[tid:%i]: Checking if resources are available for incoming"
981            "(e.g. PhysRegs/ROB/IQ/LSQ) \n",
982            tid);
983
984    bool ready = true;
985
986    // Should these all be '<' not '>='?  This seems backwards...
987    if (freeList.numFreeIntRegs() >= TheISA::NumIntRegs) {
988        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
989                "Phys. Int. Regs.\n",
990                tid);
991        ready = false;
992    } else if (freeList.numFreeFloatRegs() >= TheISA::NumFloatRegs) {
993        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
994                "Phys. Float. Regs.\n",
995                tid);
996        ready = false;
997    } else if (freeList.numFreeCCRegs() >= TheISA::NumCCRegs) {
998        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
999                "Phys. CC. Regs.\n",
1000                tid);
1001        ready = false;
1002    } else if (commit.rob->numFreeEntries() >=
1003               commit.rob->entryAmount(activeThreads.size() + 1)) {
1004        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
1005                "ROB entries.\n",
1006                tid);
1007        ready = false;
1008    } else if (iew.instQueue.numFreeEntries() >=
1009               iew.instQueue.entryAmount(activeThreads.size() + 1)) {
1010        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
1011                "IQ entries.\n",
1012                tid);
1013        ready = false;
1014    } else if (iew.ldstQueue.numFreeEntries() >=
1015               iew.ldstQueue.entryAmount(activeThreads.size() + 1)) {
1016        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
1017                "LSQ entries.\n",
1018                tid);
1019        ready = false;
1020    }
1021
1022    if (ready) {
1023        insertThread(tid);
1024
1025        contextSwitch = false;
1026
1027        cpuWaitList.remove(tid);
1028    } else {
1029        suspendContext(tid);
1030
1031        //blocks fetch
1032        contextSwitch = true;
1033
1034        //@todo: dont always add to waitlist
1035        //do waitlist
1036        cpuWaitList.push_back(tid);
1037    }
1038}
1039
1040template <class Impl>
1041Fault
1042FullO3CPU<Impl>::hwrei(ThreadID tid)
1043{
1044#if THE_ISA == ALPHA_ISA
1045    // Need to clear the lock flag upon returning from an interrupt.
1046    this->setMiscRegNoEffect(AlphaISA::MISCREG_LOCKFLAG, false, tid);
1047
1048    this->thread[tid]->kernelStats->hwrei();
1049
1050    // FIXME: XXX check for interrupts? XXX
1051#endif
1052    return NoFault;
1053}
1054
1055template <class Impl>
1056bool
1057FullO3CPU<Impl>::simPalCheck(int palFunc, ThreadID tid)
1058{
1059#if THE_ISA == ALPHA_ISA
1060    if (this->thread[tid]->kernelStats)
1061        this->thread[tid]->kernelStats->callpal(palFunc,
1062                                                this->threadContexts[tid]);
1063
1064    switch (palFunc) {
1065      case PAL::halt:
1066        halt();
1067        if (--System::numSystemsRunning == 0)
1068            exitSimLoop("all cpus halted");
1069        break;
1070
1071      case PAL::bpt:
1072      case PAL::bugchk:
1073        if (this->system->breakpoint())
1074            return false;
1075        break;
1076    }
1077#endif
1078    return true;
1079}
1080
1081template <class Impl>
1082Fault
1083FullO3CPU<Impl>::getInterrupts()
1084{
1085    // Check if there are any outstanding interrupts
1086    return this->interrupts->getInterrupt(this->threadContexts[0]);
1087}
1088
1089template <class Impl>
1090void
1091FullO3CPU<Impl>::processInterrupts(Fault interrupt)
1092{
1093    // Check for interrupts here.  For now can copy the code that
1094    // exists within isa_fullsys_traits.hh.  Also assume that thread 0
1095    // is the one that handles the interrupts.
1096    // @todo: Possibly consolidate the interrupt checking code.
1097    // @todo: Allow other threads to handle interrupts.
1098
1099    assert(interrupt != NoFault);
1100    this->interrupts->updateIntrInfo(this->threadContexts[0]);
1101
1102    DPRINTF(O3CPU, "Interrupt %s being handled\n", interrupt->name());
1103    this->trap(interrupt, 0, NULL);
1104}
1105
1106template <class Impl>
1107void
1108FullO3CPU<Impl>::trap(Fault fault, ThreadID tid, StaticInstPtr inst)
1109{
1110    // Pass the thread's TC into the invoke method.
1111    fault->invoke(this->threadContexts[tid], inst);
1112}
1113
1114template <class Impl>
1115void
1116FullO3CPU<Impl>::syscall(int64_t callnum, ThreadID tid)
1117{
1118    DPRINTF(O3CPU, "[tid:%i] Executing syscall().\n\n", tid);
1119
1120    DPRINTF(Activity,"Activity: syscall() called.\n");
1121
1122    // Temporarily increase this by one to account for the syscall
1123    // instruction.
1124    ++(this->thread[tid]->funcExeInst);
1125
1126    // Execute the actual syscall.
1127    this->thread[tid]->syscall(callnum);
1128
1129    // Decrease funcExeInst by one as the normal commit will handle
1130    // incrementing it.
1131    --(this->thread[tid]->funcExeInst);
1132}
1133
1134template <class Impl>
1135void
1136FullO3CPU<Impl>::serializeThread(std::ostream &os, ThreadID tid)
1137{
1138    thread[tid]->serialize(os);
1139}
1140
1141template <class Impl>
1142void
1143FullO3CPU<Impl>::unserializeThread(Checkpoint *cp, const std::string &section,
1144                                   ThreadID tid)
1145{
1146    thread[tid]->unserialize(cp, section);
1147}
1148
1149template <class Impl>
1150unsigned int
1151FullO3CPU<Impl>::drain(DrainManager *drain_manager)
1152{
1153    // If the CPU isn't doing anything, then return immediately.
1154    if (switchedOut()) {
1155        setDrainState(Drainable::Drained);
1156        return 0;
1157    }
1158
1159    DPRINTF(Drain, "Draining...\n");
1160    setDrainState(Drainable::Draining);
1161
1162    // We only need to signal a drain to the commit stage as this
1163    // initiates squashing controls the draining. Once the commit
1164    // stage commits an instruction where it is safe to stop, it'll
1165    // squash the rest of the instructions in the pipeline and force
1166    // the fetch stage to stall. The pipeline will be drained once all
1167    // in-flight instructions have retired.
1168    commit.drain();
1169
1170    // Wake the CPU and record activity so everything can drain out if
1171    // the CPU was not able to immediately drain.
1172    if (!isDrained())  {
1173        drainManager = drain_manager;
1174
1175        wakeCPU();
1176        activityRec.activity();
1177
1178        DPRINTF(Drain, "CPU not drained\n");
1179
1180        return 1;
1181    } else {
1182        setDrainState(Drainable::Drained);
1183        DPRINTF(Drain, "CPU is already drained\n");
1184        if (tickEvent.scheduled())
1185            deschedule(tickEvent);
1186
1187        // Flush out any old data from the time buffers.  In
1188        // particular, there might be some data in flight from the
1189        // fetch stage that isn't visible in any of the CPU buffers we
1190        // test in isDrained().
1191        for (int i = 0; i < timeBuffer.getSize(); ++i) {
1192            timeBuffer.advance();
1193            fetchQueue.advance();
1194            decodeQueue.advance();
1195            renameQueue.advance();
1196            iewQueue.advance();
1197        }
1198
1199        drainSanityCheck();
1200        return 0;
1201    }
1202}
1203
1204template <class Impl>
1205bool
1206FullO3CPU<Impl>::tryDrain()
1207{
1208    if (!drainManager || !isDrained())
1209        return false;
1210
1211    if (tickEvent.scheduled())
1212        deschedule(tickEvent);
1213
1214    DPRINTF(Drain, "CPU done draining, processing drain event\n");
1215    drainManager->signalDrainDone();
1216    drainManager = NULL;
1217
1218    return true;
1219}
1220
1221template <class Impl>
1222void
1223FullO3CPU<Impl>::drainSanityCheck() const
1224{
1225    assert(isDrained());
1226    fetch.drainSanityCheck();
1227    decode.drainSanityCheck();
1228    rename.drainSanityCheck();
1229    iew.drainSanityCheck();
1230    commit.drainSanityCheck();
1231}
1232
1233template <class Impl>
1234bool
1235FullO3CPU<Impl>::isDrained() const
1236{
1237    bool drained(true);
1238
1239    for (ThreadID i = 0; i < thread.size(); ++i) {
1240        if (activateThreadEvent[i].scheduled()) {
1241            DPRINTF(Drain, "CPU not drained, tread %i has a "
1242                    "pending activate event\n", i);
1243            drained = false;
1244        }
1245        if (deallocateContextEvent[i].scheduled()) {
1246            DPRINTF(Drain, "CPU not drained, tread %i has a "
1247                    "pending deallocate context event\n", i);
1248            drained = false;
1249        }
1250    }
1251
1252    if (!instList.empty() || !removeList.empty()) {
1253        DPRINTF(Drain, "Main CPU structures not drained.\n");
1254        drained = false;
1255    }
1256
1257    if (!fetch.isDrained()) {
1258        DPRINTF(Drain, "Fetch not drained.\n");
1259        drained = false;
1260    }
1261
1262    if (!decode.isDrained()) {
1263        DPRINTF(Drain, "Decode not drained.\n");
1264        drained = false;
1265    }
1266
1267    if (!rename.isDrained()) {
1268        DPRINTF(Drain, "Rename not drained.\n");
1269        drained = false;
1270    }
1271
1272    if (!iew.isDrained()) {
1273        DPRINTF(Drain, "IEW not drained.\n");
1274        drained = false;
1275    }
1276
1277    if (!commit.isDrained()) {
1278        DPRINTF(Drain, "Commit not drained.\n");
1279        drained = false;
1280    }
1281
1282    return drained;
1283}
1284
1285template <class Impl>
1286void
1287FullO3CPU<Impl>::commitDrained(ThreadID tid)
1288{
1289    fetch.drainStall(tid);
1290}
1291
1292template <class Impl>
1293void
1294FullO3CPU<Impl>::drainResume()
1295{
1296    setDrainState(Drainable::Running);
1297    if (switchedOut())
1298        return;
1299
1300    DPRINTF(Drain, "Resuming...\n");
1301    verifyMemoryMode();
1302
1303    fetch.drainResume();
1304    commit.drainResume();
1305
1306    _status = Idle;
1307    for (ThreadID i = 0; i < thread.size(); i++) {
1308        if (thread[i]->status() == ThreadContext::Active) {
1309            DPRINTF(Drain, "Activating thread: %i\n", i);
1310            activateThread(i);
1311            _status = Running;
1312        }
1313    }
1314
1315    assert(!tickEvent.scheduled());
1316    if (_status == Running)
1317        schedule(tickEvent, nextCycle());
1318}
1319
1320template <class Impl>
1321void
1322FullO3CPU<Impl>::switchOut()
1323{
1324    DPRINTF(O3CPU, "Switching out\n");
1325    BaseCPU::switchOut();
1326
1327    activityRec.reset();
1328
1329    _status = SwitchedOut;
1330
1331    if (checker)
1332        checker->switchOut();
1333}
1334
1335template <class Impl>
1336void
1337FullO3CPU<Impl>::takeOverFrom(BaseCPU *oldCPU)
1338{
1339    BaseCPU::takeOverFrom(oldCPU);
1340
1341    fetch.takeOverFrom();
1342    decode.takeOverFrom();
1343    rename.takeOverFrom();
1344    iew.takeOverFrom();
1345    commit.takeOverFrom();
1346
1347    assert(!tickEvent.scheduled());
1348
1349    FullO3CPU<Impl> *oldO3CPU = dynamic_cast<FullO3CPU<Impl>*>(oldCPU);
1350    if (oldO3CPU)
1351        globalSeqNum = oldO3CPU->globalSeqNum;
1352
1353    lastRunningCycle = curCycle();
1354    _status = Idle;
1355}
1356
1357template <class Impl>
1358void
1359FullO3CPU<Impl>::verifyMemoryMode() const
1360{
1361    if (!system->isTimingMode()) {
1362        fatal("The O3 CPU requires the memory system to be in "
1363              "'timing' mode.\n");
1364    }
1365}
1366
1367template <class Impl>
1368TheISA::MiscReg
1369FullO3CPU<Impl>::readMiscRegNoEffect(int misc_reg, ThreadID tid)
1370{
1371    return this->isa[tid]->readMiscRegNoEffect(misc_reg);
1372}
1373
1374template <class Impl>
1375TheISA::MiscReg
1376FullO3CPU<Impl>::readMiscReg(int misc_reg, ThreadID tid)
1377{
1378    miscRegfileReads++;
1379    return this->isa[tid]->readMiscReg(misc_reg, tcBase(tid));
1380}
1381
1382template <class Impl>
1383void
1384FullO3CPU<Impl>::setMiscRegNoEffect(int misc_reg,
1385        const TheISA::MiscReg &val, ThreadID tid)
1386{
1387    this->isa[tid]->setMiscRegNoEffect(misc_reg, val);
1388}
1389
1390template <class Impl>
1391void
1392FullO3CPU<Impl>::setMiscReg(int misc_reg,
1393        const TheISA::MiscReg &val, ThreadID tid)
1394{
1395    miscRegfileWrites++;
1396    this->isa[tid]->setMiscReg(misc_reg, val, tcBase(tid));
1397}
1398
1399template <class Impl>
1400uint64_t
1401FullO3CPU<Impl>::readIntReg(int reg_idx)
1402{
1403    intRegfileReads++;
1404    return regFile.readIntReg(reg_idx);
1405}
1406
1407template <class Impl>
1408FloatReg
1409FullO3CPU<Impl>::readFloatReg(int reg_idx)
1410{
1411    fpRegfileReads++;
1412    return regFile.readFloatReg(reg_idx);
1413}
1414
1415template <class Impl>
1416FloatRegBits
1417FullO3CPU<Impl>::readFloatRegBits(int reg_idx)
1418{
1419    fpRegfileReads++;
1420    return regFile.readFloatRegBits(reg_idx);
1421}
1422
1423template <class Impl>
1424CCReg
1425FullO3CPU<Impl>::readCCReg(int reg_idx)
1426{
1427    ccRegfileReads++;
1428    return regFile.readCCReg(reg_idx);
1429}
1430
1431template <class Impl>
1432void
1433FullO3CPU<Impl>::setIntReg(int reg_idx, uint64_t val)
1434{
1435    intRegfileWrites++;
1436    regFile.setIntReg(reg_idx, val);
1437}
1438
1439template <class Impl>
1440void
1441FullO3CPU<Impl>::setFloatReg(int reg_idx, FloatReg val)
1442{
1443    fpRegfileWrites++;
1444    regFile.setFloatReg(reg_idx, val);
1445}
1446
1447template <class Impl>
1448void
1449FullO3CPU<Impl>::setFloatRegBits(int reg_idx, FloatRegBits val)
1450{
1451    fpRegfileWrites++;
1452    regFile.setFloatRegBits(reg_idx, val);
1453}
1454
1455template <class Impl>
1456void
1457FullO3CPU<Impl>::setCCReg(int reg_idx, CCReg val)
1458{
1459    ccRegfileWrites++;
1460    regFile.setCCReg(reg_idx, val);
1461}
1462
1463template <class Impl>
1464uint64_t
1465FullO3CPU<Impl>::readArchIntReg(int reg_idx, ThreadID tid)
1466{
1467    intRegfileReads++;
1468    PhysRegIndex phys_reg = commitRenameMap[tid].lookupInt(reg_idx);
1469
1470    return regFile.readIntReg(phys_reg);
1471}
1472
1473template <class Impl>
1474float
1475FullO3CPU<Impl>::readArchFloatReg(int reg_idx, ThreadID tid)
1476{
1477    fpRegfileReads++;
1478    PhysRegIndex phys_reg = commitRenameMap[tid].lookupFloat(reg_idx);
1479
1480    return regFile.readFloatReg(phys_reg);
1481}
1482
1483template <class Impl>
1484uint64_t
1485FullO3CPU<Impl>::readArchFloatRegInt(int reg_idx, ThreadID tid)
1486{
1487    fpRegfileReads++;
1488    PhysRegIndex phys_reg = commitRenameMap[tid].lookupFloat(reg_idx);
1489
1490    return regFile.readFloatRegBits(phys_reg);
1491}
1492
1493template <class Impl>
1494CCReg
1495FullO3CPU<Impl>::readArchCCReg(int reg_idx, ThreadID tid)
1496{
1497    ccRegfileReads++;
1498    PhysRegIndex phys_reg = commitRenameMap[tid].lookupCC(reg_idx);
1499
1500    return regFile.readCCReg(phys_reg);
1501}
1502
1503template <class Impl>
1504void
1505FullO3CPU<Impl>::setArchIntReg(int reg_idx, uint64_t val, ThreadID tid)
1506{
1507    intRegfileWrites++;
1508    PhysRegIndex phys_reg = commitRenameMap[tid].lookupInt(reg_idx);
1509
1510    regFile.setIntReg(phys_reg, val);
1511}
1512
1513template <class Impl>
1514void
1515FullO3CPU<Impl>::setArchFloatReg(int reg_idx, float val, ThreadID tid)
1516{
1517    fpRegfileWrites++;
1518    PhysRegIndex phys_reg = commitRenameMap[tid].lookupFloat(reg_idx);
1519
1520    regFile.setFloatReg(phys_reg, val);
1521}
1522
1523template <class Impl>
1524void
1525FullO3CPU<Impl>::setArchFloatRegInt(int reg_idx, uint64_t val, ThreadID tid)
1526{
1527    fpRegfileWrites++;
1528    PhysRegIndex phys_reg = commitRenameMap[tid].lookupFloat(reg_idx);
1529
1530    regFile.setFloatRegBits(phys_reg, val);
1531}
1532
1533template <class Impl>
1534void
1535FullO3CPU<Impl>::setArchCCReg(int reg_idx, CCReg val, ThreadID tid)
1536{
1537    ccRegfileWrites++;
1538    PhysRegIndex phys_reg = commitRenameMap[tid].lookupCC(reg_idx);
1539
1540    regFile.setCCReg(phys_reg, val);
1541}
1542
1543template <class Impl>
1544TheISA::PCState
1545FullO3CPU<Impl>::pcState(ThreadID tid)
1546{
1547    return commit.pcState(tid);
1548}
1549
1550template <class Impl>
1551void
1552FullO3CPU<Impl>::pcState(const TheISA::PCState &val, ThreadID tid)
1553{
1554    commit.pcState(val, tid);
1555}
1556
1557template <class Impl>
1558Addr
1559FullO3CPU<Impl>::instAddr(ThreadID tid)
1560{
1561    return commit.instAddr(tid);
1562}
1563
1564template <class Impl>
1565Addr
1566FullO3CPU<Impl>::nextInstAddr(ThreadID tid)
1567{
1568    return commit.nextInstAddr(tid);
1569}
1570
1571template <class Impl>
1572MicroPC
1573FullO3CPU<Impl>::microPC(ThreadID tid)
1574{
1575    return commit.microPC(tid);
1576}
1577
1578template <class Impl>
1579void
1580FullO3CPU<Impl>::squashFromTC(ThreadID tid)
1581{
1582    this->thread[tid]->noSquashFromTC = true;
1583    this->commit.generateTCEvent(tid);
1584}
1585
1586template <class Impl>
1587typename FullO3CPU<Impl>::ListIt
1588FullO3CPU<Impl>::addInst(DynInstPtr &inst)
1589{
1590    instList.push_back(inst);
1591
1592    return --(instList.end());
1593}
1594
1595template <class Impl>
1596void
1597FullO3CPU<Impl>::instDone(ThreadID tid, DynInstPtr &inst)
1598{
1599    // Keep an instruction count.
1600    if (!inst->isMicroop() || inst->isLastMicroop()) {
1601        thread[tid]->numInst++;
1602        thread[tid]->numInsts++;
1603        committedInsts[tid]++;
1604        totalCommittedInsts++;
1605    }
1606    thread[tid]->numOp++;
1607    thread[tid]->numOps++;
1608    committedOps[tid]++;
1609
1610    system->totalNumInsts++;
1611    // Check for instruction-count-based events.
1612    comInstEventQueue[tid]->serviceEvents(thread[tid]->numInst);
1613    system->instEventQueue.serviceEvents(system->totalNumInsts);
1614}
1615
1616template <class Impl>
1617void
1618FullO3CPU<Impl>::removeFrontInst(DynInstPtr &inst)
1619{
1620    DPRINTF(O3CPU, "Removing committed instruction [tid:%i] PC %s "
1621            "[sn:%lli]\n",
1622            inst->threadNumber, inst->pcState(), inst->seqNum);
1623
1624    removeInstsThisCycle = true;
1625
1626    // Remove the front instruction.
1627    removeList.push(inst->getInstListIt());
1628}
1629
1630template <class Impl>
1631void
1632FullO3CPU<Impl>::removeInstsNotInROB(ThreadID tid)
1633{
1634    DPRINTF(O3CPU, "Thread %i: Deleting instructions from instruction"
1635            " list.\n", tid);
1636
1637    ListIt end_it;
1638
1639    bool rob_empty = false;
1640
1641    if (instList.empty()) {
1642        return;
1643    } else if (rob.isEmpty(tid)) {
1644        DPRINTF(O3CPU, "ROB is empty, squashing all insts.\n");
1645        end_it = instList.begin();
1646        rob_empty = true;
1647    } else {
1648        end_it = (rob.readTailInst(tid))->getInstListIt();
1649        DPRINTF(O3CPU, "ROB is not empty, squashing insts not in ROB.\n");
1650    }
1651
1652    removeInstsThisCycle = true;
1653
1654    ListIt inst_it = instList.end();
1655
1656    inst_it--;
1657
1658    // Walk through the instruction list, removing any instructions
1659    // that were inserted after the given instruction iterator, end_it.
1660    while (inst_it != end_it) {
1661        assert(!instList.empty());
1662
1663        squashInstIt(inst_it, tid);
1664
1665        inst_it--;
1666    }
1667
1668    // If the ROB was empty, then we actually need to remove the first
1669    // instruction as well.
1670    if (rob_empty) {
1671        squashInstIt(inst_it, tid);
1672    }
1673}
1674
1675template <class Impl>
1676void
1677FullO3CPU<Impl>::removeInstsUntil(const InstSeqNum &seq_num, ThreadID tid)
1678{
1679    assert(!instList.empty());
1680
1681    removeInstsThisCycle = true;
1682
1683    ListIt inst_iter = instList.end();
1684
1685    inst_iter--;
1686
1687    DPRINTF(O3CPU, "Deleting instructions from instruction "
1688            "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1689            tid, seq_num, (*inst_iter)->seqNum);
1690
1691    while ((*inst_iter)->seqNum > seq_num) {
1692
1693        bool break_loop = (inst_iter == instList.begin());
1694
1695        squashInstIt(inst_iter, tid);
1696
1697        inst_iter--;
1698
1699        if (break_loop)
1700            break;
1701    }
1702}
1703
1704template <class Impl>
1705inline void
1706FullO3CPU<Impl>::squashInstIt(const ListIt &instIt, ThreadID tid)
1707{
1708    if ((*instIt)->threadNumber == tid) {
1709        DPRINTF(O3CPU, "Squashing instruction, "
1710                "[tid:%i] [sn:%lli] PC %s\n",
1711                (*instIt)->threadNumber,
1712                (*instIt)->seqNum,
1713                (*instIt)->pcState());
1714
1715        // Mark it as squashed.
1716        (*instIt)->setSquashed();
1717
1718        // @todo: Formulate a consistent method for deleting
1719        // instructions from the instruction list
1720        // Remove the instruction from the list.
1721        removeList.push(instIt);
1722    }
1723}
1724
1725template <class Impl>
1726void
1727FullO3CPU<Impl>::cleanUpRemovedInsts()
1728{
1729    while (!removeList.empty()) {
1730        DPRINTF(O3CPU, "Removing instruction, "
1731                "[tid:%i] [sn:%lli] PC %s\n",
1732                (*removeList.front())->threadNumber,
1733                (*removeList.front())->seqNum,
1734                (*removeList.front())->pcState());
1735
1736        instList.erase(removeList.front());
1737
1738        removeList.pop();
1739    }
1740
1741    removeInstsThisCycle = false;
1742}
1743/*
1744template <class Impl>
1745void
1746FullO3CPU<Impl>::removeAllInsts()
1747{
1748    instList.clear();
1749}
1750*/
1751template <class Impl>
1752void
1753FullO3CPU<Impl>::dumpInsts()
1754{
1755    int num = 0;
1756
1757    ListIt inst_list_it = instList.begin();
1758
1759    cprintf("Dumping Instruction List\n");
1760
1761    while (inst_list_it != instList.end()) {
1762        cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1763                "Squashed:%i\n\n",
1764                num, (*inst_list_it)->instAddr(), (*inst_list_it)->threadNumber,
1765                (*inst_list_it)->seqNum, (*inst_list_it)->isIssued(),
1766                (*inst_list_it)->isSquashed());
1767        inst_list_it++;
1768        ++num;
1769    }
1770}
1771/*
1772template <class Impl>
1773void
1774FullO3CPU<Impl>::wakeDependents(DynInstPtr &inst)
1775{
1776    iew.wakeDependents(inst);
1777}
1778*/
1779template <class Impl>
1780void
1781FullO3CPU<Impl>::wakeCPU()
1782{
1783    if (activityRec.active() || tickEvent.scheduled()) {
1784        DPRINTF(Activity, "CPU already running.\n");
1785        return;
1786    }
1787
1788    DPRINTF(Activity, "Waking up CPU\n");
1789
1790    Cycles cycles(curCycle() - lastRunningCycle);
1791    // @todo: This is an oddity that is only here to match the stats
1792    if (cycles != 0)
1793        --cycles;
1794    idleCycles += cycles;
1795    numCycles += cycles;
1796
1797    schedule(tickEvent, clockEdge());
1798}
1799
1800template <class Impl>
1801void
1802FullO3CPU<Impl>::wakeup()
1803{
1804    if (this->thread[0]->status() != ThreadContext::Suspended)
1805        return;
1806
1807    this->wakeCPU();
1808
1809    DPRINTF(Quiesce, "Suspended Processor woken\n");
1810    this->threadContexts[0]->activate();
1811}
1812
1813template <class Impl>
1814ThreadID
1815FullO3CPU<Impl>::getFreeTid()
1816{
1817    for (ThreadID tid = 0; tid < numThreads; tid++) {
1818        if (!tids[tid]) {
1819            tids[tid] = true;
1820            return tid;
1821        }
1822    }
1823
1824    return InvalidThreadID;
1825}
1826
1827template <class Impl>
1828void
1829FullO3CPU<Impl>::doContextSwitch()
1830{
1831    if (contextSwitch) {
1832
1833        //ADD CODE TO DEACTIVE THREAD HERE (???)
1834
1835        ThreadID size = cpuWaitList.size();
1836        for (ThreadID tid = 0; tid < size; tid++) {
1837            activateWhenReady(tid);
1838        }
1839
1840        if (cpuWaitList.size() == 0)
1841            contextSwitch = true;
1842    }
1843}
1844
1845template <class Impl>
1846void
1847FullO3CPU<Impl>::updateThreadPriority()
1848{
1849    if (activeThreads.size() > 1) {
1850        //DEFAULT TO ROUND ROBIN SCHEME
1851        //e.g. Move highest priority to end of thread list
1852        list<ThreadID>::iterator list_begin = activeThreads.begin();
1853
1854        unsigned high_thread = *list_begin;
1855
1856        activeThreads.erase(list_begin);
1857
1858        activeThreads.push_back(high_thread);
1859    }
1860}
1861
1862// Forward declaration of FullO3CPU.
1863template class FullO3CPU<O3CPUImpl>;
1864