e0e4d9a158c486b9463371e6adf22b8f61b08493
[gem5.git] / src / cpu / inorder / cpu.cc
1 /*
2 * Copyright (c) 2007 MIPS Technologies, Inc.
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Korey Sewell
29 *
30 */
31
32 #include <algorithm>
33
34 #include "arch/utility.hh"
35 #include "base/bigint.hh"
36 #include "config/full_system.hh"
37 #include "config/the_isa.hh"
38 #include "cpu/inorder/resources/resource_list.hh"
39 #include "cpu/inorder/cpu.hh"
40 #include "cpu/inorder/first_stage.hh"
41 #include "cpu/inorder/inorder_dyn_inst.hh"
42 #include "cpu/inorder/pipeline_traits.hh"
43 #include "cpu/inorder/resource_pool.hh"
44 #include "cpu/inorder/thread_context.hh"
45 #include "cpu/inorder/thread_state.hh"
46 #include "cpu/activity.hh"
47 #include "cpu/base.hh"
48 #include "cpu/exetrace.hh"
49 #include "cpu/simple_thread.hh"
50 #include "cpu/thread_context.hh"
51 #include "debug/Activity.hh"
52 #include "debug/InOrderCPU.hh"
53 #include "debug/RefCount.hh"
54 #include "debug/SkedCache.hh"
55 #include "mem/translating_port.hh"
56 #include "params/InOrderCPU.hh"
57 #include "sim/process.hh"
58 #include "sim/stat_control.hh"
59
60 #if FULL_SYSTEM
61 #include "cpu/quiesce_event.hh"
62 #include "sim/system.hh"
63 #endif
64
65 #if THE_ISA == ALPHA_ISA
66 #include "arch/alpha/osfpal.hh"
67 #endif
68
69 using namespace std;
70 using namespace TheISA;
71 using namespace ThePipeline;
72
73 InOrderCPU::TickEvent::TickEvent(InOrderCPU *c)
74 : Event(CPU_Tick_Pri), cpu(c)
75 { }
76
77
78 void
79 InOrderCPU::TickEvent::process()
80 {
81 cpu->tick();
82 }
83
84
85 const char *
86 InOrderCPU::TickEvent::description()
87 {
88 return "InOrderCPU tick event";
89 }
90
91 InOrderCPU::CPUEvent::CPUEvent(InOrderCPU *_cpu, CPUEventType e_type,
92 Fault fault, ThreadID _tid, DynInstPtr inst,
93 unsigned event_pri_offset)
94 : Event(Event::Priority((unsigned int)CPU_Tick_Pri + event_pri_offset)),
95 cpu(_cpu)
96 {
97 setEvent(e_type, fault, _tid, inst);
98 }
99
100
101 std::string InOrderCPU::eventNames[NumCPUEvents] =
102 {
103 "ActivateThread",
104 "ActivateNextReadyThread",
105 "DeactivateThread",
106 "HaltThread",
107 "SuspendThread",
108 "Trap",
109 "InstGraduated",
110 "SquashFromMemStall",
111 "UpdatePCs"
112 };
113
114 void
115 InOrderCPU::CPUEvent::process()
116 {
117 switch (cpuEventType)
118 {
119 case ActivateThread:
120 cpu->activateThread(tid);
121 break;
122
123 case ActivateNextReadyThread:
124 cpu->activateNextReadyThread();
125 break;
126
127 case DeactivateThread:
128 cpu->deactivateThread(tid);
129 break;
130
131 case HaltThread:
132 cpu->haltThread(tid);
133 break;
134
135 case SuspendThread:
136 cpu->suspendThread(tid);
137 break;
138
139 case SquashFromMemStall:
140 cpu->squashDueToMemStall(inst->squashingStage, inst->seqNum, tid);
141 break;
142
143 case Trap:
144 DPRINTF(InOrderCPU, "Trapping CPU\n");
145 cpu->trap(fault, tid, inst);
146 cpu->resPool->trap(fault, tid, inst);
147 break;
148
149 default:
150 fatal("Unrecognized Event Type %s", eventNames[cpuEventType]);
151 }
152
153 cpu->cpuEventRemoveList.push(this);
154 }
155
156
157
158 const char *
159 InOrderCPU::CPUEvent::description()
160 {
161 return "InOrderCPU event";
162 }
163
164 void
165 InOrderCPU::CPUEvent::scheduleEvent(int delay)
166 {
167 assert(!scheduled() || squashed());
168 cpu->reschedule(this, cpu->nextCycle(curTick() + cpu->ticks(delay)), true);
169 }
170
171 void
172 InOrderCPU::CPUEvent::unscheduleEvent()
173 {
174 if (scheduled())
175 squash();
176 }
177
178 InOrderCPU::InOrderCPU(Params *params)
179 : BaseCPU(params),
180 cpu_id(params->cpu_id),
181 coreType("default"),
182 _status(Idle),
183 tickEvent(this),
184 stageWidth(params->stageWidth),
185 timeBuffer(2 , 2),
186 removeInstsThisCycle(false),
187 activityRec(params->name, NumStages, 10, params->activity),
188 #if FULL_SYSTEM
189 system(params->system),
190 physmem(system->physmem),
191 #endif // FULL_SYSTEM
192 #ifdef DEBUG
193 cpuEventNum(0),
194 resReqCount(0),
195 #endif // DEBUG
196 switchCount(0),
197 deferRegistration(false/*params->deferRegistration*/),
198 stageTracing(params->stageTracing),
199 instsPerSwitch(0)
200 {
201 ThreadID active_threads;
202 cpu_params = params;
203
204 resPool = new ResourcePool(this, params);
205
206 // Resize for Multithreading CPUs
207 thread.resize(numThreads);
208
209 #if FULL_SYSTEM
210 active_threads = 1;
211 #else
212 active_threads = params->workload.size();
213
214 if (active_threads > MaxThreads) {
215 panic("Workload Size too large. Increase the 'MaxThreads'"
216 "in your InOrder implementation or "
217 "edit your workload size.");
218 }
219
220
221 if (active_threads > 1) {
222 threadModel = (InOrderCPU::ThreadModel) params->threadModel;
223
224 if (threadModel == SMT) {
225 DPRINTF(InOrderCPU, "Setting Thread Model to SMT.\n");
226 } else if (threadModel == SwitchOnCacheMiss) {
227 DPRINTF(InOrderCPU, "Setting Thread Model to "
228 "Switch On Cache Miss\n");
229 }
230
231 } else {
232 threadModel = Single;
233 }
234
235
236
237 #endif
238
239 // Bind the fetch & data ports from the resource pool.
240 fetchPortIdx = resPool->getPortIdx(params->fetchMemPort);
241 if (fetchPortIdx == 0) {
242 fatal("Unable to find port to fetch instructions from.\n");
243 }
244
245 dataPortIdx = resPool->getPortIdx(params->dataMemPort);
246 if (dataPortIdx == 0) {
247 fatal("Unable to find port for data.\n");
248 }
249
250 for (ThreadID tid = 0; tid < numThreads; ++tid) {
251 #if FULL_SYSTEM
252 // SMT is not supported in FS mode yet.
253 assert(numThreads == 1);
254 thread[tid] = new Thread(this, 0);
255 #else
256 if (tid < (ThreadID)params->workload.size()) {
257 DPRINTF(InOrderCPU, "Workload[%i] process is %#x\n",
258 tid, params->workload[tid]->prog_fname);
259 thread[tid] =
260 new Thread(this, tid, params->workload[tid]);
261 } else {
262 //Allocate Empty thread so M5 can use later
263 //when scheduling threads to CPU
264 Process* dummy_proc = params->workload[0];
265 thread[tid] = new Thread(this, tid, dummy_proc);
266 }
267
268 // Eventually set this with parameters...
269 asid[tid] = tid;
270 #endif
271
272 // Setup the TC that will serve as the interface to the threads/CPU.
273 InOrderThreadContext *tc = new InOrderThreadContext;
274 tc->cpu = this;
275 tc->thread = thread[tid];
276
277 // Give the thread the TC.
278 thread[tid]->tc = tc;
279 thread[tid]->setFuncExeInst(0);
280 globalSeqNum[tid] = 1;
281
282 // Add the TC to the CPU's list of TC's.
283 this->threadContexts.push_back(tc);
284 }
285
286 // Initialize TimeBuffer Stage Queues
287 for (int stNum=0; stNum < NumStages - 1; stNum++) {
288 stageQueue[stNum] = new StageQueue(NumStages, NumStages);
289 stageQueue[stNum]->id(stNum);
290 }
291
292
293 // Set Up Pipeline Stages
294 for (int stNum=0; stNum < NumStages; stNum++) {
295 if (stNum == 0)
296 pipelineStage[stNum] = new FirstStage(params, stNum);
297 else
298 pipelineStage[stNum] = new PipelineStage(params, stNum);
299
300 pipelineStage[stNum]->setCPU(this);
301 pipelineStage[stNum]->setActiveThreads(&activeThreads);
302 pipelineStage[stNum]->setTimeBuffer(&timeBuffer);
303
304 // Take Care of 1st/Nth stages
305 if (stNum > 0)
306 pipelineStage[stNum]->setPrevStageQueue(stageQueue[stNum - 1]);
307 if (stNum < NumStages - 1)
308 pipelineStage[stNum]->setNextStageQueue(stageQueue[stNum]);
309 }
310
311 // Initialize thread specific variables
312 for (ThreadID tid = 0; tid < numThreads; tid++) {
313 archRegDepMap[tid].setCPU(this);
314
315 nonSpecInstActive[tid] = false;
316 nonSpecSeqNum[tid] = 0;
317
318 squashSeqNum[tid] = MaxAddr;
319 lastSquashCycle[tid] = 0;
320
321 memset(intRegs[tid], 0, sizeof(intRegs[tid]));
322 memset(floatRegs.i[tid], 0, sizeof(floatRegs.i[tid]));
323 isa[tid].clear();
324
325 // Define dummy instructions and resource requests to be used.
326 dummyInst[tid] = new InOrderDynInst(this,
327 thread[tid],
328 0,
329 tid,
330 asid[tid]);
331
332 dummyReq[tid] = new ResourceRequest(resPool->getResource(0));
333 }
334
335 dummyReqInst = new InOrderDynInst(this, NULL, 0, 0, 0);
336 dummyReqInst->setSquashed();
337 dummyReqInst->resetInstCount();
338
339 dummyBufferInst = new InOrderDynInst(this, NULL, 0, 0, 0);
340 dummyBufferInst->setSquashed();
341 dummyBufferInst->resetInstCount();
342
343 endOfSkedIt = skedCache.end();
344 frontEndSked = createFrontEndSked();
345
346 lastRunningCycle = curTick();
347
348 // Reset CPU to reset state.
349 #if FULL_SYSTEM
350 Fault resetFault = new ResetFault();
351 resetFault->invoke(tcBase());
352 #endif
353
354
355 // Schedule First Tick Event, CPU will reschedule itself from here on out.
356 scheduleTickEvent(0);
357 }
358
359 InOrderCPU::~InOrderCPU()
360 {
361 delete resPool;
362
363 SkedCacheIt sked_it = skedCache.begin();
364 SkedCacheIt sked_end = skedCache.end();
365
366 while (sked_it != sked_end) {
367 delete (*sked_it).second;
368 sked_it++;
369 }
370 skedCache.clear();
371 }
372
373 m5::hash_map<InOrderCPU::SkedID, ThePipeline::RSkedPtr> InOrderCPU::skedCache;
374
375 RSkedPtr
376 InOrderCPU::createFrontEndSked()
377 {
378 RSkedPtr res_sked = new ResourceSked();
379 int stage_num = 0;
380 StageScheduler F(res_sked, stage_num++);
381 StageScheduler D(res_sked, stage_num++);
382
383 // FETCH
384 F.needs(FetchSeq, FetchSeqUnit::AssignNextPC);
385 F.needs(ICache, FetchUnit::InitiateFetch);
386
387 // DECODE
388 D.needs(ICache, FetchUnit::CompleteFetch);
389 D.needs(Decode, DecodeUnit::DecodeInst);
390 D.needs(BPred, BranchPredictor::PredictBranch);
391 D.needs(FetchSeq, FetchSeqUnit::UpdateTargetPC);
392
393
394 DPRINTF(SkedCache, "Resource Sked created for instruction \"front_end\"\n");
395
396 return res_sked;
397 }
398
399 RSkedPtr
400 InOrderCPU::createBackEndSked(DynInstPtr inst)
401 {
402 RSkedPtr res_sked = lookupSked(inst);
403 if (res_sked != NULL) {
404 DPRINTF(SkedCache, "Found %s in sked cache.\n",
405 inst->instName());
406 return res_sked;
407 } else {
408 res_sked = new ResourceSked();
409 }
410
411 int stage_num = ThePipeline::BackEndStartStage;
412 StageScheduler X(res_sked, stage_num++);
413 StageScheduler M(res_sked, stage_num++);
414 StageScheduler W(res_sked, stage_num++);
415
416 if (!inst->staticInst) {
417 warn_once("Static Instruction Object Not Set. Can't Create"
418 " Back End Schedule");
419 return NULL;
420 }
421
422 // EXECUTE
423 X.needs(RegManager, UseDefUnit::MarkDestRegs);
424 for (int idx=0; idx < inst->numSrcRegs(); idx++) {
425 if (!idx || !inst->isStore()) {
426 X.needs(RegManager, UseDefUnit::ReadSrcReg, idx);
427 }
428 }
429
430 //@todo: schedule non-spec insts to operate on this cycle
431 // as long as all previous insts are done
432 if ( inst->isNonSpeculative() ) {
433 // skip execution of non speculative insts until later
434 } else if ( inst->isMemRef() ) {
435 if ( inst->isLoad() ) {
436 X.needs(AGEN, AGENUnit::GenerateAddr);
437 }
438 } else if (inst->opClass() == IntMultOp || inst->opClass() == IntDivOp) {
439 X.needs(MDU, MultDivUnit::StartMultDiv);
440 } else {
441 X.needs(ExecUnit, ExecutionUnit::ExecuteInst);
442 }
443
444 // MEMORY
445 if (!inst->isNonSpeculative()) {
446 if (inst->opClass() == IntMultOp || inst->opClass() == IntDivOp) {
447 M.needs(MDU, MultDivUnit::EndMultDiv);
448 }
449
450 if ( inst->isLoad() ) {
451 M.needs(DCache, CacheUnit::InitiateReadData);
452 if (inst->splitInst)
453 M.needs(DCache, CacheUnit::InitSecondSplitRead);
454 } else if ( inst->isStore() ) {
455 for (int i = 1; i < inst->numSrcRegs(); i++ ) {
456 M.needs(RegManager, UseDefUnit::ReadSrcReg, i);
457 }
458 M.needs(AGEN, AGENUnit::GenerateAddr);
459 M.needs(DCache, CacheUnit::InitiateWriteData);
460 if (inst->splitInst)
461 M.needs(DCache, CacheUnit::InitSecondSplitWrite);
462 }
463 }
464
465 // WRITEBACK
466 if (!inst->isNonSpeculative()) {
467 if ( inst->isLoad() ) {
468 W.needs(DCache, CacheUnit::CompleteReadData);
469 if (inst->splitInst)
470 W.needs(DCache, CacheUnit::CompleteSecondSplitRead);
471 } else if ( inst->isStore() ) {
472 W.needs(DCache, CacheUnit::CompleteWriteData);
473 if (inst->splitInst)
474 W.needs(DCache, CacheUnit::CompleteSecondSplitWrite);
475 }
476 } else {
477 // Finally, Execute Speculative Data
478 if (inst->isMemRef()) {
479 if (inst->isLoad()) {
480 W.needs(AGEN, AGENUnit::GenerateAddr);
481 W.needs(DCache, CacheUnit::InitiateReadData);
482 if (inst->splitInst)
483 W.needs(DCache, CacheUnit::InitSecondSplitRead);
484 W.needs(DCache, CacheUnit::CompleteReadData);
485 if (inst->splitInst)
486 W.needs(DCache, CacheUnit::CompleteSecondSplitRead);
487 } else if (inst->isStore()) {
488 if ( inst->numSrcRegs() >= 2 ) {
489 W.needs(RegManager, UseDefUnit::ReadSrcReg, 1);
490 }
491 W.needs(AGEN, AGENUnit::GenerateAddr);
492 W.needs(DCache, CacheUnit::InitiateWriteData);
493 if (inst->splitInst)
494 W.needs(DCache, CacheUnit::InitSecondSplitWrite);
495 W.needs(DCache, CacheUnit::CompleteWriteData);
496 if (inst->splitInst)
497 W.needs(DCache, CacheUnit::CompleteSecondSplitWrite);
498 }
499 } else {
500 W.needs(ExecUnit, ExecutionUnit::ExecuteInst);
501 }
502 }
503
504 W.needs(Grad, GraduationUnit::GraduateInst);
505
506 for (int idx=0; idx < inst->numDestRegs(); idx++) {
507 W.needs(RegManager, UseDefUnit::WriteDestReg, idx);
508 }
509
510 // Insert Back Schedule into our cache of
511 // resource schedules
512 addToSkedCache(inst, res_sked);
513
514 DPRINTF(SkedCache, "Back End Sked Created for instruction: %s (%08p)\n",
515 inst->instName(), inst->getMachInst());
516 res_sked->print();
517
518 return res_sked;
519 }
520
521 void
522 InOrderCPU::regStats()
523 {
524 /* Register the Resource Pool's stats here.*/
525 resPool->regStats();
526
527 /* Register for each Pipeline Stage */
528 for (int stage_num=0; stage_num < ThePipeline::NumStages; stage_num++) {
529 pipelineStage[stage_num]->regStats();
530 }
531
532 /* Register any of the InOrderCPU's stats here.*/
533 instsPerCtxtSwitch
534 .name(name() + ".instsPerContextSwitch")
535 .desc("Instructions Committed Per Context Switch")
536 .prereq(instsPerCtxtSwitch);
537
538 numCtxtSwitches
539 .name(name() + ".contextSwitches")
540 .desc("Number of context switches");
541
542 comLoads
543 .name(name() + ".comLoads")
544 .desc("Number of Load instructions committed");
545
546 comStores
547 .name(name() + ".comStores")
548 .desc("Number of Store instructions committed");
549
550 comBranches
551 .name(name() + ".comBranches")
552 .desc("Number of Branches instructions committed");
553
554 comNops
555 .name(name() + ".comNops")
556 .desc("Number of Nop instructions committed");
557
558 comNonSpec
559 .name(name() + ".comNonSpec")
560 .desc("Number of Non-Speculative instructions committed");
561
562 comInts
563 .name(name() + ".comInts")
564 .desc("Number of Integer instructions committed");
565
566 comFloats
567 .name(name() + ".comFloats")
568 .desc("Number of Floating Point instructions committed");
569
570 timesIdled
571 .name(name() + ".timesIdled")
572 .desc("Number of times that the entire CPU went into an idle state and"
573 " unscheduled itself")
574 .prereq(timesIdled);
575
576 idleCycles
577 .name(name() + ".idleCycles")
578 .desc("Number of cycles cpu's stages were not processed");
579
580 runCycles
581 .name(name() + ".runCycles")
582 .desc("Number of cycles cpu stages are processed.");
583
584 activity
585 .name(name() + ".activity")
586 .desc("Percentage of cycles cpu is active")
587 .precision(6);
588 activity = (runCycles / numCycles) * 100;
589
590 threadCycles
591 .init(numThreads)
592 .name(name() + ".threadCycles")
593 .desc("Total Number of Cycles A Thread Was Active in CPU (Per-Thread)");
594
595 smtCycles
596 .name(name() + ".smtCycles")
597 .desc("Total number of cycles that the CPU was in SMT-mode");
598
599 committedInsts
600 .init(numThreads)
601 .name(name() + ".committedInsts")
602 .desc("Number of Instructions Simulated (Per-Thread)");
603
604 smtCommittedInsts
605 .init(numThreads)
606 .name(name() + ".smtCommittedInsts")
607 .desc("Number of SMT Instructions Simulated (Per-Thread)");
608
609 totalCommittedInsts
610 .name(name() + ".committedInsts_total")
611 .desc("Number of Instructions Simulated (Total)");
612
613 cpi
614 .name(name() + ".cpi")
615 .desc("CPI: Cycles Per Instruction (Per-Thread)")
616 .precision(6);
617 cpi = numCycles / committedInsts;
618
619 smtCpi
620 .name(name() + ".smt_cpi")
621 .desc("CPI: Total SMT-CPI")
622 .precision(6);
623 smtCpi = smtCycles / smtCommittedInsts;
624
625 totalCpi
626 .name(name() + ".cpi_total")
627 .desc("CPI: Total CPI of All Threads")
628 .precision(6);
629 totalCpi = numCycles / totalCommittedInsts;
630
631 ipc
632 .name(name() + ".ipc")
633 .desc("IPC: Instructions Per Cycle (Per-Thread)")
634 .precision(6);
635 ipc = committedInsts / numCycles;
636
637 smtIpc
638 .name(name() + ".smt_ipc")
639 .desc("IPC: Total SMT-IPC")
640 .precision(6);
641 smtIpc = smtCommittedInsts / smtCycles;
642
643 totalIpc
644 .name(name() + ".ipc_total")
645 .desc("IPC: Total IPC of All Threads")
646 .precision(6);
647 totalIpc = totalCommittedInsts / numCycles;
648
649 BaseCPU::regStats();
650 }
651
652
653 void
654 InOrderCPU::tick()
655 {
656 DPRINTF(InOrderCPU, "\n\nInOrderCPU: Ticking main, InOrderCPU.\n");
657
658 ++numCycles;
659
660 bool pipes_idle = true;
661
662 //Tick each of the stages
663 for (int stNum=NumStages - 1; stNum >= 0 ; stNum--) {
664 pipelineStage[stNum]->tick();
665
666 pipes_idle = pipes_idle && pipelineStage[stNum]->idle;
667 }
668
669 if (pipes_idle)
670 idleCycles++;
671 else
672 runCycles++;
673
674 // Now advance the time buffers one tick
675 timeBuffer.advance();
676 for (int sqNum=0; sqNum < NumStages - 1; sqNum++) {
677 stageQueue[sqNum]->advance();
678 }
679 activityRec.advance();
680
681 // Any squashed events, or insts then remove them now
682 cleanUpRemovedEvents();
683 cleanUpRemovedInsts();
684
685 // Re-schedule CPU for this cycle
686 if (!tickEvent.scheduled()) {
687 if (_status == SwitchedOut) {
688 // increment stat
689 lastRunningCycle = curTick();
690 } else if (!activityRec.active()) {
691 DPRINTF(InOrderCPU, "sleeping CPU.\n");
692 lastRunningCycle = curTick();
693 timesIdled++;
694 } else {
695 //Tick next_tick = curTick() + cycles(1);
696 //tickEvent.schedule(next_tick);
697 schedule(&tickEvent, nextCycle(curTick() + 1));
698 DPRINTF(InOrderCPU, "Scheduled CPU for next tick @ %i.\n",
699 nextCycle(curTick() + 1));
700 }
701 }
702
703 tickThreadStats();
704 updateThreadPriority();
705 }
706
707
708 void
709 InOrderCPU::init()
710 {
711 if (!deferRegistration) {
712 registerThreadContexts();
713 }
714
715 // Set inSyscall so that the CPU doesn't squash when initially
716 // setting up registers.
717 for (ThreadID tid = 0; tid < numThreads; ++tid)
718 thread[tid]->inSyscall = true;
719
720 #if FULL_SYSTEM
721 for (ThreadID tid = 0; tid < numThreads; tid++) {
722 ThreadContext *src_tc = threadContexts[tid];
723 TheISA::initCPU(src_tc, src_tc->contextId());
724 }
725 #endif
726
727 // Clear inSyscall.
728 for (ThreadID tid = 0; tid < numThreads; ++tid)
729 thread[tid]->inSyscall = false;
730
731 // Call Initializiation Routine for Resource Pool
732 resPool->init();
733 }
734
735 Port*
736 InOrderCPU::getPort(const std::string &if_name, int idx)
737 {
738 return resPool->getPort(if_name, idx);
739 }
740
741 #if FULL_SYSTEM
742 Fault
743 InOrderCPU::hwrei(ThreadID tid)
744 {
745 panic("hwrei: Unimplemented");
746
747 return NoFault;
748 }
749
750
751 bool
752 InOrderCPU::simPalCheck(int palFunc, ThreadID tid)
753 {
754 panic("simPalCheck: Unimplemented");
755
756 return true;
757 }
758
759
760 Fault
761 InOrderCPU::getInterrupts()
762 {
763 // Check if there are any outstanding interrupts
764 return interrupts->getInterrupt(threadContexts[0]);
765 }
766
767
768 void
769 InOrderCPU::processInterrupts(Fault interrupt)
770 {
771 // Check for interrupts here. For now can copy the code that
772 // exists within isa_fullsys_traits.hh. Also assume that thread 0
773 // is the one that handles the interrupts.
774 // @todo: Possibly consolidate the interrupt checking code.
775 // @todo: Allow other threads to handle interrupts.
776
777 assert(interrupt != NoFault);
778 interrupts->updateIntrInfo(threadContexts[0]);
779
780 DPRINTF(InOrderCPU, "Interrupt %s being handled\n", interrupt->name());
781
782 // Note: Context ID ok here? Impl. of FS mode needs to revisit this
783 trap(interrupt, threadContexts[0]->contextId(), dummyBufferInst);
784 }
785
786
787 void
788 InOrderCPU::updateMemPorts()
789 {
790 // Update all ThreadContext's memory ports (Functional/Virtual
791 // Ports)
792 ThreadID size = thread.size();
793 for (ThreadID i = 0; i < size; ++i)
794 thread[i]->connectMemPorts(thread[i]->getTC());
795 }
796 #endif
797
798 void
799 InOrderCPU::trapContext(Fault fault, ThreadID tid, DynInstPtr inst, int delay)
800 {
801 scheduleCpuEvent(Trap, fault, tid, inst, delay);
802 }
803
804 void
805 InOrderCPU::trap(Fault fault, ThreadID tid, DynInstPtr inst)
806 {
807 fault->invoke(tcBase(tid), inst->staticInst);
808 }
809
810 void
811 InOrderCPU::squashFromMemStall(DynInstPtr inst, ThreadID tid, int delay)
812 {
813 scheduleCpuEvent(SquashFromMemStall, NoFault, tid, inst, delay);
814 }
815
816
817 void
818 InOrderCPU::squashDueToMemStall(int stage_num, InstSeqNum seq_num,
819 ThreadID tid)
820 {
821 DPRINTF(InOrderCPU, "Squashing Pipeline Stages Due to Memory Stall...\n");
822
823 // Squash all instructions in each stage including
824 // instruction that caused the squash (seq_num - 1)
825 // NOTE: The stage bandwidth needs to be cleared so thats why
826 // the stalling instruction is squashed as well. The stalled
827 // instruction is previously placed in another intermediate buffer
828 // while it's stall is being handled.
829 InstSeqNum squash_seq_num = seq_num - 1;
830
831 for (int stNum=stage_num; stNum >= 0 ; stNum--) {
832 pipelineStage[stNum]->squashDueToMemStall(squash_seq_num, tid);
833 }
834 }
835
836 void
837 InOrderCPU::scheduleCpuEvent(CPUEventType c_event, Fault fault,
838 ThreadID tid, DynInstPtr inst,
839 unsigned delay, unsigned event_pri_offset)
840 {
841 CPUEvent *cpu_event = new CPUEvent(this, c_event, fault, tid, inst,
842 event_pri_offset);
843
844 Tick sked_tick = nextCycle(curTick() + ticks(delay));
845 if (delay >= 0) {
846 DPRINTF(InOrderCPU, "Scheduling CPU Event (%s) for cycle %i, [tid:%i].\n",
847 eventNames[c_event], curTick() + delay, tid);
848 schedule(cpu_event, sked_tick);
849 } else {
850 cpu_event->process();
851 cpuEventRemoveList.push(cpu_event);
852 }
853
854 // Broadcast event to the Resource Pool
855 // Need to reset tid just in case this is a dummy instruction
856 inst->setTid(tid);
857 resPool->scheduleEvent(c_event, inst, 0, 0, tid);
858 }
859
860 bool
861 InOrderCPU::isThreadActive(ThreadID tid)
862 {
863 list<ThreadID>::iterator isActive =
864 std::find(activeThreads.begin(), activeThreads.end(), tid);
865
866 return (isActive != activeThreads.end());
867 }
868
869 bool
870 InOrderCPU::isThreadReady(ThreadID tid)
871 {
872 list<ThreadID>::iterator isReady =
873 std::find(readyThreads.begin(), readyThreads.end(), tid);
874
875 return (isReady != readyThreads.end());
876 }
877
878 bool
879 InOrderCPU::isThreadSuspended(ThreadID tid)
880 {
881 list<ThreadID>::iterator isSuspended =
882 std::find(suspendedThreads.begin(), suspendedThreads.end(), tid);
883
884 return (isSuspended != suspendedThreads.end());
885 }
886
887 void
888 InOrderCPU::activateNextReadyThread()
889 {
890 if (readyThreads.size() >= 1) {
891 ThreadID ready_tid = readyThreads.front();
892
893 // Activate in Pipeline
894 activateThread(ready_tid);
895
896 // Activate in Resource Pool
897 resPool->activateAll(ready_tid);
898
899 list<ThreadID>::iterator ready_it =
900 std::find(readyThreads.begin(), readyThreads.end(), ready_tid);
901 readyThreads.erase(ready_it);
902 } else {
903 DPRINTF(InOrderCPU,
904 "Attempting to activate new thread, but No Ready Threads to"
905 "activate.\n");
906 DPRINTF(InOrderCPU,
907 "Unable to switch to next active thread.\n");
908 }
909 }
910
911 void
912 InOrderCPU::activateThread(ThreadID tid)
913 {
914 if (isThreadSuspended(tid)) {
915 DPRINTF(InOrderCPU,
916 "Removing [tid:%i] from suspended threads list.\n", tid);
917
918 list<ThreadID>::iterator susp_it =
919 std::find(suspendedThreads.begin(), suspendedThreads.end(),
920 tid);
921 suspendedThreads.erase(susp_it);
922 }
923
924 if (threadModel == SwitchOnCacheMiss &&
925 numActiveThreads() == 1) {
926 DPRINTF(InOrderCPU,
927 "Ignoring activation of [tid:%i], since [tid:%i] is "
928 "already running.\n", tid, activeThreadId());
929
930 DPRINTF(InOrderCPU,"Placing [tid:%i] on ready threads list\n",
931 tid);
932
933 readyThreads.push_back(tid);
934
935 } else if (!isThreadActive(tid)) {
936 DPRINTF(InOrderCPU,
937 "Adding [tid:%i] to active threads list.\n", tid);
938 activeThreads.push_back(tid);
939
940 activateThreadInPipeline(tid);
941
942 thread[tid]->lastActivate = curTick();
943
944 tcBase(tid)->setStatus(ThreadContext::Active);
945
946 wakeCPU();
947
948 numCtxtSwitches++;
949 }
950 }
951
952 void
953 InOrderCPU::activateThreadInPipeline(ThreadID tid)
954 {
955 for (int stNum=0; stNum < NumStages; stNum++) {
956 pipelineStage[stNum]->activateThread(tid);
957 }
958 }
959
960 void
961 InOrderCPU::deactivateContext(ThreadID tid, int delay)
962 {
963 DPRINTF(InOrderCPU,"[tid:%i]: Deactivating ...\n", tid);
964
965 scheduleCpuEvent(DeactivateThread, NoFault, tid, dummyInst[tid], delay);
966
967 // Be sure to signal that there's some activity so the CPU doesn't
968 // deschedule itself.
969 activityRec.activity();
970
971 _status = Running;
972 }
973
974 void
975 InOrderCPU::deactivateThread(ThreadID tid)
976 {
977 DPRINTF(InOrderCPU, "[tid:%i]: Calling deactivate thread.\n", tid);
978
979 if (isThreadActive(tid)) {
980 DPRINTF(InOrderCPU,"[tid:%i]: Removing from active threads list\n",
981 tid);
982 list<ThreadID>::iterator thread_it =
983 std::find(activeThreads.begin(), activeThreads.end(), tid);
984
985 removePipelineStalls(*thread_it);
986
987 activeThreads.erase(thread_it);
988
989 // Ideally, this should be triggered from the
990 // suspendContext/Thread functions
991 tcBase(tid)->setStatus(ThreadContext::Suspended);
992 }
993
994 assert(!isThreadActive(tid));
995 }
996
997 void
998 InOrderCPU::removePipelineStalls(ThreadID tid)
999 {
1000 DPRINTF(InOrderCPU,"[tid:%i]: Removing all pipeline stalls\n",
1001 tid);
1002
1003 for (int stNum = 0; stNum < NumStages ; stNum++) {
1004 pipelineStage[stNum]->removeStalls(tid);
1005 }
1006
1007 }
1008
1009 void
1010 InOrderCPU::updateThreadPriority()
1011 {
1012 if (activeThreads.size() > 1)
1013 {
1014 //DEFAULT TO ROUND ROBIN SCHEME
1015 //e.g. Move highest priority to end of thread list
1016 list<ThreadID>::iterator list_begin = activeThreads.begin();
1017 list<ThreadID>::iterator list_end = activeThreads.end();
1018
1019 unsigned high_thread = *list_begin;
1020
1021 activeThreads.erase(list_begin);
1022
1023 activeThreads.push_back(high_thread);
1024 }
1025 }
1026
1027 inline void
1028 InOrderCPU::tickThreadStats()
1029 {
1030 /** Keep track of cycles that each thread is active */
1031 list<ThreadID>::iterator thread_it = activeThreads.begin();
1032 while (thread_it != activeThreads.end()) {
1033 threadCycles[*thread_it]++;
1034 thread_it++;
1035 }
1036
1037 // Keep track of cycles where SMT is active
1038 if (activeThreads.size() > 1) {
1039 smtCycles++;
1040 }
1041 }
1042
1043 void
1044 InOrderCPU::activateContext(ThreadID tid, int delay)
1045 {
1046 DPRINTF(InOrderCPU,"[tid:%i]: Activating ...\n", tid);
1047
1048
1049 scheduleCpuEvent(ActivateThread, NoFault, tid, dummyInst[tid], delay);
1050
1051 // Be sure to signal that there's some activity so the CPU doesn't
1052 // deschedule itself.
1053 activityRec.activity();
1054
1055 _status = Running;
1056 }
1057
1058 void
1059 InOrderCPU::activateNextReadyContext(int delay)
1060 {
1061 DPRINTF(InOrderCPU,"Activating next ready thread\n");
1062
1063 // NOTE: Add 5 to the event priority so that we always activate
1064 // threads after we've finished deactivating, squashing,etc.
1065 // other threads
1066 scheduleCpuEvent(ActivateNextReadyThread, NoFault, 0/*tid*/, dummyInst[0],
1067 delay, 5);
1068
1069 // Be sure to signal that there's some activity so the CPU doesn't
1070 // deschedule itself.
1071 activityRec.activity();
1072
1073 _status = Running;
1074 }
1075
1076 void
1077 InOrderCPU::haltContext(ThreadID tid, int delay)
1078 {
1079 DPRINTF(InOrderCPU, "[tid:%i]: Calling Halt Context...\n", tid);
1080
1081 scheduleCpuEvent(HaltThread, NoFault, tid, dummyInst[tid], delay);
1082
1083 activityRec.activity();
1084 }
1085
1086 void
1087 InOrderCPU::haltThread(ThreadID tid)
1088 {
1089 DPRINTF(InOrderCPU, "[tid:%i]: Placing on Halted Threads List...\n", tid);
1090 deactivateThread(tid);
1091 squashThreadInPipeline(tid);
1092 haltedThreads.push_back(tid);
1093
1094 tcBase(tid)->setStatus(ThreadContext::Halted);
1095
1096 if (threadModel == SwitchOnCacheMiss) {
1097 activateNextReadyContext();
1098 }
1099 }
1100
1101 void
1102 InOrderCPU::suspendContext(ThreadID tid, int delay)
1103 {
1104 scheduleCpuEvent(SuspendThread, NoFault, tid, dummyInst[tid], delay);
1105 }
1106
1107 void
1108 InOrderCPU::suspendThread(ThreadID tid)
1109 {
1110 DPRINTF(InOrderCPU, "[tid:%i]: Placing on Suspended Threads List...\n",
1111 tid);
1112 deactivateThread(tid);
1113 suspendedThreads.push_back(tid);
1114 thread[tid]->lastSuspend = curTick();
1115
1116 tcBase(tid)->setStatus(ThreadContext::Suspended);
1117 }
1118
1119 void
1120 InOrderCPU::squashThreadInPipeline(ThreadID tid)
1121 {
1122 //Squash all instructions in each stage
1123 for (int stNum=NumStages - 1; stNum >= 0 ; stNum--) {
1124 pipelineStage[stNum]->squash(0 /*seq_num*/, tid);
1125 }
1126 }
1127
1128 PipelineStage*
1129 InOrderCPU::getPipeStage(int stage_num)
1130 {
1131 return pipelineStage[stage_num];
1132 }
1133
1134 RegIndex
1135 InOrderCPU::flattenRegIdx(RegIndex reg_idx, RegType &reg_type, ThreadID tid)
1136 {
1137 if (reg_idx < FP_Base_DepTag) {
1138 reg_type = IntType;
1139 return isa[tid].flattenIntIndex(reg_idx);
1140 } else if (reg_idx < Ctrl_Base_DepTag) {
1141 reg_type = FloatType;
1142 reg_idx -= FP_Base_DepTag;
1143 return isa[tid].flattenFloatIndex(reg_idx);
1144 } else {
1145 reg_type = MiscType;
1146 return reg_idx - TheISA::Ctrl_Base_DepTag;
1147 }
1148 }
1149
1150 uint64_t
1151 InOrderCPU::readIntReg(RegIndex reg_idx, ThreadID tid)
1152 {
1153 DPRINTF(IntRegs, "[tid:%i]: Reading Int. Reg %i as %x\n",
1154 tid, reg_idx, intRegs[tid][reg_idx]);
1155
1156 return intRegs[tid][reg_idx];
1157 }
1158
1159 FloatReg
1160 InOrderCPU::readFloatReg(RegIndex reg_idx, ThreadID tid)
1161 {
1162 return floatRegs.f[tid][reg_idx];
1163 }
1164
1165 FloatRegBits
1166 InOrderCPU::readFloatRegBits(RegIndex reg_idx, ThreadID tid)
1167 {;
1168 return floatRegs.i[tid][reg_idx];
1169 }
1170
1171 void
1172 InOrderCPU::setIntReg(RegIndex reg_idx, uint64_t val, ThreadID tid)
1173 {
1174 if (reg_idx == TheISA::ZeroReg) {
1175 DPRINTF(IntRegs, "[tid:%i]: Ignoring Setting of ISA-ZeroReg "
1176 "(Int. Reg %i) to %x\n", tid, reg_idx, val);
1177 return;
1178 } else {
1179 DPRINTF(IntRegs, "[tid:%i]: Setting Int. Reg %i to %x\n",
1180 tid, reg_idx, val);
1181
1182 intRegs[tid][reg_idx] = val;
1183 }
1184 }
1185
1186
1187 void
1188 InOrderCPU::setFloatReg(RegIndex reg_idx, FloatReg val, ThreadID tid)
1189 {
1190 floatRegs.f[tid][reg_idx] = val;
1191 }
1192
1193
1194 void
1195 InOrderCPU::setFloatRegBits(RegIndex reg_idx, FloatRegBits val, ThreadID tid)
1196 {
1197 floatRegs.i[tid][reg_idx] = val;
1198 }
1199
1200 uint64_t
1201 InOrderCPU::readRegOtherThread(unsigned reg_idx, ThreadID tid)
1202 {
1203 // If Default value is set, then retrieve target thread
1204 if (tid == InvalidThreadID) {
1205 tid = TheISA::getTargetThread(tcBase(tid));
1206 }
1207
1208 if (reg_idx < FP_Base_DepTag) {
1209 // Integer Register File
1210 return readIntReg(reg_idx, tid);
1211 } else if (reg_idx < Ctrl_Base_DepTag) {
1212 // Float Register File
1213 reg_idx -= FP_Base_DepTag;
1214 return readFloatRegBits(reg_idx, tid);
1215 } else {
1216 reg_idx -= Ctrl_Base_DepTag;
1217 return readMiscReg(reg_idx, tid); // Misc. Register File
1218 }
1219 }
1220 void
1221 InOrderCPU::setRegOtherThread(unsigned reg_idx, const MiscReg &val,
1222 ThreadID tid)
1223 {
1224 // If Default value is set, then retrieve target thread
1225 if (tid == InvalidThreadID) {
1226 tid = TheISA::getTargetThread(tcBase(tid));
1227 }
1228
1229 if (reg_idx < FP_Base_DepTag) { // Integer Register File
1230 setIntReg(reg_idx, val, tid);
1231 } else if (reg_idx < Ctrl_Base_DepTag) { // Float Register File
1232 reg_idx -= FP_Base_DepTag;
1233 setFloatRegBits(reg_idx, val, tid);
1234 } else {
1235 reg_idx -= Ctrl_Base_DepTag;
1236 setMiscReg(reg_idx, val, tid); // Misc. Register File
1237 }
1238 }
1239
1240 MiscReg
1241 InOrderCPU::readMiscRegNoEffect(int misc_reg, ThreadID tid)
1242 {
1243 return isa[tid].readMiscRegNoEffect(misc_reg);
1244 }
1245
1246 MiscReg
1247 InOrderCPU::readMiscReg(int misc_reg, ThreadID tid)
1248 {
1249 return isa[tid].readMiscReg(misc_reg, tcBase(tid));
1250 }
1251
1252 void
1253 InOrderCPU::setMiscRegNoEffect(int misc_reg, const MiscReg &val, ThreadID tid)
1254 {
1255 isa[tid].setMiscRegNoEffect(misc_reg, val);
1256 }
1257
1258 void
1259 InOrderCPU::setMiscReg(int misc_reg, const MiscReg &val, ThreadID tid)
1260 {
1261 isa[tid].setMiscReg(misc_reg, val, tcBase(tid));
1262 }
1263
1264
1265 InOrderCPU::ListIt
1266 InOrderCPU::addInst(DynInstPtr inst)
1267 {
1268 ThreadID tid = inst->readTid();
1269
1270 instList[tid].push_back(inst);
1271
1272 return --(instList[tid].end());
1273 }
1274
1275 InOrderCPU::ListIt
1276 InOrderCPU::findInst(InstSeqNum seq_num, ThreadID tid)
1277 {
1278 ListIt it = instList[tid].begin();
1279 ListIt end = instList[tid].end();
1280
1281 while (it != end) {
1282 if ((*it)->seqNum == seq_num)
1283 return it;
1284 else if ((*it)->seqNum > seq_num)
1285 break;
1286
1287 it++;
1288 }
1289
1290 return instList[tid].end();
1291 }
1292
1293 void
1294 InOrderCPU::updateContextSwitchStats()
1295 {
1296 // Set Average Stat Here, then reset to 0
1297 instsPerCtxtSwitch = instsPerSwitch;
1298 instsPerSwitch = 0;
1299 }
1300
1301
1302 void
1303 InOrderCPU::instDone(DynInstPtr inst, ThreadID tid)
1304 {
1305 // Set the nextPC to be fetched if this is the last instruction
1306 // committed
1307 // ========
1308 // This contributes to the precise state of the CPU
1309 // which can be used when restoring a thread to the CPU after after any
1310 // type of context switching activity (fork, exception, etc.)
1311 TheISA::PCState comm_pc = inst->pcState();
1312 lastCommittedPC[tid] = comm_pc;
1313 TheISA::advancePC(comm_pc, inst->staticInst);
1314 pcState(comm_pc, tid);
1315
1316 //@todo: may be unnecessary with new-ISA-specific branch handling code
1317 if (inst->isControl()) {
1318 thread[tid]->lastGradIsBranch = true;
1319 thread[tid]->lastBranchPC = inst->pcState();
1320 TheISA::advancePC(thread[tid]->lastBranchPC, inst->staticInst);
1321 } else {
1322 thread[tid]->lastGradIsBranch = false;
1323 }
1324
1325
1326 // Finalize Trace Data For Instruction
1327 if (inst->traceData) {
1328 //inst->traceData->setCycle(curTick());
1329 inst->traceData->setFetchSeq(inst->seqNum);
1330 //inst->traceData->setCPSeq(cpu->tcBase(tid)->numInst);
1331 inst->traceData->dump();
1332 delete inst->traceData;
1333 inst->traceData = NULL;
1334 }
1335
1336 // Increment active thread's instruction count
1337 instsPerSwitch++;
1338
1339 // Increment thread-state's instruction count
1340 thread[tid]->numInst++;
1341
1342 // Increment thread-state's instruction stats
1343 thread[tid]->numInsts++;
1344
1345 // Count committed insts per thread stats
1346 committedInsts[tid]++;
1347
1348 // Count total insts committed stat
1349 totalCommittedInsts++;
1350
1351 // Count SMT-committed insts per thread stat
1352 if (numActiveThreads() > 1) {
1353 smtCommittedInsts[tid]++;
1354 }
1355
1356 // Instruction-Mix Stats
1357 if (inst->isLoad()) {
1358 comLoads++;
1359 } else if (inst->isStore()) {
1360 comStores++;
1361 } else if (inst->isControl()) {
1362 comBranches++;
1363 } else if (inst->isNop()) {
1364 comNops++;
1365 } else if (inst->isNonSpeculative()) {
1366 comNonSpec++;
1367 } else if (inst->isInteger()) {
1368 comInts++;
1369 } else if (inst->isFloating()) {
1370 comFloats++;
1371 }
1372
1373 // Check for instruction-count-based events.
1374 comInstEventQueue[tid]->serviceEvents(thread[tid]->numInst);
1375
1376 // Broadcast to other resources an instruction
1377 // has been completed
1378 resPool->scheduleEvent((CPUEventType)ResourcePool::InstGraduated, inst,
1379 0, 0, tid);
1380
1381 // Finally, remove instruction from CPU
1382 removeInst(inst);
1383 }
1384
1385 // currently unused function, but substitute repetitive code w/this function
1386 // call
1387 void
1388 InOrderCPU::addToRemoveList(DynInstPtr inst)
1389 {
1390 removeInstsThisCycle = true;
1391 if (!inst->isRemoveList()) {
1392 DPRINTF(InOrderCPU, "Pushing instruction [tid:%i] PC %s "
1393 "[sn:%lli] to remove list\n",
1394 inst->threadNumber, inst->pcState(), inst->seqNum);
1395 inst->setRemoveList();
1396 removeList.push(inst->getInstListIt());
1397 } else {
1398 DPRINTF(InOrderCPU, "Ignoring instruction removal for [tid:%i] PC %s "
1399 "[sn:%lli], already remove list\n",
1400 inst->threadNumber, inst->pcState(), inst->seqNum);
1401 }
1402
1403 }
1404
1405 void
1406 InOrderCPU::removeInst(DynInstPtr inst)
1407 {
1408 DPRINTF(InOrderCPU, "Removing graduated instruction [tid:%i] PC %s "
1409 "[sn:%lli]\n",
1410 inst->threadNumber, inst->pcState(), inst->seqNum);
1411
1412 removeInstsThisCycle = true;
1413
1414 // Remove the instruction.
1415 if (!inst->isRemoveList()) {
1416 DPRINTF(InOrderCPU, "Pushing instruction [tid:%i] PC %s "
1417 "[sn:%lli] to remove list\n",
1418 inst->threadNumber, inst->pcState(), inst->seqNum);
1419 inst->setRemoveList();
1420 removeList.push(inst->getInstListIt());
1421 } else {
1422 DPRINTF(InOrderCPU, "Ignoring instruction removal for [tid:%i] PC %s "
1423 "[sn:%lli], already on remove list\n",
1424 inst->threadNumber, inst->pcState(), inst->seqNum);
1425 }
1426
1427 }
1428
1429 void
1430 InOrderCPU::removeInstsUntil(const InstSeqNum &seq_num, ThreadID tid)
1431 {
1432 //assert(!instList[tid].empty());
1433
1434 removeInstsThisCycle = true;
1435
1436 ListIt inst_iter = instList[tid].end();
1437
1438 inst_iter--;
1439
1440 DPRINTF(InOrderCPU, "Squashing instructions from CPU instruction "
1441 "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
1442 tid, seq_num, (*inst_iter)->seqNum);
1443
1444 while ((*inst_iter)->seqNum > seq_num) {
1445
1446 bool break_loop = (inst_iter == instList[tid].begin());
1447
1448 squashInstIt(inst_iter, tid);
1449
1450 inst_iter--;
1451
1452 if (break_loop)
1453 break;
1454 }
1455 }
1456
1457
1458 inline void
1459 InOrderCPU::squashInstIt(const ListIt &instIt, ThreadID tid)
1460 {
1461 if ((*instIt)->threadNumber == tid) {
1462 DPRINTF(InOrderCPU, "Squashing instruction, "
1463 "[tid:%i] [sn:%lli] PC %s\n",
1464 (*instIt)->threadNumber,
1465 (*instIt)->seqNum,
1466 (*instIt)->pcState());
1467
1468 (*instIt)->setSquashed();
1469
1470 if (!(*instIt)->isRemoveList()) {
1471 DPRINTF(InOrderCPU, "Pushing instruction [tid:%i] PC %s "
1472 "[sn:%lli] to remove list\n",
1473 (*instIt)->threadNumber, (*instIt)->pcState(),
1474 (*instIt)->seqNum);
1475 (*instIt)->setRemoveList();
1476 removeList.push(instIt);
1477 } else {
1478 DPRINTF(InOrderCPU, "Ignoring instruction removal for [tid:%i]"
1479 " PC %s [sn:%lli], already on remove list\n",
1480 (*instIt)->threadNumber, (*instIt)->pcState(),
1481 (*instIt)->seqNum);
1482 }
1483
1484 }
1485
1486 }
1487
1488
1489 void
1490 InOrderCPU::cleanUpRemovedInsts()
1491 {
1492 while (!removeList.empty()) {
1493 DPRINTF(InOrderCPU, "Removing instruction, "
1494 "[tid:%i] [sn:%lli] PC %s\n",
1495 (*removeList.front())->threadNumber,
1496 (*removeList.front())->seqNum,
1497 (*removeList.front())->pcState());
1498
1499 DynInstPtr inst = *removeList.front();
1500 ThreadID tid = inst->threadNumber;
1501
1502 // Remove From Register Dependency Map, If Necessary
1503 archRegDepMap[tid].remove(inst);
1504
1505 // Clear if Non-Speculative
1506 if (inst->staticInst &&
1507 inst->seqNum == nonSpecSeqNum[tid] &&
1508 nonSpecInstActive[tid] == true) {
1509 nonSpecInstActive[tid] = false;
1510 }
1511
1512 inst->onInstList = false;
1513
1514 instList[tid].erase(removeList.front());
1515
1516 removeList.pop();
1517 }
1518
1519 removeInstsThisCycle = false;
1520 }
1521
1522 void
1523 InOrderCPU::cleanUpRemovedEvents()
1524 {
1525 while (!cpuEventRemoveList.empty()) {
1526 Event *cpu_event = cpuEventRemoveList.front();
1527 cpuEventRemoveList.pop();
1528 delete cpu_event;
1529 }
1530 }
1531
1532
1533 void
1534 InOrderCPU::dumpInsts()
1535 {
1536 int num = 0;
1537
1538 ListIt inst_list_it = instList[0].begin();
1539
1540 cprintf("Dumping Instruction List\n");
1541
1542 while (inst_list_it != instList[0].end()) {
1543 cprintf("Instruction:%i\nPC:%s\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
1544 "Squashed:%i\n\n",
1545 num, (*inst_list_it)->pcState(),
1546 (*inst_list_it)->threadNumber,
1547 (*inst_list_it)->seqNum, (*inst_list_it)->isIssued(),
1548 (*inst_list_it)->isSquashed());
1549 inst_list_it++;
1550 ++num;
1551 }
1552 }
1553
1554 void
1555 InOrderCPU::wakeCPU()
1556 {
1557 if (/*activityRec.active() || */tickEvent.scheduled()) {
1558 DPRINTF(Activity, "CPU already running.\n");
1559 return;
1560 }
1561
1562 DPRINTF(Activity, "Waking up CPU\n");
1563
1564 Tick extra_cycles = tickToCycles((curTick() - 1) - lastRunningCycle);
1565
1566 idleCycles += extra_cycles;
1567 for (int stage_num = 0; stage_num < NumStages; stage_num++) {
1568 pipelineStage[stage_num]->idleCycles += extra_cycles;
1569 }
1570
1571 numCycles += extra_cycles;
1572
1573 schedule(&tickEvent, nextCycle(curTick()));
1574 }
1575
1576 #if FULL_SYSTEM
1577
1578 void
1579 InOrderCPU::wakeup()
1580 {
1581 if (thread[0]->status() != ThreadContext::Suspended)
1582 return;
1583
1584 wakeCPU();
1585
1586 DPRINTF(Quiesce, "Suspended Processor woken\n");
1587 threadContexts[0]->activate();
1588 }
1589 #endif
1590
1591 #if !FULL_SYSTEM
1592 void
1593 InOrderCPU::syscall(int64_t callnum, ThreadID tid)
1594 {
1595 DPRINTF(InOrderCPU, "[tid:%i] Executing syscall().\n\n", tid);
1596
1597 DPRINTF(Activity,"Activity: syscall() called.\n");
1598
1599 // Temporarily increase this by one to account for the syscall
1600 // instruction.
1601 ++(this->thread[tid]->funcExeInst);
1602
1603 // Execute the actual syscall.
1604 this->thread[tid]->syscall(callnum);
1605
1606 // Decrease funcExeInst by one as the normal commit will handle
1607 // incrementing it.
1608 --(this->thread[tid]->funcExeInst);
1609
1610 // Clear Non-Speculative Block Variable
1611 nonSpecInstActive[tid] = false;
1612 }
1613 #endif
1614
1615 TheISA::TLB*
1616 InOrderCPU::getITBPtr()
1617 {
1618 CacheUnit *itb_res =
1619 dynamic_cast<CacheUnit*>(resPool->getResource(fetchPortIdx));
1620 return itb_res->tlb();
1621 }
1622
1623
1624 TheISA::TLB*
1625 InOrderCPU::getDTBPtr()
1626 {
1627 CacheUnit *dtb_res =
1628 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1629 return dtb_res->tlb();
1630 }
1631
1632 Fault
1633 InOrderCPU::read(DynInstPtr inst, Addr addr,
1634 uint8_t *data, unsigned size, unsigned flags)
1635 {
1636 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1637 // you want to run w/out caches?
1638 CacheUnit *cache_res =
1639 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1640
1641 return cache_res->read(inst, addr, data, size, flags);
1642 }
1643
1644 Fault
1645 InOrderCPU::write(DynInstPtr inst, uint8_t *data, unsigned size,
1646 Addr addr, unsigned flags, uint64_t *write_res)
1647 {
1648 //@TODO: Generalize name "CacheUnit" to "MemUnit" just in case
1649 // you want to run w/out caches?
1650 CacheUnit *cache_res =
1651 dynamic_cast<CacheUnit*>(resPool->getResource(dataPortIdx));
1652 return cache_res->write(inst, data, size, addr, flags, write_res);
1653 }