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"
681 InOrderCPU::activateThread(ThreadID tid
)
683 if (isThreadSuspended(tid
)) {
685 "Removing [tid:%i] from suspended threads list.\n", tid
);
687 list
<ThreadID
>::iterator susp_it
=
688 std::find(suspendedThreads
.begin(), suspendedThreads
.end(),
690 suspendedThreads
.erase(susp_it
);
693 if (threadModel
== SwitchOnCacheMiss
&&
694 numActiveThreads() == 1) {
696 "Ignoring activation of [tid:%i], since [tid:%i] is "
697 "already running.\n", tid
, activeThreadId());
699 DPRINTF(InOrderCPU
,"Placing [tid:%i] ready threads list\n",
702 readyThreads
.push_back(tid
);
704 } else if (!isThreadActive(tid
)) {
706 "Adding [tid:%i] to active threads list.\n", tid
);
707 activeThreads
.push_back(tid
);
714 InOrderCPU::deactivateContext(ThreadID tid
, int delay
)
716 DPRINTF(InOrderCPU
,"[tid:%i]: Deactivating ...\n", tid
);
718 scheduleCpuEvent(DeactivateThread
, NoFault
, tid
, dummyInst
, delay
);
720 // Be sure to signal that there's some activity so the CPU doesn't
721 // deschedule itself.
722 activityRec
.activity();
728 InOrderCPU::deactivateThread(ThreadID tid
)
730 DPRINTF(InOrderCPU
, "[tid:%i]: Calling deactivate thread.\n", tid
);
732 if (isThreadActive(tid
)) {
733 DPRINTF(InOrderCPU
,"[tid:%i]: Removing from active threads list\n",
735 list
<ThreadID
>::iterator thread_it
=
736 std::find(activeThreads
.begin(), activeThreads
.end(), tid
);
738 removePipelineStalls(*thread_it
);
740 //@TODO: change stage status' to Idle?
742 activeThreads
.erase(thread_it
);
745 assert(!isThreadActive(tid
));
749 InOrderCPU::deallocateContext(ThreadID tid
, int delay
)
751 DPRINTF(InOrderCPU
,"[tid:%i]: Deallocating ...\n", tid
);
753 scheduleCpuEvent(DeallocateThread
, NoFault
, tid
, dummyInst
, delay
);
755 // Be sure to signal that there's some activity so the CPU doesn't
756 // deschedule itself.
757 activityRec
.activity();
763 InOrderCPU::deallocateThread(ThreadID tid
)
765 DPRINTF(InOrderCPU
, "[tid:%i]: Calling deallocate thread.\n", tid
);
767 if (isThreadActive(tid
)) {
768 DPRINTF(InOrderCPU
,"[tid:%i]: Removing from active threads list\n",
770 list
<ThreadID
>::iterator thread_it
=
771 std::find(activeThreads
.begin(), activeThreads
.end(), tid
);
773 removePipelineStalls(*thread_it
);
775 activeThreads
.erase(thread_it
);
778 // TODO: "Un"Load/Unmap register file state
783 InOrderCPU::removePipelineStalls(ThreadID tid
)
785 DPRINTF(InOrderCPU
,"[tid:%i]: Removing all pipeline stalls\n",
788 for (int stNum
= 0; stNum
< NumStages
; stNum
++) {
789 pipelineStage
[stNum
]->removeStalls(tid
);
795 InOrderCPU::updateThreadPriority()
797 if (activeThreads
.size() > 1)
799 //DEFAULT TO ROUND ROBIN SCHEME
800 //e.g. Move highest priority to end of thread list
801 list
<ThreadID
>::iterator list_begin
= activeThreads
.begin();
802 list
<ThreadID
>::iterator list_end
= activeThreads
.end();
804 unsigned high_thread
= *list_begin
;
806 activeThreads
.erase(list_begin
);
808 activeThreads
.push_back(high_thread
);
813 InOrderCPU::tickThreadStats()
815 /** Keep track of cycles that each thread is active */
816 list
<ThreadID
>::iterator thread_it
= activeThreads
.begin();
817 while (thread_it
!= activeThreads
.end()) {
818 threadCycles
[*thread_it
]++;
822 // Keep track of cycles where SMT is active
823 if (activeThreads
.size() > 1) {
829 InOrderCPU::activateContext(ThreadID tid
, int delay
)
831 DPRINTF(InOrderCPU
,"[tid:%i]: Activating ...\n", tid
);
833 scheduleCpuEvent(ActivateThread
, NoFault
, tid
, dummyInst
, delay
);
835 // Be sure to signal that there's some activity so the CPU doesn't
836 // deschedule itself.
837 activityRec
.activity();
843 InOrderCPU::activateNextReadyContext(int delay
)
845 DPRINTF(InOrderCPU
,"Activating next ready thread\n");
847 // NOTE: Add 5 to the event priority so that we always activate
848 // threads after we've finished deactivating, squashing,etc.
850 scheduleCpuEvent(ActivateNextReadyThread
, NoFault
, 0/*tid*/, dummyInst
,
853 // Be sure to signal that there's some activity so the CPU doesn't
854 // deschedule itself.
855 activityRec
.activity();
861 InOrderCPU::haltContext(ThreadID tid
, int delay
)
863 suspendContext(tid
, delay
);
867 InOrderCPU::suspendContext(ThreadID tid
, int delay
)
869 scheduleCpuEvent(SuspendThread
, NoFault
, tid
, dummyInst
, delay
);
874 InOrderCPU::suspendThread(ThreadID tid
)
876 DPRINTF(InOrderCPU
, "[tid: %i]: Placing on Suspended Threads List...\n", tid
);
877 deactivateThread(tid
);
878 suspendedThreads
.push_back(tid
);
882 InOrderCPU::squashThreadInPipeline(ThreadID tid
)
884 //Squash all instructions in each stage
885 for (int stNum
=NumStages
- 1; stNum
>= 0 ; stNum
--) {
886 pipelineStage
[stNum
]->squash(0 /*seq_num*/, tid
);
891 InOrderCPU::getPipeStage(int stage_num
)
893 return pipelineStage
[stage_num
];
897 InOrderCPU::readPC(ThreadID tid
)
904 InOrderCPU::setPC(Addr new_PC
, ThreadID tid
)
911 InOrderCPU::readNextPC(ThreadID tid
)
918 InOrderCPU::setNextPC(uint64_t new_NPC
, ThreadID tid
)
920 nextPC
[tid
] = new_NPC
;
925 InOrderCPU::readNextNPC(ThreadID tid
)
932 InOrderCPU::setNextNPC(uint64_t new_NNPC
, ThreadID tid
)
934 nextNPC
[tid
] = new_NNPC
;
938 InOrderCPU::readIntReg(int reg_idx
, ThreadID tid
)
940 return intRegs
[tid
][reg_idx
];
944 InOrderCPU::readFloatReg(int reg_idx
, ThreadID tid
)
946 return floatRegs
.f
[tid
][reg_idx
];
950 InOrderCPU::readFloatRegBits(int reg_idx
, ThreadID tid
)
952 return floatRegs
.i
[tid
][reg_idx
];
956 InOrderCPU::setIntReg(int reg_idx
, uint64_t val
, ThreadID tid
)
958 intRegs
[tid
][reg_idx
] = val
;
963 InOrderCPU::setFloatReg(int reg_idx
, FloatReg val
, ThreadID tid
)
965 floatRegs
.f
[tid
][reg_idx
] = val
;
970 InOrderCPU::setFloatRegBits(int reg_idx
, FloatRegBits val
, ThreadID tid
)
972 floatRegs
.i
[tid
][reg_idx
] = val
;
976 InOrderCPU::readRegOtherThread(unsigned reg_idx
, ThreadID tid
)
978 // If Default value is set, then retrieve target thread
979 if (tid
== InvalidThreadID
) {
980 tid
= TheISA::getTargetThread(tcBase(tid
));
983 if (reg_idx
< FP_Base_DepTag
) {
984 // Integer Register File
985 return readIntReg(reg_idx
, tid
);
986 } else if (reg_idx
< Ctrl_Base_DepTag
) {
987 // Float Register File
988 reg_idx
-= FP_Base_DepTag
;
989 return readFloatRegBits(reg_idx
, tid
);
991 reg_idx
-= Ctrl_Base_DepTag
;
992 return readMiscReg(reg_idx
, tid
); // Misc. Register File
996 InOrderCPU::setRegOtherThread(unsigned reg_idx
, const MiscReg
&val
,
999 // If Default value is set, then retrieve target thread
1000 if (tid
== InvalidThreadID
) {
1001 tid
= TheISA::getTargetThread(tcBase(tid
));
1004 if (reg_idx
< FP_Base_DepTag
) { // Integer Register File
1005 setIntReg(reg_idx
, val
, tid
);
1006 } else if (reg_idx
< Ctrl_Base_DepTag
) { // Float Register File
1007 reg_idx
-= FP_Base_DepTag
;
1008 setFloatRegBits(reg_idx
, val
, tid
);
1010 reg_idx
-= Ctrl_Base_DepTag
;
1011 setMiscReg(reg_idx
, val
, tid
); // Misc. Register File
1016 InOrderCPU::readMiscRegNoEffect(int misc_reg
, ThreadID tid
)
1018 return isa
[tid
].readMiscRegNoEffect(misc_reg
);
1022 InOrderCPU::readMiscReg(int misc_reg
, ThreadID tid
)
1024 return isa
[tid
].readMiscReg(misc_reg
, tcBase(tid
));
1028 InOrderCPU::setMiscRegNoEffect(int misc_reg
, const MiscReg
&val
, ThreadID tid
)
1030 isa
[tid
].setMiscRegNoEffect(misc_reg
, val
);
1034 InOrderCPU::setMiscReg(int misc_reg
, const MiscReg
&val
, ThreadID tid
)
1036 isa
[tid
].setMiscReg(misc_reg
, val
, tcBase(tid
));
1041 InOrderCPU::addInst(DynInstPtr
&inst
)
1043 ThreadID tid
= inst
->readTid();
1045 instList
[tid
].push_back(inst
);
1047 return --(instList
[tid
].end());
1051 InOrderCPU::instDone(DynInstPtr inst
, ThreadID tid
)
1053 // Set the CPU's PCs - This contributes to the precise state of the CPU
1054 // which can be used when restoring a thread to the CPU after a fork or
1055 // after an exception
1056 // =================
1057 // @TODO: Set-Up Grad-Info/Committed-Info to let ThreadState know if
1058 // it's a branch or not
1059 setPC(inst
->readPC(), tid
);
1060 setNextPC(inst
->readNextPC(), tid
);
1061 setNextNPC(inst
->readNextNPC(), tid
);
1063 // Finalize Trace Data For Instruction
1064 if (inst
->traceData
) {
1065 //inst->traceData->setCycle(curTick);
1066 inst
->traceData
->setFetchSeq(inst
->seqNum
);
1067 //inst->traceData->setCPSeq(cpu->tcBase(tid)->numInst);
1068 inst
->traceData
->dump();
1069 delete inst
->traceData
;
1070 inst
->traceData
= NULL
;
1073 // Set Last Graduated Instruction In Thread State
1074 //thread[tid]->lastGradInst = inst;
1076 // Increment thread-state's instruction count
1077 thread
[tid
]->numInst
++;
1079 // Increment thread-state's instruction stats
1080 thread
[tid
]->numInsts
++;
1082 // Count committed insts per thread stats
1083 committedInsts
[tid
]++;
1085 // Count total insts committed stat
1086 totalCommittedInsts
++;
1088 // Count SMT-committed insts per thread stat
1089 if (numActiveThreads() > 1) {
1090 smtCommittedInsts
[tid
]++;
1093 // Check for instruction-count-based events.
1094 comInstEventQueue
[tid
]->serviceEvents(thread
[tid
]->numInst
);
1096 // Broadcast to other resources an instruction
1097 // has been completed
1098 resPool
->scheduleEvent((CPUEventType
)ResourcePool::InstGraduated
, inst
,
1101 // Finally, remove instruction from CPU
1106 InOrderCPU::addToRemoveList(DynInstPtr
&inst
)
1108 removeInstsThisCycle
= true;
1110 removeList
.push(inst
->getInstListIt());
1114 InOrderCPU::removeInst(DynInstPtr
&inst
)
1116 DPRINTF(InOrderCPU
, "Removing graduated instruction [tid:%i] PC %#x "
1118 inst
->threadNumber
, inst
->readPC(), inst
->seqNum
);
1120 removeInstsThisCycle
= true;
1122 // Remove the instruction.
1123 removeList
.push(inst
->getInstListIt());
1127 InOrderCPU::removeInstsUntil(const InstSeqNum
&seq_num
, ThreadID tid
)
1129 //assert(!instList[tid].empty());
1131 removeInstsThisCycle
= true;
1133 ListIt inst_iter
= instList
[tid
].end();
1137 DPRINTF(InOrderCPU
, "Deleting instructions from CPU instruction "
1138 "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1139 tid
, seq_num
, (*inst_iter
)->seqNum
);
1141 while ((*inst_iter
)->seqNum
> seq_num
) {
1143 bool break_loop
= (inst_iter
== instList
[tid
].begin());
1145 squashInstIt(inst_iter
, tid
);
1156 InOrderCPU::squashInstIt(const ListIt
&instIt
, ThreadID tid
)
1158 if ((*instIt
)->threadNumber
== tid
) {
1159 DPRINTF(InOrderCPU
, "Squashing instruction, "
1160 "[tid:%i] [sn:%lli] PC %#x\n",
1161 (*instIt
)->threadNumber
,
1163 (*instIt
)->readPC());
1165 (*instIt
)->setSquashed();
1167 removeList
.push(instIt
);
1173 InOrderCPU::cleanUpRemovedInsts()
1175 while (!removeList
.empty()) {
1176 DPRINTF(InOrderCPU
, "Removing instruction, "
1177 "[tid:%i] [sn:%lli] PC %#x\n",
1178 (*removeList
.front())->threadNumber
,
1179 (*removeList
.front())->seqNum
,
1180 (*removeList
.front())->readPC());
1182 DynInstPtr inst
= *removeList
.front();
1183 ThreadID tid
= inst
->threadNumber
;
1185 // Make Sure Resource Schedule Is Emptied Out
1186 ThePipeline::ResSchedule
*inst_sched
= &inst
->resSched
;
1187 while (!inst_sched
->empty()) {
1188 ThePipeline::ScheduleEntry
* sch_entry
= inst_sched
->top();
1193 // Remove From Register Dependency Map, If Necessary
1194 archRegDepMap
[(*removeList
.front())->threadNumber
].
1195 remove((*removeList
.front()));
1198 // Clear if Non-Speculative
1199 if (inst
->staticInst
&&
1200 inst
->seqNum
== nonSpecSeqNum
[tid
] &&
1201 nonSpecInstActive
[tid
] == true) {
1202 nonSpecInstActive
[tid
] = false;
1205 instList
[tid
].erase(removeList
.front());
1209 DPRINTF(RefCount
, "pop from remove list: [sn:%i]: Refcount = %i.\n",
1211 0/*inst->curCount()*/);
1215 removeInstsThisCycle
= false;
1219 InOrderCPU::cleanUpRemovedReqs()
1221 while (!reqRemoveList
.empty()) {
1222 ResourceRequest
*res_req
= reqRemoveList
.front();
1224 DPRINTF(RefCount
, "[tid:%i]: Removing Request, "
1225 "[sn:%lli] [slot:%i] [stage_num:%i] [res:%s] [refcount:%i].\n",
1226 res_req
->inst
->threadNumber
,
1227 res_req
->inst
->seqNum
,
1229 res_req
->getStageNum(),
1230 res_req
->res
->name(),
1231 0/*res_req->inst->curCount()*/);
1233 reqRemoveList
.pop();
1237 DPRINTF(RefCount
, "after remove request: [sn:%i]: Refcount = %i.\n",
1238 res_req
->inst
->seqNum
,
1239 0/*res_req->inst->curCount()*/);
1244 InOrderCPU::cleanUpRemovedEvents()
1246 while (!cpuEventRemoveList
.empty()) {
1247 Event
*cpu_event
= cpuEventRemoveList
.front();
1248 cpuEventRemoveList
.pop();
1255 InOrderCPU::dumpInsts()
1259 ListIt inst_list_it
= instList
[0].begin();
1261 cprintf("Dumping Instruction List\n");
1263 while (inst_list_it
!= instList
[0].end()) {
1264 cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1266 num
, (*inst_list_it
)->readPC(), (*inst_list_it
)->threadNumber
,
1267 (*inst_list_it
)->seqNum
, (*inst_list_it
)->isIssued(),
1268 (*inst_list_it
)->isSquashed());
1275 InOrderCPU::wakeCPU()
1277 if (/*activityRec.active() || */tickEvent
.scheduled()) {
1278 DPRINTF(Activity
, "CPU already running.\n");
1282 DPRINTF(Activity
, "Waking up CPU\n");
1284 //@todo: figure out how to count idleCycles correctly
1285 //idleCycles += (curTick - 1) - lastRunningCycle;
1287 mainEventQueue
.schedule(&tickEvent
, curTick
);
1293 InOrderCPU::wakeup()
1295 if (this->thread
[0]->status() != ThreadContext::Suspended
)
1300 DPRINTF(Quiesce
, "Suspended Processor woken\n");
1301 this->threadContexts
[0]->activate();
1307 InOrderCPU::syscall(int64_t callnum
, ThreadID tid
)
1309 DPRINTF(InOrderCPU
, "[tid:%i] Executing syscall().\n\n", tid
);
1311 DPRINTF(Activity
,"Activity: syscall() called.\n");
1313 // Temporarily increase this by one to account for the syscall
1315 ++(this->thread
[tid
]->funcExeInst
);
1317 // Execute the actual syscall.
1318 this->thread
[tid
]->syscall(callnum
);
1320 // Decrease funcExeInst by one as the normal commit will handle
1322 --(this->thread
[tid
]->funcExeInst
);
1324 // Clear Non-Speculative Block Variable
1325 nonSpecInstActive
[tid
] = false;
1330 InOrderCPU::prefetch(DynInstPtr inst
)
1332 Resource
*mem_res
= resPool
->getResource(dataPortIdx
);
1333 return mem_res
->prefetch(inst
);
1337 InOrderCPU::writeHint(DynInstPtr inst
)
1339 Resource
*mem_res
= resPool
->getResource(dataPortIdx
);
1340 return mem_res
->writeHint(inst
);
1345 InOrderCPU::getITBPtr()
1347 CacheUnit
*itb_res
=
1348 dynamic_cast<CacheUnit
*>(resPool
->getResource(fetchPortIdx
));
1349 return itb_res
->tlb();
1354 InOrderCPU::getDTBPtr()
1356 CacheUnit
*dtb_res
=
1357 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1358 return dtb_res
->tlb();
1363 InOrderCPU::read(DynInstPtr inst
, Addr addr
, T
&data
, unsigned flags
)
1365 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1366 // you want to run w/out caches?
1367 CacheUnit
*cache_res
=
1368 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1370 return cache_res
->read(inst
, addr
, data
, flags
);
1373 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1377 InOrderCPU::read(DynInstPtr inst
, Addr addr
, Twin32_t
&data
, unsigned flags
);
1381 InOrderCPU::read(DynInstPtr inst
, Addr addr
, Twin64_t
&data
, unsigned flags
);
1385 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint64_t &data
, unsigned flags
);
1389 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint32_t &data
, unsigned flags
);
1393 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint16_t &data
, unsigned flags
);
1397 InOrderCPU::read(DynInstPtr inst
, Addr addr
, uint8_t &data
, unsigned flags
);
1399 #endif //DOXYGEN_SHOULD_SKIP_THIS
1403 InOrderCPU::read(DynInstPtr inst
, Addr addr
, double &data
, unsigned flags
)
1405 return read(inst
, addr
, *(uint64_t*)&data
, flags
);
1410 InOrderCPU::read(DynInstPtr inst
, Addr addr
, float &data
, unsigned flags
)
1412 return read(inst
, addr
, *(uint32_t*)&data
, flags
);
1418 InOrderCPU::read(DynInstPtr inst
, Addr addr
, int32_t &data
, unsigned flags
)
1420 return read(inst
, addr
, (uint32_t&)data
, flags
);
1425 InOrderCPU::write(DynInstPtr inst
, T data
, Addr addr
, unsigned flags
,
1426 uint64_t *write_res
)
1428 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1429 // you want to run w/out caches?
1430 CacheUnit
*cache_res
=
1431 dynamic_cast<CacheUnit
*>(resPool
->getResource(dataPortIdx
));
1432 return cache_res
->write(inst
, data
, addr
, flags
, write_res
);
1435 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1439 InOrderCPU::write(DynInstPtr inst
, Twin32_t data
, Addr addr
,
1440 unsigned flags
, uint64_t *res
);
1444 InOrderCPU::write(DynInstPtr inst
, Twin64_t data
, Addr addr
,
1445 unsigned flags
, uint64_t *res
);
1449 InOrderCPU::write(DynInstPtr inst
, uint64_t data
, Addr addr
,
1450 unsigned flags
, uint64_t *res
);
1454 InOrderCPU::write(DynInstPtr inst
, uint32_t data
, Addr addr
,
1455 unsigned flags
, uint64_t *res
);
1459 InOrderCPU::write(DynInstPtr inst
, uint16_t data
, Addr addr
,
1460 unsigned flags
, uint64_t *res
);
1464 InOrderCPU::write(DynInstPtr inst
, uint8_t data
, Addr addr
,
1465 unsigned flags
, uint64_t *res
);
1467 #endif //DOXYGEN_SHOULD_SKIP_THIS
1471 InOrderCPU::write(DynInstPtr inst
, double data
, Addr addr
, unsigned flags
,
1474 return write(inst
, *(uint64_t*)&data
, addr
, flags
, res
);
1479 InOrderCPU::write(DynInstPtr inst
, float data
, Addr addr
, unsigned flags
,
1482 return write(inst
, *(uint32_t*)&data
, addr
, flags
, res
);
1488 InOrderCPU::write(DynInstPtr inst
, int32_t data
, Addr addr
, unsigned flags
,
1491 return write(inst
, (uint32_t)data
, addr
, flags
, res
);