2 * Copyright (c) 2007 MIPS Technologies, Inc.
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.
28 * Authors: Korey Sewell
34 #include "arch/utility.hh"
35 #include "config/full_system.hh"
36 #include "config/the_isa.hh"
37 #include "cpu/activity.hh"
38 #include "cpu/base.hh"
39 #include "cpu/exetrace.hh"
40 #include "cpu/inorder/cpu.hh"
41 #include "cpu/inorder/first_stage.hh"
42 #include "cpu/inorder/inorder_dyn_inst.hh"
43 #include "cpu/inorder/pipeline_traits.hh"
44 #include "cpu/inorder/resource_pool.hh"
45 #include "cpu/inorder/resources/resource_list.hh"
46 #include "cpu/inorder/thread_context.hh"
47 #include "cpu/inorder/thread_state.hh"
48 #include "cpu/simple_thread.hh"
49 #include "cpu/thread_context.hh"
50 #include "mem/translating_port.hh"
51 #include "params/InOrderCPU.hh"
52 #include "sim/process.hh"
53 #include "sim/stat_control.hh"
56 #include "cpu/quiesce_event.hh"
57 #include "sim/system.hh"
60 #if THE_ISA == ALPHA_ISA
61 #include "arch/alpha/osfpal.hh"
65 using namespace TheISA
;
66 using namespace ThePipeline
;
68 InOrderCPU::TickEvent::TickEvent(InOrderCPU
*c
)
69 : Event(CPU_Tick_Pri
), cpu(c
)
74 InOrderCPU::TickEvent::process()
81 InOrderCPU::TickEvent::description()
83 return "InOrderCPU tick event";
86 InOrderCPU::CPUEvent::CPUEvent(InOrderCPU
*_cpu
, CPUEventType e_type
,
87 Fault fault
, ThreadID _tid
, DynInstPtr inst
,
88 unsigned event_pri_offset
)
89 : Event(Event::Priority((unsigned int)CPU_Tick_Pri
+ event_pri_offset
)),
92 setEvent(e_type
, fault
, _tid
, inst
);
96 std::string
InOrderCPU::eventNames
[NumCPUEvents
] =
99 "ActivateNextReadyThread",
105 "SquashFromMemStall",
110 InOrderCPU::CPUEvent::process()
112 switch (cpuEventType
)
115 cpu
->activateThread(tid
);
118 case ActivateNextReadyThread
:
119 cpu
->activateNextReadyThread();
122 case DeactivateThread
:
123 cpu
->deactivateThread(tid
);
127 cpu
->haltThread(tid
);
131 cpu
->suspendThread(tid
);
134 case SquashFromMemStall
:
135 cpu
->squashDueToMemStall(inst
->squashingStage
, inst
->seqNum
, tid
);
139 cpu
->trapCPU(fault
, tid
);
143 fatal("Unrecognized Event Type %s", eventNames
[cpuEventType
]);
146 cpu
->cpuEventRemoveList
.push(this);
152 InOrderCPU::CPUEvent::description()
154 return "InOrderCPU event";
158 InOrderCPU::CPUEvent::scheduleEvent(int delay
)
161 mainEventQueue
.reschedule(this, cpu
->nextCycle(curTick
+
163 else if (!scheduled())
164 mainEventQueue
.schedule(this, cpu
->nextCycle(curTick
+
169 InOrderCPU::CPUEvent::unscheduleEvent()
175 InOrderCPU::InOrderCPU(Params
*params
)
177 cpu_id(params
->cpu_id
),
182 removeInstsThisCycle(false),
183 activityRec(params
->name
, NumStages
, 10, params
->activity
),
185 system(params
->system
),
186 physmem(system
->physmem
),
187 #endif // FULL_SYSTEM
193 deferRegistration(false/*params->deferRegistration*/),
194 stageTracing(params
->stageTracing
),
197 ThreadID active_threads
;
200 resPool
= new ResourcePool(this, params
);
202 // Resize for Multithreading CPUs
203 thread
.resize(numThreads
);
208 active_threads
= params
->workload
.size();
210 if (active_threads
> MaxThreads
) {
211 panic("Workload Size too large. Increase the 'MaxThreads'"
212 "in your InOrder implementation or "
213 "edit your workload size.");
217 if (active_threads
> 1) {
218 threadModel
= (InOrderCPU::ThreadModel
) params
->threadModel
;
220 if (threadModel
== SMT
) {
221 DPRINTF(InOrderCPU
, "Setting Thread Model to SMT.\n");
222 } else if (threadModel
== SwitchOnCacheMiss
) {
223 DPRINTF(InOrderCPU
, "Setting Thread Model to "
224 "Switch On Cache Miss\n");
228 threadModel
= Single
;
235 // Bind the fetch & data ports from the resource pool.
236 fetchPortIdx
= resPool
->getPortIdx(params
->fetchMemPort
);
237 if (fetchPortIdx
== 0) {
238 fatal("Unable to find port to fetch instructions from.\n");
241 dataPortIdx
= resPool
->getPortIdx(params
->dataMemPort
);
242 if (dataPortIdx
== 0) {
243 fatal("Unable to find port for data.\n");
246 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
) {
248 // SMT is not supported in FS mode yet.
249 assert(numThreads
== 1);
250 thread
[tid
] = new Thread(this, 0);
252 if (tid
< (ThreadID
)params
->workload
.size()) {
253 DPRINTF(InOrderCPU
, "Workload[%i] process is %#x\n",
254 tid
, params
->workload
[tid
]->prog_fname
);
256 new Thread(this, tid
, params
->workload
[tid
]);
258 //Allocate Empty thread so M5 can use later
259 //when scheduling threads to CPU
260 Process
* dummy_proc
= params
->workload
[0];
261 thread
[tid
] = new Thread(this, tid
, dummy_proc
);
264 // Eventually set this with parameters...
268 // Setup the TC that will serve as the interface to the threads/CPU.
269 InOrderThreadContext
*tc
= new InOrderThreadContext
;
271 tc
->thread
= thread
[tid
];
273 // Give the thread the TC.
274 thread
[tid
]->tc
= tc
;
275 thread
[tid
]->setFuncExeInst(0);
276 globalSeqNum
[tid
] = 1;
278 // Add the TC to the CPU's list of TC's.
279 this->threadContexts
.push_back(tc
);
282 // Initialize TimeBuffer Stage Queues
283 for (int stNum
=0; stNum
< NumStages
- 1; stNum
++) {
284 stageQueue
[stNum
] = new StageQueue(NumStages
, NumStages
);
285 stageQueue
[stNum
]->id(stNum
);
289 // Set Up Pipeline Stages
290 for (int stNum
=0; stNum
< NumStages
; stNum
++) {
292 pipelineStage
[stNum
] = new FirstStage(params
, stNum
);
294 pipelineStage
[stNum
] = new PipelineStage(params
, stNum
);
296 pipelineStage
[stNum
]->setCPU(this);
297 pipelineStage
[stNum
]->setActiveThreads(&activeThreads
);
298 pipelineStage
[stNum
]->setTimeBuffer(&timeBuffer
);
300 // Take Care of 1st/Nth stages
302 pipelineStage
[stNum
]->setPrevStageQueue(stageQueue
[stNum
- 1]);
303 if (stNum
< NumStages
- 1)
304 pipelineStage
[stNum
]->setNextStageQueue(stageQueue
[stNum
]);
307 // Initialize thread specific variables
308 for (ThreadID tid
= 0; tid
< numThreads
; tid
++) {
309 archRegDepMap
[tid
].setCPU(this);
311 nonSpecInstActive
[tid
] = false;
312 nonSpecSeqNum
[tid
] = 0;
314 squashSeqNum
[tid
] = MaxAddr
;
315 lastSquashCycle
[tid
] = 0;
317 memset(intRegs
[tid
], 0, sizeof(intRegs
[tid
]));
318 memset(floatRegs
.i
[tid
], 0, sizeof(floatRegs
.i
[tid
]));
321 isa
[tid
].expandForMultithreading(numThreads
, 1/*numVirtProcs*/);
323 // Define dummy instructions and resource requests to be used.
324 dummyInst
[tid
] = new InOrderDynInst(this,
330 dummyReq
[tid
] = new ResourceRequest(resPool
->getResource(0),
338 dummyReqInst
= new InOrderDynInst(this, NULL
, 0, 0, 0);
339 dummyReqInst
->setSquashed();
341 dummyBufferInst
= new InOrderDynInst(this, NULL
, 0, 0, 0);
342 dummyBufferInst
->setSquashed();
344 lastRunningCycle
= curTick
;
346 // Reset CPU to reset state.
348 Fault resetFault
= new ResetFault();
349 resetFault
->invoke(tcBase());
354 dummyBufferInst
->resetInstCount();
356 // Schedule First Tick Event, CPU will reschedule itself from here on out.
357 scheduleTickEvent(0);
360 InOrderCPU::~InOrderCPU()
367 InOrderCPU::regStats()
369 /* Register the Resource Pool's stats here.*/
374 .name(name() + ".maxResReqCount")
375 .desc("Maximum number of live resource requests in CPU")
376 .prereq(maxResReqCount
);
379 /* Register for each Pipeline Stage */
380 for (int stage_num
=0; stage_num
< ThePipeline::NumStages
; stage_num
++) {
381 pipelineStage
[stage_num
]->regStats();
384 /* Register any of the InOrderCPU's stats here.*/
386 .name(name() + ".instsPerContextSwitch")
387 .desc("Instructions Committed Per Context Switch")
388 .prereq(instsPerCtxtSwitch
);
391 .name(name() + ".contextSwitches")
392 .desc("Number of context switches");
395 .name(name() + ".comLoads")
396 .desc("Number of Load instructions committed");
399 .name(name() + ".comStores")
400 .desc("Number of Store instructions committed");
403 .name(name() + ".comBranches")
404 .desc("Number of Branches instructions committed");
407 .name(name() + ".comNops")
408 .desc("Number of Nop instructions committed");
411 .name(name() + ".comNonSpec")
412 .desc("Number of Non-Speculative instructions committed");
415 .name(name() + ".comInts")
416 .desc("Number of Integer instructions committed");
419 .name(name() + ".comFloats")
420 .desc("Number of Floating Point instructions committed");
423 .name(name() + ".timesIdled")
424 .desc("Number of times that the entire CPU went into an idle state and"
425 " unscheduled itself")
429 .name(name() + ".idleCycles")
430 .desc("Number of cycles cpu's stages were not processed");
433 .name(name() + ".runCycles")
434 .desc("Number of cycles cpu stages are processed.");
437 .name(name() + ".activity")
438 .desc("Percentage of cycles cpu is active")
440 activity
= (runCycles
/ numCycles
) * 100;
444 .name(name() + ".threadCycles")
445 .desc("Total Number of Cycles A Thread Was Active in CPU (Per-Thread)");
448 .name(name() + ".smtCycles")
449 .desc("Total number of cycles that the CPU was in SMT-mode");
453 .name(name() + ".committedInsts")
454 .desc("Number of Instructions Simulated (Per-Thread)");
458 .name(name() + ".smtCommittedInsts")
459 .desc("Number of SMT Instructions Simulated (Per-Thread)");
462 .name(name() + ".committedInsts_total")
463 .desc("Number of Instructions Simulated (Total)");
466 .name(name() + ".cpi")
467 .desc("CPI: Cycles Per Instruction (Per-Thread)")
469 cpi
= numCycles
/ committedInsts
;
472 .name(name() + ".smt_cpi")
473 .desc("CPI: Total SMT-CPI")
475 smtCpi
= smtCycles
/ smtCommittedInsts
;
478 .name(name() + ".cpi_total")
479 .desc("CPI: Total CPI of All Threads")
481 totalCpi
= numCycles
/ totalCommittedInsts
;
484 .name(name() + ".ipc")
485 .desc("IPC: Instructions Per Cycle (Per-Thread)")
487 ipc
= committedInsts
/ numCycles
;
490 .name(name() + ".smt_ipc")
491 .desc("IPC: Total SMT-IPC")
493 smtIpc
= smtCommittedInsts
/ smtCycles
;
496 .name(name() + ".ipc_total")
497 .desc("IPC: Total IPC of All Threads")
499 totalIpc
= totalCommittedInsts
/ numCycles
;
508 DPRINTF(InOrderCPU
, "\n\nInOrderCPU: Ticking main, InOrderCPU.\n");
512 bool pipes_idle
= true;
514 //Tick each of the stages
515 for (int stNum
=NumStages
- 1; stNum
>= 0 ; stNum
--) {
516 pipelineStage
[stNum
]->tick();
518 pipes_idle
= pipes_idle
&& pipelineStage
[stNum
]->idle
;
526 // Now advance the time buffers one tick
527 timeBuffer
.advance();
528 for (int sqNum
=0; sqNum
< NumStages
- 1; sqNum
++) {
529 stageQueue
[sqNum
]->advance();
531 activityRec
.advance();
533 // Any squashed requests, events, or insts then remove them now
534 cleanUpRemovedReqs();
535 cleanUpRemovedEvents();
536 cleanUpRemovedInsts();
538 // Re-schedule CPU for this cycle
539 if (!tickEvent
.scheduled()) {
540 if (_status
== SwitchedOut
) {
542 lastRunningCycle
= curTick
;
543 } else if (!activityRec
.active()) {
544 DPRINTF(InOrderCPU
, "sleeping CPU.\n");
545 lastRunningCycle
= curTick
;
548 //Tick next_tick = curTick + cycles(1);
549 //tickEvent.schedule(next_tick);
550 mainEventQueue
.schedule(&tickEvent
, nextCycle(curTick
+ 1));
551 DPRINTF(InOrderCPU
, "Scheduled CPU for next tick @ %i.\n",
552 nextCycle(curTick
+ 1));
557 updateThreadPriority();
564 if (!deferRegistration
) {
565 registerThreadContexts();
568 // Set inSyscall so that the CPU doesn't squash when initially
569 // setting up registers.
570 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
)
571 thread
[tid
]->inSyscall
= true;
574 for (ThreadID tid
= 0; tid
< numThreads
; tid
++) {
575 ThreadContext
*src_tc
= threadContexts
[tid
];
576 TheISA::initCPU(src_tc
, src_tc
->contextId());
581 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
)
582 thread
[tid
]->inSyscall
= false;
584 // Call Initializiation Routine for Resource Pool
591 for (int i
= 0; i
< numThreads
; i
++) {
592 isa
[i
].reset(coreType
, numThreads
,
593 1/*numVirtProcs*/, dynamic_cast<BaseCPU
*>(this));
598 InOrderCPU::getPort(const std::string
&if_name
, int idx
)
600 return resPool
->getPort(if_name
, idx
);
605 InOrderCPU::hwrei(ThreadID tid
)
607 panic("hwrei: Unimplemented");
614 InOrderCPU::simPalCheck(int palFunc
, ThreadID tid
)
616 panic("simPalCheck: Unimplemented");
623 InOrderCPU::getInterrupts()
625 // Check if there are any outstanding interrupts
626 return this->interrupts
->getInterrupt(this->threadContexts
[0]);
631 InOrderCPU::processInterrupts(Fault interrupt
)
633 // Check for interrupts here. For now can copy the code that
634 // exists within isa_fullsys_traits.hh. Also assume that thread 0
635 // is the one that handles the interrupts.
636 // @todo: Possibly consolidate the interrupt checking code.
637 // @todo: Allow other threads to handle interrupts.
639 assert(interrupt
!= NoFault
);
640 this->interrupts
->updateIntrInfo(this->threadContexts
[0]);
642 DPRINTF(InOrderCPU
, "Interrupt %s being handled\n", interrupt
->name());
643 this->trap(interrupt
, 0);
648 InOrderCPU::updateMemPorts()
650 // Update all ThreadContext's memory ports (Functional/Virtual
652 ThreadID size
= thread
.size();
653 for (ThreadID i
= 0; i
< size
; ++i
)
654 thread
[i
]->connectMemPorts(thread
[i
]->getTC());
659 InOrderCPU::trap(Fault fault
, ThreadID tid
, int delay
)
661 //@ Squash Pipeline during TRAP
662 scheduleCpuEvent(Trap
, fault
, tid
, dummyInst
[tid
], delay
);
666 InOrderCPU::trapCPU(Fault fault
, ThreadID tid
)
668 fault
->invoke(tcBase(tid
));
672 InOrderCPU::squashFromMemStall(DynInstPtr inst
, ThreadID tid
, int delay
)
674 scheduleCpuEvent(SquashFromMemStall
, NoFault
, tid
, inst
, delay
);
679 InOrderCPU::squashDueToMemStall(int stage_num
, InstSeqNum seq_num
,
682 DPRINTF(InOrderCPU
, "Squashing Pipeline Stages Due to Memory Stall...\n");
684 // Squash all instructions in each stage including
685 // instruction that caused the squash (seq_num - 1)
686 // NOTE: The stage bandwidth needs to be cleared so thats why
687 // the stalling instruction is squashed as well. The stalled
688 // instruction is previously placed in another intermediate buffer
689 // while it's stall is being handled.
690 InstSeqNum squash_seq_num
= seq_num
- 1;
692 for (int stNum
=stage_num
; stNum
>= 0 ; stNum
--) {
693 pipelineStage
[stNum
]->squashDueToMemStall(squash_seq_num
, tid
);
698 InOrderCPU::scheduleCpuEvent(CPUEventType c_event
, Fault fault
,
699 ThreadID tid
, DynInstPtr inst
,
700 unsigned delay
, unsigned event_pri_offset
)
702 CPUEvent
*cpu_event
= new CPUEvent(this, c_event
, fault
, tid
, inst
,
705 Tick sked_tick
= nextCycle(curTick
+ ticks(delay
));
707 DPRINTF(InOrderCPU
, "Scheduling CPU Event (%s) for cycle %i, [tid:%i].\n",
708 eventNames
[c_event
], curTick
+ delay
, tid
);
709 mainEventQueue
.schedule(cpu_event
, sked_tick
);
711 cpu_event
->process();
712 cpuEventRemoveList
.push(cpu_event
);
715 // Broadcast event to the Resource Pool
716 // Need to reset tid just in case this is a dummy instruction
718 resPool
->scheduleEvent(c_event
, inst
, 0, 0, tid
);
722 InOrderCPU::isThreadActive(ThreadID tid
)
724 list
<ThreadID
>::iterator isActive
=
725 std::find(activeThreads
.begin(), activeThreads
.end(), tid
);
727 return (isActive
!= activeThreads
.end());
731 InOrderCPU::isThreadReady(ThreadID tid
)
733 list
<ThreadID
>::iterator isReady
=
734 std::find(readyThreads
.begin(), readyThreads
.end(), tid
);
736 return (isReady
!= readyThreads
.end());
740 InOrderCPU::isThreadSuspended(ThreadID tid
)
742 list
<ThreadID
>::iterator isSuspended
=
743 std::find(suspendedThreads
.begin(), suspendedThreads
.end(), tid
);
745 return (isSuspended
!= suspendedThreads
.end());
749 InOrderCPU::activateNextReadyThread()
751 if (readyThreads
.size() >= 1) {
752 ThreadID ready_tid
= readyThreads
.front();
754 // Activate in Pipeline
755 activateThread(ready_tid
);
757 // Activate in Resource Pool
758 resPool
->activateAll(ready_tid
);
760 list
<ThreadID
>::iterator ready_it
=
761 std::find(readyThreads
.begin(), readyThreads
.end(), ready_tid
);
762 readyThreads
.erase(ready_it
);
765 "Attempting to activate new thread, but No Ready Threads to"
768 "Unable to switch to next active thread.\n");
773 InOrderCPU::activateThread(ThreadID tid
)
775 if (isThreadSuspended(tid
)) {
777 "Removing [tid:%i] from suspended threads list.\n", tid
);
779 list
<ThreadID
>::iterator susp_it
=
780 std::find(suspendedThreads
.begin(), suspendedThreads
.end(),
782 suspendedThreads
.erase(susp_it
);
785 if (threadModel
== SwitchOnCacheMiss
&&
786 numActiveThreads() == 1) {
788 "Ignoring activation of [tid:%i], since [tid:%i] is "
789 "already running.\n", tid
, activeThreadId());
791 DPRINTF(InOrderCPU
,"Placing [tid:%i] on ready threads list\n",
794 readyThreads
.push_back(tid
);
796 } else if (!isThreadActive(tid
)) {
798 "Adding [tid:%i] to active threads list.\n", tid
);
799 activeThreads
.push_back(tid
);
801 activateThreadInPipeline(tid
);
803 thread
[tid
]->lastActivate
= curTick
;
805 tcBase(tid
)->setStatus(ThreadContext::Active
);
814 InOrderCPU::activateThreadInPipeline(ThreadID tid
)
816 for (int stNum
=0; stNum
< NumStages
; stNum
++) {
817 pipelineStage
[stNum
]->activateThread(tid
);
822 InOrderCPU::deactivateContext(ThreadID tid
, int delay
)
824 DPRINTF(InOrderCPU
,"[tid:%i]: Deactivating ...\n", tid
);
826 scheduleCpuEvent(DeactivateThread
, NoFault
, tid
, dummyInst
[tid
], delay
);
828 // Be sure to signal that there's some activity so the CPU doesn't
829 // deschedule itself.
830 activityRec
.activity();
836 InOrderCPU::deactivateThread(ThreadID tid
)
838 DPRINTF(InOrderCPU
, "[tid:%i]: Calling deactivate thread.\n", tid
);
840 if (isThreadActive(tid
)) {
841 DPRINTF(InOrderCPU
,"[tid:%i]: Removing from active threads list\n",
843 list
<ThreadID
>::iterator thread_it
=
844 std::find(activeThreads
.begin(), activeThreads
.end(), tid
);
846 removePipelineStalls(*thread_it
);
848 activeThreads
.erase(thread_it
);
850 // Ideally, this should be triggered from the
851 // suspendContext/Thread functions
852 tcBase(tid
)->setStatus(ThreadContext::Suspended
);
855 assert(!isThreadActive(tid
));
859 InOrderCPU::removePipelineStalls(ThreadID tid
)
861 DPRINTF(InOrderCPU
,"[tid:%i]: Removing all pipeline stalls\n",
864 for (int stNum
= 0; stNum
< NumStages
; stNum
++) {
865 pipelineStage
[stNum
]->removeStalls(tid
);
871 InOrderCPU::updateThreadPriority()
873 if (activeThreads
.size() > 1)
875 //DEFAULT TO ROUND ROBIN SCHEME
876 //e.g. Move highest priority to end of thread list
877 list
<ThreadID
>::iterator list_begin
= activeThreads
.begin();
878 list
<ThreadID
>::iterator list_end
= activeThreads
.end();
880 unsigned high_thread
= *list_begin
;
882 activeThreads
.erase(list_begin
);
884 activeThreads
.push_back(high_thread
);
889 InOrderCPU::tickThreadStats()
891 /** Keep track of cycles that each thread is active */
892 list
<ThreadID
>::iterator thread_it
= activeThreads
.begin();
893 while (thread_it
!= activeThreads
.end()) {
894 threadCycles
[*thread_it
]++;
898 // Keep track of cycles where SMT is active
899 if (activeThreads
.size() > 1) {
905 InOrderCPU::activateContext(ThreadID tid
, int delay
)
907 DPRINTF(InOrderCPU
,"[tid:%i]: Activating ...\n", tid
);
910 scheduleCpuEvent(ActivateThread
, NoFault
, tid
, dummyInst
[tid
], delay
);
912 // Be sure to signal that there's some activity so the CPU doesn't
913 // deschedule itself.
914 activityRec
.activity();
920 InOrderCPU::activateNextReadyContext(int delay
)
922 DPRINTF(InOrderCPU
,"Activating next ready thread\n");
924 // NOTE: Add 5 to the event priority so that we always activate
925 // threads after we've finished deactivating, squashing,etc.
927 scheduleCpuEvent(ActivateNextReadyThread
, NoFault
, 0/*tid*/, dummyInst
[0],
930 // Be sure to signal that there's some activity so the CPU doesn't
931 // deschedule itself.
932 activityRec
.activity();
938 InOrderCPU::haltContext(ThreadID tid
, int delay
)
940 DPRINTF(InOrderCPU
, "[tid:%i]: Calling Halt Context...\n", tid
);
942 scheduleCpuEvent(HaltThread
, NoFault
, tid
, dummyInst
[tid
], delay
);
944 activityRec
.activity();
948 InOrderCPU::haltThread(ThreadID tid
)
950 DPRINTF(InOrderCPU
, "[tid:%i]: Placing on Halted Threads List...\n", tid
);
951 deactivateThread(tid
);
952 squashThreadInPipeline(tid
);
953 haltedThreads
.push_back(tid
);
955 tcBase(tid
)->setStatus(ThreadContext::Halted
);
957 if (threadModel
== SwitchOnCacheMiss
) {
958 activateNextReadyContext();
963 InOrderCPU::suspendContext(ThreadID tid
, int delay
)
965 scheduleCpuEvent(SuspendThread
, NoFault
, tid
, dummyInst
[tid
], delay
);
969 InOrderCPU::suspendThread(ThreadID tid
)
971 DPRINTF(InOrderCPU
, "[tid:%i]: Placing on Suspended Threads List...\n",
973 deactivateThread(tid
);
974 suspendedThreads
.push_back(tid
);
975 thread
[tid
]->lastSuspend
= curTick
;
977 tcBase(tid
)->setStatus(ThreadContext::Suspended
);
981 InOrderCPU::squashThreadInPipeline(ThreadID tid
)
983 //Squash all instructions in each stage
984 for (int stNum
=NumStages
- 1; stNum
>= 0 ; stNum
--) {
985 pipelineStage
[stNum
]->squash(0 /*seq_num*/, tid
);
990 InOrderCPU::getPipeStage(int stage_num
)
992 return pipelineStage
[stage_num
];
996 InOrderCPU::readPC(ThreadID tid
)
1003 InOrderCPU::setPC(Addr new_PC
, ThreadID tid
)
1010 InOrderCPU::readNextPC(ThreadID tid
)
1017 InOrderCPU::setNextPC(uint64_t new_NPC
, ThreadID tid
)
1019 nextPC
[tid
] = new_NPC
;
1024 InOrderCPU::readNextNPC(ThreadID tid
)
1026 return nextNPC
[tid
];
1031 InOrderCPU::setNextNPC(uint64_t new_NNPC
, ThreadID tid
)
1033 nextNPC
[tid
] = new_NNPC
;
1037 InOrderCPU::readIntReg(int reg_idx
, ThreadID tid
)
1039 return intRegs
[tid
][reg_idx
];
1043 InOrderCPU::readFloatReg(int reg_idx
, ThreadID tid
)
1045 return floatRegs
.f
[tid
][reg_idx
];
1049 InOrderCPU::readFloatRegBits(int reg_idx
, ThreadID tid
)
1051 return floatRegs
.i
[tid
][reg_idx
];
1055 InOrderCPU::setIntReg(int reg_idx
, uint64_t val
, ThreadID tid
)
1057 intRegs
[tid
][reg_idx
] = val
;
1062 InOrderCPU::setFloatReg(int reg_idx
, FloatReg val
, ThreadID tid
)
1064 floatRegs
.f
[tid
][reg_idx
] = val
;
1069 InOrderCPU::setFloatRegBits(int reg_idx
, FloatRegBits val
, ThreadID tid
)
1071 floatRegs
.i
[tid
][reg_idx
] = val
;
1075 InOrderCPU::readRegOtherThread(unsigned reg_idx
, ThreadID tid
)
1077 // If Default value is set, then retrieve target thread
1078 if (tid
== InvalidThreadID
) {
1079 tid
= TheISA::getTargetThread(tcBase(tid
));
1082 if (reg_idx
< FP_Base_DepTag
) {
1083 // Integer Register File
1084 return readIntReg(reg_idx
, tid
);
1085 } else if (reg_idx
< Ctrl_Base_DepTag
) {
1086 // Float Register File
1087 reg_idx
-= FP_Base_DepTag
;
1088 return readFloatRegBits(reg_idx
, tid
);
1090 reg_idx
-= Ctrl_Base_DepTag
;
1091 return readMiscReg(reg_idx
, tid
); // Misc. Register File
1095 InOrderCPU::setRegOtherThread(unsigned reg_idx
, const MiscReg
&val
,
1098 // If Default value is set, then retrieve target thread
1099 if (tid
== InvalidThreadID
) {
1100 tid
= TheISA::getTargetThread(tcBase(tid
));
1103 if (reg_idx
< FP_Base_DepTag
) { // Integer Register File
1104 setIntReg(reg_idx
, val
, tid
);
1105 } else if (reg_idx
< Ctrl_Base_DepTag
) { // Float Register File
1106 reg_idx
-= FP_Base_DepTag
;
1107 setFloatRegBits(reg_idx
, val
, tid
);
1109 reg_idx
-= Ctrl_Base_DepTag
;
1110 setMiscReg(reg_idx
, val
, tid
); // Misc. Register File
1115 InOrderCPU::readMiscRegNoEffect(int misc_reg
, ThreadID tid
)
1117 return isa
[tid
].readMiscRegNoEffect(misc_reg
);
1121 InOrderCPU::readMiscReg(int misc_reg
, ThreadID tid
)
1123 return isa
[tid
].readMiscReg(misc_reg
, tcBase(tid
));
1127 InOrderCPU::setMiscRegNoEffect(int misc_reg
, const MiscReg
&val
, ThreadID tid
)
1129 isa
[tid
].setMiscRegNoEffect(misc_reg
, val
);
1133 InOrderCPU::setMiscReg(int misc_reg
, const MiscReg
&val
, ThreadID tid
)
1135 isa
[tid
].setMiscReg(misc_reg
, val
, tcBase(tid
));
1140 InOrderCPU::addInst(DynInstPtr
&inst
)
1142 ThreadID tid
= inst
->readTid();
1144 instList
[tid
].push_back(inst
);
1146 return --(instList
[tid
].end());
1150 InOrderCPU::updateContextSwitchStats()
1152 // Set Average Stat Here, then reset to 0
1153 instsPerCtxtSwitch
= instsPerSwitch
;
1159 InOrderCPU::instDone(DynInstPtr inst
, ThreadID tid
)
1161 // Set the CPU's PCs - This contributes to the precise state of the CPU
1162 // which can be used when restoring a thread to the CPU after after any
1163 // type of context switching activity (fork, exception, etc.)
1164 setPC(inst
->readPC(), tid
);
1165 setNextPC(inst
->readNextPC(), tid
);
1166 setNextNPC(inst
->readNextNPC(), tid
);
1168 if (inst
->isControl()) {
1169 thread
[tid
]->lastGradIsBranch
= true;
1170 thread
[tid
]->lastBranchPC
= inst
->readPC();
1171 thread
[tid
]->lastBranchNextPC
= inst
->readNextPC();
1172 thread
[tid
]->lastBranchNextNPC
= inst
->readNextNPC();
1174 thread
[tid
]->lastGradIsBranch
= false;
1178 // Finalize Trace Data For Instruction
1179 if (inst
->traceData
) {
1180 //inst->traceData->setCycle(curTick);
1181 inst
->traceData
->setFetchSeq(inst
->seqNum
);
1182 //inst->traceData->setCPSeq(cpu->tcBase(tid)->numInst);
1183 inst
->traceData
->dump();
1184 delete inst
->traceData
;
1185 inst
->traceData
= NULL
;
1188 // Increment active thread's instruction count
1191 // Increment thread-state's instruction count
1192 thread
[tid
]->numInst
++;
1194 // Increment thread-state's instruction stats
1195 thread
[tid
]->numInsts
++;
1197 // Count committed insts per thread stats
1198 committedInsts
[tid
]++;
1200 // Count total insts committed stat
1201 totalCommittedInsts
++;
1203 // Count SMT-committed insts per thread stat
1204 if (numActiveThreads() > 1) {
1205 smtCommittedInsts
[tid
]++;
1208 // Instruction-Mix Stats
1209 if (inst
->isLoad()) {
1211 } else if (inst
->isStore()) {
1213 } else if (inst
->isControl()) {
1215 } else if (inst
->isNop()) {
1217 } else if (inst
->isNonSpeculative()) {
1219 } else if (inst
->isInteger()) {
1221 } else if (inst
->isFloating()) {
1225 // Check for instruction-count-based events.
1226 comInstEventQueue
[tid
]->serviceEvents(thread
[tid
]->numInst
);
1228 // Broadcast to other resources an instruction
1229 // has been completed
1230 resPool
->scheduleEvent((CPUEventType
)ResourcePool::InstGraduated
, inst
,
1233 // Finally, remove instruction from CPU
1237 // currently unused function, but substitute repetitive code w/this function
1240 InOrderCPU::addToRemoveList(DynInstPtr
&inst
)
1242 removeInstsThisCycle
= true;
1243 if (!inst
->isRemoveList()) {
1244 DPRINTF(InOrderCPU
, "Pushing instruction [tid:%i] PC %#x "
1245 "[sn:%lli] to remove list\n",
1246 inst
->threadNumber
, inst
->readPC(), inst
->seqNum
);
1247 inst
->setRemoveList();
1248 removeList
.push(inst
->getInstListIt());
1250 DPRINTF(InOrderCPU
, "Ignoring instruction removal for [tid:%i] PC %#x "
1251 "[sn:%lli], already remove list\n",
1252 inst
->threadNumber
, inst
->readPC(), inst
->seqNum
);
1258 InOrderCPU::removeInst(DynInstPtr
&inst
)
1260 DPRINTF(InOrderCPU
, "Removing graduated instruction [tid:%i] PC %#x "
1262 inst
->threadNumber
, inst
->readPC(), inst
->seqNum
);
1264 removeInstsThisCycle
= true;
1266 // Remove the instruction.
1267 if (!inst
->isRemoveList()) {
1268 DPRINTF(InOrderCPU
, "Pushing instruction [tid:%i] PC %#x "
1269 "[sn:%lli] to remove list\n",
1270 inst
->threadNumber
, inst
->readPC(), inst
->seqNum
);
1271 inst
->setRemoveList();
1272 removeList
.push(inst
->getInstListIt());
1274 DPRINTF(InOrderCPU
, "Ignoring instruction removal for [tid:%i] PC %#x "
1275 "[sn:%lli], already on remove list\n",
1276 inst
->threadNumber
, inst
->readPC(), inst
->seqNum
);
1282 InOrderCPU::removeInstsUntil(const InstSeqNum
&seq_num
, ThreadID tid
)
1284 //assert(!instList[tid].empty());
1286 removeInstsThisCycle
= true;
1288 ListIt inst_iter
= instList
[tid
].end();
1292 DPRINTF(InOrderCPU
, "Squashing instructions from CPU instruction "
1293 "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1294 tid
, seq_num
, (*inst_iter
)->seqNum
);
1296 while ((*inst_iter
)->seqNum
> seq_num
) {
1298 bool break_loop
= (inst_iter
== instList
[tid
].begin());
1300 squashInstIt(inst_iter
, tid
);
1311 InOrderCPU::squashInstIt(const ListIt
&instIt
, ThreadID tid
)
1313 if ((*instIt
)->threadNumber
== tid
) {
1314 DPRINTF(InOrderCPU
, "Squashing instruction, "
1315 "[tid:%i] [sn:%lli] PC %#x\n",
1316 (*instIt
)->threadNumber
,
1318 (*instIt
)->readPC());
1320 (*instIt
)->setSquashed();
1322 if (!(*instIt
)->isRemoveList()) {
1323 DPRINTF(InOrderCPU
, "Pushing instruction [tid:%i] PC %#x "
1324 "[sn:%lli] to remove list\n",
1325 (*instIt
)->threadNumber
, (*instIt
)->readPC(),
1327 (*instIt
)->setRemoveList();
1328 removeList
.push(instIt
);
1330 DPRINTF(InOrderCPU
, "Ignoring instruction removal for [tid:%i]"
1331 " PC %#x [sn:%lli], already on remove list\n",
1332 (*instIt
)->threadNumber
, (*instIt
)->readPC(),
1342 InOrderCPU::cleanUpRemovedInsts()
1344 while (!removeList
.empty()) {
1345 DPRINTF(InOrderCPU
, "Removing instruction, "
1346 "[tid:%i] [sn:%lli] PC %#x\n",
1347 (*removeList
.front())->threadNumber
,
1348 (*removeList
.front())->seqNum
,
1349 (*removeList
.front())->readPC());
1351 DynInstPtr inst
= *removeList
.front();
1352 ThreadID tid
= inst
->threadNumber
;
1354 // Make Sure Resource Schedule Is Emptied Out
1355 ThePipeline::ResSchedule
*inst_sched
= &inst
->resSched
;
1356 while (!inst_sched
->empty()) {
1357 ThePipeline::ScheduleEntry
* sch_entry
= inst_sched
->top();
1362 // Remove From Register Dependency Map, If Necessary
1363 archRegDepMap
[(*removeList
.front())->threadNumber
].
1364 remove((*removeList
.front()));
1367 // Clear if Non-Speculative
1368 if (inst
->staticInst
&&
1369 inst
->seqNum
== nonSpecSeqNum
[tid
] &&
1370 nonSpecInstActive
[tid
] == true) {
1371 nonSpecInstActive
[tid
] = false;
1374 instList
[tid
].erase(removeList
.front());
1379 removeInstsThisCycle
= false;
1383 InOrderCPU::cleanUpRemovedReqs()
1385 while (!reqRemoveList
.empty()) {
1386 ResourceRequest
*res_req
= reqRemoveList
.front();
1388 DPRINTF(RefCount
, "[tid:%i] [sn:%lli]: Removing Request "
1389 "[stage_num:%i] [res:%s] [slot:%i] [completed:%i].\n",
1390 res_req
->inst
->threadNumber
,
1391 res_req
->inst
->seqNum
,
1392 res_req
->getStageNum(),
1393 res_req
->res
->name(),
1394 (res_req
->isCompleted()) ?
1395 res_req
->getComplSlot() : res_req
->getSlot(),
1396 res_req
->isCompleted());
1398 reqRemoveList
.pop();
1405 InOrderCPU::cleanUpRemovedEvents()
1407 while (!cpuEventRemoveList
.empty()) {
1408 Event
*cpu_event
= cpuEventRemoveList
.front();
1409 cpuEventRemoveList
.pop();
1416 InOrderCPU::dumpInsts()
1420 ListIt inst_list_it
= instList
[0].begin();
1422 cprintf("Dumping Instruction List\n");
1424 while (inst_list_it
!= instList
[0].end()) {
1425 cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1427 num
, (*inst_list_it
)->readPC(), (*inst_list_it
)->threadNumber
,
1428 (*inst_list_it
)->seqNum
, (*inst_list_it
)->isIssued(),
1429 (*inst_list_it
)->isSquashed());
1436 InOrderCPU::wakeCPU()
1438 if (/*activityRec.active() || */tickEvent
.scheduled()) {
1439 DPRINTF(Activity
, "CPU already running.\n");
1443 DPRINTF(Activity
, "Waking up CPU\n");
1445 Tick extra_cycles
= tickToCycles((curTick
- 1) - lastRunningCycle
);
1447 idleCycles
+= extra_cycles
;
1448 for (int stage_num
= 0; stage_num
< NumStages
; stage_num
++) {
1449 pipelineStage
[stage_num
]->idleCycles
+= extra_cycles
;
1452 numCycles
+= extra_cycles
;
1454 mainEventQueue
.schedule(&tickEvent
, nextCycle(curTick
));
1460 InOrderCPU::wakeup()
1462 if (this->thread
[0]->status() != ThreadContext::Suspended
)
1467 DPRINTF(Quiesce
, "Suspended Processor woken\n");
1468 this->threadContexts
[0]->activate();
1474 InOrderCPU::syscall(int64_t callnum
, ThreadID tid
)
1476 DPRINTF(InOrderCPU
, "[tid:%i] Executing syscall().\n\n", tid
);
1478 DPRINTF(Activity
,"Activity: syscall() called.\n");
1480 // Temporarily increase this by one to account for the syscall
1482 ++(this->thread
[tid
]->funcExeInst
);
1484 // Execute the actual syscall.
1485 this->thread
[tid
]->syscall(callnum
);
1487 // Decrease funcExeInst by one as the normal commit will handle
1489 --(this->thread
[tid
]->funcExeInst
);
1491 // Clear Non-Speculative Block Variable
1492 nonSpecInstActive
[tid
] = false;
1497 InOrderCPU::prefetch(DynInstPtr inst
)
1499 Resource
*mem_res
= resPool
->getResource(dataPortIdx
);
1500 return mem_res
->prefetch(inst
);
1504 InOrderCPU::writeHint(DynInstPtr inst
)
1506 Resource
*mem_res
= resPool
->getResource(dataPortIdx
);
1507 return mem_res
->writeHint(inst
);
1512 InOrderCPU::getITBPtr()
1514 CacheUnit
*itb_res
=
1515 dynamic_cast<CacheUnit
*>(resPool
->getResource(fetchPortIdx
));
1516 return itb_res
->tlb();
1521 InOrderCPU::getDTBPtr()
1523 CacheUnit
*dtb_res
=
1524 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1525 return dtb_res
->tlb();
1530 InOrderCPU::read(DynInstPtr inst
, Addr addr
, T
&data
, unsigned flags
)
1532 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1533 // you want to run w/out caches?
1534 CacheUnit
*cache_res
=
1535 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1537 return cache_res
->read(inst
, addr
, data
, flags
);
1540 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1544 InOrderCPU::read(DynInstPtr inst
, Addr addr
, Twin32_t
&data
, unsigned flags
);
1548 InOrderCPU::read(DynInstPtr inst
, Addr addr
, Twin64_t
&data
, unsigned flags
);
1552 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint64_t &data
, unsigned flags
);
1556 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint32_t &data
, unsigned flags
);
1560 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint16_t &data
, unsigned flags
);
1564 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint8_t &data
, unsigned flags
);
1566 #endif //DOXYGEN_SHOULD_SKIP_THIS
1570 InOrderCPU::read(DynInstPtr inst
, Addr addr
, double &data
, unsigned flags
)
1572 return read(inst
, addr
, *(uint64_t*)&data
, flags
);
1577 InOrderCPU::read(DynInstPtr inst
, Addr addr
, float &data
, unsigned flags
)
1579 return read(inst
, addr
, *(uint32_t*)&data
, flags
);
1585 InOrderCPU::read(DynInstPtr inst
, Addr addr
, int32_t &data
, unsigned flags
)
1587 return read(inst
, addr
, (uint32_t&)data
, flags
);
1592 InOrderCPU::write(DynInstPtr inst
, T data
, Addr addr
, unsigned flags
,
1593 uint64_t *write_res
)
1595 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1596 // you want to run w/out caches?
1597 CacheUnit
*cache_res
=
1598 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1599 return cache_res
->write(inst
, data
, addr
, flags
, write_res
);
1602 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1606 InOrderCPU::write(DynInstPtr inst
, Twin32_t data
, Addr addr
,
1607 unsigned flags
, uint64_t *res
);
1611 InOrderCPU::write(DynInstPtr inst
, Twin64_t data
, Addr addr
,
1612 unsigned flags
, uint64_t *res
);
1616 InOrderCPU::write(DynInstPtr inst
, uint64_t data
, Addr addr
,
1617 unsigned flags
, uint64_t *res
);
1621 InOrderCPU::write(DynInstPtr inst
, uint32_t data
, Addr addr
,
1622 unsigned flags
, uint64_t *res
);
1626 InOrderCPU::write(DynInstPtr inst
, uint16_t data
, Addr addr
,
1627 unsigned flags
, uint64_t *res
);
1631 InOrderCPU::write(DynInstPtr inst
, uint8_t data
, Addr addr
,
1632 unsigned flags
, uint64_t *res
);
1634 #endif //DOXYGEN_SHOULD_SKIP_THIS
1638 InOrderCPU::write(DynInstPtr inst
, double data
, Addr addr
, unsigned flags
,
1641 return write(inst
, *(uint64_t*)&data
, addr
, flags
, res
);
1646 InOrderCPU::write(DynInstPtr inst
, float data
, Addr addr
, unsigned flags
,
1649 return write(inst
, *(uint32_t*)&data
, addr
, flags
, res
);
1655 InOrderCPU::write(DynInstPtr inst
, int32_t data
, Addr addr
, unsigned flags
,
1658 return write(inst
, (uint32_t)data
, addr
, flags
, res
);