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
);
126 case DeallocateThread
:
127 cpu
->deallocateThread(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 %d", cpuEventType
);
147 cpu
->cpuEventRemoveList
.push(this);
153 InOrderCPU::CPUEvent::description()
155 return "InOrderCPU event";
159 InOrderCPU::CPUEvent::scheduleEvent(int delay
)
162 mainEventQueue
.reschedule(this,curTick
+ cpu
->ticks(delay
));
163 else if (!scheduled())
164 mainEventQueue
.schedule(this,curTick
+ cpu
->ticks(delay
));
168 InOrderCPU::CPUEvent::unscheduleEvent()
174 InOrderCPU::InOrderCPU(Params
*params
)
176 cpu_id(params
->cpu_id
),
181 removeInstsThisCycle(false),
182 activityRec(params
->name
, NumStages
, 10, params
->activity
),
184 system(params
->system
),
185 physmem(system
->physmem
),
186 #endif // FULL_SYSTEM
192 deferRegistration(false/*params->deferRegistration*/),
193 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.");
214 if (active_threads
> 1) {
215 threadModel
= (InOrderCPU::ThreadModel
) params
->threadModel
;
217 if (threadModel
== SMT
) {
218 DPRINTF(InOrderCPU
, "Setting Thread Model to SMT.\n");
219 } else if (threadModel
== SwitchOnCacheMiss
) {
220 DPRINTF(InOrderCPU
, "Setting Thread Model to "
221 "Switch On Cache Miss\n");
225 threadModel
= Single
;
232 // Bind the fetch & data ports from the resource pool.
233 fetchPortIdx
= resPool
->getPortIdx(params
->fetchMemPort
);
234 if (fetchPortIdx
== 0) {
235 fatal("Unable to find port to fetch instructions from.\n");
238 dataPortIdx
= resPool
->getPortIdx(params
->dataMemPort
);
239 if (dataPortIdx
== 0) {
240 fatal("Unable to find port for data.\n");
243 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
) {
245 // SMT is not supported in FS mode yet.
246 assert(numThreads
== 1);
247 thread
[tid
] = new Thread(this, 0);
249 if (tid
< (ThreadID
)params
->workload
.size()) {
250 DPRINTF(InOrderCPU
, "Workload[%i] process is %#x\n",
251 tid
, params
->workload
[tid
]->prog_fname
);
253 new Thread(this, tid
, params
->workload
[tid
]);
255 //Allocate Empty thread so M5 can use later
256 //when scheduling threads to CPU
257 Process
* dummy_proc
= params
->workload
[0];
258 thread
[tid
] = new Thread(this, tid
, dummy_proc
);
262 // Setup the TC that will serve as the interface to the threads/CPU.
263 InOrderThreadContext
*tc
= new InOrderThreadContext
;
265 tc
->thread
= thread
[tid
];
267 // Give the thread the TC.
268 thread
[tid
]->tc
= tc
;
269 thread
[tid
]->setFuncExeInst(0);
270 globalSeqNum
[tid
] = 1;
272 // Add the TC to the CPU's list of TC's.
273 this->threadContexts
.push_back(tc
);
276 // Initialize TimeBuffer Stage Queues
277 for (int stNum
=0; stNum
< NumStages
- 1; stNum
++) {
278 stageQueue
[stNum
] = new StageQueue(NumStages
, NumStages
);
279 stageQueue
[stNum
]->id(stNum
);
283 // Set Up Pipeline Stages
284 for (int stNum
=0; stNum
< NumStages
; stNum
++) {
286 pipelineStage
[stNum
] = new FirstStage(params
, stNum
);
288 pipelineStage
[stNum
] = new PipelineStage(params
, stNum
);
290 pipelineStage
[stNum
]->setCPU(this);
291 pipelineStage
[stNum
]->setActiveThreads(&activeThreads
);
292 pipelineStage
[stNum
]->setTimeBuffer(&timeBuffer
);
294 // Take Care of 1st/Nth stages
296 pipelineStage
[stNum
]->setPrevStageQueue(stageQueue
[stNum
- 1]);
297 if (stNum
< NumStages
- 1)
298 pipelineStage
[stNum
]->setNextStageQueue(stageQueue
[stNum
]);
301 // Initialize thread specific variables
302 for (ThreadID tid
= 0; tid
< numThreads
; tid
++) {
303 archRegDepMap
[tid
].setCPU(this);
305 nonSpecInstActive
[tid
] = false;
306 nonSpecSeqNum
[tid
] = 0;
308 squashSeqNum
[tid
] = MaxAddr
;
309 lastSquashCycle
[tid
] = 0;
311 memset(intRegs
[tid
], 0, sizeof(intRegs
[tid
]));
312 memset(floatRegs
.i
[tid
], 0, sizeof(floatRegs
.i
[tid
]));
315 isa
[tid
].expandForMultithreading(numThreads
, 1/*numVirtProcs*/);
318 lastRunningCycle
= curTick
;
320 // Define dummy instructions and resource requests to be used.
321 dummyInst
= new InOrderDynInst(this, NULL
, 0, 0);
322 dummyReq
= new ResourceRequest(resPool
->getResource(0), NULL
, 0, 0, 0, 0);
324 // Reset CPU to reset state.
326 Fault resetFault
= new ResetFault();
327 resetFault
->invoke(tcBase());
332 // Schedule First Tick Event, CPU will reschedule itself from here on out.
333 scheduleTickEvent(0);
338 InOrderCPU::regStats()
340 /* Register the Resource Pool's stats here.*/
345 .name(name() + ".maxResReqCount")
346 .desc("Maximum number of live resource requests in CPU")
347 .prereq(maxResReqCount
);
350 /* Register any of the InOrderCPU's stats here.*/
352 .name(name() + ".timesIdled")
353 .desc("Number of times that the entire CPU went into an idle state and"
354 " unscheduled itself")
358 .name(name() + ".idleCycles")
359 .desc("Total number of cycles that the CPU has spent unscheduled due "
365 .name(name() + ".threadCycles")
366 .desc("Total Number of Cycles A Thread Was Active in CPU (Per-Thread)");
369 .name(name() + ".smtCycles")
370 .desc("Total number of cycles that the CPU was in SMT-mode");
374 .name(name() + ".committedInsts")
375 .desc("Number of Instructions Simulated (Per-Thread)");
379 .name(name() + ".smtCommittedInsts")
380 .desc("Number of SMT Instructions Simulated (Per-Thread)");
383 .name(name() + ".committedInsts_total")
384 .desc("Number of Instructions Simulated (Total)");
387 .name(name() + ".cpi")
388 .desc("CPI: Cycles Per Instruction (Per-Thread)")
390 cpi
= threadCycles
/ committedInsts
;
393 .name(name() + ".smt_cpi")
394 .desc("CPI: Total SMT-CPI")
396 smtCpi
= smtCycles
/ smtCommittedInsts
;
399 .name(name() + ".cpi_total")
400 .desc("CPI: Total CPI of All Threads")
402 totalCpi
= numCycles
/ totalCommittedInsts
;
405 .name(name() + ".ipc")
406 .desc("IPC: Instructions Per Cycle (Per-Thread)")
408 ipc
= committedInsts
/ threadCycles
;
411 .name(name() + ".smt_ipc")
412 .desc("IPC: Total SMT-IPC")
414 smtIpc
= smtCommittedInsts
/ smtCycles
;
417 .name(name() + ".ipc_total")
418 .desc("IPC: Total IPC of All Threads")
420 totalIpc
= totalCommittedInsts
/ numCycles
;
429 DPRINTF(InOrderCPU
, "\n\nInOrderCPU: Ticking main, InOrderCPU.\n");
433 //Tick each of the stages
434 for (int stNum
=NumStages
- 1; stNum
>= 0 ; stNum
--) {
435 pipelineStage
[stNum
]->tick();
438 // Now advance the time buffers one tick
439 timeBuffer
.advance();
440 for (int sqNum
=0; sqNum
< NumStages
- 1; sqNum
++) {
441 stageQueue
[sqNum
]->advance();
443 activityRec
.advance();
445 // Any squashed requests, events, or insts then remove them now
446 cleanUpRemovedReqs();
447 cleanUpRemovedEvents();
448 cleanUpRemovedInsts();
450 // Re-schedule CPU for this cycle
451 if (!tickEvent
.scheduled()) {
452 if (_status
== SwitchedOut
) {
454 lastRunningCycle
= curTick
;
455 } else if (!activityRec
.active()) {
456 DPRINTF(InOrderCPU
, "sleeping CPU.\n");
457 lastRunningCycle
= curTick
;
460 //Tick next_tick = curTick + cycles(1);
461 //tickEvent.schedule(next_tick);
462 mainEventQueue
.schedule(&tickEvent
, nextCycle(curTick
+ 1));
463 DPRINTF(InOrderCPU
, "Scheduled CPU for next tick @ %i.\n",
464 nextCycle(curTick
+ 1));
469 updateThreadPriority();
476 if (!deferRegistration
) {
477 registerThreadContexts();
480 // Set inSyscall so that the CPU doesn't squash when initially
481 // setting up registers.
482 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
)
483 thread
[tid
]->inSyscall
= true;
486 for (ThreadID tid
= 0; tid
< numThreads
; tid
++) {
487 ThreadContext
*src_tc
= threadContexts
[tid
];
488 TheISA::initCPU(src_tc
, src_tc
->contextId());
493 for (ThreadID tid
= 0; tid
< numThreads
; ++tid
)
494 thread
[tid
]->inSyscall
= false;
496 // Call Initializiation Routine for Resource Pool
503 for (int i
= 0; i
< numThreads
; i
++) {
504 isa
[i
].reset(coreType
, numThreads
,
505 1/*numVirtProcs*/, dynamic_cast<BaseCPU
*>(this));
510 InOrderCPU::getPort(const std::string
&if_name
, int idx
)
512 return resPool
->getPort(if_name
, idx
);
517 InOrderCPU::hwrei(ThreadID tid
)
519 panic("hwrei: Unimplemented");
526 InOrderCPU::simPalCheck(int palFunc
, ThreadID tid
)
528 panic("simPalCheck: Unimplemented");
535 InOrderCPU::getInterrupts()
537 // Check if there are any outstanding interrupts
538 return this->interrupts
->getInterrupt(this->threadContexts
[0]);
543 InOrderCPU::processInterrupts(Fault interrupt
)
545 // Check for interrupts here. For now can copy the code that
546 // exists within isa_fullsys_traits.hh. Also assume that thread 0
547 // is the one that handles the interrupts.
548 // @todo: Possibly consolidate the interrupt checking code.
549 // @todo: Allow other threads to handle interrupts.
551 assert(interrupt
!= NoFault
);
552 this->interrupts
->updateIntrInfo(this->threadContexts
[0]);
554 DPRINTF(InOrderCPU
, "Interrupt %s being handled\n", interrupt
->name());
555 this->trap(interrupt
, 0);
560 InOrderCPU::updateMemPorts()
562 // Update all ThreadContext's memory ports (Functional/Virtual
564 ThreadID size
= thread
.size();
565 for (ThreadID i
= 0; i
< size
; ++i
)
566 thread
[i
]->connectMemPorts(thread
[i
]->getTC());
571 InOrderCPU::trap(Fault fault
, ThreadID tid
, int delay
)
573 //@ Squash Pipeline during TRAP
574 scheduleCpuEvent(Trap
, fault
, tid
, dummyInst
, delay
);
578 InOrderCPU::trapCPU(Fault fault
, ThreadID tid
)
580 fault
->invoke(tcBase(tid
));
584 InOrderCPU::squashFromMemStall(DynInstPtr inst
, ThreadID tid
, int delay
)
586 scheduleCpuEvent(SquashFromMemStall
, NoFault
, tid
, inst
, delay
);
591 InOrderCPU::squashDueToMemStall(int stage_num
, InstSeqNum seq_num
, ThreadID tid
)
593 DPRINTF(InOrderCPU
, "Squashing Pipeline Stages Due to Memory Stall...\n");
595 // Squash all instructions in each stage including
596 // instruction that caused the squash (seq_num - 1)
597 // NOTE: The stage bandwidth needs to be cleared so thats why
598 // the stalling instruction is squashed as well. The stalled
599 // instruction is previously placed in another intermediate buffer
600 // while it's stall is being handled.
601 InstSeqNum squash_seq_num
= seq_num
- 1;
603 for (int stNum
=stage_num
; stNum
>= 0 ; stNum
--) {
604 pipelineStage
[stNum
]->squashDueToMemStall(squash_seq_num
, tid
);
609 InOrderCPU::scheduleCpuEvent(CPUEventType c_event
, Fault fault
,
610 ThreadID tid
, DynInstPtr inst
,
611 unsigned delay
, unsigned event_pri_offset
)
613 CPUEvent
*cpu_event
= new CPUEvent(this, c_event
, fault
, tid
, inst
,
617 DPRINTF(InOrderCPU
, "Scheduling CPU Event (%s) for cycle %i, [tid:%i].\n",
618 eventNames
[c_event
], curTick
+ delay
, tid
);
619 mainEventQueue
.schedule(cpu_event
,curTick
+ delay
);
621 cpu_event
->process();
622 cpuEventRemoveList
.push(cpu_event
);
625 // Broadcast event to the Resource Pool
626 // Need to reset tid just in case this is a dummy instruction
628 resPool
->scheduleEvent(c_event
, inst
, 0, 0, tid
);
632 InOrderCPU::isThreadActive(ThreadID tid
)
634 list
<ThreadID
>::iterator isActive
=
635 std::find(activeThreads
.begin(), activeThreads
.end(), tid
);
637 return (isActive
!= activeThreads
.end());
641 InOrderCPU::isThreadReady(ThreadID tid
)
643 list
<ThreadID
>::iterator isReady
=
644 std::find(readyThreads
.begin(), readyThreads
.end(), tid
);
646 return (isReady
!= readyThreads
.end());
650 InOrderCPU::isThreadSuspended(ThreadID tid
)
652 list
<ThreadID
>::iterator isSuspended
=
653 std::find(suspendedThreads
.begin(), suspendedThreads
.end(), tid
);
655 return (isSuspended
!= suspendedThreads
.end());
659 InOrderCPU::activateNextReadyThread()
661 if (readyThreads
.size() >= 1) {
662 ThreadID ready_tid
= readyThreads
.front();
664 // Activate in Pipeline
665 activateThread(ready_tid
);
667 // Activate in Resource Pool
668 resPool
->activateAll(ready_tid
);
670 list
<ThreadID
>::iterator ready_it
=
671 std::find(readyThreads
.begin(), readyThreads
.end(), ready_tid
);
672 readyThreads
.erase(ready_it
);
675 "Attempting to activate new thread, but No Ready Threads to"
678 "Unable to switch to next active thread.\n");
683 InOrderCPU::activateThread(ThreadID tid
)
685 if (isThreadSuspended(tid
)) {
687 "Removing [tid:%i] from suspended threads list.\n", tid
);
689 list
<ThreadID
>::iterator susp_it
=
690 std::find(suspendedThreads
.begin(), suspendedThreads
.end(),
692 suspendedThreads
.erase(susp_it
);
695 if (threadModel
== SwitchOnCacheMiss
&&
696 numActiveThreads() == 1) {
698 "Ignoring activation of [tid:%i], since [tid:%i] is "
699 "already running.\n", tid
, activeThreadId());
701 DPRINTF(InOrderCPU
,"Placing [tid:%i] on ready threads list\n",
704 readyThreads
.push_back(tid
);
706 } else if (!isThreadActive(tid
)) {
708 "Adding [tid:%i] to active threads list.\n", tid
);
709 activeThreads
.push_back(tid
);
711 activateThreadInPipeline(tid
);
713 thread
[tid
]->lastActivate
= curTick
;
720 InOrderCPU::activateThreadInPipeline(ThreadID tid
)
722 for (int stNum
=0; stNum
< NumStages
; stNum
++) {
723 pipelineStage
[stNum
]->activateThread(tid
);
728 InOrderCPU::deactivateContext(ThreadID tid
, int delay
)
730 DPRINTF(InOrderCPU
,"[tid:%i]: Deactivating ...\n", tid
);
732 scheduleCpuEvent(DeactivateThread
, NoFault
, tid
, dummyInst
, delay
);
734 // Be sure to signal that there's some activity so the CPU doesn't
735 // deschedule itself.
736 activityRec
.activity();
742 InOrderCPU::deactivateThread(ThreadID tid
)
744 DPRINTF(InOrderCPU
, "[tid:%i]: Calling deactivate thread.\n", tid
);
746 if (isThreadActive(tid
)) {
747 DPRINTF(InOrderCPU
,"[tid:%i]: Removing from active threads list\n",
749 list
<ThreadID
>::iterator thread_it
=
750 std::find(activeThreads
.begin(), activeThreads
.end(), tid
);
752 removePipelineStalls(*thread_it
);
754 //@TODO: change stage status' to Idle?
756 activeThreads
.erase(thread_it
);
759 assert(!isThreadActive(tid
));
763 InOrderCPU::deallocateContext(ThreadID tid
, int delay
)
765 DPRINTF(InOrderCPU
,"[tid:%i]: Deallocating ...\n", tid
);
767 scheduleCpuEvent(DeallocateThread
, NoFault
, tid
, dummyInst
, delay
);
769 // Be sure to signal that there's some activity so the CPU doesn't
770 // deschedule itself.
771 activityRec
.activity();
777 InOrderCPU::deallocateThread(ThreadID tid
)
779 DPRINTF(InOrderCPU
, "[tid:%i]: Calling deallocate thread.\n", tid
);
781 if (isThreadActive(tid
)) {
782 DPRINTF(InOrderCPU
,"[tid:%i]: Removing from active threads list\n",
784 list
<ThreadID
>::iterator thread_it
=
785 std::find(activeThreads
.begin(), activeThreads
.end(), tid
);
787 removePipelineStalls(*thread_it
);
789 activeThreads
.erase(thread_it
);
792 // TODO: "Un"Load/Unmap register file state
797 InOrderCPU::removePipelineStalls(ThreadID tid
)
799 DPRINTF(InOrderCPU
,"[tid:%i]: Removing all pipeline stalls\n",
802 for (int stNum
= 0; stNum
< NumStages
; stNum
++) {
803 pipelineStage
[stNum
]->removeStalls(tid
);
809 InOrderCPU::updateThreadPriority()
811 if (activeThreads
.size() > 1)
813 //DEFAULT TO ROUND ROBIN SCHEME
814 //e.g. Move highest priority to end of thread list
815 list
<ThreadID
>::iterator list_begin
= activeThreads
.begin();
816 list
<ThreadID
>::iterator list_end
= activeThreads
.end();
818 unsigned high_thread
= *list_begin
;
820 activeThreads
.erase(list_begin
);
822 activeThreads
.push_back(high_thread
);
827 InOrderCPU::tickThreadStats()
829 /** Keep track of cycles that each thread is active */
830 list
<ThreadID
>::iterator thread_it
= activeThreads
.begin();
831 while (thread_it
!= activeThreads
.end()) {
832 threadCycles
[*thread_it
]++;
836 // Keep track of cycles where SMT is active
837 if (activeThreads
.size() > 1) {
843 InOrderCPU::activateContext(ThreadID tid
, int delay
)
845 DPRINTF(InOrderCPU
,"[tid:%i]: Activating ...\n", tid
);
847 scheduleCpuEvent(ActivateThread
, NoFault
, tid
, dummyInst
, delay
);
849 // Be sure to signal that there's some activity so the CPU doesn't
850 // deschedule itself.
851 activityRec
.activity();
857 InOrderCPU::activateNextReadyContext(int delay
)
859 DPRINTF(InOrderCPU
,"Activating next ready thread\n");
861 // NOTE: Add 5 to the event priority so that we always activate
862 // threads after we've finished deactivating, squashing,etc.
864 scheduleCpuEvent(ActivateNextReadyThread
, NoFault
, 0/*tid*/, dummyInst
,
867 // Be sure to signal that there's some activity so the CPU doesn't
868 // deschedule itself.
869 activityRec
.activity();
875 InOrderCPU::haltContext(ThreadID tid
, int delay
)
877 suspendContext(tid
, delay
);
881 InOrderCPU::suspendContext(ThreadID tid
, int delay
)
883 scheduleCpuEvent(SuspendThread
, NoFault
, tid
, dummyInst
, delay
);
888 InOrderCPU::suspendThread(ThreadID tid
)
890 DPRINTF(InOrderCPU
, "[tid: %i]: Placing on Suspended Threads List...\n", tid
);
891 deactivateThread(tid
);
892 suspendedThreads
.push_back(tid
);
893 thread
[tid
]->lastSuspend
= curTick
;
897 InOrderCPU::squashThreadInPipeline(ThreadID tid
)
899 //Squash all instructions in each stage
900 for (int stNum
=NumStages
- 1; stNum
>= 0 ; stNum
--) {
901 pipelineStage
[stNum
]->squash(0 /*seq_num*/, tid
);
906 InOrderCPU::getPipeStage(int stage_num
)
908 return pipelineStage
[stage_num
];
912 InOrderCPU::readPC(ThreadID tid
)
919 InOrderCPU::setPC(Addr new_PC
, ThreadID tid
)
926 InOrderCPU::readNextPC(ThreadID tid
)
933 InOrderCPU::setNextPC(uint64_t new_NPC
, ThreadID tid
)
935 nextPC
[tid
] = new_NPC
;
940 InOrderCPU::readNextNPC(ThreadID tid
)
947 InOrderCPU::setNextNPC(uint64_t new_NNPC
, ThreadID tid
)
949 nextNPC
[tid
] = new_NNPC
;
953 InOrderCPU::readIntReg(int reg_idx
, ThreadID tid
)
955 return intRegs
[tid
][reg_idx
];
959 InOrderCPU::readFloatReg(int reg_idx
, ThreadID tid
)
961 return floatRegs
.f
[tid
][reg_idx
];
965 InOrderCPU::readFloatRegBits(int reg_idx
, ThreadID tid
)
967 return floatRegs
.i
[tid
][reg_idx
];
971 InOrderCPU::setIntReg(int reg_idx
, uint64_t val
, ThreadID tid
)
973 intRegs
[tid
][reg_idx
] = val
;
978 InOrderCPU::setFloatReg(int reg_idx
, FloatReg val
, ThreadID tid
)
980 floatRegs
.f
[tid
][reg_idx
] = val
;
985 InOrderCPU::setFloatRegBits(int reg_idx
, FloatRegBits val
, ThreadID tid
)
987 floatRegs
.i
[tid
][reg_idx
] = val
;
991 InOrderCPU::readRegOtherThread(unsigned reg_idx
, ThreadID tid
)
993 // If Default value is set, then retrieve target thread
994 if (tid
== InvalidThreadID
) {
995 tid
= TheISA::getTargetThread(tcBase(tid
));
998 if (reg_idx
< FP_Base_DepTag
) {
999 // Integer Register File
1000 return readIntReg(reg_idx
, tid
);
1001 } else if (reg_idx
< Ctrl_Base_DepTag
) {
1002 // Float Register File
1003 reg_idx
-= FP_Base_DepTag
;
1004 return readFloatRegBits(reg_idx
, tid
);
1006 reg_idx
-= Ctrl_Base_DepTag
;
1007 return readMiscReg(reg_idx
, tid
); // Misc. Register File
1011 InOrderCPU::setRegOtherThread(unsigned reg_idx
, const MiscReg
&val
,
1014 // If Default value is set, then retrieve target thread
1015 if (tid
== InvalidThreadID
) {
1016 tid
= TheISA::getTargetThread(tcBase(tid
));
1019 if (reg_idx
< FP_Base_DepTag
) { // Integer Register File
1020 setIntReg(reg_idx
, val
, tid
);
1021 } else if (reg_idx
< Ctrl_Base_DepTag
) { // Float Register File
1022 reg_idx
-= FP_Base_DepTag
;
1023 setFloatRegBits(reg_idx
, val
, tid
);
1025 reg_idx
-= Ctrl_Base_DepTag
;
1026 setMiscReg(reg_idx
, val
, tid
); // Misc. Register File
1031 InOrderCPU::readMiscRegNoEffect(int misc_reg
, ThreadID tid
)
1033 return isa
[tid
].readMiscRegNoEffect(misc_reg
);
1037 InOrderCPU::readMiscReg(int misc_reg
, ThreadID tid
)
1039 return isa
[tid
].readMiscReg(misc_reg
, tcBase(tid
));
1043 InOrderCPU::setMiscRegNoEffect(int misc_reg
, const MiscReg
&val
, ThreadID tid
)
1045 isa
[tid
].setMiscRegNoEffect(misc_reg
, val
);
1049 InOrderCPU::setMiscReg(int misc_reg
, const MiscReg
&val
, ThreadID tid
)
1051 isa
[tid
].setMiscReg(misc_reg
, val
, tcBase(tid
));
1056 InOrderCPU::addInst(DynInstPtr
&inst
)
1058 ThreadID tid
= inst
->readTid();
1060 instList
[tid
].push_back(inst
);
1062 return --(instList
[tid
].end());
1066 InOrderCPU::instDone(DynInstPtr inst
, ThreadID tid
)
1068 // Set the CPU's PCs - This contributes to the precise state of the CPU
1069 // which can be used when restoring a thread to the CPU after after any
1070 // type of context switching activity (fork, exception, etc.)
1071 setPC(inst
->readPC(), tid
);
1072 setNextPC(inst
->readNextPC(), tid
);
1073 setNextNPC(inst
->readNextNPC(), tid
);
1075 if (inst
->isControl()) {
1076 thread
[tid
]->lastGradIsBranch
= true;
1077 thread
[tid
]->lastBranchPC
= inst
->readPC();
1078 thread
[tid
]->lastBranchNextPC
= inst
->readNextPC();
1079 thread
[tid
]->lastBranchNextNPC
= inst
->readNextNPC();
1081 thread
[tid
]->lastGradIsBranch
= false;
1085 // Finalize Trace Data For Instruction
1086 if (inst
->traceData
) {
1087 //inst->traceData->setCycle(curTick);
1088 inst
->traceData
->setFetchSeq(inst
->seqNum
);
1089 //inst->traceData->setCPSeq(cpu->tcBase(tid)->numInst);
1090 inst
->traceData
->dump();
1091 delete inst
->traceData
;
1092 inst
->traceData
= NULL
;
1095 // Increment thread-state's instruction count
1096 thread
[tid
]->numInst
++;
1098 // Increment thread-state's instruction stats
1099 thread
[tid
]->numInsts
++;
1101 // Count committed insts per thread stats
1102 committedInsts
[tid
]++;
1104 // Count total insts committed stat
1105 totalCommittedInsts
++;
1107 // Count SMT-committed insts per thread stat
1108 if (numActiveThreads() > 1) {
1109 smtCommittedInsts
[tid
]++;
1112 // Check for instruction-count-based events.
1113 comInstEventQueue
[tid
]->serviceEvents(thread
[tid
]->numInst
);
1115 // Broadcast to other resources an instruction
1116 // has been completed
1117 resPool
->scheduleEvent((CPUEventType
)ResourcePool::InstGraduated
, inst
,
1120 // Finally, remove instruction from CPU
1125 InOrderCPU::addToRemoveList(DynInstPtr
&inst
)
1127 removeInstsThisCycle
= true;
1129 removeList
.push(inst
->getInstListIt());
1133 InOrderCPU::removeInst(DynInstPtr
&inst
)
1135 DPRINTF(InOrderCPU
, "Removing graduated instruction [tid:%i] PC %#x "
1137 inst
->threadNumber
, inst
->readPC(), inst
->seqNum
);
1139 removeInstsThisCycle
= true;
1141 // Remove the instruction.
1142 removeList
.push(inst
->getInstListIt());
1146 InOrderCPU::removeInstsUntil(const InstSeqNum
&seq_num
, ThreadID tid
)
1148 //assert(!instList[tid].empty());
1150 removeInstsThisCycle
= true;
1152 ListIt inst_iter
= instList
[tid
].end();
1156 DPRINTF(InOrderCPU
, "Deleting instructions from CPU instruction "
1157 "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1158 tid
, seq_num
, (*inst_iter
)->seqNum
);
1160 while ((*inst_iter
)->seqNum
> seq_num
) {
1162 bool break_loop
= (inst_iter
== instList
[tid
].begin());
1164 squashInstIt(inst_iter
, tid
);
1175 InOrderCPU::squashInstIt(const ListIt
&instIt
, ThreadID tid
)
1177 if ((*instIt
)->threadNumber
== tid
) {
1178 DPRINTF(InOrderCPU
, "Squashing instruction, "
1179 "[tid:%i] [sn:%lli] PC %#x\n",
1180 (*instIt
)->threadNumber
,
1182 (*instIt
)->readPC());
1184 (*instIt
)->setSquashed();
1186 removeList
.push(instIt
);
1192 InOrderCPU::cleanUpRemovedInsts()
1194 while (!removeList
.empty()) {
1195 DPRINTF(InOrderCPU
, "Removing instruction, "
1196 "[tid:%i] [sn:%lli] PC %#x\n",
1197 (*removeList
.front())->threadNumber
,
1198 (*removeList
.front())->seqNum
,
1199 (*removeList
.front())->readPC());
1201 DynInstPtr inst
= *removeList
.front();
1202 ThreadID tid
= inst
->threadNumber
;
1204 // Make Sure Resource Schedule Is Emptied Out
1205 ThePipeline::ResSchedule
*inst_sched
= &inst
->resSched
;
1206 while (!inst_sched
->empty()) {
1207 ThePipeline::ScheduleEntry
* sch_entry
= inst_sched
->top();
1212 // Remove From Register Dependency Map, If Necessary
1213 archRegDepMap
[(*removeList
.front())->threadNumber
].
1214 remove((*removeList
.front()));
1217 // Clear if Non-Speculative
1218 if (inst
->staticInst
&&
1219 inst
->seqNum
== nonSpecSeqNum
[tid
] &&
1220 nonSpecInstActive
[tid
] == true) {
1221 nonSpecInstActive
[tid
] = false;
1224 instList
[tid
].erase(removeList
.front());
1228 DPRINTF(RefCount
, "pop from remove list: [sn:%i]: Refcount = %i.\n",
1230 0/*inst->curCount()*/);
1234 removeInstsThisCycle
= false;
1238 InOrderCPU::cleanUpRemovedReqs()
1240 while (!reqRemoveList
.empty()) {
1241 ResourceRequest
*res_req
= reqRemoveList
.front();
1243 DPRINTF(RefCount
, "[tid:%i]: Removing Request, "
1244 "[sn:%lli] [slot:%i] [stage_num:%i] [res:%s] [refcount:%i].\n",
1245 res_req
->inst
->threadNumber
,
1246 res_req
->inst
->seqNum
,
1248 res_req
->getStageNum(),
1249 res_req
->res
->name(),
1250 0/*res_req->inst->curCount()*/);
1252 reqRemoveList
.pop();
1256 DPRINTF(RefCount
, "after remove request: [sn:%i]: Refcount = %i.\n",
1257 res_req
->inst
->seqNum
,
1258 0/*res_req->inst->curCount()*/);
1263 InOrderCPU::cleanUpRemovedEvents()
1265 while (!cpuEventRemoveList
.empty()) {
1266 Event
*cpu_event
= cpuEventRemoveList
.front();
1267 cpuEventRemoveList
.pop();
1274 InOrderCPU::dumpInsts()
1278 ListIt inst_list_it
= instList
[0].begin();
1280 cprintf("Dumping Instruction List\n");
1282 while (inst_list_it
!= instList
[0].end()) {
1283 cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1285 num
, (*inst_list_it
)->readPC(), (*inst_list_it
)->threadNumber
,
1286 (*inst_list_it
)->seqNum
, (*inst_list_it
)->isIssued(),
1287 (*inst_list_it
)->isSquashed());
1294 InOrderCPU::wakeCPU()
1296 if (/*activityRec.active() || */tickEvent
.scheduled()) {
1297 DPRINTF(Activity
, "CPU already running.\n");
1301 DPRINTF(Activity
, "Waking up CPU\n");
1303 //@todo: figure out how to count idleCycles correctly
1304 //idleCycles += (curTick - 1) - lastRunningCycle;
1306 mainEventQueue
.schedule(&tickEvent
, curTick
);
1312 InOrderCPU::wakeup()
1314 if (this->thread
[0]->status() != ThreadContext::Suspended
)
1319 DPRINTF(Quiesce
, "Suspended Processor woken\n");
1320 this->threadContexts
[0]->activate();
1326 InOrderCPU::syscall(int64_t callnum
, ThreadID tid
)
1328 DPRINTF(InOrderCPU
, "[tid:%i] Executing syscall().\n\n", tid
);
1330 DPRINTF(Activity
,"Activity: syscall() called.\n");
1332 // Temporarily increase this by one to account for the syscall
1334 ++(this->thread
[tid
]->funcExeInst
);
1336 // Execute the actual syscall.
1337 this->thread
[tid
]->syscall(callnum
);
1339 // Decrease funcExeInst by one as the normal commit will handle
1341 --(this->thread
[tid
]->funcExeInst
);
1343 // Clear Non-Speculative Block Variable
1344 nonSpecInstActive
[tid
] = false;
1349 InOrderCPU::prefetch(DynInstPtr inst
)
1351 Resource
*mem_res
= resPool
->getResource(dataPortIdx
);
1352 return mem_res
->prefetch(inst
);
1356 InOrderCPU::writeHint(DynInstPtr inst
)
1358 Resource
*mem_res
= resPool
->getResource(dataPortIdx
);
1359 return mem_res
->writeHint(inst
);
1364 InOrderCPU::getITBPtr()
1366 CacheUnit
*itb_res
=
1367 dynamic_cast<CacheUnit
*>(resPool
->getResource(fetchPortIdx
));
1368 return itb_res
->tlb();
1373 InOrderCPU::getDTBPtr()
1375 CacheUnit
*dtb_res
=
1376 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1377 return dtb_res
->tlb();
1382 InOrderCPU::read(DynInstPtr inst
, Addr addr
, T
&data
, unsigned flags
)
1384 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1385 // you want to run w/out caches?
1386 CacheUnit
*cache_res
=
1387 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1389 return cache_res
->read(inst
, addr
, data
, flags
);
1392 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1396 InOrderCPU::read(DynInstPtr inst
, Addr addr
, Twin32_t
&data
, unsigned flags
);
1400 InOrderCPU::read(DynInstPtr inst
, Addr addr
, Twin64_t
&data
, unsigned flags
);
1404 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint64_t &data
, unsigned flags
);
1408 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint32_t &data
, unsigned flags
);
1412 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint16_t &data
, unsigned flags
);
1416 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint8_t &data
, unsigned flags
);
1418 #endif //DOXYGEN_SHOULD_SKIP_THIS
1422 InOrderCPU::read(DynInstPtr inst
, Addr addr
, double &data
, unsigned flags
)
1424 return read(inst
, addr
, *(uint64_t*)&data
, flags
);
1429 InOrderCPU::read(DynInstPtr inst
, Addr addr
, float &data
, unsigned flags
)
1431 return read(inst
, addr
, *(uint32_t*)&data
, flags
);
1437 InOrderCPU::read(DynInstPtr inst
, Addr addr
, int32_t &data
, unsigned flags
)
1439 return read(inst
, addr
, (uint32_t&)data
, flags
);
1444 InOrderCPU::write(DynInstPtr inst
, T data
, Addr addr
, unsigned flags
,
1445 uint64_t *write_res
)
1447 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1448 // you want to run w/out caches?
1449 CacheUnit
*cache_res
=
1450 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1451 return cache_res
->write(inst
, data
, addr
, flags
, write_res
);
1454 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1458 InOrderCPU::write(DynInstPtr inst
, Twin32_t data
, Addr addr
,
1459 unsigned flags
, uint64_t *res
);
1463 InOrderCPU::write(DynInstPtr inst
, Twin64_t data
, Addr addr
,
1464 unsigned flags
, uint64_t *res
);
1468 InOrderCPU::write(DynInstPtr inst
, uint64_t data
, Addr addr
,
1469 unsigned flags
, uint64_t *res
);
1473 InOrderCPU::write(DynInstPtr inst
, uint32_t data
, Addr addr
,
1474 unsigned flags
, uint64_t *res
);
1478 InOrderCPU::write(DynInstPtr inst
, uint16_t data
, Addr addr
,
1479 unsigned flags
, uint64_t *res
);
1483 InOrderCPU::write(DynInstPtr inst
, uint8_t data
, Addr addr
,
1484 unsigned flags
, uint64_t *res
);
1486 #endif //DOXYGEN_SHOULD_SKIP_THIS
1490 InOrderCPU::write(DynInstPtr inst
, double data
, Addr addr
, unsigned flags
,
1493 return write(inst
, *(uint64_t*)&data
, addr
, flags
, res
);
1498 InOrderCPU::write(DynInstPtr inst
, float data
, Addr addr
, unsigned flags
,
1501 return write(inst
, *(uint32_t*)&data
, addr
, flags
, res
);
1507 InOrderCPU::write(DynInstPtr inst
, int32_t data
, Addr addr
, unsigned flags
,
1510 return write(inst
, (uint32_t)data
, addr
, flags
, res
);