2 * Copyright (c) 2004-2006 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "config/full_system.hh"
33 #include "config/use_checker.hh"
36 #include "sim/system.hh"
38 #include "sim/process.hh"
41 #include "cpu/activity.hh"
42 #include "cpu/simple_thread.hh"
43 #include "cpu/thread_context.hh"
44 #include "cpu/o3/isa_specific.hh"
45 #include "cpu/o3/cpu.hh"
47 #include "sim/root.hh"
48 #include "sim/stat_control.hh"
51 #include "cpu/checker/cpu.hh"
55 using namespace TheISA
;
57 BaseO3CPU::BaseO3CPU(Params
*params
)
58 : BaseCPU(params
), cpu_id(0)
69 FullO3CPU
<Impl
>::TickEvent::TickEvent(FullO3CPU
<Impl
> *c
)
70 : Event(&mainEventQueue
, CPU_Tick_Pri
), cpu(c
)
76 FullO3CPU
<Impl
>::TickEvent::process()
83 FullO3CPU
<Impl
>::TickEvent::description()
85 return "FullO3CPU tick event";
89 FullO3CPU
<Impl
>::ActivateThreadEvent::ActivateThreadEvent()
90 : Event(&mainEventQueue
, CPU_Tick_Pri
)
96 FullO3CPU
<Impl
>::ActivateThreadEvent::init(int thread_num
,
97 FullO3CPU
<Impl
> *thread_cpu
)
103 template <class Impl
>
105 FullO3CPU
<Impl
>::ActivateThreadEvent::process()
107 cpu
->activateThread(tid
);
110 template <class Impl
>
112 FullO3CPU
<Impl
>::ActivateThreadEvent::description()
114 return "FullO3CPU \"Activate Thread\" event";
117 template <class Impl
>
118 FullO3CPU
<Impl
>::DeallocateContextEvent::DeallocateContextEvent()
119 : Event(&mainEventQueue
, CPU_Tick_Pri
)
123 template <class Impl
>
125 FullO3CPU
<Impl
>::DeallocateContextEvent::init(int thread_num
,
126 FullO3CPU
<Impl
> *thread_cpu
)
132 template <class Impl
>
134 FullO3CPU
<Impl
>::DeallocateContextEvent::process()
136 cpu
->deactivateThread(tid
);
137 cpu
->removeThread(tid
);
140 template <class Impl
>
142 FullO3CPU
<Impl
>::DeallocateContextEvent::description()
144 return "FullO3CPU \"Deallocate Context\" event";
147 template <class Impl
>
148 FullO3CPU
<Impl
>::FullO3CPU(Params
*params
)
151 removeInstsThisCycle(false),
158 regFile(params
->numPhysIntRegs
, params
->numPhysFloatRegs
),
160 freeList(params
->numberOfThreads
,
161 TheISA::NumIntRegs
, params
->numPhysIntRegs
,
162 TheISA::NumFloatRegs
, params
->numPhysFloatRegs
),
164 rob(params
->numROBEntries
, params
->squashWidth
,
165 params
->smtROBPolicy
, params
->smtROBThreshold
,
166 params
->numberOfThreads
),
168 scoreboard(params
->numberOfThreads
,
169 TheISA::NumIntRegs
, params
->numPhysIntRegs
,
170 TheISA::NumFloatRegs
, params
->numPhysFloatRegs
,
171 TheISA::NumMiscRegs
* number_of_threads
,
174 timeBuffer(params
->backComSize
, params
->forwardComSize
),
175 fetchQueue(params
->backComSize
, params
->forwardComSize
),
176 decodeQueue(params
->backComSize
, params
->forwardComSize
),
177 renameQueue(params
->backComSize
, params
->forwardComSize
),
178 iewQueue(params
->backComSize
, params
->forwardComSize
),
179 activityRec(NumStages
,
180 params
->backComSize
+ params
->forwardComSize
,
186 system(params
->system
),
187 physmem(system
->physmem
),
188 #endif // FULL_SYSTEM
191 deferRegistration(params
->deferRegistration
),
192 numThreads(number_of_threads
)
198 if (params
->checker
) {
200 BaseCPU
*temp_checker
= params
->checker
;
201 checker
= dynamic_cast<Checker
<DynInstPtr
> *>(temp_checker
);
202 checker
->setMemory(mem
);
204 checker
->setSystem(params
->system
);
207 panic("Checker enabled but not compiled in!");
208 #endif // USE_CHECKER
212 thread
.resize(number_of_threads
);
213 tids
.resize(number_of_threads
);
216 // The stages also need their CPU pointer setup. However this
217 // must be done at the upper level CPU because they have pointers
218 // to the upper level CPU, and not this FullO3CPU.
220 // Set up Pointers to the activeThreads list for each stage
221 fetch
.setActiveThreads(&activeThreads
);
222 decode
.setActiveThreads(&activeThreads
);
223 rename
.setActiveThreads(&activeThreads
);
224 iew
.setActiveThreads(&activeThreads
);
225 commit
.setActiveThreads(&activeThreads
);
227 // Give each of the stages the time buffer they will use.
228 fetch
.setTimeBuffer(&timeBuffer
);
229 decode
.setTimeBuffer(&timeBuffer
);
230 rename
.setTimeBuffer(&timeBuffer
);
231 iew
.setTimeBuffer(&timeBuffer
);
232 commit
.setTimeBuffer(&timeBuffer
);
234 // Also setup each of the stages' queues.
235 fetch
.setFetchQueue(&fetchQueue
);
236 decode
.setFetchQueue(&fetchQueue
);
237 commit
.setFetchQueue(&fetchQueue
);
238 decode
.setDecodeQueue(&decodeQueue
);
239 rename
.setDecodeQueue(&decodeQueue
);
240 rename
.setRenameQueue(&renameQueue
);
241 iew
.setRenameQueue(&renameQueue
);
242 iew
.setIEWQueue(&iewQueue
);
243 commit
.setIEWQueue(&iewQueue
);
244 commit
.setRenameQueue(&renameQueue
);
246 commit
.setIEWStage(&iew
);
247 rename
.setIEWStage(&iew
);
248 rename
.setCommitStage(&commit
);
251 int active_threads
= params
->workload
.size();
253 if (active_threads
> Impl::MaxThreads
) {
254 panic("Workload Size too large. Increase the 'MaxThreads'"
255 "constant in your O3CPU impl. file (e.g. o3/alpha/impl.hh) or "
256 "edit your workload size.");
259 int active_threads
= 1;
262 //Make Sure That this a Valid Architeture
263 assert(params
->numPhysIntRegs
>= numThreads
* TheISA::NumIntRegs
);
264 assert(params
->numPhysFloatRegs
>= numThreads
* TheISA::NumFloatRegs
);
266 rename
.setScoreboard(&scoreboard
);
267 iew
.setScoreboard(&scoreboard
);
269 // Setup the rename map for whichever stages need it.
270 PhysRegIndex lreg_idx
= 0;
271 PhysRegIndex freg_idx
= params
->numPhysIntRegs
; //Index to 1 after int regs
273 for (int tid
=0; tid
< numThreads
; tid
++) {
274 bool bindRegs
= (tid
<= active_threads
- 1);
276 commitRenameMap
[tid
].init(TheISA::NumIntRegs
,
277 params
->numPhysIntRegs
,
278 lreg_idx
, //Index for Logical. Regs
280 TheISA::NumFloatRegs
,
281 params
->numPhysFloatRegs
,
282 freg_idx
, //Index for Float Regs
292 renameMap
[tid
].init(TheISA::NumIntRegs
,
293 params
->numPhysIntRegs
,
294 lreg_idx
, //Index for Logical. Regs
296 TheISA::NumFloatRegs
,
297 params
->numPhysFloatRegs
,
298 freg_idx
, //Index for Float Regs
309 rename
.setRenameMap(renameMap
);
310 commit
.setRenameMap(commitRenameMap
);
312 // Give renameMap & rename stage access to the freeList;
313 for (int i
=0; i
< numThreads
; i
++) {
314 renameMap
[i
].setFreeList(&freeList
);
316 rename
.setFreeList(&freeList
);
318 // Setup the ROB for whichever stages need it.
321 lastRunningCycle
= curTick
;
323 lastActivatedCycle
= -1;
325 contextSwitch
= false;
328 template <class Impl
>
329 FullO3CPU
<Impl
>::~FullO3CPU()
333 template <class Impl
>
335 FullO3CPU
<Impl
>::fullCPURegStats()
337 BaseO3CPU::regStats();
339 // Register any of the O3CPU's stats here.
341 .name(name() + ".timesIdled")
342 .desc("Number of times that the entire CPU went into an idle state and"
343 " unscheduled itself")
347 .name(name() + ".idleCycles")
348 .desc("Total number of cycles that the CPU has spent unscheduled due "
352 // Number of Instructions simulated
353 // --------------------------------
354 // Should probably be in Base CPU but need templated
355 // MaxThreads so put in here instead
358 .name(name() + ".committedInsts")
359 .desc("Number of Instructions Simulated");
362 .name(name() + ".committedInsts_total")
363 .desc("Number of Instructions Simulated");
366 .name(name() + ".cpi")
367 .desc("CPI: Cycles Per Instruction")
369 cpi
= simTicks
/ committedInsts
;
372 .name(name() + ".cpi_total")
373 .desc("CPI: Total CPI of All Threads")
375 totalCpi
= simTicks
/ totalCommittedInsts
;
378 .name(name() + ".ipc")
379 .desc("IPC: Instructions Per Cycle")
381 ipc
= committedInsts
/ simTicks
;
384 .name(name() + ".ipc_total")
385 .desc("IPC: Total IPC of All Threads")
387 totalIpc
= totalCommittedInsts
/ simTicks
;
391 template <class Impl
>
393 FullO3CPU
<Impl
>::getPort(const std::string
&if_name
, int idx
)
395 if (if_name
== "dcache_port")
396 return iew
.getDcachePort();
397 else if (if_name
== "icache_port")
398 return fetch
.getIcachePort();
400 panic("No Such Port\n");
403 template <class Impl
>
405 FullO3CPU
<Impl
>::tick()
407 DPRINTF(O3CPU
, "\n\nFullO3CPU: Ticking main, FullO3CPU.\n");
413 //Tick each of the stages
428 // Now advance the time buffers
429 timeBuffer
.advance();
431 fetchQueue
.advance();
432 decodeQueue
.advance();
433 renameQueue
.advance();
436 activityRec
.advance();
438 if (removeInstsThisCycle
) {
439 cleanUpRemovedInsts();
442 if (!tickEvent
.scheduled()) {
443 if (_status
== SwitchedOut
||
444 getState() == SimObject::DrainedTiming
) {
446 lastRunningCycle
= curTick
;
447 } else if (!activityRec
.active()) {
448 lastRunningCycle
= curTick
;
451 tickEvent
.schedule(curTick
+ cycles(1));
456 updateThreadPriority();
461 template <class Impl
>
463 FullO3CPU
<Impl
>::init()
465 if (!deferRegistration
) {
466 registerThreadContexts();
469 // Set inSyscall so that the CPU doesn't squash when initially
470 // setting up registers.
471 for (int i
= 0; i
< number_of_threads
; ++i
)
472 thread
[i
]->inSyscall
= true;
474 for (int tid
=0; tid
< number_of_threads
; tid
++) {
476 ThreadContext
*src_tc
= threadContexts
[tid
];
478 ThreadContext
*src_tc
= thread
[tid
]->getTC();
480 // Threads start in the Suspended State
481 if (src_tc
->status() != ThreadContext::Suspended
) {
486 TheISA::initCPU(src_tc
, src_tc
->readCpuId());
491 for (int i
= 0; i
< number_of_threads
; ++i
)
492 thread
[i
]->inSyscall
= false;
494 // Initialize stages.
500 commit
.setThreads(thread
);
503 template <class Impl
>
505 FullO3CPU
<Impl
>::activateThread(unsigned tid
)
507 list
<unsigned>::iterator isActive
= find(
508 activeThreads
.begin(), activeThreads
.end(), tid
);
510 if (isActive
== activeThreads
.end()) {
511 DPRINTF(O3CPU
, "[tid:%i]: Adding to active threads list\n",
514 activeThreads
.push_back(tid
);
518 template <class Impl
>
520 FullO3CPU
<Impl
>::deactivateThread(unsigned tid
)
522 //Remove From Active List, if Active
523 list
<unsigned>::iterator thread_it
=
524 find(activeThreads
.begin(), activeThreads
.end(), tid
);
526 if (thread_it
!= activeThreads
.end()) {
527 DPRINTF(O3CPU
,"[tid:%i]: Removing from active threads list\n",
529 activeThreads
.erase(thread_it
);
533 template <class Impl
>
535 FullO3CPU
<Impl
>::activateContext(int tid
, int delay
)
537 // Needs to set each stage to running as well.
539 DPRINTF(O3CPU
, "[tid:%i]: Scheduling thread context to activate "
540 "on cycle %d\n", tid
, curTick
+ cycles(delay
));
541 scheduleActivateThreadEvent(tid
, delay
);
546 if(lastActivatedCycle
< curTick
) {
547 scheduleTickEvent(delay
);
549 // Be sure to signal that there's some activity so the CPU doesn't
550 // deschedule itself.
551 activityRec
.activity();
552 fetch
.wakeFromQuiesce();
554 lastActivatedCycle
= curTick
;
560 template <class Impl
>
562 FullO3CPU
<Impl
>::deallocateContext(int tid
, int delay
)
564 // Schedule removal of thread data from CPU
566 DPRINTF(O3CPU
, "[tid:%i]: Scheduling thread context to deallocate "
567 "on cycle %d\n", tid
, curTick
+ cycles(delay
));
568 scheduleDeallocateContextEvent(tid
, delay
);
570 deactivateThread(tid
);
575 template <class Impl
>
577 FullO3CPU
<Impl
>::suspendContext(int tid
)
579 DPRINTF(O3CPU
,"[tid: %i]: Suspending Thread Context.\n", tid
);
580 unscheduleTickEvent();
583 //Remove From Active List, if Active
584 list<unsigned>::iterator isActive = find(
585 activeThreads.begin(), activeThreads.end(), tid);
587 if (isActive != activeThreads.end()) {
588 DPRINTF(O3CPU,"[tid:%i]: Removing from active threads list\n",
590 activeThreads.erase(isActive);
595 template <class Impl
>
597 FullO3CPU
<Impl
>::haltContext(int tid
)
599 DPRINTF(O3CPU
,"[tid:%i]: Halting Thread Context", tid
);
601 //Remove From Active List, if Active
602 list<unsigned>::iterator isActive = find(
603 activeThreads.begin(), activeThreads.end(), tid);
605 if (isActive != activeThreads.end()) {
606 DPRINTF(O3CPU,"[tid:%i]: Removing from active threads list\n",
608 activeThreads.erase(isActive);
615 template <class Impl
>
617 FullO3CPU
<Impl
>::insertThread(unsigned tid
)
619 DPRINTF(O3CPU
,"[tid:%i] Initializing thread into CPU");
620 // Will change now that the PC and thread state is internal to the CPU
621 // and not in the ThreadContext.
623 ThreadContext
*src_tc
= system
->threadContexts
[tid
];
625 ThreadContext
*src_tc
= tcBase(tid
);
628 //Bind Int Regs to Rename Map
629 for (int ireg
= 0; ireg
< TheISA::NumIntRegs
; ireg
++) {
630 PhysRegIndex phys_reg
= freeList
.getIntReg();
632 renameMap
[tid
].setEntry(ireg
,phys_reg
);
633 scoreboard
.setReg(phys_reg
);
636 //Bind Float Regs to Rename Map
637 for (int freg
= 0; freg
< TheISA::NumFloatRegs
; freg
++) {
638 PhysRegIndex phys_reg
= freeList
.getFloatReg();
640 renameMap
[tid
].setEntry(freg
,phys_reg
);
641 scoreboard
.setReg(phys_reg
);
644 //Copy Thread Data Into RegFile
645 //this->copyFromTC(tid);
648 setPC(src_tc
->readPC(), tid
);
649 setNextPC(src_tc
->readNextPC(), tid
);
650 #if THE_ISA != ALPHA_ISA
651 setNextNPC(src_tc
->readNextNPC(), tid
);
654 src_tc
->setStatus(ThreadContext::Active
);
656 activateContext(tid
,1);
658 //Reset ROB/IQ/LSQ Entries
659 commit
.rob
->resetEntries();
663 template <class Impl
>
665 FullO3CPU
<Impl
>::removeThread(unsigned tid
)
667 DPRINTF(O3CPU
,"[tid:%i] Removing thread context from CPU.\n", tid
);
669 // Copy Thread Data From RegFile
670 // If thread is suspended, it might be re-allocated
671 //this->copyToTC(tid);
673 // Unbind Int Regs from Rename Map
674 for (int ireg
= 0; ireg
< TheISA::NumIntRegs
; ireg
++) {
675 PhysRegIndex phys_reg
= renameMap
[tid
].lookup(ireg
);
677 scoreboard
.unsetReg(phys_reg
);
678 freeList
.addReg(phys_reg
);
681 // Unbind Float Regs from Rename Map
682 for (int freg
= 0; freg
< TheISA::NumFloatRegs
; freg
++) {
683 PhysRegIndex phys_reg
= renameMap
[tid
].lookup(freg
);
685 scoreboard
.unsetReg(phys_reg
);
686 freeList
.addReg(phys_reg
);
689 // Squash Throughout Pipeline
694 commit
.rob
->squash(commit
.rob
->readHeadInst(tid
)->seqNum
, tid
);
696 assert(iew
.ldstQueue
.getCount(tid
) == 0);
698 // Reset ROB/IQ/LSQ Entries
699 if (activeThreads
.size() >= 1) {
700 commit
.rob
->resetEntries();
706 template <class Impl
>
708 FullO3CPU
<Impl
>::activateWhenReady(int tid
)
710 DPRINTF(O3CPU
,"[tid:%i]: Checking if resources are available for incoming"
711 "(e.g. PhysRegs/ROB/IQ/LSQ) \n",
716 if (freeList
.numFreeIntRegs() >= TheISA::NumIntRegs
) {
717 DPRINTF(O3CPU
,"[tid:%i] Suspending thread due to not enough "
718 "Phys. Int. Regs.\n",
721 } else if (freeList
.numFreeFloatRegs() >= TheISA::NumFloatRegs
) {
722 DPRINTF(O3CPU
,"[tid:%i] Suspending thread due to not enough "
723 "Phys. Float. Regs.\n",
726 } else if (commit
.rob
->numFreeEntries() >=
727 commit
.rob
->entryAmount(activeThreads
.size() + 1)) {
728 DPRINTF(O3CPU
,"[tid:%i] Suspending thread due to not enough "
732 } else if (iew
.instQueue
.numFreeEntries() >=
733 iew
.instQueue
.entryAmount(activeThreads
.size() + 1)) {
734 DPRINTF(O3CPU
,"[tid:%i] Suspending thread due to not enough "
738 } else if (iew
.ldstQueue
.numFreeEntries() >=
739 iew
.ldstQueue
.entryAmount(activeThreads
.size() + 1)) {
740 DPRINTF(O3CPU
,"[tid:%i] Suspending thread due to not enough "
749 contextSwitch
= false;
751 cpuWaitList
.remove(tid
);
756 contextSwitch
= true;
758 //@todo: dont always add to waitlist
760 cpuWaitList
.push_back(tid
);
764 template <class Impl
>
766 FullO3CPU
<Impl
>::serialize(std::ostream
&os
)
768 SERIALIZE_ENUM(_status
);
769 BaseCPU::serialize(os
);
770 nameOut(os
, csprintf("%s.tickEvent", name()));
771 tickEvent
.serialize(os
);
773 // Use SimpleThread's ability to checkpoint to make it easier to
774 // write out the registers. Also make this static so it doesn't
775 // get instantiated multiple times (causes a panic in statistics).
776 static SimpleThread temp
;
778 for (int i
= 0; i
< thread
.size(); i
++) {
779 nameOut(os
, csprintf("%s.xc.%i", name(), i
));
780 temp
.copyTC(thread
[i
]->getTC());
785 template <class Impl
>
787 FullO3CPU
<Impl
>::unserialize(Checkpoint
*cp
, const std::string
§ion
)
789 UNSERIALIZE_ENUM(_status
);
790 BaseCPU::unserialize(cp
, section
);
791 tickEvent
.unserialize(cp
, csprintf("%s.tickEvent", section
));
793 // Use SimpleThread's ability to checkpoint to make it easier to
794 // read in the registers. Also make this static so it doesn't
795 // get instantiated multiple times (causes a panic in statistics).
796 static SimpleThread temp
;
798 for (int i
= 0; i
< thread
.size(); i
++) {
799 temp
.copyTC(thread
[i
]->getTC());
800 temp
.unserialize(cp
, csprintf("%s.xc.%i", section
, i
));
801 thread
[i
]->getTC()->copyArchRegs(temp
.getTC());
805 template <class Impl
>
807 FullO3CPU
<Impl
>::drain(Event
*drain_event
)
816 // Wake the CPU and record activity so everything can drain out if
817 // the CPU was not able to immediately drain.
818 if (getState() != SimObject::DrainedTiming
) {
819 // A bit of a hack...set the drainEvent after all the drain()
820 // calls have been made, that way if all of the stages drain
821 // immediately, the signalDrained() function knows not to call
822 // process on the drain event.
823 drainEvent
= drain_event
;
826 activityRec
.activity();
834 template <class Impl
>
836 FullO3CPU
<Impl
>::resume()
844 if (_status
== SwitchedOut
|| _status
== Idle
)
847 if (!tickEvent
.scheduled())
848 tickEvent
.schedule(curTick
);
850 changeState(SimObject::Timing
);
853 template <class Impl
>
855 FullO3CPU
<Impl
>::signalDrained()
857 if (++drainCount
== NumStages
) {
858 if (tickEvent
.scheduled())
861 changeState(SimObject::DrainedTiming
);
864 drainEvent
->process();
868 assert(drainCount
<= 5);
871 template <class Impl
>
873 FullO3CPU
<Impl
>::switchOut()
879 while (!removeList
.empty()) {
883 _status
= SwitchedOut
;
886 checker
->switchOut();
890 template <class Impl
>
892 FullO3CPU
<Impl
>::takeOverFrom(BaseCPU
*oldCPU
)
894 // Flush out any old data from the time buffers.
895 for (int i
= 0; i
< timeBuffer
.getSize(); ++i
) {
896 timeBuffer
.advance();
897 fetchQueue
.advance();
898 decodeQueue
.advance();
899 renameQueue
.advance();
905 BaseCPU::takeOverFrom(oldCPU
);
907 fetch
.takeOverFrom();
908 decode
.takeOverFrom();
909 rename
.takeOverFrom();
911 commit
.takeOverFrom();
913 assert(!tickEvent
.scheduled());
915 // @todo: Figure out how to properly select the tid to put onto
916 // the active threads list.
919 list
<unsigned>::iterator isActive
= find(
920 activeThreads
.begin(), activeThreads
.end(), tid
);
922 if (isActive
== activeThreads
.end()) {
923 //May Need to Re-code this if the delay variable is the delay
924 //needed for thread to activate
925 DPRINTF(O3CPU
, "Adding Thread %i to active threads list\n",
928 activeThreads
.push_back(tid
);
931 // Set all statuses to active, schedule the CPU's tick event.
932 // @todo: Fix up statuses so this is handled properly
933 for (int i
= 0; i
< threadContexts
.size(); ++i
) {
934 ThreadContext
*tc
= threadContexts
[i
];
935 if (tc
->status() == ThreadContext::Active
&& _status
!= Running
) {
937 tickEvent
.schedule(curTick
);
940 if (!tickEvent
.scheduled())
941 tickEvent
.schedule(curTick
);
944 template <class Impl
>
946 FullO3CPU
<Impl
>::readIntReg(int reg_idx
)
948 return regFile
.readIntReg(reg_idx
);
951 template <class Impl
>
953 FullO3CPU
<Impl
>::readFloatReg(int reg_idx
, int width
)
955 return regFile
.readFloatReg(reg_idx
, width
);
958 template <class Impl
>
960 FullO3CPU
<Impl
>::readFloatReg(int reg_idx
)
962 return regFile
.readFloatReg(reg_idx
);
965 template <class Impl
>
967 FullO3CPU
<Impl
>::readFloatRegBits(int reg_idx
, int width
)
969 return regFile
.readFloatRegBits(reg_idx
, width
);
972 template <class Impl
>
974 FullO3CPU
<Impl
>::readFloatRegBits(int reg_idx
)
976 return regFile
.readFloatRegBits(reg_idx
);
979 template <class Impl
>
981 FullO3CPU
<Impl
>::setIntReg(int reg_idx
, uint64_t val
)
983 regFile
.setIntReg(reg_idx
, val
);
986 template <class Impl
>
988 FullO3CPU
<Impl
>::setFloatReg(int reg_idx
, FloatReg val
, int width
)
990 regFile
.setFloatReg(reg_idx
, val
, width
);
993 template <class Impl
>
995 FullO3CPU
<Impl
>::setFloatReg(int reg_idx
, FloatReg val
)
997 regFile
.setFloatReg(reg_idx
, val
);
1000 template <class Impl
>
1002 FullO3CPU
<Impl
>::setFloatRegBits(int reg_idx
, FloatRegBits val
, int width
)
1004 regFile
.setFloatRegBits(reg_idx
, val
, width
);
1007 template <class Impl
>
1009 FullO3CPU
<Impl
>::setFloatRegBits(int reg_idx
, FloatRegBits val
)
1011 regFile
.setFloatRegBits(reg_idx
, val
);
1014 template <class Impl
>
1016 FullO3CPU
<Impl
>::readArchIntReg(int reg_idx
, unsigned tid
)
1018 PhysRegIndex phys_reg
= commitRenameMap
[tid
].lookup(reg_idx
);
1020 return regFile
.readIntReg(phys_reg
);
1023 template <class Impl
>
1025 FullO3CPU
<Impl
>::readArchFloatRegSingle(int reg_idx
, unsigned tid
)
1027 int idx
= reg_idx
+ TheISA::FP_Base_DepTag
;
1028 PhysRegIndex phys_reg
= commitRenameMap
[tid
].lookup(idx
);
1030 return regFile
.readFloatReg(phys_reg
);
1033 template <class Impl
>
1035 FullO3CPU
<Impl
>::readArchFloatRegDouble(int reg_idx
, unsigned tid
)
1037 int idx
= reg_idx
+ TheISA::FP_Base_DepTag
;
1038 PhysRegIndex phys_reg
= commitRenameMap
[tid
].lookup(idx
);
1040 return regFile
.readFloatReg(phys_reg
, 64);
1043 template <class Impl
>
1045 FullO3CPU
<Impl
>::readArchFloatRegInt(int reg_idx
, unsigned tid
)
1047 int idx
= reg_idx
+ TheISA::FP_Base_DepTag
;
1048 PhysRegIndex phys_reg
= commitRenameMap
[tid
].lookup(idx
);
1050 return regFile
.readFloatRegBits(phys_reg
);
1053 template <class Impl
>
1055 FullO3CPU
<Impl
>::setArchIntReg(int reg_idx
, uint64_t val
, unsigned tid
)
1057 PhysRegIndex phys_reg
= commitRenameMap
[tid
].lookup(reg_idx
);
1059 regFile
.setIntReg(phys_reg
, val
);
1062 template <class Impl
>
1064 FullO3CPU
<Impl
>::setArchFloatRegSingle(int reg_idx
, float val
, unsigned tid
)
1066 PhysRegIndex phys_reg
= commitRenameMap
[tid
].lookup(reg_idx
);
1068 regFile
.setFloatReg(phys_reg
, val
);
1071 template <class Impl
>
1073 FullO3CPU
<Impl
>::setArchFloatRegDouble(int reg_idx
, double val
, unsigned tid
)
1075 PhysRegIndex phys_reg
= commitRenameMap
[tid
].lookup(reg_idx
);
1077 regFile
.setFloatReg(phys_reg
, val
, 64);
1080 template <class Impl
>
1082 FullO3CPU
<Impl
>::setArchFloatRegInt(int reg_idx
, uint64_t val
, unsigned tid
)
1084 PhysRegIndex phys_reg
= commitRenameMap
[tid
].lookup(reg_idx
);
1086 regFile
.setFloatRegBits(phys_reg
, val
);
1089 template <class Impl
>
1091 FullO3CPU
<Impl
>::readPC(unsigned tid
)
1093 return commit
.readPC(tid
);
1096 template <class Impl
>
1098 FullO3CPU
<Impl
>::setPC(Addr new_PC
,unsigned tid
)
1100 commit
.setPC(new_PC
, tid
);
1103 template <class Impl
>
1105 FullO3CPU
<Impl
>::readNextPC(unsigned tid
)
1107 return commit
.readNextPC(tid
);
1110 template <class Impl
>
1112 FullO3CPU
<Impl
>::setNextPC(uint64_t val
,unsigned tid
)
1114 commit
.setNextPC(val
, tid
);
1117 #if THE_ISA != ALPHA_ISA
1118 template <class Impl
>
1120 FullO3CPU
<Impl
>::readNextNPC(unsigned tid
)
1122 return commit
.readNextNPC(tid
);
1125 template <class Impl
>
1127 FullO3CPU
<Impl
>::setNextNNPC(uint64_t val
,unsigned tid
)
1129 commit
.setNextNPC(val
, tid
);
1133 template <class Impl
>
1134 typename FullO3CPU
<Impl
>::ListIt
1135 FullO3CPU
<Impl
>::addInst(DynInstPtr
&inst
)
1137 instList
.push_back(inst
);
1139 return --(instList
.end());
1142 template <class Impl
>
1144 FullO3CPU
<Impl
>::instDone(unsigned tid
)
1146 // Keep an instruction count.
1147 thread
[tid
]->numInst
++;
1148 thread
[tid
]->numInsts
++;
1149 committedInsts
[tid
]++;
1150 totalCommittedInsts
++;
1152 // Check for instruction-count-based events.
1153 comInstEventQueue
[tid
]->serviceEvents(thread
[tid
]->numInst
);
1156 template <class Impl
>
1158 FullO3CPU
<Impl
>::addToRemoveList(DynInstPtr
&inst
)
1160 removeInstsThisCycle
= true;
1162 removeList
.push(inst
->getInstListIt());
1165 template <class Impl
>
1167 FullO3CPU
<Impl
>::removeFrontInst(DynInstPtr
&inst
)
1169 DPRINTF(O3CPU
, "Removing committed instruction [tid:%i] PC %#x "
1171 inst
->threadNumber
, inst
->readPC(), inst
->seqNum
);
1173 removeInstsThisCycle
= true;
1175 // Remove the front instruction.
1176 removeList
.push(inst
->getInstListIt());
1179 template <class Impl
>
1181 FullO3CPU
<Impl
>::removeInstsNotInROB(unsigned tid
)
1183 DPRINTF(O3CPU
, "Thread %i: Deleting instructions from instruction"
1188 bool rob_empty
= false;
1190 if (instList
.empty()) {
1192 } else if (rob
.isEmpty(/*tid*/)) {
1193 DPRINTF(O3CPU
, "ROB is empty, squashing all insts.\n");
1194 end_it
= instList
.begin();
1197 end_it
= (rob
.readTailInst(tid
))->getInstListIt();
1198 DPRINTF(O3CPU
, "ROB is not empty, squashing insts not in ROB.\n");
1201 removeInstsThisCycle
= true;
1203 ListIt inst_it
= instList
.end();
1207 // Walk through the instruction list, removing any instructions
1208 // that were inserted after the given instruction iterator, end_it.
1209 while (inst_it
!= end_it
) {
1210 assert(!instList
.empty());
1212 squashInstIt(inst_it
, tid
);
1217 // If the ROB was empty, then we actually need to remove the first
1218 // instruction as well.
1220 squashInstIt(inst_it
, tid
);
1224 template <class Impl
>
1226 FullO3CPU
<Impl
>::removeInstsUntil(const InstSeqNum
&seq_num
,
1229 assert(!instList
.empty());
1231 removeInstsThisCycle
= true;
1233 ListIt inst_iter
= instList
.end();
1237 DPRINTF(O3CPU
, "Deleting instructions from instruction "
1238 "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1239 tid
, seq_num
, (*inst_iter
)->seqNum
);
1241 while ((*inst_iter
)->seqNum
> seq_num
) {
1243 bool break_loop
= (inst_iter
== instList
.begin());
1245 squashInstIt(inst_iter
, tid
);
1254 template <class Impl
>
1256 FullO3CPU
<Impl
>::squashInstIt(const ListIt
&instIt
, const unsigned &tid
)
1258 if ((*instIt
)->threadNumber
== tid
) {
1259 DPRINTF(O3CPU
, "Squashing instruction, "
1260 "[tid:%i] [sn:%lli] PC %#x\n",
1261 (*instIt
)->threadNumber
,
1263 (*instIt
)->readPC());
1265 // Mark it as squashed.
1266 (*instIt
)->setSquashed();
1268 // @todo: Formulate a consistent method for deleting
1269 // instructions from the instruction list
1270 // Remove the instruction from the list.
1271 removeList
.push(instIt
);
1275 template <class Impl
>
1277 FullO3CPU
<Impl
>::cleanUpRemovedInsts()
1279 while (!removeList
.empty()) {
1280 DPRINTF(O3CPU
, "Removing instruction, "
1281 "[tid:%i] [sn:%lli] PC %#x\n",
1282 (*removeList
.front())->threadNumber
,
1283 (*removeList
.front())->seqNum
,
1284 (*removeList
.front())->readPC());
1286 instList
.erase(removeList
.front());
1291 removeInstsThisCycle
= false;
1294 template <class Impl>
1296 FullO3CPU<Impl>::removeAllInsts()
1301 template <class Impl
>
1303 FullO3CPU
<Impl
>::dumpInsts()
1307 ListIt inst_list_it
= instList
.begin();
1309 cprintf("Dumping Instruction List\n");
1311 while (inst_list_it
!= instList
.end()) {
1312 cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1314 num
, (*inst_list_it
)->readPC(), (*inst_list_it
)->threadNumber
,
1315 (*inst_list_it
)->seqNum
, (*inst_list_it
)->isIssued(),
1316 (*inst_list_it
)->isSquashed());
1322 template <class Impl>
1324 FullO3CPU<Impl>::wakeDependents(DynInstPtr &inst)
1326 iew.wakeDependents(inst);
1329 template <class Impl
>
1331 FullO3CPU
<Impl
>::wakeCPU()
1333 if (activityRec
.active() || tickEvent
.scheduled()) {
1334 DPRINTF(Activity
, "CPU already running.\n");
1338 DPRINTF(Activity
, "Waking up CPU\n");
1340 idleCycles
+= (curTick
- 1) - lastRunningCycle
;
1342 tickEvent
.schedule(curTick
);
1345 template <class Impl
>
1347 FullO3CPU
<Impl
>::getFreeTid()
1349 for (int i
=0; i
< numThreads
; i
++) {
1359 template <class Impl
>
1361 FullO3CPU
<Impl
>::doContextSwitch()
1363 if (contextSwitch
) {
1365 //ADD CODE TO DEACTIVE THREAD HERE (???)
1367 for (int tid
=0; tid
< cpuWaitList
.size(); tid
++) {
1368 activateWhenReady(tid
);
1371 if (cpuWaitList
.size() == 0)
1372 contextSwitch
= true;
1376 template <class Impl
>
1378 FullO3CPU
<Impl
>::updateThreadPriority()
1380 if (activeThreads
.size() > 1)
1382 //DEFAULT TO ROUND ROBIN SCHEME
1383 //e.g. Move highest priority to end of thread list
1384 list
<unsigned>::iterator list_begin
= activeThreads
.begin();
1385 list
<unsigned>::iterator list_end
= activeThreads
.end();
1387 unsigned high_thread
= *list_begin
;
1389 activeThreads
.erase(list_begin
);
1391 activeThreads
.push_back(high_thread
);
1395 // Forward declaration of FullO3CPU.
1396 template class FullO3CPU
<O3CPUImpl
>;