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,curTick
+ cpu
->ticks(delay
));
162 else if (!scheduled())
163 mainEventQueue
.schedule(this,curTick
+ cpu
->ticks(delay
));
167 InOrderCPU::CPUEvent::unscheduleEvent()
173 InOrderCPU::InOrderCPU(Params
*params
)
175 cpu_id(params
->cpu_id
),
180 removeInstsThisCycle(false),
181 activityRec(params
->name
, NumStages
, 10, params
->activity
),
183 system(params
->system
),
184 physmem(system
->physmem
),
185 #endif // FULL_SYSTEM
191 deferRegistration(false/*params->deferRegistration*/),
192 stageTracing(params
->stageTracing
),
195 ThreadID active_threads
;
198 resPool
= new ResourcePool(this, params
);
200 // Resize for Multithreading CPUs
201 thread
.resize(numThreads
);
206 active_threads
= params
->workload
.size();
208 if (active_threads
> MaxThreads
) {
209 panic("Workload Size too large. Increase the 'MaxThreads'"
210 "in your InOrder implementation or "
211 "edit your workload size.");
215 if (active_threads
> 1) {
216 threadModel
= (InOrderCPU::ThreadModel
) params
->threadModel
;
218 if (threadModel
== SMT
) {
219 DPRINTF(InOrderCPU
, "Setting Thread Model to SMT.\n");
220 } else if (threadModel
== SwitchOnCacheMiss
) {
221 DPRINTF(InOrderCPU
, "Setting Thread Model to "
222 "Switch On Cache Miss\n");
226 threadModel
= Single
;
233 // Bind the fetch & data ports from the resource pool.
234 fetchPortIdx
= resPool
->getPortIdx(params
->fetchMemPort
);
235 if (fetchPortIdx
== 0) {
236 fatal("Unable to find port to fetch instructions from.\n");
239 dataPortIdx
= resPool
->getPortIdx(params
->dataMemPort
);
240 if (dataPortIdx
== 0) {
241 fatal("Unable to find port for data.\n");
244 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
) {
246 // SMT is not supported in FS mode yet.
247 assert(numThreads
== 1);
248 thread
[tid
] = new Thread(this, 0);
250 if (tid
< (ThreadID
)params
->workload
.size()) {
251 DPRINTF(InOrderCPU
, "Workload[%i] process is %#x\n",
252 tid
, params
->workload
[tid
]->prog_fname
);
254 new Thread(this, tid
, params
->workload
[tid
]);
256 //Allocate Empty thread so M5 can use later
257 //when scheduling threads to CPU
258 Process
* dummy_proc
= params
->workload
[0];
259 thread
[tid
] = new Thread(this, tid
, dummy_proc
);
262 // Eventually set this with parameters...
266 // Setup the TC that will serve as the interface to the threads/CPU.
267 InOrderThreadContext
*tc
= new InOrderThreadContext
;
269 tc
->thread
= thread
[tid
];
271 // Give the thread the TC.
272 thread
[tid
]->tc
= tc
;
273 thread
[tid
]->setFuncExeInst(0);
274 globalSeqNum
[tid
] = 1;
276 // Add the TC to the CPU's list of TC's.
277 this->threadContexts
.push_back(tc
);
280 // Initialize TimeBuffer Stage Queues
281 for (int stNum
=0; stNum
< NumStages
- 1; stNum
++) {
282 stageQueue
[stNum
] = new StageQueue(NumStages
, NumStages
);
283 stageQueue
[stNum
]->id(stNum
);
287 // Set Up Pipeline Stages
288 for (int stNum
=0; stNum
< NumStages
; stNum
++) {
290 pipelineStage
[stNum
] = new FirstStage(params
, stNum
);
292 pipelineStage
[stNum
] = new PipelineStage(params
, stNum
);
294 pipelineStage
[stNum
]->setCPU(this);
295 pipelineStage
[stNum
]->setActiveThreads(&activeThreads
);
296 pipelineStage
[stNum
]->setTimeBuffer(&timeBuffer
);
298 // Take Care of 1st/Nth stages
300 pipelineStage
[stNum
]->setPrevStageQueue(stageQueue
[stNum
- 1]);
301 if (stNum
< NumStages
- 1)
302 pipelineStage
[stNum
]->setNextStageQueue(stageQueue
[stNum
]);
305 // Initialize thread specific variables
306 for (ThreadID tid
= 0; tid
< numThreads
; tid
++) {
307 archRegDepMap
[tid
].setCPU(this);
309 nonSpecInstActive
[tid
] = false;
310 nonSpecSeqNum
[tid
] = 0;
312 squashSeqNum
[tid
] = MaxAddr
;
313 lastSquashCycle
[tid
] = 0;
315 memset(intRegs
[tid
], 0, sizeof(intRegs
[tid
]));
316 memset(floatRegs
.i
[tid
], 0, sizeof(floatRegs
.i
[tid
]));
319 isa
[tid
].expandForMultithreading(numThreads
, 1/*numVirtProcs*/);
321 // Define dummy instructions and resource requests to be used.
322 dummyInst
[tid
] = new InOrderDynInst(this,
328 dummyReq
[tid
] = new ResourceRequest(resPool
->getResource(0),
336 lastRunningCycle
= curTick
;
338 // Reset CPU to reset state.
340 Fault resetFault
= new ResetFault();
341 resetFault
->invoke(tcBase());
346 // Schedule First Tick Event, CPU will reschedule itself from here on out.
347 scheduleTickEvent(0);
350 InOrderCPU::~InOrderCPU()
357 InOrderCPU::regStats()
359 /* Register the Resource Pool's stats here.*/
364 .name(name() + ".maxResReqCount")
365 .desc("Maximum number of live resource requests in CPU")
366 .prereq(maxResReqCount
);
369 /* Register for each Pipeline Stage */
370 for (int stage_num
=0; stage_num
< ThePipeline::NumStages
; stage_num
++) {
371 pipelineStage
[stage_num
]->regStats();
374 /* Register any of the InOrderCPU's stats here.*/
376 .name(name() + ".instsPerContextSwitch")
377 .desc("Instructions Committed Per Context Switch")
378 .prereq(instsPerCtxtSwitch
);
381 .name(name() + ".contextSwitches")
382 .desc("Number of context switches");
385 .name(name() + ".timesIdled")
386 .desc("Number of times that the entire CPU went into an idle state and"
387 " unscheduled itself")
391 .name(name() + ".idleCycles")
392 .desc("Number of cycles cpu's stages were not processed");
395 .name(name() + ".runCycles")
396 .desc("Number of cycles cpu stages are processed.");
399 .name(name() + ".activity")
400 .desc("Percentage of cycles cpu is active")
402 activity
= (runCycles
/ numCycles
) * 100;
406 .name(name() + ".threadCycles")
407 .desc("Total Number of Cycles A Thread Was Active in CPU (Per-Thread)");
410 .name(name() + ".smtCycles")
411 .desc("Total number of cycles that the CPU was in SMT-mode");
415 .name(name() + ".committedInsts")
416 .desc("Number of Instructions Simulated (Per-Thread)");
420 .name(name() + ".smtCommittedInsts")
421 .desc("Number of SMT Instructions Simulated (Per-Thread)");
424 .name(name() + ".committedInsts_total")
425 .desc("Number of Instructions Simulated (Total)");
428 .name(name() + ".cpi")
429 .desc("CPI: Cycles Per Instruction (Per-Thread)")
431 cpi
= threadCycles
/ committedInsts
;
434 .name(name() + ".smt_cpi")
435 .desc("CPI: Total SMT-CPI")
437 smtCpi
= smtCycles
/ smtCommittedInsts
;
440 .name(name() + ".cpi_total")
441 .desc("CPI: Total CPI of All Threads")
443 totalCpi
= numCycles
/ totalCommittedInsts
;
446 .name(name() + ".ipc")
447 .desc("IPC: Instructions Per Cycle (Per-Thread)")
449 ipc
= committedInsts
/ threadCycles
;
452 .name(name() + ".smt_ipc")
453 .desc("IPC: Total SMT-IPC")
455 smtIpc
= smtCommittedInsts
/ smtCycles
;
458 .name(name() + ".ipc_total")
459 .desc("IPC: Total IPC of All Threads")
461 totalIpc
= totalCommittedInsts
/ numCycles
;
470 DPRINTF(InOrderCPU
, "\n\nInOrderCPU: Ticking main, InOrderCPU.\n");
474 bool pipes_idle
= true;
476 //Tick each of the stages
477 for (int stNum
=NumStages
- 1; stNum
>= 0 ; stNum
--) {
478 pipelineStage
[stNum
]->tick();
480 pipes_idle
= pipes_idle
&& pipelineStage
[stNum
]->idle
;
488 // Now advance the time buffers one tick
489 timeBuffer
.advance();
490 for (int sqNum
=0; sqNum
< NumStages
- 1; sqNum
++) {
491 stageQueue
[sqNum
]->advance();
493 activityRec
.advance();
495 // Any squashed requests, events, or insts then remove them now
496 cleanUpRemovedReqs();
497 cleanUpRemovedEvents();
498 cleanUpRemovedInsts();
500 // Re-schedule CPU for this cycle
501 if (!tickEvent
.scheduled()) {
502 if (_status
== SwitchedOut
) {
504 lastRunningCycle
= curTick
;
505 } else if (!activityRec
.active()) {
506 DPRINTF(InOrderCPU
, "sleeping CPU.\n");
507 lastRunningCycle
= curTick
;
510 //Tick next_tick = curTick + cycles(1);
511 //tickEvent.schedule(next_tick);
512 mainEventQueue
.schedule(&tickEvent
, nextCycle(curTick
+ 1));
513 DPRINTF(InOrderCPU
, "Scheduled CPU for next tick @ %i.\n",
514 nextCycle(curTick
+ 1));
519 updateThreadPriority();
526 if (!deferRegistration
) {
527 registerThreadContexts();
530 // Set inSyscall so that the CPU doesn't squash when initially
531 // setting up registers.
532 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
)
533 thread
[tid
]->inSyscall
= true;
536 for (ThreadID tid
= 0; tid
< numThreads
; tid
++) {
537 ThreadContext
*src_tc
= threadContexts
[tid
];
538 TheISA::initCPU(src_tc
, src_tc
->contextId());
543 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
)
544 thread
[tid
]->inSyscall
= false;
546 // Call Initializiation Routine for Resource Pool
553 for (int i
= 0; i
< numThreads
; i
++) {
554 isa
[i
].reset(coreType
, numThreads
,
555 1/*numVirtProcs*/, dynamic_cast<BaseCPU
*>(this));
560 InOrderCPU::getPort(const std::string
&if_name
, int idx
)
562 return resPool
->getPort(if_name
, idx
);
567 InOrderCPU::hwrei(ThreadID tid
)
569 panic("hwrei: Unimplemented");
576 InOrderCPU::simPalCheck(int palFunc
, ThreadID tid
)
578 panic("simPalCheck: Unimplemented");
585 InOrderCPU::getInterrupts()
587 // Check if there are any outstanding interrupts
588 return this->interrupts
->getInterrupt(this->threadContexts
[0]);
593 InOrderCPU::processInterrupts(Fault interrupt
)
595 // Check for interrupts here. For now can copy the code that
596 // exists within isa_fullsys_traits.hh. Also assume that thread 0
597 // is the one that handles the interrupts.
598 // @todo: Possibly consolidate the interrupt checking code.
599 // @todo: Allow other threads to handle interrupts.
601 assert(interrupt
!= NoFault
);
602 this->interrupts
->updateIntrInfo(this->threadContexts
[0]);
604 DPRINTF(InOrderCPU
, "Interrupt %s being handled\n", interrupt
->name());
605 this->trap(interrupt
, 0);
610 InOrderCPU::updateMemPorts()
612 // Update all ThreadContext's memory ports (Functional/Virtual
614 ThreadID size
= thread
.size();
615 for (ThreadID i
= 0; i
< size
; ++i
)
616 thread
[i
]->connectMemPorts(thread
[i
]->getTC());
621 InOrderCPU::trap(Fault fault
, ThreadID tid
, int delay
)
623 //@ Squash Pipeline during TRAP
624 scheduleCpuEvent(Trap
, fault
, tid
, dummyInst
[tid
], delay
);
628 InOrderCPU::trapCPU(Fault fault
, ThreadID tid
)
630 fault
->invoke(tcBase(tid
));
634 InOrderCPU::squashFromMemStall(DynInstPtr inst
, ThreadID tid
, int delay
)
636 scheduleCpuEvent(SquashFromMemStall
, NoFault
, tid
, inst
, delay
);
641 InOrderCPU::squashDueToMemStall(int stage_num
, InstSeqNum seq_num
, ThreadID tid
)
643 DPRINTF(InOrderCPU
, "Squashing Pipeline Stages Due to Memory Stall...\n");
645 // Squash all instructions in each stage including
646 // instruction that caused the squash (seq_num - 1)
647 // NOTE: The stage bandwidth needs to be cleared so thats why
648 // the stalling instruction is squashed as well. The stalled
649 // instruction is previously placed in another intermediate buffer
650 // while it's stall is being handled.
651 InstSeqNum squash_seq_num
= seq_num
- 1;
653 for (int stNum
=stage_num
; stNum
>= 0 ; stNum
--) {
654 pipelineStage
[stNum
]->squashDueToMemStall(squash_seq_num
, tid
);
659 InOrderCPU::scheduleCpuEvent(CPUEventType c_event
, Fault fault
,
660 ThreadID tid
, DynInstPtr inst
,
661 unsigned delay
, unsigned event_pri_offset
)
663 CPUEvent
*cpu_event
= new CPUEvent(this, c_event
, fault
, tid
, inst
,
667 DPRINTF(InOrderCPU
, "Scheduling CPU Event (%s) for cycle %i, [tid:%i].\n",
668 eventNames
[c_event
], curTick
+ delay
, tid
);
669 mainEventQueue
.schedule(cpu_event
,curTick
+ delay
);
671 cpu_event
->process();
672 cpuEventRemoveList
.push(cpu_event
);
675 // Broadcast event to the Resource Pool
676 // Need to reset tid just in case this is a dummy instruction
678 resPool
->scheduleEvent(c_event
, inst
, 0, 0, tid
);
682 InOrderCPU::isThreadActive(ThreadID tid
)
684 list
<ThreadID
>::iterator isActive
=
685 std::find(activeThreads
.begin(), activeThreads
.end(), tid
);
687 return (isActive
!= activeThreads
.end());
691 InOrderCPU::isThreadReady(ThreadID tid
)
693 list
<ThreadID
>::iterator isReady
=
694 std::find(readyThreads
.begin(), readyThreads
.end(), tid
);
696 return (isReady
!= readyThreads
.end());
700 InOrderCPU::isThreadSuspended(ThreadID tid
)
702 list
<ThreadID
>::iterator isSuspended
=
703 std::find(suspendedThreads
.begin(), suspendedThreads
.end(), tid
);
705 return (isSuspended
!= suspendedThreads
.end());
709 InOrderCPU::activateNextReadyThread()
711 if (readyThreads
.size() >= 1) {
712 ThreadID ready_tid
= readyThreads
.front();
714 // Activate in Pipeline
715 activateThread(ready_tid
);
717 // Activate in Resource Pool
718 resPool
->activateAll(ready_tid
);
720 list
<ThreadID
>::iterator ready_it
=
721 std::find(readyThreads
.begin(), readyThreads
.end(), ready_tid
);
722 readyThreads
.erase(ready_it
);
725 "Attempting to activate new thread, but No Ready Threads to"
728 "Unable to switch to next active thread.\n");
733 InOrderCPU::activateThread(ThreadID tid
)
735 if (isThreadSuspended(tid
)) {
737 "Removing [tid:%i] from suspended threads list.\n", tid
);
739 list
<ThreadID
>::iterator susp_it
=
740 std::find(suspendedThreads
.begin(), suspendedThreads
.end(),
742 suspendedThreads
.erase(susp_it
);
745 if (threadModel
== SwitchOnCacheMiss
&&
746 numActiveThreads() == 1) {
748 "Ignoring activation of [tid:%i], since [tid:%i] is "
749 "already running.\n", tid
, activeThreadId());
751 DPRINTF(InOrderCPU
,"Placing [tid:%i] on ready threads list\n",
754 readyThreads
.push_back(tid
);
756 } else if (!isThreadActive(tid
)) {
758 "Adding [tid:%i] to active threads list.\n", tid
);
759 activeThreads
.push_back(tid
);
761 activateThreadInPipeline(tid
);
763 thread
[tid
]->lastActivate
= curTick
;
765 tcBase(tid
)->setStatus(ThreadContext::Active
);
774 InOrderCPU::activateThreadInPipeline(ThreadID tid
)
776 for (int stNum
=0; stNum
< NumStages
; stNum
++) {
777 pipelineStage
[stNum
]->activateThread(tid
);
782 InOrderCPU::deactivateContext(ThreadID tid
, int delay
)
784 DPRINTF(InOrderCPU
,"[tid:%i]: Deactivating ...\n", tid
);
786 scheduleCpuEvent(DeactivateThread
, NoFault
, tid
, dummyInst
[tid
], delay
);
788 // Be sure to signal that there's some activity so the CPU doesn't
789 // deschedule itself.
790 activityRec
.activity();
796 InOrderCPU::deactivateThread(ThreadID tid
)
798 DPRINTF(InOrderCPU
, "[tid:%i]: Calling deactivate thread.\n", tid
);
800 if (isThreadActive(tid
)) {
801 DPRINTF(InOrderCPU
,"[tid:%i]: Removing from active threads list\n",
803 list
<ThreadID
>::iterator thread_it
=
804 std::find(activeThreads
.begin(), activeThreads
.end(), tid
);
806 removePipelineStalls(*thread_it
);
808 activeThreads
.erase(thread_it
);
810 // Ideally, this should be triggered from the
811 // suspendContext/Thread functions
812 tcBase(tid
)->setStatus(ThreadContext::Suspended
);
815 assert(!isThreadActive(tid
));
819 InOrderCPU::removePipelineStalls(ThreadID tid
)
821 DPRINTF(InOrderCPU
,"[tid:%i]: Removing all pipeline stalls\n",
824 for (int stNum
= 0; stNum
< NumStages
; stNum
++) {
825 pipelineStage
[stNum
]->removeStalls(tid
);
831 InOrderCPU::updateThreadPriority()
833 if (activeThreads
.size() > 1)
835 //DEFAULT TO ROUND ROBIN SCHEME
836 //e.g. Move highest priority to end of thread list
837 list
<ThreadID
>::iterator list_begin
= activeThreads
.begin();
838 list
<ThreadID
>::iterator list_end
= activeThreads
.end();
840 unsigned high_thread
= *list_begin
;
842 activeThreads
.erase(list_begin
);
844 activeThreads
.push_back(high_thread
);
849 InOrderCPU::tickThreadStats()
851 /** Keep track of cycles that each thread is active */
852 list
<ThreadID
>::iterator thread_it
= activeThreads
.begin();
853 while (thread_it
!= activeThreads
.end()) {
854 threadCycles
[*thread_it
]++;
858 // Keep track of cycles where SMT is active
859 if (activeThreads
.size() > 1) {
865 InOrderCPU::activateContext(ThreadID tid
, int delay
)
867 DPRINTF(InOrderCPU
,"[tid:%i]: Activating ...\n", tid
);
870 scheduleCpuEvent(ActivateThread
, NoFault
, tid
, dummyInst
[tid
], delay
);
872 // Be sure to signal that there's some activity so the CPU doesn't
873 // deschedule itself.
874 activityRec
.activity();
880 InOrderCPU::activateNextReadyContext(int delay
)
882 DPRINTF(InOrderCPU
,"Activating next ready thread\n");
884 // NOTE: Add 5 to the event priority so that we always activate
885 // threads after we've finished deactivating, squashing,etc.
887 scheduleCpuEvent(ActivateNextReadyThread
, NoFault
, 0/*tid*/, dummyInst
[0],
890 // Be sure to signal that there's some activity so the CPU doesn't
891 // deschedule itself.
892 activityRec
.activity();
898 InOrderCPU::haltContext(ThreadID tid
, int delay
)
900 DPRINTF(InOrderCPU
, "[tid:%i]: Calling Halt Context...\n", tid
);
902 scheduleCpuEvent(HaltThread
, NoFault
, tid
, dummyInst
[tid
], delay
);
904 activityRec
.activity();
908 InOrderCPU::haltThread(ThreadID tid
)
910 DPRINTF(InOrderCPU
, "[tid:%i]: Placing on Halted Threads List...\n", tid
);
911 deactivateThread(tid
);
912 squashThreadInPipeline(tid
);
913 haltedThreads
.push_back(tid
);
915 tcBase(tid
)->setStatus(ThreadContext::Halted
);
917 if (threadModel
== SwitchOnCacheMiss
) {
918 activateNextReadyContext();
923 InOrderCPU::suspendContext(ThreadID tid
, int delay
)
925 scheduleCpuEvent(SuspendThread
, NoFault
, tid
, dummyInst
[tid
], delay
);
929 InOrderCPU::suspendThread(ThreadID tid
)
931 DPRINTF(InOrderCPU
, "[tid:%i]: Placing on Suspended Threads List...\n", tid
);
932 deactivateThread(tid
);
933 suspendedThreads
.push_back(tid
);
934 thread
[tid
]->lastSuspend
= curTick
;
936 tcBase(tid
)->setStatus(ThreadContext::Suspended
);
940 InOrderCPU::squashThreadInPipeline(ThreadID tid
)
942 //Squash all instructions in each stage
943 for (int stNum
=NumStages
- 1; stNum
>= 0 ; stNum
--) {
944 pipelineStage
[stNum
]->squash(0 /*seq_num*/, tid
);
949 InOrderCPU::getPipeStage(int stage_num
)
951 return pipelineStage
[stage_num
];
955 InOrderCPU::readPC(ThreadID tid
)
962 InOrderCPU::setPC(Addr new_PC
, ThreadID tid
)
969 InOrderCPU::readNextPC(ThreadID tid
)
976 InOrderCPU::setNextPC(uint64_t new_NPC
, ThreadID tid
)
978 nextPC
[tid
] = new_NPC
;
983 InOrderCPU::readNextNPC(ThreadID tid
)
990 InOrderCPU::setNextNPC(uint64_t new_NNPC
, ThreadID tid
)
992 nextNPC
[tid
] = new_NNPC
;
996 InOrderCPU::readIntReg(int reg_idx
, ThreadID tid
)
998 return intRegs
[tid
][reg_idx
];
1002 InOrderCPU::readFloatReg(int reg_idx
, ThreadID tid
)
1004 return floatRegs
.f
[tid
][reg_idx
];
1008 InOrderCPU::readFloatRegBits(int reg_idx
, ThreadID tid
)
1010 return floatRegs
.i
[tid
][reg_idx
];
1014 InOrderCPU::setIntReg(int reg_idx
, uint64_t val
, ThreadID tid
)
1016 intRegs
[tid
][reg_idx
] = val
;
1021 InOrderCPU::setFloatReg(int reg_idx
, FloatReg val
, ThreadID tid
)
1023 floatRegs
.f
[tid
][reg_idx
] = val
;
1028 InOrderCPU::setFloatRegBits(int reg_idx
, FloatRegBits val
, ThreadID tid
)
1030 floatRegs
.i
[tid
][reg_idx
] = val
;
1034 InOrderCPU::readRegOtherThread(unsigned reg_idx
, ThreadID tid
)
1036 // If Default value is set, then retrieve target thread
1037 if (tid
== InvalidThreadID
) {
1038 tid
= TheISA::getTargetThread(tcBase(tid
));
1041 if (reg_idx
< FP_Base_DepTag
) {
1042 // Integer Register File
1043 return readIntReg(reg_idx
, tid
);
1044 } else if (reg_idx
< Ctrl_Base_DepTag
) {
1045 // Float Register File
1046 reg_idx
-= FP_Base_DepTag
;
1047 return readFloatRegBits(reg_idx
, tid
);
1049 reg_idx
-= Ctrl_Base_DepTag
;
1050 return readMiscReg(reg_idx
, tid
); // Misc. Register File
1054 InOrderCPU::setRegOtherThread(unsigned reg_idx
, const MiscReg
&val
,
1057 // If Default value is set, then retrieve target thread
1058 if (tid
== InvalidThreadID
) {
1059 tid
= TheISA::getTargetThread(tcBase(tid
));
1062 if (reg_idx
< FP_Base_DepTag
) { // Integer Register File
1063 setIntReg(reg_idx
, val
, tid
);
1064 } else if (reg_idx
< Ctrl_Base_DepTag
) { // Float Register File
1065 reg_idx
-= FP_Base_DepTag
;
1066 setFloatRegBits(reg_idx
, val
, tid
);
1068 reg_idx
-= Ctrl_Base_DepTag
;
1069 setMiscReg(reg_idx
, val
, tid
); // Misc. Register File
1074 InOrderCPU::readMiscRegNoEffect(int misc_reg
, ThreadID tid
)
1076 return isa
[tid
].readMiscRegNoEffect(misc_reg
);
1080 InOrderCPU::readMiscReg(int misc_reg
, ThreadID tid
)
1082 return isa
[tid
].readMiscReg(misc_reg
, tcBase(tid
));
1086 InOrderCPU::setMiscRegNoEffect(int misc_reg
, const MiscReg
&val
, ThreadID tid
)
1088 isa
[tid
].setMiscRegNoEffect(misc_reg
, val
);
1092 InOrderCPU::setMiscReg(int misc_reg
, const MiscReg
&val
, ThreadID tid
)
1094 isa
[tid
].setMiscReg(misc_reg
, val
, tcBase(tid
));
1099 InOrderCPU::addInst(DynInstPtr
&inst
)
1101 ThreadID tid
= inst
->readTid();
1103 instList
[tid
].push_back(inst
);
1105 return --(instList
[tid
].end());
1109 InOrderCPU::updateContextSwitchStats()
1111 // Set Average Stat Here, then reset to 0
1112 instsPerCtxtSwitch
= instsPerSwitch
;
1118 InOrderCPU::instDone(DynInstPtr inst
, ThreadID tid
)
1120 // Set the CPU's PCs - This contributes to the precise state of the CPU
1121 // which can be used when restoring a thread to the CPU after after any
1122 // type of context switching activity (fork, exception, etc.)
1123 setPC(inst
->readPC(), tid
);
1124 setNextPC(inst
->readNextPC(), tid
);
1125 setNextNPC(inst
->readNextNPC(), tid
);
1127 if (inst
->isControl()) {
1128 thread
[tid
]->lastGradIsBranch
= true;
1129 thread
[tid
]->lastBranchPC
= inst
->readPC();
1130 thread
[tid
]->lastBranchNextPC
= inst
->readNextPC();
1131 thread
[tid
]->lastBranchNextNPC
= inst
->readNextNPC();
1133 thread
[tid
]->lastGradIsBranch
= false;
1137 // Finalize Trace Data For Instruction
1138 if (inst
->traceData
) {
1139 //inst->traceData->setCycle(curTick);
1140 inst
->traceData
->setFetchSeq(inst
->seqNum
);
1141 //inst->traceData->setCPSeq(cpu->tcBase(tid)->numInst);
1142 inst
->traceData
->dump();
1143 delete inst
->traceData
;
1144 inst
->traceData
= NULL
;
1147 // Increment active thread's instruction count
1150 // Increment thread-state's instruction count
1151 thread
[tid
]->numInst
++;
1153 // Increment thread-state's instruction stats
1154 thread
[tid
]->numInsts
++;
1156 // Count committed insts per thread stats
1157 committedInsts
[tid
]++;
1159 // Count total insts committed stat
1160 totalCommittedInsts
++;
1162 // Count SMT-committed insts per thread stat
1163 if (numActiveThreads() > 1) {
1164 smtCommittedInsts
[tid
]++;
1167 // Check for instruction-count-based events.
1168 comInstEventQueue
[tid
]->serviceEvents(thread
[tid
]->numInst
);
1170 // Broadcast to other resources an instruction
1171 // has been completed
1172 resPool
->scheduleEvent((CPUEventType
)ResourcePool::InstGraduated
, inst
,
1175 // Finally, remove instruction from CPU
1180 InOrderCPU::addToRemoveList(DynInstPtr
&inst
)
1182 removeInstsThisCycle
= true;
1184 removeList
.push(inst
->getInstListIt());
1188 InOrderCPU::removeInst(DynInstPtr
&inst
)
1190 DPRINTF(InOrderCPU
, "Removing graduated instruction [tid:%i] PC %#x "
1192 inst
->threadNumber
, inst
->readPC(), inst
->seqNum
);
1194 removeInstsThisCycle
= true;
1196 // Remove the instruction.
1197 removeList
.push(inst
->getInstListIt());
1201 InOrderCPU::removeInstsUntil(const InstSeqNum
&seq_num
, ThreadID tid
)
1203 //assert(!instList[tid].empty());
1205 removeInstsThisCycle
= true;
1207 ListIt inst_iter
= instList
[tid
].end();
1211 DPRINTF(InOrderCPU
, "Deleting instructions from CPU instruction "
1212 "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1213 tid
, seq_num
, (*inst_iter
)->seqNum
);
1215 while ((*inst_iter
)->seqNum
> seq_num
) {
1217 bool break_loop
= (inst_iter
== instList
[tid
].begin());
1219 squashInstIt(inst_iter
, tid
);
1230 InOrderCPU::squashInstIt(const ListIt
&instIt
, ThreadID tid
)
1232 if ((*instIt
)->threadNumber
== tid
) {
1233 DPRINTF(InOrderCPU
, "Squashing instruction, "
1234 "[tid:%i] [sn:%lli] PC %#x\n",
1235 (*instIt
)->threadNumber
,
1237 (*instIt
)->readPC());
1239 (*instIt
)->setSquashed();
1241 removeList
.push(instIt
);
1247 InOrderCPU::cleanUpRemovedInsts()
1249 while (!removeList
.empty()) {
1250 DPRINTF(InOrderCPU
, "Removing instruction, "
1251 "[tid:%i] [sn:%lli] PC %#x\n",
1252 (*removeList
.front())->threadNumber
,
1253 (*removeList
.front())->seqNum
,
1254 (*removeList
.front())->readPC());
1256 DynInstPtr inst
= *removeList
.front();
1257 ThreadID tid
= inst
->threadNumber
;
1259 // Make Sure Resource Schedule Is Emptied Out
1260 ThePipeline::ResSchedule
*inst_sched
= &inst
->resSched
;
1261 while (!inst_sched
->empty()) {
1262 ThePipeline::ScheduleEntry
* sch_entry
= inst_sched
->top();
1267 // Remove From Register Dependency Map, If Necessary
1268 archRegDepMap
[(*removeList
.front())->threadNumber
].
1269 remove((*removeList
.front()));
1272 // Clear if Non-Speculative
1273 if (inst
->staticInst
&&
1274 inst
->seqNum
== nonSpecSeqNum
[tid
] &&
1275 nonSpecInstActive
[tid
] == true) {
1276 nonSpecInstActive
[tid
] = false;
1279 instList
[tid
].erase(removeList
.front());
1283 DPRINTF(RefCount
, "pop from remove list: [sn:%i]: Refcount = %i.\n",
1285 0/*inst->curCount()*/);
1289 removeInstsThisCycle
= false;
1293 InOrderCPU::cleanUpRemovedReqs()
1295 while (!reqRemoveList
.empty()) {
1296 ResourceRequest
*res_req
= reqRemoveList
.front();
1298 DPRINTF(RefCount
, "[tid:%i]: Removing Request, "
1299 "[sn:%lli] [slot:%i] [stage_num:%i] [res:%s] [refcount:%i].\n",
1300 res_req
->inst
->threadNumber
,
1301 res_req
->inst
->seqNum
,
1303 res_req
->getStageNum(),
1304 res_req
->res
->name(),
1305 0/*res_req->inst->curCount()*/);
1307 reqRemoveList
.pop();
1311 DPRINTF(RefCount
, "after remove request: [sn:%i]: Refcount = %i.\n",
1312 res_req
->inst
->seqNum
,
1313 0/*res_req->inst->curCount()*/);
1318 InOrderCPU::cleanUpRemovedEvents()
1320 while (!cpuEventRemoveList
.empty()) {
1321 Event
*cpu_event
= cpuEventRemoveList
.front();
1322 cpuEventRemoveList
.pop();
1329 InOrderCPU::dumpInsts()
1333 ListIt inst_list_it
= instList
[0].begin();
1335 cprintf("Dumping Instruction List\n");
1337 while (inst_list_it
!= instList
[0].end()) {
1338 cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1340 num
, (*inst_list_it
)->readPC(), (*inst_list_it
)->threadNumber
,
1341 (*inst_list_it
)->seqNum
, (*inst_list_it
)->isIssued(),
1342 (*inst_list_it
)->isSquashed());
1349 InOrderCPU::wakeCPU()
1351 if (/*activityRec.active() || */tickEvent
.scheduled()) {
1352 DPRINTF(Activity
, "CPU already running.\n");
1356 DPRINTF(Activity
, "Waking up CPU\n");
1358 Tick extra_cycles
= tickToCycles((curTick
- 1) - lastRunningCycle
);
1360 idleCycles
+= extra_cycles
;
1361 for (int stage_num
= 0; stage_num
< NumStages
; stage_num
++) {
1362 pipelineStage
[stage_num
]->idleCycles
+= extra_cycles
;
1365 numCycles
+= extra_cycles
;
1367 mainEventQueue
.schedule(&tickEvent
, curTick
);
1373 InOrderCPU::wakeup()
1375 if (this->thread
[0]->status() != ThreadContext::Suspended
)
1380 DPRINTF(Quiesce
, "Suspended Processor woken\n");
1381 this->threadContexts
[0]->activate();
1387 InOrderCPU::syscall(int64_t callnum
, ThreadID tid
)
1389 DPRINTF(InOrderCPU
, "[tid:%i] Executing syscall().\n\n", tid
);
1391 DPRINTF(Activity
,"Activity: syscall() called.\n");
1393 // Temporarily increase this by one to account for the syscall
1395 ++(this->thread
[tid
]->funcExeInst
);
1397 // Execute the actual syscall.
1398 this->thread
[tid
]->syscall(callnum
);
1400 // Decrease funcExeInst by one as the normal commit will handle
1402 --(this->thread
[tid
]->funcExeInst
);
1404 // Clear Non-Speculative Block Variable
1405 nonSpecInstActive
[tid
] = false;
1410 InOrderCPU::prefetch(DynInstPtr inst
)
1412 Resource
*mem_res
= resPool
->getResource(dataPortIdx
);
1413 return mem_res
->prefetch(inst
);
1417 InOrderCPU::writeHint(DynInstPtr inst
)
1419 Resource
*mem_res
= resPool
->getResource(dataPortIdx
);
1420 return mem_res
->writeHint(inst
);
1425 InOrderCPU::getITBPtr()
1427 CacheUnit
*itb_res
=
1428 dynamic_cast<CacheUnit
*>(resPool
->getResource(fetchPortIdx
));
1429 return itb_res
->tlb();
1434 InOrderCPU::getDTBPtr()
1436 CacheUnit
*dtb_res
=
1437 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1438 return dtb_res
->tlb();
1443 InOrderCPU::read(DynInstPtr inst
, Addr addr
, T
&data
, unsigned flags
)
1445 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1446 // you want to run w/out caches?
1447 CacheUnit
*cache_res
=
1448 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1450 return cache_res
->read(inst
, addr
, data
, flags
);
1453 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1457 InOrderCPU::read(DynInstPtr inst
, Addr addr
, Twin32_t
&data
, unsigned flags
);
1461 InOrderCPU::read(DynInstPtr inst
, Addr addr
, Twin64_t
&data
, unsigned flags
);
1465 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint64_t &data
, unsigned flags
);
1469 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint32_t &data
, unsigned flags
);
1473 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint16_t &data
, unsigned flags
);
1477 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint8_t &data
, unsigned flags
);
1479 #endif //DOXYGEN_SHOULD_SKIP_THIS
1483 InOrderCPU::read(DynInstPtr inst
, Addr addr
, double &data
, unsigned flags
)
1485 return read(inst
, addr
, *(uint64_t*)&data
, flags
);
1490 InOrderCPU::read(DynInstPtr inst
, Addr addr
, float &data
, unsigned flags
)
1492 return read(inst
, addr
, *(uint32_t*)&data
, flags
);
1498 InOrderCPU::read(DynInstPtr inst
, Addr addr
, int32_t &data
, unsigned flags
)
1500 return read(inst
, addr
, (uint32_t&)data
, flags
);
1505 InOrderCPU::write(DynInstPtr inst
, T data
, Addr addr
, unsigned flags
,
1506 uint64_t *write_res
)
1508 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1509 // you want to run w/out caches?
1510 CacheUnit
*cache_res
=
1511 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1512 return cache_res
->write(inst
, data
, addr
, flags
, write_res
);
1515 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1519 InOrderCPU::write(DynInstPtr inst
, Twin32_t data
, Addr addr
,
1520 unsigned flags
, uint64_t *res
);
1524 InOrderCPU::write(DynInstPtr inst
, Twin64_t data
, Addr addr
,
1525 unsigned flags
, uint64_t *res
);
1529 InOrderCPU::write(DynInstPtr inst
, uint64_t data
, Addr addr
,
1530 unsigned flags
, uint64_t *res
);
1534 InOrderCPU::write(DynInstPtr inst
, uint32_t data
, Addr addr
,
1535 unsigned flags
, uint64_t *res
);
1539 InOrderCPU::write(DynInstPtr inst
, uint16_t data
, Addr addr
,
1540 unsigned flags
, uint64_t *res
);
1544 InOrderCPU::write(DynInstPtr inst
, uint8_t data
, Addr addr
,
1545 unsigned flags
, uint64_t *res
);
1547 #endif //DOXYGEN_SHOULD_SKIP_THIS
1551 InOrderCPU::write(DynInstPtr inst
, double data
, Addr addr
, unsigned flags
,
1554 return write(inst
, *(uint64_t*)&data
, addr
, flags
, res
);
1559 InOrderCPU::write(DynInstPtr inst
, float data
, Addr addr
, unsigned flags
,
1562 return write(inst
, *(uint32_t*)&data
, addr
, flags
, res
);
1568 InOrderCPU::write(DynInstPtr inst
, int32_t data
, Addr addr
, unsigned flags
,
1571 return write(inst
, (uint32_t)data
, addr
, flags
, res
);